summaryrefslogtreecommitdiff
path: root/src/modules
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/ast/assembly-tree.scm29
-rw-r--r--src/modules/ast/syntax-tree.scm47
-rw-r--r--src/modules/generator/generator.scm24
-rw-r--r--src/modules/parser/parser.scm17
-rw-r--r--src/modules/tuple-generator/tuple-generator.scm36
-rw-r--r--src/modules/utils/t-factory.scm24
6 files changed, 119 insertions, 58 deletions
diff --git a/src/modules/ast/assembly-tree.scm b/src/modules/ast/assembly-tree.scm
index ee3e116..3f0f1b1 100644
--- a/src/modules/ast/assembly-tree.scm
+++ b/src/modules/ast/assembly-tree.scm
@@ -4,22 +4,37 @@
subroutine?
subroutine-label
subroutine-instructions
+ subroutine-frame-size
make-instruction
instruction?
+ instruction-destination
+ set-instruction-destination
instruction-operator
- instruction-operand-1
- instruction-operand-2))
+ instruction-src-1
+ instruction-src-2
+
+ make-register
+ register?
+ register-name
+ ))
(define-record-type <subroutine>
- (make-subroutine label instrs)
+ (make-subroutine label instrs f-size)
subroutine?
(label subroutine-label)
- (instrs subroutine-instructions))
+ (instrs subroutine-instructions)
+ (f-size subroutine-frame-size))
(define-record-type <instruction>
- (make-instruction op oper1 oper2)
+ (make-instruction dest op src1 src2)
instruction?
+ (dest instruction-destination set-instruction-destination)
(op instruction-operator)
- (oper1 instruction-operand-1)
- (oper2 instruction-operand-2))
+ (src1 instruction-src-1)
+ (src2 instruction-src-2))
+
+(define-record-type <register>
+ (make-register name)
+ register?
+ (name register-name))
diff --git a/src/modules/ast/syntax-tree.scm b/src/modules/ast/syntax-tree.scm
index b7db8dd..e2ca8da 100644
--- a/src/modules/ast/syntax-tree.scm
+++ b/src/modules/ast/syntax-tree.scm
@@ -1,34 +1,30 @@
(define-module (modules ast syntax-tree)
#:use-module (srfi srfi-9)
#:export (make-program
- program?
- program-function
+ program?
+ program-function
- make-function
- function?
- function-id
- function-stmt
+ make-function
+ function?
+ function-id
+ function-stmt
- make-stmt
- stmt?
- stmt-expr
+ make-stmt
+ stmt?
+ stmt-expr
- make-expr
- expr?
- expr-type
+ make-unary
+ unary?
+ unary-operator
+ unary-expr
- make-unary
- unary?
- unary-operator
- unary-expr
+ make-const
+ const?
+ const-int
- make-const
- const?
- const-int
-
- make-id
- id?
- id-symbol))
+ make-id
+ id?
+ id-symbol))
(define-record-type <program>
(make-program func)
@@ -46,11 +42,6 @@
stmt?
(expr stmt-expr))
-(define-record-type <expr>
- (make-expr type)
- expr?
- (type expr-type))
-
(define-record-type <unary>
(make-unary op expr)
unary?
diff --git a/src/modules/generator/generator.scm b/src/modules/generator/generator.scm
index 4a36b92..ed00de5 100644
--- a/src/modules/generator/generator.scm
+++ b/src/modules/generator/generator.scm
@@ -1,23 +1,19 @@
(define-module (modules generator generator)
+ #:use-module (srfi srfi-9 gnu)
#:use-module (modules ast syntax-tree)
#:use-module (modules ast assembly-tree)
#:export (g-program))
(define (g-program p)
- (make-program (g-function (program-function p))))
+ (g-subroutine (program-function p)))
-(define (g-function f)
- (make-subroutine (g-id (function-id f))
- (g-stmt (function-stmt f))))
+(define (g-subroutine s)
+ (g-instructions (subroutine-instructions s)))
-(define (g-stmt s)
- (g-expr (stmt-expr s)))
-
-(define (g-expr e)
- ;; for now, we assume only 'return'!
- (list (make-instruction "movl" (string-append/shared "$" (number->string (const-int (expr-type e)))) "%eax")
- (make-instruction "ret" #f #f)))
-
-(define (g-id i)
- (id-symbol i))
+(define (g-instructions lst)
+ (define (g-instruction i)
+ (set-instruction-destination i "foo"))
+ (unless (null? lst)
+ (begin (g-instruction (car lst))
+ (g-instructions (cdr lst)))))
diff --git a/src/modules/parser/parser.scm b/src/modules/parser/parser.scm
index fa9202d..cab690c 100644
--- a/src/modules/parser/parser.scm
+++ b/src/modules/parser/parser.scm
@@ -26,12 +26,11 @@
(_ (die))))
(define (p-expr tokens)
- (make-expr
- (match tokens
- (((? integer? int))
- (make-const int))
- (((or 'sub 'complement) expr ...)
- (make-unary (car tokens) (p-expr expr)))
- (('left-paren expr ... 'right-paren)
- (p-expr expr))
- (_ (die)))))
+ (match tokens
+ (((? integer? int))
+ (make-const int))
+ (((or 'sub 'complement) expr ...)
+ (make-unary (car tokens) (p-expr expr)))
+ (('left-paren expr ... 'right-paren)
+ (p-expr expr))
+ (_ (die))))
diff --git a/src/modules/tuple-generator/tuple-generator.scm b/src/modules/tuple-generator/tuple-generator.scm
new file mode 100644
index 0000000..a2dde2d
--- /dev/null
+++ b/src/modules/tuple-generator/tuple-generator.scm
@@ -0,0 +1,36 @@
+(define-module (modules tuple-generator tuple-generator)
+ #:use-module (ice-9 receive)
+ #:use-module (modules utils t-factory)
+ #:use-module (modules ast syntax-tree)
+ #:use-module (modules ast assembly-tree)
+ #:export (t-program))
+
+
+(define (t-program p)
+ (make-program (t-function (program-function p))))
+
+(define (t-function f)
+ (make-subroutine (t-id (function-id f))
+ (t-stmt (function-stmt f))
+ #f))
+
+(define (t-stmt s)
+ (receive (src instrs) (t-expr (stmt-expr s))
+ (append instrs
+ (list (make-instruction (make-register 'eax) 'mov src #f)
+ (make-instruction #f 'ret #f #f)))))
+
+(define (t-expr e)
+ (cond
+ ((unary? e)
+ (receive (src instrs) (t-expr (unary-expr e))
+ (let ((dest (make-temporary)))
+ (values dest
+ (append instrs
+ (list (make-instruction dest 'mov src #f)
+ (make-instruction dest (unary-operator e) #f #f)))))))
+ (#t (values e '()))))
+
+
+(define (t-id i)
+ (id-symbol i))
diff --git a/src/modules/utils/t-factory.scm b/src/modules/utils/t-factory.scm
new file mode 100644
index 0000000..cad7c24
--- /dev/null
+++ b/src/modules/utils/t-factory.scm
@@ -0,0 +1,24 @@
+(define-module (modules utils t-factory)
+ #:use-module (srfi srfi-9)
+ #:export (temporary?
+ temporary-name
+ temporary-register
+ set-temporary-register!
+ make-temporary
+ ))
+
+
+(define-record-type <temporary>
+ (make--temporary name register)
+ temporary?
+ (name temporary-name)
+ (register temporary-register set-temporary-register!))
+
+(define make-temporary)
+(let ((count 100))
+ (set! make-temporary
+ (lambda ()
+ (set! count (1+ count))
+ (make--temporary (string->symbol
+ (format #f "t.~a" count))
+ #f))))