From: Brendan Hansen Date: Wed, 7 Jul 2021 13:50:02 +0000 (-0500) Subject: fixed aliasing issues; guaranteed new bugs showed up X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=abd3154618ba2a9d5f7afdda6ea8e89556363a5f;p=onyx.git fixed aliasing issues; guaranteed new bugs showed up --- diff --git a/bin/onyx b/bin/onyx index 668b9636..93271ecc 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index cf57b922..0e9dcc94 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -68,6 +68,7 @@ NODE(CompoundType) \ \ NODE(Binding) \ + NODE(Alias) \ NODE(MemRes) \ NODE(Include) \ NODE(UsePackage) \ @@ -108,6 +109,7 @@ typedef enum AstKind { Ast_Kind_Memres, Ast_Kind_Binding, + Ast_Kind_Alias, Ast_Kind_Function, Ast_Kind_Overloaded_Function, Ast_Kind_Polymorphic_Proc, @@ -787,12 +789,9 @@ struct AstCompoundType { // Top level nodes struct AstBinding { AstTyped_base; AstNode* node; }; +struct AstAlias { AstTyped_base; AstTyped* alias; }; struct AstMemRes { AstTyped_base; u64 addr; AstTyped *initial_value; }; struct AstInclude { AstNode_base; char* name; }; -struct AstAlias { - AstNode_base; - OnyxToken *alias; -}; struct AstGlobal { AstTyped_base; diff --git a/modules/immediate_mode/immediate_renderer.onyx b/modules/immediate_mode/immediate_renderer.onyx index 98f751ae..d31a40e2 100644 --- a/modules/immediate_mode/immediate_renderer.onyx +++ b/modules/immediate_mode/immediate_renderer.onyx @@ -94,19 +94,20 @@ Immediate_Renderer :: struct { gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer); - use package builtin.type_info { offset_of }; + offset_of :: (package builtin.type_info).offset_of; + IV :: Immediate_Vertex; // Position gl.enableVertexAttribArray(0); - gl.vertexAttribPointer(0, 2, gl.FLOAT, false, sizeof Immediate_Vertex, offset_of(Immediate_Vertex, "position")); + gl.vertexAttribPointer(0, 2, gl.FLOAT, false, sizeof IV, offset_of(IV, "position")); // Color gl.enableVertexAttribArray(1); - gl.vertexAttribPointer(1, 4, gl.FLOAT, false, sizeof Immediate_Vertex, offset_of(Immediate_Vertex, "color")); + gl.vertexAttribPointer(1, 4, gl.FLOAT, false, sizeof IV, offset_of(IV, "color")); // Texture gl.enableVertexAttribArray(2); - gl.vertexAttribPointer(2, 2, gl.FLOAT, false, sizeof Immediate_Vertex, offset_of(Immediate_Vertex, "texture")); + gl.vertexAttribPointer(2, 2, gl.FLOAT, false, sizeof IV, offset_of(IV, "texture")); gl.bindBuffer(gl.ARRAY_BUFFER, -1); diff --git a/src/onyxastnodes.c b/src/onyxastnodes.c index 94a60c45..1b4d186d 100644 --- a/src/onyxastnodes.c +++ b/src/onyxastnodes.c @@ -10,6 +10,7 @@ static const char* ast_node_names[] = { "MEMORY RESERVATION", "BINDING", + "ALIAS", "FUNCTION", "OVERLOADED_FUNCTION", "POLYMORPHIC PROC", @@ -231,27 +232,31 @@ AstNumLit* ast_reduce_binop(bh_allocator a, AstBinaryOp* node) { #define REDUCE_UNOP(op) \ if (type_is_small_integer(unop->type) || type_is_bool(unop->type)) { \ - res->value.i = op ((AstNumLit *) unop->expr)->value.i; \ + res->value.i = op (operand)->value.i; \ } else if (type_is_integer(unop->type) || unop->type->Basic.kind == Basic_Kind_Int_Unsized) { \ - res->value.l = op ((AstNumLit *) unop->expr)->value.l; \ + res->value.l = op (operand)->value.l; \ } else if (unop->type->Basic.kind == Basic_Kind_F32) { \ - res->value.f = op ((AstNumLit *) unop->expr)->value.f; \ + res->value.f = op (operand)->value.f; \ } else if (unop->type->Basic.kind == Basic_Kind_F64 || unop->type->Basic.kind == Basic_Kind_Float_Unsized) { \ - res->value.d = op ((AstNumLit *) unop->expr)->value.d; \ + res->value.d = op (operand)->value.d; \ } \ break; #define REDUCE_UNOP_INT(op) \ if (type_is_small_integer(unop->type) || type_is_bool(unop->type)) { \ - res->value.i = op ((AstNumLit *) unop->expr)->value.i; \ + res->value.i = op (operand)->value.i; \ } else if (type_is_integer(unop->type) || res->type->Basic.kind == Basic_Kind_Int_Unsized) { \ - res->value.l = op ((AstNumLit *) unop->expr)->value.l; \ + res->value.l = op (operand)->value.l; \ } AstTyped* ast_reduce_unaryop(bh_allocator a, AstUnaryOp* unop) { - unop->expr = ast_reduce(a, unop->expr); + // GROSS + AstNumLit* operand = (AstNumLit *) ast_reduce(a, unop->expr); + unop->expr = (AstTyped *) operand; - if (unop->expr->kind != Ast_Kind_NumLit) { + // while (operand->kind == Ast_Kind_Alias) operand = (AstNumLit *) ((AstAlias *) operand)->alias; + + if (operand->kind != Ast_Kind_NumLit) { return (AstTyped *) unop; } @@ -265,7 +270,7 @@ AstTyped* ast_reduce_unaryop(bh_allocator a, AstUnaryOp* unop) { switch (unop->operation) { case Unary_Op_Negate: REDUCE_UNOP(-); case Unary_Op_Not: { - if (type_is_bool(res->type)) res->value.i = ! ((AstNumLit *) unop->expr)->value.i; + if (type_is_bool(res->type)) res->value.i = ! (operand)->value.i; break; } case Unary_Op_Bitwise_Not: REDUCE_UNOP_INT(~); @@ -283,6 +288,7 @@ AstTyped* ast_reduce(bh_allocator a, AstTyped* node) { case Ast_Kind_Binary_Op: return (AstTyped *) ast_reduce_binop(a, (AstBinaryOp *) node); case Ast_Kind_Unary_Op: return (AstTyped *) ast_reduce_unaryop(a, (AstUnaryOp *) node); case Ast_Kind_Enum_Value: return (AstTyped *) ((AstEnumValue *) node)->value; + case Ast_Kind_Alias: return (AstTyped *) ast_reduce(a, ((AstAlias *) node)->alias); default: return node; } } @@ -529,6 +535,10 @@ b32 type_check_or_auto_cast(AstTyped** pnode, Type* type) { return 0; } } + else if (node->kind == Ast_Kind_Alias) { + AstAlias* alias = (AstAlias *) node; + return type_check_or_auto_cast(&alias->alias, type); + } return 0; } @@ -555,6 +565,11 @@ Type* resolve_expression_type(AstTyped* node) { if_expr->type = ltype; } + if (node->kind == Ast_Kind_Alias) { + AstAlias* alias = (AstAlias *) node; + alias->type = resolve_expression_type(alias->alias); + } + if (node_is_type((AstNode *) node)) { return &basic_types[Basic_Kind_Type_Index]; } @@ -584,6 +599,10 @@ i64 get_expression_integer_value(AstTyped* node) { return ((AstNumLit *) node)->value.l; } + if (node->kind == Ast_Kind_NumLit && type_is_bool(node->type)) { + return ((AstNumLit *) node)->value.i; + } + if (node->kind == Ast_Kind_Argument) { return get_expression_integer_value(((AstArgument *) node)->value); } @@ -596,6 +615,10 @@ i64 get_expression_integer_value(AstTyped* node) { return ((AstAlignOf *) node)->alignment; } + if (node->kind == Ast_Kind_Alias) { + return get_expression_integer_value(((AstAlias *) node)->alias); + } + if (node_is_type((AstNode*) node)) { Type* type = type_build_from_ast(context.ast_alloc, (AstType *) node); return type->id; @@ -918,8 +941,8 @@ const char* node_get_type_name(void* node) { b32 static_if_resolution(AstIf* static_if) { if (static_if->kind != Ast_Kind_Static_If) return 0; - AstNumLit* condition_value = (AstNumLit *) static_if->cond; - assert(condition_value->kind == Ast_Kind_NumLit); // This should be right, right? + // assert(condition_value->kind == Ast_Kind_NumLit); // This should be right, right? + i64 value = get_expression_integer_value(static_if->cond); - return condition_value->value.i != 0; + return value != 0; } diff --git a/src/onyxchecker.c b/src/onyxchecker.c index 4c2fbe45..7476fc32 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -1652,6 +1652,12 @@ CheckStatus check_expression(AstTyped** pexpr) { CHECK(if_expression, (AstIfExpression *) expr); break; + case Ast_Kind_Alias: + CHECK(expression, &((AstAlias *) expr)->alias); + expr->flags |= (((AstAlias *) expr)->alias->flags & Ast_Flag_Comptime); + expr->type = ((AstAlias *) expr)->alias->type; + break; + case Ast_Kind_StrLit: break; case Ast_Kind_File_Contents: break; case Ast_Kind_Overloaded_Function: break; diff --git a/src/onyxclone.c b/src/onyxclone.c index bef0eec6..3130808d 100644 --- a/src/onyxclone.c +++ b/src/onyxclone.c @@ -15,6 +15,7 @@ static inline b32 should_clone(AstNode* node) { case Ast_Kind_Enum_Value: case Ast_Kind_Overloaded_Function: case Ast_Kind_Polymorphic_Proc: + case Ast_Kind_Alias: return 0; default: return 1; diff --git a/src/onyxparser.c b/src/onyxparser.c index e42fc860..74ca3320 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -2275,10 +2275,20 @@ static AstBinding* parse_top_level_binding(OnyxParser* parser, OnyxToken* symbol node->token = symbol; } + if (node_is_type((AstNode *) node)); + else if (node->kind == Ast_Kind_Package); + else if (node->kind == Ast_Kind_NumLit); + else { + AstAlias* alias = make_node(AstAlias, Ast_Kind_Alias); + alias->token = node->token; + alias->alias = node; + node = (AstTyped *) alias; + } + // HACK: This should maybe be entered elsewhere? ENTITY_SUBMIT(node); } - + AstBinding* binding = make_node(AstBinding, Ast_Kind_Binding); binding->token = symbol; binding->node = (AstNode *) node; diff --git a/src/onyxsymres.c b/src/onyxsymres.c index b5e7c2fb..3934e308 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -220,6 +220,13 @@ static SymresStatus symres_type(AstType** type) { AstCompoundType* ctype = (AstCompoundType *) *type; bh_arr_each(AstType *, type, ctype->types) SYMRES(type, type); + break; + } + + case Ast_Kind_Alias: { + AstAlias* alias = (AstAlias *) *type; + SYMRES(type, (AstType **) &alias->alias); + break; } } @@ -415,6 +422,7 @@ static SymresStatus symres_expression(AstTyped** expr) { case Ast_Kind_Method_Call: SYMRES(method_call, (AstBinaryOp **) expr); break; case Ast_Kind_Size_Of: SYMRES(size_of, (AstSizeOf *)*expr); break; case Ast_Kind_Align_Of: SYMRES(align_of, (AstAlignOf *)*expr); break; + case Ast_Kind_Alias: SYMRES(expression, &((AstAlias *) *expr)->alias); break; case Ast_Kind_Range_Literal: SYMRES(expression, &((AstRangeLiteral *)(*expr))->low); diff --git a/src/onyxtypes.c b/src/onyxtypes.c index 58734c65..544b3f94 100644 --- a/src/onyxtypes.c +++ b/src/onyxtypes.c @@ -555,6 +555,11 @@ Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) { type_register(comp_type); return comp_type; } + + case Ast_Kind_Alias: { + AstAlias* alias = (AstAlias *) type_node; + return type_build_from_ast(alloc, (AstType *) alias->alias); + } } return NULL; diff --git a/src/onyxutils.c b/src/onyxutils.c index c4a8ef56..01b96e68 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -162,6 +162,7 @@ AstNode* try_symbol_raw_resolve_from_node(AstNode* node, char* symbol) { switch (node->kind) { case Ast_Kind_Type_Raw_Alias: node = (AstNode *) ((AstTypeRawAlias *) node)->to->ast_type; break; case Ast_Kind_Type_Alias: node = (AstNode *) ((AstTypeAlias *) node)->to; break; + case Ast_Kind_Alias: node = (AstNode *) ((AstAlias *) node)->alias; break; case Ast_Kind_Pointer_Type: { if (used_pointer) goto all_types_peeled_off; used_pointer = 1; diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 8f5fbe82..1f1b43b3 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -2467,6 +2467,7 @@ EMIT_FUNC(expression, AstTyped* expr) { case Ast_Kind_Intrinsic_Call: emit_intrinsic_call(mod, &code, (AstCall *) expr); break; case Ast_Kind_Binary_Op: emit_binop(mod, &code, (AstBinaryOp *) expr); break; case Ast_Kind_Unary_Op: emit_unaryop(mod, &code, (AstUnaryOp *) expr); break; + case Ast_Kind_Alias: emit_expression(mod, &code, ((AstAlias *) expr)->alias); break; case Ast_Kind_Address_Of: { AstAddressOf* aof = (AstAddressOf *) expr;