From ca053585ac34dbd2edb95e9142dc17ff217d1e65 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 3 Jun 2021 15:08:01 -0700 Subject: [PATCH] fix var lookups on generic structs --- generators/wraith.y | 4 +-- generic.w | 10 ++++-- src/codegen.c | 80 +++++++++++++++++++++------------------------ 3 files changed, 46 insertions(+), 48 deletions(-) diff --git a/generators/wraith.y b/generators/wraith.y index 53678f9..a3cd8c5 100644 --- a/generators/wraith.y +++ b/generators/wraith.y @@ -294,11 +294,11 @@ Statements : Statement $$ = AddStatement($1, $2); } -Arguments : PrimaryExpression +Arguments : Expression { $$ = StartFunctionArgumentSequenceNode($1); } - | Arguments COMMA PrimaryExpression + | Arguments COMMA Expression { $$ = AddFunctionArgumentNode($1, $3); } diff --git a/generic.w b/generic.w index 8007da7..51fb64f 100644 --- a/generic.w +++ b/generic.w @@ -25,8 +25,12 @@ struct Program { x: int = 4; y: int = Foo.Func(x); block: MemoryBlock; - addr: MemoryAddress = @malloc(y); - @free(addr); - return x; + block.capacity = y; + block.start = @malloc(y * @sizeof()); + z: MemoryAddress = block.AddressOf(2); + Console.PrintLine("%p", block.start); + Console.PrintLine("%p", z); + @free(block.start); + return 0; } } diff --git a/src/codegen.c b/src/codegen.c index 8919f79..fad7286 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -119,7 +119,7 @@ typedef struct MonomorphizedGenericStructHashEntry uint64_t key; TypeTag **types; uint32_t typeCount; - StructTypeDeclaration structDeclaration; + StructTypeDeclaration *structDeclaration; } MonomorphizedGenericStructHashEntry; typedef struct MonomorphizedGenericStructHashArray @@ -409,7 +409,33 @@ static LLVMTypeRef LookupCustomType(char *name) return NULL; } -static StructTypeDeclaration CompileMonomorphizedGenericStruct( +static StructTypeDeclaration *AddStructDeclaration( + LLVMModuleRef module, + LLVMTypeRef wStructType, + LLVMTypeRef wStructPointerType, + char *name) +{ + uint32_t index = structTypeDeclarationCount; + structTypeDeclarations = realloc( + structTypeDeclarations, + sizeof(StructTypeDeclaration) * (structTypeDeclarationCount + 1)); + structTypeDeclarations[index].module = module; + structTypeDeclarations[index].structType = wStructType; + structTypeDeclarations[index].structPointerType = wStructPointerType; + structTypeDeclarations[index].name = strdup(name); + structTypeDeclarations[index].fields = NULL; + structTypeDeclarations[index].fieldCount = 0; + structTypeDeclarations[index].functions = NULL; + structTypeDeclarations[index].functionCount = 0; + structTypeDeclarations[index].genericFunctions = NULL; + structTypeDeclarations[index].genericFunctionCount = 0; + + structTypeDeclarationCount += 1; + + return &structTypeDeclarations[index]; +} + +static StructTypeDeclaration *CompileMonomorphizedGenericStruct( GenericStructTypeDeclaration *genericStructTypeDeclaration, TypeTag **genericArgumentTypes, uint32_t genericArgumentTypeCount) @@ -454,17 +480,11 @@ static StructTypeDeclaration CompileMonomorphizedGenericStruct( LLVMTypeRef wStructType = LLVMStructCreateNamed(context, structName); LLVMTypeRef wStructPointerType = LLVMPointerType(wStructType, 0); - StructTypeDeclaration declaration; - declaration.module = genericStructTypeDeclaration->module; - declaration.name = structName; - declaration.structType = wStructType; - declaration.structPointerType = wStructPointerType; - declaration.genericFunctions = NULL; - declaration.genericFunctionCount = 0; - declaration.functions = NULL; - declaration.functionCount = 0; - declaration.fields = NULL; - declaration.fieldCount = 0; + StructTypeDeclaration *declaration = AddStructDeclaration( + genericStructTypeDeclaration->module, + wStructType, + wStructPointerType, + structName); /* first build the structure def */ for (i = 0; i < declarationCount; i += 1) @@ -479,7 +499,7 @@ static StructTypeDeclaration CompileMonomorphizedGenericStruct( ->declarationSequence.sequence[i] ->declaration.identifier->typeTag); AddFieldToStructDeclaration( - &declaration, + declaration, structDeclarationNode->structDeclaration.declarationSequence ->declarationSequence.sequence[i] ->declaration.identifier->identifier.name); @@ -499,7 +519,7 @@ static StructTypeDeclaration CompileMonomorphizedGenericStruct( { case FunctionDeclaration: CompileFunction( - &declaration, + declaration, structDeclarationNode->structDeclaration.declarationSequence ->declarationSequence.sequence[i]); break; @@ -563,7 +583,7 @@ static StructTypeDeclaration *LookupGenericStructType( if (hashEntry == NULL) { - StructTypeDeclaration structTypeDeclaration = + StructTypeDeclaration *structTypeDeclaration = CompileMonomorphizedGenericStruct( &genericStructTypeDeclarations[i], genericTypeTags, @@ -590,7 +610,7 @@ static StructTypeDeclaration *LookupGenericStructType( hashEntry = &hashArray->elements[hashArray->count - 1]; } - return &hashEntry->structDeclaration; + return hashEntry->structDeclaration; } } @@ -756,32 +776,6 @@ static LLVMValueRef FindVariableValue(LLVMBuilderRef builder, char *name) return NULL; } -static StructTypeDeclaration *AddStructDeclaration( - LLVMModuleRef module, - LLVMTypeRef wStructType, - LLVMTypeRef wStructPointerType, - char *name) -{ - uint32_t index = structTypeDeclarationCount; - structTypeDeclarations = realloc( - structTypeDeclarations, - sizeof(StructTypeDeclaration) * (structTypeDeclarationCount + 1)); - structTypeDeclarations[index].module = module; - structTypeDeclarations[index].structType = wStructType; - structTypeDeclarations[index].structPointerType = wStructPointerType; - structTypeDeclarations[index].name = strdup(name); - structTypeDeclarations[index].fields = NULL; - structTypeDeclarations[index].fieldCount = 0; - structTypeDeclarations[index].functions = NULL; - structTypeDeclarations[index].functionCount = 0; - structTypeDeclarations[index].genericFunctions = NULL; - structTypeDeclarations[index].genericFunctionCount = 0; - - structTypeDeclarationCount += 1; - - return &structTypeDeclarations[index]; -} - static void DeclareStructFunction( StructTypeDeclaration *structTypeDeclaration, LLVMValueRef function,