...
This commit is contained in:
@@ -131,67 +131,139 @@ public static class QBEGenerator
|
||||
return $"$impl{++_implFuncNameIndex}";
|
||||
}
|
||||
|
||||
private static string StructTypeName(BoundNubStructType structType)
|
||||
private static string CustomTypeName(NubCustomType customType)
|
||||
{
|
||||
return $":{structType.Namespace}_{structType.Name}";
|
||||
return CustomTypeName(customType.Namespace, customType.Name);
|
||||
}
|
||||
|
||||
private static string TraitTypeName(BoundNubTraitType traitType)
|
||||
private static string CustomTypeName(string @namespace, string name)
|
||||
{
|
||||
return $":{traitType.Namespace}_{traitType.Name}";
|
||||
return $":{@namespace}_{name}";
|
||||
}
|
||||
|
||||
private static void EmitStore(BoundNubType type, string value, string destination)
|
||||
private static int AlignTo(int offset, int alignment)
|
||||
{
|
||||
var store = type switch
|
||||
return (offset + alignment - 1) & ~(alignment - 1);
|
||||
}
|
||||
|
||||
private static int SizeOf(NubType type)
|
||||
{
|
||||
if (type.IsSimpleType(out var simpleType, out var complexType))
|
||||
{
|
||||
BoundNubComplexType => "storel",
|
||||
BoundNubSimpleType simpleType => simpleType switch
|
||||
return simpleType.StorageSize switch
|
||||
{
|
||||
BoundNubFuncType or BoundNubPointerType => "loadl",
|
||||
BoundNubPrimitiveType primitiveType => primitiveType.Kind switch
|
||||
StorageSize.I64 or StorageSize.U64 or StorageSize.F64 => 8,
|
||||
StorageSize.I32 or StorageSize.U32 or StorageSize.F32 => 4,
|
||||
StorageSize.I16 or StorageSize.U16 => 2,
|
||||
StorageSize.I8 or StorageSize.U8 => 1,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(simpleType.StorageSize))
|
||||
};
|
||||
}
|
||||
|
||||
// Only custom types such as structs/traits have known sizes for now. For objects other than custom types, we store pointers
|
||||
if (complexType is not NubCustomType customType)
|
||||
{
|
||||
return 8;
|
||||
}
|
||||
|
||||
switch (customType.Kind(_definitionTable))
|
||||
{
|
||||
case CustomTypeKind.Struct:
|
||||
{
|
||||
var structDef = _definitionTable.LookupStruct(customType.Namespace, customType.Name);
|
||||
var size = 0;
|
||||
var maxAlignment = 1;
|
||||
|
||||
foreach (var field in structDef.Fields)
|
||||
{
|
||||
PrimitiveTypeKind.I64 or PrimitiveTypeKind.U64 => "storel",
|
||||
PrimitiveTypeKind.I32 or PrimitiveTypeKind.U32 => "storew",
|
||||
PrimitiveTypeKind.I16 or PrimitiveTypeKind.U16 => "storeh",
|
||||
PrimitiveTypeKind.I8 or PrimitiveTypeKind.U8 or PrimitiveTypeKind.Bool => "storeb",
|
||||
PrimitiveTypeKind.F64 => "stored",
|
||||
PrimitiveTypeKind.F32 => "stores",
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
},
|
||||
_ => throw new ArgumentOutOfRangeException($"'{type}' type cannot be used in store instructions")
|
||||
},
|
||||
_ => throw new UnreachableException()
|
||||
};
|
||||
var fieldAlignment = field.Type.Alignment(_definitionTable);
|
||||
maxAlignment = Math.Max(maxAlignment, fieldAlignment);
|
||||
|
||||
size = AlignTo(size, fieldAlignment);
|
||||
size += SizeOf(field.Type);
|
||||
}
|
||||
|
||||
return AlignTo(size, maxAlignment);
|
||||
}
|
||||
case CustomTypeKind.Trait:
|
||||
{
|
||||
return 16;
|
||||
}
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
private static int OffsetOf(BoundStructNode structDefinition, string member)
|
||||
{
|
||||
var offset = 0;
|
||||
|
||||
foreach (var field in structDefinition.Fields)
|
||||
{
|
||||
if (field.Name == member)
|
||||
{
|
||||
return offset;
|
||||
}
|
||||
|
||||
var fieldAlignment = field.Type.Alignment(_definitionTable);
|
||||
|
||||
offset = AlignTo(offset, fieldAlignment);
|
||||
offset += SizeOf(field.Type);
|
||||
}
|
||||
|
||||
throw new UnreachableException($"Member '{member}' not found in struct");
|
||||
}
|
||||
|
||||
private static void EmitStore(NubType type, string value, string destination)
|
||||
{
|
||||
string store;
|
||||
|
||||
if (type.IsSimpleType(out var simpleType, out _))
|
||||
{
|
||||
store = simpleType.StorageSize switch
|
||||
{
|
||||
StorageSize.I8 or StorageSize.U8 => "storeb",
|
||||
StorageSize.I16 or StorageSize.U16 => "storeh",
|
||||
StorageSize.I32 or StorageSize.U32 => "storew",
|
||||
StorageSize.I64 or StorageSize.U64 => "storel",
|
||||
StorageSize.F32 => "stores",
|
||||
StorageSize.F64 => "stored",
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(simpleType.StorageSize))
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
store = "storel";
|
||||
}
|
||||
|
||||
_writer.Indented($"{store} {value}, {destination}");
|
||||
}
|
||||
|
||||
private static Val EmitLoad(BoundNubType type, string from)
|
||||
private static Val EmitLoad(NubType type, string from)
|
||||
{
|
||||
var into = TmpName();
|
||||
var load = type switch
|
||||
string load;
|
||||
|
||||
if (type.IsSimpleType(out var simpleType, out _))
|
||||
{
|
||||
BoundNubComplexType => "loadl",
|
||||
BoundNubSimpleType simpleType => simpleType switch
|
||||
load = simpleType.StorageSize switch
|
||||
{
|
||||
BoundNubFuncType or BoundNubPointerType => "loadl",
|
||||
BoundNubPrimitiveType primitiveType => primitiveType.Kind switch
|
||||
{
|
||||
PrimitiveTypeKind.I64 or PrimitiveTypeKind.U64 => "loadl",
|
||||
PrimitiveTypeKind.I32 or PrimitiveTypeKind.U32 => "loadw",
|
||||
PrimitiveTypeKind.I16 => "loadsh",
|
||||
PrimitiveTypeKind.I8 => "loadsb",
|
||||
PrimitiveTypeKind.U16 => "loaduh",
|
||||
PrimitiveTypeKind.U8 or PrimitiveTypeKind.Bool => "loadub",
|
||||
PrimitiveTypeKind.F64 => "loadd",
|
||||
PrimitiveTypeKind.F32 => "loads",
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
},
|
||||
_ => throw new ArgumentOutOfRangeException($"'{type}' type cannot be used in load instructions")
|
||||
},
|
||||
_ => throw new UnreachableException()
|
||||
};
|
||||
StorageSize.I64 or StorageSize.U64 => "loadl",
|
||||
StorageSize.I32 or StorageSize.U32 => "loadw",
|
||||
StorageSize.I16 => "loadsh",
|
||||
StorageSize.I8 => "loadsb",
|
||||
StorageSize.U16 => "loaduh",
|
||||
StorageSize.U8 => "loadub",
|
||||
StorageSize.F64 => "loadd",
|
||||
StorageSize.F32 => "loads",
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(simpleType.StorageSize))
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
load = "loadl";
|
||||
}
|
||||
|
||||
var into = TmpName();
|
||||
|
||||
_writer.Indented($"{into} {QBEAssign(type)} {load} {from}");
|
||||
|
||||
@@ -203,7 +275,7 @@ public static class QBEGenerator
|
||||
_writer.Indented($"call $nub_memcpy(l {source}, l {destination}, l {length})");
|
||||
}
|
||||
|
||||
private static string EmitArraySizeInBytes(BoundNubArrayType type, string array)
|
||||
private static string EmitArraySizeInBytes(NubArrayType type, string array)
|
||||
{
|
||||
var size = TmpName();
|
||||
_writer.Indented($"{size} =l loadl {array}");
|
||||
@@ -265,52 +337,30 @@ public static class QBEGenerator
|
||||
|
||||
var value = EmitUnwrap(EmitExpression(source));
|
||||
|
||||
switch (source.Type)
|
||||
if (source.Type.IsSimpleType(out var simpleType, out var complexType))
|
||||
{
|
||||
case BoundNubComplexType complexType:
|
||||
EmitStore(simpleType, value, destinationPointer);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (complexType is NubCustomType customType)
|
||||
{
|
||||
EmitMemcpy(value, destinationPointer, SizeOf(customType).ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
var size = complexType switch
|
||||
{
|
||||
BoundNubArrayType arrayType => EmitArraySizeInBytes(arrayType, value),
|
||||
BoundNubCStringType => EmitCStringSizeInBytes(value),
|
||||
BoundNubStringType => EmitStringSizeInBytes(value),
|
||||
BoundNubStructType structType => SizeOf(structType).ToString(),
|
||||
BoundNubTraitType traitType => SizeOf(traitType).ToString(),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(complexType))
|
||||
NubArrayType arrayType => EmitArraySizeInBytes(arrayType, value),
|
||||
NubCStringType => EmitCStringSizeInBytes(value),
|
||||
NubStringType => EmitStringSizeInBytes(value),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(source.Type))
|
||||
};
|
||||
|
||||
switch (complexType)
|
||||
{
|
||||
case BoundNubArrayType:
|
||||
case BoundNubCStringType:
|
||||
case BoundNubStringType:
|
||||
{
|
||||
var buffer = TmpName();
|
||||
_writer.Indented($"{buffer} =l alloc8 {size}");
|
||||
EmitMemcpy(value, buffer, size);
|
||||
EmitStore(source.Type, buffer, destinationPointer);
|
||||
return;
|
||||
}
|
||||
case BoundNubStructType:
|
||||
case BoundNubTraitType:
|
||||
{
|
||||
EmitMemcpy(value, destinationPointer, size);
|
||||
return;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(complexType));
|
||||
}
|
||||
}
|
||||
}
|
||||
case BoundNubSimpleType simpleType:
|
||||
{
|
||||
EmitStore(simpleType, value, destinationPointer);
|
||||
return;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(source.Type));
|
||||
var buffer = TmpName();
|
||||
_writer.Indented($"{buffer} =l alloc8 {size}");
|
||||
EmitMemcpy(value, buffer, size);
|
||||
EmitStore(complexType, buffer, destinationPointer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -342,46 +392,36 @@ public static class QBEGenerator
|
||||
|
||||
var value = EmitUnwrap(EmitExpression(source));
|
||||
|
||||
switch (source.Type)
|
||||
if (source.Type.IsSimpleType(out _, out var complexType))
|
||||
{
|
||||
case BoundNubComplexType complexType:
|
||||
{
|
||||
var destination = TmpName();
|
||||
|
||||
var size = complexType switch
|
||||
{
|
||||
BoundNubArrayType arrayType => EmitArraySizeInBytes(arrayType, value),
|
||||
BoundNubCStringType => EmitCStringSizeInBytes(value),
|
||||
BoundNubStringType => EmitStringSizeInBytes(value),
|
||||
BoundNubStructType structType => SizeOf(structType).ToString(),
|
||||
BoundNubTraitType traitType => SizeOf(traitType).ToString(),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(complexType))
|
||||
};
|
||||
|
||||
_writer.Indented($"{destination} =l alloc8 {size}");
|
||||
EmitMemcpy(value, destination, size);
|
||||
return destination;
|
||||
}
|
||||
case BoundNubSimpleType:
|
||||
{
|
||||
return value;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(source.Type));
|
||||
}
|
||||
// Simple types are passed in registers and are therefore always copied
|
||||
return value;
|
||||
}
|
||||
|
||||
var size = complexType switch
|
||||
{
|
||||
NubArrayType arrayType => EmitArraySizeInBytes(arrayType, value),
|
||||
NubCStringType => EmitCStringSizeInBytes(value),
|
||||
NubStringType => EmitStringSizeInBytes(value),
|
||||
NubCustomType customType => SizeOf(customType).ToString(),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(source.Type))
|
||||
};
|
||||
|
||||
var destination = TmpName();
|
||||
_writer.Indented($"{destination} =l alloc8 {size}");
|
||||
EmitMemcpy(value, destination, size);
|
||||
return destination;
|
||||
}
|
||||
|
||||
private static string QBEAssign(BoundNubType type)
|
||||
private static string QBEAssign(NubType type)
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
BoundNubComplexType => "=l",
|
||||
BoundNubSimpleType simpleType => simpleType switch
|
||||
NubComplexType => "=l",
|
||||
NubSimpleType simpleType => simpleType switch
|
||||
{
|
||||
BoundNubFuncType or BoundNubFuncType => "=l",
|
||||
BoundNubPrimitiveType primitiveType => primitiveType.Kind switch
|
||||
NubFuncType or NubFuncType => "=l",
|
||||
NubPrimitiveType primitiveType => primitiveType.Kind switch
|
||||
{
|
||||
PrimitiveTypeKind.I64 or PrimitiveTypeKind.U64 => "=l",
|
||||
PrimitiveTypeKind.I32 or PrimitiveTypeKind.U32 => "=w",
|
||||
@@ -397,139 +437,34 @@ public static class QBEGenerator
|
||||
};
|
||||
}
|
||||
|
||||
private static int AlignmentOf(BoundNubType type)
|
||||
// Utility to create QBE type names for function parameters and return types
|
||||
private static string FuncQBETypeName(NubType type)
|
||||
{
|
||||
switch (type)
|
||||
if (type.IsSimpleType(out var simpleType, out var complexType))
|
||||
{
|
||||
case BoundNubPrimitiveType primitiveType:
|
||||
return simpleType.StorageSize switch
|
||||
{
|
||||
return primitiveType.Kind switch
|
||||
{
|
||||
PrimitiveTypeKind.I64 or PrimitiveTypeKind.U64 or PrimitiveTypeKind.F64 => 8,
|
||||
PrimitiveTypeKind.I32 or PrimitiveTypeKind.U32 or PrimitiveTypeKind.F32 => 4,
|
||||
PrimitiveTypeKind.I16 or PrimitiveTypeKind.U16 => 2,
|
||||
PrimitiveTypeKind.I8 or PrimitiveTypeKind.U8 or PrimitiveTypeKind.Bool => 1,
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
};
|
||||
}
|
||||
case BoundNubStructType structType:
|
||||
{
|
||||
var structDef = _definitionTable.LookupStruct(structType.Namespace, structType.Name);
|
||||
return structDef.Fields.Max(f => AlignmentOf(f.Type));
|
||||
}
|
||||
case BoundNubTraitType:
|
||||
case BoundNubPointerType:
|
||||
case BoundNubArrayType:
|
||||
case BoundNubCStringType:
|
||||
case BoundNubStringType:
|
||||
case BoundNubFuncType:
|
||||
return 8;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
private static int AlignTo(int offset, int alignment)
|
||||
{
|
||||
return (offset + alignment - 1) & ~(alignment - 1);
|
||||
}
|
||||
|
||||
private static int SizeOf(BoundNubType type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case BoundNubPrimitiveType primitiveType:
|
||||
{
|
||||
return primitiveType.Kind switch
|
||||
{
|
||||
PrimitiveTypeKind.I64 or PrimitiveTypeKind.U64 or PrimitiveTypeKind.F64 => 8,
|
||||
PrimitiveTypeKind.I32 or PrimitiveTypeKind.U32 or PrimitiveTypeKind.F32 => 4,
|
||||
PrimitiveTypeKind.I16 or PrimitiveTypeKind.U16 => 2,
|
||||
PrimitiveTypeKind.I8 or PrimitiveTypeKind.U8 or PrimitiveTypeKind.Bool => 1,
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
};
|
||||
}
|
||||
case BoundNubStructType structType:
|
||||
{
|
||||
var structDef = _definitionTable.LookupStruct(structType.Namespace, structType.Name);
|
||||
var size = 0;
|
||||
var maxAlignment = 1;
|
||||
|
||||
foreach (var field in structDef.Fields)
|
||||
{
|
||||
var fieldAlignment = AlignmentOf(field.Type);
|
||||
maxAlignment = Math.Max(maxAlignment, fieldAlignment);
|
||||
|
||||
size = AlignTo(size, fieldAlignment);
|
||||
size += SizeOf(field.Type);
|
||||
}
|
||||
|
||||
size = AlignTo(size, maxAlignment);
|
||||
|
||||
return size;
|
||||
}
|
||||
case BoundNubTraitType:
|
||||
return 16;
|
||||
case BoundNubPointerType:
|
||||
case BoundNubArrayType:
|
||||
case BoundNubCStringType:
|
||||
case BoundNubStringType:
|
||||
case BoundNubFuncType:
|
||||
return 8;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
private static int OffsetOf(BoundStructNode structDefinition, string member)
|
||||
{
|
||||
var offset = 0;
|
||||
|
||||
foreach (var field in structDefinition.Fields)
|
||||
{
|
||||
if (field.Name == member)
|
||||
{
|
||||
return offset;
|
||||
}
|
||||
|
||||
var fieldAlignment = AlignmentOf(field.Type);
|
||||
|
||||
offset = AlignTo(offset, fieldAlignment);
|
||||
offset += SizeOf(field.Type);
|
||||
StorageSize.I64 or StorageSize.U64 => "l",
|
||||
StorageSize.I32 or StorageSize.U32 => "w",
|
||||
StorageSize.I16 => "sh",
|
||||
StorageSize.I8 => "sb",
|
||||
StorageSize.U16 => "uh",
|
||||
StorageSize.U8 => "ub",
|
||||
StorageSize.F64 => "d",
|
||||
StorageSize.F32 => "s",
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
};
|
||||
}
|
||||
|
||||
throw new UnreachableException($"Member '{member}' not found in struct");
|
||||
}
|
||||
|
||||
private static string FuncQBETypeName(BoundNubType type)
|
||||
{
|
||||
return type switch
|
||||
if (complexType is NubCustomType customType)
|
||||
{
|
||||
BoundNubStructType structType => StructTypeName(structType),
|
||||
BoundNubTraitType traitType => TraitTypeName(traitType),
|
||||
BoundNubComplexType => "l",
|
||||
BoundNubSimpleType simpleType => simpleType switch
|
||||
{
|
||||
BoundNubPointerType or BoundNubFuncType => "l",
|
||||
BoundNubPrimitiveType primitiveType => primitiveType.Kind switch
|
||||
{
|
||||
PrimitiveTypeKind.I64 or PrimitiveTypeKind.U64 => "l",
|
||||
PrimitiveTypeKind.I32 or PrimitiveTypeKind.U32 => "w",
|
||||
PrimitiveTypeKind.I16 => "sh",
|
||||
PrimitiveTypeKind.I8 => "sb",
|
||||
PrimitiveTypeKind.U16 => "uh",
|
||||
PrimitiveTypeKind.U8 or PrimitiveTypeKind.Bool => "ub",
|
||||
PrimitiveTypeKind.F64 => "d",
|
||||
PrimitiveTypeKind.F32 => "s",
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
},
|
||||
_ => throw new NotSupportedException($"'{type}' type cannot be used as a function definition type")
|
||||
},
|
||||
_ => throw new UnreachableException()
|
||||
};
|
||||
return CustomTypeName(customType);
|
||||
}
|
||||
|
||||
return "l";
|
||||
}
|
||||
|
||||
private static void EmitFuncDefinition(BoundNode debugNode, string name, List<BoundFuncParameterNode> parameters, BoundNubType returnType, BoundBlock body, bool exported)
|
||||
private static void EmitFuncDefinition(BoundNode debugNode, string name, List<BoundFuncParameterNode> parameters, NubType returnType, BoundBlock body, bool exported)
|
||||
{
|
||||
_variables.Clear();
|
||||
_variableScopes.Clear();
|
||||
@@ -544,7 +479,7 @@ public static class QBEGenerator
|
||||
}
|
||||
|
||||
builder.Append("function ");
|
||||
if (returnType is not BoundNubVoidType)
|
||||
if (returnType is not NubVoidType)
|
||||
{
|
||||
builder.Append(FuncQBETypeName(returnType) + ' ');
|
||||
}
|
||||
@@ -564,7 +499,7 @@ public static class QBEGenerator
|
||||
|
||||
if (body.Statements.LastOrDefault() is not BoundReturnNode)
|
||||
{
|
||||
if (returnType is BoundNubVoidType)
|
||||
if (returnType is NubVoidType)
|
||||
{
|
||||
_writer.Indented("ret");
|
||||
}
|
||||
@@ -575,7 +510,7 @@ public static class QBEGenerator
|
||||
|
||||
private static void EmitStructDefinition(BoundStructNode structDef)
|
||||
{
|
||||
_writer.WriteLine($"type {StructTypeName(new BoundNubStructType(structDef.Namespace, structDef.Name))} = {{ ");
|
||||
_writer.WriteLine($"type {CustomTypeName(structDef.Namespace, structDef.Name)} = {{ ");
|
||||
|
||||
var types = new Dictionary<string, string>();
|
||||
|
||||
@@ -596,34 +531,32 @@ public static class QBEGenerator
|
||||
|
||||
string StructDefQBEType(BoundStructFieldNode field)
|
||||
{
|
||||
return field.Type switch
|
||||
if (field.Type.IsSimpleType(out var simpleType, out var complexType))
|
||||
{
|
||||
BoundNubStructType structType => StructTypeName(structType),
|
||||
BoundNubTraitType traitType => TraitTypeName(traitType),
|
||||
BoundNubComplexType => "l",
|
||||
BoundNubSimpleType simpleType => simpleType switch
|
||||
return simpleType.StorageSize switch
|
||||
{
|
||||
BoundNubPointerType or BoundNubFuncType => "l",
|
||||
BoundNubPrimitiveType primitiveType => primitiveType.Kind switch
|
||||
{
|
||||
PrimitiveTypeKind.I64 or PrimitiveTypeKind.U64 => "l",
|
||||
PrimitiveTypeKind.I32 or PrimitiveTypeKind.U32 => "w",
|
||||
PrimitiveTypeKind.I16 or PrimitiveTypeKind.U16 => "h",
|
||||
PrimitiveTypeKind.I8 or PrimitiveTypeKind.U8 or PrimitiveTypeKind.Bool => "b",
|
||||
PrimitiveTypeKind.F64 => "d",
|
||||
PrimitiveTypeKind.F32 => "s",
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
},
|
||||
_ => throw new NotSupportedException($"'{field.Type}' type cannot be used in structs")
|
||||
},
|
||||
_ => throw new UnreachableException()
|
||||
};
|
||||
StorageSize.I64 or StorageSize.U64 => "l",
|
||||
StorageSize.I32 or StorageSize.U32 => "w",
|
||||
StorageSize.I16 or StorageSize.U16 => "h",
|
||||
StorageSize.I8 or StorageSize.U8 => "b",
|
||||
StorageSize.F64 => "d",
|
||||
StorageSize.F32 => "s",
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
};
|
||||
}
|
||||
|
||||
if (complexType is NubCustomType customType)
|
||||
{
|
||||
return CustomTypeName(customType);
|
||||
}
|
||||
|
||||
return "l";
|
||||
}
|
||||
}
|
||||
|
||||
private static void EmitTraitVTable(BoundTraitNode traitDef)
|
||||
{
|
||||
_writer.WriteLine($"type {TraitTypeName(new BoundNubTraitType(traitDef.Namespace, traitDef.Name))} = {{");
|
||||
_writer.WriteLine($"type {CustomTypeName(traitDef.Namespace, traitDef.Name)} = {{");
|
||||
|
||||
foreach (var func in traitDef.Functions)
|
||||
{
|
||||
@@ -643,10 +576,10 @@ public static class QBEGenerator
|
||||
EmitAssignment(assignment);
|
||||
break;
|
||||
case BoundBreakNode @break:
|
||||
EmitBreak(@break);
|
||||
EmitBreak();
|
||||
break;
|
||||
case BoundContinueNode @continue:
|
||||
EmitContinue(@continue);
|
||||
EmitContinue();
|
||||
break;
|
||||
case BoundIfNode ifStatement:
|
||||
EmitIf(ifStatement);
|
||||
@@ -700,13 +633,13 @@ public static class QBEGenerator
|
||||
_codeIsReachable = true;
|
||||
}
|
||||
|
||||
private static void EmitBreak(BoundBreakNode @break)
|
||||
private static void EmitBreak()
|
||||
{
|
||||
_writer.Indented($"jmp {_breakLabels.Peek()}");
|
||||
_codeIsReachable = false;
|
||||
}
|
||||
|
||||
private static void EmitContinue(BoundContinueNode @continue)
|
||||
private static void EmitContinue()
|
||||
{
|
||||
_writer.Indented($"jmp {_continueLabels.Peek()}");
|
||||
_codeIsReachable = false;
|
||||
@@ -821,7 +754,7 @@ public static class QBEGenerator
|
||||
var array = EmitUnwrap(EmitExpression(arrayIndexAccess.Target));
|
||||
var index = EmitUnwrap(EmitExpression(arrayIndexAccess.Index));
|
||||
|
||||
var elementType = ((BoundNubArrayType)arrayIndexAccess.Target.Type).ElementType;
|
||||
var elementType = ((NubArrayType)arrayIndexAccess.Target.Type).ElementType;
|
||||
|
||||
var pointer = TmpName();
|
||||
_writer.Indented($"{pointer} =l mul {index}, {SizeOf(elementType)}");
|
||||
@@ -900,7 +833,7 @@ public static class QBEGenerator
|
||||
return new Val(outputName, binaryExpression.Type, ValKind.Direct);
|
||||
}
|
||||
|
||||
private static string EmitBinaryInstructionFor(BinaryExpressionOperator op, BoundNubType type, string left, string right)
|
||||
private static string EmitBinaryInstructionFor(BinaryExpressionOperator op, NubType type, string left, string right)
|
||||
{
|
||||
if (op is
|
||||
BinaryExpressionOperator.Equal or
|
||||
@@ -912,47 +845,41 @@ public static class QBEGenerator
|
||||
{
|
||||
char suffix;
|
||||
|
||||
if (type.Is8BitInteger)
|
||||
if (!type.IsSimpleType(out var simpleType, out _))
|
||||
{
|
||||
if (type.IsSignedInteger)
|
||||
{
|
||||
throw new NotSupportedException("Binary operations is only supported for simple types.");
|
||||
}
|
||||
|
||||
switch (simpleType.StorageSize)
|
||||
{
|
||||
case StorageSize.I8:
|
||||
_writer.Indented($"{left} =w extsb {left}");
|
||||
_writer.Indented($"{right} =w extsb {right}");
|
||||
}
|
||||
else
|
||||
{
|
||||
suffix = 'w';
|
||||
break;
|
||||
case StorageSize.U8:
|
||||
_writer.Indented($"{left} =w extub {left}");
|
||||
_writer.Indented($"{right} =w extub {right}");
|
||||
}
|
||||
|
||||
suffix = 'w';
|
||||
}
|
||||
else if (type.Is16BitInteger)
|
||||
{
|
||||
if (type.IsSignedInteger)
|
||||
{
|
||||
suffix = 'w';
|
||||
break;
|
||||
case StorageSize.I16:
|
||||
_writer.Indented($"{left} =w extsh {left}");
|
||||
_writer.Indented($"{right} =w extsh {right}");
|
||||
}
|
||||
else
|
||||
{
|
||||
suffix = 'w';
|
||||
break;
|
||||
case StorageSize.U16:
|
||||
_writer.Indented($"{left} =w extuh {left}");
|
||||
_writer.Indented($"{right} =w extuh {right}");
|
||||
}
|
||||
|
||||
suffix = 'w';
|
||||
}
|
||||
else if (type.Is32BitInteger)
|
||||
{
|
||||
suffix = 'w';
|
||||
}
|
||||
else if (type.Is64BitInteger)
|
||||
{
|
||||
suffix = 'l';
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException($"Unsupported type '{type}' for binary operator '{op}'");
|
||||
suffix = 'w';
|
||||
break;
|
||||
case StorageSize.I32 or StorageSize.U32:
|
||||
suffix = 'w';
|
||||
break;
|
||||
case StorageSize.I64 or StorageSize.U64:
|
||||
suffix = 'l';
|
||||
break;
|
||||
default:
|
||||
throw new NotSupportedException($"Unsupported type '{simpleType}' for binary operator '{op}'");
|
||||
}
|
||||
|
||||
if (op is BinaryExpressionOperator.Equal)
|
||||
@@ -967,18 +894,14 @@ public static class QBEGenerator
|
||||
|
||||
string sign;
|
||||
|
||||
if (type.IsSignedInteger)
|
||||
if (simpleType is NubPrimitiveType { Kind: PrimitiveTypeKind.I8 or PrimitiveTypeKind.I16 or PrimitiveTypeKind.I32 or PrimitiveTypeKind.I64 })
|
||||
{
|
||||
sign = "s";
|
||||
}
|
||||
else if (type.IsUnsignedInteger)
|
||||
else if (simpleType is NubPrimitiveType { Kind: PrimitiveTypeKind.U8 or PrimitiveTypeKind.U16 or PrimitiveTypeKind.U32 or PrimitiveTypeKind.U64 })
|
||||
{
|
||||
sign = "u";
|
||||
}
|
||||
else if (type.IsFloat)
|
||||
{
|
||||
sign = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException($"Unsupported type '{type}' for binary operator '{op}'");
|
||||
@@ -1027,21 +950,21 @@ public static class QBEGenerator
|
||||
{
|
||||
case LiteralKind.Integer:
|
||||
{
|
||||
if (literal.Type.IsFloat32)
|
||||
if (literal.Type is NubPrimitiveType { Kind: PrimitiveTypeKind.F32 })
|
||||
{
|
||||
var value = float.Parse(literal.Literal, CultureInfo.InvariantCulture);
|
||||
var bits = BitConverter.SingleToInt32Bits(value);
|
||||
return new Val(bits.ToString(), literal.Type, ValKind.Direct);
|
||||
}
|
||||
|
||||
if (literal.Type.IsFloat64)
|
||||
if (literal.Type is NubPrimitiveType { Kind: PrimitiveTypeKind.F64 })
|
||||
{
|
||||
var value = double.Parse(literal.Literal, CultureInfo.InvariantCulture);
|
||||
var bits = BitConverter.DoubleToInt64Bits(value);
|
||||
return new Val(bits.ToString(), literal.Type, ValKind.Direct);
|
||||
}
|
||||
|
||||
if (literal.Type.IsInteger)
|
||||
if (literal.Type is NubPrimitiveType { Kind: PrimitiveTypeKind.I8 or PrimitiveTypeKind.U8 or PrimitiveTypeKind.I16 or PrimitiveTypeKind.U16 or PrimitiveTypeKind.I32 or PrimitiveTypeKind.U32 or PrimitiveTypeKind.I64 or PrimitiveTypeKind.U64 })
|
||||
{
|
||||
return new Val(literal.Literal, literal.Type, ValKind.Direct);
|
||||
}
|
||||
@@ -1050,19 +973,19 @@ public static class QBEGenerator
|
||||
}
|
||||
case LiteralKind.Float:
|
||||
{
|
||||
if (literal.Type.IsInteger)
|
||||
if (literal.Type is NubPrimitiveType { Kind: PrimitiveTypeKind.I8 or PrimitiveTypeKind.U8 or PrimitiveTypeKind.I16 or PrimitiveTypeKind.U16 or PrimitiveTypeKind.I32 or PrimitiveTypeKind.U32 or PrimitiveTypeKind.I64 or PrimitiveTypeKind.U64 })
|
||||
{
|
||||
return new Val(literal.Literal.Split(".").First(), literal.Type, ValKind.Direct);
|
||||
}
|
||||
|
||||
if (literal.Type.IsFloat32)
|
||||
if (literal.Type is NubPrimitiveType { Kind: PrimitiveTypeKind.F32 })
|
||||
{
|
||||
var value = float.Parse(literal.Literal, CultureInfo.InvariantCulture);
|
||||
var bits = BitConverter.SingleToInt32Bits(value);
|
||||
return new Val(bits.ToString(), literal.Type, ValKind.Direct);
|
||||
}
|
||||
|
||||
if (literal.Type.IsFloat64)
|
||||
if (literal.Type is NubPrimitiveType { Kind: PrimitiveTypeKind.F64 })
|
||||
{
|
||||
var value = double.Parse(literal.Literal, CultureInfo.InvariantCulture);
|
||||
var bits = BitConverter.DoubleToInt64Bits(value);
|
||||
@@ -1073,14 +996,14 @@ public static class QBEGenerator
|
||||
}
|
||||
case LiteralKind.String:
|
||||
{
|
||||
if (literal.Type.IsString)
|
||||
if (literal.Type is NubStringType)
|
||||
{
|
||||
var stringLiteral = new StringLiteral(literal.Literal, StringName());
|
||||
_stringLiterals.Add(stringLiteral);
|
||||
return new Val(stringLiteral.Name, literal.Type, ValKind.Direct);
|
||||
}
|
||||
|
||||
if (literal.Type.IsCString)
|
||||
if (literal.Type is NubCStringType)
|
||||
{
|
||||
var cStringLiteral = new CStringLiteral(literal.Literal, CStringName());
|
||||
_cStringLiterals.Add(cStringLiteral);
|
||||
@@ -1091,7 +1014,7 @@ public static class QBEGenerator
|
||||
}
|
||||
case LiteralKind.Bool:
|
||||
{
|
||||
if (literal.Type.IsBool)
|
||||
if (literal.Type is NubPrimitiveType { Kind: PrimitiveTypeKind.Bool })
|
||||
{
|
||||
return new Val(bool.Parse(literal.Literal) ? "1" : "0", literal.Type, ValKind.Direct);
|
||||
}
|
||||
@@ -1142,16 +1065,16 @@ public static class QBEGenerator
|
||||
{
|
||||
switch (unaryExpression.Operand.Type)
|
||||
{
|
||||
case BoundNubPrimitiveType { Kind: PrimitiveTypeKind.I64 }:
|
||||
case NubPrimitiveType { Kind: PrimitiveTypeKind.I64 }:
|
||||
_writer.Indented($"{outputName} =l neg {operand}");
|
||||
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
||||
case BoundNubPrimitiveType { Kind: PrimitiveTypeKind.I32 or PrimitiveTypeKind.I16 or PrimitiveTypeKind.I8 }:
|
||||
case NubPrimitiveType { Kind: PrimitiveTypeKind.I32 or PrimitiveTypeKind.I16 or PrimitiveTypeKind.I8 }:
|
||||
_writer.Indented($"{outputName} =w neg {operand}");
|
||||
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
||||
case BoundNubPrimitiveType { Kind: PrimitiveTypeKind.F64 }:
|
||||
case NubPrimitiveType { Kind: PrimitiveTypeKind.F64 }:
|
||||
_writer.Indented($"{outputName} =d neg {operand}");
|
||||
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
||||
case BoundNubPrimitiveType { Kind: PrimitiveTypeKind.F32 }:
|
||||
case NubPrimitiveType { Kind: PrimitiveTypeKind.F32 }:
|
||||
_writer.Indented($"{outputName} =s neg {operand}");
|
||||
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
||||
}
|
||||
@@ -1162,7 +1085,7 @@ public static class QBEGenerator
|
||||
{
|
||||
switch (unaryExpression.Operand.Type)
|
||||
{
|
||||
case BoundNubPrimitiveType { Kind: PrimitiveTypeKind.Bool }:
|
||||
case NubPrimitiveType { Kind: PrimitiveTypeKind.Bool }:
|
||||
_writer.Indented($"{outputName} =w xor {operand}, 1");
|
||||
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
||||
}
|
||||
@@ -1189,7 +1112,7 @@ public static class QBEGenerator
|
||||
_writer.Indented($"{output} =l add {target}, {offset}");
|
||||
|
||||
// If the accessed member is an inline struct, it will not be a pointer
|
||||
if (structFieldAccess.Type is BoundNubStructType)
|
||||
if (structFieldAccess.Type is NubCustomType customType && customType.Kind(_definitionTable) == CustomTypeKind.Struct)
|
||||
{
|
||||
return new Val(output, structFieldAccess.Type, ValKind.Direct);
|
||||
}
|
||||
@@ -1232,7 +1155,7 @@ public static class QBEGenerator
|
||||
parameterStrings.Add($"{FuncQBETypeName(parameter.Type)} {copy}");
|
||||
}
|
||||
|
||||
if (funcCall.Type.IsVoid)
|
||||
if (funcCall.Type is NubVoidType)
|
||||
{
|
||||
_writer.Indented($"call {funcPointer}({string.Join(", ", parameterStrings)})");
|
||||
return new Val(string.Empty, funcCall.Type, ValKind.Direct);
|
||||
@@ -1274,7 +1197,7 @@ internal class Variable(string name, Val val)
|
||||
public Val Val { get; } = val;
|
||||
}
|
||||
|
||||
internal record Val(string Name, BoundNubType Type, ValKind Kind, MethodCallContext? FuncCallContext = null);
|
||||
internal record Val(string Name, NubType Type, ValKind Kind, MethodCallContext? FuncCallContext = null);
|
||||
|
||||
internal record MethodCallContext(Val ThisArg);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user