Primitive to int/float/bool
This commit is contained in:
@@ -189,11 +189,11 @@ public partial class QBEGenerator
|
||||
|
||||
string sign;
|
||||
|
||||
if (simpleType is NubPrimitiveType { Kind: PrimitiveTypeKind.I8 or PrimitiveTypeKind.I16 or PrimitiveTypeKind.I32 or PrimitiveTypeKind.I64 })
|
||||
if (simpleType is NubIntType { Signed: true })
|
||||
{
|
||||
sign = "s";
|
||||
}
|
||||
else if (simpleType is NubPrimitiveType { Kind: PrimitiveTypeKind.U8 or PrimitiveTypeKind.U16 or PrimitiveTypeKind.U32 or PrimitiveTypeKind.U64 })
|
||||
else if (simpleType is NubIntType { Signed: false })
|
||||
{
|
||||
sign = "u";
|
||||
}
|
||||
@@ -245,21 +245,21 @@ public partial class QBEGenerator
|
||||
{
|
||||
case LiteralKind.Integer:
|
||||
{
|
||||
if (literal.Type is NubPrimitiveType { Kind: PrimitiveTypeKind.F32 })
|
||||
if (literal.Type is NubFloatType { Width: 32 })
|
||||
{
|
||||
var value = float.Parse(literal.Value, CultureInfo.InvariantCulture);
|
||||
var bits = BitConverter.SingleToInt32Bits(value);
|
||||
return new Val(bits.ToString(), literal.Type, ValKind.Direct);
|
||||
}
|
||||
|
||||
if (literal.Type is NubPrimitiveType { Kind: PrimitiveTypeKind.F64 })
|
||||
if (literal.Type is NubFloatType { Width: 64 })
|
||||
{
|
||||
var value = double.Parse(literal.Value, CultureInfo.InvariantCulture);
|
||||
var bits = BitConverter.DoubleToInt64Bits(value);
|
||||
return new Val(bits.ToString(), literal.Type, ValKind.Direct);
|
||||
}
|
||||
|
||||
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 })
|
||||
if (literal.Type is NubIntType)
|
||||
{
|
||||
return new Val(literal.Value, literal.Type, ValKind.Direct);
|
||||
}
|
||||
@@ -268,19 +268,19 @@ public partial class QBEGenerator
|
||||
}
|
||||
case LiteralKind.Float:
|
||||
{
|
||||
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 })
|
||||
if (literal.Type is NubIntType)
|
||||
{
|
||||
return new Val(literal.Value.Split(".").First(), literal.Type, ValKind.Direct);
|
||||
}
|
||||
|
||||
if (literal.Type is NubPrimitiveType { Kind: PrimitiveTypeKind.F32 })
|
||||
if (literal.Type is NubFloatType { Width: 32 })
|
||||
{
|
||||
var value = float.Parse(literal.Value, CultureInfo.InvariantCulture);
|
||||
var bits = BitConverter.SingleToInt32Bits(value);
|
||||
return new Val(bits.ToString(), literal.Type, ValKind.Direct);
|
||||
}
|
||||
|
||||
if (literal.Type is NubPrimitiveType { Kind: PrimitiveTypeKind.F64 })
|
||||
if (literal.Type is NubFloatType { Width: 64 })
|
||||
{
|
||||
var value = double.Parse(literal.Value, CultureInfo.InvariantCulture);
|
||||
var bits = BitConverter.DoubleToInt64Bits(value);
|
||||
@@ -309,7 +309,7 @@ public partial class QBEGenerator
|
||||
}
|
||||
case LiteralKind.Bool:
|
||||
{
|
||||
if (literal.Type is NubPrimitiveType { Kind: PrimitiveTypeKind.Bool })
|
||||
if (literal.Type is NubBoolType)
|
||||
{
|
||||
return new Val(bool.Parse(literal.Value) ? "1" : "0", literal.Type, ValKind.Direct);
|
||||
}
|
||||
@@ -360,16 +360,16 @@ public partial class QBEGenerator
|
||||
{
|
||||
switch (unaryExpression.Operand.Type)
|
||||
{
|
||||
case NubPrimitiveType { Kind: PrimitiveTypeKind.I64 }:
|
||||
case NubIntType { Signed: true, Width: 64 }:
|
||||
_writer.Indented($"{outputName} =l neg {operand}");
|
||||
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
||||
case NubPrimitiveType { Kind: PrimitiveTypeKind.I32 or PrimitiveTypeKind.I16 or PrimitiveTypeKind.I8 }:
|
||||
case NubIntType { Signed: true, Width: 8 or 16 or 32 }:
|
||||
_writer.Indented($"{outputName} =w neg {operand}");
|
||||
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
||||
case NubPrimitiveType { Kind: PrimitiveTypeKind.F64 }:
|
||||
case NubFloatType { Width: 64 }:
|
||||
_writer.Indented($"{outputName} =d neg {operand}");
|
||||
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
||||
case NubPrimitiveType { Kind: PrimitiveTypeKind.F32 }:
|
||||
case NubFloatType { Width: 32 }:
|
||||
_writer.Indented($"{outputName} =s neg {operand}");
|
||||
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
||||
}
|
||||
@@ -380,7 +380,7 @@ public partial class QBEGenerator
|
||||
{
|
||||
switch (unaryExpression.Operand.Type)
|
||||
{
|
||||
case NubPrimitiveType { Kind: PrimitiveTypeKind.Bool }:
|
||||
case NubBoolType:
|
||||
_writer.Indented($"{outputName} =w xor {operand}, 1");
|
||||
return new Val(outputName, unaryExpression.Type, ValKind.Direct);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user