-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacroExpand.py
More file actions
403 lines (322 loc) · 11.3 KB
/
macroExpand.py
File metadata and controls
403 lines (322 loc) · 11.3 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
#!/usr/bin/python
# Naomi Hiebert coded this
#import our data structures
from accMacDict import accMac
from unaMacDict import unaMac
from binMacDict import binMac
from jmpMacDict import jmpMac
from asmDicts import opcodes, unaryOpcodes, dataTypes
#import global variables
import globalVars
# The real heart of the operation - identifies macros anywhere,
# including inside other macros! Tends to get called recursively
# since macros inside macros need to be expanded inside macros.
#Takes: a split line
#Returns: a list of (joined) lines
def expandline(splitline):
expLine = []
if isFallthroughLine(splitline): #the base case - encompasses several other cases
expLine.append(" ".join(splitline))
elif isINFLine(splitline):
getINFValue(splitline)
elif isIncludeStatement(splitline):
expLine.append(handleInclude(splitline))
elif isAccMacro(splitline):
expLine.extend(expandAccMacro(splitline))
elif isUnaryMacro(splitline):
expLine.extend(expandUnaryMacro(splitline))
elif isBinaryMacro(splitline):
expLine.extend(expandBinaryMacro(splitline))
elif isJumpMacro(splitline):
expLine.extend(expandJumpMacro(splitline))
else:
syntaxfail(splitline)
return expLine
#boolean functions - for identifying macros and syntax errors
#all the base cases return true on this line
def isFallthroughLine(splitline):
if isBlankOrComment(splitline):
return True
elif isNativeASM(splitline):
return True
elif isSoleLabel(splitline):
return True
elif isData(splitline):
return True
else:
return False
#INF header lines get grabbed
def isINFLine(splitline):
if len(splitline) == 2 and splitline[0] == "INF":
return True
else:
return False
#blank or comment lines fall through unchanged
def isBlankOrComment(splitline):
if len(splitline) == 0 or splitline[0][0] == '#' or splitline[0][0] == ";":
return True
else:
return False
#checks if it's native ASM.
def isNativeASM(splitline):
if len(splitline) == 2 and splitline[0] in opcodes:
if globalVars.DataFields:
structurefail(splitline)
return True
elif len(splitline) == 1 and splitline[0] in unaryOpcodes:
if globalVars.DataFields:
structurefail(splitline)
return True
else:
return False
#checks if it fits the standard label syntax, alone on a line
def isSoleLabel(splitline):
if len(splitline) == 1 and splitline[0][-1] == ':':
return True
else:
return False
#checks if it's a data declaration, possibly with label
def isData(splitline):
if len(splitline) > 1 and splitline[0] in dataTypes:
globalVars.DataFields = True
return True
elif len(splitline) > 2 and splitline[1] in dataTypes:
globalVars.DataFields = True
return True
else:
return False
#detects INCL statements
def isIncludeStatement(splitline):
if splitline[0] == "INCL" and len(splitline) == 2:
return True
else:
return False
#detects accumulator-based macros
def isAccMacro(splitline):
if len(splitline) == 2 and splitline[0] in accMac and splitline[1] == "ACC":
return True
else:
return False
#detects unary operation macros
def isUnaryMacro(splitline):
if len(splitline) == 4 and splitline[0] in unaMac and splitline[2] == "INTO":
return True
elif len(splitline) == 2 and splitline[0] in unaMac and splitline[1] != "ACC":
return True
else:
return False
#detects binary operation macros
def isBinaryMacro(splitline):
if len(splitline) == 5 and splitline[0] in binMac and splitline[3] == "INTO":
return True
elif len(splitline) == 3 and splitline[0] in binMac:
return True
else:
return False
#detects jump macros
def isJumpMacro(splitline):
if len(splitline) == 5 and splitline[0] in jmpMac and splitline[3] == "TO":
return True
else:
return False
#complains when it can't figure out what you're saying
def syntaxfail(errorline):
raise Exception("Syntax Error!", " ".join(errorline))
#complains when you put data in front of instructions
def structurefail(errorline):
raise Exception("Structural Error: Instructions cannot be placed after data fields!",
" ".join(errorline))
#complains when you ask for the fifth or greater nibble of an address
def addroffsetfail(errortoken):
raise Exception("Addressing Error: Addresses are only four nibbles long!", errortoken)
#replacement functions - expand those macros!
# The simplest expasion function, since no acc macro
# takes any arguments. Some might contain other macros
# though, so we still need to check
#Takes: a split line (as list of single-word strings)
#Returns: a list of (joined) lines (possibly a single-element list)
def expandAccMacro(inMac):
outlines = []
for line in accMac[inMac[0]].splitlines():
countMacroUsage(line.split())
outlines.extend(expandline(line.split()))
return outlines
# Really the only difference between unary and binary is
# the number of arguments. That's why the functions are
# almost identical.
#Takes: a split line
#Returns: a list of (joined) lines
def expandUnaryMacro(inMac):
outlines = []
op1 = inMac[1]
#Assume in-place operation if no dest given
if len(inMac) == 4:
dest = inMac[3]
else:
dest = op1
for line in unaMac[inMac[0]].splitlines():
splitline = line.split()
#replace our placeholder labels with the input ones
splitline = replaceLabels(splitline, "$op1", op1)
splitline = replaceLabels(splitline, "&op1", op1)
splitline = replaceLabels(splitline, "$dest", dest)
countMacroUsage(splitline)
#recursively expand the resulting line
outlines.extend(expandline(splitline))
return outlines
#Takes: a split line
#Returns: a list of lines
def expandBinaryMacro(inMac):
outlines = []
op1 = inMac[1]
op2 = inMac[2]
if len(inMac) == 5:
dest = inMac[4]
else:
dest = op1
for line in binMac[inMac[0]].splitlines():
splitline = line.split()
#replace our placeholder labels with the input ones
splitline = replaceLabels(splitline, "$op1", op1)
splitline = replaceLabels(splitline, "$op2", op2)
splitline = replaceLabels(splitline, "$dest", dest)
countMacroUsage(splitline)
#recursively expand the resulting line
outlines.extend(expandline(splitline))
return outlines
# Frankly, this is no different from the operation macros.
# I just split them into different dictionaries for ease of
# coding and maintenance. The only cost of that decision was
# having to write this function, which is basically identical
# to the functions above.
#Takes: a split line
#Returns: a list of lines
def expandJumpMacro(inMac):
outlines = []
op1 = inMac[1]
op2 = inMac[2]
dest = inMac[4]
for line in jmpMac[inMac[0]].splitlines():
splitline = line.split()
#replace our placeholder labels with the input ones
splitline = replaceLabels(splitline, "$op1", op1)
splitline = replaceLabels(splitline, "$op2", op2)
splitline = replaceLabels(splitline, "$dest", dest)
countMacroUsage(splitline)
#recursively expand the resulting line
outlines.extend(expandline(splitline))
return outlines
# One of the more complex bits of code in this script, if only
# because of the amount of string operations involved. Takes
# macros and part of their context, and replaces the $-marked
# placeholder tokens in the macros with the actual labels they
# should hold. Also does math on memory offsets, so we don't
# have to define a new label for each nibble of memory. Finally,
# keeps up the counter on the amount of memory used internal to
# the macros we're using. This allows us to declare only as much
# macro scratch space as we need.
#Takes: a split line,
# the placeholder (starts with $ or maybe &) label to replace
# the new label (maybe with [offset]) to replace it with
#Also note that the placeholder in the line may also have an offset
#Returns: a split line
#Edits: global "memUsed" variable, if necessary
def replaceLabels(splitline, oldlabel, replabel):
outline = []
for token in splitline:
#put it in the output line, adapted
if token.startswith(oldlabel) and "$" in oldlabel:
outline.append(reptoken(token, replabel))
elif token.startswith(oldlabel) and "&" in oldlabel:
outline.append(repaddress(token, replabel))
else:
#not the label we're looking for
outline.append(token)
return outline
def countMacroUsage(outline):
#check if we're using macro memory. If so, we might need to
#expand our macro memory bank.
#We can get away with only checking the last token on the line
#because macro memory is always assigned to before it is used,
#and assignment is always to the last label on a line.
if "macro[" in outline[-1]:
macoffset = outline[-1][outline[-1].index('[') + 1 : outline[-1].index(']')]
macoffset = int(macoffset, 16)
macoffset += 1
if macoffset > globalVars.memUsed:
globalVars.memUsed = macoffset
#Takes: The token to replace (maybe with offset, starts with $)
# The new label to replace things with
#Returns:
# A new token, with calculate labels
#Assumes:
# If replabel or token uses the "&" syntax, it already has
# the trailing [] present, as &(label[A])[B] but definitely
# not &(label[A]). This is always the case if this program
# applied the "&" syntax itself; users might break things.
def reptoken(token, replabel):
#default values, if no offset found
oldoffset = 0
repoffset = 0
#get the offset from the replacement, if necessary
if '[' in replabel and ']' in replabel:
repoffset = replabel[replabel.rindex('[') + 1 : replabel.rindex(']')]
repoffset = hexSmartInt(repoffset)
#and from the old label, if necessary
if '[' in token and ']' in token:
oldoffset = token[token.rindex('[') + 1 : token.rindex(']')]
oldoffset = hexSmartInt(oldoffset)
#add them together
newoffset = oldoffset + repoffset
#smash together the new token
if '[' in replabel:
newtoken = replabel[:replabel.rfind('[')] + '[' + hex(newoffset)[2:] + ']'
else:
newtoken = replabel + '[' + hex(newoffset)[2:] + ']'
if '&' in newtoken and newoffset > 3:
addroffsetfail(newtoken)
return newtoken
#Takes: A token to replace (maybe with offset in [0:4], starts with &)
# A label to replace it with (completely unrelated offset, not already using &)
#Returns: A token formed as &(replabel[repoffset])[tokenoffset]
def repaddress(token, replabel):
repoffset = 0
addroffset = 0
#get the offset from the replacement, if necessary
if '[' in replabel and ']' in replabel:
repoffset = replabel[replabel.index('[') + 1 : replabel.index(']')]
repoffset = int(repoffset, 16)
replabel = replabel[:replabel.find('[')]
#and from the old label, if necessary
if '[' in token and ']' in token:
addroffset = token[token.index('[') + 1 : token.index(']')]
addroffset = int(addroffset, 16)
token = token[:token.find('[')]
#assemble new token
newtoken = "&(" + replabel + '[' + hex(repoffset)[2:] + "])[" + hex(addroffset)[2:] + ']'
return newtoken
#deal with include statements by adding them to the FList queue
def handleInclude(splitline):
if splitline[1] not in globalVars.FList:
globalVars.FList.append(splitline[1])
return ";Included " + splitline[1]
else:
return ";Ignored repeated include: " + splitline[1]
#deal with INF statements by, if they're in the first file,
#setting out output INF statement to have the given value.
#If there's no statement in the first file, use the default
#(1024).
def getINFValue(splitline):
if globalVars.FIndex != 0:
return
else:
globalVars.BAddr = int(splitline[1], 0)
#Like int(token, 0) but defaults to hexadecimal.
def hexSmartInt(token):
if token[0] == '0' and not token.isdigit():
if len(token) > 2 and token[1] == 'd':
return int(token[2:], 10)
else:
return int(token, 0)
else:
return int(token, 16)