This commit is contained in:
nub31
2025-07-02 23:39:44 +02:00
parent 28c5cd79f5
commit ae534a7a54
2 changed files with 56 additions and 7 deletions

View File

@@ -8,14 +8,11 @@ struct Human
export func main(args: []cstring): i64 export func main(args: []cstring): i64
{ {
let x: Human let x: cstring
x = alloc Human { x = "john"
name = "John"
age = 32
}
c::puts(x.name) c::puts(x)
return 0 return 0
} }

View File

@@ -264,6 +264,56 @@ public static class QBEGenerator
throw new ArgumentOutOfRangeException(nameof(type)); throw new ArgumentOutOfRangeException(nameof(type));
} }
} }
private static string EmitCopy(NubType type, string source)
{
var destination = VarName();
switch (type)
{
case NubArrayType nubArrayType:
{
var size = EmitArraySizeInBytes(nubArrayType, source);
_builder.AppendLine($" {destination} =l alloc8 {size}");
EmitMemcpy(source, destination, size);
break;
}
case NubCStringType:
{
var size = EmitCStringSizeInBytes(source);
_builder.AppendLine($" {destination} =l alloc8 {size}");
EmitMemcpy(source, destination, size);
break;
}
case NubStringType:
{
var size = EmitStringSizeInBytes(source);
_builder.AppendLine($" {destination} =l alloc8 {size}");
EmitMemcpy(source, destination, size);
break;
}
case NubStructType nubStructType:
{
var size = SizeOf(nubStructType);
_builder.AppendLine($" {destination} =l alloc8 {size}");
EmitMemcpy(source, destination, size.ToString());
break;
}
case NubPointerType:
case NubPrimitiveType:
{
_builder.AppendLine($" {destination} {QBEAssign(type)} {source}");
break;
}
case NubVoidType:
case NubFuncType:
case NubAnyType:
throw new NotSupportedException($"Cannot copy type '{type}'");
default:
throw new ArgumentOutOfRangeException(nameof(type));
}
return destination;
}
private static string QBEAssign(NubType type) private static string QBEAssign(NubType type)
{ {
@@ -1248,6 +1298,7 @@ public static class QBEGenerator
foreach (var parameter in funcCall.Parameters) foreach (var parameter in funcCall.Parameters)
{ {
var result = EmitUnwrap(EmitExpression(parameter)); var result = EmitUnwrap(EmitExpression(parameter));
var copy = EmitCopy(parameter.Type, result);
var qbeType = parameter.Type switch var qbeType = parameter.Type switch
{ {
@@ -1274,7 +1325,8 @@ public static class QBEGenerator
NubStringType => "l", NubStringType => "l",
_ => throw new NotSupportedException($"'{parameter.Type}' type cannot be used in function calls") _ => throw new NotSupportedException($"'{parameter.Type}' type cannot be used in function calls")
}; };
parameterStrings.Add($"{qbeType} {result}");
parameterStrings.Add($"{qbeType} {copy}");
} }
var funcPointer = EmitUnwrap(EmitExpression(funcCall.Expression)); var funcPointer = EmitUnwrap(EmitExpression(funcCall.Expression));