-
Notifications
You must be signed in to change notification settings - Fork 6
/
changes.txt
308 lines (226 loc) · 8.17 KB
/
changes.txt
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
This is a modified version of the Lua 5.3.4 interpreter. Here are the changes I had to make:
1) All the internal functions had to be made public (in luaconf.h):
===================================================================
// In luaconf.h
- #if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && defined(__ELF__) /* { */
- #define LUAI_FUNC __attribute__((visibility("hidden"))) extern*/
- #else*/ /* }{ */
#define LUAI_FUNC extern
- #endif /* } */
// In lvm.c
static int forlimit --> int luaV_forlimit
static int getcached --> int luaV_getcached
static void pushclosure --> int luaV_pushclosure
// In lvm.h
+ LUAI_FUNC int luaV_forlimit (const TValue *obj, lua_Integer *p, lua_Integer step,int *stopnow);
+ LUAI_FUNC LClosure *luaV_getcached (Proto *p, UpVal **encup, StkId base);
+ LUAI_FUNC void luaV_pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, StkId ra);
// In lapi.c
static TValue *index2addr --> LUA_FUNC TValue *index2addr
2) Add a "magic implementation" field to the Proto data structure
=================================================================
// In lobject.h
+ /*
+ ** For the AOT compilation hack
+ ** The parameter types are unspecified because they use internal types that
+ ** are not visible in this file.
+ */
+ typedef int (*ZZ_MAGIC_FUNC) (/* ARGS */);
/*
** Function Prototypes
*/
typedef struct Proto {
CommonHeader;
lu_byte numparams; /* number of fixed parameters */
lu_byte is_vararg;
/* ... */
+ ZZ_MAGIC_FUNC magic_implementation; /* For magic AOT compilation */
} Proto;
// In lfunc.c
Proto *luaF_newproto (lua_State *L) {
GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto));
Proto *f = gco2p(o);
f->k = NULL;
f->sizek = 0;
/* ... */
+ f->magic_implementation = NULL;
return f;
}
3) Call the magic implementation
=================================
// In ldo.c
int luaD_precall (lua_State *L, StkId func, int nresults) {
lua_CFunction f;
CallInfo *ci;
switch (ttype(func)) {
case LUA_TCCL: /* C closure */
/* ... */
goto Cfunc;
case LUA_TLCF: /* light C function */
/* ... */
return 1;
case LUA_TLCL: { /* Lua function: prepare its call */
/* ... */
+ /* AOT compilation magic */
+ if (p->magic_implementation) {
+ p->magic_implementation(L, clLvalue(func));
+ return 1;
+ }
return 0;
}
default: { /* not a function */
/* ... */
}
}
}
4) Structure of generated C modules
====================================
// ----------
// HEADER
// ----------
// Include directives:
#include "lprefix.h"
#include <assert.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "ldebug.h"
#include "ldo.h"
#include "lfunc.h"
#include "lgc.h"
#include "lobject.h"
#include "lopcodes.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
#include "lvm.h"
// Macros from lvm.c
// excluding: dojump, donextjump, vmdispatch, vmcase, vmbreak
#define MAXTAGLOOP 2000
/*
** 'l_intfitsf' checks whether a given integer can be converted to a
** float without rounding. Used in comparisons. Left undefined if
** all integers fit in a float precisely.
*/
#if !defined(l_intfitsf)
/* number of bits in the mantissa of a float */
#define NBM (l_mathlim(MANT_DIG))
/*
** Check whether some integers may not fit in a float, that is, whether
** (maxinteger >> NBM) > 0 (that implies (1 << NBM) <= maxinteger).
** (The shifts are done in parts to avoid shifting by more than the size
** of an integer. In a worst case, NBM == 113 for long double and
** sizeof(integer) == 32.)
*/
#if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \
>> (NBM - (3 * (NBM / 4)))) > 0
#define l_intfitsf(i) \
(-((lua_Integer)1 << NBM) <= (i) && (i) <= ((lua_Integer)1 << NBM))
#endif
#endif
/*
** some macros for common tasks in 'luaV_execute'
*/
#define RA(i) (base+GETARG_A(i))
#define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
#define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
#define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
#define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
#define Protect(x) { {x;}; base = ci->u.l.base; }
#define checkGC(L,c) \
{ luaC_condGC(L, L->top = (c), /* limit of live values */ \
Protect(L->top = ci->top)); /* restore top */ \
luai_threadyield(L); }
/*
** copy of 'luaV_gettable', but protecting the call to potential
** metamethod (which can reallocate the stack)
*/
#define gettableProtected(L,t,k,v) { const TValue *slot; \
if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \
else Protect(luaV_finishget(L,t,k,v,slot)); }
/* same for 'luaV_settable' */
#define settableProtected(L,t,k,v) { const TValue *slot; \
if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \
Protect(luaV_finishset(L,t,k,v,slot)); }
// ----------
// IMPLEMENTATION
// ----------
// The functions are outputed in depth-first order, just like luac does.
static int zz_magic_function_0 (lua_State *L, LClosure *cl)
{
CallInfo *ci = L->ci; (void) ci;
TValue *k = cl->p->k; (void) k;
StkId base = ci->u.l.base; (void) base;
label_0: {
Instruction i = 0x00000041;
StkId ra = RA(i);
/*...*/
}
label_1: {
Instruction i = 0x80000021;
StkId ra = RA(i);
/*...*/
}
/* ... */
}
static int zz_magic_function_1 (lua_State *L, LClosure *cl)
{
/* ... */
}
ZZ_MAGIC_FUNC zz_magic_functions[2] = {
zz_magic_function_0,
zz_magic_function_1,
};
static const char ZZ_ORIGINAL_SOURCE_CODE[] = { // (I couldn't get bytecode to work yet)
108, 111, 99, /* ... */
};
// ----------
// FOOTER
// ----------
#include "lauxlib.h"
#include "lualib.h"
static void bind_magic(Proto *p, int *next_id)
{
p->magic_implementation = zz_magic_functions[*next_id];
*next_id = *next_id + 1;
for(int i=0; i < p->sizep; i++) {
bind_magic(p->p[i], next_id);
}
}
int luaopen_fac (lua_State *L) {
int ok = luaL_loadstring(L, ZZ_ORIGINAL_SOURCE_CODE);
if (ok != LUA_OK) { /* ... */ }
LClosure *cl = (void *) lua_topointer(L, -1);
int next_id = 0;
bind_magic(cl->p, &next_id);
lua_call(L, 0, 1);
return 1;
}
5) Opcode differences
=====================
- Instruções removidas do vmfetch:
i = *(ci->u.l.savedpc++);
if (L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) Protect(luaG_traceexec(L));
lua_assert(base == ci->u.l.base);
lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
- atribuições a ci->u.l.savedpc viraram gotos
- int cmp; Protect(cmp= ...); if (cmp) { goto; } else { goto; }
- Em vez de usar os macros dojump e donextjump,
simplesmente dar um goto para o local apropriado do próximo jump.
- OP_CALL: goto newframe --> luaV_execute()
- OP_TAILCALL: complex logic --> same as OP_CALL
- OP_TFORCALL: não faço otimização de "goto tforloop"
- OP_SETLIST, OP_LOADKX: evita goto pq extra_arg é compilado para um NOP.
- OP_RETURN: simplificado, retorna um valor que nào preciso mais (?)
------------
TODO:
- remover o traceexec dos jumps
- repensar a API (acho que pode retornar void)
- parando pra pensar, muitas das minhas otimizações são avaliação parcial.