forked from sellicott/tcc-riscv32
-
Notifications
You must be signed in to change notification settings - Fork 5
/
riscv32-asm.c
1173 lines (1049 loc) · 37 KB
/
riscv32-asm.c
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
/*************************************************************/
/*
* RISCV32 assembler for TCC
* Modified from riscv64 version
*
*/
#ifdef SDE_RISCV32_DEV
#undef TARGET_DEFS_ONLY
#endif
#ifdef TARGET_DEFS_ONLY
#define CONFIG_TCC_ASM
#define NB_ASM_REGS 32
ST_FUNC void g( int c );
ST_FUNC void gen_le16( int c );
ST_FUNC void gen_le32( int c );
/*************************************************************/
#else
/*************************************************************/
#define USING_GLOBALS
#include "riscv_utils.h"
#include "tcc.h"
enum {
OPT_REG,
OPT_IM12S,
OPT_IM21,
OPT_IM32,
OPT_SYM,
};
#define OP_REG ( 1 << OPT_REG )
#define OP_IM32 ( 1 << OPT_IM32 )
#define OP_IM21 ( 1 << OPT_IM21 )
#define OP_IM12S ( 1 << OPT_IM12S )
#define OP_SYM ( 1 << OPT_SYM )
// I don't think we need these anymore, but I haven't deleted them yet
#define ENCODE_RS1( register_index ) ( ( register_index ) << 15 )
#define ENCODE_RS2( register_index ) ( ( register_index ) << 20 )
#define ENCODE_RD( register_index ) ( ( register_index ) << 7 )
// --------------------------- boilerplate tcc requirements ----------------- //
ST_FUNC void g( int i )
{
int ind1;
if( nocode_wanted )
return;
ind1 = ind + 1;
if( ind1 > cur_text_section->data_allocated )
section_realloc( cur_text_section, ind1 );
cur_text_section->data[ ind ] = i;
ind = ind1;
}
ST_FUNC void gen_le16( int i )
{
int ind1;
if( nocode_wanted )
return;
ind1 = ind + 4;
if( ind1 > cur_text_section->data_allocated )
section_realloc( cur_text_section, ind1 );
cur_text_section->data[ ind++ ] = i & 0xFF;
cur_text_section->data[ ind++ ] = ( i >> 8 ) & 0xFF;
}
ST_FUNC void gen_le32( int i )
{
int ind1;
if( nocode_wanted )
return;
ind1 = ind + 4;
if( ind1 > cur_text_section->data_allocated )
section_realloc( cur_text_section, ind1 );
cur_text_section->data[ ind++ ] = i & 0xFF;
cur_text_section->data[ ind++ ] = ( i >> 8 ) & 0xFF;
cur_text_section->data[ ind++ ] = ( i >> 16 ) & 0xFF;
cur_text_section->data[ ind++ ] = ( i >> 24 ) & 0xFF;
}
ST_FUNC void gen_expr32( ExprValue *pe )
{
gen_le32( pe->v );
}
// ---------------------- end boilerplate tcc requirements ----------------- //
typedef struct Operand {
uint32_t type;
union {
uint8_t reg;
uint16_t regset;
ExprValue e;
};
} Operand;
// return the type for a variable based on its size
static uint32_t imm_op_type_from_size( uint32_t value )
{
uint32_t type = OP_IM32;
// check if we are in a 12b signed range
if( (int32_t)value >= -2048 && (int32_t)value < 2048 ) {
type |= OP_IM12S;
}
// check if we are within a 21b range
if( value <= 0x1fffff ) {
type |= OP_IM21;
}
return type;
}
/* Parse a text containing operand and store the result in OP */
static void parse_operand( TCCState *s1, Operand *op )
{
ExprValue e;
int8_t reg;
op->type = 0;
if( ( reg = asm_parse_regvar( tok ) ) != -1 ) {
next(); // skip register name
op->type = OP_REG;
op->reg = (uint8_t)reg;
return;
}
else if( tok == '$' ) {
/* constant value */
next(); // skip '#' or '$'
}
asm_expr( s1, &e );
op->type = OP_IM32;
op->e = e;
if( !op->e.sym ) {
op->type = imm_op_type_from_size( op->e.v );
}
else {
// Deal with the case of a symbol.
// If we have a symbol we will let the linker handle it and just put in a zero for the
// immediate value, we also need to put an entry into the reallocation table, this should
// however, the entry will be different depending on the instruction, so ignore it for now.
// get the type and value information from the included symbol
Sym *sym = op->e.sym;
op->type = OP_SYM;
if( ( reg = asm_parse_regvar( sym->v ) ) != -1 ) {
op->type |= OP_REG;
op->reg = reg;
return;
}
// immediate value? do type size checking
op->type |= imm_op_type_from_size( sym->c );
op->e.v = sym->c;
}
}
// return false (0) if next operand not a register and true (1) if it is
static int check_register( const Operand *op )
{
if( op->type != OP_REG ) {
tcc_error_noabort( "expected a register" );
return 0;
}
return 1;
}
// return false (0) if next operand not an immediate and true (1) if it is
static int check_immediate( const Operand *op )
{
if( op->type != OP_IM12S && op->type != OP_IM32 && op->type != OP_IM21 ) {
tcc_error_noabort( "expected an immediate" );
return 0;
}
return 1;
}
// implement helper functions from riscv_utils.h
// Register instructions (math and stuff)
void emit_R(
uint32_t funct7, uint32_t rs2, uint32_t rs1, uint32_t funct3, uint32_t rd, uint32_t opcode )
{
const uint32_t instruction =
( funct7 << 25 ) | ( rs2 << 20 ) | ( rs1 << 15 ) | ( funct3 << 12 ) | ( rd << 7 ) | opcode;
o( instruction );
}
// immediate instructions
void emit_I( uint32_t imm, uint32_t rs1, uint32_t funct3, uint32_t rd, uint32_t opcode )
{
const uint32_t instruction =
( imm << 20 ) | ( rs1 << 15 ) | ( funct3 << 12 ) | ( rd << 7 ) | opcode;
o( instruction );
}
// store instructions
void emit_S( uint32_t imm, uint32_t rs2, uint32_t rs1, uint32_t funct3, uint32_t opcode )
{
// we have to break up the immediate value into two sections (11:5, 4:0)
const uint32_t imm_h = 0x7f & ( imm >> 5 );
const uint32_t imm_l = 0x1f & imm;
const uint32_t instruction = ( imm_h << 25 ) | ( rs2 << 20 ) | ( rs1 << 15 ) |
( funct3 << 12 ) | ( imm_l << 7 ) | opcode;
o( instruction );
}
/* branch instructions
* takes a 13-bit immediate value (LSB is discarded)
*/
void emit_B( uint32_t imm, uint32_t rs2, uint32_t rs1, uint32_t funct3, uint32_t opcode )
{
// we have to break up the immediate value into two sections (12,10:5, 4:1,11)
// imm_h[5:0] = {12, 10:5}
// imm_l[5:0] = {4:1, 11}
const uint32_t imm_h = 0x7f & ( ( ( 0x800 & imm ) >> 1 ) | ( 0x7e0 & imm ) ) >> 5;
const uint32_t imm_l = ( 0x1e & imm ) | ( 0x1 & ( imm >> 11 ) );
const uint32_t instruction = ( imm_h << 25 ) | ( rs2 << 20 ) | ( rs1 << 15 ) |
( funct3 << 12 ) | ( imm_l << 7 ) | opcode;
o( instruction );
}
/* big immediate instructions (LUI and AUIPC)
* takes a 24-bit immediate value
* we expect the immediate passed into this function to be in the lower space (i.e we will shift)
*/
void emit_U( uint32_t imm, uint32_t rd, uint32_t opcode )
{
const uint32_t instruction = ( imm << 12 ) | ( rd << 7 ) | opcode;
o( instruction );
}
/* jump instructions
* takes a 21-bit immediate value
*/
void emit_J( uint32_t imm, uint32_t rd, uint32_t opcode )
{
// immediate value 20|10:1|11|19:12
// mask off bits 19:12 then shift them back
// mask off bit 11 then shift it over to bit 20
// mask off bits 10:1, then shift over 21
// mask off bit 21, then shift over 31
const uint32_t immediate = ( ( ( imm >> 12 ) & 0xff ) << 12 ) |
( ( ( imm >> 11 ) & 1 ) << 20 ) |
( ( ( imm >> 1 ) & 0x3ff ) << 21 ) | ( ( ( imm >> 20 ) & 1 ) << 31 );
const uint32_t instruction = immediate | ( rd << 7 ) | opcode;
o( instruction );
}
/*
* Add an entry to the elf relocation table for the symbol in the operand op.
* The type of the relocation table entry needs to match format of immediate value used by the
* instruction.
*/
void generate_symbol_reallocation( const Operand *op, int type )
{
if( op && op->e.sym ) {
greloc( cur_text_section, op->e.sym, ind, type );
}
}
// ------------------------- end basic helper functions --------------------- //
static void asm_nullary_opcode( TCCState *s1, int token )
{
switch( token ) {
case TOK_ASM_wfi:
// I don't really know what WFI is
o( ( 0x1C << 2 ) | 3 | ( 0x105 << 20 ) );
return;
case TOK_ASM_ret: emit_RET(); return;
default: expect( "nullary instruction" );
}
}
// Note: Those all map to CSR--so they are pseudo-instructions.
static void asm_unary_opcode( TCCState *s1, int token )
{
Operand op;
int rd;
parse_operand( s1, &op );
if( token != TOK_ASM_j && op.type != OP_REG ) {
tcc_error( "'%s': Expected operand to be a register\n"
"received: %d",
get_tok_str( token, NULL ), op.reg );
return;
}
else if( token == TOK_ASM_j && op.type == OP_REG ) {
tcc_error( "'%s': Expected operand to be an immediate offset\n"
"received: %ld",
get_tok_str( token, NULL ), op.e.v );
return;
}
rd = op.reg;
switch( token ) {
case TOK_ASM_j: emit_J_inst( op.e.v ); return;
case TOK_ASM_rdcycle: emit_RDCYCLE( rd ); return;
case TOK_ASM_rdcycleh: emit_RDCYCLEH( rd ); return;
case TOK_ASM_rdtime: emit_RDTIME( rd ); return;
case TOK_ASM_rdtimeh: emit_RDTIMEH( rd ); return;
case TOK_ASM_rdinstret: emit_RDINSTRET( rd ); return;
case TOK_ASM_rdinstreth: emit_RDINSTRETH( rd ); return;
default: expect( "unary instruction" );
}
}
static void asm_binary_pseudocode( TCCState *s1, int token )
{
Operand rs, rd;
parse_operand( s1, &rd );
if( rd.type != OP_REG ) {
tcc_error(
"'%s': Expected destination operand that is a register", get_tok_str( token, NULL ) );
return;
}
// printf("hi 1\n");
if( tok == ',' ) {
next();
}
else {
expect( "','" );
}
// printf("hi 2\n");
parse_operand( s1, &rs );
if( rs.type != OP_REG ) {
tcc_error( "'%s': Expected source operand that is a register", get_tok_str( token, NULL ) );
return;
}
switch( token ) {
case TOK_ASM_mv: emit_MV( rd.reg, rs.reg ); return;
// comparison to zero pseudoinstructions
case TOK_ASM_seqz: emit_SEQZ( rd.reg, rs.reg ); return;
case TOK_ASM_snez: emit_SNEZ( rd.reg, rs.reg ); return;
case TOK_ASM_sltz: emit_SLTZ( rd.reg, rs.reg ); return;
case TOK_ASM_sgtz: emit_SGTZ( rd.reg, rs.reg ); return;
}
}
static void asm_branch_zero_opcode( TCCState *s1, int token )
{
// Branch (RS1,IMM); SB-format
uint32_t offset = 0;
Operand r1, imm;
parse_operand( s1, &r1 );
if( r1.type != OP_REG ) {
tcc_error( "'%s': Expected first operand to be a register\n"
"received: %d",
get_tok_str( token, NULL ), r1.reg );
return;
}
if( tok == ',' )
next();
else
expect( "','" );
parse_operand( s1, &imm );
offset = imm.e.v;
if( offset > 0xfff ) {
tcc_error( "'%s': Expected third operand that is an immediate value between 0 and 0xfff\n"
"received: %ld",
get_tok_str( token, NULL ), imm.e.v );
return;
}
if( offset & 1 ) {
tcc_error( "'%s': Expected third operand that is an even immediate value",
get_tok_str( token, NULL ) );
return;
}
generate_symbol_reallocation( &imm, R_RISCV_BRANCH );
switch( token ) {
case TOK_ASM_beqz: emit_BEQZ( r1.reg, offset ); return;
case TOK_ASM_bnez: emit_BNEZ( r1.reg, offset ); return;
case TOK_ASM_blez: emit_BEQZ( r1.reg, offset ); return;
case TOK_ASM_bgez: emit_BGEZ( r1.reg, offset ); return;
case TOK_ASM_bltz: emit_BLTZ( r1.reg, offset ); return;
case TOK_ASM_bgtz: emit_BGTZ( r1.reg, offset ); return;
default: expect( "known branch instruction" );
}
}
static void asm_binary_opcode( TCCState *s1, int token )
{
Operand ops[ 2 ];
uint32_t rd;
uint32_t imm;
int ind_bak;
parse_operand( s1, &ops[ 0 ] );
if( ops[ 0 ].type != OP_REG ) {
tcc_error(
"'%s': Expected destination operand that is a register", get_tok_str( token, NULL ) );
return;
}
// printf("hi 1\n");
if( tok == ',' ) {
next();
}
else {
expect( "','" );
}
// printf("hi 2\n");
parse_operand( s1, &ops[ 1 ] );
// la pseudoinstruction can load a full 32b word
if( token != TOK_ASM_la ) {
if( !( ops[ 1 ].type & OP_IM32 ) ) {
tcc_error( "'%s': Expected second source operand that is an immediate value",
get_tok_str( token, NULL ) );
return;
}
else if( ops[ 1 ].e.v >= 0x100000 ) {
tcc_error( "'%s': Expected second source operand that is an immediate value between 0 "
"and 0xfffff",
get_tok_str( token, NULL ) );
return;
}
}
rd = ops[ 0 ].reg;
imm = ops[ 1 ].e.v;
switch( token ) {
case TOK_ASM_la:
// hacky method to generate relocation entries at the correct offsets (ind)
ind_bak = ind;
generate_symbol_reallocation( &ops[ 1 ], R_RISCV_HI20 );
ind = ind + 4;
generate_symbol_reallocation( &ops[ 1 ], R_RISCV_LO12_I );
// rewind offset to put instructions at the correct offsets
ind = ind_bak;
emit_LA( rd, imm );
return;
case TOK_ASM_li:
// hacky method to generate relocation entries at the correct offsets (ind)
ind_bak = ind;
generate_symbol_reallocation( &ops[ 1 ], R_RISCV_HI20 );
ind = ind + 4;
generate_symbol_reallocation( &ops[ 1 ], R_RISCV_LO12_I );
// rewind offset to put instructions at the correct offsets
ind = ind_bak;
emit_LI( rd, imm );
return;
case TOK_ASM_lui:
generate_symbol_reallocation( &ops[ 1 ], R_RISCV_HI20 );
emit_LUI( rd, imm );
return;
case TOK_ASM_auipc:
generate_symbol_reallocation( &ops[ 1 ], R_RISCV_HI20 );
emit_AUIPC( rd, imm );
return;
default: expect( "binary instruction" );
}
}
static void asm_branch_opcode( TCCState *s1, int token )
{
uint32_t offset = 0;
Operand ops[ 2 ], imm;
parse_operand( s1, &ops[ 0 ] );
if( ops[ 0 ].type != OP_REG ) {
tcc_error( "'%s': Expected first operand to be a register\n"
"received: %d",
get_tok_str( token, NULL ), ops[ 0 ].reg );
return;
}
if( tok == ',' )
next();
else
expect( "','" );
parse_operand( s1, &ops[ 1 ] );
if( ops[ 1 ].type != OP_REG ) {
tcc_error( "'%s': Expected second operand to be a register\n"
"received: %d",
get_tok_str( token, NULL ), ops[ 1 ].reg );
return;
}
if( tok == ',' )
next();
else
expect( "','" );
parse_operand( s1, &imm );
offset = imm.e.v;
// if we are using a symbol, the value will be handled by the relocation table
if( imm.type & OP_SYM ) {
offset = 0;
}
if( offset > 0xfff ) {
tcc_error( "'%s': Expected third operand that is an immediate value between 0 and 0xfff\n"
"received: %u",
get_tok_str( token, NULL ), offset );
return;
}
if( offset & 1 ) {
tcc_error( "'%s': Expected third operand that is an even immediate value, received: %u",
get_tok_str( token, NULL ), offset );
return;
}
generate_symbol_reallocation( &imm, R_RISCV_BRANCH );
switch( token ) {
case TOK_ASM_beq: emit_BEQ( ops[ 0 ].reg, ops[ 1 ].reg, offset ); return;
case TOK_ASM_bne: emit_BNE( ops[ 0 ].reg, ops[ 1 ].reg, offset ); return;
case TOK_ASM_blt: emit_BLT( ops[ 0 ].reg, ops[ 1 ].reg, offset ); return;
case TOK_ASM_ble: emit_BLE( ops[ 0 ].reg, ops[ 1 ].reg, offset ); return;
case TOK_ASM_bltu: emit_BLTU( ops[ 0 ].reg, ops[ 1 ].reg, offset ); return;
case TOK_ASM_bgeu: emit_BGEU( ops[ 0 ].reg, ops[ 1 ].reg, offset ); return;
case TOK_ASM_bgtu: emit_BGTU( ops[ 0 ].reg, ops[ 1 ].reg, offset ); return;
case TOK_ASM_bleu: emit_BLEU( ops[ 0 ].reg, ops[ 1 ].reg, offset ); return;
default: expect( "known branch instruction" );
}
}
// handle the register opcodes that don't have an immediate value (except for shifts)
static void asm_binary_register_opcode( TCCState *s1, int token )
{
// start by getting the next three operands and check that they are all registers
Operand rd, r1, r2;
parse_operand( s1, &rd );
if( !check_register( &rd ) ) {
tcc_error( "'%s': Expected destination to be a register", get_tok_str( token, NULL ) );
}
if( tok == ',' ) {
next();
}
else {
expect( "','" );
}
parse_operand( s1, &r1 );
if( !check_register( &r1 ) ) {
tcc_error(
"'%s': Expected first source operand to be a register", get_tok_str( token, NULL ) );
}
if( tok == ',' ) {
next();
}
else {
expect( "','" );
}
parse_operand( s1, &r2 );
// check that we are using a register, or that we have an immediate between 0 and 31 (for
// shifts)
if( !( r2.type & OP_REG ) && r2.e.v > 31 ) {
tcc_error(
"'%s': Expected second source operand to be a register or immediate between 0 and 31",
get_tok_str( token, NULL ) );
return;
}
// switch through the various register instructions
switch( token ) {
case TOK_ASM_slli: emit_SLLI( rd.reg, r1.reg, r2.e.v ); return;
case TOK_ASM_srli: emit_SRLI( rd.reg, r1.reg, r2.e.v ); return;
case TOK_ASM_srai: emit_SRAI( rd.reg, r1.reg, r2.e.v ); return;
case TOK_ASM_add: emit_ADD( rd.reg, r1.reg, r2.reg ); return;
case TOK_ASM_sub: emit_SUB( rd.reg, r1.reg, r2.reg ); return;
case TOK_ASM_sll: emit_SLL( rd.reg, r1.reg, r2.reg ); return;
case TOK_ASM_slt: emit_SLT( rd.reg, r1.reg, r2.reg ); return;
case TOK_ASM_sltu: emit_SLTU( rd.reg, r1.reg, r2.reg ); return;
case TOK_ASM_xor: emit_XOR( rd.reg, r1.reg, r2.reg ); return;
case TOK_ASM_srl: emit_SRL( rd.reg, r1.reg, r2.reg ); return;
case TOK_ASM_sra: emit_SRA( rd.reg, r1.reg, r2.reg ); return;
case TOK_ASM_or: emit_OR( rd.reg, r1.reg, r2.reg ); return;
case TOK_ASM_and: emit_AND( rd.reg, r1.reg, r2.reg ); return;
case TOK_ASM_mul: emit_MUL( rd.reg, r1.reg, r2.reg ); return;
case TOK_ASM_mulh: emit_MULH( rd.reg, r1.reg, r2.reg ); return;
case TOK_ASM_mulhsu: emit_MULHSU( rd.reg, r1.reg, r2.reg ); return;
case TOK_ASM_div: emit_DIV( rd.reg, r1.reg, r2.reg ); return;
case TOK_ASM_divu: emit_DIVU( rd.reg, r1.reg, r2.reg ); return;
case TOK_ASM_rem: emit_REM( rd.reg, r1.reg, r2.reg ); return;
case TOK_ASM_remu: emit_REMU( rd.reg, r1.reg, r2.reg ); return;
default: expect( "register instruction" );
}
}
static void asm_immediate_opcode( TCCState *s1, int token )
{
// start by getting the next three operands and check that they are all registers
Operand regs[ 2 ], imm;
parse_operand( s1, ®s[ 0 ] );
if( !check_register( ®s[ 0 ] ) ) {
tcc_error( "'%s': Expected destination to be a register", get_tok_str( token, NULL ) );
return;
}
if( tok == ',' ) {
next();
}
else {
expect( "','" );
}
parse_operand( s1, ®s[ 1 ] );
if( !check_register( ®s[ 1 ] ) ) {
tcc_error(
"'%s': Expected second source operand to be a register", get_tok_str( token, NULL ) );
return;
}
if( tok == ',' ) {
next();
}
else {
expect( "','" );
}
parse_operand( s1, &imm );
// check that we have an 12bit immediate
if( !( imm.type & OP_IM12S ) ) {
tcc_error( "'%s': Expected third operand that is an immediate value between 0 and 0xfff",
get_tok_str( token, NULL ) );
return;
}
// switch through the various register instructions
switch( token ) {
case TOK_ASM_addi: emit_ADDI( regs[ 0 ].reg, regs[ 1 ].reg, imm.e.v ); return;
case TOK_ASM_slti: emit_SLTI( regs[ 0 ].reg, regs[ 1 ].reg, imm.e.v ); return;
case TOK_ASM_sltiu: emit_SLTIU( regs[ 0 ].reg, regs[ 1 ].reg, imm.e.v ); return;
case TOK_ASM_xori: emit_XORI( regs[ 0 ].reg, regs[ 1 ].reg, imm.e.v ); return;
case TOK_ASM_ori: emit_ORI( regs[ 0 ].reg, regs[ 1 ].reg, imm.e.v ); return;
case TOK_ASM_andi: emit_ANDI( regs[ 0 ].reg, regs[ 1 ].reg, imm.e.v ); return;
default: expect( "immediate register instruction" );
}
}
// make a special case for load/store opcodes since they have a distinct syntax from the other
// commands
static void asm_load_store_opcode( TCCState *s1, int token )
{
// start by getting the next three operands and check that they are all registers
// for a store instruction we expect the format to be
// s? src, imm(dst)
// store src -> [loc_reg + imm]
// this formats to the opcode
// s? loc_reg, src, imm
Operand src_dst, imm, loc_reg;
parse_operand( s1, &src_dst );
if( !check_register( &src_dst ) ) {
tcc_error( "'%s': Expected source to be a register", get_tok_str( token, NULL ) );
}
if( tok == ',' ) {
next();
}
else {
expect( "','" );
}
// check if the immediate is available, if it isn't we will have a '(' character.
if( tok != '(' ) {
// try to grab the immediate value
parse_operand( s1, &imm );
// check that we have an 12bit immediate
if( !( imm.type & OP_IM12S ) ) {
tcc_error( "'%s': Expected immediate value between -2048 and 2047",
get_tok_str( token, NULL ) );
return;
}
}
else {
// no immediate, set imm value to zero
imm.e.v = 0;
imm.type = OP_IM12S;
}
// try and grab the offset register after the parenthesis
if( tok == '(' ) {
next();
}
else {
expect( "'('" );
}
parse_operand( s1, &loc_reg );
if( !check_register( &loc_reg ) ) {
tcc_error( "'%s': Expected offset operand to be a register", get_tok_str( token, NULL ) );
return;
}
// get closing paren
if( tok == ')' ) {
next();
}
else {
expect( "')'" );
}
// switch through the various register instructions
switch( token ) {
// load
case TOK_ASM_lb: emit_LB( src_dst.reg, loc_reg.reg, imm.e.v ); return;
case TOK_ASM_lh: emit_LH( src_dst.reg, loc_reg.reg, imm.e.v ); return;
case TOK_ASM_lw: emit_LW( src_dst.reg, loc_reg.reg, imm.e.v ); return;
case TOK_ASM_lbu: emit_LBU( src_dst.reg, loc_reg.reg, imm.e.v ); return;
case TOK_ASM_lhu: emit_LHU( src_dst.reg, loc_reg.reg, imm.e.v ); return;
// store
case TOK_ASM_sb: emit_SB( loc_reg.reg, src_dst.reg, imm.e.v ); return;
case TOK_ASM_sh: emit_SH( loc_reg.reg, src_dst.reg, imm.e.v ); return;
case TOK_ASM_sw: emit_SW( loc_reg.reg, src_dst.reg, imm.e.v ); return;
default: expect( "store instruction" );
}
}
// support the Zicsr extension Takes in two arguments
// for CSRR a dest register and an immediate value
// or an immediate value and a source register
// or two immediate values
static void asm_control_status_pseudo_opcode( TCCState *s1, int token )
{
// set to values outside of the valid range
int rd = 64;
int rs = 64;
uint32_t csr = 0xffffffff;
uint32_t imm = 0;
Operand temp;
// start by getting the next two operands
parse_operand( s1, &temp );
// if getting the csrr instruction we want a register
if( token == TOK_ASM_csrr ) {
if( !check_register( &temp ) ) {
tcc_error( "'%s': Expected destination to be a register", get_tok_str( token, NULL ) );
return;
}
rd = temp.reg;
}
// otherwise we want an immediate between 0x and 0xfffff
else {
if( !check_immediate( &temp ) ) {
tcc_error( "'%s': Expected csr to be an immediate between 0 and 0xfffff",
get_tok_str( token, NULL ) );
return;
}
else if( !( temp.e.v >= 0 && temp.e.v <= 0xfffff ) ) {
tcc_error( "'%s': Expected csr to be an immediate between 0 and 0xfffff",
get_tok_str( token, NULL ) );
return;
}
csr = temp.e.v;
}
if( tok == ',' ) {
next();
}
else {
expect( "','" );
}
parse_operand( s1, &temp );
// for csrr, csrwi, csrsi, csrci we can get an immediate
if( token == TOK_ASM_csrr || token == TOK_ASM_csrwi || token == TOK_ASM_csrci ) {
int is_immediate = check_immediate( &temp );
// csrr needs an immediate between 0 and 0xfffff
if( token == TOK_ASM_csrr ) {
if( !is_immediate || ( is_immediate && ( temp.e.v >= 0 && temp.e.v <= 0xfffff ) ) ) {
tcc_error( "'%s': Expected csr to be an immediate between 0 and 0xfffff",
get_tok_str( token, NULL ) );
return;
}
csr = temp.e.v;
}
else {
// check for an immediate between 0 and 31
if( is_immediate && ( temp.e.v >= 0 && temp.e.v <= 31 ) ) {
imm = temp.e.v;
}
else if( !is_immediate ) {
imm = temp.reg;
}
// otherwise we should send an error
else {
tcc_error( "'%s': Expected second argument to be an immediate between 0 and 0x1f",
get_tok_str( token, NULL ) );
return;
}
}
}
// other instruction
else {
if( !check_register( &temp ) ) {
tcc_error(
"'%s': Expected second argument to be a register", get_tok_str( token, NULL ) );
return;
}
rs = temp.reg;
}
switch( token ) {
case TOK_ASM_csrr: emit_CSRR( rd, csr ); return;
case TOK_ASM_csrw: emit_CSRW( csr, rs ); return;
case TOK_ASM_csrs: emit_CSRS( csr, rs ); return;
case TOK_ASM_csrc: emit_CSRC( csr, rs ); return;
case TOK_ASM_csrwi: emit_CSRWI( csr, imm ); return;
case TOK_ASM_csrsi: emit_CSRSI( csr, imm ); return;
case TOK_ASM_csrci: emit_CSRCI( csr, imm ); return;
default: expect( "control and status instruction" ); return;
}
}
// support the Zicsr extension Takes in three arguments a dest register, immediate value, and a
// source register or a source immediate
static void asm_control_status_opcode( TCCState *s1, int token )
{
// start by getting the next three operands and check that they are all registers
Operand rd, csr;
Operand temp, rs1, uimm;
parse_operand( s1, &rd );
if( !check_register( &rd ) ) {
tcc_error( "'%s': Expected destination to be a register", get_tok_str( token, NULL ) );
return;
}
if( tok == ',' ) {
next();
}
else {
expect( "','" );
}
parse_operand( s1, &csr );
if( !check_immediate( &csr ) ) {
tcc_error( "'%s': Expected csr to be an immediate between 0 and 0xfffff",
get_tok_str( token, NULL ) );
return;
}
// range check the second immediate
else if( !( csr.e.v >= 0 && csr.e.v <= 0xfffff ) ) {
tcc_error( "'%s': Expected csr to be an immediate between 0 and 0xfffff",
get_tok_str( token, NULL ) );
return;
}
if( tok == ',' ) {
next();
}
else {
expect( "','" );
}
parse_operand( s1, &uimm );
if( tok == ',' ) {
next();
}
else {
expect( "','" );
}
parse_operand( s1, &temp );
// check that we are using a register, or that we have an immediate between 0 and 31 (for
// shifts)
if( temp.type != OP_REG && temp.e.v <= 31 ) {
tcc_error(
"'%s': Expected second source operand to be a register or immediate between 0 and 31",
get_tok_str( token, NULL ) );
return;
}
rs1 = uimm = temp;
switch( token ) {
case TOK_ASM_csrrw: emit_CSRRW( rd.reg, csr.e.v, rs1.reg ); return;
case TOK_ASM_csrrs: emit_CSRRS( rd.reg, csr.e.v, rs1.reg ); return;
case TOK_ASM_csrrc: emit_CSRRC( rd.reg, csr.e.v, rs1.reg ); return;
case TOK_ASM_csrrwi: emit_CSRRC( rd.reg, csr.e.v, rs1.e.v ); return;
case TOK_ASM_csrrsi: emit_CSRRC( rd.reg, csr.e.v, uimm.e.v ); return;
case TOK_ASM_csrrci: emit_CSRRC( rd.reg, csr.e.v, uimm.e.v ); return;
default: expect( "control and status instruction" ); return;
}
}
// Generate code for j, jal, jalr, and jr. Can take 1, 2, or 3 arguments (from TCCState)
static void asm_jump_opcode( TCCState *s1, int token )
{
// j takes 1 immediate argument
// jal can take either a register and an immediate or just an immediate
// jalr can take either a register, or two registers and an immediate
// jr takes a register
Operand ops[ 3 ];
// we always need to get the first argument
parse_operand( s1, &ops[ 0 ] );
// if we have an immediate then check if we match one of our instruction types
// (j offset, jal offset)
if( ops[ 0 ].type != OP_REG ) {
switch( token ) {
case TOK_ASM_j: emit_J_inst( ops[ 0 ].e.v ); break;
case TOK_ASM_jal: emit_JAL_x1( ops[ 0 ].e.v ); break;
// other than these two instructions, we expect to have a register, so issue an error
default: expect( "register" ); return;
}
// a bit late, but check if the data type fits our expectations on its size
// we only get here if the instructions are of type j, or jal
if( !( ops[ 0 ].type & OP_IM21 ) ) {
tcc_error( "'%s': Expected operand that is an immediate value between 0 and 0x1fffff",
get_tok_str( token, NULL ) );
}
return;
}
// first argument is a register
// test if we are in the jr case (jr register)
if( token == TOK_ASM_jr ) {
emit_JR( ops[ 0 ].reg );
return;
}
// done with one argument instructions, get the next argument
if( tok == ',' ) {
next();
}
else {
expect( "','" );
}
parse_operand( s1, &ops[ 1 ] );
// check if we have an immediate and we are in the jal instruction
if( ops[ 0 ].type != OP_REG ) {
// Other instructions need a register so issue an error
if( token != TOK_ASM_jal ) {
expect( "register" );
tcc_error( "'%s': Expected first operand to be a register\n"
"received: %d",
get_tok_str( token, NULL ), ops[ 0 ].reg );
return;
}
// check if the data type fits our expectations on its size
if( !( ops[ 1 ].type & OP_IM21 ) ) {
tcc_error(
"'%s': Expected second operand that is an immediate value between 0 and 0xfffff",
get_tok_str( token, NULL ) );
return;
}
emit_JAL( ops[ 0 ].reg, ops[ 1 ].e.v );
return;
}
// first two arguments are registers
// done with two argument instructions, get the next argument
if( tok == ',' ) {
next();
}
else {
expect( "','" );
}
parse_operand( s1, &ops[ 2 ] );
// The last argument should be a 12b immediate
if( !( ops[ 2 ].type & OP_IM12S ) ) {
tcc_error( "'%s': Expected third operand that is an immediate value between 0 and 0xfff",
get_tok_str( token, NULL ) );
return;
}
// the only instruction left is jalr rd, r1, offset
if( token == TOK_ASM_jalr ) {
emit_JALR( ops[ 0 ].reg, ops[ 1 ].reg, ops[ 2 ].e.v );
return;
}
// we didn't get the instruction we expected, send an error
expect( "jump or link instruction" );
return;
}
// emit code for call and tail pseudoinstructions
static void asm_call_opcode( TCCState *s1, int token )
{
Operand symbol;
parse_operand( s1, &symbol );
// check if the data type fits our expectations on its size
if( !( symbol.type & OP_IM32 ) ) {
tcc_error( "'%s': Expected 32b immediate value", get_tok_str( token, NULL ) );
return;
}
generate_symbol_reallocation( &symbol, R_RISCV_CALL );
switch( token ) {
case TOK_ASM_call: emit_CALL( symbol.e.v ); return;
case TOK_ASM_tail: emit_TAIL( symbol.e.v ); return;