added statistics generation
[swftools.git] / lib / as3 / code.c
1 #include <assert.h>
2 #include "code.h"
3
4 #define OP_REGISTER 1
5 #define OP_STACK_ARGS 2
6 #define OP_STACK_NS 4
7 #define OP_SET_DXNS 8
8 #define OP_RETURN 16
9 #define OP_THROW 32
10 #define OP_BRANCH 32
11 #define OP_JUMP 64
12 #define OP_LABEL 128
13 #define OP_SWITCH 256
14 #define OP_NEED_ACTIVATION 512
15 #define OP_STACK_ARGS2 1024
16
17 /* 2 = u30 index into multiname
18    m = u30 index into method
19    n = u30 number of params
20    i = u30 index into method info
21    b = byte
22    s = short
23    c = u30 index into class
24    s = string (u30 index into strings)
25    S = switch
26    u = u30
27    r = register
28 */
29 opcode_t opcodes[]={
30 {0xa0, "add", "",              -2, 1, 0, 0},
31 {0xc5, "add_i", "",            -2, 1, 0, 0},
32 {0x86, "astype", "2",          -1, 1, 0, 0},
33 {0x87, "astypelate", "",       -2, 1, 0, 0},
34 {0xA8, "bitand", "",           -2, 1, 0, 0},
35 {0x97, "bitnot", ""            -1, 1, 0, 0},
36 {0xa9, "bitor", "",            -2, 1, 0, 0},
37 {0xaa, "bitxor", ""            -2, 1, 0, 0},
38 {0x41, "call", "n",            -2, 1, 0, OP_STACK_ARGS},
39 {0x43, "callmethod", "mn",     -1, 1, 0, OP_STACK_ARGS},
40 {0x4c, "callproplex", "2n",    -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
41 {0x46, "callproperty", "2n",   -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
42 {0x4f, "callpropvoid", "2n",   -1, 0, 0, OP_STACK_ARGS|OP_STACK_NS},
43 {0x44, "callstatic", "in",     -1, 1, 0, OP_STACK_ARGS},
44 {0x45, "callsuper", "2n",      -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
45 {0x4e, "callsupervoid", "2n",  -1, 0, 0, OP_STACK_ARGS|OP_STACK_NS},
46 {0x78, "checkfilter", "",      -1, 1, 0, 0},
47 {0x80, "coerce", "2",          -1, 1, 0, 0},
48 {0x82, "coerce_a", "",         -1, 1, 0, 0},
49 {0x85, "coerce_s", "",         -1, 1, 0, 0},
50 {0x42, "construct", "n",       -1, 1, 0, OP_STACK_ARGS},
51 {0x4a, "constructprop", "2n",  -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
52 {0x49, "constructsuper", "n",  -1, 1, 0, OP_STACK_ARGS},
53 {0x76, "convert_b", "",        -1, 1, 0, 0},
54 {0x73, "convert_i", "",        -1, 1, 0, 0},
55 {0x75, "convert_d", "",        -1, 1, 0, 0},
56 {0x77, "convert_o", "",        -1, 1, 0, 0},
57 {0x74, "convert_u", "",        -1, 1, 0, 0},
58 {0x70, "convert_s", "",        -1, 1, 0, 0},
59 {0xef, "debug", "D",            0, 0, 0, 0},
60 {0xf1, "debugfile", "s",        0, 0, 0, 0},
61 {0xf0, "debugline", "u",        0, 0, 0, 0},
62 {0x94, "declocal", "r",         0, 0, 0, OP_REGISTER},
63 {0xc3, "declocal_i", "r",       0, 0, 0, OP_REGISTER},
64 {0x93, "decrement", "",        -1, 1, 0, 0}, 
65 {0xc1, "decrement_i", "",      -1, 1, 0, 0},
66 {0x6a, "deleteproperty", "2",  -1, 1, 0, OP_STACK_NS},
67 {0xa3, "divide", "",           -2, 1, 0, 0},
68 {0x2a, "dup", "",              -1, 2, 0, 0},
69 {0x06, "dxns", "s",             0, 0, 0, OP_SET_DXNS},
70 {0x07, "dxnslate", "",         -1, 0, 0, OP_SET_DXNS},
71 {0xab, "equals", "",           -2, 1, 0, 0},
72 {0x72, "esc_xattr", "",        -1, 1, 0, 0},
73 {0x71, "esc_xelem", "",        -1, 1, 0, 0},
74 {0x5e, "findproperty", "2",     0, 1, 0, OP_STACK_NS},
75 {0x5d, "findpropstrict", "2",   0, 1, 0, OP_STACK_NS},
76 {0x59, "getdescendants", "2",  -1, 1, 0, OP_STACK_NS},
77 {0x64, "getglobalscope", "",    0, 1, 0, 0},
78 {0x6e, "getglobalslot", "u",    0, 1, 0, 0},
79 {0x60, "getlex", "2",           0, 1, 0, 0}, //multiname may not be runtime
80 {0x62, "getlocal", "r",         0, 1, 0, OP_REGISTER},
81 {0xd0, "getlocal_0", "",        0, 1, 0, OP_REGISTER},
82 {0xd1, "getlocal_1", "",        0, 1, 0, OP_REGISTER},
83 {0xd2, "getlocal_2", "",        0, 1, 0, OP_REGISTER},
84 {0xd3, "getlocal_3", "",        0, 1, 0, OP_REGISTER},
85 {0x66, "getproperty", "2",     -1, 1, 0, OP_STACK_NS},
86 {0x65, "getscopeobject", "u",   0, 1, 0, 0}, // u = index into scope stack
87 {0x6c, "getslot", "u",         -1, 1, 0, 0},
88 {0x04, "getsuper", "2",        -1, 1, 0, OP_STACK_NS},
89 {0xaf, "greaterequals", "",    -2, 1, 0, 0},
90 {0x1f, "hasnext", "",          -2, 1, 0, 0},
91 {0x32, "hasnext2", "rr",        0, 1, 0, OP_REGISTER},
92 {0x13, "ifeq", "j",            -2, 0, 0, OP_BRANCH},
93 {0x12, "iffalse", "j",         -1, 0, 0, OP_BRANCH},
94 {0x18, "ifge", "j",            -2, 0, 0, OP_BRANCH},
95 {0x17, "ifgt", "j",            -2, 0, 0, OP_BRANCH},
96 {0x16, "ifle", "j",            -2, 0, 0, OP_BRANCH},
97 {0x15, "iflt", "j",            -2, 0, 0, OP_BRANCH},
98 {0x0f, "ifnge", "j",           -2, 0, 0, OP_BRANCH},
99 {0x0e, "ifngt", "j",           -2, 0, 0, OP_BRANCH},
100 {0x0d, "ifnle", "j",           -2, 0, 0, OP_BRANCH},
101 {0x0c, "ifnlt", "j",           -2, 0, 0, OP_BRANCH},
102 {0x14, "ifne", "j",            -2, 0, 0, OP_BRANCH},
103 {0x19, "ifstricteq", "j",      -2, 0, 0, OP_BRANCH},
104 {0x1a, "ifstrictne", "j",      -2, 0, 0, OP_BRANCH},
105 {0x11, "iftrue", "j",          -1, 0, 0, OP_BRANCH},
106 {0xb4, "in", "",               -2, 1, 0, 0},
107 {0x92, "inclocal", "r",         0, 0, 0, OP_REGISTER},
108 {0xc2, "inclocal_i", "r",       0, 0, 0, OP_REGISTER},
109 {0x91, "increment", "",        -1, 1, 0, 0},
110 {0xc0, "increment_i", "",      -1, 1, 0, 0},
111 {0x68, "initproperty", "2",    -1, 0, 0, OP_STACK_NS},
112 {0xb1, "instanceof", "",       -2, 1, 0, 0},
113 {0xb2, "istype", "2",          -1, 1, 0, 0}, // may not be a runtime multiname
114 {0xb3, "istypelate", "",       -2, 1, 0, 0},
115 {0x10, "jump", "j",             0, 0, 0, OP_JUMP},
116 {0x08, "kill", "r",             0, 0, 0, OP_REGISTER},
117 {0x09, "label", "",             0, 0, 0, OP_LABEL},
118 {0xae, "lessequals", "",       -2, 1, 0, 0},
119 {0xad, "lessthan", "",         -2, 1, 0, 0},
120 {0x1b, "lookupswitch", "S",    -1, 0, 0, OP_SWITCH},
121 {0xa5, "lshift", "",           -2, 1, 0, 0},
122 {0xa4, "modulo", "",           -2, 1, 0, 0},
123 {0xa2, "multiply", "",         -2, 1, 0, 0},
124 {0xc7, "multiply_i", "",       -2, 1, 0, 0},
125 {0x90, "negate", "",           -1, 1, 0, 0},
126 {0xc4, "negate_i", "",         -1, 1, 0, 0},
127 {0x57, "newactivation", "",     0, 1, 0, OP_NEED_ACTIVATION},
128 {0x56, "newarray", "n",         0, 1, 0, OP_STACK_ARGS},
129 {0x5a, "newcatch", "u",         0, 1, 0, 0}, //u = index into exception_info
130 {0x58, "newclass", "c",        -1, 1, 0, 0}, //c = index into class_info
131 {0x40, "newfunction", "m",      0, 1, 0, 0}, //i = index into method_info
132 {0x55, "newobject", "n",        0, 1, 0, OP_STACK_ARGS2},
133 {0x1e, "nextname", "",         -2, 1, 0, 0},
134 {0x23, "nextvalue", "",        -2, 1, 0, 0},
135 {0x02, "nop", "",               0, 0, 0, 0},
136 {0x96, "not", "",              -1, 1, 0 ,0},
137 {0x29, "pop", "",              -1, 0, 0, 0},
138 {0x1d, "popscope", "",          0, 0,-1, 0},
139 {0x24, "pushbyte", "b",         0, 1, 0, 0},
140 {0x2f, "pushdouble", "u",       0, 1, 0, 0}, //index into floats
141 {0x27, "pushfalse", "",         0, 1, 0, 0},
142 {0x2d, "pushint", "u",          0, 1, 0, 0}, //index into ints
143 {0x31, "pushnamespace", "u",    0, 1, 0, 0}, //index into namespace
144 {0x28, "pushnan", "",           0, 1, 0, 0},
145 {0x20, "pushnull", "",          0, 1, 0, 0},
146 {0x30, "pushscope", "",         0, 0, 1, 0},
147 {0x25, "pushshort", "u",        0, 1, 0, 0},
148 {0x2c, "pushstring", "s",       0, 1, 0, 0},
149 {0x26, "pushtrue", "",          0, 1, 0, 0},
150 {0x2e, "pushuint", "u",         0, 1, 0, 0}, //index into uints
151 {0x21, "pushundefined", "",     0, 1, 0, 0},
152 {0x1c, "pushwith", "",         -1, 0, 1, 0},
153 {0x48, "returnvalue", "",      -1, 0, 0, OP_RETURN},
154 {0x47, "returnvoid", "",        0, 0, 0, OP_RETURN},
155 {0xa6, "rshift", "",           -2, 1, 0, 0},
156 {0x63, "setlocal", "r",        -1, 0, 0, OP_REGISTER},
157 {0xd4, "setlocal_0", "",       -1, 0, 0, OP_REGISTER},
158 {0xd5, "setlocal_1", "",       -1, 0, 0, OP_REGISTER},
159 {0xd6, "setlocal_2", "",       -1, 0, 0, OP_REGISTER},
160 {0xd7, "setlocal_3", "",       -1, 0, 0, OP_REGISTER},
161 {0x6f, "setglobalslot", "u",   -1, 0, 0, 0},
162 {0x61, "setproperty", "2",     -2, 0, 0, OP_STACK_NS},
163 {0x6d, "setslot", "2",         -2, 0, 0, 0},
164 {0x05, "setsuper", "2",        -2, 0, 0, OP_STACK_NS},
165 {0xac, "strictequals", "",     -2, 1, 0, 0},
166 {0xa1, "subtract", "",         -2, 1, 0, 0},
167 {0xc6, "subtract_i", "",       -2, 1, 0, 0},
168 {0x2b, "swap", "",             -2, 2, 0, 0},
169 {0x03, "throw", "",            -1, 0, 0, OP_THROW},
170 {0x95, "typeof", "",           -1, 1, 0, 0},
171 {0xa7, "urshift", "",          -2, 1, 0, 0},
172 {0xb0, "xxx", "",               0, 0, 0, 0},
173 };
174
175 static U8 op2index[256] = {254};
176
177 opcode_t* opcode_get(U8 op)
178 {
179     int t;
180     if(op2index[0]==254) {
181         memset(op2index, 255, sizeof(op2index));
182         for(t=0;t<sizeof(opcodes)/sizeof(opcodes[0]);t++) {
183             op2index[opcodes[t].opcode] = t;
184         }
185     }
186     if(op2index[op]!=255)
187         return &opcodes[op2index[op]];
188     return 0;
189 }
190
191 abc_code_t*code_parse(TAG*tag, int len, abc_file_t*file, pool_t*pool)
192 {
193     abc_code_t*head=0;
194     abc_code_t*code=0;
195     int start=tag->pos;
196     int end=tag->pos+len;
197     //printf("-->\n");fflush(stdout);
198
199     abc_code_t**bytepos = rfx_calloc(sizeof(abc_code_t*)*len);
200         
201     while(tag->pos<end) {
202         int codepos = tag->pos-start;
203         U8 opcode = swf_GetU8(tag);
204         opcode_t*op = opcode_get(opcode);
205         if(!op) {
206             fprintf(stderr, "Can't parse opcode %02x\n", opcode);
207             return head;
208         }
209         //printf("%s\n", op->name);fflush(stdout);
210         NEW(abc_code_t,c);
211         c->pos = codepos;
212
213         bytepos[codepos] = c;
214
215         if(!head) {
216             head = code = c;
217         } else {
218             code->next = c;
219             code = c;
220         }
221
222         c->opcode = opcode;
223
224         char*p = op->params;
225         int pos = 0;
226         while(*p) {
227             void*data = 0;
228             if(*p == 'n') { // number
229                 data = (void*)(ptroff_t)swf_GetU30(tag);
230             } else if(*p == '2') { //multiname
231                 data = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
232             } else if(*p == 'm') { //method
233                 data = array_getvalue(file->methods, swf_GetU30(tag));
234             } else if(*p == 'c') { //classinfo
235                 data = array_getvalue(file->classes, swf_GetU30(tag));
236             } else if(*p == 'i') {
237                 data = array_getvalue(file->method_bodies, swf_GetU30(tag));
238             } else if(*p == 'u') { // generic integer
239                 data = (void*)(ptroff_t)swf_GetU30(tag);
240             } else if(*p == 'r') { // local register
241                 data = (void*)(ptroff_t)swf_GetU30(tag);
242             } else if(*p == 'b') { // byte
243                 data = (void*)(ptroff_t)swf_GetU8(tag);
244             } else if(*p == 'j') { // jump
245                 int j = swf_GetS24(tag);
246                 data = (void*)(ptroff_t)j;
247             } else if(*p == 's') { // string
248                 data = strdup((char*)pool_lookup_string(pool, swf_GetU30(tag)));
249             } else if(*p == 'D') { // debug
250                 /*type, usually 1*/
251                 U8 type = swf_GetU8(tag);
252                 if(type!=1) 
253                     fprintf(stderr, "Unknown debug type: %02x\n", type);
254                 /*register name*/
255                 code->data[0] = strdup((char*)pool_lookup_string(pool, swf_GetU30(tag)));
256                 /*register index*/
257                 code->data[1] = (void*)(ptroff_t)swf_GetU8(tag);
258                 /*unused*/
259                 swf_GetU30(tag);
260             } else if(*p == 'S') { // switch statement TODO
261                 /* I hate these things */
262                 swf_GetU24(tag); //default
263                 int num = swf_GetU30(tag)+1;
264                 int t;
265                 for(t=0;t<num;t++) 
266                     swf_GetU24(tag);
267                 data = 0;
268             } else {
269                 printf("Can't parse opcode param type \"%c\".\n", *p);
270                 return 0;
271             }
272             if(data)
273                 code->data[pos++] = data;
274             p++;
275         }
276     }
277
278 //#define DEBUG_BYTES
279 #ifdef DEBUG_BYTES
280     int t;
281     for(t=0;t<len;t++) {
282         abc_code_t*c = bytepos[t];
283         if(c) {
284             opcode_t*op = opcode_get(c->opcode);
285             if(op->flags & (OP_JUMP|OP_BRANCH)) {
286                 printf("%5d) %02x %s %d\n", t, tag->data[start+t], op->name, c->data[0]);
287             } else {
288                 printf("%5d) %02x %s\n", t, tag->data[start+t], op->name);
289             }
290         } else {
291             printf("%5d) %02x\n", t, tag->data[start+t]);
292         }
293     }
294     printf("%5d) %02x\n", t, tag->data[start+t]);
295 #endif
296
297     abc_code_t*c = head;
298     while(c) {
299         opcode_t*op = opcode_get(c->opcode);
300         if(op->flags & (OP_JUMP|OP_BRANCH)) {
301             int j = ((int)(ptroff_t)c->data[0]) + 4;
302 #ifdef DEBUG_BYTES
303             printf("%s %d %d\n", op->name, c->pos, j);
304 #endif
305             if(c->pos+j < 0 ||
306                c->pos+j > len ||
307               (c->pos+j!=len && !bytepos[c->pos+j])) {
308                 /* flex likes to generate these. yuck. */
309                 fprintf(stderr, "Invalid jump instruction \"%s\" from %d to %d (%d)\n", op->name, c->pos, c->pos+j, len);
310                 code->branch = 0;
311             } else {
312                 if(c->pos==len)
313                     code->branch = 0;
314                 else
315                     code->branch = bytepos[c->pos+j];
316             }
317         }
318         c = c->next;
319     } 
320     free(bytepos);
321     return head;
322 }
323
324 void code_free(abc_code_t*c)
325 {
326     while(c) {
327         abc_code_t*next = c->next;
328         opcode_t*op = opcode_get(c->opcode);
329         char*p = op?op->params:"";
330         int pos=0;
331         while(*p) {
332             void*data = c->data[pos];
333             if(*p == '2') { //multiname
334                 multiname_destroy(data);
335             } else if(strchr("sD", *p)) {
336                 free(data);
337             }
338             c->data[pos]=0;
339             p++;pos++;
340         }
341         memset(c, 0, sizeof(c));
342         free(c);
343         c = next;
344     }
345 }
346
347 int code_dump(abc_code_t*c, abc_file_t*file, char*prefix, FILE*fo)
348 {
349     pool_t*pool = pool_new();
350
351     while(c) {
352         U8 opcode = c->opcode;
353         int t;
354         char found = 0;
355         opcode_t*op = opcode_get(opcode);
356         if(!op) {
357             fprintf(stderr, "Can't parse opcode %02x.\n", opcode);
358             return 0;
359         } else {
360             fprintf(fo, "%s%s ", prefix, op->name);
361             char*p = op->params;
362             char first = 1;
363             int pos=0;
364             while(*p) {
365                 void*data = c->data[pos];
366                 if(pos>0)
367                     printf(", ");
368
369                 if(*p == 'n') {
370                     int n = (ptroff_t)data;
371                     fprintf(fo, "%d params", n);
372                 } else if(*p == '2') {
373                     multiname_t*n = (multiname_t*)data;
374                     char* m = multiname_to_string(n);
375                     fprintf(fo, "%s", m);
376                     free(m);
377                 } else if(*p == 'm') {
378                     abc_method_t*m = (abc_method_t*)data;
379                     fprintf(fo, "[method %s]", m->name);
380                 } else if(*p == 'c') {
381                     abc_class_t*cls = (abc_class_t*)data;
382                     char*classname = multiname_to_string(cls->classname);
383                     fprintf(fo, "[classinfo %s]", classname);
384                     free(classname);
385                 } else if(*p == 'i') {
386                     abc_method_body_t*b = (abc_method_body_t*)data;
387                     fprintf(fo, "[methodbody]");
388                 } else if(*p == 'u') {
389                     int n = (ptroff_t)data;
390                     fprintf(fo, "%d", n);
391                 } else if(*p == 'r') {
392                     int n = (ptroff_t)data;
393                     fprintf(fo, "r%d", n);
394                 } else if(*p == 'b') {
395                     int b = (ptroff_t)data;
396                     fprintf(fo, "%02x", b);
397                 } else if(*p == 'j') {
398                     int n = (ptroff_t)data;
399                     fprintf(fo, "%d", n);
400                 } else if(*p == 's') {
401                     fprintf(fo, "\"%s\"", data);
402                 } else if(*p == 'D') {
403                     fprintf(fo, "[register %02x=%s]", (ptroff_t)c->data[1], (char*)c->data[0]);
404                 } else if(*p == 'S') {
405                     fprintf(fo, "[switch data]");
406                 } else {
407                     fprintf(stderr, "Can't parse opcode param type \"%c\"\n", *p);
408                     return 0;
409                 }
410                 p++;
411                 pos++;
412                 first = 0;
413             }
414             fprintf(fo, "\n");
415         }
416         c = c->next;
417     }
418     return 1;
419 }
420
421 static int opcode_write(TAG*tag, abc_code_t*code, pool_t*pool, abc_file_t*file)
422 {
423     opcode_t*op = opcode_get(code->opcode);
424     char*p = op->params;
425     int pos = 0;
426     int len = 0;
427     
428     if(tag)
429         swf_SetU8(tag, code->opcode);
430     len++;
431
432     while(*p) {
433         void*data = code->data[pos++];
434         assert(pos<=2);
435         if(*p == 'n') { // number
436             len += swf_SetU30(tag, (ptroff_t)data);
437         } else if(*p == '2') { //multiname
438             multiname_t*m = (multiname_t*)data;
439             len += swf_SetU30(tag, pool_register_multiname(pool, m));
440         } else if(*p == 'm') { //method
441             abc_method_t*m = (abc_method_t*)data;
442             len += swf_SetU30(tag, m->index);
443         } else if(*p == 'c') { //classinfo 
444             abc_class_t*cls = (abc_class_t*)data;
445             len += swf_SetU30(tag, cls->index);
446         } else if(*p == 'i') { //methodbody
447             abc_method_body_t*m = (abc_method_body_t*)data;
448             len += swf_SetU30(tag, m->index);
449         } else if(*p == 'u') { // integer
450             len += swf_SetU30(tag, (ptroff_t)data);
451         } else if(*p == 'r') { // integer
452             len += swf_SetU30(tag, (ptroff_t)data);
453         } else if(*p == 'b') { // byte
454             if(tag)
455                 swf_SetU8(tag, (ptroff_t)data);
456             len++;
457         } else if(*p == 'j') { // jump
458             len += swf_SetS24(tag, (ptroff_t)data);
459         } else if(*p == 's') { // string
460             int index = pool_register_string(pool, data);
461             len += swf_SetU30(tag, index);
462         } else if(*p == 'D') { // debug statement
463             if(tag)
464                 swf_SetU8(tag, 1);
465             len++;
466             len+=swf_SetU30(tag, pool_register_string(pool,code->data[0]));
467             if(tag)
468                 swf_SetU8(tag, (ptroff_t)code->data[1]);
469             len++;
470             len+=swf_SetU30(tag, 0);
471         } else if(*p == 'S') { // switch statement
472             len+=swf_SetU24(tag, 0); //default
473             len+=swf_SetU30(tag, 0); //nr-1
474             len+=swf_SetU24(tag, 0); //first
475         } else {
476             printf("Can't parse opcode param type \"%c\"\n", *p);
477         }
478         p++;
479     }
480     return len;
481 }
482
483 void code_write(TAG*tag, abc_code_t*code, pool_t*pool, abc_file_t*file)
484 {
485     int pos = 0;
486     int length = 0;
487     abc_code_t*c = code;
488     while(c) {
489         c->pos = pos;
490         pos += opcode_write(0, c, pool, file);
491         c = c->next;
492     }
493     length = pos;
494     swf_SetU30(tag, pos);
495     int start = tag->len;
496     c = code;
497     pos = 0;
498     while(c) {
499         opcode_t*op = opcode_get(code->opcode);
500         if(op->flags&(OP_BRANCH|OP_JUMP)) {
501             int skip = 0;
502             if(c->branch)
503                 skip = (c->branch->pos) - pos - 4;
504             else
505                 skip = length - pos - 4;
506             c->data[0] = (void*)(ptroff_t)skip;
507         }
508         pos += opcode_write(tag, c, pool, file);
509         c = c->next;
510     }
511     assert(tag->len - start == pos);
512 }
513
514
515 typedef struct {
516     int stackpos;
517     int scopepos;
518     abc_code_t*code;
519     char seen;
520 } stackpos_t;
521
522 typedef struct {
523     stackpos_t*stack;
524     int num;
525     int maxlocal;
526     int maxstack;
527     int maxscope;
528 } currentstats_t;
529
530 static int stack_minus(abc_code_t*c)
531 {
532     opcode_t*op = opcode_get(c->opcode);
533     assert(op->stack_minus<=0);
534     int stack = op->stack_minus;
535     if(op->flags&OP_STACK_NS) {
536         multiname_t*m = (multiname_t*)c->data[0];
537         stack--; //read namespace
538         if(m->type == RTQNAMEL || m->type == RTQNAMELA) {
539             stack--; //read name
540         }
541     } 
542     if(op->flags&OP_STACK_ARGS || op->flags&OP_STACK_ARGS2) {
543         assert(strchr(op->params, 'n'));
544         int nr = (ptroff_t)(op->params[0]=='n'?c->data[0]:c->data[1]);
545         stack-=nr;
546         if(op->flags&OP_STACK_ARGS2)
547             stack-=nr;
548     }
549     return stack;
550 }
551 static void handleregister(currentstats_t*stats, int reg)
552 {
553     if(reg > stats->maxlocal)
554         stats->maxlocal = reg;
555 }
556
557 static void callcode(currentstats_t*stats, int pos, int stack, int scope)
558 {
559     while(pos<stats->num) {
560         if(stats->stack[pos].seen) {
561             if(stats->stack[pos].stackpos != stack ||
562                stats->stack[pos].scopepos != scope) {
563                 fprintf(stderr, "Stack mismatch at pos %d\n", pos);
564             }
565             return;
566         }
567         stats->stack[pos].seen = 1;
568         stats->stack[pos].stackpos = stack;
569         stats->stack[pos].scopepos = scope;
570
571         abc_code_t*c = stats->stack[pos].code;
572         opcode_t*op = opcode_get(c->opcode);
573
574         stack += stack_minus(c);
575
576         if(stack<0) {
577             fprintf(stderr, "error: stack underflow at %d (%s)\n", pos, op->name);
578             return;
579         }
580
581         stack += op->stack_plus;
582         scope += op->scope_stack_plus;
583
584         if(stack > stats->maxstack)
585             stats->maxstack = stack;
586         if(scope > stats->maxscope)
587             stats->maxscope = scope;
588         
589         if(op->flags & OP_REGISTER) {
590             char*p = op->params;
591             int pos = 0;
592             char ok=0;
593             while(*p) {
594                 if(*p=='r') {
595                     handleregister(stats, (ptroff_t)c->data[pos]);
596                     ok = 1;
597                 }
598                 p++;
599             }
600             if(!ok) {
601                 handleregister(stats, c->opcode&3);
602             }
603         }
604         if(op->flags & (OP_THROW|OP_RETURN))
605             return;
606         if(op->flags & OP_JUMP) {
607             c = c->branch;
608             pos = c->pos;
609             continue;
610         }
611         if(op->flags & OP_BRANCH) {
612             int newpos = c->branch->pos;
613             callcode(stats, newpos, stack, scope);
614         }
615     
616         pos++;
617         if(pos<stats->num) {
618             assert(c->next == stats->stack[pos].code);
619         }
620     }
621 }
622
623 codestats_t code_get_statistics(abc_code_t*code) 
624 {
625     int num = 0;
626     abc_code_t*c = code;
627     while(c) {
628         num++;
629         c = c->next;
630     }
631     currentstats_t current;
632     current.stack = rfx_calloc(sizeof(stackpos_t)*num);
633     current.maxlocal = 0;
634     current.maxstack = 0;
635     current.maxscope = 0;
636     current.num = num;
637
638     num = 0;
639     c = code;
640     while(c) {
641         //crosslink
642         current.stack[num].code = c;
643         c->pos = num;
644         num++;
645         c = c->next;
646     }
647     callcode(&current, 0, 0, 0);
648
649     free(current.stack);
650
651     codestats_t stats;
652     stats.local_count = current.maxlocal;
653     stats.max_stack = current.maxstack;
654     stats.max_scope_depth = current.maxscope;
655     return stats;
656 }
657 abc_code_t* add_opcode(abc_code_t*atag, U8 op)
658 {
659     abc_code_t*tmp = (abc_code_t*)malloc(sizeof(abc_code_t));
660     tmp->opcode = op;
661     tmp->next = 0;
662     if(atag) {
663         tmp->prev = atag;
664         atag->next = tmp;
665         tmp->parent = atag->parent;
666     } else {
667         tmp->prev = 0;
668         tmp->parent = tmp;
669     }
670     return tmp;
671 }
672