From 9ce3b721f00cda6153f947a89757d9460ec97f64 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Wed, 3 Apr 2019 00:08:04 +0200 Subject: [PATCH] :art: Finally embeddable :smile: --- ast | 2 +- include/array.h | 4 +- include/context.h | 2 +- include/driver.h | 2 +- include/emit.h | 1 + include/env.h | 2 +- include/func.h | 2 +- include/gwion.h | 2 +- include/import.h | 2 +- include/memoize.h | 6 +- include/nspc.h | 6 +- include/object.h | 12 +-- include/operator.h | 6 +- include/parse.h | 4 +- include/plug.h | 4 +- include/soundinfo.h | 3 +- include/template.h | 2 +- include/type.h | 4 +- include/ugen.h | 4 +- include/value.h | 2 +- include/vararg.h | 2 +- include/vm.h | 8 +- src/arg.c | 9 +- src/compile.c | 16 ++-- src/emit/emit.c | 101 ++++++++++++----------- src/emit/emitter.c | 3 +- src/emit/memoize.c | 10 +-- src/gwion.c | 26 +++--- src/lib/array.c | 31 +++---- src/lib/engine.c | 12 +-- src/lib/event.c | 11 +-- src/lib/func.c | 7 +- src/lib/import.c | 151 +++++++++++++++++----------------- src/lib/instr.c | 19 +++-- src/lib/modules.c | 25 +++--- src/lib/object.c | 29 ++++--- src/lib/prim.c | 20 +++-- src/lib/ptr.c | 4 +- src/lib/shred.c | 14 ++-- src/lib/string.c | 2 +- src/lib/ugen.c | 44 +++++----- src/lib/vararg.c | 13 +-- src/oo/context.c | 14 ++-- src/oo/env.c | 23 +++--- src/oo/nspc.c | 28 +++---- src/oo/switch.c | 31 ++++--- src/oo/type.c | 17 ++-- src/oo/value.c | 14 ++-- src/parse/check.c | 31 ++++--- src/parse/func.c | 13 +-- src/parse/operator.c | 16 ++-- src/parse/scan0.c | 24 +++--- src/parse/scan1.c | 8 +- src/parse/scan2.c | 26 +++--- src/parse/template.c | 25 +++--- src/parse/traverse_template.c | 2 + src/plug.c | 47 +++++------ src/soundinfo.c | 8 +- src/vm/driver.c | 14 ++-- src/vm/shreduler.c | 2 + src/vm/vm.c | 22 ++--- src/vm/vm_code.c | 16 ++-- src/vm/vm_shred.c | 28 ++++--- tests/import/callback.c | 2 +- tests/import/end_class.c | 1 + tests/import/extend_array.c | 6 +- tests/import/extend_event.c | 2 +- tests/import/extend_pair.c | 10 +-- tests/import/global_var.c | 4 +- tests/import/static_string.c | 4 +- tests/import/variadic.c | 2 +- util | 2 +- 72 files changed, 562 insertions(+), 509 deletions(-) diff --git a/ast b/ast index 65fbc27f..95d45403 160000 --- a/ast +++ b/ast @@ -1 +1 @@ -Subproject commit 65fbc27ff00cee3cfbef4aec8065b49c93f8cb3d +Subproject commit 95d45403dd3a01edfe7dc5e817a931f5c62c98e6 diff --git a/include/array.h b/include/array.h index a7840220..a452a0e8 100644 --- a/include/array.h +++ b/include/array.h @@ -20,6 +20,6 @@ ANN void m_vector_get(const M_Vector, const m_uint, void*); ANN void m_vector_add(const M_Vector, const void*); ANN m_bit* m_vector_addr(const M_Vector, const m_uint); ANN void m_vector_rem(const M_Vector, const m_uint); -ANEW M_Vector new_m_vector(const m_uint); -ANN void free_m_vector(M_Vector); +ANEW M_Vector new_m_vector(MemPool, const m_uint); +ANN void free_m_vector(MemPool, M_Vector); #endif diff --git a/include/context.h b/include/context.h index 2846abcb..8b70552a 100644 --- a/include/context.h +++ b/include/context.h @@ -9,7 +9,7 @@ struct Context_ { HAS_OBJ }; -ANN2(2) ANEW Context new_context(const Ast, const m_str); +ANN2(2) ANEW Context new_context(MemPool p, const Ast, const m_str); ANN void load_context(const Context, const Env); ANN void unload_context(const Context, const Env); #endif diff --git a/include/driver.h b/include/driver.h index cc610e18..f93eae59 100644 --- a/include/driver.h +++ b/include/driver.h @@ -38,6 +38,6 @@ typedef struct BBQ_ { ANN void dummy_driver(DriverData*); ANN void driver_alloc(Driver*); -ANN Driver* new_driver(void); +ANN Driver* new_driver(MemPool); ANN void free_driver(Driver*, struct VM_*); #endif diff --git a/include/emit.h b/include/emit.h index cfa84658..6b157065 100644 --- a/include/emit.h +++ b/include/emit.h @@ -18,6 +18,7 @@ typedef struct Code_ { struct Emitter_ { Env env; Code* code; + struct Gwion_ *gwion; struct Vector_ stack; }; diff --git a/include/env.h b/include/env.h index 2a05268b..8889fbf8 100644 --- a/include/env.h +++ b/include/env.h @@ -37,7 +37,7 @@ struct Env_ { struct Env_Scope_* scope; }; -ANEW Env new_env(); +ANEW Env new_env(MemPool); ANN void env_reset(const Env); ANN void free_env(Env); ANN2(1,3) m_uint env_push(const Env, const Type, const Nspc); diff --git a/include/func.h b/include/func.h index 382aa47d..eba58935 100644 --- a/include/func.h +++ b/include/func.h @@ -12,7 +12,7 @@ struct Func_ { HAS_OBJ }; -ANEW ANN Func new_func(const m_str, const Func_Def); +ANEW ANN Func new_func(MemPool, const m_str, const Func_Def); ANN2(1,2) Symbol func_symbol(const Env, const m_str, const m_str, const m_str, const m_uint); ANN2(1,3,4) m_bool check_lambda(const Env, const Type, Exp_Lambda*, const Func_Def); #endif diff --git a/include/gwion.h b/include/gwion.h index 11e5d7a2..058a46e8 100644 --- a/include/gwion.h +++ b/include/gwion.h @@ -7,12 +7,12 @@ typedef struct Gwion_* Gwion; struct Arg_; struct Gwion_ { PlugInfo* plug; -// mem Env env; Emitter emit; VM* vm; struct Map_ freearg; SymTable *st; + MemPool p; }; ANN m_bool gwion_ini(const Gwion, struct Arg_*); diff --git a/include/import.h b/include/import.h index b29b4a4c..71912ccb 100644 --- a/include/import.h +++ b/include/import.h @@ -19,7 +19,7 @@ typedef struct Gwi_* Gwi; #else #define GWION_IMPORT(a) ANN m_bool import(const Gwi gwi) #endif -#define ALLOC_PTR(a, b, c) b* a = (b*)_mp_alloc(sizeof(b)); *a = (b)c +#define ALLOC_PTR(p, a, b, c) b* a = (b*)_mp_alloc(p, sizeof(b)); *a = (b)c #define _CHECK_OP(op, check, func)\ CHECK_BB(gwi_oper_add(gwi, opck_##check))\ CHECK_BB(gwi_oper_end(gwi, op_##op, func)) diff --git a/include/memoize.h b/include/memoize.h index a5a8e7e2..946d92fd 100644 --- a/include/memoize.h +++ b/include/memoize.h @@ -5,10 +5,10 @@ #define MEMOIZE_CALL const Instr memoize = !GET_FLAG(f, pure) ? NULL : emit_add_instr(emit, MemoizeCall); #define MEMOIZE_SET(a) if(memoize)memoize->m_val = a + 1; #define MEMOIZE_STORE if(GET_FLAG(emit->env->func, pure)) emit_add_instr(emit, MemoizeStore); -#define MEMOIZE_INI if(GET_FLAG(func, pure)) func->code->memoize = memoize_ini(func, kindof(func->def->base->ret_type->size, !func->def->base->ret_type->size)); +#define MEMOIZE_INI if(GET_FLAG(func, pure)) func->code->memoize = memoize_ini(emit->gwion->p, func, kindof(func->def->base->ret_type->size, !func->def->base->ret_type->size)); typedef struct Memoize_ * Memoize; -Memoize memoize_ini(const Func f, const enum Kind); -void memoize_end(Memoize m); +Memoize memoize_ini(MemPool, const Func, const enum Kind); +void memoize_end(MemPool, Memoize); INSTR(MemoizeCall); INSTR(MemoizeStore); #else diff --git a/include/nspc.h b/include/nspc.h index e7f57524..90bb7e40 100644 --- a/include/nspc.h +++ b/include/nspc.h @@ -20,7 +20,7 @@ struct Nspc_ { HAS_OBJ }; -extern ANEW ANN Nspc new_nspc(const m_str name); +extern ANEW ANN Nspc new_nspc(MemPool p, const m_str name); extern ANN void nspc_commit(const Nspc); //extern ANN void nspc_rollback(const Nspc); @@ -46,8 +46,8 @@ describe_lookup1(A, b) \ static inline ANN void nspc_add_##b(const Nspc n, const Symbol s, const A a) { \ scope_add(&n->info->b, (vtype)s, (vtype)a); \ } \ -ANN static inline void nspc_push_##b(const Nspc n) { scope_push(&n->info->b); }\ -ANN inline static void nspc_pop_##b (const Nspc n) { scope_pop (&n->info->b); }\ +ANN static inline void nspc_push_##b(MemPool p, const Nspc n) { scope_push(p, &n->info->b); }\ +ANN inline static void nspc_pop_##b (MemPool p, const Nspc n) { scope_pop (p, &n->info->b); }\ describe_lookups(A, b) describe_nspc_func(Value, value) diff --git a/include/object.h b/include/object.h index b5ce5093..b21fcd22 100644 --- a/include/object.h +++ b/include/object.h @@ -11,12 +11,12 @@ struct M_Object_ { }; ANN void instantiate_object(const VM_Shred, const Type); -ANN void free_object(const M_Object); -ANEW M_Object new_object(const VM_Shred, const Type); -ANEW M_Object new_M_UGen(void); -ANN ANEW M_Object new_array(const Type t, const m_uint length); -ANEW M_Object new_string(const VM_Shred, const m_str); -ANEW M_Object new_string2(const VM_Shred, const m_str); +ANN void free_object(MemPool p, const M_Object); +ANEW M_Object new_object(MemPool, const VM_Shred, const Type); +ANEW M_Object new_M_UGen(MemPool); +ANN ANEW M_Object new_array(MemPool, const Type t, const m_uint length); +ANEW M_Object new_string(MemPool, const VM_Shred, const m_str); +ANEW M_Object new_string2(MemPool, const VM_Shred, const m_str); ANEW M_Object new_shred(const VM_Shred, const m_bool); ANN void fork_launch(const M_Object, const m_uint); ANN void __release(const M_Object, const VM_Shred); diff --git a/include/operator.h b/include/operator.h index 6e03e3c3..6980827a 100644 --- a/include/operator.h +++ b/include/operator.h @@ -5,11 +5,11 @@ typedef Type (*opck)(const Env, void*); typedef m_bool (*opem)(const Emitter, void*); struct Op_Import { - Operator op; Type lhs, rhs, ret; opck ck; opem em; uintptr_t data; + Operator op; m_bool mut; }; @@ -17,10 +17,10 @@ struct Implicit { void* e; Type t; }; -ANN m_bool add_op(const Nspc , const struct Op_Import*); +ANN m_bool add_op(const Gwion gwion, const Nspc , const struct Op_Import*); ANN Type op_check(const Env, struct Op_Import*); ANN m_bool op_emit(const Emitter, const struct Op_Import*); ANN m_bool operator_set_func(const struct Op_Import*); -ANN void free_op_map(Map map, void *gwion); +ANN void free_op_map(Map map, struct Gwion_* gwion); ANN m_bool env_add_op(const Env, const struct Op_Import*); #endif diff --git a/include/parse.h b/include/parse.h index f1c0fa5a..0f9c07ba 100644 --- a/include/parse.h +++ b/include/parse.h @@ -7,9 +7,9 @@ #define RET_NSPC(exp) \ ++env->scope->depth; \ -nspc_push_value(env->curr); \ +nspc_push_value(env->gwion->p, env->curr); \ const m_bool ret = exp; \ -nspc_pop_value(env->curr); \ +nspc_pop_value(env->gwion->p, env->curr); \ --env->scope->depth; \ return ret; diff --git a/include/plug.h b/include/plug.h index 9b38280e..db19cbfc 100644 --- a/include/plug.h +++ b/include/plug.h @@ -13,7 +13,7 @@ typedef struct PlugInfo_ { struct Map_ drv; } PlugInfo; -ANN PlugInfo* new_plug(Vector); +ANN PlugInfo* new_plug(MemPool, Vector); ANN void plug_run(const Gwion gwion, const Vector); ANN void free_plug(const Gwion gwion); @@ -27,5 +27,5 @@ ANN void free_plug(const Gwion gwion); #define GWMODEND(a) ANN void GWMODEND_NAME(const Gwion gwion, void* self) #define GWDRIVER(a) ANN void GWDRIVER_NAME(DriverData* d) -ANN Vector split_args(const m_str str); +ANN Vector split_args(MemPool, const m_str str); #endif diff --git a/include/soundinfo.h b/include/soundinfo.h index b2537711..b0b3d4d0 100644 --- a/include/soundinfo.h +++ b/include/soundinfo.h @@ -5,5 +5,6 @@ struct SoundInfo_ { uint8_t in, out; m_str arg; }; -struct SoundInfo_ *soundinfo_cpy(const struct SoundInfo_ *src); +struct SoundInfo_ *new_soundinfo(MemPool p); +struct SoundInfo_ *soundinfo_cpy(MemPool p, const struct SoundInfo_ *src); #endif diff --git a/include/template.h b/include/template.h index 384fb3f2..979dd760 100644 --- a/include/template.h +++ b/include/template.h @@ -1,5 +1,5 @@ #ifndef __TEMPLATE #define __TEMPLATE ANN m_bool template_push_types(const Env, ID_List, Type_List); -#define POP_RET(a) { nspc_pop_type(env->curr); return (a); } +#define POP_RET(a) { nspc_pop_type(env->gwion->p, env->curr); return (a); } #endif diff --git a/include/type.h b/include/type.h index c59d8cb4..f057eb1c 100644 --- a/include/type.h +++ b/include/type.h @@ -23,8 +23,8 @@ Type t_void, t_int, t_float, t_dur, t_time, t_now, t_complex, t_polar, t_vec3, t t_null, t_object, t_shred, t_fork, t_event, t_ugen, t_string, t_ptr, t_array, t_gack, t_function, t_fptr, t_vararg, t_lambda, t_class, t_union; -ANN2(2) ANEW Type new_type(const m_uint xid, const m_str name, const Type); -ANEW ANN Type type_copy(const Type type); +ANN2(1,3) ANEW Type new_type(MemPool, const m_uint xid, const m_str name, const Type); +ANEW ANN Type type_copy(MemPool, const Type type); ANN m_str get_type_name(const Env, const m_str, const m_uint); ANN Value find_value(const Type, const Symbol); ANN Func find_func(const Type, const Symbol); diff --git a/include/ugen.h b/include/ugen.h index 8b0af64e..55b0a6a1 100644 --- a/include/ugen.h +++ b/include/ugen.h @@ -41,8 +41,8 @@ struct UGen_ { #define TICK(a) __attribute__((hot)) ANN inline void a(const UGen u) -ANN void ugen_ini(const UGen, const uint, const uint); -ANN void ugen_gen(const UGen, const f_tick, void*, const m_bool); +ANN void ugen_ini(MemPool p, const UGen, const uint, const uint); +ANN void ugen_gen(MemPool p, const UGen, const f_tick, void*, const m_bool); ANN void ugen_connect(const UGen lhs, const UGen rhs); ANN void ugen_disconnect(const UGen lhs, const UGen rhs); #endif diff --git a/include/value.h b/include/value.h index 7410cdee..fdf93f27 100644 --- a/include/value.h +++ b/include/value.h @@ -14,5 +14,5 @@ struct Value_ { HAS_OBJ }; -ANEW ANN Value new_value(const Type type, const m_str name); +ANEW ANN Value new_value(MemPool p, const Type type, const m_str name); #endif diff --git a/include/vararg.h b/include/vararg.h index d816e2d3..ed207e29 100644 --- a/include/vararg.h +++ b/include/vararg.h @@ -5,5 +5,5 @@ struct Vararg_ { m_bit* d; // d(ata) m_uint o, i, s; // o(ffset), i(ndex), s(ize) }; -void free_vararg(struct Vararg_* arg); +void free_vararg(MemPool p, struct Vararg_* arg); #endif diff --git a/include/vm.h b/include/vm.h index 3dad9166..c326c5db 100644 --- a/include/vm.h +++ b/include/vm.h @@ -33,6 +33,7 @@ struct ShredInfo_ { struct M_Object_* me; m_str name; Vector args; + MemPool mp; }; struct ShredTick_ { @@ -41,6 +42,7 @@ struct ShredTick_ { struct ShredTick_ *next; struct ShredTick_ *parent; struct Vector_ child; + Shreduler shreduler; size_t xid; m_float wake_time; }; @@ -55,7 +57,7 @@ struct VM_Shred_ { struct ShredTick_ * tick; struct ShredInfo_ * info; }; -ANN2(4) ANEW VM_Code new_vm_code(const Vector instr, const m_uint stack_depth, const ae_flag, const m_str name); +ANN2(1,5) ANEW VM_Code new_vm_code(MemPool p, const Vector instr, const m_uint stack_depth, const ae_flag, const m_str name); ANN VM_Shred shreduler_get(const Shreduler s) __attribute__((hot)); ANN void shreduler_remove(const Shreduler s, const VM_Shred out, const m_bool erase)__attribute__((hot)); @@ -63,13 +65,13 @@ ANN void shredule(const Shreduler s, const VM_Shred shred, const m_float wake_ti ANN void shreduler_set_loop(const Shreduler s, const m_bool loop); ANN void shreduler_add(const Shreduler s, const VM_Shred shred); -ANEW ANN VM_Shred new_vm_shred(const VM_Code code) __attribute__((hot)); +ANEW ANN VM_Shred new_vm_shred(MemPool, const VM_Code code) __attribute__((hot)); __attribute__((hot)) ANN static inline void vm_shred_exit(const VM_Shred shred) { shreduler_remove(shred->info->vm->shreduler, shred, 1); } void free_vm_shred(const VM_Shred shred)__attribute__((hot, nonnull)); ANN void vm_run(const VM* vm) __attribute__((hot)); -ANEW VM* new_vm(void); +ANEW VM* new_vm(MemPool); ANN void free_vm(VM* vm); ANN m_uint vm_add_shred(const VM* vm, const VM_Shred shred)__attribute__((hot)); ANN void vm_remove(const VM* vm, const m_uint index)__attribute__((hot)); diff --git a/src/arg.c b/src/arg.c index 67afd0f6..52cd17ca 100644 --- a/src/arg.c +++ b/src/arg.c @@ -4,12 +4,15 @@ #include "soundinfo.h" #define GWIONRC ".gwionrc" +/* use before MemPool allocation */ + ANN static inline void config_end(const Vector config) { for(m_uint i = 0; i < vector_size(config); ++i) { const Vector v = (Vector)vector_at(config, i); for(m_uint i = 1; i < vector_size(v); ++i) xfree((m_str)vector_at(v, i)); - free_vector(v); + vector_release(v); + xfree(v); } } @@ -103,7 +106,9 @@ ANN static Vector get_config(const m_str name) { ssize_t nread; FILE *f = fopen(name, "r"); CHECK_OO(f) - const Vector v = new_vector(); +// const Vector v = new_vector(); + const Vector v = (Vector)xmalloc(sizeof(struct Vector_)); + vector_init(v); vector_add(v, (vtype)name); while((nread = getline(&line, &len, f)) != -1) { if(line[0] != '#') diff --git a/src/compile.c b/src/compile.c index f45120df..b9fdeeeb 100644 --- a/src/compile.c +++ b/src/compile.c @@ -28,23 +28,23 @@ struct Compiler { enum compile_type type; }; -static void compiler_name(struct Compiler* c) { +static void compiler_name(MemPool p, struct Compiler* c) { m_str d = strdup(c->base); c->name = strsep(&d, ":"); if(d) - c->args = new_vector(); + c->args = new_vector(p); while(d) vector_add(c->args, (vtype)strdup(strsep(&d, ":"))); free(d); } -static void compiler_clean(const struct Compiler* c) { +static void compiler_clean(MemPool p, const struct Compiler* c) { if(c->name) - free(c->name); + xfree(c->name); if(c->file) fclose(c->file); if(c->ast) - free_ast(c->ast); + free_ast(p, c->ast); } static m_bool compiler_open(struct Compiler* c) { @@ -69,16 +69,16 @@ static m_bool check(struct Gwion_* gwion, struct Compiler* c) { static m_uint compile(struct Gwion_* gwion, struct Compiler* c) { m_uint xid = 0; VM_Code code; - compiler_name(c); + compiler_name(gwion->p, c); if(check(gwion, c) < 0 || !(code = emit_ast(gwion->emit, c->ast))) gw_err("while compiling file '%s'\n", c->base); else { - const VM_Shred shred = new_vm_shred(code); + const VM_Shred shred = new_vm_shred(gwion->p, code); shred->info->args = c->args; xid = vm_add_shred(gwion->vm, shred); } - compiler_clean(c); + compiler_clean(gwion->p, c); return xid; } /* diff --git a/src/emit/emit.c b/src/emit/emit.c index b5faf8ea..11d4adcb 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -5,6 +5,7 @@ #include "oo.h" #include "vm.h" #include "env.h" +#include "gwion.h" #include "type.h" #include "nspc.h" #include "value.h" @@ -31,7 +32,7 @@ typedef struct Local_ { m_bool is_obj; } Local; -static inline void emit_pop_type(const Emitter emit) { nspc_pop_type(emit->env->curr); } +static inline void emit_pop_type(const Emitter emit) { nspc_pop_type(emit->gwion->p, emit->env->curr); } static inline void emit_pop(const Emitter emit, const m_uint scope) { env_pop(emit->env, scope); } static inline m_uint emit_push(const Emitter emit, const Type type, const Nspc nspc) { return env_push(emit->env, type, nspc); @@ -43,31 +44,31 @@ static inline m_uint emit_push_global(const Emitter emit) { return env_push_global(emit->env); } -ANEW static Frame* new_frame() { - Frame* frame = mp_alloc(Frame); +ANEW static Frame* new_frame(MemPool p) { + Frame* frame = mp_alloc(p, Frame); vector_init(&frame->stack); vector_add(&frame->stack, (vtype)NULL); return frame; } -ANN static void free_frame(Frame* a) { +ANN static void free_frame(MemPool p, Frame* a) { LOOP_OPTIM for(vtype i = vector_size(&a->stack) + 1; --i;) if(vector_at(&a->stack, i - 1)) - mp_free(Local, (Local*)vector_at(&a->stack, i - 1)); + mp_free(p, Local, (Local*)vector_at(&a->stack, i - 1)); vector_release(&a->stack); - mp_free(Frame, a); + mp_free(p, Frame, a); } -ANN static Local* new_local(const m_uint size, const m_bool is_obj) { - Local* local = mp_alloc(Local); +ANN static Local* new_local(MemPool p, const m_uint size, const m_bool is_obj) { + Local* local = mp_alloc(p, Local); local->size = size; local->is_obj = is_obj; return local; } -ANN static m_uint frame_local(Frame* frame, const m_uint size, const m_bool is_obj) { - Local* local = new_local(size, is_obj); +ANN static m_uint frame_local(MemPool p, Frame* frame, const m_uint size, const m_bool is_obj) { + Local* local = new_local(p, size, is_obj); local->offset = frame->curr_offset; frame->curr_offset += size; vector_add(&frame->stack, (vtype)local); @@ -92,24 +93,24 @@ ANN static m_bool emit_exp_dot(const Emitter emit, const Exp_Dot* member); ANN static m_bool emit_func_def(const Emitter emit, const Func_Def func_def); ANEW static Code* new_code(const Emitter emit, const m_str name) { - Code* code = mp_alloc(Code); + Code* code = mp_alloc(emit->gwion->p, Code); code->name = code_name_set(name, emit->env->name); vector_init(&code->instr); vector_init(&code->stack_break); vector_init(&code->stack_cont); vector_init(&code->stack_return); - code->frame = new_frame(); + code->frame = new_frame(emit->gwion->p); return code; } -ANN static void free_code(Code* code) { +ANN static void free_code(MemPool p, Code* code) { vector_release(&code->instr); vector_release(&code->stack_break); vector_release(&code->stack_cont); vector_release(&code->stack_return); - free_frame(code->frame); - free(code->name); - mp_free(Code, code); + free_frame(p, code->frame); + xfree(code->name); + mp_free(p, Code, code); } ANN static void emit_pop_scope(const Emitter emit) { GWDEBUG_EXE @@ -142,7 +143,7 @@ ANN static inline m_uint emit_code_offset(const Emitter emit) { } ANN static inline m_uint emit_local(const Emitter emit, const m_uint size, const m_bool is_obj) { GWDEBUG_EXE - return frame_local(emit->code->frame, size, is_obj); + return frame_local(emit->gwion->p, emit->code->frame, size, is_obj); } ANN static void emit_pre_ctor(const Emitter emit, const Type type) { GWDEBUG_EXE @@ -177,7 +178,7 @@ ANN static void emit_pre_constructor_array(const Emitter emit, const Type type) ANN ArrayInfo* emit_array_extend_inner(const Emitter emit, const Type t, const Exp e) { GWDEBUG_EXE CHECK_BO(emit_exp(emit, e, 0)) const Type base = array_base(t); - ArrayInfo* info = mp_alloc(ArrayInfo); + ArrayInfo* info = mp_alloc(emit->gwion->p, ArrayInfo); vector_init(&info->type); for(m_uint i = 1; i < t->array_depth; ++i) vector_add(&info->type, (vtype)array_type(emit->env, base, i)); @@ -252,23 +253,23 @@ static const f_instr allocmember[] = { RegPushImm, RegPushImm2, RegPushImm3, Al static const f_instr allocword[] = { AllocWord, AllocWord2, AllocWord3, AllocWord4 }; ANN static inline Exp this_exp(const Emitter emit, const Type t, const uint pos) { - const Exp exp = new_exp_prim_id(insert_symbol("this"), pos); + const Exp exp = new_exp_prim_id(emit->gwion->p, insert_symbol("this"), pos); exp->type = t; return exp; } ANN static inline Exp dot_this_exp(const Emitter emit, const Exp_Primary* prim, const Type t) { const Exp exp = this_exp(emit, t, prim->self->pos); - const Exp dot = new_exp_dot(exp, prim->d.var); + const Exp dot = new_exp_dot(emit->gwion->p, exp, prim->d.var); dot->d.exp_dot.t_base = t; return dot; } ANN static inline Exp dot_static_exp(const Emitter emit, const Exp_Primary* prim, const Type t) { const Symbol s = insert_symbol(t->name); - const Exp e = new_exp_prim_id(s, prim->self->pos); + const Exp e = new_exp_prim_id(emit->gwion->p, s, prim->self->pos); const Value val = nspc_lookup_value1(t->nspc->parent, s); - const Exp dot = new_exp_dot(e, prim->d.var); + const Exp dot = new_exp_dot(emit->gwion->p, e, prim->d.var); dot->d.exp_dot.t_base = val->type; return dot; } @@ -279,7 +280,7 @@ ANN static m_bool emit_symbol_owned(const Emitter emit, const Exp_Primary* prim) dot->type = v->type; dot->emit_var = prim->self->emit_var; const m_bool ret = emit_exp_dot(emit, &dot->d.exp_dot); - free_exp(dot); + free_exp(emit->gwion->p, dot); return ret; } @@ -326,9 +327,9 @@ ANN static m_bool emit_symbol(const Emitter emit, const Exp_Primary* prim) { GWD ANEW ANN VM_Code emit_code(const Emitter emit) { GWDEBUG_EXE Code* c = emit->code; - const VM_Code code = new_vm_code(&c->instr, c->stack_depth, + const VM_Code code = new_vm_code(emit->gwion->p, &c->instr, c->stack_depth, c->flag, c->name); - free_code(c); + free_code(emit->gwion->p, c); return code; } @@ -460,7 +461,7 @@ ANN static m_bool prim_str(const Emitter emit, const Exp_Primary* prim) { GWDEBU ANN static m_bool prim_gack(const Emitter emit, const Exp_Primary* primary) { const Exp exp = primary->d.exp; - const Vector v = new_vector(); + const Vector v = new_vector(emit->gwion->p); m_uint offset = 0; Exp e = exp; do { @@ -470,7 +471,7 @@ ANN static m_bool prim_gack(const Emitter emit, const Exp_Primary* primary) { ADD_REF(e->type); } while((e = e->next)); if(emit_exp(emit, exp, 0) < 0) { - free_vector(v); + free_vector(emit->gwion->p, v); ERR_B(exp->pos, "\t... in 'gack' expression.") } const Instr instr = emit_add_instr(emit, Gack); @@ -556,7 +557,7 @@ ANN static m_bool emit_exp_decl_global(const Emitter emit, const Var_Decl var_de CHECK_BB(emit_instantiate_object(emit, type, array, is_ref)) f_instr *exec = (f_instr*)dotstatic; const Instr instr = emit_kind(emit, v->type->size, emit_addr, exec); - v->d.ptr = _mp_alloc(v->type->size); + v->d.ptr = mp_alloc2(emit->gwion->p, v->type->size); SET_FLAG(v, union); instr->m_val = (m_uint)v->d.ptr; instr->m_val2 = v->type->size; @@ -631,12 +632,12 @@ ANN static inline m_uint round2szint(const m_uint i) { ANN static void emit_func_arg_vararg(const Emitter emit, const Exp_Call* exp_call) { GWDEBUG_EXE const Instr instr = emit_add_instr(emit, VarargIni); - const Vector kinds = new_vector(); + const Vector kinds = new_vector(emit->gwion->p); if((instr->m_val = round2szint(vararg_size(exp_call, kinds)))) instr->m_val2 = (m_uint)kinds; else { instr->opcode = eRegPushImm; - free_vector(kinds); + free_vector(emit->gwion->p, kinds); } } @@ -726,7 +727,7 @@ ANN m_bool traverse_dot_tmpl(const Emitter emit, const struct dottmpl_ *dt) { m_bool ret = GW_ERROR; if(traverse_func_template(emit->env, dt->def, dt->tl) > 0) { ret = emit_func_def(emit, dt->def); - nspc_pop_type(emit->env->curr); + nspc_pop_type(emit->gwion->p, emit->env->curr); } emit_pop(emit, scope); return ret; @@ -741,7 +742,7 @@ static inline m_bool push_func_code(const Emitter emit, const Func f) { char c[sz + 1]; memcpy(c, f->name, sz); c[sz] = '\0'; - struct dottmpl_ *dt = mp_alloc(dottmpl); + struct dottmpl_ *dt = mp_alloc(emit->gwion->p, dottmpl); dt->name = s_name(insert_symbol(c)); dt->overload = f->def->tmpl->base; dt->tl = tmpl_tl(emit->env, c); @@ -1256,8 +1257,8 @@ ANN static m_bool emit_switch_instr(const Emitter emit, Instr *instr) { return GW_OK; } -ANN static Map emit_switch_map(const Map map) { - const Map m = new_map(); +ANN static Map emit_switch_map(MemPool p, const Map map) { + const Map m = new_map(p); for(m_uint i = map_size(map) + 1; --i;) map_set(m, VKEY(map, i-1), VVAL(map, i -1)); return m; @@ -1274,7 +1275,7 @@ ANN static m_bool emit_stmt_switch(const Emitter emit, const Stmt_Switch stmt) { CHECK_BB(emit_stmt(emit, stmt->stmt, 1)) instr->m_val = switch_idx(emit->env) ?: emit_code_size(emit); if(push[0]) { - push[2]->m_val = push[1]->m_val2 = (m_uint) emit_switch_map((Map)instr->m_val2); + push[2]->m_val = push[1]->m_val2 = (m_uint) emit_switch_map(emit->gwion->p, (Map)instr->m_val2); Vector v = (Vector)(push[1]->m_val = (m_uint)switch_vec(emit->env)); push[0]->m_val = vector_size(v) * SZ_INT; } @@ -1327,7 +1328,7 @@ ANN static m_bool emit_stmt_enum(const Emitter emit, const Stmt_Enum stmt) { GWD for(m_uint i = 0; i < vector_size(&stmt->values); ++i) { const Value v = (Value)vector_at(&stmt->values, i); if(!emit->env->class_def) { - ALLOC_PTR(addr, m_uint, i); + ALLOC_PTR(emit->gwion->p, addr, m_uint, i); v->offset = emit_local(emit, SZ_INT, 0); v->d.ptr = addr; } else @@ -1355,19 +1356,19 @@ ANN static m_bool emit_stmt_union(const Emitter emit, const Stmt_Union stmt) { G (m_bit*)xcalloc(1, stmt->value->type->nspc->info->class_data_size); stmt->value->type->nspc->info->offset = stmt->s; if(!stmt->value->type->p) - stmt->value->type->p = mp_ini((uint32_t)stmt->value->type->size); - Type_Decl *type_decl = new_type_decl(new_id_list(stmt->xid, stmt->self->pos), + stmt->value->type->p = mp_ini(emit->gwion->p, (uint32_t)stmt->value->type->size); + Type_Decl *type_decl = new_type_decl(emit->gwion->p, new_id_list(emit->gwion->p, stmt->xid, stmt->self->pos), emit->env->class_def ? ae_flag_member : 0); - type_decl->flag = stmt->flag; - const Var_Decl var_decl = new_var_decl(stmt->xid, NULL, 0); - const Var_Decl_List var_decl_list = new_var_decl_list(var_decl, NULL); - const Exp exp = new_exp_decl(type_decl, var_decl_list); + type_decl->flag = stmt->flag;// ??? + const Var_Decl var_decl = new_var_decl(emit->gwion->p, stmt->xid, NULL, 0); + const Var_Decl_List var_decl_list = new_var_decl_list(emit->gwion->p, var_decl, NULL); + const Exp exp = new_exp_decl(emit->gwion->p, type_decl, var_decl_list); exp->d.exp_decl.type = stmt->value->type; var_decl->value = stmt->value; CHECK_BB(emit_exp_decl(emit, &exp->d.exp_decl)) - free_exp(exp); + free_exp(emit->gwion->p, exp); if(global) { - const M_Object o = new_object(NULL, stmt->value->type); + const M_Object o = new_object(emit->gwion->p, NULL, stmt->value->type); stmt->value->d.ptr = (m_uint*)o; SET_FLAG(stmt->value, builtin); SET_FLAG(stmt->value, global); @@ -1380,7 +1381,7 @@ ANN static m_bool emit_stmt_union(const Emitter emit, const Stmt_Union stmt) { G (m_bit*)xcalloc(1, stmt->type->nspc->info->class_data_size); stmt->type->nspc->info->offset = stmt->s; if(!stmt->type->p) - stmt->type->p = mp_ini((uint32_t)stmt->type->size); + stmt->type->p = mp_ini(emit->gwion->p, (uint32_t)stmt->type->size); scope = emit_push_type(emit, stmt->type); } else if(emit->env->class_def) { if(!GET_FLAG(l->self->d.exp_decl.list->self->value, member)) @@ -1752,19 +1753,19 @@ ANN static m_bool emit_class_def(const Emitter emit, const Class_Def class_def) return GW_OK; } -ANN static void emit_free_code(Code* code) { +ANN static void emit_free_code(MemPool p, Code* code) { LOOP_OPTIM for(m_uint j = vector_size(&code->instr) + 1; --j;) - mp_free(Instr, (Instr)vector_at(&code->instr, j - 1)); - free_code(code); + mp_free(p, Instr, (Instr)vector_at(&code->instr, j - 1)); + free_code(p, code); } ANN static void emit_free_stack(const Emitter emit) { GWDEBUG_EXE LOOP_OPTIM for(m_uint i = vector_size(&emit->stack) + 1; --i;) - emit_free_code((Code*)vector_at(&emit->stack, i - 1)); + emit_free_code(emit->gwion->p, (Code*)vector_at(&emit->stack, i - 1)); vector_clear(&emit->stack); - emit_free_code(emit->code); + emit_free_code(emit->gwion->p, emit->code); } ANN static inline m_bool emit_ast_inner(const Emitter emit, Ast ast) { GWDEBUG_EXE diff --git a/src/emit/emitter.c b/src/emit/emitter.c index f84b9ab5..010248ac 100644 --- a/src/emit/emitter.c +++ b/src/emit/emitter.c @@ -3,6 +3,7 @@ #include "oo.h" #include "vm.h" #include "env.h" +#include "gwion.h" #include "type.h" #include "nspc.h" #include "value.h" @@ -22,7 +23,7 @@ ANN void free_emitter(Emitter a) { __attribute__((returns_nonnull)) ANN2(1) Instr emit_add_instr(const Emitter emit, const f_instr f) { - const Instr instr = mp_alloc(Instr); + const Instr instr = mp_alloc(emit->gwion->p, Instr); if((m_uint)f < 255) instr->opcode = (m_uint)f; else { diff --git a/src/emit/memoize.c b/src/emit/memoize.c index 2061be56..4438f565 100644 --- a/src/emit/memoize.c +++ b/src/emit/memoize.c @@ -41,8 +41,8 @@ static inline void memoize_return4(m_bit* tgt __attribute__((unused)), static void(*mreturn[])(m_bit*, const m_bit*, const m_uint) = { memoize_return1, memoize_return2, memoize_return3, memoize_return4}; -Memoize memoize_ini(const Func f, const enum Kind kind) { - Memoize m = mp_alloc(Memoize); +Memoize memoize_ini(MemPool p, const Func f, const enum Kind kind) { + Memoize m = mp_alloc(p, Memoize); vector_init(&m->v); m->ret_sz = f->def->base->ret_type->size; m->kind = kind; @@ -56,11 +56,11 @@ Memoize memoize_ini(const Func f, const enum Kind kind) { return m; } -void memoize_end(Memoize m) { +void memoize_end(MemPool p, Memoize m) { vector_release(&m->v); mp_end(m->p); - free(m->p); - mp_free(Memoize, m); + xfree(m->p); + mp_free(p, Memoize, m); } static inline void memoize_set(Memoize m, const m_bit* arg) { diff --git a/src/gwion.c b/src/gwion.c index 15e71fe0..8abcbb94 100644 --- a/src/gwion.c +++ b/src/gwion.c @@ -54,30 +54,31 @@ ANN static inline void gwion_compile(const Gwion gwion, const Vector v) { ANN VM* gwion_cpy(const VM* src) { - const Gwion gwion = mp_alloc(Gwion); - gwion->vm = new_vm(); + const Gwion gwion = mp_alloc(src->gwion->p, Gwion); + gwion->vm = new_vm(src->gwion->p); gwion->vm->gwion = gwion; - gwion->vm->bbq->si = soundinfo_cpy(src->bbq->si); + gwion->vm->bbq->si = soundinfo_cpy(src->gwion->p, src->bbq->si); gwion->emit = src->gwion->emit; gwion->env = src->gwion->env; gwion->freearg = src->gwion->freearg; gwion->st = src->gwion->st; + gwion->p = src->gwion->p; return gwion->vm; } ANN m_bool gwion_ini(const Gwion gwion, Arg* arg) { - gwion->st = new_symbol_table(65347); - gwion->vm = new_vm(); + gwion->p = mempool_ini((sizeof(VM_Shred) + SIZEOF_REG + SIZEOF_MEM) / SZ_INT); + gwion->st = new_symbol_table(gwion->p, 65347); + gwion->vm = new_vm(gwion->p); gwion->emit = new_emitter(); - gwion->env = new_env(); + gwion->env = new_env(gwion->p); gwion->emit->env = gwion->env; + gwion->emit->gwion = gwion; gwion->vm->gwion = gwion; gwion->env->gwion = gwion; - gwion->vm->bbq->si = mp_alloc(SoundInfo); - gwion->vm->bbq->si->in = gwion->vm->bbq->si->out = 2; - gwion->vm->bbq->si->sr = 48000; + gwion->vm->bbq->si = new_soundinfo(gwion->p); arg->si = gwion->vm->bbq->si; arg_parse(arg); - gwion->plug = new_plug(&arg->lib); + gwion->plug = new_plug(gwion->p, &arg->lib); map_init(&gwion->freearg); shreduler_set_loop(gwion->vm->shreduler, arg->loop); if(gwion_audio(gwion) > 0 && gwion_engine(gwion)) { @@ -96,8 +97,8 @@ ANN void gwion_run(const Gwion gwion) { } ANN void gwion_end(const Gwion gwion) { - const VM_Code code = new_vm_code(NULL, 0, ae_flag_builtin, "in code dtor"); - gwion->vm->cleaner_shred = new_vm_shred(code); + const VM_Code code = new_vm_code(gwion->p, NULL, 0, ae_flag_builtin, "in code dtor"); + gwion->vm->cleaner_shred = new_vm_shred(gwion->p, code); gwion->vm->cleaner_shred->info->vm = gwion->vm; free_env(gwion->env); free_vm_shred(gwion->vm->cleaner_shred); @@ -106,4 +107,5 @@ ANN void gwion_end(const Gwion gwion) { free_plug(gwion); map_release(&gwion->freearg); free_symbols(gwion->st); + mempool_end(gwion->p); } diff --git a/src/lib/array.c b/src/lib/array.c index 0c295078..325d532b 100644 --- a/src/lib/array.c +++ b/src/lib/array.c @@ -5,6 +5,7 @@ #include "oo.h" #include "vm.h" #include "env.h" +#include "gwion.h" #include "type.h" #include "instr.h" #include "object.h" @@ -26,8 +27,8 @@ ANN m_uint m_vector_size(const M_Vector v) { return ARRAY_LEN(v); } -M_Vector new_m_vector(const m_uint size) { - const M_Vector array = mp_alloc(M_Vector); +M_Vector new_m_vector(MemPool p, const m_uint size) { + const M_Vector array = mp_alloc(p, M_Vector); const size_t sz = (ARRAY_OFFSET*SZ_INT) + (2*size); array->ptr = (m_bit*)xcalloc(1, sz); ARRAY_CAP(array) = 2; @@ -35,8 +36,8 @@ M_Vector new_m_vector(const m_uint size) { return array; } -M_Vector new_m_vector2(const m_uint size, const m_uint len) { - const M_Vector array = mp_alloc(M_Vector); +M_Vector new_m_vector2(MemPool p, const m_uint size, const m_uint len) { + const M_Vector array = mp_alloc(p, M_Vector); const size_t sz = (ARRAY_OFFSET*SZ_INT) + (len*size); array->ptr = (m_bit*)xcalloc(1, sz); m_uint cap = 1; @@ -48,9 +49,9 @@ M_Vector new_m_vector2(const m_uint size, const m_uint len) { return array; } -void free_m_vector(M_Vector a) { +void free_m_vector(MemPool p, M_Vector a) { xfree(a->ptr); - mp_free(M_Vector, a); + mp_free(p, M_Vector, a); } static DTOR(array_dtor) { @@ -60,15 +61,15 @@ static DTOR(array_dtor) { if(t->array_depth > 1 || isa(base, t_object) > 0) for(m_uint i = 0; i < ARRAY_LEN(a); ++i) release(*(M_Object*)(ARRAY_PTR(a) + i * SZ_INT), shred); - free_m_vector(a); + free_m_vector(shred->info->mp, a); REM_REF(t, shred->info->vm->gwion) } -ANN M_Object new_array(const Type t, const m_uint length) { - const M_Object a = new_object(NULL, t); +ANN M_Object new_array(MemPool p, const Type t, const m_uint length) { + const M_Object a = new_object(p, NULL, t); const m_uint depth = t->array_depth; const m_uint size = depth > 1 ? SZ_INT : array_base(t)->size; - ARRAY(a) = new_m_vector2(size,length); + ARRAY(a) = new_m_vector2(p, size,length); ADD_REF(t); return a; } @@ -191,7 +192,7 @@ static FREEARG(freearg_array) { ArrayInfo* info = (ArrayInfo*)instr->m_val; REM_REF((Type)vector_back(&info->type), gwion); vector_release(&info->type); - mp_free(ArrayInfo, info); + mp_free(((Gwion)gwion)->p, ArrayInfo, info); } GWION_IMPORT(array) { @@ -242,17 +243,17 @@ INSTR(ArrayInit) { GWDEBUG_EXE // for litteral array const m_uint sz = *(m_uint*)REG(0); const m_uint off = instr->m_val2 * sz; POP_REG(shred, off - SZ_INT); - const M_Object obj = new_array(t, sz); + const M_Object obj = new_array(shred->info->mp, t, sz); memcpy(ARRAY(obj)->ptr + ARRAY_OFFSET, REG(-SZ_INT), off); *(M_Object*)REG(-SZ_INT) = obj; } #define TOP -1 -ANN static inline M_Object do_alloc_array_object(const ArrayInfo* info, const m_int cap) { +ANN static inline M_Object do_alloc_array_object(MemPool p, const ArrayInfo* info, const m_int cap) { struct Vector_ v = info->type; const Type t = (Type)vector_at(&v, (vtype)(-info->depth - 1)); - return new_array(t, (m_uint)cap); + return new_array(p, t, (m_uint)cap); } ANN static inline M_Object do_alloc_array_init(ArrayInfo* info, const m_uint cap, @@ -284,7 +285,7 @@ ANN static M_Object do_alloc_array(const VM_Shred shred, ArrayInfo* info) { gw_err("[gwion](VM): NegativeArraySize: while allocating arrays...\n"); return NULL; } - const M_Object base = do_alloc_array_object(info, cap); + const M_Object base = do_alloc_array_object(shred->info->mp, info, cap); return info->depth < TOP ? do_alloc_array_loop(shred, info, (m_uint)cap, base) : info->data ? do_alloc_array_init(info, (m_uint)cap, base) : base; } diff --git a/src/lib/engine.c b/src/lib/engine.c index 500a93b1..7ba41df0 100644 --- a/src/lib/engine.c +++ b/src/lib/engine.c @@ -11,24 +11,26 @@ #include "gwi.h" #include "lang_private.h" #include "emit.h" +#include "env.h" +#include "vm.h" +#include "gwion.h" #include "operator.h" #include "engine.h" -#include "gwion.h" static FREEARG(freearg_switchini) { - free_vector((Vector)instr->m_val); - free_map((Map)instr->m_val2); + free_vector(((Gwion)gwion)->p, (Vector)instr->m_val); + free_map(((Gwion)gwion)->p, (Map)instr->m_val2); } static FREEARG(freearg_switchbranch) { - free_map((Map)instr->m_val2); + free_map(((Gwion)gwion)->p, (Map)instr->m_val2); } static FREEARG(freearg_gack) { const Vector v = (Vector)instr->m_val2; for(m_uint i = vector_size(v) + 1; --i;) REM_REF(((Type)vector_at(v, i - 1)), gwion); - free_vector(v); + free_vector(((Gwion)gwion)->p, v); } ANN static m_bool import_core_libs(const Gwi gwi) { diff --git a/src/lib/event.c b/src/lib/event.c index 0b9d2b36..16e7cc35 100644 --- a/src/lib/event.c +++ b/src/lib/event.c @@ -3,17 +3,18 @@ #include "oo.h" #include "vm.h" #include "env.h" +#include "gwion.h" #include "type.h" #include "instr.h" #include "object.h" #include "import.h" static CTOR(event_ctor) { - EV_SHREDS(o) = new_vector(); + EV_SHREDS(o) = new_vector(shred->info->mp); } static DTOR(event_dtor) { - free_vector(EV_SHREDS(o)); + free_vector(shred->info->mp, EV_SHREDS(o)); } static OP_CHECK(opck_eventwait) { @@ -25,7 +26,7 @@ static INSTR(EventWait) { GWDEBUG_EXE const M_Object event = *(M_Object*)REG(-SZ_INT); if(!event) Except(shred, "NullEventWait"); - shreduler_remove(shred->info->vm->shreduler, shred, 0); + shreduler_remove(shred->tick->shreduler, shred, 0); const Vector v = EV_SHREDS(event); vector_add(v, (vtype)shred); *(m_int*)REG(-SZ_INT) = 1; @@ -36,7 +37,7 @@ static MFUN(event_signal) { const Vector v = EV_SHREDS(o); const VM_Shred sh = (VM_Shred)vector_front(v); if(sh) { - shredule(shred->info->vm->shreduler, sh, .5); + shredule(sh->tick->shreduler, sh, .5); vector_rem(v, 0); } } @@ -44,7 +45,7 @@ static MFUN(event_signal) { ANN void broadcast(const M_Object o) { for(m_uint i = 0; i < vector_size(EV_SHREDS(o)); i++) { const VM_Shred sh = (VM_Shred)vector_at(EV_SHREDS(o), i); - shredule(sh->info->vm->shreduler, sh, .5); + shredule(sh->tick->shreduler, sh, .5); } vector_clear(EV_SHREDS(o)); } diff --git a/src/lib/func.c b/src/lib/func.c index 5d5187e3..777aa164 100644 --- a/src/lib/func.c +++ b/src/lib/func.c @@ -4,6 +4,7 @@ #include "oo.h" #include "vm.h" #include "env.h" +#include "gwion.h" #include "type.h" #include "value.h" #include "func.h" @@ -70,7 +71,7 @@ ANN2(1,3,4) m_bool check_lambda(const Env env, const Type owner, } if(base || arg) ERR_B(l->self->pos, "argument number does not match for lambda") - l->def = new_func_def(new_func_base(def->base->td, l->name, l->args), l->code, def->flag); + l->def = new_func_def(env->gwion->p, new_func_base(env->gwion->p, def->base->td, l->name, l->args), l->code, def->flag); const m_bool ret = traverse_func_def(env, l->def); arg = l->args; while(arg) { @@ -173,8 +174,8 @@ static FREEARG(freearg_xork) { static FREEARG(freearg_dottmpl) { struct dottmpl_ *dt = (struct dottmpl_*)instr->m_val; - free_type_list(dt->tl); - mp_free(dottmpl, dt); + free_type_list(((Gwion)gwion)->p, dt->tl); + mp_free(((Gwion)gwion)->p, dottmpl, dt); } GWION_IMPORT(func) { diff --git a/src/lib/import.c b/src/lib/import.c index 11b53fc0..f8bba1e6 100644 --- a/src/lib/import.c +++ b/src/lib/import.c @@ -16,8 +16,8 @@ #include "emit.h" #include "func.h" #include "nspc.h" -#include "operator.h" #include "gwion.h" +#include "operator.h" struct Path { m_str path, curr; @@ -26,9 +26,9 @@ struct Path { ANN static ID_List templater_def(SymTable *st, const Templater* templater) { ID_List list[templater->n]; - list[0] = new_id_list(insert_symbol(st, templater->list[0]), 0); + list[0] = new_id_list(st->p, insert_symbol(st, templater->list[0]), 0); for(m_uint i = 1; i < templater->n; i++) { - list[i] = new_id_list(insert_symbol(st, templater->list[i]), 0); + list[i] = new_id_list(st->p, insert_symbol(st, templater->list[i]), 0); list[i - 1]->next = list[i]; } return list[0]; @@ -130,7 +130,7 @@ ANN static m_bool path_valid(SymTable *st,ID_List* list, const struct Path* p) { if((i != 1 && last != '.' && last != '\0') || (i == 1 && c != '.')) { path_valid_inner(p->curr); - *list = prepend_id_list(insert_symbol(st, p->curr), *list, 0); + *list = prepend_id_list(st->p, insert_symbol(st, p->curr), *list, 0); memset(p->curr, 0, p->len + 1); } else ERR_B(0, "path '%s' must not ini or end with '.'.", p->path) @@ -155,7 +155,7 @@ ANN static ID_List str2list(SymTable *st, const m_str path, m_uint* array_depth) *array_depth = depth; if(path_valid(st, &list, &p) < 0) { if(list) - free_id_list(list); + free_id_list(st->p, list); return NULL; } CHECK_OO(list) @@ -164,17 +164,17 @@ ANN static ID_List str2list(SymTable *st, const m_str path, m_uint* array_depth) return list; } -ANN static m_bool mk_xtor(const Type type, const m_uint d, const ae_flag e) { +ANN static m_bool mk_xtor(MemPool p, const Type type, const m_uint d, const ae_flag e) { VM_Code* code = e == ae_flag_ctor ? &type->nspc->pre_ctor : &type->nspc->dtor; const m_str name = type->name; - *code = new_vm_code(NULL, SZ_INT, e | ae_flag_member | ae_flag_builtin, name); + *code = new_vm_code(p, NULL, SZ_INT, e | ae_flag_member | ae_flag_builtin, name); (*code)->native_func = (m_uint)d; type->flag |= e; return GW_OK; } ANN2(1,2) Type gwi_mk_type(const Gwi gwi __attribute__((unused)), const m_str name, const m_uint size, const Type parent) { - const Type t = new_type(0, name, parent); + const Type t = new_type(gwi->gwion->p, 0, name, parent); t->size = size; return t; } @@ -188,12 +188,12 @@ ANN m_int gwi_add_type(const Gwi gwi, const Type type) { ANN2(1,2) static void import_class_ini(const Env env, const Type type, const f_xtor pre_ctor, const f_xtor dtor) { - type->nspc = new_nspc(type->name); + type->nspc = new_nspc(env->gwion->p, type->name); type->nspc->parent = env->curr; if(pre_ctor) - mk_xtor(type, (m_uint)pre_ctor, ae_flag_ctor); + mk_xtor(env->gwion->p, type, (m_uint)pre_ctor, ae_flag_ctor); if(dtor) - mk_xtor(type, (m_uint)dtor, ae_flag_dtor); + mk_xtor(env->gwion->p, type, (m_uint)dtor, ae_flag_dtor); if(type->parent) { type->nspc->info->offset = type->parent->nspc->info->offset; if(type->parent->nspc->info->vtable.ptr) @@ -209,8 +209,8 @@ ANN2(1,2) m_int gwi_class_ini(const Gwi gwi, const Type type, const f_xtor pre_c ERR_B(0, "during import: class '%s' already imported.", type->name) if(gwi->templater.n) { const ID_List types = templater_def(gwi->gwion->st,&gwi->templater); - type->def = new_class_def(0, insert_symbol(gwi->gwion->st, type->name), NULL, NULL, 0); - type->def->tmpl = new_tmpl_class(types, -1); + type->def = new_class_def(gwi->gwion->p, 0, insert_symbol(gwi->gwion->st, type->name), NULL, NULL, 0); + type->def->tmpl = new_tmpl_class(gwi->gwion->p, types, -1); type->def->base.type = type; SET_FLAG(type, template); } else @@ -245,7 +245,7 @@ ANN m_int gwi_class_ext(const Gwi gwi, Type_Decl* td) { if(ctor) emit_ext_ctor(gwi->gwion->emit, ctor); emit_class_finish(gwi->gwion->emit, gwi->gwion->env->class_def->nspc); - free_type_decl(td); + free_type_decl(gwi->gwion->p, td); } else { SET_FLAG(td, typedef); gwi->gwion->env->class_def->def->base.ext = td; @@ -268,18 +268,18 @@ ANN m_int gwi_class_end(const Gwi gwi) { if(!gwi->gwion->env->class_def)return GW_ERROR; const Type t = gwi->gwion->env->class_def; if(t->nspc && t->nspc->info->offset) - t->p = mp_ini((uint32_t)t->nspc->info->offset); + t->p = mp_ini(gwi->gwion->p, (uint32_t)t->nspc->info->offset); return import_class_end(gwi->gwion->env); } -ANN static void dl_var_new_exp_array(DL_Var* v) { - v->t.array = new_array_sub(NULL); +ANN static void dl_var_new_exp_array(MemPool p, DL_Var* v) { + v->t.array = new_array_sub(p, NULL); v->t.array->depth = v->array_depth; - v->var.array = new_array_sub(NULL); + v->var.array = new_array_sub(p, NULL); v->var.array->depth = v->array_depth; } -ANN static void dl_var_set(DL_Var* v, const ae_flag flag) { +ANN static void dl_var_set(MemPool p, DL_Var* v, const ae_flag flag) { v->list.self = &v->var; v->t.flag = flag; v->exp.exp_type = ae_exp_decl; @@ -287,15 +287,15 @@ ANN static void dl_var_set(DL_Var* v, const ae_flag flag) { v->exp.d.exp_decl.list = &v->list; v->exp.d.exp_decl.self = &v->exp; if(v->array_depth) - dl_var_new_exp_array(v); + dl_var_new_exp_array(p, v); } -ANN static void dl_var_release(const DL_Var* v) { +ANN static void dl_var_release(MemPool p, const DL_Var* v) { if(v->array_depth) { - free_array_sub(v->t.array); - free_array_sub(v->var.array); + free_array_sub(p, v->t.array); + free_array_sub(p, v->var.array); } - free_id_list(v->t.xid); + free_id_list(p, v->t.xid); } ANN m_int gwi_item_ini(const Gwi gwi, const restrict m_str type, const restrict m_str name) { @@ -320,33 +320,33 @@ static void gwi_body(const Gwi gwi, const Class_Body body) { ANN2(1) m_int gwi_item_end(const Gwi gwi, const ae_flag flag, const m_uint* addr) { DL_Var* v = &gwi->var; - dl_var_set(v, flag | ae_flag_builtin); + dl_var_set(gwi->gwion->p, v, flag | ae_flag_builtin); v->var.addr = (void*)addr; if(gwi->gwion->env->class_def && GET_FLAG(gwi->gwion->env->class_def, template)) { - Type_Decl *type_decl = new_type_decl(v->t.xid, flag); - const Var_Decl var_decl = new_var_decl(v->var.xid, v->var.array, 0); - const Var_Decl_List var_decl_list = new_var_decl_list(var_decl, NULL); - const Exp exp = new_exp_decl(type_decl, var_decl_list); - const Stmt stmt = new_stmt_exp(ae_stmt_exp, exp); - const Stmt_List list = new_stmt_list(stmt, NULL); - Section* section = new_section_stmt_list(list); - const Class_Body body = new_class_body(section, NULL); + Type_Decl *type_decl = new_type_decl(gwi->gwion->p, v->t.xid, flag); + const Var_Decl var_decl = new_var_decl(gwi->gwion->p, v->var.xid, v->var.array, 0); + const Var_Decl_List var_decl_list = new_var_decl_list(gwi->gwion->p, var_decl, NULL); + const Exp exp = new_exp_decl(gwi->gwion->p, type_decl, var_decl_list); + const Stmt stmt = new_stmt_exp(gwi->gwion->p, ae_stmt_exp, exp); + const Stmt_List list = new_stmt_list(gwi->gwion->p, stmt, NULL); + Section* section = new_section_stmt_list(gwi->gwion->p, list); + const Class_Body body = new_class_body(gwi->gwion->p, section, NULL); type_decl->array = v->t.array; gwi_body(gwi, body); return GW_OK; } CHECK_BB(traverse_decl(gwi->gwion->env, &v->exp.d.exp_decl)) SET_FLAG(v->var.value, builtin); - dl_var_release(v); + dl_var_release(gwi->gwion->p, v); return (m_int)v->var.value->offset; } -static Array_Sub make_dll_arg_list_array(Array_Sub array_sub, +static Array_Sub make_dll_arg_list_array(MemPool p, Array_Sub array_sub, m_uint* array_depth, const m_uint array_depth2) { if(array_depth2) *array_depth = array_depth2; if(*array_depth) { - array_sub = new_array_sub(NULL); + array_sub = new_array_sub(p, NULL); for(m_uint i = 1; i < *array_depth; i++) array_sub = prepend_array_sub(array_sub, NULL); } @@ -355,8 +355,8 @@ static Array_Sub make_dll_arg_list_array(Array_Sub array_sub, ANN /*static */ Type_List str2tl(const Env env, const m_str s, m_uint *depth) { Type_Decl* td = str2decl(env, s, depth); - td->array = make_dll_arg_list_array(NULL, depth, 0); - return new_type_list(td, NULL); + td->array = make_dll_arg_list_array(env->gwion->p, NULL, depth, 0); + return new_type_list(env->gwion->p, td, NULL); } ANN Type_Decl* str2decl(const Env env, const m_str s, m_uint *depth) { @@ -365,10 +365,10 @@ ANN Type_Decl* str2decl(const Env env, const m_str s, m_uint *depth) { CHECK_OO(type_name) ID_List id = str2list(env->gwion->st, type_name, depth); CHECK_OO(id) - Type_Decl* td = new_type_decl(id, 0); + Type_Decl* td = new_type_decl(env->gwion->p, id, 0); Type_List tmp = NULL; if(!td) { - free_id_list(id); + free_id_list(env->gwion->p, id); return NULL; } while((type_name = get_type_name(env, s, i++))) { @@ -396,20 +396,20 @@ ANN static Arg_List make_dll_arg_list(const Env env, DL_Func * dl_fun) { ID_List type_path2; if(!(type_decl = str2decl(env, arg->type, &array_depth))) { if(arg_list) - free_arg_list(arg_list); + free_arg_list(env->gwion->p, arg_list); ERR_O(0, "\t...\tat argument '%i'", i + 1) } if((type_path2 = str2list(env->gwion->st, arg->name, &array_depth2))) - free_id_list(type_path2); + free_id_list(env->gwion->p, type_path2); if(array_depth && array_depth2) { - free_type_decl(type_decl); + free_type_decl(env->gwion->p, type_decl); if(arg_list) - free_arg_list(arg_list); + free_arg_list(env->gwion->p, arg_list); ERR_O(0, "array subscript specified incorrectly for built-in module") } - array_sub = make_dll_arg_list_array(array_sub, &array_depth, array_depth2); - var_decl = new_var_decl(insert_symbol(env->gwion->st, arg->name), array_sub, 0); - arg_list = new_arg_list(type_decl, var_decl, arg_list); + array_sub = make_dll_arg_list_array(env->gwion->p, array_sub, &array_depth, array_depth2); + var_decl = new_var_decl(env->gwion->p, insert_symbol(env->gwion->st, arg->name), array_sub, 0); + arg_list = new_arg_list(env->gwion->p, type_decl, var_decl, arg_list); } return arg_list; } @@ -424,17 +424,17 @@ ANN static Func_Def make_dll_as_fun(const Env env, DL_Func * dl_fun, ae_flag fla flag |= ae_flag_builtin; if(!(type_path = str2list(env->gwion->st, dl_fun->type, &array_depth)) || - !(type_decl = new_type_decl(type_path, 0))) + !(type_decl = new_type_decl(env->gwion->p, type_path, 0))) ERR_O(0, "\t...\tduring @ function import '%s' (type).", dl_fun->name) if(array_depth) { - Array_Sub array_sub = new_array_sub(NULL); + Array_Sub array_sub = new_array_sub(env->gwion->p, NULL); for(i = 1; i < array_depth; i++) array_sub = prepend_array_sub(array_sub, NULL); type_decl = add_type_decl_array(type_decl, array_sub); } name = dl_fun->name; arg_list = make_dll_arg_list(env, dl_fun); - func_def = new_func_def(new_func_base(type_decl, insert_symbol(env->gwion->st, name), arg_list), NULL, flag); + func_def = new_func_def(env->gwion->p, new_func_base(env->gwion->p, type_decl, insert_symbol(env->gwion->st, name), arg_list), NULL, flag); func_def->d.dl_func_ptr = (void*)(m_uint)dl_fun->addr; return func_def; } @@ -448,19 +448,19 @@ ANN m_int gwi_func_end(const Gwi gwi, const ae_flag flag) { Func_Def def = import_fun(gwi->gwion->env, &gwi->func, flag); CHECK_OB(def) if(gwi->templater.n) { - def = new_func_def(new_func_base(NULL, NULL, NULL), NULL, 0); + def = new_func_def(gwi->gwion->p, new_func_base(gwi->gwion->p, NULL, NULL, NULL), NULL, 0); const ID_List list = templater_def(gwi->gwion->st, &gwi->templater); - def->tmpl = new_tmpl_list(list, -1); + def->tmpl = new_tmpl_list(gwi->gwion->p, list, -1); SET_FLAG(def, template); } if(gwi->gwion->env->class_def && GET_FLAG(gwi->gwion->env->class_def, template)) { - Section* section = new_section_func_def(def); - Class_Body body = new_class_body(section, NULL); + Section* section = new_section_func_def(gwi->gwion->p, def); + Class_Body body = new_class_body(gwi->gwion->p, section, NULL); gwi_body(gwi, body); return GW_OK; } if(traverse_func_def(gwi->gwion->env, def) < 0) { - free_func_def(def); + free_func_def(gwi->gwion->p, def); return GW_ERROR; } return GW_OK; @@ -471,7 +471,7 @@ static Type get_type(const Env env, const m_str str) { const ID_List list = (str && str != (m_str)OP_ANY_TYPE) ? str2list(env->gwion->st, str, &depth) : NULL; const Type t = (str == (m_str) OP_ANY_TYPE) ? OP_ANY_TYPE : list ? find_type(env, list) : NULL; if(list) - free_id_list(list); + free_id_list(env->gwion->p, list); return t ? (depth ? array_type(env, t, depth) : t) : NULL; } @@ -480,8 +480,8 @@ ANN2(1,2) static int import_op(const Env env, const DL_Oper* op, const Type lhs = op->lhs ? get_type(env, op->lhs) : NULL; const Type rhs = op->rhs ? get_type(env, op->rhs) : NULL; const Type ret = get_type(env, op->ret); - const struct Op_Import opi = { op->op, lhs, rhs, ret, - op->ck, op->em, (uintptr_t)f, op->mut }; + const struct Op_Import opi = { lhs, rhs, ret, + op->ck, op->em, (uintptr_t)f, op->op, op->mut }; return env_add_op(env, &opi); } @@ -529,11 +529,11 @@ ANN static Stmt import_fptr(const Env env, DL_Func* dl_fun, ae_flag flag) { const Arg_List args = make_dll_arg_list(env, dl_fun); flag |= ae_flag_builtin; if(!(type_path = str2list(env->gwion->st, dl_fun->type, &array_depth)) || - !(type_decl = new_type_decl(type_path, 0))) + !(type_decl = new_type_decl(env->gwion->p, type_path, 0))) ERR_O(0, "\t...\tduring fptr import '%s' (type).", dl_fun->name) - struct Func_Base_ *base = new_func_base(type_decl, insert_symbol(env->gwion->st, dl_fun->name), args); - return new_stmt_fptr(base, flag); + struct Func_Base_ *base = new_func_base(env->gwion->p, type_decl, insert_symbol(env->gwion->st, dl_fun->name), args); + return new_stmt_fptr(env->gwion->p, base, flag); } ANN m_int gwi_fptr_end(const Gwi gwi, const ae_flag flag) { @@ -544,7 +544,7 @@ ANN m_int gwi_fptr_end(const Gwi gwi, const ae_flag flag) { else SET_FLAG(stmt->d.stmt_fptr.base->func, builtin); ADD_REF(stmt->d.stmt_fptr.type); - free_stmt(stmt); + free_stmt(gwi->gwion->p, stmt); return GW_OK; } @@ -555,13 +555,13 @@ ANN static Exp make_exp(SymTable *st, const m_str type, const m_str name) { Array_Sub array = NULL; CHECK_OO((id_list = str2list(st, type, &array_depth))) if(array_depth) { - array = new_array_sub(NULL); + array = new_array_sub(st->p, NULL); array->depth = array_depth; } - type_decl = new_type_decl(id_list, 0); - const Var_Decl var_decl = new_var_decl(insert_symbol(st, name), array, 0); - const Var_Decl_List var_decl_list = new_var_decl_list(var_decl, NULL); - return new_exp_decl(type_decl, var_decl_list); + type_decl = new_type_decl(st->p, id_list, 0); + const Var_Decl var_decl = new_var_decl(st->p, insert_symbol(st, name), array, 0); + const Var_Decl_List var_decl_list = new_var_decl_list(st->p, var_decl, NULL); + return new_exp_decl(st->p, type_decl, var_decl_list); } ANN2(1) m_int gwi_union_ini(const Gwi gwi, const m_str name) { @@ -578,21 +578,21 @@ ANN m_int gwi_union_add(const Gwi gwi, const restrict m_str type, const restrict ERR_B(0, "type '%s' unknown in union declaration.", type) if(isa(t, t_object) > 0) SET_FLAG(exp->d.exp_decl.td, ref); - gwi->union_data.list = new_decl_list(exp, gwi->union_data.list); + gwi->union_data.list = new_decl_list(gwi->gwion->p, exp, gwi->union_data.list); return GW_OK; } ANN m_int gwi_union_end(const Gwi gwi, const ae_flag flag) { if(!gwi->union_data.list) ERR_B(0, "union is empty"); - const Stmt stmt = new_stmt_union(gwi->union_data.list, 0); + const Stmt stmt = new_stmt_union(gwi->gwion->p, gwi->union_data.list, 0); stmt->d.stmt_union.flag = flag; CHECK_BB(traverse_stmt_union(gwi->gwion->env, &stmt->d.stmt_union)) emit_union_offset(stmt->d.stmt_union.l, stmt->d.stmt_union.o); if(GET_FLAG((&stmt->d.stmt_union), member)) gwi->gwion->env->class_def->nspc->info->offset = stmt->d.stmt_union.o + stmt->d.stmt_union.s; - free_stmt(stmt); + free_stmt(gwi->gwion->p, stmt); gwi->union_data.list = NULL; gwi->union_data.xid = NULL; return GW_OK; @@ -605,9 +605,9 @@ ANN2(1) m_int gwi_enum_ini(const Gwi gwi, const m_str type) { } ANN m_int gwi_enum_add(const Gwi gwi, const m_str name, const m_uint i) { - const ID_List list = new_id_list(insert_symbol(gwi->gwion->st, name), 0); + const ID_List list = new_id_list(gwi->gwion->p, insert_symbol(gwi->gwion->st, name), 0); DL_Enum* d = &gwi->enum_data; - ALLOC_PTR(addr, m_int, i); + ALLOC_PTR(gwi->gwion->p, addr, m_int, i); vector_add(&gwi->enum_data.addr, (vtype)addr); if(!d->base) d->base = list; @@ -623,6 +623,7 @@ ANN static void import_enum_end(const Gwi gwi, const Vector v) { Value value = (Value)vector_at(v, i); const m_uint addr = vector_at(&d->addr, i); SET_FLAG(value, builtin); + ADD_REF(value->type); if(!gwi->gwion->env->class_def) value->d.ptr = (m_uint*)(addr ? addr : i); else @@ -635,13 +636,13 @@ ANN static void import_enum_end(const Gwi gwi, const Vector v) { ANN m_int gwi_enum_end(const Gwi gwi) { DL_Enum* d = &gwi->enum_data; - const Stmt stmt = new_stmt_enum(d->base, d->t ? insert_symbol(gwi->gwion->st, d->t) : NULL); + const Stmt stmt = new_stmt_enum(gwi->gwion->p, d->base, d->t ? insert_symbol(gwi->gwion->st, d->t) : NULL); if(traverse_stmt_enum(gwi->gwion->env, &stmt->d.stmt_enum) < 0) { - free_id_list(d->base); + free_id_list(gwi->gwion->p, d->base); return GW_ERROR; } import_enum_end(gwi, &stmt->d.stmt_enum.values); - free_stmt(stmt); + free_stmt(gwi->gwion->p, stmt); return GW_OK; } diff --git a/src/lib/instr.c b/src/lib/instr.c index 26c43620..4564ab38 100644 --- a/src/lib/instr.c +++ b/src/lib/instr.c @@ -20,10 +20,10 @@ INSTR(EOC) { GWDEBUG_EXE INSTR(DTOR_EOC) { GWDEBUG_EXE const M_Object o = *(M_Object*)MEM(0); o->type_ref = o->type_ref->parent; - o->ref = 1; _release(o, shred); + if(shred->info->me->ref > 1) // ??? + _release(shred->info->me, shred); vm_shred_exit(shred); - _release(shred->info->me, shred); } /* branching */ @@ -48,18 +48,19 @@ INSTR(PutArgsInMem) { GWDEBUG_EXE } #endif +#include "gwion.h" +#include "emit.h" +#include "value.h" +#include "template.h" + INSTR(PopArrayClass) { GWDEBUG_EXE POP_REG(shred, SZ_INT); const M_Object obj = *(M_Object*)REG(-SZ_INT); const M_Object tmp = *(M_Object*)REG(0); ARRAY(obj) = ARRAY(tmp); - free_object(tmp); + free_object(shred->info->mp, tmp); ADD_REF(obj->type_ref) // add ref to typedef array type } -#include "gwion.h" -#include "emit.h" -#include "value.h" -#include "template.h" ANN static Func_Def from_base(const Env env, const struct dottmpl_ *dt, const Type t) { const Symbol sym = func_symbol(env, t->name, s_name(dt->base->base->xid), @@ -67,9 +68,9 @@ ANN static Func_Def from_base(const Env env, const struct dottmpl_ *dt, const Ty const Value v = nspc_lookup_value1(t->nspc, sym); CHECK_OO(v) const Func_Def base = v->d.func_ref->def; - const Func_Def def = new_func_def(new_func_base(base->base->td, insert_symbol(env->gwion->st, v->name), + const Func_Def def = new_func_def(env->gwion->p, new_func_base(env->gwion->p, base->base->td, insert_symbol(env->gwion->st, v->name), base->base->args), base->d.code, base->flag); - def->tmpl = new_tmpl_list(base->tmpl->list, dt->overload); + def->tmpl = new_tmpl_list(env->gwion->p, base->tmpl->list, dt->overload); SET_FLAG(def, template); return def; } diff --git a/src/lib/modules.c b/src/lib/modules.c index 22b55d3c..2f9a1ddf 100644 --- a/src/lib/modules.c +++ b/src/lib/modules.c @@ -5,6 +5,7 @@ #include "oo.h" #include "env.h" #include "vm.h" +#include "gwion.h" #include "type.h" #include "instr.h" #include "object.h" @@ -21,8 +22,8 @@ static TICK(gain_tick) { } static CTOR(gain_ctor) { - ugen_ini(UGEN(o), 1, 1); - ugen_gen(UGEN(o), gain_tick, (m_float*)xmalloc(SZ_FLOAT), 0); + ugen_ini(shred->info->mp, UGEN(o), 1, 1); + ugen_gen(shred->info->mp, UGEN(o), gain_tick, (m_float*)xmalloc(SZ_FLOAT), 0); UGEN(o)->module.gen.tick = gain_tick; *(m_float*)UGEN(o)->module.gen.data = 1; } @@ -53,8 +54,8 @@ static TICK(impulse_tick) { } static CTOR(impulse_ctor) { - ugen_ini(UGEN(o), 0, 1); - ugen_gen(UGEN(o), impulse_tick, (m_float*)xmalloc(SZ_FLOAT), 0); + ugen_ini(shred->info->mp, UGEN(o), 0, 1); + ugen_gen(shred->info->mp, UGEN(o), impulse_tick, (m_float*)xmalloc(SZ_FLOAT), 0); *(m_float*)UGEN(o)->module.gen.data = 0; } @@ -82,8 +83,8 @@ static TICK(fullrect_tick) { } static CTOR(fullrect_ctor) { - ugen_ini(UGEN(o), 1, 1); - ugen_gen(UGEN(o), fullrect_tick, (m_float*)xmalloc(SZ_FLOAT), 0); + ugen_ini(shred->info->mp, UGEN(o), 1, 1); + ugen_gen(shred->info->mp, UGEN(o), fullrect_tick, (m_float*)xmalloc(SZ_FLOAT), 0); *(m_float*)UGEN(o)->module.gen.data = 1; } @@ -101,8 +102,8 @@ static TICK(halfrect_tick) { } static CTOR(halfrect_ctor) { - ugen_ini(UGEN(o), 1, 1); - ugen_gen(UGEN(o), halfrect_tick, (m_float*)xmalloc(SZ_FLOAT), 0); + ugen_ini(shred->info->mp, UGEN(o), 1, 1); + ugen_gen(shred->info->mp, UGEN(o), halfrect_tick, (m_float*)xmalloc(SZ_FLOAT), 0); *(m_float*)UGEN(o)->module.gen.data = 1; } @@ -117,8 +118,8 @@ static TICK(step_tick) { } static CTOR(step_ctor) { - ugen_ini(UGEN(o), 0, 1); - ugen_gen(UGEN(o), step_tick, (m_float*)xmalloc(SZ_FLOAT), 0); + ugen_ini(shred->info->mp, UGEN(o), 0, 1); + ugen_gen(shred->info->mp, UGEN(o), step_tick, (m_float*)xmalloc(SZ_FLOAT), 0); *(m_float*)UGEN(o)->module.gen.data = 0; } @@ -149,8 +150,8 @@ static TICK(zerox_tick) { } static CTOR(zerox_ctor) { - ugen_ini(UGEN(o), 1, 1); - ugen_gen(UGEN(o), zerox_tick, (m_float*)xmalloc(SZ_FLOAT), 0); + ugen_ini(shred->info->mp, UGEN(o), 1, 1); + ugen_gen(shred->info->mp, UGEN(o), zerox_tick, (m_float*)xmalloc(SZ_FLOAT), 0); *(m_float*)UGEN(o)->module.gen.data = 1; } diff --git a/src/lib/object.c b/src/lib/object.c index 2960f520..e43ff7b9 100644 --- a/src/lib/object.c +++ b/src/lib/object.c @@ -5,6 +5,7 @@ #include "oo.h" #include "vm.h" #include "env.h" +#include "gwion.h" #include "type.h" #include "nspc.h" #include "value.h" @@ -12,7 +13,6 @@ #include "object.h" #include "import.h" #include "operator.h" -#include "gwion.h" ANN void exception(const VM_Shred shred, const m_str c) { err_msg(0, "%s: shred[id=%" UINT_F ":%s], PC=[%" UINT_F "]", @@ -20,8 +20,8 @@ ANN void exception(const VM_Shred shred, const m_str c) { vm_shred_exit(shred); } -M_Object new_object(const VM_Shred shred, const Type t) { - const M_Object a = mp_alloc(M_Object); +M_Object new_object(MemPool p, const VM_Shred shred, const Type t) { + const M_Object a = mp_alloc(p, M_Object); a->ref = 1; a->type_ref = t; a->vtable = &t->nspc->info->vtable; @@ -37,28 +37,28 @@ M_Object new_object(const VM_Shred shred, const Type t) { return a; } -M_Object new_string(const VM_Shred shred, const m_str str) { - const M_Object o = new_object(shred, t_string); +M_Object new_string(MemPool p, const VM_Shred shred, const m_str str) { + const M_Object o = new_object(p, shred, t_string); STRING(o) = s_name(insert_symbol(shred->info->vm->gwion->st, str)); return o; } -M_Object new_string2(const VM_Shred shred, const m_str str) { - const M_Object o = new_object(shred, t_string); +M_Object new_string2(MemPool p, const VM_Shred shred, const m_str str) { + const M_Object o = new_object(p, shred, t_string); STRING(o) = str; return o; } ANN void instantiate_object(const VM_Shred shred, const Type type) { // const M_Object object = new_object(NULL, type); - const M_Object object = new_object(shred, type); + const M_Object object = new_object(shred->info->mp, shred, type); *(M_Object*)REG(0) = object; PUSH_REG(shred, SZ_INT); } ANN static void handle_dtor(const M_Object object, const VM_Shred shred) { const Type t = object->type_ref; - const VM_Shred sh = new_vm_shred(t->nspc->dtor); + const VM_Shred sh = new_vm_shred(shred->info->mp, t->nspc->dtor); ADD_REF(t->nspc->dtor); sh->base = shred->base; memcpy(sh->reg, shred->reg, SIZEOF_REG); @@ -69,6 +69,9 @@ ANN static void handle_dtor(const M_Object object, const VM_Shred shred) { __attribute__((hot)) ANN void __release(const M_Object obj, const VM_Shred shred) { + // TODO: find what's going on here + // we should be able to use shred->info->mp directly + MemPool p = shred->info->mp= shred->info->vm->gwion->p; Type t = obj->type_ref; while(t->parent) { struct scope_iter iter = { &t->nspc->info->value, 0, 0 };\ @@ -88,13 +91,13 @@ ANN void __release(const M_Object obj, const VM_Shred shred) { } t = t->parent; } - free_object(obj); + free_object(p, obj); } -ANN void free_object(const M_Object o) { +ANN void free_object(MemPool p, const M_Object o) { if(o->data) _mp_free2(o->p, o->data); - mp_free(M_Object, o); + mp_free(p, M_Object, o); } #define describe_logical(name, op) \ @@ -130,7 +133,7 @@ static OP_CHECK(opck_object_cast) { const Type r = cast->self->type; // return isa(l, r) > 0 ? r : t_null; if(isa(l, r) > 0) { - const Type t = type_copy(r); + const Type t = type_copy(env->gwion->p, r); SET_FLAG(t, force); env_add_type(env, t); return t; diff --git a/src/lib/prim.c b/src/lib/prim.c index 155ba89c..a1fc03b7 100644 --- a/src/lib/prim.c +++ b/src/lib/prim.c @@ -3,10 +3,12 @@ #include "oo.h" #include "vm.h" #include "env.h" +#include "gwion.h" #include "type.h" #include "instr.h" #include "object.h" #include "import.h" +#include "gwi.h" #include "emit.h" #include "operator.h" #include "driver.h" @@ -95,15 +97,15 @@ OP_CHECK(opck_unary_meta2) { static GWION_IMPORT(values) { VM* vm = gwi_vm(gwi); - ALLOC_PTR(d_zero, m_float, 0.0); - ALLOC_PTR(sr, m_float, (m_float)vm->bbq->si->sr); - ALLOC_PTR(samp, m_float, 1.0); - ALLOC_PTR(ms, m_float, (m_float)(*sr / 1000.)); - ALLOC_PTR(second, m_float, (m_float)*sr); - ALLOC_PTR(minute, m_float, (m_float)(*sr * 60.0)); - ALLOC_PTR(hour, m_float, (m_float)(*minute * 60.0)); - ALLOC_PTR(t_zero, m_float, 0.0); - ALLOC_PTR(pi, m_float, (m_float)M_PI); + ALLOC_PTR(gwi->gwion->p, d_zero, m_float, 0.0); + ALLOC_PTR(gwi->gwion->p, sr, m_float, (m_float)vm->bbq->si->sr); + ALLOC_PTR(gwi->gwion->p, samp, m_float, 1.0); + ALLOC_PTR(gwi->gwion->p, ms, m_float, (m_float)(*sr / 1000.)); + ALLOC_PTR(gwi->gwion->p, second, m_float, (m_float)*sr); + ALLOC_PTR(gwi->gwion->p, minute, m_float, (m_float)(*sr * 60.0)); + ALLOC_PTR(gwi->gwion->p, hour, m_float, (m_float)(*minute * 60.0)); + ALLOC_PTR(gwi->gwion->p, t_zero, m_float, 0.0); + ALLOC_PTR(gwi->gwion->p, pi, m_float, (m_float)M_PI); gwi_item_ini(gwi, "float", "samplerate"); gwi_item_end(gwi, ae_flag_const, sr); gwi_item_ini(gwi, "float", "pi"); diff --git a/src/lib/ptr.c b/src/lib/ptr.c index f9601b97..de1fb3fa 100644 --- a/src/lib/ptr.c +++ b/src/lib/ptr.c @@ -5,6 +5,7 @@ #include "oo.h" #include "vm.h" #include "env.h" +#include "gwion.h" #include "type.h" #include "nspc.h" #include "instr.h" @@ -12,7 +13,6 @@ #include "import.h" #include "emit.h" #include "operator.h" -#include "gwion.h" static OP_CHECK(opck_ptr_assign) { const Exp_Binary* bin = (Exp_Binary*)data; @@ -70,7 +70,7 @@ static OP_EMIT(opem_ptr_deref) { } static INSTR(Cast2Ptr) { GWDEBUG_EXE - const M_Object o = new_object(shred, t_ptr); + const M_Object o = new_object(shred->info->mp, shred, t_ptr); *(m_uint**)o->data = *(m_uint**)REG(-SZ_INT); *(M_Object*)REG(-SZ_INT) = o; } diff --git a/src/lib/shred.c b/src/lib/shred.c index b583b27f..46f69bed 100644 --- a/src/lib/shred.c +++ b/src/lib/shred.c @@ -19,7 +19,7 @@ static m_int o_fork_thread, o_fork_retsize, o_fork_retval; #define FORK_RETVAL(o) (o->data + o_fork_retval) M_Object new_shred(const VM_Shred shred, m_bool is_spork) { - const M_Object obj = new_object(NULL, is_spork ? t_shred : t_fork); + const M_Object obj = new_object(shred->info->mp, NULL, is_spork ? t_shred : t_fork); ME(obj) = shred; if(!is_spork) { // *(M_Object*)(obj->data + o_fork_ev) = new_object(NULL, t_event); @@ -49,13 +49,13 @@ static MFUN(vm_shred_is_done) { static MFUN(shred_yield) { const VM_Shred s = ME(o); - const Shreduler sh = shred->info->vm->shreduler; + const Shreduler sh = shred->tick->shreduler; shredule(sh, s, .5); } static SFUN(vm_shred_from_id) { const m_int index = *(m_int*)MEM(0); - const VM_Shred s = (VM_Shred)vector_at(&shred->info->vm->shreduler->shreds, (vtype)index); + const VM_Shred s = (VM_Shred)vector_at(&shred->tick->shreduler->shreds, (vtype)index); if(s) { *(M_Object*)RETURN = s->info->me; // s->info->me->ref++; @@ -74,7 +74,7 @@ static MFUN(shred_arg) { const m_int idx = *(m_int*)MEM(SZ_INT); if(s->info->args && idx >= 0) { const m_str str = (m_str)vector_at(s->info->args, *(m_uint*)MEM(SZ_INT)); - *(M_Object*)RETURN = str ? new_string(shred, str) : NULL; + *(M_Object*)RETURN = str ? new_string(shred->info->mp, shred, str) : NULL; } else *(m_uint*)RETURN = 0; } @@ -83,7 +83,7 @@ static MFUN(shred_arg) { static MFUN(shred##name##_path) { \ const VM_Shred s = ME(o); \ const m_str str = code_name((src), 1); \ - *(m_uint*)RETURN = (m_uint)new_string(shred, str); \ + *(m_uint*)RETURN = (m_uint)new_string(shred->info->mp, shred, str); \ } \ static MFUN(shred##name##_dir) { \ const VM_Shred s = ME(o); \ @@ -91,7 +91,7 @@ static MFUN(shred##name##_dir) { \ const size_t len = strlen(str); \ char c[len + 1]; \ strcpy(c, str); \ - *(m_uint*)RETURN = (m_uint)new_string(shred, dirname(c)); \ + *(m_uint*)RETURN = (m_uint)new_string(shred->info->mp, shred, dirname(c)); \ } describe_path_and_dir(, s->info->name) describe_path_and_dir(_code, s->code->name) @@ -99,7 +99,7 @@ describe_path_and_dir(_code, s->code->name) static DTOR(shred_dtor) { free_vm_shred(*(VM_Shred*)o->data); } static DTOR(fork_dtor) { - mp_free(Gwion, ME(o)->info->vm->gwion); + mp_free(shred->info->mp, Gwion, ME(o)->info->vm->gwion); free_vm(ME(o)->info->vm); } diff --git a/src/lib/string.c b/src/lib/string.c index 1e2361fb..7ec375dd 100644 --- a/src/lib/string.c +++ b/src/lib/string.c @@ -20,7 +20,7 @@ ANN static void push_string(const VM_Shred shred, const M_Object obj, const m_st } ANN static void push_new_string(const VM_Shred shred, const m_str c) { - const M_Object obj = new_string(shred, c); + const M_Object obj = new_string(shred->info->mp, shred, c); *(M_Object*)REG(-SZ_INT) = (M_Object)obj; } diff --git a/src/lib/ugen.c b/src/lib/ugen.c index 43be23a6..e439d729 100644 --- a/src/lib/ugen.c +++ b/src/lib/ugen.c @@ -2,12 +2,14 @@ #include "gwion_util.h" #include "gwion_ast.h" #include "oo.h" -#include "vm.h" #include "env.h" +#include "vm.h" +#include "gwion.h" #include "type.h" #include "instr.h" #include "object.h" #include "import.h" +#include "gwi.h" #include "ugen.h" #include "driver.h" @@ -73,55 +75,55 @@ describe_compute(multi,,) describe_compute(mono, trig, {u->module.gen.trig->compute(u->module.gen.trig);}) describe_compute(multi, trig, {u->module.gen.trig->compute(u->module.gen.trig);}) -ANEW static UGen new_UGen() { - const UGen u = mp_alloc(UGen); +ANEW static UGen new_UGen(MemPool p) { + const UGen u = mp_alloc(p, UGen); u->op = ugop_add; u->compute = gen_compute_mono; return u; } -ANEW /*static */M_Object new_M_UGen() { - const M_Object o = new_object(NULL, t_ugen); - UGEN(o) = new_UGen(); +ANEW M_Object new_M_UGen(MemPool p) { + const M_Object o = new_object(p, NULL, t_ugen); + UGEN(o) = new_UGen(p); return o; } -ANN static void assign_channel(const UGen u) { +ANN static void assign_channel(MemPool p, const UGen u) { u->multi = 1; u->compute = gen_compute_multi; u->connect.multi->channel = (M_Object*)xmalloc(u->connect.multi->n_chan * SZ_INT); for(uint i = u->connect.multi->n_chan + 1; --i;) { const uint j = i - 1; - const M_Object chan = new_M_UGen(); - ugen_ini(UGEN(chan), u->connect.multi->n_in > j, u->connect.multi->n_out > j); + const M_Object chan = new_M_UGen(p); + ugen_ini(p, UGEN(chan), u->connect.multi->n_in > j, u->connect.multi->n_out > j); UGEN(chan)->module.ref = u; UGEN(chan)->compute = compute_chan; u->connect.multi->channel[j] = chan; } } -ANN void ugen_gen(const UGen u, const f_tick tick, void* data, const m_bool trig) { +ANN void ugen_gen(MemPool p, const UGen u, const f_tick tick, void* data, const m_bool trig) { u->module.gen.tick = tick; u->module.gen.data = data; if(trig) { - u->module.gen.trig = new_UGen(); + u->module.gen.trig = new_UGen(p); u->module.gen.trig->compute = compute_mono; - ugen_ini(u->module.gen.trig, 1, 1); + ugen_ini(p, u->module.gen.trig, 1, 1); u->compute = (u->compute == gen_compute_mono ? gen_compute_monotrig : gen_compute_multitrig); } } -ANN void ugen_ini(const UGen u, const uint in, const uint out) { +ANN void ugen_ini(MemPool p, const UGen u, const uint in, const uint out) { const uint chan = in > out ? in : out; if(chan == 1) { vector_init(&u->connect.net.from); vector_init(&u->connect.net.to); } else { - u->connect.multi = mp_alloc(ugen_multi); + u->connect.multi = mp_alloc(p, ugen_multi); u->connect.multi->n_in = in; u->connect.multi->n_out = out; u->connect.multi->n_chan = chan; - assign_channel(u); + assign_channel(p, u); } } @@ -203,7 +205,7 @@ describe_connect_instr(Trig, Connect, TRIG_EX) describe_connect_instr(Trig, Disconnect, TRIG_EX) static CTOR(ugen_ctor) { - UGEN(o) = new_UGen(); + UGEN(o) = new_UGen(shred->info->mp); vector_add(&shred->info->vm->ugen, (vtype)UGEN(o)); } @@ -239,9 +241,9 @@ static DTOR(ugen_dtor) { release_multi(ug, shred); if(ug->module.gen.trig) { release_mono(ug->module.gen.trig); - mp_free(UGen, ug->module.gen.trig); + mp_free(shred->info->mp, UGen, ug->module.gen.trig); } - mp_free(UGen, ug); + mp_free(shred->info->mp, UGen, ug); } static MFUN(ugen_channel) { @@ -300,10 +302,10 @@ struct ugen_importer { ANN static m_int add_ugen(const Gwi gwi, struct ugen_importer* imp) { VM* vm = gwi_vm(gwi); - const M_Object o = new_M_UGen(); + const M_Object o = new_M_UGen(gwi->gwion->p); const UGen u = imp->ugen = UGEN(o); - ugen_ini(u, imp->nchan, imp->nchan); - ugen_gen(u, imp->tick, (void*)imp->vm, 0); + ugen_ini(vm->gwion->p, u, imp->nchan, imp->nchan); + ugen_gen(vm->gwion->p, u, imp->tick, (void*)imp->vm, 0); vector_add(&vm->ugen, (vtype)u); CHECK_BB(gwi_item_ini(gwi, "UGen", imp->name)) return gwi_item_end(gwi, ae_flag_const, o); diff --git a/src/lib/vararg.c b/src/lib/vararg.c index 41aecd7d..7ac4539e 100644 --- a/src/lib/vararg.c +++ b/src/lib/vararg.c @@ -10,11 +10,12 @@ #include "object.h" #include "import.h" #include "vararg.h" +#include "gwion.h" -void free_vararg(struct Vararg_* arg) { +void free_vararg(MemPool p, struct Vararg_* arg) { xfree(arg->d); xfree(arg->k); - mp_free(Vararg, arg); + mp_free(p, Vararg, arg); } INSTR(VarargTop) { GWDEBUG_EXE @@ -24,14 +25,14 @@ INSTR(VarargTop) { GWDEBUG_EXE PUSH_REG(shred, SZ_INT) else { shred->pc = instr->m_val2 + 1; - mp_free(Vararg, arg); + mp_free(shred->info->mp, Vararg, arg); } } else shred->pc = instr->m_val2 + 1; } INSTR(VarargIni) { GWDEBUG_EXE - struct Vararg_* arg = mp_alloc(Vararg); + struct Vararg_* arg = mp_alloc(shred->info->mp, Vararg); POP_REG(shred, instr->m_val - SZ_INT) arg->d = (m_bit*)xmalloc(instr->m_val); for(m_uint i = 0; i < instr->m_val; i += SZ_INT) @@ -51,7 +52,7 @@ INSTR(VarargEnd) { GWDEBUG_EXE if(++arg->i < arg->s) shred->pc = instr->m_val2; else - free_vararg(arg); + free_vararg(shred->info->mp, arg); } INSTR(VarargMember) { GWDEBUG_EXE @@ -73,7 +74,7 @@ static INSTR(VarargAssign) { GWDEBUG_EXE static FREEARG(freearg_vararg) { if(instr->m_val2) - free_vector((Vector)instr->m_val2); + free_vector(((Gwion)gwion)->p, (Vector)instr->m_val2); } GWION_IMPORT(vararg) { diff --git a/src/oo/context.c b/src/oo/context.c index 7548425a..959d7662 100644 --- a/src/oo/context.c +++ b/src/oo/context.c @@ -4,16 +4,18 @@ #include "oo.h" #include "env.h" #include "nspc.h" +#include "vm.h" +#include "gwion.h" #include "context.h" -ANN static void free_context(const Context a, void *gwion) { +ANN static void free_context(const Context a, Gwion gwion) { REM_REF(a->nspc, gwion) - mp_free(Context, a); + mp_free(gwion->p, Context, a); } -ANN2(2) Context new_context(const Ast ast, const m_str str) { - const Context context = mp_alloc(Context); - context->nspc = new_nspc(str); +ANN2(2) Context new_context(MemPool p, const Ast ast, const m_str str) { + const Context context = mp_alloc(p, Context); + context->nspc = new_nspc(p, str); context->tree = ast; context->name = str; INIT_OO(context, free_context); @@ -31,7 +33,7 @@ ANN void unload_context(const Context context, const Env env) { if(context->lbls.ptr) { LOOP_OPTIM for(m_uint i = 0; i < map_size(&context->lbls); i++) - free_map((Map)map_at(&context->lbls, i)); + free_map(env->gwion->p, (Map)map_at(&context->lbls, i)); map_release(&context->lbls); } REM_REF(context, env->gwion); diff --git a/src/oo/env.c b/src/oo/env.c index 1cd1bf22..36391262 100644 --- a/src/oo/env.c +++ b/src/oo/env.c @@ -4,6 +4,7 @@ #include "oo.h" #include "vm.h" #include "env.h" +#include "gwion.h" #include "operator.h" #include "value.h" #include "traverse.h" @@ -15,8 +16,8 @@ #include "vm.h" #include "parse.h" -ANN static struct Env_Scope_ *new_scope(void) { - struct Env_Scope_ *a = mp_alloc(Env_Scope); +ANN static struct Env_Scope_ *new_scope(MemPool p) { + struct Env_Scope_ *a = mp_alloc(p, Env_Scope); vector_init(&a->breaks); vector_init(&a->conts); vector_init(&a->class_stack); @@ -27,11 +28,11 @@ ANN static struct Env_Scope_ *new_scope(void) { return a; } -Env new_env() { +Env new_env(MemPool p) { const Env env = (Env)xmalloc(sizeof(struct Env_)); - env->global_nspc = new_nspc("global_nspc"); + env->global_nspc = new_nspc(p, "global_nspc"); env->context = NULL; - env->scope = new_scope(); + env->scope = new_scope(p); env_reset(env); return env; } @@ -50,7 +51,7 @@ ANN void env_reset(const Env env) { switch_reset(env); } -ANN static void free_env_scope(struct Env_Scope_ *a, void *gwion) { +ANN static void free_env_scope(struct Env_Scope_ *a, Gwion gwion) { const m_uint size = vector_size(&a->known_ctx); for(m_uint i = size + 1; --i;) REM_REF((Context)vector_at(&a->known_ctx, i - 1), gwion); @@ -60,7 +61,7 @@ ANN static void free_env_scope(struct Env_Scope_ *a, void *gwion) { vector_release(&a->breaks); vector_release(&a->conts); switch_release(&a->swi); - mp_free(Env_Scope, a); + mp_free(gwion->p, Env_Scope, a); } ANN void free_env(const Env a) { @@ -87,11 +88,11 @@ ANN void env_pop(const Env env, const m_uint scope) { } ANN void env_add_type(const Env env, const Type type) { - const Type v_type = type_copy(t_class); + const Type v_type = type_copy(env->gwion->p, t_class); v_type->d.base_type = type; SET_FLAG(type, builtin); nspc_add_type(env->curr, insert_symbol(type->name), type); - const Value v = new_value(v_type, type->name); + const Value v = new_value(env->gwion->p, v_type, type->name); SET_FLAG(v, checked | ae_flag_const | ae_flag_global | ae_flag_builtin); nspc_add_value(env->curr, insert_symbol(type->name), v); type->owner = env->curr; @@ -99,7 +100,7 @@ ANN void env_add_type(const Env env, const Type type) { } ANN m_bool type_engine_check_prog(const Env env, const Ast ast) { - const Context ctx = new_context(ast, env->name); + const Context ctx = new_context(env->gwion->p, ast, env->name); env_reset(env); load_context(ctx, env); const m_bool ret = traverse_ast(env, ast); @@ -116,5 +117,5 @@ ANN m_bool env_add_op(const Env env, const struct Op_Import* opi) { const Nspc nspc = env->curr; if(!nspc->info->op_map.ptr) map_init(&nspc->info->op_map); - return add_op(nspc, opi); + return add_op(env->gwion, nspc, opi); } diff --git a/src/oo/nspc.c b/src/oo/nspc.c index e41f5d04..15402d3a 100644 --- a/src/oo/nspc.c +++ b/src/oo/nspc.c @@ -9,8 +9,8 @@ #include "value.h" #include "func.h" #include "object.h" -#include "operator.h" #include "gwion.h" +#include "operator.h" ANN void nspc_commit(const Nspc nspc) { scope_commit(&nspc->info->value); @@ -27,7 +27,7 @@ ANN static inline void nspc_release_object(const Nspc a, Value value, Gwion gwio } } -ANN static void free_nspc_value(const Nspc a, void *gwion) { +ANN static void free_nspc_value(const Nspc a, Gwion gwion) { struct scope_iter iter = { &a->info->value, 0, 0 }; Value v; while(scope_iter(&iter, &v) > 0) { @@ -38,22 +38,22 @@ ANN static void free_nspc_value(const Nspc a, void *gwion) { } REM_REF(v, gwion); } - scope_release(&a->info->value); + scope_release(gwion->p, &a->info->value); } #define describe_nspc_free(A, b) \ -ANN static void nspc_free_##b(Nspc n, void *gwion) {\ +ANN static void nspc_free_##b(Nspc n, Gwion gwion) {\ struct scope_iter iter = { &n->info->b, 0, 0 };\ A a;\ while(scope_iter(&iter, &a) > 0) \ REM_REF(a, gwion);\ - scope_release(&n->info->b);\ + scope_release(gwion->p, &n->info->b);\ } describe_nspc_free(Func, func) describe_nspc_free(Type, type) -ANN static void free_nspc(Nspc a, void *gwion) { +ANN static void free_nspc(Nspc a, Gwion gwion) { nspc_free_func(a, gwion); nspc_free_type(a, gwion); free_nspc_value(a, gwion); @@ -64,21 +64,21 @@ ANN static void free_nspc(Nspc a, void *gwion) { vector_release(&a->info->vtable); if(a->info->op_map.ptr) free_op_map(&a->info->op_map, gwion); - mp_free(NspcInfo, a->info); + mp_free(gwion->p, NspcInfo, a->info); if(a->pre_ctor) REM_REF(a->pre_ctor, gwion); if(a->dtor) REM_REF(a->dtor, gwion); - mp_free(Nspc, a); + mp_free(gwion->p, Nspc, a); } -ANN Nspc new_nspc(const m_str name) { - const Nspc a = mp_alloc(Nspc); +ANN Nspc new_nspc(MemPool p, const m_str name) { + const Nspc a = mp_alloc(p, Nspc); a->name = name; - a->info = mp_alloc(NspcInfo); - scope_init(&a->info->value); - scope_init(&a->info->type); - scope_init(&a->info->func); + a->info = mp_alloc(p, NspcInfo); + scope_init(p, &a->info->value); + scope_init(p, &a->info->type); + scope_init(p, &a->info->func); INIT_OO(a, free_nspc); return a; } diff --git a/src/oo/switch.c b/src/oo/switch.c index b4a50907..cc5e0434 100644 --- a/src/oo/switch.c +++ b/src/oo/switch.c @@ -5,28 +5,27 @@ #include "env.h" #include "vm.h" #include "env.h" +#include "gwion.h" #include "operator.h" #include "value.h" #include "type.h" #include "context.h" #include "nspc.h" -#include "mpool.h" - -static Switch new_switch() { - Switch sw = mp_alloc(Switch); - sw->cases = new_map(); // could be struct ? +static Switch new_switch(MemPool p) { + Switch sw = mp_alloc(p, Switch); + sw->cases = new_map(p); // could be struct ? vector_init(&sw->exp); - sw->vec = new_vector(); + sw->vec = new_vector(p); return sw; } -ANN static void free_switch(const Switch sw) { +ANN static void free_switch(MemPool p, const Switch sw) { // if(!sw->ok) // free_map(sw->cases); - free_vector(sw->vec); // only for dynamic + free_vector(p, sw->vec); // only for dynamic vector_release(&sw->exp); - mp_free(Switch, sw); + mp_free(p, Switch, sw); } struct SwInfo_ { @@ -36,11 +35,11 @@ struct SwInfo_ { }; ANN static Switch new_swinfo(const Env env, const Stmt_Switch stmt) { - struct SwInfo_ *info = mp_alloc(SwInfo); + struct SwInfo_ *info = mp_alloc(env->gwion->p, SwInfo); info->s = stmt; info->t = env->class_def; info->f = env->func; - const Switch sw = new_switch(); + const Switch sw = new_switch(env->gwion->p); map_set(&env->scope->swi.map, (vtype)info, (vtype)sw); return sw; } @@ -74,10 +73,10 @@ ANN void switch_get(const Env env, const Stmt_Switch stmt) { void switch_reset(const Env env) { for(m_uint i = VLEN(&env->scope->swi.map) + 1; --i;) { struct SwInfo_ *info = (struct SwInfo_ *)VKEY(&env->scope->swi.map, i - 1); - mp_free(SwInfo, info); + mp_free(env->gwion->p, SwInfo, info); Switch sw = (Switch)VVAL(&env->scope->swi.map, i - 1); - free_map(sw->cases); - free_switch(sw); + free_map(env->gwion->p, sw->cases); + free_switch(env->gwion->p, sw); } vector_clear((Vector)&env->scope->swi); map_clear(&env->scope->swi.map); @@ -141,7 +140,7 @@ ANN Map switch_map(const Env env) { ANN Vector switch_vec(const Env env) { const Switch sw = (Switch)vector_back((Vector)&env->scope->swi); - return vector_copy(sw->vec); // new_vector(); // dyn only + return vector_copy(env->gwion->p, sw->vec); // new_vector(); // dyn only } ANN m_uint switch_idx(const Env env) { @@ -157,6 +156,6 @@ ANN void switch_end(const Env env) { const Switch sw = (Switch)vector_pop((Vector)&env->scope->swi); const vtype index = VKEY(&env->scope->swi.map, VLEN(&env->scope->swi.map) - 1); map_remove(&env->scope->swi.map, index); - free_switch(sw); + free_switch(env->gwion->p, sw); // return sw->ok = 1; } diff --git a/src/oo/type.c b/src/oo/type.c index 2fad207d..815604ec 100644 --- a/src/oo/type.c +++ b/src/oo/type.c @@ -7,17 +7,18 @@ #include "nspc.h" #include "vm.h" #include "parse.h" +#include "gwion.h" -ANN static void free_type(Type a, void *gwion) { +ANN static void free_type(Type a, Gwion gwion) { if(GET_FLAG(a, template)) - free_class_def(a->def); + free_class_def(gwion->p, a->def); if(a->nspc) REM_REF(a->nspc, gwion); - mp_free(Type, a); + mp_free(gwion->p, Type, a); } -ANN2(2) Type new_type(const m_uint xid, const m_str name, const Type parent) { - const Type type = mp_alloc(Type); +Type new_type(MemPool p, const m_uint xid, const m_str name, const Type parent) { + const Type type = mp_alloc(p, Type); type->xid = xid; type->name = name; type->parent = parent; @@ -27,8 +28,8 @@ ANN2(2) Type new_type(const m_uint xid, const m_str name, const Type parent) { return type; } -ANN Type type_copy(const Type type) { - const Type a = new_type(type->xid, type->name, type->parent); +ANN Type type_copy(MemPool p, const Type type) { + const Type a = new_type(p, type->xid, type->name, type->parent); a->nspc = type->nspc; a->owner = type->owner; a->size = type->size; @@ -80,7 +81,7 @@ ANN Type array_type(const Env env, const Type base, const m_uint depth) { const Type type = nspc_lookup_type1(base->owner, sym); if(type) return type; - const Type t = new_type(t_array->xid, base->name, t_array); + const Type t = new_type(env->gwion->p, t_array->xid, base->name, t_array); t->name = s_name(sym); t->size = SZ_INT; t->array_depth = depth; diff --git a/src/oo/value.c b/src/oo/value.c index df62046f..4a8fffc1 100644 --- a/src/oo/value.c +++ b/src/oo/value.c @@ -3,21 +3,23 @@ #include "gwion_ast.h" #include "oo.h" #include "env.h" +#include "vm.h" +#include "gwion.h" #include "value.h" #include "type.h" -ANN static void free_value(Value a, void *gwion) { - if(!GET_FLAG(a, func) && a->d.ptr && +ANN static void free_value(Value a, Gwion gwion) { + if(!GET_FLAG(a, func) && a->d.ptr && !GET_FLAG(a, union) && !(GET_FLAG(a, enum) && GET_FLAG(a, builtin) && a->owner_class) && isa(a->type, t_object) < 0) - _mp_free(a->type->size, a->d.ptr); + _mp_free(gwion->p, a->type->size, a->d.ptr); if(isa(a->type, t_class) > 0 || isa(a->type, t_function) > 0 || GET_FLAG(a->type, op)) REM_REF(a->type, gwion) - mp_free(Value, a); + mp_free(gwion->p, Value, a); } -ANN Value new_value(const Type type, const m_str name) { - const Value a = mp_alloc(Value); +ANN Value new_value(MemPool p, const Type type, const m_str name) { + const Value a = mp_alloc(p, Value); a->type = type; a->name = name; INIT_OO(a, free_value); diff --git a/src/parse/check.c b/src/parse/check.c index 8686c75f..49e4a06b 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -306,11 +306,11 @@ ANN static Type_List mk_type_list(const Env env, const Type type) { } ID_List id = NULL; for(m_uint i = vector_size(&v) + 1; --i;) - id = prepend_id_list((Symbol)vector_at(&v, i - 1), id, 0); + id = prepend_id_list(env->gwion->p, (Symbol)vector_at(&v, i - 1), id, 0); vector_release(&v); assert(id); - Type_Decl* td = new_type_decl(id, 0); - return new_type_list(td, NULL); + Type_Decl* td = new_type_decl(env->gwion->p, id, 0); + return new_type_list(env->gwion->p, td, NULL); } ANN static m_bool func_match_inner(const Env env, const Exp e, const Type t, @@ -337,7 +337,6 @@ ANN2(1,2) static Func find_func_match_actual(const Env env, Func func, const Exp const m_bool implicit, const m_bool specific) { do { Exp e = args; -printf("base %s %p %p\n", func->name, func->def->base, func->def); Arg_List e1 = func->def->base->args; while(e) { if(!e1) { @@ -403,19 +402,19 @@ ANN static Func _find_template_match(const Env env, const Value v, const Exp_Cal if(!(value = template_get_ready(env, v, "template", i))) continue; base = value->d.func_ref->def; - def->tmpl = new_tmpl_list(base->tmpl->list, (m_int)i); + def->tmpl = new_tmpl_list(env->gwion->p, base->tmpl->list, (m_int)i); } } else { if(!(value = template_get_ready(env, v, "template", i))) continue; base = value->d.func_ref->def; - def = new_func_def(new_func_base(base->base->td, insert_symbol(v->name), + def = new_func_def(env->gwion->p, new_func_base(env->gwion->p, base->base->td, insert_symbol(v->name), base->base->args), base->d.code, base->flag); - def->tmpl = new_tmpl_list(base->tmpl->list, (m_int)i); + def->tmpl = new_tmpl_list(env->gwion->p, base->tmpl->list, (m_int)i); SET_FLAG(def, template); } if(traverse_func_template(env, def, types) > 0) { - nspc_pop_type(env->curr); + nspc_pop_type(env->gwion->p, env->curr); if(check_call(env, exp) > 0) { const Func next = def->base->func->next; def->base->func->next = NULL; @@ -491,7 +490,7 @@ ANN static m_uint get_type_number(ID_List list) { ANN static Func get_template_func(const Env env, const Exp_Call* func, const Value v) { const Func f = find_template_match(env, v, func); if(f) { - Tmpl_Call* tmpl = new_tmpl_call(func->tmpl->types); + Tmpl_Call* tmpl = new_tmpl_call(env->gwion->p, func->tmpl->types); tmpl->base = v->d.func_ref->def->tmpl->list; func->self->d.exp_call.tmpl = tmpl; return f; @@ -560,7 +559,7 @@ ANN static Type check_lambda_call(const Env env, const Exp_Call *exp) { } if(arg || e) ERR_O(exp->self->pos, "argument number does not match for lambda") - l->def = new_func_def(new_func_base(NULL, l->name, l->args), l->code, 0); + l->def = new_func_def(env->gwion->p, new_func_base(env->gwion->p, NULL, l->name, l->args), l->code, 0); CHECK_BO(traverse_func_def(env, l->def)) if(env->class_def) SET_FLAG(l->def, member); @@ -786,7 +785,7 @@ ANN static m_bool do_stmt_auto(const Env env, const Stmt_Auto stmt) { GWDEBUG_EX check_class_def(env, ptr->def); } t = depth ? array_type(env, ptr, depth) : ptr; - stmt->v = new_value(t, s_name(stmt->sym)); + stmt->v = new_value(env->gwion->p, t, s_name(stmt->sym)); SET_FLAG(stmt->v, checked); nspc_add_value(env->curr, stmt->sym, stmt->v); return check_conts(env, stmt->self, stmt->body); @@ -1043,7 +1042,7 @@ ANN static m_bool check_func_def_override(const Env env, const Func_Def f) { GWD } ANN static Value set_variadic(const Env env) { - const Value variadic = new_value(t_vararg, "vararg"); + const Value variadic = new_value(env->gwion->p, t_vararg, "vararg"); SET_FLAG(variadic, checked); nspc_add_value(env->curr, insert_symbol("vararg"), variadic); return variadic; @@ -1072,7 +1071,7 @@ ANN m_bool check_func_def(const Env env, const Func_Def f) { GWDEBUG_EXE const Func former = env->func; env->func = func; ++env->scope->depth; - nspc_push_value(env->curr); + nspc_push_value(env->gwion->p, env->curr); if(!f->base->args) UNSET_FLAG(f->base->func, pure); else @@ -1089,7 +1088,7 @@ ANN m_bool check_func_def(const Env env, const Func_Def f) { GWDEBUG_EXE else if(GET_FLAG(f, op)) operator_func(func); } - nspc_pop_value(env->curr); + nspc_pop_value(env->gwion->p, env->curr); --env->scope->depth; env->func = former; if(GET_FLAG(f, global)) @@ -1113,7 +1112,7 @@ ANN static m_bool check_class_parent(const Env env, const Class_Def class_def) { CHECK_BB(template_push_types(env, class_def->tmpl->list.list, class_def->tmpl->base)) CHECK_BB(traverse_template(env, t->def)) if(class_def->tmpl) - nspc_pop_type(env->curr); + nspc_pop_type(env->gwion->p, env->curr); } } if(!GET_FLAG(class_def->base.type->parent, checked)) @@ -1151,7 +1150,7 @@ ANN m_bool check_class_def(const Env env, const Class_Def class_def) { GWDEBUG_E if(class_def->body) CHECK_BB(check_class_body(env, class_def)) if(!the_class->p && the_class->nspc->info->offset) - the_class->p = mp_ini((uint32_t)the_class->nspc->info->offset); + the_class->p = mp_ini(env->gwion->p, (uint32_t)the_class->nspc->info->offset); SET_FLAG(the_class, checked | ae_flag_check); return GW_OK; } diff --git a/src/parse/func.c b/src/parse/func.c index 7f8e1805..130592bc 100644 --- a/src/parse/func.c +++ b/src/parse/func.c @@ -4,22 +4,23 @@ #include "oo.h" #include "vm.h" #include "env.h" +#include "gwion.h" #include "type.h" #include "nspc.h" #include "func.h" -ANN static void free_func(Func a, void *gwion) { +ANN static void free_func(Func a, Gwion gwion) { if(GET_FLAG(a, template)) { - free_tmpl_list(a->def->tmpl); - mp_free(Func_Def, a->def); + free_tmpl_list(gwion->p, a->def->tmpl); + mp_free(gwion->p, Func_Def, a->def); } if(a->code) REM_REF(a->code, gwion); - mp_free(Func, a); + mp_free(gwion->p, Func, a); } -ANN Func new_func(const m_str name, const Func_Def def) { - Func func = mp_alloc(Func); +ANN Func new_func(MemPool p, const m_str name, const Func_Def def) { + Func func = mp_alloc(p, Func); func->name = name; func->def = def; INIT_OO(func, free_func); diff --git a/src/parse/operator.c b/src/parse/operator.c index 500da607..9e4c13d8 100644 --- a/src/parse/operator.c +++ b/src/parse/operator.c @@ -5,6 +5,7 @@ #include "oo.h" #include "vm.h" #include "env.h" +#include "gwion.h" #include "type.h" #include "instr.h" #include "emit.h" @@ -12,7 +13,6 @@ #include "func.h" #include "nspc.h" #include "operator.h" -#include "gwion.h" typedef Type (*f_type)(const Env env, const Exp exp); @@ -25,24 +25,24 @@ typedef struct M_Operator_{ m_bool mut; } M_Operator; -ANN static void free_op(M_Operator* a, void *gwion) { +ANN static void free_op(M_Operator* a, Gwion gwion) { if(a->lhs && a->lhs != OP_ANY_TYPE) REM_REF(a->lhs, gwion) if(a->rhs && a->rhs != OP_ANY_TYPE) REM_REF(a->rhs, gwion) if(a->ret) REM_REF(a->ret, gwion) - mp_free(M_Operator, a); + mp_free(gwion->p, M_Operator, a); } -ANN void free_op_map(Map map, void *gwion) { +ANN void free_op_map(Map map, struct Gwion_ *gwion) { LOOP_OPTIM for(m_uint i = map_size(map) + 1; --i;) { const restrict Vector v = (Vector)map_at(map, (vtype)i - 1); LOOP_OPTIM for(m_uint j = vector_size(v) + 1; --j;) free_op((M_Operator*)vector_at(v, j - 1), gwion); - free_vector(v); + free_vector(gwion->p, v); } map_release(map); } @@ -78,18 +78,18 @@ ANN2(1) static M_Operator* operator_find(const Vector v, const restrict Type lhs return NULL; } -ANN m_bool add_op(const Nspc nspc, const struct Op_Import* opi) { +ANN m_bool add_op(const Gwion gwion, const Nspc nspc, const struct Op_Import* opi) { Vector v = (Vector)map_get(&nspc->info->op_map, (vtype)opi->op); M_Operator* mo; if(!v) { - v = new_vector(); + v = new_vector(gwion->p); map_set(&nspc->info->op_map, (vtype)opi->op, (vtype)v); } if((mo = operator_find(v, opi->lhs, opi->rhs))) CHECK_BB((err_msg(0, "operator '%s', for type '%s' and '%s' already imported", op2str(opi->op), opi->lhs ? opi->lhs->name : NULL, opi->rhs ? opi->rhs->name : NULL))) - mo = mp_alloc(M_Operator); + mo = mp_alloc(gwion->p, M_Operator); mo->lhs = opi->lhs; mo->rhs = opi->rhs; mo->ret = opi->ret; diff --git a/src/parse/scan0.c b/src/parse/scan0.c index 7703c7a4..2da929b1 100644 --- a/src/parse/scan0.c +++ b/src/parse/scan0.c @@ -13,8 +13,8 @@ ANN m_bool scan0_class_def(const Env env, const Class_Def class_def); ANN static Value mk_class(const Env env, const Type base) { - const Type t = type_copy(t_class); - const Value v = new_value(t, base->name); + const Type t = type_copy(env->gwion->p, t_class); + const Value v = new_value(env->gwion->p, t, base->name); t->d.base_type = base; v->owner = base->owner; SET_FLAG(v, const | ae_flag_checked); @@ -32,10 +32,10 @@ ANN m_bool scan0_stmt_fptr(const Env env, const Stmt_Fptr stmt) { GWDEBUG_EXE CHECK_BB(env_access(env, stmt->base->td->flag)) CHECK_BB(scan0_defined(env, stmt->base->xid, stmt->base->td->xid->pos)); const m_str name = s_name(stmt->base->xid); - const Type t = new_type(t_fptr->xid, name, t_fptr); + const Type t = new_type(env->gwion->p, t_fptr->xid, name, t_fptr); t->owner = !(!env->class_def && GET_FLAG(stmt->base->td, global)) ? env->curr : env->global_nspc; - t->nspc = new_nspc(name); + t->nspc = new_nspc(env->gwion->p, name); t->flag = stmt->base->td->flag; stmt->type = t; nspc_add_type(t->owner, stmt->base->xid, t); @@ -49,7 +49,7 @@ ANN static m_bool scan0_stmt_type(const Env env, const Stmt_Type stmt) { GWDEBUG CHECK_OB(base) CHECK_BB(scan0_defined(env, stmt->xid, stmt->ext->xid->pos)) if(!stmt->ext->types && (!stmt->ext->array || !stmt->ext->array->exp)) { - const Type t = new_type(++env->scope->type_xid, s_name(stmt->xid), base); + const Type t = new_type(env->gwion->p, ++env->scope->type_xid, s_name(stmt->xid), base); t->size = base->size; const Nspc nspc = (!env->class_def && GET_FLAG(stmt->ext, global)) ? env->global_nspc : env->curr; @@ -61,7 +61,7 @@ ANN static m_bool scan0_stmt_type(const Env env, const Stmt_Type stmt) { GWDEBUG SET_FLAG(t, empty); } else { const ae_flag flag = base->def ? base->def->flag : 0; - const Class_Def def = new_class_def(flag, stmt->xid, stmt->ext, NULL, stmt->ext->xid->pos); + const Class_Def def = new_class_def(env->gwion->p, flag, stmt->xid, stmt->ext, NULL, stmt->ext->xid->pos); CHECK_BB(scan0_class_def(env, def)) stmt->type = def->base.type; } @@ -79,7 +79,7 @@ ANN m_bool scan0_stmt_enum(const Env env, const Stmt_Enum stmt) { GWDEBUG_EXE s_name(stmt->xid), v->type->name) CHECK_BB(scan0_defined(env, stmt->xid, stmt->self->pos)) // test for type ? } - const Type t = type_copy(t_int); + const Type t = type_copy(env->gwion->p, t_int); t->name = stmt->xid ? s_name(stmt->xid) : "int"; t->parent = t_int; const Nspc nspc = GET_FLAG(stmt, global) ? env->global_nspc : env->curr; @@ -94,9 +94,9 @@ ANN m_bool scan0_stmt_enum(const Env env, const Stmt_Enum stmt) { GWDEBUG_EXE ANN static Type union_type(const Env env, const Nspc nspc, const Symbol s, const m_bool add) { const m_str name = s_name(s); - const Type t = type_copy(t_union); + const Type t = type_copy(env->gwion->p, t_union); t->name = name; - t->nspc = new_nspc(name); + t->nspc = new_nspc(env->gwion->p, name); t->nspc->parent = nspc; t->owner = nspc; if(add) { @@ -115,7 +115,7 @@ ANN static m_bool scan0_stmt_union(const Env env, const Stmt_Union stmt) { GWDEB env->curr : env->global_nspc; const Type t = union_type(env, nspc, stmt->type_xid ?: stmt->xid, !!stmt->type_xid); - stmt->value = new_value(t, s_name(stmt->xid)); + stmt->value = new_value(env->gwion->p, t, s_name(stmt->xid)); stmt->value->owner_class = env->class_def; stmt->value->owner = nspc; nspc_add_value(nspc, stmt->xid, stmt->value); @@ -163,9 +163,9 @@ ANN static m_bool scan0_class_def_pre(const Env env, const Class_Def class_def) } ANN static Type scan0_class_def_init(const Env env, const Class_Def class_def) { GWDEBUG_EXE - const Type t = new_type(++env->scope->type_xid, s_name(class_def->base.xid), t_object); + const Type t = new_type(env->gwion->p, ++env->scope->type_xid, s_name(class_def->base.xid), t_object); t->owner = env->curr; - t->nspc = new_nspc(t->name); + t->nspc = new_nspc(env->gwion->p, t->name); t->nspc->parent = GET_FLAG(class_def, global) ? env_nspc(env) : env->curr; t->def = class_def; t->flag = class_def->flag; diff --git a/src/parse/scan1.c b/src/parse/scan1.c index 485ebc44..8a0c53a5 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -74,7 +74,7 @@ ANN m_bool scan1_exp_decl(const Env env, Exp_Decl* decl) { GWDEBUG_EXE CHECK_BB(scan1_exp(env, var->array->exp)) t = array_type(env, decl->type, var->array->depth); } - const Value v = var->value = former ? former : new_value(t, s_name(var->xid)); + const Value v = var->value = former ? former : new_value(env->gwion->p, t, s_name(var->xid)); nspc_add_value(nspc, var->xid, v); v->flag = decl->td->flag; if(var->array && !var->array->exp) @@ -180,7 +180,7 @@ ANN m_bool scan1_stmt_enum(const Env env, const Stmt_Enum stmt) { GWDEBUG_EXE ID_List list = stmt->list; do { CHECK_BB(already_defined(env, list->xid, stmt->self->pos)) - const Value v = new_value(stmt->t, s_name(list->xid)); + const Value v = new_value(env->gwion->p, stmt->t, s_name(list->xid)); if(env->class_def) { v->owner_class = env->class_def; v->owner = env->curr; @@ -255,12 +255,12 @@ ANN static m_bool scan1_stmt_list(const Env env, Stmt_List l) { GWDEBUG_EXE Stmt_List next = l->next; l->next = l->next->next; next->next = NULL; - free_stmt_list(next); + free_stmt_list(env->gwion->p, next); } } else { Stmt_List tmp = l->next; l->next = NULL; - free_stmt_list(tmp); + free_stmt_list(env->gwion->p, tmp); } } } while((l = l->next)); diff --git a/src/parse/scan2.c b/src/parse/scan2.c index 7081b1b7..e77def4e 100644 --- a/src/parse/scan2.c +++ b/src/parse/scan2.c @@ -23,7 +23,7 @@ ANN static m_bool scan2_exp_decl_template(const Env env, const Exp_Decl* decl) { CHECK_BB(template_push_types(env, decl->base->tmpl->list.list, decl->td->types)); CHECK_BB(scan1_class_def(env, decl->type->def)) CHECK_BB(scan2_class_def(env, decl->type->def)) - nspc_pop_type(env->curr); + nspc_pop_type(env->gwion->p, env->curr); return GW_OK; } @@ -48,10 +48,10 @@ ANN m_bool scan2_exp_decl(const Env env, const Exp_Decl* decl) { GWDEBUG_EXE return GW_OK; } -ANN static Value arg_value(const Arg_List list) { +ANN static Value arg_value(MemPool p, const Arg_List list) { const Var_Decl var = list->var_decl; if(!var->value) { - const Value v = new_value(list->type, s_name(var->xid)); + const Value v = new_value(p, list->type, s_name(var->xid)); if(list->td) v->flag = list->td->flag | ae_flag_arg; return v; @@ -66,7 +66,7 @@ ANN static m_bool scan2_args(const Env env, const Func_Def f) { GWDEBUG_EXE const Var_Decl var = list->var_decl; if(var->array) list->type = array_type(env, list->type, var->array->depth); - var->value = arg_value(list); + var->value = arg_value(env->gwion->p, list); var->value->offset = f->stack_depth; f->stack_depth += list->type->size; } while((list = list->next)); @@ -90,9 +90,9 @@ ANN static Value scan2_func_assign(const Env env, const Func_Def d, } ANN m_bool scan2_stmt_fptr(const Env env, const Stmt_Fptr ptr) { GWDEBUG_EXE - const Func_Def def = new_func_def(new_func_base(ptr->base->td, ptr->base->xid, ptr->base->args), NULL, ptr->base->td->flag); + const Func_Def def = new_func_def(env->gwion->p, new_func_base(env->gwion->p, ptr->base->td, ptr->base->xid, ptr->base->args), NULL, ptr->base->td->flag); def->base->ret_type = ptr->base->ret_type; - ptr->base->func = new_func(s_name(ptr->base->xid), def); + ptr->base->func = new_func(env->gwion->p, s_name(ptr->base->xid), def); ptr->value->d.func_ref = ptr->base->func; ptr->base->func->value_ref = ptr->value; ptr->type->d.func = ptr->base->func; @@ -250,7 +250,7 @@ ANN static Map scan2_label_map(const Env env) { GWDEBUG_EXE if(!label->ptr) map_init(label); if(!(m = (Map)map_get(label, (vtype)key))) { - m = new_map(); + m = new_map(env->gwion->p); map_set(label, (vtype)key, (vtype)m); } return m; @@ -309,7 +309,7 @@ ANN static m_bool scan2_func_def_overload(const Func_Def f, const Value overload } ANN static Func scan_new_func(const Env env, const Func_Def f, const m_str name) { - const Func func = new_func(name, f); + const Func func = new_func(env->gwion->p, name, f); if(env->class_def) { if(GET_FLAG(env->class_def, template)) SET_FLAG(func, ref); @@ -320,7 +320,7 @@ ANN static Func scan_new_func(const Env env, const Func_Def f, const m_str name) } ANN static Type func_type(const Env env, const Func func) { - const Type t = type_copy(t_function); + const Type t = type_copy(env->gwion->p, t_function); t->name = func->name; t->owner = env->curr; if(GET_FLAG(func, member)) @@ -332,7 +332,7 @@ ANN static Type func_type(const Env env, const Func func) { ANN2(1,2) static Value func_value(const Env env, const Func f, const Value overload) { const Type t = func_type(env, f); - const Value v = new_value(t, t->name); + const Value v = new_value(env->gwion->p, t, t->name); CHECK_OO(scan2_func_assign(env, f->def, f, v)) if(!overload) { ADD_REF(v); @@ -389,9 +389,9 @@ ANN2(1, 2) static m_bool scan2_func_def_template(const Env env, const Func_Def f return GW_OK; } -ANN static m_bool scan2_func_def_builtin(const Func func, const m_str name) { GWDEBUG_EXE +ANN static m_bool scan2_func_def_builtin(MemPool p, const Func func, const m_str name) { GWDEBUG_EXE SET_FLAG(func, builtin); - func->code = new_vm_code(NULL, func->def->stack_depth, func->flag, name); + func->code = new_vm_code(p, NULL, func->def->stack_depth, func->flag, name); func->code->native_func = (m_uint)func->def->d.dl_func_ptr; return GW_OK; } @@ -467,7 +467,7 @@ ANN2(1,2,4) static Value func_create(const Env env, const Func_Def f, const Value v = func_value(env, func, overload); scan2_func_def_flag(env, f); if(GET_FLAG(f, builtin)) - CHECK_BO(scan2_func_def_builtin(func, func->name)) + CHECK_BO(scan2_func_def_builtin(env->gwion->p, func, func->name)) if(GET_FLAG(func, member)) f->stack_depth += SZ_INT; if(GET_FLAG(func->def, variadic)) diff --git a/src/parse/template.c b/src/parse/template.c index ed1f7c03..51581e22 100644 --- a/src/parse/template.c +++ b/src/parse/template.c @@ -9,6 +9,7 @@ #include "template.h" #include "vm.h" #include "parse.h" +#include "gwion.h" ANN static inline Type owner_type(const Env env, const Type t) { const Nspc nspc = t->nspc ? t->nspc->parent : NULL; @@ -16,18 +17,18 @@ ANN static inline Type owner_type(const Env env, const Type t) { } ANEW ANN static Vector get_types(const Env env, Type t) { - const Vector v = new_vector(); + const Vector v = new_vector(env->gwion->p); do if(GET_FLAG(t, template)) vector_add(v, (vtype)t->def->tmpl->list.list); while((t = owner_type(env, t))); return v; } -ANEW ANN static ID_List id_list_copy(ID_List src) { - const ID_List list = new_id_list(src->xid, src->pos); +ANEW ANN static ID_List id_list_copy(MemPool p, ID_List src) { + const ID_List list = new_id_list(p, src->xid, src->pos); ID_List tmp = list; while((src = src->next)) - tmp = (tmp->next = new_id_list(src->xid, src->pos)); + tmp = (tmp->next = new_id_list(p, src->xid, src->pos)); return list; } @@ -38,15 +39,15 @@ ANN static ID_List get_total_type_list(const Env env, const Type t) { const Vector v = get_types(env, parent); const ID_List base = (ID_List)vector_pop(v); if(!base) { - free_vector(v); + free_vector(env->gwion->p, v); return t->def->tmpl ? t->def->tmpl->list.list : NULL; } - const ID_List types = id_list_copy(base); + const ID_List types = id_list_copy(env->gwion->p, base); ID_List list, tmp = types; while((list = (ID_List)vector_pop(v))) - tmp = (tmp->next = id_list_copy(list)); + tmp = (tmp->next = id_list_copy(env->gwion->p, list)); tmp->next = t->def->tmpl->list.list; - free_vector(v); + free_vector(env->gwion->p, v); return types; } @@ -113,12 +114,12 @@ ANN m_bool template_match(ID_List base, Type_List call) { ANN static Class_Def template_class(const Env env, const Class_Def def, const Type_List call) { const Symbol name = template_id(env, def, call); const Type t = nspc_lookup_type1(env->curr, name); - return t ? t->def : new_class_def(def->flag, name, def->base.ext, def->body, def->pos); + return t ? t->def : new_class_def(env->gwion->p, def->flag, name, def->base.ext, def->body, def->pos); } ANN m_bool template_push_types(const Env env, ID_List base, Type_List tl) { Type_List call = tl; - nspc_push_type(env->curr); + nspc_push_type(env->gwion->p, env->curr); do { const Type t = call ? known_type(env, call->td) : NULL; if(!t) @@ -156,13 +157,13 @@ ANN Type scan_type(const Env env, const Type t, const Type_Decl* type) { if(GET_FLAG(t, builtin)) SET_FLAG(a->base.type, builtin); CHECK_BO(scan1_class_def(env, a)) - nspc_pop_type(env->curr); + nspc_pop_type(env->gwion->p, env->curr); if(t->nspc->dtor) { a->base.type->nspc->dtor = t->nspc->dtor; SET_FLAG(a->base.type, dtor); ADD_REF(t->nspc->dtor) } - a->tmpl = new_tmpl_class(get_total_type_list(env, t), 0); + a->tmpl = new_tmpl_class(env->gwion->p, get_total_type_list(env, t), 0); a->tmpl->base = type->types; nspc_add_type(t->owner, insert_symbol(a->base.type->name), a->base.type); return a->base.type; diff --git a/src/parse/traverse_template.c b/src/parse/traverse_template.c index 3e3b2bc5..85447e0d 100644 --- a/src/parse/traverse_template.c +++ b/src/parse/traverse_template.c @@ -5,6 +5,8 @@ #include "nspc.h" #include "traverse.h" #include "template.h" +#include "vm.h" +#include "gwion.h" ANN m_bool traverse_template(const Env env, const Class_Def def) { CHECK_BB(template_push_types(env, def->tmpl->list.list, def->tmpl->base)) diff --git a/src/plug.c b/src/plug.c index 22cf1ef5..dc442002 100644 --- a/src/plug.c +++ b/src/plug.c @@ -8,13 +8,13 @@ #include "gwion_util.h" #include "gwion_ast.h" #include "oo.h" -#include "vm.h" #include "env.h" +#include "vm.h" +#include "gwion.h" #include "operator.h" #include "instr.h" #include "object.h" #include "import.h" -#include "gwion.h" typedef m_bool (*import)(Gwi); typedef m_str (*modstr)(void); @@ -44,36 +44,36 @@ struct Plug_ { #define DLERROR() "plugin" #endif -ANN static void plug_get(PlugInfo* p, const m_str c) { +ANN static void plug_get(MemPool p, PlugInfo* pi, const m_str c) { void* dl = DLOPEN(c, RTLD_LAZY | RTLD_GLOBAL); if(dl) { - vector_add(&p->vec[GWPLUG_DL], (vtype)dl); + vector_add(&pi->vec[GWPLUG_DL], (vtype)dl); const import imp = DLSYM(dl, import, GWIMPORT_NAME); if(imp) - vector_add(&p->vec[GWPLUG_IMPORT], (vtype)imp); + vector_add(&pi->vec[GWPLUG_IMPORT], (vtype)imp); const modini ini = DLSYM(dl, modini, GWMODINI_NAME); if(ini) { - struct Plug_ *plug = mp_alloc(Plug); + struct Plug_ *plug = mp_alloc(p, Plug); plug->ini = ini; const modstr str = DLSYM(dl, modstr, GWMODSTR_NAME); plug->name = str(); plug->end = DLSYM(dl, modend, GWMODEND_NAME); - vector_add(&p->vec[GWPLUG_MODULE], (vtype)plug); + vector_add(&pi->vec[GWPLUG_MODULE], (vtype)plug); } const f_bbqset drv = DLSYM(dl, f_bbqset, GWDRIVER_NAME); if(drv) { const modstr str = DLSYM(dl, modstr, GWMODSTR_NAME); - map_set(&p->drv, (vtype)str(), (vtype)drv); + map_set(&pi->drv, (vtype)str(), (vtype)drv); } } else err_msg(0, "error in %s.", DLERROR()); } -ANN PlugInfo* new_plug(const Vector list) { - PlugInfo *p = (PlugInfo*)mp_alloc(PlugInfo); +ANN PlugInfo* new_plug(MemPool p, const Vector list) { + PlugInfo *pi = (PlugInfo*)mp_alloc(p, PlugInfo); for(m_uint i = 0; i < GWPLUG_LAST; ++i) - vector_init(&p->vec[i]); - map_init(&p->drv); + vector_init(&pi->vec[i]); + map_init(&pi->drv); for(m_uint i = 0; i < vector_size(list); i++) { const m_str dir = (m_str)vector_at(list, i); char gname[strlen(dir) + 6]; @@ -84,20 +84,21 @@ ANN PlugInfo* new_plug(const Vector list) { if(glob(gname, 0, NULL, &results)) continue; for(m_uint i = 0; i < results.gl_pathc; i++) - plug_get(p, results.gl_pathv[i]); + plug_get(p, pi, results.gl_pathv[i]); globfree(& results); #else WIN32_FIND_DATA filedata; HANDLE file = FindFirstFileA(gname,&filedata); if(file == INVALID_HANDLE_VALUE) continue; - do plug_get(p, filedata.cFileName); + do plug_get(p, pi, filedata.cFileName); while(FindNextFile(file,&filedata) == 0); FindClose(file); #endif } - return p; + return pi; } + void free_plug(const Gwion gwion) { PlugInfo *p = gwion->plug; struct Vector_ * const v = p->vec; @@ -105,20 +106,20 @@ void free_plug(const Gwion gwion) { struct Plug_ *plug = (struct Plug_*)vector_at(&v[GWPLUG_MODULE], i); if(plug->end) plug->end(gwion, plug->self); - mp_free(Plug, plug); + mp_free(gwion->p, Plug, plug); } for(m_uint i = 0; i < vector_size(&v[GWPLUG_DL]); ++i) DLCLOSE((void*)vector_at(&v[GWPLUG_DL], i)); for(m_uint i = 0; i < GWPLUG_LAST; ++i) vector_release(&v[i]); map_release(&p->drv); - mp_free(PlugInfo, p); + mp_free(gwion->p, PlugInfo, p); } -ANN Vector split_args(const m_str str) { +ANN Vector split_args(MemPool p, const m_str str) { const m_str arg = strchr(str, '='); m_str d = strdup(arg+1), c = d; - const Vector args = new_vector(); + const Vector args = new_vector(p); while(d) vector_add(args, (vtype)strdup(strsep(&d, ","))); free(d); @@ -126,13 +127,13 @@ ANN Vector split_args(const m_str str) { return args; } -ANN static Vector get_arg(const m_str name, const Vector v) { +ANN static Vector get_arg(MemPool p, const m_str name, const Vector v) { const size_t len = strlen(name); for(m_uint i = vector_size(v) + 1; --i;) { const m_str str = (m_str)vector_at(v, i - 1); if(!strncmp(name, str, len)) { vector_rem(v, i-1); - return split_args(str); + return split_args(p, str); } } return NULL; @@ -142,12 +143,12 @@ void plug_run(const Gwion gwion, const Vector args) { const Vector v = &gwion->plug->vec[GWPLUG_MODULE]; for(m_uint i = 0; i < vector_size(v); ++i) { struct Plug_ *plug = (struct Plug_*)vector_at(v, i); - const Vector arg = get_arg(plug->name, args); + const Vector arg = get_arg(gwion->p, plug->name, args); plug->self = plug->ini(gwion, arg); if(arg) { for(m_uint i = 0; i < vector_size(arg); ++i) xfree((m_str)vector_at(arg, i)); - free_vector(arg); + free_vector(gwion->p, arg); } } } diff --git a/src/soundinfo.c b/src/soundinfo.c index ef8dee6d..0873d842 100644 --- a/src/soundinfo.c +++ b/src/soundinfo.c @@ -1,15 +1,15 @@ #include #include -struct SoundInfo_ *new_soundinfo(void) { - struct SoundInfo_ *si = mp_alloc(SoundInfo); +struct SoundInfo_ *new_soundinfo(MemPool p) { + struct SoundInfo_ *si = mp_alloc(p, SoundInfo); si->in = si->out = 2; si->sr = 48000; return si; } -struct SoundInfo_ *soundinfo_cpy(const struct SoundInfo_ *src) { - struct SoundInfo_ *si = mp_alloc(SoundInfo); +struct SoundInfo_ *soundinfo_cpy(MemPool p, const struct SoundInfo_ *src) { + struct SoundInfo_ *si = mp_alloc(p, SoundInfo); si->in = src->in; si->out = src->out; si->sr = src->sr; diff --git a/src/vm/driver.c b/src/vm/driver.c index e025b7cb..b91a1a4e 100644 --- a/src/vm/driver.c +++ b/src/vm/driver.c @@ -7,13 +7,15 @@ #include "gwion_ast.h" #include "oo.h" #include "vm.h" +#include "env.h" +#include "gwion.h" #include "driver.h" -ANN Driver* new_driver(void) { - Driver* di = (Driver*)mp_alloc(BBQ); +ANN Driver* new_driver(MemPool p) { + Driver* di = (Driver*)mp_alloc(p, BBQ); di->func = dummy_driver; di->run = vm_run; - di->driver = (DriverData*)mp_alloc(DriverData); + di->driver = (DriverData*)mp_alloc(p, DriverData); di->is_running = 1; return di; } @@ -23,11 +25,11 @@ ANN void free_driver(Driver *d, VM *vm) { xfree(d->in); if(d->out) xfree(d->out); - mp_free(SoundInfo, d->si); + mp_free(vm->gwion->p, SoundInfo, d->si); if(d->driver->del) d->driver->del(vm, d); - mp_free(DriverData, d->driver); - mp_free(BBQ, d); + mp_free(vm->gwion->p, DriverData, d->driver); + mp_free(vm->gwion->p, BBQ, d); } ANN void driver_alloc(Driver *d) { diff --git a/src/vm/shreduler.c b/src/vm/shreduler.c index 68a93bf8..1655f160 100644 --- a/src/vm/shreduler.c +++ b/src/vm/shreduler.c @@ -112,6 +112,8 @@ ANN void shredule(const Shreduler s, const VM_Shred shred, const m_float wake_ti } ANN void shreduler_add(const Shreduler s, const VM_Shred shred) { +// create shred->tick here ? + shred->tick->shreduler = s; shred->tick->xid = ++s->shred_ids; vector_add(&s->shreds, (vtype)shred); } diff --git a/src/vm/vm.c b/src/vm/vm.c index 1e6d3ba2..9b6a9f25 100644 --- a/src/vm/vm.c +++ b/src/vm/vm.c @@ -49,11 +49,11 @@ uint32_t gw_rand(uint32_t s[2]) { return ret; } -VM* new_vm(void) { - VM* vm = (VM*)mp_alloc(VM); +VM* new_vm(MemPool p) { + VM* vm = (VM*)mp_alloc(p, VM); vector_init(&vm->ugen); - vm->bbq = new_driver(); - vm->shreduler = (Shreduler)mp_alloc(Shreduler); + vm->bbq = new_driver(p); + vm->shreduler = (Shreduler)mp_alloc(p, Shreduler); vector_init(&vm->shreduler->shreds); vm->shreduler->bbq = vm->bbq; gw_seed(vm->rand, (uint64_t)time(NULL)); @@ -75,8 +75,8 @@ ANN void free_vm(VM* vm) { vector_release(&vm->ugen); if(vm->bbq) free_driver(vm->bbq, vm); - mp_free(Shreduler, vm->shreduler); - mp_free(VM, vm); + mp_free(vm->gwion->p, Shreduler, vm->shreduler); + mp_free(vm->gwion->p, VM, vm); } ANN m_uint vm_add_shred(const VM* vm, const VM_Shred shred) { @@ -136,7 +136,7 @@ ANN static inline m_bool overflow_(const m_bit* mem, const VM_Shred c) { ANN static inline VM_Shred init_spork_shred(const VM_Shred shred, const Instr instr) { const VM_Code code = (VM_Code)instr->m_val; - const VM_Shred sh = new_vm_shred(code); + const VM_Shred sh = new_vm_shred(shred->info->mp, code); ADD_REF(code) sh->tick->parent = shred->tick; if(!shred->tick->child.ptr) @@ -149,7 +149,7 @@ ANN static inline VM_Shred init_spork_shred(const VM_Shred shred, const Instr in ANN static inline VM_Shred init_fork_shred(const VM_Shred shred, const Instr instr) { const VM_Code code = (VM_Code)instr->m_val; - const VM_Shred sh = new_vm_shred(code); + const VM_Shred sh = new_vm_shred(shred->info->mp, code); ADD_REF(code) sh->base = shred->base; vm_fork(shred->info->vm, sh); @@ -238,6 +238,7 @@ __attribute__((hot)) (m_int)(*(m_float*)(reg-SZ_INT))); \ DISPATCH() +__attribute__ ((optimize("-O2"))) ANN void vm_run(const VM* vm) { // lgtm [cpp/use-of-goto] static const void* dispatch[] = { &®setimm, @@ -701,7 +702,7 @@ arrayvalid: array_base = NULL; goto regpush; newobj: - *(M_Object*)reg = new_object(shred, (Type)instr->m_val2); + *(M_Object*)reg = new_object(vm->gwion->p, shred, (Type)instr->m_val2); reg += SZ_INT; DISPATCH() addref: @@ -778,7 +779,7 @@ staticcode: reg += SZ_INT; DISPATCH() pushstr: - *(M_Object*)reg = new_string2(shred, (m_str)instr->m_val); + *(M_Object*)reg = new_string2(vm->gwion->p, shred, (m_str)instr->m_val); reg += SZ_INT; DISPATCH(); gcini: @@ -823,3 +824,4 @@ timespecadd(&exec_time, &exec_ret, &exec_time); } vm_ugen_init(vm); } +//#pragma GCC pop_options \ No newline at end of file diff --git a/src/vm/vm_code.c b/src/vm/vm_code.c index dfb72d42..8760769d 100644 --- a/src/vm/vm_code.c +++ b/src/vm/vm_code.c @@ -21,26 +21,26 @@ ANN static void free_code_instr(const Vector v, const Gwion gwion) { map_get(&gwion->freearg, (vtype)instr->execute)); if(f) f(instr, gwion); - mp_free(Instr, instr); + mp_free(gwion->p, Instr, instr); } - free_vector(v); + free_vector(gwion->p, v); } -ANN static void free_vm_code(VM_Code a, void *gwion) { +ANN static void free_vm_code(VM_Code a, Gwion gwion) { #ifndef NOMEMOIZE if(a->memoize) - memoize_end(a->memoize); + memoize_end(gwion->p, a->memoize); #endif if(!GET_FLAG(a, builtin)) free_code_instr(a->instr, gwion); xfree(a->name); - mp_free(VM_Code, a); + mp_free(gwion->p , VM_Code, a); } -VM_Code new_vm_code(const Vector instr, const m_uint stack_depth, +VM_Code new_vm_code(MemPool p, const Vector instr, const m_uint stack_depth, const ae_flag flag, const m_str name) { - VM_Code code = mp_alloc(VM_Code); - code->instr = instr ? vector_copy(instr) : NULL; + VM_Code code = mp_alloc(p, VM_Code); + code->instr = instr ? vector_copy(p, instr) : NULL; code->name = strdup(name); code->stack_depth = stack_depth; code->flag = flag; diff --git a/src/vm/vm_shred.c b/src/vm/vm_shred.c index 1ea451fd..6b8ec5f7 100644 --- a/src/vm/vm_shred.c +++ b/src/vm/vm_shred.c @@ -3,8 +3,10 @@ #include "gwion_util.h" #include "gwion_ast.h" #include "oo.h" +#include "env.h" #include "vm.h" #include "object.h" +#include "gwion.h" struct Stack_ { VM_Shred shred; @@ -12,32 +14,33 @@ struct Stack_ { char d[SIZEOF_MEM]; }; -static inline struct ShredInfo_ *new_shredinfo(const m_str name) { - struct ShredInfo_ *info = mp_alloc(ShredInfo); +static inline struct ShredInfo_ *new_shredinfo(MemPool p, const m_str name) { + struct ShredInfo_ *info = mp_alloc(p, ShredInfo); + info->mp = p; info->name = strdup(name); return info; } -static inline void free_shredinfo(struct ShredInfo_ *info) { +static inline void free_shredinfo(MemPool mp, struct ShredInfo_ *info) { free(info->name); if(info->args) { const Vector v = info->args; LOOP_OPTIM for(m_uint i = vector_size(v) + 1; --i;) free((void*)vector_at(v, i - 1)); - free_vector(v); + free_vector(mp, v); } - mp_free(ShredInfo, info); + mp_free(mp, ShredInfo, info); // ??? info->p } -VM_Shred new_vm_shred(VM_Code c) { - const VM_Shred shred = mp_alloc(Stack); +VM_Shred new_vm_shred(MemPool p, VM_Code c) { + const VM_Shred shred = mp_alloc(p, Stack); shred->code = c; shred->reg = (m_bit*)shred + sizeof(struct VM_Shred_); shred->base = shred->mem = shred->reg + SIZEOF_REG; - shred->tick = mp_alloc(ShredTick); + shred->tick = mp_alloc(p, ShredTick); shred->tick->self = shred; - shred->info = new_shredinfo(c->name); + shred->info = new_shredinfo(p, c->name); vector_init(&shred->gc); return shred; } @@ -47,7 +50,8 @@ void free_vm_shred(VM_Shred shred) { release((M_Object)vector_at(&shred->gc, i - 1), shred); vector_release(&shred->gc); REM_REF(shred->code, shred->info->vm->gwion); - mp_free(ShredTick, shred->tick); - free_shredinfo(shred->info); - mp_free(Stack, shred); + MemPool mp = shred->info->mp; + mp_free(mp, ShredTick, shred->tick); + free_shredinfo(mp, shred->info); + mp_free(mp, Stack, shred); } \ No newline at end of file diff --git a/tests/import/callback.c b/tests/import/callback.c index 5df0a49b..d7725b18 100644 --- a/tests/import/callback.c +++ b/tests/import/callback.c @@ -43,7 +43,7 @@ static SFUN(cb_func) { } m_uint offset = shred->mem - ((m_bit*)shred + sizeof(struct VM_Shred_) + SIZEOF_REG); PUSH_MEM(shred, offset); - Instr instr = mp_alloc(Instr); + Instr instr = mp_alloc(shred->info->mp, Instr); struct ret_info* info = (struct ret_info*)xmalloc(sizeof(struct ret_info)); info->offset = offset; info->code = shred->code; diff --git a/tests/import/end_class.c b/tests/import/end_class.c index 8961e034..87a91353 100644 --- a/tests/import/end_class.c +++ b/tests/import/end_class.c @@ -4,6 +4,7 @@ #include "oo.h" #include "vm.h" #include "env.h" +#include "gwion.h" #include "operator.h" #include "object.h" #include "instr.h" diff --git a/tests/import/extend_array.c b/tests/import/extend_array.c index e6d79e61..7e124090 100644 --- a/tests/import/extend_array.c +++ b/tests/import/extend_array.c @@ -16,9 +16,9 @@ GWION_IMPORT(extend_array_test) { Type t_array_ext; CHECK_OB((t_array_ext = gwi_mk_type(gwi, "ArrayExt", SZ_INT, NULL))) CHECK_BB(gwi_class_ini(gwi, t_array_ext, NULL, NULL)) - Type_Decl* td = new_type_decl(new_id_list(insert_symbol(gwi->gwion->st, "float"), 0), 0); - Exp e = new_exp_prim_int(1, 0); - Array_Sub array = new_array_sub(e); + Type_Decl* td = new_type_decl(gwi->gwion->st->p, new_id_list(gwi->gwion->st->p, insert_symbol(gwi->gwion->st, "float"), 0), 0); + Exp e = new_exp_prim_int(gwi->gwion->st->p, 1, 0); + Array_Sub array = new_array_sub(gwi->gwion->st->p, e); add_type_decl_array(td, array); CHECK_BB(gwi_class_ext(gwi, td)) CHECK_BB(gwi_class_end(gwi)) diff --git a/tests/import/extend_event.c b/tests/import/extend_event.c index 3d004788..dc38469a 100644 --- a/tests/import/extend_event.c +++ b/tests/import/extend_event.c @@ -18,7 +18,7 @@ GWION_IMPORT(extend_event_test) { Type t_ev ; CHECK_OB((t_ev = gwi_mk_type(gwi, "Ev", SZ_INT , NULL))) CHECK_BB(gwi_class_ini(gwi, t_ev, ev_ctor, NULL)) - Type_Decl* td = new_type_decl(new_id_list(insert_symbol(gwi->gwion->st, "Event"), 0), 0); + Type_Decl* td = new_type_decl(gwi->gwion->st->p, new_id_list(gwi->gwion->st->p, insert_symbol(gwi->gwion->st, "Event"), 0), 0); CHECK_BB(gwi_class_ext(gwi, td)) CHECK_BB(gwi_class_end(gwi)) return GW_OK; diff --git a/tests/import/extend_pair.c b/tests/import/extend_pair.c index bd1f94f9..801841cd 100644 --- a/tests/import/extend_pair.c +++ b/tests/import/extend_pair.c @@ -19,11 +19,11 @@ GWION_IMPORT(extend_pair_test) { CHECK_BB(gwi_tmpl_ini(gwi, 2, types)) CHECK_BB(gwi_class_ini(gwi, t_pair_ext, NULL, NULL)) CHECK_BB(gwi_tmpl_end(gwi)) - Type_Decl* td = new_type_decl(new_id_list(insert_symbol(gwi->gwion->st, "Pair"), 0), 0); - Type_Decl* td0 = new_type_decl(new_id_list(insert_symbol(gwi->gwion->st, "A"), 0), 0); - Type_Decl* td1 = new_type_decl(new_id_list(insert_symbol(gwi->gwion->st, "B"), 0), 0); - Type_List tl1 = new_type_list(td1, NULL); - Type_List tl0 = new_type_list(td0, tl1); + Type_Decl* td = new_type_decl(gwi->gwion->st->p, new_id_list(gwi->gwion->st->p, insert_symbol(gwi->gwion->st, "Pair"), 0), 0); + Type_Decl* td0 = new_type_decl(gwi->gwion->st->p ,new_id_list(gwi->gwion->st->p, insert_symbol(gwi->gwion->st, "A"), 0), 0); + Type_Decl* td1 = new_type_decl(gwi->gwion->st->p ,new_id_list(gwi->gwion->st->p, insert_symbol(gwi->gwion->st, "B"), 0), 0); + Type_List tl1 = new_type_list(gwi->gwion->st->p, td1, NULL); + Type_List tl0 = new_type_list(gwi->gwion->st->p,td0, tl1); td->types = tl0; CHECK_BB(gwi_class_ext(gwi, td)) CHECK_BB(gwi_class_end(gwi)) diff --git a/tests/import/global_var.c b/tests/import/global_var.c index 3f46664d..7842bcac 100644 --- a/tests/import/global_var.c +++ b/tests/import/global_var.c @@ -7,11 +7,13 @@ #include "object.h" #include "instr.h" #include "import.h" +#include "gwi.h" +#include "gwion.h" GWION_IMPORT(global_var_test) { // ALLOC_PTR(i, m_uint, 1); - M_Object i = new_string(NULL, "test"); + M_Object i = new_string(gwi->gwion->p, NULL, "test"); CHECK_BB(gwi_item_ini(gwi,"string", "i")) CHECK_BB(gwi_item_end(gwi, 0, i)) return GW_OK; diff --git a/tests/import/static_string.c b/tests/import/static_string.c index 63befdf8..5a180c11 100644 --- a/tests/import/static_string.c +++ b/tests/import/static_string.c @@ -8,10 +8,12 @@ #include "object.h" #include "instr.h" #include "import.h" +#include "gwion.h" +#include "gwi.h" GWION_IMPORT(static_string_test) { CHECK_BB(gwi_item_ini(gwi, "string", "self")) - M_Object obj = new_string(NULL, "test static string"); + M_Object obj = new_string(gwi->gwion->p, NULL, "test static string"); CHECK_BB(gwi_item_end(gwi, ae_flag_global, obj)) return GW_OK; } diff --git a/tests/import/variadic.c b/tests/import/variadic.c index edbb6bf4..fa9b3cf4 100644 --- a/tests/import/variadic.c +++ b/tests/import/variadic.c @@ -33,7 +33,7 @@ static MFUN(m_variadic) { arg->i++; str++; } - free_vararg(arg); + free_vararg(shred->info->mp, arg); } GWION_IMPORT(variadic test) { diff --git a/util b/util index f3804752..30f6ebb7 160000 --- a/util +++ b/util @@ -1 +1 @@ -Subproject commit f3804752bee3bb6e080b1085dcf0e63822751a15 +Subproject commit 30f6ebb77737c1542c8eb677bf2b4d6acc0d166a -- 2.43.0