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", "", -1, 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 abc_code_t*code_find_start(abc_code_t*c)
331 void code_free(abc_code_t*c)
333 c = code_find_start(c);
335 abc_code_t*next = c->next;
336 opcode_t*op = opcode_get(c->opcode);
337 char*p = op?op->params:"";
340 void*data = c->data[pos];
341 if(*p == '2') { //multiname
342 multiname_destroy(data);
343 } else if(strchr("sD", *p)) {
349 memset(c, 0, sizeof(c));
355 int code_dump(abc_code_t*c, abc_file_t*file, char*prefix, FILE*fo)
357 c = code_find_start(c);
358 pool_t*pool = pool_new();
361 U8 opcode = c->opcode;
364 opcode_t*op = opcode_get(opcode);
366 fprintf(stderr, "Can't parse opcode %02x.\n", opcode);
369 fprintf(fo, "%s%s ", prefix, op->name);
374 void*data = c->data[pos];
379 int n = (ptroff_t)data;
380 fprintf(fo, "%d params", n);
381 } else if(*p == '2') {
382 multiname_t*n = (multiname_t*)data;
383 char* m = multiname_to_string(n);
384 fprintf(fo, "%s", m);
386 } else if(*p == 'm') {
387 abc_method_t*m = (abc_method_t*)data;
388 fprintf(fo, "[method %s]", m->name);
389 } else if(*p == 'c') {
390 abc_class_t*cls = (abc_class_t*)data;
391 char*classname = multiname_to_string(cls->classname);
392 fprintf(fo, "[classinfo %s]", classname);
394 } else if(*p == 'i') {
395 abc_method_body_t*b = (abc_method_body_t*)data;
396 fprintf(fo, "[methodbody]");
397 } else if(*p == 'u') {
398 int n = (ptroff_t)data;
399 fprintf(fo, "%d", n);
400 } else if(*p == 'r') {
401 int n = (ptroff_t)data;
402 fprintf(fo, "r%d", n);
403 } else if(*p == 'b') {
404 int b = (ptroff_t)data;
405 fprintf(fo, "%02x", b);
406 } else if(*p == 'j') {
407 int n = (ptroff_t)data;
408 fprintf(fo, "%d", n);
409 } else if(*p == 's') {
410 fprintf(fo, "\"%s\"", data);
411 } else if(*p == 'D') {
412 fprintf(fo, "[register %02x=%s]", (ptroff_t)c->data[1], (char*)c->data[0]);
413 } else if(*p == 'S') {
414 fprintf(fo, "[switch data]");
416 fprintf(stderr, "Can't parse opcode param type \"%c\"\n", *p);
430 static int opcode_write(TAG*tag, abc_code_t*code, pool_t*pool, abc_file_t*file)
432 opcode_t*op = opcode_get(code->opcode);
438 swf_SetU8(tag, code->opcode);
442 void*data = code->data[pos++];
444 if(*p == 'n') { // number
445 len += swf_SetU30(tag, (ptroff_t)data);
446 } else if(*p == '2') { //multiname
447 multiname_t*m = (multiname_t*)data;
448 len += swf_SetU30(tag, pool_register_multiname(pool, m));
449 } else if(*p == 'm') { //method
450 abc_method_t*m = (abc_method_t*)data;
451 len += swf_SetU30(tag, m->index);
452 } else if(*p == 'c') { //classinfo
453 abc_class_t*cls = (abc_class_t*)data;
454 len += swf_SetU30(tag, cls->index);
455 } else if(*p == 'i') { //methodbody
456 abc_method_body_t*m = (abc_method_body_t*)data;
457 len += swf_SetU30(tag, m->index);
458 } else if(*p == 'u') { // integer
459 len += swf_SetU30(tag, (ptroff_t)data);
460 } else if(*p == 'r') { // integer
461 len += swf_SetU30(tag, (ptroff_t)data);
462 } else if(*p == 'b') { // byte
464 swf_SetU8(tag, (ptroff_t)data);
466 } else if(*p == 'j') { // jump
467 len += swf_SetS24(tag, (ptroff_t)data);
468 } else if(*p == 's') { // string
469 int index = pool_register_string(pool, data);
470 len += swf_SetU30(tag, index);
471 } else if(*p == 'D') { // debug statement
475 len+=swf_SetU30(tag, pool_register_string(pool,code->data[0]));
477 swf_SetU8(tag, (ptroff_t)code->data[1]);
479 len+=swf_SetU30(tag, 0);
480 } else if(*p == 'S') { // switch statement
481 len+=swf_SetU24(tag, 0); //default
482 len+=swf_SetU30(tag, 0); //nr-1
483 len+=swf_SetU24(tag, 0); //first
485 printf("Can't parse opcode param type \"%c\"\n", *p);
492 void code_write(TAG*tag, abc_code_t*code, pool_t*pool, abc_file_t*file)
494 code = code_find_start(code);
500 pos += opcode_write(0, c, pool, file);
504 swf_SetU30(tag, pos);
505 int start = tag->len;
509 opcode_t*op = opcode_get(code->opcode);
510 if(op->flags&(OP_BRANCH|OP_JUMP)) {
513 skip = (c->branch->pos) - pos - 4;
515 skip = length - pos - 4;
516 c->data[0] = (void*)(ptroff_t)skip;
518 pos += opcode_write(tag, c, pool, file);
521 assert(tag->len - start == pos);
540 static int stack_minus(abc_code_t*c)
542 opcode_t*op = opcode_get(c->opcode);
543 assert(op->stack_minus<=0);
544 int stack = op->stack_minus;
545 if(op->flags&OP_STACK_NS) {
546 multiname_t*m = (multiname_t*)c->data[0];
547 stack--; //read namespace
548 if(m->type == RTQNAMEL || m->type == RTQNAMELA) {
552 if(op->flags&OP_STACK_ARGS || op->flags&OP_STACK_ARGS2) {
553 assert(strchr(op->params, 'n'));
554 int nr = (ptroff_t)(op->params[0]=='n'?c->data[0]:c->data[1]);
556 if(op->flags&OP_STACK_ARGS2)
561 static void handleregister(currentstats_t*stats, int reg)
563 if(reg+1 > stats->maxlocal)
564 stats->maxlocal = reg+1;
567 static void callcode(currentstats_t*stats, int pos, int stack, int scope)
569 while(pos<stats->num) {
570 if(stats->stack[pos].seen) {
571 if(stats->stack[pos].stackpos != stack ||
572 stats->stack[pos].scopepos != scope) {
573 fprintf(stderr, "Stack mismatch at pos %d\n", pos);
578 stats->stack[pos].seen = 1;
579 stats->stack[pos].stackpos = stack;
580 stats->stack[pos].scopepos = scope;
582 abc_code_t*c = stats->stack[pos].code;
583 opcode_t*op = opcode_get(c->opcode);
585 printf("Walking %s at position %d, stack=%d, scope=%d\n", op->name, pos, stack, scope);
587 stack += stack_minus(c);
590 fprintf(stderr, "error: stack underflow at %d (%s)\n", pos, op->name);
594 stack += op->stack_plus;
595 scope += op->scope_stack_plus;
597 if(stack > stats->maxstack)
598 stats->maxstack = stack;
599 if(scope > stats->maxscope)
600 stats->maxscope = scope;
602 if(op->flags & OP_REGISTER) {
608 handleregister(stats, (ptroff_t)c->data[pos]);
614 handleregister(stats, c->opcode&3);
617 if(op->flags & (OP_THROW|OP_RETURN))
619 if(op->flags & OP_JUMP) {
624 if(op->flags & OP_BRANCH) {
625 int newpos = c->branch->pos;
626 callcode(stats, newpos, stack, scope);
631 assert(c->next == stats->stack[pos].code);
636 codestats_t code_get_statistics(abc_code_t*code)
638 code = code_find_start(code);
645 currentstats_t current;
646 current.stack = rfx_calloc(sizeof(stackpos_t)*num);
647 current.maxlocal = 0;
648 current.maxstack = 0;
649 current.maxscope = 0;
656 current.stack[num].code = c;
661 callcode(¤t, 0, 0, 0);
666 stats.local_count = current.maxlocal;
667 stats.max_stack = current.maxstack;
668 stats.init_scope_depth = 0;
669 stats.max_scope_depth = current.maxscope;
672 abc_code_t* add_opcode(abc_code_t*atag, U8 op)
674 abc_code_t*tmp = (abc_code_t*)malloc(sizeof(abc_code_t));