This commit is contained in:
nub31
2025-09-12 17:31:40 +02:00
parent 314f7efc7b
commit 1eeeb67d88
4 changed files with 69 additions and 59 deletions

View File

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

View File

@@ -4,45 +4,55 @@ namespace NubLang.Modules;
public class Module public class Module
{ {
private readonly List<ModuleStructType> _structs = []; private readonly List<ModuleStruct> _structs = [];
private readonly List<ModuleInterfaceType> _interfaces = []; private readonly List<ModuleInterface> _interfaces = [];
private readonly List<ModuleFuncType> _functions = []; private readonly List<ModuleFunction> _functions = [];
public void RegisterStruct(bool exported, string name, List<ModuleStructTypeField> fields) public void RegisterStruct(bool exported, string name, List<ModuleStructField> fields, List<ModuleStructFunction> functions)
{ {
_structs.Add(new ModuleStructType(exported, name, fields)); _structs.Add(new ModuleStruct(exported, name, fields, functions));
} }
public void RegisterInterface(bool exported, string name) public void RegisterInterface(bool exported, string name, List<ModuleInterfaceFunction> functions)
{ {
_interfaces.Add(new ModuleInterfaceType(exported, name)); _interfaces.Add(new ModuleInterface(exported, name, functions));
} }
public void RegisterFunction(bool exported, string name, string? externSymbol, FuncTypeSyntax type) public void RegisterFunction(bool exported, string name, string? externSymbol, List<ModuleFunctionParameter> parameters, TypeSyntax returnType)
{ {
_functions.Add(new ModuleFuncType(exported, name, externSymbol, type)); _functions.Add(new ModuleFunction(exported, name, externSymbol, parameters, returnType));
} }
public List<ModuleStructType> StructTypes(bool includePrivate) public List<ModuleStruct> StructTypes(bool includePrivate)
{ {
return _structs.Where(x => x.Exported || includePrivate).ToList(); return _structs.Where(x => x.Exported || includePrivate).ToList();
} }
public List<ModuleInterfaceType> InterfaceTypes(bool includePrivate) public List<ModuleInterface> InterfaceTypes(bool includePrivate)
{ {
return _interfaces.Where(x => x.Exported || includePrivate).ToList(); return _interfaces.Where(x => x.Exported || includePrivate).ToList();
} }
public List<ModuleFuncType> Functions(bool includePrivate) public List<ModuleFunction> Functions(bool includePrivate)
{ {
return _functions.Where(x => x.Exported || includePrivate).ToList(); return _functions.Where(x => x.Exported || includePrivate).ToList();
} }
} }
public record ModuleStructTypeField(string Name, TypeSyntax Type, bool HasDefaultValue); public record ModuleStructField(string Name, TypeSyntax Type, bool HasDefaultValue);
public record ModuleStructType(bool Exported, string Name, List<ModuleStructTypeField> Fields); public record ModuleStructFunctionParameter(string Name, TypeSyntax Type);
public record ModuleInterfaceType(bool Exported, string Name); public record ModuleStructFunction(string Name, List<ModuleStructFunctionParameter> Parameters, TypeSyntax ReturnType);
public record ModuleFuncType(bool Exported, string Name, string? ExternSymbol, FuncTypeSyntax FuncType); public record ModuleStruct(bool Exported, string Name, List<ModuleStructField> Fields, List<ModuleStructFunction> Functions);
public record ModuleInterfaceFunctionParameter(string Name, TypeSyntax Type);
public record ModuleInterfaceFunction(string Name, List<ModuleInterfaceFunctionParameter> Parameters, TypeSyntax ReturnType);
public record ModuleInterface(bool Exported, string Name, List<ModuleInterfaceFunction> Functions);
public record ModuleFunctionParameter(string Name, TypeSyntax Type);
public record ModuleFunction(bool Exported, string Name, string? ExternSymbol, List<ModuleFunctionParameter> Parameters, TypeSyntax ReturnType);

View File

@@ -24,20 +24,34 @@ public class ModuleRepository
{ {
case FuncSyntax funcDef: case FuncSyntax funcDef:
{ {
var parameters = funcDef.Signature.Parameters.Select(x => x.Type).ToList(); var parameters = funcDef.Signature.Parameters.Select(x => new ModuleFunctionParameter(x.Name, x.Type)).ToList();
var type = new FuncTypeSyntax([], parameters, funcDef.Signature.ReturnType); module.RegisterFunction(funcDef.Exported, funcDef.Name, funcDef.ExternSymbol, parameters, funcDef.Signature.ReturnType);
module.RegisterFunction(funcDef.Exported, funcDef.Name, funcDef.ExternSymbol, type);
break; break;
} }
case StructSyntax structDef: case StructSyntax structDef:
{ {
var fields = structDef.Fields.Select(x => new ModuleStructTypeField(x.Name, x.Type, x.Value.HasValue)).ToList(); var fields = structDef.Fields.Select(x => new ModuleStructField(x.Name, x.Type, x.Value.HasValue)).ToList();
module.RegisterStruct(structDef.Exported, structDef.Name, fields);
var functions = new List<ModuleStructFunction>();
foreach (var function in structDef.Functions)
{
var parameters = function.Signature.Parameters.Select(x => new ModuleStructFunctionParameter(x.Name, x.Type)).ToList();
functions.AddRange(new ModuleStructFunction(function.Name, parameters, function.Signature.ReturnType));
}
module.RegisterStruct(structDef.Exported, structDef.Name, fields, functions);
break; break;
} }
case InterfaceSyntax interfaceDef: case InterfaceSyntax interfaceDef:
{ {
module.RegisterInterface(interfaceDef.Exported, interfaceDef.Name); var functions = new List<ModuleInterfaceFunction>();
foreach (var function in interfaceDef.Functions)
{
var parameters = function.Signature.Parameters.Select(x => new ModuleInterfaceFunctionParameter(x.Name, x.Type)).ToList();
functions.AddRange(new ModuleInterfaceFunction(function.Name, parameters, function.Signature.ReturnType));
}
module.RegisterInterface(interfaceDef.Exported, interfaceDef.Name, functions);
break; break;
} }
default: default:

View File

@@ -366,7 +366,9 @@ public sealed class TypeChecker
if (function != null) if (function != null)
{ {
return new FuncIdentifierNode(ResolveType(function.FuncType), _syntaxTree.Metadata.ModuleName, expression.Name, function.ExternSymbol); var parameters = function.Parameters.Select(x => ResolveType(x.Type)).ToList();
var type = new FuncTypeNode(parameters, ResolveType(function.ReturnType));
return new FuncIdentifierNode(type, _syntaxTree.Metadata.ModuleName, expression.Name, function.ExternSymbol);
} }
throw new TypeCheckerException(Diagnostic.Error($"Symbol {expression.Name} not found").At(expression).Build()); throw new TypeCheckerException(Diagnostic.Error($"Symbol {expression.Name} not found").At(expression).Build());
@@ -385,7 +387,9 @@ public sealed class TypeChecker
var function = module.Functions(includePrivate).FirstOrDefault(x => x.Name == expression.Name); var function = module.Functions(includePrivate).FirstOrDefault(x => x.Name == expression.Name);
if (function != null) if (function != null)
{ {
return new FuncIdentifierNode(ResolveType(function.FuncType), expression.Module, expression.Name, function.ExternSymbol); var parameters = function.Parameters.Select(x => ResolveType(x.Type)).ToList();
var type = new FuncTypeNode(parameters, ResolveType(function.ReturnType));
return new FuncIdentifierNode(type, expression.Module, expression.Name, function.ExternSymbol);
} }
throw new TypeCheckerException(Diagnostic.Error($"No exported symbol {expression.Name} not found in module {expression.Module}").At(expression).Build()); throw new TypeCheckerException(Diagnostic.Error($"No exported symbol {expression.Name} not found in module {expression.Module}").At(expression).Build());