Просмотр исходного кода

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
Родитель
Сommit
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
	./scheme
2
	./scheme
3
3
4
scheme: scheme.s driver.c
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
scheme-static: scheme.s driver.c
7
scheme-static: scheme.s driver.c
8
	gcc -O3 -static scheme.s driver.c -o $@
8
	gcc -O3 -static scheme.s driver.c -o $@

+ 13 - 2
scm/inc/compiler.scm

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

+ 1 - 1
scm/inc/program.scm

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