-
Notifications
You must be signed in to change notification settings - Fork 5
/
ProcCall.pas
117 lines (103 loc) · 2.59 KB
/
ProcCall.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
unit ProcCall;
interface
uses
Classes, Types, Generics.Collections, CodeElement, ProcDeclaration, WriterIntf;
type
TProcCall = class(TCodeElement)
private
FParameters: TObjectList<TCodeElement>;
FProcDeclaration: TProcDeclaration;
FIgnoreResult: Boolean;
public
constructor Create();
destructor Destroy(); override;
procedure GetDCPUSource(AWriter: IWriter); override;
function GetWordSizeOfLocals(): Integer;
property ProcDeclaration: TProcDeclaration read FProcDeclaration write FProcDeclaration;
property Parameters: TObjectList<TCodeElement> read FParameters write FParameters;
property IgnoreResult: Boolean read FIgnoreResult write FIgnoreResult;
end;
implementation
uses
Optimizer, SysUtils, VarDeclaration;
{ TProcCall }
constructor TProcCall.Create;
begin
inherited Create('');
FParameters := TObjectList<TCodeElement>.Create();
FIgnoreResult := False;
end;
destructor TProcCall.Destroy;
begin
FParameters.Free;
inherited;
end;
procedure TProcCall.GetDCPUSource;
var
i, LMax: Integer;
LRegisters: string;
begin
AWriter.AddMapping(Self);
LRegisters := 'abc';
LMax := FParameters.Count;
if LMax > 3 then
begin
LMax := 3;
end;
for i := 1 to LMax do
begin
AWriter.Write('set push, ' + LRegisters[i]);
end;
for i := FParameters.Count - 1 downto 0 do
begin
Self.FSource.Clear();
FParameters.Items[i].GetDCPUSource(Self);
case i of
0:
begin
Self.Write('set a, pop');
end;
1:
begin
Self.Write('set b, pop');
end;
2:
begin
Self.Write('set c, pop');
end;
end;
OptimizeDCPUCode(Self.FSource, Self.FSource);
AWriter.WriteList(Self.FSource);
end;
AWriter.Write('jsr ' + ProcDeclaration.Name);
if FParameters.Count-3 > 0 then
begin
AWriter.Write('add sp, ' + IntToStr(FParameters.Count-3));
end;
if ProcDeclaration.IsFunction and (not FIgnoreResult) then
begin
AWriter.Write('set x, a');
end;
for i := LMax downto 1 do
begin
AWriter.Write('set ' + LRegisters[i] + ', pop');
end;
if ProcDeclaration.IsFunction and (not FIgnoreResult) then
begin
AWriter.Write('set push, x');
end;
end;
function TProcCall.GetWordSizeOfLocals: Integer;
var
LElement: TCodeElement;
begin
Result := 0;
for LElement in FParameters do
begin
if TVarDeclaration(LElement).IsLocal then
begin
Result := Result + TVarDeclaration(LElement).DataType.GetRamWordSize();
end;
end;
end;
end.