forked from cksystemsteaching/selfie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selfie.c
10123 lines (7690 loc) · 285 KB
/
selfie.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
/*
Copyright (c) 2015-2018, the Selfie Project authors. All rights reserved.
Please see the AUTHORS file for details. Use of this source code is
governed by a BSD license that can be found in the LICENSE file.
Selfie is a project of the Computational Systems Group at the
Department of Computer Sciences of the University of Salzburg
in Austria. For further information and code please refer to:
http://selfie.cs.uni-salzburg.at
The Selfie Project provides an educational platform for teaching
undergraduate and graduate students the design and implementation
of programming languages and runtime systems. The focus is on the
construction of compilers, libraries, operating systems, and even
virtual machine monitors. The common theme is to identify and
resolve self-reference in systems code which is seen as the key
challenge when teaching systems engineering, hence the name.
Selfie is a self-contained 32-bit, 10-KLOC C implementation of:
1. a self-compiling compiler called starc that compiles
a tiny but still fast subset of C called C Star (C*) to
a tiny and easy-to-teach subset of RISC-V called RISC-U,
2. a self-executing emulator called mipster that executes
RISC-U code including itself when compiled with starc,
3. a self-hosting hypervisor called hypster that provides
RISC-U virtual machines that can host all of selfie,
that is, starc, mipster, and hypster itself,
4. a prototypical symbolic execution engine called monster
that executes RISC-U code symbolically,
5. a simple SAT solver that reads CNF DIMACS files, and
6. a tiny C* library called libcstar utilized by selfie.
Selfie is implemented in a single (!) file and kept minimal for simplicity.
There is also a simple in-memory linker, a RISC-U disassembler, a profiler,
and a debugger with replay as well as minimal operating system support in
the form of RISC-V system calls built into the emulator.
C* is a tiny Turing-complete subset of C that includes dereferencing
(the * operator) but excludes composite data types, bitwise and Boolean
operators, and many other features. There are only unsigned 32-bit
integers and 32-bit pointers as well as character and string literals.
This choice turns out to be helpful for students to understand the
true role of composite data types such as arrays and records.
Bitwise operations are implemented in libcstar using unsigned integer
arithmetics helping students better understand arithmetic operators.
C* is supposed to be close to the minimum necessary for implementing
a self-compiling, single-pass, recursive-descent compiler. C* can be
taught in one to two weeks of classes depending on student background.
The compiler can readily be extended to compile features missing in C*
and to improve performance of the generated code. The compiler generates
RISC-U executables in ELF format that are compatible with the official
RISC-V toolchain. The mipster emulator can execute RISC-U executables
loaded from file but also from memory immediately after code generation
without going through the file system.
RISC-U is a tiny Turing-complete subset of the RISC-V instruction set.
It only features unsigned 32-bit integer arithmetic, word memory,
and simple control-flow instructions but neither bitwise nor byte- and
word-level instructions. RISC-U can be taught in one week of classes.
The emulator implements minimal operating system support that is meant
to be extended by students, first as part of the emulator, and then
ported to run on top of it, similar to an actual operating system or
virtual machine monitor. The fact that the emulator can execute itself
helps exposing the self-referential nature of that challenge. In fact,
selfie goes one step further by implementing microkernel functionality
as part of the emulator and a hypervisor that can run as part of the
emulator as well as on top of it, all with the same code.
Selfie is the result of many years of teaching systems engineering.
The design of the compiler is inspired by the Oberon compiler of
Professor Niklaus Wirth from ETH Zurich. RISC-U is inspired by the
RISC-V community around Professor David Patterson from UC Berkeley.
The design of the hypervisor is inspired by microkernels of
Professor Jochen Liedtke from University of Karlsruhe.
*/
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// --------------------- L I B R A R Y ---------------------
// -----------------------------------------------------------------
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ----------------------- BUILTIN PROCEDURES ----------------------
// -----------------------------------------------------------------
// selfie bootstraps int to uint32_t!
void exit(int code);
uint32_t read(uint32_t fd, uint32_t* buffer, uint32_t bytes_to_read);
uint32_t write(uint32_t fd, uint32_t* buffer, uint32_t bytes_to_write);
uint32_t open(uint32_t* filename, uint32_t flags, uint32_t mode);
// selfie bootstraps void* and unsigned long to uint32_t* and uint32_t, respectively!
void* malloc(unsigned long);
// -----------------------------------------------------------------
// ----------------------- LIBRARY PROCEDURES ----------------------
// -----------------------------------------------------------------
void init_library();
void reset_library();
uint32_t two_to_the_power_of(uint32_t p);
uint32_t ten_to_the_power_of(uint32_t p);
uint32_t left_shift(uint32_t n, uint32_t b);
uint32_t right_shift(uint32_t n, uint32_t b);
uint32_t get_bits(uint32_t n, uint32_t i, uint32_t b);
uint32_t absolute(uint32_t n);
uint32_t signed_less_than(uint32_t a, uint32_t b);
uint32_t signed_division(uint32_t a, uint32_t b);
uint32_t is_signed_integer(uint32_t n, uint32_t b);
uint32_t sign_extend(uint32_t n, uint32_t b);
uint32_t sign_shrink(uint32_t n, uint32_t b);
uint32_t load_character(uint32_t* s, uint32_t i);
uint32_t* store_character(uint32_t* s, uint32_t i, uint32_t c);
uint32_t string_length(uint32_t* s);
uint32_t* string_copy(uint32_t* s);
void string_reverse(uint32_t* s);
uint32_t string_compare(uint32_t* s, uint32_t* t);
uint32_t atoi(uint32_t* s);
uint32_t* itoa(uint32_t n, uint32_t* s, uint32_t b, uint32_t a);
uint32_t fixed_point_ratio(uint32_t a, uint32_t b, uint32_t f);
uint32_t fixed_point_percentage(uint32_t r, uint32_t f);
void put_character(uint32_t c);
void print(uint32_t* s);
void println();
void print_character(uint32_t c);
void print_string(uint32_t* s);
void print_integer(uint32_t n);
void unprint_integer(uint32_t n);
void print_hexadecimal(uint32_t n, uint32_t a);
void print_octal(uint32_t n, uint32_t a);
void print_binary(uint32_t n, uint32_t a);
uint32_t print_format0(uint32_t* s, uint32_t i);
uint32_t print_format1(uint32_t* s, uint32_t i, uint32_t* a);
void printf1(uint32_t* s, uint32_t* a1);
void printf2(uint32_t* s, uint32_t* a1, uint32_t* a2);
void printf3(uint32_t* s, uint32_t* a1, uint32_t* a2, uint32_t* a3);
void printf4(uint32_t* s, uint32_t* a1, uint32_t* a2, uint32_t* a3, uint32_t* a4);
void printf5(uint32_t* s, uint32_t* a1, uint32_t* a2, uint32_t* a3, uint32_t* a4, uint32_t* a5);
void printf6(uint32_t* s, uint32_t* a1, uint32_t* a2, uint32_t* a3, uint32_t* a4, uint32_t* a5, uint32_t* a6);
uint32_t round_up(uint32_t n, uint32_t m);
uint32_t* smalloc(uint32_t size);
uint32_t* zalloc(uint32_t size);
// ------------------------ GLOBAL CONSTANTS -----------------------
uint32_t CHAR_EOF = -1; // end of file
uint32_t CHAR_BACKSPACE = 8; // ASCII code 8 = backspace
uint32_t CHAR_TAB = 9; // ASCII code 9 = tabulator
uint32_t CHAR_LF = 10; // ASCII code 10 = line feed
uint32_t CHAR_CR = 13; // ASCII code 13 = carriage return
uint32_t CHAR_SPACE = ' ';
uint32_t CHAR_UNDERSCORE = '_';
uint32_t CHAR_SINGLEQUOTE = 39; // ASCII code 39 = '
uint32_t CHAR_DOUBLEQUOTE = '"';
uint32_t CHAR_COMMA = ',';
uint32_t CHAR_SEMICOLON = ';';
uint32_t CHAR_LPARENTHESIS = '(';
uint32_t CHAR_RPARENTHESIS = ')';
uint32_t CHAR_LBRACE = '{';
uint32_t CHAR_RBRACE = '}';
uint32_t CHAR_PLUS = '+';
uint32_t CHAR_DASH = '-';
uint32_t CHAR_ASTERISK = '*';
uint32_t CHAR_SLASH = '/';
uint32_t CHAR_PERCENTAGE = '%';
uint32_t CHAR_EQUAL = '=';
uint32_t CHAR_EXCLAMATION = '!';
uint32_t CHAR_LT = '<';
uint32_t CHAR_GT = '>';
uint32_t CHAR_BACKSLASH = 92; // ASCII code 92 = backslash
uint32_t CPUBITWIDTH = 32;
uint32_t SIZEOFUINT32 = 4; // must be the same as REGISTERSIZE
uint32_t SIZEOFUINT32STAR = 4; // must be the same as REGISTERSIZE
uint32_t* power_of_two_table;
uint32_t INT32_MAX; // maximum numerical value of a signed 32-bit integer
uint32_t INT32_MIN; // minimum numerical value of a signed 32-bit integer
uint32_t UINT32_MAX; // maximum numerical value of an unsigned 32-bit integer
uint32_t MAX_FILENAME_LENGTH = 128;
uint32_t* character_buffer; // buffer for reading and writing characters
uint32_t* integer_buffer; // buffer for printing integers
uint32_t* filename_buffer; // buffer for opening files
uint32_t* binary_buffer; // buffer for binary I/O
// flags for opening read-only files
// LINUX: 0 = 0x0000 = O_RDONLY (0x0000)
// MAC: 0 = 0x0000 = O_RDONLY (0x0000)
// WINDOWS: 32768 = 0x8000 = _O_BINARY (0x8000) | _O_RDONLY (0x0000)
// since LINUX/MAC do not seem to mind about _O_BINARY set
// we use the WINDOWS flags as default
uint32_t O_RDONLY = 32768;
// flags for opening write-only files
// MAC: 1537 = 0x0601 = O_CREAT (0x0200) | O_TRUNC (0x0400) | O_WRONLY (0x0001)
uint32_t MAC_O_CREAT_TRUNC_WRONLY = 1537;
// LINUX: 577 = 0x0241 = O_CREAT (0x0040) | O_TRUNC (0x0200) | O_WRONLY (0x0001)
uint32_t LINUX_O_CREAT_TRUNC_WRONLY = 577;
// WINDOWS: 33537 = 0x8301 = _O_BINARY (0x8000) | _O_CREAT (0x0100) | _O_TRUNC (0x0200) | _O_WRONLY (0x0001)
uint32_t WINDOWS_O_BINARY_CREAT_TRUNC_WRONLY = 33537;
// flags for rw-r--r-- file permissions
// 420 = 00644 = S_IRUSR (00400) | S_IWUSR (00200) | S_IRGRP (00040) | S_IROTH (00004)
// these flags seem to be working for LINUX, MAC, and WINDOWS
uint32_t S_IRUSR_IWUSR_IRGRP_IROTH = 420;
// ------------------------ GLOBAL VARIABLES -----------------------
uint32_t number_of_written_characters = 0;
uint32_t* output_name = (uint32_t*) 0;
uint32_t output_fd = 1; // 1 is file descriptor of standard output
// ------------------------- INITIALIZATION ------------------------
void init_library() {
uint32_t i;
// powers of two table with CPUBITWIDTH entries for 2^0 to 2^(CPUBITWIDTH - 1)
power_of_two_table = smalloc(CPUBITWIDTH * SIZEOFUINT32);
*power_of_two_table = 1; // 2^0 == 1
i = 1;
while (i < CPUBITWIDTH) {
// compute powers of two incrementally using this recurrence relation
*(power_of_two_table + i) = *(power_of_two_table + (i - 1)) * 2;
i = i + 1;
}
// compute 32-bit unsigned integer range using signed integer arithmetic
UINT32_MAX = -1;
// compute 32-bit signed integer range using unsigned integer arithmetic
INT32_MAX = two_to_the_power_of(CPUBITWIDTH - 1) - 1;
INT32_MIN = INT32_MAX + 1;
// allocate and touch to make sure memory is mapped for read calls
character_buffer = smalloc(SIZEOFUINT32);
*character_buffer = 0;
// accommodate at least CPUBITWIDTH numbers for itoa, no mapping needed
integer_buffer = smalloc(CPUBITWIDTH + 1);
// does not need to be mapped
filename_buffer = smalloc(MAX_FILENAME_LENGTH);
// allocate and touch to make sure memory is mapped for read calls
binary_buffer = smalloc(SIZEOFUINT32);
*binary_buffer = 0;
}
void reset_library() {
number_of_written_characters = 0;
}
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// --------------------- C O M P I L E R ---------------------
// -----------------------------------------------------------------
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ---------------------------- SCANNER ----------------------------
// -----------------------------------------------------------------
void init_scanner();
void reset_scanner();
void print_symbol(uint32_t symbol);
void print_line_number(uint32_t* message, uint32_t line);
void syntax_error_message(uint32_t* message);
void syntax_error_character(uint32_t character);
void syntax_error_identifier(uint32_t* expected);
void get_character();
uint32_t is_character_new_line();
uint32_t is_character_whitespace();
uint32_t find_next_character();
uint32_t is_character_letter();
uint32_t is_character_digit();
uint32_t is_character_letter_or_digit_or_underscore();
uint32_t is_character_not_double_quote_or_new_line_or_eof();
uint32_t identifier_string_match(uint32_t string_index);
uint32_t identifier_or_keyword();
void get_symbol();
void handle_escape_sequence();
// ------------------------ GLOBAL CONSTANTS -----------------------
uint32_t SYM_EOF = -1; // end of file
// C* symbols
uint32_t SYM_INTEGER = 0; // integer
uint32_t SYM_CHARACTER = 1; // character
uint32_t SYM_STRING = 2; // string
uint32_t SYM_IDENTIFIER = 3; // identifier
uint32_t SYM_UINT32 = 4; // uint32_t
uint32_t SYM_IF = 5; // if
uint32_t SYM_ELSE = 6; // else
uint32_t SYM_VOID = 7; // void
uint32_t SYM_RETURN = 8; // return
uint32_t SYM_WHILE = 9; // while
uint32_t SYM_COMMA = 10; // ,
uint32_t SYM_SEMICOLON = 11; // ;
uint32_t SYM_LPARENTHESIS = 12; // (
uint32_t SYM_RPARENTHESIS = 13; // )
uint32_t SYM_LBRACE = 14; // {
uint32_t SYM_RBRACE = 15; // }
uint32_t SYM_PLUS = 16; // +
uint32_t SYM_MINUS = 17; // -
uint32_t SYM_ASTERISK = 18; // *
uint32_t SYM_DIV = 19; // /
uint32_t SYM_MOD = 20; // %
uint32_t SYM_ASSIGN = 21; // =
uint32_t SYM_EQUALITY = 22; // ==
uint32_t SYM_NOTEQ = 23; // !=
uint32_t SYM_LT = 24; // <
uint32_t SYM_LEQ = 25; // <=
uint32_t SYM_GT = 26; // >
uint32_t SYM_GEQ = 27; // >=
// symbols for bootstrapping
uint32_t SYM_INT = 28; // int
uint32_t SYM_CHAR = 29; // char
uint32_t SYM_UNSIGNED = 30; // unsigned
uint32_t* SYMBOLS; // strings representing symbols
uint32_t MAX_IDENTIFIER_LENGTH = 64; // maximum number of characters in an identifier
uint32_t MAX_INTEGER_LENGTH = 20; // maximum number of characters in an unsigned integer
uint32_t MAX_STRING_LENGTH = 128; // maximum number of characters in a string
// ------------------------ GLOBAL VARIABLES -----------------------
uint32_t line_number = 1; // current line number for error reporting
uint32_t* identifier = (uint32_t*) 0; // stores scanned identifier as string
uint32_t* integer = (uint32_t*) 0; // stores scanned integer as string
uint32_t* string = (uint32_t*) 0; // stores scanned string
uint32_t literal = 0; // stores numerical value of scanned integer or character
uint32_t integer_is_signed = 0; // enforce INT32_MIN limit if '-' was scanned before
uint32_t character; // most recently read character
uint32_t number_of_read_characters = 0;
uint32_t symbol; // most recently recognized symbol
uint32_t number_of_ignored_characters = 0;
uint32_t number_of_comments = 0;
uint32_t number_of_scanned_symbols = 0;
uint32_t* source_name = (uint32_t*) 0; // name of source file
uint32_t source_fd = 0; // file descriptor of open source file
// ------------------------- INITIALIZATION ------------------------
void init_scanner () {
SYMBOLS = smalloc((SYM_UNSIGNED + 1) * SIZEOFUINT32STAR);
*(SYMBOLS + SYM_INTEGER) = (uint32_t) "integer";
*(SYMBOLS + SYM_CHARACTER) = (uint32_t) "character";
*(SYMBOLS + SYM_STRING) = (uint32_t) "string";
*(SYMBOLS + SYM_IDENTIFIER) = (uint32_t) "identifier";
*(SYMBOLS + SYM_UINT32) = (uint32_t) "uint32_t";
*(SYMBOLS + SYM_IF) = (uint32_t) "if";
*(SYMBOLS + SYM_ELSE) = (uint32_t) "else";
*(SYMBOLS + SYM_VOID) = (uint32_t) "void";
*(SYMBOLS + SYM_RETURN) = (uint32_t) "return";
*(SYMBOLS + SYM_WHILE) = (uint32_t) "while";
*(SYMBOLS + SYM_COMMA) = (uint32_t) ",";
*(SYMBOLS + SYM_SEMICOLON) = (uint32_t) ";";
*(SYMBOLS + SYM_LPARENTHESIS) = (uint32_t) "(";
*(SYMBOLS + SYM_RPARENTHESIS) = (uint32_t) ")";
*(SYMBOLS + SYM_LBRACE) = (uint32_t) "{";
*(SYMBOLS + SYM_RBRACE) = (uint32_t) "}";
*(SYMBOLS + SYM_PLUS) = (uint32_t) "+";
*(SYMBOLS + SYM_MINUS) = (uint32_t) "-";
*(SYMBOLS + SYM_ASTERISK) = (uint32_t) "*";
*(SYMBOLS + SYM_DIV) = (uint32_t) "/";
*(SYMBOLS + SYM_MOD) = (uint32_t) "%";
*(SYMBOLS + SYM_ASSIGN) = (uint32_t) "=";
*(SYMBOLS + SYM_EQUALITY) = (uint32_t) "==";
*(SYMBOLS + SYM_NOTEQ) = (uint32_t) "!=";
*(SYMBOLS + SYM_LT) = (uint32_t) "<";
*(SYMBOLS + SYM_LEQ) = (uint32_t) "<=";
*(SYMBOLS + SYM_GT) = (uint32_t) ">";
*(SYMBOLS + SYM_GEQ) = (uint32_t) ">=";
*(SYMBOLS + SYM_INT) = (uint32_t) "int";
*(SYMBOLS + SYM_CHAR) = (uint32_t) "char";
*(SYMBOLS + SYM_UNSIGNED) = (uint32_t) "unsigned";
character = CHAR_EOF;
symbol = SYM_EOF;
}
void reset_scanner() {
line_number = 1;
number_of_read_characters = 0;
get_character();
number_of_ignored_characters = 0;
number_of_comments = 0;
number_of_scanned_symbols = 0;
}
// -----------------------------------------------------------------
// ------------------------- SYMBOL TABLE --------------------------
// -----------------------------------------------------------------
void reset_symbol_tables();
uint32_t hash(uint32_t* key);
void create_symbol_table_entry(uint32_t which, uint32_t* string, uint32_t line, uint32_t class, uint32_t type, uint32_t value, uint32_t address);
uint32_t* search_symbol_table(uint32_t* entry, uint32_t* string, uint32_t class);
uint32_t* search_global_symbol_table(uint32_t* string, uint32_t class);
uint32_t* get_scoped_symbol_table_entry(uint32_t* string, uint32_t class);
uint32_t is_undefined_procedure(uint32_t* entry);
uint32_t report_undefined_procedures();
// symbol table entry:
// +----+---------+
// | 0 | next | pointer to next entry
// | 1 | string | identifier string, big integer as string, string literal
// | 2 | line# | source line number
// | 3 | class | VARIABLE, BIGINT, STRING, PROCEDURE
// | 4 | type | UINT32_T, UINT32STAR_T, VOID_T
// | 5 | value | VARIABLE: initial value
// | 6 | address | VARIABLE, BIGINT, STRING: offset, PROCEDURE: address
// | 7 | scope | REG_GP, REG_FP
// +----+---------+
uint32_t* get_next_entry(uint32_t* entry) { return (uint32_t*) *entry; }
uint32_t* get_string(uint32_t* entry) { return (uint32_t*) *(entry + 1); }
uint32_t get_line_number(uint32_t* entry) { return *(entry + 2); }
uint32_t get_class(uint32_t* entry) { return *(entry + 3); }
uint32_t get_type(uint32_t* entry) { return *(entry + 4); }
uint32_t get_value(uint32_t* entry) { return *(entry + 5); }
uint32_t get_address(uint32_t* entry) { return *(entry + 6); }
uint32_t get_scope(uint32_t* entry) { return *(entry + 7); }
void set_next_entry(uint32_t* entry, uint32_t* next) { *entry = (uint32_t) next; }
void set_string(uint32_t* entry, uint32_t* identifier) { *(entry + 1) = (uint32_t) identifier; }
void set_line_number(uint32_t* entry, uint32_t line) { *(entry + 2) = line; }
void set_class(uint32_t* entry, uint32_t class) { *(entry + 3) = class; }
void set_type(uint32_t* entry, uint32_t type) { *(entry + 4) = type; }
void set_value(uint32_t* entry, uint32_t value) { *(entry + 5) = value; }
void set_address(uint32_t* entry, uint32_t address) { *(entry + 6) = address; }
void set_scope(uint32_t* entry, uint32_t scope) { *(entry + 7) = scope; }
// ------------------------ GLOBAL CONSTANTS -----------------------
// classes
uint32_t VARIABLE = 1;
uint32_t BIGINT = 2;
uint32_t STRING = 3;
uint32_t PROCEDURE = 4;
// types
uint32_t UINT32_T = 1;
uint32_t UINT32STAR_T = 2;
uint32_t VOID_T = 3;
// symbol tables
uint32_t GLOBAL_TABLE = 1;
uint32_t LOCAL_TABLE = 2;
uint32_t LIBRARY_TABLE = 3;
// hash table size for global symbol table
uint32_t HASH_TABLE_SIZE = 1024;
// ------------------------ GLOBAL VARIABLES -----------------------
// table pointers
uint32_t* global_symbol_table = (uint32_t*) 0;
uint32_t* local_symbol_table = (uint32_t*) 0;
uint32_t* library_symbol_table = (uint32_t*) 0;
uint32_t number_of_global_variables = 0;
uint32_t number_of_procedures = 0;
uint32_t number_of_strings = 0;
uint32_t number_of_searches = 0;
uint32_t total_search_time = 0;
// ------------------------- INITIALIZATION ------------------------
void reset_symbol_tables() {
global_symbol_table = (uint32_t*) zalloc(HASH_TABLE_SIZE * SIZEOFUINT32STAR);
local_symbol_table = (uint32_t*) 0;
library_symbol_table = (uint32_t*) 0;
number_of_global_variables = 0;
number_of_procedures = 0;
number_of_strings = 0;
number_of_searches = 0;
total_search_time = 0;
}
// -----------------------------------------------------------------
// ---------------------------- PARSER -----------------------------
// -----------------------------------------------------------------
void reset_parser();
uint32_t is_not_rbrace_or_eof();
uint32_t is_expression();
uint32_t is_literal();
uint32_t is_star_or_div_or_modulo();
uint32_t is_plus_or_minus();
uint32_t is_comparison();
uint32_t look_for_factor();
uint32_t look_for_statement();
uint32_t look_for_type();
void save_temporaries();
void restore_temporaries(uint32_t number_of_temporaries);
void syntax_error_symbol(uint32_t expected);
void syntax_error_unexpected();
void print_type(uint32_t type);
void type_warning(uint32_t expected, uint32_t found);
uint32_t* get_variable_or_big_int(uint32_t* variable, uint32_t class);
void load_upper_base_address(uint32_t* entry);
uint32_t load_variable_or_big_int(uint32_t* variable, uint32_t class);
void load_integer(uint32_t value);
void load_string(uint32_t* string);
uint32_t help_call_codegen(uint32_t* entry, uint32_t* procedure);
void help_procedure_prologue(uint32_t number_of_local_variable_bytes);
void help_procedure_epilogue(uint32_t number_of_parameter_bytes);
uint32_t compile_call(uint32_t* procedure);
uint32_t compile_factor();
uint32_t compile_term();
uint32_t compile_simple_expression();
uint32_t compile_expression();
void compile_while();
void compile_if();
void compile_return();
void compile_statement();
uint32_t compile_type();
void compile_variable(uint32_t offset);
uint32_t compile_initialization(uint32_t type);
void compile_procedure(uint32_t* procedure, uint32_t type);
void compile_cstar();
// ------------------------ GLOBAL VARIABLES -----------------------
uint32_t allocated_temporaries = 0; // number of allocated temporaries
uint32_t allocated_memory = 0; // number of bytes for global variables and strings
uint32_t return_branches = 0; // fixup chain for return statements
uint32_t return_type = 0; // return type of currently parsed procedure
uint32_t number_of_calls = 0;
uint32_t number_of_assignments = 0;
uint32_t number_of_while = 0;
uint32_t number_of_if = 0;
uint32_t number_of_return = 0;
// ------------------------- INITIALIZATION ------------------------
void reset_parser() {
number_of_calls = 0;
number_of_assignments = 0;
number_of_while = 0;
number_of_if = 0;
number_of_return = 0;
get_symbol();
}
// -----------------------------------------------------------------
// ---------------------- MACHINE CODE LIBRARY ---------------------
// -----------------------------------------------------------------
void emit_round_up(uint32_t reg, uint32_t m);
void emit_left_shift_by(uint32_t reg, uint32_t b);
void emit_program_entry();
void emit_bootstrapping();
// -----------------------------------------------------------------
// --------------------------- COMPILER ----------------------------
// -----------------------------------------------------------------
void selfie_compile();
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ------------------- I N T E R F A C E -------------------
// -----------------------------------------------------------------
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ---------------------------- REGISTER ---------------------------
// -----------------------------------------------------------------
void init_register();
uint32_t* get_register_name(uint32_t reg);
void print_register_name(uint32_t reg);
// ------------------------ GLOBAL CONSTANTS -----------------------
uint32_t NUMBEROFREGISTERS = 32;
uint32_t NUMBEROFTEMPORARIES = 7;
uint32_t REG_ZR = 0;
uint32_t REG_RA = 1;
uint32_t REG_SP = 2;
uint32_t REG_GP = 3;
uint32_t REG_TP = 4;
uint32_t REG_T0 = 5;
uint32_t REG_T1 = 6;
uint32_t REG_T2 = 7;
uint32_t REG_FP = 8;
uint32_t REG_S1 = 9;
uint32_t REG_A0 = 10;
uint32_t REG_A1 = 11;
uint32_t REG_A2 = 12;
uint32_t REG_A3 = 13;
uint32_t REG_A4 = 14;
uint32_t REG_A5 = 15;
uint32_t REG_A6 = 16;
uint32_t REG_A7 = 17;
uint32_t REG_S2 = 18;
uint32_t REG_S3 = 19;
uint32_t REG_S4 = 20;
uint32_t REG_S5 = 21;
uint32_t REG_S6 = 22;
uint32_t REG_S7 = 23;
uint32_t REG_S8 = 24;
uint32_t REG_S9 = 25;
uint32_t REG_S10 = 26;
uint32_t REG_S11 = 27;
uint32_t REG_T3 = 28;
uint32_t REG_T4 = 29;
uint32_t REG_T5 = 30;
uint32_t REG_T6 = 31;
uint32_t* REGISTERS; // strings representing registers
// ------------------------- INITIALIZATION ------------------------
void init_register() {
REGISTERS = smalloc(NUMBEROFREGISTERS * SIZEOFUINT32STAR);
*(REGISTERS + REG_ZR) = (uint32_t) "$zero";
*(REGISTERS + REG_RA) = (uint32_t) "$ra";
*(REGISTERS + REG_SP) = (uint32_t) "$sp";
*(REGISTERS + REG_GP) = (uint32_t) "$gp";
*(REGISTERS + REG_TP) = (uint32_t) "$tp";
*(REGISTERS + REG_T0) = (uint32_t) "$t0";
*(REGISTERS + REG_T1) = (uint32_t) "$t1";
*(REGISTERS + REG_T2) = (uint32_t) "$t2";
*(REGISTERS + REG_FP) = (uint32_t) "$fp";
*(REGISTERS + REG_S1) = (uint32_t) "$s1";
*(REGISTERS + REG_A0) = (uint32_t) "$a0";
*(REGISTERS + REG_A1) = (uint32_t) "$a1";
*(REGISTERS + REG_A2) = (uint32_t) "$a2";
*(REGISTERS + REG_A3) = (uint32_t) "$a3";
*(REGISTERS + REG_A4) = (uint32_t) "$a4";
*(REGISTERS + REG_A5) = (uint32_t) "$a5";
*(REGISTERS + REG_A6) = (uint32_t) "$a6";
*(REGISTERS + REG_A7) = (uint32_t) "$a7";
*(REGISTERS + REG_S2) = (uint32_t) "$s2";
*(REGISTERS + REG_S3) = (uint32_t) "$s3";
*(REGISTERS + REG_S4) = (uint32_t) "$s4";
*(REGISTERS + REG_S5) = (uint32_t) "$s5";
*(REGISTERS + REG_S6) = (uint32_t) "$s6";
*(REGISTERS + REG_S7) = (uint32_t) "$s7";
*(REGISTERS + REG_S8) = (uint32_t) "$s8";
*(REGISTERS + REG_S9) = (uint32_t) "$s9";
*(REGISTERS + REG_S10) = (uint32_t) "$s10";
*(REGISTERS + REG_S11) = (uint32_t) "$s11";
*(REGISTERS + REG_T3) = (uint32_t) "$t3";
*(REGISTERS + REG_T4) = (uint32_t) "$t4";
*(REGISTERS + REG_T5) = (uint32_t) "$t5";
*(REGISTERS + REG_T6) = (uint32_t) "$t6";
}
// -----------------------------------------------------------------
// ------------------------ ENCODER/DECODER ------------------------
// -----------------------------------------------------------------
void check_immediate_range(uint32_t found, uint32_t bits);
uint32_t encode_r_format(uint32_t funct7, uint32_t rs2, uint32_t rs1, uint32_t funct3, uint32_t rd, uint32_t opcode);
uint32_t get_funct7(uint32_t instruction);
uint32_t get_rs2(uint32_t instruction);
uint32_t get_rs1(uint32_t instruction);
uint32_t get_funct3(uint32_t instruction);
uint32_t get_rd(uint32_t instruction);
uint32_t get_opcode(uint32_t instruction);
void decode_r_format();
uint32_t encode_i_format(uint32_t immediate, uint32_t rs1, uint32_t funct3, uint32_t rd, uint32_t opcode);
uint32_t get_immediate_i_format(uint32_t instruction);
void decode_i_format();
uint32_t encode_s_format(uint32_t immediate, uint32_t rs2, uint32_t rs1, uint32_t funct3, uint32_t opcode);
uint32_t get_immediate_s_format(uint32_t instruction);
void decode_s_format();
uint32_t encode_b_format(uint32_t immediate, uint32_t rs2, uint32_t rs1, uint32_t funct3, uint32_t opcode);
uint32_t get_immediate_b_format(uint32_t instruction);
void decode_b_format();
uint32_t encode_j_format(uint32_t immediate, uint32_t rd, uint32_t opcode);
uint32_t get_immediate_j_format(uint32_t instruction);
void decode_j_format();
uint32_t encode_u_format(uint32_t immediate, uint32_t rd, uint32_t opcode);
uint32_t get_immediate_u_format(uint32_t instruction);
void decode_u_format();
// ------------------------ GLOBAL CONSTANTS -----------------------
// opcodes
uint32_t OP_LW = 3; // 0000011, I format (LW)
uint32_t OP_IMM = 19; // 0010011, I format (ADDI, NOP)
uint32_t OP_SW = 35; // 0100011, S format (SW)
uint32_t OP_OP = 51; // 0110011, R format (ADD, SUB, MUL, DIVU, REMU, SLTU)
uint32_t OP_LUI = 55; // 0110111, U format (LUI)
uint32_t OP_BRANCH = 99; // 1100011, B format (BEQ)
uint32_t OP_JALR = 103; // 1100111, I format (JALR)
uint32_t OP_JAL = 111; // 1101111, J format (JAL)
uint32_t OP_SYSTEM = 115; // 1110011, I format (ECALL)
// f3-codes
uint32_t F3_NOP = 0; // 000
uint32_t F3_ADDI = 0; // 000
uint32_t F3_ADD = 0; // 000
uint32_t F3_SUB = 0; // 000
uint32_t F3_MUL = 0; // 000
uint32_t F3_DIVU = 5; // 101
uint32_t F3_REMU = 7; // 111
uint32_t F3_SLTU = 3; // 011
uint32_t F3_LW = 2; // 010
uint32_t F3_SW = 2; // 010
uint32_t F3_BEQ = 0; // 000
uint32_t F3_JALR = 0; // 000
uint32_t F3_ECALL = 0; // 000
// f7-codes
uint32_t F7_ADD = 0; // 0000000
uint32_t F7_MUL = 1; // 0000001
uint32_t F7_SUB = 32; // 0100000
uint32_t F7_DIVU = 1; // 0000001
uint32_t F7_REMU = 1; // 0000001
uint32_t F7_SLTU = 0; // 0000000
// f12-codes (immediates)
uint32_t F12_ECALL = 0; // 000000000000
// ------------------------ GLOBAL VARIABLES -----------------------
uint32_t opcode = 0;
uint32_t rs1 = 0;
uint32_t rs2 = 0;
uint32_t rd = 0;
uint32_t imm = 0;
uint32_t funct3 = 0;
uint32_t funct7 = 0;
// -----------------------------------------------------------------
// ---------------------------- BINARY -----------------------------
// -----------------------------------------------------------------
void reset_instruction_counters();
uint32_t get_total_number_of_instructions();
void print_instruction_counter(uint32_t total, uint32_t counter, uint32_t* mnemonics);
void print_instruction_counters();
uint32_t load_instruction(uint32_t baddr);
void store_instruction(uint32_t baddr, uint32_t instruction);
uint32_t load_data(uint32_t baddr);
void store_data(uint32_t baddr, uint32_t data);
void emit_instruction(uint32_t instruction);
void emit_nop();
void emit_lui(uint32_t rd, uint32_t immediate);
void emit_addi(uint32_t rd, uint32_t rs1, uint32_t immediate);
void emit_add(uint32_t rd, uint32_t rs1, uint32_t rs2);
void emit_sub(uint32_t rd, uint32_t rs1, uint32_t rs2);
void emit_mul(uint32_t rd, uint32_t rs1, uint32_t rs2);
void emit_divu(uint32_t rd, uint32_t rs1, uint32_t rs2);
void emit_remu(uint32_t rd, uint32_t rs1, uint32_t rs2);
void emit_sltu(uint32_t rd, uint32_t rs1, uint32_t rs2);
void emit_lw(uint32_t rd, uint32_t rs1, uint32_t immediate);
void emit_sw(uint32_t rs1, uint32_t immediate, uint32_t rs2);
void emit_beq(uint32_t rs1, uint32_t rs2, uint32_t immediate);
void emit_jal(uint32_t rd, uint32_t immediate);
void emit_jalr(uint32_t rd, uint32_t rs1, uint32_t immediate);
void emit_ecall();
void fixup_relative_BFormat(uint32_t from_address);
void fixup_relative_JFormat(uint32_t from_address, uint32_t to_address);
void fixlink_relative(uint32_t from_address, uint32_t to_address);
void emit_data_word(uint32_t data, uint32_t offset, uint32_t source_line_number);
void emit_string_data(uint32_t* entry);
void emit_data_segment();
uint32_t* allocate_elf_header();
uint32_t* create_elf_header(uint32_t binary_length, uint32_t code_length);
uint32_t validate_elf_header(uint32_t* header);
uint32_t open_write_only(uint32_t* name);
void selfie_output();
uint32_t* touch(uint32_t* memory, uint32_t length);
void selfie_load();
// ------------------------ GLOBAL CONSTANTS -----------------------
uint32_t MAX_BINARY_LENGTH = 262144; // 256KB = MAX_CODE_LENGTH + MAX_DATA_LENGTH
uint32_t MAX_CODE_LENGTH = 245760; // 240KB
uint32_t MAX_DATA_LENGTH = 16384; // 16KB
// page-aligned ELF header for storing file header (52 bytes),
// program header (32 bytes), and code length (4 bytes)
uint32_t ELF_HEADER_LEN = 4096;
// according to RISC-V pk
uint32_t ELF_ENTRY_POINT = 65536; // = 0x10000 (address of beginning of code)
// ------------------------ GLOBAL VARIABLES -----------------------
// instruction counters
uint32_t ic_lui = 0;
uint32_t ic_addi = 0;
uint32_t ic_add = 0;
uint32_t ic_sub = 0;
uint32_t ic_mul = 0;
uint32_t ic_divu = 0;
uint32_t ic_remu = 0;
uint32_t ic_sltu = 0;
uint32_t ic_lw = 0;
uint32_t ic_sw = 0;
uint32_t ic_beq = 0;
uint32_t ic_jal = 0;
uint32_t ic_jalr = 0;
uint32_t ic_ecall = 0;
uint32_t* binary = (uint32_t*) 0; // binary of code and data segments
uint32_t binary_length = 0; // length of binary in bytes including data segment
uint32_t* binary_name = (uint32_t*) 0; // file name of binary
uint32_t code_length = 0; // length of code segment in binary in bytes
uint32_t entry_point = 0; // beginning of code segment in virtual address space
uint32_t* code_line_number = (uint32_t*) 0; // code line number per emitted instruction
uint32_t* data_line_number = (uint32_t*) 0; // data line number per emitted data
uint32_t* assembly_name = (uint32_t*) 0; // name of assembly file
uint32_t assembly_fd = 0; // file descriptor of open assembly file
uint32_t* ELF_header = (uint32_t*) 0;
// -----------------------------------------------------------------
// ----------------------- MIPSTER SYSCALLS ------------------------
// -----------------------------------------------------------------
void emit_exit();
void implement_exit(uint32_t* context);
void emit_read();
void implement_read(uint32_t* context);
void emit_write();
void implement_write(uint32_t* context);
void emit_open();
uint32_t down_load_string(uint32_t* table, uint32_t vstring, uint32_t* s);
void implement_openat(uint32_t* context);
void emit_malloc();
void implement_brk(uint32_t* context);
// ------------------------ GLOBAL CONSTANTS -----------------------
uint32_t debug_read = 0;
uint32_t debug_write = 0;
uint32_t debug_open = 0;
uint32_t debug_brk = 0;
uint32_t SYSCALL_EXIT = 93;
uint32_t SYSCALL_READ = 63;
uint32_t SYSCALL_WRITE = 64;
uint32_t SYSCALL_OPENAT = 56;
uint32_t SYSCALL_BRK = 214;
/* DIRFD_AT_FDCWD corresponds to AT_FDCWD in fcntl.h and
is passed as first argument of the openat system call
emulating the (in Linux) deprecated open system call. */
uint32_t DIRFD_AT_FDCWD = -100;
// -----------------------------------------------------------------
// ----------------------- HYPSTER SYSCALLS ------------------------
// -----------------------------------------------------------------
void emit_switch();
void do_switch(uint32_t* to_context, uint32_t timeout);
void implement_switch();
uint32_t* mipster_switch(uint32_t* to_context, uint32_t timeout);
// ------------------------ GLOBAL CONSTANTS -----------------------
// TODO: fix this syscall for spike
uint32_t SYSCALL_SWITCH = 401;
uint32_t debug_switch = 0;
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ---------------------- R U N T I M E ----------------------
// -----------------------------------------------------------------
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ---------------------------- MEMORY -----------------------------
// -----------------------------------------------------------------
void init_memory(uint32_t megabytes);
uint32_t load_physical_memory(uint32_t* paddr);
void store_physical_memory(uint32_t* paddr, uint32_t data);