Skip to content

Commit 973057c

Browse files
committed
add codegen
1 parent becfc1d commit 973057c

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/cgutils.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4684,6 +4684,45 @@ static jl_cgval_t _emit_memoryref(jl_codectx_t &ctx, const jl_cgval_t &mem, cons
46844684
return _emit_memoryref(ctx, boxed(ctx, mem), data, layout, typ);
46854685
}
46864686

4687+
static jl_cgval_t emit_memoryref_direct(jl_codectx_t &ctx, const jl_cgval_t &mem, jl_cgval_t idx, jl_value_t *typ, jl_value_t *inbounds, const jl_datatype_layout_t *layout)
4688+
{
4689+
bool isboxed = layout->flags.arrayelem_isboxed;
4690+
bool isunion = layout->flags.arrayelem_isunion;
4691+
bool isghost = layout->size == 0;
4692+
Value *boxmem = boxed(ctx, mem);
4693+
Value *i = emit_unbox(ctx, ctx.types().T_size, idx, (jl_value_t*)jl_long_type);
4694+
Value *idx0 = ctx.builder.CreateSub(i, ConstantInt::get(ctx.types().T_size, 1));
4695+
bool bc = bounds_check_enabled(ctx, inbounds);
4696+
if (bc) {
4697+
BasicBlock *failBB, *endBB;
4698+
failBB = BasicBlock::Create(ctx.builder.getContext(), "oob");
4699+
endBB = BasicBlock::Create(ctx.builder.getContext(), "idxend");
4700+
Value *mlen = emit_genericmemorylen(ctx, boxmem, typ);
4701+
Value *inbound = ctx.builder.CreateICmpULT(idx0, mlen);
4702+
setName(ctx.emission_context, inbound, "memoryref_isinbounds");
4703+
ctx.builder.CreateCondBr(inbound, endBB, failBB);
4704+
failBB->insertInto(ctx.f);
4705+
ctx.builder.SetInsertPoint(failBB);
4706+
ctx.builder.CreateCall(prepare_call(jlboundserror_func),
4707+
{ mark_callee_rooted(ctx, boxmem), i });
4708+
ctx.builder.CreateUnreachable();
4709+
endBB->insertInto(ctx.f);
4710+
ctx.builder.SetInsertPoint(endBB);
4711+
}
4712+
Value *data;
4713+
4714+
if ((!isboxed && isunion) || isghost) {
4715+
data = idx0;
4716+
4717+
} else {
4718+
data = emit_genericmemoryptr(ctx, boxmem, layout, 0);
4719+
Type *elty = isboxed ? ctx.types().T_prjlvalue : julia_type_to_llvm(ctx, jl_tparam1(typ));
4720+
data = ctx.builder.CreateInBoundsGEP(elty, data, idx0);
4721+
}
4722+
4723+
return _emit_memoryref(ctx, boxmem, data, layout, typ);
4724+
}
4725+
46874726
static Value *emit_memoryref_FCA(jl_codectx_t &ctx, const jl_cgval_t &ref, const jl_datatype_layout_t *layout)
46884727
{
46894728
if (!ref.inline_roots.empty()) {

src/codegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4154,7 +4154,7 @@ static bool emit_builtin_call(jl_codectx_t &ctx, jl_cgval_t *ret, jl_value_t *f,
41544154
if (nargs == 3)
41554155
emit_typecheck(ctx, argv[3], (jl_value_t*)jl_bool_type, "memoryrefnew");
41564156
jl_value_t *typ = jl_apply_type((jl_value_t*)jl_genericmemoryref_type, jl_svec_data(mty_dt->parameters), jl_svec_len(mty_dt->parameters));
4157-
*ret = emit_memoryref(ctx, _emit_memoryref(ctx, ref, layout, typ), argv[2], boundscheck, layout);
4157+
*ret = emit_memoryref_direct(ctx, ref, argv[2], typ, boundscheck, layout);
41584158
return true;
41594159
}
41604160
}

0 commit comments

Comments
 (0)