Skip to content

Commit 08d2d92

Browse files
committed
Export zend_dump_op() and add ZEND_DUMP_NUMERIC_OPLINES flag to print oplines as "dddd" instead of "Ld+"
1 parent d8a1e3d commit 08d2d92

File tree

2 files changed

+56
-20
lines changed

2 files changed

+56
-20
lines changed

ext/opcache/Optimizer/zend_dump.c

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -402,23 +402,17 @@ static void zend_dump_range_constraint(const zend_op_array *op_array, const zend
402402
}
403403
}
404404

405-
static void zend_dump_op(const zend_op_array *op_array, const zend_basic_block *b, const zend_op *opline, uint32_t dump_flags, const void *data)
405+
void zend_dump_op(const zend_op_array *op_array, const zend_basic_block *b, const zend_op *opline, uint32_t dump_flags, const void *data)
406406
{
407407
const char *name = zend_get_opcode_name(opline->opcode);
408408
uint32_t flags = zend_get_opcode_flags(opline->opcode);
409409
uint32_t n = 0;
410-
int len = 0;
411410
const zend_ssa *ssa = NULL;
412411

413412
if (dump_flags & ZEND_DUMP_SSA) {
414413
ssa = (const zend_ssa*)data;
415414
}
416415

417-
if (!b) {
418-
len = fprintf(stderr, "L%u (%u):", (uint32_t)(opline - op_array->opcodes), opline->lineno);
419-
}
420-
fprintf(stderr, "%*c", 12-len, ' ');
421-
422416
if (!ssa || !ssa->ops || ssa->ops[opline - op_array->opcodes].result_use < 0) {
423417
if (opline->result_type & (IS_CV|IS_VAR|IS_TMP_VAR)) {
424418
if (ssa && ssa->ops && ssa->ops[opline - op_array->opcodes].result_def >= 0) {
@@ -611,7 +605,9 @@ static void zend_dump_op(const zend_op_array *op_array, const zend_basic_block *
611605
} else {
612606
uint32_t op1_flags = ZEND_VM_OP1_FLAGS(flags);
613607
if (ZEND_VM_OP_JMP_ADDR == (op1_flags & ZEND_VM_OP_MASK)) {
614-
if (b) {
608+
if (dump_flags & ZEND_DUMP_NUMERIC_OPLINES) {
609+
fprintf(stderr, " %04u", (uint32_t)(OP_JMP_ADDR(opline, opline->op1) - op_array->opcodes));
610+
} else if (b) {
615611
fprintf(stderr, " BB%d", b->successors[n++]);
616612
} else {
617613
fprintf(stderr, " L%u", (uint32_t)(OP_JMP_ADDR(opline, opline->op1) - op_array->opcodes));
@@ -634,7 +630,9 @@ static void zend_dump_op(const zend_op_array *op_array, const zend_basic_block *
634630
} else {
635631
fprintf(stderr, " " ZEND_LONG_FMT ":", num_key);
636632
}
637-
if (b) {
633+
if (dump_flags & ZEND_DUMP_NUMERIC_OPLINES) {
634+
fprintf(stderr, " %04u,", (uint32_t)ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, Z_LVAL_P(zv)));
635+
} else if (b) {
638636
fprintf(stderr, " BB%d,", b->successors[n++]);
639637
} else {
640638
fprintf(stderr, " L%u,", (uint32_t)ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, Z_LVAL_P(zv)));
@@ -669,7 +667,9 @@ static void zend_dump_op(const zend_op_array *op_array, const zend_basic_block *
669667
uint32_t op2_flags = ZEND_VM_OP2_FLAGS(flags);
670668
if (ZEND_VM_OP_JMP_ADDR == (op2_flags & ZEND_VM_OP_MASK)) {
671669
if (opline->opcode != ZEND_CATCH || !(opline->extended_value & ZEND_LAST_CATCH)) {
672-
if (b) {
670+
if (dump_flags & ZEND_DUMP_NUMERIC_OPLINES) {
671+
fprintf(stderr, " %04u", (uint32_t)(OP_JMP_ADDR(opline, opline->op2) - op_array->opcodes));
672+
} else if (b) {
673673
fprintf(stderr, " BB%d", b->successors[n++]);
674674
} else {
675675
fprintf(stderr, " L%u", (uint32_t)(OP_JMP_ADDR(opline, opline->op2) - op_array->opcodes));
@@ -681,7 +681,9 @@ static void zend_dump_op(const zend_op_array *op_array, const zend_basic_block *
681681
}
682682

683683
if (ZEND_VM_EXT_JMP_ADDR == (flags & ZEND_VM_EXT_MASK)) {
684-
if (b) {
684+
if (dump_flags & ZEND_DUMP_NUMERIC_OPLINES) {
685+
fprintf(stderr, " %04u", (uint32_t)ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value));
686+
} else if (b) {
685687
fprintf(stderr, " BB%d", b->successors[n++]);
686688
} else {
687689
fprintf(stderr, " L%u", (uint32_t)ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value));
@@ -716,6 +718,20 @@ static void zend_dump_op(const zend_op_array *op_array, const zend_basic_block *
716718
}
717719
}
718720
}
721+
}
722+
723+
static void zend_dump_op_line(const zend_op_array *op_array, const zend_basic_block *b, const zend_op *opline, uint32_t dump_flags, const void *data)
724+
{
725+
int len = 0;
726+
727+
if (dump_flags & ZEND_DUMP_NUMERIC_OPLINES) {
728+
len = fprintf(stderr, "%04u:", (uint32_t)(opline - op_array->opcodes));
729+
} else if (!b) {
730+
len = fprintf(stderr, "L%u (%u):", (uint32_t)(opline - op_array->opcodes), opline->lineno);
731+
}
732+
fprintf(stderr, "%*c", 12-len, ' ');
733+
734+
zend_dump_op(op_array, b, opline, dump_flags, data);
719735
fprintf(stderr, "\n");
720736
}
721737

@@ -997,15 +1013,18 @@ void zend_dump_op_array(const zend_op_array *op_array, uint32_t dump_flags, cons
9971013
opline = op_array->opcodes + b->start;
9981014
end = opline + b->len;
9991015
while (opline < end) {
1000-
zend_dump_op(op_array, b, opline, dump_flags, data);
1016+
zend_dump_op_line(op_array, b, opline, dump_flags, data);
10011017
opline++;
10021018
}
10031019
}
10041020
}
10051021
if (op_array->last_live_range && (dump_flags & ZEND_DUMP_LIVE_RANGES)) {
10061022
fprintf(stderr, "LIVE RANGES:\n");
10071023
for (i = 0; i < op_array->last_live_range; i++) {
1008-
fprintf(stderr, " %u: L%u - L%u ",
1024+
fprintf(stderr,
1025+
(dump_flags & ZEND_DUMP_NUMERIC_OPLINES) ?
1026+
" %u: %04u - %04u " :
1027+
" %u: L%u - L%u ",
10091028
EX_VAR_TO_NUM(op_array->live_range[i].var & ~ZEND_LIVE_MASK),
10101029
op_array->live_range[i].start,
10111030
op_array->live_range[i].end);
@@ -1058,13 +1077,16 @@ void zend_dump_op_array(const zend_op_array *op_array, uint32_t dump_flags, cons
10581077
const zend_op *end = opline + op_array->last;
10591078

10601079
while (opline < end) {
1061-
zend_dump_op(op_array, NULL, opline, dump_flags, data);
1080+
zend_dump_op_line(op_array, NULL, opline, dump_flags, data);
10621081
opline++;
10631082
}
10641083
if (op_array->last_live_range && (dump_flags & ZEND_DUMP_LIVE_RANGES)) {
10651084
fprintf(stderr, "LIVE RANGES:\n");
10661085
for (i = 0; i < op_array->last_live_range; i++) {
1067-
fprintf(stderr, " %u: L%u - L%u ",
1086+
fprintf(stderr,
1087+
(dump_flags & ZEND_DUMP_NUMERIC_OPLINES) ?
1088+
" %u: %04u - %04u " :
1089+
" %u: L%u - L%u ",
10681090
EX_VAR_TO_NUM(op_array->live_range[i].var & ~ZEND_LIVE_MASK),
10691091
op_array->live_range[i].start,
10701092
op_array->live_range[i].end);
@@ -1090,22 +1112,35 @@ void zend_dump_op_array(const zend_op_array *op_array, uint32_t dump_flags, cons
10901112
if (op_array->last_try_catch) {
10911113
fprintf(stderr, "EXCEPTION TABLE:\n");
10921114
for (i = 0; i < op_array->last_try_catch; i++) {
1093-
fprintf(stderr, " L%u",
1115+
fprintf(stderr,
1116+
(dump_flags & ZEND_DUMP_NUMERIC_OPLINES) ?
1117+
" %04u" :
1118+
" L%u",
10941119
op_array->try_catch_array[i].try_op);
1120+
10951121
if (op_array->try_catch_array[i].catch_op) {
1096-
fprintf(stderr, ", L%u",
1122+
fprintf(stderr,
1123+
(dump_flags & ZEND_DUMP_NUMERIC_OPLINES) ?
1124+
", %04u" :
1125+
", L%u",
10971126
op_array->try_catch_array[i].catch_op);
10981127
} else {
10991128
fprintf(stderr, ", -");
11001129
}
11011130
if (op_array->try_catch_array[i].finally_op) {
1102-
fprintf(stderr, ", L%u",
1131+
fprintf(stderr,
1132+
(dump_flags & ZEND_DUMP_NUMERIC_OPLINES) ?
1133+
", %04u" :
1134+
", L%u",
11031135
op_array->try_catch_array[i].finally_op);
11041136
} else {
11051137
fprintf(stderr, ", -");
11061138
}
11071139
if (op_array->try_catch_array[i].finally_end) {
1108-
fprintf(stderr, ", L%u\n",
1140+
fprintf(stderr,
1141+
(dump_flags & ZEND_DUMP_NUMERIC_OPLINES) ?
1142+
", %04u" :
1143+
", L%u\n",
11091144
op_array->try_catch_array[i].finally_end);
11101145
} else {
11111146
fprintf(stderr, ", -\n");

ext/opcache/Optimizer/zend_dump.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@
2727
#define ZEND_DUMP_CFG (1<<2)
2828
#define ZEND_DUMP_SSA (1<<3)
2929
#define ZEND_DUMP_LIVE_RANGES (1<<4)
30-
#define ZEND_DUMP_RT_CONSTANTS ZEND_RT_CONSTANTS
30+
#define ZEND_DUMP_NUMERIC_OPLINES (1<<5)
3131

3232
BEGIN_EXTERN_C()
3333

3434
void zend_dump_op_array(const zend_op_array *op_array, uint32_t dump_flags, const char *msg, const void *data);
35+
void zend_dump_op(const zend_op_array *op_array, const zend_basic_block *b, const zend_op *opline, uint32_t dump_flags, const void *data);
3536
void zend_dump_dominators(const zend_op_array *op_array, const zend_cfg *cfg);
3637
void zend_dump_dfg(const zend_op_array *op_array, const zend_cfg *cfg, const zend_dfg *dfg);
3738
void zend_dump_phi_placement(const zend_op_array *op_array, const zend_ssa *ssa);

0 commit comments

Comments
 (0)