Skip to content

Commit

Permalink
Add quotient and remainder (#53)
Browse files Browse the repository at this point in the history
* multiplication and division

* fix division with cqo

* rename quotient remainder

* add tests and modify interpreter

* fix parenthesis

* rename multiply to *

* take out *

* fix blank spaces
  • Loading branch information
minghui-liu authored Feb 23, 2021
1 parent 582ed27 commit e553e88
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 4 deletions.
6 changes: 6 additions & 0 deletions a86/ast.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@
(instruct Mov (dst src) check:src-dest)
(instruct Add (dst src) check:arith)
(instruct Sub (dst src) check:arith)
(instruct IMul (dst src) check:arith)
(instruct IDiv (dst) check:arith-div)
(instruct Div (dst) check:arith-div)
(instruct Cmp (a1 a2) check:src-dest)
(instruct Jmp (x) check:target)
Expand All @@ -164,6 +166,7 @@
(instruct Pop (a1) check:register)
(instruct Lea (dst x) check:lea)
(instruct Bsr (dst src) check:src-dest)
(instruct Cqo () check:none)

(instruct Offset (r i) check:offset)
(instruct Extern (x) check:label-symbol)
Expand Down Expand Up @@ -191,6 +194,9 @@
(Mov? x)
(Add? x)
(Sub? x)
(IMul? x)
(IDiv? x)
(Cqo? x)
(Div? x)
(Cmp? x)
(Jmp? x)
Expand Down
9 changes: 9 additions & 0 deletions a86/printer.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
(define (instr->string i)
(match i
[(Ret) (string-append tab "ret")]
[(Cqo) (string-append tab "cqo")]
[(Global x) (string-append tab "global " (label-symbol->string x))]
[(Default x) (string-append tab "default " (symbol->string x))]
[(Section x) (string-append tab "section " (symbol->string x))]
Expand All @@ -72,6 +73,13 @@
(string-append tab "add "
(arg->string a1) ", "
(arg->string a2))]
[(IMul a1 a2)
(string-append tab "imul "
(arg->string a1) ", "
(arg->string a2))]
[(IDiv a1)
(string-append tab "idiv "
(arg->string a1) ", ")]
[(Div a1)
(string-append tab "div "
(arg->string a1) ", ")]
Expand Down Expand Up @@ -142,6 +150,7 @@
(arg->string a1) ", "
(arg->string a2))]))


(define (comment->string c)
(match c
[(% s) (string-append (make-string 32 #\space) "; " s)]
Expand Down
4 changes: 2 additions & 2 deletions villain/ast.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
;; | 'box | 'car | 'cdr | 'unbox
;; | 'string-length | 'string? | make-string
;; | 'empty? |'vector? |'vector-length |list?
;; type Op2 = '+ | '- | 'eq?
;; | 'cons | 'string-ref | 'make-vector
;; type Op2 = '+ | '- | * | quotient| remainder
;; | 'eq? | 'cons | 'string-ref | 'make-vector
;; | 'vector-ref |'vector-set!
;; type Op3 = 'string-set!
;; type Op4 = 'vector-cas!
Expand Down
22 changes: 22 additions & 0 deletions villain/compile.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,28 @@
(assert-integer rax c)
(Sub r8 rax)
(Mov rax r8))]
['quotient
(seq (Mov r8 rax)
(Pop rax)
(assert-integer r8 c)
(assert-integer rax c)
(Cmp r8 (imm->bits 0))
(Je (error-label c))
(Cqo)
(IDiv r8)
(Sal rax int-shift)
)]
['remainder
(seq (Mov r8 rax)
(Pop rax)
(assert-integer r8 c)
(assert-integer rax c)
(Cmp r8 (imm->bits 0))
(Je (error-label c))
(Cqo)
(IDiv r8)
(Mov rax rdx)
)]
['<=
(let ((leq-true (gensym 'leq)))
(seq (Pop r8)
Expand Down
3 changes: 3 additions & 0 deletions villain/interp-prims.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
(match (list p v1 v2)
[(list '+ (? integer?) (? integer?)) (+ v1 v2)]
[(list '- (? integer?) (? integer?)) (- v1 v2)]
[(list '* (? integer?) (? integer?)) (* v1 v2)]
[(list 'quotient (? integer?) (? integer?)) (quotient v1 v2)]
[(list 'remainder (? integer?) (? integer?)) (remainder v1 v2)]
[(list '<= (? integer?) (? integer?)) (<= v1 v2)]
[(list 'eq? v1 v2) (eqv? v1 v2)]
[(list 'cons v1 v2) (cons v1 v2)]
Expand Down
2 changes: 1 addition & 1 deletion villain/lib/math.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#f)
#f))

; multiplication
; multiplication
(define (*/2 x y)
(if (zero? y)
0
Expand Down
2 changes: 1 addition & 1 deletion villain/parse.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
symbol->string string->symbol symbol?
vector? vector-length))
(define op2
'(+ - eq? cons string-ref make-string <=
'(+ - quotient remainder eq? cons string-ref make-string <=
make-vector vector-ref
fl+ fl- fl<= fl=))

Expand Down
3 changes: 3 additions & 0 deletions villain/test/test-runner.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,13 @@

; Standard library: math.rkt
(check-equal? (run '(* 1 2 3 4)) 24)
(check-equal? (run '(* -2 3 4)) -24)
(check-equal? (run '(* 1 (* 2 1) 2 2)) 8)
(check-equal? (run '(let ((x (+ 1 2)))
(let ((z (* 1 x)))
(* x z)))) 9)
(check-equal? (run '(quotient -6 3)) -2)
(check-equal? (run '(remainder -11 3)) -2)

; Standard library: string.rkt
(check-equal? (run '(string)) "")
Expand Down

0 comments on commit e553e88

Please sign in to comment.