Node postfix for nodes

This commit is contained in:
nub31
2025-07-24 18:46:32 +02:00
parent 85297fc364
commit 5e23da61f0
15 changed files with 371 additions and 376 deletions

View File

@@ -1,51 +1,50 @@
using System.Diagnostics;
using System.Globalization;
using NubLang.Tokenization;
using NubLang.TypeChecking;
using NubLang.TypeChecking.Node;
namespace NubLang.Generation.QBE;
public partial class QBEGenerator
{
private Val EmitExpression(Expression expression)
private Val EmitExpression(ExpressionNode expression)
{
return expression switch
{
ArrayInitializer arrayInitializer => EmitArrayInitializer(arrayInitializer),
StructInitializer structInitializer => EmitStructInitializer(structInitializer),
AddressOf addressOf => EmitAddressOf(addressOf),
Dereference dereference => EmitDereference(dereference),
ArrowFunc arrowFunc => EmitArrowFunc(arrowFunc),
BinaryExpression binaryExpression => EmitBinaryExpression(binaryExpression),
FuncCall funcCallExpression => EmitFuncCall(funcCallExpression),
ExternFuncIdent externFuncIdent => EmitExternFuncIdent(externFuncIdent),
LocalFuncIdent localFuncIdent => EmitLocalFuncIdent(localFuncIdent),
VariableIdent variableIdent => EmitVariableIdent(variableIdent),
Literal literal => EmitLiteral(literal),
UnaryExpression unaryExpression => EmitUnaryExpression(unaryExpression),
StructFieldAccess structFieldAccess => EmitStructFieldAccess(structFieldAccess),
InterfaceFuncAccess traitFuncAccess => EmitTraitFuncAccess(traitFuncAccess),
ArrayIndexAccess arrayIndex => EmitArrayIndexAccess(arrayIndex),
ArrayInitializerNode arrayInitializer => EmitArrayInitializer(arrayInitializer),
StructInitializerNode structInitializer => EmitStructInitializer(structInitializer),
AddressOfNode addressOf => EmitAddressOf(addressOf),
DereferenceNode dereference => EmitDereference(dereference),
ArrowFuncNode arrowFunc => EmitArrowFunc(arrowFunc),
BinaryExpressionNode binaryExpression => EmitBinaryExpression(binaryExpression),
FuncCallNode funcCallExpression => EmitFuncCall(funcCallExpression),
ExternFuncIdentNode externFuncIdent => EmitExternFuncIdent(externFuncIdent),
LocalFuncIdentNode localFuncIdent => EmitLocalFuncIdent(localFuncIdent),
VariableIdentNode variableIdent => EmitVariableIdent(variableIdent),
LiteralNode literal => EmitLiteral(literal),
UnaryExpressionNode unaryExpression => EmitUnaryExpression(unaryExpression),
StructFieldAccessNode structFieldAccess => EmitStructFieldAccess(structFieldAccess),
InterfaceFuncAccessNode traitFuncAccess => EmitTraitFuncAccess(traitFuncAccess),
ArrayIndexAccessNode arrayIndex => EmitArrayIndexAccess(arrayIndex),
_ => throw new ArgumentOutOfRangeException(nameof(expression))
};
}
private Val EmitArrowFunc(ArrowFunc arrowFunc)
private Val EmitArrowFunc(ArrowFuncNode arrowFunc)
{
var name = $"$arrow_func{++_arrowFuncIndex}";
_arrowFunctions.Enqueue((arrowFunc, name));
return new Val(name, arrowFunc.Type, ValKind.Direct);
}
private Val EmitArrayIndexAccess(ArrayIndexAccess arrayIndexAccess)
private Val EmitArrayIndexAccess(ArrayIndexAccessNode arrayIndexAccess)
{
var array = EmitUnwrap(EmitExpression(arrayIndexAccess.Target));
var index = EmitUnwrap(EmitExpression(arrayIndexAccess.Index));
EmitArraysCheck(array, index);
var elementType = ((NubArrayType)arrayIndexAccess.Target.Type).ElementType;
var elementType = ((ArrayTypeNode)arrayIndexAccess.Target.Type).ElementType;
var pointer = TmpName();
_writer.Indented($"{pointer} =l mul {index}, {elementType.Size(_definitionTable)}");
@@ -78,7 +77,7 @@ public partial class QBEGenerator
_writer.Indented(notOobLabel);
}
private Val EmitArrayInitializer(ArrayInitializer arrayInitializer)
private Val EmitArrayInitializer(ArrayInitializerNode arrayInitializer)
{
var capacity = EmitUnwrap(EmitExpression(arrayInitializer.Capacity));
var elementSize = arrayInitializer.ElementType.Size(_definitionTable);
@@ -99,12 +98,12 @@ public partial class QBEGenerator
return new Val(arrayPointer, arrayInitializer.Type, ValKind.Direct);
}
private Val EmitDereference(Dereference dereference)
private Val EmitDereference(DereferenceNode dereference)
{
return EmitLoad(dereference.Type, EmitUnwrap(EmitExpression(dereference.Expression)));
}
private Val EmitAddressOf(AddressOf addressOf)
private Val EmitAddressOf(AddressOfNode addressOf)
{
var value = EmitExpression(addressOf.Expression);
if (value.Kind != ValKind.Pointer)
@@ -115,7 +114,7 @@ public partial class QBEGenerator
return new Val(value.Name, addressOf.Type, ValKind.Direct);
}
private Val EmitBinaryExpression(BinaryExpression binaryExpression)
private Val EmitBinaryExpression(BinaryExpressionNode binaryExpression)
{
var left = EmitUnwrap(EmitExpression(binaryExpression.Left));
var right = EmitUnwrap(EmitExpression(binaryExpression.Right));
@@ -128,7 +127,7 @@ public partial class QBEGenerator
return new Val(outputName, binaryExpression.Type, ValKind.Direct);
}
private string EmitBinaryInstructionFor(BinaryOperator op, NubType type, string left, string right)
private string EmitBinaryInstructionFor(BinaryOperator op, TypeNode type, string left, string right)
{
if (op is
BinaryOperator.Equal or
@@ -189,11 +188,11 @@ public partial class QBEGenerator
string sign;
if (simpleType is NubIntType { Signed: true })
if (simpleType is IntTypeNode { Signed: true })
{
sign = "s";
}
else if (simpleType is NubIntType { Signed: false })
else if (simpleType is IntTypeNode { Signed: false })
{
sign = "u";
}
@@ -222,44 +221,44 @@ public partial class QBEGenerator
};
}
private Val EmitExternFuncIdent(ExternFuncIdent externFuncIdent)
private Val EmitExternFuncIdent(ExternFuncIdentNode externFuncIdent)
{
var func = _definitionTable.LookupExternFunc(externFuncIdent.Name);
return new Val(ExternFuncName(func), externFuncIdent.Type, ValKind.Direct);
}
private Val EmitLocalFuncIdent(LocalFuncIdent localFuncIdent)
private Val EmitLocalFuncIdent(LocalFuncIdentNode localFuncIdent)
{
var func = _definitionTable.LookupLocalFunc(localFuncIdent.Name);
return new Val(LocalFuncName(func), localFuncIdent.Type, ValKind.Direct);
}
private Val EmitVariableIdent(VariableIdent variableIdent)
private Val EmitVariableIdent(VariableIdentNode variableIdent)
{
return Scope.Lookup(variableIdent.Name);
}
private Val EmitLiteral(Literal literal)
private Val EmitLiteral(LiteralNode literal)
{
switch (literal.Kind)
{
case LiteralKind.Integer:
{
if (literal.Type is NubFloatType { Width: 32 })
if (literal.Type is FloatTypeNode { Width: 32 })
{
var value = float.Parse(literal.Value, CultureInfo.InvariantCulture);
var bits = BitConverter.SingleToInt32Bits(value);
return new Val(bits.ToString(), literal.Type, ValKind.Direct);
}
if (literal.Type is NubFloatType { Width: 64 })
if (literal.Type is FloatTypeNode { Width: 64 })
{
var value = double.Parse(literal.Value, CultureInfo.InvariantCulture);
var bits = BitConverter.DoubleToInt64Bits(value);
return new Val(bits.ToString(), literal.Type, ValKind.Direct);
}
if (literal.Type is NubIntType)
if (literal.Type is IntTypeNode)
{
return new Val(literal.Value, literal.Type, ValKind.Direct);
}
@@ -268,19 +267,19 @@ public partial class QBEGenerator
}
case LiteralKind.Float:
{
if (literal.Type is NubIntType)
if (literal.Type is IntTypeNode)
{
return new Val(literal.Value.Split(".").First(), literal.Type, ValKind.Direct);
}
if (literal.Type is NubFloatType { Width: 32 })
if (literal.Type is FloatTypeNode { Width: 32 })
{
var value = float.Parse(literal.Value, CultureInfo.InvariantCulture);
var bits = BitConverter.SingleToInt32Bits(value);
return new Val(bits.ToString(), literal.Type, ValKind.Direct);
}
if (literal.Type is NubFloatType { Width: 64 })
if (literal.Type is FloatTypeNode { Width: 64 })
{
var value = double.Parse(literal.Value, CultureInfo.InvariantCulture);
var bits = BitConverter.DoubleToInt64Bits(value);
@@ -291,14 +290,14 @@ public partial class QBEGenerator
}
case LiteralKind.String:
{
if (literal.Type is NubStringType)
if (literal.Type is NubStringTypeNode)
{
var stringLiteral = new StringLiteral(literal.Value, StringName());
_stringLiterals.Add(stringLiteral);
return new Val(stringLiteral.Name, literal.Type, ValKind.Direct);
}
if (literal.Type is NubCStringType)
if (literal.Type is CStringTypeNode)
{
var cStringLiteral = new CStringLiteral(literal.Value, CStringName());
_cStringLiterals.Add(cStringLiteral);
@@ -309,7 +308,7 @@ public partial class QBEGenerator
}
case LiteralKind.Bool:
{
if (literal.Type is NubBoolType)
if (literal.Type is BoolTypeNode)
{
return new Val(bool.Parse(literal.Value) ? "1" : "0", literal.Type, ValKind.Direct);
}
@@ -321,7 +320,7 @@ public partial class QBEGenerator
throw new NotSupportedException($"Cannot create literal of kind '{literal.Kind}' for type {literal.Type}");
}
private Val EmitStructInitializer(StructInitializer structInitializer, string? destination = null)
private Val EmitStructInitializer(StructInitializerNode structInitializer, string? destination = null)
{
var @struct = _definitionTable.LookupStruct(structInitializer.StructType.Name);
@@ -349,7 +348,7 @@ public partial class QBEGenerator
return new Val(destination, structInitializer.StructType, ValKind.Direct);
}
private Val EmitUnaryExpression(UnaryExpression unaryExpression)
private Val EmitUnaryExpression(UnaryExpressionNode unaryExpression)
{
var operand = EmitUnwrap(EmitExpression(unaryExpression.Operand));
var outputName = TmpName();
@@ -360,16 +359,16 @@ public partial class QBEGenerator
{
switch (unaryExpression.Operand.Type)
{
case NubIntType { Signed: true, Width: 64 }:
case IntTypeNode { Signed: true, Width: 64 }:
_writer.Indented($"{outputName} =l neg {operand}");
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
case NubIntType { Signed: true, Width: 8 or 16 or 32 }:
case IntTypeNode { Signed: true, Width: 8 or 16 or 32 }:
_writer.Indented($"{outputName} =w neg {operand}");
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
case NubFloatType { Width: 64 }:
case FloatTypeNode { Width: 64 }:
_writer.Indented($"{outputName} =d neg {operand}");
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
case NubFloatType { Width: 32 }:
case FloatTypeNode { Width: 32 }:
_writer.Indented($"{outputName} =s neg {operand}");
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
}
@@ -380,7 +379,7 @@ public partial class QBEGenerator
{
switch (unaryExpression.Operand.Type)
{
case NubBoolType:
case BoolTypeNode:
_writer.Indented($"{outputName} =w xor {operand}, 1");
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
}
@@ -396,7 +395,7 @@ public partial class QBEGenerator
throw new NotSupportedException($"Unary operator {unaryExpression.Operator} for type {unaryExpression.Operand.Type} not supported");
}
private Val EmitStructFieldAccess(StructFieldAccess structFieldAccess)
private Val EmitStructFieldAccess(StructFieldAccessNode structFieldAccess)
{
var target = EmitUnwrap(EmitExpression(structFieldAccess.Target));
@@ -407,7 +406,7 @@ public partial class QBEGenerator
_writer.Indented($"{output} =l add {target}, {offset}");
// If the accessed member is an inline struct, it will not be a pointer
if (structFieldAccess.Type is NubCustomType customType && customType.Kind(_definitionTable) == CustomTypeKind.Struct)
if (structFieldAccess.Type is CustomTypeNode customType && customType.Kind(_definitionTable) == CustomTypeKind.Struct)
{
return new Val(output, structFieldAccess.Type, ValKind.Direct);
}
@@ -415,12 +414,12 @@ public partial class QBEGenerator
return new Val(output, structFieldAccess.Type, ValKind.Pointer);
}
private Val EmitTraitFuncAccess(InterfaceFuncAccess interfaceFuncAccess)
private Val EmitTraitFuncAccess(InterfaceFuncAccessNode interfaceFuncAccess)
{
throw new NotImplementedException();
}
private Val EmitFuncCall(FuncCall funcCall)
private Val EmitFuncCall(FuncCallNode funcCall)
{
var expression = EmitExpression(funcCall.Expression);
var funcPointer = EmitUnwrap(expression);
@@ -433,7 +432,7 @@ public partial class QBEGenerator
parameterStrings.Add($"{FuncQBETypeName(parameter.Type)} {copy}");
}
if (funcCall.Type is NubVoidType)
if (funcCall.Type is VoidTypeNode)
{
_writer.Indented($"call {funcPointer}({string.Join(", ", parameterStrings)})");
return new Val(string.Empty, funcCall.Type, ValKind.Direct);

View File

@@ -5,32 +5,32 @@ namespace NubLang.Generation.QBE;
public partial class QBEGenerator
{
private void EmitStatement(Statement statement)
private void EmitStatement(StatementNode statement)
{
switch (statement)
{
case Assignment assignment:
case AssignmentNode assignment:
EmitAssignment(assignment);
break;
case Break:
case BreakNode:
EmitBreak();
break;
case Continue:
case ContinueNode:
EmitContinue();
break;
case If ifStatement:
case IfNode ifStatement:
EmitIf(ifStatement);
break;
case Return @return:
case ReturnNode @return:
EmitReturn(@return);
break;
case StatementExpression statementExpression:
case StatementExpressionNode statementExpression:
EmitExpression(statementExpression.Expression);
break;
case VariableDeclaration variableDeclaration:
case VariableDeclarationNode variableDeclaration:
EmitVariableDeclaration(variableDeclaration);
break;
case While whileStatement:
case WhileNode whileStatement:
EmitWhile(whileStatement);
break;
default:
@@ -38,7 +38,7 @@ public partial class QBEGenerator
}
}
private void EmitAssignment(Assignment assignment)
private void EmitAssignment(AssignmentNode assignment)
{
var destination = EmitExpression(assignment.Target);
Debug.Assert(destination.Kind == ValKind.Pointer);
@@ -57,7 +57,7 @@ public partial class QBEGenerator
_codeIsReachable = false;
}
private void EmitIf(If ifStatement)
private void EmitIf(IfNode ifStatement)
{
var trueLabel = LabelName();
var falseLabel = LabelName();
@@ -81,7 +81,7 @@ public partial class QBEGenerator
_writer.WriteLine(endLabel);
}
private void EmitReturn(Return @return)
private void EmitReturn(ReturnNode @return)
{
if (@return.Value.HasValue)
{
@@ -94,7 +94,7 @@ public partial class QBEGenerator
}
}
private void EmitVariableDeclaration(VariableDeclaration variableDeclaration)
private void EmitVariableDeclaration(VariableDeclarationNode variableDeclaration)
{
var name = $"%{variableDeclaration.Name}";
_writer.Indented($"{name} =l alloc8 8");
@@ -108,7 +108,7 @@ public partial class QBEGenerator
Scope.Declare(variableDeclaration.Name, new Val(name, variableDeclaration.Type, ValKind.Pointer));
}
private void EmitWhile(While whileStatement)
private void EmitWhile(WhileNode whileStatement)
{
var conditionLabel = LabelName();
var iterationLabel = LabelName();

View File

@@ -17,7 +17,7 @@ public partial class QBEGenerator
private readonly List<StringLiteral> _stringLiterals = [];
private readonly Stack<string> _breakLabels = [];
private readonly Stack<string> _continueLabels = [];
private readonly Queue<(ArrowFunc Func, string Name)> _arrowFunctions = [];
private readonly Queue<(ArrowFuncNode Func, string Name)> _arrowFunctions = [];
private readonly Stack<Scope> _scopes = [];
private int _tmpIndex;
private int _labelIndex;
@@ -62,7 +62,7 @@ public partial class QBEGenerator
_writer.NewLine();
}
foreach (var funcDef in _syntaxTree.Definitions.OfType<LocalFunc>())
foreach (var funcDef in _syntaxTree.Definitions.OfType<LocalFuncNode>())
{
EmitFuncDefinition(LocalFuncName(funcDef), funcDef.Signature.Parameters, funcDef.Signature.ReturnType, funcDef.Body);
_writer.NewLine();
@@ -88,7 +88,7 @@ public partial class QBEGenerator
return _writer.ToString();
}
private static string QBEAssign(NubType type)
private static string QBEAssign(TypeNode type)
{
if (type.IsSimpleType(out var simpleType, out _))
{
@@ -105,7 +105,7 @@ public partial class QBEGenerator
return "=l";
}
private void EmitStore(NubType type, string value, string destination)
private void EmitStore(TypeNode type, string value, string destination)
{
string store;
@@ -130,7 +130,7 @@ public partial class QBEGenerator
_writer.Indented($"{store} {value}, {destination}");
}
private Val EmitLoad(NubType type, string from)
private Val EmitLoad(TypeNode type, string from)
{
string load;
@@ -166,7 +166,7 @@ public partial class QBEGenerator
_writer.Indented($"call $nub_memcpy(l {source}, l {destination}, l {length})");
}
private string EmitArraySizeInBytes(NubArrayType type, string array)
private string EmitArraySizeInBytes(ArrayTypeNode type, string array)
{
var size = TmpName();
_writer.Indented($"{size} =l loadl {array}");
@@ -191,21 +191,21 @@ public partial class QBEGenerator
return size;
}
private bool EmitTryMoveInto(Expression source, string destinationPointer)
private bool EmitTryMoveInto(ExpressionNode source, string destinationPointer)
{
switch (source)
{
case ArrayInitializer arrayInitializer:
case ArrayInitializerNode arrayInitializer:
{
EmitStore(source.Type, EmitUnwrap(EmitArrayInitializer(arrayInitializer)), destinationPointer);
return true;
}
case StructInitializer structInitializer:
case StructInitializerNode structInitializer:
{
EmitStructInitializer(structInitializer, destinationPointer);
return true;
}
case Literal { Kind: LiteralKind.String } literal:
case LiteralNode { 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(Expression source, string destinationPointer)
private void EmitCopyIntoOrInitialize(ExpressionNode 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))
@@ -231,7 +231,7 @@ public partial class QBEGenerator
}
else
{
if (complexType is NubCustomType customType)
if (complexType is CustomTypeNode customType)
{
EmitMemcpy(value, destinationPointer, customType.Size(_definitionTable).ToString());
}
@@ -239,9 +239,9 @@ public partial class QBEGenerator
{
var size = complexType switch
{
NubArrayType arrayType => EmitArraySizeInBytes(arrayType, value),
NubCStringType => EmitCStringSizeInBytes(value),
NubStringType => EmitStringSizeInBytes(value),
ArrayTypeNode arrayType => EmitArraySizeInBytes(arrayType, value),
CStringTypeNode => EmitCStringSizeInBytes(value),
NubStringTypeNode => EmitStringSizeInBytes(value),
_ => throw new ArgumentOutOfRangeException(nameof(source.Type))
};
@@ -253,13 +253,13 @@ public partial class QBEGenerator
}
}
private bool EmitTryCreateWithoutCopy(Expression source, [NotNullWhen(true)] out string? destination)
private bool EmitTryCreateWithoutCopy(ExpressionNode source, [NotNullWhen(true)] out string? destination)
{
switch (source)
{
case ArrayInitializer:
case StructInitializer:
case Literal { Kind: LiteralKind.String }:
case ArrayInitializerNode:
case StructInitializerNode:
case LiteralNode { Kind: LiteralKind.String }:
{
destination = EmitUnwrap(EmitExpression(source));
return true;
@@ -270,7 +270,7 @@ public partial class QBEGenerator
return false;
}
private string EmitCreateCopyOrInitialize(Expression source)
private string EmitCreateCopyOrInitialize(ExpressionNode 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))
@@ -288,10 +288,10 @@ public partial class QBEGenerator
var size = complexType switch
{
NubArrayType arrayType => EmitArraySizeInBytes(arrayType, value),
NubCStringType => EmitCStringSizeInBytes(value),
NubStringType => EmitStringSizeInBytes(value),
NubCustomType customType => customType.Size(_definitionTable).ToString(),
ArrayTypeNode arrayType => EmitArraySizeInBytes(arrayType, value),
CStringTypeNode => EmitCStringSizeInBytes(value),
NubStringTypeNode => EmitStringSizeInBytes(value),
CustomTypeNode customType => customType.Size(_definitionTable).ToString(),
_ => throw new ArgumentOutOfRangeException(nameof(source.Type))
};
@@ -302,7 +302,7 @@ public partial class QBEGenerator
}
// Utility to create QBE type names for function parameters and return types
private string FuncQBETypeName(NubType type)
private string FuncQBETypeName(TypeNode type)
{
if (type.IsSimpleType(out var simpleType, out var complexType))
{
@@ -320,7 +320,7 @@ public partial class QBEGenerator
};
}
if (complexType is NubCustomType customType)
if (complexType is CustomTypeNode customType)
{
return CustomTypeName(customType);
}
@@ -328,7 +328,7 @@ public partial class QBEGenerator
return "l";
}
private void EmitFuncDefinition(string name, IReadOnlyList<FuncParameter> parameters, NubType returnType, Block body)
private void EmitFuncDefinition(string name, IReadOnlyList<FuncParameterNode> parameters, TypeNode returnType, BlockNode body)
{
_labelIndex = 0;
_tmpIndex = 0;
@@ -337,7 +337,7 @@ public partial class QBEGenerator
builder.Append("export function ");
if (returnType is not NubVoidType)
if (returnType is not VoidTypeNode)
{
builder.Append(FuncQBETypeName(returnType) + ' ');
}
@@ -360,9 +360,9 @@ 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 Return])
if (returnType is VoidTypeNode && body.Statements is [.., not ReturnNode])
{
if (returnType is NubVoidType)
if (returnType is VoidTypeNode)
{
_writer.Indented("ret");
}
@@ -371,7 +371,7 @@ public partial class QBEGenerator
_writer.EndFunction();
}
private void EmitStructDefinition(Struct structDef)
private void EmitStructDefinition(StructNode structDef)
{
_writer.WriteLine($"type {CustomTypeName(structDef.Name)} = {{ ");
@@ -392,7 +392,7 @@ public partial class QBEGenerator
_writer.WriteLine("}");
return;
string StructDefQBEType(StructField field)
string StructDefQBEType(StructFieldNode field)
{
if (field.Type.IsSimpleType(out var simpleType, out var complexType))
{
@@ -408,7 +408,7 @@ public partial class QBEGenerator
};
}
if (complexType is NubCustomType customType)
if (complexType is CustomTypeNode customType)
{
return CustomTypeName(customType);
}
@@ -417,7 +417,7 @@ public partial class QBEGenerator
}
}
private void EmitTraitVTable(Trait traitDef)
private void EmitTraitVTable(TraitNode traitDef)
{
_writer.WriteLine($"type {CustomTypeName(traitDef.Name)} = {{");
@@ -429,7 +429,7 @@ public partial class QBEGenerator
_writer.WriteLine("}");
}
private void EmitBlock(Block block, Scope? scope = null)
private void EmitBlock(BlockNode block, Scope? scope = null)
{
_scopes.Push(scope ?? Scope.SubScope());
@@ -456,7 +456,7 @@ public partial class QBEGenerator
};
}
private int OffsetOf(Struct structDefinition, string member)
private int OffsetOf(StructNode structDefinition, string member)
{
var offset = 0;
@@ -469,7 +469,7 @@ public partial class QBEGenerator
var fieldAlignment = field.Type.Alignment(_definitionTable);
offset = NubType.AlignTo(offset, fieldAlignment);
offset = TypeNode.AlignTo(offset, fieldAlignment);
offset += field.Type.Size(_definitionTable);
}
@@ -498,17 +498,17 @@ public partial class QBEGenerator
return $"$string{++_stringLiteralIndex}";
}
private string LocalFuncName(LocalFunc funcDef)
private string LocalFuncName(LocalFuncNode funcDef)
{
return $"${funcDef.Name}";
}
private string ExternFuncName(ExternFunc funcDef)
private string ExternFuncName(ExternFuncNode funcDef)
{
return $"${funcDef.CallName}";
}
private string CustomTypeName(NubCustomType customType)
private string CustomTypeName(CustomTypeNode customType)
{
return CustomTypeName(customType.Name);
}
@@ -533,7 +533,7 @@ public class CStringLiteral(string value, string name)
public string Name { get; } = name;
}
public record Val(string Name, NubType Type, ValKind Kind);
public record Val(string Name, TypeNode Type, ValKind Kind);
public class Scope(Scope? parent = null)
{

View File

@@ -4,41 +4,41 @@ namespace NubLang.Generation;
public sealed class TypedDefinitionTable
{
private readonly List<Definition> _definitions;
private readonly List<DefinitionNode> _definitions;
public TypedDefinitionTable(IEnumerable<TypedSyntaxTree> syntaxTrees)
{
_definitions = syntaxTrees.SelectMany(x => x.Definitions).ToList();
}
public LocalFunc LookupLocalFunc(string name)
public LocalFuncNode LookupLocalFunc(string name)
{
return _definitions
.OfType<LocalFunc>()
.OfType<LocalFuncNode>()
.First(x => x.Name == name);
}
public ExternFunc LookupExternFunc(string name)
public ExternFuncNode LookupExternFunc(string name)
{
return _definitions
.OfType<ExternFunc>()
.OfType<ExternFuncNode>()
.First(x => x.Name == name);
}
public Struct LookupStruct(string name)
public StructNode LookupStruct(string name)
{
return _definitions
.OfType<Struct>()
.OfType<StructNode>()
.First(x => x.Name == name);
}
public IEnumerable<Struct> GetStructs()
public IEnumerable<StructNode> GetStructs()
{
return _definitions.OfType<Struct>();
return _definitions.OfType<StructNode>();
}
public IEnumerable<Trait> GetTraits()
public IEnumerable<TraitNode> GetTraits()
{
return _definitions.OfType<Trait>();
return _definitions.OfType<TraitNode>();
}
}