WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 6110f3c

Browse files
authored
[JuliaLowering] Remove use of @SyntaxTree in ccall (JuliaLang#60192)
I'm assuming this macro (which is defined as "go find the file or REPL history entry this Expr came from and re-parse the text to SyntaxTree") is mostly for convenience in interactive environments. It's also breakable at precompile time with changes in the way we expand `quote`, and it takes a noticable amount of time in inference. This change just uses the less-convenient `@ast` form like the rest of the project.
1 parent da57d86 commit 6110f3c

File tree

2 files changed

+50
-57
lines changed

2 files changed

+50
-57
lines changed

JuliaLowering/src/syntax_macros.jl

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,20 @@ function ccall_macro_lower(ctx, ex, convention, func, rettype, types, args, gc_s
189189
]
190190
]
191191
elseif kf == K"$"
192-
check = @SyntaxTree quote
193-
func = $(func[1])
194-
if !isa(func, Ptr{Cvoid})
195-
name = :($(func[1]))
196-
throw(ArgumentError("interpolated function `$name` was not a `Ptr{Cvoid}`, but $(typeof(func))"))
197-
end
198-
end
192+
fid = @ast ctx func[1] "func"::K"Identifier"
193+
check = @ast ctx func [K"block"
194+
[K"=" fid func[1]]
195+
[K"if"
196+
[K"call" (!isa)::K"Value" fid [K"curly" Ptr::K"Value" Cvoid::K"Value"]]
197+
[K"block"
198+
[K"=" "name"::K"Identifier" [K"quote" func[1]]]
199+
[K"call" throw::K"Value"
200+
[K"call" ArgumentError::K"Value"
201+
[K"string"
202+
"interpolated function `"::K"String"
203+
"name"::K"Identifier"
204+
"` was not a `Ptr{Cvoid}`, but "::K"String"
205+
[K"call" typeof::K"Value" fid]]]]]]]
199206
push!(statements, check)
200207
lowered_func = check[1][1]
201208
else
@@ -208,9 +215,11 @@ function ccall_macro_lower(ctx, ex, convention, func, rettype, types, args, gc_s
208215
for (i, (type, arg)) in enumerate(zip(types, args))
209216
argi = @ast ctx arg "arg$i"::K"Identifier"
210217
# TODO: Does it help to emit ssavar() here for the `argi`?
211-
push!(statements, @SyntaxTree :(local $argi = Base.cconvert($type, $arg)))
218+
push!(statements,
219+
@ast ctx arg [K"local"
220+
[K"=" argi [K"call" Base.cconvert::K"Value" type arg]]])
212221
push!(roots, argi)
213-
push!(cargs, @SyntaxTree :(Base.unsafe_convert($type, $argi)))
222+
push!(cargs, @ast ctx ex [K"call" Base.unsafe_convert::K"Value" type argi])
214223
end
215224
effect_flags = UInt16(0)
216225
push!(statements, @ast ctx ex [K"foreigncall"

JuliaLowering/test/misc_ir.jl

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -402,59 +402,43 @@ end
402402
# Basic @ccall lowering
403403
@ccall foo(x::X, y::Y)::R
404404
#---------------------
405-
1 JuliaLowering.Base
406-
2 (call top.getproperty %:cconvert)
407-
3 TestMod.X
408-
4 TestMod.x
409-
5 (= slot₁/arg1 (call %%%₄))
410-
6 JuliaLowering.Base
411-
7 (call top.getproperty %:cconvert)
412-
8 TestMod.Y
413-
9 TestMod.y
414-
10 (= slot₂/arg2 (call %%%₉))
415-
11 JuliaLowering.Base
416-
12 (call top.getproperty %₁₁ :unsafe_convert)
417-
13 TestMod.X
418-
14 slot₁/arg1
419-
15 (call %₁₂ %₁₃ %₁₄)
420-
16 JuliaLowering.Base
421-
17 (call top.getproperty %₁₆ :unsafe_convert)
422-
18 TestMod.Y
423-
19 slot₂/arg2
424-
20 (call %₁₇ %₁₈ %₁₉)
425-
21 slot₁/arg1
426-
22 slot₂/arg2
427-
23 (foreigncall :foo (static_eval TestMod.R) (static_eval (call core.svec TestMod.X TestMod.Y)) 0 :($(QuoteNode((:ccall, 0x0000, false)))) %₁₅ %₂₀ %₂₁ %₂₂)
428-
24 (return %₂₃)
405+
1 TestMod.X
406+
2 TestMod.x
407+
3 (= slot₁/arg1 (call Base.cconvert %%₂))
408+
4 TestMod.Y
409+
5 TestMod.y
410+
6 (= slot₂/arg2 (call Base.cconvert %%₅))
411+
7 TestMod.X
412+
8 slot₁/arg1
413+
9 (call Base.unsafe_convert %%₈)
414+
10 TestMod.Y
415+
11 slot₂/arg2
416+
12 (call Base.unsafe_convert %₁₀ %₁₁)
417+
13 slot₁/arg1
418+
14 slot₂/arg2
419+
15 (foreigncall :foo (static_eval TestMod.R) (static_eval (call core.svec TestMod.X TestMod.Y)) 0 :($(QuoteNode((:ccall, 0x0000, false)))) %%₁₂ %₁₃ %₁₄)
420+
16 (return %₁₅)
429421

430422
########################################
431423
# @ccall lowering with gc_safe
432424
@ccall foo(x::X; y::Y)::R gc_safe=true
433425
#---------------------
434-
1 JuliaLowering.Base
435-
2 (call top.getproperty %:cconvert)
436-
3 TestMod.X
437-
4 TestMod.x
438-
5 (= slot₁/arg1 (call %%%₄))
439-
6 JuliaLowering.Base
440-
7 (call top.getproperty %:cconvert)
441-
8 TestMod.Y
442-
9 TestMod.y
443-
10 (= slot₂/arg2 (call %%%₉))
444-
11 JuliaLowering.Base
445-
12 (call top.getproperty %₁₁ :unsafe_convert)
446-
13 TestMod.X
447-
14 slot₁/arg1
448-
15 (call %₁₂ %₁₃ %₁₄)
449-
16 JuliaLowering.Base
450-
17 (call top.getproperty %₁₆ :unsafe_convert)
451-
18 TestMod.Y
452-
19 slot₂/arg2
453-
20 (call %₁₇ %₁₈ %₁₉)
454-
21 slot₁/arg1
455-
22 slot₂/arg2
456-
23 (foreigncall :foo (static_eval TestMod.R) (static_eval (call core.svec TestMod.X TestMod.Y)) 1 :($(QuoteNode((:ccall, 0x0000, true)))) %₁₅ %₂₀ %₂₁ %₂₂)
457-
24 (return %₂₃)
426+
1 TestMod.X
427+
2 TestMod.x
428+
3 (= slot₁/arg1 (call Base.cconvert %%₂))
429+
4 TestMod.Y
430+
5 TestMod.y
431+
6 (= slot₂/arg2 (call Base.cconvert %%₅))
432+
7 TestMod.X
433+
8 slot₁/arg1
434+
9 (call Base.unsafe_convert %%₈)
435+
10 TestMod.Y
436+
11 slot₂/arg2
437+
12 (call Base.unsafe_convert %₁₀ %₁₁)
438+
13 slot₁/arg1
439+
14 slot₂/arg2
440+
15 (foreigncall :foo (static_eval TestMod.R) (static_eval (call core.svec TestMod.X TestMod.Y)) 1 :($(QuoteNode((:ccall, 0x0000, true)))) %%₁₂ %₁₃ %₁₄)
441+
16 (return %₁₅)
458442

459443
########################################
460444
# non-macro ccall with vararg in signature, but none provided

0 commit comments

Comments
 (0)