This commit is contained in:
nub31
2025-07-03 17:55:48 +02:00
parent ceab1c15da
commit 51c352465b
2 changed files with 66 additions and 82 deletions

View File

@@ -195,105 +195,86 @@ public static class QBEGenerator
return size; return size;
} }
private static void EmitCopyInto(NubType type, string source, string destination) private static void EmitCopyInto(NubType type, string source, string destinationPointer)
{ {
switch (type) switch (type)
{ {
case NubArrayType nubArrayType: case NubComplexType complexType:
{ {
var size = EmitArraySizeInBytes(nubArrayType, source); var size = complexType switch
var buffer = VarName(); {
_builder.AppendLine($" {buffer} =l alloc8 {size}"); NubArrayType nubArrayType => EmitArraySizeInBytes(nubArrayType, source),
EmitMemcpy(source, buffer, size); NubCStringType => EmitCStringSizeInBytes(source),
EmitStore(type, buffer, destination); NubStringType => EmitStringSizeInBytes(source),
break; NubStructType nubStructType => SizeOf(nubStructType).ToString(),
} _ => throw new ArgumentOutOfRangeException(nameof(complexType))
};
switch (complexType)
{
case NubArrayType:
case NubCStringType: case NubCStringType:
{
var size = EmitCStringSizeInBytes(source);
var buffer = VarName();
_builder.AppendLine($" {buffer} =l alloc8 {size}");
EmitMemcpy(source, buffer, size);
EmitStore(type, buffer, destination);
break;
}
case NubStringType: case NubStringType:
{ {
var size = EmitStringSizeInBytes(source);
var buffer = VarName(); var buffer = VarName();
_builder.AppendLine($" {buffer} =l alloc8 {size}"); _builder.AppendLine($" {buffer} =l alloc8 {size}");
EmitMemcpy(source, buffer, size); EmitMemcpy(source, buffer, size);
EmitStore(type, buffer, destination); EmitStore(type, buffer, destinationPointer);
break; return;
} }
case NubStructType nubStructType: case NubStructType:
{ {
var size = SizeOf(nubStructType); EmitMemcpy(source, destinationPointer, size);
EmitMemcpy(source, destination, size.ToString()); return;
break;
} }
case NubPointerType:
case NubPrimitiveType:
{
EmitStore(type, source, destination);
break;
}
case NubVoidType:
case NubFuncType:
case NubAnyType:
throw new NotSupportedException($"Cannot copy type '{type}'");
default: default:
{
throw new ArgumentOutOfRangeException(nameof(complexType));
}
}
}
case NubSimpleType simpleType:
{
EmitStore(simpleType, source, destinationPointer);
return;
}
default:
{
throw new ArgumentOutOfRangeException(nameof(type)); throw new ArgumentOutOfRangeException(nameof(type));
} }
} }
}
private static string EmitCopy(NubType type, string source) private static string EmitCopy(NubType type, string source)
{ {
var destination = VarName();
switch (type) switch (type)
{ {
case NubArrayType nubArrayType: case NubComplexType complexType:
{ {
var size = EmitArraySizeInBytes(nubArrayType, source); var destination = VarName();
var size = complexType switch
{
NubArrayType nubArrayType => EmitArraySizeInBytes(nubArrayType, source),
NubCStringType => EmitCStringSizeInBytes(source),
NubStringType => EmitStringSizeInBytes(source),
NubStructType nubStructType => SizeOf(nubStructType).ToString(),
_ => throw new ArgumentOutOfRangeException(nameof(complexType))
};
_builder.AppendLine($" {destination} =l alloc8 {size}"); _builder.AppendLine($" {destination} =l alloc8 {size}");
EmitMemcpy(source, destination, size); EmitMemcpy(source, destination, size);
break; return destination;
} }
case NubCStringType: case NubSimpleType:
{
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:
{ {
return source; return source;
} }
case NubVoidType:
case NubFuncType:
case NubAnyType:
throw new NotSupportedException($"Cannot copy type '{type}'");
default: default:
{
throw new ArgumentOutOfRangeException(nameof(type)); throw new ArgumentOutOfRangeException(nameof(type));
} }
}
return destination;
} }
private static string QBEAssign(NubType type) private static string QBEAssign(NubType type)

View File

@@ -29,7 +29,10 @@ public abstract class NubType
public abstract override string ToString(); public abstract override string ToString();
} }
public class NubCStringType : NubType public abstract class NubComplexType : NubType;
public abstract class NubSimpleType : NubType;
public class NubCStringType : NubComplexType
{ {
public override bool Equals(object? obj) public override bool Equals(object? obj)
{ {
@@ -47,7 +50,7 @@ public class NubCStringType : NubType
} }
} }
public class NubStringType : NubType public class NubStringType : NubComplexType
{ {
public override bool Equals(object? obj) public override bool Equals(object? obj)
{ {
@@ -65,7 +68,7 @@ public class NubStringType : NubType
} }
} }
public class NubFuncType(NubType returnType, List<NubType> parameters) : NubType public class NubFuncType(NubType returnType, List<NubType> parameters) : NubSimpleType
{ {
public NubType ReturnType { get; } = returnType; public NubType ReturnType { get; } = returnType;
public List<NubType> Parameters { get; } = parameters; public List<NubType> Parameters { get; } = parameters;
@@ -86,7 +89,7 @@ public class NubFuncType(NubType returnType, List<NubType> parameters) : NubType
} }
} }
public class NubStructType(string @namespace, string name) : NubType public class NubStructType(string @namespace, string name) : NubComplexType
{ {
public string Namespace { get; } = @namespace; public string Namespace { get; } = @namespace;
public string Name { get; } = name; public string Name { get; } = name;
@@ -107,7 +110,7 @@ public class NubStructType(string @namespace, string name) : NubType
} }
} }
public class NubPointerType(NubType baseType) : NubType public class NubPointerType(NubType baseType) : NubSimpleType
{ {
public NubType BaseType { get; } = baseType; public NubType BaseType { get; } = baseType;
@@ -127,7 +130,7 @@ public class NubPointerType(NubType baseType) : NubType
} }
} }
public class NubArrayType(NubType elementType) : NubType public class NubArrayType(NubType elementType) : NubComplexType
{ {
public NubType ElementType { get; } = elementType; public NubType ElementType { get; } = elementType;
@@ -152,7 +155,7 @@ public class NubArrayType(NubType elementType) : NubType
} }
} }
public class NubAnyType : NubType public class NubAnyType : NubSimpleType
{ {
public override string ToString() => "any"; public override string ToString() => "any";
@@ -167,7 +170,7 @@ public class NubAnyType : NubType
} }
} }
public class NubVoidType : NubType public class NubVoidType : NubSimpleType
{ {
public override string ToString() => "void"; public override string ToString() => "void";
@@ -182,7 +185,7 @@ public class NubVoidType : NubType
} }
} }
public class NubPrimitiveType(PrimitiveTypeKind kind) : NubType public class NubPrimitiveType(PrimitiveTypeKind kind) : NubSimpleType
{ {
public PrimitiveTypeKind Kind { get; } = kind; public PrimitiveTypeKind Kind { get; } = kind;