62
62
63
63
# NOTE: fixed set where sizeof(T) does match the numerical width
64
64
const SizeableInteger = Union{Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128}
65
- function ConstantInt (val:: T , ctx:: Context = GlobalContext () ) where T<: SizeableInteger
65
+ function ConstantInt (val:: T , ctx:: Context ) where T<: SizeableInteger
66
66
typ = IntType (sizeof (T)* 8 , ctx)
67
67
return ConstantInt (typ, val, T<: Signed )
68
68
end
@@ -79,11 +79,18 @@ Base.convert(::Type{T}, val::ConstantInt) where {T<:Signed} =
79
79
end
80
80
identify (:: Type{Value} , :: Val{API.LLVMConstantFPValueKind} ) = ConstantFP
81
81
82
- ConstantFP (typ:: LLVMDouble , val:: Real ) =
82
+ ConstantFP (typ:: FloatingPointType , val:: Real ) =
83
83
ConstantFP (API. LLVMConstReal (typ, Cdouble (val)))
84
84
85
- Base. convert (:: Type{Float64} , val:: ConstantFP ) =
86
- API. LLVMConstRealGetDouble (val, Ref {API.LLVMBool} ())
85
+ ConstantFP (val:: Float16 , ctx:: Context ) =
86
+ ConstantFP (HalfType (ctx), val)
87
+ ConstantFP (val:: Float32 , ctx:: Context ) =
88
+ ConstantFP (FloatType (ctx), val)
89
+ ConstantFP (val:: Float64 , ctx:: Context ) =
90
+ ConstantFP (DoubleType (ctx), val)
91
+
92
+ Base. convert (:: Type{T} , val:: ConstantFP ) where {T<: AbstractFloat } =
93
+ convert (T, API. LLVMConstRealGetDouble (val, Ref {API.LLVMBool} ()))
87
94
88
95
89
96
# # aggregate
@@ -114,6 +121,23 @@ abstract type ConstantAggregate <: Constant end
114
121
ref:: API.LLVMValueRef
115
122
end
116
123
identify (:: Type{Value} , :: Val{API.LLVMConstantArrayValueKind} ) = ConstantArray
124
+ identify (:: Type{Value} , :: Val{API.LLVMConstantDataArrayValueKind} ) = ConstantArray
125
+
126
+ ConstantArray (typ:: LLVMType , data:: Vector{T} ) where {T<: Constant } =
127
+ ConstantArray (API. LLVMConstArray (typ, data, length (data)))
128
+ ConstantArray (typ:: IntegerType , data:: Vector{T} ) where {T<: Integer } =
129
+ ConstantArray (typ, map (x-> ConstantInt (convert (T,x),context (typ)), data))
130
+ ConstantArray (typ:: FloatingPointType , data:: Vector{T} ) where {T<: AbstractFloat } =
131
+ ConstantArray (typ, map (x-> ConstantFP (convert (T,x),context (typ)), data))
132
+
133
+ Base. getindex (ca:: ConstantArray , idx:: Integer ) =
134
+ API. LLVMGetElementAsConstant (ca, idx- 1 )
135
+ Base. length (ca:: ConstantArray ) = length (llvmtype (ca))
136
+ Base. eltype (ca:: ConstantArray ) = eltype (llvmtype (ca))
137
+ Base. convert (:: Type{Array{T,1}} , ca:: ConstantArray ) where {T<: Integer } =
138
+ [convert (T,ConstantInt (ca[i])) for i in 1 : length (ca)]
139
+ Base. convert (:: Type{Array{T,1}} , ca:: ConstantArray ) where {T<: AbstractFloat } =
140
+ [convert (T,ConstantFP (ca[i])) for i in 1 : length (ca)]
117
141
118
142
@checked struct ConstantStruct <: ConstantAggregate
119
143
ref:: API.LLVMValueRef
0 commit comments