This commit is contained in:
nub31
2025-09-12 16:58:09 +02:00
parent c29d056ebc
commit 387ac34137
12 changed files with 212 additions and 278 deletions

View File

@@ -1,8 +1,8 @@
using System.Diagnostics;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using NubLang.Tokenization;
using NubLang.TypeChecking;
using NubLang.TypeChecking.Node;
namespace NubLang.Generation.QBE;
@@ -11,7 +11,8 @@ public class QBEGenerator
{
private readonly QBEWriter _writer;
private readonly IReadOnlyList<DefinitionNode> _definitions;
private readonly IReadOnlyDictionary<string, Module> _moduleSignatures;
private readonly ImmutableHashSet<StructTypeNode> _structTypes;
private readonly ImmutableHashSet<InterfaceTypeNode> _interfaceTypes;
private readonly List<CStringLiteral> _cStringLiterals = [];
private readonly List<StringLiteral> _stringLiterals = [];
@@ -23,10 +24,11 @@ public class QBEGenerator
private int _stringLiteralIndex;
private bool _codeIsReachable = true;
public QBEGenerator(IReadOnlyList<DefinitionNode> definitions, IReadOnlyDictionary<string, Module> moduleSignatures)
public QBEGenerator(IReadOnlyList<DefinitionNode> definitions, ImmutableHashSet<StructTypeNode> structTypes, ImmutableHashSet<InterfaceTypeNode> interfaceTypes)
{
_definitions = definitions;
_moduleSignatures = moduleSignatures;
_structTypes = structTypes;
_interfaceTypes = interfaceTypes;
_writer = new QBEWriter();
}
@@ -42,13 +44,10 @@ public class QBEGenerator
_stringLiteralIndex = 0;
_codeIsReachable = true;
foreach (var (module, signature) in _moduleSignatures)
foreach (var structType in _structTypes)
{
foreach (var structType in signature.ExportedStructTypes)
{
EmitStructType(module, structType.StructType);
_writer.NewLine();
}
EmitStructType(structType);
_writer.NewLine();
}
foreach (var structDef in _definitions.OfType<StructNode>())
@@ -386,7 +385,7 @@ public class QBEGenerator
_writer.Write(FuncQBETypeName(funcDef.Signature.ReturnType) + ' ');
}
_writer.Write(FuncName(funcDef.Module, funcDef.Name));
_writer.Write(FuncName(funcDef.Module, funcDef.Name, funcDef.ExternSymbol));
_writer.Write("(");
foreach (var parameter in funcDef.Signature.Parameters)
@@ -453,9 +452,9 @@ public class QBEGenerator
}
}
private void EmitStructType(string module, StructTypeNode structType)
private void EmitStructType(StructTypeNode structType)
{
_writer.WriteLine($"type {StructTypeName(module, structType.Name)} = {{ ");
_writer.WriteLine($"type {StructTypeName(structType.Module, structType.Name)} = {{ ");
foreach (var field in structType.Fields)
{
@@ -658,7 +657,7 @@ public class QBEGenerator
private string EmitFuncIdentifier(FuncIdentifierNode funcIdent)
{
return FuncName(funcIdent.Module, funcIdent.Name);
return FuncName(funcIdent.Module, funcIdent.Name, funcIdent.ExternSymbol);
}
private string EmitVariableIdentifier(VariableIdentifierNode variableIdent)
@@ -1391,10 +1390,9 @@ public class QBEGenerator
return $"$string{++_stringLiteralIndex}";
}
private string FuncName(string module, string name)
private string FuncName(string module, string name, string? externSymbol)
{
var function = _moduleSignatures[module].AllFunctions.First(x => x.Name == name);
var symbol = function.ExternSymbol ?? $"{module}.{name}";
var symbol = externSymbol ?? $"{module}.{name}";
return "$" + symbol;
}