aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-11-04 12:55:25 +0100
committerBad Diode <bd@badd10de.dev>2021-11-04 12:55:25 +0100
commit7b3b52e6307ceda963bd1a6c481baeb720c2beb3 (patch)
tree09222d7a39aa16ad00ccd8abe37327edb8a7c5b7
parent7cbef14e8393475b1e569fdbfff2c46db859d43f (diff)
downloadbdl-7b3b52e6307ceda963bd1a6c481baeb720c2beb3.tar.gz
bdl-7b3b52e6307ceda963bd1a6c481baeb720c2beb3.zip
Change heap register to `r15` from `rsi` to avoid conflicts
-rw-r--r--src/compiler.h120
-rw-r--r--src/x86_64/prelude.asm4
2 files changed, 64 insertions, 60 deletions
diff --git a/src/compiler.h b/src/compiler.h
index 070f24a..5e1bf7d 100644
--- a/src/compiler.h
+++ b/src/compiler.h
@@ -8,21 +8,21 @@
8#define HEAP_SIZE MB(32) 8#define HEAP_SIZE MB(32)
9 9
10// Immediate constants. 10// Immediate constants.
11#define NIL_VAL 47 11#define NIL_VAL 47LU
12#define BOOL_MASK 127 12#define BOOL_MASK 127LU
13#define BOOL_TAG 31 13#define BOOL_TAG 31LU
14#define BOOL_SHIFT 7 14#define BOOL_SHIFT 7LU
15#define TRUE_VAL ((1 << BOOL_SHIFT) | BOOL_TAG) 15#define TRUE_VAL ((1 << BOOL_SHIFT) | BOOL_TAG)
16#define FALSE_VAL ((0 << BOOL_SHIFT) | BOOL_TAG) 16#define FALSE_VAL ((0 << BOOL_SHIFT) | BOOL_TAG)
17#define FIXNUM_MASK 3 17#define FIXNUM_MASK 3LU
18#define FIXNUM_TAG 0 18#define FIXNUM_TAG 0LU
19#define FIXNUM_SHIFT 2 19#define FIXNUM_SHIFT 2LU
20 20
21// Heap allocated objects. 21// Heap allocated objects.
22#define STRING_MASK 7 22#define STRING_MASK 7LU
23#define STRING_TAG 3 23#define STRING_TAG 3LU
24#define PAIR_MASK 7 24#define PAIR_MASK 7LU
25#define PAIR_TAG 1 25#define PAIR_TAG 1LU
26 26
27void compile_object(Object *obj); 27void compile_object(Object *obj);
28void compile_fixnum(Object *obj); 28void compile_fixnum(Object *obj);
@@ -69,7 +69,7 @@ void
69compile_boolean(Object *obj) { 69compile_boolean(Object *obj) {
70 printf(" ;; --> compile_boolean\n"); 70 printf(" ;; --> compile_boolean\n");
71 int is_true = obj->type == OBJ_TYPE_TRUE; 71 int is_true = obj->type == OBJ_TYPE_TRUE;
72 printf(" mov rax, %d\n", (is_true << BOOL_SHIFT) | BOOL_TAG); 72 printf(" mov rax, %zu\n", (is_true << BOOL_SHIFT) | BOOL_TAG);
73 printf(" push rax\n"); 73 printf(" push rax\n");
74 printf(" ;; <-- compile_boolean\n"); 74 printf(" ;; <-- compile_boolean\n");
75} 75}
@@ -77,7 +77,7 @@ compile_boolean(Object *obj) {
77void 77void
78compile_nil(void) { 78compile_nil(void) {
79 printf(" ;; --> compile_nil\n"); 79 printf(" ;; --> compile_nil\n");
80 printf(" mov rax, %d\n", NIL_VAL); 80 printf(" mov rax, NIL_VAL\n");
81 printf(" push rax\n"); 81 printf(" push rax\n");
82 printf(" ;; <-- compile_nil\n"); 82 printf(" ;; <-- compile_nil\n");
83} 83}
@@ -109,25 +109,25 @@ compile_type_predicate(OpType op, Object* args) {
109 printf(" pop rax\n"); 109 printf(" pop rax\n");
110 switch (op) { 110 switch (op) {
111 case OP_IS_NIL: { 111 case OP_IS_NIL: {
112 printf(" cmp rax, %d\n", NIL_VAL); 112 printf(" cmp rax, NIL_VAL\n");
113 } break; 113 } break;
114 case OP_IS_ZERO: { 114 case OP_IS_ZERO: {
115 printf(" cmp rax, 0\n"); 115 printf(" cmp rax, 0\n");
116 } break; 116 } break;
117 case OP_IS_BOOL: { 117 case OP_IS_BOOL: {
118 printf(" and rax, %d\n", BOOL_MASK); 118 printf(" and rax, BOOL_MASK\n");
119 printf(" cmp rax, %d\n", BOOL_TAG); 119 printf(" cmp rax, BOOL_TAG\n");
120 } break; 120 } break;
121 case OP_IS_FIXNUM: { 121 case OP_IS_FIXNUM: {
122 printf(" and rax, %d\n", FIXNUM_MASK); 122 printf(" and rax, FIXNUM_MASK\n");
123 printf(" cmp rax, %d\n", FIXNUM_TAG); 123 printf(" cmp rax, FIXNUM_TAG\n");
124 } break; 124 } break;
125 default: break; 125 default: break;
126 } 126 }
127 printf(" mov rax, 0\n"); 127 printf(" mov rax, 0\n");
128 printf(" sete al\n"); 128 printf(" sete al\n");
129 printf(" shl rax, %d\n", BOOL_SHIFT); 129 printf(" shl rax, BOOL_SHIFT\n");
130 printf(" or rax, %d\n", BOOL_TAG); 130 printf(" or rax, BOOL_TAG\n");
131 printf(" push rax\n"); 131 printf(" push rax\n");
132 printf(" ;; <-- compile_type_predicate\n"); 132 printf(" ;; <-- compile_type_predicate\n");
133} 133}
@@ -137,11 +137,11 @@ compile_not(Object* args) {
137 printf(" ;; --> compile_not\n"); 137 printf(" ;; --> compile_not\n");
138 compile_object(args->head); 138 compile_object(args->head);
139 printf(" pop rax\n"); 139 printf(" pop rax\n");
140 printf(" cmp rax, %d\n", FALSE_VAL); 140 printf(" cmp rax, FALSE_VAL\n");
141 printf(" mov rax, 0\n"); 141 printf(" mov rax, 0\n");
142 printf(" sete al\n"); 142 printf(" sete al\n");
143 printf(" shl rax, %d\n", BOOL_SHIFT); 143 printf(" shl rax, BOOL_SHIFT\n");
144 printf(" or rax, %d\n", BOOL_TAG); 144 printf(" or rax, BOOL_TAG\n");
145 printf(" push rax\n"); 145 printf(" push rax\n");
146 printf(" ;; <-- compile_not\n"); 146 printf(" ;; <-- compile_not\n");
147} 147}
@@ -155,14 +155,14 @@ compile_and(Object *args) {
155 compile_object(args->head); 155 compile_object(args->head);
156 args = args->tail; 156 args = args->tail;
157 printf(" pop rax\n"); 157 printf(" pop rax\n");
158 printf(" cmp rax, %d\n", FALSE_VAL); 158 printf(" cmp rax, FALSE_VAL\n");
159 printf(" je %s\n", lab_false); 159 printf(" je %s\n", lab_false);
160 } 160 }
161 printf(" mov rax, %d\n", TRUE_VAL); 161 printf(" mov rax, TRUE_VAL\n");
162 printf(" push rax\n"); 162 printf(" push rax\n");
163 printf(" jmp %s\n", lab_exit); 163 printf(" jmp %s\n", lab_exit);
164 printf("%s:\n", lab_false); 164 printf("%s:\n", lab_false);
165 printf(" mov rax, %d\n", FALSE_VAL); 165 printf(" mov rax, FALSE_VAL\n");
166 printf(" push rax\n"); 166 printf(" push rax\n");
167 printf("%s:\n", lab_exit); 167 printf("%s:\n", lab_exit);
168 free(lab_false); 168 free(lab_false);
@@ -179,14 +179,14 @@ compile_or(Object *args) {
179 compile_object(args->head); 179 compile_object(args->head);
180 args = args->tail; 180 args = args->tail;
181 printf(" pop rax\n"); 181 printf(" pop rax\n");
182 printf(" cmp rax, %d\n", FALSE_VAL); 182 printf(" cmp rax, FALSE_VAL\n");
183 printf(" jne %s\n", lab_true); 183 printf(" jne %s\n", lab_true);
184 } 184 }
185 printf(" mov rax, %d\n", FALSE_VAL); 185 printf(" mov rax, FALSE_VAL\n");
186 printf(" push rax\n"); 186 printf(" push rax\n");
187 printf(" jmp %s\n", lab_exit); 187 printf(" jmp %s\n", lab_exit);
188 printf("%s:\n", lab_true); 188 printf("%s:\n", lab_true);
189 printf(" mov rax, %d\n", TRUE_VAL); 189 printf(" mov rax, TRUE_VAL\n");
190 printf(" push rax\n"); 190 printf(" push rax\n");
191 printf("%s:\n", lab_exit); 191 printf("%s:\n", lab_exit);
192 free(lab_true); 192 free(lab_true);
@@ -224,11 +224,11 @@ compile_cmp_list(OpType op, Object* args) {
224 printf(" push rcx\n"); 224 printf(" push rcx\n");
225 } 225 }
226 printf(" pop rcx\n"); 226 printf(" pop rcx\n");
227 printf(" mov rax, %d\n", TRUE_VAL); 227 printf(" mov rax, TRUE_VAL\n");
228 printf(" push rax\n"); 228 printf(" push rax\n");
229 printf(" jmp %s\n", lab_exit); 229 printf(" jmp %s\n", lab_exit);
230 printf("%s:\n", lab_false); 230 printf("%s:\n", lab_false);
231 printf(" mov rax, %d\n", FALSE_VAL); 231 printf(" mov rax, FALSE_VAL\n");
232 printf(" push rax\n"); 232 printf(" push rax\n");
233 printf("%s:\n", lab_exit); 233 printf("%s:\n", lab_exit);
234 free(lab_false); 234 free(lab_false);
@@ -250,25 +250,25 @@ compile_arithmetic_list(OpType op, Object* args) {
250 case OP_ADD: { printf(" add rax, rcx\n"); } break; 250 case OP_ADD: { printf(" add rax, rcx\n"); } break;
251 case OP_SUB: { printf(" sub rax, rcx\n"); } break; 251 case OP_SUB: { printf(" sub rax, rcx\n"); } break;
252 case OP_MUL: { 252 case OP_MUL: {
253 printf(" sar rax, %d\n", FIXNUM_SHIFT); 253 printf(" sar rax, FIXNUM_SHIFT\n");
254 printf(" sar rcx, %d\n", FIXNUM_SHIFT); 254 printf(" sar rcx, FIXNUM_SHIFT\n");
255 printf(" mul rcx\n"); 255 printf(" mul rcx\n");
256 printf(" shl rax, %d\n", FIXNUM_SHIFT); 256 printf(" shl rax, FIXNUM_SHIFT\n");
257 } break; 257 } break;
258 case OP_DIV: { 258 case OP_DIV: {
259 printf(" sar rax, %d\n", FIXNUM_SHIFT); 259 printf(" sar rax, FIXNUM_SHIFT\n");
260 printf(" sar rcx, %d\n", FIXNUM_SHIFT); 260 printf(" sar rcx, FIXNUM_SHIFT\n");
261 printf(" mov rdx, 0\n"); 261 printf(" mov rdx, 0\n");
262 printf(" div rcx\n"); 262 printf(" div rcx\n");
263 printf(" shl rax, %d\n", FIXNUM_SHIFT); 263 printf(" shl rax, FIXNUM_SHIFT\n");
264 } break; 264 } break;
265 case OP_MOD: { 265 case OP_MOD: {
266 printf(" sar rax, %d\n", FIXNUM_SHIFT); 266 printf(" sar rax, FIXNUM_SHIFT\n");
267 printf(" sar rcx, %d\n", FIXNUM_SHIFT); 267 printf(" sar rcx, FIXNUM_SHIFT\n");
268 printf(" mov rdx, 0\n"); 268 printf(" mov rdx, 0\n");
269 printf(" div rcx\n"); 269 printf(" div rcx\n");
270 printf(" mov rax, rdx\n"); 270 printf(" mov rax, rdx\n");
271 printf(" shl rax, %d\n", FIXNUM_SHIFT); 271 printf(" shl rax, FIXNUM_SHIFT\n");
272 } break; 272 } break;
273 default: break; 273 default: break;
274 } 274 }
@@ -285,25 +285,25 @@ compile_cons(Object *obj) {
285 compile_object(obj->tail->head); 285 compile_object(obj->tail->head);
286 printf(" pop rdx\n"); 286 printf(" pop rdx\n");
287 printf(" pop rax\n"); 287 printf(" pop rax\n");
288 printf(" mov [rsi], rax\n"); 288 printf(" mov [r15], rax\n");
289 printf(" mov [rsi + 8], rdx\n"); 289 printf(" mov [r15 + 8], rdx\n");
290 290
291 // Push memory address of cons cell. 291 // Push memory address of cons cell.
292 printf(" mov rax, rsi\n"); 292 printf(" mov rax, r15\n");
293 printf(" or rax, %ld\n", PAIR_TAG); 293 printf(" or rax, %zu\n", PAIR_TAG);
294 printf(" push rax\n"); 294 printf(" push rax\n");
295 295
296 // Bump allocation register. 296 // Bump allocation register.
297 printf(" add rsi, 16\n"); 297 printf(" add r15, 16\n");
298 printf(" ;; <-- compile_cons\n"); 298 printf(" ;; <-- compile_cons\n");
299} 299}
300 300
301void 301void
302compile_car(Object *obj) { 302compile_car(Object *obj) {
303 printf(" ;; <-- compile_car\n"); 303 printf(" ;; --> compile_car\n");
304 compile_object(obj->head); 304 compile_object(obj->head);
305 printf(" pop rax\n"); 305 printf(" pop rax\n");
306 printf(" and eax, %ld\n", ~PAIR_MASK); 306 printf(" and rax, %zu\n", ~PAIR_MASK);
307 printf(" mov rdx, [rax]\n"); 307 printf(" mov rdx, [rax]\n");
308 printf(" push rdx\n"); 308 printf(" push rdx\n");
309 printf(" ;; <-- compile_car\n"); 309 printf(" ;; <-- compile_car\n");
@@ -311,10 +311,10 @@ compile_car(Object *obj) {
311 311
312void 312void
313compile_cdr(Object *obj) { 313compile_cdr(Object *obj) {
314 printf(" ;; <-- compile_cdr\n"); 314 printf(" ;; --> compile_cdr\n");
315 compile_object(obj->head); 315 compile_object(obj->head);
316 printf(" pop rax\n"); 316 printf(" pop rax\n");
317 printf(" and eax, %ld\n", ~PAIR_MASK); 317 printf(" and rax, %zu\n", ~PAIR_MASK);
318 printf(" mov rdx, [rax + 8]\n"); 318 printf(" mov rdx, [rax + 8]\n");
319 printf(" push rdx\n"); 319 printf(" push rdx\n");
320 printf(" ;; <-- compile_cdr\n"); 320 printf(" ;; <-- compile_cdr\n");
@@ -379,7 +379,7 @@ compile_if(Object *obj) {
379 char *lab_false = generate_label(); 379 char *lab_false = generate_label();
380 compile_object(obj->condition); 380 compile_object(obj->condition);
381 printf(" pop rax\n"); 381 printf(" pop rax\n");
382 printf(" cmp rax, %d\n", FALSE_VAL); 382 printf(" cmp rax, FALSE_VAL\n");
383 printf(" je %s\n", lab_false); 383 printf(" je %s\n", lab_false);
384 compile_object(obj->expr_true); 384 compile_object(obj->expr_true);
385 if (obj->expr_false != NULL) { 385 if (obj->expr_false != NULL) {
@@ -410,14 +410,18 @@ compile_object(Object *obj) {
410 410
411void 411void
412compile(Root *roots) { 412compile(Root *roots) {
413 printf("%%define NIL_VAL %d\n", NIL_VAL); 413 printf("%%define NIL_VAL %zu\n", NIL_VAL);
414 printf("%%define BOOL_MASK %d\n", BOOL_MASK); 414 printf("%%define BOOL_MASK %zu\n", BOOL_MASK);
415 printf("%%define BOOL_TAG %d\n", BOOL_TAG); 415 printf("%%define BOOL_TAG %zu\n", BOOL_TAG);
416 printf("%%define BOOL_SHIFT %d\n", BOOL_SHIFT); 416 printf("%%define BOOL_SHIFT %zu\n", BOOL_SHIFT);
417 printf("%%define FIXNUM_MASK %d\n", FIXNUM_MASK); 417 printf("%%define FIXNUM_MASK %zu\n", FIXNUM_MASK);
418 printf("%%define FIXNUM_TAG %d\n", FIXNUM_TAG); 418 printf("%%define FIXNUM_TAG %zu\n", FIXNUM_TAG);
419 printf("%%define FIXNUM_SHIFT %d\n", FIXNUM_SHIFT); 419 printf("%%define FIXNUM_SHIFT %zu\n", FIXNUM_SHIFT);
420 printf("%%define HEAP_SIZE %ld\n", HEAP_SIZE); 420 printf("%%define PAIR_MASK %zu\n", PAIR_MASK);
421 printf("%%define PAIR_TAG %zu\n", PAIR_TAG);
422 printf("%%define STRING_MASK %zu\n", STRING_MASK);
423 printf("%%define STRING_TAG %zu\n", STRING_TAG);
424 printf("%%define HEAP_SIZE %zu\n", HEAP_SIZE);
421 printf("\n"); 425 printf("\n");
422 emit_file(PRELUDE_FILE); 426 emit_file(PRELUDE_FILE);
423 for (size_t i = 0; i < array_size(roots); i++) { 427 for (size_t i = 0; i < array_size(roots); i++) {
diff --git a/src/x86_64/prelude.asm b/src/x86_64/prelude.asm
index e57e820..3ad33f7 100644
--- a/src/x86_64/prelude.asm
+++ b/src/x86_64/prelude.asm
@@ -82,7 +82,7 @@ display:
82 cmp rax, NIL_VAL 82 cmp rax, NIL_VAL
83 je display_end 83 je display_end
84 84
85 ; ;; is boolean? 85 ;; is boolean?
86 mov rax, rdi 86 mov rax, rdi
87 and rax, BOOL_MASK 87 and rax, BOOL_MASK
88 cmp rax, BOOL_TAG 88 cmp rax, BOOL_TAG
@@ -100,7 +100,7 @@ display_end:
100global _start 100global _start
101_start: 101_start:
102 ;; point `rdi` to the start of the heap. 102 ;; point `rdi` to the start of the heap.
103 mov rsi, bdl_heap 103 mov r15, bdl_heap
104 104
105 ;; make sure the last element in the stack is the nil value. 105 ;; make sure the last element in the stack is the nil value.
106 push NIL_VAL 106 push NIL_VAL