Browse Source

Implement basic - and *

Although multiplication doesn't work, because the `mul` instruction
seems to be a bit special.
Lucas Stadler 8 years ago
parent
commit
a361ef0cbb
2 changed files with 18 additions and 1 deletions
  1. 10 0
      scm/inc/compiler.scm
  2. 8 1
      scm/inc/tests.scm

+ 10 - 0
scm/inc/compiler.scm

159
     (emit "movl %eax, ~a(%rsp)" si) ; move second arg on the stack
159
     (emit "movl %eax, ~a(%rsp)" si) ; move second arg on the stack
160
     (emit-expr (primcall-operand1 x) (- si wordsize) env)
160
     (emit-expr (primcall-operand1 x) (- si wordsize) env)
161
     (emit "addl ~a(%rsp), %eax" si))
161
     (emit "addl ~a(%rsp), %eax" si))
162
    ((-)
163
     (emit-expr (primcall-operand2 x) si env)
164
     (emit "movl %eax, ~a(%rsp)" si) ; move second arg on the stack
165
     (emit-expr (primcall-operand1 x) (- si wordsize) env)
166
     (emit "subl ~a(%rsp), %eax" si))
167
    ((*)
168
     (emit-expr (primcall-operand2 x) si env)
169
     (emit "movl %eax, ~a(%rsp)" si) ; move second arg on the stack
170
     (emit-expr (primcall-operand1 x) (- si wordsize) env)
171
     (emit "mull ~a(%rsp), %eax" si))
162
    ((cons)
172
    ((cons)
163
     (emit-expr (primcall-operand1 x) si env)
173
     (emit-expr (primcall-operand1 x) si env)
164
     (emit "movl %eax, 0(%rsi)") ; set the car
174
     (emit "movl %eax, 0(%rsi)") ; set the car

+ 8 - 1
scm/inc/tests.scm

106
    ["(+ 0 0)" "0\n"]
106
    ["(+ 0 0)" "0\n"]
107
    ["(+ 41 1)" "42\n"]
107
    ["(+ 41 1)" "42\n"]
108
    ["(+ 1 41)" "42\n"]
108
    ["(+ 1 41)" "42\n"]
109
    ["(+ -10 11)" "1\n"]])
109
    ["(+ -10 11)" "1\n"]
110
111
    ["(- 1 2)" "-1\n"]
112
    ["(- 42 0)" "42\n"]
113
    ["(- 0 42)" "-42\n"]
114
115
    ;["(* 3 4)" "12\n"]
116
    ])
110
117
111
(do-tests "3.5 - local variables"
118
(do-tests "3.5 - local variables"
112
  '[["(let ((x 3) (y 4)) (+ x y))" "7\n"]
119
  '[["(let ((x 3) (y 4)) (+ x y))" "7\n"]