This commit is contained in:
nub31
2025-09-09 15:37:45 +02:00
parent 14619fd678
commit ab77588b72
4 changed files with 14 additions and 17 deletions

View File

@@ -10,4 +10,5 @@ func main(args: []cstring): i64
func test(test: u32) func test(test: u32)
{ {
puts("test")
} }

View File

@@ -207,9 +207,9 @@ public class QBEGenerator
EmitStructInitializer(structInitializer, destinationLValue); EmitStructInitializer(structInitializer, destinationLValue);
return true; return true;
} }
case InterfaceInitializerNode interfaceInitializer: case ConvertToInterfaceNode convertToInterface:
{ {
EmitInterfaceInitializer(interfaceInitializer, destinationLValue); EmitConvertToInterface(convertToInterface, destinationLValue);
return true; return true;
} }
case LiteralNode { Kind: LiteralKind.String } literal: case LiteralNode { Kind: LiteralKind.String } literal:
@@ -266,7 +266,6 @@ public class QBEGenerator
{ {
case ArrayInitializerNode: case ArrayInitializerNode:
case StructInitializerNode: case StructInitializerNode:
case InterfaceInitializerNode:
case LiteralNode { Kind: LiteralKind.String }: case LiteralNode { Kind: LiteralKind.String }:
{ {
destination = EmitExpression(source); destination = EmitExpression(source);
@@ -599,7 +598,7 @@ public class QBEGenerator
BinaryExpressionNode binary => EmitBinaryExpression(binary), BinaryExpressionNode binary => EmitBinaryExpression(binary),
FuncCallNode funcCall => EmitFuncCall(funcCall), FuncCallNode funcCall => EmitFuncCall(funcCall),
InterfaceFuncCallNode interfaceFuncCall => EmitInterfaceFuncCall(interfaceFuncCall), InterfaceFuncCallNode interfaceFuncCall => EmitInterfaceFuncCall(interfaceFuncCall),
InterfaceInitializerNode interfaceInitializer => EmitInterfaceInitializer(interfaceInitializer), ConvertToInterfaceNode convertToInterface => EmitConvertToInterface(convertToInterface),
ConvertIntNode convertInt => EmitConvertInt(convertInt), ConvertIntNode convertInt => EmitConvertInt(convertInt),
ConvertFloatNode convertFloat => EmitConvertFloat(convertFloat), ConvertFloatNode convertFloat => EmitConvertFloat(convertFloat),
ExternFuncIdentNode externFuncIdent => EmitExternFuncIdent(externFuncIdent), ExternFuncIdentNode externFuncIdent => EmitExternFuncIdent(externFuncIdent),
@@ -680,10 +679,7 @@ public class QBEGenerator
return addressOf switch return addressOf switch
{ {
ArrayIndexAccessNode arrayIndexAccess => EmitAddressOfArrayIndexAccess(arrayIndexAccess), ArrayIndexAccessNode arrayIndexAccess => EmitAddressOfArrayIndexAccess(arrayIndexAccess),
ArrayInitializerNode arrayInitializer => EmitArrayInitializer(arrayInitializer),
InterfaceInitializerNode interfaceInitializer => EmitInterfaceInitializer(interfaceInitializer),
StructFieldAccessNode structFieldAccess => EmitAddressOfStructFieldAccess(structFieldAccess), StructFieldAccessNode structFieldAccess => EmitAddressOfStructFieldAccess(structFieldAccess),
StructInitializerNode structInitializer => EmitStructInitializer(structInitializer),
VariableIdentNode variableIdent => EmitAddressOfVariableIdent(variableIdent), VariableIdentNode variableIdent => EmitAddressOfVariableIdent(variableIdent),
_ => throw new ArgumentOutOfRangeException(nameof(addressOf)) _ => throw new ArgumentOutOfRangeException(nameof(addressOf))
}; };
@@ -1088,14 +1084,14 @@ public class QBEGenerator
} }
} }
private string EmitInterfaceInitializer(InterfaceInitializerNode interfaceInitializer, string? destination = null) private string EmitConvertToInterface(ConvertToInterfaceNode convertToInterface, string? destination = null)
{ {
var implementation = EmitExpression(interfaceInitializer.Implementation); var implementation = EmitExpression(convertToInterface.Implementation);
var vtableOffset = 0; var vtableOffset = 0;
foreach (var interfaceImplementation in interfaceInitializer.StructType.InterfaceImplementations) foreach (var interfaceImplementation in convertToInterface.StructType.InterfaceImplementations)
{ {
if (interfaceImplementation == interfaceInitializer.InterfaceType) if (interfaceImplementation == convertToInterface.InterfaceType)
{ {
break; break;
} }
@@ -1106,11 +1102,11 @@ public class QBEGenerator
if (destination == null) if (destination == null)
{ {
destination = TmpName(); destination = TmpName();
_writer.Indented($"{destination} =l alloc8 {SizeOf(interfaceInitializer.InterfaceType)}"); _writer.Indented($"{destination} =l alloc8 {SizeOf(convertToInterface.InterfaceType)}");
} }
var interfaceVtablePointer = TmpName(); var interfaceVtablePointer = TmpName();
_writer.Indented($"{interfaceVtablePointer} =l add {StructVtableName(interfaceInitializer.StructType.Name)}, {vtableOffset}"); _writer.Indented($"{interfaceVtablePointer} =l add {StructVtableName(convertToInterface.StructType.Name)}, {vtableOffset}");
_writer.Indented($"storel {interfaceVtablePointer}, {destination}"); _writer.Indented($"storel {interfaceVtablePointer}, {destination}");
var objectPointer = TmpName(); var objectPointer = TmpName();

View File

@@ -45,7 +45,7 @@ public record LocalFuncIdentNode(TypeNode Type, string Name) : RValueExpressionN
public record ExternFuncIdentNode(TypeNode Type, string Name) : RValueExpressionNode(Type); public record ExternFuncIdentNode(TypeNode Type, string Name) : RValueExpressionNode(Type);
public record ArrayInitializerNode(TypeNode Type, ExpressionNode Capacity, TypeNode ElementType) : LValueExpressionNode(Type); public record ArrayInitializerNode(TypeNode Type, ExpressionNode Capacity, TypeNode ElementType) : RValueExpressionNode(Type);
public record ArrayIndexAccessNode(TypeNode Type, ExpressionNode Target, ExpressionNode Index) : LValueExpressionNode(Type); public record ArrayIndexAccessNode(TypeNode Type, ExpressionNode Target, ExpressionNode Index) : LValueExpressionNode(Type);
@@ -55,11 +55,11 @@ public record LiteralNode(TypeNode Type, string Value, LiteralKind Kind) : RValu
public record StructFieldAccessNode(TypeNode Type, StructTypeNode StructType, ExpressionNode Target, string Field) : LValueExpressionNode(Type); public record StructFieldAccessNode(TypeNode Type, StructTypeNode StructType, ExpressionNode Target, string Field) : LValueExpressionNode(Type);
public record StructInitializerNode(StructTypeNode StructType, Dictionary<string, ExpressionNode> Initializers) : LValueExpressionNode(StructType); public record StructInitializerNode(StructTypeNode StructType, Dictionary<string, ExpressionNode> Initializers) : RValueExpressionNode(StructType);
public record DereferenceNode(TypeNode Type, ExpressionNode Expression) : RValueExpressionNode(Type); public record DereferenceNode(TypeNode Type, ExpressionNode Expression) : RValueExpressionNode(Type);
public record InterfaceInitializerNode(TypeNode Type, InterfaceTypeNode InterfaceType, StructTypeNode StructType, ExpressionNode Implementation) : LValueExpressionNode(Type); public record ConvertToInterfaceNode(TypeNode Type, InterfaceTypeNode InterfaceType, StructTypeNode StructType, ExpressionNode Implementation) : RValueExpressionNode(Type);
public record ConvertIntNode(TypeNode Type, ExpressionNode Value, IntTypeNode ValueType, IntTypeNode TargetType) : RValueExpressionNode(Type); public record ConvertIntNode(TypeNode Type, ExpressionNode Value, IntTypeNode ValueType, IntTypeNode TargetType) : RValueExpressionNode(Type);

View File

@@ -293,7 +293,7 @@ public sealed class TypeChecker
if (result.Type is StructTypeNode structType && expectedType is InterfaceTypeNode interfaceType) if (result.Type is StructTypeNode structType && expectedType is InterfaceTypeNode interfaceType)
{ {
return new InterfaceInitializerNode(interfaceType, interfaceType, structType, result); return new ConvertToInterfaceNode(interfaceType, interfaceType, structType, result);
} }
if (result.Type is IntTypeNode sourceIntType && expectedType is IntTypeNode targetIntType) if (result.Type is IntTypeNode sourceIntType && expectedType is IntTypeNode targetIntType)