-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCILHLOp.pas
More file actions
439 lines (373 loc) · 8.95 KB
/
Copy pathCILHLOp.pas
File metadata and controls
439 lines (373 loc) · 8.95 KB
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
unit CILHLOp;
interface
uses
Classes, SysUtils,
CILExpr,
CILCtrlFlowGraph,
DCU_Out,
DCURecs,
CILOpCode ;
type
TCILCondition = class(TCILExpr)
protected
FName: String;
FA, FB: TCILExpr;
public
constructor Create(AA,AB: TCILExpr;AName: String);
function AsString(BrRq: boolean): String; override;
procedure Show(BrRq: boolean);
function Neg: TCILCondition;
end;
TCILCall = class(TCILExpr)
protected
FName: String;
FArgs: TList;
public
constructor Create(AName: String; AArgs: TList);
function AsString(BrRq: boolean): String;override;
procedure Show(BrRq: boolean);override;
end;
TCILGoTo = class(TCILExpr)
protected
FCond: TCILCondition;
FLAbel: TCILLabel;
public
constructor Create(ACond: TCILExpr; ALabel: TCILLabel);
function AsString(BrRq: boolean): String; override;
procedure Show(BrRq: boolean); override;
property Cond: TCILCondition read FCond;
end;
TCILGoToUnCond = class(TCILExpr)
protected
FLAbel: TCILLabel;
public
constructor Create(ALabel: TCILLabel);
function AsString(BrRq: boolean): String; override;
procedure Show(BrRq: boolean); override;
end;
TCILCase = class(TCILExpr)
public
constructor Create;
function AsString(BrRq: boolean): String; override;
procedure Show(BrRq: boolean);override;
end;
TCILCaseSt = class(TCILExpr)
protected
FExpr: TCILExpr;
FSelector: TList;
FByteCode: TCILOpCode;
public
constructor Create(AExpr: TCILExpr; ASelector: TList; AByteCode: TCILOpCode);
function AsString(BrRq: boolean): String; override;
procedure Show(BrRq: boolean); override;
end;
TCILWhileSt = class(TCILExpr)
protected
FBody: TCtrlFlowNode;
FCond: TCILCondition;
public
constructor Create(ACond: TCILCondition; Abody: TCtrlFlowNode);
function AsString(BrRq: boolean): String; override;
procedure Show(BrRq: boolean); override;
property Cond: TCILCondition read FCond;
end;
TCILRepeatSt = class(TCILExpr)
protected
FBody: TCtrlFlowNode;
FCond: TCILCondition;
public
constructor Create(ACond: TCILCondition; Abody: TCtrlFlowNode);
function AsString(BrRq: boolean): String; override;
procedure Show(BrRq: boolean); override;
property Cond: TCILCondition read FCond;
end;
TCILIfThenElseBlock = class(TCILExpr)
protected
FTrue, FFalse: TCtrlFlowNode;
FCond: TCILCondition;
public
constructor Create(ACond: TCILCondition; ATrue, AFalse: TCtrlFlowNode);
function AsString(BrRq: boolean): String; override;
procedure Show(BrRq: boolean); override;
property Cond: TCILCondition read FCond;
end;
TCILIfThenBlock = class(TCILExpr)
protected
FTrue: TCtrlFlowNode;
FCond: TCILCondition;
public
constructor Create(ACond: TCILCondition; ATrue: TCtrlFlowNode);
function AsString(BrRq: boolean): String; override;
procedure Show(BrRq: boolean); override;
property Cond: TCILCondition read FCond;
end;
implementation
{ TCILCondition. }
constructor TCILCondition.Create(AA, AB: TCILExpr; AName: String);
begin
FA:= AA;
FB:= AB;
FName:= AName;
end;
function TCILCondition.AsString(BrRq: boolean): String;
begin
if (FA = nil) or (FB = nil) then
Exit;
Result:= Format('(%s %s %s)',[FA.AsString(true), FName, FB.AsString(true)]);
end;
procedure TCILCondition.Show(BrRq: boolean);
begin
PutS(AsString(BrRq));
end;
function TCILCondition.Neg: TCILCondition;
var
Name: String;
begin
if FName = '=<' then Name:= '>';
if FName = '>=' then Name:= '<';
if FName = '<' then Name:= '>=';
if FName = '>' then Name:= '=<';
if FName = '=' then Name:= '<>';
if FName = '<>' then
Name:= '=';
Result:= TCILCondition.Create(FA,FB, Name);
end;
{ TCILIfThenElseBlock. }
function TCILIfThenElseBlock.AsString(BrRq: boolean): String;
begin
Result:= Format('if %s then begin %s end else begin %s end;',[FCond.AsString(false), FTrue.GetStr, FFalse.GetStr]);
end;
procedure TCILIfThenElseBlock.Show(BrRq: boolean);
begin
PutKW('if');
if (FCond=nil) then
PutKW('false')
else
PutS(FCond.AsString(false));
PutKW(' then');
if (FTrue<>nil) then begin
if (FTrue.LinesCnt>1) {or (TInstruction(FTrue.Last).Expr.ClassType = TCILIfThenElseBlock) or
(TInstruction(FTrue.Last).Expr.ClassType = TCILWhileSt) }
then
PutKW('begin ');
ShiftNLOfs(2);
FTrue.Show;
ShiftNLOfs(-2);NL;
if (FTrue.LinesCnt>1){ or (TInstruction(FTrue.Last).Expr.ClassType = TCILIfThenElseBlock) or
(TInstruction(FTrue.Last).Expr.ClassType = TCILWhileSt) }
then
if FFalse=nil then
PutKW('end;')
else
PutKW('end');
end;
if FFalse<>nil then begin
PutKW('else');
if (FFalse.LinesCnt>1) {or (TInstruction(FFalse.Last).Expr.ClassType = TCILIfThenElseBlock) or
(TInstruction(FTrue.Last).Expr.ClassType = TCILWhileSt)}
then
PutKW('begin ');
ShiftNLOfs(2);
FFalse.Show;
ShiftNLOfs(-2);
NL;
if (FFalse.LinesCnt>1) {or (TInstruction(FFalse.Last).Expr.ClassType = TCILIfThenElseBlock) or
(TInstruction(FTrue.Last).Expr.ClassType = TCILWhileSt) }
then
PutKW('end;');
end;
end;
constructor TCILIfThenElseBlock.Create(ACond: TCILCondition; ATrue, AFalse: TCtrlFlowNode);
begin
FTrue:= ATrue;
FFalse:= AFalse;
FCond:= ACond;
end;
{ TCILIfThenBlock }
function TCILIfThenBlock.AsString(BrRq: boolean): String;
begin
end;
constructor TCILIfThenBlock.Create(ACond: TCILCondition; ATrue: TCtrlFlowNode);
begin
FTrue:= ATrue;
FCond:= ACond;
end;
procedure TCILIfThenBlock.Show(BrRq: boolean);
begin
PutKW('if');
if (FCond=nil) then
PutKW('false')
else
PutS(FCond.AsString(false));
PutKW(' then ');
if (FTrue.LinesCnt>1) then
PutKW('begin ');
ShiftNLOfs(2);
FTrue.Show;
ShiftNLOfs(-2);NL;
if (FTrue.LinesCnt>1) then
PutKW('end ');
end;
{ TCILGoTo }
constructor TCILGoTo.Create(ACond: TCILExpr; ALabel: TCILLabel);
begin
FCond:= TCILCondition(ACond);
FLAbel:= ALabel;
end;
function TCILGoTo.AsString(BrRq: boolean): String;
begin
end;
procedure TCILGoTo.Show(BrRq: boolean);
begin
PutKW('if');
if (FCond=nil) then
PutKW('false')
else
PutS(FCond.AsString(false));
PutKW(' then ');
ShiftNLOfs(2);
NL;
PutKW('goto ');FLAbel.Show(false);
ShiftNLOfs(-2);
end;
{ TCILGoToUnCond }
constructor TCILGoToUnCond.Create(ALabel: TCILLabel);
begin
FLAbel:= ALabel;
end;
function TCILGoToUnCond.AsString(BrRq: boolean): String;
begin
Result:= 'goto ' + FLAbel.AsString(false);
end;
procedure TCILGoToUnCond.Show(BrRq: boolean);
begin
PutKW('goto ');FLAbel.Show(false);
end;
{ TCILRepeatSt. }
constructor TCILRepeatSt.Create(ACond: TCILCondition; ABody: TCtrlFlowNode);
begin
FCond:= ACond;
FBody:= ABody;
end;
function TCILRepeatSt.AsString(BrRq: boolean): String;
begin
end;
procedure TCILRepeatSt.Show(BrRq: boolean);
begin
PutKW('repeat');
ShiftNLOfs(2);
if FBody <> nil then
FBody.Show;
ShiftNLOfs(-2);
NL;
PutKW('until');
if FCond <> nil then
PutS(FCond.AsString(false));
PutS(';');
end;
{ TCILWhileSt. }
constructor TCILWhileSt.Create(ACond: TCILCondition; ABody: TCtrlFlowNode);
begin
FCond:= ACond;
FBody:= ABody;
end;
function TCILWhileSt.AsString(BrRq: boolean): String;
begin
end;
procedure TCILWhileSt.Show(BrRq: boolean);
begin
PutKW('while');
if FCond <> nil then
PutS(FCond.AsString(false))
else
PutS('true');
if (FBody.LinesCnt > 1) then
PutKW(' do begin')
else
PutKW('do');
ShiftNLOfs(2);
if FBody <> nil then
FBody.Show;
ShiftNLOfs(-2);
NL;
PutKW('end;');
end;
{ TCILCaseSt. }
constructor TCILCaseSt.Create(AExpr: TCILExpr; ASelector: TList; AByteCode: TCILOpCode);
begin
FExpr:= AExpr;
FSelector:= ASelector;
FByteCode:= AByteCode;
end;
function TCILCaseSt.AsString(BrRq: boolean): String;
begin
end;
procedure TCILCaseSt.Show(BrRq: boolean);
var
i: integer;
begin
PutKW('case ');
FExpr.Show(false);
PutKW('of');
NL;
ShiftNLOfs(2);
for i:=1 to FByteCode.ArgCnt do begin
PutSFmt('%d',[FByteCode.SArgs[i]]);
PutS(': ');
if TCtrlflowNode(FSelector.Items[i]).LinesCnt > 1 then
PutKW('begin');
end;
ShiftNLOfs(-2);
end;
{ TCILCase }
function TCILCase.AsString(BrRq: boolean): String;
begin
end;
constructor TCILCase.Create;
begin
end;
procedure TCILCase.Show(BrRq: boolean);
begin
inherited;
end;
{ TCILCall. }
constructor TCILCall.Create(AName: String; AArgs: TList);
begin
FName:= AName;
FArgs:= AArgs;
end;
function TCILCall.AsString(BrRq: boolean): String;
var
i: integer;
TmpStr, Sep: String;
begin
if FArgs=nil then begin
Result:= Format('%s()',[FName]);
Exit;
end;
TmpStr:= '';
Sep:= '';
for i:=FArgs.Count-1 downto 0 do begin
TmpStr:= TmpStr+Sep+TCILArg(FArgs.Items[i]).AsString(false);
Sep:= ', ';
end;
Result:= Format('%s(%s)',[FName,TmpStr]);
end;
procedure TCILCall.Show(BrRq: boolean);
var
i: integer;
TmpStr, Sep: String;
begin
if FArgs=nil then begin
PutS(Format('%s()',[FName]));
Exit;
end;
TmpStr:= '';
Sep:= '';
for i:=FArgs.Count-1 downto 0 do begin
TmpStr:= TmpStr+Sep+TCILArg(FArgs.Items[i]).AsString(false);
Sep:= ', ';
end;
PutS(Format('%s(%s)',[FName,TmpStr]));
end;
end.