From 8acf821e56d475e75648e93f8b928534da6b7d45 Mon Sep 17 00:00:00 2001 From: David Van Horn Date: Wed, 6 Mar 2024 13:14:01 -0500 Subject: [PATCH] Remove modules-in-progress lang. --- langs/modules-in-progress/Makefile | 46 - langs/modules-in-progress/README.md | 172 ---- langs/modules-in-progress/ast.rkt | 59 -- langs/modules-in-progress/build.rkt | 26 - langs/modules-in-progress/compile-file.rkt | 10 - langs/modules-in-progress/compile-ops.rkt | 350 -------- langs/modules-in-progress/compile.rkt | 274 ------ langs/modules-in-progress/io.c | 25 - langs/modules-in-progress/main.c | 40 - langs/modules-in-progress/parse.rkt | 115 --- langs/modules-in-progress/print.c | 839 ------------------ langs/modules-in-progress/print.h | 8 - langs/modules-in-progress/runtime.h | 11 - langs/modules-in-progress/stdlib-provides.rkt | 9 - langs/modules-in-progress/stdlib.rkt | 11 - .../test/build-runtime.rkt | 8 - .../modules-in-progress/test/test-runner.rkt | 255 ------ langs/modules-in-progress/types.h | 40 - langs/modules-in-progress/types.rkt | 66 -- langs/modules-in-progress/unload-bits-asm.rkt | 43 - langs/modules-in-progress/values.c | 108 --- langs/modules-in-progress/values.h | 75 -- 22 files changed, 2590 deletions(-) delete mode 100644 langs/modules-in-progress/Makefile delete mode 100644 langs/modules-in-progress/README.md delete mode 100644 langs/modules-in-progress/ast.rkt delete mode 100644 langs/modules-in-progress/build.rkt delete mode 100644 langs/modules-in-progress/compile-file.rkt delete mode 100644 langs/modules-in-progress/compile-ops.rkt delete mode 100644 langs/modules-in-progress/compile.rkt delete mode 100644 langs/modules-in-progress/io.c delete mode 100644 langs/modules-in-progress/main.c delete mode 100644 langs/modules-in-progress/parse.rkt delete mode 100644 langs/modules-in-progress/print.c delete mode 100644 langs/modules-in-progress/print.h delete mode 100644 langs/modules-in-progress/runtime.h delete mode 100644 langs/modules-in-progress/stdlib-provides.rkt delete mode 100644 langs/modules-in-progress/stdlib.rkt delete mode 100644 langs/modules-in-progress/test/build-runtime.rkt delete mode 100644 langs/modules-in-progress/test/test-runner.rkt delete mode 100644 langs/modules-in-progress/types.h delete mode 100644 langs/modules-in-progress/types.rkt delete mode 100644 langs/modules-in-progress/unload-bits-asm.rkt delete mode 100644 langs/modules-in-progress/values.c delete mode 100644 langs/modules-in-progress/values.h diff --git a/langs/modules-in-progress/Makefile b/langs/modules-in-progress/Makefile deleted file mode 100644 index bd832496..00000000 --- a/langs/modules-in-progress/Makefile +++ /dev/null @@ -1,46 +0,0 @@ -UNAME := $(shell uname) -.PHONY: test - -.SECONDARY: $(wild *.make) - -BUILD_DIR := . - -ifeq ($(UNAME), Darwin) - format=macho64 -else - format=elf64 -endif - -objs = \ - main.o \ - values.o \ - print.o \ - stdlib.o \ - io.o - -default: runtime.o - -runtime.o: $(objs) - ld -r $(objs) -o runtime.o - -.c.o: - gcc -fPIC -c -g -o $@ $< - -.s.o: - nasm -g -f $(format) -o $@ $< - -%.s: %.rkt - racket -t compile-file.rkt -m $< > $@ - -%.make: %.rkt - echo "MAKING" - racket -t build.rkt -m $< > $@ - -%.run: %.make - make $@ - -clean: - @$(RM) *.o *.s *.run $(BUILD_DIR)/*.make ||: - @echo "$(shell basename $(shell pwd)): cleaned!" - --include $(wildcard $(BUILD_DIR)/*.make) diff --git a/langs/modules-in-progress/README.md b/langs/modules-in-progress/README.md deleted file mode 100644 index 5210cbcf..00000000 --- a/langs/modules-in-progress/README.md +++ /dev/null @@ -1,172 +0,0 @@ -Standard Library and Modules -============================ - -This is a work in progress on a uniform approach to a standard library -and modules. It's mostly OK, but needs work on the file system -aspects and the Makefile is a little brittle. Not sure it fits in -with the `test-progs` approach to testing either. - - - -Standard Library ----------------- - -Here's a proposal for how to add a standard library. I think with -some small tweaks it can be cleaner and more uniform and support -user-defined libraries and we can drop the distinction between a -program and a library and just deal with modules. But first, let me -describe what I've added: - -There is a new file `compile-library.rkt` that given a file name, -reads the definitions in that file, compiles them, and then declares -those definitions as `Global`. - -There is a standard library implementation in `stdlib.rkt`. - -The `Makefile` now declares the runtime depends on `stdlib.o` and -there is a special rule to compile `stdlib.s` by using -`compile-library.rkt` instead of `compile-file.rkt`. - -The program compiler has been updated to declared standard library -functions as `External` and the parser will parse any non-primitive -application as `(App ...)` so any use of a standard library -function is just a function call. - -Example -------- - -Here's an example of a program using a standard library function -`reverse`; notice how `stdlib.rkt` gets compiled with -`compile-library.rkt`: - -``` -> more example.rkt -#lang racket -(reverse (cons 1 (cons 2 (cons 3 '())))) - -> make example.run -gcc -fPIC -c -g -o main.o main.c -gcc -fPIC -c -g -o values.o values.c -gcc -fPIC -c -g -o print.o print.c -racket -t compile-library.rkt -m stdlib.rkt > stdlib.s -nasm -g -f macho64 -o stdlib.o stdlib.s -gcc -fPIC -c -g -o io.o io.c -ld -r main.o values.o print.o stdlib.o io.o -o runtime.o -racket -t compile-file.rkt -m example.rkt > example.s -nasm -g -f macho64 -o example.o example.s -gcc runtime.o example.o -o example.run -rm example.o example.s -> ./example.run -'(3 2 1) -``` - - -Discussion ----------- - -A library and a program are pretty similar. The key difference is a -library has no top-level expression, so the compiler does not emit an -`entry` label. - -The other difference is a library declares all defined functions with -`Global`. - -One way to unify the two is to handle `provide` specs at the top of a -library/program. Everything in the `provide` gets declared -`Global`. The programs we've been writing implicitly have an empty -`provide` spec. - -It's also pretty easy to handle `require` specs. A `require` gives a -filename, the compiler reads that file fetching the `provides` and -declares those things `External`. - -The `Makefile` would need to be updated to compute the dependencies of -.rkt file by reading the requires. Every file implicitly depends on -stdlib.rkt and there would have to be a special case when computing -stdlib.rkt's dependecies (so it wouldn't be circular). - -At that point, I think compile-library.rkt can go away and there can -be a single notion of a "module". - -EXCEPT: I don't quite know how to resolve the issue of there being a -distinguished module that contains the entry point the runtime should -jump to. - -One idea is that the runtime should jump to a `main` function. So one -of the modules will need to provide `main`, and now everything is just -module with a uniform compilation strategy. The parser could be set -up so that if there's only an expression in the file, it parses it as -`(provide main) (define (main) e)`. - -Modules -------- - -OK, I implemented roughly what's described above. Now there's just a -notion of a module and a uniform way of compiling modules. - -A module that has an expression in it or a main function is considered -the main module and the run-time will jump to it. - -I more or less am happy with this EXCEPT, building programs is more of -a pain now because `make` doesn't just magically work. If a module -requires another module, you have to build its object code and then -link it by hand. - -Here's an example; the main module is example.rkt, and it requires -sum.rkt, and also uses a standard library function, `reverse`: - -``` -> cat sum.rkt -#lang racket -(provide sum) -(define (sum xs) - (if (empty? xs) - 0 - (+ (car xs) (sum (cdr xs))))) - -> cat example.rkt -#lang racket -(require "sum.rkt") -(sum (reverse (cons 1 (cons 2 (cons 3 '()))))) - -> make runtime.o -gcc -fPIC -c -g -o main.o main.c -gcc -fPIC -c -g -o values.o values.c -gcc -fPIC -c -g -o print.o print.c -racket -t compile-file.rkt -m stdlib.rkt > stdlib.s -nasm -g -f macho64 -o stdlib.o stdlib.s -gcc -fPIC -c -g -o io.o io.c -ld -r main.o values.o print.o stdlib.o io.o -o runtime.o -rm stdlib.s -> make sum.o -racket -t compile-file.rkt -m sum.rkt > sum.s -nasm -g -f macho64 -o sum.o sum.s -rm sum.s -> make example.o -racket -t compile-file.rkt -m example.rkt > example.s -nasm -g -f macho64 -o example.o example.s -rm example.s -> gcc example.o sum.o runtime.o -o example.run -> ./example.run -6 -``` - -The problem with `make example.run` is that the Makefile doesn't know -anything about `example.rkt` requiring `sum.rkt`, so it doesn't know -to make `sum.o` and link it in to the final executable. - -I think part of the problem is that the Makefile is playing two roles: - -* as a Makefile for building the compiler -* as a utility for running the compiler - -Maybe the latter is an abuse of the Makefile. I'm not sure if we -should do something else, or continue the abuse. But if it's the -latter, I think we have to do something like this: - -https://www.gnu.org/software/make/manual/html_node/Automatic-Prerequisites.html - -which seems kinda awful. - - - diff --git a/langs/modules-in-progress/ast.rkt b/langs/modules-in-progress/ast.rkt deleted file mode 100644 index 253b5d61..00000000 --- a/langs/modules-in-progress/ast.rkt +++ /dev/null @@ -1,59 +0,0 @@ -#lang racket -(provide (all-defined-out)) - -;; Concrete syntax: - -;; module ::= * -;; ::= (provide id*) -;; | (require *) -;; | (define ( *) ) -;; | - -;; type Module = (Module [Listof Id] [Listof String] [Listof Defn]) -(struct Module (ps rs ds) #:prefab) - -;; type Defn = (Defn Id (Listof Id) Expr) -(struct Defn (f xs e) #:prefab) - -;; type Expr = (Eof) -;; | (Empty) -;; | (Int Integer) -;; | (Bool Boolean) -;; | (Char Character) -;; | (Str String) -;; | (Prim0 Op0) -;; | (Prim1 Op1 Expr) -;; | (Prim2 Op2 Expr Expr) -;; | (Prim3 Op3 Expr Expr Expr) -;; | (If Expr Expr Expr) -;; | (Begin Expr Expr) -;; | (Let Id Expr Expr) -;; | (Var Id) -;; | (App Id (Listof Expr)) -;; type Id = Symbol -;; type Op0 = 'read-byte -;; type Op1 = 'add1 | 'sub1 | 'zero? -;; | 'char? | 'integer->char | 'char->integer -;; | 'write-byte | 'eof-object? -;; | 'box | 'car | 'cdr | 'unbox -;; | 'empty? | 'cons? | 'box? -;; | 'vector? | vector-length -;; type Op2 = '+ | '- -;; | 'cons -;; | 'make-vector | 'vector-ref -;; type Op3 = 'vector-set! -(struct Eof () #:prefab) -(struct Empty () #:prefab) -(struct Int (i) #:prefab) -(struct Bool (b) #:prefab) -(struct Char (c) #:prefab) -(struct Str (s) #:prefab) -(struct Prim0 (p) #:prefab) -(struct Prim1 (p e) #:prefab) -(struct Prim2 (p e1 e2) #:prefab) -(struct Prim3 (p e1 e2 e3) #:prefab) -(struct If (e1 e2 e3) #:prefab) -(struct Begin (e1 e2) #:prefab) -(struct Let (x e1 e2) #:prefab) -(struct Var (x) #:prefab) -(struct App (f es) #:prefab) diff --git a/langs/modules-in-progress/build.rkt b/langs/modules-in-progress/build.rkt deleted file mode 100644 index d671eb94..00000000 --- a/langs/modules-in-progress/build.rkt +++ /dev/null @@ -1,26 +0,0 @@ -#lang racket -(provide main) -(require "parse.rkt" "ast.rkt" (rename-in "compile-file.rkt" [main compile-file])) - -;; Compiles a complete program, including dependencies - -(define (fmt) - (match (system-type 'os) - ['unix "elf64"] - ['macosx "macho64"])) - -(define (main fn) - (let ((root (suffix fn "")) - (ds (apply string-append (add-between (deps fn) " ")))) - ; (printf "%.s: %.rkt\n\tracket -t compile-file.rkt -m $< > $@\n\n") - ; (printf ".s.o:\n\tnasm -g -f ~a -o $@ $<\n\n" (fmt)) - (printf "~arun: ~ao runtime.o ~a\n\tgcc ~a $< runtime.o -o $@\n\n" root root ds ds))) - - -(define (suffix fn s) - (string-append (substring fn 0 (- (string-length fn) 3)) s)) - -(define (deps fn) - (match (parse-module-file fn) - [(Module ps rs ds) - (append-map (λ (r) (cons (suffix (car r) "o") (deps (car r)))) rs)])) diff --git a/langs/modules-in-progress/compile-file.rkt b/langs/modules-in-progress/compile-file.rkt deleted file mode 100644 index c786fb19..00000000 --- a/langs/modules-in-progress/compile-file.rkt +++ /dev/null @@ -1,10 +0,0 @@ -#lang racket -(provide main) -(require "parse.rkt" "compile.rkt" a86/printer) - -;; String -> Void -;; Compile contents of given file name, -;; emit asm code on stdout -(define (main fn) - (displayln (asm-string (compile-module (parse-module-file fn))))) -;(parse-module-file (simplify-path (path->complete-path fn)))) diff --git a/langs/modules-in-progress/compile-ops.rkt b/langs/modules-in-progress/compile-ops.rkt deleted file mode 100644 index bd96e16d..00000000 --- a/langs/modules-in-progress/compile-ops.rkt +++ /dev/null @@ -1,350 +0,0 @@ -#lang racket -(provide (all-defined-out)) -(require "ast.rkt" "types.rkt" a86/ast) - -(define rax 'rax) ; return -(define eax 'eax) ; 32-bit load/store -(define rbx 'rbx) ; heap -(define rdi 'rdi) ; arg -(define r8 'r8) ; scratch -(define r9 'r9) ; scratch -(define r10 'r10) ; scratch -(define r15 'r15) ; stack pad (non-volatile) -(define rsp 'rsp) ; stack - -;; Op0 -> Asm -(define (compile-op0 p) - (match p - ['void (seq (Mov rax val-void))] - ['read-byte (seq pad-stack - (Call 'read_byte) - unpad-stack)] - ['peek-byte (seq pad-stack - (Call 'peek_byte) - unpad-stack)])) - -;; Op1 -> Asm -(define (compile-op1 p) - (match p - ['add1 - (seq (assert-integer rax) - (Add rax (imm->bits 1)))] - ['sub1 - (seq (assert-integer rax) - (Sub rax (imm->bits 1)))] - ['zero? - (seq (assert-integer rax) - (eq-imm 0))] - ['char? - (type-pred mask-char type-char)] - ['char->integer - (seq (assert-char rax) - (Sar rax char-shift) - (Sal rax int-shift))] - ['integer->char - (seq (assert-codepoint rax) - (Sar rax int-shift) - (Sal rax char-shift) - (Xor rax type-char))] - ['eof-object? (eq-imm eof)] - ['write-byte - (seq (assert-byte rax) - pad-stack - (Mov rdi rax) - (Call 'write_byte) - unpad-stack - (Mov rax val-void))] - ['box - (seq (Mov (Offset rbx 0) rax) - (Mov rax rbx) - (Or rax type-box) - (Add rbx 8))] - ['unbox - (seq (assert-box rax) - (Xor rax type-box) - (Mov rax (Offset rax 0)))] - ['car - (seq (assert-cons rax) - (Xor rax type-cons) - (Mov rax (Offset rax 8)))] - ['cdr - (seq (assert-cons rax) - (Xor rax type-cons) - (Mov rax (Offset rax 0)))] - ['empty? (eq-imm '())] - ['box? - (type-pred ptr-mask type-box)] - ['cons? - (type-pred ptr-mask type-cons)] - ['vector? - (type-pred ptr-mask type-vect)] - ['string? - (type-pred ptr-mask type-str)] - ['vector-length - (let ((zero (gensym)) - (done (gensym))) - (seq (assert-vector rax) - (Xor rax type-vect) - (Cmp rax 0) - (Je zero) - (Mov rax (Offset rax 0)) - (Sal rax int-shift) - (Jmp done) - (Label zero) - (Mov rax 0) - (Label done)))] - ['string-length - (let ((zero (gensym)) - (done (gensym))) - (seq (assert-string rax) - (Xor rax type-str) - (Cmp rax 0) - (Je zero) - (Mov rax (Offset rax 0)) - (Sal rax int-shift) - (Jmp done) - (Label zero) - (Mov rax 0) - (Label done)))])) - -;; Op2 -> Asm -(define (compile-op2 p) - (match p - ['+ - (seq (Pop r8) - (assert-integer r8) - (assert-integer rax) - (Add rax r8))] - ['- - (seq (Pop r8) - (assert-integer r8) - (assert-integer rax) - (Sub r8 rax) - (Mov rax r8))] - ['< - (seq (Pop r8) - (assert-integer r8) - (assert-integer rax) - (Cmp r8 rax) - (Mov rax val-true) - (let ((true (gensym))) - (seq (Jl true) - (Mov rax val-false) - (Label true))))] - ['= - (seq (Pop r8) - (assert-integer r8) - (assert-integer rax) - (Cmp r8 rax) - (Mov rax val-true) - (let ((true (gensym))) - (seq (Je true) - (Mov rax val-false) - (Label true))))] - ['cons - (seq (Mov (Offset rbx 0) rax) - (Pop rax) - (Mov (Offset rbx 8) rax) - (Mov rax rbx) - (Or rax type-cons) - (Add rbx 16))] - ['make-vector - (let ((loop (gensym)) - (done (gensym)) - (empty (gensym))) - (seq (Pop r8) - (assert-natural r8) - (Cmp r8 0) ; special case empty vector - (Je empty) - - (Mov r9 rbx) - (Or r9 type-vect) - - (Sar r8 int-shift) - (Mov (Offset rbx 0) r8) - (Add rbx 8) - - (Label loop) - (Mov (Offset rbx 0) rax) - (Add rbx 8) - (Sub r8 1) - (Cmp r8 0) - (Jne loop) - - (Mov rax r9) - (Jmp done) - - (Label empty) - (Mov rax type-vect) - (Label done)))] - - ['vector-ref - (seq (Pop r8) - (assert-vector r8) - (assert-integer rax) - (Cmp rax 0) - (Jl 'raise_error_align) - (Xor r8 type-vect) ; r8 = ptr - (Mov r9 (Offset r8 0)) ; r9 = len - (Sar rax int-shift) ; rax = index - (Sub r9 1) - (Cmp r9 rax) - (Jl 'raise_error_align) - (Sal rax 3) - (Add r8 rax) - (Mov rax (Offset r8 8)))] - - ['make-string - (let ((loop (gensym)) - (done (gensym)) - (empty (gensym))) - (seq (Pop r8) - (assert-natural r8) - (assert-char rax) - (Cmp r8 0) ; special case empty string - (Je empty) - - (Mov r9 rbx) - (Or r9 type-str) - - (Sar r8 int-shift) - (Mov (Offset rbx 0) r8) - (Add rbx 8) - - (Sar rax char-shift) - - (Add r9 1) ; adds 1 - (Sar r9 1) ; when - (Sal r9 1) ; len is odd - - (Label loop) - (Mov (Offset rbx 0) eax) - (Add rbx 4) - (Sub r8 1) - (Cmp r8 0) - (Jne loop) - - (Mov rax r9) - (Jmp done) - - (Label empty) - (Mov rax type-str) - (Label done)))] - - ['string-ref - (seq (Pop r8) - (assert-string r8) - (assert-integer rax) - (Cmp rax 0) - (Jl 'raise_error_align) - (Xor r8 type-str) ; r8 = ptr - (Mov r9 (Offset r8 0)) ; r9 = len - (Sar rax int-shift) ; rax = index - (Sub r9 1) - (Cmp r9 rax) - (Jl 'raise_error_align) - (Sal rax 2) - (Add r8 rax) - (Mov 'eax (Offset r8 8)) - (Sal rax char-shift) - (Or rax type-char))])) - -;; Op3 -> Asm -(define (compile-op3 p) - (match p - ['vector-set! - (seq (Pop r10) - (Pop r8) - (assert-vector r8) - (assert-integer r10) - (Cmp r10 0) - (Jl 'raise_error_align) - (Xor r8 type-vect) ; r8 = ptr - (Mov r9 (Offset r8 0)) ; r9 = len - (Sar r10 int-shift) ; r10 = index - (Sub r9 1) - (Cmp r9 r10) - (Jl 'raise_error_align) - (Sal r10 3) - (Add r8 r10) - (Mov (Offset r8 8) rax) - (Mov rax val-void))])) - - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -(define (assert-type mask type) - (λ (arg) - (seq (Mov r9 arg) - (And r9 mask) - (Cmp r9 type) - (Jne 'raise_error_align)))) - -(define (type-pred mask type) - (let ((l (gensym))) - (seq (And rax mask) - (Cmp rax type) - (Mov rax (imm->bits #t)) - (Je l) - (Mov rax (imm->bits #f)) - (Label l)))) - -(define assert-integer - (assert-type mask-int type-int)) -(define assert-char - (assert-type mask-char type-char)) -(define assert-box - (assert-type ptr-mask type-box)) -(define assert-cons - (assert-type ptr-mask type-cons)) -(define assert-vector - (assert-type ptr-mask type-vect)) -(define assert-string - (assert-type ptr-mask type-str)) - -(define (assert-codepoint r) - (let ((ok (gensym))) - (seq (assert-integer r) - (Cmp r (imm->bits 0)) - (Jl 'raise_error_align) - (Cmp r (imm->bits 1114111)) - (Jg 'raise_error_align) - (Cmp r (imm->bits 55295)) - (Jl ok) - (Cmp r (imm->bits 57344)) - (Jg ok) - (Jmp 'raise_error_align) - (Label ok)))) - -(define (assert-byte r) - (seq (assert-integer r) - (Cmp r (imm->bits 0)) - (Jl 'raise_error_align) - (Cmp r (imm->bits 255)) - (Jg 'raise_error_align))) - -(define (assert-natural r) - (seq (assert-integer r) - (Cmp r (imm->bits 0)) - (Jl 'raise_error_align))) - -;; Value -> Asm -(define (eq-imm imm) - (let ((l1 (gensym))) - (seq (Cmp rax (imm->bits imm)) - (Mov rax val-true) - (Je l1) - (Mov rax val-false) - (Label l1)))) - -;; Asm -;; Dynamically pad the stack to be aligned for a call -(define pad-stack - (seq (Mov r15 rsp) - (And r15 #b1000) - (Sub rsp r15))) - -;; Asm -;; Undo the stack alignment after a call -(define unpad-stack - (seq (Add rsp r15))) diff --git a/langs/modules-in-progress/compile.rkt b/langs/modules-in-progress/compile.rkt deleted file mode 100644 index 2d2e778b..00000000 --- a/langs/modules-in-progress/compile.rkt +++ /dev/null @@ -1,274 +0,0 @@ -#lang racket -(provide (all-defined-out)) -(require "ast.rkt" "types.rkt" "compile-ops.rkt" "stdlib-provides.rkt" a86/ast) - -;; Registers used -(define rax 'rax) ; return -(define rbx 'rbx) ; heap -(define rsp 'rsp) ; stack -(define rdi 'rdi) ; arg - -;; type CEnv = [Listof Variable] - -;; Module -> Asm -(define (compile-module m) - (match (normalize-module m) - [(Module ps rs ds) - (prog (externs) - (compile-provides ps) - (compile-requires rs) - (compile-main ds) - (compile-defines ds) - (Label 'raise_error_align) - pad-stack - (Call 'raise_error))])) - -(define compile compile-module) - -;; Module -> Module -;; Remove anything from requires that is provided by this module -(define (normalize-module m) - (match m - [(Module ps rs ds) - (Module ps (normalize-provides ps rs) ds)])) - -(define (normalize-provides ps rs) - (remq* ps (append stdlib-provides - (append-map second rs)))) - -(define (compile-main ds) - (if (main? ds) - (seq (Global 'entry) - (Label 'entry) - (Mov rbx rdi) ; recv heap pointer - (Jmp (symbol->label 'main))) - (seq))) - -(define (main? ds) - (match ds - ['() #f] - [(cons (Defn 'main '() _) ds) #t] - [(cons _ ds) (main? ds)])) - -(define (compile-provides ps) - (map (λ (p) (Global (symbol->label p))) ps)) - -(define (compile-requires rs) - (map (λ (r) (Extern (symbol->label r))) rs)) - -#| -;; Prog -> Asm -(define (compile p) - (match p - [(Prog ds e) - (prog (Global 'entry) - (externs) - (stdlib) - (Label 'entry) - (Mov rbx rdi) ; recv heap pointer - (compile-e e '() #t) - (Ret) - (Label 'raise_error_align) - (Sub rsp 8) - (Jmp 'raise_error) - (compile-defines ds))])) -|# - -;; -> [Listof Extern] -(define (stdlib) - (map (lambda (l) - (Extern (symbol->label l))) - '(reverse))) - -;; -> [Listof Extern] -(define (externs) - (seq (Extern 'peek_byte) - (Extern 'read_byte) - (Extern 'write_byte) - (Extern 'raise_error))) - -;; [Listof Defn] -> Asm -(define (compile-defines ds) - (match ds - ['() (seq)] - [(cons d ds) - (seq (compile-define d) - (compile-defines ds))])) - -;; Defn -> Asm -(define (compile-define d) - (match d - [(Defn f xs e) - (seq (Label (symbol->label f)) - (compile-e e (reverse xs) #t) - (Add rsp (* 8 (length xs))) ; pop args - (Ret))])) - -;; Expr CEnv Bool -> Asm -(define (compile-e e c t?) - (match e - [(Int i) (compile-value i)] - [(Bool b) (compile-value b)] - [(Char c) (compile-value c)] - [(Eof) (compile-value eof)] - [(Empty) (compile-value '())] - [(Var x) (compile-variable x c)] - [(Str s) (compile-string s)] - [(Prim0 p) (compile-prim0 p c)] - [(Prim1 p e) (compile-prim1 p e c)] - [(Prim2 p e1 e2) (compile-prim2 p e1 e2 c)] - [(Prim3 p e1 e2 e3) (compile-prim3 p e1 e2 e3 c)] - [(If e1 e2 e3) (compile-if e1 e2 e3 c t?)] - [(Begin e1 e2) (compile-begin e1 e2 c t?)] - [(Let x e1 e2) (compile-let x e1 e2 c t?)] - [(App f es) (compile-app f es c t?)])) - -;; Value -> Asm -(define (compile-value v) - (seq (Mov rax (imm->bits v)))) - -;; Id CEnv -> Asm -(define (compile-variable x c) - (let ((i (lookup x c))) - (seq (Mov rax (Offset rsp i))))) - -;; String -> Asm -(define (compile-string s) - (let ((len (string-length s))) - (if (zero? len) - (seq (Mov rax type-str)) - (seq (Mov rax len) - (Mov (Offset rbx 0) rax) - (compile-string-chars (string->list s) 8) - (Mov rax rbx) - (Or rax type-str) - (Add rbx - (+ 8 (* 4 (if (odd? len) (add1 len) len)))))))) - -;; [Listof Char] Integer -> Asm -(define (compile-string-chars cs i) - (match cs - ['() (seq)] - [(cons c cs) - (seq (Mov rax (char->integer c)) - (Mov (Offset rbx i) 'eax) - (compile-string-chars cs (+ 4 i)))])) - -;; Op0 CEnv -> Asm -(define (compile-prim0 p c) - (compile-op0 p)) - -;; Op1 Expr CEnv -> Asm -(define (compile-prim1 p e c) - (seq (compile-e e c #f) - (compile-op1 p))) - -;; Op2 Expr Expr CEnv -> Asm -(define (compile-prim2 p e1 e2 c) - (seq (compile-e e1 c #f) - (Push rax) - (compile-e e2 (cons #f c) #f) - (compile-op2 p))) - -;; Op3 Expr Expr Expr CEnv -> Asm -(define (compile-prim3 p e1 e2 e3 c) - (seq (compile-e e1 c #f) - (Push rax) - (compile-e e2 (cons #f c) #f) - (Push rax) - (compile-e e3 (cons #f (cons #f c)) #f) - (compile-op3 p))) - -;; Expr Expr Expr CEnv Bool -> Asm -(define (compile-if e1 e2 e3 c t?) - (let ((l1 (gensym 'if)) - (l2 (gensym 'if))) - (seq (compile-e e1 c #f) - (Cmp rax val-false) - (Je l1) - (compile-e e2 c t?) - (Jmp l2) - (Label l1) - (compile-e e3 c t?) - (Label l2)))) - -;; Expr Expr CEnv Bool -> Asm -(define (compile-begin e1 e2 c t?) - (seq (compile-e e1 c #f) - (compile-e e2 c t?))) - -;; Id Expr Expr CEnv Bool -> Asm -(define (compile-let x e1 e2 c t?) - (seq (compile-e e1 c #f) - (Push rax) - (compile-e e2 (cons x c) t?) - (Add rsp 8))) - -;; Id [Listof Expr] CEnv Bool -> Asm -(define (compile-app f es c t?) - (if t? - (compile-app-tail f es c) - (compile-app-nontail f es c))) - -;; Id [Listof Expr] CEnv -> Asm -(define (compile-app-tail f es c) - (seq (compile-es es c) - (move-args (length es) (length c)) - (Add rsp (* 8 (length c))) - (Jmp (symbol->label f)))) - -;; Integer Integer -> Asm -(define (move-args i off) - (cond [(zero? off) (seq)] - [(zero? i) (seq)] - [else - (seq (Mov r8 (Offset rsp (* 8 (sub1 i)))) - (Mov (Offset rsp (* 8 (+ off (sub1 i)))) r8) - (move-args (sub1 i) off))])) - -;; Id [Listof Expr] CEnv -> Asm -;; The return address is placed above the arguments, so callee pops -;; arguments and return address is next frame -(define (compile-app-nontail f es c) - (let ((r (gensym 'ret))) - (seq (Lea rax r) - (Push rax) - (compile-es es (cons #f c)) - (Jmp (symbol->label f)) - (Label r)))) - -;; [Listof Expr] CEnv -> Asm -(define (compile-es es c) - (match es - ['() '()] - [(cons e es) - (seq (compile-e e c #f) - (Push rax) - (compile-es es (cons #f c)))])) - -;; Id CEnv -> Integer -(define (lookup x cenv) - (match cenv - ['() (error "undefined variable:" x)] - [(cons y rest) - (match (eq? x y) - [#t 0] - [#f (+ 8 (lookup x rest))])])) - -;; Symbol -> Label -;; Produce a symbol that is a valid Nasm label -(define (symbol->label s) - (string->symbol - (string-append - "label_" - (list->string - (map (λ (c) - (if (or (char<=? #\a c #\z) - (char<=? #\A c #\Z) - (char<=? #\0 c #\9) - (memq c '(#\_ #\$ #\# #\@ #\~ #\. #\?))) - c - #\_)) - (string->list (symbol->string s)))) - "_" - (number->string (eq-hash-code s) 16)))) diff --git a/langs/modules-in-progress/io.c b/langs/modules-in-progress/io.c deleted file mode 100644 index 7ef82281..00000000 --- a/langs/modules-in-progress/io.c +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include -#include "types.h" -#include "values.h" -#include "runtime.h" - -val_t read_byte(void) -{ - char c = getc(in); - return (c == EOF) ? val_wrap_eof() : val_wrap_int(c); -} - -val_t peek_byte(void) -{ - char c = getc(in); - ungetc(c, in); - return (c == EOF) ? val_wrap_eof() : val_wrap_int(c); - -} - -val_t write_byte(val_t c) -{ - putc((char) val_unwrap_int(c), out); - return val_wrap_void(); -} diff --git a/langs/modules-in-progress/main.c b/langs/modules-in-progress/main.c deleted file mode 100644 index 1ca6115f..00000000 --- a/langs/modules-in-progress/main.c +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include -#include "values.h" -#include "print.h" -#include "runtime.h" - -FILE* in; -FILE* out; -void (*error_handler)(); -val_t *heap; - -void error_exit() -{ - printf("err\n"); - exit(1); -} - -void raise_error() -{ - return error_handler(); -} - -int main(int argc, char** argv) -{ - in = stdin; - out = stdout; - error_handler = &error_exit; - heap = malloc(8 * heap_size); - - val_t result; - - result = entry(heap); - - print_result(result); - if (val_typeof(result) != T_VOID) - putchar('\n'); - - free(heap); - return 0; -} diff --git a/langs/modules-in-progress/parse.rkt b/langs/modules-in-progress/parse.rkt deleted file mode 100644 index d4f931e8..00000000 --- a/langs/modules-in-progress/parse.rkt +++ /dev/null @@ -1,115 +0,0 @@ -#lang racket -(provide parse parse-e parse-define parse-module parse-module-file) -(require "ast.rkt") - -;; Need to pass around the module's file name in order to resolve -;; paths in requires. - -;; String -> Module -(define (parse-module-file fn) - (let ((p (open-input-file fn))) - (begin (read-line p) ; ignore #lang racket line - (begin0 (parse-module (read-all p) fn) - (close-input-port p))))) - -;; Port -> SExpr -(define (read-all p) - (let ((r (read p))) - (if (eof-object? r) - '() - (cons r (read-all p))))) - -;; S-Expr Path -> Module -(define (parse-module m p) - (match (parse-module* m p) - [(list ps rs ds #f) - (Module ps rs ds)] - [(list ps rs ds e) - (Module (cons 'main ps) rs (cons (Defn 'main '() e) ds))])) - -(define parse (lambda (m) (parse-module m ""))) - -(define (parse-module* m p) - (match m - ['() (list '() '() '() #f)] - [(cons x m) - (match (parse-module* m p) - [(list ps rs ds e) - (match x - [(cons 'provide _) - (list (append (parse-provide x) ps) rs ds e)] - [(cons 'require _) - (list ps (append (parse-require x p) rs) ds e)] - [(cons 'define _) - (list ps rs (cons (parse-define x) ds) e)] - [_ - (list ps rs ds - (if e - (Begin (parse-e x) e) - (parse-e x)))])])])) - -(define (parse-provide x) - (match x - [(cons 'provide xs) - (if (andmap symbol? xs) - xs - (error "invalid provide clause"))])) - -(define (parse-require x) - (match x - [(cons 'require xs) - (if (andmap string? xs) - (map (lambda (x) - (list x (Module-ps (parse-module-file x)))) - xs) - (error "invalid require clause"))])) - -;; S-Expr -> Defn -(define (parse-define s) - (match s - [(list 'define (list (? symbol? f) (? symbol? xs) ...) e) - (Defn f xs (parse-e e))] - [_ (error "Parse defn error" s)])) - -;; S-Expr -> Expr -(define (parse-e s) - (match s - [(? integer?) (Int s)] - [(? boolean?) (Bool s)] - [(? char?) (Char s)] - [(? string?) (Str s)] - ['eof (Eof)] - [(? symbol?) (Var s)] - [(list 'quote (list)) (Empty)] - [(list (? (op? op0) p0)) (Prim0 p0)] - [(list (? (op? op1) p1) e) (Prim1 p1 (parse-e e))] - [(list (? (op? op2) p2) e1 e2) (Prim2 p2 (parse-e e1) (parse-e e2))] - [(list (? (op? op3) p3) e1 e2 e3) - (Prim3 p3 (parse-e e1) (parse-e e2) (parse-e e3))] - [(list 'begin e1 e2) - (Begin (parse-e e1) (parse-e e2))] - [(list 'if e1 e2 e3) - (If (parse-e e1) (parse-e e2) (parse-e e3))] - [(list 'let (list (list (? symbol? x) e1)) e2) - (Let x (parse-e e1) (parse-e e2))] - [(cons (? symbol? f) es) - (App f (map parse-e es))] - [_ (error "Parse error" s)])) - -(define op0 - '(read-byte peek-byte void)) - -(define op1 - '(add1 sub1 zero? char? write-byte eof-object? - integer->char char->integer - box unbox empty? cons? box? car cdr - vector? vector-length string? string-length)) -(define op2 - '(+ - < = cons make-vector vector-ref make-string string-ref)) -(define op3 - '(vector-set!)) - -(define (op? ops) - (λ (x) - (and (symbol? x) - (memq x ops)))) diff --git a/langs/modules-in-progress/print.c b/langs/modules-in-progress/print.c deleted file mode 100644 index a88a5779..00000000 --- a/langs/modules-in-progress/print.c +++ /dev/null @@ -1,839 +0,0 @@ -#include -#include -#include "values.h" - -void print_char(val_char_t); -void print_codepoint(val_char_t); -void print_cons(val_cons_t *); -void print_vect(val_vect_t*); -void print_str(val_str_t*); -void print_str_char(val_char_t); -void print_result_interior(val_t); -int utf8_encode_char(val_char_t, char *); - -void print_result(val_t x) -{ - switch (val_typeof(x)) { - case T_INT: - printf("%" PRId64, val_unwrap_int(x)); - break; - case T_BOOL: - printf(val_unwrap_bool(x) ? "#t" : "#f"); - break; - case T_CHAR: - print_char(val_unwrap_char(x)); - break; - case T_EOF: - printf("#"); - break; - case T_VOID: - break; - case T_EMPTY: - case T_BOX: - case T_CONS: - case T_VECT: - printf("'"); - print_result_interior(x); - break; - case T_STR: - putchar('"'); - print_str(val_unwrap_str(x)); - putchar('"'); - break; - case T_INVALID: - printf("internal error"); - } -} - -void print_result_interior(val_t x) -{ - switch (val_typeof(x)) { - case T_EMPTY: - printf("()"); - break; - case T_BOX: - printf("#&"); - print_result_interior(val_unwrap_box(x)->val); - break; - case T_CONS: - printf("("); - print_cons(val_unwrap_cons(x)); - printf(")"); - break; - case T_VECT: - print_vect(val_unwrap_vect(x)); - break; - default: - print_result(x); - } -} - -void print_vect(val_vect_t *v) -{ - uint64_t i; - - if (!v) { printf("#()"); return; } - - printf("#("); - for (i = 0; i < v->len; ++i) { - print_result_interior(v->elems[i]); - - if (i < v->len - 1) - putchar(' '); - } - printf(")"); -} - -void print_cons(val_cons_t *cons) -{ - print_result_interior(cons->fst); - - switch (val_typeof(cons->snd)) { - case T_EMPTY: - // nothing - break; - case T_CONS: - printf(" "); - print_cons(val_unwrap_cons(cons->snd)); - break; - default: - printf(" . "); - print_result_interior(cons->snd); - break; - } -} - -void print_str(val_str_t* s) -{ - if (!s) return; - uint64_t i; - for (i = 0; i < s->len; ++i) - print_str_char(s->codepoints[i]); -} - -void print_str_char_u(val_char_t c) -{ - printf("\\u%04X", c); -} - -void print_str_char_U(val_char_t c) -{ - printf("\\U%08X", c); -} - -void print_str_char(val_char_t c) -{ - switch (c) { - case 0 ... 6: - print_str_char_u(c); - break; - case 7: - printf("\\a"); - break; - case 8: - printf("\\b"); - break; - case 9: - printf("\\t"); - break; - case 10: - printf("\\n"); - break; - case 11: - printf("\\v"); - break; - case 12: - printf("\\f"); - break; - case 13: - printf("\\r"); - break; - case 14 ... 26: - print_str_char_u(c); - break; - case 27: - printf("\\e"); - break; - case 28 ... 31: - print_str_char_u(c); - break; - case 34: - printf("\\\""); - break; - case 39: - printf("'"); - break; - case 92: - printf("\\\\"); - break; - case 127 ... 159: - case 173 ... 173: - case 888 ... 889: - case 896 ... 899: - case 907 ... 907: - case 909 ... 909: - case 930 ... 930: - case 1328 ... 1328: - case 1367 ... 1368: - case 1376 ... 1376: - case 1416 ... 1416: - case 1419 ... 1420: - case 1424 ... 1424: - case 1480 ... 1487: - case 1515 ... 1519: - case 1525 ... 1541: - case 1564 ... 1565: - case 1757 ... 1757: - case 1806 ... 1807: - case 1867 ... 1868: - case 1970 ... 1983: - case 2043 ... 2047: - case 2094 ... 2095: - case 2111 ... 2111: - case 2140 ... 2141: - case 2143 ... 2207: - case 2227 ... 2275: - case 2436 ... 2436: - case 2445 ... 2446: - case 2449 ... 2450: - case 2473 ... 2473: - case 2481 ... 2481: - case 2483 ... 2485: - case 2490 ... 2491: - case 2501 ... 2502: - case 2505 ... 2506: - case 2511 ... 2518: - case 2520 ... 2523: - case 2526 ... 2526: - case 2532 ... 2533: - case 2556 ... 2560: - case 2564 ... 2564: - case 2571 ... 2574: - case 2577 ... 2578: - case 2601 ... 2601: - case 2609 ... 2609: - case 2612 ... 2612: - case 2615 ... 2615: - case 2618 ... 2619: - case 2621 ... 2621: - case 2627 ... 2630: - case 2633 ... 2634: - case 2638 ... 2640: - case 2642 ... 2648: - case 2653 ... 2653: - case 2655 ... 2661: - case 2678 ... 2688: - case 2692 ... 2692: - case 2702 ... 2702: - case 2706 ... 2706: - case 2729 ... 2729: - case 2737 ... 2737: - case 2740 ... 2740: - case 2746 ... 2747: - case 2758 ... 2758: - case 2762 ... 2762: - case 2766 ... 2767: - case 2769 ... 2783: - case 2788 ... 2789: - case 2802 ... 2816: - case 2820 ... 2820: - case 2829 ... 2830: - case 2833 ... 2834: - case 2857 ... 2857: - case 2865 ... 2865: - case 2868 ... 2868: - case 2874 ... 2875: - case 2885 ... 2886: - case 2889 ... 2890: - case 2894 ... 2901: - case 2904 ... 2907: - case 2910 ... 2910: - case 2916 ... 2917: - case 2936 ... 2945: - case 2948 ... 2948: - case 2955 ... 2957: - case 2961 ... 2961: - case 2966 ... 2968: - case 2971 ... 2971: - case 2973 ... 2973: - case 2976 ... 2978: - case 2981 ... 2983: - case 2987 ... 2989: - case 3002 ... 3005: - case 3011 ... 3013: - case 3017 ... 3017: - case 3022 ... 3023: - case 3025 ... 3030: - case 3032 ... 3045: - case 3067 ... 3071: - case 3076 ... 3076: - case 3085 ... 3085: - case 3089 ... 3089: - case 3113 ... 3113: - case 3130 ... 3132: - case 3141 ... 3141: - case 3145 ... 3145: - case 3150 ... 3156: - case 3159 ... 3159: - case 3162 ... 3167: - case 3172 ... 3173: - case 3184 ... 3191: - case 3200 ... 3200: - case 3204 ... 3204: - case 3213 ... 3213: - case 3217 ... 3217: - case 3241 ... 3241: - case 3252 ... 3252: - case 3258 ... 3259: - case 3269 ... 3269: - case 3273 ... 3273: - case 3278 ... 3284: - case 3287 ... 3293: - case 3295 ... 3295: - case 3300 ... 3301: - case 3312 ... 3312: - case 3315 ... 3328: - case 3332 ... 3332: - case 3341 ... 3341: - case 3345 ... 3345: - case 3387 ... 3388: - case 3397 ... 3397: - case 3401 ... 3401: - case 3407 ... 3414: - case 3416 ... 3423: - case 3428 ... 3429: - case 3446 ... 3448: - case 3456 ... 3457: - case 3460 ... 3460: - case 3479 ... 3481: - case 3506 ... 3506: - case 3516 ... 3516: - case 3518 ... 3519: - case 3527 ... 3529: - case 3531 ... 3534: - case 3541 ... 3541: - case 3543 ... 3543: - case 3552 ... 3557: - case 3568 ... 3569: - case 3573 ... 3584: - case 3643 ... 3646: - case 3676 ... 3712: - case 3715 ... 3715: - case 3717 ... 3718: - case 3721 ... 3721: - case 3723 ... 3724: - case 3726 ... 3731: - case 3736 ... 3736: - case 3744 ... 3744: - case 3748 ... 3748: - case 3750 ... 3750: - case 3752 ... 3753: - case 3756 ... 3756: - case 3770 ... 3770: - case 3774 ... 3775: - case 3781 ... 3781: - case 3783 ... 3783: - case 3790 ... 3791: - case 3802 ... 3803: - case 3808 ... 3839: - case 3912 ... 3912: - case 3949 ... 3952: - case 3992 ... 3992: - case 4029 ... 4029: - case 4045 ... 4045: - case 4059 ... 4095: - case 4294 ... 4294: - case 4296 ... 4300: - case 4302 ... 4303: - case 4681 ... 4681: - case 4686 ... 4687: - case 4695 ... 4695: - case 4697 ... 4697: - case 4702 ... 4703: - case 4745 ... 4745: - case 4750 ... 4751: - case 4785 ... 4785: - case 4790 ... 4791: - case 4799 ... 4799: - case 4801 ... 4801: - case 4806 ... 4807: - case 4823 ... 4823: - case 4881 ... 4881: - case 4886 ... 4887: - case 4955 ... 4956: - case 4989 ... 4991: - case 5018 ... 5023: - case 5109 ... 5119: - case 5789 ... 5791: - case 5881 ... 5887: - case 5901 ... 5901: - case 5909 ... 5919: - case 5943 ... 5951: - case 5972 ... 5983: - case 5997 ... 5997: - case 6001 ... 6001: - case 6004 ... 6015: - case 6110 ... 6111: - case 6122 ... 6127: - case 6138 ... 6143: - case 6158 ... 6159: - case 6170 ... 6175: - case 6264 ... 6271: - case 6315 ... 6319: - case 6390 ... 6399: - case 6431 ... 6431: - case 6444 ... 6447: - case 6460 ... 6463: - case 6465 ... 6467: - case 6510 ... 6511: - case 6517 ... 6527: - case 6572 ... 6575: - case 6602 ... 6607: - case 6619 ... 6621: - case 6684 ... 6685: - case 6751 ... 6751: - case 6781 ... 6782: - case 6794 ... 6799: - case 6810 ... 6815: - case 6830 ... 6831: - case 6847 ... 6911: - case 6988 ... 6991: - case 7037 ... 7039: - case 7156 ... 7163: - case 7224 ... 7226: - case 7242 ... 7244: - case 7296 ... 7359: - case 7368 ... 7375: - case 7415 ... 7415: - case 7418 ... 7423: - case 7670 ... 7675: - case 7958 ... 7959: - case 7966 ... 7967: - case 8006 ... 8007: - case 8014 ... 8015: - case 8024 ... 8024: - case 8026 ... 8026: - case 8028 ... 8028: - case 8030 ... 8030: - case 8062 ... 8063: - case 8117 ... 8117: - case 8133 ... 8133: - case 8148 ... 8149: - case 8156 ... 8156: - case 8176 ... 8177: - case 8181 ... 8181: - case 8191 ... 8191: - case 8203 ... 8207: - case 8232 ... 8238: - case 8288 ... 8303: - case 8306 ... 8307: - case 8335 ... 8335: - case 8349 ... 8351: - case 8382 ... 8399: - case 8433 ... 8447: - case 8586 ... 8591: - case 9211 ... 9215: - case 9255 ... 9279: - case 9291 ... 9311: - case 11124 ... 11125: - case 11158 ... 11159: - case 11194 ... 11196: - case 11209 ... 11209: - case 11218 ... 11263: - case 11311 ... 11311: - case 11359 ... 11359: - case 11508 ... 11512: - case 11558 ... 11558: - case 11560 ... 11564: - case 11566 ... 11567: - case 11624 ... 11630: - case 11633 ... 11646: - case 11671 ... 11679: - case 11687 ... 11687: - case 11695 ... 11695: - case 11703 ... 11703: - case 11711 ... 11711: - case 11719 ... 11719: - case 11727 ... 11727: - case 11735 ... 11735: - case 11743 ... 11743: - case 11843 ... 11903: - case 11930 ... 11930: - case 12020 ... 12031: - case 12246 ... 12271: - case 12284 ... 12287: - case 12352 ... 12352: - case 12439 ... 12440: - case 12544 ... 12548: - case 12590 ... 12592: - case 12687 ... 12687: - case 12731 ... 12735: - case 12772 ... 12783: - case 12831 ... 12831: - case 13055 ... 13055: - case 19894 ... 19903: - case 40909 ... 40959: - case 42125 ... 42127: - case 42183 ... 42191: - case 42540 ... 42559: - case 42654 ... 42654: - case 42744 ... 42751: - case 42895 ... 42895: - case 42926 ... 42927: - case 42930 ... 42998: - case 43052 ... 43055: - case 43066 ... 43071: - case 43128 ... 43135: - case 43205 ... 43213: - case 43226 ... 43231: - case 43260 ... 43263: - case 43348 ... 43358: - case 43389 ... 43391: - case 43470 ... 43470: - case 43482 ... 43485: - case 43519 ... 43519: - case 43575 ... 43583: - case 43598 ... 43599: - case 43610 ... 43611: - case 43715 ... 43738: - case 43767 ... 43776: - case 43783 ... 43784: - case 43791 ... 43792: - case 43799 ... 43807: - case 43815 ... 43815: - case 43823 ... 43823: - case 43872 ... 43875: - case 43878 ... 43967: - case 44014 ... 44015: - case 44026 ... 44031: - case 55204 ... 55215: - case 55239 ... 55242: - case 55292 ... 55295: - case 57344 ... 63743: - case 64110 ... 64111: - case 64218 ... 64255: - case 64263 ... 64274: - case 64280 ... 64284: - case 64311 ... 64311: - case 64317 ... 64317: - case 64319 ... 64319: - case 64322 ... 64322: - case 64325 ... 64325: - case 64450 ... 64466: - case 64832 ... 64847: - case 64912 ... 64913: - case 64968 ... 65007: - case 65022 ... 65023: - case 65050 ... 65055: - case 65070 ... 65071: - case 65107 ... 65107: - case 65127 ... 65127: - case 65132 ... 65135: - case 65141 ... 65141: - case 65277 ... 65280: - case 65471 ... 65473: - case 65480 ... 65481: - case 65488 ... 65489: - case 65496 ... 65497: - case 65501 ... 65503: - case 65511 ... 65511: - case 65519 ... 65531: - case 65534 ... 65535: - print_str_char_u(c); - break; - case 65548 ... 65548: - case 65575 ... 65575: - case 65595 ... 65595: - case 65598 ... 65598: - case 65614 ... 65615: - case 65630 ... 65663: - case 65787 ... 65791: - case 65795 ... 65798: - case 65844 ... 65846: - case 65933 ... 65935: - case 65948 ... 65951: - case 65953 ... 65999: - case 66046 ... 66175: - case 66205 ... 66207: - case 66257 ... 66271: - case 66300 ... 66303: - case 66340 ... 66351: - case 66379 ... 66383: - case 66427 ... 66431: - case 66462 ... 66462: - case 66500 ... 66503: - case 66518 ... 66559: - case 66718 ... 66719: - case 66730 ... 66815: - case 66856 ... 66863: - case 66916 ... 66926: - case 66928 ... 67071: - case 67383 ... 67391: - case 67414 ... 67423: - case 67432 ... 67583: - case 67590 ... 67591: - case 67593 ... 67593: - case 67638 ... 67638: - case 67641 ... 67643: - case 67645 ... 67646: - case 67670 ... 67670: - case 67743 ... 67750: - case 67760 ... 67839: - case 67868 ... 67870: - case 67898 ... 67902: - case 67904 ... 67967: - case 68024 ... 68029: - case 68032 ... 68095: - case 68100 ... 68100: - case 68103 ... 68107: - case 68116 ... 68116: - case 68120 ... 68120: - case 68148 ... 68151: - case 68155 ... 68158: - case 68168 ... 68175: - case 68185 ... 68191: - case 68256 ... 68287: - case 68327 ... 68330: - case 68343 ... 68351: - case 68406 ... 68408: - case 68438 ... 68439: - case 68467 ... 68471: - case 68498 ... 68504: - case 68509 ... 68520: - case 68528 ... 68607: - case 68681 ... 69215: - case 69247 ... 69631: - case 69710 ... 69713: - case 69744 ... 69758: - case 69821 ... 69821: - case 69826 ... 69839: - case 69865 ... 69871: - case 69882 ... 69887: - case 69941 ... 69941: - case 69956 ... 69967: - case 70007 ... 70015: - case 70089 ... 70092: - case 70094 ... 70095: - case 70107 ... 70112: - case 70133 ... 70143: - case 70162 ... 70162: - case 70206 ... 70319: - case 70379 ... 70383: - case 70394 ... 70400: - case 70404 ... 70404: - case 70413 ... 70414: - case 70417 ... 70418: - case 70441 ... 70441: - case 70449 ... 70449: - case 70452 ... 70452: - case 70458 ... 70459: - case 70469 ... 70470: - case 70473 ... 70474: - case 70478 ... 70486: - case 70488 ... 70492: - case 70500 ... 70501: - case 70509 ... 70511: - case 70517 ... 70783: - case 70856 ... 70863: - case 70874 ... 71039: - case 71094 ... 71095: - case 71114 ... 71167: - case 71237 ... 71247: - case 71258 ... 71295: - case 71352 ... 71359: - case 71370 ... 71839: - case 71923 ... 71934: - case 71936 ... 72383: - case 72441 ... 73727: - case 74649 ... 74751: - case 74863 ... 74863: - case 74869 ... 77823: - case 78895 ... 92159: - case 92729 ... 92735: - case 92767 ... 92767: - case 92778 ... 92781: - case 92784 ... 92879: - case 92910 ... 92911: - case 92918 ... 92927: - case 92998 ... 93007: - case 93018 ... 93018: - case 93026 ... 93026: - case 93048 ... 93052: - case 93072 ... 93951: - case 94021 ... 94031: - case 94079 ... 94094: - case 94112 ... 110591: - case 110594 ... 113663: - case 113771 ... 113775: - case 113789 ... 113791: - case 113801 ... 113807: - case 113818 ... 113819: - case 113824 ... 118783: - case 119030 ... 119039: - case 119079 ... 119080: - case 119155 ... 119162: - case 119262 ... 119295: - case 119366 ... 119551: - case 119639 ... 119647: - case 119666 ... 119807: - case 119893 ... 119893: - case 119965 ... 119965: - case 119968 ... 119969: - case 119971 ... 119972: - case 119975 ... 119976: - case 119981 ... 119981: - case 119994 ... 119994: - case 119996 ... 119996: - case 120004 ... 120004: - case 120070 ... 120070: - case 120075 ... 120076: - case 120085 ... 120085: - case 120093 ... 120093: - case 120122 ... 120122: - case 120127 ... 120127: - case 120133 ... 120133: - case 120135 ... 120137: - case 120145 ... 120145: - case 120486 ... 120487: - case 120780 ... 120781: - case 120832 ... 124927: - case 125125 ... 125126: - case 125143 ... 126463: - case 126468 ... 126468: - case 126496 ... 126496: - case 126499 ... 126499: - case 126501 ... 126502: - case 126504 ... 126504: - case 126515 ... 126515: - case 126520 ... 126520: - case 126522 ... 126522: - case 126524 ... 126529: - case 126531 ... 126534: - case 126536 ... 126536: - case 126538 ... 126538: - case 126540 ... 126540: - case 126544 ... 126544: - case 126547 ... 126547: - case 126549 ... 126550: - case 126552 ... 126552: - case 126554 ... 126554: - case 126556 ... 126556: - case 126558 ... 126558: - case 126560 ... 126560: - case 126563 ... 126563: - case 126565 ... 126566: - case 126571 ... 126571: - case 126579 ... 126579: - case 126584 ... 126584: - case 126589 ... 126589: - case 126591 ... 126591: - case 126602 ... 126602: - case 126620 ... 126624: - case 126628 ... 126628: - case 126634 ... 126634: - case 126652 ... 126703: - case 126706 ... 126975: - case 127020 ... 127023: - case 127124 ... 127135: - case 127151 ... 127152: - case 127168 ... 127168: - case 127184 ... 127184: - case 127222 ... 127231: - case 127245 ... 127247: - case 127279 ... 127279: - case 127340 ... 127343: - case 127387 ... 127461: - case 127491 ... 127503: - case 127547 ... 127551: - case 127561 ... 127567: - case 127570 ... 127743: - case 127789 ... 127791: - case 127870 ... 127871: - case 127951 ... 127955: - case 127992 ... 127999: - case 128255 ... 128255: - case 128331 ... 128335: - case 128378 ... 128378: - case 128420 ... 128420: - case 128579 ... 128580: - case 128720 ... 128735: - case 128749 ... 128751: - case 128756 ... 128767: - case 128884 ... 128895: - case 128981 ... 129023: - case 129036 ... 129039: - case 129096 ... 129103: - case 129114 ... 129119: - case 129160 ... 129167: - case 129198 ... 131071: - case 173783 ... 173823: - case 177973 ... 177983: - case 178206 ... 194559: - case 195102 ... 917759: - case 918000 ... 1114110: - print_str_char_U(c); - break; - default: - print_codepoint(c); - break; - } -} - -void print_char(val_char_t c) -{ - printf("#\\"); - switch (c) { - case 0: - printf("nul"); break; - case 8: - printf("backspace"); break; - case 9: - printf("tab"); break; - case 10: - printf("newline"); break; - case 11: - printf("vtab"); break; - case 12: - printf("page"); break; - case 13: - printf("return"); break; - case 32: - printf("space"); break; - case 127: - printf("rubout"); break; - default: - print_codepoint(c); - } -} - -void print_codepoint(val_char_t c) -{ - static char buffer[5] = {0}; - utf8_encode_char(c, buffer); - printf("%s", buffer); -} - -int utf8_encode_char(val_char_t c, char *buffer) -{ - // Output to buffer using UTF-8 encoding of codepoint - // https://en.wikipedia.org/wiki/UTF-8 - if (c < 128) { - buffer[0] = (char) c; - return 1; - } else if (c < 2048) { - buffer[0] = (char)(c >> 6) | 192; - buffer[1] = ((char) c & 63) | 128; - return 2; - } else if (c < 65536) { - buffer[0] = (char)(c >> 12) | 224; - buffer[1] = ((char)(c >> 6) & 63) | 128; - buffer[2] = ((char) c & 63) | 128; - return 3; - } else { - buffer[0] = (char)(c >> 18) | 240; - buffer[1] = ((char)(c >> 12) & 63) | 128; - buffer[2] = ((char)(c >> 6) & 63) | 128; - buffer[3] = ((char) c & 63) | 128; - return 4; - } -} diff --git a/langs/modules-in-progress/print.h b/langs/modules-in-progress/print.h deleted file mode 100644 index c22081a2..00000000 --- a/langs/modules-in-progress/print.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef PRINT_H -#define PRINT_H - -#include "values.h" - -void print_result(val_t); - -#endif diff --git a/langs/modules-in-progress/runtime.h b/langs/modules-in-progress/runtime.h deleted file mode 100644 index f594f0f6..00000000 --- a/langs/modules-in-progress/runtime.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef RUNTIME_H -#define RUNTIME_H -int64_t entry(); -extern FILE* in; -extern FILE* out; -extern void (*error_handler)(); - -// in words -#define heap_size 10000 -extern int64_t *heap; -#endif /* RUNTIME_H */ diff --git a/langs/modules-in-progress/stdlib-provides.rkt b/langs/modules-in-progress/stdlib-provides.rkt deleted file mode 100644 index 45671c5c..00000000 --- a/langs/modules-in-progress/stdlib-provides.rkt +++ /dev/null @@ -1,9 +0,0 @@ -#lang racket -(provide stdlib-provides) -(require "parse.rkt" "ast.rkt" racket/runtime-path) - -(define-runtime-path stdlib "stdlib.rkt") - -(define stdlib-provides - (Module-ps (parse-module-file stdlib))) - diff --git a/langs/modules-in-progress/stdlib.rkt b/langs/modules-in-progress/stdlib.rkt deleted file mode 100644 index 56a278e3..00000000 --- a/langs/modules-in-progress/stdlib.rkt +++ /dev/null @@ -1,11 +0,0 @@ -#lang racket -(provide reverse) - -(define (rev/acc xs a) - (if (empty? xs) - a - (rev/acc (cdr xs) (cons (car xs) a)))) - -(define (reverse xs) - (rev/acc xs '())) - diff --git a/langs/modules-in-progress/test/build-runtime.rkt b/langs/modules-in-progress/test/build-runtime.rkt deleted file mode 100644 index 7023ee0b..00000000 --- a/langs/modules-in-progress/test/build-runtime.rkt +++ /dev/null @@ -1,8 +0,0 @@ -#lang racket -(require a86/interp) - -;; link with runtime for IO operations -(unless (file-exists? "../runtime.o") - (system "make -C .. runtime.o")) -(current-objs - (list (path->string (normalize-path "../runtime.o")))) diff --git a/langs/modules-in-progress/test/test-runner.rkt b/langs/modules-in-progress/test/test-runner.rkt deleted file mode 100644 index 228a817e..00000000 --- a/langs/modules-in-progress/test/test-runner.rkt +++ /dev/null @@ -1,255 +0,0 @@ -#lang racket -(provide test-runner test-runner-io) -(require rackunit) - -(define (test-runner run) - ;; Abscond examples - (check-equal? (run 7) 7) - (check-equal? (run -8) -8) - - ;; Blackmail examples - (check-equal? (run '(add1 (add1 7))) 9) - (check-equal? (run '(add1 (sub1 7))) 7) - - ;; Con examples - (check-equal? (run '(if (zero? 0) 1 2)) 1) - (check-equal? (run '(if (zero? 1) 1 2)) 2) - (check-equal? (run '(if (zero? -7) 1 2)) 2) - (check-equal? (run '(if (zero? 0) - (if (zero? 1) 1 2) - 7)) - 2) - (check-equal? (run '(if (zero? (if (zero? 0) 1 0)) - (if (zero? 1) 1 2) - 7)) - 7) - - ;; Dupe examples - (check-equal? (run #t) #t) - (check-equal? (run #f) #f) - (check-equal? (run '(if #t 1 2)) 1) - (check-equal? (run '(if #f 1 2)) 2) - (check-equal? (run '(if 0 1 2)) 1) - (check-equal? (run '(if #t 3 4)) 3) - (check-equal? (run '(if #f 3 4)) 4) - (check-equal? (run '(if 0 3 4)) 3) - (check-equal? (run '(zero? 4)) #f) - (check-equal? (run '(zero? 0)) #t) - - ;; Dodger examples - (check-equal? (run #\a) #\a) - (check-equal? (run #\b) #\b) - (check-equal? (run '(char? #\a)) #t) - (check-equal? (run '(char? #t)) #f) - (check-equal? (run '(char? 8)) #f) - (check-equal? (run '(char->integer #\a)) (char->integer #\a)) - (check-equal? (run '(integer->char 955)) #\λ) - - ;; Extort examples - (check-equal? (run '(add1 #f)) 'err) - (check-equal? (run '(sub1 #f)) 'err) - (check-equal? (run '(zero? #f)) 'err) - (check-equal? (run '(char->integer #f)) 'err) - (check-equal? (run '(integer->char #f)) 'err) - (check-equal? (run '(integer->char -1)) 'err) - (check-equal? (run '(write-byte #f)) 'err) - (check-equal? (run '(write-byte -1)) 'err) - (check-equal? (run '(write-byte 256)) 'err) - - ;; Fraud examples - (check-equal? (run '(let ((x 7)) x)) 7) - (check-equal? (run '(let ((x 7)) 2)) 2) - (check-equal? (run '(let ((x 7)) (add1 x))) 8) - (check-equal? (run '(let ((x (add1 7))) x)) 8) - (check-equal? (run '(let ((x 7)) (let ((y 2)) x))) 7) - (check-equal? (run '(let ((x 7)) (let ((x 2)) x))) 2) - (check-equal? (run '(let ((x 7)) (let ((x (add1 x))) x))) 8) - - (check-equal? (run '(let ((x 0)) - (if (zero? x) 7 8))) - 7) - (check-equal? (run '(let ((x 1)) - (add1 (if (zero? x) 7 8)))) - 9) - (check-equal? (run '(+ 3 4)) 7) - (check-equal? (run '(- 3 4)) -1) - (check-equal? (run '(+ (+ 2 1) 4)) 7) - (check-equal? (run '(+ (+ 2 1) (+ 2 2))) 7) - (check-equal? (run '(let ((x (+ 1 2))) - (let ((z (- 4 x))) - (+ (+ x x) z)))) - 7) - (check-equal? (run '(= 5 5)) #t) - (check-equal? (run '(= 4 5)) #f) - (check-equal? (run '(= (add1 4) 5)) #t) - (check-equal? (run '(< 5 5)) #f) - (check-equal? (run '(< 4 5)) #t) - (check-equal? (run '(< (add1 4) 5)) #f) - - ;; Hustle examples - (check-equal? (run ''()) '()) - (check-equal? (run '(box 1)) (box 1)) - (check-equal? (run '(cons 1 2)) (cons 1 2)) - (check-equal? (run '(unbox (box 1))) 1) - (check-equal? (run '(car (cons 1 2))) 1) - (check-equal? (run '(cdr (cons 1 2))) 2) - (check-equal? (run '(cons 1 '())) (list 1)) - (check-equal? (run '(let ((x (cons 1 2))) - (begin (cdr x) - (car x)))) - 1) - (check-equal? (run '(let ((x (cons 1 2))) - (let ((y (box 3))) - (unbox y)))) - 3) - - ;; Hoax examples - (check-equal? (run '(make-vector 0 0)) #()) - (check-equal? (run '(make-vector 1 0)) #(0)) - (check-equal? (run '(make-vector 3 0)) #(0 0 0)) - (check-equal? (run '(make-vector 3 5)) #(5 5 5)) - (check-equal? (run '(vector? (make-vector 0 0))) #t) - (check-equal? (run '(vector? (cons 0 0))) #f) - (check-equal? (run '(vector-ref (make-vector 3 5) -1)) 'err) - (check-equal? (run '(vector-ref (make-vector 3 5) 0)) 5) - (check-equal? (run '(vector-ref (make-vector 3 5) 1)) 5) - (check-equal? (run '(vector-ref (make-vector 3 5) 2)) 5) - (check-equal? (run '(vector-ref (make-vector 3 5) 3)) 'err) - (check-equal? (run '(let ((x (make-vector 3 5))) - (begin (vector-set! x 0 4) - x))) - #(4 5 5)) - (check-equal? (run '(let ((x (make-vector 3 5))) - (begin (vector-set! x 1 4) - x))) - #(5 4 5)) - (check-equal? (run '(vector-length (make-vector 3 #f))) 3) - (check-equal? (run '(vector-length (make-vector 0 #f))) 0) - (check-equal? (run '"") "") - (check-equal? (run '"fred") "fred") - (check-equal? (run '"wilma") "wilma") - (check-equal? (run '(make-string 0 #\f)) "") - (check-equal? (run '(make-string 3 #\f)) "fff") - (check-equal? (run '(make-string 3 #\g)) "ggg") - (check-equal? (run '(string-length "")) 0) - (check-equal? (run '(string-length "fred")) 4) - (check-equal? (run '(string-ref "fred" 0)) #\f) - (check-equal? (run '(string-ref "fred" 1)) #\r) - (check-equal? (run '(string-ref "fred" 2)) #\e) - (check-equal? (run '(string-ref "fred" 4)) 'err) - (check-equal? (run '(string? "fred")) #t) - (check-equal? (run '(string? (cons 1 2))) #f) - - ;; Iniquity tests - (check-equal? (run - '(define (f x) x) - '(f 5)) - 5) - (check-equal? (run - '(define (tri x) - (if (zero? x) - 0 - (+ x (tri (sub1 x))))) - '(tri 9)) - 45) - - (check-equal? (run - '(define (even? x) - (if (zero? x) - #t - (odd? (sub1 x)))) - '(define (odd? x) - (if (zero? x) - #f - (even? (sub1 x)))) - '(even? 101)) - #f) - - (check-equal? (run - '(define (map-add1 xs) - (if (empty? xs) - '() - (cons (add1 (car xs)) - (map-add1 (cdr xs))))) - '(map-add1 (cons 1 (cons 2 (cons 3 '()))))) - '(2 3 4)) - (check-equal? (run - '(define (f x) - 10) - '(f 1)) - 10) - (check-equal? (run - ' (define (f x) - 10) - '(let ((x 2)) (f 1))) - 10) - (check-equal? (run - '(define (f x y) - 10) - '(f 1 2)) - 10) - (check-equal? (run - '(define (f x y) - 10) - '(let ((z 2)) (f 1 2))) - 10)) - - -(define (test-runner-io run) - ;; Evildoer examples - (check-equal? (run "" 7) (cons 7 "")) - (check-equal? (run "" '(write-byte 97)) (cons (void) "a")) - (check-equal? (run "a" '(read-byte)) (cons 97 "")) - (check-equal? (run "b" '(begin (write-byte 97) (read-byte))) - (cons 98 "a")) - (check-equal? (run "" '(read-byte)) (cons eof "")) - (check-equal? (run "" '(eof-object? (read-byte))) (cons #t "")) - (check-equal? (run "a" '(eof-object? (read-byte))) (cons #f "")) - (check-equal? (run "" '(begin (write-byte 97) (write-byte 98))) - (cons (void) "ab")) - - (check-equal? (run "ab" '(peek-byte)) (cons 97 "")) - (check-equal? (run "ab" '(begin (peek-byte) (read-byte))) (cons 97 "")) - ;; Extort examples - (check-equal? (run "" '(write-byte #t)) (cons 'err "")) - - ;; Fraud examples - (check-equal? (run "" '(let ((x 97)) (write-byte x))) (cons (void) "a")) - (check-equal? (run "" - '(let ((x 97)) - (begin (write-byte x) - x))) - (cons 97 "a")) - (check-equal? (run "b" '(let ((x 97)) (begin (read-byte) x))) - (cons 97 "")) - (check-equal? (run "b" '(let ((x 97)) (begin (peek-byte) x))) - (cons 97 "")) - - ;; Hustle examples - (check-equal? (run "" - '(let ((x 1)) - (begin (write-byte 97) - 1))) - (cons 1 "a")) - - (check-equal? (run "" - '(let ((x 1)) - (let ((y 2)) - (begin (write-byte 97) - 1)))) - (cons 1 "a")) - - (check-equal? (run "" - '(let ((x (cons 1 2))) - (begin (write-byte 97) - (car x)))) - (cons 1 "a")) - ;; Iniquity examples - (check-equal? (run "" - '(define (print-alphabet i) - (if (zero? i) - (void) - (begin (write-byte (- 123 i)) - (print-alphabet (sub1 i))))) - '(print-alphabet 26)) - (cons (void) "abcdefghijklmnopqrstuvwxyz"))) diff --git a/langs/modules-in-progress/types.h b/langs/modules-in-progress/types.h deleted file mode 100644 index b79f45b2..00000000 --- a/langs/modules-in-progress/types.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef TYPES_H -#define TYPES_H - -/* - Bit layout of values - - Values are either: - - Immediates: end in #b000 - - Pointers - - Immediates are either - - Integers: end in #b0 000 - - Characters: end in #b01 000 - - True: #b11 000 - - False: #b1 11 000 - - Eof: #b10 11 000 - - Void: #b11 11 000 - - Empty: #b100 11 000 -*/ -#define imm_shift 3 -#define ptr_type_mask ((1 << imm_shift) - 1) -#define box_type_tag 1 -#define cons_type_tag 2 -#define vect_type_tag 3 -#define str_type_tag 4 -#define int_shift (1 + imm_shift) -#define int_type_mask ((1 << int_shift) - 1) -#define int_type_tag (0 << (int_shift - 1)) -#define nonint_type_tag (1 << (int_shift - 1)) -#define char_shift (int_shift + 1) -#define char_type_mask ((1 << char_shift) - 1) -#define char_type_tag ((0 << (char_shift - 1)) | nonint_type_tag) -#define nonchar_type_tag ((1 << (char_shift - 1)) | nonint_type_tag) -#define val_true ((0 << char_shift) | nonchar_type_tag) -#define val_false ((1 << char_shift) | nonchar_type_tag) -#define val_eof ((2 << char_shift) | nonchar_type_tag) -#define val_void ((3 << char_shift) | nonchar_type_tag) -#define val_empty ((4 << char_shift) | nonchar_type_tag) - -#endif diff --git a/langs/modules-in-progress/types.rkt b/langs/modules-in-progress/types.rkt deleted file mode 100644 index 806fd02e..00000000 --- a/langs/modules-in-progress/types.rkt +++ /dev/null @@ -1,66 +0,0 @@ -#lang racket -(provide (all-defined-out)) - -(define imm-shift 3) -(define imm-mask #b111) -(define ptr-mask #b111) -(define type-box #b001) -(define type-cons #b010) -(define type-vect #b011) -(define type-str #b100) -(define int-shift (+ 1 imm-shift)) -(define char-shift (+ 2 imm-shift)) -(define type-int #b0000) -(define mask-int #b1111) -(define type-char #b01000) -(define mask-char #b11111) -(define val-true #b0011000) -(define val-false #b0111000) -(define val-eof #b1011000) -(define val-void #b1111000) -(define val-empty #b10011000) - -(define (bits->value b) - (cond [(= type-int (bitwise-and b mask-int)) - (arithmetic-shift b (- int-shift))] - [(= type-char (bitwise-and b mask-char)) - (integer->char (arithmetic-shift b (- char-shift)))] - [(= b val-true) #t] - [(= b val-false) #f] - [(= b val-eof) eof] - [(= b val-void) (void)] - [(= b val-empty) '()] - [else (error "invalid bits")])) - -(define (imm->bits v) - (cond [(eof-object? v) val-eof] - [(integer? v) (arithmetic-shift v int-shift)] - [(char? v) - (bitwise-ior type-char - (arithmetic-shift (char->integer v) char-shift))] - [(eq? v #t) val-true] - [(eq? v #f) val-false] - [(void? v) val-void] - [(empty? v) val-empty])) - - -(define (imm-bits? v) - (zero? (bitwise-and v imm-mask))) - -(define (int-bits? v) - (zero? (bitwise-and v mask-int))) - -(define (char-bits? v) - (= type-char (bitwise-and v mask-char))) - -(define (cons-bits? v) - (zero? (bitwise-xor (bitwise-and v imm-mask) type-cons))) - -(define (box-bits? v) - (zero? (bitwise-xor (bitwise-and v imm-mask) type-box))) - -(define (vect-bits? v) - (zero? (bitwise-xor (bitwise-and v imm-mask) type-vect))) - -(define (str-bits? v) - (zero? (bitwise-xor (bitwise-and v imm-mask) type-str))) diff --git a/langs/modules-in-progress/unload-bits-asm.rkt b/langs/modules-in-progress/unload-bits-asm.rkt deleted file mode 100644 index be9b50c8..00000000 --- a/langs/modules-in-progress/unload-bits-asm.rkt +++ /dev/null @@ -1,43 +0,0 @@ -#lang racket -(provide unload/free unload-value) -(require "types.rkt" - ffi/unsafe) - -;; Answer* -> Answer -(define (unload/free a) - (match a - ['err 'err] - [(cons h v) (begin0 (unload-value v) - (free h))])) - -;; Value* -> Value -(define (unload-value v) - (match v - [(? imm-bits?) (bits->value v)] - [(? box-bits? i) - (box (unload-value (heap-ref i)))] - [(? cons-bits? i) - (cons (unload-value (heap-ref (+ i 8))) - (unload-value (heap-ref i)))] - [(? vect-bits? i) - (if (zero? (untag i)) - (vector) - (build-vector (heap-ref i) - (lambda (j) - (unload-value (heap-ref (+ i (* 8 (add1 j))))))))] - [(? str-bits? i) - (if (zero? (untag i)) - (string) - (build-string (heap-ref i) - (lambda (j) - (char-ref (+ i 8) j))))])) - -(define (untag i) - (arithmetic-shift (arithmetic-shift i (- (integer-length ptr-mask))) - (integer-length ptr-mask))) - -(define (heap-ref i) - (ptr-ref (cast (untag i) _int64 _pointer) _uint64)) - -(define (char-ref i j) - (integer->char (ptr-ref (cast (untag i) _int64 _pointer) _uint32 j))) diff --git a/langs/modules-in-progress/values.c b/langs/modules-in-progress/values.c deleted file mode 100644 index a61d65e6..00000000 --- a/langs/modules-in-progress/values.c +++ /dev/null @@ -1,108 +0,0 @@ -#include "types.h" -#include "values.h" - -type_t val_typeof(val_t x) -{ - switch (x & ptr_type_mask) { - case box_type_tag: - return T_BOX; - case cons_type_tag: - return T_CONS; - case vect_type_tag: - return T_VECT; - case str_type_tag: - return T_STR; - } - - if ((int_type_mask & x) == int_type_tag) - return T_INT; - if ((char_type_mask & x) == char_type_tag) - return T_CHAR; - - switch (x) { - case val_true: - case val_false: - return T_BOOL; - case val_eof: - return T_EOF; - case val_void: - return T_VOID; - case val_empty: - return T_EMPTY; - } - - return T_INVALID; -} - -int64_t val_unwrap_int(val_t x) -{ - return x >> int_shift; -} -val_t val_wrap_int(int64_t i) -{ - return (i << int_shift) | int_type_tag; -} - -int val_unwrap_bool(val_t x) -{ - return x == val_true; -} -val_t val_wrap_bool(int b) -{ - return b ? val_true : val_false; -} - -val_char_t val_unwrap_char(val_t x) -{ - return (val_char_t)(x >> char_shift); -} -val_t val_wrap_char(val_char_t c) -{ - return (((val_t)c) << char_shift) | char_type_tag; -} - -val_t val_wrap_eof(void) -{ - return val_eof; -} - -val_t val_wrap_void(void) -{ - return val_void; -} - -val_box_t* val_unwrap_box(val_t x) -{ - return (val_box_t *)(x ^ box_type_tag); -} -val_t val_wrap_box(val_box_t* b) -{ - return ((val_t)b) | box_type_tag; -} - -val_cons_t* val_unwrap_cons(val_t x) -{ - return (val_cons_t *)(x ^ cons_type_tag); -} -val_t val_wrap_cons(val_cons_t *c) -{ - return ((val_t)c) | cons_type_tag; -} - -val_vect_t* val_unwrap_vect(val_t x) -{ - return (val_vect_t *)(x ^ vect_type_tag); -} -val_t val_wrap_vect(val_vect_t *v) -{ - return ((val_t)v) | vect_type_tag; -} - -val_str_t* val_unwrap_str(val_t x) -{ - return (val_str_t *)(x ^ str_type_tag); -} -val_t val_wrap_str(val_str_t *v) -{ - return ((val_t)v) | str_type_tag; -} diff --git a/langs/modules-in-progress/values.h b/langs/modules-in-progress/values.h deleted file mode 100644 index 4cc48bbe..00000000 --- a/langs/modules-in-progress/values.h +++ /dev/null @@ -1,75 +0,0 @@ -#ifndef VALUES_H -#define VALUES_H - -#include - -/* any abstract value */ -typedef int64_t val_t; - -typedef enum type_t { - T_INVALID = -1, - /* immediates */ - T_INT, - T_BOOL, - T_CHAR, - T_EOF, - T_VOID, - T_EMPTY, - /* pointers */ - T_BOX, - T_CONS, - T_VECT, - T_STR, -} type_t; - -typedef uint32_t val_char_t; -typedef struct val_box_t { - val_t val; -} val_box_t; -typedef struct val_cons_t { - val_t snd; - val_t fst; -} val_cons_t; -typedef struct val_vect_t { - uint64_t len; - val_t elems[]; -} val_vect_t; -typedef struct val_str_t { - uint64_t len; - val_char_t codepoints[]; -} val_str_t; - -/* return the type of x */ -type_t val_typeof(val_t x); - -/** - * Wrap/unwrap values - * - * The behavior of unwrap functions are undefined on type mismatch. - */ -int64_t val_unwrap_int(val_t x); -val_t val_wrap_int(int64_t i); - -int val_unwrap_bool(val_t x); -val_t val_wrap_bool(int b); - -val_char_t val_unwrap_char(val_t x); -val_t val_wrap_char(val_char_t b); - -val_t val_wrap_eof(); - -val_t val_wrap_void(); - -val_box_t* val_unwrap_box(val_t x); -val_t val_wrap_box(val_box_t* b); - -val_cons_t* val_unwrap_cons(val_t x); -val_t val_wrap_cons(val_cons_t* c); - -val_vect_t* val_unwrap_vect(val_t x); -val_t val_wrap_vect(val_vect_t* c); - -val_str_t* val_unwrap_str(val_t x); -val_t val_wrap_str(val_str_t* c); - -#endif