fixed aliasing issues; guaranteed new bugs showed up
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 7 Jul 2021 13:50:02 +0000 (08:50 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 7 Jul 2021 13:50:02 +0000 (08:50 -0500)
bin/onyx
include/onyxastnodes.h
modules/immediate_mode/immediate_renderer.onyx
src/onyxastnodes.c
src/onyxchecker.c
src/onyxclone.c
src/onyxparser.c
src/onyxsymres.c
src/onyxtypes.c
src/onyxutils.c
src/onyxwasm.c

index 668b963666db02cdcea07df7675b85be0e436c91..93271ecc3c300e6e775c12c824c43e7e29473d84 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index cf57b922d540e4db9fbbca778a03db30af7b77c6..0e9dcc9486d4fcf0fab3ad3079fd1b7cf36e874f 100644 (file)
@@ -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;
 
index 98f751aedb4d70b5c973c476048ef7c5cd7709e8..d31a40e284cca6f082f40863e92844be13ab44a9 100644 (file)
@@ -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);
 
index 94a60c457265e03e4e3fbe536b79e993c3ab0bc5..1b4d186d8be252591681546aee8af8ba4a6da4a5 100644 (file)
@@ -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;
 }
index 4c2fbe451471a8f244c6d3fd36e3a8ddf905802b..7476fc32a5de43f1bd3800391ec12b77a0cfe7cf 100644 (file)
@@ -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;
index bef0eec6e2943c582b3980ad82b24159c9ff88d0..3130808dc1636d79b62b16a43ba8d7eed9ad64d3 100644 (file)
@@ -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;
index e42fc8604a35ffbef72a687505fde73699d46388..74ca3320be83ccc8d6dfc8adcccbd9237dc19711 100644 (file)
@@ -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;
index b5e7c2fb764556f468c40adc082d6e726fc79372..3934e308b4ff22b3fbbae6592dc4d5453868652f 100644 (file)
@@ -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);
index 58734c65b7db927b8a1813bbe70e4b686129b566..544b3f94dc7a857d46e3ea83324e7e80d7483b0d 100644 (file)
@@ -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;
index c4a8ef567d490bd80ea29dad8d29d8189ec4b16e..01b96e681d561ad17be40aa51a7f034b38b97ccf 100644 (file)
@@ -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;
index 8f5fbe827bf11a0dd20f35a921da3b87314aa9df..1f1b43b35e2299aba7d9a9ed4d19d4eced8d359f 100644 (file)
@@ -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;