Skip to content

Commit 57450e5

Browse files
TimWollaarnaud-lb
andauthored
zend_vm_gen: Track line numbers on a per-file basis (#19799)
#19789 fixed the line number references for `zend_vm_def.h`, when generating with line number information some of them are also specific to `zend_vm_execute.h` and thus should reference that file instead with the correct line numbers. The line number tracking was broken, because it was tracked in a single global variable, instead of being tracked on a per-file basis. Fix this by making the line numbers an array indexed by the resource ID and consistently using the `out()` functions to write into the files. Co-authored-by: Arnaud Le Blanc <[email protected]>
1 parent 62eaa71 commit 57450e5

File tree

1 file changed

+45
-41
lines changed

1 file changed

+45
-41
lines changed

Zend/zend_vm_gen.php

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -545,24 +545,28 @@
545545
$helpers = array(); // opcode helpers by name
546546
$params = array(); // parameters of helpers
547547
$opnames = array(); // opcode name to code mapping
548-
$line_no = 1;
548+
$line_nos = [];
549549

550550
$used_extra_spec = array();
551551

552552
// Writes $s into resulting executor
553553
function out($f, $s) {
554-
global $line_no;
554+
global $line_nos;
555555

556556
fputs($f,$s);
557-
$line_no += substr_count($s, "\n");
557+
558+
$line_nos[(int)$f] ??= 1;
559+
$line_nos[(int)$f] += substr_count($s, "\n");
558560
}
559561

560562
// Resets #line directives in resulting executor
561563
function out_line($f) {
562-
global $line_no, $executor_file;
564+
global $line_nos, $executor_file;
565+
566+
$line_nos[(int)$f] ??= 1;
567+
$line_nos[(int)$f]++;
563568

564-
fputs($f,"#line ".($line_no+1)." \"".$executor_file."\"\n");
565-
++$line_no;
569+
fputs($f,"#line ".$line_nos[(int)$f]." \"".$executor_file."\"\n");
566570
}
567571

568572
function is_hot_helper($name) {
@@ -2839,46 +2843,46 @@ function gen_vm($def, $skel) {
28392843

28402844
// Insert header
28412845
out($f, HEADER_TEXT);
2842-
fputs($f,"#include <stdio.h>\n");
2843-
fputs($f,"#include <zend.h>\n");
2844-
fputs($f,"#include <zend_vm_opcodes.h>\n\n");
2846+
out($f,"#include <stdio.h>\n");
2847+
out($f,"#include <zend.h>\n");
2848+
out($f,"#include <zend_vm_opcodes.h>\n\n");
28452849

2846-
fputs($f,"static const char *zend_vm_opcodes_names[".($max_opcode + 1)."] = {\n");
2850+
out($f,"static const char *zend_vm_opcodes_names[".($max_opcode + 1)."] = {\n");
28472851
for ($i = 0; $i <= $max_opcode; $i++) {
2848-
fputs($f,"\t".(isset($opcodes[$i]["op"])?'"'.$opcodes[$i]["op"].'"':"NULL").",\n");
2852+
out($f,"\t".(isset($opcodes[$i]["op"])?'"'.$opcodes[$i]["op"].'"':"NULL").",\n");
28492853
}
2850-
fputs($f, "};\n\n");
2854+
out($f, "};\n\n");
28512855

2852-
fputs($f,"static uint32_t zend_vm_opcodes_flags[".($max_opcode + 1)."] = {\n");
2856+
out($f,"static uint32_t zend_vm_opcodes_flags[".($max_opcode + 1)."] = {\n");
28532857
for ($i = 0; $i <= $max_opcode; $i++) {
2854-
fprintf($f, "\t0x%08x,\n", isset($opcodes[$i]["flags"]) ? $opcodes[$i]["flags"] : 0);
2855-
}
2856-
fputs($f, "};\n\n");
2857-
2858-
fputs($f, "ZEND_API const char* ZEND_FASTCALL zend_get_opcode_name(uint8_t opcode) {\n");
2859-
fputs($f, "\tif (UNEXPECTED(opcode > ZEND_VM_LAST_OPCODE)) {\n");
2860-
fputs($f, "\t\treturn NULL;\n");
2861-
fputs($f, "\t}\n");
2862-
fputs($f, "\treturn zend_vm_opcodes_names[opcode];\n");
2863-
fputs($f, "}\n");
2864-
2865-
fputs($f, "ZEND_API uint32_t ZEND_FASTCALL zend_get_opcode_flags(uint8_t opcode) {\n");
2866-
fputs($f, "\tif (UNEXPECTED(opcode > ZEND_VM_LAST_OPCODE)) {\n");
2867-
fputs($f, "\t\topcode = ZEND_NOP;\n");
2868-
fputs($f, "\t}\n");
2869-
fputs($f, "\treturn zend_vm_opcodes_flags[opcode];\n");
2870-
fputs($f, "}\n");
2871-
2872-
fputs($f, "ZEND_API uint8_t zend_get_opcode_id(const char *name, size_t length) {\n");
2873-
fputs($f, "\tuint8_t opcode;\n");
2874-
fputs($f, "\tfor (opcode = 0; opcode < (sizeof(zend_vm_opcodes_names) / sizeof(zend_vm_opcodes_names[0])) - 1; opcode++) {\n");
2875-
fputs($f, "\t\tconst char *opcode_name = zend_vm_opcodes_names[opcode];\n");
2876-
fputs($f, "\t\tif (opcode_name && strncmp(opcode_name, name, length) == 0) {\n");
2877-
fputs($f, "\t\t\treturn opcode;\n");
2878-
fputs($f, "\t\t}\n");
2879-
fputs($f, "\t}\n");
2880-
fputs($f, "\treturn ZEND_VM_LAST_OPCODE + 1;\n");
2881-
fputs($f, "}\n");
2858+
out($f, sprintf("\t0x%08x,\n", isset($opcodes[$i]["flags"]) ? $opcodes[$i]["flags"] : 0));
2859+
}
2860+
out($f, "};\n\n");
2861+
2862+
out($f, "ZEND_API const char* ZEND_FASTCALL zend_get_opcode_name(uint8_t opcode) {\n");
2863+
out($f, "\tif (UNEXPECTED(opcode > ZEND_VM_LAST_OPCODE)) {\n");
2864+
out($f, "\t\treturn NULL;\n");
2865+
out($f, "\t}\n");
2866+
out($f, "\treturn zend_vm_opcodes_names[opcode];\n");
2867+
out($f, "}\n");
2868+
2869+
out($f, "ZEND_API uint32_t ZEND_FASTCALL zend_get_opcode_flags(uint8_t opcode) {\n");
2870+
out($f, "\tif (UNEXPECTED(opcode > ZEND_VM_LAST_OPCODE)) {\n");
2871+
out($f, "\t\topcode = ZEND_NOP;\n");
2872+
out($f, "\t}\n");
2873+
out($f, "\treturn zend_vm_opcodes_flags[opcode];\n");
2874+
out($f, "}\n");
2875+
2876+
out($f, "ZEND_API uint8_t zend_get_opcode_id(const char *name, size_t length) {\n");
2877+
out($f, "\tuint8_t opcode;\n");
2878+
out($f, "\tfor (opcode = 0; opcode < (sizeof(zend_vm_opcodes_names) / sizeof(zend_vm_opcodes_names[0])) - 1; opcode++) {\n");
2879+
out($f, "\t\tconst char *opcode_name = zend_vm_opcodes_names[opcode];\n");
2880+
out($f, "\t\tif (opcode_name && strncmp(opcode_name, name, length) == 0) {\n");
2881+
out($f, "\t\t\treturn opcode;\n");
2882+
out($f, "\t\t}\n");
2883+
out($f, "\t}\n");
2884+
out($f, "\treturn ZEND_VM_LAST_OPCODE + 1;\n");
2885+
out($f, "}\n");
28822886

28832887
fclose($f);
28842888
echo "zend_vm_opcodes.c generated successfully.\n";

0 commit comments

Comments
 (0)