-
Notifications
You must be signed in to change notification settings - Fork 5
/
D16Writer.pas
173 lines (157 loc) · 4.45 KB
/
D16Writer.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
unit D16Writer;
interface
uses
Classes, Types, Generics.Collections, UncountedInterfacedObject, LineMapping, VarMapping, WriterIntf,
VarDeclaration, CodeELement, RoutineMapping, ProcDeclaration;
type
TD16Writer = class(TUncountedInterfacedObject, IWriter)
private
FLineMapping: TObjectList<TLineMapping>;
FSource: TStringList;
FCurrentUnit: string;
procedure AddRoutineMapping(AProc: TProcDeclaration; AOffset: Integer =0);
procedure AddVarMapping(AVar: TVarDeclaration; AOffset: Integer = 0);
procedure AddLineMapping(AElement: TCodeElement; AOffset: Integer = 0);
public
constructor Create();
destructor Destroy(); override;
procedure Write(ALine: string);
procedure WriteList(AList: TStrings);
procedure AddMapping(AElement: TObject; AOffset: Integer = 0; AHideName: Boolean = False);
function GetMappingByASMLine(ALine: Integer): TLineMapping;
property LineMapping: TObjectList<TLineMapping> read FLineMapping;
property Source: TStringList read FSource;
property CurrentUnit: string read FCurrentUnit write FCurrentUnit;
end;
implementation
{ TD16Writer }
procedure TD16Writer.AddLineMapping(AElement: TCodeElement; AOffset: Integer);
var
LMapping: TLineMapping;
begin
LMapping := TLineMapping.Create();
try
LMapping.UnitLine := AElement.Line + AOffset;
LMapping.ASMLine := FSource.Count;
LMapping.D16UnitName := FCurrentUnit;
finally
FLineMapping.Add(LMapping);
end;
end;
procedure TD16Writer.AddMapping(AElement: TObject; AOffset: Integer;
AHideName: Boolean);
begin
if AElement is TVarDeclaration then
begin
AddVarMapping(TVarDeclaration(AElement), AOffset);
end
else
begin
if (AElement is TProcDeclaration) and (not AHideName) then
begin
AddRoutineMapping(TProcDeclaration(AElement), AOffset);
end
else
begin
AddLineMapping(TCodeElement(AElement), AOffset);
end;
end;
end;
procedure TD16Writer.AddRoutineMapping(AProc: TProcDeclaration; AOffset: Integer = 0);
var
LMapping: TRoutineMapping;
LParameter: TParameter;
LELement: TCodeElement;
LVar: TVarDeclaration;
begin
LMapping := TRoutineMapping.Create();
try
LMapping.UnitLine := AProc.Line + AOffset;
LMapping.ASMLine := FSource.Count;
LMapping.D16UnitName := FCurrentUnit;
LMapping.ElementName := AProc.Name;
if AProc.IsFunction then
begin
LMapping.TypeName := AProc.ResultType.Name;
end
else
begin
LMapping.TypeName := '';
end;
for LElement in AProc.Parameters do
begin
LVar := TVarDeclaration(LELement);
LParameter := TParameter.Create();
try
LParameter.Name := LVar.Name;
LParameter.TypeName := LVar.DataType.Name;
LParameter.Access := LVar.GetAccessIdentifier;
finally
LMapping.Parameters.Add(LParameter);
end;
end;
for LElement in AProc.Locals do
begin
LVar := TVarDeclaration(LELement);
LParameter := TParameter.Create();
try
LParameter.Name := LVar.Name;
LParameter.TypeName := LVar.DataType.Name;
LParameter.Access := LVar.GetAccessIdentifier;
finally
LMapping.Locals.Add(LParameter);
end;
end;
finally
FLineMapping.Add(LMapping);
end;
end;
procedure TD16Writer.AddVarMapping(AVar: TVarDeclaration; AOffset: Integer);
var
LMapping: TVarMapping;
begin
LMapping := TVarMapping.Create();
try
LMapping.UnitLine := AVar.Line + AOffset;
LMapping.ASMLine := FSource.Count;
LMapping.D16UnitName := FCurrentUnit;
LMapping.ElementName := AVar.Name;
LMapping.TypeName := AVar.DataType.Name;
finally
FLineMapping.Add(LMapping);
end;
end;
constructor TD16Writer.Create;
begin
FSource := TStringList.Create();
FLineMapping := TObjectList<TLineMapping>.Create();
end;
destructor TD16Writer.Destroy;
begin
FSource.Free();
FLineMapping.Free();
inherited;
end;
function TD16Writer.GetMappingByASMLine(ALine: Integer): TLineMapping;
var
LMapping: TLineMapping;
begin
Result := nil;
for LMapping in FLineMapping do
begin
if LMapping.ASMLine = ALine then
begin
Result := LMapping;
Break;
end;
end;
end;
procedure TD16Writer.Write(ALine: string);
begin
FSource.Add(ALine);
end;
procedure TD16Writer.WriteList(AList: TStrings);
begin
FSource.AddStrings(AList);
end;
end.