This commit is contained in:
nub31
2025-06-14 18:13:24 +02:00
parent 2a5a1dd4ce
commit 4a3de81d97
2 changed files with 87 additions and 82 deletions

View File

@@ -110,8 +110,8 @@ foreach (var resourceName in runtimeResources)
await using var stream = assembly.GetManifestResourceStream(resourceName); await using var stream = assembly.GetManifestResourceStream(resourceName);
if (stream == null) if (stream == null)
{ {
Console.Error.WriteLine($"Warning: Could not load embedded resource {resourceName}"); Console.Error.WriteLine($"Could not load embedded resource {resourceName}");
continue; return 1;
} }
using var reader = new StreamReader(stream); using var reader = new StreamReader(stream);

View File

@@ -90,86 +90,86 @@ public static class QBEGenerator
private static string QBEStore(NubType type) private static string QBEStore(NubType type)
{ {
return $"store{type switch return type switch
{ {
NubArrayType => "l", NubArrayType => "storel",
NubPointerType => "l", NubPointerType => "storel",
NubPrimitiveType primitiveType => primitiveType.Kind switch NubPrimitiveType primitiveType => primitiveType.Kind switch
{ {
PrimitiveTypeKind.I64 => "l", PrimitiveTypeKind.I64 => "storel",
PrimitiveTypeKind.I32 => "w", PrimitiveTypeKind.I32 => "storew",
PrimitiveTypeKind.I16 => "h", PrimitiveTypeKind.I16 => "storeh",
PrimitiveTypeKind.I8 => "b", PrimitiveTypeKind.I8 => "storeb",
PrimitiveTypeKind.U64 => "l", PrimitiveTypeKind.U64 => "storel",
PrimitiveTypeKind.U32 => "w", PrimitiveTypeKind.U32 => "storew",
PrimitiveTypeKind.U16 => "h", PrimitiveTypeKind.U16 => "storeh",
PrimitiveTypeKind.U8 => "b", PrimitiveTypeKind.U8 => "storeb",
PrimitiveTypeKind.F64 => "d", PrimitiveTypeKind.F64 => "stored",
PrimitiveTypeKind.F32 => "s", PrimitiveTypeKind.F32 => "stores",
PrimitiveTypeKind.Bool => "w", PrimitiveTypeKind.Bool => "storew",
_ => throw new UnreachableException() _ => throw new UnreachableException()
}, },
NubStructType => "l", NubStructType => "storel",
NubFixedArrayType => "l", NubFixedArrayType => "storel",
NubFuncType => "l", NubFuncType => "storel",
_ => throw new NotSupportedException($"'{type}' type cannot be used in store instructions") _ => throw new NotSupportedException($"'{type}' type cannot be used in store instructions")
}}"; };
} }
private static string QBELoad(NubType type) private static string QBELoad(NubType type)
{ {
return $"load{type switch return type switch
{ {
NubArrayType => "l", NubArrayType => "loadl",
NubPointerType => "l", NubPointerType => "loadl",
NubPrimitiveType primitiveType => primitiveType.Kind switch NubPrimitiveType primitiveType => primitiveType.Kind switch
{ {
PrimitiveTypeKind.I64 => "l", PrimitiveTypeKind.I64 => "loadl",
PrimitiveTypeKind.I32 => "w", PrimitiveTypeKind.I32 => "loadw",
PrimitiveTypeKind.I16 => "sh", PrimitiveTypeKind.I16 => "loadsh",
PrimitiveTypeKind.I8 => "sb", PrimitiveTypeKind.I8 => "loadsb",
PrimitiveTypeKind.U64 => "l", PrimitiveTypeKind.U64 => "loadl",
PrimitiveTypeKind.U32 => "w", PrimitiveTypeKind.U32 => "loadw",
PrimitiveTypeKind.U16 => "uh", PrimitiveTypeKind.U16 => "loaduh",
PrimitiveTypeKind.U8 => "ub", PrimitiveTypeKind.U8 => "loadub",
PrimitiveTypeKind.F64 => "d", PrimitiveTypeKind.F64 => "loadd",
PrimitiveTypeKind.F32 => "s", PrimitiveTypeKind.F32 => "loads",
PrimitiveTypeKind.Bool => "w", PrimitiveTypeKind.Bool => "loadw",
_ => throw new UnreachableException() _ => throw new UnreachableException()
}, },
NubStructType => "l", NubStructType => "loadl",
NubFixedArrayType => "l", NubFixedArrayType => "loadl",
NubFuncType => "l", NubFuncType => "loadl",
_ => throw new NotSupportedException($"'{type}' type cannot be used in load instructions") _ => throw new NotSupportedException($"'{type}' type cannot be used in load instructions")
}}"; };
} }
private static string QBEAssign(NubType type) private static string QBEAssign(NubType type)
{ {
return $"={type switch return type switch
{ {
NubArrayType => "l", NubArrayType => "=l",
NubPointerType => "l", NubPointerType => "=l",
NubPrimitiveType primitiveType => primitiveType.Kind switch NubPrimitiveType primitiveType => primitiveType.Kind switch
{ {
PrimitiveTypeKind.I64 => "l", PrimitiveTypeKind.I64 => "=l",
PrimitiveTypeKind.I32 => "w", PrimitiveTypeKind.I32 => "=w",
PrimitiveTypeKind.I16 => "w", PrimitiveTypeKind.I16 => "=w",
PrimitiveTypeKind.I8 => "w", PrimitiveTypeKind.I8 => "=w",
PrimitiveTypeKind.U64 => "l", PrimitiveTypeKind.U64 => "=l",
PrimitiveTypeKind.U32 => "w", PrimitiveTypeKind.U32 => "=w",
PrimitiveTypeKind.U16 => "w", PrimitiveTypeKind.U16 => "=w",
PrimitiveTypeKind.U8 => "w", PrimitiveTypeKind.U8 => "=w",
PrimitiveTypeKind.F64 => "d", PrimitiveTypeKind.F64 => "=d",
PrimitiveTypeKind.F32 => "s", PrimitiveTypeKind.F32 => "=s",
PrimitiveTypeKind.Bool => "w", PrimitiveTypeKind.Bool => "=w",
_ => throw new UnreachableException() _ => throw new UnreachableException()
}, },
NubStructType => "l", NubStructType => "=l",
NubFixedArrayType => "l", NubFixedArrayType => "=l",
NubFuncType => "l", NubFuncType => "=l",
_ => throw new NotSupportedException($"'{type}' type cannot be used in variables") _ => throw new NotSupportedException($"'{type}' type cannot be used in variables")
}}"; };
} }
private static int AlignmentOf(NubType type) private static int AlignmentOf(NubType type)
@@ -349,30 +349,35 @@ public static class QBEGenerator
_builder.Append(name); _builder.Append(name);
var parameterStrings = parameters.Select(parameter => $"{parameter.Type switch var parameterStrings = parameters.Select(parameter =>
{ {
NubArrayType => "l", var qbeType = parameter.Type switch
NubPointerType => "l",
NubPrimitiveType primitiveType => primitiveType.Kind switch
{ {
PrimitiveTypeKind.I64 => "l", NubArrayType => "l",
PrimitiveTypeKind.I32 => "w", NubPointerType => "l",
PrimitiveTypeKind.I16 => "sh", NubPrimitiveType primitiveType => primitiveType.Kind switch
PrimitiveTypeKind.I8 => "sb", {
PrimitiveTypeKind.U64 => "l", PrimitiveTypeKind.I64 => "l",
PrimitiveTypeKind.U32 => "w", PrimitiveTypeKind.I32 => "w",
PrimitiveTypeKind.U16 => "uh", PrimitiveTypeKind.I16 => "sh",
PrimitiveTypeKind.U8 => "ub", PrimitiveTypeKind.I8 => "sb",
PrimitiveTypeKind.F64 => "d", PrimitiveTypeKind.U64 => "l",
PrimitiveTypeKind.F32 => "s", PrimitiveTypeKind.U32 => "w",
PrimitiveTypeKind.Bool => "w", PrimitiveTypeKind.U16 => "uh",
_ => throw new UnreachableException() PrimitiveTypeKind.U8 => "ub",
}, PrimitiveTypeKind.F64 => "d",
NubStructType structType => $":{structType.Namespace}_{structType.Name}", PrimitiveTypeKind.F32 => "s",
NubFixedArrayType => "l", PrimitiveTypeKind.Bool => "w",
NubFuncType => "l", _ => throw new UnreachableException()
_ => throw new NotSupportedException($"'{parameter.Type}' type cannot be used as a function parameter type") },
}} %{parameter.Name}"); NubStructType structType => $":{structType.Namespace}_{structType.Name}",
NubFixedArrayType => "l",
NubFuncType => "l",
_ => throw new NotSupportedException($"'{parameter.Type}' type cannot be used as a function parameter type")
};
return $"{qbeType} %{parameter.Name}";
});
_builder.AppendLine($"({string.Join(", ", parameterStrings)}) {{"); _builder.AppendLine($"({string.Join(", ", parameterStrings)}) {{");
_builder.AppendLine("@start"); _builder.AppendLine("@start");
@@ -427,7 +432,7 @@ public static class QBEGenerator
_builder.Append($"type :{structDefinition.Namespace}_{structDefinition.Name} = {{ "); _builder.Append($"type :{structDefinition.Namespace}_{structDefinition.Name} = {{ ");
foreach (var structDefinitionField in structDefinition.Fields) foreach (var structDefinitionField in structDefinition.Fields)
{ {
var fieldQbeType = structDefinitionField.Type switch var qbeType = structDefinitionField.Type switch
{ {
NubArrayType => "l", NubArrayType => "l",
NubPointerType => "l", NubPointerType => "l",
@@ -451,7 +456,7 @@ public static class QBEGenerator
NubFuncType => "l", NubFuncType => "l",
_ => throw new NotSupportedException($"'{structDefinitionField.Type}' type cannot be used in structs") _ => throw new NotSupportedException($"'{structDefinitionField.Type}' type cannot be used in structs")
}; };
_builder.Append(fieldQbeType + ", "); _builder.Append(qbeType + ", ");
} }
_builder.AppendLine("}"); _builder.AppendLine("}");
} }
@@ -1286,7 +1291,7 @@ public static class QBEGenerator
var parameter = funcCall.Parameters[i]; var parameter = funcCall.Parameters[i];
var result = GenerateExpression(parameter); var result = GenerateExpression(parameter);
var qbeParameterType = parameter.Type switch var qbeType = parameter.Type switch
{ {
NubArrayType => "l", NubArrayType => "l",
NubPointerType => "l", NubPointerType => "l",
@@ -1310,7 +1315,7 @@ public static class QBEGenerator
NubFuncType => "l", NubFuncType => "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($"{qbeParameterType} {result}"); parameterStrings.Add($"{qbeType} {result}");
} }
string funcTarget; string funcTarget;