-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathassem4.cpp
More file actions
2191 lines (2059 loc) · 107 KB
/
assem4.cpp
File metadata and controls
2191 lines (2059 loc) · 107 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
/**************************** assem4.cpp ********************************
* Author: Agner Fog
* Date created: 2017-04-17
* Last modified: 2024-08-02
* Version: 1.13
* Project: Binary tools for ForwardCom instruction set
* Module: assem.cpp
* Description:
* Module for assembling ForwardCom .as files.
* This module contains:
* pass3(): Interpretation of code lines.
* Copyright 2017-2024 GNU General Public License http://www.gnu.org/licenses
******************************************************************************/
#include "stdafx.h"
// Interpret lines. Generate code and data
void CAssembler::pass3() {
uint16_t last_line_type = 0; // type of preceding line
makeFormatLists(); // make formatList3 and formatList4
code_size = cmd.codeSizeOption; // initialize options
data_size = cmd.dataSizeOption;
section = 0;
iLoop = iIf = iSwitch = 0; // index of current high level statements
// lines loop
for (linei = 1; linei < lines.numEntries()-1; linei++) {
tokenB = lines[linei].firstToken; // first token in line
tokenN = lines[linei].numTokens; // number of tokens in line
if (tokenN == 0 || lines[linei].type == LINE_ERROR || lines[linei].type == LINE_METADEF) continue;
lineError = false;
switch (lines[linei].type) {
case LINE_DATADEF:
if (last_line_type == LINE_CODEDEF && (lines[linei].sectionType & SHF_EXEC)) {
/* currently, the assembler cannot mix code and data because they are put in different buffers.
The only way to hard-code instructions is to put them into a separate section. */
errors.reportLine(ERR_MIX_DATA_AND_CODE); // data definition in code section
}
break;
case LINE_CODEDEF:
interpretCodeLine();
if (last_line_type == LINE_DATADEF && !(lines[linei].sectionType & SHF_EXEC)) {
errors.reportLine(ERR_MIX_DATA_AND_CODE); // code definition in data section
}
break;
case LINE_METADEF: case LINE_ERROR:
continue;
case LINE_FUNCTION:
interpretFunctionDirective();
break;
case LINE_SECTION:
interpretSectionDirective();
break;
case LINE_ENDDIR:
interpretEndDirective();
break;
case LINE_OPTIONS:
interpretOptionsLine();
break;
}
last_line_type = lines[linei].type;
}
while (hllBlocks.numEntries()) {
// unfinished block
SBlock block = hllBlocks.pop();
errors.report(tokens[block.startBracket].pos, tokens[block.startBracket].stringLength, ERR_BRACKET_BEGIN);
}
}
// extract subsets of formatList (in disasm1.cpp) for multiformat instructions and jump instructions
void CAssembler::makeFormatLists() {
uint32_t i;
for (i = 0; i < formatListSize; i++) {
if (formatList[i].category == 3) formatList3.push(formatList[i]);
if (formatList[i].category == 4) formatList4.push(formatList[i]);
}
}
// Interpret a line defining code. This covers both assembly style and high level style code
void CAssembler::interpretCodeLine() {
uint32_t tok; // token index
dataType = 0; // data type for current instruction
uint32_t nReg = 0; // number of register source operands
uint32_t state = 0; /* state during interpretation of line. example:
L1: int32 r1 = compare(r2, 5), option = 2 // assembly style
L1: int32 r1 = r2 < 5 // same in high level style
0: begin
1: after label
2: after label:
3: after type
4: after destination
5: after destination = (expecting expression or instruction)
6: after expression or instruction()
7: after instruction
8: after instruction(
9: after operand
10: after instruction(),
11: after jump instruction
*/
SExpression expr; // evaluated expression
SCode code; // current instruction code
zeroAllMembers(code); // reset code structure
if (section == 0) {
errors.reportLine(ERR_CODE_WO_SECTION);
}
// high level instructions with nothing before can be caught already here
if (tokens[tokenB].type == TOK_HLL) {
interpretHighLevelStatement(); // if, else, switch, for, do, while (){} statements
return;
}
if (tokens[tokenB].type == TOK_OPR && tokens[tokenB].id == '}') {
interpretEndBracket(); // end of {} block
return;
}
// interpret line by state machine looping through tokens
for (tok = tokenB; tok < tokenB + tokenN; tok++) {
SToken token = tokens[tok];
if (token.type == TOK_XPR && expressions[token.value.w].etype & XPR_REG) {
// this is an alias for a register. Translate to register
token.type = TOK_REG;
token.id = expressions[token.value.w].reg1;
}
if (lineError) break;
code.section = section;
if (state == 5) { // after '='
if (token.type == TOK_INS) { // instruction
if (code.instruction) errors.report(token); // instruction after += etc.
code.instruction = token.id;
state = 7;
}
else { // expression after equal sign
// interpret expression representing operands and operator
expr = expression(tok, tokenB + tokenN - tok, 0);
if (lineError) return;
if (code.instruction) {
// += operator etc. already encountered. combine the operands
uint32_t op = code.instruction; code.instruction = 0;
code.reg1 = code.dest; // first source operand is same as destination
code.etype |= XPR_REG1; code.tokens = 0;
expr = op2(op, code, expr); // operation '+' for '+=', etc.
code.instruction = 0; code.reg1 = 0;
}
if (code.etype & XPR_ERROR) {
errors.reportLine(code.value.w); // report error
}
// ordinary '=' goes here
if (lineError) return;
insertAll(code, expr);
tok += expr.tokens - 1;
state = 6;
}
}
else if (state == 11) {
// interpret jump target
expr = expression(tok, tokenB + tokenN - tok, 0);
state = 6;
if (expr.etype & XPR_REG) {
code = code | expr;
tok += expr.tokens - 1;
}
else if (expr.etype & (XPR_INT | XPR_SYM1)) {
code.sym5 = expr.sym3 ? expr.sym3 : expr.sym1;
code.offset_jump = expr.value.w;
if (expr.value.w & 3) errors.report(token.pos, token.stringLength, ERR_JUMP_TARGET_MISALIGN);
tok += expr.tokens - 1;
code.etype |= XPR_JUMPOS | (expr.etype & ~XPR_IMMEDIATE);
}
else {
errors.report(token.pos, token.stringLength, ERR_EXPECT_JUMP_TARGET);
break;
}
}
else if (state == 8 && token.type != TOK_OPT && token.type != TOK_REG) {
// expression in parameter list
if (token.type == TOK_OPR && token.id == ')') {
state = 6; break; // end of parameter list
}
// interpret any expression, except register or option
expr = expression(tok, tokenB + tokenN - tok, 0);
tok += expr.tokens - 1;
if (code.etype & expr.etype & XPR_INT) {
// multiple immediate integer constants
if (code.etype & XPR_INT2) {
// three integer operands
if (code.etype & XPR_OPTIONS) errors.report(token.pos, token.stringLength, ERR_TOO_MANY_OPERANDS);
code.optionbits = uint8_t(expr.value.w);
code.etype |= XPR_OPTIONS;
expr.value.u = 0;
}
else {
// two integer operands
if (code.value.u >> 32 != 0) errors.report(token.pos, token.stringLength, ERR_TOO_MANY_OPERANDS);
code.value.u = code.value.w | expr.value.u << 32;
code.etype |= XPR_INT2;
expr.value.u = 0;
}
}
else if (expr.etype & XPR_MEM) {
if (expr.etype & XPR_OFFSET) code.offset_mem += expr.offset_mem;
//else code.offset += expr.value.i;
if (expr.etype & XPR_IMMEDIATE) { // both memory and immediate operands
code.value.i = expr.value.i;
}
}
else if (expr.etype & XPR_IMMEDIATE) {
code.value.i = expr.value.i;
}
expr.value.i = 0;
code = code | expr;
state = 9;
}
else {
switch (token.type) {
case TOK_LAB: case TOK_SYM:
if (state == 0) {
//code.label = token.value.w;
code.label = token.id;
if (code.label) {
int32_t symi = findSymbol(code.label);
if (symi > 0) symbols[symi].st_section = section;
}
state = 1;
}
else goto ST_ERROR;
break;
case TOK_OPR:
if (token.id == ':' && state == 1) {
state = 2;
}
else if (token.id == '+' && state == 3) {
code.dtype |= TYP_PLUS;
}
else if (token.priority == 15 && state == 4) {
// assignment operator
state = 5;
if (token.id & EQ) { // combined operator and assignment: += -= *= etc.
code.reg1 = code.dest;
code.etype |= XPR_REG | XPR_REG1;
code.instruction = token.id & ~EQ; // temporarily store operator in .instruction
}
else if (token.id != '=') errors.report(token);
}
else if (token.id == '=' && state == 11) {
state = 12;
}
else if (token.id == ',' && state == 6) {
state = 10;
}
else if (token.id == ',' && state == 9) {
state = 8;
}
else if (token.id == '(' && state == 7) {
state = 8;
}
else if (token.id == ')' && (state == 8 || state == 9)) {
state = 6;
}
else if (token.id == '[' && (state == 0 || state == 2 || state == 3)) {
// interpret memory destination
expr = expression(tok, tokenB + tokenN - tok, 0);
tok += expr.tokens - 1;
insertMem(code, expr);
code.dest = 2;
state = 4;
}
else if (token.id == '[' && state == 7 && code.instruction == II_ADDRESS) {
// address []. expect memory operand
expr = expression(tok, tokenB + tokenN - tok, 0);
tok += expr.tokens - 1;
insertMem(code, expr);
state = 6;
}
else if ((token.id == '+' + D2 || token.id == '-' + D2) && (state == 3 || state == 4)) {
// ++ and -- operators
code.instruction = (token.id == '+' + D2) ? II_ADD : II_SUB;
// operand is 1, integer or float
if (dataType & TYP_FLOAT) {
code.value.d = 1.0;
code.etype |= XPR_FLT;
}
else {
code.value.i = 1;
code.etype |= XPR_INT;
}
if (state == 3) { // prefix operator. expect register
tok++;
if (token.type != TOK_REG) errors.report(token);
code.dest = token.id;
}
code.reg1 = code.dest;
code.etype |= XPR_REG1;
state = 6;
}
else if (token.id == ';') {} // ignore terminating ';'
else goto ST_ERROR;
break;
case TOK_TYP:
if (state == 0 || state == 2) {
dataType = code.dtype = token.id;
state = 3;
}
else goto ST_ERROR;
break;
case TOK_REG:
if (state == 0 || state == 2 || state == 3) {
code.dest = uint8_t(token.id);
state = 4;
}
else if (state == 8) {
if (nReg < 3) {
(&code.reg1)[nReg] = (uint8_t)token.id; // insert register in expression
code.etype |= XPR_REG1 << nReg++;
if ((code.etype & (XPR_INT | XPR_FLT | XPR_MEM)) && code.dest != 2) errors.report(token.pos, token.stringLength, ERR_OPERANDS_WRONG_ORDER);
}
else errors.report(token.pos, token.stringLength, ERR_TOO_MANY_OPERANDS);
state = 9;
}
else goto ST_ERROR;
break;
case TOK_XPR:
if (token.value.u >= expressions.numEntries()) goto ST_ERROR; // expression not found
if (expressions[token.value.w].etype & XPR_MEM) { // this is an alias for a memory operand
insertMem(code, expressions[token.value.w]);
code.dest = 2;
state = 4;
}
else goto ST_ERROR;
break;
case TOK_INS:
if (state == 0 || state == 2 || state == 3) {
// interpret instruction name
code.instruction = token.id;
state = 7; // expect parenthesis and parameters
if (code.instruction & II_JUMP_INSTR) {
// Jump or call instruction. The next may be a jump target, a register or a memory operand
state = 11; // expect jump target
// Check if there is a memory operand
for (uint32_t tok2 = tok+1; tok2 < tokenB + tokenN; tok2++) {
if (tokens[tok2].type == TOK_OPR && tokens[tok2].id == '[') {
// a jump instruction with memory operand is treated as a normal instruction
state = 7; break;
}
}
}
}
else if ((state == 6 || state == 10) && (token.id & II_JUMP_INSTR)) {
// second half of jump instruction
code.instruction |= token.id; // combine two partial instruction names
state = 11; // expect jump target
}
else goto ST_ERROR;
break;
case TOK_OPT: // option keyword
expr = expression(tok, tokenB + tokenN - tok, 4); // this will read option = value
tok += expr.tokens - 1;
code.etype |= expr.etype;
if (expr.etype & XPR_LIMIT) {
code.value.i = expr.value.i;
if (expr.value.u >= 0x100000000U) { // limit too high
errors.report(tokens[tok - 1].pos, tokens[tok - 1].stringLength, ERR_LIMIT_TOO_HIGH);
}
}
if (expr.etype & (XPR_LENGTH | XPR_BROADC)) code.length = expr.length;
if (expr.etype & XPR_MASK) code.mask = expr.mask;
if (expr.etype & XPR_FALLBACK) code.fallback = expr.fallback;
if (expr.etype & XPR_OPTIONS) code.optionbits = expr.optionbits;
if (state == 8) state = 9;
else if (state == 6 || state == 10) state = 6;
else goto ST_ERROR;
break;
case TOK_ATT:
if (token.id == ATT_ALIGN && state == 0 && tokenN >= 2) {
// align n directive
code.instruction = II_ALIGN;
expr = expression(tok + 1, tokenB + tokenN - tok - 1, 0);
tok = tokenB + tokenN;
code.value.u = expr.value.u;
code.sizeUnknown = 0x80;
if ((code.value.u & (code.value.u - 1)) || code.value.u > MAX_ALIGN
|| (expr.etype & XPR_IMMEDIATE) != XPR_INT || (expr.etype & (XPR_REG|XPR_OPTION|XPR_MEM))) {
errors.reportLine(ERR_ALIGNMENT);
}
}
else goto ST_ERROR;
break;
case TOK_HLL: // high level directive: if, else, while, for, etc.
interpretHighLevelStatement();
return;
default:;
ST_ERROR:
errors.report(token);
break;
}
}
}
if (lineError) return;
// check if state machine ends with a finished instruction
if (state != 0 && state != 2 && state != 6 && state != 7) {
errors.report(tokens[tok-1].pos, tokens[tok-1].stringLength, ERR_UNFINISHED_INSTRUCTION);
return;
}
// move and store instruction has no operator yet
if (code.instruction == 0 && code.etype) {
if (code.dest == 2) code.instruction = II_STORE; // store to memory
else {
code.instruction = II_MOVE; // move constant to register
if (cmd.optiLevel && (code.etype & XPR_INT) && code.value.i >= 0 && !code.sym3 && (code.dtype & TYP_INT) && (code.dest & REG_R)) {
code.dtype |= TYP_PLUS; // optimize to larger type for positive constant because it is zero-extended anyway
}
}
}
if (code.instruction) { // a code record with no instruction represents a label only
// code record contains instruction
if (code.etype & XPR_JUMPOS) mergeJump(code);
checkCode1(code);
if (lineError) return;
// find an instruction variant that fits
fitCode(code);
if (lineError) return;
}
// save code structure
codeBuffer.push(code);
}
// Check how many bits are needed to contain immediate constant of an instruction.
// The result is returned as bit-flags in code.fitNumX.
// The return value is nonzero if the size cannot be resolved yet.
int CAssembler::fitConstant(SCode & code) {
int64_t value = 0; // the constant or address to fit
int64_t valueScaled; // value divided by scale factor
double dvalue = 0; // floating point value if needed
bool floatType = false; // a floating point type is needed
bool floatConst = false; // a floating point constant is provided
uint32_t fitNum = 0; // return value
uint32_t sym3 = 0, sym4 = 0; // symbols
int32_t isym3 = 0, isym4 = 0; // symbol index
int32_t uncertainty; // maximum deviance if the value is uncertain
int uncertain = 0; // return value
int symscale; // scaling of difference between symbols
if (code.instruction == II_ALIGN) return 0; // not an instruction
if (!(code.etype & (XPR_IMMEDIATE | XPR_SYM1))) return 0; // has no immediate
value = value0 = code.value.i; // immediate constant
floatType = uint8_t(code.dtype) >= uint8_t(TYP_FLOAT16); // floating point needed
floatConst = (code.etype & XPR_FLT) != 0; // floating point provided
if (floatType) {
if (floatConst) dvalue = code.value.d;
else {
// Note: We are converting the immediate constant to floating point here in order to find
// the optimal representation. We have not identified the instruction yet so we don't know
// if it actually needs a floating point constant or an integer. We have saved the original
// integer value in value0 so that we can undo the conversion in case an instruction with
// floating point type needs an integer operand.
dvalue = (double)value; // value as float
if ((code.etype & XPR_INT) && !(variant & VARIANT_I2)) {
// convert integer constant to float
code.value.d = dvalue;
code.etype = (code.etype & ~XPR_IMMEDIATE) | XPR_FLT;
floatConst = true;
}
}
if ((code.etype & XPR_FLT) && uint8_t(code.dtype) == uint8_t(TYP_FLOAT32)) {
union { // check for overflow in single precision float
float f;
uint32_t i;
} u;
u.f = float(code.value.d);
if (isinf_f(u.i) && u.f > code.value.d) errors.reportLine(ERR_CONSTANT_TOO_LARGE);
}
if ((code.etype & XPR_FLT) && uint8_t(code.dtype) == uint8_t(TYP_FLOAT16)) {
// check for overflow in half precision float
if (isinf_h(double2half(code.value.d) && !isinf_d(code.value.i))) errors.reportLine(ERR_CONSTANT_TOO_LARGE);
}
}
// check for symbols
if (code.sym3) {
sym3 = code.sym3; sym4 = code.sym4;
symscale = code.symscale3;
isym3 = findSymbol(sym3);
if (isym3 < 1) {
code.sizeUnknown = 2; return 2; // should not occur
}
}
if (code.sym3 && !code.sym4 && int32_t(symbols[isym3].st_section) == SECTION_LOCAL_VAR && symbols[isym3].st_type == STT_CONSTANT) {
// convert local symbol to constant
value = symbols[isym3].st_value;
code.value.i = value;
code.sym3 = 0;
if (cmd.optiLevel && value >= 0 && (code.dtype & TYP_INT) && (code.dest & REG_R)) {
code.dtype |= TYP_PLUS; // optimize to larger type for positive constant because it is zero-extended anyway
}
}
else if (sym3) {
// there is a symbol
if (symbols[isym3].st_unitsize == 0) uncertain = 2; // symbol value is not known yet
uint32_t sym3section = symbols[isym3].st_section; // symbol section
// determine necessary relocation size if relocation needed
uint64_t relSize; // maximum size of relocated address
if (symbols[isym3].st_type == STT_CONSTANT) {
relSize = 0x10000000; // there is no command line option for the size of absolute symbols. assume 32 bit
code.etype |= XPR_INT;
}
else if (sym3section && symbols[isym3].st_type != STT_CONSTANT) { // local symbol with known section
relSize = (sectionHeaders[sym3section].sh_flags & (SHF_EXEC | SHF_IP)) ? code_size : data_size;
}
else { // external symbol with unknown section. look at symbol attributes
relSize = (symbols[isym3].st_other & (STV_EXEC | STV_IP)) ? code_size : data_size;
if (!(code.etype & (XPR_MEM | XPR_SYM2))) {
errors.reportLine(ERR_CONFLICT_TYPE); // must be memory operand
}
}
if (sym4) {
// value is (sym3 - sym4) / scale factor
isym4 = findSymbol(sym4);
if (isym4 <= 0) {
code.sizeUnknown = 2; return 2; // should not occur
}
code.etype |= XPR_INT; // symbol difference gives an integer
if (symbols[isym3].st_unitsize == 0) uncertain = 2; // symbol value is not known yet
if (symbols[isym3].st_section != symbols[isym4].st_section || symbols[isym3].st_bind != STB_LOCAL || symbols[isym4].st_bind != STB_LOCAL) {
// different sections or not local. relocation needed
fitNum = IFIT_RELOC;
if (code.symscale1 > 1) relSize /= code.symscale1; // value is scaled
if (relSize <= 1 << 7) fitNum |= IFIT_I8;
if (relSize <= 1 << 15) fitNum |= IFIT_I16;
if (relSize <= (uint64_t)1 << 31) fitNum |= IFIT_I32;
code.fitNum = fitNum;
code.sizeUnknown = uncertain;
return uncertain;
}
// difference between two local symbols
if (pass < 4) {
code.fitNum = IFIT_I8 | IFIT_I16 | IFIT_I32; // symbol values are not available yet
code.sizeUnknown = 1;
return 1;
}
value += int32_t(uint32_t(symbols[isym3].st_value) - uint32_t(symbols[isym4].st_value));
if (symscale < 1) symscale = 1;
valueScaled = value / symscale + code.offset_mem;
if (valueScaled >= -(1 << 7) && valueScaled < (1 << 7)) fitNum |= IFIT_I8;
if (valueScaled >= -(1 << 15) && valueScaled < (1 << 15)) fitNum |= IFIT_I16;
if (valueScaled >= -((int64_t)1 << 31) && valueScaled < ((int64_t)1 << 31)) fitNum |= IFIT_I32;
// check if value is certain. uncertainty is stored in high part of st_value
uncertainty = (symbols[isym3].st_value >> 32) - (symbols[isym4].st_value >> 32);
valueScaled = value / symscale + code.offset_mem + uncertainty;
if (symscale > 1) valueScaled /= symscale; // value is scaled
if ((valueScaled < -(1 << 7) || valueScaled >= (1 << 7)) && (fitNum & IFIT_I8)) uncertain |= 1;
if ((valueScaled < -(1 << 15) || valueScaled >= (1 << 15)) && (fitNum & IFIT_I16)) uncertain |= 1;
if ((valueScaled < -((int64_t)1 << 31) || valueScaled >= ((int64_t)1 << 31)) && (fitNum & IFIT_I32)) uncertain |= 1;
if (uncertain && (code.fitNum & IFIT_LARGE)) {
// choose the larger version if optimization process has convergence problems
fitNum = (fitNum & (fitNum - 1)) | IFIT_I32; // remove the lowest set bit
uncertain &= ~1;
}
code.fitNum = fitNum;
code.sizeUnknown = uncertain;
return uncertain;
}
// one symbol. must be constant
if (sym3section != 0 && symbols[isym3].st_type != STT_CONSTANT && !(code.etype & XPR_MEM)) {
errors.reportLine(ERR_MEM_WO_BRACKET);
return 1;
}
if (sym3section && symbols[isym3].st_type != STT_CONSTANT && (sectionHeaders[sym3section].sh_flags & SHF_IP)) {
// relative to instruction pointer
if (sym3section != code.section || symbols[isym3].st_bind != STB_LOCAL) {
// symbol is in different section or not local. relocation needed
fitNum = IFIT_RELOC;
if (relSize <= 1 << 7) fitNum |= IFIT_I8; // necessary relocation size
if (relSize <= 1 << 15) fitNum |= IFIT_I16;
if (relSize <= (uint64_t)1 << 31) fitNum |= IFIT_I32;
code.fitNum = fitNum;
code.sizeUnknown = uncertain;
return uncertain;
}
if (pass < 4) {
code.fitNum = IFIT_I8 | IFIT_I16 | IFIT_I32; // symbol values are not available yet
code.sizeUnknown = 1;
return 1;
}
// self-relative address to local symbol
value = int32_t((uint32_t)symbols[isym3].st_value - (code.address + code.size * 4));
valueScaled = value + code.offset_mem;
if (valueScaled >= -(1 << 7) && valueScaled < (1 << 7)) fitNum |= IFIT_I8;
if (valueScaled >= -(1 << 15) && valueScaled < (1 << 15)) fitNum |= IFIT_I16;
if (valueScaled >= -((int64_t)1 << 31) && valueScaled < ((int64_t)1 << 31)) fitNum |= IFIT_I32;
code.fitNum = fitNum;
// check if value is certain. uncertainty is stored in high part of st_value and sh_link
uncertainty = int32_t((symbols[isym3].st_value >> 32) - sectionHeaders[code.section].sh_link);
valueScaled += uncertainty;
if ((valueScaled < -(1 << 7) || valueScaled >= (1 << 7)) && (fitNum & IFIT_I8)) uncertain |= 1;
if ((valueScaled < -(1 << 15) || valueScaled >= (1 << 15)) && (fitNum & IFIT_I16)) uncertain |= 1;
if ((valueScaled < -((int64_t)1 << 31) || valueScaled >= ((int64_t)1 << 31)) && (fitNum & IFIT_I32)) uncertain |= 1;
if (uncertain && (code.fitNum & IFIT_LARGE)) {
// choose the larger version if optimization process has convergence problems
fitNum = (fitNum & (fitNum - 1)) | IFIT_I32; // remove the lowest set bit
uncertain &= ~1;
}
code.fitNum = fitNum;
code.sizeUnknown = uncertain;
return uncertain;
}
// symbol is relative to data pointer or external constant. relocation needed
fitNum = IFIT_RELOC;
if (relSize <= 1 << 7) fitNum |= IFIT_I8;
if (relSize <= 1 << 15) fitNum |= IFIT_I16;
if (relSize <= (uint64_t)1 << 31) fitNum |= IFIT_I32;
code.fitNum = fitNum;
code.sizeUnknown = uncertain;
return uncertain;
}
// no symbol. only a constant
if (floatType) {
// floating point constant
code.fitNum = fitFloat(dvalue);
if (uint8_t(code.dtype) < uint8_t(TYP_FLOAT64)) code.fitNum |= FFIT_32;
code.sizeUnknown = 0;
return 0;
}
// integer constant
uint32_t low; // index of lowest set bit
uint32_t high; // index of highest set bit
fitNum = 0;
int nbits;
if (value == int64_t(0x8000000000000000)) { // prevent overflow of -value
fitNum = 0;
}
else if (value >= 0) {
low = bitScanForward((uint64_t)value); // lowest set bit
high = bitScanReverse((uint64_t)value); // highest set bit
//if (value < 8) fitNum |= IFIT_I4;
//if (value == 8) fitNum |= IFIT_J4;
//if (value < 0x10) fitNum |= IFIT_U4;
if (value < 0x80) fitNum |= IFIT_I8 | IFIT_I8SHIFT;
if (value == 0x80) fitNum |= IFIT_J8;
if (value <= 0xFF) fitNum |= IFIT_U8;
if (value < 0x8000) fitNum |= IFIT_I16 | IFIT_I16SH16;
if (value == 0x8000) fitNum |= IFIT_J16;
if (value <= 0xFFFF) fitNum |= IFIT_U16;
if (high < 31) fitNum |= IFIT_I32;
if (high < 32) fitNum |= IFIT_U32;
if (value == 0x80000000U) fitNum |= IFIT_J32;
nbits = high - low + 1;
if (nbits < 8) fitNum |= IFIT_I8SHIFT;
if (nbits < 16) {
fitNum |= IFIT_I16SHIFT;
if (low >= 16 && high < 31) fitNum |= IFIT_I16SH16;
}
if (nbits < 32) fitNum |= IFIT_I32SHIFT;
if (low >= 32) fitNum |= IFIT_I32SH32;
}
else { // x < 0
value = -value;
low = bitScanForward(value); // lowest set bit
high = bitScanReverse(value); // highest set bit
//if (value <= 8) fitNum |= IFIT_I4;
if (value <= 0x80) fitNum |= IFIT_I8 | IFIT_I8SHIFT;
if (value <= 0x8000) fitNum |= IFIT_I16 |IFIT_I16SH16 ;
if (value <= 0x80000000U) fitNum |= IFIT_I32;
nbits = high - low + 1;
if (nbits < 8) fitNum |= IFIT_I8SHIFT;
if (nbits < 16) {
fitNum |= IFIT_I16SHIFT;
if (low >= 16 && high <= 31) fitNum |= IFIT_I16SH16;
}
if (nbits < 32) fitNum |= IFIT_I32SHIFT;
if (low >= 32) fitNum |= IFIT_I32SH32;
}
code.fitNum = fitNum;
code.sizeUnknown = 0;
return 0;
}
// Check how many bits are needed to a relative address or jump offset of an instruction.
// This result is returned as bit-flags in codefitAddr, code.fitJump, and code.fitNum
// The return value is nonzero if the size cannot be resolved yet.
int CAssembler::fitAddress(SCode & code) {
int64_t value = 0; // the constant or address to fit
int64_t valueScaled; // value divided by scale factor
uint32_t fitBits = 0; // bit flags indicating fit
int32_t isym1 = 0, isym2 = 0; // symbol index
int32_t uncertainty; // maximum deviance if the value is uncertain
int uncertain = 0; // return value
if (code.instruction == II_ALIGN) return 0; // not an instruction
if (!(code.etype & (XPR_OFFSET | XPR_JUMPOS | XPR_MEM))) return 0; // has no address
// check address of memory operand
if (code.sym1) {
// there is a memory operand symbol
code.etype |= XPR_OFFSET;
value = code.offset_mem; // memory offset
isym1 = findSymbol(code.sym1);
if (isym1 <= 0) {
code.sizeUnknown = 2; return 2; // should not occur
}
if (symbols[isym1].st_unitsize == 0) uncertain = 2; // symbol value is not known yet
uint32_t sym1section = symbols[isym1].st_section; // symbol section
if (sym1section < sectionHeaders.numEntries()) {
// determine necessary relocation size if relocation needed
uint64_t relSize; // maximum size of relocated address
if (symbols[isym1].st_type == STT_CONSTANT) {
// assume that constant offset is limited by dataSizeOption
relSize = data_size; // relocation size for code and constant data
}
else if (sym1section
&& !(sectionHeaders[sym1section].sh_flags & (SHF_WRITE | SHF_DATAP | SHF_THREADP))) {
relSize = code_size; // relocation size for code and constant data
}
else if (sym1section) { // local symbol with known section
relSize = (sectionHeaders[sym1section].sh_flags & (SHF_EXEC | SHF_IP)) ? code_size : data_size;
}
else { // external symbol with unknown section. look at symbol attributes
relSize = (symbols[isym1].st_other & (STV_EXEC | STV_IP)) ? code_size : data_size;
}
if (code.sym2) {
// value is (sym1 - sym2) / scale factor
isym2 = findSymbol(code.sym2);
if (isym2 <= 0) {
code.sizeUnknown = 2; return 2; // should not occur
}
if (symbols[isym1].st_unitsize == 0) uncertain = 2; // symbol value is not known yet
if (symbols[isym1].st_section != symbols[isym2].st_section || symbols[isym1].st_bind != STB_LOCAL || symbols[isym2].st_bind != STB_LOCAL) {
// different sections or not local. relocation needed
fitBits = IFIT_RELOC;
if (code.symscale1 > 1) relSize /= code.symscale1; // value is scaled
if (relSize <= 1 << 7) fitBits |= IFIT_I8;
if (relSize <= 1 << 15) fitBits |= IFIT_I16;
//if (relSize <= 1 << 23) fitBits |= IFIT_I24;
if (relSize <= (uint64_t)1 << 31) fitBits |= IFIT_I32;
code.fitAddr = fitBits;
code.sizeUnknown += uncertain;
//return uncertain;
}
// difference between two local symbols
else if (pass < 4) {
code.fitAddr = IFIT_I8 | IFIT_I16 | IFIT_I32; // symbol values are not available yet
code.sizeUnknown += 1;
uncertain += 1;
//return 1;
}
else {
value += int32_t(uint32_t(symbols[isym1].st_value) - uint32_t(symbols[isym2].st_value));
int scale = code.symscale1;
if (scale < 1) scale = 1;
valueScaled = value / scale + code.offset_mem;
if (valueScaled >= -(1 << 7) && valueScaled < (1 << 7)) fitBits |= IFIT_I8;
if (valueScaled >= -(1 << 15) && valueScaled < (1 << 15)) fitBits |= IFIT_I16;
if (valueScaled >= -((int64_t)1 << 31) && valueScaled < ((int64_t)1 << 31)) fitBits |= IFIT_I32;
// check if value is certain. uncertainty is stored in high part of st_value
uncertainty = (symbols[isym1].st_value >> 32) - (symbols[isym2].st_value >> 32);
valueScaled = value / scale + code.offset_mem + uncertainty;
if (code.symscale1 > 1) valueScaled /= code.symscale1; // value is scaled
if ((valueScaled < -(1 << 7) || valueScaled >= (1 << 7)) && (fitBits & IFIT_I8)) uncertain |= 1;
if ((valueScaled < -(1 << 15) || valueScaled >= (1 << 15)) && (fitBits & IFIT_I16)) uncertain |= 1;
if ((valueScaled < -((int64_t)1 << 31) || valueScaled >= ((int64_t)1 << 31)) && (fitBits & IFIT_I32)) uncertain |= 1;
if (uncertain && (code.fitAddr & IFIT_LARGE)) {
// choose the larger version if optimization process has convergence problems
fitBits = (fitBits & (fitBits - 1)) | IFIT_I32; // remove the lowest set bit
uncertain &= ~1;
}
code.fitAddr = fitBits;
code.sizeUnknown += uncertain;
//return uncertain;
}
}
// one symbol
else if (sectionHeaders[sym1section].sh_flags & SHF_IP) {
// relative to instruction pointer
if (sym1section != code.section || symbols[isym1].st_bind != STB_LOCAL) {
// symbol is in different section or not local. relocation needed
fitBits = IFIT_RELOC;
if (code.etype & XPR_JUMPOS) relSize >>= 2; // value is scaled by 4
if (relSize <= 1 << 7) fitBits |= IFIT_I8; // necessary relocation size
if (relSize <= 1 << 15) fitBits |= IFIT_I16;
if (relSize <= 1 << 23) fitBits |= IFIT_I24;
if (relSize <= (uint64_t)1 << 31) fitBits |= IFIT_I32;
code.fitAddr = fitBits;
code.sizeUnknown += uncertain;
//return uncertain;
}
else if (pass < 4) {
// code.fitBits = IFIT_I16 | IFIT_I32; // symbol values are not available yet
code.fitAddr = IFIT_I16 | IFIT_I24 | IFIT_I32; // symbol values are not available yet
code.sizeUnknown += 1;
uncertain |= 1;
//return 1;
}
else { // self-relative address to local symbol
value = int32_t((uint32_t)symbols[isym1].st_value - (code.address + code.size * 4));
valueScaled = value;
valueScaled += code.offset_mem;
if (valueScaled >= -(1 << 15) && valueScaled < (1 << 15)) fitBits |= IFIT_I16;
if (valueScaled >= -(1 << 23) && valueScaled < (1 << 23)) fitBits |= IFIT_I24;
if (valueScaled >= -((int64_t)1 << 31) && valueScaled < ((int64_t)1 << 31)) fitBits |= IFIT_I32;
code.fitAddr = fitBits;
// check if value is certain. uncertainty is stored in high part of st_value and sh_link
uncertainty = int32_t((symbols[isym1].st_value >> 32) - sectionHeaders[code.section].sh_link);
valueScaled += uncertainty;
if ((valueScaled < -(1 << 7) || valueScaled >= (1 << 7)) && (fitBits & IFIT_I8)) uncertain |= 1;
if ((valueScaled < -(1 << 15) || valueScaled >= (1 << 15)) && (fitBits & IFIT_I16)) uncertain |= 1;
if ((valueScaled < -(1 << 23) || valueScaled >= (1 << 23)) && (fitBits & IFIT_I24)) uncertain |= 1;
if ((valueScaled < -((int64_t)1 << 31) || valueScaled >= ((int64_t)1 << 31)) && (fitBits & IFIT_I32)) uncertain |= 1;
if (uncertain && (code.fitAddr & IFIT_LARGE)) {
// choose the larger version if optimization process has convergence problems
fitBits = (fitBits & (fitBits - 1)) | IFIT_I32; // remove the lowest set bit
uncertain &= ~1;
}
code.fitAddr = fitBits;
code.sizeUnknown += uncertain;
//return uncertain;
}
}
else {
// symbol is relative to data pointer. relocation needed
fitBits = IFIT_RELOC;
if (relSize <= 1 << 7) fitBits |= IFIT_I8;
if (relSize <= 1 << 15) fitBits |= IFIT_I16;
if (relSize <= (uint64_t)1 << 31) fitBits |= IFIT_I32;
code.fitAddr = fitBits;
code.sizeUnknown += uncertain;
}
}
}
else {
// no symbol. only a signed integer constant
value = code.offset_mem;
fitBits = 0;
if (value >= -(int64_t)0x80 && value < 0x80) fitBits |= IFIT_I8;
if (value >= -(int64_t)0x8000 && value < 0x8000) fitBits |= IFIT_I16;
if (value >= -(int64_t)0x80000000 && value < 0x80000000) fitBits |= IFIT_I32;
code.fitAddr = fitBits;
}
// check jump offset symbol
if (code.sym5) {
// there is a jump offset symbol
value = code.offset_jump; // jump offset
fitBits = 0;
isym1 = findSymbol(code.sym5);
if (isym1 <= 0) {
code.sizeUnknown = 2; return 2; // should not occur
}
// one symbol relative to instruction pointer
if (symbols[isym1].st_unitsize == 0) uncertain = 2; // symbol value is not known yet
uint32_t sym1section = symbols[isym1].st_section; // symbol section
if (sym1section < sectionHeaders.numEntries()) {
// determine necessary relocation size if relocation needed
uint64_t relSize; // maximum size of relocated address
relSize = code_size >> 2; // relocation size for code and constant data, scaled by 4
if (sym1section != code.section || symbols[isym1].st_bind != STB_LOCAL) {
// symbol is in different section or not local. relocation needed
fitBits = IFIT_RELOC;
if (relSize <= 1 << 7) fitBits |= IFIT_I8; // necessary relocation size
if (relSize <= 1 << 15) fitBits |= IFIT_I16;
if (relSize <= 1 << 23) fitBits |= IFIT_I24;
if (relSize <= (uint64_t)1 << 31) fitBits |= IFIT_I32;
code.fitJump = fitBits;
code.sizeUnknown += uncertain;
//return uncertain;
}
else if (pass < 4) {
code.fitJump = IFIT_I16 | IFIT_I24 | IFIT_I32; // symbol values are not available yet
code.sizeUnknown += 1;
uncertain = 1;
//return 1;
}
else {
// self-relative address to local symbol
value = int32_t((uint32_t)symbols[isym1].st_value - (code.address + code.size * 4));
valueScaled = value >> 2; // jump address is scaled
valueScaled += code.offset_jump;
if (valueScaled >= -(1 << 7) && valueScaled < (1 << 7)) fitBits |= IFIT_I8;
if (valueScaled >= -(1 << 15) && valueScaled < (1 << 15)) fitBits |= IFIT_I16;
if (valueScaled >= -(1 << 23) && valueScaled < (1 << 23)) fitBits |= IFIT_I24;
if (valueScaled >= -((int64_t)1 << 31) && valueScaled < ((int64_t)1 << 31)) fitBits |= IFIT_I32;
code.fitJump = fitBits;
// check if value is certain. uncertainty is stored in high part of st_value and sh_link
uncertainty = int32_t((symbols[isym1].st_value >> 32) - sectionHeaders[code.section].sh_link);
valueScaled += uncertainty;
if ((valueScaled < -(1 << 7) || valueScaled >= (1 << 7)) && (fitBits & IFIT_I8)) uncertain |= 1;
if ((valueScaled < -(1 << 15) || valueScaled >= (1 << 15)) && (fitBits & IFIT_I16)) uncertain |= 1;
if ((valueScaled < -(1 << 23) || valueScaled >= (1 << 23)) && (fitBits & IFIT_I24)) uncertain |= 1;
if ((valueScaled < -((int64_t)1 << 31) || valueScaled >= ((int64_t)1 << 31)) && (fitBits & IFIT_I32)) uncertain |= 1;
if (uncertain && (code.fitAddr & IFIT_LARGE)) {
// choose the larger version if optimization process has convergence problems
fitBits = (fitBits & (fitBits - 1)) | IFIT_I32; // remove the lowest set bit
uncertain &= ~1;
code.fitJump = fitBits;
//code.sizeUnknown += uncertain;
}
code.sizeUnknown += uncertain;
}
}
}
return uncertain;
}
// find format details in formatList from entry in instructionlist
uint32_t findFormat(SInstruction const & listentry, uint32_t imm) {
// listentry: record in instructionlist or instructionlistId
// imm: immediate operand, if any
// make model instruction for lookupFormat
STemplate instrModel;
instrModel.a.il = listentry.format >> 8;
instrModel.a.mode = (listentry.format >> 4) & 7;
instrModel.a.ot = (listentry.format >> 5) & 4;
if ((listentry.format & ~ 0x12F) == 0x200) { // format 0x200, 0x220, 0x300, 0x320
instrModel.a.mode2 = listentry.format & 7;
}
else if ((listentry.format & 0xFF0) == 0x270 && listentry.op1 < 8) {
instrModel.a.mode2 = listentry.op1 & 7;
}
else instrModel.a.mode2 = 0;
instrModel.a.op1 = listentry.op1;
instrModel.b[0] = imm & 0xFF;
// look op details for this format (from emulator2.cpp)
return lookupFormat(instrModel.q);
}
// find the smallest representation that the floating point operand fits into
int fitFloat(double x) {
if (x == 0.) return IFIT_I8 | FFIT_16 | FFIT_32 | FFIT_64;
union {
double d;
struct {
uint64_t mantissa: 52;
uint64_t exponent: 11;
uint64_t sign: 1;
} f;
} u;
u.d = x;
int fit = FFIT_64;
// check if mantissa fits
if ((u.f.mantissa & (((uint64_t)1 << 42) - 1)) == 0) fit |= FFIT_16;
if ((u.f.mantissa & (((uint64_t)1 << 29) - 1)) == 0) fit |= FFIT_32;
// check if exponent fits, except for infinity or nan
if (u.f.exponent != 0x7FF) {
int ex = int(u.f.exponent - 0x3FF);
if (ex < -14 || ex > 15) fit &= ~FFIT_16;
if (ex < -126 || ex > 127) fit &= ~FFIT_32;
}
// check if x fits into a small integer
if (fit & FFIT_16) {
int i = int(x);
if (i == x && i >= -128 && i < 128) {
fit |= IFIT_I8;
}
}
return fit;
}
// find an instruction variant that fits the code
int CAssembler::fitCode(SCode & code) {
// return value:
// 0: does not fit
// 1: fits
uint32_t bestInstr = 0; // best fitting instruction variant, index into instructionlistId
uint32_t bestSize = 99; // size of best fitting instruction variant
SCode codeTemp; // fitted code
SCode codeBest; // best fitted code
uint32_t instrIndex = 0, ii; // index into instructionlistId
uint32_t formatIx = 0; // index into formatList
uint32_t isize; // il bits
codeBest.category = 0;
// find instruction by id
SInstruction3 sinstr; // make dummy record with instruction id as parameter to findAll
if (code.instruction == II_ALIGN) {
return 1; // alignment directive
}
sinstr.id = code.instruction;
int32_t nInstr = instructionlistId.findAll(&instrIndex, sinstr);
if (code.etype & (XPR_IMMEDIATE | XPR_OFFSET | XPR_LIMIT | XPR_JUMPOS)) {