From 08d2d9257dfa4b08a5972461f8bdbc03b81301a8 Mon Sep 17 00:00:00 2001 From: Dmitry Stogov Date: Fri, 31 Jan 2020 13:27:35 +0300 Subject: [PATCH] Export zend_dump_op() and add ZEND_DUMP_NUMERIC_OPLINES flag to print oplines as "dddd" instead of "Ld+" --- ext/opcache/Optimizer/zend_dump.c | 73 +++++++++++++++++++++++-------- ext/opcache/Optimizer/zend_dump.h | 3 +- 2 files changed, 56 insertions(+), 20 deletions(-) diff --git a/ext/opcache/Optimizer/zend_dump.c b/ext/opcache/Optimizer/zend_dump.c index 515532b3b45..59288fabdda 100644 --- a/ext/opcache/Optimizer/zend_dump.c +++ b/ext/opcache/Optimizer/zend_dump.c @@ -402,23 +402,17 @@ static void zend_dump_range_constraint(const zend_op_array *op_array, const zend } } -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) +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) { const char *name = zend_get_opcode_name(opline->opcode); uint32_t flags = zend_get_opcode_flags(opline->opcode); uint32_t n = 0; - int len = 0; const zend_ssa *ssa = NULL; if (dump_flags & ZEND_DUMP_SSA) { ssa = (const zend_ssa*)data; } - if (!b) { - len = fprintf(stderr, "L%u (%u):", (uint32_t)(opline - op_array->opcodes), opline->lineno); - } - fprintf(stderr, "%*c", 12-len, ' '); - if (!ssa || !ssa->ops || ssa->ops[opline - op_array->opcodes].result_use < 0) { if (opline->result_type & (IS_CV|IS_VAR|IS_TMP_VAR)) { 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 * } else { uint32_t op1_flags = ZEND_VM_OP1_FLAGS(flags); if (ZEND_VM_OP_JMP_ADDR == (op1_flags & ZEND_VM_OP_MASK)) { - if (b) { + if (dump_flags & ZEND_DUMP_NUMERIC_OPLINES) { + fprintf(stderr, " %04u", (uint32_t)(OP_JMP_ADDR(opline, opline->op1) - op_array->opcodes)); + } else if (b) { fprintf(stderr, " BB%d", b->successors[n++]); } else { 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 * } else { fprintf(stderr, " " ZEND_LONG_FMT ":", num_key); } - if (b) { + if (dump_flags & ZEND_DUMP_NUMERIC_OPLINES) { + fprintf(stderr, " %04u,", (uint32_t)ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, Z_LVAL_P(zv))); + } else if (b) { fprintf(stderr, " BB%d,", b->successors[n++]); } else { 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 * uint32_t op2_flags = ZEND_VM_OP2_FLAGS(flags); if (ZEND_VM_OP_JMP_ADDR == (op2_flags & ZEND_VM_OP_MASK)) { if (opline->opcode != ZEND_CATCH || !(opline->extended_value & ZEND_LAST_CATCH)) { - if (b) { + if (dump_flags & ZEND_DUMP_NUMERIC_OPLINES) { + fprintf(stderr, " %04u", (uint32_t)(OP_JMP_ADDR(opline, opline->op2) - op_array->opcodes)); + } else if (b) { fprintf(stderr, " BB%d", b->successors[n++]); } else { 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 * } if (ZEND_VM_EXT_JMP_ADDR == (flags & ZEND_VM_EXT_MASK)) { - if (b) { + if (dump_flags & ZEND_DUMP_NUMERIC_OPLINES) { + fprintf(stderr, " %04u", (uint32_t)ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value)); + } else if (b) { fprintf(stderr, " BB%d", b->successors[n++]); } else { 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 * } } } +} + +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) +{ + int len = 0; + + if (dump_flags & ZEND_DUMP_NUMERIC_OPLINES) { + len = fprintf(stderr, "%04u:", (uint32_t)(opline - op_array->opcodes)); + } else if (!b) { + len = fprintf(stderr, "L%u (%u):", (uint32_t)(opline - op_array->opcodes), opline->lineno); + } + fprintf(stderr, "%*c", 12-len, ' '); + + zend_dump_op(op_array, b, opline, dump_flags, data); fprintf(stderr, "\n"); } @@ -997,7 +1013,7 @@ void zend_dump_op_array(const zend_op_array *op_array, uint32_t dump_flags, cons opline = op_array->opcodes + b->start; end = opline + b->len; while (opline < end) { - zend_dump_op(op_array, b, opline, dump_flags, data); + zend_dump_op_line(op_array, b, opline, dump_flags, data); opline++; } } @@ -1005,7 +1021,10 @@ void zend_dump_op_array(const zend_op_array *op_array, uint32_t dump_flags, cons if (op_array->last_live_range && (dump_flags & ZEND_DUMP_LIVE_RANGES)) { fprintf(stderr, "LIVE RANGES:\n"); for (i = 0; i < op_array->last_live_range; i++) { - fprintf(stderr, " %u: L%u - L%u ", + fprintf(stderr, + (dump_flags & ZEND_DUMP_NUMERIC_OPLINES) ? + " %u: %04u - %04u " : + " %u: L%u - L%u ", EX_VAR_TO_NUM(op_array->live_range[i].var & ~ZEND_LIVE_MASK), op_array->live_range[i].start, 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 const zend_op *end = opline + op_array->last; while (opline < end) { - zend_dump_op(op_array, NULL, opline, dump_flags, data); + zend_dump_op_line(op_array, NULL, opline, dump_flags, data); opline++; } if (op_array->last_live_range && (dump_flags & ZEND_DUMP_LIVE_RANGES)) { fprintf(stderr, "LIVE RANGES:\n"); for (i = 0; i < op_array->last_live_range; i++) { - fprintf(stderr, " %u: L%u - L%u ", + fprintf(stderr, + (dump_flags & ZEND_DUMP_NUMERIC_OPLINES) ? + " %u: %04u - %04u " : + " %u: L%u - L%u ", EX_VAR_TO_NUM(op_array->live_range[i].var & ~ZEND_LIVE_MASK), op_array->live_range[i].start, 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 if (op_array->last_try_catch) { fprintf(stderr, "EXCEPTION TABLE:\n"); for (i = 0; i < op_array->last_try_catch; i++) { - fprintf(stderr, " L%u", + fprintf(stderr, + (dump_flags & ZEND_DUMP_NUMERIC_OPLINES) ? + " %04u" : + " L%u", op_array->try_catch_array[i].try_op); + if (op_array->try_catch_array[i].catch_op) { - fprintf(stderr, ", L%u", + fprintf(stderr, + (dump_flags & ZEND_DUMP_NUMERIC_OPLINES) ? + ", %04u" : + ", L%u", op_array->try_catch_array[i].catch_op); } else { fprintf(stderr, ", -"); } if (op_array->try_catch_array[i].finally_op) { - fprintf(stderr, ", L%u", + fprintf(stderr, + (dump_flags & ZEND_DUMP_NUMERIC_OPLINES) ? + ", %04u" : + ", L%u", op_array->try_catch_array[i].finally_op); } else { fprintf(stderr, ", -"); } if (op_array->try_catch_array[i].finally_end) { - fprintf(stderr, ", L%u\n", + fprintf(stderr, + (dump_flags & ZEND_DUMP_NUMERIC_OPLINES) ? + ", %04u" : + ", L%u\n", op_array->try_catch_array[i].finally_end); } else { fprintf(stderr, ", -\n"); diff --git a/ext/opcache/Optimizer/zend_dump.h b/ext/opcache/Optimizer/zend_dump.h index 820f3daf193..5f1968fd238 100644 --- a/ext/opcache/Optimizer/zend_dump.h +++ b/ext/opcache/Optimizer/zend_dump.h @@ -27,11 +27,12 @@ #define ZEND_DUMP_CFG (1<<2) #define ZEND_DUMP_SSA (1<<3) #define ZEND_DUMP_LIVE_RANGES (1<<4) -#define ZEND_DUMP_RT_CONSTANTS ZEND_RT_CONSTANTS +#define ZEND_DUMP_NUMERIC_OPLINES (1<<5) BEGIN_EXTERN_C() void zend_dump_op_array(const zend_op_array *op_array, uint32_t dump_flags, const char *msg, const void *data); +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); void zend_dump_dominators(const zend_op_array *op_array, const zend_cfg *cfg); void zend_dump_dfg(const zend_op_array *op_array, const zend_cfg *cfg, const zend_dfg *dfg); void zend_dump_phi_placement(const zend_op_array *op_array, const zend_ssa *ssa);