-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathriscvm.c
838 lines (838 loc) · 27.4 KB
/
riscvm.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
/* Generated by Nelua 0.2.0-dev */
/* Compile command: gcc -x c "riscvm.c" -x none -pipe -fwrapv -fno-strict-aliasing -g -lm -o "riscvm" */
/* Compile hash: 3yRF316SvDmqYnDkveN73j7PiY2f */
/* ------------------------------ DIRECTIVES -------------------------------- */
#define NELUA_NIL (nlniltype){}
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
/* Macro used to force inlining a function. */
#ifdef __GNUC__
#define NELUA_INLINE __attribute__((always_inline)) inline
#elif defined(_MSC_VER)
#define NELUA_INLINE __forceinline
#elif __STDC_VERSION__ >= 199901L
#define NELUA_INLINE inline
#else
#define NELUA_INLINE
#endif
#include <stddef.h>
#include <stdlib.h>
/* Macro used to import/export extern C functions. */
#ifdef __cplusplus
#define NELUA_EXTERN extern "C"
#else
#define NELUA_EXTERN extern
#endif
/* Macro used to generate traceback on aborts when sanitizing. */
#if defined(__clang__) && defined(__has_feature)
#if __has_feature(undefined_behavior_sanitizer)
#define NELUA_UBSAN_UNREACHABLE __builtin_unreachable
#endif
#elif defined(__gnu_linux__) && defined(__GNUC__) && __GNUC__ >= 5
NELUA_EXTERN void __ubsan_handle_builtin_unreachable(void*) __attribute__((weak));
#define NELUA_UBSAN_UNREACHABLE() {if(&__ubsan_handle_builtin_unreachable) __builtin_unreachable();}
#endif
#ifndef NELUA_UBSAN_UNREACHABLE
#define NELUA_UBSAN_UNREACHABLE()
#endif
/* Macro used to specify a function that never returns. */
#if __STDC_VERSION__ >= 201112L
#define NELUA_NORETURN _Noreturn
#elif defined(__GNUC__)
#define NELUA_NORETURN __attribute__((noreturn))
#elif defined(_MSC_VER)
#define NELUA_NORETURN __declspec(noreturn)
#else
#define NELUA_NORETURN
#endif
/* Macro used sign that a type punning cast may alias (related to strict aliasing). */
#ifdef __GNUC__
#define NELUA_MAYALIAS __attribute__((may_alias))
#else
#define NELUA_MAYALIAS
#endif
/* Macro used for branch prediction. */
#if defined(__GNUC__) || defined(__clang__)
#define NELUA_LIKELY(x) __builtin_expect(x, 1)
#else
#define NELUA_LIKELY(x) (x)
#endif
/* Macro used for branch prediction. */
#if defined(__GNUC__) || defined(__clang__)
#define NELUA_UNLIKELY(x) __builtin_expect(x, 0)
#else
#define NELUA_UNLIKELY(x) (x)
#endif
/* Macro used to force not inlining a function. */
#ifdef __GNUC__
#define NELUA_NOINLINE __attribute__((noinline))
#elif defined(_MSC_VER)
#define NELUA_NOINLINE __declspec(noinline)
#else
#define NELUA_NOINLINE
#endif
/* ------------------------------ DECLARATIONS ------------------------------ */
typedef struct nlniltype {} nlniltype;
typedef struct nlniltype nltype;
typedef struct machine_Machine machine_Machine;
typedef machine_Machine* machine_Machine_ptr;
struct machine_Machine {
bool running;
int64_t pc;
uint64_t regs[32];
uint8_t memory[134217728];
int64_t exitcode;
};
typedef struct nlstring nlstring;
typedef uint8_t* nluint8_arr0_ptr;
struct nlstring {
nluint8_arr0_ptr data;
uintptr_t size;
};
static void machine_Machine_loadfile(machine_Machine_ptr self, nlstring filename);
typedef FILE* FILE_ptr;
static NELUA_INLINE char* nelua_string2cstring(nlstring s);
static NELUA_INLINE void nelua_write_stderr(const char* msg, uintptr_t len, bool flush);
static NELUA_NORETURN void nelua_abort(void);
static NELUA_NORETURN void nelua_panic_string(nlstring s);
typedef struct NELUA_MAYALIAS nluint8_arr134217728 {uint8_t v[134217728];} nluint8_arr134217728;
typedef union NELUA_MAYALIAS nluint8_arr134217728_cast {nluint8_arr134217728 a; uint8_t p[134217728];} nluint8_arr134217728_cast;
typedef struct NELUA_MAYALIAS nluint64_arr32 {uint64_t v[32];} nluint64_arr32;
typedef union NELUA_MAYALIAS nluint64_arr32_cast {nluint64_arr32 a; uint64_t p[32];} nluint64_arr32_cast;
static NELUA_INLINE uint32_t machine_Machine_fetch(machine_Machine_ptr self);
typedef uint32_t* nluint32_ptr;
static NELUA_INLINE int8_t machine_Machine_read_1(machine_Machine_ptr self, nlniltype T, uint64_t addr);
static NELUA_INLINE int16_t machine_Machine_read_2(machine_Machine_ptr self, nlniltype T, uint64_t addr);
typedef uint16_t* nluint16_ptr;
static NELUA_INLINE int32_t machine_Machine_read_3(machine_Machine_ptr self, nlniltype T, uint64_t addr);
static NELUA_INLINE int64_t machine_Machine_read_4(machine_Machine_ptr self, nlniltype T, uint64_t addr);
typedef uint64_t* nluint64_ptr;
static NELUA_INLINE uint8_t machine_Machine_read_5(machine_Machine_ptr self, nlniltype T, uint64_t addr);
static NELUA_INLINE uint16_t machine_Machine_read_6(machine_Machine_ptr self, nlniltype T, uint64_t addr);
static NELUA_INLINE uint32_t machine_Machine_read_7(machine_Machine_ptr self, nlniltype T, uint64_t addr);
static NELUA_INLINE void* machine_Machine_getptr(machine_Machine_ptr self, uint64_t addr);
static NELUA_INLINE void machine_Machine_write_1(machine_Machine_ptr self, uint64_t addr, uint8_t val);
static NELUA_INLINE void machine_Machine_write_2(machine_Machine_ptr self, uint64_t addr, uint16_t val);
static NELUA_INLINE void machine_Machine_write_3(machine_Machine_ptr self, uint64_t addr, uint32_t val);
static NELUA_INLINE void machine_Machine_write_4(machine_Machine_ptr self, uint64_t addr, uint64_t val);
static uint64_t machine_Machine_handle_syscall(machine_Machine_ptr self, uint64_t code);
static void nelua_print_1(char* a1);
static void nelua_print_2(int64_t a1);
static NELUA_INLINE void machine_Machine_execute(machine_Machine_ptr self, uint32_t inst);
static NELUA_INLINE int64_t nelua_shl_nlint64(int64_t a, int64_t b);
static NELUA_INLINE int64_t nelua_shr_nlint64(int64_t a, int64_t b);
static NELUA_INLINE int64_t nelua_asr_nlint64(int64_t a, int64_t b);
static NELUA_INLINE __int128 nelua_shr_nlint128(__int128 a, __int128 b);
static NELUA_NOINLINE void machine_Machine_run(machine_Machine_ptr self);
static int nelua_argc;
typedef char** nlcstring_arr0_ptr;
static nlcstring_arr0_ptr nelua_argv;
static void nelua_print_3(nlstring a1);
static char* riscvm_filename;
static machine_Machine riscvm_machine;
static NELUA_INLINE nlstring nelua_cstring2string(const char* s);
static int nelua_main(int argc, char** argv);
/* ------------------------------ DEFINITIONS ------------------------------- */
char* nelua_string2cstring(nlstring s) {
return (s.size == 0) ? (char*)"" : (char*)s.data;
}
void nelua_write_stderr(const char* msg, uintptr_t len, bool flush) {
if(len > 0 && msg) {
fwrite(msg, 1, len, stderr);
}
if(flush) {
fwrite("\n", 1, 1, stderr);
fflush(stderr);
}
}
void nelua_abort(void) {
NELUA_UBSAN_UNREACHABLE();
abort();
}
void nelua_panic_string(nlstring s) {
if(s.size > 0) {
nelua_write_stderr((const char*)s.data, s.size, true);
}
nelua_abort();
}
void machine_Machine_loadfile(machine_Machine_ptr self, nlstring filename) {
FILE_ptr fp = fopen(nelua_string2cstring(filename), "rb");
if((!(fp != NULL))) {
nelua_panic_string(((nlstring){(uint8_t*)"failed to open file", 19}));
}
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
fseek(fp, 0, SEEK_SET);
fread((void*)(&(((nluint8_arr134217728_cast*)&self->memory)->a).v[4096]), (size_t)size, 1U, fp);
fclose(fp);
(((nluint64_arr32_cast*)&self->regs)->a).v[2] = 134217728U;
self->pc = 4096;
}
uint32_t machine_Machine_fetch(machine_Machine_ptr self) {
return (*(nluint32_ptr)(&(((nluint8_arr134217728_cast*)&self->memory)->a).v[self->pc]));
}
int8_t machine_Machine_read_1(machine_Machine_ptr self, nlniltype T, uint64_t addr) {
return (int8_t)(*(&(((nluint8_arr134217728_cast*)&self->memory)->a).v[addr]));
}
int16_t machine_Machine_read_2(machine_Machine_ptr self, nlniltype T, uint64_t addr) {
return (int16_t)(*(nluint16_ptr)(&(((nluint8_arr134217728_cast*)&self->memory)->a).v[addr]));
}
int32_t machine_Machine_read_3(machine_Machine_ptr self, nlniltype T, uint64_t addr) {
return (int32_t)(*(nluint32_ptr)(&(((nluint8_arr134217728_cast*)&self->memory)->a).v[addr]));
}
int64_t machine_Machine_read_4(machine_Machine_ptr self, nlniltype T, uint64_t addr) {
return (int64_t)(*(nluint64_ptr)(&(((nluint8_arr134217728_cast*)&self->memory)->a).v[addr]));
}
uint8_t machine_Machine_read_5(machine_Machine_ptr self, nlniltype T, uint64_t addr) {
return (*(&(((nluint8_arr134217728_cast*)&self->memory)->a).v[addr]));
}
uint16_t machine_Machine_read_6(machine_Machine_ptr self, nlniltype T, uint64_t addr) {
return (*(nluint16_ptr)(&(((nluint8_arr134217728_cast*)&self->memory)->a).v[addr]));
}
uint32_t machine_Machine_read_7(machine_Machine_ptr self, nlniltype T, uint64_t addr) {
return (*(nluint32_ptr)(&(((nluint8_arr134217728_cast*)&self->memory)->a).v[addr]));
}
void* machine_Machine_getptr(machine_Machine_ptr self, uint64_t addr) {
return (void*)(&(((nluint8_arr134217728_cast*)&self->memory)->a).v[addr]);
}
void machine_Machine_write_1(machine_Machine_ptr self, uint64_t addr, uint8_t val) {
(*(&(((nluint8_arr134217728_cast*)&self->memory)->a).v[addr])) = val;
}
void machine_Machine_write_2(machine_Machine_ptr self, uint64_t addr, uint16_t val) {
(*(nluint16_ptr)(&(((nluint8_arr134217728_cast*)&self->memory)->a).v[addr])) = val;
}
void machine_Machine_write_3(machine_Machine_ptr self, uint64_t addr, uint32_t val) {
(*(nluint32_ptr)(&(((nluint8_arr134217728_cast*)&self->memory)->a).v[addr])) = val;
}
void machine_Machine_write_4(machine_Machine_ptr self, uint64_t addr, uint64_t val) {
(*(nluint64_ptr)(&(((nluint8_arr134217728_cast*)&self->memory)->a).v[addr])) = val;
}
void nelua_print_1(char* a1) {
fputs(a1 != NULL ? a1 : "(null cstring)", stdout);
fputs("\n", stdout);
fflush(stdout);
}
void nelua_print_2(int64_t a1) {
fprintf(stdout, "%lli", (long long)a1);
fputs("\n", stdout);
fflush(stdout);
}
uint64_t machine_Machine_handle_syscall(machine_Machine_ptr self, uint64_t code) {
switch(code) {
case 10000: {
self->running = false;
self->exitcode = (int64_t)(((nluint64_arr32_cast*)&self->regs)->a).v[10];
break;
}
case 10001: {
nelua_panic_string(((nlstring){(uint8_t*)"aborted!", 8}));
break;
}
case 10007: {
void* dest = machine_Machine_getptr(self, (((nluint64_arr32_cast*)&self->regs)->a).v[10]);
uint64_t c = (((nluint64_arr32_cast*)&self->regs)->a).v[11];
uint64_t len = (((nluint64_arr32_cast*)&self->regs)->a).v[12];
void* res = memset(dest, (int)c, (size_t)len);
return (uint64_t)res;
}
case 10101: {
char* s = (char*)machine_Machine_getptr(self, (((nluint64_arr32_cast*)&self->regs)->a).v[10]);
nelua_print_1(s);
break;
}
case 10102: {
int64_t i = (int64_t)(((nluint64_arr32_cast*)&self->regs)->a).v[10];
nelua_print_2(i);
break;
}
default: {
nelua_panic_string(((nlstring){(uint8_t*)"illegal system call", 19}));
break;
}
}
return 0U;
}
int64_t nelua_shl_nlint64(int64_t a, int64_t b) {
if(NELUA_LIKELY(b >= 0 && b < 64)) {
return ((uint64_t)a) << b;
} else if(NELUA_UNLIKELY(b < 0 && b > -64)) {
return (uint64_t)a >> -b;
} else {
return 0;
}
}
int64_t nelua_shr_nlint64(int64_t a, int64_t b) {
if(NELUA_LIKELY(b >= 0 && b < 64)) {
return (uint64_t)a >> b;
} else if(NELUA_UNLIKELY(b < 0 && b > -64)) {
return (uint64_t)a << -b;
} else {
return 0;
}
}
int64_t nelua_asr_nlint64(int64_t a, int64_t b) {
if(NELUA_LIKELY(b >= 0 && b < 64)) {
return a >> b;
} else if(NELUA_UNLIKELY(b >= 64)) {
return a < 0 ? -1 : 0;
} else if(NELUA_UNLIKELY(b < 0 && b > -64)) {
return a << -b;
} else {
return 0;
}
}
__int128 nelua_shr_nlint128(__int128 a, __int128 b) {
if(NELUA_LIKELY(b >= 0 && b < 128)) {
return (unsigned __int128)a >> b;
} else if(NELUA_UNLIKELY(b < 0 && b > -128)) {
return (unsigned __int128)a << -b;
} else {
return 0;
}
}
void machine_Machine_execute(machine_Machine_ptr self, uint32_t inst) {
uint32_t opcode = ((inst >> 0) & 127);
uint32_t rd = ((inst >> 7) & 31);
uint32_t rs1 = ((inst >> 15) & 31);
uint32_t rs2 = ((inst >> 20) & 31);
switch(opcode) {
case 0x3: {
uint32_t funct3 = ((inst >> 12) & 7);
int64_t imm = (int64_t)(((int32_t)((uint32_t)(int32_t)((inst >> 20) & 4095) << 20)) >> 20);
uint64_t addr = ((((nluint64_arr32_cast*)&self->regs)->a).v[rs1] + (uint64_t)imm);
int64_t val;
switch(funct3) {
case 0x0: {
val = (int64_t)machine_Machine_read_1(self, NELUA_NIL, addr);
break;
}
case 0x1: {
val = (int64_t)machine_Machine_read_2(self, NELUA_NIL, addr);
break;
}
case 0x2: {
val = (int64_t)machine_Machine_read_3(self, NELUA_NIL, addr);
break;
}
case 0x3: {
val = machine_Machine_read_4(self, NELUA_NIL, addr);
break;
}
case 0x4: {
val = (int64_t)machine_Machine_read_5(self, NELUA_NIL, addr);
break;
}
case 0x5: {
val = (int64_t)machine_Machine_read_6(self, NELUA_NIL, addr);
break;
}
case 0x6: {
val = (int64_t)machine_Machine_read_7(self, NELUA_NIL, addr);
break;
}
default: {
nelua_panic_string(((nlstring){(uint8_t*)"illegal load instruction", 24}));
break;
}
}
if(NELUA_LIKELY((rd != 0))) {
(((nluint64_arr32_cast*)&self->regs)->a).v[rd] = (uint64_t)val;
}
break;
}
case 0x23: {
uint32_t funct3 = ((inst >> 12) & 7);
int64_t imm = (int64_t)(((int32_t)((uint32_t)(int32_t)(((inst >> 20) & 4064) | ((inst >> 7) & 31)) << 20)) >> 20);
uint64_t addr = ((((nluint64_arr32_cast*)&self->regs)->a).v[rs1] + (uint64_t)imm);
uint64_t val = (((nluint64_arr32_cast*)&self->regs)->a).v[rs2];
switch(funct3) {
case 0x0: {
machine_Machine_write_1(self, addr, (uint8_t)val);
break;
}
case 0x1: {
machine_Machine_write_2(self, addr, (uint16_t)val);
break;
}
case 0x2: {
machine_Machine_write_3(self, addr, (uint32_t)val);
break;
}
case 0x3: {
machine_Machine_write_4(self, addr, val);
break;
}
default: {
nelua_panic_string(((nlstring){(uint8_t*)"illegal store instruction", 25}));
break;
}
}
break;
}
case 0x13: {
uint32_t funct3 = ((inst >> 12) & 7);
int64_t imm = (int64_t)(((int32_t)((uint32_t)(int32_t)((inst >> 20) & 4095) << 20)) >> 20);
uint32_t shamt = ((inst >> 20) & 63);
int64_t val = (int64_t)(((nluint64_arr32_cast*)&self->regs)->a).v[rs1];
switch(funct3) {
case 0x0: {
val = (val + imm);
break;
}
case 0x1: {
val = nelua_shl_nlint64(val, shamt);
break;
}
case 0x2: {
if((val < imm)) {
val = 1;
} else {
val = 0;
}
break;
}
case 0x3: {
if(((uint64_t)val < (uint64_t)imm)) {
val = 1;
} else {
val = 0;
}
break;
}
case 0x4: {
val = (val ^ imm);
break;
}
case 0x5: {
uint32_t funct6 = ((inst >> 26) & 63);
switch((funct6 >> 4)) {
case 0x0: {
val = nelua_shr_nlint64(val, shamt);
break;
}
case 0x1: {
val = nelua_asr_nlint64(val, shamt);
break;
}
default: {
nelua_panic_string(((nlstring){(uint8_t*)"illegal op-imm shift instruction", 32}));
break;
}
}
break;
}
case 0x6: {
val = (val | imm);
break;
}
case 0x7: {
val = (val & imm);
break;
}
default: {
nelua_panic_string(((nlstring){(uint8_t*)"illegal op-imm instruction", 26}));
break;
}
}
if(NELUA_LIKELY((rd != 0))) {
(((nluint64_arr32_cast*)&self->regs)->a).v[rd] = (uint64_t)val;
}
break;
}
case 0x1b: {
uint32_t funct3 = ((inst >> 12) & 7);
int64_t imm = (int64_t)(((int32_t)((uint32_t)(int32_t)((inst >> 20) & 4095) << 20)) >> 20);
int64_t val = (int64_t)(((nluint64_arr32_cast*)&self->regs)->a).v[rs1];
switch(funct3) {
case 0x0: {
val = (int64_t)(int32_t)(val + imm);
break;
}
case 0x1: {
val = (int64_t)(int32_t)nelua_shl_nlint64(val, imm);
break;
}
case 0x5: {
uint32_t shamt = rs2;
uint32_t funct7 = ((inst >> 25) & 127);
switch((funct7 >> 5)) {
case 0x0: {
val = (int64_t)(int32_t)nelua_shr_nlint64(val, shamt);
break;
}
case 0x1: {
val = (int64_t)(int32_t)nelua_asr_nlint64(val, shamt);
break;
}
default: {
nelua_panic_string(((nlstring){(uint8_t*)"illegal op-imm-32 shift instruction", 35}));
break;
}
}
break;
}
default: {
nelua_panic_string(((nlstring){(uint8_t*)"illegal op-imm-32 instruction", 29}));
break;
}
}
if(NELUA_LIKELY((rd != 0))) {
(((nluint64_arr32_cast*)&self->regs)->a).v[rd] = (uint64_t)val;
}
break;
}
case 0x33: {
uint32_t funct3 = ((inst >> 12) & 7);
uint32_t funct7 = ((inst >> 25) & 127);
int64_t val1 = (int64_t)(((nluint64_arr32_cast*)&self->regs)->a).v[rs1];
int64_t val2 = (int64_t)(((nluint64_arr32_cast*)&self->regs)->a).v[rs2];
int64_t val;
switch(((funct7 << 3) | funct3)) {
case 0x0: {
val = (val1 + val2);
break;
}
case 0x100: {
val = (val1 - val2);
break;
}
case 0x1: {
val = nelua_shl_nlint64(val1, (val2 & 0x1f));
break;
}
case 0x2: {
if((val1 < val2)) {
val = 1;
} else {
val = 0;
}
break;
}
case 0x3: {
if(((uint64_t)val1 < (uint64_t)val2)) {
val = 1;
} else {
val = 0;
}
break;
}
case 0x4: {
val = (val1 ^ val2);
break;
}
case 0x5: {
val = nelua_shr_nlint64(val1, (val2 & 0x1f));
break;
}
case 0x105: {
val = nelua_asr_nlint64(val1, (val2 & 0x1f));
break;
}
case 0x6: {
val = (val1 | val2);
break;
}
case 0x7: {
val = (val1 & val2);
break;
}
case 0x8: {
val = (val1 * val2);
break;
}
case 0x9: {
val = (int64_t)(uint64_t)nelua_shr_nlint128(((__int128)val1 * (__int128)val2), 64);
break;
}
case 0xa: {
val = (int64_t)(uint64_t)nelua_shr_nlint128(((__int128)val1 * (__int128)(uint64_t)val2), 64);
break;
}
case 0xb: {
val = (int64_t)(uint64_t)nelua_shr_nlint128(((__int128)(uint64_t)val1 * (__int128)(uint64_t)val2), 64);
break;
}
case 0xc: {
int64_t dividend = val1;
int64_t divisor = val2;
if(NELUA_UNLIKELY(((dividend == (-9223372036854775807LL-1)) && (divisor == -1)))) {
val = (-9223372036854775807LL-1);
} else if(NELUA_UNLIKELY((divisor == 0))) {
val = -1;
} else {
val = (dividend / divisor);
}
break;
}
case 0xd: {
uint64_t dividend = (uint64_t)val1;
uint64_t divisor = (uint64_t)val2;
if(NELUA_UNLIKELY((divisor == 0))) {
val = -1;
} else {
val = (int64_t)(dividend / divisor);
}
break;
}
case 0xe: {
int64_t dividend = val1;
int64_t divisor = val2;
if(NELUA_UNLIKELY(((dividend == (-9223372036854775807LL-1)) && (divisor == -1)))) {
val = 0;
} else if(NELUA_UNLIKELY((divisor == 0))) {
val = dividend;
} else {
val = (dividend % divisor);
}
break;
}
case 0xf: {
uint64_t dividend = (uint64_t)val1;
uint64_t divisor = (uint64_t)val2;
if(NELUA_UNLIKELY((divisor == 0))) {
val = (int64_t)dividend;
} else {
val = (int64_t)(dividend % divisor);
}
break;
}
default: {
nelua_panic_string(((nlstring){(uint8_t*)"illegal op instruction", 22}));
break;
}
}
if(NELUA_LIKELY((rd != 0))) {
(((nluint64_arr32_cast*)&self->regs)->a).v[rd] = (uint64_t)val;
}
break;
}
case 0x3b: {
uint32_t funct3 = ((inst >> 12) & 7);
uint32_t funct7 = ((inst >> 25) & 127);
int64_t val1 = (int64_t)(((nluint64_arr32_cast*)&self->regs)->a).v[rs1];
int64_t val2 = (int64_t)(((nluint64_arr32_cast*)&self->regs)->a).v[rs2];
int64_t val;
switch(((funct7 << 3) | funct3)) {
case 0x0: {
val = (int64_t)(int32_t)(val1 + val2);
break;
}
case 0x100: {
val = (int64_t)(int32_t)(val1 - val2);
break;
}
case 0x1: {
val = (int64_t)(int32_t)nelua_shl_nlint64(val1, (val2 & 0x1f));
break;
}
case 0x5: {
val = (int64_t)(int32_t)nelua_shr_nlint64(val1, (val2 & 0x1f));
break;
}
case 0x105: {
val = (int64_t)(int32_t)nelua_asr_nlint64(val1, (val2 & 0x1f));
break;
}
case 0x8: {
val = (int64_t)((int32_t)val1 * (int32_t)val2);
break;
}
case 0xc: {
int32_t dividend = (int32_t)val1;
int32_t divisor = (int32_t)val2;
if(NELUA_UNLIKELY(((dividend == (-2147483647-1)) && (divisor == -1)))) {
val = -2147483648LL;
} else if(NELUA_UNLIKELY((divisor == 0))) {
val = -1;
} else {
val = (int64_t)(dividend / divisor);
}
break;
}
case 0xd: {
uint32_t dividend = (uint32_t)val1;
uint32_t divisor = (uint32_t)val2;
if(NELUA_UNLIKELY((divisor == 0))) {
val = -1;
} else {
val = (int64_t)(int32_t)(dividend / divisor);
}
break;
}
case 0xe: {
int32_t dividend = (int32_t)val1;
int32_t divisor = (int32_t)val2;
if(NELUA_UNLIKELY(((dividend == (-2147483647-1)) && (divisor == -1)))) {
val = 0;
} else if(NELUA_UNLIKELY((divisor == 0))) {
val = (int64_t)dividend;
} else {
val = (int64_t)(dividend % divisor);
}
break;
}
case 0xf: {
uint32_t dividend = (uint32_t)val1;
uint32_t divisor = (uint32_t)val2;
if(NELUA_UNLIKELY((divisor == 0))) {
val = (int64_t)(int32_t)dividend;
} else {
val = (int64_t)(int32_t)(dividend % divisor);
}
break;
}
default: {
nelua_panic_string(((nlstring){(uint8_t*)"illegal op-32 instruction", 25}));
break;
}
}
if(NELUA_LIKELY((rd != 0))) {
(((nluint64_arr32_cast*)&self->regs)->a).v[rd] = (uint64_t)val;
}
break;
}
case 0x37: {
int64_t imm = (int64_t)(((int32_t)((uint32_t)(int32_t)((inst << 0) & 4294963200LL) << 0)) >> 0);
if(NELUA_LIKELY((rd != 0))) {
(((nluint64_arr32_cast*)&self->regs)->a).v[rd] = (uint64_t)imm;
}
break;
}
case 0x17: {
int64_t imm = (int64_t)(((int32_t)((uint32_t)(int32_t)((inst << 0) & 4294963200LL) << 0)) >> 0);
if(NELUA_LIKELY((rd != 0))) {
(((nluint64_arr32_cast*)&self->regs)->a).v[rd] = (uint64_t)(self->pc + imm);
}
break;
}
case 0x6f: {
int64_t imm = (int64_t)(((int32_t)((uint32_t)(int32_t)(((((inst >> 11) & 1048576) | ((inst >> 20) & 2046)) | ((inst >> 9) & 2048)) | ((inst << 0) & 1044480)) << 11)) >> 11);
if(NELUA_LIKELY((rd != 0))) {
(((nluint64_arr32_cast*)&self->regs)->a).v[rd] = (uint64_t)(self->pc + 4);
}
self->pc = (self->pc + imm);
return;
}
case 0x67: {
int64_t imm = (int64_t)(((int32_t)((uint32_t)(int32_t)((inst >> 20) & 4095) << 20)) >> 20);
int64_t pc = (self->pc + 4);
self->pc = ((int64_t)((((nluint64_arr32_cast*)&self->regs)->a).v[rs1] + imm) & -2);
if(NELUA_UNLIKELY((rd != 0))) {
(((nluint64_arr32_cast*)&self->regs)->a).v[rd] = (uint64_t)pc;
}
return;
}
case 0x63: {
uint32_t funct3 = ((inst >> 12) & 7);
int64_t imm = (int64_t)(((int32_t)((uint32_t)(int32_t)(((((inst >> 19) & 4096) | ((inst >> 20) & 2016)) | ((inst >> 7) & 30)) | ((inst << 4) & 2048)) << 19)) >> 19);
uint64_t val1 = (((nluint64_arr32_cast*)&self->regs)->a).v[rs1];
uint64_t val2 = (((nluint64_arr32_cast*)&self->regs)->a).v[rs2];
bool cond;
switch(funct3) {
case 0x0: {
cond = (val1 == val2);
break;
}
case 0x1: {
cond = (val1 != val2);
break;
}
case 0x4: {
cond = ((int64_t)val1 < (int64_t)val2);
break;
}
case 0x5: {
cond = ((int64_t)val1 >= (int64_t)val2);
break;
}
case 0x6: {
cond = (val1 < val2);
break;
}
case 0x7: {
cond = (val1 >= val2);
break;
}
default: {
nelua_panic_string(((nlstring){(uint8_t*)"illegal branch instruction", 26}));
break;
}
}
if(cond) {
self->pc = (self->pc + imm);
return;
}
break;
}
case 0xf: {
break;
}
case 0x73: {
uint32_t funct11 = ((inst >> 20) & 4095);
switch(funct11) {
case 0x0: {
uint64_t code = (((nluint64_arr32_cast*)&self->regs)->a).v[17];
(((nluint64_arr32_cast*)&self->regs)->a).v[10] = machine_Machine_handle_syscall(self, code);
break;
}
case 0x1: {
self->exitcode = -1;
self->running = false;
break;
}
default: {
nelua_panic_string(((nlstring){(uint8_t*)"illegal system instruction", 26}));
break;
}
}
break;
}
default: {
nelua_panic_string(((nlstring){(uint8_t*)"illegal instruction", 19}));
break;
}
}
self->pc = (self->pc + 4);
}
void machine_Machine_run(machine_Machine_ptr self) {
self->running = true;
while(NELUA_LIKELY(self->running)) {
uint32_t inst = machine_Machine_fetch(self);
machine_Machine_execute(self, inst);
}
}
void nelua_print_3(nlstring a1) {
if(a1.size > 0) {
fwrite(a1.data, 1, a1.size, stdout);
}
fputs("\n", stdout);
fflush(stdout);
}
nlstring nelua_cstring2string(const char* s) {
if(s == NULL) {
return (nlstring){0};
}
uintptr_t size = strlen(s);
if(size == 0) {
return (nlstring){0};
}
return (nlstring){(uint8_t*)s, size};
}
int nelua_main(int argc, char** argv) {
nelua_argc = argc;
nelua_argv = argv;
if((nelua_argc != 2)) {
nelua_print_3(((nlstring){(uint8_t*)"please supply a RV64I program to run!", 37}));
exit(1);
}
riscvm_filename = nelua_argv[1];
machine_Machine_loadfile((&riscvm_machine), nelua_cstring2string(riscvm_filename));
machine_Machine_run((&riscvm_machine));
exit((int)riscvm_machine.exitcode);
return 0;
}
int main(int argc, char** argv) {
return nelua_main(argc, argv);
}