forked from cksystemsteaching/selfie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selfie.c
11251 lines (8508 loc) · 316 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-2021, 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 64-bit, 11-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, and
5. 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 garbage
collector, 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 and hypervisor. The garbage collector is conservative and
may operate as library in the same address space as the mutator and
as part of the emulator in the address space of the kernel.
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 64-bit
integers and 64-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 64-bit integer arithmetic, double-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. The garbage collector
is inspired by the conservative garbage collector of Hans Boehm.
*/
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// --------------------- L I B R A R Y ---------------------
// -----------------------------------------------------------------
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ----------------------- BUILTIN PROCEDURES ----------------------
// -----------------------------------------------------------------
// selfie bootstraps int to uint64_t!
void exit(int code);
uint64_t read(uint64_t fd, uint64_t* buffer, uint64_t bytes_to_read);
uint64_t write(uint64_t fd, uint64_t* buffer, uint64_t bytes_to_write);
// selfie bootstraps char to uint64_t and ignores ellipsis!
uint64_t open(char* filename, uint64_t flags, ...);
// selfie bootstraps void* to uint64_t* and unsigned to uint64_t!
void* malloc(unsigned long);
// -----------------------------------------------------------------
// ----------------------- LIBRARY PROCEDURES ----------------------
// -----------------------------------------------------------------
void init_library();
void reset_library();
uint64_t two_to_the_power_of(uint64_t p);
uint64_t ten_to_the_power_of(uint64_t p);
uint64_t log_ten(uint64_t n);
uint64_t left_shift(uint64_t n, uint64_t b);
uint64_t right_shift(uint64_t n, uint64_t b);
uint64_t get_bits(uint64_t n, uint64_t i, uint64_t b);
uint64_t absolute(uint64_t n);
uint64_t max(uint64_t a, uint64_t b);
uint64_t signed_less_than(uint64_t a, uint64_t b);
uint64_t signed_division(uint64_t a, uint64_t b);
uint64_t is_signed_integer(uint64_t n, uint64_t b);
uint64_t sign_extend(uint64_t n, uint64_t b);
uint64_t sign_shrink(uint64_t n, uint64_t b);
uint64_t load_character(char* s, uint64_t i);
char* store_character(char* s, uint64_t i, uint64_t c);
char* string_alloc(uint64_t l);
uint64_t string_length(char* s);
char* string_copy(char* s);
void string_reverse(char* s);
uint64_t string_compare(char* s, char* t);
uint64_t atoi(char* s);
char* itoa(uint64_t n, char* s, uint64_t b, uint64_t d, uint64_t a);
uint64_t fixed_point_ratio(uint64_t a, uint64_t b, uint64_t f);
uint64_t fixed_point_percentage(uint64_t r, uint64_t f);
uint64_t ratio_format(uint64_t a, uint64_t b);
uint64_t percentage_format(uint64_t a, uint64_t b);
void put_character(uint64_t c);
void print(char* s);
void println();
void print_character(uint64_t c);
void print_string(char* s);
void print_unsigned_integer(uint64_t n);
void print_integer(uint64_t n);
void unprint_integer(uint64_t n);
void print_hexadecimal(uint64_t n, uint64_t a);
void print_octal(uint64_t n, uint64_t a);
void print_binary(uint64_t n, uint64_t a);
uint64_t print_format0(char* s, uint64_t i);
uint64_t print_format1(char* s, uint64_t i, char* a);
void printf1(char* s, char* a1);
void printf2(char* s, char* a1, char* a2);
void printf3(char* s, char* a1, char* a2, char* a3);
void printf4(char* s, char* a1, char* a2, char* a3, char* a4);
void printf5(char* s, char* a1, char* a2, char* a3, char* a4, char* a5);
void printf6(char* s, char* a1, char* a2, char* a3, char* a4, char* a5, char* a6);
void printf7(char* s, char* a1, char* a2, char* a3, char* a4, char* a5, char* a6, char* a7);
void sprintf1(char* b, char* s, char* a1);
void sprintf2(char* b, char* s, char* a1, char* a2);
void sprintf3(char* b, char* s, char* a1, char* a2, char* a3);
void sprintf4(char* b, char* s, char* a1, char* a2, char* a3, char* a4);
uint64_t round_up(uint64_t n, uint64_t m);
void zero_memory(uint64_t* memory, uint64_t size);
uint64_t* smalloc(uint64_t size); // use this to allocate memory, not malloc
uint64_t* zmalloc(uint64_t size); // use this to allocate zeroed memory
// ------------------------ GLOBAL CONSTANTS -----------------------
char* SELFIE_URL = (char*) 0;
uint64_t SIZEOFUINT64 = 8; // in bytes
uint64_t SIZEOFUINT64INBITS = 64; // SIZEOFUINT64 * 8
uint64_t SIZEOFUINT64STAR = 8; // in bytes, must be the same as SIZEOFUINT64
uint64_t SIZEOFUINT64STARINBITS = 64; // SIZEOFUINT64STAR * 8
uint64_t WORDSIZE = 8; // (double) word size in bytes, must be the same as SIZEOFUINT64
uint64_t WORDSIZEINBITS = 64; // WORDSIZE * 8
uint64_t IS64BITSYSTEM = 1; // flag indicating 64-bit selfie
uint64_t* power_of_two_table;
uint64_t UINT64_MAX; // maximum numerical value of an unsigned 64-bit integer
uint64_t INT64_MAX; // maximum numerical value of a signed 64-bit integer
uint64_t INT64_MIN; // minimum numerical value of a signed 64-bit integer
uint64_t CHAR_EOF = -1; // end of file
uint64_t CHAR_BACKSPACE = 8; // ASCII code 8 = backspace
uint64_t CHAR_TAB = 9; // ASCII code 9 = tabulator
uint64_t CHAR_LF = 10; // ASCII code 10 = line feed
uint64_t CHAR_CR = 13; // ASCII code 13 = carriage return
uint64_t CHAR_SPACE = ' ';
uint64_t CHAR_UNDERSCORE = '_';
uint64_t CHAR_SINGLEQUOTE = 39; // ASCII code 39 = '
uint64_t CHAR_DOUBLEQUOTE = '"';
uint64_t CHAR_COMMA = ',';
uint64_t CHAR_SEMICOLON = ';';
uint64_t CHAR_LPARENTHESIS = '(';
uint64_t CHAR_RPARENTHESIS = ')';
uint64_t CHAR_LBRACE = '{';
uint64_t CHAR_RBRACE = '}';
uint64_t CHAR_PLUS = '+';
uint64_t CHAR_DASH = '-';
uint64_t CHAR_ASTERISK = '*';
uint64_t CHAR_SLASH = '/';
uint64_t CHAR_PERCENTAGE = '%';
uint64_t CHAR_EQUAL = '=';
uint64_t CHAR_EXCLAMATION = '!';
uint64_t CHAR_LT = '<';
uint64_t CHAR_GT = '>';
uint64_t CHAR_BACKSLASH = 92; // ASCII code 92 = backslash
uint64_t CHAR_DOT = '.'; // for bootstrapping ellipsis ...
uint64_t* character_buffer; // buffer for reading and writing characters
char* integer_buffer; // buffer for formatting integers
uint64_t MAX_FILENAME_LENGTH = 128;
char* filename_buffer; // buffer for opening files
uint64_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
uint64_t O_RDONLY = 32768;
// flags for opening write-only files
// LINUX: 577 = 0x0241 = O_CREAT (0x0040) | O_TRUNC (0x0200) | O_WRONLY (0x0001)
uint64_t LINUX_O_CREAT_TRUNC_WRONLY = 577;
// MAC: 1537 = 0x0601 = O_CREAT (0x0200) | O_TRUNC (0x0400) | O_WRONLY (0x0001)
uint64_t MAC_O_CREAT_TRUNC_WRONLY = 1537;
// WINDOWS: 33537 = 0x8301 = _O_BINARY (0x8000) | _O_CREAT (0x0100) | _O_TRUNC (0x0200) | _O_WRONLY (0x0001)
uint64_t WINDOWS_O_BINARY_CREAT_TRUNC_WRONLY = 33537;
// default is LINUX, re-initialized in init_system
uint64_t O_CREAT_TRUNC_WRONLY = 577; // write-only flags for host operating system
// flags for rw-r--r-- (text) 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
uint64_t S_IRUSR_IWUSR_IRGRP_IROTH = 420;
// flags for rwxr-xr-x (binary) file permissions
// 493 = 00755 = S_IRUSR_IWUSR_IRGRP_IROTH | S_IXUSR (00100) | S_IXGRP (00010) | S_IXOTH (00001)
// these flags also seem to be working for LINUX, MAC, and WINDOWS
uint64_t S_IRUSR_IWUSR_IXUSR_IRGRP_IXGRP_IROTH_IXOTH = 493;
// ------------------------ GLOBAL VARIABLES -----------------------
uint64_t number_of_written_characters = 0;
char* output_name = (char*) 0;
uint64_t output_fd = 1; // 1 is file descriptor of standard output
char* output_buffer = (char*) 0;
uint64_t output_cursor = 0; // cursor for output buffer
// ------------------------- INITIALIZATION ------------------------
void init_library() {
uint64_t i;
SELFIE_URL = "selfie.cs.uni-salzburg.at";
// determine actual size of uint64_t
SIZEOFUINT64 = (uint64_t) ((uint64_t*) SELFIE_URL + 1) - (uint64_t) SELFIE_URL;
SIZEOFUINT64INBITS = SIZEOFUINT64 * 8;
// determine actual size of uint64_t*
SIZEOFUINT64STAR = (uint64_t) ((uint64_t**) SELFIE_URL + 1) - (uint64_t) SELFIE_URL;
SIZEOFUINT64STARINBITS = SIZEOFUINT64STAR * 8;
// WORDSIZE must be the same as SIZEOFUINT64
WORDSIZE = SIZEOFUINT64;
WORDSIZEINBITS = WORDSIZE * 8;
// powers of two table with SIZEOFUINT64INBITS entries for 2^0 to 2^(SIZEOFUINT64INBITS - 1)
power_of_two_table = smalloc(SIZEOFUINT64INBITS * SIZEOFUINT64);
*power_of_two_table = 1; // 2^0 == 1
i = 1;
while (i < SIZEOFUINT64INBITS) {
// 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 64-bit unsigned integer range using signed integer arithmetic
UINT64_MAX = -1;
// compute 64-bit signed integer range using unsigned integer arithmetic
INT64_MIN = two_to_the_power_of(SIZEOFUINT64INBITS - 1);
INT64_MAX = INT64_MIN - 1;
// allocate and touch to make sure memory is mapped for read calls
character_buffer = smalloc(SIZEOFUINT64);
*character_buffer = 0;
// accommodate at least SIZEOFUINT64INBITS numbers for itoa, no mapping needed
integer_buffer = string_alloc(SIZEOFUINT64INBITS);
// does not need to be mapped
filename_buffer = string_alloc(MAX_FILENAME_LENGTH);
// allocate and touch to make sure memory is mapped for read calls
binary_buffer = smalloc(SIZEOFUINT64);
*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(uint64_t symbol);
void print_line_number(char* message, uint64_t line);
void syntax_error_message(char* message);
void syntax_error_character(uint64_t character);
void syntax_error_identifier(char* expected);
void get_character();
uint64_t is_character_new_line();
uint64_t is_character_whitespace();
uint64_t find_next_character();
uint64_t is_character_letter();
uint64_t is_character_digit();
uint64_t is_character_letter_or_digit_or_underscore();
uint64_t is_character_not_double_quote_or_new_line_or_eof();
uint64_t identifier_string_match(uint64_t string_index);
uint64_t identifier_or_keyword();
void get_symbol();
void handle_escape_sequence();
// ------------------------ GLOBAL CONSTANTS -----------------------
uint64_t SYM_EOF = -1; // end of file
// C* symbols
uint64_t SYM_INTEGER = 0; // integer
uint64_t SYM_CHARACTER = 1; // character
uint64_t SYM_STRING = 2; // string
uint64_t SYM_IDENTIFIER = 3; // identifier
uint64_t SYM_UINT64 = 4; // uint64_t
uint64_t SYM_IF = 5; // if
uint64_t SYM_ELSE = 6; // else
uint64_t SYM_VOID = 7; // void
uint64_t SYM_RETURN = 8; // return
uint64_t SYM_WHILE = 9; // while
uint64_t SYM_COMMA = 10; // ,
uint64_t SYM_SEMICOLON = 11; // ;
uint64_t SYM_LPARENTHESIS = 12; // (
uint64_t SYM_RPARENTHESIS = 13; // )
uint64_t SYM_LBRACE = 14; // {
uint64_t SYM_RBRACE = 15; // }
uint64_t SYM_PLUS = 16; // +
uint64_t SYM_MINUS = 17; // -
uint64_t SYM_ASTERISK = 18; // *
uint64_t SYM_DIVISION = 19; // /
uint64_t SYM_REMAINDER = 20; // %
uint64_t SYM_ASSIGN = 21; // =
uint64_t SYM_EQUALITY = 22; // ==
uint64_t SYM_NOTEQ = 23; // !=
uint64_t SYM_LT = 24; // <
uint64_t SYM_LEQ = 25; // <=
uint64_t SYM_GT = 26; // >
uint64_t SYM_GEQ = 27; // >=
// symbols for bootstrapping
uint64_t SYM_INT = 28; // int
uint64_t SYM_CHAR = 29; // char
uint64_t SYM_UNSIGNED = 30; // unsigned
uint64_t SYM_ELLIPSIS = 31; // ...
uint64_t* SYMBOLS; // strings representing symbols
uint64_t MAX_IDENTIFIER_LENGTH = 64; // maximum number of characters in an identifier
uint64_t MAX_INTEGER_LENGTH = 20; // maximum number of characters in an unsigned integer
uint64_t MAX_STRING_LENGTH = 128; // maximum number of characters in a string
// ------------------------ GLOBAL VARIABLES -----------------------
uint64_t line_number = 1; // current line number for error reporting
char* identifier = (char*) 0; // stores scanned identifier as string
char* integer = (char*) 0; // stores scanned integer as string
char* string = (char*) 0; // stores scanned string
uint64_t literal = 0; // numerical value of most recently scanned integer or character
uint64_t integer_is_signed = 0; // enforce INT64_MIN limit if '-' was scanned before
uint64_t character; // most recently read character
uint64_t number_of_read_characters = 0;
uint64_t symbol; // most recently recognized symbol
uint64_t number_of_ignored_characters = 0;
uint64_t number_of_comments = 0;
uint64_t number_of_scanned_symbols = 0;
char* source_name = (char*) 0; // name of source file
uint64_t source_fd = 0; // file descriptor of open source file
// ------------------------- INITIALIZATION ------------------------
void init_scanner () {
SYMBOLS = smalloc((SYM_ELLIPSIS + 1) * SIZEOFUINT64STAR);
*(SYMBOLS + SYM_INTEGER) = (uint64_t) "integer";
*(SYMBOLS + SYM_CHARACTER) = (uint64_t) "character";
*(SYMBOLS + SYM_STRING) = (uint64_t) "string";
*(SYMBOLS + SYM_IDENTIFIER) = (uint64_t) "identifier";
*(SYMBOLS + SYM_UINT64) = (uint64_t) "uint64_t";
*(SYMBOLS + SYM_IF) = (uint64_t) "if";
*(SYMBOLS + SYM_ELSE) = (uint64_t) "else";
*(SYMBOLS + SYM_VOID) = (uint64_t) "void";
*(SYMBOLS + SYM_RETURN) = (uint64_t) "return";
*(SYMBOLS + SYM_WHILE) = (uint64_t) "while";
*(SYMBOLS + SYM_COMMA) = (uint64_t) ",";
*(SYMBOLS + SYM_SEMICOLON) = (uint64_t) ";";
*(SYMBOLS + SYM_LPARENTHESIS) = (uint64_t) "(";
*(SYMBOLS + SYM_RPARENTHESIS) = (uint64_t) ")";
*(SYMBOLS + SYM_LBRACE) = (uint64_t) "{";
*(SYMBOLS + SYM_RBRACE) = (uint64_t) "}";
*(SYMBOLS + SYM_PLUS) = (uint64_t) "+";
*(SYMBOLS + SYM_MINUS) = (uint64_t) "-";
*(SYMBOLS + SYM_ASTERISK) = (uint64_t) "*";
*(SYMBOLS + SYM_DIVISION) = (uint64_t) "/";
*(SYMBOLS + SYM_REMAINDER) = (uint64_t) "%";
*(SYMBOLS + SYM_ASSIGN) = (uint64_t) "=";
*(SYMBOLS + SYM_EQUALITY) = (uint64_t) "==";
*(SYMBOLS + SYM_NOTEQ) = (uint64_t) "!=";
*(SYMBOLS + SYM_LT) = (uint64_t) "<";
*(SYMBOLS + SYM_LEQ) = (uint64_t) "<=";
*(SYMBOLS + SYM_GT) = (uint64_t) ">";
*(SYMBOLS + SYM_GEQ) = (uint64_t) ">=";
*(SYMBOLS + SYM_INT) = (uint64_t) "int";
*(SYMBOLS + SYM_CHAR) = (uint64_t) "char";
*(SYMBOLS + SYM_UNSIGNED) = (uint64_t) "unsigned";
*(SYMBOLS + SYM_ELLIPSIS) = (uint64_t) "...";
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();
uint64_t hash(uint64_t* key);
void create_symbol_table_entry(uint64_t which, char* string, uint64_t line, uint64_t class, uint64_t type, uint64_t value, uint64_t address);
uint64_t* search_symbol_table(uint64_t* entry, char* string, uint64_t class);
uint64_t* search_global_symbol_table(char* string, uint64_t class);
uint64_t* get_scoped_symbol_table_entry(char* string, uint64_t class);
uint64_t is_undefined_procedure(uint64_t* entry);
uint64_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 | UINT64_T, UINT64STAR_T, VOID_T
// | 5 | value | VARIABLE: initial value
// | 6 | address | VARIABLE, BIGINT, STRING: offset, PROCEDURE: address
// | 7 | scope | REG_GP (global), REG_S0 (local)
// +---+---------+
uint64_t* allocate_symbol_table_entry() {
return smalloc(2 * SIZEOFUINT64STAR + 6 * SIZEOFUINT64);
}
uint64_t* get_next_entry(uint64_t* entry) { return (uint64_t*) *entry; }
char* get_string(uint64_t* entry) { return (char*) *(entry + 1); }
uint64_t get_line_number(uint64_t* entry) { return *(entry + 2); }
uint64_t get_class(uint64_t* entry) { return *(entry + 3); }
uint64_t get_type(uint64_t* entry) { return *(entry + 4); }
uint64_t get_value(uint64_t* entry) { return *(entry + 5); }
uint64_t get_address(uint64_t* entry) { return *(entry + 6); }
uint64_t get_scope(uint64_t* entry) { return *(entry + 7); }
void set_next_entry(uint64_t* entry, uint64_t* next) { *entry = (uint64_t) next; }
void set_string(uint64_t* entry, char* identifier) { *(entry + 1) = (uint64_t) identifier; }
void set_line_number(uint64_t* entry, uint64_t line) { *(entry + 2) = line; }
void set_class(uint64_t* entry, uint64_t class) { *(entry + 3) = class; }
void set_type(uint64_t* entry, uint64_t type) { *(entry + 4) = type; }
void set_value(uint64_t* entry, uint64_t value) { *(entry + 5) = value; }
void set_address(uint64_t* entry, uint64_t address) { *(entry + 6) = address; }
void set_scope(uint64_t* entry, uint64_t scope) { *(entry + 7) = scope; }
// ------------------------ GLOBAL CONSTANTS -----------------------
// classes
uint64_t VARIABLE = 1;
uint64_t BIGINT = 2;
uint64_t STRING = 3;
uint64_t PROCEDURE = 4;
// types
uint64_t UINT64_T = 1;
uint64_t UINT64STAR_T = 2;
uint64_t VOID_T = 3;
// symbol tables
uint64_t GLOBAL_TABLE = 1;
uint64_t LOCAL_TABLE = 2;
uint64_t LIBRARY_TABLE = 3;
// hash table size for global symbol table
uint64_t HASH_TABLE_SIZE = 1024;
// ------------------------ GLOBAL VARIABLES -----------------------
// table pointers
uint64_t* global_symbol_table = (uint64_t*) 0;
uint64_t* local_symbol_table = (uint64_t*) 0;
uint64_t* library_symbol_table = (uint64_t*) 0;
uint64_t number_of_global_variables = 0;
uint64_t number_of_procedures = 0;
uint64_t number_of_strings = 0;
uint64_t number_of_searches = 0;
uint64_t total_search_time = 0;
// ------------------------- INITIALIZATION ------------------------
void reset_symbol_tables() {
global_symbol_table = (uint64_t*) zmalloc(HASH_TABLE_SIZE * SIZEOFUINT64STAR);
local_symbol_table = (uint64_t*) 0;
library_symbol_table = (uint64_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();
uint64_t is_not_rbrace_or_eof();
uint64_t is_expression();
uint64_t is_int_or_char_literal();
uint64_t is_mult_or_div_or_rem();
uint64_t is_plus_or_minus();
uint64_t is_comparison();
uint64_t look_for_factor();
uint64_t look_for_statement();
uint64_t look_for_type();
void talloc();
uint64_t current_temporary();
uint64_t previous_temporary();
uint64_t next_temporary();
void tfree(uint64_t number_of_temporaries);
void save_temporaries();
void restore_temporaries(uint64_t number_of_temporaries);
void syntax_error_symbol(uint64_t expected);
void syntax_error_unexpected();
void print_type(uint64_t type);
void type_warning(uint64_t expected, uint64_t found);
void load_small_and_medium_integer(uint64_t reg, uint64_t value);
uint64_t* get_variable_or_big_int(char* variable, uint64_t class);
void load_upper_base_address(uint64_t* entry);
uint64_t load_variable_or_big_int(char* variable, uint64_t class);
void load_integer(uint64_t value);
void load_string(char* string);
uint64_t procedure_call(uint64_t* entry, char* procedure);
void procedure_prologue(uint64_t number_of_local_variable_bytes);
void procedure_epilogue(uint64_t number_of_parameter_bytes);
uint64_t compile_call(char* procedure);
uint64_t compile_factor();
uint64_t compile_term();
uint64_t compile_simple_expression();
uint64_t compile_expression();
void compile_while();
void compile_if();
void compile_return();
void compile_statement();
uint64_t compile_type();
void compile_variable(uint64_t offset);
uint64_t compile_initialization(uint64_t type);
void compile_procedure(char* procedure, uint64_t type);
void compile_cstar();
// ------------------------ GLOBAL VARIABLES -----------------------
uint64_t allocated_temporaries = 0; // number of allocated temporaries
uint64_t return_branches = 0; // fixup chain for return statements
uint64_t return_type = 0; // return type of currently parsed procedure
uint64_t number_of_calls = 0;
uint64_t number_of_assignments = 0;
uint64_t number_of_while = 0;
uint64_t number_of_if = 0;
uint64_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(uint64_t reg, uint64_t m);
void emit_multiply_by(uint64_t reg, uint64_t m);
void emit_program_entry();
// bootstrapping binary
void emit_bootstrapping();
// -----------------------------------------------------------------
// --------------------------- COMPILER ----------------------------
// -----------------------------------------------------------------
uint64_t open_read_only(char* name);
void selfie_compile();
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ------------------- I N T E R F A C E -------------------
// -----------------------------------------------------------------
// *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~ *~*~
// -----------------------------------------------------------------
// ---------------------------- REGISTER ---------------------------
// -----------------------------------------------------------------
void init_register();
char* get_register_name(uint64_t reg);
void print_register_name(uint64_t reg);
uint64_t is_stack_register(uint64_t reg);
uint64_t is_system_register(uint64_t reg);
uint64_t is_argument_register(uint64_t reg);
uint64_t is_temporary_register(uint64_t reg);
uint64_t read_register(uint64_t reg);
void write_register(uint64_t reg);
void update_register_counters();
// ------------------------ GLOBAL CONSTANTS -----------------------
uint64_t NUMBEROFREGISTERS = 32;
uint64_t NUMBEROFTEMPORARIES = 7;
uint64_t REG_ZR = 0;
uint64_t REG_RA = 1;
uint64_t REG_SP = 2;
uint64_t REG_GP = 3;
uint64_t REG_TP = 4;
uint64_t REG_T0 = 5;
uint64_t REG_T1 = 6;
uint64_t REG_T2 = 7;
uint64_t REG_S0 = 8;
uint64_t REG_S1 = 9;
uint64_t REG_A0 = 10;
uint64_t REG_A1 = 11;
uint64_t REG_A2 = 12;
uint64_t REG_A3 = 13;
uint64_t REG_A4 = 14;
uint64_t REG_A5 = 15;
uint64_t REG_A6 = 16;
uint64_t REG_A7 = 17;
uint64_t REG_S2 = 18;
uint64_t REG_S3 = 19;
uint64_t REG_S4 = 20;
uint64_t REG_S5 = 21;
uint64_t REG_S6 = 22;
uint64_t REG_S7 = 23;
uint64_t REG_S8 = 24;
uint64_t REG_S9 = 25;
uint64_t REG_S10 = 26;
uint64_t REG_S11 = 27;
uint64_t REG_T3 = 28;
uint64_t REG_T4 = 29;
uint64_t REG_T5 = 30;
uint64_t REG_T6 = 31;
uint64_t* REGISTERS; // strings representing registers
// ------------------------- INITIALIZATION ------------------------
void init_register() {
REGISTERS = smalloc(NUMBEROFREGISTERS * SIZEOFUINT64STAR);
*(REGISTERS + REG_ZR) = (uint64_t) "zero";
*(REGISTERS + REG_RA) = (uint64_t) "ra";
*(REGISTERS + REG_SP) = (uint64_t) "sp";
*(REGISTERS + REG_GP) = (uint64_t) "gp";
*(REGISTERS + REG_TP) = (uint64_t) "tp";
*(REGISTERS + REG_T0) = (uint64_t) "t0";
*(REGISTERS + REG_T1) = (uint64_t) "t1";
*(REGISTERS + REG_T2) = (uint64_t) "t2";
*(REGISTERS + REG_S0) = (uint64_t) "s0"; // used to be fp
*(REGISTERS + REG_S1) = (uint64_t) "s1";
*(REGISTERS + REG_A0) = (uint64_t) "a0";
*(REGISTERS + REG_A1) = (uint64_t) "a1";
*(REGISTERS + REG_A2) = (uint64_t) "a2";
*(REGISTERS + REG_A3) = (uint64_t) "a3";
*(REGISTERS + REG_A4) = (uint64_t) "a4";
*(REGISTERS + REG_A5) = (uint64_t) "a5";
*(REGISTERS + REG_A6) = (uint64_t) "a6";
*(REGISTERS + REG_A7) = (uint64_t) "a7";
*(REGISTERS + REG_S2) = (uint64_t) "s2";
*(REGISTERS + REG_S3) = (uint64_t) "s3";
*(REGISTERS + REG_S4) = (uint64_t) "s4";
*(REGISTERS + REG_S5) = (uint64_t) "s5";
*(REGISTERS + REG_S6) = (uint64_t) "s6";
*(REGISTERS + REG_S7) = (uint64_t) "s7";
*(REGISTERS + REG_S8) = (uint64_t) "s8";
*(REGISTERS + REG_S9) = (uint64_t) "s9";
*(REGISTERS + REG_S10) = (uint64_t) "s10";
*(REGISTERS + REG_S11) = (uint64_t) "s11";
*(REGISTERS + REG_T3) = (uint64_t) "t3";
*(REGISTERS + REG_T4) = (uint64_t) "t4";
*(REGISTERS + REG_T5) = (uint64_t) "t5";
*(REGISTERS + REG_T6) = (uint64_t) "t6";
}
// -----------------------------------------------------------------
// ------------------------ ENCODER/DECODER ------------------------
// -----------------------------------------------------------------
void check_immediate_range(uint64_t found, uint64_t bits);
uint64_t encode_r_format(uint64_t funct7, uint64_t rs2, uint64_t rs1, uint64_t funct3, uint64_t rd, uint64_t opcode);
uint64_t get_funct7(uint64_t instruction);
uint64_t get_rs2(uint64_t instruction);
uint64_t get_rs1(uint64_t instruction);
uint64_t get_funct3(uint64_t instruction);
uint64_t get_rd(uint64_t instruction);
uint64_t get_opcode(uint64_t instruction);
void decode_r_format();
uint64_t encode_i_format(uint64_t immediate, uint64_t rs1, uint64_t funct3, uint64_t rd, uint64_t opcode);
uint64_t get_immediate_i_format(uint64_t instruction);
void decode_i_format();
uint64_t encode_s_format(uint64_t immediate, uint64_t rs2, uint64_t rs1, uint64_t funct3, uint64_t opcode);
uint64_t get_immediate_s_format(uint64_t instruction);
void decode_s_format();
uint64_t encode_b_format(uint64_t immediate, uint64_t rs2, uint64_t rs1, uint64_t funct3, uint64_t opcode);
uint64_t get_immediate_b_format(uint64_t instruction);
void decode_b_format();
uint64_t encode_j_format(uint64_t immediate, uint64_t rd, uint64_t opcode);
uint64_t get_immediate_j_format(uint64_t instruction);
void decode_j_format();
uint64_t encode_u_format(uint64_t immediate, uint64_t rd, uint64_t opcode);
uint64_t get_immediate_u_format(uint64_t instruction);
void decode_u_format();
// ------------------------ GLOBAL CONSTANTS -----------------------
// opcodes
uint64_t OP_LOAD = 3; // 0000011, I format (LD,LW)
uint64_t OP_IMM = 19; // 0010011, I format (ADDI, NOP)
uint64_t OP_STORE = 35; // 0100011, S format (SD,SW)
uint64_t OP_OP = 51; // 0110011, R format (ADD, SUB, MUL, DIVU, REMU, SLTU)
uint64_t OP_LUI = 55; // 0110111, U format (LUI)
uint64_t OP_BRANCH = 99; // 1100011, B format (BEQ)
uint64_t OP_JALR = 103; // 1100111, I format (JALR)
uint64_t OP_JAL = 111; // 1101111, J format (JAL)
uint64_t OP_SYSTEM = 115; // 1110011, I format (ECALL)
// f3-codes
uint64_t F3_NOP = 0; // 000
uint64_t F3_ADDI = 0; // 000
uint64_t F3_ADD = 0; // 000
uint64_t F3_SUB = 0; // 000
uint64_t F3_MUL = 0; // 000
uint64_t F3_DIVU = 5; // 101
uint64_t F3_REMU = 7; // 111
uint64_t F3_SLTU = 3; // 011
uint64_t F3_LD = 3; // 011
uint64_t F3_SD = 3; // 011
uint64_t F3_LW = 2; // 010
uint64_t F3_SW = 2; // 010
uint64_t F3_BEQ = 0; // 000
uint64_t F3_JALR = 0; // 000
uint64_t F3_ECALL = 0; // 000
// f7-codes
uint64_t F7_ADD = 0; // 0000000
uint64_t F7_MUL = 1; // 0000001
uint64_t F7_SUB = 32; // 0100000
uint64_t F7_DIVU = 1; // 0000001
uint64_t F7_REMU = 1; // 0000001
uint64_t F7_SLTU = 0; // 0000000
// f12-codes (immediates)
uint64_t F12_ECALL = 0; // 000000000000
// ------------------------ GLOBAL VARIABLES -----------------------
uint64_t opcode = 0;
uint64_t rs1 = 0;
uint64_t rs2 = 0;
uint64_t rd = 0;
uint64_t imm = 0;
uint64_t funct3 = 0;
uint64_t funct7 = 0;
// -----------------------------------------------------------------
// ---------------------------- BINARY -----------------------------
// -----------------------------------------------------------------
void reset_instruction_counters();
uint64_t get_total_number_of_instructions();
uint64_t get_total_number_of_nops();
void print_instruction_counter(uint64_t counter, uint64_t ins);
void print_instruction_counter_with_nops(uint64_t counter, uint64_t nops, uint64_t ins);
void print_instruction_counters();
uint64_t get_low_instruction(uint64_t word);
uint64_t get_high_instruction(uint64_t word);
uint64_t load_code(uint64_t caddr);
void store_code(uint64_t caddr, uint64_t code);
uint64_t load_instruction(uint64_t caddr);
void store_instruction(uint64_t caddr, uint64_t instruction);
uint64_t load_data(uint64_t daddr);
void store_data(uint64_t daddr, uint64_t data);
void emit_instruction(uint64_t instruction);
uint64_t encode_nop();
void emit_nop();
void emit_lui(uint64_t rd, uint64_t immediate);
void emit_addi(uint64_t rd, uint64_t rs1, uint64_t immediate);
void emit_add(uint64_t rd, uint64_t rs1, uint64_t rs2);
void emit_sub(uint64_t rd, uint64_t rs1, uint64_t rs2);
void emit_mul(uint64_t rd, uint64_t rs1, uint64_t rs2);
void emit_divu(uint64_t rd, uint64_t rs1, uint64_t rs2);
void emit_remu(uint64_t rd, uint64_t rs1, uint64_t rs2);
void emit_sltu(uint64_t rd, uint64_t rs1, uint64_t rs2);
void emit_load(uint64_t rd, uint64_t rs1, uint64_t immediate);
void emit_store(uint64_t rs1, uint64_t immediate, uint64_t rs2);
void emit_beq(uint64_t rs1, uint64_t rs2, uint64_t immediate);
void emit_jal(uint64_t rd, uint64_t immediate);
void emit_jalr(uint64_t rd, uint64_t rs1, uint64_t immediate);
void emit_ecall();
void fixup_relative_BFormat(uint64_t from_address);
void fixup_relative_JFormat(uint64_t from_address, uint64_t to_address);
void fixlink_relative(uint64_t from_address, uint64_t to_address);
void emit_data_word(uint64_t data, uint64_t offset, uint64_t source_line_number);
void emit_string_data(uint64_t* entry);
void emit_data_segment();
uint64_t* allocate_elf_header();
uint64_t* encode_elf_header();
uint64_t get_elf_program_header_offset(uint64_t ph_index);
void encode_elf_program_header(uint64_t* header, uint64_t ph_index);
void decode_elf_program_header(uint64_t* header, uint64_t ph_index);
uint64_t validate_elf_header(uint64_t* header);
uint64_t open_write_only(char* name, uint64_t mode);
void selfie_output(char* filename);
uint64_t* touch(uint64_t* memory, uint64_t bytes);
void selfie_load();
// ------------------------ GLOBAL CONSTANTS -----------------------
// page-aligned ELF header size for storing file header, program header, code size
uint64_t ELF_HEADER_SIZE = 4096;
uint64_t MAX_CODE_SIZE = 262144; // 256KB
uint64_t MAX_DATA_SIZE = 32768; // 32KB
uint64_t PK_CODE_START = 65536; // start of code segment at 0x10000 (according to RISC-V pk)
// ELF file header
uint64_t EI_MAG0 = 127; // magic number part 0 is 0x7F
uint64_t EI_MAG1 = 'E'; // magic number part 1
uint64_t EI_MAG2 = 'L'; // magic number part 2
uint64_t EI_MAG3 = 'F'; // magic number part 3