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 | |
| parent | 73aa465a8ed8dee6c6a27a6f8b2f51227b70789d (diff) | |
| download | lua-f0b697e01caf626785cd9572f82fe98c4a3889fd.tar.gz lua-f0b697e01caf626785cd9572f82fe98c4a3889fd.tar.bz2 lua-f0b697e01caf626785cd9572f82fe98c4a3889fd.zip | |
details
| -rw-r--r-- | lcode.c | 366 | ||||
| -rw-r--r-- | lcode.h | 28 | ||||
| -rw-r--r-- | lopcodes.h | 10 | ||||
| -rw-r--r-- | lparser.c | 154 | ||||
| -rw-r--r-- | lparser.h | 6 |
5 files changed, 289 insertions, 275 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 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lcode.h,v 1.5 2000/03/09 13:57:37 roberto Exp roberto $ | 2 | ** $Id: lcode.h,v 1.6 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 | */ |
| @@ -20,20 +20,20 @@ | |||
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | void luaK_error (LexState *ls, const char *msg); | 22 | void luaK_error (LexState *ls, const char *msg); |
| 23 | int luaK_primitivecode (LexState *ls, Instruction i); | 23 | int luaK_primitivecode (FuncState *fs, Instruction i); |
| 24 | int luaK_code (LexState *ls, Instruction i, int delta); | 24 | int luaK_code (FuncState *fs, Instruction i, int delta); |
| 25 | void luaK_retcode (LexState *ls, int nlocals, int nexps); | 25 | void luaK_retcode (FuncState *fs, int nlocals, int nexps); |
| 26 | void luaK_fixjump (LexState *ls, int pc, int dest); | 26 | void luaK_fixjump (FuncState *fs, int pc, int dest); |
| 27 | void luaK_patchlist (LexState *ls, int list, int target); | 27 | void luaK_patchlist (FuncState *fs, int list, int target); |
| 28 | void luaK_goiftrue (LexState *ls, expdesc *v, int keepvalue); | 28 | void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue); |
| 29 | void luaK_goiffalse (LexState *ls, expdesc *v, int keepvalue); | 29 | void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue); |
| 30 | int luaK_getlabel (LexState *ls); | 30 | int luaK_getlabel (FuncState *fs); |
| 31 | void luaK_deltastack (LexState *ls, int delta); | 31 | void luaK_deltastack (FuncState *fs, int delta); |
| 32 | void luaK_kstr (LexState *ls, int c); | 32 | void luaK_kstr (LexState *ls, int c); |
| 33 | void luaK_number (LexState *ls, Number f); | 33 | void luaK_number (FuncState *fs, Number f); |
| 34 | void luaK_adjuststack (LexState *ls, int n); | 34 | void luaK_adjuststack (FuncState *fs, int n); |
| 35 | int luaK_lastisopen (LexState *ls); | 35 | int luaK_lastisopen (FuncState *fs); |
| 36 | void luaK_setcallreturns (LexState *ls, int nresults); | 36 | void luaK_setcallreturns (FuncState *fs, int nresults); |
| 37 | void luaK_tostack (LexState *ls, expdesc *v, int onlyone); | 37 | void luaK_tostack (LexState *ls, expdesc *v, int onlyone); |
| 38 | void luaK_storevar (LexState *ls, const expdesc *var); | 38 | void luaK_storevar (LexState *ls, const expdesc *var); |
| 39 | void luaK_prefix (LexState *ls, int op, expdesc *v); | 39 | void luaK_prefix (LexState *ls, int op, expdesc *v); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lopcodes.h,v 1.47 2000/03/09 13:57:37 roberto Exp roberto $ | 2 | ** $Id: lopcodes.h,v 1.48 2000/03/10 18:37:44 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 | */ |
| @@ -20,8 +20,8 @@ | |||
| 20 | type 3: 1st unsigned argument in the higher 16 bits (`A') | 20 | type 3: 1st unsigned argument in the higher 16 bits (`A') |
| 21 | 2nd unsigned argument in the middle 8 bits (`B') | 21 | 2nd unsigned argument in the middle 8 bits (`B') |
| 22 | 22 | ||
| 23 | The signed argument is represented in excess 2^23; that is, the Number value | 23 | The signed argument is represented in excess 2^23; that is, the number |
| 24 | is the usigned value minus 2^23. | 24 | value is the usigned value minus 2^23. |
| 25 | ===========================================================================*/ | 25 | ===========================================================================*/ |
| 26 | 26 | ||
| 27 | #define SIZE_INSTRUCTION 32 | 27 | #define SIZE_INSTRUCTION 32 |
| @@ -92,7 +92,7 @@ OP_END,/* - - (return) */ | |||
| 92 | OP_RETURN,/* U - (return) */ | 92 | OP_RETURN,/* U - (return) */ |
| 93 | 93 | ||
| 94 | OP_CALL,/* A B v_n-v_1 f(at a) r_b-r_1 f(v1,...,v_n) */ | 94 | OP_CALL,/* A B v_n-v_1 f(at a) r_b-r_1 f(v1,...,v_n) */ |
| 95 | OP_TAILCALL,/* A B v_a-v_1 f (return) f(v1,...,v_a) */ | 95 | OP_TAILCALL,/* A B v_n-v_1 f(at a) (return) f(v1,...,v_n) */ |
| 96 | 96 | ||
| 97 | OP_PUSHNIL,/* U - nil_1-nil_u */ | 97 | OP_PUSHNIL,/* U - nil_1-nil_u */ |
| 98 | OP_POP,/* U a_u-a_1 - */ | 98 | OP_POP,/* U a_u-a_1 - */ |
| @@ -158,7 +158,7 @@ OP_SETLINE/* U - - LINE=u */ | |||
| 158 | 158 | ||
| 159 | 159 | ||
| 160 | #define RFIELDS_PER_FLUSH 32 /* records (SETMAP) */ | 160 | #define RFIELDS_PER_FLUSH 32 /* records (SETMAP) */ |
| 161 | #define LFIELDS_PER_FLUSH 64 /* FPF - lists (SETLIST) (<=MAXARG_B) */ | 161 | #define LFIELDS_PER_FLUSH 64 /* FPF - lists (SETLIST) (<=MAXARG_B) */ |
| 162 | 162 | ||
| 163 | 163 | ||
| 164 | #endif | 164 | #endif |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lparser.c,v 1.67 2000/03/09 13:57:37 roberto Exp roberto $ | 2 | ** $Id: lparser.c,v 1.68 2000/03/10 18:37:44 roberto Exp roberto $ |
| 3 | ** LL(1) Parser and code generator for Lua | 3 | ** LL(1) Parser and code generator for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -115,9 +115,10 @@ static void checklimit (LexState *ls, int val, int limit, const char *msg) { | |||
| 115 | 115 | ||
| 116 | 116 | ||
| 117 | static void check_debugline (LexState *ls) { | 117 | static void check_debugline (LexState *ls) { |
| 118 | if (ls->L->debug && ls->linenumber != ls->fs->lastsetline) { | 118 | FuncState *fs = ls->fs; |
| 119 | luaK_U(ls, OP_SETLINE, ls->linenumber, 0); | 119 | if (ls->L->debug && ls->linenumber != fs->lastsetline) { |
| 120 | ls->fs->lastsetline = ls->linenumber; | 120 | luaK_U(fs, OP_SETLINE, ls->linenumber, 0); |
| 121 | fs->lastsetline = ls->linenumber; | ||
| 121 | } | 122 | } |
| 122 | } | 123 | } |
| 123 | 124 | ||
| @@ -130,11 +131,11 @@ static void check_match (LexState *ls, int what, int who, int where) { | |||
| 130 | } | 131 | } |
| 131 | 132 | ||
| 132 | 133 | ||
| 133 | static int string_constant (LexState *ls, FuncState *fs, TString *s) { | 134 | static int string_constant (FuncState *fs, TString *s) { |
| 134 | Proto *f = fs->f; | 135 | Proto *f = fs->f; |
| 135 | int c = s->constindex; | 136 | int c = s->constindex; |
| 136 | if (c >= f->nkstr || f->kstr[c] != s) { | 137 | if (c >= f->nkstr || f->kstr[c] != s) { |
| 137 | luaM_growvector(ls->L, f->kstr, f->nkstr, 1, TString *, | 138 | luaM_growvector(fs->L, f->kstr, f->nkstr, 1, TString *, |
| 138 | constantEM, MAXARG_U); | 139 | constantEM, MAXARG_U); |
| 139 | c = f->nkstr++; | 140 | c = f->nkstr++; |
| 140 | f->kstr[c] = s; | 141 | f->kstr[c] = s; |
| @@ -145,7 +146,7 @@ static int string_constant (LexState *ls, FuncState *fs, TString *s) { | |||
| 145 | 146 | ||
| 146 | 147 | ||
| 147 | static void code_string (LexState *ls, TString *s) { | 148 | static void code_string (LexState *ls, TString *s) { |
| 148 | luaK_kstr(ls, string_constant(ls, ls->fs, s)); | 149 | luaK_kstr(ls, string_constant(ls->fs, s)); |
| 149 | } | 150 | } |
| 150 | 151 | ||
| 151 | 152 | ||
| @@ -153,7 +154,7 @@ static int checkname (LexState *ls) { | |||
| 153 | int sc; | 154 | int sc; |
| 154 | if (ls->token != TK_NAME) | 155 | if (ls->token != TK_NAME) |
| 155 | luaK_error(ls, "<name> expected"); | 156 | luaK_error(ls, "<name> expected"); |
| 156 | sc = string_constant(ls, ls->fs, ls->seminfo.ts); | 157 | sc = string_constant(ls->fs, ls->seminfo.ts); |
| 157 | next(ls); | 158 | next(ls); |
| 158 | return sc; | 159 | return sc; |
| 159 | } | 160 | } |
| @@ -226,7 +227,7 @@ static void singlevar (LexState *ls, TString *n, expdesc *var, int prev) { | |||
| 226 | if (aux_localname(level, n) >= 0) | 227 | if (aux_localname(level, n) >= 0) |
| 227 | luaX_syntaxerror(ls, "cannot access a variable in outer scope", n->str); | 228 | luaX_syntaxerror(ls, "cannot access a variable in outer scope", n->str); |
| 228 | var->k = VGLOBAL; | 229 | var->k = VGLOBAL; |
| 229 | var->u.index = string_constant(ls, fs, n); | 230 | var->u.index = string_constant(fs, n); |
| 230 | } | 231 | } |
| 231 | } | 232 | } |
| 232 | 233 | ||
| @@ -249,29 +250,31 @@ static int indexupvalue (LexState *ls, TString *n) { | |||
| 249 | 250 | ||
| 250 | 251 | ||
| 251 | static void pushupvalue (LexState *ls, TString *n) { | 252 | static void pushupvalue (LexState *ls, TString *n) { |
| 252 | if (ls->fs->prev == NULL) | 253 | FuncState *fs = ls->fs; |
| 254 | if (fs->prev == NULL) | ||
| 253 | luaX_syntaxerror(ls, "cannot access upvalue in main", n->str); | 255 | luaX_syntaxerror(ls, "cannot access upvalue in main", n->str); |
| 254 | if (aux_localname(ls->fs, n) >= 0) | 256 | if (aux_localname(ls->fs, n) >= 0) |
| 255 | luaX_syntaxerror(ls, "cannot access an upvalue in current scope", n->str); | 257 | luaX_syntaxerror(ls, "cannot access an upvalue in current scope", n->str); |
| 256 | luaK_U(ls, OP_PUSHUPVALUE, indexupvalue(ls, n), 1); | 258 | luaK_U(fs, OP_PUSHUPVALUE, indexupvalue(ls, n), 1); |
| 257 | } | 259 | } |
| 258 | 260 | ||
| 259 | 261 | ||
| 260 | static void adjust_mult_assign (LexState *ls, int nvars, int nexps) { | 262 | static void adjust_mult_assign (LexState *ls, int nvars, int nexps) { |
| 263 | FuncState *fs = ls->fs; | ||
| 261 | int diff = nexps - nvars; | 264 | int diff = nexps - nvars; |
| 262 | if (nexps == 0 || !luaK_lastisopen(ls)) { /* list is empty or closed */ | 265 | if (nexps == 0 || !luaK_lastisopen(fs)) { /* list is empty or closed */ |
| 263 | /* push or pop eventual difference between list lengths */ | 266 | /* push or pop eventual difference between list lengths */ |
| 264 | luaK_adjuststack(ls, diff); | 267 | luaK_adjuststack(fs, diff); |
| 265 | } | 268 | } |
| 266 | else { /* list ends in a function call; must correct it */ | 269 | else { /* list ends in a function call; must correct it */ |
| 267 | diff--; /* do not count function call itself */ | 270 | diff--; /* do not count function call itself */ |
| 268 | if (diff <= 0) { /* more variables than values? */ | 271 | if (diff <= 0) { /* more variables than values? */ |
| 269 | /* function call must provide extra values */ | 272 | /* function call must provide extra values */ |
| 270 | luaK_setcallreturns(ls, -diff); | 273 | luaK_setcallreturns(fs, -diff); |
| 271 | } | 274 | } |
| 272 | else { /* more values than variables */ | 275 | else { /* more values than variables */ |
| 273 | luaK_setcallreturns(ls, 0); /* call should provide no value */ | 276 | luaK_setcallreturns(fs, 0); /* call should provide no value */ |
| 274 | luaK_adjuststack(ls, diff); /* pop eventual extra values */ | 277 | luaK_adjuststack(fs, diff); /* pop eventual extra values */ |
| 275 | } | 278 | } |
| 276 | } | 279 | } |
| 277 | } | 280 | } |
| @@ -285,9 +288,9 @@ static void code_args (LexState *ls, int nparams, int dots) { | |||
| 285 | fs->f->numparams = nparams; | 288 | fs->f->numparams = nparams; |
| 286 | fs->f->is_vararg = dots; | 289 | fs->f->is_vararg = dots; |
| 287 | if (!dots) | 290 | if (!dots) |
| 288 | luaK_deltastack(ls, nparams); | 291 | luaK_deltastack(fs, nparams); |
| 289 | else { | 292 | else { |
| 290 | luaK_deltastack(ls, nparams+1); | 293 | luaK_deltastack(fs, nparams+1); |
| 291 | add_localvar(ls, luaS_newfixed(ls->L, "arg")); | 294 | add_localvar(ls, luaS_newfixed(ls->L, "arg")); |
| 292 | } | 295 | } |
| 293 | } | 296 | } |
| @@ -298,7 +301,7 @@ static int getvarname (LexState *ls, expdesc *var) { | |||
| 298 | case VGLOBAL: | 301 | case VGLOBAL: |
| 299 | return var->u.index; | 302 | return var->u.index; |
| 300 | case VLOCAL: | 303 | case VLOCAL: |
| 301 | return string_constant(ls, ls->fs, ls->fs->localvar[var->u.index]); | 304 | return string_constant(ls->fs, ls->fs->localvar[var->u.index]); |
| 302 | break; | 305 | break; |
| 303 | default: | 306 | default: |
| 304 | error_unexpected(ls); /* there is no `var name' */ | 307 | error_unexpected(ls); /* there is no `var name' */ |
| @@ -308,15 +311,16 @@ static int getvarname (LexState *ls, expdesc *var) { | |||
| 308 | 311 | ||
| 309 | 312 | ||
| 310 | static void func_onstack (LexState *ls, FuncState *func) { | 313 | static void func_onstack (LexState *ls, FuncState *func) { |
| 311 | Proto *f = ls->fs->f; | 314 | FuncState *fs = ls->fs; |
| 315 | Proto *f = fs->f; | ||
| 312 | int i; | 316 | int i; |
| 313 | for (i=0; i<func->nupvalues; i++) | 317 | for (i=0; i<func->nupvalues; i++) |
| 314 | luaK_tostack(ls, &func->upvalues[i], 1); | 318 | luaK_tostack(ls, &func->upvalues[i], 1); |
| 315 | luaM_growvector(ls->L, f->kproto, f->nkproto, 1, Proto *, | 319 | luaM_growvector(ls->L, f->kproto, f->nkproto, 1, Proto *, |
| 316 | constantEM, MAXARG_A); | 320 | constantEM, MAXARG_A); |
| 317 | f->kproto[f->nkproto++] = func->f; | 321 | f->kproto[f->nkproto++] = func->f; |
| 318 | luaK_deltastack(ls, 1); /* CLOSURE puts one extra element before popping */ | 322 | luaK_deltastack(fs, 1); /* CLOSURE puts one extra element before popping */ |
| 319 | luaK_AB(ls, OP_CLOSURE, f->nkproto-1, func->nupvalues, -func->nupvalues); | 323 | luaK_AB(fs, OP_CLOSURE, f->nkproto-1, func->nupvalues, -func->nupvalues); |
| 320 | } | 324 | } |
| 321 | 325 | ||
| 322 | 326 | ||
| @@ -324,6 +328,8 @@ static void init_state (LexState *ls, FuncState *fs, TString *source) { | |||
| 324 | lua_State *L = ls->L; | 328 | lua_State *L = ls->L; |
| 325 | Proto *f = luaF_newproto(ls->L); | 329 | Proto *f = luaF_newproto(ls->L); |
| 326 | fs->prev = ls->fs; /* linked list of funcstates */ | 330 | fs->prev = ls->fs; /* linked list of funcstates */ |
| 331 | fs->ls = ls; | ||
| 332 | fs->L = ls->L; | ||
| 327 | ls->fs = fs; | 333 | ls->fs = fs; |
| 328 | fs->stacksize = 0; | 334 | fs->stacksize = 0; |
| 329 | fs->nlocalvar = 0; | 335 | fs->nlocalvar = 0; |
| @@ -346,19 +352,20 @@ static void init_state (LexState *ls, FuncState *fs, TString *source) { | |||
| 346 | 352 | ||
| 347 | 353 | ||
| 348 | static void close_func (LexState *ls) { | 354 | static void close_func (LexState *ls) { |
| 355 | lua_State *L = ls->L; | ||
| 349 | FuncState *fs = ls->fs; | 356 | FuncState *fs = ls->fs; |
| 350 | Proto *f = fs->f; | 357 | Proto *f = fs->f; |
| 351 | luaK_0(ls, OP_END, 0); | 358 | luaK_0(fs, OP_END, 0); |
| 352 | luaM_reallocvector(ls->L, f->code, fs->pc, Instruction); | 359 | luaM_reallocvector(L, f->code, fs->pc, Instruction); |
| 353 | luaM_reallocvector(ls->L, f->kstr, f->nkstr, TString *); | 360 | luaM_reallocvector(L, f->kstr, f->nkstr, TString *); |
| 354 | luaM_reallocvector(ls->L, f->knum, f->nknum, Number); | 361 | luaM_reallocvector(L, f->knum, f->nknum, Number); |
| 355 | luaM_reallocvector(ls->L, f->kproto, f->nkproto, Proto *); | 362 | luaM_reallocvector(L, f->kproto, f->nkproto, Proto *); |
| 356 | if (fs->nvars != -1) { /* debug information? */ | 363 | if (fs->nvars != -1) { /* debug information? */ |
| 357 | luaI_registerlocalvar(ls, NULL, -1); /* flag end of vector */ | 364 | luaI_registerlocalvar(ls, NULL, -1); /* flag end of vector */ |
| 358 | luaM_reallocvector(ls->L, f->locvars, fs->nvars, LocVar); | 365 | luaM_reallocvector(L, f->locvars, fs->nvars, LocVar); |
| 359 | } | 366 | } |
| 360 | ls->fs = fs->prev; | 367 | ls->fs = fs->prev; |
| 361 | ls->L->top--; /* pop function */ | 368 | L->top--; /* pop function */ |
| 362 | } | 369 | } |
| 363 | 370 | ||
| 364 | 371 | ||
| @@ -423,7 +430,7 @@ static void funcargs (LexState *ls, int slf) { | |||
| 423 | check_match(ls, ')', '(', line); | 430 | check_match(ls, ')', '(', line); |
| 424 | #ifdef LUA_COMPAT_ARGRET | 431 | #ifdef LUA_COMPAT_ARGRET |
| 425 | if (nargs > 0) /* arg list is not empty? */ | 432 | if (nargs > 0) /* arg list is not empty? */ |
| 426 | luaK_setcallreturns(ls, 1); /* last call returns only 1 value */ | 433 | luaK_setcallreturns(fs, 1); /* last call returns only 1 value */ |
| 427 | #endif | 434 | #endif |
| 428 | break; | 435 | break; |
| 429 | } | 436 | } |
| @@ -442,7 +449,7 @@ static void funcargs (LexState *ls, int slf) { | |||
| 442 | break; | 449 | break; |
| 443 | } | 450 | } |
| 444 | fs->stacksize = slevel; /* call will remove function and arguments */ | 451 | fs->stacksize = slevel; /* call will remove function and arguments */ |
| 445 | luaK_AB(ls, OP_CALL, slevel, MULT_RET, 0); | 452 | luaK_AB(fs, OP_CALL, slevel, MULT_RET, 0); |
| 446 | } | 453 | } |
| 447 | 454 | ||
| 448 | 455 | ||
| @@ -469,10 +476,10 @@ static void var_or_func_tail (LexState *ls, expdesc *v) { | |||
| 469 | next(ls); | 476 | next(ls); |
| 470 | name = checkname(ls); | 477 | name = checkname(ls); |
| 471 | luaK_tostack(ls, v, 1); /* `v' must be on stack */ | 478 | luaK_tostack(ls, v, 1); /* `v' must be on stack */ |
| 472 | luaK_U(ls, OP_PUSHSELF, name, 1); | 479 | luaK_U(ls->fs, OP_PUSHSELF, name, 1); |
| 473 | funcargs(ls, 1); | 480 | funcargs(ls, 1); |
| 474 | v->k = VEXP; | 481 | v->k = VEXP; |
| 475 | v->u.l.t = v->u.l.f = 0; | 482 | v->u.l.t = v->u.l.f = NO_JUMP; |
| 476 | break; | 483 | break; |
| 477 | } | 484 | } |
| 478 | 485 | ||
| @@ -480,7 +487,7 @@ static void var_or_func_tail (LexState *ls, expdesc *v) { | |||
| 480 | luaK_tostack(ls, v, 1); /* `v' must be on stack */ | 487 | luaK_tostack(ls, v, 1); /* `v' must be on stack */ |
| 481 | funcargs(ls, 0); | 488 | funcargs(ls, 0); |
| 482 | v->k = VEXP; | 489 | v->k = VEXP; |
| 483 | v->u.l.t = v->u.l.f = 0; | 490 | v->u.l.t = v->u.l.f = NO_JUMP; |
| 484 | break; | 491 | break; |
| 485 | 492 | ||
| 486 | default: return; /* should be follow... */ | 493 | default: return; /* should be follow... */ |
| @@ -494,7 +501,7 @@ static void var_or_func (LexState *ls, expdesc *v) { | |||
| 494 | if (optional(ls, '%')) { /* upvalue? */ | 501 | if (optional(ls, '%')) { /* upvalue? */ |
| 495 | pushupvalue(ls, str_checkname(ls)); | 502 | pushupvalue(ls, str_checkname(ls)); |
| 496 | v->k = VEXP; | 503 | v->k = VEXP; |
| 497 | v->u.l.t = v->u.l.f = 0; | 504 | v->u.l.t = v->u.l.f = NO_JUMP; |
| 498 | } | 505 | } |
| 499 | else /* variable name */ | 506 | else /* variable name */ |
| 500 | singlevar(ls, str_checkname(ls), v, 0); | 507 | singlevar(ls, str_checkname(ls), v, 0); |
| @@ -532,6 +539,7 @@ static void recfield (LexState *ls) { | |||
| 532 | 539 | ||
| 533 | static int recfields (LexState *ls) { | 540 | static int recfields (LexState *ls) { |
| 534 | /* recfields -> { ',' recfield } [','] */ | 541 | /* recfields -> { ',' recfield } [','] */ |
| 542 | FuncState *fs = ls->fs; | ||
| 535 | int n = 1; /* one has been read before */ | 543 | int n = 1; /* one has been read before */ |
| 536 | int mod_n = 1; /* mod_n == n%RFIELDS_PER_FLUSH */ | 544 | int mod_n = 1; /* mod_n == n%RFIELDS_PER_FLUSH */ |
| 537 | while (ls->token == ',') { | 545 | while (ls->token == ',') { |
| @@ -541,18 +549,19 @@ static int recfields (LexState *ls) { | |||
| 541 | recfield(ls); | 549 | recfield(ls); |
| 542 | n++; | 550 | n++; |
| 543 | if (++mod_n == RFIELDS_PER_FLUSH) { | 551 | if (++mod_n == RFIELDS_PER_FLUSH) { |
| 544 | luaK_U(ls, OP_SETMAP, RFIELDS_PER_FLUSH-1, -2*RFIELDS_PER_FLUSH); | 552 | luaK_U(fs, OP_SETMAP, RFIELDS_PER_FLUSH-1, -2*RFIELDS_PER_FLUSH); |
| 545 | mod_n = 0; | 553 | mod_n = 0; |
| 546 | } | 554 | } |
| 547 | } | 555 | } |
| 548 | if (mod_n) | 556 | if (mod_n) |
| 549 | luaK_U(ls, OP_SETMAP, mod_n-1, -2*mod_n); | 557 | luaK_U(fs, OP_SETMAP, mod_n-1, -2*mod_n); |
| 550 | return n; | 558 | return n; |
| 551 | } | 559 | } |
| 552 | 560 | ||
| 553 | 561 | ||
| 554 | static int listfields (LexState *ls) { | 562 | static int listfields (LexState *ls) { |
| 555 | /* listfields -> { ',' exp1 } [','] */ | 563 | /* listfields -> { ',' exp1 } [','] */ |
| 564 | FuncState *fs = ls->fs; | ||
| 556 | int n = 1; /* one has been read before */ | 565 | int n = 1; /* one has been read before */ |
| 557 | int mod_n = 1; /* mod_n == n%LFIELDS_PER_FLUSH */ | 566 | int mod_n = 1; /* mod_n == n%LFIELDS_PER_FLUSH */ |
| 558 | while (ls->token == ',') { | 567 | while (ls->token == ',') { |
| @@ -564,13 +573,13 @@ static int listfields (LexState *ls) { | |||
| 564 | checklimit(ls, n, MAXARG_A*LFIELDS_PER_FLUSH, | 573 | checklimit(ls, n, MAXARG_A*LFIELDS_PER_FLUSH, |
| 565 | "items in a list initializer"); | 574 | "items in a list initializer"); |
| 566 | if (++mod_n == LFIELDS_PER_FLUSH) { | 575 | if (++mod_n == LFIELDS_PER_FLUSH) { |
| 567 | luaK_AB(ls, OP_SETLIST, n/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH-1, | 576 | luaK_AB(fs, OP_SETLIST, n/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH-1, |
| 568 | -LFIELDS_PER_FLUSH); | 577 | -LFIELDS_PER_FLUSH); |
| 569 | mod_n = 0; | 578 | mod_n = 0; |
| 570 | } | 579 | } |
| 571 | } | 580 | } |
| 572 | if (mod_n > 0) | 581 | if (mod_n > 0) |
| 573 | luaK_AB(ls, OP_SETLIST, n/LFIELDS_PER_FLUSH, mod_n-1, -mod_n); | 582 | luaK_AB(fs, OP_SETLIST, n/LFIELDS_PER_FLUSH, mod_n-1, -mod_n); |
| 574 | return n; | 583 | return n; |
| 575 | } | 584 | } |
| 576 | 585 | ||
| @@ -618,8 +627,9 @@ static void constructor_part (LexState *ls, constdesc *cd) { | |||
| 618 | 627 | ||
| 619 | static void constructor (LexState *ls) { | 628 | static void constructor (LexState *ls) { |
| 620 | /* constructor -> '{' constructor_part [';' constructor_part] '}' */ | 629 | /* constructor -> '{' constructor_part [';' constructor_part] '}' */ |
| 630 | FuncState *fs = ls->fs; | ||
| 621 | int line = ls->linenumber; | 631 | int line = ls->linenumber; |
| 622 | int pc = luaK_U(ls, OP_CREATETABLE, 0, 1); | 632 | int pc = luaK_U(fs, OP_CREATETABLE, 0, 1); |
| 623 | int nelems; | 633 | int nelems; |
| 624 | constdesc cd; | 634 | constdesc cd; |
| 625 | check(ls, '{'); | 635 | check(ls, '{'); |
| @@ -635,7 +645,7 @@ static void constructor (LexState *ls) { | |||
| 635 | } | 645 | } |
| 636 | check_match(ls, '}', '{', line); | 646 | check_match(ls, '}', '{', line); |
| 637 | /* set initial table size */ | 647 | /* set initial table size */ |
| 638 | SETARG_U(ls->fs->f->code[pc], nelems); | 648 | SETARG_U(fs->f->code[pc], nelems); |
| 639 | } | 649 | } |
| 640 | 650 | ||
| 641 | /* }====================================================================== */ | 651 | /* }====================================================================== */ |
| @@ -651,12 +661,13 @@ static void constructor (LexState *ls) { | |||
| 651 | 661 | ||
| 652 | 662 | ||
| 653 | static void simpleexp (LexState *ls, expdesc *v) { | 663 | static void simpleexp (LexState *ls, expdesc *v) { |
| 664 | FuncState *fs = ls->fs; | ||
| 654 | check_debugline(ls); | 665 | check_debugline(ls); |
| 655 | switch (ls->token) { | 666 | switch (ls->token) { |
| 656 | case TK_NUMBER: { /* simpleexp -> NUMBER */ | 667 | case TK_NUMBER: { /* simpleexp -> NUMBER */ |
| 657 | Number r = ls->seminfo.r; | 668 | Number r = ls->seminfo.r; |
| 658 | next(ls); | 669 | next(ls); |
| 659 | luaK_number(ls, r); | 670 | luaK_number(fs, r); |
| 660 | break; | 671 | break; |
| 661 | } | 672 | } |
| 662 | 673 | ||
| @@ -666,7 +677,7 @@ static void simpleexp (LexState *ls, expdesc *v) { | |||
| 666 | break; | 677 | break; |
| 667 | 678 | ||
| 668 | case TK_NIL: /* simpleexp -> NIL */ | 679 | case TK_NIL: /* simpleexp -> NIL */ |
| 669 | luaK_adjuststack(ls, -1); | 680 | luaK_adjuststack(fs, -1); |
| 670 | next(ls); | 681 | next(ls); |
| 671 | break; | 682 | break; |
| 672 | 683 | ||
| @@ -694,7 +705,7 @@ static void simpleexp (LexState *ls, expdesc *v) { | |||
| 694 | return; | 705 | return; |
| 695 | } | 706 | } |
| 696 | v->k = VEXP; | 707 | v->k = VEXP; |
| 697 | v->u.l.t = v->u.l.f = 0; | 708 | v->u.l.t = v->u.l.f = NO_JUMP; |
| 698 | } | 709 | } |
| 699 | 710 | ||
| 700 | 711 | ||
| @@ -776,7 +787,7 @@ static void block (LexState *ls) { | |||
| 776 | FuncState *fs = ls->fs; | 787 | FuncState *fs = ls->fs; |
| 777 | int nlocalvar = fs->nlocalvar; | 788 | int nlocalvar = fs->nlocalvar; |
| 778 | chunk(ls); | 789 | chunk(ls); |
| 779 | luaK_adjuststack(ls, fs->nlocalvar - nlocalvar); /* remove local variables */ | 790 | luaK_adjuststack(fs, fs->nlocalvar - nlocalvar); /* remove local variables */ |
| 780 | for (; fs->nlocalvar > nlocalvar; fs->nlocalvar--) | 791 | for (; fs->nlocalvar > nlocalvar; fs->nlocalvar--) |
| 781 | luaI_unregisterlocalvar(ls, fs->lastsetline); | 792 | luaI_unregisterlocalvar(ls, fs->lastsetline); |
| 782 | } | 793 | } |
| @@ -806,7 +817,7 @@ static int assignment (LexState *ls, expdesc *v, int nvars) { | |||
| 806 | luaK_storevar(ls, v); | 817 | luaK_storevar(ls, v); |
| 807 | } | 818 | } |
| 808 | else { /* indexed var with values in between*/ | 819 | else { /* indexed var with values in between*/ |
| 809 | luaK_U(ls, OP_SETTABLE, left+(nvars-1), -1); | 820 | luaK_U(ls->fs, OP_SETTABLE, left+(nvars-1), -1); |
| 810 | left += 2; /* table&index are not popped, because they aren't on top */ | 821 | left += 2; /* table&index are not popped, because they aren't on top */ |
| 811 | } | 822 | } |
| 812 | return left; | 823 | return left; |
| @@ -822,7 +833,7 @@ static void whilestat (LexState *ls, int line) { | |||
| 822 | /* whilestat -> WHILE exp1 DO block END */ | 833 | /* whilestat -> WHILE exp1 DO block END */ |
| 823 | Instruction buffer[MAX_WHILE_EXP]; | 834 | Instruction buffer[MAX_WHILE_EXP]; |
| 824 | FuncState *fs = ls->fs; | 835 | FuncState *fs = ls->fs; |
| 825 | int while_init = luaK_getlabel(ls); | 836 | int while_init = luaK_getlabel(fs); |
| 826 | int loopentry; /* point to jump to repeat the loop */ | 837 | int loopentry; /* point to jump to repeat the loop */ |
| 827 | int cond_init; /* init of condition, after the move */ | 838 | int cond_init; /* init of condition, after the move */ |
| 828 | int cond_size; | 839 | int cond_size; |
| @@ -830,7 +841,7 @@ static void whilestat (LexState *ls, int line) { | |||
| 830 | int i; | 841 | int i; |
| 831 | next(ls); /* skip WHILE */ | 842 | next(ls); /* skip WHILE */ |
| 832 | expr(ls, &v); /* read condition */ | 843 | expr(ls, &v); /* read condition */ |
| 833 | luaK_goiffalse(ls, &v, 0); | 844 | luaK_goiffalse(fs, &v, 0); |
| 834 | cond_size = fs->pc - while_init; | 845 | cond_size = fs->pc - while_init; |
| 835 | /* save condition (to move it to after body) */ | 846 | /* save condition (to move it to after body) */ |
| 836 | if (cond_size > MAX_WHILE_EXP) | 847 | if (cond_size > MAX_WHILE_EXP) |
| @@ -838,31 +849,32 @@ static void whilestat (LexState *ls, int line) { | |||
| 838 | for (i=0; i<cond_size; i++) buffer[i] = fs->f->code[while_init+i]; | 849 | for (i=0; i<cond_size; i++) buffer[i] = fs->f->code[while_init+i]; |
| 839 | /* go back to state prior condition */ | 850 | /* go back to state prior condition */ |
| 840 | fs->pc = while_init; | 851 | fs->pc = while_init; |
| 841 | luaK_S(ls, OP_JMP, 0, 0); /* initial jump to condition */ | 852 | luaK_S(fs, OP_JMP, 0, 0); /* initial jump to condition */ |
| 842 | check(ls, TK_DO); | 853 | check(ls, TK_DO); |
| 843 | loopentry = luaK_getlabel(ls); | 854 | loopentry = luaK_getlabel(fs); |
| 844 | block(ls); | 855 | block(ls); |
| 845 | check_match(ls, TK_END, TK_WHILE, line); | 856 | check_match(ls, TK_END, TK_WHILE, line); |
| 846 | cond_init = luaK_getlabel(ls); | 857 | cond_init = luaK_getlabel(fs); |
| 847 | luaK_fixjump(ls, while_init, cond_init); | 858 | luaK_fixjump(fs, while_init, cond_init); |
| 848 | /* correct `v' and copy condition to new position */ | 859 | /* correct `v' and copy condition to new position */ |
| 849 | if (v.u.l.t != 0) v.u.l.t += cond_init-while_init; | 860 | if (v.u.l.t != NO_JUMP) v.u.l.t += cond_init-while_init; |
| 850 | for (i=0; i<cond_size; i++) luaK_primitivecode(ls, buffer[i]); | 861 | for (i=0; i<cond_size; i++) luaK_primitivecode(fs, buffer[i]); |
| 851 | luaK_patchlist(ls, v.u.l.t, loopentry); | 862 | luaK_patchlist(fs, v.u.l.t, loopentry); |
| 852 | luaK_getlabel(ls); /* mark possible jump to this point */ | 863 | luaK_getlabel(fs); /* mark possible jump to this point */ |
| 853 | } | 864 | } |
| 854 | 865 | ||
| 855 | 866 | ||
| 856 | static void repeatstat (LexState *ls, int line) { | 867 | static void repeatstat (LexState *ls, int line) { |
| 857 | /* repeatstat -> REPEAT block UNTIL exp1 */ | 868 | /* repeatstat -> REPEAT block UNTIL exp1 */ |
| 858 | int repeat_init = luaK_getlabel(ls); | 869 | FuncState *fs = ls->fs; |
| 870 | int repeat_init = luaK_getlabel(fs); | ||
| 859 | expdesc v; | 871 | expdesc v; |
| 860 | next(ls); | 872 | next(ls); |
| 861 | block(ls); | 873 | block(ls); |
| 862 | check_match(ls, TK_UNTIL, TK_REPEAT, line); | 874 | check_match(ls, TK_UNTIL, TK_REPEAT, line); |
| 863 | expr(ls, &v); | 875 | expr(ls, &v); |
| 864 | luaK_goiftrue(ls, &v, 0); | 876 | luaK_goiftrue(fs, &v, 0); |
| 865 | luaK_patchlist(ls, v.u.l.f, repeat_init); | 877 | luaK_patchlist(fs, v.u.l.f, repeat_init); |
| 866 | } | 878 | } |
| 867 | 879 | ||
| 868 | 880 | ||
| @@ -935,17 +947,18 @@ static int funcstat (LexState *ls, int line) { | |||
| 935 | 947 | ||
| 936 | static void namestat (LexState *ls) { | 948 | static void namestat (LexState *ls) { |
| 937 | /* stat -> func | ['%'] NAME assignment */ | 949 | /* stat -> func | ['%'] NAME assignment */ |
| 950 | FuncState *fs = ls->fs; | ||
| 938 | expdesc v; | 951 | expdesc v; |
| 939 | check_debugline(ls); | 952 | check_debugline(ls); |
| 940 | var_or_func(ls, &v); | 953 | var_or_func(ls, &v); |
| 941 | if (v.k == VEXP) { /* stat -> func */ | 954 | if (v.k == VEXP) { /* stat -> func */ |
| 942 | if (!luaK_lastisopen(ls)) /* is just an upvalue? */ | 955 | if (!luaK_lastisopen(fs)) /* is just an upvalue? */ |
| 943 | luaK_error(ls, "syntax error"); | 956 | luaK_error(ls, "syntax error"); |
| 944 | luaK_setcallreturns(ls, 0); /* call statement uses no results */ | 957 | luaK_setcallreturns(fs, 0); /* call statement uses no results */ |
| 945 | } | 958 | } |
| 946 | else { /* stat -> ['%'] NAME assignment */ | 959 | else { /* stat -> ['%'] NAME assignment */ |
| 947 | int left = assignment(ls, &v, 1); | 960 | int left = assignment(ls, &v, 1); |
| 948 | luaK_adjuststack(ls, left); /* remove eventual garbage left on stack */ | 961 | luaK_adjuststack(fs, left); /* remove eventual garbage left on stack */ |
| 949 | } | 962 | } |
| 950 | } | 963 | } |
| 951 | 964 | ||
| @@ -957,11 +970,11 @@ static void ifpart (LexState *ls, int line) { | |||
| 957 | int elseinit; | 970 | int elseinit; |
| 958 | next(ls); /* skip IF or ELSEIF */ | 971 | next(ls); /* skip IF or ELSEIF */ |
| 959 | expr(ls, &v); /* cond */ | 972 | expr(ls, &v); /* cond */ |
| 960 | luaK_goiftrue(ls, &v, 0); | 973 | luaK_goiftrue(fs, &v, 0); |
| 961 | check(ls, TK_THEN); | 974 | check(ls, TK_THEN); |
| 962 | block(ls); /* `then' part */ | 975 | block(ls); /* `then' part */ |
| 963 | luaK_S(ls, OP_JMP, 0, 0); /* 2nd jump: over `else' part */ | 976 | luaK_S(fs, OP_JMP, 0, 0); /* 2nd jump: over `else' part */ |
| 964 | elseinit = luaK_getlabel(ls); /* address of 2nd jump == elseinit-1 */ | 977 | elseinit = luaK_getlabel(fs); /* address of 2nd jump == elseinit-1 */ |
| 965 | if (ls->token == TK_ELSEIF) | 978 | if (ls->token == TK_ELSEIF) |
| 966 | ifpart(ls, line); | 979 | ifpart(ls, line); |
| 967 | else { | 980 | else { |
| @@ -970,13 +983,13 @@ static void ifpart (LexState *ls, int line) { | |||
| 970 | check_match(ls, TK_END, TK_IF, line); | 983 | check_match(ls, TK_END, TK_IF, line); |
| 971 | } | 984 | } |
| 972 | if (fs->pc > elseinit) { /* is there an `else' part? */ | 985 | if (fs->pc > elseinit) { /* is there an `else' part? */ |
| 973 | luaK_fixjump(ls, elseinit-1, luaK_getlabel(ls)); /* fix 2nd jump */ | 986 | luaK_fixjump(fs, elseinit-1, luaK_getlabel(fs)); /* fix 2nd jump */ |
| 974 | } | 987 | } |
| 975 | else { /* no else part */ | 988 | else { /* no else part */ |
| 976 | fs->pc--; /* remove 2nd jump */ | 989 | fs->pc--; /* remove 2nd jump */ |
| 977 | elseinit = luaK_getlabel(ls); /* `elseinit' points to end */ | 990 | elseinit = luaK_getlabel(fs); /* `elseinit' points to end */ |
| 978 | } | 991 | } |
| 979 | luaK_patchlist(ls, v.u.l.f, elseinit); /* fix 1st jump to `else' part */ | 992 | luaK_patchlist(fs, v.u.l.f, elseinit); /* fix 1st jump to `else' part */ |
| 980 | } | 993 | } |
| 981 | 994 | ||
| 982 | 995 | ||
| @@ -1080,12 +1093,13 @@ static void body (LexState *ls, int needself, int line) { | |||
| 1080 | static void ret (LexState *ls) { | 1093 | static void ret (LexState *ls) { |
| 1081 | /* ret -> [RETURN explist sc] */ | 1094 | /* ret -> [RETURN explist sc] */ |
| 1082 | if (ls->token == TK_RETURN) { | 1095 | if (ls->token == TK_RETURN) { |
| 1096 | FuncState *fs = ls->fs; | ||
| 1083 | int nexps; /* number of expressions returned */ | 1097 | int nexps; /* number of expressions returned */ |
| 1084 | check_debugline(ls); | 1098 | check_debugline(ls); |
| 1085 | next(ls); | 1099 | next(ls); |
| 1086 | nexps = explist(ls); | 1100 | nexps = explist(ls); |
| 1087 | luaK_retcode(ls, ls->fs->nlocalvar, nexps); | 1101 | luaK_retcode(fs, ls->fs->nlocalvar, nexps); |
| 1088 | ls->fs->stacksize = ls->fs->nlocalvar; /* removes all temp values */ | 1102 | fs->stacksize = fs->nlocalvar; /* removes all temp values */ |
| 1089 | optional(ls, ';'); | 1103 | optional(ls, ';'); |
| 1090 | } | 1104 | } |
| 1091 | } | 1105 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lparser.h,v 1.11 2000/03/09 13:57:37 roberto Exp roberto $ | 2 | ** $Id: lparser.h,v 1.12 2000/03/10 18:37:44 roberto Exp roberto $ |
| 3 | ** LL(1) Parser and code generator for Lua | 3 | ** LL(1) Parser and code generator for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -65,11 +65,15 @@ typedef struct expdesc { | |||
| 65 | } expdesc; | 65 | } expdesc; |
| 66 | 66 | ||
| 67 | 67 | ||
| 68 | #define NO_JUMP (-1) /* marks end of patch list */ | ||
| 69 | |||
| 68 | 70 | ||
| 69 | /* state needed to generate code for a given function */ | 71 | /* state needed to generate code for a given function */ |
| 70 | typedef struct FuncState { | 72 | typedef struct FuncState { |
| 71 | Proto *f; /* current function header */ | 73 | Proto *f; /* current function header */ |
| 72 | struct FuncState *prev; /* enclosing function */ | 74 | struct FuncState *prev; /* enclosing function */ |
| 75 | struct LexState *ls; /* lexical state */ | ||
| 76 | struct lua_State *L; /* copy of the Lua state */ | ||
| 73 | int pc; /* next position to code */ | 77 | int pc; /* next position to code */ |
| 74 | int lasttarget; /* `pc' of last `jump target' */ | 78 | int lasttarget; /* `pc' of last `jump target' */ |
| 75 | int stacksize; /* number of values on activation register */ | 79 | int stacksize; /* number of values on activation register */ |
