Skip to content

Commit db4db04

Browse files
committed
Move the bootstrap code up one level
1 parent fe407f2 commit db4db04

File tree

158 files changed

+426
-38
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

158 files changed

+426
-38
lines changed

README.md

+8-8

bootstrap/README.md

-9
This file was deleted.

bootstrap/bootstrap5/compiler0/main.s

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
# Create the include list
8383
%call :_ll_init
8484
mov @ll, @ret
85-
%call :_ll_create_node_int, &"bootstrap/bootstrap5"
85+
%call :_ll_create_node_int, &"bootstrap5"
8686
mov @node, @ret
8787
%call :_ll_insert_head, @ll, @node
8888

bootstrap/bootstrap5/lex/tests/lex_test.s

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
dd 0
137137

138138
:_lex_test_tokens
139-
%call :__lex_confirm_file, &"bootstrap/bootstrap5/lex/tests/c/test.c", :__lex_test_tokens_expected
139+
%call :__lex_confirm_file, &"bootstrap5/lex/tests/c/test.c", :__lex_test_tokens_expected
140140
%ret
141141

142142
:__lex_test_tokens_define_expected
@@ -149,7 +149,7 @@
149149
dd 0
150150

151151
:_lex_test_tokens_define
152-
%call :__lex_confirm_file, &"bootstrap/bootstrap5/lex/tests/c/test_define.c", :__lex_test_tokens_define_expected
152+
%call :__lex_confirm_file, &"bootstrap5/lex/tests/c/test_define.c", :__lex_test_tokens_define_expected
153153
%ret
154154

155155
:__lex_test_tokens_include_expected
@@ -162,5 +162,5 @@
162162
dd 0
163163

164164
:_lex_test_tokens_include
165-
%call :__lex_confirm_file, &"bootstrap/bootstrap5/lex/tests/c/test_include.c", :__lex_test_tokens_include_expected
165+
%call :__lex_confirm_file, &"bootstrap5/lex/tests/c/test_include.c", :__lex_test_tokens_include_expected
166166
%ret

bootstrap/bootstrap5/lex/tests/tests.s

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# Create the include list
1313
%call :_ll_init
1414
mov @ll, @ret
15-
%call :_ll_create_node_int, &"bootstrap/bootstrap5/lex/tests/c"
15+
%call :_ll_create_node_int, &"bootstrap5/lex/tests/c"
1616
mov @node, @ret
1717
%call :_ll_insert_head, @ll, @node
1818

bootstrap/tests/bootstrap4/test6.s

-6
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

bootstrap5/compiler0/main.s

+210
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
# Stage 0 compiler: we want to get enough C that we can move out of assembly land
2+
3+
#include "regs.h"
4+
#include "../lex/lex.h"
5+
6+
#define BUFFER_SIZE 256
7+
8+
# Track global identifiers
9+
:_global_symbols
10+
dd 0
11+
12+
:_track_global
13+
%arg global
14+
%arg size
15+
%local ht
16+
ld.d @ht, [:_global_symbols]
17+
# Make a copy and insert it into the globals hash table
18+
%call :_stralloc, @global
19+
mov @global, @ret
20+
%call :_ht_insert, @ht, @global, @size
21+
%ret
22+
23+
:_is_global
24+
%arg global
25+
%local ht
26+
ld.d @ht, [:_global_symbols]
27+
%call :_ht_lookup, @ht, @global
28+
%ret
29+
30+
# Track local identifiers
31+
:_local_symbols
32+
dd 0
33+
34+
:_track_local
35+
%arg local
36+
%arg size
37+
%local ht
38+
ld.d @ht, [:_local_symbols]
39+
# Make a copy and insert it into the locals hash table
40+
%call :_stralloc, @local
41+
mov @local, @ret
42+
%call :_ht_insert, @ht, @local, @size
43+
%ret
44+
45+
:_is_local
46+
%arg local
47+
%local ht
48+
ld.d @ht, [:_local_symbols]
49+
%call :_ht_lookup, @ht, @local
50+
%ret
51+
52+
:_teardown_locals
53+
# Leaks locals because we don't have a proper heap yet
54+
mov @tmp0, 0
55+
st.d [:_local_symbols], @tmp0
56+
%ret
57+
58+
:_main
59+
%arg argc
60+
%arg argv
61+
%local buf1
62+
%local buf2
63+
%local token
64+
%local ll
65+
%local node
66+
%local lex
67+
%local file
68+
%local args
69+
%local output
70+
%local size
71+
72+
# Allocate a 256-byte buffer
73+
%call :_malloc, @BUFFER_SIZE
74+
mov @buf1, @ret
75+
%call :_malloc, @BUFFER_SIZE
76+
mov @buf2, @ret
77+
78+
# Create the globals hash table
79+
%call :_ht_init, :__lex_hash_table_test_key_hash, :__lex_hash_table_test_key_compare
80+
st.d [:_global_symbols], @ret
81+
82+
# Create the include list
83+
%call :_ll_init
84+
mov @ll, @ret
85+
%call :_ll_create_node_int, &"bootstrap5"
86+
mov @node, @ret
87+
%call :_ll_insert_head, @ll, @node
88+
89+
# Create the lex environment
90+
%call :__lex_create, @ll
91+
mov @lex, @ret
92+
93+
# Open a file
94+
mov @args, @argv
95+
96+
# Get argv[1] - the file the open
97+
add @args, 4
98+
ld.d @file, [@args]
99+
100+
# Get argv[2] - the output file
101+
add @args, 4
102+
ld.d @output, [@args]
103+
%call :_compiler_out_open, @output
104+
105+
%call :_compiler_out, &"# %s\n", @file
106+
%call :_compiler_out, &"#include \"regs.h\"\n"
107+
%call :__lex_open, @lex, @file
108+
mov @file, @ret
109+
110+
.loop
111+
%call :_lex_peek, @file, @buf1, @BUFFER_SIZE
112+
mov @token, @ret
113+
114+
eq @token, @TOKEN_EOF
115+
jump? .done
116+
117+
eq @token, @TOKEN_EXTERN
118+
jump? .extern
119+
120+
# We only support int functions for this basic parser
121+
%call :_compile_function_type, @file, @buf1, @BUFFER_SIZE
122+
mov @size, @ret
123+
124+
%call :_lex, @file, @buf1, @BUFFER_SIZE
125+
mov @token, @ret
126+
eq @token, @TOKEN_IDENTIFIER
127+
jump^ .error
128+
129+
%call :_compiler_out, &"# global %s\n:_%s\n", @buf1, @buf1
130+
131+
%call :_lex_peek, @file, 0, 0
132+
eq @ret, '('
133+
jump? .fn
134+
135+
eq @ret, '='
136+
jump? .inited
137+
eq @ret, '['
138+
jump? .equal
139+
140+
%call :_track_global, @buf1, @size
141+
%call :_compiler_out, &" dd 0\n"
142+
%call :_compiler_read_expect, @file, @buf1, @BUFFER_SIZE, ';'
143+
jump .loop
144+
145+
.equal
146+
mov @size, 4
147+
%call :_compiler_read_expect, @file, 0, 0, '['
148+
%call :_compiler_read_expect, @file, 0, 0, ']'
149+
150+
.inited
151+
%call :_track_global, @buf1, @size
152+
%call :_compiler_read_expect, @file, 0, 0, '='
153+
%call :_compile_constant, @file, @buf1, @BUFFER_SIZE, @size
154+
%call :_compiler_read_expect, @file, @buf1, @BUFFER_SIZE, ';'
155+
jump .loop
156+
157+
.extern
158+
# extern (type) (identifier);
159+
%call :_compiler_read_expect, @file, @buf1, @BUFFER_SIZE, @TOKEN_EXTERN
160+
%call :_compile_function_type, @file, @buf1, @BUFFER_SIZE
161+
mov @size, @ret
162+
%call :_lex, @file, @buf1, @BUFFER_SIZE
163+
%call :_track_global, @buf1, @size
164+
%call :_compiler_read_expect, @file, @buf1, @BUFFER_SIZE, ';'
165+
jump .loop
166+
167+
.fn
168+
%call :_track_global, @buf1, @size
169+
%call :_ht_init, :__lex_hash_table_test_key_hash, :__lex_hash_table_test_key_compare
170+
st.d [:_local_symbols], @ret
171+
%call :_compile_function_args, @file, @buf1, @BUFFER_SIZE
172+
%call :_lex_peek, @file, 0, 0
173+
# Abort on forward declarations
174+
eq @ret, ';'
175+
jump? .fn_forward
176+
%call :_compiler_out, &" %%local _carg0\n"
177+
%call :_compiler_out, &" %%local _carg1\n"
178+
%call :_compiler_out, &" %%local _carg2\n"
179+
%call :_compiler_out, &" %%local _carg3\n"
180+
%call :_compiler_out, &" %%local _carg4\n"
181+
%call :_compiler_out, &" %%local _carg5\n"
182+
%call :_compiler_out, &" %%local _carg6\n"
183+
%call :_compiler_out, &" %%local _carg7\n"
184+
mov @tmp0, 0
185+
st.d [:_function_has_saved_stack], @tmp0
186+
%call :_compile_block, @file, @buf1, @BUFFER_SIZE
187+
ld.d @tmp0, [:_function_has_saved_stack]
188+
eq @tmp0, 0
189+
jump? .fn_no_stack
190+
%call :_compiler_out, &" pop @tmp0\n"
191+
%call :_compiler_out, &" mov @sp, @tmp0\n"
192+
.fn_no_stack
193+
%call :_compiler_out, &" %%ret\n"
194+
%call :_teardown_locals
195+
jump .loop
196+
.fn_forward
197+
%call :_compiler_read_expect, @file, @buf1, @BUFFER_SIZE, ';'
198+
%call :_compiler_out, &"# forward\n"
199+
jump .loop
200+
201+
.done
202+
%call :_compiler_out, &"# EOF\n"
203+
mov @ret, 0
204+
%ret
205+
206+
207+
.error
208+
%call :_compiler_out, &"# Error\n"
209+
mov @ret, 1
210+
%ret
File renamed without changes.

bootstrap/bootstrap5/compiler1/lex.c bootstrap5/compiler1/lex.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ int* lex_tokens;
1313
// we may need to compute an expression's value before we store it to the local.
1414
void lex_init(const char* file) {
1515
void* ll = _ll_init();
16-
void* ll_node = _ll_create_node_int("bootstrap/bootstrap4");
16+
void* ll_node = _ll_create_node_int("bootstrap4");
1717
int i;
1818
_ll_insert_head(ll, ll_node);
1919
lex_lexer = __lex_open(__lex_create(ll), file);
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

bootstrap/bootstrap5/lex/tests/lex_io_test.s bootstrap5/lex/tests/lex_io_test.s

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
mov @lex, @ret
1717

1818
# Open a file
19-
%call :__lex_open, @lex, &"bootstrap/bootstrap5/lex/tests/c/test.c"
19+
%call :__lex_open, @lex, &"bootstrap5/lex/tests/c/test.c"
2020
mov @file, @ret
2121

2222
%call :__lex_read, @file
@@ -36,7 +36,7 @@
3636
# Create the include list
3737
%call :_ll_init
3838
mov @ll, @ret
39-
%call :_ll_create_node_int, &"bootstrap/bootstrap5/lex/tests"
39+
%call :_ll_create_node_int, &"bootstrap5/lex/tests"
4040
mov @node, @ret
4141
%call :_ll_insert_head, @ll, @node
4242

@@ -45,7 +45,7 @@
4545
mov @lex, @ret
4646

4747
# Open a file
48-
%call :__lex_open, @lex, &"bootstrap/bootstrap5/lex/tests/c/test.c"
48+
%call :__lex_open, @lex, &"bootstrap5/lex/tests/c/test.c"
4949
mov @file, @ret
5050

5151
%call :_lex_check_read, @file, &"int "

0 commit comments

Comments
 (0)