5 #define OP_STACK_ARGS 2
14 #define OP_NEED_ACTIVATION 512
15 #define OP_STACK_ARGS2 1024
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
23 c = u30 index into class
24 s = string (u30 index into strings)
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},
175 static U8 op2index[256] = {254};
177 opcode_t* opcode_get(U8 op)
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;
186 if(op2index[op]!=255)
187 return &opcodes[op2index[op]];
191 abc_code_t*code_parse(TAG*tag, int len, abc_file_t*file, pool_t*pool)
196 int end=tag->pos+len;
197 //printf("-->\n");fflush(stdout);
199 abc_code_t**bytepos = rfx_calloc(sizeof(abc_code_t*)*len);
201 while(tag->pos<end) {
202 int codepos = tag->pos-start;
203 U8 opcode = swf_GetU8(tag);
204 opcode_t*op = opcode_get(opcode);
206 fprintf(stderr, "Can't parse opcode %02x\n", opcode);
209 //printf("%s\n", op->name);fflush(stdout);
213 bytepos[codepos] = c;
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
251 U8 type = swf_GetU8(tag);
253 fprintf(stderr, "Unknown debug type: %02x\n", type);
255 code->data[0] = strdup((char*)pool_lookup_string(pool, swf_GetU30(tag)));
257 code->data[1] = (void*)(ptroff_t)swf_GetU8(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;
269 printf("Can't parse opcode param type \"%c\".\n", *p);
273 code->data[pos++] = data;
278 //#define DEBUG_BYTES
282 abc_code_t*c = bytepos[t];
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]);
288 printf("%5d) %02x %s\n", t, tag->data[start+t], op->name);
291 printf("%5d) %02x\n", t, tag->data[start+t]);
294 printf("%5d) %02x\n", t, tag->data[start+t]);
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;
303 printf("%s %d %d\n", op->name, c->pos, j);
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);
315 code->branch = bytepos[c->pos+j];
324 void code_free(abc_code_t*c)
327 abc_code_t*next = c->next;
328 opcode_t*op = opcode_get(c->opcode);
329 char*p = op?op->params:"";
332 void*data = c->data[pos];
333 if(*p == '2') { //multiname
334 multiname_destroy(data);
335 } else if(strchr("sD", *p)) {
341 memset(c, 0, sizeof(c));
347 int code_dump(abc_code_t*c, abc_file_t*file, char*prefix, FILE*fo)
349 pool_t*pool = pool_new();
352 U8 opcode = c->opcode;
355 opcode_t*op = opcode_get(opcode);
357 fprintf(stderr, "Can't parse opcode %02x.\n", opcode);
360 fprintf(fo, "%s%s ", prefix, op->name);
365 void*data = c->data[pos];
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);
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);
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]");
407 fprintf(stderr, "Can't parse opcode param type \"%c\"\n", *p);
421 static int opcode_write(TAG*tag, abc_code_t*code, pool_t*pool, abc_file_t*file)
423 opcode_t*op = opcode_get(code->opcode);
429 swf_SetU8(tag, code->opcode);
433 void*data = code->data[pos++];
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
455 swf_SetU8(tag, (ptroff_t)data);
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
466 len+=swf_SetU30(tag, pool_register_string(pool,code->data[0]));
468 swf_SetU8(tag, (ptroff_t)code->data[1]);
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
476 printf("Can't parse opcode param type \"%c\"\n", *p);
483 void code_write(TAG*tag, abc_code_t*code, pool_t*pool, abc_file_t*file)
490 pos += opcode_write(0, c, pool, file);
494 swf_SetU30(tag, pos);
495 int start = tag->len;
499 opcode_t*op = opcode_get(code->opcode);
500 if(op->flags&(OP_BRANCH|OP_JUMP)) {
503 skip = (c->branch->pos) - pos - 4;
505 skip = length - pos - 4;
506 c->data[0] = (void*)(ptroff_t)skip;
508 pos += opcode_write(tag, c, pool, file);
511 assert(tag->len - start == pos);
530 static int stack_minus(abc_code_t*c)
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) {
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]);
546 if(op->flags&OP_STACK_ARGS2)
551 static void handleregister(currentstats_t*stats, int reg)
553 if(reg > stats->maxlocal)
554 stats->maxlocal = reg;
557 static void callcode(currentstats_t*stats, int pos, int stack, int scope)
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);
567 stats->stack[pos].seen = 1;
568 stats->stack[pos].stackpos = stack;
569 stats->stack[pos].scopepos = scope;
571 abc_code_t*c = stats->stack[pos].code;
572 opcode_t*op = opcode_get(c->opcode);
574 stack += stack_minus(c);
577 fprintf(stderr, "error: stack underflow at %d (%s)\n", pos, op->name);
581 stack += op->stack_plus;
582 scope += op->scope_stack_plus;
584 if(stack > stats->maxstack)
585 stats->maxstack = stack;
586 if(scope > stats->maxscope)
587 stats->maxscope = scope;
589 if(op->flags & OP_REGISTER) {
595 handleregister(stats, (ptroff_t)c->data[pos]);
601 handleregister(stats, c->opcode&3);
604 if(op->flags & (OP_THROW|OP_RETURN))
606 if(op->flags & OP_JUMP) {
611 if(op->flags & OP_BRANCH) {
612 int newpos = c->branch->pos;
613 callcode(stats, newpos, stack, scope);
618 assert(c->next == stats->stack[pos].code);
623 codestats_t code_get_statistics(abc_code_t*code)
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;
642 current.stack[num].code = c;
647 callcode(¤t, 0, 0, 0);
652 stats.local_count = current.maxlocal;
653 stats.max_stack = current.maxstack;
654 stats.max_scope_depth = current.maxscope;
657 abc_code_t* add_opcode(abc_code_t*atag, U8 op)
659 abc_code_t*tmp = (abc_code_t*)malloc(sizeof(abc_code_t));
665 tmp->parent = atag->parent;