1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/*
** $Id: lcode.c,v 1.4 2000/03/03 18:53:17 roberto Exp roberto $
** Code generator for Lua
** See Copyright Notice in lua.h
*/
#include "lcode.h"
#include "ldo.h"
#include "llex.h"
#include "lmem.h"
#include "lobject.h"
#include "lopcodes.h"
#include "lparser.h"
#include "lstring.h"
void luaK_error (LexState *ls, const char *msg) {
luaX_error(ls, msg, ls->token);
}
static Instruction *last_i (LexState *ls, expdesc *v) {
FuncState *fs = ls->fs;
int last_pc = (v->info != NOJUMPS) ? v->info : fs->pc-1;
return &fs->f->code[last_pc];
}
int luaK_primitivecode (LexState *ls, Instruction i) {
FuncState *fs = ls->fs;
luaM_growvector(ls->L, fs->f->code, fs->pc, 1, Instruction, codeEM, MAXARG_S);
fs->f->code[fs->pc] = i;
return fs->pc++;
}
static void luaK_minus (LexState *ls, expdesc *v) {
Instruction *last = last_i(ls, v);
switch(GET_OPCODE(*last)) {
case PUSHINT: *last = SETARG_S(*last, -GETARG_S(*last)); return;
case PUSHNUM: *last = SET_OPCODE(*last, PUSHNEGNUM); return;
case PUSHNEGNUM: *last = SET_OPCODE(*last, PUSHNUM); return;
default: luaK_primitivecode(ls, CREATE_0(MINUSOP));
}
}
static void luaK_gettable (LexState *ls, expdesc *v) {
Instruction *last = last_i(ls, v);
luaK_deltastack(ls, -1);
switch(GET_OPCODE(*last)) {
case PUSHSTRING: *last = SET_OPCODE(*last, GETDOTTED); break;
default: luaK_primitivecode(ls, CREATE_0(GETTABLE));
}
}
static void luaK_add (LexState *ls, expdesc *v) {
Instruction *last = last_i(ls, v);
luaK_deltastack(ls, -1);
switch(GET_OPCODE(*last)) {
case PUSHINT: *last = SET_OPCODE(*last, ADDI); break;
default: luaK_primitivecode(ls, CREATE_0(ADDOP));
}
}
static void luaK_sub (LexState *ls, expdesc *v) {
Instruction *last = last_i(ls, v);
luaK_deltastack(ls, -1);
switch(GET_OPCODE(*last)) {
case PUSHINT:
*last = SET_OPCODE(*last, ADDI);
*last = SETARG_S(*last, -GETARG_S(*last));
break;
default: luaK_primitivecode(ls, CREATE_0(SUBOP));
}
}
void luaK_retcode (LexState *ls, int nlocals, listdesc *e) {
if (e->n > 0 && luaK_iscall(ls, e->info)) {
Instruction *last = &ls->fs->f->code[ls->fs->pc-1];
*last = SET_OPCODE(*last, TAILCALL);
*last = SETARG_B(*last, nlocals);
}
else
luaK_primitivecode(ls, CREATE_U(RETCODE, nlocals));
}
int luaK_code (LexState *ls, Instruction i, int delta) {
luaK_deltastack(ls, delta);
return luaK_primitivecode(ls, i);
}
void luaK_fixjump (LexState *ls, int pc, int dest) {
FuncState *fs = ls->fs;
Instruction *jmp = &fs->f->code[pc];
/* jump is relative to position following jump instruction */
*jmp = SETARG_S(*jmp, dest-(pc+1));
}
void luaK_deltastack (LexState *ls, int delta) {
FuncState *fs = ls->fs;
fs->stacksize += delta;
if (delta > 0 && fs->stacksize > fs->f->maxstacksize) {
if (fs->stacksize > MAXSTACK)
luaK_error(ls, "function or expression too complex");
fs->f->maxstacksize = fs->stacksize;
}
}
void luaK_kstr (LexState *ls, int c) {
luaK_U(ls, PUSHSTRING, c, 1);
}
#ifndef LOOKBACKNUMS
#define LOOKBACKNUMS 20 /* arbitrary limit */
#endif
static int real_constant (LexState *ls, real r) {
/* check whether `r' has appeared within the last LOOKBACKNUMS entries */
TProtoFunc *f = ls->fs->f;
int c = f->nknum;
int lim = c < LOOKBACKNUMS ? 0 : c-LOOKBACKNUMS;
while (--c >= lim)
if (f->knum[c] == r) return c;
/* not found; create a new entry */
luaM_growvector(ls->L, f->knum, f->nknum, 1, real, constantEM, MAXARG_U);
c = f->nknum++;
f->knum[c] = r;
return c;
}
void luaK_number (LexState *ls, real f) {
if (f <= (real)MAXARG_S && (int)f == f)
luaK_S(ls, PUSHINT, (int)f, 1); /* f has a short integer value */
else
luaK_U(ls, PUSHNUM, real_constant(ls, f), 1);
}
void luaK_adjuststack (LexState *ls, int n) {
if (n > 0)
luaK_U(ls, POP, n, -n);
else if (n < 0)
luaK_U(ls, PUSHNIL, (-n)-1, -n);
}
int luaK_iscall (LexState *ls, int hasjumps) {
if (hasjumps) return 0; /* a call cannot have internal jumps */
else /* check whether last instruction is a function call */
return (GET_OPCODE(ls->fs->f->code[ls->fs->pc-1]) == CALL);
}
void luaK_setcallreturns (LexState *ls, int hasjumps, int nresults) {
if (!hasjumps) { /* if `hasjumps' cannot be a function call */
Instruction *i = &ls->fs->f->code[ls->fs->pc-1];
if (GET_OPCODE(*i) == CALL) { /* expression is a function call? */
int old_nresults = GETARG_B(*i);
if (old_nresults != MULT_RET)
luaK_deltastack(ls, -old_nresults); /* pop old nresults */
*i = SETARG_B(*i, nresults); /* set nresults */
if (nresults != MULT_RET)
luaK_deltastack(ls, nresults); /* push results */
}
}
}
static void assertglobal (LexState *ls, int index) {
luaS_assertglobal(ls->L, ls->fs->f->kstr[index]);
}
void luaK_2stack (LexState *ls, expdesc *var) {
switch (var->k) {
case VLOCAL:
luaK_U(ls, PUSHLOCAL, var->info, 1);
break;
case VGLOBAL:
luaK_U(ls, GETGLOBAL, var->info, 1);
assertglobal(ls, var->info); /* make sure that there is a global */
break;
case VINDEXED:
luaK_gettable(ls, var);
break;
case VEXP:
luaK_setcallreturns(ls, var->info, 1); /* call must return 1 value */
return; /* does not change var->info */
}
var->k = VEXP;
var->info = NOJUMPS;
}
void luaK_storevar (LexState *ls, const expdesc *var) {
switch (var->k) {
case VLOCAL:
luaK_U(ls, SETLOCAL, var->info, -1);
break;
case VGLOBAL:
luaK_U(ls, SETGLOBAL, var->info, -1);
assertglobal(ls, var->info); /* make sure that there is a global */
break;
case VINDEXED:
luaK_0(ls, SETTABLEPOP, -3);
break;
default:
LUA_INTERNALERROR(ls->L, "invalid var kind to store");
}
}
void luaK_prefix (LexState *ls, int op, expdesc *v) {
luaK_2stack(ls, v);
if (op == '-') luaK_minus(ls, v);
else luaK_0(ls, NOTOP, 0);
v->info = NOJUMPS;
}
void luaK_infix (LexState *ls, expdesc *v) {
luaK_2stack(ls, v);
if (ls->token == AND)
v->info = luaK_0(ls, ONFJMP, -1);
else if (ls->token == OR)
v->info = luaK_0(ls, ONTJMP, -1);
}
void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) {
luaK_2stack(ls, v2);
switch (op) {
case AND: case OR:
luaK_fixjump(ls, v1->info, ls->fs->pc);
return; /* keep v1->info != NOJUMPS */
case '+': luaK_add(ls, v2); break;
case '-': luaK_sub(ls, v2); break;
case '*': luaK_0(ls, MULTOP, -1); break;
case '/': luaK_0(ls, DIVOP, -1); break;
case '^': luaK_0(ls, POWOP, -1); break;
case CONC: luaK_0(ls, CONCOP, -1); break;
case EQ: luaK_0(ls, EQOP, -1); break;
case NE: luaK_0(ls, NEQOP, -1); break;
case '>': luaK_0(ls, GTOP, -1); break;
case '<': luaK_0(ls, LTOP, -1); break;
case GE: luaK_0(ls, GEOP, -1); break;
case LE: luaK_0(ls, LEOP, -1); break;
}
v1->info = NOJUMPS;
}
|