This commit is contained in:
nub31
2025-09-12 17:31:40 +02:00
parent 51ca1b9211
commit 8856df6bde
4 changed files with 69 additions and 59 deletions

View File

@@ -46,19 +46,16 @@ public class QBEGenerator
foreach (var structType in _structTypes)
{
EmitStructType(structType);
_writer.NewLine();
}
foreach (var structDef in _definitions.OfType<StructNode>())
{
EmitStructDefinition(structDef);
_writer.NewLine();
}
foreach (var funcDef in _definitions.OfType<FuncNode>())
{
EmitFuncDefinition(funcDef);
_writer.NewLine();
}
// foreach (var structDef in _definitions.OfType<StructNode>().Where(x => x.InterfaceImplementations.Count > 0))
@@ -408,19 +405,14 @@ public class QBEGenerator
private void EmitStructDefinition(StructNode structDef)
{
// _writer.WriteLine($"export function {StructCtorName(_module.Name, structDef.Name)}() {{");
// _writer.WriteLine("@start");
// _writer.Indented($"%struct =l alloc8 {SizeOf(type)}");
// // todo(nub31): Finish constructor
// _writer.Indented("ret %struct");
// _writer.WriteLine("}");
// todo(nub31): Find a way do run the initializers from other modules where the definition is not available.
// A constructor is probably the answer
foreach (var function in structDef.Functions)
{
_labelIndex = 0;
_tmpIndex = 0;
_writer.NewLine();
_writer.Write("export function ");
if (function.Signature.ReturnType is not VoidTypeNode)
@@ -453,14 +445,14 @@ public class QBEGenerator
private void EmitStructType(StructTypeNode structType)
{
_writer.WriteLine($"type {StructTypeName(structType.Module, structType.Name)} = {{ ");
_writer.Write($"type {StructTypeName(structType.Module, structType.Name)} = {{ ");
foreach (var field in structType.Fields)
{
_writer.Indented($"{StructDefQBEType(field.Type)},");
_writer.Write($"{StructDefQBEType(field.Type)},");
}
_writer.WriteLine("}");
_writer.WriteLine(" }");
return;
string StructDefQBEType(TypeNode type)
@@ -1367,55 +1359,45 @@ public class QBEGenerator
throw new UnreachableException($"Member '{member}' not found in struct");
}
#region Naming utilities
private string TmpName()
{
return $"%t{++_tmpIndex}";
return $"%t.{++_tmpIndex}";
}
private string LabelName()
{
return $"@l{++_labelIndex}";
return $"@l.{++_labelIndex}";
}
private string CStringName()
{
return $"$cstring{++_cStringLiteralIndex}";
return $"$cstr.{++_cStringLiteralIndex}";
}
private string StringName()
{
return $"$string{++_stringLiteralIndex}";
return $"$str.{++_stringLiteralIndex}";
}
private string FuncName(string module, string name, string? externSymbol)
private static string FuncName(string module, string name, string? externSymbol)
{
var symbol = externSymbol ?? $"{module}.{name}";
return "$" + symbol;
return $"${externSymbol ?? $"{module}.{name}"}";
}
private string StructTypeName(string module, string name)
private static string StructTypeName(string module, string name)
{
return $":{module}.{name}";
}
private string StructFuncName(string module, string structName, string funcName)
private static string StructFuncName(string module, string structName, string funcName)
{
return $"${module}.{structName}_func.{funcName}";
return $"${module}.{structName}.func.{funcName}";
}
private string StructCtorName(string module, string structName)
private static string StructVtableName(string module, string structName)
{
return $"${module}.{structName}_ctor";
return $"${module}.{structName}.vtable";
}
private string StructVtableName(string module, string structName)
{
return $"${module}.{structName}_vtable";
}
#endregion
}
public class StringLiteral(string value, string name)