-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcompile.py
292 lines (251 loc) · 7.58 KB
/
compile.py
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
#
# Copyright 2013 Jeff Bush
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import sys
class CodeGenerator:
def __init__(self, filename):
self.operandStack = []
self.freeRegisters = [ x for x in range(3, -1, -1) ]
self.outputFile = open(filename, 'w')
self.numInstructions = 0
def pushConstant(self, value):
self.operandStack += [ ('const', value) ]
def pushVariableRef(self, index):
self.operandStack += [ ('freg', index ) ]
def doOp(self, operation):
type2, op2 = self.operandStack.pop()
type1, op1 = self.operandStack.pop()
if type1 == 'const':
# If the first operator is a constant, copy it into a register
tmpReg = self._allocateTemporary()
self._emitInstruction(8, tmpReg, 0, 0, 1, op1)
type1 = 'reg'
op1 = tmpReg
# Free up temporary registers, allocate a result reg
if type2 == 'reg': self._freeTemporary(op2)
if type1 == 'reg': self._freeTemporary(op1)
resultReg = self._allocateTemporary()
self.operandStack += [ ('reg', resultReg)]
if type2 == 'const':
self._emitInstruction(operation, resultReg, op1, 0, 1, op2)
else:
self._emitInstruction(operation, resultReg, op1, op2, 0, 0)
def saveResult(self):
# Emit an instruction to move into the result register (7)
type, op = self.operandStack.pop()
if type == 'reg' or type == 'freg':
self._emitInstruction(0, 7, op, op, 0, 0)
elif type == 'const':
self._emitInstruction(8, 7, 0, 0, 1, op) # Constant
else:
raise Exception('internal error: bad type on operand stack')
# Pad the remaining instructions with NOPs.
for i in range(self.numInstructions, 16):
self.outputFile.write('0000000000000\n')
self.outputFile.close()
def _emitInstruction(self, opcode, dest, srca, srcb, isConst, constVal):
self.numInstructions += 1
if self.numInstructions == 16:
raise Exception('formula too complex: exceeded instruction memory')
self.outputFile.write('%013x\n' % ((dest << 43) | (srca << 40) | (srcb << 37) | (opcode << 33) | (isConst << 32) | constVal))
# Pretty print operation
pretty = [ 'and', 'xor', 'or', 'add', 'sub', 'mul', 'shl', 'shr', 'mov',
'eq', 'neq', 'gt', 'gte', 'lt', 'lte' ]
if isConst:
print '%s r%d, r%d, #%d' % (pretty[opcode], dest, srca, constVal)
else:
print '%s r%d, r%d, r%d' % (pretty[opcode], dest, srca, srcb)
def _allocateTemporary(self):
if len(self.freeRegisters) == 0:
raise Exception('formula too complex: out of registers')
else:
return self.freeRegisters.pop()
def _freeTemporary(self, val):
self.freeRegisters += [ val ]
class Scanner:
def __init__(self, stream):
self.pushBackChar = None
self.pushBackToken = False
self.lastToken = None
self.stream = stream
def pushBack(self):
self.pushBackToken = True
MULTIBYTE_TOKENS = {
'>' : [ '=', '>' ],
'<' : [ '=', '<' ],
'=' : [ '=' ],
'!' : [ '=' ],
}
def nextToken(self):
if self.pushBackToken:
self.pushBackToken = False
return self.lastToken
# Consume whitespace
ch = self._nextChar()
while ch.isspace():
ch = self._nextChar()
# Get next token
if ch == '':
# End of string
self.lastToken = None
elif ch.isdigit():
lookahead = self._nextChar()
if lookahead == '':
self.lastToken = None
elif ch == '0' and lookahead == 'x':
# Parse a hexadecimal number
number = 0
while True:
ch = self._nextChar()
if ch >= 'A' and ch <= 'F':
number = (number * 16) + (ord(ch) - ord('A') + 10)
elif ch >= 'a' and ch <= 'f':
number = (number * 16) + (ord(ch) - ord('a') + 10)
elif ch.isdigit():
number = (number * 16) + (ord(ch) - ord('0'))
else:
self._pushBackChar(ch)
break
self.lastToken = number
else:
# Parse a decimal number
self._pushBackChar(lookahead)
number = 0
while ch.isdigit():
number = (number * 10) + (ord(ch) - ord('0'))
ch = self._nextChar()
self._pushBackChar(ch)
self.lastToken = number
elif ch in self.MULTIBYTE_TOKENS:
secondchars = self.MULTIBYTE_TOKENS[ch]
lookahead = self._nextChar()
if lookahead in secondchars:
self.lastToken = ch + lookahead
else:
self._pushBackChar(lookahead)
self.lastToken = ch
elif ch.isalpha():
strval = ch
while True:
ch = self._nextChar()
if ch.isalnum():
strval += ch
else:
self._pushBackChar(ch)
break
self.lastToken = strval
else:
# Single character symbolic token
self.lastToken = ch
return self.lastToken
def _nextChar(self):
if self.pushBackChar:
ch = self.pushBackChar
self.pushBackChar = None
return ch
else:
return self.stream.read(1)
def _pushBackChar(self, ch):
self.pushBackChar = ch
class Parser:
def __init__(self, inputStream, generator):
self.scanner = Scanner(inputStream)
self.generator = generator
def parse(self):
self._parseExpression()
self.generator.saveResult()
def _parseExpression(self):
self._parseUnaryExpression()
self._parseInfixExpression(0)
BUILTIN_VARS = {
'x' : 4,
'ix' : 4,
'y' : 5,
'iy' : 5,
'f' : 6
}
def _parsePrimaryExpression(self):
tok = self.scanner.nextToken()
if tok == '(':
self._parseExpression()
tok = self.scanner.nextToken()
if tok != ')':
raise Exception('parse error: expected )')
elif isinstance(tok, int) or isinstance(tok, long):
self.generator.pushConstant(tok)
elif tok in self.BUILTIN_VARS:
self.generator.pushVariableRef(self.BUILTIN_VARS[tok])
else:
raise Exception('unexpected: ' + str(tok))
def _parseUnaryExpression(self):
lookahead = self.scanner.nextToken()
if lookahead == '-':
self.generator.pushConstant(0)
self._parseUnaryExpression()
self.generator.doOp(4) # Subtract
elif lookahead == '~':
self._parseUnaryExpression()
self.generator.pushConstant(0xffffffff)
self.generator.doOp(1) # Exclusive Or
elif lookahead == '!':
self._parseUnaryExpression()
self.generator.pushConstant(0)
self.generator.doOp(9) # Equal to
else:
self.scanner.pushBack()
self._parsePrimaryExpression()
# Operator lookup table
# (precedence, opcode)
OPERATORS = {
'|' : ( 1, 2 ),
'^' : ( 2, 1 ),
'&' : ( 3, 0 ),
'==' : ( 4, 9 ),
'!=' : ( 4, 10 ),
'>' : ( 5, 11 ),
'<' : ( 5, 13 ),
'>=' : ( 5, 12 ),
'<=' : ( 5, 14 ),
'<<' : ( 6, 6 ),
'>>' : ( 6, 7 ),
'+' : ( 7, 3 ),
'-' : ( 7, 4 ),
'*' : ( 8, 5 ),
# '/' : ( 9, -1 )
}
# https://en.wikipedia.org/wiki/Operator-precedence_parser#Precedence_climbing_method
def _parseInfixExpression(self, minPrecedence):
while True:
outerOp = self.scanner.nextToken()
if outerOp not in self.OPERATORS:
self.scanner.pushBack()
break
outerPrecedence, outerOpcode = self.OPERATORS[outerOp]
if outerPrecedence < minPrecedence:
self.scanner.pushBack()
break
self._parseUnaryExpression()
while True:
lookahead = self.scanner.nextToken()
self.scanner.pushBack()
if lookahead not in self.OPERATORS:
break
innerPrecedence, _ignore = self.OPERATORS[lookahead]
if innerPrecedence <= outerPrecedence:
break
self._parseInfixExpression(innerPrecedence)
self.generator.doOp(outerOpcode)
p = Parser(sys.stdin, CodeGenerator('microcode.hex'))
p.parse()