WIP: dev #1
@@ -61,9 +61,7 @@ public class Generator
|
|||||||
writer.Write("struct ");
|
writer.Write("struct ");
|
||||||
|
|
||||||
if (s.Packed)
|
if (s.Packed)
|
||||||
{
|
|
||||||
writer.Write("__attribute__((__packed__)) ");
|
writer.Write("__attribute__((__packed__)) ");
|
||||||
}
|
|
||||||
|
|
||||||
writer.WriteLine(NameMangler.Mangle(module.Name, name, NubTypeStruct.Get(module.Name, name)));
|
writer.WriteLine(NameMangler.Mangle(module.Name, name, NubTypeStruct.Get(module.Name, name)));
|
||||||
writer.WriteLine("{");
|
writer.WriteLine("{");
|
||||||
@@ -87,8 +85,15 @@ public class Generator
|
|||||||
{
|
{
|
||||||
if (info.Type is NubTypeFunc fn)
|
if (info.Type is NubTypeFunc fn)
|
||||||
{
|
{
|
||||||
if (info.External)
|
switch (info.Kind)
|
||||||
|
{
|
||||||
|
case Module.DefinitionKind.External:
|
||||||
writer.Write("extern ");
|
writer.Write("extern ");
|
||||||
|
break;
|
||||||
|
case Module.DefinitionKind.Internal:
|
||||||
|
writer.Write("static ");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
writer.WriteLine($"{CType(fn.ReturnType, info.MangledName)}({string.Join(", ", fn.Parameters.Select(p => CType(p)))});");
|
writer.WriteLine($"{CType(fn.ReturnType, info.MangledName)}({string.Join(", ", fn.Parameters.Select(p => CType(p)))});");
|
||||||
}
|
}
|
||||||
@@ -112,16 +117,20 @@ public class Generator
|
|||||||
return {{info.MangledName}}();
|
return {{info.MangledName}}();
|
||||||
}
|
}
|
||||||
""");
|
""");
|
||||||
}
|
|
||||||
|
|
||||||
writer.WriteLine();
|
writer.WriteLine();
|
||||||
|
}
|
||||||
|
|
||||||
foreach (var function in functions)
|
foreach (var function in functions)
|
||||||
{
|
{
|
||||||
if (!moduleGraph.TryResolveIdentifier(function.Module, function.Name.Ident, true, out var info))
|
if (!moduleGraph.TryResolveIdentifier(function.Module, function.Name.Ident, true, out var info))
|
||||||
throw new UnreachableException($"Module graph does not have info about the function {function.Module}::{function.Name.Ident}. This should have been caught earlier");
|
throw new UnreachableException($"Module graph does not have info about the function {function.Module}::{function.Name.Ident}. This should have been caught earlier");
|
||||||
|
|
||||||
|
if (info.Kind == Module.DefinitionKind.Internal)
|
||||||
|
writer.Write("static ");
|
||||||
|
|
||||||
var parameters = function.Parameters.Select(x => CType(x.Type, x.Name.Ident));
|
var parameters = function.Parameters.Select(x => CType(x.Type, x.Name.Ident));
|
||||||
|
|
||||||
writer.WriteLine($"{CType(function.ReturnType, info.MangledName)}({string.Join(", ", parameters)})");
|
writer.WriteLine($"{CType(function.ReturnType, info.MangledName)}({string.Join(", ", parameters)})");
|
||||||
writer.WriteLine("{");
|
writer.WriteLine("{");
|
||||||
using (writer.Indent())
|
using (writer.Indent())
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ public class ModuleGraph
|
|||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
case Manifest.Module.TypeInfoStruct s:
|
case Manifest.Module.TypeInfoStruct s:
|
||||||
var info = new Module.TypeInfoStruct(true, s.Packed, true);
|
var info = new Module.TypeInfoStruct(Module.DefinitionKind.External, s.Packed);
|
||||||
var fields = s.Fields.Select(x => new Module.TypeInfoStruct.Field(x.Name, x.Type)).ToList();
|
var fields = s.Fields.Select(x => new Module.TypeInfoStruct.Field(x.Name, x.Type)).ToList();
|
||||||
info.SetFields(fields);
|
info.SetFields(fields);
|
||||||
module.AddType(name, info);
|
module.AddType(name, info);
|
||||||
@@ -97,7 +97,7 @@ public class ModuleGraph
|
|||||||
|
|
||||||
foreach (var (name, identifier) in manifestModule.Identifiers)
|
foreach (var (name, identifier) in manifestModule.Identifiers)
|
||||||
{
|
{
|
||||||
module.AddIdentifier(name, new Module.IdentifierInfo(true, identifier.Type, identifier.MangledName, true));
|
module.AddIdentifier(name, new Module.IdentifierInfo(Module.DefinitionKind.External, identifier.Type, identifier.MangledName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,7 +108,8 @@ public class ModuleGraph
|
|||||||
|
|
||||||
foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>())
|
foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>())
|
||||||
{
|
{
|
||||||
module.AddType(structDef.Name.Ident, new Module.TypeInfoStruct(structDef.Exported, structDef.Packed, false));
|
var kind = structDef.Exported ? Module.DefinitionKind.Exported : Module.DefinitionKind.Internal;
|
||||||
|
module.AddType(structDef.Name.Ident, new Module.TypeInfoStruct(kind, structDef.Packed));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,14 +139,16 @@ public class ModuleGraph
|
|||||||
var parameters = funcDef.Parameters.Select(x => ResolveType(x.Type, module.Name)).ToList();
|
var parameters = funcDef.Parameters.Select(x => ResolveType(x.Type, module.Name)).ToList();
|
||||||
var returnType = ResolveType(funcDef.ReturnType, module.Name);
|
var returnType = ResolveType(funcDef.ReturnType, module.Name);
|
||||||
var funcType = NubTypeFunc.Get(parameters, returnType);
|
var funcType = NubTypeFunc.Get(parameters, returnType);
|
||||||
var info = new Module.IdentifierInfo(funcDef.Exported, funcType, NameMangler.Mangle(module.Name, funcDef.Name.Ident, funcType), false);
|
var kind = funcDef.Exported ? Module.DefinitionKind.Exported : Module.DefinitionKind.Internal;
|
||||||
|
var info = new Module.IdentifierInfo(kind, funcType, NameMangler.Mangle(module.Name, funcDef.Name.Ident, funcType));
|
||||||
module.AddIdentifier(funcDef.Name.Ident, info);
|
module.AddIdentifier(funcDef.Name.Ident, info);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var globalVariable in ast.Definitions.OfType<NodeDefinitionGlobalVariable>())
|
foreach (var globalVariable in ast.Definitions.OfType<NodeDefinitionGlobalVariable>())
|
||||||
{
|
{
|
||||||
var type = ResolveType(globalVariable.Type, module.Name);
|
var type = ResolveType(globalVariable.Type, module.Name);
|
||||||
var info = new Module.IdentifierInfo(globalVariable.Exported, type, NameMangler.Mangle(module.Name, globalVariable.Name.Ident, type), false);
|
var kind = globalVariable.Exported ? Module.DefinitionKind.Exported : Module.DefinitionKind.Internal;
|
||||||
|
var info = new Module.IdentifierInfo(kind, type, NameMangler.Mangle(module.Name, globalVariable.Name.Ident, type));
|
||||||
module.AddIdentifier(globalVariable.Name.Ident, info);
|
module.AddIdentifier(globalVariable.Name.Ident, info);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -222,7 +225,7 @@ public class Module(string name)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (info.Exported || searchPrivate)
|
if (searchPrivate || info.Kind is DefinitionKind.External or DefinitionKind.Exported)
|
||||||
{
|
{
|
||||||
customType = info;
|
customType = info;
|
||||||
return true;
|
return true;
|
||||||
@@ -241,7 +244,7 @@ public class Module(string name)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (info.Exported || searchPrivate)
|
if (searchPrivate || info.Kind is DefinitionKind.External or DefinitionKind.Exported)
|
||||||
{
|
{
|
||||||
identifierType = info;
|
identifierType = info;
|
||||||
return true;
|
return true;
|
||||||
@@ -261,21 +264,26 @@ public class Module(string name)
|
|||||||
identifiers.Add(name, info);
|
identifiers.Add(name, info);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class IdentifierInfo(bool exported, NubType type, string mangledName, bool external)
|
public enum DefinitionKind
|
||||||
{
|
{
|
||||||
public bool Exported { get; } = exported;
|
Internal,
|
||||||
|
Exported,
|
||||||
|
External,
|
||||||
|
}
|
||||||
|
|
||||||
|
public class IdentifierInfo(DefinitionKind kind, NubType type, string mangledName)
|
||||||
|
{
|
||||||
|
public DefinitionKind Kind { get; } = kind;
|
||||||
public NubType Type { get; } = type;
|
public NubType Type { get; } = type;
|
||||||
public string MangledName { get; } = mangledName;
|
public string MangledName { get; } = mangledName;
|
||||||
public bool External { get; } = external;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract class TypeInfo(bool exported, bool external)
|
public abstract class TypeInfo(DefinitionKind kind)
|
||||||
{
|
{
|
||||||
public bool Exported { get; } = exported;
|
public DefinitionKind Kind { get; } = kind;
|
||||||
public bool External { get; } = external;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class TypeInfoStruct(bool exported, bool packed, bool external) : TypeInfo(exported, external)
|
public class TypeInfoStruct(DefinitionKind kind, bool packed) : TypeInfo(kind)
|
||||||
{
|
{
|
||||||
private IReadOnlyList<Field>? fields;
|
private IReadOnlyList<Field>? fields;
|
||||||
|
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ public record Manifest(Dictionary<string, Manifest.Module> Modules)
|
|||||||
|
|
||||||
foreach (var (name, typeInfo) in module.GetTypes())
|
foreach (var (name, typeInfo) in module.GetTypes())
|
||||||
{
|
{
|
||||||
if (typeInfo.Exported && !typeInfo.External)
|
if (typeInfo.Kind == Compiler.Module.DefinitionKind.Exported)
|
||||||
{
|
{
|
||||||
types.Add(name, ConvertType(typeInfo));
|
types.Add(name, ConvertType(typeInfo));
|
||||||
}
|
}
|
||||||
@@ -83,7 +83,7 @@ public record Manifest(Dictionary<string, Manifest.Module> Modules)
|
|||||||
|
|
||||||
foreach (var (name, identifierInfo) in module.GetIdentifiers())
|
foreach (var (name, identifierInfo) in module.GetIdentifiers())
|
||||||
{
|
{
|
||||||
if (identifierInfo.Exported && !identifierInfo.External)
|
if (identifierInfo.Kind == Compiler.Module.DefinitionKind.Exported)
|
||||||
{
|
{
|
||||||
identifiers.Add(name, new Module.IdentifierInfo(identifierInfo.Type, identifierInfo.MangledName));
|
identifiers.Add(name, new Module.IdentifierInfo(identifierInfo.Type, identifierInfo.MangledName));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user