1
0
mirror of https://github.com/lua/lua.git synced 2025-01-28 06:03:00 +08:00

better code for unary/binary operators

This commit is contained in:
Roberto Ierusalimschy 2000-08-09 11:49:13 -03:00
parent a7c1390ffa
commit 5f22f8961c
3 changed files with 117 additions and 74 deletions

84
lcode.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lcode.c,v 1.43 2000/08/08 18:26:05 roberto Exp roberto $
** $Id: lcode.c,v 1.44 2000/08/08 20:42:07 roberto Exp roberto $
** Code generator for Lua
** See Copyright Notice in lua.h
*/
@ -348,9 +348,9 @@ void luaK_tostack (LexState *ls, expdesc *v, int onlyone) {
}
void luaK_prefix (LexState *ls, int op, expdesc *v) {
void luaK_prefix (LexState *ls, UnOpr op, expdesc *v) {
FuncState *fs = ls->fs;
if (op == '-') {
if (op == OPR_MINUS) {
luaK_tostack(ls, v, 1);
luaK_code0(fs, OP_MINUS);
}
@ -368,46 +368,54 @@ void luaK_prefix (LexState *ls, int op, expdesc *v) {
}
void luaK_infix (LexState *ls, int op, expdesc *v) {
void luaK_infix (LexState *ls, BinOpr op, expdesc *v) {
FuncState *fs = ls->fs;
if (op == TK_AND)
luaK_goiftrue(fs, v, 1);
else if (op == TK_OR)
luaK_goiffalse(fs, v, 1);
else
luaK_tostack(ls, v, 1); /* all other binary operators need a value */
switch (op) {
case OPR_AND:
luaK_goiftrue(fs, v, 1);
break;
case OPR_OR:
luaK_goiffalse(fs, v, 1);
break;
default:
luaK_tostack(ls, v, 1); /* all other binary operators need a value */
}
}
void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) {
static const struct {
OpCode opcode; /* opcode for each binary operator */
int arg; /* default argument for the opcode */
} codes[] = { /* ORDER OPR */
{OP_ADD, 0}, {OP_SUB, 0}, {OP_MULT, 0}, {OP_DIV, 0},
{OP_POW, 0}, {OP_CONCAT, 2},
{OP_JMPNE, NO_JUMP}, {OP_JMPEQ, NO_JUMP},
{OP_JMPLT, NO_JUMP}, {OP_JMPLE, NO_JUMP},
{OP_JMPGT, NO_JUMP}, {OP_JMPGE, NO_JUMP}
};
void luaK_posfix (LexState *ls, BinOpr op, expdesc *v1, expdesc *v2) {
FuncState *fs = ls->fs;
if (op == TK_AND) {
LUA_ASSERT(v1->u.l.t == NO_JUMP, "list must be closed");
discharge1(fs, v2);
v1->u.l.t = v2->u.l.t;
luaK_concat(fs, &v1->u.l.f, v2->u.l.f);
}
else if (op == TK_OR) {
LUA_ASSERT(v1->u.l.f == NO_JUMP, "list must be closed");
discharge1(fs, v2);
v1->u.l.f = v2->u.l.f;
luaK_concat(fs, &v1->u.l.t, v2->u.l.t);
}
else {
luaK_tostack(ls, v2, 1); /* `v2' must be a value */
switch (op) {
case '+': luaK_code0(fs, OP_ADD); break;
case '-': luaK_code0(fs, OP_SUB); break;
case '*': luaK_code0(fs, OP_MULT); break;
case '/': luaK_code0(fs, OP_DIV); break;
case '^': luaK_code0(fs, OP_POW); break;
case TK_CONCAT: luaK_code1(fs, OP_CONCAT, 2); break;
case TK_EQ: luaK_code1(fs, OP_JMPEQ, NO_JUMP); break;
case TK_NE: luaK_code1(fs, OP_JMPNE, NO_JUMP); break;
case '>': luaK_code1(fs, OP_JMPGT, NO_JUMP); break;
case '<': luaK_code1(fs, OP_JMPLT, NO_JUMP); break;
case TK_GE: luaK_code1(fs, OP_JMPGE, NO_JUMP); break;
case TK_LE: luaK_code1(fs, OP_JMPLE, NO_JUMP); break;
switch (op) {
case OPR_AND: {
LUA_ASSERT(v1->u.l.t == NO_JUMP, "list must be closed");
discharge1(fs, v2);
v1->u.l.t = v2->u.l.t;
luaK_concat(fs, &v1->u.l.f, v2->u.l.f);
break;
}
case OPR_OR: {
LUA_ASSERT(v1->u.l.f == NO_JUMP, "list must be closed");
discharge1(fs, v2);
v1->u.l.f = v2->u.l.f;
luaK_concat(fs, &v1->u.l.t, v2->u.l.t);
break;
}
default: {
luaK_tostack(ls, v2, 1); /* `v2' must be a value */
luaK_code1(fs, codes[op].opcode, codes[op].arg);
}
}
}

22
lcode.h
View File

@ -1,5 +1,5 @@
/*
** $Id: lcode.h,v 1.14 2000/06/16 17:51:40 roberto Exp roberto $
** $Id: lcode.h,v 1.15 2000/06/28 20:20:36 roberto Exp roberto $
** Code generator for Lua
** See Copyright Notice in lua.h
*/
@ -20,6 +20,20 @@
#define NO_JUMP (-1)
/*
** grep "ORDER OPR" if you change these enums
*/
typedef enum BinOpr {
OPR_ADD, OPR_SUB, OPR_MULT, OPR_DIV, OPR_POW,
OPR_CONCAT,
OPR_NE, OPR_EQ, OPR_LT, OPR_LE, OPR_GT, OPR_GE,
OPR_AND, OPR_OR,
OPR_NOBINOPR
} BinOpr;
typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_NOUNOPR } UnOpr;
enum Mode {iO, iU, iS, iAB}; /* instruction format */
#define VD 100 /* flag for variable delta */
@ -48,9 +62,9 @@ int luaK_lastisopen (FuncState *fs);
void luaK_setcallreturns (FuncState *fs, int nresults);
void luaK_tostack (LexState *ls, expdesc *v, int onlyone);
void luaK_storevar (LexState *ls, const expdesc *var);
void luaK_prefix (LexState *ls, int op, expdesc *v);
void luaK_infix (LexState *ls, int op, expdesc *v);
void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2);
void luaK_prefix (LexState *ls, UnOpr op, expdesc *v);
void luaK_infix (LexState *ls, BinOpr op, expdesc *v);
void luaK_posfix (LexState *ls, BinOpr op, expdesc *v1, expdesc *v2);
#endif

View File

@ -1,5 +1,5 @@
/*
** $Id: lparser.c,v 1.104 2000/08/08 20:42:07 roberto Exp roberto $
** $Id: lparser.c,v 1.105 2000/08/08 20:48:55 roberto Exp roberto $
** LL(1) Parser and code generator for Lua
** See Copyright Notice in lua.h
*/
@ -670,55 +670,76 @@ static void exp1 (LexState *ls) {
}
/*
** gets priorities of an operator. Returns the priority to the left, and
** sets `rp' to the priority to the right.
*/
static int get_priority (int op, int *rp) {
static UnOpr getunopr (int op) {
switch (op) {
case '^': *rp = 8; return 9; /* right associative */
#define UNARY_PRIORITY 7
case '*': case '/': *rp = 6; return 6;
case '+': case '-': *rp = 5; return 5;
case TK_CONCAT: *rp = 3; return 4; /* right associative (?) */
case TK_EQ: case TK_NE: case '>': case '<': case TK_LE: case TK_GE:
*rp = 2; return 2;
case TK_AND: case TK_OR: *rp = 1; return 1;
default: *rp = -1; return -1;
case TK_NOT: return OPR_NOT;
case '-': return OPR_MINUS;
default: return OPR_NOUNOPR;
}
}
static BinOpr getbinopr (int op) {
switch (op) {
case '+': return OPR_ADD;
case '-': return OPR_SUB;
case '*': return OPR_MULT;
case '/': return OPR_DIV;
case '^': return OPR_POW;
case TK_CONCAT: return OPR_CONCAT;
case TK_NE: return OPR_NE;
case TK_EQ: return OPR_EQ;
case '<': return OPR_LT;
case TK_LE: return OPR_LE;
case '>': return OPR_GT;
case TK_GE: return OPR_GE;
case TK_AND: return OPR_AND;
case TK_OR: return OPR_OR;
default: return OPR_NOBINOPR;
}
}
static const struct {
char left; /* left priority for each binary operator */
char right; /* right priority */
} priority[] = { /* ORDER OPR */
{5, 5}, {5, 5}, {6, 6}, {6, 6}, /* arithmetic */
{9, 8}, {4, 3}, /* power and concat (right associative) */
{2, 2}, {2, 2}, /* equality */
{2, 2}, {2, 2}, {2, 2}, {2, 2}, /* order */
{1, 1}, {1, 1} /* logical */
};
#define UNARY_PRIORITY 7 /* priority for unary operators */
/*
** subexpr -> (simplexep | (NOT | '-') subexpr) { binop subexpr }
** subexpr -> (simplexep | unop subexpr) { binop subexpr }
** where `binop' is any binary operator with a priority higher than `limit'
*/
static void subexpr (LexState *ls, expdesc *v, int limit) {
int rp;
if (ls->t.token == '-' || ls->t.token == TK_NOT) {
int op = ls->t.token; /* operator */
static BinOpr subexpr (LexState *ls, expdesc *v, int limit) {
BinOpr op;
UnOpr uop = getunopr(ls->t.token);
if (uop != OPR_NOUNOPR) {
next(ls);
subexpr(ls, v, UNARY_PRIORITY);
luaK_prefix(ls, op, v);
luaK_prefix(ls, uop, v);
}
else simpleexp(ls, v);
/* expand while operators have priorities higher than `limit' */
while (get_priority(ls->t.token, &rp) > limit) {
op = getbinopr(ls->t.token);
while (op != OPR_NOBINOPR && priority[op].left > limit) {
expdesc v2;
int op = ls->t.token; /* current operator (with priority == `rp') */
BinOpr nextop;
next(ls);
luaK_infix(ls, op, v);
subexpr(ls, &v2, rp); /* read sub-expression with priority > `rp' */
/* read sub-expression with higher priority */
nextop = subexpr(ls, &v2, priority[op].right);
luaK_posfix(ls, op, v, &v2);
op = nextop;
}
return op; /* return first untreated operator */
}