Skip to content

Commit

Permalink
hook up runtime-intrinsics
Browse files Browse the repository at this point in the history
now IntrinsicFunctions works just like any other BuiltinFunction
(except for ccall and llvmcall, which don't yet have runtime versions)
  • Loading branch information
vtjnash committed Sep 7, 2016
1 parent 017d6a2 commit 8f75347
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 26 deletions.
6 changes: 5 additions & 1 deletion base/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ end
function show(io::IO, ::MIME"text/plain", f::Function)
ft = typeof(f)
mt = ft.name.mt
if isa(f, Core.Builtin)
if isa(f, Core.IntrinsicFunction)
show(io, f)
id = Core.Intrinsics.box(Int32, f)
print(io, " (intrinsic function #$id)")
elseif isa(f, Core.Builtin)
print(io, mt.name, " (built-in function)")
else
name = mt.name
Expand Down
3 changes: 2 additions & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ function show(io::IO, f::Function)
end

function show(io::IO, x::Core.IntrinsicFunction)
print(io, "(intrinsic function #", box(Int32, unbox(Core.IntrinsicFunction, x)), ")")
name = ccall(:jl_intrinsic_name, Cstring, (Core.IntrinsicFunction,), x)
print(io, unsafe_string(name))
end

function show(io::IO, x::Union)
Expand Down
2 changes: 1 addition & 1 deletion src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ jl_fptr_t jl_get_builtin_fptr(jl_value_t *b)

static void add_builtin_func(const char *name, jl_fptr_t fptr)
{
add_builtin(name, jl_mk_builtin_func(name, fptr));
jl_mk_builtin_func(NULL, name, fptr);
}

void jl_init_primitives(void)
Expand Down
17 changes: 15 additions & 2 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -2050,7 +2050,13 @@ static void jl_save_system_image_to_stream(ios_t *f)
jl_serialize_value(&s, jl_main_module);
jl_serialize_value(&s, jl_top_module);
jl_serialize_value(&s, jl_typeinf_func);

// deserialize method tables of builtin types
jl_serialize_value(&s, jl_type_type->name->mt);
jl_serialize_value(&s, jl_intrinsic_type->name->mt);
jl_serialize_value(&s, jl_sym_type->name->mt);
jl_serialize_value(&s, jl_array_type->name->mt);
jl_serialize_value(&s, jl_module_type->name->mt);

jl_prune_type_cache(jl_tuple_typename->cache);
jl_prune_type_cache(jl_tuple_typename->linearcache);
Expand Down Expand Up @@ -2146,10 +2152,17 @@ static void jl_restore_system_image_from_stream(ios_t *f)
jl_main_module = (jl_module_t*)jl_deserialize_value(&s, NULL);
jl_top_module = (jl_module_t*)jl_deserialize_value(&s, NULL);
jl_internal_main_module = jl_main_module;

jl_typeinf_func = (jl_function_t*)jl_deserialize_value(&s, NULL);
jl_type_type_mt = (jl_methtable_t*)jl_deserialize_value(&s, NULL);
jl_type_type->name->mt = jl_typector_type->name->mt = jl_uniontype_type->name->mt = jl_datatype_type->name->mt =
jl_type_type_mt;
jl_type_type->name->mt = jl_type_type_mt;
jl_typector_type->name->mt = jl_type_type_mt;
jl_uniontype_type->name->mt = jl_type_type_mt;
jl_datatype_type->name->mt = jl_type_type_mt;
jl_intrinsic_type->name->mt = (jl_methtable_t*)jl_deserialize_value(&s, NULL);
jl_sym_type->name->mt = (jl_methtable_t*)jl_deserialize_value(&s, NULL);
jl_array_type->name->mt = (jl_methtable_t*)jl_deserialize_value(&s, NULL);
jl_module_type->name->mt = (jl_methtable_t*)jl_deserialize_value(&s, NULL);

intptr_t i;
for(i=0; i < builtin_types.len; i++) {
Expand Down
13 changes: 8 additions & 5 deletions src/gf.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,14 @@ JL_DLLEXPORT jl_value_t *jl_methtable_lookup(jl_methtable_t *mt, jl_tupletype_t

JL_DLLEXPORT jl_method_t *jl_new_method_uninit(void);
static jl_function_t *jl_new_generic_function_with_supertype(jl_sym_t *name, jl_module_t *module, jl_datatype_t *st, int iskw);
jl_value_t *jl_mk_builtin_func(const char *name, jl_fptr_t fptr)
void jl_mk_builtin_func(jl_datatype_t *dt, const char *name, jl_fptr_t fptr)
{
jl_sym_t *sname = jl_symbol(name);
jl_value_t *f = jl_new_generic_function_with_supertype(sname, jl_core_module, jl_builtin_type, 0);
if (dt == NULL) {
jl_value_t *f = jl_new_generic_function_with_supertype(sname, jl_core_module, jl_builtin_type, 0);
jl_set_const(jl_core_module, sname, f);
dt = (jl_datatype_t*)jl_typeof(f);
}
jl_lambda_info_t *li = jl_new_lambda_info_uninit();
li->fptr = fptr;
li->code = jl_nothing;
Expand All @@ -168,14 +172,13 @@ jl_value_t *jl_mk_builtin_func(const char *name, jl_fptr_t fptr)

li->def = jl_new_method_uninit();
li->def->name = sname;
// li->def->module will be set to jl_core_module by init.c
li->def->module = jl_core_module;
li->def->lambda_template = li;
li->def->sig = jl_anytuple_type;
li->def->tvars = jl_emptysvec;

jl_methtable_t *mt = jl_gf_mtable(f);
jl_methtable_t *mt = dt->name->mt;
jl_typemap_insert(&mt->cache, (jl_value_t*)mt, jl_anytuple_type, jl_emptysvec, NULL, jl_emptysvec, (jl_value_t*)li, 0, &lambda_cache, NULL);
return f;
}

/*
Expand Down
26 changes: 13 additions & 13 deletions src/intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ enum intrinsic {
#ifdef __cplusplus
extern "C"
#endif
const char *jl_intrinsic_name(int f)
JL_DLLEXPORT const char *jl_intrinsic_name(int f)
{
switch ((enum intrinsic)f) {
default: return "invalid";
Expand Down Expand Up @@ -153,23 +153,25 @@ extern "C"
JL_CALLABLE(jl_f_intrinsic_call)
{
JL_NARGSV(intrinsic_call, 1);
JL_TYPECHK(intrinsic_call, intrinsic, args[0]);
enum intrinsic f = (enum intrinsic)*(uint32_t*)jl_data_ptr(args[0]);
if (f == fptoui && nargs == 2)
JL_TYPECHK(intrinsic_call, intrinsic, F);
enum intrinsic f = (enum intrinsic)*(uint32_t*)jl_data_ptr(F);
if (f == fptoui && nargs == 1)
f = fptoui_auto;
if (f == fptosi && nargs == 2)
if (f == fptosi && nargs == 1)
f = fptosi_auto;
if (f == cglobal && nargs == 2)
if (f == cglobal && nargs == 1)
f = cglobal_auto;
unsigned fargs = intrinsic_nargs[f];
JL_NARGS(intrinsic_call, 1 + fargs, 1 + fargs);
if (!fargs)
jl_error("this intrinsic must be compiled to be called");
JL_NARGS(intrinsic_call, fargs, fargs);
switch (fargs) {
case 1:
return ((intrinsic_call_1_arg)runtime_fp[f])(args[1]);
return ((intrinsic_call_1_arg)runtime_fp[f])(args[0]);
case 2:
return ((intrinsic_call_2_arg)runtime_fp[f])(args[1], args[2]);
return ((intrinsic_call_2_arg)runtime_fp[f])(args[0], args[1]);
case 3:
return ((intrinsic_call_3_arg)runtime_fp[f])(args[1], args[2], args[3]);
return ((intrinsic_call_3_arg)runtime_fp[f])(args[0], args[1], args[2]);
default:
assert(0 && "unexpected number of arguments to an intrinsic function");
}
Expand Down Expand Up @@ -215,6 +217,7 @@ void jl_init_intrinsic_functions(void)
jl_module_t *inm = jl_new_module(jl_symbol("Intrinsics"));
inm->parent = jl_core_module;
jl_set_const(jl_core_module, jl_symbol("Intrinsics"), (jl_value_t*)inm);
jl_mk_builtin_func(jl_intrinsic_type, "IntrinsicFunction", jl_f_intrinsic_call);

#define ADD_I(name, nargs) add_intrinsic(inm, #name, name);
#define ADD_HIDDEN(name, nargs)
Expand All @@ -223,7 +226,4 @@ void jl_init_intrinsic_functions(void)
#undef ADD_I
#undef ADD_HIDDEN
#undef ALIAS

jl_set_const(inm, jl_symbol("intrinsic_call"),
jl_mk_builtin_func("intrinsic_call", jl_f_intrinsic_call));
}
2 changes: 1 addition & 1 deletion src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -3947,7 +3947,7 @@ void jl_init_types(void)
jl_type_type->name->mt;

jl_intrinsic_type = jl_new_bitstype((jl_value_t*)jl_symbol("IntrinsicFunction"),
jl_any_type, jl_emptysvec, 32);
jl_builtin_type, jl_emptysvec, 32);

tv = jl_svec1(tvar("T"));
jl_ref_type =
Expand Down
4 changes: 2 additions & 2 deletions src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ int jl_types_equal_generic(jl_value_t *a, jl_value_t *b, int useenv);
jl_datatype_t *jl_inst_concrete_tupletype_v(jl_value_t **p, size_t np);
jl_datatype_t *jl_inst_concrete_tupletype(jl_svec_t *p);
void jl_method_table_insert(jl_methtable_t *mt, jl_method_t *method, jl_tupletype_t *simpletype);
jl_value_t *jl_mk_builtin_func(const char *name, jl_fptr_t fptr);
void jl_mk_builtin_func(jl_datatype_t *dt, const char *name, jl_fptr_t fptr);
STATIC_INLINE int jl_is_type(jl_value_t *v)
{
jl_value_t *t = jl_typeof(v);
Expand Down Expand Up @@ -528,7 +528,7 @@ extern JL_DLLEXPORT jl_value_t *jl_segv_exception;
#endif

// -- Runtime intrinsics -- //
const char *jl_intrinsic_name(int f);
JL_DLLEXPORT const char *jl_intrinsic_name(int f);

JL_DLLEXPORT jl_value_t *jl_reinterpret(jl_value_t *ty, jl_value_t *v);
JL_DLLEXPORT jl_value_t *jl_pointerref(jl_value_t *p, jl_value_t *i, jl_value_t *align);
Expand Down
7 changes: 7 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,13 @@ glotest()
@test glob_x == 88
@test loc_x == 10

# runtime intrinsics

let f = Any[Core.Intrinsics.add_int, Core.Intrinsics.sub_int]
@test f[1](1, 1) == 2
@test f[2](1, 1) == 0
end

# issue #7234
begin
glob_x2 = 24
Expand Down

0 comments on commit 8f75347

Please sign in to comment.