3 Routines for handling Flash2 AVM2 ABC Actionscript
5 Extension module for the rfxswf library.
6 Part of the swftools package.
8 Copyright (c) 2008 Matthias Kramm <kramm@quiss.org>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
29 #define OP_STACK_ARGS 2
37 #define OP_LOOKUPSWITCH 512
38 #define OP_NEED_ACTIVATION 1024
39 #define OP_STACK_ARGS2 2048
41 /* 2 = u30 index into multiname
42 m = u30 index into method
43 n = u30 number of params
44 i = u30 index into method info
46 c = u30 index into class
47 s = string (u30 index into strings)
54 {0xa0, "add", "", -2, 1, 0, 0},
55 {0xc5, "add_i", "", -2, 1, 0, 0},
56 {0x86, "astype", "2", -1, 1, 0, 0},
57 {0x87, "astypelate", "", -2, 1, 0, 0},
58 {0xA8, "bitand", "", -2, 1, 0, 0},
59 {0x97, "bitnot", "", -1, 1, 0, 0},
60 {0xa9, "bitor", "", -2, 1, 0, 0},
61 {0xaa, "bitxor", "", -2, 1, 0, 0},
62 {0x41, "call", "n", -2, 1, 0, OP_STACK_ARGS},
63 {0x43, "callmethod", "mn", -1, 1, 0, OP_STACK_ARGS},
64 {0x4c, "callproplex", "2n", -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
65 {0x46, "callproperty", "2n", -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
66 {0x4f, "callpropvoid", "2n", -1, 0, 0, OP_STACK_ARGS|OP_STACK_NS},
67 {0x44, "callstatic", "in", -1, 1, 0, OP_STACK_ARGS},
68 {0x45, "callsuper", "2n", -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
69 {0x4e, "callsupervoid", "2n", -1, 0, 0, OP_STACK_ARGS|OP_STACK_NS},
70 {0x78, "checkfilter", "", -1, 1, 0, 0},
71 {0x80, "coerce", "2", -1, 1, 0, 0},
72 {0x82, "coerce_a", "", -1, 1, 0, 0},
73 {0x85, "coerce_s", "", -1, 1, 0, 0},
74 {0x42, "construct", "n", -1, 1, 0, OP_STACK_ARGS},
75 {0x4a, "constructprop", "2n", -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
76 {0x49, "constructsuper", "n", -1, 0, 0, OP_STACK_ARGS},
77 {0x76, "convert_b", "", -1, 1, 0, 0},
78 {0x73, "convert_i", "", -1, 1, 0, 0},
79 {0x75, "convert_d", "", -1, 1, 0, 0},
80 {0x77, "convert_o", "", -1, 1, 0, 0},
81 {0x74, "convert_u", "", -1, 1, 0, 0},
82 {0x70, "convert_s", "", -1, 1, 0, 0},
83 {0xef, "debug", "D", 0, 0, 0, 0},
84 {0xf1, "debugfile", "s", 0, 0, 0, 0},
85 {0xf0, "debugline", "u", 0, 0, 0, 0},
86 {0x94, "declocal", "r", 0, 0, 0, OP_REGISTER},
87 {0xc3, "declocal_i", "r", 0, 0, 0, OP_REGISTER},
88 {0x93, "decrement", "", -1, 1, 0, 0},
89 {0xc1, "decrement_i", "", -1, 1, 0, 0},
90 {0x6a, "deleteproperty", "2", -1, 1, 0, OP_STACK_NS},
91 {0xa3, "divide", "", -2, 1, 0, 0},
92 {0x2a, "dup", "", -1, 2, 0, 0},
93 {0x06, "dxns", "s", 0, 0, 0, OP_SET_DXNS},
94 {0x07, "dxnslate", "", -1, 0, 0, OP_SET_DXNS},
95 {0xab, "equals", "", -2, 1, 0, 0},
96 {0x72, "esc_xattr", "", -1, 1, 0, 0},
97 {0x71, "esc_xelem", "", -1, 1, 0, 0},
98 {0x5e, "findproperty", "2", 0, 1, 0, OP_STACK_NS},
99 {0x5d, "findpropstrict", "2", 0, 1, 0, OP_STACK_NS},
100 {0x59, "getdescendants", "2", -1, 1, 0, OP_STACK_NS},
101 {0x64, "getglobalscope", "", 0, 1, 0, 0},
102 {0x6e, "getglobalslot", "u", 0, 1, 0, 0},
103 {0x60, "getlex", "2", 0, 1, 0, 0}, //multiname may not be runtime
104 {0x62, "getlocal", "r", 0, 1, 0, OP_REGISTER},
105 {0xd0, "getlocal_0", "", 0, 1, 0, OP_REGISTER},
106 {0xd1, "getlocal_1", "", 0, 1, 0, OP_REGISTER},
107 {0xd2, "getlocal_2", "", 0, 1, 0, OP_REGISTER},
108 {0xd3, "getlocal_3", "", 0, 1, 0, OP_REGISTER},
109 {0x66, "getproperty", "2", -1, 1, 0, OP_STACK_NS},
110 {0x65, "getscopeobject", "u", 0, 1, 0, 0}, // u = index into scope stack
111 {0x6c, "getslot", "u", -1, 1, 0, 0},
112 {0x04, "getsuper", "2", -1, 1, 0, OP_STACK_NS},
113 {0xaf, "greaterthan", "", -2, 1, 0, 0},
114 {0xb0, "greaterequals", "", -2, 1, 0, 0},
115 {0x1f, "hasnext", "", -2, 1, 0, 0},
116 {0x32, "hasnext2", "rr", 0, 1, 0, OP_REGISTER},
117 {0x13, "ifeq", "j", -2, 0, 0, OP_BRANCH},
118 {0x12, "iffalse", "j", -1, 0, 0, OP_BRANCH},
119 {0x18, "ifge", "j", -2, 0, 0, OP_BRANCH},
120 {0x17, "ifgt", "j", -2, 0, 0, OP_BRANCH},
121 {0x16, "ifle", "j", -2, 0, 0, OP_BRANCH},
122 {0x15, "iflt", "j", -2, 0, 0, OP_BRANCH},
123 {0x0f, "ifnge", "j", -2, 0, 0, OP_BRANCH},
124 {0x0e, "ifngt", "j", -2, 0, 0, OP_BRANCH},
125 {0x0d, "ifnle", "j", -2, 0, 0, OP_BRANCH},
126 {0x0c, "ifnlt", "j", -2, 0, 0, OP_BRANCH},
127 {0x14, "ifne", "j", -2, 0, 0, OP_BRANCH},
128 {0x19, "ifstricteq", "j", -2, 0, 0, OP_BRANCH},
129 {0x1a, "ifstrictne", "j", -2, 0, 0, OP_BRANCH},
130 {0x11, "iftrue", "j", -1, 0, 0, OP_BRANCH},
131 {0xb4, "in", "", -2, 1, 0, 0},
132 {0x92, "inclocal", "r", 0, 0, 0, OP_REGISTER},
133 {0xc2, "inclocal_i", "r", 0, 0, 0, OP_REGISTER},
134 {0x91, "increment", "", -1, 1, 0, 0},
135 {0xc0, "increment_i", "", -1, 1, 0, 0},
136 {0x68, "initproperty", "2", -2, 0, 0, OP_STACK_NS},
137 {0xb1, "instanceof", "", -2, 1, 0, 0},
138 {0xb2, "istype", "2", -1, 1, 0, 0}, // may not be a runtime multiname
139 {0xb3, "istypelate", "", -2, 1, 0, 0},
140 {0x10, "jump", "j", 0, 0, 0, OP_JUMP},
141 {0x08, "kill", "r", 0, 0, 0, OP_REGISTER},
142 {0x09, "label", "", 0, 0, 0, OP_LABEL},
143 {0xae, "lessequals", "", -2, 1, 0, 0},
144 {0xad, "lessthan", "", -2, 1, 0, 0},
145 {0x1b, "lookupswitch", "S", -1, 0, 0, OP_LOOKUPSWITCH},
146 {0xa5, "lshift", "", -2, 1, 0, 0},
147 {0xa4, "modulo", "", -2, 1, 0, 0},
148 {0xa2, "multiply", "", -2, 1, 0, 0},
149 {0xc7, "multiply_i", "", -2, 1, 0, 0},
150 {0x90, "negate", "", -1, 1, 0, 0},
151 {0xc4, "negate_i", "", -1, 1, 0, 0},
152 {0x57, "newactivation", "", 0, 1, 0, OP_NEED_ACTIVATION},
153 {0x56, "newarray", "n", 0, 1, 0, OP_STACK_ARGS},
154 {0x5a, "newcatch", "u", 0, 1, 0, 0}, //u = index into exception_info
155 #define OP_NEWCLASS 0x58
156 {0x58, "newclass", "c", -1, 1, 0, 0}, //c = index into class_info
157 #define OP_NEWFUNCTION 0x40
158 {0x40, "newfunction", "m", 0, 1, 0, 0}, //i = index into method_info
159 {0x55, "newobject", "n", 0, 1, 0, OP_STACK_ARGS2},
160 {0x1e, "nextname", "", -2, 1, 0, 0},
161 {0x23, "nextvalue", "", -2, 1, 0, 0},
162 {0x02, "nop", "", 0, 0, 0, 0},
163 {0x96, "not", "", -1, 1, 0 ,0},
164 {0x29, "pop", "", -1, 0, 0, 0},
165 {0x1d, "popscope", "", 0, 0,-1, 0},
166 {0x24, "pushbyte", "b", 0, 1, 0, 0},
167 {0x2f, "pushdouble", "f", 0, 1, 0, 0}, //index into floats
168 {0x27, "pushfalse", "", 0, 1, 0, 0},
169 {0x2d, "pushint", "I", 0, 1, 0, 0}, //index into ints
170 {0x31, "pushnamespace", "u", 0, 1, 0, 0}, //index into namespace TODO
171 {0x28, "pushnan", "", 0, 1, 0, 0},
172 {0x20, "pushnull", "", 0, 1, 0, 0},
173 {0x30, "pushscope", "", -1, 0, 1, 0},
174 {0x25, "pushshort", "u", 0, 1, 0, 0},
175 {0x2c, "pushstring", "s", 0, 1, 0, 0},
176 {0x26, "pushtrue", "", 0, 1, 0, 0},
177 {0x2e, "pushuint", "U", 0, 1, 0, 0}, //index into uints
178 {0x21, "pushundefined", "", 0, 1, 0, 0},
179 {0x1c, "pushwith", "", -1, 0, 1, 0},
180 {0x48, "returnvalue", "", -1, 0, 0, OP_RETURN},
181 {0x47, "returnvoid", "", 0, 0, 0, OP_RETURN},
182 {0xa6, "rshift", "", -2, 1, 0, 0},
183 {0x63, "setlocal", "r", -1, 0, 0, OP_REGISTER},
184 {0xd4, "setlocal_0", "", -1, 0, 0, OP_REGISTER},
185 {0xd5, "setlocal_1", "", -1, 0, 0, OP_REGISTER},
186 {0xd6, "setlocal_2", "", -1, 0, 0, OP_REGISTER},
187 {0xd7, "setlocal_3", "", -1, 0, 0, OP_REGISTER},
188 {0x6f, "setglobalslot", "u", -1, 0, 0, 0},
189 {0x61, "setproperty", "2", -2, 0, 0, OP_STACK_NS},
190 {0x6d, "setslot", "u", -2, 0, 0, 0},
191 {0x05, "setsuper", "2", -2, 0, 0, OP_STACK_NS},
192 {0xac, "strictequals", "", -2, 1, 0, 0},
193 {0xa1, "subtract", "", -2, 1, 0, 0},
194 {0xc6, "subtract_i", "", -2, 1, 0, 0},
195 {0x2b, "swap", "", -2, 2, 0, 0},
196 {0x03, "throw", "", -1, 0, 0, OP_THROW},
197 {0x95, "typeof", "", -1, 1, 0, 0},
198 {0xa7, "urshift", "", -2, 1, 0, 0},
201 static U8 op2index[256] = {254};
203 opcode_t* opcode_get(U8 op)
206 if(op2index[0]==254) {
207 memset(op2index, 255, sizeof(op2index));
208 for(t=0;t<sizeof(opcodes)/sizeof(opcodes[0]);t++) {
209 op2index[opcodes[t].opcode] = t;
212 if(op2index[op]!=255)
213 return &opcodes[op2index[op]];
217 static code_t*pos2code(code_t**bytepos, code_t*c, int pos, int len)
224 (pos!=len && !bytepos[pos])) {
225 /* flex likes to generate these. yuck. */
227 opcode_t*op = opcode_get(c->opcode);
228 fprintf(stderr, "Warning: Invalid jump instruction \"%s\" from %d to %d (%d)\n", op->name, c->pos, pos, len);
230 fprintf(stderr, "Warning: Invalid jump to %d (%d)\n", pos, len);
235 //opcode_t*op = opcode_get(c->opcode);
236 //fprintf(stderr, "Warning: jump beyond end of code in instruction %s at position %d\n", op->name, c->pos);
243 code_t* code_atposition(codelookup_t*l, int pos)
245 return pos2code(l->bytepos, 0, pos, l->len);
248 void lookupswitch_print(lookupswitch_t*l)
250 printf("default: %08x\n", l->def);
251 code_list_t*t = l->targets;
253 printf("target: %08x\n", t->code);
258 code_t*code_parse(TAG*tag, int len, abc_file_t*file, pool_t*pool, codelookup_t**codelookup)
263 int end=tag->pos+len;
264 //printf("-->\n");fflush(stdout);
266 code_t**bytepos = rfx_calloc(sizeof(code_t*)*len);
268 while(tag->pos<end) {
269 int codepos = tag->pos-start;
270 U8 opcode = swf_GetU8(tag);
271 opcode_t*op = opcode_get(opcode);
273 fprintf(stderr, "Can't parse opcode %02x\n", opcode);
276 //printf("%s\n", op->name);fflush(stdout);
280 bytepos[codepos] = c;
295 if(*p == 'n') { // number
296 data = (void*)(ptroff_t)swf_GetU30(tag);
297 } else if(*p == '2') { //multiname
298 data = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
299 } else if(*p == 'U') { //uint
300 data = (void*)(ptroff_t)pool_lookup_uint(pool, swf_GetU30(tag));
301 } else if(*p == 'I') { //int
302 data = (void*)(ptroff_t)pool_lookup_int(pool, swf_GetU30(tag));
303 } else if(*p == 'f') { //int
304 double*fp = malloc(sizeof(double));
305 *fp = pool_lookup_float(pool, swf_GetU30(tag));
307 } else if(*p == 'm') { //method
308 data = array_getvalue(file->methods, swf_GetU30(tag));
309 } else if(*p == 'c') { //classinfo
310 data = array_getvalue(file->classes, swf_GetU30(tag));
311 } else if(*p == 'i') {
312 data = array_getvalue(file->method_bodies, swf_GetU30(tag));
313 } else if(*p == 'u') { // generic integer
314 data = (void*)(ptroff_t)swf_GetU30(tag);
315 } else if(*p == 'r') { // local register
316 data = (void*)(ptroff_t)swf_GetU30(tag);
317 } else if(*p == 'b') { // byte
318 data = (void*)(ptroff_t)swf_GetU8(tag);
319 } else if(*p == 'j') { // jump
320 int j = swf_GetS24(tag);
321 data = (void*)(ptroff_t)j;
322 } else if(*p == 's') { // string
323 data = strdup((char*)pool_lookup_string(pool, swf_GetU30(tag)));
324 } else if(*p == 'D') { // debug
326 U8 type = swf_GetU8(tag);
328 fprintf(stderr, "Unknown debug type: %02x\n", type);
330 code->data[0] = strdup((char*)pool_lookup_string(pool, swf_GetU30(tag)));
332 code->data[1] = (void*)(ptroff_t)swf_GetU8(tag);
335 } else if(*p == 'S') { // switch statement
336 lookupswitch_t*l = malloc(sizeof(lookupswitch_t));
337 l->def = (code_t*)(ptroff_t)swf_GetS24(tag);
338 l->targets = list_new();
339 int num = swf_GetU30(tag)+1;
342 list_append(l->targets, (code_t*)(ptroff_t)swf_GetS24(tag));
345 printf("Can't parse opcode param type \"%c\" (for op %02x %s).\n", *p, code->opcode, op->name);
349 code->data[pos++] = data;
354 //#define DEBUG_BYTES
358 code_t*c = bytepos[t];
360 opcode_t*op = opcode_get(c->opcode);
361 if(op->flags & (OP_JUMP|OP_BRANCH)) {
362 printf("%5d) %02x %s %d\n", t, tag->data[start+t], op->name, c->data[0]);
364 printf("%5d) %02x %s\n", t, tag->data[start+t], op->name);
367 printf("%5d) %02x\n", t, tag->data[start+t]);
370 //printf("%5d) %02x\n", t, tag->data[start+t]);
375 opcode_t*op = opcode_get(c->opcode);
376 if(op->flags & (OP_JUMP|OP_BRANCH)) {
377 int j = ((int)(ptroff_t)c->data[0]);
378 c->branch = pos2code(bytepos,c,j+4,len);
379 } else if(op->flags & (OP_LOOKUPSWITCH)) {
380 lookupswitch_t*l = (lookupswitch_t*)c->data[0];
382 l->def = pos2code(bytepos,c,(ptroff_t)l->def+offset,len);
383 code_list_t*t=l->targets;
385 t->code = pos2code(bytepos,c,(ptroff_t)t->code+offset,len);
393 (*codelookup) = malloc(sizeof(codelookup_t));
394 (*codelookup)->bytepos = bytepos;
395 (*codelookup)->len = len;
403 void codelookup_free(codelookup_t*codelookup)
405 free(codelookup->bytepos);codelookup->bytepos=0;
409 code_t*code_find_start(code_t*c)
416 void code_free(code_t*c)
418 c = code_find_start(c);
420 code_t*next = c->next;
421 opcode_t*op = opcode_get(c->opcode);
422 char*p = op?op->params:"";
425 void*data = c->data[pos];
426 if(*p == '2') { //multiname
427 multiname_destroy(data);
428 } else if(strchr("sDf", *p)) {
430 } else if(strchr("S", *p)) {
431 lookupswitch_t*l = (lookupswitch_t*)data;
432 list_free(l->targets);l->targets=0;
438 memset(c, 0, sizeof(c));
444 static int opcode_write(TAG*tag, code_t*c, pool_t*pool, abc_file_t*file, int length)
446 opcode_t*op = opcode_get(c->opcode);
452 swf_SetU8(tag, c->opcode);
456 void*data = c->data[pos++];
458 if(*p == 'n') { // number
459 len += swf_SetU30(tag, (ptroff_t)data);
460 } else if(*p == '2') { //multiname
461 multiname_t*m = (multiname_t*)data;
462 len += swf_SetU30(tag, pool_register_multiname(pool, m));
463 } else if(*p == 'm') { //method
464 abc_method_t*m = (abc_method_t*)data;
465 len += swf_SetU30(tag, m->index);
466 } else if(*p == 'c') { //classinfo
467 abc_class_t*cls = (abc_class_t*)data;
468 len += swf_SetU30(tag, cls->index);
469 } else if(*p == 'i') { //methodbody
470 abc_method_body_t*m = (abc_method_body_t*)data;
471 len += swf_SetU30(tag, m->index);
472 } else if(*p == 'I') { // int
473 len += swf_SetU30(tag, pool_register_int(pool, (ptroff_t)data));
474 } else if(*p == 'U') { // uint
475 len += swf_SetU30(tag, pool_register_uint(pool, (ptroff_t)data));
476 } else if(*p == 'f') { // float
477 len += swf_SetU30(tag, pool_register_float(pool, *(double*)data));
478 } else if(*p == 'u') { // integer
479 len += swf_SetU30(tag, (ptroff_t)data);
480 } else if(*p == 'r') { // integer
481 len += swf_SetU30(tag, (ptroff_t)data);
482 } else if(*p == 'b') { // byte
484 swf_SetU8(tag, (ptroff_t)data);
486 } else if(*p == 'j') { // jump
487 int skip = length-c->pos-4;
489 skip = (c->branch->pos) - c->pos - 4;
490 len += swf_SetS24(tag, skip);
491 } else if(*p == 's') { // string
492 int index = pool_register_string(pool, data);
493 len += swf_SetU30(tag, index);
494 } else if(*p == 'D') { // debug statement
498 len+=swf_SetU30(tag, pool_register_string(pool,c->data[0]));
500 swf_SetU8(tag, (ptroff_t)c->data[1]);
502 len+=swf_SetU30(tag, 0);
503 } else if(*p == 'S') { // switch statement
504 lookupswitch_t*l = (lookupswitch_t*)data;
506 len+=swf_SetS24(tag, l->def->pos-c->pos+offset); //default
507 code_list_t*t = l->targets;
509 len+=swf_SetU30(tag, list_length(t)-1); //nr-1
510 code_list_t*t = l->targets;
512 len+=swf_SetS24(tag, t->code->pos - c->pos+offset);
516 len+=swf_SetU30(tag, 0); //nr-1
517 len+=swf_SetS24(tag, l->def->pos-c->pos+offset);
520 printf("Can't parse opcode param type \"%c\"\n", *p);
527 void code_write(TAG*tag, code_t*code, pool_t*pool, abc_file_t*file)
529 code = code_find_start(code);
535 pos += opcode_write(0, c, pool, file, 0);
539 swf_SetU30(tag, pos);
540 int start = tag->len;
544 opcode_t*op = opcode_get(code->opcode);
545 if(op->flags&(OP_BRANCH|OP_JUMP)) {
548 pos += opcode_write(tag, c, pool, file, length);
551 assert(tag->len - start == pos);
570 static int stack_minus(code_t*c)
572 opcode_t*op = opcode_get(c->opcode);
573 if(op->stack_minus>0) {
574 fprintf(stderr, "Invalid opcode entry %02x %s\n", c->opcode, op->name);
576 int stack = op->stack_minus;
577 if(op->flags&OP_STACK_NS) {
578 multiname_t*m = (multiname_t*)c->data[0];
579 if(multiname_late_namespace(m))
581 if(multiname_late_name(m))
584 if(op->flags&OP_STACK_ARGS || op->flags&OP_STACK_ARGS2) {
585 assert(strchr(op->params, 'n'));
586 int nr = (ptroff_t)(op->params[0]=='n'?c->data[0]:c->data[1]);
588 if(op->flags&OP_STACK_ARGS2)
593 static void handleregister(currentstats_t*stats, int reg)
595 if(reg+1 > stats->maxlocal)
596 stats->maxlocal = reg+1;
599 static void dumpstack(currentstats_t*stats)
602 for(t=0;t<stats->num;t++) {
603 code_t*c = stats->stack[t].code;
604 opcode_t*op = opcode_get(c->opcode);
605 printf("%5d) %c %d:%d %s", t, stats->stack[t].seen?'x':'|',
606 stats->stack[t].stackpos,
607 stats->stack[t].scopepos,
610 if(op->flags&(OP_BRANCH|OP_JUMP)) {
612 printf(" ->%d\n", c->branch->pos);
614 printf(" 00000000\n");
616 if(op->params[0]=='2') {
617 printf(" %s", multiname_tostring(c->data[0]));
623 static char callcode(currentstats_t*stats, int pos, int stack, int scope)
625 while(pos<stats->num) {
626 if(stats->stack[pos].seen) {
627 if(stats->stack[pos].stackpos != stack ||
628 stats->stack[pos].scopepos != scope) {
630 fprintf(stderr, "Stack mismatch at pos %d\n", pos);
631 fprintf(stderr, "Should be: %d:%d, is: %d:%d\n", stack, scope,
632 stats->stack[pos].stackpos, stats->stack[pos].scopepos);
634 /* return error here if we do verification */
640 stats->stack[pos].seen = 1;
641 stats->stack[pos].stackpos = stack;
642 stats->stack[pos].scopepos = scope;
644 code_t*c = stats->stack[pos].code;
645 opcode_t*op = opcode_get(c->opcode);
647 //printf("Walking %s at position %d, stack=%d, scope=%d\n", op->name, pos, stack, scope);
649 stack += stack_minus(c);
652 fprintf(stderr, "error: stack underflow at %d (%s)\n", pos, op->name);
654 /* if we would do true verification (if we would be a vm), this is
655 where we would return the error
660 stack += op->stack_plus;
661 scope += op->scope_stack_plus;
663 if(stack > stats->maxstack)
664 stats->maxstack = stack;
665 if(scope > stats->maxscope)
666 stats->maxscope = scope;
668 if(op->flags & OP_SET_DXNS)
669 stats->flags |= FLAGS_SET_DXNS;
670 if(op->flags & OP_NEED_ACTIVATION)
671 stats->flags |= FLAGS_ACTIVATION;
673 if(c->opcode == OP_NEWCLASS) {
674 abc_class_t*cls = (abc_class_t*)(c->data[0]);
675 if(scope > cls->init_scope_depth)
676 cls->init_scope_depth = scope;
678 if(c->opcode == OP_NEWFUNCTION) {
679 abc_method_t*m = (abc_method_t*)(c->data[0]);
680 if(m->body && scope > m->body->init_scope_depth)
681 m->body->init_scope_depth = scope;
684 if(op->flags & OP_REGISTER) {
690 handleregister(stats, (ptroff_t)c->data[pos]);
696 handleregister(stats, c->opcode&3);
699 if(op->flags&OP_RETURN) {
700 if(OP_RETURN==0x48/*returnvalue*/) {
702 fprintf(stderr, "return(value) with stackposition %d\n", stack);
703 } else if(OP_RETURN==0x47) {
705 fprintf(stderr, "return(void) with stackposition %d\n", stack);
708 if(op->flags & (OP_THROW|OP_RETURN))
710 if(op->flags & OP_JUMP) {
712 fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d.\n", op->name, pos);
719 if(op->flags & OP_BRANCH) {
721 fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d\n", op->name, pos);
724 int newpos = c->branch->pos;
725 if(!callcode(stats, newpos, stack, scope))
728 if(op->flags & OP_LOOKUPSWITCH) {
729 lookupswitch_t*l = c->data[0];
731 fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d\n", op->name, pos);
734 if(!callcode(stats, l->def->pos, stack, scope))
736 code_list_t*t = l->targets;
739 fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d\n", op->name, pos);
742 if(!callcode(stats, t->code->pos, stack, scope))
750 assert(c->next == stats->stack[pos].code);
756 static currentstats_t* code_get_stats(code_t*code, exception_list_t*exceptions)
758 code = code_find_start(code);
765 currentstats_t* current = malloc(sizeof(currentstats_t));
766 current->stack = rfx_calloc(sizeof(stackpos_t)*num);
767 current->maxlocal = 0;
768 current->maxstack = 0;
769 current->maxscope = 0;
773 //#define DEBUG_BYTES
778 opcode_t*op = opcode_get(c->opcode);
779 if(op->flags & (OP_JUMP|OP_BRANCH)) {
780 printf("%5d) %s %08x\n", t, op->name, c->branch);
781 } else if(op->params[0]=='2') {
782 printf("%5d) %s %s\n", t, op->name, multiname_tostring(c->data[0]));
784 printf("%5d) %s\n", t, op->name);
788 //printf("%5d) %02x\n", t, tag->data[start+t]);
795 current->stack[num].code = c;
801 if(!callcode(current, 0, 0, 0)) {
805 exception_list_t*e = exceptions;
807 if(e->exception->target)
808 callcode(current, e->exception->target->pos, 1, 0);
815 void stats_free(currentstats_t*stats)
818 free(stats->stack);stats->stack=0;
823 int code_dump(code_t*c, exception_list_t*exceptions, abc_file_t*file, char*prefix, FILE*fo)
825 exception_list_t*e = exceptions;
826 c = code_find_start(c);
827 currentstats_t*stats = code_get_stats(c, exceptions);
831 U8 opcode = c->opcode;
833 opcode_t*op = opcode_get(opcode);
837 if(c==e->exception->from)
838 fprintf(fo, "%s TRY {\n", prefix);
839 if(c==e->exception->target) {
840 char*s1 = multiname_tostring(e->exception->exc_type);
841 char*s2 = multiname_tostring(e->exception->var_name);
842 fprintf(fo, "%s CATCH(%s %s)\n", prefix, s1, s2);
850 fprintf(stderr, "Can't parse opcode %02x.\n", opcode);
858 fprintf(fo, "%s%5d) %c %d:%d %s ", prefix, c->pos, stats->stack[c->pos].seen?'x':'|',
859 stats->stack[c->pos].stackpos,
860 stats->stack[c->pos].scopepos,
863 fprintf(fo, "%s%5d) ? ?:? %s ", prefix, c->pos, op->name);
867 void*data = c->data[i];
872 int n = (ptroff_t)data;
873 fprintf(fo, "%d params", n);
874 } else if(*p == '2') {
875 multiname_t*n = (multiname_t*)data;
876 char* m = multiname_tostring(n);
877 fprintf(fo, "%s", m);
879 } else if(*p == 'm') {
880 abc_method_t*m = (abc_method_t*)data;
881 fprintf(fo, "[method %s]", m->name);
882 } else if(*p == 'c') {
883 abc_class_t*cls = (abc_class_t*)data;
884 char*classname = multiname_tostring(cls->classname);
885 fprintf(fo, "[classinfo %s]", classname);
887 } else if(*p == 'i') {
888 abc_method_body_t*b = (abc_method_body_t*)data;
889 fprintf(fo, "[methodbody]");
890 } else if(*p == 'u' || *p == 'I' || *p == 'U') {
891 int n = (ptroff_t)data;
892 fprintf(fo, "%d", n);
893 } else if(*p == 'f') {
894 double f = *(double*)data;
895 fprintf(fo, "%f", f);
896 } else if(*p == 'r') {
897 int n = (ptroff_t)data;
898 fprintf(fo, "r%d", n);
899 } else if(*p == 'b') {
900 int b = (ptroff_t)data;
901 fprintf(fo, "%02x", b);
902 } else if(*p == 'j') {
904 fprintf(fo, "->%d", c->branch->pos);
906 fprintf(fo, "%08x", c->branch);
907 } else if(*p == 's') {
908 fprintf(fo, "\"%s\"", data);
909 } else if(*p == 'D') {
910 fprintf(fo, "[register %02x=%s]", (ptroff_t)c->data[1], (char*)c->data[0]);
911 } else if(*p == 'S') {
912 lookupswitch_t*l = c->data[0];
915 fprintf(fo, "default->%d", l->def->pos);
917 fprintf(fo, "default->00000000", l->def->pos);
918 code_list_t*t = l->targets;
921 fprintf(fo, ",->%d", t->code->pos);
923 fprintf(fo, ",->00000000");
928 fprintf(stderr, "Can't parse opcode param type \"%c\"\n", *p);
940 if(c==e->exception->to) {
941 if(e->exception->target)
942 fprintf(fo, "%s } // END TRY (HANDLER: %d)\n", prefix, e->exception->target->pos);
944 fprintf(fo, "%s } // END TRY (HANDLER: 00000000)\n", prefix);
956 codestats_t* code_get_statistics(code_t*code, exception_list_t*exceptions)
958 currentstats_t*current = code_get_stats(code, exceptions);
961 codestats_t*stats = rfx_calloc(sizeof(codestats_t));
962 stats->local_count = current->maxlocal;
963 stats->max_stack = current->maxstack;
964 stats->max_scope_depth = current->maxscope;
965 stats->flags = current->flags;
967 stats_free(current);current=0;
971 void codestats_free(codestats_t*s)
976 code_t* add_opcode(code_t*atag, U8 op)
978 code_t*tmp = (code_t*)rfx_calloc(sizeof(code_t));
983 tmp->next = atag->next;
991 void codestats_print(codestats_t*stats)
993 printf("max_stack: %d\n", stats->max_stack);
994 printf("local_count: %d\n", stats->local_count);
995 printf("scope_depth: %d\n", stats->max_scope_depth);
998 code_t* code_append(code_t*code, code_t*toappend)
1004 //find end of first list
1008 code_t*start=toappend;
1009 //and start of second list
1010 while(start->prev) {
1011 start = start->prev;