6 #define OP_STACK_ARGS 2
14 #define OP_LOOKUPSWITCH 512
15 #define OP_NEED_ACTIVATION 1024
16 #define OP_STACK_ARGS2 2048
18 /* 2 = u30 index into multiname
19 m = u30 index into method
20 n = u30 number of params
21 i = u30 index into method info
24 c = u30 index into class
25 s = string (u30 index into strings)
32 {0xa0, "add", "", -2, 1, 0, 0},
33 {0xc5, "add_i", "", -2, 1, 0, 0},
34 {0x86, "astype", "2", -1, 1, 0, 0},
35 {0x87, "astypelate", "", -2, 1, 0, 0},
36 {0xA8, "bitand", "", -2, 1, 0, 0},
37 {0x97, "bitnot", "", -1, 1, 0, 0},
38 {0xa9, "bitor", "", -2, 1, 0, 0},
39 {0xaa, "bitxor", "", -2, 1, 0, 0},
40 {0x41, "call", "n", -2, 1, 0, OP_STACK_ARGS},
41 {0x43, "callmethod", "mn", -1, 1, 0, OP_STACK_ARGS},
42 {0x4c, "callproplex", "2n", -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
43 {0x46, "callproperty", "2n", -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
44 {0x4f, "callpropvoid", "2n", -1, 0, 0, OP_STACK_ARGS|OP_STACK_NS},
45 {0x44, "callstatic", "in", -1, 1, 0, OP_STACK_ARGS},
46 {0x45, "callsuper", "2n", -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
47 {0x4e, "callsupervoid", "2n", -1, 0, 0, OP_STACK_ARGS|OP_STACK_NS},
48 {0x78, "checkfilter", "", -1, 1, 0, 0},
49 {0x80, "coerce", "2", -1, 1, 0, 0},
50 {0x82, "coerce_a", "", -1, 1, 0, 0},
51 {0x85, "coerce_s", "", -1, 1, 0, 0},
52 {0x42, "construct", "n", -1, 1, 0, OP_STACK_ARGS},
53 {0x4a, "constructprop", "2n", -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
54 {0x49, "constructsuper", "n", -1, 0, 0, OP_STACK_ARGS},
55 {0x76, "convert_b", "", -1, 1, 0, 0},
56 {0x73, "convert_i", "", -1, 1, 0, 0},
57 {0x75, "convert_d", "", -1, 1, 0, 0},
58 {0x77, "convert_o", "", -1, 1, 0, 0},
59 {0x74, "convert_u", "", -1, 1, 0, 0},
60 {0x70, "convert_s", "", -1, 1, 0, 0},
61 {0xef, "debug", "D", 0, 0, 0, 0},
62 {0xf1, "debugfile", "s", 0, 0, 0, 0},
63 {0xf0, "debugline", "u", 0, 0, 0, 0},
64 {0x94, "declocal", "r", 0, 0, 0, OP_REGISTER},
65 {0xc3, "declocal_i", "r", 0, 0, 0, OP_REGISTER},
66 {0x93, "decrement", "", -1, 1, 0, 0},
67 {0xc1, "decrement_i", "", -1, 1, 0, 0},
68 {0x6a, "deleteproperty", "2", -1, 1, 0, OP_STACK_NS},
69 {0xa3, "divide", "", -2, 1, 0, 0},
70 {0x2a, "dup", "", -1, 2, 0, 0},
71 {0x06, "dxns", "s", 0, 0, 0, OP_SET_DXNS},
72 {0x07, "dxnslate", "", -1, 0, 0, OP_SET_DXNS},
73 {0xab, "equals", "", -2, 1, 0, 0},
74 {0x72, "esc_xattr", "", -1, 1, 0, 0},
75 {0x71, "esc_xelem", "", -1, 1, 0, 0},
76 {0x5e, "findproperty", "2", 0, 1, 0, OP_STACK_NS},
77 {0x5d, "findpropstrict", "2", 0, 1, 0, OP_STACK_NS},
78 {0x59, "getdescendants", "2", -1, 1, 0, OP_STACK_NS},
79 {0x64, "getglobalscope", "", 0, 1, 0, 0},
80 {0x6e, "getglobalslot", "u", 0, 1, 0, 0},
81 {0x60, "getlex", "2", 0, 1, 0, 0}, //multiname may not be runtime
82 {0x62, "getlocal", "r", 0, 1, 0, OP_REGISTER},
83 {0xd0, "getlocal_0", "", 0, 1, 0, OP_REGISTER},
84 {0xd1, "getlocal_1", "", 0, 1, 0, OP_REGISTER},
85 {0xd2, "getlocal_2", "", 0, 1, 0, OP_REGISTER},
86 {0xd3, "getlocal_3", "", 0, 1, 0, OP_REGISTER},
87 {0x66, "getproperty", "2", -1, 1, 0, OP_STACK_NS},
88 {0x65, "getscopeobject", "u", 0, 1, 0, 0}, // u = index into scope stack
89 {0x6c, "getslot", "u", -1, 1, 0, 0},
90 {0x04, "getsuper", "2", -1, 1, 0, OP_STACK_NS},
91 {0xaf, "greaterthan", "", -2, 1, 0, 0},
92 {0xb0, "greaterequals", "", -2, 1, 0, 0},
93 {0x1f, "hasnext", "", -2, 1, 0, 0},
94 {0x32, "hasnext2", "rr", 0, 1, 0, OP_REGISTER},
95 {0x13, "ifeq", "j", -2, 0, 0, OP_BRANCH},
96 {0x12, "iffalse", "j", -1, 0, 0, OP_BRANCH},
97 {0x18, "ifge", "j", -2, 0, 0, OP_BRANCH},
98 {0x17, "ifgt", "j", -2, 0, 0, OP_BRANCH},
99 {0x16, "ifle", "j", -2, 0, 0, OP_BRANCH},
100 {0x15, "iflt", "j", -2, 0, 0, OP_BRANCH},
101 {0x0f, "ifnge", "j", -2, 0, 0, OP_BRANCH},
102 {0x0e, "ifngt", "j", -2, 0, 0, OP_BRANCH},
103 {0x0d, "ifnle", "j", -2, 0, 0, OP_BRANCH},
104 {0x0c, "ifnlt", "j", -2, 0, 0, OP_BRANCH},
105 {0x14, "ifne", "j", -2, 0, 0, OP_BRANCH},
106 {0x19, "ifstricteq", "j", -2, 0, 0, OP_BRANCH},
107 {0x1a, "ifstrictne", "j", -2, 0, 0, OP_BRANCH},
108 {0x11, "iftrue", "j", -1, 0, 0, OP_BRANCH},
109 {0xb4, "in", "", -2, 1, 0, 0},
110 {0x92, "inclocal", "r", 0, 0, 0, OP_REGISTER},
111 {0xc2, "inclocal_i", "r", 0, 0, 0, OP_REGISTER},
112 {0x91, "increment", "", -1, 1, 0, 0},
113 {0xc0, "increment_i", "", -1, 1, 0, 0},
114 {0x68, "initproperty", "2", -2, 0, 0, OP_STACK_NS},
115 {0xb1, "instanceof", "", -2, 1, 0, 0},
116 {0xb2, "istype", "2", -1, 1, 0, 0}, // may not be a runtime multiname
117 {0xb3, "istypelate", "", -2, 1, 0, 0},
118 {0x10, "jump", "j", 0, 0, 0, OP_JUMP},
119 {0x08, "kill", "r", 0, 0, 0, OP_REGISTER},
120 {0x09, "label", "", 0, 0, 0, OP_LABEL},
121 {0xae, "lessequals", "", -2, 1, 0, 0},
122 {0xad, "lessthan", "", -2, 1, 0, 0},
123 {0x1b, "lookupswitch", "S", -1, 0, 0, OP_LOOKUPSWITCH},
124 {0xa5, "lshift", "", -2, 1, 0, 0},
125 {0xa4, "modulo", "", -2, 1, 0, 0},
126 {0xa2, "multiply", "", -2, 1, 0, 0},
127 {0xc7, "multiply_i", "", -2, 1, 0, 0},
128 {0x90, "negate", "", -1, 1, 0, 0},
129 {0xc4, "negate_i", "", -1, 1, 0, 0},
130 {0x57, "newactivation", "", 0, 1, 0, OP_NEED_ACTIVATION},
131 {0x56, "newarray", "n", 0, 1, 0, OP_STACK_ARGS},
132 {0x5a, "newcatch", "u", 0, 1, 0, 0}, //u = index into exception_info
133 {0x58, "newclass", "c", -1, 1, 0, 0}, //c = index into class_info
134 {0x40, "newfunction", "m", 0, 1, 0, 0}, //i = index into method_info
135 {0x55, "newobject", "n", 0, 1, 0, OP_STACK_ARGS2},
136 {0x1e, "nextname", "", -2, 1, 0, 0},
137 {0x23, "nextvalue", "", -2, 1, 0, 0},
138 {0x02, "nop", "", 0, 0, 0, 0},
139 {0x96, "not", "", -1, 1, 0 ,0},
140 {0x29, "pop", "", -1, 0, 0, 0},
141 {0x1d, "popscope", "", 0, 0,-1, 0},
142 {0x24, "pushbyte", "b", 0, 1, 0, 0},
143 {0x2f, "pushdouble", "u", 0, 1, 0, 0}, //index into floats
144 {0x27, "pushfalse", "", 0, 1, 0, 0},
145 {0x2d, "pushint", "u", 0, 1, 0, 0}, //index into ints
146 {0x31, "pushnamespace", "u", 0, 1, 0, 0}, //index into namespace
147 {0x28, "pushnan", "", 0, 1, 0, 0},
148 {0x20, "pushnull", "", 0, 1, 0, 0},
149 {0x30, "pushscope", "", -1, 0, 1, 0},
150 {0x25, "pushshort", "u", 0, 1, 0, 0},
151 {0x2c, "pushstring", "s", 0, 1, 0, 0},
152 {0x26, "pushtrue", "", 0, 1, 0, 0},
153 {0x2e, "pushuint", "u", 0, 1, 0, 0}, //index into uints
154 {0x21, "pushundefined", "", 0, 1, 0, 0},
155 {0x1c, "pushwith", "", -1, 0, 1, 0},
156 {0x48, "returnvalue", "", -1, 0, 0, OP_RETURN},
157 {0x47, "returnvoid", "", 0, 0, 0, OP_RETURN},
158 {0xa6, "rshift", "", -2, 1, 0, 0},
159 {0x63, "setlocal", "r", -1, 0, 0, OP_REGISTER},
160 {0xd4, "setlocal_0", "", -1, 0, 0, OP_REGISTER},
161 {0xd5, "setlocal_1", "", -1, 0, 0, OP_REGISTER},
162 {0xd6, "setlocal_2", "", -1, 0, 0, OP_REGISTER},
163 {0xd7, "setlocal_3", "", -1, 0, 0, OP_REGISTER},
164 {0x6f, "setglobalslot", "u", -1, 0, 0, 0},
165 {0x61, "setproperty", "2", -2, 0, 0, OP_STACK_NS},
166 {0x6d, "setslot", "2", -2, 0, 0, 0},
167 {0x05, "setsuper", "2", -2, 0, 0, OP_STACK_NS},
168 {0xac, "strictequals", "", -2, 1, 0, 0},
169 {0xa1, "subtract", "", -2, 1, 0, 0},
170 {0xc6, "subtract_i", "", -2, 1, 0, 0},
171 {0x2b, "swap", "", -2, 2, 0, 0},
172 {0x03, "throw", "", -1, 0, 0, OP_THROW},
173 {0x95, "typeof", "", -1, 1, 0, 0},
174 {0xa7, "urshift", "", -2, 1, 0, 0},
177 static U8 op2index[256] = {254};
179 opcode_t* opcode_get(U8 op)
182 if(op2index[0]==254) {
183 memset(op2index, 255, sizeof(op2index));
184 for(t=0;t<sizeof(opcodes)/sizeof(opcodes[0]);t++) {
185 op2index[opcodes[t].opcode] = t;
188 if(op2index[op]!=255)
189 return &opcodes[op2index[op]];
193 static code_t*pos2code(code_t**bytepos, code_t*c, int pos, int len)
200 (pos!=len && !bytepos[pos])) {
201 /* flex likes to generate these. yuck. */
203 opcode_t*op = opcode_get(c->opcode);
204 fprintf(stderr, "Warning: Invalid jump instruction \"%s\" from %d to %d (%d)\n", op->name, c->pos, pos, len);
206 fprintf(stderr, "Warning: Invalid jump to %d (%d)\n", pos, len);
211 //opcode_t*op = opcode_get(c->opcode);
212 //fprintf(stderr, "Warning: jump beyond end of code in instruction %s at position %d\n", op->name, c->pos);
219 code_t* code_atposition(codelookup_t*l, int pos)
221 return pos2code(l->bytepos, 0, pos, l->len);
224 void lookupswitch_print(lookupswitch_t*l)
226 printf("default: %08x\n", l->def);
227 code_list_t*t = l->targets;
229 printf("target: %08x\n", t->code);
234 code_t*code_parse(TAG*tag, int len, abc_file_t*file, pool_t*pool, codelookup_t**codelookup)
239 int end=tag->pos+len;
240 //printf("-->\n");fflush(stdout);
242 code_t**bytepos = rfx_calloc(sizeof(code_t*)*len);
244 while(tag->pos<end) {
245 int codepos = tag->pos-start;
246 U8 opcode = swf_GetU8(tag);
247 opcode_t*op = opcode_get(opcode);
249 fprintf(stderr, "Can't parse opcode %02x\n", opcode);
252 //printf("%s\n", op->name);fflush(stdout);
256 bytepos[codepos] = c;
271 if(*p == 'n') { // number
272 data = (void*)(ptroff_t)swf_GetU30(tag);
273 } else if(*p == '2') { //multiname
274 data = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
275 } else if(*p == 'm') { //method
276 data = array_getvalue(file->methods, swf_GetU30(tag));
277 } else if(*p == 'c') { //classinfo
278 data = array_getvalue(file->classes, swf_GetU30(tag));
279 } else if(*p == 'i') {
280 data = array_getvalue(file->method_bodies, swf_GetU30(tag));
281 } else if(*p == 'u') { // generic integer
282 data = (void*)(ptroff_t)swf_GetU30(tag);
283 } else if(*p == 'r') { // local register
284 data = (void*)(ptroff_t)swf_GetU30(tag);
285 } else if(*p == 'b') { // byte
286 data = (void*)(ptroff_t)swf_GetU8(tag);
287 } else if(*p == 'j') { // jump
288 int j = swf_GetS24(tag);
289 data = (void*)(ptroff_t)j;
290 } else if(*p == 's') { // string
291 data = strdup((char*)pool_lookup_string(pool, swf_GetU30(tag)));
292 } else if(*p == 'D') { // debug
294 U8 type = swf_GetU8(tag);
296 fprintf(stderr, "Unknown debug type: %02x\n", type);
298 code->data[0] = strdup((char*)pool_lookup_string(pool, swf_GetU30(tag)));
300 code->data[1] = (void*)(ptroff_t)swf_GetU8(tag);
303 } else if(*p == 'S') { // switch statement
304 lookupswitch_t*l = malloc(sizeof(lookupswitch_t));
305 l->def = (code_t*)(ptroff_t)swf_GetS24(tag);
306 l->targets = list_new();
307 int num = swf_GetU30(tag)+1;
310 list_append(l->targets, (code_t*)(ptroff_t)swf_GetS24(tag));
313 printf("Can't parse opcode param type \"%c\" (for op %02x %s).\n", *p, code->opcode, op->name);
317 code->data[pos++] = data;
322 //#define DEBUG_BYTES
326 code_t*c = bytepos[t];
328 opcode_t*op = opcode_get(c->opcode);
329 if(op->flags & (OP_JUMP|OP_BRANCH)) {
330 printf("%5d) %02x %s %d\n", t, tag->data[start+t], op->name, c->data[0]);
332 printf("%5d) %02x %s\n", t, tag->data[start+t], op->name);
335 printf("%5d) %02x\n", t, tag->data[start+t]);
338 //printf("%5d) %02x\n", t, tag->data[start+t]);
343 opcode_t*op = opcode_get(c->opcode);
344 if(op->flags & (OP_JUMP|OP_BRANCH)) {
345 int j = ((int)(ptroff_t)c->data[0]);
346 c->branch = pos2code(bytepos,c,j+4,len);
347 } else if(op->flags & (OP_LOOKUPSWITCH)) {
348 lookupswitch_t*l = (lookupswitch_t*)c->data[0];
350 l->def = pos2code(bytepos,c,(ptroff_t)l->def+offset,len);
351 code_list_t*t=l->targets;
353 t->code = pos2code(bytepos,c,(ptroff_t)t->code+offset,len);
361 (*codelookup) = malloc(sizeof(codelookup_t));
362 (*codelookup)->bytepos = bytepos;
363 (*codelookup)->len = len;
371 void codelookup_free(codelookup_t*codelookup)
373 free(codelookup->bytepos);codelookup->bytepos=0;
377 code_t*code_find_start(code_t*c)
384 void code_free(code_t*c)
386 c = code_find_start(c);
388 code_t*next = c->next;
389 opcode_t*op = opcode_get(c->opcode);
390 char*p = op?op->params:"";
393 void*data = c->data[pos];
394 if(*p == '2') { //multiname
395 multiname_destroy(data);
396 } else if(strchr("sD", *p)) {
398 } else if(strchr("S", *p)) {
399 lookupswitch_t*l = (lookupswitch_t*)data;
400 list_free(l->targets);l->targets=0;
406 memset(c, 0, sizeof(c));
412 static int opcode_write(TAG*tag, code_t*c, pool_t*pool, abc_file_t*file, int length)
414 opcode_t*op = opcode_get(c->opcode);
420 swf_SetU8(tag, c->opcode);
424 void*data = c->data[pos++];
426 if(*p == 'n') { // number
427 len += swf_SetU30(tag, (ptroff_t)data);
428 } else if(*p == '2') { //multiname
429 multiname_t*m = (multiname_t*)data;
430 len += swf_SetU30(tag, pool_register_multiname(pool, m));
431 } else if(*p == 'm') { //method
432 abc_method_t*m = (abc_method_t*)data;
433 len += swf_SetU30(tag, m->index);
434 } else if(*p == 'c') { //classinfo
435 abc_class_t*cls = (abc_class_t*)data;
436 len += swf_SetU30(tag, cls->index);
437 } else if(*p == 'i') { //methodbody
438 abc_method_body_t*m = (abc_method_body_t*)data;
439 len += swf_SetU30(tag, m->index);
440 } else if(*p == 'u') { // integer
441 len += swf_SetU30(tag, (ptroff_t)data);
442 } else if(*p == 'r') { // integer
443 len += swf_SetU30(tag, (ptroff_t)data);
444 } else if(*p == 'b') { // byte
446 swf_SetU8(tag, (ptroff_t)data);
448 } else if(*p == 'j') { // jump
449 int skip = length-c->pos-4;
451 skip = (c->branch->pos) - c->pos - 4;
452 len += swf_SetS24(tag, skip);
453 } else if(*p == 's') { // string
454 int index = pool_register_string(pool, data);
455 len += swf_SetU30(tag, index);
456 } else if(*p == 'D') { // debug statement
460 len+=swf_SetU30(tag, pool_register_string(pool,c->data[0]));
462 swf_SetU8(tag, (ptroff_t)c->data[1]);
464 len+=swf_SetU30(tag, 0);
465 } else if(*p == 'S') { // switch statement
466 lookupswitch_t*l = (lookupswitch_t*)data;
468 len+=swf_SetS24(tag, l->def->pos-c->pos+offset); //default
469 code_list_t*t = l->targets;
471 len+=swf_SetU30(tag, list_length(t)-1); //nr-1
472 code_list_t*t = l->targets;
474 len+=swf_SetS24(tag, t->code->pos - c->pos+offset);
478 len+=swf_SetU30(tag, 0); //nr-1
479 len+=swf_SetS24(tag, l->def->pos-c->pos+offset);
482 printf("Can't parse opcode param type \"%c\"\n", *p);
489 void code_write(TAG*tag, code_t*code, pool_t*pool, abc_file_t*file)
491 code = code_find_start(code);
497 pos += opcode_write(0, c, pool, file, 0);
501 swf_SetU30(tag, pos);
502 int start = tag->len;
506 opcode_t*op = opcode_get(code->opcode);
507 if(op->flags&(OP_BRANCH|OP_JUMP)) {
510 pos += opcode_write(tag, c, pool, file, length);
513 assert(tag->len - start == pos);
531 static int stack_minus(code_t*c)
533 opcode_t*op = opcode_get(c->opcode);
534 if(op->stack_minus>0) {
535 fprintf(stderr, "Invalid opcode entry %02x %s\n", c->opcode, op->name);
537 int stack = op->stack_minus;
538 if(op->flags&OP_STACK_NS) {
539 multiname_t*m = (multiname_t*)c->data[0];
540 if(multiname_late_namespace(m))
542 if(multiname_late_name(m))
545 if(op->flags&OP_STACK_ARGS || op->flags&OP_STACK_ARGS2) {
546 assert(strchr(op->params, 'n'));
547 int nr = (ptroff_t)(op->params[0]=='n'?c->data[0]:c->data[1]);
549 if(op->flags&OP_STACK_ARGS2)
554 static void handleregister(currentstats_t*stats, int reg)
556 if(reg+1 > stats->maxlocal)
557 stats->maxlocal = reg+1;
560 static void dumpstack(currentstats_t*stats)
563 for(t=0;t<stats->num;t++) {
564 code_t*c = stats->stack[t].code;
565 opcode_t*op = opcode_get(c->opcode);
566 printf("%5d) %c %d:%d %s", t, stats->stack[t].seen?'x':'|',
567 stats->stack[t].stackpos,
568 stats->stack[t].scopepos,
571 if(op->flags&(OP_BRANCH|OP_JUMP)) {
573 printf(" ->%d\n", c->branch->pos);
575 printf(" 00000000\n");
577 if(op->params[0]=='2') {
578 printf(" %s", multiname_to_string(c->data[0]));
584 static char callcode(currentstats_t*stats, int pos, int stack, int scope)
586 while(pos<stats->num) {
587 if(stats->stack[pos].seen) {
588 if(stats->stack[pos].stackpos != stack ||
589 stats->stack[pos].scopepos != scope) {
591 fprintf(stderr, "Stack mismatch at pos %d\n", pos);
592 fprintf(stderr, "Should be: %d:%d, is: %d:%d\n", stack, scope,
593 stats->stack[pos].stackpos, stats->stack[pos].scopepos);
599 stats->stack[pos].seen = 1;
600 stats->stack[pos].stackpos = stack;
601 stats->stack[pos].scopepos = scope;
603 code_t*c = stats->stack[pos].code;
604 opcode_t*op = opcode_get(c->opcode);
606 //printf("Walking %s at position %d, stack=%d, scope=%d\n", op->name, pos, stack, scope);
608 stack += stack_minus(c);
611 fprintf(stderr, "error: stack underflow at %d (%s)\n", pos, op->name);
612 printf("error: stack underflow at %d (%s)\n", pos, op->name);exit(0);
616 stack += op->stack_plus;
617 scope += op->scope_stack_plus;
619 if(stack > stats->maxstack)
620 stats->maxstack = stack;
621 if(scope > stats->maxscope)
622 stats->maxscope = scope;
624 if(op->flags & OP_REGISTER) {
630 handleregister(stats, (ptroff_t)c->data[pos]);
636 handleregister(stats, c->opcode&3);
639 if(op->flags&OP_RETURN) {
640 if(OP_RETURN==0x48/*returnvalue*/) {
642 fprintf(stderr, "return(value) with stackposition %d\n", stack);
643 } else if(OP_RETURN==0x47) {
645 fprintf(stderr, "return(void) with stackposition %d\n", stack);
648 if(op->flags & (OP_THROW|OP_RETURN))
650 if(op->flags & OP_JUMP) {
652 fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d.\n", op->name, pos);
659 if(op->flags & OP_BRANCH) {
661 fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d\n", op->name, pos);
664 int newpos = c->branch->pos;
665 if(!callcode(stats, newpos, stack, scope))
668 if(op->flags & OP_LOOKUPSWITCH) {
669 lookupswitch_t*l = c->data[0];
671 fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d\n", op->name, pos);
674 if(!callcode(stats, l->def->pos, stack, scope))
676 code_list_t*t = l->targets;
679 fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d\n", op->name, pos);
682 if(!callcode(stats, t->code->pos, stack, scope))
690 assert(c->next == stats->stack[pos].code);
696 static currentstats_t* code_get_stats(code_t*code, exception_list_t*exceptions)
698 code = code_find_start(code);
705 currentstats_t* current = malloc(sizeof(currentstats_t));
706 current->stack = rfx_calloc(sizeof(stackpos_t)*num);
707 current->maxlocal = 0;
708 current->maxstack = 0;
709 current->maxscope = 0;
712 //#define DEBUG_BYTES
717 opcode_t*op = opcode_get(c->opcode);
718 if(op->flags & (OP_JUMP|OP_BRANCH)) {
719 printf("%5d) %s %08x\n", t, op->name, c->branch);
720 } else if(op->params[0]=='2') {
721 printf("%5d) %s %s\n", t, op->name, multiname_to_string(c->data[0]));
723 printf("%5d) %s\n", t, op->name);
727 //printf("%5d) %02x\n", t, tag->data[start+t]);
734 current->stack[num].code = c;
740 if(!callcode(current, 0, 0, 0)) {
744 exception_list_t*e = exceptions;
746 if(e->exception->target)
747 callcode(current, e->exception->target->pos, 1, 0);
754 void stats_free(currentstats_t*stats)
756 free(stats->stack);stats->stack=0;
760 int code_dump(code_t*c, exception_list_t*exceptions, abc_file_t*file, char*prefix, FILE*fo)
762 exception_list_t*e = exceptions;
763 c = code_find_start(c);
764 currentstats_t*stats = code_get_stats(c, exceptions);
768 U8 opcode = c->opcode;
770 opcode_t*op = opcode_get(opcode);
774 if(c==e->exception->from)
775 fprintf(fo, "%s TRY {\n", prefix);
776 if(c==e->exception->target) {
777 char*s1 = multiname_to_string(e->exception->exc_type);
778 char*s2 = multiname_to_string(e->exception->var_name);
779 fprintf(fo, "%s CATCH(%s %s)\n", prefix, s1, s2);
787 fprintf(stderr, "Can't parse opcode %02x.\n", opcode);
794 fprintf(fo, "%s%5d) %c %d:%d %s ", prefix, c->pos, stats->stack[c->pos].seen?'x':'|',
795 stats->stack[c->pos].stackpos,
796 stats->stack[c->pos].scopepos,
800 void*data = c->data[i];
805 int n = (ptroff_t)data;
806 fprintf(fo, "%d params", n);
807 } else if(*p == '2') {
808 multiname_t*n = (multiname_t*)data;
809 char* m = multiname_to_string(n);
810 fprintf(fo, "%s", m);
812 } else if(*p == 'm') {
813 abc_method_t*m = (abc_method_t*)data;
814 fprintf(fo, "[method %s]", m->name);
815 } else if(*p == 'c') {
816 abc_class_t*cls = (abc_class_t*)data;
817 char*classname = multiname_to_string(cls->classname);
818 fprintf(fo, "[classinfo %s]", classname);
820 } else if(*p == 'i') {
821 abc_method_body_t*b = (abc_method_body_t*)data;
822 fprintf(fo, "[methodbody]");
823 } else if(*p == 'u') {
824 int n = (ptroff_t)data;
825 fprintf(fo, "%d", n);
826 } else if(*p == 'r') {
827 int n = (ptroff_t)data;
828 fprintf(fo, "r%d", n);
829 } else if(*p == 'b') {
830 int b = (ptroff_t)data;
831 fprintf(fo, "%02x", b);
832 } else if(*p == 'j') {
834 fprintf(fo, "->%d", c->branch->pos);
836 fprintf(fo, "%08x", c->branch);
837 } else if(*p == 's') {
838 fprintf(fo, "\"%s\"", data);
839 } else if(*p == 'D') {
840 fprintf(fo, "[register %02x=%s]", (ptroff_t)c->data[1], (char*)c->data[0]);
841 } else if(*p == 'S') {
842 lookupswitch_t*l = c->data[0];
845 fprintf(fo, "default->%d", l->def->pos);
847 fprintf(fo, "default->00000000", l->def->pos);
848 code_list_t*t = l->targets;
851 fprintf(fo, ",->%d", t->code->pos);
853 fprintf(fo, ",->00000000");
858 fprintf(stderr, "Can't parse opcode param type \"%c\"\n", *p);
870 if(c==e->exception->to) {
871 if(e->exception->target)
872 fprintf(fo, "%s } // END TRY (HANDLER: %d)\n", prefix, e->exception->target->pos);
874 fprintf(fo, "%s } // END TRY (HANDLER: 00000000)\n", prefix);
886 codestats_t* code_get_statistics(code_t*code, exception_list_t*exceptions)
888 currentstats_t*current = code_get_stats(code, exceptions);
891 codestats_t*stats = malloc(sizeof(codestats_t));
892 stats->local_count = current->maxlocal;
893 stats->max_stack = current->maxstack;
894 stats->init_scope_depth = 0;
895 stats->max_scope_depth = current->maxscope;
897 stats_free(current);current=0;
901 void codestats_free(codestats_t*s)
906 code_t* add_opcode(code_t*atag, U8 op)
908 code_t*tmp = (code_t*)rfx_calloc(sizeof(code_t));
920 void codestats_print(codestats_t*stats)
922 printf("max_stack: %d\n", stats->max_stack);
923 printf("local_count: %d\n", stats->local_count);
924 printf("init_scope_depth: %d\n", stats->init_scope_depth);
925 printf("max_scope_depth: %d\n", stats->max_scope_depth);