Compare commits

..

2 Commits

Author SHA1 Message Date
Enrico Lumetti 04f48fe0ee wip rework 2024-08-13 21:31:29 +02:00
Enrico Lumetti a858cbee76 RInt -> Lint 2024-01-18 09:53:11 +01:00
30 changed files with 221 additions and 205 deletions

9
.kakrc Normal file
View File

@ -0,0 +1,9 @@
hook -once -group eoc global KakBegin .* %{
repl-buffer-new racket -i
repl-buffer-send-text "(require xrepl)
"
%}
define-command xrepl-enter %{
repl-buffer-send-text ",en %val{buffile}"
}

View File

@ -4,3 +4,5 @@ Tracking the [Essentials of Compilation](https://jeapostrophe.github.io/courses/
Requires `racket`.
The compiler emits aarch64 assembly; I use the aarch64 linux cross-compiler and qemu to run them.
## Runnin tests

28
all-tests.rkt Normal file
View File

@ -0,0 +1,28 @@
#lang racket
(provide all-tests)
(require rackunit)
(require "lint/test-lint.rkt")
(require "lvar/test-lvar.rkt")
(require "lvar/test-cvar.rkt")
(require "lvar/test-uniquify.rkt")
(require "lvar/test-remove-complex-opera.rkt")
;(require "test-explicate-control.rkt")
(require "lvar/test-cvar-to-bril.rkt")
;(require "test-select-instr.rkt")
;(require "test-allocate-regs.rkt")
(define all-tests
(test-suite
"All tests"
lint-tests
lvar-tests
cvar-tests
uniquify-tests
remove-complex-opera-tests
;explicate-control-tests
cvar-to-bril-tests))
;select-instr-tests
;allocate-regs-tests))

View File

@ -2,7 +2,7 @@
(require racket/fixnum)
(provide Int Prim Program interp-RInt)
(provide Int Prim Program interp-Lint)
(struct Int (value))
(struct Prim (op args))
@ -24,6 +24,6 @@
[(Prim '- (list e1 e2)) (fx- (interp-exp e1) (interp-exp e2))]
[(Prim '+ (list e1 e2)) (fx+ (interp-exp e1) (interp-exp e2))]))
(define (interp-RInt p)
(define (interp-Lint p)
(match p
[(Program '() body) (interp-exp body)]))

View File

@ -1,10 +1,10 @@
#lang racket
(provide rint-tests)
(provide lint-tests)
(require rackunit)
(require "test-util.rkt")
(require "../rint.rkt")
(require "../test-util.rkt")
(require "lint.rkt")
(define eight (Int 8))
(define rd (Prim 'read '()))
@ -12,19 +12,19 @@
(define ast1.1 (Prim '+ (list rd neg-eight)))
(define program (Program '() ast1.1))
(define rint-tests
(define lint-tests
(test-suite
"RInt interpretation tests"
"Lint interpretation tests"
(test-case
"program with input"
(check-equal?
-5
(with-input-from-num-list '(3)
(lambda () (interp-RInt program)))))
(lambda () (interp-Lint program)))))
(test-case
"simple difference"
(check-equal?
-3
(interp-RInt (Program '() (Prim '- (list (Int 5) (Int 8)))))))))
(interp-Lint (Program '() (Prim '- (list (Int 5) (Int 8)))))))))

View File

@ -36,20 +36,20 @@
(define (generate-bril-instrs dest expr)
(match expr
[(Int n)
(list (bril:ConstantInstr dest (bril:Type 'int) n))]
(list (bril:ConstantInstr dest 'int n))]
[(Prim '+ (list (Var v1) (Var v2)))
(list (bril:ValueInstr 'add dest (bril:Type 'int)
(list (bril:ValueInstr 'add dest 'int
(list (symbol->string v1)
(symbol->string v2))
'() '()))]
[(Prim '- (list (Var v1)))
(list (bril:ConstantInstr dest (bril:Type 'int) 0)
(bril:ValueInstr 'sub dest (bril:Type 'int)
(list (bril:ConstantInstr dest 'int 0)
(bril:ValueInstr 'sub dest 'int
(list dest
(symbol->string v1))
'() '()))]
[(Prim '- (list (Var v1) (Var v2)))
(list (bril:ValueInstr 'sub dest (bril:Type 'int)
(list (bril:ValueInstr 'sub dest 'int
(list (symbol->string v1)
(symbol->string v2))
'() '()))]))

View File

@ -2,7 +2,7 @@
(provide Int Prim Var Var-name Assign Seq Return CProgram interp-CVar% interp-CVar)
(require "rvar.rkt")
(require "lvar.rkt")
(require racket/dict)
(require racket/struct)
@ -13,7 +13,7 @@
(struct Return (exp) #:transparent)
(define interp-CVar%
(class interp-RVar-class
(class interp-LVar-class
(super-new)
(define/public ((interp-stmt env) stmt)

View File

@ -3,7 +3,7 @@
(provide explicate-control)
(require racket/fixnum)
(require "rvar.rkt")
(require "lvar.rkt")
(require "cvar.rkt")
(define (explicate-control program)

View File

@ -1,6 +1,6 @@
#lang racket
(provide Int Prim Var Var-name Let Program interp-RVar-class interp-RVar)
(provide Int Prim Var Var-name Let Program interp-LVar-class interp-LVar)
(require racket/fixnum)
(require racket/dict)
@ -13,7 +13,7 @@
(struct Program (info body) #:transparent)
(define interp-RVar-class
(define interp-LVar-class
(class object%
(super-new)
@ -44,5 +44,5 @@
(dict-ref env s)
(error "Symbol " s " not found")))
(define (interp-RVar program)
(send (new interp-RVar-class) interp-program program))
(define (interp-LVar program)
(send (new interp-LVar-class) interp-program program))

View File

@ -3,7 +3,7 @@
; converts the program in monadic normal form
(provide remove-complex-opera* remove-complex-opera*-2)
(require "rvar.rkt")
(require "lvar.rkt")
; find number of the last variable named tmp.n
(define (find-tmp-last p)

View File

@ -3,11 +3,11 @@
(provide cvar-to-bril-tests)
(require rackunit)
(require "../cvar-to-bril.rkt")
(require "../uniquify.rkt")
(require "../remove-complex-oper.rkt")
(require "../explicate-control.rkt")
(require "../rvar.rkt")
(require "cvar-to-bril.rkt")
(require "uniquify.rkt")
(require "remove-complex-oper.rkt")
(require "explicate-control.rkt")
(require "lvar.rkt")
(require bril/interpreter)
(define listings
@ -27,5 +27,5 @@
"CVar to bril testsuite"
(test-case "semantics preservation under interpretation"
(for ([program listings])
(check-equal? (interp-RVar program)
(check-equal? (interp-LVar program)
(evaluate-bril-main (pass program)))))))

View File

@ -3,8 +3,8 @@
(provide cvar-tests)
(require rackunit)
(require "test-util.rkt")
(require "../cvar.rkt")
(require "../test-util.rkt")
(require "cvar.rkt")
(define seq-1
(Return (Int 0)))

View File

@ -4,7 +4,7 @@
(require "uniquify.rkt")
(require "remove-complex-oper.rkt")
(require "explicate-control.rkt")
(require "rvar.rkt")
(require "lvar.rkt")
(require "cvar.rkt")
(define programs

View File

@ -1,16 +1,16 @@
#lang racket
(provide rvar-tests)
(provide lvar-tests)
(require rackunit)
(require "../rvar.rkt")
(require "lvar.rkt")
(define (interp-exp env e)
((send (new interp-RVar-class) interp-exp env) e))
((send (new interp-LVar-class) interp-exp env) e))
(define rvar-tests
(define lvar-tests
(test-suite
"RVar interpretation testsuite"
"LVar interpretation testsuite"
(check-equal?
(let ([env `((a . 1) (b . 2))])
@ -44,7 +44,7 @@
6)
(check-equal?
(interp-RVar (Program `() (Let 'a
(interp-LVar (Program `() (Let 'a
(Prim '+ (list (Int 1) (Int 2)))
(Prim '+ (list (Var 'a) (Int 3))))))
6)))

View File

@ -0,0 +1,136 @@
#lang racket
(provide remove-complex-opera-tests)
(require rackunit)
(require "../test-util.rkt")
(require "uniquify.rkt")
(require "remove-complex-oper.rkt")
(require/expose "remove-complex-oper.rkt" (find-tmp-last rco-exp rco-arg))
(require "lvar.rkt")
(define (pass p)
(remove-complex-opera* (uniquify p)))
(define (pass-2 p)
(remove-complex-opera*-2 (uniquify p)))
(define programs
(list
(Program '()
(Prim '- (list (Int 20))))
(Program '()
(Prim '- (list (Prim '- (list (Int 20))))))
(Program '()
(Prim '- (list (Prim '- (list (Prim '- (list (Int 20))))))))
(Program '() (Prim '+ (list (Int 3) (Prim '- (list (Int 20))))))
(Program '()
(Prim '+ (list (Prim '+ (list (Int 3) (Int 2)))
(Prim '+ (list (Int 4) (Int 5))))))
(Program '() (Let 'x (Int 1) (Var 'x)))
(Program '() (Let 'x (Prim '+ (list (Prim '- (list (Int 2)))
(Int 3)))
(Prim '+ (list (Var 'x)
(Prim '+ (list (Int 2) (Int 3)))))))
(Program '() (Prim '+
(list (Let 'x
(Prim '+
(list (Prim '- (list (Int 1)))
(Int 2)))
(Prim '+ (list (Var 'x) (Int 2))))
(Prim '+ (list (Int 4) (Int 5))))))
(Program '()
(Let 'a (Int 42)
(Let 'b (Var 'a)
(Var 'b))))
(Program '() (Let 'tmp (Prim '- (list (Int 1))) (Var 'tmp)))
(Program '() (Prim '-
(list (Let 'x (Int 1) (Var 'x)))))
(Program '() (Let 'x (Let 'x (Int 1) (Var 'x)) (Prim '+ (list (Int 2) (Var 'x)))))
(Program '()
(Let 'y (Let 'x (Int 20)
(Prim '+ (list (Var 'x)
(Let 'x (Int 22) (Var 'x)))))
(Var 'y)))
(Program '() ; - pos 13
(Prim '+ (list (Prim 'read '())
(Prim 'read '()))))
(Program '()
(Prim '+ (list (Let 'x (Int 1)
(Let 'x (Int 2)
(Int 2)))
(Let 'x (Int 2)
(Var 'x)))))))
(define inputs
(let ([empty-inputs (build-list (length programs) (lambda (_) '()))])
(list-set empty-inputs 13 '(2 3))))
(define remove-complex-opera-tests
(test-suite
"Remove Complex Opera* Testsuite"
(test-case
"find-tmp-last"
(check-equal?
(find-tmp-last (Program '()
(Let 'tmp.0 (Let 'x (Int 20)
(Prim '+ (list (Var 'x)
(Let 'tmp.1 (Int 22)
(Var 'tmp.1)))))
(Var 'tmp.0))))
1)
(check-equal?
(find-tmp-last (Program '()
(Let 'tmp.x (Let 'x (Int 20)
(Prim '+ (list (Var 'x)
(Let 'tmp.y (Int 22)
(Var 'tmp.y)))))
(Var 'tmp.x))))
-1)
(test-case
"rco-arg"
(let-values ([(a b c) (rco-arg (Prim '- (list (Int 20))) -1)])
(check-equal? a (Var 'tmp.0))))
(test-case
"remove-complex-opera*-2"
(check-equal?
(pass-2 (Program '() (Int 20)))
(Program '() (Let 'tmp.0 (Int 20) (Var 'tmp.0))))
(check-equal?
(pass-2 (Program '() (Let 'x (Int 20) (Var 'x))))
(Program '() (Let 'x.1 (Int 20) (Var 'x.1))))
(check-equal?
(pass-2 (Program '() (Let 'x (Int 20) (Int 40))))
(Program '() (Let 'x.1 (Int 20) (Let 'tmp.0 (Int 40) (Var 'tmp.0)))))
(check-equal?
(pass-2 (Program '() (Let 'x (Int 20) (Let 'y (Int 40) (Prim '+ (list (Var 'y) (Int 1)))))))
(Program '() (Let 'x.1 (Int 20)
(Let 'y.1 (Int 40)
(Let 'tmp.0 (Int 1)
(Let 'tmp.1 (Prim '+ (list (Var 'y.1) (Var 'tmp.0)))
(Var 'tmp.1)))))))
(check-equal?
(pass-2 (Program '() (Let 'x (Let 'y (Int 40) (Prim '+ (list (Var 'y) (Int 1)))) (Var 'x))))
(Program '() (Let 'x.1 (Let 'y.1 (Int 40)
(Let 'tmp.0 (Int 1)
(Prim '+ (list (Var 'y.1) (Var 'tmp.0)))))
(Var 'x.1))))
(check-equal?
(pass-2 (list-ref programs 0))
(Program '() (Let 'tmp.0 (Int 20) (Let 'tmp.1 (Prim '- (list (Var 'tmp.0))) (Var 'tmp.1))))))
(test-case
"interpretation after pass"
(begin
(for ([program programs]
[input-list inputs])
(begin
(check-equal? (with-input-from-num-list input-list
(lambda () (interp-LVar program)))
(with-input-from-num-list input-list
(lambda () (interp-LVar (pass program)))))
(check-equal? (with-input-from-num-list input-list
(lambda () (interp-LVar program)))
(with-input-from-num-list input-list
(lambda () (interp-LVar (pass-2 program))))))))))))

View File

@ -4,9 +4,9 @@
(require rackunit)
(require "../rvar.rkt")
(require "../uniquify.rkt")
(require/expose "../uniquify.rkt" (uniquify-exp))
(require "lvar.rkt")
(require "uniquify.rkt")
(require/expose "uniquify.rkt" (uniquify-exp))
; returns both the resulting symtable and the uniquified program
(define (list-uniquify-exp symtable)
@ -90,5 +90,5 @@
(test-case
"Uniquify interpretation"
(for ([program (list p1 p2)])
(check-equal? (interp-RVar program)
(interp-RVar (uniquify program)))))))
(check-equal? (interp-LVar program)
(interp-LVar (uniquify program)))))))

View File

@ -2,7 +2,7 @@
(provide uniquify)
(require "rvar.rkt")
(require "lvar.rkt")
(define (uniquify p)
(let-values ([(_ res) (uniquify-ret-symtable p)])

View File

View File

0
old/emit-aarch64-asm.rkt Normal file
View File

View File

@ -1,9 +1,9 @@
#lang racket
(require rackunit rackunit/text-ui)
(require "tests/all-tests.rkt")
(require "complete-tests.rkt")
(require "all-tests.rkt")
;(require "complete-tests.rkt")
(run-tests all-tests)
(run-tests complete-tests)
;(run-tests complete-tests)

View File

@ -1,28 +0,0 @@
#lang racket
(provide all-tests)
(require rackunit)
(require "test-rint.rkt")
(require "test-rvar.rkt")
(require "test-cvar.rkt")
(require "test-uniquify.rkt")
;(require "test-remove-complex-opera.rkt")
;(require "test-explicate-control.rkt")
(require "test-cvar-to-bril.rkt")
(require "test-select-instr.rkt")
(require "test-allocate-regs.rkt")
(define all-tests
(test-suite
"All tests"
rint-tests
rvar-tests
cvar-tests
uniquify-tests
;remove-complex-opera-tests
;explicate-control-tests
cvar-to-bril-tests
select-instr-tests
allocate-regs-tests))

View File

@ -1,131 +0,0 @@
#lang racket
(provide remove-complex-opera-tests)
(require rackunit)
(require "test-util.rkt")
(require "uniquify.rkt")
(require "remove-complex-oper.rkt")
(require/expose "remove-complex-oper.rkt" (find-tmp-last rco-exp rco-arg))
(require "rvar.rkt")
(test-eq
(find-tmp-last (Program '()
(Let 'tmp.0 (Let 'x (Int 20)
(Prim '+ (list (Var 'x)
(Let 'tmp.1 (Int 22)
(Var 'tmp.1)))))
(Var 'tmp.0))))
1)
(test-eq
(find-tmp-last (Program '()
(Let 'tmp.x (Let 'x (Int 20)
(Prim '+ (list (Var 'x)
(Let 'tmp.y (Int 22)
(Var 'tmp.y)))))
(Var 'tmp.x))))
-1)
(define programs
(list
(Program '()
(Prim '- (list (Int 20))))
(Program '()
(Prim '- (list (Prim '- (list (Int 20))))))
(Program '()
(Prim '- (list (Prim '- (list (Prim '- (list (Int 20))))))))
(Program '() (Prim '+ (list (Int 3) (Prim '- (list (Int 20))))))
(Program '()
(Prim '+ (list (Prim '+ (list (Int 3) (Int 2)))
(Prim '+ (list (Int 4) (Int 5))))))
(Program '() (Let 'x (Int 1) (Var 'x)))
(Program '() (Let 'x (Prim '+ (list (Prim '- (list (Int 2)))
(Int 3)))
(Prim '+ (list (Var 'x)
(Prim '+ (list (Int 2) (Int 3)))))))
(Program '() (Prim '+
(list (Let 'x
(Prim '+
(list (Prim '- (list (Int 1)))
(Int 2)))
(Prim '+ (list (Var 'x) (Int 2))))
(Prim '+ (list (Int 4) (Int 5))))))
(Program '()
(Let 'a (Int 42)
(Let 'b (Var 'a)
(Var 'b))))
(Program '() (Let 'tmp (Prim '- (list (Int 1))) (Var 'tmp)))
(Program '() (Prim '-
(list (Let 'x (Int 1) (Var 'x)))))
(Program '() (Let 'x (Let 'x (Int 1) (Var 'x)) (Prim '+ (list (Int 2) (Var 'x)))))
(Program '()
(Let 'y (Let 'x (Int 20)
(Prim '+ (list (Var 'x)
(Let 'x (Int 22) (Var 'x)))))
(Var 'y)))
(Program '() ; - pos 13
(Prim '+ (list (Prim 'read '())
(Prim 'read '()))))
(Program '()
(Prim '+ (list (Let 'x (Int 1)
(Let 'x (Int 2)
(Int 2)))
(Let 'x (Int 2)
(Var 'x)))))))
(begin
(define-values (a b c) (rco-arg (Prim '- (list (Int 20))) -1))
(test-eq a (Var 'tmp.0)))
(define (pass p)
(remove-complex-opera* (uniquify p)))
(define (pass-2 p)
(remove-complex-opera*-2 (uniquify p)))
(test-eq
(pass-2 (Program '() (Int 20)))
(Program '() (Let 'tmp.0 (Int 20) (Var 'tmp.0))))
(test-eq
(pass-2 (Program '() (Let 'x (Int 20) (Var 'x))))
(Program '() (Let 'x.1 (Int 20) (Var 'x.1))))
(test-eq
(pass-2 (Program '() (Let 'x (Int 20) (Int 40))))
(Program '() (Let 'x.1 (Int 20) (Let 'tmp.0 (Int 40) (Var 'tmp.0)))))
(test-eq
(pass-2 (Program '() (Let 'x (Int 20) (Let 'y (Int 40) (Prim '+ (list (Var 'y) (Int 1)))))))
(Program '() (Let 'x.1 (Int 20)
(Let 'y.1 (Int 40)
(Let 'tmp.0 (Int 1)
(Let 'tmp.1 (Prim '+ (list (Var 'y.1) (Var 'tmp.0)))
(Var 'tmp.1)))))))
(test-eq
(pass-2 (Program '() (Let 'x (Let 'y (Int 40) (Prim '+ (list (Var 'y) (Int 1)))) (Var 'x))))
(Program '() (Let 'x.1 (Let 'y.1 (Int 40)
(Let 'tmp.0 (Int 1)
(Prim '+ (list (Var 'y.1) (Var 'tmp.0)))))
(Var 'x.1))))
(test-eq
(pass-2 (list-ref programs 0))
(Program '() (Let 'tmp.0 (Int 20) (Let 'tmp.1 (Prim '- (list (Var 'tmp.0))) (Var 'tmp.1)))))
(define inputs
(let ([empty-inputs (build-list (length programs) (lambda (_) '()))])
(list-set empty-inputs 13 '(2 3))))
(for ([program programs]
[input-list inputs])
(begin
(test-eq (with-input-from-num-list input-list
(lambda () (interp-RVar program)))
(with-input-from-num-list input-list
(lambda () (interp-RVar (pass program)))))
(test-eq (with-input-from-num-list input-list
(lambda () (interp-RVar program)))
(with-input-from-num-list input-list
(lambda () (interp-RVar (pass-2 program)))))))