/
Main.p
418 lines (378 loc) · 9.27 KB
/
Main.p
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
Program PCQ_Pascal;
{
PCQ Pascal Compiler
Copyright (c) 1989 Patrick Quaid.
This is the main file of the compiler. When this file is
compiled, it allocates BSS for all the global variables.
}
{$O-}
{$I "Pascal.i"}
{$I "Include/Utils/StringLib.i"}
{$I "Include/Utils/Parameters.i"}
{ The following routines are all exported by the other
compiler files. }
Procedure Error(s : string);
external;
Function CheckID(s : string): IDPtr;
external;
Function EnterStandard( st_Name : String;
st_Object : IDObject;
st_Type : TypePtr;
st_Storage : IDStorage;
st_Offset : Integer) : IDPtr;
external;
Procedure NextSymbol;
external;
Function Match(s : Symbols): Boolean;
external;
Procedure DeclType;
external;
Procedure DeclConst;
external;
Procedure DeclLabel;
External;
Function DeclArgs(ob : IDObject) : IDPtr;
external;
Procedure ns;
external;
Procedure EnterID(CB : BlockPtr; ID : IDPtr);
external;
Procedure ReformArgs(ID : IDPtr);
external;
Function ReadType(): TypePtr;
external;
Function EndOfFile(): boolean;
external;
Function OpenInputFile(n : String) : Boolean;
external;
Procedure CloseInputFile;
external;
Procedure VarDeclarations;
external;
Procedure InitReserved;
external;
Procedure InitGlobals;
external;
Function GetLabel() : Integer;
External;
Procedure DumpIds;
external;
Procedure DumpRefs;
external;
Procedure DumpLits;
external;
Procedure Trailer;
external;
Procedure Compound;
external;
Procedure Header;
external;
Procedure InitStandard;
external;
Procedure ReadChar;
external;
Procedure NeedRightParent; { Utilities.p }
external;
Function SimpleType(T : TypePtr): Boolean; { Utilities.p }
external;
Procedure NewBlock; { Utilities.p }
external;
Procedure KillBlock; { Utilities.p }
external;
Procedure KillIDList(ID : IDPtr); { Utilities.p }
external;
Procedure Decompose; { Utilities.p }
external;
Function CompareProcs(Proc1, Proc2 : IDPtr) : Boolean; { Utilities.p }
external;
Procedure BackUpSpell(Position : Integer);
external;
Procedure Banner;
begin
writeln('PCQ Compiler 1.1d (May 6, 1990)');
writeln('Copyright ', chr(169),
' 1989 Patrick Quaid. All rights reserved.');
end;
Procedure GetFileNames;
var
Parm : String;
ParmNum : Short;
Procedure Die(LastWords : string);
begin
Banner;
Writeln(LastWords);
Exit(20);
end;
Procedure DoOption;
begin
if toupper(Parm[1]) = 'Q' then
Inform := False
{ else if toupper(Parm[1]) = 'O' then
Do_Offsets := True }
else
Die("Unknown Directive");
end;
begin
Parm := AllocString(256);
MainName := Nil;
OutName := Nil;
ParmNum := 1;
repeat
GetParam(ParmNum, Parm);
if Parm^ = Chr(0) then begin
if MainName = Nil then
Die("No Input File Name");
if OutName = Nil then
Die("Missing Output File Name");
end else begin
if Parm^ = '-' then
DoOption
else if MainName = Nil then
MainName := strdup(Parm)
else if OutName = Nil then
OutName := strdup(Parm)
else
Die("Unknown Command");
end;
Inc(ParmNum);
until Parm^ = Chr(0);
FreeString(Parm);
end;
Procedure OpenFiles;
begin
InFile := nil;
if not OpenInputFile(MainName) then begin
Writeln('Could not open ', MainName);
Exit(20);
end;
if not open(OutName, OutFile, 2048) then begin
Writeln('Could not open ', OutName);
Exit(20);
end;
end;
Procedure DoBlock(isfunction : boolean);
{
This is the main routine for handling program, procedure
and function blocks. It handles the various declaration blocks and
the procedure and function parameters. This is one of the many
routines which should, and will, be broken into more manageable
parts.
}
var
ID : IDPtr;
DupID : IDPtr;
savefn : IDPtr;
ForwardID : IDRec;
Forwarded : Boolean;
FirstVar : IDPtr;
SaveStack : Integer;
SaveSpell : Integer;
begin
fnstart := lineno;
Forwarded := False;
FirstVar := Nil;
if CurrentBlock^.Level > 0 then begin
if currsym <> ident1 then begin
error("Missing function or procedure name!");
return;
end;
CurrFn := CheckID(symtext);
if CurrFn <> Nil then begin
if CurrFn^.Storage <> st_forward then
error("Duplicate ID")
else begin
ForwardID := CurrFn^;
Forwarded := True;
CurrFn^.Param := Nil;
end;
end else begin
case isfunction of
True : CurrFn := EnterStandard(symtext, func, Nil, st_none, 0);
False: CurrFn := EnterStandard(symtext, proc, Nil, st_none, 0);
end;
CurrFn^.Unique := GetLabel();
end;
nextsymbol;
SaveSpell := SpellPtr;
if Match(leftparent1) then begin
CurrFn^.Param := DeclArgs(valarg); { Dummy param here }
ReformArgs(CurrFn); { Set offsets of args, plus totalsize }
NeedRightParent;
end else
CurrFn^.Param := Nil;
if isfunction then begin
if not match(colon1) then
error("expecting :");
CurrFn^.VType := readtype();
if not simpletype(CurrFn^.VType) then begin
error("expecting a simple type");
CurrFn^.VType := BadType;
end;
end;
ns;
end;
if match(forward1) then begin
if Forwarded then
Error("Already declared");
CurrFn^.Storage := st_forward;
ns;
end else if Match(extern1) then begin
CurrFn^.Storage := st_external;
ns;
end else begin
if Forwarded then begin
if not CompareProcs(Adr(ForwardID), CurrFn) then
Error("Definitions do not match");
KillIDList(ForwardID.Param);
end;
NewBlock;
if CurrentBlock^.Level > 1 then begin
CurrFn^.Storage := st_internal;
ID := CurrFn^.Param;
while ID <> nil do begin
New(DupID);
DupID^ := ID^;
ID^.Name := Nil;
EnterID(CurrentBlock, DupID);
ID := ID^.Next;
end;
end;
StackSpace := 0;
while currsym <> begin1 do begin
if endoffile() then begin
if mainmode or (CurrentBlock^.Level > 1) then
error("There was no code section!");
return;
end else if match(var1) then
VarDeclarations
else if match(type1) then
DeclType
else if match(const1) then
DeclConst
else if match(proc1) then begin
savefn := currfn;
SaveStack := StackSpace;
doblock(false);
StackSpace := SaveStack;
currfn := savefn;
end else if match(func1) then begin
savefn := currfn;
SaveStack := StackSpace;
doblock(true);
StackSpace := SaveStack;
currfn := savefn;
end else if match(label1) then
DeclLabel
else begin
error("expecting block identifier");
nextsymbol;
end;
end;
if CurrentBlock^.Level > 1 then begin
if odd(StackSpace) then
StackSpace := Succ(StackSpace);
CurrFn^.Offset := StackSpace;
end;
if (not mainmode) and (CurrentBlock^.Level = 1) then begin
error("Expected a procedure or function header");
return;
end;
case CurrentBlock^.Level of
1 : if MainMode then begin
writeln(OutFile, '_MAIN');
end;
2 : begin
if StandardStorage <> st_private then
writeln(OutFile, "\n\tXDEF\t_", CurrFn^.Name);
writeln(OutFile, '_', CurrFn^.Name, "\tlink\ta5,#",
-CurrFn^.Offset);
end;
else
Writeln(OutFile, '_', CurrFn^.Name, '%', CurrFn^.Unique,
"\tlink\ta5,#", -CurrFn^.Offset);
end;
NextSymbol;
Compound;
if CurrentBlock^.Level > 1 then begin
ns;
writeln(OutFile, "\tunlk\ta5");
KillBlock;
BackUpSpell(SaveSpell);
end;
writeln(OutFile, "\trts");
end;
end;
Procedure Parse;
{
This is the outermost parsing routine. It uses doblock()
mainly, and will eventually be able to handle program parameters.
}
begin
if Match(program1) then begin
mainmode:= true;
StandardStorage := st_internal;
if currsym = ident1 then
NextSymbol { Eat program name }
else
error("Missing program name.");
if Match(LeftParent1) then begin
while CurrSym = Ident1 do begin
NextSymbol;
if CurrSym <> RightParent1 then
if not Match(Comma1) then
Error("Expecting a comma");
end;
NeedRightParent;
end;
ns;
end else if match(extern1) then begin
mainmode := false;
StandardStorage := st_external;
ns;
end else begin
error("First symbol must be PROGRAM or EXTERNAL.");
StandardStorage := st_internal;
MainMode:= false;
end;
Header;
DoBlock(false);
if MainMode then
if not Match(period1) then
Error("Program must end with a period.");
if (not EndOfFile) and (MainMode) then
Error("There should be nothing after the main procedure.");
end;
begin
{
This is the big one, the main routine, which by itself does
very little. Read parse() and doblock() to get a much better idea
of how things work.
}
initglobals; { initialize everything }
initreserved;
initstandard;
GetFileNames;
if Inform then
Banner;
OpenFiles;
nextsymbol;
parse; { do everything }
if Inform then begin
if errorcount = 0 then
writeln('There were no errors.')
else if errorcount = 1 then
writeln('There was one error')
else
writeln('There were ', errorcount, ' errors.');
end;
DumpRefs;
DumpLits;
DumpIds; { write IDs and literals to assem file }
trailer; { write 'END' }
while InFile <> nil do
CloseInputFile; { be sure to close the main file }
{ if Do_Offsets then
Decompose; Write offset information }
if errorcount <> 0 then
exit(10); { make sure there's an error if necessary }
end.