summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbd <bdunahu@operationnull.com>2025-01-28 17:52:23 -0500
committerbd <bdunahu@operationnull.com>2025-01-28 17:52:23 -0500
commit5cea39649d613864f3e7a688cb2a7ecbde8bd6cd (patch)
tree9cfdeb42231f771b344fdf7b541d5f135eaf276f
parent467357ef13ea5d935d2e3aa5baaeef6317cd9590 (diff)
Simplified declaring new IR nodes in (backend ast ir)
-rw-r--r--README.org4
-rw-r--r--src/backend/ast/ir.scm41
-rw-r--r--src/backend/tacky/traverse.scm6
-rwxr-xr-xull18
4 files changed, 21 insertions, 48 deletions
diff --git a/README.org b/README.org
index 0604e2a..49af2a5 100644
--- a/README.org
+++ b/README.org
@@ -2,11 +2,11 @@
The =manifest.scm= file provides the ideal build environment automatically to Guix users. flex, pkg-config, bison, make, and gcc are required to compile the frontend into a shared object file with =make all=, and guile is required to run the =ull= script.
-Running the ./ull script will print allowed arguments, which follows the interface prescribed by the book //Writing a C Compiler// by Nora Sandler.
+Running the ./ull script will print allowed arguments, which follows the interface prescribed by the book /Writing a C Compiler/ by Nora Sandler.
* General
-This compiler was made following the general guidelines provided in //Writing a C Compiler// by Nora Sandler.
+This compiler was made following the general guidelines provided in /Writing a C Compiler/ by Nora Sandler.
Since this a functional-programing/guile learning project, I utilized flex and bison to allow use of the Guile/C interface. Development takes a functionality-first approach---memory leaks and other undesired behavior likely.
diff --git a/src/backend/ast/ir.scm b/src/backend/ast/ir.scm
index 102739c..e4c1713 100644
--- a/src/backend/ast/ir.scm
+++ b/src/backend/ast/ir.scm
@@ -16,35 +16,12 @@
;;;
;;; Code:
-(define (ir-node type . args)
- (cons type args))
-
-(define (prog func)
- (ir-node 'prog func))
-
-(define (srout label instrs)
- (ir-node 'srout label instrs))
-
-(define (neg dst)
- (ir-node 'neg dst))
-
-(define (not dst)
- (ir-node 'not dst))
-
-(define (mov src dst)
- (ir-node 'mov src dst))
-
-(define (stack val)
- (ir-node 'stack val))
-
-(define (reg val)
- (ir-node 'reg val))
-
-(define (imm val)
- (ir-node 'imm val))
-
-(define (tmp num)
- (ir-node 'tmp num))
-
-(define (ret)
- (ir-node 'ret))
+(define-syntax def-ir-nodes
+ (syntax-rules ()
+ ((_ names ...)
+ (begin
+ (define (names . args)
+ (cons 'names args)) ...))))
+
+(def-ir-nodes
+ prog srout neg not mov stack reg imm tmp ret)
diff --git a/src/backend/tacky/traverse.scm b/src/backend/tacky/traverse.scm
index 9a1b7aa..aec0f23 100644
--- a/src/backend/tacky/traverse.scm
+++ b/src/backend/tacky/traverse.scm
@@ -31,9 +31,3 @@
(lambda ()
(set! count (1+ count))
(list 'tmp count))))
-
-(define (neg src dst)
- (list 'neg src dst))
-
-(define (not src dst)
- (list 'not src dst))
diff --git a/ull b/ull
index 6217544..7532be9 100755
--- a/ull
+++ b/ull
@@ -68,12 +68,6 @@ Returns an string representing the generated assembly."
(when write?
(assembly->string ast)))))))
-(define (postprocess src dest)
- "Assembles and links SRC, producing executable DEST.
-Returns #f on a failure, #t on a success."
- (when (zero? (system (string-concatenate `("gcc " ,src " -o " ,dest))))
- (display (format #f "Postprocess reported success (wrote ~a).\n" dst))))
-
(define (write str dst)
"Writes STR to the file at DST."
(cleanup-file dst)
@@ -82,6 +76,12 @@ Returns #f on a failure, #t on a success."
(close-port port))
(display (format #f "Assembly generation reported success (wrote ~a).\n" dst)))
+(define (postprocess src dst)
+ "Assembles and links SRC, producing executable DST.
+Returns #f on a failure, #t on a success."
+ (when (zero? (system (string-concatenate `("gcc " ,src " -o " ,dst))))
+ (display (format #f "Postprocess reported success (wrote ~a).\n" dst))))
+
(define (main args)
"Entry point for ull. Handles user args and performs initial validity check."
(let* ((option-spec
@@ -110,7 +110,9 @@ Returns #f on a failure, #t on a success."
(let* ((parse? (not (option-ref options 'lex #f)))
(tack? (not (option-ref options 'parse #f)))
(generate? (not (option-ref options 'tacky #f)))
- (write? (not (option-ref options 'codegen #f)))
+ (write? (and (not (option-ref options 'codegen #f))
+ generate?
+ tack?))
(executable-file-name
(string-drop-right file-name 2))
(preprocessed-file-name
@@ -126,7 +128,7 @@ Returns #f on a failure, #t on a success."
;; call the backend
(begin (display "Parser reported success\n")
(let ((program (backend c-ast tack? generate? write?)))
- (when (and write? (not tack?) (not generate?))
+ (when write?
(write program assembly-file-name)
;; call postprocessing
(postprocess assembly-file-name executable-file-name))))