-
Notifications
You must be signed in to change notification settings - Fork 0
/
zas.py
executable file
·642 lines (566 loc) · 20.8 KB
/
zas.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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
#! /usr/bin/python
# Copyright (c) 2009 Marshall Vandegrift
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import with_statement
import sys
import re
from cStringIO import StringIO
from operator import add, sub
import copy
import struct
from itertools import islice, izip
import antlr3
import antlr3.tree
from ZasLexer import ZasLexer
from ZasParser import ZasParser
from ZasWalker import ZasWalker
from opcodes import OPCODES, COUNT_0OP, COUNT_1OP, COUNT_2OP, COUNT_VAR, \
COUNT_EXT, RESULT, BRANCH, BYREF, PACK1, RELATIVE, REVERSE, INVERT
from zheader import ZHeader
class ZasError(Exception):
pass
class Atom(object):
def argbits(self, byref=False, offset=0):
try:
_, value = self.eval()
except ZasError:
return 0
value = value - offset
if value >= 0 and value < (1 << 8):
return 1
return 0
def arg2opbits(self):
bits = self.argbits()
if bits == 1 or bits == 2:
return bits - 1
return None
class Register(Atom):
def __init__(self, name, indirect=False):
self.name = name
self.indirect = indirect
self.value = self._eval()
def __repr__(self):
return "Register(%r)" % (self.name,)
def _eval(self):
value = None
if self.name == '%sp':
value = 0
elif self.name.startswith('%l'):
value = int(self.name[2:]) + 1
if value < 1 or value > 15:
raise ZasError("invalid local register")
elif self.name.startswith('%g'):
value = int(self.name[2:]) + 16
if value < 16 or value > 255:
raise ZasError("invalid global register")
return value
def eval(self):
return set(), self.value
def argbits(self, byref=False, offset=0):
if byref and not self.indirect:
return 1
return 2
class String(Atom):
ESCAPE_RE = re.compile(r'[\\](.)')
# Newline is carriage return
ESCAPES = {'a': '\a', 'b': '\b', 'f': '\f', 'n': '\r', 'r': '\r',
't': '\t', 'v': '\v'}
def _escape(self, match):
char = match.group(1)
return self.ESCAPES.get(char, char)
def __init__(self, value):
self.value = self.ESCAPE_RE.sub(self._escape, value[1:-1])
def eval(self):
return set(), ord(self.value[0])
def __str__(self):
return self.value
def __repr__(self):
return "String(%r)" % (self.value,)
class Integer(Atom):
def __init__(self, value):
if isinstance(value, (int, long)):
pass
elif value[:2] == '0b':
value = int(value[2:], 2)
elif value[:2] == '0x':
value = int(value[2:], 16)
else:
value = int(value)
self.value = value
def eval(self):
return set(), self.value
def argbits(self, byref=False, offset=0):
if offset > 0:
return 0
if self.value >= 0 and self.value < (1 << 8):
return 1
return 0
def __repr__(self):
return "Integer(%r)" % (self.value,)
class RetBool(Atom):
VALUES = {':rfalse': 0,':rtrue': 1}
def __init__(self, value):
self.value = value
def eval(self):
return set(), self.VALUES[self.value]
def argbits(self, byref=False, offset=0):
return 1
def __repr__(self):
return "RetBool(%s)" % (self.value,)
class Symbol(Atom):
def __init__(self, name, locdir=None):
self.name = name
self.locdir = locdir
self.local = bool(locdir) or name[0].isdigit()
self.sections = set()
self.value = None
self.expr = None
def eval(self):
if self.value is not None:
return self.sections, self.value
if self.expr is not None:
sections, value = self.expr.eval()
return sections, value
raise ZasError('Evaluated not-yet-defined symbol %r' % self.name)
def argbits(self, byref=False, offset=0):
if self.local and not self.value and not self.expr and offset > 0:
return 1
try:
value = self.eval()[1]
except ZasError:
return 0
if value >= 0 and value < (1 << 8):
return 1
return 0
def __repr__(self):
return "Symbol(%r)" % (self.name + (self.locdir or ''),)
class Expr(Atom):
def __init__(self, oper, *args):
self.oper = oper
self.args = args
def eval(self):
sections, args = set(), []
for arg in self.args:
s, a = arg.eval()
if self.oper == sub:
sections.difference_update(s)
else:
sections.update(s)
args.append(a)
value = self.oper(*args)
return sections, value
def __repr__(self):
result = []
for arg in self.args:
result.append(repr(arg))
return 'Expr(?, %s)' % ', '.join(result)
class Section(object):
def __init__(self, name):
self.name = name
self.content = StringIO()
# Fake-out pre-relocation symbol sizes
self.base = 0x10000
def offset():
def fget(self):
return self.content.tell()
def fset(self, value):
self.content.seek(value)
return property(fget=fget, fset=fset)
offset = offset()
def __len__(self):
offset = self.content.tell()
try:
self.content.seek(0, 2)
result = self.content.tell()
finally:
self.content.seek(offset, 0)
return result
def eval(self):
return set([self]), self.base
def __getattr__(self, name):
return getattr(self.content, name)
def __repr__(self):
return "Section(%r)" % (self.name,)
class ZasAssembler(object):
ALPHATAB = ["abcdefghijklmnopqrstuvwxyz",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"\r0123456789.,!?_#'\"/\\-:()"]
def __init__(self):
self.symtab = {}
self.relocs = []
self.sections = {'data': Section('data'),
'rodata': Section('rodata'),
'text': Section('text')}
self.section = self.sections['text']
self.start = None
def finalize(self):
sections = self.sections
base = sections['data'].base = 0
datasz = len(sections['data'])
mod = datasz % 4
if mod != 0:
sections['data'].write('\0' * (8 - mod))
datasz = len(sections['data'])
sections['rodata'].base = base + datasz
rodatasz = len(sections['rodata'])
mod = rodatasz % 4
if mod != 0:
sections['rodata'].write('\0' * (8 - mod))
rodatasz = len(sections['rodata'])
sections['text'].base = base + datasz + rodatasz
mod = len(sections['text']) % 4
if mod != 0:
sections['text'].write('\0' * (8 - mod))
for reloc in self.relocs:
section, offset, expr, widths, relative, branch, invert = reloc
# print (section, hex(offset), widths, expr, expr.eval())
self._relocate(expr, widths, relative=relative, branch=branch,
invert=invert, fixup=True, section=section,
offset=offset)
self.start = self.start.eval()[1]
self.globals = self.globals.eval()[1]
def symbol(self, name):
if name == '.':
return self._current_address()
if name[0].isdigit() and not name[-1].isdigit():
locdir = name[-1]
name = name[:-1]
else:
locdir = None
if name not in self.symtab or \
(locdir == 'f' and self.symtab[name].expr is not None):
self.symtab[name] = Symbol(name, locdir)
return self.symtab[name]
def packaddr(self, number):
return number >> 2
def integer(self, *args):
return Integer(*args)
def expr(self, *args):
return Expr(*args)
def register(self, *args):
return Register(*args)
def string(self, *args):
return String(*args)
def retbool(self, *args):
return RetBool(*args)
def insn(self, name, args):
meth = '_insn_' + name.replace('.', 'dot_')
if hasattr(self, meth):
return getattr(self, meth)(*args)
raise ZasError('unknown instruction or directive %r' % name)
def _relocate(self, expr, widths, relative=False, branch=False,
invert=False, fixup=False, section=None, offset=None):
section = self.section if section is None else section
if offset is not None:
section.offset = offset
else:
offset = section.offset
if isinstance(expr, (Register, RetBool)):
relative = False
elif branch:
relative = True
sections, value = None, None
try:
sections, value = expr.eval()
except ZasError:
if fixup:
raise
if value is None or \
(not fixup and sections and
(not relative or (len(sections) > 1 or
section not in sections))):
if relative and isinstance(expr, Symbol) and expr.local:
width = widths[0]
else:
width = widths[-1]
reloc = (section, offset, expr, width, relative, branch, invert)
if fixup:
raise ZasError('could not perform relocation: %r' % (reloc,))
self.relocs.append(reloc)
self.section.write(struct.pack('>' + width, 0))
return struct.calcsize(width)
for width in widths:
bytes = struct.calcsize(width)
v = value
if relative:
v = v - (section.base + offset + bytes) + 2
fbits = 2 if branch else 0
max = 1 << ((bytes * 8) - fbits)
min = 0 if bytes == 1 else -(max >> 1)
if relative and bytes == 2:
max = (max >> 1)
if v < min or v > max:
continue
if branch:
if v < 0:
width = width.upper()
v = (1 << 14) - abs(v)
if not invert:
v |= 0x80 << (8 * (bytes - 1))
if bytes == 1:
v |= 0x40
if v > 0 and v >= (max >> 1):
width = width.upper()
#print 1, (expr, relative, width, v)
section.write(struct.pack('>' + width, v))
return struct.calcsize(width)
else:
raise ZasError("number %r out of range for width options %r" %
(value, widths))
return
def _current_address(self):
return Expr(add, self.section, Integer(self.section.offset))
def _zencode(self, chars):
enchars = []
for char in chars:
if char == ' ':
enchars.append(0)
elif char in self.ALPHATAB[0]:
zchar = self.ALPHATAB[0].index(char) + 6
enchars.append(zchar)
elif char in self.ALPHATAB[1]:
zchar = self.ALPHATAB[1].index(char) + 6
enchars.extend([4, zchar])
elif char in self.ALPHATAB[2]:
zchar = self.ALPHATAB[2].index(char) + 7
enchars.extend([5, zchar])
else:
zchar = ord(char)
enchars.extend([5, 6, (zchar >> 5) & 0x1f, zchar & 0x1f])
mod = len(enchars) % 3
pad = 0 if mod == 0 else 3 - mod
enchars.extend([5] * pad)
words = []
slices = [islice(enchars, i, None, 3) for i in xrange(3)]
for hi, mi, lo in izip(*slices):
words.append((hi << 10) | (mi << 5) | (lo << 0))
words[-1] |= 0x8000
result = struct.pack('>' + ('H' * len(words)), *words)
return result
def _insn_dot_set(self, symbol, expr):
if symbol.value is None and symbol.expr is None:
symbol.expr = expr
return
symbol = copy.copy(symbol)
symbol.expr = expr
self.symtab[symbol.name] = symbol
def _insn_dot_section(self, symbol):
self.section = self.sections[symbol.name]
def _insn_dot_align(self, expr):
_, value = expr.eval()
self._align(value)
def _align(self, value):
mod = self.section.offset % value
if mod != 0:
self.section.offset += value - mod
return
def _insn_dot_byte(self, *args):
for arg in args:
if isinstance(arg, String):
self.section.write(str(arg))
else:
self._relocate(arg, 'b')
return
def _insn_dot_word(self, *args):
self._align(2)
for arg in args:
self._relocate(arg, 'h')
return
def _insn_dot_ascii(self, *args):
for arg in args:
self.section.write(str(arg))
return
def _insn_dot_asciz(self, *args):
for arg in args:
self.section.write(str(arg) + '\0')
return
def _insn_dot_zscii(self, *args):
for arg in args:
self.section.write(self._zencode(str(arg)))
return
def _insn_dot_fill(self, repeat, size=None, value=None):
repeat = repeat.eval()[1]
size = 1 if size is None else size.eval()[1]
value = 0 if value is None else value.eval()[1]
format = {1: '>b', 2: '>h', 4: '>i'}[size]
code = struct.pack(format, value) * repeat
self.section.write(code)
def _insn_dot_org(self, expr):
sections, value = expr.eval()
if len(sections) > 1:
raise ZasError('nonsensical .org address')
if len(sections) > 0:
self.section = sections.pop()
value = value - self.section.base
self.section.offset = value
def _insn_dot_start(self, expr):
self.start = expr
def _insn_dot_label(self, label):
return self._insn_dot_set(label, self.symbol('.'))
def _insn_dot_routine(self, label, regcount):
regcount = regcount.eval()[1]
if regcount < 0 or regcount > 15:
raise ZasError('invalid number of local registers %r' %
(regcount,))
self._align(4)
self._insn_dot_label(label)
self.section.write(struct.pack('>b', regcount))
return
def _insn_dot_globals(self, label):
self.globals = label
def _insn_calln(self, *args):
if len(args) == 1:
return self._insn_call_1n(*args)
elif len(args) == 2:
return self._insn_call_2n(*args)
elif len(args) <= 4:
return self._insn_call_vn(*args)
else:
return self._insn_call_vn2(*args)
def _insn_calls(self, *args):
if len(args) == 1:
raise ZasError('calls instruction without result operand')
if len(args) == 2:
return self._insn_call_1s(*args)
elif len(args) == 3:
return self._insn_call_2s(*args)
elif len(args) <= 5:
return self._insn_call_vs(*args)
else:
return self._insn_call_vs2(*args)
def _insn_print(self, string):
self.section.write(struct.pack('>B', 0xb2))
self.section.write(self._zencode(str(string)))
def create_insns():
def create_insn(name, count, opcode, flags):
def do_insn(self, *args):
args = list(args)
branch = None
result = None
if flags & BRANCH:
branch = args.pop()
if flags & RESULT:
result = args.pop()
if not isinstance(result, Register):
raise ZasError('non-register result operand')
if flags & REVERSE:
args.reverse()
if flags & PACK1 and not isinstance(args[0], Register):
args[0] = Expr(self.packaddr, args[0])
if flags & BYREF and not args[0].indirect:
args[0] = Integer(args[0].value)
if count == COUNT_0OP:
if len(args) != 0:
raise ZasError("bad arguments for %r" % (name,))
code = 0xb0 | opcode
self.section.write(struct.pack('>B', code))
elif count == COUNT_1OP:
if len(args) != 1:
raise ZasError("bad arg count for %r" % (name,))
arg, = args
if flags & RELATIVE:
relative = True
argbits = arg.argbits(offset=self.section.offset)
else:
relative = False
argbits = arg.argbits()
code = 0x80 | (argbits << 4) | opcode
self.section.write(struct.pack('>B', code))
self._relocate(arg, 'bh', relative=relative)
elif count == COUNT_2OP and len(args) == 2 and \
args[0].arg2opbits() is not None and \
args[1].arg2opbits() is not None:
code = ((args[0].arg2opbits() << 6) |
(args[1].arg2opbits() << 5) |
opcode)
self.section.write(struct.pack('>B', code))
self._relocate(args[0], 'bh')
self._relocate(args[1], 'bh')
elif count == COUNT_2OP or count == COUNT_VAR:
if len(args) > 4 and name not in ('call_vn2', 'call_vs2'):
raise ZasError("bad arg count for %r" % (name,))
if len(args) > 8:
raise ZasError("bad arg count for %r" % (name,))
code = 0xc0 if count == COUNT_2OP else 0xe0
code = code | opcode
self.section.write(struct.pack('>B', code))
argb = 0xff
for arg in reversed(args[:4]):
argb = (argb >> 2) | (arg.argbits() << 6)
self.section.write(struct.pack('>B', argb))
if len(args) > 4:
argb = 0xff
for arg in reversed(args[4:]):
argb = (argb >> 2) | (arg.argbits() << 6)
self.section.write(struct.pack('>B', argb))
for arg in args:
self._relocate(arg, 'bh')
elif count == COUNT_EXT:
if len(args) > 4:
raise ZasError("bad arg count for %r" % (name,))
argb = 0xff
for arg in reversed(args):
argb = (argb >> 2) | (arg.argbits() << 6)
self.section.write(struct.pack('>BBB', 0xbe, opcode, argb))
for arg in args:
self._relocate(arg, 'bh')
else:
raise ZasError('wtf? unexpected instruction type')
if result:
self._relocate(result, 'b')
if branch:
invert = flags & INVERT
self._relocate(branch, 'bh', branch=True, invert=invert)
setattr(ZasAssembler, '_insn_' + name, do_insn)
for name, count, opcode, flags in OPCODES:
create_insn(name, count, opcode, flags)
create_insns()
class ZasWalker(ZasWalker):
def __init__(self, input, assembler):
super(ZasWalker, self).__init__(input)
self.assembler = assembler
def main(argv=sys.argv):
inpath, outpath = argv[1:]
with open(inpath, 'rb') as inf:
char_stream = antlr3.ANTLRInputStream(inf)
lexer = ZasLexer(char_stream)
tokens = antlr3.CommonTokenStream(lexer)
parser = ZasParser(tokens)
r = parser.program()
t = r.tree
#print t.toStringTree()
nodes = antlr3.tree.CommonTreeNodeStream(t)
nodes.setTokenStream(tokens)
assembler = ZasAssembler()
walker = ZasWalker(nodes, assembler)
walker.program()
assembler.finalize()
zcode = []
for secname in ('data', 'rodata', 'text'):
zcode.append(assembler.sections[secname].getvalue())
zcode = ''.join(zcode)[0x40:]
header = ZHeader()
header.version = 5
header.initpc = assembler.start
header.globals = assembler.globals
header.statmem = assembler.sections['rodata'].base
header.himem = assembler.sections['text'].base
header.filesz = len(zcode) + 0x40
with open(outpath, 'wb') as outf:
outf.write(str(header))
outf.write(zcode)
return 0
if __name__ == '__main__':
sys.exit(main())