...
This commit is contained in:
@@ -1,23 +1,23 @@
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text;
|
||||
using NubLang.Syntax.Binding;
|
||||
using NubLang.Syntax.Binding.Node;
|
||||
using NubLang.Syntax.Tokenization;
|
||||
using NubLang.Tokenization;
|
||||
using NubLang.TypeChecking;
|
||||
using NubLang.TypeChecking.Node;
|
||||
|
||||
namespace NubLang.Generation.QBE;
|
||||
|
||||
public partial class QBEGenerator
|
||||
{
|
||||
private readonly BoundSyntaxTree _syntaxTree;
|
||||
private readonly BoundDefinitionTable _definitionTable;
|
||||
private readonly TypedSyntaxTree _syntaxTree;
|
||||
private readonly TypedDefinitionTable _definitionTable;
|
||||
private readonly QBEWriter _writer;
|
||||
|
||||
private readonly List<CStringLiteral> _cStringLiterals = [];
|
||||
private readonly List<StringLiteral> _stringLiterals = [];
|
||||
private readonly Stack<string> _breakLabels = [];
|
||||
private readonly Stack<string> _continueLabels = [];
|
||||
private readonly Queue<(BoundArrowFunc Func, string Name)> _arrowFunctions = [];
|
||||
private readonly Queue<(ArrowFunc Func, string Name)> _arrowFunctions = [];
|
||||
private readonly Stack<Scope> _scopes = [];
|
||||
private int _tmpIndex;
|
||||
private int _labelIndex;
|
||||
@@ -28,7 +28,7 @@ public partial class QBEGenerator
|
||||
|
||||
private Scope Scope => _scopes.Peek();
|
||||
|
||||
public QBEGenerator(BoundSyntaxTree syntaxTree, BoundDefinitionTable definitionTable)
|
||||
public QBEGenerator(TypedSyntaxTree syntaxTree, TypedDefinitionTable definitionTable)
|
||||
{
|
||||
_syntaxTree = syntaxTree;
|
||||
_definitionTable = definitionTable;
|
||||
@@ -62,7 +62,7 @@ public partial class QBEGenerator
|
||||
_writer.NewLine();
|
||||
}
|
||||
|
||||
foreach (var funcDef in _syntaxTree.Definitions.OfType<BoundLocalFunc>())
|
||||
foreach (var funcDef in _syntaxTree.Definitions.OfType<LocalFunc>())
|
||||
{
|
||||
EmitFuncDefinition(LocalFuncName(funcDef), funcDef.Signature.Parameters, funcDef.Signature.ReturnType, funcDef.Body);
|
||||
_writer.NewLine();
|
||||
@@ -191,21 +191,21 @@ public partial class QBEGenerator
|
||||
return size;
|
||||
}
|
||||
|
||||
private bool EmitTryMoveInto(BoundExpression source, string destinationPointer)
|
||||
private bool EmitTryMoveInto(Expression source, string destinationPointer)
|
||||
{
|
||||
switch (source)
|
||||
{
|
||||
case BoundArrayInitializer arrayInitializer:
|
||||
case ArrayInitializer arrayInitializer:
|
||||
{
|
||||
EmitStore(source.Type, EmitUnwrap(EmitArrayInitializer(arrayInitializer)), destinationPointer);
|
||||
return true;
|
||||
}
|
||||
case BoundStructInitializer structInitializer:
|
||||
case StructInitializer structInitializer:
|
||||
{
|
||||
EmitStructInitializer(structInitializer, destinationPointer);
|
||||
return true;
|
||||
}
|
||||
case BoundLiteral { Kind: LiteralKind.String } literal:
|
||||
case Literal { Kind: LiteralKind.String } literal:
|
||||
{
|
||||
EmitStore(source.Type, EmitUnwrap(EmitLiteral(literal)), destinationPointer);
|
||||
return true;
|
||||
@@ -215,7 +215,7 @@ public partial class QBEGenerator
|
||||
return false;
|
||||
}
|
||||
|
||||
private void EmitCopyIntoOrInitialize(BoundExpression source, string destinationPointer)
|
||||
private void EmitCopyIntoOrInitialize(Expression source, string destinationPointer)
|
||||
{
|
||||
// If the source is a value which is not used yet such as an array/struct initializer or literal, we can skip copying
|
||||
if (EmitTryMoveInto(source, destinationPointer))
|
||||
@@ -253,13 +253,13 @@ public partial class QBEGenerator
|
||||
}
|
||||
}
|
||||
|
||||
private bool EmitTryCreateWithoutCopy(BoundExpression source, [NotNullWhen(true)] out string? destination)
|
||||
private bool EmitTryCreateWithoutCopy(Expression source, [NotNullWhen(true)] out string? destination)
|
||||
{
|
||||
switch (source)
|
||||
{
|
||||
case BoundArrayInitializer:
|
||||
case BoundStructInitializer:
|
||||
case BoundLiteral { Kind: LiteralKind.String }:
|
||||
case ArrayInitializer:
|
||||
case StructInitializer:
|
||||
case Literal { Kind: LiteralKind.String }:
|
||||
{
|
||||
destination = EmitUnwrap(EmitExpression(source));
|
||||
return true;
|
||||
@@ -270,7 +270,7 @@ public partial class QBEGenerator
|
||||
return false;
|
||||
}
|
||||
|
||||
private string EmitCreateCopyOrInitialize(BoundExpression source)
|
||||
private string EmitCreateCopyOrInitialize(Expression source)
|
||||
{
|
||||
// If the source is a value which is not used yet such as an array/struct initializer or literal, we can skip copying
|
||||
if (EmitTryCreateWithoutCopy(source, out var uncopiedValue))
|
||||
@@ -328,7 +328,7 @@ public partial class QBEGenerator
|
||||
return "l";
|
||||
}
|
||||
|
||||
private void EmitFuncDefinition(string name, IReadOnlyList<BoundFuncParameter> parameters, NubType returnType, BoundBlock body)
|
||||
private void EmitFuncDefinition(string name, IReadOnlyList<FuncParameter> parameters, NubType returnType, Block body)
|
||||
{
|
||||
_labelIndex = 0;
|
||||
_tmpIndex = 0;
|
||||
@@ -360,7 +360,7 @@ public partial class QBEGenerator
|
||||
EmitBlock(body, scope);
|
||||
|
||||
// Implicit return for void functions if no explicit return has been set
|
||||
if (returnType is NubVoidType && body.Statements is [.., not BoundReturn])
|
||||
if (returnType is NubVoidType && body.Statements is [.., not Return])
|
||||
{
|
||||
if (returnType is NubVoidType)
|
||||
{
|
||||
@@ -371,7 +371,7 @@ public partial class QBEGenerator
|
||||
_writer.EndFunction();
|
||||
}
|
||||
|
||||
private void EmitStructDefinition(BoundStruct structDef)
|
||||
private void EmitStructDefinition(Struct structDef)
|
||||
{
|
||||
_writer.WriteLine($"type {CustomTypeName(structDef.Name)} = {{ ");
|
||||
|
||||
@@ -392,7 +392,7 @@ public partial class QBEGenerator
|
||||
_writer.WriteLine("}");
|
||||
return;
|
||||
|
||||
string StructDefQBEType(BoundStructField field)
|
||||
string StructDefQBEType(StructField field)
|
||||
{
|
||||
if (field.Type.IsSimpleType(out var simpleType, out var complexType))
|
||||
{
|
||||
@@ -417,7 +417,7 @@ public partial class QBEGenerator
|
||||
}
|
||||
}
|
||||
|
||||
private void EmitTraitVTable(BoundTrait traitDef)
|
||||
private void EmitTraitVTable(Trait traitDef)
|
||||
{
|
||||
_writer.WriteLine($"type {CustomTypeName(traitDef.Name)} = {{");
|
||||
|
||||
@@ -429,7 +429,7 @@ public partial class QBEGenerator
|
||||
_writer.WriteLine("}");
|
||||
}
|
||||
|
||||
private void EmitBlock(BoundBlock block, Scope? scope = null)
|
||||
private void EmitBlock(Block block, Scope? scope = null)
|
||||
{
|
||||
_scopes.Push(scope ?? Scope.SubScope());
|
||||
|
||||
@@ -456,7 +456,7 @@ public partial class QBEGenerator
|
||||
};
|
||||
}
|
||||
|
||||
private int OffsetOf(BoundStruct structDefinition, string member)
|
||||
private int OffsetOf(Struct structDefinition, string member)
|
||||
{
|
||||
var offset = 0;
|
||||
|
||||
@@ -498,12 +498,12 @@ public partial class QBEGenerator
|
||||
return $"$string{++_stringLiteralIndex}";
|
||||
}
|
||||
|
||||
private string LocalFuncName(BoundLocalFunc funcDef)
|
||||
private string LocalFuncName(LocalFunc funcDef)
|
||||
{
|
||||
return $"${funcDef.Name}";
|
||||
}
|
||||
|
||||
private string ExternFuncName(BoundExternFunc funcDef)
|
||||
private string ExternFuncName(ExternFunc funcDef)
|
||||
{
|
||||
return $"${funcDef.CallName}";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user