diff --git a/abscond/compiler/compile.rkt b/abscond/compiler/compile.rkt index e67640c..ee7eddd 100644 --- a/abscond/compiler/compile.rkt +++ b/abscond/compiler/compile.rkt @@ -1,6 +1,5 @@ #lang racket -(provide compile - compile-e) +(provide compile) (require "../syntax/ast.rkt") (require a86/ast a86/registers) @@ -9,11 +8,7 @@ (define (compile e) (prog (Global 'entry) (Label 'entry) - (compile-e e) + (match e + [(Lit i) (Mov rax i)]) (Ret))) -;; Expr -> Asm -(define (compile-e e) - (match e - [(Lit i) (seq (Mov rax i))])) - diff --git a/abscond/correct.rkt b/abscond/correct.rkt index 9ee319c..69f6400 100644 --- a/abscond/correct.rkt +++ b/abscond/correct.rkt @@ -2,11 +2,11 @@ (provide check-compiler) (require rackunit) (require "interpreter/interp.rkt") -(require "executor/run.rkt") (require "compiler/compile.rkt") +(require a86/interp) ;; Expr -> Void (define (check-compiler e) (check-equal? (interp e) - (run (compile e)))) + (asm-interp (compile e)))) diff --git a/blackmail/executor/run-stdin.rkt b/abscond/executor/exec-stdin.rkt similarity index 78% rename from blackmail/executor/run-stdin.rkt rename to abscond/executor/exec-stdin.rkt index 7e7170f..8f50771 100644 --- a/blackmail/executor/run-stdin.rkt +++ b/abscond/executor/exec-stdin.rkt @@ -2,11 +2,11 @@ (provide main) (require "../syntax/parse.rkt") (require "../compiler/compile.rkt") -(require "run.rkt") +(require a86/interp) ;; -> Void ;; Compile contents of stdin and use asm-interp to run (define (main) (read-line) ; ignore #lang racket line - (run (compile (parse (read))))) + (asm-interp (compile (parse (read))))) diff --git a/abscond/executor/exec.rkt b/abscond/executor/exec.rkt deleted file mode 100644 index 7800ef5..0000000 --- a/abscond/executor/exec.rkt +++ /dev/null @@ -1,44 +0,0 @@ -#lang racket -(require a86/interp) - -(provide exec - (struct-out exec-state) - exec-unload - call-with-exec) - -(require a86/interp - ffi/unsafe) - -(struct exec-state (program) #:transparent) - -(define _val _int64) - -(define (exec/state prog) - (exec-state - (asm-load prog))) - -(define (exec-call st) - (match-define (exec-state program) st) - (asm-call program 'entry)) - -(define (exec-unload st) - (asm-unload (exec-state-program st))) - -;; ------------------------------------------------------------ -;; public API - -;; execute with runtime system and Racket host -;; return raw bits plus the live state needed to interpret them safely - -;; CAUTION: this does not unload -(define (exec asm) - (exec-call (exec/state asm))) - -;; version of above that ensures unloading -(define (call-with-exec e f) - (define st (exec/state e)) - (dynamic-wind - void - (λ () (f (exec-call st))) - (λ () (exec-unload st)))) - diff --git a/abscond/executor/run.rkt b/abscond/executor/run.rkt deleted file mode 100644 index 39c354a..0000000 --- a/abscond/executor/run.rkt +++ /dev/null @@ -1,11 +0,0 @@ -#lang racket -(require a86/interp) -(require "exec.rkt") -(provide run) - -;; Asm -> Integer -(define (run asm) - (call-with-exec - asm - identity)) - diff --git a/abscond/main.rkt b/abscond/main.rkt index 16c671d..a334722 100644 --- a/abscond/main.rkt +++ b/abscond/main.rkt @@ -3,12 +3,8 @@ (require "syntax/parse.rkt") (require "interpreter/interp.rkt") (require "compiler/compile.rkt") -(require "executor/run.rkt") -(require "executor/exec.rkt") (provide (all-from-out "syntax/ast.rkt")) (provide (all-from-out "syntax/parse.rkt")) (provide (all-from-out "interpreter/interp.rkt")) (provide (all-from-out "compiler/compile.rkt")) -(provide (all-from-out "executor/run.rkt")) -(provide (all-from-out "executor/exec.rkt")) diff --git a/abscond/runtime/Makefile b/abscond/runtime/Makefile index 8b22999..34d7577 100644 --- a/abscond/runtime/Makefile +++ b/abscond/runtime/Makefile @@ -6,11 +6,9 @@ else LANGS_AS ?= clang -c endif -CFLAGS += -fPIC -g +CFLAGS += -g -OBJS = \ - main.o \ - print.o +OBJS = main.o default: runtime.o diff --git a/abscond/runtime/main.c b/abscond/runtime/main.c index 4c8ad77..163618e 100644 --- a/abscond/runtime/main.c +++ b/abscond/runtime/main.c @@ -1,13 +1,12 @@ #include #include -#include "print.h" int64_t entry(); int main(int argc, char** argv) { int64_t result = entry(); - print_result(result); + printf("%" PRId64, result); putchar('\n'); return 0; } diff --git a/abscond/runtime/print.c b/abscond/runtime/print.c deleted file mode 100644 index cf19daf..0000000 --- a/abscond/runtime/print.c +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include - -void print_result(int64_t x) -{ - printf("%" PRId64, x); -} diff --git a/abscond/runtime/print.h b/abscond/runtime/print.h deleted file mode 100644 index 08ae346..0000000 --- a/abscond/runtime/print.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef PRINT_H -#define PRINT_H - -void print_result(int64_t); - -#endif diff --git a/abscond/test/run-compile-tests.rkt b/abscond/test/run-compile-tests.rkt index 638f5e8..9fc6cc6 100644 --- a/abscond/test/run-compile-tests.rkt +++ b/abscond/test/run-compile-tests.rkt @@ -1,8 +1,8 @@ #lang racket (require "../compiler/compile.rkt") (require "../syntax/parse.rkt") -(require "../executor/run.rkt") (require "define-tests.rkt") +(require a86/interp) -(test (λ (e) (run (compile (parse e))))) +(test (λ (e) (asm-interp (compile (parse e))))) diff --git a/blackmail/correct.rkt b/blackmail/correct.rkt index 9ee319c..69f6400 100644 --- a/blackmail/correct.rkt +++ b/blackmail/correct.rkt @@ -2,11 +2,11 @@ (provide check-compiler) (require rackunit) (require "interpreter/interp.rkt") -(require "executor/run.rkt") (require "compiler/compile.rkt") +(require a86/interp) ;; Expr -> Void (define (check-compiler e) (check-equal? (interp e) - (run (compile e)))) + (asm-interp (compile e)))) diff --git a/abscond/executor/run-stdin.rkt b/blackmail/executor/exec-stdin.rkt similarity index 78% rename from abscond/executor/run-stdin.rkt rename to blackmail/executor/exec-stdin.rkt index 7e7170f..8f50771 100644 --- a/abscond/executor/run-stdin.rkt +++ b/blackmail/executor/exec-stdin.rkt @@ -2,11 +2,11 @@ (provide main) (require "../syntax/parse.rkt") (require "../compiler/compile.rkt") -(require "run.rkt") +(require a86/interp) ;; -> Void ;; Compile contents of stdin and use asm-interp to run (define (main) (read-line) ; ignore #lang racket line - (run (compile (parse (read))))) + (asm-interp (compile (parse (read))))) diff --git a/blackmail/executor/exec.rkt b/blackmail/executor/exec.rkt deleted file mode 100644 index 7800ef5..0000000 --- a/blackmail/executor/exec.rkt +++ /dev/null @@ -1,44 +0,0 @@ -#lang racket -(require a86/interp) - -(provide exec - (struct-out exec-state) - exec-unload - call-with-exec) - -(require a86/interp - ffi/unsafe) - -(struct exec-state (program) #:transparent) - -(define _val _int64) - -(define (exec/state prog) - (exec-state - (asm-load prog))) - -(define (exec-call st) - (match-define (exec-state program) st) - (asm-call program 'entry)) - -(define (exec-unload st) - (asm-unload (exec-state-program st))) - -;; ------------------------------------------------------------ -;; public API - -;; execute with runtime system and Racket host -;; return raw bits plus the live state needed to interpret them safely - -;; CAUTION: this does not unload -(define (exec asm) - (exec-call (exec/state asm))) - -;; version of above that ensures unloading -(define (call-with-exec e f) - (define st (exec/state e)) - (dynamic-wind - void - (λ () (f (exec-call st))) - (λ () (exec-unload st)))) - diff --git a/blackmail/executor/run.rkt b/blackmail/executor/run.rkt deleted file mode 100644 index 39c354a..0000000 --- a/blackmail/executor/run.rkt +++ /dev/null @@ -1,11 +0,0 @@ -#lang racket -(require a86/interp) -(require "exec.rkt") -(provide run) - -;; Asm -> Integer -(define (run asm) - (call-with-exec - asm - identity)) - diff --git a/blackmail/main.rkt b/blackmail/main.rkt index 16c671d..a334722 100644 --- a/blackmail/main.rkt +++ b/blackmail/main.rkt @@ -3,12 +3,8 @@ (require "syntax/parse.rkt") (require "interpreter/interp.rkt") (require "compiler/compile.rkt") -(require "executor/run.rkt") -(require "executor/exec.rkt") (provide (all-from-out "syntax/ast.rkt")) (provide (all-from-out "syntax/parse.rkt")) (provide (all-from-out "interpreter/interp.rkt")) (provide (all-from-out "compiler/compile.rkt")) -(provide (all-from-out "executor/run.rkt")) -(provide (all-from-out "executor/exec.rkt")) diff --git a/blackmail/runtime/Makefile b/blackmail/runtime/Makefile index 8b22999..34d7577 100644 --- a/blackmail/runtime/Makefile +++ b/blackmail/runtime/Makefile @@ -6,11 +6,9 @@ else LANGS_AS ?= clang -c endif -CFLAGS += -fPIC -g +CFLAGS += -g -OBJS = \ - main.o \ - print.o +OBJS = main.o default: runtime.o diff --git a/blackmail/runtime/main.c b/blackmail/runtime/main.c index 4c8ad77..163618e 100644 --- a/blackmail/runtime/main.c +++ b/blackmail/runtime/main.c @@ -1,13 +1,12 @@ #include #include -#include "print.h" int64_t entry(); int main(int argc, char** argv) { int64_t result = entry(); - print_result(result); + printf("%" PRId64, result); putchar('\n'); return 0; } diff --git a/blackmail/runtime/print.c b/blackmail/runtime/print.c deleted file mode 100644 index cf19daf..0000000 --- a/blackmail/runtime/print.c +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include - -void print_result(int64_t x) -{ - printf("%" PRId64, x); -} diff --git a/blackmail/runtime/print.h b/blackmail/runtime/print.h deleted file mode 100644 index 08ae346..0000000 --- a/blackmail/runtime/print.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef PRINT_H -#define PRINT_H - -void print_result(int64_t); - -#endif diff --git a/blackmail/test/run-compile-tests.rkt b/blackmail/test/run-compile-tests.rkt index 638f5e8..9fc6cc6 100644 --- a/blackmail/test/run-compile-tests.rkt +++ b/blackmail/test/run-compile-tests.rkt @@ -1,8 +1,8 @@ #lang racket (require "../compiler/compile.rkt") (require "../syntax/parse.rkt") -(require "../executor/run.rkt") (require "define-tests.rkt") +(require a86/interp) -(test (λ (e) (run (compile (parse e))))) +(test (λ (e) (asm-interp (compile (parse e))))) diff --git a/con/correct.rkt b/con/correct.rkt index 9ee319c..69f6400 100644 --- a/con/correct.rkt +++ b/con/correct.rkt @@ -2,11 +2,11 @@ (provide check-compiler) (require rackunit) (require "interpreter/interp.rkt") -(require "executor/run.rkt") (require "compiler/compile.rkt") +(require a86/interp) ;; Expr -> Void (define (check-compiler e) (check-equal? (interp e) - (run (compile e)))) + (asm-interp (compile e)))) diff --git a/con/executor/run-stdin.rkt b/con/executor/exec-stdin.rkt similarity index 78% rename from con/executor/run-stdin.rkt rename to con/executor/exec-stdin.rkt index 7e7170f..8f50771 100644 --- a/con/executor/run-stdin.rkt +++ b/con/executor/exec-stdin.rkt @@ -2,11 +2,11 @@ (provide main) (require "../syntax/parse.rkt") (require "../compiler/compile.rkt") -(require "run.rkt") +(require a86/interp) ;; -> Void ;; Compile contents of stdin and use asm-interp to run (define (main) (read-line) ; ignore #lang racket line - (run (compile (parse (read))))) + (asm-interp (compile (parse (read))))) diff --git a/con/executor/exec.rkt b/con/executor/exec.rkt deleted file mode 100644 index 7800ef5..0000000 --- a/con/executor/exec.rkt +++ /dev/null @@ -1,44 +0,0 @@ -#lang racket -(require a86/interp) - -(provide exec - (struct-out exec-state) - exec-unload - call-with-exec) - -(require a86/interp - ffi/unsafe) - -(struct exec-state (program) #:transparent) - -(define _val _int64) - -(define (exec/state prog) - (exec-state - (asm-load prog))) - -(define (exec-call st) - (match-define (exec-state program) st) - (asm-call program 'entry)) - -(define (exec-unload st) - (asm-unload (exec-state-program st))) - -;; ------------------------------------------------------------ -;; public API - -;; execute with runtime system and Racket host -;; return raw bits plus the live state needed to interpret them safely - -;; CAUTION: this does not unload -(define (exec asm) - (exec-call (exec/state asm))) - -;; version of above that ensures unloading -(define (call-with-exec e f) - (define st (exec/state e)) - (dynamic-wind - void - (λ () (f (exec-call st))) - (λ () (exec-unload st)))) - diff --git a/con/executor/run.rkt b/con/executor/run.rkt deleted file mode 100644 index 39c354a..0000000 --- a/con/executor/run.rkt +++ /dev/null @@ -1,11 +0,0 @@ -#lang racket -(require a86/interp) -(require "exec.rkt") -(provide run) - -;; Asm -> Integer -(define (run asm) - (call-with-exec - asm - identity)) - diff --git a/con/main.rkt b/con/main.rkt index 16c671d..a334722 100644 --- a/con/main.rkt +++ b/con/main.rkt @@ -3,12 +3,8 @@ (require "syntax/parse.rkt") (require "interpreter/interp.rkt") (require "compiler/compile.rkt") -(require "executor/run.rkt") -(require "executor/exec.rkt") (provide (all-from-out "syntax/ast.rkt")) (provide (all-from-out "syntax/parse.rkt")) (provide (all-from-out "interpreter/interp.rkt")) (provide (all-from-out "compiler/compile.rkt")) -(provide (all-from-out "executor/run.rkt")) -(provide (all-from-out "executor/exec.rkt")) diff --git a/con/runtime/Makefile b/con/runtime/Makefile index 8b22999..34d7577 100644 --- a/con/runtime/Makefile +++ b/con/runtime/Makefile @@ -6,11 +6,9 @@ else LANGS_AS ?= clang -c endif -CFLAGS += -fPIC -g +CFLAGS += -g -OBJS = \ - main.o \ - print.o +OBJS = main.o default: runtime.o diff --git a/con/runtime/main.c b/con/runtime/main.c index c608bf9..163618e 100644 --- a/con/runtime/main.c +++ b/con/runtime/main.c @@ -1,13 +1,12 @@ #include #include -#include "print.h" int64_t entry(); int main(int argc, char** argv) { - int64_t result = entry(); - print_result(result); + int64_t result = entry(); + printf("%" PRId64, result); putchar('\n'); return 0; } diff --git a/con/runtime/print.c b/con/runtime/print.c deleted file mode 100644 index cf19daf..0000000 --- a/con/runtime/print.c +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include - -void print_result(int64_t x) -{ - printf("%" PRId64, x); -} diff --git a/con/runtime/print.h b/con/runtime/print.h deleted file mode 100644 index 08ae346..0000000 --- a/con/runtime/print.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef PRINT_H -#define PRINT_H - -void print_result(int64_t); - -#endif diff --git a/con/test/run-compile-tests.rkt b/con/test/run-compile-tests.rkt index 638f5e8..9fc6cc6 100644 --- a/con/test/run-compile-tests.rkt +++ b/con/test/run-compile-tests.rkt @@ -1,8 +1,8 @@ #lang racket (require "../compiler/compile.rkt") (require "../syntax/parse.rkt") -(require "../executor/run.rkt") (require "define-tests.rkt") +(require a86/interp) -(test (λ (e) (run (compile (parse e))))) +(test (λ (e) (asm-interp (compile (parse e))))) diff --git a/dodger/executor/run-stdin.rkt b/dodger/executor/run-stdin.rkt index 7e7170f..ac60d60 100644 --- a/dodger/executor/run-stdin.rkt +++ b/dodger/executor/run-stdin.rkt @@ -8,5 +8,6 @@ ;; Compile contents of stdin and use asm-interp to run (define (main) (read-line) ; ignore #lang racket line + (run (compile (parse (read))))) diff --git a/dodger/test/run-compile-tests.rkt b/dodger/test/run-compile-tests.rkt index 638f5e8..1360550 100644 --- a/dodger/test/run-compile-tests.rkt +++ b/dodger/test/run-compile-tests.rkt @@ -3,6 +3,5 @@ (require "../syntax/parse.rkt") (require "../executor/run.rkt") (require "define-tests.rkt") - (test (λ (e) (run (compile (parse e))))) diff --git a/dupe/correct.rkt b/dupe/correct.rkt index 2129ca0..16ae264 100644 --- a/dupe/correct.rkt +++ b/dupe/correct.rkt @@ -2,13 +2,12 @@ (provide check-compiler) (require rackunit) (require "interpreter/interp.rkt") -(require "executor/run.rkt") -(require "compiler/compile.rkt") +(require "executor/exec.rkt") ;; Expr -> Void (define (check-compiler e) (let ((r (with-handlers ([exn:fail? identity]) (interp e)))) (unless (exn? r) - (check-equal? r (run (compile e)))))) + (check-equal? r (exec e))))) diff --git a/dupe/executor/exec-stdin.rkt b/dupe/executor/exec-stdin.rkt new file mode 100644 index 0000000..9669e7a --- /dev/null +++ b/dupe/executor/exec-stdin.rkt @@ -0,0 +1,11 @@ +#lang racket +(provide main) +(require "../syntax/parse.rkt") +(require "exec.rkt") + +;; -> Value +;; Parse, compile, and execute contents of stdin +(define (main) + (read-line) ; ignore #lang racket line + (exec (parse (read)))) + diff --git a/dupe/executor/exec.rkt b/dupe/executor/exec.rkt index 8b8c3be..71f3c17 100644 --- a/dupe/executor/exec.rkt +++ b/dupe/executor/exec.rkt @@ -1,44 +1,9 @@ #lang racket +(provide exec) (require a86/interp) - -(provide exec - (struct-out exec-state) - exec-unload - call-with-exec) - -(require a86/interp - ffi/unsafe) +(require "../compiler/compile.rkt") (require "decode.rkt") -(require "../runtime/types.rkt") - -(struct exec-state (program) #:transparent) - -(define (exec/state prog) - (exec-state - (asm-load prog))) - -(define (exec-call st) - (match-define (exec-state program) st) - (asm-call program 'entry)) - -(define (exec-unload st) - (asm-unload (exec-state-program st))) - -;; ------------------------------------------------------------ -;; public API - -;; execute with runtime system and Racket host -;; return raw bits plus the live state needed to interpret them safely - -;; CAUTION: this does not unload -(define (exec asm) - (exec-call (exec/state asm))) - -;; version of above that ensures unloading -(define (call-with-exec e f) - (define st (exec/state e)) - (dynamic-wind - void - (λ () (f (exec-call st))) - (λ () (exec-unload st)))) +;; Expr -> Value +(define (exec e) + (bits->value (asm-interp (compile e)))) diff --git a/dupe/executor/run-stdin.rkt b/dupe/executor/run-stdin.rkt deleted file mode 100644 index 7e7170f..0000000 --- a/dupe/executor/run-stdin.rkt +++ /dev/null @@ -1,12 +0,0 @@ -#lang racket -(provide main) -(require "../syntax/parse.rkt") -(require "../compiler/compile.rkt") -(require "run.rkt") - -;; -> Void -;; Compile contents of stdin and use asm-interp to run -(define (main) - (read-line) ; ignore #lang racket line - (run (compile (parse (read))))) - diff --git a/dupe/executor/run.rkt b/dupe/executor/run.rkt deleted file mode 100644 index 5b4c9cd..0000000 --- a/dupe/executor/run.rkt +++ /dev/null @@ -1,11 +0,0 @@ -#lang racket -(require a86/interp) -(require "decode.rkt") -(require "exec.rkt") -(provide run) -(define (run asm) - (call-with-exec - asm - (λ (r) - (bits->value r)))) - diff --git a/dupe/main.rkt b/dupe/main.rkt index d6a5d90..dc085fd 100644 --- a/dupe/main.rkt +++ b/dupe/main.rkt @@ -4,13 +4,11 @@ (require "interpreter/interp.rkt") (require "compiler/compile.rkt") (require "runtime/types.rkt") -(require "executor/run.rkt") (require "executor/exec.rkt") (provide (all-from-out "syntax/ast.rkt")) (provide (all-from-out "syntax/parse.rkt")) (provide (all-from-out "interpreter/interp.rkt")) (provide (all-from-out "compiler/compile.rkt")) (provide (all-from-out "runtime/types.rkt")) -(provide (all-from-out "executor/run.rkt")) (provide (all-from-out "executor/exec.rkt")) diff --git a/dupe/runtime/Makefile b/dupe/runtime/Makefile index cf0e413..32de08f 100644 --- a/dupe/runtime/Makefile +++ b/dupe/runtime/Makefile @@ -6,10 +6,9 @@ else LANGS_AS ?= clang -c endif -CFLAGS += -fPIC -g +CFLAGS += -g -OBJS = \ - main.o \ +OBJS = main.o \ print.o \ values.o diff --git a/dupe/test/run-compile-tests.rkt b/dupe/test/run-compile-tests.rkt index 638f5e8..a13ebef 100644 --- a/dupe/test/run-compile-tests.rkt +++ b/dupe/test/run-compile-tests.rkt @@ -1,8 +1,6 @@ #lang racket -(require "../compiler/compile.rkt") (require "../syntax/parse.rkt") -(require "../executor/run.rkt") +(require "../executor/exec.rkt") (require "define-tests.rkt") - -(test (λ (e) (run (compile (parse e))))) +(test (λ (e) (exec (parse e)))) diff --git a/evildoer/Makefile b/evildoer/Makefile index 5205a2f..2d442f8 100644 --- a/evildoer/Makefile +++ b/evildoer/Makefile @@ -6,30 +6,33 @@ else LANGS_AS ?= clang -c endif -objs = \ - main.o \ - print.o +RACKET ?= racket -default: runtime.o +RUNTIME_DIR := runtime +RUNTIME := $(RUNTIME_DIR)/runtime.o -runtime.o: $(objs) - ld -r $(objs) -o runtime.o +# Example source extension for this language. +SRC_EXT := rkt -%.run: %.o runtime.o - $(LANGS_CC) runtime.o $< -o $@ +default: + @echo "example: make foo.run" -.c.o: - $(LANGS_CC) -fPIC -c -g -o $@ $< +# Build the runtime bundles if needed. +$(RUNTIME): + $(MAKE) -C $(RUNTIME_DIR) -.s.o: +# Compile source program to assembly. +%.s: %.$(SRC_EXT) + cat $< | $(RACKET) -t compiler/compile-stdin.rkt -m > $@ + +# Assemble to object. +%.o: %.s $(LANGS_AS) -o $@ $< -%.s: %.rkt - cat $< | racket -t compile-stdin.rkt -m > $@ +# Link standalone executable. +%.run: %.o $(RUNTIME) + $(LANGS_CC) -o $@ $^ clean: @$(RM) *.o *.s *.run ||: - @echo "$(shell basename $(shell pwd)): cleaned!" - -%.test: %.run %.rkt - @test "$(shell ./$(<))" = "$(shell racket $(word 2,$^))" + @$(MAKE) -C $(RUNTIME_DIR) clean diff --git a/evildoer/correct.rkt b/evildoer/correct.rkt index 01914a1..cf21df8 100644 --- a/evildoer/correct.rkt +++ b/evildoer/correct.rkt @@ -2,12 +2,12 @@ (provide check-compiler) (require rackunit) (require "interpreter/interp-io.rkt") -(require "executor/run.rkt") +(require "executor/exec.rkt") (require "compiler/compile.rkt") ;; Expr String -> Void (define (check-compiler e i) (let ((r (with-handlers ([exn:fail? identity]) (interp/io e i)))) (unless (exn? r) - (check-equal? r (run/io (compile e) i))))) + (check-equal? r (exec/io e i))))) diff --git a/evildoer/executor/exec-stdin.rkt b/evildoer/executor/exec-stdin.rkt new file mode 100644 index 0000000..9669e7a --- /dev/null +++ b/evildoer/executor/exec-stdin.rkt @@ -0,0 +1,11 @@ +#lang racket +(provide main) +(require "../syntax/parse.rkt") +(require "exec.rkt") + +;; -> Value +;; Parse, compile, and execute contents of stdin +(define (main) + (read-line) ; ignore #lang racket line + (exec (parse (read)))) + diff --git a/evildoer/executor/exec.rkt b/evildoer/executor/exec.rkt index 34d9d42..ae90943 100644 --- a/evildoer/executor/exec.rkt +++ b/evildoer/executor/exec.rkt @@ -1,50 +1,17 @@ #lang racket -(require a86/interp) - -(provide exec - (struct-out exec-state) - exec-unload - call-with-exec) - -(require a86/interp - ffi/unsafe) +(provide exec exec/io) +(require "../compiler/compile.rkt") (require "decode.rkt") -(require "../runtime/types.rkt") - -(struct exec-state (program) #:transparent) - -(define (exec/state prog) - (exec-state - (parameterize - ([current-externs - (list - (extern 'read_byte read-byte (_fun -> _val)) - (extern 'peek_byte peek-byte (_fun -> _val)) - (extern 'write_byte write-byte (_fun _val -> _val)))]) - (asm-load prog)))) - -(define (exec-call st) - (match-define (exec-state program) st) - (asm-call program 'entry)) - -(define (exec-unload st) - (asm-unload (exec-state-program st))) - -;; ------------------------------------------------------------ -;; public API - -;; execute with runtime system and Racket host -;; return raw bits plus the live state needed to interpret them safely - -;; CAUTION: this does not unload -(define (exec asm) - (exec-call (exec/state asm))) - -;; version of above that ensures unloading -(define (call-with-exec e f) - (define st (exec/state e)) - (dynamic-wind - void - (λ () (f (exec-call st))) - (λ () (exec-unload st)))) - +(require "host.rkt") + +;; Expr -> Value +(define (exec e) + (bits->value (asm-interp/host (compile e)))) + +;; Asm String -> (cons Value String) +(define (exec/io e in) + (parameterize ((current-output-port (open-output-string)) + (current-input-port (open-input-string in))) + (cons (exec e) + (get-output-string (current-output-port))))) + diff --git a/evildoer/executor/host.rkt b/evildoer/executor/host.rkt new file mode 100644 index 0000000..26a64e8 --- /dev/null +++ b/evildoer/executor/host.rkt @@ -0,0 +1,22 @@ +#lang racket +(require a86/interp) +(require ffi/unsafe) +(require "decode.rkt") +(require "../runtime/types.rkt") +(provide (all-defined-out)) + +(define (prim-read-byte) + (value->bits (read-byte))) +(define (prim-peek-byte) + (value->bits (peek-byte))) +(define (prim-write-byte bs) + (value->bits (write-byte (bits->value bs)))) + +(define (asm-interp/host asm) + (parameterize + ([current-externs + (list (extern 'read_byte prim-read-byte (_fun -> _int64)) + (extern 'peek_byte prim-peek-byte (_fun -> _int64)) + (extern 'write_byte prim-write-byte (_fun _int64 -> _int64)))]) + (asm-interp asm))) + diff --git a/evildoer/executor/run-stdin.rkt b/evildoer/executor/run-stdin.rkt deleted file mode 100644 index 7e7170f..0000000 --- a/evildoer/executor/run-stdin.rkt +++ /dev/null @@ -1,12 +0,0 @@ -#lang racket -(provide main) -(require "../syntax/parse.rkt") -(require "../compiler/compile.rkt") -(require "run.rkt") - -;; -> Void -;; Compile contents of stdin and use asm-interp to run -(define (main) - (read-line) ; ignore #lang racket line - (run (compile (parse (read))))) - diff --git a/evildoer/executor/run.rkt b/evildoer/executor/run.rkt deleted file mode 100644 index e568605..0000000 --- a/evildoer/executor/run.rkt +++ /dev/null @@ -1,19 +0,0 @@ -#lang racket -(require a86/interp) -(require "decode.rkt") -(require "exec.rkt") -(provide run run/io) -;; Asm -> Value -(define (run asm) - (call-with-exec - asm - (λ (r) - (bits->value r)))) - -;; Asm String -> (cons Value String) -(define (run/io asm in) - (parameterize ((current-output-port (open-output-string)) - (current-input-port (open-input-string in))) - (cons (run asm) - (get-output-string (current-output-port))))) - diff --git a/evildoer/interpreter/interp-io.rkt b/evildoer/interpreter/interp-io.rkt index 0550189..ccc2dfe 100644 --- a/evildoer/interpreter/interp-io.rkt +++ b/evildoer/interpreter/interp-io.rkt @@ -5,13 +5,8 @@ ;; String Expr -> (Cons Value String) ;; Interpret e with given string as input, ;; return value and collected output as string -(define (interp/io e input) - (define result (box #f)) - (define output - (with-input-from-string input - (λ () - (with-output-to-string - (λ () - (set-box! result (interp e))))))) - (cons (unbox result) output)) - +(define (interp/io e in) + (parameterize ((current-output-port (open-output-string)) + (current-input-port (open-input-string in))) + (cons (interp e) + (get-output-string (current-output-port))))) diff --git a/evildoer/main.rkt b/evildoer/main.rkt index f9851a3..ae83f33 100644 --- a/evildoer/main.rkt +++ b/evildoer/main.rkt @@ -5,7 +5,6 @@ (require "interpreter/interp-io.rkt") (require "compiler/compile.rkt") (require "runtime/types.rkt") -(require "executor/run.rkt") (require "executor/exec.rkt") (provide (all-from-out "syntax/ast.rkt")) (provide (all-from-out "syntax/parse.rkt")) @@ -13,6 +12,5 @@ (provide (all-from-out "interpreter/interp-io.rkt")) (provide (all-from-out "compiler/compile.rkt")) (provide (all-from-out "runtime/types.rkt")) -(provide (all-from-out "executor/run.rkt")) (provide (all-from-out "executor/exec.rkt")) diff --git a/evildoer/test/run-compile-tests.rkt b/evildoer/test/run-compile-tests.rkt index 253889d..2a0cabf 100644 --- a/evildoer/test/run-compile-tests.rkt +++ b/evildoer/test/run-compile-tests.rkt @@ -1,10 +1,9 @@ #lang racket (require "../compiler/compile.rkt") (require "../syntax/parse.rkt") -(require "../executor/run.rkt") +(require "../executor/exec.rkt") (require "define-tests.rkt") +(test (λ (e) (exec (parse e)))) -(test (λ (e) (run (compile (parse e))))) - -(test/io (λ (i e) (run/io (compile (parse e)) i))) +(test/io (λ (i e) (exec/io (parse e) i))) diff --git a/extort/executor/run-stdin.rkt b/extort/executor/run-stdin.rkt index 7e7170f..ac60d60 100644 --- a/extort/executor/run-stdin.rkt +++ b/extort/executor/run-stdin.rkt @@ -8,5 +8,6 @@ ;; Compile contents of stdin and use asm-interp to run (define (main) (read-line) ; ignore #lang racket line + (run (compile (parse (read))))) diff --git a/extort/test/run-compile-tests.rkt b/extort/test/run-compile-tests.rkt index 253889d..95e541e 100644 --- a/extort/test/run-compile-tests.rkt +++ b/extort/test/run-compile-tests.rkt @@ -3,7 +3,6 @@ (require "../syntax/parse.rkt") (require "../executor/run.rkt") (require "define-tests.rkt") - (test (λ (e) (run (compile (parse e))))) (test/io (λ (i e) (run/io (compile (parse e)) i))) diff --git a/fraud/executor/run-stdin.rkt b/fraud/executor/run-stdin.rkt index 7e7170f..ac60d60 100644 --- a/fraud/executor/run-stdin.rkt +++ b/fraud/executor/run-stdin.rkt @@ -8,5 +8,6 @@ ;; Compile contents of stdin and use asm-interp to run (define (main) (read-line) ; ignore #lang racket line + (run (compile (parse (read))))) diff --git a/hoax/executor/run-stdin.rkt b/hoax/executor/run-stdin.rkt index 7e7170f..ac60d60 100644 --- a/hoax/executor/run-stdin.rkt +++ b/hoax/executor/run-stdin.rkt @@ -8,5 +8,6 @@ ;; Compile contents of stdin and use asm-interp to run (define (main) (read-line) ; ignore #lang racket line + (run (compile (parse (read))))) diff --git a/hustle/executor/run-stdin.rkt b/hustle/executor/run-stdin.rkt index 7e7170f..ac60d60 100644 --- a/hustle/executor/run-stdin.rkt +++ b/hustle/executor/run-stdin.rkt @@ -8,5 +8,6 @@ ;; Compile contents of stdin and use asm-interp to run (define (main) (read-line) ; ignore #lang racket line + (run (compile (parse (read))))) diff --git a/iniquity/executor/run-stdin.rkt b/iniquity/executor/run-stdin.rkt index 7e7170f..ac60d60 100644 --- a/iniquity/executor/run-stdin.rkt +++ b/iniquity/executor/run-stdin.rkt @@ -8,5 +8,6 @@ ;; Compile contents of stdin and use asm-interp to run (define (main) (read-line) ; ignore #lang racket line + (run (compile (parse (read))))) diff --git a/jig/executor/run-stdin.rkt b/jig/executor/run-stdin.rkt index 7e7170f..ac60d60 100644 --- a/jig/executor/run-stdin.rkt +++ b/jig/executor/run-stdin.rkt @@ -8,5 +8,6 @@ ;; Compile contents of stdin and use asm-interp to run (define (main) (read-line) ; ignore #lang racket line + (run (compile (parse (read))))) diff --git a/knock/executor/run-stdin.rkt b/knock/executor/run-stdin.rkt index 7e7170f..ac60d60 100644 --- a/knock/executor/run-stdin.rkt +++ b/knock/executor/run-stdin.rkt @@ -8,5 +8,6 @@ ;; Compile contents of stdin and use asm-interp to run (define (main) (read-line) ; ignore #lang racket line + (run (compile (parse (read))))) diff --git a/loot/executor/run-stdin.rkt b/loot/executor/run-stdin.rkt index 7e7170f..ac60d60 100644 --- a/loot/executor/run-stdin.rkt +++ b/loot/executor/run-stdin.rkt @@ -8,5 +8,6 @@ ;; Compile contents of stdin and use asm-interp to run (define (main) (read-line) ; ignore #lang racket line + (run (compile (parse (read))))) diff --git a/mug/executor/run-stdin.rkt b/mug/executor/run-stdin.rkt index 7e7170f..ac60d60 100644 --- a/mug/executor/run-stdin.rkt +++ b/mug/executor/run-stdin.rkt @@ -8,5 +8,6 @@ ;; Compile contents of stdin and use asm-interp to run (define (main) (read-line) ; ignore #lang racket line + (run (compile (parse (read)))))