This commit is contained in:
nub31
2025-06-15 01:47:10 +02:00
parent 7c05870e24
commit 3f7499d2b9
10 changed files with 63 additions and 50 deletions

View File

@@ -122,6 +122,7 @@ public static class QBEGenerator
NubStructType => "storel",
NubFixedArrayType => "storel",
NubFuncType => "storel",
NubCStringType => "storel",
_ => throw new NotSupportedException($"'{type}' type cannot be used in store instructions")
};
}
@@ -150,6 +151,7 @@ public static class QBEGenerator
NubStructType => "loadl",
NubFixedArrayType => "loadl",
NubFuncType => "loadl",
NubCStringType => "loadl",
_ => throw new NotSupportedException($"'{type}' type cannot be used in load instructions")
};
}
@@ -178,6 +180,7 @@ public static class QBEGenerator
NubStructType => "=l",
NubFixedArrayType => "=l",
NubFuncType => "=l",
NubCStringType => "=l",
_ => throw new NotSupportedException($"'{type}' type cannot be used in variables")
};
}
@@ -204,6 +207,7 @@ public static class QBEGenerator
}
case NubPointerType:
case NubArrayType:
case NubCStringType:
case NubFuncType:
{
return 8;
@@ -263,6 +267,7 @@ public static class QBEGenerator
}
case NubPointerType:
case NubArrayType:
case NubCStringType:
case NubFuncType:
{
return 8;
@@ -300,16 +305,7 @@ public static class QBEGenerator
private static bool IsPointerType(NubType type)
{
return type switch
{
NubPointerType => false,
NubPrimitiveType => false,
NubStructType => true,
NubArrayType => true,
NubFixedArrayType => true,
NubFuncType => false,
_ => throw new ArgumentOutOfRangeException(nameof(type))
};
return type is NubStructType or NubArrayType or NubFixedArrayType;
}
private static void GenerateFuncDefinition(string name, List<FuncParameter> parameters, NubType returnType, BlockNode body, bool exported)
@@ -347,6 +343,7 @@ public static class QBEGenerator
NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()),
NubFixedArrayType => "l",
NubFuncType => "l",
NubCStringType => "l",
_ => throw new NotSupportedException($"'{returnType}' type cannot be used as a function return type")
});
_builder.Append(' ');
@@ -378,6 +375,7 @@ public static class QBEGenerator
NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()),
NubFixedArrayType => "l",
NubFuncType => "l",
NubCStringType => "l",
_ => throw new NotSupportedException($"'{parameter.Type}' type cannot be used as a function parameter type")
};
@@ -459,6 +457,7 @@ public static class QBEGenerator
NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()),
NubFixedArrayType fixedArrayType => $"b {SizeOf(fixedArrayType)}",
NubFuncType => "l",
NubCStringType => "l",
_ => throw new NotSupportedException($"'{structDefinitionField.Type}' type cannot be used in structs")
};
_builder.Append(qbeType + ", ");
@@ -1200,12 +1199,19 @@ public static class QBEGenerator
var item = GenerateExpression(memberAccess.Expression);
switch (memberAccess.Expression.Type)
{
case NubArrayType:
case NubArrayType arrayType:
{
if (memberAccess.Member == "count")
{
return item;
}
if (arrayType.ElementType is NubPrimitiveType { Kind: PrimitiveTypeKind.U8 } && memberAccess.Member == "cstring")
{
var result = VarName();
_builder.AppendLine($" {result} =l call $nub_string_to_cstring(l {item})");
return result;
}
throw new UnreachableException(nameof(memberAccess.Member));
}
@@ -1291,6 +1297,7 @@ public static class QBEGenerator
NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()),
NubFixedArrayType => "l",
NubFuncType => "l",
NubCStringType => "l",
_ => throw new NotSupportedException($"'{parameter.Type}' type cannot be used in function calls")
};
parameterStrings.Add($"{qbeType} {result}");