Skip to content
← Back

src/ir/emitter/cil_operations.ghul

1
@@IF.dotnet()
2
3
namespace IR.Emitter is
4
use System.Reflection.Metadata.ILOpCode
5
6
// How the CIL backend encodes the target-neutral operations.
7
class CIL_OPERATIONS is
8
// A conversion to `to` from a value of kind `from`, or of a kind
9
// that is not a scalar (an enum) where `from` is null.
10
//
11
// A uint, ulong or uword fills its whole stack slot, so its top
12
// bit is part of the magnitude rather than a sign, and widening
13
// it to a signed integer or a floating type takes the unsigned
14
// form of the conversion. The narrower unsigned types are already
15
// zero-extended on the stack, and need nothing different.
16
conversion(from: Scalar?, to: Scalar) -> Collections.List[ILOpCode] static is
17
let full_width_unsigned =
18
if let f = from then
19
f == Scalar.UINT32 \/ f == Scalar.UINT64 \/ f == Scalar.UINT_NATIVE
20
else
21
false
22
fi
23
24
if full_width_unsigned then
25
if to == Scalar.FLOAT32 \/ to == Scalar.FLOAT64 then
26
return [ILOpCode.CONV_R_UN, _conversion_to(to)]
27
elif to == Scalar.INT64 then
28
return [ILOpCode.CONV_U8]
29
elif to == Scalar.INT_NATIVE then
30
return [ILOpCode.CONV_U]
31
fi
32
fi
33
34
return [_conversion_to(to)]
35
si
36
37
_conversion_to(to: Scalar) -> ILOpCode static =>
38
case to
39
when Scalar.BOOL then ILOpCode.CONV_U1
40
when Scalar.UINT8 then ILOpCode.CONV_U1
41
when Scalar.INT8 then ILOpCode.CONV_I1
42
when Scalar.CHAR then ILOpCode.CONV_U2
43
when Scalar.UINT16 then ILOpCode.CONV_U2
44
when Scalar.INT16 then ILOpCode.CONV_I2
45
when Scalar.UINT32 then ILOpCode.CONV_U4
46
when Scalar.INT32 then ILOpCode.CONV_I4
47
when Scalar.UINT64 then ILOpCode.CONV_U8
48
when Scalar.INT64 then ILOpCode.CONV_I8
49
when Scalar.UINT_NATIVE then ILOpCode.CONV_U
50
when Scalar.INT_NATIVE then ILOpCode.CONV_I
51
when Scalar.FLOAT32 then ILOpCode.CONV_R4
52
when Scalar.FLOAT64 then ILOpCode.CONV_R8
53
esac
54
55
arithmetic(operation: ArithmeticOperation) -> ILOpCode static =>
56
case operation
57
when ArithmeticOperation.ADD then ILOpCode.ADD
58
when ArithmeticOperation.SUBTRACT then ILOpCode.SUB
59
when ArithmeticOperation.MULTIPLY then ILOpCode.MUL
60
when ArithmeticOperation.DIVIDE then ILOpCode.DIV
61
when ArithmeticOperation.DIVIDE_UNSIGNED then ILOpCode.DIV_UN
62
when ArithmeticOperation.REMAINDER then ILOpCode.REM
63
when ArithmeticOperation.REMAINDER_UNSIGNED then ILOpCode.REM_UN
64
when ArithmeticOperation.NEGATE then ILOpCode.NEG
65
when ArithmeticOperation.COMPLEMENT then ILOpCode.NOT
66
when ArithmeticOperation.AND then ILOpCode.AND
67
when ArithmeticOperation.OR then ILOpCode.OR
68
when ArithmeticOperation.XOR then ILOpCode.XOR
69
when ArithmeticOperation.SHIFT_LEFT then ILOpCode.SHL
70
when ArithmeticOperation.SHIFT_RIGHT then ILOpCode.SHR
71
when ArithmeticOperation.SHIFT_RIGHT_UNSIGNED then ILOpCode.SHR_UN
72
esac
73
74
// A comparison of the two values on the stack. CIL has only
75
// equal, greater and less, so the other three are the negation
76
// of one of those.
77
comparison(comparison: Comparison, is_unsigned: bool) -> (op_code: ILOpCode, negates: bool) static =>
78
case comparison
79
when Comparison.EQUAL then (ILOpCode.CEQ, false)
80
when Comparison.NOT_EQUAL then (ILOpCode.CEQ, true)
81
when Comparison.GREATER then (_greater(is_unsigned), false)
82
when Comparison.LESS_OR_EQUAL then (_greater(is_unsigned), true)
83
when Comparison.LESS then (_less(is_unsigned), false)
84
when Comparison.GREATER_OR_EQUAL then (_less(is_unsigned), true)
85
esac
86
87
// System.Decimal has no instructions of its own: each operator
88
// is a static member, named by the operator it implements.
89
decimal_arithmetic(operation: ArithmeticOperation) -> string static =>
90
case operation
91
when ArithmeticOperation.ADD then "op_Addition"
92
when ArithmeticOperation.SUBTRACT then "op_Subtraction"
93
when ArithmeticOperation.MULTIPLY then "op_Multiply"
94
when ArithmeticOperation.DIVIDE then "op_Division"
95
when ArithmeticOperation.REMAINDER then "op_Modulus"
96
when ArithmeticOperation.NEGATE then "op_UnaryNegation"
97
else throw System.InvalidOperationException("no decimal member for {operation}")
98
esac
99
100
// A widening conversion to decimal is its implicit operator;
101
// every other conversion to or from decimal is its explicit one.
102
decimal_conversion(is_widening: bool) -> string static =>
103
if is_widening then "op_Implicit" else "op_Explicit" fi
104
105
decimal_comparison(comparison: Comparison) -> string static =>
106
case comparison
107
when Comparison.EQUAL then "op_Equality"
108
when Comparison.NOT_EQUAL then "op_Inequality"
109
when Comparison.GREATER then "op_GreaterThan"
110
when Comparison.GREATER_OR_EQUAL then "op_GreaterThanOrEqual"
111
when Comparison.LESS then "op_LessThan"
112
when Comparison.LESS_OR_EQUAL then "op_LessThanOrEqual"
113
esac
114
115
_greater(is_unsigned: bool) -> ILOpCode static =>
116
if is_unsigned then ILOpCode.CGT_UN else ILOpCode.CGT fi
117
118
_less(is_unsigned: bool) -> ILOpCode static =>
119
if is_unsigned then ILOpCode.CLT_UN else ILOpCode.CLT fi
120
si
121
si