From 92ebefd3d6fa598436a9443072f55dcd1c758360 Mon Sep 17 00:00:00 2001 From: nub31 Date: Sun, 6 Jul 2025 00:29:55 +0200 Subject: [PATCH] ... --- src/compiler/Generation/QBE/QBEGenerator.cs | 140 ++++++++++---------- src/compiler/Syntax/DefinitionTable.cs | 10 ++ 2 files changed, 80 insertions(+), 70 deletions(-) diff --git a/src/compiler/Generation/QBE/QBEGenerator.cs b/src/compiler/Generation/QBE/QBEGenerator.cs index a0f5b72..ab59939 100644 --- a/src/compiler/Generation/QBE/QBEGenerator.cs +++ b/src/compiler/Generation/QBE/QBEGenerator.cs @@ -53,17 +53,17 @@ public static class QBEGenerator _implFuncNameIndex = 0; _codeIsReachable = true; - // foreach (var structDef in _definitionTable.GetStructs()) - // { - // EmitStructDefinition(structDef); - // _writer.NewLine(); - // } - // - // foreach (var trait in _definitionTable.GetTraits()) - // { - // EmitTraitVTable(trait); - // _writer.NewLine(); - // } + foreach (var structDef in _definitionTable.GetStructs()) + { + EmitStructDefinition(structDef); + _writer.NewLine(); + } + + foreach (var trait in _definitionTable.GetTraits()) + { + EmitTraitVTable(trait); + _writer.NewLine(); + } foreach (var funcDef in _syntaxTree.TopLevelNodes.OfType()) { @@ -574,65 +574,65 @@ public static class QBEGenerator _writer.EndFunction(); } - // private static void EmitStructDefinition(BoundStructNode structDef) - // { - // var structType = new BoundNubCustomType(structDef.Namespace, structDef.Name); - // _writer.WriteLine($"type {CustomTypeName(structType)} = {{ "); - // - // var types = new Dictionary(); - // - // foreach (var field in structDef.Fields) - // { - // types.Add(field.Name, StructDefQBEType(field)); - // } - // - // var longest = types.Values.Max(x => x.Length); - // foreach (var (name, type) in types) - // { - // var padding = longest - type.Length; - // _writer.Indented($"{type},{new string(' ', padding)} # {name}"); - // } - // - // _writer.WriteLine("}"); - // return; - // - // string StructDefQBEType(BoundStructFieldNode field) - // { - // return field.Type switch - // { - // BoundNubCustomType customType => CustomTypeName(customType), - // BoundNubComplexType => "l", - // BoundNubSimpleType simpleType => simpleType switch - // { - // BoundNubPointerType or BoundNubFuncType => "l", - // BoundNubPrimitiveType primitiveType => primitiveType.Kind switch - // { - // PrimitiveTypeKind.I64 or PrimitiveTypeKind.U64 => "l", - // PrimitiveTypeKind.I32 or PrimitiveTypeKind.U32 => "w", - // PrimitiveTypeKind.I16 or PrimitiveTypeKind.U16 => "h", - // PrimitiveTypeKind.I8 or PrimitiveTypeKind.U8 or PrimitiveTypeKind.Bool => "b", - // PrimitiveTypeKind.F64 => "d", - // PrimitiveTypeKind.F32 => "s", - // _ => throw new ArgumentOutOfRangeException() - // }, - // _ => throw new NotSupportedException($"'{field.Type}' type cannot be used in structs") - // }, - // _ => throw new UnreachableException() - // }; - // } - // } - // - // private static void EmitTraitVTable(BoundTraitNode traitDef) - // { - // _writer.WriteLine($"type {CustomTypeName(new BoundNubCustomType(traitDef.Namespace, traitDef.Name))} = {{"); - // - // foreach (var func in traitDef.Functions) - // { - // _writer.Indented($"l, # func {func.Name}({string.Join(", ", func.Parameters.Select(x => $"{x.Name}: {x.Type}"))}): {func.ReturnType}"); - // } - // - // _writer.WriteLine("}"); - // } + private static void EmitStructDefinition(BoundStructNode structDef) + { + _writer.WriteLine($"type {StructTypeName(new BoundNubStructType(structDef.Namespace, structDef.Name))} = {{ "); + + var types = new Dictionary(); + + foreach (var field in structDef.Fields) + { + types.Add(field.Name, StructDefQBEType(field)); + } + + var longest = types.Values.Max(x => x.Length); + foreach (var (name, type) in types) + { + var padding = longest - type.Length; + _writer.Indented($"{type},{new string(' ', padding)} # {name}"); + } + + _writer.WriteLine("}"); + return; + + string StructDefQBEType(BoundStructFieldNode field) + { + return field.Type switch + { + BoundNubStructType structType => StructTypeName(structType), + BoundNubTraitType traitType => TraitTypeName(traitType), + BoundNubComplexType => "l", + BoundNubSimpleType simpleType => simpleType switch + { + BoundNubPointerType or BoundNubFuncType => "l", + BoundNubPrimitiveType primitiveType => primitiveType.Kind switch + { + PrimitiveTypeKind.I64 or PrimitiveTypeKind.U64 => "l", + PrimitiveTypeKind.I32 or PrimitiveTypeKind.U32 => "w", + PrimitiveTypeKind.I16 or PrimitiveTypeKind.U16 => "h", + PrimitiveTypeKind.I8 or PrimitiveTypeKind.U8 or PrimitiveTypeKind.Bool => "b", + PrimitiveTypeKind.F64 => "d", + PrimitiveTypeKind.F32 => "s", + _ => throw new ArgumentOutOfRangeException() + }, + _ => throw new NotSupportedException($"'{field.Type}' type cannot be used in structs") + }, + _ => throw new UnreachableException() + }; + } + } + + private static void EmitTraitVTable(BoundTraitNode traitDef) + { + _writer.WriteLine($"type {TraitTypeName(new BoundNubTraitType(traitDef.Namespace, traitDef.Name))} = {{"); + + foreach (var func in traitDef.Functions) + { + _writer.Indented($"l, # func {func.Name}({string.Join(", ", func.Parameters.Select(x => $"{x.Name}: {x.Type}"))}): {func.ReturnType}"); + } + + _writer.WriteLine("}"); + } private static void EmitStatement(BoundStatementNode statement) { diff --git a/src/compiler/Syntax/DefinitionTable.cs b/src/compiler/Syntax/DefinitionTable.cs index c1b9d6d..d21843d 100644 --- a/src/compiler/Syntax/DefinitionTable.cs +++ b/src/compiler/Syntax/DefinitionTable.cs @@ -121,4 +121,14 @@ public sealed class BoundDefinitionTable { return trait.Functions.First(x => x.Name == name); } + + public IEnumerable GetStructs() + { + return _topLevelNodes.OfType(); + } + + public IEnumerable GetTraits() + { + return _topLevelNodes.OfType(); + } } \ No newline at end of file