From ae534a7a54112bf46dafa0b9eab4d97ba40f3504 Mon Sep 17 00:00:00 2001 From: nub31 Date: Wed, 2 Jul 2025 23:39:44 +0200 Subject: [PATCH] ... --- example/src/main.nub | 9 ++-- src/compiler/Generation/QBE/QBEGenerator.cs | 54 ++++++++++++++++++++- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/example/src/main.nub b/example/src/main.nub index 85d54d5..a77314f 100644 --- a/example/src/main.nub +++ b/example/src/main.nub @@ -8,14 +8,11 @@ struct Human export func main(args: []cstring): i64 { - let x: Human + let x: cstring - x = alloc Human { - name = "John" - age = 32 - } + x = "john" - c::puts(x.name) + c::puts(x) return 0 } diff --git a/src/compiler/Generation/QBE/QBEGenerator.cs b/src/compiler/Generation/QBE/QBEGenerator.cs index 793c46e..49c743e 100644 --- a/src/compiler/Generation/QBE/QBEGenerator.cs +++ b/src/compiler/Generation/QBE/QBEGenerator.cs @@ -264,6 +264,56 @@ public static class QBEGenerator 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) { @@ -1248,6 +1298,7 @@ public static class QBEGenerator foreach (var parameter in funcCall.Parameters) { var result = EmitUnwrap(EmitExpression(parameter)); + var copy = EmitCopy(parameter.Type, result); var qbeType = parameter.Type switch { @@ -1274,7 +1325,8 @@ public static class QBEGenerator NubStringType => "l", _ => 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));