1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-16 18:29:56 +00:00

Merge remote-tracking branch 'origin/compile-opt' into compile-opt

This commit is contained in:
Calvin Rose 2023-10-10 20:28:09 -05:00
commit 6a78b6d1c6
14 changed files with 1676 additions and 1 deletions

View File

@ -149,6 +149,7 @@ JANET_CORE_SOURCES=src/core/abstract.c \
src/core/strtod.c \
src/core/struct.c \
src/core/symcache.c \
src/core/sysir.c \
src/core/table.c \
src/core/tuple.c \
src/core/util.c \

View File

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

View File

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

View File

@ -0,0 +1,36 @@
(def ir-asm
@{:instructions
'(
# Types
(type-prim Int s32)
(type-prim Double f64)
(type-struct MyPair 0 1)
(type-pointer PInt 0)
(type-array DoubleArray 1 1024)
# Declarations
(bind 0 Int)
(bind 1 Int)
(bind 2 Int)
(bind 3 Double)
(bind bob Double)
(bind 5 Double)
(bind 6 MyPair)
# Code
(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))
:parameter-count 0
:link-name "test_function"})
(def as (sysir/asm ir-asm))
(print (sysir/to-c as))

View File

@ -0,0 +1,62 @@
### typedef struct {float x; float y; float z;} Vec3;
###
### Vec3 addv(Vec3 a, Vec3 b) {
### Vec3 ret;
### ret.x = a.x + b.x;
### ret.y = a.y + b.y;
### ret.z = a.z + b.z;
### return ret;
### }
# Use fgetp for code gen
(def ir-asm
@{:instructions
'(
# Types
(type-prim Real f32)
(type-struct Vec3 Real Real Real)
(type-pointer PReal Real)
# Declarations
(bind position Vec3)
(bind velocity Vec3)
(bind next-position Vec3)
(bind dest Real)
(bind lhs Real)
(bind rhs Real)
(bind pdest PReal)
(bind plhs PReal)
(bind prhs PReal)
# Code
(fgetp pdest next-position 0)
(fgetp plhs position 0)
(fgetp prhs velocity 0)
(load lhs plhs)
(load rhs prhs)
(add dest lhs rhs)
(store pdest dest)
(fgetp pdest next-position 1)
(fgetp plhs position 1)
(fgetp prhs velocity 1)
(load lhs plhs)
(load rhs prhs)
(add dest lhs rhs)
(store pdest dest)
(fgetp pdest next-position 2)
(fgetp plhs position 2)
(fgetp prhs velocity 2)
(load lhs plhs)
(load rhs prhs)
(add dest lhs rhs)
(store pdest dest)
(return next-position))
:parameter-count 2
:link-name "addv"})
(def as (sysir/asm ir-asm))
(print (sysir/to-c as))

View File

@ -0,0 +1,56 @@
### typedef struct {float x; float y; float z;} Vec3;
###
### Vec3 addv(Vec3 a, Vec3 b) {
### Vec3 ret;
### ret.x = a.x + b.x;
### ret.y = a.y + b.y;
### ret.z = a.z + b.z;
### return ret;
### }
(def ir-asm
@{:instructions
'(
# Types
(type-prim Real f32)
(type-struct Vec3 Real Real Real)
(type-pointer PReal Real)
# Declarations
(bind position Vec3)
(bind velocity Vec3)
(bind next-position Vec3)
(bind dest Real)
(bind lhs Real)
(bind rhs Real)
(bind pdest PReal)
(bind plhs PReal)
(bind prhs PReal)
# Code (has type errors)
(fgetp pdest next-position 0)
(fgetp plhs position 0)
(fgetp prhs velocity 0)
(add dest plhs prhs)
(store pdest dest)
(fgetp pdest next-position 1)
(fgetp plhs position 1)
(fgetp prhs velocity 1)
(add dest lhs rhs)
(load lhs plhs)
(load rhs prhs)
(store pdest dest)
(fgetp pdest next-position 2)
(fgetp plhs position 2)
(fgetp prhs velocity 2)
(add dest plhs prhs)
(store pdest dest)
(return next-position))
:parameter-count 2
:link-name "addv_with_err"})
(def as (sysir/asm ir-asm))
(print (sysir/to-c as))

View File

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

View File

@ -138,6 +138,7 @@ core_src = [
'src/core/strtod.c',
'src/core/struct.c',
'src/core/symcache.c',
'src/core/sysir.c',
'src/core/table.c',
'src/core/tuple.c',
'src/core/util.c',

View File

@ -4212,6 +4212,7 @@
"src/core/strtod.c"
"src/core/struct.c"
"src/core/symcache.c"
"src/core/sysir.c"
"src/core/table.c"
"src/core/tuple.c"
"src/core/util.c"

View File

@ -166,7 +166,7 @@ void janet_os_mutex_lock(JanetOSMutex *mutex) {
void janet_os_mutex_unlock(JanetOSMutex *mutex) {
int ret = pthread_mutex_unlock((pthread_mutex_t *) mutex);
if (ret) janet_panic("cannot release lock");
if (ret) janet_panicf("cannot release lock: %s", strerror(ret));
}
void janet_os_rwlock_init(JanetOSRWLock *rwlock) {

View File

@ -1127,4 +1127,5 @@ void janet_lib_compile(JanetTable *env) {
JANET_REG_END
};
janet_core_cfuns_ext(env, NULL, cfuns);
janet_lib_sysir(env);
}

View File

@ -268,6 +268,9 @@ JanetSlot janetc_cslot(Janet x);
/* Search for a symbol */
JanetSlot janetc_resolve(JanetCompiler *c, const uint8_t *sym);
/* Load the system dialect IR */
void janet_lib_sysir(JanetTable *env);
/* Bytecode optimization */
void janet_bytecode_movopt(JanetFuncDef *def);
void janet_bytecode_remove_noops(JanetFuncDef *def);

View File

@ -493,6 +493,7 @@ void janet_sweep() {
/* If not visited... */
if (!janet_truthy(items[i].value)) {
void *abst = janet_unwrap_abstract(items[i].key);
if (0 == janet_abstract_decref(abst)) {
/* Run finalizer */
JanetAbstractHead *head = janet_abstract_head(abst);

1460
src/core/sysir.c Normal file

File diff suppressed because it is too large Load Diff