Node postfix for nodes
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user