This commit is contained in:
nub31
2025-08-12 20:01:43 +02:00
parent ad33cd7318
commit 1ef1df545f
10 changed files with 204 additions and 37 deletions

View File

@@ -51,10 +51,20 @@ public partial class QBEGenerator
foreach (var structDef in _definitionTable.GetStructs())
{
EmitStructDefinition(structDef);
EmitStructTypeDefinition(structDef);
_writer.NewLine();
}
foreach (var structDef in _syntaxTree.Definitions.OfType<StructNode>())
{
foreach (var func in structDef.Functions)
{
var funcName = StructFuncName(structDef.Name, func.Name);
EmitFuncDefinition(funcName, func.Signature.Parameters, func.Signature.ReturnType, func.Body);
_writer.NewLine();
}
}
foreach (var funcDef in _syntaxTree.Definitions.OfType<LocalFuncNode>())
{
EmitFuncDefinition(LocalFuncName(funcDef), funcDef.Signature.Parameters, funcDef.Signature.ReturnType, funcDef.Body);
@@ -67,6 +77,22 @@ public partial class QBEGenerator
_writer.NewLine();
}
foreach (var structDef in _syntaxTree.Definitions.OfType<StructNode>().Where(x => x.InterfaceImplementations.Count > 0))
{
_writer.Write($"data {StructVtableName(structDef.Name)} = {{ ");
foreach (var interfaceImplementation in structDef.InterfaceImplementations)
{
var interfaceDef = _definitionTable.LookupInterface(interfaceImplementation.Name);
foreach (var func in interfaceDef.Functions)
{
_writer.Write($"l {StructFuncName(structDef.Name, func.Name)}, ");
}
}
_writer.WriteLine("}");
}
foreach (var cStringLiteral in _cStringLiterals)
{
_writer.WriteLine($"data {cStringLiteral.Name} = {{ b \"{cStringLiteral.Value}\", b 0 }}");
@@ -315,7 +341,7 @@ public partial class QBEGenerator
if (complexType is CustomTypeNode customType)
{
return CustomTypeName(customType);
return CustomTypeName(customType.Name);
}
return "l";
@@ -359,7 +385,7 @@ public partial class QBEGenerator
_writer.WriteLine("}");
}
private void EmitStructDefinition(StructNode structDef)
private void EmitStructTypeDefinition(StructNode structDef)
{
_writer.WriteLine($"type {CustomTypeName(structDef.Name)} = {{ ");
@@ -398,7 +424,7 @@ public partial class QBEGenerator
if (complexType is CustomTypeNode customType)
{
return CustomTypeName(customType);
return CustomTypeName(customType.Name);
}
return "l";
@@ -432,11 +458,16 @@ public partial class QBEGenerator
};
}
private int OffsetOf(StructNode structDefinition, string member)
private int OffsetOf(StructNode structDef, string member)
{
var offset = 0;
foreach (var field in structDefinition.Fields)
if (structDef.InterfaceImplementations.Any())
{
offset = 8;
}
foreach (var field in structDef.Fields)
{
if (field.Name == member)
{
@@ -484,16 +515,21 @@ public partial class QBEGenerator
return $"${funcDef.CallName}";
}
private string CustomTypeName(CustomTypeNode customType)
{
return CustomTypeName(customType.Name);
}
private string CustomTypeName(string name)
{
return $":{name}";
}
private string StructFuncName(string structName, string funcName)
{
return $"${structName}_{funcName}";
}
private string StructVtableName(string structName)
{
return $"${structName}_vtable";
}
#endregion
}
@@ -509,7 +545,7 @@ public class CStringLiteral(string value, string name)
public string Name { get; } = name;
}
public record Val(string Name, TypeNode Type, ValKind Kind);
public record Val(string Name, TypeNode Type, ValKind Kind, Val? ThisArg = null);
public class Scope(Scope? parent = null)
{