From ae2d0ac56fdddde665e6aaff39cccf7c02686a24 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Sun, 13 Jun 2021 16:45:01 +0200 Subject: [PATCH] :wrench: more format --- .clang-format | 149 ++ Makefile | 5 + ast | 2 +- include/arg.h | 22 +- include/array.h | 19 +- include/clean.h | 2 +- include/compile.h | 7 +- include/driver.h | 42 +- include/emit.h | 74 +- include/env/context.h | 14 +- include/env/env.h | 87 +- include/env/envset.h | 28 +- include/env/func.h | 34 +- include/env/nspc.h | 85 +- include/env/oo.h | 20 +- include/env/trait.h | 8 +- include/env/tuple.h | 4 +- include/env/type.h | 118 +- include/env/value.h | 50 +- include/escape.h | 4 +- include/gack.h | 11 +- include/gwi.h | 38 +- include/gwion.h | 36 +- include/gwiondata.h | 24 +- include/import.h | 75 +- include/import/cdef.h | 7 +- include/import/checker.h | 66 +- include/import/edef.h | 4 +- include/import/fdef.h | 14 +- include/import/internals.h | 44 +- include/import/item.h | 5 +- include/import/oper.h | 21 +- include/import/special.h | 6 +- include/import/tdef.h | 7 +- include/import/type.h | 8 +- include/import/udef.h | 7 +- include/instr.h | 35 +- include/match.h | 25 +- include/memoize.h | 6 +- include/object.h | 83 +- include/opcode.h | 398 ++--- include/operator.h | 93 +- include/parse.h | 122 +- include/pass.h | 10 +- include/plug.h | 34 +- include/shreduler_private.h | 12 +- include/soundinfo.h | 4 +- include/specialid.h | 27 +- include/template.h | 10 +- include/tmp_resolve.h | 3 +- include/tmpl_info.h | 4 +- include/traverse.h | 4 +- include/ugen.h | 31 +- include/vararg.h | 10 +- include/vm.h | 150 +- src/arg.c | 408 +++--- src/clean.c | 192 +-- src/compile.c | 118 +- src/emit/emit.c | 2121 ++++++++++++++------------- src/emit/emitter.c | 17 +- src/emit/escape.c | 66 +- src/emit/memoize.c | 38 +- src/env/context.c | 12 +- src/env/env.c | 50 +- src/env/env_utils.c | 74 +- src/env/envset.c | 64 +- src/env/func.c | 37 +- src/env/nspc.c | 96 +- src/env/trait.c | 8 +- src/env/tupleform.c | 23 +- src/env/type.c | 129 +- src/env/value.c | 16 +- src/gwion.c | 99 +- src/gwiondata.c | 21 +- src/import/cleaner.c | 24 +- src/import/import_cdef.c | 97 +- src/import/import_checker.c | 205 ++- src/import/import_enum.c | 53 +- src/import/import_fdef.c | 81 +- src/import/import_internals.c | 24 +- src/import/import_item.c | 33 +- src/import/import_oper.c | 73 +- src/import/import_special.c | 30 +- src/import/import_tdef.c | 26 +- src/import/import_type.c | 25 +- src/import/import_udef.c | 56 +- src/lib/array.c | 822 ++++++----- src/lib/engine.c | 72 +- src/lib/event.c | 22 +- src/lib/instr.c | 126 +- src/lib/lib_func.c | 502 ++++--- src/lib/modules.c | 160 +- src/lib/object.c | 77 +- src/lib/object_op.c | 308 ++-- src/lib/opfunc.c | 100 +- src/lib/prim.c | 478 +++--- src/lib/prim_values.c | 8 +- src/lib/ptr.c | 118 +- src/lib/ref.c | 49 +- src/lib/shred.c | 246 ++-- src/lib/string.c | 415 +++--- src/lib/tmpl_info.c | 53 +- src/lib/ugen.c | 429 +++--- src/lib/union.c | 139 +- src/lib/vararg.c | 155 +- src/main.c | 20 +- src/parse/check.c | 1505 ++++++++++--------- src/parse/check_traits.c | 107 +- src/parse/compat_func.c | 11 +- src/parse/did_you_mean.c | 56 +- src/parse/func_operator.c | 18 +- src/parse/func_resolve_tmpl.c | 182 +-- src/parse/operator.c | 336 ++--- src/parse/scan0.c | 380 +++-- src/parse/scan1.c | 500 ++++--- src/parse/scan2.c | 424 +++--- src/parse/scanx.c | 47 +- src/parse/template.c | 135 +- src/parse/traverse.c | 35 +- src/parse/type_decl.c | 68 +- src/pass.c | 32 +- src/plug.c | 155 +- src/soundinfo.c | 8 +- src/vm/closure.c | 9 +- src/vm/driver.c | 29 +- src/vm/gack.c | 31 +- src/vm/shreduler.c | 74 +- src/vm/vm.c | 1885 ++++++++++++------------ src/vm/vm_code.c | 163 +- src/vm/vm_name.c | 4 +- src/vm/vm_shred.c | 31 +- tests/driver/simple_driver.c | 6 +- tests/module/basic_module.c | 8 +- tests/module/get_module.c | 6 +- tests/module/module.c | 4 +- tests/plug/array.c | 2 +- tests/plug/callback.c | 62 +- tests/plug/class_template.c | 26 +- tests/plug/class_template_invalid.c | 16 +- tests/plug/compile_file.c | 6 +- tests/plug/compile_string.c | 3 +- tests/plug/coverage.c | 14 +- tests/plug/deps.c | 2 +- tests/plug/end_class.c | 2 +- tests/plug/enum.c | 88 +- tests/plug/enum_fail.c | 2 +- tests/plug/enum_fail2.c | 2 +- tests/plug/enum_fail3.c | 2 +- tests/plug/fptr.c | 12 +- tests/plug/fptr_tmpl.c | 2 +- tests/plug/func_tmpl.c | 4 +- tests/plug/func_tmpl_fail.c | 4 +- tests/plug/global_func.c | 2 +- tests/plug/global_var.c | 8 +- tests/plug/invalid_arg.c | 2 +- tests/plug/invalid_array.c | 20 +- tests/plug/invalid_func.c | 2 +- tests/plug/invalid_type1.c | 2 +- tests/plug/invalid_type2.c | 2 +- tests/plug/invalid_type3.c | 2 +- tests/plug/mk_type_array.c | 5 +- tests/plug/pass.c | 4 +- tests/plug/specialid_emit.c | 6 +- tests/plug/static_string.c | 3 +- tests/plug/str2decl.c | 4 +- tests/plug/str2list_fail.c | 3 +- tests/plug/struct.c | 12 +- tests/plug/template_arg.c | 2 +- tests/plug/trig.c | 9 +- tests/plug/union.c | 4 +- tests/plug/union_fail_exp.c | 4 +- tests/plug/union_member.c | 8 +- tests/plug/union_name.c | 4 +- tests/plug/union_tmpl.c | 6 +- tests/plug/union_tmpl_fail.c | 6 +- tests/plug/union_tmpl_fail2.c | 6 +- tests/plug/union_tmpl_fail3.c | 6 +- tests/plug/variadic.c | 38 +- tests/plug/vm_remove.c | 4 +- util | 2 +- 180 files changed, 8981 insertions(+), 8516 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..9e30bfc9 --- /dev/null +++ b/.clang-format @@ -0,0 +1,149 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignConsecutiveMacros: true +AlignConsecutiveAssignments: true +AlignConsecutiveBitFields: true +AlignConsecutiveDeclarations: true +AlignEscapedNewlines: Right +AlignOperands: Align +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortEnumsOnASingleLine: true +AllowShortBlocksOnASingleLine: true +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortLambdasOnASingleLine: All +AllowShortIfStatementsOnASingleLine: true +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Attach +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DeriveLineEnding: true +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + SortPriority: 0 + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + SortPriority: 0 + - Regex: '.*' + Priority: 1 + SortPriority: 0 +IncludeIsMainRegex: '(Test)?$' +IncludeIsMainSourceRegex: '' +IndentCaseLabels: false +IndentCaseBlocks: false +IndentGotoLabels: true +IndentPPDirectives: None +IndentExternBlock: AfterExternBlock +IndentWidth: 2 +IndentWrappedFunctionNames: false +InsertTrailingCommas: None +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Right +ReflowComments: true +SortIncludes: false +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: true +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +SpaceBeforeSquareBrackets: false +Standard: Latest +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 8 +UseCRLF: false +UseTab: Never +WhitespaceSensitiveMacros: + - STRINGIZE + - PP_STRINGIZE + - BOOST_PP_STRINGIZE +... + diff --git a/Makefile b/Makefile index a72065ea..18f4f632 100644 --- a/Makefile +++ b/Makefile @@ -146,5 +146,10 @@ uninstall: translation-uninstall test: @bash scripts/test.sh ${test_dir} +scan: + @rm -rf scan_output + @scan-build -o ./scan_output make + @scan-view ./scan_output/* + include $(wildcard .d/*.d) include util/locale.mk diff --git a/ast b/ast index bbaaa66e..591953c6 160000 --- a/ast +++ b/ast @@ -1 +1 @@ -Subproject commit bbaaa66e1ecae79f4b48c45e433cba1709dba938 +Subproject commit 591953c6a241aa5ec7cbfed3ed73ab819fdeb75f diff --git a/include/arg.h b/include/arg.h index b3d4ef06..7542fc12 100644 --- a/include/arg.h +++ b/include/arg.h @@ -8,18 +8,18 @@ enum COLOR { } __attribute__((packed)); typedef struct Arg_ { - struct CArg arg; - struct Map_ mod; - struct Vector_ add; - struct Vector_ lib; - struct Vector_ config; + struct CArg arg; + struct Map_ mod; + struct Vector_ add; + struct Vector_ lib; + struct Vector_ config; struct SoundInfo_ *si; - bool loop; - bool quit; - enum COLOR color; + bool loop; + bool quit; + enum COLOR color; } Arg; -ANN void arg_release(Arg*); -ANN m_bool arg_parse(const Gwion, Arg*); -ANN void arg_compile(const Gwion, Arg*); +ANN void arg_release(Arg *); +ANN m_bool arg_parse(const Gwion, Arg *); +ANN void arg_compile(const Gwion, Arg *); #endif diff --git a/include/array.h b/include/array.h index 68e5bc63..db9dcb22 100644 --- a/include/array.h +++ b/include/array.h @@ -3,22 +3,23 @@ struct ArrayAccessInfo { struct Array_Sub_ array; - const Type type; - const bool is_var; + const Type type; + const bool is_var; }; typedef struct ArrayInfo_ { - m_int depth; - struct Vector_ type; - Type base; - M_Object* data; + m_int depth; + struct Vector_ type; + Type base; + M_Object * data; union { - m_int* idx; // for object array + m_int *idx; // for object array size_t length; // array init } d; bool is_obj; } ArrayInfo; -ANN Type check_array_access(const Env env, const Array_Sub array); -ANN m_bool emit_array_access(const Emitter emit, struct ArrayAccessInfo *const info); +ANN Type check_array_access(const Env env, const Array_Sub array); +ANN m_bool emit_array_access(const Emitter emit, + struct ArrayAccessInfo *const info); #endif diff --git a/include/clean.h b/include/clean.h index d77408f1..40b2e8c4 100644 --- a/include/clean.h +++ b/include/clean.h @@ -7,7 +7,7 @@ void func_def_cleaner(const Gwion, Func_Def); #ifdef __CLEAN_IMPL typedef struct { m_uint scope; - Gwion gwion; + Gwion gwion; } Clean; ANN static void clean_array_sub(Clean *a, Array_Sub b); diff --git a/include/compile.h b/include/compile.h index acb61b92..f8865c19 100644 --- a/include/compile.h +++ b/include/compile.h @@ -1,6 +1,7 @@ #ifndef __COMPILE #define __COMPILE -m_uint compile_filename(struct Gwion_* vm, const m_str filename); -m_uint compile_string(struct Gwion_* vm, const m_str filename, const m_str data); -m_uint compile_file(struct Gwion_* vm, const m_str filename, FILE* file); +m_uint compile_filename(struct Gwion_ *vm, const m_str filename); +m_uint compile_string(struct Gwion_ *vm, const m_str filename, + const m_str data); +m_uint compile_file(struct Gwion_ *vm, const m_str filename, FILE *file); #endif diff --git a/include/driver.h b/include/driver.h index 433bc8a4..915fd009 100644 --- a/include/driver.h +++ b/include/driver.h @@ -1,37 +1,37 @@ #ifndef __DRIVER #define __DRIVER #include "soundinfo.h" -typedef m_bool (*f_drvini)(struct VM_*, struct BBQ_*); -typedef void (*f_drvrun)(struct VM_*, struct BBQ_*); -typedef void (*f_drvdel)(struct VM_*, struct BBQ_*); +typedef m_bool (*f_drvini)(struct VM_ *, struct BBQ_ *); +typedef void (*f_drvrun)(struct VM_ *, struct BBQ_ *); +typedef void (*f_drvdel)(struct VM_ *, struct BBQ_ *); typedef struct DriverData_ { - void* data; + void * data; f_drvini ini; f_drvrun run; f_drvdel del; } DriverData; -typedef void (*f_bbqset)(struct DriverData_*); -typedef void (*f_bbqrun)(const struct VM_*); +typedef void (*f_bbqset)(struct DriverData_ *); +typedef void (*f_bbqrun)(const struct VM_ *); typedef struct BBQ_ { - volatile uint64_t pos; - m_float* in; - m_float* out; - struct SoundInfo_ *si; - f_bbqset func; - f_bbqrun run; - struct DriverData_* driver; - volatile bool is_running; + volatile uint64_t pos; + m_float * in; + m_float * out; + struct SoundInfo_ * si; + f_bbqset func; + f_bbqrun run; + struct DriverData_ *driver; + volatile bool is_running; } Driver; -#define DRVINI(a) ANN m_bool a(struct VM_ *vm NUSED, Driver* di NUSED) -#define DRVRUN(a) ANN void a(struct VM_ *vm NUSED, Driver *di NUSED) -#define DRVDEL(a) ANN void a(struct VM_ *vm NUSED, Driver* di NUSED) +#define DRVINI(a) ANN m_bool a(struct VM_ *vm NUSED, Driver *di NUSED) +#define DRVRUN(a) ANN void a(struct VM_ *vm NUSED, Driver *di NUSED) +#define DRVDEL(a) ANN void a(struct VM_ *vm NUSED, Driver *di NUSED) -ANN void dummy_driver(DriverData*); -ANN void driver_alloc(Driver*); -ANN Driver* new_driver(MemPool); -ANN void free_driver(Driver*, struct VM_*); +ANN void dummy_driver(DriverData *); +ANN void driver_alloc(Driver *); +ANN Driver *new_driver(MemPool); +ANN void free_driver(Driver *, struct VM_ *); #endif diff --git a/include/emit.h b/include/emit.h index cdfd55ef..31a61794 100644 --- a/include/emit.h +++ b/include/emit.h @@ -3,74 +3,82 @@ typedef struct Frame_ { struct Vector_ stack; - size_t curr_offset; + size_t curr_offset; struct Map_ handlers; struct Vector_ defer; - m_uint try_top; + m_uint try_top; } Frame; typedef struct Code_ { - Frame* frame; + Frame * frame; struct Vector_ instr; - size_t stack_depth; - struct Vector_ stack_cont; + size_t stack_depth; + struct Vector_ stack_cont; struct Vector_ stack_break; struct Vector_ stack_return; - m_str name; + m_str name; } Code; struct EmitterInfo_ { struct Vector_ pure; - char *escape; + char * escape; VM_Code (*emit_code)(const Emitter); - VM_Code code; + VM_Code code; uint16_t memoize; uint16_t unroll; uint16_t line; - bool debug; + bool debug; }; struct Emitter_ { - Env env; - Code* code; - struct Gwion_ *gwion; + Env env; + Code * code; + struct Gwion_ * gwion; struct EmitterInfo_ *info; - struct Vector_ stack; - m_uint this_offset; // reset - m_uint vararg_offset; // reset + struct Vector_ stack; + m_uint this_offset; // reset + m_uint vararg_offset; // reset }; ANEW ANN Emitter new_emitter(MemPool); -ANN void free_emitter(MemPool, Emitter); -ANN m_bool emit_ast(const Env env, Ast ast); -ANN m_bool emit_func_def(const Emitter emit, const Func_Def fdef); +ANN void free_emitter(MemPool, Emitter); +ANN m_bool emit_ast(const Env env, Ast ast); +ANN m_bool emit_func_def(const Emitter emit, const Func_Def fdef); ANN m_bool emit_exp_call1(const Emitter, const Func, const bool is_static); -ANN2(1) Instr emit_add_instr(const Emitter, const f_instr) __attribute__((returns_nonnull)); -ANN Code* emit_class_code(const Emitter, const m_str); +ANN2(1) +Instr emit_add_instr(const Emitter, const f_instr) + __attribute__((returns_nonnull)); +ANN Code * emit_class_code(const Emitter, const m_str); ANN m_bool emit_array_extend(const Emitter, const Type, const Exp); -ANN void emit_class_finish(const Emitter, const Nspc); -ANN2(1,2) m_bool emit_instantiate_object(const Emitter, const Type, const Array_Sub, const m_bool); +ANN void emit_class_finish(const Emitter, const Nspc); +ANN2(1, 2) +m_bool emit_instantiate_object(const Emitter, const Type, const Array_Sub, + const m_bool); ANN m_uint emit_code_offset(const Emitter emit); ANN m_uint emit_local(const Emitter emit, const Type t); -ANN m_bool emit_exp_spork(const Emitter, const Exp_Unary*); +ANN m_bool emit_exp_spork(const Emitter, const Exp_Unary *); ANN m_bool emit_exp(const Emitter, const Exp); ANN static inline void emit_gc(const Emitter emit, const m_int offset) { const Instr gc = emit_add_instr(emit, GcAdd); - gc->m_val = offset; + gc->m_val = offset; } - -ANN Instr emit_object_addref(const Emitter emit, const m_int size, const m_bool emit_var); -ANN Instr emit_struct_addref(const Emitter emit, const Type t, const m_int size, const m_bool emit_var); -ANN static inline Instr emit_compound_addref(const Emitter emit, const Type t, const m_int size, const m_bool emit_var) { - return !tflag(t, tflag_struct) ? emit_object_addref(emit, size, emit_var) : - emit_struct_addref(emit, t, size, emit_var); +ANN Instr emit_object_addref(const Emitter emit, const m_int size, + const m_bool emit_var); +ANN Instr emit_struct_addref(const Emitter emit, const Type t, const m_int size, + const m_bool emit_var); +ANN static inline Instr emit_compound_addref(const Emitter emit, const Type t, + const m_int size, + const m_bool emit_var) { + return !tflag(t, tflag_struct) ? emit_object_addref(emit, size, emit_var) + : emit_struct_addref(emit, t, size, emit_var); } ANN static inline bool is_static_call(const Emitter emit, const Exp e) { - if(e->exp_type != ae_exp_dot) - return true; + if (e->exp_type != ae_exp_dot) return true; const Exp_Dot *member = &e->d.exp_dot; - return GET_FLAG(member->base->type, final) || is_class(emit->gwion, member->base->type) || member->base->exp_type == ae_exp_cast; + return GET_FLAG(member->base->type, final) || + is_class(emit->gwion, member->base->type) || + member->base->exp_type == ae_exp_cast; } #endif diff --git a/include/env/context.h b/include/env/context.h index 7f8e964a..3613978a 100644 --- a/include/env/context.h +++ b/include/env/context.h @@ -1,18 +1,18 @@ #ifndef __CONTEXT #define __CONTEXT -typedef struct Context_ * Context; +typedef struct Context_ *Context; struct Context_ { - Nspc nspc; - m_str name; - Ast tree; + Nspc nspc; + m_str name; + Ast tree; uint16_t ref; uint16_t weight; - bool error; - bool global; + bool error; + bool global; }; REF_FUNC(Context, context) -ANN2(1,3) ANEW Context new_context(MemPool p, const Ast, const m_str); +ANN2(1, 3) 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/env/env.h b/include/env/env.h index 4b55911e..7ece02a5 100644 --- a/include/env/env.h +++ b/include/env/env.h @@ -1,59 +1,72 @@ #ifndef __ENV #define __ENV -#define SCOPE(a) { ++env->scope; a ;--env->scope; } -#define NSPC(a) { nspc_push_value(env->curr); SCOPE(a); nspc_pop_value(env->curr); } +#define SCOPE(a) \ + { \ + ++env->scope; \ + a; \ + --env->scope; \ + } +#define NSPC(a) \ + { \ + nspc_push_value(env->curr); \ + SCOPE(a); \ + nspc_pop_value(env->curr); \ + } struct Env_Scope_ { - struct Vector_ nspc_stack; - struct Vector_ class_stack; - struct Vector_ breaks; - struct Vector_ conts; - struct Vector_ known_ctx; - struct Match_ *match; - struct Vector_ effects; - uint16_t depth; - bool in_try; - bool in_loop; + struct Vector_ nspc_stack; + struct Vector_ class_stack; + struct Vector_ breaks; + struct Vector_ conts; + struct Vector_ known_ctx; + struct Match_ *match; + struct Vector_ effects; + uint16_t depth; + bool in_try; + bool in_loop; }; typedef struct Env_ { - m_str name; - Nspc curr; - Type class_def; - Func func; - struct Context_ *context; - Nspc global_nspc; - struct Gwion_ *gwion; - struct Env_Scope_* scope; -} *Env; + m_str name; + Nspc curr; + Type class_def; + Func func; + struct Context_ * context; + Nspc global_nspc; + struct Gwion_ * gwion; + struct Env_Scope_ *scope; +} * 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); -ANN static inline m_uint env_push_global(const Env env) { return env_push(env, NULL, env->global_nspc); } -ANN void env_pop(const Env, const m_uint); -ANN Type scan_type(const Env, const Type, Type_Decl*); -ANN Value mk_class(const Env env, const Type base, const loc_t); -ANN m_bool compat_func(const __restrict__ Func_Def, const __restrict__ Func_Def); -ANN Type known_type(const Env env, Type_Decl*); -ANN Type prim_ref(const Env env, const Type t, const Type_Decl* td); +ANN2(1, 3) m_uint env_push(const Env, const Type, const Nspc); +ANN static inline m_uint env_push_global(const Env env) { + return env_push(env, NULL, env->global_nspc); +} +ANN void env_pop(const Env, const m_uint); +ANN Type scan_type(const Env, const Type, Type_Decl *); +ANN Value mk_class(const Env env, const Type base, const loc_t); +ANN m_bool compat_func(const __restrict__ Func_Def, + const __restrict__ Func_Def); +ANN Type known_type(const Env env, Type_Decl *); +ANN Type prim_ref(const Env env, const Type t, const Type_Decl *td); ANN m_bool env_access(const Env env, const ae_flag flag, const loc_t pos); ANN m_bool env_storage(const Env env, ae_flag flag, const loc_t pos); -ANN void env_add_type(const Env, const Type, const loc_t); -ANN Type find_type(const Env, Type_Decl*); +ANN void env_add_type(const Env, const Type, const loc_t); +ANN Type find_type(const Env, Type_Decl *); ANN m_bool already_defined(const Env env, const Symbol s, const loc_t pos); ANN m_bool traverse_func_template(const Env, const Func_Def); -ANN void env_err(const Env, const loc_t pos, const m_str fmt, ...); -ANN void env_warn(const Env, const loc_t pos, const m_str fmt, ...); -ANN void env_error_footer(const Env env); -ANN Value global_string(const Env env, const m_str str); -ANN void release_ctx(struct Env_Scope_ *a, struct Gwion_* gwion); +ANN void env_err(const Env, const loc_t pos, const m_str fmt, ...); +ANN void env_warn(const Env, const loc_t pos, const m_str fmt, ...); +ANN void env_error_footer(const Env env); +ANN Value global_string(const Env env, const m_str str); +ANN void release_ctx(struct Env_Scope_ *a, struct Gwion_ *gwion); struct ScopeEffect { Symbol sym; - loc_t pos; + loc_t pos; }; ANN void env_add_effect(const Env a, const Symbol effect, const loc_t pos); diff --git a/include/env/envset.h b/include/env/envset.h index aff25a5f..63f4da4e 100644 --- a/include/env/envset.h +++ b/include/env/envset.h @@ -1,36 +1,34 @@ #ifndef __ENVSET #define __ENVSET -typedef m_bool (*envset_func)(const void*,const void*); +typedef m_bool (*envset_func)(const void *, const void *); struct EnvSet { - const Env env; + const Env env; const envset_func func; - const void *data; - const m_int scope; - const enum tflag flag; - Context _ctx; - m_str _filename; - bool run; + const void * data; + const m_int scope; + const enum tflag flag; + Context _ctx; + m_str _filename; + bool run; }; -ANN m_bool envset_run(struct EnvSet*, const Type); -ANN2(1,3) m_bool envset_push(struct EnvSet*, const Type, const Nspc); +ANN m_bool envset_run(struct EnvSet *, const Type); +ANN2(1, 3) m_bool envset_push(struct EnvSet *, const Type, const Nspc); ANN static inline m_bool envset_pushv(struct EnvSet *es, const Value v) { return envset_push(es, v->from->owner_class, v->from->owner); } -ANN2(1) void envset_pop(struct EnvSet*, const Type); +ANN2(1) void envset_pop(struct EnvSet *, const Type); ANN static inline m_bool extend_push(const Env env, const Type t) { const Type owner = t->info->value->from->owner_class; - if(owner) - CHECK_BB(extend_push(env, owner)); + if (owner) CHECK_BB(extend_push(env, owner)); return env_push_type(env, t); } ANN static inline void extend_pop(const Env env, const Type t) { env_pop(env, 0); const Type owner = t->info->value->from->owner_class; - if(owner) - extend_pop(env, owner); + if (owner) extend_pop(env, owner); } #endif diff --git a/include/env/func.h b/include/env/func.h index 3ff76fc2..f2a5d968 100644 --- a/include/env/func.h +++ b/include/env/func.h @@ -12,26 +12,28 @@ enum fflag { } __attribute__((packed)); struct Func_ { - Func_Def def; - struct VM_Code_* code; - Value value_ref; - Func next; - m_str name; - float inline_mult; - uint16_t weight; - uint16_t ref; - struct Map_ upvalues; - uint16_t vt_index; - ae_flag flag; - enum fflag fflag; + Func_Def def; + struct VM_Code_ *code; + Value value_ref; + Func next; + m_str name; + float inline_mult; + uint16_t weight; + uint16_t ref; + struct Map_ upvalues; + uint16_t vt_index; + ae_flag flag; + enum fflag fflag; }; REF_FUNC(Func, func) FLAG_FUNC(Func, f) 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); -ANN m_bool check_lambda(const Env, const Type, Exp_Lambda*); -ANN Type check_op_call(const Env env, Exp_Call *const exp); -ANN void builtin_func(const MemPool mp, const Func f, void* func_ptr); +ANN2(1, 2) +Symbol func_symbol(const Env, const m_str, const m_str, const m_str, + const m_uint); +ANN m_bool check_lambda(const Env, const Type, Exp_Lambda *); +ANN Type check_op_call(const Env env, Exp_Call *const exp); +ANN void builtin_func(const MemPool mp, const Func f, void *func_ptr); #endif diff --git a/include/env/nspc.h b/include/env/nspc.h index 3790790c..a988f253 100644 --- a/include/env/nspc.h +++ b/include/env/nspc.h @@ -1,7 +1,7 @@ #ifndef __NSPC #define __NSPC struct NspcInfo_ { - m_bit* class_data; + m_bit * class_data; struct Map_ op_map; Scope value; Scope type; @@ -12,12 +12,12 @@ struct NspcInfo_ { }; struct Nspc_ { - struct NspcInfo_* info; + struct NspcInfo_ *info; Nspc parent; m_str name; struct Vector_ vtable; - struct VM_Code_* pre_ctor; - struct VM_Code_* dtor; + struct VM_Code_ * pre_ctor; + struct VM_Code_ * dtor; uint16_t ref; }; @@ -25,58 +25,61 @@ REF_FUNC(Nspc, nspc) 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); +// extern ANN void nspc_rollback(const Nspc); #define describe_lookup0(A, b) \ -static inline ANN A nspc_lookup_##b##0(const Nspc n, const Symbol s){ \ - return (A)scope_lookup0(n->info->b, (vtype)s); \ -} + static inline ANN A nspc_lookup_##b##0(const Nspc n, const Symbol s) { \ + return (A)scope_lookup0(n->info->b, (vtype)s); \ + } #define describe_lookup1(A, b) \ -static inline ANN A nspc_lookup_##b##1(const Nspc n, const Symbol s) { \ - const A a = (A)scope_lookup1(n->info->b, (vtype)s); \ - if(!a && n->parent) \ - return nspc_lookup_##b##1(n->parent, s); \ - return a; \ -} + static inline ANN A nspc_lookup_##b##1(const Nspc n, const Symbol s) { \ + const A a = (A)scope_lookup1(n->info->b, (vtype)s); \ + if (!a && n->parent) return nspc_lookup_##b##1(n->parent, s); \ + return a; \ + } #define describe_lookup2(A, b) \ -static inline ANN A nspc_lookup_##b##2(const Nspc n, const Symbol s) { \ - return (A)scope_lookup2(n->info->b, (vtype)s); \ -} + static inline ANN A nspc_lookup_##b##2(const Nspc n, const Symbol s) { \ + return (A)scope_lookup2(n->info->b, (vtype)s); \ + } #define describe_lookups(A, b) \ -describe_lookup0(A, b) \ -describe_lookup1(A, b) \ -describe_lookup2(A, b) + describe_lookup0(A, b) describe_lookup1(A, b) describe_lookup2(A, b) #define describe_nspc_func(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); \ -} \ -static inline ANN void nspc_add_##b##_front(const Nspc n, const Symbol s, const A a) { \ - map_set(&n->info->b->map, (vtype)s, (vtype)a); \ -} \ -ANN static inline void nspc_push_##b(MemPool p, const Nspc n) { scope_push(p, n->info->b); }\ -ANN static inline void nspc_pop_##b (MemPool p, const Nspc n) { scope_pop (p, n->info->b); }\ -describe_lookups(A, b) - -describe_nspc_func(Value, value) -describe_nspc_func(Type, type) -describe_nspc_func(Func, func) -describe_nspc_func(Trait, trait) -/* howere there is no need for lookup_func0, push_func, pop_func */ -ANN void did_you_mean_nspc(const Nspc, const char*); -ANN void did_you_mean_type(const Type, const char*); + 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); \ + } \ + static inline ANN void nspc_add_##b##_front(const Nspc n, const Symbol s, \ + const A a) { \ + map_set(&n->info->b->map, (vtype)s, (vtype)a); \ + } \ + ANN static inline void nspc_push_##b(MemPool p, const Nspc n) { \ + scope_push(p, n->info->b); \ + } \ + ANN static inline void nspc_pop_##b(MemPool p, const Nspc n) { \ + scope_pop(p, n->info->b); \ + } \ + describe_lookups(A, b) +describe_nspc_func(Value, value) describe_nspc_func(Type, type) + describe_nspc_func(Func, func) describe_nspc_func(Trait, trait) + /* howere there is no need for lookup_func0, push_func, pop_func */ + ANN void did_you_mean_nspc(const Nspc, const char *); +ANN void did_you_mean_type(const Type, const char *); #define DID_YOU_MEAN_LIMIT 128 -#define did_you_mean_nspc(a, b) if(strlen(b) < DID_YOU_MEAN_LIMIT) did_you_mean_nspc(a,b); -#define did_you_mean_type(a, b) if(strlen(b) < DID_YOU_MEAN_LIMIT) did_you_mean_type(a,b); +#define did_you_mean_nspc(a, b) \ + if (strlen(b) < DID_YOU_MEAN_LIMIT) did_you_mean_nspc(a, b); +#define did_you_mean_type(a, b) \ + if (strlen(b) < DID_YOU_MEAN_LIMIT) did_you_mean_type(a, b); ANN static inline void nspc_allocdata(MemPool mp, const Nspc nspc) { - if(nspc->info->class_data_size) { - nspc->info->class_data = (m_bit*)mp_calloc2(mp, nspc->info->class_data_size); + if (nspc->info->class_data_size) { + nspc->info->class_data = + (m_bit *)mp_calloc2(mp, nspc->info->class_data_size); } } #endif diff --git a/include/env/oo.h b/include/env/oo.h index 9ec8eeaa..e0c02a6a 100644 --- a/include/env/oo.h +++ b/include/env/oo.h @@ -1,15 +1,17 @@ #ifndef __OO #define __OO -typedef struct Gwion_ * Gwion; +typedef struct Gwion_ *Gwion; -typedef struct Type_ * Type; -typedef struct Nspc_ * Nspc; -typedef struct Value_ * Value; -typedef struct Func_ * Func; +typedef struct Type_ * Type; +typedef struct Nspc_ * Nspc; +typedef struct Value_ *Value; +typedef struct Func_ * Func; -#define REF_FUNC(A, b) \ -ANN void free_##b(const A, struct Gwion_*const); \ -ANN static inline void b##_addref(const A a) { ++a->ref; } \ -ANN static inline void b##_remref(const A a, struct Gwion_ *const gwion) { if(!--a->ref) free_##b(a, gwion); } +#define REF_FUNC(A, b) \ + ANN void free_##b(const A, struct Gwion_ *const); \ + ANN static inline void b##_addref(const A a) { ++a->ref; } \ + ANN static inline void b##_remref(const A a, struct Gwion_ *const gwion) { \ + if (!--a->ref) free_##b(a, gwion); \ + } #endif diff --git a/include/env/trait.h b/include/env/trait.h index 6a83b10f..a479a020 100644 --- a/include/env/trait.h +++ b/include/env/trait.h @@ -4,10 +4,10 @@ typedef struct Trait_ { struct Vector_ requested_values; struct Vector_ requested_funcs; - m_str filename; - loc_t loc; -} *Trait; + m_str filename; + loc_t loc; +} * Trait; ANN Trait new_trait(MemPool, const loc_t); -ANN void free_trait(MemPool, Trait); +ANN void free_trait(MemPool, Trait); #endif diff --git a/include/env/tuple.h b/include/env/tuple.h index 690afc07..9da2bfee 100644 --- a/include/env/tuple.h +++ b/include/env/tuple.h @@ -1,11 +1,11 @@ #ifndef __TUPLE #define __TUPLE -typedef struct TupleForm_* TupleForm; +typedef struct TupleForm_ *TupleForm; struct TupleForm_ { struct Vector_ contains; struct Vector_ types; struct Vector_ offset; - m_uint start; + m_uint start; }; ANN Type tuple_type(const Env, const Vector, const loc_t); diff --git a/include/env/type.h b/include/env/type.h index 61563234..c77f7ea9 100644 --- a/include/env/type.h +++ b/include/env/type.h @@ -2,26 +2,26 @@ #define __TYPE struct TypeInfo_ { - Type parent; + Type parent; Value value; union { Union_Def udef; Class_Def cdef; Func func; }; - Type base_type; - struct TupleForm_* tuple; - struct VM_Code_ *gack; - ID_List traits; + Type base_type; + struct TupleForm_ *tuple; + struct VM_Code_ * gack; + ID_List traits; }; enum tflag { tflag_none = 1 << 0, - tflag_scan0 = 1 << 1,// - tflag_scan1 = 1 << 2,// - tflag_scan2 = 1 << 3,// - tflag_check = 1 << 4,// - tflag_emit = 1 << 5,// + tflag_scan0 = 1 << 1, // + tflag_scan1 = 1 << 2, // + tflag_scan2 = 1 << 3, // + tflag_check = 1 << 4, // + tflag_emit = 1 << 5, // tflag_infer = 1 << 6, tflag_empty = 1 << 7, tflag_ftmpl = 1 << 8, @@ -39,56 +39,55 @@ enum tflag { } __attribute__((packed)); struct Type_ { - m_str name; - Nspc nspc; + m_str name; + Nspc nspc; struct TypeInfo_ *info; - size_t size; - size_t array_depth; - struct Vector_ effects; // pre-ctor effects - uint16_t ref; - uint16_t weight; - ae_flag flag; - enum tflag tflag; + size_t size; + size_t array_depth; + struct Vector_ effects; // pre-ctor effects + uint16_t ref; + uint16_t weight; + ae_flag flag; + enum tflag tflag; }; REF_FUNC(Type, type) FLAG_FUNC(Type, t) -ANN2(1,2) ANEW Type new_type(MemPool, const m_str name, const Type); +ANN2(1, 2) ANEW Type new_type(MemPool, const m_str name, const Type); ANEW ANN Type type_copy(MemPool, const Type type); -ANN Value find_value(const Type, const Symbol); -ANN m_bool isa(const Type, const Type) __attribute__((pure)); -ANN m_bool isres(const Env, const Symbol, const loc_t pos); -ANN Type array_type(const Env, const Type, const m_uint); -ANN Type find_common_anc(const Type, const Type) __attribute__((pure)); -ANN Type typedef_base(Type) __attribute__((pure)); -ANN Type array_base(Type) __attribute__((pure)); -ANN m_bool type_ref(Type) __attribute__((pure)); -ANN Type actual_type(const struct Gwion_* gwion, const Type t); -ANN static inline m_uint env_push_type(const Env env, const Type type) { return env_push(env, type, type->nspc); } -ANN m_bool is_fptr(const struct Gwion_*, const Type t); -ANN m_bool is_class(const struct Gwion_*, const Type t); -ANN __attribute__((returns_nonnull)) -static inline Type _class_base(Type t) { +ANN Value find_value(const Type, const Symbol); +ANN m_bool isa(const Type, const Type) __attribute__((pure)); +ANN m_bool isres(const Env, const Symbol, const loc_t pos); +ANN Type array_type(const Env, const Type, const m_uint); +ANN Type find_common_anc(const Type, const Type) __attribute__((pure)); +ANN Type typedef_base(Type) __attribute__((pure)); +ANN Type array_base(Type) __attribute__((pure)); +ANN m_bool type_ref(Type) __attribute__((pure)); +ANN Type actual_type(const struct Gwion_ *gwion, const Type t); +ANN static inline m_uint env_push_type(const Env env, const Type type) { + return env_push(env, type, type->nspc); +} +ANN m_bool is_fptr(const struct Gwion_ *, const Type t); +ANN m_bool is_class(const struct Gwion_ *, const Type t); +ANN __attribute__((returns_nonnull)) static inline Type _class_base(Type t) { return t->info->base_type; } ANN m_uint get_depth(const Type type); -ANN void inherit(const Type); +ANN void inherit(const Type); -__attribute__((returns_nonnull)) -ANN static inline Type get_gack(Type t) { - do if(t->info->gack) - return t; - while((t = t->info->parent)); +__attribute__((returns_nonnull)) ANN static inline Type get_gack(Type t) { + do + if (t->info->gack) return t; + while ((t = t->info->parent)); return t; // unreachable } // trait helpers ANN static inline bool has_trait(const Type t, const Symbol trait) { ID_List list = t->info->traits; - while(list) { - if(list->xid == trait) - return true; + while (list) { + if (list->xid == trait) return true; list = list->next; } return false; @@ -96,17 +95,38 @@ ANN static inline bool has_trait(const Type t, const Symbol trait) { ANN static inline ID_List miss_traits(const Type t, const Specialized_List sl) { ID_List traits = sl->traits; - while(traits) { - if(!has_trait(t, traits->xid)) - return traits; + while (traits) { + if (!has_trait(t, traits->xid)) return traits; traits = traits->next; } return NULL; } typedef enum { - et_void, et_int, et_bool, et_char, et_float, - et_error, et_compound, et_object, et_shred, et_fork, et_event, et_ugen, et_string, et_ptr, et_array, et_gack, - et_function, et_fptr, et_vararg, et_lambda, et_op, et_class, et_union, et_auto, et_none, + et_void, + et_int, + et_bool, + et_char, + et_float, + et_error, + et_compound, + et_object, + et_shred, + et_fork, + et_event, + et_ugen, + et_string, + et_ptr, + et_array, + et_gack, + et_function, + et_fptr, + et_vararg, + et_lambda, + et_op, + et_class, + et_union, + et_auto, + et_none, MAX_TYPE } type_enum; #endif diff --git a/include/env/value.h b/include/env/value.h index ec3ebf05..c4c70097 100644 --- a/include/env/value.h +++ b/include/env/value.h @@ -1,49 +1,49 @@ #ifndef __VALUE #define __VALUE struct ValueFrom_ { - Nspc owner; - Type owner_class; + Nspc owner; + Type owner_class; struct Context_ *ctx; - size_t offset; - m_str filename; - loc_t loc; + size_t offset; + m_str filename; + loc_t loc; }; enum vflag { - vflag_none = 1 << 0, - vflag_func = 1 << 1, - vflag_fglobal = 1 << 2, - vflag_valid = 1 << 3, - vflag_direct = 1 << 4, - vflag_builtin = 1 << 5, - vflag_member = 1 << 6, - vflag_closed = 1 << 7 -// vflag_used = 1 << 3 + vflag_none = 1 << 0, + vflag_func = 1 << 1, + vflag_fglobal = 1 << 2, + vflag_valid = 1 << 3, + vflag_direct = 1 << 4, + vflag_builtin = 1 << 5, + vflag_member = 1 << 6, + vflag_closed = 1 << 7 + // vflag_used = 1 << 3 } __attribute__((packed)); union value_data { - m_uint num; - m_float fnum; - m_uint* ptr; + m_uint num; + m_float fnum; + m_uint * ptr; struct M_Object_ *obj; - Func func_ref; + Func func_ref; }; struct Value_ { - Type type; - m_str name; + Type type; + m_str name; struct ValueFrom_ *from; - union value_data d; - uint16_t ref; - ae_flag flag; - enum vflag vflag; + union value_data d; + uint16_t ref; + ae_flag flag; + enum vflag vflag; }; REF_FUNC(Value, value) FLAG_FUNC(Value, v) ANEW ANN Value new_value(MemPool p, const Type type, const m_str name); -ANN void valuefrom(const Env, struct ValueFrom_*, const loc_t loc); +ANN void valuefrom(const Env, struct ValueFrom_ *, const loc_t loc); ANN static inline void defined_here(const Value v) { gwerr_secondary(_("defined here"), v->from->filename, v->from->loc); diff --git a/include/escape.h b/include/escape.h index 463f5661..dd960c4d 100644 --- a/include/escape.h +++ b/include/escape.h @@ -1,6 +1,6 @@ #ifndef __ESCAPE #define __ESCAPE -char* escape_table(MemPool); -ANN char str2char(const Emitter, const m_str, const loc_t); +char * escape_table(MemPool); +ANN char str2char(const Emitter, const m_str, const loc_t); ANN m_bool escape_str(const Emitter, m_str, const loc_t); #endif diff --git a/include/gack.h b/include/gack.h index f7c901a6..d926119b 100644 --- a/include/gack.h +++ b/include/gack.h @@ -2,11 +2,12 @@ #define __GACK ANN void gack(const VM_Shred, const m_uint); -#define INTERP_PRINTF(fmt, ...) { \ - m_str ret = *(m_str*)(VALUE - SZ_INT);\ - gw_asprintf(shred->info->mp, &ret, fmt, ##__VA_ARGS__); \ - *(m_str*)(VALUE - SZ_INT) = ret;\ -} +#define INTERP_PRINTF(fmt, ...) \ + { \ + m_str ret = *(m_str *)(VALUE - SZ_INT); \ + gw_asprintf(shred->info->mp, &ret, fmt, ##__VA_ARGS__); \ + *(m_str *)(VALUE - SZ_INT) = ret; \ + } ANN2(2) int gw_asprintf(MemPool mp, char **str, const char *fmt, ...); #endif diff --git a/include/gwi.h b/include/gwi.h index 2ded2e39..e35e4747 100644 --- a/include/gwi.h +++ b/include/gwi.h @@ -2,26 +2,40 @@ #define __GWI #include "gwfmt.h" -#define gwiheader(a,...) if(a->gwion->data->cdoc) do {lint_nl(gwi->lint); lint_indent(gwi->lint); lint(gwi->lint, "{-}#!+ {/}%s{0}\n", __VA_ARGS__); } while(0) -#define gwidoc(a,...) if(a->gwion->data->cdoc) do { lint_nl(a->lint); lint_indent(a->lint); lint(a->lint, "{-}#!- {/}%s{0}\n", __VA_ARGS__); } while(0) -#define gwinote(a,...) if(a->gwion->data->cdoc) do { lint_indent(a->lint); lint(a->lint, "{-}#!- {/}%s{0}\n", __VA_ARGS__); } while(0) +#define gwiheader(a, ...) \ + if (a->gwion->data->cdoc) do { \ + lint_nl(gwi->lint); \ + lint_indent(gwi->lint); \ + lint(gwi->lint, "{-}#!+ {/}%s{0}\n", __VA_ARGS__); \ + } while (0) +#define gwidoc(a, ...) \ + if (a->gwion->data->cdoc) do { \ + lint_nl(a->lint); \ + lint_indent(a->lint); \ + lint(a->lint, "{-}#!- {/}%s{0}\n", __VA_ARGS__); \ + } while (0) +#define gwinote(a, ...) \ + if (a->gwion->data->cdoc) do { \ + lint_indent(a->lint); \ + lint(a->lint, "{-}#!- {/}%s{0}\n", __VA_ARGS__); \ + } while (0) struct Gwi_ { struct Gwion_ *const gwion; - Ast body; - struct ImportCK *ck; - struct OperCK *oper; // _misc - struct Vector_ effects; - loc_t loc; - Lint *lint; - uint8_t tmpls; + Ast body; + struct ImportCK * ck; + struct OperCK * oper; // _misc + struct Vector_ effects; + loc_t loc; + Lint * lint; + uint8_t tmpls; }; #include "import/internals.h" -static inline Tmpl* gwi_tmpl(const Gwi gwi) { +static inline Tmpl *gwi_tmpl(const Gwi gwi) { return new_tmpl_base(gwi->gwion->mp, gwi->ck->sl); } -ANN void gwi_effects(const Gwi gwi, const m_str name); +ANN void gwi_effects(const Gwi gwi, const m_str name); ANN m_bool gwi_run(const Gwion gwion, m_bool (*f)(const Gwi)); #endif diff --git a/include/gwion.h b/include/gwion.h index 688ca942..94fb00d6 100644 --- a/include/gwion.h +++ b/include/gwion.h @@ -1,38 +1,40 @@ #ifndef __GWION #define __GWION -//typedef struct Gwion_* Gwion; +// typedef struct Gwion_* Gwion; #include "plug.h" #include "driver.h" #include "gwiondata.h" struct Arg_; struct Gwion_ { - MemPool mp; - VM* vm; - SymTable *st; - Env env; - Emitter emit; + MemPool mp; + VM * vm; + SymTable * st; + Env env; + Emitter emit; struct GwionData_ *data; - Type *type; - struct PPArg_ *ppa; + Type * type; + struct PPArg_ * ppa; }; -ANN m_bool gwion_ini(const Gwion, struct Arg_*); -ANN VM* gwion_cpy(const VM*); +ANN m_bool gwion_ini(const Gwion, struct Arg_ *); +ANN VM * gwion_cpy(const VM *); ANN void gwion_run(const Gwion gwion); ANN void gwion_end(const Gwion gwion); -void free_code_instr(const Vector v, const Gwion gwion); +void free_code_instr(const Vector v, const Gwion gwion); ANN void gwion_end_child(const VM_Shred shred, const Gwion gwion); ANN void push_global(const Gwion gwion, const m_str name); ANN Nspc pop_global(const Gwion gwion); -__attribute__((returns_nonnull)) -ANN static inline Value type_value(const Gwion gwion, const Type t) { - return (Value)nspc_lookup_value1(t->info->value->from->owner, insert_symbol(gwion->st, t->name)); +__attribute__((returns_nonnull)) ANN static inline Value +type_value(const Gwion gwion, const Type t) { + return (Value)nspc_lookup_value1(t->info->value->from->owner, + insert_symbol(gwion->st, t->name)); } -__attribute__((returns_nonnull)) -ANN static inline Type type_class(const Gwion gwion, const Type t) { - const Value v = nspc_lookup_value1(t->info->value->from->owner, insert_symbol(gwion->st, t->name)); +__attribute__((returns_nonnull)) ANN static inline Type +type_class(const Gwion gwion, const Type t) { + const Value v = nspc_lookup_value1(t->info->value->from->owner, + insert_symbol(gwion->st, t->name)); return v->type; } diff --git a/include/gwiondata.h b/include/gwiondata.h index eb56f935..39203c68 100644 --- a/include/gwiondata.h +++ b/include/gwiondata.h @@ -1,18 +1,18 @@ #ifndef __GWIONDATA #define __GWIONDATA typedef struct GwionData_ { - struct Map_ freearg; - struct Map_ id; - MUTEX_TYPE mutex; - struct Vector_ child; - struct Vector_ child2; - struct Passes_ *passes; - struct Map_ plug; - bool cdoc; + struct Map_ freearg; + struct Map_ id; + MUTEX_TYPE mutex; + struct Vector_ child; + struct Vector_ child2; + struct Passes_ *passes; + struct Map_ plug; + bool cdoc; } GwionData; -ANN GwionData* new_gwiondata(const MemPool); -ANN GwionData* cpy_gwiondata(MemPool, const GwionData*); -ANN void free_gwiondata(const Gwion); -ANN void free_gwiondata_cpy(const MemPool, GwionData*); +ANN GwionData *new_gwiondata(const MemPool); +ANN GwionData *cpy_gwiondata(MemPool, const GwionData *); +ANN void free_gwiondata(const Gwion); +ANN void free_gwiondata_cpy(const MemPool, GwionData *); #endif diff --git a/include/import.h b/include/import.h index 373847f5..fd10bc9e 100644 --- a/include/import.h +++ b/include/import.h @@ -1,33 +1,62 @@ #ifndef __IMPORT #define __IMPORT -typedef void (*f_xtor)(const M_Object, const m_bit*, const VM_Shred); -typedef void (*f_mfun)(const M_Object, const m_bit*, const VM_Shred); -typedef void (*f_sfun)(const m_bit*, const m_bit*, const VM_Shred); -typedef void (*f_gack)(const Type, const m_bit*, const VM_Shred); +typedef void (*f_xtor)(const M_Object, const m_bit *, const VM_Shred); +typedef void (*f_mfun)(const M_Object, const m_bit *, const VM_Shred); +typedef void (*f_sfun)(const m_bit *, const m_bit *, const VM_Shred); +typedef void (*f_gack)(const Type, const m_bit *, const VM_Shred); typedef void (*f_xfun)(); -typedef struct Gwi_* Gwi; +typedef struct Gwi_ *Gwi; -#define MFUN(a) ANN void a(const M_Object o NUSED, const m_bit* RETURN NUSED, const VM_Shred shred NUSED) -#define SFUN(a) ANN void a(const M_Object o NUSED, const m_bit* RETURN NUSED, const VM_Shred shred NUSED) -#define CTOR(a) ANN void a(const M_Object o NUSED, const m_bit* _ NUSED, const VM_Shred shred NUSED) -#define DTOR(a) ANN void a(const M_Object o NUSED, const m_bit* _ NUSED, const VM_Shred shred NUSED) -#define GACK(a) ANN2(2) void a(const Type t NUSED, const m_bit* VALUE NUSED, const VM_Shred shred NUSED) -#define OP_CHECK(a) ANN Type a(const Env env NUSED, void* data NUSED) -#define OP_EMIT(a) ANN m_bool a(const Emitter emit NUSED, void* data NUSED) +#define MFUN(a) \ + ANN void a(const M_Object o NUSED, const m_bit *RETURN NUSED, \ + const VM_Shred shred NUSED) +#define SFUN(a) \ + ANN void a(const M_Object o NUSED, const m_bit *RETURN NUSED, \ + const VM_Shred shred NUSED) +#define CTOR(a) \ + ANN void a(const M_Object o NUSED, const m_bit *_ NUSED, \ + const VM_Shred shred NUSED) +#define DTOR(a) \ + ANN void a(const M_Object o NUSED, const m_bit *_ NUSED, \ + const VM_Shred shred NUSED) +#define GACK(a) \ + ANN2(2) \ + void a(const Type t NUSED, const m_bit *VALUE NUSED, \ + const VM_Shred shred NUSED) +#define OP_CHECK(a) ANN Type a(const Env env NUSED, void *data NUSED) +#define OP_EMIT(a) ANN m_bool a(const Emitter emit NUSED, void *data NUSED) #ifdef GWION_BUILTIN -#define GWI_BB(a) { gwi_set_loc(gwi, __FILE__, __LINE__); (void)(a); } -#define GWI_OB(a) { gwi_set_loc(gwi, __FILE__, __LINE__); (void)(a); } +#define GWI_BB(a) \ + { \ + gwi_set_loc(gwi, __FILE__, __LINE__); \ + (void)(a); \ + } +#define GWI_OB(a) \ + { \ + gwi_set_loc(gwi, __FILE__, __LINE__); \ + (void)(a); \ + } #define GWION_IMPORT(a) ANN m_bool import_##a(const Gwi gwi) #else -#define GWI_BB(a) { gwi_set_loc(gwi, (m_str)__FILE__, __LINE__); CHECK_BB(a); } -#define GWI_OB(a) { gwi_set_loc(gwi, (m_str)__FILE__, __LINE__); CHECK_OB(a); } +#define GWI_BB(a) \ + { \ + gwi_set_loc(gwi, (m_str)__FILE__, __LINE__); \ + CHECK_BB(a); \ + } +#define GWI_OB(a) \ + { \ + gwi_set_loc(gwi, (m_str)__FILE__, __LINE__); \ + CHECK_OB(a); \ + } #define GWION_IMPORT(a) ANN m_bool import(const Gwi gwi) #endif -#define ALLOC_PTR(p, a, b, c) b* a = (b*)_mp_calloc(p, sizeof(b)); *a = (b)c -#define _CHECK_OP(op, check, func)\ - GWI_BB(gwi_oper_add(gwi, opck_##check))\ - GWI_BB(gwi_oper_end(gwi, op, func)) +#define ALLOC_PTR(p, a, b, c) \ + b *a = (b *)_mp_calloc(p, sizeof(b)); \ + *a = (b)c +#define _CHECK_OP(op, check, func) \ + GWI_BB(gwi_oper_add(gwi, opck_##check)) \ + GWI_BB(gwi_oper_end(gwi, op, func)) #include "import/checker.h" #include "import/type.h" @@ -52,8 +81,10 @@ OP_CHECK(opck_usr_implicit); OP_CHECK(opck_new); OP_EMIT(opem_new); -ANN2(1,3) static inline M_Object new_object_str(const Gwion gwion, const VM_Shred shred, const m_str str) { - struct loc_t_ loc = {}; +ANN2(1, 3) +static inline M_Object new_object_str(const Gwion gwion, const VM_Shred shred, + const m_str str) { + struct loc_t_ loc = {}; DECL_OO(const Type, t, = str2type(gwion, str, loc)); return new_object(gwion->mp, shred, t); } diff --git a/include/import/cdef.h b/include/import/cdef.h index 3cdb9728..4c84ae33 100644 --- a/include/import/cdef.h +++ b/include/import/cdef.h @@ -1,8 +1,9 @@ #ifndef __IMPORT_CLASS #define __IMPORT_CLASS -ANN2(1,2) Type gwi_class_ini(const Gwi gwi, const m_str, const m_str parent); -ANN2(1,2) Type gwi_struct_ini(const Gwi gwi, const m_str); -ANN2(1) void gwi_class_xtor(const Gwi gwi, const f_xtor ctor, const f_xtor dtor); +ANN2(1, 2) Type gwi_class_ini(const Gwi gwi, const m_str, const m_str parent); +ANN2(1, 2) Type gwi_struct_ini(const Gwi gwi, const m_str); +ANN2(1) +void gwi_class_xtor(const Gwi gwi, const f_xtor ctor, const f_xtor dtor); ANN m_int gwi_class_end(const Gwi gwi); #define gwi_struct_end(a) gwi_class_end(a) ANN void inherit(const Type); diff --git a/include/import/checker.h b/include/import/checker.h index 5f80b16e..26b7d0e8 100644 --- a/include/import/checker.h +++ b/include/import/checker.h @@ -1,81 +1,75 @@ #ifndef __IMPORT_CHECKER #define __IMPORT_CHECKER -enum importck_type { - ck_edef, - ck_udef, - ck_tdef, - ck_oper, - ck_item, - ck_fdef -}; +enum importck_type { ck_edef, ck_udef, ck_tdef, ck_oper, ck_item, ck_fdef }; typedef struct ImportCK { // name_checker ? - m_str name; + m_str name; Symbol sym; union { - ID_List tmpl; + ID_List tmpl; Specialized_List sl; - Exp exp; + Exp exp; }; union { - Symbol xid;// union name + Symbol xid; // union name f_xfun addr; }; union { - Union_List list;// union + Union_List list; // union struct Vector_ v; -// ID_List curr;// enum + // ID_List curr;// enum }; union { - Type_Decl *td; // typedef - ID_List curr;// enum + Type_Decl *td; // typedef + ID_List curr; // enum }; - bool variadic; + bool variadic; enum importck_type type; - ae_flag flag; // ???? + ae_flag flag; // ???? } ImportCK; typedef struct OperCK { // name_checker ? - m_str ret; + m_str ret; Symbol sym; - Type (*ck)(Env, void*); // oper - m_bool (*em)(Emitter, void*); // oper - m_str lhs;// oper - m_str rhs;// oper + Type (*ck)(Env, void *); // oper + m_bool (*em)(Emitter, void *); // oper + m_str lhs; // oper + m_str rhs; // oper struct Vector_ effect; } OperCK; -ANN void func_checker_clean(const Gwi gwi, struct ImportCK *ck); +ANN void func_checker_clean(const Gwi gwi, struct ImportCK *ck); ANN m_bool check_typename_def(const Gwi gwi, struct ImportCK *ck); -ANN Symbol str2sym(const Gwion, const m_str, const loc_t); -ANN ID_List str2symlist(const Gwion, const m_str, const loc_t); +ANN Symbol str2sym(const Gwion, const m_str, const loc_t); +ANN ID_List str2symlist(const Gwion, const m_str, const loc_t); ANN Var_Decl str2var(const Gwion, const m_str, const loc_t); ANN Var_Decl_List str2varlist(const Gwion, const m_str, const loc_t); -ANN Type_Decl* str2td(const Gwion, const m_str, const loc_t); -ANN Type str2type(const Gwion, const m_str, const loc_t); +ANN Type_Decl *str2td(const Gwion, const m_str, const loc_t); +ANN Type str2type(const Gwion, const m_str, const loc_t); // those functions return a mp_alloced string ANEW ANN m_str tl2str(const Gwion, const Type_List, const loc_t); ANEW ANN m_str type2str(const Gwion, const Type, const loc_t); -ANN static inline Type_Decl* type2td(const Gwion gwion, const Type t, const loc_t pos) { +ANN static inline Type_Decl *type2td(const Gwion gwion, const Type t, + const loc_t pos) { const m_str str = type2str(gwion, t, pos); - Type_Decl *td = str2td(gwion, str, pos); + Type_Decl * td = str2td(gwion, str, pos); free_mstr(gwion->mp, str); return td; } -#define gwi_str2sym(gwi, path) str2sym(gwi->gwion, path, gwi->loc) +#define gwi_str2sym(gwi, path) str2sym(gwi->gwion, path, gwi->loc) #define gwi_str2symlist(gwi, path) str2symlist(gwi->gwion, path, gwi->loc) -#define gwi_str2var(gwi, path) str2var(gwi->gwion, path, gwi->loc) +#define gwi_str2var(gwi, path) str2var(gwi->gwion, path, gwi->loc) #define gwi_str2varlist(gwi, path) str2varlist(gwi->gwion, path, gwi->loc) -#define gwi_str2td(gwi, path) str2td(gwi->gwion, path, gwi->loc) -#define gwi_str2type(gwi, path) str2type(gwi->gwion, path, gwi->loc) +#define gwi_str2td(gwi, path) str2td(gwi->gwion, path, gwi->loc) +#define gwi_str2type(gwi, path) str2type(gwi->gwion, path, gwi->loc) ANN m_bool ck_ini(const Gwi, const enum importck_type); ANN m_bool ck_ok(const Gwi, const enum importck_type); -ANN void ck_end(const Gwi gwi); -ANN void ck_clean(const Gwi gwi); +ANN void ck_end(const Gwi gwi); +ANN void ck_clean(const Gwi gwi); #endif diff --git a/include/import/edef.h b/include/import/edef.h index 3fcdf761..48c13ff7 100644 --- a/include/import/edef.h +++ b/include/import/edef.h @@ -3,6 +3,6 @@ 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 value); -ANN Type gwi_enum_end(const Gwi gwi); -ANN void ck_clean_edef(MemPool, ImportCK*); +ANN Type gwi_enum_end(const Gwi gwi); +ANN void ck_clean_edef(MemPool, ImportCK *); #endif diff --git a/include/import/fdef.h b/include/import/fdef.h index d46a6933..b433f120 100644 --- a/include/import/fdef.h +++ b/include/import/fdef.h @@ -1,11 +1,15 @@ #ifndef __IMPORT_FUNC #define __IMPORT_FUNC -ANN m_int gwi_func_ini(const Gwi gwi, const __restrict__ m_str type, const __restrict__ m_str name); -ANN m_int gwi_func_arg(const Gwi gwi, const __restrict__ m_str t, const __restrict__ m_str n); +ANN m_int gwi_func_ini(const Gwi gwi, const __restrict__ m_str type, + const __restrict__ m_str name); +ANN m_int gwi_func_arg(const Gwi gwi, const __restrict__ m_str t, + const __restrict__ m_str n); ANN m_int gwi_func_end(const Gwi gwi, const f_xfun addr, const ae_flag flag); -ANN m_int gwi_fptr_ini(const Gwi gwi, const m_str __restrict__ type, const __restrict__ m_str name); +ANN m_int gwi_fptr_ini(const Gwi gwi, const m_str __restrict__ type, + const __restrict__ m_str name); ANN Type gwi_fptr_end(const Gwi gwi, const ae_flag flag); -ANN m_int gwi_func_arg(const Gwi gwi, const restrict m_str t, const restrict m_str n); -ANN void ck_clean_fdef(MemPool, ImportCK*); +ANN m_int gwi_func_arg(const Gwi gwi, const restrict m_str t, + const restrict m_str n); +ANN void ck_clean_fdef(MemPool, ImportCK *); #endif diff --git a/include/import/internals.h b/include/import/internals.h index bbb3b162..0c476ec5 100644 --- a/include/import/internals.h +++ b/include/import/internals.h @@ -1,20 +1,46 @@ #ifndef __IMPORT_INTERNALS #define __IMPORT_INTERNALS -#define GWI_ERR(a,...) { env_err(gwi->gwion->env, gwi->loc, (a), ## __VA_ARGS__); } -#define GWI_ERR_B(a,...) { GWI_ERR((a), ## __VA_ARGS__); return GW_ERROR; } -#define GWI_ERR_O(a,...) { GWI_ERR((a), ## __VA_ARGS__); return NULL; } -#define GWION_ERR(pos, a,...) { env_err(gwion->env, pos, (a), ## __VA_ARGS__); } -#define GWION_ERR_B(pos, a,...) { GWION_ERR(pos, (a), ## __VA_ARGS__); return GW_ERROR; } -#define GWION_ERR_O(pos, a,...) { GWION_ERR(pos, (a), ## __VA_ARGS__); return NULL; } -#define ENV_ERR_B(pos, a,...) { env_err(env, pos, (a), ## __VA_ARGS__); return GW_ERROR; } -#define ENV_ERR_O(pos, a,...) { env_err(env, pos, (a), ## __VA_ARGS__); return NULL; } +#define GWI_ERR(a, ...) \ + { env_err(gwi->gwion->env, gwi->loc, (a), ##__VA_ARGS__); } +#define GWI_ERR_B(a, ...) \ + { \ + GWI_ERR((a), ##__VA_ARGS__); \ + return GW_ERROR; \ + } +#define GWI_ERR_O(a, ...) \ + { \ + GWI_ERR((a), ##__VA_ARGS__); \ + return NULL; \ + } +#define GWION_ERR(pos, a, ...) \ + { env_err(gwion->env, pos, (a), ##__VA_ARGS__); } +#define GWION_ERR_B(pos, a, ...) \ + { \ + GWION_ERR(pos, (a), ##__VA_ARGS__); \ + return GW_ERROR; \ + } +#define GWION_ERR_O(pos, a, ...) \ + { \ + GWION_ERR(pos, (a), ##__VA_ARGS__); \ + return NULL; \ + } +#define ENV_ERR_B(pos, a, ...) \ + { \ + env_err(env, pos, (a), ##__VA_ARGS__); \ + return GW_ERROR; \ + } +#define ENV_ERR_O(pos, a, ...) \ + { \ + env_err(env, pos, (a), ##__VA_ARGS__); \ + return NULL; \ + } ANN void gwi_body(const Gwi, const Ast); ANN Exp make_exp(const Gwi gwi, const m_str type, const m_str name); ANN void gwi_reset(const Gwi gwi); -ANN VM* gwi_vm(const Gwi); +ANN VM * gwi_vm(const Gwi); ANN m_bool gwi_gack(const Gwi gwi, const Type type, const f_gack d); #endif diff --git a/include/import/item.h b/include/import/item.h index 46a0cd69..5b197dcc 100644 --- a/include/import/item.h +++ b/include/import/item.h @@ -3,6 +3,7 @@ ANN m_int gwi_item_ini(const Gwi gwi, const m_str type, const m_str name); ANN2(1) m_int gwi_item_end(const Gwi gwi, const ae_flag flag, union value_data); -#define gwi_item_end(a, b, member, c) gwi_item_end(a, (ae_flag)(b), (union value_data){.member=c}) -ANN void ck_clean_item(MemPool, ImportCK*); +#define gwi_item_end(a, b, member, c) \ + gwi_item_end(a, (ae_flag)(b), (union value_data) {.member = c}) +ANN void ck_clean_item(MemPool, ImportCK *); #endif diff --git a/include/import/oper.h b/include/import/oper.h index f7514865..3e196e2d 100644 --- a/include/import/oper.h +++ b/include/import/oper.h @@ -1,19 +1,20 @@ #ifndef __IMPORT_OPER #define __IMPORT_OPER -ANN2(1) m_int gwi_oper_ini(const Gwi gwi, const m_str l, const m_str r, const m_str t); +ANN2(1) +m_int gwi_oper_ini(const Gwi gwi, const m_str l, const m_str r, const m_str t); ANN m_int gwi_oper_add(const Gwi gwi, const opck); ANN m_int gwi_oper_emi(const Gwi gwi, const opem); ANN2(1) m_int gwi_oper_end(const Gwi gwi, const m_str op, const f_instr f); -ANN m_int gwi_oper_cond(const Gwi, const m_str, const f_instr, const f_instr); -ANN void gwi_oper_eff(const Gwi gwi, const m_str effect); -#define _EQUALITY_OPER(sz, sign) \ - POP_REG(shred, sz*2 - SZ_INT); \ - *(m_uint*)REG(-SZ_INT) = sign \ - memcmp(shred->reg + SZ_INT, shred->reg + SZ_INT + sz, sz); \ +ANN m_int gwi_oper_cond(const Gwi, const m_str, const f_instr, const f_instr); +ANN void gwi_oper_eff(const Gwi gwi, const m_str effect); +#define _EQUALITY_OPER(sz, sign) \ + POP_REG(shred, sz * 2 - SZ_INT); \ + *(m_uint *)REG(-SZ_INT) = \ + sign memcmp(shred->reg + SZ_INT, shred->reg + SZ_INT + sz, sz); -#define EQUALITY_OPER(name, sz) \ -static INSTR(name##_eq) { _EQUALITY_OPER(sz, !) } \ -static INSTR(name##_ne) { _EQUALITY_OPER(sz, !!) } +#define EQUALITY_OPER(name, sz) \ + static INSTR(name##_eq) { _EQUALITY_OPER(sz, !) } \ + static INSTR(name##_ne) { _EQUALITY_OPER(sz, !!) } #endif diff --git a/include/import/special.h b/include/import/special.h index b655823c..09392a7c 100644 --- a/include/import/special.h +++ b/include/import/special.h @@ -3,12 +3,12 @@ #include "pass.h" -#define FREEARG(a) ANN void a(Instr instr NUSED, void *gwion NUSED) -typedef void (*f_freearg)(Instr, void*); +#define FREEARG(a) ANN void a(Instr instr NUSED, void *gwion NUSED) +typedef void (*f_freearg)(Instr, void *); ANN void gwi_register_freearg(const Gwi, const f_instr, const f_freearg); ANN void gwi_register_pass(const Gwi, const m_str, const compilation_pass); ANN void gwi_reserve(const Gwi, const m_str); -typedef struct SpecialId_* SpecialId; +typedef struct SpecialId_ *SpecialId; ANN void gwi_specialid(const Gwi gwi, const m_str id, const SpecialId); ANN void gwi_set_loc(const Gwi, const m_str, const uint); #endif diff --git a/include/import/tdef.h b/include/import/tdef.h index e766419b..fb19235e 100644 --- a/include/import/tdef.h +++ b/include/import/tdef.h @@ -1,7 +1,8 @@ #ifndef __IMPORT_TYPEDEF #define __IMPORT_TYPEDEF -ANN m_int gwi_typedef_ini(const Gwi gwi, const restrict m_str type, const restrict m_str name); -ANN Type gwi_typedef_end(const Gwi gwi, const ae_flag flag); -ANN void ck_clean_tdef(MemPool, ImportCK*); +ANN m_int gwi_typedef_ini(const Gwi gwi, const restrict m_str type, + const restrict m_str name); +ANN Type gwi_typedef_end(const Gwi gwi, const ae_flag flag); +ANN void ck_clean_tdef(MemPool, ImportCK *); #endif diff --git a/include/import/type.h b/include/import/type.h index 814a58a4..736c9126 100644 --- a/include/import/type.h +++ b/include/import/type.h @@ -1,8 +1,10 @@ #ifndef __IMPORT_TYPE #define __IMPORT_TYPE -ANN2(1,2) ANEW Type gwi_mk_type(const Gwi, const m_str, const m_uint, const m_str); -ANN void gwi_add_type(const Gwi gwi, Type type); -ANN void gwi_set_global_type(const Gwi gwi, const Type type, const type_enum te); +ANN2(1, 2) +ANEW Type gwi_mk_type(const Gwi, const m_str, const m_uint, const m_str); +ANN void gwi_add_type(const Gwi gwi, Type type); +ANN void gwi_set_global_type(const Gwi gwi, const Type type, + const type_enum te); #endif diff --git a/include/import/udef.h b/include/import/udef.h index dc25503c..89ffa3e7 100644 --- a/include/import/udef.h +++ b/include/import/udef.h @@ -2,8 +2,9 @@ #define __IMPORT_UNION ANN2(1) m_int gwi_union_ini(const Gwi gwi, const m_str name); -ANN m_int gwi_union_add(const Gwi gwi, const __restrict__ m_str type, const __restrict__ m_str name); -ANN Type gwi_union_end(const Gwi gwi, const ae_flag flag); -ANN void ck_clean_udef(MemPool, ImportCK*); +ANN m_int gwi_union_add(const Gwi gwi, const __restrict__ m_str type, + const __restrict__ m_str name); +ANN Type gwi_union_end(const Gwi gwi, const ae_flag flag); +ANN void ck_clean_udef(MemPool, ImportCK *); #endif diff --git a/include/instr.h b/include/instr.h index e31356e3..8cc6d2a5 100644 --- a/include/instr.h +++ b/include/instr.h @@ -7,17 +7,13 @@ #define MEM(a) (shred->mem + (a)) #define REG(a) (shred->reg + (a)) -#define INSTR(a) __attribute__((hot))\ -ANN2(1) void a(const VM_Shred shred NUSED, const Instr instr NUSED) +#define INSTR(a) \ + __attribute__((hot)) ANN2(1) void a(const VM_Shred shred NUSED, \ + const Instr instr NUSED) -enum Kind { - KIND_INT, - KIND_FLOAT, - KIND_OTHER, - KIND_ADDR -}; +enum Kind { KIND_INT, KIND_FLOAT, KIND_OTHER, KIND_ADDR }; -typedef struct Instr_ * Instr; +typedef struct Instr_ *Instr; typedef void (*f_instr)(const VM_Shred, const Instr); struct InstrVal { @@ -29,15 +25,16 @@ struct Instr_ { m_uint opcode; union { m_float f; - m_uint m_val; + m_uint m_val; }; union { - m_uint m_val2; + m_uint m_val2; struct InstrVal udata; }; void (*execute)(const VM_Shred shred, const Instr instr); }; -#define BYTECODE_SZ ((2*sizeof(unsigned)) + sizeof(struct Instr_) - SZ_INT*2) +#define BYTECODE_SZ \ + ((2 * sizeof(unsigned)) + sizeof(struct Instr_) - SZ_INT * 2) INSTR(EOC); INSTR(DTOR_EOC); @@ -63,15 +60,15 @@ INSTR(DotTmpl); INSTR(GTmpl); struct dottmpl_ { - size_t len; - m_str name; - Func_Def base, def; - Type owner_class; - Nspc owner; + size_t len; + m_str name; + Func_Def base, def; + Type owner_class; + Nspc owner; Type_List tl; - void* xfun;// (type is f_xfun) + void * xfun; // (type is f_xfun) }; -ANN void free_dottmpl(struct dottmpl_*); +ANN void free_dottmpl(struct dottmpl_ *); ANN m_bool traverse_dot_tmpl(const Emitter emit, const struct dottmpl_ *dt); INSTR(SetFunc); diff --git a/include/match.h b/include/match.h index 4a092db9..480421fb 100644 --- a/include/match.h +++ b/include/match.h @@ -5,35 +5,34 @@ struct Match_ { struct Vector_ vec; }; -ANN static inline void match_map(struct Match_ * const match, Exp e) { +ANN static inline void match_map(struct Match_ *const match, Exp e) { const Vector cond = &match->cond; vector_init(cond); Exp next; do { - next = e->next; + next = e->next; e->next = NULL; vector_add(cond, (vtype)e); - } while((e = next)); + } while ((e = next)); } -ANN static inline void match_unmap(struct Match_ * const match) { +ANN static inline void match_unmap(struct Match_ *const match) { const Vector cond = &match->cond; - const vtype sz = vector_size(cond); - for(m_uint i = 0; i < sz -1; ++i) { - const Exp e = (Exp)vector_at(cond, i), - next = (Exp)vector_at(cond, i + 1); + const vtype sz = vector_size(cond); + for (m_uint i = 0; i < sz - 1; ++i) { + const Exp e = (Exp)vector_at(cond, i), next = (Exp)vector_at(cond, i + 1); e->next = next; } vector_release(cond); } -#define MATCH_INI(scope) \ - struct Match_ *former = scope->match, m = {}; \ - scope->match = &m; \ +#define MATCH_INI(scope) \ + struct Match_ *former = scope->match, m = {}; \ + scope->match = &m; \ match_map(&m, stmt->cond); -#define MATCH_END(scope) \ - match_unmap(scope->match); \ +#define MATCH_END(scope) \ + match_unmap(scope->match); \ scope->match = former; #endif diff --git a/include/memoize.h b/include/memoize.h index c95e3b2d..a926adff 100644 --- a/include/memoize.h +++ b/include/memoize.h @@ -1,9 +1,9 @@ #ifndef __MEMOIZE #define __MEMOIZE -typedef struct Memoize_ * Memoize; -Memoize memoize_ini(const Emitter, const Func); -void memoize_end(MemPool, Memoize); +typedef struct Memoize_ *Memoize; +Memoize memoize_ini(const Emitter, const Func); +void memoize_end(MemPool, Memoize); INSTR(MemoizeIni); INSTR(MemoizeStore); #endif diff --git a/include/object.h b/include/object.h index b7891efb..07b88771 100644 --- a/include/object.h +++ b/include/object.h @@ -1,59 +1,68 @@ #ifndef __OBJECT #define __OBJECT -typedef struct M_Object_ * M_Object; +typedef struct M_Object_ *M_Object; struct M_Object_ { - m_bit* data; - Type type_ref; - struct Vector_ vtable; + m_bit * data; + Type type_ref; + struct Vector_ vtable; volatile size_t ref; }; -ANN void instantiate_object(const VM_Shred, const Type); -ANN void free_object(MemPool p, const M_Object); -ANEW M_Object new_object(MemPool, const VM_Shred, const Type); -ANEW struct UGen_* new_UGen(MemPool); -ANEW M_Object new_M_UGen(const struct Gwion_*); -ANN void fork_clean(const VM_Shred, const Vector); -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(const struct Gwion_*, const VM_Shred, const m_str); +ANN void instantiate_object(const VM_Shred, const Type); +ANN void free_object(MemPool p, const M_Object); +ANEW M_Object new_object(MemPool, const VM_Shred, const Type); +ANEW struct UGen_ *new_UGen(MemPool); +ANEW M_Object new_M_UGen(const struct Gwion_ *); +ANN void fork_clean(const VM_Shred, const Vector); +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(const struct Gwion_ *, const VM_Shred, const m_str); ANEW M_Object new_shred(const VM_Shred); -ANN void fork_launch(const M_Object, const m_uint); -ANN void __release(const M_Object, const VM_Shred); -ANN void broadcast(const M_Object); +ANN void fork_launch(const M_Object, const m_uint); +ANN void __release(const M_Object, const VM_Shred); +ANN void broadcast(const M_Object); -#define STRING(o) (*(m_str*) ((M_Object)o)->data) -#define ME(o) (*(VM_Shred*) ((M_Object)o)->data) -#define EV_SHREDS(o) (*(struct Vector_*) ((M_Object)o)->data) -#define UGEN(o) (*(UGen*) ((M_Object)o)->data) -#define ARRAY(o) (*(M_Vector*) ((M_Object)o)->data) -#define IO_FILE(o) (*(FILE**) (((M_Object)o)->data + SZ_INT)) +#define STRING(o) (*(m_str *)((M_Object)o)->data) +#define ME(o) (*(VM_Shred *)((M_Object)o)->data) +#define EV_SHREDS(o) (*(struct Vector_ *)((M_Object)o)->data) +#define UGEN(o) (*(UGen *)((M_Object)o)->data) +#define ARRAY(o) (*(M_Vector *)((M_Object)o)->data) +#define IO_FILE(o) (*(FILE **)(((M_Object)o)->data + SZ_INT)) -static inline void _release(const restrict M_Object obj, const restrict VM_Shred shred) { - if(!--obj->ref)__release(obj, shred); +static inline void _release(const restrict M_Object obj, + const restrict VM_Shred shred) { + if (!--obj->ref) __release(obj, shred); } -static inline void release(const restrict M_Object obj, const restrict VM_Shred shred) { - if(obj)_release(obj, shred); +static inline void release(const restrict M_Object obj, + const restrict VM_Shred shred) { + if (obj) _release(obj, shred); } -typedef void (f_release)(const VM_Shred shred, const Type t NUSED, const m_bit* ptr); -#define RELEASE_FUNC(a) void (a)(const VM_Shred shred, const Type t NUSED, const m_bit* ptr) -static inline RELEASE_FUNC(object_release) { release(*(M_Object*)ptr, shred); } +typedef void(f_release)(const VM_Shred shred, const Type t NUSED, + const m_bit *ptr); +#define RELEASE_FUNC(a) \ + void(a)(const VM_Shred shred, const Type t NUSED, const m_bit *ptr) +static inline RELEASE_FUNC(object_release) { release(*(M_Object *)ptr, shred); } RELEASE_FUNC(struct_release); -static inline void struct_addref(const Gwion gwion, const Type type, const m_bit* ptr) { - for(m_uint i = 0; i < vector_size(&type->info->tuple->types); ++i) { +static inline void struct_addref(const Gwion gwion, const Type type, + const m_bit *ptr) { + for (m_uint i = 0; i < vector_size(&type->info->tuple->types); ++i) { const Type t = (Type)vector_at(&type->info->tuple->types, i); - if(isa(t, gwion->type[et_object]) > 0) { - const M_Object o = *(M_Object*)(ptr + vector_at(&type->info->tuple->offset, i)); + if (isa(t, gwion->type[et_object]) > 0) { + const M_Object o = + *(M_Object *)(ptr + vector_at(&type->info->tuple->offset, i)); ++o->ref; - } else if(tflag(t, tflag_struct)) { - struct_addref(gwion, t, *(m_bit**)(ptr + vector_at(&type->info->tuple->offset, i))); + } else if (tflag(t, tflag_struct)) { + struct_addref( + gwion, t, + *(m_bit **)(ptr + vector_at(&type->info->tuple->offset, i))); } } } -static inline void compound_release(const VM_Shred shred, const Type t, const m_bit* ptr) { - if(!tflag(t, tflag_struct)) +static inline void compound_release(const VM_Shred shred, const Type t, + const m_bit *ptr) { + if (!tflag(t, tflag_struct)) object_release(shred, t, ptr); else struct_release(shred, t, ptr); diff --git a/include/opcode.h b/include/opcode.h index 96d7997b..aaa6b6d1 100644 --- a/include/opcode.h +++ b/include/opcode.h @@ -202,203 +202,203 @@ enum { eDotTmplVal, }; -#define RegSetImm (f_instr)eRegSetImm -#define RegPushImm (f_instr)eRegPushImm -#define RegPushImm2 (f_instr)eRegPushImm2 -#define RegPushImm3 (f_instr)eRegPushImm3 -#define RegPushImm4 (f_instr)eRegPushImm4 -#define RegPushMem (f_instr)eRegPushMem -#define RegPushMem2 (f_instr)eRegPushMem2 -#define RegPushMem3 (f_instr)eRegPushMem3 -#define RegPushMem4 (f_instr)eRegPushMem4 -#define RegPushMemDeref (f_instr)eRegPushMemDeref -#define RegPushNow (f_instr)eRegPushNow -#define RegPushBase (f_instr)eRegPushBase -#define RegPushBase2 (f_instr)eRegPushBase2 -#define RegPushBase3 (f_instr)eRegPushBase3 -#define RegPushBase4 (f_instr)eRegPushBase4 -#define Reg2Reg (f_instr)eReg2Reg -#define Reg2RegOther (f_instr)eReg2RegOther -#define Reg2RegAddr (f_instr)eReg2RegAddr -#define Reg2RegDeref (f_instr)eReg2RegDeref -#define StructMember (f_instr)eStructMember -#define StructMemberFloat (f_instr)eStructMemberFloat -#define StructMemberOther (f_instr)eStructMemberOther -#define StructMemberAddr (f_instr)eStructMemberAddr -#define MemSetImm (f_instr)eMemSetImm -#define MemAddImm (f_instr)eMemAddImm -#define RepeatIdx (f_instr)eRepeatIdx -#define Repeat (f_instr)eRepeat -#define RegPushMe (f_instr)eRegPushMe -#define RegPushMaybe (f_instr)eRegPushMaybe -#define FuncReturn (f_instr)eFuncReturn -#define Goto (f_instr)eGoto -#define AllocWord (f_instr)eAllocWord -#define AllocWord2 (f_instr)eAllocWord2 -#define AllocWord3 (f_instr)eAllocWord3 -#define int_plus (f_instr)eint_plus -#define int_minus (f_instr)eint_minus -#define int_mul (f_instr)eint_mul -#define int_div (f_instr)eint_div -#define int_modulo (f_instr)eint_modulo -#define int_eq (f_instr)eint_eq -#define int_neq (f_instr)eint_neq -#define int_and (f_instr)eint_and -#define int_or (f_instr)eint_or -#define int_gt (f_instr)eint_gt -#define int_ge (f_instr)eint_ge -#define int_lt (f_instr)eint_lt -#define int_le (f_instr)eint_le -#define int_sl (f_instr)eint_sl -#define int_sr (f_instr)eint_sr -#define int_sand (f_instr)eint_sand -#define int_sor (f_instr)eint_sor -#define int_xor (f_instr)eint_xor -#define int_negate (f_instr)eint_negate -#define IntNot (f_instr)eIntNot -#define int_cmp (f_instr)eint_cmp -#define int_r_assign (f_instr)eint_r_assign -#define int_r_plus (f_instr)eint_r_plus -#define int_r_minus (f_instr)eint_r_minus -#define int_r_mul (f_instr)eint_r_mul -#define int_r_div (f_instr)eint_r_div -#define int_r_modulo (f_instr)eint_r_modulo -#define int_r_sl (f_instr)eint_r_sl -#define int_r_sr (f_instr)eint_r_sr -#define int_r_sand (f_instr)eint_r_sand -#define int_r_sor (f_instr)eint_r_sor -#define int_r_sxor (f_instr)eint_r_sxor -#define int_pre_inc (f_instr)eint_pre_inc -#define int_pre_dec (f_instr)eint_pre_dec -#define int_post_inc (f_instr)eint_post_inc -#define int_post_dec (f_instr)eint_post_dec -#define FloatPlus (f_instr)eFloatPlus -#define FloatMinus (f_instr)eFloatMinus -#define FloatTimes (f_instr)eFloatTimes -#define FloatDivide (f_instr)eFloatDivide -#define float_and (f_instr)efloat_and -#define float_or (f_instr)efloat_or -#define float_eq (f_instr)efloat_eq -#define float_neq (f_instr)efloat_neq -#define float_gt (f_instr)efloat_gt -#define float_ge (f_instr)efloat_ge -#define float_lt (f_instr)efloat_lt -#define float_le (f_instr)efloat_le -#define float_negate (f_instr)efloat_negate -#define float_not (f_instr)efloat_not -#define float_r_assign (f_instr)efloat_r_assign -#define float_r_plus (f_instr)efloat_r_plus -#define float_r_minus (f_instr)efloat_r_minus -#define float_r_mul (f_instr)efloat_r_mul -#define float_r_div (f_instr)efloat_r_div -#define int_float_plus (f_instr)eint_float_plus -#define int_float_minus (f_instr)eint_float_minus -#define int_float_mul (f_instr)eint_float_mul -#define int_float_div (f_instr)eint_float_div -#define int_float_and (f_instr)eint_float_and -#define int_float_or (f_instr)eint_float_or -#define int_float_eq (f_instr)eint_float_eq -#define int_float_neq (f_instr)eint_float_neq -#define int_float_gt (f_instr)eint_float_gt -#define int_float_ge (f_instr)eint_float_ge -#define int_float_lt (f_instr)eint_float_lt -#define int_float_le (f_instr)eint_float_le -#define int_float_r_assign (f_instr)eint_float_r_assign -#define int_float_r_plus (f_instr)eint_float_r_plus -#define int_float_r_minus (f_instr)eint_float_r_minus -#define int_float_r_mul (f_instr)eint_float_r_mul -#define int_float_r_div (f_instr)eint_float_r_div -#define float_int_plus (f_instr)efloat_int_plus -#define float_int_minus (f_instr)efloat_int_minus -#define float_int_mul (f_instr)efloat_int_mul -#define float_int_div (f_instr)efloat_int_div -#define float_int_and (f_instr)efloat_int_and -#define float_int_or (f_instr)efloat_int_or -#define float_int_eq (f_instr)efloat_int_eq -#define float_int_neq (f_instr)efloat_int_neq -#define float_int_gt (f_instr)efloat_int_gt -#define float_int_ge (f_instr)efloat_int_ge -#define float_int_lt (f_instr)efloat_int_lt -#define float_int_le (f_instr)efloat_int_le -#define float_int_r_assign (f_instr)efloat_int_r_assign -#define float_int_r_plus (f_instr)efloat_int_r_plus -#define float_int_r_minus (f_instr)efloat_int_r_minus -#define float_int_r_mul (f_instr)efloat_int_r_mul -#define float_int_r_div (f_instr)efloat_int_r_div -#define CastI2F (f_instr)eCastI2F -#define CastF2I (f_instr)eCastF2I -#define Time_Advance (f_instr)eTime_Advance -#define Recurs (f_instr)eRecurs -#define SetCode (f_instr)eSetCode -#define RegMove (f_instr)eRegMove -#define Reg2Mem (f_instr)eReg2Mem -#define Reg2Mem4 (f_instr)eReg2Mem4 -#define Overflow (f_instr)eOverflow -#define FuncUsrEnd (f_instr)eFuncUsrEnd -#define FuncUsrEnd2 (f_instr)eFuncUsrEnd2 -#define FuncMemberEnd (f_instr)eFuncMemberEnd -#define SporkIni (f_instr)eSporkIni -#define ForkIni (f_instr)eForkIni -#define SporkFunc (f_instr)eSporkFunc -#define SporkMemberFptr (f_instr)eSporkMemberFptr -#define SporkExp (f_instr)eSporkExp -#define SporkEnd (f_instr)eSporkEnd -#define BranchEqInt (f_instr)eBranchEqInt -#define BranchNeqInt (f_instr)eBranchNeqInt -#define BranchEqFloat (f_instr)eBranchEqFloat -#define BranchNeqFloat (f_instr)eBranchNeqFloat -#define Unroll (f_instr)eUnroll -#define ArrayAppend (f_instr)eArrayAppend -#define AutoUnrollInit (f_instr)eAutoUnrollInit -#define AutoLoop (f_instr)eAutoLoop -#define ArrayTop (f_instr)eArrayTop -#define ArrayAccess (f_instr)eArrayAccess -#define ArrayGet (f_instr)eArrayGet -#define ArrayAddr (f_instr)eArrayAddr -#define ObjectInstantiate (f_instr)eObjectInstantiate -#define RegAddRef (f_instr)eRegAddRef -#define RegAddRefAddr (f_instr)eRegAddRefAddr -#define StructRegAddRef (f_instr)eStructRegAddRef -#define StructRegAddRefAddr (f_instr)eStructRegAddRefAddr -#define ObjectAssign (f_instr)eObjectAssign -#define Assign (f_instr)eAssign -#define ObjectRelease (f_instr)eObjectRelease -#define GWOP_EXCEPT (f_instr)eGWOP_EXCEPT -#define AllocMember4 (f_instr)eAllocMember4 -#define DotMember (f_instr)eDotMember -#define DotMember2 (f_instr)eDotMember2 -#define DotMember3 (f_instr)eDotMember3 -#define DotMember4 (f_instr)eDotMember4 -#define UnionCheck (f_instr)eUnionCheck -#define UnionMember (f_instr)eUnionMember -#define UnionMember2 (f_instr)eUnionMember2 -#define UnionMember3 (f_instr)eUnionMember3 -#define UnionMember4 (f_instr)eUnionMember4 -#define DotStatic (f_instr)eDotStatic -#define DotStatic2 (f_instr)eDotStatic2 -#define DotStatic3 (f_instr)eDotStatic3 -#define UpvalueInt (f_instr)eUpvalueInt -#define UpvalueFloat (f_instr)eUpvalueFloat -#define UpvalueOther (f_instr)eUpvalueOther -#define UpvalueAddr (f_instr)eUpvalueAddr -#define DotFunc (f_instr)eDotFunc -#define GcIni (f_instr)eGcIni -#define GcAdd (f_instr)eGcAdd -#define GcEnd (f_instr)eGcEnd -#define GackType (f_instr)eGackType -#define GackEnd (f_instr)eGackEnd -#define Gack (f_instr)eGack -#define TryIni (f_instr)eTryIni -#define TryEnd (f_instr)eTryEnd -#define HandleEffect (f_instr)eHandleEffect -#define PerformEffect (f_instr)ePerformEffect -#define NoOp (f_instr)eNoOp -#define DebugLine (f_instr)eDebugLine -#define DebugValue (f_instr)eDebugValue -#define DebugPush (f_instr)eDebugPush -#define DebugPop (f_instr)eDebugPop -#define EOC (f_instr)eEOC -#define Unroll2 (f_instr)eUnroll2 -#define OP_MAX (f_instr)eOP_MAX -#define DotTmplVal (f_instr)eDotTmplVal +#define RegSetImm (f_instr) eRegSetImm +#define RegPushImm (f_instr) eRegPushImm +#define RegPushImm2 (f_instr) eRegPushImm2 +#define RegPushImm3 (f_instr) eRegPushImm3 +#define RegPushImm4 (f_instr) eRegPushImm4 +#define RegPushMem (f_instr) eRegPushMem +#define RegPushMem2 (f_instr) eRegPushMem2 +#define RegPushMem3 (f_instr) eRegPushMem3 +#define RegPushMem4 (f_instr) eRegPushMem4 +#define RegPushMemDeref (f_instr) eRegPushMemDeref +#define RegPushNow (f_instr) eRegPushNow +#define RegPushBase (f_instr) eRegPushBase +#define RegPushBase2 (f_instr) eRegPushBase2 +#define RegPushBase3 (f_instr) eRegPushBase3 +#define RegPushBase4 (f_instr) eRegPushBase4 +#define Reg2Reg (f_instr) eReg2Reg +#define Reg2RegOther (f_instr) eReg2RegOther +#define Reg2RegAddr (f_instr) eReg2RegAddr +#define Reg2RegDeref (f_instr) eReg2RegDeref +#define StructMember (f_instr) eStructMember +#define StructMemberFloat (f_instr) eStructMemberFloat +#define StructMemberOther (f_instr) eStructMemberOther +#define StructMemberAddr (f_instr) eStructMemberAddr +#define MemSetImm (f_instr) eMemSetImm +#define MemAddImm (f_instr) eMemAddImm +#define RepeatIdx (f_instr) eRepeatIdx +#define Repeat (f_instr) eRepeat +#define RegPushMe (f_instr) eRegPushMe +#define RegPushMaybe (f_instr) eRegPushMaybe +#define FuncReturn (f_instr) eFuncReturn +#define Goto (f_instr) eGoto +#define AllocWord (f_instr) eAllocWord +#define AllocWord2 (f_instr) eAllocWord2 +#define AllocWord3 (f_instr) eAllocWord3 +#define int_plus (f_instr) eint_plus +#define int_minus (f_instr) eint_minus +#define int_mul (f_instr) eint_mul +#define int_div (f_instr) eint_div +#define int_modulo (f_instr) eint_modulo +#define int_eq (f_instr) eint_eq +#define int_neq (f_instr) eint_neq +#define int_and (f_instr) eint_and +#define int_or (f_instr) eint_or +#define int_gt (f_instr) eint_gt +#define int_ge (f_instr) eint_ge +#define int_lt (f_instr) eint_lt +#define int_le (f_instr) eint_le +#define int_sl (f_instr) eint_sl +#define int_sr (f_instr) eint_sr +#define int_sand (f_instr) eint_sand +#define int_sor (f_instr) eint_sor +#define int_xor (f_instr) eint_xor +#define int_negate (f_instr) eint_negate +#define IntNot (f_instr) eIntNot +#define int_cmp (f_instr) eint_cmp +#define int_r_assign (f_instr) eint_r_assign +#define int_r_plus (f_instr) eint_r_plus +#define int_r_minus (f_instr) eint_r_minus +#define int_r_mul (f_instr) eint_r_mul +#define int_r_div (f_instr) eint_r_div +#define int_r_modulo (f_instr) eint_r_modulo +#define int_r_sl (f_instr) eint_r_sl +#define int_r_sr (f_instr) eint_r_sr +#define int_r_sand (f_instr) eint_r_sand +#define int_r_sor (f_instr) eint_r_sor +#define int_r_sxor (f_instr) eint_r_sxor +#define int_pre_inc (f_instr) eint_pre_inc +#define int_pre_dec (f_instr) eint_pre_dec +#define int_post_inc (f_instr) eint_post_inc +#define int_post_dec (f_instr) eint_post_dec +#define FloatPlus (f_instr) eFloatPlus +#define FloatMinus (f_instr) eFloatMinus +#define FloatTimes (f_instr) eFloatTimes +#define FloatDivide (f_instr) eFloatDivide +#define float_and (f_instr) efloat_and +#define float_or (f_instr) efloat_or +#define float_eq (f_instr) efloat_eq +#define float_neq (f_instr) efloat_neq +#define float_gt (f_instr) efloat_gt +#define float_ge (f_instr) efloat_ge +#define float_lt (f_instr) efloat_lt +#define float_le (f_instr) efloat_le +#define float_negate (f_instr) efloat_negate +#define float_not (f_instr) efloat_not +#define float_r_assign (f_instr) efloat_r_assign +#define float_r_plus (f_instr) efloat_r_plus +#define float_r_minus (f_instr) efloat_r_minus +#define float_r_mul (f_instr) efloat_r_mul +#define float_r_div (f_instr) efloat_r_div +#define int_float_plus (f_instr) eint_float_plus +#define int_float_minus (f_instr) eint_float_minus +#define int_float_mul (f_instr) eint_float_mul +#define int_float_div (f_instr) eint_float_div +#define int_float_and (f_instr) eint_float_and +#define int_float_or (f_instr) eint_float_or +#define int_float_eq (f_instr) eint_float_eq +#define int_float_neq (f_instr) eint_float_neq +#define int_float_gt (f_instr) eint_float_gt +#define int_float_ge (f_instr) eint_float_ge +#define int_float_lt (f_instr) eint_float_lt +#define int_float_le (f_instr) eint_float_le +#define int_float_r_assign (f_instr) eint_float_r_assign +#define int_float_r_plus (f_instr) eint_float_r_plus +#define int_float_r_minus (f_instr) eint_float_r_minus +#define int_float_r_mul (f_instr) eint_float_r_mul +#define int_float_r_div (f_instr) eint_float_r_div +#define float_int_plus (f_instr) efloat_int_plus +#define float_int_minus (f_instr) efloat_int_minus +#define float_int_mul (f_instr) efloat_int_mul +#define float_int_div (f_instr) efloat_int_div +#define float_int_and (f_instr) efloat_int_and +#define float_int_or (f_instr) efloat_int_or +#define float_int_eq (f_instr) efloat_int_eq +#define float_int_neq (f_instr) efloat_int_neq +#define float_int_gt (f_instr) efloat_int_gt +#define float_int_ge (f_instr) efloat_int_ge +#define float_int_lt (f_instr) efloat_int_lt +#define float_int_le (f_instr) efloat_int_le +#define float_int_r_assign (f_instr) efloat_int_r_assign +#define float_int_r_plus (f_instr) efloat_int_r_plus +#define float_int_r_minus (f_instr) efloat_int_r_minus +#define float_int_r_mul (f_instr) efloat_int_r_mul +#define float_int_r_div (f_instr) efloat_int_r_div +#define CastI2F (f_instr) eCastI2F +#define CastF2I (f_instr) eCastF2I +#define Time_Advance (f_instr) eTime_Advance +#define Recurs (f_instr) eRecurs +#define SetCode (f_instr) eSetCode +#define RegMove (f_instr) eRegMove +#define Reg2Mem (f_instr) eReg2Mem +#define Reg2Mem4 (f_instr) eReg2Mem4 +#define Overflow (f_instr) eOverflow +#define FuncUsrEnd (f_instr) eFuncUsrEnd +#define FuncUsrEnd2 (f_instr) eFuncUsrEnd2 +#define FuncMemberEnd (f_instr) eFuncMemberEnd +#define SporkIni (f_instr) eSporkIni +#define ForkIni (f_instr) eForkIni +#define SporkFunc (f_instr) eSporkFunc +#define SporkMemberFptr (f_instr) eSporkMemberFptr +#define SporkExp (f_instr) eSporkExp +#define SporkEnd (f_instr) eSporkEnd +#define BranchEqInt (f_instr) eBranchEqInt +#define BranchNeqInt (f_instr) eBranchNeqInt +#define BranchEqFloat (f_instr) eBranchEqFloat +#define BranchNeqFloat (f_instr) eBranchNeqFloat +#define Unroll (f_instr) eUnroll +#define ArrayAppend (f_instr) eArrayAppend +#define AutoUnrollInit (f_instr) eAutoUnrollInit +#define AutoLoop (f_instr) eAutoLoop +#define ArrayTop (f_instr) eArrayTop +#define ArrayAccess (f_instr) eArrayAccess +#define ArrayGet (f_instr) eArrayGet +#define ArrayAddr (f_instr) eArrayAddr +#define ObjectInstantiate (f_instr) eObjectInstantiate +#define RegAddRef (f_instr) eRegAddRef +#define RegAddRefAddr (f_instr) eRegAddRefAddr +#define StructRegAddRef (f_instr) eStructRegAddRef +#define StructRegAddRefAddr (f_instr) eStructRegAddRefAddr +#define ObjectAssign (f_instr) eObjectAssign +#define Assign (f_instr) eAssign +#define ObjectRelease (f_instr) eObjectRelease +#define GWOP_EXCEPT (f_instr) eGWOP_EXCEPT +#define AllocMember4 (f_instr) eAllocMember4 +#define DotMember (f_instr) eDotMember +#define DotMember2 (f_instr) eDotMember2 +#define DotMember3 (f_instr) eDotMember3 +#define DotMember4 (f_instr) eDotMember4 +#define UnionCheck (f_instr) eUnionCheck +#define UnionMember (f_instr) eUnionMember +#define UnionMember2 (f_instr) eUnionMember2 +#define UnionMember3 (f_instr) eUnionMember3 +#define UnionMember4 (f_instr) eUnionMember4 +#define DotStatic (f_instr) eDotStatic +#define DotStatic2 (f_instr) eDotStatic2 +#define DotStatic3 (f_instr) eDotStatic3 +#define UpvalueInt (f_instr) eUpvalueInt +#define UpvalueFloat (f_instr) eUpvalueFloat +#define UpvalueOther (f_instr) eUpvalueOther +#define UpvalueAddr (f_instr) eUpvalueAddr +#define DotFunc (f_instr) eDotFunc +#define GcIni (f_instr) eGcIni +#define GcAdd (f_instr) eGcAdd +#define GcEnd (f_instr) eGcEnd +#define GackType (f_instr) eGackType +#define GackEnd (f_instr) eGackEnd +#define Gack (f_instr) eGack +#define TryIni (f_instr) eTryIni +#define TryEnd (f_instr) eTryEnd +#define HandleEffect (f_instr) eHandleEffect +#define PerformEffect (f_instr) ePerformEffect +#define NoOp (f_instr) eNoOp +#define DebugLine (f_instr) eDebugLine +#define DebugValue (f_instr) eDebugValue +#define DebugPush (f_instr) eDebugPush +#define DebugPop (f_instr) eDebugPop +#define EOC (f_instr) eEOC +#define Unroll2 (f_instr) eUnroll2 +#define OP_MAX (f_instr) eOP_MAX +#define DotTmplVal (f_instr) eDotTmplVal #endif diff --git a/include/operator.h b/include/operator.h index d90afd79..a75fa009 100644 --- a/include/operator.h +++ b/include/operator.h @@ -2,36 +2,67 @@ #define __OPERATOR #define OP_ANY_TYPE (Type)1 -#define ERR_N(a, b, ...) do { env_err(env, (a), (b), ## __VA_ARGS__); return env->gwion->type[et_error]; } while(0) -#define DECL_ON(decl, f, exp) decl f exp; do { if(!f) return env->gwion->type[et_error]; } while(0) -#define DECL_BN(decl, f, exp) decl f exp; do { if(f < 0) return env->gwion->type[et_error]; } while(0) -#define DECL_NN(decl, f, exp) decl f exp; do { if(f == env->gwion->type[et_error]) return env->gwion->type[et_error]; } while(0) -#define CHECK_ON(f) do { if(!f) return env->gwion->type[et_error]; } while(0) -#define CHECK_NB(f) do { if(f == env->gwion->type[et_error]) return GW_ERROR; } while(0) -#define CHECK_NO(f) do { if(f == env->gwion->type[et_error]) return NULL; } while(0) -#define CHECK_BN(f) do { if(f < 0) return env->gwion->type[et_error]; } while(0) -#define CHECK_NN(f) do { if(f == env->gwion->type[et_error]) return env->gwion->type[et_error]; } while(0) +#define ERR_N(a, b, ...) \ + do { \ + env_err(env, (a), (b), ##__VA_ARGS__); \ + return env->gwion->type[et_error]; \ + } while (0) +#define DECL_ON(decl, f, exp) \ + decl f exp; \ + do { \ + if (!f) return env->gwion->type[et_error]; \ + } while (0) +#define DECL_BN(decl, f, exp) \ + decl f exp; \ + do { \ + if (f < 0) return env->gwion->type[et_error]; \ + } while (0) +#define DECL_NN(decl, f, exp) \ + decl f exp; \ + do { \ + if (f == env->gwion->type[et_error]) return env->gwion->type[et_error]; \ + } while (0) +#define CHECK_ON(f) \ + do { \ + if (!f) return env->gwion->type[et_error]; \ + } while (0) +#define CHECK_NB(f) \ + do { \ + if (f == env->gwion->type[et_error]) return GW_ERROR; \ + } while (0) +#define CHECK_NO(f) \ + do { \ + if (f == env->gwion->type[et_error]) return NULL; \ + } while (0) +#define CHECK_BN(f) \ + do { \ + if (f < 0) return env->gwion->type[et_error]; \ + } while (0) +#define CHECK_NN(f) \ + do { \ + if (f == env->gwion->type[et_error]) return env->gwion->type[et_error]; \ + } while (0) -typedef Type (*opck)(const Env, void*); -typedef m_bool (*opem)(const Emitter, void*); +typedef Type (*opck)(const Env, void *); +typedef m_bool (*opem)(const Emitter, void *); struct Op_Func { - opck ck; - opem em; + opck ck; + opem em; struct Vector_ effect; }; struct Op_Import { - Type lhs; - Type rhs; - Type ret; + Type lhs; + Type rhs; + Type ret; const struct Op_Func *func; - uintptr_t data; + uintptr_t data; // used to return nspc in op_check. // could be put in a union with `ret` - Nspc nspc; + Nspc nspc; Symbol op; - loc_t pos; + loc_t pos; }; struct Implicit { @@ -41,28 +72,26 @@ struct Implicit { }; struct TemplateScan { - const Type t; - const Type_Decl *td; + const Type t; + const Type_Decl *td; }; -ANN void op_cpy(const Env env, const struct Op_Import* opi); -ANN m_bool add_op(const Gwion gwion, 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, struct Gwion_* gwion); - +ANN void op_cpy(const Env env, const struct Op_Import *opi); +ANN m_bool add_op(const Gwion gwion, 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, struct Gwion_ *gwion); -ANN void operator_suspend(const Nspc, struct Op_Import*); +ANN void operator_suspend(const Nspc, struct Op_Import *); ANN static inline void operator_resume(struct Op_Import *opi) { - *(uintptr_t*)opi->ret = opi->data; + *(uintptr_t *)opi->ret = opi->data; } ANN static inline void set_decl_ref(const Exp e) { - if(e->exp_type == ae_exp_decl) + if (e->exp_type == ae_exp_decl) SET_FLAG(e->d.exp_decl.list->self->value, late); } - ANN void func_operator(const Func_Def fdef, struct Op_Import *opi); #endif diff --git a/include/parse.h b/include/parse.h index 98a6166e..bde830d2 100644 --- a/include/parse.h +++ b/include/parse.h @@ -5,72 +5,81 @@ #define insert_symbol(a) insert_symbol(env->gwion->st, (a)) #undef ERR_B -#define ERR_B(a, b, ...) { env_err(env, (a), (b), ## __VA_ARGS__); return GW_ERROR; } +#define ERR_B(a, b, ...) \ + { \ + env_err(env, (a), (b), ##__VA_ARGS__); \ + return GW_ERROR; \ + } #undef ERR_O -#define ERR_O(a, b, ...) { env_err(env, (a), (b), ## __VA_ARGS__); return NULL; } - -#define RET_NSPC(exp) \ -++env->scope->depth; \ -nspc_push_value(env->gwion->mp, env->curr); \ -const m_bool ret = exp; \ -nspc_pop_value(env->gwion->mp, env->curr); \ ---env->scope->depth; \ -return ret; - -#define SET_ACCESS(a,b) \ -if(GET_FLAG(a, private)) \ - SET_FLAG(b, private); \ -else if(GET_FLAG(a, protect)) \ - SET_FLAG(b, protect); - -#define HANDLE_SECTION_FUNC(prefix, type, Arg) \ -DECL_SECTION_FUNC(prefix, type, Arg) \ -ANN static inline type prefix##_section(const Arg a, /* const */ Section* section) { \ - void* d = §ion->d.stmt_list; \ - return prefix##_section_func[section->section_type](a, *(void**)d); \ -} - -#define HANDLE_EXP_FUNC(prefix, type, Arg) \ -DECL_EXP_FUNC(prefix, type, Arg) \ -ANN type prefix##_exp(const Arg arg, Exp exp) { \ - do CHECK_BB(prefix##_exp_func[exp->exp_type](arg, &exp->d)); \ - while((exp = exp->next)); \ - return GW_OK; \ -} +#define ERR_O(a, b, ...) \ + { \ + env_err(env, (a), (b), ##__VA_ARGS__); \ + return NULL; \ + } + +#define RET_NSPC(exp) \ + ++env->scope->depth; \ + nspc_push_value(env->gwion->mp, env->curr); \ + const m_bool ret = exp; \ + nspc_pop_value(env->gwion->mp, env->curr); \ + --env->scope->depth; \ + return ret; + +#define SET_ACCESS(a, b) \ + if (GET_FLAG(a, private)) \ + SET_FLAG(b, private); \ + else if (GET_FLAG(a, protect)) \ + SET_FLAG(b, protect); + +#define HANDLE_SECTION_FUNC(prefix, type, Arg) \ + DECL_SECTION_FUNC(prefix, type, Arg) \ + ANN static inline type prefix##_section(const Arg a, \ + /* const */ Section *section) { \ + void *d = §ion->d.stmt_list; \ + return prefix##_section_func[section->section_type](a, *(void **)d); \ + } + +#define HANDLE_EXP_FUNC(prefix, type, Arg) \ + DECL_EXP_FUNC(prefix, type, Arg) \ + ANN type prefix##_exp(const Arg arg, Exp exp) { \ + do CHECK_BB(prefix##_exp_func[exp->exp_type](arg, &exp->d)); \ + while ((exp = exp->next)); \ + return GW_OK; \ + } ANN m_bool scan1_exp(const Env, Exp); ANN m_bool scan2_exp(const Env, Exp); -#define describe_stmt_func(prefix, name, type, prolog, exp) \ -ANN static m_bool prefix##_stmt_##name(const Env env, const type stmt) { \ - RET_NSPC(exp) \ -} +#define describe_stmt_func(prefix, name, type, prolog, exp) \ + ANN static m_bool prefix##_stmt_##name(const Env env, const type stmt) { \ + RET_NSPC(exp) \ + } ANN m_bool check_stmt(const Env env, const Stmt stmt); -typedef m_bool (*_exp_func)(const void*, const void*); -ANN m_bool scanx_body(const Env env, const Class_Def cdef, const _exp_func f, void* data); -static inline ANN m_bool env_body(const Env env, const Class_Def cdef, const _exp_func f) { +typedef m_bool (*_exp_func)(const void *, const void *); +ANN m_bool scanx_body(const Env env, const Class_Def cdef, const _exp_func f, + void *data); +static inline ANN m_bool env_body(const Env env, const Class_Def cdef, + const _exp_func f) { return scanx_body(env, cdef, f, env); } -#define env_body(a,b,c) env_body(a,b,(_exp_func)c) +#define env_body(a, b, c) env_body(a, b, (_exp_func)c) -ANN m_bool scanx_cdef(const Env, void *, const Type, - const _exp_func f_cdef, const _exp_func f_union); +ANN m_bool scanx_cdef(const Env, void *, const Type, const _exp_func f_cdef, + const _exp_func f_union); -#define xxx_cdef(prefix) \ -static inline m_bool prefix##_cdef(const Env env, const Type t) { \ - return scanx_cdef(env, env, t, \ - (_exp_func)prefix##_class_def, (_exp_func)prefix##_union_def); \ -} +#define xxx_cdef(prefix) \ + static inline m_bool prefix##_cdef(const Env env, const Type t) { \ + return scanx_cdef(env, env, t, (_exp_func)prefix##_class_def, \ + (_exp_func)prefix##_union_def); \ + } -xxx_cdef(scan0) -xxx_cdef(scan1) -xxx_cdef(scan2) -xxx_cdef(check) -xxx_cdef(traverse) +xxx_cdef(scan0) xxx_cdef(scan1) xxx_cdef(scan2) xxx_cdef(check) + xxx_cdef(traverse) -ANN m_bool scanx_fdef(const Env, void *, const Func_Def, const _exp_func); + ANN m_bool + scanx_fdef(const Env, void *, const Func_Def, const _exp_func); ANN m_bool check_subscripts(const Env, const Array_Sub, const m_bool is_decl); ANN m_bool check_implicit(const Env env, const Exp e, const Type t); @@ -78,16 +87,15 @@ ANN m_bool ensure_traverse(const Env env, const Type t); ANN m_bool check_traverse_fdef(const Env env, const Func_Def fdef); ANN static inline void env_weight(const Env env, const uint16_t weight) { - if(env->func) + if (env->func) env->func->weight += weight; - else if(env->class_def) + else if (env->class_def) env->class_def->weight += weight; - else if(env->context) + else if (env->context) env->context->weight += weight; } ANN static inline void env_inline_mult(const Env env, const float mult) { - if(env->func) - env->func->inline_mult += mult; + if (env->func) env->func->inline_mult += mult; } #endif diff --git a/include/pass.h b/include/pass.h index 119a1cbf..631a229f 100644 --- a/include/pass.h +++ b/include/pass.h @@ -3,7 +3,7 @@ typedef union __attribute__((__transparent_union__)) { struct Ast_ *ast; - m_bool *ret; + m_bool * ret; } PassArg; struct Passes_ { @@ -13,9 +13,9 @@ struct Passes_ { // change this to gwion ? typedef m_bool (*compilation_pass)(const Env, PassArg); -ANEW ANN struct Passes_* new_passes(const Gwion); -ANN void free_passes(MemPool mp, struct Passes_*); -ANN void pass_register(const Gwion, const m_str, const compilation_pass); -ANN void pass_default(const Gwion); +ANEW ANN struct Passes_ *new_passes(const Gwion); +ANN void free_passes(MemPool mp, struct Passes_ *); +ANN void pass_register(const Gwion, const m_str, const compilation_pass); +ANN void pass_default(const Gwion); ANN m_bool pass_set(const Gwion, const Vector); #endif diff --git a/include/plug.h b/include/plug.h index bc258ac2..07b2e53f 100644 --- a/include/plug.h +++ b/include/plug.h @@ -1,22 +1,26 @@ #ifndef __PLUG #define __PLUG -ANN m_bool plug_ini(const struct Gwion_*, const Vector); -ANN m_bool driver_ini(const struct Gwion_*); -ANN void plug_run(const struct Gwion_*, const Map); -ANN void free_plug(const struct Gwion_*); -ANN void* get_module(const struct Gwion_*, const m_str); -ANN void set_module(const struct Gwion_ *gwion, const m_str name, void *const ptr); +ANN m_bool plug_ini(const struct Gwion_ *, const Vector); +ANN m_bool driver_ini(const struct Gwion_ *); +ANN void plug_run(const struct Gwion_ *, const Map); +ANN void free_plug(const struct Gwion_ *); +ANN void * get_module(const struct Gwion_ *, const m_str); +ANN void set_module(const struct Gwion_ *gwion, const m_str name, + void *const ptr); ANN m_bool plugin_ini(struct Gwion_ *gwion, const m_str iname); -#define GWIMPORT_NAME import -#define GWMODINI_NAME gwmodini -#define GWMODEND_NAME gwmodend -#define GWDRIVER_NAME gwdriver -#define GWDEPEND_NAME gwdepend -#define GWMODINI(a) ANN2(1) void* GWMODINI_NAME(const struct Gwion_ *gwion NUSED, const Vector args NUSED) -#define GWMODEND(a) ANN void GWMODEND_NAME(const struct Gwion_ *gwion NUSED, void* self NUSED) -#define GWDRIVER(a) ANN void GWDRIVER_NAME(DriverData* d) -#define GWDEPEND ANN m_str const* GWDEPEND_NAME(void) +#define GWIMPORT_NAME import +#define GWMODINI_NAME gwmodini +#define GWMODEND_NAME gwmodend +#define GWDRIVER_NAME gwdriver +#define GWDEPEND_NAME gwdepend +#define GWMODINI(a) \ + ANN2(1) \ + void *GWMODINI_NAME(const struct Gwion_ *gwion NUSED, const Vector args NUSED) +#define GWMODEND(a) \ + ANN void GWMODEND_NAME(const struct Gwion_ *gwion NUSED, void *self NUSED) +#define GWDRIVER(a) ANN void GWDRIVER_NAME(DriverData *d) +#define GWDEPEND ANN m_str const *GWDEPEND_NAME(void) #endif diff --git a/include/shreduler_private.h b/include/shreduler_private.h index a5e5fe6b..96899348 100644 --- a/include/shreduler_private.h +++ b/include/shreduler_private.h @@ -1,12 +1,12 @@ #ifndef __SHREDULER #define __SHREDULER -struct Shreduler_ { - struct BBQ_ *bbq; +struct Shreduler_ { + struct BBQ_ * bbq; struct ShredTick_ *list; struct ShredTick_ *curr; - struct Vector_ shreds; - MUTEX_TYPE mutex; - size_t shred_ids; - bool loop; + struct Vector_ shreds; + MUTEX_TYPE mutex; + size_t shred_ids; + bool loop; }; #endif diff --git a/include/soundinfo.h b/include/soundinfo.h index 5fa57b5a..fe9b8c83 100644 --- a/include/soundinfo.h +++ b/include/soundinfo.h @@ -1,9 +1,9 @@ #ifndef __SOUNDINFO #define __SOUNDINFO struct SoundInfo_ { - m_str arg; + m_str arg; uint32_t sr; - uint8_t in, out; + uint8_t in, out; }; struct SoundInfo_ *new_soundinfo(MemPool p); struct SoundInfo_ *soundinfo_cpy(MemPool p, const struct SoundInfo_ *src); diff --git a/include/specialid.h b/include/specialid.h index b0ed5707..c707944e 100644 --- a/include/specialid.h +++ b/include/specialid.h @@ -1,27 +1,28 @@ #ifndef __SPECIALID #define __SPECIALID -typedef Type (*idck)(const Env, const Exp_Primary*); -typedef struct Instr_* (*idem)(const Emitter, const Exp_Primary*); +typedef Type (*idck)(const Env, const Exp_Primary *); +typedef struct Instr_ *(*idem)(const Emitter, const Exp_Primary *); struct SpecialId_ { - Type type; - idck ck; + Type type; + idck ck; f_instr exec; - idem em; - bool is_const; + idem em; + bool is_const; }; -#define ID_CHECK(a) ANN Type a(const Env env NUSED, const Exp_Primary* prim NUSED) -#define ID_EMIT(a) ANN Instr a(const Emitter emit NUSED, const Exp_Primary* prim NUSED) +#define ID_CHECK(a) \ + ANN Type a(const Env env NUSED, const Exp_Primary *prim NUSED) +#define ID_EMIT(a) \ + ANN Instr a(const Emitter emit NUSED, const Exp_Primary *prim NUSED) -ANN static inline Type specialid_type(const Env env, - struct SpecialId_ *spid, const Exp_Primary* prim) { +ANN static inline Type specialid_type(const Env env, struct SpecialId_ *spid, + const Exp_Primary *prim) { exp_self(prim)->type = spid->type; - if(spid->is_const) - exp_setmeta(exp_self(prim), 1); + if (spid->is_const) exp_setmeta(exp_self(prim), 1); return spid->ck ? spid->ck(env, prim) : spid->type; } -ANN struct SpecialId_* specialid_get(const Gwion, const Symbol); +ANN struct SpecialId_ *specialid_get(const Gwion, const Symbol); #endif diff --git a/include/template.h b/include/template.h index df61fe13..a91f5be6 100644 --- a/include/template.h +++ b/include/template.h @@ -1,7 +1,11 @@ #ifndef __TEMPLATE #define __TEMPLATE -ANN m_bool template_push_types(const Env, const Tmpl*); +ANN m_bool template_push_types(const Env, const Tmpl *); ANN m_bool template_push(const Env env, const Type t); -ANN Tmpl* mk_tmpl(const Env, const Tmpl*, const Type_List); -#define POP_RET(a) { nspc_pop_type(env->gwion->mp, env->curr); return (a); } +ANN Tmpl *mk_tmpl(const Env, const Tmpl *, const Type_List); +#define POP_RET(a) \ + { \ + nspc_pop_type(env->gwion->mp, env->curr); \ + return (a); \ + } #endif diff --git a/include/tmp_resolve.h b/include/tmp_resolve.h index fd0bde35..fe9729be 100644 --- a/include/tmp_resolve.h +++ b/include/tmp_resolve.h @@ -1,5 +1,6 @@ #ifndef __TMPL_RESOLVE #define __TMPL_RESOLVE -ANN Func find_template_match(const Env env, const Value value, Exp_Call *const exp); +ANN Func find_template_match(const Env env, const Value value, + Exp_Call *const exp); ANN Func find_func_match(const Env env, const Func up, Exp_Call *const exp); #endif diff --git a/include/tmpl_info.h b/include/tmpl_info.h index 9f46f95d..39b7e26d 100644 --- a/include/tmpl_info.h +++ b/include/tmpl_info.h @@ -3,12 +3,12 @@ struct tmpl_info { Symbol name; Specialized_List list; - const Type_Decl* td; + const Type_Decl *td; Type ret; Type base; struct Vector_ type; struct Vector_ size; - uint8_t index; + uint8_t index; }; ANN Type tmpl_exists(const Env env, struct tmpl_info *const info); diff --git a/include/traverse.h b/include/traverse.h index 7c841d0c..69dfa680 100644 --- a/include/traverse.h +++ b/include/traverse.h @@ -25,7 +25,7 @@ ANN m_bool check_func_def(const Env, const Func_Def); ANN m_bool scan0_fptr_def(const Env, const Fptr_Def); ANN m_bool scan1_fptr_def(const Env, const Fptr_Def); ANN m_bool scan2_fptr_def(const Env, const Fptr_Def); -//ANN m_bool check_fptr_def(const Env, const Fptr); +// ANN m_bool check_fptr_def(const Env, const Fptr); ANN m_bool scan0_union_def(const Env, const Union_Def); ANN m_bool scan1_union_def(const Env, const Union_Def); @@ -34,7 +34,7 @@ ANN m_bool check_union_def(const Env, const Union_Def); ANN m_bool scan0_enum_def(const Env, const Enum_Def); ANN m_bool scan1_enum_def(const Env, const Enum_Def); -//ANN m_bool scan2_enum_def(const Env, const Enum_Def); +// ANN m_bool scan2_enum_def(const Env, const Enum_Def); ANN m_bool check_enum_def(const Env, const Enum_Def); ANN m_bool scan0_type_def(const Env, const Type_Def); diff --git a/include/ugen.h b/include/ugen.h index 305be4f7..afa2adbd 100644 --- a/include/ugen.h +++ b/include/ugen.h @@ -1,48 +1,49 @@ #ifndef __UGEN #define __UGEN -typedef struct UGen_ * UGen; +typedef struct UGen_ *UGen; typedef void (*f_tick)(const UGen ug) ANN; -//typedef void (*f_ugop)(const UGen, const m_float) ANN; +// typedef void (*f_ugop)(const UGen, const m_float) ANN; struct ugen_net_ { struct Vector_ from; struct Vector_ to; - uint16_t size; + uint16_t size; }; struct ugen_multi_ { - M_Object* channel; - uint8_t n_in; - uint8_t n_out; - uint8_t n_chan; + M_Object *channel; + uint8_t n_in; + uint8_t n_out; + uint8_t n_chan; }; struct ugen_gen { f_tick tick; - void* data; + void * data; UGen trig; }; struct UGen_ { f_tick compute; union { - struct ugen_net_ *net; - struct ugen_multi_* multi; + struct ugen_net_ * net; + struct ugen_multi_ *multi; } connect; union { struct ugen_gen gen; - UGen ref; + UGen ref; } module; m_float in, out; uint8_t op; - bool multi; - bool done; + bool multi; + bool done; }; #define TICK(a) __attribute__((hot)) ANN inline void a(const UGen u) -ANN void ugen_ini(const struct Gwion_*, const UGen, const uint, const uint); -ANN void ugen_gen(const struct Gwion_*, const UGen, const f_tick, void*, const m_bool); +ANN void ugen_ini(const struct Gwion_ *, const UGen, const uint, const uint); +ANN void ugen_gen(const struct Gwion_ *, 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/vararg.h b/include/vararg.h index 14dbdf14..bf3aac31 100644 --- a/include/vararg.h +++ b/include/vararg.h @@ -1,11 +1,11 @@ #ifndef __VARARG #define __VARARG struct Vararg_ { - struct Vector_ t; // types - m_bit *d; // d(ata) - m_uint /*o, i, s,*/ l; // o(ffset), i(ndex), s(ize), l(en) - m_uint pc; + struct Vector_ t; // types + m_bit * d; // d(ata) + m_uint /*o, i, s,*/ l; // o(ffset), i(ndex), s(ize), l(en) + m_uint pc; }; ANN void emit_vararg_end(const Emitter emit, const m_uint pc); -void free_vararg(MemPool p, struct Vararg_* arg); +void free_vararg(MemPool p, struct Vararg_ *arg); #endif diff --git a/include/vm.h b/include/vm.h index 1150ae41..6ab8a31f 100644 --- a/include/vm.h +++ b/include/vm.h @@ -2,129 +2,137 @@ #define __VM typedef struct Closure_ { - m_bit *data; + m_bit * data; struct Map_ m; - m_uint sz; + m_uint sz; } Closure; -ANN Closure* new_closure(MemPool mp, const m_uint sz); -ANN void free_closure(Closure *a, const Gwion gwion); +ANN Closure *new_closure(MemPool mp, const m_uint sz); +ANN void free_closure(Closure *a, const Gwion gwion); -typedef struct VM_Code_* VM_Code; +typedef struct VM_Code_ *VM_Code; struct VM_Code_ { m_bit *bytecode; union { struct Vector_ instr; - m_uint native_func; + m_uint native_func; }; size_t stack_depth; - Type ret_type; // could be `struct Vector_ tmpl_types;` + Type ret_type; // could be `struct Vector_ tmpl_types;` union { - void* memoize; + void * memoize; Closure *closure; }; struct Map_ handlers; - m_str name; - uint16_t ref; - ae_flag flag; - bool builtin; - bool callback; - bool is_memoize; + m_str name; + uint16_t ref; + ae_flag flag; + bool builtin; + bool callback; + bool is_memoize; }; typedef struct frame_t { uint32_t push, pc; - VM_Code code; + VM_Code code; } frame_t; -typedef struct Shreduler_* Shreduler; -typedef struct Emitter_ * Emitter; -typedef struct VM_Shred_* VM_Shred; +typedef struct Shreduler_ *Shreduler; +typedef struct Emitter_ * Emitter; +typedef struct VM_Shred_ * VM_Shred; typedef struct VMDebugger_ { struct VM_ *vm; -// struct VM_Shred *shred; + // struct VM_Shred *shred; struct Map_ map; // code, Vector - bool step; + bool step; } VMDebugger; typedef struct VM_ { - Shreduler shreduler; + Shreduler shreduler; struct Vector_ ugen; - struct BBQ_* bbq; - struct Gwion_* gwion; - VM_Shred cleaner_shred; - struct VM_ *parent; - uint32_t rand[2]; - VMDebugger *dbg; + struct BBQ_ * bbq; + struct Gwion_ *gwion; + VM_Shred cleaner_shred; + struct VM_ * parent; + uint32_t rand[2]; + VMDebugger * dbg; } VM; typedef struct ShredDebugger_ { struct Vector_ breakpoint; struct Vector_ line; - struct Scope_ scope; + struct Scope_ scope; } ShredDebugger; struct ShredInfo_ { - VM* vm; - MemPool mp; - struct M_Object_* me; - VM_Code orig; - struct Vector_ frame; - struct Vector_ args; - struct Vector_ line; - ShredDebugger *dbg; + VM * vm; + MemPool mp; + struct M_Object_ *me; + VM_Code orig; + struct Vector_ frame; + struct Vector_ args; + struct Vector_ line; + ShredDebugger * dbg; }; struct ShredTick_ { - VM_Shred self; + VM_Shred self; struct ShredTick_ *prev; struct ShredTick_ *next; struct ShredTick_ *parent; - struct Vector_ child; - Shreduler shreduler; - size_t xid; - m_float wake_time; + struct Vector_ child; + Shreduler shreduler; + size_t xid; + m_float wake_time; }; struct VM_Shred_ { - VM_Code code; - m_bit* reg; - m_bit* mem; - m_bit* base; - size_t pc; - struct Vector_ gc; - struct ShredTick_ * tick; - struct ShredInfo_ * info; + VM_Code code; + m_bit * reg; + m_bit * mem; + m_bit * base; + size_t pc; + struct Vector_ gc; + struct ShredTick_ *tick; + struct ShredInfo_ *info; }; REF_FUNC(VM_Code, vmcode) -ANN2(1,5) ANEW VM_Code new_vmcode(MemPool p, const Vector instr, const m_uint stack_depth, const int builtin, const m_str name); +ANN2(1, 5) +ANEW VM_Code new_vmcode(MemPool p, const Vector instr, const m_uint stack_depth, + const int builtin, const m_str name); ANN ANEW VM_Code vmcode_callback(MemPool p, const VM_Code code); ANN VM_Shred shreduler_get(const Shreduler s) __attribute__((hot)); -ANN void shreduler_remove(const Shreduler s, const VM_Shred out, const bool erase)__attribute__((hot)); -ANN void shredule(const Shreduler s, const VM_Shred shred, const m_float wake_time)__attribute__((hot)); -ANN void shreduler_set_loop(const Shreduler s, const bool loop); -ANN void shreduler_ini(const Shreduler s, const VM_Shred shred); -ANN void shreduler_add(const Shreduler s, const VM_Shred shred); +ANN void shreduler_remove(const Shreduler s, const VM_Shred out, + const bool erase) __attribute__((hot)); +ANN void shredule(const Shreduler s, const VM_Shred shred, + const m_float wake_time) __attribute__((hot)); +ANN void shreduler_set_loop(const Shreduler s, const bool loop); +ANN void shreduler_ini(const Shreduler s, const VM_Shred shred); +ANN void shreduler_add(const Shreduler s, const VM_Shred shred); -ANEW ANN VM_Shred new_vm_shred(MemPool, const VM_Code code) __attribute__((hot)); -ANEW ANN VM_Shred new_shred_base(const VM_Shred, 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, true); } -void free_vm_shred(const VM_Shred shred)__attribute__((hot, nonnull)); +ANEW ANN VM_Shred new_vm_shred(MemPool, const VM_Code code) + __attribute__((hot)); +ANEW ANN VM_Shred new_shred_base(const VM_Shred, 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, true); +} +void free_vm_shred(const VM_Shred shred) __attribute__((hot, nonnull)); -ANN void vm_run(const VM* vm) __attribute__((hot)); -ANEW VM* new_vm(MemPool, const bool); -ANN void vm_lock(VM const*); -ANN void vm_unlock(VM const*); -ANN bool vm_running(VM const*); -ANN void free_vm(VM* vm); -ANN void vm_ini_shred(const VM* vm, const VM_Shred shred)__attribute__((hot)); -ANN void 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)); +ANN void vm_run(const VM *vm) __attribute__((hot)); +ANEW VM * new_vm(MemPool, const bool); +ANN void vm_lock(VM const *); +ANN void vm_unlock(VM const *); +ANN bool vm_running(VM const *); +ANN void free_vm(VM *vm); +ANN void vm_ini_shred(const VM *vm, const VM_Shred shred) __attribute__((hot)); +ANN void 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)); ANN m_str code_name_set(MemPool p, const m_str, const m_str); ANN m_str code_name(const m_str, const bool); ANN uint32_t gw_rand(uint32_t s[2]); -ANN void gw_seed(uint32_t s[2], const uint64_t); -ANN void handle(VM_Shred shred, const m_str effect); +ANN void gw_seed(uint32_t s[2], const uint64_t); +ANN void handle(VM_Shred shred, const m_str effect); #endif diff --git a/src/arg.c b/src/arg.c index 133d7ea6..85df3182 100644 --- a/src/arg.c +++ b/src/arg.c @@ -12,32 +12,42 @@ #define GWIONRC ".gwionrc" enum { - CONFIG, PLUGIN, MODULE, - LOOP, PASS, STDIN, COLOR, -// sound options - DRIVER, SRATE, NINPUT, NOUTPUT, -// pp options - DEFINE, UNDEF, INCLUDE, - DEBUG, CDOC, + CONFIG, + PLUGIN, + MODULE, + LOOP, + PASS, + STDIN, + COLOR, + // sound options + DRIVER, + SRATE, + NINPUT, + NOUTPUT, + // pp options + DEFINE, + UNDEF, + INCLUDE, + DEBUG, + CDOC, NOPTIONS }; /* use before MemPool allocation */ ANN static inline void config_end(const Vector config) { - for(m_uint i = 0; i < vector_size(config); ++i) { + 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)); + for (m_uint i = 1; i < vector_size(v); ++i) xfree((m_str)vector_at(v, i)); vector_release(v); xfree(v); } } ANN static m_str plug_dir(void) { - const m_str home = getenv("HOME"); - const size_t sz = strlen(home); - const m_str pdir = "/.gwplug"; - m_str plug_dir = (m_str)xmalloc(sz + strlen(pdir) + 1); + const m_str home = getenv("HOME"); + const size_t sz = strlen(home); + const m_str pdir = "/.gwplug"; + m_str plug_dir = (m_str)xmalloc(sz + strlen(pdir) + 1); strcpy(plug_dir, home); strcpy(plug_dir + sz, pdir); return plug_dir; @@ -53,7 +63,7 @@ enum arg_type { ARG_CDOC }; -ANN static void arg_init(Arg* arg) { +ANN static void arg_init(Arg *arg) { map_init(&arg->mod); vector_init(&arg->add); vector_init(&arg->lib); @@ -62,7 +72,7 @@ ANN static void arg_init(Arg* arg) { arg->color = COLOR_AUTO; } -ANN void arg_release(Arg* arg) { +ANN void arg_release(Arg *arg) { map_release(&arg->mod); vector_release(&arg->add); xfree((m_str)vector_front(&arg->lib)); @@ -72,10 +82,8 @@ ANN void arg_release(Arg* arg) { } static inline bool str2bool(const char *str) { - if(!str || !strcmp(str, "true")) - return true; - if(!strcmp(str, "false")) - return false; + if (!str || !strcmp(str, "true")) return true; + if (!strcmp(str, "false")) return false; return atoi(str) ? true : false; } @@ -91,26 +99,26 @@ ANN static inline void get_cdoc(const Gwion gwion, const char *cdoc) { ANN void arg_compile(const Gwion gwion, Arg *arg) { const Vector v = &arg->add; - for(m_uint i = 0; i < vector_size(v); i++) { - switch(vector_at(v, i)) { - case ARG_FILE: - compile_filename(gwion, (m_str)VPTR(v, ++i)); - break; - case ARG_STDIN: - compile_file(gwion, "stdin", stdin); - break; - case ARG_DEFINE: - pparg_add(gwion->ppa, (m_str)VPTR(v, ++i)); - break; - case ARG_UNDEF: - pparg_rem(gwion->ppa, (m_str)VPTR(v, ++i)); - break; - case ARG_INCLUDE: - pparg_inc(gwion->ppa, (m_str)VPTR(v, ++i)); - break; - case ARG_DEBUG: - get_debug(gwion, (m_str)VPTR(v, ++i)); - break; + for (m_uint i = 0; i < vector_size(v); i++) { + switch (vector_at(v, i)) { + case ARG_FILE: + compile_filename(gwion, (m_str)VPTR(v, ++i)); + break; + case ARG_STDIN: + compile_file(gwion, "stdin", stdin); + break; + case ARG_DEFINE: + pparg_add(gwion->ppa, (m_str)VPTR(v, ++i)); + break; + case ARG_UNDEF: + pparg_rem(gwion->ppa, (m_str)VPTR(v, ++i)); + break; + case ARG_INCLUDE: + pparg_inc(gwion->ppa, (m_str)VPTR(v, ++i)); + break; + case ARG_DEBUG: + get_debug(gwion, (m_str)VPTR(v, ++i)); + break; } } } @@ -118,134 +126,84 @@ ANN void arg_compile(const Gwion gwion, Arg *arg) { ANN2(1) static inline void arg_set_pass(const Gwion gwion, const char *str) { const Vector v = split_args(gwion->mp, (const m_str)str); pass_set(gwion, v); - for(m_uint i = 0; i < vector_size(v); ++i) + for (m_uint i = 0; i < vector_size(v); ++i) free_mstr(gwion->mp, (m_str)vector_at(v, i)); free_vector(gwion->mp, v); } ANN2(1) static void module_arg(const Map map, const char *str) { m_str val = strchr(str, '='); - if(val) { + if (val) { *val = '\0'; ++val; } map_set(map, (vtype)str, (vtype)val); } -static void setup_options(cmdapp_t* app, cmdopt_t* opt) { - cmdapp_set(app, - 'C', "config", - CMDOPT_TAKESARG, NULL, - "parse a config file", &opt[CONFIG] - ); - cmdapp_set(app, - 'p', "plugdir", - CMDOPT_TAKESARG, NULL, - "add ARG to the plugin search path", &opt[PLUGIN] - ); - cmdapp_set(app, - 'm', "module", - CMDOPT_TAKESARG, NULL, - "activate module (and arguments)", &opt[MODULE] - ); - cmdapp_set(app, - 'l', "loop", - CMDOPT_TAKESARG, NULL, - "set loop mode", &opt[LOOP] - ); - cmdapp_set(app, - 'g', "passes", - CMDOPT_TAKESARG, NULL, - "set pass order", &opt[PASS] - ); - cmdapp_set(app, - '\0', "stdin", - 0, NULL, - "read from stdin", &opt[STDIN] - ); - cmdapp_set(app, - 'c', "color", - CMDOPT_TAKESARG, NULL, - "set colored output (never/auto/always, defaults to auto)", &opt[COLOR] - ); -// sound options - cmdapp_set(app, - 'd', "driver", - CMDOPT_TAKESARG, NULL, - "set driver (and arguments)", &opt[DRIVER] - ); - cmdapp_set(app, - 's', "samplerate", - CMDOPT_TAKESARG, NULL, - "set the samplerate", &opt[SRATE] - ); - cmdapp_set(app, - 'i', "input", - CMDOPT_TAKESARG, NULL, - "number of input channel", &opt[NINPUT] - ); - cmdapp_set(app, - 'o', "output", - CMDOPT_TAKESARG, NULL, - "number of output channel", &opt[NOUTPUT] - ); - cmdapp_set(app, - 'D', "define", - CMDOPT_TAKESARG, NULL, - "define a macro", &opt[DEFINE] - ); - cmdapp_set(app, - 'U', "undef", - CMDOPT_TAKESARG, NULL, - "undefine a macro", &opt[UNDEF] - ); - cmdapp_set(app, - 'I', "include", - CMDOPT_TAKESARG, NULL, - "add ARG to include path", &opt[INCLUDE] - ); - cmdapp_set(app, - 'G', "debug", - CMDOPT_MAYTAKEARG, NULL, - "set/unset debug mode", &opt[DEBUG] - ); - cmdapp_set(app, - 'H', "cdoc", - CMDOPT_MAYTAKEARG, NULL, - "set/unset cdoc mode", &opt[CDOC] - ); +static void setup_options(cmdapp_t *app, cmdopt_t *opt) { + cmdapp_set(app, 'C', "config", CMDOPT_TAKESARG, NULL, "parse a config file", + &opt[CONFIG]); + cmdapp_set(app, 'p', "plugdir", CMDOPT_TAKESARG, NULL, + "add ARG to the plugin search path", &opt[PLUGIN]); + cmdapp_set(app, 'm', "module", CMDOPT_TAKESARG, NULL, + "activate module (and arguments)", &opt[MODULE]); + cmdapp_set(app, 'l', "loop", CMDOPT_TAKESARG, NULL, "set loop mode", + &opt[LOOP]); + cmdapp_set(app, 'g', "passes", CMDOPT_TAKESARG, NULL, "set pass order", + &opt[PASS]); + cmdapp_set(app, '\0', "stdin", 0, NULL, "read from stdin", &opt[STDIN]); + cmdapp_set(app, 'c', "color", CMDOPT_TAKESARG, NULL, + "set colored output (never/auto/always, defaults to auto)", + &opt[COLOR]); + // sound options + cmdapp_set(app, 'd', "driver", CMDOPT_TAKESARG, NULL, + "set driver (and arguments)", &opt[DRIVER]); + cmdapp_set(app, 's', "samplerate", CMDOPT_TAKESARG, NULL, + "set the samplerate", &opt[SRATE]); + cmdapp_set(app, 'i', "input", CMDOPT_TAKESARG, NULL, + "number of input channel", &opt[NINPUT]); + cmdapp_set(app, 'o', "output", CMDOPT_TAKESARG, NULL, + "number of output channel", &opt[NOUTPUT]); + cmdapp_set(app, 'D', "define", CMDOPT_TAKESARG, NULL, "define a macro", + &opt[DEFINE]); + cmdapp_set(app, 'U', "undef", CMDOPT_TAKESARG, NULL, "undefine a macro", + &opt[UNDEF]); + cmdapp_set(app, 'I', "include", CMDOPT_TAKESARG, NULL, + "add ARG to include path", &opt[INCLUDE]); + cmdapp_set(app, 'G', "debug", CMDOPT_MAYTAKEARG, NULL, "set/unset debug mode", + &opt[DEBUG]); + cmdapp_set(app, 'H', "cdoc", CMDOPT_MAYTAKEARG, NULL, "set/unset cdoc mode", + &opt[CDOC]); } -static inline void add2arg(Arg *const arg, const char *data, const enum arg_type type) { +static inline void add2arg(Arg *const arg, const char *data, + const enum arg_type type) { vector_add(&arg->add, type); vector_add(&arg->add, (vtype)data); } ANN static void split_line(const m_str line, const Vector v) { m_str d = strdup(line), c = d; - while(d) { - const m_str str = strsep(&d, " "); - const size_t sz = strlen(str); - const m_bool arg = (str[sz-1] == '\n'); + while (d) { + const m_str str = strsep(&d, " "); + const size_t sz = strlen(str); + const m_bool arg = (str[sz - 1] == '\n'); vector_add(v, (vtype)strndup(str, arg ? sz - 1 : sz)); } xfree(d); xfree(c); } - ANN static Vector get_config(const char *name) { - char *line = NULL; - size_t len = 0; - ssize_t nread; - FILE *f = fopen(name, "r"); + char * line = NULL; + size_t len = 0; + FILE * f = fopen(name, "r"); CHECK_OO(f); 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] != '#') - split_line(line, v); + while (getline(&line, &len, f) != -1) { + if (line[0] != '#') split_line(line, v); } free(line); fclose(f); @@ -254,17 +212,17 @@ ANN static Vector get_config(const char *name) { struct ArgInternal { const Gwion gwion; - Arg *arg; + Arg * arg; }; -ANN m_bool _arg_parse(struct ArgInternal* arg); +ANN m_bool _arg_parse(struct ArgInternal *arg); -ANN static void config_parse(struct ArgInternal* arg, const char *name) { +ANN static void config_parse(struct ArgInternal *arg, const char *name) { const Vector v = get_config(name); - if(v) { - struct CArg ca = arg->arg->arg; + if (v) { + struct CArg ca = arg->arg->arg; arg->arg->arg.argc = vector_size(v); - arg->arg->arg.argv = (m_str*)(v->ptr + OFFSET); + arg->arg->arg.argv = (m_str *)(v->ptr + OFFSET); _arg_parse(arg); arg->arg->arg = ca; vector_add(&arg->arg->config, (vtype)v); @@ -273,73 +231,73 @@ ANN static void config_parse(struct ArgInternal* arg, const char *name) { #define ARG2INT(a) strtol(a, NULL, 10) -static void myproc(void *data, cmdopt_t* option, const char* arg) { +static void myproc(void *data, cmdopt_t *option, const char *arg) { struct ArgInternal *arg_int = data; - Arg *_arg = arg_int->arg; - if(arg) { - if(!_arg->arg.idx) + Arg * _arg = arg_int->arg; + if (arg) { + if (!_arg->arg.idx) _arg->arg.idx++; else add2arg(_arg, arg, ARG_FILE); } else { - switch(option->shorto) { - case 'p': - vector_add(&_arg->lib, (vtype)option->value); - break; - case 'm': - module_arg(&_arg->mod, option->value); - break; - case 'C': - config_parse(arg_int, option->value); - break; - case 'l': - _arg->loop = ARG2INT(option->value) > 0 ? true : false; - break; - case 'g': - arg_set_pass(arg_int->gwion, option->value); - break; - case '\0': - vector_add(&_arg->add, (vtype)ARG_STDIN); - break; - case 'c': - if(!strcmp(option->value, "never")) - _arg->color = COLOR_NEVER; - else if(!strcmp(option->value, "auto")) - _arg->color = COLOR_AUTO; - else if(!strcmp(option->value, "always")) - _arg->color = COLOR_ALWAYS; - // ignore error silently - break; -// sound options - case 's': - _arg->si->sr = (uint32_t)ARG2INT(option->value); - break; - case 'd': - _arg->si->arg = (m_str)option->value; - break; - case 'i': - _arg->si->in = (uint8_t)ARG2INT(option->value); - break; - case 'o': - _arg->si->out = (uint8_t)ARG2INT(option->value); - break; -// pp options - case 'D': - add2arg(_arg, option->value, ARG_DEFINE); - break; - case 'U': - add2arg(_arg, option->value, ARG_UNDEF); - break; - case 'I': - add2arg(_arg, option->value, ARG_INCLUDE); - break; -// debug - case 'G': - add2arg(_arg, option->value, ARG_DEBUG); - break; - case 'H': - get_cdoc(arg_int->gwion, option->value); - break; + switch (option->shorto) { + case 'p': + vector_add(&_arg->lib, (vtype)option->value); + break; + case 'm': + module_arg(&_arg->mod, option->value); + break; + case 'C': + config_parse(arg_int, option->value); + break; + case 'l': + _arg->loop = ARG2INT(option->value) > 0 ? true : false; + break; + case 'g': + arg_set_pass(arg_int->gwion, option->value); + break; + case '\0': + vector_add(&_arg->add, (vtype)ARG_STDIN); + break; + case 'c': + if (!strcmp(option->value, "never")) + _arg->color = COLOR_NEVER; + else if (!strcmp(option->value, "auto")) + _arg->color = COLOR_AUTO; + else if (!strcmp(option->value, "always")) + _arg->color = COLOR_ALWAYS; + // ignore error silently + break; + // sound options + case 's': + _arg->si->sr = (uint32_t)ARG2INT(option->value); + break; + case 'd': + _arg->si->arg = (m_str)option->value; + break; + case 'i': + _arg->si->in = (uint8_t)ARG2INT(option->value); + break; + case 'o': + _arg->si->out = (uint8_t)ARG2INT(option->value); + break; + // pp options + case 'D': + add2arg(_arg, option->value, ARG_DEFINE); + break; + case 'U': + add2arg(_arg, option->value, ARG_UNDEF); + break; + case 'I': + add2arg(_arg, option->value, ARG_INCLUDE); + break; + // debug + case 'G': + add2arg(_arg, option->value, ARG_DEBUG); + break; + case 'H': + get_cdoc(arg_int->gwion, option->value); + break; } } } @@ -349,40 +307,40 @@ static void myproc(void *data, cmdopt_t* option, const char* arg) { #endif ANN m_bool _arg_parse(struct ArgInternal *arg) { - cmdapp_t app; + cmdapp_t app; const cmdapp_info_t info = { - .program = "gwion", - .synopses = NULL, // so it's automatic - .version = GWION_VERSION, - .author = "Jérémie Astor", - .year = 2016, - .description = "Strongly timed musical programming language.", - .help_des_offset = 28, - .ver_extra = - "License GPLv3+: GNU GPL version 3 or later \n" - "This is free software: you are free to change and redistribute it.\n" - "There is NO WARRANTY, to the extent permitted by law.\n" - }; + .program = "gwion", + .synopses = NULL, // so it's automatic + .version = GWION_VERSION, + .author = "Jérémie Astor", + .year = 2016, + .description = "Strongly timed musical programming language.", + .help_des_offset = 28, + .ver_extra = + "License GPLv3+: GNU GPL version 3 or later " + "\n" + "This is free software: you are free to change and redistribute it.\n" + "There is NO WARRANTY, to the extent permitted by law.\n"}; struct CArg *ca = &arg->arg->arg; cmdapp_init(&app, ca->argc, ca->argv, CMDAPP_MODE_SHORTARG, &info); cmdapp_enable_procedure(&app, myproc, arg); cmdopt_t opt[NOPTIONS]; setup_options(&app, opt); - if(cmdapp_run(&app) == EXIT_SUCCESS && cmdapp_should_exit(&app)) - arg->arg->quit = 1; + if (cmdapp_run(&app) == EXIT_SUCCESS && cmdapp_should_exit(&app)) + arg->arg->quit = 1; cmdapp_destroy(&app); return GW_OK; } -ANN static void config_default(struct ArgInternal* arg) { - char* home = getenv("HOME"); - char c[strlen(home) + strlen(GWIONRC) + 2]; +ANN static void config_default(struct ArgInternal *arg) { + char *home = getenv("HOME"); + char c[strlen(home) + strlen(GWIONRC) + 2]; sprintf(c, "%s/%s", home, GWIONRC); config_parse(arg, c); } -ANN m_bool arg_parse(const Gwion gwion, Arg* a) { - struct ArgInternal arg = { .gwion=gwion, .arg=a }; +ANN m_bool arg_parse(const Gwion gwion, Arg *a) { + struct ArgInternal arg = {.gwion = gwion, .arg = a}; arg_init(a); #ifdef __FUZZING return; diff --git a/src/clean.c b/src/clean.c index fb9b0764..fb4dee1a 100644 --- a/src/clean.c +++ b/src/clean.c @@ -7,76 +7,60 @@ #include "clean.h" ANN static void clean_array_sub(Clean *a, Array_Sub b) { - if(b->exp) - clean_exp(a, b->exp); + if (b->exp) clean_exp(a, b->exp); } ANN static void clean_id_list(Clean *a, ID_List b) { - if(b->next) - clean_id_list(a, b->next); + if (b->next) clean_id_list(a, b->next); } ANN static void clean_specialized_list(Clean *a, Specialized_List b) { - if(b->traits) - clean_id_list(a, b->traits); - if(b->next) - clean_specialized_list(a, b->next); + if (b->traits) clean_id_list(a, b->traits); + if (b->next) clean_specialized_list(a, b->next); } ANN static void clean_type_list(Clean *a, Type_List b) { clean_type_decl(a, b->td); - if(b->next) - clean_type_list(a, b->next); + if (b->next) clean_type_list(a, b->next); } ANN static void clean_tmpl(Clean *a, Tmpl *b) { - if(b->base < 0 && b->list) - clean_specialized_list(a, b->list); - if(b->call) - clean_type_list(a, b->call); + if (b->base < 0 && b->list) clean_specialized_list(a, b->list); + if (b->call) clean_type_list(a, b->call); } ANN static void clean_range(Clean *a, Range *b) { - if(b->start) - clean_exp(a, b->start); - if(b->end) - clean_exp(a, b->end); + if (b->start) clean_exp(a, b->start); + if (b->end) clean_exp(a, b->end); } ANN static void clean_type_decl(Clean *a, Type_Decl *b) { - if(b->array) - clean_array_sub(a, b->array); - if(b->types) - clean_type_list(a, b->types); - if(b->next) - clean_type_decl(a, b->next); + if (b->array) clean_array_sub(a, b->array); + if (b->types) clean_type_list(a, b->types); + if (b->next) clean_type_decl(a, b->next); } ANN static void clean_prim(Clean *a, Exp_Primary *b) { - if(b->prim_type == ae_prim_hack || b->prim_type == ae_prim_interp) + if (b->prim_type == ae_prim_hack || b->prim_type == ae_prim_interp) clean_exp(a, b->d.exp); - else if(b->prim_type == ae_prim_array) + else if (b->prim_type == ae_prim_array) clean_array_sub(a, b->d.array); - else if(b->prim_type == ae_prim_range) + else if (b->prim_type == ae_prim_range) clean_range(a, b->d.range); } ANN static void clean_var_decl(Clean *a, Var_Decl b) { - if(b->array) - clean_array_sub(a, b->array); - if(a->scope && b->value) - value_remref(b->value, a->gwion); + if (b->array) clean_array_sub(a, b->array); + if (a->scope && b->value) value_remref(b->value, a->gwion); } ANN static void clean_var_decl_list(Clean *a, Var_Decl_List b) { clean_var_decl(a, b->self); - if(b->next) - clean_var_decl_list(a, b->next); + if (b->next) clean_var_decl_list(a, b->next); } ANN static void clean_exp_decl(Clean *a, Exp_Decl *b) { - if(b->td) - clean_type_decl(a, b->td); + if (b->td) clean_type_decl(a, b->td); clean_var_decl_list(a, b->list); } @@ -86,8 +70,8 @@ ANN static void clean_exp_binary(Clean *a, Exp_Binary *b) { } ANN static void clean_exp_unary(Clean *a, Exp_Unary *b) { - switch(b->unary_type) { - case unary_exp: + switch (b->unary_type) { + case unary_exp: clean_exp(a, b->exp); break; case unary_td: @@ -110,10 +94,8 @@ ANN static void clean_exp_post(Clean *a, Exp_Postfix *b) { ANN static void clean_exp_call(Clean *a, Exp_Call *b) { clean_exp(a, b->func); - if(b->args) - clean_exp(a, b->args); - if(b->tmpl) - clean_tmpl(a, b->tmpl); + if (b->args) clean_exp(a, b->args); + if (b->tmpl) clean_tmpl(a, b->tmpl); } ANN static void clean_exp_array(Clean *a, Exp_Array *b) { @@ -128,14 +110,11 @@ ANN static void clean_exp_slice(Clean *a, Exp_Slice *b) { ANN static void clean_exp_if(Clean *a, Exp_If *b) { clean_exp(a, b->cond); - if(b->if_exp) - clean_exp(a, b->if_exp); + if (b->if_exp) clean_exp(a, b->if_exp); clean_exp(a, b->else_exp); } -ANN static void clean_exp_dot(Clean *a, Exp_Dot *b) { - clean_exp(a, b->base); -} +ANN static void clean_exp_dot(Clean *a, Exp_Dot *b) { clean_exp(a, b->base); } ANN static void clean_exp_lambda(Clean *a, Exp_Lambda *b) { clean_func_def(a, b->def); @@ -145,16 +124,14 @@ ANN static void clean_exp_td(Clean *a, Type_Decl **b) { clean_type_decl(a, *b); } -DECL_EXP_FUNC(clean, void, Clean*) +DECL_EXP_FUNC(clean, void, Clean *) ANN static void clean_exp(Clean *a, Exp b) { clean_exp_func[b->exp_type](a, &b->d); - if(b->next) - clean_exp(a, b->next); + if (b->next) clean_exp(a, b->next); } ANN static void clean_stmt_exp(Clean *a, Stmt_Exp b) { - if(b->val) - clean_exp(a, b->val); + if (b->val) clean_exp(a, b->val); } ANN static void clean_stmt_flow(Clean *a, Stmt_Flow b) { @@ -170,27 +147,22 @@ ANN static void clean_stmt_flow(Clean *a, Stmt_Flow b) { ANN static void clean_stmt_for(Clean *a, Stmt_For b) { ++a->scope; clean_stmt(a, b->c1); - if(b->c2) - clean_stmt(a, b->c2); - if(b->c3) - clean_exp(a, b->c3); + if (b->c2) clean_stmt(a, b->c2); + if (b->c3) clean_exp(a, b->c3); clean_stmt(a, b->body); --a->scope; } ANN static void clean_idx(Clean *a, struct EachIdx_ *b) { - if(b->v) - value_remref(b->v, a->gwion); + if (b->v) value_remref(b->v, a->gwion); } ANN static void clean_stmt_each(Clean *a, Stmt_Each b) { ++a->scope; clean_exp(a, b->exp); clean_stmt(a, b->body); - if(b->v) - value_remref(b->v, a->gwion); - if(b->idx) - clean_idx(a, b->idx); + if (b->v) value_remref(b->v, a->gwion); + if (b->idx) clean_idx(a, b->idx); --a->scope; } @@ -205,15 +177,13 @@ ANN static void clean_stmt_if(Clean *a, Stmt_If b) { ++a->scope; clean_exp(a, b->cond); clean_stmt(a, b->if_body); - if(b->else_body) - clean_stmt(a, b->else_body); + if (b->else_body) clean_stmt(a, b->else_body); --a->scope; } ANN static void clean_stmt_code(Clean *a, Stmt_Code b) { ++a->scope; - if(b->stmt_list) - clean_stmt_list(a, b->stmt_list); + if (b->stmt_list) clean_stmt_list(a, b->stmt_list); --a->scope; } @@ -225,22 +195,19 @@ ANN static void clean_stmt_varloop(Clean *a, Stmt_VarLoop b) { } ANN static void clean_stmt_return(Clean *a, Stmt_Exp b) { - if(b->val) - clean_exp(a, b->val); + if (b->val) clean_exp(a, b->val); } ANN static void clean_case_list(Clean *a, Stmt_List b) { clean_stmt_case(a, &b->stmt->d.stmt_match); - if(b->next) - clean_case_list(a, b->next); + if (b->next) clean_case_list(a, b->next); } ANN static void clean_handler_list(Clean *a, Handler_List b) { ++a->scope; clean_stmt(a, b->stmt); --a->scope; - if(b->next) - clean_handler_list(a, b->next); + if (b->next) clean_handler_list(a, b->next); } ANN static void clean_stmt_try(Clean *a, Stmt_Try b) { ++a->scope; @@ -253,8 +220,7 @@ ANN static void clean_stmt_match(Clean *a, Stmt_Match b) { ++a->scope; clean_exp(a, b->cond); clean_case_list(a, b->list); - if(b->where) - clean_stmt(a, b->where); + if (b->where) clean_stmt(a, b->where); --a->scope; } @@ -262,8 +228,7 @@ ANN static void clean_stmt_case(Clean *a, Stmt_Match b) { ++a->scope; clean_exp(a, b->cond); clean_stmt_list(a, b->list); - if(b->when) - clean_exp(a, b->when); + if (b->when) clean_exp(a, b->when); --a->scope; } @@ -272,44 +237,39 @@ ANN static void clean_stmt_defer(Clean *a, Stmt_Defer b) { } ANN static void clean_dummy(Clean *a NUSED, void *b NUSED) {} -#define clean_stmt_jump clean_dummy -#define clean_stmt_pp clean_dummy -#define clean_stmt_break clean_dummy +#define clean_stmt_jump clean_dummy +#define clean_stmt_pp clean_dummy +#define clean_stmt_break clean_dummy #define clean_stmt_continue clean_dummy #define clean_stmt_retry clean_dummy -DECL_STMT_FUNC(clean, void, Clean*) +DECL_STMT_FUNC(clean, void, Clean *) ANN static void clean_stmt(Clean *a, Stmt b) { clean_stmt_func[b->stmt_type](a, &b->d); } ANN static void clean_arg_list(Clean *a, Arg_List b) { - if(b->td) - clean_type_decl(a, b->td); + if (b->td) clean_type_decl(a, b->td); clean_var_decl(a, b->var_decl); - if(b->next) - clean_arg_list(a, b->next); + if (b->next) clean_arg_list(a, b->next); } ANN static void clean_stmt_list(Clean *a, Stmt_List b) { clean_stmt(a, b->stmt); - if(b->next) - clean_stmt_list(a, b->next); + if (b->next) clean_stmt_list(a, b->next); } ANN static void clean_func_base(Clean *a, Func_Base *b) { - if(b->td) - clean_type_decl(a, b->td); - if(b->args) - clean_arg_list(a, b->args); - if(b->tmpl) - clean_tmpl(a, b->tmpl); + if (b->td) clean_type_decl(a, b->td); + if (b->args) clean_arg_list(a, b->args); + if (b->tmpl) clean_tmpl(a, b->tmpl); } ANN static void clean_func_def(Clean *a, Func_Def b) { clean_func_base(a, b->base); ++a->scope; - if(b->d.code && !(b->base->func && safe_vflag(b->base->func->value_ref, vflag_builtin))) + if (b->d.code && + !(b->base->func && safe_vflag(b->base->func->value_ref, vflag_builtin))) clean_stmt(a, b->d.code); else b->d.code = NULL; @@ -317,7 +277,7 @@ ANN static void clean_func_def(Clean *a, Func_Def b) { } ANN void func_def_cleaner(const Gwion gwion, Func_Def b) { - Clean a = { .gwion=gwion }; + Clean a = {.gwion = gwion}; clean_func_def(&a, b); free_func_def(gwion->mp, b); } @@ -329,75 +289,63 @@ ANN static void clean_extend_def(Clean *a, Extend_Def b) { ANN static void clean_class_def(Clean *a, Class_Def b) { clean_type_def(a, &b->base); - if(b->traits) - clean_id_list(a, b->traits); - if(b->body) - clean_ast(a, b->body); + if (b->traits) clean_id_list(a, b->traits); + if (b->body) clean_ast(a, b->body); } ANN void class_def_cleaner(const Gwion gwion, Class_Def b) { - Clean a = { .gwion=gwion }; + Clean a = {.gwion = gwion}; clean_class_def(&a, b); free_class_def(gwion->mp, b); } ANN static void clean_enum_def(Clean *a, Enum_Def b) { clean_id_list(a, b->list); - if(b->values.ptr) - vector_release(&b->values); + if (b->values.ptr) vector_release(&b->values); } ANN static void clean_union_list(Clean *a, Union_List b) { clean_type_decl(a, b->td); - if(b->next) - clean_union_list(a, b->next); + if (b->next) clean_union_list(a, b->next); } ANN static void clean_union_def(Clean *a, Union_Def b) { clean_union_list(a, b->l); - if(b->tmpl) - clean_tmpl(a, b->tmpl); + if (b->tmpl) clean_tmpl(a, b->tmpl); } ANN static void clean_fptr_def(Clean *a, Fptr_Def b) { clean_func_base(a, b->base); - if(b->type) - type_remref(b->type, a->gwion); + if (b->type) type_remref(b->type, a->gwion); } ANN static void clean_type_def(Clean *a, Type_Def b) { - if(b->ext) - clean_type_decl(a, b->ext); - if(b->when) { + if (b->ext) clean_type_decl(a, b->ext); + if (b->when) { clean_exp(a, b->when); - if(b->when_def) - clean_func_def(a, b->when_def); + if (b->when_def) clean_func_def(a, b->when_def); } - if(b->tmpl) - clean_tmpl(a, b->tmpl); + if (b->tmpl) clean_tmpl(a, b->tmpl); } ANN static void clean_trait_def(Clean *a, Trait_Def b) { - if(b->traits) - clean_id_list(a, b->traits); - if(b->body) - clean_ast(a, b->body); + if (b->traits) clean_id_list(a, b->traits); + if (b->body) clean_ast(a, b->body); } -DECL_SECTION_FUNC(clean, void, Clean*) +DECL_SECTION_FUNC(clean, void, Clean *) ANN static inline void clean_section(Clean *a, Section *b) { - clean_section_func[b->section_type](a, *(void**)&b->d); + clean_section_func[b->section_type](a, *(void **)&b->d); } ANN static void clean_ast(Clean *a, Ast b) { clean_section(a, b->section); - if(b->next) - clean_ast(a, b->next); + if (b->next) clean_ast(a, b->next); } ANN void ast_cleaner(const Gwion gwion, Ast b) { - Clean a = { .gwion=gwion }; + Clean a = {.gwion = gwion}; clean_ast(&a, b); free_ast(gwion->mp, b); } diff --git a/src/compile.c b/src/compile.c index 0c9d6e50..e4120788 100644 --- a/src/compile.c +++ b/src/compile.c @@ -9,59 +9,50 @@ #include "pass.h" #include "clean.h" -enum compile_type { - COMPILE_NAME, - COMPILE_MSTR, - COMPILE_FILE -}; +enum compile_type { COMPILE_NAME, COMPILE_MSTR, COMPILE_FILE }; struct Compiler { - const m_str base; - m_str name; - m_str data; - FILE* file; - Ast ast; - struct Vector_ args; + const m_str base; + m_str name; + m_str data; + FILE * file; + Ast ast; + struct Vector_ args; enum compile_type type; }; -ANN static void compiler_name(struct Compiler* c) { +ANN static void compiler_name(struct Compiler *c) { m_str d = strdup(c->base); c->name = strsep(&d, ":"); - if(d) - vector_init(&c->args); - while(d) - vector_add(&c->args, (vtype)strdup(strsep(&d, ":"))); + if (d) vector_init(&c->args); + while (d) vector_add(&c->args, (vtype)strdup(strsep(&d, ":"))); free(d); } -ANN static inline void compiler_error(struct Compiler*const c) { - if(c->args.ptr) { +ANN static inline void compiler_error(struct Compiler *const c) { + if (c->args.ptr) { const Vector v = &c->args; - for(m_uint i = 0; i < vector_size(v); ++i) { + for (m_uint i = 0; i < vector_size(v); ++i) { const m_str str = (m_str)vector_at(v, i); - if(str) - xfree((m_str)vector_at(v, i)); + if (str) xfree((m_str)vector_at(v, i)); } vector_release(v); } } -ANN static void compiler_clean(const struct Compiler* c) { - if(c->name) - xfree(c->name); +ANN static void compiler_clean(const struct Compiler *c) { + if (c->name) xfree(c->name); /* test c->type because COMPILE_FILE does not own file */ - if(c->type != COMPILE_FILE && c->file) - fclose(c->file); + if (c->type != COMPILE_FILE && c->file) fclose(c->file); } -ANN static m_bool _compiler_open(struct Compiler* c) { - if(c->type == COMPILE_NAME) { +ANN static m_bool _compiler_open(struct Compiler *c) { + if (c->type == COMPILE_NAME) { m_str name = c->name; - c->name = realpath(name, NULL); + c->name = realpath(name, NULL); xfree(name); return c->name ? !!(c->file = fopen(c->name, "r")) : GW_ERROR; - } else if(c->type == COMPILE_MSTR) { + } else if (c->type == COMPILE_MSTR) { c->file = c->data ? fmemopen(c->data, strlen(c->data), "r") : NULL; return c->file ? GW_OK : GW_ERROR; } @@ -73,28 +64,26 @@ ANN static m_bool _compiler_open(struct Compiler* c) { ANN static int is_reg(const m_str path) { struct stat s = {}; stat(path, &s); - return !S_ISDIR(s.st_mode) && - (S_ISREG(s.st_mode) || !S_ISFIFO(s.st_mode)); + return !S_ISDIR(s.st_mode) && (S_ISREG(s.st_mode) || !S_ISFIFO(s.st_mode)); } #else ANN static m_bool is_reg(const m_str path) { const DWORD dw = GetFileAttributes(path); - return !(dw == INVALID_FILE_ATTRIBUTES || - dw & FILE_ATTRIBUTE_DIRECTORY); + return !(dw == INVALID_FILE_ATTRIBUTES || dw & FILE_ATTRIBUTE_DIRECTORY); } #endif -ANN static inline m_bool compiler_open(struct Compiler* c) { +ANN static inline m_bool compiler_open(struct Compiler *c) { char name[strlen(c->name) + 1]; strcpy(name, c->name); #ifndef __FUZZING__ - if((c->type == COMPILE_FILE || c->type == COMPILE_NAME) && !is_reg(name)) { + if ((c->type == COMPILE_FILE || c->type == COMPILE_NAME) && !is_reg(name)) { gw_err(_("'%s': is a not a regular file\n"), name); return GW_ERROR; } #endif - if(_compiler_open(c) < 0) { + if (_compiler_open(c) < 0) { compiler_error(c); gw_err(_("can't open '%s'\n"), name); return GW_ERROR; @@ -102,27 +91,27 @@ ANN static inline m_bool compiler_open(struct Compiler* c) { return GW_OK; } -ANN static inline m_bool _passes(struct Gwion_* gwion, struct Compiler* c) { - for(m_uint i = 0; i < vector_size(&gwion->data->passes->vec); ++i) { - const compilation_pass pass = (compilation_pass)vector_at(&gwion->data->passes->vec, i); +ANN static inline m_bool _passes(struct Gwion_ *gwion, struct Compiler *c) { + for (m_uint i = 0; i < vector_size(&gwion->data->passes->vec); ++i) { + const compilation_pass pass = + (compilation_pass)vector_at(&gwion->data->passes->vec, i); CHECK_BB(pass(gwion->env, c->ast)); } return GW_OK; } - -ANN static inline m_bool passes(struct Gwion_* gwion, struct Compiler* c) { - const Env env = gwion->env; +ANN static inline m_bool passes(struct Gwion_ *gwion, struct Compiler *c) { + const Env env = gwion->env; const Context ctx = new_context(env->gwion->mp, c->ast, env->name); env_reset(env); load_context(ctx, env); const m_bool ret = _passes(gwion, c); - if(ret > 0) //{ + if (ret > 0) //{ nspc_commit(env->curr); - if(ret > 0 || env->context->global) + if (ret > 0 || env->context->global) vector_add(&env->scope->known_ctx, (vtype)ctx); - else {//nspc_rollback(env->global_nspc); - if(!ctx->error) { + else { // nspc_rollback(env->global_nspc); + if (!ctx->error) { gw_err(_("{-}while compiling file `{0}{/}%s{-}`{0}\n"), c->base); ctx->error = 1; } @@ -132,23 +121,20 @@ ANN static inline m_bool passes(struct Gwion_* gwion, struct Compiler* c) { return ret; } -ANN static inline m_bool _check(struct Gwion_* gwion, struct Compiler* c) { - struct AstGetter_ arg = { c->name, c->file, gwion->st, .ppa=gwion->ppa }; +ANN static inline m_bool _check(struct Gwion_ *gwion, struct Compiler *c) { + struct AstGetter_ arg = {c->name, c->file, gwion->st, .ppa = gwion->ppa}; CHECK_OB((c->ast = parse(&arg))); gwion->env->name = c->name; const m_bool ret = passes(gwion, c); - if(!arg.global) - ast_cleaner(gwion, c->ast); + if (!arg.global) ast_cleaner(gwion, c->ast); return ret; } -ANN static m_uint _compile(struct Gwion_* gwion, struct Compiler* c) { - if(compiler_open(c) < 0) - return 0; - if(_check(gwion, c) < 0) - return 0; - if(gwion->emit->info->code) { - const VM_Shred shred = new_vm_shred(gwion->mp, gwion->emit->info->code); +ANN static m_uint _compile(struct Gwion_ *gwion, struct Compiler *c) { + if (compiler_open(c) < 0) return 0; + if (_check(gwion, c) < 0) return 0; + if (gwion->emit->info->code) { + const VM_Shred shred = new_vm_shred(gwion->mp, gwion->emit->info->code); shred->info->args.ptr = c->args.ptr; vm_add_shred(gwion->vm, shred); gwion->emit->info->code = NULL; @@ -157,7 +143,7 @@ ANN static m_uint _compile(struct Gwion_* gwion, struct Compiler* c) { return GW_OK; } -ANN static m_uint compile(struct Gwion_* gwion, struct Compiler* c) { +ANN static m_uint compile(struct Gwion_ *gwion, struct Compiler *c) { compiler_name(c); MUTEX_LOCK(gwion->data->mutex); const m_uint ret = _compile(gwion, c); @@ -166,17 +152,19 @@ ANN static m_uint compile(struct Gwion_* gwion, struct Compiler* c) { return ret; } -ANN m_uint compile_filename(struct Gwion_* gwion, const m_str filename) { - struct Compiler c = { .base=filename, .type=COMPILE_NAME }; +ANN m_uint compile_filename(struct Gwion_ *gwion, const m_str filename) { + struct Compiler c = {.base = filename, .type = COMPILE_NAME}; return compile(gwion, &c); } -ANN m_uint compile_string(struct Gwion_* gwion, const m_str filename, const m_str data) { - struct Compiler c = { .base=filename, .type=COMPILE_MSTR, .data=data }; +ANN m_uint compile_string(struct Gwion_ *gwion, const m_str filename, + const m_str data) { + struct Compiler c = {.base = filename, .type = COMPILE_MSTR, .data = data}; return compile(gwion, &c); } -ANN m_uint compile_file(struct Gwion_* gwion, const m_str filename, FILE* file) { - struct Compiler c = { .base=filename, .type=COMPILE_FILE, .file=file }; +ANN m_uint compile_file(struct Gwion_ *gwion, const m_str filename, + FILE *file) { + struct Compiler c = {.base = filename, .type = COMPILE_FILE, .file = file}; return compile(gwion, &c); } diff --git a/src/emit/emit.c b/src/emit/emit.c index d33a0cab..09c4317f 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -22,18 +22,29 @@ #define insert_symbol(a) insert_symbol(emit->gwion->st, (a)) #undef ERR_B -#define ERR_B(a, b, ...) { env_err(emit->env, (a), (b), ## __VA_ARGS__); return GW_ERROR; } +#define ERR_B(a, b, ...) \ + { \ + env_err(emit->env, (a), (b), ##__VA_ARGS__); \ + return GW_ERROR; \ + } #undef ERR_O -#define ERR_O(a, b, ...) { env_err(emit->env, (a), (b), ## __VA_ARGS__); return NULL; } +#define ERR_O(a, b, ...) \ + { \ + env_err(emit->env, (a), (b), ##__VA_ARGS__); \ + return NULL; \ + } typedef struct Local_ { - Type type; + Type type; m_uint offset; - bool skip; + bool skip; } Local; -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) { +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); } @@ -42,14 +53,13 @@ static inline m_uint emit_push_global(const Emitter emit) { } static inline void emit_debug(const Emitter emit, const Value v) { - if(!emit->info->debug) - return; + if (!emit->info->debug) return; const Instr instr = emit_add_instr(emit, DebugValue); - instr->m_val = (m_uint)v; + instr->m_val = (m_uint)v; } -ANEW static Frame* new_frame(MemPool p) { - Frame* frame = mp_calloc(p, Frame); +ANEW static Frame *new_frame(MemPool p) { + Frame *frame = mp_calloc(p, Frame); vector_init(&frame->stack); vector_add(&frame->stack, (vtype)NULL); vector_init(&frame->defer); @@ -57,84 +67,92 @@ ANEW static Frame* new_frame(MemPool p) { return frame; } -ANN static void free_frame(MemPool p, 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(p, Local, (Local*)vector_at(&a->stack, i - 1)); + for (vtype i = vector_size(&a->stack) + 1; --i;) + if (vector_at(&a->stack, i - 1)) + mp_free(p, Local, (Local *)vector_at(&a->stack, i - 1)); vector_release(&a->stack); vector_release(&a->defer); - if(a->handlers.ptr) - map_release(&a->handlers); + if (a->handlers.ptr) map_release(&a->handlers); mp_free(p, Frame, a); } -ANN static Local* new_local(MemPool p, const Type type) { - Local* local = mp_calloc(p, Local); - local->type = type; +ANN static Local *new_local(MemPool p, const Type type) { + Local *local = mp_calloc(p, Local); + local->type = type; return local; } -ANN static m_uint frame_local(MemPool p, Frame* frame, const Type t, const bool skip) { - Local* local = new_local(p, t); +ANN static m_uint frame_local(MemPool p, Frame *frame, const Type t, + const bool skip) { + Local *local = new_local(p, t); local->offset = frame->curr_offset; - local->skip = skip; + local->skip = skip; frame->curr_offset += t->size; vector_add(&frame->stack, (vtype)local); return local->offset; } -ANN static inline void frame_push(Frame* frame) { +ANN static inline void frame_push(Frame *frame) { vector_add(&frame->stack, (vtype)NULL); vector_add(&frame->defer, (vtype)NULL); } -static const f_instr regpushimm[] = { RegPushImm, RegPushImm2, RegPushImm3, RegPushImm4 }; -static const f_instr regpushmem[] = { RegPushMem, RegPushMem2, RegPushMem3, RegPushMem4 }; -static const f_instr regpushbase[] = { RegPushBase, RegPushBase2, RegPushBase3, RegPushBase4 }; -static const f_instr dotstatic[] = { DotStatic, DotStatic2, DotStatic3, RegPushImm }; -static const f_instr allocmember[] = { RegPushImm, RegPushImm2, RegPushImm3, AllocMember4 }; -static const f_instr allocword[] = { AllocWord, AllocWord2, AllocWord3, RegPushMem4 }; -static const f_instr structmember[] = { StructMember, StructMemberFloat, StructMemberOther, StructMemberAddr }; - -#define regmove(name, op) \ -ANN static inline Instr reg##name(const Emitter emit, const m_uint sz) { \ - const Instr instr = emit_add_instr(emit, RegMove); \ - instr->m_val = op sz; \ - return instr; \ -} -regmove(pop,-) -regmove(push,) -#define regxxx(name, instr) \ -ANN static inline Instr reg##name(const Emitter emit, const m_uint sz) { \ - const Instr instr = emit_add_instr(emit, Reg##instr); \ - instr->m_val = sz; \ - return instr; \ -} -regxxx(pushi, PushImm) -regxxx(seti, SetImm) - -static inline enum Kind kindof(const m_uint size, const uint emit_var) { - if(emit_var) - return KIND_ADDR; +static const f_instr regpushimm[] = {RegPushImm, RegPushImm2, RegPushImm3, + RegPushImm4}; +static const f_instr regpushmem[] = {RegPushMem, RegPushMem2, RegPushMem3, + RegPushMem4}; +static const f_instr regpushbase[] = {RegPushBase, RegPushBase2, RegPushBase3, + RegPushBase4}; +static const f_instr dotstatic[] = {DotStatic, DotStatic2, DotStatic3, + RegPushImm}; +static const f_instr allocmember[] = {RegPushImm, RegPushImm2, RegPushImm3, + AllocMember4}; +static const f_instr allocword[] = {AllocWord, AllocWord2, AllocWord3, + RegPushMem4}; +static const f_instr structmember[] = {StructMember, StructMemberFloat, + StructMemberOther, StructMemberAddr}; + +#define regmove(name, op) \ + ANN static inline Instr reg##name(const Emitter emit, const m_uint sz) { \ + const Instr instr = emit_add_instr(emit, RegMove); \ + instr->m_val = op sz; \ + return instr; \ + } +regmove(pop, -) regmove(push, ) +#define regxxx(name, instr) \ + ANN static inline Instr reg##name(const Emitter emit, const m_uint sz) { \ + const Instr instr = emit_add_instr(emit, Reg##instr); \ + instr->m_val = sz; \ + return instr; \ + } + regxxx(pushi, PushImm) regxxx(seti, SetImm) + + static inline enum Kind kindof(const m_uint size, const uint emit_var) { + if (emit_var) return KIND_ADDR; return size == SZ_INT ? KIND_INT : size == SZ_FLOAT ? KIND_FLOAT : KIND_OTHER; } -ANN /*static */Instr emit_kind(Emitter emit, const m_uint size, const uint addr, const f_instr func[]) { - const enum Kind kind = kindof(size, addr); - const Instr instr = emit_add_instr(emit, func[kind]); - instr->m_val2 = size; +ANN /*static */ Instr emit_kind(Emitter emit, const m_uint size, + const uint addr, const f_instr func[]) { + const enum Kind kind = kindof(size, addr); + const Instr instr = emit_add_instr(emit, func[kind]); + instr->m_val2 = size; return instr; } -ANN static m_bool emit_class_def(const Emitter, const Class_Def); -ANN /*static */m_bool emit_cdef(const Emitter, const Type); +ANN static m_bool emit_class_def(const Emitter, const Class_Def); +ANN /*static */ m_bool emit_cdef(const Emitter, const Type); ANN static inline m_bool ensure_emit(const Emitter emit, const Type t) { - if(tflag(t, tflag_emit) || !(tflag(t, tflag_cdef) || tflag(t, tflag_udef))) - return GW_OK;//clean callers - struct EnvSet es = { .env=emit->env, .data=emit, .func=(_exp_func)emit_cdef, - .scope=emit->env->scope->depth, .flag=tflag_emit }; + if (tflag(t, tflag_emit) || !(tflag(t, tflag_cdef) || tflag(t, tflag_udef))) + return GW_OK; // clean callers + struct EnvSet es = {.env = emit->env, + .data = emit, + .func = (_exp_func)emit_cdef, + .scope = emit->env->scope->depth, + .flag = tflag_emit}; return envset_run(&es, t); } @@ -142,62 +160,62 @@ ANN static inline m_uint emit_code_size(const Emitter emit) { return vector_size(&emit->code->instr); } -ANN static void emit_struct_ctor(const Emitter emit, const Type type, const m_uint offset) { +ANN static void emit_struct_ctor(const Emitter emit, const Type type, + const m_uint offset) { emit->code->frame->curr_offset += SZ_INT; const Instr instr = emit_add_instr(emit, RegPushMem4); - instr->m_val = offset; + instr->m_val = offset; const Instr tomem = emit_add_instr(emit, Reg2Mem); - tomem->m_val = emit->code->frame->curr_offset; - tomem->m_val2 = -SZ_INT; + tomem->m_val = emit->code->frame->curr_offset; + tomem->m_val2 = -SZ_INT; regpushi(emit, (m_uint)type); - const Instr tomem2 = emit_add_instr(emit, Reg2Mem); - tomem2->m_val = emit->code->frame->curr_offset + SZ_INT; - tomem2->m_val2 = -SZ_INT; - const Instr set_code = regseti(emit, (m_uint)type->nspc->dtor); - set_code->m_val2 = SZ_INT; + const Instr tomem2 = emit_add_instr(emit, Reg2Mem); + tomem2->m_val = emit->code->frame->curr_offset + SZ_INT; + tomem2->m_val2 = -SZ_INT; + const Instr set_code = regseti(emit, (m_uint)type->nspc->dtor); + set_code->m_val2 = SZ_INT; const m_uint code_offset = emit_code_offset(emit); - const Instr regset = regseti(emit, code_offset /*+ SZ_INT*/ /*+ sizeof(frame_t)*/); - regset->m_val2 = SZ_INT *2; - regpush(emit, SZ_INT *2); + const Instr regset = + regseti(emit, code_offset /*+ SZ_INT*/ /*+ sizeof(frame_t)*/); + regset->m_val2 = SZ_INT * 2; + regpush(emit, SZ_INT * 2); const Instr prelude = emit_add_instr(emit, SetCode); - prelude->m_val = -SZ_INT*4; - prelude->udata.one = 2; - const Instr next = emit_add_instr(emit, Overflow); - next->m_val2 = code_offset; + prelude->m_val = -SZ_INT * 4; + prelude->udata.one = 2; + const Instr next = emit_add_instr(emit, Overflow); + next->m_val2 = code_offset; emit->code->frame->curr_offset -= SZ_INT; } -ANN static void struct_pop(const Emitter emit, const Type type, const m_uint offset) { - if(!type->info->tuple) - return; - if(type->nspc->dtor) - emit_struct_ctor(emit, type, offset); - for(m_uint i = 0; i < vector_size(&type->info->tuple->types); ++i) { +ANN static void struct_pop(const Emitter emit, const Type type, + const m_uint offset) { + if (!type->info->tuple) return; + if (type->nspc->dtor) emit_struct_ctor(emit, type, offset); + for (m_uint i = 0; i < vector_size(&type->info->tuple->types); ++i) { const Type t = (Type)vector_at(&type->info->tuple->types, i); - if(isa(t, emit->gwion->type[et_object]) > 0) { + if (isa(t, emit->gwion->type[et_object]) > 0) { const Instr instr = emit_add_instr(emit, ObjectRelease); - instr->m_val = offset + vector_at(&type->info->tuple->offset, i); - } else if(tflag(t, tflag_struct)) + instr->m_val = offset + vector_at(&type->info->tuple->offset, i); + } else if (tflag(t, tflag_struct)) struct_pop(emit, t, offset + vector_at(&type->info->tuple->offset, i)); } } -ANN static m_bool emit_stmt(const Emitter emit, const Stmt stmt, const m_bool pop); +ANN static m_bool emit_stmt(const Emitter emit, const Stmt stmt, + const m_bool pop); ANN static m_bool emit_defers(const Emitter emit) { - if(!vector_size(&emit->code->frame->defer)) - return GW_OK; + if (!vector_size(&emit->code->frame->defer)) return GW_OK; Stmt stmt; - while((stmt = (Stmt)vector_pop(&emit->code->frame->defer))) + while ((stmt = (Stmt)vector_pop(&emit->code->frame->defer))) CHECK_BB(emit_stmt(emit, stmt, 1)); return GW_OK; } ANN static m_bool emit_defers2(const Emitter emit) { - for(m_uint i = vector_size(&emit->code->frame->defer) + 1; --i;) { - const Stmt stmt = (Stmt)vector_at(&emit->code->frame->defer, i-1); - if(!stmt) - break; + for (m_uint i = vector_size(&emit->code->frame->defer) + 1; --i;) { + const Stmt stmt = (Stmt)vector_at(&emit->code->frame->defer, i - 1); + if (!stmt) break; CHECK_BB(emit_stmt(emit, stmt, 1)); } return GW_OK; @@ -205,15 +223,15 @@ ANN static m_bool emit_defers2(const Emitter emit) { ANN static m_int _frame_pop(const Emitter emit) { Frame *frame = emit->code->frame; - DECL_OB(const Local*, l, = (Local*)vector_pop(&frame->stack)); + DECL_OB(const Local *, l, = (Local *)vector_pop(&frame->stack)); frame->curr_offset -= l->type->size; - if(l->skip) - return _frame_pop(emit); - if(tflag(l->type, tflag_struct)) { + if (l->skip) return _frame_pop(emit); + if (tflag(l->type, tflag_struct)) { struct_pop(emit, l->type, l->offset); return _frame_pop(emit); } - return isa(l->type, emit->gwion->type[et_object]) > 0 ? (m_int)l->offset : _frame_pop(emit); + return isa(l->type, emit->gwion->type[et_object]) > 0 ? (m_int)l->offset + : _frame_pop(emit); } ANN static m_int frame_pop(const Emitter emit) { @@ -221,13 +239,14 @@ ANN static m_int frame_pop(const Emitter emit) { return _frame_pop(emit); } -ANN /*static */m_bool emit_exp(const Emitter emit, Exp exp); -ANN static m_bool emit_stmt(const Emitter emit, const Stmt stmt, const m_bool pop); -ANN static m_bool emit_stmt_list(const Emitter emit, Stmt_List list); -ANN static m_bool emit_exp_dot(const Emitter emit, const Exp_Dot* member); +ANN /*static */ m_bool emit_exp(const Emitter emit, Exp exp); +ANN static m_bool emit_stmt(const Emitter emit, const Stmt stmt, + const m_bool pop); +ANN static m_bool emit_stmt_list(const Emitter emit, Stmt_List list); +ANN static m_bool emit_exp_dot(const Emitter emit, const Exp_Dot *member); -ANEW static Code* new_code(const Emitter emit, const m_str name) { - Code* code = mp_calloc(emit->gwion->mp, Code); +ANEW static Code *new_code(const Emitter emit, const m_str name) { + Code *code = mp_calloc(emit->gwion->mp, Code); code->name = code_name_set(emit->gwion->mp, name, emit->env->name); vector_init(&code->instr); vector_init(&code->stack_break); @@ -237,7 +256,7 @@ ANEW static Code* new_code(const Emitter emit, const m_str name) { return code; } -ANN static void free_code(MemPool p, 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); @@ -249,31 +268,28 @@ ANN static void free_code(MemPool p, Code* code) { ANN static void emit_pop_scope(const Emitter emit) { m_int offset; - while((offset = frame_pop(emit)) > -1) { - Instr instr = emit_add_instr(emit, ObjectRelease); + while ((offset = frame_pop(emit)) > -1) { + Instr instr = emit_add_instr(emit, ObjectRelease); instr->m_val = (m_uint)offset; } vector_pop(&emit->info->pure); - if(emit->info->debug) - emit_add_instr(emit, DebugPop); + if (emit->info->debug) emit_add_instr(emit, DebugPop); } ANN static inline void emit_push_code(const Emitter emit, const m_str name) { vector_add(&emit->stack, (vtype)emit->code); emit->code = new_code(emit, name); - if(emit->info->debug) - emit_add_instr(emit, DebugLine); + if (emit->info->debug) emit_add_instr(emit, DebugLine); } ANN static inline void emit_pop_code(const Emitter emit) { - emit->code = (Code*)vector_pop(&emit->stack); + emit->code = (Code *)vector_pop(&emit->stack); } ANN static inline void emit_push_scope(const Emitter emit) { frame_push(emit->code->frame); vector_add(&emit->info->pure, 0); - if(emit->info->debug) - emit_add_instr(emit, DebugPush); + if (emit->info->debug) emit_add_instr(emit, DebugPush); } ANN m_uint emit_code_offset(const Emitter emit) { @@ -291,65 +307,62 @@ ANN m_uint emit_localn(const Emitter emit, const Type t) { ANN void emit_ext_ctor(const Emitter emit, const Type t); ANN static inline void maybe_ctor(const Emitter emit, const Type t) { - if(tflag(t, tflag_ctor)) - emit_ext_ctor(emit, t); + if (tflag(t, tflag_ctor)) emit_ext_ctor(emit, t); } ANN static void emit_pre_ctor(const Emitter emit, const Type type) { - if(type->info->parent) - emit_pre_ctor(emit, type->info->parent); - if(tflag(type, tflag_typedef) && type->info->parent->array_depth) + if (type->info->parent) emit_pre_ctor(emit, type->info->parent); + if (tflag(type, tflag_typedef) && type->info->parent->array_depth) emit_array_extend(emit, type, type->info->cdef->base.ext->array->exp); maybe_ctor(emit, type); } ANN static void struct_expand(const Emitter emit, const Type t) { const Instr instr = emit_add_instr(emit, Reg2RegDeref); - instr->m_val = -SZ_INT; - instr->m_val2 = t->size; + instr->m_val = -SZ_INT; + instr->m_val2 = t->size; } - -ANN static void emit_pre_constructor_array(const Emitter emit, const Type type) { +ANN static void emit_pre_constructor_array(const Emitter emit, + const Type type) { const m_uint start_index = emit_code_size(emit); - const Instr top = emit_add_instr(emit, ArrayTop); - top->m_val2 = (m_uint)type; - if(tflag(type, tflag_struct)) { + const Instr top = emit_add_instr(emit, ArrayTop); + top->m_val2 = (m_uint)type; + if (tflag(type, tflag_struct)) { const Instr instr = emit_add_instr(emit, ArrayStruct); - instr->m_val = type->size; + instr->m_val = type->size; } emit_pre_ctor(emit, type); - if(!tflag(type, tflag_struct)) + if (!tflag(type, tflag_struct)) emit_add_instr(emit, ArrayBottom); else regpop(emit, SZ_INT); regpop(emit, SZ_INT); const Instr pc = emit_add_instr(emit, Goto); - pc->m_val = start_index; - top->m_val = emit_code_size(emit); - regpop(emit, SZ_INT*3); + pc->m_val = start_index; + top->m_val = emit_code_size(emit); + regpop(emit, SZ_INT * 3); emit_add_instr(emit, ArrayPost); } -ANN2(1) static m_bool extend_indices(const Emitter emit, Exp e, const m_uint depth) { - if(e) - CHECK_BB(emit_exp(emit, e)); +ANN2(1) +static m_bool extend_indices(const Emitter emit, Exp e, const m_uint depth) { + if (e) CHECK_BB(emit_exp(emit, e)); m_uint count = 0; - while(e) { + while (e) { ++count; e = e->next; } - for(m_uint i = count; i < depth; ++i) - regpushi(emit, 0); + for (m_uint i = count; i < depth; ++i) regpushi(emit, 0); return GW_OK; } -ANEW ANN static ArrayInfo* new_arrayinfo(const Emitter emit, const Type t) { +ANEW ANN static ArrayInfo *new_arrayinfo(const Emitter emit, const Type t) { const Type base = array_base(t); - ArrayInfo* info = mp_calloc(emit->gwion->mp, ArrayInfo); + ArrayInfo *info = mp_calloc(emit->gwion->mp, ArrayInfo); vector_init(&info->type); info->depth = get_depth(t); - for(long i = 1; i < info->depth; ++i) + for (long i = 1; i < info->depth; ++i) vector_add(&info->type, (vtype)array_type(emit->env, base, i)); vector_add(&info->type, (vtype)t); info->base = base; @@ -358,50 +371,53 @@ ANEW ANN static ArrayInfo* new_arrayinfo(const Emitter emit, const Type t) { ANN static inline void arrayinfo_ctor(const Emitter emit, ArrayInfo *info) { const Type base = info->base; - if(isa(base, emit->gwion->type[et_compound]) > 0 && !GET_FLAG(base, abstract)) { + if (isa(base, emit->gwion->type[et_compound]) > 0 && + !GET_FLAG(base, abstract)) { emit_pre_constructor_array(emit, base); info->is_obj = 1; } } -ANN2(1,2) static ArrayInfo* emit_array_extend_inner(const Emitter emit, const Type t, const Exp e, const uint is_ref) { +ANN2(1, 2) +static ArrayInfo *emit_array_extend_inner(const Emitter emit, const Type t, + const Exp e, const uint is_ref) { CHECK_BO(extend_indices(emit, e, get_depth(t))); - ArrayInfo* info = new_arrayinfo(emit, t); + ArrayInfo * info = new_arrayinfo(emit, t); const Instr alloc = emit_add_instr(emit, ArrayAlloc); - alloc->m_val = (m_uint)info; - if(!is_ref) - arrayinfo_ctor(emit, info); + alloc->m_val = (m_uint)info; + if (!is_ref) arrayinfo_ctor(emit, info); return info; } ANN void emit_ext_ctor(const Emitter emit, const Type t) { const Instr cpy = emit_add_instr(emit, Reg2Reg); - cpy->m_val2 = -SZ_INT; - if(t->nspc->pre_ctor) { + cpy->m_val2 = -SZ_INT; + if (t->nspc->pre_ctor) { const Instr set_code = regseti(emit, (m_uint)t->nspc->pre_ctor); - set_code->m_val2 = SZ_INT; + set_code->m_val2 = SZ_INT; } else { const Instr instr = emit_add_instr(emit, SetCtor); - instr->m_val = (m_uint)t; + instr->m_val = (m_uint)t; } const m_uint offset = emit_code_offset(emit); - const Instr regset = regseti(emit, offset /*+ SZ_INT */ /*+ sizeof(frame_t)*/); - regset->m_val2 = SZ_INT *2; - regpush(emit, SZ_INT*2); + const Instr regset = + regseti(emit, offset /*+ SZ_INT */ /*+ sizeof(frame_t)*/); + regset->m_val2 = SZ_INT * 2; + regpush(emit, SZ_INT * 2); const Instr prelude = emit_add_instr(emit, SetCode); - prelude->m_val = -SZ_INT * 2; - prelude->udata.one = 2; + prelude->m_val = -SZ_INT * 2; + prelude->udata.one = 2; emit_add_instr(emit, Reg2Mem); const Instr next = emit_add_instr(emit, Overflow); - next->m_val2 = offset; + next->m_val2 = offset; } ANN m_bool emit_array_extend(const Emitter emit, const Type t, const Exp e) { CHECK_OB(emit_array_extend_inner(emit, t, e, 0)); regpop(emit, SZ_INT); const Instr instr = emit_add_instr(emit, Reg2Reg); - instr->m_val = -SZ_INT; -// emit_add_instr(emit, RegAddRef); + instr->m_val = -SZ_INT; + // emit_add_instr(emit, RegAddRef); return GW_OK; } @@ -409,47 +425,54 @@ ANN static inline void emit_notpure(const Emitter emit) { ++VPTR(&emit->info->pure, VLEN(&emit->info->pure) - 1); } -ANN2(1,2) m_bool emit_instantiate_object(const Emitter emit, const Type type, - const Array_Sub array, const m_bool is_ref) { +ANN2(1, 2) +m_bool emit_instantiate_object(const Emitter emit, const Type type, + const Array_Sub array, const m_bool is_ref) { emit_notpure(emit); - if(type->array_depth) { - DECL_OB(ArrayInfo*, info, = emit_array_extend_inner(emit, type, array ? array->exp : NULL, is_ref)); + if (type->array_depth) { + DECL_OB(ArrayInfo *, info, + = emit_array_extend_inner(emit, type, array ? array->exp : NULL, + is_ref)); return GW_OK; - } else if(!is_ref) { + } else if (!is_ref) { const Instr instr = emit_add_instr(emit, ObjectInstantiate); - instr->m_val2 = (m_uint)type; + instr->m_val2 = (m_uint)type; emit_pre_ctor(emit, type); } return GW_OK; } -ANN2(1,2) m_bool emit_instantiate_decl(const Emitter emit, const Type type, - const Type_Decl *td, const Array_Sub array, const m_bool is_ref) { +ANN2(1, 2) +m_bool emit_instantiate_decl(const Emitter emit, const Type type, + const Type_Decl *td, const Array_Sub array, + const m_bool is_ref) { Exp base = td->array ? td->array->exp : NULL, exp = base, - next = array ? array->exp : NULL; - const m_uint depth = (td->array ? td->array->depth : 0) + (array ? array->depth : 0); - if(exp) { - while(exp->next) - exp = exp->next; + next = array ? array->exp : NULL; + const m_uint depth = + (td->array ? td->array->depth : 0) + (array ? array->depth : 0); + if (exp) { + while (exp->next) exp = exp->next; exp->next = next; - } else base = next; - struct Array_Sub_ a = { .exp=base, .depth=depth }; - const m_bool ret = emit_instantiate_object(emit, type, &a, is_ref); - if(td->array && td->array->exp) - exp->next = NULL; + } else + base = next; + struct Array_Sub_ a = {.exp = base, .depth = depth}; + const m_bool ret = emit_instantiate_object(emit, type, &a, is_ref); + if (td->array && td->array->exp) exp->next = NULL; return ret; } ANN static m_bool emit_symbol_builtin(const Emitter emit, const Symbol *data) { const Value v = prim_self(data)->value; - if(vflag(v, vflag_direct)) { + if (vflag(v, vflag_direct)) { const m_uint size = v->type->size; - const Instr instr = emit_kind(emit, size, exp_getvar(prim_exp(data)), dotstatic); + const Instr instr = + emit_kind(emit, size, exp_getvar(prim_exp(data)), dotstatic); instr->m_val = (m_uint)&v->d.ptr; } else { const m_uint size = v->type->size; - const Instr instr = emit_kind(emit, size, exp_getvar(prim_exp(data)), regpushimm); - if(v->type->size == SZ_FLOAT) + const Instr instr = + emit_kind(emit, size, exp_getvar(prim_exp(data)), regpushimm); + if (v->type->size == SZ_FLOAT) instr->f = v->d.fnum; else instr->m_val = v->d.num; @@ -460,35 +483,38 @@ ANN static m_bool emit_symbol_builtin(const Emitter emit, const Symbol *data) { ANN static m_bool _emit_symbol(const Emitter emit, const Symbol *data) { const Value v = prim_self(data)->value; - if(is_class(emit->gwion, v->type)) { + if (is_class(emit->gwion, v->type)) { regpushi(emit, (m_uint)actual_type(emit->gwion, v->type)); return GW_OK; } - if(vflag(v, vflag_builtin) || vflag(v, vflag_direct)) + if (vflag(v, vflag_builtin) || vflag(v, vflag_direct)) return emit_symbol_builtin(emit, data); - if(!strncmp(v->type->name, "Ref:[", 5)) { - if(exp_getvar(exp_self(prim_self(data)))) { + if (!strncmp(v->type->name, "Ref:[", 5)) { + if (exp_getvar(exp_self(prim_self(data)))) { const Instr instr = emit_add_instr(emit, RegPushMem); - instr->m_val = v->from->offset; - instr->m_val2 = SZ_INT; + instr->m_val = v->from->offset; + instr->m_val2 = SZ_INT; } else { const Instr instr = emit_add_instr(emit, RegPushMemDeref); - instr->m_val = v->from->offset; - instr->m_val2 = v->type->size; + instr->m_val = v->from->offset; + instr->m_val2 = v->type->size; } return GW_OK; } const m_uint size = v->type->size; - const Instr instr = emit_kind(emit, size, exp_getvar(prim_exp(data)), !vflag(v, vflag_fglobal) ? regpushmem : regpushbase); - instr->m_val = v->from->offset; - if(GET_FLAG(v, late) && !exp_getvar(prim_exp(data)) && isa(v->type, emit->gwion->type[et_object]) > 0) { + const Instr instr = + emit_kind(emit, size, exp_getvar(prim_exp(data)), + !vflag(v, vflag_fglobal) ? regpushmem : regpushbase); + instr->m_val = v->from->offset; + if (GET_FLAG(v, late) && !exp_getvar(prim_exp(data)) && + isa(v->type, emit->gwion->type[et_object]) > 0) { const Instr instr = emit_add_instr(emit, GWOP_EXCEPT); - instr->m_val = -SZ_INT; + instr->m_val = -SZ_INT; } return GW_OK; } -ANN static m_bool emit_symbol(const Emitter emit, const Exp_Primary* prim) { +ANN static m_bool emit_symbol(const Emitter emit, const Exp_Primary *prim) { return _emit_symbol(emit, &prim->d.var); } @@ -496,52 +522,56 @@ ANN static VM_Code finalyze(const Emitter emit, const f_instr exec) { emit_add_instr(emit, exec); const VM_Code code = emit->info->emit_code(emit); free_code(emit->gwion->mp, emit->code); - emit->code = (Code*)vector_pop(&emit->stack); + emit->code = (Code *)vector_pop(&emit->stack); return code; } ANN static inline m_uint exp_size(const Exp e) { - if(exp_getvar(e)) - return SZ_INT; + if (exp_getvar(e)) return SZ_INT; const Type type = e->cast_to ?: e->type; return type->size; } - ANN static inline m_uint exp_totalsize(Exp e) { m_uint size = 0; do size += exp_size(e); - while((e = e->next)); + while ((e = e->next)); return size; } -ANN Instr emit_object_addref(const Emitter emit, const m_int size, const m_bool emit_var) { - const f_instr exec = !emit_var ? RegAddRef : RegAddRefAddr; - const Instr instr = emit_add_instr(emit, exec); - instr->m_val = size; +ANN Instr emit_object_addref(const Emitter emit, const m_int size, + const m_bool emit_var) { + const f_instr exec = !emit_var ? RegAddRef : RegAddRefAddr; + const Instr instr = emit_add_instr(emit, exec); + instr->m_val = size; return instr; } -ANN Instr emit_struct_addref(const Emitter emit, const Type t, const m_int size, const m_bool emit_var) { - const Instr instr = emit_add_instr(emit, !emit_var ? StructRegAddRef : StructRegAddRefAddr); - instr->m_val = (m_uint)t; +ANN Instr emit_struct_addref(const Emitter emit, const Type t, const m_int size, + const m_bool emit_var) { + const Instr instr = + emit_add_instr(emit, !emit_var ? StructRegAddRef : StructRegAddRefAddr); + instr->m_val = (m_uint)t; instr->m_val2 = size; return instr; } -ANN2(1) static void emit_exp_addref1(const Emitter emit, const Exp exp, m_int size) { +ANN2(1) +static void emit_exp_addref1(const Emitter emit, const Exp exp, m_int size) { const Type t = exp->cast_to ?: exp->type; - if(isa(t, emit->gwion->type[et_compound]) > 0) -// emit_object_addref(emit, size, exp_getvar(exp)); -// else if(tflag(t, tflag_struct)) + if (isa(t, emit->gwion->type[et_compound]) > 0) + // emit_object_addref(emit, size, exp_getvar(exp)); + // else if(tflag(t, tflag_struct)) emit_compound_addref(emit, exp->type, size, exp_getvar(exp)); } -ANN2(1) static void emit_exp_addref(const Emitter emit, /* const */Exp exp, m_int size) { +ANN2(1) +static void emit_exp_addref(const Emitter emit, /* const */ Exp exp, + m_int size) { do { emit_exp_addref1(emit, exp, size); size += exp_size(exp); - } while((exp = exp->next)); + } while ((exp = exp->next)); } ANN static m_bool emit_prim_array(const Emitter emit, const Array_Sub *data) { @@ -550,12 +580,12 @@ ANN static m_bool emit_prim_array(const Emitter emit, const Array_Sub *data) { emit_exp_addref(emit, e, -exp_totalsize(e)); m_uint count = 0; do ++count; - while((e = e->next)); + while ((e = e->next)); const Type type = (*data)->type; regseti(emit, count); const Instr instr = emit_add_instr(emit, ArrayInit); - instr->m_val = (m_uint)type; - instr->m_val2 = array_base(type)->size; + instr->m_val = (m_uint)type; + instr->m_val2 = array_base(type)->size; emit_gc(emit, -SZ_INT); emit_notpure(emit); return GW_OK; @@ -564,11 +594,11 @@ ANN static m_bool emit_prim_array(const Emitter emit, const Array_Sub *data) { ANN static inline m_bool emit_exp_pop_next(const Emitter emit, Exp e); ANN static m_bool emit_range(const Emitter emit, Range *range) { - if(range->start) + if (range->start) CHECK_BB(emit_exp_pop_next(emit, range->start)); else regpushi(emit, 0); - if(range->end) + if (range->end) CHECK_BB(emit_exp_pop_next(emit, range->end)); else regpushi(emit, -1); @@ -578,74 +608,86 @@ ANN static m_bool emit_range(const Emitter emit, Range *range) { ANN static m_bool emit_prim_range(const Emitter emit, Range **data) { Range *range = *data; CHECK_BB(emit_range(emit, range)); - const Exp e = range->start ?: range->end; - const Symbol sym = insert_symbol("@range"); - struct Op_Import opi = { .op=sym, .rhs=e->type, - .pos=e->pos, .data=(uintptr_t)prim_exp(data) }; + const Exp e = range->start ?: range->end; + const Symbol sym = insert_symbol("@range"); + struct Op_Import opi = {.op = sym, + .rhs = e->type, + .pos = e->pos, + .data = (uintptr_t)prim_exp(data)}; CHECK_BB(op_emit(emit, &opi)); emit_gc(emit, -SZ_INT); return GW_OK; } -ANN m_bool emit_array_access(const Emitter emit, struct ArrayAccessInfo *const info) { - if(tflag(info->array.type, tflag_typedef)) { +ANN m_bool emit_array_access(const Emitter emit, + struct ArrayAccessInfo *const info) { + if (tflag(info->array.type, tflag_typedef)) { info->array.type = info->array.type->info->parent; return emit_array_access(emit, info); } // look mum no pos - struct Op_Import opi = { .op=insert_symbol("@array"), .lhs=info->array.exp->type, .rhs=info->array.type, - .data=(uintptr_t)info }; - if(!info->is_var && (GET_FLAG(info->array.type, abstract) || type_ref(info->array.type))) { + struct Op_Import opi = {.op = insert_symbol("@array"), + .lhs = info->array.exp->type, + .rhs = info->array.type, + .data = (uintptr_t)info}; + if (!info->is_var && + (GET_FLAG(info->array.type, abstract) || type_ref(info->array.type))) { const Instr instr = emit_add_instr(emit, GWOP_EXCEPT); - instr->m_val = -SZ_INT; + instr->m_val = -SZ_INT; } return op_emit(emit, &opi); } -ANN static m_bool emit_exp_array(const Emitter emit, const Exp_Array* array) { +ANN static m_bool emit_exp_array(const Emitter emit, const Exp_Array *array) { CHECK_BB(emit_exp(emit, array->base)); - const Exp e = exp_self(array); - struct ArrayAccessInfo info = { *array->array, e->type, exp_getvar(e) }; + const Exp e = exp_self(array); + struct ArrayAccessInfo info = {*array->array, e->type, exp_getvar(e)}; return emit_array_access(emit, &info); } -ANN static m_bool emit_exp_slice(const Emitter emit, const Exp_Slice* range) { +ANN static m_bool emit_exp_slice(const Emitter emit, const Exp_Slice *range) { CHECK_BB(emit_exp(emit, range->base)); CHECK_BB(emit_range(emit, range->range)); - const Symbol sym = insert_symbol("@slice"); - const Exp e = range->range->start ?: range->range->end; - struct Op_Import opi = { .op=sym, .lhs=e->type, .rhs=range->base->type, - .pos=e->pos, .data=(uintptr_t)exp_self(range) }; + const Symbol sym = insert_symbol("@slice"); + const Exp e = range->range->start ?: range->range->end; + struct Op_Import opi = {.op = sym, + .lhs = e->type, + .rhs = range->base->type, + .pos = e->pos, + .data = (uintptr_t)exp_self(range)}; return op_emit(emit, &opi); } -ANN static inline Instr specialid_instr(const Emitter emit, - struct SpecialId_ *spid, const Exp_Primary* prim) { +ANN static inline Instr specialid_instr(const Emitter emit, + struct SpecialId_ *spid, + const Exp_Primary *prim) { return spid->exec ? emit_add_instr(emit, spid->exec) : spid->em(emit, prim); } -static const f_instr upvalue[] = { UpvalueInt, UpvalueFloat, UpvalueOther, UpvalueAddr }; -ANN static m_bool emit_prim_id(const Emitter emit, const Symbol *data) { +static const f_instr upvalue[] = {UpvalueInt, UpvalueFloat, UpvalueOther, + UpvalueAddr}; +ANN static m_bool emit_prim_id(const Emitter emit, const Symbol *data) { const Exp_Primary *prim = prim_self(data); - if(prim->value && emit->env->func && emit->env->func->upvalues.ptr) { + if (prim->value && emit->env->func && emit->env->func->upvalues.ptr) { const Map map = &emit->env->func->upvalues; - for(m_uint i = 0; i < map_size(map); ++i) { - if(prim->value == (Value)((Exp_Primary*)VKEY(map, i))->value) { - const Instr instr = emit_kind(emit, prim->value->type->size, exp_getvar(exp_self(prim)), upvalue); - instr->m_val = i ? VVAL(map, i) : 0; + for (m_uint i = 0; i < map_size(map); ++i) { + if (prim->value == (Value)((Exp_Primary *)VKEY(map, i))->value) { + const Instr instr = emit_kind(emit, prim->value->type->size, + exp_getvar(exp_self(prim)), upvalue); + instr->m_val = i ? VVAL(map, i) : 0; return GW_OK; } } } - struct SpecialId_ * spid = specialid_get(emit->gwion, *data); - if(spid) + struct SpecialId_ *spid = specialid_get(emit->gwion, *data); + if (spid) return specialid_instr(emit, spid, prim_self(data)) ? GW_OK : GW_ERROR; return emit_symbol(emit, prim_self(data)); } ANN static m_bool emit_prim_perform(const Emitter emit, const Symbol *xid) { const Instr instr = emit_add_instr(emit, PerformEffect); - instr->m_val = (m_uint)s_name(*xid); + instr->m_val = (m_uint)s_name(*xid); return GW_OK; } @@ -656,7 +698,7 @@ ANN static m_bool emit_prim_num(const Emitter emit, const m_uint *num) { ANN static m_bool emit_prim_float(const Emitter emit, const m_float *fnum) { const Instr instr = emit_add_instr(emit, RegPushImm2); - instr->f = *fnum; + instr->f = *fnum; return GW_OK; } @@ -668,67 +710,68 @@ ANN static m_bool emit_prim_char(const Emitter emit, const m_str *str) { ANN static m_bool emit_prim_str(const Emitter emit, const m_str *str) { const Value v = prim_self(str)->value; - if(!v->d.obj) { + if (!v->d.obj) { char c[strlen(*str) + 1]; - if(strlen(*str)) { + if (strlen(*str)) { strcpy(c, *str); - CHECK_BB(escape_str(emit, c, prim_pos(str)));; - } else c[0] = '\0'; - v->d.obj = new_string2(emit->gwion, NULL, c); + CHECK_BB(escape_str(emit, c, prim_pos(str))); + ; + } else + c[0] = '\0'; + v->d.obj = new_string2(emit->gwion, NULL, c); } regpushi(emit, (m_uint)v->d.obj); emit_object_addref(emit, -SZ_INT, 0); return GW_OK; } -#define emit_prim_nil (void*)dummy_func +#define emit_prim_nil (void *)dummy_func ANN static void interp_multi(const Emitter emit, const Exp e) { - Var_Decl_List list = e->d.exp_decl.list; - const int emit_var = exp_getvar(e); - m_uint offset = 0; - while((list = list->next)) + Var_Decl_List list = e->d.exp_decl.list; + const int emit_var = exp_getvar(e); + m_uint offset = 0; + while ((list = list->next)) offset += !emit_var ? list->self->value->type->size : SZ_INT; - if(offset) - regpop(emit, offset); + if (offset) regpop(emit, offset); } ANN static inline void interp_size(const Emitter emit, const Type t) { const Instr instr = regseti(emit, t->size); - instr->m_val2 = SZ_INT; + instr->m_val2 = SZ_INT; } ANN /*static*/ m_bool emit_interp(const Emitter emit, const Exp exp) { regpushi(emit, 0); Exp e = exp, next = NULL; do { - next = e->next; + next = e->next; e->next = NULL; - if(emit_exp(emit, e) < 0) { + if (emit_exp(emit, e) < 0) { e->next = next; return GW_ERROR; } - if(e->exp_type == ae_exp_decl) // why only objects? + if (e->exp_type == ae_exp_decl) // why only objects? interp_multi(emit, e); regseti(emit, (m_uint)e->type); interp_size(emit, e->type); const m_bool isobj = isa(e->type, emit->gwion->type[et_object]) > 0; - if(isobj && e->exp_type != ae_exp_cast) - emit_add_instr(emit, GackType); + if (isobj && e->exp_type != ae_exp_cast) emit_add_instr(emit, GackType); const Instr instr = emit_add_instr(emit, Gack); - instr->m_val = emit_code_offset(emit); - } while((e = e->next = next)); + instr->m_val = emit_code_offset(emit); + } while ((e = e->next = next)); return GW_OK; } ANN static m_bool emit_prim_hack(const Emitter emit, const Exp *exp) { CHECK_BB(emit_interp(emit, *exp)); - if(!(emit->env->func && emit->env->func->def->base->xid == insert_symbol("@gack"))) + if (!(emit->env->func && + emit->env->func->def->base->xid == insert_symbol("@gack"))) emit_add_instr(emit, GackEnd); else { const Instr instr = emit_add_instr(emit, Reg2Mem); - instr->m_val = SZ_INT; - instr->m_val2 = -SZ_INT; + instr->m_val = SZ_INT; + instr->m_val2 = -SZ_INT; } return GW_OK; } @@ -737,31 +780,34 @@ ANN static m_bool emit_prim_interp(const Emitter emit, const Exp *exp) { const Exp e = *exp; CHECK_BB(emit_interp(emit, e)); const Instr instr = emit_add_instr(emit, GackEnd); - instr->m_val = 1; + instr->m_val = 1; return GW_OK; } -DECL_PRIM_FUNC(emit, m_bool , Emitter); +DECL_PRIM_FUNC(emit, m_bool, Emitter); ANN static m_bool emit_prim(const Emitter emit, Exp_Primary *const prim) { return emit_prim_func[prim->prim_type](emit, &prim->d); } -ANN static m_bool emit_dot_static_data(const Emitter emit, const Value v, const uint emit_var) { - const m_uint size = v->type->size; - const Instr instr = emit_kind(emit, size, emit_var, dotstatic); - instr->m_val = (m_uint)(v->from->owner->info->class_data + v->from->offset); +ANN static m_bool emit_dot_static_data(const Emitter emit, const Value v, + const uint emit_var) { + const m_uint size = v->type->size; + const Instr instr = emit_kind(emit, size, emit_var, dotstatic); + instr->m_val = (m_uint)(v->from->owner->info->class_data + v->from->offset); instr->m_val2 = size; return GW_OK; } -ANN static m_bool decl_static(const Emitter emit, const Exp_Decl *decl, const Var_Decl var_decl, const uint is_ref) { - const Value v = var_decl->value; - Code* code = emit->code; - emit->code = (Code*)vector_back(&emit->stack); - CHECK_BB(emit_instantiate_decl(emit, v->type, decl->td, var_decl->array, is_ref)); +ANN static m_bool decl_static(const Emitter emit, const Exp_Decl *decl, + const Var_Decl var_decl, const uint is_ref) { + const Value v = var_decl->value; + Code * code = emit->code; + emit->code = (Code *)vector_back(&emit->stack); + CHECK_BB( + emit_instantiate_decl(emit, v->type, decl->td, var_decl->array, is_ref)); CHECK_BB(emit_dot_static_data(emit, v, 1)); emit_add_instr(emit, Assign); -// (void)emit_object_addref(emit, -SZ_INT, 0); + // (void)emit_object_addref(emit, -SZ_INT, 0); regpop(emit, SZ_INT); emit->code = code; return GW_OK; @@ -776,153 +822,172 @@ ANN static void decl_expand(const Emitter emit, const Type t) { regpush(emit, t->size - SZ_INT); } -ANN static void emit_struct_decl_finish(const Emitter emit, const Type t, const uint emit_addr) { +ANN static void emit_struct_decl_finish(const Emitter emit, const Type t, + const uint emit_addr) { emit->code->frame->curr_offset += t->size + SZ_INT; emit_ext_ctor(emit, t); - if(!emit_addr) - decl_expand(emit, t); + if (!emit_addr) decl_expand(emit, t); emit->code->frame->curr_offset -= t->size + SZ_INT; } -ANN static m_bool emit_exp_decl_static(const Emitter emit, const Exp_Decl *decl, const Var_Decl var_decl, const uint is_ref, const uint emit_addr) { +ANN static m_bool emit_exp_decl_static(const Emitter emit, const Exp_Decl *decl, + const Var_Decl var_decl, + const uint is_ref, + const uint emit_addr) { const Value v = var_decl->value; - if(isa(v->type, emit->gwion->type[et_object]) > 0 && !is_ref) + if (isa(v->type, emit->gwion->type[et_object]) > 0 && !is_ref) CHECK_BB(decl_static(emit, decl, var_decl, 0)); CHECK_BB(emit_dot_static_data(emit, v, !struct_ctor(v) ? emit_addr : 1)); - if(struct_ctor(v)) - emit_struct_decl_finish(emit, v->type, emit_addr); + if (struct_ctor(v)) emit_struct_decl_finish(emit, v->type, emit_addr); return GW_OK; } -ANN static Instr emit_struct_decl(const Emitter emit, const Value v, const m_bool emit_addr) { +ANN static Instr emit_struct_decl(const Emitter emit, const Value v, + const m_bool emit_addr) { emit_add_instr(emit, RegPushMem); const Instr instr = emit_kind(emit, v->type->size, emit_addr, structmember); - if(!emit_addr) { + if (!emit_addr) { const m_int sz = v->type->size - SZ_INT; - if(sz) - regpush(emit, v->type->size - SZ_INT); + if (sz) regpush(emit, v->type->size - SZ_INT); } return instr; } -ANN static m_bool emit_exp_decl_non_static(const Emitter emit, const Exp_Decl *decl, const Var_Decl var_decl, - const uint is_ref, const uint emit_var) { - const Value v = var_decl->value; - const Type type = v->type; - const Array_Sub array = var_decl->array; - const m_bool is_array = array && array->exp; - const m_bool is_obj = isa(type, emit->gwion->type[et_object]) > 0; - const uint emit_addr = (!is_obj || (is_ref && !is_array)) ? emit_var : 1; - if(is_obj && (is_array || !is_ref)) +ANN static m_bool emit_exp_decl_non_static(const Emitter emit, + const Exp_Decl *decl, + const Var_Decl var_decl, + const uint is_ref, + const uint emit_var) { + const Value v = var_decl->value; + const Type type = v->type; + const Array_Sub array = var_decl->array; + const m_bool is_array = array && array->exp; + const m_bool is_obj = isa(type, emit->gwion->type[et_object]) > 0; + const uint emit_addr = (!is_obj || (is_ref && !is_array)) ? emit_var : 1; + if (is_obj && (is_array || !is_ref)) CHECK_BB(emit_instantiate_decl(emit, type, decl->td, array, is_ref)); - f_instr *exec = (f_instr*)allocmember; - if(!emit->env->scope->depth) - emit_debug(emit, v); - if(!vflag(v, vflag_member)) { + f_instr *exec = (f_instr *)allocmember; + if (!emit->env->scope->depth) emit_debug(emit, v); + if (!vflag(v, vflag_member)) { v->from->offset = emit_local(emit, type); - exec = (f_instr*)(allocword); - if(GET_FLAG(v, late)) { // ref or emit_var ? + exec = (f_instr *)(allocword); + if (GET_FLAG(v, late)) { // ref or emit_var ? const Instr clean = emit_add_instr(emit, MemSetImm); - clean->m_val = v->from->offset; + clean->m_val = v->from->offset; } } - const Instr instr = !(safe_tflag(emit->env->class_def, tflag_struct) && !emit->env->scope->depth) ? - emit_kind(emit, v->type->size, !struct_ctor(v) ? emit_addr : 1, exec) : emit_struct_decl(emit, v, !struct_ctor(v) ? emit_addr : 1); + const Instr instr = + !(safe_tflag(emit->env->class_def, tflag_struct) && + !emit->env->scope->depth) + ? emit_kind(emit, v->type->size, !struct_ctor(v) ? emit_addr : 1, + exec) + : emit_struct_decl(emit, v, !struct_ctor(v) ? emit_addr : 1); instr->m_val = v->from->offset; - if(is_obj && (is_array || !is_ref)) { - if(!emit_var) + if (is_obj && (is_array || !is_ref)) { + if (!emit_var) emit_add_instr(emit, Assign); else { regpop(emit, SZ_INT); const Instr instr = emit_add_instr(emit, Reg2Reg); - instr->m_val = -SZ_INT; + instr->m_val = -SZ_INT; } - } else if(struct_ctor(v)) + } else if (struct_ctor(v)) emit_struct_decl_finish(emit, v->type, emit_addr); return GW_OK; } -ANN static m_bool emit_exp_decl_global(const Emitter emit, const Exp_Decl *decl, const Var_Decl var_decl, - const uint is_ref, const uint emit_var) { - const Value v = var_decl->value; - const Type type = v->type; - const Array_Sub array = var_decl->array; - const m_bool is_array = array && array->exp; - const m_bool is_obj = isa(type, emit->gwion->type[et_object]) > 0; - const uint emit_addr = (!is_obj || (is_ref && !is_array)) ? emit_var : 1; - if(is_obj && (is_array || !is_ref)) +ANN static m_bool emit_exp_decl_global(const Emitter emit, const Exp_Decl *decl, + const Var_Decl var_decl, + const uint is_ref, const uint emit_var) { + const Value v = var_decl->value; + const Type type = v->type; + const Array_Sub array = var_decl->array; + const m_bool is_array = array && array->exp; + const m_bool is_obj = isa(type, emit->gwion->type[et_object]) > 0; + const uint emit_addr = (!is_obj || (is_ref && !is_array)) ? emit_var : 1; + if (is_obj && (is_array || !is_ref)) CHECK_BB(emit_instantiate_decl(emit, type, decl->td, array, is_ref)); - const Instr instr = emit_kind(emit, v->type->size, !struct_ctor(v) ? emit_addr : 1, dotstatic); - if(type->size > SZ_INT) //{ + const Instr instr = emit_kind(emit, v->type->size, + !struct_ctor(v) ? emit_addr : 1, dotstatic); + if (type->size > SZ_INT) //{ v->d.ptr = mp_calloc2(emit->gwion->mp, v->type->size); instr->m_val = (m_uint)&v->d.ptr; - set_vflag(v, vflag_direct);// mpalloc + set_vflag(v, vflag_direct); // mpalloc instr->m_val2 = v->type->size; - if(is_obj && (is_array || !is_ref || emit_addr)) { + if (is_obj && (is_array || !is_ref || emit_addr)) { const Instr assign = emit_add_instr(emit, Assign); - assign->m_val = emit_var; + assign->m_val = emit_var; (void)emit_object_addref(emit, -SZ_INT, emit_var); - } else if(struct_ctor(v)) + } else if (struct_ctor(v)) emit_struct_decl_finish(emit, v->type, emit_addr); return GW_OK; } -ANN static void set_late(const Gwion gwion, const Exp_Decl *decl, const Var_Decl var) { - const Value v = var->value; - const uint array_ref = (decl->td->array && !decl->td->array->exp) || (var->array && !var->array->exp); - if(!exp_getvar(exp_self(decl)) && (GET_FLAG(array_base(v->type), abstract) || - GET_FLAG(decl->td, late) || is_fptr(gwion, v->type) || array_ref)) { +ANN static void set_late(const Gwion gwion, const Exp_Decl *decl, + const Var_Decl var) { + const Value v = var->value; + const uint array_ref = (decl->td->array && !decl->td->array->exp) || + (var->array && !var->array->exp); + if (!exp_getvar(exp_self(decl)) && + (GET_FLAG(array_base(v->type), abstract) || GET_FLAG(decl->td, late) || + is_fptr(gwion, v->type) || array_ref)) { SET_FLAG(v, late); } else UNSET_FLAG(v, late); } -ANN static m_bool emit_decl(const Emitter emit, const Exp_Decl* decl) { - const m_bool global = GET_FLAG(decl->td, global); - const uint var = exp_getvar(exp_self(decl)); - const uint ref = GET_FLAG(decl->td, late) || type_ref(decl->type); - Var_Decl_List list = decl->list; +ANN static m_bool emit_decl(const Emitter emit, const Exp_Decl *decl) { + const m_bool global = GET_FLAG(decl->td, global); + const uint var = exp_getvar(exp_self(decl)); + const uint ref = GET_FLAG(decl->td, late) || type_ref(decl->type); + Var_Decl_List list = decl->list; do { const Value v = list->self->value; - const uint r = ref || GET_FLAG(v, late); - if(GET_FLAG(decl->td, static)) + const uint r = ref || GET_FLAG(v, late); + if (GET_FLAG(decl->td, static)) CHECK_BB(emit_exp_decl_static(emit, decl, list->self, r, var)); - else if(!global) + else if (!global) CHECK_BB(emit_exp_decl_non_static(emit, decl, list->self, r, var)); else CHECK_BB(emit_exp_decl_global(emit, decl, list->self, r, var)); - if(tflag(list->self->value->type, tflag_contract) && !exp_getvar(exp_self(decl))) { - const Type t = list->self->value->type; - struct Op_Import opi = { .lhs=t->info->base_type, .op=insert_symbol("@implicit"), .rhs=t }; + if (tflag(list->self->value->type, tflag_contract) && + !exp_getvar(exp_self(decl))) { + const Type t = list->self->value->type; + struct Op_Import opi = {.lhs = t->info->base_type, + .op = insert_symbol("@implicit"), + .rhs = t}; CHECK_BB(op_emit(emit, &opi)); } set_late(emit->gwion, decl, list->self); - if(GET_FLAG(array_base(v->type), abstract) && !GET_FLAG(decl->td, late) && GET_FLAG(v, late)) { - env_warn(emit->env, decl->td->pos, _("Type '%s' is abstract, use late"), v->type->name); + if (GET_FLAG(array_base(v->type), abstract) && !GET_FLAG(decl->td, late) && + GET_FLAG(v, late)) { + env_warn(emit->env, decl->td->pos, _("Type '%s' is abstract, use late"), + v->type->name); } - } while((list = list->next)); + } while ((list = list->next)); return GW_OK; } -ANN /*static */m_bool emit_exp_decl(const Emitter emit, const Exp_Decl* decl) { +ANN /*static */ m_bool emit_exp_decl(const Emitter emit, const Exp_Decl *decl) { const Type t = decl->type; CHECK_BB(ensure_emit(emit, t)); const m_bool global = GET_FLAG(decl->td, global); - const m_uint scope = !global ? emit->env->scope->depth : emit_push_global(emit); + const m_uint scope = + !global ? emit->env->scope->depth : emit_push_global(emit); const m_bool ret = emit_decl(emit, decl); - if(global) - emit_pop(emit, scope); + if (global) emit_pop(emit, scope); return ret; } -ANN static m_uint vararg_size(const Gwion gwion, const Exp_Call* exp_call, const Vector kinds) { - Exp e = exp_call->args; - const Type t = actual_type(gwion, exp_call->func->type); - Arg_List l = t->info->func->def->base->args; - m_uint size = 0; - while(e) { - if(!l) { +ANN static m_uint vararg_size(const Gwion gwion, const Exp_Call *exp_call, + const Vector kinds) { + Exp e = exp_call->args; + const Type t = actual_type(gwion, exp_call->func->type); + Arg_List l = t->info->func->def->base->args; + m_uint size = 0; + while (e) { + if (!l) { size += e->type->size; vector_add(kinds, (vtype)e->type); // ->size } else @@ -932,102 +997,107 @@ ANN static m_uint vararg_size(const Gwion gwion, const Exp_Call* exp_call, const return size; } -ANN static void emit_func_arg_vararg(const Emitter emit, const Exp_Call* exp_call) { - const Instr instr = emit_add_instr(emit, VarargIni); +ANN static void emit_func_arg_vararg(const Emitter emit, + const Exp_Call *exp_call) { + const Instr instr = emit_add_instr(emit, VarargIni); const Vector kinds = new_vector(emit->gwion->mp); - if((instr->m_val = vararg_size(emit->gwion, exp_call, kinds))) + if ((instr->m_val = vararg_size(emit->gwion, exp_call, kinds))) instr->m_val2 = (m_uint)kinds; else free_vector(emit->gwion->mp, kinds); } -ANN static m_bool emit_func_args(const Emitter emit, const Exp_Call* exp_call) { - if(exp_call->args) { +ANN static m_bool emit_func_args(const Emitter emit, const Exp_Call *exp_call) { + if (exp_call->args) { CHECK_BB(emit_exp(emit, exp_call->args)); -// emit_exp_addref(emit, exp_call->args, -exp_totalsize(exp_call->args)); + // emit_exp_addref(emit, exp_call->args, -exp_totalsize(exp_call->args)); } const Type t = actual_type(emit->gwion, exp_call->func->type); - if(isa(t, emit->gwion->type[et_function]) > 0 && - fbflag(t->info->func->def->base, fbflag_variadic)) + if (isa(t, emit->gwion->type[et_function]) > 0 && + fbflag(t->info->func->def->base, fbflag_variadic)) emit_func_arg_vararg(emit, exp_call); return GW_OK; } -ANN static m_bool prepare_call(const Emitter emit, const Exp_Call* exp_call) { +ANN static m_bool prepare_call(const Emitter emit, const Exp_Call *exp_call) { CHECK_BB(emit_func_args(emit, exp_call)); return emit_exp(emit, exp_call->func); } ANN static inline void emit_return_pc(const Emitter emit, const m_uint val) { LOOP_OPTIM - for(m_uint i = vector_size(&emit->code->stack_return) + 1; --i; ) { - const Instr instr = (Instr)vector_at(&emit->code->stack_return, i-1); - instr->m_val = val; + for (m_uint i = vector_size(&emit->code->stack_return) + 1; --i;) { + const Instr instr = (Instr)vector_at(&emit->code->stack_return, i - 1); + instr->m_val = val; } } ANN static inline void pop_exp(const Emitter emit, Exp e); ANN static inline bool check_inline(const Emitter emit, const Func f) { - const uint16_t caller_size = emit->env->func ? emit->env->func->weight : - emit->env->class_def ? emit->env->class_def->weight : - emit->env->context ? emit->env->context->weight : 0; - const float threshold = caller_size * f->inline_mult; + const uint16_t caller_size = emit->env->func ? emit->env->func->weight + : emit->env->class_def + ? emit->env->class_def->weight + : emit->env->context ? emit->env->context->weight + : 0; + const float threshold = caller_size * f->inline_mult; return f->weight < threshold; } ANN static inline bool member_inlinable(const Func f, const Exp e) { - if(fflag(f, fflag_recurs)) - return false; - const Type owner_class =f->value_ref->from->owner_class; - if(!owner_class) - return true; - return GET_FLAG(owner_class, final) || - GET_FLAG(f->def->base, final) || - (e->exp_type == ae_exp_dot && e->d.exp_dot.base->exp_type == ae_exp_cast); + if (fflag(f, fflag_recurs)) return false; + const Type owner_class = f->value_ref->from->owner_class; + if (!owner_class) return true; + return GET_FLAG(owner_class, final) || GET_FLAG(f->def->base, final) || + (e->exp_type == ae_exp_dot && + e->d.exp_dot.base->exp_type == ae_exp_cast); } -ANN static inline Func is_inlinable(const Emitter emit, const Exp_Call *exp_call) { +ANN static inline Func is_inlinable(const Emitter emit, + const Exp_Call *exp_call) { const Type ftype = exp_call->func->type; - if(isa(ftype, emit->gwion->type[et_function]) < 0 || is_fptr(emit->gwion, ftype) || - !ftype->info->func->code || ftype->info->func->code->builtin) + if (isa(ftype, emit->gwion->type[et_function]) < 0 || + is_fptr(emit->gwion, ftype) || !ftype->info->func->code || + ftype->info->func->code->builtin) return false; const Func f = ftype->info->func; - return (member_inlinable(f, exp_call->func) && check_inline(emit, f)) ? - f : NULL; + return (member_inlinable(f, exp_call->func) && check_inline(emit, f)) ? f + : NULL; } -ANN static inline void inline_args_ini(const Emitter emit, const Func f, const Vector v) { +ANN static inline void inline_args_ini(const Emitter emit, const Func f, + const Vector v) { const m_uint start_offset = emit_code_offset(emit); - Arg_List arg = f->def->base->args; - while(arg) { + Arg_List arg = f->def->base->args; + while (arg) { const Value value = arg->var_decl->value; vector_add(v, value->from->offset); value->from->offset = emit_local(emit, value->type); nspc_add_value(emit->env->curr, arg->var_decl->xid, value); arg = arg->next; } - if(fbflag(f->def->base, fbflag_variadic)) + if (fbflag(f->def->base, fbflag_variadic)) emit->vararg_offset = emit_local(emit, emit->gwion->type[et_int]) + SZ_INT; regpop(emit, f->code->stack_depth); const Instr cpy = emit_add_instr(emit, Reg2Mem4); - cpy->m_val2 = f->code->stack_depth; - cpy->m_val = start_offset; + cpy->m_val2 = f->code->stack_depth; + cpy->m_val = start_offset; } ANN static inline void inline_args_end(const Func f, const Vector v) { Arg_List arg = f->def->base->args; - m_uint i = 0; - while(arg) { - const Value value = arg->var_decl->value; + m_uint i = 0; + while (arg) { + const Value value = arg->var_decl->value; value->from->offset = vector_at(v, i++); - arg = arg->next; + arg = arg->next; } } -ANN static m_bool scoped_stmt(const Emitter emit, const Stmt stmt, const m_bool pop); +ANN static m_bool scoped_stmt(const Emitter emit, const Stmt stmt, + const m_bool pop); ANN static inline m_bool inline_body(const Emitter emit, const Func f) { - struct Vector_ v = { .ptr=emit->code->stack_return.ptr }; + struct Vector_ v = {.ptr = emit->code->stack_return.ptr}; vector_init(&emit->code->stack_return); nspc_push_value(emit->gwion->mp, emit->env->curr); const m_bool ret = scoped_stmt(emit, f->def->d.code, 1); @@ -1041,21 +1111,21 @@ ANN static inline m_bool inline_body(const Emitter emit, const Func f) { ANN static inline m_bool inline_run(const Emitter emit, const Func f) { struct Vector_ arg_offset; vector_init(&arg_offset); - const uint16_t this_offset = emit->this_offset; + const uint16_t this_offset = emit->this_offset; const uint16_t vararg_offset = emit->vararg_offset; inline_args_ini(emit, f, &arg_offset); - const m_bool ret = inline_body(emit, f); - emit->this_offset = this_offset; + const m_bool ret = inline_body(emit, f); + emit->this_offset = this_offset; emit->vararg_offset = vararg_offset; inline_args_end(f, &arg_offset); vector_release(&arg_offset); return ret; } -ANN static inline m_bool emit_inline(const Emitter emit, const Func f, const Exp_Call *exp_call) { - if(!f->weight) - return GW_OK; - if(f->value_ref->from->owner_class && vflag(f->value_ref, vflag_member)) { +ANN static inline m_bool emit_inline(const Emitter emit, const Func f, + const Exp_Call *exp_call) { + if (!f->weight) return GW_OK; + if (f->value_ref->from->owner_class && vflag(f->value_ref, vflag_member)) { CHECK_BB(emit_exp(emit, exp_call->func->d.exp_dot.base)); emit->this_offset = emit_local(emit, emit->gwion->type[et_int]); } @@ -1063,240 +1133,255 @@ ANN static inline m_bool emit_inline(const Emitter emit, const Func f, const Exp return inline_run(emit, f); } -ANN static m_bool _emit_exp_call(const Emitter emit, const Exp_Call* exp_call) { -/* -#ifndef GWION_NOINLINE - const Func f = is_inlinable(emit, exp_call); - if(f) - return emit_inline(emit, f, exp_call); -#endif -*/ +ANN static m_bool _emit_exp_call(const Emitter emit, const Exp_Call *exp_call) { + /* + #ifndef GWION_NOINLINE + const Func f = is_inlinable(emit, exp_call); + if(f) + return emit_inline(emit, f, exp_call); + #endif + */ // skip when recursing const Type t = actual_type(emit->gwion, exp_call->func->type); - const Func f = t->info->func; - if(is_fptr(emit->gwion, t) || f != emit->env->func || f->value_ref->from->owner_class || strstr(emit->code->name, "ork~")) + const Func f = t->info->func; + if (is_fptr(emit->gwion, t) || f != emit->env->func || + f->value_ref->from->owner_class || strstr(emit->code->name, "ork~")) CHECK_BB(prepare_call(emit, exp_call)); else CHECK_BB(emit_func_args(emit, exp_call)); - if(isa(t, emit->gwion->type[et_function]) > 0) - CHECK_BB(emit_exp_call1(emit, t->info->func, is_static_call(emit, exp_call->func))); + if (isa(t, emit->gwion->type[et_function]) > 0) + CHECK_BB(emit_exp_call1(emit, t->info->func, + is_static_call(emit, exp_call->func))); else { - struct Op_Import opi = { .op=insert_symbol("@ctor"), .rhs=t, - .data=(uintptr_t)exp_call, .pos=exp_self(exp_call)->pos }; + struct Op_Import opi = {.op = insert_symbol("@ctor"), + .rhs = t, + .data = (uintptr_t)exp_call, + .pos = exp_self(exp_call)->pos}; CHECK_BB(op_emit(emit, &opi)); } return GW_OK; } -ANN static m_bool emit_exp_call(const Emitter emit, const Exp_Call* exp_call) { +ANN static m_bool emit_exp_call(const Emitter emit, const Exp_Call *exp_call) { const Exp e = exp_self(exp_call); - if(exp_getvar(e)) - regpush(emit, SZ_INT); + if (exp_getvar(e)) regpush(emit, SZ_INT); CHECK_BB(_emit_exp_call(emit, exp_call)); - if(exp_getvar(e)) { + if (exp_getvar(e)) { regpop(emit, exp_self(exp_call)->type->size); const Instr instr = emit_add_instr(emit, Reg2RegAddr); - instr->m_val = -SZ_INT; - } else if(isa(exp_call->func->type, emit->gwion->type[et_function]) < 0 && - tflag(e->type, tflag_struct)) + instr->m_val = -SZ_INT; + } else if (isa(exp_call->func->type, emit->gwion->type[et_function]) < 0 && + tflag(e->type, tflag_struct)) regpop(emit, SZ_INT); return GW_OK; } ANN static m_uint get_decl_size(Var_Decl_List a, uint emit_addr) { m_uint size = 0; - do //if(GET_FLAG(a->self->value, used)) + do // if(GET_FLAG(a->self->value, used)) size += !emit_addr ? a->self->value->type->size : SZ_INT; - while((a = a->next)); + while ((a = a->next)); return size; } ANN static m_uint pop_exp_size(Exp e) { const uint emit_addr = exp_getvar(e); - m_uint size = 0; + m_uint size = 0; do { // account for emit_var ? - size += (e->exp_type == ae_exp_decl ? - get_decl_size(e->d.exp_decl.list, emit_addr) : e->type->size); - } while((e = e->next)); + size += (e->exp_type == ae_exp_decl + ? get_decl_size(e->d.exp_decl.list, emit_addr) + : e->type->size); + } while ((e = e->next)); return size; } ANN static inline void pop_exp(const Emitter emit, Exp e) { const m_uint size = pop_exp_size(e); - if(size) - regpop(emit, size); + if (size) regpop(emit, size); } ANN static inline m_bool emit_exp_pop_next(const Emitter emit, Exp e) { CHECK_BB(emit_exp(emit, e)); - if(e->exp_type == ae_exp_decl) { + if (e->exp_type == ae_exp_decl) { Var_Decl_List list = e->d.exp_decl.list->next; - while(list) { + while (list) { regpop(emit, !exp_getvar(e) ? list->self->value->type->size : SZ_INT); list = list->next; } } - if(e->next) - pop_exp(emit, e->next); + if (e->next) pop_exp(emit, e->next); return GW_OK; } -ANN static m_bool emit_exp_binary(const Emitter emit, const Exp_Binary* bin) { +ANN static m_bool emit_exp_binary(const Emitter emit, const Exp_Binary *bin) { const Exp lhs = bin->lhs; const Exp rhs = bin->rhs; CHECK_BB(emit_exp_pop_next(emit, lhs)); CHECK_BB(emit_exp_pop_next(emit, rhs)); -// const m_int size = exp_size(rhs); -// emit_exp_addref1(emit, lhs, -exp_size(lhs) - size); -// emit_exp_addref1(emit, rhs, -size); - struct Op_Import opi = { .op=bin->op, .lhs=lhs->type, .rhs=rhs->type, - .pos=exp_self(bin)->pos, .data=(uintptr_t)bin }; + // const m_int size = exp_size(rhs); + // emit_exp_addref1(emit, lhs, -exp_size(lhs) - size); + // emit_exp_addref1(emit, rhs, -size); + struct Op_Import opi = {.op = bin->op, + .lhs = lhs->type, + .rhs = rhs->type, + .pos = exp_self(bin)->pos, + .data = (uintptr_t)bin}; return op_emit(emit, &opi); } -ANN static m_bool emit_exp_cast(const Emitter emit, const Exp_Cast* cast) { +ANN static m_bool emit_exp_cast(const Emitter emit, const Exp_Cast *cast) { CHECK_BB(emit_exp(emit, cast->exp)); - struct Op_Import opi = { .op=insert_symbol("$"), .lhs=cast->exp->type, .rhs=exp_self(cast)->type, - .data=(uintptr_t)cast }; // no pos ? + struct Op_Import opi = {.op = insert_symbol("$"), + .lhs = cast->exp->type, + .rhs = exp_self(cast)->type, + .data = (uintptr_t)cast}; // no pos ? (void)op_emit(emit, &opi); return GW_OK; } -ANN static m_bool emit_exp_post(const Emitter emit, const Exp_Postfix* post) { +ANN static m_bool emit_exp_post(const Emitter emit, const Exp_Postfix *post) { CHECK_BB(emit_exp(emit, post->exp)); - struct Op_Import opi = { .op=post->op, .lhs=post->exp->type, - .data=(uintptr_t)post }; // no pos ? + struct Op_Import opi = {.op = post->op, + .lhs = post->exp->type, + .data = (uintptr_t)post}; // no pos ? return op_emit(emit, &opi); } -ANN static inline m_bool traverse_emit_func_def(const Emitter emit, const Func_Def fdef) { - if(!fdef->base->ret_type) - CHECK_BB(traverse_func_def(emit->env, fdef)); +ANN static inline m_bool traverse_emit_func_def(const Emitter emit, + const Func_Def fdef) { + if (!fdef->base->ret_type) CHECK_BB(traverse_func_def(emit->env, fdef)); return emit_func_def(emit, fdef); } ANN m_bool traverse_dot_tmpl(const Emitter emit, const struct dottmpl_ *dt) { - const m_uint scope = emit->env->scope->depth; - struct EnvSet es = { .env=emit->env, .data=emit, .func=(_exp_func)emit_cdef, - .scope=scope, .flag=tflag_emit }; + const m_uint scope = emit->env->scope->depth; + struct EnvSet es = {.env = emit->env, + .data = emit, + .func = (_exp_func)emit_cdef, + .scope = scope, + .flag = tflag_emit}; CHECK_BB(envset_push(&es, dt->owner_class, dt->owner)); (void)emit_push(emit, dt->owner_class, dt->owner); const m_bool ret = traverse_emit_func_def(emit, dt->def); - if(es.run) - envset_pop(&es, dt->owner_class); + if (es.run) envset_pop(&es, dt->owner_class); emit_pop(emit, scope); return ret; } static inline m_bool push_func_code(const Emitter emit, const Func f) { - if(!vector_size(&emit->code->instr)) - return GW_OK; + if (!vector_size(&emit->code->instr)) return GW_OK; const Instr instr = (Instr)vector_back(&emit->code->instr); - if(instr->opcode == eDotTmplVal) { + if (instr->opcode == eDotTmplVal) { size_t len = strlen(f->name); - size_t sz = len - strlen(f->value_ref->from->owner_class->name); - char c[sz + 1]; + size_t sz = len - strlen(f->value_ref->from->owner_class->name); + char c[sz + 1]; memcpy(c, f->name, sz); - c[sz] = '\0'; + c[sz] = '\0'; struct dottmpl_ *dt = mp_calloc(emit->gwion->mp, dottmpl); - dt->name = s_name(insert_symbol(c)); - dt->tl = cpy_type_list(emit->gwion->mp, f->def->base->tmpl->call); - dt->base = f->def; - instr->opcode = eOP_MAX; - instr->m_val = (m_uint)dt; - instr->m_val2 = strlen(c); + dt->name = s_name(insert_symbol(c)); + dt->tl = cpy_type_list(emit->gwion->mp, f->def->base->tmpl->call); + dt->base = f->def; + instr->opcode = eOP_MAX; + instr->m_val = (m_uint)dt; + instr->m_val2 = strlen(c); instr->execute = DotTmpl; return GW_OK; } instr->opcode = eRegPushImm; - instr->m_val = (m_uint)f->code; + instr->m_val = (m_uint)f->code; return GW_OK; } ANN static m_bool emit_template_code(const Emitter emit, const Func f) { - const Value v = f->value_ref; - const size_t scope = emit->env->scope->depth; - struct EnvSet es = { .env=emit->env, .data=emit, .func=(_exp_func)emit_cdef, - .scope=scope, .flag=tflag_emit }; + const Value v = f->value_ref; + const size_t scope = emit->env->scope->depth; + struct EnvSet es = {.env = emit->env, + .data = emit, + .func = (_exp_func)emit_cdef, + .scope = scope, + .flag = tflag_emit}; CHECK_BB(envset_pushv(&es, v)); (void)emit_push(emit, v->from->owner_class, v->from->owner); const m_bool ret = emit_func_def(emit, f->def); - if(es.run) - envset_pop(&es, v->from->owner_class); + if (es.run) envset_pop(&es, v->from->owner_class); emit_pop(emit, scope); return ret > 0 ? push_func_code(emit, f) : GW_ERROR; } ANN static void tmpl_prelude(const Emitter emit, const Func f) { - struct dottmpl_ *dt = (struct dottmpl_*)mp_calloc(emit->gwion->mp, dottmpl); - size_t len = strlen(f->name); - size_t slen = strlen(f->value_ref->from->owner->name); + struct dottmpl_ *dt = (struct dottmpl_ *)mp_calloc(emit->gwion->mp, dottmpl); + size_t len = strlen(f->name); + size_t slen = strlen(f->value_ref->from->owner->name); assert(len > slen); size_t sz = len - slen; - char c[sz + 1]; + char c[sz + 1]; memcpy(c, f->name, sz); - c[sz] = '\0'; - dt->tl = cpy_type_list(emit->gwion->mp, f->def->base->tmpl->call); - dt->name = s_name(insert_symbol(c)); - dt->base = f->def; - dt->owner = f->value_ref->from->owner; - dt->owner_class = f->value_ref->from->owner_class; + c[sz] = '\0'; + dt->tl = cpy_type_list(emit->gwion->mp, f->def->base->tmpl->call); + dt->name = s_name(insert_symbol(c)); + dt->base = f->def; + dt->owner = f->value_ref->from->owner; + dt->owner_class = f->value_ref->from->owner_class; const Instr gtmpl = emit_add_instr(emit, GTmpl); - gtmpl->m_val = (m_uint)dt; - gtmpl->m_val2 = strlen(c); + gtmpl->m_val = (m_uint)dt; + gtmpl->m_val2 = strlen(c); } -ANN static Instr get_prelude(const Emitter emit, const Func f, const bool is_static) { - const Type t = actual_type(emit->gwion, f->value_ref->type); +ANN static Instr get_prelude(const Emitter emit, const Func f, + const bool is_static) { + const Type t = actual_type(emit->gwion, f->value_ref->type); const bool fp = is_fptr(emit->gwion, t); - if(is_fptr(emit->gwion, t)) { - if(f->def->base->tmpl) - tmpl_prelude(emit, f); + if (is_fptr(emit->gwion, t)) { + if (f->def->base->tmpl) tmpl_prelude(emit, f); } - if(fp || f != emit->env->func || !is_static || strstr(emit->code->name, "ork~")) { + if (fp || f != emit->env->func || !is_static || + strstr(emit->code->name, "ork~")) { const Instr instr = emit_add_instr(emit, SetCode); - instr->udata.one = 1; + instr->udata.one = 1; return instr; } const Instr instr = emit_add_instr(emit, Recurs); - instr->m_val = SZ_INT; - instr->udata.one = 1; - instr->udata.two = emit_code_offset(emit) + sizeof(frame_t); + instr->m_val = SZ_INT; + instr->udata.one = 1; + instr->udata.two = emit_code_offset(emit) + sizeof(frame_t); return instr; } ANN static void emit_args(const Emitter emit, const Func f) { const m_uint member = vflag(f->value_ref, vflag_member) ? SZ_INT : 0; - if((f->def->stack_depth - member) == SZ_INT) { + if ((f->def->stack_depth - member) == SZ_INT) { const Instr instr = emit_add_instr(emit, Reg2Mem); - instr->m_val = member; + instr->m_val = member; } else { const Instr instr = emit_add_instr(emit, Reg2Mem4); - instr->m_val = member; - instr->m_val2 = f->def->stack_depth - member; + instr->m_val = member; + instr->m_val2 = f->def->stack_depth - member; } } typedef struct { const Emitter emit; const Func_Def fdef; - struct Vector_ branch; + struct Vector_ branch; const m_uint offset; m_uint arg_offset; } MemoizeEmitter; ANN static Instr me_push(const MemoizeEmitter *me, const m_uint sz) { const Instr instr = emit_kind(me->emit, sz, 0, regpushmem); - instr->m_val = me->arg_offset; + instr->m_val = me->arg_offset; return instr; } static m_bool me_cmp(MemoizeEmitter *me, const Arg_List arg) { - const Emitter emit = me->emit; - const Symbol sym = insert_symbol("=="); - struct Exp_ exp = {}; - struct Op_Import opi = { .op=sym, .lhs=arg->type, .rhs=arg->type, - .pos=me->fdef->base->pos, .data=(uintptr_t)&exp.d }; + const Emitter emit = me->emit; + const Symbol sym = insert_symbol("=="); + struct Exp_ exp = {}; + struct Op_Import opi = {.op = sym, + .lhs = arg->type, + .rhs = arg->type, + .pos = me->fdef->base->pos, + .data = (uintptr_t)&exp.d}; CHECK_BB(op_emit(emit, &opi)); const Instr instr = emit_add_instr(emit, BranchEqInt); vector_add(&me->branch, (vtype)instr); @@ -1309,103 +1394,112 @@ ANN static m_bool me_arg(MemoizeEmitter *me) { const m_uint sz = arg->type->size; (void)me_push(me, sz); const Instr instr = me_push(me, sz); - instr->m_val += me->offset + SZ_INT *2; + instr->m_val += me->offset + SZ_INT * 2; CHECK_BB(me_cmp(me, arg)); me->arg_offset += arg->type->size; - } while((arg = arg->next)); + } while ((arg = arg->next)); return GW_OK; } -ANN static Instr emit_call(const Emitter emit, const Func f, const bool is_static) { +ANN static Instr emit_call(const Emitter emit, const Func f, + const bool is_static) { const Instr prelude = get_prelude(emit, f, is_static); prelude->m_val += -f->def->stack_depth - SZ_INT; const m_uint member = vflag(f->value_ref, vflag_member) ? SZ_INT : 0; - if(member) { + if (member) { const Instr instr = emit_add_instr(emit, Reg2Mem); - instr->m_val2 = f->def->stack_depth - SZ_INT; + instr->m_val2 = f->def->stack_depth - SZ_INT; ++prelude->m_val2; } - if(f->def->stack_depth - member) { -// if(is_fptr(emit->gwion, t) || f != emit->env->func || f->value_ref->from->owner_class || strstr(emit->code->name, "ork~")) -{ - emit_args(emit, f); - ++prelude->m_val2; -} + if (f->def->stack_depth - member) { + // if(is_fptr(emit->gwion, t) || f != emit->env->func || + // f->value_ref->from->owner_class || strstr(emit->code->name, "ork~")) + { + emit_args(emit, f); + ++prelude->m_val2; + } } return emit_add_instr(emit, Overflow); } -ANN m_bool emit_exp_call1(const Emitter emit, const Func f, const bool is_static) { - const m_uint this_offset = emit->this_offset; +ANN m_bool emit_exp_call1(const Emitter emit, const Func f, + const bool is_static) { + const m_uint this_offset = emit->this_offset; const m_uint vararg_offset = emit->vararg_offset; - emit->this_offset = 0; - const int tmpl = fflag(f, fflag_tmpl); - if(!f->code || (fflag(f, fflag_ftmpl) && !vflag(f->value_ref, vflag_builtin))) { - if(tmpl && !is_fptr(emit->gwion, f->value_ref->type)) { - if(emit->env->func != f) + emit->this_offset = 0; + const int tmpl = fflag(f, fflag_tmpl); + if (!f->code || + (fflag(f, fflag_ftmpl) && !vflag(f->value_ref, vflag_builtin))) { + if (tmpl && !is_fptr(emit->gwion, f->value_ref->type)) { + if (emit->env->func != f) CHECK_BB(emit_template_code(emit, f)); else { // recursive function. (maybe should be used only for global funcs) - /* const Instr back = (Instr) vector_size(&emit->code->instr) ? - (Instr)vector_back(&emit->code->instr) : emit_add_instr(emit, RegPushImm); - back->opcode = eOP_MAX; - back->execute = SetRecurs; - back->m_val = 0;*/ + /* const Instr back = (Instr) vector_size(&emit->code->instr) ? + (Instr)vector_back(&emit->code->instr) : emit_add_instr(emit, + RegPushImm); back->opcode = eOP_MAX; back->execute = SetRecurs; + back->m_val = 0;*/ } - } else if(emit->env->func != f && !f->value_ref->from->owner_class && !f->code && !is_fptr(emit->gwion, f->value_ref->type)) { - if(fbflag(f->def->base, fbflag_op)) { + } else if (emit->env->func != f && !f->value_ref->from->owner_class && + !f->code && !is_fptr(emit->gwion, f->value_ref->type)) { + if (fbflag(f->def->base, fbflag_op)) { const Instr back = (Instr)vector_back(&emit->code->instr); - back->m_val = (m_uint)f; + back->m_val = (m_uint)f; } else { -// ensure env? + // ensure env? CHECK_BB(emit_func_def(emit, f->def)); const Instr instr = emit_add_instr(emit, RegSetImm); - instr->m_val = (m_uint)f->code; - instr->m_val2 = -SZ_INT; + instr->m_val = (m_uint)f->code; + instr->m_val2 = -SZ_INT; } } - } else if((f->value_ref->from->owner_class && tflag(f->value_ref->from->owner_class, tflag_struct)) || - !f->value_ref->from->owner_class || (tmpl && - !is_fptr(emit->gwion, f->value_ref->type))) + } else if ((f->value_ref->from->owner_class && + tflag(f->value_ref->from->owner_class, tflag_struct)) || + !f->value_ref->from->owner_class || + (tmpl && !is_fptr(emit->gwion, f->value_ref->type))) push_func_code(emit, f); - else if(vector_size(&emit->code->instr)) { + else if (vector_size(&emit->code->instr)) { const Instr back = (Instr)vector_back(&emit->code->instr); - if((f_instr)(m_uint)back->opcode == DotFunc) - back->m_val = f->vt_index; + if ((f_instr)(m_uint)back->opcode == DotFunc) back->m_val = f->vt_index; } - if(vector_size(&emit->code->instr) && vflag(f->value_ref, vflag_member) && - is_fptr(emit->gwion, f->value_ref->type)) { + if (vector_size(&emit->code->instr) && vflag(f->value_ref, vflag_member) && + is_fptr(emit->gwion, f->value_ref->type)) { const Instr back = (Instr)vector_back(&emit->code->instr); - const Instr base = back->opcode != eGWOP_EXCEPT ? - back : (Instr)vector_at(&emit->code->instr, vector_size(&emit->code->instr) -2); - const m_bit exec = base->opcode; - const m_uint val = base->m_val; + const Instr base = + back->opcode != eGWOP_EXCEPT + ? back + : (Instr)vector_at(&emit->code->instr, + vector_size(&emit->code->instr) - 2); + const m_bit exec = base->opcode; + const m_uint val = base->m_val; const m_uint val2 = base->m_val2; - base->opcode = eReg2Reg; - base->m_val2 = -SZ_INT; + base->opcode = eReg2Reg; + base->m_val2 = -SZ_INT; regpush(emit, SZ_INT); const Instr instr = emit_add_instr(emit, (f_instr)(m_uint)exec); - instr->m_val = val; - instr->m_val2 = val2; - } else if(f != emit->env->func && !f->code && !is_fptr(emit->gwion, f->value_ref->type)){ + instr->m_val = val; + instr->m_val2 = val2; + } else if (f != emit->env->func && !f->code && + !is_fptr(emit->gwion, f->value_ref->type)) { // not yet emitted static func - if(f->value_ref->from->owner_class) { - const Instr instr = vector_size(&emit->code->instr) ? - (Instr)vector_back(&emit->code->instr) : emit_add_instr(emit, SetFunc); - instr->opcode = eOP_MAX; - instr->execute = SetFunc; - instr->m_val = (m_uint)f; + if (f->value_ref->from->owner_class) { + const Instr instr = vector_size(&emit->code->instr) + ? (Instr)vector_back(&emit->code->instr) + : emit_add_instr(emit, SetFunc); + instr->opcode = eOP_MAX; + instr->execute = SetFunc; + instr->m_val = (m_uint)f; } else { const Instr instr = emit_add_instr(emit, SetFunc); - instr->m_val = (m_uint)f; + instr->m_val = (m_uint)f; } } const m_uint offset = emit_code_offset(emit); - if(f != emit->env->func || !is_static) + if (f != emit->env->func || !is_static) regseti(emit, offset /*+ f->def->stack_depth + */ /*+ sizeof(frame_t)*/); - const Instr instr = emit_call(emit, f, is_static); - instr->m_val = f->def->base->ret_type->size; - instr->m_val2 = offset; - emit->this_offset = this_offset; + const Instr instr = emit_call(emit, f, is_static); + instr->m_val = f->def->base->ret_type->size; + instr->m_val2 = offset; + emit->this_offset = this_offset; emit->vararg_offset = vararg_offset; return GW_OK; } @@ -1413,12 +1507,14 @@ ANN m_bool emit_exp_call1(const Emitter emit, const Func f, const bool is_static ANN static void emit_exp_spork_finish(const Emitter emit, const m_uint depth) { regpop(emit, depth); const Instr spork = emit_add_instr(emit, SporkFunc); - spork->m_val = depth + SZ_INT; - spork->m_val2 = -SZ_INT; + spork->m_val = depth + SZ_INT; + spork->m_val2 = -SZ_INT; } -ANN static inline void stack_alloc(const Emitter emit) { // maybe vararg could use t_vararg instead - emit_local(emit, emit->gwion->type[et_int]); // hiding the fact it is an object +ANN static inline void +stack_alloc(const Emitter emit) { // maybe vararg could use t_vararg instead + emit_local(emit, + emit->gwion->type[et_int]); // hiding the fact it is an object emit->code->stack_depth += SZ_INT; } @@ -1430,168 +1526,183 @@ ANN static inline Instr scoped_ini(const Emitter emit) { ANN static inline void scoped_end(const Emitter emit, const Instr gc) { emit_pop_scope(emit); - const m_bool pure = vector_size(&emit->info->pure) && !vector_back(&emit->info->pure); - if(!pure) { + const m_bool pure = + vector_size(&emit->info->pure) && !vector_back(&emit->info->pure); + if (!pure) { gc->opcode = eGcIni; emit_add_instr(emit, GcEnd); } --emit->env->scope->depth; } -ANN static m_bool scoped_stmt(const Emitter emit, const Stmt stmt, const m_bool pop) { - const Instr gc = scoped_ini(emit); +ANN static m_bool scoped_stmt(const Emitter emit, const Stmt stmt, + const m_bool pop) { + const Instr gc = scoped_ini(emit); const m_bool ret = emit_stmt(emit, stmt, pop); scoped_end(emit, gc); return ret; } #define SPORK_FUNC_PREFIX "spork~func:%u" -#define FORK_FUNC_PREFIX "fork~func:%u" +#define FORK_FUNC_PREFIX "fork~func:%u" #define SPORK_CODE_PREFIX "spork~code:%u" #define FORK_CODE_PREFIX "fork~code:%u" -static void push_spork_code(const Emitter emit, const m_str prefix, const loc_t pos) { +static void push_spork_code(const Emitter emit, const m_str prefix, + const loc_t pos) { char c[strlen(SPORK_FUNC_PREFIX) + num_digit(pos.first.line) + 1]; sprintf(c, prefix, pos.first.line); emit_push_code(emit, c); } struct Sporker { - const Stmt code; - const Exp exp; - VM_Code vm_code; - const Type type; + const Stmt code; + const Exp exp; + VM_Code vm_code; + const Type type; const m_bool emit_var; const m_bool is_spork; }; -ANN static m_bool spork_prepare_code(const Emitter emit, const struct Sporker *sp) { +ANN static m_bool spork_prepare_code(const Emitter emit, + const struct Sporker *sp) { emit_add_instr(emit, RegPushImm); - push_spork_code(emit, sp->is_spork ? SPORK_CODE_PREFIX : FORK_CODE_PREFIX, sp->code->pos); - if(emit->env->func && vflag(emit->env->func->value_ref, vflag_member)) + push_spork_code(emit, sp->is_spork ? SPORK_CODE_PREFIX : FORK_CODE_PREFIX, + sp->code->pos); + if (emit->env->func && vflag(emit->env->func->value_ref, vflag_member)) stack_alloc(emit); return scoped_stmt(emit, sp->code, 0); } -ANN static m_bool spork_prepare_func(const Emitter emit, const struct Sporker *sp) { - push_spork_code(emit, sp->is_spork ? SPORK_FUNC_PREFIX : FORK_CODE_PREFIX, sp->exp->pos); +ANN static m_bool spork_prepare_func(const Emitter emit, + const struct Sporker *sp) { + push_spork_code(emit, sp->is_spork ? SPORK_FUNC_PREFIX : FORK_CODE_PREFIX, + sp->exp->pos); const Type t = actual_type(emit->gwion, sp->exp->d.exp_call.func->type); return emit_exp_call1(emit, t->info->func, false); } ANN static VM_Code spork_prepare(const Emitter emit, const struct Sporker *sp) { - if(!sp->code) - CHECK_BO(prepare_call(emit, &sp->exp->d.exp_call)); - if((sp->code ? spork_prepare_code : spork_prepare_func)(emit, sp) > 0) + if (!sp->code) CHECK_BO(prepare_call(emit, &sp->exp->d.exp_call)); + if ((sp->code ? spork_prepare_code : spork_prepare_func)(emit, sp) > 0) return finalyze(emit, EOC); emit_pop_code(emit); return NULL; } ANN void spork_code(const Emitter emit, const struct Sporker *sp) { - const Instr args = emit_add_instr(emit, SporkExp); - args->m_val = emit->code->stack_depth; + const Instr args = emit_add_instr(emit, SporkExp); + args->m_val = emit->code->stack_depth; const Instr instr = emit_add_instr(emit, SporkEnd); - instr->m_val = sp->emit_var; + instr->m_val = sp->emit_var; } ANN void spork_func(const Emitter emit, const struct Sporker *sp) { - const Func f = actual_type(emit->gwion, sp->exp->d.exp_call.func->type)->info->func; - if(vflag(f->value_ref, vflag_member) && is_fptr(emit->gwion, f->value_ref->type)) { - regpush(emit, SZ_INT*2); + const Func f = + actual_type(emit->gwion, sp->exp->d.exp_call.func->type)->info->func; + if (vflag(f->value_ref, vflag_member) && + is_fptr(emit->gwion, f->value_ref->type)) { + regpush(emit, SZ_INT * 2); // (re-)emit owner - if(sp->exp->d.exp_call.func->exp_type == ae_exp_dot) + if (sp->exp->d.exp_call.func->exp_type == ae_exp_dot) emit_exp(emit, sp->exp->d.exp_call.func->d.exp_dot.base); else { assert(sp->exp->d.exp_call.func->exp_type == ae_exp_primary); emit_add_instr(emit, RegPushMem); } - regpop(emit, SZ_INT*3); + regpop(emit, SZ_INT * 3); const m_uint depth = f->def->stack_depth; - regpop(emit, depth -SZ_INT); + regpop(emit, depth - SZ_INT); const Instr spork = emit_add_instr(emit, SporkMemberFptr); - spork->m_val = depth; + spork->m_val = depth; } else emit_exp_spork_finish(emit, f->def->stack_depth); (void)emit_add_instr(emit, SporkEnd); } ANN static void spork_ini(const Emitter emit, const struct Sporker *sp) { - if(sp->is_spork) { + if (sp->is_spork) { const Instr instr = emit_add_instr(emit, SporkIni); - instr->m_val = (m_uint)sp->vm_code; - instr->m_val2 = sp->is_spork; + instr->m_val = (m_uint)sp->vm_code; + instr->m_val2 = sp->is_spork; return; } regpushi(emit, (m_uint)sp->type); const Instr instr = emit_add_instr(emit, ForkIni); - instr->m_val = (m_uint)sp->vm_code; - instr->m_val2 = sp->type->size; + instr->m_val = (m_uint)sp->vm_code; + instr->m_val2 = sp->type->size; } -ANN m_bool emit_exp_spork(const Emitter emit, const Exp_Unary* unary) { +ANN m_bool emit_exp_spork(const Emitter emit, const Exp_Unary *unary) { struct Sporker sporker = { - .exp= unary->unary_type == unary_exp ? unary->exp : NULL, - .code= unary->unary_type == unary_code ? unary->code : NULL, - .type=exp_self(unary)->type, - .is_spork=(unary->op == insert_symbol("spork")), - .emit_var=exp_getvar(exp_self(unary)) - }; + .exp = unary->unary_type == unary_exp ? unary->exp : NULL, + .code = unary->unary_type == unary_code ? unary->code : NULL, + .type = exp_self(unary)->type, + .is_spork = (unary->op == insert_symbol("spork")), + .emit_var = exp_getvar(exp_self(unary))}; CHECK_OB((sporker.vm_code = spork_prepare(emit, &sporker))); spork_ini(emit, &sporker); (unary->unary_type == unary_code ? spork_code : spork_func)(emit, &sporker); return GW_OK; } -ANN static m_bool emit_exp_unary(const Emitter emit, const Exp_Unary* unary) { - const Type t = exp_self(unary)->type; +ANN static m_bool emit_exp_unary(const Emitter emit, const Exp_Unary *unary) { + const Type t = exp_self(unary)->type; const Type base = actual_type(emit->gwion, t); CHECK_BB(ensure_emit(emit, base)); // no pos ? - struct Op_Import opi = { .op=unary->op, .data=(uintptr_t)unary }; - if(unary->unary_type == unary_exp && unary->op != insert_symbol("spork") && unary->op != insert_symbol("fork")) { + struct Op_Import opi = {.op = unary->op, .data = (uintptr_t)unary}; + if (unary->unary_type == unary_exp && unary->op != insert_symbol("spork") && + unary->op != insert_symbol("fork")) { CHECK_BB(emit_exp_pop_next(emit, unary->exp)); opi.rhs = unary->exp->type; } return op_emit(emit, &opi); } -ANN static m_bool emit_implicit_cast(const Emitter emit, - const restrict Exp from, const restrict Type to) { - const struct Implicit imp = { from, to, from->pos }; +ANN static m_bool emit_implicit_cast(const Emitter emit, + const restrict Exp from, + const restrict Type to) { + const struct Implicit imp = {from, to, from->pos}; // no pos - struct Op_Import opi = { .op=insert_symbol("@implicit"), .lhs=from->type, .rhs=to, - .data=(m_uint)&imp }; + struct Op_Import opi = {.op = insert_symbol("@implicit"), + .lhs = from->type, + .rhs = to, + .data = (m_uint)&imp}; return op_emit(emit, &opi); } ANN static Instr _flow(const Emitter emit, const Exp e, const m_bool b) { CHECK_BO(emit_exp_pop_next(emit, e)); emit_exp_addref1(emit, e, -exp_size(e)); - struct Op_Import opi = { .op=insert_symbol(b ? "@conditional" : "@unconditional"), - .rhs=e->type, .pos=e->pos, .data=(uintptr_t)e }; + struct Op_Import opi = { + .op = insert_symbol(b ? "@conditional" : "@unconditional"), + .rhs = e->type, + .pos = e->pos, + .data = (uintptr_t)e}; CHECK_BO(op_emit(emit, &opi)); return (Instr)vector_back(&emit->code->instr); } -#define emit_flow(emit,b) _flow(emit, b, 1) +#define emit_flow(emit, b) _flow(emit, b, 1) -ANN static m_bool emit_exp_if(const Emitter emit, const Exp_If* exp_if) { +ANN static m_bool emit_exp_if(const Emitter emit, const Exp_If *exp_if) { const Exp e = exp_if->if_exp ?: exp_if->cond; - if(exp_getvar(exp_self(exp_if))) { + if (exp_getvar(exp_self(exp_if))) { exp_setvar(e, 1); exp_setvar(exp_if->else_exp, 1); } DECL_OB(const Instr, op, = emit_flow(emit, exp_if->cond)); CHECK_BB(emit_exp_pop_next(emit, exp_if->if_exp ?: exp_if->cond)); - const Instr op2 = emit_add_instr(emit, Goto); - op->m_val = emit_code_size(emit); + const Instr op2 = emit_add_instr(emit, Goto); + op->m_val = emit_code_size(emit); const m_bool ret = emit_exp_pop_next(emit, exp_if->else_exp); - op2->m_val = emit_code_size(emit); + op2->m_val = emit_code_size(emit); return ret; } -ANN static inline m_bool emit_prim_novar(const Emitter emit, const Exp_Primary *prim) { - const Exp e = exp_self(prim); +ANN static inline m_bool emit_prim_novar(const Emitter emit, + const Exp_Primary *prim) { + const Exp e = exp_self(prim); const uint var = exp_getvar(e); exp_setvar(e, 0); CHECK_BB(emit_symbol(emit, prim)); @@ -1601,14 +1712,15 @@ ANN static inline m_bool emit_prim_novar(const Emitter emit, const Exp_Primary * ANN static m_bool emit_upvalues(const Emitter emit, const Func func) { const Map map = &func->upvalues; - for(m_uint i = 0; i < map_size(map); ++i) { - const Exp_Primary *prim = (Exp_Primary*)VKEY(map, i); - const Value v = prim->value; - CHECK_BB(emit_prim_novar(emit, prim));; - if(isa(prim->value->type, emit->gwion->type[et_compound]) > 0) { - if(vflag(v, vflag_fglobal) && !vflag(v, vflag_closed)) - emit_exp_addref1(emit, exp_self(prim), -v->type->size); - map_set(&func->code->closure->m, (vtype)v->type, VVAL(map, i)); + for (m_uint i = 0; i < map_size(map); ++i) { + const Exp_Primary *prim = (Exp_Primary *)VKEY(map, i); + const Value v = prim->value; + CHECK_BB(emit_prim_novar(emit, prim)); + ; + if (isa(prim->value->type, emit->gwion->type[et_compound]) > 0) { + if (vflag(v, vflag_fglobal) && !vflag(v, vflag_closed)) + emit_exp_addref1(emit, exp_self(prim), -v->type->size); + map_set(&func->code->closure->m, (vtype)v->type, VVAL(map, i)); } set_vflag(v, vflag_closed); } @@ -1616,85 +1728,91 @@ ANN static m_bool emit_upvalues(const Emitter emit, const Func func) { } ANN static m_bool emit_closure(const Emitter emit, const Func func) { - const Map map = &func->upvalues; - const m_uint sz = VVAL(map, VLEN(map) - 1) + ((Exp_Primary*)VKEY(map, VLEN(map) - 1))->value->type->size; + const Map map = &func->upvalues; + const m_uint sz = + VVAL(map, VLEN(map) - 1) + + ((Exp_Primary *)VKEY(map, VLEN(map) - 1))->value->type->size; func->code->closure = new_closure(emit->gwion->mp, sz); regpushi(emit, (m_uint)func->code->closure->data); CHECK_BB(emit_upvalues(emit, func)); regpop(emit, sz); const Instr cpy = emit_add_instr(emit, Reg2RegOther); - cpy->m_val2 = sz; + cpy->m_val2 = sz; regpop(emit, SZ_INT); return GW_OK; } -ANN static m_bool emit_lambda(const Emitter emit, const Exp_Lambda * lambda) { +ANN static m_bool emit_lambda(const Emitter emit, const Exp_Lambda *lambda) { CHECK_BB(emit_func_def(emit, lambda->def)); - if(lambda->def->base->func->upvalues.ptr) + if (lambda->def->base->func->upvalues.ptr) CHECK_BB(emit_closure(emit, lambda->def->base->func)); - if(vflag(lambda->def->base->func->value_ref, vflag_member) && !exp_getvar(exp_self(lambda))) + if (vflag(lambda->def->base->func->value_ref, vflag_member) && + !exp_getvar(exp_self(lambda))) emit_add_instr(emit, RegPushMem); regpushi(emit, (m_uint)lambda->def->base->func->code); return GW_OK; } -ANN static m_bool emit_exp_lambda(const Emitter emit, const Exp_Lambda * lambda) { - if(!lambda->def->base->func) { +ANN static m_bool emit_exp_lambda(const Emitter emit, + const Exp_Lambda *lambda) { + if (!lambda->def->base->func) { regpushi(emit, SZ_INT); return GW_OK; } - struct EnvSet es = { .env=emit->env, .data=emit, .func=(_exp_func)emit_cdef, - .scope=emit->env->scope->depth, .flag=tflag_emit }; + struct EnvSet es = {.env = emit->env, + .data = emit, + .func = (_exp_func)emit_cdef, + .scope = emit->env->scope->depth, + .flag = tflag_emit}; CHECK_BB(envset_pushv(&es, lambda->def->base->func->value_ref)); const m_bool ret = emit_lambda(emit, lambda); - if(es.run) - envset_pop(&es, lambda->owner); + if (es.run) envset_pop(&es, lambda->owner); return ret; } -ANN static m_bool emit_exp_td(const Emitter emit, Type_Decl* td) { +ANN static m_bool emit_exp_td(const Emitter emit, Type_Decl *td) { regpushi(emit, (m_uint)_class_base(exp_self(td)->type)); return GW_OK; } DECL_EXP_FUNC(emit, m_bool, Emitter) -ANN2(1) /*static */m_bool emit_exp(const Emitter emit, /* const */Exp e) { +ANN2(1) /*static */ m_bool emit_exp(const Emitter emit, /* const */ Exp e) { Exp exp = e; do { - if(emit->info->debug && emit->info->line < e->pos.first.line) { + if (emit->info->debug && emit->info->line < e->pos.first.line) { const Instr instr = emit_add_instr(emit, DebugLine); instr->m_val = emit->info->line = e->pos.first.line; } CHECK_BB(emit_exp_func[exp->exp_type](emit, &exp->d)); - if(exp->cast_to) - CHECK_BB(emit_implicit_cast(emit, exp, exp->cast_to)); - if(isa(e->type, emit->gwion->type[et_object]) > 0 && + if (exp->cast_to) CHECK_BB(emit_implicit_cast(emit, exp, exp->cast_to)); + if (isa(e->type, emit->gwion->type[et_object]) > 0 && (e->cast_to ? isa(e->cast_to, emit->gwion->type[et_object]) > 0 : 1) && - e->exp_type == ae_exp_decl && GET_FLAG(e->d.exp_decl.td, late) && exp_getuse(e) && !exp_getvar(e) && GET_FLAG(e->d.exp_decl.list->self->value, late)) { -// e->exp_type == ae_exp_decl && !exp_getvar(e)) { + e->exp_type == ae_exp_decl && GET_FLAG(e->d.exp_decl.td, late) && + exp_getuse(e) && !exp_getvar(e) && + GET_FLAG(e->d.exp_decl.list->self->value, late)) { + // e->exp_type == ae_exp_decl && !exp_getvar(e)) { const Instr instr = emit_add_instr(emit, GWOP_EXCEPT); - instr->m_val = -SZ_INT; + instr->m_val = -SZ_INT; } - } while((exp = exp->next)); + } while ((exp = exp->next)); return GW_OK; } ANN static m_bool emit_if_const(const Emitter emit, const Stmt_If stmt) { - if(stmt->cond->d.prim.d.num) - return emit_stmt(emit, stmt->if_body, 1); + if (stmt->cond->d.prim.d.num) return emit_stmt(emit, stmt->if_body, 1); return stmt->else_body ? emit_stmt(emit, stmt->else_body, 1) : GW_OK; } ANN static m_bool emit_if(const Emitter emit, const Stmt_If stmt) { - if(stmt->cond->exp_type == ae_exp_primary && stmt->cond->d.prim.prim_type == ae_prim_num) + if (stmt->cond->exp_type == ae_exp_primary && + stmt->cond->d.prim.prim_type == ae_prim_num) return emit_if_const(emit, stmt); DECL_OB(const Instr, op, = emit_flow(emit, stmt->cond)); CHECK_BB(scoped_stmt(emit, stmt->if_body, 1)); const Instr op2 = emit_add_instr(emit, Goto); - op->m_val = emit_code_size(emit); - if(stmt->else_body) - CHECK_BB(scoped_stmt(emit, stmt->else_body, 1)); + op->m_val = emit_code_size(emit); + if (stmt->else_body) CHECK_BB(scoped_stmt(emit, stmt->else_body, 1)); op2->m_val = emit_code_size(emit); return GW_OK; } @@ -1709,14 +1827,15 @@ ANN static m_bool emit_stmt_if(const Emitter emit, const Stmt_If stmt) { ANN static m_bool emit_stmt_code(const Emitter emit, const Stmt_Code stmt) { emit_push_scope(emit); ++emit->env->scope->depth; - const m_bool ret = stmt->stmt_list ? emit_stmt_list(emit, stmt->stmt_list) : 1; + const m_bool ret = + stmt->stmt_list ? emit_stmt_list(emit, stmt->stmt_list) : 1; emit_pop_scope(emit); --emit->env->scope->depth; return ret; } -ANN static m_bool optimize_tail_call(const Emitter emit, const Exp_Call* e) { - if(e->args) { +ANN static m_bool optimize_tail_call(const Emitter emit, const Exp_Call *e) { + if (e->args) { emit_func_args(emit, e); const Func f = e->func->type->info->func; regpop(emit, f->def->stack_depth); @@ -1728,10 +1847,10 @@ ANN static m_bool optimize_tail_call(const Emitter emit, const Exp_Call* e) { ANN static m_bool emit_stmt_return(const Emitter emit, const Stmt_Exp stmt) { CHECK_BB(emit_defers2(emit)); - if(stmt->val) { - if(stmt->val->exp_type == ae_exp_call) { + if (stmt->val) { + if (stmt->val->exp_type == ae_exp_call) { const Func f = stmt->val->d.exp_call.func->type->info->func; - if(stmt->val->exp_type == ae_exp_call && emit->env->func == f) + if (stmt->val->exp_type == ae_exp_call && emit->env->func == f) return optimize_tail_call(emit, &stmt->val->d.exp_call); } CHECK_BB(emit_exp_pop_next(emit, stmt->val)); @@ -1740,39 +1859,42 @@ ANN static m_bool emit_stmt_return(const Emitter emit, const Stmt_Exp stmt) { return GW_OK; } -ANN static inline m_bool emit_jump_index(const Emitter emit, const Vector v, const m_int n) { +ANN static inline m_bool emit_jump_index(const Emitter emit, const Vector v, + const m_int n) { vector_add(v, 0); // make room - const m_uint sz = vector_size(v); - m_int idx = 1; - for(m_uint i = sz; --i > 1;) { - if(!vector_at(v, i) && ++idx == n) { + const m_uint sz = vector_size(v); + m_int idx = 1; + for (m_uint i = sz; --i > 1;) { + if (!vector_at(v, i) && ++idx == n) { m_uint *data = v->ptr + OFFSET + i; - memmove(data + 1, data, (sz-i) * SZ_INT); + memmove(data + 1, data, (sz - i) * SZ_INT); const Instr instr = emit_add_instr(emit, Goto); - VPTR(v, i-1) = (m_uint)instr; + VPTR(v, i - 1) = (m_uint)instr; return GW_OK; } } return GW_ERROR; } -ANN static inline m_bool emit_stmt_continue(const Emitter emit, const Stmt_Index stmt) { +ANN static inline m_bool emit_stmt_continue(const Emitter emit, + const Stmt_Index stmt) { CHECK_BB(emit_defers2(emit)); - if(stmt->idx == -1 || stmt->idx == 1) + if (stmt->idx == -1 || stmt->idx == 1) vector_add(&emit->code->stack_cont, (vtype)emit_add_instr(emit, Goto)); - else if(stmt->idx) { - if(emit_jump_index(emit, &emit->code->stack_cont, stmt->idx) < 0) + else if (stmt->idx) { + if (emit_jump_index(emit, &emit->code->stack_cont, stmt->idx) < 0) ERR_B(stmt_self(stmt)->pos, _("to many jumps required.")) } return GW_OK; } -ANN static inline m_bool emit_stmt_break(const Emitter emit, const Stmt_Index stmt NUSED) { +ANN static inline m_bool emit_stmt_break(const Emitter emit, + const Stmt_Index stmt NUSED) { CHECK_BB(emit_defers2(emit)); - if(stmt->idx == -1 || stmt->idx == 1) + if (stmt->idx == -1 || stmt->idx == 1) vector_add(&emit->code->stack_break, (vtype)emit_add_instr(emit, Goto)); - else if(stmt->idx) { - if(emit_jump_index(emit, &emit->code->stack_break, stmt->idx) < 0) + else if (stmt->idx) { + if (emit_jump_index(emit, &emit->code->stack_break, stmt->idx) < 0) ERR_B(stmt_self(stmt)->pos, _("to many jumps required.")) } return GW_OK; @@ -1784,10 +1906,9 @@ ANN static inline void emit_push_stack(const Emitter emit) { vector_add(&emit->code->stack_break, (vtype)NULL); } -ANN static void pop_vector(Vector v, const m_uint pc) { +ANN static void pop_vector(Vector v, const m_uint pc) { Instr instr; - while((instr = (Instr)vector_pop(v))) - instr->m_val = pc; + while ((instr = (Instr)vector_pop(v))) instr->m_val = pc; } ANN static void emit_pop_stack(const Emitter emit, const m_uint index) { @@ -1796,32 +1917,33 @@ ANN static void emit_pop_stack(const Emitter emit, const m_uint index) { emit_pop_scope(emit); } -ANN static m_bool _emit_stmt_flow(const Emitter emit, const Stmt_Flow stmt, const m_uint index) { - Instr op = NULL; +ANN static m_bool _emit_stmt_flow(const Emitter emit, const Stmt_Flow stmt, + const m_uint index) { + Instr op = NULL; const ae_stmt_t is_while = stmt_self(stmt)->stmt_type == ae_stmt_while; - const uint is_const = stmt->cond->exp_type == ae_exp_primary && stmt->cond->d.prim.prim_type == ae_prim_num; - if(!stmt->is_do) { - if(!is_const) + const uint is_const = stmt->cond->exp_type == ae_exp_primary && + stmt->cond->d.prim.prim_type == ae_prim_num; + if (!stmt->is_do) { + if (!is_const) op = _flow(emit, stmt->cond, is_while); - else if((!is_while && stmt->cond->d.prim.d.num) || - (is_while && !stmt->cond->d.prim.d.num)) + else if ((!is_while && stmt->cond->d.prim.d.num) || + (is_while && !stmt->cond->d.prim.d.num)) return GW_OK; } CHECK_BB(scoped_stmt(emit, stmt->body, 1)); - if(stmt->is_do) { - if(!is_const) { + if (stmt->is_do) { + if (!is_const) { CHECK_OB((op = _flow(emit, stmt->cond, !is_while))); op->m_val = index; - } else if((is_while && stmt->cond->d.prim.d.num) || - (!is_while && !stmt->cond->d.prim.d.num)) { + } else if ((is_while && stmt->cond->d.prim.d.num) || + (!is_while && !stmt->cond->d.prim.d.num)) { const Instr goto_ = emit_add_instr(emit, Goto); - goto_->m_val = index; + goto_->m_val = index; } } else { const Instr goto_ = emit_add_instr(emit, Goto); - goto_->m_val = index; - if(op) - op->m_val = emit_code_size(emit); + goto_->m_val = index; + if (op) op->m_val = emit_code_size(emit); } return GW_OK; } @@ -1834,29 +1956,31 @@ ANN static m_bool emit_stmt_flow(const Emitter emit, const Stmt_Flow stmt) { return ret; } -ANN static m_bool variadic_state(const Emitter emit, const Stmt_VarLoop stmt, const m_uint status) { +ANN static m_bool variadic_state(const Emitter emit, const Stmt_VarLoop stmt, + const m_uint status) { regpushi(emit, status); CHECK_BB(emit_exp(emit, stmt->exp)); const Instr member = emit_add_instr(emit, DotMember4); - member->m_val = SZ_INT; + member->m_val = SZ_INT; emit_add_instr(emit, int_r_assign); regpop(emit, SZ_INT); return GW_OK; } -ANN static m_bool emit_stmt_varloop(const Emitter emit, const Stmt_VarLoop stmt) { +ANN static m_bool emit_stmt_varloop(const Emitter emit, + const Stmt_VarLoop stmt) { CHECK_BB(variadic_state(emit, stmt, 1)); CHECK_BB(emit_exp(emit, stmt->exp)); - const Instr s = emit_add_instr(emit, DotMember); - s->m_val = SZ_INT * 5; + const Instr s = emit_add_instr(emit, DotMember); + s->m_val = SZ_INT * 5; const Instr nonnull = emit_add_instr(emit, Goto); regpop(emit, SZ_INT); - const Instr check = emit_add_instr(emit, Goto); + const Instr check = emit_add_instr(emit, Goto); const Instr member = emit_add_instr(emit, DotMember4); - member->m_val = SZ_INT*2; - nonnull->m_val = emit_code_size(emit); - const Instr instr = emit_add_instr(emit, BranchEqInt); - const m_uint pc = emit_code_size(emit); + member->m_val = SZ_INT * 2; + nonnull->m_val = emit_code_size(emit); + const Instr instr = emit_add_instr(emit, BranchEqInt); + const m_uint pc = emit_code_size(emit); emit_stmt(emit, stmt->body, 1); CHECK_BB(emit_exp(emit, stmt->exp)); emit_vararg_end(emit, pc); @@ -1865,40 +1989,40 @@ ANN static m_bool emit_stmt_varloop(const Emitter emit, const Stmt_VarLoop stmt) return GW_OK; } -ANN static m_bool _emit_stmt_for(const Emitter emit, const Stmt_For stmt, m_uint *action_index) { +ANN static m_bool _emit_stmt_for(const Emitter emit, const Stmt_For stmt, + m_uint *action_index) { CHECK_BB(emit_stmt(emit, stmt->c1, 1)); const m_uint index = emit_code_size(emit); DECL_OB(const Instr, op, = emit_flow(emit, stmt->c2->d.stmt_exp.val)); CHECK_BB(scoped_stmt(emit, stmt->body, 1)); *action_index = emit_code_size(emit); - if(stmt->c3) { + if (stmt->c3) { CHECK_BB(emit_exp(emit, stmt->c3)); pop_exp(emit, stmt->c3); } const Instr _goto = emit_add_instr(emit, Goto); - _goto->m_val = index; - op->m_val = emit_code_size(emit); + _goto->m_val = index; + op->m_val = emit_code_size(emit); return GW_OK; } ANN static m_bool emit_stmt_for(const Emitter emit, const Stmt_For stmt) { emit_push_stack(emit); - m_uint action_index = 0; - const m_bool ret = _emit_stmt_for(emit, stmt, &action_index); + m_uint action_index = 0; + const m_bool ret = _emit_stmt_for(emit, stmt, &action_index); emit_pop_stack(emit, action_index); return ret; } - struct Looper; typedef Instr (*f_looper_init)(const Emitter, const struct Looper *); typedef void (*f_looper)(const Emitter, const struct Looper *); struct Looper { - const Stmt stmt; - /*const */m_uint offset; - const m_uint n; + const Stmt stmt; + /*const */ m_uint offset; + const m_uint n; const f_looper_init roll; - const f_looper unroll; + const f_looper unroll; }; ANN static inline m_bool roll(const Emitter emit, const struct Looper *loop) { @@ -1910,207 +2034,215 @@ ANN static inline m_bool roll(const Emitter emit, const struct Looper *loop) { ANN static Instr stmt_each_roll(const Emitter emit, const struct Looper *loop) { const Instr instr = emit_add_instr(emit, AutoLoop); - instr->m_val2 = loop->offset + SZ_INT; + instr->m_val2 = loop->offset + SZ_INT; return instr; } ANN static inline void unroll_init(const Emitter emit, const m_uint n) { emit->info->unroll = 0; - const Instr instr = emit_add_instr(emit, MemSetImm); - instr->m_val = emit_local(emit, emit->gwion->type[et_int]); - instr->m_val2 = n; + const Instr instr = emit_add_instr(emit, MemSetImm); + instr->m_val = emit_local(emit, emit->gwion->type[et_int]); + instr->m_val2 = n; } -ANN static inline m_bool unroll_run(const Emitter emit, const struct Looper *loop) { - if(loop->unroll) - loop->unroll(emit, loop); +ANN static inline m_bool unroll_run(const Emitter emit, + const struct Looper *loop) { + if (loop->unroll) loop->unroll(emit, loop); return emit_stmt(emit, loop->stmt, 1); } ANN static m_bool unroll(const Emitter emit, const struct Looper *loop) { - const Instr gc = scoped_ini(emit); + const Instr gc = scoped_ini(emit); const Instr unroll = emit_add_instr(emit, Unroll); - unroll->m_val = loop->offset; + unroll->m_val = loop->offset; const m_uint start = emit_code_size(emit); CHECK_BB(unroll_run(emit, loop)); const m_uint end = emit_code_size(emit); - for(m_uint i = 1; i < loop->n; ++i) - CHECK_BB(unroll_run(emit, loop)); - unroll->m_val2 = end - start; + for (m_uint i = 1; i < loop->n; ++i) CHECK_BB(unroll_run(emit, loop)); + unroll->m_val2 = end - start; const Instr unroll2 = emit_add_instr(emit, Unroll2); - unroll2->m_val = (m_uint)unroll; + unroll2->m_val = (m_uint)unroll; scoped_end(emit, gc); return GW_OK; } -ANN static void stmt_each_unroll(const Emitter emit, const struct Looper *loop) { +ANN static void stmt_each_unroll(const Emitter emit, + const struct Looper *loop) { const Instr instr = emit_add_instr(emit, AutoLoop); - instr->m_val = loop->offset + SZ_INT*2; + instr->m_val = loop->offset + SZ_INT * 2; } -ANN static inline m_bool looper_run(const Emitter emit, const struct Looper *loop) { +ANN static inline m_bool looper_run(const Emitter emit, + const struct Looper *loop) { return (!loop->n ? roll : unroll)(emit, loop); } -ANN static m_bool _emit_stmt_each(const Emitter emit, const Stmt_Each stmt, m_uint *end_pc) { +ANN static m_bool _emit_stmt_each(const Emitter emit, const Stmt_Each stmt, + m_uint *end_pc) { const uint n = emit->info->unroll; - if(n) { + if (n) { unroll_init(emit, n); emit_local(emit, emit->gwion->type[et_int]); } CHECK_BB(emit_exp(emit, stmt->exp)); // add ref? regpop(emit, SZ_INT); - const m_uint offset = emit_local(emit, emit->gwion->type[et_int]);//array? + const m_uint offset = emit_local(emit, emit->gwion->type[et_int]); // array? emit_local(emit, emit->gwion->type[et_int]); emit_local(emit, emit->gwion->type[et_int]); - const Instr tomem = emit_add_instr(emit, Reg2Mem); - tomem->m_val = offset; - const Instr s1 = emit_add_instr(emit, MemSetImm); - s1->m_val = offset + SZ_INT; - const Instr loop_idx = emit_add_instr(emit, MemSetImm); - loop_idx->m_val = offset + SZ_INT; - loop_idx->m_val2 = -1; - stmt->v->from->offset = offset + SZ_INT *2; + const Instr tomem = emit_add_instr(emit, Reg2Mem); + tomem->m_val = offset; + const Instr s1 = emit_add_instr(emit, MemSetImm); + s1->m_val = offset + SZ_INT; + const Instr loop_idx = emit_add_instr(emit, MemSetImm); + loop_idx->m_val = offset + SZ_INT; + loop_idx->m_val2 = -1; + stmt->v->from->offset = offset + SZ_INT * 2; emit_debug(emit, stmt->v); - if(stmt->idx) - stmt->idx->v->from->offset = offset + SZ_INT; - if(n) { + if (stmt->idx) stmt->idx->v->from->offset = offset + SZ_INT; + if (n) { const Instr instr = emit_add_instr(emit, AutoUnrollInit); - instr->m_val = offset-SZ_INT; - } - const m_uint ini_pc = emit_code_size(emit); - struct Looper loop = { .stmt=stmt->body, .offset=offset, .n=n, - .roll=stmt_each_roll, .unroll=stmt_each_unroll }; - if(n) - loop.offset -= SZ_INT; + instr->m_val = offset - SZ_INT; + } + const m_uint ini_pc = emit_code_size(emit); + struct Looper loop = {.stmt = stmt->body, + .offset = offset, + .n = n, + .roll = stmt_each_roll, + .unroll = stmt_each_unroll}; + if (n) loop.offset -= SZ_INT; CHECK_BB(looper_run(emit, &loop)); - *end_pc = emit_code_size(emit); + *end_pc = emit_code_size(emit); const Instr tgt = emit_add_instr(emit, Goto); - tgt->m_val = ini_pc; + tgt->m_val = ini_pc; return GW_OK; } ANN static m_bool emit_stmt_each(const Emitter emit, const Stmt_Each stmt) { emit_push_stack(emit); - m_uint end_pc = 0; - const m_bool ret = _emit_stmt_each(emit, stmt, &end_pc); + m_uint end_pc = 0; + const m_bool ret = _emit_stmt_each(emit, stmt, &end_pc); emit_pop_stack(emit, end_pc); return ret; } ANN static Instr stmt_loop_roll(const Emitter emit, const struct Looper *loop) { const Instr eq = emit_add_instr(emit, Repeat); - eq->m_val2 = loop->offset; + eq->m_val2 = loop->offset; return eq; } -ANN static Instr stmt_loop_roll_idx(const Emitter emit, const struct Looper *loop) { +ANN static Instr stmt_loop_roll_idx(const Emitter emit, + const struct Looper *loop) { const Instr instr = emit_add_instr(emit, RepeatIdx); - instr->m_val2 = loop->offset; + instr->m_val2 = loop->offset; return instr; } ANN static m_bool _emit_stmt_loop(const Emitter emit, const Stmt_Loop stmt, - m_uint *index) { + m_uint *index) { const uint n = emit->info->unroll; - if(n) - unroll_init(emit, n); + if (n) unroll_init(emit, n); const m_uint offset = emit_local(emit, emit->gwion->type[et_int]); - if(stmt->idx) { - const Instr instr = emit_add_instr(emit, MemSetImm); - instr->m_val = emit_local(emit, emit->gwion->type[et_int]); + if (stmt->idx) { + const Instr instr = emit_add_instr(emit, MemSetImm); + instr->m_val = emit_local(emit, emit->gwion->type[et_int]); stmt->idx->v->from->offset = offset; - const Instr idx = emit_add_instr(emit, MemSetImm); - idx->m_val = offset; - idx->m_val2 = -1; + const Instr idx = emit_add_instr(emit, MemSetImm); + idx->m_val = offset; + idx->m_val2 = -1; } CHECK_BB(emit_exp_pop_next(emit, stmt->cond)); regpop(emit, SZ_INT); - const Instr tomem = emit_add_instr(emit, Reg2Mem); - tomem->m_val = offset + (!stmt->idx ? 0 : SZ_INT); - *index = emit_code_size(emit); - struct Looper loop = { .stmt=stmt->body, .offset=offset, .n=n, - .roll= !stmt->idx ? stmt_loop_roll : stmt_loop_roll_idx}; + const Instr tomem = emit_add_instr(emit, Reg2Mem); + tomem->m_val = offset + (!stmt->idx ? 0 : SZ_INT); + *index = emit_code_size(emit); + struct Looper loop = {.stmt = stmt->body, + .offset = offset, + .n = n, + .roll = + !stmt->idx ? stmt_loop_roll : stmt_loop_roll_idx}; CHECK_BB(looper_run(emit, &loop)); const Instr _goto = emit_add_instr(emit, Goto); - _goto->m_val = *index; + _goto->m_val = *index; return GW_OK; } ANN static m_bool emit_stmt_loop(const Emitter emit, const Stmt_Loop stmt) { emit_push_stack(emit); - m_uint index = 0; - const m_bool ret = _emit_stmt_loop(emit, stmt, &index); + m_uint index = 0; + const m_bool ret = _emit_stmt_loop(emit, stmt, &index); emit_pop_stack(emit, index); return ret; } ANN static m_bool emit_type_def(const Emitter emit, const Type_Def tdef) { - if(tdef->when) - CHECK_BB(emit_func_def(emit, tdef->when_def)); - return (!is_fptr(emit->gwion, tdef->type) && tdef->type->info->cdef) ? - emit_class_def(emit, tdef->type->info->cdef) : GW_OK; + if (tdef->when) CHECK_BB(emit_func_def(emit, tdef->when_def)); + return (!is_fptr(emit->gwion, tdef->type) && tdef->type->info->cdef) + ? emit_class_def(emit, tdef->type->info->cdef) + : GW_OK; } ANN static m_bool emit_enum_def(const Emitter emit, const Enum_Def edef) { LOOP_OPTIM - for(m_uint i = 0; i < vector_size(&edef->values); ++i) { + for (m_uint i = 0; i < vector_size(&edef->values); ++i) { const Value v = (Value)vector_at(&edef->values, i); - if(!emit->env->class_def) { + if (!emit->env->class_def) { v->from->offset = emit_local(emit, emit->gwion->type[et_int]); - v->d.num = i; + v->d.num = i; } else - *(m_bit*)(emit->env->class_def->nspc->info->class_data + v->from->offset) = i; + *(m_bit *)(emit->env->class_def->nspc->info->class_data + + v->from->offset) = i; } return GW_OK; } -ANN static m_bool emit_union_def(const Emitter emit NUSED, const Union_Def udef) { - if(tmpl_base(udef->tmpl)) - return GW_OK; +ANN static m_bool emit_union_def(const Emitter emit NUSED, + const Union_Def udef) { + if (tmpl_base(udef->tmpl)) return GW_OK; set_tflag(udef->type, tflag_emit); return GW_OK; } - // add a Goto. later the index is set to the ont of the happy path -// maybe this and the function above can use the same machinery as returns or breaks -ANN static inline void emit_try_goto(const restrict Emitter emit, const Vector v) { +// maybe this and the function above can use the same machinery as returns or +// breaks +ANN static inline void emit_try_goto(const restrict Emitter emit, + const Vector v) { const Instr instr = emit_add_instr(emit, Goto); vector_add(v, (m_uint)instr); } // set Goto indexes the one of the happy path ANN static inline void try_goto_indexes(const Vector v, const m_uint pc) { - for(m_uint i = 0; i < vector_size(v); i++) { + for (m_uint i = 0; i < vector_size(v); i++) { const Instr instr = (Instr)vector_at(v, i); - instr->m_val = pc; + instr->m_val = pc; } } -ANN static inline m_bool emit_handler_list(const restrict Emitter emit, const Handler_List handler, - const Vector v) { +ANN static inline m_bool emit_handler_list(const restrict Emitter emit, + const Handler_List handler, + const Vector v) { const Instr instr = emit_add_instr(emit, HandleEffect); - instr->m_val2 = (m_uint)handler->xid; + instr->m_val2 = (m_uint)handler->xid; CHECK_BB(scoped_stmt(emit, handler->stmt, 1)); - if(handler->next) - CHECK_BB(emit_handler_list(emit, handler->next, v)); + if (handler->next) CHECK_BB(emit_handler_list(emit, handler->next, v)); emit_try_goto(emit, v); instr->m_val = emit_code_size(emit); return GW_OK; } -ANN static inline m_bool emit_stmt_try(const restrict Emitter emit, const Stmt_Try stmt) { - const m_uint top = emit->code->frame->try_top; +ANN static inline m_bool emit_stmt_try(const restrict Emitter emit, + const Stmt_Try stmt) { + const m_uint top = emit->code->frame->try_top; emit->code->frame->try_top = emit_code_size(emit); (void)emit_add_instr(emit, TryIni); struct Vector_ v; // store Gotos to the happy path vector_init(&v); CHECK_BB(scoped_stmt(emit, stmt->stmt, 1)); emit_try_goto(emit, &v); - if(!emit->code->frame->handlers.ptr) - map_init(&emit->code->frame->handlers); + if (!emit->code->frame->handlers.ptr) map_init(&emit->code->frame->handlers); CHECK_BB(emit_handler_list(emit, stmt->handler, &v)); try_goto_indexes(&v, emit_code_size(emit)); vector_release(&v); @@ -2119,67 +2251,73 @@ ANN static inline m_bool emit_stmt_try(const restrict Emitter emit, const Stmt_T return GW_OK; } -ANN static m_bool emit_stmt_exp(const Emitter emit, const struct Stmt_Exp_* exp) { +ANN static m_bool emit_stmt_exp(const Emitter emit, + const struct Stmt_Exp_ *exp) { return exp->val ? emit_exp(emit, exp->val) : GW_OK; } ANN static inline m_bool emit_exp1(const Emitter emit, const Exp e) { - const Exp next = e->next; - e->next = NULL; + const Exp next = e->next; + e->next = NULL; const m_bool ret = emit_exp(emit, e); - e->next = next; + e->next = next; return ret; } ANN static m_bool emit_case_head(const Emitter emit, const Exp base, - const Exp e, const Symbol op, const Vector v) { + const Exp e, const Symbol op, const Vector v) { CHECK_BB(emit_exp1(emit, base)); emit_exp_addref1(emit, base, -exp_size(base)); CHECK_BB(emit_exp1(emit, e)); emit_exp_addref1(emit, e, -exp_size(e)); - const Exp_Binary bin = { .lhs=base, .rhs=e, .op=op }; - struct Exp_ ebin = { .d={.exp_binary=bin}, }; - struct Op_Import opi = { .op=op, .lhs=base->type, .rhs=e->type, - .data=(uintptr_t)&ebin.d.exp_binary, .pos=e->pos }; + const Exp_Binary bin = {.lhs = base, .rhs = e, .op = op}; + struct Exp_ ebin = { + .d = {.exp_binary = bin}, + }; + struct Op_Import opi = {.op = op, + .lhs = base->type, + .rhs = e->type, + .data = (uintptr_t)&ebin.d.exp_binary, + .pos = e->pos}; CHECK_BB(op_emit(emit, &opi)); const Instr instr = emit_add_instr(emit, BranchEqInt); vector_add(v, (vtype)instr); return GW_OK; } -ANN static m_bool emit_case_body(const Emitter emit, const struct Stmt_Match_* stmt) { +ANN static m_bool emit_case_body(const Emitter emit, + const struct Stmt_Match_ *stmt) { const Instr when = stmt->when ? emit_flow(emit, stmt->when) : NULL; - if(stmt->when) - CHECK_OB(when); + if (stmt->when) CHECK_OB(when); CHECK_BB(emit_stmt_list(emit, stmt->list)); const Instr instr = emit_add_instr(emit, Goto); vector_add(&emit->env->scope->match->vec, (vtype)instr); - if(when) - when->m_val = emit_code_size(emit); + if (when) when->m_val = emit_code_size(emit); return GW_OK; } ANN static m_bool case_value(const Emitter emit, const Exp base, const Exp e) { - const Value v = e->d.prim.value; + const Value v = e->d.prim.value; v->from->offset = emit_local(emit, base->type); emit_debug(emit, v); const Instr instr = emit_add_instr(emit, Reg2Mem4); - instr->m_val = v->from->offset; - instr->m_val2 = base->type->size; + instr->m_val = v->from->offset; + instr->m_val2 = base->type->size; return GW_OK; } // should be in src/lib/union.c -static const f_instr unionmember[] = { UnionMember, UnionMember2, UnionMember3, UnionMember4 }; +static const f_instr unionmember[] = {UnionMember, UnionMember2, UnionMember3, + UnionMember4}; #define CASE_PASS (Symbol)1 -ANN static Symbol case_op(const Emitter emit, const Exp base, const Exp e, const Vector vec, const uint n) { - if(e->exp_type == ae_exp_primary) { - if(e->d.prim.prim_type == ae_prim_id) { - if(e->d.prim.d.var == insert_symbol("_")) - return CASE_PASS; - if(!nspc_lookup_value1(emit->env->curr, e->d.prim.d.var)) { - if(!n) { +ANN static Symbol case_op(const Emitter emit, const Exp base, const Exp e, + const Vector vec, const uint n) { + if (e->exp_type == ae_exp_primary) { + if (e->d.prim.prim_type == ae_prim_id) { + if (e->d.prim.d.var == insert_symbol("_")) return CASE_PASS; + if (!nspc_lookup_value1(emit->env->curr, e->d.prim.d.var)) { + if (!n) { CHECK_BO(emit_exp(emit, base)); emit_exp_addref(emit, base, -exp_totalsize(base)); regpop(emit, base->type->size); @@ -2188,22 +2326,24 @@ ANN static Symbol case_op(const Emitter emit, const Exp base, const Exp e, const return CASE_PASS; } } - } else if(isa(base->type, emit->gwion->type[et_union]) > 0 && e->exp_type == ae_exp_call) { + } else if (isa(base->type, emit->gwion->type[et_union]) > 0 && + e->exp_type == ae_exp_call) { const Exp func = e->d.exp_call.func; - if(func->d.prim.prim_type == ae_prim_id) { + if (func->d.prim.prim_type == ae_prim_id) { const Map map = &base->type->nspc->info->value->map; - for(m_uint i = 0; i < map_size(map); ++i) { - if(VKEY(map, i) == (m_uint)func->d.prim.d.var) { + for (m_uint i = 0; i < map_size(map); ++i) { + if (VKEY(map, i) == (m_uint)func->d.prim.d.var) { const Value v = (Value)VVAL(map, i); - if(v) { - if(!n) + if (v) { + if (!n) CHECK_BO(emit_exp(emit, base)); else regpush(emit, SZ_INT); const Instr check = emit_add_instr(emit, UnionCheck); - check->m_val2 = i; + check->m_val2 = i; vector_add(vec, (m_uint)check); - const Instr instr = emit_kind(emit, v->type->size, 0 /*emit_addr*/, unionmember); + const Instr instr = + emit_kind(emit, v->type->size, 0 /*emit_addr*/, unionmember); instr->m_val = i; regpop(emit, v->type->size); case_op(emit, e->d.exp_call.args, e->d.exp_call.args, vec, i + 1); @@ -2213,66 +2353,72 @@ ANN static Symbol case_op(const Emitter emit, const Exp base, const Exp e, const } } } - if(!n) - return insert_symbol("=="); + if (!n) return insert_symbol("=="); regpush(emit, SZ_INT); CHECK_BO(emit_exp(emit, e)); - const Exp_Binary bin = { .lhs=base, .rhs=e, .op=insert_symbol("==") }; - struct Exp_ ebin = { .d={.exp_binary=bin}, }; - struct Op_Import opi = { .op=insert_symbol("=="), .lhs=base->type, .rhs=e->type, - .data=(uintptr_t)&ebin.d.exp_binary, .pos=e->pos }; + const Exp_Binary bin = {.lhs = base, .rhs = e, .op = insert_symbol("==")}; + struct Exp_ ebin = { + .d = {.exp_binary = bin}, + }; + struct Op_Import opi = {.op = insert_symbol("=="), + .lhs = base->type, + .rhs = e->type, + .data = (uintptr_t)&ebin.d.exp_binary, + .pos = e->pos}; CHECK_BO(op_emit(emit, &opi)); const Instr instr = emit_add_instr(emit, BranchEqInt); vector_add(vec, (vtype)instr); return CASE_PASS; } -ANN static m_bool _emit_stmt_match_case(const Emitter emit, const struct Stmt_Match_* stmt, - const Vector v) { - Exp e = stmt->cond; +ANN static m_bool _emit_stmt_match_case(const Emitter emit, + const struct Stmt_Match_ *stmt, + const Vector v) { + Exp e = stmt->cond; const Vector cond = &emit->env->scope->match->cond; - for(m_uint i = 0; i < vector_size(cond) && e; e = e->next, ++i) { - const Exp base = (Exp)vector_at(cond, i); - const Symbol op = case_op(emit, base, e, v, 0); - if(op != CASE_PASS) - CHECK_BB(emit_case_head(emit, base, e, op, v)); + for (m_uint i = 0; i < vector_size(cond) && e; e = e->next, ++i) { + const Exp base = (Exp)vector_at(cond, i); + const Symbol op = case_op(emit, base, e, v, 0); + if (op != CASE_PASS) CHECK_BB(emit_case_head(emit, base, e, op, v)); } CHECK_BB(emit_case_body(emit, stmt)); return GW_OK; } -ANN static m_bool emit_stmt_match_case(const Emitter emit, const struct Stmt_Match_* stmt) { +ANN static m_bool emit_stmt_match_case(const Emitter emit, + const struct Stmt_Match_ *stmt) { emit_push_scope(emit); struct Vector_ v; vector_init(&v); const m_bool ret = _emit_stmt_match_case(emit, stmt, &v); emit_pop_scope(emit); - for(m_uint i = 0; i < vector_size(&v); ++i) { + for (m_uint i = 0; i < vector_size(&v); ++i) { const Instr instr = (Instr)vector_at(&v, i); - instr->m_val = emit_code_size(emit); + instr->m_val = emit_code_size(emit); } vector_release(&v); return ret; } -ANN static inline void match_unvec(struct Match_ *const match, const m_uint pc) { +ANN static inline void match_unvec(struct Match_ *const match, + const m_uint pc) { const Vector vec = &match->vec; - for(m_uint i = 0; i < vector_size(vec); ++i) { + for (m_uint i = 0; i < vector_size(vec); ++i) { const Instr instr = (Instr)VPTR(vec, i); - instr->m_val = pc; + instr->m_val = pc; } vector_release(vec); } ANN static m_bool emit_stmt_cases(const Emitter emit, Stmt_List list) { do CHECK_BB(emit_stmt_match_case(emit, &list->stmt->d.stmt_match)); - while((list = list->next)); + while ((list = list->next)); return GW_OK; } -ANN static m_bool emit_match(const Emitter emit, const struct Stmt_Match_* stmt) { - if(stmt->where) - CHECK_BB(emit_stmt(emit, stmt->where, 1)); +ANN static m_bool emit_match(const Emitter emit, + const struct Stmt_Match_ *stmt) { + if (stmt->where) CHECK_BB(emit_stmt(emit, stmt->where, 1)); MATCH_INI(emit->env->scope) vector_init(&m.vec); const m_bool ret = emit_stmt_cases(emit, stmt->list); @@ -2281,42 +2427,47 @@ ANN static m_bool emit_match(const Emitter emit, const struct Stmt_Match_* stmt) return ret; } -ANN static m_bool emit_stmt_match(const Emitter emit, const struct Stmt_Match_* stmt) { +ANN static m_bool emit_stmt_match(const Emitter emit, + const struct Stmt_Match_ *stmt) { emit_push_scope(emit); const m_bool ret = emit_match(emit, stmt); emit_pop_scope(emit); return ret; } -ANN static m_bool emit_stmt_pp(const Emitter emit, const struct Stmt_PP_* stmt) { - if(stmt->pp_type == ae_pp_pragma) { - if(!strncmp(stmt->data, "memoize", strlen("memoize"))) +ANN static m_bool emit_stmt_pp(const Emitter emit, + const struct Stmt_PP_ *stmt) { + if (stmt->pp_type == ae_pp_pragma) { + if (!strncmp(stmt->data, "memoize", strlen("memoize"))) emit->info->memoize = strtol(stmt->data + 7, NULL, 10); - else if(!strncmp(stmt->data, "unroll", strlen("unroll"))) + else if (!strncmp(stmt->data, "unroll", strlen("unroll"))) emit->info->unroll = strtol(stmt->data + 6, NULL, 10); - } else if(stmt->pp_type == ae_pp_include) + } else if (stmt->pp_type == ae_pp_include) emit->env->name = stmt->data; return GW_OK; } -ANN static m_bool emit_stmt_defer(const Emitter emit, const struct Stmt_Defer_* stmt) { +ANN static m_bool emit_stmt_defer(const Emitter emit, + const struct Stmt_Defer_ *stmt) { vector_add(&emit->code->frame->defer, (m_uint)stmt->stmt); return GW_OK; } -ANN static m_bool emit_stmt_retry(const Emitter emit, const struct Stmt_Index_* stmt NUSED) { +ANN static m_bool emit_stmt_retry(const Emitter emit, + const struct Stmt_Index_ *stmt NUSED) { const Instr instr = emit_add_instr(emit, Goto); - instr->m_val = emit->code->frame->try_top + 1; + instr->m_val = emit->code->frame->try_top + 1; return GW_OK; } #define emit_stmt_while emit_stmt_flow #define emit_stmt_until emit_stmt_flow -DECL_STMT_FUNC(emit, m_bool , Emitter) +DECL_STMT_FUNC(emit, m_bool, Emitter) -ANN static m_bool emit_stmt(const Emitter emit, const Stmt stmt, const m_bool pop) { +ANN static m_bool emit_stmt(const Emitter emit, const Stmt stmt, + const m_bool pop) { CHECK_BB(emit_stmt_func[stmt->stmt_type](emit, &stmt->d)); - if(pop && stmt->stmt_type == ae_stmt_exp && stmt->d.stmt_exp.val) { + if (pop && stmt->stmt_type == ae_stmt_exp && stmt->d.stmt_exp.val) { pop_exp(emit, stmt->d.stmt_exp.val); } return GW_OK; @@ -2324,25 +2475,29 @@ ANN static m_bool emit_stmt(const Emitter emit, const Stmt stmt, const m_bool po ANN static m_bool emit_stmt_list(const Emitter emit, Stmt_List l) { do CHECK_BB(emit_stmt(emit, l->stmt, 1)); - while((l = l->next)); + while ((l = l->next)); return GW_OK; } -ANN static m_bool emit_exp_dot(const Emitter emit, const Exp_Dot* member) { - struct Op_Import opi = { .op=insert_symbol("@dot"), .lhs=member->base->type, - .rhs=exp_self(member)->type, .data=(uintptr_t)member, .pos=exp_self(member)->pos }; +ANN static m_bool emit_exp_dot(const Emitter emit, const Exp_Dot *member) { + struct Op_Import opi = {.op = insert_symbol("@dot"), + .lhs = member->base->type, + .rhs = exp_self(member)->type, + .data = (uintptr_t)member, + .pos = exp_self(member)->pos}; return op_emit(emit, &opi); } -ANN static inline void emit_func_def_fglobal(const Emitter emit, const Value value) { +ANN static inline void emit_func_def_fglobal(const Emitter emit, + const Value value) { const Instr set_mem = emit_add_instr(emit, MemSetImm); - set_mem->m_val = value->from->offset; - set_mem->m_val2 = (m_uint)value->d.func_ref->code; + set_mem->m_val = value->from->offset; + set_mem->m_val2 = (m_uint)value->d.func_ref->code; } ANN static void emit_func_def_init(const Emitter emit, const Func func) { const Type t = emit->env->class_def; - char c[(t ? strlen(t->name) + 1 : 0) + strlen(func->name) + 6]; + char c[(t ? strlen(t->name) + 1 : 0) + strlen(func->name) + 6]; sprintf(c, "%s%s%s(...)", t ? t->name : "", t ? "." : "", func->name); emit_push_code(emit, c); } @@ -2353,14 +2508,14 @@ ANN static void emit_func_def_args(const Emitter emit, Arg_List a) { emit->code->stack_depth += type->size; a->var_decl->value->from->offset = emit_localn(emit, type); emit_debug(emit, a->var_decl->value); - } while((a = a->next)); + } while ((a = a->next)); } ANN static void emit_func_def_ensure(const Emitter emit, const Func_Def fdef) { const m_uint size = fdef->base->ret_type->size; - if(size) { + if (size) { const Instr instr = emit_kind(emit, size, 0, regpushimm); - instr->m_val2 = size; + instr->m_val2 = size; } vector_add(&emit->code->stack_return, (vtype)emit_add_instr(emit, Goto)); } @@ -2370,89 +2525,86 @@ ANN static m_bool emit_func_def_return(const Emitter emit) { CHECK_BB(emit_defers(emit)); emit_return_pc(emit, val); vector_clear(&emit->code->stack_return); - if(emit->info->memoize && fflag(emit->env->func, fflag_pure)) { + if (emit->info->memoize && fflag(emit->env->func, fflag_pure)) { const Instr instr = emit_add_instr(emit, MemoizeStore); - instr->m_val = emit->env->func->def->stack_depth; + instr->m_val = emit->env->func->def->stack_depth; } return GW_OK; } ANN static VM_Code emit_internal(const Emitter emit, const Func f) { - if(f->def->base->xid == insert_symbol("@dtor")) { + if (f->def->base->xid == insert_symbol("@dtor")) { emit->env->class_def->nspc->dtor = f->code = finalyze(emit, DTOR_EOC); vmcode_addref(f->code); return f->code; - } else if(f->def->base->xid == insert_symbol("@gack")) { + } else if (f->def->base->xid == insert_symbol("@gack")) { regpop(emit, SZ_INT + f->value_ref->from->owner_class->size); - const Instr instr = emit_add_instr(emit, RegPushMem); - instr->m_val = SZ_INT; - f->code = finalyze(emit, FuncReturn); + const Instr instr = emit_add_instr(emit, RegPushMem); + instr->m_val = SZ_INT; + f->code = finalyze(emit, FuncReturn); return emit->env->class_def->info->gack = f->code; } return finalyze(emit, FuncReturn); } -ANN static inline VM_Code _emit_func_def_code(const Emitter emit, const Func func) { - return !fbflag(func->def->base, fbflag_internal) ? - finalyze(emit, FuncReturn) : emit_internal(emit, func); +ANN static inline VM_Code _emit_func_def_code(const Emitter emit, + const Func func) { + return !fbflag(func->def->base, fbflag_internal) ? finalyze(emit, FuncReturn) + : emit_internal(emit, func); } ANN static VM_Code emit_func_def_code(const Emitter emit, const Func func) { const VM_Code code = _emit_func_def_code(emit, func); - if(emit->info->memoize && fflag(func, fflag_pure)) - code->is_memoize = true; + if (emit->info->memoize && fflag(func, fflag_pure)) code->is_memoize = true; return code; } ANN static m_bool emit_func_def_body(const Emitter emit, const Func_Def fdef) { - if(fdef->base->args) - emit_func_def_args(emit, fdef->base->args); - if(fbflag(fdef->base, fbflag_variadic)) - stack_alloc(emit); - if(fdef->d.code) - CHECK_BB(scoped_stmt(emit, fdef->d.code, 1)); + if (fdef->base->args) emit_func_def_args(emit, fdef->base->args); + if (fbflag(fdef->base, fbflag_variadic)) stack_alloc(emit); + if (fdef->d.code) CHECK_BB(scoped_stmt(emit, fdef->d.code, 1)); emit_func_def_ensure(emit, fdef); return GW_OK; } ANN static Instr me_top(MemoizeEmitter *me) { const Instr idx = emit_add_instr(me->emit, MemSetImm); - idx->m_val = me->offset; - const Instr pc = emit_add_instr(me->emit, MemSetImm); - pc->m_val = me->offset + SZ_INT; + idx->m_val = me->offset; + const Instr pc = emit_add_instr(me->emit, MemSetImm); + pc->m_val = me->offset + SZ_INT; return pc; } ANN static void me_ini(MemoizeEmitter *me) { const Instr ini = emit_add_instr(me->emit, MemoizeIni); - ini->m_val = me->offset; - ini->m_val2 = me->fdef->stack_depth + me->fdef->base->ret_type->size; + ini->m_val = me->offset; + ini->m_val2 = me->fdef->stack_depth + me->fdef->base->ret_type->size; } ANN static void me_end(MemoizeEmitter *me, const m_uint pc) { - for(m_uint i = 0; i < vector_size(&me->branch); ++i) - ((Instr)vector_at(&me->branch, i))->m_val = pc; + for (m_uint i = 0; i < vector_size(&me->branch); ++i) + ((Instr)vector_at(&me->branch, i))->m_val = pc; } ANN static void me_bottom(MemoizeEmitter *me, const m_uint pc) { const Instr idx = emit_add_instr(me->emit, RegPushMem4); - idx->m_val = me->offset; + idx->m_val = me->offset; emit_add_instr(me->emit, int_pre_inc); regpop(me->emit, SZ_INT); const Instr loop = emit_add_instr(me->emit, Goto); - loop->m_val = pc; + loop->m_val = pc; } ANN static void me_ret(MemoizeEmitter *me) { - const Instr instr = emit_kind(me->emit, me->fdef->base->ret_type->size, 0, regpushmem); - instr->m_val = (me->offset + SZ_INT)*2; + const Instr instr = + emit_kind(me->emit, me->fdef->base->ret_type->size, 0, regpushmem); + instr->m_val = (me->offset + SZ_INT) * 2; emit_add_instr(me->emit, FuncReturn); } ANN static m_bool me_run(MemoizeEmitter *me, const m_uint pc) { me_ini(me); - if(me->fdef->base->args) - CHECK_BB(me_arg(me)); + if (me->fdef->base->args) CHECK_BB(me_arg(me)); me_ret(me); me_end(me, emit_code_size(me->emit)); me_bottom(me, pc); @@ -2460,9 +2612,9 @@ ANN static m_bool me_run(MemoizeEmitter *me, const m_uint pc) { } ANN static m_bool emit_memoize(const Emitter emit, const Func_Def fdef) { - MemoizeEmitter me = { .emit=emit, .fdef=fdef, .offset=fdef->stack_depth }; - const Instr pc = me_top(&me); - const m_uint top = emit_code_size(emit); + MemoizeEmitter me = {.emit = emit, .fdef = fdef, .offset = fdef->stack_depth}; + const Instr pc = me_top(&me); + const m_uint top = emit_code_size(emit); vector_init(&me.branch); const m_bool ret = me_run(&me, top); vector_release(&me.branch); @@ -2471,7 +2623,7 @@ ANN static m_bool emit_memoize(const Emitter emit, const Func_Def fdef) { } ANN static m_bool emit_fdef(const Emitter emit, const Func_Def fdef) { - if(emit->info->memoize && fflag(fdef->base->func, fflag_pure)) + if (emit->info->memoize && fflag(fdef->base->func, fflag_pure)) CHECK_BB(emit_memoize(emit, fdef)); CHECK_BB(emit_func_def_body(emit, fdef)); emit_func_def_return(emit); @@ -2479,66 +2631,66 @@ ANN static m_bool emit_fdef(const Emitter emit, const Func_Def fdef) { } static ANN int fdef_is_file_global(const Emitter emit, const Func_Def fdef) { - return !fbflag(fdef->base, fbflag_lambda) && - !emit->env->class_def && !GET_FLAG(fdef->base, global) && !fdef->base->tmpl && - !emit->env->scope->depth; + return !fbflag(fdef->base, fbflag_lambda) && !emit->env->class_def && + !GET_FLAG(fdef->base, global) && !fdef->base->tmpl && + !emit->env->scope->depth; } ANN static void emit_fdef_finish(const Emitter emit, const Func_Def fdef) { const Func func = fdef->base->func; - func->code = emit_func_def_code(emit, func); - if(fdef_is_file_global(emit, fdef)) + func->code = emit_func_def_code(emit, func); + if (fdef_is_file_global(emit, fdef)) emit_func_def_fglobal(emit, func->value_ref); - if(emit->info->memoize && fflag(func, fflag_pure)) + if (emit->info->memoize && fflag(func, fflag_pure)) func->code->memoize = memoize_ini(emit, func); } ANN m_bool emit_func_def(const Emitter emit, const Func_Def f) { - const Func func = f->base->func; - const Func_Def fdef = func->def; - const Func former = emit->env->func; - if(func->code || tmpl_base(fdef->base->tmpl)) - return GW_OK; - if(vflag(func->value_ref, vflag_builtin) && safe_tflag(emit->env->class_def, tflag_tmpl)) { - const Func base = nspc_lookup_func1(func->value_ref->from->owner, f->base->xid); + const Func func = f->base->func; + const Func_Def fdef = func->def; + const Func former = emit->env->func; + if (func->code || tmpl_base(fdef->base->tmpl)) return GW_OK; + if (vflag(func->value_ref, vflag_builtin) && + safe_tflag(emit->env->class_def, tflag_tmpl)) { + const Func base = + nspc_lookup_func1(func->value_ref->from->owner, f->base->xid); builtin_func(emit->gwion->mp, func, (f_xfun)base->code->native_func); func->code->ret_type = fdef->base->ret_type; return GW_OK; } - if(fdef_is_file_global(emit, fdef)) + if (fdef_is_file_global(emit, fdef)) func->value_ref->from->offset = emit_local(emit, emit->gwion->type[et_int]); - const uint global = GET_FLAG(f->base, global); - const m_uint scope = !global ? emit->env->scope->depth : env_push_global(emit->env); + const uint global = GET_FLAG(f->base, global); + const m_uint scope = + !global ? emit->env->scope->depth : env_push_global(emit->env); emit_func_def_init(emit, func); - if(vflag(func->value_ref, vflag_member)) - stack_alloc(emit); + if (vflag(func->value_ref, vflag_member)) stack_alloc(emit); emit->env->func = func; emit_push_scope(emit); - if(!strcmp(s_name(fdef->base->xid), "@gack")) { + if (!strcmp(s_name(fdef->base->xid), "@gack")) { emit_local(emit, emit->gwion->type[et_int]); const Instr instr = emit_add_instr(emit, MemSetImm); - instr->m_val = SZ_INT; + instr->m_val = SZ_INT; } const m_bool ret = scanx_fdef(emit->env, emit, fdef, (_exp_func)emit_fdef); emit_pop_scope(emit); emit->env->func = former; - if(ret > 0) + if (ret > 0) emit_fdef_finish(emit, fdef); else emit_pop_code(emit); - if(global) - env_pop(emit->env, scope); + if (global) env_pop(emit->env, scope); return ret; } ANN static m_bool emit_extend_def(const Emitter emit, const Extend_Def xdef); -#define emit_fptr_def dummy_func +#define emit_fptr_def dummy_func #define emit_trait_def dummy_func HANDLE_SECTION_FUNC(emit, m_bool, Emitter) ANN static inline m_bool emit_ast_inner(const Emitter emit, Ast ast) { do CHECK_BB(emit_section(emit, ast->section)); - while((ast = ast->next)); + while ((ast = ast->next)); return emit_defers(emit); } @@ -2550,9 +2702,9 @@ ANN static m_bool emit_extend_def(const Emitter emit, const Extend_Def xdef) { return ret; } -ANN Code* emit_class_code(const Emitter emit, const m_str name) { +ANN Code *emit_class_code(const Emitter emit, const m_str name) { const m_uint len = strlen(name) + 7; - char c[len]; + char c[len]; snprintf(c, len, "class %s", name); emit_push_code(emit, c); stack_alloc(emit); @@ -2564,37 +2716,33 @@ ANN static m_bool emit_parent(const Emitter emit, const Class_Def cdef) { return ensure_emit(emit, parent); } -ANN /*static */inline m_bool emit_cdef(const Emitter emit, const Type t) { - return scanx_cdef(emit->env, emit, t, - (_exp_func)emit_class_def, (_exp_func)emit_union_def); +ANN /*static */ inline m_bool emit_cdef(const Emitter emit, const Type t) { + return scanx_cdef(emit->env, emit, t, (_exp_func)emit_class_def, + (_exp_func)emit_union_def); } ANN static m_bool cdef_parent(const Emitter emit, const Class_Def cdef) { const bool tmpl = !!cdef->base.tmpl; - if(tmpl) - CHECK_BB(template_push_types(emit->env, cdef->base.tmpl)); + if (tmpl) CHECK_BB(template_push_types(emit->env, cdef->base.tmpl)); const m_bool ret = emit_parent(emit, cdef); - if(tmpl) - nspc_pop_type(emit->gwion->mp, emit->env->curr); + if (tmpl) nspc_pop_type(emit->gwion->mp, emit->env->curr); return ret; } ANN static m_bool emit_class_def(const Emitter emit, const Class_Def cdef) { - if(tmpl_base(cdef->base.tmpl)) - return GW_OK; + if (tmpl_base(cdef->base.tmpl)) return GW_OK; const Type t = cdef->base.type; - if(tflag(t, tflag_emit)) - return GW_OK; + if (tflag(t, tflag_emit)) return GW_OK; set_tflag(t, tflag_emit); const Type owner = t->info->value->from->owner_class; - if(owner) - CHECK_BB(ensure_emit(emit, owner)); - if(cdef->base.ext && t->info->parent->info->cdef && !tflag(t->info->parent, tflag_emit)) // ????? + if (owner) CHECK_BB(ensure_emit(emit, owner)); + if (cdef->base.ext && t->info->parent->info->cdef && + !tflag(t->info->parent, tflag_emit)) // ????? CHECK_BB(cdef_parent(emit, cdef)); nspc_allocdata(emit->gwion->mp, t->nspc); - if(cdef->body) { + if (cdef->body) { emit_class_code(emit, t->name); - if(scanx_body(emit->env, cdef, (_exp_func)emit_section, emit) > 0) + if (scanx_body(emit->env, cdef, (_exp_func)emit_section, emit) > 0) t->nspc->pre_ctor = finalyze(emit, FuncReturn); else { free_code(emit->gwion->mp, emit->code); @@ -2605,16 +2753,15 @@ ANN static m_bool emit_class_def(const Emitter emit, const Class_Def cdef) { return GW_OK; } -ANN static inline void emit_free_code(const Emitter emit, Code* code) { - if(vector_size(&code->instr)) - free_code_instr(&code->instr, emit->gwion); +ANN static inline void emit_free_code(const Emitter emit, Code *code) { + if (vector_size(&code->instr)) free_code_instr(&code->instr, emit->gwion); free_code(emit->gwion->mp, code); } ANN static VM_Code emit_free_stack(const Emitter emit) { LOOP_OPTIM - for(m_uint i = vector_size(&emit->stack) + 1; --i;) - emit_free_code(emit, (Code*)vector_at(&emit->stack, i - 1)); + for (m_uint i = vector_size(&emit->stack) + 1; --i;) + emit_free_code(emit, (Code *)vector_at(&emit->stack, i - 1)); vector_clear(&emit->stack); vector_clear(&emit->info->pure); emit_free_code(emit, emit->code); @@ -2622,17 +2769,17 @@ ANN static VM_Code emit_free_stack(const Emitter emit) { } ANN m_bool emit_ast(const Env env, Ast ast) { - const Emitter emit = env->gwion->emit; + const Emitter emit = env->gwion->emit; emit->info->memoize = 0; - emit->info->unroll = 0; - emit->info->line = 0; - emit->this_offset = 0; + emit->info->unroll = 0; + emit->info->line = 0; + emit->this_offset = 0; emit->vararg_offset = 0; - emit->code = new_code(emit, emit->env->name); + emit->code = new_code(emit, emit->env->name); emit_push_scope(emit); const m_bool ret = emit_ast_inner(emit, ast); emit_pop_scope(emit); - if(ret > 0) + if (ret > 0) emit->info->code = finalyze(emit, EOC); else emit_free_stack(emit); diff --git a/src/emit/emitter.c b/src/emit/emitter.c index 46641ea8..95350b2d 100644 --- a/src/emit/emitter.c +++ b/src/emit/emitter.c @@ -8,17 +8,18 @@ #include "escape.h" static ANEW ANN VM_Code emit_code(const Emitter emit) { - Code* const c = emit->code; - const VM_Code code = new_vmcode(emit->gwion->mp, &c->instr, c->stack_depth, 0, c->name); + Code *const c = emit->code; + const VM_Code code = + new_vmcode(emit->gwion->mp, &c->instr, c->stack_depth, 0, c->name); return code; } ANEW Emitter new_emitter(MemPool p) { Emitter emit = (Emitter)mp_calloc(p, Emitter); vector_init(&emit->stack); - emit->info = (struct EmitterInfo_*)mp_calloc(p, EmitterInfo); + emit->info = (struct EmitterInfo_ *)mp_calloc(p, EmitterInfo); vector_init(&emit->info->pure); - emit->info->escape = escape_table(p); + emit->info->escape = escape_table(p); emit->info->emit_code = emit_code; return emit; } @@ -31,13 +32,13 @@ ANN void free_emitter(MemPool p, Emitter a) { mp_free(p, Emitter, a); } -__attribute__((returns_nonnull)) -ANN2(1) Instr emit_add_instr(const Emitter emit, const f_instr f) { +__attribute__((returns_nonnull)) ANN2(1) Instr + emit_add_instr(const Emitter emit, const f_instr f) { const Instr instr = mp_calloc(emit->gwion->mp, Instr); - if((m_uint)f < 255) + if ((m_uint)f < 255) instr->opcode = (m_uint)f; else { - instr->opcode = eOP_MAX; + instr->opcode = eOP_MAX; instr->execute = f; } vector_add(&emit->code->instr, (vtype)instr); diff --git a/src/emit/escape.c b/src/emit/escape.c index e8c943cc..d58b15d9 100644 --- a/src/emit/escape.c +++ b/src/emit/escape.c @@ -6,66 +6,66 @@ #include "emit.h" #include "escape.h" -char* escape_table(MemPool p) { - char *escape = (char*)mp_calloc2(p, 256); - escape['0'] = '0'; +char *escape_table(MemPool p) { + char *escape = (char *)mp_calloc2(p, 256); + escape['0'] = '0'; escape['\''] = '\''; - escape['"'] = '"'; + escape['"'] = '"'; escape['\\'] = '\\'; - escape['a'] = (char)7; // audible bell - escape['b'] = (char)8; // back space - escape['f'] = (char)12; // form feed - escape['n'] = (char)10; // new line - escape['r'] = (char)13; // carriage return - escape['t'] = (char)9; // horizontal tab - escape['v'] = (char)11; // vertical tab + escape['a'] = (char)7; // audible bell + escape['b'] = (char)8; // back space + escape['f'] = (char)12; // form feed + escape['n'] = (char)10; // new line + escape['r'] = (char)13; // carriage return + escape['t'] = (char)9; // horizontal tab + escape['v'] = (char)11; // vertical tab return escape; } static int get_escape(const Emitter emit, const char c, const loc_t pos) { - if(emit->info->escape[(int)c]) - return emit->info->escape[(int)c]; + if (emit->info->escape[(int)c]) return emit->info->escape[(int)c]; env_err(emit->env, pos, _("unrecognized escape sequence '\\%c'"), c); return GW_ERROR; } m_bool escape_str(const Emitter emit, const m_str base, const loc_t pos) { - unsigned char* str_lit = (unsigned char*)base; - m_str str = base; - while(*str_lit) { - if(*str_lit == '\\') { + unsigned char *str_lit = (unsigned char *)base; + m_str str = base; + while (*str_lit) { + if (*str_lit == '\\') { ++str_lit; - const unsigned char c = *(str_lit); - const unsigned char c2 = *(str_lit+1); - if(c >= '0' && c <= '7') { - if(c == '0' && (c2 < '0' || c2 > '7')) + const unsigned char c = *(str_lit); + const unsigned char c2 = *(str_lit + 1); + if (c >= '0' && c <= '7') { + if (c == '0' && (c2 < '0' || c2 > '7')) *str++ = '\0'; else { - const unsigned char c3 = *(str_lit+2); - if(c2 >= '0' && c2 <= '7' && c3 >= '0' && c3 <= '7') { - *str++ = (char)((c-'0') * 64 + (c2-'0') * 8 + (c3-'0')); + const unsigned char c3 = *(str_lit + 2); + if (c2 >= '0' && c2 <= '7' && c3 >= '0' && c3 <= '7') { + *str++ = (char)((c - '0') * 64 + (c2 - '0') * 8 + (c3 - '0')); str_lit += 2; } else { - env_err(emit->env, pos, _("malformed octal escape sequence '\\%c%c%c'"), c, c2, c3); + env_err(emit->env, pos, + _("malformed octal escape sequence '\\%c%c%c'"), c, c2, c3); return GW_ERROR; } } - } else if(c == 'x' || c == 'x' || c == 'u' || c == 'U') { + } else if (c == 'x' || c == 'x' || c == 'u' || c == 'U') { ++str_lit; const unsigned char c1 = *(str_lit); - const unsigned char c3 = *(str_lit+1); - if(c1 >= '0' && c1 <= 'F' && c3 >= '0' && c3 <= 'F') { - *str++ = (char)((c1-'0') * 16 + (c3-'0')); + const unsigned char c3 = *(str_lit + 1); + if (c1 >= '0' && c1 <= 'F' && c3 >= '0' && c3 <= 'F') { + *str++ = (char)((c1 - '0') * 16 + (c3 - '0')); ++str_lit; } else { - env_err(emit->env, pos, _("malformed hex escape sequence '\\%c%c'"), c1, c3); + env_err(emit->env, pos, _("malformed hex escape sequence '\\%c%c'"), + c1, c3); return GW_ERROR; } } else CHECK_BB((*str++ = (char)get_escape(emit, (char)c, pos))); - } - else - *str++ = (char)*str_lit; + } else + *str++ = (char)*str_lit; ++str_lit; } *str = '\0'; diff --git a/src/emit/memoize.c b/src/emit/memoize.c index cc5b598b..18bbbe4b 100644 --- a/src/emit/memoize.c +++ b/src/emit/memoize.c @@ -9,10 +9,10 @@ struct Memoize_ { struct Vector_ v; - m_uint arg_sz; - m_uint ret_sz; - size_t limit; - size_t curr; + m_uint arg_sz; + m_uint ret_sz; + size_t limit; + size_t curr; }; Memoize memoize_ini(const Emitter emit, const Func f) { @@ -20,38 +20,38 @@ Memoize memoize_ini(const Emitter emit, const Func f) { vector_init(&m->v); m->ret_sz = f->def->base->ret_type->size; m->arg_sz = f->def->stack_depth; - m->limit = emit->info->memoize; + m->limit = emit->info->memoize; return m; } void memoize_end(MemPool p, Memoize m) { - for(m_uint i = 0; i < vector_size(&m->v); ++i) - mp_free2(p, m->arg_sz + m->arg_sz, (void*)vector_at(&m->v, i)); + for (m_uint i = 0; i < vector_size(&m->v); ++i) + mp_free2(p, m->arg_sz + m->arg_sz, (void *)vector_at(&m->v, i)); vector_release(&m->v); mp_free(p, Memoize, m); } -ANN static inline m_bit* get_data(MemPool mp, Memoize m) { - if(vector_size(&m->v) >= m->limit) - return (m_bit*)vector_at(&m->v, m->curr++ % m->limit); - m_bit* data = mp_calloc2(mp, m->arg_sz + m->ret_sz); +ANN static inline m_bit *get_data(MemPool mp, Memoize m) { + if (vector_size(&m->v) >= m->limit) + return (m_bit *)vector_at(&m->v, m->curr++ % m->limit); + m_bit *data = mp_calloc2(mp, m->arg_sz + m->ret_sz); vector_add(&m->v, (vtype)data); return data; } INSTR(MemoizeStore) { - const Memoize m = shred->code->memoize; - m_bit* data = get_data(shred->info->mp, m); + const Memoize m = shred->code->memoize; + m_bit * data = get_data(shred->info->mp, m); memcpy(data, shred->mem, m->arg_sz); memcpy(data + m->arg_sz, shred->reg - m->ret_sz, m->ret_sz); } INSTR(MemoizeIni) { - const m_uint idx = *(m_uint*)MEM(instr->m_val); - const Memoize m = shred->code->memoize; - if(idx < VLEN(&m->v)) { - const m_bit* data = (m_bit*)vector_at(&m->v, idx); - memcpy(MEM(instr->m_val + SZ_INT *2), data, instr->m_val2); + const m_uint idx = *(m_uint *)MEM(instr->m_val); + const Memoize m = shred->code->memoize; + if (idx < VLEN(&m->v)) { + const m_bit *data = (m_bit *)vector_at(&m->v, idx); + memcpy(MEM(instr->m_val + SZ_INT * 2), data, instr->m_val2); } else - shred->pc = *(m_uint*)MEM(instr->m_val + SZ_INT); + shred->pc = *(m_uint *)MEM(instr->m_val + SZ_INT); } diff --git a/src/env/context.c b/src/env/context.c index 3670f442..9cf288f5 100644 --- a/src/env/context.c +++ b/src/env/context.c @@ -12,19 +12,19 @@ ANN void free_context(const Context a, Gwion gwion) { ANN2(2) Context new_context(MemPool p, const Ast ast, const m_str str) { const Context context = mp_calloc(p, Context); - context->name = mstrdup(p, str); - context->nspc = new_nspc(p, context->name); - context->tree = ast; - context->ref = 1; + context->name = mstrdup(p, str); + context->nspc = new_nspc(p, context->name); + context->tree = ast; + context->ref = 1; return context; } ANN void load_context(const Context context, const Env env) { context_addref((env->context = context)); vector_add(&env->scope->nspc_stack, (vtype)env->curr); - env->name = context->name; + env->name = context->name; context->nspc->parent = env->curr; - env->curr = context->nspc; + env->curr = context->nspc; } ANN void unload_context(const Context context, const Env env) { diff --git a/src/env/env.c b/src/env/env.c index 1331ea86..5f5a9e57 100644 --- a/src/env/env.c +++ b/src/env/env.c @@ -21,10 +21,10 @@ ANN static struct Env_Scope_ *new_envscope(MemPool p) { } Env new_env(MemPool p) { - const Env env = (Env)xmalloc(sizeof(struct Env_)); + const Env env = (Env)xmalloc(sizeof(struct Env_)); env->global_nspc = new_nspc(p, "global_nspc"); - env->context = NULL; - env->scope = new_envscope(p); + env->context = NULL; + env->scope = new_envscope(p); env_reset(env); return env; } @@ -37,24 +37,23 @@ ANN void env_reset(const Env env) { vector_clear(&env->scope->class_stack); vector_add(&env->scope->class_stack, (vtype)NULL); vector_add(&env->scope->effects, 0); - env->curr = env->global_nspc; - env->class_def = NULL; - env->func = NULL; - env->scope->depth = 0; + env->curr = env->global_nspc; + env->class_def = NULL; + env->func = NULL; + env->scope->depth = 0; env->scope->in_try = false; } ANN void release_ctx(struct Env_Scope_ *a, struct Gwion_ *gwion) { const m_uint size = vector_size(&a->known_ctx); - for(m_uint i = size + 1; --i;) { + for (m_uint i = size + 1; --i;) { const Context ctx = (Context)vector_at(&a->known_ctx, i - 1); - if(!ctx->error && ctx->global) - ast_cleaner(gwion, ctx->tree); + if (!ctx->error && ctx->global) ast_cleaner(gwion, ctx->tree); context_remref(ctx, gwion); } } -ANN static void free_env_scope(struct Env_Scope_ *a, Gwion gwion) { +ANN static void free_env_scope(struct Env_Scope_ *a, Gwion gwion) { release_ctx(a, gwion); vector_release(&a->known_ctx); vector_release(&a->nspc_stack); @@ -62,47 +61,45 @@ ANN static void free_env_scope(struct Env_Scope_ *a, Gwion gwion) { vector_release(&a->breaks); vector_release(&a->conts); const Vector v = &a->effects; - for(m_uint i = 0; i < vector_size(v); i++) { + for (m_uint i = 0; i < vector_size(v); i++) { m_uint _v = vector_at(v, i); - if(_v) - m_vector_release((M_Vector)&_v); + if (_v) m_vector_release((M_Vector)&_v); } vector_release(&a->effects); mp_free(gwion->mp, Env_Scope, a); } ANN void env_add_effect(const Env a, const Symbol effect, const loc_t pos) { - const Vector v = &a->scope->effects; + const Vector v = &a->scope->effects; const M_Vector w = (M_Vector)&VPTR(v, VLEN(v) - 1); - if(!w->ptr) - m_vector_init(w, sizeof(struct ScopeEffect), 0); - struct ScopeEffect eff = { effect, pos }; + if (!w->ptr) m_vector_init(w, sizeof(struct ScopeEffect), 0); + struct ScopeEffect eff = {effect, pos}; m_vector_add(w, &eff); } ANN void free_env(const Env a) { free_env_scope(a->scope, a->gwion); - while(pop_global(a->gwion)); + while (pop_global(a->gwion)) + ; xfree(a); } -ANN2(1,3) m_uint env_push(const Env env, const Type type, const Nspc nspc) { +ANN2(1, 3) m_uint env_push(const Env env, const Type type, const Nspc nspc) { const m_uint scope = env->scope->depth; vector_add(&env->scope->class_stack, (vtype)env->class_def); env->class_def = type; vector_add(&env->scope->nspc_stack, (vtype)env->curr); vector_add(&env->scope->effects, 0); - env->curr = nspc; + env->curr = nspc; env->scope->depth = 0; return scope; } ANN void env_pop(const Env env, const m_uint scope) { - env->class_def = (Type)vector_pop(&env->scope->class_stack); - env->curr = (Nspc)vector_pop(&env->scope->nspc_stack); + env->class_def = (Type)vector_pop(&env->scope->class_stack); + env->curr = (Nspc)vector_pop(&env->scope->nspc_stack); const m_uint _v = vector_pop(&env->scope->effects); - if(_v) - m_vector_release((M_Vector)&_v); + if (_v) m_vector_release((M_Vector)&_v); env->scope->depth = scope; } @@ -112,5 +109,6 @@ ANN void env_add_type(const Env env, const Type type, const loc_t loc) { const Value v = mk_class(env, type, loc); SET_FLAG(v, global); set_vflag(v, vflag_builtin); - set_tflag(type, tflag_scan0 | tflag_scan1 | tflag_scan2 | tflag_check | tflag_emit); + set_tflag(type, + tflag_scan0 | tflag_scan1 | tflag_scan2 | tflag_check | tflag_emit); } diff --git a/src/env/env_utils.c b/src/env/env_utils.c index fe3c882c..f415c881 100644 --- a/src/env/env_utils.c +++ b/src/env/env_utils.c @@ -5,35 +5,35 @@ #include "vm.h" #include "parse.h" -#define GET(a,b) ((a) & (b)) == (b) +#define GET(a, b) ((a) & (b)) == (b) ANN m_bool env_access(const Env env, const ae_flag flag, const loc_t pos) { - if(env->scope->depth) { - if(GET(flag, ae_flag_global)) + if (env->scope->depth) { + if (GET(flag, ae_flag_global)) ERR_B(pos, _("'global' can only be used at %s scope."), - GET(flag, ae_flag_global) && !env->class_def ? - "file" : "class") + GET(flag, ae_flag_global) && !env->class_def ? "file" : "class") } - if((GET(flag, ae_flag_static) || GET(flag, ae_flag_private) || - GET(flag, ae_flag_protect)) && (!env->class_def || env->scope->depth)) - ERR_B(pos, _("static/private/protect can only be used at class scope.")) + if ((GET(flag, ae_flag_static) || GET(flag, ae_flag_private) || + GET(flag, ae_flag_protect)) && + (!env->class_def || env->scope->depth)) + ERR_B(pos, _("static/private/protect can only be used at class scope.")) return GW_OK; } ANN m_bool env_storage(const Env env, ae_flag flag, const loc_t pos) { CHECK_BB(env_access(env, flag, pos)); - return !(env->class_def && GET(flag, ae_flag_global)) ? GW_OK :GW_ERROR; + return !(env->class_def && GET(flag, ae_flag_global)) ? GW_OK : GW_ERROR; } #undef GET -#define RETURN_TYPE(a) \ -do { \ - const Type t = (a); \ - if(t) return t; \ -} while(0) +#define RETURN_TYPE(a) \ + do { \ + const Type t = (a); \ + if (t) return t; \ + } while (0) ANN static Type find_in_parent(const Type type, const Symbol xid) { Type base = type; - while(base && base->nspc) { + while (base && base->nspc) { RETURN_TYPE(nspc_lookup_type1(base->nspc, xid)); base = base->info->parent; } @@ -41,12 +41,11 @@ ANN static Type find_in_parent(const Type type, const Symbol xid) { } ANN Type find_initial(const Env env, const Symbol xid) { - if(env->class_def) - RETURN_TYPE(find_in_parent(env->class_def, xid)); + if (env->class_def) RETURN_TYPE(find_in_parent(env->class_def, xid)); RETURN_TYPE(nspc_lookup_type1(env->curr, xid)); const Vector v = &env->scope->nspc_stack; - for(m_uint i = vector_size(v) + 1; --i;) { - const Nspc nspc = (Nspc)vector_at(v, i-1); + for (m_uint i = vector_size(v) + 1; --i;) { + const Nspc nspc = (Nspc)vector_at(v, i - 1); RETURN_TYPE(nspc_lookup_type1(nspc, xid)); } return NULL; @@ -55,38 +54,37 @@ ANN Type find_initial(const Env env, const Symbol xid) { ANN Type find_type(const Env env, Type_Decl *path) { DECL_OO(const Type, type, = find_initial(env, path->xid)); - while((path = path->next) && type && type->nspc) { - const Nspc nspc = type->nspc; + while ((path = path->next) && type && type->nspc) { + const Nspc nspc = type->nspc; const Type child = find_in_parent(type, path->xid); - if(!child) - ERR_O(path->pos, _("...(cannot find class '%s' in nspc '%s')"), s_name(path->xid), nspc->name) + if (!child) + ERR_O(path->pos, _("...(cannot find class '%s' in nspc '%s')"), + s_name(path->xid), nspc->name) } return type; } ANN m_bool already_defined(const Env env, const Symbol s, const loc_t pos) { const Value v = nspc_lookup_value0(env->curr, s); - if(!v || is_class(env->gwion, v->type)) - return GW_OK; - env_err(env, pos, - _("'%s' already declared as variable of type '%s'."), s_name(s), v->type->name); + if (!v || is_class(env->gwion, v->type)) return GW_OK; + env_err(env, pos, _("'%s' already declared as variable of type '%s'."), + s_name(s), v->type->name); return GW_ERROR; } - ANN static Type class_type(const Env env, const Type base) { const Type t_class = env->gwion->type[et_class]; - const Type t = type_copy(env->gwion->mp, t_class); - t->info->parent = t_class; + const Type t = type_copy(env->gwion->mp, t_class); + t->info->parent = t_class; t->info->base_type = base; set_tflag(t, tflag_infer); return t; } ANN Value mk_class(const Env env, const Type base, const loc_t loc) { - const Type t = class_type(env, base); + const Type t = class_type(env, base); const Symbol sym = insert_symbol(base->name); - const Value v = new_value(env->gwion->mp, t, s_name(sym)); + const Value v = new_value(env->gwion->mp, t, s_name(sym)); valuefrom(env, v->from, loc); SET_FLAG(v, const); set_vflag(v, vflag_valid); @@ -99,18 +97,18 @@ ANN Value global_string(const Env env, const m_str str) { char c[strlen(str) + 8]; sprintf(c, "%s:string", str); const Symbol sym = insert_symbol(c); - const Value v = nspc_lookup_value0(env->global_nspc, sym); - if(v) - return v; - const Value value = new_value(env->gwion->mp, env->gwion->type[et_string], s_name(sym)); + const Value v = nspc_lookup_value0(env->global_nspc, sym); + if (v) return v; + const Value value = + new_value(env->gwion->mp, env->gwion->type[et_string], s_name(sym)); nspc_add_value_front(env->global_nspc, sym, value); return value; } ANN m_bool isres(const Env env, const Symbol xid, const loc_t pos) { const Map map = &env->gwion->data->id; - for(m_uint i = 0; i < map_size(map); i++) { - if(xid == (Symbol)VKEY(map, i)) + for (m_uint i = 0; i < map_size(map); i++) { + if (xid == (Symbol)VKEY(map, i)) ERR_B(pos, _("%s is reserved."), s_name(xid)); } return GW_OK; diff --git a/src/env/envset.c b/src/env/envset.c index 0559da8a..2be348fe 100644 --- a/src/env/envset.c +++ b/src/env/envset.c @@ -6,10 +6,10 @@ #include "template.h" ANN static void check(struct EnvSet *es, const Type t) { - const Vector v = &es->env->scope->class_stack; - Type owner = t->info->value->from->owner_class; - for(vtype i = vector_size(v) + 1; owner && --i;) { - if(owner != (Type)vector_at(v, i - 1)) { + const Vector v = &es->env->scope->class_stack; + Type owner = t->info->value->from->owner_class; + for (vtype i = vector_size(v) + 1; owner && --i;) { + if (owner != (Type)vector_at(v, i - 1)) { es->run = 1; return; } @@ -18,30 +18,34 @@ ANN static void check(struct EnvSet *es, const Type t) { } ANN static m_bool push(struct EnvSet *es, const Type t) { - es->env->scope->depth = 0; + es->env->scope->depth = 0; const Type owner_class = t->info->value->from->owner_class; - if(owner_class) + if (owner_class) CHECK_BB(push(es, owner_class)); else - env_push(es->env, NULL, t->info->value->from->ctx ? t->info->value->from->ctx->nspc : es->env->curr); - if(es->func && !(t->tflag & es->flag)) - CHECK_BB(es->func((void*)es->data, t)); - env_push_type((void*)es->env, t); - if(tflag(t, tflag_tmpl)) - CHECK_BB(template_push_types(es->env, t->info->cdef->base.tmpl)); // incorrect templates? - es->_ctx = es->env->context; - es->_filename = es->env->name; + env_push(es->env, NULL, + t->info->value->from->ctx ? t->info->value->from->ctx->nspc + : es->env->curr); + if (es->func && !(t->tflag & es->flag)) + CHECK_BB(es->func((void *)es->data, t)); + env_push_type((void *)es->env, t); + if (tflag(t, tflag_tmpl)) + CHECK_BB(template_push_types( + es->env, t->info->cdef->base.tmpl)); // incorrect templates? + es->_ctx = es->env->context; + es->_filename = es->env->name; es->env->context = t->info->value->from->ctx; - es->env->name = t->info->value->from->filename; + es->env->name = t->info->value->from->filename; return GW_OK; } -ANN2(1,3) m_bool envset_push(struct EnvSet *es, const Type t, const Nspc nspc) { - if(t) { +ANN2(1, 3) +m_bool envset_push(struct EnvSet *es, const Type t, const Nspc nspc) { + if (t) { check(es, t); return es->run ? push(es, t) : GW_OK; } - if(nspc != es->env->curr) { + if (nspc != es->env->curr) { env_push(es->env, NULL, nspc); es->run = 1; } @@ -49,13 +53,12 @@ ANN2(1,3) m_bool envset_push(struct EnvSet *es, const Type t, const Nspc nspc) { } ANN2(1) static void _envset_pop(struct EnvSet *es, const Type t) { - if(safe_tflag(t, tflag_tmpl)) // might not be useful + if (safe_tflag(t, tflag_tmpl)) // might not be useful nspc_pop_type(es->env->gwion->mp, es->env->curr); env_pop(es->env, es->scope); - if(!t) - return; + if (!t) return; const Type owner_class = t->info->value->from->owner_class; - if(owner_class) + if (owner_class) envset_pop(es, owner_class); else env_pop(es->env, es->scope); @@ -63,21 +66,16 @@ ANN2(1) static void _envset_pop(struct EnvSet *es, const Type t) { ANN2(1) void envset_pop(struct EnvSet *es, const Type t) { _envset_pop(es, t); - if(es->_ctx) - es->env->context = es->_ctx; - if(es->_filename) - es->env->name = es->_filename; + if (es->_ctx) es->env->context = es->_ctx; + if (es->_filename) es->env->name = es->_filename; } ANN m_bool envset_run(struct EnvSet *es, const Type t) { check(es, t); const Type owner_class = t->info->value->from->owner_class; - if(es->run) - CHECK_BB(push(es, owner_class)); - const m_bool ret = t->info->cdef && - !(t->tflag & es->flag) ? - es->func(es->data, t) : GW_OK; - if(es->run) - envset_pop(es, owner_class); + if (es->run) CHECK_BB(push(es, owner_class)); + const m_bool ret = + t->info->cdef && !(t->tflag & es->flag) ? es->func(es->data, t) : GW_OK; + if (es->run) envset_pop(es, owner_class); return ret; } diff --git a/src/env/func.c b/src/env/func.c index 9855ff42..5f6e7242 100644 --- a/src/env/func.c +++ b/src/env/func.c @@ -6,40 +6,37 @@ #include "clean.h" ANN void free_func(Func a, Gwion gwion) { - if(fflag(a, fflag_tmpl)) - func_def_cleaner(gwion, a->def); - if(a->code) - vmcode_remref(a->code, gwion); - if(a->upvalues.ptr) - map_release(&a->upvalues); + if (fflag(a, fflag_tmpl)) func_def_cleaner(gwion, a->def); + if (a->code) vmcode_remref(a->code, gwion); + if (a->upvalues.ptr) map_release(&a->upvalues); mp_free(gwion->mp, Func, a); } ANN Func new_func(MemPool p, const m_str name, const Func_Def def) { - Func func = mp_calloc(p, Func); - func->name = name; - func->def = def; + Func func = mp_calloc(p, Func); + func->name = name; + func->def = def; func->inline_mult = 1.0; - func->ref = 1; + func->ref = 1; return func; } -ANN2(1,2) Symbol func_symbol(const Env env, const m_str nspc, const m_str base, - const m_str tmpl, const m_uint i) { +ANN2(1, 2) +Symbol func_symbol(const Env env, const m_str nspc, const m_str base, + const m_str tmpl, const m_uint i) { const size_t base_len = strlen(base); const size_t tmpl_len = !tmpl ? 0 : strlen(tmpl) + 4; const size_t nspc_len = strlen(nspc); - const size_t idx_len = num_digit(i); - const size_t len = base_len + tmpl_len + nspc_len + idx_len + 2; - char name[len + 1]; - CHECK_BO(sprintf(name, "%s%s%s%s@%" UINT_F "@%s", - base, !tmpl ? "" : ":[", !tmpl ? "" : tmpl, !tmpl ? "" : "]", - i, nspc)); + const size_t idx_len = num_digit(i); + const size_t len = base_len + tmpl_len + nspc_len + idx_len + 2; + char name[len + 1]; + CHECK_BO(sprintf(name, "%s%s%s%s@%" UINT_F "@%s", base, !tmpl ? "" : ":[", + !tmpl ? "" : tmpl, !tmpl ? "" : "]", i, nspc)); return insert_symbol(env->gwion->st, name); } -ANN void builtin_func(const MemPool mp, const Func f, void* func_ptr) { +ANN void builtin_func(const MemPool mp, const Func f, void *func_ptr) { set_vflag(f->value_ref, vflag_builtin); - f->code = new_vmcode(mp, NULL, f->def->stack_depth, 1, f->name); + f->code = new_vmcode(mp, NULL, f->def->stack_depth, 1, f->name); f->code->native_func = (m_uint)func_ptr; } diff --git a/src/env/nspc.c b/src/env/nspc.c index 1fad63ab..fadbf4ad 100644 --- a/src/env/nspc.c +++ b/src/env/nspc.c @@ -13,59 +13,63 @@ ANN void nspc_commit(const Nspc nspc) { scope_commit(nspc->info->trait); } -ANN static inline void nspc_release_object(const Nspc a, Value value, Gwion gwion) { - if((GET_FLAG(value, static) && a->info->class_data) || - (value->d.ptr && vflag(value, vflag_builtin))) { - const M_Object obj = value->d.ptr ? (M_Object)value->d.ptr : - *(M_Object*)(a->info->class_data + value->from->offset); - release(obj, gwion->vm->cleaner_shred); +ANN static inline void nspc_release_object(const Nspc a, Value value, + Gwion gwion) { + if ((GET_FLAG(value, static) && a->info->class_data) || + (value->d.ptr && vflag(value, vflag_builtin))) { + const M_Object obj = + value->d.ptr ? (M_Object)value->d.ptr + : *(M_Object *)(a->info->class_data + value->from->offset); + release(obj, gwion->vm->cleaner_shred); } } -ANN2(1,3) static inline void nspc_release_struct(const Nspc a, Value value, Gwion gwion) { - if(value && ((GET_FLAG(value, static) && a->info->class_data) || - (vflag(value, vflag_builtin) && value->d.ptr))) { - const m_bit *ptr = (value && value->d.ptr) ? (m_bit*)value->d.ptr: - (m_bit*)(a->info->class_data + value->from->offset); - for(m_uint i = 0; i < vector_size(&value->type->info->tuple->types); ++i) { +ANN2(1, 3) +static inline void nspc_release_struct(const Nspc a, Value value, Gwion gwion) { + if (value && ((GET_FLAG(value, static) && a->info->class_data) || + (vflag(value, vflag_builtin) && value->d.ptr))) { + const m_bit *ptr = + (value && value->d.ptr) + ? (m_bit *)value->d.ptr + : (m_bit *)(a->info->class_data + value->from->offset); + for (m_uint i = 0; i < vector_size(&value->type->info->tuple->types); ++i) { const Type t = (Type)vector_at(&value->type->info->tuple->types, i); - if(isa(t, gwion->type[et_object]) > 0) - release(*(M_Object*)(ptr + vector_at(&value->type->info->tuple->offset, i)), gwion->vm->cleaner_shred); - else if(tflag(t, tflag_struct)) + if (isa(t, gwion->type[et_object]) > 0) + release(*(M_Object *)(ptr + + vector_at(&value->type->info->tuple->offset, i)), + gwion->vm->cleaner_shred); + else if (tflag(t, tflag_struct)) nspc_release_struct(t->nspc, NULL, 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) { - if(isa(v->type, gwion->type[et_object]) > 0) + struct scope_iter iter = {a->info->value, 0, 0}; + Value v; + while (scope_iter(&iter, &v) > 0) { + if (isa(v->type, gwion->type[et_object]) > 0) nspc_release_object(a, v, gwion); - else if(tflag(v->type, tflag_struct)) + else if (tflag(v->type, tflag_struct)) nspc_release_struct(a, v, gwion); value_remref(v, gwion); } free_scope(gwion->mp, a->info->value); } -#define describe_nspc_free(A, b) \ -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) \ - b##_remref(a, gwion);\ - free_scope(gwion->mp, n->info->b);\ -} +#define describe_nspc_free(A, b) \ + 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) b##_remref(a, gwion); \ + free_scope(gwion->mp, n->info->b); \ + } -describe_nspc_free(Func, func) -describe_nspc_free(Type, type) -ANN static void nspc_free_trait(Nspc n, Gwion gwion) { - struct scope_iter iter = { n->info->trait, 0, 0 }; - Trait a; - while(scope_iter(&iter, &a) > 0) - free_trait(gwion->mp, a); +describe_nspc_free(Func, func) describe_nspc_free(Type, type) ANN + static void nspc_free_trait(Nspc n, Gwion gwion) { + struct scope_iter iter = {n->info->trait, 0, 0}; + Trait a; + while (scope_iter(&iter, &a) > 0) free_trait(gwion->mp, a); free_scope(gwion->mp, n->info->trait); } @@ -73,29 +77,25 @@ ANN void free_nspc(const Nspc a, const Gwion gwion) { free_nspc_value(a, gwion); nspc_free_func(a, gwion); nspc_free_trait(a, gwion); - if(a->info->op_map.ptr) - free_op_map(&a->info->op_map, gwion); + if (a->info->op_map.ptr) free_op_map(&a->info->op_map, gwion); nspc_free_type(a, gwion); - if(a->info->class_data && a->info->class_data_size) + if (a->info->class_data && a->info->class_data_size) mp_free2(gwion->mp, a->info->class_data_size, a->info->class_data); - if(a->vtable.ptr) - vector_release(&a->vtable); + if (a->vtable.ptr) vector_release(&a->vtable); mp_free(gwion->mp, NspcInfo, a->info); - if(a->pre_ctor) - vmcode_remref(a->pre_ctor, gwion); - if(a->dtor) - vmcode_remref(a->dtor, gwion); + if (a->pre_ctor) vmcode_remref(a->pre_ctor, gwion); + if (a->dtor) vmcode_remref(a->dtor, gwion); mp_free(gwion->mp, Nspc, a); } ANN Nspc new_nspc(MemPool p, const m_str name) { - const Nspc a = mp_calloc(p, Nspc); - a->name = name; - a->info = mp_calloc(p, NspcInfo); + const Nspc a = mp_calloc(p, Nspc); + a->name = name; + a->info = mp_calloc(p, NspcInfo); a->info->value = new_scope(p); a->info->type = new_scope(p); a->info->func = new_scope(p); a->info->trait = new_scope(p); - a->ref = 1; + a->ref = 1; return a; } diff --git a/src/env/trait.c b/src/env/trait.c index c6b53fbe..321bea69 100644 --- a/src/env/trait.c +++ b/src/env/trait.c @@ -4,14 +4,12 @@ ANN Trait new_trait(MemPool mp, const loc_t loc) { const Trait a = mp_calloc(mp, Trait); - a->loc = loc; + a->loc = loc; return a; } ANN void free_trait(MemPool mp, Trait a) { - if(a->requested_values.ptr) - vector_release(&a->requested_values); - if(a->requested_funcs.ptr) - vector_release(&a->requested_funcs); + if (a->requested_values.ptr) vector_release(&a->requested_values); + if (a->requested_funcs.ptr) vector_release(&a->requested_funcs); mp_free(mp, Trait, a); } diff --git a/src/env/tupleform.c b/src/env/tupleform.c index 5e215440..2dbf7bb9 100644 --- a/src/env/tupleform.c +++ b/src/env/tupleform.c @@ -23,11 +23,10 @@ ANN void tuple_info(const Env env, const Value v) { ANN void tuple_contains(const Env env, const Value value) { const Type t = value->type; - if(!env->class_def->info->tuple || env->class_def == value->type) - return; - const Vector v = &env->class_def->info->tuple->contains; - const m_int idx = vector_size(v) ? vector_find(v, (vtype)t) : -1; - if(idx == -1) { + if (!env->class_def->info->tuple || env->class_def == value->type) return; + const Vector v = &env->class_def->info->tuple->contains; + const m_int idx = vector_size(v) ? vector_find(v, (vtype)t) : -1; + if (idx == -1) { type_addref(t); vector_add(v, (vtype)t); } @@ -38,12 +37,12 @@ ANN2(1) TupleForm new_tupleform(MemPool p, const Type parent_type) { vector_init(&tuple->contains); vector_init(&tuple->types); vector_init(&tuple->offset); - if(parent_type && parent_type->info->tuple) { + if (parent_type && parent_type->info->tuple) { const TupleForm parent = parent_type->info->tuple; - const m_uint sz = vector_size(&parent->types); - tuple->start = parent->start + sz; - if(sz) { - const Type last = (Type)vector_back(&parent->types); + const m_uint sz = vector_size(&parent->types); + tuple->start = parent->start + sz; + if (sz) { + const Type last = (Type)vector_back(&parent->types); const m_uint offset = vector_back(&parent->offset); vector_add(&tuple->offset, offset + last->size); } else { @@ -57,8 +56,8 @@ ANN2(1) TupleForm new_tupleform(MemPool p, const Type parent_type) { } ANN void free_tupleform(const TupleForm tuple, const struct Gwion_ *gwion) { - for(m_uint i = 0; i < vector_size(&tuple->contains); ++i) - type_remref((Type)vector_at(&tuple->contains, i), (void*)gwion); + for (m_uint i = 0; i < vector_size(&tuple->contains); ++i) + type_remref((Type)vector_at(&tuple->contains, i), (void *)gwion); vector_release(&tuple->contains); vector_release(&tuple->types); vector_release(&tuple->offset); diff --git a/src/env/type.c b/src/env/type.c index 2ac183fb..4110a777 100644 --- a/src/env/type.c +++ b/src/env/type.c @@ -9,69 +9,64 @@ #include "object.h" ANN static inline m_bool freeable(const Type a) { - return tflag(a, tflag_tmpl) ||GET_FLAG(a, global); + return tflag(a, tflag_tmpl) || GET_FLAG(a, global); } ANN void free_type(const Type a, struct Gwion_ *const gwion) { - if(freeable(a)) { - if(tflag(a, tflag_udef)) + if (freeable(a)) { + if (tflag(a, tflag_udef)) free_union_def(gwion->mp, a->info->udef); - else if(tflag(a, tflag_cdef)) + else if (tflag(a, tflag_cdef)) class_def_cleaner(gwion, a->info->cdef); } - if(a->effects.ptr) - vector_release(&a->effects); - if(a->nspc) - nspc_remref(a->nspc, gwion); - if(a->info->tuple) - free_tupleform(a->info->tuple, gwion); + if (a->effects.ptr) vector_release(&a->effects); + if (a->nspc) nspc_remref(a->nspc, gwion); + if (a->info->tuple) free_tupleform(a->info->tuple, gwion); mp_free(gwion->mp, TypeInfo, a->info); mp_free(gwion->mp, Type, a); - } Type new_type(MemPool p, const m_str name, const Type parent) { - const Type type = mp_calloc(p, Type); - type->name = name; - type->info = mp_calloc(p, TypeInfo); + const Type type = mp_calloc(p, Type); + type->name = name; + type->info = mp_calloc(p, TypeInfo); type->info->parent = parent; - if(parent) - type->size = parent->size; + if (parent) type->size = parent->size; type->ref = 1; return type; } ANN Type type_copy(MemPool p, const Type type) { - const Type a = new_type(p, type->name, type->info->parent); - a->size = type->size; - a->array_depth = type->array_depth; - a->info->gack = type->info->gack; + const Type a = new_type(p, type->name, type->info->parent); + a->size = type->size; + a->array_depth = type->array_depth; + a->info->gack = type->info->gack; return a; } ANN m_bool isa(const restrict Type var, const restrict Type parent) { - return (var == parent) ? GW_OK : var->info->parent ? isa(var->info->parent, parent) : GW_ERROR; + return (var == parent) ? GW_OK + : var->info->parent ? isa(var->info->parent, parent) + : GW_ERROR; } ANN Type find_common_anc(const restrict Type lhs, const restrict Type rhs) { return isa(lhs, rhs) > 0 ? rhs : isa(rhs, lhs) > 0 ? lhs : NULL; } -#define describe_find(name, t) \ -ANN t find_##name(const Type type, const Symbol xid) { \ - if(type->nspc) { \ - const t val = nspc_lookup_##name##2(type->nspc, xid); \ - if(val) \ - return val; \ - } \ - return type->info->parent ? find_##name(type->info->parent, xid) : NULL; \ -} +#define describe_find(name, t) \ + ANN t find_##name(const Type type, const Symbol xid) { \ + if (type->nspc) { \ + const t val = nspc_lookup_##name##2(type->nspc, xid); \ + if (val) return val; \ + } \ + return type->info->parent ? find_##name(type->info->parent, xid) : NULL; \ + } describe_find(value, Value) -//describe_find(func, Func) + // describe_find(func, Func) -ANN Type typedef_base(Type t) { - while(tflag(t, tflag_typedef)) - t = t->info->parent; + ANN Type typedef_base(Type t) { + while (tflag(t, tflag_typedef)) t = t->info->parent; return t; } @@ -80,17 +75,17 @@ ANN Type array_base(Type type) { return t->array_depth ? array_base(t->info->base_type) : t; } -ANN /*static */Symbol array_sym(const Env env, const Type src, const m_uint depth) { - if(src->array_depth == depth) - return insert_symbol(src->name); +ANN /*static */ Symbol array_sym(const Env env, const Type src, + const m_uint depth) { + if (src->array_depth == depth) return insert_symbol(src->name); const m_uint total_depth = src->array_depth + depth; - const Type t = array_base(src); - size_t len = strlen(t->name); - char name[len + 2 * total_depth + 1]; + const Type t = array_base(src); + size_t len = strlen(t->name); + char name[len + 2 * total_depth + 1]; strcpy(name, t->name); m_uint i = total_depth + 1; - while(--i) { - strcpy(name+len, "[]"); + while (--i) { + strcpy(name + len, "[]"); len += 2; } return insert_symbol(name); @@ -100,66 +95,62 @@ ANN /*static */Symbol array_sym(const Env env, const Type src, const m_uint dept #include "operator.h" #include "import.h" ANN Type array_type(const Env env, const Type src, const m_uint depth) { - const Symbol sym = array_sym(env, src, depth); - const Type type = nspc_lookup_type1(src->info->value->from->owner, sym); - if(type) - return type; - const size_t tdepth = depth + src->array_depth; - const Type base = tdepth > 1 ? array_type(env, src, tdepth-1) : src; - struct TemplateScan ts = { .t=base, /*.td=td*/ }; - struct Op_Import opi = { .op=insert_symbol("@scan"), .lhs=env->gwion->type[et_array], .data=(uintptr_t)&ts }; + const Symbol sym = array_sym(env, src, depth); + const Type type = nspc_lookup_type1(src->info->value->from->owner, sym); + if (type) return type; + const size_t tdepth = depth + src->array_depth; + const Type base = tdepth > 1 ? array_type(env, src, tdepth - 1) : src; + struct TemplateScan ts = {.t = base, /*.td=td*/}; + struct Op_Import opi = {.op = insert_symbol("@scan"), + .lhs = env->gwion->type[et_array], + .data = (uintptr_t)&ts}; return op_check(env, &opi); } ANN m_bool type_ref(Type t) { do { - if(tflag(t, tflag_empty)) - return GW_OK; - if(tflag(t, tflag_typedef) && tflag(t, tflag_cdef)) { - if(t->info->cdef->base.ext && t->info->cdef->base.ext->array) { - if(!t->info->cdef->base.ext->array->exp) + if (tflag(t, tflag_empty)) return GW_OK; + if (tflag(t, tflag_typedef) && tflag(t, tflag_cdef)) { + if (t->info->cdef->base.ext && t->info->cdef->base.ext->array) { + if (!t->info->cdef->base.ext->array->exp) return GW_OK; else { const Type type = t->info->parent->info->base_type; - if(tflag(type, tflag_empty)) - return GW_OK; + if (tflag(type, tflag_empty)) return GW_OK; } } } - } while((t = t->info->parent)); + } while ((t = t->info->parent)); return 0; } - ANN m_uint get_depth(const Type type) { m_uint depth = 0; - Type t = type; + Type t = type; do { - if(t->array_depth) { + if (t->array_depth) { depth += t->array_depth; t = t->info->base_type; } else t = t->info->parent; - } while(t); + } while (t); return depth; } -ANN m_bool is_fptr(const struct Gwion_* gwion, const Type t) { +ANN m_bool is_fptr(const struct Gwion_ *gwion, const Type t) { return isa(actual_type(gwion, t), gwion->type[et_fptr]) > 0; } -ANN inline m_bool is_class(const struct Gwion_* gwion, const Type t) { +ANN inline m_bool is_class(const struct Gwion_ *gwion, const Type t) { return isa(t, gwion->type[et_class]) > 0; } -ANN Type actual_type(const struct Gwion_* gwion, const Type t) { +ANN Type actual_type(const struct Gwion_ *gwion, const Type t) { return is_class(gwion, t) ? t->info->base_type : t; } ANN void inherit(const Type t) { const Nspc nspc = t->nspc, parent = t->info->parent->nspc; - if(!nspc || !parent) - return; + if (!nspc || !parent) return; nspc->info->offset = parent->info->offset; - if(parent->vtable.ptr) - vector_copy2(&parent->vtable, &nspc->vtable); + if (parent->vtable.ptr) vector_copy2(&parent->vtable, &nspc->vtable); } diff --git a/src/env/value.c b/src/env/value.c index c7689f84..5c95cf31 100644 --- a/src/env/value.c +++ b/src/env/value.c @@ -7,9 +7,9 @@ #define MAX(a, b) (a >= b ? a : b) ANN void free_value(Value a, Gwion gwion) { const Type t = a->type; - if(t->size > SZ_INT && !vflag(a, vflag_func) && a->d.ptr) + if (t->size > SZ_INT && !vflag(a, vflag_func) && a->d.ptr) _mp_free(gwion->mp, t->size, a->d.ptr); - else if(is_class(gwion, t)) + else if (is_class(gwion, t)) type_remref(t, gwion); mp_free(gwion->mp, ValueFrom, a->from); mp_free(gwion->mp, Value, a); @@ -17,17 +17,17 @@ ANN void free_value(Value a, Gwion gwion) { ANN Value new_value(MemPool p, const Type type, const m_str name) { const Value a = mp_calloc(p, Value); - a->from = mp_calloc(p, ValueFrom); + a->from = mp_calloc(p, ValueFrom); a->type = type; a->name = name; - a->ref = 1; + a->ref = 1; return a; } ANN void valuefrom(const Env env, struct ValueFrom_ *from, const loc_t loc) { - from->owner = env->curr; + from->owner = env->curr; from->owner_class = env->class_def; - from->ctx = env->context; - from->filename = env->name; - from->loc = loc; + from->ctx = env->context; + from->filename = env->name; + from->loc = loc; } diff --git a/src/gwion.c b/src/gwion.c index 17b05f60..d39cccdb 100644 --- a/src/gwion.c +++ b/src/gwion.c @@ -15,15 +15,15 @@ #include "arg.h" #include "compile.h" #include "object.h" // fork_clean -#include "pass.h" // fork_clean +#include "pass.h" // fork_clean #include "shreduler_private.h" ANN m_bool gwion_audio(const Gwion gwion) { Driver *const di = gwion->vm->bbq; - if(di->si->arg) - driver_ini(gwion); + if (di->si->arg) driver_ini(gwion); di->func(di->driver); - CHECK_BB(di->driver->ini(gwion->vm, di));; + CHECK_BB(di->driver->ini(gwion->vm, di)); + ; driver_alloc(di); return GW_OK; } @@ -33,39 +33,39 @@ ANN static inline m_bool gwion_engine(const Gwion gwion) { } ANN static void gwion_cleaner(const Gwion gwion) { - const VM_Code code = new_vmcode(gwion->mp, NULL, 0, 1, "in code dtor"); + const VM_Code code = new_vmcode(gwion->mp, NULL, 0, 1, "in code dtor"); gwion->vm->cleaner_shred = new_vm_shred(gwion->mp, code); vm_ini_shred(gwion->vm, gwion->vm->cleaner_shred); } -ANN VM* gwion_cpy(const VM* src) { - const Gwion gwion = mp_calloc(src->gwion->mp, Gwion); - gwion->vm = new_vm(src->gwion->mp, false); - gwion->vm->gwion = gwion; +ANN VM *gwion_cpy(const VM *src) { + const Gwion gwion = mp_calloc(src->gwion->mp, Gwion); + gwion->vm = new_vm(src->gwion->mp, false); + gwion->vm->gwion = gwion; gwion->vm->bbq->si = soundinfo_cpy(src->gwion->mp, src->bbq->si); - gwion->emit = src->gwion->emit; - gwion->env = src->gwion->env; - gwion->data = cpy_gwiondata(src->gwion->mp, src->gwion->data); - gwion->st = src->gwion->st; - gwion->mp = src->gwion->mp; - gwion->type = src->gwion->type; + gwion->emit = src->gwion->emit; + gwion->env = src->gwion->env; + gwion->data = cpy_gwiondata(src->gwion->mp, src->gwion->data); + gwion->st = src->gwion->st; + gwion->mp = src->gwion->mp; + gwion->type = src->gwion->type; return gwion->vm; } ANN static void gwion_core(const Gwion gwion) { - gwion->vm = new_vm(gwion->mp, true); - gwion->emit = new_emitter(gwion->mp); - gwion->env = new_env(gwion->mp); + gwion->vm = new_vm(gwion->mp, true); + gwion->emit = new_emitter(gwion->mp); + gwion->env = new_env(gwion->mp); gwion->emit->env = gwion->env; gwion->vm->gwion = gwion->emit->gwion = gwion->env->gwion = gwion; } -ANN static m_bool gwion_ok(const Gwion gwion, Arg* arg) { +ANN static m_bool gwion_ok(const Gwion gwion, Arg *arg) { CHECK_BB(plug_ini(gwion, &arg->lib)); shreduler_set_loop(gwion->vm->shreduler, arg->loop); - if(gwion_audio(gwion) > 0) { + if (gwion_audio(gwion) > 0) { plug_run(gwion, &arg->mod); - if(gwion_engine(gwion)) { + if (gwion_engine(gwion)) { gwion_cleaner(gwion); (void)arg_compile(gwion, arg); return GW_OK; @@ -79,16 +79,16 @@ ANN static m_bool gwion_ok(const Gwion gwion, Arg* arg) { ANN static void doc_mode(const Gwion gwion) { struct Vector_ v; vector_init(&v); - vector_add(&v, (m_uint)"scan0"); + vector_add(&v, (m_uint) "scan0"); pass_set(gwion, &v); } -ANN m_bool gwion_ini(const Gwion gwion, Arg* arg) { +ANN m_bool gwion_ini(const Gwion gwion, Arg *arg) { #ifdef USE_GETTEXT setlocale(LC_ALL, NULL); - bindtextdomain (GWION_PACKAGE, LOCALE_INFO); - bindtextdomain (GWION_PACKAGE "_util", LOCALE_INFO); - bindtextdomain (GWION_PACKAGE "_ast", LOCALE_INFO); + bindtextdomain(GWION_PACKAGE, LOCALE_INFO); + bindtextdomain(GWION_PACKAGE "_util", LOCALE_INFO); + bindtextdomain(GWION_PACKAGE "_ast", LOCALE_INFO); #endif gwion->mp = mempool_ini((sizeof(struct VM_Shred_) + SIZEOF_REG + SIZEOF_MEM)); gwion->st = new_symbol_table(gwion->mp, 65347); @@ -96,17 +96,17 @@ ANN m_bool gwion_ini(const Gwion gwion, Arg* arg) { pparg_ini(gwion->mp, gwion->ppa); gwion_core(gwion); gwion->data = new_gwiondata(gwion->mp); - gwion->type = (Type*)xcalloc(MAX_TYPE, sizeof(struct Type_*)); + gwion->type = (Type *)xcalloc(MAX_TYPE, sizeof(struct Type_ *)); arg->si = gwion->vm->bbq->si = new_soundinfo(gwion->mp); new_passes(gwion); CHECK_BB(arg_parse(gwion, arg)); - if(arg->color == COLOR_NEVER) + if (arg->color == COLOR_NEVER) tcol_override_color_checks(0); - else if(arg->color == COLOR_AUTO) + else if (arg->color == COLOR_AUTO) tcol_override_color_checks(isatty(1)); - else if(arg->color == COLOR_ALWAYS) + else if (arg->color == COLOR_ALWAYS) tcol_override_color_checks(1); - if(!gwion->data->cdoc) + if (!gwion->data->cdoc) pass_default(gwion); else doc_mode(gwion); @@ -114,7 +114,7 @@ ANN m_bool gwion_ini(const Gwion gwion, Arg* arg) { } ANN void gwion_run(const Gwion gwion) { - VM* vm = gwion->vm; + VM *vm = gwion->vm; vm->bbq->driver->run(vm, vm->bbq); } @@ -126,7 +126,7 @@ ANN static inline void free_gwion_cpy(const Gwion gwion, const VM_Shred shred) { } ANN static void fork_clean2(const VM_Shred shred, const Vector v) { - for(m_uint i = 0; i < vector_size(v); ++i) { + for (m_uint i = 0; i < vector_size(v); ++i) { const Gwion gwion = (Gwion)vector_at(v, i); free_gwion_cpy(gwion, shred); } @@ -135,17 +135,14 @@ ANN static void fork_clean2(const VM_Shred shred, const Vector v) { } ANN void gwion_end_child(const VM_Shred shred, const Gwion gwion) { - if(gwion->data->child.ptr) - fork_clean(shred, &gwion->data->child); - if(gwion->data->child2.ptr) - fork_clean2(shred, &gwion->data->child2); + if (gwion->data->child.ptr) fork_clean(shred, &gwion->data->child); + if (gwion->data->child2.ptr) fork_clean2(shred, &gwion->data->child2); } ANN void gwion_end(const Gwion gwion) { gwion_end_child(gwion->vm->cleaner_shred, gwion); free_env(gwion->env); - if(gwion->vm->cleaner_shred) - free_vm_shred(gwion->vm->cleaner_shred); + if (gwion->vm->cleaner_shred) free_vm_shred(gwion->vm->cleaner_shred); free_emitter(gwion->mp, gwion->emit); free_vm(gwion->vm); pparg_end(gwion->ppa); @@ -157,13 +154,14 @@ ANN void gwion_end(const Gwion gwion) { } ANN void env_error_footer(const Env env) { - if(env->class_def && tflag(env->class_def, tflag_cdef)) + if (env->class_def && tflag(env->class_def, tflag_cdef)) gwerr_secondary("in class", env->name, env->class_def->info->cdef->pos); - if(env->func && env->func->def) + if (env->func && env->func->def) gwerr_secondary("in function", env->name, env->func->def->base->pos); } -ANN static void env_xxx(const Env env, const loc_t pos, const m_str fmt, va_list arg) { +ANN static void env_xxx(const Env env, const loc_t pos, const m_str fmt, + va_list arg) { #ifndef __FUZZING__ va_list tmpa; va_copy(tmpa, arg); @@ -186,30 +184,27 @@ ANN void env_warn(const Env env, const loc_t pos, const m_str fmt, ...) { } ANN void env_err(const Env env, const loc_t pos, const m_str fmt, ...) { - if(env->context && env->context->error) - return; + if (env->context && env->context->error) return; #ifndef __FUZZING__ va_list arg; va_start(arg, fmt); env_xxx(env, pos, fmt, arg); va_end(arg); #endif - if(env->context) - env->context->error = 1; + if (env->context) env->context->error = 1; } -ANN struct SpecialId_* specialid_get(const Gwion gwion, const Symbol sym) { +ANN struct SpecialId_ *specialid_get(const Gwion gwion, const Symbol sym) { const Map map = &gwion->data->id; - for(m_uint i = 0; i < map_size(map); ++i) { - if(sym == (Symbol)VKEY(map, i)) - return (struct SpecialId_*)VVAL(map, i); + for (m_uint i = 0; i < map_size(map); ++i) { + if (sym == (Symbol)VKEY(map, i)) return (struct SpecialId_ *)VVAL(map, i); } return NULL; } ANN void push_global(struct Gwion_ *gwion, const m_str name) { - const Nspc nspc = new_nspc(gwion->mp, name); - nspc->parent = gwion->env->global_nspc; + const Nspc nspc = new_nspc(gwion->mp, name); + nspc->parent = gwion->env->global_nspc; gwion->env->curr = gwion->env->global_nspc = nspc; } diff --git a/src/gwiondata.c b/src/gwiondata.c index 7b2af4b5..3ded0aa8 100644 --- a/src/gwiondata.c +++ b/src/gwiondata.c @@ -10,25 +10,25 @@ #include "specialid.h" #include "pass.h" -ANN static inline GwionData* gwiondata(MemPool mp) { +ANN static inline GwionData *gwiondata(MemPool mp) { struct GwionData_ *data = mp_calloc(mp, GwionData); MUTEX_SETUP(data->mutex); return data; } -ANN GwionData* new_gwiondata(const MemPool mp) { +ANN GwionData *new_gwiondata(const MemPool mp) { GwionData *data = gwiondata(mp); map_init(&data->freearg); map_init(&data->id); return data; } -ANN GwionData* cpy_gwiondata(MemPool mp, const GwionData* src) { +ANN GwionData *cpy_gwiondata(MemPool mp, const GwionData *src) { GwionData *data = gwiondata(mp); - data->freearg = src->freearg; - data->id = src->id; - data->plug = src->plug; - data->passes = src->passes; + data->freearg = src->freearg; + data->id = src->id; + data->plug = src->plug; + data->passes = src->passes; return data; } @@ -40,11 +40,10 @@ ANN void free_gwiondata_cpy(const MemPool mp, GwionData *data) { ANN void free_gwiondata(const Gwion gwion) { GwionData *data = gwion->data; map_release(&data->freearg); - for(m_uint i = 0; i < map_size(&data->id); ++i) - mp_free(gwion->mp, SpecialId, (struct SpecialId_*)map_at(&data->id, i)); + for (m_uint i = 0; i < map_size(&data->id); ++i) + mp_free(gwion->mp, SpecialId, (struct SpecialId_ *)map_at(&data->id, i)); map_release(&data->id); free_passes(gwion->mp, data->passes); - if(data->plug.ptr) - free_plug(gwion); + if (data->plug.ptr) free_plug(gwion); free_gwiondata_cpy(gwion->mp, data); } diff --git a/src/import/cleaner.c b/src/import/cleaner.c index c2b860ef..7d6b65de 100644 --- a/src/import/cleaner.c +++ b/src/import/cleaner.c @@ -15,18 +15,16 @@ #include "gwi.h" ANN m_bool ck_ini(const Gwi gwi, const enum importck_type t) { - if(gwi->ck) // TODO: improve error message + if (gwi->ck) // TODO: improve error message GWI_ERR_B(_("already importing")) - gwi->ck = mp_calloc2(gwi->gwion->mp, sizeof(ImportCK)); + gwi->ck = mp_calloc2(gwi->gwion->mp, sizeof(ImportCK)); gwi->ck->type = t; return GW_OK; } ANN m_bool ck_ok(const Gwi gwi, const enum importck_type t) { - if(!gwi->ck) - GWI_ERR_B(_("import not started")) - if(gwi->ck->type == t) - return GW_OK; + if (!gwi->ck) GWI_ERR_B(_("import not started")) + if (gwi->ck->type == t) return GW_OK; // TODO: improve error message GWI_ERR_B(_("already importing")) } @@ -36,16 +34,10 @@ ANN void ck_end(const Gwi gwi) { gwi->ck = NULL; } -typedef void (*cleaner) (MemPool, ImportCK*); -static cleaner cleaners[] = -{ - ck_clean_edef, - ck_clean_udef, - ck_clean_tdef, - NULL,// ck_clean_oper, - ck_clean_item, - ck_clean_fdef -}; +typedef void (*cleaner)(MemPool, ImportCK *); +static cleaner cleaners[] = {ck_clean_edef, ck_clean_udef, ck_clean_tdef, + NULL, // ck_clean_oper, + ck_clean_item, ck_clean_fdef}; ANN void ck_clean(const Gwi gwi) { cleaners[gwi->ck->type](gwi->gwion->mp, gwi->ck); diff --git a/src/import/import_cdef.c b/src/import/import_cdef.c index f8f202fd..93469d3f 100644 --- a/src/import/import_cdef.c +++ b/src/import/import_cdef.c @@ -17,53 +17,53 @@ #include "specialid.h" #include "template.h" -ANN static m_bool mk_xtor(MemPool p, const Type type, const m_uint d, const enum tflag e) { - VM_Code* code = e == tflag_ctor ? &type->nspc->pre_ctor : &type->nspc->dtor; - const m_str name = type->name; - *code = new_vmcode(p, NULL, SZ_INT, 1, name); +ANN static m_bool mk_xtor(MemPool p, const Type type, const m_uint d, + const enum tflag e) { + VM_Code *code = e == tflag_ctor ? &type->nspc->pre_ctor : &type->nspc->dtor; + const m_str name = type->name; + *code = new_vmcode(p, NULL, SZ_INT, 1, name); (*code)->native_func = (m_uint)d; type->tflag |= e; return GW_OK; } -ANN2(1,2) static void import_class_ini(const Env env, const Type t) { - t->nspc = new_nspc(env->gwion->mp, t->name); +ANN2(1, 2) static void import_class_ini(const Env env, const Type t) { + t->nspc = new_nspc(env->gwion->mp, t->name); t->nspc->parent = env->curr; - if(isa(t, env->gwion->type[et_object]) > 0) - inherit(t); + if (isa(t, env->gwion->type[et_object]) > 0) inherit(t); env_push_type(env, t); } ANN2(1) void add_template(const Env env, const Type t) { - Tmpl* tmpl = t->info->cdef->base.tmpl; - if(tmpl) { - nspc_push_type(env->gwion->mp, env->curr);// + Tmpl *tmpl = t->info->cdef->base.tmpl; + if (tmpl) { + nspc_push_type(env->gwion->mp, env->curr); // Specialized_List il = tmpl->list; do nspc_add_type(env->curr, il->xid, env->gwion->type[et_auto]); - while((il = il->next)); + while ((il = il->next)); } } -ANN2(1) void gwi_class_xtor(const Gwi gwi, const f_xtor ctor, const f_xtor dtor) { +ANN2(1) +void gwi_class_xtor(const Gwi gwi, const f_xtor ctor, const f_xtor dtor) { const Type t = gwi->gwion->env->class_def; - if(ctor) - mk_xtor(gwi->gwion->mp, t, (m_uint)ctor, tflag_ctor); - if(dtor) - mk_xtor(gwi->gwion->mp, t, (m_uint)dtor, tflag_dtor); + if (ctor) mk_xtor(gwi->gwion->mp, t, (m_uint)ctor, tflag_ctor); + if (dtor) mk_xtor(gwi->gwion->mp, t, (m_uint)dtor, tflag_dtor); } ANN static inline void gwi_type_flag(const Type t) { - set_tflag(t, tflag_scan0 | tflag_scan1 | tflag_scan2 | tflag_check | tflag_emit); + set_tflag(t, + tflag_scan0 | tflag_scan1 | tflag_scan2 | tflag_check | tflag_emit); } ANN static Type type_finish(const Gwi gwi, const Type t) { gwi_add_type(gwi, t); import_class_ini(gwi->gwion->env, t); - if(t->info->cdef && t->info->cdef->base.tmpl) { + if (t->info->cdef && t->info->cdef->base.tmpl) { gwi->tmpls++; add_template(gwi->gwion->env, t); } - if(gwi->gwion->data->cdoc) { + if (gwi->gwion->data->cdoc) { lint_indent(gwi->lint); gwi->lint->indent++; lint_class_def(gwi->lint, t->info->cdef); @@ -71,31 +71,28 @@ ANN static Type type_finish(const Gwi gwi, const Type t) { return t; } -ANN2(1,2) Type gwi_class_ini(const Gwi gwi, const m_str name, const m_str parent) { - struct ImportCK ck = { .name=name }; +ANN2(1, 2) +Type gwi_class_ini(const Gwi gwi, const m_str name, const m_str parent) { + struct ImportCK ck = {.name = name}; CHECK_BO(check_typename_def(gwi, &ck)); - DECL_OO(Type_Decl *,td, = gwi_str2td(gwi, parent ?: "Object")); - Tmpl* tmpl = ck.sl ? new_tmpl_base(gwi->gwion->mp, ck.sl) : NULL; - if(tmpl) - CHECK_BO(template_push_types(gwi->gwion->env, tmpl)); - const Type base = find_type(gwi->gwion->env, td); - const Type_List tl = td->types; - if(tflag(base, tflag_ntmpl)) - td->types = NULL; + DECL_OO(Type_Decl *, td, = gwi_str2td(gwi, parent ?: "Object")); + Tmpl *tmpl = ck.sl ? new_tmpl_base(gwi->gwion->mp, ck.sl) : NULL; + if (tmpl) CHECK_BO(template_push_types(gwi->gwion->env, tmpl)); + const Type base = find_type(gwi->gwion->env, td); + const Type_List tl = td->types; + if (tflag(base, tflag_ntmpl)) td->types = NULL; const Type p = !td->types ? known_type(gwi->gwion->env, td) : NULL; - td->types = tl; - if(tmpl) - nspc_pop_type(gwi->gwion->mp, gwi->gwion->env->curr); + td->types = tl; + if (tmpl) nspc_pop_type(gwi->gwion->mp, gwi->gwion->env->curr); CHECK_OO(p); - const Type t = new_type(gwi->gwion->mp, s_name(ck.sym), p); + const Type t = new_type(gwi->gwion->mp, s_name(ck.sym), p); t->info->cdef = new_class_def(gwi->gwion->mp, 0, ck.sym, td, NULL, gwi->loc); t->info->cdef->base.tmpl = tmpl; t->info->cdef->base.type = t; - t->info->tuple = new_tupleform(gwi->gwion->mp, p); - t->info->parent = p; - if(td->array) - set_tflag(t, tflag_typedef); - if(ck.sl) + t->info->tuple = new_tupleform(gwi->gwion->mp, p); + t->info->parent = p; + if (td->array) set_tflag(t, tflag_typedef); + if (ck.sl) set_tflag(t, tflag_tmpl | tflag_ntmpl); else gwi_type_flag(t); @@ -103,18 +100,20 @@ ANN2(1,2) Type gwi_class_ini(const Gwi gwi, const m_str name, const m_str parent } ANN Type gwi_struct_ini(const Gwi gwi, const m_str name) { - struct ImportCK ck = { .name=name }; + struct ImportCK ck = {.name = name}; CHECK_BO(check_typename_def(gwi, &ck)); - const Type t = new_type(gwi->gwion->mp, s_name(ck.sym), gwi->gwion->type[et_compound]); + const Type t = + new_type(gwi->gwion->mp, s_name(ck.sym), gwi->gwion->type[et_compound]); set_tflag(t, tflag_struct); - if(!ck.sl) + if (!ck.sl) gwi_type_flag(t); else { - t->info->cdef = new_class_def(gwi->gwion->mp, 0, ck.sym, NULL, NULL, gwi->loc); + t->info->cdef = + new_class_def(gwi->gwion->mp, 0, ck.sym, NULL, NULL, gwi->loc); t->info->cdef->base.type = t; t->info->cdef->base.tmpl = new_tmpl_base(gwi->gwion->mp, ck.sl); - t->info->tuple = new_tupleform(gwi->gwion->mp, NULL); - t->info->parent = NULL; + t->info->tuple = new_tupleform(gwi->gwion->mp, NULL); + t->info->parent = NULL; t->info->cdef->cflag |= cflag_struct; set_tflag(t, tflag_tmpl | tflag_ntmpl); } @@ -122,20 +121,20 @@ ANN Type gwi_struct_ini(const Gwi gwi, const m_str name) { } ANN m_int gwi_class_end(const Gwi gwi) { - if(gwi->gwion->data->cdoc) { + if (gwi->gwion->data->cdoc) { gwi->lint->indent--; lint_rbrace(gwi->lint); lint_nl(gwi->lint); } - if(!gwi->gwion->env->class_def) + if (!gwi->gwion->env->class_def) GWI_ERR_B(_("import: too many class_end called.")) nspc_allocdata(gwi->gwion->mp, gwi->gwion->env->class_def->nspc); const Type t = gwi->gwion->env->class_def; - if(tflag(t, tflag_tmpl)) { + if (tflag(t, tflag_tmpl)) { --gwi->tmpls; nspc_pop_type(gwi->gwion->mp, gwi->gwion->env->curr); } - if(gwi->effects.ptr) { + if (gwi->effects.ptr) { vector_init(&t->effects); vector_copy2(&gwi->effects, &t->effects); vector_release(&gwi->effects); diff --git a/src/import/import_checker.c b/src/import/import_checker.c index 45c297df..c855a586 100644 --- a/src/import/import_checker.c +++ b/src/import/import_checker.c @@ -16,58 +16,57 @@ #include "gwi.h" struct td_checker { - m_str str; + m_str str; const loc_t pos; }; struct AC { - m_str str; - Exp base; - Exp exp; - loc_t pos; + m_str str; + Exp base; + Exp exp; + loc_t pos; m_uint depth; }; -ANN static m_bool ac_run(const Gwion gwion, struct AC *const ac); +ANN static m_bool ac_run(const Gwion gwion, struct AC *const ac); ANN static Array_Sub mk_array(MemPool mp, struct AC *ac) { const Array_Sub array = new_array_sub(mp, ac->base); - array->depth = ac->depth; - array->exp = ac->base; + array->depth = ac->depth; + array->exp = ac->base; return array; } -__attribute__((returns_nonnull)) -ANN static Symbol gwisym(const Gwion gwion, const m_str str) { +__attribute__((returns_nonnull)) ANN static Symbol gwisym(const Gwion gwion, + const m_str str) { return insert_symbol(gwion->st, str); } ANN static Symbol __str2sym(const Gwion gwion, struct td_checker *tdc) { - char buf[strlen(tdc->str) + 1]; + char buf[strlen(tdc->str) + 1]; m_str tmp = buf; - if(*tdc->str == '@') - *tmp++ = *tdc->str++; - while(*tdc->str) { + if (*tdc->str == '@') *tmp++ = *tdc->str++; + while (*tdc->str) { const char c = *tdc->str; - if(!isalnum(c) && c != '_') - break; + if (!isalnum(c) && c != '_') break; *tmp++ = *tdc->str++; } - if(tmp == buf) - GWION_ERR_O(tdc->pos, "empty symbol"); + if (tmp == buf) GWION_ERR_O(tdc->pos, "empty symbol"); *tmp = '\0'; return gwisym(gwion, buf); } -ANN static inline Symbol _str2sym(const Gwion gwion, struct td_checker *tdc, const m_str path) { +ANN static inline Symbol _str2sym(const Gwion gwion, struct td_checker *tdc, + const m_str path) { const Symbol sym = __str2sym(gwion, tdc); - if(*tdc->str && *tdc->str != ':') - GWION_ERR_O(tdc->pos, _("illegal character '%c' in path '%s'."), *tdc->str, path) + if (*tdc->str && *tdc->str != ':') + GWION_ERR_O(tdc->pos, _("illegal character '%c' in path '%s'."), *tdc->str, + path) return sym; } /** convert a string to a symbol, with error checking **/ ANN Symbol str2sym(const Gwion gwion, const m_str path, const loc_t pos) { - struct td_checker tdc = { .str=path, .pos=pos }; + struct td_checker tdc = {.str = path, .pos = pos}; return _str2sym(gwion, &tdc, path); } @@ -78,48 +77,47 @@ ANN ID_List str2symlist(const Gwion gwion, const m_str path, const loc_t pos) { } ANN Var_Decl str2var(const Gwion gwion, const m_str path, const loc_t pos) { - struct td_checker tdc = { .str=path, .pos=pos }; + struct td_checker tdc = {.str = path, .pos = pos}; DECL_OO(const Symbol, sym, = __str2sym(gwion, &tdc)); - struct AC ac = { .str = tdc.str, .pos=pos }; + struct AC ac = {.str = tdc.str, .pos = pos}; CHECK_BO(ac_run(gwion, &ac)); - const Array_Sub array = ac.depth ? - mk_array(gwion->mp, &ac) : NULL; + const Array_Sub array = ac.depth ? mk_array(gwion->mp, &ac) : NULL; return new_var_decl(gwion->mp, sym, array, pos); } // only in udef.c -ANN Var_Decl_List str2varlist(const Gwion gwion, const m_str path, const loc_t pos) { +ANN Var_Decl_List str2varlist(const Gwion gwion, const m_str path, + const loc_t pos) { DECL_OO(const Var_Decl, var, = str2var(gwion, path, pos)); return new_var_decl_list(gwion->mp, var, NULL); } -#define SPEC_ERROR (Specialized_List)GW_ERROR -ANN static Specialized_List _tmpl_list(const Gwion gwion, struct td_checker *tdc) { +#define SPEC_ERROR (Specialized_List) GW_ERROR +ANN static Specialized_List _tmpl_list(const Gwion gwion, + struct td_checker *tdc) { DECL_OO(const Symbol, sym, = __str2sym(gwion, tdc)); Specialized_List next = NULL; - if(*tdc->str == ',') { + if (*tdc->str == ',') { ++tdc->str; - if(!(next = _tmpl_list(gwion, tdc)) || next == SPEC_ERROR) + if (!(next = _tmpl_list(gwion, tdc)) || next == SPEC_ERROR) return SPEC_ERROR; } // TODO: handle traits? - const Specialized_List list = new_specialized_list(gwion->mp, sym, NULL, tdc->pos); + const Specialized_List list = + new_specialized_list(gwion->mp, sym, NULL, tdc->pos); list->next = next; return list; } -ANN static Specialized_List __tmpl_list(const Gwion gwion, struct td_checker *tdc) { - if(tdc->str[0] != ':') - return NULL; - if(tdc->str[1] != '[') - return SPEC_ERROR; +ANN static Specialized_List __tmpl_list(const Gwion gwion, + struct td_checker *tdc) { + if (tdc->str[0] != ':') return NULL; + if (tdc->str[1] != '[') return SPEC_ERROR; tdc->str += 2; - const Specialized_List list = _tmpl_list(gwion, tdc); - if(list == SPEC_ERROR) - return SPEC_ERROR; - if(tdc->str[0] != ']') { // unfinished template - if(list) - free_specialized_list(gwion->mp, list); + const Specialized_List list = _tmpl_list(gwion, tdc); + if (list == SPEC_ERROR) return SPEC_ERROR; + if (tdc->str[0] != ']') { // unfinished template + if (list) free_specialized_list(gwion->mp, list); return SPEC_ERROR; } ++tdc->str; @@ -127,27 +125,23 @@ ANN static Specialized_List __tmpl_list(const Gwion gwion, struct td_checker *td } ANN m_bool check_typename_def(const Gwi gwi, ImportCK *ck) { - struct td_checker tdc = { .str= ck->name, .pos=gwi->loc }; - if(!(ck->sym = _str2sym(gwi->gwion, &tdc, tdc.str))) - return GW_ERROR; + struct td_checker tdc = {.str = ck->name, .pos = gwi->loc}; + if (!(ck->sym = _str2sym(gwi->gwion, &tdc, tdc.str))) return GW_ERROR; Specialized_List sl = __tmpl_list(gwi->gwion, &tdc); - if(sl == SPEC_ERROR) - return GW_ERROR; - ck->sl = sl; + if (sl == SPEC_ERROR) return GW_ERROR; + ck->sl = sl; ck->name = s_name(ck->sym); return GW_OK; - } -ANN static Type_Decl* _str2td(const Gwion gwion, struct td_checker *tdc); -ANN Type_List __str2tl(const Gwion gwion, struct td_checker *tdc) { +ANN static Type_Decl *_str2td(const Gwion gwion, struct td_checker *tdc); +ANN Type_List __str2tl(const Gwion gwion, struct td_checker *tdc) { Type_Decl *td = _str2td(gwion, tdc); - if(!td) - GWION_ERR_O(tdc->pos, "invalid types"); + if (!td) GWION_ERR_O(tdc->pos, "invalid types"); Type_List next = NULL; - if(*tdc->str == ',') { + if (*tdc->str == ',') { ++tdc->str; - if(!(next = __str2tl(gwion, tdc))) { + if (!(next = __str2tl(gwion, tdc))) { free_type_decl(gwion->mp, td); return NULL; } @@ -156,18 +150,16 @@ ANN Type_List __str2tl(const Gwion gwion, struct td_checker *tdc) { } ANN static Type_List td_tmpl(const Gwion gwion, struct td_checker *tdc) { - if(*tdc->str != ':') - return NULL; // GW_PASS + if (*tdc->str != ':') return NULL; // GW_PASS ++tdc->str; - if(*tdc->str != '[') { + if (*tdc->str != '[') { GWION_ERR(tdc->pos, "invalid character"); return (Type_List)GW_ERROR; } ++tdc->str; Type_List tl = __str2tl(gwion, tdc); - if(!tl) - return (Type_List)GW_ERROR; - if(tdc->str[0] != ']') { + if (!tl) return (Type_List)GW_ERROR; + if (tdc->str[0] != ']') { free_type_list(gwion->mp, tl); GWION_ERR(tdc->pos, "unfinished template"); return (Type_List)GW_ERROR; @@ -178,48 +170,44 @@ ANN static Type_List td_tmpl(const Gwion gwion, struct td_checker *tdc) { ANN static inline uint get_n(struct td_checker *tdc, const char c) { uint n = 0; - while(*tdc->str == c) { + while (*tdc->str == c) { n++; ++tdc->str; } return n; } -ANN static Type_Decl* _str2td(const Gwion gwion, struct td_checker *tdc) { +ANN static Type_Decl *_str2td(const Gwion gwion, struct td_checker *tdc) { const uint ref = get_n(tdc, '&'); DECL_OO(const Symbol, sym, = __str2sym(gwion, tdc)); - struct AC ac = { .str = tdc->str, .pos=tdc->pos }; + struct AC ac = {.str = tdc->str, .pos = tdc->pos}; CHECK_BO(ac_run(gwion, &ac)); - tdc->str = ac.str; + tdc->str = ac.str; Type_List tl = td_tmpl(gwion, tdc); - if(tl == (Type_List)GW_ERROR) - return NULL; + if (tl == (Type_List)GW_ERROR) return NULL; const uint option = get_n(tdc, '?'); - Type_Decl *next = NULL; - if(*tdc->str == '.') { + Type_Decl *next = NULL; + if (*tdc->str == '.') { ++tdc->str; - if(!(next = _str2td(gwion, tdc))) { - if(tl) - free_type_list(gwion->mp, tl); - if(ac.base) - free_exp(gwion->mp, ac.base); + if (!(next = _str2td(gwion, tdc))) { + if (tl) free_type_list(gwion->mp, tl); + if (ac.base) free_exp(gwion->mp, ac.base); return NULL; } } Type_Decl *td = new_type_decl(gwion->mp, sym, tdc->pos); - td->next = next; - td->types = tl; - td->option = option; - td->ref = ref; - if(ac.depth) - td->array = mk_array(gwion->mp, &ac); + td->next = next; + td->types = tl; + td->option = option; + td->ref = ref; + if (ac.depth) td->array = mk_array(gwion->mp, &ac); return td; } -ANN Type_Decl* str2td(const Gwion gwion, const m_str str, const loc_t pos) { - struct td_checker tdc = { .str=str, .pos=pos }; +ANN Type_Decl *str2td(const Gwion gwion, const m_str str, const loc_t pos) { + struct td_checker tdc = {.str = str, .pos = pos}; DECL_OO(Type_Decl *, td, = _str2td(gwion, &tdc)); - if(*tdc.str) { + if (*tdc.str) { free_type_decl(gwion->mp, td); GWION_ERR_O(pos, "excedental character '%c'", *tdc.str); } @@ -235,33 +223,33 @@ ANN Type str2type(const Gwion gwion, const m_str str, const loc_t pos) { struct td_info { Type_List tl; - GwText text; + GwText text; }; ANN static void td_fullname(const Env env, GwText *text, const Type t) { const Type owner = t->info->value->from->owner_class; - if(owner) { + if (owner) { td_fullname(env, text, owner); text_add(text, "."); } text_add(text, t->name); } -ANN static m_bool td_info_run(const Env env, struct td_info* info) { +ANN static m_bool td_info_run(const Env env, struct td_info *info) { Type_List tl = info->tl; do { - DECL_OB(const Type, t, = known_type(env, tl->td)); + DECL_OB(const Type, t, = known_type(env, tl->td)); td_fullname(env, &info->text, t); - if(tl->next) - text_add(&info->text, ","); - } while((tl = tl->next)); + if (tl->next) text_add(&info->text, ","); + } while ((tl = tl->next)); return GW_OK; } -ANEW ANN m_str type2str(const Gwion gwion, const Type t, const loc_t pos NUSED) { - GwText text = { .mp=gwion->mp }; +ANEW ANN m_str type2str(const Gwion gwion, const Type t, + const loc_t pos NUSED) { + GwText text = {.mp = gwion->mp}; const Type owner = t->info->value->from->owner_class; - if(owner) { + if (owner) { td_fullname(gwion->env, &text, owner); text_add(&text, "."); } @@ -269,40 +257,38 @@ ANEW ANN m_str type2str(const Gwion gwion, const Type t, const loc_t pos NUSED) return text.str; } -ANEW ANN m_str tl2str(const Gwion gwion, const Type_List tl, const loc_t pos NUSED) { - struct td_info info = { .tl=tl, { .mp=gwion->mp} }; +ANEW ANN m_str tl2str(const Gwion gwion, const Type_List tl, + const loc_t pos NUSED) { + struct td_info info = {.tl = tl, {.mp = gwion->mp}}; CHECK_BO(td_info_run(gwion->env, &info)); return info.text.str; } ANN static inline m_bool ac_finish(const Gwion gwion, const struct AC *ac) { - if(*ac->str == ']') - return GW_OK; + if (*ac->str == ']') return GW_OK; GWION_ERR_B(ac->pos, "unfinished array"); } -ANN static inline m_bool ac_num(const Gwion gwion, const struct AC *ac, const m_int num) { - if(num >= 0) - return GW_OK; +ANN static inline m_bool ac_num(const Gwion gwion, const struct AC *ac, + const m_int num) { + if (num >= 0) return GW_OK; GWION_ERR_B(ac->pos, "negative array dimension") } ANN static inline m_bool ac_exp(const Gwion gwion, const struct AC *ac) { - if(!ac->depth || ac->base) - return GW_OK; + if (!ac->depth || ac->base) return GW_OK; GWION_ERR_B(ac->pos, "malformed array [][...]") } ANN static void ac_add_exp(struct AC *ac, const Exp exp) { - if(ac->exp) + if (ac->exp) ac->exp = (ac->exp->next = exp); else ac->base = ac->exp = exp; } ANN static inline m_bool ac_noexp(const Gwion gwion, struct AC *ac) { - if(!ac->exp) - return GW_OK; + if (!ac->exp) return GW_OK; GWION_ERR_B(ac->pos, "malformed array [...][]") } @@ -310,7 +296,7 @@ ANN static m_bool _ac_run(const Gwion gwion, struct AC *const ac) { const m_str str = ac->str; const m_int num = strtol(str, &ac->str, 10); CHECK_BB(ac_finish(gwion, ac)); - if(str != ac->str) { + if (str != ac->str) { CHECK_BB(ac_num(gwion, ac, num)); CHECK_BB(ac_exp(gwion, ac)); const Exp exp = new_prim_int(gwion->mp, num, ac->pos); @@ -324,9 +310,8 @@ ANN static m_bool _ac_run(const Gwion gwion, struct AC *const ac) { } ANN static m_bool ac_run(const Gwion gwion, struct AC *const ac) { - while(*ac->str) { - if(*ac->str != '[') - break; + while (*ac->str) { + if (*ac->str != '[') break; ++ac->str; CHECK_BB(_ac_run(gwion, ac)); ++ac->depth; diff --git a/src/import/import_enum.c b/src/import/import_enum.c index 499d61a0..df1d549e 100644 --- a/src/import/import_enum.c +++ b/src/import/import_enum.c @@ -20,16 +20,15 @@ //! why is return type m_int ? ANN2(1) m_int gwi_enum_ini(const Gwi gwi, const m_str type) { CHECK_BB(ck_ini(gwi, ck_edef)); - if(type) - CHECK_OB((gwi->ck->xid = gwi_str2sym(gwi, type))); + if (type) CHECK_OB((gwi->ck->xid = gwi_str2sym(gwi, type))); vector_init(&gwi->ck->v); return GW_OK; } // adds the id_list to the enum // change that algo? -ANN static void add2list(struct ImportCK* ck, const ID_List list) { - if(!ck->tmpl) +ANN static void add2list(struct ImportCK *ck, const ID_List list) { + if (!ck->tmpl) ck->tmpl = list; else ck->curr->next = list; @@ -61,11 +60,11 @@ ANN m_int gwi_enum_add(const Gwi gwi, const m_str name, const m_uint i) { //! \note [internal] ANN static void import_enum_end(const Gwi gwi, const Vector v) { ImportCK *ck = gwi->ck; - for(m_uint i = 0; i < vector_size(v); i++) { - const Value value = (Value)vector_at(v, i); - const m_uint addr = vector_at(&ck->v, i); + for (m_uint i = 0; i < vector_size(v); i++) { + const Value value = (Value)vector_at(v, i); + const m_uint addr = vector_at(&ck->v, i); set_vflag(value, vflag_builtin); - value->d.num = addr ?: i; + value->d.num = addr ?: i; } // better clean ? } @@ -75,22 +74,19 @@ ANN static void import_enum_end(const Gwi gwi, const Vector v) { //! TODO: check what happens in inside template class ANN Type gwi_enum_end(const Gwi gwi) { CHECK_BO(ck_ok(gwi, ck_edef)); - if(!vector_size(&gwi->ck->v)) - GWI_ERR_O("Enum is empty"); - const Gwion gwion = gwi->gwion; - const Enum_Def edef = new_enum_def(gwion->mp, gwi->ck->tmpl, - gwi->ck->xid, gwi->loc); - gwi->ck->tmpl = NULL; + if (!vector_size(&gwi->ck->v)) GWI_ERR_O("Enum is empty"); + const Gwion gwion = gwi->gwion; + const Enum_Def edef = + new_enum_def(gwion->mp, gwi->ck->tmpl, gwi->ck->xid, gwi->loc); + gwi->ck->tmpl = NULL; const m_bool ret = traverse_enum_def(gwion->env, edef); - if(ret > 0) - import_enum_end(gwi, &edef->values); - if(gwi->gwion->data->cdoc) { + if (ret > 0) import_enum_end(gwi, &edef->values); + if (gwi->gwion->data->cdoc) { lint_indent(gwi->lint); lint_enum_def(gwi->lint, edef); } const Type t = ret > 0 ? edef->t : NULL; - if(edef->values.ptr) - vector_release(&edef->values); + if (edef->values.ptr) vector_release(&edef->values); free_enum_def(gwion->mp, edef); vector_release(&gwi->ck->v); gwi->ck->v.ptr = NULL; @@ -99,14 +95,13 @@ ANN Type gwi_enum_end(const Gwi gwi) { } ANN void ck_clean_edef(MemPool mp, ImportCK *ck) { -/* - if(ck->tmpl) - free_id_list(mp, ck->tmpl); - if(ck->v.ptr) { - for(m_uint i = 0; i < vector_size(&ck->v); ++i) - mp_free2(mp, SZ_INT, (m_uint*)vector_at(&ck->v, i)); - vector_release(&ck->v); - } -*/ + /* + if(ck->tmpl) + free_id_list(mp, ck->tmpl); + if(ck->v.ptr) { + for(m_uint i = 0; i < vector_size(&ck->v); ++i) + mp_free2(mp, SZ_INT, (m_uint*)vector_at(&ck->v, i)); + vector_release(&ck->v); + } + */ } - diff --git a/src/import/import_fdef.c b/src/import/import_fdef.c index 42f8baab..4dbcc173 100644 --- a/src/import/import_fdef.c +++ b/src/import/import_fdef.c @@ -15,8 +15,9 @@ #include "gwi.h" #include "clean.h" -ANN2(1,2,3) static m_bool dl_func_init(const Gwi gwi, const restrict m_str t, - const restrict m_str n) { +ANN2(1, 2, 3) +static m_bool dl_func_init(const Gwi gwi, const restrict m_str t, + const restrict m_str n) { CHECK_BB(ck_ini(gwi, ck_fdef)); gwi->ck->name = n; CHECK_BB(check_typename_def(gwi, gwi->ck)); @@ -25,40 +26,41 @@ ANN2(1,2,3) static m_bool dl_func_init(const Gwi gwi, const restrict m_str t, return GW_OK; } -ANN m_int gwi_func_ini(const Gwi gwi, const restrict m_str t, const restrict m_str n) { +ANN m_int gwi_func_ini(const Gwi gwi, const restrict m_str t, + const restrict m_str n) { return dl_func_init(gwi, t, n); } ANN Arg_List make_dll_arg_list(const Vector v) { Arg_List base = (Arg_List)vector_front(v), arg_list = base; - for(m_uint i = 1; i < vector_size(v); ++i) + for (m_uint i = 1; i < vector_size(v); ++i) arg_list = (arg_list->next = (Arg_List)vector_at(v, i)); vector_release(v); v->ptr = NULL; return base; } -ANEW ANN static Func_Base* gwi_func_base(const Gwi gwi, ImportCK *ck) { +ANEW ANN static Func_Base *gwi_func_base(const Gwi gwi, ImportCK *ck) { const Arg_List arg_list = make_dll_arg_list(&gwi->ck->v); - Func_Base *base = new_func_base(gwi->gwion->mp, ck->td, ck->sym, arg_list, ck->flag, gwi->loc); - if(ck->variadic) - base->fbflag |= fbflag_variadic; + Func_Base * base = new_func_base(gwi->gwion->mp, ck->td, ck->sym, arg_list, + ck->flag, gwi->loc); + if (ck->variadic) base->fbflag |= fbflag_variadic; ck->td = NULL; - if(ck->tmpl) { + if (ck->tmpl) { base->tmpl = gwi_tmpl(gwi); - ck->tmpl = NULL; + ck->tmpl = NULL; } return base; } ANEW ANN static Func_Def import_fdef(const Gwi gwi, ImportCK *ck) { - Func_Base* base = gwi_func_base(gwi, ck); + Func_Base * base = gwi_func_base(gwi, ck); const Func_Def fdef = new_func_def(gwi->gwion->mp, base, NULL); - if(gwi->gwion->data->cdoc) { + if (gwi->gwion->data->cdoc) { lint_indent(gwi->lint); lint_func_def(gwi->lint, fdef); } - if(gwi->effects.ptr) { + if (gwi->effects.ptr) { vector_init(&fdef->base->effects); vector_copy2(&gwi->effects, &fdef->base->effects); vector_release(&gwi->effects); @@ -67,8 +69,8 @@ ANEW ANN static Func_Def import_fdef(const Gwi gwi, ImportCK *ck) { } ANN static m_bool section_fdef(const Gwi gwi, const Func_Def fdef) { - Section* section = new_section_func_def(gwi->gwion->mp, fdef); - const Ast body = new_ast(gwi->gwion->mp, section, NULL); + Section * section = new_section_func_def(gwi->gwion->mp, fdef); + const Ast body = new_ast(gwi->gwion->mp, section, NULL); gwi_body(gwi, body); return GW_OK; } @@ -80,9 +82,9 @@ ANN static m_bool error_fdef(const Gwi gwi, const Func_Def fdef) { ANN m_int gwi_func_valid(const Gwi gwi, ImportCK *ck) { const Func_Def fdef = import_fdef(gwi, ck); - if(safe_tflag(gwi->gwion->env->class_def, tflag_tmpl)) + if (safe_tflag(gwi->gwion->env->class_def, tflag_tmpl)) return section_fdef(gwi, fdef); - if(traverse_func_def(gwi->gwion->env, fdef) < 0) + if (traverse_func_def(gwi->gwion->env, fdef) < 0) return error_fdef(gwi, fdef); builtin_func(gwi->gwion->mp, fdef->base->func, ck->addr); return GW_OK; @@ -90,24 +92,24 @@ ANN m_int gwi_func_valid(const Gwi gwi, ImportCK *ck) { ANN m_int gwi_func_end(const Gwi gwi, const f_xfun addr, const ae_flag flag) { CHECK_BB(ck_ok(gwi, ck_fdef)); - gwi->ck->addr = addr; - gwi->ck->flag = flag; + gwi->ck->addr = addr; + gwi->ck->flag = flag; const m_bool ret = gwi_func_valid(gwi, gwi->ck); ck_end(gwi); return ret; } -ANN m_int gwi_func_arg(const Gwi gwi, const restrict m_str t, const restrict m_str n) { +ANN m_int gwi_func_arg(const Gwi gwi, const restrict m_str t, + const restrict m_str n) { CHECK_BB(ck_ok(gwi, ck_fdef)); - if(gwi->ck->variadic) - GWI_ERR_B(_("already declared as variadic")); - if(!strcmp(n, "...")) { + if (gwi->ck->variadic) GWI_ERR_B(_("already declared as variadic")); + if (!strcmp(n, "...")) { gwi->ck->variadic = 1; return GW_OK; } - DECL_OB(Type_Decl*, td, = gwi_str2td(gwi, t)); + DECL_OB(Type_Decl *, td, = gwi_str2td(gwi, t)); const Var_Decl var = gwi_str2var(gwi, n); - if(var) { + if (var) { const Arg_List arg = new_arg_list(gwi->gwion->mp, td, var, NULL); vector_add(&gwi->ck->v, (vtype)arg); return GW_OK; @@ -116,7 +118,8 @@ ANN m_int gwi_func_arg(const Gwi gwi, const restrict m_str t, const restrict m_s return GW_ERROR; } -ANN m_int gwi_fptr_ini(const Gwi gwi, const restrict m_str type, const restrict m_str name) { +ANN m_int gwi_fptr_ini(const Gwi gwi, const restrict m_str type, + const restrict m_str name) { return dl_func_init(gwi, type, name); } @@ -126,8 +129,8 @@ ANN static Fptr_Def import_fptr(const Gwi gwi) { } ANN static m_bool section_fptr(const Gwi gwi, const Fptr_Def fdef) { - Section* section = new_section_fptr_def(gwi->gwion->mp, fdef); - const Ast body = new_ast(gwi->gwion->mp, section, NULL); + Section * section = new_section_fptr_def(gwi->gwion->mp, fdef); + const Ast body = new_ast(gwi->gwion->mp, section, NULL); gwi_body(gwi, body); return GW_OK; } @@ -136,37 +139,35 @@ ANN Type gwi_fptr_end(const Gwi gwi, const ae_flag flag) { CHECK_BO(ck_ok(gwi, ck_fdef)); DECL_OO(const Fptr_Def, fptr, = import_fptr(gwi)); fptr->base->flag |= flag; - if(gwi->gwion->data->cdoc) { + if (gwi->gwion->data->cdoc) { lint_indent(gwi->lint); lint_fptr_def(gwi->lint, fptr); } - if(safe_tflag(gwi->gwion->env->class_def, tflag_tmpl)/* && !fptr->base->tmpl*/) { + if (safe_tflag(gwi->gwion->env->class_def, + tflag_tmpl) /* && !fptr->base->tmpl*/) { section_fptr(gwi, fptr); ck_end(gwi); return (Type)GW_OK; } const m_bool ret = traverse_fptr_def(gwi->gwion->env, fptr); - if(fptr->base->func) // is it needed ? + if (fptr->base->func) // is it needed ? set_vflag(fptr->base->func->value_ref, vflag_builtin); const Type t = ret > 0 ? fptr->type : NULL; free_fptr_def(gwi->gwion->mp, fptr); - if(fptr->type) - type_remref(fptr->type, gwi->gwion); + if (fptr->type) type_remref(fptr->type, gwi->gwion); ck_end(gwi); return t; } ANN void ck_clean_fdef(MemPool mp, ImportCK *ck) { - if(ck->td) - free_type_decl(mp, ck->td); - if(ck->v.ptr) { - for(m_uint i = 0; i < vector_size(&ck->v); ++i) { + if (ck->td) free_type_decl(mp, ck->td); + if (ck->v.ptr) { + for (m_uint i = 0; i < vector_size(&ck->v); ++i) { Arg_List list = (Arg_List)vector_at(&ck->v, i); - list->next = NULL; + list->next = NULL; free_arg_list(mp, list); } vector_release(&ck->v); } - if(ck->tmpl) - free_id_list(mp, ck->tmpl); + if (ck->tmpl) free_id_list(mp, ck->tmpl); } diff --git a/src/import/import_internals.c b/src/import/import_internals.c index 93f2587c..49377553 100644 --- a/src/import/import_internals.c +++ b/src/import/import_internals.c @@ -12,7 +12,7 @@ void gwi_body(const Gwi gwi, const Ast body) { const Class_Def cdef = gwi->gwion->env->class_def->info->cdef; - if(!cdef->body) + if (!cdef->body) cdef->body = body; else gwi->body->next = body; @@ -20,7 +20,7 @@ void gwi_body(const Gwi gwi, const Ast body) { } ANN void gwi_reset(const Gwi gwi) { - if(gwi->ck) { + if (gwi->ck) { ck_clean(gwi); mp_free2(gwi->gwion->mp, sizeof(ImportCK), gwi->ck); gwi->ck = NULL; @@ -29,8 +29,8 @@ ANN void gwi_reset(const Gwi gwi) { } ANN static m_bool run_with_doc(const Gwi gwi, m_bool (*f)(const Gwi)) { - struct LintState ls = { .builtin=true }; - Lint linter = { .mp=gwi->gwion->mp, .ls=&ls }; + struct LintState ls = {.builtin = true}; + Lint linter = {.mp = gwi->gwion->mp, .ls = &ls}; lint(&linter, "{-}#!+ %s{0}\n", gwi->gwion->env->name); gwi->lint = &linter; return f(gwi); @@ -38,15 +38,13 @@ ANN static m_bool run_with_doc(const Gwi gwi, m_bool (*f)(const Gwi)) { ANN m_bool gwi_run(const Gwion gwion, m_bool (*f)(const Gwi)) { const m_str name = gwion->env->name; -// const Context ctx = gwion->env->context; -// gwion->env->context = NULL; - OperCK oper = {}; - struct Gwi_ gwi = { .gwion=gwion, .oper=&oper }; - const m_bool ret = !gwion->data->cdoc ? - f(&gwi) : run_with_doc(&gwi, f); - if(ret < 0) - gwi_reset(&gwi); + // const Context ctx = gwion->env->context; + // gwion->env->context = NULL; + OperCK oper = {}; + struct Gwi_ gwi = {.gwion = gwion, .oper = &oper}; + const m_bool ret = !gwion->data->cdoc ? f(&gwi) : run_with_doc(&gwi, f); + if (ret < 0) gwi_reset(&gwi); gwion->env->name = name; -// gwion->env->context = ctx; + // gwion->env->context = ctx; return ret; } diff --git a/src/import/import_item.c b/src/import/import_item.c index f5c29824..2d9bd42b 100644 --- a/src/import/import_item.c +++ b/src/import/import_item.c @@ -10,19 +10,19 @@ #include "import.h" #include "gwi.h" -ANN m_int gwi_item_ini(const Gwi gwi, const restrict m_str type, const restrict m_str name) { +ANN m_int gwi_item_ini(const Gwi gwi, const restrict m_str type, + const restrict m_str name) { CHECK_BB(ck_ini(gwi, ck_item)); - if((gwi->ck->exp = make_exp(gwi, type, name))) - return GW_OK; + if ((gwi->ck->exp = make_exp(gwi, type, name))) return GW_OK; GWI_ERR_B(_(" ... during var import '%s.%s'."), gwi->gwion->env->name, name) } ANN static m_int gwi_item_tmpl(const Gwi gwi) { - const MemPool mp = gwi->gwion->mp; - const Stmt stmt = new_stmt_exp(mp, ae_stmt_exp, gwi->ck->exp, gwi->loc); + const MemPool mp = gwi->gwion->mp; + const Stmt stmt = new_stmt_exp(mp, ae_stmt_exp, gwi->ck->exp, gwi->loc); const Stmt_List slist = new_stmt_list(mp, stmt, NULL); - Section* section = new_section_stmt_list(mp, slist); - const Ast body = new_ast(mp, section, NULL); + Section * section = new_section_stmt_list(mp, slist); + const Ast body = new_ast(mp, section, NULL); gwi_body(gwi, body); mp_free2(gwi->gwion->mp, sizeof(ImportCK), gwi->ck); gwi->ck = NULL; @@ -30,31 +30,30 @@ ANN static m_int gwi_item_tmpl(const Gwi gwi) { } #undef gwi_item_end -ANN2(1) m_int gwi_item_end(const Gwi gwi, const ae_flag flag, union value_data addr) { +ANN2(1) +m_int gwi_item_end(const Gwi gwi, const ae_flag flag, union value_data addr) { CHECK_BB(ck_ok(gwi, ck_item)); - const Env env = gwi->gwion->env; + const Env env = gwi->gwion->env; gwi->ck->exp->d.exp_decl.td->flag = flag; - if(gwi->gwion->data->cdoc) { + if (gwi->gwion->data->cdoc) { lint_indent(gwi->lint); lint_exp(gwi->lint, gwi->ck->exp); lint_sc(gwi->lint); lint_nl(gwi->lint); } - if(env->class_def && tflag(env->class_def, tflag_tmpl)) + if (env->class_def && tflag(env->class_def, tflag_tmpl)) return gwi_item_tmpl(gwi); CHECK_BB(traverse_exp(env, gwi->ck->exp)); const Value value = gwi->ck->exp->d.exp_decl.list->self->value; - value->d = addr; + value->d = addr; set_vflag(value, vflag_builtin); - if(!env->class_def) - SET_FLAG(value, global); + if (!env->class_def) SET_FLAG(value, global); const m_uint offset = value->from->offset; free_exp(gwi->gwion->mp, gwi->ck->exp); ck_end(gwi); return offset; } -ANN void ck_clean_item(MemPool mp, ImportCK* ck) { - if(ck->exp) - free_exp(mp, ck->exp); +ANN void ck_clean_item(MemPool mp, ImportCK *ck) { + if (ck->exp) free_exp(mp, ck->exp); } diff --git a/src/import/import_oper.c b/src/import/import_oper.c index 9dea3fa1..970a1003 100644 --- a/src/import/import_oper.c +++ b/src/import/import_oper.c @@ -17,11 +17,10 @@ #include "specialid.h" ANN static Type _get_type(const Gwi gwi, const m_str s) { - if(s == (m_str)OP_ANY_TYPE) - return OP_ANY_TYPE; + if (s == (m_str)OP_ANY_TYPE) return OP_ANY_TYPE; // str2type Type_Decl *td = gwi_str2td(gwi, s); - const Type t = known_type(gwi->gwion->env, td); + const Type t = known_type(gwi->gwion->env, td); free_type_decl(gwi->gwion->mp, td); return t; } @@ -30,29 +29,34 @@ ANN2(1) static inline Type gwi_get_type(const Gwi gwi, const m_str str) { return str ? _get_type(gwi, str) : NULL; } -ANN2(1,2) static int import_op(const Gwi gwi, struct OperCK*const op, - const f_instr f) { - const Type lhs = gwi_get_type(gwi, op->lhs), - rhs = gwi_get_type(gwi, op->rhs), - ret = gwi_get_type(gwi, op->ret); - const struct Op_Func opfunc = { .ck=op->ck, .em=op->em, .effect = { .ptr=op->effect.ptr } }; - const struct Op_Import opi = { .lhs=lhs, .rhs=rhs, .ret=ret, - .func=&opfunc, .data=(uintptr_t)f, .pos=gwi->loc, .op=op->sym }; - const m_bool b = add_op(gwi->gwion, &opi); - op->effect.ptr = NULL; +ANN2(1, 2) +static int import_op(const Gwi gwi, struct OperCK *const op, const f_instr f) { + const Type lhs = gwi_get_type(gwi, op->lhs), rhs = gwi_get_type(gwi, op->rhs), + ret = gwi_get_type(gwi, op->ret); + const struct Op_Func opfunc = { + .ck = op->ck, .em = op->em, .effect = {.ptr = op->effect.ptr}}; + const struct Op_Import opi = {.lhs = lhs, + .rhs = rhs, + .ret = ret, + .func = &opfunc, + .data = (uintptr_t)f, + .pos = gwi->loc, + .op = op->sym}; + const m_bool b = add_op(gwi->gwion, &opi); + op->effect.ptr = NULL; return b; } - -ANN2(1) m_int gwi_oper_ini(const Gwi gwi, const restrict m_str l, - const restrict m_str r, const restrict m_str t) { +ANN2(1) +m_int gwi_oper_ini(const Gwi gwi, const restrict m_str l, + const restrict m_str r, const restrict m_str t) { gwi->oper->ret = t; gwi->oper->rhs = r; gwi->oper->lhs = l; return GW_OK; } -ANN m_int gwi_oper_add(const Gwi gwi, Type (*ck)(Env, void*)) { +ANN m_int gwi_oper_add(const Gwi gwi, Type (*ck)(Env, void *)) { gwi->oper->ck = ck; return GW_OK; } @@ -63,45 +67,48 @@ ANN m_int gwi_oper_emi(const Gwi gwi, const opem em) { } ANN void gwi_oper_eff(const Gwi gwi, const m_str effect) { - if(!gwi->oper->effect.ptr) - vector_init(&gwi->oper->effect); + if (!gwi->oper->effect.ptr) vector_init(&gwi->oper->effect); vector_add(&gwi->oper->effect, (m_uint)insert_symbol(gwi->gwion->st, effect)); } ANN m_int gwi_oper_end(const Gwi gwi, const m_str op, const f_instr f) { - if(gwi->gwion->data->cdoc) { + if (gwi->gwion->data->cdoc) { lint_indent(gwi->lint); lint(gwi->lint, "{+C}operator{0} "); - if(gwi->oper->lhs && !gwi->oper->rhs) { - lint(gwi->lint, "{+}%s{0}", gwi->oper->lhs != (m_str)1 ? gwi->oper->lhs : "@Any"); + if (gwi->oper->lhs && !gwi->oper->rhs) { + lint(gwi->lint, "{+}%s{0}", + gwi->oper->lhs != (m_str)1 ? gwi->oper->lhs : "@Any"); lint_space(gwi->lint); } - if(gwi->oper->ret) { - lint(gwi->lint, "{+}%s{0}", gwi->oper->ret != (m_str)1 ? gwi->oper->ret : "@Any"); + if (gwi->oper->ret) { + lint(gwi->lint, "{+}%s{0}", + gwi->oper->ret != (m_str)1 ? gwi->oper->ret : "@Any"); lint_space(gwi->lint); } lint(gwi->lint, "{/}%s{0}", op); lint_lparen(gwi->lint); - if(gwi->oper->lhs && gwi->oper->rhs) { - lint(gwi->lint, "{+}%s{0}", gwi->oper->lhs != (m_str)1 ? gwi->oper->lhs : "@Any"); + if (gwi->oper->lhs && gwi->oper->rhs) { + lint(gwi->lint, "{+}%s{0}", + gwi->oper->lhs != (m_str)1 ? gwi->oper->lhs : "@Any"); lint_comma(gwi->lint); lint_space(gwi->lint); } - if(gwi->oper->rhs) - lint(gwi->lint, "{+}%s{0}", gwi->oper->rhs != (m_str)1 ? gwi->oper->rhs : "@Any"); + if (gwi->oper->rhs) + lint(gwi->lint, "{+}%s{0}", + gwi->oper->rhs != (m_str)1 ? gwi->oper->rhs : "@Any"); lint_rparen(gwi->lint); lint_sc(gwi->lint); lint_nl(gwi->lint); } - gwi->oper->sym = insert_symbol(gwi->gwion->st, op); + gwi->oper->sym = insert_symbol(gwi->gwion->st, op); const m_bool ret = import_op(gwi, gwi->oper, f); - gwi->oper->ck = NULL; - gwi->oper->em = NULL; + gwi->oper->ck = NULL; + gwi->oper->em = NULL; return ret; } -ANN m_int gwi_oper_cond(const Gwi gwi, const m_str type, - const f_instr f1, const f_instr f2) { +ANN m_int gwi_oper_cond(const Gwi gwi, const m_str type, const f_instr f1, + const f_instr f2) { GWI_BB(gwi_oper_ini(gwi, NULL, type, "bool")) GWI_BB(gwi_oper_end(gwi, "@conditional", f1)) GWI_BB(gwi_oper_end(gwi, "@unconditional", f2)) diff --git a/src/import/import_special.c b/src/import/import_special.c index c062929c..b57b2a21 100644 --- a/src/import/import_special.c +++ b/src/import/import_special.c @@ -17,34 +17,37 @@ #include "specialid.h" #include "pass.h" -ANN void gwi_register_freearg(const Gwi gwi, const f_instr _exec, const f_freearg _free) { +ANN void gwi_register_freearg(const Gwi gwi, const f_instr _exec, + const f_freearg _free) { map_set(&gwi->gwion->data->freearg, (vtype)_exec, (vtype)_free); } -ANN void gwi_register_pass(const Gwi gwi, const m_str name, const compilation_pass pass) { +ANN void gwi_register_pass(const Gwi gwi, const m_str name, + const compilation_pass pass) { pass_register(gwi->gwion, name, pass); } ANN void gwi_specialid(const Gwi gwi, const m_str id, const SpecialId spid) { - if(gwi->gwion->data->cdoc) + if (gwi->gwion->data->cdoc) lint(gwi->lint, "{+C}specialid{0} %s{/}%s{0};\n", - spid->is_const ? "{+G}const{0} " : "", - id); + spid->is_const ? "{+G}const{0} " : "", id); struct SpecialId_ *a = mp_calloc(gwi->gwion->mp, SpecialId); memcpy(a, spid, sizeof(struct SpecialId_)); - map_set(&gwi->gwion->data->id, (vtype)insert_symbol(gwi->gwion->st, id), (vtype)a); + map_set(&gwi->gwion->data->id, (vtype)insert_symbol(gwi->gwion->st, id), + (vtype)a); } ANN void gwi_set_loc(const Gwi gwi, const m_str file, const uint line) { gwi->loc.first.line = gwi->loc.last.line = line; - gwi->loc.first.column = 1; gwi->loc.last.column = 2; - gwi->gwion->env->name = file; + gwi->loc.first.column = 1; + gwi->loc.last.column = 2; + gwi->gwion->env->name = file; } ANN static m_bool mk_gack(MemPool p, const Type type, const f_gack d) { const VM_Code code = new_vmcode(p, NULL, SZ_INT, 1, "@gack"); - code->native_func = (m_uint)d; - type->info->gack = code; + code->native_func = (m_uint)d; + type->info->gack = code; return GW_OK; } @@ -52,13 +55,10 @@ ANN m_bool gwi_gack(const Gwi gwi, const Type type, const f_gack d) { return mk_gack(gwi->gwion->mp, type, d); } -ANN VM* gwi_vm(const Gwi gwi) { - return gwi->gwion->vm; -} +ANN VM *gwi_vm(const Gwi gwi) { return gwi->gwion->vm; } ANN void gwi_effects(const Gwi gwi, const m_str name) { - if(!gwi->effects.ptr) - vector_init(&gwi->effects); + if (!gwi->effects.ptr) vector_init(&gwi->effects); const Symbol sym = insert_symbol(gwi->gwion->st, name); vector_add(&gwi->effects, (m_uint)sym); } diff --git a/src/import/import_tdef.c b/src/import/import_tdef.c index f08323cc..ab4a4092 100644 --- a/src/import/import_tdef.c +++ b/src/import/import_tdef.c @@ -16,7 +16,8 @@ #include "mpool.h" #include "specialid.h" -ANN m_int gwi_typedef_ini(const Gwi gwi, const restrict m_str type, const restrict m_str name) { +ANN m_int gwi_typedef_ini(const Gwi gwi, const restrict m_str type, + const restrict m_str name) { CHECK_BB(ck_ini(gwi, ck_tdef)); gwi->ck->name = name; CHECK_BB(check_typename_def(gwi, gwi->ck)); @@ -27,27 +28,26 @@ ANN Type gwi_typedef_end(const Gwi gwi, const ae_flag flag) { CHECK_BO(ck_ok(gwi, ck_tdef)); Type_Decl *td = gwi->ck->td; td->flag |= flag; - const Type_Def tdef = new_type_def(gwi->gwion->mp, td, gwi->ck->sym, gwi->loc); - if(gwi->ck->tmpl) - tdef->tmpl = gwi_tmpl(gwi); - gwi->ck->td = NULL; - gwi->ck->tmpl = NULL; + const Type_Def tdef = + new_type_def(gwi->gwion->mp, td, gwi->ck->sym, gwi->loc); + if (gwi->ck->tmpl) tdef->tmpl = gwi_tmpl(gwi); + gwi->ck->td = NULL; + gwi->ck->tmpl = NULL; const m_bool ret = traverse_type_def(gwi->gwion->env, tdef); - if(gwi->gwion->data->cdoc) { + if (gwi->gwion->data->cdoc) { lint_indent(gwi->lint); lint_type_def(gwi->lint, tdef); } const Type t = tdef->type; - if(ret > 0) - set_tflag(t, tflag_scan0 | tflag_scan1 | tflag_scan2 | tflag_check | tflag_emit); + if (ret > 0) + set_tflag(t, tflag_scan0 | tflag_scan1 | tflag_scan2 | tflag_check | + tflag_emit); free_type_def(gwi->gwion->mp, tdef); ck_end(gwi); return ret > 0 ? t : NULL; } ANN void ck_clean_tdef(MemPool mp, ImportCK *ck) { - if(ck->td) - free_type_decl(mp, ck->td); - if(ck->tmpl) - free_id_list(mp, ck->tmpl); + if (ck->td) free_type_decl(mp, ck->td); + if (ck->tmpl) free_id_list(mp, ck->tmpl); } diff --git a/src/import/import_type.c b/src/import/import_type.c index 6237ea57..34fe9b8a 100644 --- a/src/import/import_type.c +++ b/src/import/import_type.c @@ -15,33 +15,33 @@ #include "gwi.h" ANN2(1) static Type get_parent(const Gwi gwi, const m_str parent_name) { - Type_Decl* td = parent_name ? gwi_str2td(gwi, parent_name) : NULL; - if(td) { - if(td->array || td->types) { + Type_Decl *td = parent_name ? gwi_str2td(gwi, parent_name) : NULL; + if (td) { + if (td->array || td->types) { const m_str str = td->array ? "array" : "template"; free_type_decl(gwi->gwion->mp, td); GWI_ERR_O(_("can't use gwi_mk_type to extend %s types"), str) } } const Type parent = td ? known_type(gwi->gwion->env, td) : NULL; - if(td) - free_type_decl(gwi->gwion->mp, td); + if (td) free_type_decl(gwi->gwion->mp, td); return parent; } -ANN2(1,2) Type gwi_mk_type(const Gwi gwi, const m_str name, const m_uint size, const m_str parent_name) { - if(gwi->gwion->data->cdoc) { +ANN2(1, 2) +Type gwi_mk_type(const Gwi gwi, const m_str name, const m_uint size, + const m_str parent_name) { + if (gwi->gwion->data->cdoc) { lint_indent(gwi->lint); lint(gwi->lint, "{+C}primitive{0} {+}%s{0}", name); - if(parent_name) - lint(gwi->lint, " {+C}extends{0} {+}%s{0}", parent_name); + if (parent_name) lint(gwi->lint, " {+C}extends{0} {+}%s{0}", parent_name); lint_sc(gwi->lint); lint_nl(gwi->lint); } CHECK_OO(gwi_str2sym(gwi, name)); const Type parent = get_parent(gwi, parent_name); - const Type t = new_type(gwi->gwion->mp, name, parent); - t->size = size; + const Type t = new_type(gwi->gwion->mp, name, parent); + t->size = size; return t; } @@ -49,7 +49,8 @@ ANN void gwi_add_type(const Gwi gwi, const Type type) { return env_add_type(gwi->gwion->env, type, gwi->loc); } -ANN void gwi_set_global_type(const Gwi gwi, const Type type, const type_enum te) { +ANN void gwi_set_global_type(const Gwi gwi, const Type type, + const type_enum te) { gwi->gwion->type[te] = type; gwi_add_type(gwi, type); } diff --git a/src/import/import_udef.c b/src/import/import_udef.c index fe3c74f4..97e89fa7 100644 --- a/src/import/import_udef.c +++ b/src/import/import_udef.c @@ -16,10 +16,9 @@ // move me ANN Exp make_exp(const Gwi gwi, const m_str type, const m_str name) { - DECL_OO(Type_Decl*, td, = gwi_str2td(gwi, type)); + DECL_OO(Type_Decl *, td, = gwi_str2td(gwi, type)); const Var_Decl_List vlist = gwi_str2varlist(gwi, name); - if(vlist) - return new_exp_decl(gwi->gwion->mp, td, vlist, gwi->loc); + if (vlist) return new_exp_decl(gwi->gwion->mp, td, vlist, gwi->loc); free_type_decl(gwi->gwion->mp, td); return NULL; } @@ -31,28 +30,29 @@ ANN m_int gwi_union_ini(const Gwi gwi, const m_str name) { return GW_OK; } -ANN m_int gwi_union_add(const Gwi gwi, const restrict m_str type, const restrict m_str name) { +ANN m_int gwi_union_add(const Gwi gwi, const restrict m_str type, + const restrict m_str name) { CHECK_BB(ck_ok(gwi, ck_udef)); - DECL_OB(Type_Decl*, td, = str2td(gwi->gwion, type, gwi->loc)); + DECL_OB(Type_Decl *, td, = str2td(gwi->gwion, type, gwi->loc)); DECL_OB(const Symbol, xid, = str2sym(gwi->gwion, name, gwi->loc)); const Union_List l = new_union_list(gwi->gwion->mp, td, xid, gwi->loc); - l->next = gwi->ck->list; - gwi->ck->list = l; + l->next = gwi->ck->list; + gwi->ck->list = l; return GW_OK; } ANN static Type union_type(const Gwi gwi, const Union_Def udef) { CHECK_BO(scan0_union_def(gwi->gwion->env, udef)); CHECK_BO(traverse_union_def(gwi->gwion->env, udef)); -// if(!udef->tmpl) -// emit_union_offset(udef->l, udef->o); -// if(gwi->gwion->env->class_def && !GET_FLAG(udef, static)) -// gwi->gwion->env->class_def->nspc->info->offset = -// udef->o + udef->s; -// set_vflag(udef->value, vflag_builtin); -// const M_Object o = new_object(gwi->gwion->mp, NULL, udef->value->type); -// udef->value->d.ptr = (m_uint*)o; - if(gwi->gwion->data->cdoc) { + // if(!udef->tmpl) + // emit_union_offset(udef->l, udef->o); + // if(gwi->gwion->env->class_def && !GET_FLAG(udef, static)) + // gwi->gwion->env->class_def->nspc->info->offset = + // udef->o + udef->s; + // set_vflag(udef->value, vflag_builtin); + // const M_Object o = new_object(gwi->gwion->mp, NULL, udef->value->type); + // udef->value->d.ptr = (m_uint*)o; + if (gwi->gwion->data->cdoc) { lint_indent(gwi->lint); lint_union_def(gwi->lint, udef); } @@ -61,26 +61,22 @@ ANN static Type union_type(const Gwi gwi, const Union_Def udef) { ANN Type gwi_union_end(const Gwi gwi, const ae_flag flag) { CHECK_BO(ck_ok(gwi, ck_udef)); - if(!gwi->ck->list) - GWI_ERR_O(_("union is empty")); + if (!gwi->ck->list) GWI_ERR_O(_("union is empty")); const Union_Def udef = new_union_def(gwi->gwion->mp, gwi->ck->list, gwi->loc); - gwi->ck->list = NULL; - udef->flag = flag; - udef->xid = gwi->ck->sym; - if(gwi->ck->tmpl) { - udef->tmpl = gwi_tmpl(gwi); + gwi->ck->list = NULL; + udef->flag = flag; + udef->xid = gwi->ck->sym; + if (gwi->ck->tmpl) { + udef->tmpl = gwi_tmpl(gwi); gwi->ck->tmpl = NULL; } const Type t = union_type(gwi, udef); - if(!safe_tflag(t, tflag_tmpl)) - free_union_def(gwi->gwion->mp, udef); + if (!safe_tflag(t, tflag_tmpl)) free_union_def(gwi->gwion->mp, udef); ck_end(gwi); return t; } -ANN void ck_clean_udef(MemPool mp, ImportCK* ck) { - if(ck->list) - free_union_list(mp, ck->list); - if(ck->tmpl) - free_id_list(mp, ck->tmpl); +ANN void ck_clean_udef(MemPool mp, ImportCK *ck) { + if (ck->list) free_union_list(mp, ck->list); + if (ck->tmpl) free_id_list(mp, ck->tmpl); } diff --git a/src/lib/array.c b/src/lib/array.c index 9da86cf5..c47f760a 100644 --- a/src/lib/array.c +++ b/src/lib/array.c @@ -17,174 +17,167 @@ #include "emit.h" static DTOR(array_dtor) { - if(*(void**)(o->data + SZ_INT)) - xfree(*(void**)(o->data + SZ_INT)); - struct M_Vector_* a = ARRAY(o); + if (*(void **)(o->data + SZ_INT)) xfree(*(void **)(o->data + SZ_INT)); + struct M_Vector_ *a = ARRAY(o); free_m_vector(shred->info->mp, a); } static DTOR(array_dtor_obj) { - struct M_Vector_* a = ARRAY(o); - for(m_uint i = 0; i < ARRAY_LEN(a); ++i) - release(*(M_Object*)(ARRAY_PTR(a) + i * SZ_INT), shred); + struct M_Vector_ *a = ARRAY(o); + for (m_uint i = 0; i < ARRAY_LEN(a); ++i) + release(*(M_Object *)(ARRAY_PTR(a) + i * SZ_INT), shred); } static DTOR(array_dtor_struct) { - struct M_Vector_* a = ARRAY(o); - for(m_uint i = 0; i < ARRAY_LEN(a); ++i) - struct_release(shred, array_base(o->type_ref), &*(m_bit*)(ARRAY_PTR(a) + i * SZ_INT)); + struct M_Vector_ *a = ARRAY(o); + for (m_uint i = 0; i < ARRAY_LEN(a); ++i) + struct_release(shred, array_base(o->type_ref), + &*(m_bit *)(ARRAY_PTR(a) + i * SZ_INT)); } 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 = !tflag(t, tflag_typedef) ? t->array_depth : t->info->parent->array_depth; + const m_uint depth = + !tflag(t, tflag_typedef) ? t->array_depth : t->info->parent->array_depth; const m_uint size = depth > 1 ? SZ_INT : array_base(t)->size; - ARRAY(a) = new_m_vector(p, size, length); + ARRAY(a) = new_m_vector(p, size, length); return a; } static MFUN(vm_vector_rem) { - const m_int index = *(m_int*)(shred->mem + SZ_INT); - const M_Vector v = ARRAY(o); - if(index < 0 || (m_uint)index >= ARRAY_LEN(v)) - return; + const m_int index = *(m_int *)(shred->mem + SZ_INT); + const M_Vector v = ARRAY(o); + if (index < 0 || (m_uint)index >= ARRAY_LEN(v)) return; m_vector_rem(v, (vtype)index); } static MFUN(vm_vector_rem_obj) { - const m_int index = *(m_int*)(shred->mem + SZ_INT); - const M_Vector v = ARRAY(o); - if(index < 0 || (m_uint)index >= ARRAY_LEN(v)) - return; - release(*(M_Object*)(ARRAY_PTR(v) + index * ARRAY_SIZE(v)), shred); + const m_int index = *(m_int *)(shred->mem + SZ_INT); + const M_Vector v = ARRAY(o); + if (index < 0 || (m_uint)index >= ARRAY_LEN(v)) return; + release(*(M_Object *)(ARRAY_PTR(v) + index * ARRAY_SIZE(v)), shred); m_vector_rem(v, (vtype)index); } static MFUN(vm_vector_rem_struct) { - const m_int index = *(m_int*)(shred->mem + SZ_INT); - const M_Vector v = ARRAY(o); - if(index < 0 || (m_uint)index >= ARRAY_LEN(v)) - return; + const m_int index = *(m_int *)(shred->mem + SZ_INT); + const M_Vector v = ARRAY(o); + if (index < 0 || (m_uint)index >= ARRAY_LEN(v)) return; const Type t = o->type_ref; struct_release(shred, array_base(t), ARRAY_PTR(v) + index * ARRAY_SIZE(v)); m_vector_rem(v, (vtype)index); } static MFUN(vm_vector_insert) { - const m_int index = *(m_int*)(shred->mem + SZ_INT); - const M_Vector v = ARRAY(o); - if(index < 0 || (m_uint)index > ARRAY_LEN(v)) - return; - m_vector_insert(v, index, shred->mem + SZ_INT*2); + const m_int index = *(m_int *)(shred->mem + SZ_INT); + const M_Vector v = ARRAY(o); + if (index < 0 || (m_uint)index > ARRAY_LEN(v)) return; + m_vector_insert(v, index, shred->mem + SZ_INT * 2); } static MFUN(vm_vector_insert_obj) { - const m_int index = *(m_int*)(shred->mem + SZ_INT); - const M_Vector v = ARRAY(o); - if(index < 0 || (m_uint)index > ARRAY_LEN(v)) - return; - m_vector_insert(v, index, shred->mem + SZ_INT*2); - ++(*(M_Object*)(shred->mem + SZ_INT*2))->ref; + const m_int index = *(m_int *)(shred->mem + SZ_INT); + const M_Vector v = ARRAY(o); + if (index < 0 || (m_uint)index > ARRAY_LEN(v)) return; + m_vector_insert(v, index, shred->mem + SZ_INT * 2); + ++(*(M_Object *)(shred->mem + SZ_INT * 2))->ref; } static MFUN(vm_vector_insert_struct) { - const m_int index = *(m_int*)(shred->mem + SZ_INT); - const M_Vector v = ARRAY(o); - if(index < 0 || (m_uint)index > ARRAY_LEN(v)) - return; - m_vector_insert(v, index, shred->mem + SZ_INT*2); - struct_addref(shred->info->vm->gwion, array_base(o->type_ref), shred->mem + SZ_INT*2); + const m_int index = *(m_int *)(shred->mem + SZ_INT); + const M_Vector v = ARRAY(o); + if (index < 0 || (m_uint)index > ARRAY_LEN(v)) return; + m_vector_insert(v, index, shred->mem + SZ_INT * 2); + struct_addref(shred->info->vm->gwion, array_base(o->type_ref), + shred->mem + SZ_INT * 2); } -static MFUN(vm_vector_size) { - *(m_uint*)RETURN = ARRAY_LEN(ARRAY(o)); -} +static MFUN(vm_vector_size) { *(m_uint *)RETURN = ARRAY_LEN(ARRAY(o)); } -static MFUN(vm_vector_depth) { - *(m_uint*)RETURN = o->type_ref->array_depth; -} +static MFUN(vm_vector_depth) { *(m_uint *)RETURN = o->type_ref->array_depth; } -static MFUN(vm_vector_cap) { - *(m_uint*)RETURN = ARRAY_CAP(ARRAY(o)); -} +static MFUN(vm_vector_cap) { *(m_uint *)RETURN = ARRAY_CAP(ARRAY(o)); } static MFUN(vm_vector_random) { const M_Vector array = ARRAY(o); - const m_uint sz = ARRAY_LEN(array); - const m_uint idx = (m_int)(sz) * (gw_rand(shred->info->vm->rand) / (UINT32_MAX + 1.0)); - m_vector_get(array, idx, (void*)RETURN); + const m_uint sz = ARRAY_LEN(array); + const m_uint idx = + (m_int)(sz) * (gw_rand(shred->info->vm->rand) / (UINT32_MAX + 1.0)); + m_vector_get(array, idx, (void *)RETURN); } -#define ARRAY_OPCK(a, b, pos) \ - const Type l = array_base(a->type); \ - const Type r = array_base(b->type); \ - if(isa(r, l) < 0) \ - ERR_N(pos, _("array types do not match.")); +#define ARRAY_OPCK(a, b, pos) \ + const Type l = array_base(a->type); \ + const Type r = array_base(b->type); \ + if (isa(r, l) < 0) ERR_N(pos, _("array types do not match.")); static OP_CHECK(opck_array_at) { - const Exp_Binary* bin = (Exp_Binary*)data; - if(opck_const_rhs(env, data) == env->gwion->type[et_error]) + const Exp_Binary *bin = (Exp_Binary *)data; + if (opck_const_rhs(env, data) == env->gwion->type[et_error]) return env->gwion->type[et_error]; - if(bin->lhs->type != env->gwion->type[et_error]) { + if (bin->lhs->type != env->gwion->type[et_error]) { ARRAY_OPCK(bin->lhs, bin->rhs, exp_self(bin)->pos) - if(bin->lhs->type->array_depth != bin->rhs->type->array_depth) + if (bin->lhs->type->array_depth != bin->rhs->type->array_depth) ERR_N(exp_self(bin)->pos, _("array depths do not match.")); } - if(bin->rhs->exp_type == ae_exp_decl) { - if(bin->rhs->d.exp_decl.list->self->array && - bin->rhs->d.exp_decl.list->self->array->exp) - ERR_N(exp_self(bin)->pos, _("do not provide array for 'xxx => declaration'.")); + if (bin->rhs->exp_type == ae_exp_decl) { + if (bin->rhs->d.exp_decl.list->self->array && + bin->rhs->d.exp_decl.list->self->array->exp) + ERR_N(exp_self(bin)->pos, + _("do not provide array for 'xxx => declaration'.")); } exp_setvar(bin->rhs, 1); return bin->rhs->type; } -ANN static Type check_array_shift(const Env env, - const Exp a, const Exp b, const m_str str, const loc_t pos) { -/* if(a->type == env->gwion->type[et_error] && - b->type->array_depth > 1) - return a->type;*/ +ANN static Type check_array_shift(const Env env, const Exp a, const Exp b, + const m_str str, const loc_t pos) { + /* if(a->type == env->gwion->type[et_error] && + b->type->array_depth > 1) + return a->type;*/ ARRAY_OPCK(a, b, pos) - if(a->type->array_depth == b->type->array_depth + 1) + if (a->type->array_depth == b->type->array_depth + 1) return a->type; - else if(a->type->array_depth == b->type->array_depth) + else if (a->type->array_depth == b->type->array_depth) return a->type; ERR_N(pos, "array depths do not match for '%s'.", str); } static OP_CHECK(opck_array_sl) { - const Exp_Binary* bin = (Exp_Binary*)data; + const Exp_Binary *bin = (Exp_Binary *)data; return check_array_shift(env, bin->lhs, bin->rhs, "<<", exp_self(bin)->pos); } static OP_CHECK(opck_array_sr) { - const Exp_Binary* bin = (Exp_Binary*)data; + const Exp_Binary *bin = (Exp_Binary *)data; return check_array_shift(env, bin->rhs, bin->lhs, ">>", exp_self(bin)->pos); } -ANN static inline m_bool emit_array_shift(const Emitter emit, const f_instr exec) { +ANN static inline m_bool emit_array_shift(const Emitter emit, + const f_instr exec) { const Instr pop = emit_add_instr(emit, RegMove); - pop->m_val = -SZ_INT; + pop->m_val = -SZ_INT; (void)emit_add_instr(emit, exec); return GW_OK; } static INSTR(ArrayAppendFront) { - const M_Object o = *(M_Object*)(shred->reg); + const M_Object o = *(M_Object *)(shred->reg); const M_Vector a = ARRAY(o); m_vector_add_front(a, shred->reg - ARRAY_SIZE(a)); } static INSTR(ArrayConcatLeft) { - const M_Object obase = *(M_Object*)(shred->reg - SZ_INT); - const M_Object omore = *(M_Object*)(shred->reg); - const M_Vector base = ARRAY(obase); - const M_Vector more = ARRAY(omore); - const m_uint len = ARRAY_LEN(base); - const m_uint sz = ARRAY_SIZE(base); - if((ARRAY_LEN(base) += ARRAY_LEN(more)) >= ARRAY_CAP(base)) { + const M_Object obase = *(M_Object *)(shred->reg - SZ_INT); + const M_Object omore = *(M_Object *)(shred->reg); + const M_Vector base = ARRAY(obase); + const M_Vector more = ARRAY(omore); + const m_uint len = ARRAY_LEN(base); + const m_uint sz = ARRAY_SIZE(base); + if ((ARRAY_LEN(base) += ARRAY_LEN(more)) >= ARRAY_CAP(base)) { ARRAY_CAP(base) += ARRAY_CAP(more); - m_bit *ptr = (m_bit*)xrealloc(base->ptr, ARRAY_OFFSET + ARRAY_CAP(base) * sz); + m_bit *ptr = + (m_bit *)xrealloc(base->ptr, ARRAY_OFFSET + ARRAY_CAP(base) * sz); base->ptr = ptr; } m_bit *data = more->ptr + ARRAY_OFFSET; @@ -192,42 +185,44 @@ static INSTR(ArrayConcatLeft) { } static INSTR(ArrayConcatRight) { - const M_Object obase = *(M_Object*)(shred->reg); - const M_Object omore = *(M_Object*)(shred->reg - SZ_INT); - const M_Vector base = ARRAY(obase); - const M_Vector more = ARRAY(omore); - const m_uint len = ARRAY_LEN(base); - const m_uint sz = ARRAY_SIZE(base); - if((ARRAY_LEN(base) += ARRAY_LEN(more)) >= ARRAY_CAP(base)) { + const M_Object obase = *(M_Object *)(shred->reg); + const M_Object omore = *(M_Object *)(shred->reg - SZ_INT); + const M_Vector base = ARRAY(obase); + const M_Vector more = ARRAY(omore); + const m_uint len = ARRAY_LEN(base); + const m_uint sz = ARRAY_SIZE(base); + if ((ARRAY_LEN(base) += ARRAY_LEN(more)) >= ARRAY_CAP(base)) { ARRAY_CAP(base) += ARRAY_CAP(more); - m_bit *ptr = (m_bit*)xrealloc(base->ptr, ARRAY_OFFSET + ARRAY_CAP(base) * sz); + m_bit *ptr = + (m_bit *)xrealloc(base->ptr, ARRAY_OFFSET + ARRAY_CAP(base) * sz); base->ptr = ptr; } - memmove(ARRAY_PTR(base) + (ARRAY_LEN(more) + len - 1) * sz, ARRAY_PTR(base), len * sz); + memmove(ARRAY_PTR(base) + (ARRAY_LEN(more) + len - 1) * sz, ARRAY_PTR(base), + len * sz); memmove(ARRAY_PTR(base), ARRAY_PTR(more), ARRAY_LEN(more) * sz); } static OP_EMIT(opem_array_sr) { - const Exp_Binary* bin = (Exp_Binary*)data; - if(bin->rhs->type->array_depth == bin->lhs->type->array_depth) + const Exp_Binary *bin = (Exp_Binary *)data; + if (bin->rhs->type->array_depth == bin->lhs->type->array_depth) return emit_array_shift(emit, ArrayConcatRight); const Instr pop = emit_add_instr(emit, RegMove); - pop->m_val = -SZ_INT; - if(isa(bin->rhs->type, emit->gwion->type[et_object]) > 0) + pop->m_val = -SZ_INT; + if (isa(bin->rhs->type, emit->gwion->type[et_object]) > 0) emit_add_instr(emit, RegAddRef); (void)emit_add_instr(emit, ArrayAppendFront); return GW_OK; } static OP_EMIT(opem_array_sl) { - const Exp_Binary* bin = (Exp_Binary*)data; - if(bin->lhs->type->array_depth == bin->rhs->type->array_depth) + const Exp_Binary *bin = (Exp_Binary *)data; + if (bin->lhs->type->array_depth == bin->rhs->type->array_depth) return emit_array_shift(emit, ArrayConcatLeft); const Instr pop = emit_add_instr(emit, RegMove); - pop->m_val = -bin->rhs->type->size; - if(isa(bin->rhs->type, emit->gwion->type[et_object]) > 0){ + pop->m_val = -bin->rhs->type->size; + if (isa(bin->rhs->type, emit->gwion->type[et_object]) > 0) { const Instr ref = emit_add_instr(emit, RegAddRef); - ref->m_val = -SZ_INT; + ref->m_val = -SZ_INT; } emit_add_instr(emit, ArrayAppend); return GW_OK; @@ -235,10 +230,11 @@ static OP_EMIT(opem_array_sl) { // check me. use common ancestor maybe static OP_CHECK(opck_array_cast) { - const Exp_Cast* cast = (Exp_Cast*)data; - const Type l = array_base(cast->exp->type); - const Type r = array_base(exp_self(cast)->type); - if(get_depth(cast->exp->type) == get_depth(exp_self(cast)->type) && isa(l->info->base_type, r->info->base_type) > 0) + const Exp_Cast *cast = (Exp_Cast *)data; + const Type l = array_base(cast->exp->type); + const Type r = array_base(exp_self(cast)->type); + if (get_depth(cast->exp->type) == get_depth(exp_self(cast)->type) && + isa(l->info->base_type, r->info->base_type) > 0) return l; return NULL; } @@ -255,26 +251,25 @@ static inline m_bool bounds(const M_Vector v, const m_int i) { } static INSTR(ArraySlice) { - shred->reg -= SZ_INT *2; - const M_Object array = *(M_Object*)REG(-SZ_INT); - const M_Vector in = ARRAY(array); - const m_int start = *(m_uint*)REG(0); - m_int end = *(m_uint*)REG(SZ_INT); - if(end < 0) - end = ARRAY_LEN(in) + end; - const m_int op = start < end ? 1 : -1; - const m_uint sz = op > 0 ? end - start : start - end; - if(bounds(in, start) < 0 || bounds(in, end) < 0) { + shred->reg -= SZ_INT * 2; + const M_Object array = *(M_Object *)REG(-SZ_INT); + const M_Vector in = ARRAY(array); + const m_int start = *(m_uint *)REG(0); + m_int end = *(m_uint *)REG(SZ_INT); + if (end < 0) end = ARRAY_LEN(in) + end; + const m_int op = start < end ? 1 : -1; + const m_uint sz = op > 0 ? end - start : start - end; + if (bounds(in, start) < 0 || bounds(in, end) < 0) { handle(shred, "OutOfBoundsArraySliceException"); return; } const M_Object out = new_array(shred->info->mp, array->type_ref, sz); - for(m_int i = start, j = 0; i != end; i += op, ++j) { + for (m_int i = start, j = 0; i != end; i += op, ++j) { m_bit buf[ARRAY_SIZE(in)]; m_vector_get(in, i, &buf); m_vector_set(ARRAY(out), j, buf); } - *(M_Object*)REG(-SZ_INT) = out; + *(M_Object *)REG(-SZ_INT) = out; } static OP_EMIT(opem_array_slice) { @@ -284,7 +279,7 @@ static OP_EMIT(opem_array_slice) { } static FREEARG(freearg_array) { - ArrayInfo* info = (ArrayInfo*)instr->m_val; + ArrayInfo *info = (ArrayInfo *)instr->m_val; vector_release(&info->type); mp_free(((Gwion)gwion)->mp, ArrayInfo, info); } @@ -293,173 +288,171 @@ ANN Type check_array_access(const Env env, const Array_Sub array); static OP_CHECK(opck_array) { const Array_Sub array = (Array_Sub)data; - const Type t_int = env->gwion->type[et_int]; - Exp e = array->exp; + const Type t_int = env->gwion->type[et_int]; + Exp e = array->exp; do CHECK_BN(check_implicit(env, e, t_int)); - while((e = e->next)); - const Type t = array->type->array_depth ? array->type : typedef_base(array->type); - if(t->array_depth >= array->depth) + while ((e = e->next)); + const Type t = + array->type->array_depth ? array->type : typedef_base(array->type); + if (t->array_depth >= array->depth) return array_type(env, array_base(t), t->array_depth - array->depth); - const Exp curr = take_exp(array->exp, t->array_depth); - struct Array_Sub_ next = { curr->next, array_base(t), array->depth - t->array_depth }; + const Exp curr = take_exp(array->exp, t->array_depth); + struct Array_Sub_ next = {curr->next, array_base(t), + array->depth - t->array_depth}; return check_array_access(env, &next) ?: env->gwion->type[et_error]; } ANN static void array_loop(const Emitter emit, const m_uint depth) { const Instr pre_pop = emit_add_instr(emit, RegMove); - pre_pop->m_val = -depth * SZ_INT; - for(m_uint i = 0; i < depth - 1; ++i) { + pre_pop->m_val = -depth * SZ_INT; + for (m_uint i = 0; i < depth - 1; ++i) { const Instr access = emit_add_instr(emit, ArrayAccess); - access->m_val = i * SZ_INT; - access->m_val2 = !i ? SZ_INT : 0; - const Instr get = emit_add_instr(emit, ArrayGet); - get->m_val = i * SZ_INT; - get->m_val2 = -SZ_INT; + access->m_val = i * SZ_INT; + access->m_val2 = !i ? SZ_INT : 0; + const Instr get = emit_add_instr(emit, ArrayGet); + get->m_val = i * SZ_INT; + get->m_val2 = -SZ_INT; } const Instr post_pop = emit_add_instr(emit, RegMove); - post_pop->m_val = -SZ_INT; - const Instr access = emit_add_instr(emit, ArrayAccess); - access->m_val = depth * SZ_INT; + post_pop->m_val = -SZ_INT; + const Instr access = emit_add_instr(emit, ArrayAccess); + access->m_val = depth * SZ_INT; } ANN static void array_finish(const Emitter emit, const m_uint depth, - const m_uint size, const m_bool is_var) { + const m_uint size, const m_bool is_var) { const Instr get = emit_add_instr(emit, is_var ? ArrayAddr : ArrayGet); - get->m_val = depth * SZ_INT; -// emit_add_instr(emit, ArrayValid); + get->m_val = depth * SZ_INT; + // emit_add_instr(emit, ArrayValid); const Instr push = emit_add_instr(emit, RegMove); - push->m_val = is_var ? SZ_INT : size; + push->m_val = is_var ? SZ_INT : size; } -ANN static inline m_bool array_do(const Emitter emit, const Array_Sub array, const m_bool is_var) { -// emit_gc(emit, -SZ_INT); +ANN static inline m_bool array_do(const Emitter emit, const Array_Sub array, + const m_bool is_var) { + // emit_gc(emit, -SZ_INT); CHECK_BB(emit_exp(emit, array->exp)); array_loop(emit, array->depth); array_finish(emit, array->depth, array->type->size, is_var); return GW_OK; } -ANN static inline Exp emit_n_exp(const Emitter emit, struct ArrayAccessInfo *const info) { - const Exp e = take_exp(info->array.exp, info->array.depth); - const Exp next = e->next; - e->next = NULL; - struct Array_Sub_ partial = { info->array.exp, info->array.type, info->array.depth }; - const m_bool ret = array_do(emit, &partial, 0); - e->next = next; +ANN static inline Exp emit_n_exp(const Emitter emit, + struct ArrayAccessInfo *const info) { + const Exp e = take_exp(info->array.exp, info->array.depth); + const Exp next = e->next; + e->next = NULL; + struct Array_Sub_ partial = {info->array.exp, info->array.type, + info->array.depth}; + const m_bool ret = array_do(emit, &partial, 0); + e->next = next; return ret > 0 ? next : NULL; } static OP_EMIT(opem_array_access) { - struct ArrayAccessInfo *const info = (struct ArrayAccessInfo*)data; - if(info->array.type->array_depth >= info->array.depth) { - struct Array_Sub_ next = { .exp=info->array.exp, .type=info->type, .depth=info->array.depth }; + struct ArrayAccessInfo *const info = (struct ArrayAccessInfo *)data; + if (info->array.type->array_depth >= info->array.depth) { + struct Array_Sub_ next = { + .exp = info->array.exp, .type = info->type, .depth = info->array.depth}; return array_do(emit, &next, info->is_var); } - struct Array_Sub_ partial = { info->array.exp, info->array.type, info->array.type->array_depth }; - struct Array_Sub_ next = { info->array.exp, array_base(info->array.type), info->array.depth - info->array.type->array_depth }; - info->array = partial; - const Exp exp = emit_n_exp(emit, info); - next.exp = exp; - info->array = next; + struct Array_Sub_ partial = {info->array.exp, info->array.type, + info->array.type->array_depth}; + struct Array_Sub_ next = {info->array.exp, array_base(info->array.type), + info->array.depth - info->array.type->array_depth}; + info->array = partial; + const Exp exp = emit_n_exp(emit, info); + next.exp = exp; + info->array = next; return exp ? emit_array_access(emit, info) : GW_ERROR; } -static m_bit map_byte[BYTECODE_SZ*4]; -static const struct VM_Code_ map_run_code = { - .name = "map_run_code", - .bytecode = map_byte -}; +static m_bit map_byte[BYTECODE_SZ * 4]; +static const struct VM_Code_ map_run_code = {.name = "map_run_code", + .bytecode = map_byte}; -static m_bit compactmap_byte[BYTECODE_SZ*4]; +static m_bit compactmap_byte[BYTECODE_SZ * 4]; static const struct VM_Code_ compactmap_run_code = { - .name = "compactmap_run_code", - .bytecode = compactmap_byte -}; - -static m_bit filter_byte[BYTECODE_SZ*4]; -static const struct VM_Code_ filter_run_code = { - .name = "filter_run_code", - .bytecode = filter_byte -}; - -static m_bit count_byte[BYTECODE_SZ*4]; -static const struct VM_Code_ count_run_code = { - .name = "count_run_code", - .bytecode = count_byte -}; - -static m_bit foldl_byte[BYTECODE_SZ*4]; -static const struct VM_Code_ foldl_run_code = { - .name = "foldl_run_code", - .bytecode = foldl_byte -}; - -static m_bit foldr_byte[BYTECODE_SZ*4]; -static const struct VM_Code_ foldr_run_code = { - .name = "foldr_run_code", - .bytecode = foldr_byte -}; + .name = "compactmap_run_code", .bytecode = compactmap_byte}; + +static m_bit filter_byte[BYTECODE_SZ * 4]; +static const struct VM_Code_ filter_run_code = {.name = "filter_run_code", + .bytecode = filter_byte}; + +static m_bit count_byte[BYTECODE_SZ * 4]; +static const struct VM_Code_ count_run_code = {.name = "count_run_code", + .bytecode = count_byte}; + +static m_bit foldl_byte[BYTECODE_SZ * 4]; +static const struct VM_Code_ foldl_run_code = {.name = "foldl_run_code", + .bytecode = foldl_byte}; + +static m_bit foldr_byte[BYTECODE_SZ * 4]; +static const struct VM_Code_ foldr_run_code = {.name = "foldr_run_code", + .bytecode = foldr_byte}; typedef struct FunctionalFrame { - VM_Code code; + VM_Code code; uint16_t pc; uint16_t offset; uint16_t index; } FunctionalFrame; -ANN static inline void _init(const VM_Shred shred, - const struct VM_Code_ *code, const m_uint offset, const m_uint start) { - FunctionalFrame *frame = &*(FunctionalFrame*)MEM(SZ_INT*2 + start); - frame->pc = shred->pc; - frame->code = shred->code; - frame->offset = offset; - frame->index = 0; - *(m_uint*)REG(SZ_INT) = offset;// + sizeof(frame_t);// + shred->code->stack_depth; +ANN static inline void _init(const VM_Shred shred, const struct VM_Code_ *code, + const m_uint offset, const m_uint start) { + FunctionalFrame *frame = &*(FunctionalFrame *)MEM(SZ_INT * 2 + start); + frame->pc = shred->pc; + frame->code = shred->code; + frame->offset = offset; + frame->index = 0; + *(m_uint *)REG(SZ_INT) = + offset; // + sizeof(frame_t);// + shred->code->stack_depth; shred->code = (VM_Code)code; - shred->pc = 0; + shred->pc = 0; shredule(shred->tick->shreduler, shred, 0); } ANN static inline void _next(const VM_Shred shred, const m_uint offset) { - shred->pc = 0; - *(m_uint*)REG(0) = offset;// + sizeof(frame_t); + shred->pc = 0; + *(m_uint *)REG(0) = offset; // + sizeof(frame_t); POP_REG(shred, SZ_INT); } -ANN static inline void _return(const VM_Shred shred, const FunctionalFrame* frame) { - shred->pc = frame->pc; +ANN static inline void _return(const VM_Shred shred, + const FunctionalFrame *frame) { + shred->pc = frame->pc; shred->code = frame->code; } -ANN static inline void _finish(const VM_Shred shred, const FunctionalFrame* frame) { +ANN static inline void _finish(const VM_Shred shred, + const FunctionalFrame *frame) { POP_MEM(shred, frame->offset); shredule(shred->tick->shreduler, shred, 0); } #define MAP_CODE_OFFSET (sizeof(FunctionalFrame) + sizeof(struct frame_t)) static INSTR(map_run_ini) { - const m_uint offset = *(m_uint*)REG(SZ_INT); - if(offset) - PUSH_MEM(shred, offset); + const m_uint offset = *(m_uint *)REG(SZ_INT); + if (offset) PUSH_MEM(shred, offset); PUSH_REG(shred, SZ_INT); - const M_Object self = *(M_Object*)MEM(0); - const M_Vector array = ARRAY(self); - FunctionalFrame *frame = &*(FunctionalFrame*)MEM(SZ_INT*3); + const M_Object self = *(M_Object *)MEM(0); + const M_Vector array = ARRAY(self); + FunctionalFrame *frame = &*(FunctionalFrame *)MEM(SZ_INT * 3); shred->pc++; shred->mem += MAP_CODE_OFFSET + SZ_INT; // work in a safe memory space - m_vector_get(array, frame->index, &*(m_bit**)(shred->mem + SZ_INT*3)); + m_vector_get(array, frame->index, &*(m_bit **)(shred->mem + SZ_INT * 3)); } static INSTR(map_run_end) { shred->mem -= MAP_CODE_OFFSET + SZ_INT; - const M_Object ret_obj = *(M_Object*)MEM(SZ_INT*2); - const M_Vector array = ARRAY(*(M_Object*)MEM(0)); + const M_Object ret_obj = *(M_Object *)MEM(SZ_INT * 2); + const M_Vector array = ARRAY(*(M_Object *)MEM(0)); POP_REG(shred, ARRAY_SIZE(array)); - FunctionalFrame *const frame = &*(FunctionalFrame*)MEM(SZ_INT*3); + FunctionalFrame *const frame = &*(FunctionalFrame *)MEM(SZ_INT * 3); m_vector_set(ARRAY(ret_obj), frame->index, shred->reg); - if(++frame->index == ARRAY_LEN(array)) { + if (++frame->index == ARRAY_LEN(array)) { _return(shred, frame); - *(M_Object*)(REG(-SZ_INT)) = ret_obj; + *(M_Object *)(REG(-SZ_INT)) = ret_obj; } else _next(shred, frame->offset); _finish(shred, frame); @@ -467,19 +460,19 @@ static INSTR(map_run_end) { static INSTR(compactmap_run_end) { shred->mem -= MAP_CODE_OFFSET + SZ_INT; - const M_Object self = *(M_Object*)MEM(0); + const M_Object self = *(M_Object *)MEM(0); const M_Vector self_array = ARRAY(self); - const M_Object ret_obj = *(M_Object*)MEM(SZ_INT*2); - const M_Vector ret_array = ARRAY(ret_obj); + const M_Object ret_obj = *(M_Object *)MEM(SZ_INT * 2); + const M_Vector ret_array = ARRAY(ret_obj); POP_REG(shred, ARRAY_SIZE(ret_array)); - FunctionalFrame *const frame = &*(FunctionalFrame*)MEM(SZ_INT*3); - const m_uint size = m_vector_size(self_array); - const M_Object obj = *(M_Object*)REG(0); - if(*(m_uint*)obj->data) - m_vector_add(ret_array, &*(m_bit*)(obj->data + SZ_INT)); - if(++frame->index == size) { + FunctionalFrame *const frame = &*(FunctionalFrame *)MEM(SZ_INT * 3); + const m_uint size = m_vector_size(self_array); + const M_Object obj = *(M_Object *)REG(0); + if (*(m_uint *)obj->data) + m_vector_add(ret_array, &*(m_bit *)(obj->data + SZ_INT)); + if (++frame->index == size) { _return(shred, frame); - *(M_Object*)(REG(-SZ_INT)) = ret_obj; + *(M_Object *)(REG(-SZ_INT)) = ret_obj; } else _next(shred, frame->offset); _finish(shred, frame); @@ -488,15 +481,16 @@ static INSTR(compactmap_run_end) { static INSTR(filter_run_end) { shred->mem -= MAP_CODE_OFFSET + SZ_INT; POP_REG(shred, SZ_INT); - const M_Object self = *(M_Object*)MEM(0); - const M_Object ret_obj = *(M_Object*)MEM(SZ_INT*2); - const M_Vector array = ARRAY(ret_obj); - FunctionalFrame *const frame = &*(FunctionalFrame*)MEM(SZ_INT*3); - if(*(m_uint*)(shred->reg)) - m_vector_add(array, ARRAY_PTR(ARRAY(self)) + frame->index * ARRAY_SIZE(array)); - if(++frame->index == ARRAY_LEN(ARRAY(self))) { + const M_Object self = *(M_Object *)MEM(0); + const M_Object ret_obj = *(M_Object *)MEM(SZ_INT * 2); + const M_Vector array = ARRAY(ret_obj); + FunctionalFrame *const frame = &*(FunctionalFrame *)MEM(SZ_INT * 3); + if (*(m_uint *)(shred->reg)) + m_vector_add(array, + ARRAY_PTR(ARRAY(self)) + frame->index * ARRAY_SIZE(array)); + if (++frame->index == ARRAY_LEN(ARRAY(self))) { _return(shred, frame); - *(M_Object*)(REG(-SZ_INT)) = ret_obj; + *(M_Object *)(REG(-SZ_INT)) = ret_obj; } else _next(shred, frame->offset); _finish(shred, frame); @@ -504,217 +498,238 @@ static INSTR(filter_run_end) { static INSTR(count_run_end) { shred->mem -= MAP_CODE_OFFSET + SZ_INT; - const M_Object self= *(M_Object*)MEM(0); + const M_Object self = *(M_Object *)MEM(0); POP_REG(shred, SZ_INT); - FunctionalFrame *const frame = &*(FunctionalFrame*)MEM(SZ_INT*3); - if(*(m_uint*)(shred->reg)) - (*(m_uint*)MEM(SZ_INT*2))++; - if(++frame->index == ARRAY_LEN(ARRAY(self))) { + FunctionalFrame *const frame = &*(FunctionalFrame *)MEM(SZ_INT * 3); + if (*(m_uint *)(shred->reg)) (*(m_uint *)MEM(SZ_INT * 2))++; + if (++frame->index == ARRAY_LEN(ARRAY(self))) { _return(shred, frame); - *(m_uint*)(REG(-SZ_INT)) = *(m_uint*)MEM(SZ_INT*2); + *(m_uint *)(REG(-SZ_INT)) = *(m_uint *)MEM(SZ_INT * 2); } else _next(shred, frame->offset); _finish(shred, frame); } static MFUN(vm_vector_map) { - const m_uint offset = *(m_uint*)REG(SZ_INT*3); - const M_Object ret = new_array(shred->info->mp, o->type_ref, ARRAY_LEN(ARRAY(o))); + const m_uint offset = *(m_uint *)REG(SZ_INT * 3); + const M_Object ret = + new_array(shred->info->mp, o->type_ref, ARRAY_LEN(ARRAY(o))); vector_add(&shred->gc, (m_uint)ret); - if(ARRAY_LEN(ARRAY(o))) { + if (ARRAY_LEN(ARRAY(o))) { _init(shred, &map_run_code, offset, SZ_INT); - *(M_Object*)MEM(SZ_INT*2) = ret; + *(M_Object *)MEM(SZ_INT * 2) = ret; } else - *(M_Object*)RETURN = ret; + *(M_Object *)RETURN = ret; } static MFUN(vm_vector_compactmap) { - const VM_Code code = *(VM_Code*)REG(SZ_INT*2); - const m_uint offset = *(m_uint*)REG(SZ_INT*3); - const M_Object ret = new_array(shred->info->mp, - code->ret_type, 0); + const VM_Code code = *(VM_Code *)REG(SZ_INT * 2); + const m_uint offset = *(m_uint *)REG(SZ_INT * 3); + const M_Object ret = new_array(shred->info->mp, code->ret_type, 0); vector_add(&shred->gc, (m_uint)ret); - if(ARRAY_LEN(ARRAY(o))) { + if (ARRAY_LEN(ARRAY(o))) { _init(shred, &compactmap_run_code, offset, SZ_INT); - *(M_Object*)MEM(SZ_INT*2) = ret; + *(M_Object *)MEM(SZ_INT * 2) = ret; } else - *(M_Object*)RETURN = ret; + *(M_Object *)RETURN = ret; } static MFUN(vm_vector_filter) { - const m_uint offset = *(m_uint*)REG(SZ_INT*3); - const M_Object ret = new_array(shred->info->mp, o->type_ref, 0); + const m_uint offset = *(m_uint *)REG(SZ_INT * 3); + const M_Object ret = new_array(shred->info->mp, o->type_ref, 0); vector_add(&shred->gc, (m_uint)ret); - if(ARRAY_LEN(ARRAY(o))) { + if (ARRAY_LEN(ARRAY(o))) { _init(shred, &filter_run_code, offset, SZ_INT); - *(M_Object*)MEM(SZ_INT*2) = ret; + *(M_Object *)MEM(SZ_INT * 2) = ret; } else - *(M_Object*)RETURN = ret; + *(M_Object *)RETURN = ret; } static MFUN(vm_vector_count) { - const m_uint offset = *(m_uint*)REG(SZ_INT*3); - if(ARRAY_LEN(ARRAY(o))) { + const m_uint offset = *(m_uint *)REG(SZ_INT * 3); + if (ARRAY_LEN(ARRAY(o))) { _init(shred, &count_run_code, offset, SZ_INT); - *(m_uint*)MEM(SZ_INT*2) = 0; + *(m_uint *)MEM(SZ_INT * 2) = 0; } else - *(m_uint*)RETURN = 0; + *(m_uint *)RETURN = 0; } static INSTR(foldl_run_ini) { - const m_uint offset = *(m_uint*)REG(SZ_INT); - if(offset) - PUSH_MEM(shred, offset); - const M_Object self = *(M_Object*)MEM(0); - *(m_uint*)(shred->reg + SZ_INT) = 0; + const m_uint offset = *(m_uint *)REG(SZ_INT); + if (offset) PUSH_MEM(shred, offset); + const M_Object self = *(M_Object *)MEM(0); + *(m_uint *)(shred->reg + SZ_INT) = 0; PUSH_REG(shred, SZ_INT); shred->pc++; - const FunctionalFrame *frame = &*(FunctionalFrame*)MEM(SZ_INT*3); + const FunctionalFrame *frame = &*(FunctionalFrame *)MEM(SZ_INT * 3); shred->mem += MAP_CODE_OFFSET + SZ_INT; // work in a safe memory space - m_vector_get(ARRAY(self), frame->index, &*(m_bit**)(shred->mem + SZ_INT*2)); + m_vector_get(ARRAY(self), frame->index, + &*(m_bit **)(shred->mem + SZ_INT * 2)); } static INSTR(foldr_run_ini) { - const m_uint offset = *(m_uint*)REG(SZ_INT); - if(offset) - PUSH_MEM(shred, offset); - const M_Object self = *(M_Object*)MEM(0); - *(m_uint*)(shred->reg + SZ_INT) = 0; + const m_uint offset = *(m_uint *)REG(SZ_INT); + if (offset) PUSH_MEM(shred, offset); + const M_Object self = *(M_Object *)MEM(0); + *(m_uint *)(shred->reg + SZ_INT) = 0; PUSH_REG(shred, SZ_INT); shred->pc++; - const FunctionalFrame *frame = &*(FunctionalFrame*)MEM(SZ_INT*3); + const FunctionalFrame *frame = &*(FunctionalFrame *)MEM(SZ_INT * 3); shred->mem += MAP_CODE_OFFSET + SZ_INT; // work in a safe memory space const M_Vector array = ARRAY(self); - m_vector_get(array, ARRAY_LEN(array) - frame->index - 1, &*(m_bit**)(shred->mem + SZ_INT*2)); + m_vector_get(array, ARRAY_LEN(array) - frame->index - 1, + &*(m_bit **)(shred->mem + SZ_INT * 2)); } static INSTR(fold_run_end) { shred->mem -= MAP_CODE_OFFSET + SZ_INT; - FunctionalFrame *const frame = &*(FunctionalFrame*)MEM(SZ_INT*3); - const M_Object self = *(M_Object*)MEM(0); - const VM_Code code = *(VM_Code*)MEM(SZ_INT); - const m_uint sz = code->stack_depth - ARRAY_SIZE(ARRAY(self)); - const m_uint base_sz = code->stack_depth - sz; + FunctionalFrame *const frame = &*(FunctionalFrame *)MEM(SZ_INT * 3); + const M_Object self = *(M_Object *)MEM(0); + const VM_Code code = *(VM_Code *)MEM(SZ_INT); + const m_uint sz = code->stack_depth - ARRAY_SIZE(ARRAY(self)); + const m_uint base_sz = code->stack_depth - sz; POP_REG(shred, base_sz); - if(++frame->index == ARRAY_LEN(ARRAY(self))) { + if (++frame->index == ARRAY_LEN(ARRAY(self))) { POP_REG(shred, SZ_INT - base_sz); - shred->pc = frame->pc; + shred->pc = frame->pc; shred->code = frame->code; memcpy(REG(-sz), REG(0), base_sz); } else { - memcpy(shred->mem + MAP_CODE_OFFSET + SZ_INT*3 + sz, shred->reg, base_sz); + memcpy(shred->mem + MAP_CODE_OFFSET + SZ_INT * 3 + sz, shred->reg, base_sz); _next(shred, frame->offset); } _finish(shred, frame); } static MFUN(vm_vector_foldl) { - const m_bit *byte = shred->code->bytecode + (shred->pc-1) * BYTECODE_SZ; - const m_uint acc_sz = *(m_uint*)(byte + SZ_INT); - const m_uint offset = *(m_uint*)REG(SZ_INT*3 + acc_sz); - if(ARRAY_LEN(ARRAY(o))) { + const m_bit *byte = shred->code->bytecode + (shred->pc - 1) * BYTECODE_SZ; + const m_uint acc_sz = *(m_uint *)(byte + SZ_INT); + const m_uint offset = *(m_uint *)REG(SZ_INT * 3 + acc_sz); + if (ARRAY_LEN(ARRAY(o))) { _init(shred, &foldl_run_code, offset, SZ_INT); - memcpy(shred->mem + MAP_CODE_OFFSET + SZ_INT*3 + acc_sz, MEM(SZ_INT*2), acc_sz); + memcpy(shred->mem + MAP_CODE_OFFSET + SZ_INT * 3 + acc_sz, MEM(SZ_INT * 2), + acc_sz); } else - memcpy((m_bit*)RETURN, MEM(SZ_INT*2), acc_sz); + memcpy((m_bit *)RETURN, MEM(SZ_INT * 2), acc_sz); } static MFUN(vm_vector_foldr) { - const m_bit *byte = shred->code->bytecode + (shred->pc-1) * BYTECODE_SZ; - const m_uint acc_sz = *(m_uint*)(byte + SZ_INT); - const m_uint offset = *(m_uint*)REG(SZ_INT*3 + acc_sz); - if(ARRAY_LEN(ARRAY(o))) { + const m_bit *byte = shred->code->bytecode + (shred->pc - 1) * BYTECODE_SZ; + const m_uint acc_sz = *(m_uint *)(byte + SZ_INT); + const m_uint offset = *(m_uint *)REG(SZ_INT * 3 + acc_sz); + if (ARRAY_LEN(ARRAY(o))) { _init(shred, &foldr_run_code, offset, SZ_INT); - memcpy(shred->mem + MAP_CODE_OFFSET + SZ_INT*3 + acc_sz, MEM(SZ_INT*2), acc_sz); + memcpy(shred->mem + MAP_CODE_OFFSET + SZ_INT * 3 + acc_sz, MEM(SZ_INT * 2), + acc_sz); } else - memcpy((m_bit*)RETURN, MEM(SZ_INT*2), acc_sz); + memcpy((m_bit *)RETURN, MEM(SZ_INT * 2), acc_sz); } -ANN /*static */Symbol array_sym(const Env env, const Type src, const m_uint depth); +ANN /*static */ Symbol array_sym(const Env env, const Type src, + const m_uint depth); #include "template.h" static OP_CHECK(opck_array_scan) { - struct TemplateScan *ts = (struct TemplateScan*)data; - const Type t_array = env->gwion->type[et_array]; - const Class_Def c = t_array->info->cdef; - DECL_ON(const Type, base, = ts->t != t_array ? - ts->t : known_type(env, ts->td->types->td)); - if(!strncmp(base->name, "Ref:[", 5)) { - gwerr_basic("Can't use ref types as array base", NULL, NULL, "/dev/null", (loc_t){}, 0); + struct TemplateScan *ts = (struct TemplateScan *)data; + const Type t_array = env->gwion->type[et_array]; + const Class_Def c = t_array->info->cdef; + DECL_ON(const Type, base, + = ts->t != t_array ? ts->t : known_type(env, ts->td->types->td)); + if (!strncmp(base->name, "Ref:[", 5)) { + gwerr_basic("Can't use ref types as array base", NULL, NULL, "/dev/null", + (loc_t) {}, 0); env->context->error = true; return env->gwion->type[et_error]; } - if(!strncmp(base->name, "Option:[", 5)) { - gwerr_basic("Can't use option types as array base", NULL, NULL, "/dev/null", (loc_t){}, 0); + if (!strncmp(base->name, "Option:[", 5)) { + gwerr_basic("Can't use option types as array base", NULL, NULL, "/dev/null", + (loc_t) {}, 0); env->context->error = true; return env->gwion->type[et_error]; } - const Symbol sym = array_sym(env, array_base(base), base->array_depth + 1); - const Type type = nspc_lookup_type1(base->info->value->from->owner, sym); - if(type) - return type; - const Class_Def cdef = cpy_class_def(env->gwion->mp, c); - cdef->base.ext = type2td(env->gwion, t_array, (loc_t){}); - cdef->base.xid = sym; + const Symbol sym = array_sym(env, array_base(base), base->array_depth + 1); + const Type type = nspc_lookup_type1(base->info->value->from->owner, sym); + if (type) return type; + const Class_Def cdef = cpy_class_def(env->gwion->mp, c); + cdef->base.ext = type2td(env->gwion, t_array, (loc_t) {}); + cdef->base.xid = sym; cdef->base.tmpl->base = 1; // could store depth here? - cdef->base.tmpl->call = new_type_list(env->gwion->mp, type2td(env->gwion, base, (loc_t){}), NULL); - const Context ctx = env->context; - env->context = base->info->value->from->ctx; - const m_uint scope = env_push(env, base->info->value->from->owner_class, base->info->value->from->owner); + cdef->base.tmpl->call = new_type_list( + env->gwion->mp, type2td(env->gwion, base, (loc_t) {}), NULL); + const Context ctx = env->context; + env->context = base->info->value->from->ctx; + const m_uint scope = env_push(env, base->info->value->from->owner_class, + base->info->value->from->owner); (void)scan0_class_def(env, cdef); const Type t = cdef->base.type; (void)traverse_cdef(env, t); env_pop(env, scope); env->context = ctx; - if(GET_FLAG(base, abstract)) + if (GET_FLAG(base, abstract)) SET_FLAG(t, abstract); else UNSET_FLAG(t, abstract); set_tflag(t, tflag_emit); - t->array_depth = base->array_depth + 1; + t->array_depth = base->array_depth + 1; t->info->base_type = array_base(base); set_tflag(t, tflag_cdef | tflag_tmpl); - void* rem = isa(base, env->gwion->type[et_compound]) > 0 ? - !tflag(base, tflag_struct) ? vm_vector_rem_obj : vm_vector_rem_struct : vm_vector_rem; + void *rem = isa(base, env->gwion->type[et_compound]) > 0 + ? !tflag(base, tflag_struct) ? vm_vector_rem_obj + : vm_vector_rem_struct + : vm_vector_rem; builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 0), rem); - void* insert = isa(base, env->gwion->type[et_compound]) > 0 ? - !tflag(base, tflag_struct) ? vm_vector_insert_obj : vm_vector_insert_struct : vm_vector_insert; + void *insert = isa(base, env->gwion->type[et_compound]) > 0 + ? !tflag(base, tflag_struct) ? vm_vector_insert_obj + : vm_vector_insert_struct + : vm_vector_insert; builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 1), insert); - builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 2), vm_vector_size); - builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 3), vm_vector_depth); - builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 4), vm_vector_cap); - builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 5), vm_vector_random); - builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 6), vm_vector_map); - builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 7), vm_vector_compactmap); - builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 8), vm_vector_filter); - builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 9), vm_vector_count); - builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 10), vm_vector_foldl); - builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 11), vm_vector_foldr); - if(isa(base, env->gwion->type[et_compound]) > 0) { - t->nspc->dtor = new_vmcode(env->gwion->mp, NULL, SZ_INT, 1, "array component dtor"); + builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 2), + vm_vector_size); + builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 3), + vm_vector_depth); + builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 4), + vm_vector_cap); + builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 5), + vm_vector_random); + builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 6), + vm_vector_map); + builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 7), + vm_vector_compactmap); + builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 8), + vm_vector_filter); + builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 9), + vm_vector_count); + builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 10), + vm_vector_foldl); + builtin_func(env->gwion->mp, (Func)vector_at(&t->nspc->vtable, 11), + vm_vector_foldr); + if (isa(base, env->gwion->type[et_compound]) > 0) { + t->nspc->dtor = + new_vmcode(env->gwion->mp, NULL, SZ_INT, 1, "array component dtor"); set_tflag(t, tflag_dtor); - t->nspc->dtor->native_func = (m_uint) (!tflag(base, tflag_struct) ? - array_dtor_obj : array_dtor_struct); + t->nspc->dtor->native_func = (m_uint)( + !tflag(base, tflag_struct) ? array_dtor_obj : array_dtor_struct); } return t; } static OP_CHECK(opck_array_implicit) { - const struct Implicit* imp = (struct Implicit*)data; - if(imp->t->array_depth != imp->e->type->array_depth) + const struct Implicit *imp = (struct Implicit *)data; + if (imp->t->array_depth != imp->e->type->array_depth) return env->gwion->type[et_error]; - if(isa(array_base(imp->e->type), array_base(imp->t)) < 0) + if (isa(array_base(imp->e->type), array_base(imp->t)) < 0) return env->gwion->type[et_error]; return imp->t; } -ANN static void prepare_run(m_bit *const byte, const f_instr ini, const f_instr end) { - *(unsigned*)byte = eOP_MAX; - *(f_instr*)(byte + SZ_INT*2) = ini; - *(unsigned*)(byte+ BYTECODE_SZ) = eSetCode; - *(uint16_t*)(byte + BYTECODE_SZ + SZ_INT*2) = 3; - *(unsigned*)(byte+ BYTECODE_SZ*2) = eOverflow; - *(unsigned*)(byte+ BYTECODE_SZ*3) = eOP_MAX; - *(f_instr*)(byte + BYTECODE_SZ*3 + SZ_INT*2) = end; +ANN static void prepare_run(m_bit *const byte, const f_instr ini, + const f_instr end) { + *(unsigned *)byte = eOP_MAX; + *(f_instr *)(byte + SZ_INT * 2) = ini; + *(unsigned *)(byte + BYTECODE_SZ) = eSetCode; + *(uint16_t *)(byte + BYTECODE_SZ + SZ_INT * 2) = 3; + *(unsigned *)(byte + BYTECODE_SZ * 2) = eOverflow; + *(unsigned *)(byte + BYTECODE_SZ * 3) = eOP_MAX; + *(f_instr *)(byte + BYTECODE_SZ * 3 + SZ_INT * 2) = end; } ANN static void prepare_map_run(m_bit *const byte, const f_instr end) { @@ -732,7 +747,7 @@ GWION_IMPORT(array) { prepare_map_run(count_byte, count_run_end); prepare_fold_run(foldl_byte, foldl_run_ini); prepare_fold_run(foldr_byte, foldr_run_ini); - const Type t_array = gwi_class_ini(gwi, "@Array:[T]", "Object"); + const Type t_array = gwi_class_ini(gwi, "@Array:[T]", "Object"); set_tflag(t_array, tflag_infer); gwi->gwion->type[et_array] = t_array; gwi_class_xtor(gwi, NULL, array_dtor); @@ -839,55 +854,57 @@ GWION_IMPORT(array) { } INSTR(ArrayStruct) { - const M_Object ref = *(M_Object*)(REG(-SZ_INT * 5)); - const m_int idx = (*(m_int*)((shred->reg -SZ_INT * 3)))++; - *(m_bit**)(shred->reg) = m_vector_addr(ARRAY(ref), idx); + const M_Object ref = *(M_Object *)(REG(-SZ_INT * 5)); + const m_int idx = (*(m_int *)((shred->reg - SZ_INT * 3)))++; + *(m_bit **)(shred->reg) = m_vector_addr(ARRAY(ref), idx); shred->reg += SZ_INT; // regpush } INSTR(ArrayBottom) { - *(M_Object*)(*(m_uint**)REG(-SZ_INT * 4))[(*(m_int*)REG(-SZ_INT * 3))++] = *(M_Object*)REG(-SZ_INT); + *(M_Object *)(*(m_uint **)REG(-SZ_INT * 4))[(*(m_int *)REG(-SZ_INT * 3))++] = + *(M_Object *)REG(-SZ_INT); } INSTR(ArrayPost) { - xfree(*(m_uint**)REG(0)); - const M_Object o = *(M_Object*)(REG(-SZ_INT)); - *(m_uint*)(o->data + SZ_INT) = 0; + xfree(*(m_uint **)REG(0)); + const M_Object o = *(M_Object *)(REG(-SZ_INT)); + *(m_uint *)(o->data + SZ_INT) = 0; } -INSTR(ArrayInit) {// for litteral array - const Type t = (Type)instr->m_val; - const m_uint sz = *(m_uint*)REG(0); +INSTR(ArrayInit) { // for litteral array + const Type t = (Type)instr->m_val; + 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(shred->info->mp, t, sz); memcpy(ARRAY(obj)->ptr + ARRAY_OFFSET, REG(-SZ_INT), off); - *(M_Object*)REG(-SZ_INT) = obj; + *(M_Object *)REG(-SZ_INT) = obj; } #define TOP -1 -ANN static inline M_Object do_alloc_array_object(MemPool p, 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)); + const Type t = (Type)vector_at(&v, (vtype)(-info->depth - 1)); return new_array(p, t, (m_uint)cap); } -ANN static inline M_Object do_alloc_array_init(ArrayInfo* info, const m_uint cap, - const M_Object base) { - for(m_uint i = 0; i < cap; ++i) +ANN static inline M_Object +do_alloc_array_init(ArrayInfo *info, const m_uint cap, const M_Object base) { + for (m_uint i = 0; i < cap; ++i) info->data[(*info->d.idx)++] = (M_Object)m_vector_addr(ARRAY(base), i); return base; } -ANN static M_Object do_alloc_array(const VM_Shred shred, ArrayInfo* info); -ANN static M_Object do_alloc_array_loop(const VM_Shred shred, ArrayInfo* info, - const m_uint cap, const M_Object base) { - for(m_uint i = 0; i < cap; ++i) { - struct ArrayInfo_ aai = { info->depth + 1, info->type, - info->base, info->data, { info->d.idx } , info->is_obj }; - const M_Object next = do_alloc_array(shred, &aai); - if(!next) { +ANN static M_Object do_alloc_array(const VM_Shred shred, ArrayInfo *info); +ANN static M_Object do_alloc_array_loop(const VM_Shred shred, ArrayInfo *info, + const m_uint cap, const M_Object base) { + for (m_uint i = 0; i < cap; ++i) { + struct ArrayInfo_ aai = {info->depth + 1, info->type, info->base, + info->data, {info->d.idx}, info->is_obj}; + const M_Object next = do_alloc_array(shred, &aai); + if (!next) { _release(base, shred); return NULL; } @@ -896,51 +913,52 @@ ANN static M_Object do_alloc_array_loop(const VM_Shred shred, ArrayInfo* info, return base; } -ANN static M_Object do_alloc_array(const VM_Shred shred, ArrayInfo* info) { - const m_int cap = *(m_int*)REG(info->depth * SZ_INT); - if(cap < 0) { +ANN static M_Object do_alloc_array(const VM_Shred shred, ArrayInfo *info) { + const m_int cap = *(m_int *)REG(info->depth * SZ_INT); + if (cap < 0) { gw_err("[gwion](VM): NegativeArraySize: while allocating arrays...\n"); return NULL; } 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; + 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; } -ANN static M_Object* init_array(const VM_Shred shred, const ArrayInfo* info, m_uint* num_obj) { +ANN static M_Object *init_array(const VM_Shred shred, const ArrayInfo *info, + m_uint *num_obj) { m_int curr = -info->depth; - while(curr <= TOP) { - *num_obj *= *(m_uint*)REG(SZ_INT * curr); + while (curr <= TOP) { + *num_obj *= *(m_uint *)REG(SZ_INT * curr); ++curr; } - return *num_obj > 0 ? (M_Object*)xcalloc(*num_obj, info->base->size) : NULL; + return *num_obj > 0 ? (M_Object *)xcalloc(*num_obj, info->base->size) : NULL; } INSTR(ArrayAlloc) { - const ArrayInfo* info = (ArrayInfo*)instr->m_val; - m_uint num_obj = 1; - m_int idx = 0; - struct ArrayInfo_ aai = { -info->depth, info->type, .base=info->base, - NULL, { &idx }, info->is_obj}; - if(info->is_obj) - aai.data = init_array(shred, info, &num_obj); + const ArrayInfo * info = (ArrayInfo *)instr->m_val; + m_uint num_obj = 1; + m_int idx = 0; + struct ArrayInfo_ aai = {-info->depth, info->type, .base = info->base, + NULL, {&idx}, info->is_obj}; + if (info->is_obj) aai.data = init_array(shred, info, &num_obj); const M_Object ref = do_alloc_array(shred, &aai); - if(!ref) { - gw_err("[Gwion](VM): (note: in shred[id=%" UINT_F ":%s])\n", shred->tick->xid, shred->code->name); + if (!ref) { + gw_err("[Gwion](VM): (note: in shred[id=%" UINT_F ":%s])\n", + shred->tick->xid, shred->code->name); vm_shred_exit(shred); return; // TODO make exception vararg } - *(void**)(ref->data + SZ_INT) = aai.data; + *(void **)(ref->data + SZ_INT) = aai.data; vector_add(&shred->gc, (m_uint)ref); - if(!info->is_obj) { + if (!info->is_obj) { POP_REG(shred, SZ_INT * (info->depth - 1)); - *(M_Object*)REG(-SZ_INT) = ref; + *(M_Object *)REG(-SZ_INT) = ref; } else { POP_REG(shred, SZ_INT * (info->depth - 4)); - *(M_Object*)REG(-SZ_INT*4) = ref; - *(M_Object**)REG(-SZ_INT*3) = aai.data; - *(m_uint*) REG(-SZ_INT*2) = 0; - *(m_uint*) REG(-SZ_INT) = num_obj; + *(M_Object *)REG(-SZ_INT * 4) = ref; + *(M_Object **)REG(-SZ_INT * 3) = aai.data; + *(m_uint *)REG(-SZ_INT * 2) = 0; + *(m_uint *)REG(-SZ_INT) = num_obj; } } - diff --git a/src/lib/engine.c b/src/lib/engine.c index 28f6785b..b61a9ad3 100644 --- a/src/lib/engine.c +++ b/src/lib/engine.c @@ -18,58 +18,42 @@ static GACK(gack_class) { INTERP_PRINTF("class(%s)", type->name) } -static GACK(gack_function) { - INTERP_PRINTF("%s", t->name) -} +static GACK(gack_function) { INTERP_PRINTF("%s", t->name) } -static GACK(gack_gack) { - INTERP_PRINTF("%s", *(m_str*)VALUE) -} +static GACK(gack_gack) { INTERP_PRINTF("%s", *(m_str *)VALUE) } static GACK(gack_fptr) { - const VM_Code code = *(VM_Code*)VALUE; - if(code) + const VM_Code code = *(VM_Code *)VALUE; + if (code) INTERP_PRINTF("%s", code->name) else INTERP_PRINTF("%s", t->name) } -static GACK(gack_void) { - INTERP_PRINTF("void"); -} +static GACK(gack_void) { INTERP_PRINTF("void"); } -static GACK(gack_int) { - INTERP_PRINTF("%"INT_F, *(m_uint*)VALUE); -} +static GACK(gack_int) { INTERP_PRINTF("%" INT_F, *(m_uint *)VALUE); } -static GACK(gack_char) { - INTERP_PRINTF("%c", *(char*)VALUE); -} +static GACK(gack_char) { INTERP_PRINTF("%c", *(char *)VALUE); } -static GACK(gack_float) { - INTERP_PRINTF("%.4f", *(m_float*)VALUE); -} +static GACK(gack_float) { INTERP_PRINTF("%.4f", *(m_float *)VALUE); } -static GACK(gack_compound) { - INTERP_PRINTF("%p", *(void**)VALUE); -} -#define mk_class_instr(op, arg0, arg1, ...) \ -static INSTR(instr_class_##op) { \ - POP_REG(shred, SZ_INT); \ - const Type l = *(Type*)(shred->reg - SZ_INT); \ - const Type r = *(Type*)(shred->reg); \ - *(m_uint*)(shred->reg - SZ_INT) = isa(arg0, arg1) > 0 __VA_ARGS__; \ -} -mk_class_instr(ge, l, r) -mk_class_instr(gt, l, r, && l != r) -mk_class_instr(le, r, l) -mk_class_instr(lt, r, l, && l != r) +static GACK(gack_compound) { INTERP_PRINTF("%p", *(void **)VALUE); } +#define mk_class_instr(op, arg0, arg1, ...) \ + static INSTR(instr_class_##op) { \ + POP_REG(shred, SZ_INT); \ + const Type l = *(Type *)(shred->reg - SZ_INT); \ + const Type r = *(Type *)(shred->reg); \ + *(m_uint *)(shred->reg - SZ_INT) = isa(arg0, arg1) > 0 __VA_ARGS__; \ + } +mk_class_instr(ge, l, r) mk_class_instr(gt, l, r, &&l != r) + mk_class_instr(le, r, l) mk_class_instr(lt, r, l, &&l != r) -OP_CHECK(opck_object_dot); + OP_CHECK(opck_object_dot); OP_EMIT(opem_object_dot); static OP_CHECK(opck_basic_ctor) { - const Exp_Call* call = (Exp_Call*)data; + const Exp_Call *call = (Exp_Call *)data; ERR_N(exp_self(call)->pos, _("can't call a non-callable value")); } @@ -79,8 +63,7 @@ static ID_CHECK(idck_predicate) { } static INSTR(PredicateCheck) { - if(!*(m_uint*)REG(-SZ_INT)) - handle(shred, "PredicateFail"); + if (!*(m_uint *)REG(-SZ_INT)) handle(shred, "PredicateFail"); } ANN static m_bool import_core_libs(const Gwi gwi) { @@ -96,7 +79,7 @@ ANN static m_bool import_core_libs(const Gwi gwi) { GWI_BB(gwi_set_global_type(gwi, t_auto, et_auto)) gwidoc(gwi, "a void type."); - const Type t_void = gwi_mk_type(gwi, "void", 0, NULL); + const Type t_void = gwi_mk_type(gwi, "void", 0, NULL); GWI_BB(gwi_gack(gwi, t_void, gack_void)) GWI_BB(gwi_set_global_type(gwi, t_void, et_void)) @@ -133,11 +116,14 @@ ANN static m_bool import_core_libs(const Gwi gwi) { gwidoc(gwi, "internal time for `{/}now{0}{-}`."); const Type t_now = gwi_mk_type(gwi, "@now", SZ_FLOAT, "time"); GWI_BB(gwi_add_type(gwi, t_now)) - struct SpecialId_ spid = { .type=t_now, .exec=RegPushNow, .is_const=1 }; + struct SpecialId_ spid = {.type = t_now, .exec = RegPushNow, .is_const = 1}; gwi_specialid(gwi, "now", &spid); gwidoc(gwi, "internal predicate representation."); - struct SpecialId_ predicate = { .type=t_void, .ck=idck_predicate, .exec=PredicateCheck, .is_const=1 }; + struct SpecialId_ predicate = {.type = t_void, + .ck = idck_predicate, + .exec = PredicateCheck, + .is_const = 1}; gwi_specialid(gwi, "@predicate", &predicate); gwidoc(gwi, "internal base of all objects and structures."); @@ -193,9 +179,9 @@ ANN static m_bool import_core_libs(const Gwi gwi) { GWI_BB(gwi_oper_end(gwi, "==", int_eq)) GWI_BB(gwi_oper_end(gwi, "!=", int_neq)) GWI_BB(gwi_oper_end(gwi, ">=", instr_class_ge)) - GWI_BB(gwi_oper_end(gwi, ">", instr_class_gt)) + GWI_BB(gwi_oper_end(gwi, ">", instr_class_gt)) GWI_BB(gwi_oper_end(gwi, "<=", instr_class_le)) - GWI_BB(gwi_oper_end(gwi, "<", instr_class_lt)) + GWI_BB(gwi_oper_end(gwi, "<", instr_class_lt)) gwidoc(gwi, "internal constructor operator."); GWI_BB(gwi_oper_ini(gwi, NULL, (m_str)OP_ANY_TYPE, NULL)) diff --git a/src/lib/event.c b/src/lib/event.c index d8b4d376..2b1cc33e 100644 --- a/src/lib/event.c +++ b/src/lib/event.c @@ -9,43 +9,37 @@ #include "import.h" #include "gwi.h" -static CTOR(event_ctor) { - vector_init(&EV_SHREDS(o)); -} +static CTOR(event_ctor) { vector_init(&EV_SHREDS(o)); } -static DTOR(event_dtor) { - vector_release(&EV_SHREDS(o)); -} +static DTOR(event_dtor) { vector_release(&EV_SHREDS(o)); } static INSTR(EventWait) { POP_REG(shred, SZ_FLOAT); - const M_Object event = *(M_Object*)REG(-SZ_INT); + const M_Object event = *(M_Object *)REG(-SZ_INT); shreduler_remove(shred->tick->shreduler, shred, false); const Vector v = &EV_SHREDS(event); vector_add(v, (vtype)shred); - *(m_int*)REG(-SZ_INT) = 1; + *(m_int *)REG(-SZ_INT) = 1; } static MFUN(event_signal) { - const Vector v = &EV_SHREDS(o); + const Vector v = &EV_SHREDS(o); const VM_Shred sh = (VM_Shred)vector_front(v); - if(sh) { + if (sh) { shredule(sh->tick->shreduler, sh, GWION_EPSILON); vector_rem(v, 0); } } ANN void broadcast(const M_Object o) { - for(m_uint i = 0; i < vector_size(&EV_SHREDS(o)); i++) { + 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->tick->shreduler, sh, GWION_EPSILON); } vector_clear(&EV_SHREDS(o)); } -static MFUN(event_broadcast) { - broadcast(o); -} +static MFUN(event_broadcast) { broadcast(o); } GWION_IMPORT(event) { const Type t_event = gwi_class_ini(gwi, "Event", "Object"); diff --git a/src/lib/instr.c b/src/lib/instr.c index ae30ed7f..f843970f 100644 --- a/src/lib/instr.c +++ b/src/lib/instr.c @@ -15,134 +15,132 @@ #include "template.h" INSTR(DTOR_EOC) { - const M_Object o = *(M_Object*)MEM(0); - o->type_ref = o->type_ref->info->parent; + const M_Object o = *(M_Object *)MEM(0); + o->type_ref = o->type_ref->info->parent; _release(o, shred); shred->info->me->ref = 1; vm_shred_exit(shred); } -ANN static Func_Def from_base(const Env env, struct dottmpl_ *const dt, const Nspc nspc) { +ANN static Func_Def from_base(const Env env, struct dottmpl_ *const dt, + const Nspc nspc) { const Func_Def fdef = dt->def ?: dt->base; - const Symbol sym = func_symbol(env, nspc->name, s_name(fdef->base->xid), - "template", dt->base->base->tmpl->base); - DECL_OO(const Value, v, = nspc_lookup_value0(nspc, sym) ?: nspc_lookup_value0(nspc, fdef->base->xid)); - if(is_class(env->gwion, v->type)) - return NULL; - if(vflag(v, vflag_builtin)) { - dt->xfun = v->d.func_ref->def->d.dl_func_ptr; + const Symbol sym = func_symbol(env, nspc->name, s_name(fdef->base->xid), + "template", dt->base->base->tmpl->base); + DECL_OO(const Value, v, = nspc_lookup_value0(nspc, sym) + ?: nspc_lookup_value0(nspc, fdef->base->xid)); + if (is_class(env->gwion, v->type)) return NULL; + if (vflag(v, vflag_builtin)) { + dt->xfun = v->d.func_ref->def->d.dl_func_ptr; v->d.func_ref->def->d.dl_func_ptr = NULL; } const Func_Def def = cpy_func_def(env->gwion->mp, v->d.func_ref->def); - if(vflag(v, vflag_builtin)) - v->d.func_ref->def->d.dl_func_ptr = dt->xfun; + if (vflag(v, vflag_builtin)) v->d.func_ref->def->d.dl_func_ptr = dt->xfun; def->base->tmpl->call = cpy_type_list(env->gwion->mp, dt->tl); def->base->tmpl->base = dt->base->base->tmpl->base; - dt->def = def; - dt->owner = v->from->owner; - dt->owner_class = v->from->owner_class; + dt->def = def; + dt->owner = v->from->owner; + dt->owner_class = v->from->owner_class; return def; } -ANN static Func_Def traverse_tmpl(const Emitter emit, struct dottmpl_ *const dt, const Nspc nspc) { +ANN static Func_Def traverse_tmpl(const Emitter emit, struct dottmpl_ *const dt, + const Nspc nspc) { DECL_OO(const Func_Def, def, = from_base(emit->env, dt, nspc)); CHECK_BO(traverse_dot_tmpl(emit, dt)); - if(dt->xfun) - builtin_func(emit->gwion->mp, def->base->func, dt->xfun); + if (dt->xfun) builtin_func(emit->gwion->mp, def->base->func, dt->xfun); return def; } INSTR(GTmpl) { - struct dottmpl_ *dt = (struct dottmpl_*)instr->m_val; - const Func f = *(Func*)REG(-SZ_INT); - if(!f) { + struct dottmpl_ *dt = (struct dottmpl_ *)instr->m_val; + const Func f = *(Func *)REG(-SZ_INT); + if (!f) { handle(shred, "EmptyFuncPointerException"); return; } - if(f->code) { - *(VM_Code*)(shred->reg -SZ_INT) = f->code; + if (f->code) { + *(VM_Code *)(shred->reg - SZ_INT) = f->code; return; } - const m_str name = f->name; - const Emitter emit = shred->info->vm->gwion->emit; - emit->env->name = "runtime"; - struct loc_t_ pos = {}; - m_str tmpl_name = tl2str(emit->gwion, dt->tl, pos); - for(m_uint i = 0 ; i <= f->value_ref->from->offset; ++i) { - const Symbol sym = func_symbol(emit->env, f->value_ref->from->owner->name, - name, tmpl_name, i); - const Func base = nspc_lookup_func0(f->value_ref->from->owner, sym); - if(base) { + const m_str name = f->name; + const Emitter emit = shred->info->vm->gwion->emit; + emit->env->name = "runtime"; + struct loc_t_ pos = {}; + m_str tmpl_name = tl2str(emit->gwion, dt->tl, pos); + for (m_uint i = 0; i <= f->value_ref->from->offset; ++i) { + const Symbol sym = func_symbol(emit->env, f->value_ref->from->owner->name, + name, tmpl_name, i); + const Func base = nspc_lookup_func0(f->value_ref->from->owner, sym); + if (base) { free_mstr(emit->gwion->mp, tmpl_name); assert(base->code); - *(VM_Code*)(shred->reg -SZ_INT) = base->code; + *(VM_Code *)(shred->reg - SZ_INT) = base->code; return; } } free_mstr(emit->gwion->mp, tmpl_name); - dt->def = f->def; + dt->def = f->def; const Func_Def def = traverse_tmpl(emit, dt, f->value_ref->from->owner); - if(def) - *(VM_Code*)(shred->reg -SZ_INT) = def->base->func->code; + if (def) + *(VM_Code *)(shred->reg - SZ_INT) = def->base->func->code; else handle(shred, "MissigTmplPtrException"); } INSTR(DotTmpl) { - struct dottmpl_ *dt = (struct dottmpl_*)instr->m_val; - const m_str name = dt->name; - const M_Object o = *(M_Object*)REG(-SZ_INT); - Type t = o->type_ref; + struct dottmpl_ *dt = (struct dottmpl_ *)instr->m_val; + const m_str name = dt->name; + const M_Object o = *(M_Object *)REG(-SZ_INT); + Type t = o->type_ref; do { const Emitter emit = shred->info->vm->gwion->emit; - emit->env->name = "runtime"; + emit->env->name = "runtime"; char str[instr->m_val2 + strlen(t->name) + 1]; strcpy(str, name); strcpy(str + instr->m_val2, t->name); - const Func f = nspc_lookup_func0(t->nspc, insert_symbol(emit->env->gwion->st, str)); - if(f) { - if(!f->code) - continue; - if(vflag(f->value_ref, vflag_member)) - shred->reg += SZ_INT; - *(VM_Code*)(shred->reg-SZ_INT) = f->code; + const Func f = + nspc_lookup_func0(t->nspc, insert_symbol(emit->env->gwion->st, str)); + if (f) { + if (!f->code) continue; + if (vflag(f->value_ref, vflag_member)) shred->reg += SZ_INT; + *(VM_Code *)(shred->reg - SZ_INT) = f->code; return; } else { const Func_Def def = traverse_tmpl(emit, dt, t->nspc); - if(!def) - continue; + if (!def) continue; const Func f = def->base->func; - if(vflag(f->value_ref, vflag_member)) - shred->reg += SZ_INT; - *(VM_Code*)(shred->reg-SZ_INT) = f->code; + if (vflag(f->value_ref, vflag_member)) shred->reg += SZ_INT; + *(VM_Code *)(shred->reg - SZ_INT) = f->code; return; } - } while((t = t->info->parent)); + } while ((t = t->info->parent)); handle(shred, "MissigTmplException"); } -#define VAL (*(m_uint*)(byte + SZ_INT)) -#define FVAL (*(m_float*)(byte + SZ_INT)) -#define VAL2 (*(m_uint*)(byte + SZ_INT*2)) -#define BYTE(a) m_bit *byte = shred->code->bytecode + (shred->pc -1)* SZ_INT*3; *(m_bit*)byte = a; +#define VAL (*(m_uint *)(byte + SZ_INT)) +#define FVAL (*(m_float *)(byte + SZ_INT)) +#define VAL2 (*(m_uint *)(byte + SZ_INT * 2)) +#define BYTE(a) \ + m_bit *byte = shred->code->bytecode + (shred->pc - 1) * SZ_INT * 3; \ + *(m_bit *)byte = a; INSTR(SetFunc) { BYTE(eRegPushImm) const Func f = (Func)instr->m_val; - VAL = *(m_uint*)(shred->reg) = (m_uint)f->code; + VAL = *(m_uint *)(shred->reg) = (m_uint)f->code; shred->reg += SZ_INT; } INSTR(SetRecurs) { BYTE(eRegPushImm) - VAL = *(m_uint*)(shred->reg) = (m_uint)shred->code; + VAL = *(m_uint *)(shred->reg) = (m_uint)shred->code; shred->reg += SZ_INT; } INSTR(SetCtor) { BYTE(eRegSetImm) const Type t = (Type)instr->m_val; - VAL = *(m_uint*)(shred->reg + SZ_INT) = (m_uint)t->nspc->pre_ctor; - VAL2 = SZ_INT; + VAL = *(m_uint *)(shred->reg + SZ_INT) = (m_uint)t->nspc->pre_ctor; + VAL2 = SZ_INT; } - diff --git a/src/lib/lib_func.c b/src/lib/lib_func.c index 992cb370..338b2e45 100644 --- a/src/lib/lib_func.c +++ b/src/lib/lib_func.c @@ -14,36 +14,38 @@ #include "parse.h" static OP_CHECK(opck_func_call) { - Exp_Binary* bin = (Exp_Binary*)data; - Exp_Call call = { .func=bin->rhs, .args=bin->lhs }; - Exp e = exp_self(bin); - e->exp_type = ae_exp_call; + Exp_Binary *bin = (Exp_Binary *)data; + Exp_Call call = {.func = bin->rhs, .args = bin->lhs}; + Exp e = exp_self(bin); + e->exp_type = ae_exp_call; memcpy(&e->d.exp_call, &call, sizeof(Exp_Call)); return check_exp_call1(env, &e->d.exp_call) ?: env->gwion->type[et_error]; } -static inline void fptr_instr(const Emitter emit, const Func f, const m_uint i) { +static inline void fptr_instr(const Emitter emit, const Func f, + const m_uint i) { const Instr set = emit_add_instr(emit, RegSetImm); - set->m_val = (m_uint)f; - set->m_val2 = -SZ_INT*i; + set->m_val = (m_uint)f; + set->m_val2 = -SZ_INT * i; } static OP_EMIT(opem_func_assign) { - Exp_Binary* bin = (Exp_Binary*)data; - if(bin->rhs->type->info->func->def->base->tmpl) + Exp_Binary *bin = (Exp_Binary *)data; + if (bin->rhs->type->info->func->def->base->tmpl) fptr_instr(emit, bin->lhs->type->info->func, 2); (void)emit_add_instr(emit, int_r_assign); - if(!is_fptr(emit->gwion, bin->lhs->type) && vflag(bin->rhs->type->info->func->value_ref, vflag_member)) { + if (!is_fptr(emit->gwion, bin->lhs->type) && + vflag(bin->rhs->type->info->func->value_ref, vflag_member)) { const Instr pop = emit_add_instr(emit, RegMove); - pop->m_val = -SZ_INT; + pop->m_val = -SZ_INT; const Instr cpy = emit_add_instr(emit, Reg2Reg); - cpy->m_val = -SZ_INT; + cpy->m_val = -SZ_INT; } return GW_OK; } struct FptrInfo { - Func lhs; + Func lhs; const Func rhs; const Exp exp; const loc_t pos; @@ -51,13 +53,11 @@ struct FptrInfo { ANN static void _fptr_tmpl_push(const Env env, const Func f) { const Tmpl *tmpl = f->def->base->tmpl; - if(!tmpl) - return; + if (!tmpl) return; Type_List tl = tmpl->call; - if(!tl) - return; + if (!tl) return; Specialized_List il = tmpl->list; - while(il) { + while (il) { const Type t = known_type(env, tl->td); nspc_add_type(env->curr, il->xid, t); il = il->next; @@ -66,16 +66,15 @@ ANN static void _fptr_tmpl_push(const Env env, const Func f) { } ANN static m_bool fptr_tmpl_push(const Env env, struct FptrInfo *info) { -// if(safe_tflag(info->rhs->value_ref->from->owner_class, tflag_tmpl)) - if(!info->rhs->def->base->tmpl) - return GW_OK; + // if(safe_tflag(info->rhs->value_ref->from->owner_class, tflag_tmpl)) + if (!info->rhs->def->base->tmpl) return GW_OK; nspc_push_type(env->gwion->mp, env->curr); -// Type owner = info->rhs->value_ref->from->owner_class; -// while(owner) { -// owner = owner->info->value->from->owner_class; -// } + // Type owner = info->rhs->value_ref->from->owner_class; + // while(owner) { + // owner = owner->info->value->from->owner_class; + // } -// _fptr_tmpl_push(env, info->lhs); + // _fptr_tmpl_push(env, info->lhs); _fptr_tmpl_push(env, info->rhs); return GW_OK; } @@ -83,19 +82,18 @@ ANN static m_bool fptr_tmpl_push(const Env env, struct FptrInfo *info) { static m_bool td_match(const Env env, Type_Decl *id[2]) { DECL_OB(const Type, t0, = known_type(env, id[0])); DECL_OB(const Type, t1, = known_type(env, id[1])); - if(isa(t0, t1) > 0) - return GW_OK; - return t1 == env->gwion->type[et_auto] ? GW_OK:GW_ERROR; + if (isa(t0, t1) > 0) return GW_OK; + return t1 == env->gwion->type[et_auto] ? GW_OK : GW_ERROR; } ANN static m_bool fptr_args(const Env env, Func_Base *base[2]) { Arg_List arg0 = base[0]->args, arg1 = base[1]->args; - while(arg0) { + while (arg0) { CHECK_OB(arg1); - if(arg0->type && arg1->type) + if (arg0->type && arg1->type) CHECK_BB(isa(arg0->type, arg1->type)); else { - Type_Decl* td[2] = { arg0->td, arg1->td }; + Type_Decl *td[2] = {arg0->td, arg1->td}; CHECK_BB(td_match(env, td)); } arg0 = arg0->next; @@ -105,16 +103,15 @@ ANN static m_bool fptr_args(const Env env, Func_Base *base[2]) { } ANN static bool fptr_effects(const Env env, struct FptrInfo *info) { - if(!info->lhs->def->base->effects.ptr) - return true; - if(!info->rhs->def->base->effects.ptr) { + if (!info->lhs->def->base->effects.ptr) return true; + if (!info->rhs->def->base->effects.ptr) { gwerr_secondary("too many effects", env->name, info->pos); return false; } const Vector lhs = &info->lhs->def->base->effects; const Vector rhs = &info->rhs->def->base->effects; - for(m_uint i = 0; i < vector_size(lhs); i++) { - if(vector_find(rhs, vector_at(lhs, 0)) == -1) { + for (m_uint i = 0; i < vector_size(lhs); i++) { + if (vector_find(rhs, vector_at(lhs, 0)) == -1) { gwerr_secondary("effect not handled", env->name, info->pos); return false; } @@ -123,28 +120,32 @@ ANN static bool fptr_effects(const Env env, struct FptrInfo *info) { } ANN static m_bool fptr_check(const Env env, struct FptrInfo *info) { -// if(!info->lhs->def->base->tmpl != !info->rhs->def->base->tmpl) -// return GW_ERROR; + // if(!info->lhs->def->base->tmpl != !info->rhs->def->base->tmpl) + // return GW_ERROR; const Type l_type = info->lhs->value_ref->from->owner_class; const Type r_type = info->rhs->value_ref->from->owner_class; - if(!r_type && l_type) - ERR_B(info->pos, _("can't assign member function to non member function pointer")) - else if(!l_type && r_type) { - if(!GET_FLAG(info->rhs, global)) - ERR_B(info->pos, _("can't assign non member function to member function pointer")) - } else if(l_type && isa(r_type, l_type) < 0) - ERR_B(info->pos, _("can't assign member function to a pointer of an other class")) - if(vflag(info->rhs->value_ref, vflag_member)) { - if(!vflag(info->lhs->value_ref, vflag_member)) - ERR_B(info->pos, _("can't assign static function to member function pointer")) - } else if(vflag(info->lhs->value_ref, vflag_member)) - ERR_B(info->pos, _("can't assign member function to static function pointer")) + if (!r_type && l_type) + ERR_B(info->pos, + _("can't assign member function to non member function pointer")) + else if (!l_type && r_type) { + if (!GET_FLAG(info->rhs, global)) + ERR_B(info->pos, + _("can't assign non member function to member function pointer")) + } else if (l_type && isa(r_type, l_type) < 0) + ERR_B(info->pos, + _("can't assign member function to a pointer of an other class")) + if (vflag(info->rhs->value_ref, vflag_member)) { + if (!vflag(info->lhs->value_ref, vflag_member)) + ERR_B(info->pos, + _("can't assign static function to member function pointer")) + } else if (vflag(info->lhs->value_ref, vflag_member)) + ERR_B(info->pos, + _("can't assign member function to static function pointer")) return GW_OK; } ANN static inline m_bool fptr_rettype(const Env env, struct FptrInfo *info) { - Type_Decl* td[2] = { info->lhs->def->base->td, - info->rhs->def->base->td }; + Type_Decl *td[2] = {info->lhs->def->base->td, info->rhs->def->base->td}; return td_match(env, td); } @@ -154,67 +155,75 @@ ANN static inline m_bool fptr_arity(struct FptrInfo *info) { } ANN static Type fptr_type(const Env env, struct FptrInfo *info) { - const Value v = info->lhs->value_ref; - const Nspc nspc = v->from->owner; - const m_str c = s_name(info->lhs->def->base->xid), - stmpl = !info->rhs->def->base->tmpl ? NULL : "template"; - for(m_uint i = 0; i <= v->from->offset; ++i) { - const Symbol sym = (!info->lhs->def->base->tmpl || i != 0) ? - func_symbol(env, nspc->name, c, stmpl, i) : info->lhs->def->base->xid; - if(!is_class(env->gwion, info->lhs->value_ref->type)) { - if(!(info->lhs = nspc_lookup_func1(nspc, sym))) { + const Value v = info->lhs->value_ref; + const Nspc nspc = v->from->owner; + const m_str c = s_name(info->lhs->def->base->xid), + stmpl = !info->rhs->def->base->tmpl ? NULL : "template"; + for (m_uint i = 0; i <= v->from->offset; ++i) { + const Symbol sym = (!info->lhs->def->base->tmpl || i != 0) + ? func_symbol(env, nspc->name, c, stmpl, i) + : info->lhs->def->base->xid; + if (!is_class(env->gwion, info->lhs->value_ref->type)) { + if (!(info->lhs = nspc_lookup_func1(nspc, sym))) { const Value v = nspc_lookup_value1(nspc, insert_symbol(c)); - if(isa(v->type, env->gwion->type[et_function]) < 0) - return NULL; + if (isa(v->type, env->gwion->type[et_function]) < 0) return NULL; info->lhs = v->type->info->func; } } else { - DECL_OO(const Type, t, = nspc_lookup_type1(nspc, info->lhs->def->base->xid)); + DECL_OO(const Type, t, + = nspc_lookup_type1(nspc, info->lhs->def->base->xid)); info->lhs = actual_type(env->gwion, t)->info->func; } - Type type = NULL; - Func_Base *base[2] = { info->lhs->def->base, info->rhs->def->base }; + Type type = NULL; + Func_Base *base[2] = {info->lhs->def->base, info->rhs->def->base}; CHECK_BO(fptr_tmpl_push(env, info)); if (fptr_rettype(env, info) > 0 && fptr_arity(info) && fptr_args(env, base) > 0 && fptr_effects(env, info)) - type = actual_type(env->gwion, info->lhs->value_ref->type) ?: info->lhs->value_ref->type; - if(info->rhs->def->base->tmpl) - nspc_pop_type(env->gwion->mp, env->curr); - if(type)return type; + type = actual_type(env->gwion, info->lhs->value_ref->type) + ?: info->lhs->value_ref->type; + if (info->rhs->def->base->tmpl) nspc_pop_type(env->gwion->mp, env->curr); + if (type) return type; } return NULL; } -ANN static m_bool _check_lambda(const Env env, Exp_Lambda *l, const Func_Def def) { -//if(l->def->base->func)return GW_OK; - const bool is_tmpl = safe_tflag(def->base->func->value_ref->from->owner_class, tflag_tmpl); - if(is_tmpl) - template_push_types(env, def->base->func->value_ref->from->owner_class->info->cdef->base.tmpl); +ANN static m_bool _check_lambda(const Env env, Exp_Lambda *l, + const Func_Def def) { + // if(l->def->base->func)return GW_OK; + const bool is_tmpl = + safe_tflag(def->base->func->value_ref->from->owner_class, tflag_tmpl); + if (is_tmpl) + template_push_types( + env, + def->base->func->value_ref->from->owner_class->info->cdef->base.tmpl); Arg_List base = def->base->args, arg = l->def->base->args; - while(base && arg) { + while (base && arg) { arg->td = type2td(env->gwion, known_type(env, base->td), exp_self(l)->pos); - base = base->next; - arg = arg->next; + base = base->next; + arg = arg->next; } - l->def->base->td = type2td(env->gwion, known_type(env, def->base->td), exp_self(l)->pos); - if(is_tmpl) - nspc_pop_type(env->gwion->mp, env->curr); - if(base || arg) + l->def->base->td = + type2td(env->gwion, known_type(env, def->base->td), exp_self(l)->pos); + if (is_tmpl) nspc_pop_type(env->gwion->mp, env->curr); + if (base || arg) ERR_B(exp_self(l)->pos, _("argument number does not match for lambda")) l->def->base->flag = def->base->flag; -// if(GET_FLAG(def->base, global) && !l->owner && def->base->func->value_ref->from->owner_class) - UNSET_FLAG(l->def->base, global); + // if(GET_FLAG(def->base, global) && !l->owner && + // def->base->func->value_ref->from->owner_class) + UNSET_FLAG(l->def->base, global); l->def->base->values = env->curr->info->value; - const m_uint scope = env->scope->depth; -// if(GET_FLAG(def->base, global) && !l->owner && def->base->func->value_ref->from->owner_class) -//env_push(env, NULL, env->context->nspc); + const m_uint scope = env->scope->depth; + // if(GET_FLAG(def->base, global) && !l->owner && + // def->base->func->value_ref->from->owner_class) + // env_push(env, NULL, env->context->nspc); env->scope->depth = 0; - const m_bool ret = traverse_func_def(env, l->def); + const m_bool ret = traverse_func_def(env, l->def); env->scope->depth = scope; -// if(GET_FLAG(def->base, global) && !l->owner && def->base->func->value_ref->from->owner_class) -//env_pop(env, scope); - if(l->def->base->func) { - if(env->curr->info->value != l->def->base->values) { + // if(GET_FLAG(def->base, global) && !l->owner && + // def->base->func->value_ref->from->owner_class) + // env_pop(env, scope); + if (l->def->base->func) { + if (env->curr->info->value != l->def->base->values) { free_scope(env->gwion->mp, env->curr->info->value); env->curr->info->value = l->def->base->values; } @@ -224,7 +233,7 @@ ANN static m_bool _check_lambda(const Env env, Exp_Lambda *l, const Func_Def def ANN m_bool check_lambda(const Env env, const Type t, Exp_Lambda *l) { const Func_Def fdef = t->info->func->def; - if(!GET_FLAG(t->info->func->value_ref, global)) + if (!GET_FLAG(t->info->func->value_ref, global)) l->owner = t->info->value->from->owner_class; CHECK_BB(_check_lambda(env, l, fdef)); exp_self(l)->type = l->def->base->func->value_ref->type; @@ -232,71 +241,78 @@ ANN m_bool check_lambda(const Env env, const Type t, Exp_Lambda *l) { } ANN static m_bool fptr_do(const Env env, struct FptrInfo *info) { - if(isa(info->exp->type, env->gwion->type[et_lambda]) < 0) { + if (isa(info->exp->type, env->gwion->type[et_lambda]) < 0) { CHECK_BB(fptr_check(env, info)); - if(!(info->exp->type = fptr_type(env, info))) + if (!(info->exp->type = fptr_type(env, info))) ERR_B(info->pos, _("no match found")) return GW_OK; } Exp_Lambda *l = &info->exp->d.exp_lambda; - return check_lambda(env, actual_type(env->gwion, info->rhs->value_ref->type), l); + return check_lambda(env, actual_type(env->gwion, info->rhs->value_ref->type), + l); } static OP_CHECK(opck_auto_fptr) { - const Exp_Binary* bin = (Exp_Binary*)data; + const Exp_Binary *bin = (Exp_Binary *)data; // we'll only deal with auto fptr declaration - if(bin->rhs->exp_type != ae_exp_decl && bin->rhs->d.exp_decl.td->xid != insert_symbol("auto")) + if (bin->rhs->exp_type != ae_exp_decl && + bin->rhs->d.exp_decl.td->xid != insert_symbol("auto")) return env->gwion->type[et_error]; // create a matching signature // TODO: we could check first if there a matching existing one - Func_Base *const fbase = cpy_func_base(env->gwion->mp, bin->lhs->type->info->func->def->base); + Func_Base *const fbase = + cpy_func_base(env->gwion->mp, bin->lhs->type->info->func->def->base); const Fptr_Def fptr_def = new_fptr_def(env->gwion->mp, fbase); - char name[13 + strlen(env->curr->name) + - num_digit(bin->rhs->pos.first.line) + num_digit(bin->rhs->pos.first.column)]; - sprintf(name, "generated@%s@%u:%u", env->curr->name, bin->rhs->pos.first.line, bin->rhs->pos.first.column); + char name[13 + strlen(env->curr->name) + num_digit(bin->rhs->pos.first.line) + + num_digit(bin->rhs->pos.first.column)]; + sprintf(name, "generated@%s@%u:%u", env->curr->name, bin->rhs->pos.first.line, + bin->rhs->pos.first.column); fptr_def->base->xid = insert_symbol(name); - const m_bool ret = traverse_fptr_def(env, fptr_def); - const Type t = fptr_def->type; + const m_bool ret = traverse_fptr_def(env, fptr_def); + const Type t = fptr_def->type; free_fptr_def(env->gwion->mp, fptr_def); type_remref(t, env->gwion); - bin->rhs->d.exp_decl.list->self->value->type = bin->rhs->type = bin->rhs->d.exp_decl.type = t; + bin->rhs->d.exp_decl.list->self->value->type = bin->rhs->type = + bin->rhs->d.exp_decl.type = t; exp_setvar(bin->rhs, 1); return ret > 0 ? t : env->gwion->type[et_error]; } static OP_CHECK(opck_fptr_at) { - Exp_Binary* bin = (Exp_Binary*)data; - if(bin->rhs->exp_type == ae_exp_decl) + Exp_Binary *bin = (Exp_Binary *)data; + if (bin->rhs->exp_type == ae_exp_decl) UNSET_FLAG(bin->rhs->d.exp_decl.list->self->value, late); - if(bin->rhs->type->info->func->def->base->tmpl && - bin->rhs->type->info->func->def->base->tmpl->call) { - struct FptrInfo info = { bin->lhs->type->info->func, bin->rhs->type->info->parent->info->func, - bin->lhs, exp_self(bin)->pos }; + if (bin->rhs->type->info->func->def->base->tmpl && + bin->rhs->type->info->func->def->base->tmpl->call) { + struct FptrInfo info = {bin->lhs->type->info->func, + bin->rhs->type->info->parent->info->func, bin->lhs, + exp_self(bin)->pos}; CHECK_BN(fptr_do(env, &info)); exp_setvar(bin->rhs, 1); return bin->rhs->type; } - struct FptrInfo info = { bin->lhs->type->info->func, bin->rhs->type->info->func, - bin->lhs, exp_self(bin)->pos }; + struct FptrInfo info = {bin->lhs->type->info->func, + bin->rhs->type->info->func, bin->lhs, + exp_self(bin)->pos}; CHECK_BN(fptr_do(env, &info)); exp_setvar(bin->rhs, 1); return bin->rhs->type; } static OP_CHECK(opck_fptr_cast) { - Exp_Cast* cast = (Exp_Cast*)data; - const Type t = exp_self(cast)->type; - struct FptrInfo info = { cast->exp->type->info->func, t->info->func, - cast->exp, exp_self(cast)->pos }; + Exp_Cast * cast = (Exp_Cast *)data; + const Type t = exp_self(cast)->type; + struct FptrInfo info = {cast->exp->type->info->func, t->info->func, cast->exp, + exp_self(cast)->pos}; CHECK_BN(fptr_do(env, &info)); return t; } static void member_fptr(const Emitter emit) { const Instr instr = emit_add_instr(emit, RegMove); - instr->m_val = -SZ_INT; - const Instr dup = emit_add_instr(emit, Reg2Reg); - dup->m_val = -SZ_INT; + instr->m_val = -SZ_INT; + const Instr dup = emit_add_instr(emit, Reg2Reg); + dup->m_val = -SZ_INT; } static inline int is_member(const Type from) { @@ -304,34 +320,33 @@ static inline int is_member(const Type from) { } static OP_EMIT(opem_fptr_cast) { - const Exp_Cast* cast = (Exp_Cast*)data; - if(is_member(cast->exp->type)) - member_fptr(emit); - if(exp_self(cast)->type->info->func->def->base->tmpl) + const Exp_Cast *cast = (Exp_Cast *)data; + if (is_member(cast->exp->type)) member_fptr(emit); + if (exp_self(cast)->type->info->func->def->base->tmpl) fptr_instr(emit, cast->exp->type->info->func, 1); return GW_OK; } static OP_CHECK(opck_fptr_impl) { - struct Implicit *impl = (struct Implicit*)data; - struct FptrInfo info = { impl->e->type->info->func, impl->t->info->func, - impl->e, impl->e->pos }; + struct Implicit *impl = (struct Implicit *)data; + struct FptrInfo info = {impl->e->type->info->func, impl->t->info->func, + impl->e, impl->e->pos}; CHECK_BN(fptr_do(env, &info)); return impl->t; } // smh the VM should be able to do that static INSTR(Func2Code) { - *(VM_Code*)REG(-SZ_INT) = (*(Func*)REG(-SZ_INT))->code; + *(VM_Code *)REG(-SZ_INT) = (*(Func *)REG(-SZ_INT))->code; } static OP_EMIT(opem_fptr_impl) { - struct Implicit *impl = (struct Implicit*)data; - if(is_member(impl->e->type)) - member_fptr(emit); - if(impl->t->info->func->def->base->tmpl) { + struct Implicit *impl = (struct Implicit *)data; + if (is_member(impl->e->type)) member_fptr(emit); + if (impl->t->info->func->def->base->tmpl) { fptr_instr(emit, ((Exp)impl->e)->type->info->func, 1); - if(!is_fptr(emit->gwion, impl->e->type) && safe_tflag(impl->t->info->value->from->owner_class, tflag_tmpl)){ + if (!is_fptr(emit->gwion, impl->e->type) && + safe_tflag(impl->t->info->value->from->owner_class, tflag_tmpl)) { emit_add_instr(emit, Func2Code); } } @@ -339,123 +354,134 @@ static OP_EMIT(opem_fptr_impl) { } static void op_narg_err(const Env env, const Func_Def fdef, const loc_t loc) { - if(!env->context->error) { - gwerr_basic(_("invalid operator decay"), _("Decayed operators take two arguments"), NULL, - env->name, loc, 0); - if(fdef) - gwerr_secondary("declared here", env->name, fdef->base->pos); + if (!env->context->error) { + gwerr_basic(_("invalid operator decay"), + _("Decayed operators take two arguments"), NULL, env->name, loc, + 0); + if (fdef) gwerr_secondary("declared here", env->name, fdef->base->pos); env->context->error = true; } } -static m_bool op_call_narg(const Env env, Exp arg, - const loc_t loc) { +static m_bool op_call_narg(const Env env, Exp arg, const loc_t loc) { m_uint narg = 0; - while(arg) { + while (arg) { narg++; arg = arg->next; } - if(narg == 2) - return GW_OK; + if (narg == 2) return GW_OK; op_narg_err(env, NULL, loc); return GW_ERROR; } ANN Type check_op_call(const Env env, Exp_Call *const exp) { CHECK_BO(op_call_narg(env, exp->args, exp->func->pos)); - const Exp base = exp_self(exp); + const Exp base = exp_self(exp); const Exp op_exp = exp->func; - base->exp_type = ae_exp_binary; - Exp_Binary *bin = &base->d.exp_binary; - bin->lhs = exp->args; - bin->lhs = exp->args->next; - exp->args->next = NULL; - bin->op = op_exp->d.prim.d.var; + base->exp_type = ae_exp_binary; + Exp_Binary *bin = &base->d.exp_binary; + bin->lhs = exp->args; + bin->lhs = exp->args->next; + exp->args->next = NULL; + bin->op = op_exp->d.prim.d.var; free_exp(env->gwion->mp, op_exp); return check_exp(env, base); } static m_bool op_impl_narg(const Env env, const Func_Def fdef, - const loc_t loc) { - m_uint narg = 0; - Arg_List arg = fdef->base->args; - while(arg) { + const loc_t loc) { + m_uint narg = 0; + Arg_List arg = fdef->base->args; + while (arg) { narg++; arg = arg->next; } - if(narg == 2) - return GW_OK; + if (narg == 2) return GW_OK; op_narg_err(env, fdef, loc); return GW_ERROR; } static inline void op_impl_ensure_types(const Env env, const Func func) { - Arg_List arg = func->def->base->args; - const bool owner_tmpl = safe_tflag(func->value_ref->from->owner_class, tflag_tmpl); + Arg_List arg = func->def->base->args; + const bool owner_tmpl = + safe_tflag(func->value_ref->from->owner_class, tflag_tmpl); const bool func_tmpl = fflag(func, fflag_tmpl); - if(owner_tmpl) - template_push_types(env, func->value_ref->from->owner_class->info->cdef->base.tmpl); - if(func_tmpl) - template_push_types(env, func->def->base->tmpl); - while(arg) { - if(!arg->type) - arg->type = known_type(env, arg->td); + if (owner_tmpl) + template_push_types( + env, func->value_ref->from->owner_class->info->cdef->base.tmpl); + if (func_tmpl) template_push_types(env, func->def->base->tmpl); + while (arg) { + if (!arg->type) arg->type = known_type(env, arg->td); arg = arg->next; } - if(!func->def->base->ret_type) + if (!func->def->base->ret_type) func->def->base->ret_type = known_type(env, func->def->base->td); - if(owner_tmpl) - nspc_pop_type(env->gwion->mp, env->curr); - if(func_tmpl) - nspc_pop_type(env->gwion->mp, env->curr); + if (owner_tmpl) nspc_pop_type(env->gwion->mp, env->curr); + if (func_tmpl) nspc_pop_type(env->gwion->mp, env->curr); } #include "tmp_resolve.h" -static OP_CHECK(opck_op_impl){ - struct Implicit *impl = (struct Implicit*)data; - const Func func = impl->t->info->func; +static OP_CHECK(opck_op_impl) { + struct Implicit *impl = (struct Implicit *)data; + const Func func = impl->t->info->func; CHECK_BN(op_impl_narg(env, func->def, impl->e->pos)); op_impl_ensure_types(env, func); const Symbol lhs_sym = insert_symbol("@lhs"); const Symbol rhs_sym = insert_symbol("@rhs"); - struct Exp_ _lhs = { .d={ .prim={ .d={.var=lhs_sym} , .prim_type=ae_prim_id }}, .exp_type=ae_exp_primary, .type=func->def->base->args->type, .pos=func->def->base->args->td->pos }; - struct Exp_ _rhs = { .d={ .prim={ .d={.var=rhs_sym} , .prim_type=ae_prim_id }}, .exp_type=ae_exp_primary, .type=func->def->base->args->next->type, .pos=func->def->base->args->next->td->pos }; - struct Exp_ self = { .pos=impl->e->pos }; -// Exp_Binary _bin = { .lhs=&_lhs, .op=impl->e->d.prim.d.var, .rhs=&_rhs };// .lhs=func->def->base->args // TODO + struct Exp_ _lhs = { + .d = {.prim = {.d = {.var = lhs_sym}, .prim_type = ae_prim_id}}, + .exp_type = ae_exp_primary, + .type = func->def->base->args->type, + .pos = func->def->base->args->td->pos}; + struct Exp_ _rhs = { + .d = {.prim = {.d = {.var = rhs_sym}, .prim_type = ae_prim_id}}, + .exp_type = ae_exp_primary, + .type = func->def->base->args->next->type, + .pos = func->def->base->args->next->td->pos}; + struct Exp_ self = {.pos = impl->e->pos}; + // Exp_Binary _bin = { .lhs=&_lhs, .op=impl->e->d.prim.d.var, .rhs=&_rhs };// + // .lhs=func->def->base->args // TODO self.d.exp_binary.lhs = &_lhs; self.d.exp_binary.rhs = &_rhs; - self.d.exp_binary.op = impl->e->d.prim.d.var; - struct Op_Import opi = { .op=impl->e->d.prim.d.var, .lhs=func->def->base->args->type, - .rhs=func->def->base->args->next->type, .data=(uintptr_t)&self.d.exp_binary, .pos=impl->e->pos }; + self.d.exp_binary.op = impl->e->d.prim.d.var; + struct Op_Import opi = {.op = impl->e->d.prim.d.var, + .lhs = func->def->base->args->type, + .rhs = func->def->base->args->next->type, + .data = (uintptr_t)&self.d.exp_binary, + .pos = impl->e->pos}; vector_add(&env->scope->effects, 0); DECL_ON(const Type, t, = op_check(env, &opi)); CHECK_BN(isa(t, func->def->base->ret_type)); // error message? const m_uint _eff = vector_back(&env->scope->effects); - if(!check_effect_overload((Vector)&_eff, func)) - ERR_N(impl->pos, _("`{+Y}%s{0}` has effects not present in `{+G}%s{0}`\n"), s_name(impl->e->d.prim.d.var), func->name); + if (!check_effect_overload((Vector)&_eff, func)) + ERR_N(impl->pos, _("`{+Y}%s{0}` has effects not present in `{+G}%s{0}`\n"), + s_name(impl->e->d.prim.d.var), func->name); Value v = nspc_lookup_value0(opi.nspc, impl->e->d.prim.d.var); - if(v) { - const m_uint scope = env_push(env, NULL, opi.nspc); - _lhs.next = &_rhs; - Exp_Call call = { .args=&_lhs }; - const Func exists = (Func)find_func_match(env, v->d.func_ref, &call); - env_pop(env, scope); - if(exists) { // improve me - if(_eff) { - const M_Vector eff = (M_Vector)&_eff; - m_vector_release(eff); - ERR_N(impl->pos, _("`{+Y}%s{0}` has effects not present in `{+G}%s{0}`\n"), s_name(impl->e->d.prim.d.var), func->name); - } - return actual_type(env->gwion, func->value_ref->type); + if (v) { + const m_uint scope = env_push(env, NULL, opi.nspc); + _lhs.next = &_rhs; + Exp_Call call = {.args = &_lhs}; + const Func exists = (Func)find_func_match(env, v->d.func_ref, &call); + env_pop(env, scope); + if (exists) { // improve me + if (_eff) { + const M_Vector eff = (M_Vector)&_eff; + m_vector_release(eff); + ERR_N(impl->pos, + _("`{+Y}%s{0}` has effects not present in `{+G}%s{0}`\n"), + s_name(impl->e->d.prim.d.var), func->name); } + return actual_type(env->gwion, func->value_ref->type); + } } const Arg_List args = cpy_arg_list(env->gwion->mp, func->def->base->args); // beware shadowing ? - args->var_decl->xid = lhs_sym; + args->var_decl->xid = lhs_sym; args->next->var_decl->xid = rhs_sym; - Func_Base *base = new_func_base(env->gwion->mp, type2td(env->gwion, t, impl->e->pos), - impl->e->d.prim.d.var, args, ae_flag_none, impl->e->pos); - if(_eff) { + Func_Base *base = + new_func_base(env->gwion->mp, type2td(env->gwion, t, impl->e->pos), + impl->e->d.prim.d.var, args, ae_flag_none, impl->e->pos); + if (_eff) { const M_Vector eff = (M_Vector)&_eff; for (m_uint i = 0; i < m_vector_size(eff); i++) { struct ScopeEffect effect; @@ -464,57 +490,61 @@ static OP_CHECK(opck_op_impl){ } m_vector_release(eff); } - const Exp lhs = new_prim_id(env->gwion->mp, args->var_decl->xid, impl->e->pos); - const Exp rhs = new_prim_id(env->gwion->mp, args->next->var_decl->xid, impl->e->pos); - const Exp bin = new_exp_binary(env->gwion->mp, lhs, impl->e->d.prim.d.var, rhs, impl->e->pos); - const Stmt stmt = new_stmt_exp(env->gwion->mp, ae_stmt_return, bin, impl->e->pos); + const Exp lhs = + new_prim_id(env->gwion->mp, args->var_decl->xid, impl->e->pos); + const Exp rhs = + new_prim_id(env->gwion->mp, args->next->var_decl->xid, impl->e->pos); + const Exp bin = new_exp_binary(env->gwion->mp, lhs, impl->e->d.prim.d.var, + rhs, impl->e->pos); + const Stmt stmt = + new_stmt_exp(env->gwion->mp, ae_stmt_return, bin, impl->e->pos); const Stmt_List list = new_stmt_list(env->gwion->mp, stmt, NULL); - const Stmt code = new_stmt_code(env->gwion->mp, list, impl->e->pos); - const Func_Def def = new_func_def(env->gwion->mp, base, code); - def->base->xid = impl->e->d.prim.d.var; - const m_uint scope = env_push(env, NULL, opi.nspc); + const Stmt code = new_stmt_code(env->gwion->mp, list, impl->e->pos); + const Func_Def def = new_func_def(env->gwion->mp, base, code); + def->base->xid = impl->e->d.prim.d.var; + const m_uint scope = env_push(env, NULL, opi.nspc); // we assume succes here - /*const m_bool ret = */traverse_func_def(env, def); + /*const m_bool ret = */ traverse_func_def(env, def); env_pop(env, scope); def->base->func->value_ref->type->info->parent = env->gwion->type[et_op]; - impl->e->type = def->base->func->value_ref->type; + impl->e->type = def->base->func->value_ref->type; impl->e->d.prim.value = def->base->func->value_ref; return actual_type(env->gwion, func->value_ref->type); } static OP_EMIT(opem_op_impl) { - struct Implicit *impl = (struct Implicit*)data; - const Func_Def fdef = impl->e->type->info->func->def; - const m_bool ret = emit_func_def(emit, fdef); - const Instr instr = emit_add_instr(emit, RegSetImm); - instr->m_val = (m_uint)fdef->base->func->code; - instr->m_val2 = -SZ_INT; + struct Implicit *impl = (struct Implicit *)data; + const Func_Def fdef = impl->e->type->info->func->def; + const m_bool ret = emit_func_def(emit, fdef); + const Instr instr = emit_add_instr(emit, RegSetImm); + instr->m_val = (m_uint)fdef->base->func->code; + instr->m_val2 = -SZ_INT; return ret; } ANN Type check_exp_unary_spork(const Env env, const Stmt code); -ANN static void fork_exp(const Env env, const Exp_Unary* unary) { - const Stmt stmt = new_stmt_exp(env->gwion->mp, ae_stmt_exp, unary->exp, unary->exp->pos); +ANN static void fork_exp(const Env env, const Exp_Unary *unary) { + const Stmt stmt = + new_stmt_exp(env->gwion->mp, ae_stmt_exp, unary->exp, unary->exp->pos); const Stmt_List list = new_stmt_list(env->gwion->mp, stmt, NULL); - const Stmt code = new_stmt_code(env->gwion->mp, list, unary->exp->pos); - ((Exp_Unary*)unary)->exp = NULL; - ((Exp_Unary*)unary)->code = code; - ((Exp_Unary*)unary)->unary_type = unary_code; + const Stmt code = new_stmt_code(env->gwion->mp, list, unary->exp->pos); + ((Exp_Unary *)unary)->exp = NULL; + ((Exp_Unary *)unary)->code = code; + ((Exp_Unary *)unary)->unary_type = unary_code; } -ANN static Type fork_type(const Env env, const Exp_Unary* unary) { +ANN static Type fork_type(const Env env, const Exp_Unary *unary) { const Type t = unary->exp->type; fork_exp(env, unary); - if(t == env->gwion->type[et_void]) - return env->gwion->type[et_fork]; + if (t == env->gwion->type[et_void]) return env->gwion->type[et_fork]; char c[21 + strlen(t->name)]; sprintf(c, "TypedFork:[%s]", t->name); const Type fork = env->gwion->type[et_fork]; UNSET_FLAG(fork, final); const Type typed = str2type(env->gwion, "TypedFork", exp_self(unary)->pos); // ensure room for retval - if(typed->nspc->info->offset == fork->nspc->info->offset) + if (typed->nspc->info->offset == fork->nspc->info->offset) typed->nspc->info->offset += t->size; UNSET_FLAG(typed, final); const Type ret = str2type(env->gwion, c, exp_self(unary)->pos); @@ -524,36 +554,34 @@ ANN static Type fork_type(const Env env, const Exp_Unary* unary) { } static OP_CHECK(opck_spork) { - const Exp_Unary* unary = (Exp_Unary*)data; - if(unary->unary_type == unary_exp && unary->exp->exp_type == ae_exp_call) { + const Exp_Unary *unary = (Exp_Unary *)data; + if (unary->unary_type == unary_exp && unary->exp->exp_type == ae_exp_call) { const m_bool is_spork = unary->op == insert_symbol("spork"); return is_spork ? env->gwion->type[et_shred] : fork_type(env, unary); } - if(unary->unary_type == unary_code) { + if (unary->unary_type == unary_code) { ++env->scope->depth; nspc_push_value(env->gwion->mp, env->curr); const m_bool ret = check_stmt(env, unary->code); nspc_pop_value(env->gwion->mp, env->curr); --env->scope->depth; CHECK_BN(ret); - return env->gwion->type[unary->op == insert_symbol("spork") ? et_shred : et_fork]; + return env->gwion + ->type[unary->op == insert_symbol("spork") ? et_shred : et_fork]; } ERR_O(exp_self(unary)->pos, _("only function calls can be sporked...")) } static OP_EMIT(opem_spork) { - const Exp_Unary* unary = (Exp_Unary*)data; + const Exp_Unary *unary = (Exp_Unary *)data; return emit_exp_spork(emit, unary); } -static FREEARG(freearg_xork) { - vmcode_remref((VM_Code)instr->m_val, gwion); -} +static FREEARG(freearg_xork) { vmcode_remref((VM_Code)instr->m_val, gwion); } static FREEARG(freearg_dottmpl) { - struct dottmpl_ *dt = (struct dottmpl_*)instr->m_val; - if(dt->tl) - free_type_list(((Gwion)gwion)->mp, dt->tl); + struct dottmpl_ *dt = (struct dottmpl_ *)instr->m_val; + if (dt->tl) free_type_list(((Gwion)gwion)->mp, dt->tl); mp_free(((Gwion)gwion)->mp, dottmpl, dt); } diff --git a/src/lib/modules.c b/src/lib/modules.c index ef48b4d4..11c319b7 100644 --- a/src/lib/modules.c +++ b/src/lib/modules.c @@ -13,32 +13,29 @@ #include "gwi.h" #include "emit.h" -static DTOR(basic_dtor) { - xfree(UGEN(o)->module.gen.data); -} +static DTOR(basic_dtor) { xfree(UGEN(o)->module.gen.data); } -static TICK(gain_tick) { - u->out = (u->in * *(m_float*)u->module.gen.data); -} +static TICK(gain_tick) { u->out = (u->in * *(m_float *)u->module.gen.data); } static CTOR(gain_ctor) { ugen_ini(shred->info->vm->gwion, UGEN(o), 1, 1); - ugen_gen(shred->info->vm->gwion, 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; + ugen_gen(shred->info->vm->gwion, 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; } - static MFUN(gain_get_gain) { - *(m_float*)RETURN = *(m_float*)UGEN(o)->module.gen.data; + *(m_float *)RETURN = *(m_float *)UGEN(o)->module.gen.data; } static MFUN(gain_set_gain) { - *(m_float*)RETURN = *(m_float*)UGEN(o)->module.gen.data = *(m_float*)MEM(SZ_INT); + *(m_float *)RETURN = *(m_float *)UGEN(o)->module.gen.data = + *(m_float *)MEM(SZ_INT); } static GWION_IMPORT(gain) { - GWI_OB(gwi_class_ini(gwi, "Gain", "UGen")) + GWI_OB(gwi_class_ini(gwi, "Gain", "UGen")) gwi_class_xtor(gwi, gain_ctor, basic_dtor); gwi_func_ini(gwi, "float", "gain"); GWI_BB(gwi_func_end(gwi, gain_get_gain, ae_flag_none)) @@ -49,22 +46,24 @@ static GWION_IMPORT(gain) { } static TICK(impulse_tick) { - u->out = *(m_float*)u->module.gen.data; - *(m_float*)u->module.gen.data = 0; + u->out = *(m_float *)u->module.gen.data; + *(m_float *)u->module.gen.data = 0; } static CTOR(impulse_ctor) { ugen_ini(shred->info->vm->gwion, UGEN(o), 0, 1); - ugen_gen(shred->info->vm->gwion, UGEN(o), impulse_tick, (m_float*)xmalloc(SZ_FLOAT), 0); - *(m_float*)UGEN(o)->module.gen.data = 0; + ugen_gen(shred->info->vm->gwion, UGEN(o), impulse_tick, + (m_float *)xmalloc(SZ_FLOAT), 0); + *(m_float *)UGEN(o)->module.gen.data = 0; } static MFUN(impulse_get_next) { - *(m_float*)RETURN = *(m_float*)UGEN(o)->module.gen.data; + *(m_float *)RETURN = *(m_float *)UGEN(o)->module.gen.data; } static MFUN(impulse_set_next) { - *(m_float*)RETURN = (*(m_float*)UGEN(o)->module.gen.data = *(m_float*)MEM(SZ_INT)); + *(m_float *)RETURN = + (*(m_float *)UGEN(o)->module.gen.data = *(m_float *)MEM(SZ_INT)); } static GWION_IMPORT(impulse) { @@ -78,14 +77,13 @@ static GWION_IMPORT(impulse) { return gwi_class_end(gwi); } -static TICK(fullrect_tick) { - u->out = fabs(u->in); -} +static TICK(fullrect_tick) { u->out = fabs(u->in); } static CTOR(fullrect_ctor) { ugen_ini(shred->info->vm->gwion, UGEN(o), 1, 1); - ugen_gen(shred->info->vm->gwion, UGEN(o), fullrect_tick, (m_float*)xmalloc(SZ_FLOAT), 0); - *(m_float*)UGEN(o)->module.gen.data = 1; + ugen_gen(shred->info->vm->gwion, UGEN(o), fullrect_tick, + (m_float *)xmalloc(SZ_FLOAT), 0); + *(m_float *)UGEN(o)->module.gen.data = 1; } static GWION_IMPORT(fullrect) { @@ -95,7 +93,7 @@ static GWION_IMPORT(fullrect) { } static TICK(halfrect_tick) { - if(u->in > 0) + if (u->in > 0) u->out = u->in; else u->out = 0; @@ -103,8 +101,9 @@ static TICK(halfrect_tick) { static CTOR(halfrect_ctor) { ugen_ini(shred->info->vm->gwion, UGEN(o), 1, 1); - ugen_gen(shred->info->vm->gwion, UGEN(o), halfrect_tick, (m_float*)xmalloc(SZ_FLOAT), 0); - *(m_float*)UGEN(o)->module.gen.data = 1; + ugen_gen(shred->info->vm->gwion, UGEN(o), halfrect_tick, + (m_float *)xmalloc(SZ_FLOAT), 0); + *(m_float *)UGEN(o)->module.gen.data = 1; } static GWION_IMPORT(halfrect) { @@ -113,22 +112,22 @@ static GWION_IMPORT(halfrect) { return gwi_class_end(gwi); } -static TICK(step_tick) { - u->out = *(m_float*)u->module.gen.data; -} +static TICK(step_tick) { u->out = *(m_float *)u->module.gen.data; } static CTOR(step_ctor) { ugen_ini(shred->info->vm->gwion, UGEN(o), 0, 1); - ugen_gen(shred->info->vm->gwion, UGEN(o), step_tick, (m_float*)xmalloc(SZ_FLOAT), 0); - *(m_float*)UGEN(o)->module.gen.data = 0; + ugen_gen(shred->info->vm->gwion, UGEN(o), step_tick, + (m_float *)xmalloc(SZ_FLOAT), 0); + *(m_float *)UGEN(o)->module.gen.data = 0; } static MFUN(step_get_next) { - *(m_float*)RETURN = *(m_float*)UGEN(o)->module.gen.data; + *(m_float *)RETURN = *(m_float *)UGEN(o)->module.gen.data; } static MFUN(step_set_next) { - *(m_float*)RETURN = *(m_float*)UGEN(o)->module.gen.data = *(m_float*)(shred->mem + SZ_INT); + *(m_float *)RETURN = *(m_float *)UGEN(o)->module.gen.data = + *(m_float *)(shred->mem + SZ_INT); } static GWION_IMPORT(step) { @@ -143,16 +142,17 @@ static GWION_IMPORT(step) { } static TICK(zerox_tick) { - m_float in = (u->in < 0) ? -1 : (u->in > 0); - m_float f = *(m_float*)u->module.gen.data; - u->out = f == in ? 1 : 0; - *(m_float*) u->module.gen.data = in; + m_float in = (u->in < 0) ? -1 : (u->in > 0); + m_float f = *(m_float *)u->module.gen.data; + u->out = f == in ? 1 : 0; + *(m_float *)u->module.gen.data = in; } static CTOR(zerox_ctor) { ugen_ini(shred->info->vm->gwion, UGEN(o), 1, 1); - ugen_gen(shred->info->vm->gwion, UGEN(o), zerox_tick, (m_float*)xmalloc(SZ_FLOAT), 0); - *(m_float*)UGEN(o)->module.gen.data = 1; + ugen_gen(shred->info->vm->gwion, UGEN(o), zerox_tick, + (m_float *)xmalloc(SZ_FLOAT), 0); + *(m_float *)UGEN(o)->module.gen.data = 1; } static GWION_IMPORT(zerox) { @@ -162,24 +162,22 @@ static GWION_IMPORT(zerox) { } struct UUGen_ { - M_Object self; - VM_Code code; + M_Object self; + VM_Code code; VM_Shred shred; - void (*prep)(struct UUGen_*, const m_float); + void (*prep)(struct UUGen_ *, const m_float); }; ANN static void global_prep(struct UUGen_ *uu, const m_float in) { - *(m_float*)uu->shred->mem = in; + *(m_float *)uu->shred->mem = in; } ANN static void member_prep(struct UUGen_ *uu, const m_float in) { - *(M_Object*)uu->shred->mem = uu->self; - *(m_float*)(uu->shred->mem + SZ_INT) = in; + *(M_Object *)uu->shred->mem = uu->self; + *(m_float *)(uu->shred->mem + SZ_INT) = in; } -static TICK(id_tick) { - u->out = u->in; -} +static TICK(id_tick) { u->out = u->in; } static TICK(usrugen_tick) { struct UUGen_ *uu = u->module.gen.data; @@ -188,55 +186,55 @@ static TICK(usrugen_tick) { shredule(uu->shred->tick->shreduler, uu->shred, 0); vm_run(uu->shred->info->vm); uu->shred->reg -= SZ_FLOAT; - u->out = *(m_float*)(uu->shred->reg); + u->out = *(m_float *)(uu->shred->reg); } static CTOR(usrugen_ctor) { - struct UUGen_* uu = mp_calloc(shred->info->mp, UUGen); - uu->self = o; + struct UUGen_ *uu = mp_calloc(shred->info->mp, UUGen); + uu->self = o; ugen_ini(shred->info->vm->gwion, UGEN(o), 1, 1); ugen_gen(shred->info->vm->gwion, UGEN(o), id_tick, uu, 0); } static DTOR(usrugen_dtor) { struct UUGen_ *uu = UGEN(o)->module.gen.data; - if(uu->shred) - free_vm_shred(uu->shred); + if (uu->shred) free_vm_shred(uu->shred); mp_free(shred->info->mp, UUGen, UGEN(o)->module.gen.data); } static OP_CHECK(opck_usrugen) { - Exp_Binary *bin = (Exp_Binary*)data; + Exp_Binary * bin = (Exp_Binary *)data; const Arg_List arg = bin->lhs->type->info->func->def->base->args; - if(!arg || arg->next) - ERR_N(exp_self(bin)->pos, _("Tick function take one and only one argument")); - if(isa(arg->type, env->gwion->type[et_float]) < 0) - ERR_N(exp_self(bin)->pos, _("Tick functions argument must be of type float")); - if(isa(bin->lhs->type->info->func->def->base->ret_type, env->gwion->type[et_float]) < 0) + if (!arg || arg->next) + ERR_N(exp_self(bin)->pos, + _("Tick function take one and only one argument")); + if (isa(arg->type, env->gwion->type[et_float]) < 0) + ERR_N(exp_self(bin)->pos, + _("Tick functions argument must be of type float")); + if (isa(bin->lhs->type->info->func->def->base->ret_type, + env->gwion->type[et_float]) < 0) ERR_N(exp_self(bin)->pos, _("Tick function must return float")); - if(bin->lhs->type->info->func->value_ref->from->owner_class) + if (bin->lhs->type->info->func->value_ref->from->owner_class) CHECK_BN(isa(bin->lhs->type->info->func->value_ref->from->owner_class, - bin->rhs->type)); + bin->rhs->type)); return bin->rhs->type; } -static INSTR(UURet) { - shreduler_remove(shred->tick->shreduler, shred, false); -} +static INSTR(UURet) { shreduler_remove(shred->tick->shreduler, shred, false); } ANN static void code_prepare(const VM_Code code) { m_bit *byte = code->bytecode; - for(m_uint i = 0; i < vector_size(&code->instr); ++i) { - if(*(m_bit*)(byte + i *BYTECODE_SZ) == eFuncReturn) { - *(m_bit*)(byte + i * BYTECODE_SZ)= eOP_MAX; - *(f_instr*)(byte + (i*BYTECODE_SZ) + SZ_INT*2) = UURet; + for (m_uint i = 0; i < vector_size(&code->instr); ++i) { + if (*(m_bit *)(byte + i * BYTECODE_SZ) == eFuncReturn) { + *(m_bit *)(byte + i * BYTECODE_SZ) = eOP_MAX; + *(f_instr *)(byte + (i * BYTECODE_SZ) + SZ_INT * 2) = UURet; } } } static MFUN(default_tick) { struct UUGen_ *uu = UGEN(o)->module.gen.data; - if(uu->shred) { + if (uu->shred) { free_vm_shred(uu->shred); uu->shred = NULL; } @@ -245,34 +243,34 @@ static MFUN(default_tick) { static INSTR(UsrUGenTick) { const m_uint offset = !instr->m_val ? SZ_INT : 0; - shred->reg -= SZ_INT*2 - offset; - const M_Object o = *(M_Object*)(shred->reg + SZ_INT - offset); - if(!o) { + shred->reg -= SZ_INT * 2 - offset; + const M_Object o = *(M_Object *)(shred->reg + SZ_INT - offset); + if (!o) { handle(shred, "NullPtrException"); return; } struct UUGen_ *uu = UGEN(o)->module.gen.data; - if(uu->shred) { + if (uu->shred) { free_vm_shred(uu->shred); return; } UGEN(o)->module.gen.tick = usrugen_tick; - const VM_Code code = *(VM_Code*)(shred->reg-offset); - if(!code) { + const VM_Code code = *(VM_Code *)(shred->reg - offset); + if (!code) { handle(shred, "NullTickException"); return; } - uu->shred = new_vm_shred(shred->info->mp, *(VM_Code*)(shred->reg-offset)); - vmcode_addref(*(VM_Code*)(shred->reg - offset)); + uu->shred = new_vm_shred(shred->info->mp, *(VM_Code *)(shred->reg - offset)); + vmcode_addref(*(VM_Code *)(shred->reg - offset)); uu->shred->info->vm = shred->info->vm; code_prepare(vmcode_callback(shred->info->mp, uu->shred->code)); shreduler_ini(uu->shred->info->vm->shreduler, uu->shred); - uu->prep = instr->m_val ? member_prep : global_prep; - *(M_Object*)(shred->reg - SZ_INT) = o; + uu->prep = instr->m_val ? member_prep : global_prep; + *(M_Object *)(shred->reg - SZ_INT) = o; } static OP_EMIT(opem_usrugen) { - Exp_Binary *bin = (Exp_Binary*)data; + Exp_Binary *bin = (Exp_Binary *)data; const Instr instr = emit_add_instr(emit, UsrUGenTick); instr->m_val = !!bin->lhs->type->info->func->value_ref->from->owner_class; return GW_OK; diff --git a/src/lib/object.c b/src/lib/object.c index 74549429..8854107f 100644 --- a/src/lib/object.c +++ b/src/lib/object.c @@ -22,57 +22,58 @@ M_Object new_object(MemPool p, const VM_Shred shred, const Type t) { const M_Object a = mp_calloc(p, M_Object); - a->ref = 1; - a->type_ref = t; - if(t->nspc) { + a->ref = 1; + a->type_ref = t; + if (t->nspc) { a->vtable.ptr = t->nspc->vtable.ptr; - if(t->nspc->info->offset) - a->data = (m_bit*)_mp_calloc(p, t->nspc->info->offset); + if (t->nspc->info->offset) + a->data = (m_bit *)_mp_calloc(p, t->nspc->info->offset); } - if(shred) - vector_add(&shred->gc, (vtype)a); + if (shred) vector_add(&shred->gc, (vtype)a); return a; } M_Object new_string(MemPool p, const VM_Shred shred, const m_str str) { - const M_Object o = new_object(p, shred, shred->info->vm->gwion->type[et_string]); + const M_Object o = + new_object(p, shred, shred->info->vm->gwion->type[et_string]); STRING(o) = mstrdup(p, str); return o; } -M_Object new_string2(const struct Gwion_ *gwion, const VM_Shred shred, const m_str str) { +M_Object new_string2(const struct Gwion_ *gwion, const VM_Shred shred, + const m_str str) { const M_Object o = new_object(gwion->mp, shred, gwion->type[et_string]); - STRING(o) = mstrdup(gwion->mp, str); + STRING(o) = mstrdup(gwion->mp, str); return o; } ANN static void handle_dtor(const M_Object o, const VM_Shred shred) { const VM_Shred sh = new_vm_shred(shred->info->mp, o->type_ref->nspc->dtor); vmcode_addref(o->type_ref->nspc->dtor); - sh->base = shred->base; - *(M_Object*)sh->mem = o; + sh->base = shred->base; + *(M_Object *)sh->mem = o; vm_add_shred(shred->info->vm, sh); ++sh->info->me->ref; } -__attribute__((hot)) -ANN void __release(const M_Object o, const VM_Shred shred) { +__attribute__((hot)) ANN void __release(const M_Object o, + const VM_Shred shred) { vector_rem2(&shred->gc, (vtype)o); MemPool p = shred->info->mp; - Type t = o->type_ref; + Type t = o->type_ref; do { - if(!t->nspc) - continue; - if(isa(t, shred->info->vm->gwion->type[et_union]) < 0) { - struct scope_iter iter = { t->nspc->info->value, 0, 0 };\ - Value v; - while(scope_iter(&iter, &v) > 0) { - if(!GET_FLAG(v, static) && isa(v->type, shred->info->vm->gwion->type[et_compound]) > 0) + if (!t->nspc) continue; + if (isa(t, shred->info->vm->gwion->type[et_union]) < 0) { + struct scope_iter iter = {t->nspc->info->value, 0, 0}; + Value v; + while (scope_iter(&iter, &v) > 0) { + if (!GET_FLAG(v, static) && + isa(v->type, shred->info->vm->gwion->type[et_compound]) > 0) compound_release(shred, v->type, o->data + v->from->offset); } } - if(tflag(t, tflag_dtor)) { - if(t->nspc->dtor->builtin) + if (tflag(t, tflag_dtor)) { + if (t->nspc->dtor->builtin) ((f_xtor)t->nspc->dtor->native_func)(o, NULL, shred); else { o->type_ref = t; @@ -80,41 +81,45 @@ ANN void __release(const M_Object o, const VM_Shred shred) { return; } } - } while((t = t->info->parent)); + } while ((t = t->info->parent)); free_object(p, o); } ANN void free_object(MemPool p, const M_Object o) { - if(o->type_ref->nspc && o->type_ref->nspc->info->offset) + if (o->type_ref->nspc && o->type_ref->nspc->info->offset) mp_free2(p, o->type_ref->nspc->info->offset, o->data); mp_free(p, M_Object, o); } static ID_CHECK(opck_this) { - if(!env->class_def) - ERR_O(exp_self(prim)->pos, _("keyword 'this' can be used only inside class definition...")) - if(env->func && !vflag(env->func->value_ref, vflag_member)) - ERR_O(exp_self(prim)->pos, _("keyword 'this' cannot be used inside static functions...")) - if(!exp_getuse(exp_self(prim)) && env->func && !strcmp(s_name(env->func->def->base->xid), "@gack")) + if (!env->class_def) + ERR_O(exp_self(prim)->pos, + _("keyword 'this' can be used only inside class definition...")) + if (env->func && !vflag(env->func->value_ref, vflag_member)) + ERR_O(exp_self(prim)->pos, + _("keyword 'this' cannot be used inside static functions...")) + if (!exp_getuse(exp_self(prim)) && env->func && + !strcmp(s_name(env->func->def->base->xid), "@gack")) return get_gack(env->class_def->info->parent); // get_gack ? return env->class_def; } static ID_EMIT(opem_this) { - if(!exp_getvar(exp_self(prim)) && tflag(exp_self(prim)->type, tflag_struct)) { + if (!exp_getvar(exp_self(prim)) && + tflag(exp_self(prim)->type, tflag_struct)) { const Instr instr = emit_add_instr(emit, RegPushMemDeref); - instr->m_val2 = emit->env->class_def->size; + instr->m_val2 = emit->env->class_def->size; return (Instr)GW_OK; } const Instr instr = emit_add_instr(emit, RegPushMem); - instr->m_val = emit->this_offset; + instr->m_val = emit->this_offset; return instr; } GWION_IMPORT(object) { - const Type t_object = gwi_mk_type(gwi, "Object", SZ_INT, "@Compound"); + const Type t_object = gwi_mk_type(gwi, "Object", SZ_INT, "@Compound"); gwi_set_global_type(gwi, t_object, et_object); - struct SpecialId_ spid = { .ck=opck_this, .em=opem_this, .is_const=1 }; + struct SpecialId_ spid = {.ck = opck_this, .em = opem_this, .is_const = 1}; gwi_specialid(gwi, "this", &spid); return GW_OK; } diff --git a/src/lib/object_op.c b/src/lib/object_op.c index 05e15b44..f553a935 100644 --- a/src/lib/object_op.c +++ b/src/lib/object_op.c @@ -16,227 +16,241 @@ #undef insert_symbol -#define describe_logical(name, op) \ -static INSTR(name##Object) { \ - POP_REG(shred, SZ_INT); \ - const M_Object lhs = *(M_Object*)REG(-SZ_INT); \ - const M_Object rhs = *(M_Object*)REG(0); \ - *(m_uint*)REG(-SZ_INT) = (lhs op rhs); \ -} +#define describe_logical(name, op) \ + static INSTR(name##Object) { \ + POP_REG(shred, SZ_INT); \ + const M_Object lhs = *(M_Object *)REG(-SZ_INT); \ + const M_Object rhs = *(M_Object *)REG(0); \ + *(m_uint *)REG(-SZ_INT) = (lhs op rhs); \ + } -describe_logical(Eq, ==) -describe_logical(Neq, !=) +describe_logical(Eq, ==) describe_logical(Neq, !=) -static OP_CHECK(opck_object_at) { - const Exp_Binary* bin = (Exp_Binary*)data; - if(opck_rassign(env, data) == env->gwion->type[et_error]) + static OP_CHECK(opck_object_at) { + const Exp_Binary *bin = (Exp_Binary *)data; + if (opck_rassign(env, data) == env->gwion->type[et_error]) return env->gwion->type[et_error]; - if(bin->rhs->exp_type == ae_exp_decl) + if (bin->rhs->exp_type == ae_exp_decl) SET_FLAG(bin->rhs->d.exp_decl.list->self->value, late); exp_setvar(bin->rhs, 1); - CHECK_BO(isa(bin->lhs->type , bin->rhs->type)); + CHECK_BO(isa(bin->lhs->type, bin->rhs->type)); return bin->rhs->type; } static OP_EMIT(opem_object_at) { const Instr addref = emit_add_instr(emit, RegAddRef); - addref->m_val = -SZ_INT*2; + addref->m_val = -SZ_INT * 2; (void)emit_add_instr(emit, ObjectAssign); return GW_OK; } static OP_CHECK(opck_object_cast) { - const Exp_Cast* cast = (Exp_Cast*)data; - const Type to = known_type(env, cast->td); - if(isa(cast->exp->type, to) < 0) { - if(isa(to, cast->exp->type) > 0) - ERR_N(exp_self(cast)->pos, _("can't upcast '%s' to '%s'"), cast->exp->type->name, to->name); - ERR_N(exp_self(cast)->pos, _("can't cast '%s' to '%s'"), cast->exp->type->name, to->name); + const Exp_Cast *cast = (Exp_Cast *)data; + const Type to = known_type(env, cast->td); + if (isa(cast->exp->type, to) < 0) { + if (isa(to, cast->exp->type) > 0) + ERR_N(exp_self(cast)->pos, _("can't upcast '%s' to '%s'"), + cast->exp->type->name, to->name); + ERR_N(exp_self(cast)->pos, _("can't cast '%s' to '%s'"), + cast->exp->type->name, to->name); } return exp_self(cast)->type; } -ANN /*static*/ Type scan_class(const Env env, const Type t, const Type_Decl * td); +ANN /*static*/ Type scan_class(const Env env, const Type t, + const Type_Decl *td); static Type opck_object_scan(const Env env, const struct TemplateScan *ts) { - if(ts->td->types) + if (ts->td->types) return scan_class(env, ts->t, ts->td) ?: env->gwion->type[et_error]; - Type_Decl *td = (Type_Decl*)ts->td; - ERR_N(td->pos, _("you must provide template types for type '%s'"), ts->t->name); + Type_Decl *td = (Type_Decl *)ts->td; + ERR_N(td->pos, _("you must provide template types for type '%s'"), + ts->t->name); } static OP_CHECK(opck_struct_scan) { - struct TemplateScan *ts = (struct TemplateScan*)data; + struct TemplateScan *ts = (struct TemplateScan *)data; return opck_object_scan(env, ts); } -static const f_instr dotstatic[] = { DotStatic, DotStatic2, DotStatic3, RegPushImm }; -static const f_instr structmember[] = { StructMember, StructMemberFloat, StructMemberOther, StructMemberAddr }; +static const f_instr dotstatic[] = {DotStatic, DotStatic2, DotStatic3, + RegPushImm}; +static const f_instr structmember[] = {StructMember, StructMemberFloat, + StructMemberOther, StructMemberAddr}; -ANN Instr emit_kind(Emitter emit, const m_uint size, const uint addr, const f_instr func[]); -ANN static void emit_dot_static_data(const Emitter emit, const Value v, const uint emit_var) { - const m_uint size = v->type->size; - const Instr instr = emit_kind(emit, size, emit_var, dotstatic); - instr->m_val = (m_uint)(v->from->owner->info->class_data + v->from->offset); +ANN Instr emit_kind(Emitter emit, const m_uint size, const uint addr, + const f_instr func[]); +ANN static void emit_dot_static_data(const Emitter emit, const Value v, + const uint emit_var) { + const m_uint size = v->type->size; + const Instr instr = emit_kind(emit, size, emit_var, dotstatic); + instr->m_val = (m_uint)(v->from->owner->info->class_data + v->from->offset); instr->m_val2 = size; } -static const f_instr regpushimm[] = { RegPushImm, RegPushImm2, RegPushImm3, RegPushImm4 }; -ANN static void emit_dot_static_import_data(const Emitter emit, const Value v, const uint emit_addr) { -// if(v->d.ptr && vflag(v, vflag_builtin) && GET_FLAG(v, const)) { - if(vflag(v, vflag_builtin) && GET_FLAG(v, const)) { - const m_uint size = v->type->size; - const Instr instr = emit_kind(emit, size, emit_addr, regpushimm); - instr->m_val = (m_uint)v->d.ptr; - instr->m_val2 = size; +static const f_instr regpushimm[] = {RegPushImm, RegPushImm2, RegPushImm3, + RegPushImm4}; +ANN static void emit_dot_static_import_data(const Emitter emit, const Value v, + const uint emit_addr) { + // if(v->d.ptr && vflag(v, vflag_builtin) && GET_FLAG(v, const)) { + if (vflag(v, vflag_builtin) && GET_FLAG(v, const)) { + const m_uint size = v->type->size; + const Instr instr = emit_kind(emit, size, emit_addr, regpushimm); + instr->m_val = (m_uint)v->d.ptr; + instr->m_val2 = size; } else emit_dot_static_data(emit, v, emit_addr); } -static const f_instr dotmember[] = { DotMember, DotMember2, DotMember3, DotMember4 }; +static const f_instr dotmember[] = {DotMember, DotMember2, DotMember3, + DotMember4}; -ANN static void emit_member_func(const Emitter emit, const Exp_Dot* member) { +ANN static void emit_member_func(const Emitter emit, const Exp_Dot *member) { const Func f = exp_self(member)->type->info->func; - if(f->def->base->tmpl) + if (f->def->base->tmpl) emit_add_instr(emit, DotTmplVal); - else if(is_static_call(emit, exp_self(member))) { - if(f == emit->env->func) - return; + else if (is_static_call(emit, exp_self(member))) { + if (f == emit->env->func) return; const Instr func_i = emit_add_instr(emit, f->code ? RegPushImm : SetFunc); - func_i->m_val = (m_uint)f->code ?: (m_uint)f; + func_i->m_val = (m_uint)f->code ?: (m_uint)f; return; } else { - if(tflag(member->base->type, tflag_struct)) { - if(!GET_FLAG(f->def->base, static)) { + if (tflag(member->base->type, tflag_struct)) { + if (!GET_FLAG(f->def->base, static)) { exp_setvar(member->base, 1); emit_exp(emit, member->base); } const Instr func_i = emit_add_instr(emit, f->code ? RegPushImm : SetFunc); - func_i->m_val = (m_uint)f->code ?: (m_uint)f; + func_i->m_val = (m_uint)f->code ?: (m_uint)f; return; } const Instr instr = emit_add_instr(emit, DotFunc); - instr->m_val = f->vt_index; - if(!vflag(f->value_ref, vflag_member)) + instr->m_val = f->vt_index; + if (!vflag(f->value_ref, vflag_member)) instr->m_val2 = -SZ_INT; else { const Instr instr = emit_add_instr(emit, RegMove); - instr->m_val = SZ_INT; + instr->m_val = SZ_INT; } } return; } -ANN static inline void emit_member(const Emitter emit, const Value v, const uint emit_addr) { - const m_uint size = v->type->size; - const Instr instr = emit_kind(emit, size, emit_addr, dotmember); - instr->m_val = v->from->offset; - instr->m_val2 = size; +ANN static inline void emit_member(const Emitter emit, const Value v, + const uint emit_addr) { + const m_uint size = v->type->size; + const Instr instr = emit_kind(emit, size, emit_addr, dotmember); + instr->m_val = v->from->offset; + instr->m_val2 = size; } -ANN static inline void emit_struct_data(const Emitter emit, const Value v, const uint emit_addr) { +ANN static inline void emit_struct_data(const Emitter emit, const Value v, + const uint emit_addr) { const Instr instr = emit_kind(emit, v->type->size, emit_addr, structmember); - instr->m_val = v->from->offset; - if(!emit_addr) { + instr->m_val = v->from->offset; + if (!emit_addr) { const Instr instr = emit_add_instr(emit, RegMove); - instr->m_val = v->type->size -SZ_INT; + instr->m_val = v->type->size - SZ_INT; } } -ANN m_bool not_from_owner_class(const Env env, const Type t, const Value v, const loc_t pos); +ANN m_bool not_from_owner_class(const Env env, const Type t, const Value v, + const loc_t pos); -ANN static inline Value get_value(const Env env, const Exp_Dot *member, const Type t) { +ANN static inline Value get_value(const Env env, const Exp_Dot *member, + const Type t) { const Value value = find_value(t, member->xid); - if(value) - return value; - if(env->func && env->func->def->base->values) - return (Value)scope_lookup1(env->func->def->base->values, (m_uint)member->xid); + if (value) return value; + if (env->func && env->func->def->base->values) + return (Value)scope_lookup1(env->func->def->base->values, + (m_uint)member->xid); return NULL; } OP_CHECK(opck_object_dot) { - const Exp_Dot *member = (Exp_Dot*)data; - const m_str str = s_name(member->xid); - const m_bool base_static = is_class(env->gwion, member->base->type); - const Type the_base = base_static ? _class_base(member->base->type) : member->base->type; -// if(!the_base->nspc) -// ERR_N(&member->base->pos, -// _("type '%s' does not have members - invalid use in dot expression of %s"), -// the_base->name, str); - if(member->xid == insert_symbol(env->gwion->st, "this") && base_static) + const Exp_Dot *member = (Exp_Dot *)data; + const m_str str = s_name(member->xid); + const m_bool base_static = is_class(env->gwion, member->base->type); + const Type the_base = + base_static ? _class_base(member->base->type) : member->base->type; + // if(!the_base->nspc) + // ERR_N(&member->base->pos, + // _("type '%s' does not have members - invalid use in dot expression + // of %s"), the_base->name, str); + if (member->xid == insert_symbol(env->gwion->st, "this") && base_static) ERR_N(exp_self(member)->pos, _("keyword 'this' must be associated with object instance...")); const Value value = get_value(env, member, the_base); - if(!value) { + if (!value) { const Value v = nspc_lookup_value1(env->curr, member->xid); - if(v && isa(v->type, env->gwion->type[et_function]) > 0) - return v->type; - env_err(env, exp_self(member)->pos, - _("class '%s' has no member '%s'"), the_base->name, str); - if(member->base->type->nspc) - did_you_mean_type(the_base, str); + if (v && isa(v->type, env->gwion->type[et_function]) > 0) return v->type; + env_err(env, exp_self(member)->pos, _("class '%s' has no member '%s'"), + the_base->name, str); + if (member->base->type->nspc) did_you_mean_type(the_base, str); return env->gwion->type[et_error]; } CHECK_BN(not_from_owner_class(env, the_base, value, exp_self(member)->pos)); - if(!env->class_def || isa(env->class_def, value->from->owner_class) < 0) { - if(GET_FLAG(value, private)) { - gwerr_basic("invalid variable access", "is private", NULL, - env->name, exp_self(member)->pos, 0); + if (!env->class_def || isa(env->class_def, value->from->owner_class) < 0) { + if (GET_FLAG(value, private)) { + gwerr_basic("invalid variable access", "is private", NULL, env->name, + exp_self(member)->pos, 0); env_error_footer(env); gwerr_secondary("declared here", value->from->filename, value->from->loc); env->context->error = true; - } else if(GET_FLAG(value, protect)) + } else if (GET_FLAG(value, protect)) exp_setprot(exp_self(member), 1); } - if(base_static && vflag(value, vflag_member)) + if (base_static && vflag(value, vflag_member)) ERR_N(exp_self(member)->pos, _("cannot access member '%s.%s' without object instance..."), the_base->name, str); - if(GET_FLAG(value, const)) - exp_setmeta(exp_self(member), 1); + if (GET_FLAG(value, const)) exp_setmeta(exp_self(member), 1); return value->type; } OP_EMIT(opem_object_dot) { - const Exp_Dot *member = (Exp_Dot*)data; - const Type t_base = actual_type(emit->gwion, member->base->type); - const Value value = find_value(t_base, member->xid); - if(!is_class(emit->gwion, member->base->type) && (vflag(value, vflag_member) || + const Exp_Dot *member = (Exp_Dot *)data; + const Type t_base = actual_type(emit->gwion, member->base->type); + const Value value = find_value(t_base, member->xid); + if (!is_class(emit->gwion, member->base->type) && + (vflag(value, vflag_member) || (isa(exp_self(member)->type, emit->gwion->type[et_function]) > 0 && - !is_fptr(emit->gwion, exp_self(member)->type)))) { - if(!tflag(t_base, tflag_struct)) - CHECK_BB(emit_exp(emit, member->base)); + !is_fptr(emit->gwion, exp_self(member)->type)))) { + if (!tflag(t_base, tflag_struct)) CHECK_BB(emit_exp(emit, member->base)); } - if(isa(exp_self(member)->type, emit->gwion->type[et_function]) > 0 && !is_fptr(emit->gwion, exp_self(member)->type)) - emit_member_func(emit, member); - else if(vflag(value, vflag_member)) { - if(!tflag(t_base, tflag_struct)) + if (isa(exp_self(member)->type, emit->gwion->type[et_function]) > 0 && + !is_fptr(emit->gwion, exp_self(member)->type)) + emit_member_func(emit, member); + else if (vflag(value, vflag_member)) { + if (!tflag(t_base, tflag_struct)) emit_member(emit, value, exp_getvar(exp_self(member))); else { exp_setvar(member->base, 1); CHECK_BB(emit_exp(emit, member->base)); emit_struct_data(emit, value, exp_getvar(exp_self(member))); } - } else if(GET_FLAG(value, static)) + } else if (GET_FLAG(value, static)) emit_dot_static_import_data(emit, value, exp_getvar(exp_self(member))); else { // member type const Instr instr = emit_add_instr(emit, RegPushImm); - instr->m_val = (m_uint)value->type; + instr->m_val = (m_uint)value->type; } - if(GET_FLAG(value, late) && !exp_getvar(exp_self(member))) { + if (GET_FLAG(value, late) && !exp_getvar(exp_self(member))) { const Instr instr = emit_add_instr(emit, GWOP_EXCEPT); - instr->m_val = -SZ_INT; + instr->m_val = -SZ_INT; } return GW_OK; } ANN static m_bool scantmpl_class_def(const Env env, struct tmpl_info *info) { - const Class_Def c = info->base->info->cdef; - const Class_Def cdef = new_class_def(env->gwion->mp, c->flag, info->name, c->base.ext ? cpy_type_decl(env->gwion->mp, c->base.ext) : NULL, + const Class_Def c = info->base->info->cdef; + const Class_Def cdef = new_class_def( + env->gwion->mp, c->flag, info->name, + c->base.ext ? cpy_type_decl(env->gwion->mp, c->base.ext) : NULL, c->body ? cpy_ast(env->gwion->mp, c->body) : NULL, c->pos); - cdef->cflag = c->cflag; - cdef->base.tmpl = mk_tmpl(env, c->base.tmpl, info->td->types); + cdef->cflag = c->cflag; + cdef->base.tmpl = mk_tmpl(env, c->base.tmpl, info->td->types); const m_bool ret = scan0_class_def(env, cdef); - if((info->ret = cdef->base.type)) { + if ((info->ret = cdef->base.type)) { info->ret->info->cdef = cdef; set_tflag(info->ret, tflag_cdef); } else @@ -245,17 +259,17 @@ ANN static m_bool scantmpl_class_def(const Env env, struct tmpl_info *info) { } ANN static m_bool scantmpl_union_def(const Env env, struct tmpl_info *info) { - const Union_Def u = info->base->info->udef; - const Union_Def udef = new_union_def(env->gwion->mp, cpy_union_list(env->gwion->mp, u->l), u->pos); - udef->xid = info->name; + const Union_Def u = info->base->info->udef; + const Union_Def udef = new_union_def( + env->gwion->mp, cpy_union_list(env->gwion->mp, u->l), u->pos); + udef->xid = info->name; udef->tmpl = mk_tmpl(env, u->tmpl, info->td->types); // resolve the template here - if(GET_FLAG(info->base, global)) - SET_FLAG(udef, global); + if (GET_FLAG(info->base, global)) SET_FLAG(udef, global); const m_bool ret = scan0_union_def(env, udef); - if(udef->type) { - udef->type->info->udef = udef;// mark as udef - info->ret = udef->type;// is info->ret necessary? + if (udef->type) { + udef->type->info->udef = udef; // mark as udef + info->ret = udef->type; // is info->ret necessary? set_tflag(info->ret, tflag_udef); } else free_union_def(env->gwion->mp, udef); @@ -263,7 +277,7 @@ ANN static m_bool scantmpl_union_def(const Env env, struct tmpl_info *info) { } ANN static Type _scan_class(const Env env, struct tmpl_info *info) { - if(info->base->info->parent != env->gwion->type[et_union]) + if (info->base->info->parent != env->gwion->type[et_union]) CHECK_BO(scantmpl_class_def(env, info)); else CHECK_BO(scantmpl_union_def(env, info)); @@ -272,51 +286,53 @@ ANN static Type _scan_class(const Env env, struct tmpl_info *info) { ANN Type tmpl_exists(const Env env, struct tmpl_info *const info); ANN Type scan_class(const Env env, const Type t, const Type_Decl *td) { - struct tmpl_info info = { .base=t, .td=td, .list=t->info->cdef->base.tmpl->list }; + struct tmpl_info info = { + .base = t, .td = td, .list = t->info->cdef->base.tmpl->list}; const Type exists = tmpl_exists(env, &info); - if(exists) - return exists != env->gwion->type[et_error] ? exists : NULL; - struct EnvSet es = { .env=env, .data=env, .func=(_exp_func)scan0_cdef, - .scope=env->scope->depth, .flag=tflag_scan0 }; - const Type owner = t->info->value->from->owner_class; + if (exists) return exists != env->gwion->type[et_error] ? exists : NULL; + struct EnvSet es = {.env = env, + .data = env, + .func = (_exp_func)scan0_cdef, + .scope = env->scope->depth, + .flag = tflag_scan0}; + const Type owner = t->info->value->from->owner_class; CHECK_BO(envset_pushv(&es, t->info->value)); const Type ret = _scan_class(env, &info); - if(es.run) - envset_pop(&es, owner); + if (es.run) envset_pop(&es, owner); return ret; } -ANN void struct_release(const VM_Shred shred, const Type base, const m_bit *ptr) { +ANN void struct_release(const VM_Shred shred, const Type base, + const m_bit *ptr) { const Vector types = &base->info->tuple->types; const Vector offsets = &base->info->tuple->offset; - for(m_uint i = 0; i < vector_size(types); ++i) { + for (m_uint i = 0; i < vector_size(types); ++i) { const Type t = (Type)vector_at(types, i); - if(isa(t, shred->info->vm->gwion->type[et_compound]) < 0) - continue; + if (isa(t, shred->info->vm->gwion->type[et_compound]) < 0) continue; const m_uint offset = vector_at(offsets, i); - if(!tflag(t, tflag_struct)) - release(*(M_Object*)(ptr + offset), shred); + if (!tflag(t, tflag_struct)) + release(*(M_Object *)(ptr + offset), shred); else - struct_release(shred, t, *(m_bit**)(ptr + offset)); + struct_release(shred, t, *(m_bit **)(ptr + offset)); } } static OP_EMIT(opem_not_object) { - const Vector v = &emit->code->instr; - const Instr back = (Instr)vector_back(v); - if(back->opcode == eGWOP_EXCEPT) { + const Vector v = &emit->code->instr; + const Instr back = (Instr)vector_back(v); + if (back->opcode == eGWOP_EXCEPT) { vector_pop(v); mp_free(emit->gwion->mp, Instr, back); emit_add_instr(emit, IntNot); return GW_OK; } const Instr instr = emit_add_instr(emit, RegSetImm); - instr->m_val2 = -SZ_INT; + instr->m_val2 = -SZ_INT; return GW_OK; } GWION_IMPORT(object_op) { - const Type t_error = gwi_mk_type(gwi, "@error", 0, NULL); + const Type t_error = gwi_mk_type(gwi, "@error", 0, NULL); gwi->gwion->type[et_error] = t_error; GWI_BB(gwi_set_global_type(gwi, t_error, et_error)) GWI_BB(gwi_oper_ini(gwi, "Object", "Object", NULL)) @@ -324,13 +340,13 @@ GWION_IMPORT(object_op) { GWI_BB(gwi_oper_emi(gwi, opem_object_at)) GWI_BB(gwi_oper_end(gwi, "=>", NULL)) GWI_BB(gwi_oper_ini(gwi, "Object", "Object", "bool")) - GWI_BB(gwi_oper_end(gwi, "==", EqObject)) + GWI_BB(gwi_oper_end(gwi, "==", EqObject)) GWI_BB(gwi_oper_end(gwi, "!=", NeqObject)) GWI_BB(gwi_oper_add(gwi, opck_object_cast)) GWI_BB(gwi_oper_end(gwi, "$", NULL)) -//. GWI_BB(gwi_oper_add(gwi, opck_implicit_null2obj)) -// GWI_BB(gwi_oper_emi(gwi, opem_implicit_null2obj)) -// GWI_BB(gwi_oper_end(gwi, "@implicit", NULL)) + //. GWI_BB(gwi_oper_add(gwi, opck_implicit_null2obj)) + // GWI_BB(gwi_oper_emi(gwi, opem_implicit_null2obj)) + // GWI_BB(gwi_oper_end(gwi, "@implicit", NULL)) GWI_BB(gwi_oper_ini(gwi, NULL, "Object", "bool")) GWI_BB(gwi_oper_emi(gwi, opem_not_object)) GWI_BB(gwi_oper_end(gwi, "!", IntNot)) diff --git a/src/lib/opfunc.c b/src/lib/opfunc.c index 1d5e4a8b..195bb2b9 100644 --- a/src/lib/opfunc.c +++ b/src/lib/opfunc.c @@ -12,71 +12,70 @@ #include "import.h" OP_CHECK(opck_basic_cast) { - const Exp_Cast* cast = (Exp_Cast*)data; - return isa(cast->exp->type, exp_self(cast)->type) > 0 ? - exp_self(cast)->type : env->gwion->type[et_error]; + const Exp_Cast *cast = (Exp_Cast *)data; + return isa(cast->exp->type, exp_self(cast)->type) > 0 + ? exp_self(cast)->type + : env->gwion->type[et_error]; } OP_CHECK(opck_usr_implicit) { - struct Implicit* imp = (struct Implicit*)data; -/* -// in use for refinement types - const Value v = nspc_lookup_value1(imp->t->info->owner, insert_symbol("@implicit")); - if(v) { - Func f = v->d.func_ref; - while(f) { - if(f->def->base->ret_type == imp->t) - break; - f = f->next; + struct Implicit *imp = (struct Implicit *)data; + /* + // in use for refinement types + const Value v = nspc_lookup_value1(imp->t->info->owner, + insert_symbol("@implicit")); if(v) { Func f = v->d.func_ref; while(f) { + if(f->def->base->ret_type == imp->t) + break; + f = f->next; + } + if(f) { + // TODO: add call exp + struct Exp_ call = { .exp_type=ae_exp_call, + .d={.exp_call={.args=imp->e}}, .pos=imp->e->pos, .type=f->value_ref->type }; + struct Op_Import opi = { .op=insert_symbol("@func_check"), + .rhs=f->value_ref->type, .pos=imp->e->pos, .data=(uintptr_t)&call }; + CHECK_NN(op_check(env, &opi)); + } } - if(f) { - // TODO: add call exp - struct Exp_ call = { .exp_type=ae_exp_call, .d={.exp_call={.args=imp->e}}, .pos=imp->e->pos, - .type=f->value_ref->type }; - struct Op_Import opi = { .op=insert_symbol("@func_check"), - .rhs=f->value_ref->type, .pos=imp->e->pos, .data=(uintptr_t)&call }; - CHECK_NN(op_check(env, &opi)); - } - } -*/ + */ return imp->t; } OP_CHECK(opck_const_rhs) { - const Exp_Binary* bin = (Exp_Binary*)data; - const m_str access = exp_access(bin->rhs); - if(access) - ERR_N(bin->rhs->pos, _("cannot assign '%s' on types '%s' and '%s'.\n" - " ... (reason: --- right-side operand is %s.)"), - s_name(bin->op), bin->lhs->type->name, bin->rhs->type->name, - access); + const Exp_Binary *bin = (Exp_Binary *)data; + const m_str access = exp_access(bin->rhs); + if (access) + ERR_N(bin->rhs->pos, + _("cannot assign '%s' on types '%s' and '%s'.\n" + " ... (reason: --- right-side operand is %s.)"), + s_name(bin->op), bin->lhs->type->name, bin->rhs->type->name, access); return bin->rhs->type; } OP_CHECK(opck_rassign) { - const Exp_Binary* bin = (Exp_Binary*)data; - if(opck_const_rhs(env, data) == env->gwion->type[et_error]) + const Exp_Binary *bin = (Exp_Binary *)data; + if (opck_const_rhs(env, data) == env->gwion->type[et_error]) return env->gwion->type[et_error]; exp_setvar(bin->rhs, 1); return bin->rhs->type; } OP_CHECK(opck_unary_meta) { - const Exp_Unary* unary = (Exp_Unary*)data; + const Exp_Unary *unary = (Exp_Unary *)data; exp_setmeta(exp_self(unary), 1); return unary->exp->type; } OP_CHECK(opck_unary_meta2) { - const Exp_Unary* unary = (Exp_Unary*)data; + const Exp_Unary *unary = (Exp_Unary *)data; exp_setmeta(exp_self(unary), 1); return env->gwion->type[et_bool]; } OP_CHECK(opck_unary) { - const Exp_Unary* unary = (Exp_Unary*)data; - const m_str access = exp_access(unary->exp); - if(access) + const Exp_Unary *unary = (Exp_Unary *)data; + const m_str access = exp_access(unary->exp); + if (access) ERR_N(unary->exp->pos, _("unary operator '%s' cannot be used on %s data-types."), s_name(unary->op), access); @@ -85,10 +84,11 @@ OP_CHECK(opck_unary) { } OP_CHECK(opck_post) { - const Exp_Postfix* post = (Exp_Postfix*)data; - const m_str access = exp_access(post->exp); - if(access) - ERR_N(post->exp->pos, _("post operator '%s' cannot be used on %s data-type."), + const Exp_Postfix *post = (Exp_Postfix *)data; + const m_str access = exp_access(post->exp); + if (access) + ERR_N(post->exp->pos, + _("post operator '%s' cannot be used on %s data-type."), s_name(post->op), access); exp_setvar(post->exp, 1); return post->exp->type; @@ -97,23 +97,23 @@ OP_CHECK(opck_post) { ANN Type check_td(const Env env, Type_Decl *td); OP_CHECK(opck_new) { - const Exp_Unary* unary = (Exp_Unary*)data; + const Exp_Unary *unary = (Exp_Unary *)data; DECL_ON(const Type, t, = known_type(env, unary->td)); - if(isa(t, env->gwion->type[et_object]) < 0) + if (isa(t, env->gwion->type[et_object]) < 0) ERR_N(exp_self(unary)->pos, _("can't use 'new' on non-object types...\n")); - if(type_ref(t)) + if (type_ref(t)) ERR_N(unary->td->pos, _("can't use 'new' on ref type '%s'\n"), t->name); - if(GET_FLAG(t, abstract)) - ERR_N(unary->td->pos, _("can't use 'new' on abstract type '%s'\n"), t->name); - if(unary->td->array) - CHECK_BN(check_subscripts(env, unary->td->array, 1)); + if (GET_FLAG(t, abstract)) + ERR_N(unary->td->pos, _("can't use 'new' on abstract type '%s'\n"), + t->name); + if (unary->td->array) CHECK_BN(check_subscripts(env, unary->td->array, 1)); return t; } OP_EMIT(opem_new) { - const Exp_Unary* unary = (Exp_Unary*)data; + const Exp_Unary *unary = (Exp_Unary *)data; CHECK_BB(emit_instantiate_object(emit, exp_self(unary)->type, - unary->td->array, 0)); + unary->td->array, 0)); emit_gc(emit, -SZ_INT); return GW_OK; } diff --git a/src/lib/prim.c b/src/lib/prim.c index fec7d7ba..5d73847c 100644 --- a/src/lib/prim.c +++ b/src/lib/prim.c @@ -18,9 +18,7 @@ #define CHECK_OP(op, check, func) _CHECK_OP(op, check, int_##func) -static inline int is_prim(const Exp e) { - return e->exp_type == ae_exp_primary; -} +static inline int is_prim(const Exp e) { return e->exp_type == ae_exp_primary; } static inline int is_prim_int(const Exp e) { return (is_prim(e) && e->d.prim.prim_type == ae_prim_num); @@ -30,57 +28,58 @@ static inline int is_prim_float(const Exp e) { return (is_prim(e) && e->d.prim.prim_type == ae_prim_float); } -uint pot(const m_int x){ - return (x > 0) && ((x & (x - 1)) == 0); -} +uint pot(const m_int x) { return (x > 0) && ((x & (x - 1)) == 0); } -#define POWEROF2_OPT(name, OP) \ - if(is_prim_int(bin->rhs) && pot(bin->rhs->d.prim.d.num)) { \ - bin->op = insert_symbol(#OP);\ - bin->rhs->d.prim.d.num = sqrt(bin->rhs->d.prim.d.num);\ - return check_exp(env, exp_self(bin));\ +#define POWEROF2_OPT(name, OP) \ + if (is_prim_int(bin->rhs) && pot(bin->rhs->d.prim.d.num)) { \ + bin->op = insert_symbol(#OP); \ + bin->rhs->d.prim.d.num = sqrt(bin->rhs->d.prim.d.num); \ + return check_exp(env, exp_self(bin)); \ } -#define BINARY_FOLD(ntype, name, TYPE, OP, pre, post, funcl, funcr, ctype, exptype, \ - lmember, rmember, retmember) \ -static OP_CHECK(opck_##ntype##_##name) { \ - /*const*/ Exp_Binary *bin = (Exp_Binary*)data; \ - const Type t = env->gwion->type[TYPE]; \ - if(!exp_self(bin)->pos.first.line)return t; \ - pre \ - if(!funcl(bin->lhs) || !funcr(bin->rhs)) \ - return t; \ - post \ - const ctype num = bin->lhs->d.prim.d.lmember OP bin->rhs->d.prim.d.rmember; \ - free_exp(env->gwion->mp, bin->lhs); \ - free_exp(env->gwion->mp, bin->rhs); \ - exp_self(bin)->exp_type = ae_exp_primary; \ - exp_self(bin)->d.prim.prim_type = exptype; \ - exp_self(bin)->d.prim.d.retmember = num; \ - return t; \ -} +#define BINARY_FOLD(ntype, name, TYPE, OP, pre, post, funcl, funcr, ctype, \ + exptype, lmember, rmember, retmember) \ + static OP_CHECK(opck_##ntype##_##name) { \ + /*const*/ Exp_Binary *bin = (Exp_Binary *)data; \ + const Type t = env->gwion->type[TYPE]; \ + if (!exp_self(bin)->pos.first.line) return t; \ + pre if (!funcl(bin->lhs) || !funcr(bin->rhs)) return t; \ + post const ctype num = \ + bin->lhs->d.prim.d.lmember OP bin->rhs->d.prim.d.rmember; \ + free_exp(env->gwion->mp, bin->lhs); \ + free_exp(env->gwion->mp, bin->rhs); \ + exp_self(bin)->exp_type = ae_exp_primary; \ + exp_self(bin)->d.prim.prim_type = exptype; \ + exp_self(bin)->d.prim.d.retmember = num; \ + return t; \ + } -#define BINARY_INT_FOLD(name, TYPE, OP, pre, post) \ - BINARY_FOLD(int, name, TYPE, OP, pre, post, is_prim_int, is_prim_int, m_int, ae_prim_num, num, num, num) - -BINARY_INT_FOLD(add, et_int, +,,) -BINARY_INT_FOLD(sub, et_int, -,,) -BINARY_INT_FOLD(mul, et_int, *,POWEROF2_OPT(name, <<),) -BINARY_INT_FOLD(div, et_int, /,POWEROF2_OPT(name, >>),if(bin->rhs->d.prim.d.num == 0)ERR_N(exp_self(bin)->pos, _("ZeroDivideException"));) -BINARY_INT_FOLD(mod, et_int, %,, if(bin->rhs->d.prim.d.num == 0)ERR_N(exp_self(bin)->pos, _("ZeroDivideException"));) -BINARY_INT_FOLD(sl, et_int, <<,,) -BINARY_INT_FOLD(sr, et_int, >>,,) -BINARY_INT_FOLD(sand, et_int, &,,) -BINARY_INT_FOLD(sor, et_int, |,,) -BINARY_INT_FOLD(xor, et_int, ^,,) -BINARY_INT_FOLD(gt, et_bool, >,,) -BINARY_INT_FOLD(lt, et_bool, <,,) -BINARY_INT_FOLD(ge, et_bool, >=,,) -BINARY_INT_FOLD(le, et_bool, <=,,) -BINARY_INT_FOLD(and, et_bool, &&,,) -BINARY_INT_FOLD(or, et_bool, ||,,) -BINARY_INT_FOLD(eq, et_bool, ==,,) -BINARY_INT_FOLD(neq, et_bool, !=,,) +#define BINARY_INT_FOLD(name, TYPE, OP, pre, post) \ + BINARY_FOLD(int, name, TYPE, OP, pre, post, is_prim_int, is_prim_int, m_int, \ + ae_prim_num, num, num, num) + +BINARY_INT_FOLD(add, et_int, +, , ) +BINARY_INT_FOLD(sub, et_int, -, , ) +BINARY_INT_FOLD(mul, et_int, *, POWEROF2_OPT(name, <<), ) +BINARY_INT_FOLD(div, et_int, /, POWEROF2_OPT(name, >>), + if (bin->rhs->d.prim.d.num == 0) + ERR_N(exp_self(bin)->pos, _("ZeroDivideException"));) +BINARY_INT_FOLD(mod, et_int, %, , + if (bin->rhs->d.prim.d.num == 0) + ERR_N(exp_self(bin)->pos, _("ZeroDivideException"));) +BINARY_INT_FOLD(sl, et_int, <<, , ) +BINARY_INT_FOLD(sr, et_int, >>, , ) +BINARY_INT_FOLD(sand, et_int, &, , ) +BINARY_INT_FOLD(sor, et_int, |, , ) +BINARY_INT_FOLD(xor, et_int, ^, , ) +BINARY_INT_FOLD(gt, et_bool, >, , ) +BINARY_INT_FOLD(lt, et_bool, <, , ) +BINARY_INT_FOLD(ge, et_bool, >=, , ) +BINARY_INT_FOLD(le, et_bool, <=, , ) +BINARY_INT_FOLD(and, et_bool, &&, , ) +BINARY_INT_FOLD(or, et_bool, ||, , ) +BINARY_INT_FOLD(eq, et_bool, ==, , ) +BINARY_INT_FOLD(neq, et_bool, !=, , ) GWION_IMPORT(int_op) { GWI_BB(gwi_oper_ini(gwi, "int", "int", "int")) @@ -100,90 +99,90 @@ GWION_IMPORT(int_op) { static GWION_IMPORT(int_logical) { GWI_BB(gwi_oper_ini(gwi, "int", "int", "int")) GWI_BB(gwi_oper_add(gwi, opck_int_gt)) - GWI_BB(gwi_oper_end(gwi, ">", int_gt)) + GWI_BB(gwi_oper_end(gwi, ">", int_gt)) GWI_BB(gwi_oper_add(gwi, opck_int_ge)) - GWI_BB(gwi_oper_end(gwi, ">=", int_ge)) + GWI_BB(gwi_oper_end(gwi, ">=", int_ge)) GWI_BB(gwi_oper_add(gwi, opck_int_lt)) - GWI_BB(gwi_oper_end(gwi, "<", int_lt)) + GWI_BB(gwi_oper_end(gwi, "<", int_lt)) GWI_BB(gwi_oper_add(gwi, opck_int_le)) - GWI_BB(gwi_oper_end(gwi, "<=", int_le)) + GWI_BB(gwi_oper_end(gwi, "<=", int_le)) GWI_BB(gwi_oper_add(gwi, opck_int_sr)) - GWI_BB(gwi_oper_end(gwi, ">>", int_sr)) + GWI_BB(gwi_oper_end(gwi, ">>", int_sr)) GWI_BB(gwi_oper_add(gwi, opck_int_sl)) - GWI_BB(gwi_oper_end(gwi, "<<", int_sl)) + GWI_BB(gwi_oper_end(gwi, "<<", int_sl)) GWI_BB(gwi_oper_add(gwi, opck_int_sand)) GWI_BB(gwi_oper_end(gwi, "&", int_sand)) GWI_BB(gwi_oper_add(gwi, opck_int_sor)) - GWI_BB(gwi_oper_end(gwi, "|", int_sor)) + GWI_BB(gwi_oper_end(gwi, "|", int_sor)) GWI_BB(gwi_oper_add(gwi, opck_int_xor)) GWI_BB(gwi_oper_end(gwi, "^", int_xor)) GWI_BB(gwi_oper_ini(gwi, "int", "int", "bool")) GWI_BB(gwi_oper_add(gwi, opck_int_and)) - GWI_BB(gwi_oper_end(gwi, "&&", int_and)) + GWI_BB(gwi_oper_end(gwi, "&&", int_and)) GWI_BB(gwi_oper_add(gwi, opck_int_or)) - GWI_BB(gwi_oper_end(gwi, "||", int_or)) + GWI_BB(gwi_oper_end(gwi, "||", int_or)) GWI_BB(gwi_oper_add(gwi, opck_int_eq)) - GWI_BB(gwi_oper_end(gwi, "==", int_eq)) + GWI_BB(gwi_oper_end(gwi, "==", int_eq)) GWI_BB(gwi_oper_add(gwi, opck_int_neq)) - return gwi_oper_end(gwi, "!=", int_neq); + return gwi_oper_end(gwi, "!=", int_neq); } static GWION_IMPORT(int_r) { GWI_BB(gwi_oper_ini(gwi, "int", "int", "int")) CHECK_OP("=>", rassign, r_assign) - CHECK_OP("+=>", rassign, r_plus) - CHECK_OP("-=>", rassign, r_minus) - CHECK_OP("*=>", rassign, r_mul) - CHECK_OP("/=>", rassign, r_div) - CHECK_OP("%=>", rassign, r_modulo) - CHECK_OP("<<=>", rassign, r_sl) - CHECK_OP(">>=>", rassign, r_sr) + CHECK_OP("+=>", rassign, r_plus) + CHECK_OP("-=>", rassign, r_minus) + CHECK_OP("*=>", rassign, r_mul) + CHECK_OP("/=>", rassign, r_div) + CHECK_OP("%=>", rassign, r_modulo) + CHECK_OP("<<=>", rassign, r_sl) + CHECK_OP(">>=>", rassign, r_sr) CHECK_OP("&=>", rassign, r_sand) - CHECK_OP("|=>", rassign, r_sor) + CHECK_OP("|=>", rassign, r_sor) CHECK_OP("^=>", rassign, r_sxor) return GW_OK; } static INSTR(IntRange) { shred->reg -= SZ_INT; - const m_int start = *(m_int*)REG(-SZ_INT); - const m_int end = *(m_int*)REG(0); - const m_int op = start < end ? 1 : -1; - const m_uint sz = op > 0 ? end - start : start - end; + const m_int start = *(m_int *)REG(-SZ_INT); + const m_int end = *(m_int *)REG(0); + const m_int op = start < end ? 1 : -1; + const m_uint sz = op > 0 ? end - start : start - end; const M_Object array = new_array(shred->info->mp, (Type)instr->m_val, sz); - for(m_int i = start, j = 0; i != end; i += op, ++j) + for (m_int i = start, j = 0; i != end; i += op, ++j) m_vector_set(ARRAY(array), j, &i); - *(M_Object*)REG(-SZ_INT) = array; + *(M_Object *)REG(-SZ_INT) = array; } static OP_CHECK(opck_int_range) { - const Exp exp = (Exp)data; + const Exp exp = (Exp)data; const Range *range = exp->d.prim.d.range; - const Exp e = range->start ?: range->end; + const Exp e = range->start ?: range->end; return array_type(env, e->type, 1); } static OP_EMIT(opem_int_range) { - const Exp exp = (Exp)data; + const Exp exp = (Exp)data; const Instr instr = emit_add_instr(emit, IntRange); - instr->m_val = (m_uint)exp->type; + instr->m_val = (m_uint)exp->type; return GW_OK; } -#define UNARY_FOLD(ntype, name, TYPE, OP, func, ctype, exptype, member) \ -static OP_CHECK(opck_##ntype##_##name) { \ - /*const*/ Exp_Unary *unary = (Exp_Unary*)data; \ - const Type t = env->gwion->type[TYPE]; \ - if(!exp_self(unary)->pos.first.line || !func(unary->exp)) \ - return t; \ - CHECK_NN(opck_unary_meta(env, data)); \ - const ctype num = OP unary->exp->d.prim.d.member; \ - exp_self(unary)->exp_type = ae_exp_primary; \ - exp_self(unary)->d.prim.prim_type = exptype; \ - exp_self(unary)->d.prim.d.num = num; \ - return t; \ -} -#define UNARY_INT_FOLD(name, TYPE, OP) UNARY_FOLD(int, name, TYPE, OP, is_prim_int, m_int, ae_prim_num, num) +#define UNARY_FOLD(ntype, name, TYPE, OP, func, ctype, exptype, member) \ + static OP_CHECK(opck_##ntype##_##name) { \ + /*const*/ Exp_Unary *unary = (Exp_Unary *)data; \ + const Type t = env->gwion->type[TYPE]; \ + if (!exp_self(unary)->pos.first.line || !func(unary->exp)) return t; \ + CHECK_NN(opck_unary_meta(env, data)); \ + const ctype num = OP unary->exp->d.prim.d.member; \ + exp_self(unary)->exp_type = ae_exp_primary; \ + exp_self(unary)->d.prim.prim_type = exptype; \ + exp_self(unary)->d.prim.d.num = num; \ + return t; \ + } +#define UNARY_INT_FOLD(name, TYPE, OP) \ + UNARY_FOLD(int, name, TYPE, OP, is_prim_int, m_int, ae_prim_num, num) UNARY_INT_FOLD(negate, et_int, -) UNARY_INT_FOLD(cmp, et_int, ~) UNARY_INT_FOLD(not, et_bool, !) @@ -195,7 +194,7 @@ static GWION_IMPORT(int_unary) { CHECK_OP("++", unary, pre_inc) CHECK_OP("--", unary, pre_dec) GWI_BB(gwi_oper_add(gwi, opck_int_cmp)) - GWI_BB(gwi_oper_end(gwi, "~", int_cmp)) + GWI_BB(gwi_oper_end(gwi, "~", int_cmp)) GWI_BB(gwi_oper_ini(gwi, NULL, "int", NULL)) GWI_BB(gwi_oper_add(gwi, opck_int_range)) GWI_BB(gwi_oper_emi(gwi, opem_int_range)) @@ -207,8 +206,8 @@ static GWION_IMPORT(int_unary) { return GW_OK; } static GACK(gack_bool) { -// gw_out("%s", *(m_uint*)VALUE ? "true" : "false"); - INTERP_PRINTF("%s", *(m_uint*)VALUE ? "true" : "false"); + // gw_out("%s", *(m_uint*)VALUE ? "true" : "false"); + INTERP_PRINTF("%s", *(m_uint *)VALUE ? "true" : "false"); } static GWION_IMPORT(int_values) { @@ -221,8 +220,9 @@ static GWION_IMPORT(int_values) { GWI_BB(gwi_oper_ini(gwi, NULL, "int", "bool")) GWI_BB(gwi_oper_add(gwi, opck_unary_meta)) GWI_BB(gwi_oper_add(gwi, opck_int_not)) - GWI_BB(gwi_oper_end(gwi, "!", IntNot)) - struct SpecialId_ spid = { .type=t_bool, .exec=RegPushMaybe, .is_const=1 }; + GWI_BB(gwi_oper_end(gwi, "!", IntNot)) + struct SpecialId_ spid = { + .type = t_bool, .exec = RegPushMaybe, .is_const = 1}; gwi_specialid(gwi, "maybe", &spid); return GW_OK; } @@ -238,64 +238,60 @@ static GWION_IMPORT(int) { return GW_OK; } -static OP_CHECK(opck_cast_f2i) { - return env->gwion->type[et_int]; -} +static OP_CHECK(opck_cast_f2i) { return env->gwion->type[et_int]; } -static OP_CHECK(opck_implicit_f2i) { - return env->gwion->type[et_error]; -} +static OP_CHECK(opck_implicit_f2i) { return env->gwion->type[et_error]; } -static OP_CHECK(opck_cast_i2f) { - return env->gwion->type[et_float]; -} +static OP_CHECK(opck_cast_i2f) { return env->gwion->type[et_float]; } -static OP_CHECK(opck_implicit_i2f) { - return env->gwion->type[et_float]; -} +static OP_CHECK(opck_implicit_i2f) { return env->gwion->type[et_float]; } #define CHECK_FF(op, check, func) _CHECK_OP(op, check, float_##func) #define CHECK_IF(op, check, func) _CHECK_OP(op, check, int_float_##func) #define CHECK_FI(op, check, func) _CHECK_OP(op, check, float_int_##func) -#define BINARY_INT_FLOAT_FOLD(name, TYPE, OP, pre, post) \ - BINARY_FOLD(int_float, name, TYPE, OP, pre, post, is_prim_int, is_prim_float, m_float, ae_prim_float, num, fnum, fnum) -#define BINARY_INT_FLOAT_FOLD2(name, TYPE, OP, pre, post) \ - BINARY_FOLD(int_float, name, TYPE, OP, pre, post, is_prim_int, is_prim_float, m_float, ae_prim_float, num, fnum, num) - -BINARY_INT_FLOAT_FOLD(add, et_float, +,,) -BINARY_INT_FLOAT_FOLD(sub, et_float, -,,) -BINARY_INT_FLOAT_FOLD(mul, et_float, *,/*POWEROF2_OPT(name, <<)*/,) -BINARY_INT_FLOAT_FOLD(div, et_float, /,/*POWEROF2_OPT(name, >>)*/,if(bin->rhs->d.prim.d.fnum == 0)ERR_N(exp_self(bin)->pos, _("ZeroDivideException"));) -BINARY_INT_FLOAT_FOLD2(gt, et_bool, >,,) -BINARY_INT_FLOAT_FOLD2(ge, et_bool, >=,,) -BINARY_INT_FLOAT_FOLD2(lt, et_bool, <=,,) -BINARY_INT_FLOAT_FOLD2(le, et_bool, <=,,) -BINARY_INT_FLOAT_FOLD2(and, et_bool, &&,,) -BINARY_INT_FLOAT_FOLD2(or, et_bool, ||,,) -BINARY_INT_FLOAT_FOLD2(eq, et_bool, ==,,) -BINARY_INT_FLOAT_FOLD2(neq, et_bool, !=,,) +#define BINARY_INT_FLOAT_FOLD(name, TYPE, OP, pre, post) \ + BINARY_FOLD(int_float, name, TYPE, OP, pre, post, is_prim_int, \ + is_prim_float, m_float, ae_prim_float, num, fnum, fnum) +#define BINARY_INT_FLOAT_FOLD2(name, TYPE, OP, pre, post) \ + BINARY_FOLD(int_float, name, TYPE, OP, pre, post, is_prim_int, \ + is_prim_float, m_float, ae_prim_float, num, fnum, num) + +BINARY_INT_FLOAT_FOLD(add, et_float, +, , ) +BINARY_INT_FLOAT_FOLD(sub, et_float, -, , ) +BINARY_INT_FLOAT_FOLD(mul, et_float, *, /*POWEROF2_OPT(name, <<)*/, ) +BINARY_INT_FLOAT_FOLD(div, et_float, /, /*POWEROF2_OPT(name, >>)*/, + if (bin->rhs->d.prim.d.fnum == 0) + ERR_N(exp_self(bin)->pos, _("ZeroDivideException"));) +BINARY_INT_FLOAT_FOLD2(gt, et_bool, >, , ) +BINARY_INT_FLOAT_FOLD2(ge, et_bool, >=, , ) +BINARY_INT_FLOAT_FOLD2(lt, et_bool, <=, , ) +BINARY_INT_FLOAT_FOLD2(le, et_bool, <=, , ) +BINARY_INT_FLOAT_FOLD2(and, et_bool, &&, , ) +BINARY_INT_FLOAT_FOLD2(or, et_bool, ||, , ) +BINARY_INT_FLOAT_FOLD2(eq, et_bool, ==, , ) +BINARY_INT_FLOAT_FOLD2(neq, et_bool, !=, , ) static GWION_IMPORT(intfloat) { GWI_BB(gwi_oper_ini(gwi, "int", "float", "int")) GWI_BB(gwi_oper_add(gwi, opck_int_float_gt)) - GWI_BB(gwi_oper_end(gwi, ">", int_float_gt)) + GWI_BB(gwi_oper_end(gwi, ">", int_float_gt)) GWI_BB(gwi_oper_add(gwi, opck_int_float_ge)) - GWI_BB(gwi_oper_end(gwi, ">=", int_float_ge)) + GWI_BB(gwi_oper_end(gwi, ">=", int_float_ge)) GWI_BB(gwi_oper_add(gwi, opck_int_float_lt)) - GWI_BB(gwi_oper_end(gwi, "<", int_float_lt)) + GWI_BB(gwi_oper_end(gwi, "<", int_float_lt)) GWI_BB(gwi_oper_add(gwi, opck_int_float_le)) - GWI_BB(gwi_oper_end(gwi, "<=", int_float_le)) + GWI_BB(gwi_oper_end(gwi, "<=", int_float_le)) GWI_BB(gwi_oper_ini(gwi, "int", "float", "float")) GWI_BB(gwi_oper_add(gwi, opck_int_float_add)) - GWI_BB(gwi_oper_end(gwi, "+", int_float_plus)) + GWI_BB(gwi_oper_end(gwi, "+", int_float_plus)) GWI_BB(gwi_oper_add(gwi, opck_int_float_mul)) - GWI_BB(gwi_oper_end(gwi, "*", int_float_mul)) + GWI_BB(gwi_oper_end(gwi, "*", int_float_mul)) GWI_BB(gwi_oper_add(gwi, opck_int_float_sub)) - GWI_BB(gwi_oper_end(gwi, "-", int_float_minus)) + GWI_BB(gwi_oper_end(gwi, "-", int_float_minus)) GWI_BB(gwi_oper_add(gwi, opck_int_float_div)) GWI_BB(gwi_oper_eff(gwi, "ZeroDivideException")) - GWI_BB(gwi_oper_end(gwi, "/", int_float_div)) + GWI_BB(gwi_oper_end(gwi, "/", int_float_div)) CHECK_IF("=>", rassign, r_assign) CHECK_IF("+=>", rassign, r_plus) CHECK_IF("-=>", rassign, r_minus) @@ -305,47 +301,51 @@ static GWION_IMPORT(intfloat) { _CHECK_OP("@implicit", implicit_i2f, CastI2F) GWI_BB(gwi_oper_ini(gwi, "int", "float", "bool")) GWI_BB(gwi_oper_add(gwi, opck_int_float_and)) - GWI_BB(gwi_oper_end(gwi, "&&", int_float_and)) + GWI_BB(gwi_oper_end(gwi, "&&", int_float_and)) GWI_BB(gwi_oper_add(gwi, opck_int_float_or)) - GWI_BB(gwi_oper_end(gwi, "||", int_float_or)) + GWI_BB(gwi_oper_end(gwi, "||", int_float_or)) GWI_BB(gwi_oper_add(gwi, opck_int_float_eq)) - GWI_BB(gwi_oper_end(gwi, "==", int_float_eq)) + GWI_BB(gwi_oper_end(gwi, "==", int_float_eq)) GWI_BB(gwi_oper_add(gwi, opck_int_float_neq)) - GWI_BB(gwi_oper_end(gwi, "!=", int_float_neq)) + GWI_BB(gwi_oper_end(gwi, "!=", int_float_neq)) return GW_OK; } -#define BINARY_FLOAT_INT_FOLD(name, TYPE, OP, pre, post) \ - BINARY_FOLD(float_int, name, TYPE, OP, pre, post, is_prim_float, is_prim_int, m_float, ae_prim_float, fnum, num, fnum) - -#define BINARY_FLOAT_INT_FOLD2(name, TYPE, OP, pre, post) \ - BINARY_FOLD(float_int, name, TYPE, OP, pre, post, is_prim_float, is_prim_int, m_int, ae_prim_num, fnum, num, num) - -BINARY_FLOAT_INT_FOLD(add, et_float, +,,) -BINARY_FLOAT_INT_FOLD(sub, et_float, -,,) -BINARY_FLOAT_INT_FOLD(mul, et_float, *,/*POWEROF2_OPT(name, <<)*/,) -BINARY_FLOAT_INT_FOLD(div, et_float, /,/*POWEROF2_OPT(name, >>)*/,if(bin->rhs->d.prim.d.num == 0)ERR_N(exp_self(bin)->pos, _("ZeroDivideException"));) - -BINARY_FLOAT_INT_FOLD2(gt, et_bool, >,,) -BINARY_FLOAT_INT_FOLD2(ge, et_bool, >=,,) -BINARY_FLOAT_INT_FOLD2(lt, et_bool, <=,,) -BINARY_FLOAT_INT_FOLD2(le, et_bool, <=,,) -BINARY_FLOAT_INT_FOLD2(and, et_bool, &&,,) -BINARY_FLOAT_INT_FOLD2(or, et_bool, ||,,) -BINARY_FLOAT_INT_FOLD2(eq, et_bool, ==,,) -BINARY_FLOAT_INT_FOLD2(neq, et_bool, !=,,) +#define BINARY_FLOAT_INT_FOLD(name, TYPE, OP, pre, post) \ + BINARY_FOLD(float_int, name, TYPE, OP, pre, post, is_prim_float, \ + is_prim_int, m_float, ae_prim_float, fnum, num, fnum) + +#define BINARY_FLOAT_INT_FOLD2(name, TYPE, OP, pre, post) \ + BINARY_FOLD(float_int, name, TYPE, OP, pre, post, is_prim_float, \ + is_prim_int, m_int, ae_prim_num, fnum, num, num) + +BINARY_FLOAT_INT_FOLD(add, et_float, +, , ) +BINARY_FLOAT_INT_FOLD(sub, et_float, -, , ) +BINARY_FLOAT_INT_FOLD(mul, et_float, *, /*POWEROF2_OPT(name, <<)*/, ) +BINARY_FLOAT_INT_FOLD(div, et_float, /, /*POWEROF2_OPT(name, >>)*/, + if (bin->rhs->d.prim.d.num == 0) + ERR_N(exp_self(bin)->pos, _("ZeroDivideException"));) + +BINARY_FLOAT_INT_FOLD2(gt, et_bool, >, , ) +BINARY_FLOAT_INT_FOLD2(ge, et_bool, >=, , ) +BINARY_FLOAT_INT_FOLD2(lt, et_bool, <=, , ) +BINARY_FLOAT_INT_FOLD2(le, et_bool, <=, , ) +BINARY_FLOAT_INT_FOLD2(and, et_bool, &&, , ) +BINARY_FLOAT_INT_FOLD2(or, et_bool, ||, , ) +BINARY_FLOAT_INT_FOLD2(eq, et_bool, ==, , ) +BINARY_FLOAT_INT_FOLD2(neq, et_bool, !=, , ) static GWION_IMPORT(floatint) { GWI_BB(gwi_oper_ini(gwi, "float", "int", "float")) GWI_BB(gwi_oper_add(gwi, opck_float_int_add)) - GWI_BB(gwi_oper_end(gwi, "+", float_int_plus)) + GWI_BB(gwi_oper_end(gwi, "+", float_int_plus)) GWI_BB(gwi_oper_add(gwi, opck_float_int_sub)) - GWI_BB(gwi_oper_end(gwi, "-", float_int_minus)) + GWI_BB(gwi_oper_end(gwi, "-", float_int_minus)) GWI_BB(gwi_oper_add(gwi, opck_float_int_mul)) - GWI_BB(gwi_oper_end(gwi, "*", float_int_mul)) + GWI_BB(gwi_oper_end(gwi, "*", float_int_mul)) GWI_BB(gwi_oper_add(gwi, opck_float_int_div)) GWI_BB(gwi_oper_eff(gwi, "ZeroDivideException")) - GWI_BB(gwi_oper_end(gwi, "/", float_int_div)) + GWI_BB(gwi_oper_end(gwi, "/", float_int_div)) CHECK_FI("=>", rassign, r_assign) CHECK_FI("+=>", rassign, r_plus) CHECK_FI("-=>", rassign, r_minus) @@ -355,21 +355,21 @@ static GWION_IMPORT(floatint) { _CHECK_OP("@implicit", implicit_f2i, CastF2I) GWI_BB(gwi_oper_ini(gwi, "float", "int", "bool")) GWI_BB(gwi_oper_add(gwi, opck_float_int_and)) - GWI_BB(gwi_oper_end(gwi, "&&", float_int_and)) + GWI_BB(gwi_oper_end(gwi, "&&", float_int_and)) GWI_BB(gwi_oper_add(gwi, opck_float_int_or)) - GWI_BB(gwi_oper_end(gwi, "||", float_int_or)) + GWI_BB(gwi_oper_end(gwi, "||", float_int_or)) GWI_BB(gwi_oper_add(gwi, opck_float_int_eq)) - GWI_BB(gwi_oper_end(gwi, "==", float_int_eq)) + GWI_BB(gwi_oper_end(gwi, "==", float_int_eq)) GWI_BB(gwi_oper_add(gwi, opck_float_int_neq)) - GWI_BB(gwi_oper_end(gwi, "!=", float_int_neq)) + GWI_BB(gwi_oper_end(gwi, "!=", float_int_neq)) GWI_BB(gwi_oper_add(gwi, opck_float_int_gt)) - GWI_BB(gwi_oper_end(gwi, ">", float_int_gt)) + GWI_BB(gwi_oper_end(gwi, ">", float_int_gt)) GWI_BB(gwi_oper_add(gwi, opck_float_int_ge)) - GWI_BB(gwi_oper_end(gwi, ">=", float_int_ge)) + GWI_BB(gwi_oper_end(gwi, ">=", float_int_ge)) GWI_BB(gwi_oper_add(gwi, opck_float_int_lt)) - GWI_BB(gwi_oper_end(gwi, "<", float_int_lt)) + GWI_BB(gwi_oper_end(gwi, "<", float_int_lt)) GWI_BB(gwi_oper_add(gwi, opck_float_int_le)) - GWI_BB(gwi_oper_end(gwi, "<=", float_int_le)) + GWI_BB(gwi_oper_end(gwi, "<=", float_int_le)) return GW_OK; } @@ -381,39 +381,37 @@ static GWION_IMPORT(dur) { CHECK_FF("-=>", rassign, r_minus) CHECK_FF("*=>", rassign, r_mul) CHECK_FF("/=>", rassign, r_div) - GWI_BB(gwi_oper_end(gwi, "+", FloatPlus)) - GWI_BB(gwi_oper_end(gwi, "-", FloatMinus)) - GWI_BB(gwi_oper_end(gwi, "*", FloatTimes)) + GWI_BB(gwi_oper_end(gwi, "+", FloatPlus)) + GWI_BB(gwi_oper_end(gwi, "-", FloatMinus)) + GWI_BB(gwi_oper_end(gwi, "*", FloatTimes)) GWI_BB(gwi_oper_ini(gwi, "dur", "dur", "float")) GWI_BB(gwi_oper_eff(gwi, "ZeroDivideException")) - GWI_BB(gwi_oper_end(gwi, "/", FloatDivide)) + GWI_BB(gwi_oper_end(gwi, "/", FloatDivide)) GWI_BB(gwi_oper_ini(gwi, "dur", "float", "dur")) GWI_BB(gwi_oper_eff(gwi, "ZeroDivideException")) - GWI_BB(gwi_oper_end(gwi, "/", FloatDivide)) + GWI_BB(gwi_oper_end(gwi, "/", FloatDivide)) GWI_BB(gwi_oper_ini(gwi, "float", "dur", "dur")) CHECK_FF("*=>", rassign, r_mul) CHECK_FF("/=>", rassign, r_div) GWI_BB(gwi_oper_ini(gwi, "dur", "dur", "int")) - GWI_BB(gwi_oper_end(gwi, ">", float_gt)) - GWI_BB(gwi_oper_end(gwi, ">=", float_ge)) - GWI_BB(gwi_oper_end(gwi, "<", float_lt)) - return gwi_oper_end(gwi, "<=", float_le); + GWI_BB(gwi_oper_end(gwi, ">", float_gt)) + GWI_BB(gwi_oper_end(gwi, ">=", float_ge)) + GWI_BB(gwi_oper_end(gwi, "<", float_lt)) + return gwi_oper_end(gwi, "<=", float_le); } - static inline int is_now(const Env env, const Exp exp) { return exp->exp_type == ae_exp_primary && - exp->d.prim.prim_type == ae_prim_id && - exp->d.prim.d.var == insert_symbol("now"); + exp->d.prim.prim_type == ae_prim_id && + exp->d.prim.d.var == insert_symbol("now"); } static OP_CHECK(opck_now) { - const Exp_Binary* bin = (Exp_Binary*)data; - if(!is_now(env, bin->rhs)) - CHECK_NN(opck_const_rhs(env, data)); + const Exp_Binary *bin = (Exp_Binary *)data; + if (!is_now(env, bin->rhs)) CHECK_NN(opck_const_rhs(env, data)); exp_setvar(bin->rhs, 1); return bin->rhs->type; } @@ -423,64 +421,70 @@ static GWION_IMPORT(time) { GWI_BB(gwi_oper_ini(gwi, "time", "time", "time")) CHECK_FF("=>", rassign, r_assign) GWI_BB(gwi_oper_ini(gwi, "time", "dur", "time")) - GWI_BB(gwi_oper_end(gwi, "+", FloatPlus)) - GWI_BB(gwi_oper_end(gwi, "*", FloatTimes)) + GWI_BB(gwi_oper_end(gwi, "+", FloatPlus)) + GWI_BB(gwi_oper_end(gwi, "*", FloatTimes)) GWI_BB(gwi_oper_eff(gwi, "ZeroDivideException")) - GWI_BB(gwi_oper_end(gwi, "/", FloatDivide)) + GWI_BB(gwi_oper_end(gwi, "/", FloatDivide)) GWI_BB(gwi_oper_ini(gwi, "time", "time", "dur")) - GWI_BB(gwi_oper_end(gwi, "-", FloatMinus)) + GWI_BB(gwi_oper_end(gwi, "-", FloatMinus)) GWI_BB(gwi_oper_ini(gwi, "float", "time", "time")) CHECK_FF("*=>", rassign, r_mul) CHECK_FF("/=>", rassign, r_div) GWI_BB(gwi_oper_ini(gwi, "dur", "time", "time")) CHECK_FF("=>", rassign, r_assign) - GWI_BB(gwi_oper_end(gwi, "+", FloatPlus)) - GWI_BB(gwi_oper_ini(gwi, "dur", "@now", "time")) + GWI_BB(gwi_oper_end(gwi, "+", FloatPlus)) + GWI_BB(gwi_oper_ini(gwi, "dur", "@now", "time")) _CHECK_OP("=>", now, Time_Advance) GWI_BB(gwi_oper_ini(gwi, "time", "time", "int")) - GWI_BB(gwi_oper_end(gwi, ">", float_gt)) - GWI_BB(gwi_oper_end(gwi, ">=", float_ge)) - GWI_BB(gwi_oper_end(gwi, "<", float_lt)) - return gwi_oper_end(gwi, "<=", float_le); + GWI_BB(gwi_oper_end(gwi, ">", float_gt)) + GWI_BB(gwi_oper_end(gwi, ">=", float_ge)) + GWI_BB(gwi_oper_end(gwi, "<", float_lt)) + return gwi_oper_end(gwi, "<=", float_le); } -#define BINARY_FLOAT_FOLD(name, TYPE, OP, pre, post) \ - BINARY_FOLD(float, name, TYPE, OP, pre, post, is_prim_float, is_prim_float, m_float, ae_prim_float, fnum, fnum, fnum) - -#define BINARY_FLOAT_FOLD2(name, TYPE, OP, pre, post) \ - BINARY_FOLD(float, name, TYPE, OP, pre, post, is_prim_float, is_prim_float, m_int, ae_prim_num, fnum, fnum, num) - -BINARY_FLOAT_FOLD(add, et_float, +,,) -BINARY_FLOAT_FOLD(sub, et_float, -,,) -BINARY_FLOAT_FOLD(mul, et_float, *,/*POWEROF2_OPT(name, <<)*/,) -BINARY_FLOAT_FOLD(div, et_float, /,/*POWEROF2_OPT(name, >>)*/,if(bin->rhs->d.prim.d.fnum == 0)ERR_N(exp_self(bin)->pos, _("ZeroDivideException"));) -//BINARY_FLOAT_FOLD(mod, et_float, %,, if(bin->rhs->d.prim.d.fnum == 0)ERR_N(exp_self(bin)->pos, _("ZeroDivideException"));) -BINARY_FLOAT_FOLD2(and, et_bool, &&,,) -BINARY_FLOAT_FOLD2(or, et_bool, ||,,) -BINARY_FLOAT_FOLD2(eq, et_bool, ==,,) -BINARY_FLOAT_FOLD2(neq, et_bool, !=,,) -BINARY_FLOAT_FOLD2(gt, et_bool, >,,) -BINARY_FLOAT_FOLD2(ge, et_bool, >=,,) -BINARY_FLOAT_FOLD2(lt, et_bool, <,,) -BINARY_FLOAT_FOLD2(le, et_bool, <=,,) - -#define UNARY_FLOAT_FOLD(name, TYPE, OP) UNARY_FOLD(float, name, TYPE, OP, is_prim_int, m_float, ae_prim_float, fnum) +#define BINARY_FLOAT_FOLD(name, TYPE, OP, pre, post) \ + BINARY_FOLD(float, name, TYPE, OP, pre, post, is_prim_float, is_prim_float, \ + m_float, ae_prim_float, fnum, fnum, fnum) + +#define BINARY_FLOAT_FOLD2(name, TYPE, OP, pre, post) \ + BINARY_FOLD(float, name, TYPE, OP, pre, post, is_prim_float, is_prim_float, \ + m_int, ae_prim_num, fnum, fnum, num) + +BINARY_FLOAT_FOLD(add, et_float, +, , ) +BINARY_FLOAT_FOLD(sub, et_float, -, , ) +BINARY_FLOAT_FOLD(mul, et_float, *, /*POWEROF2_OPT(name, <<)*/, ) +BINARY_FLOAT_FOLD(div, et_float, /, /*POWEROF2_OPT(name, >>)*/, + if (bin->rhs->d.prim.d.fnum == 0) + ERR_N(exp_self(bin)->pos, _("ZeroDivideException"));) +// BINARY_FLOAT_FOLD(mod, et_float, %,, if(bin->rhs->d.prim.d.fnum == +// 0)ERR_N(exp_self(bin)->pos, _("ZeroDivideException"));) +BINARY_FLOAT_FOLD2(and, et_bool, &&, , ) +BINARY_FLOAT_FOLD2(or, et_bool, ||, , ) +BINARY_FLOAT_FOLD2(eq, et_bool, ==, , ) +BINARY_FLOAT_FOLD2(neq, et_bool, !=, , ) +BINARY_FLOAT_FOLD2(gt, et_bool, >, , ) +BINARY_FLOAT_FOLD2(ge, et_bool, >=, , ) +BINARY_FLOAT_FOLD2(lt, et_bool, <, , ) +BINARY_FLOAT_FOLD2(le, et_bool, <=, , ) + +#define UNARY_FLOAT_FOLD(name, TYPE, OP) \ + UNARY_FOLD(float, name, TYPE, OP, is_prim_int, m_float, ae_prim_float, fnum) UNARY_FLOAT_FOLD(negate, et_float, -) -//UNARY_INT_FOLD(cmp, et_float, ~) +// UNARY_INT_FOLD(cmp, et_float, ~) UNARY_FLOAT_FOLD(not, et_bool, !) static GWION_IMPORT(float) { GWI_BB(gwi_oper_cond(gwi, "float", BranchEqFloat, BranchNeqFloat)) GWI_BB(gwi_oper_ini(gwi, "float", "float", "float")) GWI_BB(gwi_oper_add(gwi, opck_float_add)) - GWI_BB(gwi_oper_end(gwi, "+", FloatPlus)) + GWI_BB(gwi_oper_end(gwi, "+", FloatPlus)) GWI_BB(gwi_oper_add(gwi, opck_float_sub)) - GWI_BB(gwi_oper_end(gwi, "-", FloatMinus)) + GWI_BB(gwi_oper_end(gwi, "-", FloatMinus)) GWI_BB(gwi_oper_add(gwi, opck_float_mul)) - GWI_BB(gwi_oper_end(gwi, "*", FloatTimes)) + GWI_BB(gwi_oper_end(gwi, "*", FloatTimes)) GWI_BB(gwi_oper_add(gwi, opck_float_div)) GWI_BB(gwi_oper_eff(gwi, "ZeroDivideException")) - GWI_BB(gwi_oper_end(gwi, "/", FloatDivide)) + GWI_BB(gwi_oper_end(gwi, "/", FloatDivide)) GWI_BB(gwi_oper_end(gwi, "@implicit", NULL)) CHECK_FF("=>", rassign, r_assign) CHECK_FF("+=>", rassign, r_plus) @@ -489,33 +493,33 @@ static GWION_IMPORT(float) { CHECK_FF("/=>", rassign, r_div) GWI_BB(gwi_oper_ini(gwi, "float", "float", "bool")) GWI_BB(gwi_oper_add(gwi, opck_float_and)) - GWI_BB(gwi_oper_end(gwi, "&&", float_and)) + GWI_BB(gwi_oper_end(gwi, "&&", float_and)) GWI_BB(gwi_oper_add(gwi, opck_float_or)) - GWI_BB(gwi_oper_end(gwi, "||", float_or)) + GWI_BB(gwi_oper_end(gwi, "||", float_or)) GWI_BB(gwi_oper_add(gwi, opck_float_eq)) - GWI_BB(gwi_oper_end(gwi, "==", float_eq)) + GWI_BB(gwi_oper_end(gwi, "==", float_eq)) GWI_BB(gwi_oper_add(gwi, opck_float_neq)) - GWI_BB(gwi_oper_end(gwi, "!=", float_neq)) + GWI_BB(gwi_oper_end(gwi, "!=", float_neq)) GWI_BB(gwi_oper_add(gwi, opck_float_gt)) - GWI_BB(gwi_oper_end(gwi, ">", float_gt)) + GWI_BB(gwi_oper_end(gwi, ">", float_gt)) GWI_BB(gwi_oper_add(gwi, opck_float_ge)) - GWI_BB(gwi_oper_end(gwi, ">=", float_ge)) + GWI_BB(gwi_oper_end(gwi, ">=", float_ge)) GWI_BB(gwi_oper_add(gwi, opck_float_lt)) - GWI_BB(gwi_oper_end(gwi, "<", float_lt)) + GWI_BB(gwi_oper_end(gwi, "<", float_lt)) GWI_BB(gwi_oper_add(gwi, opck_float_le)) - GWI_BB(gwi_oper_end(gwi, "<=", float_le)) - GWI_BB(gwi_oper_ini(gwi, NULL, "float", "float")) -// CHECK_FF("-", unary_meta, negate) + GWI_BB(gwi_oper_end(gwi, "<=", float_le)) + GWI_BB(gwi_oper_ini(gwi, NULL, "float", "float")) + // CHECK_FF("-", unary_meta, negate) GWI_BB(gwi_oper_add(gwi, opck_float_negate)) - GWI_BB(gwi_oper_end(gwi, "-", float_negate)) + GWI_BB(gwi_oper_end(gwi, "-", float_negate)) GWI_BB(gwi_oper_ini(gwi, "int", "dur", "dur")) - GWI_BB(gwi_oper_end(gwi, "::", int_float_mul)) + GWI_BB(gwi_oper_end(gwi, "::", int_float_mul)) GWI_BB(gwi_oper_ini(gwi, "float", "dur", "dur")) - GWI_BB(gwi_oper_end(gwi, "::", FloatTimes)) - GWI_BB(gwi_oper_ini(gwi, NULL, "float", "bool")) -// GWI_BB(gwi_oper_add(gwi, opck_unary_meta2)) + GWI_BB(gwi_oper_end(gwi, "::", FloatTimes)) + GWI_BB(gwi_oper_ini(gwi, NULL, "float", "bool")) + // GWI_BB(gwi_oper_add(gwi, opck_unary_meta2)) GWI_BB(gwi_oper_add(gwi, opck_float_not)) - GWI_BB(gwi_oper_end(gwi, "!", float_not)) + GWI_BB(gwi_oper_end(gwi, "!", float_not)) return GW_OK; } diff --git a/src/lib/prim_values.c b/src/lib/prim_values.c index 4236549e..c148fd25 100644 --- a/src/lib/prim_values.c +++ b/src/lib/prim_values.c @@ -17,7 +17,7 @@ #include "gack.h" GWION_IMPORT(values) { - VM* vm = gwi_vm(gwi); + VM * vm = gwi_vm(gwi); const m_float sr = vm->bbq->si->sr; gwi_item_ini(gwi, "float", "samplerate"); gwi_item_end(gwi, ae_flag_const, fnum, sr); @@ -28,13 +28,13 @@ GWION_IMPORT(values) { gwi_item_ini(gwi, "dur", "samp"); gwi_item_end(gwi, ae_flag_const, fnum, 1.0); gwi_item_ini(gwi, "dur", "ms"); - gwi_item_end(gwi, ae_flag_const, fnum, sr/1000.0); + gwi_item_end(gwi, ae_flag_const, fnum, sr / 1000.0); gwi_item_ini(gwi, "dur", "second"); gwi_item_end(gwi, ae_flag_const, fnum, sr); gwi_item_ini(gwi, "dur", "minute"); - gwi_item_end(gwi, ae_flag_const, fnum, sr*60.0); + gwi_item_end(gwi, ae_flag_const, fnum, sr * 60.0); gwi_item_ini(gwi, "dur", "hour"); - gwi_item_end(gwi, ae_flag_const, fnum, sr*60*60); + gwi_item_end(gwi, ae_flag_const, fnum, sr * 60 * 60); gwi_item_ini(gwi, "time", "t_zero"); gwi_item_end(gwi, ae_flag_const, fnum, 0.0); return GW_OK; diff --git a/src/lib/ptr.c b/src/lib/ptr.c index 86414867..a0693e65 100644 --- a/src/lib/ptr.c +++ b/src/lib/ptr.c @@ -17,8 +17,7 @@ static m_bool ptr_access(const Env env, const Exp e) { const m_str access = exp_access(e); - if(!access) - return GW_OK; + if (!access) return GW_OK; ERR_B(e->pos, _("operand is %s"), access); } @@ -28,7 +27,7 @@ ANN static inline Type ptr_base(const Env env, const Type t) { } static OP_CHECK(opck_ptr_assign) { - const Exp_Binary* bin = (Exp_Binary*)data; + const Exp_Binary *bin = (Exp_Binary *)data; CHECK_BN(ptr_access(env, bin->lhs)); CHECK_BN(ptr_access(env, bin->rhs)); exp_setvar(bin->lhs, 1); @@ -38,49 +37,46 @@ static OP_CHECK(opck_ptr_assign) { Type u = bin->rhs->type; do { const Type base = ptr_base(env, u); - if(isa(t, base) > 0) - return t; - } while((u = u->info->parent) && u->info->cdef->base.tmpl->call); - } while((t = t->info->parent)); + if (isa(t, base) > 0) return t; + } while ((u = u->info->parent) && u->info->cdef->base.tmpl->call); + } while ((t = t->info->parent)); return env->gwion->type[et_error]; } static OP_EMIT(opem_ptr_assign) { - const Exp_Binary* bin = (Exp_Binary*)data; - const Type t = bin->lhs->cast_to ?: bin->lhs->type; - if(isa(t, emit->gwion->type[et_compound]) > 0) + const Exp_Binary *bin = (Exp_Binary *)data; + const Type t = bin->lhs->cast_to ?: bin->lhs->type; + if (isa(t, emit->gwion->type[et_compound]) > 0) (void)emit_compound_addref(emit, t, -(SZ_INT + t->size), 0); emit_add_instr(emit, int_r_assign); return GW_OK; } static OP_CHECK(opck_ptr_deref) { - const Exp_Unary* unary = (Exp_Unary*)data; + const Exp_Unary *unary = (Exp_Unary *)data; return ptr_base(env, unary->exp->type); } static OP_CHECK(opck_ptr_cast) { - const Exp_Cast* cast = (Exp_Cast*)data; - if(!cast->td->types || !cast->td->types->td) + const Exp_Cast *cast = (Exp_Cast *)data; + if (!cast->td->types || !cast->td->types->td) ERR_N(exp_self(cast)->pos, "'Ptr' needs types to cast"); DECL_ON(const Type, t, = known_type(env, cast->td)); - if(t->info->cdef && !tflag(t, tflag_check)) + if (t->info->cdef && !tflag(t, tflag_check)) CHECK_BN(ensure_traverse(env, t)); const Type to = known_type(env, cast->td->types->td); exp_setvar(cast->exp, 1); - if(isa(cast->exp->type, to) > 0) - return t; + if (isa(cast->exp->type, to) > 0) return t; ERR_N(exp_self(cast)->pos, "invalid pointer cast"); } static OP_CHECK(opck_ptr_implicit) { - const struct Implicit* imp = (struct Implicit*)data; - const Exp e = imp->e; - const Type base = ptr_base(env, imp->t); - if(isa(e->type, base) > 0) { + const struct Implicit *imp = (struct Implicit *)data; + const Exp e = imp->e; + const Type base = ptr_base(env, imp->t); + if (isa(e->type, base) > 0) { const m_str access = exp_access(e); - if(access) - ERR_N(e->pos, _("can't cast %s value to Ptr"), access); + if (access) ERR_N(e->pos, _("can't cast %s value to Ptr"), access); exp_setvar(e, 1); return imp->t; } @@ -88,12 +84,12 @@ static OP_CHECK(opck_ptr_implicit) { } static INSTR(instr_ptr_deref) { - const m_bit *o = *(m_bit**)REG(-SZ_INT); - if(!o) { + const m_bit *o = *(m_bit **)REG(-SZ_INT); + if (!o) { handle(shred, _("EmptyPointerException")); return; } - if(instr->m_val2) + if (instr->m_val2) memcpy(REG(-SZ_INT), &o, SZ_INT); else { shred->reg -= SZ_INT - instr->m_val; @@ -102,66 +98,68 @@ static INSTR(instr_ptr_deref) { } static INSTR(Cast2Ptr) { - m_bit *o = *(m_bit**)REG(-SZ_INT); - *(m_bit**)REG(-SZ_INT) = o; + m_bit *o = *(m_bit **)REG(-SZ_INT); + *(m_bit **)REG(-SZ_INT) = o; } static OP_EMIT(opem_ptr_cast) { - const Exp_Cast* cast = (Exp_Cast*)data; - const Instr instr = emit_add_instr(emit, Cast2Ptr); - instr->m_val = (m_uint)exp_self(cast)->type; + const Exp_Cast *cast = (Exp_Cast *)data; + const Instr instr = emit_add_instr(emit, Cast2Ptr); + instr->m_val = (m_uint)exp_self(cast)->type; return GW_OK; } static OP_EMIT(opem_ptr_implicit) { - const struct Implicit* imp = (struct Implicit*)data; - const Instr instr = emit_add_instr(emit, Cast2Ptr); - instr->m_val = (m_uint)imp->t; + const struct Implicit *imp = (struct Implicit *)data; + const Instr instr = emit_add_instr(emit, Cast2Ptr); + instr->m_val = (m_uint)imp->t; return GW_OK; } static OP_EMIT(opem_ptr_deref) { - const Exp_Unary* unary = (Exp_Unary*)data; - const Instr instr = emit_add_instr(emit, instr_ptr_deref); - instr->m_val = exp_self(unary)->type->size; - instr->m_val2 = exp_getvar(exp_self(unary)); + const Exp_Unary *unary = (Exp_Unary *)data; + const Instr instr = emit_add_instr(emit, instr_ptr_deref); + instr->m_val = exp_self(unary)->type->size; + instr->m_val2 = exp_getvar(exp_self(unary)); return GW_OK; } -ANN Type scan_class(const Env env, const Type t, const Type_Decl* td); +ANN Type scan_class(const Env env, const Type t, const Type_Decl *td); -static DTOR(ptr_object_dtor) { - release(**(M_Object**)o, shred); -} +static DTOR(ptr_object_dtor) { release(**(M_Object **)o, shred); } static DTOR(ptr_struct_dtor) { - const Type base = *(Type*)(shred->mem + SZ_INT); - const m_uint scope = env_push(shred->info->vm->gwion->env, base->info->value->from->owner_class, base->info->value->from->owner); - const Type t = known_type(shred->info->vm->gwion->env, base->info->cdef->base.tmpl->call->td); + const Type base = *(Type *)(shred->mem + SZ_INT); + const m_uint scope = env_push(shred->info->vm->gwion->env, + base->info->value->from->owner_class, + base->info->value->from->owner); + const Type t = known_type(shred->info->vm->gwion->env, + base->info->cdef->base.tmpl->call->td); env_pop(shred->info->vm->gwion->env, scope); - struct_release(shred, t, *(m_bit**)o); + struct_release(shred, t, *(m_bit **)o); } #include "tmpl_info.h" static OP_CHECK(opck_ptr_scan) { - struct TemplateScan *ts = (struct TemplateScan*)data; - struct tmpl_info info = { .base=ts->t, .td=ts->td, .list=ts->t->info->cdef->base.tmpl->list }; + struct TemplateScan *ts = (struct TemplateScan *)data; + struct tmpl_info info = { + .base = ts->t, .td = ts->td, .list = ts->t->info->cdef->base.tmpl->list}; const Type exists = tmpl_exists(env, &info); - if(exists) - return exists != env->gwion->type[et_error] ? exists : NULL; + if (exists) return exists != env->gwion->type[et_error] ? exists : NULL; const Type base = known_type(env, ts->td->types->td); - const Type t = new_type(env->gwion->mp, s_name(info.name), base); + const Type t = new_type(env->gwion->mp, s_name(info.name), base); t->info->parent = env->gwion->type[et_ptr]; SET_FLAG(t, abstract | ae_flag_final); t->info->tuple = new_tupleform(env->gwion->mp, NULL); - t->nspc = new_nspc(env->gwion->mp, t->name); + t->nspc = new_nspc(env->gwion->mp, t->name); vector_add(&t->info->tuple->types, (m_uint)base); - const m_uint scope = env_push(env, base->info->value->from->owner_class, base->info->value->from->owner); - mk_class(env, t, (loc_t){}); + const m_uint scope = env_push(env, base->info->value->from->owner_class, + base->info->value->from->owner); + mk_class(env, t, (loc_t) {}); env_pop(env, scope); nspc_add_type_front(t->info->value->from->owner, info.name, t); - if(isa(base, env->gwion->type[et_compound]) > 0) { + if (isa(base, env->gwion->type[et_compound]) > 0) { t->nspc->dtor = new_vmcode(env->gwion->mp, NULL, SZ_INT, 1, "@PtrDtor"); - if(!tflag(base, tflag_struct)) + if (!tflag(base, tflag_struct)) t->nspc->dtor->native_func = (m_uint)ptr_object_dtor; else t->nspc->dtor->native_func = (m_uint)ptr_struct_dtor; @@ -171,17 +169,15 @@ static OP_CHECK(opck_ptr_scan) { } static OP_CHECK(opck_ptr_ref) { - const Exp_Binary* bin = (Exp_Binary*)data; + const Exp_Binary *bin = (Exp_Binary *)data; exp_setvar(bin->rhs, 1); return bin->rhs->type; } -static GACK(gack_ptr) { - INTERP_PRINTF("%p", *(m_str*)VALUE); -} +static GACK(gack_ptr) { INTERP_PRINTF("%p", *(m_str *)VALUE); } GWION_IMPORT(ptr) { - const Type t_ptr = gwi_struct_ini(gwi, "Ptr:[A]"); + const Type t_ptr = gwi_struct_ini(gwi, "Ptr:[A]"); gwi->gwion->type[et_ptr] = t_ptr; GWI_BB(gwi_gack(gwi, t_ptr, gack_ptr)) GWI_BB(gwi_item_ini(gwi, "@internal", "@val")) @@ -206,7 +202,7 @@ GWION_IMPORT(ptr) { GWI_BB(gwi_oper_end(gwi, "*", instr_ptr_deref)) GWI_BB(gwi_oper_ini(gwi, "Ptr", "Ptr", "Ptr")) GWI_BB(gwi_oper_add(gwi, opck_ptr_ref)) -// GWI_BB(gwi_oper_emi(gwi, opem_ptr_ref)) + // GWI_BB(gwi_oper_emi(gwi, opem_ptr_ref)) GWI_BB(gwi_oper_end(gwi, "=>", int_r_assign)) return GW_OK; } diff --git a/src/lib/ref.c b/src/lib/ref.c index 305dfbc3..075f4087 100644 --- a/src/lib/ref.c +++ b/src/lib/ref.c @@ -19,53 +19,62 @@ static m_bool ref_access(const Env env, const Exp e) { const m_str access = exp_access(e); - if(!access) - return GW_OK; - env_err(env, e->pos, _("operand is %s"), access ); + if (!access) return GW_OK; + env_err(env, e->pos, _("operand is %s"), access); return GW_ERROR; } static OP_CHECK(opck_implicit_similar) { - const struct Implicit *imp = (struct Implicit*)data; + const struct Implicit *imp = (struct Implicit *)data; CHECK_BN(ref_access(env, imp->e)); exp_setvar(imp->e, 1); return imp->t; } static OP_CHECK(opck_cast_similar) { - const Exp_Cast *cast = (Exp_Cast*)data; + const Exp_Cast *cast = (Exp_Cast *)data; return exp_self(cast)->type; } ANN static void base2ref(Env env, const Type lhs, const Type rhs) { - struct Op_Func opfunc = { .ck=opck_cast_similar }; - struct Op_Import opi = { .op=insert_symbol("$"), .lhs=lhs, .ret=rhs, .rhs=rhs, .func=&opfunc, .data=eNoOp }; + struct Op_Func opfunc = {.ck = opck_cast_similar}; + struct Op_Import opi = {.op = insert_symbol("$"), + .lhs = lhs, + .ret = rhs, + .rhs = rhs, + .func = &opfunc, + .data = eNoOp}; add_op(env->gwion, &opi); opfunc.ck = opck_implicit_similar; - opi.op=insert_symbol("@implicit"); + opi.op = insert_symbol("@implicit"); add_op(env->gwion, &opi); } ANN static void ref2base(Env env, const Type lhs, const Type rhs) { - struct Op_Import opi = { .op=insert_symbol("$"), .lhs=lhs, .ret=rhs, .rhs=rhs, /*.func=&opfunc,*/ .data=eNoOp }; + struct Op_Import opi = {.op = insert_symbol("$"), + .lhs = lhs, + .ret = rhs, + .rhs = rhs, + /*.func=&opfunc,*/ .data = eNoOp}; add_op(env->gwion, &opi); - opi.op=insert_symbol("@implicit"); + opi.op = insert_symbol("@implicit"); add_op(env->gwion, &opi); } static OP_CHECK(opck_ref_scan) { - struct TemplateScan *ts = (struct TemplateScan*)data; - struct tmpl_info info = { .base=ts->t, .td=ts->td, .list=ts->t->info->cdef->base.tmpl->list }; + struct TemplateScan *ts = (struct TemplateScan *)data; + struct tmpl_info info = { + .base = ts->t, .td = ts->td, .list = ts->t->info->cdef->base.tmpl->list}; const Type exists = tmpl_exists(env, &info); - if(exists) - return exists != env->gwion->type[et_error] ? exists : NULL; + if (exists) return exists != env->gwion->type[et_error] ? exists : NULL; const Type base = known_type(env, ts->td->types->td); - const Type t = new_type(env->gwion->mp, s_name(info.name), base); + const Type t = new_type(env->gwion->mp, s_name(info.name), base); SET_FLAG(t, abstract | ae_flag_final); set_tflag(t, tflag_infer); set_tflag(t, tflag_noret); - const m_uint scope = env_push(env, base->info->value->from->owner_class, base->info->value->from->owner); - mk_class(env, t, (loc_t){}); + const m_uint scope = env_push(env, base->info->value->from->owner_class, + base->info->value->from->owner); + mk_class(env, t, (loc_t) {}); base2ref(env, base, t); ref2base(env, t, base); env_pop(env, scope); @@ -81,9 +90,9 @@ GWION_IMPORT(ref) { const Type t_foreach = gwi_struct_ini(gwi, "Ref:[A]"); set_tflag(t_foreach, tflag_infer); - gwinote(gwi, "a pointer to the referenced variable."); - GWI_BB(gwi_item_ini(gwi, "@internal", "val")) - GWI_BB(gwi_item_end(gwi, ae_flag_none, num, 0)) + gwinote(gwi, "a pointer to the referenced variable."); + GWI_BB(gwi_item_ini(gwi, "@internal", "val")) + GWI_BB(gwi_item_end(gwi, ae_flag_none, num, 0)) GWI_BB(gwi_struct_end(gwi)) diff --git a/src/lib/shred.c b/src/lib/shred.c index 875ab6a6..31b875fc 100644 --- a/src/lib/shred.c +++ b/src/lib/shred.c @@ -13,12 +13,13 @@ #include "specialid.h" #include "gwi.h" -static m_int o_fork_thread, o_fork_cond, o_fork_mutex, o_shred_cancel, o_fork_done, o_fork_ev, o_fork_retsize; +static m_int o_fork_thread, o_fork_cond, o_fork_mutex, o_shred_cancel, + o_fork_done, o_fork_ev, o_fork_retsize; -#define FORK_THREAD(o) *(THREAD_TYPE*)(o->data + o_fork_thread) -#define FORK_COND(o) *(THREAD_COND_TYPE*)(o->data + o_fork_cond) -#define FORK_MUTEX(o) *(MUTEX_TYPE*)(o->data + o_fork_mutex) -#define FORK_RETSIZE(o) *(m_int*)(o->data + o_fork_retsize) +#define FORK_THREAD(o) *(THREAD_TYPE *)(o->data + o_fork_thread) +#define FORK_COND(o) *(THREAD_COND_TYPE *)(o->data + o_fork_cond) +#define FORK_MUTEX(o) *(MUTEX_TYPE *)(o->data + o_fork_mutex) +#define FORK_RETSIZE(o) *(m_int *)(o->data + o_fork_retsize) VM_Shred new_shred_base(const VM_Shred shred, const VM_Code code) { const VM_Shred sh = new_vm_shred(shred->info->mp, code); @@ -28,33 +29,33 @@ VM_Shred new_shred_base(const VM_Shred shred, const VM_Code code) { } M_Object new_shred(const VM_Shred shred) { - const M_Object obj = new_object(shred->info->mp, NULL, - shred->info->vm->gwion->type[et_shred]); + const M_Object obj = + new_object(shred->info->mp, NULL, shred->info->vm->gwion->type[et_shred]); ME(obj) = shred; return obj; } ANN static inline M_Object fork_object(const VM_Shred shred, const Type t) { - const Gwion gwion = shred->info->vm->gwion; - const M_Object o = new_object(gwion->mp, shred, t); - *(M_Object*)(o->data + o_fork_ev) = new_object(gwion->mp, NULL, gwion->type[et_event]); - vector_init(&EV_SHREDS(*(M_Object*)(o->data + o_fork_ev))); + const Gwion gwion = shred->info->vm->gwion; + const M_Object o = new_object(gwion->mp, shred, t); + *(M_Object *)(o->data + o_fork_ev) = + new_object(gwion->mp, NULL, gwion->type[et_event]); + vector_init(&EV_SHREDS(*(M_Object *)(o->data + o_fork_ev))); return o; } ANN M_Object new_fork(const VM_Shred shred, const VM_Code code, const Type t) { - VM* parent = shred->info->vm; - const VM_Shred sh = new_shred_base(shred, code); - VM* vm = (sh->info->vm = gwion_cpy(parent)); - vm->parent = parent; + VM * parent = shred->info->vm; + const VM_Shred sh = new_shred_base(shred, code); + VM * vm = (sh->info->vm = gwion_cpy(parent)); + vm->parent = parent; const M_Object o = sh->info->me = fork_object(shred, t); - ME(o) = sh; + ME(o) = sh; ++o->ref; shreduler_add(vm->shreduler, sh); return o; } - static MFUN(gw_shred_exit) { const VM_Shred s = ME(o); s->mem -= SZ_INT; @@ -63,53 +64,51 @@ static MFUN(gw_shred_exit) { static MFUN(vm_shred_id) { const VM_Shred s = ME(o); - *(m_int*)RETURN = s ? (m_int)s->tick->xid : -1; + *(m_int *)RETURN = s ? (m_int)s->tick->xid : -1; } static MFUN(vm_shred_is_running) { - const VM_Shred s = ME(o); - *(m_uint*)RETURN = (s->tick->next || s->tick->prev) ? 1 : 0; + const VM_Shred s = ME(o); + *(m_uint *)RETURN = (s->tick->next || s->tick->prev) ? 1 : 0; } -static MFUN(vm_shred_is_done) { - *(m_uint*)RETURN = ME(o) ? 0 : 1; -} +static MFUN(vm_shred_is_done) { *(m_uint *)RETURN = ME(o) ? 0 : 1; } static MFUN(shred_yield) { - const VM_Shred s = ME(o); + const VM_Shred s = ME(o); const Shreduler sh = s->tick->shreduler; - if(s != shred) - shreduler_remove(sh, s, false); + if (s != shred) shreduler_remove(sh, s, false); shredule(sh, s, GWION_EPSILON); } static SFUN(vm_shred_from_id) { - const m_int index = *(m_int*)MEM(0); - if(index > 0) { - for(m_uint i = 0; i < vector_size(&shred->tick->shreduler->shreds); ++i) { - const VM_Shred s = (VM_Shred)vector_at(&shred->tick->shreduler->shreds, i); - if(s->tick->xid == (m_uint)index) { - *(M_Object*)RETURN = s->info->me; + const m_int index = *(m_int *)MEM(0); + if (index > 0) { + for (m_uint i = 0; i < vector_size(&shred->tick->shreduler->shreds); ++i) { + const VM_Shred s = + (VM_Shred)vector_at(&shred->tick->shreduler->shreds, i); + if (s->tick->xid == (m_uint)index) { + *(M_Object *)RETURN = s->info->me; return; } } } - *(m_uint*)RETURN = 0; + *(m_uint *)RETURN = 0; } static MFUN(shred_args) { - const VM_Shred s = ME(o); - *(m_uint*)RETURN = s->info->args.ptr ? vector_size(&s->info->args) : 0; + const VM_Shred s = ME(o); + *(m_uint *)RETURN = s->info->args.ptr ? vector_size(&s->info->args) : 0; } static MFUN(shred_arg) { - const VM_Shred s = ME(o); - const m_int idx = *(m_int*)MEM(SZ_INT); - if(s->info->args.ptr && 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->info->mp, shred, str) : NULL; + const VM_Shred s = ME(o); + const m_int idx = *(m_int *)MEM(SZ_INT); + if (s->info->args.ptr && 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->info->mp, shred, str) : NULL; } else - *(m_uint*)RETURN = 0; + *(m_uint *)RETURN = 0; } #ifndef BUILD_ON_WINDOWS @@ -118,42 +117,41 @@ static MFUN(shred_arg) { #define PATH_CHR '\\' #endif -#define describe_name(name, src) \ -static MFUN(shred##name##_name) { \ - const VM_Shred s = ME(o); \ - const m_str str = code_name((src), 0); \ - *(m_uint*)RETURN = (m_uint)new_string(shred->info->mp, shred, str); \ -} -describe_name(, s->info->orig->name) -describe_name(_code, s->code->name) - -#define describe_path_and_dir(name, src) \ -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->info->mp, shred, str); \ -} \ -static MFUN(shred##name##_dir) { \ - const VM_Shred s = ME(o); \ - const m_str str = code_name((src), 1); \ - const size_t len = strlen(str); \ - char c[len + 1]; \ - strcpy(c, str); \ - size_t sz = len;\ - while(sz) {\ - if(c[sz] == PATH_CHR) {\ - c[sz] = 0;\ - break;\ - }\ - --sz;\ - }\ - *(m_uint*)RETURN = (m_uint)new_string(shred->info->mp, shred, c); \ -} -describe_path_and_dir(, s->info->orig->name) -describe_path_and_dir(_code, s->code->name) +#define describe_name(name, src) \ + static MFUN(shred##name##_name) { \ + const VM_Shred s = ME(o); \ + const m_str str = code_name((src), 0); \ + *(m_uint *)RETURN = (m_uint)new_string(shred->info->mp, shred, str); \ + } +describe_name(, s->info->orig->name) describe_name(_code, s->code->name) + +#define describe_path_and_dir(name, src) \ + 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->info->mp, shred, str); \ + } \ + static MFUN(shred##name##_dir) { \ + const VM_Shred s = ME(o); \ + const m_str str = code_name((src), 1); \ + const size_t len = strlen(str); \ + char c[len + 1]; \ + strcpy(c, str); \ + size_t sz = len; \ + while (sz) { \ + if (c[sz] == PATH_CHR) { \ + c[sz] = 0; \ + break; \ + } \ + --sz; \ + } \ + *(m_uint *)RETURN = (m_uint)new_string(shred->info->mp, shred, c); \ + } + describe_path_and_dir(, s->info->orig->name) + describe_path_and_dir(_code, s->code->name) -static DTOR(shred_dtor) { - if(ME(o)) { + static DTOR(shred_dtor) { + if (ME(o)) { MUTEX_TYPE mutex = ME(o)->tick->shreduler->mutex; MUTEX_LOCK(mutex); free_vm_shred(ME(o)); @@ -161,41 +159,36 @@ static DTOR(shred_dtor) { } } -static MFUN(shred_lock) { - MUTEX_LOCK(ME(o)->tick->shreduler->mutex); -} +static MFUN(shred_lock) { MUTEX_LOCK(ME(o)->tick->shreduler->mutex); } -static MFUN(shred_unlock) { - MUTEX_UNLOCK(ME(o)->tick->shreduler->mutex); -} +static MFUN(shred_unlock) { MUTEX_UNLOCK(ME(o)->tick->shreduler->mutex); } static void stop(const M_Object o) { VM *vm = ME(o)->info->vm; MUTEX_LOCK(vm->shreduler->mutex); - vm->shreduler->bbq->is_running = 0; - *(m_int*)(o->data + o_shred_cancel) = 1; + vm->shreduler->bbq->is_running = 0; + *(m_int *)(o->data + o_shred_cancel) = 1; MUTEX_UNLOCK(vm->shreduler->mutex); } static inline void join(const M_Object o) { - if(FORK_THREAD(o)) { + if (FORK_THREAD(o)) { THREAD_JOIN(FORK_THREAD(o)); FORK_THREAD(o) = 0; } } static DTOR(fork_dtor) { - *(m_int*)(o->data + o_fork_done) = 1; + *(m_int *)(o->data + o_fork_done) = 1; stop(o); join(o); VM *parent = ME(o)->info->vm->parent; MUTEX_LOCK(parent->shreduler->mutex); - if(parent->gwion->data->child.ptr) { + if (parent->gwion->data->child.ptr) { const m_int idx = vector_find(&parent->gwion->data->child, (vtype)o); - if(idx > -1) - VPTR(&parent->gwion->data->child, idx) = 0; + if (idx > -1) VPTR(&parent->gwion->data->child, idx) = 0; } - if(!parent->gwion->data->child2.ptr) + if (!parent->gwion->data->child2.ptr) vector_init(&parent->gwion->data->child2); vector_add(&parent->gwion->data->child2, (vtype)ME(o)->info->vm->gwion); vmcode_remref(ME(o)->code, ME(o)->info->vm->gwion); @@ -203,25 +196,23 @@ static DTOR(fork_dtor) { } static MFUN(fork_join) { - if(*(m_int*)(o->data + o_fork_done)) - return; + if (*(m_int *)(o->data + o_fork_done)) return; shreduler_remove(shred->tick->shreduler, shred, false); - vector_add(&EV_SHREDS(*(M_Object*)(o->data + o_fork_ev)), (vtype)shred); + vector_add(&EV_SHREDS(*(M_Object *)(o->data + o_fork_ev)), (vtype)shred); } static MFUN(shred_cancel) { MUTEX_LOCK(ME(o)->tick->shreduler->mutex); - *(m_int*)(o->data + o_shred_cancel) = *(m_int*)MEM(SZ_INT); + *(m_int *)(o->data + o_shred_cancel) = *(m_int *)MEM(SZ_INT); MUTEX_UNLOCK(ME(o)->tick->shreduler->mutex); } static MFUN(shred_test_cancel) { - if(*(m_int*)(o->data + o_shred_cancel)) - vm_shred_exit(ME(o)); + if (*(m_int *)(o->data + o_shred_cancel)) vm_shred_exit(ME(o)); } static MFUN(fork_test_cancel) { - if(*(m_int*)(o->data + o_shred_cancel)) { + if (*(m_int *)(o->data + o_shred_cancel)) { stop(o); join(o); _release(o, ME(o)); @@ -231,44 +222,45 @@ static MFUN(fork_test_cancel) { static MFUN(shred_now) { VM *vm = shred->info->vm; - while(vm->parent) - vm = vm->parent; - *(m_float*)RETURN = vm->bbq->pos; + while (vm->parent) vm = vm->parent; + *(m_float *)RETURN = vm->bbq->pos; } struct ThreadLauncher { - MUTEX_TYPE mutex; + MUTEX_TYPE mutex; THREAD_COND_TYPE cond; - VM *vm; + VM * vm; }; static inline int fork_running(VM *vm, const M_Object o) { MUTEX_LOCK(vm->shreduler->mutex); - const int ret = vm->bbq->is_running && !*(m_int*)(o->data + o_shred_cancel); + const int ret = vm->bbq->is_running && !*(m_int *)(o->data + o_shred_cancel); MUTEX_UNLOCK(vm->shreduler->mutex); return ret; } static ANN THREAD_FUNC(fork_run) { -struct ThreadLauncher *tl = data; - VM *vm = tl->vm; - MUTEX_TYPE mutex = tl->mutex; - const M_Object me = vm->shreduler->list->self->info->me; + struct ThreadLauncher *tl = data; + VM * vm = tl->vm; + MUTEX_TYPE mutex = tl->mutex; + const M_Object me = vm->shreduler->list->self->info->me; ++me->ref; MUTEX_COND_LOCK(mutex); THREAD_COND_SIGNAL(FORK_COND(me)); MUTEX_COND_UNLOCK(mutex); - while(fork_running(vm, me)) { + while (fork_running(vm, me)) { vm_run(vm); ++vm->bbq->pos; } gwion_end_child(ME(me), vm->gwion); MUTEX_LOCK(vm->parent->shreduler->mutex); - if(!*(m_int*)(me->data + o_shred_cancel) && me->type_ref != vm->gwion->type[et_fork]) - memcpy(me->data + vm->gwion->type[et_fork]->nspc->info->offset, ME(me)->reg, FORK_RETSIZE(me)); - *(m_int*)(me->data + o_fork_done) = 1; - if(!*(m_int*)(me->data + o_shred_cancel)) - broadcast(*(M_Object*)(me->data + o_fork_ev)); + if (!*(m_int *)(me->data + o_shred_cancel) && + me->type_ref != vm->gwion->type[et_fork]) + memcpy(me->data + vm->gwion->type[et_fork]->nspc->info->offset, ME(me)->reg, + FORK_RETSIZE(me)); + *(m_int *)(me->data + o_fork_done) = 1; + if (!*(m_int *)(me->data + o_shred_cancel)) + broadcast(*(M_Object *)(me->data + o_fork_ev)); MUTEX_UNLOCK(vm->parent->shreduler->mutex); THREAD_RETURN(0); } @@ -277,7 +269,8 @@ ANN void fork_launch(const M_Object o, const m_uint sz) { FORK_RETSIZE(o) = sz; MUTEX_SETUP(FORK_MUTEX(o)); THREAD_COND_SETUP(FORK_COND(o)); - struct ThreadLauncher tl = { .mutex=FORK_MUTEX(o), .cond=FORK_COND(o), .vm=ME(o)->info->vm }; + struct ThreadLauncher tl = { + .mutex = FORK_MUTEX(o), .cond = FORK_COND(o), .vm = ME(o)->info->vm}; MUTEX_COND_LOCK(tl.mutex); THREAD_CREATE(FORK_THREAD(o), fork_run, &tl); THREAD_COND_WAIT(FORK_COND(o), tl.mutex); @@ -287,30 +280,27 @@ ANN void fork_launch(const M_Object o, const m_uint sz) { } ANN void fork_clean(const VM_Shred shred, const Vector v) { - for(m_uint i = 0; i < vector_size(v); ++i) { + for (m_uint i = 0; i < vector_size(v); ++i) { const M_Object o = (M_Object)vector_at(v, i); - if(!o) - continue; + if (!o) continue; stop(o); } - for(m_uint i = 0; i < vector_size(v); ++i) { + for (m_uint i = 0; i < vector_size(v); ++i) { const M_Object o = (M_Object)vector_at(v, i); - if(!o) - continue; + if (!o) continue; join(o); } - for(m_uint i = 0; i < vector_size(v); ++i) { + for (m_uint i = 0; i < vector_size(v); ++i) { const M_Object o = (M_Object)vector_at(v, i); - if(!o) - continue; + if (!o) continue; _release(o, shred); - } + } vector_release(v); v->ptr = NULL; } GWION_IMPORT(shred) { - const Type t_shred = gwi_class_ini(gwi, "Shred", NULL); + const Type t_shred = gwi_class_ini(gwi, "Shred", NULL); gwi_class_xtor(gwi, NULL, shred_dtor); gwi_item_ini(gwi, "@internal", "@me"); @@ -378,10 +368,10 @@ GWION_IMPORT(shred) { SET_FLAG(t_shred, abstract | ae_flag_final); gwi->gwion->type[et_shred] = t_shred; - struct SpecialId_ spid = { .type=t_shred, .exec=RegPushMe, .is_const=1 }; + struct SpecialId_ spid = {.type = t_shred, .exec = RegPushMe, .is_const = 1}; gwi_specialid(gwi, "me", &spid); - const Type t_fork= gwi_class_ini(gwi, "Fork", "Shred"); + const Type t_fork = gwi_class_ini(gwi, "Fork", "Shred"); gwi_class_xtor(gwi, NULL, fork_dtor); gwi->gwion->type[et_fork] = t_fork; @@ -404,7 +394,7 @@ GWION_IMPORT(shred) { GWI_BB(gwi_class_end(gwi)) SET_FLAG(t_fork, abstract | ae_flag_final); - const Type t_typed = gwi_class_ini(gwi, "TypedFork:[A]", "Fork"); + const Type t_typed = gwi_class_ini(gwi, "TypedFork:[A]", "Fork"); gwi_item_ini(gwi, "A", "retval"); GWI_BB((gwi_item_end(gwi, ae_flag_const, num, 0))) GWI_BB(gwi_class_end(gwi)) diff --git a/src/lib/string.c b/src/lib/string.c index 8acf522b..b094c2b4 100644 --- a/src/lib/string.c +++ b/src/lib/string.c @@ -15,57 +15,56 @@ #include "gwi.h" #include "gack.h" -#define describe_string_logical(name, action) \ -static INSTR(String_##name) { \ - POP_REG(shred, SZ_INT); \ - const M_Object lhs = *(M_Object*)REG(-SZ_INT); \ - const M_Object rhs = *(M_Object*)REG(0); \ - *(m_int*)REG(-SZ_INT) = action; \ -} +#define describe_string_logical(name, action) \ + static INSTR(String_##name) { \ + POP_REG(shred, SZ_INT); \ + const M_Object lhs = *(M_Object *)REG(-SZ_INT); \ + const M_Object rhs = *(M_Object *)REG(0); \ + *(m_int *)REG(-SZ_INT) = action; \ + } describe_string_logical(eq, (!strcmp(STRING(lhs), STRING(rhs)))) -describe_string_logical(neq, (strcmp(STRING(lhs), STRING(rhs)))) + describe_string_logical(neq, (strcmp(STRING(lhs), STRING(rhs)))) -static inline uint is_const_str(const Exp exp) { + static inline uint is_const_str(const Exp exp) { return exp->exp_type == ae_exp_primary && exp->d.prim.prim_type == ae_prim_str; } -#define opck_str(name, __exp__) \ -OP_CHECK(opck_string_##name) { \ - Exp_Binary *bin = (Exp_Binary*)data; \ - if(!is_const_str(bin->lhs) || !is_const_str(bin->rhs)) \ - return env->gwion->type[et_bool]; \ - const int ret = __exp__; \ - free_exp(env->gwion->mp, bin->lhs); \ - free_exp(env->gwion->mp, bin->rhs); \ - const Exp e = exp_self(bin); \ - e->exp_type = ae_exp_primary; \ - e->d.prim.prim_type = ae_prim_num; \ - e->d.prim.d.num = ret; \ - return env->gwion->type[et_bool]; \ -} +#define opck_str(name, __exp__) \ + OP_CHECK(opck_string_##name) { \ + Exp_Binary *bin = (Exp_Binary *)data; \ + if (!is_const_str(bin->lhs) || !is_const_str(bin->rhs)) \ + return env->gwion->type[et_bool]; \ + const int ret = __exp__; \ + free_exp(env->gwion->mp, bin->lhs); \ + free_exp(env->gwion->mp, bin->rhs); \ + const Exp e = exp_self(bin); \ + e->exp_type = ae_exp_primary; \ + e->d.prim.prim_type = ae_prim_num; \ + e->d.prim.d.num = ret; \ + return env->gwion->type[et_bool]; \ + } opck_str(eq, !strcmp(bin->lhs->d.prim.d.str, bin->rhs->d.prim.d.str)) -opck_str(neq, strcmp(bin->lhs->d.prim.d.str, bin->rhs->d.prim.d.str)) + opck_str(neq, strcmp(bin->lhs->d.prim.d.str, bin->rhs->d.prim.d.str)) -static CTOR(string_ctor) { + static CTOR(string_ctor) { STRING(o) = _mp_calloc(shred->info->mp, 1); } -static DTOR(string_dtor) { - free_mstr(shred->info->mp, STRING(o)); -} +static DTOR(string_dtor) { free_mstr(shred->info->mp, STRING(o)); } ID_CHECK(check_funcpp) { - ((Exp_Primary*)prim)->prim_type = ae_prim_str; - ((Exp_Primary*)prim)->d.str = env->func ? env->func->name : env->class_def ? - env->class_def->name : env->name; - ((Exp_Primary*)prim)->value = global_string(env, prim->d.str); + ((Exp_Primary *)prim)->prim_type = ae_prim_str; + ((Exp_Primary *)prim)->d.str = env->func ? env->func->name + : env->class_def ? env->class_def->name + : env->name; + ((Exp_Primary *)prim)->value = global_string(env, prim->d.str); return prim->value->type; } static GACK(gack_string) { - const M_Object obj = *(M_Object*)VALUE; + const M_Object obj = *(M_Object *)VALUE; INTERP_PRINTF("%s", obj ? STRING(obj) : "(null string)"); } @@ -75,319 +74,302 @@ static inline m_bool bounds(const m_str str, const m_int i) { } static INSTR(StringSlice) { - shred->reg -= SZ_INT *2; - const M_Object obj = *(M_Object*)REG(-SZ_INT); - m_str str = STRING(obj); - const m_int start = *(m_uint*)REG(0); - const size_t strsz = strlen(str); - m_int end = *(m_uint*)REG(SZ_INT); - if(end < 0) - end = strsz + end; - if(bounds(str, start) < 0 || bounds(str, end) < 0) { + shred->reg -= SZ_INT * 2; + const M_Object obj = *(M_Object *)REG(-SZ_INT); + m_str str = STRING(obj); + const m_int start = *(m_uint *)REG(0); + const size_t strsz = strlen(str); + m_int end = *(m_uint *)REG(SZ_INT); + if (end < 0) end = strsz + end; + if (bounds(str, start) < 0 || bounds(str, end) < 0) { handle(shred, "OutOfBoundsStringSlice"); return; } - const m_int op = start < end ? 1 : -1; - const m_uint sz = op > 0 ? end - start : start - end; - char c[sz + 1]; - for(m_int i = start, j = 0; i != end; i += op, ++j) - c[j] = str[i]; - c[sz] = '\0'; - *(M_Object*)REG(-SZ_INT) = new_string(shred->info->mp, shred, c); + const m_int op = start < end ? 1 : -1; + const m_uint sz = op > 0 ? end - start : start - end; + char c[sz + 1]; + for (m_int i = start, j = 0; i != end; i += op, ++j) c[j] = str[i]; + c[sz] = '\0'; + *(M_Object *)REG(-SZ_INT) = new_string(shred->info->mp, shred, c); } -static MFUN(string_len) { - *(m_uint*)RETURN = strlen(STRING(o)); -} +static MFUN(string_len) { *(m_uint *)RETURN = strlen(STRING(o)); } static MFUN(string_upper) { char c[strlen(STRING(o)) + 1]; strcpy(c, STRING(o)); - for(m_uint i = 0; i < strlen(c); i++) - if(c[i] >= 'a' && c[i] <= 'z') - c[i] += 'A' - 'a'; - *(M_Object*)RETURN = new_string(shred->info->mp, shred, c); + for (m_uint i = 0; i < strlen(c); i++) + if (c[i] >= 'a' && c[i] <= 'z') c[i] += 'A' - 'a'; + *(M_Object *)RETURN = new_string(shred->info->mp, shred, c); } static MFUN(string_lower) { char c[strlen(STRING(o)) + 1]; strcpy(c, STRING(o)); - for(m_uint i = 0; i < strlen(c); i++) - if(c[i] >= 'A' && c[i] <= 'Z') - c[i] -= 'A' - 'a'; - *(M_Object*)RETURN = new_string(shred->info->mp, shred, c); + for (m_uint i = 0; i < strlen(c); i++) + if (c[i] >= 'A' && c[i] <= 'Z') c[i] -= 'A' - 'a'; + *(M_Object *)RETURN = new_string(shred->info->mp, shred, c); } static MFUN(string_ltrim) { - m_uint i = 0; + m_uint i = 0; const m_str str = STRING(o); - while(str[i] == ' ') - i++; + while (str[i] == ' ') i++; char c[strlen(str) - i + 1]; strcpy(c, STRING(o) + i); - *(M_Object*)RETURN = new_string(shred->info->mp, shred, c); + *(M_Object *)RETURN = new_string(shred->info->mp, shred, c); } static MFUN(string_rtrim) { - const m_str str = STRING(o); - const size_t sz = strlen(str); - if(sz) { + const m_str str = STRING(o); + const size_t sz = strlen(str); + if (sz) { m_uint len = strlen(str) - 1; - while(str[len] == ' ') - len--; + while (str[len] == ' ') len--; char c[len + 2]; strncpy(c, str, len + 1); - c[len + 1] = '\0'; - *(M_Object*)RETURN = new_string(shred->info->mp, shred, c); + c[len + 1] = '\0'; + *(M_Object *)RETURN = new_string(shred->info->mp, shred, c); } else { ++o->ref; - *(M_Object*)RETURN = o; + *(M_Object *)RETURN = o; } } static MFUN(string_trim) { const m_str str = STRING(o); - m_int i, start = 0, end = 0, len = 0; - while(str[len] != '\0') - len++; - for(i = 0; i < len; i++) { - if(str[i] == ' ') + m_int i, start = 0, end = 0, len = 0; + while (str[len] != '\0') len++; + for (i = 0; i < len; i++) { + if (str[i] == ' ') start++; - else break; + else + break; } - for(i = len - 1; i >= 0; i--) { - if(str[i] == ' ') + for (i = len - 1; i >= 0; i--) { + if (str[i] == ' ') end++; - else break; + else + break; } - if(len - start - end <= 0) { - *(m_uint*)RETURN = 0; + if (len - start - end <= 0) { + *(m_uint *)RETURN = 0; return; } char c[len - start - end + 1]; - for(i = start; i < len - end; i++) - c[i - start] = str[i]; - c[len - start - end ] = '\0'; - *(M_Object*)RETURN = new_string(shred->info->mp, shred, c); + for (i = start; i < len - end; i++) c[i - start] = str[i]; + c[len - start - end] = '\0'; + *(M_Object *)RETURN = new_string(shred->info->mp, shred, c); } static MFUN(string_charAt) { - const m_str str = STRING(o); - const m_int i = *(m_int*)MEM(SZ_INT); + const m_str str = STRING(o); + const m_int i = *(m_int *)MEM(SZ_INT); const m_uint len = strlen(str); - if(i < 0 || (m_uint)i >= len) - *(m_uint*)RETURN = -1; + if (i < 0 || (m_uint)i >= len) + *(m_uint *)RETURN = -1; else - *(m_uint*)RETURN = str[i]; + *(m_uint *)RETURN = str[i]; } static MFUN(string_setCharAt) { - const m_str str = STRING(o); - const m_int i = *(m_int*)MEM(SZ_INT); - const m_int c = *(m_int*)MEM(SZ_INT * 2); + const m_str str = STRING(o); + const m_int i = *(m_int *)MEM(SZ_INT); + const m_int c = *(m_int *)MEM(SZ_INT * 2); const m_uint len = strlen(str); - if(i < 0 || (m_uint)i >= len) - *(m_uint*)RETURN = -1; + if (i < 0 || (m_uint)i >= len) + *(m_uint *)RETURN = -1; else { - str[i] = c; - STRING(o) = s_name(insert_symbol(shred->info->vm->gwion->st, str)); - *(m_uint*)RETURN = c; + str[i] = c; + STRING(o) = s_name(insert_symbol(shred->info->vm->gwion->st, str)); + *(m_uint *)RETURN = c; } } static MFUN(string_insert) { char str[strlen(STRING(o)) + 1]; strcpy(str, STRING(o)); - m_int i, len_insert = 0, index = *(m_int*)MEM(SZ_INT); - const M_Object arg = *(M_Object*)MEM(SZ_INT * 2); + m_int i, len_insert = 0, index = *(m_int *)MEM(SZ_INT); + const M_Object arg = *(M_Object *)MEM(SZ_INT * 2); - if(!arg) { - *(M_Object*)RETURN = NULL; + if (!arg) { + *(M_Object *)RETURN = NULL; return; } char insert[strlen(STRING(arg)) + 1]; strcpy(insert, STRING(arg)); const m_uint len = strlen(str); - len_insert = strlen(insert); + len_insert = strlen(insert); char c[len + len_insert + 1]; - for(i = 0; i < index; i++) - c[i] = str[i]; - for(i = 0; i < len_insert; i++) - c[i + index] = insert[i]; - for(i = index; i < (m_int)len; i++) - c[i + len_insert] = str[i]; + for (i = 0; i < index; i++) c[i] = str[i]; + for (i = 0; i < len_insert; i++) c[i + index] = insert[i]; + for (i = index; i < (m_int)len; i++) c[i + len_insert] = str[i]; c[len + len_insert] = '\0'; - *(M_Object*)RETURN = new_string(shred->info->mp, shred, c);; + *(M_Object *)RETURN = new_string(shred->info->mp, shred, c); + ; } static MFUN(string_replace) { char str[strlen(STRING(o)) + 1]; strcpy(str, STRING(o)); - m_int i, len_insert = 0, index = *(m_int*)MEM(SZ_INT); - const M_Object arg = *(M_Object*)MEM(SZ_INT * 2); - if(!arg) { - *(M_Object*)RETURN = o; + m_int i, len_insert = 0, index = *(m_int *)MEM(SZ_INT); + const M_Object arg = *(M_Object *)MEM(SZ_INT * 2); + if (!arg) { + *(M_Object *)RETURN = o; return; } char insert[strlen(STRING(arg)) + 1]; strcpy(insert, STRING(arg)); const m_uint len = strlen(str); - len_insert = strlen(insert); - if(index >= (m_int)len || index < 0 || (index + len_insert + 1) <= 0) { - *(M_Object*)RETURN = NULL; + len_insert = strlen(insert); + if (index >= (m_int)len || index < 0 || (index + len_insert + 1) <= 0) { + *(M_Object *)RETURN = NULL; return; } char c[index + len_insert + 1]; - for(i = 0; i < index; i++) - c[i] = str[i]; - for(i = 0; i < len_insert; i++) - c[i + index] = insert[i]; + for (i = 0; i < index; i++) c[i] = str[i]; + for (i = 0; i < len_insert; i++) c[i + index] = insert[i]; c[index + len_insert] = '\0'; - *(M_Object*)RETURN = new_string(shred->info->mp, shred, c);; + *(M_Object *)RETURN = new_string(shred->info->mp, shred, c); + ; } static MFUN(string_replaceN) { char str[strlen(STRING(o)) + 1]; strcpy(str, STRING(o)); - m_int i, index = *(m_int*)MEM(SZ_INT); - const M_Object arg = *(M_Object*)MEM(SZ_INT * 3); - const m_int _len = *(m_int*)MEM(SZ_INT * 2); - if(!arg || index > (m_int)strlen(STRING(o)) || _len > (m_int)strlen(STRING(arg))) { - *(M_Object*)RETURN = NULL; + m_int i, index = *(m_int *)MEM(SZ_INT); + const M_Object arg = *(M_Object *)MEM(SZ_INT * 3); + const m_int _len = *(m_int *)MEM(SZ_INT * 2); + if (!arg || index > (m_int)strlen(STRING(o)) || + _len > (m_int)strlen(STRING(arg))) { + *(M_Object *)RETURN = NULL; return; } - char insert[strlen(STRING(arg)) + 1]; + char insert[strlen(STRING(arg)) + 1]; const m_uint len = strlen(str); memset(insert, 0, len + 1); strcpy(insert, STRING(arg)); str[len] = '\0'; - if(index > (m_int)len) - index = len - 1; + if (index > (m_int)len) index = len - 1; char c[len + _len]; memset(c, 0, len + _len); - for(i = 0; i < index; i++) - c[i] = str[i]; - for(i = 0; i < _len; i++) - c[i + index] = insert[i]; - for(i = index + _len; i < (m_int)len; i++) - c[i] = str[i]; - c[len + _len - 1] = '\0'; - *(M_Object*)RETURN = new_string(shred->info->mp, shred, c);; + for (i = 0; i < index; i++) c[i] = str[i]; + for (i = 0; i < _len; i++) c[i + index] = insert[i]; + for (i = index + _len; i < (m_int)len; i++) c[i] = str[i]; + c[len + _len - 1] = '\0'; + *(M_Object *)RETURN = new_string(shred->info->mp, shred, c); + ; } static MFUN(string_find) { - const m_str base = STRING(o); - char c = *(m_int*)MEM(SZ_INT); - char *str = strchr(base, c); - *(m_uint*)RETURN = str ? str - base : -1; + const m_str base = STRING(o); + char c = *(m_int *)MEM(SZ_INT); + char * str = strchr(base, c); + *(m_uint *)RETURN = str ? str - base : -1; } static MFUN(string_findStart) { - const m_str base = STRING(o); - const size_t sz = strlen(base); - const char pos = *(m_int*)MEM(SZ_INT *2); - if(pos >= 0 && (size_t)pos < sz) { - const char arg = *(m_int*)MEM(SZ_INT); - char *str = strchr(base + pos, arg); - *(m_uint*)RETURN = str ? str - pos - base : -1; + const m_str base = STRING(o); + const size_t sz = strlen(base); + const char pos = *(m_int *)MEM(SZ_INT * 2); + if (pos >= 0 && (size_t)pos < sz) { + const char arg = *(m_int *)MEM(SZ_INT); + char * str = strchr(base + pos, arg); + *(m_uint *)RETURN = str ? str - pos - base : -1; } else - *(m_uint*)RETURN = -1; + *(m_uint *)RETURN = -1; } static MFUN(string_findStr) { - const m_str base = STRING(o); - const size_t sz = strlen(base); - const M_Object obj = *(M_Object*)MEM(SZ_INT); - if(sz) { - const m_str arg = STRING(obj); - const m_str str = strstr(base, arg); - *(m_uint*)RETURN = str ? str - base : -1; + const m_str base = STRING(o); + const size_t sz = strlen(base); + const M_Object obj = *(M_Object *)MEM(SZ_INT); + if (sz) { + const m_str arg = STRING(obj); + const m_str str = strstr(base, arg); + *(m_uint *)RETURN = str ? str - base : -1; } else - *(m_uint*)RETURN = -1; + *(m_uint *)RETURN = -1; } static MFUN(string_findStrStart) { - const m_str base = STRING(o); - const size_t sz = strlen(base); - const char pos = *(m_int*)MEM(SZ_INT*2); - const M_Object obj = *(M_Object*)MEM(SZ_INT); - if(pos >= 0 && (size_t)pos < sz) { - const m_str arg = STRING(obj); - const m_str str = strstr(base + pos, arg); - *(m_uint*)RETURN = str ? str - pos- base : -1; + const m_str base = STRING(o); + const size_t sz = strlen(base); + const char pos = *(m_int *)MEM(SZ_INT * 2); + const M_Object obj = *(M_Object *)MEM(SZ_INT); + if (pos >= 0 && (size_t)pos < sz) { + const m_str arg = STRING(obj); + const m_str str = strstr(base + pos, arg); + *(m_uint *)RETURN = str ? str - pos - base : -1; } else - *(m_uint*)RETURN = -1; + *(m_uint *)RETURN = -1; } static MFUN(string_rfind) { - const m_str base = STRING(o); - char c = *(m_int*)MEM(SZ_INT); - char *str = strrchr(base, c); - *(m_uint*)RETURN = str ? str - base : -1; + const m_str base = STRING(o); + char c = *(m_int *)MEM(SZ_INT); + char * str = strrchr(base, c); + *(m_uint *)RETURN = str ? str - base : -1; } static MFUN(string_rfindStart) { - const m_str base = STRING(o); - const size_t sz = strlen(base); - const char pos = *(m_int*)MEM(SZ_INT); - if(pos >= 0 && (size_t)pos < sz) { - const char arg = *(m_int*)MEM(SZ_INT * 2); - char *str = strrchr(base + pos, arg); - *(m_uint*)RETURN = str ? str - pos - base : -1; + const m_str base = STRING(o); + const size_t sz = strlen(base); + const char pos = *(m_int *)MEM(SZ_INT); + if (pos >= 0 && (size_t)pos < sz) { + const char arg = *(m_int *)MEM(SZ_INT * 2); + char * str = strrchr(base + pos, arg); + *(m_uint *)RETURN = str ? str - pos - base : -1; } else - *(m_uint*)RETURN = -1; + *(m_uint *)RETURN = -1; } static MFUN(string_rfindStr) { - const m_str base = STRING(o); - const size_t sz = strlen(base); - const M_Object obj = *(M_Object*)MEM(SZ_INT); - if(sz) { + const m_str base = STRING(o); + const size_t sz = strlen(base); + const M_Object obj = *(M_Object *)MEM(SZ_INT); + if (sz) { const m_str arg = STRING(obj); - m_str tmp = base, str = NULL; - while((tmp = strstr(tmp, arg))) - str = tmp++; - *(m_uint*)RETURN = str ? str - base : -1; + m_str tmp = base, str = NULL; + while ((tmp = strstr(tmp, arg))) str = tmp++; + *(m_uint *)RETURN = str ? str - base : -1; } else - *(m_uint*)RETURN = -1; + *(m_uint *)RETURN = -1; } static MFUN(string_rfindStrStart) { - const m_str base = STRING(o); - const size_t sz = strlen(base); - const char pos = *(m_int*)MEM(SZ_INT *2); - const M_Object obj = *(M_Object*)MEM(SZ_INT * 2); - if(sz) { + const m_str base = STRING(o); + const size_t sz = strlen(base); + const char pos = *(m_int *)MEM(SZ_INT * 2); + const M_Object obj = *(M_Object *)MEM(SZ_INT * 2); + if (sz) { const m_str arg = STRING(obj); - m_str tmp = base + pos, str = NULL; - while((tmp = strstr(tmp, arg))) - str = tmp++; - *(m_uint*)RETURN = str ? str - pos - base : -1; + m_str tmp = base + pos, str = NULL; + while ((tmp = strstr(tmp, arg))) str = tmp++; + *(m_uint *)RETURN = str ? str - pos - base : -1; } else - *(m_uint*)RETURN = -1; + *(m_uint *)RETURN = -1; } static MFUN(string_erase) { - const m_str str = STRING(o); - const m_int start = *(m_int*)MEM(SZ_INT); - const m_int rem = *(m_int*)MEM(SZ_INT * 2); - const m_int len = strlen(str); - const m_int size = len - rem + 1; - if(start >= len || size <= 0) { - *(M_Object*)RETURN = NULL; + const m_str str = STRING(o); + const m_int start = *(m_int *)MEM(SZ_INT); + const m_int rem = *(m_int *)MEM(SZ_INT * 2); + const m_int len = strlen(str); + const m_int size = len - rem + 1; + if (start >= len || size <= 0) { + *(M_Object *)RETURN = NULL; return; } char c[size]; c[size - 1] = '\0'; - for(m_int i = 0; i < start; i++) - c[i] = str[i]; - for(m_int i = start + rem; i < len; i++) - c[i - rem] = str[i]; + for (m_int i = 0; i < start; i++) c[i] = str[i]; + for (m_int i = start + rem; i < len; i++) c[i - rem] = str[i]; STRING(o) = s_name(insert_symbol(shred->info->vm->gwion->st, c)); } GWION_IMPORT(string) { - const Type t_string = gwi_class_ini(gwi, "string", NULL); + const Type t_string = gwi_class_ini(gwi, "string", NULL); gwi->gwion->type[et_string] = t_string; // use func gwi_class_xtor(gwi, string_ctor, string_dtor); GWI_BB(gwi_gack(gwi, t_string, gack_string)) @@ -481,16 +463,17 @@ GWION_IMPORT(string) { GWI_BB(gwi_class_end(gwi)) - GWI_BB(gwi_oper_ini(gwi, "string", "string", "bool")) + GWI_BB(gwi_oper_ini(gwi, "string", "string", "bool")) GWI_BB(gwi_oper_add(gwi, opck_string_eq)) - GWI_BB(gwi_oper_end(gwi, "==", String_eq)) + GWI_BB(gwi_oper_end(gwi, "==", String_eq)) GWI_BB(gwi_oper_add(gwi, opck_string_neq)) - GWI_BB(gwi_oper_end(gwi, "!=", String_neq)) + GWI_BB(gwi_oper_end(gwi, "!=", String_neq)) GWI_BB(gwi_oper_ini(gwi, "int", "string", "string")) GWI_BB(gwi_oper_end(gwi, "@slice", StringSlice)) - struct SpecialId_ spid = { .ck=check_funcpp, .exec=RegPushMe, .is_const=1 }; + struct SpecialId_ spid = { + .ck = check_funcpp, .exec = RegPushMe, .is_const = 1}; gwi_specialid(gwi, "__func__", &spid); return GW_OK; } diff --git a/src/lib/tmpl_info.c b/src/lib/tmpl_info.c index 8414edcc..a1d17d14 100644 --- a/src/lib/tmpl_info.c +++ b/src/lib/tmpl_info.c @@ -12,21 +12,21 @@ #include "import.h" #include "tmpl_info.h" -ANN static inline m_str tmpl_get(struct tmpl_info* info, m_str str) { +ANN static inline m_str tmpl_get(struct tmpl_info *info, m_str str) { const m_str tmp = (m_str)vector_at(&info->type, info->index); strcpy(str, tmp); - return str += vector_at(&info->size, info->index); + return str + vector_at(&info->size, info->index); } -ANN static void template_name(struct tmpl_info* info, m_str s) { - m_str str = s; - const m_uint size = info->index = vector_size(&info->type) -1; - str = tmpl_get(info, str); - *str++ = ':'; - *str++ = '['; - for(info->index = 0; info->index < size; ++info->index) { +ANN static void template_name(struct tmpl_info *info, m_str s) { + m_str str = s; + const m_uint size = info->index = vector_size(&info->type) - 1; + str = tmpl_get(info, str); + *str++ = ':'; + *str++ = '['; + for (info->index = 0; info->index < size; ++info->index) { str = tmpl_get(info, str); - if(info->index < size - 1) + if (info->index < size - 1) *str++ = ','; else *str++ = ']'; @@ -34,53 +34,58 @@ ANN static void template_name(struct tmpl_info* info, m_str s) { *str = '\0'; } -ANN static inline size_t tmpl_set(struct tmpl_info* info, const m_str str) { +ANN static inline size_t tmpl_set(struct tmpl_info *info, const m_str str) { vector_add(&info->type, (vtype)str); const size_t len = strlen(str); vector_add(&info->size, len); return len; } -ANN static ssize_t template_size(const Env env, struct tmpl_info* info) { - DECL_OB(const m_str, str, = tl2str(env->gwion, info->td->types, info->td->pos)); +ANN static ssize_t template_size(const Env env, struct tmpl_info *info) { + DECL_OB(const m_str, str, + = tl2str(env->gwion, info->td->types, info->td->pos)); const size_t tmpl_sz = tmpl_set(info, str); - const m_str base = type2str(env->gwion, info->base, info->td->pos); + const m_str base = type2str(env->gwion, info->base, info->td->pos); return tmpl_sz + tmpl_set(info, base) + 4; } -ANEW ANN static Symbol _template_id(const Env env, struct tmpl_info *const info, const size_t sz) { +ANEW ANN static Symbol _template_id(const Env env, struct tmpl_info *const info, + const size_t sz) { char name[sz]; template_name(info, name); return insert_symbol(name); } -ANEW ANN static Symbol template_id(const Env env, struct tmpl_info *const info) { +ANEW ANN static Symbol template_id(const Env env, + struct tmpl_info *const info) { vector_init(&info->type); vector_init(&info->size); - ssize_t sz = template_size(env, info); + ssize_t sz = template_size(env, info); const Symbol sym = sz > GW_ERROR ? _template_id(env, info, sz) : NULL; - for(m_uint i = 0; i < vector_size(&info->type); ++i) - mp_free2(env->gwion->mp, vector_at(&info->size, i), (m_str)vector_at(&info->type, i)); + for (m_uint i = 0; i < vector_size(&info->type); ++i) + mp_free2(env->gwion->mp, vector_at(&info->size, i), + (m_str)vector_at(&info->type, i)); vector_release(&info->type); vector_release(&info->size); return sym; } ANN static m_bool template_match(Specialized_List base, Type_List call) { - while((call = call->next) && (base = base->next)); + while ((call = call->next) && (base = base->next)) + ; return !call ? GW_OK : GW_ERROR; } ANN static Type _tmpl_exists(const Env env, const Symbol name) { - if(env->class_def && name == insert_symbol(env->class_def->name)) - return env->class_def; + if (env->class_def && name == insert_symbol(env->class_def->name)) + return env->class_def; return nspc_lookup_type1(env->curr, name); } ANN Type tmpl_exists(const Env env, struct tmpl_info *const info) { - if(template_match(info->list, info->td->types) < 0) // invalid template + if (template_match(info->list, info->td->types) < 0) // invalid template ERR_N(info->td->pos, _("invalid template types number")); - if(!info->name) { + if (!info->name) { DECL_ON(const Symbol, name, = info->name = template_id(env, info)); return _tmpl_exists(env, name); } diff --git a/src/lib/ugen.c b/src/lib/ugen.c index f6ccf921..1dff2c77 100644 --- a/src/lib/ugen.c +++ b/src/lib/ugen.c @@ -14,123 +14,125 @@ #include "array.h" static TICK(dac_tick) { - m_float* out = ((VM*)u->module.gen.data)->bbq->out; - uint i = 0; + m_float *out = ((VM *)u->module.gen.data)->bbq->out; + uint i = 0; do out[i] = UGEN(u->connect.multi->channel[i])->in; - while(++i < u->connect.multi->n_out); + while (++i < u->connect.multi->n_out); } static TICK(adc_tick) { - const m_float* in = ((VM*)u->module.gen.data)->bbq->in; - uint i = 0; + const m_float *in = ((VM *)u->module.gen.data)->bbq->in; + uint i = 0; do UGEN(u->connect.multi->channel[i])->out = in[i]; - while(++i < u->connect.multi->n_out); + while (++i < u->connect.multi->n_out); } -#define COMPUTE(a) if(!(a)->done)(a)->compute((a)); +#define COMPUTE(a) \ + if (!(a)->done) (a)->compute((a)); -__attribute__((hot)) -ANN void compute_mono(const UGen u) { - u->done = 1; +__attribute__((hot)) ANN void compute_mono(const UGen u) { + u->done = 1; const uint size = u->connect.net->size; - if(size) { + if (size) { const Vector vec = &u->connect.net->from; const UGen v = (UGen)vector_front(vec); COMPUTE(v) u->in = v->out; - for(uint i = 1; i < size; ++i) { + for (uint i = 1; i < size; ++i) { const UGen w = (UGen)vector_at(vec, i); COMPUTE(w) - static const void* dispatch[] = { &&add, &&sub, &&mul, &&div }; + static const void *dispatch[] = {&&add, &&sub, &&mul, &&div}; do { goto *dispatch[u->op]; - add: - u->in += w->out; - break; - sub: - u->in -= w->out; - break; - mul: - u->in *= w->out; - break; - div: - u->in *= w->out; - break; - }while(0); + add: + u->in += w->out; + break; + sub: + u->in -= w->out; + break; + mul: + u->in *= w->out; + break; + div: + u->in *= w->out; + break; + } while (0); } } } -__attribute__((hot)) -ANN void compute_multi(const UGen u) { +__attribute__((hot)) ANN void compute_multi(const UGen u) { u->done = 1; - uint i = 0; + uint i = 0; do compute_mono(UGEN(u->connect.multi->channel[i])); - while(++i < u->connect.multi->n_chan); + while (++i < u->connect.multi->n_chan); } -__attribute__((hot)) -ANN void compute_chan(const UGen u) { +__attribute__((hot)) ANN void compute_chan(const UGen u) { COMPUTE(u->module.ref); } -#define describe_compute(func, opt, aux) \ -__attribute__((hot)) \ -ANN void gen_compute_##func##opt(const UGen u) { \ - compute_##func(u); \ - aux \ - u->module.gen.tick(u); \ -} -describe_compute(mono,,) -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);}) +#define describe_compute(func, opt, aux) \ + __attribute__((hot)) ANN void gen_compute_##func##opt(const UGen u) { \ + compute_##func(u); \ + aux u->module.gen.tick(u); \ + } +describe_compute(mono, , ) 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 UGen new_UGen(MemPool p) { + ANEW UGen new_UGen(MemPool p) { const UGen u = mp_calloc(p, UGen); - u->op = 0; - u->compute = gen_compute_mono; + u->op = 0; + u->compute = gen_compute_mono; return u; } ANEW M_Object new_M_UGen(const struct Gwion_ *gwion) { const M_Object o = new_object(gwion->mp, NULL, gwion->type[et_ugen]); - UGEN(o) = new_UGen(gwion->mp); + UGEN(o) = new_UGen(gwion->mp); return o; } ANN static void assign_channel(const struct Gwion_ *gwion, const UGen u) { - u->multi = 1; + 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; + 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(gwion); - ugen_ini(gwion, 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; + ugen_ini(gwion, 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 struct Gwion_ *gwion, const UGen u, const f_tick tick, void* data, const m_bool trig) { +ANN void ugen_gen(const struct Gwion_ *gwion, 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(gwion->mp); + if (trig) { + u->module.gen.trig = new_UGen(gwion->mp); u->module.gen.trig->compute = compute_mono; ugen_ini(gwion, u->module.gen.trig, 1, 1); - u->compute = (u->compute == gen_compute_mono ? gen_compute_monotrig : gen_compute_multitrig); + u->compute = (u->compute == gen_compute_mono ? gen_compute_monotrig + : gen_compute_multitrig); } } -ANN void ugen_ini(const struct Gwion_ *gwion, const UGen u, const uint in, const uint out) { +ANN void ugen_ini(const struct Gwion_ *gwion, const UGen u, const uint in, + const uint out) { const uint chan = in > out ? in : out; - if(chan == 1) { + if (chan == 1) { u->connect.net = mp_calloc(gwion->mp, ugen_net); vector_init(&u->connect.net->from); vector_init(&u->connect.net->to); } else { - u->connect.multi = mp_calloc(gwion->mp, ugen_multi); + u->connect.multi = mp_calloc(gwion->mp, ugen_multi); u->connect.multi->n_in = in; u->connect.multi->n_out = out; u->connect.multi->n_chan = chan; @@ -138,23 +140,24 @@ ANN void ugen_ini(const struct Gwion_ *gwion, const UGen u, const uint in, const } } -ANN void connect_init(const VM_Shred shred, restrict M_Object* lhs, restrict M_Object* rhs) { +ANN void connect_init(const VM_Shred shred, restrict M_Object *lhs, + restrict M_Object *rhs) { POP_REG(shred, SZ_INT * 2); - *rhs = *(M_Object*)REG(SZ_INT); - *lhs = *(M_Object*)REG(0); + *rhs = *(M_Object *)REG(SZ_INT); + *lhs = *(M_Object *)REG(0); } -#define describe_connect(name, func) \ -ANN static inline void name##onnect(const restrict UGen lhs, const restrict UGen rhs) { \ - func(&rhs->connect.net->from, (vtype)lhs); \ - func(&lhs->connect.net->to, (vtype)rhs); \ - rhs->connect.net->size = (uint)vector_size(&rhs->connect.net->from); \ -} -describe_connect(C,vector_add) -describe_connect(Disc,vector_rem2) +#define describe_connect(name, func) \ + ANN static inline void name##onnect(const restrict UGen lhs, \ + const restrict UGen rhs) { \ + func(&rhs->connect.net->from, (vtype)lhs); \ + func(&lhs->connect.net->to, (vtype)rhs); \ + rhs->connect.net->size = (uint)vector_size(&rhs->connect.net->from); \ + } +describe_connect(C, vector_add) describe_connect(Disc, vector_rem2) -ANN static void release_connect(const VM_Shred shred) { - *(M_Object*)REG(0) = *(M_Object*)REG(SZ_INT); + ANN static void release_connect(const VM_Shred shred) { + *(M_Object *)REG(0) = *(M_Object *)REG(SZ_INT); PUSH_REG(shred, SZ_INT); } @@ -162,16 +165,16 @@ typedef ANN void (*f_connect)(const UGen lhs, const UGen rhs); ANN /* static */ void _do_(const f_connect f, const UGen lhs, const UGen rhs) { const m_bool l_multi = lhs->multi; const m_bool r_multi = rhs->multi; - const uint l_max = l_multi ? lhs->connect.multi->n_out : 1; - const uint r_max = r_multi ? rhs->connect.multi->n_in : 1; - const uint max = l_max > r_max ? l_max : r_max; - uint i = 0; + const uint l_max = l_multi ? lhs->connect.multi->n_out : 1; + const uint r_max = r_multi ? rhs->connect.multi->n_in : 1; + const uint max = l_max > r_max ? l_max : r_max; + uint i = 0; assert(r_max > 0); do { const UGen l = l_multi ? UGEN(lhs->connect.multi->channel[i % l_max]) : lhs; const UGen r = r_multi ? UGEN(rhs->connect.multi->channel[i % r_max]) : rhs; f(l, r); - } while(++i < max); + } while (++i < max); } ANN void ugen_connect(const restrict UGen lhs, const restrict UGen rhs) { @@ -182,123 +185,134 @@ ANN void ugen_disconnect(const restrict UGen lhs, const restrict UGen rhs) { _do_(Disconnect, lhs, rhs); } -#define TRIG_EX \ -if(!UGEN(rhs)->module.gen.trig) { \ - release_connect(shred); \ - handle(shred, "NonTriggerException"); \ - return; \ -} -#define describe_connect_instr(name, func, tgt, opt) \ -static INSTR(name##func) { \ - M_Object lhs, rhs; \ - connect_init(shred, &lhs, &rhs); \ - opt \ - _do_(func, UGEN(lhs), tgt); \ - release_connect(shred); \ -} -describe_connect_instr(Ugen, Connect, UGEN(rhs),) -describe_connect_instr(Ugen, Disconnect, UGEN(rhs),) -describe_connect_instr(Trig, Connect, UGEN(rhs)->module.gen.trig, TRIG_EX) -describe_connect_instr(Trig, Disconnect, UGEN(rhs)->module.gen.trig, TRIG_EX) - -#define describe_connectaa_instr(name, func, tgt, opt) \ -static INSTR(name##func) { \ - M_Object lhs, rhs; \ - connect_init(shred, &lhs, &rhs); \ - const m_uint lsz = m_vector_size(ARRAY(lhs)); \ - const m_uint rsz = m_vector_size(ARRAY(rhs)); \ - const m_uint min = lsz < rsz ? lsz : rsz; \ - for(m_uint i = 0; i < min; ++i) { \ - M_Object ulhs, urhs; \ - m_vector_get(ARRAY(lhs), i, &ulhs); \ - m_vector_get(ARRAY(rhs), i, &urhs); \ - opt \ - if(ulhs && urhs) \ - _do_(func, UGEN(ulhs), tgt); \ - } \ - release_connect(shred); \ -} -describe_connectaa_instr(UgenAA, Connect, UGEN(urhs),) -describe_connectaa_instr(UgenAA, Disconnect,UGEN(urhs),) -describe_connectaa_instr(TrigAA, Connect, UGEN(urhs)->module.gen.trig, TRIG_EX) -describe_connectaa_instr(TrigAA, Disconnect, UGEN(urhs)->module.gen.trig, TRIG_EX) - -#define describe_connectua_instr(name, func, tgt, opt) \ -static INSTR(name##func) { \ - M_Object lhs, rhs; \ - connect_init(shred, &lhs, &rhs); \ - const m_uint rsz = m_vector_size(ARRAY(rhs)); \ - for(m_uint i = 0; i < rsz; ++i) { \ - M_Object urhs; \ - m_vector_get(ARRAY(rhs), i, &urhs); \ - opt \ - if(urhs) \ - _do_(func, UGEN(lhs), tgt); \ - } \ - release_connect(shred); \ -} -describe_connectua_instr(UgenUA, Connect, UGEN(urhs),) -describe_connectua_instr(UgenUA, Disconnect, UGEN(urhs),) -describe_connectua_instr(TrigUA, Connect, UGEN(urhs)->module.gen.trig, TRIG_EX) -describe_connectua_instr(TrigUA, Disconnect, UGEN(urhs)->module.gen.trig, TRIG_EX) - -#define describe_connectau_instr(name, func, tgt, opt) \ -static INSTR(name##func) { \ - M_Object lhs, rhs; \ - connect_init(shred, &lhs, &rhs); \ - const m_uint lsz = m_vector_size(ARRAY(lhs)); \ - for(m_uint i = 0; i < lsz; ++i) { \ - M_Object ulhs; \ - m_vector_get(ARRAY(lhs), i, &ulhs); \ - opt \ - if(ulhs) \ - _do_(func, UGEN(ulhs), tgt); \ - } \ - release_connect(shred); \ -} -describe_connectau_instr(UgenAU, Connect, UGEN(rhs),) -describe_connectau_instr(UgenAU, Disconnect, UGEN(rhs),) -describe_connectau_instr(TrigAU, Connect, UGEN(rhs)->module.gen.trig, TRIG_EX) -describe_connectau_instr(TrigAU, Disconnect, UGEN(rhs)->module.gen.trig, TRIG_EX) - -static CTOR(ugen_ctor) { +#define TRIG_EX \ + if (!UGEN(rhs)->module.gen.trig) { \ + release_connect(shred); \ + handle(shred, "NonTriggerException"); \ + return; \ + } +#define describe_connect_instr(name, func, tgt, opt) \ + static INSTR(name##func) { \ + M_Object lhs, rhs; \ + connect_init(shred, &lhs, &rhs); \ + opt _do_(func, UGEN(lhs), tgt); \ + release_connect(shred); \ + } +describe_connect_instr(Ugen, Connect, UGEN(rhs), ) describe_connect_instr( + Ugen, Disconnect, + UGEN(rhs), ) describe_connect_instr(Trig, Connect, + UGEN(rhs)->module.gen.trig, TRIG_EX) + describe_connect_instr(Trig, Disconnect, UGEN(rhs)->module.gen.trig, + TRIG_EX) + +#define describe_connectaa_instr(name, func, tgt, opt) \ + static INSTR(name##func) { \ + M_Object lhs, rhs; \ + connect_init(shred, &lhs, &rhs); \ + const m_uint lsz = m_vector_size(ARRAY(lhs)); \ + const m_uint rsz = m_vector_size(ARRAY(rhs)); \ + const m_uint min = lsz < rsz ? lsz : rsz; \ + for (m_uint i = 0; i < min; ++i) { \ + M_Object ulhs, urhs; \ + m_vector_get(ARRAY(lhs), i, &ulhs); \ + m_vector_get(ARRAY(rhs), i, &urhs); \ + opt if (ulhs && urhs) _do_(func, UGEN(ulhs), tgt); \ + } \ + release_connect(shred); \ + } + describe_connectaa_instr( + UgenAA, Connect, + UGEN(urhs), ) describe_connectaa_instr(UgenAA, Disconnect, + UGEN(urhs), ) + describe_connectaa_instr( + TrigAA, Connect, UGEN(urhs)->module.gen.trig, + TRIG_EX) describe_connectaa_instr(TrigAA, Disconnect, + UGEN(urhs)->module.gen.trig, + TRIG_EX) + +#define describe_connectua_instr(name, func, tgt, opt) \ + static INSTR(name##func) { \ + M_Object lhs, rhs; \ + connect_init(shred, &lhs, &rhs); \ + const m_uint rsz = m_vector_size(ARRAY(rhs)); \ + for (m_uint i = 0; i < rsz; ++i) { \ + M_Object urhs; \ + m_vector_get(ARRAY(rhs), i, &urhs); \ + opt if (urhs) _do_(func, UGEN(lhs), tgt); \ + } \ + release_connect(shred); \ + } + describe_connectua_instr(UgenUA, Connect, UGEN(urhs), ) + describe_connectua_instr(UgenUA, Disconnect, UGEN(urhs), ) + describe_connectua_instr(TrigUA, Connect, + UGEN(urhs)->module.gen.trig, + TRIG_EX) + describe_connectua_instr( + TrigUA, Disconnect, UGEN(urhs)->module.gen.trig, + TRIG_EX) + +#define describe_connectau_instr(name, func, tgt, opt) \ + static INSTR(name##func) { \ + M_Object lhs, rhs; \ + connect_init(shred, &lhs, &rhs); \ + const m_uint lsz = m_vector_size(ARRAY(lhs)); \ + for (m_uint i = 0; i < lsz; ++i) { \ + M_Object ulhs; \ + m_vector_get(ARRAY(lhs), i, &ulhs); \ + opt if (ulhs) _do_(func, UGEN(ulhs), tgt); \ + } \ + release_connect(shred); \ + } + describe_connectau_instr(UgenAU, Connect, + UGEN(rhs), ) + describe_connectau_instr(UgenAU, Disconnect, + UGEN(rhs), ) + describe_connectau_instr( + TrigAU, Connect, + UGEN(rhs)->module.gen.trig, TRIG_EX) + describe_connectau_instr( + TrigAU, Disconnect, + UGEN(rhs)->module.gen.trig, + TRIG_EX) + + static CTOR(ugen_ctor) { UGEN(o) = new_UGen(shred->info->mp); vector_add(&shred->info->vm->ugen, (vtype)UGEN(o)); } -#define describe_release_func(src, tgt, opt) \ -ANN static void release_##src(const UGen ug) { \ - for(uint i = (uint)vector_size(&ug->connect.net->src) + 1; --i;) { \ - const UGen u = (UGen)vector_at(&ug->connect.net->src, i - 1); \ - vector_rem2(&u->connect.net->tgt, (vtype)ug); \ - opt \ - } \ - vector_release(&ug->connect.net->src); \ -} -describe_release_func(from, to,) -describe_release_func(to, from, --u->connect.net->size;) +#define describe_release_func(src, tgt, opt) \ + ANN static void release_##src(const UGen ug) { \ + for (uint i = (uint)vector_size(&ug->connect.net->src) + 1; --i;) { \ + const UGen u = (UGen)vector_at(&ug->connect.net->src, i - 1); \ + vector_rem2(&u->connect.net->tgt, (vtype)ug); \ + opt \ + } \ + vector_release(&ug->connect.net->src); \ + } +describe_release_func(from, to, ) + describe_release_func(to, from, --u->connect.net->size;) -ANN static void release_mono(MemPool p, const UGen ug) { + ANN static void release_mono(MemPool p, const UGen ug) { release_to(ug); release_from(ug); mp_free(p, ugen_net, ug->connect.net); } ANN static void release_multi(const UGen ug, const VM_Shred shred) { - for(uint i = ug->connect.multi->n_chan + 1; --i;) + for (uint i = ug->connect.multi->n_chan + 1; --i;) release(ug->connect.multi->channel[i - 1], shred); xfree(ug->connect.multi->channel); } static DTOR(ugen_dtor) { const UGen ug = UGEN(o); - MemPool p = shred->info->mp; + MemPool p = shred->info->mp; vector_rem2(&shred->info->vm->ugen, (vtype)ug); - if(!ug->multi) + if (!ug->multi) release_mono(p, ug); else release_multi(ug, shred); - if(ug->module.gen.trig) { + if (ug->module.gen.trig) { release_mono(p, ug->module.gen.trig); mp_free(shred->info->mp, UGen, ug->module.gen.trig); } @@ -306,51 +320,46 @@ static DTOR(ugen_dtor) { } static MFUN(ugen_channel) { - const m_int i = *(m_int*)MEM(SZ_INT); - if(!UGEN(o)->multi) - *(M_Object*)RETURN = !i ? o : NULL; - else if(i < 0 || (m_uint)i >= UGEN(o)->connect.multi->n_chan) - *(M_Object*)RETURN = NULL; + const m_int i = *(m_int *)MEM(SZ_INT); + if (!UGEN(o)->multi) + *(M_Object *)RETURN = !i ? o : NULL; + else if (i < 0 || (m_uint)i >= UGEN(o)->connect.multi->n_chan) + *(M_Object *)RETURN = NULL; else - *(M_Object*)RETURN = UGEN(o)->connect.multi->channel[i]; + *(M_Object *)RETURN = UGEN(o)->connect.multi->channel[i]; } -static MFUN(ugen_get_op) { - *(m_uint*)RETURN = UGEN(o)->op + 1; -} +static MFUN(ugen_get_op) { *(m_uint *)RETURN = UGEN(o)->op + 1; } ANN static void set_op(const UGen u, const uint f) { - if(u->multi) { - for(uint i = u->connect.multi->n_chan + 1; --i;) - UGEN(u->connect.multi->channel[i-1])->op = f; + if (u->multi) { + for (uint i = u->connect.multi->n_chan + 1; --i;) + UGEN(u->connect.multi->channel[i - 1])->op = f; } u->op = f; } static MFUN(ugen_set_op) { - const m_int i = *(m_int*)MEM(SZ_INT); - if(i >= 1 && i <= 4) - set_op(UGEN(o), i-1); - *(m_int*)RETURN = UGEN(o)->op + 1; + const m_int i = *(m_int *)MEM(SZ_INT); + if (i >= 1 && i <= 4) set_op(UGEN(o), i - 1); + *(m_int *)RETURN = UGEN(o)->op + 1; } -static MFUN(ugen_get_last) { - *(m_float*)RETURN = UGEN(o)->out; -} +static MFUN(ugen_get_last) { *(m_float *)RETURN = UGEN(o)->out; } struct ugen_importer { - const VM* vm; + const VM * vm; const f_tick tick; - const m_str name; - const uint nchan; + const m_str name; + const uint nchan; }; -ANN static UGen add_ugen(const Gwi gwi, struct ugen_importer* imp) { - VM* vm = gwi_vm(gwi); - const M_Object o = new_M_UGen(gwi->gwion); - const UGen u = UGEN(o); +ANN static UGen add_ugen(const Gwi gwi, struct ugen_importer *imp) { + VM * vm = gwi_vm(gwi); + const M_Object o = new_M_UGen(gwi->gwion); + const UGen u = UGEN(o); ugen_ini(vm->gwion, u, imp->nchan, imp->nchan); - ugen_gen(vm->gwion, u, imp->tick, (void*)imp->vm, 0); + ugen_gen(vm->gwion, u, imp->tick, (void *)imp->vm, 0); vector_add(&vm->ugen, (vtype)u); gwi_item_ini(gwi, "UGen", imp->name); gwi_item_end(gwi, ae_flag_const, obj, o); @@ -358,12 +367,12 @@ ANN static UGen add_ugen(const Gwi gwi, struct ugen_importer* imp) { } static GWION_IMPORT(global_ugens) { - const VM* vm = gwi_vm(gwi); - struct ugen_importer imp_hole = { vm, compute_mono, "blackhole", 1 }; - const UGen hole = add_ugen(gwi, &imp_hole); - struct ugen_importer imp_dac = { vm, dac_tick, "dac", vm->bbq->si->out }; - const UGen dac = add_ugen(gwi, &imp_dac); - struct ugen_importer imp_adc = { vm, adc_tick, "adc", vm->bbq->si->in }; + const VM * vm = gwi_vm(gwi); + struct ugen_importer imp_hole = {vm, compute_mono, "blackhole", 1}; + const UGen hole = add_ugen(gwi, &imp_hole); + struct ugen_importer imp_dac = {vm, dac_tick, "dac", vm->bbq->si->out}; + const UGen dac = add_ugen(gwi, &imp_dac); + struct ugen_importer imp_adc = {vm, adc_tick, "adc", vm->bbq->si->in}; (void)add_ugen(gwi, &imp_adc); ugen_connect(dac, hole); SET_FLAG(gwi->gwion->type[et_ugen], abstract); @@ -371,7 +380,7 @@ static GWION_IMPORT(global_ugens) { } static OP_CHECK(opck_chuck_ugen) { - const Exp_Binary* bin = (Exp_Binary*)data; + const Exp_Binary *bin = (Exp_Binary *)data; return bin->rhs->type; } diff --git a/src/lib/union.c b/src/lib/union.c index 74bfef01..2ed7e311 100644 --- a/src/lib/union.c +++ b/src/lib/union.c @@ -13,12 +13,10 @@ #include "gack.h" #include "traverse.h" -static GACK(gack_none) { - INTERP_PRINTF("None") -} +static GACK(gack_none) { INTERP_PRINTF("None") } static OP_CHECK(opck_none) { - Exp_Binary *bin = (Exp_Binary*)data; + Exp_Binary *bin = (Exp_Binary *)data; CHECK_NN(opck_rassign(env, data)); exp_setvar(bin->rhs, 1); return bin->rhs->type; @@ -26,33 +24,37 @@ static OP_CHECK(opck_none) { static OP_EMIT(opem_none) { const Instr instr = emit_add_instr(emit, RegMove); - instr->m_val = -SZ_INT; + instr->m_val = -SZ_INT; return GW_OK; } -static const f_instr unionmember[] = { UnionMember, UnionMember2, UnionMember3, UnionMember4 }; -ANN Instr emit_kind(Emitter emit, const m_uint size, const uint addr, const f_instr func[]); +static const f_instr unionmember[] = {UnionMember, UnionMember2, UnionMember3, + UnionMember4}; +ANN Instr emit_kind(Emitter emit, const m_uint size, const uint addr, + const f_instr func[]); static OP_EMIT(opem_union_dot) { - const Exp_Dot *member = (Exp_Dot*)data; - const Map map = &member->base->type->nspc->info->value->map; + const Exp_Dot *member = (Exp_Dot *)data; + const Map map = &member->base->type->nspc->info->value->map; CHECK_BB(emit_exp(emit, member->base)); - if(isa(exp_self(member)->type, emit->gwion->type[et_function]) > 0) { + if (isa(exp_self(member)->type, emit->gwion->type[et_function]) > 0) { const Instr instr = emit_add_instr(emit, RegPushImm); - const Func f = (Func)vector_front(&member->base->type->info->parent->nspc->vtable); + const Func f = + (Func)vector_front(&member->base->type->info->parent->nspc->vtable); instr->m_val = (m_uint)f->code; return GW_OK; } - if(!strcmp(s_name(member->xid), "@index")) { + if (!strcmp(s_name(member->xid), "@index")) { emit_add_instr(emit, DotMember); return GW_OK; } - for(m_uint i = 0; i < map_size(map); ++i) { - if(VKEY(map, i) == (m_uint)member->xid) { - const Value v = (Value)VVAL(map, i); - const uint emit_addr = exp_getvar(exp_self(member)); - const Instr instr = emit_kind(emit, v->type->size, emit_addr, unionmember); - instr->m_val = i;// + 1; + for (m_uint i = 0; i < map_size(map); ++i) { + if (VKEY(map, i) == (m_uint)member->xid) { + const Value v = (Value)VVAL(map, i); + const uint emit_addr = exp_getvar(exp_self(member)); + const Instr instr = + emit_kind(emit, v->type->size, emit_addr, unionmember); + instr->m_val = i; // + 1; instr->m_val2 = v->type->size; return GW_OK; } @@ -61,35 +63,37 @@ static OP_EMIT(opem_union_dot) { } static DTOR(UnionDtor) { - const m_uint idx = *(m_uint*)o->data; - if(idx) { - const Map map = &o->type_ref->nspc->info->value->map; - const Value v = (Value)map_at(map, idx-1); - if(isa(v->type, shred->info->vm->gwion->type[et_compound]) > 0) + const m_uint idx = *(m_uint *)o->data; + if (idx) { + const Map map = &o->type_ref->nspc->info->value->map; + const Value v = (Value)map_at(map, idx - 1); + if (isa(v->type, shred->info->vm->gwion->type[et_compound]) > 0) compound_release(shred, v->type, (o->data + SZ_INT)); } } static OP_CHECK(opck_union_is) { - const Exp e = (Exp)data; + const Exp e = (Exp)data; const Exp_Call *call = &e->d.exp_call; - const Exp exp = call->args; - if(exp->exp_type != ae_exp_primary && exp->d.prim.prim_type != ae_prim_id) + const Exp exp = call->args; + if (exp->exp_type != ae_exp_primary && exp->d.prim.prim_type != ae_prim_id) ERR_N(exp->pos, "Union.is() argument must be of form id"); - const Type t = call->func->d.exp_dot.base->type; + const Type t = call->func->d.exp_dot.base->type; const Value v = find_value(t, exp->d.prim.d.var); - if(!v) - ERR_N(exp->pos, "'%s' has no member '%s'", t->name, s_name(exp->d.prim.d.var)); + if (!v) + ERR_N(exp->pos, "'%s' has no member '%s'", t->name, + s_name(exp->d.prim.d.var)); const Map map = &t->nspc->info->value->map; - for(m_uint i = 0; i < map_size(map); ++i) { + for (m_uint i = 0; i < map_size(map); ++i) { const Value v = (Value)VVAL(map, i); - if(!strcmp(s_name(exp->d.prim.d.var), v->name)) { - const Exp exp_func = call->func; - const Exp exp_args = call->args; - e->exp_type = ae_exp_binary; + if (!strcmp(s_name(exp->d.prim.d.var), v->name)) { + const Exp exp_func = call->func; + const Exp exp_args = call->args; + e->exp_type = ae_exp_binary; e->d.exp_binary.lhs = cpy_exp(env->gwion->mp, exp_func); - e->d.exp_binary.lhs->d.exp_dot.xid = insert_symbol(env->gwion->st, "@index"); -// e->d.exp_binary.rhs = new_prim_int(env->gwion->mp, i+1, e->pos); + e->d.exp_binary.lhs->d.exp_dot.xid = + insert_symbol(env->gwion->st, "@index"); + // e->d.exp_binary.rhs = new_prim_int(env->gwion->mp, i+1, e->pos); e->d.exp_binary.rhs = new_prim_int(env->gwion->mp, i, e->pos); free_exp(env->gwion->mp, exp_func); free_exp(env->gwion->mp, exp_args); @@ -102,31 +106,30 @@ static OP_CHECK(opck_union_is) { } static MFUN(union_is) { - *(m_uint*)RETURN = *(m_uint*)MEM(SZ_INT) == *(m_uint*)o->data; + *(m_uint *)RETURN = *(m_uint *)MEM(SZ_INT) == *(m_uint *)o->data; } static OP_CHECK(opck_union_ctor) { - Exp_Call *call = (Exp_Call*)data; + Exp_Call *call = (Exp_Call *)data; const Exp name = call->args; - if(!name || !name->next || name->next->next) + if (!name || !name->next || name->next->next) ERR_N(name->pos, "Union constructor takes two arguments, " - "'id' and 'value'"); - if(name->exp_type != ae_exp_primary || - name->d.prim.prim_type != ae_prim_id) - return NULL; - const Exp val = name->next; + "'id' and 'value'"); + if (name->exp_type != ae_exp_primary || name->d.prim.prim_type != ae_prim_id) + return NULL; + const Exp val = name->next; const Type base = actual_type(env->gwion, call->func->type); - const Map map = &base->nspc->info->value->map; - for(m_uint i = 0; i < map_size(map); ++i) { - if(VKEY(map, i) == (m_uint)name->d.prim.d.var) { - const Value v = (Value)VVAL(map, i); + const Map map = &base->nspc->info->value->map; + for (m_uint i = 0; i < map_size(map); ++i) { + if (VKEY(map, i) == (m_uint)name->d.prim.d.var) { + const Value v = (Value)VVAL(map, i); name->d.prim.prim_type = ae_prim_num; - name->d.prim.d.num = i; - name->type = env->gwion->type[et_int]; + name->d.prim.d.num = i; + name->type = env->gwion->type[et_int]; DECL_ON(const Type, t, = check_exp(env, val)); - if(isa(t, v->type) < 0) { - ERR_N(val->pos, "Invalid type '%s' for '%s', should be '%s'", - t->name, v->name, v->type->name); + if (isa(t, v->type) < 0) { + ERR_N(val->pos, "Invalid type '%s' for '%s', should be '%s'", t->name, + v->name, v->type->name); } return base; } @@ -137,18 +140,19 @@ static OP_CHECK(opck_union_ctor) { static INSTR(UnionCtor) { POP_REG(shred, instr->m_val2); POP_REG(shred, SZ_INT); - const m_uint index = *(m_uint*)REG(-SZ_INT); - const M_Object o = *(M_Object*)REG(-SZ_INT) = new_object(shred->info->mp, NULL, (Type)instr->m_val); - *(m_uint*)o->data = index;// + 1; + const m_uint index = *(m_uint *)REG(-SZ_INT); + const M_Object o = *(M_Object *)REG(-SZ_INT) = + new_object(shred->info->mp, NULL, (Type)instr->m_val); + *(m_uint *)o->data = index; // + 1; memcpy(o->data + SZ_INT, REG(0), instr->m_val2); } static OP_EMIT(opem_union_ctor) { - Exp_Call *call = (Exp_Call*)data; - const Type base = actual_type(emit->gwion, call->func->type); + Exp_Call * call = (Exp_Call *)data; + const Type base = actual_type(emit->gwion, call->func->type); const Instr instr = emit_add_instr(emit, UnionCtor); - instr->m_val = (m_uint)base; - instr->m_val2 = call->args->next->type->size; + instr->m_val = (m_uint)base; + instr->m_val2 = call->args->next->type->size; return GW_OK; } @@ -157,7 +161,8 @@ ANN GWION_IMPORT(union) { GWI_BB(gwi_set_global_type(gwi, t_none, et_none)) GWI_BB(gwi_gack(gwi, t_none, gack_none)) gwi_add_type(gwi, t_none); - struct SpecialId_ spid = { .type=gwi->gwion->type[et_none], .exec=NoOp, .is_const=1 }; + struct SpecialId_ spid = { + .type = gwi->gwion->type[et_none], .exec = NoOp, .is_const = 1}; gwi_specialid(gwi, "None", &spid); GWI_BB(gwi_oper_ini(gwi, "None", "None", "None")) @@ -179,10 +184,14 @@ ANN GWION_IMPORT(union) { GWI_BB(gwi_oper_emi(gwi, opem_union_ctor)) GWI_BB(gwi_oper_end(gwi, "@ctor", NULL)) - const Func f = (Func)vector_front(&t_union->nspc->vtable); - const struct Op_Func opfunc = { .ck=opck_union_is }; - const struct Op_Import opi = { .rhs=f->value_ref->type, - .func=&opfunc, .data=(uintptr_t)f, .pos=gwi->loc, .op=insert_symbol(gwi->gwion->st, "@func_check") }; + const Func f = (Func)vector_front(&t_union->nspc->vtable); + const struct Op_Func opfunc = {.ck = opck_union_is}; + const struct Op_Import opi = { + .rhs = f->value_ref->type, + .func = &opfunc, + .data = (uintptr_t)f, + .pos = gwi->loc, + .op = insert_symbol(gwi->gwion->st, "@func_check")}; CHECK_BB(add_op(gwi->gwion, &opi)); gwi->gwion->type[et_union] = t_union; diff --git a/src/lib/vararg.c b/src/lib/vararg.c index f418b88a..0bed6204 100644 --- a/src/lib/vararg.c +++ b/src/lib/vararg.c @@ -15,8 +15,8 @@ #include "parse.h" #include "gack.h" -void free_vararg(MemPool p, struct Vararg_* arg) { - if(arg->t.ptr) { +void free_vararg(MemPool p, struct Vararg_ *arg) { + if (arg->t.ptr) { xfree(arg->d); vector_release(&arg->t); } @@ -24,16 +24,16 @@ void free_vararg(MemPool p, struct Vararg_* arg) { } static DTOR(vararg_dtor) { - struct Vararg_ *arg = *(struct Vararg_**)o->data; - if(*(m_uint*)(o->data + SZ_INT*2)) { + struct Vararg_ *arg = *(struct Vararg_ **)o->data; + if (*(m_uint *)(o->data + SZ_INT * 2)) { m_uint offset = 0; - for(m_uint i = 0; i < vector_size(&arg->t); ++i) { -// could be compound release + for (m_uint i = 0; i < vector_size(&arg->t); ++i) { + // could be compound release const Type t = (Type)vector_at(&arg->t, i); - if(isa(t, shred->info->vm->gwion->type[et_object]) > 0) - release(*(M_Object*)(arg->d + offset), shred); - else if(tflag(t, tflag_struct)) - struct_release(shred, t, *(m_bit**)(arg->d + offset)); + if (isa(t, shred->info->vm->gwion->type[et_object]) > 0) + release(*(M_Object *)(arg->d + offset), shred); + else if (tflag(t, tflag_struct)) + struct_release(shred, t, *(m_bit **)(arg->d + offset)); offset += t->size; } } @@ -41,128 +41,130 @@ static DTOR(vararg_dtor) { } static MFUN(mfun_vararg_cpy) { - struct Vararg_ *src = *(struct Vararg_**)o->data; - struct Vararg_* arg = mp_calloc(shred->info->mp, Vararg); + struct Vararg_ *src = *(struct Vararg_ **)o->data; + struct Vararg_ *arg = mp_calloc(shred->info->mp, Vararg); vector_copy2(&src->t, &arg->t); - arg->d = (m_bit*)xmalloc(round2szint(*(m_uint*)(o->data + SZ_INT*2))); + arg->d = (m_bit *)xmalloc(round2szint(*(m_uint *)(o->data + SZ_INT * 2))); m_uint offset = 0; - for(m_uint i = 0; i < vector_size(&arg->t); ++i) { - const Type t = (Type)vector_at(&arg->t, *(m_uint*)(o->data + SZ_INT*4)); - *(m_uint*)(arg->d + offset) = *(m_uint*)(src->d + offset); - if(isa(t, shred->info->vm->gwion->type[et_object]) > 0) - ++(*(M_Object*)(arg->d + offset))->ref; + for (m_uint i = 0; i < vector_size(&arg->t); ++i) { + const Type t = (Type)vector_at(&arg->t, *(m_uint *)(o->data + SZ_INT * 4)); + *(m_uint *)(arg->d + offset) = *(m_uint *)(src->d + offset); + if (isa(t, shred->info->vm->gwion->type[et_object]) > 0) + ++(*(M_Object *)(arg->d + offset))->ref; offset += t->size; } const M_Object obj = new_object(shred->info->mp, shred, o->type_ref); - *(struct Vararg_**)obj->data = arg; - *(m_uint*)(obj->data + SZ_INT*2) = *(m_uint*)(o->data + SZ_INT*2); - *(m_uint*)(obj->data + SZ_INT*3) = *(m_uint*)(o->data + SZ_INT*3); - *(m_uint*)(obj->data + SZ_INT*4) = *(m_uint*)(o->data + SZ_INT*4); - *(m_uint*)(obj->data + SZ_INT*4) = vector_size(&arg->t); // can we copy? - *(M_Object*)RETURN = obj; + *(struct Vararg_ **)obj->data = arg; + *(m_uint *)(obj->data + SZ_INT * 2) = *(m_uint *)(o->data + SZ_INT * 2); + *(m_uint *)(obj->data + SZ_INT * 3) = *(m_uint *)(o->data + SZ_INT * 3); + *(m_uint *)(obj->data + SZ_INT * 4) = *(m_uint *)(o->data + SZ_INT * 4); + *(m_uint *)(obj->data + SZ_INT * 4) = vector_size(&arg->t); // can we copy? + *(M_Object *)RETURN = obj; } INSTR(VarargIni) { - const M_Object o = new_object(shred->info->mp, shred, shred->info->vm->gwion->type[et_vararg]); - struct Vararg_* arg = mp_calloc(shred->info->mp, Vararg); - *(struct Vararg_**)o->data = arg; + const M_Object o = new_object(shred->info->mp, shred, + shred->info->vm->gwion->type[et_vararg]); + struct Vararg_ *arg = mp_calloc(shred->info->mp, Vararg); + *(struct Vararg_ **)o->data = arg; POP_REG(shred, instr->m_val - SZ_INT) - if((*(m_uint*)(o->data + SZ_INT * 2) = instr->m_val)) { - arg->d = (m_bit*)xmalloc(round2szint(instr->m_val)); + if ((*(m_uint *)(o->data + SZ_INT * 2) = instr->m_val)) { + arg->d = (m_bit *)xmalloc(round2szint(instr->m_val)); const Vector kinds = (Vector)instr->m_val2; vector_copy2(kinds, &arg->t); m_uint offset = 0; - for(m_uint i = 0; i < vector_size(&arg->t); ++i) { - const Type t = (Type)vector_at(&arg->t, i); - *(m_uint*)(arg->d + offset) = *(m_uint*)(shred->reg - SZ_INT + offset); - if(isa(t, shred->info->vm->gwion->type[et_object]) > 0) { - const M_Object obj = *(M_Object*)(arg->d + offset); - if(obj) - ++obj->ref; + for (m_uint i = 0; i < vector_size(&arg->t); ++i) { + const Type t = (Type)vector_at(&arg->t, i); + *(m_uint *)(arg->d + offset) = *(m_uint *)(shred->reg - SZ_INT + offset); + if (isa(t, shred->info->vm->gwion->type[et_object]) > 0) { + const M_Object obj = *(M_Object *)(arg->d + offset); + if (obj) ++obj->ref; } offset += t->size; } - *(m_uint*)(o->data + SZ_INT * 5) = vector_size(kinds); + *(m_uint *)(o->data + SZ_INT * 5) = vector_size(kinds); } - *(M_Object*)REG(-SZ_INT) = o; + *(M_Object *)REG(-SZ_INT) = o; } static INSTR(VarargEnd) { - const M_Object o = *(M_Object*)REG(0); - struct Vararg_* arg = *(struct Vararg_**)o->data; - *(m_uint*)(o->data + SZ_INT*3) += arg->t.ptr ? ((Type)vector_at(&arg->t, *(m_uint*)(o->data + SZ_INT*4)))->size : 0; - if(++*(m_uint*)(o->data + SZ_INT*4) == *(m_uint*)(o->data + SZ_INT * 5)) { -// if(++*(m_uint*)(o->data + SZ_INT*4) < *(m_uint*)(o->data + SZ_INT * 5)) -// shred->pc = instr->m_val; -// else - *(m_uint*)(o->data + SZ_INT*4) = *(m_uint*)(o->data + SZ_INT*3) = 0; + const M_Object o = *(M_Object *)REG(0); + struct Vararg_ *arg = *(struct Vararg_ **)o->data; + *(m_uint *)(o->data + SZ_INT * 3) += + arg->t.ptr + ? ((Type)vector_at(&arg->t, *(m_uint *)(o->data + SZ_INT * 4)))->size + : 0; + if (++*(m_uint *)(o->data + SZ_INT * 4) == + *(m_uint *)(o->data + SZ_INT * 5)) { + // if(++*(m_uint*)(o->data + SZ_INT*4) < *(m_uint*)(o->data + SZ_INT * 5)) + // shred->pc = instr->m_val; + // else + *(m_uint *)(o->data + SZ_INT * 4) = *(m_uint *)(o->data + SZ_INT * 3) = 0; ++shred->pc; } } static OP_CHECK(opck_vararg_cast) { - const Exp_Cast* cast = (Exp_Cast*)data; + const Exp_Cast *cast = (Exp_Cast *)data; return known_type(env, cast->td); } static INSTR(VarargCast) { - const M_Object o = *(M_Object*)REG(-SZ_INT); - if(!*(m_uint*)(o->data + SZ_INT)) { - handle(shred, "Using Vararg outside varloop"); + const M_Object o = *(M_Object *)REG(-SZ_INT); + if (!*(m_uint *)(o->data + SZ_INT)) { + handle(shred, "Using Vararg outside varloop"); return; } - struct Vararg_* arg = *(struct Vararg_**)o->data; - const Type t = (Type)instr->m_val, - u = (Type)vector_at(&arg->t, *(m_uint*)(o->data + SZ_INT*4)); - if(isa(u, t) > 0 || - (u == shred->info->vm->gwion->type[et_error] && - isa(t, shred->info->vm->gwion->type[et_object]) > 0)) { - for(m_uint i = 0; i < t->size; i += SZ_INT) - *(m_uint*)REG(i - SZ_INT) = *(m_uint*)(arg->d + *(m_uint*)(o->data + SZ_INT*3) + i); + struct Vararg_ *arg = *(struct Vararg_ **)o->data; + const Type t = (Type)instr->m_val, + u = (Type)vector_at(&arg->t, *(m_uint *)(o->data + SZ_INT * 4)); + if (isa(u, t) > 0 || (u == shred->info->vm->gwion->type[et_error] && + isa(t, shred->info->vm->gwion->type[et_object]) > 0)) { + for (m_uint i = 0; i < t->size; i += SZ_INT) + *(m_uint *)REG(i - SZ_INT) = + *(m_uint *)(arg->d + *(m_uint *)(o->data + SZ_INT * 3) + i); } else - handle(shred, "InvalidVariadicAccess"); + handle(shred, "InvalidVariadicAccess"); } static OP_EMIT(opem_vararg_cast) { - const Exp_Cast* cast = (Exp_Cast*)data; - const Instr instr = emit_add_instr(emit, VarargCast); - instr->m_val = (m_uint)exp_self(cast)->type; - const Instr push = emit_add_instr(emit, RegMove); - push->m_val = exp_self(cast)->type->size - SZ_INT; + const Exp_Cast *cast = (Exp_Cast *)data; + const Instr instr = emit_add_instr(emit, VarargCast); + instr->m_val = (m_uint)exp_self(cast)->type; + const Instr push = emit_add_instr(emit, RegMove); + push->m_val = exp_self(cast)->type->size - SZ_INT; return GW_OK; } static FREEARG(freearg_vararg) { - if(instr->m_val2) - free_vector(((Gwion)gwion)->mp, (Vector)instr->m_val2); + if (instr->m_val2) free_vector(((Gwion)gwion)->mp, (Vector)instr->m_val2); } static ID_CHECK(idck_vararg) { - if(env->func && fbflag(env->func->def->base, fbflag_variadic)) + if (env->func && fbflag(env->func->def->base, fbflag_variadic)) return exp_self(prim)->type; - ERR_O(exp_self(prim)->pos, _("'vararg' must be used inside variadic function")) + ERR_O(exp_self(prim)->pos, + _("'vararg' must be used inside variadic function")) } static ID_EMIT(idem_vararg) { const Instr instr = emit_add_instr(emit, RegPushMem); - instr->m_val = emit->vararg_offset + emit->code->stack_depth - SZ_INT; + instr->m_val = emit->vararg_offset + emit->code->stack_depth - SZ_INT; return instr; } -static GACK(gack_vararg) { - INTERP_PRINTF("%p", *(M_Object*)VALUE); -} +static GACK(gack_vararg) { INTERP_PRINTF("%p", *(M_Object *)VALUE); } ANN void emit_vararg_end(const Emitter emit, const m_uint pc) { const Instr pop = emit_add_instr(emit, RegMove); - pop->m_val = -SZ_INT; + pop->m_val = -SZ_INT; (void)emit_add_instr(emit, VarargEnd); const Instr instr = emit_add_instr(emit, Goto); - instr->m_val = pc; + instr->m_val = pc; } GWION_IMPORT(vararg) { - const Type t_vararg = gwi_class_ini(gwi, "Vararg", "Object"); + const Type t_vararg = gwi_class_ini(gwi, "Vararg", "Object"); gwi_class_xtor(gwi, NULL, vararg_dtor); gwi_gack(gwi, t_vararg, gack_vararg); GWI_BB(gwi_item_ini(gwi, "@internal", "@data")) @@ -187,7 +189,8 @@ GWION_IMPORT(vararg) { GWI_BB(gwi_oper_emi(gwi, opem_vararg_cast)) GWI_BB(gwi_oper_end(gwi, "$", NULL)) gwi_register_freearg(gwi, VarargIni, freearg_vararg); - struct SpecialId_ spid = { .type=t_vararg, .is_const=1, .ck=idck_vararg, .em=idem_vararg}; + struct SpecialId_ spid = { + .type = t_vararg, .is_const = 1, .ck = idck_vararg, .em = idem_vararg}; gwi_specialid(gwi, "vararg", &spid); return GW_OK; } diff --git a/src/main.c b/src/main.c index 32638362..864cb9dc 100644 --- a/src/main.c +++ b/src/main.c @@ -21,26 +21,28 @@ static void afl_run(const Gwion gwion) { gw_seed(gwion->vm->rand, 0); __AFL_INIT(); while (__AFL_LOOP(256)) { - FILE* f = fdopen(0, "r"); + FILE *f = fdopen(0, "r"); push_global(gwion, "[afl]"); - if(compile_file(gwion, "afl", f)) - gwion_run(gwion); + if (compile_file(gwion, "afl", f)) gwion_run(gwion); pop_global(gwion); } } -#define gwion_run(a) { afl_run(a); return 0; } +#define gwion_run(a) \ + { \ + afl_run(a); \ + return 0; \ + } #endif -int main(int argc, char** argv) { - Arg arg = { .arg={.argc=argc, .argv=argv}, .loop=false }; +int main(int argc, char **argv) { + Arg arg = {.arg = {.argc = argc, .argv = argv}, .loop = false}; signal(SIGINT, sig); signal(SIGTERM, sig); struct Gwion_ gwion = {}; - const m_bool ini = gwion_ini(&gwion, &arg); + const m_bool ini = gwion_ini(&gwion, &arg); arg_release(&arg); - if(ini > 0) - gwion_run(&gwion); + if (ini > 0) gwion_run(&gwion); gwion_end(&gwion); #ifndef BUILD_ON_WINDOWS pthread_exit(NULL); diff --git a/src/parse/check.c b/src/parse/check.c index 022a6eae..7b4678c9 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -16,95 +16,95 @@ #include "tmp_resolve.h" ANN static m_bool check_stmt_list(const Env env, Stmt_List list); -ANN m_bool check_class_def(const Env env, const Class_Def class_def); - -ANN static Type check_internal(const Env env, const Symbol sym, - const Exp e, const Type t) { - struct Implicit imp = { .e=e, .t=t, .pos=e->pos }; - struct Op_Import opi = { .op=sym, .lhs=e->type, - .rhs=t, .data=(uintptr_t)&imp, .pos=e->pos }; +ANN m_bool check_class_def(const Env env, const Class_Def class_def); + +ANN static Type check_internal(const Env env, const Symbol sym, const Exp e, + const Type t) { + struct Implicit imp = {.e = e, .t = t, .pos = e->pos}; + struct Op_Import opi = {.op = sym, + .lhs = e->type, + .rhs = t, + .data = (uintptr_t)&imp, + .pos = e->pos}; return op_check(env, &opi); } ANN m_bool check_implicit(const Env env, const Exp e, const Type t) { - if(e->type == t) - return GW_OK; + if (e->type == t) return GW_OK; const Symbol sym = insert_symbol("@implicit"); return (e->cast_to = check_internal(env, sym, e, t)) ? GW_OK : GW_ERROR; } -ANN m_bool check_subscripts(Env env, const Array_Sub array, const m_bool is_decl) { +ANN m_bool check_subscripts(Env env, const Array_Sub array, + const m_bool is_decl) { CHECK_OB(check_exp(env, array->exp)); m_uint depth = 0; - Exp e = array->exp; - do if(is_decl) - CHECK_BB(check_implicit(env, e, env->gwion->type[et_int])); - while(++depth && (e = e->next)); - if(depth != array->depth) + Exp e = array->exp; + do + if (is_decl) CHECK_BB(check_implicit(env, e, env->gwion->type[et_int])); + while (++depth && (e = e->next)); + if (depth != array->depth) ERR_B(array->exp->pos, _("invalid array acces expression.")) return GW_OK; } -ANN static inline m_bool check_exp_decl_parent(const Env env, const Var_Decl var) { +ANN static inline m_bool check_exp_decl_parent(const Env env, + const Var_Decl var) { const Value value = find_value(env->class_def->info->parent, var->xid); - if(value) { - env_err(env, var->pos, - _("Value defined in parent class")); + if (value) { + env_err(env, var->pos, _("Value defined in parent class")); defined_here(value); return GW_ERROR; } return GW_OK; } -#define describe_check_decl(a, b, flag) \ -ANN static inline void decl_##a(const Env env, const Value v) { \ - const Nspc nspc = env->curr; \ - flag; \ - v->from->offset = nspc->info->b; \ - nspc->info->b += v->type->size; \ -} +#define describe_check_decl(a, b, flag) \ + ANN static inline void decl_##a(const Env env, const Value v) { \ + const Nspc nspc = env->curr; \ + flag; \ + v->from->offset = nspc->info->b; \ + nspc->info->b += v->type->size; \ + } describe_check_decl(member, offset, v->vflag |= vflag_member) -describe_check_decl(static, class_data_size, SET_FLAG(v, static)) - -ANN static m_bool check_fptr_decl(const Env env, const Var_Decl var) { - const Value v = var->value; - Type t = v->type; - while(tflag(t, tflag_typedef)) - t = t->info->parent; - if(!t->info->func) - return GW_ERROR; - if(!env->class_def) - return GW_OK; - const Func func = t->info->func; + describe_check_decl(static, class_data_size, SET_FLAG(v, static)) + + ANN static m_bool check_fptr_decl(const Env env, const Var_Decl var) { + const Value v = var->value; + Type t = v->type; + while (tflag(t, tflag_typedef)) t = t->info->parent; + if (!t->info->func) return GW_ERROR; + if (!env->class_def) return GW_OK; + const Func func = t->info->func; const Type type = func->value_ref->from->owner_class; - if(type && isa(type, env->class_def) < 0 && !GET_FLAG(func, global)) + if (type && isa(type, env->class_def) < 0 && !GET_FLAG(func, global)) ERR_B(var->pos, _("can't use non global fptr of other class.")) - if(vflag(func->value_ref, vflag_member) && GET_FLAG(v, static)) - ERR_B(var->pos, _("can't use static variables for member function.")) + if (vflag(func->value_ref, vflag_member) && GET_FLAG(v, static)) + ERR_B(var->pos, _("can't use static variables for member function.")) return GW_OK; } ANN static m_bool check_var(const Env env, const Var_Decl var) { - if(env->class_def && !env->scope->depth && env->class_def->info->parent) + if (env->class_def && !env->scope->depth && env->class_def->info->parent) CHECK_BB(check_exp_decl_parent(env, var)); - if(var->value->type->effects.ptr) { + if (var->value->type->effects.ptr) { const Vector v = &var->value->type->effects; - for(m_uint i = 0; i < vector_size(v); i++) + for (m_uint i = 0; i < vector_size(v); i++) env_add_effect(env, (Symbol)vector_at(v, i), var->pos); } - if(var->array && var->array->exp) + if (var->array && var->array->exp) return check_subscripts(env, var->array, 1); return GW_OK; } -ANN static m_bool check_var_td(const Env env, const Var_Decl var, Type_Decl *const td) { +ANN static m_bool check_var_td(const Env env, const Var_Decl var, + Type_Decl *const td) { const Value v = var->value; - if(env->class_def) { - if(vflag(v, vflag_member)) { + if (env->class_def) { + if (vflag(v, vflag_member)) { decl_member(env, v); - if(env->class_def->info->tuple) - tuple_info(env, v); - } else if(GET_FLAG(td, static)) + if (env->class_def->info->tuple) tuple_info(env, v); + } else if (GET_FLAG(td, static)) decl_static(env, v); } return GW_OK; @@ -116,70 +116,71 @@ ANN static m_bool check_decl(const Env env, const Exp_Decl *decl) { const Var_Decl var = list->self; CHECK_BB(check_var(env, var)); CHECK_BB(check_var_td(env, var, decl->td)); - if(is_fptr(env->gwion, decl->type)) - CHECK_BB(check_fptr_decl(env, var)); + if (is_fptr(env->gwion, decl->type)) CHECK_BB(check_fptr_decl(env, var)); set_vflag(var->value, vflag_valid); - //set_vflag(var->value, vflag_used)); + // set_vflag(var->value, vflag_used)); nspc_add_value(env->curr, var->xid, var->value); - } while((list = list->next)); + } while ((list = list->next)); return GW_OK; } ANN static inline m_bool ensure_check(const Env env, const Type t) { - if(tflag(t, tflag_check) || !(tflag(t, tflag_cdef) || tflag(t, tflag_udef))) + if (tflag(t, tflag_check) || !(tflag(t, tflag_cdef) || tflag(t, tflag_udef))) return GW_OK; - struct EnvSet es = { .env=env, .data=env, .func=(_exp_func)check_cdef, - .scope=env->scope->depth, .flag=tflag_check }; + struct EnvSet es = {.env = env, + .data = env, + .func = (_exp_func)check_cdef, + .scope = env->scope->depth, + .flag = tflag_check}; return envset_run(&es, t); } ANN m_bool ensure_traverse(const Env env, const Type t) { - if(tflag(t, tflag_check) || !(tflag(t, tflag_cdef) || tflag(t, tflag_udef))) + if (tflag(t, tflag_check) || !(tflag(t, tflag_cdef) || tflag(t, tflag_udef))) return GW_OK; - struct EnvSet es = { .env=env, .data=env, .func=(_exp_func)traverse_cdef, - .scope=env->scope->depth, .flag=tflag_check }; + struct EnvSet es = {.env = env, + .data = env, + .func = (_exp_func)traverse_cdef, + .scope = env->scope->depth, + .flag = tflag_check}; return envset_run(&es, t); } -ANN static inline m_bool inferable(const Env env, const Type t, const loc_t pos) { - if(!tflag(t, tflag_infer)) - return GW_OK; +ANN static inline m_bool inferable(const Env env, const Type t, + const loc_t pos) { + if (!tflag(t, tflag_infer)) return GW_OK; ERR_B(pos, _("can't infer type.")) } -ANN Type check_exp_decl(const Env env, const Exp_Decl* decl) { - if(decl->td->array && decl->td->array->exp) +ANN Type check_exp_decl(const Env env, const Exp_Decl *decl) { + if (decl->td->array && decl->td->array->exp) CHECK_OO(check_exp(env, decl->td->array->exp)); - if(decl->td->xid == insert_symbol("auto")) { // should be better + if (decl->td->xid == insert_symbol("auto")) { // should be better CHECK_BO(scan1_exp(env, exp_self(decl))); CHECK_BO(scan2_exp(env, exp_self(decl))); } - if(!decl->type) - ERR_O(decl->td->pos, _("can't find type")); + if (!decl->type) ERR_O(decl->td->pos, _("can't find type")); { CHECK_BO(inferable(env, decl->type, decl->td->pos)); CHECK_BO(ensure_check(env, decl->type)); } const m_bool global = GET_FLAG(decl->td, global); - const m_uint scope = !global ? env->scope->depth : env_push_global(env); - const m_bool ret = check_decl(env, decl); - if(global) - env_pop(env, scope); + const m_uint scope = !global ? env->scope->depth : env_push_global(env); + const m_bool ret = check_decl(env, decl); + if (global) env_pop(env, scope); return ret > 0 ? decl->list->self->value->type : NULL; } ANN static m_bool prim_array_inner(const Env env, Type type, const Exp e, - const loc_t loc) { + const loc_t loc) { const Type common = find_common_anc(e->type, type); - if(common) - return GW_OK; - if(check_implicit(env, e, type) > 0) - return GW_OK; + if (common) return GW_OK; + if (check_implicit(env, e, type) > 0) return GW_OK; char fst[20 + strlen(type->name)]; sprintf(fst, "expected `{+/}%s{0}`", type->name); - gwerr_basic(_("literal contains incompatible types"), fst, NULL, - env->name, loc, 0); + gwerr_basic(_("literal contains incompatible types"), fst, NULL, env->name, + loc, 0); // suggested fix: rewrite int 2 as float 2.0" char sec[12 + strlen(e->type->name)]; sprintf(sec, "got `{+/}%s{0}`", e->type->name); @@ -189,34 +190,31 @@ ANN static m_bool prim_array_inner(const Env env, Type type, const Exp e, } ANN static inline Type prim_array_match(const Env env, Exp e) { - const Type type = e->type; - bool err = false; - const loc_t loc = e->pos; - do if(prim_array_inner(env, type, e, loc) < 0) - err = true; - while((e = e->next)); - if(!err) - return array_type(env, array_base(type), type->array_depth + 1); + const Type type = e->type; + bool err = false; + const loc_t loc = e->pos; + do + if (prim_array_inner(env, type, e, loc) < 0) err = true; + while ((e = e->next)); + if (!err) return array_type(env, array_base(type), type->array_depth + 1); env->context->error = true; return NULL; } ANN static Type check_prim_array(const Env env, const Array_Sub *data) { const Array_Sub array = *data; - const Exp e = array->exp; - if(!e) + const Exp e = array->exp; + if (!e) ERR_O(prim_pos(data), _("must provide values/expressions for array [...]")) CHECK_OO(check_exp(env, e)); return array->type = prim_array_match(env, e); } ANN static m_bool check_range(const Env env, Range *range) { - if(range->start) - CHECK_OB(check_exp(env, range->start)); - if(range->end) - CHECK_OB(check_exp(env, range->end)); - if(range->start && range->end) { - if(isa(range->end->type, range->start->type) < 0) + if (range->start) CHECK_OB(check_exp(env, range->start)); + if (range->end) CHECK_OB(check_exp(env, range->end)); + if (range->start && range->end) { + if (isa(range->end->type, range->start->type) < 0) ERR_B(range->start->pos, _("range types do not match")) } return GW_OK; @@ -225,55 +223,63 @@ ANN static m_bool check_range(const Env env, Range *range) { ANN static Type check_prim_range(const Env env, Range **data) { Range *range = *data; CHECK_BO(check_range(env, range)); - const Exp e = range->start ?: range->end; - const Symbol sym = insert_symbol("@range"); - struct Op_Import opi = { .op=sym, .rhs=e->type, .pos=e->pos, .data=(uintptr_t)prim_exp(data) }; + const Exp e = range->start ?: range->end; + const Symbol sym = insert_symbol("@range"); + struct Op_Import opi = {.op = sym, + .rhs = e->type, + .pos = e->pos, + .data = (uintptr_t)prim_exp(data)}; return op_check(env, &opi); } -ANN m_bool not_from_owner_class(const Env env, const Type t, - const Value v, const loc_t pos) { - if(!v->from->owner_class || isa(t, v->from->owner_class) < 0) { - ERR_B(pos, - _("'%s' from owner namespace '%s' used in '%s'."), - v->name, v->from->owner ? v->from->owner->name : "?", t->name) +ANN m_bool not_from_owner_class(const Env env, const Type t, const Value v, + const loc_t pos) { + if (!v->from->owner_class || isa(t, v->from->owner_class) < 0) { + ERR_B(pos, _("'%s' from owner namespace '%s' used in '%s'."), v->name, + v->from->owner ? v->from->owner->name : "?", t->name) } return GW_OK; } ANN static inline Value get_value(const Env env, const Symbol sym) { const Value value = nspc_lookup_value1(env->curr, sym); - if(value) - return value; - if(env->func && env->func->def->base->values) - return (Value)scope_lookup1(env->func->def->base->values, (vtype)sym); + if (value) return value; + if (env->func && env->func->def->base->values) + return (Value)scope_lookup1(env->func->def->base->values, (vtype)sym); return NULL; } ANN static Value check_non_res_value(const Env env, const Symbol *data) { - const Symbol var = *data; - const Value value = get_value(env, var); - if(env->class_def) { - if(value && value->from->owner_class) - CHECK_BO(not_from_owner_class(env, env->class_def, value, prim_pos(data))); + const Symbol var = *data; + const Value value = get_value(env, var); + if (env->class_def) { + if (value && value->from->owner_class) + CHECK_BO( + not_from_owner_class(env, env->class_def, value, prim_pos(data))); const Value v = value ?: find_value(env->class_def, var); - if(v) { - if(env->func && GET_FLAG(env->func->def->base, static) && vflag(v, vflag_member)) + if (v) { + if (env->func && GET_FLAG(env->func->def->base, static) && + vflag(v, vflag_member)) ERR_O(prim_pos(data), - _("non-static member '%s' used from static function."), s_name(var)) + _("non-static member '%s' used from static function."), + s_name(var)) } return v; - } else if(SAFE_FLAG(env->class_def, global) || (env->func && GET_FLAG(env->func->def->base, global))) { - if(!value || !GET_FLAG(value, global)) + } else if (SAFE_FLAG(env->class_def, global) || + (env->func && GET_FLAG(env->func->def->base, global))) { + if (!value || !GET_FLAG(value, global)) ERR_O(prim_pos(data), - _("non-global variable '%s' used from global function/class."), s_name(var)) + _("non-global variable '%s' used from global function/class."), + s_name(var)) } return value; } ANN static Type check_dot(const Env env, const Exp_Dot *member) { - struct Op_Import opi = { .op=insert_symbol("@dot"), .lhs=member->base->type, .data=(uintptr_t)member, - .pos=exp_self(member)->pos }; + struct Op_Import opi = {.op = insert_symbol("@dot"), + .lhs = member->base->type, + .data = (uintptr_t)member, + .pos = exp_self(member)->pos}; return op_check(env, &opi); } @@ -283,70 +289,71 @@ static inline Nspc value_owner(const Env env, const Value v) { ANN static void check_upvalue(const Env env, const Exp_Primary *prim) { const Value v = prim->value; - if(GET_FLAG(v, global) || (v->from->owner_class && isa(v->from->owner_class, env->class_def) > 0) || + if (GET_FLAG(v, global) || + (v->from->owner_class && isa(v->from->owner_class, env->class_def) > 0) || nspc_lookup_value1(env->curr, insert_symbol(v->name))) return; const Map map = &env->func->upvalues; - if(!map->ptr) { + if (!map->ptr) { map_init(map); map_set(&env->func->upvalues, (vtype)prim, 0); } else { - if(map_get(map, (vtype)v))return; - const m_uint offset = VVAL(map, VLEN(map) -1) + ((Exp_Primary*)VKEY(map, VLEN(map) - 1))->value->type->size; + if (map_get(map, (vtype)v)) return; + const m_uint offset = + VVAL(map, VLEN(map) - 1) + + ((Exp_Primary *)VKEY(map, VLEN(map) - 1))->value->type->size; map_set(&env->func->upvalues, (vtype)prim, offset); } } ANN static Type prim_owned(const Env env, const Symbol *data) { - const Exp exp = exp_self(prim_exp(data)); - const Value v = exp->d.prim.value; + const Exp exp = exp_self(prim_exp(data)); + const Value v = exp->d.prim.value; const m_str name = !GET_FLAG(v, static) ? "this" : v->from->owner_class->name; - const Exp base = new_prim_id(env->gwion->mp, insert_symbol(name), prim_pos(data)); + const Exp base = + new_prim_id(env->gwion->mp, insert_symbol(name), prim_pos(data)); exp_setuse(base, 1); - exp->exp_type = ae_exp_dot; + exp->exp_type = ae_exp_dot; exp->d.exp_dot.base = base; - exp->d.exp_dot.xid = *data; + exp->d.exp_dot.xid = *data; return check_exp(env, exp); } ANN static Type prim_id_non_res(const Env env, const Symbol *data) { const Symbol sym = *data; - const Value v = check_non_res_value(env, data); - if(!v || !vflag(v, vflag_valid) || (v->from->ctx && v->from->ctx->error)) { + const Value v = check_non_res_value(env, data); + if (!v || !vflag(v, vflag_valid) || (v->from->ctx && v->from->ctx->error)) { const m_str name = s_name(*data); - if(!isalpha(*name) && *name != '_') /* && *name != '@' ???*/ + if (!isalpha(*name) && *name != '_') /* && *name != '@' ???*/ return env->gwion->type[et_op]; gwerr_basic(_("Invalid variable"), _("not legit at this point."), NULL, - env->name, prim_pos(data), 0); + env->name, prim_pos(data), 0); did_you_mean_nspc(v ? value_owner(env, v) : env->curr, s_name(sym)); env->context->error = true; return NULL; } prim_self(data)->value = v; - if(v->from->owner_class) - return prim_owned(env, data); - if(GET_FLAG(v, const)) - exp_setmeta(prim_exp(data), 1); - if(env->func) { - if(!GET_FLAG(v, const) && v->from->owner) + if (v->from->owner_class) return prim_owned(env, data); + if (GET_FLAG(v, const)) exp_setmeta(prim_exp(data), 1); + if (env->func) { + if (!GET_FLAG(v, const) && v->from->owner) unset_fflag(env->func, fflag_pure); - if(fbflag(env->func->def->base, fbflag_lambda)) + if (fbflag(env->func->def->base, fbflag_lambda)) check_upvalue(env, prim_self(data)); } - //set_vflag(v->vflag, vflag_used); + // set_vflag(v->vflag, vflag_used); return v->type; } ANN Type check_prim_str(const Env env, const m_str *data) { - if(!prim_self(data)->value) + if (!prim_self(data)->value) prim_self(data)->value = global_string(env, *data); - return env->gwion->type[et_string];// prim->value + return env->gwion->type[et_string]; // prim->value } ANN static Type check_prim_id(const Env env, const Symbol *data) { - struct SpecialId_ * spid = specialid_get(env->gwion, *data); - if(spid) - return specialid_type(env, spid, prim_self(data)); + struct SpecialId_ *spid = specialid_get(env->gwion, *data); + if (spid) return specialid_type(env, spid, prim_self(data)); return prim_id_non_res(env, data); } @@ -355,14 +362,13 @@ ANN static Type check_prim_perform(const Env env, const Symbol *data) { return env->gwion->type[et_void]; } -ANN static Type check_prim_interp(const Env env, const Exp* exp) { +ANN static Type check_prim_interp(const Env env, const Exp *exp) { CHECK_OO(check_exp(env, *exp)); return env->gwion->type[et_string]; } ANN static Type check_prim_hack(const Env env, const Exp *data) { - if(env->func) - unset_fflag(env->func, fflag_pure); + if (env->func) unset_fflag(env->func, fflag_pure); CHECK_OO(check_prim_interp(env, data)); return env->gwion->type[et_gack]; } @@ -402,155 +408,164 @@ ANN static Type check_prim_map(const Env env, const Exp *data) { return NULL; } */ -#define describe_prim_xxx(name, type) \ -ANN static Type check_prim_##name(const Env env NUSED, const union prim_data* data NUSED) {\ - return type; \ -} +#define describe_prim_xxx(name, type) \ + ANN static Type check_prim_##name(const Env env NUSED, \ + const union prim_data *data NUSED) { \ + return type; \ + } describe_prim_xxx(num, env->gwion->type[et_int]) -describe_prim_xxx(char, env->gwion->type[et_char]) -describe_prim_xxx(float, env->gwion->type[et_float]) -describe_prim_xxx(nil, env->gwion->type[et_void]) + describe_prim_xxx(char, env->gwion->type[et_char]) + describe_prim_xxx(float, env->gwion->type[et_float]) + describe_prim_xxx(nil, env->gwion->type[et_void]) -DECL_PRIM_FUNC(check, Type, Env); + DECL_PRIM_FUNC(check, Type, Env); ANN static Type check_prim(const Env env, Exp_Primary *prim) { return exp_self(prim)->type = check_prim_func[prim->prim_type](env, &prim->d); } ANN Type check_array_access(const Env env, const Array_Sub array) { - const Symbol sym = insert_symbol("@array"); - struct Op_Import opi = { .op=sym, .lhs=array->exp->type, .rhs=array->type, - .pos=array->exp->pos, .data=(uintptr_t)array }; + const Symbol sym = insert_symbol("@array"); + struct Op_Import opi = {.op = sym, + .lhs = array->exp->type, + .rhs = array->type, + .pos = array->exp->pos, + .data = (uintptr_t)array}; return op_check(env, &opi); } -static ANN Type check_exp_array(const Env env, const Exp_Array* array) { +static ANN Type check_exp_array(const Env env, const Exp_Array *array) { CHECK_OO((array->array->type = check_exp(env, array->base))); CHECK_BO(check_subscripts(env, array->array, 0)); return check_array_access(env, array->array); } -static ANN Type check_exp_slice(const Env env, const Exp_Slice* range) { +static ANN Type check_exp_slice(const Env env, const Exp_Slice *range) { CHECK_OO(check_exp(env, range->base)); CHECK_BO(check_range(env, range->range)); - const Symbol sym = insert_symbol("@slice"); - const Exp e = range->range->start ?: range->range->end; - struct Op_Import opi = { .op=sym, .lhs=e->type, .rhs=range->base->type, - .pos=e->pos, .data=(uintptr_t)exp_self(range) }; + const Symbol sym = insert_symbol("@slice"); + const Exp e = range->range->start ?: range->range->end; + struct Op_Import opi = {.op = sym, + .lhs = e->type, + .rhs = range->base->type, + .pos = e->pos, + .data = (uintptr_t)exp_self(range)}; return op_check(env, &opi); } -ANN static Type_List mk_type_list(const Env env, const Arg_List arg, const Type type, const loc_t pos) { - const Type t = !arg->td->array ? - type : array_type(env, type, arg->td->array->depth); +ANN static Type_List mk_type_list(const Env env, const Arg_List arg, + const Type type, const loc_t pos) { + const Type t = + !arg->td->array ? type : array_type(env, type, arg->td->array->depth); Type_Decl *td = type2td(env->gwion, t, pos); return new_type_list(env->gwion->mp, td, NULL); } ANN static m_bool func_match_inner(const Env env, const Exp e, const Type t, - const m_bool implicit, const m_bool specific) { + const m_bool implicit, + const m_bool specific) { const m_bool match = (specific ? e->type == t : isa(e->type, t) > 0); - if(!match) { - if(e->type == env->gwion->type[et_lambda] && is_fptr(env->gwion, t)) { + if (!match) { + if (e->type == env->gwion->type[et_lambda] && is_fptr(env->gwion, t)) { exp_setvar(e, 1); return check_lambda(env, t, &e->d.exp_lambda); } - if(implicit) { + if (implicit) { const m_bool ret = check_implicit(env, e, t); - if(ret == GW_OK) - return ret; + if (ret == GW_OK) return ret; } } return match ? 1 : -1; } -ANN2(1,2) static Func find_func_match_actual(const Env env, Func func, const Exp args, - const m_bool implicit, const m_bool specific) { +ANN2(1, 2) +static Func find_func_match_actual(const Env env, Func func, const Exp args, + const m_bool implicit, + const m_bool specific) { do { - Exp e = args; + Exp e = args; Arg_List e1 = func->def->base->args; - while(e) { - if(!strncmp(e->type->name, "Ref:[", 5)) - exp_setvar(e, true); - if(!e1) { - if(fbflag(func->def->base, fbflag_variadic)) - return func; + while (e) { + if (!strncmp(e->type->name, "Ref:[", 5)) exp_setvar(e, true); + if (!e1) { + if (fbflag(func->def->base, fbflag_variadic)) return func; CHECK_OO(func->next); - return find_func_match_actual(env, func->next, args, implicit, specific); - } //else if(!e->type) //fix bug found with Cytosol - //return NULL; - if(e1->type == env->gwion->type[et_auto] || - (func->def->base->tmpl && is_fptr(env->gwion, func->value_ref->type) > 0)) { + return find_func_match_actual(env, func->next, args, implicit, + specific); + } // else if(!e->type) //fix bug found with Cytosol + // return NULL; + if (e1->type == env->gwion->type[et_auto] || + (func->def->base->tmpl && + is_fptr(env->gwion, func->value_ref->type) > 0)) { const Type owner = func->value_ref->from->owner_class; - if(owner) - CHECK_BO(template_push(env, owner)); + if (owner) CHECK_BO(template_push(env, owner)); e1->type = known_type(env, e1->td); - if(owner) - nspc_pop_type(env->gwion->mp, env->curr); + if (owner) nspc_pop_type(env->gwion->mp, env->curr); CHECK_OO(e1->type); } -// if(!func->def->base->tmpl && func->next) -// env->context->error = 1; + // if(!func->def->base->tmpl && func->next) + // env->context->error = 1; const m_bool ret = func_match_inner(env, e, e1->type, implicit, specific); -// if(func->next) -// env->context->error = ret < 0; - if(ret < 0) - break; - e = e->next; + // if(func->next) + // env->context->error = ret < 0; + if (ret < 0) break; + e = e->next; e1 = e1->next; } - if(!e1) - return func; - } while((func = func->next)); + if (!e1) return func; + } while ((func = func->next)); return NULL; } -ANN static Type check_exp_call(const Env env, Exp_Call* exp); +ANN static Type check_exp_call(const Env env, Exp_Call *exp); -ANN static Func call2ufcs(const Env env, Exp_Call* call, const Value v) { - const Exp this = call->func->d.exp_dot.base; - this->next = call->args; - call->args = this; - call->func->type = v->type; - call->func->d.prim.value = v; - call->func->d.prim.d.var = call->func->d.exp_dot.xid; - call->func->exp_type = ae_exp_primary; +ANN static Func call2ufcs(const Env env, Exp_Call *call, const Value v) { + const Exp this = call->func->d.exp_dot.base; + this->next = call->args; + call->args = this; + call->func->type = v->type; + call->func->d.prim.value = v; + call->func->d.prim.d.var = call->func->d.exp_dot.xid; + call->func->exp_type = ae_exp_primary; call->func->d.prim.prim_type = ae_prim_id; - call->args = this; + call->args = this; CHECK_OO(check_exp_call(env, call)); return call->func->type->info->func; } ANN Func ufcs(const Env env, const Func up, Exp_Call *const call) { const Value v = nspc_lookup_value1(env->curr, up->def->base->xid); - if(v && isa(v->type, env->gwion->type[et_function]) > 0 && !vflag(v, vflag_member)) + if (v && isa(v->type, env->gwion->type[et_function]) > 0 && + !vflag(v, vflag_member)) return call2ufcs(env, call, v); return NULL; } ANN Func find_func_match(const Env env, const Func up, Exp_Call *const call) { - Func func; + Func func; const Exp exp = call->args; - const Exp args = (exp && isa(exp->type, env->gwion->type[et_void]) < 0) ? exp : NULL; - if((func = find_func_match_actual(env, up, args, 0, 1)) || - (func = find_func_match_actual(env, up, args, 1, 1)) || - (func = find_func_match_actual(env, up, args, 0, 0)) || - (func = find_func_match_actual(env, up, args, 1, 0))) + const Exp args = + (exp && isa(exp->type, env->gwion->type[et_void]) < 0) ? exp : NULL; + if ((func = find_func_match_actual(env, up, args, 0, 1)) || + (func = find_func_match_actual(env, up, args, 1, 1)) || + (func = find_func_match_actual(env, up, args, 0, 0)) || + (func = find_func_match_actual(env, up, args, 1, 0))) return func; - return call->func->exp_type == ae_exp_dot && up->value_ref->from->owner_class ? - ufcs(env, up, call) : NULL; + return call->func->exp_type == ae_exp_dot && up->value_ref->from->owner_class + ? ufcs(env, up, call) + : NULL; } ANN m_bool check_traverse_fdef(const Env env, const Func_Def fdef) { - struct Vector_ v = {}; - const m_uint scope = env->scope->depth; - env->scope->depth = 0; + struct Vector_ v = {}; + const m_uint scope = env->scope->depth; + env->scope->depth = 0; vector_init(&v); - while(vector_size((Vector)&env->curr->info->value->ptr) > 1) + while (vector_size((Vector)&env->curr->info->value->ptr) > 1) vector_add(&v, vector_pop((Vector)&env->curr->info->value->ptr)); const m_bool ret = traverse_func_def(env, fdef); - for(m_uint i = vector_size(&v) + 1; --i;) - vector_add((Vector)&env->curr->info->value->ptr, vector_at(&v, i-1)); + for (m_uint i = vector_size(&v) + 1; --i;) + vector_add((Vector)&env->curr->info->value->ptr, vector_at(&v, i - 1)); vector_release(&v); env->scope->depth = scope; return ret; @@ -559,53 +574,54 @@ ANN m_bool check_traverse_fdef(const Env env, const Func_Def fdef) { ANN static m_bool check_func_args(const Env env, Arg_List arg_list) { do { const Var_Decl decl = arg_list->var_decl; - const Value v = decl->value; + const Value v = decl->value; CHECK_BB(already_defined(env, decl->xid, decl->pos)); set_vflag(v, vflag_valid); nspc_add_value(env->curr, decl->xid, v); - } while((arg_list = arg_list->next)); + } while ((arg_list = arg_list->next)); return GW_OK; } -#define next_arg(type) \ -ANN static inline type next_arg_##type(const type e) { \ - const type next = e->next; \ - if(next) \ - gw_err(", "); \ - return next; \ -} -next_arg(Exp) -next_arg(Arg_List) +#define next_arg(type) \ + ANN static inline type next_arg_##type(const type e) { \ + const type next = e->next; \ + if (next) gw_err(", "); \ + return next; \ + } +next_arg(Exp) next_arg(Arg_List) -ANN static void print_current_args(Exp e) { + ANN static void print_current_args(Exp e) { gw_err(_("and not\n ")); do gw_err(" {G}%s{0}", e->type ? e->type->name : "{-/}Unknown"); - while((e = next_arg_Exp(e))); + while ((e = next_arg_Exp(e))); gw_err("\n"); } ANN static void print_arg(Arg_List e) { - do gw_err("{G}%s{0} {/}%s{0}", e->type ? e->type->name : NULL, - e->var_decl->xid ? s_name(e->var_decl->xid) : ""); - while((e = next_arg_Arg_List(e))); + do + gw_err("{G}%s{0} {/}%s{0}", e->type ? e->type->name : NULL, + e->var_decl->xid ? s_name(e->var_decl->xid) : ""); + while ((e = next_arg_Arg_List(e))); } -ANN2(1) static void function_alternative(const Env env, const Type f, const Exp args, const loc_t pos){ - if(env->context && env->context->error) // needed for ufcs +ANN2(1) +static void function_alternative(const Env env, const Type f, const Exp args, + const loc_t pos) { + if (env->context && env->context->error) // needed for ufcs return; - gwerr_basic("Argument type mismatch", "call site", "valid alternatives:", - env->name, pos, 0); + gwerr_basic("Argument type mismatch", "call site", + "valid alternatives:", env->name, pos, 0); Func up = f->info->func; do { gw_err(" {-}(%s){0} ", up->name); const Arg_List e = up->def->base->args; - if(e) + if (e) print_arg(e); else gw_err("{G}void{0}"); gw_err("\n"); - } while((up = up->next)); - if(args) + } while ((up = up->next)); + if (args) print_current_args(args); else gw_err(_("and not:\n {G}void{0}\n")); @@ -614,74 +630,83 @@ ANN2(1) static void function_alternative(const Env env, const Type f, const Exp ANN static m_uint get_type_number(Specialized_List list) { m_uint type_number = 0; do ++type_number; - while((list = list->next)); + while ((list = list->next)); return type_number; } -ANN static Func get_template_func(const Env env, Exp_Call *const func, const Value v) { +ANN static Func get_template_func(const Env env, Exp_Call *const func, + const Value v) { const Func f = find_template_match(env, v, func); - if(f) { -// copy that tmpl->call? - Tmpl* tmpl = new_tmpl_call(env->gwion->mp, func->tmpl->call); - tmpl->list = v->d.func_ref ? v->d.func_ref->def->base->tmpl->list : func->func->type->info->func->def->base->tmpl->list; - ((Exp_Call*)func)->tmpl = tmpl; - func->func->type = f->value_ref->type; + if (f) { + // copy that tmpl->call? + Tmpl *tmpl = new_tmpl_call(env->gwion->mp, func->tmpl->call); + tmpl->list = v->d.func_ref + ? v->d.func_ref->def->base->tmpl->list + : func->func->type->info->func->def->base->tmpl->list; + ((Exp_Call *)func)->tmpl = tmpl; + func->func->type = f->value_ref->type; return f; } - ((Exp_Call*)func)->tmpl = NULL; + ((Exp_Call *)func)->tmpl = NULL; assert(exp_self(func)); ERR_O(exp_self(func)->pos, - _("function is template. automatic type guess not fully implemented yet.\n" - " please provide template types. eg: ':[type1, type2, ...]'")) + _("function is template. automatic type guess not fully implemented " + "yet.\n" + " please provide template types. eg: ':[type1, type2, ...]'")) } -ANN static Func predefined_func(const Env env, const Value v, - Exp_Call *exp, const Tmpl *tm) { - Tmpl tmpl = { .call=tm->call }; +ANN static Func predefined_func(const Env env, const Value v, Exp_Call *exp, + const Tmpl *tm) { + Tmpl tmpl = {.call = tm->call}; exp->tmpl = &tmpl; DECL_OO(const Func, func, = get_template_func(env, exp, v)); return v->d.func_ref = func; } -ANN static Type check_predefined(const Env env, Exp_Call *exp, const Value v, const Tmpl *tm, const Func_Def fdef) { - DECL_OO(const Func, func, = v->d.func_ref ?: predefined_func(env, v, exp, tm)); - if(!fdef->base->ret_type) { // template fptr - struct EnvSet es = { .env=env, .data=env, .func=(_exp_func)check_cdef, - .scope=env->scope->depth, .flag=tflag_check }; +ANN static Type check_predefined(const Env env, Exp_Call *exp, const Value v, + const Tmpl *tm, const Func_Def fdef) { + DECL_OO(const Func, func, + = v->d.func_ref ?: predefined_func(env, v, exp, tm)); + if (!fdef->base->ret_type) { // template fptr + struct EnvSet es = {.env = env, + .data = env, + .func = (_exp_func)check_cdef, + .scope = env->scope->depth, + .flag = tflag_check}; CHECK_BO(envset_pushv(&es, v)); func->def->base->fbflag |= fbflag_internal; const m_bool ret = check_traverse_fdef(env, func->def); - if(es.run) - envset_pop(&es, v->from->owner_class); + if (es.run) envset_pop(&es, v->from->owner_class); CHECK_BO(ret); } exp->func->type = func->value_ref->type; return func->def->base->ret_type; } -ANN static Type_List check_template_args(const Env env, Exp_Call *exp, const Tmpl *tm, const Func_Def fdef) { - m_uint args_number = 0; +ANN static Type_List check_template_args(const Env env, Exp_Call *exp, + const Tmpl *tm, const Func_Def fdef) { + m_uint args_number = 0; const m_uint type_number = get_type_number(tm->list); - Type_List tl[type_number]; - tl[0] = NULL; + Type_List tl[type_number]; + tl[0] = NULL; Specialized_List list = tm->list; - while(list) { - Arg_List arg = fdef->base->args; - Exp template_arg = exp->args; - while(arg && template_arg) { - if(list->xid == arg->td->xid) { - tl[args_number] = mk_type_list(env, arg, template_arg->type, fdef->base->pos); - if(args_number) - tl[args_number - 1]->next = tl[args_number]; + while (list) { + Arg_List arg = fdef->base->args; + Exp template_arg = exp->args; + while (arg && template_arg) { + if (list->xid == arg->td->xid) { + tl[args_number] = + mk_type_list(env, arg, template_arg->type, fdef->base->pos); + if (args_number) tl[args_number - 1]->next = tl[args_number]; ++args_number; break; } - arg = arg->next; + arg = arg->next; template_arg = template_arg->next; } list = list->next; } - if(args_number < type_number) + if (args_number < type_number) ERR_O(exp->func->pos, _("not able to guess types for template call.")) return tl[0]; } @@ -689,114 +714,112 @@ ANN static Type_List check_template_args(const Env env, Exp_Call *exp, const Tmp ANN static Type check_exp_call_template(const Env env, Exp_Call *exp) { const Type t = exp->func->type; DECL_OO(const Value, value, = type_value(env->gwion, t)); - const Func_Def fdef = value->d.func_ref ? value->d.func_ref->def : t->info->func->def; + const Func_Def fdef = + value->d.func_ref ? value->d.func_ref->def : t->info->func->def; Tmpl *tm = fdef->base->tmpl; - if(tm->call) - return check_predefined(env, exp, value, tm, fdef); - DECL_OO(const Type_List, tl, = check_template_args(env, exp, tm, fdef));; - Tmpl tmpl = { .call=tl }; - ((Exp_Call*)exp)->tmpl = &tmpl; - DECL_OO(const Func,func, = get_template_func(env, exp, value)); + if (tm->call) return check_predefined(env, exp, value, tm, fdef); + DECL_OO(const Type_List, tl, = check_template_args(env, exp, tm, fdef)); + ; + Tmpl tmpl = {.call = tl}; + ((Exp_Call *)exp)->tmpl = &tmpl; + DECL_OO(const Func, func, = get_template_func(env, exp, value)); return func->def->base->ret_type; } ANN static Type check_lambda_call(const Env env, const Exp_Call *exp) { - if(exp->args) - CHECK_OO(check_exp(env, exp->args)); - Exp_Lambda *l = &exp->func->d.exp_lambda; - Arg_List arg = l->def->base->args; - Exp e = exp->args; - while(arg && e) { + if (exp->args) CHECK_OO(check_exp(env, exp->args)); + Exp_Lambda *l = &exp->func->d.exp_lambda; + Arg_List arg = l->def->base->args; + Exp e = exp->args; + while (arg && e) { arg->type = e->type; - arg = arg->next; - e = e->next; + arg = arg->next; + e = e->next; } - if(arg || e) + if (arg || e) ERR_O(exp_self(exp)->pos, _("argument number does not match for lambda")) l->def->base->values = env->curr->info->value; - const m_bool ret = traverse_func_def(env, l->def); - if(l->def->base->func) { + const m_bool ret = traverse_func_def(env, l->def); + if (l->def->base->func) { free_scope(env->gwion->mp, env->curr->info->value); env->curr->info->value = l->def->base->values; - if(env->class_def) - set_vflag(l->def->base->func->value_ref, vflag_member); + if (env->class_def) set_vflag(l->def->base->func->value_ref, vflag_member); } exp->func->type = l->def->base->func->value_ref->type; - if(!l->def->base->ret_type) + if (!l->def->base->ret_type) l->def->base->ret_type = env->gwion->type[et_void]; return ret > 0 ? l->def->base->ret_type : NULL; } ANN m_bool func_check(const Env env, Exp_Call *const exp) { CHECK_OB(check_exp(env, exp->func)); - if(exp->func->exp_type == ae_exp_decl) - ERR_B(exp->func->pos, _("Can't call late function pointer at declaration site. did you meant to use `@=>`?")) + if (exp->func->exp_type == ae_exp_decl) + ERR_B(exp->func->pos, _("Can't call late function pointer at declaration " + "site. did you meant to use `@=>`?")) const Type t = actual_type(env->gwion, exp->func->type); - if(isa(t, env->gwion->type[et_function]) > 0 && - exp->func->exp_type == ae_exp_dot && !t->info->value->from->owner_class) { - if(exp->args) - CHECK_OB(check_exp(env, exp->args)); - return call2ufcs(env, exp, t->info->func->value_ref) ? - GW_OK: GW_ERROR; + if (isa(t, env->gwion->type[et_function]) > 0 && + exp->func->exp_type == ae_exp_dot && !t->info->value->from->owner_class) { + if (exp->args) CHECK_OB(check_exp(env, exp->args)); + return call2ufcs(env, exp, t->info->func->value_ref) ? GW_OK : GW_ERROR; } - const Exp e = exp_self(exp); - struct Op_Import opi = { .op=insert_symbol("@func_check"), - .rhs=t, .pos=e->pos, .data=(uintptr_t)e }; + const Exp e = exp_self(exp); + struct Op_Import opi = {.op = insert_symbol("@func_check"), + .rhs = t, + .pos = e->pos, + .data = (uintptr_t)e}; CHECK_NB(op_check(env, &opi)); // doesn't really return NULL - if(e->exp_type != ae_exp_call) - return 0; - return e->type != env->gwion->type[et_error] ? - GW_OK : GW_ERROR; + if (e->exp_type != ae_exp_call) return 0; + return e->type != env->gwion->type[et_error] ? GW_OK : GW_ERROR; } ANN void call_add_effect(const Env env, const Func func, const loc_t pos) { - if(func != env->func && func->def->base->effects.ptr) { + if (func != env->func && func->def->base->effects.ptr) { const Vector v = &func->def->base->effects; - for(m_uint i = 0; i < vector_size(v); i++) + for (m_uint i = 0; i < vector_size(v); i++) env_add_effect(env, (Symbol)vector_at(v, i), pos); } } ANN Type check_exp_call1(const Env env, Exp_Call *const exp) { DECL_BO(const m_bool, ret, = func_check(env, exp)); - if(!ret) - return exp_self(exp)->type; + if (!ret) return exp_self(exp)->type; const Type t = actual_type(env->gwion, exp->func->type); - if(isa(t, env->gwion->type[et_function]) < 0) { // use func flag? - struct Op_Import opi = { .op=insert_symbol("@ctor"), .rhs=actual_type(env->gwion, exp->func->type), - .data=(uintptr_t)exp, .pos=exp_self(exp)->pos }; - const Type t = op_check(env, &opi); + if (isa(t, env->gwion->type[et_function]) < 0) { // use func flag? + struct Op_Import opi = {.op = insert_symbol("@ctor"), + .rhs = actual_type(env->gwion, exp->func->type), + .data = (uintptr_t)exp, + .pos = exp_self(exp)->pos}; + const Type t = op_check(env, &opi); return t; } - if(t == env->gwion->type[et_op]) - return check_op_call(env, exp); - if(t == env->gwion->type[et_lambda]) // TODO: effects? + if (t == env->gwion->type[et_op]) return check_op_call(env, exp); + if (t == env->gwion->type[et_lambda]) // TODO: effects? return check_lambda_call(env, exp); - if(fflag(t->info->func, fflag_ftmpl)) { + if (fflag(t->info->func, fflag_ftmpl)) { const Value value = t->info->func->value_ref; - if(value->from->owner_class) + if (value->from->owner_class) CHECK_BO(ensure_traverse(env, value->from->owner_class)); } - if(exp->args) - CHECK_OO(check_exp(env, exp->args)); - if(tflag(t, tflag_ftmpl)) - return check_exp_call_template(env, (Exp_Call*)exp); // TODO: effects? + if (exp->args) CHECK_OO(check_exp(env, exp->args)); + if (tflag(t, tflag_ftmpl)) + return check_exp_call_template(env, (Exp_Call *)exp); // TODO: effects? const Func func = find_func_match(env, t->info->func, exp); - if(func) { - if(!is_fptr(env->gwion, func->value_ref->type))// skip function pointers - if(func != env->func && func->def && !fflag(func, fflag_valid)) { -// if(!fflag(func, fflag_valid)) { - struct EnvSet es = { .env=env, .data=env, .func=(_exp_func)check_cdef, - .scope=env->scope->depth, .flag=tflag_check }; - CHECK_BO(envset_pushv(&es, func->value_ref)); - CHECK_BO(check_func_def(env, func->def)); - if(es.run) - envset_pop(&es, func->value_ref->from->owner_class); - } + if (func) { + if (!is_fptr(env->gwion, func->value_ref->type)) // skip function pointers + if (func != env->func && func->def && !fflag(func, fflag_valid)) { + // if(!fflag(func, fflag_valid)) { + struct EnvSet es = {.env = env, + .data = env, + .func = (_exp_func)check_cdef, + .scope = env->scope->depth, + .flag = tflag_check}; + CHECK_BO(envset_pushv(&es, func->value_ref)); + CHECK_BO(check_func_def(env, func->def)); + if (es.run) envset_pop(&es, func->value_ref->from->owner_class); + } exp->func->type = func->value_ref->type; call_add_effect(env, func, exp->func->pos); - if(func == env->func) - set_fflag(env->func, fflag_recurs); + if (func == env->func) set_fflag(env->func, fflag_recurs); return func->def->base->ret_type; } const loc_t pos = exp->args ? exp->args->pos : exp->func->pos; @@ -804,69 +827,76 @@ ANN Type check_exp_call1(const Env env, Exp_Call *const exp) { return NULL; } -ANN static Type check_exp_binary(const Env env, const Exp_Binary* bin) { +ANN static Type check_exp_binary(const Env env, const Exp_Binary *bin) { CHECK_OO(check_exp(env, bin->lhs)); - const m_bool is_auto = bin->rhs->exp_type == ae_exp_decl && bin->rhs->d.exp_decl.type == env->gwion->type[et_auto]; - if(is_auto) - bin->rhs->d.exp_decl.type = bin->lhs->type; + const m_bool is_auto = bin->rhs->exp_type == ae_exp_decl && + bin->rhs->d.exp_decl.type == env->gwion->type[et_auto]; + if (is_auto) bin->rhs->d.exp_decl.type = bin->lhs->type; CHECK_OO(check_exp(env, bin->rhs)); - if(is_auto) - bin->rhs->type = bin->lhs->type; - struct Op_Import opi = { .op=bin->op, .lhs=bin->lhs->type, - .rhs=bin->rhs->type, .data=(uintptr_t)bin, .pos=exp_self(bin)->pos }; + if (is_auto) bin->rhs->type = bin->lhs->type; + struct Op_Import opi = {.op = bin->op, + .lhs = bin->lhs->type, + .rhs = bin->rhs->type, + .data = (uintptr_t)bin, + .pos = exp_self(bin)->pos}; exp_setuse(bin->lhs, 1); exp_setuse(bin->rhs, 1); const Type ret = op_check(env, &opi); - if(!ret && is_auto && exp_self(bin)->exp_type == ae_exp_binary) + if (!ret && is_auto && exp_self(bin)->exp_type == ae_exp_binary) bin->rhs->d.exp_decl.list->self->value->type = env->gwion->type[et_auto]; return ret; } -ANN static Type check_exp_cast(const Env env, const Exp_Cast* cast) { +ANN static Type check_exp_cast(const Env env, const Exp_Cast *cast) { DECL_OO(const Type, t, = check_exp(env, cast->exp)); CHECK_OO((exp_self(cast)->type = known_type(env, cast->td))); - struct Op_Import opi = { .op=insert_symbol("$"), .lhs=t, .rhs=exp_self(cast)->type, - .data=(uintptr_t)cast, .pos=exp_self(cast)->pos }; + struct Op_Import opi = {.op = insert_symbol("$"), + .lhs = t, + .rhs = exp_self(cast)->type, + .data = (uintptr_t)cast, + .pos = exp_self(cast)->pos}; return op_check(env, &opi); } -ANN static Type check_exp_post(const Env env, const Exp_Postfix* post) { - struct Op_Import opi = { .op=post->op, .lhs=check_exp(env, post->exp), - .data=(uintptr_t)post, .pos=exp_self(post)->pos }; +ANN static Type check_exp_post(const Env env, const Exp_Postfix *post) { + struct Op_Import opi = {.op = post->op, + .lhs = check_exp(env, post->exp), + .data = (uintptr_t)post, + .pos = exp_self(post)->pos}; CHECK_OO(opi.lhs); exp_setuse(post->exp, 1); const Type t = op_check(env, &opi); - if(t && isa(t, env->gwion->type[et_object]) < 0) + if (t && isa(t, env->gwion->type[et_object]) < 0) exp_setmeta(exp_self(post), 1); return t; } -ANN static m_bool predefined_call(const Env env, const Type t, const loc_t pos) { - const m_str str = tl2str(env->gwion, t->info->func->def->base->tmpl->call, pos); - env_err(env, pos, _("Type '%s' has '%s' as pre-defined types."), - t->name, str); +ANN static m_bool predefined_call(const Env env, const Type t, + const loc_t pos) { + const m_str str = + tl2str(env->gwion, t->info->func->def->base->tmpl->call, pos); + env_err(env, pos, _("Type '%s' has '%s' as pre-defined types."), t->name, + str); free_mstr(env->gwion->mp, str); - if(tflag(t, tflag_typedef)) { - gwerr_secondary("from definition:", - env->name, t->info->func->def->base->pos); + if (tflag(t, tflag_typedef)) { + gwerr_secondary("from definition:", env->name, + t->info->func->def->base->pos); } return GW_ERROR; } -ANN static Type check_exp_call(const Env env, Exp_Call* exp) { - if(exp->tmpl) { +ANN static Type check_exp_call(const Env env, Exp_Call *exp) { + if (exp->tmpl) { DECL_BO(const m_bool, ret, = func_check(env, exp)); - if(!ret) - return exp_self(exp)->type; + if (!ret) return exp_self(exp)->type; const Type t = actual_type(env->gwion, exp->func->type); - if(isa(t, env->gwion->type[et_function]) < 0) - return check_exp_call1(env, exp); - if(exp->args) - CHECK_OO(check_exp(env, exp->args)); - if(!t->info->func->def->base->tmpl) + if (isa(t, env->gwion->type[et_function]) < 0) + return check_exp_call1(env, exp); + if (exp->args) CHECK_OO(check_exp(env, exp->args)); + if (!t->info->func->def->base->tmpl) ERR_O(exp_self(exp)->pos, _("template call of non-template function.")) - if(t->info->func->def->base->tmpl->call) { - if(env->func == t->info->func) { + if (t->info->func->def->base->tmpl->call) { + if (env->func == t->info->func) { exp->func->type = env->func->value_ref->type; return env->func->def->base->ret_type; } else @@ -880,14 +910,17 @@ ANN static Type check_exp_call(const Env env, Exp_Call* exp) { return check_exp_call1(env, exp); } -ANN static Type check_exp_unary(const Env env, const Exp_Unary* unary) { - const Type rhs = unary->unary_type == unary_exp ? check_exp(env, unary->exp) : NULL; - if(unary->unary_type == unary_exp) { +ANN static Type check_exp_unary(const Env env, const Exp_Unary *unary) { + const Type rhs = + unary->unary_type == unary_exp ? check_exp(env, unary->exp) : NULL; + if (unary->unary_type == unary_exp) { CHECK_OO(rhs); exp_setuse(unary->exp, 1); } - struct Op_Import opi = { .op=unary->op, .rhs=rhs, - .data=(uintptr_t)unary, .pos=exp_self(unary)->pos }; + struct Op_Import opi = {.op = unary->op, + .rhs = rhs, + .data = (uintptr_t)unary, + .pos = exp_self(unary)->pos}; DECL_OO(const Type, ret, = op_check(env, &opi)); const Type t = actual_type(env->gwion, ret); CHECK_BO(ensure_traverse(env, t)); @@ -896,33 +929,36 @@ ANN static Type check_exp_unary(const Env env, const Exp_Unary* unary) { ANN static Type _flow(const Env env, const Exp e, const m_bool b) { DECL_OO(const Type, type, = check_exp(env, e)); - struct Op_Import opi = { .op=insert_symbol(b ? "@conditional" : "@unconditional"), - .rhs=type, .pos=e->pos, .data=(uintptr_t)e }; + struct Op_Import opi = { + .op = insert_symbol(b ? "@conditional" : "@unconditional"), + .rhs = type, + .pos = e->pos, + .data = (uintptr_t)e}; return op_check(env, &opi); } -#define check_flow(emit,b) _flow(emit, b, 1) +#define check_flow(emit, b) _flow(emit, b, 1) -ANN static Type check_exp_if(const Env env, const Exp_If* exp_if) { +ANN static Type check_exp_if(const Env env, const Exp_If *exp_if) { const Exp e = exp_if->if_exp ?: exp_if->cond; DECL_OO(const Type, cond, = check_flow(env, exp_if->cond)); - DECL_OO(const Type, if_exp, = (exp_if->if_exp ? check_exp(env, exp_if->if_exp) : cond)); + DECL_OO(const Type, if_exp, + = (exp_if->if_exp ? check_exp(env, exp_if->if_exp) : cond)); DECL_OO(const Type, else_exp, = check_exp(env, exp_if->else_exp)); const uint meta = exp_getmeta(e) || exp_getmeta(exp_if->else_exp); exp_setmeta(exp_self(exp_if), meta); const Type ret = find_common_anc(if_exp, else_exp); - if(!ret) + if (!ret) ERR_O(exp_self(exp_if)->pos, _("incompatible types '%s' and '%s' in if expression..."), if_exp->name, else_exp->name) - if(isa(if_exp, else_exp) < 0) - ERR_O(exp_self(exp_if)->pos, - _("condition type '%s' does not match '%s'"), - cond->name, ret->name) + if (isa(if_exp, else_exp) < 0) + ERR_O(exp_self(exp_if)->pos, _("condition type '%s' does not match '%s'"), + cond->name, ret->name) return ret; } -ANN static Type check_exp_dot(const Env env, Exp_Dot* member) { +ANN static Type check_exp_dot(const Env env, Exp_Dot *member) { CHECK_OO(check_exp(env, member->base)); return check_dot(env, member); } @@ -941,57 +977,66 @@ const Func f = exp_self(call)->type->info->func; */ ANN m_bool check_type_def(const Env env, const Type_Def tdef) { - if(tdef->when) { + if (tdef->when) { set_tflag(tdef->type, tflag_contract); - const Var_Decl decl = new_var_decl(env->gwion->mp, insert_symbol("self"), NULL, tdef->when->pos); - const Arg_List args = new_arg_list(env->gwion->mp, cpy_type_decl(env->gwion->mp, tdef->ext), decl, NULL); - Func_Base *fb = new_func_base(env->gwion->mp, type2td(env->gwion, tdef->type, tdef->pos), - insert_symbol("@implicit"), args, ae_flag_none, tdef->pos); + const Var_Decl decl = new_var_decl(env->gwion->mp, insert_symbol("self"), + NULL, tdef->when->pos); + const Arg_List args = new_arg_list( + env->gwion->mp, cpy_type_decl(env->gwion->mp, tdef->ext), decl, NULL); + Func_Base *fb = new_func_base( + env->gwion->mp, type2td(env->gwion, tdef->type, tdef->pos), + insert_symbol("@implicit"), args, ae_flag_none, tdef->pos); set_fbflag(fb, fbflag_op); - const Exp helper = new_prim_id(env->gwion->mp, insert_symbol("@predicate"), tdef->when->pos); - const Exp when = cpy_exp(env->gwion->mp, tdef->when); - when->next = helper; - const Stmt stmt = new_stmt_exp(env->gwion->mp, ae_stmt_exp, when, when->pos); - const Stmt_List body = new_stmt_list(env->gwion->mp, stmt, NULL);//ret_list); - const Stmt code = new_stmt_code(env->gwion->mp, body, when->pos); + const Exp helper = new_prim_id(env->gwion->mp, insert_symbol("@predicate"), + tdef->when->pos); + const Exp when = cpy_exp(env->gwion->mp, tdef->when); + when->next = helper; + const Stmt stmt = + new_stmt_exp(env->gwion->mp, ae_stmt_exp, when, when->pos); + const Stmt_List body = + new_stmt_list(env->gwion->mp, stmt, NULL); // ret_list); + const Stmt code = new_stmt_code(env->gwion->mp, body, when->pos); const Func_Def fdef = new_func_def(env->gwion->mp, fb, code); CHECK_BB(traverse_func_def(env, fdef)); const Exp predicate = stmt->d.stmt_exp.val; - if(isa(predicate->type, env->gwion->type[et_bool]) < 0) { + if (isa(predicate->type, env->gwion->type[et_bool]) < 0) { char explain[strlen(predicate->type->name) + 20]; sprintf(explain, "found `{/+}%s{0}`", predicate->type->name); - gwerr_basic("Invalid `{/+}when{0}` predicate expression type", explain, "use `{/+}bool{0}`", - env->name, when->pos, 0); + gwerr_basic("Invalid `{/+}when{0}` predicate expression type", explain, + "use `{/+}bool{0}`", env->name, when->pos, 0); char from[strlen(tdef->type->name) + 39]; sprintf(from, "in `{/+}%s{0}` definition", tdef->type->name); gwerr_secondary(from, env->name, tdef->pos); - if(env->context) - env->context->error = true; + if (env->context) env->context->error = true; return GW_ERROR; } -/* - // enable static checking - const Func f = fdef->base->func; - const struct Op_Func opfunc = { .ck=opck_predicate }; - const struct Op_Import opi = { .rhs=f->value_ref->type, - .func=&opfunc, .data=(uintptr_t)f, .pos=tdef->pos, .op=insert_symbol("@func_check") }; - CHECK_BB(add_op(env->gwion, &opi)); -*/ - // we handle the return after, so that we don't get *cant' use implicit casting while defining it* - const Exp ret_id = new_prim_id(env->gwion->mp, insert_symbol("self"), when->pos); + /* + // enable static checking + const Func f = fdef->base->func; + const struct Op_Func opfunc = { .ck=opck_predicate }; + const struct Op_Import opi = { .rhs=f->value_ref->type, + .func=&opfunc, .data=(uintptr_t)f, .pos=tdef->pos, + .op=insert_symbol("@func_check") }; CHECK_BB(add_op(env->gwion, &opi)); + */ + // we handle the return after, so that we don't get *cant' use implicit + // casting while defining it* + const Exp ret_id = + new_prim_id(env->gwion->mp, insert_symbol("self"), when->pos); ret_id->d.prim.value = new_value(env->gwion->mp, tdef->type, "self"); - const Stmt ret = new_stmt_exp(env->gwion->mp, ae_stmt_return, ret_id, when->pos); + const Stmt ret = + new_stmt_exp(env->gwion->mp, ae_stmt_return, ret_id, when->pos); const Stmt_List ret_list = new_stmt_list(env->gwion->mp, ret, NULL); - ret_id->type = tdef->type; - body->next = ret_list; - tdef->when_def = fdef; - + ret_id->type = tdef->type; + body->next = ret_list; + tdef->when_def = fdef; } - return (!is_fptr(env->gwion, tdef->type) && tdef->type->info->cdef) ? - check_class_def(env, tdef->type->info->cdef) : GW_OK; + return (!is_fptr(env->gwion, tdef->type) && tdef->type->info->cdef) + ? check_class_def(env, tdef->type->info->cdef) + : GW_OK; +} +ANN static Type check_exp_lambda(const Env env, const Exp_If *exp_if NUSED) { + return env->gwion->type[et_lambda]; } -ANN static Type check_exp_lambda(const Env env, - const Exp_If* exp_if NUSED) { return env->gwion->type[et_lambda]; } ANN static Type check_exp_td(const Env env, Type_Decl **td) { DECL_OO(const Type, t, = known_type(env, *td)); @@ -1002,43 +1047,43 @@ DECL_EXP_FUNC(check, Type, Env) ANN Type check_exp(const Env env, const Exp exp) { Exp curr = exp; - if(!exp->type) { + if (!exp->type) { env_weight(env, 1); do { CHECK_OO((curr->type = check_exp_func[curr->exp_type](env, &curr->d))); - if(env->func && isa(curr->type, env->gwion->type[et_lambda]) < 0 && isa(curr->type, env->gwion->type[et_function]) > 0 && + if (env->func && isa(curr->type, env->gwion->type[et_lambda]) < 0 && + isa(curr->type, env->gwion->type[et_function]) > 0 && !fflag(curr->type->info->func, fflag_pure)) unset_fflag(env->func, fflag_pure); - } while((curr = curr->next)); + } while ((curr = curr->next)); } return exp->type; } ANN m_bool check_enum_def(const Env env, const Enum_Def edef) { - if(env->class_def) { + if (env->class_def) { ID_List list = edef->list; do decl_static(env, nspc_lookup_value0(env->curr, list->xid)); - while((list = list->next)); + while ((list = list->next)); } return GW_OK; } ANN static m_bool check_stmt_code(const Env env, const Stmt_Code stmt) { - if(stmt->stmt_list) { RET_NSPC(check_stmt_list(env, stmt->stmt_list)) } + if (stmt->stmt_list) { RET_NSPC(check_stmt_list(env, stmt->stmt_list)) } return GW_OK; } ANN static m_bool check_stmt_varloop(const Env env, const Stmt_VarLoop stmt) { CHECK_OB(check_exp(env, stmt->exp)); - if(isa(stmt->exp->type, env->gwion->type[et_vararg]) < 0) + if (isa(stmt->exp->type, env->gwion->type[et_vararg]) < 0) ERR_B(stmt->exp->pos, "varloop expression type must be '%s', not '%s'", - env->gwion->type[et_vararg]->name, stmt->exp->type->name) + env->gwion->type[et_vararg]->name, stmt->exp->type->name) return check_stmt(env, stmt->body); } ANN static inline m_bool _check_breaks(const Env env, const Stmt b) { - RET_NSPC(check_stmt(env, b)) -} + RET_NSPC(check_stmt(env, b))} ANN static m_bool check_breaks(const Env env, const Stmt a, const Stmt b) { vector_add(&env->scope->breaks, (vtype)a); @@ -1055,18 +1100,21 @@ ANN static m_bool check_conts(const Env env, const Stmt a, const Stmt b) { } ANN static inline m_bool for_empty(const Env env, const Stmt_For stmt) { - if(!stmt->c2 || !stmt->c2->d.stmt_exp.val) - ERR_B(stmt_self(stmt)->pos, _("empty for loop condition..." - "...(note: explicitly use 'true' if it's the intent)" - "...(e.g., 'for(; true;){{ /*...*/ }')")) + if (!stmt->c2 || !stmt->c2->d.stmt_exp.val) + ERR_B(stmt_self(stmt)->pos, + _("empty for loop condition..." + "...(note: explicitly use 'true' if it's the intent)" + "...(e.g., 'for(; true;){{ /*...*/ }')")) return GW_OK; } ANN static inline Type foreach_type(const Env env, const Exp exp) { DECL_OO(Type, et, = check_exp(env, exp)); - if(isa(et, env->gwion->type[et_array]) < 0) - ERR_O(exp->pos, _("type '%s' is not array.\n" - " This is not allowed in foreach loop"), et->name) + if (isa(et, env->gwion->type[et_array]) < 0) + ERR_O(exp->pos, + _("type '%s' is not array.\n" + " This is not allowed in foreach loop"), + et->name) DECL_OO(Type, base, = typedef_base(et)); DECL_OO(const Type, t, = array_base(base)); const m_uint depth = base->array_depth - 1; @@ -1074,7 +1122,8 @@ ANN static inline Type foreach_type(const Env env, const Exp exp) { } ANN static void check_idx(const Env env, struct EachIdx_ *const idx) { - idx->v = new_value(env->gwion->mp, env->gwion->type[et_int], s_name(idx->sym)); + idx->v = + new_value(env->gwion->mp, env->gwion->type[et_int], s_name(idx->sym)); valuefrom(env, idx->v->from, idx->pos); set_vflag(idx->v, vflag_valid); nspc_add_value(env->curr, idx->sym, idx->v); @@ -1084,39 +1133,38 @@ ANN static m_bool do_stmt_each(const Env env, const Stmt_Each stmt) { DECL_OB(const Type, base, = foreach_type(env, stmt->exp)); CHECK_BB(ensure_traverse(env, base)); const m_str basename = type2str(env->gwion, base, stmt->exp->pos); - char c[15 + strlen(basename)]; + char c[15 + strlen(basename)]; sprintf(c, "Ref:[%s]", basename); const Type ret = str2type(env->gwion, c, stmt->exp->pos); - if(base->array_depth) - set_tflag(ret, tflag_typedef); + if (base->array_depth) set_tflag(ret, tflag_typedef); stmt->v = new_value(env->gwion->mp, ret, s_name(stmt->sym)); set_vflag(stmt->v, vflag_valid); nspc_add_value(env->curr, stmt->sym, stmt->v); - if(stmt->idx) - check_idx(env, stmt->idx); + if (stmt->idx) check_idx(env, stmt->idx); return check_conts(env, stmt_self(stmt), stmt->body); } ANN static m_bool do_stmt_repeat(const Env env, const Stmt_Loop stmt) { - if(stmt->idx) - check_idx(env, stmt->idx); + if (stmt->idx) check_idx(env, stmt->idx); return check_conts(env, stmt_self(stmt), stmt->body); } ANN static inline m_bool cond_type(const Env env, const Exp e) { const Type t_int = env->gwion->type[et_int]; - if(check_implicit(env, e, t_int) < 0) { + if (check_implicit(env, e, t_int) < 0) { char explain[40 + strlen(e->type->name)]; sprintf(explain, "expected `{/+}int{0}`, got `{/+}%s{0}`", e->type->name); gwerr_basic(_("invalid repeat condition type"), explain, - _("use an integer or cast to int if possible"), env->name, e->pos, 0); + _("use an integer or cast to int if possible"), env->name, + e->pos, 0); env->context->error = true; return GW_ERROR; } return GW_OK; } -#define stmt_func_xxx(name, type, prolog, exp) describe_stmt_func(check, name, type, prolog, exp) +#define stmt_func_xxx(name, type, prolog, exp) \ + describe_stmt_func(check, name, type, prolog, exp) stmt_func_xxx(if, Stmt_If,, !(!check_flow(env, stmt->cond) || check_stmt(env, stmt->if_body) < 0 || (stmt->else_body && check_stmt(env, stmt->else_body) < 0)) ? 1 : -1) @@ -1138,44 +1186,49 @@ stmt_func_xxx(loop, Stmt_Loop, env_inline_mult(env, 1.5); check_idx(env, stmt->i stmt_func_xxx(each, Stmt_Each, env_inline_mult(env, 1.5), do_stmt_each(env, stmt)) ANN static m_bool check_stmt_return(const Env env, const Stmt_Exp stmt) { - if(!env->func) - ERR_B(stmt_self(stmt)->pos, _("'return' statement found outside function definition")) - if(env->scope->depth == 1) // so ops no dot set scope->depth ? + if (!env->func) + ERR_B(stmt_self(stmt)->pos, + _("'return' statement found outside function definition")) + if (env->scope->depth == 1) // so ops no dot set scope->depth ? set_fflag(env->func, fflag_return); - DECL_OB(const Type, ret_type, = stmt->val ? check_exp(env, stmt->val) : env->gwion->type[et_void]); - if(!env->func->def->base->ret_type) { + DECL_OB(const Type, ret_type, + = stmt->val ? check_exp(env, stmt->val) : env->gwion->type[et_void]); + if (!env->func->def->base->ret_type) { assert(isa(env->func->value_ref->type, env->gwion->type[et_lambda]) > 0); env->func->def->base->ret_type = ret_type; return GW_OK; } - if(isa(ret_type, env->func->def->base->ret_type) > 0) - return GW_OK; - if(tflag(ret_type, tflag_noret)) - ERR_B(stmt->val->pos, _("Can't use type `{+G}%s{+G}` for return"), ret_type->name); - if(stmt->val) { - if(env->func->def->base->xid == insert_symbol("@implicit") && ret_type == env->func->def->base->args->type) - ERR_B(stmt_self(stmt)->pos, _("can't use implicit casting while defining it")) - if(check_implicit(env, stmt->val, env->func->def->base->ret_type) > 0) + if (isa(ret_type, env->func->def->base->ret_type) > 0) return GW_OK; + if (tflag(ret_type, tflag_noret)) + ERR_B(stmt->val->pos, _("Can't use type `{+G}%s{+G}` for return"), + ret_type->name); + if (stmt->val) { + if (env->func->def->base->xid == insert_symbol("@implicit") && + ret_type == env->func->def->base->args->type) + ERR_B(stmt_self(stmt)->pos, + _("can't use implicit casting while defining it")) + if (check_implicit(env, stmt->val, env->func->def->base->ret_type) > 0) return GW_OK; - ERR_B(stmt_self(stmt)->pos, _("invalid return type: got '%s', expected '%s'"), - ret_type->name, env->func->def->base->ret_type->name) + ERR_B(stmt_self(stmt)->pos, + _("invalid return type: got '%s', expected '%s'"), ret_type->name, + env->func->def->base->ret_type->name) } - if(isa(env->func->def->base->ret_type, env->gwion->type[et_void]) > 0) + if (isa(env->func->def->base->ret_type, env->gwion->type[et_void]) > 0) return GW_OK; ERR_B(stmt_self(stmt)->pos, _("missing value for return statement")) } -#define describe_check_stmt_stack(stack, name) \ -ANN static m_bool check_stmt_##name(const Env env, const Stmt stmt) {\ - if(!vector_size(&env->scope->stack)) \ - ERR_B(stmt->pos, _("'"#name"' found outside of for/while/until...")) \ - return GW_OK; \ -} -describe_check_stmt_stack(conts, continue) -describe_check_stmt_stack(breaks, break) +#define describe_check_stmt_stack(stack, name) \ + ANN static m_bool check_stmt_##name(const Env env, const Stmt stmt) { \ + if (!vector_size(&env->scope->stack)) \ + ERR_B(stmt->pos, _("'" #name "' found outside of for/while/until...")) \ + return GW_OK; \ + } +describe_check_stmt_stack(conts, continue) + describe_check_stmt_stack(breaks, break) -ANN m_bool check_union_def(const Env env NUSED, const Union_Def udef) { - if(tmpl_base(udef->tmpl)) // there's a func for this + ANN m_bool check_union_def(const Env env NUSED, const Union_Def udef) { + if (tmpl_base(udef->tmpl)) // there's a func for this return GW_OK; set_tflag(udef->type, tflag_check); return GW_OK; @@ -1185,29 +1238,30 @@ ANN static m_bool check_stmt_exp(const Env env, const Stmt_Exp stmt) { return stmt->val ? check_exp(env, stmt->val) ? 1 : -1 : 1; } -ANN static Value match_value(const Env env, const Type base, const Exp_Primary* prim) { +ANN static Value match_value(const Env env, const Type base, + const Exp_Primary *prim) { const Symbol sym = prim->d.var; - const Value v = new_value(env->gwion->mp, base, s_name(sym)); + const Value v = new_value(env->gwion->mp, base, s_name(sym)); set_vflag(v, vflag_valid); nspc_add_value(env->curr, sym, v); return v; } ANN static Symbol case_op(const Env env, const Type base, const Exp e) { - if(e->exp_type == ae_exp_primary) { - if(e->d.prim.prim_type == ae_prim_id) { - if(e->d.prim.d.var == insert_symbol("_")) - return NULL; - if(!nspc_lookup_value1(env->curr, e->d.prim.d.var)) { + if (e->exp_type == ae_exp_primary) { + if (e->d.prim.prim_type == ae_prim_id) { + if (e->d.prim.d.var == insert_symbol("_")) return NULL; + if (!nspc_lookup_value1(env->curr, e->d.prim.d.var)) { e->d.prim.value = match_value(env, base, &e->d.prim); return NULL; } } - } else if(isa(base, env->gwion->type[et_union]) > 0 && e->exp_type == ae_exp_call) { + } else if (isa(base, env->gwion->type[et_union]) > 0 && + e->exp_type == ae_exp_call) { const Exp func = e->d.exp_call.func; - if(func->d.prim.prim_type == ae_prim_id) { - const Value v= find_value(base, func->d.prim.d.var); - if(v) { + if (func->d.prim.prim_type == ae_prim_id) { + const Value v = find_value(base, func->d.prim.d.var); + if (v) { e->type = v->type; case_op(env, v->type, e->d.exp_call.args); e->d.exp_call.args->type = v->type; @@ -1220,88 +1274,87 @@ ANN static Symbol case_op(const Env env, const Type base, const Exp e) { ANN static m_bool match_case_exp(const Env env, Exp e) { Exp last = e; - for(m_uint i = 0; i < vector_size(&env->scope->match->cond); e = e->next, ++i) { - if(!e) - ERR_B(last->pos, _("no enough to match")) - last = e; - const Exp base = (Exp)vector_at(&env->scope->match->cond, i); - const Symbol op = case_op(env, base->type, e); - if(op) { + for (m_uint i = 0; i < vector_size(&env->scope->match->cond); + e = e->next, ++i) { + if (!e) ERR_B(last->pos, _("no enough to match")) + last = e; + const Exp base = (Exp)vector_at(&env->scope->match->cond, i); + const Symbol op = case_op(env, base->type, e); + if (op) { const Exp next = e->next; - e->next = NULL; - const Type t = check_exp(env, e); - e->next = next; + e->next = NULL; + const Type t = check_exp(env, e); + e->next = next; CHECK_OB(t); - Exp_Binary bin = { .lhs=base, .rhs=e, .op=op }; - struct Exp_ ebin = { .d={.exp_binary=bin} }; - struct Op_Import opi = { .op=op, .lhs=base->type, .rhs=e->type, - .data=(uintptr_t)&ebin.d.exp_binary, .pos=e->pos }; + Exp_Binary bin = {.lhs = base, .rhs = e, .op = op}; + struct Exp_ ebin = {.d = {.exp_binary = bin}}; + struct Op_Import opi = {.op = op, + .lhs = base->type, + .rhs = e->type, + .data = (uintptr_t)&ebin.d.exp_binary, + .pos = e->pos}; CHECK_OB(op_check(env, &opi)); } } - if(e) - ERR_B(e->pos, _("too many expression to match")) + if (e) ERR_B(e->pos, _("too many expression to match")) return GW_OK; } ANN static m_bool _check_stmt_case(const Env env, const Stmt_Match stmt) { CHECK_BB(match_case_exp(env, stmt->cond)); - if(stmt->when) - CHECK_OB(check_flow(env, stmt->when)); + if (stmt->when) CHECK_OB(check_flow(env, stmt->when)); return check_stmt_list(env, stmt->list); } ANN static m_bool check_stmt_case(const Env env, const Stmt_Match stmt) { - RET_NSPC(_check_stmt_case(env, stmt)) -} + RET_NSPC(_check_stmt_case(env, stmt))} ANN static m_bool case_loop(const Env env, const Stmt_Match stmt) { Stmt_List list = stmt->list; do CHECK_BB(check_stmt_case(env, &list->stmt->d.stmt_match)); - while((list = list->next)); + while ((list = list->next)); return GW_OK; } - -ANN static inline m_bool check_handler_list(const restrict Env env, const Handler_List handler) { - if(handler->next) - CHECK_BB(check_handler_list(env, handler->next)); +ANN static inline m_bool check_handler_list(const restrict Env env, + const Handler_List handler) { + if (handler->next) CHECK_BB(check_handler_list(env, handler->next)); RET_NSPC(check_stmt(env, handler->stmt)) } -ANN static inline m_bool check_stmt_try_start(const restrict Env env, const Stmt_Try stmt) { - RET_NSPC(check_stmt(env, stmt->stmt)) -} -ANN static inline m_bool _check_stmt_try(const restrict Env env, const Stmt_Try stmt) { +ANN static inline m_bool check_stmt_try_start(const restrict Env env, + const Stmt_Try stmt) { + RET_NSPC(check_stmt(env, stmt->stmt))} ANN static inline m_bool + _check_stmt_try(const restrict Env env, const Stmt_Try stmt) { CHECK_BB(check_handler_list(env, stmt->handler)); vector_add(&env->scope->effects, 0); const m_bool ret = check_stmt_try_start(env, stmt); - const m_uint _v = vector_pop(&env->scope->effects); - if(_v) { + const m_uint _v = vector_pop(&env->scope->effects); + if (_v) { const M_Vector v = (M_Vector)&_v; - for(m_uint i = 0; i < m_vector_size(v); i++) { + for (m_uint i = 0; i < m_vector_size(v); i++) { struct ScopeEffect eff; m_vector_get(v, i, &eff); Handler_List handler = stmt->handler; - bool found = false; + bool found = false; do { // check there is no duplicate handler - if(eff.sym == handler->xid) { + if (eff.sym == handler->xid) { found = true; break; } - } while((handler = handler->next)); - if(!found) - env_add_effect(env, eff.sym, eff.pos); + } while ((handler = handler->next)); + if (!found) env_add_effect(env, eff.sym, eff.pos); } m_vector_release(v); } return ret; } -ANN static inline m_bool check_stmt_try(const restrict Env env, const Stmt_Try stmt) { - const bool in_try = env->scope->in_try; - const m_bool ret = _check_stmt_try(env, stmt); - env->scope->in_try = in_try; +ANN static inline m_bool check_stmt_try(const restrict Env env, + const Stmt_Try stmt) { + const bool in_try = env->scope->in_try; + const m_bool ret = _check_stmt_try(env, stmt); + env->scope->in_try = in_try; return ret; } @@ -1314,8 +1367,7 @@ ANN static m_bool _check_stmt_match(const Env env, const Stmt_Match stmt) { } ANN static inline m_bool handle_where(const Env env, const Stmt_Match stmt) { - if(stmt->where) - CHECK_BB(check_stmt(env, stmt->where)); + if (stmt->where) CHECK_BB(check_stmt(env, stmt->where)); RET_NSPC(_check_stmt_match(env, stmt)) } @@ -1327,8 +1379,7 @@ ANN static m_bool check_stmt_match(const Env env, const Stmt_Match stmt) { #define check_stmt_until check_stmt_flow ANN static m_bool check_stmt_pp(const Env env, const Stmt_PP stmt) { - if(stmt->pp_type == ae_pp_include) - env->name = stmt->data; + if (stmt->pp_type == ae_pp_include) env->name = stmt->data; return GW_OK; } @@ -1337,7 +1388,7 @@ ANN static m_bool check_stmt_defer(const Env env, const Stmt_Defer stmt) { } #define check_stmt_retry dummy_func -DECL_STMT_FUNC(check, m_bool , Env) +DECL_STMT_FUNC(check, m_bool, Env) ANN m_bool check_stmt(const Env env, const Stmt stmt) { return check_stmt_func[stmt->stmt_type](env, &stmt->d); @@ -1345,53 +1396,57 @@ ANN m_bool check_stmt(const Env env, const Stmt stmt) { ANN static m_bool check_stmt_list(const Env env, Stmt_List l) { do CHECK_BB(check_stmt(env, l->stmt)); - while((l = l->next)); + while ((l = l->next)); return GW_OK; } -ANN static m_bool check_signature_match(const Env env, const Func_Def fdef, const Func parent) { - if(GET_FLAG(parent->def->base, final)) - ERR_B(fdef->base->td->pos, _("can't override final function '%s'\n"), parent->name) - if(GET_FLAG(parent->def->base, static) != GET_FLAG(fdef->base, static)) { - const m_str c_name = fdef->base->func->value_ref->from->owner_class->name; +ANN static m_bool check_signature_match(const Env env, const Func_Def fdef, + const Func parent) { + if (GET_FLAG(parent->def->base, final)) + ERR_B(fdef->base->td->pos, _("can't override final function '%s'\n"), + parent->name) + if (GET_FLAG(parent->def->base, static) != GET_FLAG(fdef->base, static)) { + const m_str c_name = fdef->base->func->value_ref->from->owner_class->name; const m_str p_name = parent->value_ref->from->owner_class->name; const m_str f_name = s_name(fdef->base->xid); ERR_B(fdef->base->td->pos, _("function '%s.%s' ressembles '%s.%s' but cannot override...\n" - " ...(reason: '%s.%s' is declared as 'static')"), + " ...(reason: '%s.%s' is declared as 'static')"), c_name, f_name, p_name, c_name, GET_FLAG(fdef->base, static) ? c_name : p_name, f_name) } - return !fdef->base->tmpl ? isa(fdef->base->ret_type, parent->def->base->ret_type) : GW_OK; + return !fdef->base->tmpl + ? isa(fdef->base->ret_type, parent->def->base->ret_type) + : GW_OK; } -ANN static m_bool parent_match_actual(const Env env, const restrict Func_Def fdef, - const restrict Func func) { +ANN static m_bool parent_match_actual(const Env env, + const restrict Func_Def fdef, + const restrict Func func) { Func parent_func = func; do { - if(parent_func->def->base && compat_func(fdef, parent_func->def) > 0) { + if (parent_func->def->base && compat_func(fdef, parent_func->def) > 0) { CHECK_BB(check_signature_match(env, fdef, parent_func)); - if(!fdef->base->tmpl) { + if (!fdef->base->tmpl) { fdef->base->func->vt_index = parent_func->vt_index; - vector_set(&env->curr->vtable, fdef->base->func->vt_index, (vtype)fdef->base->func); + vector_set(&env->curr->vtable, fdef->base->func->vt_index, + (vtype)fdef->base->func); } return GW_OK; } - } while((parent_func = parent_func->next)); + } while ((parent_func = parent_func->next)); return 0; } ANN static m_bool check_parent_match(const Env env, const Func_Def fdef) { - const Func func = fdef->base->func; + const Func func = fdef->base->func; const Type parent = env->class_def->info->parent; - if(!env->curr->vtable.ptr) - vector_init(&env->curr->vtable); - if(parent) { + if (!env->curr->vtable.ptr) vector_init(&env->curr->vtable); + if (parent) { const Value v = find_value(parent, fdef->base->xid); - if(v && isa(v->type, env->gwion->type[et_function]) > 0) { + if (v && isa(v->type, env->gwion->type[et_function]) > 0) { const m_bool match = parent_match_actual(env, fdef, v->d.func_ref); - if(match) - return match; + if (match) return match; } } func->vt_index = vector_size(&env->curr->vtable); @@ -1399,124 +1454,131 @@ ANN static m_bool check_parent_match(const Env env, const Func_Def fdef) { return GW_OK; } -ANN static inline Func get_overload(const Env env, const Func_Def fdef, const m_uint i) { - const Symbol sym = func_symbol(env, env->curr->name, s_name(fdef->base->xid), NULL, i); +ANN static inline Func get_overload(const Env env, const Func_Def fdef, + const m_uint i) { + const Symbol sym = + func_symbol(env, env->curr->name, s_name(fdef->base->xid), NULL, i); return nspc_lookup_func1(env->curr, sym); } ANN static m_bool check_func_overload(const Env env, const Func_Def fdef) { const Value v = fdef->base->func->value_ref; - for(m_uint i = 0; i <= v->from->offset; ++i) { + for (m_uint i = 0; i <= v->from->offset; ++i) { const Func f1 = get_overload(env, fdef, i); - for(m_uint j = i + 1; f1 && j <= v->from->offset; ++j) { + for (m_uint j = i + 1; f1 && j <= v->from->offset; ++j) { const Func f2 = get_overload(env, fdef, j); - if(f2 && compat_func(f1->def, f2->def) > 0 && - fbflag(f1->def->base, fbflag_op) == fbflag(f2->def->base, fbflag_op) && - fbflag(f1->def->base, fbflag_unary) == fbflag(f2->def->base, fbflag_unary) && - fbflag(f1->def->base, fbflag_postfix) == fbflag(f2->def->base, fbflag_postfix)) - ERR_B(f2->def->base->td->pos, _("global function '%s' already defined" - " for those arguments"), s_name(fdef->base->xid)) + if (f2 && compat_func(f1->def, f2->def) > 0 && + fbflag(f1->def->base, fbflag_op) == + fbflag(f2->def->base, fbflag_op) && + fbflag(f1->def->base, fbflag_unary) == + fbflag(f2->def->base, fbflag_unary) && + fbflag(f1->def->base, fbflag_postfix) == + fbflag(f2->def->base, fbflag_postfix)) + ERR_B(f2->def->base->td->pos, + _("global function '%s' already defined" + " for those arguments"), + s_name(fdef->base->xid)) } } return GW_OK; } ANN bool check_effect_overload(const Vector base, const Func override) { - if(!base->ptr) - return true; - if(!override->def->base->effects.ptr) - return false; // TODO: error message + if (!base->ptr) return true; + if (!override->def->base->effects.ptr) return false; // TODO: error message const Vector v = &override->def->base->effects; - for(m_uint i = 0; i < vector_size(v); i++) { - if(vector_find((Vector)base, vector_at(v, i)) == -1) - return false; + for (m_uint i = 0; i < vector_size(v); i++) { + if (vector_find((Vector)base, vector_at(v, i)) == -1) return false; } return true; } -ANN static m_bool check_func_def_override(const Env env, const Func_Def fdef, Value *ov) { +ANN static m_bool check_func_def_override(const Env env, const Func_Def fdef, + Value *ov) { const Func func = fdef->base->func; - if(env->class_def && env->class_def->info->parent) { - const Value override = find_value(env->class_def->info->parent, fdef->base->xid); - if(override && override->from->owner_class && isa(override->type, env->gwion->type[et_function]) < 0) - ERR_B(fdef->base->pos, + if (env->class_def && env->class_def->info->parent) { + const Value override = + find_value(env->class_def->info->parent, fdef->base->xid); + if (override && override->from->owner_class && + isa(override->type, env->gwion->type[et_function]) < 0) + ERR_B(fdef->base->pos, _("function name '%s' conflicts with previously defined value...\n" - " from super class '%s'..."), + " from super class '%s'..."), s_name(fdef->base->xid), override->from->owner_class->name) *ov = override; } - if(func->value_ref->from->offset && (!fdef->base->tmpl || !fdef->base->tmpl->base)) + if (func->value_ref->from->offset && + (!fdef->base->tmpl || !fdef->base->tmpl->base)) CHECK_BB(check_func_overload(env, fdef)); return GW_OK; } ANN m_bool check_fdef(const Env env, const Func_Def fdef) { - if(fdef->base->args) - CHECK_BB(check_func_args(env, fdef->base->args)); - if(fdef->d.code) { + if (fdef->base->args) CHECK_BB(check_func_args(env, fdef->base->args)); + if (fdef->d.code) { env->scope->depth--; CHECK_BB(check_stmt_code(env, &fdef->d.code->d.stmt_code)); env->scope->depth++; } - if(fdef->base->ret_type && fdef->base->ret_type != env->gwion->type[et_void] && - fdef->d.code && !fflag(fdef->base->func, fflag_return)) - ERR_B(fdef->base->td->pos, _("missing return statement in a non void function")); + if (fdef->base->ret_type && + fdef->base->ret_type != env->gwion->type[et_void] && fdef->d.code && + !fflag(fdef->base->func, fflag_return)) + ERR_B(fdef->base->td->pos, + _("missing return statement in a non void function")); return GW_OK; } ANN m_bool check_func_def(const Env env, const Func_Def f) { - const Func func = f->base->func; + const Func func = f->base->func; const Func_Def fdef = func->def; assert(func == fdef->base->func); - if(env->class_def) // tmpl ? + if (env->class_def) // tmpl ? CHECK_BB(check_parent_match(env, fdef)); - if(tmpl_base(fdef->base->tmpl)) - return GW_OK; + if (tmpl_base(fdef->base->tmpl)) return GW_OK; Value override = NULL; CHECK_BB(check_func_def_override(env, fdef, &override)); - DECL_BB(const m_int, scope, = GET_FLAG(fdef->base, global) ? env_push_global(env) : env->scope->depth); + DECL_BB(const m_int, scope, = GET_FLAG(fdef->base, global) + ? env_push_global(env) + : env->scope->depth); const Func former = env->func; - env->func = func; + env->func = func; ++env->scope->depth; nspc_push_value(env->gwion->mp, env->curr); - struct Op_Import opi = { }; - if(fbflag(fdef->base, fbflag_op)) { + struct Op_Import opi = {}; + if (fbflag(fdef->base, fbflag_op)) { func_operator(f, &opi); operator_suspend(env->curr, &opi); } vector_add(&env->scope->effects, 0); const m_bool ret = scanx_fdef(env, env, fdef, (_exp_func)check_fdef); - const m_uint _v = vector_back(&env->scope->effects); - if(_v) { - if(fdef->base->xid == insert_symbol("@dtor")) + const m_uint _v = vector_back(&env->scope->effects); + if (_v) { + if (fdef->base->xid == insert_symbol("@dtor")) ERR_B(fdef->base->pos, _("can't use effects in destructors")); - const Vector v = (Vector)&_v; + const Vector v = (Vector)&_v; const Vector base = &fdef->base->effects; - if(!base->ptr) - vector_init(base); - for(m_uint i = 0; i < vector_size(v); i += 2) { + if (!base->ptr) vector_init(base); + for (m_uint i = 0; i < vector_size(v); i += 2) { const Symbol effect = (Symbol)vector_at(v, i); - if(vector_find(base, (m_uint)effect) == -1) + if (vector_find(base, (m_uint)effect) == -1) vector_add(base, (m_uint)effect); } vector_release(v); } vector_pop(&env->scope->effects); - if(fbflag(fdef->base, fbflag_op)) - operator_resume(&opi); + if (fbflag(fdef->base, fbflag_op)) operator_resume(&opi); nspc_pop_value(env->gwion->mp, env->curr); --env->scope->depth; env->func = former; - if(ret > 0) { + if (ret > 0) { set_fflag(fdef->base->func, fflag_valid); - if(env->class_def && fdef->base->effects.ptr && !check_effect_overload(&fdef->base->effects, override->d.func_ref)) - ERR_B(fdef->base->pos, - _("too much effects in override."), - s_name(fdef->base->xid)) + if (env->class_def && fdef->base->effects.ptr && + !check_effect_overload(&fdef->base->effects, override->d.func_ref)) + ERR_B(fdef->base->pos, _("too much effects in override."), + s_name(fdef->base->xid)) } - if(GET_FLAG(fdef->base, global)) - env_pop(env,scope); - if(func->value_ref->from->owner_class) + if (GET_FLAG(fdef->base, global)) env_pop(env, scope); + if (func->value_ref->from->owner_class) func->inline_mult += 3; else func->inline_mult += 4; @@ -1533,20 +1595,21 @@ ANN static m_bool check_extend_def(const Env env, const Extend_Def xdef) { ANN static m_bool _check_trait_def(const Env env, const Trait_Def pdef) { const Trait trait = nspc_lookup_trait1(env->curr, pdef->xid); - Ast ast = pdef->body; - while(ast) { - Section * section = ast->section; - if(section->section_type == ae_section_stmt) { + Ast ast = pdef->body; + while (ast) { + Section *section = ast->section; + if (section->section_type == ae_section_stmt) { Stmt_List list = section->d.stmt_list; - while(list) { + while (list) { const Stmt stmt = list->stmt; - if(stmt->stmt_type == ae_stmt_exp) { + if (stmt->stmt_type == ae_stmt_exp) { CHECK_BB(traverse_exp(env, stmt->d.stmt_exp.val)); Var_Decl_List list = stmt->d.stmt_exp.val->d.exp_decl.list; - while(list) { + while (list) { const Value value = list->self->value; - valuefrom(env, value->from, list->self->pos); // we do not need owner - if(!trait->requested_values.ptr) + valuefrom(env, value->from, + list->self->pos); // we do not need owner + if (!trait->requested_values.ptr) vector_init(&trait->requested_values); vector_add(&trait->requested_values, (m_uint)value); list = list->next; @@ -1568,42 +1631,38 @@ ANN static m_bool check_trait_def(const Env env, const Trait_Def pdef) { HANDLE_SECTION_FUNC(check, m_bool, Env) ANN static m_bool check_parent(const Env env, const Class_Def cdef) { - const Type parent = cdef->base.type->info->parent; - const Type_Decl *td = cdef->base.ext; -// if(td->array) - if(td->array && td->array->exp) + const Type parent = cdef->base.type->info->parent; + const Type_Decl *td = cdef->base.ext; + // if(td->array) + if (td->array && td->array->exp) CHECK_BB(check_subscripts(env, td->array, 1)); CHECK_BB(ensure_check(env, parent)); -// if(tflag(parent, tflag_typedef)) { -// set_tflag(cdef->base.type, tflag_typedef); -// } + // if(tflag(parent, tflag_typedef)) { + // set_tflag(cdef->base.type, tflag_typedef); + // } return GW_OK; } ANN static m_bool cdef_parent(const Env env, const Class_Def cdef) { const bool tmpl = !!cdef->base.tmpl; - if(tmpl) - CHECK_BB(template_push_types(env, cdef->base.tmpl)); + if (tmpl) CHECK_BB(template_push_types(env, cdef->base.tmpl)); const m_bool ret = check_parent(env, cdef); - if(tmpl) - nspc_pop_type(env->gwion->mp, env->curr); + if (tmpl) nspc_pop_type(env->gwion->mp, env->curr); return ret; } ANN m_bool check_abstract(const Env env, const Class_Def cdef) { - if(!cdef->base.type->nspc->vtable.ptr) - return GW_OK; + if (!cdef->base.type->nspc->vtable.ptr) return GW_OK; bool err = false; - for(m_uint i = 0; i < vector_size(&cdef->base.type->nspc->vtable); ++i) { + for (m_uint i = 0; i < vector_size(&cdef->base.type->nspc->vtable); ++i) { Func f = (Func)vector_at(&cdef->base.type->nspc->vtable, i); - if(f && GET_FLAG(f->def->base, abstract)) { - if(!err) { + if (f && GET_FLAG(f->def->base, abstract)) { + if (!err) { err = true; - gwerr_basic( - _("missing function definition"), - _("must be declared 'abstract'"), - _("provide an implementation for the following:"), - env->name, cdef->pos, 0); + gwerr_basic(_("missing function definition"), + _("must be declared 'abstract'"), + _("provide an implementation for the following:"), + env->name, cdef->pos, 0); } struct ValueFrom_ *from = f->value_ref->from; gwerr_secondary("implementation missing", from->filename, from->loc); @@ -1614,13 +1673,12 @@ ANN m_bool check_abstract(const Env env, const Class_Def cdef) { } ANN static inline void ctor_effects(const Env env) { - const Vector v = &env->scope->effects; + const Vector v = &env->scope->effects; const m_uint _w = vector_back(v); - if(!_w) - return; + if (!_w) return; vector_init(&env->class_def->effects); const M_Vector w = (M_Vector)&_w; - for(m_uint j = 0; j < m_vector_size(w); j++) { + for (m_uint j = 0; j < m_vector_size(w); j++) { struct ScopeEffect eff; m_vector_get(w, j, &eff); vector_add(&env->class_def->effects, (m_uint)eff.sym); @@ -1638,59 +1696,52 @@ ANN static m_bool check_body(const Env env, Section *const section) { ANN static bool class_def_has_body(const Env env, Ast ast) { do { const Section *section = ast->section; - if(section->section_type == ae_section_stmt) { + if (section->section_type == ae_section_stmt) { Stmt_List list = section->d.stmt_list; do { const Stmt stmt = list->stmt; - if(stmt->stmt_type == ae_stmt_pp) - continue; - if(stmt->stmt_type == ae_stmt_exp) { + if (stmt->stmt_type == ae_stmt_pp) continue; + if (stmt->stmt_type == ae_stmt_exp) { const Exp exp = stmt->d.stmt_exp.val; - if(!exp) - continue; - if(exp->exp_type != ae_exp_decl) - return true; - if(GET_FLAG(exp->d.exp_decl.td, late)) - continue; + if (!exp) continue; + if (exp->exp_type != ae_exp_decl) return true; + if (GET_FLAG(exp->d.exp_decl.td, late)) continue; Var_Decl_List dlist = exp->d.exp_decl.list; do { - if(GET_FLAG(dlist->self->value, late)) - continue; -if(isa(dlist->self->value->type, env->gwion->type[et_compound]) > 0) -// if(tflag(dlist->self->value->type, tflag_ctor) || -// dlist->self->value->type->array_depth) + if (GET_FLAG(dlist->self->value, late)) continue; + if (isa(dlist->self->value->type, env->gwion->type[et_compound]) > + 0) + // if(tflag(dlist->self->value->type, tflag_ctor) || + // dlist->self->value->type->array_depth) return true; - } while((dlist = dlist->next)); + } while ((dlist = dlist->next)); } else return true; - } while((list = list->next)); + } while ((list = list->next)); } - } while((ast = ast->next)); + } while ((ast = ast->next)); return false; } ANN bool check_trait_requests(const Env env, const Type t, const ID_List list); ANN static m_bool _check_class_def(const Env env, const Class_Def cdef) { const Type t = cdef->base.type; - if(cdef->base.ext) - CHECK_BB(cdef_parent(env, cdef)); - if(!tflag(t, tflag_struct)) - inherit(t); - if(cdef->body) { + if (cdef->base.ext) CHECK_BB(cdef_parent(env, cdef)); + if (!tflag(t, tflag_struct)) inherit(t); + if (cdef->body) { CHECK_BB(env_body(env, cdef, check_body)); - if(cflag(cdef, cflag_struct) || class_def_has_body(env, cdef->body)) + if (cflag(cdef, cflag_struct) || class_def_has_body(env, cdef->body)) set_tflag(t, tflag_ctor); } - if(!GET_FLAG(cdef, abstract)) - CHECK_BB(check_abstract(env, cdef)); - if(cdef->traits) { - ID_List list = cdef->traits; - bool value_error = false; - do if(!check_trait_requests(env, t, list)) - value_error = true; - while((list = list->next)); - - if(value_error) { + if (!GET_FLAG(cdef, abstract)) CHECK_BB(check_abstract(env, cdef)); + if (cdef->traits) { + ID_List list = cdef->traits; + bool value_error = false; + do + if (!check_trait_requests(env, t, list)) value_error = true; + while ((list = list->next)); + + if (value_error) { env->class_def = t; env_error_footer(env); env->context->error = true; @@ -1698,52 +1749,50 @@ ANN static m_bool _check_class_def(const Env env, const Class_Def cdef) { } } - Value value; - struct scope_iter iter = { t->nspc->info->value, 0, 0 }; - while(scope_iter(&iter, &value) > 0) { - if(isa(value->type, env->gwion->type[et_compound]) < 0) - continue; - if(value->type->nspc && !GET_FLAG(value, late)) { - Value v; - struct scope_iter inner = { value->type->nspc->info->value, 0, 0 }; - while(scope_iter(&inner, &v) > 0) { -// if(isa(v->type, t) > 0 || isa(t, v->type) > 0) { - if(v->type == t) { - env_err(env, v->from->loc, _("recursive type")); - env->context->error = false; - env_err(env, value->from->loc, _("recursive type")); - env->context->error = true; -type_remref(t, env->gwion); -return GW_ERROR; -} -} -} + Value value; + struct scope_iter iter = {t->nspc->info->value, 0, 0}; + while (scope_iter(&iter, &value) > 0) { + if (isa(value->type, env->gwion->type[et_compound]) < 0) continue; + if (value->type->nspc && !GET_FLAG(value, late)) { + Value v; + struct scope_iter inner = {value->type->nspc->info->value, 0, 0}; + while (scope_iter(&inner, &v) > 0) { + // if(isa(v->type, t) > 0 || isa(t, v->type) > 0) { + if (v->type == t) { + env_err(env, v->from->loc, _("recursive type")); + env->context->error = false; + env_err(env, value->from->loc, _("recursive type")); + env->context->error = true; + type_remref(t, env->gwion); + return GW_ERROR; + } + } + } } return GW_OK; } ANN m_bool check_class_def(const Env env, const Class_Def cdef) { - if(tmpl_base(cdef->base.tmpl)) - return GW_OK; - const Type t = cdef->base.type; - struct Op_Import opi = { .op=insert_symbol("@class_check"), .lhs=t, - .data=(uintptr_t)cdef, .pos=cdef->pos }; + if (tmpl_base(cdef->base.tmpl)) return GW_OK; + const Type t = cdef->base.type; + struct Op_Import opi = {.op = insert_symbol("@class_check"), + .lhs = t, + .data = (uintptr_t)cdef, + .pos = cdef->pos}; CHECK_OB(op_check(env, &opi)); - if(t->info->value->from->owner_class) + if (t->info->value->from->owner_class) CHECK_BB(ensure_check(env, t->info->value->from->owner_class)); - if(tflag(t, tflag_check)) - return GW_OK; + if (tflag(t, tflag_check)) return GW_OK; set_tflag(t, tflag_check); return _check_class_def(env, cdef); } ANN static inline void check_unhandled(const Env env) { - const Vector v = &env->scope->effects; + const Vector v = &env->scope->effects; const m_uint _w = vector_back(v); - if(!_w) - return; + if (!_w) return; const M_Vector w = (M_Vector)&_w; - for(m_uint j = 0; j < m_vector_size(w); j++) { + for (m_uint j = 0; j < m_vector_size(w); j++) { struct ScopeEffect eff; m_vector_get(w, j, &eff); gwerr_secondary("Unhandled effect", env->name, eff.pos); @@ -1755,7 +1804,7 @@ ANN static inline void check_unhandled(const Env env) { ANN m_bool check_ast(const Env env, Ast ast) { do CHECK_BB(check_section(env, ast->section)); - while((ast = ast->next)); + while ((ast = ast->next)); check_unhandled(env); return GW_OK; } diff --git a/src/parse/check_traits.c b/src/parse/check_traits.c index f9a938a9..b188fa5c 100644 --- a/src/parse/check_traits.c +++ b/src/parse/check_traits.c @@ -14,106 +14,105 @@ ANN static bool var_match(const Value a, const Value b) { bool error = true; - if(isa(a->type, a->type) < 0) { - gwerr_basic("invalid variable type", NULL, NULL, - a->from->filename, a->from->loc, 0); + if (isa(a->type, a->type) < 0) { + gwerr_basic("invalid variable type", NULL, NULL, a->from->filename, + a->from->loc, 0); // can we point to the type decl? error = false; } - if(GET_FLAG(a, const) && !GET_FLAG(b, const)) { + if (GET_FLAG(a, const) && !GET_FLAG(b, const)) { gwerr_basic("variable differs in {/}constness{0}", NULL, NULL, - a->from->filename, a->from->loc, 0); + a->from->filename, a->from->loc, 0); // can we point to the flag? error = false; } - if(GET_FLAG(a, static) && !GET_FLAG(b, static)) { + if (GET_FLAG(a, static) && !GET_FLAG(b, static)) { gwerr_basic("variable differs in {/}storage{0}", NULL, NULL, - a->from->filename, a->from->loc, 0); + a->from->filename, a->from->loc, 0); // can we point to the flag? error = false; } - if(error) - return true; + if (error) return true; gwerr_secondary("from requested variable", b->from->filename, b->from->loc); return error; } ANN static bool request_var(const Env env, const Type t, const Value request) { const Value value = nspc_lookup_value0(t->nspc, insert_symbol(request->name)); - if(!value) { + if (!value) { gwerr_basic("missing requested variable", NULL, NULL, - request->from->filename, request->from->loc, 0); + request->from->filename, request->from->loc, 0); return false; } return var_match(value, request); } -ANN static bool check_trait_variables(const Env env, const Type t, const Trait trait) { +ANN static bool check_trait_variables(const Env env, const Type t, + const Trait trait) { bool error = false; - for(m_uint i = 0; i < vector_size(&trait->requested_values); i++) { + for (m_uint i = 0; i < vector_size(&trait->requested_values); i++) { const Value request = (Value)vector_at(&trait->requested_values, i); - if(!request_var(env, t, request)) - error = true; + if (!request_var(env, t, request)) error = true; } return error; } -ANN static bool request_fun(const Env env, const Type t, const Func_Def request) { +ANN static bool request_fun(const Env env, const Type t, + const Func_Def request) { const Value v = nspc_lookup_value0(t->nspc, request->base->xid); -if(v) { - if(isa(v->type, env->gwion->type[et_function]) < 0 || is_fptr(env->gwion, v->type)) { - gwerr_basic("is not a function", NULL, NULL, - v->from->filename, v->from->loc, 0); - return false; - } - Func f = v->d.func_ref; - do { - if(compat_func(f->def, request) > 0) { - if(!GET_FLAG(f->def->base, abstract)) - return true; + if (v) { + if (isa(v->type, env->gwion->type[et_function]) < 0 || + is_fptr(env->gwion, v->type)) { + gwerr_basic("is not a function", NULL, NULL, v->from->filename, + v->from->loc, 0); + return false; } - } while ((f = f->next)); -} - if(!GET_FLAG(request->base, abstract)){ - const m_uint scope = env_push_type(env, t); - const Func_Def cpy = cpy_func_def(env->gwion->mp, request); - const m_bool ret = traverse_func_def(env, cpy); + Func f = v->d.func_ref; + do { + if (compat_func(f->def, request) > 0) { + if (!GET_FLAG(f->def->base, abstract)) return true; + } + } while ((f = f->next)); + } + if (!GET_FLAG(request->base, abstract)) { + const m_uint scope = env_push_type(env, t); + const Func_Def cpy = cpy_func_def(env->gwion->mp, request); + const m_bool ret = traverse_func_def(env, cpy); env_pop(env, scope); - if(ret > 0){ - Section *section = new_section_func_def(env->gwion->mp, cpy); - const Ast ast = new_ast(env->gwion->mp, section, NULL); - Ast last = t->info->cdef->body; - while(last->next) - last = last->next; + if (ret > 0) { + Section * section = new_section_func_def(env->gwion->mp, cpy); + const Ast ast = new_ast(env->gwion->mp, section, NULL); + Ast last = t->info->cdef->body; + while (last->next) last = last->next; last->next = ast; return true; } else - free_func_def(env->gwion->mp, cpy); + free_func_def(env->gwion->mp, cpy); } - if(!v) { - gwerr_basic("missing requested function", NULL, NULL, - env->name, request->base->pos, 0); + if (!v) { + gwerr_basic("missing requested function", NULL, NULL, env->name, + request->base->pos, 0); return false; } return true; } -ANN static bool check_trait_functions(const Env env, const Type t, const Trait trait) { +ANN static bool check_trait_functions(const Env env, const Type t, + const Trait trait) { bool error = false; - for(m_uint i = 0; i < vector_size(&trait->requested_funcs); i++) { + for (m_uint i = 0; i < vector_size(&trait->requested_funcs); i++) { const Func_Def request = (Func_Def)vector_at(&trait->requested_funcs, i); - if(!request_fun(env, t, request)) - error = true; + if (!request_fun(env, t, request)) error = true; } return error; } -ANN2(1,2) static inline bool trait_nodup(Type t, const Symbol trait, ID_List list) { +ANN2(1, 2) +static inline bool trait_nodup(Type t, const Symbol trait, ID_List list) { bool nodup = true; do { - while(list) { - if(trait == list->xid) - nodup = false; + while (list) { + if (trait == list->xid) nodup = false; list = list->next; } } while ((t = t->info->parent)); @@ -122,16 +121,14 @@ ANN2(1,2) static inline bool trait_nodup(Type t, const Symbol trait, ID_List lis ANN bool check_trait_requests(const Env env, const Type t, const ID_List list) { const Trait trait = nspc_lookup_trait1(env->curr, list->xid); - if(!trait_nodup(t, list->xid, list->next)) { + if (!trait_nodup(t, list->xid, list->next)) { gwerr_secondary("duplicated trait", trait->filename, trait->loc); return false; } const bool value_error = check_trait_variables(env, t, trait); const bool funcs_error = check_trait_functions(env, t, trait); - if(!value_error && !funcs_error) - return true; + if (!value_error && !funcs_error) return true; const Value request = (Value)vector_front(&trait->requested_values); gwerr_secondary("from trait", request->from->filename, trait->loc); return false; } - diff --git a/src/parse/compat_func.c b/src/parse/compat_func.c index f8646a7a..03d956ad 100644 --- a/src/parse/compat_func.c +++ b/src/parse/compat_func.c @@ -2,17 +2,16 @@ #include "gwion_ast.h" #include "gwion_env.h" -ANN m_bool compat_func(const restrict Func_Def lhs, const restrict Func_Def rhs) { +ANN m_bool compat_func(const restrict Func_Def lhs, + const restrict Func_Def rhs) { Arg_List e1 = lhs->base->args; Arg_List e2 = rhs->base->args; - while(e1 && e2) { - if(e1->type != e2->type) - return GW_ERROR; + while (e1 && e2) { + if (e1->type != e2->type) return GW_ERROR; e1 = e1->next; e2 = e2->next; } - if(e1 || e2) - return GW_ERROR; + if (e1 || e2) return GW_ERROR; return GW_OK; } diff --git a/src/parse/did_you_mean.c b/src/parse/did_you_mean.c index 83d58718..0255ee96 100644 --- a/src/parse/did_you_mean.c +++ b/src/parse/did_you_mean.c @@ -7,61 +7,61 @@ #endif #define MAX_DISTANCE 2 -#define min2(a, b) ((a) < (b) ? (a) : (b)) +#define min2(a, b) ((a) < (b) ? (a) : (b)) #define min(a, b, c) (min2(min2((a), (b)), (c))) -static m_bool wagner_fisher(const char *s, const char* t) { +static m_bool wagner_fisher(const char *s, const char *t) { const size_t m = strlen(s); const size_t n = strlen(t); unsigned int d[m][n]; - uint i, j; - for(i = 0; i < m; ++i) - d[i][0] = i; - for(i = 0; i < n; ++i) - d[0][i] = i; - for(j = 1; j < n; ++j) { - for(i = 1; i < m; ++i) { - if(s[i] == t[j]) - d[i][j] = d[i-1][j-1]; + uint i, j; + for (i = 0; i < m; ++i) d[i][0] = i; + for (i = 0; i < n; ++i) d[0][i] = i; + for (j = 1; j < n; ++j) { + for (i = 1; i < m; ++i) { + if (s[i] == t[j]) + d[i][j] = d[i - 1][j - 1]; else - d[i][j] = min(d[i-1][j] + 1, d[i][j-1] + 1, d[i-1][j-1] + 1); - if(d[i][j] > MAX_DISTANCE + 1) - return 0; + d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + 1); + if (d[i][j] > MAX_DISTANCE + 1) return 0; } } - return (i && j && d[m-1][n-1] < MAX_DISTANCE); + return (i && j && d[m - 1][n - 1] < MAX_DISTANCE); } -ANN static void ressembles(const Nspc nspc, const char* name, bool *const done) { - struct scope_iter iter = { nspc->info->value, 0, 0 }; - Value value; - while(scope_iter(&iter, &value) > 0) { - if(wagner_fisher(name, value->name)) { - if(!*done) { +ANN static void ressembles(const Nspc nspc, const char *name, + bool *const done) { + struct scope_iter iter = {nspc->info->value, 0, 0}; + Value value; + while (scope_iter(&iter, &value) > 0) { + if (wagner_fisher(name, value->name)) { + if (!*done) { *done = true; gw_err("{-/}did you mean{0}:\n"); } - if(!vflag(value, vflag_builtin)) - gwerr_secondary("declared here", value->from->filename, value->from->loc); + if (!vflag(value, vflag_builtin)) + gwerr_secondary("declared here", value->from->filename, + value->from->loc); } } } #define MAX_NAME_LEN 16 -#define CHECK_LEN(name) if(strlen(name) > MAX_NAME_LEN) return; +#define CHECK_LEN(name) \ + if (strlen(name) > MAX_NAME_LEN) return; #undef did_you_mean_nspc -ANN void did_you_mean_nspc(Nspc nspc, const char* name) { +ANN void did_you_mean_nspc(Nspc nspc, const char *name) { CHECK_LEN(name) bool done = false; do ressembles(nspc, name, &done); - while((nspc = nspc->parent)); + while ((nspc = nspc->parent)); } #undef did_you_mean_type -ANN void did_you_mean_type(Type type, const char* name) { +ANN void did_you_mean_type(Type type, const char *name) { CHECK_LEN(name) bool done = false; do ressembles(type->nspc, name, &done); - while((type = type->info->parent) && type->nspc); + while ((type = type->info->parent) && type->nspc); } diff --git a/src/parse/func_operator.c b/src/parse/func_operator.c index 8ae9cb00..4020f08c 100644 --- a/src/parse/func_operator.c +++ b/src/parse/func_operator.c @@ -8,16 +8,18 @@ #include "operator.h" ANN void func_operator(const Func_Def fdef, struct Op_Import *opi) { - opi->op =fdef->base->xid; + opi->op = fdef->base->xid; const m_str str = s_name(fdef->base->xid); - const uint is_unary = fbflag(fdef->base, fbflag_unary) + - (!strcmp(str, "@conditional") || !strcmp(str, "@unconditional")); + const uint is_unary = + fbflag(fdef->base, fbflag_unary) + + (!strcmp(str, "@conditional") || !strcmp(str, "@unconditional")); const Arg_List args = fdef->base->args; - opi->lhs = is_unary ? NULL : - args ? args->var_decl->value->type : NULL; - if(strcmp(str, "@implicit")) - opi->rhs = args ? is_unary ? args->var_decl->value->type : - (args->next ? args->next->var_decl->value->type : NULL) : NULL; + opi->lhs = is_unary ? NULL : args ? args->var_decl->value->type : NULL; + if (strcmp(str, "@implicit")) + opi->rhs = args ? is_unary ? args->var_decl->value->type + : (args->next ? args->next->var_decl->value->type + : NULL) + : NULL; else opi->rhs = fdef->base->ret_type; } diff --git a/src/parse/func_resolve_tmpl.c b/src/parse/func_resolve_tmpl.c index 5ad14d16..47e34c18 100644 --- a/src/parse/func_resolve_tmpl.c +++ b/src/parse/func_resolve_tmpl.c @@ -15,171 +15,181 @@ #include "tmp_resolve.h" struct ResolverArgs { - const Value v; + const Value v; Exp_Call *const e; - const m_str tmpl_name; + const m_str tmpl_name; const Type_List types; }; -ANN static inline Value template_get_ready(const Env env, const Value v, const m_str tmpl, const m_uint i) { +ANN static inline Value template_get_ready(const Env env, const Value v, + const m_str tmpl, const m_uint i) { const Symbol sym = func_symbol(env, v->from->owner->name, v->name, tmpl, i); - return v->from->owner_class ? find_value(v->from->owner_class, sym) : - nspc_lookup_value1(v->from->owner, sym); + return v->from->owner_class ? find_value(v->from->owner_class, sym) + : nspc_lookup_value1(v->from->owner, sym); } -ANN static inline bool tmpl_valid(const Env env, const Func_Def fdef/*, Exp_Call *const exp*/) { - if(safe_fflag(fdef->base->func, fflag_valid)) - return true; -// const Tmpl tmpl = { .list=fdef->base->tmpl->list, .call=exp->tmpl->call }; -// CHECK_BO(template_push_types(env, &tmpl));; +ANN static inline bool +tmpl_valid(const Env env, const Func_Def fdef /*, Exp_Call *const exp*/) { + if (safe_fflag(fdef->base->func, fflag_valid)) return true; + // const Tmpl tmpl = { .list=fdef->base->tmpl->list, .call=exp->tmpl->call }; + // CHECK_BO(template_push_types(env, &tmpl));; const bool ret = check_traverse_fdef(env, fdef) > 0; -// nspc_pop_type(env->gwion->mp, env->curr); + // nspc_pop_type(env->gwion->mp, env->curr); return ret; } -ANN static Func ensure_tmpl(const Env env, const Func_Def fdef, Exp_Call *const exp) { - if(!tmpl_valid(env, fdef/*, exp*/)) - return NULL; - if(exp->args && !exp->args->type) - return NULL; - const Func f = fdef->base->func; +ANN static Func ensure_tmpl(const Env env, const Func_Def fdef, + Exp_Call *const exp) { + if (!tmpl_valid(env, fdef /*, exp*/)) return NULL; + if (exp->args && !exp->args->type) return NULL; + const Func f = fdef->base->func; const Func next = f->next; - f->next = NULL; - const Tmpl tmpl = { .list=fdef->base->tmpl->list, .call=exp->tmpl->call }; - CHECK_BO(template_push_types(env, &tmpl));; + f->next = NULL; + const Tmpl tmpl = {.list = fdef->base->tmpl->list, .call = exp->tmpl->call}; + CHECK_BO(template_push_types(env, &tmpl)); + ; const Func func = find_func_match(env, f, exp); nspc_pop_type(env->gwion->mp, env->curr); f->next = next; - if(func) { + if (func) { set_fflag(func, fflag_tmpl | fflag_valid); call_add_effect(env, func, exp->func->pos); } return func; } -ANN static inline Func ensure_fptr(const Env env, struct ResolverArgs* ra, const Fptr_Def fptr) { +ANN static inline Func ensure_fptr(const Env env, struct ResolverArgs *ra, + const Fptr_Def fptr) { CHECK_BO(traverse_fptr_def(env, fptr)); return find_func_match(env, fptr->base->func, ra->e); } -ANN static Func fptr_match(const Env env, struct ResolverArgs* ra) { - const Value v = ra->v; - const Symbol sym = func_symbol(env, v->from->owner->name, v->name, ra->tmpl_name, 0); +ANN static Func fptr_match(const Env env, struct ResolverArgs *ra) { + const Value v = ra->v; + const Symbol sym = + func_symbol(env, v->from->owner->name, v->name, ra->tmpl_name, 0); const Type exists = nspc_lookup_type0(v->from->owner, sym); - if(exists) - return exists->info->func; - const Func_Def base = v->d.func_ref ? v->d.func_ref->def : ra->e->func->type->info->func->def; - const Tmpl tmpl = { .list=base->base->tmpl->list, .call=ra->types }; - CHECK_BO(template_push_types(env, &tmpl));; + if (exists) return exists->info->func; + const Func_Def base = + v->d.func_ref ? v->d.func_ref->def : ra->e->func->type->info->func->def; + const Tmpl tmpl = {.list = base->base->tmpl->list, .call = ra->types}; + CHECK_BO(template_push_types(env, &tmpl)); + ; Func_Base *const fbase = cpy_func_base(env->gwion->mp, base->base); - fbase->xid = sym; - fbase->tmpl->base = 0; - fbase->tmpl->call = cpy_type_list(env->gwion->mp, ra->types); - const Fptr_Def fptr = new_fptr_def(env->gwion->mp, fbase); - const Func m_func = ensure_fptr(env, ra, fptr); - if(m_func) - nspc_add_type_front(v->from->owner, sym, actual_type(env->gwion, m_func->value_ref->type)); - if(fptr->type) - type_remref(fptr->type, env->gwion); + fbase->xid = sym; + fbase->tmpl->base = 0; + fbase->tmpl->call = cpy_type_list(env->gwion->mp, ra->types); + const Fptr_Def fptr = new_fptr_def(env->gwion->mp, fbase); + const Func m_func = ensure_fptr(env, ra, fptr); + if (m_func) + nspc_add_type_front(v->from->owner, sym, + actual_type(env->gwion, m_func->value_ref->type)); + if (fptr->type) type_remref(fptr->type, env->gwion); free_fptr_def(env->gwion->mp, fptr); nspc_pop_type(env->gwion->mp, env->curr); return m_func; } -ANN static Func tmpl_exists(const Env env, struct ResolverArgs* ra, const Value exists) { - if(env->func == exists->d.func_ref) +ANN static Func tmpl_exists(const Env env, struct ResolverArgs *ra, + const Value exists) { + if (env->func == exists->d.func_ref) return find_func_match(env, env->func, ra->e) ? env->func : NULL; return ensure_tmpl(env, exists->d.func_ref->def, ra->e); } -ANN static Func create_tmpl(const Env env, struct ResolverArgs* ra, const m_uint i) { +ANN static Func create_tmpl(const Env env, struct ResolverArgs *ra, + const m_uint i) { DECL_OO(const Value, value, = template_get_ready(env, ra->v, "template", i)); - const Func_Def fdef = (Func_Def)cpy_func_def(env->gwion->mp, value->d.func_ref->def); - if(vflag(ra->v, vflag_builtin)) - set_vflag(value, vflag_builtin); + const Func_Def fdef = + (Func_Def)cpy_func_def(env->gwion->mp, value->d.func_ref->def); + if (vflag(ra->v, vflag_builtin)) set_vflag(value, vflag_builtin); fdef->base->tmpl->call = cpy_type_list(env->gwion->mp, ra->types); fdef->base->tmpl->base = i; - const Func func = ensure_tmpl(env, fdef, ra->e); - if(!func && !fdef->base->func) - free_func_def(env->gwion->mp, fdef); - if(func && vflag(ra->v, vflag_builtin)) + const Func func = ensure_tmpl(env, fdef, ra->e); + if (!func && !fdef->base->func) free_func_def(env->gwion->mp, fdef); + if (func && vflag(ra->v, vflag_builtin)) set_vflag(func->value_ref, vflag_builtin); return func; } -ANN static Func func_match(const Env env, struct ResolverArgs* ra) { - for(m_uint i = 0; i < ra->v->from->offset + 1; ++i) { +ANN static Func func_match(const Env env, struct ResolverArgs *ra) { + for (m_uint i = 0; i < ra->v->from->offset + 1; ++i) { const Value exists = template_get_ready(env, ra->v, ra->tmpl_name, i); - const Func func = exists ? - tmpl_exists(env, ra, exists) : create_tmpl(env, ra, i); - if(func) - return func; + const Func func = + exists ? tmpl_exists(env, ra, exists) : create_tmpl(env, ra, i); + if (func) return func; } return NULL; } -ANN static Func find_tmpl(const Env env, const Value v, Exp_Call *const exp, const m_str tmpl_name) { - const Type_List types = exp->tmpl->call; - const Func former = env->func; - const m_uint scope = env->scope->depth; - struct EnvSet es = { .env=env, .data=env, .func=(_exp_func)check_cdef, - .scope=scope, .flag=tflag_check }; - struct ResolverArgs ra = {.v = v, .e = exp, .tmpl_name = tmpl_name, .types = types}; +ANN static Func find_tmpl(const Env env, const Value v, Exp_Call *const exp, + const m_str tmpl_name) { + const Type_List types = exp->tmpl->call; + const Func former = env->func; + const m_uint scope = env->scope->depth; + struct EnvSet es = {.env = env, + .data = env, + .func = (_exp_func)check_cdef, + .scope = scope, + .flag = tflag_check}; + struct ResolverArgs ra = { + .v = v, .e = exp, .tmpl_name = tmpl_name, .types = types}; CHECK_BO(envset_pushv(&es, v)); (void)env_push(env, v->from->owner_class, v->from->owner); - if(v->from->owner_class && v->from->owner_class->info->cdef->base.tmpl) + if (v->from->owner_class && v->from->owner_class->info->cdef->base.tmpl) (void)template_push_types(env, v->from->owner_class->info->cdef->base.tmpl); - const Func m_func = !is_fptr(env->gwion, v->type) ? - func_match(env, &ra) : fptr_match(env, &ra); - if(v->from->owner_class && v->from->owner_class->info->cdef->base.tmpl) + const Func m_func = !is_fptr(env->gwion, v->type) ? func_match(env, &ra) + : fptr_match(env, &ra); + if (v->from->owner_class && v->from->owner_class->info->cdef->base.tmpl) nspc_pop_type(env->gwion->mp, env->curr); env_pop(env, scope); - if(es.run) - envset_pop(&es, v->from->owner_class); + if (es.run) envset_pop(&es, v->from->owner_class); env->func = former; return m_func; } -ANN static Func __find_template_match(const Env env, const Value v, Exp_Call *const exp) { - DECL_OO(const m_str, tmpl_name, = tl2str(env->gwion, exp->tmpl->call, exp->func->pos)); +ANN static Func __find_template_match(const Env env, const Value v, + Exp_Call *const exp) { + DECL_OO(const m_str, tmpl_name, + = tl2str(env->gwion, exp->tmpl->call, exp->func->pos)); const Func f = find_tmpl(env, v, exp, tmpl_name); free_mstr(env->gwion->mp, tmpl_name); return f; } -ANN static Func _find_template_match(const Env env, const Value v, Exp_Call *const exp) { +ANN static Func _find_template_match(const Env env, const Value v, + Exp_Call *const exp) { DECL_OO(const Func, f, = __find_template_match(env, v, exp)); - Type_List tl = exp->tmpl->call; + Type_List tl = exp->tmpl->call; Specialized_List sl = f->def->base->tmpl->list; - while(tl) { + while (tl) { DECL_OO(const Type, t, = known_type(env, tl->td)); - if(miss_traits(t, sl)) - return NULL; + if (miss_traits(t, sl)) return NULL; tl = tl->next; } return f; } -ANN static inline m_bool check_call(const Env env, const Exp_Call* exp) { +ANN static inline m_bool check_call(const Env env, const Exp_Call *exp) { const ae_exp_t et = exp->func->exp_type; - if(et != ae_exp_primary && et != ae_exp_dot && et != ae_exp_cast) + if (et != ae_exp_primary && et != ae_exp_dot && et != ae_exp_cast) ERR_B(exp->func->pos, _("invalid expression for function call.")) return GW_OK; } -ANN Func find_template_match(const Env env, const Value value, Exp_Call *const exp) { +ANN Func find_template_match(const Env env, const Value value, + Exp_Call *const exp) { CHECK_BO(check_call(env, exp)); const Func f = _find_template_match(env, value, exp); - if(f) - return f; + if (f) return f; Type t = value->from->owner_class; - while(t && t->nspc) { - const Func_Def fdef = value->d.func_ref ? value->d.func_ref->def : value->type->info->func->def; - const Value v = nspc_lookup_value0(t->nspc, fdef->base->xid); - if(v) { + while (t && t->nspc) { + const Func_Def fdef = value->d.func_ref ? value->d.func_ref->def + : value->type->info->func->def; + const Value v = nspc_lookup_value0(t->nspc, fdef->base->xid); + if (v) { const Func f = _find_template_match(env, v, exp); - if(f) - return f; + if (f) return f; } t = t->info->parent; } diff --git a/src/parse/operator.c b/src/parse/operator.c index b1e20632..ece58a17 100644 --- a/src/parse/operator.c +++ b/src/parse/operator.c @@ -13,26 +13,24 @@ typedef Type (*f_type)(const Env env, const Exp exp); -typedef struct M_Operator_{ - Type lhs, rhs, ret; - f_instr instr; - Func func; - opck ck; - opem em; +typedef struct M_Operator_ { + Type lhs, rhs, ret; + f_instr instr; + Func func; + opck ck; + opem em; struct Vector_ effect; } M_Operator; ANN void free_op_map(Map map, struct Gwion_ *gwion) { LOOP_OPTIM - for(m_uint i = map_size(map) + 1; --i;) { + for (m_uint i = map_size(map) + 1; --i;) { const restrict Vector v = (Vector)&VVAL(map, (vtype)i - 1); LOOP_OPTIM - for(m_uint j = vector_size(v) + 1; --j;) { - M_Operator *const mop = (M_Operator*)vector_at(v, j - 1); - if(mop->effect.ptr) - vector_release(&mop->effect); + for (m_uint j = vector_size(v) + 1; --j;) { + M_Operator *const mop = (M_Operator *)vector_at(v, j - 1); + if (mop->effect.ptr) vector_release(&mop->effect); mp_free(gwion->mp, M_Operator, mop); - } vector_release(v); } @@ -44,185 +42,193 @@ static m_str type_name(const Type t) { } static m_bool op_match(const restrict Type t, const restrict Type mo) { - if(t == OP_ANY_TYPE || mo == OP_ANY_TYPE) - return GW_OK; + if (t == OP_ANY_TYPE || mo == OP_ANY_TYPE) return GW_OK; return t == mo; } -ANN2(1) static M_Operator* operator_find(const Vector v, const restrict Type lhs, const restrict Type rhs) { - for(m_uint i = vector_size(v) + 1; --i;) { - M_Operator* mo = (M_Operator*)vector_at(v, i - 1); - if(!mo) - continue; - if(op_match(lhs, mo->lhs) && op_match(rhs, mo->rhs)) - return mo; +ANN2(1) +static M_Operator *operator_find(const Vector v, const restrict Type lhs, + const restrict Type rhs) { + for (m_uint i = vector_size(v) + 1; --i;) { + M_Operator *mo = (M_Operator *)vector_at(v, i - 1); + if (!mo) continue; + if (op_match(lhs, mo->lhs) && op_match(rhs, mo->rhs)) return mo; } return NULL; } -ANN2(1) static M_Operator* operator_find2(const Vector v, const restrict Type lhs, const restrict Type rhs) { - for(m_uint i = vector_size(v) + 1; --i;) { - M_Operator* mo = (M_Operator*)vector_at(v, i - 1); - if(mo && lhs == mo->lhs && rhs == mo->rhs) - return mo; +ANN2(1) +static M_Operator *operator_find2(const Vector v, const restrict Type lhs, + const restrict Type rhs) { + for (m_uint i = vector_size(v) + 1; --i;) { + M_Operator *mo = (M_Operator *)vector_at(v, i - 1); + if (mo && lhs == mo->lhs && rhs == mo->rhs) return mo; } return NULL; } ANN void operator_suspend(const Nspc n, struct Op_Import *opi) { - const m_int idx = map_index(&n->info->op_map, (vtype)opi->op); - const Vector v = (Vector)&VVAL(&n->info->op_map, idx); - for(m_uint i = vector_size(v) + 1; --i;) { - M_Operator* mo = (M_Operator*)vector_at(v, i - 1); - if(opi->lhs == mo->lhs && opi->rhs == mo->rhs) { - opi->data = (uintptr_t)mo; - opi->ret = (Type)&VPTR(v, i-1); - VPTR(v, i-1) = 0; + const m_int idx = map_index(&n->info->op_map, (vtype)opi->op); + const Vector v = (Vector)&VVAL(&n->info->op_map, idx); + for (m_uint i = vector_size(v) + 1; --i;) { + M_Operator *mo = (M_Operator *)vector_at(v, i - 1); + if (opi->lhs == mo->lhs && opi->rhs == mo->rhs) { + opi->data = (uintptr_t)mo; + opi->ret = (Type)&VPTR(v, i - 1); + VPTR(v, i - 1) = 0; break; } } } -ANN static M_Operator* new_mo(MemPool p, const struct Op_Import* opi) { - M_Operator* mo = mp_calloc(p, M_Operator); - mo->lhs = opi->lhs; - mo->rhs = opi->rhs; - mo->ret = opi->ret; - mo->instr = (f_instr)opi->data; - if(opi->func) { - mo->ck = opi->func->ck; - mo->em = opi->func->em; +ANN static M_Operator *new_mo(MemPool p, const struct Op_Import *opi) { + M_Operator *mo = mp_calloc(p, M_Operator); + mo->lhs = opi->lhs; + mo->rhs = opi->rhs; + mo->ret = opi->ret; + mo->instr = (f_instr)opi->data; + if (opi->func) { + mo->ck = opi->func->ck; + mo->em = opi->func->em; mo->effect.ptr = opi->func->effect.ptr; } return mo; } struct OpChecker { - const Env env; - const Map map; - const struct Op_Import* opi; - struct Vector_ effect; + const Env env; + const Map map; + const struct Op_Import *opi; + struct Vector_ effect; }; -__attribute__((returns_nonnull)) -ANN static Vector op_vector(const struct OpChecker *ock) { +__attribute__((returns_nonnull)) ANN static Vector +op_vector(const struct OpChecker *ock) { const m_int idx = map_index(ock->map, (vtype)ock->opi->op); - if(idx > -1) - return (Vector)&VVAL(ock->map, idx); + if (idx > -1) return (Vector)&VVAL(ock->map, idx); map_set(ock->map, (vtype)ock->opi->op, 0); const Vector create = (Vector)&VVAL(ock->map, VLEN(ock->map) - 1); vector_init(create); return create; } -ANN static m_bool _op_exist(const struct OpChecker* ock, const Nspc n) { +ANN static m_bool _op_exist(const struct OpChecker *ock, const Nspc n) { const m_int idx = map_index(&n->info->op_map, (vtype)ock->opi->op); - if(idx == -1 || !operator_find2((Vector)&VVAL(ock->map, idx), ock->opi->lhs, ock->opi->rhs)) + if (idx == -1 || !operator_find2((Vector)&VVAL(ock->map, idx), ock->opi->lhs, + ock->opi->rhs)) return GW_OK; - env_err(ock->env, ock->opi->pos, _("operator '%s', for type '%s' and '%s' already imported"), - s_name(ock->opi->op), type_name(ock->opi->lhs), type_name(ock->opi->rhs)); + env_err(ock->env, ock->opi->pos, + _("operator '%s', for type '%s' and '%s' already imported"), + s_name(ock->opi->op), type_name(ock->opi->lhs), + type_name(ock->opi->rhs)); return GW_ERROR; } -ANN static m_bool op_exist(const struct OpChecker* ock, const Nspc n) { +ANN static m_bool op_exist(const struct OpChecker *ock, const Nspc n) { return n->info->op_map.ptr ? _op_exist(ock, n) : GW_OK; } -ANN m_bool add_op(const Gwion gwion, const struct Op_Import* opi) { +ANN m_bool add_op(const Gwion gwion, const struct Op_Import *opi) { Nspc n = gwion->env->curr; do { - struct OpChecker ock = { .env=gwion->env, .map=&n->info->op_map, .opi=opi }; + struct OpChecker ock = { + .env = gwion->env, .map = &n->info->op_map, .opi = opi}; CHECK_BB(op_exist(&ock, n)); - } while((n = n->parent)); - if(!gwion->env->curr->info->op_map.ptr) + } while ((n = n->parent)); + if (!gwion->env->curr->info->op_map.ptr) map_init(&gwion->env->curr->info->op_map); - struct OpChecker ock = { .env=gwion->env, .map=&gwion->env->curr->info->op_map, .opi=opi }; - const Vector v = op_vector(&ock); - const M_Operator* mo = new_mo(gwion->mp, opi); + struct OpChecker ock = { + .env = gwion->env, .map = &gwion->env->curr->info->op_map, .opi = opi}; + const Vector v = op_vector(&ock); + const M_Operator *mo = new_mo(gwion->mp, opi); vector_add(v, (vtype)mo); return GW_OK; } ANN static inline Type op_parent(const Env env, const Type t) { const size_t depth = t->array_depth; - return !depth || !array_base(t)->info->parent ? - t->info->parent : - array_type(env, array_base(t)->info->parent, depth); + return !depth || !array_base(t)->info->parent + ? t->info->parent + : array_type(env, array_base(t)->info->parent, depth); } -ANN static Type op_check_inner(const Env env, struct OpChecker* ock, const uint i) { +ANN static Type op_check_inner(const Env env, struct OpChecker *ock, + const uint i) { Type t, r = ock->opi->rhs; do { - const M_Operator* mo; - const m_int idx = map_index(ock->map, (vtype)ock->opi->op); - if(idx > -1 && (mo = !i ? - operator_find2((Vector)&VVAL(ock->map, idx), ock->opi->lhs, r) : - operator_find( (Vector)&VVAL(ock->map, idx), ock->opi->lhs, r))) { - if((mo->ck && (t = mo->ck(ock->env, (void*)ock->opi->data)))) { + const M_Operator *mo; + const m_int idx = map_index(ock->map, (vtype)ock->opi->op); + if (idx > -1 && (mo = !i ? operator_find2((Vector)&VVAL(ock->map, idx), + ock->opi->lhs, r) + : operator_find((Vector)&VVAL(ock->map, idx), + ock->opi->lhs, r))) { + if ((mo->ck && (t = mo->ck(ock->env, (void *)ock->opi->data)))) { ock->effect.ptr = mo->effect.ptr; return t; } else return mo->ret; } - } while(r && (r = op_parent(env, r))); + } while (r && (r = op_parent(env, r))); return NULL; } -ANN Type op_check(const Env env, struct Op_Import* opi) { -for(int i = 0; i < 2; ++i) { - Nspc nspc = env->curr; - do { - if(!nspc->info->op_map.ptr) - continue; - Type l = opi->lhs; +ANN Type op_check(const Env env, struct Op_Import *opi) { + for (int i = 0; i < 2; ++i) { + Nspc nspc = env->curr; do { - struct Op_Import opi2 = { .op=opi->op, .lhs=l, .rhs=opi->rhs, .data=opi->data }; - struct OpChecker ock = { .env=env, .map=&nspc->info->op_map, .opi=&opi2 }; - const Type ret = op_check_inner(env, &ock, i); - if(ret) { - if(ret == env->gwion->type[et_error]) - return NULL; - if(ock.effect.ptr) { - const Vector base = &ock.effect; - for(m_uint i = 0; i < vector_size(base); i++) - env_add_effect(env, (Symbol)vector_at(base, i), opi->pos); + if (!nspc->info->op_map.ptr) continue; + Type l = opi->lhs; + do { + struct Op_Import opi2 = { + .op = opi->op, .lhs = l, .rhs = opi->rhs, .data = opi->data}; + struct OpChecker ock = { + .env = env, .map = &nspc->info->op_map, .opi = &opi2}; + const Type ret = op_check_inner(env, &ock, i); + if (ret) { + if (ret == env->gwion->type[et_error]) return NULL; + if (ock.effect.ptr) { + const Vector base = &ock.effect; + for (m_uint i = 0; i < vector_size(base); i++) + env_add_effect(env, (Symbol)vector_at(base, i), opi->pos); + } + opi->nspc = nspc; + return ret; } - opi->nspc = nspc; - return ret; - } - } while(l && (l = op_parent(env, l))); - } while((nspc = nspc->parent)); -} + } while (l && (l = op_parent(env, l))); + } while ((nspc = nspc->parent)); + } // this should be an any case - if(opi->op == insert_symbol(env->gwion->st, "$") && opi->rhs == opi->lhs) + if (opi->op == insert_symbol(env->gwion->st, "$") && opi->rhs == opi->lhs) return opi->rhs; - if(opi->op == insert_symbol(env->gwion->st, "@func_check")) - return NULL; - if(opi->op == insert_symbol(env->gwion->st, "@class_check")) + if (opi->op == insert_symbol(env->gwion->st, "@func_check")) return NULL; + if (opi->op == insert_symbol(env->gwion->st, "@class_check")) return env->gwion->type[et_error]; - if(opi->op != insert_symbol(env->gwion->st, "@implicit")) + if (opi->op != insert_symbol(env->gwion->st, "@implicit")) env_err(env, opi->pos, _("%s %s %s: no match found for operator"), - type_name(opi->lhs), s_name(opi->op), type_name(opi->rhs)); + type_name(opi->lhs), s_name(opi->op), type_name(opi->rhs)); return NULL; } -ANN m_bool operator_set_func(const struct Op_Import* opi) { - const Nspc nspc = ((Func)opi->data)->value_ref->from->owner; - const m_int idx = map_index(&nspc->info->op_map, (vtype)opi->op); - const Vector v = (Vector)&VVAL(&nspc->info->op_map, idx); - DECL_OB(M_Operator*, mo, = operator_find(v, opi->lhs, opi->rhs)); +ANN m_bool operator_set_func(const struct Op_Import *opi) { + const Nspc nspc = ((Func)opi->data)->value_ref->from->owner; + const m_int idx = map_index(&nspc->info->op_map, (vtype)opi->op); + const Vector v = (Vector)&VVAL(&nspc->info->op_map, idx); + DECL_OB(M_Operator *, mo, = operator_find(v, opi->lhs, opi->rhs)); mo->func = (Func)opi->data; return GW_OK; } -ANN static m_bool handle_instr(const Emitter emit, const M_Operator* mo) { - if(mo->func) { - const Instr push = emit_add_instr(emit, mo->func->code ? RegPushImm : SetFunc); - push->m_val = ((m_uint)mo->func->code ?:(m_uint)mo->func); +ANN static m_bool handle_instr(const Emitter emit, const M_Operator *mo) { + if (mo->func) { + const Instr push = + emit_add_instr(emit, mo->func->code ? RegPushImm : SetFunc); + push->m_val = ((m_uint)mo->func->code ?: (m_uint)mo->func); CHECK_BB(emit_exp_call1(emit, mo->func, true)); - if(mo->func->def->base->xid == insert_symbol(emit->gwion->st, "@conditional")) + if (mo->func->def->base->xid == + insert_symbol(emit->gwion->st, "@conditional")) emit_add_instr(emit, BranchEqInt); - else if(mo->func->def->base->xid == insert_symbol(emit->gwion->st, "@unconditional")) + else if (mo->func->def->base->xid == + insert_symbol(emit->gwion->st, "@unconditional")) emit_add_instr(emit, BranchNeqInt); return GW_OK; } @@ -230,47 +236,46 @@ ANN static m_bool handle_instr(const Emitter emit, const M_Operator* mo) { return GW_OK; } -ANN m_bool op_emit(const Emitter emit, const struct Op_Import* opi) { - for(int i = 0; i < 2; ++i) { - Nspc nspc = emit->env->curr; - do { - if(!nspc->info->op_map.ptr)continue; - Type l = opi->lhs; +ANN m_bool op_emit(const Emitter emit, const struct Op_Import *opi) { + for (int i = 0; i < 2; ++i) { + Nspc nspc = emit->env->curr; do { - Type r = opi->rhs; + if (!nspc->info->op_map.ptr) continue; + Type l = opi->lhs; do { - const m_int idx = map_index(&nspc->info->op_map, (vtype)opi->op); - if(idx == -1) - continue; - const Vector v = (Vector)&VVAL(&nspc->info->op_map, idx); - const M_Operator* mo = !i ? operator_find2(v, l, r) :operator_find(v, l, r); - if(mo) { - if(mo->em) { - const m_bool ret = mo->em(emit, (void*)opi->data); - if(ret) - return ret; - } else if(mo->func || mo->instr) - return handle_instr(emit, mo); - } - } while(r && (r = op_parent(emit->env, r))); - } while(l && (l = op_parent(emit->env, l))); - } while((nspc = nspc->parent)); + Type r = opi->rhs; + do { + const m_int idx = map_index(&nspc->info->op_map, (vtype)opi->op); + if (idx == -1) continue; + const Vector v = (Vector)&VVAL(&nspc->info->op_map, idx); + const M_Operator *mo = + !i ? operator_find2(v, l, r) : operator_find(v, l, r); + if (mo) { + if (mo->em) { + const m_bool ret = mo->em(emit, (void *)opi->data); + if (ret) return ret; + } else if (mo->func || mo->instr) + return handle_instr(emit, mo); + } + } while (r && (r = op_parent(emit->env, r))); + } while (l && (l = op_parent(emit->env, l))); + } while ((nspc = nspc->parent)); } return GW_ERROR; } #define CONVERT(t) t != from ? t : to -ANN static M_Operator* cpy_mo(MemPool p, M_Operator *const base, - const Type from, const Type to) { - M_Operator* mo = mp_calloc(p, M_Operator); - mo->lhs = CONVERT(base->lhs); - mo->rhs = CONVERT(base->rhs); - mo->ret = CONVERT(base->ret); - mo->instr = base->instr; - mo->func = base->func; - mo->ck = base->ck; - mo->em = base->em; - if(base->effect.ptr) { +ANN static M_Operator *cpy_mo(MemPool p, M_Operator *const base, + const Type from, const Type to) { + M_Operator *mo = mp_calloc(p, M_Operator); + mo->lhs = CONVERT(base->lhs); + mo->rhs = CONVERT(base->rhs); + mo->ret = CONVERT(base->ret); + mo->instr = base->instr; + mo->func = base->func; + mo->ck = base->ck; + mo->em = base->em; + if (base->effect.ptr) { vector_init(&mo->effect); vector_copy2(&base->effect, &mo->effect); } @@ -280,46 +285,43 @@ ANN static M_Operator* cpy_mo(MemPool p, M_Operator *const base, ANN static inline Map ensure_map(const Nspc nspc) { const Map map = &nspc->info->op_map; - if(!map->ptr) - map_init(map); + if (!map->ptr) map_init(map); return map; } ANN static inline Vector ensure_vec(const Map map, const m_uint key) { const m_int idx = map_index(map, key); - if(idx > -1) - return (Vector)&VVAL(map, idx); + if (idx > -1) return (Vector)&VVAL(map, idx); map_set(map, key, 0); const Vector v = (Vector)&VVAL(map, VLEN(map) - 1); vector_init(v); return v; } -ANN static void op_visit(const MemPool mp, const Nspc nspc, const struct Op_Import* opi, const Vector visited) { - if(vector_find(visited, (m_uint)nspc) != -1) - return; +ANN static void op_visit(const MemPool mp, const Nspc nspc, + const struct Op_Import *opi, const Vector visited) { + if (vector_find(visited, (m_uint)nspc) != -1) return; vector_add(visited, (m_uint)nspc); - if(nspc->info->op_map.ptr) { - const Map map = &nspc->info->op_map; + if (nspc->info->op_map.ptr) { + const Map map = &nspc->info->op_map; const Map base_map = ensure_map(opi->rhs->info->value->from->owner); - for(m_uint i = 0; i < map_size(map); i++) { - const Vector v = (Vector)&VVAL(map, i); + for (m_uint i = 0; i < map_size(map); i++) { + const Vector v = (Vector)&VVAL(map, i); const m_uint sz = vector_size(v); - for(m_uint j = 0; j < sz; j++) { - M_Operator *const mo = (M_Operator*)vector_at(v, j); - if(opi->lhs == mo->lhs || opi->lhs == mo->rhs || opi->lhs == mo->ret) { - const M_Operator* tmp = cpy_mo(mp, mo, opi->lhs, opi->rhs); - const Vector target = ensure_vec(base_map, VKEY(map, i)); + for (m_uint j = 0; j < sz; j++) { + M_Operator *const mo = (M_Operator *)vector_at(v, j); + if (opi->lhs == mo->lhs || opi->lhs == mo->rhs || opi->lhs == mo->ret) { + const M_Operator *tmp = cpy_mo(mp, mo, opi->lhs, opi->rhs); + const Vector target = ensure_vec(base_map, VKEY(map, i)); vector_add(target, (vtype)tmp); } } } } - if(nspc->parent) - op_visit(mp, nspc->parent, opi, visited); + if (nspc->parent) op_visit(mp, nspc->parent, opi, visited); } -ANN void op_cpy(const Env env, const struct Op_Import* opi) { +ANN void op_cpy(const Env env, const struct Op_Import *opi) { struct Vector_ visited; vector_init(&visited); op_visit(env->gwion->mp, opi->rhs->info->value->from->owner, opi, &visited); diff --git a/src/parse/scan0.c b/src/parse/scan0.c index 21958549..685bb306 100644 --- a/src/parse/scan0.c +++ b/src/parse/scan0.c @@ -17,27 +17,27 @@ static inline void add_type(const Env env, const Nspc nspc, const Type t) { } static inline void context_global(const Env env) { - if(env->context) - env->context->global = true; + if (env->context) env->context->global = true; } static inline Type scan0_type(const Env env, const m_str name, const Type t) { return new_type(env->gwion->mp, name, t); } -ANN static inline m_bool scan0_defined(const Env env, const Symbol s, const loc_t pos) { - if(nspc_lookup_type1(env->curr, s)) +ANN static inline m_bool scan0_defined(const Env env, const Symbol s, + const loc_t pos) { + if (nspc_lookup_type1(env->curr, s)) ERR_B(pos, _("type '%s' already defined"), s_name(s)); return already_defined(env, s, pos); } ANN static void fptr_assign(const Fptr_Def fptr) { const Func_Def def = fptr->type->info->func->def; - if(GET_FLAG(fptr->base, global)) { + if (GET_FLAG(fptr->base, global)) { SET_FLAG(fptr->value, global); SET_FLAG(fptr->base->func, global); SET_FLAG(def->base, global); - } else if(!GET_FLAG(fptr->base, static)) { + } else if (!GET_FLAG(fptr->base, static)) { set_vflag(fptr->value, vflag_member); set_vflag(fptr->base->func->value_ref, vflag_member); def->stack_depth += SZ_INT; @@ -46,40 +46,37 @@ ANN static void fptr_assign(const Fptr_Def fptr) { SET_FLAG(fptr->base->func, static); SET_FLAG(def->base, static); } - if(fbflag(def->base, fbflag_variadic)) - def->stack_depth += SZ_INT; + if (fbflag(def->base, fbflag_variadic)) def->stack_depth += SZ_INT; } static void fptr_def(const Env env, const Fptr_Def fptr) { - const Func_Def def = new_func_def(env->gwion->mp, - cpy_func_base(env->gwion->mp, fptr->base), - NULL); + const Func_Def def = new_func_def( + env->gwion->mp, cpy_func_base(env->gwion->mp, fptr->base), NULL); fptr->base->func = new_func(env->gwion->mp, s_name(fptr->base->xid), def); - fptr->value->d.func_ref = fptr->base->func; + fptr->value->d.func_ref = fptr->base->func; fptr->base->func->value_ref = fptr->value; - fptr->type->info->func = fptr->base->func; - def->base->func = fptr->base->func; + fptr->type->info->func = fptr->base->func; + def->base->func = fptr->base->func; } ANN m_bool scan0_fptr_def(const Env env, const Fptr_Def fptr) { CHECK_BB(env_access(env, fptr->base->flag, fptr->base->td->pos)); - CHECK_BB(scan0_defined(env, fptr->base->xid, fptr->base->td->pos));; - const m_str name = s_name(fptr->base->xid); - const Type t = scan0_type(env, name, env->gwion->type[et_fptr]); - const bool global = !env->class_def && GET_FLAG(fptr->base, global); + CHECK_BB(scan0_defined(env, fptr->base->xid, fptr->base->td->pos)); + ; + const m_str name = s_name(fptr->base->xid); + const Type t = scan0_type(env, name, env->gwion->type[et_fptr]); + const bool global = !env->class_def && GET_FLAG(fptr->base, global); t->flag |= fptr->base->flag; fptr->type = t; - if(global) { + if (global) { context_global(env); env_push_global(env); } fptr->value = mk_class(env, t, fptr->base->pos); - if(global) - env_pop(env, 0); + if (global) env_pop(env, 0); valuefrom(env, fptr->value->from, fptr->base->pos); fptr_def(env, fptr); - if(env->class_def) - fptr_assign(fptr); + if (env->class_def) fptr_assign(fptr); set_vflag(fptr->value, vflag_func); add_type(env, t->info->value->from->owner, t); type_addref(t); @@ -87,139 +84,147 @@ ANN m_bool scan0_fptr_def(const Env env, const Fptr_Def fptr) { } static OP_CHECK(opck_implicit_similar) { - const struct Implicit *imp = (struct Implicit*)data; + const struct Implicit *imp = (struct Implicit *)data; return imp->e->type; } static OP_CHECK(opck_cast_similar) { - const Exp_Cast *cast = (Exp_Cast*)data; + const Exp_Cast *cast = (Exp_Cast *)data; return exp_self(cast)->type; } -ANN /*static */void scan0_implicit_similar(const Env env, const Type lhs, const Type rhs) { - struct Op_Func opfunc = { .ck=opck_cast_similar }; - struct Op_Import opi = { .op=insert_symbol("$"), .lhs=lhs, .rhs=rhs, .func=&opfunc }; +ANN /*static */ void scan0_implicit_similar(const Env env, const Type lhs, + const Type rhs) { + struct Op_Func opfunc = {.ck = opck_cast_similar}; + struct Op_Import opi = { + .op = insert_symbol("$"), .lhs = lhs, .rhs = rhs, .func = &opfunc}; add_op(env->gwion, &opi); - opi.lhs=rhs; - opi.rhs=lhs; + opi.lhs = rhs; + opi.rhs = lhs; add_op(env->gwion, &opi); opfunc.ck = opck_implicit_similar; - opi.op=insert_symbol("@implicit"); + opi.op = insert_symbol("@implicit"); add_op(env->gwion, &opi); } -ANN static void scan0_explicit_distinct(const Env env, const Type lhs, const Type rhs) { - struct Op_Func opfunc = { .ck=opck_cast_similar }; - struct Op_Import opi = { .op=insert_symbol("$"), .lhs=lhs, .rhs=rhs, .func=&opfunc }; +ANN static void scan0_explicit_distinct(const Env env, const Type lhs, + const Type rhs) { + struct Op_Func opfunc = {.ck = opck_cast_similar}; + struct Op_Import opi = { + .op = insert_symbol("$"), .lhs = lhs, .rhs = rhs, .func = &opfunc}; add_op(env->gwion, &opi); } -ANN static void typedef_simple(const Env env, const Type_Def tdef, const Type base) { - const Type t = scan0_type(env, s_name(tdef->xid), base); - t->size = base->size; - const Nspc nspc = (!env->class_def && GET_FLAG(tdef->ext, global)) ? - env->global_nspc : env->curr; - if(GET_FLAG(tdef->ext, global)) - context_global(env); +ANN static void typedef_simple(const Env env, const Type_Def tdef, + const Type base) { + const Type t = scan0_type(env, s_name(tdef->xid), base); + t->size = base->size; + const Nspc nspc = (!env->class_def && GET_FLAG(tdef->ext, global)) + ? env->global_nspc + : env->curr; + if (GET_FLAG(tdef->ext, global)) context_global(env); add_type(env, nspc, t); tdef->type = t; - if(base->nspc) // create a new nspc if `distinct`? + if (base->nspc) // create a new nspc if `distinct`? nspc_addref((t->nspc = base->nspc)); t->flag = tdef->ext->flag; - if(tdef->ext->array && !tdef->ext->array->exp) - set_tflag(t, tflag_empty); + if (tdef->ext->array && !tdef->ext->array->exp) set_tflag(t, tflag_empty); mk_class(env, tdef->type, tdef->pos); } -ANN static m_bool typedef_complex(const Env env, const Type_Def tdef, const Type base) { - const ae_flag flag = base->info->cdef ? base->info->cdef->flag : 0; +ANN static m_bool typedef_complex(const Env env, const Type_Def tdef, + const Type base) { + const ae_flag flag = base->info->cdef ? base->info->cdef->flag : 0; const Class_Def cdef = new_class_def(env->gwion->mp, flag, tdef->xid, - cpy_type_decl(env->gwion->mp, tdef->ext), NULL, tdef->ext->pos); + cpy_type_decl(env->gwion->mp, tdef->ext), + NULL, tdef->ext->pos); CHECK_BB(scan0_class_def(env, cdef)); - tdef->type = cdef->base.type; - cdef->base.tmpl = tdef->tmpl;// check cpy + tdef->type = cdef->base.type; + cdef->base.tmpl = tdef->tmpl; // check cpy mk_class(env, tdef->type, tdef->pos); return GW_OK; } -ANN static void typedef_fptr(const Env env, const Type_Def tdef, const Type base) { - tdef->type = type_copy(env->gwion->mp, base); - tdef->type->info->func = base->info->func; - tdef->type->name = s_name(tdef->xid); +ANN static void typedef_fptr(const Env env, const Type_Def tdef, + const Type base) { + tdef->type = type_copy(env->gwion->mp, base); + tdef->type->info->func = base->info->func; + tdef->type->name = s_name(tdef->xid); tdef->type->info->parent = base; add_type(env, env->curr, tdef->type); mk_class(env, tdef->type, tdef->pos); - if(base->info->func->def->base->tmpl) - set_tflag(tdef->type, tflag_ftmpl); + if (base->info->func->def->base->tmpl) set_tflag(tdef->type, tflag_ftmpl); } ANN m_bool scan0_type_def(const Env env, const Type_Def tdef) { CHECK_BB(env_access(env, tdef->ext->flag, tdef->ext->pos)); - DECL_OB(const Type, base, = tdef->tmpl ? find_type(env, tdef->ext) : known_type(env, tdef->ext)); + DECL_OB(const Type, base, = tdef->tmpl ? find_type(env, tdef->ext) + : known_type(env, tdef->ext)); CHECK_BB(scan0_defined(env, tdef->xid, tdef->ext->pos)); - const bool global = GET_FLAG(tdef->ext, global); // TODO: handle global in class - if(global) { + const bool global = + GET_FLAG(tdef->ext, global); // TODO: handle global in class + if (global) { context_global(env); env_push_global(env); } - if(isa(base, env->gwion->type[et_function]) < 0) { - if(!tdef->ext->types && (!tdef->ext->array || !tdef->ext->array->exp)) + if (isa(base, env->gwion->type[et_function]) < 0) { + if (!tdef->ext->types && (!tdef->ext->array || !tdef->ext->array->exp)) typedef_simple(env, tdef, base); else CHECK_BB(typedef_complex(env, tdef, base)); } else typedef_fptr(env, tdef, base); - if(!tdef->distinct && !tdef->when) + if (!tdef->distinct && !tdef->when) scan0_implicit_similar(env, base, tdef->type); - if(tdef->distinct) { + if (tdef->distinct) { tdef->type->info->parent = base->info->parent; - if(base->info->gack) + if (base->info->gack) vmcode_addref(tdef->type->info->gack = base->info->gack); set_tflag(tdef->type, tflag_distinct); - struct Op_Import opi = { .lhs=base, .rhs=tdef->type }; + struct Op_Import opi = {.lhs = base, .rhs = tdef->type}; op_cpy(env, &opi); scan0_explicit_distinct(env, base, tdef->type); type_addref(tdef->type); // maybe because of scope_iter in nspc_free_values } else set_tflag(tdef->type, tflag_typedef); - if(global) - env_pop(env, 0); + if (global) env_pop(env, 0); tdef->type->info->base_type = base; return GW_OK; } ANN static Symbol scan0_sym(const Env env, const m_str name, const loc_t pos) { const size_t line_len = num_digit(pos.first.line); - const size_t col_len = num_digit(pos.first.column); - char c[strlen(env->curr->name) + strlen(env->name) + line_len + col_len + strlen(name) + 6]; + const size_t col_len = num_digit(pos.first.column); + char c[strlen(env->curr->name) + strlen(env->name) + line_len + col_len + + strlen(name) + 6]; sprintf(c, "@%s:%s:%s:%u:%u", name, env->name, env->curr->name, - pos.first.line, pos.first.column); + pos.first.line, pos.first.column); return insert_symbol(c); } -#define scan0_nspc(env, a) \ +#define scan0_nspc(env, a) \ GET_FLAG(a, global) ? !env->class_def ? env->global_nspc : NULL : env->curr ANN static Type enum_type(const Env env, const Enum_Def edef) { - const Type t = type_copy(env->gwion->mp, env->gwion->type[et_int]); - const Symbol sym = scan0_sym(env, "enum", edef->pos); - t->name = edef->xid ? s_name(edef->xid) : s_name(sym); - t->info->parent = env->gwion->type[et_int]; + const Type t = type_copy(env->gwion->mp, env->gwion->type[et_int]); + const Symbol sym = scan0_sym(env, "enum", edef->pos); + t->name = edef->xid ? s_name(edef->xid) : s_name(sym); + t->info->parent = env->gwion->type[et_int]; const bool global = GET_FLAG(edef, global); // TODO: handle global in class - if(global) { + if (global) { context_global(env); env_push_global(env); } add_type(env, env->curr, t); mk_class(env, t, edef->pos); - if(global) - env_pop(env, 0); -// scan0_implicit_similar(env, t, env->gwion->type[et_int]); + if (global) env_pop(env, 0); + // scan0_implicit_similar(env, t, env->gwion->type[et_int]); return t; } -ANN static inline m_bool scan0_global(const Env env, const ae_flag flag, const loc_t pos) { - if(!env->class_def || !((flag & ae_flag_global) == ae_flag_global)) +ANN static inline m_bool scan0_global(const Env env, const ae_flag flag, + const loc_t pos) { + if (!env->class_def || !((flag & ae_flag_global) == ae_flag_global)) return GW_OK; ERR_B(pos, _("can't declare as global in class def")) } @@ -230,58 +235,53 @@ ANN m_bool scan0_enum_def(const Env env, const Enum_Def edef) { CHECK_BB(scan0_global(env, edef->flag, edef->pos)); edef->t = enum_type(env, edef); vector_init(&edef->values); - if(GET_FLAG(edef, global)) - context_global(env); + if (GET_FLAG(edef, global)) context_global(env); return GW_OK; } ANN static Type union_type(const Env env, const Symbol s, const loc_t loc) { const m_str name = s_name(s); - const Type t = new_type(env->gwion->mp, name, env->gwion->type[et_union]); - t->nspc = new_nspc(env->gwion->mp, name); - t->nspc->parent = env->curr; - t->info->tuple = new_tupleform(env->gwion->mp, NULL); // ??? + const Type t = new_type(env->gwion->mp, name, env->gwion->type[et_union]); + t->nspc = new_nspc(env->gwion->mp, name); + t->nspc->parent = env->curr; + t->info->tuple = new_tupleform(env->gwion->mp, NULL); // ??? add_type(env, env->curr, t); mk_class(env, t, loc); SET_FLAG(t, final); - if(strncmp(t->name, "Option", 6)) - SET_FLAG(t, abstract); + if (strncmp(t->name, "Option", 6)) SET_FLAG(t, abstract); return t; } ANN static void union_tmpl(const Env env, const Union_Def udef) { - if(tmpl_base(udef->tmpl)) { - const Union_Def u = cpy_union_def(env->gwion->mp, udef); - u->type = udef->type; + if (tmpl_base(udef->tmpl)) { + const Union_Def u = cpy_union_def(env->gwion->mp, udef); + u->type = udef->type; udef->type->info->udef = u; set_tflag(u->type, tflag_tmpl); set_tflag(u->type, tflag_udef); } -// if(GET_FLAG(udef, global)) -// SET_FLAG(udef->type, global); + // if(GET_FLAG(udef, global)) + // SET_FLAG(udef->type, global); } ANN m_bool scan0_union_def(const Env env, const Union_Def udef) { CHECK_BB(env_storage(env, udef->flag, udef->pos)); CHECK_BB(scan0_global(env, udef->flag, udef->pos)); const bool global = GET_FLAG(udef, global); // TODO: handle global in class - if(global) { + if (global) { context_global(env); env_push_global(env); } - if(GET_FLAG(udef, global)) - context_global(env); + if (GET_FLAG(udef, global)) context_global(env); CHECK_BB(scan0_defined(env, udef->xid, udef->pos)); - udef->type = union_type(env, udef->xid, udef->pos); + udef->type = union_type(env, udef->xid, udef->pos); Union_List l = udef->l; do udef->type->nspc->info->offset += SZ_INT; - while((l = l->next)); + while ((l = l->next)); udef->type->nspc->info->offset += SZ_INT; SET_ACCESS(udef, udef->type); - if(udef->tmpl) - union_tmpl(env, udef); - if(global) - env_pop(env, 0); + if (udef->tmpl) union_tmpl(env, udef); + if (global) env_pop(env, 0); set_tflag(udef->type, tflag_scan0); return GW_OK; } @@ -294,17 +294,15 @@ ANN static m_bool scan0_class_def_pre(const Env env, const Class_Def cdef) { } ANN static void cdef_flag(const Class_Def cdef, const Type t) { - if(cdef->base.tmpl) - set_tflag(t, tflag_tmpl); - if(cdef->base.ext && cdef->base.ext->array) - set_tflag(t, tflag_typedef); + if (cdef->base.tmpl) set_tflag(t, tflag_tmpl); + if (cdef->base.ext && cdef->base.ext->array) set_tflag(t, tflag_typedef); } ANN static Type get_parent_base(const Env env, Type_Decl *td) { DECL_OO(const Type, t, = find_type(env, td)); Type owner = env->class_def; - while(owner) { - if(t == owner) + while (owner) { + if (t == owner) ERR_O(td->pos, _("'%s' as parent inside itself\n."), owner->name); owner = owner->info->value->from->owner_class; } @@ -313,66 +311,59 @@ ANN static Type get_parent_base(const Env env, Type_Decl *td) { ANN static inline Type scan0_final(const Env env, Type_Decl *td) { DECL_OO(const Type, t, = known_type(env, td)); - if(!GET_FLAG(t, final)) - return t; + if (!GET_FLAG(t, final)) return t; ERR_O(td->pos, _("can't inherit from final parent class '%s'\n."), t->name); } ANN static Type cdef_parent(const Env env, const Class_Def cdef) { - if(cflag(cdef, cflag_struct)) - return env->gwion->type[et_compound]; - if(!cdef->base.ext) - return env->gwion->type[et_object]; - if(tmpl_base(cdef->base.tmpl)) - return get_parent_base(env, cdef->base.ext); + if (cflag(cdef, cflag_struct)) return env->gwion->type[et_compound]; + if (!cdef->base.ext) return env->gwion->type[et_object]; + if (tmpl_base(cdef->base.tmpl)) return get_parent_base(env, cdef->base.ext); const bool tmpl = !!cdef->base.tmpl; - if(tmpl) - template_push_types(env, cdef->base.tmpl); + if (tmpl) template_push_types(env, cdef->base.tmpl); const Type t = scan0_final(env, cdef->base.ext); - if(tmpl) - nspc_pop_type(env->gwion->mp, env->curr); + if (tmpl) nspc_pop_type(env->gwion->mp, env->curr); return t ?: (Type)GW_ERROR; } ANN static m_bool find_traits(const Env env, ID_List traits, const loc_t pos) { do { - if(!nspc_lookup_trait1(env->curr, traits->xid)) + if (!nspc_lookup_trait1(env->curr, traits->xid)) ERR_B(pos, _("can't find trait")); - } while((traits = traits->next)); + } while ((traits = traits->next)); return GW_OK; } ANN static Type scan0_class_def_init(const Env env, const Class_Def cdef) { CHECK_BO(scan0_defined(env, cdef->base.xid, cdef->pos)); const Type parent = cdef_parent(env, cdef); - if(parent == (Type)GW_ERROR) - return NULL; - if(cdef->traits) - CHECK_BO(find_traits(env, cdef->traits, cdef->pos)); + if (parent == (Type)GW_ERROR) return NULL; + if (cdef->traits) CHECK_BO(find_traits(env, cdef->traits, cdef->pos)); const Type t = scan0_type(env, s_name(cdef->base.xid), parent); - if(cflag(cdef, cflag_struct)) { + if (cflag(cdef, cflag_struct)) { t->size = 0; set_tflag(t, tflag_struct); } - t->info->tuple = new_tupleform(env->gwion->mp, parent); - t->nspc = new_nspc(env->gwion->mp, t->name); + t->info->tuple = new_tupleform(env->gwion->mp, parent); + t->nspc = new_nspc(env->gwion->mp, t->name); t->nspc->parent = env->curr; - t->info->cdef = cdef; + t->info->cdef = cdef; t->flag |= cdef->flag; -// add_type(env, t->info->value->from->owner, t); + // add_type(env, t->info->value->from->owner, t); cdef_flag(cdef, t); - if(cdef->base.ext && cdef->base.ext->array) - set_tflag(t, tflag_typedef); + if (cdef->base.ext && cdef->base.ext->array) set_tflag(t, tflag_typedef); return t; } ANN static m_bool scan0_stmt_list(const Env env, Stmt_List list) { - do if(list->stmt->stmt_type == ae_stmt_pp) { - if(list->stmt->d.stmt_pp.pp_type == ae_pp_include) - env->name = list->stmt->d.stmt_pp.data; - else if(list->stmt->d.stmt_pp.pp_type == ae_pp_import) - CHECK_BB(plugin_ini(env->gwion, list->stmt->d.stmt_pp.data)); - } while((list = list->next)); + do + if (list->stmt->stmt_type == ae_stmt_pp) { + if (list->stmt->d.stmt_pp.pp_type == ae_pp_include) + env->name = list->stmt->d.stmt_pp.data; + else if (list->stmt->d.stmt_pp.pp_type == ae_pp_import) + CHECK_BB(plugin_ini(env->gwion, list->stmt->d.stmt_pp.data)); + } + while ((list = list->next)); return GW_OK; } @@ -380,9 +371,8 @@ ANN static m_bool scan0_stmt_list(const Env env, Stmt_List list) { ANN static m_bool fdef_defaults(const Func_Def fdef) { Arg_List list = fdef->base->args; - while(list) { - if(list->exp) - return true; + while (list) { + if (list->exp) return true; list = list->next; } return false; @@ -390,40 +380,43 @@ ANN static m_bool fdef_defaults(const Func_Def fdef) { ANN static Exp arglist2exp(MemPool p, Arg_List arg, const Exp default_arg) { Exp exp = new_prim_id(p, arg->var_decl->xid, arg->var_decl->pos); - if(arg->next) + if (arg->next) exp->next = arglist2exp(p, arg->next, default_arg); else exp->next = cpy_exp(p, default_arg); return exp; } -ANN static Ast scan0_func_def_default(const MemPool p, const Ast ast, const Ast next) { +ANN static Ast scan0_func_def_default(const MemPool p, const Ast ast, + const Ast next) { const Func_Def base_fdef = ast->section->d.func_def; - Arg_List base_arg = base_fdef->base->args, former = NULL; - while(base_arg) { - if(!base_arg->next && base_arg->exp) { - if(former) - former->next = NULL; + Arg_List base_arg = base_fdef->base->args, former = NULL; + while (base_arg) { + if (!base_arg->next && base_arg->exp) { + if (former) former->next = NULL; // use cpy_func_base? - Func_Base *base = new_func_base(p, cpy_type_decl(p, base_fdef->base->td), - base_fdef->base->xid, former ? cpy_arg_list(p, base_fdef->base->args) : NULL, - base_fdef->base->flag, base_fdef->base->pos); - const Exp efunc = new_prim_id(p, base_fdef->base->xid, base_fdef->base->pos); - Exp arg_exp = former ? arglist2exp(p, base_fdef->base->args, base_arg->exp) : - cpy_exp(p, base_arg->exp); - const Exp ecall = new_exp_call(p, efunc, arg_exp, base_fdef->base->pos); - const Stmt code = new_stmt_exp(p, ae_stmt_return, ecall, base_fdef->base->pos); + Func_Base *base = new_func_base( + p, cpy_type_decl(p, base_fdef->base->td), base_fdef->base->xid, + former ? cpy_arg_list(p, base_fdef->base->args) : NULL, + base_fdef->base->flag, base_fdef->base->pos); + const Exp efunc = + new_prim_id(p, base_fdef->base->xid, base_fdef->base->pos); + Exp arg_exp = former + ? arglist2exp(p, base_fdef->base->args, base_arg->exp) + : cpy_exp(p, base_arg->exp); + const Exp ecall = new_exp_call(p, efunc, arg_exp, base_fdef->base->pos); + const Stmt code = + new_stmt_exp(p, ae_stmt_return, ecall, base_fdef->base->pos); const Stmt_List slist = new_stmt_list(p, code, NULL); - const Stmt body = new_stmt_code(p, slist, base_fdef->base->pos); - const Func_Def fdef = new_func_def(p, base, body); - Section *new_section = new_section_func_def(p, fdef); - if(former) - former->next = base_arg; + const Stmt body = new_stmt_code(p, slist, base_fdef->base->pos); + const Func_Def fdef = new_func_def(p, base, body); + Section * new_section = new_section_func_def(p, fdef); + if (former) former->next = base_arg; const Ast tmp_ast = new_ast(p, new_section, NULL); - ast->next = scan0_func_def_default(p, tmp_ast, next); + ast->next = scan0_func_def_default(p, tmp_ast, next); return ast; } - former = base_arg; + former = base_arg; base_arg = base_arg->next; } ast->next = next; @@ -434,32 +427,32 @@ ANN static Ast scan0_func_def_default(const MemPool p, const Ast ast, const Ast ANN static m_bool scan0_extend_def(const Env env, const Extend_Def xdef) { DECL_OB(const Type, t, = known_type(env, xdef->td)); - if(isa(t, env->gwion->type[et_compound]) < 0) + if (isa(t, env->gwion->type[et_compound]) < 0) ERR_B(xdef->td->pos, _("only compound types can be extended")) - if(GET_FLAG(t, final)) // TODO: add type initial declaration + if (GET_FLAG(t, final)) // TODO: add type initial declaration ERR_B(xdef->td->pos, _("can't extend final type")) Ast ast = xdef->body; do { - if(ast->section->section_type == ae_section_func && - GET_FLAG(ast->section->d.func_def->base, abstract)) - ERR_B(ast->section->d.func_def->base->pos, _("can't use {/+}abstract{0} functions in {+/}extends{0}")) - } while((ast = ast->next)); + if (ast->section->section_type == ae_section_func && + GET_FLAG(ast->section->d.func_def->base, abstract)) + ERR_B(ast->section->d.func_def->base->pos, + _("can't use {/+}abstract{0} functions in {+/}extends{0}")) + } while ((ast = ast->next)); xdef->t = t; return GW_OK; } ANN static m_bool _scan0_trait_def(const Env env, const Trait_Def pdef) { const Trait trait = new_trait(env->gwion->mp, pdef->pos); - trait->loc = pdef->pos; - trait->filename = env->name; + trait->loc = pdef->pos; + trait->filename = env->name; nspc_add_trait(env->curr, pdef->xid, trait); Ast ast = pdef->body; - while(ast) { - Section * section = ast->section; - if(section->section_type == ae_section_func) { + while (ast) { + Section *section = ast->section; + if (section->section_type == ae_section_func) { const Func_Def fdef = section->d.func_def; - if(!trait->requested_funcs.ptr) - vector_init(&trait->requested_funcs); + if (!trait->requested_funcs.ptr) vector_init(&trait->requested_funcs); vector_add(&trait->requested_funcs, (m_uint)fdef); } ast = ast->next; @@ -468,16 +461,15 @@ ANN static m_bool _scan0_trait_def(const Env env, const Trait_Def pdef) { } ANN static m_bool scan0_trait_def(const Env env, const Trait_Def pdef) { - const Symbol s = pdef->xid; - const Trait exists = nspc_lookup_trait1(env->curr, s); - if(exists) { + const Symbol s = pdef->xid; + const Trait exists = nspc_lookup_trait1(env->curr, s); + if (exists) { gwerr_basic("trait already defined", NULL, NULL, env->name, pdef->pos, 0); gwerr_secondary("defined here", env->name, exists->loc); env->context->error = true; return already_defined(env, s, pdef->pos); } - if(pdef->traits) - CHECK_BB(find_traits(env, pdef->traits, pdef->pos)); + if (pdef->traits) CHECK_BB(find_traits(env, pdef->traits, pdef->pos)); _scan0_trait_def(env, pdef); return GW_OK; } @@ -490,25 +482,25 @@ ANN static m_bool scan0_class_def_inner(const Env env, const Class_Def cdef) { set_tflag(cdef->base.type, tflag_scan0); (void)mk_class(env, cdef->base.type, cdef->pos); add_type(env, cdef->base.type->info->value->from->owner, cdef->base.type); - if(cdef->body) - CHECK_BB(env_body(env, cdef, scan0_section)); + if (cdef->body) CHECK_BB(env_body(env, cdef, scan0_section)); return GW_OK; } ANN m_bool scan0_class_def(const Env env, const Class_Def c) { - const int cpy = tmpl_base(c->base.tmpl) || GET_FLAG(c, global); + const int cpy = tmpl_base(c->base.tmpl) || GET_FLAG(c, global); const Class_Def cdef = !cpy ? c : cpy_class_def(env->gwion->mp, c); - if(GET_FLAG(cdef, global)) { // could be updated + if (GET_FLAG(cdef, global)) { // could be updated vector_add(&env->scope->nspc_stack, (vtype)env->curr); env->curr = env->global_nspc; context_global(env); } - const m_bool ret = scan0_class_def_pre(env, cdef) > 0 ? - scan0_class_def_inner(env, cdef) : GW_ERROR; - if(GET_FLAG(cdef, global)) + const m_bool ret = scan0_class_def_pre(env, cdef) > 0 + ? scan0_class_def_inner(env, cdef) + : GW_ERROR; + if (GET_FLAG(cdef, global)) env->curr = (Nspc)vector_pop(&env->scope->nspc_stack); - if(cpy && cdef->base.type) { - c->base.type = cdef->base.type; + if (cpy && cdef->base.type) { + c->base.type = cdef->base.type; c->base.type->info->cdef = cdef; set_tflag(c->base.type, tflag_cdef); } @@ -518,12 +510,12 @@ ANN m_bool scan0_class_def(const Env env, const Class_Def c) { ANN m_bool scan0_ast(const Env env, Ast ast) { do { CHECK_BB(scan0_section(env, ast->section)); - if(ast->section->section_type != ae_section_func || !fdef_defaults(ast->section->d.func_def)) + if (ast->section->section_type != ae_section_func || + !fdef_defaults(ast->section->d.func_def)) continue; const Ast next = ast->next; scan0_func_def_default(env->gwion->mp, ast, next); ast = next; - } - while((ast = ast->next)); + } while ((ast = ast->next)); return GW_OK; } diff --git a/src/parse/scan1.c b/src/parse/scan1.c index ef20822c..b3309684 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -10,61 +10,66 @@ ANN static m_bool scan1_stmt_list(const Env env, Stmt_List list); ANN static m_bool scan1_stmt(const Env env, Stmt stmt); -ANN static inline m_bool type_cyclic(const Env env, const Type t, const Type_Decl *td) { +ANN static inline m_bool type_cyclic(const Env env, const Type t, + const Type_Decl *td) { Type owner = env->class_def; do { Type parent = t; - while(parent) { - if(parent == owner) + while (parent) { + if (parent == owner) ERR_B(td->pos, _("%s declared inside %s"), t->name, owner->name); parent = parent->info->parent; } - } while((owner = owner->info->value->from->owner_class)); + } while ((owner = owner->info->value->from->owner_class)); return GW_OK; } ANN static inline m_bool ensure_scan1(const Env env, const Type t) { - if(tflag(t, tflag_scan1) || !(tflag(t, tflag_cdef) || tflag(t, tflag_udef))) + if (tflag(t, tflag_scan1) || !(tflag(t, tflag_cdef) || tflag(t, tflag_udef))) return GW_OK; - struct EnvSet es = { .env=env, .data=env, .func=(_exp_func)scan1_cdef, - .scope=env->scope->depth, .flag=tflag_scan1 }; + struct EnvSet es = {.env = env, + .data = env, + .func = (_exp_func)scan1_cdef, + .scope = env->scope->depth, + .flag = tflag_scan1}; return envset_run(&es, t); } -ANN static Type scan1_type(const Env env, Type_Decl* td) { +ANN static Type scan1_type(const Env env, Type_Decl *td) { DECL_OO(const Type, t, = known_type(env, td)); - if(!env->func && env->class_def && !GET_FLAG(td, late)) + if (!env->func && env->class_def && !GET_FLAG(td, late)) CHECK_BO(type_cyclic(env, t, td)); CHECK_BO(ensure_scan1(env, t)); return t; } -ANN static Type void_type(const Env env, Type_Decl* td) { +ANN static Type void_type(const Env env, Type_Decl *td) { DECL_OO(const Type, type, = scan1_type(env, td)); - if(type->size) - return type; + if (type->size) return type; ERR_O(td->pos, _("cannot declare variables of size '0' (i.e. 'void')...")) } -ANN static Type scan1_exp_decl_type(const Env env, Exp_Decl* decl) { - if(decl->type) - return decl->type; - DECL_OO(const Type ,t, = void_type(env, decl->td)); - if(decl->td->xid == insert_symbol("auto") && decl->type) - return decl->type; - if(GET_FLAG(t, private) && t->info->value->from->owner != env->curr) +ANN static Type scan1_exp_decl_type(const Env env, Exp_Decl *decl) { + if (decl->type) return decl->type; + DECL_OO(const Type, t, = void_type(env, decl->td)); + if (decl->td->xid == insert_symbol("auto") && decl->type) return decl->type; + if (GET_FLAG(t, private) && t->info->value->from->owner != env->curr) ERR_O(exp_self(decl)->pos, _("can't use private type %s"), t->name) - if(GET_FLAG(t, protect) && (!env->class_def || isa(t, env->class_def) < 0)) + if (GET_FLAG(t, protect) && (!env->class_def || isa(t, env->class_def) < 0)) ERR_O(exp_self(decl)->pos, _("can't use protected type %s"), t->name) return decl->type = t; } static inline m_bool scan1_defined(const Env env, const Var_Decl var) { - if(var->value) // from an auto declaration + if (var->value) // from an auto declaration return GW_OK; - if(((!env->class_def || !GET_FLAG(env->class_def, final) || env->scope->depth) ? nspc_lookup_value1 : nspc_lookup_value2)(env->curr, var->xid)) - ERR_B(var->pos, _("variable %s has already been defined in the same scope..."), - s_name(var->xid)) + if (((!env->class_def || !GET_FLAG(env->class_def, final) || + env->scope->depth) + ? nspc_lookup_value1 + : nspc_lookup_value2)(env->curr, var->xid)) + ERR_B(var->pos, + _("variable %s has already been defined in the same scope..."), + s_name(var->xid)) return GW_OK; } @@ -72,260 +77,264 @@ static inline uint array_ref(const Array_Sub array) { return array && !array->exp; } -ANN static m_bool scan1_decl(const Env env, const Exp_Decl* decl) { - Var_Decl_List list = decl->list; - const uint decl_ref = array_ref(decl->td->array); +ANN static m_bool scan1_decl(const Env env, const Exp_Decl *decl) { + Var_Decl_List list = decl->list; + const uint decl_ref = array_ref(decl->td->array); do { const Var_Decl var = list->self; CHECK_BB(isres(env, var->xid, exp_self(decl)->pos)); Type t = decl->type; CHECK_BB(scan1_defined(env, var)); - if(var->array) { - if(var->array->exp) - CHECK_BB(scan1_exp(env, var->array->exp)); + if (var->array) { + if (var->array->exp) CHECK_BB(scan1_exp(env, var->array->exp)); CHECK_OB((t = array_type(env, decl->type, var->array->depth))); } - if(GET_FLAG(array_base(t), abstract) && ((var->array && var->array->exp) - || (decl->td->array && decl->td->array->exp))) + if (GET_FLAG(array_base(t), abstract) && + ((var->array && var->array->exp) || + (decl->td->array && decl->td->array->exp))) ERR_B(var->pos, _("arrays of abstract type '%s' must be declared empty"), - array_base(t)->name); - const Value v = var->value = var->value ?: new_value(env->gwion->mp, t, s_name(var->xid)); + array_base(t)->name); + const Value v = var->value = + var->value ?: new_value(env->gwion->mp, t, s_name(var->xid)); nspc_add_value(env->curr, var->xid, v); - if(GET_FLAG(t, abstract) && !GET_FLAG(decl->td, late)) - SET_FLAG(v, late); + if (GET_FLAG(t, abstract) && !GET_FLAG(decl->td, late)) SET_FLAG(v, late); v->type = t; - if(decl_ref || array_ref(var->array)) - SET_FLAG(v, late); + if (decl_ref || array_ref(var->array)) SET_FLAG(v, late); v->flag |= decl->td->flag; - if(!env->scope->depth) { + if (!env->scope->depth) { valuefrom(env, v->from, var->pos); - if(env->class_def) { - if(env->class_def->info->tuple) - tuple_contains(env, v); - if(!GET_FLAG(decl->td, static)) { + if (env->class_def) { + if (env->class_def->info->tuple) tuple_contains(env, v); + if (!GET_FLAG(decl->td, static)) { set_vflag(v, vflag_member); - if(tflag(env->class_def, tflag_struct)) { + if (tflag(env->class_def, tflag_struct)) { v->from->offset = env->class_def->size; env->class_def->size += t->size; } } } - set_vflag(v, vflag_fglobal);// file global - } else - if(GET_FLAG(decl->td, global)) + set_vflag(v, vflag_fglobal); // file global + } else if (GET_FLAG(decl->td, global)) SET_FLAG(v, global); - } while((list = list->next)); - ((Exp_Decl*)decl)->type = decl->list->self->value->type; + } while ((list = list->next)); + ((Exp_Decl *)decl)->type = decl->list->self->value->type; return GW_OK; } ANN int is_global(const Nspc nspc, Nspc global) { - do if(nspc == global) - return 1; - while((global = global->parent)); + do + if (nspc == global) return 1; + while ((global = global->parent)); return 0; } -ANN m_bool scan1_exp_decl(const Env env, const Exp_Decl* decl) { +ANN m_bool scan1_exp_decl(const Env env, const Exp_Decl *decl) { CHECK_BB(env_storage(env, decl->td->flag, exp_self(decl)->pos)); - ((Exp_Decl*)decl)->type = scan1_exp_decl_type(env, (Exp_Decl*)decl); + ((Exp_Decl *)decl)->type = scan1_exp_decl_type(env, (Exp_Decl *)decl); CHECK_OB(decl->type); const bool global = GET_FLAG(decl->td, global); - if(global) { - if(env->context) - env->context->global = true; - if(!is_global(decl->type->info->value->from->owner, env->global_nspc)) + if (global) { + if (env->context) env->context->global = true; + if (!is_global(decl->type->info->value->from->owner, env->global_nspc)) ERR_B(exp_self(decl)->pos, _("type '%s' is not global"), decl->type->name) } const m_uint scope = !global ? env->scope->depth : env_push_global(env); - const m_bool ret = scan1_decl(env, decl); - if(global) - env_pop(env, scope); + const m_bool ret = scan1_decl(env, decl); + if (global) env_pop(env, scope); return ret; } ANN static inline int opiscall(const Symbol sym) { const m_str opname = s_name(sym); return (opname[0] == '@' || opname[0] == '&') && - (isalpha(opname[1]) || opname[1] == '_'); + (isalpha(opname[1]) || opname[1] == '_'); } -ANN static inline Exp sym2func(const Env env, const Symbol sym, const loc_t pos) { - MemPool mp = env->gwion->mp; +ANN static inline Exp sym2func(const Env env, const Symbol sym, + const loc_t pos) { + MemPool mp = env->gwion->mp; const m_str name = s_name(sym); return new_prim_id(mp, insert_symbol(name + 1), pos); } -ANN static void binary_args(const Exp_Binary* bin) { +ANN static void binary_args(const Exp_Binary *bin) { Exp arg = bin->lhs; - while(arg->next) - arg = arg->next; + while (arg->next) arg = arg->next; arg->next = bin->rhs; } -ANN static m_bool exp2call(const Env env, const Exp e, const Symbol sym, const Exp args) { - e->exp_type = ae_exp_call; +ANN static m_bool exp2call(const Env env, const Exp e, const Symbol sym, + const Exp args) { + e->exp_type = ae_exp_call; e->d.exp_call.func = sym2func(env, sym, e->pos); e->d.exp_call.args = args; e->d.exp_call.tmpl = NULL; return scan1_exp(env, e); } -ANN static m_bool binary2call(const Env env, const Exp_Binary* bin) { +ANN static m_bool binary2call(const Env env, const Exp_Binary *bin) { binary_args(bin); return exp2call(env, exp_self(bin), bin->op, bin->lhs); } -ANN static inline m_bool scan1_exp_binary(const Env env, const Exp_Binary* bin) { - if(opiscall(bin->op)) - return binary2call(env, bin); +ANN static inline m_bool scan1_exp_binary(const Env env, + const Exp_Binary *bin) { + if (opiscall(bin->op)) return binary2call(env, bin); CHECK_BB(scan1_exp(env, bin->lhs)); return scan1_exp(env, bin->rhs); } ANN static m_bool scan1_range(const Env env, Range *range) { - if(range->start) - CHECK_BB(scan1_exp(env, range->start)); - if(range->end) - CHECK_BB(scan1_exp(env, range->end)); + if (range->start) CHECK_BB(scan1_exp(env, range->start)); + if (range->end) CHECK_BB(scan1_exp(env, range->end)); return GW_OK; } -ANN static inline m_bool scan1_prim(const Env env, const Exp_Primary* prim) { - if(prim->prim_type == ae_prim_hack || prim->prim_type == ae_prim_interp) +ANN static inline m_bool scan1_prim(const Env env, const Exp_Primary *prim) { + if (prim->prim_type == ae_prim_hack || prim->prim_type == ae_prim_interp) return scan1_exp(env, prim->d.exp); - if(prim->prim_type == ae_prim_array && prim->d.array->exp) + if (prim->prim_type == ae_prim_array && prim->d.array->exp) return scan1_exp(env, prim->d.array->exp); - if(prim->prim_type == ae_prim_range) - return scan1_range(env, prim->d.range); + if (prim->prim_type == ae_prim_range) return scan1_range(env, prim->d.range); return GW_OK; } -ANN static inline m_bool scan1_exp_array(const Env env, const Exp_Array* array) { +ANN static inline m_bool scan1_exp_array(const Env env, + const Exp_Array *array) { CHECK_BB(scan1_exp(env, array->base)); return scan1_exp(env, array->array->exp); } -ANN static inline m_bool scan1_exp_slice(const Env env, const Exp_Slice* range) { +ANN static inline m_bool scan1_exp_slice(const Env env, + const Exp_Slice *range) { CHECK_BB(scan1_exp(env, range->base)); return scan1_range(env, range->range); } -ANN static inline m_bool scan1_exp_cast(const Env env, const Exp_Cast* cast) { +ANN static inline m_bool scan1_exp_cast(const Env env, const Exp_Cast *cast) { return scan1_exp(env, cast->exp); } -ANN static m_bool scan1_exp_post(const Env env, const Exp_Postfix* post) { - if(opiscall(post->op)) { +ANN static m_bool scan1_exp_post(const Env env, const Exp_Postfix *post) { + if (opiscall(post->op)) { return exp2call(env, exp_self(post), post->op, post->exp); } CHECK_BB(scan1_exp(env, post->exp)); const m_str access = exp_access(post->exp); - if(!access) - return GW_OK; - ERR_B(post->exp->pos, _("post operator '%s' cannot be used" - " on %s data-type..."), s_name(post->op), access); + if (!access) return GW_OK; + ERR_B(post->exp->pos, + _("post operator '%s' cannot be used" + " on %s data-type..."), + s_name(post->op), access); } -ANN static m_bool scan1_exp_call(const Env env, const Exp_Call* exp_call) { - if(exp_call->tmpl) - return GW_OK; +ANN static m_bool scan1_exp_call(const Env env, const Exp_Call *exp_call) { + if (exp_call->tmpl) return GW_OK; CHECK_BB(scan1_exp(env, exp_call->func)); const Exp args = exp_call->args; return args ? scan1_exp(env, args) : GW_OK; } -ANN static inline m_bool scan1_exp_dot(const Env env, const Exp_Dot* member) { +ANN static inline m_bool scan1_exp_dot(const Env env, const Exp_Dot *member) { return scan1_exp(env, member->base); } -ANN static m_bool scan1_exp_if(const Env env, const Exp_If* exp_if) { +ANN static m_bool scan1_exp_if(const Env env, const Exp_If *exp_if) { CHECK_BB(scan1_exp(env, exp_if->cond)); CHECK_BB(scan1_exp(env, exp_if->if_exp ?: exp_if->cond)); return scan1_exp(env, exp_if->else_exp); } -ANN static inline m_bool scan1_exp_unary(const restrict Env env, const Exp_Unary *unary) { - if(unary->unary_type == unary_code) - { RET_NSPC(scan1_stmt(env, unary->code)) } - else if(opiscall(unary->op)) { +ANN static inline m_bool scan1_exp_unary(const restrict Env env, + const Exp_Unary * unary) { + if (unary->unary_type == unary_code) { + RET_NSPC(scan1_stmt(env, unary->code)) + } else if (opiscall(unary->op)) { return exp2call(env, exp_self(unary), unary->op, unary->exp); } return unary->unary_type == unary_exp ? scan1_exp(env, unary->exp) : GW_OK; } #define scan1_exp_lambda dummy_func -#define scan1_exp_td dummy_func +#define scan1_exp_td dummy_func HANDLE_EXP_FUNC(scan1, m_bool, Env) -ANN static inline m_bool _scan1_stmt_match_case(const restrict Env env, const Stmt_Match stmt) { +ANN static inline m_bool _scan1_stmt_match_case(const restrict Env env, + const Stmt_Match stmt) { CHECK_BB(scan1_exp(env, stmt->cond)); - if(stmt->when) - CHECK_BB(scan1_exp(env, stmt->when)); + if (stmt->when) CHECK_BB(scan1_exp(env, stmt->when)); return scan1_stmt_list(env, stmt->list); } -ANN static inline m_bool scan1_stmt_match_case(const restrict Env env, const Stmt_Match stmt) { - RET_NSPC(_scan1_stmt_match_case(env, stmt)) -} +ANN static inline m_bool scan1_stmt_match_case(const restrict Env env, + const Stmt_Match stmt) { + RET_NSPC(_scan1_stmt_match_case(env, stmt))} -ANN static inline m_bool _scan1_stmt_match(const restrict Env env, const Stmt_Match stmt) { - if(stmt->where) - CHECK_BB(scan1_stmt(env, stmt->where)); +ANN static inline m_bool + _scan1_stmt_match(const restrict Env env, const Stmt_Match stmt) { + if (stmt->where) CHECK_BB(scan1_stmt(env, stmt->where)); Stmt_List list = stmt->list; do CHECK_BB(scan1_stmt_match_case(env, &list->stmt->d.stmt_match)); - while((list = list->next)); + while ((list = list->next)); return GW_OK; } -ANN static inline m_bool scan1_stmt_match(const restrict Env env, const Stmt_Match stmt) { +ANN static inline m_bool scan1_stmt_match(const restrict Env env, + const Stmt_Match stmt) { CHECK_BB(scan1_exp(env, stmt->cond)); RET_NSPC(_scan1_stmt_match(env, stmt)) } - -ANN static inline m_bool scan1_handler_list(const restrict Env env, const Handler_List handler) { - if(handler->next) - CHECK_BB(scan1_handler_list(env, handler->next)); +ANN static inline m_bool scan1_handler_list(const restrict Env env, + const Handler_List handler) { + if (handler->next) CHECK_BB(scan1_handler_list(env, handler->next)); RET_NSPC(scan1_stmt(env, handler->stmt)) } -ANN static inline m_bool scan1_stmt_try(const restrict Env env, const Stmt_Try stmt) { +ANN static inline m_bool scan1_stmt_try(const restrict Env env, + const Stmt_Try stmt) { CHECK_BB(scan1_handler_list(env, stmt->handler)); RET_NSPC(scan1_stmt(env, stmt->stmt)) } -ANN static inline m_bool stmt_each_defined(const restrict Env env, const Stmt_Each stmt) { - if(nspc_lookup_value1(env->curr, stmt->sym)) - ERR_B(stmt_self(stmt)->pos, _("foreach value '%s' is already defined"), s_name(stmt->sym)) - if(stmt->idx && nspc_lookup_value1(env->curr, stmt->idx->sym)) - ERR_B(stmt_self(stmt)->pos, _("foreach index '%s' is already defined"), s_name(stmt->idx->sym)) +ANN static inline m_bool stmt_each_defined(const restrict Env env, + const Stmt_Each stmt) { + if (nspc_lookup_value1(env->curr, stmt->sym)) + ERR_B(stmt_self(stmt)->pos, _("foreach value '%s' is already defined"), + s_name(stmt->sym)) + if (stmt->idx && nspc_lookup_value1(env->curr, stmt->idx->sym)) + ERR_B(stmt_self(stmt)->pos, _("foreach index '%s' is already defined"), + s_name(stmt->idx->sym)) return GW_OK; } -ANN static inline m_bool shadow_err(const Env env, const Value v, const loc_t loc) { - gwerr_basic(_("shadowing a previously defined variable"), NULL, NULL, env->name, loc, 0); +ANN static inline m_bool shadow_err(const Env env, const Value v, + const loc_t loc) { + gwerr_basic(_("shadowing a previously defined variable"), NULL, NULL, + env->name, loc, 0); defined_here(v); env->context->error = true; return GW_ERROR; } -ANN static inline m_bool shadow_arg(const Env env, const Symbol sym, const loc_t loc) { +ANN static inline m_bool shadow_arg(const Env env, const Symbol sym, + const loc_t loc) { Nspc nspc = env->curr; do { const Value v = nspc_lookup_value0(nspc, sym); - if(v) - return shadow_err(env, v, loc); - } while((nspc = nspc->parent)); + if (v) return shadow_err(env, v, loc); + } while ((nspc = nspc->parent)); return GW_OK; } -ANN static inline m_bool shadow_var(const Env env, const Symbol sym, const loc_t loc) { +ANN static inline m_bool shadow_var(const Env env, const Symbol sym, + const loc_t loc) { const Value v = nspc_lookup_value1(env->curr, sym); return !v ? GW_OK : shadow_err(env, v, loc); } -#define describe_ret_nspc(name, type, prolog, exp) describe_stmt_func(scan1, name, type, prolog, exp) +#define describe_ret_nspc(name, type, prolog, exp) \ + describe_stmt_func(scan1, name, type, prolog, exp) describe_ret_nspc(flow, Stmt_Flow,, !(scan1_exp(env, stmt->cond) < 0 || scan1_stmt(env, stmt->body) < 0) ? 1 : -1) describe_ret_nspc(varloop, Stmt_VarLoop,, !(scan1_exp(env, stmt->exp) < 0 || @@ -344,8 +353,7 @@ describe_ret_nspc(if, Stmt_If,, !(scan1_exp(env, stmt->cond) < 0 || (stmt->else_body && scan1_stmt(env, stmt->else_body) < 0)) ? 1 : -1) ANN static inline m_bool scan1_stmt_code(const Env env, const Stmt_Code stmt) { - if(stmt->stmt_list) - { RET_NSPC(scan1_stmt_list(env, stmt->stmt_list)) } + if (stmt->stmt_list) { RET_NSPC(scan1_stmt_list(env, stmt->stmt_list)) } return GW_OK; } @@ -359,7 +367,7 @@ ANN m_bool scan1_enum_def(const Env env, const Enum_Def edef) { CHECK_BB(already_defined(env, list->xid, edef->pos)); const Value v = new_value(env->gwion->mp, edef->t, s_name(list->xid)); valuefrom(env, v->from, edef->pos); - if(env->class_def) { + if (env->class_def) { SET_FLAG(v, static); SET_ACCESS(edef, v) SET_ACCESS(edef, edef->t) @@ -369,18 +377,19 @@ ANN m_bool scan1_enum_def(const Env env, const Enum_Def edef) { set_vflag(v, vflag_valid); nspc_add_value(edef->t->info->value->from->owner, list->xid, v); vector_add(&edef->values, (vtype)v); - } while((list = list->next)); + } while ((list = list->next)); return GW_OK; } ANN static Value arg_value(const Env env, const Arg_List list) { const Var_Decl var = list->var_decl; - const Value v = new_value(env->gwion->mp, list->type, var->xid ? s_name(var->xid) : (m_str)__func__); - if(var->array) + const Value v = new_value(env->gwion->mp, list->type, + var->xid ? s_name(var->xid) : (m_str) __func__); + if (var->array) v->type = list->type = array_type(env, list->type, var->array->depth); - if(list->td) { + if (list->td) { v->flag = list->td->flag; -// SET_FLAG(v, global); ??? + // SET_FLAG(v, global); ??? } return v; } @@ -388,36 +397,33 @@ ANN static Value arg_value(const Env env, const Arg_List list) { ANN static m_bool scan1_args(const Env env, Arg_List list) { do { const Var_Decl var = list->var_decl; - if(var->xid) - CHECK_BB(isres(env, var->xid, var->pos)); - if(list->td) { + if (var->xid) CHECK_BB(isres(env, var->xid, var->pos)); + if (list->td) { SET_FLAG(list->td, late); CHECK_OB((list->type = void_type(env, list->td))); UNSET_FLAG(list->td, late); } var->value = arg_value(env, list); nspc_add_value(env->curr, var->xid, var->value); - } while((list = list->next)); + } while ((list = list->next)); return GW_OK; } ANN static Type scan1_noret(const Env env, const Func_Base *base) { DECL_OO(const Type, t, = known_type(env, base->td)); - if(!tflag(t, tflag_noret)) - return t; - ERR_O(base->pos, _("Can't use type `{+G}%s{0}` for return"), - t->name); + if (!tflag(t, tflag_noret)) return t; + ERR_O(base->pos, _("Can't use type `{+G}%s{0}` for return"), t->name); } ANN static m_bool _scan1_fdef_base_tmpl(const Env env, Func_Base *base) { Specialized_List id = base->tmpl->list; do nspc_add_type(env->curr, id->xid, env->gwion->type[et_auto]); - while((id = id->next)); + while ((id = id->next)); CHECK_OB((base->ret_type = scan1_noret(env, base))); - if(base->args) { + if (base->args) { Arg_List arg = base->args; do CHECK_OB(known_type(env, arg->td)); - while((arg = arg->next)); + while ((arg = arg->next)); } return GW_OK; } @@ -430,66 +436,62 @@ ANN static m_bool scan1_fdef_base_tmpl(const Env env, Func_Base *base) { } ANN m_bool scan1_fptr_def(const Env env, const Fptr_Def fptr) { - if(tmpl_base(fptr->base->tmpl)) - return scan1_fdef_base_tmpl(env, fptr->base); - if(!fptr->base->func) { - fptr->base->func = nspc_lookup_value0(env->curr, fptr->base->xid)->d.func_ref; + if (tmpl_base(fptr->base->tmpl)) return scan1_fdef_base_tmpl(env, fptr->base); + if (!fptr->base->func) { + fptr->base->func = + nspc_lookup_value0(env->curr, fptr->base->xid)->d.func_ref; fptr->type = nspc_lookup_type0(env->curr, fptr->base->xid); } const Func_Def fdef = fptr->base->func->def; CHECK_OB((fdef->base->ret_type = scan1_noret(env, fdef->base))); - if(!fdef->base->args) - return GW_OK; + if (!fdef->base->args) return GW_OK; RET_NSPC(scan1_args(env, fdef->base->args)) } ANN m_bool scan1_type_def(const Env env, const Type_Def tdef) { - if(!tdef->type) - tdef->type = nspc_lookup_type0(env->curr, tdef->xid); - if(tdef->when) - CHECK_BB(scan1_exp(env, tdef->when)); - return (!is_fptr(env->gwion, tdef->type) && tdef->type->info->cdef) ? - scan1_cdef(env, tdef->type) : GW_OK; + if (!tdef->type) tdef->type = nspc_lookup_type0(env->curr, tdef->xid); + if (tdef->when) CHECK_BB(scan1_exp(env, tdef->when)); + return (!is_fptr(env->gwion, tdef->type) && tdef->type->info->cdef) + ? scan1_cdef(env, tdef->type) + : GW_OK; } -ANN static inline m_bool scan1_union_def_inner_loop(const Env env, Union_Def udef) { +ANN static inline m_bool scan1_union_def_inner_loop(const Env env, + Union_Def udef) { nspc_allocdata(env->gwion->mp, udef->type->nspc); - Union_List l = udef->l; - m_uint sz = 0; + Union_List l = udef->l; + m_uint sz = 0; const Value v = new_value(env->gwion->mp, env->gwion->type[et_int], "@index"); nspc_add_value_front(env->curr, insert_symbol("@index"), v); valuefrom(env, v->from, udef->pos); do { DECL_OB(const Type, t, = known_type(env, l->td)); - if(nspc_lookup_value0(env->curr, l->xid)) + if (nspc_lookup_value0(env->curr, l->xid)) ERR_B(l->pos, _("'%s' already declared in union"), s_name(l->xid)) const Value v = new_value(env->gwion->mp, t, s_name(l->xid)); - if(!tflag(t, tflag_scan1)) // ??? - tuple_contains(env, v); // ??? + if (!tflag(t, tflag_scan1)) // ??? + tuple_contains(env, v); // ??? v->from->offset = SZ_INT; - valuefrom(env ,v->from, udef->pos); + valuefrom(env, v->from, udef->pos); nspc_add_value_front(env->curr, l->xid, v); - if(t->size > sz) - sz = t->size; - } while((l = l->next)); - udef->type->nspc->info->offset = SZ_INT +sz; + if (t->size > sz) sz = t->size; + } while ((l = l->next)); + udef->type->nspc->info->offset = SZ_INT + sz; return GW_OK; } ANN static m_bool scan1_union_def_inner(const Env env, const Union_Def udef) { - if(udef->tmpl && udef->tmpl->call) + if (udef->tmpl && udef->tmpl->call) CHECK_BB(template_push_types(env, udef->tmpl)); const m_bool ret = scan1_union_def_inner_loop(env, udef); - if(udef->tmpl && udef->tmpl->call) - nspc_pop_type(env->gwion->mp, env->curr); + if (udef->tmpl && udef->tmpl->call) nspc_pop_type(env->gwion->mp, env->curr); return ret; } ANN m_bool scan1_union_def(const Env env, const Union_Def udef) { - if(tmpl_base(udef->tmpl)) - return GW_OK; + if (tmpl_base(udef->tmpl)) return GW_OK; const m_uint scope = env_push_type(env, udef->type); - const m_bool ret = scan1_union_def_inner(env, udef); + const m_bool ret = scan1_union_def_inner(env, udef); env_pop(env, scope); set_tflag(udef->type, tflag_scan1); return ret; @@ -503,8 +505,7 @@ ANN m_bool scan1_union_def(const Env env, const Union_Def udef) { #define scan1_stmt_retry dummy_func ANN static m_bool scan1_stmt_pp(const Env env, const Stmt_PP stmt) { - if(stmt->pp_type == ae_pp_include) - env->name = stmt->data; + if (stmt->pp_type == ae_pp_include) env->name = stmt->data; return GW_OK; } @@ -521,115 +522,116 @@ ANN static inline m_bool scan1_stmt(const Env env, const Stmt stmt) { ANN static m_bool scan1_stmt_list(const Env env, Stmt_List l) { do { CHECK_BB(scan1_stmt(env, l->stmt)); - if(l->next) { - if(l->stmt->stmt_type != ae_stmt_return) { - if(l->next->stmt->stmt_type == ae_stmt_exp && - !l->next->stmt->d.stmt_exp.val) { - Stmt_List next = l->next; - l->next = l->next->next; - next->next = NULL; - free_stmt_list(env->gwion->mp, next); + if (l->next) { + if (l->stmt->stmt_type != ae_stmt_return) { + if (l->next->stmt->stmt_type == ae_stmt_exp && + !l->next->stmt->d.stmt_exp.val) { + Stmt_List next = l->next; + l->next = l->next->next; + next->next = NULL; + free_stmt_list(env->gwion->mp, next); } } else { Stmt_List tmp = l->next; - l->next = NULL; + l->next = NULL; free_stmt_list(env->gwion->mp, tmp); } } - } while((l = l->next)); + } while ((l = l->next)); return GW_OK; } ANN static m_bool class_internal(const Env env, const Func_Base *base) { - if(!env->class_def) + if (!env->class_def) ERR_B(base->td->pos, _("'%s' must be in class def!!"), s_name(base->xid)) - if(base->args) + if (base->args) ERR_B(base->td->pos, _("'%s' must not have args"), s_name(base->xid)) - if(base->ret_type != env->gwion->type[et_void]) + if (base->ret_type != env->gwion->type[et_void]) ERR_B(base->td->pos, _("'%s' must return 'void'"), s_name(base->xid)) return GW_OK; } -ANN static inline m_bool scan_internal_arg(const Env env, const Func_Base *base) { - if(base->args && !base->args->next) - return GW_OK; - ERR_B(base->td->pos, _("'%s' must have one (and only one) argument"), s_name(base->xid)) +ANN static inline m_bool scan_internal_arg(const Env env, + const Func_Base *base) { + if (base->args && !base->args->next) return GW_OK; + ERR_B(base->td->pos, _("'%s' must have one (and only one) argument"), + s_name(base->xid)) } -ANN static inline m_bool scan_internal_int(const Env env, const Func_Base *base) { - CHECK_BB(scan_internal_arg(env, base)); - if(isa(base->ret_type, env->gwion->type[et_int]) > 0) - return GW_OK; - ERR_B(base->td->pos, _("'%s' must return 'int'"), s_name(base->xid)) +ANN static inline m_bool scan_internal_int(const Env env, + const Func_Base *base) { + CHECK_BB(scan_internal_arg(env, base)); + if (isa(base->ret_type, env->gwion->type[et_int]) > 0) return GW_OK; + ERR_B(base->td->pos, _("'%s' must return 'int'"), s_name(base->xid)) } ANN static m_bool scan_internal(const Env env, const Func_Base *base) { const Symbol op = base->xid; - if(op == insert_symbol("@dtor") || op == insert_symbol("@gack")) + if (op == insert_symbol("@dtor") || op == insert_symbol("@gack")) return class_internal(env, base); - if(op == insert_symbol("@implicit")) - return scan_internal_arg(env, base); - if(op == insert_symbol("@conditional") || - op == insert_symbol("@unconditional")) + if (op == insert_symbol("@implicit")) return scan_internal_arg(env, base); + if (op == insert_symbol("@conditional") || + op == insert_symbol("@unconditional")) return scan_internal_int(env, base); return GW_OK; } ANN static m_bool scan1_fdef_args(const Env env, Arg_List list) { do CHECK_BB(shadow_arg(env, list->var_decl->xid, list->var_decl->pos)); - while((list = list->next)); + while ((list = list->next)); return GW_OK; } ANN m_bool scan1_fbody(const Env env, const Func_Def fdef) { - if(fdef->base->args) { + if (fdef->base->args) { CHECK_BB(scan1_fdef_args(env, fdef->base->args)); CHECK_BB(scan1_args(env, fdef->base->args)); } - if(fdef->d.code && fdef->d.code->d.stmt_code.stmt_list) + if (fdef->d.code && fdef->d.code->d.stmt_code.stmt_list) CHECK_BB(scan1_stmt_list(env, fdef->d.code->d.stmt_code.stmt_list)); return GW_OK; } ANN m_bool scan1_fdef(const Env env, const Func_Def fdef) { - if(fdef->base->td) + if (fdef->base->td) CHECK_OB((fdef->base->ret_type = scan1_noret(env, fdef->base))); - if(fbflag(fdef->base, fbflag_internal)) + if (fbflag(fdef->base, fbflag_internal)) CHECK_BB(scan_internal(env, fdef->base)); - else if(fbflag(fdef->base, fbflag_op) && env->class_def) + else if (fbflag(fdef->base, fbflag_op) && env->class_def) SET_FLAG(fdef->base, static); RET_NSPC(scan1_fbody(env, fdef)) return GW_OK; } -ANN static inline m_bool scan1_fdef_defined(const Env env, const Func_Def fdef) { +ANN static inline m_bool scan1_fdef_defined(const Env env, + const Func_Def fdef) { const Value v = nspc_lookup_value1(env->curr, fdef->base->xid); - if(!v) - return GW_OK; - if(isa(actual_type(env->gwion, v->type), env->gwion->type[et_function]) > 0) + if (!v) return GW_OK; + if (isa(actual_type(env->gwion, v->type), env->gwion->type[et_function]) > 0) return GW_OK; - if((!env->class_def || !GET_FLAG(env->class_def, final)) && !nspc_lookup_value0(env->curr, fdef->base->xid)) - ERR_B(fdef->base->pos, _("function '%s' has already been defined in the same scope..."), - s_name(fdef->base->xid)) + if ((!env->class_def || !GET_FLAG(env->class_def, final)) && + !nspc_lookup_value0(env->curr, fdef->base->xid)) + ERR_B(fdef->base->pos, + _("function '%s' has already been defined in the same scope..."), + s_name(fdef->base->xid)) return GW_OK; } ANN m_bool scan1_func_def(const Env env, const Func_Def fdef) { - const bool global = GET_FLAG(fdef->base, global); - const m_uint scope = !global ? env->scope->depth : env_push_global(env); - if(fdef->base->td) + const bool global = GET_FLAG(fdef->base, global); + const m_uint scope = !global ? env->scope->depth : env_push_global(env); + if (fdef->base->td) CHECK_BB(env_storage(env, fdef->base->flag, fdef->base->td->pos)); CHECK_BB(scan1_fdef_defined(env, fdef)); - if(tmpl_base(fdef->base->tmpl)) - return scan1_fdef_base_tmpl(env, fdef->base); - struct Func_ fake = { .name=s_name(fdef->base->xid) }, *const former = env->func; + if (tmpl_base(fdef->base->tmpl)) return scan1_fdef_base_tmpl(env, fdef->base); + struct Func_ fake = {.name = s_name(fdef->base->xid)}, *const former = + env->func; env->func = &fake; ++env->scope->depth; const m_bool ret = scanx_fdef(env, env, fdef, (_exp_func)scan1_fdef); --env->scope->depth; env->func = former; - if(global) - env_pop(env, scope); + if (global) env_pop(env, scope); return ret; } @@ -646,55 +648,51 @@ HANDLE_SECTION_FUNC(scan1, m_bool, Env) ANN static Type scan1_get_parent(const Env env, const Type_Def tdef) { const Type parent = known_type(env, tdef->ext); - CHECK_OO((tdef->type->info->parent = parent));; + CHECK_OO((tdef->type->info->parent = parent)); + ; Type t = parent; - do if(tdef->type == t) - ERR_O(tdef->ext->pos, _("recursive (%s <= %s) class declaration."), tdef->type->name, t->name) - while((t = t->info->parent)); + do + if (tdef->type == t) + ERR_O(tdef->ext->pos, _("recursive (%s <= %s) class declaration."), + tdef->type->name, t->name) + while ((t = t->info->parent)); return parent; } ANN static m_bool scan1_parent(const Env env, const Class_Def cdef) { const loc_t pos = cdef->base.ext->pos; - if(cdef->base.ext->array && cdef->base.ext->array->exp) + if (cdef->base.ext->array && cdef->base.ext->array->exp) CHECK_BB(scan1_exp(env, cdef->base.ext->array->exp)); - DECL_OB(const Type , parent, = scan1_get_parent(env, &cdef->base)); - if(isa(parent, env->gwion->type[et_object]) < 0) + DECL_OB(const Type, parent, = scan1_get_parent(env, &cdef->base)); + if (isa(parent, env->gwion->type[et_object]) < 0) ERR_B(pos, _("cannot extend primitive type '%s'"), parent->name) CHECK_BB(ensure_scan1(env, parent)); - if(type_ref(parent)) - ERR_B(pos, _("can't use ref type in class extend")) + if (type_ref(parent)) ERR_B(pos, _("can't use ref type in class extend")) return GW_OK; } ANN static m_bool cdef_parent(const Env env, const Class_Def cdef) { const bool tmpl = !!cdef->base.tmpl; - if(tmpl) - CHECK_BB(template_push_types(env, cdef->base.tmpl)); + if (tmpl) CHECK_BB(template_push_types(env, cdef->base.tmpl)); const m_bool ret = scan1_parent(env, cdef); - if(tmpl) - nspc_pop_type(env->gwion->mp, env->curr); + if (tmpl) nspc_pop_type(env->gwion->mp, env->curr); return ret; } ANN m_bool scan1_class_def(const Env env, const Class_Def cdef) { - if(tmpl_base(cdef->base.tmpl)) - return GW_OK; + if (tmpl_base(cdef->base.tmpl)) return GW_OK; const Type t = cdef->base.type; - if(tflag(t, tflag_scan1)) - return GW_OK; + if (tflag(t, tflag_scan1)) return GW_OK; set_tflag(t, tflag_scan1); - if(t->info->value->from->owner_class) + if (t->info->value->from->owner_class) CHECK_BB(ensure_scan1(env, t->info->value->from->owner_class)); - if(cdef->base.ext) - CHECK_BB(cdef_parent(env, cdef)); - if(cdef->body) - CHECK_BB(env_body(env, cdef, scan1_section)); + if (cdef->base.ext) CHECK_BB(cdef_parent(env, cdef)); + if (cdef->body) CHECK_BB(env_body(env, cdef, scan1_section)); return GW_OK; } ANN m_bool scan1_ast(const Env env, Ast ast) { do CHECK_BB(scan1_section(env, ast->section)); - while((ast = ast->next)); + while ((ast = ast->next)); return GW_OK; } diff --git a/src/parse/scan2.c b/src/parse/scan2.c index f8e70f41..38f99a9b 100644 --- a/src/parse/scan2.c +++ b/src/parse/scan2.c @@ -15,213 +15,214 @@ ANN static m_bool scan2_stmt(const Env, const Stmt); ANN static m_bool scan2_stmt_list(const Env, Stmt_List); ANN static inline m_bool ensure_scan2(const Env env, const Type t) { - if(tflag(t, tflag_scan2) || !(tflag(t, tflag_cdef) || tflag(t, tflag_udef))) + if (tflag(t, tflag_scan2) || !(tflag(t, tflag_cdef) || tflag(t, tflag_udef))) return GW_OK; - struct EnvSet es = { .env=env, .data=env, .func=(_exp_func)scan2_cdef, - .scope=env->scope->depth, .flag=tflag_scan2 }; + struct EnvSet es = {.env = env, + .data = env, + .func = (_exp_func)scan2_cdef, + .scope = env->scope->depth, + .flag = tflag_scan2}; return envset_run(&es, t); } -ANN static m_bool scan2_decl(const Env env, const Exp_Decl* decl) { +ANN static m_bool scan2_decl(const Env env, const Exp_Decl *decl) { const Type t = decl->type; CHECK_BB(ensure_scan2(env, t)); Var_Decl_List list = decl->list; do { - const Var_Decl var = list->self; - const Exp array = var->array ? var->array->exp : NULL; - if(array) - CHECK_BB(scan2_exp(env, array)); + const Var_Decl var = list->self; + const Exp array = var->array ? var->array->exp : NULL; + if (array) CHECK_BB(scan2_exp(env, array)); nspc_add_value(env->curr, var->xid, var->value); - } while((list = list->next)); + } while ((list = list->next)); return GW_OK; } -ANN m_bool scan2_exp_decl(const Env env, const Exp_Decl* decl) { - const bool global = GET_FLAG(decl->td, global); - const m_uint scope = !global ? env->scope->depth : env_push_global(env); - const m_bool ret = scan2_decl(env, decl); - if(global) - env_pop(env, scope); +ANN m_bool scan2_exp_decl(const Env env, const Exp_Decl *decl) { + const bool global = GET_FLAG(decl->td, global); + const m_uint scope = !global ? env->scope->depth : env_push_global(env); + const m_bool ret = scan2_decl(env, decl); + if (global) env_pop(env, scope); return ret; } ANN static m_bool scan2_args(const Func_Def f) { - Arg_List list = f->base->args; + Arg_List list = f->base->args; const bool global = GET_FLAG(f->base, global); do { - const Value v = list->var_decl->value; + const Value v = list->var_decl->value; v->from->offset = f->stack_depth; f->stack_depth += v->type->size; - if(global) - SET_FLAG(v, global); - } while((list = list->next)); + if (global) SET_FLAG(v, global); + } while ((list = list->next)); return GW_OK; } ANN static Value scan2_func_assign(const Env env, const Func_Def d, - const Func f, const Value v) { + const Func f, const Value v) { valuefrom(env, v->from, d->base->pos); SET_FLAG(v, const); set_vflag(v, vflag_func); - if(!env->class_def) { - if(!GET_FLAG(d->base, global)) - set_vflag(v, vflag_fglobal); + if (!env->class_def) { + if (!GET_FLAG(d->base, global)) set_vflag(v, vflag_fglobal); } else { - if(GET_FLAG(d->base, static)) + if (GET_FLAG(d->base, static)) SET_FLAG(v, static); else set_vflag(v, vflag_member); SET_ACCESS(d->base, v) - } + } d->base->func = v->d.func_ref = f; - return f->value_ref = v; + return f->value_ref = v; } - ANN m_bool scan2_fptr_def(const Env env NUSED, const Fptr_Def fptr) { - if(!tmpl_base(fptr->base->tmpl)) { + if (!tmpl_base(fptr->base->tmpl)) { const Func_Def def = fptr->type->info->func->def; - if(def->base->args) { - RET_NSPC(scan2_args(def)) - } + if (def->base->args) { RET_NSPC(scan2_args(def)) } } else set_tflag(fptr->type, tflag_ftmpl); return GW_OK; } ANN static m_bool scan2_func_def_op(const Env env, const Func_Def f); -ANN m_bool scan2_type_def(const Env env, const Type_Def tdef) { - if(tdef->when) - CHECK_BB(scan2_exp(env, tdef->when)); - if(!tdef->type->info->cdef) - return GW_OK; - return (!is_fptr(env->gwion, tdef->type) && tdef->type->info->cdef) ? - scan2_class_def(env, tdef->type->info->cdef) : GW_OK; +ANN m_bool scan2_type_def(const Env env, const Type_Def tdef) { + if (tdef->when) CHECK_BB(scan2_exp(env, tdef->when)); + if (!tdef->type->info->cdef) return GW_OK; + return (!is_fptr(env->gwion, tdef->type) && tdef->type->info->cdef) + ? scan2_class_def(env, tdef->type->info->cdef) + : GW_OK; } ANN static m_bool scan2_range(const Env env, Range *range) { - if(range->start) - CHECK_BB(scan2_exp(env, range->start)); - if(range->end) - CHECK_BB(scan2_exp(env, range->end)); + if (range->start) CHECK_BB(scan2_exp(env, range->start)); + if (range->end) CHECK_BB(scan2_exp(env, range->end)); return GW_OK; } -ANN static inline m_bool scan2_prim(const Env env, const Exp_Primary* prim) { - if(prim->prim_type == ae_prim_hack || prim->prim_type == ae_prim_interp) +ANN static inline m_bool scan2_prim(const Env env, const Exp_Primary *prim) { + if (prim->prim_type == ae_prim_hack || prim->prim_type == ae_prim_interp) CHECK_BB(scan2_exp(env, prim->d.exp)); -/* else if(prim->prim_type == ae_prim_id) { - const Value v = prim_value(env, prim->d.var); - if(v) - v->vflag |= used; - } */else if(prim->prim_type == ae_prim_array && prim->d.array->exp) + /* else if(prim->prim_type == ae_prim_id) { + const Value v = prim_value(env, prim->d.var); + if(v) + v->vflag |= used; + } */ + else if (prim->prim_type == ae_prim_array && prim->d.array->exp) return scan2_exp(env, prim->d.array->exp); - else if(prim->prim_type == ae_prim_range) + else if (prim->prim_type == ae_prim_range) return scan2_range(env, prim->d.range); return GW_OK; } -ANN static inline m_bool scan2_exp_array(const Env env, const Exp_Array* array) { +ANN static inline m_bool scan2_exp_array(const Env env, + const Exp_Array *array) { CHECK_BB(scan2_exp(env, array->base)); return scan2_exp(env, array->array->exp); } -ANN static inline m_bool scan2_exp_slice(const Env env, const Exp_Slice* exp) { +ANN static inline m_bool scan2_exp_slice(const Env env, const Exp_Slice *exp) { CHECK_BB(scan2_exp(env, exp->base)); return scan2_range(env, exp->range); } ANN static m_bool multi_decl(const Env env, const Exp e, const Symbol op) { - if(e->exp_type == ae_exp_decl) { - if(e->d.exp_decl.list->next) - ERR_B(e->pos, _("cant '%s' from/to a multi-variable declaration."), s_name(op)) -// set_vflag(e->d.exp_decl.list->self->value, vflag_used); + if (e->exp_type == ae_exp_decl) { + if (e->d.exp_decl.list->next) + ERR_B(e->pos, _("cant '%s' from/to a multi-variable declaration."), + s_name(op)) + // set_vflag(e->d.exp_decl.list->self->value, vflag_used); } return GW_OK; } -ANN static inline m_bool scan2_exp_binary(const Env env, const Exp_Binary* bin) { +ANN static inline m_bool scan2_exp_binary(const Env env, + const Exp_Binary *bin) { CHECK_BB(scan2_exp(env, bin->lhs)); CHECK_BB(scan2_exp(env, bin->rhs)); CHECK_BB(multi_decl(env, bin->lhs, bin->op)); return multi_decl(env, bin->rhs, bin->op); } -ANN static inline m_bool scan2_exp_cast(const Env env, const Exp_Cast* cast) { +ANN static inline m_bool scan2_exp_cast(const Env env, const Exp_Cast *cast) { return scan2_exp(env, cast->exp); } -ANN static inline m_bool scan2_exp_post(const Env env, const Exp_Postfix* post) { +ANN static inline m_bool scan2_exp_post(const Env env, + const Exp_Postfix *post) { return scan2_exp(env, post->exp); } -ANN static inline m_bool scan2_exp_call(const Env env, const Exp_Call* exp_call) { - if(exp_call->tmpl) - return GW_OK; +ANN static inline m_bool scan2_exp_call(const Env env, + const Exp_Call *exp_call) { + if (exp_call->tmpl) return GW_OK; CHECK_BB(scan2_exp(env, exp_call->func)); const Exp args = exp_call->args; return args ? scan2_exp(env, args) : GW_OK; } -ANN static inline m_bool scan2_exp_dot(const Env env, const Exp_Dot* member) { +ANN static inline m_bool scan2_exp_dot(const Env env, const Exp_Dot *member) { return scan2_exp(env, member->base); } -ANN static inline m_bool scan2_exp_if(const Env env, const Exp_If* exp_if) { +ANN static inline m_bool scan2_exp_if(const Env env, const Exp_If *exp_if) { CHECK_BB(scan2_exp(env, exp_if->cond)); CHECK_BB(scan2_exp(env, exp_if->if_exp ?: exp_if->cond)); return scan2_exp(env, exp_if->else_exp); } -ANN static m_bool scan2_exp_unary(const Env env, const Exp_Unary * unary) { - if(unary->unary_type == unary_code) { +ANN static m_bool scan2_exp_unary(const Env env, const Exp_Unary *unary) { + if (unary->unary_type == unary_code) { RET_NSPC(scan2_stmt(env, unary->code)) - } else if(unary->unary_type == unary_exp) + } else if (unary->unary_type == unary_exp) return scan2_exp(env, unary->exp); return GW_OK; } -ANN static inline m_bool _scan2_stmt_match_case(const restrict Env env, const Stmt_Match stmt) { +ANN static inline m_bool _scan2_stmt_match_case(const restrict Env env, + const Stmt_Match stmt) { CHECK_BB(scan2_exp(env, stmt->cond)); - if(stmt->when) - CHECK_BB(scan2_exp(env, stmt->when)); + if (stmt->when) CHECK_BB(scan2_exp(env, stmt->when)); return scan2_stmt_list(env, stmt->list); } -ANN static inline m_bool scan2_stmt_match_case(const restrict Env env, const Stmt_Match stmt) { - RET_NSPC(_scan2_stmt_match_case(env, stmt)) -} +ANN static inline m_bool scan2_stmt_match_case(const restrict Env env, + const Stmt_Match stmt) { + RET_NSPC(_scan2_stmt_match_case(env, stmt))} -ANN static inline m_bool _scan2_stmt_match(const restrict Env env, const Stmt_Match stmt) { - if(stmt->where) - CHECK_BB(scan2_stmt(env, stmt->where)); +ANN static inline m_bool + _scan2_stmt_match(const restrict Env env, const Stmt_Match stmt) { + if (stmt->where) CHECK_BB(scan2_stmt(env, stmt->where)); Stmt_List list = stmt->list; do CHECK_BB(scan2_stmt_match_case(env, &list->stmt->d.stmt_match)); - while((list = list->next)); + while ((list = list->next)); return GW_OK; } -ANN static inline m_bool scan2_handler_list(const restrict Env env, const Handler_List handler) { - if(handler->next) - CHECK_BB(scan2_handler_list(env, handler->next)); +ANN static inline m_bool scan2_handler_list(const restrict Env env, + const Handler_List handler) { + if (handler->next) CHECK_BB(scan2_handler_list(env, handler->next)); RET_NSPC(scan2_stmt(env, handler->stmt)) } -ANN static inline m_bool scan2_stmt_try(const restrict Env env, const Stmt_Try stmt) { +ANN static inline m_bool scan2_stmt_try(const restrict Env env, + const Stmt_Try stmt) { CHECK_BB(scan2_handler_list(env, stmt->handler)); RET_NSPC(scan2_stmt(env, stmt->stmt)) } -ANN static inline m_bool scan2_stmt_match(const restrict Env env, const Stmt_Match stmt) { +ANN static inline m_bool scan2_stmt_match(const restrict Env env, + const Stmt_Match stmt) { CHECK_BB(scan2_exp(env, stmt->cond)); RET_NSPC(_scan2_stmt_match(env, stmt)) } #define scan2_exp_lambda dummy_func -#define scan2_exp_td dummy_func +#define scan2_exp_td dummy_func HANDLE_EXP_FUNC(scan2, m_bool, Env) -#define scan2_stmt_func(name, type, prolog, exp) describe_stmt_func(scan2, name, type, prolog, exp) +#define scan2_stmt_func(name, type, prolog, exp) \ + describe_stmt_func(scan2, name, type, prolog, exp) scan2_stmt_func(flow, Stmt_Flow,, !(scan2_exp(env, stmt->cond) < 0 || scan2_stmt(env, stmt->body) < 0) ? 1 : -1) scan2_stmt_func(varloop, Stmt_VarLoop,, !(scan2_exp(env, stmt->exp) < 0 || @@ -239,8 +240,7 @@ scan2_stmt_func(if, Stmt_If,, !(scan2_exp(env, stmt->cond) < 0 || (stmt->else_body && scan2_stmt(env, stmt->else_body) < 0)) ? 1 : -1) ANN static inline m_bool scan2_stmt_code(const Env env, const Stmt_Code stmt) { - if(stmt->stmt_list) - { RET_NSPC(scan2_stmt_list(env, stmt->stmt_list)) } + if (stmt->stmt_list) { RET_NSPC(scan2_stmt_list(env, stmt->stmt_list)) } return GW_OK; } @@ -249,13 +249,11 @@ ANN static inline m_bool scan2_stmt_exp(const Env env, const Stmt_Exp stmt) { } ANN m_bool scan2_union_def(const Env env NUSED, const Union_Def udef) { - if(tmpl_base(udef->tmpl)) - return GW_OK; + if (tmpl_base(udef->tmpl)) return GW_OK; set_tflag(udef->type, tflag_scan2); return GW_OK; } - #define scan2_stmt_while scan2_stmt_flow #define scan2_stmt_until scan2_stmt_flow #define scan2_stmt_continue dummy_func @@ -264,8 +262,7 @@ ANN m_bool scan2_union_def(const Env env NUSED, const Union_Def udef) { #define scan2_stmt_retry dummy_func ANN static m_bool scan2_stmt_pp(const Env env, const Stmt_PP stmt) { - if(stmt->pp_type == ae_pp_include) - env->name = stmt->data; + if (stmt->pp_type == ae_pp_include) env->name = stmt->data; return GW_OK; } @@ -281,104 +278,118 @@ ANN static m_bool scan2_stmt(const Env env, const Stmt stmt) { ANN static m_bool scan2_stmt_list(const Env env, Stmt_List list) { do CHECK_BB(scan2_stmt(env, list->stmt)); - while((list = list->next)); + while ((list = list->next)); return GW_OK; } -ANN static m_bool scan2_func_def_overload(const Env env, const Func_Def f, const Value overload) { +ANN static m_bool scan2_func_def_overload(const Env env, const Func_Def f, + const Value overload) { const m_bool fptr = is_fptr(env->gwion, overload->type); - if(isa(overload->type, env->gwion->type[et_function]) < 0 || is_fptr(env->gwion, overload->type)) { - if(!fbflag(f->base, fbflag_internal)) - ERR_B(f->base->pos, _("function name '%s' is already used by another value"), overload->name) + if (isa(overload->type, env->gwion->type[et_function]) < 0 || + is_fptr(env->gwion, overload->type)) { + if (!fbflag(f->base, fbflag_internal)) + ERR_B(f->base->pos, + _("function name '%s' is already used by another value"), + overload->name) } - const Func obase = !fptr ? overload->d.func_ref : _class_base(overload->type)->info->func; - if(GET_FLAG(obase->def->base, final)) + const Func obase = + !fptr ? overload->d.func_ref : _class_base(overload->type)->info->func; + if (GET_FLAG(obase->def->base, final)) ERR_B(f->base->pos, _("can't overload final function %s"), overload->name) const m_bool base = tmpl_base(f->base->tmpl); const m_bool tmpl = fflag(obase, fflag_tmpl); - if((!tmpl && base) || (tmpl && !base && !f->base->tmpl)) + if ((!tmpl && base) || (tmpl && !base && !f->base->tmpl)) ERR_B(f->base->pos, _("must overload template function with template")) return GW_OK; } -ANN static Func scan_new_func(const Env env, const Func_Def f, const m_str name) { +ANN static Func scan_new_func(const Env env, const Func_Def f, + const m_str name) { const Func func = new_func(env->gwion->mp, name, f); - if(env->class_def && tflag(env->class_def, tflag_tmpl)) + if (env->class_def && tflag(env->class_def, tflag_tmpl)) set_fflag(func, fflag_ftmpl); - if(fbflag(f->base, fbflag_lambda)) + if (fbflag(f->base, fbflag_lambda)) env->curr->info->value = new_scope(env->gwion->mp); return func; } ANN static Type func_type(const Env env, const Func func) { - const Type base = env->gwion->type[!fbflag(func->def->base, fbflag_lambda) ? et_function : et_lambda]; - const Type t = type_copy(env->gwion->mp, base); + const Type base = + env->gwion->type[!fbflag(func->def->base, fbflag_lambda) ? et_function + : et_lambda]; + const Type t = type_copy(env->gwion->mp, base); t->info->parent = base; - t->name = func->name; - t->info->func = func; + t->name = func->name; + t->info->func = func; return t; } -ANN2(1,2) static Value func_value(const Env env, const Func f, - const Value overload) { +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 = t->info->value = new_value(env->gwion->mp, t, t->name); valuefrom(env, v->from, f->def->base->pos); CHECK_OO(scan2_func_assign(env, f->def, f, v)); - if(!overload) { + if (!overload) { value_addref(v); nspc_add_value_front(env->curr, f->def->base->xid, v); - } else if(overload->d.func_ref) { - f->next = overload->d.func_ref->next; + } else if (overload->d.func_ref) { + f->next = overload->d.func_ref->next; overload->d.func_ref->next = f; } - if(env->class_def && !GET_FLAG(f->def->base, static)) { + if (env->class_def && !GET_FLAG(f->def->base, static)) { t->size += SZ_INT; set_vflag(v, vflag_member); } return v; } -ANN2(1, 2) static m_bool scan2_fdef_tmpl(const Env env, const Func_Def f, const Value overload) { - const m_str name = s_name(f->base->xid); - const Func func = scan_new_func(env, f, name); +ANN2(1, 2) +static m_bool scan2_fdef_tmpl(const Env env, const Func_Def f, + const Value overload) { + const m_str name = s_name(f->base->xid); + const Func func = scan_new_func(env, f, name); const Value value = func_value(env, func, overload); set_fflag(func, fflag_tmpl); set_vflag(value, vflag_valid); - set_tflag(value->type, tflag_ftmpl); // the only types with func flag, name could be better + set_tflag(value->type, + tflag_ftmpl); // the only types with func flag, name could be better Type type = env->class_def; Nspc nspc = env->curr; - uint i = 0; + uint i = 0; do { const Value v = nspc_lookup_value0(nspc, f->base->xid); - if(v) { + if (v) { Func ff = v->d.func_ref; - if(!ff)continue; + if (!ff) continue; do { - if(ff->def == f) { + if (ff->def == f) { ++i; continue; } - if(compat_func(ff->def, f) > 0) { - if(ff->value_ref->from->owner == env->curr) - ERR_B(f->base->pos, "template function '%s' already defined with those arguments in this namespace", name) - const Symbol sym = func_symbol(env, env->curr->name, name, - "template", ff->vt_index); + if (compat_func(ff->def, f) > 0) { + if (ff->value_ref->from->owner == env->curr) + ERR_B(f->base->pos, + "template function '%s' already defined with those arguments " + "in this namespace", + name) + const Symbol sym = + func_symbol(env, env->curr->name, name, "template", ff->vt_index); nspc_add_value(env->curr, sym, value); - if(!overload) { + if (!overload) { value_addref(value); nspc_add_value(env->curr, f->base->xid, value); } func->vt_index = ff->vt_index; return GW_OK; } - } while((ff = ff->next) && ++i); - } - } while(type && (type = type->info->parent) && (nspc = type->nspc)); + } while ((ff = ff->next) && ++i); + } + } while (type && (type = type->info->parent) && (nspc = type->nspc)); --i; const Symbol sym = func_symbol(env, env->curr->name, name, "template", i); nspc_add_value(env->curr, sym, value); - if(!overload) { + if (!overload) { value_addref(value); nspc_add_value(env->curr, f->base->xid, value); nspc_add_func(env->curr, f->base->xid, func); @@ -388,10 +399,13 @@ ANN2(1, 2) static m_bool scan2_fdef_tmpl(const Env env, const Func_Def f, const } ANN static m_bool scan2_func_def_op(const Env env, const Func_Def f) { - const m_str str = s_name(f->base->xid); - struct Op_Func opfunc = { .ck=strcmp(str, "@implicit") ? 0 : opck_usr_implicit }; - struct Op_Import opi = { .ret=f->base->ret_type, .pos=f->base->pos, - .data=(uintptr_t)f->base->func, .func=&opfunc }; + const m_str str = s_name(f->base->xid); + struct Op_Func opfunc = {.ck = strcmp(str, "@implicit") ? 0 + : opck_usr_implicit}; + struct Op_Import opi = {.ret = f->base->ret_type, + .pos = f->base->pos, + .data = (uintptr_t)f->base->func, + .func = &opfunc}; func_operator(f, &opi); CHECK_BB(add_op(env->gwion, &opi)); operator_set_func(&opi); @@ -400,7 +414,7 @@ ANN static m_bool scan2_func_def_op(const Env env, const Func_Def f) { ANN static m_bool scan2_func_def_code(const Env env, const Func_Def f) { const Func former = env->func; - env->func = f->base->func; + env->func = f->base->func; CHECK_BB(scan2_stmt_code(env, &f->d.code->d.stmt_code)); env->func = former; return GW_OK; @@ -408,40 +422,40 @@ ANN static m_bool scan2_func_def_code(const Env env, const Func_Def f) { ANN static void scan2_func_def_flag(const Env env, const Func_Def f) { set_fflag(f->base->func, fflag_pure); - if(f->base->xid == insert_symbol("@dtor")) + if (f->base->xid == insert_symbol("@dtor")) set_tflag(env->class_def, tflag_dtor); } ANN static m_str func_tmpl_name(const Env env, const Func_Def f) { - const m_str name = s_name(f->base->xid); - struct Vector_ v; - Specialized_List id = f->base->tmpl->list; - m_uint tlen = 0; + const m_str name = s_name(f->base->xid); + struct Vector_ v; + Specialized_List id = f->base->tmpl->list; + m_uint tlen = 0; vector_init(&v); do { const Type t = nspc_lookup_type0(env->curr, id->xid); - if(!t)return NULL; + if (!t) return NULL; vector_add(&v, (vtype)t); tlen += strlen(t->name); - } while((id = id->next) && ++tlen); - char tmpl_name[tlen + 2]; + } while ((id = id->next) && ++tlen); + char tmpl_name[tlen + 2]; m_str str = tmpl_name; - for(m_uint i = 0; i < vector_size(&v); ++i) { + for (m_uint i = 0; i < vector_size(&v); ++i) { const m_str s = ((Type)vector_at(&v, i))->name; strcpy(str, s); str += strlen(s); - if(i + 1 < vector_size(&v)) - *str++ = ','; + if (i + 1 < vector_size(&v)) *str++ = ','; } - tmpl_name[tlen+1] = '\0'; + tmpl_name[tlen + 1] = '\0'; vector_release(&v); - const Symbol sym = func_symbol(env, env->curr->name, name, tmpl_name, (m_uint)f->base->tmpl->base); + const Symbol sym = func_symbol(env, env->curr->name, name, tmpl_name, + (m_uint)f->base->tmpl->base); return s_name(sym); } - -ANN2(1,2,4) static Value func_create(const Env env, const Func_Def f, - const Value overload, const m_str name) { +ANN2(1, 2, 4) +static Value func_create(const Env env, const Func_Def f, const Value overload, + const m_str name) { const Func func = scan_new_func(env, f, name); nspc_add_func(env->curr, insert_symbol(func->name), func); const Value v = func_value(env, func, overload); @@ -450,30 +464,29 @@ ANN2(1,2,4) static Value func_create(const Env env, const Func_Def f, return v; } -ANN2(1,2) static m_str func_name(const Env env, const Func_Def f, const Value v) { - if(!f->base->tmpl) { - const Symbol sym = func_symbol(env, env->curr->name, - s_name(f->base->xid), NULL, v ? ++v->from->offset : 0); +ANN2(1, 2) +static m_str func_name(const Env env, const Func_Def f, const Value v) { + if (!f->base->tmpl) { + const Symbol sym = func_symbol(env, env->curr->name, s_name(f->base->xid), + NULL, v ? ++v->from->offset : 0); return s_name(sym); } return f->base->func ? f->base->func->name : func_tmpl_name(env, f); } -ANN2(1,2) m_bool scan2_fdef_std(const Env env, const Func_Def f, const Value overload) { +ANN2(1, 2) +m_bool scan2_fdef_std(const Env env, const Func_Def f, const Value overload) { const m_str name = func_name(env, f, overload ?: NULL); - if(!name)return GW_ERROR; + if (!name) return GW_ERROR; const Func base = f->base->func; - if(!base) + if (!base) CHECK_OB(func_create(env, f, overload, name)); else f->base->func = base; - if(f->base->args) - CHECK_BB(scan2_args(f)); - if(f->d.code) - CHECK_BB(scan2_func_def_code(env, f)); - if(!base) { - if(fbflag(f->base, fbflag_op)) - CHECK_BB(scan2_func_def_op(env, f)); + if (f->base->args) CHECK_BB(scan2_args(f)); + if (f->d.code) CHECK_BB(scan2_func_def_code(env, f)); + if (!base) { + if (fbflag(f->base, fbflag_op)) CHECK_BB(scan2_func_def_op(env, f)); set_vflag(f->base->func->value_ref, vflag_valid); } return GW_OK; @@ -482,62 +495,59 @@ ANN2(1,2) m_bool scan2_fdef_std(const Env env, const Func_Def f, const Value ove //! use function from parent class as next. ANN static void upfunction(const Env env, const Func_Base *fb) { const Value v = find_value(env->class_def->info->parent, fb->xid); - if(!v) - return; + if (!v) return; Func func = fb->func; - while(func->next && func->next->value_ref->from->owner == env->curr) + while (func->next && func->next->value_ref->from->owner == env->curr) func = func->next; func->next = v->d.func_ref; } ANN m_bool scan2_fdef(const Env env, const Func_Def fdef) { const Value overload = nspc_lookup_value2(env->curr, fdef->base->xid); - if(overload) - CHECK_BB(scan2_func_def_overload(env, fdef, overload)); - CHECK_BB((!tmpl_base(fdef->base->tmpl) ? scan2_fdef_std : scan2_fdef_tmpl)(env, fdef, overload)); - if(env->class_def) - upfunction(env, fdef->base); + if (overload) CHECK_BB(scan2_func_def_overload(env, fdef, overload)); + CHECK_BB((!tmpl_base(fdef->base->tmpl) ? scan2_fdef_std : scan2_fdef_tmpl)( + env, fdef, overload)); + if (env->class_def) upfunction(env, fdef->base); return GW_OK; } -__attribute__((returns_nonnull)) -static ANN Func_Def scan2_cpy_fdef(const Env env, const Func_Def fdef) { - const Func_Def f = cpy_func_def(env->gwion->mp, fdef); +__attribute__((returns_nonnull)) static ANN Func_Def +scan2_cpy_fdef(const Env env, const Func_Def fdef) { + const Func_Def f = cpy_func_def(env->gwion->mp, fdef); f->base->ret_type = fdef->base->ret_type; Arg_List a = f->base->args, b = fdef->base->args; - while(a) { + while (a) { a->var_decl->value = b->var_decl->value; - a->type = b->type; - a = a->next; - b = b->next; + a->type = b->type; + a = a->next; + b = b->next; } scan1_func_def(env, f); return f; } static inline int is_cpy(const Func_Def fdef) { - return GET_FLAG(fdef->base, global) || - (fdef->base->tmpl && !fdef->base->tmpl->call); + return GET_FLAG(fdef->base, global) || + (fdef->base->tmpl && !fdef->base->tmpl->call); } ANN m_bool scan2_func_def(const Env env, const Func_Def fdef) { - if(GET_FLAG(fdef->base, global) && !env->class_def) - env->context->global = 1; - const Func_Def f = !is_cpy(fdef) ? - fdef : scan2_cpy_fdef(env, fdef); - const m_uint scope = !GET_FLAG(f->base, global) ? env->scope->depth : env_push_global(env); - f->stack_depth = (env->class_def && !GET_FLAG(f->base, static) && !GET_FLAG(f->base, global)) ? SZ_INT : 0; - if(fbflag(f->base, fbflag_variadic)) - f->stack_depth += SZ_INT; + if (GET_FLAG(fdef->base, global) && !env->class_def) env->context->global = 1; + const Func_Def f = !is_cpy(fdef) ? fdef : scan2_cpy_fdef(env, fdef); + const m_uint scope = + !GET_FLAG(f->base, global) ? env->scope->depth : env_push_global(env); + f->stack_depth = (env->class_def && !GET_FLAG(f->base, static) && + !GET_FLAG(f->base, global)) + ? SZ_INT + : 0; + if (fbflag(f->base, fbflag_variadic)) f->stack_depth += SZ_INT; const m_bool ret = scanx_fdef(env, env, f, (_exp_func)scan2_fdef); - if(GET_FLAG(f->base, global)) - env_pop(env, scope); + if (GET_FLAG(f->base, global)) env_pop(env, scope); CHECK_BB(ret); fdef->base->func = f->base->func; // only needed if 'is_cpy()' return GW_OK; } - ANN static m_bool scan2_extend_def(const Env env, const Extend_Def xdef) { CHECK_BB(ensure_scan2(env, xdef->t)); CHECK_BB(extend_push(env, xdef->t)); @@ -546,7 +556,7 @@ ANN static m_bool scan2_extend_def(const Env env, const Extend_Def xdef) { return ret; } -#define scan2_enum_def dummy_func +#define scan2_enum_def dummy_func #define scan2_trait_def dummy_func HANDLE_SECTION_FUNC(scan2, m_bool, Env) @@ -554,39 +564,33 @@ HANDLE_SECTION_FUNC(scan2, m_bool, Env) ANN static m_bool scan2_parent(const Env env, const Class_Def cdef) { const Type parent = cdef->base.type->info->parent; CHECK_BB(ensure_scan2(env, parent)); - if(cdef->base.ext->array && cdef->base.ext->array->exp) + if (cdef->base.ext->array && cdef->base.ext->array->exp) CHECK_BB(scan2_exp(env, cdef->base.ext->array->exp)); return GW_OK; } ANN static m_bool cdef_parent(const Env env, const Class_Def cdef) { const bool tmpl = !!cdef->base.tmpl; - if(tmpl) - CHECK_BB(template_push_types(env, cdef->base.tmpl)); + if (tmpl) CHECK_BB(template_push_types(env, cdef->base.tmpl)); const m_bool ret = scan2_parent(env, cdef); - if(tmpl) - nspc_pop_type(env->gwion->mp, env->curr); + if (tmpl) nspc_pop_type(env->gwion->mp, env->curr); return ret; } ANN m_bool scan2_class_def(const Env env, const Class_Def cdef) { - if(tmpl_base(cdef->base.tmpl)) - return GW_OK; + if (tmpl_base(cdef->base.tmpl)) return GW_OK; const Type t = cdef->base.type; - if(tflag(t, tflag_scan2)) - return GW_OK; - if(t->info->value->from->owner_class) + if (tflag(t, tflag_scan2)) return GW_OK; + if (t->info->value->from->owner_class) CHECK_BB(ensure_scan2(env, t->info->value->from->owner_class)); set_tflag(t, tflag_scan2); - if(cdef->base.ext) - CHECK_BB(cdef_parent(env, cdef)); - if(cdef->body) - CHECK_BB(env_body(env, cdef, scan2_section)); + if (cdef->base.ext) CHECK_BB(cdef_parent(env, cdef)); + if (cdef->body) CHECK_BB(env_body(env, cdef, scan2_section)); return GW_OK; } ANN m_bool scan2_ast(const Env env, Ast ast) { do CHECK_BB(scan2_section(env, ast->section)); - while((ast = ast->next)); + while ((ast = ast->next)); return GW_OK; } diff --git a/src/parse/scanx.c b/src/parse/scanx.c index 90c0ff8e..487f65bb 100644 --- a/src/parse/scanx.c +++ b/src/parse/scanx.c @@ -8,7 +8,7 @@ ANN static inline m_bool _body(const Env e, Ast b, const _exp_func f) { do CHECK_BB(f(e, b->section)); - while((b = b->next)); + while ((b = b->next)); return GW_OK; } @@ -16,55 +16,52 @@ ANN static inline int actual(const Tmpl *tmpl) { return tmpl->call && tmpl->call != (Type_List)1 && tmpl->list; } -ANN static inline m_bool tmpl_push(const Env env, const Tmpl* tmpl) { +ANN static inline m_bool tmpl_push(const Env env, const Tmpl *tmpl) { return actual(tmpl) ? template_push_types(env, tmpl) : GW_ERROR; } ANN static inline m_int _push(const Env env, const Class_Def c) { DECL_BB(const m_int, scope, = env_push_type(env, c->base.type)); - return (!c->base.tmpl || tmpl_push(env, c->base.tmpl)) ? - scope : GW_ERROR; + return (!c->base.tmpl || tmpl_push(env, c->base.tmpl)) ? scope : GW_ERROR; } ANN static inline void _pop(const Env e, const Class_Def c, const m_uint s) { - if(c->base.tmpl && actual(c->base.tmpl) && c->base.tmpl->list) + if (c->base.tmpl && actual(c->base.tmpl) && c->base.tmpl->list) nspc_pop_type(e->gwion->mp, e->curr); env_pop(e, s); } // TODO: 'v' should be 2° argument -ANN m_bool -scanx_body(const Env e, const Class_Def c, const _exp_func f, void* d) { +ANN m_bool scanx_body(const Env e, const Class_Def c, const _exp_func f, + void *d) { DECL_BB(const m_int, scope, = _push(e, c)); - const m_bool ret = _body(d, c->body, f); + const m_bool ret = _body(d, c->body, f); _pop(e, c, scope); return ret; } -ANN static m_bool _scanx_cdef(const Env env, void* opt, const Type t, - const _exp_func f_cdef, const _exp_func f_udef) { - if(t->info->parent != env->gwion->type[et_union]) - return f_cdef(opt, t->info->cdef); +ANN static m_bool _scanx_cdef(const Env env, void *opt, const Type t, + const _exp_func f_cdef, const _exp_func f_udef) { + if (t->info->parent != env->gwion->type[et_union]) + return f_cdef(opt, t->info->cdef); const m_bool ret = f_udef(opt, t->info->udef); return ret; } -ANN m_bool scanx_cdef(const Env env, void* opt, const Type t, - const _exp_func f_cdef, const _exp_func f_udef) { - const bool in_try = env->scope->in_try; - const m_bool ret = _scanx_cdef(env, opt, t, f_cdef, f_udef); - env->scope->in_try = in_try; +ANN m_bool scanx_cdef(const Env env, void *opt, const Type t, + const _exp_func f_cdef, const _exp_func f_udef) { + const bool in_try = env->scope->in_try; + const m_bool ret = _scanx_cdef(env, opt, t, f_cdef, f_udef); + env->scope->in_try = in_try; return ret; } -ANN m_bool scanx_fdef(const Env env, void *data, - const Func_Def fdef, const _exp_func func) { - if(fdef->base->tmpl) - CHECK_BB(template_push_types(env, fdef->base->tmpl)); - const bool in_try = env->scope->in_try; - const m_bool ret = func(data, fdef); - if(fdef->base->tmpl) - nspc_pop_type(env->gwion->mp, env->curr); +ANN m_bool scanx_fdef(const Env env, void *data, const Func_Def fdef, + const _exp_func func) { + if (fdef->base->tmpl) CHECK_BB(template_push_types(env, fdef->base->tmpl)); + const bool in_try = env->scope->in_try; + const m_bool ret = func(data, fdef); + if (fdef->base->tmpl) nspc_pop_type(env->gwion->mp, env->curr); env->scope->in_try = in_try; return ret; } diff --git a/src/parse/template.c b/src/parse/template.c index 1805a942..5691dd9b 100644 --- a/src/parse/template.c +++ b/src/parse/template.c @@ -14,23 +14,21 @@ ANN static m_bool push_types(const Env env, const Tmpl *tmpl) { Specialized_List list = tmpl->list; - Type_List call = tmpl->call; + Type_List call = tmpl->call; do { - if(!call) - break; + if (!call) break; const Type t = known_type(env, call->td); - if(!t) - return 1; + if (!t) return 1; nspc_add_type(env->curr, list->xid, t); call = call->next; - } while((list = list->next)); + } while ((list = list->next)); return !call; } ANN static m_bool _template_push(const Env env, const Type t) { - if(t->info->value->from->owner_class) + if (t->info->value->from->owner_class) CHECK_BB(template_push(env, t->info->value->from->owner_class)); - if(tflag(t, tflag_tmpl)) + if (tflag(t, tflag_tmpl)) return push_types(env, t->info->cdef->base.tmpl); // incorrect return GW_OK; } @@ -42,105 +40,110 @@ ANN m_bool template_push(const Env env, const Type t) { ANN m_bool template_push_types(const Env env, const Tmpl *tmpl) { nspc_push_type(env->gwion->mp, env->curr); - if(push_types(env, tmpl)) - return GW_OK; + if (push_types(env, tmpl)) return GW_OK; POP_RET(GW_ERROR); } -ANN Tmpl* mk_tmpl(const Env env, const Tmpl *tm, const Type_List types) { +ANN Tmpl *mk_tmpl(const Env env, const Tmpl *tm, const Type_List types) { Tmpl *tmpl = new_tmpl(env->gwion->mp, tm->list, 0); tmpl->call = cpy_type_list(env->gwion->mp, types); return tmpl; } -static ANN Type scan_func(const Env env, const Type t, const Type_Decl* td) { +static ANN Type scan_func(const Env env, const Type t, const Type_Decl *td) { DECL_OO(const m_str, tl_name, = tl2str(env->gwion, td->types, td->pos)); - const Symbol sym = func_symbol(env, t->info->value->from->owner->name, t->info->func->name, tl_name, 0); + const Symbol sym = func_symbol(env, t->info->value->from->owner->name, + t->info->func->name, tl_name, 0); free_mstr(env->gwion->mp, tl_name); const Type base_type = nspc_lookup_type1(t->info->value->from->owner, sym); - if(base_type) - return base_type; - const Type ret = type_copy(env->gwion->mp, t); + if (base_type) return base_type; + const Type ret = type_copy(env->gwion->mp, t); ret->info->parent = t; - ret->info->value = t->info->func->value_ref; - ret->name = s_name(sym); + ret->info->value = t->info->func->value_ref; + ret->name = s_name(sym); set_tflag(ret, tflag_ftmpl); nspc_add_type_front(t->info->value->from->owner, sym, ret); - void* func_ptr = t->info->func->def->d.dl_func_ptr; - if(vflag(t->info->func->value_ref, vflag_builtin)) + void *func_ptr = t->info->func->def->d.dl_func_ptr; + if (vflag(t->info->func->value_ref, vflag_builtin)) t->info->func->def->d.dl_func_ptr = NULL; - const Func_Def def = cpy_func_def(env->gwion->mp, t->info->func->def); - const Func func = ret->info->func = new_func(env->gwion->mp, s_name(sym), def); + const Func_Def def = cpy_func_def(env->gwion->mp, t->info->func->def); + const Func func = ret->info->func = + new_func(env->gwion->mp, s_name(sym), def); const Value value = new_value(env->gwion->mp, ret, s_name(sym)); - func->flag = def->base->flag; - if(vflag(t->info->func->value_ref, vflag_member)) + func->flag = def->base->flag; + if (vflag(t->info->func->value_ref, vflag_member)) set_vflag(value, vflag_member); - value->d.func_ref = func; + value->d.func_ref = func; value->from->owner = t->info->value->from->owner; -// value->from->owner_class = t->info->owner_class; + // value->from->owner_class = t->info->owner_class; func->value_ref = value; - func->def->base->tmpl = mk_tmpl(env, t->info->func->def->base->tmpl, td->types); + func->def->base->tmpl = + mk_tmpl(env, t->info->func->def->base->tmpl, td->types); def->base->func = func; nspc_add_value_front(t->info->value->from->owner, sym, value); - if(vflag(t->info->func->value_ref, vflag_builtin)) { + if (vflag(t->info->func->value_ref, vflag_builtin)) { builtin_func(env->gwion->mp, func, func_ptr); t->info->func->def->d.dl_func_ptr = func_ptr; } return ret; } -static ANN Type maybe_func(const Env env, const Type t, const Type_Decl* td) { - if(isa(t, env->gwion->type[et_function]) > 0 && t->info->func->def->base->tmpl) - return scan_func(env, t, td); +static ANN Type maybe_func(const Env env, const Type t, const Type_Decl *td) { + if (isa(t, env->gwion->type[et_function]) > 0 && + t->info->func->def->base->tmpl) + return scan_func(env, t, td); ERR_O(td->pos, - _("type '%s' is not template. You should not provide template types"), t->name) + _("type '%s' is not template. You should not provide template types"), + t->name) } -ANN Type _scan_type(const Env env, const Type t, Type_Decl* td) { - if(tflag(t, tflag_tmpl) && isa(t, env->gwion->type[et_function]) < 0) { - if(tflag(t, tflag_ntmpl) && !td->types) - return t; - struct TemplateScan ts = { .t=t, .td=td }; -Type_List tl = td->types; -Specialized_List sl = t->info->cdef->base.tmpl->list; +ANN Type _scan_type(const Env env, const Type t, Type_Decl *td) { + if (tflag(t, tflag_tmpl) && isa(t, env->gwion->type[et_function]) < 0) { + if (tflag(t, tflag_ntmpl) && !td->types) return t; + struct TemplateScan ts = {.t = t, .td = td}; + Type_List tl = td->types; + Specialized_List sl = t->info->cdef->base.tmpl->list; -while(tl && sl) { - DECL_OO(const Type, t, = known_type(env, tl->td)); - ID_List missing = miss_traits(t, sl); - if(missing) { - ERR_O(tl->td->pos, "does not implement requested trait '{/}%s{0}'", s_name(missing->xid)); - } - tl = tl->next; - sl = sl->next; -} - struct Op_Import opi = { .op=insert_symbol("@scan"), .lhs=t, .data=(uintptr_t)&ts, .pos=td->pos }; + while (tl && sl) { + DECL_OO(const Type, t, = known_type(env, tl->td)); + ID_List missing = miss_traits(t, sl); + if (missing) { + ERR_O(tl->td->pos, "does not implement requested trait '{/}%s{0}'", + s_name(missing->xid)); + } + tl = tl->next; + sl = sl->next; + } + struct Op_Import opi = {.op = insert_symbol("@scan"), + .lhs = t, + .data = (uintptr_t)&ts, + .pos = td->pos}; return op_check(env, &opi); - } else if(td->types) + } else if (td->types) return maybe_func(env, t, td); return find_type(env, td); } -ANN Type scan_type(const Env env, const Type t, Type_Decl* td) { - if(td->next) { - Type_Decl *next = td->next; - td->next = NULL; +ANN Type scan_type(const Env env, const Type t, Type_Decl *td) { + if (td->next) { + Type_Decl *next = td->next; + td->next = NULL; const Type maybe_array = known_type(env, td); - const Type owner = array_base(maybe_array); - td->next = next; + const Type owner = array_base(maybe_array); + td->next = next; CHECK_OO(owner); - if(!owner->nspc) - ERR_O(td->pos, "type '%s' has no namespace", owner->name) - struct EnvSet es = { .env=env, .data=env, - .scope=env->scope->depth, .flag=tflag_none }; + if (!owner->nspc) ERR_O(td->pos, "type '%s' has no namespace", owner->name) + struct EnvSet es = {.env = env, + .data = env, + .scope = env->scope->depth, + .flag = tflag_none}; envset_push(&es, owner, owner->nspc); - (void)env_push(env, owner, owner->nspc);// TODO: is this needed? -// const Type ret = scan_type(env, t, td->next); + (void)env_push(env, owner, owner->nspc); // TODO: is this needed? + // const Type ret = scan_type(env, t, td->next); const Type ret = known_type(env, td->next); env_pop(env, es.scope); - if(es.run) - envset_pop(&es, owner); - if(!td->array) - return ret; + if (es.run) envset_pop(&es, owner); + if (!td->array) return ret; return array_type(env, ret, td->array->depth); } return !t->array_depth ? _scan_type(env, t, td) : t; diff --git a/src/parse/traverse.c b/src/parse/traverse.c index 0a5c5b5c..c9d36fb8 100644 --- a/src/parse/traverse.c +++ b/src/parse/traverse.c @@ -11,9 +11,9 @@ ANN m_bool traverse_ast(const Env env, const Ast ast) { } ANN m_bool traverse_exp(const Env env, const Exp exp) { - CHECK_BB(scan1_exp(env, exp)); - CHECK_BB(scan2_exp(env, exp)); - return check_exp(env, exp) ? 1 : -1; + CHECK_BB(scan1_exp(env, exp)); + CHECK_BB(scan2_exp(env, exp)); + return check_exp(env, exp) ? 1 : -1; } ANN static m_bool _traverse_func_def(const Env env, const Func_Def fdef) { @@ -23,26 +23,26 @@ ANN static m_bool _traverse_func_def(const Env env, const Func_Def fdef) { } ANN m_bool traverse_func_def(const Env env, const Func_Def fdef) { - const Func former = env->func; - const m_bool ret = _traverse_func_def(env, fdef); - env->func = former; + const Func former = env->func; + const m_bool ret = _traverse_func_def(env, fdef); + env->func = former; return ret; } ANN m_bool traverse_union_def(const Env env, const Union_Def def) { -// if(!GET_FLAG(def, scan1)) - CHECK_BB(scan1_union_def(env, def)); -// if(!GET_FLAG(def, scan2)) + // if(!GET_FLAG(def, scan1)) + CHECK_BB(scan1_union_def(env, def)); + // if(!GET_FLAG(def, scan2)) CHECK_BB(scan2_union_def(env, def)); -// if(!GET_FLAG(def, check)) - CHECK_BB(check_union_def(env, def)); + // if(!GET_FLAG(def, check)) + CHECK_BB(check_union_def(env, def)); return check_union_def(env, def); } ANN m_bool traverse_enum_def(const Env env, const Enum_Def def) { CHECK_BB(scan0_enum_def(env, def)); CHECK_BB(scan1_enum_def(env, def)); -// CHECK_BB(scan2_enum_def(env, def)); + // CHECK_BB(scan2_enum_def(env, def)); return check_enum_def(env, def); } @@ -50,7 +50,7 @@ ANN m_bool traverse_fptr_def(const Env env, const Fptr_Def def) { CHECK_BB(scan0_fptr_def(env, def)); CHECK_BB(scan1_fptr_def(env, def)); return scan2_fptr_def(env, def); -// CHECK_BB(check_fptr_def(env, def)); + // CHECK_BB(check_fptr_def(env, def)); } ANN m_bool traverse_type_def(const Env env, const Type_Def def) { @@ -62,11 +62,8 @@ ANN m_bool traverse_type_def(const Env env, const Type_Def def) { ANN m_bool traverse_class_def(const Env env, const Class_Def def) { const Type t = def->base.type; - if(!tflag(t, tflag_scan1)) - CHECK_BB(scan1_class_def(env, def)); - if(!tflag(t, tflag_scan2)) - CHECK_BB(scan2_class_def(env, def)); - if(!tflag(t, tflag_check)) - return check_class_def(env, def); + if (!tflag(t, tflag_scan1)) CHECK_BB(scan1_class_def(env, def)); + if (!tflag(t, tflag_scan2)) CHECK_BB(scan2_class_def(env, def)); + if (!tflag(t, tflag_check)) return check_class_def(env, def); return GW_OK; } diff --git a/src/parse/type_decl.c b/src/parse/type_decl.c index 603fec39..56e7f718 100644 --- a/src/parse/type_decl.c +++ b/src/parse/type_decl.c @@ -5,72 +5,68 @@ #include "traverse.h" #include "parse.h" -ANN static Type _option(const Env env, Type_Decl* td, const uint8_t n) { - struct Type_List_ tl = { .td=td }; - Type_Decl tmp = { .xid=insert_symbol("Option"), .types=&tl, .pos=td->pos }; - return !(n-1) ? known_type(env, &tmp) : _option(env, &tmp, n-1); +ANN static Type _option(const Env env, Type_Decl *td, const uint8_t n) { + struct Type_List_ tl = {.td = td}; + Type_Decl tmp = { + .xid = insert_symbol("Option"), .types = &tl, .pos = td->pos}; + return !(n - 1) ? known_type(env, &tmp) : _option(env, &tmp, n - 1); } -ANN static Type option(const Env env, Type_Decl* td) { +ANN static Type option(const Env env, Type_Decl *td) { const uint8_t option = td->option; - td->option = 0; - const Type t = _option(env, td, option); - td->option = option; + td->option = 0; + const Type t = _option(env, td, option); + td->option = option; return t; } -ANN static Type _ref(const Env env, Type_Decl* td, const uint8_t n) { - struct Type_List_ tl = { .td=td }; - Type_Decl tmp = { .xid=insert_symbol("Ref"), .types=&tl, .pos=td->pos }; - return !(n-1) ? known_type(env, &tmp) : _ref(env, &tmp, n-1); +ANN static Type _ref(const Env env, Type_Decl *td, const uint8_t n) { + struct Type_List_ tl = {.td = td}; + Type_Decl tmp = {.xid = insert_symbol("Ref"), .types = &tl, .pos = td->pos}; + return !(n - 1) ? known_type(env, &tmp) : _ref(env, &tmp, n - 1); } -ANN static inline Type ref(const Env env, Type_Decl* td) { +ANN static inline Type ref(const Env env, Type_Decl *td) { const uint8_t ref = td->ref; - td->ref = 0; - const Type t = _ref(env, td, ref); - td->ref = ref; + td->ref = 0; + const Type t = _ref(env, td, ref); + td->ref = ref; return t; } -ANN static inline Type find(const Env env, Type_Decl* td) { - if(!td->fptr) - return find_type(env, td); - if(!td->fptr->type) - CHECK_BO(traverse_fptr_def(env, td->fptr)); +ANN static inline Type find(const Env env, Type_Decl *td) { + if (!td->fptr) return find_type(env, td); + if (!td->fptr->type) CHECK_BO(traverse_fptr_def(env, td->fptr)); return td->fptr->type; } -ANN static inline Type find1(const Env env, const Type base, Type_Decl* td) { - if(!td->fptr) - return scan_type(env, base, td); - if(!td->fptr->type) { +ANN static inline Type find1(const Env env, const Type base, Type_Decl *td) { + if (!td->fptr) return scan_type(env, base, td); + if (!td->fptr->type) { CHECK_BO(scan0_fptr_def(env, td->fptr)); CHECK_BO(traverse_fptr_def(env, td->fptr)); } return td->fptr->type; } -ANN static Type resolve(const Env env, Type_Decl* td) { +ANN static Type resolve(const Env env, Type_Decl *td) { Type_Decl *last = td; - while(last->next) - last = last->next; + while (last->next) last = last->next; DECL_OO(const Type, base, = find(env, td)); const Context ctx = base->info->value->from->ctx; - if(ctx && ctx->error) - ERR_O(td->pos, _("type '%s' is invalid"), base->name) + if (ctx && ctx->error) ERR_O(td->pos, _("type '%s' is invalid"), base->name) DECL_OO(const Type, type, = find1(env, base, td)); - const Type t = !td->ref ? type : ref(env, td); - const Type ret = !td->option ? t : option(env, td); + const Type t = !td->ref ? type : ref(env, td); + const Type ret = !td->option ? t : option(env, td); const Array_Sub array = last->array; - return !array ? ret: array_type(env, ret, array->depth); + return !array ? ret : array_type(env, ret, array->depth); } -ANN static inline void* type_unknown(const Env env, const Type_Decl* td) { +ANN static inline void *type_unknown(const Env env, const Type_Decl *td) { env_err(env, td->pos, _("unknown type '%s'"), s_name(td->xid)); return NULL; } -ANN Type known_type(const Env env, Type_Decl* td) { - return resolve(env, td) ?:type_unknown(env, td); +ANN Type known_type(const Env env, Type_Decl *td) { + return resolve(env, td) ?: type_unknown(env, td); } diff --git a/src/pass.c b/src/pass.c index 57cb6b78..7daaba71 100644 --- a/src/pass.c +++ b/src/pass.c @@ -11,12 +11,15 @@ #define N_PASS 2 #define N_SCANPASS 4 -static const m_str default_passes_name[2] = { "check", "emit" }; -static const compilation_pass default_passes[4] = { traverse_ast, emit_ast }; -static const m_str scan_passes_name[4] = { "scan0", "scan1", "scan2", "type_check" }; -static const compilation_pass scan_passes[4] = { scan0_ast, scan1_ast, scan2_ast, check_ast }; +static const m_str default_passes_name[2] = {"check", "emit"}; +static const compilation_pass default_passes[4] = {traverse_ast, emit_ast}; +static const m_str scan_passes_name[4] = {"scan0", "scan1", "scan2", + "type_check"}; +static const compilation_pass scan_passes[4] = {scan0_ast, scan1_ast, scan2_ast, + check_ast}; -ANN void pass_register(const Gwion gwion, const m_str name, const compilation_pass pass) { +ANN void pass_register(const Gwion gwion, const m_str name, + const compilation_pass pass) { const Symbol sym = insert_symbol(gwion->st, name); map_set(&gwion->data->passes->map, (vtype)sym, (vtype)pass); } @@ -24,11 +27,12 @@ ANN void pass_register(const Gwion gwion, const m_str name, const compilation_pa ANN m_bool pass_set(const Gwion gwion, const Vector passes) { const Vector v = &gwion->data->passes->vec; vector_clear(v); - for(m_uint i = 0; i < vector_size(passes); ++i) { - const m_str name = (m_str)vector_at(passes, i); - const Symbol sym = insert_symbol(gwion->st, name); - const compilation_pass pass = (compilation_pass)map_get(&gwion->data->passes->map, (vtype)sym); - if(!pass) { + for (m_uint i = 0; i < vector_size(passes); ++i) { + const m_str name = (m_str)vector_at(passes, i); + const Symbol sym = insert_symbol(gwion->st, name); + const compilation_pass pass = + (compilation_pass)map_get(&gwion->data->passes->map, (vtype)sym); + if (!pass) { gw_err("Failed to set compilation passes, back to default\n"); pass_default(gwion); return GW_ERROR; @@ -41,18 +45,18 @@ ANN m_bool pass_set(const Gwion gwion, const Vector passes) { ANN void pass_default(const Gwion gwion) { struct Vector_ v; vector_init(&v); - for(m_uint i = 0; i < N_PASS; ++i) + for (m_uint i = 0; i < N_PASS; ++i) vector_add(&v, (vtype)default_passes_name[i]); pass_set(gwion, &v); vector_release(&v); } -ANEW ANN struct Passes_* new_passes(const Gwion gwion) { +ANEW ANN struct Passes_ *new_passes(const Gwion gwion) { struct Passes_ *a = gwion->data->passes = mp_calloc(gwion->mp, Passes); map_init(&a->map); - for(m_uint i = 0; i < N_PASS; ++i) + for (m_uint i = 0; i < N_PASS; ++i) pass_register(gwion, default_passes_name[i], default_passes[i]); - for(m_uint i = 0; i < N_SCANPASS; ++i) + for (m_uint i = 0; i < N_SCANPASS; ++i) pass_register(gwion, scan_passes_name[i], scan_passes[i]); vector_init(&a->vec); return a; diff --git a/src/plug.c b/src/plug.c index 6f142b05..93d90d28 100644 --- a/src/plug.c +++ b/src/plug.c @@ -17,36 +17,36 @@ #include "gwi.h" typedef m_bool (*plugin)(Gwi); -typedef void* (*modini)(const struct Gwion_*, const Vector); -typedef void* (*modend)(const struct Gwion_*, void*); -typedef m_str* (*gwdeps)(void); +typedef void *(*modini)(const struct Gwion_ *, const Vector); +typedef void *(*modend)(const struct Gwion_ *, void *); +typedef m_str *(*gwdeps)(void); struct PlugHandle { MemPool mp; - Map map; - size_t len; + Map map; + size_t len; }; typedef struct Plug_ { void *dl; void *self; - int imp; -} *Plug; + int imp; +} * Plug; -ANN static struct Plug_* new_plug(MemPool p, void *dl) { +ANN static struct Plug_ *new_plug(MemPool p, void *dl) { struct Plug_ *plug = mp_calloc(p, Plug); - plug->dl = dl; + plug->dl = dl; return plug; } ANN static void plug_get(struct PlugHandle *h, const m_str c) { - void *dl = DLOPEN(c, RTLD_LAZY | RTLD_GLOBAL); - const m_str pname = c + h->len + 1; - const size_t sz = strlen(pname) - 3; - char name[PATH_MAX]; + void * dl = DLOPEN(c, RTLD_LAZY | RTLD_GLOBAL); + const m_str pname = c + h->len + 1; + const size_t sz = strlen(pname) - 3; + char name[PATH_MAX]; memcpy(name, pname, sz); name[sz] = '\0'; - if(dl) { + if (dl) { Plug plug = new_plug(h->mp, dl); map_set(h->map, (vtype)strdup(name), (vtype)plug); } else @@ -56,22 +56,20 @@ ANN static void plug_get(struct PlugHandle *h, const m_str c) { ANN static void plug_get_all(struct PlugHandle *h, const m_str name) { #ifndef BUILD_ON_WINDOWS glob_t results; - if(glob(name, 0, NULL, &results)) - return; - for(m_uint i = 0; i < results.gl_pathc; i++) + if (glob(name, 0, NULL, &results)) return; + for (m_uint i = 0; i < results.gl_pathc; i++) plug_get(h, results.gl_pathv[i]); globfree(&results); #else WIN32_FIND_DATA filedata; - HANDLE file = FindFirstFile(name, &filedata); - if(file == INVALID_HANDLE_VALUE) - return; + HANDLE file = FindFirstFile(name, &filedata); + if (file == INVALID_HANDLE_VALUE) return; do { char c[PATH_MAX]; strcpy(c, name); strcpy(c + strlen(name) - 4, filedata.cFileName); plug_get(h, c); - } while(FindNextFile(file, &filedata)); + } while (FindNextFile(file, &filedata)); FindClose(file); #endif } @@ -79,10 +77,10 @@ ANN static void plug_get_all(struct PlugHandle *h, const m_str name) { ANN m_bool plug_ini(const struct Gwion_ *gwion, const Vector list) { const Map map = &gwion->data->plug; map_init(map); - struct PlugHandle h = { .mp=gwion->mp, .map=map }; - for(m_uint i = 0; i < vector_size(list); i++) { + struct PlugHandle h = {.mp = gwion->mp, .map = map}; + for (m_uint i = 0; i < vector_size(list); i++) { const m_str dir = (m_str)vector_at(list, i); - h.len = strlen(dir); + h.len = strlen(dir); char name[PATH_MAX]; sprintf(name, "%s/*.so", dir); plug_get_all(&h, name); @@ -92,11 +90,10 @@ ANN m_bool plug_ini(const struct Gwion_ *gwion, const Vector list) { void free_plug(const struct Gwion_ *gwion) { const Map map = &gwion->data->plug; - for(m_uint i = 0; i < map_size(map); ++i) { - const Plug plug = (Plug)VVAL(map, i); - const modend end = DLSYM(plug->dl, modend, GWMODEND_NAME); - if(end && plug->self) - end(gwion, plug->self); + for (m_uint i = 0; i < map_size(map); ++i) { + const Plug plug = (Plug)VVAL(map, i); + const modend end = DLSYM(plug->dl, modend, GWMODEND_NAME); + if (end && plug->self) end(gwion, plug->self); free((m_str)VKEY(map, i)); DLCLOSE(plug->dl); } @@ -104,16 +101,17 @@ void free_plug(const struct Gwion_ *gwion) { } ANN static void plug_free_arg(MemPool p, const Vector v) { - for(m_uint i = 0; i < vector_size(v); ++i) + for (m_uint i = 0; i < vector_size(v); ++i) free_mstr(p, (m_str)vector_at(v, i)); free_vector(p, v); } -ANN void set_module(const struct Gwion_ *gwion, const m_str name, void *const ptr) { +ANN void set_module(const struct Gwion_ *gwion, const m_str name, + void *const ptr) { const Map map = &gwion->data->plug; - for(m_uint j = 0; j < map_size(map); ++j) { - if(!strcmp(name, (m_str)VKEY(map, j))) { - Plug plug = (Plug)VVAL(map, j); + for (m_uint j = 0; j < map_size(map); ++j) { + if (!strcmp(name, (m_str)VKEY(map, j))) { + Plug plug = (Plug)VVAL(map, j); plug->self = ptr; return; } @@ -122,17 +120,16 @@ ANN void set_module(const struct Gwion_ *gwion, const m_str name, void *const pt ANN void plug_run(const struct Gwion_ *gwion, const Map mod) { const Map map = &gwion->data->plug; - for(m_uint i = 0; i < map_size(mod); ++i) { + for (m_uint i = 0; i < map_size(mod); ++i) { const m_str name = (m_str)VKEY(mod, i); - const m_str opt = (m_str)VVAL(mod, i); - for(m_uint j = 0; j < map_size(map); ++j) { - if(!strcmp(name, (m_str)VKEY(map, j))) { - Plug plug = (Plug)VVAL(map, j); - const Vector arg = opt ? split_args(gwion->mp, opt) : NULL; - const modini ini = DLSYM(plug->dl, modini, GWMODINI_NAME); - plug->self = ini(gwion, arg); - if(arg) - plug_free_arg(gwion->mp, arg); + const m_str opt = (m_str)VVAL(mod, i); + for (m_uint j = 0; j < map_size(map); ++j) { + if (!strcmp(name, (m_str)VKEY(map, j))) { + Plug plug = (Plug)VVAL(map, j); + const Vector arg = opt ? split_args(gwion->mp, opt) : NULL; + const modini ini = DLSYM(plug->dl, modini, GWMODINI_NAME); + plug->self = ini(gwion, arg); + if (arg) plug_free_arg(gwion->mp, arg); } } } @@ -140,10 +137,10 @@ ANN void plug_run(const struct Gwion_ *gwion, const Map mod) { ANN static m_bool dependencies(struct Gwion_ *gwion, const Plug plug) { const gwdeps dep = DLSYM(plug->dl, gwdeps, GWDEPEND_NAME); - if(dep) { + if (dep) { m_str *const base = dep(); - m_str *deps = base; - while(*deps) { + m_str * deps = base; + while (*deps) { CHECK_BB(plugin_ini(gwion, *deps)); ++deps; } @@ -153,29 +150,26 @@ ANN static m_bool dependencies(struct Gwion_ *gwion, const Plug plug) { ANN static m_bool _plugin_ini(struct Gwion_ *gwion, const m_str iname) { const Map map = &gwion->data->plug; - for(m_uint i = 0; i < map_size(map); ++i) { - const Plug plug = (Plug)VVAL(map, i); - const m_str base = (m_str)VKEY(map, i); + for (m_uint i = 0; i < map_size(map); ++i) { + const Plug plug = (Plug)VVAL(map, i); + const m_str base = (m_str)VKEY(map, i); const size_t size = strlen(iname); - char name[size + 1]; + char name[size + 1]; strcpy(name, iname); - for(size_t i = 0; i < size; i++) { - if(name[i] == ':' || name[i] == '[' || name[i] == ']') - name[i] = '_'; + for (size_t i = 0; i < size; i++) { + if (name[i] == ':' || name[i] == '[' || name[i] == ']') name[i] = '_'; } - if(!strcmp(name, base)) { - if(plug->imp) - return GW_OK; + if (!strcmp(name, base)) { + if (plug->imp) return GW_OK; const plugin imp = DLSYM(plug->dl, plugin, GWIMPORT_NAME); - if(!imp) - break; + if (!imp) break; plug->imp = 1; CHECK_BB(dependencies(gwion, plug)); const m_uint scope = env_push_global(gwion->env); - const m_str name = gwion->env->name; - gwion->env->name = iname; - const m_bool ret = gwi_run(gwion, imp); - gwion->env->name = name; + const m_str name = gwion->env->name; + gwion->env->name = iname; + const m_bool ret = gwi_run(gwion, imp); + gwion->env->name = name; env_pop(gwion->env, scope); return ret; } @@ -185,30 +179,27 @@ ANN static m_bool _plugin_ini(struct Gwion_ *gwion, const m_str iname) { } ANN m_bool plugin_ini(struct Gwion_ *gwion, const m_str iname) { - const Context ctx = gwion->env->context; + const Context ctx = gwion->env->context; gwion->env->context = NULL; - const m_bool ret = _plugin_ini(gwion, iname); + const m_bool ret = _plugin_ini(gwion, iname); gwion->env->context = ctx; return ret; } ANN m_bool driver_ini(const struct Gwion_ *gwion) { - const Map map = &gwion->data->plug; - m_str dname = gwion->vm->bbq->si->arg; - m_str opt = strchr(dname, '='); - const char c = opt ? *opt : '\0'; - if(opt) - *opt = '\0'; - for(m_uint i = 0; i < map_size(map); ++i) { + const Map map = &gwion->data->plug; + m_str dname = gwion->vm->bbq->si->arg; + m_str opt = strchr(dname, '='); + const char c = opt ? *opt : '\0'; + if (opt) *opt = '\0'; + for (m_uint i = 0; i < map_size(map); ++i) { const m_str name = (m_str)VKEY(map, i); - if(!strcmp(name, dname)) { - const Plug plug = (Plug)VVAL(map, i); - const f_bbqset drv = DLSYM(plug->dl, f_bbqset, GWDRIVER_NAME); - if(!drv) - break; + if (!strcmp(name, dname)) { + const Plug plug = (Plug)VVAL(map, i); + const f_bbqset drv = DLSYM(plug->dl, f_bbqset, GWDRIVER_NAME); + if (!drv) break; gwion->vm->bbq->func = drv; - if(opt) - *opt = c; + if (opt) *opt = c; return GW_OK; } } @@ -216,10 +207,10 @@ ANN m_bool driver_ini(const struct Gwion_ *gwion) { return GW_ERROR; } -ANN void* get_module(const struct Gwion_ *gwion, const m_str name) { +ANN void *get_module(const struct Gwion_ *gwion, const m_str name) { const Map map = &gwion->data->plug; - for(m_uint i = 0; i < map_size(map); ++i) { - if(!strcmp(name, (m_str)VKEY(map, i))) { + for (m_uint i = 0; i < map_size(map); ++i) { + if (!strcmp(name, (m_str)VKEY(map, i))) { const Plug plug = (Plug)VVAL(map, i); return plug->self; } diff --git a/src/soundinfo.c b/src/soundinfo.c index 7f232a11..6fc0472a 100644 --- a/src/soundinfo.c +++ b/src/soundinfo.c @@ -4,14 +4,14 @@ struct SoundInfo_ *new_soundinfo(MemPool p) { struct SoundInfo_ *si = mp_calloc(p, SoundInfo); si->in = si->out = 2; - si->sr = 48000; + si->sr = 48000; return si; } struct SoundInfo_ *soundinfo_cpy(MemPool p, const struct SoundInfo_ *src) { struct SoundInfo_ *si = mp_calloc(p, SoundInfo); - si->in = src->in; - si->out = src->out; - si->sr = src->sr; + si->in = src->in; + si->out = src->out; + si->sr = src->sr; return si; } diff --git a/src/vm/closure.c b/src/vm/closure.c index df3a0539..6d49a172 100644 --- a/src/vm/closure.c +++ b/src/vm/closure.c @@ -10,9 +10,9 @@ #include "operator.h" #include "import.h" -ANN Closure* new_closure(MemPool mp, const m_uint sz) { +ANN Closure *new_closure(MemPool mp, const m_uint sz) { Closure *a = mp_malloc(mp, Closure); - a->data = mp_malloc2(mp, sz); + a->data = mp_malloc2(mp, sz); map_init(&a->m); a->sz = sz; return a; @@ -20,8 +20,9 @@ ANN Closure* new_closure(MemPool mp, const m_uint sz) { ANN void free_closure(Closure *a, const Gwion gwion) { const Map m = &a->m; - for(m_uint i = 0; i < map_size(m); ++i) - compound_release(gwion->vm->cleaner_shred, (Type)VKEY(m, i), a->data + VVAL(m, i)); + for (m_uint i = 0; i < map_size(m); ++i) + compound_release(gwion->vm->cleaner_shred, (Type)VKEY(m, i), + a->data + VVAL(m, i)); map_release(m); _mp_free(gwion->mp, a->sz, a->data); } diff --git a/src/vm/driver.c b/src/vm/driver.c index 71028033..d6f289f5 100644 --- a/src/vm/driver.c +++ b/src/vm/driver.c @@ -6,43 +6,38 @@ #include "gwion.h" #include "driver.h" -ANN Driver* new_driver(MemPool p) { - Driver* di = (Driver*)mp_calloc(p, BBQ); - di->func = dummy_driver; - di->driver = (DriverData*)mp_calloc(p, DriverData); +ANN Driver *new_driver(MemPool p) { + Driver *di = (Driver *)mp_calloc(p, BBQ); + di->func = dummy_driver; + di->driver = (DriverData *)mp_calloc(p, DriverData); di->is_running = 1; return di; } ANN void free_driver(Driver *d, VM *vm) { - if(d->in) - xfree(d->in); - if(d->out) - xfree(d->out); + if (d->in) xfree(d->in); + if (d->out) xfree(d->out); mp_free(vm->gwion->mp, SoundInfo, d->si); - if(d->driver->del) - d->driver->del(vm, d); + if (d->driver->del) d->driver->del(vm, d); mp_free(vm->gwion->mp, DriverData, d->driver); mp_free(vm->gwion->mp, BBQ, d); } ANN void driver_alloc(Driver *d) { - d->out = (m_float*)xcalloc(d->si->out, SZ_FLOAT); - d->in = (m_float*)xcalloc(d->si->in, SZ_FLOAT); + d->out = (m_float *)xcalloc(d->si->out, SZ_FLOAT); + d->in = (m_float *)xcalloc(d->si->in, SZ_FLOAT); } static DRVRUN(dummy_run) { - while(di->is_running) { + while (di->is_running) { di->run(vm); ++di->pos; } } -static DRVINI(dummy_ini) { - return GW_OK; -} +static DRVINI(dummy_ini) { return GW_OK; } -void dummy_driver(DriverData* dd) { +void dummy_driver(DriverData *dd) { dd->ini = dummy_ini; dd->run = dummy_run; } diff --git a/src/vm/gack.c b/src/vm/gack.c index 9bcae7ff..64cd6a0d 100644 --- a/src/vm/gack.c +++ b/src/vm/gack.c @@ -20,20 +20,19 @@ ANN2(1) static int fmtlen(const char *fmt, va_list args) { return size; } -ANN2(2) static int gw_vasprintf(MemPool mp, char **str, const char *fmt, va_list args) { - char *base = *str; +ANN2(2) +static int gw_vasprintf(MemPool mp, char **str, const char *fmt, va_list args) { + char * base = *str; const size_t base_len = base ? strlen(base) : 0; DECL_BB(const int, size, = fmtlen(fmt, args)); char *ret = mp_malloc2(mp, base_len + size + 1); - if(base) - strcpy(ret, base); + if (base) strcpy(ret, base); const int final_len = vsprintf(ret + base_len, fmt, args); - if(final_len < 0) { + if (final_len < 0) { mp_free2(mp, base_len + size + 1, ret); return -1; } - if(base) - mp_free2(mp, strlen(base), base); + if (base) mp_free2(mp, strlen(base), base); *str = ret; return final_len; } @@ -49,28 +48,28 @@ ANN2(2) int gw_asprintf(MemPool mp, char **str, const char *fmt, ...) { ANN static void prepare_call(const VM_Shred shred, const m_uint offset) { const m_uint push = offset + SZ_INT + sizeof(struct frame_t); shred->mem += push; - struct frame_t *frame = (struct frame_t*)(shred->mem - sizeof(struct frame_t)); + struct frame_t *frame = + (struct frame_t *)(shred->mem - sizeof(struct frame_t)); frame->push = push; frame->code = shred->code; frame->pc = shred->pc; - shred->pc = 0; + shred->pc = 0; } ANN void gack(const VM_Shred shred, const m_uint offset) { - const Type t = *(Type*)shred->reg; + const Type t = *(Type *)shred->reg; const VM_Code code = get_gack(t)->info->gack; - if(code->builtin) { - const m_uint sz = *(m_uint*)(shred->reg + SZ_INT); + if (code->builtin) { + const m_uint sz = *(m_uint *)(shred->reg + SZ_INT); ((f_gack)code->native_func)(t, (shred->reg - sz), shred); POP_REG(shred, sz); } else { prepare_call(shred, offset); - if(tflag(t, tflag_struct)) - *(void**)(shred->mem) = (void*)(shred->reg - t->size); + if (tflag(t, tflag_struct)) + *(void **)(shred->mem) = (void *)(shred->reg - t->size); else - *(M_Object*)(shred->mem) = *(M_Object*)(shred->reg - SZ_INT); + *(M_Object *)(shred->mem) = *(M_Object *)(shred->reg - SZ_INT); shred->code = code; } return; } - diff --git a/src/vm/shreduler.c b/src/vm/shreduler.c index db051de9..063db600 100644 --- a/src/vm/shreduler.c +++ b/src/vm/shreduler.c @@ -13,50 +13,46 @@ ANN void shreduler_set_loop(const Shreduler s, const bool loop) { } ANN VM_Shred shreduler_get(const Shreduler s) { - Driver *const bbq = s->bbq; - struct ShredTick_ *const tk = s->list; - if(tk) { + Driver *const bbq = s->bbq; + struct ShredTick_ *const tk = s->list; + if (tk) { const m_float time = (m_float)bbq->pos + (m_float)GWION_EPSILON; - if(tk->wake_time <= time) { - if((s->list = tk->next)) - s->list->prev = NULL; + if (tk->wake_time <= time) { + if ((s->list = tk->next)) s->list->prev = NULL; tk->next = tk->prev = NULL; - s->curr = tk; + s->curr = tk; return tk->self; } } - if(!s->loop && !vector_size(&s->shreds)) - bbq->is_running = 0; + if (!s->loop && !vector_size(&s->shreds)) bbq->is_running = 0; return NULL; } ANN static inline void shreduler_child(const Vector v) { - for(m_uint i = vector_size(v) + 1; --i;) { + for (m_uint i = vector_size(v) + 1; --i;) { const VM_Shred child = (VM_Shred)vector_at(v, i - 1); shreduler_remove(child->info->vm->shreduler, child, true); } } -ANN static void shreduler_erase(const Shreduler s, struct ShredTick_ *const tk) { - if(tk->parent) - vector_rem2(&tk->parent->child, (vtype)tk->self); - if(tk->child.ptr) - shreduler_child(&tk->child); +ANN static void shreduler_erase(const Shreduler s, + struct ShredTick_ *const tk) { + if (tk->parent) vector_rem2(&tk->parent->child, (vtype)tk->self); + if (tk->child.ptr) shreduler_child(&tk->child); vector_rem2(&s->shreds, (vtype)tk->self); } -ANN void shreduler_remove(const Shreduler s, const VM_Shred out, const bool erase) { +ANN void shreduler_remove(const Shreduler s, const VM_Shred out, + const bool erase) { MUTEX_LOCK(s->mutex); struct ShredTick_ *const tk = out->tick; - if(tk == s->curr) + if (tk == s->curr) s->curr = NULL; - else if(tk == s->list) + else if (tk == s->list) s->list = tk->next; - if(tk->prev) - tk->prev->next = tk->next; - if(tk->next) - tk->next->prev = tk->prev; - if(!erase) + if (tk->prev) tk->prev->next = tk->next; + if (tk->next) tk->next->prev = tk->prev; + if (!erase) tk->prev = tk->next = NULL; else { shreduler_erase(s, tk); @@ -65,35 +61,33 @@ ANN void shreduler_remove(const Shreduler s, const VM_Shred out, const bool eras MUTEX_UNLOCK(s->mutex); } -ANN void shredule(const Shreduler s, const VM_Shred shred, const m_float wake_time) { - const m_float time = wake_time + (m_float)s->bbq->pos; - struct ShredTick_ *tk = shred->tick; - tk->wake_time = time; - if(s->list) { +ANN void shredule(const Shreduler s, const VM_Shred shred, + const m_float wake_time) { + const m_float time = wake_time + (m_float)s->bbq->pos; + struct ShredTick_ *tk = shred->tick; + tk->wake_time = time; + if (s->list) { struct ShredTick_ *curr = s->list, *prev = NULL; do { - if(curr->wake_time > time) - break; + if (curr->wake_time > time) break; prev = curr; - } while((curr = curr->next)); - if(!prev) { + } while ((curr = curr->next)); + if (!prev) { tk->next = s->list; - s->list = (s->list->prev = tk); + s->list = (s->list->prev = tk); } else { - if((tk->next = prev->next)) - prev->next->prev = tk; - tk->prev = prev; + if ((tk->next = prev->next)) prev->next->prev = tk; + tk->prev = prev; prev->next = tk; } } else s->list = tk; - if(tk == s->curr) - s->curr = NULL; + if (tk == s->curr) s->curr = NULL; } ANN void shreduler_ini(const Shreduler s, const VM_Shred shred) { - shred->tick = mp_calloc(shred->info->mp, ShredTick); - shred->tick->self = shred; + shred->tick = mp_calloc(shred->info->mp, ShredTick); + shred->tick->self = shred; shred->tick->shreduler = s; } diff --git a/src/vm/vm.c b/src/vm/vm.c index 26009df8..edd08535 100644 --- a/src/vm/vm.c +++ b/src/vm/vm.c @@ -16,13 +16,13 @@ static inline uint64_t splitmix64_stateless(uint64_t index) { uint64_t z = (index + UINT64_C(0x9E3779B97F4A7C15)); - z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9); - z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB); + z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9); + z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB); return z ^ (z >> 31); } static inline uint32_t rotl(const uint32_t x, int k) { - return (x << k) | (x >> (32 -k)); + return (x << k) | (x >> (32 - k)); } void gw_seed(uint32_t rnd[2], const uint64_t s) { @@ -32,73 +32,72 @@ void gw_seed(uint32_t rnd[2], const uint64_t s) { /*xoroshiro32** */ uint32_t gw_rand(uint32_t s[2]) { - const uint32_t s0 = s[0]; - const uint32_t s1 = s[1] ^ s0; + const uint32_t s0 = s[0]; + const uint32_t s1 = s[1] ^ s0; const uint32_t ret = rotl(s0 * 0x9E3779BB, 5) * 5; - s[0] = rotl(s0, 26) ^ s1 ^ (s1 << 9); - s[1] = rotl(s1, 13); + s[0] = rotl(s0, 26) ^ s1 ^ (s1 << 9); + s[1] = rotl(s1, 13); return ret; } ANN static inline void shred_unwind(const VM_Shred shred) { - register frame_t *frame = &*(frame_t*)(shred->mem - sizeof(frame_t)); - shred->code = frame->code; - shred->pc = frame->pc; + register frame_t *frame = &*(frame_t *)(shred->mem - sizeof(frame_t)); + shred->code = frame->code; + shred->pc = frame->pc; shred->mem -= frame->push; } ANN static bool unwind(VM_Shred shred, const Symbol effect, const m_uint size) { - if(!size) - return true; - if(shred->code->handlers.ptr) { + if (!size) return true; + if (shred->code->handlers.ptr) { const m_uint start = VKEY(&shred->info->frame, size - 1); - if(start > shred->pc) - return true; - const Map m = &shred->code->handlers; - m_uint pc = 0; - for(m_uint i = 0; i < map_size(m); i++) { - if(start > shred->pc) - break; - if(start < shred->pc && VKEY(m, i) > shred->pc) { - const m_uint next = VKEY(m, i); - const Instr instr = (Instr)vector_at(&shred->code->instr, next + 1); - if(!instr->m_val2 || (Symbol)instr->m_val2 == effect) { - pc = next + 1; - break; - } + if (start > shred->pc) return true; + const Map m = &shred->code->handlers; + m_uint pc = 0; + for (m_uint i = 0; i < map_size(m); i++) { + if (start > shred->pc) break; + if (start < shred->pc && VKEY(m, i) > shred->pc) { + const m_uint next = VKEY(m, i); + const Instr instr = (Instr)vector_at(&shred->code->instr, next + 1); + if (!instr->m_val2 || (Symbol)instr->m_val2 == effect) { + pc = next + 1; + break; + } } } - if(!pc) // outside of a try statement + if (!pc) // outside of a try statement return true; - shred->reg = (m_bit*)VPTR(&shred->info->frame, VLEN(&shred->info->frame) - 1); + shred->reg = + (m_bit *)VPTR(&shred->info->frame, VLEN(&shred->info->frame) - 1); shredule(shred->tick->shreduler, shred, 0); - shred->pc = pc;//VKEY(m, i); + shred->pc = pc; // VKEY(m, i); // should we pop? VLEN(&shred->info->frame) = size; return false; } // there might be no more stack to unwind - if(shred->mem == (m_bit*)shred + sizeof(struct VM_Shred_) + SIZEOF_REG) + if (shred->mem == (m_bit *)shred + sizeof(struct VM_Shred_) + SIZEOF_REG) return true; shred_unwind(shred); return unwind(shred, effect, size - 1); } ANN static void trace(VM_Shred shred, const m_uint size) { - const m_uint line = vector_at(&shred->info->line, size-1); - m_uint i; - bool can_skip = false; - for(i = size; --i;) { - const m_uint val = VPTR(&shred->info->line, i-1); - if(!val) + const m_uint line = vector_at(&shred->info->line, size - 1); + m_uint i; + bool can_skip = false; + for (i = size; --i;) { + const m_uint val = VPTR(&shred->info->line, i - 1); + if (!val) can_skip = true; - else if(can_skip && val) + else if (can_skip && val) break; } - loc_t loc = {.first={.line=line, .column=1},.last={.line=line, .column=1}}; + loc_t loc = {.first = {.line = line, .column = 1}, + .last = {.line = line, .column = 1}}; gwerr_secondary("called from here", code_name(shred->code->name, true), loc); gw_err(" {M}┗━╸{0} {-}in code{0} {+W}%s{0}{-}:{0}\n", shred->code->name); - if(shred->mem == (m_bit*)shred + sizeof(struct VM_Shred_) + SIZEOF_REG) + if (shred->mem == (m_bit *)shred + sizeof(struct VM_Shred_) + SIZEOF_REG) return; shred_unwind(shred); trace(shred, i); @@ -107,16 +106,17 @@ ANN static void trace(VM_Shred shred, const m_uint size) { ANN void handle(VM_Shred shred, const m_str effect) { m_bit *const reg = shred->reg; m_bit *const mem = shred->mem; - const m_uint size = shred->info->frame.ptr ? - vector_size(&shred->info->frame) : 0; + const m_uint size = + shred->info->frame.ptr ? vector_size(&shred->info->frame) : 0; const VM_Code code = shred->code; shreduler_remove(shred->tick->shreduler, shred, false); - if(!unwind(shred, insert_symbol(shred->info->vm->gwion->st, effect), size)) + if (!unwind(shred, insert_symbol(shred->info->vm->gwion->st, effect), size)) return; - gw_err("{-C}shred{W}[{Y}% 5" UINT_F "{W}]{-}: {-R}Unhandled {+R}%s{0}\n", shred->tick->xid, effect); - if(shred->info->line.ptr) { // trace if available - shred->reg = reg; - shred->mem = mem; + gw_err("{-C}shred{W}[{Y}% 5" UINT_F "{W}]{-}: {-R}Unhandled {+R}%s{0}\n", + shred->tick->xid, effect); + if (shred->info->line.ptr) { // trace if available + shred->reg = reg; + shred->mem = mem; shred->code = code; gw_err("\n{-/}here is the trace:\n"); trace(shred, vector_size(&shred->info->line)); @@ -124,70 +124,62 @@ ANN void handle(VM_Shred shred, const m_str effect) { vm_shred_exit(shred); } - -void vm_remove(const VM* vm, const m_uint index) { +void vm_remove(const VM *vm, const m_uint index) { const Vector v = (Vector)&vm->shreduler->shreds; LOOP_OPTIM - for(m_uint i = vector_size(v) + 1; --i;) { + for (m_uint i = vector_size(v) + 1; --i;) { const VM_Shred sh = (VM_Shred)vector_at(v, i - 1); - if(sh && sh->tick->xid == index) - handle(sh, "MsgRemove"); + if (sh && sh->tick->xid == index) handle(sh, "MsgRemove"); } } -ANN void free_vm(VM* vm) { +ANN void free_vm(VM *vm) { vector_release(&vm->shreduler->shreds); vector_release(&vm->ugen); - if(vm->bbq) - free_driver(vm->bbq, vm); + if (vm->bbq) free_driver(vm->bbq, vm); MUTEX_CLEANUP(vm->shreduler->mutex); mp_free(vm->gwion->mp, Shreduler, vm->shreduler); mp_free(vm->gwion->mp, VM, vm); } -ANN void vm_add_shred(const VM* vm, const VM_Shred shred) { - shred->info->vm = (VM*)vm; +ANN void vm_add_shred(const VM *vm, const VM_Shred shred) { + shred->info->vm = (VM *)vm; shred->info->me = new_shred(shred); shreduler_add(vm->shreduler, shred); } -ANN void vm_ini_shred(const VM* vm, const VM_Shred shred) { - shred->info->vm = (VM*)vm; +ANN void vm_ini_shred(const VM *vm, const VM_Shred shred) { + shred->info->vm = (VM *)vm; shred->info->me = new_shred(shred); shreduler_ini(vm->shreduler, shred); } ANN void vm_lock(VM const *vm) { - if(vm->parent) - vm_lock(vm->parent); + if (vm->parent) vm_lock(vm->parent); MUTEX_LOCK(vm->shreduler->mutex); } ANN void vm_unlock(VM const *vm) { do MUTEX_UNLOCK(vm->shreduler->mutex); - while((vm = vm->parent)); + while ((vm = vm->parent)); } ANN bool vm_running(VM const *vm) { - if(!vm->shreduler->bbq->is_running) - return 0; - if(!vm->parent) - return 1; + if (!vm->shreduler->bbq->is_running) return 0; + if (!vm->parent) return 1; return vm->shreduler->bbq->is_running = vm_running(vm->parent); } -__attribute__((hot)) -ANN static inline void vm_ugen_init(const VM* vm) { +__attribute__((hot)) ANN static inline void vm_ugen_init(const VM *vm) { const Vector v = (Vector)&vm->ugen; LOOP_OPTIM - for(m_uint i = vector_size(v) + 1; --i;) { + for (m_uint i = vector_size(v) + 1; --i;) { const UGen u = (UGen)vector_at(v, i - 1); - u->done = 0; - if(u->multi) { - struct ugen_multi_* m = u->connect.multi; + u->done = 0; + if (u->multi) { + struct ugen_multi_ *m = u->connect.multi; LOOP_OPTIM - for(m_uint j = m->n_chan + 1; --j;) - UGEN(m->channel[j - 1])->done = 0; + for (m_uint j = m->n_chan + 1; --j;) UGEN(m->channel[j - 1])->done = 0; } } const UGen hole = (UGen)vector_at(v, 0); @@ -195,147 +187,163 @@ ANN static inline void vm_ugen_init(const VM* vm) { } #ifdef DEBUG_STACK -#define VM_INFO \ - gw_err("{-C}shred{W}[{Y}% 5" UINT_F "{W}] {C}mem{W}[{Y}% 5" INT_F"{W}] {C}reg{W}[{Y}% 5" INT_F"{W}]{0}\n", \ - shred->tick->xid, \ - mem - ((m_bit*)shred + sizeof(struct VM_Shred_) + SIZEOF_REG), reg - ((m_bit*)shred + sizeof(struct VM_Shred_))); +#define VM_INFO \ + gw_err("{-C}shred{W}[{Y}% 5" UINT_F "{W}] {C}mem{W}[{Y}% 5" INT_F \ + "{W}] {C}reg{W}[{Y}% 5" INT_F "{W}]{0}\n", \ + shred->tick->xid, \ + mem - ((m_bit *)shred + sizeof(struct VM_Shred_) + SIZEOF_REG), \ + reg - ((m_bit *)shred + sizeof(struct VM_Shred_))); #else #define VM_INFO #endif -ANN static inline m_bool overflow_(const m_bit* mem, const VM_Shred c) { - return mem > (((m_bit*)c + sizeof(struct VM_Shred_) + SIZEOF_REG) + (SIZEOF_MEM) - (MEM_STEP*16)); +ANN static inline m_bool overflow_(const m_bit *mem, const VM_Shred c) { + return mem > (((m_bit *)c + sizeof(struct VM_Shred_) + SIZEOF_REG) + + (SIZEOF_MEM) - (MEM_STEP * 16)); } -ANN /*static inline */VM_Shred init_spork_shred(const VM_Shred shred, const VM_Code code) { +ANN /*static inline */ VM_Shred init_spork_shred(const VM_Shred shred, + const VM_Code code) { const VM_Shred sh = new_shred_base(shred, code); vm_add_shred(shred->info->vm, sh); sh->tick->parent = shred->tick; - if(!shred->tick->child.ptr) - vector_init(&shred->tick->child); + if (!shred->tick->child.ptr) vector_init(&shred->tick->child); vector_add(&shred->tick->child, (vtype)sh); vector_add(&shred->gc, (vtype)sh->info->me); return sh; } -ANN M_Object new_fork(const VM_Shred, const VM_Code code, const Type); -ANN static VM_Shred init_fork_shred(const VM_Shred shred, const VM_Code code, const Type t, const m_uint retsz) { - const M_Object o = new_fork(shred, code, t); - VM* vm = shred->info->vm; - if(!vm->gwion->data->child.ptr) - vector_init(&vm->gwion->data->child); +ANN M_Object new_fork(const VM_Shred, const VM_Code code, const Type); +ANN static VM_Shred init_fork_shred(const VM_Shred shred, const VM_Code code, + const Type t, const m_uint retsz) { + const M_Object o = new_fork(shred, code, t); + VM * vm = shred->info->vm; + if (!vm->gwion->data->child.ptr) vector_init(&vm->gwion->data->child); vector_add(&vm->gwion->data->child, (vtype)o); fork_launch(o, retsz); return ME(o); } -#define TEST0(t, pos) if(!*(t*)(reg-pos)){ shred->pc = PC; handle(shred, "ZeroDivideException"); break; } +#define TEST0(t, pos) \ + if (!*(t *)(reg - pos)) { \ + shred->pc = PC; \ + handle(shred, "ZeroDivideException"); \ + break; \ + } #define ADVANCE() byte += BYTECODE_SZ; -#define SDISPATCH() goto *dispatch[*(m_bit*)byte]; -#define IDISPATCH() { VM_INFO; SDISPATCH(); } +#define SDISPATCH() goto *dispatch[*(m_bit *)byte]; +#define IDISPATCH() \ + { \ + VM_INFO; \ + SDISPATCH(); \ + } -#define SET_BYTE(pc) (byte = bytecode + (pc) * BYTECODE_SZ) +#define SET_BYTE(pc) (byte = bytecode + (pc)*BYTECODE_SZ) -#define PC_DISPATCH(pc)\ - SET_BYTE((pc));\ +#define PC_DISPATCH(pc) \ + SET_BYTE((pc)); \ IDISPATCH(); -#define DISPATCH()\ - ADVANCE();\ +#define DISPATCH() \ + ADVANCE(); \ IDISPATCH(); - #define ADVANCE() byte += BYTECODE_SZ; -#define ADISPATCH() { ADVANCE(); SDISPATCH(); } +#define ADISPATCH() \ + { \ + ADVANCE(); \ + SDISPATCH(); \ + } -#define PC (*(unsigned*)(byte + 1)) +#define PC (*(unsigned *)(byte + 1)) -#define OP(t, sz, op, ...) \ - reg -= sz;\ - __VA_ARGS__\ - *(t*)(reg - sz) op##= *(t*)reg;\ +#define OP(t, sz, op, ...) \ + reg -= sz; \ + __VA_ARGS__ \ + *(t *)(reg - sz) op## = *(t *)reg; \ DISPATCH(); -#define INT_OP(op, ...) OP(m_int, SZ_INT, op, __VA_ARGS__) +#define INT_OP(op, ...) OP(m_int, SZ_INT, op, __VA_ARGS__) #define FLOAT_OP(op, ...) OP(m_float, SZ_FLOAT, op, __VA_ARGS__) -#define LOGICAL(t, sz0, sz, op)\ -reg -= sz0;\ -*(m_int*)(reg-SZ_INT) = (*(t*)(reg - SZ_INT) op *(t*)(reg+sz));\ -DISPATCH() +#define LOGICAL(t, sz0, sz, op) \ + reg -= sz0; \ + *(m_int *)(reg - SZ_INT) = (*(t *)(reg - SZ_INT) op * (t *)(reg + sz)); \ + DISPATCH() #define INT_LOGICAL(op) LOGICAL(m_int, SZ_INT, 0, op) -#define FLOAT_LOGICAL(op) LOGICAL(m_float, SZ_FLOAT * 2 - SZ_INT, \ - SZ_FLOAT - SZ_INT, op) +#define FLOAT_LOGICAL(op) \ + LOGICAL(m_float, SZ_FLOAT * 2 - SZ_INT, SZ_FLOAT - SZ_INT, op) -#define SELF(t, sz,op) \ - *(t*)(reg - sz) = op*(t*)(reg - sz);\ +#define SELF(t, sz, op) \ + *(t *)(reg - sz) = op * (t *)(reg - sz); \ DISPATCH(); // check me -#define R(t, sz, op, ...) \ -reg -= SZ_INT;\ -__VA_ARGS__\ -*(t*)(reg-sz) = (**(t**)reg op##= (*(t*)(reg-sz)));\ -DISPATCH() -#define INT_R(op, ...) R(m_int, SZ_INT, op, __VA_ARGS__) +#define R(t, sz, op, ...) \ + reg -= SZ_INT; \ + __VA_ARGS__ \ + *(t *)(reg - sz) = (**(t **)reg op## = (*(t *)(reg - sz))); \ + DISPATCH() +#define INT_R(op, ...) R(m_int, SZ_INT, op, __VA_ARGS__) #define FLOAT_R(op, ...) R(m_float, SZ_FLOAT, op) -#define INT_PRE(op) \ -/*assert(*(m_int**)(reg-SZ_INT));*/\ -*(m_int*)(reg- SZ_INT) = op(**(m_int**)(reg-SZ_INT));\ -DISPATCH() +#define INT_PRE(op) \ + /*assert(*(m_int**)(reg-SZ_INT));*/ \ + *(m_int *)(reg - SZ_INT) = op(**(m_int **)(reg - SZ_INT)); \ + DISPATCH() -#define INT_POST(op) \ -/*assert(*(m_int**)(reg-SZ_INT));*/\ -*(m_int*)(reg- SZ_INT) = (**(m_int**)(reg-SZ_INT))op;\ -DISPATCH() +#define INT_POST(op) \ + /*assert(*(m_int**)(reg-SZ_INT));*/ \ + *(m_int *)(reg - SZ_INT) = (**(m_int **)(reg - SZ_INT)) op; \ + DISPATCH() -#define IF_OP(op) \ - reg -=SZ_INT;\ - *(m_float*)(reg-SZ_FLOAT) = (m_float)*(m_int*)(reg-SZ_FLOAT) op \ - *(m_float*)(reg + SZ_INT - SZ_FLOAT); \ +#define IF_OP(op) \ + reg -= SZ_INT; \ + *(m_float *)(reg - SZ_FLOAT) = (m_float) * (m_int *)(reg - SZ_FLOAT) op * \ + (m_float *)(reg + SZ_INT - SZ_FLOAT); \ DISPATCH() -#define IF_LOGICAL(op)\ - reg -= SZ_FLOAT; \ - *(m_int*)(reg-SZ_INT) = (m_int)(*(m_int*)(reg-SZ_INT) op *(m_float*)reg); \ +#define IF_LOGICAL(op) \ + reg -= SZ_FLOAT; \ + *(m_int *)(reg - SZ_INT) = \ + (m_int)(*(m_int *)(reg - SZ_INT) op * (m_float *)reg); \ DISPATCH() __attribute__((hot)) -#define IF_R(op) \ - reg -= SZ_INT * 2 - SZ_FLOAT; \ - *(m_float*)(reg-SZ_FLOAT) = (**(m_float**)(reg +SZ_INT - SZ_FLOAT) op##= \ - (m_float)*(m_int*)(reg-SZ_FLOAT)); \ +#define IF_R(op) \ + reg -= SZ_INT * 2 - SZ_FLOAT; \ + *(m_float *)(reg - SZ_FLOAT) = \ + (**(m_float **)(reg + SZ_INT - SZ_FLOAT) op## = \ + (m_float) * (m_int *)(reg - SZ_FLOAT)); \ DISPATCH() -#define FI_OP(op)\ - reg -= SZ_INT; \ - *(m_float*)(reg-SZ_FLOAT) op##= (m_float)*(m_int*)reg; \ +#define FI_OP(op) \ + reg -= SZ_INT; \ + *(m_float *)(reg - SZ_FLOAT) op## = (m_float) * (m_int *)reg; \ DISPATCH() -#define FI_LOGICAL(op) \ - reg -= SZ_FLOAT; \ - *(m_int*)(reg-SZ_INT) = (m_int)(*(m_float*)(reg-SZ_INT) op\ - *(m_int*)(reg + SZ_FLOAT-SZ_INT)); \ +#define FI_LOGICAL(op) \ + reg -= SZ_FLOAT; \ + *(m_int *)(reg - SZ_INT) = (m_int)(*(m_float *)(reg - SZ_INT) op * \ + (m_int *)(reg + SZ_FLOAT - SZ_INT)); \ DISPATCH() -#define FI_R(op, ...) \ - reg -= SZ_FLOAT; \ - __VA_ARGS__ \ - *(m_int*)(reg-SZ_INT) = (**(m_int**)(reg+SZ_FLOAT -SZ_INT) op##= \ - /*(m_int)*/(*(m_float*)(reg-SZ_INT))); \ +#define FI_R(op, ...) \ + reg -= SZ_FLOAT; \ + __VA_ARGS__ \ + *(m_int *)(reg - SZ_INT) = (**(m_int **)(reg + SZ_FLOAT - SZ_INT) op## = \ + /*(m_int)*/ (*(m_float *)(reg - SZ_INT))); \ DISPATCH() - #define STRINGIFY_NX(a) #a -#define STRINGIFY(a) STRINGIFY_NX(a) -#define PPCAT_NX(A, B) A ## B -#define PPCAT(A, B) PPCAT_NX(A, B) +#define STRINGIFY(a) STRINGIFY_NX(a) +#define PPCAT_NX(A, B) A##B +#define PPCAT(A, B) PPCAT_NX(A, B) #if defined(__clang__) #define COMPILER clang @@ -345,766 +353,837 @@ __attribute__((hot)) #define UNINITIALIZED "-Wmaybe-uninitialized") #endif -#define PRAGMA_PUSH() \ +#define PRAGMA_PUSH() \ _Pragma(STRINGIFY(COMPILER diagnostic push)) \ _Pragma(STRINGIFY(COMPILER diagnostic ignored UNINITIALIZED) -#define PRAGMA_POP() _Pragma(STRINGIFY(COMPILER diagnostic pop)) \ +#define PRAGMA_POP() _Pragma(STRINGIFY(COMPILER diagnostic pop)) #define VMSZ (SZ_INT > SZ_FLOAT ? SZ_INT : SZ_FLOAT) -#define VAL (*(m_uint*)(byte + VMSZ)) -#define FVAL (*(m_float*)(byte + VMSZ)) -#define VAL2 (*(m_uint*)(byte + SZ_INT + SZ_INT)) -#define UVAL (*(uint16_t*)(byte + SZ_INT + SZ_INT)) -#define UVAL2 (*(uint16_t*)(byte + SZ_INT + SZ_INT + sizeof(uint16_t))) - -#define BRANCH_DISPATCH(check) \ - if(check) SET_BYTE(VAL);\ - else ADVANCE(); \ +#define VAL (*(m_uint *)(byte + VMSZ)) +#define FVAL (*(m_float *)(byte + VMSZ)) +#define VAL2 (*(m_uint *)(byte + SZ_INT + SZ_INT)) +#define UVAL (*(uint16_t *)(byte + SZ_INT + SZ_INT)) +#define UVAL2 (*(uint16_t *)(byte + SZ_INT + SZ_INT + sizeof(uint16_t))) + +#define BRANCH_DISPATCH(check) \ + if (check) \ + SET_BYTE(VAL); \ + else \ + ADVANCE(); \ IDISPATCH(); -#define VM_OUT shred->code = code; shred->reg = reg; shred->mem = mem; shred->pc = PC; - -__attribute__ ((hot)) -ANN void vm_run(const VM* vm) { // lgtm [cpp/use-of-goto] - static const void* dispatch[] = { - &®setimm, - &®pushimm, &®pushfloat, &®pushother, &®pushaddr, - &®pushmem, &®pushmemfloat, &®pushmemother, &®pushmemaddr, &®pushmemderef, - &&pushnow, - &&baseint, &&basefloat, &&baseother, &&baseaddr, - &®toreg, &®toregother, &®toregaddr, &®toregderef, - &&structmember, &&structmemberfloat, &&structmemberother, &&structmemberaddr, - &&memsetimm, &&memaddimm, &&repeatidx, &&repeat, - &®pushme, &®pushmaybe, - &&funcreturn, - &&_goto, - &&allocint, &&allocfloat, &&allocother, - &&intplus, &&intminus, && intmul, &&intdiv, &&intmod, - // int relationnal - &&inteq, &&intne, &&intand, &&intor, - &&intgt, &&intge, &&intlt, &&intle, - &&intsl, &&intsr, &&intsand, &&intsor, &&intxor, - &&intnegate, &&intnot, &&intcmp, - &&intrassign, - &&intradd, &&intrsub, &&intrmul, &&intrdiv, &&intrmod, - &&intrsl, &&intrsr, &&intrsand, &&intrsor, &&intrxor, - &&preinc, &&predec, - &&postinc, &&postdec, - &&floatadd, &&floatsub, &&floatmul, &&floatdiv, -// logical - &&floatand, &&floator, &&floateq, &&floatne, - &&floatgt, &&floatge, &&floatlt, &&floatle, - &&floatneg, &&floatnot, - &&floatrassign, &&floatradd, &&floatrsub, &&floatrmul, &&floatrdiv, - &&ifadd, &&ifsub, &&ifmul, &&ifdiv, - &&ifand, &&ifor, &&ifeq, &&ifne, &&ifgt, &&ifge, &&iflt, &&ifle, - &&ifrassign, &&ifradd, &&ifrsub, &&ifrmul, &&ifrdiv, - &&fiadd, &&fisub, &&fimul, &&fidiv, - &&fiand, &&fior, &&fieq, &&fine, &&figt, &&fige, &&filt, &&file, - &&firassign, &&firadd, &&firsub, &&firmul, &&firdiv, - &&itof, &&ftoi, - &&timeadv, - &&recurs, &&setcode, &®move, &®tomem, &®tomemother, &&overflow, &&funcusrend, &&funcusrend2, &&funcmemberend, - &&sporkini, &&forkini, &&sporkfunc, &&sporkmemberfptr, &&sporkexp, &&sporkend, - &&brancheqint, &&branchneint, &&brancheqfloat, &&branchnefloat, &&unroll, - &&arrayappend, &&autounrollinit, &&autoloop, &&arraytop, &&arrayaccess, &&arrayget, &&arrayaddr, - &&newobj, &&addref, &&addrefaddr, &&structaddref, &&structaddrefaddr, &&objassign, &&assign, &&remref, - &&except, &&allocmemberaddr, &&dotmember, &&dotfloat, &&dotother, &&dotaddr, - &&unioncheck, &&unionint, &&unionfloat, &&unionother, &&unionaddr, - &&staticint, &&staticfloat, &&staticother, - &&upvalueint, &&upvaluefloat, &&upvalueother, &&upvalueaddr, - &&dotfunc, - &&gcini, &&gcadd, &&gcend, - &&gacktype, &&gackend, &&gack, &&try_ini, &&try_end, &&handleeffect, &&performeffect, &&noop, - &&debugline, &&debugvalue, &&debugpush, &&debugpop, - &&eoc, &&unroll2, &&other, &®pushimm - }; - const Shreduler s = vm->shreduler; +#define VM_OUT \ + shred->code = code; \ + shred->reg = reg; \ + shred->mem = mem; \ + shred->pc = PC; + +__attribute__((hot)) ANN void +vm_run(const VM *vm) { // lgtm [cpp/use-of-goto] + static const void *dispatch[] = { + &®setimm, &®pushimm, &®pushfloat, &®pushother, &®pushaddr, + &®pushmem, &®pushmemfloat, &®pushmemother, &®pushmemaddr, + &®pushmemderef, &&pushnow, &&baseint, &&basefloat, &&baseother, + &&baseaddr, &®toreg, &®toregother, &®toregaddr, &®toregderef, + &&structmember, &&structmemberfloat, &&structmemberother, + &&structmemberaddr, &&memsetimm, &&memaddimm, &&repeatidx, &&repeat, + &®pushme, &®pushmaybe, &&funcreturn, &&_goto, &&allocint, + &&allocfloat, &&allocother, &&intplus, &&intminus, &&intmul, &&intdiv, + &&intmod, + // int relationnal + &&inteq, &&intne, &&intand, &&intor, &&intgt, &&intge, &&intlt, &&intle, + &&intsl, &&intsr, &&intsand, &&intsor, &&intxor, &&intnegate, &&intnot, + &&intcmp, &&intrassign, &&intradd, &&intrsub, &&intrmul, &&intrdiv, + &&intrmod, &&intrsl, &&intrsr, &&intrsand, &&intrsor, &&intrxor, &&preinc, + &&predec, &&postinc, &&postdec, &&floatadd, &&floatsub, &&floatmul, + &&floatdiv, + // logical + &&floatand, &&floator, &&floateq, &&floatne, &&floatgt, &&floatge, + &&floatlt, &&floatle, &&floatneg, &&floatnot, &&floatrassign, &&floatradd, + &&floatrsub, &&floatrmul, &&floatrdiv, &&ifadd, &&ifsub, &&ifmul, &&ifdiv, + &&ifand, &&ifor, &&ifeq, &&ifne, &&ifgt, &&ifge, &&iflt, &&ifle, + &&ifrassign, &&ifradd, &&ifrsub, &&ifrmul, &&ifrdiv, &&fiadd, &&fisub, + &&fimul, &&fidiv, &&fiand, &&fior, &&fieq, &&fine, &&figt, &&fige, &&filt, + &&file, &&firassign, &&firadd, &&firsub, &&firmul, &&firdiv, &&itof, + &&ftoi, &&timeadv, &&recurs, &&setcode, &®move, &®tomem, + &®tomemother, &&overflow, &&funcusrend, &&funcusrend2, &&funcmemberend, + &&sporkini, &&forkini, &&sporkfunc, &&sporkmemberfptr, &&sporkexp, + &&sporkend, &&brancheqint, &&branchneint, &&brancheqfloat, + &&branchnefloat, &&unroll, &&arrayappend, &&autounrollinit, &&autoloop, + &&arraytop, &&arrayaccess, &&arrayget, &&arrayaddr, &&newobj, &&addref, + &&addrefaddr, &&structaddref, &&structaddrefaddr, &&objassign, &&assign, + &&remref, &&except, &&allocmemberaddr, &&dotmember, &&dotfloat, + &&dotother, &&dotaddr, &&unioncheck, &&unionint, &&unionfloat, + &&unionother, &&unionaddr, &&staticint, &&staticfloat, &&staticother, + &&upvalueint, &&upvaluefloat, &&upvalueother, &&upvalueaddr, &&dotfunc, + &&gcini, &&gcadd, &&gcend, &&gacktype, &&gackend, &&gack, &&try_ini, + &&try_end, &&handleeffect, &&performeffect, &&noop, &&debugline, + &&debugvalue, &&debugpush, &&debugpop, &&eoc, &&unroll2, &&other, + &®pushimm}; + const Shreduler s = vm->shreduler; register VM_Shred shred; - register m_bit next; - - while((shred = shreduler_get(s))) { - register VM_Code code = shred->code; - register m_bit* bytecode = code->bytecode; - register m_bit* byte = bytecode + shred->pc * BYTECODE_SZ; - register m_bit* reg = shred->reg; - register m_bit* mem = shred->mem; + register m_bit next; + + while ((shred = shreduler_get(s))) { + register VM_Code code = shred->code; + register m_bit * bytecode = code->bytecode; + register m_bit * byte = bytecode + shred->pc * BYTECODE_SZ; + register m_bit * reg = shred->reg; + register m_bit * mem = shred->mem; register union { M_Object obj; - VM_Code code; + VM_Code code; } a; -PRAGMA_PUSH() + PRAGMA_PUSH() register VM_Shred child; -PRAGMA_POP() - MUTEX_LOCK(s->mutex); - do { - SDISPATCH(); -regsetimm: - *(m_uint*)(reg + (m_int)VAL2) = VAL; - DISPATCH(); -regpushimm: - *(m_uint*)reg = VAL; - reg += SZ_INT; - DISPATCH(); -regpushfloat: - *(m_float*)reg = FVAL; - reg += SZ_FLOAT; - DISPATCH(); -regpushother: -// LOOP_OPTIM - for(m_uint i = 0; i <= VAL2; i+= SZ_INT) - *(m_bit**)(reg+i) = *(m_bit**)(VAL + i); - reg += VAL2; - DISPATCH(); -regpushaddr: - *(m_uint**)reg = &VAL; - reg += SZ_INT; - DISPATCH() -regpushmem: - *(m_uint*)reg = *(m_uint*)(mem + (m_int)VAL); - reg += SZ_INT; - DISPATCH(); -regpushmemfloat: - *(m_float*)reg = *(m_float*)(mem + (m_int)VAL); - reg += SZ_FLOAT; - DISPATCH(); -regpushmemother: - for(m_uint i = 0; i <= VAL2; i+= SZ_INT) - *(m_uint*)(reg+i) = *(m_uint*)((m_bit*)(mem + (m_int)VAL) + i); - reg += VAL2; - DISPATCH(); -regpushmemaddr: - *(m_bit**)reg = &*(m_bit*)(mem + (m_int)VAL); - reg += SZ_INT; - DISPATCH() -regpushmemderef: - memcpy(reg, *(m_uint**)(mem+(m_int)VAL), VAL2); - reg += VAL2; - DISPATCH() -pushnow: - *(m_float*)reg = vm->bbq->pos; - reg += SZ_FLOAT; - DISPATCH(); -baseint: - *(m_uint*)reg = *(m_uint*)(shred->base + VAL); - reg += SZ_INT; - DISPATCH(); -basefloat: - *(m_float*)reg = *(m_float*)(shred->base + VAL); - reg += SZ_FLOAT; - DISPATCH(); -baseother: -// LOOP_OPTIM - for(m_uint i = 0; i <= VAL2; i+= SZ_INT) - *(m_uint*)(reg+i) = *(m_uint*)((shred->base + VAL) + i); - reg += VAL2; - DISPATCH(); -baseaddr: - *(m_uint**)reg = &*(m_uint*)(shred->base + (m_int)VAL); - reg += SZ_INT; - DISPATCH(); -regtoreg: - *(m_uint*)(reg + (m_int)VAL) = *(m_uint*)(reg + (m_int)VAL2); - DISPATCH() -regtoregother: - memcpy(*(m_bit**)(reg - SZ_INT), reg + (m_int)VAL, VAL2); - DISPATCH() -regtoregaddr: - *(m_uint**)(reg + (m_int)VAL) = &*(m_uint*)(reg + (m_int)VAL2); - DISPATCH() -regtoregderef: - memcpy(*(m_bit**)(reg - SZ_INT), *(m_bit**)(reg + (m_int)VAL), VAL2); - DISPATCH() -structmember: - *(m_bit**)(reg-SZ_INT) = *(m_bit**)(*(m_bit**)(reg-SZ_INT) + (m_int)VAL); - DISPATCH() -structmemberfloat: - *(m_bit**)(reg-SZ_INT) = *(m_bit**)(*(m_bit**)(reg-SZ_INT) + (m_int)VAL); - DISPATCH() -structmemberother: - *(m_bit**)(reg-SZ_INT) = *(m_bit**)(*(m_bit**)(reg-SZ_INT) + (m_int)VAL); - DISPATCH() -structmemberaddr: - *(m_bit**)(reg-SZ_INT) = &*(*(m_bit**)(reg-SZ_INT) + (m_int)VAL); - DISPATCH() -memsetimm: - *(m_uint*)(mem+VAL) = VAL2; - DISPATCH(); -memaddimm: - *(m_int*)(mem+VAL) += (m_int)VAL2; -// (*(m_int*)(mem+VAL))--; - DISPATCH(); -repeatidx: - BRANCH_DISPATCH(*(m_int*)(mem+VAL2+SZ_INT) == ++(*(m_int*)(mem+VAL2))); -repeat: - BRANCH_DISPATCH(!(*(m_uint*)(mem+VAL2))--); -regpushme: - *(M_Object*)reg = shred->info->me; - reg += SZ_INT; - DISPATCH() -regpushmaybe: - *(m_uint*)reg = gw_rand((uint32_t*)vm->rand) > (UINT32_MAX / 2); - reg += SZ_INT; - DISPATCH(); -funcreturn: -{ - register frame_t frame = *(frame_t*)(mem - sizeof(frame_t)); - bytecode = (code = frame.code)->bytecode; - mem -= frame.push; - PC_DISPATCH(frame.pc); -} -_goto: - PC_DISPATCH(VAL); -allocint: - *(m_uint*)reg = *(m_uint*)(mem+VAL) = 0; - reg += SZ_INT; - DISPATCH() -allocfloat: - *(m_float*)reg = *(m_float*)(mem+VAL) = 0; - reg += SZ_FLOAT; - DISPATCH() -allocother: -// LOOP_OPTIM - for(m_uint i = 0; i <= VAL2; i += SZ_INT) - *(m_uint*)(reg+i) = (*(m_uint*)(mem+VAL+i) = 0); - reg += VAL2; - DISPATCH() - -intplus: INT_OP(+) -intminus: INT_OP(-) -intmul: INT_OP(*) -intdiv: INT_OP(/, TEST0(m_int, 0)) -intmod: INT_OP(%, TEST0(m_int, 0)) - -inteq: INT_LOGICAL(==) -intne: INT_LOGICAL(!=) -intand: INT_LOGICAL(&&) -intor: INT_LOGICAL(||) -intgt: INT_LOGICAL(>) -intge: INT_LOGICAL(>=) -intlt: INT_LOGICAL(<) -intle: INT_LOGICAL(<=) -intsl: INT_LOGICAL(<<) -intsr: INT_LOGICAL(>>) -intsand: INT_LOGICAL(&) -intsor: INT_LOGICAL(|) -intxor: INT_LOGICAL(^) - -intnegate: - *(m_int*)(reg - SZ_INT) *= -1; - DISPATCH() -intnot: SELF(m_int, SZ_INT, !) -intcmp: SELF(m_int, SZ_INT, ~) - -intrassign: - reg -= SZ_INT; - **(m_int**)reg = *(m_int*)(reg-SZ_INT); - DISPATCH() - -intradd: INT_R(+) -intrsub: INT_R(-) -intrmul: INT_R(*) -//intrdiv: INT_R(/, TEST0(m_int, -SZ_INT)) -//intrmod: INT_R(%, TEST0(m_int, -SZ_INT)) -intrdiv: INT_R(/, TEST0(m_int, SZ_INT)) -intrmod: INT_R(%, TEST0(m_int, SZ_INT)) -intrsl: INT_R(<<) -intrsr: INT_R(>>) -intrsand: INT_R(&) -intrsor: INT_R(|) -intrxor: INT_R(^) - -preinc: INT_PRE(++) -predec: INT_PRE(--) - -postinc: INT_POST(++) -postdec: INT_POST(--) - -floatadd: FLOAT_OP(+) -floatsub: FLOAT_OP(-) -floatmul: FLOAT_OP(*) -floatdiv: FLOAT_OP(/) - -floatand: FLOAT_LOGICAL(&&) -floator: FLOAT_LOGICAL(||) -floateq: FLOAT_LOGICAL(==) -floatne: FLOAT_LOGICAL(!=) -floatgt: FLOAT_LOGICAL(>) -floatge: FLOAT_LOGICAL(>=) -floatlt: FLOAT_LOGICAL(<) -floatle: FLOAT_LOGICAL(<=) - -floatneg: SELF(m_float, SZ_FLOAT, -) - -floatnot: - reg -= SZ_FLOAT - SZ_INT; - *(m_int*)(reg - SZ_INT) = !*(m_float*)(reg - SZ_INT); - DISPATCH() - -floatrassign: - reg -= SZ_INT; - **(m_float**)reg = *(m_float*)(reg-SZ_FLOAT); - DISPATCH() - -floatradd: FLOAT_R(+) -floatrsub: FLOAT_R(-) -floatrmul: FLOAT_R(*) -floatrdiv: FLOAT_R(/) - -ifadd: IF_OP(+) -ifsub: IF_OP(-) -ifmul: IF_OP(*) -ifdiv: IF_OP(/) - -ifand: IF_LOGICAL(&&) -ifor: IF_LOGICAL(||) -ifeq: IF_LOGICAL(==) -ifne: IF_LOGICAL(!=) -ifgt: IF_LOGICAL(>) -ifge: IF_LOGICAL(>=) -iflt: IF_LOGICAL(<) -ifle: IF_LOGICAL(<=) - -ifrassign: IF_R() -ifradd: IF_R(+) -ifrsub: IF_R(-) -ifrmul: IF_R(*) -ifrdiv: IF_R(/) - -fiadd: FI_OP(+) -fisub: FI_OP(-) -fimul: FI_OP(*) -fidiv: FI_OP(/) - -fiand: FI_LOGICAL(&&) -fior: FI_LOGICAL(||) -fieq: FI_LOGICAL(==) -fine: FI_LOGICAL(!=) -figt: FI_LOGICAL( >) -fige: FI_LOGICAL(>=) -filt: FI_LOGICAL( <) -file: FI_LOGICAL(<=) - -firassign: - reg -=SZ_FLOAT; - *(m_int*)(reg-SZ_INT) = **(m_int**)(reg + SZ_FLOAT-SZ_INT) = - (m_int)*(m_float*)(reg-SZ_INT); - DISPATCH() - -firadd: FI_R(+) -firsub: FI_R(-) -firmul: FI_R(*) -firdiv: FI_R(/, TEST0(m_float, SZ_INT)) - -itof: - reg -= SZ_INT - SZ_FLOAT; - *(m_float*)(reg-SZ_FLOAT) = (m_float)*(m_int*)(reg-SZ_FLOAT); - DISPATCH() -ftoi: - reg -= SZ_FLOAT - SZ_INT; - *(m_int*)(reg-SZ_INT) = (m_int)*(m_float*)(reg-SZ_INT); - DISPATCH() -timeadv: - reg -= SZ_FLOAT; - shredule(s, shred, *(m_float*)(reg-SZ_FLOAT)); - *(m_float*)(reg-SZ_FLOAT) += vm->bbq->pos; - VM_OUT - break; -recurs: -{ - register const uint push = UVAL2; - mem += push; - *(frame_t*)(mem - sizeof(frame_t)) = (frame_t){.code=code,.pc=UVAL,.push=push}; - reg += (m_int)VAL; - next = eFuncUsrEnd2; -} - DISPATCH(); -setcode: -PRAGMA_PUSH() - a.code = *(VM_Code*)(reg - SZ_INT); - if(!a.code->builtin) { - register const uint push = *(m_uint*)reg /*+ code->stack_depth*/ + sizeof(frame_t); - mem += push; - *(frame_t*)(mem - sizeof(frame_t)) = (frame_t){ .code=code, .pc=PC+UVAL, .push=push }; - next = eFuncUsrEnd; - } else { - mem += *(m_uint*)reg; - next = eFuncMemberEnd; - } -PRAGMA_POP() -regmove: - reg += (m_int)VAL; - DISPATCH(); -regtomem: - *(m_uint*)(mem+VAL) = *(m_uint*)(reg+(m_int)VAL2); - DISPATCH() -regtomemother: - memcpy(mem+VAL, reg, VAL2); - DISPATCH() -overflow: - if(overflow_(mem + VAL2, shred)) { - shred->pc = PC; - handle(shred, "StackOverflow"); - continue; - } -PRAGMA_PUSH() - goto *dispatch[next]; -PRAGMA_POP() -funcusrend: -PRAGMA_PUSH() - byte = bytecode = (code = a.code)->bytecode; -PRAGMA_POP() - SDISPATCH(); -funcusrend2: - byte = bytecode; - SDISPATCH(); -funcmemberend: - VM_OUT - { - register const m_uint val = VAL; - register const m_uint val2 = VAL2; - ((f_mfun)a.code->native_func)((*(M_Object*)mem), reg, shred); - reg += val; - shred->mem = (mem -= val2); - if(!s->curr)break; - } - PC_DISPATCH(shred->pc) -sporkini: - child = init_spork_shred(shred, (VM_Code)VAL); - DISPATCH() -forkini: - reg -= SZ_INT; - child = init_fork_shred(shred, (VM_Code)VAL, *(Type*)reg, VAL2), - DISPATCH() -sporkfunc: -// LOOP_OPTIM -PRAGMA_PUSH() - for(m_uint i = 0; i < VAL; i+= SZ_INT) - *(m_uint*)(child->reg + i) = *(m_uint*)(reg + i + (m_int)VAL2); - child->reg += VAL; - DISPATCH() -PRAGMA_POP() -sporkmemberfptr: - for(m_uint i = SZ_INT; i < VAL; i+= SZ_INT) - *(m_uint*)(child->reg + i) = *(m_uint*)(reg - VAL + i); - *(M_Object*)(child->reg + VAL) = *(M_Object*)(reg + VAL + SZ_INT); - *(m_uint*)(child->reg + VAL + SZ_INT) = *(m_uint*)(reg + VAL - SZ_INT*2); - child->reg += VAL + SZ_INT*2; - DISPATCH() -sporkexp: -// LOOP_OPTIM - for(m_uint i = 0; i < VAL; i+= SZ_INT) - *(m_uint*)(child->mem + i) = *(m_uint*)(mem+i); - DISPATCH() -sporkend: - assert(!VAL); // spork are not mutable -PRAGMA_PUSH() - *(M_Object*)(reg-SZ_INT) = child->info->me; -PRAGMA_POP() - DISPATCH() -brancheqint: - reg -= SZ_INT; - BRANCH_DISPATCH(!*(m_uint*)reg); -branchneint: - reg -= SZ_INT; - BRANCH_DISPATCH(*(m_uint*)reg); -brancheqfloat: - reg -= SZ_FLOAT; - BRANCH_DISPATCH(!*(m_float*)reg); -branchnefloat: - reg -= SZ_FLOAT; - BRANCH_DISPATCH(*(m_float*)reg); -unroll: -{ - const m_uint n = *(m_uint*)(mem + VAL - SZ_INT); - const m_uint idx = *(m_uint*)(mem + VAL); - if(idx > 0) { - if(idx >= n) { - *(m_uint*)(mem + VAL) -= n; - DISPATCH() + PRAGMA_POP() + MUTEX_LOCK(s->mutex); + do { + SDISPATCH(); + regsetimm: + *(m_uint *)(reg + (m_int)VAL2) = VAL; + DISPATCH(); + regpushimm: + *(m_uint *)reg = VAL; + reg += SZ_INT; + DISPATCH(); + regpushfloat: + *(m_float *)reg = FVAL; + reg += SZ_FLOAT; + DISPATCH(); + regpushother: + // LOOP_OPTIM + for (m_uint i = 0; i <= VAL2; i += SZ_INT) + *(m_bit **)(reg + i) = *(m_bit **)(VAL + i); + reg += VAL2; + DISPATCH(); + regpushaddr: + *(m_uint **)reg = &VAL; + reg += SZ_INT; + DISPATCH() + regpushmem: + *(m_uint *)reg = *(m_uint *)(mem + (m_int)VAL); + reg += SZ_INT; + DISPATCH(); + regpushmemfloat: + *(m_float *)reg = *(m_float *)(mem + (m_int)VAL); + reg += SZ_FLOAT; + DISPATCH(); + regpushmemother: + for (m_uint i = 0; i <= VAL2; i += SZ_INT) + *(m_uint *)(reg + i) = *(m_uint *)((m_bit *)(mem + (m_int)VAL) + i); + reg += VAL2; + DISPATCH(); + regpushmemaddr: + *(m_bit **)reg = &*(m_bit *)(mem + (m_int)VAL); + reg += SZ_INT; + DISPATCH() + regpushmemderef: + memcpy(reg, *(m_uint **)(mem + (m_int)VAL), VAL2); + reg += VAL2; + DISPATCH() + pushnow: + *(m_float *)reg = vm->bbq->pos; + reg += SZ_FLOAT; + DISPATCH(); + baseint: + *(m_uint *)reg = *(m_uint *)(shred->base + VAL); + reg += SZ_INT; + DISPATCH(); + basefloat: + *(m_float *)reg = *(m_float *)(shred->base + VAL); + reg += SZ_FLOAT; + DISPATCH(); + baseother: + // LOOP_OPTIM + for (m_uint i = 0; i <= VAL2; i += SZ_INT) + *(m_uint *)(reg + i) = *(m_uint *)((shred->base + VAL) + i); + reg += VAL2; + DISPATCH(); + baseaddr: + *(m_uint **)reg = &*(m_uint *)(shred->base + (m_int)VAL); + reg += SZ_INT; + DISPATCH(); + regtoreg: + *(m_uint *)(reg + (m_int)VAL) = *(m_uint *)(reg + (m_int)VAL2); + DISPATCH() + regtoregother: + memcpy(*(m_bit **)(reg - SZ_INT), reg + (m_int)VAL, VAL2); + DISPATCH() + regtoregaddr: + *(m_uint **)(reg + (m_int)VAL) = &*(m_uint *)(reg + (m_int)VAL2); + DISPATCH() + regtoregderef: + memcpy(*(m_bit **)(reg - SZ_INT), *(m_bit **)(reg + (m_int)VAL), VAL2); + DISPATCH() + structmember: + *(m_bit **)(reg - SZ_INT) = + *(m_bit **)(*(m_bit **)(reg - SZ_INT) + (m_int)VAL); + DISPATCH() + structmemberfloat: + *(m_bit **)(reg - SZ_INT) = + *(m_bit **)(*(m_bit **)(reg - SZ_INT) + (m_int)VAL); + DISPATCH() + structmemberother: + *(m_bit **)(reg - SZ_INT) = + *(m_bit **)(*(m_bit **)(reg - SZ_INT) + (m_int)VAL); + DISPATCH() + structmemberaddr: + *(m_bit **)(reg - SZ_INT) = &*(*(m_bit **)(reg - SZ_INT) + (m_int)VAL); + DISPATCH() + memsetimm: + *(m_uint *)(mem + VAL) = VAL2; + DISPATCH(); + memaddimm: + *(m_int *)(mem + VAL) += (m_int)VAL2; + // (*(m_int*)(mem+VAL))--; + DISPATCH(); + repeatidx: + BRANCH_DISPATCH(*(m_int *)(mem + VAL2 + SZ_INT) == + ++(*(m_int *)(mem + VAL2))); + repeat: + BRANCH_DISPATCH(!(*(m_uint *)(mem + VAL2))--); + regpushme: + *(M_Object *)reg = shred->info->me; + reg += SZ_INT; + DISPATCH() + regpushmaybe: + *(m_uint *)reg = gw_rand((uint32_t *)vm->rand) > (UINT32_MAX / 2); + reg += SZ_INT; + DISPATCH(); + funcreturn : { + register frame_t frame = *(frame_t *)(mem - sizeof(frame_t)); + bytecode = (code = frame.code)->bytecode; + mem -= frame.push; + PC_DISPATCH(frame.pc); } - *(m_uint*)(mem + VAL) = 0; - PC_DISPATCH(PC + VAL2*(idx+1)); - } - PC_DISPATCH(PC + VAL2 *n + 1); -} -arrayappend: - m_vector_add(ARRAY(*(M_Object*)(reg-SZ_INT)), reg); - DISPATCH() -autounrollinit: - *(m_uint*)(mem + VAL) = m_vector_size(ARRAY(*(M_Object*)(mem+VAL+SZ_INT))); - DISPATCH() -autoloop: - *(m_bit**)(mem + VAL2 + SZ_INT) = m_vector_addr(ARRAY(*(M_Object*)(mem+VAL2-SZ_INT)), *(m_uint*)(mem + VAL2) + 1); - BRANCH_DISPATCH(m_vector_size(ARRAY(*(M_Object*)(mem+VAL2-SZ_INT))) == ++*(m_uint*)(mem + VAL2)); -arraytop: - if(*(m_uint*)(reg - SZ_INT * 2) < *(m_uint*)(reg-SZ_INT)) - goto newobj; - else - goto _goto; -arrayaccess: -{ - register const m_int idx = *(m_int*)(reg + VAL); - a.obj = *(M_Object*)(reg-VAL2); - if(idx < 0 || (m_uint)idx >= m_vector_size(ARRAY(a.obj))) { - gw_err(_("{-} ... at index {W}[{Y}%" INT_F "{W}]{0}\n"), idx); -// gw_err(_(" ... at dimension [%" INT_F "]\n"), VAL); - VM_OUT - handle(shred, "ArrayOutofBounds"); - continue; // or break ? - } - DISPATCH() -} -arrayget: -PRAGMA_PUSH() - m_vector_get(ARRAY(a.obj), *(m_int*)(reg + VAL), (reg + (m_int)VAL2)); -PRAGMA_POP() - DISPATCH() -arrayaddr: -PRAGMA_PUSH() - *(m_bit**)(reg + (m_int)VAL2) = m_vector_addr(ARRAY(a.obj), *(m_int*)(reg + VAL)); -PRAGMA_POP() - DISPATCH() -newobj: - *(M_Object*)reg = new_object(vm->gwion->mp, NULL, (Type)VAL2); - reg += SZ_INT; - DISPATCH() -addref: - { - const M_Object o = *(M_Object*)(reg+(m_int)VAL); -// if(o) - ++o->ref; - } - DISPATCH() -addrefaddr: - { - const M_Object o = **(M_Object**)(reg+(m_int)VAL); - if(o) + _goto: + PC_DISPATCH(VAL); + allocint: + *(m_uint *)reg = *(m_uint *)(mem + VAL) = 0; + reg += SZ_INT; + DISPATCH() + allocfloat: + *(m_float *)reg = *(m_float *)(mem + VAL) = 0; + reg += SZ_FLOAT; + DISPATCH() + allocother: + // LOOP_OPTIM + for (m_uint i = 0; i <= VAL2; i += SZ_INT) + *(m_uint *)(reg + i) = (*(m_uint *)(mem + VAL + i) = 0); + reg += VAL2; + DISPATCH() + + intplus: + INT_OP(+) + intminus: + INT_OP(-) + intmul: + INT_OP(*) + intdiv: + INT_OP(/, TEST0(m_int, 0)) + intmod: + INT_OP(%, TEST0(m_int, 0)) + + inteq: + INT_LOGICAL(==) + intne: + INT_LOGICAL(!=) + intand: + INT_LOGICAL(&&) + intor: + INT_LOGICAL(||) + intgt: + INT_LOGICAL(>) + intge: + INT_LOGICAL(>=) + intlt: + INT_LOGICAL(<) + intle: + INT_LOGICAL(<=) + intsl: + INT_LOGICAL(<<) + intsr: + INT_LOGICAL(>>) + intsand: + INT_LOGICAL(&) + intsor: + INT_LOGICAL(|) + intxor: + INT_LOGICAL(^) + + intnegate: + *(m_int *)(reg - SZ_INT) *= -1; + DISPATCH() + intnot: + SELF(m_int, SZ_INT, !) + intcmp: + SELF(m_int, SZ_INT, ~) + + intrassign: + reg -= SZ_INT; + **(m_int **)reg = *(m_int *)(reg - SZ_INT); + DISPATCH() + + intradd: + INT_R(+) + intrsub: + INT_R(-) + intrmul: + INT_R(*) + // intrdiv: INT_R(/, TEST0(m_int, -SZ_INT)) + // intrmod: INT_R(%, TEST0(m_int, -SZ_INT)) + intrdiv: + INT_R(/, TEST0(m_int, SZ_INT)) + intrmod: + INT_R(%, TEST0(m_int, SZ_INT)) + intrsl: + INT_R(<<) + intrsr: + INT_R(>>) + intrsand: + INT_R(&) + intrsor: + INT_R(|) + intrxor: + INT_R(^) + + preinc: + INT_PRE(++) + predec: + INT_PRE(--) + + postinc: + INT_POST(++) + postdec: + INT_POST(--) + + floatadd: + FLOAT_OP(+) + floatsub: + FLOAT_OP(-) + floatmul: + FLOAT_OP(*) + floatdiv: + FLOAT_OP(/) + + floatand: + FLOAT_LOGICAL(&&) + floator: + FLOAT_LOGICAL(||) + floateq: + FLOAT_LOGICAL(==) + floatne: + FLOAT_LOGICAL(!=) + floatgt: + FLOAT_LOGICAL(>) + floatge: + FLOAT_LOGICAL(>=) + floatlt: + FLOAT_LOGICAL(<) + floatle: + FLOAT_LOGICAL(<=) + + floatneg: + SELF(m_float, SZ_FLOAT, -) + + floatnot: + reg -= SZ_FLOAT - SZ_INT; + *(m_int *)(reg - SZ_INT) = !*(m_float *)(reg - SZ_INT); + DISPATCH() + + floatrassign: + reg -= SZ_INT; + **(m_float **)reg = *(m_float *)(reg - SZ_FLOAT); + DISPATCH() + + floatradd: + FLOAT_R(+) + floatrsub: + FLOAT_R(-) + floatrmul: + FLOAT_R(*) + floatrdiv: + FLOAT_R(/) + + ifadd: + IF_OP(+) + ifsub: + IF_OP(-) + ifmul: + IF_OP(*) + ifdiv: + IF_OP(/) + + ifand: + IF_LOGICAL(&&) + ifor: + IF_LOGICAL(||) + ifeq: + IF_LOGICAL(==) + ifne: + IF_LOGICAL(!=) + ifgt: + IF_LOGICAL(>) + ifge: + IF_LOGICAL(>=) + iflt: + IF_LOGICAL(<) + ifle: + IF_LOGICAL(<=) + + ifrassign: + IF_R() + ifradd: + IF_R(+) + ifrsub: + IF_R(-) + ifrmul: + IF_R(*) + ifrdiv: + IF_R(/) + + fiadd: + FI_OP(+) + fisub: + FI_OP(-) + fimul: + FI_OP(*) + fidiv: + FI_OP(/) + + fiand: + FI_LOGICAL(&&) + fior: + FI_LOGICAL(||) + fieq: + FI_LOGICAL(==) + fine: + FI_LOGICAL(!=) + figt: + FI_LOGICAL(>) + fige: + FI_LOGICAL(>=) + filt: + FI_LOGICAL(<) + file: + FI_LOGICAL(<=) + + firassign: + reg -= SZ_FLOAT; + *(m_int *)(reg - SZ_INT) = **(m_int **)(reg + SZ_FLOAT - SZ_INT) = + (m_int) * (m_float *)(reg - SZ_INT); + DISPATCH() + + firadd: + FI_R(+) + firsub: + FI_R(-) + firmul: + FI_R(*) + firdiv: + FI_R(/, TEST0(m_float, SZ_INT)) + + itof: + reg -= SZ_INT - SZ_FLOAT; + *(m_float *)(reg - SZ_FLOAT) = (m_float) * (m_int *)(reg - SZ_FLOAT); + DISPATCH() + ftoi: + reg -= SZ_FLOAT - SZ_INT; + *(m_int *)(reg - SZ_INT) = (m_int) * (m_float *)(reg - SZ_INT); + DISPATCH() + timeadv: + reg -= SZ_FLOAT; + shredule(s, shred, *(m_float *)(reg - SZ_FLOAT)); + *(m_float *)(reg - SZ_FLOAT) += vm->bbq->pos; + VM_OUT + break; + recurs : { + register const uint push = UVAL2; + mem += push; + *(frame_t *)(mem - sizeof(frame_t)) = + (frame_t) {.code = code, .pc = UVAL, .push = push}; + reg += (m_int)VAL; + next = eFuncUsrEnd2; + } + DISPATCH(); + setcode: + PRAGMA_PUSH() + a.code = *(VM_Code *)(reg - SZ_INT); + if (!a.code->builtin) { + register const uint push = + *(m_uint *)reg /*+ code->stack_depth*/ + sizeof(frame_t); + mem += push; + *(frame_t *)(mem - sizeof(frame_t)) = + (frame_t) {.code = code, .pc = PC + UVAL, .push = push}; + next = eFuncUsrEnd; + } else { + mem += *(m_uint *)reg; + next = eFuncMemberEnd; + } + PRAGMA_POP() + regmove: + reg += (m_int)VAL; + DISPATCH(); + regtomem: + *(m_uint *)(mem + VAL) = *(m_uint *)(reg + (m_int)VAL2); + DISPATCH() + regtomemother: + memcpy(mem + VAL, reg, VAL2); + DISPATCH() + overflow: + if (overflow_(mem + VAL2, shred)) { + shred->pc = PC; + handle(shred, "StackOverflow"); + continue; + } + PRAGMA_PUSH() + goto *dispatch[next]; + PRAGMA_POP() + funcusrend: + PRAGMA_PUSH() + byte = bytecode = (code = a.code)->bytecode; + PRAGMA_POP() + SDISPATCH(); + funcusrend2: + byte = bytecode; + SDISPATCH(); + funcmemberend: + VM_OUT { + register const m_uint val = VAL; + register const m_uint val2 = VAL2; + ((f_mfun)a.code->native_func)((*(M_Object *)mem), reg, shred); + reg += val; + shred->mem = (mem -= val2); + if (!s->curr) break; + } + PC_DISPATCH(shred->pc) + sporkini: + child = init_spork_shred(shred, (VM_Code)VAL); + DISPATCH() + forkini: + reg -= SZ_INT; + child = init_fork_shred(shred, (VM_Code)VAL, *(Type *)reg, VAL2), + DISPATCH() sporkfunc : + // LOOP_OPTIM + PRAGMA_PUSH() for (m_uint i = 0; i < VAL; i += SZ_INT) * + (m_uint *)(child->reg + i) = *(m_uint *)(reg + i + (m_int)VAL2); + child->reg += VAL; + DISPATCH() + PRAGMA_POP() + sporkmemberfptr: + for (m_uint i = SZ_INT; i < VAL; i += SZ_INT) + *(m_uint *)(child->reg + i) = *(m_uint *)(reg - VAL + i); + *(M_Object *)(child->reg + VAL) = *(M_Object *)(reg + VAL + SZ_INT); + *(m_uint *)(child->reg + VAL + SZ_INT) = + *(m_uint *)(reg + VAL - SZ_INT * 2); + child->reg += VAL + SZ_INT * 2; + DISPATCH() + sporkexp: + // LOOP_OPTIM + for (m_uint i = 0; i < VAL; i += SZ_INT) + *(m_uint *)(child->mem + i) = *(m_uint *)(mem + i); + DISPATCH() + sporkend: + assert(!VAL); // spork are not mutable + PRAGMA_PUSH() + *(M_Object *)(reg - SZ_INT) = child->info->me; + PRAGMA_POP() + DISPATCH() + brancheqint: + reg -= SZ_INT; + BRANCH_DISPATCH(!*(m_uint *)reg); + branchneint: + reg -= SZ_INT; + BRANCH_DISPATCH(*(m_uint *)reg); + brancheqfloat: + reg -= SZ_FLOAT; + BRANCH_DISPATCH(!*(m_float *)reg); + branchnefloat: + reg -= SZ_FLOAT; + BRANCH_DISPATCH(*(m_float *)reg); + unroll : { + const m_uint n = *(m_uint *)(mem + VAL - SZ_INT); + const m_uint idx = *(m_uint *)(mem + VAL); + if (idx > 0) { + if (idx >= n) { + *(m_uint *)(mem + VAL) -= n; + DISPATCH() + } + *(m_uint *)(mem + VAL) = 0; + PC_DISPATCH(PC + VAL2 * (idx + 1)); + } + PC_DISPATCH(PC + VAL2 * n + 1); + } + arrayappend: + m_vector_add(ARRAY(*(M_Object *)(reg - SZ_INT)), reg); + DISPATCH() + autounrollinit: + *(m_uint *)(mem + VAL) = + m_vector_size(ARRAY(*(M_Object *)(mem + VAL + SZ_INT))); + DISPATCH() + autoloop: + *(m_bit **)(mem + VAL2 + SZ_INT) = + m_vector_addr(ARRAY(*(M_Object *)(mem + VAL2 - SZ_INT)), + *(m_uint *)(mem + VAL2) + 1); + BRANCH_DISPATCH( + m_vector_size(ARRAY(*(M_Object *)(mem + VAL2 - SZ_INT))) == + ++*(m_uint *)(mem + VAL2)); + arraytop: + if (*(m_uint *)(reg - SZ_INT * 2) < *(m_uint *)(reg - SZ_INT)) + goto newobj; + else + goto _goto; + arrayaccess : { + register const m_int idx = *(m_int *)(reg + VAL); + a.obj = *(M_Object *)(reg - VAL2); + if (idx < 0 || (m_uint)idx >= m_vector_size(ARRAY(a.obj))) { + gw_err(_("{-} ... at index {W}[{Y}%" INT_F "{W}]{0}\n"), idx); + // gw_err(_(" ... at dimension [%" INT_F "]\n"), VAL); + VM_OUT + handle(shred, "ArrayOutofBounds"); + continue; // or break ? + } + DISPATCH() + } + arrayget: + PRAGMA_PUSH() + m_vector_get(ARRAY(a.obj), *(m_int *)(reg + VAL), (reg + (m_int)VAL2)); + PRAGMA_POP() + DISPATCH() + arrayaddr: + PRAGMA_PUSH() + *(m_bit **)(reg + (m_int)VAL2) = + m_vector_addr(ARRAY(a.obj), *(m_int *)(reg + VAL)); + PRAGMA_POP() + DISPATCH() + newobj: + *(M_Object *)reg = new_object(vm->gwion->mp, NULL, (Type)VAL2); + reg += SZ_INT; + DISPATCH() + addref : { + const M_Object o = *(M_Object *)(reg + (m_int)VAL); + // if(o) ++o->ref; - } - DISPATCH() -structaddref: - struct_addref(vm->gwion, (Type)VAL2, *(m_bit**)(reg + (m_int)VAL)); - DISPATCH() -structaddrefaddr: - struct_addref(vm->gwion, (Type)VAL2, **(m_bit***)(reg + (m_int)VAL)); - DISPATCH() -objassign: -{ - const M_Object o = **(M_Object**)(reg -SZ_INT); - release(o, shred); -} -assign: - reg -= SZ_INT; - **(M_Object**)reg = *(M_Object*)(reg-SZ_INT); - DISPATCH() -remref: - release(*(M_Object*)(mem + VAL), shred); - DISPATCH() -except: -/* TODO: Refactor except instruction * - * so that * - * VAL = offset (no default SZ_INT) * - * VAL2 = error message * - * grep for GWOP_EXCEPT and Except, exception... */ - if(!*(m_bit**)(reg+(m_int)(VAL))) { - shred->pc = PC; - handle(shred, "NullPtrException"); - continue; - } - DISPATCH(); -allocmemberaddr: - *(m_bit**)reg = (*(M_Object*)mem)->data + VAL; - reg += SZ_INT; - DISPATCH() -dotmember: - *(m_uint*)(reg-SZ_INT) = *(m_uint*)((*(M_Object*)(reg-SZ_INT))->data + VAL); - DISPATCH() -dotfloat: - *(m_float*)(reg-SZ_INT) = *(m_float*)((*(M_Object*)(reg-SZ_INT))->data + VAL); - reg += SZ_FLOAT - SZ_INT; - DISPATCH() -dotother: -// LOOP_OPTIM -PRAGMA_PUSH() - for(m_uint i = 0; i <= VAL2; i += SZ_INT) - *(m_uint*)(reg+i-SZ_INT) = *(m_uint*)(((*(M_Object*)(reg-SZ_INT))->data + VAL) + i); -PRAGMA_POP() - reg += VAL2 - SZ_INT; - DISPATCH() -dotaddr: - *(m_bit**)(reg-SZ_INT) = ((*(M_Object*)(reg-SZ_INT))->data + VAL); - DISPATCH() - -#define UNION_CHECK\ - register const m_bit *data = (*(M_Object*)(reg-SZ_INT))->data;\ - if(*(m_uint*)data != VAL) {\ - handle(shred, "invalid union acces");\ - continue;\ + } + DISPATCH() + addrefaddr : { + const M_Object o = **(M_Object **)(reg + (m_int)VAL); + if (o) ++o->ref; + } + DISPATCH() + structaddref: + struct_addref(vm->gwion, (Type)VAL2, *(m_bit **)(reg + (m_int)VAL)); + DISPATCH() + structaddrefaddr: + struct_addref(vm->gwion, (Type)VAL2, **(m_bit ***)(reg + (m_int)VAL)); + DISPATCH() + objassign : { + const M_Object o = **(M_Object **)(reg - SZ_INT); + release(o, shred); + } + assign: + reg -= SZ_INT; + **(M_Object **)reg = *(M_Object *)(reg - SZ_INT); + DISPATCH() + remref: + release(*(M_Object *)(mem + VAL), shred); + DISPATCH() + except: + /* TODO: Refactor except instruction * + * so that * + * VAL = offset (no default SZ_INT) * + * VAL2 = error message * + * grep for GWOP_EXCEPT and Except, exception... */ + if (!*(m_bit **)(reg + (m_int)(VAL))) { + shred->pc = PC; + handle(shred, "NullPtrException"); + continue; + } + DISPATCH(); + allocmemberaddr: + *(m_bit **)reg = (*(M_Object *)mem)->data + VAL; + reg += SZ_INT; + DISPATCH() + dotmember: + *(m_uint *)(reg - SZ_INT) = + *(m_uint *)((*(M_Object *)(reg - SZ_INT))->data + VAL); + DISPATCH() + dotfloat: + *(m_float *)(reg - SZ_INT) = + *(m_float *)((*(M_Object *)(reg - SZ_INT))->data + VAL); + reg += SZ_FLOAT - SZ_INT; + DISPATCH() + dotother: + // LOOP_OPTIM + PRAGMA_PUSH() + for (m_uint i = 0; i <= VAL2; i += SZ_INT) + *(m_uint *)(reg + i - SZ_INT) = + *(m_uint *)(((*(M_Object *)(reg - SZ_INT))->data + VAL) + i); + PRAGMA_POP() + reg += VAL2 - SZ_INT; + DISPATCH() + dotaddr: + *(m_bit **)(reg - SZ_INT) = ((*(M_Object *)(reg - SZ_INT))->data + VAL); + DISPATCH() + +#define UNION_CHECK \ + register const m_bit *data = (*(M_Object *)(reg - SZ_INT))->data; \ + if (*(m_uint *)data != VAL) { \ + handle(shred, "invalid union acces"); \ + continue; \ } -unioncheck: -{ - if(*(m_uint*)(*(M_Object*)(reg-SZ_INT))->data != VAL2) { - reg -= SZ_INT; - PC_DISPATCH(VAL); - } - DISPATCH() -} -unionint: -{ - UNION_CHECK - *(m_uint*)(reg-SZ_INT) = *(m_uint*)(data + SZ_INT); - DISPATCH() -} -unionfloat: -{ - UNION_CHECK - *(m_float*)(reg-SZ_INT) = *(m_float*)(data + SZ_INT); - reg += SZ_FLOAT - SZ_INT; - DISPATCH() -} -unionother: -{ - UNION_CHECK -PRAGMA_PUSH() - for(m_uint i = 0; i <= VAL2; i += SZ_INT) - *(m_uint*)(reg+i-SZ_INT) = *(m_uint*)(data + SZ_INT + i); -PRAGMA_POP() - reg += VAL2 - SZ_INT; - DISPATCH() -} -unionaddr: -{ - *(m_uint*)(*(M_Object*)(reg-SZ_INT))->data = VAL; - *(m_bit**)(reg - SZ_INT)= &*(m_bit*)((*(M_Object*)(reg-SZ_INT))->data + SZ_INT); - DISPATCH() -} -staticint: - *(m_uint*)reg = *(m_uint*)VAL; - reg += SZ_INT; - DISPATCH() -staticfloat: - *(m_float*)reg = *(m_float*)VAL; - reg += SZ_FLOAT; - DISPATCH() -staticother: -// LOOP_OPTIM -// for(m_uint i = 0; i <= VAL2; i += SZ_INT) -// *(m_uint*)(reg+i) = *(m_uint*)((m_bit*)VAL + i); - memcpy(reg, (m_bit*)VAL, VAL2); - reg += VAL2; - DISPATCH() -upvalueint: - *(m_uint*)reg = *(m_uint*)(code->closure->data + VAL); - reg += SZ_INT; - DISPATCH() -upvaluefloat: - *(m_float*)reg = *(m_float*)(code->closure->data + VAL); - reg += SZ_FLOAT; - DISPATCH() -upvalueother: - memcpy(reg, code->closure->data + VAL, VAL2); - reg += VAL2; - DISPATCH() -upvalueaddr: - *(m_uint**)reg = (m_uint*)(code->closure->data + VAL); - reg += SZ_INT; - DISPATCH() -dotfunc: - *(VM_Code*)(reg+(m_uint)VAL2) = ((Func)(*(M_Object*)(reg-SZ_INT))->vtable.ptr[OFFSET + VAL])->code; - DISPATCH() -gcini: - vector_add(&shred->gc, 0); - DISPATCH(); -gcadd: - vector_add(&shred->gc, *(vtype*)(reg+(m_int)VAL)); - DISPATCH(); -gcend: -{ - M_Object o; - while((o = (M_Object)vector_pop(&shred->gc))) - _release(o, shred); -} - DISPATCH() -gacktype: -{ - const M_Object o = *(M_Object*)(reg - SZ_INT); - if(o) - *(Type*)reg = o->type_ref; -} - DISPATCH() -gackend: -{ - m_str str = *(m_str*)(reg - SZ_INT); - if(!VAL) - gw_out("%s\n", str); - else - *(M_Object*)(reg - SZ_INT)= new_string(vm->gwion->mp, shred, str); - if(str) - mp_free2(vm->gwion->mp, strlen(str), str); - DISPATCH(); -} -gack: - VM_OUT - gack(shred, VAL); - goto in; -try_ini: - if(!shred->info->frame.ptr) // ??? - vector_init(&shred->info->frame); - vector_add(&shred->info->frame, PC); - vector_add(&shred->info->frame, (m_uint)reg); - DISPATCH(); -try_end: - vector_pop(&shred->info->frame); - vector_pop(&shred->info->frame); -handleeffect: -// this should check the *xid* of the exception - DISPATCH(); -performeffect: - VM_OUT - handle(shred, (m_str)VAL); - break; -noop: - DISPATCH(); -other: - VM_OUT - ((f_instr)VAL2)(shred, (Instr)VAL); -unroll2: -in: - if(!s->curr) - break; - bytecode = (code = shred->code)->bytecode; - reg = shred->reg; - mem = shred->mem; - PC_DISPATCH(shred->pc) -debugline: - if(!shred->info->line.ptr) {// from a problem with spork it seems - vector_init(&shred->info->line); - vector_add(&shred->info->line, VAL); - } else if(!VAL) { - vector_add(&shred->info->line, VAL); - } else { - const m_uint sz = vector_size(&shred->info->line); - vector_set(&shred->info->line, sz - 1, VAL); - } - DISPATCH(); -debugvalue: - DISPATCH(); -debugpush: - if(!shred->info->line.ptr) - vector_init(&shred->info->line); - vector_add(&shred->info->line, 0); - DISPATCH(); -debugpop: - vector_pop(&shred->info->line); - DISPATCH(); -eoc: - VM_OUT - vm_shred_exit(shred); - } while(s->curr); - MUTEX_UNLOCK(s->mutex); + unioncheck : { + if (*(m_uint *)(*(M_Object *)(reg - SZ_INT))->data != VAL2) { + reg -= SZ_INT; + PC_DISPATCH(VAL); + } + DISPATCH() + } + unionint : { + UNION_CHECK + *(m_uint *)(reg - SZ_INT) = *(m_uint *)(data + SZ_INT); + DISPATCH() + } + unionfloat : { + UNION_CHECK + *(m_float *)(reg - SZ_INT) = *(m_float *)(data + SZ_INT); + reg += SZ_FLOAT - SZ_INT; + DISPATCH() + } + unionother : { + UNION_CHECK + PRAGMA_PUSH() + for (m_uint i = 0; i <= VAL2; i += SZ_INT) + *(m_uint *)(reg + i - SZ_INT) = *(m_uint *)(data + SZ_INT + i); + PRAGMA_POP() + reg += VAL2 - SZ_INT; + DISPATCH() + } + unionaddr : { + *(m_uint *)(*(M_Object *)(reg - SZ_INT))->data = VAL; + *(m_bit **)(reg - SZ_INT) = + &*(m_bit *)((*(M_Object *)(reg - SZ_INT))->data + SZ_INT); + DISPATCH() + } + staticint: + *(m_uint *)reg = *(m_uint *)VAL; + reg += SZ_INT; + DISPATCH() + staticfloat: + *(m_float *)reg = *(m_float *)VAL; + reg += SZ_FLOAT; + DISPATCH() + staticother: + // LOOP_OPTIM + // for(m_uint i = 0; i <= VAL2; i += SZ_INT) + // *(m_uint*)(reg+i) = *(m_uint*)((m_bit*)VAL + i); + memcpy(reg, (m_bit *)VAL, VAL2); + reg += VAL2; + DISPATCH() + upvalueint: + *(m_uint *)reg = *(m_uint *)(code->closure->data + VAL); + reg += SZ_INT; + DISPATCH() + upvaluefloat: + *(m_float *)reg = *(m_float *)(code->closure->data + VAL); + reg += SZ_FLOAT; + DISPATCH() + upvalueother: + memcpy(reg, code->closure->data + VAL, VAL2); + reg += VAL2; + DISPATCH() + upvalueaddr: + *(m_uint **)reg = (m_uint *)(code->closure->data + VAL); + reg += SZ_INT; + DISPATCH() + dotfunc: + *(VM_Code *)(reg + (m_uint)VAL2) = + ((Func)(*(M_Object *)(reg - SZ_INT))->vtable.ptr[OFFSET + VAL])->code; + DISPATCH() + gcini: + vector_add(&shred->gc, 0); + DISPATCH(); + gcadd: + vector_add(&shred->gc, *(vtype *)(reg + (m_int)VAL)); + DISPATCH(); + gcend : { + M_Object o; + while ((o = (M_Object)vector_pop(&shred->gc))) _release(o, shred); + } + DISPATCH() + gacktype : { + const M_Object o = *(M_Object *)(reg - SZ_INT); + if (o) *(Type *)reg = o->type_ref; + } + DISPATCH() + gackend : { + m_str str = *(m_str *)(reg - SZ_INT); + if (!VAL) + gw_out("%s\n", str); + else + *(M_Object *)(reg - SZ_INT) = new_string(vm->gwion->mp, shred, str); + if (str) mp_free2(vm->gwion->mp, strlen(str), str); + DISPATCH(); + } + gack: + VM_OUT + gack(shred, VAL); + goto in; + try_ini: + if (!shred->info->frame.ptr) // ??? + vector_init(&shred->info->frame); + vector_add(&shred->info->frame, PC); + vector_add(&shred->info->frame, (m_uint)reg); + DISPATCH(); + try_end: + vector_pop(&shred->info->frame); + vector_pop(&shred->info->frame); + handleeffect: + // this should check the *xid* of the exception + DISPATCH(); + performeffect: + VM_OUT + handle(shred, (m_str)VAL); + break; + noop: + DISPATCH(); + other: + VM_OUT((f_instr)VAL2)(shred, (Instr)VAL); + unroll2: + in: + if (!s->curr) break; + bytecode = (code = shred->code)->bytecode; + reg = shred->reg; + mem = shred->mem; + PC_DISPATCH(shred->pc) + debugline: + if (!shred->info->line.ptr) { // from a problem with spork it seems + vector_init(&shred->info->line); + vector_add(&shred->info->line, VAL); + } else if (!VAL) { + vector_add(&shred->info->line, VAL); + } else { + const m_uint sz = vector_size(&shred->info->line); + vector_set(&shred->info->line, sz - 1, VAL); + } + DISPATCH(); + debugvalue: + DISPATCH(); + debugpush: + if (!shred->info->line.ptr) vector_init(&shred->info->line); + vector_add(&shred->info->line, 0); + DISPATCH(); + debugpop: + vector_pop(&shred->info->line); + DISPATCH(); + eoc: + VM_OUT + vm_shred_exit(shred); + } while (s->curr); + MUTEX_UNLOCK(s->mutex); } } @@ -1113,12 +1192,12 @@ static void vm_run_audio(const VM *vm) { vm_ugen_init(vm); } -VM* new_vm(MemPool p, const bool audio) { - VM* vm = (VM*)mp_calloc(p, VM); +VM *new_vm(MemPool p, const bool audio) { + VM *vm = (VM *)mp_calloc(p, VM); vector_init(&vm->ugen); - vm->bbq = new_driver(p); - vm->bbq->run = audio ? vm_run_audio : vm_run; - vm->shreduler = (Shreduler)mp_calloc(p, Shreduler); + vm->bbq = new_driver(p); + vm->bbq->run = audio ? vm_run_audio : vm_run; + vm->shreduler = (Shreduler)mp_calloc(p, Shreduler); vector_init(&vm->shreduler->shreds); MUTEX_SETUP(vm->shreduler->mutex); vm->shreduler->bbq = vm->bbq; diff --git a/src/vm/vm_code.c b/src/vm/vm_code.c index 0438c52a..956f4708 100644 --- a/src/vm/vm_code.c +++ b/src/vm/vm_code.c @@ -11,68 +11,68 @@ #include "import.h" ANN void free_code_instr(const Vector v, const Gwion gwion) { - for(m_uint i = vector_size(v) + 1; --i;) { - const Instr instr = (Instr)vector_at(v, i - 1); - const f_freearg f = (f_freearg)(map_get(&gwion->data->freearg, instr->opcode) ?: - map_get(&gwion->data->freearg, (vtype)instr->execute)); - if(f) - f(instr, gwion); + for (m_uint i = vector_size(v) + 1; --i;) { + const Instr instr = (Instr)vector_at(v, i - 1); + const f_freearg f = (f_freearg)( + map_get(&gwion->data->freearg, instr->opcode) + ?: map_get(&gwion->data->freearg, (vtype)instr->execute)); + if (f) f(instr, gwion); mp_free(gwion->mp, Instr, instr); } } ANN void free_vmcode(VM_Code a, Gwion gwion) { - if(!a->builtin) { + if (!a->builtin) { _mp_free(gwion->mp, vector_size(&a->instr) * BYTECODE_SZ, a->bytecode); - if(likely(!a->callback)) { - if(a->closure) { - if(!a->is_memoize) + if (likely(!a->callback)) { + if (a->closure) { + if (!a->is_memoize) free_closure(a->closure, gwion); else memoize_end(gwion->mp, a->memoize); } free_code_instr(&a->instr, gwion); } - if(a->handlers.ptr) - map_release(&a->handlers); + if (a->handlers.ptr) map_release(&a->handlers); vector_release(&a->instr); } free_mstr(gwion->mp, a->name); - mp_free(gwion->mp , VM_Code, a); + mp_free(gwion->mp, VM_Code, a); } static inline uint isgoto(const unsigned opcode) { - return opcode == eGoto || opcode == eArrayTop || - opcode == eBranchEqInt || opcode == eBranchNeqInt || - opcode == eBranchEqFloat || opcode == eBranchNeqFloat || - opcode == eHandleEffect || opcode == eRepeat || - opcode == eRepeatIdx || opcode == eAutoLoop; + return opcode == eGoto || opcode == eArrayTop || opcode == eBranchEqInt || + opcode == eBranchNeqInt || opcode == eBranchEqFloat || + opcode == eBranchNeqFloat || opcode == eHandleEffect || + opcode == eRepeat || opcode == eRepeatIdx || opcode == eAutoLoop; } ANN static inline void setpc(const m_bit *data, const m_uint i) { - *(unsigned*)(data+1) = i + 1; + *(unsigned *)(data + 1) = i + 1; } -ANN static m_bit* tobytecode(MemPool p, const VM_Code code) { - const Vector v = &code->instr; - const m_uint sz = vector_size(v); - m_bit *const ptr = _mp_malloc(p, sz * BYTECODE_SZ); +ANN static m_bit *tobytecode(MemPool p, const VM_Code code) { + const Vector v = &code->instr; + const m_uint sz = vector_size(v); + m_bit *const ptr = _mp_malloc(p, sz * BYTECODE_SZ); struct Vector_ nop; vector_init(&nop); - for(m_uint i= 0; i < sz; ++i) { - m_bit *const data = ptr + i*BYTECODE_SZ; - const Instr instr = (Instr)vector_at(v, i); - if(instr->opcode < eOP_MAX) { - if(instr->opcode == eRegMove) { - m_int move = (m_int)instr->m_val; - m_uint j = 0; - Instr next; - while((next = (Instr)vector_at(v, i + j +1)) && next->opcode == eRegMove) { - ++j; vector_add(&nop, i+j); + for (m_uint i = 0; i < sz; ++i) { + m_bit *const data = ptr + i * BYTECODE_SZ; + const Instr instr = (Instr)vector_at(v, i); + if (instr->opcode < eOP_MAX) { + if (instr->opcode == eRegMove) { + m_int move = (m_int)instr->m_val; + m_uint j = 0; + Instr next; + while ((next = (Instr)vector_at(v, i + j + 1)) && + next->opcode == eRegMove) { + ++j; + vector_add(&nop, i + j); move += (m_int)next->m_val; next->opcode = eNoOp; } - if((instr->m_val = move)) { + if ((instr->m_val = move)) { memcpy(data, instr, BYTECODE_SZ); setpc(data, i); } else { @@ -81,91 +81,88 @@ ANN static m_bit* tobytecode(MemPool p, const VM_Code code) { } i += j; continue; - } else if(instr->opcode == eUnroll2) { - const Instr unroll = (Instr)instr->m_val; - const m_uint pc = vector_find(v, (m_uint)unroll); - m_uint reduce_pre = 0, reduce = 0; - for(m_uint j = 0; j < vector_size(&nop); ++j) { + } else if (instr->opcode == eUnroll2) { + const Instr unroll = (Instr)instr->m_val; + const m_uint pc = vector_find(v, (m_uint)unroll); + m_uint reduce_pre = 0, reduce = 0; + for (m_uint j = 0; j < vector_size(&nop); ++j) { const m_uint at = vector_at(&nop, j); - if(at < pc) - ++reduce_pre; - if(at >= pc) { - if(at > (pc + unroll->m_val2)) - break; + if (at < pc) ++reduce_pre; + if (at >= pc) { + if (at > (pc + unroll->m_val2)) break; ++reduce; } } - m_bit *const unroll_data = ptr + (pc-reduce_pre)*BYTECODE_SZ; + m_bit *const unroll_data = ptr + (pc - reduce_pre) * BYTECODE_SZ; unroll->m_val2 -= reduce; - *(m_uint*)(unroll_data + SZ_INT *2) -= reduce; + *(m_uint *)(unroll_data + SZ_INT * 2) -= reduce; instr->opcode = eNoOp; vector_add(&nop, i); continue; } - if(instr->opcode == eGoto && instr->m_val == i+1) { + if (instr->opcode == eGoto && instr->m_val == i + 1) { instr->opcode = eNoOp; vector_add(&nop, i); - } else if(instr->opcode != eNoOp) + } else if (instr->opcode != eNoOp) memcpy(data, instr, BYTECODE_SZ); else vector_add(&nop, i); } else { - *(m_bit*)(data) = instr->opcode; - *(Instr*)(data + SZ_INT) = instr; - *(f_instr*)(data + SZ_INT*2) = instr->execute; + *(m_bit *)(data) = instr->opcode; + *(Instr *)(data + SZ_INT) = instr; + *(f_instr *)(data + SZ_INT * 2) = instr->execute; } setpc(data, i); } - if(!vector_size(&nop)) { + if (!vector_size(&nop)) { vector_release(&nop); return ptr; } - m_bit *const final = _mp_malloc(p, sz * BYTECODE_SZ); // could use smaller size - for(m_uint i= 0, j = 0; i < sz; ++i) { - const Instr instr = (Instr)vector_at(v, i); + m_bit *const final = + _mp_malloc(p, sz * BYTECODE_SZ); // could use smaller size + for (m_uint i = 0, j = 0; i < sz; ++i) { + const Instr instr = (Instr)vector_at(v, i); const unsigned opcode = instr->opcode; - if(opcode != eNoOp) { - m_bit *const base = ptr + i*BYTECODE_SZ, - *const data = final + j*BYTECODE_SZ; + if (opcode != eNoOp) { + m_bit *const base = ptr + i * BYTECODE_SZ, *const data = + final + j * BYTECODE_SZ; memcpy(data, base, BYTECODE_SZ); - if(isgoto(opcode)) { + if (isgoto(opcode)) { m_uint pc = 0; - for(pc = 0; pc < vector_size(&nop); ++pc) { - if(instr->m_val <= vector_at(&nop, pc)) - break; + for (pc = 0; pc < vector_size(&nop); ++pc) { + if (instr->m_val <= vector_at(&nop, pc)) break; } const m_uint new_pc = instr->m_val > pc ? instr->m_val - pc : 0; - if(instr->opcode == eHandleEffect) { - if(!code->handlers.ptr) - map_init(&code->handlers); - map_set(&code->handlers, j, new_pc); + if (instr->opcode == eHandleEffect) { + if (!code->handlers.ptr) map_init(&code->handlers); + map_set(&code->handlers, j, new_pc); } - *(m_uint*)(data + SZ_INT) = new_pc; - } else if(opcode == eRecurs /*|| opcode == eSetCode*/) { - *(uint16_t*)(final + j * BYTECODE_SZ + SZ_INT*2) = - instr->udata.one += j + 1; + *(m_uint *)(data + SZ_INT) = new_pc; + } else if (opcode == eRecurs /*|| opcode == eSetCode*/) { + *(uint16_t *)(final + j * BYTECODE_SZ + SZ_INT * 2) = + instr->udata.one += j + 1; } setpc(data, j); ++j; } } vector_release(&nop); - mp_free2(p, sz*BYTECODE_SZ, ptr); + mp_free2(p, sz * BYTECODE_SZ, ptr); return final; } VM_Code new_vmcode(MemPool p, const Vector instr, const m_uint stack_depth, - const int builtin, const m_str name) { - VM_Code code = mp_calloc(p, VM_Code); - code->name = mstrdup(p, name); - if(instr) { + const int builtin, const m_str name) { + VM_Code code = mp_calloc(p, VM_Code); + code->name = mstrdup(p, name); + if (instr) { vector_init(&code->instr); vector_copy2(instr, &code->instr); code->bytecode = tobytecode(p, code); } - code->stack_depth = stack_depth; - code->builtin = builtin; - code->ref = 1; + code->stack_depth = stack_depth; + code->builtin = builtin; + code->ref = 1; return code; } @@ -175,11 +172,11 @@ VM_Code vmcode_callback(MemPool mp, VM_Code base) { char name[strlen(base->name) + 11]; sprintf(name, "%s(callback)", base->name); const Instr instr = (Instr)vector_back(&base->instr); - instr->opcode = eEOC; - const VM_Code code = new_vmcode(mp, &base->instr, - base->stack_depth, base->builtin, name); - code->closure = base->closure; + instr->opcode = eEOC; + const VM_Code code = + new_vmcode(mp, &base->instr, base->stack_depth, base->builtin, name); + code->closure = base->closure; code->callback = 1; - instr->opcode = eFuncReturn; + instr->opcode = eFuncReturn; return code; } diff --git a/src/vm/vm_name.c b/src/vm/vm_name.c index d80d22d9..49b9c931 100644 --- a/src/vm/vm_name.c +++ b/src/vm/vm_name.c @@ -3,12 +3,12 @@ m_str code_name_set(MemPool p, const m_str name, const m_str file) { const size_t len = strlen(name) + strlen(file) + 2; - const m_str str = (const m_str)_mp_malloc(p, len); + const m_str str = (const m_str)_mp_malloc(p, len); sprintf(str, "%s$%s", name, file); return str; } m_str code_name(const m_str name, const bool b) { const m_str str = strchr(name, '$'); - return (b && str) ? (str + 1) : name; + return (b && str) ? (str + 1) : name; } diff --git a/src/vm/vm_shred.c b/src/vm/vm_shred.c index c98a0d56..3e524339 100644 --- a/src/vm/vm_shred.c +++ b/src/vm/vm_shred.c @@ -7,48 +7,45 @@ struct Stack_ { VM_Shred shred; - char c[SIZEOF_REG]; - char d[SIZEOF_MEM]; + char c[SIZEOF_REG]; + char d[SIZEOF_MEM]; }; static inline struct ShredInfo_ *new_shredinfo(MemPool p, const VM_Code c) { struct ShredInfo_ *const info = mp_calloc(p, ShredInfo); - info->mp = p; - info->orig = c; + info->mp = p; + info->orig = c; return info; } static inline void free_shredinfo(MemPool mp, struct ShredInfo_ *info) { - if(info->args.ptr) { + if (info->args.ptr) { const Vector v = &info->args; LOOP_OPTIM - for(m_uint i = vector_size(v) + 1; --i;) - xfree((void*)vector_at(v, i - 1)); + for (m_uint i = vector_size(v) + 1; --i;) + xfree((void *)vector_at(v, i - 1)); vector_release(v); } - if(info->line.ptr) - vector_release(&info->line); + if (info->line.ptr) vector_release(&info->line); mp_free(mp, ShredInfo, info); } VM_Shred new_vm_shred(MemPool p, VM_Code c) { const VM_Shred shred = mp_calloc(p, Stack); - shred->code = c; - shred->reg = (m_bit*)shred + sizeof(struct VM_Shred_); + shred->code = c; + shred->reg = (m_bit *)shred + sizeof(struct VM_Shred_); shred->base = shred->mem = shred->reg + SIZEOF_REG; - shred->info = new_shredinfo(p, c); + shred->info = new_shredinfo(p, c); vector_init(&shred->gc); return shred; } void free_vm_shred(VM_Shred shred) { - for(m_uint i = vector_size(&shred->gc) + 1; --i;) + for (m_uint i = vector_size(&shred->gc) + 1; --i;) release((M_Object)vector_at(&shred->gc, i - 1), shred); vector_release(&shred->gc); - if(shred->info->frame.ptr) - vector_release(&shred->info->frame); - if(shred->tick->child.ptr) - vector_release(&shred->tick->child); + if (shred->info->frame.ptr) vector_release(&shred->info->frame); + if (shred->tick->child.ptr) vector_release(&shred->tick->child); vmcode_remref(shred->info->orig, shred->info->vm->gwion); const MemPool mp = shred->info->mp; mp_free(mp, ShredTick, shred->tick); diff --git a/tests/driver/simple_driver.c b/tests/driver/simple_driver.c index 22c2bccf..97bb056d 100644 --- a/tests/driver/simple_driver.c +++ b/tests/driver/simple_driver.c @@ -9,15 +9,13 @@ #include "plug.h" static DRVRUN(simple_driver_run) { - while(di->is_running) { + while (di->is_running) { di->run(vm); ++di->pos; } } -static DRVINI(simple_driver_ini) { - return GW_OK; -} +static DRVINI(simple_driver_ini) { return GW_OK; } static DRVDEL(simple_driver_del) {} diff --git a/tests/module/basic_module.c b/tests/module/basic_module.c index 18d05fce..c558ddf7 100644 --- a/tests/module/basic_module.c +++ b/tests/module/basic_module.c @@ -6,14 +6,12 @@ GWMODINI(basic_module) { puts("ini module"); - if(args) { + if (args) { puts("have arguments"); - for(vtype i = 0; i < vector_size(args); ++i) + for (vtype i = 0; i < vector_size(args); ++i) puts((m_str)vector_at(args, i)); } return NULL; } -GWMODEND(basic_module) { - puts("end module"); -} +GWMODEND(basic_module) { puts("end module"); } diff --git a/tests/module/get_module.c b/tests/module/get_module.c index f6b41695..789b6ec8 100644 --- a/tests/module/get_module.c +++ b/tests/module/get_module.c @@ -18,12 +18,10 @@ GWMODINI(get_module) { return NULL; } -GWMODEND(get_module) { - puts(__func__); -} +GWMODEND(get_module) { puts(__func__); } GWION_IMPORT(dummy_module) { - set_module(gwi->gwion, "get_module", (void*)1); + set_module(gwi->gwion, "get_module", (void *)1); GWI_OB(get_module(gwi->gwion, "get_module")) puts("test passed"); get_module(gwi->gwion, "non_existant_module"); diff --git a/tests/module/module.c b/tests/module/module.c index 77ead7d7..dffddb40 100644 --- a/tests/module/module.c +++ b/tests/module/module.c @@ -12,6 +12,4 @@ GWMODINI(dummy_module) { puts(__func__); return NULL; } -GWMODEND(dummy_module) { - puts(__func__); -} +GWMODEND(dummy_module) { puts(__func__); } diff --git a/tests/plug/array.c b/tests/plug/array.c index 7fe9ed0d..ad287f2e 100644 --- a/tests/plug/array.c +++ b/tests/plug/array.c @@ -9,7 +9,7 @@ #include "import.h" #include "gwi.h" -MFUN(test_mfun){} +MFUN(test_mfun) {} GWION_IMPORT(array_test) { GWI_OB(gwi_class_ini(gwi, "ArrayTest", NULL)) diff --git a/tests/plug/callback.c b/tests/plug/callback.c index e7f662df..db4bbdd7 100644 --- a/tests/plug/callback.c +++ b/tests/plug/callback.c @@ -11,59 +11,57 @@ #include "gwi.h" struct ret_info { - Instr instr; + Instr instr; VM_Code code; - m_uint offset; - m_uint size; - size_t pc; + m_uint offset; + m_uint size; + size_t pc; }; static INSTR(my_ret) { - struct ret_info* info = (struct ret_info*)instr->m_val; + struct ret_info *info = (struct ret_info *)instr->m_val; POP_MEM(shred, info->offset); vector_set(&shred->code->instr, shred->pc, (vtype)info->instr); shred->code = info->code; -//*(VM_Code*)instr->ptr; + //*(VM_Code*)instr->ptr; POP_REG(shred, info->size) - if(shred->mem == (m_bit*)shred + sizeof(struct VM_Shred_) + SIZEOF_REG) + if (shred->mem == (m_bit *)shred + sizeof(struct VM_Shred_) + SIZEOF_REG) POP_REG(shred, SZ_INT) POP_REG(shred, shred->code->stack_depth); -// shred->pc = instr->m_val2; + // shred->pc = instr->m_val2; shred->pc = info->pc; free(info); - *(m_int*)shred->reg = 2; + *(m_int *)shred->reg = 2; PUSH_REG(shred, SZ_INT); } static SFUN(cb_func) { m_uint i; - Func f = *(Func*)MEM(0); - if(!f){ - handle(shred, "NullCallbackException"); - } - m_uint offset = shred->mem - ((m_bit*)shred + sizeof(struct VM_Shred_) + SIZEOF_REG); + Func f = *(Func *)MEM(0); + if (!f) { handle(shred, "NullCallbackException"); } + m_uint offset = + shred->mem - ((m_bit *)shred + sizeof(struct VM_Shred_) + SIZEOF_REG); PUSH_MEM(shred, offset); - Instr instr = mp_calloc(shred->info->mp, Instr); - struct ret_info* info = (struct ret_info*)xmalloc(sizeof(struct ret_info)); - info->offset = offset; - info->code = shred->code; - info->size = f->def->base->ret_type->size; - info->pc = shred->pc; - instr->execute = my_ret; -// *(VM_Code*)instr->ptr = shred->code; + Instr instr = mp_calloc(shred->info->mp, Instr); + struct ret_info *info = (struct ret_info *)xmalloc(sizeof(struct ret_info)); + info->offset = offset; + info->code = shred->code; + info->size = f->def->base->ret_type->size; + info->pc = shred->pc; + instr->execute = my_ret; + // *(VM_Code*)instr->ptr = shred->code; instr->m_val = (m_uint)info; -// instr->m_val2 = shred->pc; - for(i = 0; i < vector_size(&f->code->instr); i++) { + // instr->m_val2 = shred->pc; + for (i = 0; i < vector_size(&f->code->instr); i++) { Instr in = (Instr)vector_at(&f->code->instr, i); - if(in->execute == FuncReturn || - in->execute == my_ret) { + if (in->execute == FuncReturn || in->execute == my_ret) { info->instr = in; vector_set(&f->code->instr, i, (vtype)instr); } } - *(m_int*)RETURN = 1; - shred->pc = 0; - shred->code = f->code; + *(m_int *)RETURN = 1; + shred->pc = 0; + shred->code = f->code; } GWION_IMPORT(callback) { @@ -71,9 +69,9 @@ GWION_IMPORT(callback) { GWI_OB(gwi_fptr_end(gwi, 0)) GWI_OB(gwi_class_ini(gwi, "Callback", NULL)) - GWI_BB(gwi_func_ini(gwi, "int", "callback")) - GWI_BB(gwi_func_arg(gwi, "PtrType", "func")) - GWI_BB(gwi_func_end(gwi, cb_func, ae_flag_static)) + GWI_BB(gwi_func_ini(gwi, "int", "callback")) + GWI_BB(gwi_func_arg(gwi, "PtrType", "func")) + GWI_BB(gwi_func_end(gwi, cb_func, ae_flag_static)) GWI_BB(gwi_class_end(gwi)) return GW_OK; } diff --git a/tests/plug/class_template.c b/tests/plug/class_template.c index f0699abd..2a05bf3c 100644 --- a/tests/plug/class_template.c +++ b/tests/plug/class_template.c @@ -11,28 +11,30 @@ static m_int o_map_key; static m_int o_map_value; -#define MAP_KEY(a) *((M_Object*)(a->data + o_map_key)) -#define MAP_VAL(a) *((M_Object*)(a->data + o_map_value)) +#define MAP_KEY(a) *((M_Object *)(a->data + o_map_key)) +#define MAP_VAL(a) *((M_Object *)(a->data + o_map_value)) static CTOR(class_template_ctor) { /*char* name = strdup(o->type_ref->name);*/ /*char* tmp = strsep(&name, "@");*/ /*char* name1 = strsep(&name, "@");*/ -/*Type t1 = nspc_lookup_type1(o->type_ref->info->parent, insert_symbol(name1));*/ - /*Type t2 = nspc_lookup_type0(shred->vm->emit->env->curr, insert_symbol(name));*/ -/*free(tmp);*/ -/**(M_Object*)(o->data) = new_array(t1->size, 0, t1->array_depth);*/ + /*Type t1 = nspc_lookup_type1(o->type_ref->info->parent, + * insert_symbol(name1));*/ + /*Type t2 = nspc_lookup_type0(shred->vm->emit->env->curr, + * insert_symbol(name));*/ + /*free(tmp);*/ + /**(M_Object*)(o->data) = new_array(t1->size, 0, t1->array_depth);*/ /**(M_Object*)(o->data + SZ_INT) = new_array(t2->size, 0, t2->array_depth);*/ } GWION_IMPORT(class_template) { GWI_OB(gwi_class_ini(gwi, ":[A,B]ClassTemplate", NULL)) gwi_class_xtor(gwi, class_template_ctor, NULL); - GWI_BB(gwi_item_ini(gwi, "A[]", "key")) - GWI_BB((o_map_key = gwi_item_end(gwi, ae_flag_none, obj, NULL))) - GWI_BB(gwi_item_ini(gwi, "B[]", "value")) - GWI_BB((o_map_value = gwi_item_end(gwi, ae_flag_none, obj, NULL))) - GWI_BB(gwi_func_ini(gwi, "int", ":[C,D]test")) - GWI_BB(gwi_func_end(gwi, (f_xfun)1, ae_flag_none)) + GWI_BB(gwi_item_ini(gwi, "A[]", "key")) + GWI_BB((o_map_key = gwi_item_end(gwi, ae_flag_none, obj, NULL))) + GWI_BB(gwi_item_ini(gwi, "B[]", "value")) + GWI_BB((o_map_value = gwi_item_end(gwi, ae_flag_none, obj, NULL))) + GWI_BB(gwi_func_ini(gwi, "int", ":[C,D]test")) + GWI_BB(gwi_func_end(gwi, (f_xfun)1, ae_flag_none)) GWI_BB(gwi_class_end(gwi)) GWI_BB(gwi_item_ini(gwi, ":[:[int]Ptr,int[]]ClassTemplate", "testObject")) diff --git a/tests/plug/class_template_invalid.c b/tests/plug/class_template_invalid.c index 350ad7c6..47a4209b 100644 --- a/tests/plug/class_template_invalid.c +++ b/tests/plug/class_template_invalid.c @@ -11,17 +11,17 @@ static m_int o_map_key; static m_int o_map_value; -#define MAP_KEY(a) *((M_Object*)(a->data + o_map_key)) -#define MAP_VAL(a) *((M_Object*)(a->data + o_map_value)) +#define MAP_KEY(a) *((M_Object *)(a->data + o_map_key)) +#define MAP_VAL(a) *((M_Object *)(a->data + o_map_value)) GWION_IMPORT(class_template) { GWI_OB(gwi_class_ini(gwi, "ClassTemplate:[A,B]", NULL)) - GWI_BB(gwi_item_ini(gwi, "A[]", "key")) - GWI_BB((o_map_key = gwi_item_end(gwi, ae_flag_none, obj, NULL))) - GWI_BB(gwi_item_ini(gwi, "B[]", "value")) - GWI_BB((o_map_value = gwi_item_end(gwi, ae_flag_none, obj, NULL))) - GWI_BB(gwi_func_ini(gwi, "int", "test:[C,D]")) - GWI_BB(gwi_func_end(gwi, (f_xfun)1, ae_flag_none)) + GWI_BB(gwi_item_ini(gwi, "A[]", "key")) + GWI_BB((o_map_key = gwi_item_end(gwi, ae_flag_none, obj, NULL))) + GWI_BB(gwi_item_ini(gwi, "B[]", "value")) + GWI_BB((o_map_value = gwi_item_end(gwi, ae_flag_none, obj, NULL))) + GWI_BB(gwi_func_ini(gwi, "int", "test:[C,D]")) + GWI_BB(gwi_func_end(gwi, (f_xfun)1, ae_flag_none)) GWI_BB(gwi_class_end(gwi)) GWI_BB(gwi_item_ini(gwi, "ClassTemplate:[:[int]Ptr,int[]]", "testObject")) diff --git a/tests/plug/compile_file.c b/tests/plug/compile_file.c index cc821ee7..9745d808 100644 --- a/tests/plug/compile_file.c +++ b/tests/plug/compile_file.c @@ -13,11 +13,11 @@ #define _XOPEN_SOURCE 500 #include GWION_IMPORT(compile_string) { - DECL_OB(FILE *,file, = fopen("rm_me.gw", "w+")); + DECL_OB(FILE *, file, = fopen("rm_me.gw", "w+")); fprintf(file, "1;"); rewind(file); - const m_bool ret = compile_file(gwi->gwion, __FILE__, file) ? - GW_OK : GW_ERROR; + const m_bool ret = + compile_file(gwi->gwion, __FILE__, file) ? GW_OK : GW_ERROR; fclose(file); return ret; } diff --git a/tests/plug/compile_string.c b/tests/plug/compile_string.c index 46ed5c89..df02fede 100644 --- a/tests/plug/compile_string.c +++ b/tests/plug/compile_string.c @@ -13,6 +13,5 @@ #define _XOPEN_SOURCE 500 #include GWION_IMPORT(compile_string) { - return compile_string(gwi->gwion, __FILE__, "1;") ? - GW_OK : GW_ERROR; + return compile_string(gwi->gwion, __FILE__, "1;") ? GW_OK : GW_ERROR; } diff --git a/tests/plug/coverage.c b/tests/plug/coverage.c index 2b4635e4..4737dfd9 100644 --- a/tests/plug/coverage.c +++ b/tests/plug/coverage.c @@ -9,8 +9,8 @@ #include "import.h" #include "gwi.h" -SFUN(coverage_int) { *(m_uint*)RETURN = 0; } -SFUN(coverage_float) { *(m_float*)RETURN = 0; } +SFUN(coverage_int) { *(m_uint *)RETURN = 0; } +SFUN(coverage_float) { *(m_float *)RETURN = 0; } GWION_IMPORT(coverage) { GWI_OB(gwi_class_ini(gwi, "Coverage", NULL)) @@ -19,19 +19,19 @@ GWION_IMPORT(coverage) { GWI_BB(gwi_func_ini(gwi, "float", "f")) GWI_BB(gwi_func_end(gwi, coverage_float, ae_flag_static)) - GWI_BB(gwi_item_ini(gwi,"int", "s_i")) + GWI_BB(gwi_item_ini(gwi, "int", "s_i")) GWI_BB(gwi_item_end(gwi, ae_flag_static, num, 5)) - GWI_BB(gwi_item_ini(gwi,"int", "s_f")) + GWI_BB(gwi_item_ini(gwi, "int", "s_f")) GWI_BB(gwi_item_end(gwi, ae_flag_static, fnum, 2.1)) - GWI_BB(gwi_item_ini(gwi,"int", "sc_i")) + GWI_BB(gwi_item_ini(gwi, "int", "sc_i")) GWI_BB(gwi_item_end(gwi, ae_flag_static | ae_flag_const, num, 5)) - GWI_BB(gwi_item_ini(gwi,"float", "sc_f")) + GWI_BB(gwi_item_ini(gwi, "float", "sc_f")) GWI_BB(gwi_item_end(gwi, ae_flag_static | ae_flag_const, fnum, 2.1)) - GWI_BB(gwi_item_ini(gwi,"int[][]", "test_array")) + GWI_BB(gwi_item_ini(gwi, "int[][]", "test_array")) GWI_BB(gwi_item_end(gwi, ae_flag_none, obj, NULL)) GWI_BB(gwi_class_end(gwi)) diff --git a/tests/plug/deps.c b/tests/plug/deps.c index 5448becd..b66381f2 100644 --- a/tests/plug/deps.c +++ b/tests/plug/deps.c @@ -14,5 +14,5 @@ GWION_IMPORT(deps) { return GW_OK; } -static const m_str deps[] = { "array", NULL }; +static const m_str deps[] = {"array", NULL}; GWDEPEND { return deps; } diff --git a/tests/plug/end_class.c b/tests/plug/end_class.c index 84324621..08b1d655 100644 --- a/tests/plug/end_class.c +++ b/tests/plug/end_class.c @@ -8,7 +8,7 @@ #include "instr.h" #include "import.h" -MFUN(test_mfun){} +MFUN(test_mfun) {} GWION_IMPORT(end_class) { GWI_BB(gwi_class_end(gwi)) return GW_OK; diff --git a/tests/plug/enum.c b/tests/plug/enum.c index c7961d46..749cb51d 100644 --- a/tests/plug/enum.c +++ b/tests/plug/enum.c @@ -10,57 +10,57 @@ GWION_IMPORT(enum_test) { GWI_BB(gwi_enum_ini(gwi, NULL)) - GWI_BB(gwi_enum_add(gwi, "ENUM0", 0)) - GWI_BB(gwi_enum_add(gwi, "ENUM1", 1)) - GWI_BB(gwi_enum_add(gwi, "ENUM2", 2)) - GWI_BB(gwi_enum_add(gwi, "ENUM3", 3)) - GWI_BB(gwi_enum_add(gwi, "ENUM4", 4)) - GWI_BB(gwi_enum_add(gwi, "ENUM5", 5)) - GWI_BB(gwi_enum_add(gwi, "ENUM6", 6)) - GWI_BB(gwi_enum_add(gwi, "ENUM7", 7)) - GWI_BB(gwi_enum_add(gwi, "ENUM8", 8)) - GWI_BB(gwi_enum_add(gwi, "ENUM9", 9)) + GWI_BB(gwi_enum_add(gwi, "ENUM0", 0)) + GWI_BB(gwi_enum_add(gwi, "ENUM1", 1)) + GWI_BB(gwi_enum_add(gwi, "ENUM2", 2)) + GWI_BB(gwi_enum_add(gwi, "ENUM3", 3)) + GWI_BB(gwi_enum_add(gwi, "ENUM4", 4)) + GWI_BB(gwi_enum_add(gwi, "ENUM5", 5)) + GWI_BB(gwi_enum_add(gwi, "ENUM6", 6)) + GWI_BB(gwi_enum_add(gwi, "ENUM7", 7)) + GWI_BB(gwi_enum_add(gwi, "ENUM8", 8)) + GWI_BB(gwi_enum_add(gwi, "ENUM9", 9)) GWI_OB(gwi_enum_end(gwi)) GWI_BB(gwi_enum_ini(gwi, "test")) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM0", 0)) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM1", 1)) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM2", 2)) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM3", 3)) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM4", 4)) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM5", 5)) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM6", 6)) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM7", 7)) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM8", 8)) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM9", 9)) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM0", 0)) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM1", 1)) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM2", 2)) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM3", 3)) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM4", 4)) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM5", 5)) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM6", 6)) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM7", 7)) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM8", 8)) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM9", 9)) GWI_OB(gwi_enum_end(gwi)) GWI_OB(gwi_class_ini(gwi, "Enum", NULL)) - GWI_BB(gwi_enum_ini(gwi, 0)) - GWI_BB(gwi_enum_add(gwi, "ENUM0", 0)) - GWI_BB(gwi_enum_add(gwi, "ENUM1", 1)) - GWI_BB(gwi_enum_add(gwi, "ENUM2", 2)) - GWI_BB(gwi_enum_add(gwi, "ENUM3", 3)) - GWI_BB(gwi_enum_add(gwi, "ENUM4", 4)) - GWI_BB(gwi_enum_add(gwi, "ENUM5", 5)) - GWI_BB(gwi_enum_add(gwi, "ENUM6", 6)) - GWI_BB(gwi_enum_add(gwi, "ENUM7", 7)) - GWI_BB(gwi_enum_add(gwi, "ENUM8", 8)) - GWI_BB(gwi_enum_add(gwi, "ENUM9", 9)) - GWI_OB(gwi_enum_end(gwi)) + GWI_BB(gwi_enum_ini(gwi, 0)) + GWI_BB(gwi_enum_add(gwi, "ENUM0", 0)) + GWI_BB(gwi_enum_add(gwi, "ENUM1", 1)) + GWI_BB(gwi_enum_add(gwi, "ENUM2", 2)) + GWI_BB(gwi_enum_add(gwi, "ENUM3", 3)) + GWI_BB(gwi_enum_add(gwi, "ENUM4", 4)) + GWI_BB(gwi_enum_add(gwi, "ENUM5", 5)) + GWI_BB(gwi_enum_add(gwi, "ENUM6", 6)) + GWI_BB(gwi_enum_add(gwi, "ENUM7", 7)) + GWI_BB(gwi_enum_add(gwi, "ENUM8", 8)) + GWI_BB(gwi_enum_add(gwi, "ENUM9", 9)) + GWI_OB(gwi_enum_end(gwi)) - GWI_BB(gwi_enum_ini(gwi, "Enumtest")) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM0", 0)) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM1", 1)) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM2", 2)) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM3", 3)) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM4", 4)) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM5", 5)) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM6", 6)) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM7", 7)) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM8", 8)) - GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM9", 9)) - GWI_OB(gwi_enum_end(gwi)) + GWI_BB(gwi_enum_ini(gwi, "Enumtest")) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM0", 0)) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM1", 1)) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM2", 2)) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM3", 3)) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM4", 4)) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM5", 5)) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM6", 6)) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM7", 7)) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM8", 8)) + GWI_BB(gwi_enum_add(gwi, "TYPED_ENUM9", 9)) + GWI_OB(gwi_enum_end(gwi)) GWI_OB(gwi_class_end(gwi)) return GW_OK; diff --git a/tests/plug/enum_fail.c b/tests/plug/enum_fail.c index d500aedc..cba7eb0f 100644 --- a/tests/plug/enum_fail.c +++ b/tests/plug/enum_fail.c @@ -10,7 +10,7 @@ GWION_IMPORT(enum_test) { GWI_BB(gwi_enum_ini(gwi, "int")) - GWI_BB(gwi_enum_add(gwi, "zero", 0)) + GWI_BB(gwi_enum_add(gwi, "zero", 0)) GWI_OB(gwi_enum_end(gwi)) return GW_OK; } diff --git a/tests/plug/enum_fail2.c b/tests/plug/enum_fail2.c index 8af6c474..b1026e26 100644 --- a/tests/plug/enum_fail2.c +++ b/tests/plug/enum_fail2.c @@ -10,7 +10,7 @@ GWION_IMPORT(enum_test) { GWI_BB(gwi_enum_ini(gwi, NULL)) - GWI_BB(gwi_enum_add(gwi, "adc", 0)) + GWI_BB(gwi_enum_add(gwi, "adc", 0)) GWI_OB(gwi_enum_end(gwi)) return GW_OK; } diff --git a/tests/plug/enum_fail3.c b/tests/plug/enum_fail3.c index 63291dca..a64d7199 100644 --- a/tests/plug/enum_fail3.c +++ b/tests/plug/enum_fail3.c @@ -10,7 +10,7 @@ GWION_IMPORT(enum_test) { GWI_BB(gwi_enum_ini(gwi, NULL)) - GWI_BB(gwi_enum_add(gwi, "adc", 0)) + GWI_BB(gwi_enum_add(gwi, "adc", 0)) GWI_BB(gwi_enum_ini(gwi, NULL)) return GW_OK; } diff --git a/tests/plug/fptr.c b/tests/plug/fptr.c index 50811bd4..7ad1fd49 100644 --- a/tests/plug/fptr.c +++ b/tests/plug/fptr.c @@ -14,12 +14,12 @@ GWION_IMPORT(typedef_test) { GWI_OB(gwi_fptr_end(gwi, 0)) GWI_OB(gwi_class_ini(gwi, "FuncTypedef", NULL)) - GWI_BB(gwi_fptr_ini(gwi, "void", "PtrType")) - GWI_OB(gwi_fptr_end(gwi, ae_flag_static)) - GWI_BB(gwi_func_ini(gwi, "void", "test_func")) - GWI_BB(gwi_func_end(gwi, test_func, ae_flag_static)) - GWI_BB(gwi_item_ini(gwi, "PtrType", "ptr")) - GWI_BB(gwi_item_end(gwi, ae_flag_static, obj, NULL)) + GWI_BB(gwi_fptr_ini(gwi, "void", "PtrType")) + GWI_OB(gwi_fptr_end(gwi, ae_flag_static)) + GWI_BB(gwi_func_ini(gwi, "void", "test_func")) + GWI_BB(gwi_func_end(gwi, test_func, ae_flag_static)) + GWI_BB(gwi_item_ini(gwi, "PtrType", "ptr")) + GWI_BB(gwi_item_end(gwi, ae_flag_static, obj, NULL)) GWI_BB(gwi_class_end(gwi)) return GW_OK; } diff --git a/tests/plug/fptr_tmpl.c b/tests/plug/fptr_tmpl.c index 370f629d..efdcfd31 100644 --- a/tests/plug/fptr_tmpl.c +++ b/tests/plug/fptr_tmpl.c @@ -10,7 +10,7 @@ GWION_IMPORT(typedef_test) { Type t_func_typedef; - GWI_OB((t_func_typedef = gwi_mk_type(gwi, "FuncTypedef", SZ_INT , NULL))) + GWI_OB((t_func_typedef = gwi_mk_type(gwi, "FuncTypedef", SZ_INT, NULL))) GWI_BB(gwi_fptr_ini(gwi, "int", "test:[test]")) GWI_OB(gwi_fptr_end(gwi, 0)) return GW_OK; diff --git a/tests/plug/func_tmpl.c b/tests/plug/func_tmpl.c index 5d3e73a7..4c0e8da8 100644 --- a/tests/plug/func_tmpl.c +++ b/tests/plug/func_tmpl.c @@ -9,9 +9,7 @@ #include "import.h" #include "instr.h" -static SFUN(func_tmpl_xfun) { - -} +static SFUN(func_tmpl_xfun) {} GWION_IMPORT(func_tmpl) { GWI_BB(gwi_func_ini(gwi, "int[]", "test:[A]")) diff --git a/tests/plug/func_tmpl_fail.c b/tests/plug/func_tmpl_fail.c index 362e853b..5b571765 100644 --- a/tests/plug/func_tmpl_fail.c +++ b/tests/plug/func_tmpl_fail.c @@ -9,9 +9,7 @@ #include "import.h" #include "instr.h" -static SFUN(func_tmpl_xfun) { - -} +static SFUN(func_tmpl_xfun) {} GWION_IMPORT(func_tmpl) { GWI_BB(gwi_func_ini(gwi, "voit", "test:[A]")) diff --git a/tests/plug/global_func.c b/tests/plug/global_func.c index 6b47cd53..b9c69bbe 100644 --- a/tests/plug/global_func.c +++ b/tests/plug/global_func.c @@ -11,7 +11,7 @@ SFUN(coverage_int) { puts("test"); - *(m_int*)RETURN = *(m_int*)MEM(0); + *(m_int *)RETURN = *(m_int *)MEM(0); } GWION_IMPORT(global_func_test) { diff --git a/tests/plug/global_var.c b/tests/plug/global_var.c index de157f0d..58316cd3 100644 --- a/tests/plug/global_var.c +++ b/tests/plug/global_var.c @@ -9,13 +9,13 @@ #include "import.h" #include "gwi.h" - GWION_IMPORT(global_var_test) { - const M_Object o = new_object(gwi->gwion->mp, NULL, gwi->gwion->type[et_string]); + const M_Object o = + new_object(gwi->gwion->mp, NULL, gwi->gwion->type[et_string]); STRING(o) = s_name(insert_symbol(gwi->gwion->st, "test")); - GWI_BB(gwi_item_ini(gwi,"string", "i")) + GWI_BB(gwi_item_ini(gwi, "string", "i")) GWI_BB(gwi_item_end(gwi, 0, obj, o)) - GWI_BB(gwi_item_ini(gwi,"float", "f")) + GWI_BB(gwi_item_ini(gwi, "float", "f")) GWI_BB(gwi_item_end(gwi, 0, fnum, 2.1)) return GW_OK; } diff --git a/tests/plug/invalid_arg.c b/tests/plug/invalid_arg.c index 02c58e77..27cb25ad 100644 --- a/tests/plug/invalid_arg.c +++ b/tests/plug/invalid_arg.c @@ -8,7 +8,7 @@ #include "operator.h" #include "import.h" -static MFUN(test_mfun){} +static MFUN(test_mfun) {} GWION_IMPORT(invalid_arg_test) { GWI_OB(gwi_class_ini(gwi, "InvalidArg", NULL)) GWI_BB(gwi_func_ini(gwi, "int[]", "func")) diff --git a/tests/plug/invalid_array.c b/tests/plug/invalid_array.c index baf33fc2..339aae28 100644 --- a/tests/plug/invalid_array.c +++ b/tests/plug/invalid_array.c @@ -8,25 +8,25 @@ #include "operator.h" #include "import.h" -static MFUN(test_mfun){} +static MFUN(test_mfun) {} GWION_IMPORT(invalid_array_test) { GWI_OB(gwi_class_ini(gwi, "InvalidArray", NULL)) GWI_BB(gwi_func_ini(gwi, "int[]", "func")) - GWI_BB(gwi_func_arg(gwi, "int[][]", "i")) - GWI_BB(gwi_func_arg(gwi, "int", "j[]")) - GWI_BB(gwi_func_arg(gwi, "int[]", "k[]")) - GWI_BB(gwi_func_arg(gwi, "int", "l")) + GWI_BB(gwi_func_arg(gwi, "int[][]", "i")) + GWI_BB(gwi_func_arg(gwi, "int", "j[]")) + GWI_BB(gwi_func_arg(gwi, "int[]", "k[]")) + GWI_BB(gwi_func_arg(gwi, "int", "l")) GWI_BB(gwi_func_end(gwi, test_mfun, ae_flag_static)) GWI_BB(gwi_func_ini(gwi, "int[]", "func")) - GWI_BB(gwi_func_arg(gwi, "int", "j[][]")) - GWI_BB(gwi_func_arg(gwi, "int[]", "+k[][][]")) - GWI_BB(gwi_func_arg(gwi, "int", "l")) + GWI_BB(gwi_func_arg(gwi, "int", "j[][]")) + GWI_BB(gwi_func_arg(gwi, "int[]", "+k[][][]")) + GWI_BB(gwi_func_arg(gwi, "int", "l")) GWI_BB(gwi_func_end(gwi, test_mfun, ae_flag_static)) GWI_BB(gwi_func_ini(gwi, "int[]", "func")) - GWI_BB(gwi_func_arg(gwi, "+int", "j[][]")) - GWI_BB(gwi_func_arg(gwi, "int[]", "+k[][][]")) + GWI_BB(gwi_func_arg(gwi, "+int", "j[][]")) + GWI_BB(gwi_func_arg(gwi, "int[]", "+k[][][]")) GWI_BB(gwi_func_end(gwi, test_mfun, ae_flag_static)) GWI_BB(gwi_class_end(gwi)) diff --git a/tests/plug/invalid_func.c b/tests/plug/invalid_func.c index cd5c6c54..f30b7398 100644 --- a/tests/plug/invalid_func.c +++ b/tests/plug/invalid_func.c @@ -8,7 +8,7 @@ #include "operator.h" #include "import.h" -static MFUN(test_mfun){} +static MFUN(test_mfun) {} GWION_IMPORT(invalid_func_test) { GWI_OB(gwi_class_ini(gwi, "t_invalid_var_type", NULL)) GWI_BB(gwi_func_ini(gwi, ".int", "i")) diff --git a/tests/plug/invalid_type1.c b/tests/plug/invalid_type1.c index ed294aa7..2656c619 100644 --- a/tests/plug/invalid_type1.c +++ b/tests/plug/invalid_type1.c @@ -10,7 +10,7 @@ GWION_IMPORT(invalid_type1_test) { GWI_OB(gwi_class_ini(gwi, "invalid_var_type", NULL)) - GWI_BB(gwi_item_ini(gwi,"i|nt", "test")) + GWI_BB(gwi_item_ini(gwi, "i|nt", "test")) GWI_BB(gwi_item_end(gwi, 0, num, 0)) GWI_BB(gwi_class_end(gwi)) return GW_OK; diff --git a/tests/plug/invalid_type2.c b/tests/plug/invalid_type2.c index fd7cf9d5..6b123756 100644 --- a/tests/plug/invalid_type2.c +++ b/tests/plug/invalid_type2.c @@ -10,7 +10,7 @@ GWION_IMPORT(invalid_type2_test) { GWI_OB(gwi_class_ini(gwi, "invalid_var_type", NULL)) - GWI_BB(gwi_item_ini(gwi,".int", "test")) + GWI_BB(gwi_item_ini(gwi, ".int", "test")) GWI_BB(gwi_item_end(gwi, 0, num, 0)) GWI_BB(gwi_class_end(gwi)) return GW_OK; diff --git a/tests/plug/invalid_type3.c b/tests/plug/invalid_type3.c index 4d6ae887..e33fa0fa 100644 --- a/tests/plug/invalid_type3.c +++ b/tests/plug/invalid_type3.c @@ -10,7 +10,7 @@ GWION_IMPORT(invalid_type3_test) { GWI_OB(gwi_class_ini(gwi, "InvalidVarType", NULL)) - GWI_BB(gwi_item_ini(gwi,".int", "test")) + GWI_BB(gwi_item_ini(gwi, ".int", "test")) GWI_BB(gwi_item_end(gwi, 0, num, 0)) GWI_BB(gwi_class_end(gwi)) return GW_OK; diff --git a/tests/plug/mk_type_array.c b/tests/plug/mk_type_array.c index 5c5ee325..2d9959d3 100644 --- a/tests/plug/mk_type_array.c +++ b/tests/plug/mk_type_array.c @@ -9,9 +9,10 @@ #include "import.h" #include "gwi.h" -MFUN(test_mfun){} +MFUN(test_mfun) {} GWION_IMPORT(begin_class) { Type t_invalid_var_name; - GWI_OB((t_invalid_var_name = gwi_mk_type(gwi, "invalid_var_name", SZ_INT, "Object[]"))) + GWI_OB((t_invalid_var_name = + gwi_mk_type(gwi, "invalid_var_name", SZ_INT, "Object[]"))) return GW_OK; } diff --git a/tests/plug/pass.c b/tests/plug/pass.c index b082eb87..fd52f6fa 100644 --- a/tests/plug/pass.c +++ b/tests/plug/pass.c @@ -9,9 +9,7 @@ #include "import.h" #include "gwi.h" -ANN static m_bool pass(Env nv NUSED, Ast ast NUSED) { - return GW_OK; -} +ANN static m_bool pass(Env nv NUSED, Ast ast NUSED) { return GW_OK; } GWION_IMPORT(array_test) { gwi_register_pass(gwi, "dummy", pass); diff --git a/tests/plug/specialid_emit.c b/tests/plug/specialid_emit.c index a64d925f..7a5ffef1 100644 --- a/tests/plug/specialid_emit.c +++ b/tests/plug/specialid_emit.c @@ -11,15 +11,15 @@ #include "specialid.h" #include "gwi.h" - static ID_EMIT(spidem) { const Instr instr = emit_add_instr(emit, RegPushImm); - instr->m_val = 1234; + instr->m_val = 1234; return instr; } GWION_IMPORT(spid_test) { - struct SpecialId_ spid = { .type=gwi->gwion->type[et_int], .em=spidem, .is_const = 1 }; + struct SpecialId_ spid = { + .type = gwi->gwion->type[et_int], .em = spidem, .is_const = 1}; gwi_specialid(gwi, "testid", &spid); return GW_OK; } diff --git a/tests/plug/static_string.c b/tests/plug/static_string.c index 3a5177ea..31434bcd 100644 --- a/tests/plug/static_string.c +++ b/tests/plug/static_string.c @@ -11,7 +11,8 @@ #include "gwi.h" GWION_IMPORT(static_string_test) { - const M_Object o = new_object(gwi->gwion->mp, NULL, gwi->gwion->type[et_string]); + const M_Object o = + new_object(gwi->gwion->mp, NULL, gwi->gwion->type[et_string]); STRING(o) = s_name(insert_symbol(gwi->gwion->st, "test static string")); GWI_BB(gwi_item_ini(gwi, "string", "self")) GWI_BB(gwi_item_end(gwi, ae_flag_global, obj, o)) diff --git a/tests/plug/str2decl.c b/tests/plug/str2decl.c index 0d504726..acbffe27 100644 --- a/tests/plug/str2decl.c +++ b/tests/plug/str2decl.c @@ -14,8 +14,8 @@ GWION_IMPORT(str2td) { GWI_OB(gwi_class_ini(gwi, "Test", NULL)) - GWI_OB(gwi_class_ini(gwi, "Child", NULL)) - GWI_BB(gwi_class_end(gwi)) + GWI_OB(gwi_class_ini(gwi, "Child", NULL)) + GWI_BB(gwi_class_end(gwi)) GWI_BB(gwi_class_end(gwi)) GWI_OB(gwi_class_ini(gwi, "Test2", "Test.Child")) return gwi_class_end(gwi); diff --git a/tests/plug/str2list_fail.c b/tests/plug/str2list_fail.c index 3205ab32..77ae59dd 100644 --- a/tests/plug/str2list_fail.c +++ b/tests/plug/str2list_fail.c @@ -9,9 +9,8 @@ #include "import.h" #include "gwi.h" - GWION_IMPORT(global_var_test) { - GWI_BB(gwi_item_ini(gwi,"SoemClass.child", "i")) + GWI_BB(gwi_item_ini(gwi, "SoemClass.child", "i")) GWI_BB(gwi_item_end(gwi, ae_flag_none, num, 0)) return GW_OK; } diff --git a/tests/plug/struct.c b/tests/plug/struct.c index 9fa4767e..64115913 100644 --- a/tests/plug/struct.c +++ b/tests/plug/struct.c @@ -9,8 +9,8 @@ #include "import.h" #include "gwi.h" -SFUN(coverage_int) { *(m_uint*)RETURN = 0; } -SFUN(coverage_float) { *(m_float*)RETURN = 0; } +SFUN(coverage_int) { *(m_uint *)RETURN = 0; } +SFUN(coverage_float) { *(m_float *)RETURN = 0; } GWION_IMPORT(coverage) { GWI_OB(gwi_struct_ini(gwi, "Struct")) @@ -21,16 +21,16 @@ GWION_IMPORT(coverage) { GWI_BB(gwi_item_end(gwi, ae_flag_static, num, 5)) - GWI_BB(gwi_item_ini(gwi,"int", "s_f")) + GWI_BB(gwi_item_ini(gwi, "int", "s_f")) GWI_BB(gwi_item_end(gwi, ae_flag_static, fnum, 2.1)) - GWI_BB(gwi_item_ini(gwi,"int", "sc_i")) + GWI_BB(gwi_item_ini(gwi, "int", "sc_i")) GWI_BB(gwi_item_end(gwi, ae_flag_static | ae_flag_const, num, 5)) - GWI_BB(gwi_item_ini(gwi,"float", "sc_f")) + GWI_BB(gwi_item_ini(gwi, "float", "sc_f")) GWI_BB(gwi_item_end(gwi, ae_flag_static | ae_flag_const, fnum, 2.1)) - GWI_BB(gwi_item_ini(gwi,"int[][]", "test_array")) + GWI_BB(gwi_item_ini(gwi, "int[][]", "test_array")) GWI_BB(gwi_item_end(gwi, ae_flag_none, obj, NULL)) GWI_BB(gwi_struct_end(gwi)) diff --git a/tests/plug/template_arg.c b/tests/plug/template_arg.c index 39744071..75c33ad1 100644 --- a/tests/plug/template_arg.c +++ b/tests/plug/template_arg.c @@ -12,7 +12,7 @@ static MFUN(template_arg_fun) {} GWION_IMPORT(template_arg_test) { GWI_OB(gwi_class_ini(gwi, "TemplateArg", NULL)) GWI_BB(gwi_func_ini(gwi, "int", "set")) - GWI_BB(gwi_func_arg(gwi, "Pair,float>","test")) + GWI_BB(gwi_func_arg(gwi, "Pair,float>", "test")) GWI_BB(gwi_func_end(gwi, template_arg_fun, ae_flag_none)) GWI_BB(gwi_class_end(gwi)) return GW_OK; diff --git a/tests/plug/trig.c b/tests/plug/trig.c index 0ebcf3b8..b0521872 100644 --- a/tests/plug/trig.c +++ b/tests/plug/trig.c @@ -12,21 +12,18 @@ #include "ugen.h" #include "gwi.h" -static TICK(trig_tick) { - u->out = u->module.gen.trig->in ? u->in : 0; -} +static TICK(trig_tick) { u->out = u->module.gen.trig->in ? u->in : 0; } static CTOR(trig_ctor) { ugen_ini(shred->info->vm->gwion, UGEN(o), 1, 1); - ugen_gen(shred->info->vm->gwion, UGEN(o), trig_tick, (void*)1, 1); + ugen_gen(shred->info->vm->gwion, UGEN(o), trig_tick, (void *)1, 1); } static CTOR(trig2_ctor) { ugen_ini(shred->info->vm->gwion, UGEN(o), 2, 2); - ugen_gen(shred->info->vm->gwion, UGEN(o), trig_tick, (void*)1, 1); + ugen_gen(shred->info->vm->gwion, UGEN(o), trig_tick, (void *)1, 1); } - GWION_IMPORT(trig) { GWI_OB(gwi_class_ini(gwi, "Trig", "UGen")) gwi_class_xtor(gwi, trig_ctor, NULL); diff --git a/tests/plug/union.c b/tests/plug/union.c index 54b26d79..31c44a44 100644 --- a/tests/plug/union.c +++ b/tests/plug/union.c @@ -10,8 +10,8 @@ GWION_IMPORT(union_test) { GWI_BB(gwi_union_ini(gwi, "U")) - GWI_BB(gwi_union_add(gwi,"float", "f")) - GWI_BB(gwi_union_add(gwi,"int", "i")) + GWI_BB(gwi_union_add(gwi, "float", "f")) + GWI_BB(gwi_union_add(gwi, "int", "i")) GWI_OB(gwi_union_end(gwi, 0)) return GW_OK; } diff --git a/tests/plug/union_fail_exp.c b/tests/plug/union_fail_exp.c index dc89fc93..69267987 100644 --- a/tests/plug/union_fail_exp.c +++ b/tests/plug/union_fail_exp.c @@ -10,8 +10,8 @@ GWION_IMPORT(union_test) { GWI_BB(gwi_union_ini(gwi, "U")) - GWI_BB(gwi_union_add(gwi,"Float", "f")) - GWI_BB(gwi_union_add(gwi,"int", "i")) + GWI_BB(gwi_union_add(gwi, "Float", "f")) + GWI_BB(gwi_union_add(gwi, "int", "i")) GWI_OB(gwi_union_end(gwi, 0)) return GW_OK; } diff --git a/tests/plug/union_member.c b/tests/plug/union_member.c index c7e25327..2c207a03 100644 --- a/tests/plug/union_member.c +++ b/tests/plug/union_member.c @@ -10,10 +10,10 @@ GWION_IMPORT(union_member) { GWI_OB(gwi_class_ini(gwi, "UnionMember", NULL)) - GWI_BB(gwi_union_ini(gwi, "U")) - GWI_BB(gwi_union_add(gwi,"float", "f")) - GWI_BB(gwi_union_add(gwi,"int[]", "array")) - GWI_OB(gwi_union_end(gwi, ae_flag_none)) + GWI_BB(gwi_union_ini(gwi, "U")) + GWI_BB(gwi_union_add(gwi, "float", "f")) + GWI_BB(gwi_union_add(gwi, "int[]", "array")) + GWI_OB(gwi_union_end(gwi, ae_flag_none)) GWI_BB(gwi_class_end(gwi)) return GW_OK; } diff --git a/tests/plug/union_name.c b/tests/plug/union_name.c index 166b6242..81d804fd 100644 --- a/tests/plug/union_name.c +++ b/tests/plug/union_name.c @@ -10,8 +10,8 @@ GWION_IMPORT(union_test) { GWI_BB(gwi_union_ini(gwi, "my_union")) - GWI_BB(gwi_union_add(gwi,"float", "f")) - GWI_BB(gwi_union_add(gwi,"int", "i")) + GWI_BB(gwi_union_add(gwi, "float", "f")) + GWI_BB(gwi_union_add(gwi, "int", "i")) GWI_OB(gwi_union_end(gwi, 0)) return GW_OK; } diff --git a/tests/plug/union_tmpl.c b/tests/plug/union_tmpl.c index a3979944..77124075 100644 --- a/tests/plug/union_tmpl.c +++ b/tests/plug/union_tmpl.c @@ -10,9 +10,9 @@ GWION_IMPORT(union_test) { GWI_BB(gwi_union_ini(gwi, "U:[A]")) - GWI_BB(gwi_union_add(gwi,"float", "f")) - GWI_BB(gwi_union_add(gwi,"int", "i")) - GWI_BB(gwi_union_add(gwi,"A", "a")) + GWI_BB(gwi_union_add(gwi, "float", "f")) + GWI_BB(gwi_union_add(gwi, "int", "i")) + GWI_BB(gwi_union_add(gwi, "A", "a")) GWI_OB(gwi_union_end(gwi, 0)) return GW_OK; } diff --git a/tests/plug/union_tmpl_fail.c b/tests/plug/union_tmpl_fail.c index a3979944..77124075 100644 --- a/tests/plug/union_tmpl_fail.c +++ b/tests/plug/union_tmpl_fail.c @@ -10,9 +10,9 @@ GWION_IMPORT(union_test) { GWI_BB(gwi_union_ini(gwi, "U:[A]")) - GWI_BB(gwi_union_add(gwi,"float", "f")) - GWI_BB(gwi_union_add(gwi,"int", "i")) - GWI_BB(gwi_union_add(gwi,"A", "a")) + GWI_BB(gwi_union_add(gwi, "float", "f")) + GWI_BB(gwi_union_add(gwi, "int", "i")) + GWI_BB(gwi_union_add(gwi, "A", "a")) GWI_OB(gwi_union_end(gwi, 0)) return GW_OK; } diff --git a/tests/plug/union_tmpl_fail2.c b/tests/plug/union_tmpl_fail2.c index 7a65b7e8..f0e48a37 100644 --- a/tests/plug/union_tmpl_fail2.c +++ b/tests/plug/union_tmpl_fail2.c @@ -10,9 +10,9 @@ GWION_IMPORT(union_test) { GWI_BB(gwi_union_ini(gwi, "U:[A]")) - GWI_BB(gwi_union_add(gwi,"float", "f")) - GWI_BB(gwi_union_add(gwi,"int", "i")) - GWI_BB(gwi_union_add(gwi,"A", "a")) + GWI_BB(gwi_union_add(gwi, "float", "f")) + GWI_BB(gwi_union_add(gwi, "int", "i")) + GWI_BB(gwi_union_add(gwi, "A", "a")) GWI_BB(gwi_union_ini(gwi, "U:[A]")) GWI_OB(gwi_union_end(gwi, 0)) return GW_OK; diff --git a/tests/plug/union_tmpl_fail3.c b/tests/plug/union_tmpl_fail3.c index a3979944..77124075 100644 --- a/tests/plug/union_tmpl_fail3.c +++ b/tests/plug/union_tmpl_fail3.c @@ -10,9 +10,9 @@ GWION_IMPORT(union_test) { GWI_BB(gwi_union_ini(gwi, "U:[A]")) - GWI_BB(gwi_union_add(gwi,"float", "f")) - GWI_BB(gwi_union_add(gwi,"int", "i")) - GWI_BB(gwi_union_add(gwi,"A", "a")) + GWI_BB(gwi_union_add(gwi, "float", "f")) + GWI_BB(gwi_union_add(gwi, "int", "i")) + GWI_BB(gwi_union_add(gwi, "A", "a")) GWI_OB(gwi_union_end(gwi, 0)) return GW_OK; } diff --git a/tests/plug/variadic.c b/tests/plug/variadic.c index 5ad04f6b..08ddc128 100644 --- a/tests/plug/variadic.c +++ b/tests/plug/variadic.c @@ -10,29 +10,27 @@ #include "vararg.h" #include "gwi.h" -static MFUN(m_test) { - gw_out("%p\n", *(M_Object*)MEM(0)); -} +static MFUN(m_test) { gw_out("%p\n", *(M_Object *)MEM(0)); } static MFUN(m_variadic) { - M_Object str_obj = *(M_Object*)MEM(SZ_INT); - if(!str_obj)return; - m_str str = STRING(str_obj); - const M_Object vararg_obj = *(M_Object*)MEM(SZ_INT*2); - struct Vararg_* arg = *(struct Vararg_**)vararg_obj->data; + M_Object str_obj = *(M_Object *)MEM(SZ_INT); + if (!str_obj) return; + m_str str = STRING(str_obj); + const M_Object vararg_obj = *(M_Object *)MEM(SZ_INT * 2); + struct Vararg_ *arg = *(struct Vararg_ **)vararg_obj->data; - m_uint i = 0; - const m_uint offset = *(m_uint*)(vararg_obj->data + SZ_INT * 3); - while(i < *(m_uint*)(vararg_obj->data + SZ_INT * 5)) { - if(*str == 'i') { - gw_out("%"INT_F "\n", *(m_int*)(arg->d + offset)); - *(m_uint*)(vararg_obj->data + SZ_INT * 3) += SZ_INT; - } else if(*str == 'f') { - gw_out("%f\n", *(m_float*)(arg->d + offset)); - *(m_uint*)(vararg_obj->data + SZ_INT * 3) += SZ_FLOAT; - } else if(*str == 'o') { - gw_out("%p\n", (void*)*(M_Object*)(arg->d + offset)); - *(m_uint*)(vararg_obj->data + SZ_INT * 3) += SZ_INT; + m_uint i = 0; + const m_uint offset = *(m_uint *)(vararg_obj->data + SZ_INT * 3); + while (i < *(m_uint *)(vararg_obj->data + SZ_INT * 5)) { + if (*str == 'i') { + gw_out("%" INT_F "\n", *(m_int *)(arg->d + offset)); + *(m_uint *)(vararg_obj->data + SZ_INT * 3) += SZ_INT; + } else if (*str == 'f') { + gw_out("%f\n", *(m_float *)(arg->d + offset)); + *(m_uint *)(vararg_obj->data + SZ_INT * 3) += SZ_FLOAT; + } else if (*str == 'o') { + gw_out("%p\n", (void *)*(M_Object *)(arg->d + offset)); + *(m_uint *)(vararg_obj->data + SZ_INT * 3) += SZ_INT; } ++i; str++; diff --git a/tests/plug/vm_remove.c b/tests/plug/vm_remove.c index b700d1e3..119a6ffb 100644 --- a/tests/plug/vm_remove.c +++ b/tests/plug/vm_remove.c @@ -9,9 +9,7 @@ #include "import.h" #include "instr.h" -SFUN(coverage_int) { - vm_remove(shred->info->vm, *(m_int*)MEM(0)); -} +SFUN(coverage_int) { vm_remove(shred->info->vm, *(m_int *)MEM(0)); } GWION_IMPORT(gwion_remove_test) { GWI_BB(gwi_func_ini(gwi, "int", "test")) diff --git a/util b/util index 278742dd..7364841e 160000 --- a/util +++ b/util @@ -1 +1 @@ -Subproject commit 278742ddb8dee96156e8edff72586c71d2f093b6 +Subproject commit 7364841ef46d456037eec97cdcb3ff3abb41289d -- 2.43.0