diff options
Diffstat (limited to 'lcode.c')
-rw-r--r-- | lcode.c | 92 |
1 files changed, 42 insertions, 50 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lcode.c,v 1.18 2000/03/24 17:26:08 roberto Exp roberto $ | 2 | ** $Id: lcode.c,v 1.19 2000/04/04 20:48:44 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 | */ |
@@ -23,6 +23,18 @@ void luaK_error (LexState *ls, const char *msg) { | |||
23 | luaX_error(ls, msg, ls->token); | 23 | luaX_error(ls, msg, ls->token); |
24 | } | 24 | } |
25 | 25 | ||
26 | /* | ||
27 | ** Returns the the previous instruction, for optimizations. | ||
28 | ** If there is a jump target between this and the current instruction, | ||
29 | ** returns a dummy instruction to avoid wrong optimizations. | ||
30 | */ | ||
31 | static Instruction previous_instruction (FuncState *fs) { | ||
32 | if (fs->pc > fs->lasttarget) /* no jumps to current position? */ | ||
33 | return fs->f->code[fs->pc-1]; /* returns previous instruction */ | ||
34 | else | ||
35 | return CREATE_0(OP_END); /* no optimizations after an `END' */ | ||
36 | } | ||
37 | |||
26 | 38 | ||
27 | int luaK_code (FuncState *fs, Instruction i, int delta) { | 39 | int luaK_code (FuncState *fs, Instruction i, int delta) { |
28 | luaK_deltastack(fs, delta); | 40 | luaK_deltastack(fs, delta); |
@@ -48,19 +60,6 @@ int luaK_AB(FuncState *fs, OpCode o, int a, int b, int d) { | |||
48 | } | 60 | } |
49 | 61 | ||
50 | 62 | ||
51 | /* | ||
52 | ** Returns the the previous instruction, for optimizations. | ||
53 | ** If there is a jump target between this and the current instruction, | ||
54 | ** returns a dummy instruction to avoid wrong optimizations. | ||
55 | */ | ||
56 | static Instruction previous_instruction (FuncState *fs) { | ||
57 | if (fs->pc > fs->lasttarget) /* no jumps to current position? */ | ||
58 | return fs->f->code[fs->pc-1]; /* returns previous instruction */ | ||
59 | else | ||
60 | return CREATE_0(OP_END); /* no optimizations after an `END' */ | ||
61 | } | ||
62 | |||
63 | |||
64 | static Instruction prepare (FuncState *fs, Instruction i, int delta) { | 63 | static Instruction prepare (FuncState *fs, Instruction i, int delta) { |
65 | Instruction previous = previous_instruction(fs); | 64 | Instruction previous = previous_instruction(fs); |
66 | luaK_code(fs, i, delta); | 65 | luaK_code(fs, i, delta); |
@@ -154,7 +153,7 @@ static void luaK_setlocal (FuncState *fs, int l) { | |||
154 | 153 | ||
155 | static void luaK_eq (FuncState *fs) { | 154 | static void luaK_eq (FuncState *fs) { |
156 | /* PUSHNIL 1; JMPEQ -> NOT (a==nil) */ | 155 | /* PUSHNIL 1; JMPEQ -> NOT (a==nil) */ |
157 | Instruction previous = prepare(fs, CREATE_S(OP_JMPEQ, 0), -2); | 156 | Instruction previous = prepare(fs, CREATE_S(OP_JMPEQ, NO_JUMP), -2); |
158 | if (previous == CREATE_U(OP_PUSHNIL, 1)) { | 157 | if (previous == CREATE_U(OP_PUSHNIL, 1)) { |
159 | setprevious(fs, CREATE_0(OP_NOT)); | 158 | setprevious(fs, CREATE_0(OP_NOT)); |
160 | luaK_deltastack(fs, 1); /* undo delta from `prepare' */ | 159 | luaK_deltastack(fs, 1); /* undo delta from `prepare' */ |
@@ -164,13 +163,18 @@ static void luaK_eq (FuncState *fs) { | |||
164 | 163 | ||
165 | static void luaK_neq (FuncState *fs) { | 164 | static void luaK_neq (FuncState *fs) { |
166 | /* PUSHNIL 1; JMPNEQ -> JMPT (a~=nil) */ | 165 | /* PUSHNIL 1; JMPNEQ -> JMPT (a~=nil) */ |
167 | Instruction previous = prepare(fs, CREATE_S(OP_JMPNEQ, 0), -2); | 166 | Instruction previous = prepare(fs, CREATE_S(OP_JMPNEQ, NO_JUMP), -2); |
168 | if (previous == CREATE_U(OP_PUSHNIL, 1)) { | 167 | if (previous == CREATE_U(OP_PUSHNIL, 1)) { |
169 | setprevious(fs, CREATE_S(OP_JMPT, 0)); | 168 | setprevious(fs, CREATE_S(OP_JMPT, NO_JUMP)); |
170 | } | 169 | } |
171 | } | 170 | } |
172 | 171 | ||
173 | 172 | ||
173 | int luaK_jump (FuncState *fs) { | ||
174 | return luaK_S(fs, OP_JMP, NO_JUMP, 0); | ||
175 | } | ||
176 | |||
177 | |||
174 | void luaK_retcode (FuncState *fs, int nlocals, int nexps) { | 178 | void luaK_retcode (FuncState *fs, int nlocals, int nexps) { |
175 | Instruction previous = prepare(fs, CREATE_U(OP_RETURN, nlocals), 0); | 179 | Instruction previous = prepare(fs, CREATE_U(OP_RETURN, nlocals), 0); |
176 | if (nexps > 0 && GET_OPCODE(previous) == OP_CALL) { | 180 | if (nexps > 0 && GET_OPCODE(previous) == OP_CALL) { |
@@ -192,12 +196,13 @@ static void luaK_pushnil (FuncState *fs, int n) { | |||
192 | } | 196 | } |
193 | 197 | ||
194 | 198 | ||
195 | void luaK_fixjump (FuncState *fs, int pc, int dest) { | 199 | static void luaK_fixjump (FuncState *fs, int pc, int dest) { |
196 | Instruction *jmp = &fs->f->code[pc]; | 200 | Instruction *jmp = &fs->f->code[pc]; |
197 | if (dest == NO_JUMP) | 201 | if (dest == NO_JUMP) |
198 | SETARG_S(*jmp, 0); /* absolute value to represent end of list */ | 202 | SETARG_S(*jmp, NO_JUMP); /* point to itself to represent end of list */ |
199 | else { /* jump is relative to position following jump instruction */ | 203 | else { /* jump is relative to position following jump instruction */ |
200 | int offset = dest-(pc+1); | 204 | int offset = dest-(pc+1); |
205 | LUA_ASSERT(L, offset != NO_JUMP, "cannot link to itself"); | ||
201 | if (abs(offset) > MAXARG_S) | 206 | if (abs(offset) > MAXARG_S) |
202 | luaK_error(fs->ls, "control structure too long"); | 207 | luaK_error(fs->ls, "control structure too long"); |
203 | SETARG_S(*jmp, offset); | 208 | SETARG_S(*jmp, offset); |
@@ -207,7 +212,7 @@ void luaK_fixjump (FuncState *fs, int pc, int dest) { | |||
207 | 212 | ||
208 | static int luaK_getjump (FuncState *fs, int pc) { | 213 | static int luaK_getjump (FuncState *fs, int pc) { |
209 | int offset = GETARG_S(fs->f->code[pc]); | 214 | int offset = GETARG_S(fs->f->code[pc]); |
210 | if (offset == 0) | 215 | if (offset == NO_JUMP) /* point to itself represents end of list */ |
211 | return NO_JUMP; /* end of list */ | 216 | return NO_JUMP; /* end of list */ |
212 | else | 217 | else |
213 | return (pc+1)+offset; /* turn offset into absolute position */ | 218 | return (pc+1)+offset; /* turn offset into absolute position */ |
@@ -225,11 +230,11 @@ int luaK_getlabel (FuncState *fs) { | |||
225 | 230 | ||
226 | 231 | ||
227 | void luaK_deltastack (FuncState *fs, int delta) { | 232 | void luaK_deltastack (FuncState *fs, int delta) { |
228 | fs->stacksize += delta; | 233 | fs->stacklevel += delta; |
229 | if (delta > 0 && fs->stacksize > fs->f->maxstacksize) { | 234 | if (delta > 0 && fs->stacklevel > fs->f->maxstacksize) { |
230 | if (fs->stacksize > MAXSTACK) | 235 | if (fs->stacklevel > MAXSTACK) |
231 | luaK_error(fs->ls, "function or expression too complex"); | 236 | luaK_error(fs->ls, "function or expression too complex"); |
232 | fs->f->maxstacksize = fs->stacksize; | 237 | fs->f->maxstacksize = fs->stacklevel; |
233 | } | 238 | } |
234 | } | 239 | } |
235 | 240 | ||
@@ -357,29 +362,16 @@ static OpCode invertjump (OpCode op) { | |||
357 | } | 362 | } |
358 | 363 | ||
359 | 364 | ||
360 | static void luaK_jump (FuncState *fs, OpCode jump) { | 365 | static void luaK_condjump (FuncState *fs, OpCode jump) { |
361 | Instruction previous = prepare(fs, CREATE_S(jump, 0), -1); | 366 | Instruction previous = prepare(fs, CREATE_S(jump, NO_JUMP), -1); |
362 | switch (GET_OPCODE(previous)) { | 367 | switch (GET_OPCODE(previous)) { |
363 | case OP_NOT: previous = CREATE_S(invertjump(jump), 0); break; | 368 | case OP_NOT: previous = CREATE_S(invertjump(jump), NO_JUMP); break; |
364 | case OP_PUSHNIL: /* optimize `repeat until nil' */ | ||
365 | if (GETARG_U(previous) == 1 && jump == OP_JMPF) { | ||
366 | previous = CREATE_S(OP_JMP, 0); | ||
367 | break; | ||
368 | } | ||
369 | else return; /* do not set previous */ | ||
370 | default: return; | 369 | default: return; |
371 | } | 370 | } |
372 | setprevious(fs, previous); | 371 | setprevious(fs, previous); |
373 | } | 372 | } |
374 | 373 | ||
375 | 374 | ||
376 | static void insert_last (FuncState *fs, int *list) { | ||
377 | int first = *list; | ||
378 | *list = fs->pc-1; /* insert last instruction in the list */ | ||
379 | luaK_fixjump(fs, *list, first); | ||
380 | } | ||
381 | |||
382 | |||
383 | static void luaK_patchlistaux (FuncState *fs, int list, int target, | 375 | static void luaK_patchlistaux (FuncState *fs, int list, int target, |
384 | OpCode special, int special_target) { | 376 | OpCode special, int special_target) { |
385 | Instruction *code = fs->f->code; | 377 | Instruction *code = fs->f->code; |
@@ -414,7 +406,7 @@ static int need_value (FuncState *fs, int list, OpCode hasvalue) { | |||
414 | } | 406 | } |
415 | 407 | ||
416 | 408 | ||
417 | static void concatlists (FuncState *fs, int *l1, int l2) { | 409 | void luaK_concat (FuncState *fs, int *l1, int l2) { |
418 | if (*l1 == NO_JUMP) | 410 | if (*l1 == NO_JUMP) |
419 | *l1 = l2; | 411 | *l1 = l2; |
420 | else { | 412 | else { |
@@ -446,8 +438,8 @@ static void luaK_testgo (FuncState *fs, expdesc *v, int invert, OpCode jump) { | |||
446 | SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous))); | 438 | SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous))); |
447 | } | 439 | } |
448 | else | 440 | else |
449 | luaK_jump(fs, jump); | 441 | luaK_condjump(fs, jump); |
450 | insert_last(fs, exitlist); | 442 | luaK_concat(fs, exitlist, fs->pc-1); /* insert last jump in `exitlist' */ |
451 | luaK_patchlist(fs, *golist, luaK_getlabel(fs)); | 443 | luaK_patchlist(fs, *golist, luaK_getlabel(fs)); |
452 | *golist = NO_JUMP; | 444 | *golist = NO_JUMP; |
453 | } | 445 | } |
@@ -478,7 +470,7 @@ void luaK_tostack (LexState *ls, expdesc *v, int onlyone) { | |||
478 | int p_1 = 0; /* position of an eventual PUSHINT */ | 470 | int p_1 = 0; /* position of an eventual PUSHINT */ |
479 | int final; /* position after whole expression */ | 471 | int final; /* position after whole expression */ |
480 | if (ISJUMP(previous)) { | 472 | if (ISJUMP(previous)) { |
481 | insert_last(fs, &v->u.l.t); /* put `previous' in true list */ | 473 | luaK_concat(fs, &v->u.l.t, fs->pc-1); /* put `previous' in true list */ |
482 | p_nil = luaK_0(fs, OP_PUSHNILJMP, 0); | 474 | p_nil = luaK_0(fs, OP_PUSHNILJMP, 0); |
483 | p_1 = luaK_S(fs, OP_PUSHINT, 1, 1); | 475 | p_1 = luaK_S(fs, OP_PUSHINT, 1, 1); |
484 | } | 476 | } |
@@ -544,13 +536,13 @@ void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) { | |||
544 | LUA_ASSERT(ls->L, v1->u.l.t == NO_JUMP, "list must be closed"); | 536 | LUA_ASSERT(ls->L, v1->u.l.t == NO_JUMP, "list must be closed"); |
545 | discharge1(fs, v2); | 537 | discharge1(fs, v2); |
546 | v1->u.l.t = v2->u.l.t; | 538 | v1->u.l.t = v2->u.l.t; |
547 | concatlists(fs, &v1->u.l.f, v2->u.l.f); | 539 | luaK_concat(fs, &v1->u.l.f, v2->u.l.f); |
548 | } | 540 | } |
549 | else if (op == TK_OR) { | 541 | else if (op == TK_OR) { |
550 | LUA_ASSERT(ls->L, v1->u.l.f == NO_JUMP, "list must be closed"); | 542 | LUA_ASSERT(ls->L, v1->u.l.f == NO_JUMP, "list must be closed"); |
551 | discharge1(fs, v2); | 543 | discharge1(fs, v2); |
552 | v1->u.l.f = v2->u.l.f; | 544 | v1->u.l.f = v2->u.l.f; |
553 | concatlists(fs, &v1->u.l.t, v2->u.l.t); | 545 | luaK_concat(fs, &v1->u.l.t, v2->u.l.t); |
554 | } | 546 | } |
555 | else { | 547 | else { |
556 | luaK_tostack(ls, v2, 1); /* `v2' must be a value */ | 548 | luaK_tostack(ls, v2, 1); /* `v2' must be a value */ |
@@ -563,10 +555,10 @@ void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) { | |||
563 | case TK_CONC: luaK_conc(fs); break; | 555 | case TK_CONC: luaK_conc(fs); break; |
564 | case TK_EQ: luaK_eq(fs); break; | 556 | case TK_EQ: luaK_eq(fs); break; |
565 | case TK_NE: luaK_neq(fs); break; | 557 | case TK_NE: luaK_neq(fs); break; |
566 | case '>': luaK_S(fs, OP_JMPGT, 0, -2); break; | 558 | case '>': luaK_S(fs, OP_JMPGT, NO_JUMP, -2); break; |
567 | case '<': luaK_S(fs, OP_JMPLT, 0, -2); break; | 559 | case '<': luaK_S(fs, OP_JMPLT, NO_JUMP, -2); break; |
568 | case TK_GE: luaK_S(fs, OP_JMPGE, 0, -2); break; | 560 | case TK_GE: luaK_S(fs, OP_JMPGE, NO_JUMP, -2); break; |
569 | case TK_LE: luaK_S(fs, OP_JMPLE, 0, -2); break; | 561 | case TK_LE: luaK_S(fs, OP_JMPLE, NO_JUMP, -2); break; |
570 | } | 562 | } |
571 | } | 563 | } |
572 | } | 564 | } |