-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.lemon
1538 lines (1159 loc) · 30.1 KB
/
grammar.lemon
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
%start_symbol program
%include {
#include <string>
#include <memory>
#include <stdexcept>
#include <cstdint>
#include <cassert>
#include <utility>
#include <numeric>
#include <unistd.h>
#include "Machine.h"
#include "Instruction.h"
#include "Expression.h"
#include "token.h"
#include "common.h"
#include "dp_register.h"
#include "lexer.h"
#include "parser.h"
#include "cxx/mapped_file.h"
#include "pragma.h"
#include "omf.h"
#define LEMON_SUPER parser
#define YYSTACKDEPTH 0
// dynamic stack size!
}
%token_type {Token}
%default_type {int}
%token_prefix tk
// allow X, Y, Z to be used as identifiers if appropriate.
%fallback IDENTIFIER REGISTER_X REGISTER_Y REGISTER_S REGISTER_Z REGISTER_A REGISTER_B REGISTER_K REGISTER_D.
%fallback IDENTIFIER SIGNED UNSIGNED CC.
%fallback IDENTIFIER OPCODE OPCODE_0 OPCODE_2 OPCODE_3 DS DC DCB.
%fallback IDENTIFIER DO WHILE IF.
%fallback IDENTIFIER EXPORT .
%left PIPEPIPE.
%left AMPAMP.
%left PIPE.
%left CARET.
%left AMP.
%left EQEQ BANGEQ.
%left LT LTEQ GT GTEQ.
%left LTLT GTGT.
%left PLUS MINUS.
%left STAR SLASH PERCENT.
%right BANG TILDE.
// % qualifiers.
%token CDECL PASCAL STDCALL WEAK SIZEOF .
%include {
bool is_zp(AddressMode mode) {
switch(mode) {
case zp:
case zp_x:
case zp_y:
case zp_indirect:
case zp_indirect_x:
case zp_indirect_y:
case zp_relative: // bbr/bbs
case zp_indirect_long:
case zp_indirect_long_y:
return true;
default:
return false;
}
}
AddressMode to_zp(AddressMode mode) {
switch(mode) {
case absolute_long:
case absolute:
return zp;
case absolute_long_x:
case absolute_x:
return zp_x;
case absolute_y:
return zp_y;
case absolute_indirect:
return zp_indirect;
case absolute_indirect_x:
return zp_indirect_x;
case absolute_indirect_long:
return zp_indirect_long;
default:
return mode;
}
}
AddressMode to_abs(AddressMode mode) {
switch(mode) {
case zp:
case absolute_long:
return absolute;
case zp_x:
case absolute_long_x:
return absolute_x;
case zp_y:
return absolute_y;
case zp_indirect:
return absolute_indirect;
case zp_indirect_x:
return absolute_indirect_x;
case zp_indirect_long:
return absolute_indirect_long;
default:
return mode;
}
}
OpCode find_opcode(Instruction &instr, AddressMode mode, bool explicit_mode, ExpressionPtr e) {
if (e && e->is_register() && mode != immediate) {
if (!is_zp(mode)) {
if (explicit_mode) {
throw std::domain_error("Invalid address mode for dp register.");
} else {
mode = to_zp(mode);
}
}
}
uint32_t value;
if (!explicit_mode && e->is_integer(value)) {
if (value <= 256) {
mode = to_zp(mode);
}
}
bool ok = Instruction::coerce(instr, mode, explicit_mode);
if (!ok) {
throw std::domain_error("Invalid address mode for this instruction.");
return OpCode();
}
return OpCode(instr, mode);
}
}
program ::= top_level .
top_level ::= .
top_level ::= top_level segment .
top_level ::= top_level record .
top_level ::= top_level macro .
top_level ::= top_level tl_directive .
tl_directive ::= EOL.
tl_directive ::= error EOL.
tl_directive ::= EXPORT identifier_list(L). {
for (auto &x : L) _export_set.insert(x);
}
/*
* IMPORT name [, name...]
* IMPORT name:type
* IMPORT (name, name):type
* todo -- IMPORT %cdecl name? IMPORT name:%cdecl?
*/
tl_directive ::= IMPORT import_list.
%type import_list {void}
%type import_item {void}
import_list ::= import_item.
import_list ::= import_item COMMA nl import_list.
import_item ::= IDENTIFIER(t) . {
identifier id = expand_at(t);
if (id) _import_set.emplace(id, nullptr);
}
import_item ::= IDENTIFIER(id) COLON IDENTIFIER(type). {
identifier id = expand_at(id); //?
if (id) {
_import_set.emplace(id, type.id);
_types.emplace(id, type.id);
}
}
import_item ::= LPAREN identifier_list(l) RPAREN. {
for(auto id : l) {
_import_set.emplace(id, nullptr);
}
}
import_item ::= LPAREN identifier_list(l) RPAREN COLON IDENTIFIER(type). {
for(auto id : l) {
_import_set.emplace(id, type.id);
_types.emplace(id, type.id);
}
}
tl_directive ::= LABEL(id) EQU expr(e). {
install_equate(id, e);
}
tl_directive ::= INCLUDE(I) STRING(s). {
expand_include(I, s.string_value());
}
nl ::= .
nl ::= EOL.
segment ::= segment_start segment_attr nl block . {
end_segment();
}
record ::= record_start(R) nl record_block. {
install_record(R);
}
macro ::= macro_start(M) opt_macro_parms(P) nl macro_block. {
install_macro(M, P);
}
%type macro_parms { std::vector<Token> }
%type opt_macro_parms { std::vector<Token> }
opt_macro_parms ::= .
opt_macro_parms(X) ::= macro_parms(X).
macro_parms(L) ::= MPARAM(X). { L.emplace_back(std::move(X)); }
macro_parms(L) ::= macro_parms(L) COMMA nl MPARAM(X). { L.emplace_back(std::move(X)); }
segment_attr ::= .
segment_attr ::= LPAREN nl RPAREN.
segment_attr ::= LPAREN nl attr_list nl RPAREN.
segment_attr ::= LPAREN error RPAREN.
attr_list ::= attr .
attr_list ::= attr_list COMMA nl attr .
%include {
unsigned reparse_pragma(const std::string &s);
char reparse_modifier(const std::string &s);
#include "grammar.h"
}
attr ::= IDENTIFIER(ID). {
const std::string &s = ID.string_value();
unsigned type = reparse_pragma(s);
switch(type) {
case 0:
warn(ID, "Unknown pragma: %s", s.c_str());
break;
default:
warn(ID, "Pragma requires a parameter: %s", s.c_str());
break;
case p_CDECL:
_segment->convention = Segment::cdecl;
break;
case p_PASCAL:
_segment->convention = Segment::pascal;
break;
case p_STDCALL:
_segment->convention = Segment::stdcall;
break;
case p_NAKED:
_segment->convention = Segment::naked;
break;
case p_RTS:
_segment->return_type = Segment::rts;
break;
case p_RTL:
_segment->return_type = Segment::rtl;
break;
case p_RTI:
_segment->return_type = Segment::rti;
break;
case p_PRIVATE:
_segment->kind |= OMF::ATTR_PRIVATE;
break;
case p_PUBLIC:
_segment->kind &= ~OMF::ATTR_PRIVATE;
break;
case p_VOID:
_segment->return_size = 0;
break;
case p_DYNAMIC:
_segment->kind |= OMF::ATTR_DYNAMIC;
break;
case p_DATABANK:
_segment->databank = true;
break;
case p_DEBUG:
_segment->debug = true;
break;
case p_VOLATILE:
_segment->has_volatile = true;
break;
case p_NORETURN:
_segment->noreturn = true;
break;
}
}
attr ::= IDENTIFIER(ID) EQ STRING(S). {
const std::string &s = ID.string_value();
unsigned type = reparse_pragma(s);
switch(type) {
case 0:
warn(ID, "Unknown pragma: %s", s.c_str());
break;
default:
warn(ID, "Pragma does not take a string parameter: %s", s.c_str());
break;
case p_SEGMENT:
_segment->segment = S.id;
break;
}
}
attr ::= IDENTIFIER(ID) EQ expr(e). {
const std::string &s = ID.string_value();
unsigned type = reparse_pragma(s);
e = e->simplify();
uint32_t i;
if (e->is_integer(i)) {
switch(type) {
case 0:
warn(ID, "Unknown pragma: %s", s.c_str());
break;
default:
warn(ID, "Pragma does not take an expression parameter: %s", s.c_str());
break;
case p_LOCALS:
_segment->local_size = i;
break;
case p_PARAMETERS:
_segment->parm_size = i;
break;
case p_RETURN:
_segment->return_size = i;
break;
case p_KIND:
_segment->kind = i;
break;
}
}
else {
warn(ID, "Expression too complex for pragma %s", s.c_str());
}
}
%type opt_identifier {identifier}
opt_identifier ::= .
opt_identifier(RV) ::= IDENTIFIER(ID). { RV = ID.id; }
segment_start ::= FUNCTION opt_identifier(name). {
begin_segment(name, code);
}
segment_start ::= DATA opt_identifier(name). {
begin_segment(name, data);
}
/*
* records
*/
%type record_start {Token}
record_start(R) ::= RECORD IDENTIFIER(X). {
R = std::move(X);
_tmp_record = {};
}
record_block ::= LBRACKET record_block_lines RBRACKET.
record_block ::= LBRACKET error RBRACKET.
record_block_lines ::= record_block_line .
record_block_lines ::= record_block_lines EOL record_block_line.
record_block_line ::= error EOL .
record_block_line ::= .
record_block_line ::= record_label.
record_block_line ::= record_label(L) DS(DS) dc_modifier(M) expr(E) . {
install_record_ds(DS, L, M, std::move(E));
}
record_block_line ::= DS(DS) dc_modifier(M) expr(E) . {
install_record_ds(DS, nullptr, M, std::move(E));
}
%type record_label {identifier}
record_label(L) ::= LABEL(ID). {
L = install_record_label(ID);
}
%type dc_modifier {int}
dc_modifier(RV) ::= . { RV = 0; }
dc_modifier(RV) ::= DOT IDENTIFIER(ID). {
const std::string &s = ID.string_value();
int type = reparse_modifier(s);
switch (type) {
case 'b':
RV = 1;
break;
case 'w':
RV = 2;
break;
case 'l':
RV = 4;
break;
default:
RV = 1;
error(ID, "Invalid modifier: %s", s.c_str());
break;
}
}
/*
* macro definitions
*/
%type macro_start { Token }
%type macro_block { void }
%type macro_tokens { void }
%type macro_lbracket { void }
%type macro_rbracket { void }
macro_start(M) ::= MACRO IDENTIFIER(X). {
M = std::move(X);
_tmp_macro = {};
}
%wildcard ANY.
/*
* macro block. capture all tokens, balance () and {}.
*/
macro_block ::= LBRACKET macro_tokens RBRACKET.
macro_block ::= LBRACKET error RBRACKET.
macro_tokens ::= .
macro_tokens ::= macro_tokens macro_token.
macro_token ::= macro_lbracket macro_tokens macro_rbracket.
macro_token ::= macro_lbracket error macro_rbracket.
macro_token ::= macro_lparen macro_tokens macro_rparen.
macro_token ::= macro_lparen error macro_rparen.
macro_token ::= ANY(X). { _tmp_macro.body.emplace_back(std::move(X)); }
macro_lbracket ::= LBRACKET(X). { _tmp_macro.body.emplace_back(std::move(X)); }
macro_rbracket ::= RBRACKET(X). { _tmp_macro.body.emplace_back(std::move(X)); }
macro_lparen ::= LPAREN(X). { _tmp_macro.body.emplace_back(std::move(X)); }
macro_rparen ::= RPAREN(X). { _tmp_macro.body.emplace_back(std::move(X)); }
/*
*
*/
block ::= LBRACKET block_lines RBRACKET.
block ::= LBRACKET error RBRACKET.
block_lines ::= block_line .
block_lines ::= block_lines EOL block_line.
block_line ::= error EOL .
block_line ::= LABEL(ID) EQU expr(e). {
install_equate(ID, e);
}
block_line ::= EQU(E) expr(e). {
warn(E, "Missing label");
}
block_line ::= opt_label(L) DS(DS) dc_modifier(M) expr(e). {
add_ds(DS, L, M, std::move(e));
}
block_line ::= opt_label opcode(OP). {
add_line(std::move(OP));
}
block_line ::= opt_label block_opcode.
block_line ::= opt_label directive.
%type opt_label {identifier}
opt_label(RV) ::= . { RV = nullptr; }
opt_label(RV) ::= LABEL(ID). { RV = add_label(ID); }
block_opcode ::= .
block_opcode ::= if_stmt.
block_opcode ::= do_stmt.
block_opcode ::= while_stmt.
%type if_head {BasicLinePtr}
if_head(RV) ::= IF condition(C). {
auto sym = gen_sym();
auto b = BasicLine::Make(SMART_BRANCH, Expression::Identifier(sym));
b->branch = branch{branch::invert(C), false};
add_line(std::move(b));
RV = BasicLine::Make(sym);
}
if_stmt ::= if_head(T) nl block. {
add_line(std::move(T));
}
if_stmt ::= if_head(T) opcode(OP). {
add_line(std::move(OP));
add_line(std::move(T));
}
%type do_head {BasicLinePtr}
do_head(RV) ::= DO. {
auto sym = gen_sym();
add_label(sym, true);
auto b = BasicLine::Make(SMART_BRANCH, Expression::Identifier(sym));
b->branch = branch{branch::always, false};
RV = std::move(b);
}
do_stmt ::= do_head(T) nl block nl WHILE condition(C). {
T->branch = branch{C, false};
add_line(std::move(T));
}
do_stmt ::= do_head(T) opcode(OP) WHILE condition(C). {
add_line(std::move(OP));
T->branch = branch{C, false};
add_line(std::move(T));
}
%type while_head {std::pair<BasicLinePtr, BasicLinePtr>}
while_head(RV) ::= WHILE condition(C). {
auto ssym = gen_sym();
auto esym = gen_sym();
add_label(ssym, true);
auto b = BasicLine::Make(SMART_BRANCH, Expression::Identifier(esym));
b->branch = branch{branch::invert(C), false};
add_line(std::move(b));
b = BasicLine::Make(SMART_BRANCH, Expression::Identifier(ssym));
b->branch = branch{branch::always, false};
RV = std::make_pair(std::move(b), BasicLine::Make(esym));
}
while_stmt ::= while_head(T) nl block. {
add_line(std::move(T.first));
add_line(std::move(T.second));
}
while_stmt ::= while_head(T) opcode(OP). {
add_line(std::move(OP));
add_line(std::move(T.first));
add_line(std::move(T.second));
}
%type opcode {BasicLinePtr}
%type block {std::vector<BasicLinePtr> }
/* mvn or mvp -- two parameters.*/
opcode(RV) ::= OPCODE_2(INSTR) expr(a) COMMA expr(b). {
auto instr = INSTR.instruction_value();
AddressMode mode;
if (instr.hasAddressMode(block)) mode = block;
if (instr.hasAddressMode(zp_relative)) mode = zp_relative;
a = a->simplify();
b = b->simplify();
RV = BasicLine::Make(instr.mnemonic(), mode, std::move(a), std::move(b));
}
/* bbs, bbr, etc -- three parameters.*/
opcode(RV) ::= OPCODE_3(INSTR) expr(a) COMMA expr(b) COMMA expr(c). {
auto instr = INSTR.instruction_value();
a = a->simplify();
b = b->simplify();
c = c->simplify();
// not yet!
RV = nullptr;
}
opcode(RV) ::= OPCODE_0(INSTR). {
auto instr = INSTR.instruction_value();
RV = BasicLine::Make(instr.mnemonic(), implied);
}
opcode(RV) ::= OPCODE(INSTR) operand(op). {
AddressMode mode;
bool explicit_mode;
ExpressionPtr e;
auto instr = INSTR.instruction_value();
std::tie(mode, explicit_mode, e) = op;
if (e) e = e->simplify();
OpCode opcode;
try {
opcode = find_opcode(instr, mode, explicit_mode, e);
} catch(std::exception &e) {
error(INSTR, e.what());
}
RV = BasicLine::Make(opcode, std::move(e));
}
opcode(RV) ::= OPCODE(INSTR) REGISTER_A. {
// inc a, dec a, etc.
auto instr = INSTR.instruction_value();
bool ok = false;
switch (instr.mnemonic()) {
case INC:
case DEC:
case ASL:
case LSR:
case ROL:
case ROR:
ok = true;
break;
default:
break;
}
if (ok) {
RV = BasicLine::Make(instr.mnemonic(), implied);
} else {
error(INSTR, "Invalid address mode.");
}
}
opcode(RV) ::= OPCODE(INSTR) REGISTER_X. {
// inc x -> inx
auto instr = INSTR.instruction_value();
switch (instr.mnemonic()) {
case INC:
RV = BasicLine::Make(INX, implied);
break;
case DEC:
RV = BasicLine::Make(DEX, implied);
break;
default:
error(INSTR, "Invalid address mode.");
break;
}
}
opcode(RV) ::= OPCODE(INSTR) REGISTER_Y. {
// inc y -> iny
auto instr = INSTR.instruction_value();
switch (instr.mnemonic()) {
case INC:
RV = BasicLine::Make(INY, implied);
break;
case DEC:
RV = BasicLine::Make(DEY, implied);
break;
default:
error(INSTR, "Invalid address mode.");
break;
}
}
opcode(RV) ::= BRANCH cc(c) COMMA expr(e). {
RV = BasicLine::Make(SMART_BRANCH, std::move(e));
RV->branch = branch{c, false};
}
opcode(RV) ::= BRANCH expr(e). {
// branch target
RV = BasicLine::Make(SMART_BRANCH, std::move(e));
RV->branch = branch{branch::always, false};
}
block_opcode ::= PUSH push_list.
push_list ::= push_item.
push_list ::= push_list COMMA push_item.
push_item ::= REGISTER_A. {
add_line(BasicLine::Make(PHA, implied));
}
push_item ::= REGISTER_B. {
add_line(BasicLine::Make(PHB, implied));
}
push_item ::= REGISTER_D. {
add_line(BasicLine::Make(PHD, implied));
}
push_item ::= REGISTER_K. {
add_line(BasicLine::Make(PHK, implied));
}
push_item ::= REGISTER_P. {
add_line(BasicLine::Make(PHP, implied));
}
push_item ::= REGISTER_X. {
add_line(BasicLine::Make(PHX, implied));
}
push_item ::= REGISTER_Y. {
add_line(BasicLine::Make(PHY, implied));
}
push_item ::= REGISTER_Z. {
add_line(BasicLine::Make(PHZ, implied));
}
push_item ::= HASH expr(e). {
add_line(BasicLine::Make(PEA, absolute, std::move(e)));
}
push_item ::= dp_register(dp). {
add_line(BasicLine::Make(PEI, zp_indirect, std::move(dp)));
}
block_opcode ::= PULL pull_list.
pull_list ::= pull_item.
pull_list ::= pull_list COMMA pull_item.
pull_item ::= REGISTER_A. {
add_line(BasicLine::Make(PLA, implied));
}
pull_item ::= REGISTER_B. {
add_line(BasicLine::Make(PLB, implied));
}
pull_item ::= REGISTER_D. {
add_line(BasicLine::Make(PLD, implied));
}
pull_item ::= REGISTER_P. {
add_line(BasicLine::Make(PLP, implied));
}
pull_item ::= REGISTER_X. {
add_line(BasicLine::Make(PLX, implied));
}
pull_item ::= REGISTER_Y. {
add_line(BasicLine::Make(PLY, implied));
}
pull_item ::= REGISTER_Z. {
add_line(BasicLine::Make(PLZ, implied));
}
%type begin_macro {Token}
begin_macro(M) ::= IDENTIFIER(M). {
_tmp_macro = {};
}
block_opcode ::= begin_macro(M) macro_tokens. {
// might be a macro!
expand_macro(M, std::move(_tmp_macro.body));
_tmp_macro = {};
}
%type cc {branch::branch_type}
%type condition {branch::branch_type}
condition(RV) ::= LPAREN cc(C) RPAREN. { RV = C; }
cc(RV) ::= CC(c). {
RV = c.branch_value();
}
cc(RV) ::= BANG CC(c). {
RV = branch::invert(c.branch_value());
}
cc(RV) ::= SIGNED CC(c). {
RV = branch::make_signed(c.branch_value());
}
cc(RV) ::= UNSIGNED CC(c). {
RV = c.branch_value();
}
cc(RV) ::= expr(e). {
uint32_t i;
e = e->simplify();
if (e->is_integer(i)) {
RV = i ? branch::always : branch::never;
}
else {
RV = branch::always;
error("Expression too complex for branch");
}
}
opcode(RV) ::= SMART_BRANCH(b) expr(e). {
// operand vs expression... __bgt <xxx ?
RV = BasicLine::Make(SMART_BRANCH, std::move(e));
RV->branch = branch{b.branch_value(), false};
}
/* dc.b expression [, expression] */
opcode(RV) ::= DC dc_modifier(M) expr_list(e). {
Directive op;
switch(M) {
case 0:
case 2: op = DCW; break;
case 1: op = DCB; break;
case 4: op = DCL; break;
}
//e = e->simplify();
RV = BasicLine::Make(op, std::move(e));
}
/* dcb count,expression */
opcode(RV) ::= DCB(t) dc_modifier(M) expr(count) COMMA expr(e). {
Directive op;
switch(M) {
case 0:
case 2: op = DCW; break;
case 1: op = DCB; break;
case 4: op = DCL; break;
}
count = count->simplify();
e = e->simplify();
uint32_t i;
if (count->is_integer(i)) {
if (i == 0) warn(t, "Invalid DCB count");
// assign_pc expects a vector.
std::vector<ExpressionPtr> tmp(i, e);
RV = BasicLine::Make(op, Expression::Vector(std::move(tmp)));
} else {
error(t, "Invalid DCB count");
}
}
/*
* str - raw string
* str.b - pascal string
* str.w - gsos string
* str.l - longword
*
* str "hello",$00 to make c string.
* str.b "hello" -> 5,'h','e','l','l','o'
*/
%type str_list {std::vector<ExpressionPtr>}
str_list(l) ::= expr(e). { l.emplace_back(std::move(e));}
str_list(l) ::= STRING(s). { l.emplace_back(Expression::String(s.id)); }
str_list(l) ::= str_list(l) COMMA expr(e). { l.emplace_back(std::move(e));}
str_list(l) ::= str_list(l) COMMA STRING(s). { l.emplace_back(Expression::String(s.id)); }
opcode(RV) ::= STR(t) dc_modifier(M) str_list(l). {
size_t size = 0;
for (const auto &e : l) {
const std::string *s;
if (e->is_string(s)) {
size += s->length();
}
else size += 1;
}
Directive op = kUndefinedDirective;
switch(M) {
case 2: op = DCW; break;
case 1: op = DCB; break;
case 4: op = DCL; break;
}
RV = nullptr;
}
opcode(RV) ::= ALIGN expr(e). {
e = e->simplify();
RV = BasicLine::Make(ALIGN, std::move(e));
}
directive ::= PRAGMA(P) pragma_list. {
if (!_segment)
warn(P, "PRAGMA outside of segment.");
}
// differes from attr_list in that nl not allowed.
pragma_list ::= attr.
pragma_list ::= pragma_list COMMA attr.
directive ::= EXPORT identifier_list(L). {
for (auto &x : L) _export_set.insert(x);
}
directive ::= IMPORT import_list.
directive ::= STRONG identifier_list(L). {
if (_segment)
_segment->strong_vector.insert(_segment->strong_vector.end(), L.begin(), L.end());
}
%type identifier_list { std::vector<identifier> }
identifier_list(L) ::= IDENTIFIER(a). {
L.push_back(a.id);
}