Skip to content

Commit

Permalink
Change creation of exception type
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Jun 1, 2017
1 parent 24666ad commit f954cbb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
39 changes: 26 additions & 13 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2267,6 +2267,31 @@ static bool might_need_root(jl_value_t *ex)
!jl_is_globalref(ex));
}

// Emit an "empty" structure (with uninitialized fields) of type
// ty. Do not use for isbits types.
static jl_cgval_t emit_uninitialized_struct(jl_value_t *ty, jl_codectx_t *ctx)
{
assert(jl_is_datatype(ty));
assert(jl_is_leaf_type(ty));
jl_datatype_t *sty = (jl_datatype_t*)ty;
assert(!jl_isbits(sty));
size_t nf = jl_datatype_nfields(sty);
Value *strct = emit_allocobj(ctx, jl_datatype_size(sty),
literal_pointer_val((jl_value_t*)ty));
jl_cgval_t strctinfo = mark_julia_type(strct, true, ty, ctx);
for (size_t i = 0; i < nf; i++) {
if (jl_field_isptr(sty, i)) {
tbaa_decorate(strctinfo.tbaa, builder.CreateStore(
V_null,
builder.CreatePointerCast(
builder.CreateGEP(emit_bitcast(strct, T_pint8),
ConstantInt::get(T_size, jl_field_offset(sty, i))),
T_ppjlvalue)));
}
}
return strctinfo;
}

static jl_cgval_t emit_new_struct(jl_value_t *ty, size_t nargs, jl_value_t **args, jl_codectx_t *ctx)
{
assert(jl_is_datatype(ty));
Expand Down Expand Up @@ -2325,19 +2350,7 @@ static jl_cgval_t emit_new_struct(jl_value_t *ty, size_t nargs, jl_value_t **arg
else
return mark_julia_slot(strct, ty, NULL, tbaa_stack);
}
Value *strct = emit_allocobj(ctx, jl_datatype_size(sty),
literal_pointer_val((jl_value_t*)ty));
jl_cgval_t strctinfo = mark_julia_type(strct, true, ty, ctx);
for (size_t i = 0; i < nf; i++) {
if (jl_field_isptr(sty, i)) {
tbaa_decorate(strctinfo.tbaa, builder.CreateStore(
V_null,
builder.CreatePointerCast(
builder.CreateGEP(emit_bitcast(strct, T_pint8),
ConstantInt::get(T_size, jl_field_offset(sty, i))),
T_ppjlvalue)));
}
}
jl_cgval_t strctinfo = emit_uninitialized_struct(ty, ctx);
bool need_wb = false;
// TODO: verify that nargs <= nf (currently handled by front-end)
for (size_t i = 1; i < nargs; i++) {
Expand Down
30 changes: 19 additions & 11 deletions src/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ static jl_cgval_t generic_cast(
if (!to || !vt)
return emit_runtime_call(f, argv, 2, ctx);
Value *from = emit_unbox(vt, v, v.typ);
Value *jltov = boxed(emit_expr(jlto, ctx), ctx);
Value *jltov = boxed(emit_expr(jlto, ctx), ctx, false);
Value *ans = generic(to, from, ctx, jltov);
return mark_julia_type(ans, false, jlto, ctx);
}
Expand All @@ -504,16 +504,24 @@ static Value *generic_trunc(Type *to, Value *x, jl_codectx_t *ctx, Value *jlto)

static Value *generic_trunc_exception(Type *to, Value *x, jl_codectx_t *ctx, Value *jlto)
{
Type *lt = julia_type_to_llvm((jl_value_t*)jl_invalidvalueerror_type);
Value *strct = UndefValue::get(lt);
strct = builder.CreateInsertElement(strct,
ConstantDataArray::getString(builder.getContext(),
StringRef("convert")),
uint64_t(0));
strct = builder.CreateInsertElement(strct, jlto, uint64_t(1));
strct = builder.CreateInsertElement(strct, x, uint64_t(2));
return strct;
//return mark_julia_type(strct, false, (jl_value_t*)jl_invalidvalueerror_type, ctx);
jl_datatype_t * const sty = jl_invalidvalueerror_type;
jl_cgval_t strct = emit_uninitialized_struct((jl_value_t*)sty, ctx);
Value *addr = builder.CreateGEP(data_pointer(strct, ctx, T_pint8),
ConstantInt::get(T_size, jl_field_offset(sty, 0)));
Constant *funcsym = ConstantDataArray::getString(builder.getContext(),
StringRef("convert"));
tbaa_decorate(strct.tbaa, builder.CreateStore(funcsym,
emit_bitcast(addr, T_ppjlvalue)));
addr = builder.CreateGEP(data_pointer(strct, ctx, T_pint8),
ConstantInt::get(T_size, jl_field_offset(sty, 1)));
tbaa_decorate(strct.tbaa, builder.CreateStore(jlto,
emit_bitcast(addr, T_ppjlvalue)));
addr = builder.CreateGEP(data_pointer(strct, ctx, T_pint8),
ConstantInt::get(T_size, jl_field_offset(sty, 2)));
tbaa_decorate(strct.tbaa, builder.CreateStore(x,
emit_bitcast(addr, T_ppjlvalue)));
mark_gc_use(strct);
return strct.V;
}

static Value *generic_trunc_uchecked(Type *to, Value *x, jl_codectx_t *ctx, Value *jlto)
Expand Down

0 comments on commit f954cbb

Please sign in to comment.