Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
simeonschaub committed Jan 6, 2022
1 parent c12180e commit a633b01
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 8 deletions.
1 change: 1 addition & 0 deletions base/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ modifyproperty!(x, f::Symbol, op, v, order::Symbol=:notatomic) =
replaceproperty!(x, f::Symbol, expected, desired, success_order::Symbol=:notatomic, fail_order::Symbol=success_order) =
(@inline; Core.replacefield!(x, f, expected, convert(fieldtype(typeof(x), f), desired), success_order, fail_order))

convert(::Type{Any}, Core.@nospecialize x) = x

include("coreio.jl")

Expand Down
7 changes: 6 additions & 1 deletion base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ export
# constants
nothing, Main

function convert end
ccall(:jl_toplevel_eval_in, Any, (Any, Any),
Core, quote
(::typeof(convert))(::Type{Any}, $(_expr(:meta, :nospecialize, :x))) = x
end)

const getproperty = getfield
const setproperty! = setfield!

Expand Down Expand Up @@ -437,7 +443,6 @@ end
# simple convert for use by constructors of types in Core
# note that there is no actual conversion defined here,
# so the methods and ccall's in Core aren't permitted to use convert
convert(::Type{Any}, @nospecialize(x)) = x
convert(::Type{T}, x::T) where {T} = x
cconvert(::Type{T}, x) where {T} = convert(T, x)
unsafe_convert(::Type{T}, x::T) where {T} = x
Expand Down
2 changes: 1 addition & 1 deletion base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1809,7 +1809,7 @@ function abstract_eval_global(M::Module, s::Symbol)
if isdefined(M,s) && isconst(M,s)
return Const(getfield(M,s))
end
return ccall(:jl_get_binding_type, Any, (Any, Any), M, s)
return Core._get_binding_type(M, s)
end

function abstract_eval_ssavalue(s::SSAValue, src::CodeInfo)
Expand Down
2 changes: 2 additions & 0 deletions base/compiler/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const replaceproperty! = Core.replacefield!

ccall(:jl_set_istopmod, Cvoid, (Any, Bool), Compiler, false)

convert(::Type{Any}, Core.@nospecialize x) = x

eval(x) = Core.eval(Compiler, x)
eval(m, x) = Core.eval(m, x)

Expand Down
2 changes: 1 addition & 1 deletion base/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ const _PURE_BUILTINS = Any[tuple, svec, ===, typeof, nfields]
const _PURE_OR_ERROR_BUILTINS = [
fieldtype, apply_type, isa, UnionAll,
getfield, arrayref, const_arrayref, isdefined, Core.sizeof,
Core.kwfunc, Core.ifelse, Core._typevar, (<:)
Core.kwfunc, Core.ifelse, Core._typevar, (<:), Core._get_binding_type,
]

const TOP_TUPLE = GlobalRef(Core, :tuple)
Expand Down
12 changes: 12 additions & 0 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,9 @@ function _builtin_nothrow(@nospecialize(f), argtypes::Array{Any,1}, @nospecializ
elseif f === Core.ifelse
length(argtypes) == 3 || return false
return argtypes[1] Bool
elseif f === Core._get_binding_type
length(argtypes) == 2 || return false
return argtypes[1] Module && argtypes[2] Symbol
end
return false
end
Expand Down Expand Up @@ -1763,4 +1766,13 @@ function typename_static(@nospecialize(t))
return isType(t) ? _typename(t.parameters[1]) : Core.TypeName
end

function get_binding_type_tfunc(@nospecialize(M), @nospecialize(s))
if M isa Const && widenconst(M) === Module &&
s isa Const && widenconst(s) === Symbol
return Type{Core._get_binding_type(M.val, s.val)}
end
return Type
end
add_tfunc(Core._get_binding_type, 2, 2, get_binding_type_tfunc, 0)

@specialize
1 change: 0 additions & 1 deletion base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ See also: [`round`](@ref), [`trunc`](@ref), [`oftype`](@ref), [`reinterpret`](@r
function convert end

convert(::Type{Union{}}, @nospecialize x) = throw(MethodError(convert, (Union{}, x)))
convert(::Type{Any}, @nospecialize x) = x
convert(::Type{T}, x::T) where {T} = x
convert(::Type{Type}, x::Type) = x # the ssair optimizer is strongly dependent on this method existing to avoid over-specialization
# in the absence of inlining-enabled
Expand Down
1 change: 1 addition & 0 deletions src/builtin_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ JL_CALLABLE(jl_f__abstracttype);
JL_CALLABLE(jl_f__primitivetype);
JL_CALLABLE(jl_f__setsuper);
JL_CALLABLE(jl_f__equiv_typedef);
JL_CALLABLE(jl_f__get_binding_type);
JL_CALLABLE(jl_f__set_binding_type);

#ifdef __cplusplus
Expand Down
12 changes: 12 additions & 0 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -1655,6 +1655,17 @@ JL_CALLABLE(jl_f__equiv_typedef)
return equiv_type(args[0], args[1]) ? jl_true : jl_false;
}

JL_CALLABLE(jl_f__get_binding_type)
{
JL_NARGS(_get_binding_type, 2, 2);
JL_TYPECHK(_get_binding_type, module, args[0]);
JL_TYPECHK(_get_binding_type, symbol, args[1]);
jl_binding_t *b = jl_get_binding((jl_module_t*)args[0], (jl_sym_t*)args[1]);
if (b == NULL || b->ty == NULL)
return (jl_value_t*)jl_any_type;
return b->ty;
}

JL_CALLABLE(jl_f__set_binding_type)
{
JL_NARGS(_set_binding_type!, 3, 3);
Expand Down Expand Up @@ -1863,6 +1874,7 @@ void jl_init_primitives(void) JL_GC_DISABLED
add_builtin_func("_setsuper!", jl_f__setsuper);
jl_builtin__typebody = add_builtin_func("_typebody!", jl_f__typebody);
add_builtin_func("_equiv_typedef", jl_f__equiv_typedef);
add_builtin_func("_get_binding_type", jl_f__get_binding_type);
add_builtin_func("_set_binding_type!", jl_f__set_binding_type);

// builtin types
Expand Down
8 changes: 6 additions & 2 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -3405,7 +3405,9 @@ f(x) = yt(x)
(make-ssavalue)))
(rhs (if (equal? vt '(core Any))
(if (and (not closed) (not capt))
(convert-for-type-decl rhs1 (get globals (binding-to-globalref var) '(core Any)))
(let* ((ref (binding-to-globalref var))
(ty `(call (core _get_binding_type) ,(cadr ref) (inert ,(caddr ref)))))
(convert-for-type-decl rhs1 ty))
rhs1)
(convert-for-type-decl rhs1 (cl-convert vt fname lam #f #f #f interp opaq globals))))
(ex (cond (closed `(call (core setfield!)
Expand All @@ -3426,7 +3428,9 @@ f(x) = yt(x)
(equal? rhs0 '(the_exception)))
rhs0
(make-ssavalue)))
(ex `(= ,var ,(convert-for-type-decl rhs1 (get globals (binding-to-globalref var) '(core Any))))))
(ref (binding-to-globalref var))
(ty `(call (core _get_binding_type) ,(cadr ref) (inert ,(caddr ref))))
(ex `(= ,var ,(convert-for-type-decl rhs1 ty))))
(if (eq? rhs1 rhs0)
`(block ,ex ,rhs0)
`(block (= ,rhs1 ,rhs0)
Expand Down
4 changes: 2 additions & 2 deletions src/staticdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ static const jl_fptr_args_t id_to_fptrs[] = {
&jl_f_arrayref, &jl_f_const_arrayref, &jl_f_arrayset, &jl_f_arraysize, &jl_f_apply_type,
&jl_f_applicable, &jl_f_invoke, &jl_f_sizeof, &jl_f__expr, &jl_f__typevar,
&jl_f_ifelse, &jl_f__structtype, &jl_f__abstracttype, &jl_f__primitivetype,
&jl_f__typebody, &jl_f__setsuper, &jl_f__equiv_typedef, &jl_f__set_binding_type,
&jl_f_opaque_closure_call,
&jl_f__typebody, &jl_f__setsuper, &jl_f__equiv_typedef, &jl_f__get_binding_type,
&jl_f__set_binding_type, &jl_f_opaque_closure_call,
NULL };

typedef struct {
Expand Down

0 comments on commit a633b01

Please sign in to comment.