Skip to content

Commit e16993a

Browse files
committed
Prettify the ConstantInt API.
1 parent f33e004 commit e16993a

File tree

2 files changed

+53
-25
lines changed

2 files changed

+53
-25
lines changed

src/core/value/constant.jl

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,37 @@ export ConstantInt, ConstantFP
66

77
@reftypedef proxy=Value kind=LLVMConstantIntValueKind immutable ConstantInt <: Constant end
88

9-
function ConstantInt{T<:Signed}(typ::IntegerType, val::T)
9+
# NOTE: fixed set for dispatch, and because we can't rely on sizeof(T)==width(T)
10+
typealias SmallInteger Union{Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64}
11+
function ConstantInt{T<:SmallInteger}(typ::IntegerType, val::T, signed=false)
1012
wideval = convert(Int, val)
11-
return construct(ConstantInt,
12-
API.LLVMConstInt(ref(typ), reinterpret(Culonglong, wideval), True))
13+
bits = reinterpret(Culonglong, wideval)
14+
return construct(ConstantInt, API.LLVMConstInt(ref(typ), bits, BoolToLLVM(signed)))
1315
end
1416

15-
function ConstantInt{T<:Unsigned}(typ::IntegerType, val::T)
16-
wideval = convert(UInt, val)
17+
function ConstantInt{T<:Integer}(typ::IntegerType, val::T, signed=false)
18+
valbits = ceil(Int, log2(abs(val))) + 1
19+
numwords = ceil(Int, valbits / 64)
20+
words = Vector{Culonglong}(numwords)
21+
for i in 1:numwords
22+
words[i] = (val >> 64(i-1)) % Culonglong
23+
end
1724
return construct(ConstantInt,
18-
API.LLVMConstInt(ref(typ), reinterpret(Culonglong, wideval), False))
25+
API.LLVMConstIntOfArbitraryPrecision(ref(typ), Cuint(numwords), words))
26+
end
27+
28+
# NOTE: fixed set where sizeof(T) does match the numerical width
29+
typealias SizeableIntegers Union{Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128}
30+
function ConstantInt{T<:Integer}(val::T, ctx::Context=GlobalContext())
31+
typ = IntType(sizeof(T)*8, ctx)
32+
return ConstantInt(typ, val, T<:Signed)
1933
end
2034

21-
convert(::Type{UInt}, val::ConstantInt) =
22-
API.LLVMConstIntGetZExtValue(ref(val))
35+
convert{T<:Unsigned}(::Type{T}, val::ConstantInt) =
36+
convert(T, API.LLVMConstIntGetZExtValue(ref(val)))
2337

24-
convert(::Type{Int}, val::ConstantInt) =
25-
API.LLVMConstIntGetSExtValue(ref(val))
38+
convert{T<:Signed}(::Type{T}, val::ConstantInt) =
39+
convert(T, API.LLVMConstIntGetSExtValue(ref(val)))
2640

2741

2842
@reftypedef proxy=Value kind=LLVMConstantFPValueKind immutable ConstantFP <: Constant end

test/core.jl

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ LLVM.Module("SomeModule", ctx) do mod
202202
position!(builder, entry)
203203

204204
valueinst1 = add!(builder, parameters(fn)[1],
205-
ConstantInt(LLVM.Int32Type(ctx), 1))
205+
ConstantInt(Int32(1), ctx))
206206

207207
userinst = add!(builder, valueinst1,
208-
ConstantInt(LLVM.Int32Type(ctx), 1))
208+
ConstantInt(Int32(1), ctx))
209209

210210
# use iteration
211211
let usepairs = uses(valueinst1)
@@ -224,7 +224,7 @@ LLVM.Module("SomeModule", ctx) do mod
224224
end
225225

226226
valueinst2 = add!(builder, parameters(fn)[1],
227-
ConstantInt(LLVM.Int32Type(ctx), 2))
227+
ConstantInt(Int32(2), ctx))
228228

229229
replace_uses!(valueinst1, valueinst2)
230230
@test user.(collect(uses(valueinst2))) == [userinst]
@@ -236,17 +236,31 @@ end
236236

237237
# scalar
238238
Context() do ctx
239-
t1 = LLVM.Int32Type(ctx)
240-
c1 = ConstantInt(t1, UInt32(1))
241-
@test convert(UInt, c1) == 1
242-
c2 = ConstantInt(t1, Int32(-1))
243-
@test convert(Int, c2) == -1
244-
245-
# construction from wider ints
246-
c3 = ConstantInt(t1, UInt(1))
247-
@test convert(UInt, c3) == 1
248-
c4 = ConstantInt(t1, -1)
249-
@test convert(Int, c4) == -1
239+
## integer constants
240+
241+
# manual construction of small values
242+
let
243+
typ = LLVM.Int32Type(ctx)
244+
constval = ConstantInt(typ, -1)
245+
@test convert(Int, constval) == -1
246+
@test convert(UInt32, constval) == typemax(UInt32)
247+
end
248+
249+
# manual construction of large values
250+
let
251+
typ = LLVM.Int64Type(ctx)
252+
constval = ConstantInt(typ, BigInt(2)^100-1)
253+
@test convert(Int, constval) == -1
254+
end
255+
256+
# automatic construction
257+
let
258+
constval = ConstantInt(UInt32(1))
259+
@test convert(UInt, constval) == 1
260+
end
261+
262+
263+
## floating point constants
250264

251265
t2 = LLVM.DoubleType(ctx)
252266
c = ConstantFP(t2, 1.1)
@@ -295,7 +309,7 @@ LLVM.Module("SomeModule", ctx) do mod
295309
show(DevNull, gv)
296310

297311
@test_throws NullException initializer(gv)
298-
init = ConstantInt(LLVM.Int32Type(), 0)
312+
init = ConstantInt(Int32(0))
299313
initializer!(gv, init)
300314
@test initializer(gv) == init
301315

0 commit comments

Comments
 (0)