2 /* coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
3 * Understanding is not required. Only obedience.
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. */
13 writeln("ERROR: ", msg);
26 \["GVAR", "vmFindGVarIndex"],
27 \["TVAR", "vmFindTVarIndex"],
28 \["SVAR", "vmFindSVarIndex"],
29 \["CODE", "vmFindPC"],
30 \["CONST", "vmFindConst"]
37 fl = new File(args.length<1?"awish.vmd":args[0], "rb");
38 if (!fl) abort("no code file");
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
51 for (local f = 0; f < elcnt; ++f) {
54 fl.position += 1; // type
55 nlen = fl.getc()^0x2a;
57 rcnt = fl.readWord()^0x2a2a;
58 fl.position += rcnt*3;
62 for (local f = 0; f < lcnt; ++f) {
63 local typet = fl.getc()^0x2a, value, name, nlen, vname, v, type = typet&0x7f;
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;
73 abort("invalid code file: "+type.toString());
75 nlen = fl.getc()^0x2a;
76 name = fl.readBuf(nlen);
77 for (local f = 0; f < name.length; ++f) name[f] ^= 0x2a;
79 for (local f = 0; f < vname.length; ++f) {
83 if (ch < '0' || (ch > '9' && ch < 'A') || (ch > 'Z' && ch < 'a')) ch = '_';
87 vname = types[type][0]+"_"+vname.toUpper();
88 if ((typet&0x80) > 0) {
89 v = \[type, name, vname];
97 fo = new File("vm_gamelabels.h", "w");
98 foreach (local v with list.items()) {
99 fo.writeln("extern int %s;".format(v[2]));
103 fo = new File("vm_gamelabelsdef.c", "w");
104 foreach (local v with list.items()) {
105 fo.writeln("int %s;".format(v[2]));
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]));