-
Notifications
You must be signed in to change notification settings - Fork 0
/
globals.pp
executable file
·246 lines (218 loc) · 4.7 KB
/
globals.pp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
unit Globals;
interface
uses
Classes;
const
MaxBranch = 16;
MaxInts = 64;
type
TName = String;
PName = String;
PCodeString = String;
TCodeType = (ctWord, ctIntLit, ctByteLit, ctQuadLit, ctASM, ctLabel, ctVar,
ctPrimitive, ctBranch, ctCondBranch, ctNone);
PWord = ^TWord;
PCodeList = ^TCodeList;
TCodeList = record
AWord: PWord;
Code: PCodeString;
Next: PCodeList;
Prev: PCodeList;
CodeType: TCodeType;
BrLabel: Integer;
Inst: Integer;
end;
TWordType = (wtWord, wtVar, wtStr, wtCG, wtConst);
TWord = record
Name: PName;
AsmName: PName;
Link: PWord;
Immediate: Boolean;
InlineFlag: Boolean;
Smugde: Boolean;
Code: PCodeList;
InUse: Boolean;
IsInt: Boolean;
CodeGen: String;
IsInit: Boolean;
SwitchWord: Boolean;
WordType: TWordType;
Used: Boolean;
Size: Integer;
end;
TBr = record
C: PCodeList;
W: PWord;
end;
TBrStack = array [1..MaxBranch] of TBr;
TInt = record
Name: PName;
AWord: PWord;
end;
TIntTable = array [1..MaxInts] of TInt;
var
LastWord: PWord;
LastCode: PCodeList;
InCompile: Boolean;
Name: TName;
MainWord: PWord;
BrSP: Integer;
BrStack: TBrStack;
GlBrLabel: Integer;
DP, StrP: Integer;
Listing: TStringList;
IntTable: TIntTable;
IntP: Integer;
Files: array [1..10] of Text;
FilesP: Integer;
LastFileName: String;
LoopDepth: Integer;
Included: TStringList;
PStr: TStringList;
DoInline: Boolean;
MainInFile: String;
MainOutFile: String;
NotFound: Boolean;
LLL: TStringList;
Depth: Integer;
ListingFile: Text;
AsmInclude: String;
const
StackSize = 64;
type
TStack = array [0 .. StackSize - 1] of Integer;
var
FullPathPrefix: String;
InitList: TStringList;
StackP: Integer;
DataStack: TStack;
function CodeTypeToStr(C: TCodeType): String;
function FindWord(Name: TName): PWord;
procedure PushData(D: Integer);
function PopData: Integer;
procedure Execute(W: PWord);
implementation
uses
Utils, SysUtils;
function CodeTypeToStr(C: TCodeType): String;
begin
case C of
ctWord: Result := 'ctWord';
ctIntLit: Result := 'ctIntLit';
ctByteLit: Result := 'ctByteLit';
ctQuadLit: Result := 'ctQuadLit';
ctASM: Result := 'ctASM';
ctLabel: Result := 'ctLabel';
ctVar: Result := 'ctVar';
ctPrimitive: Result := 'ctPrimitive';
ctBranch: Result := 'ctBranch';
ctCondBranch: Result := 'ctCondBranch';
ctNone: Result := 'ctNone';
end;
end;
function FindWord(Name: TName): PWord;
var
W: PWord;
begin
Result := nil;
W := LastWord;
while W <> nil do
begin
if not W^.Smugde then
begin
if W^.Name = Name then
begin
Result := W;
break;
end;
end;
W := W^.Link;
end;
end;
procedure PushData(D: Integer);
begin
Inc(StackP);
if StackP > StackSize - 1 then
AbortMsg('Data stack overflow');
DataStack[StackP] := D;
end;
function PopData: Integer;
begin
if StackP < 0 then
AbortMsg('Data stack underflow');
Result := DataStack[StackP];
Dec(StackP);
end;
procedure Execute(W: PWord);
var
C: PCodeList;
R0, R1, R2: Integer;
S: String;
begin
if W = nil then
AbortMsg('Null word');
C := W^.Code;
while C <> nil do
begin
case C^.CodeType of
ctWord: Execute(C^.AWord);
ctByteLit,
ctIntLit,
ctQuadLit,
ctVar: PushData(StrToInt(C^.Code));
ctPrimitive:
begin
S := C^.Code;
if (Pos('%', S) = 2) then
Delete(S, 1, 2);
if S = 'ADDBYTE' then
begin
R0 := PopData;
R1 := PopData;
R0 := R0 + R1;
end
else if S = 'SUBBYTE' then
begin
R0 := PopData;
R1 := PopData;
R0 := R1 - R0;
end
else if S = 'MULBYTE' then
begin
R0 := PopData;
R1 := PopData;
R0 := R1 * R0;
end
else if S = 'DIVMODBYTE' then
begin
R0 := PopData;
R1 := PopData;
R2 := R1;
R1 := R1 mod R0;
R0 := R2 div R0;
end
else if S = 'SWAPBYTE' then
begin
R0 := PopData;
R1 := PopData;
end
else if S = 'PUSH_B0' then
PushData(R0)
else if S = 'PUSH_B1' then
PushData(R1)
else if S = 'PUSH_B2' then
PushData(R2)
else if S = 'DROPBYTE' then
PopData
else
AbortMsg('Can not execute primitive ' + S);
end;
else
AbortMsg('Can not execute code type ' + CodeTypeToStr(C^.CodeType));
end;
C := C^.Next;
end;
end;
begin
StackP := -1;
end.