code loader moved to VM source
[awish.git] / tools / labellist.bob
blob6d37daa8778ca6c65ed5a73c9ee4e59d2f42a0d0
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. */
10 require("fileex");
13 define abort (msg) {
14   writeln("ERROR: ", msg);
15   quit(1);
19 LB_GVAR = 0;
20 LB_TVAR = 1;
21 LB_SVAR = 2;
22 LB_CODE = 3;
23 LB_CONST = 4;
26 types = \[
27   \["GVAR", "vmFindGVarIndex"],
28   \["TVAR", "vmFindTVarIndex"],
29   \["SVAR", "vmFindSVarIndex"],
30   \["CODE", "vmFindPC"],
31   \["CONST", "vmFindConst"]
35 list = \[];
38 fl = new File("awish.vmd", "rb");
39 if (!fl) abort("no code file");
40 sign = fl.readBuf(4);
41 if (sign != "AVM1") abort("invalid code file");
42 csize = fl.readWord()^0x2a2a; // code size
43 rcnt = fl.readWord()^0x2a2a; // number of fixups
44 elcnt = fl.readWord()^0x2a2a; // number of extern labels
45 lcnt = fl.readWord()^0x2a2a; // number of labels
46 writeln("code: %d bytes, %d public labels, %d relocations, %d externs".format(csize, lcnt, rcnt, elcnt));
47 fl.position += csize; // skip code
48 fl.position += rcnt*2; // skip fixups
49 // skip externals
50 for (local f = 0; f < elcnt; ++f) {
51   local nlen, rcnt;
52   //
53   fl.position += 1; // type
54   nlen = fl.getc()^0x2a;
55   fl.position += nlen;
56   rcnt = fl.readWord()^0x2a2a;
57   fl.position += rcnt*3;
60 // read labels
61 for (local f = 0; f < lcnt; ++f) {
62   local typet = fl.getc()^0x2a, value, name, nlen, vname, v, type = typet&0x7f;
63   //
64   if (type == LB_GVAR || type == LB_TVAR) {
65     value = fl.getc()^0x2a;
66   } else if (type == LB_CODE || type == LB_SVAR) {
67     value = fl.readWord()^0x2a2a;
68   } else if (type == LB_CONST) {
69     value = fl.readWord()^0x2a2a;
70     if (value >= 32768) value -= 65536;
71   } else {
72     abort("invalid code file: "+type.toString());
73   }
74   nlen = fl.getc()^0x2a;
75   name = fl.readBuf(nlen);
76   for (local f = 0; f < name.length; ++f) name[f] ^= 0x2a;
77   vname = ""+name;
78   for (local f = 0; f < vname.length; ++f) {
79     local ch = vname[f];
80     //
81     if (ch != '_') {
82       if (ch < '0' || (ch > '9' && ch < 'A') || (ch > 'Z' && ch < 'a')) ch = '_';
83     }
84     vname[f] = ch;
85   }
86   vname = types[type][0]+"_"+vname.toUpper();
87   if ((typet&0x80) > 0) {
88     v = \[type, name, vname];
89     list.push(v);
90   }
93 fl.close();
96 fo = new File("vm_gamelabels.h", "w");
97 foreach (local v with list.items()) {
98   fo.writeln("extern int %s;".format(v[2]));
100 fo.close();
102 fo = new File("vm_gamelabelsdef.c", "w");
103 foreach (local v with list.items()) {
104   fo.writeln("int %s;".format(v[2]));
106 fo.close();
108 fo = new File("vm_gamelabelsinit.c", "w");
109 foreach (local v with list.items()) {
110   fo.writeln("  %s = %s(\"%s\");".format(v[2], types[v[0]][1], v[1]));
112 fo.close();