Compare commits
2 Commits
17e754fc6e
...
llvm
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1923ed5a78 | ||
|
|
1dd14c8a77 |
@@ -632,4 +632,12 @@ public class ConstArrayInitializerNode(List<Token> tokens, NubType type, List<Ex
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class EnumTypeIntermediateNode(List<Token> tokens, NubType type) : ExpressionNode(tokens, type)
|
||||||
|
{
|
||||||
|
public override IEnumerable<Node> Children()
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@@ -837,6 +837,11 @@ public sealed class TypeChecker
|
|||||||
return new ModuleFuncIdentifierNode(expression.Tokens, type, expression.ModuleToken, expression.NameToken, function.ExternSymbolToken);
|
return new ModuleFuncIdentifierNode(expression.Tokens, type, expression.ModuleToken, expression.NameToken, function.ExternSymbolToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (module.TryResolveEnum(expression.NameToken, out var enumType, out var _))
|
||||||
|
{
|
||||||
|
return new EnumTypeIntermediateNode(expression.Tokens, enumType);
|
||||||
|
}
|
||||||
|
|
||||||
throw new CompileException(Diagnostic
|
throw new CompileException(Diagnostic
|
||||||
.Error($"Module {expression.ModuleToken.Value} does not export a member named {expression.NameToken.Value}")
|
.Error($"Module {expression.ModuleToken.Value} does not export a member named {expression.NameToken.Value}")
|
||||||
.At(expression, _syntaxTree.Tokens)
|
.At(expression, _syntaxTree.Tokens)
|
||||||
@@ -920,6 +925,29 @@ public sealed class TypeChecker
|
|||||||
|
|
||||||
return new StructFieldAccessNode(expression.Tokens, field.Type, target, expression.MemberToken);
|
return new StructFieldAccessNode(expression.Tokens, field.Type, target, expression.MemberToken);
|
||||||
}
|
}
|
||||||
|
case NubEnumType enumType:
|
||||||
|
{
|
||||||
|
if (!enumType.Members.TryGetValue(expression.MemberToken.Value, out var value))
|
||||||
|
{
|
||||||
|
throw new CompileException(Diagnostic
|
||||||
|
.Error($"Enum {target.Type} does not have a member with the name {expression.MemberToken.Value}")
|
||||||
|
.At(expression, _syntaxTree.Tokens)
|
||||||
|
.Build());
|
||||||
|
}
|
||||||
|
|
||||||
|
return (enumType.UnderlyingType.Width, enumType.UnderlyingType.Signed) switch
|
||||||
|
{
|
||||||
|
(8, false) => new U8LiteralNode(expression.Tokens, (byte)value),
|
||||||
|
(16, false) => new U16LiteralNode(expression.Tokens, (ushort)value),
|
||||||
|
(32, false) => new U32LiteralNode(expression.Tokens, (uint)value),
|
||||||
|
(64, false) => new U64LiteralNode(expression.Tokens, value),
|
||||||
|
(8, true) => new I8LiteralNode(expression.Tokens, (sbyte)value),
|
||||||
|
(16, true) => new I16LiteralNode(expression.Tokens, (short)value),
|
||||||
|
(32, true) => new I32LiteralNode(expression.Tokens, (int)value),
|
||||||
|
(64, true) => new I64LiteralNode(expression.Tokens, (long)value),
|
||||||
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
|
};
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
throw new CompileException(Diagnostic
|
throw new CompileException(Diagnostic
|
||||||
@@ -1062,7 +1090,7 @@ public sealed class TypeChecker
|
|||||||
return structType;
|
return structType;
|
||||||
}
|
}
|
||||||
|
|
||||||
var enumType = module.EnumTypes.GetValueOrDefault(customType.NameToken.Value);
|
var enumType = module.EnumTypes.FirstOrDefault(x => x.Name == customType.NameToken.Value);
|
||||||
if (enumType != null)
|
if (enumType != null)
|
||||||
{
|
{
|
||||||
return enumType;
|
return enumType;
|
||||||
|
|||||||
@@ -16,6 +16,9 @@ public class LlvmSharpGenerator
|
|||||||
private readonly Dictionary<string, LLVMValueRef> _functions = new();
|
private readonly Dictionary<string, LLVMValueRef> _functions = new();
|
||||||
private readonly Dictionary<string, LLVMValueRef> _locals = new();
|
private readonly Dictionary<string, LLVMValueRef> _locals = new();
|
||||||
private readonly Stack<(LLVMBasicBlockRef breakBlock, LLVMBasicBlockRef continueBlock)> _loopStack = new();
|
private readonly Stack<(LLVMBasicBlockRef breakBlock, LLVMBasicBlockRef continueBlock)> _loopStack = new();
|
||||||
|
private readonly Stack<Scope> _scopes = new();
|
||||||
|
|
||||||
|
private Scope CurrentScope => _scopes.Peek();
|
||||||
|
|
||||||
public void Emit(List<TopLevelNode> topLevelNodes, ModuleRepository repository, string sourceFileName, string outputPath)
|
public void Emit(List<TopLevelNode> topLevelNodes, ModuleRepository repository, string sourceFileName, string outputPath)
|
||||||
{
|
{
|
||||||
@@ -32,50 +35,97 @@ public class LlvmSharpGenerator
|
|||||||
_functions.Clear();
|
_functions.Clear();
|
||||||
_locals.Clear();
|
_locals.Clear();
|
||||||
_loopStack.Clear();
|
_loopStack.Clear();
|
||||||
|
_scopes.Clear();
|
||||||
|
|
||||||
var stringType = _context.CreateNamedStruct("nub.string");
|
var stringType = _context.CreateNamedStruct("nub.string");
|
||||||
stringType.StructSetBody([LLVMTypeRef.Int64, LLVMTypeRef.CreatePointer(LLVMTypeRef.Int8, 0)], false);
|
stringType.StructSetBody([LLVMTypeRef.Int64, LLVMTypeRef.CreatePointer(LLVMTypeRef.Int8, 0)], false);
|
||||||
_structTypes["nub.string"] = stringType;
|
_structTypes["nub.string"] = stringType;
|
||||||
|
|
||||||
|
// note(nub31): Declare all structs and functions
|
||||||
foreach (var module in repository.GetAll())
|
foreach (var module in repository.GetAll())
|
||||||
{
|
{
|
||||||
foreach (var structType in module.StructTypes)
|
foreach (var structType in module.StructTypes)
|
||||||
{
|
{
|
||||||
var structName = StructName(structType.Module, structType.Name);
|
var llvmStructType = _context.CreateNamedStruct(StructName(structType.Module, structType.Name));
|
||||||
var llvmStructType = _context.CreateNamedStruct(structName);
|
llvmStructType.StructSetBody(structType.Fields.Select(f => MapType(f.Type)).ToArray(), structType.Packed);
|
||||||
_structTypes[structName] = llvmStructType;
|
_structTypes[StructName(structType.Module, structType.Name)] = llvmStructType;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var module in repository.GetAll())
|
var constructorType = LLVMTypeRef.CreateFunction(LLVMTypeRef.Void, [LLVMTypeRef.CreatePointer(llvmStructType, 0)]);
|
||||||
{
|
var constructor = _llvmModule.AddFunction(StructConstructorName(structType.Module, structType.Name), constructorType);
|
||||||
foreach (var structType in module.StructTypes)
|
|
||||||
{
|
_functions[StructConstructorName(structType.Module, structType.Name)] = constructor;
|
||||||
var structName = StructName(structType.Module, structType.Name);
|
}
|
||||||
var llvmStructType = _structTypes[structName];
|
|
||||||
var fieldTypes = structType.Fields.Select(f => MapType(f.Type)).ToArray();
|
|
||||||
llvmStructType.StructSetBody(fieldTypes, false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var module in repository.GetAll())
|
|
||||||
{
|
|
||||||
foreach (var prototype in module.FunctionPrototypes)
|
foreach (var prototype in module.FunctionPrototypes)
|
||||||
{
|
{
|
||||||
CreateFunctionDeclaration(prototype, module.Name);
|
var funcName = FuncName(module.Name, prototype.NameToken.Value, prototype.ExternSymbolToken?.Value);
|
||||||
|
|
||||||
|
var paramTypes = prototype.Parameters.Select(p => MapType(p.Type)).ToArray();
|
||||||
|
var funcType = LLVMTypeRef.CreateFunction(MapType(prototype.ReturnType), paramTypes);
|
||||||
|
var func = _llvmModule.AddFunction(funcName, funcType);
|
||||||
|
|
||||||
|
func.FunctionCallConv = (uint)LLVMCallConv.LLVMCCallConv;
|
||||||
|
|
||||||
|
_functions[funcName] = func;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// note(nub31): Define struct constructors
|
||||||
foreach (var structNode in topLevelNodes.OfType<StructNode>())
|
foreach (var structNode in topLevelNodes.OfType<StructNode>())
|
||||||
{
|
{
|
||||||
EmitStructConstructor(structNode);
|
var structType = _structTypes[StructName(_module, structNode.NameToken.Value)];
|
||||||
|
var constructor = _functions[StructConstructorName(_module, structNode.NameToken.Value)];
|
||||||
|
|
||||||
|
var entryBlock = constructor.AppendBasicBlock("entry");
|
||||||
|
_builder.PositionAtEnd(entryBlock);
|
||||||
|
|
||||||
|
var selfParam = constructor.GetParam(0);
|
||||||
|
selfParam.Name = "self";
|
||||||
|
|
||||||
|
_locals.Clear();
|
||||||
|
|
||||||
|
foreach (var field in structNode.Fields)
|
||||||
|
{
|
||||||
|
if (field.Value == null) continue;
|
||||||
|
|
||||||
|
var index = structNode.StructType.GetFieldIndex(field.NameToken.Value);
|
||||||
|
var fieldPtr = _builder.BuildStructGEP2(structType, selfParam, (uint)index);
|
||||||
|
EmitExpressionInto(field.Value, fieldPtr);
|
||||||
|
}
|
||||||
|
|
||||||
|
_builder.BuildRetVoid();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// note(nub31): Define function bodies
|
||||||
foreach (var funcNode in topLevelNodes.OfType<FuncNode>())
|
foreach (var funcNode in topLevelNodes.OfType<FuncNode>())
|
||||||
{
|
{
|
||||||
if (funcNode.Body != null)
|
if (funcNode.Body == null) continue;
|
||||||
|
|
||||||
|
var funcName = FuncName(_module, funcNode.Prototype.NameToken.Value, funcNode.Prototype.ExternSymbolToken?.Value);
|
||||||
|
var func = _functions[funcName];
|
||||||
|
|
||||||
|
var entryBlock = func.AppendBasicBlock("entry");
|
||||||
|
_builder.PositionAtEnd(entryBlock);
|
||||||
|
|
||||||
|
_locals.Clear();
|
||||||
|
|
||||||
|
for (uint i = 0; i < funcNode.Prototype.Parameters.Count; i++)
|
||||||
{
|
{
|
||||||
EmitFunction(funcNode);
|
var param = func.GetParam(i);
|
||||||
|
var paramNode = funcNode.Prototype.Parameters[(int)i];
|
||||||
|
var alloca = _builder.BuildAlloca(MapType(paramNode.Type), paramNode.NameToken.Value);
|
||||||
|
_builder.BuildStore(param, alloca);
|
||||||
|
_locals[paramNode.NameToken.Value] = alloca;
|
||||||
|
}
|
||||||
|
|
||||||
|
EmitBlock(funcNode.Body);
|
||||||
|
|
||||||
|
if (funcNode.Prototype.ReturnType is NubVoidType)
|
||||||
|
{
|
||||||
|
if (_builder.InsertBlock.Terminator.Handle == IntPtr.Zero)
|
||||||
|
{
|
||||||
|
_builder.BuildRetVoid();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,94 +139,16 @@ public class LlvmSharpGenerator
|
|||||||
_builder.Dispose();
|
_builder.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CreateFunctionDeclaration(FuncPrototypeNode prototype, string moduleName)
|
|
||||||
{
|
|
||||||
var funcName = FuncName(moduleName, prototype.NameToken.Value, prototype.ExternSymbolToken?.Value);
|
|
||||||
|
|
||||||
var paramTypes = prototype.Parameters.Select(p => MapType(p.Type)).ToArray();
|
|
||||||
var returnType = MapType(prototype.ReturnType);
|
|
||||||
|
|
||||||
var funcType = LLVMTypeRef.CreateFunction(returnType, paramTypes);
|
|
||||||
var func = _llvmModule.AddFunction(funcName, funcType);
|
|
||||||
|
|
||||||
func.FunctionCallConv = (uint)LLVMCallConv.LLVMCCallConv;
|
|
||||||
|
|
||||||
for (var i = 0; i < prototype.Parameters.Count; i++)
|
|
||||||
{
|
|
||||||
func.GetParam((uint)i).Name = prototype.Parameters[i].NameToken.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
_functions[funcName] = func;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EmitStructConstructor(StructNode structNode)
|
|
||||||
{
|
|
||||||
var structType = _structTypes[StructName(_module, structNode.NameToken.Value)];
|
|
||||||
var ptrType = LLVMTypeRef.CreatePointer(structType, 0);
|
|
||||||
|
|
||||||
var funcType = LLVMTypeRef.CreateFunction(LLVMTypeRef.Void, [ptrType]);
|
|
||||||
var funcName = StructConstructorName(_module, structNode.NameToken.Value);
|
|
||||||
var func = _llvmModule.AddFunction(funcName, funcType);
|
|
||||||
func.FunctionCallConv = (uint)LLVMCallConv.LLVMCCallConv;
|
|
||||||
|
|
||||||
var entryBlock = func.AppendBasicBlock("entry");
|
|
||||||
_builder.PositionAtEnd(entryBlock);
|
|
||||||
|
|
||||||
var selfParam = func.GetParam(0);
|
|
||||||
selfParam.Name = "self";
|
|
||||||
|
|
||||||
_locals.Clear();
|
|
||||||
|
|
||||||
foreach (var field in structNode.Fields)
|
|
||||||
{
|
|
||||||
if (field.Value != null)
|
|
||||||
{
|
|
||||||
var index = structNode.StructType.GetFieldIndex(field.NameToken.Value);
|
|
||||||
var fieldPtr = _builder.BuildStructGEP2(structType, selfParam, (uint)index);
|
|
||||||
EmitExpressionInto(field.Value, fieldPtr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_builder.BuildRetVoid();
|
|
||||||
_functions[funcName] = func;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EmitFunction(FuncNode funcNode)
|
|
||||||
{
|
|
||||||
var funcName = FuncName(_module, funcNode.Prototype.NameToken.Value, funcNode.Prototype.ExternSymbolToken?.Value);
|
|
||||||
var func = _functions[funcName];
|
|
||||||
|
|
||||||
var entryBlock = func.AppendBasicBlock("entry");
|
|
||||||
_builder.PositionAtEnd(entryBlock);
|
|
||||||
|
|
||||||
_locals.Clear();
|
|
||||||
|
|
||||||
for (uint i = 0; i < funcNode.Prototype.Parameters.Count; i++)
|
|
||||||
{
|
|
||||||
var param = func.GetParam(i);
|
|
||||||
var paramNode = funcNode.Prototype.Parameters[(int)i];
|
|
||||||
var alloca = _builder.BuildAlloca(MapType(paramNode.Type), paramNode.NameToken.Value);
|
|
||||||
_builder.BuildStore(param, alloca);
|
|
||||||
_locals[paramNode.NameToken.Value] = alloca;
|
|
||||||
}
|
|
||||||
|
|
||||||
EmitBlock(funcNode.Body!);
|
|
||||||
|
|
||||||
if (funcNode.Prototype.ReturnType is NubVoidType)
|
|
||||||
{
|
|
||||||
if (_builder.InsertBlock.Terminator.Handle == IntPtr.Zero)
|
|
||||||
{
|
|
||||||
_builder.BuildRetVoid();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EmitBlock(BlockNode blockNode)
|
private void EmitBlock(BlockNode blockNode)
|
||||||
{
|
{
|
||||||
|
_scopes.Push(new Scope());
|
||||||
foreach (var statement in blockNode.Statements)
|
foreach (var statement in blockNode.Statements)
|
||||||
{
|
{
|
||||||
EmitStatement(statement);
|
EmitStatement(statement);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EmitScopeExit();
|
||||||
|
_scopes.Pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void EmitStatement(StatementNode statement)
|
private void EmitStatement(StatementNode statement)
|
||||||
@@ -195,6 +167,13 @@ public class LlvmSharpGenerator
|
|||||||
case ContinueNode:
|
case ContinueNode:
|
||||||
EmitContinue();
|
EmitContinue();
|
||||||
break;
|
break;
|
||||||
|
case DeferNode deferNode:
|
||||||
|
CurrentScope.DeferredActions.Push(() => EmitStatement(deferNode.Statement));
|
||||||
|
break;
|
||||||
|
case ForConstArrayNode forConstArrayNode:
|
||||||
|
throw new NotImplementedException();
|
||||||
|
case ForSliceNode forSliceNode:
|
||||||
|
throw new NotImplementedException();
|
||||||
case IfNode ifNode:
|
case IfNode ifNode:
|
||||||
EmitIf(ifNode);
|
EmitIf(ifNode);
|
||||||
break;
|
break;
|
||||||
@@ -211,7 +190,7 @@ public class LlvmSharpGenerator
|
|||||||
EmitWhile(whileNode);
|
EmitWhile(whileNode);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new NotImplementedException($"Statement type {statement.GetType()} not implemented");
|
throw new ArgumentOutOfRangeException(nameof(statement));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,12 +201,14 @@ public class LlvmSharpGenerator
|
|||||||
_builder.BuildStore(value, targetPtr);
|
_builder.BuildStore(value, targetPtr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo(nub31): Needs to call EmitScopeExit. However, the jump might span multiple scopes, so we must handle that
|
||||||
private void EmitBreak()
|
private void EmitBreak()
|
||||||
{
|
{
|
||||||
var (breakBlock, _) = _loopStack.Peek();
|
var (breakBlock, _) = _loopStack.Peek();
|
||||||
_builder.BuildBr(breakBlock);
|
_builder.BuildBr(breakBlock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo(nub31): Needs to call EmitScopeExit. However, the jump might span multiple scopes, so we must handle that
|
||||||
private void EmitContinue()
|
private void EmitContinue()
|
||||||
{
|
{
|
||||||
var (_, continueBlock) = _loopStack.Peek();
|
var (_, continueBlock) = _loopStack.Peek();
|
||||||
@@ -270,10 +251,12 @@ public class LlvmSharpGenerator
|
|||||||
if (returnNode.Value != null)
|
if (returnNode.Value != null)
|
||||||
{
|
{
|
||||||
var value = EmitExpression(returnNode.Value);
|
var value = EmitExpression(returnNode.Value);
|
||||||
|
EmitScopeExit();
|
||||||
_builder.BuildRet(value);
|
_builder.BuildRet(value);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
EmitScopeExit();
|
||||||
_builder.BuildRetVoid();
|
_builder.BuildRetVoid();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -512,7 +495,10 @@ public class LlvmSharpGenerator
|
|||||||
{
|
{
|
||||||
var funcPtr = EmitExpression(funcCall.Expression);
|
var funcPtr = EmitExpression(funcCall.Expression);
|
||||||
var args = funcCall.Parameters.Select(x => EmitExpression(x)).ToArray();
|
var args = funcCall.Parameters.Select(x => EmitExpression(x)).ToArray();
|
||||||
return _builder.BuildCall2(MapType(funcCall.Expression.Type), funcPtr, args, funcCall.Type is NubVoidType ? "" : "call");
|
|
||||||
|
var functionType = (NubFuncType)funcCall.Expression.Type;
|
||||||
|
var llvmFunctionType = LLVMTypeRef.CreateFunction(MapType(functionType.ReturnType), functionType.Parameters.Select(MapType).ToArray());
|
||||||
|
return _builder.BuildCall2(llvmFunctionType, funcPtr, args, funcCall.Type is NubVoidType ? "" : "call");
|
||||||
}
|
}
|
||||||
|
|
||||||
private LLVMValueRef EmitStructFieldAccess(StructFieldAccessNode field)
|
private LLVMValueRef EmitStructFieldAccess(StructFieldAccessNode field)
|
||||||
@@ -733,13 +719,14 @@ public class LlvmSharpGenerator
|
|||||||
NubBoolType => LLVMTypeRef.Int1,
|
NubBoolType => LLVMTypeRef.Int1,
|
||||||
NubIntType intType => LLVMTypeRef.CreateInt((uint)intType.Width),
|
NubIntType intType => LLVMTypeRef.CreateInt((uint)intType.Width),
|
||||||
NubFloatType floatType => floatType.Width == 32 ? LLVMTypeRef.Float : LLVMTypeRef.Double,
|
NubFloatType floatType => floatType.Width == 32 ? LLVMTypeRef.Float : LLVMTypeRef.Double,
|
||||||
NubFuncType funcType => LLVMTypeRef.CreateFunction(MapType(funcType.ReturnType), funcType.Parameters.Select(MapType).ToArray()),
|
NubFuncType funcType => LLVMTypeRef.CreatePointer(LLVMTypeRef.CreateFunction(MapType(funcType.ReturnType), funcType.Parameters.Select(MapType).ToArray()), 0),
|
||||||
NubPointerType ptrType => LLVMTypeRef.CreatePointer(MapType(ptrType.BaseType), 0),
|
NubPointerType ptrType => LLVMTypeRef.CreatePointer(MapType(ptrType.BaseType), 0),
|
||||||
NubSliceType nubSliceType => MapSliceType(nubSliceType),
|
NubSliceType nubSliceType => MapSliceType(nubSliceType),
|
||||||
NubStringType => _structTypes["nub.string"],
|
NubStringType => _structTypes["nub.string"],
|
||||||
NubArrayType arrType => LLVMTypeRef.CreatePointer(MapType(arrType.ElementType), 0),
|
NubArrayType arrType => LLVMTypeRef.CreatePointer(MapType(arrType.ElementType), 0),
|
||||||
NubConstArrayType constArr => LLVMTypeRef.CreateArray(MapType(constArr.ElementType), (uint)constArr.Size),
|
NubConstArrayType constArr => LLVMTypeRef.CreateArray(MapType(constArr.ElementType), (uint)constArr.Size),
|
||||||
NubStructType structType => _structTypes[StructName(structType.Module, structType.Name)],
|
NubStructType structType => _structTypes[StructName(structType.Module, structType.Name)],
|
||||||
|
NubEnumType enumType => MapType(enumType.UnderlyingType),
|
||||||
NubVoidType => LLVMTypeRef.Void,
|
NubVoidType => LLVMTypeRef.Void,
|
||||||
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
|
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
|
||||||
};
|
};
|
||||||
@@ -778,4 +765,17 @@ public class LlvmSharpGenerator
|
|||||||
|
|
||||||
return $"{module}.{name}";
|
return $"{module}.{name}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void EmitScopeExit()
|
||||||
|
{
|
||||||
|
while (CurrentScope.DeferredActions.TryPop(out var action))
|
||||||
|
{
|
||||||
|
action.Invoke();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Scope
|
||||||
|
{
|
||||||
|
public readonly Stack<Action> DeferredActions = [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -11,7 +11,7 @@ public sealed class ModuleRepository
|
|||||||
public static ModuleRepository Create(List<SyntaxTree> syntaxTrees)
|
public static ModuleRepository Create(List<SyntaxTree> syntaxTrees)
|
||||||
{
|
{
|
||||||
var structTypes = new Dictionary<(string module, string name), NubStructType>();
|
var structTypes = new Dictionary<(string module, string name), NubStructType>();
|
||||||
var enumTypes = new Dictionary<(string module, string name), NubIntType>();
|
var enumTypes = new Dictionary<(string module, string name), NubEnumType>();
|
||||||
|
|
||||||
foreach (var syntaxTree in syntaxTrees)
|
foreach (var syntaxTree in syntaxTrees)
|
||||||
{
|
{
|
||||||
@@ -44,11 +44,27 @@ public sealed class ModuleRepository
|
|||||||
underlyingType ??= new NubIntType(false, 64);
|
underlyingType ??= new NubIntType(false, 64);
|
||||||
|
|
||||||
var key = (module.NameToken.Value, enumSyntax.NameToken.Value);
|
var key = (module.NameToken.Value, enumSyntax.NameToken.Value);
|
||||||
enumTypes.Add(key, underlyingType);
|
|
||||||
|
var memberValues = new Dictionary<string, ulong>();
|
||||||
|
|
||||||
|
ulong currentValue = 0;
|
||||||
|
|
||||||
|
foreach (var member in enumSyntax.Members)
|
||||||
|
{
|
||||||
|
if (member.ValueToken != null)
|
||||||
|
{
|
||||||
|
currentValue = member.ValueToken.AsU64;
|
||||||
|
}
|
||||||
|
|
||||||
|
memberValues[member.NameToken.Value] = currentValue;
|
||||||
|
currentValue++;
|
||||||
|
}
|
||||||
|
|
||||||
|
enumTypes.Add(key, new NubEnumType(module.NameToken.Value, enumSyntax.NameToken.Value, underlyingType, memberValues));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// note(nub31): Since all struct types are now registered, we can safely resolve the field types
|
// note(nub31): Since all struct and enum types are now registered, we can safely resolve the field types
|
||||||
foreach (var syntaxTree in syntaxTrees)
|
foreach (var syntaxTree in syntaxTrees)
|
||||||
{
|
{
|
||||||
var module = syntaxTree.TopLevelSyntaxNodes.OfType<ModuleSyntax>().FirstOrDefault();
|
var module = syntaxTree.TopLevelSyntaxNodes.OfType<ModuleSyntax>().FirstOrDefault();
|
||||||
@@ -90,9 +106,7 @@ public sealed class ModuleRepository
|
|||||||
{
|
{
|
||||||
Name = moduleDecl.NameToken.Value,
|
Name = moduleDecl.NameToken.Value,
|
||||||
StructTypes = structTypes.Where(x => x.Key.module == moduleDecl.NameToken.Value).Select(x => x.Value).ToList(),
|
StructTypes = structTypes.Where(x => x.Key.module == moduleDecl.NameToken.Value).Select(x => x.Value).ToList(),
|
||||||
EnumTypes = enumTypes
|
EnumTypes = enumTypes.Where(x => x.Key.module == moduleDecl.NameToken.Value).Select(x => x.Value).ToList(),
|
||||||
.Where(x => x.Key.module == moduleDecl.NameToken.Value)
|
|
||||||
.ToDictionary(x => x.Key.name, x => x.Value),
|
|
||||||
FunctionPrototypes = functionPrototypes
|
FunctionPrototypes = functionPrototypes
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -167,12 +181,6 @@ public sealed class ModuleRepository
|
|||||||
return module != null;
|
return module != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool TryGet(string name, [NotNullWhen(true)] out Module? module)
|
|
||||||
{
|
|
||||||
module = _modules.GetValueOrDefault(name);
|
|
||||||
return module != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Module> GetAll()
|
public List<Module> GetAll()
|
||||||
{
|
{
|
||||||
return _modules.Values.ToList();
|
return _modules.Values.ToList();
|
||||||
@@ -183,22 +191,7 @@ public sealed class ModuleRepository
|
|||||||
public required string Name { get; init; }
|
public required string Name { get; init; }
|
||||||
public required List<FuncPrototypeNode> FunctionPrototypes { get; init; } = [];
|
public required List<FuncPrototypeNode> FunctionPrototypes { get; init; } = [];
|
||||||
public required List<NubStructType> StructTypes { get; init; } = [];
|
public required List<NubStructType> StructTypes { get; init; } = [];
|
||||||
public required Dictionary<string, NubIntType> EnumTypes { get; init; } = [];
|
public required List<NubEnumType> EnumTypes { get; init; } = [];
|
||||||
|
|
||||||
public bool TryResolveFunc(string name, [NotNullWhen(true)] out FuncPrototypeNode? value, [NotNullWhen(false)] out Diagnostic? diagnostic)
|
|
||||||
{
|
|
||||||
value = FunctionPrototypes.FirstOrDefault(x => x.NameToken.Value == name);
|
|
||||||
|
|
||||||
if (value == null)
|
|
||||||
{
|
|
||||||
value = null;
|
|
||||||
diagnostic = Diagnostic.Error($"Func {name} not found in module {Name}").Build();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
diagnostic = null;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool TryResolveFunc(IdentifierToken name, [NotNullWhen(true)] out FuncPrototypeNode? value, [NotNullWhen(false)] out Diagnostic? diagnostic)
|
public bool TryResolveFunc(IdentifierToken name, [NotNullWhen(true)] out FuncPrototypeNode? value, [NotNullWhen(false)] out Diagnostic? diagnostic)
|
||||||
{
|
{
|
||||||
@@ -215,31 +208,6 @@ public sealed class ModuleRepository
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FuncPrototypeNode ResolveFunc(IdentifierToken name)
|
|
||||||
{
|
|
||||||
if (!TryResolveFunc(name, out var value, out var diagnostic))
|
|
||||||
{
|
|
||||||
throw new CompileException(diagnostic);
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool TryResolveStruct(string name, [NotNullWhen(true)] out NubStructType? value, [NotNullWhen(false)] out Diagnostic? diagnostic)
|
|
||||||
{
|
|
||||||
value = StructTypes.FirstOrDefault(x => x.Name == name);
|
|
||||||
|
|
||||||
if (value == null)
|
|
||||||
{
|
|
||||||
value = null;
|
|
||||||
diagnostic = Diagnostic.Error($"Struct {name} not found in module {Name}").Build();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
diagnostic = null;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool TryResolveStruct(IdentifierToken name, [NotNullWhen(true)] out NubStructType? value, [NotNullWhen(false)] out Diagnostic? diagnostic)
|
public bool TryResolveStruct(IdentifierToken name, [NotNullWhen(true)] out NubStructType? value, [NotNullWhen(false)] out Diagnostic? diagnostic)
|
||||||
{
|
{
|
||||||
value = StructTypes.FirstOrDefault(x => x.Name == name.Value);
|
value = StructTypes.FirstOrDefault(x => x.Name == name.Value);
|
||||||
@@ -255,34 +223,9 @@ public sealed class ModuleRepository
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NubStructType ResolveStruct(IdentifierToken name)
|
public bool TryResolveEnum(IdentifierToken name, [NotNullWhen(true)] out NubEnumType? value, [NotNullWhen(false)] out Diagnostic? diagnostic)
|
||||||
{
|
{
|
||||||
if (!TryResolveStruct(name, out var value, out var diagnostic))
|
value = EnumTypes.FirstOrDefault(x => x.Name == name.Value);
|
||||||
{
|
|
||||||
throw new CompileException(diagnostic);
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool TryResolveEnum(string name, [NotNullWhen(true)] out NubIntType? value, [NotNullWhen(false)] out Diagnostic? diagnostic)
|
|
||||||
{
|
|
||||||
value = EnumTypes.GetValueOrDefault(name);
|
|
||||||
|
|
||||||
if (value == null)
|
|
||||||
{
|
|
||||||
value = null;
|
|
||||||
diagnostic = Diagnostic.Error($"Enum {name} not found in module {Name}").Build();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
diagnostic = null;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool TryResolveEnum(IdentifierToken name, [NotNullWhen(true)] out NubIntType? value, [NotNullWhen(false)] out Diagnostic? diagnostic)
|
|
||||||
{
|
|
||||||
value = EnumTypes.GetValueOrDefault(name.Value);
|
|
||||||
|
|
||||||
if (value == null)
|
if (value == null)
|
||||||
{
|
{
|
||||||
@@ -294,15 +237,5 @@ public sealed class ModuleRepository
|
|||||||
diagnostic = null;
|
diagnostic = null;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NubIntType ResolveEnum(IdentifierToken name)
|
|
||||||
{
|
|
||||||
if (!TryResolveEnum(name, out var value, out var diagnostic))
|
|
||||||
{
|
|
||||||
throw new CompileException(diagnostic);
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -174,7 +174,7 @@ public sealed class Parser
|
|||||||
type = ParseType();
|
type = ParseType();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<EnumFieldSyntax> fields = [];
|
List<EnumMemberSyntax> fields = [];
|
||||||
|
|
||||||
ExpectSymbol(Symbol.OpenBrace);
|
ExpectSymbol(Symbol.OpenBrace);
|
||||||
|
|
||||||
@@ -197,7 +197,7 @@ public sealed class Parser
|
|||||||
value = intLiteralToken;
|
value = intLiteralToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
fields.Add(new EnumFieldSyntax(GetTokens(memberStartIndex), fieldName, value));
|
fields.Add(new EnumMemberSyntax(GetTokens(memberStartIndex), fieldName, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new EnumSyntax(GetTokens(startIndex), name, exported, type, fields);
|
return new EnumSyntax(GetTokens(startIndex), name, exported, type, fields);
|
||||||
|
|||||||
@@ -20,9 +20,9 @@ public record StructFieldSyntax(List<Token> Tokens, IdentifierToken NameToken, T
|
|||||||
|
|
||||||
public record StructSyntax(List<Token> Tokens, IdentifierToken NameToken, bool Exported, bool Packed, List<StructFieldSyntax> Fields) : DefinitionSyntax(Tokens, NameToken, Exported);
|
public record StructSyntax(List<Token> Tokens, IdentifierToken NameToken, bool Exported, bool Packed, List<StructFieldSyntax> Fields) : DefinitionSyntax(Tokens, NameToken, Exported);
|
||||||
|
|
||||||
public record EnumFieldSyntax(List<Token> Tokens, IdentifierToken NameToken, IntLiteralToken? ValueToken) : SyntaxNode(Tokens);
|
public record EnumMemberSyntax(List<Token> Tokens, IdentifierToken NameToken, IntLiteralToken? ValueToken) : SyntaxNode(Tokens);
|
||||||
|
|
||||||
public record EnumSyntax(List<Token> Tokens, IdentifierToken NameToken, bool Exported, TypeSyntax? Type, List<EnumFieldSyntax> Fields) : DefinitionSyntax(Tokens, NameToken, Exported);
|
public record EnumSyntax(List<Token> Tokens, IdentifierToken NameToken, bool Exported, TypeSyntax? Type, List<EnumMemberSyntax> Members) : DefinitionSyntax(Tokens, NameToken, Exported);
|
||||||
|
|
||||||
public enum UnaryOperatorSyntax
|
public enum UnaryOperatorSyntax
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -183,6 +183,22 @@ public class NubStructFieldType(string name, NubType type, bool hasDefaultValue)
|
|||||||
public bool HasDefaultValue { get; } = hasDefaultValue;
|
public bool HasDefaultValue { get; } = hasDefaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class NubEnumType(string module, string name, NubIntType underlyingType, Dictionary<string, ulong> members) : NubType
|
||||||
|
{
|
||||||
|
public string Module { get; } = module;
|
||||||
|
public string Name { get; } = name;
|
||||||
|
public NubIntType UnderlyingType { get; } = underlyingType;
|
||||||
|
public Dictionary<string, ulong> Members { get; } = members;
|
||||||
|
|
||||||
|
public override ulong GetSize() => UnderlyingType.GetSize();
|
||||||
|
public override ulong GetAlignment() => UnderlyingType.GetSize();
|
||||||
|
public override bool IsAggregate() => false;
|
||||||
|
|
||||||
|
public override bool Equals(NubType? other) => other is NubEnumType enumType && Name == enumType.Name && Module == enumType.Module;
|
||||||
|
public override int GetHashCode() => HashCode.Combine(typeof(NubEnumType), Module, Name);
|
||||||
|
public override string ToString() => $"{Module}::{Name}";
|
||||||
|
}
|
||||||
|
|
||||||
public class NubSliceType(NubType elementType) : NubType
|
public class NubSliceType(NubType elementType) : NubType
|
||||||
{
|
{
|
||||||
public NubType ElementType { get; } = elementType;
|
public NubType ElementType { get; } = elementType;
|
||||||
|
|||||||
Reference in New Issue
Block a user