Skip to content

Commit eb87ecd

Browse files
committed
support test_codeinfo
1 parent 9b96145 commit eb87ecd

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

src/CompilerPluginTools.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export
5757
method_instance,
5858
@make_codeinfo,
5959
@make_ircode,
60+
@test_codeinfo,
6061
@intrinsic_stub
6162

6263
using Base:

src/utils.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,39 @@ function method_instance(@nospecialize(f), @nospecialize(tt), world=Base.get_wor
7474
return ccall(:jl_specializations_get_linfo, Ref{Core.MethodInstance},
7575
(Any, Any, Any, UInt), meth, ti, env, world)
7676
end
77+
78+
macro test_codeinfo(ci, ex)
79+
esc(test_codeinfo_m(ci, ex))
80+
end
81+
82+
function test_codeinfo_m(ci, ex::Expr)
83+
Meta.isexpr(ex, :block) || error("expect a begin ... end")
84+
ret = Expr(:block)
85+
stmt_count = 1
86+
for each in ex.args
87+
@switch each begin
88+
@case :($stmt::$type)
89+
push!(ret.args, quote
90+
@test $MLStyle.@match $ci.code[$stmt_count] begin
91+
$stmt => true
92+
_ => false
93+
end
94+
end)
95+
push!(ret.args, quote
96+
@test $ci.ssavaluetypes[$stmt_count] == $type
97+
end)
98+
stmt_count += 1
99+
@case ::LineNumberNode
100+
continue
101+
@case _
102+
push!(ret.args, quote
103+
@test $MLStyle.@match $ci.code[$stmt_count] begin
104+
$each => true
105+
_ => false
106+
end
107+
end)
108+
stmt_count += 1
109+
end
110+
end
111+
return ret
112+
end

test/codeinfo.jl

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ end
2020
end
2121
end
2222

23-
@test test_ci.code[1] == :(overdub($(GlobalRef(Main, :>)), $(SlotNumber(2)), 0))
24-
@test test_ci.code[2] isa GotoIfNot
25-
@test test_ci.code[3] == :(overdub($(GlobalRef(Main, :*)), 2, $(SlotNumber(2))))
26-
@test test_ci.code[4] == ReturnNode(SSAValue(3))
27-
@test test_ci.code[5] == :(overdub($(GlobalRef(Main, :*)), 3, $(SlotNumber(2))))
28-
@test test_ci.code[6] == ReturnNode(SSAValue(5))
23+
@test_codeinfo test_ci begin
24+
:(overdub($(GlobalRef(Main, :>)), $(SlotNumber(2)), 0))
25+
::GotoIfNot
26+
:(overdub($(GlobalRef(Main, :*)), 2, $(SlotNumber(2))))
27+
ReturnNode(SSAValue(3))
28+
:(overdub($(GlobalRef(Main, :*)), 3, $(SlotNumber(2))))
29+
ReturnNode(SSAValue(5))
30+
end
2931
end
3032

3133
@testset "NewCodeInfo" begin
@@ -45,12 +47,17 @@ end
4547
end
4648
test_ci = finish(new)
4749

48-
@test test_ci.code[1] == :(1 + 1)
49-
@test test_ci.code[2] isa GotoIfNot
50-
@test test_ci.code[3] == "variable: %3"
51-
@test test_ci.code[4] == :(overdub($(SSAValue(3)), $(GlobalRef(Main, :*)), 2, $(SlotNumber(2))))
52-
@test test_ci.code[5] == ReturnNode(SSAValue(4))
53-
@test test_ci.code[6] == "variable: %5"
50+
@test_codeinfo test_ci begin
51+
:(1 + 1)
52+
::GotoIfNot
53+
54+
"variable: %3"
55+
56+
:(overdub($(SSAValue(3)), $(GlobalRef(Main, :*)), 2, $(SlotNumber(2))))
57+
ReturnNode(SSAValue(4))
58+
59+
"variable: %5"
60+
end
5461

5562
@testset "slot insertion" begin
5663
ci = code_lowered(foo, (Float64, ))[1]

0 commit comments

Comments
 (0)