-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZXVariableMap.cs
More file actions
382 lines (283 loc) · 16.6 KB
/
ZXVariableMap.cs
File metadata and controls
382 lines (283 loc) · 16.6 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
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ZXBasicStudio.BuildSystem
{
public class ZXVariableMap
{
static Regex regGlobalFlatVars = new Regex("\\('var',\\ '([^']+)',\\ '([0-9]+)'\\)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
static Regex regGlobalInitializedFlatVars = new Regex("\\('vard',\\ '(_[^'\\.]+)',", RegexOptions.IgnoreCase | RegexOptions.Multiline);
static Regex regGlobalArrVars = new Regex("\\('varx',\\ '([^'\\.]+)',\\ 'u16',\\ \"\\['([^']+)']\\\"", RegexOptions.IgnoreCase | RegexOptions.Multiline);
static Regex regLocalEndFunction = new Regex("\\('label', '([^']*)__leave'\\)", RegexOptions.Multiline | RegexOptions.IgnoreCase);
static Regex regLocalVar = new Regex("\\('pstore([iuf](8|16|32)|str|f)', '(-[0-9]+)'", RegexOptions.Multiline | RegexOptions.IgnoreCase);
static Regex regLocalVar2 = new Regex("\\('pload([iuf](8|16|32)|str|f)', '[^']*', '(-[0-9]+)'\\)", RegexOptions.Multiline | RegexOptions.IgnoreCase);
static Regex regLocalVar3 = new Regex("\\('lvard',\\ '([0-9]+)',\\ \"\\[('[0-9]+'(,\\ )?)+\\]", RegexOptions.Multiline | RegexOptions.IgnoreCase);
static Regex regLocalArr = new Regex("\\('larrd',\\ '([0-9a-fA-F]+)', \"\\[('[0-9A-F]+',?\\ ?)+\\]\",\\ '([0-9]+)'", RegexOptions.Multiline | RegexOptions.IgnoreCase);
static string arrGlobalInfoTemplate = "\\('vard',\\ '{0}',\\ \"\\['(?<extraDims>[0-9A-Fa-f]{{4}})',\\ (?<extraDimsSize>'[0-9A-Fa-f]{{4}}',\\ )*'(?<itemSize>[0-9]+)'\\]\\\"";
static string addrGlobalTemplate = "^([0-9A-F]+):[^\\n]*\\ {0}\\s*$";
static string regLocalStartFunction = "\\('label', '{0}'\\)";
static string addrLocalTemplate = "^([0-9A-F]+):[^\\n]*\\ \\.{0}\\s*$";
List<ZXVariable> vars = new List<ZXVariable>();
public IEnumerable<ZXVariable> Variables { get { return vars; } }
public IEnumerable<ZXVariable> VariablesInRange(ushort ScopeAddress)
{
return vars.Where(v => v.Scope.InRange(ScopeAddress));
}
public ZXVariableMap(string ICFile, string MapFile, ZXBasicMap BasicMap)
{
string icContent = File.ReadAllText(ICFile);
string mapContent = File.ReadAllText(MapFile);
ProcessGlobalVariables(icContent, mapContent, BasicMap);
ProcessLocalVariables(icContent, mapContent, BasicMap);
}
internal void ProcessGlobalVariables(string icContent, string mapContent, ZXBasicMap BasicMap)
{
int splitIndex = icContent.IndexOf("--- end of user code ---");
string icTop = icContent.Substring(0, splitIndex);
string icBottom = icContent.Substring(splitIndex);
var variableMatches = regGlobalFlatVars.Matches(icBottom);
var initVariableMatches = regGlobalInitializedFlatVars.Matches(icBottom);
List<Match> varMatches = new List<Match>();
varMatches.AddRange(variableMatches.Cast<Match>());
varMatches.AddRange(initVariableMatches.Cast<Match>());
foreach (Match m in varMatches)
{
string varName = m.Groups[1].Value;
string bVarName = varName.Substring(1);
var basicVar = BasicMap.GlobalVariables.FirstOrDefault(v => string.Equals(v.Name.TrimEnd('$'), bVarName.TrimEnd('$'), StringComparison.OrdinalIgnoreCase));
if (basicVar == null)
continue;
ZXVariableStorage storage = basicVar.Storage;
Regex regAddr = new Regex(string.Format(addrGlobalTemplate, "\\." + varName), RegexOptions.IgnoreCase | RegexOptions.Multiline);
var addrMatch = regAddr.Match(mapContent);
if (addrMatch == null || !addrMatch.Success)
continue;
var addr = ushort.Parse(addrMatch.Groups[1].Value, System.Globalization.NumberStyles.HexNumber);
ZXVariable newVar = new ZXVariable
{
Name = basicVar.Name,
Address = new ZXVariableAddress { AddressType = ZXVariableAddressType.Absolute, AddressValue = addr },
Scope = ZXVariableScope.GlobalScope,
VariableType = ZXVariableType.Flat,
StorageType = storage
};
vars.Add(newVar);
}
var arrayMatches = regGlobalArrVars.Matches(icBottom);
foreach (Match m in arrayMatches)
{
string varName = m.Groups[1].Value;
string infoLabelName = m.Groups[2].Value;
string bVarName = varName.Substring(1);
var basicVar = BasicMap.GlobalVariables.FirstOrDefault(v => string.Equals(v.Name.TrimEnd('$'), bVarName.TrimEnd('$'), StringComparison.OrdinalIgnoreCase));
if (basicVar == null)
continue;
ZXVariableStorage storage = basicVar.Storage;
Regex regInfo = new Regex(string.Format(arrGlobalInfoTemplate, infoLabelName.Replace(".", "\\.")), RegexOptions.IgnoreCase | RegexOptions.Multiline);
var infoMatch = regInfo.Match(icBottom);
if (infoMatch == null || !infoMatch.Success)
continue;
var elemSize = int.Parse(infoMatch.Groups["itemSize"].Value, System.Globalization.NumberStyles.HexNumber);
List<int> dimsSize = new List<int>();
foreach (Capture capture in infoMatch.Groups["extraDimsSize"].Captures)
{
string cleanValue = capture.Value.Replace("'", "").Replace(",", "").Replace(" ", "");
int realValue = int.Parse(cleanValue, System.Globalization.NumberStyles.HexNumber);
dimsSize.Add(realValue + 1);
}
Regex regAddr = new Regex(string.Format(addrGlobalTemplate, "\\." + varName), RegexOptions.IgnoreCase | RegexOptions.Multiline);
var addrMatch = regAddr.Match(mapContent);
if (addrMatch == null || !addrMatch.Success)
continue;
var addr = ushort.Parse(addrMatch.Groups[1].Value, System.Globalization.NumberStyles.HexNumber);
ZXVariable newVar = new ZXVariable
{
Name = basicVar.Name,
Address = new ZXVariableAddress { AddressType = ZXVariableAddressType.Absolute, AddressValue = addr },
Scope = ZXVariableScope.GlobalScope,
VariableType = ZXVariableType.Array,
StorageType = storage
};
vars.Add(newVar);
}
}
internal void ProcessLocalVariables(string icContent, string mapContent, ZXBasicMap BasicMap)
{
int splitIndex = icContent.IndexOf("--- end of user code ---");
string icTop = icContent.Substring(0, splitIndex);
var functionEnds = regLocalEndFunction.Matches(icContent);
foreach (Match functionEnd in functionEnds)
{
string label = functionEnd.Groups[1].Value;
var regStart = new Regex(string.Format(regLocalStartFunction, label));
var startMatch = regStart.Match(icTop);
if (startMatch == null || !startMatch.Success)
continue;
var startAddrReg = new Regex(string.Format(addrLocalTemplate, label), RegexOptions.Multiline | RegexOptions.IgnoreCase);
var endAddrReg = new Regex(string.Format(addrLocalTemplate, label + "__leave"), RegexOptions.Multiline | RegexOptions.IgnoreCase);
var mStart = startAddrReg.Match(mapContent);
var mEnd = endAddrReg.Match(mapContent);
if (mStart == null || mEnd == null || !mStart.Success || !mEnd.Success)
continue;
ushort startAddr = ushort.Parse(mStart.Groups[1].Value, System.Globalization.NumberStyles.HexNumber);
ushort endAddr = ushort.Parse(mEnd.Groups[1].Value, System.Globalization.NumberStyles.HexNumber);
string locName = label.Substring(1);
ZXVariableScope currentScope = new ZXVariableScope { ScopeName = locName, StartAddress = startAddr, EndAddress = endAddr };
ZXBasicSub? sub = BasicMap.Subs.FirstOrDefault(m => string.Equals(m.Name.TrimEnd('$'), locName.TrimEnd('$'), StringComparison.OrdinalIgnoreCase));
if (sub == null)
sub = BasicMap.Functions.FirstOrDefault(m => string.Equals(m.Name.TrimEnd('$'), locName.TrimEnd('$'), StringComparison.OrdinalIgnoreCase));
//Function params
if (sub != null)
{
foreach (var param in sub.InputParameters)
{
ZXVariable lVar = new ZXVariable { Name = param.Name, Address = new ZXVariableAddress { AddressType = ZXVariableAddressType.Relative, AddressValue = param.Offset }, Scope = currentScope, StorageType = param.Storage, VariableType = param.IsArray ? ZXVariableType.Array : ZXVariableType.Flat, IsReference = param.IsArray | param.ByRef, IsParameter = true };
vars.Add(lVar);
}
}
string funcCode = icTop.Substring(startMatch.Index, functionEnd.Index - startMatch.Index);
List<Match> localVarMatches = new List<Match>();
List<ZXVariable> localVars = new List<ZXVariable>();
localVarMatches.AddRange(regLocalVar.Matches(funcCode));
localVarMatches.AddRange(regLocalVar2.Matches(funcCode));
foreach (Match localMatch in localVarMatches)
{
ZXVariableStorage storage = Enum.Parse<ZXVariableStorage>(localMatch.Groups[1].Value.ToUpper());
int offset = int.Parse(localMatch.Groups[3].Value);
ZXVariable lVar = new ZXVariable { Name = offset.ToString(), Address = new ZXVariableAddress { AddressType = ZXVariableAddressType.Relative, AddressValue = offset }, Scope = currentScope, StorageType = storage, VariableType = ZXVariableType.Flat };
localVars.Add(lVar);
}
var matchesLocalAssign = regLocalVar3.Matches(funcCode);
foreach (Match localMatch in matchesLocalAssign)
{
ZXVariableStorage storage;
int bytes = localMatch.Groups[2].Captures.Count;
switch(bytes)
{
case 1:
storage = ZXVariableStorage.U8;
break;
case 2:
storage = ZXVariableStorage.U16;
break;
case 4:
storage = ZXVariableStorage.U32;
break;
default:
storage = ZXVariableStorage.F;
break;
}
int offset = -int.Parse(localMatch.Groups[1].Value);
ZXVariable lVar = new ZXVariable { Name = offset.ToString(), Address = new ZXVariableAddress { AddressType = ZXVariableAddressType.Relative, AddressValue = offset }, Scope = currentScope, StorageType = storage, VariableType = ZXVariableType.Flat };
localVars.Add(lVar);
}
var localArrayMatches = regLocalArr.Matches(funcCode);
foreach (Match localMatch in localArrayMatches)
{
int arrOffset = -int.Parse(localMatch.Groups[1].Value);
int loadOffset = arrOffset + 2;
int itemLen = int.Parse(localMatch.Groups[2].Captures.Last().Value.Replace("\"", "").Replace("'", "").Replace(",", "").Trim(), System.Globalization.NumberStyles.HexNumber);
int store = int.Parse(localMatch.Groups[3].Value);
ZXVariableStorage storage = ZXVariableStorage.LA8;
switch (itemLen)
{
case 1:
storage = ZXVariableStorage.LA8;
break;
case 2:
storage = ZXVariableStorage.LA16;
break;
case 4:
storage = ZXVariableStorage.LA32;
break;
case 5:
storage = ZXVariableStorage.LAF;
break;
}
ZXVariable lVar = new ZXVariable { Name = arrOffset.ToString(), Address = new ZXVariableAddress { AddressType = ZXVariableAddressType.Relative, AddressValue = arrOffset }, Scope = currentScope, StorageType = storage, VariableType = ZXVariableType.Array, StorageSize = store };
var pload = localVars.Where(v => v.Address.AddressValue == loadOffset).ToArray();
if (pload != null && pload.Length > 0)
foreach (var p in pload)
localVars.Remove(p);
localVars.Add(lVar);
}
if (localVars.Count == 0)
continue;
localVars = localVars.Distinct(new localVarComparer()).ToList();
vars.AddRange(localVars);
if (sub != null)
{
var vars = sub.LocalVariables.ToArray();
if (localVars.Count != vars.Length)
vars = vars.Where(v => !v.Unused).ToArray();
if (localVars.Count == vars.Length)
{
var nonArrays = localVars.Where(v => v.VariableType == ZXVariableType.Flat).OrderByDescending(v => v.Address.AddressValue).ToArray();
var lNonArrays = vars.Where(v => !v.IsArray).OrderBy(v => StackSizeByStorageType(v.Storage)).ToArray();
if (nonArrays.Length != lNonArrays.Length)
continue;
for (int buc = 0; buc < nonArrays.Length; buc++)
{
nonArrays[buc].StorageType = lNonArrays[buc].Storage;
nonArrays[buc].Name = lNonArrays[buc].Name;
}
var arrays = localVars.Where(v => v.VariableType == ZXVariableType.Array).OrderByDescending(v => v.Address.AddressValue).ToArray();
var lArrays = vars.Where(v => v.IsArray).ToArray();
if (arrays.Length != lArrays.Length)
continue;
for (int buc = 0; buc < arrays.Length; buc++)
{
arrays[buc].StorageType = lArrays[buc].Storage;
arrays[buc].Name = lArrays[buc].Name;
}
}
else
continue;
}
}
}
static int StackSizeByStorageType(ZXVariableStorage Storage)
{
switch (Storage)
{
case ZXVariableStorage.I8:
case ZXVariableStorage.U8:
return 1;
case ZXVariableStorage.I16:
case ZXVariableStorage.U16:
case ZXVariableStorage.STR:
return 2;
case ZXVariableStorage.I32:
case ZXVariableStorage.U32:
case ZXVariableStorage.F16:
return 4;
default: //float
return 5;
}
}
class localVarComparer : IEqualityComparer<ZXVariable>
{
public bool Equals(ZXVariable? x, ZXVariable? y)
{
if (x == y)
return true;
if (x == null || y == null)
return false;
if (x.Scope != y.Scope)
return false;
if (x.Address.AddressValue != y.Address.AddressValue)
return false;
return true;
}
public int GetHashCode([DisallowNull] ZXVariable obj)
{
return $"{obj.Name}-{obj.Address.AddressType}-{obj.Address.AddressValue}-{obj.Scope.ScopeName}-{obj.Scope.StartAddress}-{obj.Scope.EndAddress}".GetHashCode();
}
}
}
}