as3: Ast supports brackets now, too. Fixed Math bug in builtins.c
[swftools.git] / lib / as3 / code.c
index 8f2033f..403c85a 100644 (file)
@@ -371,15 +371,15 @@ code_t*code_parse(TAG*tag, int len, abc_file_t*file, pool_t*pool, codelookup_t**
         if(c) {
             opcode_t*op = opcode_get(c->opcode);
             if(op->flags & (OP_JUMP|OP_BRANCH)) {
-                printf("%5d) %02x %s %d\n", t, tag->data[start+t], op->name, c->data[0]);
+                printf("%50d) %02x %s %d\n", t, tag->data[start+t], op->name, c->data[0]);
             } else {
-                printf("%5d) %02x %s\n", t, tag->data[start+t], op->name);
+                printf("%50d) %02x %s\n", t, tag->data[start+t], op->name);
             }
         } else {
-            printf("%5d) %02x\n", t, tag->data[start+t]);
+            printf("%50d) %02x\n", t, tag->data[start+t]);
         }
     }
-    //printf("%5d) %02x\n", t, tag->data[start+t]);
+    //printf("%05d) %02x\n", t, tag->data[start+t]);
 #endif
 
     code_t*c = head;
@@ -625,7 +625,7 @@ static void dumpstack(currentstats_t*stats)
     for(t=0;t<stats->num;t++) {
         code_t*c = stats->stack[t].code;
         opcode_t*op = opcode_get(c->opcode);
-        printf("%5d) %c %d:%d %s", t, (stats->stack[t].flags&FLAG_SEEN)?'x':'|', 
+        printf("%05d) %c %d:%d %s", t, (stats->stack[t].flags&FLAG_SEEN)?'x':'|', 
                                stats->stack[t].stackpos,
                                stats->stack[t].scopepos,
                                op->name);
@@ -812,17 +812,17 @@ static currentstats_t* code_get_stats(code_t*code, abc_exception_list_t*exceptio
     for(t=0;t<num;t++) {
         opcode_t*op = opcode_get(c->opcode);
         if(op->flags & (OP_JUMP|OP_BRANCH)) {
-            printf("%5d) %s %08x\n", t, op->name, c->branch);
+            printf("%05d) %s %08x\n", t, op->name, c->branch);
         } else if(op->params[0]=='2') {
-            printf("%5d) %s %s\n", t, op->name, multiname_tostring(c->data[0]));
+            printf("%05d) %s %s\n", t, op->name, multiname_tostring(c->data[0]));
         } else if(op->params[0]=='N') {
-            printf("%5d) %s %s\n", t, op->name, namespace_tostring(c->data[0]));
+            printf("%05d) %s %s\n", t, op->name, namespace_tostring(c->data[0]));
         } else {
-            printf("%5d) %s\n", t, op->name);
+            printf("%05d) %s\n", t, op->name);
         }
         c = c->next;
     }
-    //printf("%5d) %02x\n", t, tag->data[start+t]);
+    //printf("%05d) %02x\n", t, tag->data[start+t]);
 #endif
 
     num = 0;
@@ -903,13 +903,13 @@ int code_dump2(code_t*c, abc_exception_list_t*exceptions, abc_file_t*file, char*
 
             if(stats) {
                 int f = stats->stack[c->pos].flags;
-                fprintf(fo, "%s%5d) %c %d:%d %s ", prefix, c->pos, 
+                fprintf(fo, "%s%05d) %c %d:%d %s ", prefix, c->pos, 
                                        (f&FLAG_ERROR)?'E':((f&FLAG_SEEN)?'+':'|'),
                                        stats->stack[c->pos].stackpos,
                                        stats->stack[c->pos].scopepos,
                                        op->name);
             } else {
-                fprintf(fo, "%s%5d) ? ?:? %s ", prefix, c->pos, op->name);
+                fprintf(fo, "%s%05d) ? ?:? %s ", prefix, c->pos, op->name);
             }
 
             while(*p) {
@@ -1102,18 +1102,27 @@ code_t*code_dup(code_t*c)
 {
     if(!c) return 0;
 
-    while(c->prev) c = c->prev;
+    dict_t*pos2pos = dict_new2(&ptr_type);
 
     code_t*last = 0;
+    c = code_start(c);
+    code_t*start = 0;
+    char does_branch = 0;
     while(c) {
         NEW(code_t, n);
         memcpy(n, c, sizeof(code_t));
+        if(!start) 
+            start=n;
 
-        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;
+        if(c->opcode == OPCODE_LABEL || c->opcode == OPCODE_NOP) {
+            dict_put(pos2pos, c, n);
         }
+        if(c->branch) {
+            does_branch = 1;
+        }
+
+        opcode_t*op = opcode_get(c->opcode);
+        
         char*p = op?op->params:"";
         int pos=0;
         while(*p) {
@@ -1142,6 +1151,22 @@ code_t*code_dup(code_t*c)
         last = n;
         c = c->next;
     }
+   
+    if(does_branch) {
+        c = start;
+        while(c) {
+            if(c->branch) {
+                code_t*target = dict_lookup(pos2pos, c->branch);
+                if(!target) {
+                    fprintf(stderr, "Error: Can't find branch target in code_dup\n");
+                    return 0;
+                }
+                c->branch = target;
+            }
+            c = c->next;
+        }
+    }
+    dict_destroy(pos2pos);
     return last;
 }