-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathassem2.cpp
More file actions
1797 lines (1726 loc) · 80.9 KB
/
assem2.cpp
File metadata and controls
1797 lines (1726 loc) · 80.9 KB
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
/**************************** assem2.cpp ********************************
* Author: Agner Fog
* Date created: 2017-04-17
* Last modified: 2021-08-11
* Version: 1.13
* Project: Binary tools for ForwardCom instruction set
* Module: assem.cpp
* Description:
* Module for assembling ForwardCom .as files.
* This module contains:
* - expression(): Interpretation of expressions containing operators and
* any type of operands.
* Copyright 2017-2024 GNU General Public License http://www.gnu.org/licenses
******************************************************************************/
#include "stdafx.h"
// Interpret and evaluate expression
SExpression CAssembler::expression(uint32_t tok1, uint32_t maxtok, uint32_t options) {
// tok1: index to first token,
// maxtok: maximum number of tokens to use,
// options: 0: normal,
// 1: unsigned
// 2: inside []. interpret as memory operand
// 4: interpret option = keyword
// 8: inside {}. has no meaning yet
// 0x10: check syntax and count tokens, but do not call functions or report numeric
// overflow, wrong operand types, or unknown names
// This function scans the tokens and finds the operator with lowest priority.
// The function is called recursively for each operand to this operator.
// The level of parantheses is saved in the brackets stack.
// The scanning terminates at any of these conditions:
// * a token that cannot be part of the expression is encountered
// * all tokens are used
// * a comma is encountered
// * an unmatched end bracket is encountered
uint32_t tok; // current token
uint32_t toklow = tok1; // operator with lowest priority
uint32_t tokcolon = 0; // matching triadic operator with lowest priority
uint32_t ntok = 0; // number of tokens used
uint32_t priority = 0; // priority of this operator
uint32_t bracketlevel = 0; // number of brackets in stack
uint32_t state = 0; // 0: expecting value, 1: after value, expecting operator or end
uint32_t i; // loop counter
uint32_t temp; // temporary result
uint32_t tokid; // token.id
int32_t symi; // symbol index
bool is_local = false; // symbol is local constant
uint8_t endbracket; // expected end bracket
SExpression exp1, exp2; // expressions during evaluation
zeroAllMembers(exp1); // reset exp1
exp1.tokens = 1;
for (tok = tok1; tok < tok1 + maxtok; tok++) {
if (lineError) {exp1.etype = 0; return exp1;}
if (tokens[tok].type == TOK_OPR) {
// operator found. search for brackets
if (tokens[tok].priority == 1 || tokens[tok].priority == 14) {
// bracket found. ?: operator treated as bracket here
switch (tokens[tok].id) {
case '?':
if (tokens[tok].priority > priority && bracketlevel == 0) { // if multiple ?:, split by the last one
priority = tokens[tok].priority; toklow = tok;
}
// continue in next case
case '(': case '[': case '{': // opening bracket. push on bracket stack
brackets.push(uint8_t(tokens[tok].id));
bracketlevel++;
state = 0;
break;
case ')': case ']': case '}': case ':': // closing bracket
if (bracketlevel == 0) {
goto EXIT_LOOP; // this end bracket is not part of the expression.
}
// remove matching opening bracket from stack
bracketlevel--;
endbracket = brackets.pop();
switch (endbracket) {
case '(': endbracket = ')'; break;
case '[': endbracket = ']'; break;
case '{': endbracket = '}'; break;
case '?': endbracket = ':'; break;
}
if (endbracket != tokens[tok].id) {
// end bracket does not match begin bracket
errors.report(tokens[tok].pos, tokens[tok].stringLength, ERR_BRACKET_END);
goto EXIT_LOOP;
}
if (tokens[tok].id == ':') {
if (bracketlevel == 0 && priority == 14 && tokcolon == 0) {
tokcolon = tok; // ':' matches current '?' with lowest priority
}
state = 0;
continue;
}
state = 1;
continue; // finished with this token
}
}
if (bracketlevel) continue; // don't search for priority inside brackets
if (state == 1) {
// expecting operator
if (tokens[tok].id == ';') break; // end at semicolon
if (tokens[tok].id == ',' && !(options & 2)) break; // end at comma, except inside []
if (tokens[tok].id == '=' && !(options & 6)) break; // end at =, except inside [] or when interpreting option = value
if (tokens[tok].priority >= priority) { // if multiple operators with same priority, split by the last one to get the first evaluated first
// operator with lower priority found
priority = tokens[tok].priority;
toklow = tok;
}
if (tokens[tok].priority == 3) state = 1; else state = 0; // state 0 except after monadic operator
}
else if (state == 0 && (tokens[tok].id == '-' || tokens[tok].id == '+' || tokens[tok].priority == 3)) {
// monadic operator
if (priority < 3) {
priority = 3; toklow = tok;
}
}
else {
errors.report(tokens[tok]); break; // unexpected operator
}
}
else {
// not an operator
if (bracketlevel) continue; // inside brackets: search only for end bracket
if (state == 0) {
// expecting value
switch (tokens[tok].type) {
case TOK_NAM: case TOK_LAB: case TOK_VAR: case TOK_SEC:
case TOK_NUM: case TOK_FLT: case TOK_CHA: case TOK_STR:
case TOK_REG: case TOK_SYM: case TOK_XPR: case TOK_OPT:
state = 1; // allowed value tokens
break;
case TOK_TYP:
state = 1; // type expression
break;
case TOK_HLL:
if (tokens[tok].id == HLL_FALSE || tokens[tok].id == HLL_TRUE) {
state = 1;
}
else {
errors.report(tokens[tok]);
}
break;
default:
errors.report(tokens[tok]); break;
}
}
else {
break; // no operator found after value. end here
}
}
}
EXIT_LOOP:
if (lineError) {exp1.etype = 0; return exp1;}
// number of tokens used
ntok = tok - tok1;
exp1.tokens = ntok;
if (bracketlevel) {
endbracket = brackets.pop();
errors.report(tokens[tok1].pos, tokens[tok].pos - tokens[tok1].pos, endbracket == '?' ? ERR_QUESTION_MARK : ERR_BRACKET_BEGIN);
if (exp1.etype == 0) exp1.etype = XPR_INT;
return exp1;
}
if (ntok == 0) { // no expression found
if (maxtok == 0 && tok > 0) tok--;
errors.report(tokens[tok].pos, tokens[tok].stringLength, ERR_MISSING_EXPR);
return exp1;
}
switch (priority) {
case 0: // no operator found. just an expression
if (ntok > 2 && tokens[tok1].type == TOK_OPR && tokens[tok1].priority == 1) {
// this is an expression in brackets
uint32_t option1 = options;
if (tokens[tok1].id == '[') {
if (options & 2) errors.report(tokens[tok1]); // nested [[]] not allowed
option1 |= 2;
}
if (tokens[tok1].id == '{') option1 |= 8;
// evaluate expression inside bracket
exp1 = expression(tok1 + 1, ntok - 2, option1);
exp1.tokens += 2;
goto RETURNEXP1;
}
else if (ntok == 1) {
// this is a single token. get value
switch (tokens[tok1].type) {
case TOK_LAB: case TOK_VAR: case TOK_SEC: case TOK_SYM:
exp1.etype = XPR_SYM1; // symbol address
exp1.sym3 = tokens[tok1].id;
symi = findSymbol(exp1.sym3);
// is symbol local with known value?
is_local = symi > 0 && symbols[symi].st_bind == STB_LOCAL && (symbols[symi].st_type == STT_CONSTANT || symbols[symi].st_type == STT_VARIABLE);
if (options & 2) { // symbol inside []
exp1.etype |= XPR_MEM;
exp1.sym3 = 0;
if (is_local) {
exp1.offset_mem = tokens[tok1].value.w;// don't take value from symbol, it may change
exp1.etype &= ~XPR_SYM1; // symbol reference no longer needed
exp1.etype |= XPR_OFFSET; // has offset
}
else {
exp1.sym1 = tokens[tok1].id;
}
if (exp1.etype & (XPR_FLT | XPR_STRING)) { // float or string not allowed in memory operand
errors.report(tokens[tok1].pos, tokens[tok1].stringLength, ERR_WRONG_TYPE);
}
}
else { // symbol outside []
if (is_local) {
if (symbols[symi].st_other & STV_FLOAT) exp1.etype |= XPR_FLT;
else exp1.etype |= XPR_INT;
exp1.value.i = tokens[tok1].value.u; // don't take value from symbol, it may change
if (symbols[symi].st_other & STV_STRING) {
exp1.etype = XPR_STRING;
exp1.sym2 = (uint32_t)symbols[symi].st_unitnum; // sym2 used for string length
}
else {
exp1.etype &= ~XPR_SYM1; // symbol reference no longer needed
exp1.sym3 = 0;
}
}
else {
exp1.etype |= XPR_INT; // type not known yet?
exp1.sym3 = tokens[tok1].id;
}
}
break;
case TOK_NUM:
if (options & 2) { // number inside [] is offset
exp1.etype = XPR_OFFSET; // integer value
exp1.offset_mem = (int32_t)interpretNumber((char*)buf()+tokens[tok1].pos, tokens[tok1].stringLength, &temp);
}
else { // number outside [] is operand
exp1.etype = XPR_INT; // integer value
exp1.value.i = interpretNumber((char*)buf() + tokens[tok1].pos, tokens[tok1].stringLength, &temp);
}
if (temp) errors.report(tokens[tok1]);
break;
case TOK_FLT:
exp1.etype = XPR_FLT; // floating point value
exp1.value.d = interpretFloat((char*)buf()+tokens[tok1].pos, tokens[tok1].stringLength);
if (options & 2) { // float not allowed in memory operand
errors.report(tokens[tok1].pos, tokens[tok1].stringLength, ERR_WRONG_TYPE);
}
break;
case TOK_CHA: { // character(s). convert to integer
exp1.etype = XPR_INT;
exp1.value.u = 0;
bool escape = false; // check for \ escape characters
int j = 0; // count characters
for (i = 0; i < tokens[tok1].stringLength; i++) {
uint8_t c = get<uint8_t>(tokens[tok1].pos + i);
if (c == '\\' && !escape) {
escape = true; continue; // escape next character
}
if (escape) { // special escape characters
switch (c) {
case '\\': break;
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
case '0': c = 0; break;
}
}
escape = false;
exp1.value.u += uint64_t(c) << j*8;
j++;
}
if (options & 2) { // string not allowed in memory operand
errors.report(tokens[tok1].pos, tokens[tok1].stringLength, ERR_WRONG_TYPE);
}
break;}
case TOK_STR: { // string
exp1.etype = XPR_STRING;
exp1.value.u = stringBuffer.dataSize(); // save position of string
exp1.sym2 = tokens[tok1].stringLength; // string length
bool escape = false; // check for \ escape characters
for (i = 0; i < tokens[tok1].stringLength; i++) {
char c = get<char>(tokens[tok1].pos + i);
if (c == '\\' && !escape) {
escape = true; continue; // escape next character
}
if (escape) { // special escape characters
switch (c) {
case '\\': escape = false; break;
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
case '0': c = 0; break;
}
}
if (escape && exp1.sym2) exp1.sym2--; // reduce length
stringBuffer.put(c);
escape = false;
}
stringBuffer.put(char(0)); // terminate string
if (options & 2) { // string not allowed in memory operand
errors.report(tokens[tok1].pos, tokens[tok1].stringLength, ERR_WRONG_TYPE);
}
break;}
case TOK_REG:
if (options & 2) { // register inside [] is base register
exp1.etype = XPR_BASE | XPR_MEM;
exp1.base = uint8_t(tokens[tok1].id);
if ((tokens[tok1].id & 0xFE0) == REG_SPEC) {
exp1.base = tokens[tok1].id >> 16; // special register. to do: check if register type is valid
}
}
else { // normal register operand
exp1.etype = XPR_REG | XPR_REG1;
exp1.reg1 = tokens[tok1].id;
}
break;
case TOK_NAM:
if ((options & 0x10) == 0) errors.report(tokens[tok1]);
exp1.etype |= XPR_UNRESOLV; // unresolved name
break;
case TOK_OPT:
exp1.etype = XPR_OPTION;
if ((tokens[tok1].id) == OPT_SCALAR) {
exp1.etype |= XPR_SCALAR;
}
else {
exp1.value.u = tokens[tok1].id;
}
break;
case TOK_XPR: // expression
if (tokens[tok1].value.u < expressions.numEntries()) {
exp1 = expressions[tokens[tok1].value.w];
exp1.tokens = ntok;
if ((exp1.etype & XPR_REG) && !(exp1.etype & XPR_MEM) && (options & 2)) { // register inside [] is base register
exp1.etype = XPR_BASE | XPR_MEM;
exp1.base = exp1.reg1;
exp1.reg1 = 0;
}
}
else errors.report(tokens[tok1]);
break;
case TOK_TYP:
exp1.etype = XPR_TYPENAME;
exp1.value.u = tokens[tok1].id;
break;
case TOK_HLL:
if (tokens[tok1].id == HLL_FALSE || tokens[tok1].id == HLL_TRUE) { // translate to constant
exp1.etype = XPR_INT;
exp1.value.u = tokens[tok1].id & 1;
}
else {
errors.report(tokens[tok1]);
}
break;
default:
errors.report(tokens[tok1]);
}
if (options & 2) exp1.etype |= XPR_MEM; // inside [], interpret as memory operand
goto RETURNEXP1;
}
else {
// unrecognized token
errors.report(tokens[tok1]);
}
break;
case 3: // monadic operator
if (toklow == tok1) { // operator comes first
exp1 = expression(toklow + 1, maxtok - 1, options); // evaluate the rest
if (exp1.etype & XPR_UNRESOLV) {
exp1.tokens++; // unresolved expression. return unresolved result
goto RETURNEXP1;
}
zeroAllMembers(exp2); // zero exp2
switch (tokens[toklow].id) {
case '+': // value is unchanged
exp1.tokens++;
goto RETURNEXP1;; // value is unchanged
case '-':
if (exp1.etype & (XPR_OP | XPR_REG | XPR_MEM)) {
exp1 = op1minus(exp1); // convert -(A+B) etc.
goto RETURNEXP1;
}
exp2 = exp1; // convert -A to 0-A
exp1.tokens = 0;
exp1.etype = XPR_INT;
exp1.value.i = 0;
tokid = '-';
break; // continue in dyadic operators with 0-exp2
case '!':
exp1.tokens++;
if (exp1.instruction == II_COMPARE
&& (exp1.etype & XPR_REG1) && (exp1.etype & (XPR_REG2 | XPR_INT | XPR_IMMEDIATE))) {
// compare instruction. invert condition
exp1.optionbits ^= 1;
exp1.etype |= XPR_OPTIONS;
if ((exp1.reg1 & REG_V) && (dataType & TYP_FLOAT)) exp1.optionbits ^= 8; // floating point compare. invert gives unordered
goto RETURNEXP1;
}
if (exp1.instruction == II_AND
&& (exp1.etype & XPR_REG1) && (exp1.etype & XPR_INT)) {
// test_bit/test_bits_or/jump instruction. invert condition
exp1.optionbits ^= 4;
exp1.etype |= XPR_OPTIONS;
goto RETURNEXP1;
}
if (exp1.instruction == II_TEST_BITS_AND && (exp1.etype & XPR_REG1) && (exp1.etype & XPR_INT)) {
// test_bits_and/jump instruction. invert condition
exp1.optionbits ^= 1;
exp1.etype |= XPR_OPTIONS;
goto RETURNEXP1;
}
if (exp1.etype & (XPR_MEM | XPR_REG)) { // '!' ambiguous on register and memory operands
errors.report(tokens[toklow].pos, tokens[toklow].stringLength, ERR_NOT_OP_AMBIGUOUS);
}
exp2.tokens = 0;
exp2.etype = XPR_INT;
exp2.value.i = 0;
tokid = '='+D2;
break; // continue in dyadic operators with exp1 == 0
case '~':
exp2.tokens = 0;
exp2.etype = XPR_INT;
exp2.value.i = -1;
tokid = '^';
break; // continue in dyadic operators with exp1 ^ -1
default:
errors.report(tokens[tok1]); // ++ and -- not supported in expression
return exp1;
}
goto DYADIC; // proceed to dyadic operator
}
else { // postfix ++ and --
errors.report(tokens[tok1+1]); // ++ and -- not supported in expression
}
goto RETURNEXP1;
case 14: // triadic operator ?:
// evaluate exp1 ? exp2 : exp3 for all expression types
return op3(tok1, toklow, tokcolon, maxtok, options);
default:; // continue below for dyadic operator
}
// dyadic operator. evaluate two subexpressions
exp1 = expression(tok1, toklow - tok1, options); // evaluate fist expression
if (exp1.tokens != toklow - tok1) errors.report(tokens[tok1 + exp1.tokens]);
if (lineError) return exp1;
exp2 = expression(toklow + 1, tok1 + maxtok - (toklow + 1), options); // evaluate second expression
ntok = toklow - tok1 + 1 + exp2.tokens;
tokid = tokens[toklow].id; // operator id
if (lineError) return exp1;
DYADIC:
exp1 = op2(tokid, exp1, exp2);
RETURNEXP1:
if (lineError) return exp1;
if (exp1.etype & XPR_ERROR) {
errors.report(tokens[toklow].pos, tokens[toklow].stringLength, exp1.value.w);
}
return exp1;
}
// Interpret dyadic expression with any type of operands
SExpression CAssembler::op2(uint32_t op, SExpression & exp1, SExpression & exp2) {
if ((exp1.etype | exp2.etype) & XPR_UNRESOLV) {
exp1.etype = XPR_UNRESOLV; // unresolved operand. make unresolved result
exp1.tokens += exp2.tokens + 1;
}
else if ((exp1.etype & exp2.etype & XPR_MEM)
//&& ((exp1.etype|exp2.etype) & (XPR_BASE|XPR_INDEX|XPR_OPTION|XPR_SYM1|XPR_SYM2|XPR_LIMIT|XPR_LENGTH|XPR_BROADC))
) {
exp1 = op2Memory(op, exp1, exp2); // generation of memory operand. both operands inside [] and contain not only constants
}
else if (exp1.etype == XPR_OPTION && op == '=') {
// option = value is handled by op2Memory
exp1 = op2Memory(op, exp1, exp2);
}
else if (exp1.etype & exp2.etype & XPR_SYM1) {
// adding or subtracting symbols and integers
exp1 = op2Memory(op, exp1, exp2);
}
else if ((exp1.etype & XPR_SYM2) && (exp2.etype & XPR_INT)) {
// (sym1-sym2)/const
exp1 = op2Memory(op, exp1, exp2);
}
// generation of instruction involving registers and/or memory operand:
// (don't rely on XPR_MEM flag here because we would catch expressions involving constants only inside [] )
//!else if ((exp1.etype | exp2.etype) & (XPR_REG | XPR_BASE /*| XPR_SYM1*/)) {
// else if ((exp1.etype | exp2.etype) & (XPR_REG | XPR_BASE | XPR_SYM1)) { //??
else if (((exp1.etype | exp2.etype) & (XPR_REG | XPR_BASE)) || (exp1.sym1 | exp2.sym1)) { //??
exp1 = op2Registers(op, exp1, exp2);
}
else if ((exp1.etype | exp2.etype) & XPR_STRING) {
exp1 = op2String(op, exp1, exp2); // string operation
}
else if ((exp1.etype & 0xF) == XPR_FLT || (exp2.etype & 0xF) == XPR_FLT) {
// dyadic operators for float // floating point operation
exp1 = op2Float(op, exp1, exp2);
}
else if ((exp1.etype & 0xF) == XPR_INT && (exp2.etype & 0xF) == XPR_INT) {
// dyadic operators for integers // integer operation
exp1 = op2Int(op, exp1, exp2);
}
else {
// other types
exp1.etype = XPR_ERROR;
exp1.value.u = ERR_WRONG_TYPE;
}
return exp1;
}
// Interpret dyadic expression with integer operands
SExpression CAssembler::op2Int(uint32_t op, SExpression const & exp1, SExpression const & exp2) {
SExpression expr = exp1;
expr.tokens = exp1.tokens + exp2.tokens + 1;
switch (op & ~OP_UNS) {
case '+':
expr.value.u = exp1.value.u + exp2.value.u;
break;
case '-':
expr.value.u = exp1.value.u - exp2.value.u;
break;
case '*':
expr.value.i = exp1.value.i * exp2.value.i;
break;
case '/':
if (exp2.value.i == 0) {
expr.etype |= XPR_ERROR;
expr.value.u = ERR_OVERFLOW;
break;
}
if (op & OP_UNS) expr.value.u = exp1.value.u / exp2.value.u; // unsigned division
else expr.value.i = exp1.value.i / exp2.value.i; // signed division
break;
case '%':
if (exp2.value.i == 0) {
expr.etype |= XPR_ERROR;
expr.value.u = ERR_OVERFLOW;
break;
}
if (op & OP_UNS) expr.value.u = exp1.value.u % exp2.value.u; // unsigned modulo
else expr.value.i = exp1.value.i % exp2.value.i; // signed modulo
break;
case '<' + D2: // <<
expr.value.u = exp1.value.u << exp2.value.u;
break;
case '>' + D2: // >> shift right signed
if (op & OP_UNS) expr.value.u = exp1.value.u >> exp2.value.u; // unsigned shift right
else expr.value.i = exp1.value.i >> exp2.value.i; // signed shift right
break;
case '>' + D3: // >>> unsigned shift right
expr.value.u = exp1.value.u >> exp2.value.u;
break;
case '<': // < compare
if (op & OP_UNS) expr.value.i = exp1.value.u < exp2.value.u; // unsigned compare
else expr.value.i = exp1.value.i < exp2.value.i; // signed compare
break;
case '<' + EQ: // <= compare
if (op & OP_UNS) expr.value.i = exp1.value.u <= exp2.value.u; // unsigned compare
else expr.value.i = exp1.value.i <= exp2.value.i; // signed compare
break;
case '>': // > compare
if (op & OP_UNS) expr.value.i = exp1.value.u > exp2.value.u; // unsigned compare
else expr.value.i = exp1.value.i > exp2.value.i; // signed compare
break;
case '>' + EQ: // >= compare
if (op & OP_UNS) expr.value.i = exp1.value.u >= exp2.value.u; // unsigned compare
else expr.value.i = exp1.value.i >= exp2.value.i; // signed compare
break;
case '=' + D2: // ==
expr.value.u = exp1.value.u == exp2.value.u;
break;
case '!' + EQ: // !=
expr.value.u = exp1.value.u != exp2.value.u;
break;
case '&': // bitwise and
expr.value.u = exp1.value.u & exp2.value.u;
break;
case '|': // bitwise or
expr.value.u = exp1.value.u | exp2.value.u;
break;
case '^': // bitwise xor
expr.value.u = exp1.value.u ^ exp2.value.u;
break;
case '&' + D2: // logical and
expr.value.u = exp1.value.u && exp2.value.u;
break;
case '|' + D2: // logical or
expr.value.u = exp1.value.u || exp2.value.u;
break;
case '^' + D2: // logical xor
expr.value.u = (exp1.value.u != 0) ^ (exp2.value.u != 0);
break;
default: // unsupported operator
expr.etype |= XPR_ERROR;
expr.value.u = ERR_WRONG_TYPE;
}
return expr;
}
// Interpret dyadic expression with floating point operands
SExpression CAssembler::op2Float(uint32_t op, SExpression & exp1, SExpression & exp2) {
SExpression expr = exp1;
expr.tokens = exp1.tokens + exp2.tokens + 1;
if (exp1.etype == XPR_INT) { // convert exp1 to float
exp1.value.d = (double)exp1.value.i;
expr.etype = XPR_FLT;
}
if (exp2.etype == XPR_INT) { // convert exp2 to float
exp2.value.d = (double)exp2.value.i;
expr.etype = XPR_FLT;
}
// dyadic operator on float
switch (op) {
case '+':
expr.value.d = exp1.value.d + exp2.value.d;
break;
case '-':
expr.value.d = exp1.value.d - exp2.value.d;
break;
case '*':
expr.value.d = exp1.value.d * exp2.value.d;
break;
case '/':
if (exp2.value.d == 0.) {
expr.etype |= XPR_ERROR;
expr.value.u = ERR_OVERFLOW;
break;
}
expr.value.d = exp1.value.d / exp2.value.d;
break;
case '<': // signed compare
expr.value.i = exp1.value.d < exp2.value.d;
expr.etype = XPR_INT;
break;
case '<' + EQ: // <= signed compare
expr.value.i = exp1.value.d <= exp2.value.d;
expr.etype = XPR_INT;
break;
case '>': // signed compare
expr.value.i = exp1.value.d > exp2.value.d;
expr.etype = XPR_INT;
break;
case '>' + EQ: // >= signed compare
expr.value.i = exp1.value.d <= exp2.value.d;
expr.etype = XPR_INT;
break;
case '=' + D2: // ==
expr.value.i = exp1.value.d == exp2.value.d;
expr.etype = XPR_INT;
break;
case '!' + EQ: // !=
expr.value.i = exp1.value.d != exp2.value.d;
expr.etype = XPR_INT;
break;
case '&' + D2: // logical and
expr.value.i = exp1.value.d != 0. && exp2.value.d != 0.;
expr.etype = XPR_INT; break;
break;
case '|' + D2: // logical or
expr.value.i = exp1.value.d != 0. || exp2.value.d != 0.;
expr.etype = XPR_INT; break;
break;
default: // unsupported operator
expr.etype |= XPR_ERROR;
expr.value.u = ERR_WRONG_TYPE;
}
return expr;
}
// Interpret dyadic expression with register or memory operands, generating instruction
SExpression CAssembler::op2Registers(uint32_t op, SExpression const & ex1, SExpression const & ex2) {
SExpression expr = {{0}}; // return expression
uint8_t swapped = false; // operands are swapped
uint8_t cannotSwap = false; // cannot swap operands because both contain vector registers
uint32_t i; // loop counter
// make array of the two expressions
SExpression exp12[2]; // copy of expressions
uint32_t numtokens = ex1.tokens + ex2.tokens + 1; // number of tokens
expr.tokens = numtokens;
// resolve nested expressions
if ((ex1.etype | ex2.etype) & XPR_OP) {
/*
if (op == '&' && (ex1.etype & XPR_REG) && !(ex1.etype & XPR_OP) && ex2.instruction == II_XOR && ((ex2.etype & 0xF) == XPR_INT) && ex2.value.i == -1) {
// A & (B ^ -1) = and_not(A,B). This instruction is removed
expr = ex1; expr.tokens = numtokens;
expr.etype |= XPR_OP;
expr.instruction = II_AND_NOT;
expr.reg2 = ex2.reg1;
return expr;
} */
// simplify both expressions if possible
exp12[0] = ex1; exp12[1] = ex2;
for (i = 0; i < 2; i++) {
if ((exp12[i].etype & (XPR_REG | XPR_MEM)) && (exp12[i].etype & XPR_IMMEDIATE) && exp12[i].value.i == 0) {
if (exp12[i].instruction == II_SUB_REV) {
// expression is -A converted to (0-A). change to register and sign bit
exp12[i].etype &= ~(XPR_OPTIONS | XPR_IMMEDIATE | XPR_OP);
exp12[i].instruction = 0;
exp12[i].optionbits = 1;
}
else if (exp12[i].instruction == II_MUL_ADD && exp12[i].value.i == 0) {
// expression is -A*B converted to (-A*B+0). change to A*B and sign bit
exp12[i].instruction = II_MUL;
exp12[i].optionbits = exp12[i].optionbits & 1;
exp12[i].etype &= ~(XPR_OPTIONS | XPR_IMMEDIATE);
}
else if (exp12[i].instruction == II_ADD_ADD && (exp12[i].etype & (XPR_INT | XPR_FLT)) && (exp12[i].optionbits & 3) == 3 && exp12[i].value.i == 0) {
// expression is -(A+B) converted to (-A-B+0). change to A+B and sign bit
exp12[i].etype &= ~(XPR_INT | XPR_FLT);
exp12[i].instruction = II_ADD;
exp12[i].optionbits ^= 3;
exp12[i].etype &= ~(XPR_OPTIONS | XPR_IMMEDIATE);
}
}
else if (exp12[i].instruction == II_SUB_REV) {
// change -A+B to -(A-B)
exp12[i].instruction = II_SUB;
exp12[i].optionbits ^= 3;
}
}
if ((exp12[0].etype & XPR_IMMEDIATE) && (exp12[1].etype & XPR_IMMEDIATE)) {
// both operands contain an immediate. combine the immediates if possible
bool isfloat[2]; // check if operands are float
for (i = 0; i < 2; i++) isfloat[i] = (exp12[i].etype & XPR_IMMEDIATE) == XPR_FLT;
// convert integer to float if the other operand is float
for (i = 0; i < 2; i++) {
if (isfloat[1-i] && !isfloat[i]) {
exp12[i].value.d = (double)exp12[i].value.i;
isfloat[i] = true;
}
}
if (op == '+' || op == '-') { // add or subtract operands and store in exp12[1]
uint8_t sign = 0;
switch (exp12[0].instruction) {
case II_ADD: case II_SUB_REV:
sign = exp12[0].optionbits >> 1 & 1;
if (op == '-') sign ^= 1;
break;
case II_SUB:
sign = (exp12[0].optionbits >> 1 & 1) ^ 1;
if (op == '-') sign ^= 1;
break;
case II_ADD_ADD:
sign = exp12[0].optionbits >> 2 & 1;
if (op == '-') sign ^= 1;
break;
default: // no other instructions can be combined with + or -
expr.etype |= XPR_ERROR; expr.value.u = ERR_WRONG_OPERANDS;
return expr;
}
if (exp12[1].instruction == II_SUB) sign ^= 1;
// add immediates and store them in exp12[1]
if (sign) {
if (isfloat[1]) exp12[1].value.d -= exp12[0].value.d;
else exp12[1].value.i -= exp12[0].value.i;
}
else {
if (isfloat[1]) exp12[1].value.d += exp12[0].value.d;
else exp12[1].value.i += exp12[0].value.i;
}
exp12[0].value.i = 0;
exp12[0].etype &= ~ (XPR_INT | XPR_FLT);
if (exp12[0].instruction == II_ADD_ADD) {
exp12[0].instruction = II_ADD;
exp12[0].optionbits &= ~ 4;
}
else {
exp12[0].instruction = 0;
}
}
else if (op == '*' && exp12[0].instruction == II_MUL) {
if (isfloat[0]) {
exp12[1].value.d *= exp12[0].value.d;
}
else {
exp12[1].value.i *= exp12[0].value.i;
}
exp12[0].value.i = 0;
exp12[0].etype &= ~ (XPR_INT | XPR_FLT | XPR_OP);
exp12[0].instruction = 0;
} /*
else if (op == '&' && exp12[0].instruction == II_AND && !isfloat[0]) {
exp12[1].value.i &= exp12[0].value.i;
exp12[0].value.i = 0;
exp12[0].etype &= ~ XPR_INT;
exp12[0].instruction = 0;
}
else if (op == '|' && exp12[0].instruction == II_OR && !isfloat[0]) {
exp12[1].value.i |= exp12[0].value.i;
exp12[0].value.i = 0;
exp12[0].etype &= ~ XPR_INT;
exp12[0].instruction = 0;
}
else if (op == '^' && exp12[0].instruction == II_XOR && !isfloat[0]) {
exp12[1].value.i ^= exp12[0].value.i;
exp12[0].value.i = 0;
exp12[0].etype &= ~ XPR_INT;
exp12[0].instruction = 0;
} */
else {
expr.etype |= XPR_ERROR; expr.value.u = ERR_WRONG_OPERANDS;
}
}
// error if two memory operands
uint32_t etyp0 = exp12[0].etype, etyp1 = exp12[1].etype;
//if ((etyp0 & etyp1 & XPR_MEM) || (exp12[0].value.i && exp12[1].value.i)) {
if (etyp0 & etyp1 & XPR_MEM) {
expr.etype |= XPR_ERROR; expr.value.u = ERR_WRONG_OPERANDS;
return expr;
}
// error if too many operands
if (((etyp0 & XPR_REG1) != 0) + ((etyp0 & XPR_REG2) != 0) + ((etyp0 & XPR_REG3) != 0)
+ ((etyp1 & XPR_REG1) != 0) + ((etyp1 & XPR_REG2) != 0) + ((etyp1 & XPR_REG3) != 0)
+ (((etyp0 | etyp1) & XPR_MEM) != 0) + (((etyp0 | etyp1) & XPR_IMMEDIATE) != 0) > 3) {
expr.etype |= XPR_ERROR; expr.value.u = ERR_TOO_MANY_OPERANDS;
return expr;
}
// check which operations can swap
if (op != '+' && op != '*' && op != '&' && op != '|' && op != '^' && op != '-') {
cannotSwap = true; // operation is not commutative ('-' is handled with sign bits)
}
// put operands in this order: register, memory, immediate
if ((exp12[0].etype & (XPR_IMMEDIATE | XPR_MEM)) && !(exp12[1].etype & XPR_IMMEDIATE) && !cannotSwap) {
// first operand is immediate or memory, and second operant is not immediate
// swap operands if not two vector registers
if (exp12[0].reg1 & exp12[1].reg1 & REG_V) {
// both operands contain a vector register. cannot swap. make error message later if swapping required
cannotSwap = true;
}
else if ((exp12[1].etype & XPR_MEM) && op == '*') {
// second operand also contains memory
cannotSwap = true;
}
else { // swap operands to get immediate or memory operand last
expr = exp12[0]; exp12[0] = exp12[1]; exp12[1] = expr;
if (op == '-') {
op = '+'; // convert '-' to '+' and flip sign bit to make operation commutative
exp12[0].optionbits ^= 1;
}
swapped = true;
}
}
if (op == '+' || op == '-') {
/* done above:
if (exp12[0].etype & (XPR_IMMEDIATE | XPR_MEM) && exp12[1].instruction == II_MUL && !(exp12[1].etype & (XPR_INT | XPR_FLT))) {
// (memory or constant) + reg*reg. swap operands
expr = exp12[0]; exp12[0] = exp12[1]; exp12[1] = expr;
if (op == '-') {
exp12[0].optionbits ^= 1; // invert signs in both operands
exp12[1].optionbits ^= 1;
}
} */
if (!((exp12[0].etype | exp12[1].etype) & XPR_OP)) {
// +/-R1 +/-R2
if (op == '-') exp12[1].optionbits ^= 1; // sign of second operand
// change sign of constant if this simplifies it
if ((exp12[1].etype & XPR_INT) && (exp12[1].optionbits & 1)) {
exp12[1].value.i = -exp12[1].value.i;
exp12[1].optionbits = 0;
}
else if ((exp12[1].etype & XPR_FLT) && (exp12[1].optionbits & 1)) {
exp12[1].value.d = -exp12[1].value.d;
exp12[1].optionbits = 0;
}
uint8_t s = exp12[0].optionbits | exp12[1].optionbits << 1; // combine signs
expr = exp12[1]; expr.tokens = numtokens;
expr.reg1 = exp12[0].reg1;
if (exp12[1].etype & XPR_REG1) {
expr.reg2 = exp12[1].reg1; expr.etype |= XPR_REG2;
}
expr.etype |= XPR_OP | XPR_REG1;
expr.optionbits = 0;
switch (s) {
case 0: // R1 + R2
expr.instruction = II_ADD; break;
case 1: // -R1 + R2
expr.instruction = II_SUB_REV; break;
case 2: // R1 - R2
expr.instruction = II_SUB; break;
case 3: // -R1 -R2
expr.instruction = II_ADD_ADD;
expr.value.i = 0;
expr.optionbits = s;
expr.etype |= XPR_INT | XPR_OPTIONS;
break;
}
return expr;
}
else if (exp12[0].instruction == II_MUL || exp12[1].instruction == II_MUL) {
// (A*B)+C
if (op == '-') exp12[1].optionbits ^= 1; // change sign if '-'
if (exp12[1].instruction == II_MUL) { // swap expressions if A+(B*C)
if (exp12[0].reg1 & REG_V) {
expr.etype |= XPR_ERROR;
expr.value.w = ERR_CANNOT_SWAP_VECT; // cannot put vector addend as first operand
return expr;
}
expr = exp12[0]; exp12[0] = exp12[1]; exp12[1] = expr; // swap expressions
}
expr = exp12[0] | exp12[1]; // combine expressions
expr.tokens = numtokens;
if ((exp12[0].etype & exp12[1].etype & (XPR_MEM|XPR_IMMEDIATE)) || // two memory or two immediate operands
((exp12[0].etype & (XPR_MEM|XPR_IMMEDIATE)) == (XPR_MEM|XPR_IMMEDIATE))) { // exp12[0] has both memory and immediate
expr.etype |= XPR_ERROR;
expr.value.w = ERR_TOO_COMPLEX;
return expr;
}
expr.instruction = II_MUL_ADD;
expr.etype |= XPR_OPTIONS;
if (((exp12[0].etype & XPR_MEM) && !(exp12[1].etype & XPR_IMMEDIATE)) || (exp12[0].etype & XPR_IMMEDIATE)) {
expr.instruction = II_MUL_ADD2; // get A*C+B
// we don't need to do anything with signs here because the sign options apply to product and addend, not to specific operands
}
expr.etype |= XPR_OP;
expr.reg1 = exp12[0].reg1; expr.reg2 = exp12[0].reg2;
if (exp12[1].etype & XPR_REG) { // C has a register
if (exp12[0].etype & XPR_REG2) { // 3 registers
expr.reg3 = exp12[1].reg1;
expr.etype |= XPR_REG3;
}
else {
expr.reg2 = exp12[1].reg1; // 2 registers
expr.etype |= XPR_REG2;
}
}
// optionbits 0-1 = sign of product. optionbits 2-3 = sign of addend.
expr.optionbits = 3 * (exp12[0].optionbits & 1) | 0xC * (exp12[1].optionbits & 1);
expr.etype |= XPR_OPTIONS;
return expr;
}
else if (exp12[0].instruction == II_ADD || exp12[0].instruction == II_SUB) {
// (A+B)+C
expr = exp12[0] | exp12[1]; // combine expressions
expr.tokens = numtokens;
expr.reg1 = exp12[0].reg1;
expr.etype |= XPR_OP;
expr.instruction = II_ADD_ADD;
if ((exp12[0].etype & XPR_IMMEDIATE) || ((exp12[0].etype & XPR_MEM) && !(exp12[1].etype & XPR_IMMEDIATE))) {
// does not fit
expr.etype |= XPR_ERROR;
expr.value.w = cannotSwap ? ERR_CANNOT_SWAP_VECT : ERR_TOO_COMPLEX;
return expr;
}
if (exp12[1].etype & XPR_REG) { // C has a register
if (exp12[0].etype & XPR_REG2) { // 3 registers
expr.reg3 = exp12[1].reg1;
expr.etype |= XPR_REG3;
}
else if (exp12[0].etype & XPR_REG1) { // 2 registers
expr.reg2 = exp12[1].reg1;
expr.etype |= XPR_REG2;
}
else {
expr.reg1 = exp12[1].reg1; // 1 registers
expr.etype |= XPR_REG1;
}
}
expr.optionbits = (exp12[0].optionbits & 3) | ((exp12[1].optionbits & 1) ^ (op == '-')) << 2;
if (exp12[0].instruction == II_SUB) expr.optionbits ^= 2;
if (swapped && op == '-') expr.optionbits ^= 7;
expr.etype |= XPR_OPTIONS;
return expr;
}
else if (exp12[1].instruction == II_ADD || exp12[1].instruction == II_SUB) {
// A+(B+C)
expr = exp12[0] | exp12[1]; // combine expressions
expr.tokens = numtokens;
expr.reg1 = exp12[0].reg1;
expr.etype |= XPR_OP;
expr.instruction = II_ADD_ADD;
if (exp12[0].etype & exp12[1].etype & (XPR_IMMEDIATE | XPR_MEM)) {
// does not fit
expr.etype |= XPR_ERROR;
expr.value.w = ERR_TOO_COMPLEX;
return expr;
}