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
|
/*
** dump.c
** thread and save bytecodes to file
*/
char* rcs_dump="$Id: dump.c,v 1.17 1996/11/18 11:18:29 lhf Exp $";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "luac.h"
static TFunc* lastF=NULL; /* list of functions seen in code */
static int SawVar(int i, int at)
{
int old=VarLoc(i);
VarLoc(i)=at;
return old;
}
static int SawStr(int i, int at)
{
int old=StrLoc(i);
StrLoc(i)=at;
return old;
}
static void ThreadCode(Byte* code, Byte* end)
{
Byte* p;
int i;
for (i=0; i<lua_ntable; i++) VarLoc(i)=0;
for (i=0; i<lua_nconstant; i++) StrLoc(i)=0;
for (p=code; p!=end;)
{
OpCode op=(OpCode)*p;
int at=p-code+1;
switch (op)
{
case PUSHNIL:
case PUSH0:
case PUSH1:
case PUSH2:
case PUSHLOCAL0:
case PUSHLOCAL1:
case PUSHLOCAL2:
case PUSHLOCAL3:
case PUSHLOCAL4:
case PUSHLOCAL5:
case PUSHLOCAL6:
case PUSHLOCAL7:
case PUSHLOCAL8:
case PUSHLOCAL9:
case PUSHINDEXED:
case STORELOCAL0:
case STORELOCAL1:
case STORELOCAL2:
case STORELOCAL3:
case STORELOCAL4:
case STORELOCAL5:
case STORELOCAL6:
case STORELOCAL7:
case STORELOCAL8:
case STORELOCAL9:
case STOREINDEXED0:
case ADJUST0:
case EQOP:
case LTOP:
case LEOP:
case GTOP:
case GEOP:
case ADDOP:
case SUBOP:
case MULTOP:
case DIVOP:
case POWOP:
case CONCOP:
case MINUSOP:
case NOTOP:
case POP:
case RETCODE0:
p++;
break;
case PUSHBYTE:
case PUSHLOCAL:
case STORELOCAL:
case STOREINDEXED:
case STORELIST0:
case ADJUST:
case RETCODE:
p+=2;
break;
case PUSHWORD:
case CREATEARRAY:
case ONTJMP:
case ONFJMP:
case JMP:
case UPJMP:
case IFFJMP:
case IFFUPJMP:
case SETLINE:
case STORELIST:
case CALLFUNC:
p+=3;
break;
case PUSHFLOAT:
p+=5; /* assumes sizeof(float)==4 */
break;
case PUSHSELF:
case PUSHSTRING:
{
Word w;
p++;
get_word(w,p);
w=SawStr(w,at);
memcpy(p-2,&w,sizeof(w));
break;
}
case PUSHFUNCTION:
{
TFunc* tf;
p++;
get_code(tf,p);
tf->marked=at;
tf->next=NULL; /* TODO: remove? */
lastF=lastF->next=tf;
break;
}
case PUSHGLOBAL:
case STOREGLOBAL:
{
Word w;
p++;
get_word(w,p);
w=SawVar(w,at);
memcpy(p-2,&w,sizeof(w));
break;
}
case STORERECORD:
{
int n=*++p;
p++;
while (n--)
{
Word w;
at=p-code;
get_word(w,p);
w=SawStr(w,at);
memcpy(p-2,&w,sizeof(w));
}
break;
}
default:
fprintf(stderr,"luac: cannot happen: opcode=%d",*p);
exit(1);
break;
}
}
}
static void DumpWord(int i, FILE* D)
{
Word w=i;
fwrite(&w,sizeof(w),1,D);
}
static void DumpBlock(void* b, int size, FILE* D)
{
fwrite(b,size,1,D);
}
static void DumpSize(int i, FILE* D)
{
Word lo=i&0x0FFFF;
Word hi=(i>>16)&0x0FFFF;
fwrite(&hi,sizeof(hi),1,D);
fwrite(&lo,sizeof(lo),1,D);
if (hi!=0)
fprintf(stderr,
"luac: warning: code too long for 16-bit machines (%d bytes)\n",i);
}
static void DumpString(char* s, FILE* D)
{
int n=strlen(s)+1;
if ((Word)n != n)
{
fprintf(stderr,"luac: string too long: \"%.32s...\"\n",s);
exit(1);
}
DumpWord(n,D);
DumpBlock(s,n,D);
}
static void DumpStrings(FILE* D)
{
int i;
for (i=0; i<lua_ntable; i++)
{
if (VarLoc(i)!=0)
{
fputc(ID_VAR,D);
DumpWord(VarLoc(i),D);
DumpString(VarStr(i),D);
}
VarLoc(i)=i;
}
for (i=0; i<lua_nconstant; i++)
{
if (StrLoc(i)!=0)
{
fputc(ID_STR,D);
DumpWord(StrLoc(i),D);
DumpString(StrStr(i),D);
}
StrLoc(i)=i;
}
}
void DumpFunction(TFunc* tf, FILE* D)
{
lastF=tf;
ThreadCode(tf->code,tf->code+tf->size);
fputc(ID_FUN,D);
DumpSize(tf->size,D);
DumpWord(tf->lineDefined,D);
if (IsMain(tf))
DumpString(tf->fileName,D);
else
DumpWord(tf->marked,D);
DumpBlock(tf->code,tf->size,D);
DumpStrings(D);
}
void DumpHeader(FILE* D)
{
Word w=TEST_WORD;
float f=TEST_FLOAT;
fputc(ID_CHUNK,D);
fputs(SIGNATURE,D);
fputc(VERSION,D);
fputc(sizeof(Word),D);
fputc(sizeof(float),D);
fputc(sizeof(TFunc*),D);
fwrite(&w,sizeof(w),1,D);
fwrite(&f,sizeof(f),1,D);
}
|