...
This commit is contained in:
@@ -140,7 +140,6 @@ public static class QBEGenerator
|
|||||||
_ => throw new ArgumentOutOfRangeException()
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
},
|
},
|
||||||
NubStructType => "storel",
|
NubStructType => "storel",
|
||||||
NubFixedArrayType => "storel",
|
|
||||||
NubFuncType => "storel",
|
NubFuncType => "storel",
|
||||||
NubCStringType => "storel",
|
NubCStringType => "storel",
|
||||||
NubStringType => "storel",
|
NubStringType => "storel",
|
||||||
@@ -170,7 +169,6 @@ public static class QBEGenerator
|
|||||||
_ => throw new ArgumentOutOfRangeException()
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
},
|
},
|
||||||
NubStructType => "loadl",
|
NubStructType => "loadl",
|
||||||
NubFixedArrayType => "loadl",
|
|
||||||
NubFuncType => "loadl",
|
NubFuncType => "loadl",
|
||||||
NubCStringType => "loadl",
|
NubCStringType => "loadl",
|
||||||
NubStringType => "loadl",
|
NubStringType => "loadl",
|
||||||
@@ -200,7 +198,6 @@ public static class QBEGenerator
|
|||||||
_ => throw new ArgumentOutOfRangeException()
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
},
|
},
|
||||||
NubStructType => "=l",
|
NubStructType => "=l",
|
||||||
NubFixedArrayType => "=l",
|
|
||||||
NubFuncType => "=l",
|
NubFuncType => "=l",
|
||||||
NubCStringType => "=l",
|
NubCStringType => "=l",
|
||||||
NubStringType => "=l",
|
NubStringType => "=l",
|
||||||
@@ -236,10 +233,6 @@ public static class QBEGenerator
|
|||||||
{
|
{
|
||||||
return 8;
|
return 8;
|
||||||
}
|
}
|
||||||
case NubFixedArrayType nubFixedArrayType:
|
|
||||||
{
|
|
||||||
return AlignmentOf(nubFixedArrayType.ElementType);
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
throw new ArgumentOutOfRangeException();
|
throw new ArgumentOutOfRangeException();
|
||||||
@@ -295,10 +288,6 @@ public static class QBEGenerator
|
|||||||
{
|
{
|
||||||
return 8;
|
return 8;
|
||||||
}
|
}
|
||||||
case NubFixedArrayType nubFixedArrayType:
|
|
||||||
{
|
|
||||||
return SizeOf(nubFixedArrayType.ElementType) * nubFixedArrayType.Capacity + 8;
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
throw new ArgumentOutOfRangeException();
|
throw new ArgumentOutOfRangeException();
|
||||||
@@ -326,16 +315,6 @@ public static class QBEGenerator
|
|||||||
throw new UnreachableException($"Member '{member}' not found in struct");
|
throw new UnreachableException($"Member '{member}' not found in struct");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool IsPointerType(NubType type)
|
|
||||||
{
|
|
||||||
if (type.IsVoid)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException($"{nameof(IsPointerType)} should not be called on void types");
|
|
||||||
}
|
|
||||||
|
|
||||||
return type is NubStructType or NubFixedArrayType;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void EmitFuncDefinition(string name, List<BoundFuncParameter> parameters, NubType returnType, BoundBlockNode body, bool exported)
|
private static void EmitFuncDefinition(string name, List<BoundFuncParameter> parameters, NubType returnType, BoundBlockNode body, bool exported)
|
||||||
{
|
{
|
||||||
_variables.Clear();
|
_variables.Clear();
|
||||||
@@ -369,7 +348,6 @@ public static class QBEGenerator
|
|||||||
_ => throw new ArgumentOutOfRangeException()
|
_ => 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",
|
|
||||||
NubFuncType => "l",
|
NubFuncType => "l",
|
||||||
NubCStringType => "l",
|
NubCStringType => "l",
|
||||||
NubStringType => "l",
|
NubStringType => "l",
|
||||||
@@ -402,7 +380,6 @@ public static class QBEGenerator
|
|||||||
_ => throw new ArgumentOutOfRangeException()
|
_ => 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",
|
|
||||||
NubFuncType => "l",
|
NubFuncType => "l",
|
||||||
NubCStringType => "l",
|
NubCStringType => "l",
|
||||||
NubStringType => "l",
|
NubStringType => "l",
|
||||||
@@ -485,7 +462,6 @@ public static class QBEGenerator
|
|||||||
_ => throw new ArgumentOutOfRangeException()
|
_ => 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)}",
|
|
||||||
NubFuncType => "l",
|
NubFuncType => "l",
|
||||||
NubCStringType => "l",
|
NubCStringType => "l",
|
||||||
NubStringType => "l",
|
NubStringType => "l",
|
||||||
@@ -558,16 +534,6 @@ public static class QBEGenerator
|
|||||||
EmitCopy(arrayType.ElementType, value, pointer);
|
EmitCopy(arrayType.ElementType, value, pointer);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case NubFixedArrayType fixedArrayType:
|
|
||||||
{
|
|
||||||
var pointer = VarName();
|
|
||||||
_builder.AppendLine($" {pointer} =l mul {index}, {SizeOf(fixedArrayType.ElementType)}");
|
|
||||||
_builder.AppendLine($" {pointer} =l add {pointer}, 8");
|
|
||||||
_builder.AppendLine($" {pointer} =l add {array}, {pointer}");
|
|
||||||
|
|
||||||
EmitCopy(fixedArrayType.ElementType, value, pointer);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
throw new ArgumentOutOfRangeException();
|
throw new ArgumentOutOfRangeException();
|
||||||
@@ -746,7 +712,6 @@ public static class QBEGenerator
|
|||||||
var elementType = arrayIndexAccess.Array.Type switch
|
var elementType = arrayIndexAccess.Array.Type switch
|
||||||
{
|
{
|
||||||
NubArrayType arrayType => arrayType.ElementType,
|
NubArrayType arrayType => arrayType.ElementType,
|
||||||
NubFixedArrayType fixedArrayType => fixedArrayType.ElementType,
|
|
||||||
_ => throw new ArgumentOutOfRangeException()
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1100,6 +1065,7 @@ public static class QBEGenerator
|
|||||||
{
|
{
|
||||||
return new Val(literal.Literal, literal.Type, ValKind.Immediate);
|
return new Val(literal.Literal, literal.Type, ValKind.Immediate);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LiteralKind.Float:
|
case LiteralKind.Float:
|
||||||
@@ -1122,6 +1088,7 @@ public static class QBEGenerator
|
|||||||
var bits = BitConverter.DoubleToInt64Bits(value);
|
var bits = BitConverter.DoubleToInt64Bits(value);
|
||||||
return new Val(bits.ToString(), literal.Type, ValKind.Immediate);
|
return new Val(bits.ToString(), literal.Type, ValKind.Immediate);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LiteralKind.String:
|
case LiteralKind.String:
|
||||||
@@ -1139,6 +1106,7 @@ public static class QBEGenerator
|
|||||||
_cStringLiterals.Add(cStringLiteral);
|
_cStringLiterals.Add(cStringLiteral);
|
||||||
return new Val(cStringLiteral.Name, literal.Type, ValKind.Immediate);
|
return new Val(cStringLiteral.Name, literal.Type, ValKind.Immediate);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LiteralKind.Bool:
|
case LiteralKind.Bool:
|
||||||
@@ -1147,6 +1115,7 @@ public static class QBEGenerator
|
|||||||
{
|
{
|
||||||
return new Val(bool.Parse(literal.Literal) ? "1" : "0", literal.Type, ValKind.Immediate);
|
return new Val(bool.Parse(literal.Literal) ? "1" : "0", literal.Type, ValKind.Immediate);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1330,7 +1299,6 @@ public static class QBEGenerator
|
|||||||
_ => throw new ArgumentOutOfRangeException()
|
_ => 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",
|
|
||||||
NubFuncType => "l",
|
NubFuncType => "l",
|
||||||
NubCStringType => "l",
|
NubCStringType => "l",
|
||||||
NubStringType => "l",
|
NubStringType => "l",
|
||||||
@@ -1377,22 +1345,26 @@ public static class QBEGenerator
|
|||||||
{
|
{
|
||||||
case ValKind.Func:
|
case ValKind.Func:
|
||||||
case ValKind.Immediate:
|
case ValKind.Immediate:
|
||||||
return val.Name;
|
|
||||||
case ValKind.Pointer:
|
|
||||||
if (IsPointerType(val.Type))
|
|
||||||
{
|
{
|
||||||
return val.Name;
|
return val.Name;
|
||||||
}
|
}
|
||||||
else
|
case ValKind.Pointer:
|
||||||
{
|
{
|
||||||
|
if (val.Type is NubStructType)
|
||||||
|
{
|
||||||
|
return val.Name;
|
||||||
|
}
|
||||||
|
|
||||||
var result = VarName();
|
var result = VarName();
|
||||||
_builder.AppendLine($" {result} {QBEAssign(val.Type)} {QBELoad(val.Type)} {val.Name}");
|
_builder.AppendLine($" {result} {QBEAssign(val.Type)} {QBELoad(val.Type)} {val.Name}");
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
{
|
||||||
throw new ArgumentOutOfRangeException();
|
throw new ArgumentOutOfRangeException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class StringLiteral(string value, string name)
|
internal class StringLiteral(string value, string name)
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using Common;
|
using Common;
|
||||||
using Syntax.Diagnostics;
|
using Syntax.Diagnostics;
|
||||||
using Syntax.Parsing.Node;
|
using Syntax.Parsing.Node;
|
||||||
@@ -729,31 +728,11 @@ public static class Parser
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (TryExpectSymbol(Symbol.OpenBracket))
|
if (TryExpectSymbol(Symbol.OpenBracket))
|
||||||
{
|
|
||||||
if (Peek().TryGetValue(out var token) && token is LiteralToken { Kind: LiteralKind.Integer, Value: var sizeValue })
|
|
||||||
{
|
|
||||||
Next();
|
|
||||||
ExpectSymbol(Symbol.CloseBracket);
|
|
||||||
var baseType = ParseType();
|
|
||||||
|
|
||||||
var size = int.Parse(sizeValue);
|
|
||||||
|
|
||||||
if (size > 0)
|
|
||||||
{
|
|
||||||
return new NubFixedArrayType(baseType, size);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new UnreachableException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
ExpectSymbol(Symbol.CloseBracket);
|
ExpectSymbol(Symbol.CloseBracket);
|
||||||
var baseType = ParseType();
|
var baseType = ParseType();
|
||||||
return new NubArrayType(baseType);
|
return new NubArrayType(baseType);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!Peek().TryGetValue(out var peekToken))
|
if (!Peek().TryGetValue(out var peekToken))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -166,24 +166,6 @@ public class NubArrayType(NubType elementType) : NubType
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class NubFixedArrayType(NubType elementType, int capacity) : NubType
|
|
||||||
{
|
|
||||||
public NubType ElementType { get; } = elementType;
|
|
||||||
public int Capacity { get; } = capacity;
|
|
||||||
|
|
||||||
public override string ToString() => $"[{Capacity}]{ElementType}";
|
|
||||||
|
|
||||||
public override bool Equals(object? obj)
|
|
||||||
{
|
|
||||||
return obj is NubFixedArrayType other && ElementType.Equals(other.ElementType) && Capacity == other.Capacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override int GetHashCode()
|
|
||||||
{
|
|
||||||
return HashCode.Combine(ElementType, Capacity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class NubAnyType : NubType
|
public class NubAnyType : NubType
|
||||||
{
|
{
|
||||||
public override string ToString() => "any";
|
public override string ToString() => "any";
|
||||||
|
|||||||
Reference in New Issue
Block a user