diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-04-04 17:48:44 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-04-04 17:48:44 -0300 |
commit | 4e7e9e8de5b9e50694e161653ef03b5c15a53054 (patch) | |
tree | a908866ce0a34b591c92b67143ac581861aeb216 | |
parent | 3e45496295bef65692f690d1633a9ce6ad9408b0 (diff) | |
download | lua-4e7e9e8de5b9e50694e161653ef03b5c15a53054.tar.gz lua-4e7e9e8de5b9e50694e161653ef03b5c15a53054.tar.bz2 lua-4e7e9e8de5b9e50694e161653ef03b5c15a53054.zip |
new opcode INCLOCAL.
-rw-r--r-- | lcode.c | 89 | ||||
-rw-r--r-- | llimits.h | 6 | ||||
-rw-r--r-- | lopcodes.h | 77 | ||||
-rw-r--r-- | lvm.c | 41 |
4 files changed, 132 insertions, 81 deletions
@@ -1,10 +1,12 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lcode.c,v 1.17 2000/03/24 12:18:30 roberto Exp roberto $ | 2 | ** $Id: lcode.c,v 1.18 2000/03/24 17:26:08 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 | */ |
6 | 6 | ||
7 | 7 | ||
8 | #include "stdlib.h" | ||
9 | |||
8 | #define LUA_REENTRANT | 10 | #define LUA_REENTRANT |
9 | 11 | ||
10 | #include "lcode.h" | 12 | #include "lcode.h" |
@@ -73,11 +75,12 @@ static void setprevious (FuncState *fs, Instruction i) { | |||
73 | 75 | ||
74 | 76 | ||
75 | static void luaK_minus (FuncState *fs) { | 77 | static void luaK_minus (FuncState *fs) { |
78 | /* PUSHINT s; MINUS -> PUSHINT -s (-k) */ | ||
79 | /* PUSHNUM u; MINUS -> PUSHNEGNUM u (-k) */ | ||
76 | Instruction previous = prepare(fs, CREATE_0(OP_MINUS), 0); | 80 | Instruction previous = prepare(fs, CREATE_0(OP_MINUS), 0); |
77 | switch(GET_OPCODE(previous)) { | 81 | switch(GET_OPCODE(previous)) { |
78 | case OP_PUSHINT: SETARG_S(previous, -GETARG_S(previous)); break; | 82 | case OP_PUSHINT: SETARG_S(previous, -GETARG_S(previous)); break; |
79 | case OP_PUSHNUM: SET_OPCODE(previous, OP_PUSHNEGNUM); break; | 83 | case OP_PUSHNUM: SET_OPCODE(previous, OP_PUSHNEGNUM); break; |
80 | case OP_PUSHNEGNUM: SET_OPCODE(previous, OP_PUSHNUM); break; | ||
81 | default: return; | 84 | default: return; |
82 | } | 85 | } |
83 | setprevious(fs, previous); | 86 | setprevious(fs, previous); |
@@ -85,6 +88,7 @@ static void luaK_minus (FuncState *fs) { | |||
85 | 88 | ||
86 | 89 | ||
87 | static void luaK_gettable (FuncState *fs) { | 90 | static void luaK_gettable (FuncState *fs) { |
91 | /* PUSHSTRING u; GETTABLE -> GETDOTTED u (t.x) */ | ||
88 | Instruction previous = prepare(fs, CREATE_0(OP_GETTABLE), -1); | 92 | Instruction previous = prepare(fs, CREATE_0(OP_GETTABLE), -1); |
89 | switch(GET_OPCODE(previous)) { | 93 | switch(GET_OPCODE(previous)) { |
90 | case OP_PUSHSTRING: SET_OPCODE(previous, OP_GETDOTTED); break; | 94 | case OP_PUSHSTRING: SET_OPCODE(previous, OP_GETDOTTED); break; |
@@ -95,6 +99,7 @@ static void luaK_gettable (FuncState *fs) { | |||
95 | 99 | ||
96 | 100 | ||
97 | static void luaK_add (FuncState *fs) { | 101 | static void luaK_add (FuncState *fs) { |
102 | /* PUSHINT s; ADD -> ADDI s (a+k) */ | ||
98 | Instruction previous = prepare(fs, CREATE_0(OP_ADD), -1); | 103 | Instruction previous = prepare(fs, CREATE_0(OP_ADD), -1); |
99 | switch(GET_OPCODE(previous)) { | 104 | switch(GET_OPCODE(previous)) { |
100 | case OP_PUSHINT: SET_OPCODE(previous, OP_ADDI); break; | 105 | case OP_PUSHINT: SET_OPCODE(previous, OP_ADDI); break; |
@@ -105,6 +110,7 @@ static void luaK_add (FuncState *fs) { | |||
105 | 110 | ||
106 | 111 | ||
107 | static void luaK_sub (FuncState *fs) { | 112 | static void luaK_sub (FuncState *fs) { |
113 | /* PUSHINT s; SUB -> ADDI -s (a-k) */ | ||
108 | Instruction previous = prepare(fs, CREATE_0(OP_SUB), -1); | 114 | Instruction previous = prepare(fs, CREATE_0(OP_SUB), -1); |
109 | switch(GET_OPCODE(previous)) { | 115 | switch(GET_OPCODE(previous)) { |
110 | case OP_PUSHINT: | 116 | case OP_PUSHINT: |
@@ -118,6 +124,7 @@ static void luaK_sub (FuncState *fs) { | |||
118 | 124 | ||
119 | 125 | ||
120 | static void luaK_conc (FuncState *fs) { | 126 | static void luaK_conc (FuncState *fs) { |
127 | /* CONC u; CONC 2 -> CONC u+1 (a..b..c) */ | ||
121 | Instruction previous = prepare(fs, CREATE_U(OP_CONC, 2), -1); | 128 | Instruction previous = prepare(fs, CREATE_U(OP_CONC, 2), -1); |
122 | switch(GET_OPCODE(previous)) { | 129 | switch(GET_OPCODE(previous)) { |
123 | case OP_CONC: SETARG_U(previous, GETARG_U(previous)+1); break; | 130 | case OP_CONC: SETARG_U(previous, GETARG_U(previous)+1); break; |
@@ -127,8 +134,27 @@ static void luaK_conc (FuncState *fs) { | |||
127 | } | 134 | } |
128 | 135 | ||
129 | 136 | ||
137 | static void luaK_setlocal (FuncState *fs, int l) { | ||
138 | /* PUSHLOCAL l; ADDI k, SETLOCAL l -> INCLOCAL k, l ((local)a=a+k) */ | ||
139 | Instruction *code = fs->f->code; | ||
140 | int pc = fs->pc; | ||
141 | if (pc-1 > fs->lasttarget && /* no jumps in-between instructions? */ | ||
142 | code[pc-2] == CREATE_U(OP_PUSHLOCAL, l) && | ||
143 | GET_OPCODE(code[pc-1]) == OP_ADDI && | ||
144 | abs(GETARG_S(code[pc-1])) <= MAXARG_sA) { | ||
145 | int inc = GETARG_S(code[pc-1]); | ||
146 | fs->pc = pc-1; | ||
147 | code[pc-2] = CREATE_sAB(OP_INCLOCAL, inc, l); | ||
148 | luaK_deltastack(fs, -1); | ||
149 | } | ||
150 | else | ||
151 | luaK_U(fs, OP_SETLOCAL, l, -1); | ||
152 | } | ||
153 | |||
154 | |||
130 | static void luaK_eq (FuncState *fs) { | 155 | static void luaK_eq (FuncState *fs) { |
131 | Instruction previous = prepare(fs, CREATE_S(OP_IFEQJMP, 0), -2); | 156 | /* PUSHNIL 1; JMPEQ -> NOT (a==nil) */ |
157 | Instruction previous = prepare(fs, CREATE_S(OP_JMPEQ, 0), -2); | ||
132 | if (previous == CREATE_U(OP_PUSHNIL, 1)) { | 158 | if (previous == CREATE_U(OP_PUSHNIL, 1)) { |
133 | setprevious(fs, CREATE_0(OP_NOT)); | 159 | setprevious(fs, CREATE_0(OP_NOT)); |
134 | luaK_deltastack(fs, 1); /* undo delta from `prepare' */ | 160 | luaK_deltastack(fs, 1); /* undo delta from `prepare' */ |
@@ -137,9 +163,10 @@ static void luaK_eq (FuncState *fs) { | |||
137 | 163 | ||
138 | 164 | ||
139 | static void luaK_neq (FuncState *fs) { | 165 | static void luaK_neq (FuncState *fs) { |
140 | Instruction previous = prepare(fs, CREATE_S(OP_IFNEQJMP, 0), -2); | 166 | /* PUSHNIL 1; JMPNEQ -> JMPT (a~=nil) */ |
167 | Instruction previous = prepare(fs, CREATE_S(OP_JMPNEQ, 0), -2); | ||
141 | if (previous == CREATE_U(OP_PUSHNIL, 1)) { | 168 | if (previous == CREATE_U(OP_PUSHNIL, 1)) { |
142 | setprevious(fs, CREATE_S(OP_IFTJMP, 0)); | 169 | setprevious(fs, CREATE_S(OP_JMPT, 0)); |
143 | } | 170 | } |
144 | } | 171 | } |
145 | 172 | ||
@@ -171,7 +198,7 @@ void luaK_fixjump (FuncState *fs, int pc, int dest) { | |||
171 | SETARG_S(*jmp, 0); /* absolute value to represent end of list */ | 198 | SETARG_S(*jmp, 0); /* absolute value to represent end of list */ |
172 | else { /* jump is relative to position following jump instruction */ | 199 | else { /* jump is relative to position following jump instruction */ |
173 | int offset = dest-(pc+1); | 200 | int offset = dest-(pc+1); |
174 | if (offset < -MAXARG_S || offset > MAXARG_S) | 201 | if (abs(offset) > MAXARG_S) |
175 | luaK_error(fs->ls, "control structure too long"); | 202 | luaK_error(fs->ls, "control structure too long"); |
176 | SETARG_S(*jmp, offset); | 203 | SETARG_S(*jmp, offset); |
177 | } | 204 | } |
@@ -298,7 +325,7 @@ void luaK_storevar (LexState *ls, const expdesc *var) { | |||
298 | FuncState *fs = ls->fs; | 325 | FuncState *fs = ls->fs; |
299 | switch (var->k) { | 326 | switch (var->k) { |
300 | case VLOCAL: | 327 | case VLOCAL: |
301 | luaK_U(fs, OP_SETLOCAL, var->u.index, -1); | 328 | luaK_setlocal(fs, var->u.index); |
302 | break; | 329 | break; |
303 | case VGLOBAL: | 330 | case VGLOBAL: |
304 | luaK_U(fs, OP_SETGLOBAL, var->u.index, -1); | 331 | luaK_U(fs, OP_SETGLOBAL, var->u.index, -1); |
@@ -315,14 +342,14 @@ void luaK_storevar (LexState *ls, const expdesc *var) { | |||
315 | 342 | ||
316 | static OpCode invertjump (OpCode op) { | 343 | static OpCode invertjump (OpCode op) { |
317 | switch (op) { | 344 | switch (op) { |
318 | case OP_IFNEQJMP: return OP_IFEQJMP; | 345 | case OP_JMPNEQ: return OP_JMPEQ; |
319 | case OP_IFEQJMP: return OP_IFNEQJMP; | 346 | case OP_JMPEQ: return OP_JMPNEQ; |
320 | case OP_IFLTJMP: return OP_IFGEJMP; | 347 | case OP_JMPLT: return OP_JMPGE; |
321 | case OP_IFLEJMP: return OP_IFGTJMP; | 348 | case OP_JMPLE: return OP_JMPGT; |
322 | case OP_IFGTJMP: return OP_IFLEJMP; | 349 | case OP_JMPGT: return OP_JMPLE; |
323 | case OP_IFGEJMP: return OP_IFLTJMP; | 350 | case OP_JMPGE: return OP_JMPLT; |
324 | case OP_IFTJMP: case OP_ONTJMP: return OP_IFFJMP; | 351 | case OP_JMPT: case OP_JMPONT: return OP_JMPF; |
325 | case OP_IFFJMP: case OP_ONFJMP: return OP_IFTJMP; | 352 | case OP_JMPF: case OP_JMPONF: return OP_JMPT; |
326 | default: | 353 | default: |
327 | LUA_INTERNALERROR(NULL, "invalid jump instruction"); | 354 | LUA_INTERNALERROR(NULL, "invalid jump instruction"); |
328 | return OP_END; /* to avoid warnings */ | 355 | return OP_END; /* to avoid warnings */ |
@@ -334,8 +361,8 @@ static void luaK_jump (FuncState *fs, OpCode jump) { | |||
334 | Instruction previous = prepare(fs, CREATE_S(jump, 0), -1); | 361 | Instruction previous = prepare(fs, CREATE_S(jump, 0), -1); |
335 | switch (GET_OPCODE(previous)) { | 362 | switch (GET_OPCODE(previous)) { |
336 | case OP_NOT: previous = CREATE_S(invertjump(jump), 0); break; | 363 | case OP_NOT: previous = CREATE_S(invertjump(jump), 0); break; |
337 | case OP_PUSHINT: | 364 | case OP_PUSHNIL: /* optimize `repeat until nil' */ |
338 | if (jump == OP_IFTJMP) { | 365 | if (GETARG_U(previous) == 1 && jump == OP_JMPF) { |
339 | previous = CREATE_S(OP_JMP, 0); | 366 | previous = CREATE_S(OP_JMP, 0); |
340 | break; | 367 | break; |
341 | } | 368 | } |
@@ -364,10 +391,10 @@ static void luaK_patchlistaux (FuncState *fs, int list, int target, | |||
364 | luaK_fixjump(fs, list, special_target); | 391 | luaK_fixjump(fs, list, special_target); |
365 | else { | 392 | else { |
366 | luaK_fixjump(fs, list, target); /* do the patch */ | 393 | luaK_fixjump(fs, list, target); /* do the patch */ |
367 | if (op == OP_ONTJMP) /* remove eventual values */ | 394 | if (op == OP_JMPONT) /* remove eventual values */ |
368 | SET_OPCODE(*i, OP_IFTJMP); | 395 | SET_OPCODE(*i, OP_JMPT); |
369 | else if (op == OP_ONFJMP) | 396 | else if (op == OP_JMPONF) |
370 | SET_OPCODE(*i, OP_IFFJMP); | 397 | SET_OPCODE(*i, OP_JMPF); |
371 | } | 398 | } |
372 | list = next; | 399 | list = next; |
373 | } | 400 | } |
@@ -427,12 +454,12 @@ static void luaK_testgo (FuncState *fs, expdesc *v, int invert, OpCode jump) { | |||
427 | 454 | ||
428 | 455 | ||
429 | void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue) { | 456 | void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue) { |
430 | luaK_testgo(fs, v, 1, keepvalue ? OP_ONFJMP : OP_IFFJMP); | 457 | luaK_testgo(fs, v, 1, keepvalue ? OP_JMPONF : OP_JMPF); |
431 | } | 458 | } |
432 | 459 | ||
433 | 460 | ||
434 | void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue) { | 461 | void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue) { |
435 | luaK_testgo(fs, v, 0, keepvalue ? OP_ONTJMP : OP_IFTJMP); | 462 | luaK_testgo(fs, v, 0, keepvalue ? OP_JMPONT : OP_JMPT); |
436 | } | 463 | } |
437 | 464 | ||
438 | 465 | ||
@@ -456,8 +483,8 @@ void luaK_tostack (LexState *ls, expdesc *v, int onlyone) { | |||
456 | p_1 = luaK_S(fs, OP_PUSHINT, 1, 1); | 483 | p_1 = luaK_S(fs, OP_PUSHINT, 1, 1); |
457 | } | 484 | } |
458 | else { /* still may need a PUSHNIL or a PUSHINT */ | 485 | else { /* still may need a PUSHNIL or a PUSHINT */ |
459 | int need_nil = need_value(fs, v->u.l.f, OP_ONFJMP); | 486 | int need_nil = need_value(fs, v->u.l.f, OP_JMPONF); |
460 | int need_1 = need_value(fs, v->u.l.t, OP_ONTJMP); | 487 | int need_1 = need_value(fs, v->u.l.t, OP_JMPONT); |
461 | if (need_nil && need_1) { | 488 | if (need_nil && need_1) { |
462 | luaK_S(fs, OP_JMP, 2, 0); /* skip both pushes */ | 489 | luaK_S(fs, OP_JMP, 2, 0); /* skip both pushes */ |
463 | p_nil = luaK_0(fs, OP_PUSHNILJMP, 0); | 490 | p_nil = luaK_0(fs, OP_PUSHNILJMP, 0); |
@@ -472,8 +499,8 @@ void luaK_tostack (LexState *ls, expdesc *v, int onlyone) { | |||
472 | } | 499 | } |
473 | } | 500 | } |
474 | final = luaK_getlabel(fs); | 501 | final = luaK_getlabel(fs); |
475 | luaK_patchlistaux(fs, v->u.l.f, p_nil, OP_ONFJMP, final); | 502 | luaK_patchlistaux(fs, v->u.l.f, p_nil, OP_JMPONF, final); |
476 | luaK_patchlistaux(fs, v->u.l.t, p_1, OP_ONTJMP, final); | 503 | luaK_patchlistaux(fs, v->u.l.t, p_1, OP_JMPONT, final); |
477 | v->u.l.f = v->u.l.t = NO_JUMP; | 504 | v->u.l.f = v->u.l.t = NO_JUMP; |
478 | } | 505 | } |
479 | } | 506 | } |
@@ -536,10 +563,10 @@ void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) { | |||
536 | case TK_CONC: luaK_conc(fs); break; | 563 | case TK_CONC: luaK_conc(fs); break; |
537 | case TK_EQ: luaK_eq(fs); break; | 564 | case TK_EQ: luaK_eq(fs); break; |
538 | case TK_NE: luaK_neq(fs); break; | 565 | case TK_NE: luaK_neq(fs); break; |
539 | case '>': luaK_S(fs, OP_IFGTJMP, 0, -2); break; | 566 | case '>': luaK_S(fs, OP_JMPGT, 0, -2); break; |
540 | case '<': luaK_S(fs, OP_IFLTJMP, 0, -2); break; | 567 | case '<': luaK_S(fs, OP_JMPLT, 0, -2); break; |
541 | case TK_GE: luaK_S(fs, OP_IFGEJMP, 0, -2); break; | 568 | case TK_GE: luaK_S(fs, OP_JMPGE, 0, -2); break; |
542 | case TK_LE: luaK_S(fs, OP_IFLEJMP, 0, -2); break; | 569 | case TK_LE: luaK_S(fs, OP_JMPLE, 0, -2); break; |
543 | } | 570 | } |
544 | } | 571 | } |
545 | } | 572 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: llimits.h,v 1.3 2000/03/27 20:08:33 roberto Exp roberto $ | 2 | ** $Id: llimits.h,v 1.4 2000/03/31 16:28:45 roberto Exp roberto $ |
3 | ** Limits, basic types, and some other "instalation-dependent" definitions | 3 | ** Limits, basic types, and some other "instalation-dependent" definitions |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -69,14 +69,12 @@ typedef unsigned long Instruction; | |||
69 | 69 | ||
70 | #define SIZE_U (SIZE_INSTRUCTION-SIZE_OP) | 70 | #define SIZE_U (SIZE_INSTRUCTION-SIZE_OP) |
71 | #define POS_U SIZE_OP | 71 | #define POS_U SIZE_OP |
72 | #define SIZE_S (SIZE_INSTRUCTION-SIZE_OP) | ||
73 | #define POS_S SIZE_OP | ||
74 | #define POS_B SIZE_OP | 72 | #define POS_B SIZE_OP |
75 | #define SIZE_A (SIZE_INSTRUCTION-(SIZE_OP+SIZE_B)) | 73 | #define SIZE_A (SIZE_INSTRUCTION-(SIZE_OP+SIZE_B)) |
76 | #define POS_A (SIZE_OP+SIZE_B) | 74 | #define POS_A (SIZE_OP+SIZE_B) |
77 | 75 | ||
78 | #define MAXARG_U ((1<<SIZE_U)-1) | 76 | #define MAXARG_U ((1<<SIZE_U)-1) |
79 | #define MAXARG_S ((1<<(SIZE_S-1))-1) /* `S' is signed */ | 77 | #define MAXARG_S (MAXARG_U>>1) /* `S' is signed */ |
80 | #define MAXARG_A ((1<<SIZE_A)-1) | 78 | #define MAXARG_A ((1<<SIZE_A)-1) |
81 | #define MAXARG_B ((1<<SIZE_B)-1) | 79 | #define MAXARG_B ((1<<SIZE_B)-1) |
82 | 80 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lopcodes.h,v 1.52 2000/03/24 19:49:23 roberto Exp roberto $ | 2 | ** $Id: lopcodes.h,v 1.53 2000/03/27 14:31:12 roberto Exp roberto $ |
3 | ** Opcodes for Lua virtual machine | 3 | ** Opcodes for Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -13,26 +13,30 @@ | |||
13 | /*=========================================================================== | 13 | /*=========================================================================== |
14 | We assume that instructions are unsigned numbers. | 14 | We assume that instructions are unsigned numbers. |
15 | All instructions have an opcode in the first 6 bits. Moreover, | 15 | All instructions have an opcode in the first 6 bits. Moreover, |
16 | an instruction can have 0, 1, or 2 arguments. There are 4 types of | 16 | an instruction can have 0, 1, or 2 arguments. Instructions can |
17 | Instructions: | 17 | have the following types: |
18 | type 0: no arguments | 18 | type 0: no arguments |
19 | type 1: 1 unsigned argument in the higher bits (called `U') | 19 | type 1: 1 unsigned argument in the higher bits (called `U') |
20 | type 2: 1 signed argument in the higher bits (`S') | 20 | type 2: 1 signed argument in the higher bits (`S') |
21 | type 3: 1st unsigned argument in the higher bits (`A') | 21 | type 3: 1st unsigned argument in the higher bits (`A') |
22 | 2nd unsigned argument in the middle bits (`B') | 22 | 2nd unsigned argument in the middle bits (`B') |
23 | 23 | ||
24 | The signed argument is represented in excess 2^K; that is, the number | 24 | A signed argument is represented in excess K; that is, the number |
25 | value is the usigned value minus 2^K. | 25 | value is the unsigned value minus K. K is exactly the maximum value |
26 | for that argument (so that -max is represented by 0, and +max is | ||
27 | represented by 2*max), which is half the maximum for the corresponding | ||
28 | unsigned argument. | ||
26 | 29 | ||
27 | The size of each argument is defined in `llimits.h'. The usual is an | 30 | The size of each argument is defined in `llimits.h'. The usual is an |
28 | instruction with 32 bits, U and S arguments with 26 bits (32-6), B | 31 | instruction with 32 bits, U arguments with 26 bits (32-6), B arguments |
29 | argument with 9 bits, and A argument with 17 bits (32-6-9). For small | 32 | with 9 bits, and A arguments with 17 bits (32-6-9). For small |
30 | instalations, the instruction size can be 16, so U and S have 10 bits, | 33 | instalations, the instruction size can be 16, so U has 10 bits, |
31 | and A and B have 5 bits each. | 34 | and A and B have 5 bits each. |
32 | ===========================================================================*/ | 35 | ===========================================================================*/ |
33 | 36 | ||
34 | 37 | ||
35 | #define EXCESS_S (1<<(SIZE_S-1)) /* == 2^K */ | 38 | |
39 | #define MAXARG_sA (MAXARG_A>>1) /* max value for a signed A */ | ||
36 | 40 | ||
37 | 41 | ||
38 | /* creates a mask with `n' 1 bits at position `p' */ | 42 | /* creates a mask with `n' 1 bits at position `p' */ |
@@ -45,27 +49,33 @@ | |||
45 | ** the following macros help to manipulate instructions | 49 | ** the following macros help to manipulate instructions |
46 | */ | 50 | */ |
47 | 51 | ||
52 | #define CREATE_0(o) ((Instruction)(o)) | ||
48 | #define GET_OPCODE(i) ((OpCode)((i)&MASK1(SIZE_OP,0))) | 53 | #define GET_OPCODE(i) ((OpCode)((i)&MASK1(SIZE_OP,0))) |
49 | #define GETARG_U(i) ((int)((i)>>POS_U)) | ||
50 | #define GETARG_S(i) ((int)((i)>>POS_S)-EXCESS_S) | ||
51 | #define GETARG_A(i) ((int)((i)>>POS_A)) | ||
52 | #define GETARG_B(i) ((int)(((i)>>POS_B) & MASK1(SIZE_B,0))) | ||
53 | |||
54 | #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o))) | 54 | #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o))) |
55 | |||
56 | #define CREATE_U(o,u) ((Instruction)(o) | (Instruction)(u)<<POS_U) | ||
57 | #define GETARG_U(i) ((int)((i)>>POS_U)) | ||
55 | #define SETARG_U(i,u) ((i) = (((i)&MASK0(SIZE_U,POS_U)) | \ | 58 | #define SETARG_U(i,u) ((i) = (((i)&MASK0(SIZE_U,POS_U)) | \ |
56 | ((Instruction)(u)<<POS_U))) | 59 | ((Instruction)(u)<<POS_U))) |
57 | #define SETARG_S(i,s) ((i) = (((i)&MASK0(SIZE_S,POS_S)) | \ | 60 | |
58 | ((Instruction)((s)+EXCESS_S)<<POS_S))) | 61 | #define CREATE_S(o,s) CREATE_U((o),(s)+MAXARG_S) |
62 | #define GETARG_S(i) (GETARG_U(i)-MAXARG_S) | ||
63 | #define SETARG_S(i,s) SETARG_U((i),(s)+MAXARG_S) | ||
64 | |||
65 | |||
66 | #define CREATE_AB(o,a,b) ((Instruction)(o) | ((Instruction)(a)<<POS_A) \ | ||
67 | | ((Instruction)(b)<<POS_B)) | ||
68 | #define GETARG_A(i) ((int)((i)>>POS_A)) | ||
59 | #define SETARG_A(i,a) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \ | 69 | #define SETARG_A(i,a) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \ |
60 | ((Instruction)(a)<<POS_A))) | 70 | ((Instruction)(a)<<POS_A))) |
71 | #define GETARG_B(i) ((int)(((i)>>POS_B) & MASK1(SIZE_B,0))) | ||
61 | #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \ | 72 | #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \ |
62 | ((Instruction)(b)<<POS_B))) | 73 | ((Instruction)(b)<<POS_B))) |
63 | 74 | ||
64 | #define CREATE_0(o) ((Instruction)(o)) | 75 | |
65 | #define CREATE_U(o,u) ((Instruction)(o) | (Instruction)(u)<<POS_U) | 76 | #define CREATE_sAB(o,a,b) (CREATE_AB((o),(a)+MAXARG_sA,(b))) |
66 | #define CREATE_S(o,s) ((Instruction)(o) | ((Instruction)(s)+EXCESS_S)<<POS_S) | 77 | #define GETARG_sA(i) (GETARG_A(i)-MAXARG_sA) |
67 | #define CREATE_AB(o,a,b) ((Instruction)(o) | ((Instruction)(a)<<POS_A) \ | 78 | |
68 | | ((Instruction)(b)<<POS_B)) | ||
69 | 79 | ||
70 | 80 | ||
71 | /* | 81 | /* |
@@ -112,6 +122,7 @@ OP_SETTABLE,/* U v a_u-a_1 i t a_u-a_1 i t t[i]=v */ | |||
112 | OP_SETLIST,/* A B v_b-v_0 t t t[i+a*FPF]=v_i */ | 122 | OP_SETLIST,/* A B v_b-v_0 t t t[i+a*FPF]=v_i */ |
113 | OP_SETMAP,/* U v_u k_u - v_0 k_0 t t t[k_i]=v_i */ | 123 | OP_SETMAP,/* U v_u k_u - v_0 k_0 t t t[k_i]=v_i */ |
114 | 124 | ||
125 | OP_INCLOCAL,/* sA B - - LOC[B]+=sA */ | ||
115 | OP_ADD,/* - y x x+y */ | 126 | OP_ADD,/* - y x x+y */ |
116 | OP_ADDI,/* S x x+s */ | 127 | OP_ADDI,/* S x x+s */ |
117 | OP_SUB,/* - y x x-y */ | 128 | OP_SUB,/* - y x x-y */ |
@@ -122,17 +133,17 @@ OP_CONC,/* U v_u-v_1 v1..-..v_u */ | |||
122 | OP_MINUS,/* - x -x */ | 133 | OP_MINUS,/* - x -x */ |
123 | OP_NOT,/* - x (x==nil)? 1 : nil */ | 134 | OP_NOT,/* - x (x==nil)? 1 : nil */ |
124 | 135 | ||
125 | OP_IFNEQJMP,/* J y x - (x~=y)? PC+=s */ | 136 | OP_JMPNEQ,/* J y x - (x~=y)? PC+=s */ |
126 | OP_IFEQJMP,/* J y x - (x==y)? PC+=s */ | 137 | OP_JMPEQ,/* J y x - (x==y)? PC+=s */ |
127 | OP_IFLTJMP,/* J y x - (x<y)? PC+=s */ | 138 | OP_JMPLT,/* J y x - (x<y)? PC+=s */ |
128 | OP_IFLEJMP,/* J y x - (x<y)? PC+=s */ | 139 | OP_JMPLE,/* J y x - (x<y)? PC+=s */ |
129 | OP_IFGTJMP,/* J y x - (x>y)? PC+=s */ | 140 | OP_JMPGT,/* J y x - (x>y)? PC+=s */ |
130 | OP_IFGEJMP,/* J y x - (x>=y)? PC+=s */ | 141 | OP_JMPGE,/* J y x - (x>=y)? PC+=s */ |
131 | 142 | ||
132 | OP_IFTJMP,/* J x - (x!=nil)? PC+=s */ | 143 | OP_JMPT,/* J x - (x!=nil)? PC+=s */ |
133 | OP_IFFJMP,/* J x - (x==nil)? PC+=s */ | 144 | OP_JMPF,/* J x - (x==nil)? PC+=s */ |
134 | OP_ONTJMP,/* J x (x!=nil)? x : - (x!=nil)? PC+=s */ | 145 | OP_JMPONT,/* J x (x!=nil)? x : - (x!=nil)? PC+=s */ |
135 | OP_ONFJMP,/* J x (x==nil)? x : - (x==nil)? PC+=s */ | 146 | OP_JMPONF,/* J x (x==nil)? x : - (x==nil)? PC+=s */ |
136 | OP_JMP,/* J - - PC+=s */ | 147 | OP_JMP,/* J - - PC+=s */ |
137 | 148 | ||
138 | OP_PUSHNILJMP,/* - - nil PC++; */ | 149 | OP_PUSHNILJMP,/* - - nil PC++; */ |
@@ -145,7 +156,7 @@ OP_SETLINE/* U - - LINE=u */ | |||
145 | 156 | ||
146 | 157 | ||
147 | 158 | ||
148 | #define ISJUMP(o) (OP_IFNEQJMP <= (o) && (o) <= OP_JMP) | 159 | #define ISJUMP(o) (OP_JMPNEQ <= (o) && (o) <= OP_JMP) |
149 | 160 | ||
150 | 161 | ||
151 | #endif | 162 | #endif |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 1.97 2000/03/27 20:10:21 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.98 2000/03/29 20:19:20 roberto Exp roberto $ |
3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -365,9 +365,9 @@ StkId luaV_execute (lua_State *L, const Closure *cl, register StkId base) { | |||
365 | case OP_PUSHNIL: { | 365 | case OP_PUSHNIL: { |
366 | int n = GETARG_U(i); | 366 | int n = GETARG_U(i); |
367 | LUA_ASSERT(L, n>0, "invalid argument"); | 367 | LUA_ASSERT(L, n>0, "invalid argument"); |
368 | do { | 368 | do { |
369 | ttype(top++) = TAG_NIL; | 369 | ttype(top++) = TAG_NIL; |
370 | } while (--n > 0); | 370 | } while (--n > 0); |
371 | break; | 371 | break; |
372 | } | 372 | } |
373 | 373 | ||
@@ -491,6 +491,21 @@ StkId luaV_execute (lua_State *L, const Closure *cl, register StkId base) { | |||
491 | top--; | 491 | top--; |
492 | break; | 492 | break; |
493 | 493 | ||
494 | case OP_INCLOCAL: { | ||
495 | TObject *var = base+GETARG_B(i); | ||
496 | int n = GETARG_sA(i); | ||
497 | if (tonumber(var)) { | ||
498 | *top = *var; /* PUSHLOCAL */ | ||
499 | ttype(top+1) = TAG_NUMBER; | ||
500 | nvalue(top+1) = (Number)n; /* PUSHINT */ | ||
501 | call_arith(L, top+2, IM_ADD); | ||
502 | *var = *top; /* SETLOCAL */ | ||
503 | } | ||
504 | else | ||
505 | nvalue(var) += (Number)n; | ||
506 | break; | ||
507 | } | ||
508 | |||
494 | case OP_ADDI: | 509 | case OP_ADDI: |
495 | if (tonumber(top-1)) { | 510 | if (tonumber(top-1)) { |
496 | ttype(top) = TAG_NUMBER; | 511 | ttype(top) = TAG_NUMBER; |
@@ -554,50 +569,50 @@ StkId luaV_execute (lua_State *L, const Closure *cl, register StkId base) { | |||
554 | nvalue(top-1) = 1; | 569 | nvalue(top-1) = 1; |
555 | break; | 570 | break; |
556 | 571 | ||
557 | case OP_IFNEQJMP: | 572 | case OP_JMPNEQ: |
558 | top -= 2; | 573 | top -= 2; |
559 | if (!luaO_equalObj(top, top+1)) pc += GETARG_S(i); | 574 | if (!luaO_equalObj(top, top+1)) pc += GETARG_S(i); |
560 | break; | 575 | break; |
561 | 576 | ||
562 | case OP_IFEQJMP: | 577 | case OP_JMPEQ: |
563 | top -= 2; | 578 | top -= 2; |
564 | if (luaO_equalObj(top, top+1)) pc += GETARG_S(i); | 579 | if (luaO_equalObj(top, top+1)) pc += GETARG_S(i); |
565 | break; | 580 | break; |
566 | 581 | ||
567 | case OP_IFLTJMP: | 582 | case OP_JMPLT: |
568 | top -= 2; | 583 | top -= 2; |
569 | if (luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i); | 584 | if (luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i); |
570 | break; | 585 | break; |
571 | 586 | ||
572 | case OP_IFLEJMP: /* a <= b === !(b<a) */ | 587 | case OP_JMPLE: /* a <= b === !(b<a) */ |
573 | top -= 2; | 588 | top -= 2; |
574 | if (!luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i); | 589 | if (!luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i); |
575 | break; | 590 | break; |
576 | 591 | ||
577 | case OP_IFGTJMP: /* a > b === (b<a) */ | 592 | case OP_JMPGT: /* a > b === (b<a) */ |
578 | top -= 2; | 593 | top -= 2; |
579 | if (luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i); | 594 | if (luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i); |
580 | break; | 595 | break; |
581 | 596 | ||
582 | case OP_IFGEJMP: /* a >= b === !(a<b) */ | 597 | case OP_JMPGE: /* a >= b === !(a<b) */ |
583 | top -= 2; | 598 | top -= 2; |
584 | if (!luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i); | 599 | if (!luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i); |
585 | break; | 600 | break; |
586 | 601 | ||
587 | case OP_IFTJMP: | 602 | case OP_JMPT: |
588 | if (ttype(--top) != TAG_NIL) pc += GETARG_S(i); | 603 | if (ttype(--top) != TAG_NIL) pc += GETARG_S(i); |
589 | break; | 604 | break; |
590 | 605 | ||
591 | case OP_IFFJMP: | 606 | case OP_JMPF: |
592 | if (ttype(--top) == TAG_NIL) pc += GETARG_S(i); | 607 | if (ttype(--top) == TAG_NIL) pc += GETARG_S(i); |
593 | break; | 608 | break; |
594 | 609 | ||
595 | case OP_ONTJMP: | 610 | case OP_JMPONT: |
596 | if (ttype(top-1) != TAG_NIL) pc += GETARG_S(i); | 611 | if (ttype(top-1) != TAG_NIL) pc += GETARG_S(i); |
597 | else top--; | 612 | else top--; |
598 | break; | 613 | break; |
599 | 614 | ||
600 | case OP_ONFJMP: | 615 | case OP_JMPONF: |
601 | if (ttype(top-1) == TAG_NIL) pc += GETARG_S(i); | 616 | if (ttype(top-1) == TAG_NIL) pc += GETARG_S(i); |
602 | else top--; | 617 | else top--; |
603 | break; | 618 | break; |