Allow for multiple functions in a sysir "context".

Allows for in memory linking.
This commit is contained in:
Calvin Rose 2023-10-22 15:42:03 -05:00
parent f08874e65e
commit 3a782d27b1
7 changed files with 620 additions and 480 deletions

View File

@ -1,18 +1,28 @@
(def ir-asm (def types-asm
@{:instructions '((type-prim Double f64)
'( (type-array BigVec Double 100)))
# Types
(type-prim Double f64)
(type-array BigVec Double 100)
# Declarations (def add-asm
(bind 0 BigVec) '((link-name "add_vector")
(bind 1 BigVec) (parameter-count 2)
(bind 2 BigVec) # Declarations
(add 2 0 1) (bind a BigVec)
(return 2)) (bind b BigVec)
:parameter-count 2 (bind c BigVec)
:link-name "add_vector"}) (add c a b)
(return c)))
(def as (sysir/asm ir-asm)) (def sub-asm
(print (sysir/to-c as)) '((link-name "sub_vector")
(parameter-count 2)
(bind a BigVec)
(bind b BigVec)
(bind c BigVec)
(subtract c a b)
(return c)))
(def ctx (sysir/context))
(sysir/asm ctx types-asm)
(sysir/asm ctx add-asm)
(sysir/asm ctx sub-asm)
(print (sysir/to-c ctx))

View File

@ -1,20 +1,19 @@
(def ir-asm (def ir-asm
@{:instructions '((link-name "add_vectorp")
'( (parameter-count 2)
# Types
(type-prim Double f64)
(type-array BigVec Double 100)
(type-pointer BigVecP BigVec)
# Declarations # Types
(bind 0 BigVecP) (type-prim Double f64)
(bind 1 BigVecP) (type-array BigVec Double 100)
(bind 2 BigVecP) (type-pointer BigVecP BigVec)
(add 2 0 1)
(return 2))
:parameter-count 2
:link-name "add_vectorp"})
(def as (sysir/asm ir-asm)) # Declarations
(print (sysir/to-c as)) (bind 0 BigVecP)
(bind 1 BigVecP)
(bind 2 BigVecP)
(add 2 0 1)
(return 2)))
(def ctx (sysir/context))
(sysir/asm ctx ir-asm)
(print (sysir/to-c ctx))

View File

@ -1,36 +1,35 @@
(def ir-asm (def ir-asm
@{:instructions '((link-name "test_function")
'(
# Types
(type-prim Int s32)
(type-prim Double f64)
(type-struct MyPair 0 1)
(type-pointer PInt 0)
(type-array DoubleArray 1 1024)
# Declarations # Types
(bind 0 Int) (type-prim Int s32)
(bind 1 Int) (type-prim Double f64)
(bind 2 Int) (type-struct MyPair 0 1)
(bind 3 Double) (type-pointer PInt 0)
(bind bob Double) (type-array DoubleArray 1 1024)
(bind 5 Double)
(bind 6 MyPair)
# Code # Declarations
(constant 0 10) (bind 0 Int)
(constant 0 21) (bind 1 Int)
:location (bind 2 Int)
(add 2 1 0) (bind 3 Double)
(constant 3 1.77) (bind bob Double)
(call 3 sin 3) (bind 5 Double)
(cast bob 2) (bind 6 MyPair)
(call bob test_function)
(add 5 bob 3)
(jump :location)
(return 5))
:parameter-count 0
:link-name "test_function"})
(def as (sysir/asm ir-asm)) # Code
(print (sysir/to-c as)) (constant 0 10)
(constant 0 21)
:location
(add 2 1 0)
(constant 3 1.77)
(call 3 sin 3)
(cast bob 2)
(call bob test_function)
(add 5 bob 3)
(jump :location)
(return 5)))
(def ctx (sysir/context))
(sysir/asm ctx ir-asm)
(print (sysir/to-c ctx))

View File

@ -11,52 +11,52 @@
# Use fgetp for code gen # Use fgetp for code gen
(def ir-asm (def ir-asm
@{:instructions '((link-name "addv")
'( (parameter-count 2)
# Types
(type-prim Real f32)
(type-struct Vec3 Real Real Real)
(type-pointer PReal Real)
# Declarations # Types
(bind position Vec3) (type-prim Real f32)
(bind velocity Vec3) (type-struct Vec3 Real Real Real)
(bind next-position Vec3) (type-pointer PReal Real)
(bind dest Real)
(bind lhs Real)
(bind rhs Real)
(bind pdest PReal)
(bind plhs PReal)
(bind prhs PReal)
# Code # Declarations
(fgetp pdest next-position 0) (bind position Vec3)
(fgetp plhs position 0) (bind velocity Vec3)
(fgetp prhs velocity 0) (bind next-position Vec3)
(load lhs plhs) (bind dest Real)
(load rhs prhs) (bind lhs Real)
(add dest lhs rhs) (bind rhs Real)
(store pdest dest) (bind pdest PReal)
(bind plhs PReal)
(bind prhs PReal)
(fgetp pdest next-position 1) # Code
(fgetp plhs position 1) (fgetp pdest next-position 0)
(fgetp prhs velocity 1) (fgetp plhs position 0)
(load lhs plhs) (fgetp prhs velocity 0)
(load rhs prhs) (load lhs plhs)
(add dest lhs rhs) (load rhs prhs)
(store pdest dest) (add dest lhs rhs)
(store pdest dest)
(fgetp pdest next-position 2) (fgetp pdest next-position 1)
(fgetp plhs position 2) (fgetp plhs position 1)
(fgetp prhs velocity 2) (fgetp prhs velocity 1)
(load lhs plhs) (load lhs plhs)
(load rhs prhs) (load rhs prhs)
(add dest lhs rhs) (add dest lhs rhs)
(store pdest dest) (store pdest dest)
(return next-position)) (fgetp pdest next-position 2)
:parameter-count 2 (fgetp plhs position 2)
:link-name "addv"}) (fgetp prhs velocity 2)
(load lhs plhs)
(load rhs prhs)
(add dest lhs rhs)
(store pdest dest)
(def as (sysir/asm ir-asm)) (return next-position)))
(print (sysir/to-c as))
(def ctx (sysir/context))
(sysir/asm ctx ir-asm)
(print (sysir/to-c ctx))

View File

@ -9,48 +9,47 @@
### } ### }
(def ir-asm (def ir-asm
@{:instructions '((link-name "addv_with_err")
'( (parameter-count 2)
# Types # Types
(type-prim Real f32) (type-prim Real f32)
(type-struct Vec3 Real Real Real) (type-struct Vec3 Real Real Real)
(type-pointer PReal Real) (type-pointer PReal Real)
# Declarations # Declarations
(bind position Vec3) (bind position Vec3)
(bind velocity Vec3) (bind velocity Vec3)
(bind next-position Vec3) (bind next-position Vec3)
(bind dest Real) (bind dest Real)
(bind lhs Real) (bind lhs Real)
(bind rhs Real) (bind rhs Real)
(bind pdest PReal) (bind pdest PReal)
(bind plhs PReal) (bind plhs PReal)
(bind prhs PReal) (bind prhs PReal)
# Code (has type errors) # Code (has type errors)
(fgetp pdest next-position 0) (fgetp pdest next-position 0)
(fgetp plhs position 0) (fgetp plhs position 0)
(fgetp prhs velocity 0) (fgetp prhs velocity 0)
(add dest plhs prhs) (add dest plhs prhs)
(store pdest dest) (store pdest dest)
(fgetp pdest next-position 1) (fgetp pdest next-position 1)
(fgetp plhs position 1) (fgetp plhs position 1)
(fgetp prhs velocity 1) (fgetp prhs velocity 1)
(add dest lhs rhs) (add dest lhs rhs)
(load lhs plhs) (load lhs plhs)
(load rhs prhs) (load rhs prhs)
(store pdest dest) (store pdest dest)
(fgetp pdest next-position 2) (fgetp pdest next-position 2)
(fgetp plhs position 2) (fgetp plhs position 2)
(fgetp prhs velocity 2) (fgetp prhs velocity 2)
(add dest plhs prhs) (add dest plhs prhs)
(store pdest dest) (store pdest dest)
(return next-position)) (return next-position)))
:parameter-count 2
:link-name "addv_with_err"})
(def as (sysir/asm ir-asm)) (def ctx (sysir/context))
(print (sysir/to-c as)) (sysir/asm ctx ir-asm)
(print (sysir/to-c ctx))

View File

@ -1,15 +1,10 @@
(def ir-asm (def ir-asm
@{:instructions '((link-name "redefine_type_fail")
'( (type-prim Real f32)
# Types (type-prim 1 s32)
(type-prim Real f32) (bind bob Real)
(type-prim 1 s32) (return bob)))
(bind bob Real) (def ctx (sysir/context))
(sysir/asm ctx ir-asm)
(return bob)) (print (sysir/to-c ctx))
:parameter-count 0
:link-name "redefine_type_fail"})
(def as (sysir/asm ir-asm))
(print (sysir/to-c as))

File diff suppressed because it is too large Load Diff