-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmkoptbl.pas
84 lines (74 loc) · 1.93 KB
/
mkoptbl.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
program mkoptbl;
{
this reads the source of the vm
and generates an optable unit.
kind of a lot of work to avoid
copying and pasting. :)
}
const
ipath = 'ub4.pas';
opath = 'ub4ops.pas';
var
line : string;
optbl : array[$80..$FF] of string[2];
function match(have, want:string) : boolean;
var i : byte; sofar : boolean;
begin
i := 1;
sofar := length(want) <= length(have);
while sofar and (i < length(want)) do
begin
sofar := have[i] = want[i];
inc(i);
end;
match := sofar;
end;
function unhex(c : char) : byte;
const hexits = '0123456789ABCDEF';
var i : byte;
begin
unhex := 0;
for i := 0 to 15 do if c = hexits[i+1] then unhex := i
end;
procedure readops;
var i, op : byte;
begin
fillchar(optbl, sizeof(optbl), 0);
assign(input, ipath);
reset(input);
repeat readln(line) until match(line, 'procedure runop');
repeat readln(line) until match(line, ' else case');
repeat
readln(line);
{ lines we want look like: }
{ ' $00 : (xx) ... ' }
{ #123456789012 }
if line[7] = '$' then
begin
op := 16 * unhex(line[8]) + unhex(line[9]);
for i := 1 to 2 do
if line[13+i] <> ' ' then
begin
optbl[op][i] := line[13+i];
inc(optbl[op][0]); { length byte }
end
end
until match(line, ' end');
end;
var i : byte; out : text;
begin
readops;
assign(out, opath);
rewrite(out);
writeln(out, '{-- do not edit! regenerate with mkoptbl.pas --}');
writeln(out, 'unit ub4ops;');
writeln(out, 'interface');
writeln(out, ' type opstring = string[2];');
writeln(out, ' var optbl : array[ $80 .. $FF ] of opstring;');
writeln(out, 'implementation');
writeln(out, 'begin');
for i := $80 to $FF do
writeln(out, ' optbl[', i:2, '] := ''', optbl[ i ], ''';');
writeln(out, 'end.');
close(out);
end.