annotate lib/lua/lua-5.2.2/lcode.c @ 2553:48708da03b7f

Party Creation UI logic separated from MainMenu
author a.parshin
date Wed, 13 May 2015 02:20:05 +0200
parents 41cc4dd3c122
children
rev   line source
1866
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
2 ** $Id: lcode.c,v 2.62 2012/08/16 17:34:28 roberto Exp $
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
3 ** Code generator for Lua
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
4 ** See Copyright Notice in lua.h
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
5 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
6
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
7
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
8 #include <stdlib.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
9
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
10 #define lcode_c
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
11 #define LUA_CORE
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
12
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
13 #include "lua.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
14
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
15 #include "lcode.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
16 #include "ldebug.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
17 #include "ldo.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
18 #include "lgc.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
19 #include "llex.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
20 #include "lmem.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
21 #include "lobject.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
22 #include "lopcodes.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
23 #include "lparser.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
24 #include "lstring.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
25 #include "ltable.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
26 #include "lvm.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
27
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
28
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
29 #define hasjumps(e) ((e)->t != (e)->f)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
30
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
31
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
32 static int isnumeral(expdesc *e) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
33 return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
34 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
35
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
36
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
37 void luaK_nil (FuncState *fs, int from, int n) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
38 Instruction *previous;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
39 int l = from + n - 1; /* last register to set nil */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
40 if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
41 previous = &fs->f->code[fs->pc-1];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
42 if (GET_OPCODE(*previous) == OP_LOADNIL) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
43 int pfrom = GETARG_A(*previous);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
44 int pl = pfrom + GETARG_B(*previous);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
45 if ((pfrom <= from && from <= pl + 1) ||
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
46 (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
47 if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
48 if (pl > l) l = pl; /* l = max(l, pl) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
49 SETARG_A(*previous, from);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
50 SETARG_B(*previous, l - from);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
51 return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
52 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
53 } /* else go through */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
54 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
55 luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
56 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
57
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
58
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
59 int luaK_jump (FuncState *fs) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
60 int jpc = fs->jpc; /* save list of jumps to here */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
61 int j;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
62 fs->jpc = NO_JUMP;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
63 j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
64 luaK_concat(fs, &j, jpc); /* keep them on hold */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
65 return j;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
66 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
67
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
68
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
69 void luaK_ret (FuncState *fs, int first, int nret) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
70 luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
71 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
72
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
73
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
74 static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
75 luaK_codeABC(fs, op, A, B, C);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
76 return luaK_jump(fs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
77 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
78
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
79
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
80 static void fixjump (FuncState *fs, int pc, int dest) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
81 Instruction *jmp = &fs->f->code[pc];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
82 int offset = dest-(pc+1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
83 lua_assert(dest != NO_JUMP);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
84 if (abs(offset) > MAXARG_sBx)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
85 luaX_syntaxerror(fs->ls, "control structure too long");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
86 SETARG_sBx(*jmp, offset);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
87 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
88
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
89
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
90 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
91 ** returns current `pc' and marks it as a jump target (to avoid wrong
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
92 ** optimizations with consecutive instructions not in the same basic block).
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
93 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
94 int luaK_getlabel (FuncState *fs) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
95 fs->lasttarget = fs->pc;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
96 return fs->pc;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
97 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
98
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
99
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
100 static int getjump (FuncState *fs, int pc) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
101 int offset = GETARG_sBx(fs->f->code[pc]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
102 if (offset == NO_JUMP) /* point to itself represents end of list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
103 return NO_JUMP; /* end of list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
104 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
105 return (pc+1)+offset; /* turn offset into absolute position */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
106 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
107
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
108
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
109 static Instruction *getjumpcontrol (FuncState *fs, int pc) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
110 Instruction *pi = &fs->f->code[pc];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
111 if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
112 return pi-1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
113 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
114 return pi;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
115 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
116
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
117
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
118 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
119 ** check whether list has any jump that do not produce a value
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
120 ** (or produce an inverted value)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
121 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
122 static int need_value (FuncState *fs, int list) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
123 for (; list != NO_JUMP; list = getjump(fs, list)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
124 Instruction i = *getjumpcontrol(fs, list);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
125 if (GET_OPCODE(i) != OP_TESTSET) return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
126 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
127 return 0; /* not found */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
128 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
129
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
130
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
131 static int patchtestreg (FuncState *fs, int node, int reg) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
132 Instruction *i = getjumpcontrol(fs, node);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
133 if (GET_OPCODE(*i) != OP_TESTSET)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
134 return 0; /* cannot patch other instructions */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
135 if (reg != NO_REG && reg != GETARG_B(*i))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
136 SETARG_A(*i, reg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
137 else /* no register to put value or register already has the value */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
138 *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
139
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
140 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
141 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
142
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
143
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
144 static void removevalues (FuncState *fs, int list) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
145 for (; list != NO_JUMP; list = getjump(fs, list))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
146 patchtestreg(fs, list, NO_REG);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
147 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
148
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
149
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
150 static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
151 int dtarget) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
152 while (list != NO_JUMP) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
153 int next = getjump(fs, list);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
154 if (patchtestreg(fs, list, reg))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
155 fixjump(fs, list, vtarget);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
156 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
157 fixjump(fs, list, dtarget); /* jump to default target */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
158 list = next;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
159 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
160 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
161
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
162
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
163 static void dischargejpc (FuncState *fs) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
164 patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
165 fs->jpc = NO_JUMP;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
166 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
167
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
168
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
169 void luaK_patchlist (FuncState *fs, int list, int target) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
170 if (target == fs->pc)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
171 luaK_patchtohere(fs, list);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
172 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
173 lua_assert(target < fs->pc);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
174 patchlistaux(fs, list, target, NO_REG, target);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
175 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
176 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
177
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
178
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
179 LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
180 level++; /* argument is +1 to reserve 0 as non-op */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
181 while (list != NO_JUMP) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
182 int next = getjump(fs, list);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
183 lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP &&
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
184 (GETARG_A(fs->f->code[list]) == 0 ||
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
185 GETARG_A(fs->f->code[list]) >= level));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
186 SETARG_A(fs->f->code[list], level);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
187 list = next;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
188 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
189 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
190
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
191
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
192 void luaK_patchtohere (FuncState *fs, int list) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
193 luaK_getlabel(fs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
194 luaK_concat(fs, &fs->jpc, list);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
195 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
196
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
197
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
198 void luaK_concat (FuncState *fs, int *l1, int l2) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
199 if (l2 == NO_JUMP) return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
200 else if (*l1 == NO_JUMP)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
201 *l1 = l2;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
202 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
203 int list = *l1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
204 int next;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
205 while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
206 list = next;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
207 fixjump(fs, list, l2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
208 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
209 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
210
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
211
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
212 static int luaK_code (FuncState *fs, Instruction i) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
213 Proto *f = fs->f;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
214 dischargejpc(fs); /* `pc' will change */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
215 /* put new instruction in code array */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
216 luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
217 MAX_INT, "opcodes");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
218 f->code[fs->pc] = i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
219 /* save corresponding line information */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
220 luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
221 MAX_INT, "opcodes");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
222 f->lineinfo[fs->pc] = fs->ls->lastline;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
223 return fs->pc++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
224 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
225
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
226
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
227 int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
228 lua_assert(getOpMode(o) == iABC);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
229 lua_assert(getBMode(o) != OpArgN || b == 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
230 lua_assert(getCMode(o) != OpArgN || c == 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
231 lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
232 return luaK_code(fs, CREATE_ABC(o, a, b, c));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
233 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
234
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
235
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
236 int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
237 lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
238 lua_assert(getCMode(o) == OpArgN);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
239 lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
240 return luaK_code(fs, CREATE_ABx(o, a, bc));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
241 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
242
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
243
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
244 static int codeextraarg (FuncState *fs, int a) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
245 lua_assert(a <= MAXARG_Ax);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
246 return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
247 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
248
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
249
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
250 int luaK_codek (FuncState *fs, int reg, int k) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
251 if (k <= MAXARG_Bx)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
252 return luaK_codeABx(fs, OP_LOADK, reg, k);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
253 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
254 int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
255 codeextraarg(fs, k);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
256 return p;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
257 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
258 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
259
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
260
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
261 void luaK_checkstack (FuncState *fs, int n) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
262 int newstack = fs->freereg + n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
263 if (newstack > fs->f->maxstacksize) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
264 if (newstack >= MAXSTACK)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
265 luaX_syntaxerror(fs->ls, "function or expression too complex");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
266 fs->f->maxstacksize = cast_byte(newstack);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
267 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
268 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
269
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
270
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
271 void luaK_reserveregs (FuncState *fs, int n) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
272 luaK_checkstack(fs, n);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
273 fs->freereg += n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
274 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
275
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
276
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
277 static void freereg (FuncState *fs, int reg) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
278 if (!ISK(reg) && reg >= fs->nactvar) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
279 fs->freereg--;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
280 lua_assert(reg == fs->freereg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
281 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
282 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
283
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
284
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
285 static void freeexp (FuncState *fs, expdesc *e) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
286 if (e->k == VNONRELOC)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
287 freereg(fs, e->u.info);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
288 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
289
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
290
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
291 static int addk (FuncState *fs, TValue *key, TValue *v) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
292 lua_State *L = fs->ls->L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
293 TValue *idx = luaH_set(L, fs->h, key);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
294 Proto *f = fs->f;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
295 int k, oldsize;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
296 if (ttisnumber(idx)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
297 lua_Number n = nvalue(idx);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
298 lua_number2int(k, n);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
299 if (luaV_rawequalobj(&f->k[k], v))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
300 return k;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
301 /* else may be a collision (e.g., between 0.0 and "\0\0\0\0\0\0\0\0");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
302 go through and create a new entry for this value */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
303 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
304 /* constant not found; create a new entry */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
305 oldsize = f->sizek;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
306 k = fs->nk;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
307 /* numerical value does not need GC barrier;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
308 table has no metatable, so it does not need to invalidate cache */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
309 setnvalue(idx, cast_num(k));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
310 luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
311 while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
312 setobj(L, &f->k[k], v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
313 fs->nk++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
314 luaC_barrier(L, f, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
315 return k;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
316 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
317
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
318
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
319 int luaK_stringK (FuncState *fs, TString *s) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
320 TValue o;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
321 setsvalue(fs->ls->L, &o, s);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
322 return addk(fs, &o, &o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
323 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
324
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
325
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
326 int luaK_numberK (FuncState *fs, lua_Number r) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
327 int n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
328 lua_State *L = fs->ls->L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
329 TValue o;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
330 setnvalue(&o, r);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
331 if (r == 0 || luai_numisnan(NULL, r)) { /* handle -0 and NaN */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
332 /* use raw representation as key to avoid numeric problems */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
333 setsvalue(L, L->top++, luaS_newlstr(L, (char *)&r, sizeof(r)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
334 n = addk(fs, L->top - 1, &o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
335 L->top--;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
336 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
337 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
338 n = addk(fs, &o, &o); /* regular case */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
339 return n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
340 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
341
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
342
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
343 static int boolK (FuncState *fs, int b) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
344 TValue o;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
345 setbvalue(&o, b);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
346 return addk(fs, &o, &o);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
347 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
348
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
349
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
350 static int nilK (FuncState *fs) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
351 TValue k, v;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
352 setnilvalue(&v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
353 /* cannot use nil as key; instead use table itself to represent nil */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
354 sethvalue(fs->ls->L, &k, fs->h);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
355 return addk(fs, &k, &v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
356 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
357
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
358
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
359 void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
360 if (e->k == VCALL) { /* expression is an open function call? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
361 SETARG_C(getcode(fs, e), nresults+1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
362 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
363 else if (e->k == VVARARG) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
364 SETARG_B(getcode(fs, e), nresults+1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
365 SETARG_A(getcode(fs, e), fs->freereg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
366 luaK_reserveregs(fs, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
367 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
368 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
369
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
370
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
371 void luaK_setoneret (FuncState *fs, expdesc *e) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
372 if (e->k == VCALL) { /* expression is an open function call? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
373 e->k = VNONRELOC;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
374 e->u.info = GETARG_A(getcode(fs, e));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
375 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
376 else if (e->k == VVARARG) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
377 SETARG_B(getcode(fs, e), 2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
378 e->k = VRELOCABLE; /* can relocate its simple result */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
379 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
380 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
381
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
382
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
383 void luaK_dischargevars (FuncState *fs, expdesc *e) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
384 switch (e->k) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
385 case VLOCAL: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
386 e->k = VNONRELOC;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
387 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
388 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
389 case VUPVAL: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
390 e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
391 e->k = VRELOCABLE;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
392 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
393 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
394 case VINDEXED: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
395 OpCode op = OP_GETTABUP; /* assume 't' is in an upvalue */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
396 freereg(fs, e->u.ind.idx);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
397 if (e->u.ind.vt == VLOCAL) { /* 't' is in a register? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
398 freereg(fs, e->u.ind.t);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
399 op = OP_GETTABLE;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
400 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
401 e->u.info = luaK_codeABC(fs, op, 0, e->u.ind.t, e->u.ind.idx);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
402 e->k = VRELOCABLE;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
403 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
404 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
405 case VVARARG:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
406 case VCALL: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
407 luaK_setoneret(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
408 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
409 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
410 default: break; /* there is one value available (somewhere) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
411 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
412 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
413
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
414
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
415 static int code_label (FuncState *fs, int A, int b, int jump) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
416 luaK_getlabel(fs); /* those instructions may be jump targets */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
417 return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
418 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
419
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
420
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
421 static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
422 luaK_dischargevars(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
423 switch (e->k) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
424 case VNIL: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
425 luaK_nil(fs, reg, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
426 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
427 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
428 case VFALSE: case VTRUE: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
429 luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
430 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
431 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
432 case VK: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
433 luaK_codek(fs, reg, e->u.info);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
434 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
435 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
436 case VKNUM: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
437 luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
438 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
439 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
440 case VRELOCABLE: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
441 Instruction *pc = &getcode(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
442 SETARG_A(*pc, reg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
443 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
444 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
445 case VNONRELOC: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
446 if (reg != e->u.info)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
447 luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
448 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
449 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
450 default: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
451 lua_assert(e->k == VVOID || e->k == VJMP);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
452 return; /* nothing to do... */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
453 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
454 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
455 e->u.info = reg;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
456 e->k = VNONRELOC;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
457 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
458
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
459
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
460 static void discharge2anyreg (FuncState *fs, expdesc *e) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
461 if (e->k != VNONRELOC) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
462 luaK_reserveregs(fs, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
463 discharge2reg(fs, e, fs->freereg-1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
464 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
465 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
466
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
467
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
468 static void exp2reg (FuncState *fs, expdesc *e, int reg) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
469 discharge2reg(fs, e, reg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
470 if (e->k == VJMP)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
471 luaK_concat(fs, &e->t, e->u.info); /* put this jump in `t' list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
472 if (hasjumps(e)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
473 int final; /* position after whole expression */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
474 int p_f = NO_JUMP; /* position of an eventual LOAD false */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
475 int p_t = NO_JUMP; /* position of an eventual LOAD true */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
476 if (need_value(fs, e->t) || need_value(fs, e->f)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
477 int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
478 p_f = code_label(fs, reg, 0, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
479 p_t = code_label(fs, reg, 1, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
480 luaK_patchtohere(fs, fj);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
481 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
482 final = luaK_getlabel(fs);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
483 patchlistaux(fs, e->f, final, reg, p_f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
484 patchlistaux(fs, e->t, final, reg, p_t);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
485 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
486 e->f = e->t = NO_JUMP;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
487 e->u.info = reg;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
488 e->k = VNONRELOC;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
489 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
490
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
491
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
492 void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
493 luaK_dischargevars(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
494 freeexp(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
495 luaK_reserveregs(fs, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
496 exp2reg(fs, e, fs->freereg - 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
497 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
498
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
499
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
500 int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
501 luaK_dischargevars(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
502 if (e->k == VNONRELOC) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
503 if (!hasjumps(e)) return e->u.info; /* exp is already in a register */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
504 if (e->u.info >= fs->nactvar) { /* reg. is not a local? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
505 exp2reg(fs, e, e->u.info); /* put value on it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
506 return e->u.info;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
507 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
508 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
509 luaK_exp2nextreg(fs, e); /* default */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
510 return e->u.info;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
511 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
512
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
513
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
514 void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
515 if (e->k != VUPVAL || hasjumps(e))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
516 luaK_exp2anyreg(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
517 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
518
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
519
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
520 void luaK_exp2val (FuncState *fs, expdesc *e) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
521 if (hasjumps(e))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
522 luaK_exp2anyreg(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
523 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
524 luaK_dischargevars(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
525 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
526
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
527
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
528 int luaK_exp2RK (FuncState *fs, expdesc *e) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
529 luaK_exp2val(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
530 switch (e->k) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
531 case VTRUE:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
532 case VFALSE:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
533 case VNIL: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
534 if (fs->nk <= MAXINDEXRK) { /* constant fits in RK operand? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
535 e->u.info = (e->k == VNIL) ? nilK(fs) : boolK(fs, (e->k == VTRUE));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
536 e->k = VK;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
537 return RKASK(e->u.info);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
538 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
539 else break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
540 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
541 case VKNUM: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
542 e->u.info = luaK_numberK(fs, e->u.nval);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
543 e->k = VK;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
544 /* go through */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
545 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
546 case VK: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
547 if (e->u.info <= MAXINDEXRK) /* constant fits in argC? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
548 return RKASK(e->u.info);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
549 else break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
550 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
551 default: break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
552 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
553 /* not a constant in the right range: put it in a register */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
554 return luaK_exp2anyreg(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
555 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
556
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
557
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
558 void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
559 switch (var->k) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
560 case VLOCAL: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
561 freeexp(fs, ex);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
562 exp2reg(fs, ex, var->u.info);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
563 return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
564 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
565 case VUPVAL: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
566 int e = luaK_exp2anyreg(fs, ex);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
567 luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
568 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
569 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
570 case VINDEXED: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
571 OpCode op = (var->u.ind.vt == VLOCAL) ? OP_SETTABLE : OP_SETTABUP;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
572 int e = luaK_exp2RK(fs, ex);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
573 luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
574 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
575 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
576 default: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
577 lua_assert(0); /* invalid var kind to store */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
578 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
579 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
580 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
581 freeexp(fs, ex);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
582 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
583
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
584
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
585 void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
586 int ereg;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
587 luaK_exp2anyreg(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
588 ereg = e->u.info; /* register where 'e' was placed */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
589 freeexp(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
590 e->u.info = fs->freereg; /* base register for op_self */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
591 e->k = VNONRELOC;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
592 luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
593 luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
594 freeexp(fs, key);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
595 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
596
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
597
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
598 static void invertjump (FuncState *fs, expdesc *e) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
599 Instruction *pc = getjumpcontrol(fs, e->u.info);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
600 lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
601 GET_OPCODE(*pc) != OP_TEST);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
602 SETARG_A(*pc, !(GETARG_A(*pc)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
603 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
604
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
605
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
606 static int jumponcond (FuncState *fs, expdesc *e, int cond) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
607 if (e->k == VRELOCABLE) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
608 Instruction ie = getcode(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
609 if (GET_OPCODE(ie) == OP_NOT) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
610 fs->pc--; /* remove previous OP_NOT */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
611 return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
612 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
613 /* else go through */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
614 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
615 discharge2anyreg(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
616 freeexp(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
617 return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
618 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
619
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
620
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
621 void luaK_goiftrue (FuncState *fs, expdesc *e) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
622 int pc; /* pc of last jump */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
623 luaK_dischargevars(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
624 switch (e->k) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
625 case VJMP: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
626 invertjump(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
627 pc = e->u.info;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
628 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
629 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
630 case VK: case VKNUM: case VTRUE: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
631 pc = NO_JUMP; /* always true; do nothing */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
632 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
633 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
634 default: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
635 pc = jumponcond(fs, e, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
636 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
637 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
638 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
639 luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
640 luaK_patchtohere(fs, e->t);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
641 e->t = NO_JUMP;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
642 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
643
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
644
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
645 void luaK_goiffalse (FuncState *fs, expdesc *e) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
646 int pc; /* pc of last jump */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
647 luaK_dischargevars(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
648 switch (e->k) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
649 case VJMP: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
650 pc = e->u.info;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
651 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
652 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
653 case VNIL: case VFALSE: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
654 pc = NO_JUMP; /* always false; do nothing */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
655 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
656 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
657 default: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
658 pc = jumponcond(fs, e, 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
659 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
660 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
661 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
662 luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
663 luaK_patchtohere(fs, e->f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
664 e->f = NO_JUMP;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
665 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
666
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
667
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
668 static void codenot (FuncState *fs, expdesc *e) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
669 luaK_dischargevars(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
670 switch (e->k) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
671 case VNIL: case VFALSE: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
672 e->k = VTRUE;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
673 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
674 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
675 case VK: case VKNUM: case VTRUE: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
676 e->k = VFALSE;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
677 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
678 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
679 case VJMP: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
680 invertjump(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
681 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
682 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
683 case VRELOCABLE:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
684 case VNONRELOC: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
685 discharge2anyreg(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
686 freeexp(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
687 e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
688 e->k = VRELOCABLE;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
689 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
690 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
691 default: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
692 lua_assert(0); /* cannot happen */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
693 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
694 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
695 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
696 /* interchange true and false lists */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
697 { int temp = e->f; e->f = e->t; e->t = temp; }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
698 removevalues(fs, e->f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
699 removevalues(fs, e->t);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
700 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
701
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
702
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
703 void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
704 lua_assert(!hasjumps(t));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
705 t->u.ind.t = t->u.info;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
706 t->u.ind.idx = luaK_exp2RK(fs, k);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
707 t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
708 : check_exp(vkisinreg(t->k), VLOCAL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
709 t->k = VINDEXED;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
710 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
711
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
712
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
713 static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
714 lua_Number r;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
715 if (!isnumeral(e1) || !isnumeral(e2)) return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
716 if ((op == OP_DIV || op == OP_MOD) && e2->u.nval == 0)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
717 return 0; /* do not attempt to divide by 0 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
718 r = luaO_arith(op - OP_ADD + LUA_OPADD, e1->u.nval, e2->u.nval);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
719 e1->u.nval = r;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
720 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
721 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
722
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
723
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
724 static void codearith (FuncState *fs, OpCode op,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
725 expdesc *e1, expdesc *e2, int line) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
726 if (constfolding(op, e1, e2))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
727 return;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
728 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
729 int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
730 int o1 = luaK_exp2RK(fs, e1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
731 if (o1 > o2) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
732 freeexp(fs, e1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
733 freeexp(fs, e2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
734 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
735 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
736 freeexp(fs, e2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
737 freeexp(fs, e1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
738 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
739 e1->u.info = luaK_codeABC(fs, op, 0, o1, o2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
740 e1->k = VRELOCABLE;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
741 luaK_fixline(fs, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
742 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
743 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
744
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
745
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
746 static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
747 expdesc *e2) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
748 int o1 = luaK_exp2RK(fs, e1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
749 int o2 = luaK_exp2RK(fs, e2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
750 freeexp(fs, e2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
751 freeexp(fs, e1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
752 if (cond == 0 && op != OP_EQ) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
753 int temp; /* exchange args to replace by `<' or `<=' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
754 temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
755 cond = 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
756 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
757 e1->u.info = condjump(fs, op, cond, o1, o2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
758 e1->k = VJMP;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
759 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
760
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
761
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
762 void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
763 expdesc e2;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
764 e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
765 switch (op) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
766 case OPR_MINUS: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
767 if (isnumeral(e)) /* minus constant? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
768 e->u.nval = luai_numunm(NULL, e->u.nval); /* fold it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
769 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
770 luaK_exp2anyreg(fs, e);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
771 codearith(fs, OP_UNM, e, &e2, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
772 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
773 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
774 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
775 case OPR_NOT: codenot(fs, e); break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
776 case OPR_LEN: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
777 luaK_exp2anyreg(fs, e); /* cannot operate on constants */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
778 codearith(fs, OP_LEN, e, &e2, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
779 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
780 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
781 default: lua_assert(0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
782 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
783 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
784
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
785
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
786 void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
787 switch (op) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
788 case OPR_AND: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
789 luaK_goiftrue(fs, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
790 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
791 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
792 case OPR_OR: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
793 luaK_goiffalse(fs, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
794 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
795 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
796 case OPR_CONCAT: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
797 luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
798 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
799 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
800 case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
801 case OPR_MOD: case OPR_POW: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
802 if (!isnumeral(v)) luaK_exp2RK(fs, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
803 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
804 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
805 default: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
806 luaK_exp2RK(fs, v);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
807 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
808 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
809 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
810 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
811
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
812
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
813 void luaK_posfix (FuncState *fs, BinOpr op,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
814 expdesc *e1, expdesc *e2, int line) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
815 switch (op) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
816 case OPR_AND: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
817 lua_assert(e1->t == NO_JUMP); /* list must be closed */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
818 luaK_dischargevars(fs, e2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
819 luaK_concat(fs, &e2->f, e1->f);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
820 *e1 = *e2;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
821 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
822 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
823 case OPR_OR: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
824 lua_assert(e1->f == NO_JUMP); /* list must be closed */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
825 luaK_dischargevars(fs, e2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
826 luaK_concat(fs, &e2->t, e1->t);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
827 *e1 = *e2;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
828 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
829 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
830 case OPR_CONCAT: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
831 luaK_exp2val(fs, e2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
832 if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
833 lua_assert(e1->u.info == GETARG_B(getcode(fs, e2))-1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
834 freeexp(fs, e1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
835 SETARG_B(getcode(fs, e2), e1->u.info);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
836 e1->k = VRELOCABLE; e1->u.info = e2->u.info;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
837 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
838 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
839 luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
840 codearith(fs, OP_CONCAT, e1, e2, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
841 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
842 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
843 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
844 case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
845 case OPR_MOD: case OPR_POW: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
846 codearith(fs, cast(OpCode, op - OPR_ADD + OP_ADD), e1, e2, line);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
847 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
848 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
849 case OPR_EQ: case OPR_LT: case OPR_LE: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
850 codecomp(fs, cast(OpCode, op - OPR_EQ + OP_EQ), 1, e1, e2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
851 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
852 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
853 case OPR_NE: case OPR_GT: case OPR_GE: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
854 codecomp(fs, cast(OpCode, op - OPR_NE + OP_EQ), 0, e1, e2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
855 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
856 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
857 default: lua_assert(0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
858 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
859 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
860
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
861
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
862 void luaK_fixline (FuncState *fs, int line) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
863 fs->f->lineinfo[fs->pc - 1] = line;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
864 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
865
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
866
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
867 void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
868 int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
869 int b = (tostore == LUA_MULTRET) ? 0 : tostore;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
870 lua_assert(tostore != 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
871 if (c <= MAXARG_C)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
872 luaK_codeABC(fs, OP_SETLIST, base, b, c);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
873 else if (c <= MAXARG_Ax) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
874 luaK_codeABC(fs, OP_SETLIST, base, b, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
875 codeextraarg(fs, c);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
876 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
877 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
878 luaX_syntaxerror(fs->ls, "constructor too long");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
879 fs->freereg = base + 1; /* free registers with list values */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
880 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
881