From a18fff4ca7042b2960f8717c3c80a5d2683f137c Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Tue, 5 Mar 2019 12:26:35 -0500 Subject: [PATCH] avoid some internal methods and invalid objects --- src/JuliaInterpreter.jl | 18 +++++++++--------- src/localmethtable.jl | 22 +++++++++++++--------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/JuliaInterpreter.jl b/src/JuliaInterpreter.jl index b9f26da47dc47..b528d8c7d904f 100644 --- a/src/JuliaInterpreter.jl +++ b/src/JuliaInterpreter.jl @@ -347,9 +347,15 @@ function prepare_args(@nospecialize(f), allargs, kwargs) return f, allargs end +if VERSION < v"1.2-" || !isdefined(Core.Compiler, :specialize_method) + specialize_method(method::Method, @nospecialize(atypes), sparams::SimpleVector) = + Core.Compiler.code_for_method(method, atypes, sparams, typemax(UInt)) +else + const specialize_method = Core.Compiler.specialize_method +end + function prepare_framecode(method::Method, argtypes; enter_generated=false) sig = method.sig - isa(method, TypeMapEntry) && (method = method.func) if method.module == Core.Compiler || method.module == Base.Threads || method ∈ compiled_methods return Compiled() end @@ -367,7 +373,7 @@ function prepare_framecode(method::Method, argtypes; enter_generated=false) # If we're stepping into a staged function, we need to use # the specialization, rather than stepping through the # unspecialized method. - code = Core.Compiler.get_staged(Core.Compiler.code_for_method(method, argtypes, lenv, typemax(UInt), false)) + code = Core.Compiler.get_staged(specialize_method(method, argtypes, lenv)) code === nothing && return nothing generator = false else @@ -645,13 +651,7 @@ function determine_method_for_expr(expr; enter_generated = false) return prepare_call(f, allargs; enter_generated=enter_generated) end -function get_source(meth) - if isa(meth.source, Array{UInt8,1}) - return ccall(:jl_uncompress_ast, Any, (Any, Any), meth, meth.source) - else - return meth.source - end -end +get_source(meth) = Base.uncompressed_ast(meth) function get_source(g::GeneratedFunctionStub) b = g(g.argnames...) diff --git a/src/localmethtable.jl b/src/localmethtable.jl index 244ec68c38a03..9a9b76ab50466 100644 --- a/src/localmethtable.jl +++ b/src/localmethtable.jl @@ -1,5 +1,10 @@ const max_methods = 4 # maximum number of MethodInstances tracked for a particular :call statement +struct FrameInstance + framecode::JuliaFrameCode + sparam_vals::SimpleVector +end + """ framecode, lenv = get_call_framecode(fargs, parentframe::JuliaFrameCode, idx::Int) @@ -34,9 +39,8 @@ function get_call_framecode(fargs, parentframe::JuliaFrameCode, idx::Int) tmeprev.next = tme.next tme.next = tme1 end - # The framecode is stashed in the `inferred` field of the MethodInstance - mi = tme.func::MethodInstance - return mi.inferred::JuliaFrameCode, mi.sparam_vals + mi = tme.func::FrameInstance + return mi.framecode, mi.sparam_vals end end depth += 1 @@ -53,17 +57,17 @@ function get_call_framecode(fargs, parentframe::JuliaFrameCode, idx::Int) isa(ret, Compiled) && return ret, nothing framecode, args, env, argtypes = ret # Store the results of the method lookup in the local method table + mi = FrameInstance(framecode, env) + # it's sort of odd to call this a TypeMapEntry, then set most of the fields incorrectly + # but since we're just using it as a linked list, it's probably ok tme = ccall(:jl_new_struct_uninit, Any, (Any,), TypeMapEntry)::TypeMapEntry - tme.func = mi = ccall(:jl_new_struct_uninit, Any, (Any,), MethodInstance)::MethodInstance - tme.sig = mi.specTypes = argtypes + tme.func = mi + tme.simplesig = nothing + tme.sig = argtypes tme.isleafsig = true tme.issimplesig = false method = framecode.scope::Method tme.va = method.isva - mi.def = method - mi.rettype = Any - mi.sparam_vals = env - mi.inferred = framecode # a slight abuse, but not insane if isassigned(parentframe.methodtables, idx) tme.next = parentframe.methodtables[idx] # Drop the oldest tme, if necessary