Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

disasm: Initial assembly output

Probably more intelligible than the dumper output.
  • Loading branch information...
commit 47b2f254cb4079bbc9435a650364707eee977fff 1 parent f5b312d
Brian Gernhardt authored August 07, 2012

Showing 1 changed file with 212 additions and 1 deletion. Show diff stats Hide diff stats

  1. 213  src/disasm.winxed
213  src/disasm.winxed
@@ -6,9 +6,18 @@
6 6
 $include 'PACT/Packfile/Decompile.winxed';
7 7
 $load    'PACT/Packfile/Decompile.pbc';
8 8
 
  9
+$include_const 'call_bits.pasm';
  10
+
9 11
 // Load dumper for output
10 12
 $load 'dumper.pbc';
11 13
 
  14
+// Useful opcode
  15
+inline get_repr(var v) return string {
  16
+    string ret;
  17
+    ${ get_repr ret, v };
  18
+    return ret;
  19
+}
  20
+
12 21
 function main[main](var argv) {
13 22
     // Process arguments
14 23
     string progname = argv[0];
@@ -22,7 +31,7 @@ function main[main](var argv) {
22 31
     var packfile;
23 32
     try {
24 33
         :PACT.Packfile.Decompile decomp(filename);
25  
-        _dumper(decomp.pact, 'Packfile');
  34
+        packfile = decomp.pact;
26 35
     } catch (e) {
27 36
         cry( progname, ': Error during disassembly ', filename );
28 37
         cry( e.message );
@@ -30,4 +39,206 @@ function main[main](var argv) {
30 39
             cry(bt);
31 40
         exit(1);
32 41
     }
  42
+
  43
+    // Version number
  44
+    say(".pact pbc 0\n");
  45
+
  46
+    // Constants
  47
+    int i;
  48
+    string s;
  49
+    var v;
  50
+    var vi;
  51
+    var sc_map = {};
  52
+    say('.constants num');
  53
+    v = packfile.floats;
  54
+    for (i = 0; i < elements(v); ++i)
  55
+        say(string(i) + ' ' + string(v[i]));
  56
+    say(".end\n");
  57
+
  58
+    say('.constants string');
  59
+    v = packfile.strings;
  60
+    for (i = 0; i < elements(v); ++i) {
  61
+        s = v[i];
  62
+        print(string(i));
  63
+        if(s == null) {
  64
+            say(' (null)');
  65
+        } else {
  66
+            say(' "' + escape(s) + '"');
  67
+            sc_map[s] = i;
  68
+        }
  69
+    }
  70
+    say(".end\n");
  71
+
  72
+    say('.constants pmc');
  73
+    v = packfile.pmcs;
  74
+    var sub_map = {};
  75
+    for (i = 0; i < elements(v); ++i) {
  76
+        vi = v[i];
  77
+        print(string(i) + ' ' + string(typeof(vi)) + ' ');
  78
+        switch(typeof(vi)) {
  79
+        case 'Sub':
  80
+            // label, name, options
  81
+            sub_map[vi] = s = '_sub' + string(i);
  82
+            print(s + ', sc'+ string(sc_map[string(vi)]));
  83
+            // TODO: main, tags, subid, multi, etc
  84
+            say();
  85
+            break;
  86
+
  87
+        default:
  88
+            try {
  89
+                say(get_repr(vi));
  90
+            } catch() {
  91
+                try {
  92
+                    say(string(vi));
  93
+                } catch() {
  94
+                    say('(Unprintable)');
  95
+                }
  96
+            }
  97
+        }
  98
+    }
  99
+    say(".end\n");
  100
+
  101
+    // Oplibs
  102
+    for (s in packfile.oplibs)
  103
+        if (s != 'core_ops')
  104
+            say('.oplib ' + s);
  105
+
  106
+    // Subs
  107
+    show_namespace(sub_map, sc_map, packfile.root);
  108
+}
  109
+
  110
+function show_namespace(var sub_map, var sc_map, var ns, var path = []) {
  111
+    for (string s in ns.contents) {
  112
+        var v = ns.contents[s];
  113
+        switch(typeof(v)) {
  114
+        case 'PACT;Packfile;Subroutine':
  115
+            say('.sub ' + string(sub_map[v.name]));
  116
+            show_sub(sc_map, v);
  117
+            say('.end');
  118
+            say();
  119
+            break;
  120
+        case 'PACT;Packfile;Namespace':
  121
+            push(path, sc_map[s]);
  122
+            print('.namespace sc');
  123
+            print(path[0]);
  124
+            for(int i = 1; i < elements(path); ++i) {
  125
+                print(', sc');
  126
+                print(path[i]);
  127
+            }
  128
+            say("\n"); // Yes, two newlines
  129
+            show_namespace(sub_map, sc_map, v);
  130
+            break;
  131
+        default:
  132
+            die('Object of unexpected type (' + string(typeof(v))
  133
+                        + ') found in namespace');
  134
+        }
  135
+    }
  136
+}
  137
+
  138
+function show_sub(var sc_map, var sub) {
  139
+    string label;
  140
+    for(var op in sub.ops) {
  141
+        switch(typeof(op)) {
  142
+        case 'PACT;Packfile;Debug':
  143
+            say('.debug sc' + string(sc_map[op.filename]));
  144
+            break;
  145
+        case 'PACT;Packfile;Annotation':
  146
+            print('.annotate sc' + string(sc_map[op.name]) + ', ');
  147
+            var c = op.value;
  148
+            switch(typeof(c)) {
  149
+            case 'PACT;Packfile;Constant;Reference':
  150
+                if(c.type != PARROT_ARG_STRING)
  151
+                    die("Unexpected annotation constant reference type " +
  152
+                            c.type);
  153
+                say('sc' + string(c.value));
  154
+                break;
  155
+
  156
+            case 'PACT;Packfile;Constant':
  157
+                switch(c.type) {
  158
+                case PARROT_ARG_INTVAL:
  159
+                    say(c.value);
  160
+                    break;
  161
+                case PARROT_ARG_STRING:
  162
+                    say('sc' + string(sc_map[c.value]));
  163
+                    break;
  164
+                default:
  165
+                    die("Unexpected annotation constant type " + c.type);
  166
+                }
  167
+                break;
  168
+
  169
+            default:
  170
+                die("Unexpected annotation value type " + string(typeof(c)));
  171
+            }
  172
+            break;
  173
+
  174
+        case 'PACT;Packfile;Label':
  175
+            if (label != null)
  176
+                say(label + ':');
  177
+
  178
+            if (length(op.name) < 7)
  179
+                label = op.name;
  180
+            else
  181
+                say(string(op.name) + ':');
  182
+
  183
+            break;
  184
+
  185
+        case 'PACT;Packfile;Op':
  186
+            print(label + ":\t");
  187
+            label = null;
  188
+            print(op.name);
  189
+
  190
+            var args = op.args;
  191
+            for(int i = 0; i < elements(args); ++i) {
  192
+                print( i == 0 ? ' ' : ', ' );
  193
+                var arg = args[i];
  194
+                switch(typeof(arg)) {
  195
+                case 'PACT;Packfile;Constant;Reference':
  196
+                    switch(arg.type) {
  197
+                    case PARROT_ARG_INTVAL:   die('Integer reference?');
  198
+                    case PARROT_ARG_STRING:   print('sc'); break;
  199
+                    case PARROT_ARG_PMC:      print('pc'); break;
  200
+                    case PARROT_ARG_FLOATVAL: print('fc'); break;
  201
+                    default: die('Unknown constref type ' + string(arg.type));
  202
+                    }
  203
+                    print(arg.value);
  204
+                    break;
  205
+
  206
+                case 'PACT;Packfile;Constant':
  207
+                    switch(arg.type) {
  208
+                    case PARROT_ARG_INTVAL:
  209
+                        print(arg.value);
  210
+                        break;
  211
+
  212
+                    case PARROT_ARG_STRING:
  213
+                        print('sc' + string(sc_map[op.value]));
  214
+                        break;
  215
+
  216
+                    default:
  217
+                        die('Unexpected constant type ' + string(arg.type));
  218
+                    }
  219
+                    break;
  220
+
  221
+                case 'PACT;Packfile;Register':
  222
+                    switch(arg.type) {
  223
+                    case PARROT_ARG_INTVAL:   print('i'); break;
  224
+                    case PARROT_ARG_STRING:   print('s'); break;
  225
+                    case PARROT_ARG_PMC:      print('p'); break;
  226
+                    case PARROT_ARG_FLOATVAL: print('f'); break;
  227
+                    default: die('Unknown register type ' + string(arg.type));
  228
+                    }
  229
+                    print(arg.number);
  230
+                    break;
  231
+
  232
+                default:
  233
+                    die('Unknown argument type ' + string(typeof(arg)));
  234
+                }
  235
+            }
  236
+
  237
+            say();
  238
+            break;
  239
+
  240
+        default:
  241
+            die("Unknown sub contents type " + string(typeof(op)));
  242
+        }
  243
+    }
33 244
 }

0 notes on commit 47b2f25

Please sign in to comment.
Something went wrong with that request. Please try again.