浏览代码

Add numbers (!)

This is the point where we get confused about 32 vs 64 bits.  We'll
handle that... later, I guess?
Lucas Stadler 8 年之前
父节点
当前提交
ebca191458
共有 3 个文件被更改,包括 15 次插入4 次删除
  1. 1 1
      scm/inc/Makefile
  2. 13 2
      scm/inc/compiler.scm
  3. 1 1
      scm/inc/program.scm

+ 1 - 1
scm/inc/Makefile

@ -2,7 +2,7 @@ run-scheme: scheme
2 2
	./scheme
3 3
4 4
scheme: scheme.s driver.c
5
	gcc -O3 -g scheme.s driver.c -o $@
5
	gcc -g scheme.s driver.c -o $@
6 6
7 7
scheme-static: scheme.s driver.c
8 8
	gcc -O3 -static scheme.s driver.c -o $@

+ 13 - 2
scm/inc/compiler.scm

@ -5,6 +5,8 @@
5 5
  (display (apply format instr args))
6 6
  (display "\n"))
7 7
8
(define wordsize 4)
9
8 10
(define fixnum-shift 2)
9 11
(define char-shift 8)
10 12
(define boolean-shift 7)
@ -32,6 +34,9 @@
32 34
(define (primcall-operand1 x)
33 35
  (cadr x))
34 36
37
(define (primcall-operand2 x)
38
  (caddr x))
39
35 40
(define (emit-compare)
36 41
  (emit "movl $0,  %eax")                ; zero %eax to put the result of the comparison into
37 42
  (emit "sete %al")                      ; set low byte of %eax to 1 if cmp succeeded
@ -77,11 +82,17 @@
77 82
     (emit-expr (primcall-operand1 x) si)
78 83
     (emit "andl $~a, %eax" #b1111111)
79 84
     (emit "cmpl $~a, %eax" #b0011111)
80
     (emit-compare))))
85
     (emit-compare))
86
    ((+)
87
     (emit-expr (primcall-operand2 x) si)
88
     (emit "movl %eax, ~a(%rbp)" si) ; move second arg on the stack
89
     (emit-expr (primcall-operand1 x) (- si wordsize))
90
     (emit "addl ~a(%rbp), %eax" si))
91
    ))
81 92
82 93
(define (compile-program x)
83 94
  (display ".globl scheme_entry\n\n")
84 95
  (display "scheme_entry:\n")
85 96
86
  (emit-expr x 0)
97
  (emit-expr x (- wordsize))
87 98
  (emit "ret"))

+ 1 - 1
scm/inc/program.scm

@ -1,3 +1,3 @@
1 1
(load "compiler.scm")
2 2
3
(compile-program '(null? 0))
3
(compile-program '(+ 1 2))