fix some generic recursion issues
parent
ff5011b813
commit
05b3707258
140
src/codegen.c
140
src/codegen.c
|
@ -111,7 +111,7 @@ struct StructTypeDeclaration
|
||||||
uint32_t genericFunctionCount;
|
uint32_t genericFunctionCount;
|
||||||
};
|
};
|
||||||
|
|
||||||
StructTypeDeclaration *structTypeDeclarations;
|
StructTypeDeclaration **structTypeDeclarations;
|
||||||
uint32_t structTypeDeclarationCount;
|
uint32_t structTypeDeclarationCount;
|
||||||
|
|
||||||
typedef struct MonomorphizedGenericStructHashEntry
|
typedef struct MonomorphizedGenericStructHashEntry
|
||||||
|
@ -401,9 +401,9 @@ static LLVMTypeRef LookupCustomType(char *name)
|
||||||
|
|
||||||
for (i = 0; i < structTypeDeclarationCount; i += 1)
|
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;
|
uint32_t index = structTypeDeclarationCount;
|
||||||
structTypeDeclarations = realloc(
|
structTypeDeclarations = realloc(
|
||||||
structTypeDeclarations,
|
structTypeDeclarations,
|
||||||
sizeof(StructTypeDeclaration) * (structTypeDeclarationCount + 1));
|
sizeof(StructTypeDeclaration*) * (structTypeDeclarationCount + 1));
|
||||||
structTypeDeclarations[index].module = module;
|
structTypeDeclarations[index] = malloc(sizeof(StructTypeDeclaration));
|
||||||
structTypeDeclarations[index].structType = wStructType;
|
structTypeDeclarations[index]->module = module;
|
||||||
structTypeDeclarations[index].structPointerType = wStructPointerType;
|
structTypeDeclarations[index]->structType = wStructType;
|
||||||
structTypeDeclarations[index].name = strdup(name);
|
structTypeDeclarations[index]->structPointerType = wStructPointerType;
|
||||||
structTypeDeclarations[index].fields = NULL;
|
structTypeDeclarations[index]->name = strdup(name);
|
||||||
structTypeDeclarations[index].fieldCount = 0;
|
structTypeDeclarations[index]->fields = NULL;
|
||||||
structTypeDeclarations[index].functions = NULL;
|
structTypeDeclarations[index]->fieldCount = 0;
|
||||||
structTypeDeclarations[index].functionCount = 0;
|
structTypeDeclarations[index]->functions = NULL;
|
||||||
structTypeDeclarations[index].genericFunctions = NULL;
|
structTypeDeclarations[index]->functionCount = 0;
|
||||||
structTypeDeclarations[index].genericFunctionCount = 0;
|
structTypeDeclarations[index]->genericFunctions = NULL;
|
||||||
|
structTypeDeclarations[index]->genericFunctionCount = 0;
|
||||||
|
|
||||||
structTypeDeclarationCount += 1;
|
structTypeDeclarationCount += 1;
|
||||||
|
|
||||||
return &structTypeDeclarations[index];
|
return structTypeDeclarations[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
static StructTypeDeclaration *CompileMonomorphizedGenericStruct(
|
static MonomorphizedGenericStructHashEntry *CompileMonomorphizedGenericStruct(
|
||||||
GenericStructTypeDeclaration *genericStructTypeDeclaration,
|
GenericStructTypeDeclaration *genericStructTypeDeclaration,
|
||||||
TypeTag **genericArgumentTypes,
|
TypeTag **genericArgumentTypes,
|
||||||
uint32_t genericArgumentTypeCount)
|
uint32_t genericArgumentTypeCount,
|
||||||
|
MonomorphizedGenericStructHashArray *hashArray,
|
||||||
|
uint64_t typeHash)
|
||||||
{
|
{
|
||||||
uint32_t i = 0;
|
uint32_t i = 0;
|
||||||
uint32_t fieldCount = 0;
|
uint32_t fieldCount = 0;
|
||||||
|
@ -486,6 +489,25 @@ static StructTypeDeclaration *CompileMonomorphizedGenericStruct(
|
||||||
|
|
||||||
free(structName);
|
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 */
|
/* first build the structure def */
|
||||||
for (i = 0; i < declarationCount; i += 1)
|
for (i = 0; i < declarationCount; i += 1)
|
||||||
{
|
{
|
||||||
|
@ -528,7 +550,7 @@ static StructTypeDeclaration *CompileMonomorphizedGenericStruct(
|
||||||
|
|
||||||
PopScopeFrame(scope);
|
PopScopeFrame(scope);
|
||||||
|
|
||||||
return declaration;
|
return &hashArray->elements[hashArray->count - 1];;
|
||||||
}
|
}
|
||||||
|
|
||||||
static StructTypeDeclaration *LookupGenericStructType(
|
static StructTypeDeclaration *LookupGenericStructType(
|
||||||
|
@ -578,38 +600,20 @@ static StructTypeDeclaration *LookupGenericStructType(
|
||||||
|
|
||||||
if (match)
|
if (match)
|
||||||
{
|
{
|
||||||
hashEntry = &hashArray->elements[i];
|
hashEntry = &hashArray->elements[j];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hashEntry == NULL)
|
if (hashEntry == NULL)
|
||||||
{
|
{
|
||||||
StructTypeDeclaration *structTypeDeclaration =
|
hashEntry =
|
||||||
CompileMonomorphizedGenericStruct(
|
CompileMonomorphizedGenericStruct(
|
||||||
&genericStructTypeDeclarations[i],
|
&genericStructTypeDeclarations[i],
|
||||||
genericTypeTags,
|
genericTypeTags,
|
||||||
typeTag->genericArgumentCount);
|
typeTag->genericArgumentCount,
|
||||||
|
hashArray,
|
||||||
hashArray->elements = realloc(
|
typeHash);
|
||||||
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];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return hashEntry->structDeclaration;
|
return hashEntry->structDeclaration;
|
||||||
|
@ -688,9 +692,9 @@ static LLVMTypeRef LookupStructTypeByName(char *name)
|
||||||
|
|
||||||
for (i = 0; i < structTypeDeclarationCount; i += 1)
|
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)
|
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)
|
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);
|
char *ptrName = strdup(name);
|
||||||
ptrName = w_strcat(ptrName, "_ptr");
|
ptrName = w_strcat(ptrName, "_ptr");
|
||||||
return LLVMBuildStructGEP(
|
return LLVMBuildStructGEP(
|
||||||
builder,
|
builder,
|
||||||
structPointer,
|
structPointer,
|
||||||
structTypeDeclarations[i].fields[j].index,
|
structTypeDeclarations[i]->fields[j].index,
|
||||||
ptrName);
|
ptrName);
|
||||||
free(ptrName);
|
free(ptrName);
|
||||||
}
|
}
|
||||||
|
@ -1175,29 +1179,29 @@ static LLVMValueRef LookupFunctionByType(
|
||||||
|
|
||||||
for (i = 0; i < structTypeDeclarationCount; i += 1)
|
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)
|
0)
|
||||||
{
|
{
|
||||||
*pReturnType =
|
*pReturnType =
|
||||||
structTypeDeclarations[i].functions[j].returnType;
|
structTypeDeclarations[i]->functions[j].returnType;
|
||||||
*pStatic = structTypeDeclarations[i].functions[j].isStatic;
|
*pStatic = structTypeDeclarations[i]->functions[j].isStatic;
|
||||||
return structTypeDeclarations[i].functions[j].function;
|
return structTypeDeclarations[i]->functions[j].function;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j = 0; j < structTypeDeclarations[i].genericFunctionCount;
|
for (j = 0; j < structTypeDeclarations[i]->genericFunctionCount;
|
||||||
j += 1)
|
j += 1)
|
||||||
{
|
{
|
||||||
if (strcmp(
|
if (strcmp(
|
||||||
structTypeDeclarations[i].genericFunctions[j].name,
|
structTypeDeclarations[i]->genericFunctions[j].name,
|
||||||
name) == 0)
|
name) == 0)
|
||||||
{
|
{
|
||||||
return LookupGenericFunction(
|
return LookupGenericFunction(
|
||||||
&structTypeDeclarations[i].genericFunctions[j],
|
&structTypeDeclarations[i]->genericFunctions[j],
|
||||||
functionCallExpression,
|
functionCallExpression,
|
||||||
pReturnType,
|
pReturnType,
|
||||||
pStatic);
|
pStatic);
|
||||||
|
@ -1221,29 +1225,29 @@ static LLVMValueRef LookupFunctionByPointerType(
|
||||||
|
|
||||||
for (i = 0; i < structTypeDeclarationCount; i += 1)
|
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)
|
0)
|
||||||
{
|
{
|
||||||
*pReturnType =
|
*pReturnType =
|
||||||
structTypeDeclarations[i].functions[j].returnType;
|
structTypeDeclarations[i]->functions[j].returnType;
|
||||||
*pStatic = structTypeDeclarations[i].functions[j].isStatic;
|
*pStatic = structTypeDeclarations[i]->functions[j].isStatic;
|
||||||
return structTypeDeclarations[i].functions[j].function;
|
return structTypeDeclarations[i]->functions[j].function;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j = 0; j < structTypeDeclarations[i].genericFunctionCount;
|
for (j = 0; j < structTypeDeclarations[i]->genericFunctionCount;
|
||||||
j += 1)
|
j += 1)
|
||||||
{
|
{
|
||||||
if (strcmp(
|
if (strcmp(
|
||||||
structTypeDeclarations[i].genericFunctions[j].name,
|
structTypeDeclarations[i]->genericFunctions[j].name,
|
||||||
name) == 0)
|
name) == 0)
|
||||||
{
|
{
|
||||||
return LookupGenericFunction(
|
return LookupGenericFunction(
|
||||||
&structTypeDeclarations[i].genericFunctions[j],
|
&structTypeDeclarations[i]->genericFunctions[j],
|
||||||
functionCallExpression,
|
functionCallExpression,
|
||||||
pReturnType,
|
pReturnType,
|
||||||
pStatic);
|
pStatic);
|
||||||
|
@ -1391,7 +1395,7 @@ static LLVMValueRef CompileFunctionCallExpression(
|
||||||
&isStatic
|
&isStatic
|
||||||
);
|
);
|
||||||
|
|
||||||
free(typeTag);
|
//free(typeTag);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue