Explorar el Código

Allocate pairs on the heap

Which means there's a heap now.  The pairs can't be printed yet, because
the display thing has to rewritten for that.  (I think there's going to
be a recursive print_value function.)
Lucas Stadler %!s(int64=8) %!d(string=hace) años
padre
commit
28af9e0f3f
Se han modificado 3 ficheros con 25 adiciones y 3 borrados
  1. 9 0
      scm/inc/compiler.scm
  2. 15 2
      scm/inc/driver.c
  3. 1 1
      scm/inc/program.scm

+ 9 - 0
scm/inc/compiler.scm

@ -159,11 +159,20 @@
159 159
     (emit "movl %eax, ~a(%rsp)" si) ; move second arg on the stack
160 160
     (emit-expr (primcall-operand1 x) (- si wordsize) env)
161 161
     (emit "addl ~a(%rsp), %eax" si))
162
    ((cons)
163
     (emit-expr (primcall-operand1 x) si env)
164
     (emit "movl %eax, 0(%rsi)") ; set the car
165
     (emit-expr (primcall-operand2 x) si env)
166
     (emit "movl %eax, 4(%rsi)") ; set the cdr
167
     (emit "movq %rsi, %rax") ; rax = rsi | 1  (cons cell/pair tag)
168
     (emit "orq  $~a, %rax" #b001)
169
     (emit "addq $8,  %rsi")) ; bump rsi
162 170
    ))
163 171
164 172
(define (compile-program x)
165 173
  (display ".globl scheme_entry\n\n")
166 174
  (display "scheme_entry:\n")
167 175
176
  (emit "movq %rax, %rsi") ; store pointer to heap memory
168 177
  (emit-expr x (- wordsize) '())
169 178
  (emit "ret"))

+ 15 - 2
scm/inc/driver.c

@ -1,6 +1,7 @@
1 1
/* a simple driver for scheme_entry */
2 2
3 3
#include <stdio.h>
4
#include <stdlib.h>
4 5
5 6
#define fixnum_mask  3 // 11
6 7
#define fixnum_tag   0 // 00
@ -16,16 +17,28 @@
16 17
17 18
#define empty_list   47 // 00101111
18 19
19
int scheme_entry();
20
#define cons_tag  1 // 001
21
#define cons_mask 7 // 111
22
23
#define MAX_MEMORY (1 << 20)
24
25
int scheme_entry(void *memory);
20 26
21 27
int main(int argc, char **argv) {
22
	int val = scheme_entry();
28
	void *mem = malloc(MAX_MEMORY);
29
	if (mem == NULL) {
30
		perror("malloc");
31
		exit(1);
32
	}
33
	int val = scheme_entry(mem);
23 34
	if ((val & fixnum_mask) == fixnum_tag) {
24 35
		printf("%d\n", val >> fixnum_shift);
25 36
	} else if ((val & char_mask) == char_tag) {
26 37
		printf("#\\%c\n", val >> char_shift);
27 38
	} else if ((val & boolean_mask) == boolean_tag) {
28 39
		printf("#%s\n", (val >> boolean_shift) == 1 ? "t" : "f");
40
	} else if ((val & cons_mask) == cons_tag) {
41
		printf("cons!\n");
29 42
	} else if (val == empty_list) {
30 43
		printf("()\n");
31 44
	} else {

+ 1 - 1
scm/inc/program.scm

@ -1,3 +1,3 @@
1 1
(load "compiler.scm")
2 2
3
(compile-program '(let ((x 3) (y 4)) (if (integer? (+ x y)) 42 #f)))
3
(compile-program '(cons 10 20))