-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacSize.py
More file actions
executable file
·90 lines (66 loc) · 2.34 KB
/
macSize.py
File metadata and controls
executable file
·90 lines (66 loc) · 2.34 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
#!/usr/bin/python
#for parsing the argument vector
import sys
#make it more BASH-friendly
import errno
#The back end of the macro expander
import macroExpand
#The data structures holding the macro language
from asmDicts import opcodes
#Takes: a split line, assumed to be a syntactically valid mac4 statement
#Returns: an integer describing the number of assembly instructions
# it expands into
def macroSize(splitInput):
#Call the macro expander to give us the expanded form of the macro
expLine = macroExpand.expandline(splitInput)
lineCount = 0
#Grep through the output, counting lines that hold actual assembly instructions
for line in expLine:
thisLine = line.split()
if thisLine[0] in opcodes:
lineCount += 1
return lineCount
#Main function: Parses the argument vector, checks that it's been
# given a valid opcode, gets the size, prints the size
if __name__ == "__main__":
#In this mode, we actually need the dictionaries of macros
from accMacDict import accMac
from unaMacDict import unaMac
from binMacDict import binMac
from jmpMacDict import jmpMac
#The three actual variables we care about
splitInput = []
expLine = []
count = 0
#First error condition: No opcode given
#Play dumb, say that no opcode equals zero length
if len(sys.argv) < 2:
print "0"
quit()
#If they explicitly mark an opcode as the accumulator version of the opcode
if len(sys.argv) == 3 and sys.argv[2] == "ACC" and sys.argv[1] in accMac:
splitInput = sys.argv[1:]
#If they give an opcode that is only valid as an accumulator macro
elif sys.argv[1] in accMac and sys.argv[1] not in unaMac:
splitInput = sys.argv[1:] + ["ACC"]
#Unary macros - opcodes that can be either unary or ACC default to unary
elif sys.argv[1] in unaMac:
splitInput.append(sys.argv[1])
splitInput.extend("arg1 INTO location".split())
#Binary macros
elif sys.argv[1] in binMac:
splitInput.append(sys.argv[1])
splitInput.extend("arg1 arg2 INTO location".split())
#Jump macros
elif sys.argv[1] in jmpMac:
splitInput.append(sys.argv[1])
splitInput.extend("arg1 arg2 TO location".split())
#Error condition 2: They give us something that looks like an opcode,
#but isn't valid
else:
print "No valid opcode detected!"
quit(errno.EINVAL)
#Call our externally-visible function to get the size
count = macroSize(splitInput)
#Output our results
print count