tools fixed
[awish.git] / tools / labellist.bob
blob7bf1ad5751c2deb70bb8cba5c9883ffd08169a8a
1 #!/usr/bin/bob
2 /* coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
3  * Understanding is not required. Only obedience.
4  *
5  * This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://sam.zoy.org/wtfpl/COPYING for more details. */
12 define abort (msg) {
13   writeln("ERROR: ", msg);
14   quit(1);
18 LB_GVAR = 0;
19 LB_TVAR = 1;
20 LB_SVAR = 2;
21 LB_CODE = 3;
22 LB_CONST = 4;
25 types = \[
26   \["GVAR", "vmFindGVarIndex"],
27   \["TVAR", "vmFindTVarIndex"],
28   \["SVAR", "vmFindSVarIndex"],
29   \["CODE", "vmFindPC"],
30   \["CONST", "vmFindConst"]
34 list = \[];
37 fl = new File(args.length<1?"awish.vmd":args[0], "rb");
38 if (!fl) abort("no code file");
39 sign = fl.readBuf(4);
40 if (sign != "AVM2") abort("invalid code file");
41 csize = fl.readWord()^0x2a2a; // code size
42 rcnt = fl.readWord()^0x2a2a; // number of fixups
43 elcnt = fl.readWord()^0x2a2a; // number of extern labels
44 lcnt = fl.readWord()^0x2a2a; // number of labels
45 gvmax = fl.readWord()^0x2a2a; // number of global vars
46 tvmax = fl.readWord()^0x2a2a; // number of thread local vars
47 writeln("code: %d bytes, %d public labels, %d relocations, %d externs".format(csize, lcnt, rcnt, elcnt));
48 fl.position += csize; // skip code
49 fl.position += rcnt*2; // skip fixups
50 // skip externals
51 for (local f = 0; f < elcnt; ++f) {
52   local nlen, rcnt;
53   //
54   fl.position += 1; // type
55   nlen = fl.getc()^0x2a;
56   fl.position += nlen;
57   rcnt = fl.readWord()^0x2a2a;
58   fl.position += rcnt*3;
61 // read labels
62 for (local f = 0; f < lcnt; ++f) {
63   local typet = fl.getc()^0x2a, value, name, nlen, vname, v, type = typet&0x7f;
64   //
65   if (type == LB_GVAR || type == LB_TVAR) {
66     value = fl.getc()^0x2a;
67   } else if (type == LB_CODE || type == LB_SVAR) {
68     value = fl.readWord()^0x2a2a;
69   } else if (type == LB_CONST) {
70     value = fl.readWord()^0x2a2a;
71     if (value >= 32768) value -= 65536;
72   } else {
73     abort("invalid code file: "+type.toString());
74   }
75   nlen = fl.getc()^0x2a;
76   name = fl.readBuf(nlen);
77   for (local f = 0; f < name.length; ++f) name[f] ^= 0x2a;
78   vname = ""+name;
79   for (local f = 0; f < vname.length; ++f) {
80     local ch = vname[f];
81     //
82     if (ch != '_') {
83       if (ch < '0' || (ch > '9' && ch < 'A') || (ch > 'Z' && ch < 'a')) ch = '_';
84     }
85     vname[f] = ch;
86   }
87   vname = types[type][0]+"_"+vname.toUpper();
88   if ((typet&0x80) > 0) {
89     v = \[type, name, vname];
90     list.push(v);
91   }
94 fl.close();
97 fo = new File("vm_gamelabels.h", "w");
98 foreach (local v with list.items()) {
99   fo.writeln("extern int %s;".format(v[2]));
101 fo.close();
103 fo = new File("vm_gamelabelsdef.c", "w");
104 foreach (local v with list.items()) {
105   fo.writeln("int %s;".format(v[2]));
107 fo.close();
109 fo = new File("vm_gamelabelsinit.c", "w");
110 foreach (local v with list.items()) {
111   fo.writeln("  %s = %s(\"%s\");".format(v[2], types[v[0]][1], v[1]));
113 fo.close();