-
Notifications
You must be signed in to change notification settings - Fork 1
/
assemble.pas
388 lines (348 loc) · 8.62 KB
/
assemble.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
unit assemble;
{
Assembly unit
}
interface
uses DateUtils, Classes, SysUtils, Contnrs, parser;
type
TLabelOffset = class(TObject)
public
Name: String;
Ident, Offset: Cardinal;
constructor Create(const Name: String; const Offset: Cardinal);
end;
procedure Finalize;
procedure Init;
procedure SetOutputFile (const Fn: String);
procedure Error (const Msg: String);
procedure AsmByte(const B: Byte);
procedure AsmCardinal(const C: Cardinal);
procedure AsmIdent(const Id: String);
procedure AsmInt(const I: Integer);
procedure AsmString(const S: String);
procedure ParseTypeString;
function LocateId (const Id: String): Cardinal;
function StrConst (const S: String): Cardinal;
procedure CheckEOL;
procedure CompileLine(const S: String);
procedure WriteMetadata;
procedure AsmIntFromKeyword(const S: String);
procedure PrintStats;
procedure AsmStringRes(const S: String);
procedure AsmLabelRes(const Lbl: TLabelOffset);
implementation
const
ERROR_IDENT = 'Identifier expected';
ERROR_COMMA = 'Comma expected';
ERROR_STR_IDENT = 'String or identifier expected';
var
AsmOffset: Cardinal;
LineNum: Integer;
FOut: TFileStream;
StrTable, Idents: TStringList;
FParser: TParser;
Labels: TObjectList;
StartTime, EndTime: TDateTime;
constructor TLabelOffset.Create(const Name: String; const Offset: Cardinal);
begin
inherited Create;
Self.Name := Name;
Self.Offset := Offset;
end;
{ Internal }
procedure Init;
begin
StartTime := Now;
Idents := TStringList.Create;
StrTable := TStringList.Create;
FParser := TParser.Create;
Labels := TObjectList.Create(True);
LineNum := 1;
AsmOffset := 0;
end;
procedure WriteMetadata;
var tmpLoc, ResOffset, j: Integer;
begin
ResOffset := AsmOffset;
// Precompute label identifiers in table
// TODO: Remove this and just make second pass
j := 0;
while j < Labels.Count do begin
tmpLoc := Idents.IndexOf((Labels[j] as TLabelOffset).Name);
if tmpLoc < 0 then begin
// If the label is never identified, remove it from output
Labels.Delete(j);
end else begin
(Labels[j] as TLabelOffset).Ident := tmpLoc;
Inc(j);
end;
end;
// -- RESOURCES BEGIN --
// Write # of identifiers (VM metadata)
AsmCardinal(Idents.Count);
// Write all strings
for j := 0 to StrTable.Count-1 do begin
AsmStringRes(StrTable[j]);
end;
// Write label translation table
for j := 0 to Labels.Count-1 do begin
AsmLabelRes(Labels[j] as TLabelOffset);
end;
// -- RESOURCES END --
// NOTE: IMPORTANT
// Write ending marker for splitting opcode and resource sections
AsmCardinal(ResOffset);
end;
procedure PrintStats;
begin
EndTime := Now;
Writeln('Successfully compiled');
Writeln(Format('Input: %d lines', [LineNum-1]));
Writeln(Format('Output: %d bytes', [AsmOffset]));
Writeln(Format('Time: %d.%d seconds', [Trunc(SecondSpan(StartTime, EndTime)), Trunc(MilliSecondSpan(StartTime, EndTime))]));
Writeln;
Writeln('[Resources]');
Writeln(Format('Labels: %d', [Labels.Count]));
Writeln(Format('Strings: %d', [StrTable.Count]));
end;
procedure Finalize;
begin
FParser.Free;
Idents.Free;
StrTable.Free;
Labels.Free;
FOut.Free;
end;
procedure SetOutputFile (const Fn: String);
begin
FOut := TFileStream.Create(Fn, fmCreate or fmOpenWrite)
end;
procedure Error (const Msg: String);
begin
Writeln(Format('ERROR: Line %d:%d - %s', [LineNum, FParser.Offset+1, Msg]));
Halt;
end;
{ Assembly byte emission - compiler-level }
procedure AsmByte(const B: Byte);
begin
Inc(AsmOffset, FOut.Write(B, Sizeof(Byte)));
end;
procedure AsmCardinal(const C: Cardinal);
begin
Inc(AsmOffset, FOut.Write(C, Sizeof(Cardinal)));
end;
{ Assembly byte emission - type-identified }
procedure AsmIdent(const Id: String);
begin
AsmByte($00);
AsmCardinal(LocateId(Id));
end;
procedure AsmInt(const I: Integer);
begin
AsmByte($01);
Inc(AsmOffset, FOut.Write(I, Sizeof(Integer)));
end;
procedure AsmIntFromKeyword(const S: String);
begin
try
AsmInt(StrToInt(S));
except
on E: Exception do begin
Error(Format('"%s" is not a valid integer', [S]));
end;
end;
end;
procedure AsmString(const S: String);
var Index: Cardinal;
begin
AsmByte($02);
Index := StrConst(S);
Inc(AsmOffset, FOut.Write(Index, Sizeof(Cardinal)));
end;
procedure AsmStringRes(const S: String);
begin
AsmByte($00);
AsmCardinal(Length(S));
Inc(AsmOffset, FOut.Write(S[1], Length(S)));
end;
procedure AsmLabelRes(const Lbl: TLabelOffset);
begin
AsmByte($01);
AsmCardinal(Lbl.Ident);
AsmCardinal(Lbl.Offset);
end;
{ Parsing }
procedure ParseTypeString;
var tk: TToken;
begin
tk := FParser.Next;
if tk = tkString then begin
AsmString(FParser.Keyword);
end else if tk = tkID then begin
AsmIdent(FParser.Keyword);
end else Error(ERROR_STR_IDENT);
end;
procedure ParseTypeInt;
var tk: TToken;
begin
tk := FParser.Next;
if tk = tkInteger then begin
AsmIntFromKeyword(FParser.Keyword);
end else if tk = tkID then begin
AsmIdent(FParser.Keyword);
end else Error(ERROR_STR_IDENT);
end;
procedure ParseTypeLabel;
begin
Assert(FParser.Next = tkKeyword, ERROR_IDENT);
Labels.Add(TLabelOffset.Create(Lowercase(FParser.Keyword), AsmOffset));
end;
{ Utilities }
function LocateId (const Id: String): Cardinal;
var tmpId: String;
tmpLoc: Integer;
begin
tmpId := Lowercase(Copy(Id, 2, Length(Id)-1));
tmpLoc := Idents.IndexOf(tmpId);
if tmpLoc >= 0 then Result := tmpLoc
else Result := Idents.Add(tmpId);
end;
function StrConst (const S: String): Cardinal;
var tmpStr: String;
j: Integer;
begin
SetLength(tmpStr, Length(S)-2);
tmpStr := '';
j := 2;
while j < Length(S) do begin
if S[j] = '\' then Inc(j);
tmpStr := tmpStr + S[j];
Inc(j);
end;
Result := StrTable.Add(tmpStr);
end;
procedure CheckEOL;
begin
if FParser.Next <> tkEOL then Error('End-of-line expected');
end;
{ Compilation }
procedure CompileLine(const S: String);
begin
FParser.SetLine(S);
case FParser.Next of
tkWindowCreate:
begin
AsmByte($00);
Assert(FParser.Next = tkID, ERROR_IDENT);
AsmIdent(FParser.Keyword);
end;
tkWindowSetTitle:
begin
AsmByte($01);
Assert(FParser.Next = tkID, ERROR_IDENT);
AsmIdent(FParser.Keyword);
Assert(FParser.Next = tkComma);
ParseTypeString;
end;
tkIntCreate:
begin
AsmByte($02);
Assert(FParser.Next = tkID, ERROR_IDENT);
AsmIdent(FParser.Keyword);
end;
tkIntAdd:
begin
AsmByte($03);
Assert(FParser.Next = tkID, ERROR_IDENT);
AsmIdent(FParser.Keyword);
Assert(FParser.Next = tkComma, ERROR_COMMA);
ParseTypeInt;
end;
tkIntSet:
begin
AsmByte($04);
Assert(FParser.Next = tkID, ERROR_IDENT);
AsmIdent(FParser.Keyword);
Assert(FParser.Next = tkComma, ERROR_COMMA);
ParseTypeInt;
end;
tkBtnCreate:
begin
AsmByte($05);
Assert(FParser.Next = tkID, ERROR_IDENT);
AsmIdent(FParser.Keyword);
end;
tkBtnSetParent:
begin
AsmByte($06);
Assert(FParser.Next = tkID, ERROR_IDENT);
AsmIdent(FParser.Keyword);
Assert(FParser.Next = tkComma, ERROR_COMMA);
Assert(FParser.Next = tkID, ERROR_IDENT);
AsmIdent(FParser.Keyword);
end;
tkBtnSetText:
begin
AsmByte($07);
Assert(FParser.Next = tkID, ERROR_IDENT);
AsmIdent(FParser.Keyword);
Assert(FParser.Next = tkComma, ERROR_COMMA);
ParseTypeString;
end;
tkBtnSetOnClick:
begin
AsmByte($08);
Assert(FParser.Next = tkID, ERROR_IDENT);
AsmIdent(FParser.Keyword);
Assert(FParser.Next = tkComma, ERROR_COMMA);
// NOTE: Label is just an identifier to a block of code...
Assert(FParser.Next = tkID, ERROR_IDENT);
AsmIdent(FParser.Keyword);
end;
tkStrCreate:
begin
AsmByte($09);
Assert(FParser.Next = tkID, ERROR_IDENT);
AsmIdent(FParser.Keyword);
Assert(FParser.Next = tkComma, ERROR_COMMA);
ParseTypeString;
end;
tkStrConcat:
begin
AsmByte($0A);
Assert(FParser.Next = tkID, ERROR_IDENT);
AsmIdent(FParser.Keyword);
Assert(FParser.Next = tkComma, ERROR_COMMA);
ParseTypeString;
end;
tkConvertIntStr:
begin
AsmByte($0B);
ParseTypeInt;
Assert(FParser.Next = tkComma, ERROR_COMMA);
ParseTypeString;
end;
tkAppRun:
begin
AsmByte($0C);
Assert(FParser.Next = tkID, ERROR_IDENT);
AsmIdent(FParser.Keyword);
end;
tkColon: // Label
begin
ParseTypeLabel;
end;
tkReturn:
begin
AsmByte($0D);
end;
tkEOL:
begin
// Skip
end;
else Error('Invalid opcode');
end;
CheckEOL;
Inc(LineNum);
end;
end.