1
0
mirror of https://github.com/janet-lang/janet synced 2025-10-29 14:47:42 +00:00

More work compile-opt.

This commit is contained in:
Calvin Rose
2024-06-12 18:28:05 -05:00
parent c31d8b52ff
commit 232a8faa35
5 changed files with 147 additions and 79 deletions

View File

@@ -126,14 +126,13 @@
result)))
# Named bindings
# TODO - type inference
'def
(do
(assert (= 2 (length args)))
(def [full-name value] args)
(assert (symbol? full-name))
(def [name tp] (type-extract full-name 'int))
(def result (visit1 value into))
(def result (visit1 value into false tp))
(def slot (get-slot name))
(when tp
(array/push into ~(bind ,slot ,tp)))
@@ -147,7 +146,7 @@
(def [full-name value] args)
(assert (symbol? full-name))
(def [name tp] (type-extract full-name 'int))
(def result (visit1 value into))
(def result (visit1 value into false tp))
(def slot (get-slot name))
(when tp
(array/push into ~(bind ,slot ,tp)))
@@ -179,7 +178,7 @@
'do
(do
(each form (slice args 0 -2) (visit1 form into true))
(visit1 (last args) into))
(visit1 (last args) into false type-hint))
# While loop
'while
@@ -204,15 +203,16 @@
(def lab-end (keyword (gensym)))
(assert (< 2 (length args) 4))
(def [cnd tru fal] args)
(def condition-slot (visit1 cnd into))
(def condition-slot (visit1 cnd into false 'boolean))
(def ret (get-slot))
(array/push into ~(bind ,ret ,type-hint))
(array/push into ~(branch ,condition-slot ,lab))
# false path
(array/push into ~(move ,ret ,(visit1 tru into)))
(array/push into ~(move ,ret ,(visit1 tru into false type-hint)))
(array/push into ~(jump ,lab-end))
(array/push into lab)
# true path
(array/push into ~(move ,ret ,(visit1 fal into)))
(array/push into ~(move ,ret ,(visit1 fal into false type-hint)))
(array/push into lab-end)
ret)
@@ -312,7 +312,7 @@
(array/push ir-asm ~(bind ,slot ,tp)))
(each part body
(visit1 part ir-asm true))
#(eprintf "%.99M" ir-asm)
# (eprintf "%.99M" ir-asm)
(sysir/asm ctx ir-asm))
(errorf "unknown form %v" form)))

View File

@@ -6,16 +6,16 @@
(return (* x 2 x))))
(def myprog
'(defn myprog []
'(defn myprog:int []
(def xyz:int (+ 1 2 3))
(def abc:int (* 4 5 6))
(def x:boolean (= 5 7))
(def x:boolean (= (the int 5) xyz))
(var i:int 0)
(while (< i 10)
(set i (+ 1 i))
(while (< i (the int 10))
(set i (the int (+ 1 i)))
(printf "i = %d\n" i))
(printf "hello, world!\n%d\n" (the int (if x abc xyz)))
(return (/ abc xyz))))
(return (* abc xyz))))
(def doloop
'(defn doloop [x:int y:int]
@@ -23,6 +23,7 @@
(while (< i y)
(set i (the int (+ 1 i)))
(printf "i = %d\n" (the int i)))
(myprog)
(return x)))
(def main-fn
@@ -34,7 +35,7 @@
####
#(compile1 myprog)
(compile1 myprog)
(compile1 doloop)
(compile1 main-fn)
#(dump)