Skip to content

Commit 3b86ffe

Browse files
committed
add codegen
1 parent 01de623 commit 3b86ffe

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
@@ -4672,6 +4672,45 @@ static jl_cgval_t _emit_memoryref(jl_codectx_t &ctx, const jl_cgval_t &mem, cons
46724672
return _emit_memoryref(ctx, boxed(ctx, mem), data, layout, typ);
46734673
}
46744674

4675+
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)
4676+
{
4677+
bool isboxed = layout->flags.arrayelem_isboxed;
4678+
bool isunion = layout->flags.arrayelem_isunion;
4679+
bool isghost = layout->size == 0;
4680+
Value *boxmem = boxed(ctx, mem);
4681+
Value *i = emit_unbox(ctx, ctx.types().T_size, idx, (jl_value_t*)jl_long_type);
4682+
Value *idx0 = ctx.builder.CreateSub(i, ConstantInt::get(ctx.types().T_size, 1));
4683+
bool bc = bounds_check_enabled(ctx, inbounds);
4684+
if (bc) {
4685+
BasicBlock *failBB, *endBB;
4686+
failBB = BasicBlock::Create(ctx.builder.getContext(), "oob");
4687+
endBB = BasicBlock::Create(ctx.builder.getContext(), "idxend");
4688+
Value *mlen = emit_genericmemorylen(ctx, boxmem, typ);
4689+
Value *inbound = ctx.builder.CreateICmpULT(idx0, mlen);
4690+
setName(ctx.emission_context, inbound, "memoryref_isinbounds");
4691+
ctx.builder.CreateCondBr(inbound, endBB, failBB);
4692+
failBB->insertInto(ctx.f);
4693+
ctx.builder.SetInsertPoint(failBB);
4694+
ctx.builder.CreateCall(prepare_call(jlboundserror_func),
4695+
{ mark_callee_rooted(ctx, boxmem), i });
4696+
ctx.builder.CreateUnreachable();
4697+
endBB->insertInto(ctx.f);
4698+
ctx.builder.SetInsertPoint(endBB);
4699+
}
4700+
Value *data;
4701+
4702+
if ((!isboxed && isunion) || isghost) {
4703+
data = idx0;
4704+
4705+
} else {
4706+
data = emit_genericmemoryptr(ctx, boxmem, layout, 0);
4707+
Type *elty = isboxed ? ctx.types().T_prjlvalue : julia_type_to_llvm(ctx, jl_tparam1(typ));
4708+
data = ctx.builder.CreateInBoundsGEP(elty, data, idx0);
4709+
}
4710+
4711+
return _emit_memoryref(ctx, boxmem, data, layout, typ);
4712+
}
4713+
46754714
static Value *emit_memoryref_FCA(jl_codectx_t &ctx, const jl_cgval_t &ref, const jl_datatype_layout_t *layout)
46764715
{
46774716
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)