From 506ebdb3e249807f692374f7d243725e422d8fb6 Mon Sep 17 00:00:00 2001 From: hazel levine Date: Tue, 21 Jul 2020 09:57:48 -0400 Subject: [PATCH] implement llir as racket #lang --- .gitignore | 23 +++++++++++++++-- README.md | 8 ++++++ compiler.rkt | 60 +++++--------------------------------------- demo.rkt | 50 ++++++++++++++++++++++++++++++++++++ info.rkt | 2 ++ llir/lang/reader.rkt | 2 ++ 6 files changed, 89 insertions(+), 56 deletions(-) mode change 100755 => 100644 compiler.rkt create mode 100755 demo.rkt create mode 100644 info.rkt create mode 100644 llir/lang/reader.rkt diff --git a/.gitignore b/.gitignore index 0af6c41..80f7f89 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/README.md b/README.md index dee6e4b..bda8f65 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/compiler.rkt b/compiler.rkt old mode 100755 new mode 100644 index 473a31f..247cd1a --- a/compiler.rkt +++ b/compiler.rkt @@ -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])) diff --git a/demo.rkt b/demo.rkt new file mode 100755 index 0000000..0d0cd69 --- /dev/null +++ b/demo.rkt @@ -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) diff --git a/info.rkt b/info.rkt new file mode 100644 index 0000000..8fa429e --- /dev/null +++ b/info.rkt @@ -0,0 +1,2 @@ +#lang info +(define collection "shark") diff --git a/llir/lang/reader.rkt b/llir/lang/reader.rkt new file mode 100644 index 0000000..95303a3 --- /dev/null +++ b/llir/lang/reader.rkt @@ -0,0 +1,2 @@ +#lang s-exp syntax/module-reader +"compiler.rkt" -- 2.20.1