implement llir as racket #lang

This commit is contained in:
hazel levine 2020-07-21 09:57:48 -04:00
parent 83a2064123
commit 506ebdb3e2
Signed by untrusted user who does not match committer: hazel
GPG Key ID: 1884029A28789A62
6 changed files with 89 additions and 56 deletions

23
.gitignore vendored
View File

@ -1,3 +1,22 @@
*.zo
*.rktd
# Created by https://www.toptal.com/developers/gitignore/api/racket
# Edit at https://www.toptal.com/developers/gitignore?templates=racket
### Racket ###
# gitignore template for the Racket language
# website: http://www.racket-lang.org/
# DrRacket autosave files
*.rkt~
*.rkt.bak
\#*.rkt#
\#*.rkt#*#
# Compiled racket bytecode
compiled/
*.zo
# Dependency tracking files
*.dep
# End of https://www.toptal.com/developers/gitignore/api/racket

View File

@ -5,3 +5,11 @@ i wanted to make a framework for extremely retargetable esolang compilation so i
currently this defines a low-level IR and compilation framework for targeting. in the future, there
will be a more usable language that compiles to the LLIR. and polyfills in case targets don't
implement operations, etc
## installation/usage
```sh
cd /path/to/shark-compiler
raco pkg install
```
then write a file with `#lang shark/llir`

60
compiler.rkt Executable file → Normal file
View File

@ -1,4 +1,3 @@
#!/usr/bin/env racket
#lang racket
;; LLIR
@ -394,57 +393,10 @@
(hash-ref render (cons y x) #\space))))
"\n"))
;;;;;;;; #lang
(define-syntax-rule (llir-module-begin EXPR ...)
(#%module-begin
(displayln (compile `(EXPR ...)))))
;;;;;;;; demo
(define prog
'((global count)
(global one)
(global zero)
(global j)
(global largest-j)
(global largest-value)
(global tmp)
(label main)
(set! zero 0)
(set! one 1)
(set! j 0)
(set! largest-j 0)
(set! largest-value -999999)
;; get size of input stack
(asm! "DDDcsUsUsU" "r" "w" count)
(ifeq count zero exit sort-step)
(label sort-step)
;; get value from input stack
(asm! "DDDrsuUsUsU" "r" "w" tmp)
(iflt tmp largest-value sort-step-cont sort-step-update)
(label sort-step-update)
(set! largest-j j)
(set! largest-value tmp)
(label sort-step-cont)
(add! j one)
(ifeq j count move-val sort-step)
(label move-val)
(set! j 0)
(label shift-loop)
(ifeq j largest-j shift-done shift-cont)
(label shift-cont)
(asm! "DDDuUUU" "r" "w" #f)
(add! j one)
(goto shift-loop)
(label shift-done)
;; move input->output
(asm! "DDDsUUU" "r" "w" #f)
(goto main)
(label exit)
(terminate)))
(displayln (compile prog))
(provide (except-out (all-from-out racket) #%module-begin) ; probably overkill, w/e
(rename-out [llir-module-begin #%module-begin]))

50
demo.rkt Executable file
View File

@ -0,0 +1,50 @@
#!/usr/bin/env racket
#lang shark/llir
(global count)
(global one)
(global zero)
(global j)
(global largest-j)
(global largest-value)
(global tmp)
(label main)
(set! zero 0)
(set! one 1)
(set! j 0)
(set! largest-j 0)
(set! largest-value -999999)
;; get size of input stack
(asm! "DDDcsUsUsU" "r" "w" count)
(ifeq count zero exit sort-step)
(label sort-step)
;; get value from input stack
(asm! "DDDrsuUsUsU" "r" "w" tmp)
(iflt tmp largest-value sort-step-cont sort-step-update)
(label sort-step-update)
(set! largest-j j)
(set! largest-value tmp)
(label sort-step-cont)
(add! j one)
(ifeq j count move-val sort-step)
(label move-val)
(set! j 0)
(label shift-loop)
(ifeq j largest-j shift-done shift-cont)
(label shift-cont)
(asm! "DDDuUUU" "r" "w" #f)
(add! j one)
(goto shift-loop)
(label shift-done)
;; move input->output
(asm! "DDDsUUU" "r" "w" #f)
(goto main)
(label exit)
(terminate)

2
info.rkt Normal file
View File

@ -0,0 +1,2 @@
#lang info
(define collection "shark")

2
llir/lang/reader.rkt Normal file
View File

@ -0,0 +1,2 @@
#lang s-exp syntax/module-reader
"compiler.rkt"