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
40 #define OP_INTERNAL 32768
42 /* 2 = u30 index into multiname
43 m = u30 index into method
44 n = u30 number of params
45 i = u30 index into method info
47 c = u30 index into class
48 s = string (u30 index into strings)
55 {0xa0, "add", "", -2, 1, 0, 0},
56 {0xc5, "add_i", "", -2, 1, 0, 0},
57 {0x86, "astype", "2", -1, 1, 0, 0},
58 {0x87, "astypelate", "", -2, 1, 0, 0},
59 {0xA8, "bitand", "", -2, 1, 0, 0},
60 {0x97, "bitnot", "", -1, 1, 0, 0},
61 {0xa9, "bitor", "", -2, 1, 0, 0},
62 {0xaa, "bitxor", "", -2, 1, 0, 0},
63 {0x41, "call", "n", -2, 1, 0, OP_STACK_ARGS},
64 {0x43, "callmethod", "mn", -1, 1, 0, OP_STACK_ARGS},
65 {0x4c, "callproplex", "2n", -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
66 {0x46, "callproperty", "2n", -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
67 {0x4f, "callpropvoid", "2n", -1, 0, 0, OP_STACK_ARGS|OP_STACK_NS},
68 {0x44, "callstatic", "in", -1, 1, 0, OP_STACK_ARGS},
69 {0x45, "callsuper", "2n", -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
70 {0x4e, "callsupervoid", "2n", -1, 0, 0, OP_STACK_ARGS|OP_STACK_NS},
71 {0x78, "checkfilter", "", -1, 1, 0, 0},
72 {0x80, "coerce", "2", -1, 1, 0, 0},
73 {0x82, "coerce_a", "", -1, 1, 0, 0},
74 {0x85, "coerce_s", "", -1, 1, 0, 0},
75 {0x42, "construct", "n", -1, 1, 0, OP_STACK_ARGS},
76 {0x4a, "constructprop", "2n", -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
77 {0x49, "constructsuper", "n", -1, 0, 0, OP_STACK_ARGS},
78 {0x76, "convert_b", "", -1, 1, 0, 0},
79 {0x73, "convert_i", "", -1, 1, 0, 0},
80 {0x75, "convert_d", "", -1, 1, 0, 0},
81 {0x77, "convert_o", "", -1, 1, 0, 0},
82 {0x74, "convert_u", "", -1, 1, 0, 0},
83 {0x70, "convert_s", "", -1, 1, 0, 0},
84 {0xef, "debug", "D", 0, 0, 0, 0},
85 {0xf1, "debugfile", "s", 0, 0, 0, 0},
86 {0xf0, "debugline", "u", 0, 0, 0, 0},
87 {0x94, "declocal", "r", 0, 0, 0, OP_REGISTER},
88 {0xc3, "declocal_i", "r", 0, 0, 0, OP_REGISTER},
89 {0x93, "decrement", "", -1, 1, 0, 0},
90 {0xc1, "decrement_i", "", -1, 1, 0, 0},
91 {0x6a, "deleteproperty", "2", -1, 1, 0, OP_STACK_NS},
92 {0xa3, "divide", "", -2, 1, 0, 0},
93 {0x2a, "dup", "", -1, 2, 0, 0},
94 {0x06, "dxns", "s", 0, 0, 0, OP_SET_DXNS},
95 {0x07, "dxnslate", "", -1, 0, 0, OP_SET_DXNS},
96 {0xab, "equals", "", -2, 1, 0, 0},
97 {0x72, "esc_xattr", "", -1, 1, 0, 0},
98 {0x71, "esc_xelem", "", -1, 1, 0, 0},
99 {0x5e, "findproperty", "2", 0, 1, 0, OP_STACK_NS},
100 {0x5d, "findpropstrict", "2", 0, 1, 0, OP_STACK_NS},
101 {0x59, "getdescendants", "2", -1, 1, 0, OP_STACK_NS},
102 {0x64, "getglobalscope", "", 0, 1, 0, 0},
103 {0x6e, "getglobalslot", "u", 0, 1, 0, 0},
104 {0x60, "getlex", "2", 0, 1, 0, 0}, //multiname may not be runtime
105 {0x62, "getlocal", "r", 0, 1, 0, OP_REGISTER},
106 {0xd0, "getlocal_0", "", 0, 1, 0, OP_REGISTER},
107 {0xd1, "getlocal_1", "", 0, 1, 0, OP_REGISTER},
108 {0xd2, "getlocal_2", "", 0, 1, 0, OP_REGISTER},
109 {0xd3, "getlocal_3", "", 0, 1, 0, OP_REGISTER},
110 {0x66, "getproperty", "2", -1, 1, 0, OP_STACK_NS},
111 {0x65, "getscopeobject", "u", 0, 1, 0, 0}, // u = index into scope stack
112 {0x6c, "getslot", "u", -1, 1, 0, 0},
113 {0x04, "getsuper", "2", -1, 1, 0, OP_STACK_NS},
114 {0xaf, "greaterthan", "", -2, 1, 0, 0},
115 {0xb0, "greaterequals", "", -2, 1, 0, 0},
116 {0x1f, "hasnext", "", -2, 1, 0, 0},
117 {0x32, "hasnext2", "rr", 0, 1, 0, OP_REGISTER},
118 {0x13, "ifeq", "j", -2, 0, 0, OP_BRANCH},
119 {0x12, "iffalse", "j", -1, 0, 0, OP_BRANCH},
120 {0x18, "ifge", "j", -2, 0, 0, OP_BRANCH},
121 {0x17, "ifgt", "j", -2, 0, 0, OP_BRANCH},
122 {0x16, "ifle", "j", -2, 0, 0, OP_BRANCH},
123 {0x15, "iflt", "j", -2, 0, 0, OP_BRANCH},
124 {0x0f, "ifnge", "j", -2, 0, 0, OP_BRANCH},
125 {0x0e, "ifngt", "j", -2, 0, 0, OP_BRANCH},
126 {0x0d, "ifnle", "j", -2, 0, 0, OP_BRANCH},
127 {0x0c, "ifnlt", "j", -2, 0, 0, OP_BRANCH},
128 {0x14, "ifne", "j", -2, 0, 0, OP_BRANCH},
129 {0x19, "ifstricteq", "j", -2, 0, 0, OP_BRANCH},
130 {0x1a, "ifstrictne", "j", -2, 0, 0, OP_BRANCH},
131 {0x11, "iftrue", "j", -1, 0, 0, OP_BRANCH},
132 {0xb4, "in", "", -2, 1, 0, 0},
133 {0x92, "inclocal", "r", 0, 0, 0, OP_REGISTER},
134 {0xc2, "inclocal_i", "r", 0, 0, 0, OP_REGISTER},
135 {0x91, "increment", "", -1, 1, 0, 0},
136 {0xc0, "increment_i", "", -1, 1, 0, 0},
137 {0x68, "initproperty", "2", -2, 0, 0, OP_STACK_NS},
138 {0xb1, "instanceof", "", -2, 1, 0, 0},
139 {0xb2, "istype", "2", -1, 1, 0, 0}, // may not be a runtime multiname
140 {0xb3, "istypelate", "", -2, 1, 0, 0},
141 {0x10, "jump", "j", 0, 0, 0, OP_JUMP},
142 {0x08, "kill", "r", 0, 0, 0, OP_REGISTER},
143 {0x09, "label", "", 0, 0, 0, OP_LABEL},
144 {0xae, "lessequals", "", -2, 1, 0, 0},
145 {0xad, "lessthan", "", -2, 1, 0, 0},
146 {0x1b, "lookupswitch", "S", -1, 0, 0, OP_LOOKUPSWITCH},
147 {0xa5, "lshift", "", -2, 1, 0, 0},
148 {0xa4, "modulo", "", -2, 1, 0, 0},
149 {0xa2, "multiply", "", -2, 1, 0, 0},
150 {0xc7, "multiply_i", "", -2, 1, 0, 0},
151 {0x90, "negate", "", -1, 1, 0, 0},
152 {0xc4, "negate_i", "", -1, 1, 0, 0},
153 {0x57, "newactivation", "", 0, 1, 0, OP_NEED_ACTIVATION},
154 {0x56, "newarray", "n", 0, 1, 0, OP_STACK_ARGS},
155 {0x5a, "newcatch", "u", 0, 1, 0, 0}, //u = index into exception_info
156 {0x58, "newclass", "c", -1, 1, 0, 0}, //c = index into class_info
157 {0x40, "newfunction", "m", 0, 1, 0, 0}, //i = index into method_info
158 {0x55, "newobject", "n", 0, 1, 0, OP_STACK_ARGS2},
159 {0x1e, "nextname", "", -2, 1, 0, 0},
160 {0x23, "nextvalue", "", -2, 1, 0, 0},
161 {0x02, "nop", "", 0, 0, 0, 0},
162 {0x96, "not", "", -1, 1, 0 ,0},
163 {0x29, "pop", "", -1, 0, 0, 0},
164 {0x1d, "popscope", "", 0, 0,-1, 0},
165 {0x24, "pushbyte", "b", 0, 1, 0, 0},
166 {0x2f, "pushdouble", "f", 0, 1, 0, 0}, //index into floats
167 {0x27, "pushfalse", "", 0, 1, 0, 0},
168 {0x2d, "pushint", "I", 0, 1, 0, 0}, //index into ints
169 {0x31, "pushnamespace", "u", 0, 1, 0, 0}, //index into namespace TODO
170 {0x28, "pushnan", "", 0, 1, 0, 0},
171 {0x20, "pushnull", "", 0, 1, 0, 0},
172 {0x30, "pushscope", "", -1, 0, 1, 0},
173 {0x25, "pushshort", "u", 0, 1, 0, 0},
174 {0x2c, "pushstring", "s", 0, 1, 0, 0},
175 {0x26, "pushtrue", "", 0, 1, 0, 0},
176 {0x2e, "pushuint", "U", 0, 1, 0, 0}, //index into uints
177 {0x21, "pushundefined", "", 0, 1, 0, 0},
178 {0x1c, "pushwith", "", -1, 0, 1, 0},
179 {0x48, "returnvalue", "", -1, 0, 0, OP_RETURN},
180 {0x47, "returnvoid", "", 0, 0, 0, OP_RETURN},
181 {0xa6, "rshift", "", -2, 1, 0, 0},
182 {0x63, "setlocal", "r", -1, 0, 0, OP_REGISTER},
183 {0xd4, "setlocal_0", "", -1, 0, 0, OP_REGISTER},
184 {0xd5, "setlocal_1", "", -1, 0, 0, OP_REGISTER},
185 {0xd6, "setlocal_2", "", -1, 0, 0, OP_REGISTER},
186 {0xd7, "setlocal_3", "", -1, 0, 0, OP_REGISTER},
187 {0x6f, "setglobalslot", "u", -1, 0, 0, 0},
188 {0x61, "setproperty", "2", -2, 0, 0, OP_STACK_NS},
189 {0x6d, "setslot", "u", -2, 0, 0, 0},
190 {0x05, "setsuper", "2", -2, 0, 0, OP_STACK_NS},
191 {0xac, "strictequals", "", -2, 1, 0, 0},
192 {0xa1, "subtract", "", -2, 1, 0, 0},
193 {0xc6, "subtract_i", "", -2, 1, 0, 0},
194 {0x2b, "swap", "", -2, 2, 0, 0},
195 {0x03, "throw", "", -1, 0, 0, OP_THROW},
196 {0x95, "typeof", "", -1, 1, 0, 0},
197 {0xa7, "urshift", "", -2, 1, 0, 0},
199 /* opcodes not documented, but seen in the wild */
200 //0x53: seen in builtin.abc
201 {0x53, "applytype", "n", -1, 1, 0, OP_STACK_ARGS},
203 /* dummy instructions. Warning: these are not actually supported by flash */
204 {0xfd, "__fallthrough__", "s", 0, 0, 0, OP_INTERNAL},
205 {0xfe, "__continue__", "s", 0, 0, 0, OP_RETURN|OP_INTERNAL},
206 {0xff, "__break__", "s", 0, 0, 0, OP_RETURN|OP_INTERNAL},
209 static U8 op2index[256] = {254};
211 opcode_t* opcode_get(U8 op)
214 if(op2index[0]==254) {
215 memset(op2index, 255, sizeof(op2index));
216 for(t=0;t<sizeof(opcodes)/sizeof(opcodes[0]);t++) {
217 op2index[opcodes[t].opcode] = t;
220 if(op2index[op]!=255)
221 return &opcodes[op2index[op]];
225 static code_t*pos2code(code_t**bytepos, code_t*c, int pos, int len)
232 (pos!=len && !bytepos[pos])) {
233 /* flex likes to generate these. yuck. */
235 opcode_t*op = opcode_get(c->opcode);
236 fprintf(stderr, "Warning: Invalid jump instruction \"%s\" from %d to %d (%d)\n", op->name, c->pos, pos, len);
238 fprintf(stderr, "Warning: Invalid jump to %d (%d)\n", pos, len);
243 //opcode_t*op = opcode_get(c->opcode);
244 //fprintf(stderr, "Warning: jump beyond end of code in instruction %s at position %d\n", op->name, c->pos);
251 code_t* code_atposition(codelookup_t*l, int pos)
253 return pos2code(l->bytepos, 0, pos, l->len);
256 void lookupswitch_print(lookupswitch_t*l)
258 printf("default: %08x\n", l->def);
259 code_list_t*t = l->targets;
261 printf("target: %08x\n", t->code);
266 code_t*code_parse(TAG*tag, int len, abc_file_t*file, pool_t*pool, codelookup_t**codelookup)
271 int end=tag->pos+len;
272 //printf("-->\n");fflush(stdout);
274 code_t**bytepos = rfx_calloc(sizeof(code_t*)*len);
276 while(tag->pos<end) {
277 int codepos = tag->pos-start;
278 U8 opcode = swf_GetU8(tag);
279 opcode_t*op = opcode_get(opcode);
281 fprintf(stderr, "Can't parse opcode %02x\n", opcode);
284 //printf("%s\n", op->name);fflush(stdout);
288 bytepos[codepos] = c;
303 if(*p == 'n') { // number
304 data = (void*)(ptroff_t)swf_GetU30(tag);
305 } else if(*p == '2') { //multiname
306 data = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
307 } else if(*p == 'U') { //uint
308 data = (void*)(ptroff_t)pool_lookup_uint(pool, swf_GetU30(tag));
309 } else if(*p == 'I') { //int
310 data = (void*)(ptroff_t)pool_lookup_int(pool, swf_GetU30(tag));
311 } else if(*p == 'f') { //int
312 double*fp = malloc(sizeof(double));
313 *fp = pool_lookup_float(pool, swf_GetU30(tag));
315 } else if(*p == 'm') { //method
316 data = array_getvalue(file->methods, swf_GetU30(tag));
317 } else if(*p == 'c') { //classinfo
318 data = array_getvalue(file->classes, swf_GetU30(tag));
319 } else if(*p == 'i') {
320 data = array_getvalue(file->method_bodies, swf_GetU30(tag));
321 } else if(*p == 'u') { // generic integer
322 data = (void*)(ptroff_t)swf_GetU30(tag);
323 } else if(*p == 'r') { // local register
324 data = (void*)(ptroff_t)swf_GetU30(tag);
325 } else if(*p == 'b') { // byte
326 data = (void*)(ptroff_t)swf_GetU8(tag);
327 } else if(*p == 'j') { // jump
328 int j = swf_GetS24(tag);
329 data = (void*)(ptroff_t)j;
330 } else if(*p == 's') { // string
331 string_t s = pool_lookup_string2(pool, swf_GetU30(tag));
332 data = string_dup3(&s);
333 } else if(*p == 'D') { // debug
335 U8 type = swf_GetU8(tag);
337 fprintf(stderr, "Unknown debug type: %02x\n", type);
339 code->data[0] = strdup((char*)pool_lookup_string(pool, swf_GetU30(tag)));
341 code->data[1] = (void*)(ptroff_t)swf_GetU8(tag);
344 } else if(*p == 'S') { // switch statement
345 lookupswitch_t*l = malloc(sizeof(lookupswitch_t));
346 l->def = (code_t*)(ptroff_t)swf_GetS24(tag);
347 l->targets = list_new();
348 int num = swf_GetU30(tag)+1;
351 list_append(l->targets, (code_t*)(ptroff_t)swf_GetS24(tag));
354 printf("Can't parse opcode param type \"%c\" (for op %02x %s).\n", *p, code->opcode, op->name);
358 code->data[pos++] = data;
363 //#define DEBUG_BYTES
367 code_t*c = bytepos[t];
369 opcode_t*op = opcode_get(c->opcode);
370 if(op->flags & (OP_JUMP|OP_BRANCH)) {
371 printf("%5d) %02x %s %d\n", t, tag->data[start+t], op->name, c->data[0]);
373 printf("%5d) %02x %s\n", t, tag->data[start+t], op->name);
376 printf("%5d) %02x\n", t, tag->data[start+t]);
379 //printf("%5d) %02x\n", t, tag->data[start+t]);
384 opcode_t*op = opcode_get(c->opcode);
385 if(op->flags & (OP_JUMP|OP_BRANCH)) {
386 int j = ((int)(ptroff_t)c->data[0]);
387 c->branch = pos2code(bytepos,c,j+4,len);
388 } else if(op->flags & (OP_LOOKUPSWITCH)) {
389 lookupswitch_t*l = (lookupswitch_t*)c->data[0];
391 l->def = pos2code(bytepos,c,(ptroff_t)l->def+offset,len);
392 code_list_t*t=l->targets;
394 t->code = pos2code(bytepos,c,(ptroff_t)t->code+offset,len);
402 (*codelookup) = malloc(sizeof(codelookup_t));
403 (*codelookup)->bytepos = bytepos;
404 (*codelookup)->len = len;
412 void codelookup_free(codelookup_t*codelookup)
414 free(codelookup->bytepos);codelookup->bytepos=0;
418 code_t*code_find_start(code_t*c)
425 void code_free(code_t*c)
427 c = code_find_start(c);
429 code_t*next = c->next;
430 opcode_t*op = opcode_get(c->opcode);
431 char*p = op?op->params:"";
434 void*data = c->data[pos];
435 if(*p == '2') { //multiname
436 multiname_destroy(data);
437 } else if(strchr("sDf", *p)) {
439 } else if(strchr("S", *p)) {
440 lookupswitch_t*l = (lookupswitch_t*)data;
441 list_free(l->targets);l->targets=0;
447 memset(c, 0, sizeof(c));
453 static int opcode_write(TAG*tag, code_t*c, pool_t*pool, abc_file_t*file, int length)
455 opcode_t*op = opcode_get(c->opcode);
461 swf_SetU8(tag, c->opcode);
464 if(op->flags & OP_INTERNAL) {
465 if(c->opcode == OPCODE___BREAK__ ||
466 c->opcode == OPCODE___CONTINUE__) {
467 fprintf(stderr, "Unresolved %s\n", op->name);
469 fprintf(stderr, "Error: writing undefined internal opcode %s\n", op->name);
474 void*data = c->data[pos++];
476 if(*p == 'n') { // number
477 len += swf_SetU30(tag, (ptroff_t)data);
478 } else if(*p == '2') { //multiname
479 multiname_t*m = (multiname_t*)data;
480 len += swf_SetU30(tag, pool_register_multiname(pool, m));
481 } else if(*p == 'm') { //method
482 abc_method_t*m = (abc_method_t*)data;
483 len += swf_SetU30(tag, m->index);
484 } else if(*p == 'c') { //classinfo
485 abc_class_t*cls = (abc_class_t*)data;
486 len += swf_SetU30(tag, cls->index);
487 } else if(*p == 'i') { //methodbody
488 abc_method_body_t*m = (abc_method_body_t*)data;
489 len += swf_SetU30(tag, m->index);
490 } else if(*p == 'I') { // int
491 len += swf_SetU30(tag, pool_register_int(pool, (ptroff_t)data));
492 } else if(*p == 'U') { // uint
493 len += swf_SetU30(tag, pool_register_uint(pool, (ptroff_t)data));
494 } else if(*p == 'f') { // float
495 len += swf_SetU30(tag, pool_register_float(pool, *(double*)data));
496 } else if(*p == 'u') { // integer
497 len += swf_SetU30(tag, (ptroff_t)data);
498 } else if(*p == 'r') { // integer
499 len += swf_SetU30(tag, (ptroff_t)data);
500 } else if(*p == 'b') { // byte
502 swf_SetU8(tag, (ptroff_t)data);
504 } else if(*p == 'j') { // jump
505 int skip = length-c->pos-4;
507 skip = (c->branch->pos) - c->pos - 4;
508 len += swf_SetS24(tag, skip);
509 } else if(*p == 's') { // string
510 int index = pool_register_string2(pool, (string_t*)data);
511 len += swf_SetU30(tag, index);
512 } else if(*p == 'D') { // debug statement
516 len+=swf_SetU30(tag, pool_register_string(pool,c->data[0]));
518 swf_SetU8(tag, (ptroff_t)c->data[1]);
520 len+=swf_SetU30(tag, 0);
521 } else if(*p == 'S') { // switch statement
522 lookupswitch_t*l = (lookupswitch_t*)data;
524 len+=swf_SetS24(tag, l->def->pos-c->pos+offset); //default
525 code_list_t*t = l->targets;
527 len+=swf_SetU30(tag, list_length(t)-1); //nr-1
528 code_list_t*t = l->targets;
530 len+=swf_SetS24(tag, t->code->pos - c->pos+offset);
534 len+=swf_SetU30(tag, 0); //nr-1
535 len+=swf_SetS24(tag, l->def->pos-c->pos+offset);
538 printf("Can't parse opcode param type \"%c\"\n", *p);
545 void code_write(TAG*tag, code_t*code, pool_t*pool, abc_file_t*file)
547 code = code_find_start(code);
553 pos += opcode_write(0, c, pool, file, 0);
557 swf_SetU30(tag, pos);
558 int start = tag->len;
562 opcode_t*op = opcode_get(code->opcode);
563 if(op->flags&(OP_BRANCH|OP_JUMP)) {
566 pos += opcode_write(tag, c, pool, file, length);
569 assert(tag->len - start == pos);
589 static int stack_minus(code_t*c)
591 opcode_t*op = opcode_get(c->opcode);
592 if(op->stack_minus>0) {
593 fprintf(stderr, "Invalid opcode entry %02x %s\n", c->opcode, op->name);
595 int stack = op->stack_minus;
596 if(op->flags&OP_STACK_NS) {
597 multiname_t*m = (multiname_t*)c->data[0];
598 if(multiname_late_namespace(m))
600 if(multiname_late_name(m))
603 if(op->flags&OP_STACK_ARGS || op->flags&OP_STACK_ARGS2) {
604 assert(strchr(op->params, 'n'));
605 int nr = (ptroff_t)(op->params[0]=='n'?c->data[0]:c->data[1]);
607 if(op->flags&OP_STACK_ARGS2)
612 static void handleregister(currentstats_t*stats, int reg)
614 if(reg+1 > stats->maxlocal)
615 stats->maxlocal = reg+1;
621 static void dumpstack(currentstats_t*stats)
624 for(t=0;t<stats->num;t++) {
625 code_t*c = stats->stack[t].code;
626 opcode_t*op = opcode_get(c->opcode);
627 printf("%5d) %c %d:%d %s", t, (stats->stack[t].flags&FLAG_SEEN)?'x':'|',
628 stats->stack[t].stackpos,
629 stats->stack[t].scopepos,
632 if(op->flags&(OP_BRANCH|OP_JUMP)) {
634 printf(" ->%d\n", c->branch->pos);
636 printf(" 00000000\n");
638 if(op->params[0]=='2') {
639 printf(" %s", multiname_tostring(c->data[0]));
645 static char callcode(currentstats_t*stats, int pos, int stack, int scope)
647 while(pos<stats->num) {
648 if(stats->stack[pos].flags&FLAG_SEEN) {
649 if(stats->stack[pos].stackpos != stack ||
650 stats->stack[pos].scopepos != scope) {
652 stats->stack[pos].flags |= FLAG_ERROR;
653 fprintf(stderr, "Stack mismatch at pos %d\n", pos);
654 fprintf(stderr, "Should be: %d:%d, is: %d:%d\n", stack, scope,
655 stats->stack[pos].stackpos, stats->stack[pos].scopepos);
657 /* return error here if we do verification */
663 stats->stack[pos].flags |= FLAG_SEEN;
664 stats->stack[pos].stackpos = stack;
665 stats->stack[pos].scopepos = scope;
667 code_t*c = stats->stack[pos].code;
668 opcode_t*op = opcode_get(c->opcode);
670 //printf("Walking %s at position %d, stack=%d, scope=%d\n", op->name, pos, stack, scope);
672 stack += stack_minus(c);
675 stats->stack[pos].flags |= FLAG_ERROR;
676 fprintf(stderr, "error: stack underflow at %d (%s)\n", pos, op->name);
678 /* if we would do true verification (if we would be a vm), this is
679 where we would return the error
684 stack += op->stack_plus;
685 scope += op->scope_stack_plus;
687 if(stack > stats->maxstack)
688 stats->maxstack = stack;
689 if(scope > stats->maxscope)
690 stats->maxscope = scope;
692 if(op->flags & OP_SET_DXNS)
693 stats->flags |= FLAGS_SET_DXNS;
694 if(op->flags & OP_NEED_ACTIVATION)
695 stats->flags |= FLAGS_ACTIVATION;
697 if(c->opcode == OPCODE_NEWCLASS) {
698 abc_class_t*cls = (abc_class_t*)(c->data[0]);
699 if(scope > cls->init_scope_depth)
700 cls->init_scope_depth = scope;
702 if(c->opcode == OPCODE_NEWFUNCTION) {
703 abc_method_t*m = (abc_method_t*)(c->data[0]);
704 if(m->body && scope > m->body->init_scope_depth)
705 m->body->init_scope_depth = scope;
708 if(op->flags & OP_REGISTER) {
714 handleregister(stats, (ptroff_t)c->data[pos]);
720 handleregister(stats, c->opcode&3);
723 if(op->flags&OP_RETURN) {
724 if(OP_RETURN==0x48/*returnvalue*/) {
726 stats->stack[pos].flags |= FLAG_ERROR;
727 fprintf(stderr, "return(value) with stackposition %d\n", stack);
729 } else if(OP_RETURN==0x47) {
731 stats->stack[pos].flags |= FLAG_ERROR;
732 fprintf(stderr, "return(void) with stackposition %d\n", stack);
736 if(op->flags & (OP_THROW|OP_RETURN))
738 if(op->flags & OP_JUMP) {
740 stats->stack[pos].flags |= FLAG_ERROR;
741 fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d.\n", op->name, pos);
748 if(op->flags & OP_BRANCH) {
750 stats->stack[pos].flags |= FLAG_ERROR;
751 fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d\n", op->name, pos);
754 int newpos = c->branch->pos;
755 if(!callcode(stats, newpos, stack, scope))
758 if(op->flags & OP_LOOKUPSWITCH) {
759 lookupswitch_t*l = c->data[0];
761 stats->stack[pos].flags |= FLAG_ERROR;
762 fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d\n", op->name, pos);
765 if(!callcode(stats, l->def->pos, stack, scope))
767 code_list_t*t = l->targets;
770 stats->stack[pos].flags |= FLAG_ERROR;
771 fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d\n", op->name, pos);
774 if(!callcode(stats, t->code->pos, stack, scope))
782 assert(c->next == stats->stack[pos].code);
788 static currentstats_t* code_get_stats(code_t*code, abc_exception_list_t*exceptions)
790 code = code_find_start(code);
797 currentstats_t* current = malloc(sizeof(currentstats_t));
798 current->stack = rfx_calloc(sizeof(stackpos_t)*num);
799 current->maxlocal = 0;
800 current->maxstack = 0;
801 current->maxscope = 0;
805 //#define DEBUG_BYTES
810 opcode_t*op = opcode_get(c->opcode);
811 if(op->flags & (OP_JUMP|OP_BRANCH)) {
812 printf("%5d) %s %08x\n", t, op->name, c->branch);
813 } else if(op->params[0]=='2') {
814 printf("%5d) %s %s\n", t, op->name, multiname_tostring(c->data[0]));
816 printf("%5d) %s\n", t, op->name);
820 //printf("%5d) %02x\n", t, tag->data[start+t]);
827 current->stack[num].code = c;
833 if(!callcode(current, 0, 0, 0)) {
837 abc_exception_list_t*e = exceptions;
839 if(e->abc_exception->target)
840 callcode(current, e->abc_exception->target->pos, 1, 0);
847 void stats_free(currentstats_t*stats)
850 free(stats->stack);stats->stack=0;
855 int code_dump(code_t*c, abc_exception_list_t*exceptions, abc_file_t*file, char*prefix, FILE*fo)
857 abc_exception_list_t*e = exceptions;
858 c = code_find_start(c);
859 currentstats_t*stats = code_get_stats(c, exceptions);
863 U8 opcode = c->opcode;
865 opcode_t*op = opcode_get(opcode);
869 if(c==e->abc_exception->from)
870 fprintf(fo, "%s TRY {\n", prefix);
871 if(c==e->abc_exception->target) {
872 char*s1 = multiname_tostring(e->abc_exception->exc_type);
873 char*s2 = multiname_tostring(e->abc_exception->var_name);
874 fprintf(fo, "%s CATCH(%s %s)\n", prefix, s1, s2);
882 fprintf(stderr, "Can't parse opcode %02x.\n", opcode);
890 int f = stats->stack[c->pos].flags;
891 fprintf(fo, "%s%5d) %c %d:%d %s ", prefix, c->pos,
892 (f&FLAG_ERROR)?'E':((f&FLAG_SEEN)?'+':'|'),
893 stats->stack[c->pos].stackpos,
894 stats->stack[c->pos].scopepos,
897 fprintf(fo, "%s%5d) ? ?:? %s ", prefix, c->pos, op->name);
901 void*data = c->data[i];
906 int n = (ptroff_t)data;
907 fprintf(fo, "%d params", n);
908 } else if(*p == '2') {
909 multiname_t*n = (multiname_t*)data;
910 char* m = multiname_tostring(n);
911 fprintf(fo, "%s", m);
913 } else if(*p == 'm') {
914 abc_method_t*m = (abc_method_t*)data;
915 fprintf(fo, "[method %08x %s]", m, m->name);
916 } else if(*p == 'c') {
917 abc_class_t*cls = (abc_class_t*)data;
918 char*classname = multiname_tostring(cls->classname);
919 fprintf(fo, "[classinfo %s]", classname);
921 } else if(*p == 'i') {
922 abc_method_body_t*b = (abc_method_body_t*)data;
923 fprintf(fo, "[methodbody]");
924 } else if(*p == 'u' || *p == 'I' || *p == 'U') {
925 int n = (ptroff_t)data;
926 fprintf(fo, "%d", n);
927 } else if(*p == 'f') {
928 double f = *(double*)data;
929 fprintf(fo, "%f", f);
930 } else if(*p == 'r') {
931 int n = (ptroff_t)data;
932 fprintf(fo, "r%d", n);
933 } else if(*p == 'b') {
934 int b = (ptroff_t)data;
935 fprintf(fo, "%02x", b);
936 } else if(*p == 'j') {
938 fprintf(fo, "->%d", c->branch->pos);
940 fprintf(fo, "%08x", c->branch);
941 } else if(*p == 's') {
942 char*s = string_escape((string_t*)data);
943 fprintf(fo, "\"%s\"", s);
945 } else if(*p == 'D') {
946 fprintf(fo, "[register %02x=%s]", (ptroff_t)c->data[1], (char*)c->data[0]);
947 } else if(*p == 'S') {
948 lookupswitch_t*l = c->data[0];
951 fprintf(fo, "default->%d", l->def->pos);
953 fprintf(fo, "default->00000000", l->def->pos);
954 code_list_t*t = l->targets;
957 fprintf(fo, ",->%d", t->code->pos);
959 fprintf(fo, ",->00000000");
964 fprintf(stderr, "Can't parse opcode param type \"%c\"\n", *p);
976 if(c==e->abc_exception->to) {
977 if(e->abc_exception->target)
978 fprintf(fo, "%s } // END TRY (HANDLER: %d)\n", prefix, e->abc_exception->target->pos);
980 fprintf(fo, "%s } // END TRY (HANDLER: 00000000)\n", prefix);
992 codestats_t* code_get_statistics(code_t*code, abc_exception_list_t*exceptions)
994 currentstats_t*current = code_get_stats(code, exceptions);
997 codestats_t*stats = rfx_calloc(sizeof(codestats_t));
998 stats->local_count = current->maxlocal;
999 stats->max_stack = current->maxstack;
1000 stats->max_scope_depth = current->maxscope;
1001 stats->flags = current->flags;
1003 stats_free(current);current=0;
1007 void codestats_free(codestats_t*s)
1012 code_t* add_opcode(code_t*atag, U8 op)
1014 code_t*tmp = (code_t*)rfx_calloc(sizeof(code_t));
1019 tmp->next = atag->next;
1027 void codestats_print(codestats_t*stats)
1029 printf("max_stack: %d\n", stats->max_stack);
1030 printf("local_count: %d\n", stats->local_count);
1031 printf("scope_depth: %d\n", stats->max_scope_depth);
1034 code_t* code_end(code_t*code)
1042 code_t* code_start(code_t*code)
1051 code_t* code_append(code_t*code, code_t*toappend)
1054 return code_end(toappend);
1056 return code_end(code);
1057 //find end of first list
1061 code_t*start=toappend;
1062 //and start of second list
1063 while(start->prev) {
1064 start = start->prev;
1068 return code_end(toappend);
1071 lookupswitch_t*lookupswitch_dup(lookupswitch_t*l)
1073 lookupswitch_t*n = malloc(sizeof(lookupswitch_t));
1074 fprintf(stderr, "Error: lookupswitch dupping not supported yet\n");
1075 n->targets = list_clone(l->targets);
1079 code_t*code_dup(code_t*c)
1083 while(c->prev) c = c->prev;
1088 memcpy(n, c, sizeof(code_t));
1090 opcode_t*op = opcode_get(c->opcode);
1092 fprintf(stderr, "Error: Can't duplicate branching code\n");
1095 char*p = op?op->params:"";
1098 if(*p == '2') { //multiname
1099 c->data[pos] = multiname_clone(c->data[pos]);
1100 } else if(*p == 's') {
1101 c->data[pos] = string_dup3(c->data[pos]);
1102 } else if(*p == 'D') {
1103 c->data[pos] = strdup(c->data[pos]);
1104 } else if(*p == 'f') {
1105 double old = *(double*)c->data[pos];
1106 c->data[pos] = malloc(sizeof(double));
1107 *(double*)c->data[pos] = old;
1108 } else if(strchr("S", *p)) {
1109 c->data[pos] = lookupswitch_dup(c->data[pos]);
1124 code_t*code_cut(code_t*c)
1127 code_t*prev = c->prev;
1128 code_t*next = c->next;
1131 if(prev) prev->next=next;
1132 if(next) next->prev=prev;
1135 if(next) return code_end(next);
1139 code_t*code_cutlast(code_t*c)
1146 code_t* cut_last_push(code_t*c)
1150 opcode_t*op = opcode_get(c->opcode);
1151 /* cut conversion type operations */
1152 if(op->stack_minus == -1 && op->stack_plus == 1 && !(op->flags)) {
1153 c = code_cutlast(c);
1156 /* cut any type of push */
1157 else if(op->stack_minus == 0 && op->stack_plus == 1 && !(op->flags)) {
1158 return code_cutlast(c);
1160 /* cut register lookups */
1161 else if(c->opcode == OPCODE_GETLOCAL ||
1162 c->opcode == OPCODE_GETLOCAL_0 ||
1163 c->opcode == OPCODE_GETLOCAL_1 ||
1164 c->opcode == OPCODE_GETLOCAL_2 ||
1165 c->opcode == OPCODE_GETLOCAL_3) {
1166 return code_cutlast(c);
1168 /* discard function call values */
1169 else if(c->opcode == OPCODE_CALLPROPERTY) {
1170 c->opcode = OPCODE_CALLPROPVOID;
1172 } else if(c->opcode == OPCODE_CALLSUPER) {
1173 c->opcode = OPCODE_CALLSUPERVOID;
1175 } else if(c->opcode == OPCODE_NEWOBJECT ||
1176 c->opcode == OPCODE_NEWARRAY) {
1177 // we can discard these if they're not eating up stack parameters
1179 return code_cutlast(c);
1180 } else if(op->stack_minus ==0 && op->stack_plus == 0 &&
1181 !(op->flags&~(OP_REGISTER|OP_SET_DXNS)) && c->prev) {
1182 // trim code *before* the kill, inclocal, declocal, dxns
1186 return code_append(cut_last_push(p), c);