...
This commit is contained in:
@@ -122,7 +122,7 @@ public static class QBEGenerator
|
|||||||
PrimitiveTypeKind.F64 => "stored",
|
PrimitiveTypeKind.F64 => "stored",
|
||||||
PrimitiveTypeKind.F32 => "stores",
|
PrimitiveTypeKind.F32 => "stores",
|
||||||
PrimitiveTypeKind.Bool => "storew",
|
PrimitiveTypeKind.Bool => "storew",
|
||||||
_ => throw new UnreachableException()
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
},
|
},
|
||||||
NubStructType => "storel",
|
NubStructType => "storel",
|
||||||
NubFixedArrayType => "storel",
|
NubFixedArrayType => "storel",
|
||||||
@@ -150,7 +150,7 @@ public static class QBEGenerator
|
|||||||
PrimitiveTypeKind.F64 => "loadd",
|
PrimitiveTypeKind.F64 => "loadd",
|
||||||
PrimitiveTypeKind.F32 => "loads",
|
PrimitiveTypeKind.F32 => "loads",
|
||||||
PrimitiveTypeKind.Bool => "loadw",
|
PrimitiveTypeKind.Bool => "loadw",
|
||||||
_ => throw new UnreachableException()
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
},
|
},
|
||||||
NubStructType => "loadl",
|
NubStructType => "loadl",
|
||||||
NubFixedArrayType => "loadl",
|
NubFixedArrayType => "loadl",
|
||||||
@@ -178,7 +178,7 @@ public static class QBEGenerator
|
|||||||
PrimitiveTypeKind.F64 => "=d",
|
PrimitiveTypeKind.F64 => "=d",
|
||||||
PrimitiveTypeKind.F32 => "=s",
|
PrimitiveTypeKind.F32 => "=s",
|
||||||
PrimitiveTypeKind.Bool => "=w",
|
PrimitiveTypeKind.Bool => "=w",
|
||||||
_ => throw new UnreachableException()
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
},
|
},
|
||||||
NubStructType => "=l",
|
NubStructType => "=l",
|
||||||
NubFixedArrayType => "=l",
|
NubFixedArrayType => "=l",
|
||||||
@@ -231,7 +231,7 @@ public static class QBEGenerator
|
|||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
throw new UnreachableException();
|
throw new ArgumentOutOfRangeException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -303,11 +303,31 @@ public static class QBEGenerator
|
|||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
throw new UnreachableException();
|
throw new ArgumentOutOfRangeException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int OffsetOf(StructDefinitionNode 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new UnreachableException($"Member '{member}' not found in struct");
|
||||||
|
}
|
||||||
|
|
||||||
private static bool IsLargeType(NubType type)
|
private static bool IsLargeType(NubType type)
|
||||||
{
|
{
|
||||||
return type switch
|
return type switch
|
||||||
@@ -352,7 +372,7 @@ public static class QBEGenerator
|
|||||||
PrimitiveTypeKind.F64 => "d",
|
PrimitiveTypeKind.F64 => "d",
|
||||||
PrimitiveTypeKind.F32 => "s",
|
PrimitiveTypeKind.F32 => "s",
|
||||||
PrimitiveTypeKind.Bool => "w",
|
PrimitiveTypeKind.Bool => "w",
|
||||||
_ => throw new UnreachableException()
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
},
|
},
|
||||||
NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()),
|
NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()),
|
||||||
NubFixedArrayType => "l",
|
NubFixedArrayType => "l",
|
||||||
@@ -383,7 +403,7 @@ public static class QBEGenerator
|
|||||||
PrimitiveTypeKind.F64 => "d",
|
PrimitiveTypeKind.F64 => "d",
|
||||||
PrimitiveTypeKind.F32 => "s",
|
PrimitiveTypeKind.F32 => "s",
|
||||||
PrimitiveTypeKind.Bool => "w",
|
PrimitiveTypeKind.Bool => "w",
|
||||||
_ => throw new UnreachableException()
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
},
|
},
|
||||||
NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()),
|
NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()),
|
||||||
NubFixedArrayType => "l",
|
NubFixedArrayType => "l",
|
||||||
@@ -464,7 +484,7 @@ public static class QBEGenerator
|
|||||||
PrimitiveTypeKind.F64 => "d",
|
PrimitiveTypeKind.F64 => "d",
|
||||||
PrimitiveTypeKind.F32 => "s",
|
PrimitiveTypeKind.F32 => "s",
|
||||||
PrimitiveTypeKind.Bool => "w",
|
PrimitiveTypeKind.Bool => "w",
|
||||||
_ => throw new UnreachableException()
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
},
|
},
|
||||||
NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()),
|
NubStructType structType => StructName(_definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue()),
|
||||||
NubFixedArrayType fixedArrayType => $"b {SizeOf(fixedArrayType)}",
|
NubFixedArrayType fixedArrayType => $"b {SizeOf(fixedArrayType)}",
|
||||||
@@ -557,7 +577,7 @@ public static class QBEGenerator
|
|||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
throw new UnreachableException();
|
throw new ArgumentOutOfRangeException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -636,7 +656,7 @@ public static class QBEGenerator
|
|||||||
Debug.Assert(structType != null);
|
Debug.Assert(structType != null);
|
||||||
|
|
||||||
var structDefinition = _definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue();
|
var structDefinition = _definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue();
|
||||||
var offset = LookupMemberOffset(structDefinition, memberAssignment.MemberAccess.Member);
|
var offset = OffsetOf(structDefinition, memberAssignment.MemberAccess.Member);
|
||||||
|
|
||||||
var item = GenerateExpression(memberAssignment.MemberAccess.Expression);
|
var item = GenerateExpression(memberAssignment.MemberAccess.Expression);
|
||||||
var pointer = VarName();
|
var pointer = VarName();
|
||||||
@@ -770,7 +790,7 @@ public static class QBEGenerator
|
|||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
throw new UnreachableException();
|
throw new ArgumentOutOfRangeException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -859,7 +879,7 @@ public static class QBEGenerator
|
|||||||
case MemberAccessNode memberAccess:
|
case MemberAccessNode memberAccess:
|
||||||
return GenerateMemberAccessPointer(memberAccess);
|
return GenerateMemberAccessPointer(memberAccess);
|
||||||
default:
|
default:
|
||||||
throw new UnreachableException();
|
throw new ArgumentOutOfRangeException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1160,7 +1180,7 @@ public static class QBEGenerator
|
|||||||
|
|
||||||
foreach (var field in structDefinition.Fields)
|
foreach (var field in structDefinition.Fields)
|
||||||
{
|
{
|
||||||
var offset = LookupMemberOffset(structDefinition, field.Name);
|
var offset = OffsetOf(structDefinition, field.Name);
|
||||||
|
|
||||||
if (structInitializer.Initializers.TryGetValue(field.Name, out var fieldValue))
|
if (structInitializer.Initializers.TryGetValue(field.Name, out var fieldValue))
|
||||||
{
|
{
|
||||||
@@ -1249,7 +1269,7 @@ public static class QBEGenerator
|
|||||||
case NubStructType structType:
|
case NubStructType structType:
|
||||||
{
|
{
|
||||||
var structDefinition = _definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue();
|
var structDefinition = _definitionTable.LookupStruct(structType.Namespace, structType.Name).GetValue();
|
||||||
var offset = LookupMemberOffset(structDefinition, memberAccess.Member);
|
var offset = OffsetOf(structDefinition, memberAccess.Member);
|
||||||
|
|
||||||
var offsetName = VarName();
|
var offsetName = VarName();
|
||||||
_builder.AppendLine($" {offsetName} =l add {item}, {offset}");
|
_builder.AppendLine($" {offsetName} =l add {item}, {offset}");
|
||||||
@@ -1257,7 +1277,7 @@ public static class QBEGenerator
|
|||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
throw new UnreachableException(nameof(memberAccess.Expression.Type));
|
throw new ArgumentOutOfRangeException(nameof(memberAccess.Expression.Type));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1384,24 +1404,4 @@ public static class QBEGenerator
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int LookupMemberOffset(StructDefinitionNode 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new ArgumentException($"Member '{member}' not found in struct");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user