diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-08-09 11:49:13 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-08-09 11:49:13 -0300 |
commit | 5f22f8961c80ce36297f086ae31d87bbb6897c97 (patch) | |
tree | 61837abde7ea51dfe61140734af716437253f605 /lcode.c | |
parent | a7c1390ffaf0edfe839d075df484f2b81b3151bf (diff) | |
download | lua-5f22f8961c80ce36297f086ae31d87bbb6897c97.tar.gz lua-5f22f8961c80ce36297f086ae31d87bbb6897c97.tar.bz2 lua-5f22f8961c80ce36297f086ae31d87bbb6897c97.zip |
better code for unary/binary operators
Diffstat (limited to 'lcode.c')
-rw-r--r-- | lcode.c | 84 |
1 files changed, 46 insertions, 38 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lcode.c,v 1.43 2000/08/08 18:26:05 roberto Exp roberto $ | 2 | ** $Id: lcode.c,v 1.44 2000/08/08 20:42:07 roberto Exp roberto $ |
3 | ** Code generator for Lua | 3 | ** Code generator for Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -348,9 +348,9 @@ void luaK_tostack (LexState *ls, expdesc *v, int onlyone) { | |||
348 | } | 348 | } |
349 | 349 | ||
350 | 350 | ||
351 | void luaK_prefix (LexState *ls, int op, expdesc *v) { | 351 | void luaK_prefix (LexState *ls, UnOpr op, expdesc *v) { |
352 | FuncState *fs = ls->fs; | 352 | FuncState *fs = ls->fs; |
353 | if (op == '-') { | 353 | if (op == OPR_MINUS) { |
354 | luaK_tostack(ls, v, 1); | 354 | luaK_tostack(ls, v, 1); |
355 | luaK_code0(fs, OP_MINUS); | 355 | luaK_code0(fs, OP_MINUS); |
356 | } | 356 | } |
@@ -368,46 +368,54 @@ void luaK_prefix (LexState *ls, int op, expdesc *v) { | |||
368 | } | 368 | } |
369 | 369 | ||
370 | 370 | ||
371 | void luaK_infix (LexState *ls, int op, expdesc *v) { | 371 | void luaK_infix (LexState *ls, BinOpr op, expdesc *v) { |
372 | FuncState *fs = ls->fs; | 372 | FuncState *fs = ls->fs; |
373 | if (op == TK_AND) | 373 | switch (op) { |
374 | luaK_goiftrue(fs, v, 1); | 374 | case OPR_AND: |
375 | else if (op == TK_OR) | 375 | luaK_goiftrue(fs, v, 1); |
376 | luaK_goiffalse(fs, v, 1); | 376 | break; |
377 | else | 377 | case OPR_OR: |
378 | luaK_tostack(ls, v, 1); /* all other binary operators need a value */ | 378 | luaK_goiffalse(fs, v, 1); |
379 | break; | ||
380 | default: | ||
381 | luaK_tostack(ls, v, 1); /* all other binary operators need a value */ | ||
382 | } | ||
379 | } | 383 | } |
380 | 384 | ||
381 | 385 | ||
382 | void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) { | 386 | |
387 | static const struct { | ||
388 | OpCode opcode; /* opcode for each binary operator */ | ||
389 | int arg; /* default argument for the opcode */ | ||
390 | } codes[] = { /* ORDER OPR */ | ||
391 | {OP_ADD, 0}, {OP_SUB, 0}, {OP_MULT, 0}, {OP_DIV, 0}, | ||
392 | {OP_POW, 0}, {OP_CONCAT, 2}, | ||
393 | {OP_JMPNE, NO_JUMP}, {OP_JMPEQ, NO_JUMP}, | ||
394 | {OP_JMPLT, NO_JUMP}, {OP_JMPLE, NO_JUMP}, | ||
395 | {OP_JMPGT, NO_JUMP}, {OP_JMPGE, NO_JUMP} | ||
396 | }; | ||
397 | |||
398 | |||
399 | void luaK_posfix (LexState *ls, BinOpr op, expdesc *v1, expdesc *v2) { | ||
383 | FuncState *fs = ls->fs; | 400 | FuncState *fs = ls->fs; |
384 | if (op == TK_AND) { | 401 | switch (op) { |
385 | LUA_ASSERT(v1->u.l.t == NO_JUMP, "list must be closed"); | 402 | case OPR_AND: { |
386 | discharge1(fs, v2); | 403 | LUA_ASSERT(v1->u.l.t == NO_JUMP, "list must be closed"); |
387 | v1->u.l.t = v2->u.l.t; | 404 | discharge1(fs, v2); |
388 | luaK_concat(fs, &v1->u.l.f, v2->u.l.f); | 405 | v1->u.l.t = v2->u.l.t; |
389 | } | 406 | luaK_concat(fs, &v1->u.l.f, v2->u.l.f); |
390 | else if (op == TK_OR) { | 407 | break; |
391 | LUA_ASSERT(v1->u.l.f == NO_JUMP, "list must be closed"); | 408 | } |
392 | discharge1(fs, v2); | 409 | case OPR_OR: { |
393 | v1->u.l.f = v2->u.l.f; | 410 | LUA_ASSERT(v1->u.l.f == NO_JUMP, "list must be closed"); |
394 | luaK_concat(fs, &v1->u.l.t, v2->u.l.t); | 411 | discharge1(fs, v2); |
395 | } | 412 | v1->u.l.f = v2->u.l.f; |
396 | else { | 413 | luaK_concat(fs, &v1->u.l.t, v2->u.l.t); |
397 | luaK_tostack(ls, v2, 1); /* `v2' must be a value */ | 414 | break; |
398 | switch (op) { | 415 | } |
399 | case '+': luaK_code0(fs, OP_ADD); break; | 416 | default: { |
400 | case '-': luaK_code0(fs, OP_SUB); break; | 417 | luaK_tostack(ls, v2, 1); /* `v2' must be a value */ |
401 | case '*': luaK_code0(fs, OP_MULT); break; | 418 | luaK_code1(fs, codes[op].opcode, codes[op].arg); |
402 | case '/': luaK_code0(fs, OP_DIV); break; | ||
403 | case '^': luaK_code0(fs, OP_POW); break; | ||
404 | case TK_CONCAT: luaK_code1(fs, OP_CONCAT, 2); break; | ||
405 | case TK_EQ: luaK_code1(fs, OP_JMPEQ, NO_JUMP); break; | ||
406 | case TK_NE: luaK_code1(fs, OP_JMPNE, NO_JUMP); break; | ||
407 | case '>': luaK_code1(fs, OP_JMPGT, NO_JUMP); break; | ||
408 | case '<': luaK_code1(fs, OP_JMPLT, NO_JUMP); break; | ||
409 | case TK_GE: luaK_code1(fs, OP_JMPGE, NO_JUMP); break; | ||
410 | case TK_LE: luaK_code1(fs, OP_JMPLE, NO_JUMP); break; | ||
411 | } | 419 | } |
412 | } | 420 | } |
413 | } | 421 | } |