From a700f9be90c7dae11107f18fca7a488ad794cfda Mon Sep 17 00:00:00 2001 From: nub31 Date: Sat, 14 Jun 2025 18:24:10 +0200 Subject: [PATCH] ... --- src/CLI/Program.cs | 6 +++--- src/Generation/QBE/QBEGenerator.cs | 21 +++++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/CLI/Program.cs b/src/CLI/Program.cs index 24cff8c..3094ef4 100644 --- a/src/CLI/Program.cs +++ b/src/CLI/Program.cs @@ -77,15 +77,15 @@ var objectFiles = new List(); 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 outputPath = Path.Combine(BIN_INT_DIR, "program", relativeFilePath); var outputDirectory = Path.GetDirectoryName(outputPath); Debug.Assert(!string.IsNullOrWhiteSpace(outputDirectory)); Directory.CreateDirectory(outputDirectory); + + var ssa = QBEGenerator.Generate(compilationUnit, definitionTable); + var asm = await QBE.Invoke(ssa); var asmPath = Path.ChangeExtension(outputPath, "s"); await File.WriteAllTextAsync(asmPath, asm); diff --git a/src/Generation/QBE/QBEGenerator.cs b/src/Generation/QBE/QBEGenerator.cs index 09fc689..0050546 100644 --- a/src/Generation/QBE/QBEGenerator.cs +++ b/src/Generation/QBE/QBEGenerator.cs @@ -86,18 +86,23 @@ public static class QBEGenerator 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}", LocalFuncDefinitionNode localFuncDefinition => localFuncDefinition.Exported ? $"${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) { return type switch @@ -349,7 +354,7 @@ public static class QBEGenerator PrimitiveTypeKind.Bool => "w", _ => throw new UnreachableException() }, - NubStructType structType => $":{structType.Namespace}_{structType.Name}", + NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()), NubFixedArrayType => "l", NubFuncType => "l", _ => throw new NotSupportedException($"'{returnType}' type cannot be used as a function return type") @@ -380,7 +385,7 @@ public static class QBEGenerator PrimitiveTypeKind.Bool => "w", _ => throw new UnreachableException() }, - NubStructType structType => $":{structType.Namespace}_{structType.Name}", + NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()), NubFixedArrayType => "l", NubFuncType => "l", _ => 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) { - _builder.Append($"type :{structDefinition.Namespace}_{structDefinition.Name} = {{ "); + _builder.Append($"type {StructName(structDefinition)} = {{ "); foreach (var structDefinitionField in structDefinition.Fields) { var qbeType = structDefinitionField.Type switch @@ -461,7 +466,7 @@ public static class QBEGenerator PrimitiveTypeKind.Bool => "w", _ => throw new UnreachableException() }, - NubStructType structType => $":{structType.Namespace}_{structType.Name}", + NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()), NubFixedArrayType fixedArrayType => $"b {SizeOf(fixedArrayType)}", NubFuncType => "l", _ => throw new NotSupportedException($"'{structDefinitionField.Type}' type cannot be used in structs") @@ -1320,7 +1325,7 @@ public static class QBEGenerator PrimitiveTypeKind.Bool => "w", _ => throw new ArgumentOutOfRangeException() }, - NubStructType structType => $":{structType.Namespace}_{structType.Name}", + NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()), NubFixedArrayType => "l", NubFuncType => "l", _ => throw new NotSupportedException($"'{parameter.Type}' type cannot be used in function calls")