fixed format warnings
[swftools.git] / lib / as3 / code.c
1 /* code.c
2
3    Routines for handling Flash2 AVM2 ABC Actionscript
4
5    Extension module for the rfxswf library.
6    Part of the swftools package.
7
8    Copyright (c) 2008 Matthias Kramm <kramm@quiss.org>
9  
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.
14
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.
19
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 */
23
24 #include <assert.h>
25 #include "code.h"
26 #include "pool.h"
27
28 #define OP_REGISTER 1
29 #define OP_STACK_ARGS 2
30 #define OP_STACK_NS 4
31 #define OP_SET_DXNS 8
32 #define OP_RETURN 16
33 #define OP_THROW 32
34 #define OP_BRANCH 64
35 #define OP_JUMP 128
36 #define OP_LABEL 256
37 #define OP_LOOKUPSWITCH 512
38 #define OP_NEED_ACTIVATION 1024
39 #define OP_STACK_ARGS2 2048
40 #define OP_INTERNAL 32768
41
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
46    b = byte
47    c = u30 index into class
48    N = u30 index into namespaces
49    s = string (u30 index into strings)
50    S = switch
51    u = u30
52    r = register
53 */
54
55 opcode_t opcodes[]={
56 {0xa0, "add", "",              -2, 1, 0, 0},
57 {0xc5, "add_i", "",            -2, 1, 0, 0},
58 {0x86, "astype", "2",          -1, 1, 0, 0},
59 {0x87, "astypelate", "",       -2, 1, 0, 0},
60 {0xA8, "bitand", "",           -2, 1, 0, 0},
61 {0x97, "bitnot", "",           -1, 1, 0, 0},
62 {0xa9, "bitor", "",            -2, 1, 0, 0},
63 {0xaa, "bitxor", "",           -2, 1, 0, 0},
64 {0x41, "call", "n",            -2, 1, 0, OP_STACK_ARGS},
65 {0x43, "callmethod", "mn",     -1, 1, 0, OP_STACK_ARGS},
66 {0x4c, "callproplex", "2n",    -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
67 {0x46, "callproperty", "2n",   -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
68 {0x4f, "callpropvoid", "2n",   -1, 0, 0, OP_STACK_ARGS|OP_STACK_NS},
69 {0x44, "callstatic", "in",     -1, 1, 0, OP_STACK_ARGS},
70 {0x45, "callsuper", "2n",      -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
71 {0x4e, "callsupervoid", "2n",  -1, 0, 0, OP_STACK_ARGS|OP_STACK_NS},
72 {0x78, "checkfilter", "",      -1, 1, 0, 0},
73 {0x80, "coerce", "2",          -1, 1, 0, 0},
74 {0x82, "coerce_a", "",         -1, 1, 0, 0},
75 {0x85, "coerce_s", "",         -1, 1, 0, 0},
76 {0x42, "construct", "n",       -1, 1, 0, OP_STACK_ARGS},
77 {0x4a, "constructprop", "2n",  -1, 1, 0, OP_STACK_ARGS|OP_STACK_NS},
78 {0x49, "constructsuper", "n",  -1, 0, 0, OP_STACK_ARGS},
79 {0x76, "convert_b", "",        -1, 1, 0, 0},
80 {0x73, "convert_i", "",        -1, 1, 0, 0},
81 {0x75, "convert_d", "",        -1, 1, 0, 0},
82 {0x77, "convert_o", "",        -1, 1, 0, 0},
83 {0x74, "convert_u", "",        -1, 1, 0, 0},
84 {0x70, "convert_s", "",        -1, 1, 0, 0},
85 {0xef, "debug", "D",            0, 0, 0, 0},
86 {0xf1, "debugfile", "s",        0, 0, 0, 0},
87 {0xf0, "debugline", "u",        0, 0, 0, 0},
88 {0x94, "declocal", "r",         0, 0, 0, OP_REGISTER},
89 {0xc3, "declocal_i", "r",       0, 0, 0, OP_REGISTER},
90 {0x93, "decrement", "",        -1, 1, 0, 0}, 
91 {0xc1, "decrement_i", "",      -1, 1, 0, 0},
92 {0x6a, "deleteproperty", "2",  -1, 1, 0, OP_STACK_NS},
93 {0xa3, "divide", "",           -2, 1, 0, 0},
94 {0x2a, "dup", "",              -1, 2, 0, 0},
95 {0x06, "dxns", "s",             0, 0, 0, OP_SET_DXNS},
96 {0x07, "dxnslate", "",         -1, 0, 0, OP_SET_DXNS},
97 {0xab, "equals", "",           -2, 1, 0, 0},
98 {0x72, "esc_xattr", "",        -1, 1, 0, 0},
99 {0x71, "esc_xelem", "",        -1, 1, 0, 0},
100 {0x5e, "findproperty", "2",     0, 1, 0, OP_STACK_NS},
101 {0x5d, "findpropstrict", "2",   0, 1, 0, OP_STACK_NS},
102 {0x59, "getdescendants", "2",  -1, 1, 0, OP_STACK_NS},
103 {0x64, "getglobalscope", "",    0, 1, 0, 0},
104 {0x6e, "getglobalslot", "u",    0, 1, 0, 0},
105 {0x60, "getlex", "2",           0, 1, 0, 0}, //multiname may not be runtime
106 {0x62, "getlocal", "r",         0, 1, 0, OP_REGISTER},
107 {0xd0, "getlocal_0", "",        0, 1, 0, OP_REGISTER},
108 {0xd1, "getlocal_1", "",        0, 1, 0, OP_REGISTER},
109 {0xd2, "getlocal_2", "",        0, 1, 0, OP_REGISTER},
110 {0xd3, "getlocal_3", "",        0, 1, 0, OP_REGISTER},
111 {0x66, "getproperty", "2",     -1, 1, 0, OP_STACK_NS},
112 {0x65, "getscopeobject", "u",   0, 1, 0, 0}, // u = index into scope stack
113 {0x6c, "getslot", "u",         -1, 1, 0, 0},
114 {0x04, "getsuper", "2",        -1, 1, 0, OP_STACK_NS},
115 {0xaf, "greaterthan", "",      -2, 1, 0, 0},
116 {0xb0, "greaterequals", "",    -2, 1, 0, 0},
117 {0x1f, "hasnext", "",          -2, 1, 0, 0},
118 {0x32, "hasnext2", "rr",        0, 1, 0, OP_REGISTER},
119 {0x13, "ifeq", "j",            -2, 0, 0, OP_BRANCH},
120 {0x12, "iffalse", "j",         -1, 0, 0, OP_BRANCH},
121 {0x18, "ifge", "j",            -2, 0, 0, OP_BRANCH},
122 {0x17, "ifgt", "j",            -2, 0, 0, OP_BRANCH},
123 {0x16, "ifle", "j",            -2, 0, 0, OP_BRANCH},
124 {0x15, "iflt", "j",            -2, 0, 0, OP_BRANCH},
125 {0x0f, "ifnge", "j",           -2, 0, 0, OP_BRANCH},
126 {0x0e, "ifngt", "j",           -2, 0, 0, OP_BRANCH},
127 {0x0d, "ifnle", "j",           -2, 0, 0, OP_BRANCH},
128 {0x0c, "ifnlt", "j",           -2, 0, 0, OP_BRANCH},
129 {0x14, "ifne", "j",            -2, 0, 0, OP_BRANCH},
130 {0x19, "ifstricteq", "j",      -2, 0, 0, OP_BRANCH},
131 {0x1a, "ifstrictne", "j",      -2, 0, 0, OP_BRANCH},
132 {0x11, "iftrue", "j",          -1, 0, 0, OP_BRANCH},
133 {0xb4, "in", "",               -2, 1, 0, 0},
134 {0x92, "inclocal", "r",         0, 0, 0, OP_REGISTER},
135 {0xc2, "inclocal_i", "r",       0, 0, 0, OP_REGISTER},
136 {0x91, "increment", "",        -1, 1, 0, 0},
137 {0xc0, "increment_i", "",      -1, 1, 0, 0},
138 {0x68, "initproperty", "2",    -2, 0, 0, OP_STACK_NS},
139 {0xb1, "instanceof", "",       -2, 1, 0, 0},
140 {0xb2, "istype", "2",          -1, 1, 0, 0}, // may not be a runtime multiname
141 {0xb3, "istypelate", "",       -2, 1, 0, 0},
142 {0x10, "jump", "j",             0, 0, 0, OP_JUMP},
143 {0x08, "kill", "r",             0, 0, 0, OP_REGISTER},
144 {0x09, "label", "",             0, 0, 0, OP_LABEL},
145 {0xae, "lessequals", "",       -2, 1, 0, 0},
146 {0xad, "lessthan", "",         -2, 1, 0, 0},
147 {0x1b, "lookupswitch", "S",    -1, 0, 0, OP_LOOKUPSWITCH},
148 {0xa5, "lshift", "",           -2, 1, 0, 0},
149 {0xa4, "modulo", "",           -2, 1, 0, 0},
150 {0xa2, "multiply", "",         -2, 1, 0, 0},
151 {0xc7, "multiply_i", "",       -2, 1, 0, 0},
152 {0x90, "negate", "",           -1, 1, 0, 0},
153 {0xc4, "negate_i", "",         -1, 1, 0, 0},
154 {0x57, "newactivation", "",     0, 1, 0, OP_NEED_ACTIVATION},
155 {0x56, "newarray", "n",         0, 1, 0, OP_STACK_ARGS},
156 {0x5a, "newcatch", "u",         0, 1, 0, 0}, //u = index into exception_info
157 {0x58, "newclass", "c",        -1, 1, 0, 0}, //c = index into class_info
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", "N",    0, 1, 0, 0}, //index into namespace
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},
199
200 /* opcodes not documented, but seen in the wild */
201 {0x53, "applytype", "n",       -1, 1, 0, OP_STACK_ARGS}, //seen in builtin.abc
202
203 /* dummy instructions. Warning: these are not actually supported by flash */
204 {0xfb, "__pushpackage__", "s",      0, 1, 0, OP_INTERNAL},
205 {0xfc, "__rethrow__", "",           0, 0, 0, OP_THROW|OP_INTERNAL},
206 {0xfd, "__fallthrough__", "s",      0, 0, 0, OP_INTERNAL},
207 {0xfe, "__continue__", "s",         0, 0, 0, OP_RETURN|OP_INTERNAL},
208 {0xff, "__break__", "s",            0, 0, 0, OP_RETURN|OP_INTERNAL},
209 };
210
211 static opcode_t* op2op[256] = {0,0,0};
212
213 static inline opcode_t* opcode_get(U8 op)
214 {
215     int t;
216     if(!op2op[0x02]) {
217         memset(op2op, 0, sizeof(op2op));
218         for(t=0;t<sizeof(opcodes)/sizeof(opcodes[0]);t++) {
219             op2op[opcodes[t].opcode] = &opcodes[t];
220         }
221     }
222     return op2op[op];
223 }
224
225 static code_t*pos2code(code_t**bytepos, code_t*c, int pos, int len)
226 {
227     if(c) {
228         pos+=c->pos;
229     }
230     if(pos < 0 ||
231        pos > len ||
232       (pos!=len && !bytepos[pos])) {
233         /* flex likes to generate these. yuck. */
234         if(c) {
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);
237         } else {
238             fprintf(stderr, "Warning: Invalid jump to %d (%d)\n", pos, len);
239         }
240         return 0;
241     } else {
242         if(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);
245             return 0;
246         } else {
247             return bytepos[pos];
248         }
249     }
250 }
251 code_t* code_atposition(codelookup_t*l, int pos)
252 {
253     return pos2code(l->bytepos, 0, pos, l->len);
254 }
255
256 void lookupswitch_print(lookupswitch_t*l)
257 {
258     printf("default: %08x\n", (int)l->def);
259     code_list_t*t = l->targets;
260     while(t) {
261         printf("target: %08x\n", (int)t->code);
262         t = t->next;
263     }
264 }
265
266 code_t*code_parse(TAG*tag, int len, abc_file_t*file, pool_t*pool, codelookup_t**codelookup)
267 {
268     code_t*head=0;
269     code_t*code=0;
270     int start=tag->pos;
271     int end=tag->pos+len;
272     //printf("-->\n");fflush(stdout);
273
274     code_t**bytepos = rfx_calloc(sizeof(code_t*)*len);
275         
276     while(tag->pos<end) {
277         int codepos = tag->pos-start;
278         U8 opcode = swf_GetU8(tag);
279         opcode_t*op = opcode_get(opcode);
280         if(!op) {
281             fprintf(stderr, "Can't parse opcode %02x\n", opcode);
282             continue;
283         }
284         //printf("%s\n", op->name);fflush(stdout);
285         NEW(code_t,c);
286         c->pos = codepos;
287
288         bytepos[codepos] = c;
289
290         if(!head) {
291             head = code = c;
292         } else {
293             code->next = c;
294             c->prev = code;
295             code = c;
296         }
297
298         c->opcode = opcode;
299
300         char*p = op->params;
301         int pos = 0;
302         while(*p) {
303             void*data = 0;
304             if(*p == 'n') { // number
305                 data = (void*)(ptroff_t)swf_GetU30(tag);
306             } else if(*p == '2') { //multiname
307                 data = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
308             } else if(*p == 'N') { //namespace
309                 data = namespace_clone(pool_lookup_namespace(pool, swf_GetU30(tag)));
310             } else if(*p == 'U') { //uint
311                 data = (void*)(ptroff_t)pool_lookup_uint(pool, swf_GetU30(tag));
312             } else if(*p == 'I') { //int
313                 data = (void*)(ptroff_t)pool_lookup_int(pool, swf_GetU30(tag));
314             } else if(*p == 'f') { //int
315                 double*fp = malloc(sizeof(double));
316                 *fp = pool_lookup_float(pool, swf_GetU30(tag));
317                 data = fp;
318             } else if(*p == 'm') { //method
319                 data = array_getvalue(file->methods, swf_GetU30(tag));
320             } else if(*p == 'c') { //classinfo
321                 data = array_getvalue(file->classes, swf_GetU30(tag));
322             } else if(*p == 'i') {
323                 data = array_getvalue(file->method_bodies, swf_GetU30(tag));
324             } else if(*p == 'u') { // generic integer
325                 data = (void*)(ptroff_t)swf_GetU30(tag);
326             } else if(*p == 'r') { // local register
327                 data = (void*)(ptroff_t)swf_GetU30(tag);
328             } else if(*p == 'b') { // byte
329                 data = (void*)(ptroff_t)swf_GetU8(tag);
330             } else if(*p == 'j') { // jump
331                 int j = swf_GetS24(tag);
332                 data = (void*)(ptroff_t)j;
333             } else if(*p == 's') { // string
334                 string_t s = pool_lookup_string2(pool, swf_GetU30(tag));
335                 data = string_dup3(&s);
336             } else if(*p == 'D') { // debug
337                 /*type, usually 1*/
338                 U8 type = swf_GetU8(tag);
339                 if(type!=1) 
340                     fprintf(stderr, "Unknown debug type: %02x\n", type);
341                 /*register name*/
342                 code->data[0] = strdup((char*)pool_lookup_string(pool, swf_GetU30(tag)));
343                 /*register index*/
344                 code->data[1] = (void*)(ptroff_t)swf_GetU8(tag);
345                 /*unused*/
346                 swf_GetU30(tag);
347             } else if(*p == 'S') { // switch statement
348                 lookupswitch_t*l = malloc(sizeof(lookupswitch_t));
349                 l->def = (code_t*)(ptroff_t)swf_GetS24(tag);
350                 l->targets = list_new();
351                 int num = swf_GetU30(tag)+1;
352                 int t;
353                 for(t=0;t<num;t++) 
354                     list_append(l->targets, (code_t*)(ptroff_t)swf_GetS24(tag));
355                 data = l;
356             } else {
357                 printf("Can't parse opcode param type \"%c\" (for op %02x %s).\n", *p, code->opcode, op->name);
358                 return 0;
359             }
360             if(data)
361                 code->data[pos++] = data;
362             p++;
363         }
364     }
365
366 //#define DEBUG_BYTES
367 #ifdef DEBUG_BYTES
368     int t;
369     for(t=0;t<len;t++) {
370         code_t*c = bytepos[t];
371         if(c) {
372             opcode_t*op = opcode_get(c->opcode);
373             if(op->flags & (OP_JUMP|OP_BRANCH)) {
374                 printf("%50d) %02x %s %d\n", t, tag->data[start+t], op->name, c->data[0]);
375             } else {
376                 printf("%50d) %02x %s\n", t, tag->data[start+t], op->name);
377             }
378         } else {
379             printf("%50d) %02x\n", t, tag->data[start+t]);
380         }
381     }
382     //printf("%05d) %02x\n", t, tag->data[start+t]);
383 #endif
384
385     code_t*c = head;
386     while(c) {
387         opcode_t*op = opcode_get(c->opcode);
388         if(op->flags & (OP_JUMP|OP_BRANCH)) {
389             int j = ((int)(ptroff_t)c->data[0]);
390             c->branch = pos2code(bytepos,c,j+4,len);
391         } else if(op->flags & (OP_LOOKUPSWITCH)) {
392             lookupswitch_t*l = (lookupswitch_t*)c->data[0];
393             int offset = 0;
394             l->def = pos2code(bytepos,c,(ptroff_t)l->def+offset,len);
395             code_list_t*t=l->targets;
396             while(t) {
397                 t->code = pos2code(bytepos,c,(ptroff_t)t->code+offset,len);
398                 t = t->next;
399             }
400         }
401         c = c->next;
402     } 
403
404     if(codelookup) {
405         (*codelookup) = malloc(sizeof(codelookup_t));
406         (*codelookup)->bytepos = bytepos;
407         (*codelookup)->len = len;
408     } else {
409         free(bytepos);
410     }
411
412     return head;
413 }
414
415 void codelookup_free(codelookup_t*codelookup)
416 {
417     free(codelookup->bytepos);codelookup->bytepos=0;
418     free(codelookup);
419 }
420
421 void code_free(code_t*c)
422 {
423     c = code_start(c);
424     while(c) {
425         code_t*next = c->next;
426         opcode_t*op = opcode_get(c->opcode);
427         char*p = op?op->params:"";
428         int pos=0;
429         while(*p) {
430             void*data = c->data[pos];
431             if(*p == '2') { //multiname
432                 multiname_destroy(data);
433             } else if(*p == 'N') { //namespace
434                 namespace_destroy(data);
435             } else if(strchr("sDf", *p)) {
436                 free(data);
437             } else if(strchr("S", *p)) {
438                 lookupswitch_t*l = (lookupswitch_t*)data;
439                 list_free(l->targets);l->targets=0;
440                 free(l);
441             }
442             c->data[pos]=0;
443             p++;pos++;
444         }
445         memset(c, 0, sizeof(c));
446         free(c);
447         c = next;
448     }
449 }
450
451 static int opcode_write(TAG*tag, code_t*c, pool_t*pool, abc_file_t*file, int length)
452 {
453     opcode_t*op = opcode_get(c->opcode);
454     char*p = op->params;
455     int pos = 0;
456     int len = 0;
457
458     if(tag)
459         swf_SetU8(tag, c->opcode);
460     len++;
461
462     if(op->flags & OP_INTERNAL) {
463         if(c->opcode == OPCODE___BREAK__ ||
464            c->opcode == OPCODE___CONTINUE__) {
465             fprintf(stderr, "Unresolved %s\n", op->name);
466         } else {
467             fprintf(stderr, "Error: writing undefined internal opcode %s\n", op->name);
468         }
469     }
470
471     while(*p) {
472         void*data = c->data[pos++];
473         assert(pos<=2);
474         switch(*p) {
475             case 'n': { // number
476                 len += swf_SetU30(tag, (ptroff_t)data);
477                 break;
478             }
479             case '2': { //multiname
480                 multiname_t*m = (multiname_t*)data;
481                 len += swf_SetU30(tag, pool_register_multiname(pool, m));
482                 break;
483             }
484             case 'N': { //namespace
485                 namespace_t*ns = (namespace_t*)data;
486                 len += swf_SetU30(tag, pool_register_namespace(pool, ns));
487                 break;
488             }
489             case 'm': { //method
490                 abc_method_t*m = (abc_method_t*)data;
491                 len += swf_SetU30(tag, m->index);
492                 break;
493             }
494             case 'c': { //classinfo 
495                 abc_class_t*cls = (abc_class_t*)data;
496                 len += swf_SetU30(tag, cls->index);
497                 break;
498             }
499             case 'i': { //methodbody
500                 abc_method_body_t*m = (abc_method_body_t*)data;
501                 len += swf_SetU30(tag, m->index);
502                 break;
503             }
504             case 'I': { // int
505                 len += swf_SetU30(tag, pool_register_int(pool, (ptroff_t)data));
506                 break;
507             }
508             case 'U': { // uint
509                 len += swf_SetU30(tag, pool_register_uint(pool, (ptroff_t)data));
510                 break;
511             }
512             case 'f': { //  float
513                 len += swf_SetU30(tag, pool_register_float(pool, *(double*)data));
514                 break;
515             }
516             case 'u': { // integer
517                 len += swf_SetU30(tag, (ptroff_t)data);
518                 break;
519             }
520             case 'r': { // integer
521                 len += swf_SetU30(tag, (ptroff_t)data);
522                 break;
523             }
524             case 'b': { // byte
525                 if(tag)
526                     swf_SetU8(tag, (ptroff_t)data);
527                 len++;
528                 break;
529             }
530             case 'j': { // jump
531                 int skip = length-c->pos-4;
532                 if(c->branch) 
533                     skip = (c->branch->pos) - c->pos - 4;
534                 len += swf_SetS24(tag, skip);
535                 break;
536             }
537             case 's': { // string
538                 int index = pool_register_string2(pool, (string_t*)data);
539                 len += swf_SetU30(tag, index);
540                 break;
541             }
542             case 'D': { // debug statement
543                 if(tag)
544                     swf_SetU8(tag, 1);
545                 len++;
546                 len+=swf_SetU30(tag, pool_register_string(pool,c->data[0]));
547                 if(tag)
548                     swf_SetU8(tag, (ptroff_t)c->data[1]);
549                 len++;
550                 len+=swf_SetU30(tag, 0);
551                 break;
552             }
553             case 'S': { // switch statement
554                 lookupswitch_t*l = (lookupswitch_t*)data;
555                 int offset = 0;
556                 len+=swf_SetS24(tag, l->def->pos-c->pos+offset); //default
557                 code_list_t*t = l->targets;
558                 if(list_length(t)) {
559                     len+=swf_SetU30(tag, list_length(t)-1); //nr-1
560                     code_list_t*t = l->targets;
561                     while(t) {
562                         len+=swf_SetS24(tag, t->code->pos - c->pos+offset);
563                         t = t->next;
564                     }
565                 } else {
566                     len+=swf_SetU30(tag, 0); //nr-1
567                     len+=swf_SetS24(tag, l->def->pos-c->pos+offset);
568                 }
569                 break;
570             }
571             default:
572                 printf("Can't parse opcode param type \"%c\"\n", *p);
573         }
574         p++;
575     }
576     return len;
577 }
578
579 void code_write(TAG*tag, code_t*code, pool_t*pool, abc_file_t*file)
580 {
581     code = code_start(code);
582     int pos = 0;
583     int length = 0;
584     code_t*c = code;
585     while(c) {
586         c->pos = pos;
587         pos += opcode_write(0, c, pool, file, 0);
588         c = c->next;
589     }
590     length = pos;
591     swf_SetU30(tag, pos);
592     int start = tag->len;
593     c = code;
594     pos = 0;
595     while(c) {
596         opcode_t*op = opcode_get(code->opcode);
597         if(op->flags&(OP_BRANCH|OP_JUMP)) {
598             int skip = 0;
599         }
600         pos += opcode_write(tag, c, pool, file, length);
601         c = c->next;
602     }
603     assert(tag->len - start == pos);
604 }
605
606 typedef struct {
607     int stackpos;
608     int scopepos;
609     code_t*code;
610     char flags;
611     char error;
612 } stackpos_t;
613
614 typedef struct {
615     stackpos_t*stack;
616     int num;
617     int maxlocal;
618     int maxstack;
619     int maxscope;
620     int flags;
621 } currentstats_t;
622
623 static int stack_minus(code_t*c)
624 {
625     opcode_t*op = opcode_get(c->opcode);
626     if(op->stack_minus>0) {
627         fprintf(stderr, "Invalid opcode entry %02x %s\n", c->opcode, op->name);
628     }
629     int stack = op->stack_minus;
630     if(op->flags&OP_STACK_NS) {
631         multiname_t*m = (multiname_t*)c->data[0];
632         if(multiname_late_namespace(m))
633             stack--;
634         if(multiname_late_name(m))
635             stack--;
636     } 
637     if(op->flags&OP_STACK_ARGS || op->flags&OP_STACK_ARGS2) {
638         assert(strchr(op->params, 'n'));
639         int nr = (ptroff_t)(op->params[0]=='n'?c->data[0]:c->data[1]);
640         stack-=nr;
641         if(op->flags&OP_STACK_ARGS2)
642             stack-=nr;
643     }
644     return stack;
645 }
646 static void handleregister(currentstats_t*stats, int reg)
647 {
648     if(reg+1 > stats->maxlocal)
649         stats->maxlocal = reg+1;
650 }
651
652 #define FLAG_SEEN 1
653 #define FLAG_ERROR 2
654
655 static void dumpstack(currentstats_t*stats)
656 {
657     int t;
658     for(t=0;t<stats->num;t++) {
659         code_t*c = stats->stack[t].code;
660         opcode_t*op = opcode_get(c->opcode);
661         printf("%05d) %c %d:%d %s", t, (stats->stack[t].flags&FLAG_SEEN)?'x':'|', 
662                                stats->stack[t].stackpos,
663                                stats->stack[t].scopepos,
664                                op->name);
665
666         if(op->flags&(OP_BRANCH|OP_JUMP)) {
667             if(c->branch)
668                 printf(" ->%d\n", c->branch->pos);
669             else
670                 printf(" 00000000\n");
671         }
672         if(op->params[0]=='2') {
673             printf(" %s", multiname_tostring(c->data[0]));
674         } else if(op->params[0]=='N') {
675             printf(" %s", namespace_tostring(c->data[0]));
676         }
677         printf("\n");
678     }
679 }
680
681 static char callcode(currentstats_t*stats, int pos, int stack, int scope)
682 {
683     while(pos<stats->num) {
684         if(stats->stack[pos].flags&FLAG_SEEN) {
685             if(stats->stack[pos].stackpos != stack ||
686                stats->stack[pos].scopepos != scope) {
687                 //dumpstack(stats);
688                 stats->stack[pos].flags |= FLAG_ERROR;
689                 fprintf(stderr, "Stack mismatch at pos %d\n", pos);
690                 fprintf(stderr, "Should be: %d:%d, is: %d:%d\n", stack, scope,
691                     stats->stack[pos].stackpos, stats->stack[pos].scopepos);
692                
693                 /* return error here if we do verification */
694                 //return 0;
695             }
696             return 1;
697         }
698     
699         stats->stack[pos].flags |= FLAG_SEEN;
700         stats->stack[pos].stackpos = stack;
701         stats->stack[pos].scopepos = scope;
702
703         code_t*c = stats->stack[pos].code;
704         opcode_t*op = opcode_get(c->opcode);
705         
706         //printf("Walking %s at position %d, stack=%d, scope=%d\n", op->name, pos, stack, scope);
707
708         stack += stack_minus(c);
709
710         if(stack<0) {
711             stats->stack[pos].flags |= FLAG_ERROR;
712             fprintf(stderr, "error: stack underflow at %d (%s)\n", pos, op->name);
713            
714             /* if we would do true verification (if we would be a vm), this is 
715                where we would return the error 
716                return 0;
717              */
718         }
719
720         stack += op->stack_plus;
721         scope += op->scope_stack_plus;
722
723         if(stack > stats->maxstack)
724             stats->maxstack = stack;
725         if(scope > stats->maxscope)
726             stats->maxscope = scope;
727
728         if(op->flags & OP_SET_DXNS)
729             stats->flags |= FLAGS_SET_DXNS;
730         if(op->flags & OP_NEED_ACTIVATION)
731             stats->flags |= FLAGS_ACTIVATION;
732
733         if(c->opcode == OPCODE_NEWCLASS) {
734             abc_class_t*cls = (abc_class_t*)(c->data[0]);
735             if(scope > cls->init_scope_depth)
736                 cls->init_scope_depth = scope;
737         }
738         if(c->opcode == OPCODE_NEWFUNCTION) {
739             abc_method_t*m = (abc_method_t*)(c->data[0]);
740             if(m->body && scope > m->body->init_scope_depth)
741                 m->body->init_scope_depth = scope;
742         }
743         
744         if(op->flags & OP_REGISTER) {
745             char*p = op->params;
746             int pos = 0;
747             char ok=0;
748             while(*p) {
749                 if(*p=='r') {
750                     handleregister(stats, (ptroff_t)c->data[pos]);
751                     ok = 1;
752                 }
753                 p++;
754             }
755             if(!ok) {
756                 handleregister(stats, c->opcode&3);
757             }
758         }
759         if(op->flags&OP_RETURN) {
760             if(OP_RETURN==0x48/*returnvalue*/) {
761                 if(stack!=1) {
762                     stats->stack[pos].flags |= FLAG_ERROR;
763                     fprintf(stderr, "return(value) with stackposition %d\n", stack);
764                 }
765             } else if(OP_RETURN==0x47) {
766                 if(stack!=0) {
767                     stats->stack[pos].flags |= FLAG_ERROR;
768                     fprintf(stderr, "return(void) with stackposition %d\n", stack);
769                 }
770             }
771         }
772         if(op->flags & (OP_THROW|OP_RETURN))
773             return 1;
774         if(op->flags & OP_JUMP) {
775             if(!c->branch) {
776                 stats->stack[pos].flags |= FLAG_ERROR;
777                 fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d.\n", op->name, pos);
778                 return 0;
779             }
780             c = c->branch;
781             pos = c->pos;
782             continue;
783         }
784         if(op->flags & OP_BRANCH) {
785             if(!c->branch) {
786                 stats->stack[pos].flags |= FLAG_ERROR;
787                 fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d\n", op->name, pos);
788                 return 0;
789             }
790             int newpos = c->branch->pos;
791             if(!callcode(stats, newpos, stack, scope))
792                 return 0;
793         }
794         if(op->flags & OP_LOOKUPSWITCH) {
795             lookupswitch_t*l = c->data[0];
796             if(!l->def) {
797                 stats->stack[pos].flags |= FLAG_ERROR;
798                 fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d\n", op->name, pos);
799                 return 0;
800             }
801             if(!callcode(stats, l->def->pos, stack, scope))
802                 return 0;
803             code_list_t*t = l->targets;
804             while(t) {
805                 if(!t->code) {
806                     stats->stack[pos].flags |= FLAG_ERROR;
807                     fprintf(stderr, "Error: Invalid jump target in instruction %s at position %d\n", op->name, pos);
808                     return 0;
809                 }
810                 if(!callcode(stats, t->code->pos, stack, scope))
811                     return 0;
812                 t = t->next;
813             }
814         }
815     
816         pos++;
817         if(pos<stats->num) {
818             assert(c->next == stats->stack[pos].code);
819         }
820     }
821     return 1;
822 }
823
824 static currentstats_t* code_get_stats(code_t*code, abc_exception_list_t*exceptions) 
825 {
826     code = code_start(code);
827     int num = 0;
828     code_t*c = code;
829     while(c) {
830         num++;
831         c = c->next;
832     }
833     currentstats_t* current = malloc(sizeof(currentstats_t));
834     current->stack = rfx_calloc(sizeof(stackpos_t)*num);
835     current->maxlocal = 0;
836     current->maxstack = 0;
837     current->maxscope = 0;
838     current->num = num;
839     current->flags = 0;
840
841 //#define DEBUG_BYTES
842 #ifdef DEBUG_BYTES
843     int t;
844     c = code;
845     for(t=0;t<num;t++) {
846         opcode_t*op = opcode_get(c->opcode);
847         if(op->flags & (OP_JUMP|OP_BRANCH)) {
848             printf("%05d) %s %08x\n", t, op->name, c->branch);
849         } else if(op->params[0]=='2') {
850             printf("%05d) %s %s\n", t, op->name, multiname_tostring(c->data[0]));
851         } else if(op->params[0]=='N') {
852             printf("%05d) %s %s\n", t, op->name, namespace_tostring(c->data[0]));
853         } else {
854             printf("%05d) %s\n", t, op->name);
855         }
856         c = c->next;
857     }
858     //printf("%05d) %02x\n", t, tag->data[start+t]);
859 #endif
860
861     num = 0;
862     c = code;
863     while(c) {
864         //crosslink
865         current->stack[num].code = c;
866         c->pos = num;
867         num++;
868         c = c->next;
869     }
870
871     if(!callcode(current, 0, 0, 0)) {
872         free(current);
873         return 0;
874     }
875     abc_exception_list_t*e = exceptions;
876     while(e) {
877         if(e->abc_exception->target)
878             callcode(current, e->abc_exception->target->pos, 1, 0);
879         e = e->next;
880     }
881
882     return current;
883 }
884
885 void stats_free(currentstats_t*stats)
886 {
887     if(stats) {
888         free(stats->stack);stats->stack=0;
889         free(stats);
890     }
891 }
892
893 int code_dump(code_t*c)
894 {
895     code_t*cc = code_start(c);
896     while(cc) {
897         assert(!cc->next || cc->next->prev == cc);
898         cc = cc->next;
899     }
900
901     return code_dump2(c, 0, 0, "", stdout);
902 }
903 int code_dump2(code_t*c, abc_exception_list_t*exceptions, abc_file_t*file, char*prefix, FILE*fo)
904 {
905     abc_exception_list_t*e = exceptions;
906     c = code_start(c);
907     currentstats_t*stats =  code_get_stats(c, exceptions);
908
909     int pos = 0;
910     while(c) {
911         U8 opcode = c->opcode;
912         char found = 0;
913         opcode_t*op = opcode_get(opcode);
914
915         e = exceptions;
916         while(e) {
917             if(c==e->abc_exception->from)
918                 fprintf(fo, "%s   TRY {\n", prefix);
919             if(c==e->abc_exception->target) {
920                 char*s1 = multiname_tostring(e->abc_exception->exc_type);
921                 char*s2 = multiname_tostring(e->abc_exception->var_name);
922                 fprintf(fo, "%s   CATCH(%s %s)\n", prefix, s1, s2);
923                 free(s1);
924                 free(s2);
925             }
926             e = e->next;
927         }
928
929         if(!op) {
930             fprintf(stderr, "Can't parse opcode %02x.\n", opcode);
931             return 0;
932         } else {
933             char*p = op->params;
934             char first = 1;
935             int i=0;
936
937             if(stats) {
938                 int f = stats->stack[c->pos].flags;
939                 fprintf(fo, "%s%05d) %c %d:%d %s ", prefix, c->pos, 
940                                        (f&FLAG_ERROR)?'E':((f&FLAG_SEEN)?'+':'|'),
941                                        stats->stack[c->pos].stackpos,
942                                        stats->stack[c->pos].scopepos,
943                                        op->name);
944             } else {
945                 fprintf(fo, "%s%05d) ? ?:? %s ", prefix, c->pos, op->name);
946             }
947
948             while(*p) {
949                 void*data = c->data[i];
950                 if(i>0)
951                     printf(", ");
952
953                 if(*p == 'n') {
954                     int n = (ptroff_t)data;
955                     fprintf(fo, "%d params", n);
956                 } else if(*p == '2') {
957                     multiname_t*n = (multiname_t*)data;
958                     char* m = multiname_tostring(n);
959                     fprintf(fo, "%s", m);
960                     free(m);
961                 } else if(*p == 'N') {
962                     namespace_t*ns = (namespace_t*)data;
963                     char* m = namespace_tostring(ns);
964                     fprintf(fo, "%s", m);
965                     free(m);
966                 } else if(*p == 'm') {
967                     abc_method_t*m = (abc_method_t*)data;
968                     fprintf(fo, "[method %08x %s]", m->index, m->name);
969                 } else if(*p == 'c') {
970                     abc_class_t*cls = (abc_class_t*)data;
971                     char*classname = multiname_tostring(cls->classname);
972                     fprintf(fo, "[classinfo %08x %s]", cls->index, classname);
973                     free(classname);
974                 } else if(*p == 'i') {
975                     abc_method_body_t*b = (abc_method_body_t*)data;
976                     fprintf(fo, "[methodbody]");
977                 } else if(*p == 'u' || *p == 'I' || *p == 'U') {
978                     int n = (ptroff_t)data;
979                     fprintf(fo, "%d", n);
980                 } else if(*p == 'f') {
981                     double f = *(double*)data;
982                     fprintf(fo, "%f", f);
983                 } else if(*p == 'r') {
984                     int n = (ptroff_t)data;
985                     fprintf(fo, "r%d", n);
986                 } else if(*p == 'b') {
987                     int b = (signed char)(ptroff_t)data;
988                     fprintf(fo, "%d", b);
989                 } else if(*p == 'j') {
990                     if(c->branch)
991                         fprintf(fo, "->%d", c->branch->pos);
992                     else
993                         fprintf(fo, "%08x", (unsigned int)c->branch);
994                 } else if(*p == 's') {
995                     char*s = string_escape((string_t*)data);
996                     fprintf(fo, "\"%s\"", s);
997                     free(s);
998                 } else if(*p == 'D') {
999                     fprintf(fo, "[register %02x=%s]", (ptroff_t)c->data[1], (char*)c->data[0]);
1000                 } else if(*p == 'S') {
1001                     lookupswitch_t*l = c->data[0];
1002                     fprintf(fo, "[");
1003                     if(l->def)
1004                         fprintf(fo, "default->%d", l->def->pos);
1005                     else
1006                         fprintf(fo, "default->00000000");
1007                     code_list_t*t = l->targets;
1008                     while(t) {
1009                         if(t->code)
1010                             fprintf(fo, ",->%d", t->code->pos);
1011                         else
1012                             fprintf(fo, ",->00000000");
1013                         t = t->next;
1014                     }
1015                     fprintf(fo, "]");
1016                 } else {
1017                     fprintf(stderr, "Can't parse opcode param type \"%c\"\n", *p);
1018                     return 0;
1019                 }
1020                 p++;
1021                 i++;
1022                 first = 0;
1023             }
1024             fprintf(fo, "\n");
1025         }
1026         
1027         e = exceptions;
1028         while(e) {
1029             if(c==e->abc_exception->to) {
1030                 if(e->abc_exception->target)
1031                     fprintf(fo, "%s   } // END TRY (HANDLER: %d)\n", prefix, e->abc_exception->target->pos);
1032                 else
1033                     fprintf(fo, "%s   } // END TRY (HANDLER: 00000000)\n", prefix);
1034             }
1035             e = e->next;
1036         }
1037
1038         pos++;
1039         c = c->next;
1040     }
1041     stats_free(stats);
1042     return 1;
1043 }
1044
1045 codestats_t* code_get_statistics(code_t*code, abc_exception_list_t*exceptions) 
1046 {
1047     currentstats_t*current = code_get_stats(code, exceptions);
1048     if(!current)
1049         return 0;
1050     codestats_t*stats = rfx_calloc(sizeof(codestats_t));
1051     stats->local_count = current->maxlocal;
1052     stats->max_stack = current->maxstack;
1053     stats->max_scope_depth = current->maxscope;
1054     stats->flags = current->flags;
1055
1056     stats_free(current);current=0;
1057     return stats;
1058 }
1059
1060 void codestats_free(codestats_t*s)
1061 {
1062     free(s);
1063 }
1064
1065 code_t* add_opcode(code_t*atag, U8 op)
1066 {
1067     code_t*tmp = (code_t*)rfx_calloc(sizeof(code_t));
1068     tmp->opcode = op;
1069     if(atag) {
1070         tmp->prev = atag;
1071         tmp->next = atag->next;
1072         if(tmp->next)
1073             tmp->next->prev = tmp;
1074         atag->next = tmp;
1075     } else {
1076         tmp->prev = 0;
1077         tmp->next = 0;
1078     }
1079     return tmp;
1080 }
1081     
1082 void codestats_print(codestats_t*stats)
1083 {
1084     printf("max_stack: %d\n", stats->max_stack);
1085     printf("local_count: %d\n", stats->local_count);
1086     printf("scope_depth: %d\n", stats->max_scope_depth);
1087 }
1088
1089 code_t* code_end(code_t*code)
1090 {
1091     if(!code)
1092         return 0;
1093     while(code->next)
1094         code = code->next;
1095     return code;
1096 }
1097 code_t* code_start(code_t*code)
1098 {
1099     if(!code)
1100         return 0;
1101     while(code->prev)
1102         code = code->prev;
1103     return code;
1104 }
1105
1106 code_t* code_append(code_t*code, code_t*toappend)
1107 {
1108     if(!code)
1109         return code_end(toappend);
1110     if(!toappend)
1111         return code_end(code);
1112     //find end of first list
1113     while(code->next) {
1114         code = code->next;
1115     }
1116     code_t*start=toappend;
1117     //and start of second list
1118     while(start->prev) {
1119         start = start->prev;
1120     }
1121     code->next = start;
1122     start->prev = code;
1123     return code_end(toappend);
1124 }
1125
1126 lookupswitch_t*lookupswitch_dup(lookupswitch_t*l)
1127 {
1128     lookupswitch_t*n = malloc(sizeof(lookupswitch_t));
1129     fprintf(stderr, "Error: lookupswitch dupping not supported yet\n");
1130     n->targets = list_clone(l->targets);
1131     return 0;
1132 }
1133
1134 code_t*code_dup(code_t*c)
1135 {
1136     if(!c) return 0;
1137
1138     dict_t*pos2pos = dict_new2(&ptr_type);
1139
1140     code_t*last = 0;
1141     c = code_start(c);
1142     code_t*start = 0;
1143     char does_branch = 0;
1144     while(c) {
1145         NEW(code_t, n);
1146         memcpy(n, c, sizeof(code_t));
1147         if(!start) 
1148             start=n;
1149
1150         if(c->opcode == OPCODE_LABEL || c->opcode == OPCODE_NOP) {
1151             dict_put(pos2pos, c, n);
1152         }
1153         if(c->branch) {
1154             does_branch = 1;
1155         }
1156
1157         opcode_t*op = opcode_get(c->opcode);
1158         
1159         char*p = op?op->params:"";
1160         int pos=0;
1161         while(*p) {
1162             if(*p == '2') { //multiname
1163                 c->data[pos] = multiname_clone(c->data[pos]);
1164             } else if(*p == 'N') { //multiname
1165                 c->data[pos] = namespace_clone(c->data[pos]);
1166             } else if(*p == 's') {
1167                 c->data[pos] = string_dup3(c->data[pos]);
1168             } else if(*p == 'D') {
1169                 c->data[pos] = strdup(c->data[pos]);
1170             } else if(*p == 'f') {
1171                 double old = *(double*)c->data[pos];
1172                 c->data[pos] = malloc(sizeof(double));
1173                 *(double*)c->data[pos] = old;
1174             } else if(strchr("S", *p)) {
1175                 c->data[pos] = lookupswitch_dup(c->data[pos]);
1176             }
1177             p++;pos++;
1178         }
1179
1180         n->prev = last;
1181         if(last) {
1182             last->next = n;
1183         }
1184         last = n;
1185         c = c->next;
1186     }
1187    
1188     if(does_branch) {
1189         c = start;
1190         while(c) {
1191             if(c->branch) {
1192                 code_t*target = dict_lookup(pos2pos, c->branch);
1193                 if(!target) {
1194                     fprintf(stderr, "Error: Can't find branch target in code_dup\n");
1195                     return 0;
1196                 }
1197                 c->branch = target;
1198             }
1199             c = c->next;
1200         }
1201     }
1202     dict_destroy(pos2pos);
1203     return last;
1204 }
1205
1206 code_t*code_cut(code_t*c)
1207 {
1208     if(!c) return c;
1209     code_t*prev = c->prev;
1210     code_t*next = c->next;
1211     c->prev = 0;
1212     c->next = 0;
1213     if(prev) prev->next=next;
1214     if(next) next->prev=prev;
1215     code_free(c);
1216
1217     if(next) return code_end(next);
1218     else     return prev;
1219 }
1220
1221 code_t*code_cutlast(code_t*c)
1222 {
1223     if(!c) return c;
1224     assert(!c->next);
1225     return code_cut(c);
1226 }
1227
1228 char is_getlocal(code_t*c)
1229 {
1230     if(!c) return 0;
1231     if(c->opcode == OPCODE_GETLOCAL ||
1232        c->opcode == OPCODE_GETLOCAL_0 ||
1233        c->opcode == OPCODE_GETLOCAL_1 ||
1234        c->opcode == OPCODE_GETLOCAL_2 ||
1235        c->opcode == OPCODE_GETLOCAL_3) {
1236         return 1;
1237     }
1238     return 0;
1239 }
1240
1241 code_t* cut_last_push(code_t*c)
1242 {
1243     assert(!c->next);
1244     while(c) {
1245         if(!c) break;
1246         opcode_t*op = opcode_get(c->opcode);
1247         /* cut conversion type operations */
1248         if(op->stack_minus == -1 && op->stack_plus == 1 && !(op->flags)) {
1249             c = code_cutlast(c);
1250             continue;
1251         }
1252         /* cut any type of push */
1253         else if(op->stack_minus == 0 && op->stack_plus == 1 && !(op->flags)) {
1254             return code_cutlast(c);
1255         }
1256         /* cut register lookups */
1257         else if(c->opcode == OPCODE_GETLOCAL ||
1258            c->opcode == OPCODE_GETLOCAL_0 ||
1259            c->opcode == OPCODE_GETLOCAL_1 ||
1260            c->opcode == OPCODE_GETLOCAL_2 ||
1261            c->opcode == OPCODE_GETLOCAL_3) {
1262             return code_cutlast(c);
1263         }
1264         /* discard function call values */
1265         else if(c->opcode == OPCODE_CALLPROPERTY) {
1266             c->opcode = OPCODE_CALLPROPVOID;
1267             return c;
1268         } else if(c->opcode == OPCODE_CALLSUPER) {
1269             c->opcode = OPCODE_CALLSUPERVOID;
1270             return c;
1271         } else if((c->opcode == OPCODE_NEWOBJECT ||
1272                    c->opcode == OPCODE_NEWARRAY) &&
1273                    !c->data[0]) {
1274             // we can discard these if they're not eating up stack parameters
1275             return code_cutlast(c);
1276         } else if(op->stack_minus ==0 && op->stack_plus == 0 && 
1277                 !(op->flags&~(OP_REGISTER|OP_SET_DXNS)) && c->prev) {
1278             // trim code *before* the kill, inclocal, declocal, dxns
1279             code_t*p = c->prev; 
1280             p->next = 0;
1281             c->prev = 0;
1282             return code_append(cut_last_push(p), c);
1283         } else
1284             break;
1285     }
1286     c = abc_pop(c);
1287     return c;
1288 }
1289
1290