implement llir as racket #lang
This commit is contained in:
parent
83a2064123
commit
506ebdb3e2
|
@ -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~
|
||||||
|
*.rkt.bak
|
||||||
|
\#*.rkt#
|
||||||
|
\#*.rkt#*#
|
||||||
|
|
||||||
|
# Compiled racket bytecode
|
||||||
|
compiled/
|
||||||
|
*.zo
|
||||||
|
|
||||||
|
# Dependency tracking files
|
||||||
|
*.dep
|
||||||
|
|
||||||
|
# End of https://www.toptal.com/developers/gitignore/api/racket
|
||||||
|
|
|
@ -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
|
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
|
will be a more usable language that compiles to the LLIR. and polyfills in case targets don't
|
||||||
implement operations, etc
|
implement operations, etc
|
||||||
|
|
||||||
|
## installation/usage
|
||||||
|
```sh
|
||||||
|
cd /path/to/shark-compiler
|
||||||
|
raco pkg install
|
||||||
|
```
|
||||||
|
|
||||||
|
then write a file with `#lang shark/llir`
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
#!/usr/bin/env racket
|
|
||||||
#lang racket
|
#lang racket
|
||||||
|
|
||||||
;; LLIR
|
;; LLIR
|
||||||
|
@ -394,57 +393,10 @@
|
||||||
(hash-ref render (cons y x) #\space))))
|
(hash-ref render (cons y x) #\space))))
|
||||||
"\n"))
|
"\n"))
|
||||||
|
|
||||||
|
;;;;;;;; #lang
|
||||||
|
(define-syntax-rule (llir-module-begin EXPR ...)
|
||||||
|
(#%module-begin
|
||||||
|
(displayln (compile `(EXPR ...)))))
|
||||||
|
|
||||||
;;;;;;;; demo
|
(provide (except-out (all-from-out racket) #%module-begin) ; probably overkill, w/e
|
||||||
|
(rename-out [llir-module-begin #%module-begin]))
|
||||||
(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))
|
|
||||||
|
|
|
@ -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)
|
|
@ -0,0 +1,2 @@
|
||||||
|
#lang s-exp syntax/module-reader
|
||||||
|
"compiler.rkt"
|
Loading…
Reference in New Issue