diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-03-13 17:37:16 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-03-13 17:37:16 -0300 |
| commit | f0b697e01caf626785cd9572f82fe98c4a3889fd (patch) | |
| tree | 13711db24b370bf0484ab05702eaf1871eb6b9a1 /lcode.c | |
| parent | 73aa465a8ed8dee6c6a27a6f8b2f51227b70789d (diff) | |
| download | lua-f0b697e01caf626785cd9572f82fe98c4a3889fd.tar.gz lua-f0b697e01caf626785cd9572f82fe98c4a3889fd.tar.bz2 lua-f0b697e01caf626785cd9572f82fe98c4a3889fd.zip | |
details
Diffstat (limited to 'lcode.c')
| -rw-r--r-- | lcode.c | 366 |
1 files changed, 181 insertions, 185 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lcode.c,v 1.9 2000/03/10 14:38:10 roberto Exp roberto $ | 2 | ** $Id: lcode.c,v 1.10 2000/03/10 18:37: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 | */ |
| @@ -27,8 +27,7 @@ void luaK_error (LexState *ls, const char *msg) { | |||
| 27 | ** If there is a jump target between this and the current instruction, | 27 | ** If there is a jump target between this and the current instruction, |
| 28 | ** returns the address of a dummy instruction to avoid wrong optimizations. | 28 | ** returns the address of a dummy instruction to avoid wrong optimizations. |
| 29 | */ | 29 | */ |
| 30 | static Instruction *previous_instruction (LexState *ls) { | 30 | static Instruction *previous_instruction (FuncState *fs) { |
| 31 | FuncState *fs = ls->fs; | ||
| 32 | if (fs->pc > fs->lasttarget) /* no jumps to current position? */ | 31 | if (fs->pc > fs->lasttarget) /* no jumps to current position? */ |
| 33 | return &fs->f->code[fs->pc-1]; /* returns previous instruction */ | 32 | return &fs->f->code[fs->pc-1]; /* returns previous instruction */ |
| 34 | else { | 33 | else { |
| @@ -38,123 +37,134 @@ static Instruction *previous_instruction (LexState *ls) { | |||
| 38 | } | 37 | } |
| 39 | 38 | ||
| 40 | 39 | ||
| 41 | int luaK_primitivecode (LexState *ls, Instruction i) { | 40 | int luaK_primitivecode (FuncState *fs, Instruction i) { |
| 42 | FuncState *fs = ls->fs; | 41 | luaM_growvector(fs->L, fs->f->code, fs->pc, 1, Instruction, codeEM, MAXARG_S); |
| 43 | luaM_growvector(ls->L, fs->f->code, fs->pc, 1, Instruction, codeEM, MAXARG_S); | ||
| 44 | fs->f->code[fs->pc] = i; | 42 | fs->f->code[fs->pc] = i; |
| 45 | return fs->pc++; | 43 | return fs->pc++; |
| 46 | } | 44 | } |
| 47 | 45 | ||
| 48 | 46 | ||
| 49 | static void luaK_minus (LexState *ls) { | 47 | static void luaK_minus (FuncState *fs) { |
| 50 | Instruction *previous = previous_instruction(ls); | 48 | Instruction *previous = previous_instruction(fs); |
| 51 | switch(GET_OPCODE(*previous)) { | 49 | switch(GET_OPCODE(*previous)) { |
| 52 | case OP_PUSHINT: SETARG_S(*previous, -GETARG_S(*previous)); return; | 50 | case OP_PUSHINT: SETARG_S(*previous, -GETARG_S(*previous)); return; |
| 53 | case OP_PUSHNUM: SET_OPCODE(*previous, OP_PUSHNEGNUM); return; | 51 | case OP_PUSHNUM: SET_OPCODE(*previous, OP_PUSHNEGNUM); return; |
| 54 | case OP_PUSHNEGNUM: SET_OPCODE(*previous, OP_PUSHNUM); return; | 52 | case OP_PUSHNEGNUM: SET_OPCODE(*previous, OP_PUSHNUM); return; |
| 55 | default: luaK_primitivecode(ls, CREATE_0(OP_MINUS)); | 53 | default: luaK_primitivecode(fs, CREATE_0(OP_MINUS)); |
| 56 | } | 54 | } |
| 57 | } | 55 | } |
| 58 | 56 | ||
| 59 | 57 | ||
| 60 | static void luaK_gettable (LexState *ls) { | 58 | static void luaK_gettable (FuncState *fs) { |
| 61 | Instruction *previous = previous_instruction(ls); | 59 | Instruction *previous = previous_instruction(fs); |
| 62 | luaK_deltastack(ls, -1); | 60 | luaK_deltastack(fs, -1); |
| 63 | switch(GET_OPCODE(*previous)) { | 61 | switch(GET_OPCODE(*previous)) { |
| 64 | case OP_PUSHSTRING: SET_OPCODE(*previous, OP_GETDOTTED); break; | 62 | case OP_PUSHSTRING: SET_OPCODE(*previous, OP_GETDOTTED); break; |
| 65 | default: luaK_primitivecode(ls, CREATE_0(OP_GETTABLE)); | 63 | default: luaK_primitivecode(fs, CREATE_0(OP_GETTABLE)); |
| 66 | } | 64 | } |
| 67 | } | 65 | } |
| 68 | 66 | ||
| 69 | 67 | ||
| 70 | static void luaK_add (LexState *ls) { | 68 | static void luaK_add (FuncState *fs) { |
| 71 | Instruction *previous = previous_instruction(ls); | 69 | Instruction *previous = previous_instruction(fs); |
| 72 | luaK_deltastack(ls, -1); | 70 | luaK_deltastack(fs, -1); |
| 73 | switch(GET_OPCODE(*previous)) { | 71 | switch(GET_OPCODE(*previous)) { |
| 74 | case OP_PUSHINT: SET_OPCODE(*previous, OP_ADDI); break; | 72 | case OP_PUSHINT: SET_OPCODE(*previous, OP_ADDI); break; |
| 75 | default: luaK_primitivecode(ls, CREATE_0(OP_ADD)); | 73 | default: luaK_primitivecode(fs, CREATE_0(OP_ADD)); |
| 76 | } | 74 | } |
| 77 | } | 75 | } |
| 78 | 76 | ||
| 79 | 77 | ||
| 80 | static void luaK_sub (LexState *ls) { | 78 | static void luaK_sub (FuncState *fs) { |
| 81 | Instruction *previous = previous_instruction(ls); | 79 | Instruction *previous = previous_instruction(fs); |
| 82 | luaK_deltastack(ls, -1); | 80 | luaK_deltastack(fs, -1); |
| 83 | switch(GET_OPCODE(*previous)) { | 81 | switch(GET_OPCODE(*previous)) { |
| 84 | case OP_PUSHINT: | 82 | case OP_PUSHINT: |
| 85 | SET_OPCODE(*previous, OP_ADDI); | 83 | SET_OPCODE(*previous, OP_ADDI); |
| 86 | SETARG_S(*previous, -GETARG_S(*previous)); | 84 | SETARG_S(*previous, -GETARG_S(*previous)); |
| 87 | break; | 85 | break; |
| 88 | default: luaK_primitivecode(ls, CREATE_0(OP_SUB)); | 86 | default: luaK_primitivecode(fs, CREATE_0(OP_SUB)); |
| 89 | } | 87 | } |
| 90 | } | 88 | } |
| 91 | 89 | ||
| 92 | 90 | ||
| 93 | static void luaK_conc (LexState *ls) { | 91 | static void luaK_conc (FuncState *fs) { |
| 94 | Instruction *previous = previous_instruction(ls); | 92 | Instruction *previous = previous_instruction(fs); |
| 95 | luaK_deltastack(ls, -1); | 93 | luaK_deltastack(fs, -1); |
| 96 | switch(GET_OPCODE(*previous)) { | 94 | switch(GET_OPCODE(*previous)) { |
| 97 | case OP_CONC: SETARG_U(*previous, GETARG_U(*previous)+1); break; | 95 | case OP_CONC: SETARG_U(*previous, GETARG_U(*previous)+1); break; |
| 98 | default: luaK_primitivecode(ls, CREATE_U(OP_CONC, 2)); | 96 | default: luaK_primitivecode(fs, CREATE_U(OP_CONC, 2)); |
| 99 | } | 97 | } |
| 100 | } | 98 | } |
| 101 | 99 | ||
| 102 | 100 | ||
| 103 | static void luaK_eq (LexState *ls) { | 101 | static void luaK_eq (FuncState *fs) { |
| 104 | Instruction *previous = previous_instruction(ls); | 102 | Instruction *previous = previous_instruction(fs); |
| 105 | if (*previous == CREATE_U(OP_PUSHNIL, 1)) { | 103 | if (*previous == CREATE_U(OP_PUSHNIL, 1)) { |
| 106 | *previous = CREATE_0(OP_NOT); | 104 | *previous = CREATE_0(OP_NOT); |
| 107 | luaK_deltastack(ls, -1); /* undo effect of PUSHNIL */ | 105 | luaK_deltastack(fs, -1); /* undo effect of PUSHNIL */ |
| 108 | } | 106 | } |
| 109 | else | 107 | else |
| 110 | luaK_S(ls, OP_IFEQJMP, 0, -2); | 108 | luaK_S(fs, OP_IFEQJMP, 0, -2); |
| 111 | } | 109 | } |
| 112 | 110 | ||
| 113 | 111 | ||
| 114 | static void luaK_neq (LexState *ls) { | 112 | static void luaK_neq (FuncState *fs) { |
| 115 | Instruction *previous = previous_instruction(ls); | 113 | Instruction *previous = previous_instruction(fs); |
| 116 | if (*previous == CREATE_U(OP_PUSHNIL, 1)) { | 114 | if (*previous == CREATE_U(OP_PUSHNIL, 1)) { |
| 117 | ls->fs->pc--; /* remove PUSHNIL */ | 115 | fs->pc--; /* remove PUSHNIL */ |
| 118 | luaK_deltastack(ls, -1); /* undo effect of PUSHNIL */ | 116 | luaK_deltastack(fs, -1); /* undo effect of PUSHNIL */ |
| 119 | } | 117 | } |
| 120 | else | 118 | else |
| 121 | luaK_S(ls, OP_IFNEQJMP, 0, -2); | 119 | luaK_S(fs, OP_IFNEQJMP, 0, -2); |
| 122 | } | 120 | } |
| 123 | 121 | ||
| 124 | 122 | ||
| 125 | void luaK_retcode (LexState *ls, int nlocals, int nexps) { | 123 | void luaK_retcode (FuncState *fs, int nlocals, int nexps) { |
| 126 | Instruction *previous = previous_instruction(ls); | 124 | Instruction *previous = previous_instruction(fs); |
| 127 | if (nexps > 0 && GET_OPCODE(*previous) == OP_CALL) { | 125 | if (nexps > 0 && GET_OPCODE(*previous) == OP_CALL) { |
| 128 | LUA_ASSERT(ls->L, GETARG_B(*previous) == MULT_RET, "call should be open"); | 126 | LUA_ASSERT(fs->L, GETARG_B(*previous) == MULT_RET, "call should be open"); |
| 129 | SET_OPCODE(*previous, OP_TAILCALL); | 127 | SET_OPCODE(*previous, OP_TAILCALL); |
| 130 | SETARG_B(*previous, nlocals); | 128 | SETARG_B(*previous, nlocals); |
| 131 | } | 129 | } |
| 132 | else | 130 | else |
| 133 | luaK_primitivecode(ls, CREATE_U(OP_RETURN, nlocals)); | 131 | luaK_primitivecode(fs, CREATE_U(OP_RETURN, nlocals)); |
| 134 | } | 132 | } |
| 135 | 133 | ||
| 136 | 134 | ||
| 137 | static void luaK_pushnil (LexState *ls, int n) { | 135 | static void luaK_pushnil (FuncState *fs, int n) { |
| 138 | Instruction *previous = previous_instruction(ls); | 136 | Instruction *previous = previous_instruction(fs); |
| 139 | luaK_deltastack(ls, n); | 137 | luaK_deltastack(fs, n); |
| 140 | switch(GET_OPCODE(*previous)) { | 138 | switch(GET_OPCODE(*previous)) { |
| 141 | case OP_PUSHNIL: SETARG_U(*previous, GETARG_U(*previous)+n); break; | 139 | case OP_PUSHNIL: SETARG_U(*previous, GETARG_U(*previous)+n); break; |
| 142 | default: luaK_primitivecode(ls, CREATE_U(OP_PUSHNIL, n)); | 140 | default: luaK_primitivecode(fs, CREATE_U(OP_PUSHNIL, n)); |
| 143 | } | 141 | } |
| 144 | } | 142 | } |
| 145 | 143 | ||
| 146 | 144 | ||
| 147 | int luaK_code (LexState *ls, Instruction i, int delta) { | 145 | int luaK_code (FuncState *fs, Instruction i, int delta) { |
| 148 | luaK_deltastack(ls, delta); | 146 | luaK_deltastack(fs, delta); |
| 149 | return luaK_primitivecode(ls, i); | 147 | return luaK_primitivecode(fs, i); |
| 150 | } | 148 | } |
| 151 | 149 | ||
| 152 | 150 | ||
| 153 | void luaK_fixjump (LexState *ls, int pc, int dest) { | 151 | void luaK_fixjump (FuncState *fs, int pc, int dest) { |
| 154 | FuncState *fs = ls->fs; | ||
| 155 | Instruction *jmp = &fs->f->code[pc]; | 152 | Instruction *jmp = &fs->f->code[pc]; |
| 156 | /* jump is relative to position following jump instruction */ | 153 | if (dest != NO_JUMP) { |
| 157 | SETARG_S(*jmp, dest-(pc+1)); | 154 | /* jump is relative to position following jump instruction */ |
| 155 | SETARG_S(*jmp, dest-(pc+1)); | ||
| 156 | } | ||
| 157 | else | ||
| 158 | SETARG_S(*jmp, 0); /* absolute value to represent end of list */ | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | static int luaK_getjump (FuncState *fs, int pc) { | ||
| 163 | int offset = GETARG_S(fs->f->code[pc]); | ||
| 164 | if (offset == 0) | ||
| 165 | return NO_JUMP; /* end of list */ | ||
| 166 | else | ||
| 167 | return (pc+1)+offset; | ||
| 158 | } | 168 | } |
| 159 | 169 | ||
| 160 | 170 | ||
| @@ -162,26 +172,24 @@ void luaK_fixjump (LexState *ls, int pc, int dest) { | |||
| 162 | ** returns current `pc' and marks it as a jump target (to avoid wrong | 172 | ** returns current `pc' and marks it as a jump target (to avoid wrong |
| 163 | ** optimizations with consecutive instructions not in the same basic block). | 173 | ** optimizations with consecutive instructions not in the same basic block). |
| 164 | */ | 174 | */ |
| 165 | int luaK_getlabel (LexState *ls) { | 175 | int luaK_getlabel (FuncState *fs) { |
| 166 | FuncState *fs = ls->fs; | ||
| 167 | fs->lasttarget = fs->pc; | 176 | fs->lasttarget = fs->pc; |
| 168 | return fs->pc; | 177 | return fs->pc; |
| 169 | } | 178 | } |
| 170 | 179 | ||
| 171 | 180 | ||
| 172 | void luaK_deltastack (LexState *ls, int delta) { | 181 | void luaK_deltastack (FuncState *fs, int delta) { |
| 173 | FuncState *fs = ls->fs; | ||
| 174 | fs->stacksize += delta; | 182 | fs->stacksize += delta; |
| 175 | if (delta > 0 && fs->stacksize > fs->f->maxstacksize) { | 183 | if (delta > 0 && fs->stacksize > fs->f->maxstacksize) { |
| 176 | if (fs->stacksize > MAXSTACK) | 184 | if (fs->stacksize > MAXSTACK) |
| 177 | luaK_error(ls, "function or expression too complex"); | 185 | luaK_error(fs->ls, "function or expression too complex"); |
| 178 | fs->f->maxstacksize = fs->stacksize; | 186 | fs->f->maxstacksize = fs->stacksize; |
| 179 | } | 187 | } |
| 180 | } | 188 | } |
| 181 | 189 | ||
| 182 | 190 | ||
| 183 | void luaK_kstr (LexState *ls, int c) { | 191 | void luaK_kstr (LexState *ls, int c) { |
| 184 | luaK_U(ls, OP_PUSHSTRING, c, 1); | 192 | luaK_U(ls->fs, OP_PUSHSTRING, c, 1); |
| 185 | } | 193 | } |
| 186 | 194 | ||
| 187 | 195 | ||
| @@ -189,103 +197,104 @@ void luaK_kstr (LexState *ls, int c) { | |||
| 189 | #define LOOKBACKNUMS 20 /* arbitrary limit */ | 197 | #define LOOKBACKNUMS 20 /* arbitrary limit */ |
| 190 | #endif | 198 | #endif |
| 191 | 199 | ||
| 192 | static int real_constant (LexState *ls, Number r) { | 200 | static int real_constant (FuncState *fs, Number r) { |
| 193 | /* check whether `r' has appeared within the last LOOKBACKNUMS entries */ | 201 | /* check whether `r' has appeared within the last LOOKBACKNUMS entries */ |
| 194 | Proto *f = ls->fs->f; | 202 | Proto *f = fs->f; |
| 195 | int c = f->nknum; | 203 | int c = f->nknum; |
| 196 | int lim = c < LOOKBACKNUMS ? 0 : c-LOOKBACKNUMS; | 204 | int lim = c < LOOKBACKNUMS ? 0 : c-LOOKBACKNUMS; |
| 197 | while (--c >= lim) | 205 | while (--c >= lim) |
| 198 | if (f->knum[c] == r) return c; | 206 | if (f->knum[c] == r) return c; |
| 199 | /* not found; create a new entry */ | 207 | /* not found; create a new entry */ |
| 200 | luaM_growvector(ls->L, f->knum, f->nknum, 1, Number, constantEM, MAXARG_U); | 208 | luaM_growvector(fs->L, f->knum, f->nknum, 1, Number, constantEM, MAXARG_U); |
| 201 | c = f->nknum++; | 209 | c = f->nknum++; |
| 202 | f->knum[c] = r; | 210 | f->knum[c] = r; |
| 203 | return c; | 211 | return c; |
| 204 | } | 212 | } |
| 205 | 213 | ||
| 206 | 214 | ||
| 207 | void luaK_number (LexState *ls, Number f) { | 215 | void luaK_number (FuncState *fs, Number f) { |
| 208 | if (f <= (Number)MAXARG_S && (int)f == f) | 216 | if (f <= (Number)MAXARG_S && (int)f == f) |
| 209 | luaK_S(ls, OP_PUSHINT, (int)f, 1); /* f has a short integer value */ | 217 | luaK_S(fs, OP_PUSHINT, (int)f, 1); /* f has a short integer value */ |
| 210 | else | 218 | else |
| 211 | luaK_U(ls, OP_PUSHNUM, real_constant(ls, f), 1); | 219 | luaK_U(fs, OP_PUSHNUM, real_constant(fs, f), 1); |
| 212 | } | 220 | } |
| 213 | 221 | ||
| 214 | 222 | ||
| 215 | void luaK_adjuststack (LexState *ls, int n) { | 223 | void luaK_adjuststack (FuncState *fs, int n) { |
| 216 | if (n > 0) | 224 | if (n > 0) |
| 217 | luaK_U(ls, OP_POP, n, -n); | 225 | luaK_U(fs, OP_POP, n, -n); |
| 218 | else if (n < 0) | 226 | else if (n < 0) |
| 219 | luaK_pushnil(ls, -n); | 227 | luaK_pushnil(fs, -n); |
| 220 | } | 228 | } |
| 221 | 229 | ||
| 222 | 230 | ||
| 223 | int luaK_lastisopen (LexState *ls) { | 231 | int luaK_lastisopen (FuncState *fs) { |
| 224 | /* check whether last instruction is an (open) function call */ | 232 | /* check whether last instruction is an (open) function call */ |
| 225 | Instruction *i = previous_instruction(ls); | 233 | Instruction *i = previous_instruction(fs); |
| 226 | if (GET_OPCODE(*i) == OP_CALL) { | 234 | if (GET_OPCODE(*i) == OP_CALL) { |
| 227 | LUA_ASSERT(ls->L, GETARG_B(*i) == MULT_RET, "call should be open"); | 235 | LUA_ASSERT(fs->L, GETARG_B(*i) == MULT_RET, "call should be open"); |
| 228 | return 1; | 236 | return 1; |
| 229 | } | 237 | } |
| 230 | else return 0; | 238 | else return 0; |
| 231 | } | 239 | } |
| 232 | 240 | ||
| 233 | 241 | ||
| 234 | void luaK_setcallreturns (LexState *ls, int nresults) { | 242 | void luaK_setcallreturns (FuncState *fs, int nresults) { |
| 235 | Instruction *i = previous_instruction(ls); | 243 | Instruction *i = previous_instruction(fs); |
| 236 | if (GET_OPCODE(*i) == OP_CALL) { /* expression is a function call? */ | 244 | if (GET_OPCODE(*i) == OP_CALL) { /* expression is a function call? */ |
| 237 | LUA_ASSERT(ls->L, GETARG_B(*i) == MULT_RET, "call should be open"); | 245 | LUA_ASSERT(fs->L, GETARG_B(*i) == MULT_RET, "call should be open"); |
| 238 | SETARG_B(*i, nresults); /* set nresults */ | 246 | SETARG_B(*i, nresults); /* set nresults */ |
| 239 | luaK_deltastack(ls, nresults); /* push results */ | 247 | luaK_deltastack(fs, nresults); /* push results */ |
| 240 | } | 248 | } |
| 241 | } | 249 | } |
| 242 | 250 | ||
| 243 | 251 | ||
| 244 | static void assertglobal (LexState *ls, int index) { | 252 | static void assertglobal (FuncState *fs, int index) { |
| 245 | luaS_assertglobal(ls->L, ls->fs->f->kstr[index]); | 253 | luaS_assertglobal(fs->L, fs->f->kstr[index]); |
| 246 | } | 254 | } |
| 247 | 255 | ||
| 248 | 256 | ||
| 249 | static int discharge (LexState *ls, expdesc *var) { | 257 | static int discharge (FuncState *fs, expdesc *var) { |
| 250 | switch (var->k) { | 258 | switch (var->k) { |
| 251 | case VLOCAL: | 259 | case VLOCAL: |
| 252 | luaK_U(ls, OP_PUSHLOCAL, var->u.index, 1); | 260 | luaK_U(fs, OP_PUSHLOCAL, var->u.index, 1); |
| 253 | break; | 261 | break; |
| 254 | case VGLOBAL: | 262 | case VGLOBAL: |
| 255 | luaK_U(ls, OP_GETGLOBAL, var->u.index, 1); | 263 | luaK_U(fs, OP_GETGLOBAL, var->u.index, 1); |
| 256 | assertglobal(ls, var->u.index); /* make sure that there is a global */ | 264 | assertglobal(fs, var->u.index); /* make sure that there is a global */ |
| 257 | break; | 265 | break; |
| 258 | case VINDEXED: | 266 | case VINDEXED: |
| 259 | luaK_gettable(ls); | 267 | luaK_gettable(fs); |
| 260 | break; | 268 | break; |
| 261 | case VEXP: | 269 | case VEXP: |
| 262 | return 0; /* nothing to do */ | 270 | return 0; /* nothing to do */ |
| 263 | } | 271 | } |
| 264 | var->k = VEXP; | 272 | var->k = VEXP; |
| 265 | var->u.l.t = var->u.l.f = 0; | 273 | var->u.l.t = var->u.l.f = NO_JUMP; |
| 266 | return 1; | 274 | return 1; |
| 267 | } | 275 | } |
| 268 | 276 | ||
| 269 | 277 | ||
| 270 | static void discharge1 (LexState *ls, expdesc *var) { | 278 | static void discharge1 (FuncState *fs, expdesc *var) { |
| 271 | discharge(ls, var); | 279 | discharge(fs, var); |
| 272 | /* if it has jumps it is already discharged */ | 280 | /* if it has jumps it is already discharged */ |
| 273 | if (var->u.l.t == 0 && var->u.l.f == 0) | 281 | if (var->u.l.t == NO_JUMP && var->u.l.f == NO_JUMP) |
| 274 | luaK_setcallreturns(ls, 1); /* call must return 1 value */ | 282 | luaK_setcallreturns(fs, 1); /* call must return 1 value */ |
| 275 | } | 283 | } |
| 276 | 284 | ||
| 277 | 285 | ||
| 278 | void luaK_storevar (LexState *ls, const expdesc *var) { | 286 | void luaK_storevar (LexState *ls, const expdesc *var) { |
| 287 | FuncState *fs = ls->fs; | ||
| 279 | switch (var->k) { | 288 | switch (var->k) { |
| 280 | case VLOCAL: | 289 | case VLOCAL: |
| 281 | luaK_U(ls, OP_SETLOCAL, var->u.index, -1); | 290 | luaK_U(fs, OP_SETLOCAL, var->u.index, -1); |
| 282 | break; | 291 | break; |
| 283 | case VGLOBAL: | 292 | case VGLOBAL: |
| 284 | luaK_U(ls, OP_SETGLOBAL, var->u.index, -1); | 293 | luaK_U(fs, OP_SETGLOBAL, var->u.index, -1); |
| 285 | assertglobal(ls, var->u.index); /* make sure that there is a global */ | 294 | assertglobal(fs, var->u.index); /* make sure that there is a global */ |
| 286 | break; | 295 | break; |
| 287 | case VINDEXED: | 296 | case VINDEXED: |
| 288 | luaK_0(ls, OP_SETTABLEPOP, -3); | 297 | luaK_0(fs, OP_SETTABLEPOP, -3); |
| 289 | break; | 298 | break; |
| 290 | default: | 299 | default: |
| 291 | LUA_INTERNALERROR(ls->L, "invalid var kind to store"); | 300 | LUA_INTERNALERROR(ls->L, "invalid var kind to store"); |
| @@ -310,128 +319,113 @@ static OpCode invertjump (OpCode op) { | |||
| 310 | } | 319 | } |
| 311 | 320 | ||
| 312 | 321 | ||
| 313 | static void luaK_jump (LexState *ls, OpCode jump) { | 322 | static void luaK_jump (FuncState *fs, OpCode jump) { |
| 314 | Instruction *previous = previous_instruction(ls); | 323 | Instruction *previous = previous_instruction(fs); |
| 315 | luaK_deltastack(ls, -1); | 324 | luaK_deltastack(fs, -1); |
| 316 | if (*previous == CREATE_0(OP_NOT)) | 325 | if (*previous == CREATE_0(OP_NOT)) |
| 317 | *previous = CREATE_S(invertjump(jump), 0); | 326 | *previous = CREATE_S(invertjump(jump), 0); |
| 318 | else | 327 | else |
| 319 | luaK_primitivecode(ls, CREATE_S(jump, 0)); | 328 | luaK_primitivecode(fs, CREATE_S(jump, 0)); |
| 320 | } | 329 | } |
| 321 | 330 | ||
| 322 | 331 | ||
| 323 | static void insert_last (FuncState *fs, int *list) { | 332 | static void insert_last (FuncState *fs, int *list) { |
| 324 | int first = *list; | 333 | int first = *list; |
| 325 | *list = fs->pc-1; /* insert last instruction in the list */ | 334 | *list = fs->pc-1; /* insert last instruction in the list */ |
| 326 | if (first == 0) | 335 | luaK_fixjump(fs, *list, first); |
| 327 | SETARG_S(fs->f->code[*list], 0); | ||
| 328 | else | ||
| 329 | SETARG_S(fs->f->code[*list], first-fs->pc); | ||
| 330 | } | 336 | } |
| 331 | 337 | ||
| 332 | 338 | ||
| 333 | static void luaK_patchlistaux (LexState *ls, int list, int target, | 339 | static void luaK_patchlistaux (FuncState *fs, int list, int target, |
| 334 | OpCode special, int special_target) { | 340 | OpCode special, int special_target) { |
| 335 | if (list != 0) { | 341 | Instruction *code = fs->f->code; |
| 336 | Instruction *code = ls->fs->f->code; | 342 | while (list != NO_JUMP) { |
| 337 | for (;;) { | 343 | int next = luaK_getjump(fs, list); |
| 338 | Instruction *i = &code[list]; | 344 | Instruction *i = &code[list]; |
| 339 | OpCode op = GET_OPCODE(*i); | 345 | OpCode op = GET_OPCODE(*i); |
| 340 | int next = GETARG_S(*i); | 346 | if (op == special) /* this `op' already has a value */ |
| 341 | if (op == special) /* this `op' already has a value */ | 347 | SETARG_S(*i, special_target-(list+1)); |
| 342 | SETARG_S(*i, special_target-(list+1)); | 348 | else { |
| 343 | else { | 349 | SETARG_S(*i, target-(list+1)); /* do the patch */ |
| 344 | SETARG_S(*i, target-(list+1)); /* do the patch */ | 350 | if (op == OP_ONTJMP) /* remove eventual values */ |
| 345 | if (op == OP_ONTJMP) /* remove eventual values */ | 351 | SET_OPCODE(*i, OP_IFTJMP); |
| 346 | SET_OPCODE(*i, OP_IFTJMP); | 352 | else if (op == OP_ONFJMP) |
| 347 | else if (op == OP_ONFJMP) | 353 | SET_OPCODE(*i, OP_IFFJMP); |
| 348 | SET_OPCODE(*i, OP_IFFJMP); | ||
| 349 | } | ||
| 350 | if (next == 0) return; | ||
| 351 | list += next+1; | ||
| 352 | } | 354 | } |
| 355 | list = next; | ||
| 353 | } | 356 | } |
| 354 | } | 357 | } |
| 355 | 358 | ||
| 356 | 359 | ||
| 357 | void luaK_patchlist (LexState *ls, int list, int target) { | 360 | void luaK_patchlist (FuncState *fs, int list, int target) { |
| 358 | luaK_patchlistaux(ls, list, target, OP_END, 0); | 361 | luaK_patchlistaux(fs, list, target, OP_END, 0); |
| 359 | } | 362 | } |
| 360 | 363 | ||
| 361 | 364 | ||
| 362 | static int need_value (FuncState *fs, int list, OpCode hasvalue) { | 365 | static int need_value (FuncState *fs, int list, OpCode hasvalue) { |
| 363 | if (list == 0) return 0; | 366 | /* check whether list has a jump without a value */ |
| 364 | else { /* check whether list has a jump without a value */ | 367 | for (; list != NO_JUMP; list = luaK_getjump(fs, list)) |
| 365 | Instruction *code = fs->f->code; | 368 | if (GET_OPCODE(fs->f->code[list]) != hasvalue) return 1; |
| 366 | for (;;) { | 369 | return 0; /* not found */ |
| 367 | int next = GETARG_S(code[list]); | ||
| 368 | if (GET_OPCODE(code[list]) != hasvalue) return 1; | ||
| 369 | else if (next == 0) return 0; | ||
| 370 | list += next+1; | ||
| 371 | } | ||
| 372 | } | ||
| 373 | } | 370 | } |
| 374 | 371 | ||
| 375 | 372 | ||
| 376 | static void concatlists (LexState *ls, int *l1, int l2) { | 373 | static void concatlists (FuncState *fs, int *l1, int l2) { |
| 377 | if (*l1 == 0) | 374 | if (*l1 == NO_JUMP) |
| 378 | *l1 = l2; | 375 | *l1 = l2; |
| 379 | else if (l2 != 0) { | 376 | else { |
| 380 | FuncState *fs = ls->fs; | ||
| 381 | int list = *l1; | 377 | int list = *l1; |
| 382 | for (;;) { /* traverse `l1' */ | 378 | for (;;) { /* traverse `l1' */ |
| 383 | int next = GETARG_S(fs->f->code[list]); | 379 | int next = luaK_getjump(fs, list); |
| 384 | if (next == 0) { /* end of list? */ | 380 | if (next == NO_JUMP) { /* end of list? */ |
| 385 | SETARG_S(fs->f->code[list], l2-(list+1)); /* end points to `l2' */ | 381 | luaK_fixjump(fs, list, l2); |
| 386 | return; | 382 | return; |
| 387 | } | 383 | } |
| 388 | list += next+1; | 384 | list = next; |
| 389 | } | 385 | } |
| 390 | } | 386 | } |
| 391 | } | 387 | } |
| 392 | 388 | ||
| 393 | 389 | ||
| 394 | void luaK_goiftrue (LexState *ls, expdesc *v, int keepvalue) { | 390 | void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue) { |
| 395 | FuncState *fs = ls->fs; | ||
| 396 | Instruction *previous; | 391 | Instruction *previous; |
| 397 | discharge1(ls, v); | 392 | discharge1(fs, v); |
| 398 | previous = &fs->f->code[fs->pc-1]; | 393 | previous = &fs->f->code[fs->pc-1]; |
| 399 | if (ISJUMP(GET_OPCODE(*previous))) | 394 | if (ISJUMP(GET_OPCODE(*previous))) |
| 400 | SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous))); | 395 | SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous))); |
| 401 | else { | 396 | else { |
| 402 | OpCode jump = keepvalue ? OP_ONFJMP : OP_IFFJMP; | 397 | OpCode jump = keepvalue ? OP_ONFJMP : OP_IFFJMP; |
| 403 | luaK_jump(ls, jump); | 398 | luaK_jump(fs, jump); |
| 404 | } | 399 | } |
| 405 | insert_last(fs, &v->u.l.f); | 400 | insert_last(fs, &v->u.l.f); |
| 406 | luaK_patchlist(ls, v->u.l.t, luaK_getlabel(ls)); | 401 | luaK_patchlist(fs, v->u.l.t, luaK_getlabel(fs)); |
| 407 | v->u.l.t = 0; | 402 | v->u.l.t = NO_JUMP; |
| 408 | } | 403 | } |
| 409 | 404 | ||
| 410 | 405 | ||
| 411 | void luaK_goiffalse (LexState *ls, expdesc *v, int keepvalue) { | 406 | void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue) { |
| 412 | FuncState *fs = ls->fs; | ||
| 413 | Instruction previous; | 407 | Instruction previous; |
| 414 | discharge1(ls, v); | 408 | discharge1(fs, v); |
| 415 | previous = fs->f->code[fs->pc-1]; | 409 | previous = fs->f->code[fs->pc-1]; |
| 416 | if (!ISJUMP(GET_OPCODE(previous))) { | 410 | if (!ISJUMP(GET_OPCODE(previous))) { |
| 417 | OpCode jump = keepvalue ? OP_ONTJMP : OP_IFTJMP; | 411 | OpCode jump = keepvalue ? OP_ONTJMP : OP_IFTJMP; |
| 418 | luaK_jump(ls, jump); | 412 | luaK_jump(fs, jump); |
| 419 | } | 413 | } |
| 420 | insert_last(fs, &v->u.l.t); | 414 | insert_last(fs, &v->u.l.t); |
| 421 | luaK_patchlist(ls, v->u.l.f, luaK_getlabel(ls)); | 415 | luaK_patchlist(fs, v->u.l.f, luaK_getlabel(fs)); |
| 422 | v->u.l.f = 0; | 416 | v->u.l.f = NO_JUMP; |
| 423 | } | 417 | } |
| 424 | 418 | ||
| 425 | 419 | ||
| 426 | void luaK_tostack (LexState *ls, expdesc *v, int onlyone) { | 420 | void luaK_tostack (LexState *ls, expdesc *v, int onlyone) { |
| 427 | if (discharge(ls, v)) return; | 421 | FuncState *fs = ls->fs; |
| 422 | if (discharge(fs, v)) return; | ||
| 428 | else { /* is an expression */ | 423 | else { /* is an expression */ |
| 429 | FuncState *fs = ls->fs; | ||
| 430 | OpCode previous = GET_OPCODE(fs->f->code[fs->pc-1]); | 424 | OpCode previous = GET_OPCODE(fs->f->code[fs->pc-1]); |
| 431 | if (!ISJUMP(previous) && v->u.l.f == 0 && v->u.l.t == 0) { | 425 | if (!ISJUMP(previous) && v->u.l.f == NO_JUMP && v->u.l.t == NO_JUMP) { |
| 432 | /* it is an expression without jumps */ | 426 | /* it is an expression without jumps */ |
| 433 | if (onlyone && v->k == VEXP) | 427 | if (onlyone && v->k == VEXP) |
| 434 | luaK_setcallreturns(ls, 1); /* call must return 1 value */ | 428 | luaK_setcallreturns(fs, 1); /* call must return 1 value */ |
| 435 | return; | 429 | return; |
| 436 | } | 430 | } |
| 437 | else { /* expression has jumps... */ | 431 | else { /* expression has jumps... */ |
| @@ -440,48 +434,48 @@ void luaK_tostack (LexState *ls, expdesc *v, int onlyone) { | |||
| 440 | int final; /* position after whole expression */ | 434 | int final; /* position after whole expression */ |
| 441 | if (ISJUMP(previous)) { | 435 | if (ISJUMP(previous)) { |
| 442 | insert_last(fs, &v->u.l.t); /* put `previous' in true list */ | 436 | insert_last(fs, &v->u.l.t); /* put `previous' in true list */ |
| 443 | p_nil = luaK_0(ls, OP_PUSHNILJMP, 0); | 437 | p_nil = luaK_0(fs, OP_PUSHNILJMP, 0); |
| 444 | p_1 = luaK_S(ls, OP_PUSHINT, 1, 1); | 438 | p_1 = luaK_S(fs, OP_PUSHINT, 1, 1); |
| 445 | } | 439 | } |
| 446 | else { /* still may need a PUSHNIL or a PUSHINT */ | 440 | else { /* still may need a PUSHNIL or a PUSHINT */ |
| 447 | int need_nil = need_value(fs, v->u.l.f, OP_ONFJMP); | 441 | int need_nil = need_value(fs, v->u.l.f, OP_ONFJMP); |
| 448 | int need_1 = need_value(fs, v->u.l.t, OP_ONTJMP); | 442 | int need_1 = need_value(fs, v->u.l.t, OP_ONTJMP); |
| 449 | if (need_nil && need_1) { | 443 | if (need_nil && need_1) { |
| 450 | luaK_S(ls, OP_JMP, 2, 0); /* skip both pushes */ | 444 | luaK_S(fs, OP_JMP, 2, 0); /* skip both pushes */ |
| 451 | p_nil = luaK_0(ls, OP_PUSHNILJMP, 0); | 445 | p_nil = luaK_0(fs, OP_PUSHNILJMP, 0); |
| 452 | p_1 = luaK_S(ls, OP_PUSHINT, 1, 0); | 446 | p_1 = luaK_S(fs, OP_PUSHINT, 1, 0); |
| 453 | } | 447 | } |
| 454 | else if (need_nil || need_1) { | 448 | else if (need_nil || need_1) { |
| 455 | luaK_S(ls, OP_JMP, 1, 0); /* skip one push */ | 449 | luaK_S(fs, OP_JMP, 1, 0); /* skip one push */ |
| 456 | if (need_nil) | 450 | if (need_nil) |
| 457 | p_nil = luaK_U(ls, OP_PUSHNIL, 1, 0); | 451 | p_nil = luaK_U(fs, OP_PUSHNIL, 1, 0); |
| 458 | else /* need_1 */ | 452 | else /* need_1 */ |
| 459 | p_1 = luaK_S(ls, OP_PUSHINT, 1, 0); | 453 | p_1 = luaK_S(fs, OP_PUSHINT, 1, 0); |
| 460 | } | 454 | } |
| 461 | } | 455 | } |
| 462 | final = luaK_getlabel(ls); | 456 | final = luaK_getlabel(fs); |
| 463 | luaK_patchlistaux(ls, v->u.l.f, p_nil, OP_ONFJMP, final); | 457 | luaK_patchlistaux(fs, v->u.l.f, p_nil, OP_ONFJMP, final); |
| 464 | luaK_patchlistaux(ls, v->u.l.t, p_1, OP_ONTJMP, final); | 458 | luaK_patchlistaux(fs, v->u.l.t, p_1, OP_ONTJMP, final); |
| 465 | v->u.l.f = v->u.l.t = 0; | 459 | v->u.l.f = v->u.l.t = NO_JUMP; |
| 466 | } | 460 | } |
| 467 | } | 461 | } |
| 468 | } | 462 | } |
| 469 | 463 | ||
| 470 | 464 | ||
| 471 | void luaK_prefix (LexState *ls, int op, expdesc *v) { | 465 | void luaK_prefix (LexState *ls, int op, expdesc *v) { |
| 466 | FuncState *fs = ls->fs; | ||
| 472 | if (op == '-') { | 467 | if (op == '-') { |
| 473 | luaK_tostack(ls, v, 1); | 468 | luaK_tostack(ls, v, 1); |
| 474 | luaK_minus(ls); | 469 | luaK_minus(fs); |
| 475 | } | 470 | } |
| 476 | else { /* op == NOT */ | 471 | else { /* op == NOT */ |
| 477 | FuncState *fs = ls->fs; | ||
| 478 | Instruction *previous; | 472 | Instruction *previous; |
| 479 | discharge1(ls, v); | 473 | discharge1(fs, v); |
| 480 | previous = &fs->f->code[fs->pc-1]; | 474 | previous = &fs->f->code[fs->pc-1]; |
| 481 | if (ISJUMP(GET_OPCODE(*previous))) | 475 | if (ISJUMP(GET_OPCODE(*previous))) |
| 482 | SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous))); | 476 | SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous))); |
| 483 | else | 477 | else |
| 484 | luaK_0(ls, OP_NOT, 0); | 478 | luaK_0(fs, OP_NOT, 0); |
| 485 | /* interchange true and false lists */ | 479 | /* interchange true and false lists */ |
| 486 | { int temp = v->u.l.f; v->u.l.f = v->u.l.t; v->u.l.t = temp; } | 480 | { int temp = v->u.l.f; v->u.l.f = v->u.l.t; v->u.l.t = temp; } |
| 487 | } | 481 | } |
| @@ -489,43 +483,45 @@ void luaK_prefix (LexState *ls, int op, expdesc *v) { | |||
| 489 | 483 | ||
| 490 | 484 | ||
| 491 | void luaK_infix (LexState *ls, int op, expdesc *v) { | 485 | void luaK_infix (LexState *ls, int op, expdesc *v) { |
| 486 | FuncState *fs = ls->fs; | ||
| 492 | if (op == TK_AND) | 487 | if (op == TK_AND) |
| 493 | luaK_goiftrue(ls, v, 1); | 488 | luaK_goiftrue(fs, v, 1); |
| 494 | else if (op == TK_OR) | 489 | else if (op == TK_OR) |
| 495 | luaK_goiffalse(ls, v, 1); | 490 | luaK_goiffalse(fs, v, 1); |
| 496 | else | 491 | else |
| 497 | luaK_tostack(ls, v, 1); /* all other binary operators need a value */ | 492 | luaK_tostack(ls, v, 1); /* all other binary operators need a value */ |
| 498 | } | 493 | } |
| 499 | 494 | ||
| 500 | 495 | ||
| 501 | void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) { | 496 | void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) { |
| 497 | FuncState *fs = ls->fs; | ||
| 502 | if (op == TK_AND) { | 498 | if (op == TK_AND) { |
| 503 | LUA_ASSERT(ls->L, v1->u.l.t == 0, "list must be closed"); | 499 | LUA_ASSERT(ls->L, v1->u.l.t == NO_JUMP, "list must be closed"); |
| 504 | discharge1(ls, v2); | 500 | discharge1(fs, v2); |
| 505 | v1->u.l.t = v2->u.l.t; | 501 | v1->u.l.t = v2->u.l.t; |
| 506 | concatlists(ls, &v1->u.l.f, v2->u.l.f); | 502 | concatlists(fs, &v1->u.l.f, v2->u.l.f); |
| 507 | } | 503 | } |
| 508 | else if (op == TK_OR) { | 504 | else if (op == TK_OR) { |
| 509 | LUA_ASSERT(ls->L, v1->u.l.f == 0, "list must be closed"); | 505 | LUA_ASSERT(ls->L, v1->u.l.f == NO_JUMP, "list must be closed"); |
| 510 | discharge1(ls, v2); | 506 | discharge1(fs, v2); |
| 511 | v1->u.l.f = v2->u.l.f; | 507 | v1->u.l.f = v2->u.l.f; |
| 512 | concatlists(ls, &v1->u.l.t, v2->u.l.t); | 508 | concatlists(fs, &v1->u.l.t, v2->u.l.t); |
| 513 | } | 509 | } |
| 514 | else { | 510 | else { |
| 515 | luaK_tostack(ls, v2, 1); /* `v2' must be a value */ | 511 | luaK_tostack(ls, v2, 1); /* `v2' must be a value */ |
| 516 | switch (op) { | 512 | switch (op) { |
| 517 | case '+': luaK_add(ls); break; | 513 | case '+': luaK_add(fs); break; |
| 518 | case '-': luaK_sub(ls); break; | 514 | case '-': luaK_sub(fs); break; |
| 519 | case '*': luaK_0(ls, OP_MULT, -1); break; | 515 | case '*': luaK_0(fs, OP_MULT, -1); break; |
| 520 | case '/': luaK_0(ls, OP_DIV, -1); break; | 516 | case '/': luaK_0(fs, OP_DIV, -1); break; |
| 521 | case '^': luaK_0(ls, OP_POW, -1); break; | 517 | case '^': luaK_0(fs, OP_POW, -1); break; |
| 522 | case TK_CONC: luaK_conc(ls); break; | 518 | case TK_CONC: luaK_conc(fs); break; |
| 523 | case TK_EQ: luaK_eq(ls); break; | 519 | case TK_EQ: luaK_eq(fs); break; |
| 524 | case TK_NE: luaK_neq(ls); break; | 520 | case TK_NE: luaK_neq(fs); break; |
| 525 | case '>': luaK_S(ls, OP_IFGTJMP, 0, -2); break; | 521 | case '>': luaK_S(fs, OP_IFGTJMP, 0, -2); break; |
| 526 | case '<': luaK_S(ls, OP_IFLTJMP, 0, -2); break; | 522 | case '<': luaK_S(fs, OP_IFLTJMP, 0, -2); break; |
| 527 | case TK_GE: luaK_S(ls, OP_IFGEJMP, 0, -2); break; | 523 | case TK_GE: luaK_S(fs, OP_IFGEJMP, 0, -2); break; |
| 528 | case TK_LE: luaK_S(ls, OP_IFLEJMP, 0, -2); break; | 524 | case TK_LE: luaK_S(fs, OP_IFLEJMP, 0, -2); break; |
| 529 | } | 525 | } |
| 530 | } | 526 | } |
| 531 | } | 527 | } |
