Skip to content

Commit 096670d

Browse files
authored
Move code around for easier VS Code integration (#401)
1 parent 7d21908 commit 096670d

File tree

2 files changed

+132
-121
lines changed

2 files changed

+132
-121
lines changed

src/JuliaInterpreter.jl

Lines changed: 9 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,16 @@
11
module JuliaInterpreter
22

3-
using Base.Meta
4-
import Base: +, -, convert, isless
5-
using Core: CodeInfo, SimpleVector, LineInfoNode, GotoNode, Slot,
6-
GeneratedFunctionStub, MethodInstance, NewvarNode, TypeName
3+
# We use a code structure where all `using` and `import`
4+
# statements in the package that load anything other than
5+
# a Julia base or stdlib package are located in this file here.
6+
# Nothing else should appear in this file here, apart from
7+
# the `include("packagedef.jl")` statement, which loads what
8+
# we would normally consider the bulk of the package code.
9+
# This somewhat unusual structure is in place to support
10+
# the VS Code extension integration.
711

8-
using UUIDs
9-
using Random
10-
# The following are for circumventing #28, memcpy invalid instruction error,
11-
# in Base and stdlib
12-
using Random.DSFMT
13-
using InteractiveUtils
1412
using CodeTracking
1513

16-
export @interpret, Compiled, Frame, root, leaf,
17-
BreakpointRef, breakpoint, @breakpoint, breakpoints, enable, disable, remove, toggle,
18-
debug_command, @bp, break_on, break_off, on_breakpoints_updated
19-
20-
module CompiledCalls
21-
# This module is for handling intrinsics that must be compiled (llvmcall) as well as ccalls
22-
end
23-
24-
# "Backport" of https://github.com/JuliaLang/julia/pull/31536
25-
if VERSION < v"1.2.0-DEV.572"
26-
Base.convert(::Type{Some{T}}, x::Some{T}) where {T} = x
27-
end
28-
29-
const SlotNamesType = VERSION < v"1.2.0-DEV.606" ? Vector{Any} : Vector{Symbol}
30-
31-
@static if VERSION < v"1.3.0-DEV.179"
32-
const append_any = Base.append_any
33-
else
34-
append_any(@nospecialize x...) = append!([], Core.svec((x...)...))
35-
end
36-
37-
include("types.jl")
38-
include("utils.jl")
39-
include("construct.jl")
40-
include("localmethtable.jl")
41-
include("interpret.jl")
42-
include("builtins.jl")
43-
include("optimize.jl")
44-
include("commands.jl")
45-
include("breakpoints.jl")
46-
47-
function set_compiled_methods()
48-
###########
49-
# Methods #
50-
###########
51-
# Work around #28 by preventing interpretation of all Base methods that have a ccall to memcpy
52-
push!(compiled_methods, which(vcat, (Vector,)))
53-
push!(compiled_methods, first(methods(Base._getindex_ra)))
54-
push!(compiled_methods, first(methods(Base._setindex_ra!)))
55-
push!(compiled_methods, which(Base.decompose, (BigFloat,)))
56-
push!(compiled_methods, which(DSFMT.dsfmt_jump, (DSFMT.DSFMT_state, DSFMT.GF2X)))
57-
@static if Sys.iswindows()
58-
push!(compiled_methods, which(InteractiveUtils.clipboard, (AbstractString,)))
59-
end
60-
# issue #76
61-
push!(compiled_methods, which(unsafe_store!, (Ptr{Any}, Any, Int)))
62-
push!(compiled_methods, which(unsafe_store!, (Ptr, Any, Int)))
63-
# issue #92
64-
push!(compiled_methods, which(objectid, Tuple{Any}))
65-
# issue #106 --- anything that uses sigatomic_(begin|end)
66-
push!(compiled_methods, which(flush, Tuple{IOStream}))
67-
push!(compiled_methods, which(disable_sigint, Tuple{Function}))
68-
push!(compiled_methods, which(reenable_sigint, Tuple{Function}))
69-
# Signal-handling in the `print` dispatch hierarchy
70-
push!(compiled_methods, which(Base.unsafe_write, Tuple{Base.LibuvStream, Ptr{UInt8}, UInt}))
71-
push!(compiled_methods, which(print, Tuple{IO, Any}))
72-
push!(compiled_methods, which(print, Tuple{IO, Any, Any}))
73-
# Libc.GetLastError()
74-
@static if Sys.iswindows()
75-
push!(compiled_methods, which(Base.access_env, Tuple{Function, AbstractString}))
76-
push!(compiled_methods, which(Base._hasenv, Tuple{Vector{UInt16}}))
77-
end
78-
# These are currently extremely slow to interpret (https://github.com/JuliaDebug/JuliaInterpreter.jl/issues/193)
79-
push!(compiled_methods, which(subtypes, Tuple{Module, Type}))
80-
push!(compiled_methods, which(subtypes, Tuple{Type}))
81-
push!(compiled_methods, which(match, Tuple{Regex, String, Int, UInt32}))
82-
83-
# Anything that ccalls jl_typeinf_begin cannot currently be handled
84-
for finf in (Core.Compiler.typeinf_code, Core.Compiler.typeinf_ext, Core.Compiler.typeinf_type)
85-
for m in methods(finf)
86-
push!(compiled_methods, m)
87-
end
88-
end
89-
90-
###########
91-
# Modules #
92-
###########
93-
push!(compiled_modules, Base.Threads)
94-
end
95-
96-
function __init__()
97-
set_compiled_methods()
98-
# If we interpret into Core.Compiler, we need to take precautions to avoid needing
99-
# inference of JuliaInterpreter methods in the middle of a `ccall(:jl_typeinf_begin, ...)`
100-
# block.
101-
# for (sym, RT, AT) in ((:jl_typeinf_begin, Cvoid, ()),
102-
# (:jl_typeinf_end, Cvoid, ()),
103-
# (:jl_isa_compileable_sig, Int32, (Any, Any)),
104-
# (:jl_compress_ast, Any, (Any, Any)),
105-
# # (:jl_set_method_inferred, Ref{Core.CodeInstance}, (Any, Any, Any, Any, Int32, UInt, UInt)),
106-
# (:jl_method_instance_add_backedge, Cvoid, (Any, Any)),
107-
# (:jl_method_table_add_backedge, Cvoid, (Any, Any, Any)),
108-
# (:jl_new_code_info_uninit, Ref{CodeInfo}, ()),
109-
# (:jl_uncompress_argnames, Vector{Symbol}, (Any,)),
110-
# (:jl_get_tls_world_age, UInt, ()),
111-
# (:jl_call_in_typeinf_world, Any, (Ptr{Ptr{Cvoid}}, Cint)),
112-
# (:jl_value_ptr, Any, (Ptr{Cvoid},)),
113-
# (:jl_value_ptr, Ptr{Cvoid}, (Any,)))
114-
# fname = Symbol(:ccall_, sym)
115-
# qsym = QuoteNode(sym)
116-
# argnames = [Symbol(:arg_, string(i)) for i = 1:length(AT)]
117-
# TAT = Expr(:tuple, [parametric_type_to_expr(t) for t in AT]...)
118-
# def = :($fname($(argnames...)) = ccall($qsym, $RT, $TAT, $(argnames...)))
119-
# f = Core.eval(Core.Compiler, def)
120-
# compiled_calls[(qsym, RT, Core.svec(AT...), Core.Compiler)] = f
121-
# precompile(f, AT)
122-
# end
123-
end
124-
125-
include("precompile.jl")
126-
_precompile_()
14+
include("packagedef.jl")
12715

12816
end # module

src/packagedef.jl

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using Base.Meta
2+
import Base: +, -, convert, isless
3+
using Core: CodeInfo, SimpleVector, LineInfoNode, GotoNode, Slot,
4+
GeneratedFunctionStub, MethodInstance, NewvarNode, TypeName
5+
6+
using UUIDs
7+
using Random
8+
# The following are for circumventing #28, memcpy invalid instruction error,
9+
# in Base and stdlib
10+
using Random.DSFMT
11+
using InteractiveUtils
12+
13+
export @interpret, Compiled, Frame, root, leaf,
14+
BreakpointRef, breakpoint, @breakpoint, breakpoints, enable, disable, remove, toggle,
15+
debug_command, @bp, break_on, break_off, on_breakpoints_updated
16+
17+
module CompiledCalls
18+
# This module is for handling intrinsics that must be compiled (llvmcall) as well as ccalls
19+
end
20+
21+
# "Backport" of https://github.com/JuliaLang/julia/pull/31536
22+
if VERSION < v"1.2.0-DEV.572"
23+
Base.convert(::Type{Some{T}}, x::Some{T}) where {T} = x
24+
end
25+
26+
const SlotNamesType = VERSION < v"1.2.0-DEV.606" ? Vector{Any} : Vector{Symbol}
27+
28+
@static if VERSION < v"1.3.0-DEV.179"
29+
const append_any = Base.append_any
30+
else
31+
append_any(@nospecialize x...) = append!([], Core.svec((x...)...))
32+
end
33+
34+
include("types.jl")
35+
include("utils.jl")
36+
include("construct.jl")
37+
include("localmethtable.jl")
38+
include("interpret.jl")
39+
include("builtins.jl")
40+
include("optimize.jl")
41+
include("commands.jl")
42+
include("breakpoints.jl")
43+
44+
function set_compiled_methods()
45+
###########
46+
# Methods #
47+
###########
48+
# Work around #28 by preventing interpretation of all Base methods that have a ccall to memcpy
49+
push!(compiled_methods, which(vcat, (Vector,)))
50+
push!(compiled_methods, first(methods(Base._getindex_ra)))
51+
push!(compiled_methods, first(methods(Base._setindex_ra!)))
52+
push!(compiled_methods, which(Base.decompose, (BigFloat,)))
53+
push!(compiled_methods, which(DSFMT.dsfmt_jump, (DSFMT.DSFMT_state, DSFMT.GF2X)))
54+
@static if Sys.iswindows()
55+
push!(compiled_methods, which(InteractiveUtils.clipboard, (AbstractString,)))
56+
end
57+
# issue #76
58+
push!(compiled_methods, which(unsafe_store!, (Ptr{Any}, Any, Int)))
59+
push!(compiled_methods, which(unsafe_store!, (Ptr, Any, Int)))
60+
# issue #92
61+
push!(compiled_methods, which(objectid, Tuple{Any}))
62+
# issue #106 --- anything that uses sigatomic_(begin|end)
63+
push!(compiled_methods, which(flush, Tuple{IOStream}))
64+
push!(compiled_methods, which(disable_sigint, Tuple{Function}))
65+
push!(compiled_methods, which(reenable_sigint, Tuple{Function}))
66+
# Signal-handling in the `print` dispatch hierarchy
67+
push!(compiled_methods, which(Base.unsafe_write, Tuple{Base.LibuvStream,Ptr{UInt8},UInt}))
68+
push!(compiled_methods, which(print, Tuple{IO,Any}))
69+
push!(compiled_methods, which(print, Tuple{IO,Any,Any}))
70+
# Libc.GetLastError()
71+
@static if Sys.iswindows()
72+
push!(compiled_methods, which(Base.access_env, Tuple{Function,AbstractString}))
73+
push!(compiled_methods, which(Base._hasenv, Tuple{Vector{UInt16}}))
74+
end
75+
# These are currently extremely slow to interpret (https://github.com/JuliaDebug/JuliaInterpreter.jl/issues/193)
76+
push!(compiled_methods, which(subtypes, Tuple{Module,Type}))
77+
push!(compiled_methods, which(subtypes, Tuple{Type}))
78+
push!(compiled_methods, which(match, Tuple{Regex,String,Int,UInt32}))
79+
80+
# Anything that ccalls jl_typeinf_begin cannot currently be handled
81+
for finf in (Core.Compiler.typeinf_code, Core.Compiler.typeinf_ext, Core.Compiler.typeinf_type)
82+
for m in methods(finf)
83+
push!(compiled_methods, m)
84+
end
85+
end
86+
87+
###########
88+
# Modules #
89+
###########
90+
push!(compiled_modules, Base.Threads)
91+
end
92+
93+
function __init__()
94+
set_compiled_methods()
95+
# If we interpret into Core.Compiler, we need to take precautions to avoid needing
96+
# inference of JuliaInterpreter methods in the middle of a `ccall(:jl_typeinf_begin, ...)`
97+
# block.
98+
# for (sym, RT, AT) in ((:jl_typeinf_begin, Cvoid, ()),
99+
# (:jl_typeinf_end, Cvoid, ()),
100+
# (:jl_isa_compileable_sig, Int32, (Any, Any)),
101+
# (:jl_compress_ast, Any, (Any, Any)),
102+
# # (:jl_set_method_inferred, Ref{Core.CodeInstance}, (Any, Any, Any, Any, Int32, UInt, UInt)),
103+
# (:jl_method_instance_add_backedge, Cvoid, (Any, Any)),
104+
# (:jl_method_table_add_backedge, Cvoid, (Any, Any, Any)),
105+
# (:jl_new_code_info_uninit, Ref{CodeInfo}, ()),
106+
# (:jl_uncompress_argnames, Vector{Symbol}, (Any,)),
107+
# (:jl_get_tls_world_age, UInt, ()),
108+
# (:jl_call_in_typeinf_world, Any, (Ptr{Ptr{Cvoid}}, Cint)),
109+
# (:jl_value_ptr, Any, (Ptr{Cvoid},)),
110+
# (:jl_value_ptr, Ptr{Cvoid}, (Any,)))
111+
# fname = Symbol(:ccall_, sym)
112+
# qsym = QuoteNode(sym)
113+
# argnames = [Symbol(:arg_, string(i)) for i = 1:length(AT)]
114+
# TAT = Expr(:tuple, [parametric_type_to_expr(t) for t in AT]...)
115+
# def = :($fname($(argnames...)) = ccall($qsym, $RT, $TAT, $(argnames...)))
116+
# f = Core.eval(Core.Compiler, def)
117+
# compiled_calls[(qsym, RT, Core.svec(AT...), Core.Compiler)] = f
118+
# precompile(f, AT)
119+
# end
120+
end
121+
122+
include("precompile.jl")
123+
_precompile_()

0 commit comments

Comments
 (0)