-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathasm-11
executable file
·3339 lines (2975 loc) · 107 KB
/
asm-11
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/perl -w
# $Id: asm-11 1373 2023-02-16 11:21:26Z mueller $
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright 2013-2023 by Walter F.J. Mueller <[email protected]>
#
# Revision History:
# Date Rev Version Comment
# 2023-02-16 1373 1.2.7 BUGFIX: fix directly nested .if behavior
# 2023-01-30 1361 1.2.6 use 'R' error code; fix 'S' logic
# 2023-01-29 1360 1.2.5 BUFGIX: expressions: allow uop after bop operator;
# proper sign handling for '/','*' and .if ge,gt,le,lt
# 2023-01-27 1359 1.2.4 add minimal .list,.nlist (cnd,me,meb); add -(n)list
# 2023-01-26 1357 1.2.3 skip redundant loads in .mcall
# 2023-01-25 1355 1.2.2 rewrite macro args handling; add .narg,.nchr,.ntype
# 2023-01-24 1354 1.2.1 add .error,.print,.mcall,.mdelete; add -L; add ...top
# 2023-01-22 1353 1.2 add .if, .if(f|t|tf), .endc, .rept, .endr, .mexit
# add flag (MRmrd) column in output format
# 2023-01-13 1352 1.1.5 BUGFIX: misused # and @ don't cause BUGCHECK anymore
# 2023-01-12 1351 1.1.4 BUGFIX: support @(R) modifier with omitted offset
# 2022-07-28 1264 1.1.3 add -E and -M options
# 2022-07-23 1262 1.1.2 BUGFIX: '100(pc)' was compiled as '100'
# 2019-07-13 1189 1.1.1 drop superfluous exists for $opts
# 2019-05-25 1152 1.1 add .macro,.endm; dummy .list,.nlist
# 2019-04-25 1138 1.0.7 print lines with errors to stderr unless -lst seen
# 2019-04-19 1133 1.0.6 .end directive autocreates '...end' label
# 2018-11-03 1065 1.0.5 add and use bailout
# 2015-11-01 712 1.0.4 BUGFIX: fix '.' handling in instructions
# 2014-07-26 575 1.0.3 add 'call' and 'return' to pst (as in macro-11)
# 2013-04-07 503 1.0.2 list dot for .even,.dot,.blkb,.blkw
# 2013-04-01 502 1.0.1 BUGFIX: -2(r0),@-2(r0) was broken, parser fixed
# add -lsm (lsmem format) output; add implicit .word
# 2013-03-29 501 1.0 Initial version
# 2013-03-22 498 0.5 Second draft (functional, but very limited...)
# 2013-03-07 496 0.1 First draft
#
use 5.14.0; # require Perl 5.14 or higher
use strict; # require strict checking
use FileHandle;
use Getopt::Long;
use constant TMASK_STRING => 0x0001;
use constant TMASK_STRINGEXP => 0x0002;
use constant TMASK_MACROARG => 0x0004;
my %opts = ();
GetOptions(\%opts, "help", "E", "M",
"tpass1", "tpass2",
"dsym1", "dsym2",
"ttoken", "tparse", "temit", "tout",
"I=s@",
"L=s@",
"list=s@", "nlist=s@",
"lst", "olst=s",
"lda", "olda=s",
"cof", "ocof=s",
"lsm", "olsm=s"
)
or bailout("bad command options");
unshift @{$opts{I}}, "."; # ./ is first in include path
push @{$opts{I}}, "$ENV{RETROBASE}/tools/asm-11" if defined $ENV{RETROBASE};
push @{$opts{L}}, "$ENV{RETROBASE}/tools/asm-11/mlib" if defined $ENV{RETROBASE};
# .list/.nlist status
my %list = (cnd => 0, # unsatisfied conditional code off
me => 0, # macro&repeat expansions off
meb => 1); # macro&repeat expansions binary on
# --list and --nlist
foreach my $arg (@{$opts{list}}) {
if (exists $list{$arg}) {
$list{$arg} = 1;
} else {
bailout("bad -list option '$arg'");
}
}
foreach my $arg (@{$opts{nlist}}) {
if (exists $list{$arg}) {
$list{$arg} = 0;
} else {
bailout("bad -nlist option '$arg'");
}
}
# Permanant symbol table
my %pst = (
# directives
'.include' => {typ=>'dir'}, #
'.word' => {typ=>'dir'}, #
'.byte' => {typ=>'dir'}, #
'.blkw' => {typ=>'dir'}, #
'.blkb' => {typ=>'dir'}, #
'.ascii' => {typ=>'dir'}, #
'.asciz' => {typ=>'dir'}, #
'.even' => {typ=>'dir'}, #
'.odd' => {typ=>'dir'}, #
'.asect' => {typ=>'dir'}, #
'.macro' => {typ=>'dir'}, #
'.endm' => {typ=>'dir'}, #
'.mexit' => {typ=>'dir'}, #
'.mcall' => {typ=>'dir'}, #
'.mdelete' => {typ=>'dir'}, #
'.narg' => {typ=>'dir'}, #
'.nchr' => {typ=>'dir'}, #
'.ntype' => {typ=>'dir'}, #
'.rept' => {typ=>'dir'}, #
'.endr' => {typ=>'dir'}, #
'.if' => {typ=>'dir'}, #
'.iff' => {typ=>'dir'}, #
'.ift' => {typ=>'dir'}, #
'.iftf' => {typ=>'dir'}, #
'.endc' => {typ=>'dir'}, #
'.error' => {typ=>'dir'}, #
'.print' => {typ=>'dir'}, #
'.list' => {typ=>'dir'}, #
'.nlist' => {typ=>'dir'}, #
'.end' => {typ=>'dir'}, #
#register defs
'r0' => {typ=>'reg', val=>0},
'r1' => {typ=>'reg', val=>1},
'r2' => {typ=>'reg', val=>2},
'r3' => {typ=>'reg', val=>3},
'r4' => {typ=>'reg', val=>4},
'r5' => {typ=>'reg', val=>5},
'sp' => {typ=>'reg', val=>6},
'pc' => {typ=>'reg', val=>7},
#opcodes
'halt' => {typ=>'op', val=>0000000, fmt=>'-' },
'wait' => {typ=>'op', val=>0000001, fmt=>'-' },
'rti' => {typ=>'op', val=>0000002, fmt=>'-' },
'bpt' => {typ=>'op', val=>0000003, fmt=>'-' },
'iot' => {typ=>'op', val=>0000004, fmt=>'-' },
'reset' => {typ=>'op', val=>0000005, fmt=>'-' },
'rtt' => {typ=>'op', val=>0000006, fmt=>'-' },
'mfpt' => {typ=>'op', val=>0000007, fmt=>'-' },
'jmp' => {typ=>'op', val=>0000100, fmt=>'g' },
'rts' => {typ=>'op', val=>0000200, fmt=>'r' },
'return' => {typ=>'op', val=>0000207, fmt=>'-' }, # alias for rts pc
'spl' => {typ=>'op', val=>0000230, fmt=>'n3' },
'nop' => {typ=>'op', val=>0000240, fmt=>'-' },
'clc' => {typ=>'op', val=>0000241, fmt=>'-' },
'clv' => {typ=>'op', val=>0000242, fmt=>'-' },
'clz' => {typ=>'op', val=>0000244, fmt=>'-' },
'cln' => {typ=>'op', val=>0000250, fmt=>'-' },
'ccc' => {typ=>'op', val=>0000257, fmt=>'-' },
'sec' => {typ=>'op', val=>0000261, fmt=>'-' },
'sev' => {typ=>'op', val=>0000262, fmt=>'-' },
'sez' => {typ=>'op', val=>0000264, fmt=>'-' },
'sen' => {typ=>'op', val=>0000270, fmt=>'-' },
'scc' => {typ=>'op', val=>0000277, fmt=>'-' },
'swab' => {typ=>'op', val=>0000300, fmt=>'g' },
'br' => {typ=>'op', val=>0000400, fmt=>'s8' },
'bne' => {typ=>'op', val=>0001000, fmt=>'s8' },
'beq' => {typ=>'op', val=>0001400, fmt=>'s8' },
'bge' => {typ=>'op', val=>0002000, fmt=>'s8' },
'blt' => {typ=>'op', val=>0002400, fmt=>'s8' },
'bgt' => {typ=>'op', val=>0003000, fmt=>'s8' },
'ble' => {typ=>'op', val=>0003400, fmt=>'s8' },
'jsr' => {typ=>'op', val=>0004000, fmt=>'rg' },
'call' => {typ=>'op', val=>0004700, fmt=>'g' }, # alias for jsr pc,<dst>
'clr' => {typ=>'op', val=>0005000, fmt=>'g' },
'com' => {typ=>'op', val=>0005100, fmt=>'g' },
'inc' => {typ=>'op', val=>0005200, fmt=>'g' },
'dec' => {typ=>'op', val=>0005300, fmt=>'g' },
'neg' => {typ=>'op', val=>0005400, fmt=>'g' },
'adc' => {typ=>'op', val=>0005500, fmt=>'g' },
'sbc' => {typ=>'op', val=>0005600, fmt=>'g' },
'tst' => {typ=>'op', val=>0005700, fmt=>'g' },
'ror' => {typ=>'op', val=>0006000, fmt=>'g' },
'rol' => {typ=>'op', val=>0006100, fmt=>'g' },
'asr' => {typ=>'op', val=>0006200, fmt=>'g' },
'asl' => {typ=>'op', val=>0006300, fmt=>'g' },
'mark' => {typ=>'op', val=>0006400, fmt=>'n6' },
'mfpi' => {typ=>'op', val=>0006500, fmt=>'g' },
'mtpi' => {typ=>'op', val=>0006600, fmt=>'g' },
'sxt' => {typ=>'op', val=>0006700, fmt=>'g' },
'csm' => {typ=>'op', val=>0007000, fmt=>'g' },
'tstset' => {typ=>'op', val=>0007200, fmt=>'g' },
'wrtlck' => {typ=>'op', val=>0007300, fmt=>'g' },
'mov' => {typ=>'op', val=>0010000, fmt=>'gg' },
'cmp' => {typ=>'op', val=>0020000, fmt=>'gg' },
'bit' => {typ=>'op', val=>0030000, fmt=>'gg' },
'bic' => {typ=>'op', val=>0040000, fmt=>'gg' },
'bis' => {typ=>'op', val=>0050000, fmt=>'gg' },
'add' => {typ=>'op', val=>0060000, fmt=>'gg' },
'mul' => {typ=>'op', val=>0070000, fmt=>'gr' },
'div' => {typ=>'op', val=>0071000, fmt=>'gr' },
'ash' => {typ=>'op', val=>0072000, fmt=>'gr' },
'ashc' => {typ=>'op', val=>0073000, fmt=>'gr' },
'xor' => {typ=>'op', val=>0074000, fmt=>'rg' },
'sob' => {typ=>'op', val=>0077000, fmt=>'ru6'},
'bpl' => {typ=>'op', val=>0100000, fmt=>'s8' },
'bmi' => {typ=>'op', val=>0100400, fmt=>'s8' },
'bhi' => {typ=>'op', val=>0101000, fmt=>'s8' },
'blos' => {typ=>'op', val=>0101400, fmt=>'s8' },
'bvc' => {typ=>'op', val=>0102000, fmt=>'s8' },
'bvs' => {typ=>'op', val=>0102400, fmt=>'s8' },
'bcc' => {typ=>'op', val=>0103000, fmt=>'s8' },
'bhis' => {typ=>'op', val=>0103000, fmt=>'s8' }, #alias
'bcs' => {typ=>'op', val=>0103400, fmt=>'s8' },
'blo' => {typ=>'op', val=>0103400, fmt=>'s8' }, #alias
'emt' => {typ=>'op', val=>0104000, fmt=>'n8' },
'trap' => {typ=>'op', val=>0104400, fmt=>'n8' },
'clrb' => {typ=>'op', val=>0105000, fmt=>'g' },
'comb' => {typ=>'op', val=>0105100, fmt=>'g' },
'incb' => {typ=>'op', val=>0105200, fmt=>'g' },
'decb' => {typ=>'op', val=>0105300, fmt=>'g' },
'negb' => {typ=>'op', val=>0105400, fmt=>'g' },
'adcb' => {typ=>'op', val=>0105500, fmt=>'g' },
'sbcb' => {typ=>'op', val=>0105600, fmt=>'g' },
'tstb' => {typ=>'op', val=>0105700, fmt=>'g' },
'rorb' => {typ=>'op', val=>0106000, fmt=>'g' },
'rolb' => {typ=>'op', val=>0106100, fmt=>'g' },
'asrb' => {typ=>'op', val=>0106200, fmt=>'g' },
'aslb' => {typ=>'op', val=>0106300, fmt=>'g' },
'mtps' => {typ=>'op', val=>0106400, fmt=>'g' },
'mfpd' => {typ=>'op', val=>0106500, fmt=>'g' },
'mtpd' => {typ=>'op', val=>0106600, fmt=>'g' },
'mfps' => {typ=>'op', val=>0106700, fmt=>'g' },
'movb' => {typ=>'op', val=>0110000, fmt=>'gg' },
'cmpb' => {typ=>'op', val=>0120000, fmt=>'gg' },
'bitb' => {typ=>'op', val=>0130000, fmt=>'gg' },
'bicb' => {typ=>'op', val=>0140000, fmt=>'gg' },
'bisb' => {typ=>'op', val=>0150000, fmt=>'gg' },
'sub' => {typ=>'op', val=>0160000, fmt=>'gg' },
'cfcc' => {typ=>'op', val=>0170000, fmt=>'-' ,fpp=>1 },
'setf' => {typ=>'op', val=>0170001, fmt=>'-' ,fpp=>1 },
'setd' => {typ=>'op', val=>0170011, fmt=>'-' ,fpp=>1 },
'seti' => {typ=>'op', val=>0170002, fmt=>'-' ,fpp=>1 },
'setl' => {typ=>'op', val=>0170012, fmt=>'-' ,fpp=>1 },
'ldfps' => {typ=>'op', val=>0170100, fmt=>'g' ,fpp=>1 },
'stfps' => {typ=>'op', val=>0170200, fmt=>'g' ,fpp=>1 },
'stst' => {typ=>'op', val=>0170300, fmt=>'g' ,fpp=>1 },
'clrf' => {typ=>'op', val=>0170400, fmt=>'g' ,fpp=>1 },
'clrd' => {typ=>'op', val=>0170400, fmt=>'g' ,fpp=>1 }, # alias
'tstf' => {typ=>'op', val=>0170500, fmt=>'g' ,fpp=>1 },
'tstd' => {typ=>'op', val=>0170500, fmt=>'g' ,fpp=>1 }, # alias
'absf' => {typ=>'op', val=>0170600, fmt=>'g' ,fpp=>1 },
'absd' => {typ=>'op', val=>0170600, fmt=>'g' ,fpp=>1 }, # alias
'negf' => {typ=>'op', val=>0170700, fmt=>'g' ,fpp=>1 },
'negd' => {typ=>'op', val=>0170700, fmt=>'g' ,fpp=>1 }, # alias
'mulf' => {typ=>'op', val=>0171000, fmt=>'gr' ,fpp=>1 },
'muld' => {typ=>'op', val=>0171000, fmt=>'gr' ,fpp=>1 }, # alias
'modf' => {typ=>'op', val=>0171400, fmt=>'gr' ,fpp=>1 },
'modd' => {typ=>'op', val=>0171400, fmt=>'gr' ,fpp=>1 }, # alias
'addf' => {typ=>'op', val=>0172000, fmt=>'gr' ,fpp=>1 },
'addd' => {typ=>'op', val=>0172000, fmt=>'gr' ,fpp=>1 }, # alias
'ldf' => {typ=>'op', val=>0172400, fmt=>'gr' ,fpp=>1 },
'ldd' => {typ=>'op', val=>0172400, fmt=>'gr' ,fpp=>1 }, # alias
'subf' => {typ=>'op', val=>0173000, fmt=>'gr' ,fpp=>1 },
'subd' => {typ=>'op', val=>0173000, fmt=>'gr' ,fpp=>1 }, # alias
'cmpf' => {typ=>'op', val=>0173400, fmt=>'gr' ,fpp=>1 },
'cmpd' => {typ=>'op', val=>0173400, fmt=>'gr' ,fpp=>1 }, # alias
'stf' => {typ=>'op', val=>0174000, fmt=>'rg' ,fpp=>1 },
'std' => {typ=>'op', val=>0174000, fmt=>'rg' ,fpp=>1 }, # alias
'divf' => {typ=>'op', val=>0174400, fmt=>'gr' ,fpp=>1 },
'divd' => {typ=>'op', val=>0174400, fmt=>'gr' ,fpp=>1 }, # alias
'stexp' => {typ=>'op', val=>0175000, fmt=>'rg' ,fpp=>1 },
'stcfi' => {typ=>'op', val=>0175400, fmt=>'rg' ,fpp=>1 },
'stcfl' => {typ=>'op', val=>0175400, fmt=>'rg' ,fpp=>1 }, # alias
'stcdi' => {typ=>'op', val=>0175400, fmt=>'rg' ,fpp=>1 }, # alias
'stcdl' => {typ=>'op', val=>0175400, fmt=>'rg' ,fpp=>1 }, # alias
'stcfd' => {typ=>'op', val=>0176000, fmt=>'rg' ,fpp=>1 },
'stcdf' => {typ=>'op', val=>0176000, fmt=>'rg' ,fpp=>1 }, # alias
'ldexp' => {typ=>'op', val=>0176400, fmt=>'gr' ,fpp=>1 },
'ldcif' => {typ=>'op', val=>0177000, fmt=>'gr' ,fpp=>1 },
'ldcid' => {typ=>'op', val=>0177000, fmt=>'gr' ,fpp=>1 }, # alias
'ldclf' => {typ=>'op', val=>0177000, fmt=>'gr' ,fpp=>1 }, # alias
'ldcld' => {typ=>'op', val=>0177000, fmt=>'gr' ,fpp=>1 }, # alias
'ldcdf' => {typ=>'op', val=>0177400, fmt=>'gr' ,fpp=>1 },
'ldcfd' => {typ=>'op', val=>0177400, fmt=>'gr' ,fpp=>1 } # alias
);
# operand formats
my %opfmt = (
'-' => [], # halt,...
'n3' => [{typ=>'e', pref=>''}], # spl
'n6' => [{typ=>'e', pref=>''}], # mark
'n8' => [{typ=>'e', pref=>''}], # trap,emt
'r' => [{typ=>'r', pref=>'o1'}], # rts
'g' => [{typ=>'g', pref=>'o1'}], # inc,...
'rg' => [{typ=>'r', pref=>'o1'}, {typ=>'g', pref=>'o2'}], # xor,jsr
'gr' => [{typ=>'g', pref=>'o2'}, {typ=>'r', pref=>'o1'}], # ash,...
'gg' => [{typ=>'g', pref=>'o1'}, {typ=>'g', pref=>'o2'}], # add,...
's8' => [{typ=>'e', pref=>''}], # br,...
'ru6' => [{typ=>'r', pref=>'o1'}, {typ=>'e', pref=>''}] # sob
);
# psect table
my %psect =
('.abs.' => {dot=>0, dotmax=>0}
);
my $cur_psect = '.abs.'; # current psect
# local symbol table
my %lst =
('.' => {name=>'.', typ=>'dot', val=>0, psect=>'.abs.'},
'...top' => {name=>'...top', typ=>'lbl', val=>0, psect=>'.abs.'}
);
my $llbl_scope = '0'; # current local label scope
my $llbl_ascope = 0; # annonymous local label scope count
# macro table
my %mst;
my $mdefnam; # macro currently being defined
my $mdeflvl = 0; # .macro definition depth
my @mexecstk; # macro execution stack
my $mexit = 0; # mexit flag
my $mautolbl = 30000; # auto-generated label
# rept stack
my @reptstk; # active .rept stack
my $reptlvl = 0; # .rept definition depth
# conditional assembly stack
my @ifstk; # if stack {tst=>0|1,cur=>0|1,dif=>n}
my $ifcond; # current if condition
# .end handlinh
my $end = 0;
# files
my @flist; # list of filenames
my $fstem; # stem of last file name
my $lst_do; # generate listing
my $lst_fname; # listing file name
my $lda_do; # generate lda output
my $lda_fname; # lda file name
my $cof_do; # generate cof output
my $cof_fname; # cof file name
my $lsm_do; # generate lsm output
my $lsm_fname; # lsm file name
my @src;
my %errcnt; # error tag counter
my $errcnt_tot=0; # total error count
my $pass;
my @t_pushback;
my $defincdot = 0; # defered increment for '.'
my $out_dot; # current . for output
my @out_data; # output data
autoflush STDOUT 1 if (-p STDOUT); # autoflush if output into pipe
if ($opts{help}) {
print_help();
exit 0;
}
bailout("no input files specified") if scalar(@ARGV) == 0;
# find stem of last file name
$fstem = $ARGV[-1];
$fstem =~ s|^.*/||; # drop leading dirs
$fstem =~ s|\.mac$||; # drop trailing '.mac'
if ($opts{lst} || $opts{olst}) {
$lst_do = 1;
$lst_fname = create_fname($opts{olst},'.lst');
}
if ($opts{lda} || $opts{olda}) {
$lda_do = 1;
$lda_fname = create_fname($opts{olda},'.lda');
}
if ($opts{cof} || $opts{ocof}) {
$cof_do = 1;
$cof_fname = create_fname($opts{ocof},'.cof');
}
if ($opts{lsm} || $opts{olsm}) {
$lsm_do = 1;
$lsm_fname = create_fname($opts{olsm},'.lsm');
}
# do pass 1
$pass = 1;
foreach my $fname (@ARGV) {
read_file($fname);
}
if ($end == 0 && ($mdeflvl + $reptlvl + # no .end but .macro or .rept open
scalar(@ifstk) != 0)) { # or .endc open
pass1_line(1, 0, ''); # add dummy line
add_err($src[-1], 'E'); # and signal E error
}
exit 0 if $opts{E} || $opts{M}; # stop after pass1 for -E -M
$lst{'...top'}->{val} = $psect{'.abs.'}{dotmax};
dump_sym() if $opts{dsym1};
# prepare pass 2
foreach (keys %psect) {
$psect{$_}{dot} = 0;
}
$lst{'.'}->{val} = 0;
$lst{'.'}->{psect} = '.abs.';
$cur_psect = '.abs.';
$llbl_scope = '0';
# do pass 2
$pass = 2;
pass2();
dump_sym() if $opts{dsym2};
# create object output files
write_lda($lda_fname) if $lda_do;
write_cof($cof_fname) if $cof_do;
write_lsm($lsm_fname) if $lsm_do;
# and exit
if ($errcnt_tot > 0) {
print STDERR "asm-11-E: compilation errors:";
foreach my $err (sort keys %errcnt) {
printf STDERR " %s: %d", $err, $errcnt{$err};
}
print STDERR "\n";
exit 1;
}
exit 0;
#-------------------------------------------------------------------------------
sub create_fname {
my ($fname,$suff) = @_;
if (defined $fname) {
$fname =~ s|\%|$fstem|;
return $fname;
}
$fname = $fstem;
$fname .= $suff unless $fname eq '-';
return $fname;
}
#-------------------------------------------------------------------------------
sub read_file {
my ($fname) = @_;
my $fh;
if ($fname eq "-") {
$fh = *STDIN;
} else {
bailout("'$fname' not found or readable") if (not -r $fname);
$fh = new FileHandle;
$fh->open($fname) or bailout("failed to open '$fname': $!");
}
push @flist, $fname;
if ($opts{M}) {
printf "%s : %s\n", create_fname($opts{olda},'.lda'), $fname;
printf "%s : %s\n", create_fname($opts{olda},'.lst'), $fname;
}
my $lineno = 0;
my $fileno = scalar(@flist);
while (<$fh>) {
chomp;
my $line = $_;
if ($opts{E} && $line !~ m/\s*\.(include|mcall)/) { # if -E and no include
printf "$line\n"; # write to stdout
}
$lineno += 1;
pass1_line($fileno, $lineno, $line);
last if $end;
}
return;
}
#-------------------------------------------------------------------------------
sub exec_macro {
my ($rmexec) = @_;
my $mname = $rmexec->{name};
my $ifstklvl = scalar(@ifstk); # remember initial .if stack depth
push @mexecstk, {name => $mname,
narg => $rmexec->{narg}};
foreach my $rmline (@{$mst{$mname}{body}}) {
my $lrest = $rmline->{line};
my $fileno = $rmline->{fileno};
my $lineno = $rmline->{lineno};
my $line = '';
while ($lrest =~ m/([a-zA-Z\$\.][a-zA-Z0-9\$\.]*)/) {
$line .= $`;
$line .= (defined $rmexec->{vals}{$1}) ? $rmexec->{vals}{$1} : $1;
$lrest = $';
if (length($lrest) > 0 && substr($lrest,0,1) eq "'") { # concatenation
$lrest = substr($lrest,1);
}
}
$line .= $lrest;
$mexit = 0;
pass1_line($fileno, $lineno, $line);
last if $mexit;
}
$mexit = 0;
pop @mexecstk;
while (scalar(@ifstk) > $ifstklvl) {pop @ifstk;} # drop dangling .if's
return;
}
#-------------------------------------------------------------------------------
sub delete_macro {
my ($mname) = @_;
return 1 unless exists $mst{$mname};
delete $mst{$mname};
return 0;
}
#-------------------------------------------------------------------------------
sub pass1_line {
my ($fileno,$lineno,$line) = @_;
my $rl = parse_line($fileno, $lineno, $line);
dump_rl($rl) if $opts{tpass1};
push @src, $rl;
# handle .include ----------------------------------------
if (defined $$rl{oper} && $$rl{oper} eq '.include' && defined $$rl{ifile}) {
my $fnam = $$rl{ifile};
unless ($fnam =~ m|^/|) {
foreach (@{$opts{I}}) {
if (-r "$_/$fnam") {
$fnam = "$_/$fnam";
last;
}
}
}
read_file($fnam);
}
# handle .mcall ------------------------------------------
if (defined $$rl{oper} && $$rl{oper} eq '.mcall') {
foreach my $mname (@{$$rl{mcall}}) {
next if exists $mst{$mname}; # quit if already loaded
my $fname;
foreach (@{$opts{L}}) {
if (-r "$_/${mname}.mac") {
$fname = "$_/${mname}.mac";
last;
}
}
if (defined $fname) {
my $fh = new FileHandle;
$fh->open($fname) or bailout("failed to open '$fname': $!");
push @flist, $fname;
if ($opts{M}) {
printf "%s : %s\n", create_fname($opts{olda},'.lda'), $fname;
printf "%s : %s\n", create_fname($opts{olda},'.lst'), $fname;
}
my $lineno = 0;
my $fileno = scalar(@flist);
my $mseen;
while (<$fh>) {
chomp;
my $line = $_;
$lineno += 1;
$mseen = 1 if $line =~m/^\s*\.macro/; # .macro seen ?
next unless $mseen;
pass1_line($fileno, $lineno, $line);
printf "$line\n" if $opts{E}; # if -E write to stdout
last if $mdeflvl == 0; # final .endm seen ?
}
} else {
add_err($rl,'U');
}
}
}
# handle .rept expansions --------------------------------
if (defined $$rl{oper} && $$rl{oper} eq '.endr') {
if (scalar(@reptstk) > 0) {
my $ifstklvl = scalar(@ifstk); # remember initial .if stack depth
my $cnt = int16($reptstk[-1]{cnt});
for (my $i=0; $i<$cnt; $i++) { # repeat body cnt times
foreach my $rline (@{$reptstk[-1]{body}}) {
$mexit = 0;
pass1_line($rline->{fileno}, $rline->{lineno}, $rline->{line});
last if $mexit;
}
last if $mexit;
}
$mexit = 0;
pop @reptstk;
while (scalar(@ifstk) > $ifstklvl) {pop @ifstk;} # drop dangling .if's
}
}
# handle macro expansions --------------------------------
if (defined $$rl{oper} && defined $$rl{mexec}) {
$$rl{lstdot} = 1 unless $list{me} || $list{meb};
exec_macro($$rl{mexec});
}
return;
}
#-------------------------------------------------------------------------------
sub parse_line {
my ($fileno,$lineno,$line) = @_;
my %l = ( fileno => $fileno, # file number
lineno => $lineno, # line number
line => $line, # line
cl => [split '',$line], # char list
tl => [], # token list
err => '', # error tags
flag => '', # line flags
psect => $cur_psect, # current psect
dot => getdot(), # current dot
outw => [], # output: words
outb => [] # output: bytes
);
my $state = 'start'; # parser state
my $op_code; # op code
my $op_fmt; # op format
my $op_fpp; # true if floating opcode
my @op_ops; # list of operands
my $op_rop; # ref of current operand dsc
my $s_incok;
my $op_ibeg;
my $op_creg; # current register
my $op_cmod; # current modifier
my $op_crel; # current pc relative flag
my $op_cmod_def;
my @e_pbeg;
my $e_ibeg;
my $e_iend;
my $a_sym;
my $a_typ;
my $d_dire;
my @d_elist;
my $c;
my $rt;
my $tmask = 0;
my @stack;
@t_pushback = ();
printf "-- parse: '$line'\n" if $opts{tparse} || $opts{ttoken};
# basics flags
add_flag(\%l, 'm') if scalar(@mexecstk); # in macro execution
add_flag(\%l, 'r') if scalar(@reptstk); # in .rept expansion
# quit if invalid character found (non 7 bit ascii in asm-11)
foreach my $c (@{$l{cl}}) {
if (ord($c) > 127) {
add_err(\%l, 'I');
return \%l;
}
}
# handling for .macro and .rept definitions and conditional blocks
my $dnam = '';
my $ltrim = $line;
$ltrim =~ s/^[a-zA-Z\$\.][a-zA-Z0-9\$\.]*://; # trim label
$ltrim =~ s/^[0-9]+\$]://; # trim local label
$ltrim =~ s/^\s*//; # trim leading white
if ($ltrim =~ m/^(\.[a-zA-Z\$\.][a-zA-Z0-9\$\.]*)/) { # directive seen
$dnam = $1;
}
if (scalar(@ifstk)) { # is .if conditional block active ?
add_flag(\%l,'c'); # flag conditional block
unless ($ifstk[-1]{cur}) { # if (sub-)cond false
$ifstk[-1]{dif} += 1 if $dnam eq '.if'; # nested .if in dropped code
if ($ifstk[-1]{dif} > 0) {
$ifstk[-1]{dif} -= 1 if $dnam eq '.endc'; # nested .endc in dropped code
add_flag(\%l,'d'); # flag condition drop
return \%l; # ignore line
}
unless ($dnam =~ m/^\.(iff|ift|iftf|endc)$/) { # and not sub-cond or end
add_flag(\%l,'d'); # flag condition drop
return \%l; # ignore line
}
}
}
if ($reptlvl > 0) { # handle .rept definition
$reptlvl += 1 if $dnam eq '.rept'; # nested .rept found ?
$reptlvl -= 1 if $dnam eq '.endr'; # .endr found ?
if ($reptlvl > 0) { # final .endr not yet seen
add_flag(\%l,'R'); # flag .rept definition
push @{$reptstk[-1]{body}}, {line => $l{line},
fileno => $l{fileno},
lineno => $l{lineno}
};
return \%l;
}
}
if (defined $mdefnam) { # handle .macro definition
$mdeflvl += 1 if $dnam eq '.macro'; # nested .macro found ?
$mdeflvl -= 1 if $dnam eq '.endm'; # .endm found ?
if ($mdeflvl > 0) {
add_flag(\%l,'M'); # flag .macro definition
push @{$mst{$mdefnam}{body}}, {line => $l{line},
fileno => $l{fileno},
lineno => $l{lineno}
};
return \%l;
}
}
STATE: while (1) {
if ($opts{tparse}) {
printf "-- state = %-8s",$state;
my $rest = '';
foreach my $t (@t_pushback) { $rest .= $t->{val} if defined $t->{val}; }
foreach my $c (@{$l{cl}}) { $rest .= $c; }
$rest =~ s/\s+/ /g;
if (length($rest)>17) {
$rest = substr($rest,0,17) . '...';
}
while (length($rest) < 20) { $rest .= ' ' }
printf "@ \"%s\"", $rest;
printf ", nest = %d", scalar(@e_pbeg) if $state =~ m/^e_/;
print "\n";
}
if ($state eq 'start') { # state: start -------------------
$rt = get_token(\%l, $tmask);
# end of line seen ?
if ($$rt{tag} eq 'EOL') {
last;
# name seen
} elsif ($$rt{tag} eq 'SYM') {
# directive name seen ?
if (exists $pst{$$rt{val}} && $pst{$$rt{val}}{typ} eq 'dir') {
$state = 'oper';
# macro name seen ?
} elsif (exists $mst{$$rt{val}}) {
$state = 'oper';
# otherwise check for label or assignment
} else {
my $isllbl = check_llbl($$rt{val});
$rt = get_token(\%l, $tmask);
# handle local labels
if ($isllbl) {
if ($$rt{tag} eq 'LBL') {
setsym(\%l, 'lbl' ,$l{tl}[-2]{val}, getdot());
$l{lscope} = $llbl_scope;
$l{label} = $l{tl}[-2]{val};
$state = 'start1';
} else {
$state = 'q';
}
# handle assignments
} elsif ($$rt{tag} eq 'ASS') {
$a_sym = $l{tl}[-2]{val};
$a_typ = $l{tl}[-1]{val};
push @stack, 'a_end';
$state = 'e_beg';
# handle normal labels
} elsif ($$rt{tag} eq 'LBL') {
setsym(\%l, 'lbl' ,$l{tl}[-2]{val}, getdot());
$llbl_scope = $l{tl}[-2]{val};
$l{lscope} = $l{tl}[-2]{val};
$l{label} = $l{tl}[-2]{val};
$state = 'start1';
# if neither label or assigmnent, handle as operation or directive
} else {
pushback_token(\%l);
$state = 'oper';
}
}
# anything else seen, treat as implicit .word
} else {
pushback_token(\%l);
$state = 'iword';
}
} elsif ($state eq 'start1') { # state: start1 ------------------
$rt = get_token(\%l, $tmask);
if ($$rt{tag} eq 'EOL') {
last;
} elsif ($$rt{tag} eq 'SYM') {
$state = 'oper';
} else { # not symbol -> implicit .word
pushback_token(\%l);
$state = 'iword';
}
} elsif ($state eq 'oper') { # state: oper --------------------
# Note: state oper is entered with token already on tl list !!
my $rt0 = $l{tl}[-1];
my $op = $$rt0{val};
$l{oper} = $op;
if (exists $pst{$op}) {
my $rs = $pst{$op};
if ($$rs{typ} eq 'dir') { # directives ------------------
$d_dire = $op;
if ($op eq '.word' || # .word ----------------
$op eq '.byte') { # .byte ----------------
$state = 'dl_beg';
} elsif ($op eq '.blkw' || # .blkw ----------------
$op eq '.blkb') { # .blkb ----------------
$state = 'dl_beg';
} elsif ($op eq '.ascii' || # .ascii ---------------
$op eq '.asciz') { # .asciz ---------------
$tmask = TMASK_STRING;
$state = 'al_next';
} elsif ($op eq '.even' || # .even ----------------
$op eq '.odd') { # .odd -----------------
my $dot = getdot();
my $inc = 0;
$inc = 1 if $op eq '.even' && ($dot&01)==1;
$inc = 1 if $op eq '.odd' && ($dot&01)==0;
incdot(1) if $inc;
$l{typ} = 'data';
$l{incdot} = $inc;
$l{lstdot} = 1;
$state = 'end';
} elsif ($op eq '.asect') { # .asect ---------------
# .asect is currently a noop because asect is start default
$l{lstdot} = 1;
$state = 'end';
} elsif ($op eq '.include') { # .include -------------
$rt = get_token(\%l, TMASK_STRING);
if ($$rt{tag} eq 'STR') {
my $ifile = $$rt{val};
my $rt = get_token(\%l, TMASK_STRING);
if ($$rt{tag} eq 'EOL') {
$l{ifile} = substr($ifile,1,-1) unless $l{err} ne '';
$state = 'end';
} else {
$state = 'q';
}
} else {
$state = 'q';
}
} elsif ($op eq '.if') { # .if ------------------
my $tst = 0;
$rt = get_token(\%l, $tmask); # get condition
if ($$rt{tag} eq 'SYM') { # if SYM found
my $cond = $$rt{val};
if ($cond =~ m/^(eq|ne|gt|le|lt|ge)$/) { # eq|ne|gt|le|lt|ge
$rt = get_token(\%l, $tmask); # drop ',' delimiter
pushback_token(\%l) unless check_token($rt, 'DEL', ',');
$ifcond = $cond;
$state = 'dl_beg';
next STATE;
} elsif ($cond =~ m/^(df|ndf)$/) { # df|ndf -----------
$rt = get_token(\%l, $tmask); # drop ',' delimiter
pushback_token(\%l) unless check_token($rt, 'DEL', ',');
my $op = '=';
while (1) {
$rt = get_token(\%l, $tmask); # get symbol name
if ($$rt{tag} eq 'SYM') {
my $def = defined getsym(\%l, $$rt{val});
$tst = $def if $op eq '=';
$tst &= $def if $op eq '&';
$tst |= $def if $op eq '|';
} else {
add_err(\%l, 'A'); # quit with A error
last;
}
$rt = get_token(\%l, $tmask); # get & or ! operator
last if ($$rt{tag} eq 'EOL'); # quit if EOL
$op = undef;
$op = '&' if check_token($rt, 'OP', '&');
$op = '|' if check_token($rt, 'OP', '!');
if (not defined $op) {
add_err(\%l, 'A'); # quit with A error
last;
}
}
$tst = not $tst if $cond eq 'ndf';
} elsif ($cond =~ m/^(b|nb)$/) { # b|nb --------------
$rt = get_token(\%l, $tmask); # drop ',' delimiter
pushback_token(\%l) unless check_token($rt, 'DEL', ',');
my $cnt = 0;
while (1) {
$rt = get_token(\%l, $tmask); # count all tokens
last if ($$rt{tag} eq 'EOL'); # quit if EOL
$cnt += 1;
}
$tst = ($cond eq 'b') ? $cnt == 0 : $cnt > 0;
} elsif ($cond =~ m/^(idn|dif)$/) { # idn|dif ----------
my $val1;
my $val2;
$rt = get_token(\%l, TMASK_MACROARG); # get 1st value
$rt = get_token(\%l, TMASK_MACROARG) if $$rt{tag} eq 'DEL';
if ($$rt{tag} eq 'SYM' || $$rt{tag} eq 'MVAL') {
$val1 = $$rt{val};
$rt = get_token(\%l, TMASK_MACROARG); # get 2nd value
$rt = get_token(\%l, TMASK_MACROARG) if $$rt{tag} eq 'DEL';
if ($$rt{tag} eq 'SYM' || $$rt{tag} eq 'MVAL') {
$val2 = $$rt{val};
}
}
if (defined $val1 && defined $val2) {
$tst = ($cond eq 'idn') ? ($val1 eq $val2) : ($val1 ne $val2);
} else {
add_err(\%l, 'A');
}
} else { # bad condition -> A error
add_err(\%l, 'A');
}
} else { # no conditiion -> A error
add_err(\%l, 'A');
}
add_flag(\%l,'c'); # flag conditional block
push @ifstk, {tst => $tst, cur => $tst, dif => 0};
$state = 'end';
} elsif ($op =~ m/^\.if(t|f|tf)$/) { # .if(f|t|tf) ----------
if (scalar(@ifstk) == 0) { # .if block not active ?
add_err(\%l, 'O'); # quit with O error
last STATE;
}
my $tst = $ifstk[-1]{tst}; # test value
my $cur = $tst; # for .ift
$cur = $tst ? 0 : 1 if $op eq '.iff';
$cur = 1 if $op eq '.iftf';
$ifstk[-1]{cur} = $cur;
$state = 'end';
} elsif ($op eq '.endc') { # .endc ----------------
if (scalar(@ifstk) == 0) { # .if block not active ?
add_err(\%l, 'O'); # quit with O error
last STATE;
}
pop @ifstk;
$state = 'end';
} elsif ($op eq '.macro') { # .macro ---------------
$rt = get_token(\%l, $tmask);
if ($$rt{tag} eq 'SYM') {
add_flag(\%l,'M'); # flag .macro definition
$mdeflvl = 1;
$mdefnam = $$rt{val};
$mst{$mdefnam} = {name => $mdefnam,
args => [],
body => []
};
while (1) { # loop over argument definitions
$rt = get_token(\%l, TMASK_MACROARG);
last if $$rt{tag} eq 'EOL';
next if $$rt{tag} eq 'DEL'; # FIXME_code: is that OK ?
my $mod;
my $key;
my $val;
if ($$rt{tag} eq 'MMOD') { # modifier seen ?
if ($$rt{val} eq '?') { # modifier '?' ?
$mod = $$rt{val};
$rt = get_token(\%l, TMASK_MACROARG);
if ($$rt{tag} eq 'SYM') { # ensure it's symbol
$key = $$rt{val};
} else {
add_err(\%l, 'AQ'); # no symbol after ? -> AQ error
}
} else {
add_err(\%l, 'AQ'); # other then ? modifier -> AQ error
}
} elsif ($$rt{tag} eq 'SYM') { # if symbol
$key = $$rt{val};
} elsif ($$rt{tag} eq 'MKEY') { # if key
$key = $$rt{val};
$rt = get_token(\%l, TMASK_MACROARG);
if ($$rt{tag} eq 'SYM' || $$rt{tag} eq 'MVAL') {
$val = $$rt{val};
} else {
add_err(\%l, 'Q'); # no value after key= -> Q error
}
} else {
add_err(\%l, 'AQ'); # neither mod,sym,key -> AQ error
}
foreach my $rarg (@{$mst{$mdefnam}{args}}) {
if ($rarg->{key} eq $key) { # key already seen ?