Skip to content

Commit

Permalink
Box lowering for P6[int|num|str]
Browse files Browse the repository at this point in the history
This is aimed at helping NQP code rather than Perl 6 code, but does
the same trick that we've done for the Perl 6 ones. Alas, the int one
loses the use of the int box cache, so needs a bit more work yet.
  • Loading branch information
jnthn committed Aug 16, 2018
1 parent 8c87236 commit 41132cc
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
31 changes: 30 additions & 1 deletion src/6model/reprs/P6int.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,35 @@ static void serialize(MVMThreadContext *tc, MVMSTable *st, void *data, MVMSerial
MVM_serialization_write_int(tc, writer, get_int(tc, st, NULL, data));
}

static void spesh(MVMThreadContext *tc, MVMSTable *st, MVMSpeshGraph *g, MVMSpeshBB *bb, MVMSpeshIns *ins) {
MVMP6intREPRData *repr_data = (MVMP6intREPRData *)st->REPR_data;
switch (ins->info->opcode) {
case MVM_OP_box_i: {
if (repr_data->bits == 64 && !(st->mode_flags & MVM_FINALIZE_TYPE)) {
/* Prepend a fastcreate instruction. */
MVMSpeshIns *fastcreate = MVM_spesh_alloc(tc, g, sizeof(MVMSpeshIns));
MVMSpeshFacts *tgt_facts = MVM_spesh_get_facts(tc, g, ins->operands[0]);
fastcreate->info = MVM_op_get_op(MVM_OP_sp_fastcreate);
fastcreate->operands = MVM_spesh_alloc(tc, g, 3 * sizeof(MVMSpeshOperand));
fastcreate->operands[0] = ins->operands[0];
tgt_facts->writer = fastcreate;
fastcreate->operands[1].lit_i16 = st->size;
fastcreate->operands[2].lit_i16 = MVM_spesh_add_spesh_slot(tc, g, (MVMCollectable *)st);
MVM_spesh_manipulate_insert_ins(tc, bb, ins->prev, fastcreate);
tgt_facts->flags |= MVM_SPESH_FACT_KNOWN_TYPE | MVM_SPESH_FACT_CONCRETE;
tgt_facts->type = st->WHAT;

/* Change instruction to a bind. */
MVM_spesh_usages_delete_by_reg(tc, g, ins->operands[2], ins);
ins->info = MVM_op_get_op(MVM_OP_sp_bind_i64);
ins->operands[2] = ins->operands[1];
ins->operands[1].lit_i16 = offsetof(MVMP6int, body.value);
MVM_spesh_usages_add_by_reg(tc, g, ins->operands[0], ins);
}
break;
}
}
}
/* Initializes the representation. */
const MVMREPROps * MVMP6int_initialize(MVMThreadContext *tc) {
return &P6int_this_repr;
Expand Down Expand Up @@ -237,7 +266,7 @@ static const MVMREPROps P6int_this_repr = {
NULL, /* gc_mark_repr_data */
gc_free_repr_data,
compose,
NULL, /* spesh */
spesh,
"P6int", /* name */
MVM_REPR_ID_P6int,
NULL, /* unmanaged_size */
Expand Down
32 changes: 31 additions & 1 deletion src/6model/reprs/P6num.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,36 @@ static void serialize(MVMThreadContext *tc, MVMSTable *st, void *data, MVMSerial
MVM_serialization_write_num(tc, writer, get_num(tc, st, NULL, data));
}

static void spesh(MVMThreadContext *tc, MVMSTable *st, MVMSpeshGraph *g, MVMSpeshBB *bb, MVMSpeshIns *ins) {
MVMP6numREPRData *repr_data = (MVMP6numREPRData *)st->REPR_data;
switch (ins->info->opcode) {
case MVM_OP_box_n: {
if (repr_data->bits == 64 && !(st->mode_flags & MVM_FINALIZE_TYPE)) {
/* Prepend a fastcreate instruction. */
MVMSpeshIns *fastcreate = MVM_spesh_alloc(tc, g, sizeof(MVMSpeshIns));
MVMSpeshFacts *tgt_facts = MVM_spesh_get_facts(tc, g, ins->operands[0]);
fastcreate->info = MVM_op_get_op(MVM_OP_sp_fastcreate);
fastcreate->operands = MVM_spesh_alloc(tc, g, 3 * sizeof(MVMSpeshOperand));
fastcreate->operands[0] = ins->operands[0];
tgt_facts->writer = fastcreate;
fastcreate->operands[1].lit_i16 = st->size;
fastcreate->operands[2].lit_i16 = MVM_spesh_add_spesh_slot(tc, g, (MVMCollectable *)st);
MVM_spesh_manipulate_insert_ins(tc, bb, ins->prev, fastcreate);
tgt_facts->flags |= MVM_SPESH_FACT_KNOWN_TYPE | MVM_SPESH_FACT_CONCRETE;
tgt_facts->type = st->WHAT;

/* Change instruction to a bind. */
MVM_spesh_usages_delete_by_reg(tc, g, ins->operands[2], ins);
ins->info = MVM_op_get_op(MVM_OP_sp_bind_n);
ins->operands[2] = ins->operands[1];
ins->operands[1].lit_i16 = offsetof(MVMP6num, body.value);
MVM_spesh_usages_add_by_reg(tc, g, ins->operands[0], ins);
}
break;
}
}
}

/* Initializes the representation. */
const MVMREPROps * MVMP6num_initialize(MVMThreadContext *tc) {
return &P6num_this_repr;
Expand Down Expand Up @@ -184,7 +214,7 @@ static const MVMREPROps P6num_this_repr = {
NULL, /* gc_mark_repr_data */
gc_free_repr_data,
compose,
NULL, /* spesh */
spesh,
"P6num", /* name */
MVM_REPR_ID_P6num,
NULL, /* unmanaged_size */
Expand Down
31 changes: 30 additions & 1 deletion src/6model/reprs/P6str.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,35 @@ static void serialize(MVMThreadContext *tc, MVMSTable *st, void *data, MVMSerial
MVM_serialization_write_str(tc, writer, ((MVMP6strBody *)data)->value);
}

static void spesh(MVMThreadContext *tc, MVMSTable *st, MVMSpeshGraph *g, MVMSpeshBB *bb, MVMSpeshIns *ins) {
switch (ins->info->opcode) {
case MVM_OP_box_s: {
if (!(st->mode_flags & MVM_FINALIZE_TYPE)) {
/* Prepend a fastcreate instruction. */
MVMSpeshIns *fastcreate = MVM_spesh_alloc(tc, g, sizeof(MVMSpeshIns));
MVMSpeshFacts *tgt_facts = MVM_spesh_get_facts(tc, g, ins->operands[0]);
fastcreate->info = MVM_op_get_op(MVM_OP_sp_fastcreate);
fastcreate->operands = MVM_spesh_alloc(tc, g, 3 * sizeof(MVMSpeshOperand));
fastcreate->operands[0] = ins->operands[0];
tgt_facts->writer = fastcreate;
fastcreate->operands[1].lit_i16 = st->size;
fastcreate->operands[2].lit_i16 = MVM_spesh_add_spesh_slot(tc, g, (MVMCollectable *)st);
MVM_spesh_manipulate_insert_ins(tc, bb, ins->prev, fastcreate);
tgt_facts->flags |= MVM_SPESH_FACT_KNOWN_TYPE | MVM_SPESH_FACT_CONCRETE;
tgt_facts->type = st->WHAT;

/* Change instruction to a bind. */
MVM_spesh_usages_delete_by_reg(tc, g, ins->operands[2], ins);
ins->info = MVM_op_get_op(MVM_OP_sp_bind_s);
ins->operands[2] = ins->operands[1];
ins->operands[1].lit_i16 = offsetof(MVMP6str, body.value);
MVM_spesh_usages_add_by_reg(tc, g, ins->operands[0], ins);
}
break;
}
}
}

/* Initializes the representation. */
const MVMREPROps * MVMP6str_initialize(MVMThreadContext *tc) {
return &P6str_this_repr;
Expand Down Expand Up @@ -107,7 +136,7 @@ static const MVMREPROps P6str_this_repr = {
NULL, /* gc_mark_repr_data */
NULL, /* gc_free_repr_data */
compose,
NULL, /* spesh */
spesh,
"P6str", /* name */
MVM_REPR_ID_P6str,
NULL, /* unmanaged_size */
Expand Down

0 comments on commit 41132cc

Please sign in to comment.