zend_vm_gen.php 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. <?php
  2. /*
  3. +----------------------------------------------------------------------+
  4. | Zend Engine |
  5. +----------------------------------------------------------------------+
  6. | Copyright (c) 1998-2016 Zend Technologies Ltd. (http://www.zend.com) |
  7. +----------------------------------------------------------------------+
  8. | This source file is subject to version 2.00 of the Zend license, |
  9. | that is bundled with this package in the file LICENSE, and is |
  10. | available through the world-wide-web at the following url: |
  11. | http://www.zend.com/license/2_00.txt. |
  12. | If you did not receive a copy of the Zend license and are unable to |
  13. | obtain it through the world-wide-web, please send a note to |
  14. | license@zend.com so we can mail you a copy immediately. |
  15. +----------------------------------------------------------------------+
  16. | Authors: Dmitry Stogov <dmitry@zend.com> |
  17. +----------------------------------------------------------------------+
  18. $Id$
  19. */
  20. $header_text = <<< DATA
  21. /*
  22. +----------------------------------------------------------------------+
  23. | Zend Engine |
  24. +----------------------------------------------------------------------+
  25. | Copyright (c) 1998-2016 Zend Technologies Ltd. (http://www.zend.com) |
  26. +----------------------------------------------------------------------+
  27. | This source file is subject to version 2.00 of the Zend license, |
  28. | that is bundled with this package in the file LICENSE, and is |
  29. | available through the world-wide-web at the following url: |
  30. | http://www.zend.com/license/2_00.txt. |
  31. | If you did not receive a copy of the Zend license and are unable to |
  32. | obtain it through the world-wide-web, please send a note to |
  33. | license@zend.com so we can mail you a copy immediately. |
  34. +----------------------------------------------------------------------+
  35. | Authors: Andi Gutmans <andi@zend.com> |
  36. | Zeev Suraski <zeev@zend.com> |
  37. | Dmitry Stogov <dmitry@zend.com> |
  38. +----------------------------------------------------------------------+
  39. */
  40. DATA;
  41. /*
  42. This script creates zend_vm_execute.h and zend_vm_opcodes.h
  43. from existing zend_vm_def.h and zend_vm_execute.skl
  44. */
  45. error_reporting(E_ALL);
  46. define("ZEND_VM_KIND_CALL", 1);
  47. define("ZEND_VM_KIND_SWITCH", 2);
  48. define("ZEND_VM_KIND_GOTO", 3);
  49. $op_types = array(
  50. "ANY",
  51. "CONST",
  52. "TMP",
  53. "VAR",
  54. "UNUSED",
  55. "CV"
  56. );
  57. $prefix = array(
  58. "ANY" => "",
  59. "TMP" => "_TMP",
  60. "VAR" => "_VAR",
  61. "CONST" => "_CONST",
  62. "UNUSED" => "_UNUSED",
  63. "CV" => "_CV",
  64. );
  65. $typecode = array(
  66. "ANY" => 0,
  67. "TMP" => 1,
  68. "VAR" => 2,
  69. "CONST" => 0,
  70. "UNUSED" => 3,
  71. "CV" => 4,
  72. );
  73. $op1_type = array(
  74. "ANY" => "opline->op1_type",
  75. "TMP" => "IS_TMP_VAR",
  76. "VAR" => "IS_VAR",
  77. "CONST" => "IS_CONST",
  78. "UNUSED" => "IS_UNUSED",
  79. "CV" => "IS_CV",
  80. );
  81. $op2_type = array(
  82. "ANY" => "opline->op2_type",
  83. "TMP" => "IS_TMP_VAR",
  84. "VAR" => "IS_VAR",
  85. "CONST" => "IS_CONST",
  86. "UNUSED" => "IS_UNUSED",
  87. "CV" => "IS_CV",
  88. );
  89. $op1_free = array(
  90. "ANY" => "(free_op1.var != NULL)",
  91. "TMP" => "1",
  92. "VAR" => "(free_op1.var != NULL)",
  93. "CONST" => "0",
  94. "UNUSED" => "0",
  95. "CV" => "0",
  96. );
  97. $op2_free = array(
  98. "ANY" => "(free_op2.var != NULL)",
  99. "TMP" => "1",
  100. "VAR" => "(free_op2.var != NULL)",
  101. "CONST" => "0",
  102. "UNUSED" => "0",
  103. "CV" => "0",
  104. );
  105. $op1_get_zval_ptr = array(
  106. "ANY" => "get_zval_ptr(opline->op1_type, &opline->op1, execute_data, &free_op1, \\1)",
  107. "TMP" => "_get_zval_ptr_tmp(opline->op1.var, execute_data, &free_op1 TSRMLS_CC)",
  108. "VAR" => "_get_zval_ptr_var(opline->op1.var, execute_data, &free_op1 TSRMLS_CC)",
  109. "CONST" => "opline->op1.zv",
  110. "UNUSED" => "NULL",
  111. "CV" => "_get_zval_ptr_cv_\\1(execute_data, opline->op1.var TSRMLS_CC)",
  112. );
  113. $op2_get_zval_ptr = array(
  114. "ANY" => "get_zval_ptr(opline->op2_type, &opline->op2, execute_data, &free_op2, \\1)",
  115. "TMP" => "_get_zval_ptr_tmp(opline->op2.var, execute_data, &free_op2 TSRMLS_CC)",
  116. "VAR" => "_get_zval_ptr_var(opline->op2.var, execute_data, &free_op2 TSRMLS_CC)",
  117. "CONST" => "opline->op2.zv",
  118. "UNUSED" => "NULL",
  119. "CV" => "_get_zval_ptr_cv_\\1(execute_data, opline->op2.var TSRMLS_CC)",
  120. );
  121. $op1_get_zval_ptr_ptr = array(
  122. "ANY" => "get_zval_ptr_ptr(opline->op1_type, &opline->op1, execute_data, &free_op1, \\1)",
  123. "TMP" => "NULL",
  124. "VAR" => "_get_zval_ptr_ptr_var(opline->op1.var, execute_data, &free_op1 TSRMLS_CC)",
  125. "CONST" => "NULL",
  126. "UNUSED" => "NULL",
  127. "CV" => "_get_zval_ptr_ptr_cv_\\1(execute_data, opline->op1.var TSRMLS_CC)",
  128. );
  129. $op1_get_zval_ptr_ptr_fast = $op1_get_zval_ptr_ptr;
  130. $op1_get_zval_ptr_ptr_fast["VAR"] = "_get_zval_ptr_ptr_var_fast(opline->op1.var, execute_data, &free_op1 TSRMLS_CC)";
  131. $op2_get_zval_ptr_ptr = array(
  132. "ANY" => "get_zval_ptr_ptr(opline->op2_type, &opline->op2, execute_data, &free_op2, \\1)",
  133. "TMP" => "NULL",
  134. "VAR" => "_get_zval_ptr_ptr_var(opline->op2.var, execute_data, &free_op2 TSRMLS_CC)",
  135. "CONST" => "NULL",
  136. "UNUSED" => "NULL",
  137. "CV" => "_get_zval_ptr_ptr_cv_\\1(execute_data, opline->op2.var TSRMLS_CC)",
  138. );
  139. $op2_get_zval_ptr_ptr_fast = $op2_get_zval_ptr_ptr;
  140. $op2_get_zval_ptr_ptr_fast["VAR"] = "_get_zval_ptr_ptr_var_fast(opline->op2.var, execute_data, &free_op2 TSRMLS_CC)";
  141. $op1_get_obj_zval_ptr = array(
  142. "ANY" => "get_obj_zval_ptr(opline->op1_type, &opline->op1, execute_data, &free_op1, \\1)",
  143. "TMP" => "_get_zval_ptr_tmp(opline->op1.var, execute_data, &free_op1 TSRMLS_CC)",
  144. "VAR" => "_get_zval_ptr_var(opline->op1.var, execute_data, &free_op1 TSRMLS_CC)",
  145. "CONST" => "opline->op1.zv",
  146. "UNUSED" => "_get_obj_zval_ptr_unused(TSRMLS_C)",
  147. "CV" => "_get_zval_ptr_cv_\\1(execute_data, opline->op1.var TSRMLS_CC)",
  148. );
  149. $op2_get_obj_zval_ptr = array(
  150. "ANY" => "get_obj_zval_ptr(opline->op2_type, &opline->op2, execute_data, &free_op2, \\1)",
  151. "TMP" => "_get_zval_ptr_tmp(opline->op2.var, execute_data, &free_op2 TSRMLS_CC)",
  152. "VAR" => "_get_zval_ptr_var(opline->op2.var, execute_data, &free_op2 TSRMLS_CC)",
  153. "CONST" => "opline->op2.zv",
  154. "UNUSED" => "_get_obj_zval_ptr_unused(TSRMLS_C)",
  155. "CV" => "_get_zval_ptr_cv_\\1(execute_data, opline->op2.var TSRMLS_CC)",
  156. );
  157. $op1_get_obj_zval_ptr_ptr = array(
  158. "ANY" => "get_obj_zval_ptr_ptr(opline->op1_type, &opline->op1, execute_data, &free_op1, \\1)",
  159. "TMP" => "NULL",
  160. "VAR" => "_get_zval_ptr_ptr_var(opline->op1.var, execute_data, &free_op1 TSRMLS_CC)",
  161. "CONST" => "NULL",
  162. "UNUSED" => "_get_obj_zval_ptr_ptr_unused(TSRMLS_C)",
  163. "CV" => "_get_zval_ptr_ptr_cv_\\1(execute_data, opline->op1.var TSRMLS_CC)",
  164. );
  165. $op1_get_obj_zval_ptr_ptr_fast = $op1_get_obj_zval_ptr_ptr;
  166. $op1_get_obj_zval_ptr_ptr_fast["VAR"] = "_get_zval_ptr_ptr_var_fast(opline->op1.var, execute_data, &free_op1 TSRMLS_CC)";
  167. $op2_get_obj_zval_ptr_ptr = array(
  168. "ANY" => "get_obj_zval_ptr_ptr(opline->op2_type, &opline->op2, execute_data, &free_op2, \\1)",
  169. "TMP" => "NULL",
  170. "VAR" => "_get_zval_ptr_ptr_var(opline->op2.var, execute_data, &free_op2 TSRMLS_CC)",
  171. "CONST" => "NULL",
  172. "UNUSED" => "_get_obj_zval_ptr_ptr_unused(TSRMLS_C)",
  173. "CV" => "_get_zval_ptr_ptr_cv_\\1(execute_data, opline->op2.var TSRMLS_CC)",
  174. );
  175. $op2_get_obj_zval_ptr_ptr_fast = $op2_get_obj_zval_ptr_ptr;
  176. $op2_get_obj_zval_ptr_ptr_fast["VAR"] = "_get_zval_ptr_ptr_var_fast(opline->op2.var, execute_data, &free_op2 TSRMLS_CC)";
  177. $op1_is_tmp_free = array(
  178. "ANY" => "IS_TMP_FREE(free_op1)",
  179. "TMP" => "1",
  180. "VAR" => "0",
  181. "CONST" => "0",
  182. "UNUSED" => "0",
  183. "CV" => "0",
  184. );
  185. $op2_is_tmp_free = array(
  186. "ANY" => "IS_TMP_FREE(free_op2)",
  187. "TMP" => "1",
  188. "VAR" => "0",
  189. "CONST" => "0",
  190. "UNUSED" => "0",
  191. "CV" => "0",
  192. );
  193. $op1_free_op = array(
  194. "ANY" => "FREE_OP(free_op1)",
  195. "TMP" => "zval_dtor(free_op1.var)",
  196. "VAR" => "zval_ptr_dtor_nogc(&free_op1.var)",
  197. "CONST" => "",
  198. "UNUSED" => "",
  199. "CV" => "",
  200. );
  201. $op2_free_op = array(
  202. "ANY" => "FREE_OP(free_op2)",
  203. "TMP" => "zval_dtor(free_op2.var)",
  204. "VAR" => "zval_ptr_dtor_nogc(&free_op2.var)",
  205. "CONST" => "",
  206. "UNUSED" => "",
  207. "CV" => "",
  208. );
  209. $op1_free_op_if_var = array(
  210. "ANY" => "FREE_OP_IF_VAR(free_op1)",
  211. "TMP" => "",
  212. "VAR" => "zval_ptr_dtor_nogc(&free_op1.var)",
  213. "CONST" => "",
  214. "UNUSED" => "",
  215. "CV" => "",
  216. );
  217. $op2_free_op_if_var = array(
  218. "ANY" => "FREE_OP_IF_VAR(free_op2)",
  219. "TMP" => "",
  220. "VAR" => "zval_ptr_dtor_nogc(&free_op2.var)",
  221. "CONST" => "",
  222. "UNUSED" => "",
  223. "CV" => "",
  224. );
  225. $op1_free_op_var_ptr = array(
  226. "ANY" => "if (free_op1.var) {zval_ptr_dtor_nogc(&free_op1.var);}",
  227. "TMP" => "",
  228. "VAR" => "if (free_op1.var) {zval_ptr_dtor_nogc(&free_op1.var);}",
  229. "CONST" => "",
  230. "UNUSED" => "",
  231. "CV" => "",
  232. );
  233. $op1_free_op_var_ptr_fast = $op1_free_op_var_ptr;
  234. $op1_free_op_var_ptr_fast["VAR"] = "zval_ptr_dtor_nogc(&free_op1.var)";
  235. $op2_free_op_var_ptr = array(
  236. "ANY" => "if (free_op2.var) {zval_ptr_dtor_nogc(&free_op2.var);}",
  237. "TMP" => "",
  238. "VAR" => "if (free_op2.var) {zval_ptr_dtor_nogc(&free_op2.var);}",
  239. "CONST" => "",
  240. "UNUSED" => "",
  241. "CV" => "",
  242. );
  243. $op2_free_op_var_ptr_fast = $op2_free_op_var_ptr;
  244. $op2_free_op_var_ptr_fast["VAR"] = "zval_ptr_dtor_nogc(&free_op2.var)";
  245. $list = array(); // list of opcode handlers and helpers in original order
  246. $opcodes = array(); // opcode handlers by code
  247. $helpers = array(); // opcode helpers by name
  248. $params = array(); // parameters of helpers
  249. $opnames = array(); // opcode name to code mapping
  250. $line_no = 1;
  251. // Writes $s into resulting executor
  252. function out($f, $s) {
  253. global $line_no;
  254. fputs($f,$s);
  255. $line_no += substr_count($s, "\n");
  256. }
  257. // Resets #line directives in resulting executor
  258. function out_line($f) {
  259. global $line_no, $executor_file;
  260. fputs($f,"#line ".($line_no+1)." \"".$executor_file."\"\n");
  261. ++$line_no;
  262. }
  263. // Returns name of specialized helper
  264. function helper_name($name, $spec, $op1, $op2) {
  265. global $prefix, $helpers;
  266. if (isset($helpers[$name])) {
  267. // If we haven't helper with specified spicialized operands then
  268. // using unspecialized helper
  269. if (!isset($helpers[$name]["op1"][$op1]) &&
  270. isset($helpers[$name]["op1"]["ANY"])) {
  271. $op1 = "ANY";
  272. }
  273. if (!isset($helpers[$name]["op2"][$op2]) &&
  274. isset($helpers[$name]["op2"]["ANY"])) {
  275. $op2 = "ANY";
  276. }
  277. }
  278. return $name.($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2];
  279. }
  280. // Generates code for opcode handler or helper
  281. function gen_code($f, $spec, $kind, $export, $code, $op1, $op2, $name) {
  282. global $op1_type, $op2_type, $op1_get_zval_ptr, $op2_get_zval_ptr,
  283. $op1_get_zval_ptr_ptr, $op2_get_zval_ptr_ptr,
  284. $op1_get_obj_zval_ptr, $op2_get_obj_zval_ptr,
  285. $op1_get_obj_zval_ptr_ptr, $op2_get_obj_zval_ptr_ptr,
  286. $op1_is_tmp_free, $op2_is_tmp_free, $op1_free, $op2_free,
  287. $op1_free_op, $op2_free_op, $op1_free_op_if_var, $op2_free_op_if_var,
  288. $op1_free_op_var_ptr, $op2_free_op_var_ptr, $prefix,
  289. $op1_get_zval_ptr_ptr_fast, $op2_get_zval_ptr_ptr_fast,
  290. $op1_get_obj_zval_ptr_ptr_fast, $op2_get_obj_zval_ptr_ptr_fast,
  291. $op1_free_op_var_ptr_fast, $op2_free_op_var_ptr_fast;
  292. // Specializing
  293. $code = preg_replace(
  294. array(
  295. "/OP1_TYPE/",
  296. "/OP2_TYPE/",
  297. "/OP1_FREE/",
  298. "/OP2_FREE/",
  299. "/GET_OP1_ZVAL_PTR\(([^)]*)\)/",
  300. "/GET_OP2_ZVAL_PTR\(([^)]*)\)/",
  301. "/GET_OP1_ZVAL_PTR_PTR\(([^)]*)\)/",
  302. "/GET_OP2_ZVAL_PTR_PTR\(([^)]*)\)/",
  303. "/GET_OP1_OBJ_ZVAL_PTR\(([^)]*)\)/",
  304. "/GET_OP2_OBJ_ZVAL_PTR\(([^)]*)\)/",
  305. "/GET_OP1_OBJ_ZVAL_PTR_PTR\(([^)]*)\)/",
  306. "/GET_OP2_OBJ_ZVAL_PTR_PTR\(([^)]*)\)/",
  307. "/IS_OP1_TMP_FREE\(\)/",
  308. "/IS_OP2_TMP_FREE\(\)/",
  309. "/FREE_OP1\(\)/",
  310. "/FREE_OP2\(\)/",
  311. "/FREE_OP1_IF_VAR\(\)/",
  312. "/FREE_OP2_IF_VAR\(\)/",
  313. "/FREE_OP1_VAR_PTR\(\)/",
  314. "/FREE_OP2_VAR_PTR\(\)/",
  315. "/GET_OP1_ZVAL_PTR_PTR_FAST\(([^)]*)\)/",
  316. "/GET_OP2_ZVAL_PTR_PTR_FAST\(([^)]*)\)/",
  317. "/GET_OP1_OBJ_ZVAL_PTR_PTR_FAST\(([^)]*)\)/",
  318. "/GET_OP2_OBJ_ZVAL_PTR_PTR_FAST\(([^)]*)\)/",
  319. "/FREE_OP1_VAR_PTR_FAST\(\)/",
  320. "/FREE_OP2_VAR_PTR_FAST\(\)/",
  321. "/^#ifdef\s+ZEND_VM_SPEC\s*\n/m",
  322. "/^#ifndef\s+ZEND_VM_SPEC\s*\n/m",
  323. "/\!defined\(ZEND_VM_SPEC\)/m",
  324. "/defined\(ZEND_VM_SPEC\)/m",
  325. "/ZEND_VM_C_LABEL\(\s*([A-Za-z_]*)\s*\)/m",
  326. "/ZEND_VM_C_GOTO\(\s*([A-Za-z_]*)\s*\)/m",
  327. "/^#if\s+1\s*\\|\\|.*[^\\\\]$/m",
  328. "/^#if\s+0\s*&&.*[^\\\\]$/m",
  329. "/^#ifdef\s+ZEND_VM_EXPORT\s*\n/m",
  330. "/^#ifndef\s+ZEND_VM_EXPORT\s*\n/m"
  331. ),
  332. array(
  333. $op1_type[$op1],
  334. $op2_type[$op2],
  335. $op1_free[$op1],
  336. $op2_free[$op2],
  337. $op1_get_zval_ptr[$op1],
  338. $op2_get_zval_ptr[$op2],
  339. $op1_get_zval_ptr_ptr[$op1],
  340. $op2_get_zval_ptr_ptr[$op2],
  341. $op1_get_obj_zval_ptr[$op1],
  342. $op2_get_obj_zval_ptr[$op2],
  343. $op1_get_obj_zval_ptr_ptr[$op1],
  344. $op2_get_obj_zval_ptr_ptr[$op2],
  345. $op1_is_tmp_free[$op1],
  346. $op2_is_tmp_free[$op2],
  347. $op1_free_op[$op1],
  348. $op2_free_op[$op2],
  349. $op1_free_op_if_var[$op1],
  350. $op2_free_op_if_var[$op2],
  351. $op1_free_op_var_ptr[$op1],
  352. $op2_free_op_var_ptr[$op2],
  353. $op1_get_zval_ptr_ptr_fast[$op1],
  354. $op2_get_zval_ptr_ptr_fast[$op2],
  355. $op1_get_obj_zval_ptr_ptr_fast[$op1],
  356. $op2_get_obj_zval_ptr_ptr_fast[$op2],
  357. $op1_free_op_var_ptr_fast[$op1],
  358. $op2_free_op_var_ptr_fast[$op2],
  359. ($op1!="ANY"||$op2!="ANY")?"#if 1\n":"#if 0\n",
  360. ($op1!="ANY"||$op2!="ANY")?"#if 0\n":"#if 1\n",
  361. ($op1!="ANY"||$op2!="ANY")?"0":"1",
  362. ($op1!="ANY"||$op2!="ANY")?"1":"0",
  363. "\\1".(($spec && $kind != ZEND_VM_KIND_CALL)?("_SPEC".$prefix[$op1].$prefix[$op2]):""),
  364. "goto \\1".(($spec && $kind != ZEND_VM_KIND_CALL)?("_SPEC".$prefix[$op1].$prefix[$op2]):""),
  365. "#if 1",
  366. "#if 0",
  367. $export?"#if 1\n":"#if 0\n",
  368. $export?"#if 0\n":"#if 1\n"
  369. ),
  370. $code);
  371. if (0 && strpos($code, '{') === 0) {
  372. $code = "{\n\tfprintf(stderr, \"$name\\n\");\n" . substr($code, 1);
  373. }
  374. // Updating code according to selected threading model
  375. switch($kind) {
  376. case ZEND_VM_KIND_CALL:
  377. $code = preg_replace_callback(
  378. array(
  379. "/EXECUTE_DATA/m",
  380. "/ZEND_VM_DISPATCH_TO_HANDLER\(\s*([A-Z_]*)\s*\)/m",
  381. "/ZEND_VM_DISPATCH_TO_HELPER\(\s*([A-Za-z_]*)\s*\)/m",
  382. "/ZEND_VM_DISPATCH_TO_HELPER_EX\(\s*([A-Za-z_]*)\s*,\s*[A-Za-z_]*\s*,\s*(.*)\s*\);/m",
  383. ),
  384. function($matches) use ($spec, $prefix, $op1, $op2) {
  385. if (strncasecmp($matches[0], "EXECUTE_DATA", strlen("EXECUTE_DATA")) == 0) {
  386. return "execute_data";
  387. } else if (strncasecmp($matches[0], "ZEND_VM_DISPATCH_TO_HANDLER", strlen("ZEND_VM_DISPATCH_TO_HANDLER")) == 0) {
  388. return "return " . $matches[1] . ($spec?"_SPEC":"") . $prefix[$op1] . $prefix[$op2] . "_HANDLER(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU)";
  389. } else if (strncasecmp($matches[0], "ZEND_VM_DISPATCH_TO_HELPER_EX", strlen("ZEND_VM_DISPATCH_TO_HELPER_EX")) == 0) {
  390. return "return " . helper_name($matches[1], $spec, $op1, $op2) . "(" . $matches[2]. ", ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);";
  391. } else {
  392. return "return " . helper_name($matches[1], $spec, $op1, $op2) . "(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU)";
  393. }
  394. },
  395. $code);
  396. break;
  397. case ZEND_VM_KIND_SWITCH:
  398. $code = preg_replace_callback(
  399. array(
  400. "/EXECUTE_DATA/m",
  401. "/ZEND_VM_DISPATCH_TO_HANDLER\(\s*([A-Z_]*)\s*\)/m",
  402. "/ZEND_VM_DISPATCH_TO_HELPER\(\s*([A-Za-z_]*)\s*\)/m",
  403. "/ZEND_VM_DISPATCH_TO_HELPER_EX\(\s*([A-Za-z_]*)\s*,\s*([A-Za-z_]*)\s*,\s*(.*)\s*\);/m",
  404. ),
  405. function($matches) use ($spec, $prefix, $op1, $op2) {
  406. if (strncasecmp($matches[0], "EXECUTE_DATA", strlen("EXECUTE_DATA")) == 0) {
  407. return "execute_data";
  408. } else if (strncasecmp($matches[0], "ZEND_VM_DISPATCH_TO_HANDLER", strlen("ZEND_VM_DISPATCH_TO_HANDLER")) == 0) {
  409. return "goto " . $matches[1] . ($spec?"_SPEC":"") . $prefix[$op1] . $prefix[$op2] . "_LABEL";
  410. } else if (strncasecmp($matches[0], "ZEND_VM_DISPATCH_TO_HELPER_EX", strlen("ZEND_VM_DISPATCH_TO_HELPER_EX")) == 0) {
  411. return $matches[2] . " = " . $matches[3] . "; goto " . helper_name($matches[1], $spec, $op1, $op2) . ";";
  412. } else {
  413. return "goto " . helper_name($matches[1], $spec, $op1, $op2);
  414. }
  415. },
  416. $code);
  417. break;
  418. case ZEND_VM_KIND_GOTO:
  419. $code = preg_replace_callback(
  420. array(
  421. "/EXECUTE_DATA/m",
  422. "/ZEND_VM_DISPATCH_TO_HANDLER\(\s*([A-Z_]*)\s*\)/m",
  423. "/ZEND_VM_DISPATCH_TO_HELPER\(\s*([A-Za-z_]*)\s*\)/m",
  424. "/ZEND_VM_DISPATCH_TO_HELPER_EX\(\s*([A-Za-z_]*)\s*,\s*([A-Za-z_]*)\s*,\s*(.*)\s*\);/m",
  425. ),
  426. function($matches) use ($spec, $prefix, $op1, $op2) {
  427. if (strncasecmp($matches[0], "EXECUTE_DATA", strlen("EXECUTE_DATA")) == 0) {
  428. return "execute_data";
  429. } else if (strncasecmp($matches[0], "ZEND_VM_DISPATCH_TO_HANDLER", strlen("ZEND_VM_DISPATCH_TO_HANDLER")) == 0) {
  430. return "goto " . $matches[1] . ($spec?"_SPEC":"") . $prefix[$op1] . $prefix[$op2] . "_HANDLER";
  431. } else if (strncasecmp($matches[0], "ZEND_VM_DISPATCH_TO_HELPER_EX", strlen("ZEND_VM_DISPATCH_TO_HELPER_EX")) == 0) {
  432. return $matches[2] . " = " . $matches[3] . "; goto " . helper_name($matches[1], $spec, $op1, $op2) . ";";
  433. } else {
  434. return "goto " . helper_name($matches[1], $spec, $op1, $op2);
  435. }
  436. },
  437. $code);
  438. break;
  439. }
  440. /* Remove unused free_op1 and free_op2 declarations */
  441. if ($spec && preg_match_all('/^\s*zend_free_op\s+[^;]+;\s*$/me', $code, $matches, PREG_SET_ORDER)) {
  442. $n = 0;
  443. foreach ($matches as $match) {
  444. $code = preg_replace('/'.preg_quote($match[0],'/').'/', "\$D$n", $code);
  445. ++$n;
  446. }
  447. $del_free_op1 = (strpos($code, "free_op1") === false);
  448. $del_free_op2 = (strpos($code, "free_op2") === false);
  449. $n = 0;
  450. foreach ($matches as $match) {
  451. $dcl = $match[0];
  452. $changed = 0;
  453. if ($del_free_op1 && strpos($dcl, "free_op1") !== false) {
  454. $dcl = preg_replace("/free_op1\s*,\s*/", "", $dcl);
  455. $dcl = preg_replace("/free_op1\s*;/", ";", $dcl);
  456. $changed = 1;
  457. }
  458. if ($del_free_op2 && strpos($dcl, "free_op2") !== false) {
  459. $dcl = preg_replace("/free_op2\s*,\s*/", "", $dcl);
  460. $dcl = preg_replace("/free_op2\s*;/", ";", $dcl);
  461. $changed = 1;
  462. }
  463. if ($changed) {
  464. $dcl = preg_replace("/,\s*;/", ";", $dcl);
  465. $dcl = preg_replace("/zend_free_op\s*;/", "", $dcl);
  466. }
  467. $code = preg_replace("/\\\$D$n/", $dcl, $code);
  468. ++$n;
  469. }
  470. }
  471. /* Remove unnecessary ';' */
  472. $code = preg_replace('/^\s*;\s*$/m', '', $code);
  473. /* Remove WS */
  474. $code = preg_replace('/[ \t]+\n/m', "\n", $code);
  475. out($f, $code);
  476. }
  477. // Generates opcode handler
  478. function gen_handler($f, $spec, $kind, $name, $op1, $op2, $use, $code, $lineno) {
  479. global $definition_file, $prefix, $typecode, $opnames;
  480. if (ZEND_VM_LINES) {
  481. out($f, "#line $lineno \"$definition_file\"\n");
  482. }
  483. // Generate opcode handler's entry point according to selected threading model
  484. switch($kind) {
  485. case ZEND_VM_KIND_CALL:
  486. out($f,"static int ZEND_FASTCALL ".$name.($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2]."_HANDLER(ZEND_OPCODE_HANDLER_ARGS)\n");
  487. break;
  488. case ZEND_VM_KIND_SWITCH:
  489. if ($spec) {
  490. out($f,"case ".((string)($opnames[$name]*25+($typecode[$op1]*5)+$typecode[$op2])).": /*".$name."_SPEC".$prefix[$op1].$prefix[$op2]."_HANDLER*/");
  491. } else {
  492. out($f,"case ".$name.":");
  493. }
  494. if ($use) {
  495. // This handler is used by other handlers. We will add label to call it.
  496. out($f," ".$name.($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2]."_LABEL:\n");
  497. } else {
  498. out($f,"\n");
  499. }
  500. break;
  501. case ZEND_VM_KIND_GOTO:
  502. out($f,$name.($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2]."_HANDLER: ZEND_VM_GUARD(".$name.($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2].");\n");
  503. break;
  504. }
  505. // Generate opcode handler's code
  506. gen_code($f, $spec, $kind, 0, $code, $op1, $op2, $name);
  507. }
  508. // Generates helper
  509. function gen_helper($f, $spec, $kind, $name, $op1, $op2, $param, $code, $lineno) {
  510. global $definition_file, $prefix;
  511. if (ZEND_VM_LINES) {
  512. out($f, "#line $lineno \"$definition_file\"\n");
  513. }
  514. // Generate helper's entry point according to selected threading model
  515. switch($kind) {
  516. case ZEND_VM_KIND_CALL:
  517. if ($param == null) {
  518. // Helper without parameters
  519. out($f, "static int ZEND_FASTCALL ".$name.($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2]."(ZEND_OPCODE_HANDLER_ARGS)\n");
  520. } else {
  521. // Helper with parameter
  522. out($f, "static int ZEND_FASTCALL ".$name.($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2]."(".$param.", ZEND_OPCODE_HANDLER_ARGS)\n");
  523. }
  524. break;
  525. case ZEND_VM_KIND_SWITCH:
  526. out($f, $name.($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2].":\n");
  527. break;
  528. case ZEND_VM_KIND_GOTO:
  529. out($f, $name.($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2].":\n");
  530. break;
  531. }
  532. // Generate helper's code
  533. gen_code($f, $spec, $kind, 0, $code, $op1, $op2, $name);
  534. }
  535. // Generates array of opcode handlers (specialized or unspecialized)
  536. function gen_labels($f, $spec, $kind, $prolog) {
  537. global $opcodes, $op_types, $prefix, $typecode;
  538. $next = 0;
  539. if ($spec) {
  540. // Emit labels for specialized executor
  541. // For each opcode in opcode number order
  542. foreach($opcodes as $num => $dsc) {
  543. while ($next != $num) {
  544. // If some opcode numbers are not used then fill hole with pointers
  545. // to handler of undefined opcode
  546. $op1t = $op_types;
  547. // For each op1.op_type except ANY
  548. foreach($op1t as $op1) {
  549. if ($op1 != "ANY") {
  550. $op2t = $op_types;
  551. // For each op2.op_type except ANY
  552. foreach($op2t as $op2) {
  553. if ($op2 != "ANY") {
  554. // Emit pointer to handler of undefined opcode
  555. switch ($kind) {
  556. case ZEND_VM_KIND_CALL:
  557. out($f,$prolog."ZEND_NULL_HANDLER,\n");
  558. break;
  559. case ZEND_VM_KIND_SWITCH:
  560. out($f,$prolog."(opcode_handler_t)-1,\n");
  561. break;
  562. case ZEND_VM_KIND_GOTO:
  563. out($f,$prolog."(opcode_handler_t)&&ZEND_NULL_HANDLER,\n");
  564. break;
  565. }
  566. }
  567. }
  568. }
  569. }
  570. $next++;
  571. }
  572. $next = $num + 1;
  573. $op1t = $op_types;
  574. // For each op1.op_type except ANY
  575. foreach($op1t as $op1) {
  576. if ($op1 != "ANY") {
  577. if (!isset($dsc["op1"][$op1])) {
  578. // Try to use unspecialized handler
  579. $op1 = "ANY";
  580. }
  581. $op2t = $op_types;
  582. // For each op2.op_type except ANY
  583. foreach($op2t as $op2) {
  584. if ($op2 != "ANY") {
  585. if (!isset($dsc["op2"][$op2])) {
  586. // Try to use unspecialized handler
  587. $op2 = "ANY";
  588. }
  589. // Check if specialized handler is defined
  590. if (isset($dsc["op1"][$op1]) &&
  591. isset($dsc["op2"][$op2])) {
  592. // Emit pointer to specialized handler
  593. switch ($kind) {
  594. case ZEND_VM_KIND_CALL:
  595. out($f,$prolog.$dsc["op"]."_SPEC".$prefix[$op1].$prefix[$op2]."_HANDLER,\n");
  596. break;
  597. case ZEND_VM_KIND_SWITCH:
  598. out($f,$prolog."(opcode_handler_t)".((string)($num*25+$typecode[$op1]*5+$typecode[$op2])).",\n");
  599. break;
  600. case ZEND_VM_KIND_GOTO:
  601. out($f,$prolog."(opcode_handler_t)&&".$dsc["op"]."_SPEC".$prefix[$op1].$prefix[$op2]."_HANDLER,\n");
  602. break;
  603. }
  604. } else {
  605. // Emit pinter to handler of undefined opcode
  606. switch ($kind) {
  607. case ZEND_VM_KIND_CALL:
  608. out($f,$prolog."ZEND_NULL_HANDLER,\n");
  609. break;
  610. case ZEND_VM_KIND_SWITCH:
  611. out($f,$prolog."(opcode_handler_t)-1,\n");
  612. break;
  613. case ZEND_VM_KIND_GOTO:
  614. out($f,$prolog."(opcode_handler_t)&&ZEND_NULL_HANDLER,\n");
  615. break;
  616. }
  617. }
  618. }
  619. }
  620. }
  621. }
  622. }
  623. } else {
  624. // Emit labels for unspecialized executor
  625. // For each opcode in opcode number order
  626. foreach($opcodes as $num => $dsc) {
  627. while ($next != $num) {
  628. // If some opcode numbers are not used then fill hole with pointers
  629. // to handler of undefined opcode
  630. switch ($kind) {
  631. case ZEND_VM_KIND_CALL:
  632. out($f,$prolog."ZEND_NULL_HANDLER,\n");
  633. break;
  634. case ZEND_VM_KIND_SWITCH:
  635. out($f,$prolog."(opcode_handler_t)-1,\n");
  636. break;
  637. case ZEND_VM_KIND_GOTO:
  638. out($f,$prolog."(opcode_handler_t)&&ZEND_NULL_HANDLER,\n");
  639. break;
  640. }
  641. $next++;
  642. }
  643. $next = $num+1;
  644. // Emit pointer to unspecialized handler
  645. switch ($kind) {
  646. case ZEND_VM_KIND_CALL:
  647. out($f,$prolog.$dsc["op"]."_HANDLER,\n");
  648. break;
  649. case ZEND_VM_KIND_SWITCH:
  650. out($f,$prolog."(opcode_handler_t)".((string)$num).",\n");
  651. break;
  652. case ZEND_VM_KIND_GOTO:
  653. out($f,$prolog."(opcode_handler_t)&&".$dsc["op"]."_HANDLER,\n");
  654. break;
  655. }
  656. }
  657. }
  658. // Emit last handler's label (undefined opcode)
  659. switch ($kind) {
  660. case ZEND_VM_KIND_CALL:
  661. out($f,$prolog."ZEND_NULL_HANDLER\n");
  662. break;
  663. case ZEND_VM_KIND_SWITCH:
  664. out($f,$prolog."(opcode_handler_t)-1\n");
  665. break;
  666. case ZEND_VM_KIND_GOTO:
  667. out($f,$prolog."(opcode_handler_t)&&ZEND_NULL_HANDLER\n");
  668. break;
  669. }
  670. }
  671. // Generates handler for undefined opcodes (CALL threading model)
  672. function gen_null_handler($f) {
  673. static $done = 0;
  674. // New and all executors with CALL threading model can use the same handler
  675. // for undefined opcodes, do we emit code for it only once
  676. if (!$done) {
  677. $done = 1;
  678. out($f,"static int ZEND_FASTCALL ZEND_NULL_HANDLER(ZEND_OPCODE_HANDLER_ARGS)\n");
  679. out($f,"{\n");
  680. out($f,"\tzend_error_noreturn(E_ERROR, \"Invalid opcode %d/%d/%d.\", OPLINE->opcode, OPLINE->op1_type, OPLINE->op2_type);\n");
  681. out($f,"\tZEND_VM_NEXT_OPCODE(); /* Never reached */\n");
  682. out($f,"}\n\n");
  683. }
  684. }
  685. // Generates all opcode handlers and helpers (specialized or unspecilaized)
  686. function gen_executor_code($f, $spec, $kind, $prolog) {
  687. global $list, $opcodes, $helpers, $op_types;
  688. if ($spec) {
  689. // Produce specialized executor
  690. $op1t = $op_types;
  691. // for each op1.op_type
  692. foreach($op1t as $op1) {
  693. $op2t = $op_types;
  694. // for each op2.op_type
  695. foreach($op2t as $op2) {
  696. // for each handlers in helpers in original order
  697. foreach ($list as $lineno => $dsc) {
  698. if (isset($dsc["handler"])) {
  699. $num = $dsc["handler"];
  700. // Check if handler accepts such types of operands (op1 and op2)
  701. if (isset($opcodes[$num]["op1"][$op1]) &&
  702. isset($opcodes[$num]["op2"][$op2])) {
  703. // Generate handler code
  704. gen_handler($f, 1, $kind, $opcodes[$num]["op"], $op1, $op2, isset($opcodes[$num]["use"]), $opcodes[$num]["code"], $lineno);
  705. }
  706. } else if (isset($dsc["helper"])) {
  707. $num = $dsc["helper"];
  708. // Check if handler accepts such types of operands (op1 and op2)
  709. if (isset($helpers[$num]["op1"][$op1]) &&
  710. isset($helpers[$num]["op2"][$op2])) {
  711. // Generate helper code
  712. gen_helper($f, 1, $kind, $num, $op1, $op2, $helpers[$num]["param"], $helpers[$num]["code"], $lineno);
  713. }
  714. } else {
  715. var_dump($dsc);
  716. die("??? $kind:$num\n");
  717. }
  718. }
  719. }
  720. }
  721. } else {
  722. // Produce unspecialized executor
  723. // for each handlers in helpers in original order
  724. foreach ($list as $lineno => $dsc) {
  725. if (isset($dsc["handler"])) {
  726. $num = $dsc["handler"];
  727. // Generate handler code
  728. gen_handler($f, 0, $kind, $opcodes[$num]["op"], "ANY", "ANY", isset($opcodes[$num]["use"]), $opcodes[$num]["code"], $lineno);
  729. } else if (isset($dsc["helper"])) {
  730. $num = $dsc["helper"];
  731. // Generate helper code
  732. gen_helper($f, 0, $kind, $num, "ANY", "ANY", $helpers[$num]["param"], $helpers[$num]["code"], $lineno);
  733. } else {
  734. var_dump($dsc);
  735. die("??? $kind:$num\n");
  736. }
  737. }
  738. }
  739. if (ZEND_VM_LINES) {
  740. // Reset #line directives
  741. out_line($f);
  742. }
  743. // Generate handler for undefined opcodes
  744. switch ($kind) {
  745. case ZEND_VM_KIND_CALL:
  746. gen_null_handler($f);
  747. break;
  748. case ZEND_VM_KIND_SWITCH:
  749. out($f,"default:\n");
  750. out($f,"\tzend_error_noreturn(E_ERROR, \"Invalid opcode %d/%d/%d.\", OPLINE->opcode, OPLINE->op1_type, OPLINE->op2_type);\n");
  751. out($f,"\tZEND_VM_NEXT_OPCODE(); /* Never reached */\n");
  752. break;
  753. case ZEND_VM_KIND_GOTO:
  754. out($f,"ZEND_NULL_HANDLER:\n");
  755. out($f,"\tzend_error_noreturn(E_ERROR, \"Invalid opcode %d/%d/%d.\", OPLINE->opcode, OPLINE->op1_type, OPLINE->op2_type);\n");
  756. out($f,"\tZEND_VM_NEXT_OPCODE(); /* Never reached */\n");
  757. break;
  758. }
  759. }
  760. function skip_blanks($f, $prolog, $epilog) {
  761. if (trim($prolog) != "" || trim($epilog) != "") {
  762. out($f, $prolog.$epilog);
  763. }
  764. }
  765. // Generates executor from skeleton file and definition (specialized or unspecialized)
  766. function gen_executor($f, $skl, $spec, $kind, $executor_name, $initializer_name, $old) {
  767. global $params, $skeleton_file, $line_no;
  768. $lineno = 0;
  769. foreach ($skl as $line) {
  770. // Skeleton file contains special markers in form %NAME% those are
  771. // substituted by custom code
  772. if (preg_match("/(.*)[{][%]([A-Z_]*)[%][}](.*)/", $line, $m)) {
  773. switch ($m[2]) {
  774. case "DEFINES":
  775. if (ZEND_VM_OLD_EXECUTOR && $spec) {
  776. out($f,"static int zend_vm_old_executor = 0;\n\n");
  777. }
  778. out($f,"static opcode_handler_t zend_vm_get_opcode_handler(zend_uchar opcode, zend_op* op);\n\n");
  779. switch ($kind) {
  780. case ZEND_VM_KIND_CALL:
  781. out($f,"\n");
  782. out($f,"#undef OPLINE\n");
  783. out($f,"#undef DCL_OPLINE\n");
  784. out($f,"#undef USE_OPLINE\n");
  785. out($f,"#undef LOAD_OPLINE\n");
  786. out($f,"#undef SAVE_OPLINE\n");
  787. out($f,"#define OPLINE EX(opline)\n");
  788. out($f,"#define DCL_OPLINE\n");
  789. out($f,"#define USE_OPLINE zend_op *opline = EX(opline);\n");
  790. out($f,"#define LOAD_OPLINE()\n");
  791. out($f,"#define SAVE_OPLINE()\n");
  792. out($f,"#undef CHECK_EXCEPTION\n");
  793. out($f,"#undef HANDLE_EXCEPTION\n");
  794. out($f,"#undef HANDLE_EXCEPTION_LEAVE\n");
  795. out($f,"#define CHECK_EXCEPTION() LOAD_OPLINE()\n");
  796. out($f,"#define HANDLE_EXCEPTION() LOAD_OPLINE(); ZEND_VM_CONTINUE()\n");
  797. out($f,"#define HANDLE_EXCEPTION_LEAVE() LOAD_OPLINE(); ZEND_VM_LEAVE()\n");
  798. out($f,"#define LOAD_REGS()\n");
  799. out($f,"#define ZEND_VM_CONTINUE() return 0\n");
  800. out($f,"#define ZEND_VM_RETURN() return 1\n");
  801. out($f,"#define ZEND_VM_ENTER() return 2\n");
  802. out($f,"#define ZEND_VM_LEAVE() return 3\n");
  803. out($f,"#define ZEND_VM_DISPATCH(opcode, opline) return zend_vm_get_opcode_handler(opcode, opline)(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);\n\n");
  804. out($f,"#define ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_INTERNAL execute_data TSRMLS_CC\n");
  805. break;
  806. case ZEND_VM_KIND_SWITCH:
  807. out($f,"\n");
  808. out($f,"#undef OPLINE\n");
  809. out($f,"#undef DCL_OPLINE\n");
  810. out($f,"#undef USE_OPLINE\n");
  811. out($f,"#undef LOAD_OPLINE\n");
  812. out($f,"#undef SAVE_OPLINE\n");
  813. out($f,"#define OPLINE opline\n");
  814. out($f,"#define DCL_OPLINE zend_op *opline;\n");
  815. out($f,"#define USE_OPLINE\n");
  816. out($f,"#define LOAD_OPLINE() opline = EX(opline)\n");
  817. out($f,"#define SAVE_OPLINE() EX(opline) = opline\n");
  818. out($f,"#undef CHECK_EXCEPTION\n");
  819. out($f,"#undef HANDLE_EXCEPTION\n");
  820. out($f,"#undef HANDLE_EXCEPTION_LEAVE\n");
  821. out($f,"#define CHECK_EXCEPTION() LOAD_OPLINE()\n");
  822. out($f,"#define HANDLE_EXCEPTION() LOAD_OPLINE(); ZEND_VM_CONTINUE()\n");
  823. out($f,"#define HANDLE_EXCEPTION_LEAVE() LOAD_OPLINE(); ZEND_VM_LEAVE()\n");
  824. out($f,"#define LOAD_REGS()\n");
  825. out($f,"#define ZEND_VM_CONTINUE() goto zend_vm_continue\n");
  826. out($f,"#define ZEND_VM_RETURN() EG(in_execution) = original_in_execution; return\n");
  827. out($f,"#define ZEND_VM_ENTER() goto zend_vm_enter\n");
  828. out($f,"#define ZEND_VM_LEAVE() ZEND_VM_CONTINUE()\n");
  829. out($f,"#define ZEND_VM_DISPATCH(opcode, opline) dispatch_handler = zend_vm_get_opcode_handler(opcode, opline); goto zend_vm_dispatch;\n\n");
  830. out($f,"#define ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_INTERNAL execute_data TSRMLS_CC\n");
  831. break;
  832. case ZEND_VM_KIND_GOTO:
  833. out($f,"\n");
  834. out($f,"#undef OPLINE\n");
  835. out($f,"#undef DCL_OPLINE\n");
  836. out($f,"#undef USE_OPLINE\n");
  837. out($f,"#undef LOAD_OPLINE\n");
  838. out($f,"#undef SAVE_OPLINE\n");
  839. out($f,"#define OPLINE opline\n");
  840. out($f,"#define DCL_OPLINE zend_op *opline;\n");
  841. out($f,"#define USE_OPLINE\n");
  842. out($f,"#define LOAD_OPLINE() opline = EX(opline)\n");
  843. out($f,"#define SAVE_OPLINE() EX(opline) = opline\n");
  844. out($f,"#undef CHECK_EXCEPTION\n");
  845. out($f,"#undef HANDLE_EXCEPTION\n");
  846. out($f,"#undef HANDLE_EXCEPTION_LEAVE\n");
  847. if (ZEND_VM_SPEC) {
  848. out($f,"#define CHECK_EXCEPTION() if (UNEXPECTED(EG(exception) != NULL)) goto ZEND_HANDLE_EXCEPTION_SPEC_HANDLER\n");
  849. out($f,"#define HANDLE_EXCEPTION() goto ZEND_HANDLE_EXCEPTION_SPEC_HANDLER\n");
  850. out($f,"#define HANDLE_EXCEPTION_LEAVE() goto ZEND_HANDLE_EXCEPTION_SPEC_HANDLER\n");
  851. } else {
  852. out($f,"#define CHECK_EXCEPTION() if (UNEXPECTED(EG(exception) != NULL)) goto ZEND_HANDLE_EXCEPTION_HANDLER\n");
  853. out($f,"#define HANDLE_EXCEPTION() goto ZEND_HANDLE_EXCEPTION_HANDLER\n");
  854. out($f,"#define HANDLE_EXCEPTION_LEAVE() goto ZEND_HANDLE_EXCEPTION_HANDLER\n");
  855. }
  856. out($f,"#define LOAD_REGS()\n");
  857. out($f,"#define ZEND_VM_CONTINUE() goto *(void**)(OPLINE->handler)\n");
  858. out($f,"#define ZEND_VM_RETURN() EG(in_execution) = original_in_execution; return\n");
  859. out($f,"#define ZEND_VM_ENTER() goto zend_vm_enter\n");
  860. out($f,"#define ZEND_VM_LEAVE() ZEND_VM_CONTINUE()\n");
  861. out($f,"#define ZEND_VM_DISPATCH(opcode, opline) goto *(void**)(zend_vm_get_opcode_handler(opcode, opline));\n\n");
  862. out($f,"#define ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_INTERNAL execute_data TSRMLS_CC\n");
  863. break;
  864. }
  865. break;
  866. case "EXECUTOR_NAME":
  867. out($f, $m[1].$executor_name.$m[3]."\n");
  868. break;
  869. case "HELPER_VARS":
  870. if ($kind != ZEND_VM_KIND_CALL) {
  871. if ($kind == ZEND_VM_KIND_SWITCH) {
  872. out($f,$m[1]."opcode_handler_t dispatch_handler;\n");
  873. }
  874. // Emit local variables those are used for helpers' parameters
  875. foreach ($params as $param => $x) {
  876. out($f,$m[1].$param.";\n");
  877. }
  878. } else {
  879. skip_blanks($f, $m[1], $m[3]."\n");
  880. }
  881. break;
  882. case "INTERNAL_LABELS":
  883. if ($kind == ZEND_VM_KIND_GOTO) {
  884. // Emit array of labels of opcode handlers and code for
  885. // zend_opcode_handlers initialization
  886. $prolog = $m[1];
  887. out($f,$prolog."if (execute_data == NULL) {\n");
  888. out($f,$prolog."\tstatic const opcode_handler_t labels[] = {\n");
  889. gen_labels($f, $spec, $kind, $prolog."\t\t");
  890. out($f,$prolog."\t};\n");
  891. out($f,$prolog."\tzend_opcode_handlers = (opcode_handler_t*)labels;\n");
  892. out($f,$prolog."\treturn;\n");
  893. out($f,$prolog."}\n");
  894. } else {
  895. skip_blanks($f, $m[1], $m[3]);
  896. }
  897. break;
  898. case "ZEND_VM_CONTINUE_LABEL":
  899. if ($kind == ZEND_VM_KIND_CALL) {
  900. // Only SWITCH dispatch method use it
  901. out($f,$m[1]."\tint ret;".$m[3]."\n");
  902. } else if ($kind == ZEND_VM_KIND_SWITCH) {
  903. // Only SWITCH dispatch method use it
  904. out($f,"zend_vm_continue:".$m[3]."\n");
  905. } else {
  906. skip_blanks($f, $m[1], $m[3]);
  907. }
  908. break;
  909. case "ZEND_VM_DISPATCH":
  910. // Emit code that dispatches to opcode handler
  911. switch ($kind) {
  912. case ZEND_VM_KIND_CALL:
  913. out($f, $m[1]."if ((ret = OPLINE->handler(execute_data TSRMLS_CC)) > 0)".$m[3]."\n");
  914. break;
  915. case ZEND_VM_KIND_SWITCH:
  916. out($f, $m[1]."dispatch_handler = OPLINE->handler;\nzend_vm_dispatch:\n".$m[1]."switch ((int)dispatch_handler)".$m[3]."\n");
  917. break;
  918. case ZEND_VM_KIND_GOTO:
  919. out($f, $m[1]."goto *(void**)(OPLINE->handler);".$m[3]."\n");
  920. break;
  921. }
  922. break;
  923. case "INTERNAL_EXECUTOR":
  924. if ($kind == ZEND_VM_KIND_CALL) {
  925. // Executor is defined as a set of functions
  926. out($f, $m[1]."switch (ret) {\n" .
  927. $m[1]."\tcase 1:\n" .
  928. $m[1]."\t\tEG(in_execution) = original_in_execution;\n".
  929. $m[1]."\t\treturn;\n".
  930. $m[1]."\tcase 2:\n" .
  931. $m[1]."\t\tgoto zend_vm_enter;\n".
  932. $m[1]."\t\tbreak;\n" .
  933. $m[1]."\tcase 3:\n" .
  934. $m[1]."\t\texecute_data = EG(current_execute_data);\n".
  935. $m[1]."\t\tbreak;\n" .
  936. $m[1]."\tdefault:\n".
  937. $m[1]."\t\tbreak;\n".
  938. $m[1]."}".$m[3]."\n");
  939. } else {
  940. // Emit executor code
  941. gen_executor_code($f, $spec, $kind, $m[1]);
  942. }
  943. break;
  944. case "EXTERNAL_EXECUTOR":
  945. if ($kind == ZEND_VM_KIND_CALL) {
  946. // Unspecialized executor with CALL threading is the same as the
  947. // old one, so we don't need to produce code twitch
  948. if (!$old || ZEND_VM_SPEC || (ZEND_VM_KIND != ZEND_VM_KIND_CALL)) {
  949. // Emit executor code
  950. gen_executor_code($f, $spec, $kind, $m[1]);
  951. }
  952. }
  953. break;
  954. case "INITIALIZER_NAME":
  955. out($f, $m[1].$initializer_name.$m[3]."\n");
  956. break;
  957. case "EXTERNAL_LABELS":
  958. // Emit code that initializes zend_opcode_handlers array
  959. $prolog = $m[1];
  960. if ($kind == ZEND_VM_KIND_GOTO) {
  961. // Labels are defined in the executor itself, so we call it
  962. // with execute_data NULL and it sets zend_opcode_handlers array
  963. out($f,$prolog."TSRMLS_FETCH();\n");
  964. out($f,$prolog.$executor_name."_ex(NULL TSRMLS_CC);\n");
  965. } else {
  966. if ($old) {
  967. // Reserving space for user-defined opcodes
  968. out($f,$prolog."static opcode_handler_t labels[512] = {\n");
  969. } else {
  970. out($f,$prolog."static const opcode_handler_t labels[] = {\n");
  971. }
  972. gen_labels($f, $spec, $kind, $prolog."\t");
  973. out($f,$prolog."};\n");
  974. out($f,$prolog."zend_opcode_handlers = (opcode_handler_t*)labels;\n");
  975. if ($old) {
  976. // Setup old executor
  977. out($f,$prolog."zend_vm_old_executor = 1;\n");
  978. out($f,$prolog."zend_execute = old_execute;\n");
  979. }
  980. }
  981. break;
  982. default:
  983. die("ERROR: Unknown keyword ".$m[2]." in skeleton file.\n");
  984. }
  985. } else {
  986. // Copy the line as is
  987. out($f, $line);
  988. }
  989. }
  990. }
  991. function gen_vm($def, $skel) {
  992. global $definition_file, $skeleton_file, $executor_file,
  993. $op_types, $list, $opcodes, $helpers, $params, $opnames;
  994. // Load definition file
  995. $in = @file($def);
  996. if (!$in) {
  997. die("ERROR: Can not open definition file '$def'\n");
  998. }
  999. // We need absolute path to definition file to use it in #line directives
  1000. $definition_file = realpath($def);
  1001. // Load skeleton file
  1002. $skl = @file($skel);
  1003. if (!$skl) {
  1004. die("ERROR: Can not open skeleton file '$skel'\n");
  1005. }
  1006. // We need absolute path to skeleton file to use it in #line directives
  1007. $skeleton_file = realpath($skel);
  1008. // Parse definition file into tree
  1009. $lineno = 0;
  1010. $handler = null;
  1011. $helper = null;
  1012. $max_opcode_len = 0;
  1013. $max_opcode = 0;
  1014. $export = array();
  1015. foreach ($in as $line) {
  1016. ++$lineno;
  1017. if (strpos($line,"ZEND_VM_HANDLER(") === 0) {
  1018. // Parsing opcode handler's definition
  1019. if (preg_match(
  1020. "/^ZEND_VM_HANDLER\(\s*([0-9]+)\s*,\s*([A-Z_]+)\s*,\s*([A-Z|]+)\s*,\s*([A-Z|]+)\s*\)/",
  1021. $line,
  1022. $m) == 0) {
  1023. die("ERROR ($def:$lineno): Invalid ZEND_VM_HANDLER definition.\n");
  1024. }
  1025. $code = (int)$m[1];
  1026. $op = $m[2];
  1027. $len = strlen($op);
  1028. $op1 = array_flip(explode("|",$m[3]));
  1029. $op2 = array_flip(explode("|",$m[4]));
  1030. if ($len > $max_opcode_len) {
  1031. $max_opcode_len = $len;
  1032. }
  1033. if ($code > $max_opcode) {
  1034. $max_opcode = $code;
  1035. }
  1036. if (isset($opcodes[$code])) {
  1037. die("ERROR ($def:$lineno): Opcode with code '$code' is already defined.\n");
  1038. }
  1039. if (isset($opnames[$op])) {
  1040. die("ERROR ($def:$lineno): Opcode with name '$op' is already defined.\n");
  1041. }
  1042. $opcodes[$code] = array("op"=>$op,"op1"=>$op1,"op2"=>$op2,"code"=>"");
  1043. $opnames[$op] = $code;
  1044. $handler = $code;
  1045. $helper = null;
  1046. $list[$lineno] = array("handler"=>$handler);
  1047. } else if (strpos($line,"ZEND_VM_HELPER(") === 0) {
  1048. // Parsing helper's definition
  1049. if (preg_match(
  1050. "/^ZEND_VM_HELPER\(\s*([A-Za-z_]+)\s*,\s*([A-Z|]+)\s*,\s*([A-Z|]+)\s*\)/",
  1051. $line,
  1052. $m) == 0) {
  1053. die("ERROR ($def:$lineno): Invalid ZEND_VM_HELPER definition.\n");
  1054. }
  1055. $helper = $m[1];
  1056. $op1 = array_flip(explode("|",$m[2]));
  1057. $op2 = array_flip(explode("|",$m[3]));
  1058. if (isset($helpers[$helper])) {
  1059. die("ERROR ($def:$lineno): Helper with name '$helper' is already defined.\n");
  1060. }
  1061. $helpers[$helper] = array("op1"=>$op1,"op2"=>$op2,"param"=>null,"code"=>"");
  1062. $handler = null;
  1063. $list[$lineno] = array("helper"=>$helper);
  1064. } else if (strpos($line,"ZEND_VM_HELPER_EX(") === 0) {
  1065. // Parsing helper with parameter definition
  1066. if (preg_match(
  1067. "/^ZEND_VM_HELPER_EX\(\s*([A-Za-z_]+)\s*,\s*([A-Z|]+)\s*,\s*([A-Z|]+)\s*,\s*(.*)\s*\)/",
  1068. $line,
  1069. $m) == 0) {
  1070. die("ERROR ($def:$lineno): Invalid ZEND_VM_HELPER definition.\n");
  1071. }
  1072. $helper = $m[1];
  1073. $op1 = array_flip(explode("|",$m[2]));
  1074. $op2 = array_flip(explode("|",$m[3]));
  1075. $param = $m[4];
  1076. if (isset($helpers[$helper])) {
  1077. die("ERROR ($def:$lineno): Helper with name '$helper' is already defined.\n");
  1078. }
  1079. // Store parameter
  1080. $params[$param] = 1;
  1081. $helpers[$helper] = array("op1"=>$op1,"op2"=>$op2,"param"=>$param,"code"=>"");
  1082. $handler = null;
  1083. $list[$lineno] = array("helper"=>$helper);
  1084. } else if (strpos($line,"ZEND_VM_EXPORT_HANDLER(") === 0) {
  1085. if (preg_match(
  1086. "/^ZEND_VM_EXPORT_HANDLER\(\s*([A-Za-z_]+)\s*,\s*([A-Z_]+)\s*\)/",
  1087. $line,
  1088. $m) == 0) {
  1089. die("ERROR ($def:$lineno): Invalid ZEND_VM_EXPORT_HANDLER definition.\n");
  1090. }
  1091. if (!isset($opnames[$m[2]])) {
  1092. die("ERROR ($def:$lineno): opcode '{$m[2]}' is not defined.\n");
  1093. }
  1094. $export[] = array("handler",$m[1],$m[2]);
  1095. } else if (strpos($line,"ZEND_VM_EXPORT_HELPER(") === 0) {
  1096. if (preg_match(
  1097. "/^ZEND_VM_EXPORT_HELPER\(\s*([A-Za-z_]+)\s*,\s*([A-Za-z_]+)\s*\)/",
  1098. $line,
  1099. $m) == 0) {
  1100. die("ERROR ($def:$lineno): Invalid ZEND_VM_EXPORT_HELPER definition.\n");
  1101. }
  1102. if (!isset($helpers[$m[2]])) {
  1103. die("ERROR ($def:$lineno): helper '{$m[2]}' is not defined.\n");
  1104. }
  1105. $export[] = array("helper",$m[1],$m[2]);
  1106. } else if ($handler !== null) {
  1107. // Add line of code to current opcode handler
  1108. $opcodes[$handler]["code"] .= $line;
  1109. } else if ($helper !== null) {
  1110. // Add line of code to current helper
  1111. $helpers[$helper]["code"] .= $line;
  1112. }
  1113. }
  1114. ksort($opcodes);
  1115. // Search for opcode handlers those are used by other opcode handlers
  1116. foreach ($opcodes as $dsc) {
  1117. if (preg_match("/ZEND_VM_DISPATCH_TO_HANDLER\(\s*([A-Z_]*)\s*\)/m", $dsc["code"], $m)) {
  1118. $op = $m[1];
  1119. if (!isset($opnames[$op])) {
  1120. die("ERROR ($def:$lineno): Opcode with name '$op' is not defined.\n");
  1121. }
  1122. $code = $opnames[$op];
  1123. $opcodes[$code]['use'] = 1;
  1124. }
  1125. }
  1126. // Generate opcode #defines (zend_vm_opcodes.h)
  1127. $code_len = strlen((string)$max_opcode);
  1128. $f = fopen(__DIR__ . "/zend_vm_opcodes.h", "w+") or die("ERROR: Cannot create zend_vm_opcodes.h\n");
  1129. // Insert header
  1130. out($f, $GLOBALS['header_text']);
  1131. fputs($f, "#ifndef ZEND_VM_OPCODES_H\n#define ZEND_VM_OPCODES_H\n\n");
  1132. fputs($f, "BEGIN_EXTERN_C()\n\n");
  1133. fputs($f, "ZEND_API const char *zend_get_opcode_name(zend_uchar opcode);\n\n");
  1134. fputs($f, "END_EXTERN_C()\n\n");
  1135. foreach ($opcodes as $code => $dsc) {
  1136. $code = str_pad((string)$code,$code_len," ",STR_PAD_LEFT);
  1137. $op = str_pad($dsc["op"],$max_opcode_len);
  1138. fputs($f,"#define $op $code\n");
  1139. }
  1140. fputs($f, "\n#endif\n");
  1141. fclose($f);
  1142. echo "zend_vm_opcodes.h generated successfully.\n";
  1143. // zend_vm_opcodes.c
  1144. $f = fopen(__DIR__ . "/zend_vm_opcodes.c", "w+") or die("ERROR: Cannot create zend_vm_opcodes.c\n");
  1145. // Insert header
  1146. out($f, $GLOBALS['header_text']);
  1147. fputs($f,"#include <stdio.h>\n");
  1148. fputs($f,"#include <zend.h>\n\n");
  1149. fputs($f,"const char *zend_vm_opcodes_map[".($max_opcode + 1)."] = {\n");
  1150. for ($i = 0; $i <= $max_opcode; $i++) {
  1151. fputs($f,"\t".(isset($opcodes[$i]["op"])?'"'.$opcodes[$i]["op"].'"':"NULL").",\n");
  1152. }
  1153. fputs($f, "};\n\n");
  1154. fputs($f, "ZEND_API const char* zend_get_opcode_name(zend_uchar opcode) {\n");
  1155. fputs($f, "\treturn zend_vm_opcodes_map[opcode];\n");
  1156. fputs($f, "}\n");
  1157. fclose($f);
  1158. echo "zend_vm_opcodes.c generated successfully.\n";
  1159. // Generate zend_vm_execute.h
  1160. $f = fopen(__DIR__ . "/zend_vm_execute.h", "w+") or die("ERROR: Cannot create zend_vm_execute.h\n");
  1161. $executor_file = realpath(__DIR__ . "/zend_vm_execute.h");
  1162. // Insert header
  1163. out($f, $GLOBALS['header_text']);
  1164. out($f, "#ifdef ZEND_WIN32\n");
  1165. // Suppress free_op1 warnings on Windows
  1166. out($f, "# pragma warning(once : 4101)\n");
  1167. if (ZEND_VM_SPEC) {
  1168. // Suppress (<non-zero constant> || <expression>) warnings on windows
  1169. out($f, "# pragma warning(once : 6235)\n");
  1170. // Suppress (<zero> && <expression>) warnings on windows
  1171. out($f, "# pragma warning(once : 6237)\n");
  1172. // Suppress (<non-zero constant> && <expression>) warnings on windows
  1173. out($f, "# pragma warning(once : 6239)\n");
  1174. // Suppress (<expression> && <non-zero constant>) warnings on windows
  1175. out($f, "# pragma warning(once : 6240)\n");
  1176. // Suppress (<non-zero constant> || <non-zero constant>) warnings on windows
  1177. out($f, "# pragma warning(once : 6285)\n");
  1178. // Suppress (<non-zero constant> || <expression>) warnings on windows
  1179. out($f, "# pragma warning(once : 6286)\n");
  1180. // Suppress constant with constant comparison warnings on windows
  1181. out($f, "# pragma warning(once : 6326)\n");
  1182. }
  1183. out($f, "#endif\n");
  1184. // Support for ZEND_USER_OPCODE
  1185. out($f, "static user_opcode_handler_t zend_user_opcode_handlers[256] = {\n");
  1186. for ($i = 0; $i < 255; ++$i) {
  1187. out($f, "\t(user_opcode_handler_t)NULL,\n");
  1188. }
  1189. out($f, "\t(user_opcode_handler_t)NULL\n};\n\n");
  1190. out($f, "static zend_uchar zend_user_opcodes[256] = {");
  1191. for ($i = 0; $i < 255; ++$i) {
  1192. if ($i % 16 == 1) out($f, "\n\t");
  1193. out($f, "$i,");
  1194. }
  1195. out($f, "255\n};\n\n");
  1196. // Generate specialized executor
  1197. gen_executor($f, $skl, ZEND_VM_SPEC, ZEND_VM_KIND, "execute", "zend_init_opcodes_handlers", 0);
  1198. // Generate un-specialized executor
  1199. if (ZEND_VM_OLD_EXECUTOR) {
  1200. out($f,"\n/* Old executor */\n\n");
  1201. out($f,"#undef ZEND_VM_CONTINUE\n\n");
  1202. out($f,"#undef ZEND_VM_RETURN\n\n");
  1203. out($f,"#undef ZEND_VM_ENTER\n\n");
  1204. out($f,"#undef ZEND_VM_LEAVE\n\n");
  1205. out($f,"#undef ZEND_VM_DISPATCH\n\n");
  1206. out($f,"#undef ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_INTERNAL\n\n");
  1207. gen_executor($f, $skl, 0, ZEND_VM_KIND_CALL, "old_execute", "zend_vm_use_old_executor", 1);
  1208. }
  1209. // Generate zend_vm_get_opcode_handler() function
  1210. out($f, "static opcode_handler_t zend_vm_get_opcode_handler(zend_uchar opcode, zend_op* op)\n");
  1211. out($f, "{\n");
  1212. if (!ZEND_VM_SPEC) {
  1213. out($f, "\treturn zend_opcode_handlers[opcode];\n");
  1214. } else {
  1215. if (ZEND_VM_OLD_EXECUTOR) {
  1216. out($f, "\tif (zend_vm_old_executor) {\n");
  1217. out($f, "\t\treturn zend_opcode_handlers[opcode];\n");
  1218. out($f, "\t} else {\n");
  1219. }
  1220. out($f, "\t\tstatic const int zend_vm_decode[] = {\n");
  1221. out($f, "\t\t\t_UNUSED_CODE, /* 0 */\n");
  1222. out($f, "\t\t\t_CONST_CODE, /* 1 = IS_CONST */\n");
  1223. out($f, "\t\t\t_TMP_CODE, /* 2 = IS_TMP_VAR */\n");
  1224. out($f, "\t\t\t_UNUSED_CODE, /* 3 */\n");
  1225. out($f, "\t\t\t_VAR_CODE, /* 4 = IS_VAR */\n");
  1226. out($f, "\t\t\t_UNUSED_CODE, /* 5 */\n");
  1227. out($f, "\t\t\t_UNUSED_CODE, /* 6 */\n");
  1228. out($f, "\t\t\t_UNUSED_CODE, /* 7 */\n");
  1229. out($f, "\t\t\t_UNUSED_CODE, /* 8 = IS_UNUSED */\n");
  1230. out($f, "\t\t\t_UNUSED_CODE, /* 9 */\n");
  1231. out($f, "\t\t\t_UNUSED_CODE, /* 10 */\n");
  1232. out($f, "\t\t\t_UNUSED_CODE, /* 11 */\n");
  1233. out($f, "\t\t\t_UNUSED_CODE, /* 12 */\n");
  1234. out($f, "\t\t\t_UNUSED_CODE, /* 13 */\n");
  1235. out($f, "\t\t\t_UNUSED_CODE, /* 14 */\n");
  1236. out($f, "\t\t\t_UNUSED_CODE, /* 15 */\n");
  1237. out($f, "\t\t\t_CV_CODE /* 16 = IS_CV */\n");
  1238. out($f, "\t\t};\n");
  1239. out($f, "\t\treturn zend_opcode_handlers[opcode * 25 + zend_vm_decode[op->op1_type] * 5 + zend_vm_decode[op->op2_type]];\n");
  1240. if (ZEND_VM_OLD_EXECUTOR) {
  1241. out($f, "\t}\n");
  1242. }
  1243. }
  1244. out($f, "}\n\n");
  1245. // Generate zend_vm_get_opcode_handler() function
  1246. out($f, "ZEND_API void zend_vm_set_opcode_handler(zend_op* op)\n");
  1247. out($f, "{\n");
  1248. out($f, "\top->handler = zend_vm_get_opcode_handler(zend_user_opcodes[op->opcode], op);\n");
  1249. out($f, "}\n\n");
  1250. // Export handlers and helpers
  1251. if (count($export) > 0 &&
  1252. !ZEND_VM_OLD_EXECUTOR &&
  1253. ZEND_VM_KIND != ZEND_VM_KIND_CALL) {
  1254. out($f,"#undef OPLINE\n");
  1255. out($f,"#undef DCL_OPLINE\n");
  1256. out($f,"#undef USE_OPLINE\n");
  1257. out($f,"#undef LOAD_OPLINE\n");
  1258. out($f,"#undef SAVE_OPLINE\n");
  1259. out($f,"#define OPLINE EX(opline)\n");
  1260. out($f,"#define DCL_OPLINE\n");
  1261. out($f,"#define USE_OPLINE zend_op *opline = EX(opline);\n");
  1262. out($f,"#define LOAD_OPLINE()\n");
  1263. out($f,"#define SAVE_OPLINE()\n");
  1264. out($f,"#undef CHECK_EXCEPTION\n");
  1265. out($f,"#undef HANDLE_EXCEPTION\n");
  1266. out($f,"#undef HANDLE_EXCEPTION_LEAVE\n");
  1267. out($f,"#define CHECK_EXCEPTION() LOAD_OPLINE()\n");
  1268. out($f,"#define HANDLE_EXCEPTION() LOAD_OPLINE(); ZEND_VM_CONTINUE()\n");
  1269. out($f,"#define HANDLE_EXCEPTION_LEAVE() LOAD_OPLINE(); ZEND_VM_LEAVE()\n");
  1270. out($f,"#undef ZEND_VM_CONTINUE\n");
  1271. out($f,"#undef ZEND_VM_RETURN\n");
  1272. out($f,"#undef ZEND_VM_ENTER\n");
  1273. out($f,"#undef ZEND_VM_LEAVE\n");
  1274. out($f,"#undef ZEND_VM_DISPATCH\n");
  1275. out($f,"#undef ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_INTERNAL\n\n");
  1276. out($f,"#define ZEND_VM_CONTINUE() return 0\n");
  1277. out($f,"#define ZEND_VM_RETURN() return 1\n");
  1278. out($f,"#define ZEND_VM_ENTER() return 2\n");
  1279. out($f,"#define ZEND_VM_LEAVE() return 3\n");
  1280. out($f,"#define ZEND_VM_DISPATCH(opcode, opline) return zend_vm_get_opcode_handler(opcode, opline)(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);\n\n");
  1281. out($f,"#define ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_INTERNAL execute_data TSRMLS_CC\n\n");
  1282. }
  1283. foreach ($export as $dsk) {
  1284. list($kind, $func, $name) = $dsk;
  1285. out($f, "ZEND_API int $func(");
  1286. if ($kind == "handler") {
  1287. out($f, "ZEND_OPCODE_HANDLER_ARGS)\n");
  1288. $code = $opcodes[$opnames[$name]]['code'];
  1289. } else {
  1290. $h = $helpers[$name];
  1291. if ($h['param'] == null) {
  1292. out($f, "ZEND_OPCODE_HANDLER_ARGS)\n");
  1293. } else {
  1294. out($f, $h['param']. ", ZEND_OPCODE_HANDLER_ARGS)\n");
  1295. }
  1296. $code = $h['code'];
  1297. }
  1298. $done = 0;
  1299. if (ZEND_VM_OLD_EXECUTOR) {
  1300. if ($kind == "handler") {
  1301. out($f, "{\n\treturn ".$name."_HANDLER(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);\n}\n\n");
  1302. $done = 1;
  1303. } else if ($helpers[$name]["param"] == null) {
  1304. out($f, "{\n\treturn ".$name."(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);\n}\n\n");
  1305. $done = 1;
  1306. }
  1307. } else if (ZEND_VM_KIND == ZEND_VM_KIND_CALL) {
  1308. if ($kind == "handler") {
  1309. $op = $opcodes[$opnames[$name]];
  1310. if (isset($op['op1']["ANY"]) && isset($op['op2']["ANY"])) {
  1311. out($f, "{\n\treturn ".$name.(ZEND_VM_SPEC?"_SPEC":"")."_HANDLER(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);\n}\n\n");
  1312. $done = 1;
  1313. }
  1314. } else if ($helpers[$name]["param"] == null) {
  1315. $h = $helpers[$name];
  1316. if (isset($h['op1']["ANY"]) && isset($h['op2']["ANY"])) {
  1317. out($f, "{\n\treturn ".$name.(ZEND_VM_SPEC?"_SPEC":"")."(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);\n}\n\n");
  1318. $done = 1;
  1319. }
  1320. }
  1321. }
  1322. if (!$done) {
  1323. gen_code($f, 0, ZEND_VM_KIND_CALL, 1, $code, 'ANY', 'ANY', $name);
  1324. }
  1325. }
  1326. fclose($f);
  1327. echo "zend_vm_execute.h generated successfully.\n";
  1328. }
  1329. function usage() {
  1330. echo("\nUsage: php zend_vm_gen.php [options]\n".
  1331. "\nOptions:".
  1332. "\n --with-vm-kind=CALL|SWITCH|GOTO - select threading model (default is CALL)".
  1333. "\n --without-specializer - disable executor specialization".
  1334. "\n --with-old-executor - enable old executor".
  1335. "\n --with-lines - enable #line directives".
  1336. "\n\n");
  1337. }
  1338. // Parse arguments
  1339. for ($i = 1; $i < $argc; $i++) {
  1340. if (strpos($argv[$i],"--with-vm-kind=") === 0) {
  1341. $kind = substr($argv[$i], strlen("--with-vm-kind="));
  1342. switch ($kind) {
  1343. case "CALL":
  1344. define("ZEND_VM_KIND", ZEND_VM_KIND_CALL);
  1345. break;
  1346. case "SWITCH":
  1347. define("ZEND_VM_KIND", ZEND_VM_KIND_SWITCH);
  1348. break;
  1349. case "GOTO":
  1350. define("ZEND_VM_KIND", ZEND_VM_KIND_GOTO);
  1351. break;
  1352. default:
  1353. echo("ERROR: Invalid vm kind '$kind'\n");
  1354. usage();
  1355. die();
  1356. }
  1357. } else if ($argv[$i] == "--without-specializer") {
  1358. // Disabling specialization
  1359. define("ZEND_VM_SPEC", 0);
  1360. } else if ($argv[$i] == "--with-old-executor") {
  1361. // Disabling code for old-style executor
  1362. define("ZEND_VM_OLD_EXECUTOR", 1);
  1363. } else if ($argv[$i] == "--with-lines") {
  1364. // Enabling debugging using original zend_vm_def.h
  1365. define("ZEND_VM_LINES", 1);
  1366. } else if ($argv[$i] == "--help") {
  1367. usage();
  1368. exit();
  1369. } else {
  1370. echo("ERROR: Invalid option '".$argv[$i]."'\n");
  1371. usage();
  1372. die();
  1373. }
  1374. }
  1375. // Using defaults
  1376. if (!defined("ZEND_VM_KIND")) {
  1377. // Using CALL threading by default
  1378. define("ZEND_VM_KIND", ZEND_VM_KIND_CALL);
  1379. }
  1380. if (!defined("ZEND_VM_SPEC")) {
  1381. // Using specialized executor by default
  1382. define("ZEND_VM_SPEC", 1);
  1383. }
  1384. if (!defined("ZEND_VM_OLD_EXECUTOR")) {
  1385. // Include old-style executor by default
  1386. define("ZEND_VM_OLD_EXECUTOR", 0);
  1387. }
  1388. if (!defined("ZEND_VM_LINES")) {
  1389. // Disabling #line directives
  1390. define("ZEND_VM_LINES", 0);
  1391. }
  1392. gen_vm(__DIR__ . "/zend_vm_def.h", __DIR__ . "/zend_vm_execute.skl");
  1393. ?>