+lookupswitch_t*lookupswitch_dup(lookupswitch_t*l)
+{
+ lookupswitch_t*n = malloc(sizeof(lookupswitch_t));
+ fprintf(stderr, "Error: lookupswitch dupping not supported yet\n");
+ n->targets = list_clone(l->targets);
+ return 0;
+}
+
+code_t*code_dup(code_t*c)
+{
+ if(!c) return 0;
+
+ while(c->prev) c = c->prev;
+
+ code_t*last = 0;
+ while(c) {
+ NEW(code_t, n);
+ memcpy(n, c, sizeof(code_t));
+
+ opcode_t*op = opcode_get(c->opcode);
+ if(c->branch || c->opcode == OPCODE_LABEL) {
+ fprintf(stderr, "Error: Can't duplicate branching code\n");
+ return 0;
+ }
+ char*p = op?op->params:"";
+ int pos=0;
+ while(*p) {
+ if(*p == '2') { //multiname
+ c->data[pos] = multiname_clone(c->data[pos]);
+ } else if(*p == 's') {
+ c->data[pos] = string_dup3(c->data[pos]);
+ } else if(*p == 'D') {
+ c->data[pos] = strdup(c->data[pos]);
+ } else if(*p == 'f') {
+ double old = *(double*)c->data[pos];
+ c->data[pos] = malloc(sizeof(double));
+ *(double*)c->data[pos] = old;
+ } else if(strchr("S", *p)) {
+ c->data[pos] = lookupswitch_dup(c->data[pos]);
+ }
+ p++;pos++;
+ }
+
+ n->prev = last;
+ if(last) {
+ last->next = n;
+ }
+ last = n;
+ c = c->next;
+ }
+ return last;
+}
+
+code_t*code_cut(code_t*c)
+{
+ if(!c) return c;
+ code_t*prev = c->prev;
+ code_t*next = c->next;
+ c->prev = 0;
+ c->next = 0;
+ if(prev) prev->next=next;
+ if(next) next->prev=prev;
+ code_free(c);
+
+ if(next) return code_end(next);
+ else return prev;
+}
+
+code_t*code_cutlast(code_t*c)
+{
+ if(!c) return c;
+ assert(!c->next);
+ return code_cut(c);
+}
+
+code_t* cut_last_push(code_t*c)
+{
+ assert(!c->next);
+ while(c) {
+ if(!c) break;
+ opcode_t*op = opcode_get(c->opcode);
+ /* cut conversion type operations */
+ if(op->stack_minus == -1 && op->stack_plus == 1 && !(op->flags)) {
+ c = code_cutlast(c);
+ continue;
+ }
+ /* cut any type of push */
+ else if(op->stack_minus == 0 && op->stack_plus == 1 && !(op->flags)) {
+ return code_cutlast(c);
+ }
+ /* cut register lookups */
+ else if(c->opcode == OPCODE_GETLOCAL ||
+ c->opcode == OPCODE_GETLOCAL_0 ||
+ c->opcode == OPCODE_GETLOCAL_1 ||
+ c->opcode == OPCODE_GETLOCAL_2 ||
+ c->opcode == OPCODE_GETLOCAL_3) {
+ return code_cutlast(c);
+ }
+ /* discard function call values */
+ else if(c->opcode == OPCODE_CALLPROPERTY) {
+ c->opcode = OPCODE_CALLPROPVOID;
+ return c;
+ } else if(c->opcode == OPCODE_CALLSUPER) {
+ c->opcode = OPCODE_CALLSUPERVOID;
+ return c;
+ } else if(c->opcode == OPCODE_NEWOBJECT ||
+ c->opcode == OPCODE_NEWARRAY) {
+ // we can discard these if they're not eating up stack parameters
+ if(!c->data[0])
+ return code_cutlast(c);
+ } else if(op->stack_minus ==0 && op->stack_plus == 0 &&
+ !(op->flags&~(OP_REGISTER|OP_SET_DXNS)) && c->prev) {
+ // trim code *before* the kill, inclocal, declocal, dxns
+ code_t*p = c->prev;
+ p->next = 0;
+ c->prev = 0;
+ return code_append(cut_last_push(p), c);
+ } else
+ break;
+ }
+ c = abc_pop(c);
+ return c;
+}
+
+