-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathasm.py
executable file
·1116 lines (1004 loc) · 38.6 KB
/
asm.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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python2.7
import sys
import os.path
import re
import struct
import argparse
srcs = {}
filename = ''
pos = 0
def fatal(msg):
prog = os.path.basename(sys.argv[0])
if sys.stderr.isatty():
print >> sys.stderr, '\x1b[1m{}: \x1b[31mfatal error:\x1b[39m'.format(prog), msg
sys.stderr.write('\x1b[0m')
else:
print >> sys.stderr, '{}: fatal error:'.format(prog), msg
sys.exit(1)
def error(msg):
if sys.stderr.isatty():
print >> sys.stderr, '\x1b[1m{}:{}: \x1b[31merror:\x1b[39m'.format(filename, pos), msg
sys.stderr.write('\x1b[0m')
else:
print >> sys.stderr, '{}:{}: error:'.format(filename, pos), msg
print >> sys.stderr, ' ' + srcs[filename][pos]
sys.exit(1)
def warning(msg, show_line=False):
if sys.stderr.isatty():
print >> sys.stderr, '\x1b[1m{}:{}: \x1b[35mwarning:\x1b[39m'.format(filename, pos), msg
sys.stderr.write('\x1b[0m')
else:
print >> sys.stderr, '{}:{}: warning:'.format(filename, pos), msg
if show_line:
print >> sys.stderr, ' ' + srcs[filename][pos]
# ----------------------------------------------------------------------
# utility functions (mainly parsing)
# ----------------------------------------------------------------------
regs = {'rsp': 30, 'rbp': 31, 'rk0': 26, 'rk1': 27}
for i in range(32):
regs['r' + str(i)] = i
def regnum(reg):
if reg not in regs:
error('expected register: ' + reg)
return regs[reg]
def parse_int(s):
try:
return True, int(s, 0)
except ValueError:
return False, 0
def parse_float(s):
try:
return True, float(s)
except ValueError:
return False, 0.0
def check_int_range(i, b):
x = 1 << (b - 1)
return -x <= i < x
def float_to_bit(f):
try:
s = struct.pack('f', f)
return struct.unpack('I', s)[0]
except OverflowError:
error('floating point value is too large')
def parse_memaccess(operand):
m = re.match(r'\[\s*(r\w+)\s*([+-])\s*(\w+)\s*\]$', operand)
if m:
base = m.group(1)
success, imm = parse_int(m.group(2) + m.group(3))
if base in regs and success:
return True, base, imm
m = re.match(r'\[\s*(r\w+)\s*\]$', operand)
if m and m.group(1) in regs:
return True, m.group(1), 0
m = re.match(r'\[\s*([+-]?\s*\w+)\s*\]$', operand)
if m:
success, imm = parse_int(m.group(1))
if success:
return True, 'r0', imm
return False, 'r0', 0
def check_operands_n(operands, n, m=-1):
l = len(operands)
if l < n:
error('expected {} operands, but {} given'.format(n, l))
if l > max(n, m):
error('expected {} operands, but {} given'.format(max(n, m), l))
def split_comma(s):
lit = esc = False
for i, c in enumerate(s):
if esc:
esc = False
continue
if c == '\"':
lit ^= True
if c == '\\' and lit:
esc = True
if c == ',' and not lit:
return [s[0:i]] + split_comma(s[i+1:])
if c == '#' and not lit:
return [s[0:i]]
return [s]
def parse(line):
mnemonic, rest = line.split(None, 1) if ' ' in line else (line, '')
if '#' in mnemonic:
return mnemonic[0 : mnemonic.find('#')], []
if len(rest) == 0 or rest[0] == '#':
return mnemonic, []
operands = split_comma(rest)
return mnemonic, map(str.strip, operands)
# ----------------------------------------------------------------------
# mnemonic definitions
# ----------------------------------------------------------------------
alu3_table = {
# 'fcmpne': 28,
# 'fcmpeq': 29,
'fcmplt': 30,
'fcmple': 31,
}
alu4_table = {
'add': 0,
'sub': 1,
'shl': 2,
'shr': 3,
'sar': 4,
'and': 5,
'or': 6,
'xor': 7,
'adda': 8,
'cmpult': 22,
'cmpule': 23,
'cmpne': 24,
'cmpeq': 25,
'cmplt': 26,
'cmple': 27,
}
fpu2_table = {
'finv': 4,
'fsqrt': 5,
'ftoi': 6,
'itof': 7,
'floor': 8,
}
fpu3_table = {
'fadd': 0,
'fsub': 1,
'fmul': 2,
# 'fdiv': 3,
}
misc0_table = {
'sysenter': 12,
'sysexit': 13,
}
misc2_table = {
'ldl': 2,
'jl': 4,
}
misc3_table = {
'ldh': 3,
'ld': 6,
'ldb': 7,
'st': 8,
'stb': 9,
'bne': 14,
'beq': 15,
}
debug_table = {
'break': 1,
'penv': 2,
'ptrace': 3,
}
sign_table = {
'': 0,
'neg': 1,
'abs': 2,
'abs.neg': 3,
}
def code_i(op, rx, ra, rb, imm, tag):
x = regnum(rx)
a = regnum(ra)
b = regnum(rb)
success, i = parse_int(imm)
if not success:
error('expected integer literal: ' + imm)
if not check_int_range(i, 8):
error('immediate value too large: ' + imm)
c0 = ((i & 7) << 5) + tag
c1 = ((b & 7) << 5) + ((i >> 3) & 31)
c2 = ((x & 1) << 7) + (a << 2) + (b >> 3)
c3 = (op << 4) + (x >> 1)
return chr(c0) + chr(c1) + chr(c2) + chr(c3)
def code_f(rx, ra, rb, sign, tag):
x = regnum(rx)
a = regnum(ra)
b = regnum(rb)
c0 = (sign << 5) + tag
c1 = ((b & 7) << 5)
c2 = ((x & 1) << 7) + (a << 2) + (b >> 3)
c3 = (1 << 4) + (x >> 1)
return chr(c0) + chr(c1) + chr(c2) + chr(c3)
def code_m(op, rx, ra, pred, disp, disp_mode):
x = regnum(rx)
a = regnum(ra)
success, d = parse_int(disp)
if not success:
error('expected displacement: ' + disp)
if disp_mode == 0:
if not -0x8000 <= d <= 0xffff:
error('immediate value too large: ' + disp)
elif disp_mode == 1:
if not check_int_range(d, 16):
error('displacement too large: ' + disp)
else:
if d & 3 != 0:
error('displacement must be a multiple of 4')
if not check_int_range(d, 18):
error('displacement too large: ' + disp)
d >>= 2
c0 = d & 255
c1 = (d >> 8) & 255
c2 = ((x & 1) << 7) + (a << 2) + pred
c3 = (op << 4) + (x >> 1)
return chr(c0) + chr(c1) + chr(c2) + chr(c3)
def on_alu3(operands, tag):
check_operands_n(operands, 3)
return code_i(0, operands[0], operands[1], operands[2], '0', tag)
def on_alu4(operands, tag):
check_operands_n(operands, 4)
return code_i(0, operands[0], operands[1], operands[2], operands[3], tag)
def on_fpu2(operands, sign, tag):
check_operands_n(operands, 2)
return code_f(operands[0], operands[1], 'r0', sign, tag)
def on_fpu3(operands, sign, tag):
check_operands_n(operands, 3)
return code_f(operands[0], operands[1], operands[2], sign, tag)
def on_misc0(operands, op, pred, disp_mode):
check_operands_n(operands, 0)
return code_m(op, 'r0', 'r0', pred, '0', disp_mode)
def on_misc2(operands, op, pred, disp_mode):
check_operands_n(operands, 2)
return code_m(op, operands[0], 'r0', pred, operands[1], disp_mode)
def on_misc3(operands, op, pred, disp_mode):
check_operands_n(operands, 3)
return code_m(op, operands[0], operands[1], pred, operands[2], disp_mode)
def on_jr(operands):
check_operands_n(operands, 2)
return code_m(5, operands[0], operands[1], 3, '0', 0)
def on_debug(operands, tag):
check_operands_n(operands, 1)
return code_m(10, 'r{}'.format(tag), 'r0', 0, operands[0], 0)
def on_dot_int(operand):
success, imm = parse_int(operand)
if not success:
error('expected integer literal: ' + operand)
if not -0x80000000 <= imm <= 0xffffffff:
error('immediate value too large: ' + operand)
return ''.join(chr(imm >> x & 255) for x in [0, 8, 16, 24])
def on_dot_byte(operand):
success, imm = parse_int(operand)
if not success:
error('expected integer literal: ' + operand)
if not -128 <= imm <= 255:
error('immediate value too large: ' + operand)
return chr(imm & 255)
def on_dot_short(operand):
success, imm = parse_int(operand)
if not success:
error('expected integer literal: ' + operand)
if not -0x8000 <= imm <= 0xffff:
error('immediate value too large: ' + operand)
return chr(imm & 255) + chr(imm >> 8 & 255)
def on_dot_space(operands):
check_operands_n(operands, 2)
success, imm = parse_int(operands[1])
if not success:
error('expected integer literal: ' + operands[1])
if not -128 <= imm <= 255:
error('immediate value too large: ' + operand)
size = int(operands[0], 0)
return ''.ljust(size, chr(imm & 255))
def code(mnemonic, operands):
if mnemonic in alu3_table:
return on_alu3(operands, alu3_table[mnemonic])
if mnemonic in alu4_table:
return on_alu4(operands, alu4_table[mnemonic])
fpu_mnemonic, fpu_suffix = mnemonic, ''
if '.' in mnemonic:
fpu_mnemonic, fpu_suffix = mnemonic.split('.', 1)
if fpu_mnemonic in fpu2_table:
return on_fpu2(operands, sign_table[fpu_suffix], fpu2_table[fpu_mnemonic])
if fpu_mnemonic in fpu3_table:
return on_fpu3(operands, sign_table[fpu_suffix], fpu3_table[fpu_mnemonic])
pred = 3 if mnemonic in ['jl', 'bne+', 'beq+'] else 0
disp_mode = 0 if mnemonic in ['ldl', 'ldh'] else \
1 if mnemonic in ['ldb', 'stb'] else 2
if mnemonic in ['bne-', 'bne+']:
mnemonic = 'bne'
if mnemonic in ['beq-', 'beq+']:
mnemonic = 'beq'
if mnemonic in misc0_table:
return on_misc0(operands, misc0_table[mnemonic], pred, disp_mode)
if mnemonic in misc2_table:
return on_misc2(operands, misc2_table[mnemonic], pred, disp_mode)
if mnemonic in misc3_table:
return on_misc3(operands, misc3_table[mnemonic], pred, disp_mode)
if mnemonic == 'jr':
return on_jr(operands)
if mnemonic in debug_table:
return on_debug(operands, debug_table[mnemonic])
if mnemonic == '.int':
return ''.join(on_dot_int(operand) for operand in operands)
if mnemonic == '.byte':
return ''.join(on_dot_byte(operand) for operand in operands)
if mnemonic == '.short':
return ''.join(on_dot_short(operand) for operand in operands)
if mnemonic == '.space':
return on_dot_space(operands)
error('unknown mnemonic \'{}\''.format(mnemonic))
# ----------------------------------------------------------------------
# macro definitions
# ----------------------------------------------------------------------
def eval_string(arg):
try:
s = eval(arg)
if not isinstance(s, str):
error('expected string literal: ' + arg)
return s
except Exception:
error('invalid string literal: ' + arg)
def expand_nop(operands):
check_operands_n(operands, 0)
return [('add', ['r0', 'r0', 'r0', '0'])]
def mov_imm(dest, imm):
if check_int_range(imm, 16):
return [('ldl', [dest, str(imm)])]
if not -0x80000000 <= imm <= 0xffffffff:
error('immediate value too large: ' + hex(imm))
if imm & 0xffff == 0:
return [('ldh', [dest, 'r0', hex((imm >> 16) & 0xffff)])]
return [('ldl', [dest, hex(imm & 0xffff)]),
('ldh', [dest, dest, hex((imm >> 16) & 0xffff)])]
def expand_mov(operands):
check_operands_n(operands, 2)
if operands[0] in regs and operands[1] in regs:
return [('add', [operands[0], operands[1], 'r0', '0'])]
if operands[1][0] == '[' and operands[1][-1] == ']':
success, base, disp = parse_memaccess(operands[1])
if not success:
return [('ld2', [operands[0], operands[1][1:-1].strip()])]
if check_int_range(disp, 18):
return [('ld', [operands[0], base, str(disp)])]
return [('ldh', ['r29', 'r0', hex(disp >> 16 & 0xffff)])] + \
([('add', ['r29', base, 'r29', '0'])] if base != 'r0' else []) + \
[('ld', [operands[0], 'r29', hex(disp & 0xffff)])]
if operands[0][0] == '[' and operands[0][-1] == ']':
success, base, disp = parse_memaccess(operands[0])
if not success:
return [('st2', [operands[1], operands[0][1:-1].strip()])]
if check_int_range(disp, 18):
d, p = (operands[1], []) if operands[1] in regs else ('r29', expand_mov(['r29', operands[1]]))
return p + [('st', [d, base, str(disp)])]
return [('ldh', ['r29', 'r0', hex(disp >> 16 & 0xffff)])] + \
([('add', ['r29', base, 'r29', '0'])] if base != 'r0' else []) + \
[('st', [operands[1], 'r29', hex(disp & 0xffff)])]
success, imm = parse_int(operands[1])
if success:
return mov_imm(operands[0], imm)
success, imm = parse_float(operands[1])
if success:
return mov_imm(operands[0], float_to_bit(imm))
if operands[0] in regs:
return [('mov', operands)]
error('invalid syntax')
# and, sub, shl, shr, sar, and, or, xor, cmpne, cmpeq, cmplt, cmple
def expand_alu(op, operands):
check_operands_n(operands, 3, 4)
if (len(operands) == 4):
return [(op, operands)]
if operands[2] in regs:
return [(op, operands + ['0'])]
success, imm = parse_int(operands[2])
if success:
if check_int_range(imm, 8):
return [(op, [operands[0], operands[1], 'r0', operands[2]])]
return mov_imm('r29', imm) + [(op, [operands[0], operands[1], 'r29', '0'])]
error('expected register or immediate value: ' + operands[2])
def expand_movb(operands):
if operands[1][0] == '[' and operands[1][-1] == ']':
success, base, disp = parse_memaccess(operands[1])
if not success:
return [('ldb2', [operands[0], operands[1][1:-1].strip()])]
if check_int_range(disp, 16):
return [('ldb', [operands[0], base, str(disp)])]
return [('ldh', ['r29', 'r0', hex((disp + 0x8000) >> 16 & 0xffff)])] + \
([('add', ['r29', base, 'r29', '0'])] if base != 'r0' else []) + \
[('ldb', [operands[0], 'r29', hex(((disp + 0x8000) & 0xffff) - 0x8000)])]
if operands[0][0] == '[' and operands[0][-1] == ']':
success, base, disp = parse_memaccess(operands[0])
if not success:
return [('stb2', [operands[1], operands[0][1:-1].strip()])]
if check_int_range(disp, 16):
d, p = (operands[1], []) if operands[1] in regs else ('r29', expand_mov('r29', operands[1]))
return p + [('stb', [d, base, str(disp)])]
return [('ldh', ['r29', 'r0', hex((disp + 0x8000) >> 16 & 0xffff)])] + \
([('add', ['r29', base, 'r29', '0'])] if base != 'r0' else []) + \
[('stb', [operands[1], 'r29', hex(((disp + 0x8000) & 0xffff) - 0x8000)])]
error('movb only supports move between register and memory')
def expand_neg(operands):
check_operands_n(operands, 2)
return [('sub', [operands[0], 'r0', operands[1], '0'])]
def expand_not(operands):
check_operands_n(operands, 2)
return [('xor', [operands[0], operands[1], 'r0', '-1'])]
def expand_sextb(operands):
check_operands_n(operands, 2)
return [('shl', ['r29', operands[1], 'r0', '24']),
('sar', [operands[0], 'r29', 'r0', '24'])]
def expand_sextw(operands):
check_operands_n(operands, 2)
return [('shl', ['r29', operands[1], 'r0', '16']),
('sar', [operands[0], 'r29', 'r0', '16'])]
def expand_zextb(operands):
check_operands_n(operands, 2)
return [('shl', ['r29', operands[1], 'r0', '24']),
('shr', [operands[0], 'r29', 'r0', '24'])]
def expand_zextw(operands):
check_operands_n(operands, 2)
return [('ldh', [operands[0], operands[1], '0'])]
# cmpgt, cmpge, cmpugt, cmpuge
def expand_cmpgt(mnemonic, operands):
check_operands_n(operands, 3)
m = mnemonic.replace('g', 'l')
if operands[2] in regs:
return [(m, [operands[0], operands[2], operands[1], '0'])]
success, imm = parse_int(operands[2])
if success:
return mov_imm('r29', imm) + [(m, [operands[0], 'r29', operands[1], '0'])]
error('expected register or immediate value: ' + operands[2])
def expand_fcmpgt(operands):
check_operands_n(operands, 3)
return [('fcmplt', [operands[0], operands[2], operands[1]])]
def expand_fcmpge(operands):
check_operands_n(operands, 3)
return [('fcmple', [operands[0], operands[2], operands[1]])]
def expand_read(operands):
check_operands_n(operands, 1)
return [('ldh', ['r29', 'r0', '0x8000']),
('ld', [operands[0], 'r29', '0x1000']),
('cmplt', ['r29', operands[0], 'r0', '0']),
('bne', ['r29', 'r0', '-16'])]
def expand_write(operands):
check_operands_n(operands, 1, 2)
if len(operands) == 1:
return [('ldh', ['r29', 'r0', '0x8000']),
('ld', ['r29', 'r29', '0x1004']),
('beq', ['r29', 'r0', '-12']),
('ldh', ['r29', 'r0', '0x8000']),
('st', [operands[0], 'r29', '0x1000'])]
s = eval_string(operands[1])
l = [mov_imm(operands[0], ord(c)) + [('st', [operands[0], 'r29', '0x1000'])] for c in s]
return [('ldh', ['r29', 'r0', '0x8000'])] + sum(l, [])
def expand_jr(operands):
check_operands_n(operands, 1, 2)
if len(operands) == 1:
return [('jr', ['r29', operands[0]])]
return [('jr', operands)]
def expand_br(operands):
check_operands_n(operands, 1)
return [('jl', ['r29', operands[0]])]
def expand_bz(operands, pred):
check_operands_n(operands, 2)
return [('beq' + pred, [operands[0], 'r0', operands[1]])]
def expand_bnz(operands, pred):
check_operands_n(operands, 2)
return [('bne' + pred, [operands[0], 'r0', operands[1]])]
# bne, beq
def expand_bne(op, operands, pred):
check_operands_n(operands, 3)
success, imm = parse_int(operands[1])
if success:
return mov_imm('r29', imm) + [(op + pred, [operands[0], 'r29', operands[2]])]
return [(op + pred, operands)]
# blt, ble, bgt, bge
def expand_blt(op, operands, pred):
check_operands_n(operands, 3)
b, c = ('beq', 'cmple') if op == 'bgt' else \
('beq', 'cmplt') if op == 'bge' else \
('bne', 'cmp' + op[1:])
return expand_alu(c, ['r29', operands[0], operands[1]]) + [(b + pred, ['r29', 'r0', operands[2]])]
# bflt, bfle, bfgt, bfge
def expand_bfne(op, operands, pred):
check_operands_n(operands, 3)
b, c = ('beq', 'fcmple') if op == 'bfgt' else \
('beq', 'fcmplt') if op == 'bfge' else \
('bne', 'fcmp' + op[2:])
return [(c, ['r29', operands[0], operands[1]]),
(b + pred, ['r29', 'r0', operands[2]])]
def expand_push(operands):
check_operands_n(operands, 1)
pre = [('sub', ['rsp', 'rsp', 'r0', '4'])]
success, imm = parse_int(operands[0])
if success:
return mov_imm('r29', imm) + pre + [('st', ['r29', 'rsp', '0'])]
return pre + [('st', [operands[0], 'rsp', '0'])]
def expand_pop(operands):
check_operands_n(operands, 1)
return [('ld', [operands[0], 'rsp', '0']),
('add', ['rsp', 'rsp', 'r0', '4'])]
def expand_call(operands):
check_operands_n(operands, 1)
if operands[0] in regs:
return [('st', ['rbp', 'rsp', '-4']),
('sub', ['rsp', 'rsp', 'r0', '4']),
('add', ['rbp', 'rsp', 'r0', '0']),
('jr', ['r28', operands[0]]),
('add', ['rsp', 'rbp', 'r0', '4']),
('ld', ['rbp', 'rsp', '-4'])]
return [('call', operands)]
def expand_ret(operands):
check_operands_n(operands, 0)
return [('jr', ['r29', 'r28'])]
def expand_enter(operands):
check_operands_n(operands, 0, 1)
success, imm = parse_int(operands[0] if operands else '0')
if success:
if imm & 3 != 0:
error('immediate value must be a multiple of 4')
return expand_alu('sub', ['rsp', 'rsp', str(imm + 4)]) + [('st', ['r28', 'rsp', '0'])]
error('expected integer literal: ' + operands[0])
def expand_leave(operands):
check_operands_n(operands, 0)
return [('ld', ['r28', 'rsp', '0'])]
def expand_halt(operands):
check_operands_n(operands, 0)
return [('beq+', ['r31', 'r31', '-4'])]
def expand_dot_float(operands):
def go(operand):
success, floimm = parse_float(operand)
if not success:
error('expected floating point literal: ' + operand)
return hex(float_to_bit(floimm))
return [('.int', map(go, operands))]
def expand_dot_space(operands):
check_operands_n(operands, 1, 2)
if len(operands) == 2:
return [('.space', operands)]
return [('.space', [operands[0], '0'])]
def expand_dot_string(operands):
check_operands_n(operands, 1)
s = eval_string(operands[0])
return [('.byte', [str(ord(c)) for c in s] + ['0'])]
macro_table = {
'nop': expand_nop,
'mov': expand_mov,
'movb': expand_movb,
'neg': expand_neg,
'not': expand_not,
'sextb': expand_sextb,
'sextw': expand_sextw,
'zextb': expand_zextb,
'zextw': expand_zextw,
'fcmpgt': expand_fcmpgt,
'fcmpge': expand_fcmpge,
'read': expand_read,
'write': expand_write,
'jr': expand_jr,
'br': expand_br,
'push': expand_push,
'pop': expand_pop,
'call': expand_call,
'ret': expand_ret,
'enter': expand_enter,
'leave': expand_leave,
'halt': expand_halt,
'.float': expand_dot_float,
'.space': expand_dot_space,
'.string': expand_dot_string,
}
def expand_macro(line):
mnemonic, operands = parse(line)
if not mnemonic:
return []
if mnemonic in macro_table:
return macro_table[mnemonic](operands)
if mnemonic in ['add', 'sub', 'shl', 'shr', 'sar', 'and', 'or', 'xor', 'adda',
'cmpne', 'cmpeq', 'cmplt', 'cmple', 'cmpult', 'cmpule']:
return expand_alu(mnemonic, operands)
if mnemonic in ['cmpgt', 'cmpge', 'cmpugt', 'cmpuge']:
return expand_cmpgt(mnemonic, operands)
m = re.match(r'(\w+)([+-]?)$', mnemonic)
if m:
br_mnemonic, pred = m.groups()
if br_mnemonic == 'bz':
return expand_bz(operands, pred)
if br_mnemonic == 'bnz':
return expand_bnz(operands, pred)
if br_mnemonic in ['bne', 'beq']:
return expand_bne(br_mnemonic, operands, pred)
if br_mnemonic in ['blt', 'ble', 'bgt', 'bge']:
return expand_blt(br_mnemonic, operands, pred)
if br_mnemonic in ['bflt', 'bfle', 'bfgt', 'bfge']:
return expand_bfne(br_mnemonic, operands, pred)
return [(mnemonic, operands)]
# ----------------------------------------------------------------------
# label resolution
# ----------------------------------------------------------------------
labels = {}
rev_labels = {}
library = []
entry_point = 0x2000
start_label = 'main'
ofs_table = {
'mov': 8,
'ld2': 8,
'ldb2': 8,
'st2': 8,
'stb2': 8,
'call': 32,
'call6': 24,
'call7': 28,
}
def add_label(label, i):
if label in regs:
error('\'{}\' is register name'.format(label))
if parse_int(label)[0]:
error('\'{}\' can be parsed as integer'.format(label))
if re.search(r'[^\w.$!?]', label):
c = re.search(r'[^\w.$!?]', label).group()
error('label name cannot contain \'{}\' character'.format(c))
labels.setdefault(label, {}).setdefault(filename, [-1, False, False])
if labels[label][filename][0] >= 0:
error('duplicate declaration of label \'{}\''.format(label))
labels[label][filename][0] = i
rev_labels.setdefault(i, []).append(label)
def add_global(label):
labels.setdefault(label, {}).setdefault(filename, [-1, False, False])
labels[label][filename][1] = True
def label_addr(label):
dic = labels.get(label, {})
if filename in dic:
decl = [filename]
else:
decl = filter(lambda x: dic[x][1], dic)
if len(decl) == 0:
if label == start_label:
fatal('global label \'{}\' is required'.format(label))
else:
error('label \'{}\' is not declared'.format(label))
if len(decl) > 1 and not set(decl) <= set(library):
decl = list(set(decl) - set(library))
if len(decl) > 1:
msg = 'label \'{}\' is declared in multiple files ({})'.format(label, ', '.join(sorted(decl)))
if label == start_label:
fatal(msg)
else:
error(msg)
dic[decl[0]][2] = True
return dic[decl[0]][0]
def eval_expr(expr):
r = re.compile(r'[\w.$!?]+')
m = r.search(expr)
while m:
success, imm = parse_int(m.group())
if not success:
addr = str(label_addr(m.group()))
expr = expr[:m.start()] + addr + expr[m.end():]
m = r.search(expr, m.end() if success else m.start() + len(addr))
try:
res = eval(expr, {})
if not isinstance(res, int):
error('expression type must be int')
return res
except Exception:
error('eval error: ' + expr)
def calc_ofs(mnemonic, operands, addr=0):
if mnemonic[-1] == ':' or mnemonic in ['.global', '.set']:
return 0
if mnemonic == '.align':
align = int(operands[0], 0)
return ((addr + align - 1) & ~(align - 1)) - addr
if mnemonic == '.byte':
return len(operands)
if mnemonic == '.int':
return 4 * len(operands)
if mnemonic == '.short':
return 2 * len(operands)
if mnemonic == '.space':
return int(operands[0], 0)
return ofs_table.get(mnemonic, 4)
def init_label_first(lines):
global labels, rev_labels, filename, pos
labels = {}
rev_labels = {}
addr = entry_point
for mnemonic, operands, filename, pos in lines:
if mnemonic[-1] == ':':
if len(operands) > 0:
error('label declaration must be followed by new line')
add_label(mnemonic[:-1], addr)
elif mnemonic == '.align':
check_operands_n(operands, 1)
success, imm = parse_int(operands[0])
if not success:
error('expected integer literal: ' + operands[0])
if imm < 4 or (imm & (imm - 1)) > 0:
error('alignment must be a power of 2 which is not less than 4')
addr += ((addr + imm - 1) & ~(imm - 1)) - addr
elif mnemonic == '.byte':
addr += len(operands)
elif mnemonic == '.global':
check_operands_n(operands, 1)
add_global(operands[0])
elif mnemonic == '.int':
addr += 4 * len(operands)
elif mnemonic == '.set':
check_operands_n(operands, 2)
add_label(operands[0], eval_expr(operands[1]))
elif mnemonic == '.short':
addr += 2 * len(operands)
elif mnemonic == '.space':
check_operands_n(operands, 2)
success, imm = parse_int(operands[0])
if not success:
error('expected integer literal: ' + operands[0])
addr += imm
else:
if addr & 3:
error('instruction must be aligned on 4-byte boundaries')
addr += ofs_table.get(mnemonic, 4)
def init_label(lines):
global labels, rev_labels, filename, pos
labels = {}
rev_labels = {}
addr = entry_point
for mnemonic, operands, filename, pos in lines:
if mnemonic[-1] == ':':
add_label(mnemonic[:-1], addr)
elif mnemonic == '.global':
add_global(operands[0])
elif mnemonic == '.set':
add_label(operands[0], eval_expr(operands[1]))
else:
addr += calc_ofs(mnemonic, operands, addr)
def optimize(lines):
global filename, pos
eff = 0
addr = entry_point
for i, (mnemonic, operands, filename, pos) in enumerate(lines):
if mnemonic == 'mov':
addr += 8
if check_int_range(eval_expr(operands[1]), 16):
eff += 4
lines[i] = ('mov1', operands, filename, pos)
elif mnemonic in ['ld2', 'st2']:
addr += 8
if check_int_range(eval_expr(operands[1]), 18):
eff += 4
lines[i] = (mnemonic[:2] + '1', operands, filename, pos)
elif mnemonic in ['ldb2', 'stb2']:
addr += 8
if check_int_range(eval_expr(operands[1]), 16):
eff += 4
lines[i] = (mnemonic[:3] + '1', operands, filename, pos)
elif mnemonic in ['call', 'call7']:
val = label_addr(operands[0])
if check_int_range(val - addr - 16 + (eff if val > addr else -eff), 18):
eff += ofs_table[mnemonic] - 24
lines[i] = ('call6', operands, filename, pos)
elif mnemonic == 'call' and check_int_range(val, 16):
eff += 4
lines[i] = ('call7', operands, filename, pos)
addr += ofs_table[mnemonic]
else:
addr += calc_ofs(mnemonic, operands, addr)
return eff > 0
def resolve_label(lines):
global filename, pos
ret = []
addr = entry_point
for mnemonic, operands, filename, pos in lines:
if mnemonic[-1] == ':' or mnemonic in ['.global', '.set']:
continue
if mnemonic == 'mov1':
addr += 4
ret.append(('ldl', [operands[0], hex(eval_expr(operands[1]))], filename, pos))
continue
if mnemonic == 'mov':
addr += 8
val = eval_expr(operands[1])
if not -0x80000000 <= val <= 0xffffffff:
if not filename:
fatal('address of start label is too large: ' + hex(val))
else:
error('expression value too large: ' + hex(val))
ret.append(('ldl', [operands[0], hex(val & 0xffff)], filename, pos))
ret.append(('ldh', [operands[0], operands[0], hex(val >> 16 & 0xffff)], filename, pos))
continue
if mnemonic in ['ld1', 'ldb1', 'st1', 'stb1']:
addr += 4
ret.append((mnemonic[:-1], [operands[0], 'r0', hex(eval_expr(operands[1]))], filename, pos))
continue
if mnemonic in ['ld2', 'ldb2', 'st2', 'stb2']:
addr += 8
val = eval_expr(operands[1])
if not -0x80000000 <= val <= 0xffffffff:
error('expression value too large: ' + hex(val))
hi, lo = (val + 0x8000) >> 16 & 0xffff, ((val + 0x8000) & 0xffff) - 0x8000
ret.append(('ldh', ['r29', 'r0', hex(hi)], filename, pos))
ret.append((mnemonic[:-1], [operands[0], 'r29', hex(lo)], filename, pos))
continue
if mnemonic in ['call', 'call6', 'call7']:
addr += ofs_table[mnemonic]
val = label_addr(operands[0])
if not -0x80000000 <= val <= 0xffffffff:
error('expression value too large: ' + hex(val))
pre = [('st', ['rbp', 'rsp', '-4']),
('sub', ['rsp', 'rsp', 'r0', '4']),
('add', ['rbp', 'rsp', 'r0', '0'])]
if mnemonic == 'call6':
mid = [('jl', ['r28', hex(val - addr + 8)])]
else:
if mnemonic == 'call7':
mid = [('ldl', ['r29', hex(val)])]
else:
mid = [('ldl', ['r29', hex(val & 0xffff)]),
('ldh', ['r29', 'r29', hex(val >> 16 & 0xffff)])]
mid.append(('jr', ['r28', 'r29']))
post = [('add', ['rsp', 'rbp', 'r0', '4']), ('ld', ['rbp', 'rsp', '-4'])]
ret.extend(map(lambda (x, y): (x, y, filename, pos), pre + mid + post))
continue
if mnemonic == '.align':
align = int(operands[0], 0)
padding = ((addr + align - 1) & ~(align - 1)) - addr
if padding:
addr += padding
ret.append(('.space', [str(padding), '0'],filename, pos))
continue
if mnemonic in ['jl', 'bne', 'bne-', 'bne+', 'beq', 'beq-', 'beq+']:
check_operands_n(operands, 2, 3)
if not parse_int(operands[-1])[0]:
operands[-1] = hex(label_addr(operands[-1]) - addr - 4)
if mnemonic == '.int':
def go(operand):
val = eval_expr(operand)
if not -0x80000000 <= val <= 0xffffffff:
error('expression value too large: ' + hex(val))
return str(val) if check_int_range(val, 8) else hex(val)
operands = map(go, operands)
addr += calc_ofs(mnemonic, operands)
ret.append((mnemonic, operands, filename, pos))
if addr - entry_point > 0x400000:
fatal('program size exceeds 4MB limit ({:,} bytes)'.format(addr - entry_point))
return ret
def check_global(label):
if labels[label][filename][0] < 0:
error('label \'{}\' is not declared'.format(label))
def warn_unused_label(label):
if not labels[label][filename][2] and not (filename in library and labels[label][filename][1]):
warning('unused label \'{}\''.format(label))
def show_label(i):
if i in rev_labels:
return format(', '.join(rev_labels[i]))
return ''
# ----------------------------------------------------------------------
# main process
# ----------------------------------------------------------------------
# parse command line arguments
argparser = argparse.ArgumentParser(usage='%(prog)s [options] file...')
argparser.add_argument('inputs', nargs='*', help='input files', metavar='file...')
argparser.add_argument('-a', help='output as rs232c send test format', action='store_true')
argparser.add_argument('-c', help='do not append file header', action='store_true')
argparser.add_argument('-e', help='set entry point address', metavar='<integer>')
argparser.add_argument('-f', help='append label to end of program', metavar='<label>')
argparser.add_argument('-k', help='output as array of std_logic_vector format', action='store_true')
argparser.add_argument('-l', help='set library file to <file>', metavar='<file>', action='append')
argparser.add_argument('-o', help='set output file to <file>', metavar='<file>', default='a.out')
argparser.add_argument('-O', help='set optimization level', metavar='<integer>', default=2, type=int)
argparser.add_argument('-r', help='do not insert main label jump instruction', action='store_true')
argparser.add_argument('-s', help='output preprocessed assembly', action='store_true')
argparser.add_argument('-start', help='same as -t (deprecated)', metavar='<label>', dest='t')
argparser.add_argument('-t', help='start execution from <label>', metavar='<label>')
argparser.add_argument('-v', help='output more detailed assembly than -s', action='store_true')
argparser.add_argument('-Wno-unused-label', help='disable unused label warning', action='store_true')
argparser.add_argument('-Wr29', help='enable use of r29 warning', action='store_true')
args = argparser.parse_args()
if args.inputs == []:
argparser.print_help(sys.stderr)
sys.exit(1)
if args.e:
success, entry_point = parse_int(args.e)
if not success:
argparser.print_usage(sys.stderr)
fatal('argument -e: expected integer: ' + args.e)
if entry_point & 3 != 0:
argparser.print_usage(sys.stderr)
fatal('argument -e: entry address must be a multiple of 4')
if entry_point < 0:
argparser.print_usage(sys.stderr)
fatal('argument -e: entry address must be zero or positive')
if args.l: