@@ -46,12 +46,12 @@ const WideInteger = Union{Int64, UInt64}
46
46
ConstantInt (typ:: IntegerType , val:: WideInteger , signed= false ) =
47
47
ConstantInt (API. LLVMConstInt (typ, reinterpret (Culonglong, val),
48
48
convert (Bool, signed)))
49
- const SmallInteger = Union{Int8, Int16, Int32, UInt8, UInt16, UInt32}
49
+ const SmallInteger = Union{Core . Bool, Int8, Int16, Int32, UInt8, UInt16, UInt32}
50
50
ConstantInt (typ:: IntegerType , val:: SmallInteger , signed= false ) =
51
51
ConstantInt (typ, convert (Int64, val), signed)
52
52
53
53
function ConstantInt (typ:: IntegerType , val:: Integer , signed= false )
54
- valbits = ceil (Int, log2 (abs (val))) + 1
54
+ valbits = ceil (Int, log2 (abs (val))) + 1 # FIXME : doesn't work for val=0
55
55
numwords = ceil (Int, valbits / 64 )
56
56
words = Vector {Culonglong} (undef, numwords)
57
57
for i in 1 : numwords
@@ -67,12 +67,18 @@ function ConstantInt(val::T, ctx::Context) where T<:SizeableInteger
67
67
return ConstantInt (typ, val, T<: Signed )
68
68
end
69
69
70
+ # Booleans are encoded with a single bit, so we can't use sizeof
71
+ ConstantInt (val:: Core.Bool , ctx:: Context ) = ConstantInt (Int1Type (ctx), val ? 1 : 0 )
72
+
70
73
Base. convert (:: Type{T} , val:: ConstantInt ) where {T<: Unsigned } =
71
74
convert (T, API. LLVMConstIntGetZExtValue (val))
72
75
73
76
Base. convert (:: Type{T} , val:: ConstantInt ) where {T<: Signed } =
74
77
convert (T, API. LLVMConstIntGetSExtValue (val))
75
78
79
+ # Booleans aren't Signed or Unsigned
80
+ Base. convert (:: Type{Core.Bool} , val:: ConstantInt ) = convert (Int, val) != 0
81
+
76
82
77
83
@checked struct ConstantFP <: Constant
78
84
ref:: API.LLVMValueRef
@@ -146,8 +152,11 @@ function ConstantArray(data::AbstractArray{<:Constant,N},
146
152
end
147
153
148
154
# shorthands with arrays of plain Julia data
155
+ # FIXME : duplicates the ConstantInt/ConstantFP conversion rules (to support empty arrays)
149
156
ConstantArray (data:: AbstractArray{T,N} , ctx:: Context = GlobalContext ()) where {T<: Integer ,N} =
150
157
ConstantArray (ConstantInt .(data, Ref (ctx)), IntType (sizeof (T)* 8 , ctx))
158
+ ConstantArray (data:: AbstractArray{Core.Bool,N} , ctx:: Context = GlobalContext ()) where {N} =
159
+ ConstantArray (ConstantInt .(data, Ref (ctx)), Int1Type (ctx))
151
160
ConstantArray (data:: AbstractArray{Float16,N} , ctx:: Context = GlobalContext ()) where {N} =
152
161
ConstantArray (ConstantFP .(data, Ref (ctx)), HalfType (ctx))
153
162
ConstantArray (data:: AbstractArray{Float32,N} , ctx:: Context = GlobalContext ()) where {N} =
@@ -224,10 +233,7 @@ function ConstantStruct(value::T, ctx::Context=GlobalContext(); name=String(name
224
233
for fieldname in fieldnames (T)
225
234
field = getfield (value, fieldname)
226
235
227
- if isa (field, Core. Bool)
228
- typ = LLVM. Int1Type (ctx)
229
- push! (constants, ConstantInt (typ, Int (field)))
230
- elseif isa (field, Integer)
236
+ if isa (field, Integer)
231
237
push! (constants, ConstantInt (field, ctx))
232
238
elseif isa (field, AbstractFloat)
233
239
push! (constants, ConstantFP (field, ctx))
0 commit comments