summaryrefslogtreecommitdiff
path: root/lcode.c
blob: d507e179d7601af65714668c7c8bace1de58d964 (plain)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
** $Id: lcode.c,v 1.17 2000/03/24 12:18:30 roberto Exp roberto $
** Code generator for Lua
** See Copyright Notice in lua.h
*/


#define LUA_REENTRANT

#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);
}


int luaK_code (FuncState *fs, Instruction i, int delta) {
  luaK_deltastack(fs, delta);
  luaM_growvector(fs->L, fs->f->code, fs->pc, 1, Instruction, codeEM, MAX_INT);
  fs->f->code[fs->pc] = i;
  return fs->pc++;
}

int luaK_0(FuncState *fs, OpCode o, int d) {
  return luaK_code(fs, CREATE_0(o), d);
}

int luaK_U(FuncState *fs, OpCode o, int u, int d) {
  return luaK_code(fs, CREATE_U(o,u), d);
}

int luaK_S(FuncState *fs, OpCode o, int s, int d) {
  return luaK_code(fs, CREATE_S(o,s), d);
}

int luaK_AB(FuncState *fs, OpCode o, int a, int b, int d) {
  return luaK_code(fs, CREATE_AB(o,a,b), d);
}


/*
** Returns the the previous instruction, for optimizations. 
** If there is a jump target between this and the current instruction,
** returns a dummy instruction to avoid wrong optimizations.
*/
static Instruction previous_instruction (FuncState *fs) {
  if (fs->pc > fs->lasttarget)  /* no jumps to current position? */
    return fs->f->code[fs->pc-1];  /* returns previous instruction */
  else
    return CREATE_0(OP_END);  /* no optimizations after an `END' */
}


static Instruction prepare (FuncState *fs, Instruction i, int delta) {
  Instruction previous = previous_instruction(fs);
  luaK_code(fs, i, delta);
  return previous;
}


static void setprevious (FuncState *fs, Instruction i) {
  fs->pc--;  /* remove last instruction */
  fs->f->code[fs->pc-1] = i;  /* change previous instruction */
}


static void luaK_minus (FuncState *fs) {
  Instruction previous = prepare(fs, CREATE_0(OP_MINUS), 0);
  switch(GET_OPCODE(previous)) {
    case OP_PUSHINT: SETARG_S(previous, -GETARG_S(previous)); break;
    case OP_PUSHNUM: SET_OPCODE(previous, OP_PUSHNEGNUM); break;
    case OP_PUSHNEGNUM: SET_OPCODE(previous, OP_PUSHNUM); break;
    default: return;
  }
  setprevious(fs, previous);
}


static void luaK_gettable (FuncState *fs) {
  Instruction previous = prepare(fs, CREATE_0(OP_GETTABLE), -1);
  switch(GET_OPCODE(previous)) {
    case OP_PUSHSTRING: SET_OPCODE(previous, OP_GETDOTTED); break;
    default: return;
  }
  setprevious(fs, previous);
}


static void luaK_add (FuncState *fs) {
  Instruction previous = prepare(fs, CREATE_0(OP_ADD), -1);
  switch(GET_OPCODE(previous)) {
    case OP_PUSHINT: SET_OPCODE(previous, OP_ADDI); break;
    default: return;
  }
  setprevious(fs, previous);
}


static void luaK_sub (FuncState *fs) {
  Instruction previous = prepare(fs, CREATE_0(OP_SUB), -1);
  switch(GET_OPCODE(previous)) {
    case OP_PUSHINT:
      SET_OPCODE(previous, OP_ADDI);
      SETARG_S(previous, -GETARG_S(previous));
      break;
    default: return;
  }
  setprevious(fs, previous);
}


static void luaK_conc (FuncState *fs) {
  Instruction previous = prepare(fs, CREATE_U(OP_CONC, 2), -1);
  switch(GET_OPCODE(previous)) {
    case OP_CONC: SETARG_U(previous, GETARG_U(previous)+1); break;
    default: return;
  }
  setprevious(fs, previous);
}


static void luaK_eq (FuncState *fs) {
  Instruction previous = prepare(fs, CREATE_S(OP_IFEQJMP, 0), -2);
  if (previous == CREATE_U(OP_PUSHNIL, 1)) {
    setprevious(fs, CREATE_0(OP_NOT));
    luaK_deltastack(fs, 1);  /* undo delta from `prepare' */
  }
}


static void luaK_neq (FuncState *fs) {
  Instruction previous = prepare(fs, CREATE_S(OP_IFNEQJMP, 0), -2);
  if (previous == CREATE_U(OP_PUSHNIL, 1)) {
    setprevious(fs, CREATE_S(OP_IFTJMP, 0));
  }
}


void luaK_retcode (FuncState *fs, int nlocals, int nexps) {
  Instruction previous = prepare(fs, CREATE_U(OP_RETURN, nlocals), 0);
  if (nexps > 0 && GET_OPCODE(previous) == OP_CALL) {
    LUA_ASSERT(fs->L, GETARG_B(previous) == MULT_RET, "call should be open");
    SET_OPCODE(previous, OP_TAILCALL);
    SETARG_B(previous, nlocals);
    setprevious(fs, previous);
  }
}


static void luaK_pushnil (FuncState *fs, int n) {
  Instruction previous = prepare(fs, CREATE_U(OP_PUSHNIL, n), n);
  switch(GET_OPCODE(previous)) {
    case OP_PUSHNIL: SETARG_U(previous, GETARG_U(previous)+n); break;
    default: return;
  }
  setprevious(fs, previous);
}


void luaK_fixjump (FuncState *fs, int pc, int dest) {
  Instruction *jmp = &fs->f->code[pc];
  if (dest == NO_JUMP)
    SETARG_S(*jmp, 0);  /* absolute value to represent end of list */
  else {  /* jump is relative to position following jump instruction */
    int offset = dest-(pc+1);
    if (offset < -MAXARG_S || offset > MAXARG_S)
      luaK_error(fs->ls, "control structure too long");
    SETARG_S(*jmp, offset);
  }
}


static int luaK_getjump (FuncState *fs, int pc) {
  int offset = GETARG_S(fs->f->code[pc]);
  if (offset == 0)
    return NO_JUMP;  /* end of list */
  else
    return (pc+1)+offset;  /* turn offset into absolute position */
}


/*
** returns current `pc' and marks it as a jump target (to avoid wrong
** optimizations with consecutive instructions not in the same basic block).
*/
int luaK_getlabel (FuncState *fs) {
  fs->lasttarget = fs->pc;
  return fs->pc;
}


void luaK_deltastack (FuncState *fs, int delta) {
  fs->stacksize += delta;
  if (delta > 0 && fs->stacksize > fs->f->maxstacksize) {
    if (fs->stacksize > MAXSTACK)
      luaK_error(fs->ls, "function or expression too complex");
    fs->f->maxstacksize = fs->stacksize;
  }
}


void luaK_kstr (LexState *ls, int c) {
  luaK_U(ls->fs, OP_PUSHSTRING, c, 1);
}


static int real_constant (FuncState *fs, Number r) {
  /* check whether `r' has appeared within the last LOOKBACKNUMS entries */
  Proto *f = 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(fs->L, f->knum, f->nknum, 1, Number, constantEM, MAXARG_U);
  c = f->nknum++;
  f->knum[c] = r;
  return c;
}


void luaK_number (FuncState *fs, Number f) {
  if (f <= (Number)MAXARG_S && (int)f == f)
    luaK_S(fs, OP_PUSHINT, (int)f, 1);  /* f has a short integer value */
  else
    luaK_U(fs, OP_PUSHNUM, real_constant(fs, f), 1);
}


void luaK_adjuststack (FuncState *fs, int n) {
  if (n > 0)
    luaK_U(fs, OP_POP, n, -n);
  else if (n < 0)
    luaK_pushnil(fs, -n);
}


int luaK_lastisopen (FuncState *fs) {
  /* check whether last instruction is an open function call */
  Instruction i = previous_instruction(fs);
  if (GET_OPCODE(i) == OP_CALL && GETARG_B(i) == MULT_RET)
    return 1;
  else return 0;
}


void luaK_setcallreturns (FuncState *fs, int nresults) {
  if (luaK_lastisopen(fs)) {  /* expression is an open function call? */
    SETARG_B(fs->f->code[fs->pc-1], nresults);  /* set number of results */
    luaK_deltastack(fs, nresults);  /* push results */
  }
}


static void assertglobal (FuncState *fs, int index) {
  luaS_assertglobal(fs->L, fs->f->kstr[index]);
}


static int discharge (FuncState *fs, expdesc *var) {
  switch (var->k) {
    case VLOCAL:
      luaK_U(fs, OP_PUSHLOCAL, var->u.index, 1);
      break;
    case VGLOBAL:
      luaK_U(fs, OP_GETGLOBAL, var->u.index, 1);
      assertglobal(fs, var->u.index);  /* make sure that there is a global */
      break;
    case VINDEXED:
      luaK_gettable(fs);
      break;
    case VEXP:
      return 0;  /* nothing to do */
  }
  var->k = VEXP;
  var->u.l.t = var->u.l.f = NO_JUMP;
  return 1;
}


static void discharge1 (FuncState *fs, expdesc *var) {
  discharge(fs, var);
 /* if it has jumps it is already discharged */
  if (var->u.l.t == NO_JUMP && var->u.l.f  == NO_JUMP)
    luaK_setcallreturns(fs, 1);  /* call must return 1 value */
}
  

void luaK_storevar (LexState *ls, const expdesc *var) {
  FuncState *fs = ls->fs;
  switch (var->k) {
    case VLOCAL:
      luaK_U(fs, OP_SETLOCAL, var->u.index, -1);
      break;
    case VGLOBAL:
      luaK_U(fs, OP_SETGLOBAL, var->u.index, -1);
      assertglobal(fs, var->u.index);  /* make sure that there is a global */
      break;
    case VINDEXED:
      luaK_0(fs, OP_SETTABLEPOP, -3);
      break;
    default:
      LUA_INTERNALERROR(ls->L, "invalid var kind to store");
  }
}


static OpCode invertjump (OpCode op) {
  switch (op) {
    case OP_IFNEQJMP: return OP_IFEQJMP;
    case OP_IFEQJMP: return OP_IFNEQJMP;
    case OP_IFLTJMP: return OP_IFGEJMP;
    case OP_IFLEJMP: return OP_IFGTJMP;
    case OP_IFGTJMP: return OP_IFLEJMP;
    case OP_IFGEJMP: return OP_IFLTJMP;
    case OP_IFTJMP: case OP_ONTJMP:  return OP_IFFJMP;
    case OP_IFFJMP: case OP_ONFJMP:  return OP_IFTJMP;
    default:
      LUA_INTERNALERROR(NULL, "invalid jump instruction");
      return OP_END;  /* to avoid warnings */
  }
}


static void luaK_jump (FuncState *fs, OpCode jump) {
  Instruction previous = prepare(fs, CREATE_S(jump, 0), -1);
  switch (GET_OPCODE(previous)) {
    case OP_NOT: previous = CREATE_S(invertjump(jump), 0); break;
    case OP_PUSHINT:
      if (jump == OP_IFTJMP) {
        previous = CREATE_S(OP_JMP, 0);
        break;
      }
      else return;  /* do not set previous */
    default: return;
  }
  setprevious(fs, previous);
}


static void insert_last (FuncState *fs, int *list) {
  int first = *list;
  *list = fs->pc-1;  /* insert last instruction in the list */
  luaK_fixjump(fs, *list, first);
}


static void luaK_patchlistaux (FuncState *fs, int list, int target,
                               OpCode special, int special_target) {
  Instruction *code = fs->f->code;
  while (list != NO_JUMP) {
    int next = luaK_getjump(fs, list);
    Instruction *i = &code[list];
    OpCode op = GET_OPCODE(*i);
    if (op == special)  /* this `op' already has a value */
      luaK_fixjump(fs, list, special_target);
    else {
      luaK_fixjump(fs, list, target);  /* do the patch */
      if (op == OP_ONTJMP)  /* remove eventual values */
        SET_OPCODE(*i, OP_IFTJMP);
      else if (op == OP_ONFJMP)
        SET_OPCODE(*i, OP_IFFJMP);
    }
    list = next;
  }
}


void luaK_patchlist (FuncState *fs, int list, int target) {
  luaK_patchlistaux(fs, list, target, OP_END, 0);
}


static int need_value (FuncState *fs, int list, OpCode hasvalue) {
  /* check whether list has a jump without a value */
  for (; list != NO_JUMP; list = luaK_getjump(fs, list))
    if (GET_OPCODE(fs->f->code[list]) != hasvalue) return 1;
  return 0;  /* not found */
}


static void concatlists (FuncState *fs, int *l1, int l2) {
  if (*l1 == NO_JUMP)
    *l1 = l2;
  else {
    int list = *l1;
    for (;;) {  /* traverse `l1' */
      int next = luaK_getjump(fs, list);
      if (next == NO_JUMP) {  /* end of list? */
        luaK_fixjump(fs, list, l2);
        return;
      }
      list = next;
    }
  }
}


static void luaK_testgo (FuncState *fs, expdesc *v, int invert, OpCode jump) {
  Instruction *previous;
  int *golist = &v->u.l.f;
  int *exitlist = &v->u.l.t;
  if (invert) {  /* interchange `golist' and `exitlist' */
    int *temp = golist; golist = exitlist; exitlist = temp;
  }
  discharge1(fs, v);
  previous = &fs->f->code[fs->pc-1];
  LUA_ASSERT(L, GET_OPCODE(*previous) != OP_SETLINE, "bad place to set line");
  if (ISJUMP(GET_OPCODE(*previous))) {
    if (invert)
      SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
  }
  else
    luaK_jump(fs, jump);
  insert_last(fs, exitlist);
  luaK_patchlist(fs, *golist, luaK_getlabel(fs));
  *golist = NO_JUMP;
}


void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue) {
  luaK_testgo(fs, v, 1, keepvalue ? OP_ONFJMP : OP_IFFJMP);
}


void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue) {
  luaK_testgo(fs, v, 0, keepvalue ? OP_ONTJMP : OP_IFTJMP);
}


void luaK_tostack (LexState *ls, expdesc *v, int onlyone) {
  FuncState *fs = ls->fs;
  if (!discharge(fs, v)) {  /* `v' is an expression? */
    OpCode previous = GET_OPCODE(fs->f->code[fs->pc-1]);
    LUA_ASSERT(L, previous != OP_SETLINE, "bad place to set line");
    if (!ISJUMP(previous) && v->u.l.f == NO_JUMP && v->u.l.t == NO_JUMP) {
      /* it is an expression without jumps */
      if (onlyone)
        luaK_setcallreturns(fs, 1);  /* call must return 1 value */
    }
    else {  /* expression has jumps... */
      int p_nil = 0;  /* position of an eventual PUSHNIL */
      int p_1 = 0;  /* position of an eventual PUSHINT */
      int final;  /* position after whole expression */
      if (ISJUMP(previous)) {
        insert_last(fs, &v->u.l.t);  /* put `previous' in true list */
        p_nil = luaK_0(fs, OP_PUSHNILJMP, 0);
        p_1 = luaK_S(fs, OP_PUSHINT, 1, 1);
      }
      else {  /* still may need a PUSHNIL or a PUSHINT */
        int need_nil = need_value(fs, v->u.l.f, OP_ONFJMP);
        int need_1 = need_value(fs, v->u.l.t, OP_ONTJMP);
        if (need_nil && need_1) {
          luaK_S(fs, OP_JMP, 2, 0);  /* skip both pushes */
          p_nil = luaK_0(fs, OP_PUSHNILJMP, 0);
          p_1 = luaK_S(fs, OP_PUSHINT, 1, 0);
        }
        else if (need_nil || need_1) {
          luaK_S(fs, OP_JMP, 1, 0);  /* skip one push */
          if (need_nil)
            p_nil = luaK_U(fs, OP_PUSHNIL, 1, 0);
          else  /* need_1 */
            p_1 = luaK_S(fs, OP_PUSHINT, 1, 0);
        }
      }
      final = luaK_getlabel(fs);
      luaK_patchlistaux(fs, v->u.l.f, p_nil, OP_ONFJMP, final);
      luaK_patchlistaux(fs, v->u.l.t, p_1, OP_ONTJMP, final);
      v->u.l.f = v->u.l.t = NO_JUMP;
    }
  }
}


void luaK_prefix (LexState *ls, int op, expdesc *v) {
  FuncState *fs = ls->fs;
  if (op == '-') {
    luaK_tostack(ls, v, 1);
    luaK_minus(fs);
  }
  else {  /* op == NOT */
    Instruction *previous;
    discharge1(fs, v);
    previous = &fs->f->code[fs->pc-1];
    if (ISJUMP(GET_OPCODE(*previous)))
      SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
    else
      luaK_0(fs, OP_NOT, 0);
    /* interchange true and false lists */
    { int temp = v->u.l.f; v->u.l.f = v->u.l.t; v->u.l.t = temp; }
  }
}


void luaK_infix (LexState *ls, int op, expdesc *v) {
  FuncState *fs = ls->fs;
  if (op == TK_AND)
    luaK_goiftrue(fs, v, 1);
  else if (op == TK_OR)
    luaK_goiffalse(fs, v, 1);
  else
    luaK_tostack(ls, v, 1);  /* all other binary operators need a value */
} 


void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) {
  FuncState *fs = ls->fs;
  if (op == TK_AND) {
    LUA_ASSERT(ls->L, v1->u.l.t == NO_JUMP, "list must be closed");
    discharge1(fs, v2);
    v1->u.l.t = v2->u.l.t;
    concatlists(fs, &v1->u.l.f, v2->u.l.f);
  }
  else if (op == TK_OR) {
    LUA_ASSERT(ls->L, v1->u.l.f == NO_JUMP, "list must be closed");
    discharge1(fs, v2);
    v1->u.l.f = v2->u.l.f;
    concatlists(fs, &v1->u.l.t, v2->u.l.t);
  }
  else {
    luaK_tostack(ls, v2, 1);  /* `v2' must be a value */
    switch (op) {
      case '+': luaK_add(fs); break;
      case '-': luaK_sub(fs); break;
      case '*': luaK_0(fs, OP_MULT, -1); break;
      case '/': luaK_0(fs, OP_DIV, -1); break;
      case '^': luaK_0(fs, OP_POW, -1); break;
      case TK_CONC: luaK_conc(fs); break;
      case TK_EQ: luaK_eq(fs); break;
      case TK_NE: luaK_neq(fs); break;
      case '>': luaK_S(fs, OP_IFGTJMP, 0, -2); break;
      case '<': luaK_S(fs, OP_IFLTJMP, 0, -2); break;
      case TK_GE: luaK_S(fs, OP_IFGEJMP, 0, -2); break;
      case TK_LE: luaK_S(fs, OP_IFLEJMP, 0, -2); break;
    }
  }
}