This commit is contained in:
nub31
2025-06-14 18:24:10 +02:00
parent f23686eca1
commit a700f9be90
2 changed files with 16 additions and 11 deletions

View File

@@ -77,15 +77,15 @@ var objectFiles = new List<string>();
foreach (var compilationUnit in compilationUnits) foreach (var compilationUnit in compilationUnits)
{ {
var ssa = QBEGenerator.Generate(compilationUnit, definitionTable);
var asm = await QBE.Invoke(ssa);
var relativeFilePath = Path.GetRelativePath(Environment.CurrentDirectory, sourceTexts[compilationUnit].Path); var relativeFilePath = Path.GetRelativePath(Environment.CurrentDirectory, sourceTexts[compilationUnit].Path);
var outputPath = Path.Combine(BIN_INT_DIR, "program", relativeFilePath); var outputPath = Path.Combine(BIN_INT_DIR, "program", relativeFilePath);
var outputDirectory = Path.GetDirectoryName(outputPath); var outputDirectory = Path.GetDirectoryName(outputPath);
Debug.Assert(!string.IsNullOrWhiteSpace(outputDirectory)); Debug.Assert(!string.IsNullOrWhiteSpace(outputDirectory));
Directory.CreateDirectory(outputDirectory); Directory.CreateDirectory(outputDirectory);
var ssa = QBEGenerator.Generate(compilationUnit, definitionTable);
var asm = await QBE.Invoke(ssa);
var asmPath = Path.ChangeExtension(outputPath, "s"); var asmPath = Path.ChangeExtension(outputPath, "s");
await File.WriteAllTextAsync(asmPath, asm); await File.WriteAllTextAsync(asmPath, asm);

View File

@@ -86,18 +86,23 @@ public static class QBEGenerator
return $"@l{++_labelIndex}"; return $"@l{++_labelIndex}";
} }
private static string FuncName(IFuncSignature func) private static string FuncName(IFuncSignature funcDef)
{ {
return func switch return funcDef switch
{ {
ExternFuncDefinitionNode externFuncDefinition => $"${externFuncDefinition.CallName}", ExternFuncDefinitionNode externFuncDefinition => $"${externFuncDefinition.CallName}",
LocalFuncDefinitionNode localFuncDefinition => localFuncDefinition.Exported LocalFuncDefinitionNode localFuncDefinition => localFuncDefinition.Exported
? $"${localFuncDefinition.Name}" ? $"${localFuncDefinition.Name}"
: $"${localFuncDefinition.Namespace}_{localFuncDefinition.Name}", : $"${localFuncDefinition.Namespace}_{localFuncDefinition.Name}",
_ => throw new ArgumentOutOfRangeException(nameof(func)) _ => throw new ArgumentOutOfRangeException(nameof(funcDef))
}; };
} }
private static string StructName(StructDefinitionNode structDef)
{
return $":{structDef.Namespace}_{structDef.Name}";
}
private static string QBEStore(NubType type) private static string QBEStore(NubType type)
{ {
return type switch return type switch
@@ -349,7 +354,7 @@ public static class QBEGenerator
PrimitiveTypeKind.Bool => "w", PrimitiveTypeKind.Bool => "w",
_ => throw new UnreachableException() _ => throw new UnreachableException()
}, },
NubStructType structType => $":{structType.Namespace}_{structType.Name}", NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()),
NubFixedArrayType => "l", NubFixedArrayType => "l",
NubFuncType => "l", NubFuncType => "l",
_ => throw new NotSupportedException($"'{returnType}' type cannot be used as a function return type") _ => throw new NotSupportedException($"'{returnType}' type cannot be used as a function return type")
@@ -380,7 +385,7 @@ public static class QBEGenerator
PrimitiveTypeKind.Bool => "w", PrimitiveTypeKind.Bool => "w",
_ => throw new UnreachableException() _ => throw new UnreachableException()
}, },
NubStructType structType => $":{structType.Namespace}_{structType.Name}", NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()),
NubFixedArrayType => "l", NubFixedArrayType => "l",
NubFuncType => "l", NubFuncType => "l",
_ => throw new NotSupportedException($"'{parameter.Type}' type cannot be used as a function parameter type") _ => throw new NotSupportedException($"'{parameter.Type}' type cannot be used as a function parameter type")
@@ -439,7 +444,7 @@ public static class QBEGenerator
private static void GenerateStructDefinition(StructDefinitionNode structDefinition) private static void GenerateStructDefinition(StructDefinitionNode structDefinition)
{ {
_builder.Append($"type :{structDefinition.Namespace}_{structDefinition.Name} = {{ "); _builder.Append($"type {StructName(structDefinition)} = {{ ");
foreach (var structDefinitionField in structDefinition.Fields) foreach (var structDefinitionField in structDefinition.Fields)
{ {
var qbeType = structDefinitionField.Type switch var qbeType = structDefinitionField.Type switch
@@ -461,7 +466,7 @@ public static class QBEGenerator
PrimitiveTypeKind.Bool => "w", PrimitiveTypeKind.Bool => "w",
_ => throw new UnreachableException() _ => throw new UnreachableException()
}, },
NubStructType structType => $":{structType.Namespace}_{structType.Name}", NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()),
NubFixedArrayType fixedArrayType => $"b {SizeOf(fixedArrayType)}", NubFixedArrayType fixedArrayType => $"b {SizeOf(fixedArrayType)}",
NubFuncType => "l", NubFuncType => "l",
_ => throw new NotSupportedException($"'{structDefinitionField.Type}' type cannot be used in structs") _ => throw new NotSupportedException($"'{structDefinitionField.Type}' type cannot be used in structs")
@@ -1320,7 +1325,7 @@ public static class QBEGenerator
PrimitiveTypeKind.Bool => "w", PrimitiveTypeKind.Bool => "w",
_ => throw new ArgumentOutOfRangeException() _ => throw new ArgumentOutOfRangeException()
}, },
NubStructType structType => $":{structType.Namespace}_{structType.Name}", NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()),
NubFixedArrayType => "l", NubFixedArrayType => "l",
NubFuncType => "l", NubFuncType => "l",
_ => throw new NotSupportedException($"'{parameter.Type}' type cannot be used in function calls") _ => throw new NotSupportedException($"'{parameter.Type}' type cannot be used in function calls")