diff --git a/src/codegen.c b/src/codegen.c index c4fee87..15b5f11 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -111,7 +111,7 @@ struct StructTypeDeclaration uint32_t genericFunctionCount; }; -StructTypeDeclaration *structTypeDeclarations; +StructTypeDeclaration **structTypeDeclarations; uint32_t structTypeDeclarationCount; typedef struct MonomorphizedGenericStructHashEntry @@ -401,9 +401,9 @@ static LLVMTypeRef LookupCustomType(char *name) for (i = 0; i < structTypeDeclarationCount; i += 1) { - if (strcmp(structTypeDeclarations[i].name, name) == 0) + if (strcmp(structTypeDeclarations[i]->name, name) == 0) { - return structTypeDeclarations[i].structType; + return structTypeDeclarations[i]->structType; } } @@ -420,27 +420,30 @@ static StructTypeDeclaration *AddStructDeclaration( 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; + sizeof(StructTypeDeclaration*) * (structTypeDeclarationCount + 1)); + structTypeDeclarations[index] = malloc(sizeof(StructTypeDeclaration)); + 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]; + return structTypeDeclarations[index]; } -static StructTypeDeclaration *CompileMonomorphizedGenericStruct( +static MonomorphizedGenericStructHashEntry *CompileMonomorphizedGenericStruct( GenericStructTypeDeclaration *genericStructTypeDeclaration, TypeTag **genericArgumentTypes, - uint32_t genericArgumentTypeCount) + uint32_t genericArgumentTypeCount, + MonomorphizedGenericStructHashArray *hashArray, + uint64_t typeHash) { uint32_t i = 0; uint32_t fieldCount = 0; @@ -486,6 +489,25 @@ static StructTypeDeclaration *CompileMonomorphizedGenericStruct( free(structName); + /* add entry to the hash array here in case of recursion */ + hashArray->elements = realloc( + hashArray->elements, + sizeof(MonomorphizedGenericStructHashEntry) * + (hashArray->count + 1)); + hashArray->elements[hashArray->count].key = typeHash; + hashArray->elements[hashArray->count].types = + malloc(sizeof(TypeTag *) * genericArgumentTypeCount); + hashArray->elements[hashArray->count].typeCount = + genericArgumentTypeCount; + hashArray->elements[hashArray->count].structDeclaration = + declaration; + for (uint32_t j = 0; j < genericArgumentTypeCount; j += 1) + { + hashArray->elements[hashArray->count].types[j] = + genericArgumentTypes[j]; + } + hashArray->count += 1; + /* first build the structure def */ for (i = 0; i < declarationCount; i += 1) { @@ -528,7 +550,7 @@ static StructTypeDeclaration *CompileMonomorphizedGenericStruct( PopScopeFrame(scope); - return declaration; + return &hashArray->elements[hashArray->count - 1];; } static StructTypeDeclaration *LookupGenericStructType( @@ -578,38 +600,20 @@ static StructTypeDeclaration *LookupGenericStructType( if (match) { - hashEntry = &hashArray->elements[i]; + hashEntry = &hashArray->elements[j]; break; } } if (hashEntry == NULL) { - StructTypeDeclaration *structTypeDeclaration = + hashEntry = CompileMonomorphizedGenericStruct( &genericStructTypeDeclarations[i], genericTypeTags, - typeTag->genericArgumentCount); - - hashArray->elements = realloc( - hashArray->elements, - sizeof(MonomorphizedGenericStructHashEntry) * - (hashArray->count + 1)); - hashArray->elements[hashArray->count].key = typeHash; - hashArray->elements[hashArray->count].types = - malloc(sizeof(TypeTag *) * typeTag->genericArgumentCount); - hashArray->elements[hashArray->count].typeCount = - typeTag->genericArgumentCount; - hashArray->elements[hashArray->count].structDeclaration = - structTypeDeclaration; - for (j = 0; j < typeTag->genericArgumentCount; j += 1) - { - hashArray->elements[hashArray->count].types[j] = - genericTypeTags[j]; - } - hashArray->count += 1; - - hashEntry = &hashArray->elements[hashArray->count - 1]; + typeTag->genericArgumentCount, + hashArray, + typeHash); } return hashEntry->structDeclaration; @@ -688,9 +692,9 @@ static LLVMTypeRef LookupStructTypeByName(char *name) for (i = 0; i < structTypeDeclarationCount; i += 1) { - if (strcmp(structTypeDeclarations[i].name, name) == 0) + if (strcmp(structTypeDeclarations[i]->name, name) == 0) { - return structTypeDeclarations[i].structType; + return structTypeDeclarations[i]->structType; } } @@ -703,9 +707,9 @@ static StructTypeDeclaration *LookupStructDeclaration(LLVMTypeRef structType) for (i = 0; i < structTypeDeclarationCount; i += 1) { - if (structTypeDeclarations[i].structType == structType) + if (structTypeDeclarations[i]->structType == structType) { - return &structTypeDeclarations[i]; + return structTypeDeclarations[i]; } } @@ -724,18 +728,18 @@ static LLVMValueRef FindStructFieldPointer( for (i = 0; i < structTypeDeclarationCount; i += 1) { - if (structTypeDeclarations[i].structPointerType == structPointerType) + if (structTypeDeclarations[i]->structPointerType == structPointerType) { - for (j = 0; j < structTypeDeclarations[i].fieldCount; j += 1) + for (j = 0; j < structTypeDeclarations[i]->fieldCount; j += 1) { - if (strcmp(structTypeDeclarations[i].fields[j].name, name) == 0) + if (strcmp(structTypeDeclarations[i]->fields[j].name, name) == 0) { char *ptrName = strdup(name); ptrName = w_strcat(ptrName, "_ptr"); return LLVMBuildStructGEP( builder, structPointer, - structTypeDeclarations[i].fields[j].index, + structTypeDeclarations[i]->fields[j].index, ptrName); free(ptrName); } @@ -1175,29 +1179,29 @@ static LLVMValueRef LookupFunctionByType( for (i = 0; i < structTypeDeclarationCount; i += 1) { - if (structTypeDeclarations[i].structType == structType) + if (structTypeDeclarations[i]->structType == structType) { - for (j = 0; j < structTypeDeclarations[i].functionCount; j += 1) + for (j = 0; j < structTypeDeclarations[i]->functionCount; j += 1) { - if (strcmp(structTypeDeclarations[i].functions[j].name, name) == + if (strcmp(structTypeDeclarations[i]->functions[j].name, name) == 0) { *pReturnType = - structTypeDeclarations[i].functions[j].returnType; - *pStatic = structTypeDeclarations[i].functions[j].isStatic; - return structTypeDeclarations[i].functions[j].function; + structTypeDeclarations[i]->functions[j].returnType; + *pStatic = structTypeDeclarations[i]->functions[j].isStatic; + return structTypeDeclarations[i]->functions[j].function; } } - for (j = 0; j < structTypeDeclarations[i].genericFunctionCount; + for (j = 0; j < structTypeDeclarations[i]->genericFunctionCount; j += 1) { if (strcmp( - structTypeDeclarations[i].genericFunctions[j].name, + structTypeDeclarations[i]->genericFunctions[j].name, name) == 0) { return LookupGenericFunction( - &structTypeDeclarations[i].genericFunctions[j], + &structTypeDeclarations[i]->genericFunctions[j], functionCallExpression, pReturnType, pStatic); @@ -1221,29 +1225,29 @@ static LLVMValueRef LookupFunctionByPointerType( for (i = 0; i < structTypeDeclarationCount; i += 1) { - if (structTypeDeclarations[i].structPointerType == structPointerType) + if (structTypeDeclarations[i]->structPointerType == structPointerType) { - for (j = 0; j < structTypeDeclarations[i].functionCount; j += 1) + for (j = 0; j < structTypeDeclarations[i]->functionCount; j += 1) { - if (strcmp(structTypeDeclarations[i].functions[j].name, name) == + if (strcmp(structTypeDeclarations[i]->functions[j].name, name) == 0) { *pReturnType = - structTypeDeclarations[i].functions[j].returnType; - *pStatic = structTypeDeclarations[i].functions[j].isStatic; - return structTypeDeclarations[i].functions[j].function; + structTypeDeclarations[i]->functions[j].returnType; + *pStatic = structTypeDeclarations[i]->functions[j].isStatic; + return structTypeDeclarations[i]->functions[j].function; } } - for (j = 0; j < structTypeDeclarations[i].genericFunctionCount; + for (j = 0; j < structTypeDeclarations[i]->genericFunctionCount; j += 1) { if (strcmp( - structTypeDeclarations[i].genericFunctions[j].name, + structTypeDeclarations[i]->genericFunctions[j].name, name) == 0) { return LookupGenericFunction( - &structTypeDeclarations[i].genericFunctions[j], + &structTypeDeclarations[i]->genericFunctions[j], functionCallExpression, pReturnType, pStatic); @@ -1391,7 +1395,7 @@ static LLVMValueRef CompileFunctionCallExpression( &isStatic ); - free(typeTag); + //free(typeTag); } else {