zend_vm_gen.php 138 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. +----------------------------------------------------------------------+
  5. | Zend Engine |
  6. +----------------------------------------------------------------------+
  7. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  8. +----------------------------------------------------------------------+
  9. | This source file is subject to version 2.00 of the Zend license, |
  10. | that is bundled with this package in the file LICENSE, and is |
  11. | available through the world-wide-web at the following url: |
  12. | http://www.zend.com/license/2_00.txt. |
  13. | If you did not receive a copy of the Zend license and are unable to |
  14. | obtain it through the world-wide-web, please send a note to |
  15. | license@zend.com so we can mail you a copy immediately. |
  16. +----------------------------------------------------------------------+
  17. | Authors: Dmitry Stogov <dmitry@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. const HEADER_TEXT = <<< DATA
  21. /*
  22. +----------------------------------------------------------------------+
  23. | Zend Engine |
  24. +----------------------------------------------------------------------+
  25. | Copyright (c) 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@php.net> |
  36. | Zeev Suraski <zeev@php.net> |
  37. | Dmitry Stogov <dmitry@php.net> |
  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. const ZEND_VM_KIND_CALL = 1;
  47. const ZEND_VM_KIND_SWITCH = 2;
  48. const ZEND_VM_KIND_GOTO = 3;
  49. const ZEND_VM_KIND_HYBRID = 4;
  50. $vm_op_flags = array(
  51. "ZEND_VM_OP_SPEC" => 1<<0,
  52. "ZEND_VM_OP_CONST" => 1<<1,
  53. "ZEND_VM_OP_TMPVAR" => 1<<2,
  54. "ZEND_VM_OP_TMPVARCV" => 1<<3,
  55. "ZEND_VM_OP_MASK" => 0xf0,
  56. "ZEND_VM_OP_NUM" => 0x10,
  57. "ZEND_VM_OP_JMP_ADDR" => 0x20,
  58. "ZEND_VM_OP_TRY_CATCH" => 0x30,
  59. // unused 0x40
  60. "ZEND_VM_OP_THIS" => 0x50,
  61. "ZEND_VM_OP_NEXT" => 0x60,
  62. "ZEND_VM_OP_CLASS_FETCH" => 0x70,
  63. "ZEND_VM_OP_CONSTRUCTOR" => 0x80,
  64. "ZEND_VM_OP_CONST_FETCH" => 0x90,
  65. "ZEND_VM_OP_CACHE_SLOT" => 0xa0,
  66. "ZEND_VM_EXT_VAR_FETCH" => 1<<16,
  67. "ZEND_VM_EXT_ISSET" => 1<<17,
  68. "ZEND_VM_EXT_CACHE_SLOT" => 1<<18,
  69. "ZEND_VM_EXT_ARRAY_INIT" => 1<<19,
  70. "ZEND_VM_EXT_REF" => 1<<20,
  71. "ZEND_VM_EXT_FETCH_REF" => 1<<21,
  72. "ZEND_VM_EXT_DIM_WRITE" => 1<<22,
  73. "ZEND_VM_EXT_MASK" => 0x0f000000,
  74. "ZEND_VM_EXT_NUM" => 0x01000000,
  75. "ZEND_VM_EXT_LAST_CATCH" => 0x02000000,
  76. "ZEND_VM_EXT_JMP_ADDR" => 0x03000000,
  77. "ZEND_VM_EXT_OP" => 0x04000000,
  78. // unused 0x5000000
  79. // unused 0x6000000
  80. "ZEND_VM_EXT_TYPE" => 0x07000000,
  81. "ZEND_VM_EXT_EVAL" => 0x08000000,
  82. "ZEND_VM_EXT_TYPE_MASK" => 0x09000000,
  83. // unused 0x0a000000,
  84. "ZEND_VM_EXT_SRC" => 0x0b000000,
  85. // unused 0x0c000000,
  86. "ZEND_VM_NO_CONST_CONST" => 0x40000000,
  87. "ZEND_VM_COMMUTATIVE" => 0x80000000,
  88. );
  89. foreach ($vm_op_flags as $name => $val) {
  90. define($name, $val);
  91. }
  92. $vm_op_decode = array(
  93. "ANY" => 0,
  94. "CONST" => ZEND_VM_OP_SPEC | ZEND_VM_OP_CONST,
  95. "TMP" => ZEND_VM_OP_SPEC,
  96. "VAR" => ZEND_VM_OP_SPEC,
  97. "UNUSED" => ZEND_VM_OP_SPEC,
  98. "CV" => ZEND_VM_OP_SPEC,
  99. "TMPVAR" => ZEND_VM_OP_SPEC | ZEND_VM_OP_TMPVAR,
  100. "TMPVARCV" => ZEND_VM_OP_SPEC | ZEND_VM_OP_TMPVARCV,
  101. "NUM" => ZEND_VM_OP_NUM,
  102. "JMP_ADDR" => ZEND_VM_OP_JMP_ADDR,
  103. "TRY_CATCH" => ZEND_VM_OP_TRY_CATCH,
  104. "THIS" => ZEND_VM_OP_THIS,
  105. "NEXT" => ZEND_VM_OP_NEXT,
  106. "CLASS_FETCH" => ZEND_VM_OP_CLASS_FETCH,
  107. "CONSTRUCTOR" => ZEND_VM_OP_CONSTRUCTOR,
  108. "CONST_FETCH" => ZEND_VM_OP_CONST_FETCH,
  109. "CACHE_SLOT" => ZEND_VM_OP_CACHE_SLOT,
  110. );
  111. $vm_ext_decode = array(
  112. "NUM" => ZEND_VM_EXT_NUM,
  113. "LAST_CATCH" => ZEND_VM_EXT_LAST_CATCH,
  114. "JMP_ADDR" => ZEND_VM_EXT_JMP_ADDR,
  115. "OP" => ZEND_VM_EXT_OP,
  116. "VAR_FETCH" => ZEND_VM_EXT_VAR_FETCH,
  117. "ARRAY_INIT" => ZEND_VM_EXT_ARRAY_INIT,
  118. "TYPE" => ZEND_VM_EXT_TYPE,
  119. "EVAL" => ZEND_VM_EXT_EVAL,
  120. "TYPE_MASK" => ZEND_VM_EXT_TYPE_MASK,
  121. "ISSET" => ZEND_VM_EXT_ISSET,
  122. "REF" => ZEND_VM_EXT_REF,
  123. "FETCH_REF" => ZEND_VM_EXT_FETCH_REF,
  124. "SRC" => ZEND_VM_EXT_SRC,
  125. "CACHE_SLOT" => ZEND_VM_EXT_CACHE_SLOT,
  126. "DIM_WRITE" => ZEND_VM_EXT_DIM_WRITE,
  127. );
  128. $vm_kind_name = array(
  129. ZEND_VM_KIND_CALL => "ZEND_VM_KIND_CALL",
  130. ZEND_VM_KIND_SWITCH => "ZEND_VM_KIND_SWITCH",
  131. ZEND_VM_KIND_GOTO => "ZEND_VM_KIND_GOTO",
  132. ZEND_VM_KIND_HYBRID => "ZEND_VM_KIND_HYBRID",
  133. );
  134. $op_types = array(
  135. "ANY",
  136. "CONST",
  137. "TMP",
  138. "VAR",
  139. "UNUSED",
  140. "CV",
  141. );
  142. $op_types_ex = array(
  143. "ANY",
  144. "CONST",
  145. "TMPVARCV",
  146. "TMPVAR",
  147. "TMP",
  148. "VAR",
  149. "UNUSED",
  150. "CV",
  151. );
  152. $prefix = array(
  153. "ANY" => "",
  154. "TMP" => "_TMP",
  155. "VAR" => "_VAR",
  156. "CONST" => "_CONST",
  157. "UNUSED" => "_UNUSED",
  158. "CV" => "_CV",
  159. "TMPVAR" => "_TMPVAR",
  160. "TMPVARCV" => "_TMPVARCV",
  161. );
  162. $commutative_order = array(
  163. "ANY" => 0,
  164. "TMP" => 1,
  165. "VAR" => 2,
  166. "CONST" => 0,
  167. "UNUSED" => 0,
  168. "CV" => 4,
  169. "TMPVAR" => 2,
  170. "TMPVARCV" => 4,
  171. );
  172. $op1_type = array(
  173. "ANY" => "opline->op1_type",
  174. "TMP" => "IS_TMP_VAR",
  175. "VAR" => "IS_VAR",
  176. "CONST" => "IS_CONST",
  177. "UNUSED" => "IS_UNUSED",
  178. "CV" => "IS_CV",
  179. "TMPVAR" => "(IS_TMP_VAR|IS_VAR)",
  180. "TMPVARCV" => "(IS_TMP_VAR|IS_VAR|IS_CV)",
  181. );
  182. $op2_type = array(
  183. "ANY" => "opline->op2_type",
  184. "TMP" => "IS_TMP_VAR",
  185. "VAR" => "IS_VAR",
  186. "CONST" => "IS_CONST",
  187. "UNUSED" => "IS_UNUSED",
  188. "CV" => "IS_CV",
  189. "TMPVAR" => "(IS_TMP_VAR|IS_VAR)",
  190. "TMPVARCV" => "(IS_TMP_VAR|IS_VAR|IS_CV)",
  191. );
  192. $op1_get_zval_ptr = array(
  193. "ANY" => "get_zval_ptr(opline->op1_type, opline->op1, \\1)",
  194. "TMP" => "_get_zval_ptr_tmp(opline->op1.var EXECUTE_DATA_CC)",
  195. "VAR" => "_get_zval_ptr_var(opline->op1.var EXECUTE_DATA_CC)",
  196. "CONST" => "RT_CONSTANT(opline, opline->op1)",
  197. "UNUSED" => "NULL",
  198. "CV" => "_get_zval_ptr_cv_\\1(opline->op1.var EXECUTE_DATA_CC)",
  199. "TMPVAR" => "_get_zval_ptr_var(opline->op1.var EXECUTE_DATA_CC)",
  200. "TMPVARCV" => "???",
  201. );
  202. $op2_get_zval_ptr = array(
  203. "ANY" => "get_zval_ptr(opline->op2_type, opline->op2, \\1)",
  204. "TMP" => "_get_zval_ptr_tmp(opline->op2.var EXECUTE_DATA_CC)",
  205. "VAR" => "_get_zval_ptr_var(opline->op2.var EXECUTE_DATA_CC)",
  206. "CONST" => "RT_CONSTANT(opline, opline->op2)",
  207. "UNUSED" => "NULL",
  208. "CV" => "_get_zval_ptr_cv_\\1(opline->op2.var EXECUTE_DATA_CC)",
  209. "TMPVAR" => "_get_zval_ptr_var(opline->op2.var EXECUTE_DATA_CC)",
  210. "TMPVARCV" => "???",
  211. );
  212. $op1_get_zval_ptr_ptr = array(
  213. "ANY" => "get_zval_ptr_ptr(opline->op1_type, opline->op1, \\1)",
  214. "TMP" => "NULL",
  215. "VAR" => "_get_zval_ptr_ptr_var(opline->op1.var EXECUTE_DATA_CC)",
  216. "CONST" => "NULL",
  217. "UNUSED" => "NULL",
  218. "CV" => "_get_zval_ptr_cv_\\1(opline->op1.var EXECUTE_DATA_CC)",
  219. "TMPVAR" => "???",
  220. "TMPVARCV" => "???",
  221. );
  222. $op2_get_zval_ptr_ptr = array(
  223. "ANY" => "get_zval_ptr_ptr(opline->op2_type, opline->op2, \\1)",
  224. "TMP" => "NULL",
  225. "VAR" => "_get_zval_ptr_ptr_var(opline->op2.var EXECUTE_DATA_CC)",
  226. "CONST" => "NULL",
  227. "UNUSED" => "NULL",
  228. "CV" => "_get_zval_ptr_cv_\\1(opline->op2.var EXECUTE_DATA_CC)",
  229. "TMPVAR" => "???",
  230. "TMPVARCV" => "???",
  231. );
  232. $op1_get_zval_ptr_deref = array(
  233. "ANY" => "get_zval_ptr_deref(opline->op1_type, opline->op1, \\1)",
  234. "TMP" => "_get_zval_ptr_tmp(opline->op1.var EXECUTE_DATA_CC)",
  235. "VAR" => "_get_zval_ptr_var_deref(opline->op1.var EXECUTE_DATA_CC)",
  236. "CONST" => "RT_CONSTANT(opline, opline->op1)",
  237. "UNUSED" => "NULL",
  238. "CV" => "_get_zval_ptr_cv_deref_\\1(opline->op1.var EXECUTE_DATA_CC)",
  239. "TMPVAR" => "???",
  240. "TMPVARCV" => "???",
  241. );
  242. $op2_get_zval_ptr_deref = array(
  243. "ANY" => "get_zval_ptr_deref(opline->op2_type, opline->op2, \\1)",
  244. "TMP" => "_get_zval_ptr_tmp(opline->op2.var EXECUTE_DATA_CC)",
  245. "VAR" => "_get_zval_ptr_var_deref(opline->op2.var EXECUTE_DATA_CC)",
  246. "CONST" => "RT_CONSTANT(opline, opline->op2)",
  247. "UNUSED" => "NULL",
  248. "CV" => "_get_zval_ptr_cv_deref_\\1(opline->op2.var EXECUTE_DATA_CC)",
  249. "TMPVAR" => "???",
  250. "TMPVARCV" => "???",
  251. );
  252. $op1_get_zval_ptr_undef = array(
  253. "ANY" => "get_zval_ptr_undef(opline->op1_type, opline->op1, \\1)",
  254. "TMP" => "_get_zval_ptr_tmp(opline->op1.var EXECUTE_DATA_CC)",
  255. "VAR" => "_get_zval_ptr_var(opline->op1.var EXECUTE_DATA_CC)",
  256. "CONST" => "RT_CONSTANT(opline, opline->op1)",
  257. "UNUSED" => "NULL",
  258. "CV" => "EX_VAR(opline->op1.var)",
  259. "TMPVAR" => "_get_zval_ptr_var(opline->op1.var EXECUTE_DATA_CC)",
  260. "TMPVARCV" => "EX_VAR(opline->op1.var)",
  261. );
  262. $op2_get_zval_ptr_undef = array(
  263. "ANY" => "get_zval_ptr_undef(opline->op2_type, opline->op2, \\1)",
  264. "TMP" => "_get_zval_ptr_tmp(opline->op2.var EXECUTE_DATA_CC)",
  265. "VAR" => "_get_zval_ptr_var(opline->op2.var EXECUTE_DATA_CC)",
  266. "CONST" => "RT_CONSTANT(opline, opline->op2)",
  267. "UNUSED" => "NULL",
  268. "CV" => "EX_VAR(opline->op2.var)",
  269. "TMPVAR" => "_get_zval_ptr_var(opline->op2.var EXECUTE_DATA_CC)",
  270. "TMPVARCV" => "EX_VAR(opline->op2.var)",
  271. );
  272. $op1_get_zval_ptr_ptr_undef = array(
  273. "ANY" => "get_zval_ptr_ptr_undef(opline->op1_type, opline->op1, \\1)",
  274. "TMP" => "NULL",
  275. "VAR" => "_get_zval_ptr_ptr_var(opline->op1.var EXECUTE_DATA_CC)",
  276. "CONST" => "NULL",
  277. "UNUSED" => "NULL",
  278. "CV" => "EX_VAR(opline->op1.var)",
  279. "TMPVAR" => "???",
  280. "TMPVARCV" => "???",
  281. );
  282. $op2_get_zval_ptr_ptr_undef = array(
  283. "ANY" => "get_zval_ptr_ptr_undef(opline->op2_type, opline->op2, \\1)",
  284. "TMP" => "NULL",
  285. "VAR" => "_get_zval_ptr_ptr_var(opline->op2.var EXECUTE_DATA_CC)",
  286. "CONST" => "NULL",
  287. "UNUSED" => "NULL",
  288. "CV" => "EX_VAR(opline->op2.var)",
  289. "TMPVAR" => "???",
  290. "TMPVARCV" => "???",
  291. );
  292. $op1_get_obj_zval_ptr = array(
  293. "ANY" => "get_obj_zval_ptr(opline->op1_type, opline->op1, \\1)",
  294. "TMP" => "_get_zval_ptr_tmp(opline->op1.var EXECUTE_DATA_CC)",
  295. "VAR" => "_get_zval_ptr_var(opline->op1.var EXECUTE_DATA_CC)",
  296. "CONST" => "RT_CONSTANT(opline, opline->op1)",
  297. "UNUSED" => "&EX(This)",
  298. "CV" => "_get_zval_ptr_cv_\\1(opline->op1.var EXECUTE_DATA_CC)",
  299. "TMPVAR" => "_get_zval_ptr_var(opline->op1.var EXECUTE_DATA_CC)",
  300. "TMPVARCV" => "???",
  301. );
  302. $op2_get_obj_zval_ptr = array(
  303. "ANY" => "get_obj_zval_ptr(opline->op2_type, opline->op2, \\1)",
  304. "TMP" => "_get_zval_ptr_tmp(opline->op2.var EXECUTE_DATA_CC)",
  305. "VAR" => "_get_zval_ptr_var(opline->op2.var EXECUTE_DATA_CC)",
  306. "CONST" => "RT_CONSTANT(opline, opline->op2)",
  307. "UNUSED" => "&EX(This)",
  308. "CV" => "_get_zval_ptr_cv_\\1(opline->op2.var EXECUTE_DATA_CC)",
  309. "TMPVAR" => "_get_zval_ptr_var(opline->op2.var EXECUTE_DATA_CC)",
  310. "TMPVARCV" => "???",
  311. );
  312. $op1_get_obj_zval_ptr_undef = array(
  313. "ANY" => "get_obj_zval_ptr_undef(opline->op1_type, opline->op1, \\1)",
  314. "TMP" => "_get_zval_ptr_tmp(opline->op1.var EXECUTE_DATA_CC)",
  315. "VAR" => "_get_zval_ptr_var(opline->op1.var EXECUTE_DATA_CC)",
  316. "CONST" => "RT_CONSTANT(opline, opline->op1)",
  317. "UNUSED" => "&EX(This)",
  318. "CV" => "EX_VAR(opline->op1.var)",
  319. "TMPVAR" => "_get_zval_ptr_var(opline->op1.var EXECUTE_DATA_CC)",
  320. "TMPVARCV" => "EX_VAR(opline->op1.var)",
  321. );
  322. $op2_get_obj_zval_ptr_undef = array(
  323. "ANY" => "get_obj_zval_ptr_undef(opline->op2_type, opline->op2, \\1)",
  324. "TMP" => "_get_zval_ptr_tmp(opline->op2.var EXECUTE_DATA_CC)",
  325. "VAR" => "_get_zval_ptr_var(opline->op2.var EXECUTE_DATA_CC)",
  326. "CONST" => "RT_CONSTANT(opline, opline->op2)",
  327. "UNUSED" => "&EX(This)",
  328. "CV" => "EX_VAR(opline->op2.var)",
  329. "TMPVAR" => "_get_zval_ptr_var(opline->op2.var EXECUTE_DATA_CC)",
  330. "TMPVARCV" => "EX_VAR(opline->op2.var)",
  331. );
  332. $op1_get_obj_zval_ptr_deref = array(
  333. "ANY" => "get_obj_zval_ptr(opline->op1_type, opline->op1, \\1)",
  334. "TMP" => "_get_zval_ptr_tmp(opline->op1.var EXECUTE_DATA_CC)",
  335. "VAR" => "_get_zval_ptr_var_deref(opline->op1.var EXECUTE_DATA_CC)",
  336. "CONST" => "RT_CONSTANT(opline, opline->op1)",
  337. "UNUSED" => "&EX(This)",
  338. "CV" => "_get_zval_ptr_cv_deref_\\1(opline->op1.var EXECUTE_DATA_CC)",
  339. "TMPVAR" => "???",
  340. "TMPVARCV" => "???",
  341. );
  342. $op2_get_obj_zval_ptr_deref = array(
  343. "ANY" => "get_obj_zval_ptr(opline->op2_type, opline->op2, \\1)",
  344. "TMP" => "_get_zval_ptr_tmp(opline->op2.var EXECUTE_DATA_CC)",
  345. "VAR" => "_get_zval_ptr_var_deref(opline->op2.var EXECUTE_DATA_CC)",
  346. "CONST" => "RT_CONSTANT(opline, opline->op2)",
  347. "UNUSED" => "&EX(This)",
  348. "CV" => "_get_zval_ptr_cv_deref_\\1(opline->op2.var EXECUTE_DATA_CC)",
  349. "TMPVAR" => "???",
  350. "TMPVARCV" => "???",
  351. );
  352. $op1_get_obj_zval_ptr_ptr = array(
  353. "ANY" => "get_obj_zval_ptr_ptr(opline->op1_type, opline->op1, \\1)",
  354. "TMP" => "NULL",
  355. "VAR" => "_get_zval_ptr_ptr_var(opline->op1.var EXECUTE_DATA_CC)",
  356. "CONST" => "NULL",
  357. "UNUSED" => "&EX(This)",
  358. "CV" => "_get_zval_ptr_cv_\\1(opline->op1.var EXECUTE_DATA_CC)",
  359. "TMPVAR" => "???",
  360. "TMPVARCV" => "???",
  361. );
  362. $op2_get_obj_zval_ptr_ptr = array(
  363. "ANY" => "get_obj_zval_ptr_ptr(opline->op2_type, opline->op2, \\1)",
  364. "TMP" => "NULL",
  365. "VAR" => "_get_zval_ptr_ptr_var(opline->op2.var EXECUTE_DATA_CC)",
  366. "CONST" => "NULL",
  367. "UNUSED" => "&EX(This)",
  368. "CV" => "_get_zval_ptr_cv_\\1(opline->op2.var EXECUTE_DATA_CC)",
  369. "TMPVAR" => "???",
  370. "TMPVARCV" => "???",
  371. );
  372. $op1_get_obj_zval_ptr_ptr_undef = array(
  373. "ANY" => "get_obj_zval_ptr_ptr(opline->op1_type, opline->op1, \\1)",
  374. "TMP" => "NULL",
  375. "VAR" => "_get_zval_ptr_ptr_var(opline->op1.var EXECUTE_DATA_CC)",
  376. "CONST" => "NULL",
  377. "UNUSED" => "&EX(This)",
  378. "CV" => "EX_VAR(opline->op1.var)",
  379. "TMPVAR" => "???",
  380. "TMPVARCV" => "???",
  381. );
  382. $op2_get_obj_zval_ptr_ptr_undef = array(
  383. "ANY" => "get_obj_zval_ptr_ptr(opline->op2_type, opline->op2, \\1)",
  384. "TMP" => "NULL",
  385. "VAR" => "_get_zval_ptr_ptr_var(opline->op2.var EXECUTE_DATA_CC)",
  386. "CONST" => "NULL",
  387. "UNUSED" => "&EX(This)",
  388. "CV" => "EX_VAR(opline->op2.var)",
  389. "TMPVAR" => "???",
  390. "TMPVARCV" => "???",
  391. );
  392. $op1_free_op = array(
  393. "ANY" => "FREE_OP(opline->op1_type, opline->op1.var)",
  394. "TMP" => "zval_ptr_dtor_nogc(EX_VAR(opline->op1.var))",
  395. "VAR" => "zval_ptr_dtor_nogc(EX_VAR(opline->op1.var))",
  396. "CONST" => "",
  397. "UNUSED" => "",
  398. "CV" => "",
  399. "TMPVAR" => "zval_ptr_dtor_nogc(EX_VAR(opline->op1.var))",
  400. "TMPVARCV" => "???",
  401. );
  402. $op2_free_op = array(
  403. "ANY" => "FREE_OP(opline->op2_type, opline->op2.var)",
  404. "TMP" => "zval_ptr_dtor_nogc(EX_VAR(opline->op2.var))",
  405. "VAR" => "zval_ptr_dtor_nogc(EX_VAR(opline->op2.var))",
  406. "CONST" => "",
  407. "UNUSED" => "",
  408. "CV" => "",
  409. "TMPVAR" => "zval_ptr_dtor_nogc(EX_VAR(opline->op2.var))",
  410. "TMPVARCV" => "???",
  411. );
  412. $op1_free_op_if_var = array(
  413. "ANY" => "if (opline->op1_type == IS_VAR) {zval_ptr_dtor_nogc(EX_VAR(opline->op1.var));}",
  414. "TMP" => "",
  415. "VAR" => "zval_ptr_dtor_nogc(EX_VAR(opline->op1.var))",
  416. "CONST" => "",
  417. "UNUSED" => "",
  418. "CV" => "",
  419. "TMPVAR" => "???",
  420. "TMPVARCV" => "???",
  421. );
  422. $op2_free_op_if_var = array(
  423. "ANY" => "if (opline->op2_type == IS_VAR) {zval_ptr_dtor_nogc(EX_VAR(opline->op2.var));}",
  424. "TMP" => "",
  425. "VAR" => "zval_ptr_dtor_nogc(EX_VAR(opline->op2.var))",
  426. "CONST" => "",
  427. "UNUSED" => "",
  428. "CV" => "",
  429. "TMPVAR" => "???",
  430. "TMPVARCV" => "???",
  431. );
  432. $op1_free_op_var_ptr = array(
  433. "ANY" => "if (opline->op1_type == IS_VAR) {zval_ptr_dtor_nogc(EX_VAR(opline->op1.var));}",
  434. "TMP" => "",
  435. "VAR" => "zval_ptr_dtor_nogc(EX_VAR(opline->op1.var))",
  436. "CONST" => "",
  437. "UNUSED" => "",
  438. "CV" => "",
  439. "TMPVAR" => "???",
  440. "TMPVARCV" => "???",
  441. );
  442. $op2_free_op_var_ptr = array(
  443. "ANY" => "if (opline->op2_type == IS_VAR) {zval_ptr_dtor_nogc(EX_VAR(opline->op2.var));}",
  444. "TMP" => "",
  445. "VAR" => "zval_ptr_dtor_nogc(EX_VAR(opline->op2.var))",
  446. "CONST" => "",
  447. "UNUSED" => "",
  448. "CV" => "",
  449. "TMPVAR" => "???",
  450. "TMPVARCV" => "???",
  451. );
  452. $op_data_type = array(
  453. "ANY" => "(opline+1)->op1_type",
  454. "TMP" => "IS_TMP_VAR",
  455. "VAR" => "IS_VAR",
  456. "CONST" => "IS_CONST",
  457. "UNUSED" => "IS_UNUSED",
  458. "CV" => "IS_CV",
  459. "TMPVAR" => "(IS_TMP_VAR|IS_VAR)",
  460. "TMPVARCV" => "(IS_TMP_VAR|IS_VAR|IS_CV)",
  461. );
  462. $op_data_get_zval_ptr = array(
  463. "ANY" => "get_op_data_zval_ptr_r((opline+1)->op1_type, (opline+1)->op1)",
  464. "TMP" => "_get_zval_ptr_tmp((opline+1)->op1.var EXECUTE_DATA_CC)",
  465. "VAR" => "_get_zval_ptr_var((opline+1)->op1.var EXECUTE_DATA_CC)",
  466. "CONST" => "RT_CONSTANT((opline+1), (opline+1)->op1)",
  467. "UNUSED" => "NULL",
  468. "CV" => "_get_zval_ptr_cv_\\1((opline+1)->op1.var EXECUTE_DATA_CC)",
  469. "TMPVAR" => "_get_zval_ptr_var((opline+1)->op1.var EXECUTE_DATA_CC)",
  470. "TMPVARCV" => "???",
  471. );
  472. $op_data_get_zval_ptr_undef = array(
  473. "ANY" => "get_op_data_zval_ptr_undef((opline+1)->op1_type, (opline+1)->op1)",
  474. "TMP" => "_get_zval_ptr_tmp((opline+1)->op1.var EXECUTE_DATA_CC)",
  475. "VAR" => "_get_zval_ptr_var((opline+1)->op1.var EXECUTE_DATA_CC)",
  476. "CONST" => "RT_CONSTANT((opline+1), (opline+1)->op1)",
  477. "UNUSED" => "NULL",
  478. "CV" => "EX_VAR((opline+1)->op1.var)",
  479. "TMPVAR" => "_get_zval_ptr_var((opline+1)->op1.var EXECUTE_DATA_CC)",
  480. "TMPVARCV" => "EX_VAR((opline+1)->op1.var)",
  481. );
  482. $op_data_get_zval_ptr_deref = array(
  483. "ANY" => "get_op_data_zval_ptr_deref_r((opline+1)->op1_type, (opline+1)->op1)",
  484. "TMP" => "_get_zval_ptr_tmp((opline+1)->op1.var EXECUTE_DATA_CC)",
  485. "VAR" => "_get_zval_ptr_var_deref((opline+1)->op1.var EXECUTE_DATA_CC)",
  486. "CONST" => "RT_CONSTANT((opline+1), (opline+1)->op1)",
  487. "UNUSED" => "NULL",
  488. "CV" => "_get_zval_ptr_cv_deref_\\1((opline+1)->op1.var EXECUTE_DATA_CC)",
  489. "TMPVAR" => "???",
  490. "TMPVARCV" => "???",
  491. );
  492. $op_data_get_zval_ptr_ptr = array(
  493. "ANY" => "get_zval_ptr_ptr((opline+1)->op1_type, (opline+1)->op1, \\1)",
  494. "TMP" => "NULL",
  495. "VAR" => "_get_zval_ptr_ptr_var((opline+1)->op1.var EXECUTE_DATA_CC)",
  496. "CONST" => "NULL",
  497. "UNUSED" => "NULL",
  498. "CV" => "_get_zval_ptr_cv_\\1((opline+1)->op1.var EXECUTE_DATA_CC)",
  499. "TMPVAR" => "???",
  500. "TMPVARCV" => "???",
  501. );
  502. $op_data_free_op = array(
  503. "ANY" => "FREE_OP((opline+1)->op1_type, (opline+1)->op1.var)",
  504. "TMP" => "zval_ptr_dtor_nogc(EX_VAR((opline+1)->op1.var))",
  505. "VAR" => "zval_ptr_dtor_nogc(EX_VAR((opline+1)->op1.var))",
  506. "CONST" => "",
  507. "UNUSED" => "",
  508. "CV" => "",
  509. "TMPVAR" => "zval_ptr_dtor_nogc(EX_VAR((opline+1)->op1.var))",
  510. "TMPVARCV" => "???",
  511. );
  512. $op_data_free_op_var_ptr = array(
  513. "ANY" => "if ((opline+1)->op1_type == IS_VAR) {zval_ptr_dtor_nogc(EX_VAR((opline+1)->op1.var));}",
  514. "TMP" => "",
  515. "VAR" => "zval_ptr_dtor_nogc(EX_VAR((opline+1)->op1.var));",
  516. "CONST" => "",
  517. "UNUSED" => "",
  518. "CV" => "",
  519. "TMPVAR" => "???",
  520. "TMPVARCV" => "???",
  521. );
  522. $list = array(); // list of opcode handlers and helpers in original order
  523. $opcodes = array(); // opcode handlers by code
  524. $helpers = array(); // opcode helpers by name
  525. $params = array(); // parameters of helpers
  526. $opnames = array(); // opcode name to code mapping
  527. $line_no = 1;
  528. $used_extra_spec = array();
  529. // Writes $s into resulting executor
  530. function out($f, $s) {
  531. global $line_no;
  532. fputs($f,$s);
  533. $line_no += substr_count($s, "\n");
  534. }
  535. // Resets #line directives in resulting executor
  536. function out_line($f) {
  537. global $line_no, $executor_file;
  538. fputs($f,"#line ".($line_no+1)." \"".$executor_file."\"\n");
  539. ++$line_no;
  540. }
  541. function is_hot_helper($name) {
  542. global $helpers;
  543. if (isset($helpers[$name]["hot"])) {
  544. return $helpers[$name]["hot"];
  545. }
  546. return false;
  547. }
  548. // Returns name of specialized helper
  549. function helper_name($name, $spec, $op1, $op2, $extra_spec) {
  550. global $prefix, $helpers;
  551. $extra = "";
  552. if (isset($helpers[$name])) {
  553. // If we have no helper with specified specialized operands then
  554. // using unspecialized helper
  555. if (!isset($helpers[$name]["op1"][$op1])) {
  556. if (($op1 == 'TMP' || $op1 == 'VAR') &&
  557. isset($helpers[$name]["op1"]["TMPVAR"])) {
  558. $op1 = "TMPVAR";
  559. } else if (($op1 == 'TMP' || $op1 == 'VAR') &&
  560. isset($helpers[$name]["op1"]["TMPVARCV"])) {
  561. $op1 = "TMPVARCV";
  562. } else if ($op1 == 'CV' &&
  563. isset($helpers[$name]["op1"]["TMPVARCV"])) {
  564. $op1 = "TMPVARCV";
  565. } else if (isset($helpers[$name]["op1"]["ANY"])) {
  566. $op1 = "ANY";
  567. }
  568. }
  569. if (!isset($helpers[$name]["op2"][$op2])) {
  570. if (($op2 == 'TMP' || $op2 == 'VAR') &&
  571. isset($helpers[$name]["op2"]["TMPVAR"])) {
  572. $op2 = "TMPVAR";
  573. } else if (($op2 == 'TMP' || $op2 == 'VAR') &&
  574. isset($helpers[$name]["op2"]["TMPVARCV"])) {
  575. $op2 = "TMPVARCV";
  576. } else if ($op2 == 'CV' &&
  577. isset($helpers[$name]["op2"]["TMPVARCV"])) {
  578. $op2 = "TMPVARCV";
  579. } else if (isset($helpers[$name]["op2"]["ANY"])) {
  580. $op2 = "ANY";
  581. }
  582. }
  583. /* forward common specs (e.g. in ZEND_VM_DISPATCH_TO_HELPER) */
  584. if (isset($extra_spec, $helpers[$name]["spec"])) {
  585. $extra = extra_spec_name(array_intersect_key($extra_spec, $helpers[$name]["spec"]));
  586. }
  587. }
  588. return $name . ($spec ? "_SPEC" : "") . $prefix[$op1] . $prefix[$op2] . $extra;
  589. }
  590. function opcode_name($name, $spec, $op1, $op2, $extra_spec) {
  591. global $prefix, $opnames, $opcodes;
  592. $extra = "";
  593. if (isset($opnames[$name])) {
  594. $opcode = $opcodes[$opnames[$name]];
  595. // If we have no helper with specified specialized operands then
  596. // using unspecialized helper
  597. if (!isset($opcode["op1"][$op1])) {
  598. if (($op1 == 'TMP' || $op1 == 'VAR') &&
  599. isset($opcode["op1"]["TMPVAR"])) {
  600. $op1 = "TMPVAR";
  601. } else if (($op1 == 'TMP' || $op1 == 'VAR') &&
  602. isset($opcode["op1"]["TMPVARCV"])) {
  603. $op1 = "TMPVARCV";
  604. } else if ($op1 == 'CV' &&
  605. isset($opcode["op1"]["TMPVARCV"])) {
  606. $op1 = "TMPVARCV";
  607. } else if (isset($opcode["op1"]["ANY"])) {
  608. $op1 = "ANY";
  609. } else if ($spec) {
  610. /* dispatch to invalid handler from unreachable code */
  611. return "ZEND_NULL";
  612. }
  613. }
  614. if (!isset($opcode["op2"][$op2])) {
  615. if (($op2 == 'TMP' || $op2 == 'VAR') &&
  616. isset($opcode["op2"]["TMPVAR"])) {
  617. $op2 = "TMPVAR";
  618. } else if (($op2 == 'TMP' || $op2 == 'VAR') &&
  619. isset($opcode["op2"]["TMPVARCV"])) {
  620. $op2 = "TMPVARCV";
  621. } else if ($op2 == 'CV' &&
  622. isset($opcode["op2"]["TMPVARCV"])) {
  623. $op2 = "TMPVARCV";
  624. } else if (isset($opcode["op2"]["ANY"])) {
  625. $op2 = "ANY";
  626. } else if ($spec) {
  627. /* dispatch to unknown handler in unreachable code */
  628. return "ZEND_NULL";
  629. }
  630. }
  631. /* forward common specs (e.g. in ZEND_VM_DISPATCH_TO_HANDLER) */
  632. if (isset($extra_spec, $opcode["spec"])) {
  633. $extra = extra_spec_name(array_intersect_key($extra_spec, $opcode["spec"]));
  634. }
  635. }
  636. return $name . ($spec ? "_SPEC" : "") . $prefix[$op1] . $prefix[$op2] . $extra;
  637. }
  638. // Formats condition, protecting it by parentheses when needed.
  639. function format_condition($condition) {
  640. if ($condition === "") {
  641. throw new InvalidArgumentException("A non empty string condition was expected.");
  642. }
  643. if ($condition[0] === "(" && substr($condition, -1) === ")") {
  644. return $condition;
  645. }
  646. return "(" . $condition . ")";
  647. }
  648. // Generates code for opcode handler or helper
  649. function gen_code($f, $spec, $kind, $code, $op1, $op2, $name, $extra_spec=null) {
  650. global $op1_type, $op2_type, $op1_get_zval_ptr, $op2_get_zval_ptr,
  651. $op1_get_zval_ptr_deref, $op2_get_zval_ptr_deref,
  652. $op1_get_zval_ptr_undef, $op2_get_zval_ptr_undef,
  653. $op1_get_zval_ptr_ptr, $op2_get_zval_ptr_ptr,
  654. $op1_get_zval_ptr_ptr_undef, $op2_get_zval_ptr_ptr_undef,
  655. $op1_get_obj_zval_ptr, $op2_get_obj_zval_ptr,
  656. $op1_get_obj_zval_ptr_undef, $op2_get_obj_zval_ptr_undef,
  657. $op1_get_obj_zval_ptr_deref, $op2_get_obj_zval_ptr_deref,
  658. $op1_get_obj_zval_ptr_ptr, $op2_get_obj_zval_ptr_ptr,
  659. $op1_get_obj_zval_ptr_ptr_undef, $op2_get_obj_zval_ptr_ptr_undef,
  660. $op1_free_unfetched, $op2_free_unfetched,
  661. $op1_free_op, $op2_free_op, $op1_free_op_if_var, $op2_free_op_if_var,
  662. $op1_free_op_var_ptr, $op2_free_op_var_ptr, $prefix,
  663. $op_data_type, $op_data_get_zval_ptr, $op_data_get_zval_ptr_undef,
  664. $op_data_get_zval_ptr_deref, $op_data_get_zval_ptr_ptr,
  665. $op_data_free_op, $op_data_free_op_var_ptr, $op_data_free_unfetched;
  666. // Specializing
  667. $specialized_replacements = array(
  668. "/OP1_TYPE/" => $op1_type[$op1],
  669. "/OP2_TYPE/" => $op2_type[$op2],
  670. "/GET_OP1_ZVAL_PTR\(([^)]*)\)/" => $op1_get_zval_ptr[$op1],
  671. "/GET_OP2_ZVAL_PTR\(([^)]*)\)/" => $op2_get_zval_ptr[$op2],
  672. "/GET_OP1_ZVAL_PTR_DEREF\(([^)]*)\)/" => $op1_get_zval_ptr_deref[$op1],
  673. "/GET_OP2_ZVAL_PTR_DEREF\(([^)]*)\)/" => $op2_get_zval_ptr_deref[$op2],
  674. "/GET_OP1_ZVAL_PTR_UNDEF\(([^)]*)\)/" => $op1_get_zval_ptr_undef[$op1],
  675. "/GET_OP2_ZVAL_PTR_UNDEF\(([^)]*)\)/" => $op2_get_zval_ptr_undef[$op2],
  676. "/GET_OP1_ZVAL_PTR_PTR\(([^)]*)\)/" => $op1_get_zval_ptr_ptr[$op1],
  677. "/GET_OP2_ZVAL_PTR_PTR\(([^)]*)\)/" => $op2_get_zval_ptr_ptr[$op2],
  678. "/GET_OP1_ZVAL_PTR_PTR_UNDEF\(([^)]*)\)/" => $op1_get_zval_ptr_ptr_undef[$op1],
  679. "/GET_OP2_ZVAL_PTR_PTR_UNDEF\(([^)]*)\)/" => $op2_get_zval_ptr_ptr_undef[$op2],
  680. "/GET_OP1_OBJ_ZVAL_PTR\(([^)]*)\)/" => $op1_get_obj_zval_ptr[$op1],
  681. "/GET_OP2_OBJ_ZVAL_PTR\(([^)]*)\)/" => $op2_get_obj_zval_ptr[$op2],
  682. "/GET_OP1_OBJ_ZVAL_PTR_UNDEF\(([^)]*)\)/" => $op1_get_obj_zval_ptr_undef[$op1],
  683. "/GET_OP2_OBJ_ZVAL_PTR_UNDEF\(([^)]*)\)/" => $op2_get_obj_zval_ptr_undef[$op2],
  684. "/GET_OP1_OBJ_ZVAL_PTR_DEREF\(([^)]*)\)/" => $op1_get_obj_zval_ptr_deref[$op1],
  685. "/GET_OP2_OBJ_ZVAL_PTR_DEREF\(([^)]*)\)/" => $op2_get_obj_zval_ptr_deref[$op2],
  686. "/GET_OP1_OBJ_ZVAL_PTR_PTR\(([^)]*)\)/" => $op1_get_obj_zval_ptr_ptr[$op1],
  687. "/GET_OP2_OBJ_ZVAL_PTR_PTR\(([^)]*)\)/" => $op2_get_obj_zval_ptr_ptr[$op2],
  688. "/GET_OP1_OBJ_ZVAL_PTR_PTR_UNDEF\(([^)]*)\)/" => $op1_get_obj_zval_ptr_ptr_undef[$op1],
  689. "/GET_OP2_OBJ_ZVAL_PTR_PTR_UNDEF\(([^)]*)\)/" => $op2_get_obj_zval_ptr_ptr_undef[$op2],
  690. "/FREE_OP1\(\)/" => $op1_free_op[$op1],
  691. "/FREE_OP2\(\)/" => $op2_free_op[$op2],
  692. "/FREE_OP1_IF_VAR\(\)/" => $op1_free_op_if_var[$op1],
  693. "/FREE_OP2_IF_VAR\(\)/" => $op2_free_op_if_var[$op2],
  694. "/FREE_OP1_VAR_PTR\(\)/" => $op1_free_op_var_ptr[$op1],
  695. "/FREE_OP2_VAR_PTR\(\)/" => $op2_free_op_var_ptr[$op2],
  696. "/\!ZEND_VM_SPEC/m" => ($op1!="ANY"||$op2!="ANY")?"0":"1",
  697. "/ZEND_VM_SPEC/m" => ($op1!="ANY"||$op2!="ANY")?"1":"0",
  698. "/ZEND_VM_C_LABEL\(\s*([A-Za-z_]*)\s*\)/m" => "\\1".(($spec && $kind != ZEND_VM_KIND_CALL)?("_SPEC".$prefix[$op1].$prefix[$op2].extra_spec_name($extra_spec)):""),
  699. "/ZEND_VM_C_GOTO\(\s*([A-Za-z_]*)\s*\)/m" => "goto \\1".(($spec && $kind != ZEND_VM_KIND_CALL)?("_SPEC".$prefix[$op1].$prefix[$op2].extra_spec_name($extra_spec)):""),
  700. "/^#(\s*)if\s+1\s*\\|\\|.*[^\\\\]$/m" => "#\\1if 1",
  701. "/^#(\s*)if\s+0\s*&&.*[^\\\\]$/m" => "#\\1if 0",
  702. "/^#(\s*)elif\s+1\s*\\|\\|.*[^\\\\]$/m" => "#\\1elif 1",
  703. "/^#(\s*)elif\s+0\s*&&.*[^\\\\]$/m" => "#\\1elif 0",
  704. "/OP_DATA_TYPE/" => $op_data_type[isset($extra_spec['OP_DATA']) ? $extra_spec['OP_DATA'] : "ANY"],
  705. "/GET_OP_DATA_ZVAL_PTR\(([^)]*)\)/" => $op_data_get_zval_ptr[isset($extra_spec['OP_DATA']) ? $extra_spec['OP_DATA'] : "ANY"],
  706. "/GET_OP_DATA_ZVAL_PTR_UNDEF\(([^)]*)\)/" => $op_data_get_zval_ptr_undef[isset($extra_spec['OP_DATA']) ? $extra_spec['OP_DATA'] : "ANY"],
  707. "/GET_OP_DATA_ZVAL_PTR_DEREF\(([^)]*)\)/" => $op_data_get_zval_ptr_deref[isset($extra_spec['OP_DATA']) ? $extra_spec['OP_DATA'] : "ANY"],
  708. "/GET_OP_DATA_ZVAL_PTR_PTR\(([^)]*)\)/" => $op_data_get_zval_ptr_ptr[isset($extra_spec['OP_DATA']) ? $extra_spec['OP_DATA'] : "ANY"],
  709. "/FREE_OP_DATA\(\)/" => $op_data_free_op[isset($extra_spec['OP_DATA']) ? $extra_spec['OP_DATA'] : "ANY"],
  710. "/FREE_OP_DATA_VAR_PTR\(\)/" => $op_data_free_op_var_ptr[isset($extra_spec['OP_DATA']) ? $extra_spec['OP_DATA'] : "ANY"],
  711. "/RETURN_VALUE_USED\(opline\)/" => isset($extra_spec['RETVAL']) ? $extra_spec['RETVAL'] : "RETURN_VALUE_USED(opline)",
  712. "/arg_num <= MAX_ARG_FLAG_NUM/" => isset($extra_spec['QUICK_ARG']) ? $extra_spec['QUICK_ARG'] : "arg_num <= MAX_ARG_FLAG_NUM",
  713. "/ZEND_VM_SMART_BRANCH\(\s*([^,)]*)\s*,\s*([^)]*)\s*\)/" => isset($extra_spec['SMART_BRANCH']) ?
  714. ($extra_spec['SMART_BRANCH'] == 1 ?
  715. "ZEND_VM_SMART_BRANCH_JMPZ(\\1, \\2)"
  716. : ($extra_spec['SMART_BRANCH'] == 2 ?
  717. "ZEND_VM_SMART_BRANCH_JMPNZ(\\1, \\2)" : "ZEND_VM_SMART_BRANCH_NONE(\\1, \\2)"))
  718. : "ZEND_VM_SMART_BRANCH(\\1, \\2)",
  719. "/ZEND_VM_SMART_BRANCH_TRUE\(\s*\)/" => isset($extra_spec['SMART_BRANCH']) ?
  720. ($extra_spec['SMART_BRANCH'] == 1 ?
  721. "ZEND_VM_SMART_BRANCH_TRUE_JMPZ()"
  722. : ($extra_spec['SMART_BRANCH'] == 2 ?
  723. "ZEND_VM_SMART_BRANCH_TRUE_JMPNZ()" : "ZEND_VM_SMART_BRANCH_TRUE_NONE()"))
  724. : "ZEND_VM_SMART_BRANCH_TRUE()",
  725. "/ZEND_VM_SMART_BRANCH_FALSE\(\s*\)/" => isset($extra_spec['SMART_BRANCH']) ?
  726. ($extra_spec['SMART_BRANCH'] == 1 ?
  727. "ZEND_VM_SMART_BRANCH_FALSE_JMPZ()"
  728. : ($extra_spec['SMART_BRANCH'] == 2 ?
  729. "ZEND_VM_SMART_BRANCH_FALSE_JMPNZ()" : "ZEND_VM_SMART_BRANCH_FALSE_NONE()"))
  730. : "ZEND_VM_SMART_BRANCH_FALSE()",
  731. "/opline->extended_value\s*&\s*ZEND_ISEMPTY/" => isset($extra_spec['ISSET']) ?
  732. ($extra_spec['ISSET'] == 0 ? "0" : "1")
  733. : "\\0",
  734. "/opline->extended_value\s*&\s*~\s*ZEND_ISEMPTY/" => isset($extra_spec['ISSET']) ?
  735. ($extra_spec['ISSET'] == 0 ? "\\0" : "opline->extended_value")
  736. : "\\0",
  737. "/ZEND_OBSERVER_ENABLED/" => isset($extra_spec['OBSERVER']) && $extra_spec['OBSERVER'] == 1 ? "1" : "0",
  738. "/ZEND_OBSERVER_USE_RETVAL/" => isset($extra_spec['OBSERVER']) && $extra_spec['OBSERVER'] == 1 ? "zval observer_retval" : "",
  739. "/ZEND_OBSERVER_SET_RETVAL\(\)/" => isset($extra_spec['OBSERVER']) && $extra_spec['OBSERVER'] == 1 ? "if (!return_value) { return_value = &observer_retval; }" : "",
  740. "/ZEND_OBSERVER_FREE_RETVAL\(\)/" => isset($extra_spec['OBSERVER']) && $extra_spec['OBSERVER'] == 1 ? "if (return_value == &observer_retval) { zval_ptr_dtor_nogc(&observer_retval); }" : "",
  741. "/ZEND_OBSERVER_SAVE_OPLINE\(\)/" => isset($extra_spec['OBSERVER']) && $extra_spec['OBSERVER'] == 1 ? "SAVE_OPLINE()" : "",
  742. "/ZEND_OBSERVER_FCALL_BEGIN\(\s*(.*)\s*\)/" => isset($extra_spec['OBSERVER']) ?
  743. ($extra_spec['OBSERVER'] == 0 ? "" : "zend_observer_fcall_begin(\\1)")
  744. : "",
  745. "/ZEND_OBSERVER_FCALL_END\(\s*([^,]*)\s*,\s*(.*)\s*\)/" => isset($extra_spec['OBSERVER']) ?
  746. ($extra_spec['OBSERVER'] == 0 ? "" : "zend_observer_fcall_end(\\1, \\2)")
  747. : "",
  748. );
  749. $code = preg_replace(array_keys($specialized_replacements), array_values($specialized_replacements), $code);
  750. if (0 && strpos($code, '{') === 0) {
  751. $code = "{\n\tfprintf(stderr, \"$name\\n\");\n" . substr($code, 1);
  752. }
  753. // Updating code according to selected threading model
  754. switch ($kind) {
  755. case ZEND_VM_KIND_HYBRID:
  756. $code = preg_replace_callback(
  757. array(
  758. "/EXECUTE_DATA(?=[^_])/m",
  759. "/ZEND_VM_DISPATCH_TO_HANDLER\(\s*([A-Z_]*)\s*\)/m",
  760. "/ZEND_VM_DISPATCH_TO_HELPER\(\s*([A-Za-z_]*)\s*(,[^)]*)?\)/m",
  761. ),
  762. function($matches) use ($spec, $prefix, $op1, $op2, $extra_spec) {
  763. if (strncasecmp($matches[0], "EXECUTE_DATA", strlen("EXECUTE_DATA")) == 0) {
  764. return "execute_data";
  765. } else if (strncasecmp($matches[0], "ZEND_VM_DISPATCH_TO_HANDLER", strlen("ZEND_VM_DISPATCH_TO_HANDLER")) == 0) {
  766. global $opcodes, $opnames;
  767. $name = $matches[1];
  768. $opcode = $opcodes[$opnames[$name]];
  769. return "goto " . opcode_name($name, $spec, $op1, $op2, $extra_spec) . "_LABEL";
  770. } else {
  771. // ZEND_VM_DISPATCH_TO_HELPER
  772. if (is_hot_helper($matches[1])) {
  773. if (isset($matches[2])) {
  774. // extra args
  775. $args = preg_replace("/,\s*([A-Za-z0-9_]*)\s*,\s*([^,)\s]*)\s*/", "$1 = $2; ", $matches[2]);
  776. return $args . "goto " . helper_name($matches[1], $spec, $op1, $op2, $extra_spec) . "_LABEL";
  777. }
  778. return "goto " . helper_name($matches[1], $spec, $op1, $op2, $extra_spec) . "_LABEL";
  779. }
  780. if (isset($matches[2])) {
  781. // extra args
  782. $args = substr(preg_replace("/,\s*[A-Za-z0-9_]*\s*,\s*([^,)\s]*)\s*/", ", $1", $matches[2]), 2);
  783. return "ZEND_VM_TAIL_CALL(" . helper_name($matches[1], $spec, $op1, $op2, $extra_spec) . "(" . $args. " ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_CC))";
  784. }
  785. return "ZEND_VM_TAIL_CALL(" . helper_name($matches[1], $spec, $op1, $op2, $extra_spec) . "(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU))";
  786. }
  787. },
  788. $code);
  789. break;
  790. case ZEND_VM_KIND_CALL:
  791. $code = preg_replace_callback(
  792. array(
  793. "/EXECUTE_DATA(?=[^_])/m",
  794. "/ZEND_VM_DISPATCH_TO_HANDLER\(\s*([A-Z_]*)\s*\)/m",
  795. "/ZEND_VM_DISPATCH_TO_HELPER\(\s*([A-Za-z_]*)\s*(,[^)]*)?\)/m",
  796. ),
  797. function($matches) use ($spec, $prefix, $op1, $op2, $extra_spec, $name) {
  798. if (strncasecmp($matches[0], "EXECUTE_DATA", strlen("EXECUTE_DATA")) == 0) {
  799. return "execute_data";
  800. } else if (strncasecmp($matches[0], "ZEND_VM_DISPATCH_TO_HANDLER", strlen("ZEND_VM_DISPATCH_TO_HANDLER")) == 0) {
  801. global $opcodes, $opnames;
  802. $handler = $matches[1];
  803. $opcode = $opcodes[$opnames[$handler]];
  804. $inline =
  805. ZEND_VM_KIND == ZEND_VM_KIND_HYBRID &&
  806. isset($opcode["use"]) &&
  807. is_hot_handler($opcode["hot"], $op1, $op2, $extra_spec) &&
  808. is_hot_handler($opcodes[$opnames[$name]]["hot"], $op1, $op2, $extra_spec) ?
  809. "_INLINE" : "";
  810. return "ZEND_VM_TAIL_CALL(" . opcode_name($handler, $spec, $op1, $op2, $extra_spec) . $inline . "_HANDLER(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU))";
  811. } else {
  812. // ZEND_VM_DISPATCH_TO_HELPER
  813. if (isset($matches[2])) {
  814. // extra args
  815. $args = substr(preg_replace("/,\s*[A-Za-z0-9_]*\s*,\s*([^,)\s]*)\s*/", ", $1", $matches[2]), 2);
  816. return "ZEND_VM_TAIL_CALL(" . helper_name($matches[1], $spec, $op1, $op2, $extra_spec) . "(" . $args. " ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_CC))";
  817. }
  818. return "ZEND_VM_TAIL_CALL(" . helper_name($matches[1], $spec, $op1, $op2, $extra_spec) . "(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU))";
  819. }
  820. },
  821. $code);
  822. break;
  823. case ZEND_VM_KIND_SWITCH:
  824. $code = preg_replace_callback(
  825. array(
  826. "/EXECUTE_DATA(?=[^_])/m",
  827. "/ZEND_VM_DISPATCH_TO_HANDLER\(\s*([A-Z_]*)\s*\)/m",
  828. "/ZEND_VM_DISPATCH_TO_HELPER\(\s*([A-Za-z_]*)\s*(,[^)]*)?\)/m",
  829. ),
  830. function($matches) use ($spec, $prefix, $op1, $op2, $extra_spec) {
  831. if (strncasecmp($matches[0], "EXECUTE_DATA", strlen("EXECUTE_DATA")) == 0) {
  832. return "execute_data";
  833. } else if (strncasecmp($matches[0], "ZEND_VM_DISPATCH_TO_HANDLER", strlen("ZEND_VM_DISPATCH_TO_HANDLER")) == 0) {
  834. return "goto " . opcode_name($matches[1], $spec, $op1, $op2, $extra_spec) . "_LABEL";
  835. } else {
  836. // ZEND_VM_DISPATCH_TO_HELPER
  837. if (isset($matches[2])) {
  838. // extra args
  839. $args = preg_replace("/,\s*([A-Za-z0-9_]*)\s*,\s*([^,)\s]*)\s*/", "$1 = $2; ", $matches[2]);
  840. return $args . "goto " . helper_name($matches[1], $spec, $op1, $op2, $extra_spec);
  841. }
  842. return "goto " . helper_name($matches[1], $spec, $op1, $op2, $extra_spec);
  843. }
  844. },
  845. $code);
  846. break;
  847. case ZEND_VM_KIND_GOTO:
  848. $code = preg_replace_callback(
  849. array(
  850. "/EXECUTE_DATA(?=[^_])/m",
  851. "/ZEND_VM_DISPATCH_TO_HANDLER\(\s*([A-Z_]*)\s*\)/m",
  852. "/ZEND_VM_DISPATCH_TO_HELPER\(\s*([A-Za-z_]*)\s*(,[^)]*)?\)/m",
  853. ),
  854. function($matches) use ($spec, $prefix, $op1, $op2, $extra_spec) {
  855. if (strncasecmp($matches[0], "EXECUTE_DATA", strlen("EXECUTE_DATA")) == 0) {
  856. return "execute_data";
  857. } else if (strncasecmp($matches[0], "ZEND_VM_DISPATCH_TO_HANDLER", strlen("ZEND_VM_DISPATCH_TO_HANDLER")) == 0) {
  858. return "goto " . opcode_name($matches[1], $spec, $op1, $op2, $extra_spec) . "_LABEL";
  859. } else {
  860. // ZEND_VM_DISPATCH_TO_HELPER
  861. if (isset($matches[2])) {
  862. // extra args
  863. $args = preg_replace("/,\s*([A-Za-z0-9_]*)\s*,\s*([^,)\s]*)\s*/", "$1 = $2; ", $matches[2]);
  864. return $args . "goto " . helper_name($matches[1], $spec, $op1, $op2, $extra_spec);
  865. }
  866. return "goto " . helper_name($matches[1], $spec, $op1, $op2, $extra_spec);
  867. }
  868. },
  869. $code);
  870. break;
  871. }
  872. /* Remove unnecessary ';' */
  873. $code = preg_replace('/^\s*;\s*$/m', '', $code);
  874. /* Remove WS */
  875. $code = preg_replace('/[ \t]+\n/m', "\n", $code);
  876. out($f, $code);
  877. }
  878. function skip_extra_spec_function($op1, $op2, $extra_spec) {
  879. global $commutative_order;
  880. if (isset($extra_spec["NO_CONST_CONST"]) &&
  881. $op1 == "CONST" && $op2 == "CONST") {
  882. // Skip useless constant handlers
  883. return true;
  884. }
  885. if (isset($extra_spec["COMMUTATIVE"]) &&
  886. $commutative_order[$op1] < $commutative_order[$op2]) {
  887. // Skip duplicate commutative handlers
  888. return true;
  889. }
  890. return false;
  891. }
  892. function is_hot_handler($hot, $op1, $op2, $extra_spec) {
  893. if (isset($extra_spec["SMART_BRANCH"]) && $extra_spec["SMART_BRANCH"] == 0) {
  894. return false;
  895. }
  896. if (isset($extra_spec["OBSERVER"]) && $extra_spec["OBSERVER"] == 1) {
  897. return false;
  898. }
  899. if ($hot === 'HOT_' || $hot === 'INLINE_') {
  900. return true;
  901. } else if ($hot === 'HOT_NOCONST_') {
  902. return ($op1 !== 'CONST');
  903. } else if ($hot === 'HOT_NOCONSTCONST_') {
  904. return (($op1 !== 'CONST') || ($op2 !== 'CONST')) ;
  905. } else if ($hot === 'HOT_OBJ_') {
  906. return (($op1 === 'UNUSED') || ($op1 === 'CV')) && ($op2 === 'CONST');
  907. } else if ($hot === 'HOT_SEND_') {
  908. return !empty($extra_spec["QUICK_ARG"]);
  909. } else {
  910. return false;
  911. }
  912. }
  913. function is_cold_handler($hot, $op1, $op2, $extra_spec) {
  914. if ($hot === 'COLD_') {
  915. return true;
  916. } else if (isset($extra_spec["OBSERVER"]) && $extra_spec["OBSERVER"] == 1) {
  917. return true;
  918. } else if ($hot === 'COLD_CONST_') {
  919. return ($op1 === 'CONST');
  920. } else if ($hot === 'COLD_CONSTCONST_') {
  921. return ($op1 === 'CONST' && $op2 === 'CONST');
  922. } else if ($hot === 'HOT_OBJ_') {
  923. return ($op1 === 'CONST');
  924. } else if ($hot === 'HOT_NOCONST_') {
  925. return ($op1 === 'CONST');
  926. } else if ($hot === 'HOT_NOCONSTCONST_') {
  927. return ($op1 === 'CONST' && $op2 === 'CONST');
  928. } else {
  929. return false;
  930. }
  931. }
  932. function is_inline_hybrid_handler($name, $hot, $op1, $op2, $extra_spec) {
  933. return ($hot === 'INLINE_');
  934. }
  935. // Generates opcode handler
  936. function gen_handler($f, $spec, $kind, $name, $op1, $op2, $use, $code, $lineno, $opcode, $extra_spec = null, &$switch_labels = array()) {
  937. global $definition_file, $prefix, $opnames, $gen_order;
  938. static $used_observer_handlers = array();
  939. if (isset($opcode['alias']) && ($spec || $kind != ZEND_VM_KIND_SWITCH)) {
  940. return;
  941. }
  942. if ($spec && skip_extra_spec_function($op1, $op2, $extra_spec)) {
  943. return;
  944. }
  945. /* Skip SMART_BRANCH specialization for "cold" CONST_CONST instructions */
  946. if (isset($extra_spec["SMART_BRANCH"])) {
  947. if ($opcode["hot"] === 'HOT_NOCONSTCONST_'
  948. || $opcode["hot"] === 'COLD_CONSTCONST_') {
  949. if (($op1 === 'CONST') && ($op2 === 'CONST')) {
  950. if ($extra_spec["SMART_BRANCH"] == 0) {
  951. unset($extra_spec["SMART_BRANCH"]);
  952. } else {
  953. return;
  954. }
  955. }
  956. }
  957. }
  958. /* Skip QUICK_ARG specialization for named parameters */
  959. if (isset($extra_spec["QUICK_ARG"])) {
  960. if ($op2 === "CONST") {
  961. if ($extra_spec["QUICK_ARG"] == 0) {
  962. unset($extra_spec["QUICK_ARG"]);
  963. } else {
  964. return;
  965. }
  966. }
  967. }
  968. /* Skip all specialization for OBSERVER handlers */
  969. if (isset($extra_spec["OBSERVER"]) && $extra_spec["OBSERVER"] == 1) {
  970. if (isset($extra_spec["RETVAL"])) {
  971. if ($extra_spec["RETVAL"] == 0) {
  972. unset($extra_spec["RETVAL"]);
  973. } else {
  974. return;
  975. }
  976. }
  977. if ($op1 != "ANY" || $op2 != "ANY") {
  978. if (!isset($used_observer_handlers[$kind][$opcode["op"]])) {
  979. $used_observer_handlers[$kind][$opcode["op"]] = true;
  980. $op1 = "ANY";
  981. $op2 = "ANY";
  982. } else {
  983. return;
  984. }
  985. }
  986. }
  987. if (ZEND_VM_LINES) {
  988. out($f, "#line $lineno \"$definition_file\"\n");
  989. }
  990. // Generate opcode handler's entry point according to selected threading model
  991. $additional_func = false;
  992. $spec_name = $name.($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2].($spec?extra_spec_name($extra_spec):"");
  993. switch ($kind) {
  994. case ZEND_VM_KIND_HYBRID:
  995. if (is_inline_hybrid_handler($name, $opcode["hot"], $op1, $op2, $extra_spec)) {
  996. $out = fopen('php://memory', 'w+');
  997. gen_code($out, $spec, $kind, $code, $op1, $op2, $name, $extra_spec);
  998. rewind($out);
  999. $code =
  1000. "\t\t\tHYBRID_CASE({$spec_name}):\n"
  1001. . "\t\t\t\tVM_TRACE($spec_name)\n"
  1002. . stream_get_contents($out);
  1003. fclose($out);
  1004. } else {
  1005. $inline =
  1006. isset($opcode["use"]) &&
  1007. is_hot_handler($opcode["hot"], $op1, $op2, $extra_spec) ?
  1008. "_INLINE" : "";
  1009. $code =
  1010. "\t\t\tHYBRID_CASE({$spec_name}):\n"
  1011. . "\t\t\t\tVM_TRACE($spec_name)\n"
  1012. . "\t\t\t\t{$spec_name}{$inline}_HANDLER(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);\n"
  1013. . "\t\t\t\tHYBRID_BREAK();\n";
  1014. }
  1015. if (is_array($gen_order)) {
  1016. $gen_order[$spec_name] = $code;
  1017. } else {
  1018. out($f, $code);
  1019. }
  1020. return;
  1021. case ZEND_VM_KIND_CALL:
  1022. if ($opcode["hot"] && ZEND_VM_KIND == ZEND_VM_KIND_HYBRID && is_hot_handler($opcode["hot"], $op1, $op2, $extra_spec)) {
  1023. if (isset($opcode["use"])) {
  1024. out($f,"static zend_always_inline ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL {$spec_name}_INLINE_HANDLER(ZEND_OPCODE_HANDLER_ARGS)\n");
  1025. $additional_func = true;
  1026. } else {
  1027. out($f,"static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL {$spec_name}_HANDLER(ZEND_OPCODE_HANDLER_ARGS)\n");
  1028. }
  1029. } else if ($opcode["hot"] && is_cold_handler($opcode["hot"], $op1, $op2, $extra_spec)) {
  1030. out($f,"static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL {$spec_name}_HANDLER(ZEND_OPCODE_HANDLER_ARGS)\n");
  1031. } else {
  1032. out($f,"static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL {$spec_name}_HANDLER(ZEND_OPCODE_HANDLER_ARGS)\n");
  1033. }
  1034. break;
  1035. case ZEND_VM_KIND_SWITCH:
  1036. if ($spec) {
  1037. $cur = $switch_labels ? end($switch_labels) + 1 : 0;
  1038. out($f,"case $cur: /* $spec_name */");
  1039. $switch_labels[$spec_name] = $cur;
  1040. } else {
  1041. out($f,"case ".$name.":");
  1042. }
  1043. if ($use) {
  1044. // This handler is used by other handlers. We will add label to call it.
  1045. out($f," {$spec_name}_LABEL: ZEND_ATTRIBUTE_UNUSED_LABEL\n");
  1046. } else {
  1047. out($f,"\n");
  1048. }
  1049. break;
  1050. case ZEND_VM_KIND_GOTO:
  1051. out($f,"{$spec_name}_LABEL: ZEND_VM_GUARD($spec_name);\n");
  1052. break;
  1053. }
  1054. // Generate opcode handler's code
  1055. gen_code($f, $spec, $kind, $code, $op1, $op2, $name, $extra_spec);
  1056. if ($additional_func) {
  1057. out($f,"static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL {$spec_name}_HANDLER(ZEND_OPCODE_HANDLER_ARGS)\n");
  1058. out($f,"{\n");
  1059. out($f,"\tZEND_VM_TAIL_CALL({$spec_name}_INLINE_HANDLER(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU));\n");
  1060. out($f,"}\n");
  1061. out($f,"\n");
  1062. }
  1063. }
  1064. // Generates helper
  1065. function gen_helper($f, $spec, $kind, $name, $op1, $op2, $param, $code, $lineno, $inline, $cold = false, $hot = false, $extra_spec = null) {
  1066. global $definition_file, $prefix;
  1067. if ($kind == ZEND_VM_KIND_HYBRID && !$hot) {
  1068. return;
  1069. }
  1070. if ($spec && skip_extra_spec_function($op1, $op2, $extra_spec)) {
  1071. return;
  1072. }
  1073. if (ZEND_VM_LINES) {
  1074. out($f, "#line $lineno \"$definition_file\"\n");
  1075. }
  1076. $spec_name = $name.($spec?"_SPEC":"").$prefix[$op1].$prefix[$op2].($spec?extra_spec_name($extra_spec):"");
  1077. // Generate helper's entry point according to selected threading model
  1078. switch ($kind) {
  1079. case ZEND_VM_KIND_HYBRID:
  1080. out($f, $spec_name . "_LABEL:\n");
  1081. break;
  1082. case ZEND_VM_KIND_CALL:
  1083. if ($inline) {
  1084. $zend_attributes = " zend_always_inline";
  1085. $zend_fastcall = "";
  1086. } else {
  1087. if ($cold) {
  1088. $zend_attributes = " zend_never_inline ZEND_COLD";
  1089. } else {
  1090. $zend_attributes = " zend_never_inline";
  1091. }
  1092. $zend_fastcall = " ZEND_FASTCALL";
  1093. }
  1094. if ($param == null) {
  1095. // Helper without parameters
  1096. out($f, "static$zend_attributes ZEND_OPCODE_HANDLER_RET$zend_fastcall $spec_name(ZEND_OPCODE_HANDLER_ARGS)\n");
  1097. } else {
  1098. // Helper with parameter
  1099. out($f, "static$zend_attributes ZEND_OPCODE_HANDLER_RET$zend_fastcall $spec_name($param ZEND_OPCODE_HANDLER_ARGS_DC)\n");
  1100. }
  1101. break;
  1102. case ZEND_VM_KIND_SWITCH:
  1103. out($f, "$spec_name:\n");
  1104. break;
  1105. case ZEND_VM_KIND_GOTO:
  1106. out($f, "$spec_name:\n");
  1107. break;
  1108. }
  1109. // Generate helper's code
  1110. gen_code($f, $spec, $kind, $code, $op1, $op2, $name, $extra_spec);
  1111. }
  1112. function gen_null_label($f, $kind, $prolog) {
  1113. switch ($kind) {
  1114. case ZEND_VM_KIND_CALL:
  1115. out($f,$prolog."ZEND_NULL_HANDLER,\n");
  1116. break;
  1117. case ZEND_VM_KIND_SWITCH:
  1118. out($f,$prolog."(void*)(uintptr_t)-1,\n");
  1119. break;
  1120. case ZEND_VM_KIND_GOTO:
  1121. out($f,$prolog."(void*)&&ZEND_NULL_LABEL,\n");
  1122. break;
  1123. }
  1124. }
  1125. // Generates array of opcode handlers (specialized or unspecialized)
  1126. function gen_labels($f, $spec, $kind, $prolog, &$specs, $switch_labels = array()) {
  1127. global $opcodes, $opnames, $op_types, $prefix, $op_types_ex;
  1128. $list = [];
  1129. $next = 0;
  1130. $label = 0;
  1131. if ($spec) {
  1132. // Emit labels for specialized executor
  1133. // For each opcode in opcode number order
  1134. foreach ($opcodes as $num => $dsc) {
  1135. if (isset($dsc['alias'])) {
  1136. $specs[$num] = $specs[$opnames[$dsc['alias']]];
  1137. continue;
  1138. }
  1139. $specs[$num] = "$label";
  1140. $spec_op1 = $spec_op2 = $spec_extra = false;
  1141. $def_op1_type = $def_op2_type = "ANY";
  1142. $next = $num + 1;
  1143. if (isset($dsc["op1"]) && !isset($dsc["op1"]["ANY"])) {
  1144. $count = 0;
  1145. foreach ($op_types_ex as $t) {
  1146. if (isset($dsc["op1"][$t])) {
  1147. $def_op1_type = $t;
  1148. $count++;
  1149. }
  1150. }
  1151. if ($count > 1) {
  1152. $spec_op1 = true;
  1153. $specs[$num] .= " | SPEC_RULE_OP1";
  1154. $def_op1_type = "ANY";
  1155. }
  1156. }
  1157. if (isset($dsc["op2"]) && !isset($dsc["op2"]["ANY"])) {
  1158. $count = 0;
  1159. foreach ($op_types_ex as $t) {
  1160. if (isset($dsc["op2"][$t])) {
  1161. $def_op2_type = $t;
  1162. $count++;
  1163. }
  1164. }
  1165. if ($count > 1) {
  1166. $spec_op2 = true;
  1167. $specs[$num] .= " | SPEC_RULE_OP2";
  1168. $def_op2_type = "ANY";
  1169. }
  1170. }
  1171. $spec_extra = call_user_func_array("array_merge", extra_spec_handler($dsc) ?: array(array()));
  1172. $flags = extra_spec_flags($spec_extra);
  1173. if ($flags) {
  1174. $specs[$num] .= " | " . implode(" | ", $flags);
  1175. }
  1176. if ($num >= 256) {
  1177. $opcodes[$num]['spec_code'] = $specs[$num];
  1178. unset($specs[$num]);
  1179. }
  1180. $foreach_op1 = function($do) use ($dsc, $op_types) {
  1181. return function($_, $op2) use ($do, $dsc, $op_types) {
  1182. // For each op1.op_type except ANY
  1183. foreach ($op_types as $op1) {
  1184. if ($op1 != "ANY") {
  1185. if (!isset($dsc["op1"][$op1])) {
  1186. if ($op1 == "TMP" || $op1 == "VAR") {
  1187. if (isset($dsc["op1"]["TMPVAR"])) {
  1188. $op1 = "TMPVAR";
  1189. } else if (isset($dsc["op1"]["TMPVARCV"])) {
  1190. $op1 = "TMPVARCV";
  1191. } else {
  1192. $op1 = "ANY";
  1193. }
  1194. } else if ($op1 == "CV" && isset($dsc["op1"]["TMPVARCV"])) {
  1195. $op1 = "TMPVARCV";
  1196. } else {
  1197. // Try to use unspecialized handler
  1198. $op1 = "ANY";
  1199. }
  1200. }
  1201. $do($op1, $op2);
  1202. }
  1203. }
  1204. };
  1205. };
  1206. $foreach_op2 = function($do) use ($dsc, $op_types) {
  1207. return function($op1, $_) use ($do, $dsc, $op_types) {
  1208. // For each op2.op_type except ANY
  1209. foreach ($op_types as $op2) {
  1210. if ($op2 != "ANY") {
  1211. if (!isset($dsc["op2"][$op2])) {
  1212. if ($op2 == "TMP" || $op2 == "VAR") {
  1213. if (isset($dsc["op2"]["TMPVAR"])) {
  1214. $op2 = "TMPVAR";
  1215. } else if (isset($dsc["op2"]["TMPVARCV"])) {
  1216. $op2 = "TMPVARCV";
  1217. } else {
  1218. $op2 = "ANY";
  1219. }
  1220. } else if ($op2 == "CV" && isset($dsc["op2"]["TMPVARCV"])) {
  1221. $op2 = "TMPVARCV";
  1222. } else {
  1223. // Try to use unspecialized handler
  1224. $op2 = "ANY";
  1225. }
  1226. }
  1227. $do($op1, $op2);
  1228. }
  1229. }
  1230. };
  1231. };
  1232. $foreach_op_data = function($do) use ($dsc, $op_types) {
  1233. return function($op1, $op2, $extra_spec = array()) use ($do, $dsc, $op_types) {
  1234. // For each op_data.op_type except ANY
  1235. foreach ($op_types as $op_data) {
  1236. if ($op_data != "ANY") {
  1237. if (!isset($dsc["spec"]["OP_DATA"][$op_data])) {
  1238. if ($op_data == "TMP" || $op_data == "VAR") {
  1239. if (isset($dsc["spec"]["OP_DATA"]["TMPVAR"])) {
  1240. $op_data = "TMPVAR";
  1241. } else if (isset($dsc["spec"]["OP_DATA"]["TMPVARCV"])) {
  1242. $op_data = "TMPVARCV";
  1243. } else {
  1244. // Try to use unspecialized handler
  1245. $op_data = "ANY";
  1246. }
  1247. } else if ($op_data == "CV" && isset($dsc["OP_DATA"]["TMPVARCV"])) {
  1248. $op_data = "TMPVARCV";
  1249. } else {
  1250. // Try to use unspecialized handler
  1251. $op_data = "ANY";
  1252. }
  1253. }
  1254. $do($op1, $op2, array("OP_DATA" => $op_data) + $extra_spec);
  1255. }
  1256. }
  1257. };
  1258. };
  1259. $foreach_extra_spec = function($do, $spec) use ($dsc) {
  1260. return function($op1, $op2, $extra_spec = array()) use ($do, $spec, $dsc) {
  1261. foreach ($dsc["spec"][$spec] as $val) {
  1262. $do($op1, $op2, array($spec => $val) + $extra_spec);
  1263. }
  1264. };
  1265. };
  1266. $generate = function ($op1, $op2, $extra_spec = array()) use ($f, $kind, $dsc, $prefix, $prolog, $num, $switch_labels, &$label, &$list) {
  1267. global $commutative_order;
  1268. // Check if specialized handler is defined
  1269. /* TODO: figure out better way to signal "specialized and not defined" than an extra lookup */
  1270. if (isset($dsc["op1"][$op1]) &&
  1271. isset($dsc["op2"][$op2]) &&
  1272. (!isset($extra_spec["OP_DATA"]) || isset($dsc["spec"]["OP_DATA"][$extra_spec["OP_DATA"]]))) {
  1273. if (skip_extra_spec_function($op1, $op2, $extra_spec)) {
  1274. gen_null_label($f, $kind, $prolog);
  1275. $list[$label] = null;
  1276. $label++;
  1277. return;
  1278. }
  1279. /* Skip SMART_BRANCH specialization for "cold" CONST_CONST instructions */
  1280. if (isset($extra_spec["SMART_BRANCH"])) {
  1281. if ($dsc["hot"] === 'HOT_NOCONSTCONST_'
  1282. || $dsc["hot"] === 'COLD_CONSTCONST_') {
  1283. if (($op1 === 'CONST') && ($op2 === 'CONST')) {
  1284. unset($extra_spec["SMART_BRANCH"]);
  1285. }
  1286. }
  1287. }
  1288. /* Skip QUICK_ARG specialization for named parameters */
  1289. if (isset($extra_spec["QUICK_ARG"])) {
  1290. if ($op2 === "CONST") {
  1291. unset($extra_spec["QUICK_ARG"]);
  1292. }
  1293. }
  1294. /* Skip all specialization for OBSERVER handlers */
  1295. if (isset($extra_spec["OBSERVER"]) && $extra_spec["OBSERVER"] == 1) {
  1296. if (isset($extra_spec["RETVAL"])) {
  1297. unset($extra_spec["RETVAL"]);
  1298. }
  1299. if ($op1 != "ANY" || $op2 != "ANY") {
  1300. $op1 = "ANY";
  1301. $op2 = "ANY";
  1302. }
  1303. }
  1304. // Emit pointer to specialized handler
  1305. $spec_name = $dsc["op"]."_SPEC".$prefix[$op1].$prefix[$op2].extra_spec_name($extra_spec);
  1306. switch ($kind) {
  1307. case ZEND_VM_KIND_CALL:
  1308. out($f,"$prolog{$spec_name}_HANDLER,\n");
  1309. break;
  1310. case ZEND_VM_KIND_SWITCH:
  1311. out($f,$prolog."(void*)(uintptr_t)$switch_labels[$spec_name],\n");
  1312. break;
  1313. case ZEND_VM_KIND_GOTO:
  1314. out($f,$prolog."(void*)&&{$spec_name}_LABEL,\n");
  1315. break;
  1316. }
  1317. $list[$label] = $spec_name;
  1318. $label++;
  1319. } else {
  1320. // Emit pointer to handler of undefined opcode
  1321. gen_null_label($f, $kind, $prolog);
  1322. $list[$label] = null;
  1323. $label++;
  1324. }
  1325. };
  1326. $do = $generate;
  1327. if ($spec_extra) {
  1328. foreach ($spec_extra as $extra => $devnull) {
  1329. if ($extra == "OP_DATA") {
  1330. $do = $foreach_op_data($do);
  1331. } else {
  1332. $do = $foreach_extra_spec($do, $extra);
  1333. }
  1334. }
  1335. }
  1336. if ($spec_op2) {
  1337. $do = $foreach_op2($do);
  1338. }
  1339. if ($spec_op1) {
  1340. $do = $foreach_op1($do);
  1341. }
  1342. $do($def_op1_type, $def_op2_type);
  1343. }
  1344. } else {
  1345. // Emit labels for unspecialized executor
  1346. // For each opcode in opcode number order
  1347. foreach ($opcodes as $num => $dsc) {
  1348. while ($next != $num) {
  1349. // If some opcode numbers are not used then fill hole with pointers
  1350. // to handler of undefined opcode
  1351. switch ($kind) {
  1352. case ZEND_VM_KIND_CALL:
  1353. out($f,$prolog."ZEND_NULL_HANDLER,\n");
  1354. break;
  1355. case ZEND_VM_KIND_SWITCH:
  1356. out($f,$prolog."(void*)(uintptr_t)-1,\n");
  1357. break;
  1358. case ZEND_VM_KIND_GOTO:
  1359. out($f,$prolog."(void*)&&ZEND_NULL_LABEL,\n");
  1360. break;
  1361. }
  1362. $next++;
  1363. }
  1364. if ($num >= 256) {
  1365. continue;
  1366. }
  1367. $next = $num+1;
  1368. if (isset($dsc['alias']) && $kind != ZEND_VM_KIND_SWITCH) {
  1369. // Emit pointer to unspecialized handler
  1370. switch ($kind) {
  1371. case ZEND_VM_KIND_CALL:
  1372. out($f,$prolog.$dsc['alias']."_HANDLER,\n");
  1373. break;
  1374. case ZEND_VM_KIND_GOTO:
  1375. out($f,$prolog."(void*)&&".$dsc['alias']."_LABEL,\n");
  1376. break;
  1377. }
  1378. $list[] = $dsc["op"];
  1379. } else if ($dsc["code"]) { //ugly trick for ZEND_VM_DEFINE_OP
  1380. // Emit pointer to unspecialized handler
  1381. switch ($kind) {
  1382. case ZEND_VM_KIND_CALL:
  1383. out($f,$prolog.$dsc["op"]."_HANDLER,\n");
  1384. break;
  1385. case ZEND_VM_KIND_SWITCH:
  1386. out($f,$prolog."(void*)(uintptr_t)".((string)$num).",\n");
  1387. break;
  1388. case ZEND_VM_KIND_GOTO:
  1389. out($f,$prolog."(void*)&&".$dsc["op"]."_LABEL,\n");
  1390. break;
  1391. }
  1392. $list[] = $dsc["op"];
  1393. } else {
  1394. switch ($kind) {
  1395. case ZEND_VM_KIND_CALL:
  1396. out($f,$prolog."ZEND_NULL_HANDLER,\n");
  1397. break;
  1398. case ZEND_VM_KIND_SWITCH:
  1399. out($f,$prolog."(void*)(uintptr_t)-1,\n");
  1400. break;
  1401. case ZEND_VM_KIND_GOTO:
  1402. out($f,$prolog."(void*)&&ZEND_NULL_LABEL,\n");
  1403. break;
  1404. }
  1405. $list[] = null;
  1406. }
  1407. }
  1408. }
  1409. // Emit last handler's label (undefined opcode)
  1410. switch ($kind) {
  1411. case ZEND_VM_KIND_CALL:
  1412. out($f,$prolog."ZEND_NULL_HANDLER\n");
  1413. break;
  1414. case ZEND_VM_KIND_SWITCH:
  1415. out($f,$prolog."(void*)(uintptr_t)-1\n");
  1416. break;
  1417. case ZEND_VM_KIND_GOTO:
  1418. out($f,$prolog."(void*)&&ZEND_NULL_LABEL\n");
  1419. break;
  1420. }
  1421. $specs[$num + 1] = "$label";
  1422. $l = fopen(__DIR__ . "/zend_vm_handlers.h", "w+") or die("ERROR: Cannot create zend_vm_handlers.h\n");
  1423. out($l, "#define VM_HANDLERS(_) \\\n");
  1424. foreach ($list as $n => $name) {
  1425. if (null !== $name) {
  1426. out($l, "\t_($n, $name) \\\n");
  1427. }
  1428. }
  1429. out($l, "\t_($n+1, ZEND_NULL)\n");
  1430. fclose($l);
  1431. }
  1432. // Generates specialized offsets
  1433. function gen_specs($f, $prolog, $specs) {
  1434. $lastdef = array_pop($specs);
  1435. $last = 0;
  1436. foreach ($specs as $num => $def) {
  1437. while (++$last < $num) {
  1438. out($f, "$prolog$lastdef,\n");
  1439. }
  1440. $last = $num;
  1441. out($f, "$prolog$def,\n");
  1442. }
  1443. out($f, "$prolog$lastdef\n");
  1444. }
  1445. // Generates handler for undefined opcodes (CALL threading model)
  1446. function gen_null_handler($f) {
  1447. static $done = 0;
  1448. // New and all executors with CALL threading model can use the same handler
  1449. // for undefined opcodes, do we emit code for it only once
  1450. if (!$done) {
  1451. $done = 1;
  1452. out($f,"static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_NULL_HANDLER(ZEND_OPCODE_HANDLER_ARGS)\n");
  1453. out($f,"{\n");
  1454. out($f,"\tUSE_OPLINE\n");
  1455. out($f,"\n");
  1456. out($f,"\tSAVE_OPLINE();\n");
  1457. out($f,"\tzend_error_noreturn(E_ERROR, \"Invalid opcode %d/%d/%d.\", OPLINE->opcode, OPLINE->op1_type, OPLINE->op2_type);\n");
  1458. out($f,"\tZEND_VM_NEXT_OPCODE(); /* Never reached */\n");
  1459. out($f,"}\n\n");
  1460. }
  1461. }
  1462. function extra_spec_name($extra_spec) {
  1463. global $prefix;
  1464. $s = "";
  1465. if (isset($extra_spec["OP_DATA"])) {
  1466. $s .= "_OP_DATA" . $prefix[$extra_spec["OP_DATA"]];
  1467. }
  1468. if (isset($extra_spec["RETVAL"])) {
  1469. $s .= "_RETVAL_".($extra_spec["RETVAL"] ? "USED" : "UNUSED");
  1470. }
  1471. if (isset($extra_spec["QUICK_ARG"])) {
  1472. if ($extra_spec["QUICK_ARG"]) {
  1473. $s .= "_QUICK";
  1474. }
  1475. }
  1476. if (isset($extra_spec["SMART_BRANCH"])) {
  1477. if ($extra_spec["SMART_BRANCH"] == 1) {
  1478. $s .= "_JMPZ";
  1479. } else if ($extra_spec["SMART_BRANCH"] == 2) {
  1480. $s .= "_JMPNZ";
  1481. }
  1482. }
  1483. if (isset($extra_spec["ISSET"])) {
  1484. if ($extra_spec["ISSET"] == 0) {
  1485. $s .= "_SET";
  1486. } else {
  1487. $s .= "_EMPTY";
  1488. }
  1489. }
  1490. if (isset($extra_spec["OBSERVER"])) {
  1491. if ($extra_spec["OBSERVER"]) {
  1492. $s .= "_OBSERVER";
  1493. }
  1494. }
  1495. return $s;
  1496. }
  1497. function extra_spec_flags($extra_spec) {
  1498. $s = array();
  1499. if (isset($extra_spec["OP_DATA"])) {
  1500. $s[] = "SPEC_RULE_OP_DATA";
  1501. }
  1502. if (isset($extra_spec["RETVAL"])) {
  1503. $s[] = "SPEC_RULE_RETVAL";
  1504. }
  1505. if (isset($extra_spec["QUICK_ARG"])) {
  1506. $s[] = "SPEC_RULE_QUICK_ARG";
  1507. }
  1508. if (isset($extra_spec["SMART_BRANCH"])) {
  1509. $s[] = "SPEC_RULE_SMART_BRANCH";
  1510. }
  1511. if (isset($extra_spec["COMMUTATIVE"])) {
  1512. $s[] = "SPEC_RULE_COMMUTATIVE";
  1513. }
  1514. if (isset($extra_spec["ISSET"])) {
  1515. $s[] = "SPEC_RULE_ISSET";
  1516. }
  1517. if (isset($extra_spec["OBSERVER"])) {
  1518. $s[] = "SPEC_RULE_OBSERVER";
  1519. }
  1520. return $s;
  1521. }
  1522. function extra_spec_handler($dsc) {
  1523. global $op_types_ex;
  1524. if (!isset($dsc["spec"])) {
  1525. return array(array());
  1526. }
  1527. $specs = $dsc["spec"];
  1528. if (isset($specs["OP_DATA"])) {
  1529. $op_data_specs = $specs["OP_DATA"];
  1530. $specs["OP_DATA"] = array();
  1531. foreach ($op_types_ex as $op_data) {
  1532. if (isset($dsc["spec"]["OP_DATA"][$op_data])) {
  1533. $specs["OP_DATA"][] = $op_data;
  1534. }
  1535. }
  1536. }
  1537. $f = function($specs) use (&$f) {
  1538. $spec = key($specs);
  1539. $top = array_shift($specs);
  1540. if ($specs) {
  1541. $next = $f($specs);
  1542. } else {
  1543. $next = array(array());
  1544. }
  1545. $ret = array();
  1546. foreach ($next as $existing) {
  1547. foreach ($top as $mode) {
  1548. $ret[] = array($spec => $mode) + $existing;
  1549. }
  1550. }
  1551. return $ret;
  1552. };
  1553. return $f($specs);
  1554. }
  1555. function read_order_file($fn) {
  1556. $f = fopen($fn, "r");
  1557. if (!is_resource($f)) {
  1558. return false;
  1559. }
  1560. $order = [];
  1561. while (!feof($f)) {
  1562. $op = trim(fgets($f));
  1563. if ($op !== "") {
  1564. $order[$op] = null;
  1565. }
  1566. }
  1567. fclose($f);
  1568. return $order;
  1569. }
  1570. // Generates all opcode handlers and helpers (specialized or unspecialized)
  1571. function gen_executor_code($f, $spec, $kind, $prolog, &$switch_labels = array()) {
  1572. global $list, $opcodes, $helpers, $op_types_ex, $gen_order;
  1573. if ($spec) {
  1574. // Produce specialized executor
  1575. $op1t = $op_types_ex;
  1576. // for each op1.op_type
  1577. foreach ($op1t as $op1) {
  1578. $op2t = $op_types_ex;
  1579. // for each op2.op_type
  1580. foreach ($op2t as $op2) {
  1581. // for each handlers in helpers in original order
  1582. foreach ($list as $lineno => $dsc) {
  1583. if (isset($dsc["handler"])) {
  1584. $num = $dsc["handler"];
  1585. foreach (extra_spec_handler($opcodes[$num]) as $extra_spec) {
  1586. // Check if handler accepts such types of operands (op1 and op2)
  1587. if (isset($opcodes[$num]["op1"][$op1]) &&
  1588. isset($opcodes[$num]["op2"][$op2])) {
  1589. // Generate handler code
  1590. gen_handler($f, 1, $kind, $opcodes[$num]["op"], $op1, $op2, isset($opcodes[$num]["use"]), $opcodes[$num]["code"], $lineno, $opcodes[$num], $extra_spec, $switch_labels);
  1591. }
  1592. }
  1593. } else if (isset($dsc["helper"])) {
  1594. $num = $dsc["helper"];
  1595. foreach (extra_spec_handler($helpers[$num]) as $extra_spec) {
  1596. // Check if handler accepts such types of operands (op1 and op2)
  1597. if (isset($helpers[$num]["op1"][$op1]) &&
  1598. isset($helpers[$num]["op2"][$op2])) {
  1599. // Generate helper code
  1600. gen_helper($f, 1, $kind, $num, $op1, $op2, $helpers[$num]["param"], $helpers[$num]["code"], $lineno, $helpers[$num]["inline"], $helpers[$num]["cold"], $helpers[$num]["hot"], $extra_spec);
  1601. }
  1602. }
  1603. } else {
  1604. var_dump($dsc);
  1605. die("??? $kind:$num\n");
  1606. }
  1607. }
  1608. }
  1609. }
  1610. } else {
  1611. // Produce unspecialized executor
  1612. // for each handlers in helpers in original order
  1613. foreach ($list as $lineno => $dsc) {
  1614. if (isset($dsc["handler"])) {
  1615. $num = $dsc["handler"];
  1616. // Generate handler code
  1617. if ($num < 256) {
  1618. gen_handler($f, 0, $kind, $opcodes[$num]["op"], "ANY", "ANY", isset($opcodes[$num]["use"]), $opcodes[$num]["code"], $lineno, $opcodes[$num]);
  1619. }
  1620. } else if (isset($dsc["helper"])) {
  1621. $num = $dsc["helper"];
  1622. // Generate helper code
  1623. gen_helper($f, 0, $kind, $num, "ANY", "ANY", $helpers[$num]["param"], $helpers[$num]["code"], $lineno, $helpers[$num]["inline"], $helpers[$num]["cold"], $helpers[$num]["hot"]);
  1624. } else {
  1625. var_dump($dsc);
  1626. die("??? $kind:$num\n");
  1627. }
  1628. }
  1629. }
  1630. if (is_array($gen_order)) {
  1631. foreach ($gen_order as $txt) {
  1632. if ($txt !== null) {
  1633. out($f, $txt);
  1634. }
  1635. }
  1636. }
  1637. if (ZEND_VM_LINES) {
  1638. // Reset #line directives
  1639. out_line($f);
  1640. }
  1641. // Generate handler for undefined opcodes
  1642. switch ($kind) {
  1643. case ZEND_VM_KIND_CALL:
  1644. gen_null_handler($f);
  1645. break;
  1646. case ZEND_VM_KIND_SWITCH:
  1647. out($f,"default: ZEND_NULL_LABEL:\n");
  1648. out($f,"\tzend_error_noreturn(E_ERROR, \"Invalid opcode %d/%d/%d.\", OPLINE->opcode, OPLINE->op1_type, OPLINE->op2_type);\n");
  1649. out($f,"\tZEND_VM_NEXT_OPCODE(); /* Never reached */\n");
  1650. break;
  1651. case ZEND_VM_KIND_GOTO:
  1652. out($f,"ZEND_NULL_LABEL:\n");
  1653. out($f,"\tzend_error_noreturn(E_ERROR, \"Invalid opcode %d/%d/%d.\", OPLINE->opcode, OPLINE->op1_type, OPLINE->op2_type);\n");
  1654. out($f,"\tZEND_VM_NEXT_OPCODE(); /* Never reached */\n");
  1655. break;
  1656. case ZEND_VM_KIND_HYBRID:
  1657. out($f,"\t\t\tHYBRID_CASE(HYBRID_HALT):\n");
  1658. out($f,"#ifdef ZEND_VM_FP_GLOBAL_REG\n");
  1659. out($f,"\t\t\t\texecute_data = vm_stack_data.orig_execute_data;\n");
  1660. out($f,"#endif\n");
  1661. out($f,"#ifdef ZEND_VM_IP_GLOBAL_REG\n");
  1662. out($f,"\t\t\t\topline = vm_stack_data.orig_opline;\n");
  1663. out($f,"#endif\n");
  1664. out($f,"\t\t\t\treturn;\n");
  1665. out($f,"\t\t\tHYBRID_DEFAULT:\n");
  1666. out($f,"\t\t\t\tVM_TRACE(ZEND_NULL)\n");
  1667. out($f,"\t\t\t\tZEND_NULL_HANDLER(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);\n");
  1668. out($f,"\t\t\t\tHYBRID_BREAK(); /* Never reached */\n");
  1669. break;
  1670. }
  1671. }
  1672. function skip_blanks($f, $prolog, $epilog) {
  1673. if (trim($prolog) != "" || trim($epilog) != "") {
  1674. out($f, $prolog.$epilog);
  1675. }
  1676. }
  1677. // Generates executor from skeleton file and definition (specialized or unspecialized)
  1678. function gen_executor($f, $skl, $spec, $kind, $executor_name, $initializer_name) {
  1679. global $params, $skeleton_file, $line_no, $gen_order;
  1680. if ($kind == ZEND_VM_KIND_HYBRID && file_exists(__DIR__ . "/zend_vm_order.txt")) {
  1681. $gen_order = read_order_file(__DIR__ . "/zend_vm_order.txt");
  1682. } else {
  1683. $gen_order = null;
  1684. }
  1685. $switch_labels = array();
  1686. $lineno = 0;
  1687. foreach ($skl as $line) {
  1688. // Skeleton file contains special markers in form %NAME% those are
  1689. // substituted by custom code
  1690. if (preg_match("/(.*)[{][%]([A-Z_]*)[%][}](.*)/", $line, $m)) {
  1691. switch ($m[2]) {
  1692. case "DEFINES":
  1693. out($f,"#define SPEC_START_MASK 0x0000ffff\n");
  1694. out($f,"#define SPEC_EXTRA_MASK 0xfffc0000\n");
  1695. out($f,"#define SPEC_RULE_OP1 0x00010000\n");
  1696. out($f,"#define SPEC_RULE_OP2 0x00020000\n");
  1697. out($f,"#define SPEC_RULE_OP_DATA 0x00040000\n");
  1698. out($f,"#define SPEC_RULE_RETVAL 0x00080000\n");
  1699. out($f,"#define SPEC_RULE_QUICK_ARG 0x00100000\n");
  1700. out($f,"#define SPEC_RULE_SMART_BRANCH 0x00200000\n");
  1701. out($f,"#define SPEC_RULE_COMMUTATIVE 0x00800000\n");
  1702. out($f,"#define SPEC_RULE_ISSET 0x01000000\n");
  1703. out($f,"#define SPEC_RULE_OBSERVER 0x02000000\n");
  1704. out($f,"\n");
  1705. out($f,"static const uint32_t *zend_spec_handlers;\n");
  1706. out($f,"static const void * const *zend_opcode_handlers;\n");
  1707. out($f,"static int zend_handlers_count;\n");
  1708. if ($kind == ZEND_VM_KIND_HYBRID) {
  1709. out($f,"#if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID)\n");
  1710. out($f,"static const void * const * zend_opcode_handler_funcs;\n");
  1711. out($f,"static zend_op hybrid_halt_op;\n");
  1712. out($f,"#endif\n");
  1713. }
  1714. out($f,"#if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID) || !ZEND_VM_SPEC\n");
  1715. out($f,"static const void *zend_vm_get_opcode_handler(zend_uchar opcode, const zend_op* op);\n");
  1716. out($f,"#endif\n\n");
  1717. if ($kind == ZEND_VM_KIND_HYBRID) {
  1718. out($f,"#if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID)\n");
  1719. out($f,"static const void *zend_vm_get_opcode_handler_func(zend_uchar opcode, const zend_op* op);\n");
  1720. out($f,"#else\n");
  1721. out($f,"# define zend_vm_get_opcode_handler_func zend_vm_get_opcode_handler\n");
  1722. out($f,"#endif\n\n");
  1723. }
  1724. out($f,"#ifndef VM_TRACE\n");
  1725. if (is_array($gen_order)) {
  1726. out($f,"# define VM_TRACE(op) ZEND_VM_GUARD(op);\n");
  1727. } else {
  1728. out($f,"# define VM_TRACE(op)\n");
  1729. }
  1730. out($f,"#endif\n");
  1731. out($f,"#ifndef VM_TRACE_START\n");
  1732. out($f,"# define VM_TRACE_START()\n");
  1733. out($f,"#endif\n");
  1734. out($f,"#ifndef VM_TRACE_END\n");
  1735. out($f,"# define VM_TRACE_END()\n");
  1736. out($f,"#endif\n");
  1737. switch ($kind) {
  1738. case ZEND_VM_KIND_HYBRID:
  1739. out($f,"#if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID)\n");
  1740. out($f,"#define HYBRID_NEXT() goto *(void**)(OPLINE->handler)\n");
  1741. out($f,"#define HYBRID_SWITCH() HYBRID_NEXT();\n");
  1742. out($f,"#define HYBRID_CASE(op) op ## _LABEL\n");
  1743. out($f,"#define HYBRID_BREAK() HYBRID_NEXT()\n");
  1744. out($f,"#define HYBRID_DEFAULT ZEND_NULL_LABEL\n");
  1745. out($f,"#endif\n");
  1746. case ZEND_VM_KIND_CALL:
  1747. out($f,"\n");
  1748. out($f,"#ifdef ZEND_VM_FP_GLOBAL_REG\n");
  1749. out($f,"# define ZEND_OPCODE_HANDLER_ARGS void\n");
  1750. out($f,"# define ZEND_OPCODE_HANDLER_ARGS_PASSTHRU\n");
  1751. out($f,"# define ZEND_OPCODE_HANDLER_ARGS_DC\n");
  1752. out($f,"# define ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_CC\n");
  1753. out($f,"#else\n");
  1754. out($f,"# define ZEND_OPCODE_HANDLER_ARGS zend_execute_data *execute_data\n");
  1755. out($f,"# define ZEND_OPCODE_HANDLER_ARGS_PASSTHRU execute_data\n");
  1756. out($f,"# define ZEND_OPCODE_HANDLER_ARGS_DC , ZEND_OPCODE_HANDLER_ARGS\n");
  1757. out($f,"# define ZEND_OPCODE_HANDLER_ARGS_PASSTHRU_CC , ZEND_OPCODE_HANDLER_ARGS_PASSTHRU\n");
  1758. out($f,"#endif\n");
  1759. out($f,"\n");
  1760. out($f,"#if defined(ZEND_VM_FP_GLOBAL_REG) && defined(ZEND_VM_IP_GLOBAL_REG)\n");
  1761. out($f,"# define ZEND_OPCODE_HANDLER_RET void\n");
  1762. out($f,"# define ZEND_VM_TAIL_CALL(call) call; return\n");
  1763. out($f,"# ifdef ZEND_VM_TAIL_CALL_DISPATCH\n");
  1764. out($f,"# define ZEND_VM_CONTINUE() ((opcode_handler_t)OPLINE->handler)(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU); return\n");
  1765. out($f,"# else\n");
  1766. out($f,"# define ZEND_VM_CONTINUE() return\n");
  1767. out($f,"# endif\n");
  1768. if ($kind == ZEND_VM_KIND_HYBRID) {
  1769. out($f,"# if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID)\n");
  1770. out($f,"# define ZEND_VM_RETURN() opline = &hybrid_halt_op; return\n");
  1771. out($f,"# define ZEND_VM_HOT zend_always_inline ZEND_COLD ZEND_OPT_SIZE\n");
  1772. out($f,"# define ZEND_VM_COLD ZEND_COLD ZEND_OPT_SIZE\n");
  1773. out($f,"# else\n");
  1774. out($f,"# define ZEND_VM_RETURN() opline = NULL; return\n");
  1775. out($f,"# define ZEND_VM_HOT\n");
  1776. out($f,"# define ZEND_VM_COLD ZEND_COLD ZEND_OPT_SIZE\n");
  1777. out($f,"# endif\n");
  1778. } else {
  1779. out($f,"# define ZEND_VM_RETURN() opline = NULL; return\n");
  1780. out($f,"# define ZEND_VM_COLD ZEND_COLD ZEND_OPT_SIZE\n");
  1781. }
  1782. out($f,"#else\n");
  1783. out($f,"# define ZEND_OPCODE_HANDLER_RET int\n");
  1784. out($f,"# define ZEND_VM_TAIL_CALL(call) return call\n");
  1785. out($f,"# define ZEND_VM_CONTINUE() return 0\n");
  1786. out($f,"# define ZEND_VM_RETURN() return -1\n");
  1787. if ($kind == ZEND_VM_KIND_HYBRID) {
  1788. out($f,"# define ZEND_VM_HOT\n");
  1789. }
  1790. out($f,"# define ZEND_VM_COLD ZEND_COLD ZEND_OPT_SIZE\n");
  1791. out($f,"#endif\n");
  1792. out($f,"\n");
  1793. out($f,"typedef ZEND_OPCODE_HANDLER_RET (ZEND_FASTCALL *opcode_handler_t) (ZEND_OPCODE_HANDLER_ARGS);\n");
  1794. out($f,"\n");
  1795. out($f,"#define DCL_OPLINE\n");
  1796. out($f,"#ifdef ZEND_VM_IP_GLOBAL_REG\n");
  1797. out($f,"# define OPLINE opline\n");
  1798. out($f,"# define USE_OPLINE\n");
  1799. out($f,"# define LOAD_OPLINE() opline = EX(opline)\n");
  1800. out($f,"# define LOAD_OPLINE_EX()\n");
  1801. out($f,"# define LOAD_NEXT_OPLINE() opline = EX(opline) + 1\n");
  1802. out($f,"# define SAVE_OPLINE() EX(opline) = opline\n");
  1803. out($f,"# define SAVE_OPLINE_EX() SAVE_OPLINE()\n");
  1804. out($f,"#else\n");
  1805. out($f,"# define OPLINE EX(opline)\n");
  1806. out($f,"# define USE_OPLINE const zend_op *opline = EX(opline);\n");
  1807. out($f,"# define LOAD_OPLINE()\n");
  1808. out($f,"# define LOAD_OPLINE_EX()\n");
  1809. out($f,"# define LOAD_NEXT_OPLINE() ZEND_VM_INC_OPCODE()\n");
  1810. out($f,"# define SAVE_OPLINE()\n");
  1811. out($f,"# define SAVE_OPLINE_EX()\n");
  1812. out($f,"#endif\n");
  1813. out($f,"#define HANDLE_EXCEPTION() ZEND_ASSERT(EG(exception)); LOAD_OPLINE(); ZEND_VM_CONTINUE()\n");
  1814. out($f,"#define HANDLE_EXCEPTION_LEAVE() ZEND_ASSERT(EG(exception)); LOAD_OPLINE(); ZEND_VM_LEAVE()\n");
  1815. out($f,"#if defined(ZEND_VM_FP_GLOBAL_REG)\n");
  1816. out($f,"# define ZEND_VM_ENTER_EX() ZEND_VM_INTERRUPT_CHECK(); ZEND_VM_CONTINUE()\n");
  1817. out($f,"# define ZEND_VM_ENTER() execute_data = EG(current_execute_data); LOAD_OPLINE(); ZEND_VM_ENTER_EX()\n");
  1818. out($f,"# define ZEND_VM_LEAVE() ZEND_VM_CONTINUE()\n");
  1819. out($f,"#elif defined(ZEND_VM_IP_GLOBAL_REG)\n");
  1820. out($f,"# define ZEND_VM_ENTER_EX() return 1\n");
  1821. out($f,"# define ZEND_VM_ENTER() opline = EG(current_execute_data)->opline; ZEND_VM_ENTER_EX()\n");
  1822. out($f,"# define ZEND_VM_LEAVE() return 2\n");
  1823. out($f,"#else\n");
  1824. out($f,"# define ZEND_VM_ENTER_EX() return 1\n");
  1825. out($f,"# define ZEND_VM_ENTER() return 1\n");
  1826. out($f,"# define ZEND_VM_LEAVE() return 2\n");
  1827. out($f,"#endif\n");
  1828. out($f,"#define ZEND_VM_INTERRUPT() ZEND_VM_TAIL_CALL(zend_interrupt_helper".($spec?"_SPEC":"")."(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU));\n");
  1829. out($f,"#define ZEND_VM_LOOP_INTERRUPT() zend_interrupt_helper".($spec?"_SPEC":"")."(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);\n");
  1830. if ($kind == ZEND_VM_KIND_HYBRID) {
  1831. out($f,"#define ZEND_VM_DISPATCH(opcode, opline) ZEND_VM_TAIL_CALL(((opcode_handler_t)zend_vm_get_opcode_handler_func(opcode, opline))(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU));\n");
  1832. } else {
  1833. out($f,"#define ZEND_VM_DISPATCH(opcode, opline) ZEND_VM_TAIL_CALL(((opcode_handler_t)zend_vm_get_opcode_handler(opcode, opline))(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU));\n");
  1834. }
  1835. out($f,"\n");
  1836. out($f,"static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL zend_interrupt_helper".($spec?"_SPEC":"")."(ZEND_OPCODE_HANDLER_ARGS);\n");
  1837. out($f,"static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_NULL_HANDLER(ZEND_OPCODE_HANDLER_ARGS);\n");
  1838. out($f,"\n");
  1839. break;
  1840. case ZEND_VM_KIND_SWITCH:
  1841. out($f,"\n");
  1842. out($f,"#define OPLINE opline\n");
  1843. out($f,"#ifdef ZEND_VM_IP_GLOBAL_REG\n");
  1844. out($f,"# define DCL_OPLINE register const zend_op *opline __asm__(ZEND_VM_IP_GLOBAL_REG);\n");
  1845. out($f,"#else\n");
  1846. out($f,"# define DCL_OPLINE const zend_op *opline;\n");
  1847. out($f,"#endif\n");
  1848. out($f,"#define USE_OPLINE\n");
  1849. out($f,"#define LOAD_OPLINE() opline = EX(opline)\n");
  1850. out($f,"# define LOAD_OPLINE_EX() LOAD_OPLINE()\n");
  1851. out($f,"#define LOAD_NEXT_OPLINE() opline = EX(opline) + 1\n");
  1852. out($f,"#define SAVE_OPLINE() EX(opline) = opline\n");
  1853. out($f,"#define SAVE_OPLINE_EX()\n");
  1854. out($f,"#define HANDLE_EXCEPTION() ZEND_ASSERT(EG(exception)); LOAD_OPLINE(); ZEND_VM_CONTINUE()\n");
  1855. out($f,"#define HANDLE_EXCEPTION_LEAVE() ZEND_ASSERT(EG(exception)); LOAD_OPLINE(); ZEND_VM_LEAVE()\n");
  1856. out($f,"#define ZEND_VM_CONTINUE() goto zend_vm_continue\n");
  1857. out($f,"#define ZEND_VM_RETURN() return\n");
  1858. out($f,"#define ZEND_VM_ENTER_EX() ZEND_VM_INTERRUPT_CHECK(); ZEND_VM_CONTINUE()\n");
  1859. out($f,"#define ZEND_VM_ENTER() execute_data = EG(current_execute_data); LOAD_OPLINE(); ZEND_VM_ENTER_EX()\n");
  1860. out($f,"#define ZEND_VM_LEAVE() ZEND_VM_CONTINUE()\n");
  1861. out($f,"#define ZEND_VM_INTERRUPT() goto zend_interrupt_helper".($spec?"_SPEC":"").";\n");
  1862. out($f,"#define ZEND_VM_LOOP_INTERRUPT() goto zend_interrupt_helper".($spec?"_SPEC":"").";\n");
  1863. out($f,"#define ZEND_VM_DISPATCH(opcode, opline) dispatch_handler = zend_vm_get_opcode_handler(opcode, opline); goto zend_vm_dispatch;\n");
  1864. out($f,"\n");
  1865. break;
  1866. case ZEND_VM_KIND_GOTO:
  1867. out($f,"\n");
  1868. out($f,"#define OPLINE opline\n");
  1869. out($f,"#ifdef ZEND_VM_IP_GLOBAL_REG\n");
  1870. out($f,"# define DCL_OPLINE register const zend_op *opline __asm__(ZEND_VM_IP_GLOBAL_REG);\n");
  1871. out($f,"#else\n");
  1872. out($f,"# define DCL_OPLINE const zend_op *opline;\n");
  1873. out($f,"#endif\n");
  1874. out($f,"#define USE_OPLINE\n");
  1875. out($f,"#define LOAD_OPLINE() opline = EX(opline)\n");
  1876. out($f,"#define LOAD_OPLINE_EX() LOAD_OPLINE()\n");
  1877. out($f,"#define LOAD_NEXT_OPLINE() opline = EX(opline) + 1\n");
  1878. out($f,"#define SAVE_OPLINE() EX(opline) = opline\n");
  1879. out($f,"#define SAVE_OPLINE_EX()\n");
  1880. if (ZEND_VM_SPEC) {
  1881. out($f,"#define HANDLE_EXCEPTION() ZEND_ASSERT(EG(exception)); goto ZEND_HANDLE_EXCEPTION_SPEC_LABEL\n");
  1882. out($f,"#define HANDLE_EXCEPTION_LEAVE() ZEND_ASSERT(EG(exception)); goto ZEND_HANDLE_EXCEPTION_SPEC_LABEL\n");
  1883. } else {
  1884. out($f,"#define HANDLE_EXCEPTION() ZEND_ASSERT(EG(exception)); goto ZEND_HANDLE_EXCEPTION_LABEL\n");
  1885. out($f,"#define HANDLE_EXCEPTION_LEAVE() ZEND_ASSERT(EG(exception)); goto ZEND_HANDLE_EXCEPTION_LABEL\n");
  1886. }
  1887. out($f,"#define ZEND_VM_CONTINUE() goto *(void**)(OPLINE->handler)\n");
  1888. out($f,"#define ZEND_VM_RETURN() return\n");
  1889. out($f,"#define ZEND_VM_ENTER_EX() ZEND_VM_INTERRUPT_CHECK(); ZEND_VM_CONTINUE()\n");
  1890. out($f,"#define ZEND_VM_ENTER() execute_data = EG(current_execute_data); LOAD_OPLINE(); ZEND_VM_ENTER_EX()\n");
  1891. out($f,"#define ZEND_VM_LEAVE() ZEND_VM_CONTINUE()\n");
  1892. out($f,"#define ZEND_VM_INTERRUPT() goto zend_interrupt_helper".($spec?"_SPEC":"").";\n");
  1893. out($f,"#define ZEND_VM_LOOP_INTERRUPT() goto zend_interrupt_helper".($spec?"_SPEC":"").";\n");
  1894. out($f,"#define ZEND_VM_DISPATCH(opcode, opline) goto *(void**)(zend_vm_get_opcode_handler(opcode, opline));\n");
  1895. out($f,"\n");
  1896. break;
  1897. }
  1898. if ($kind == ZEND_VM_KIND_HYBRID) {
  1899. gen_executor_code($f, $spec, ZEND_VM_KIND_CALL, $m[1]);
  1900. out($f,"\n");
  1901. out($f,"#if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID)\n");
  1902. out($f,"# undef ZEND_VM_TAIL_CALL\n");
  1903. out($f,"# undef ZEND_VM_CONTINUE\n");
  1904. out($f,"# undef ZEND_VM_RETURN\n");
  1905. // out($f,"# undef ZEND_VM_INTERRUPT\n");
  1906. out($f,"\n");
  1907. out($f,"# define ZEND_VM_TAIL_CALL(call) call; ZEND_VM_CONTINUE()\n");
  1908. out($f,"# define ZEND_VM_CONTINUE() HYBRID_NEXT()\n");
  1909. out($f,"# define ZEND_VM_RETURN() goto HYBRID_HALT_LABEL\n");
  1910. // out($f,"# define ZEND_VM_INTERRUPT() goto zend_interrupt_helper_SPEC_LABEL\n");
  1911. out($f,"#endif\n\n");
  1912. }
  1913. break;
  1914. case "EXECUTOR_NAME":
  1915. out($f, $m[1].$executor_name.$m[3]."\n");
  1916. break;
  1917. case "HELPER_VARS":
  1918. if ($kind == ZEND_VM_KIND_SWITCH) {
  1919. out($f,$m[1]."const void *dispatch_handler;\n");
  1920. }
  1921. if ($kind != ZEND_VM_KIND_CALL && count($params)) {
  1922. if ($kind == ZEND_VM_KIND_HYBRID) {
  1923. out($f, "#if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID)\n");
  1924. }
  1925. // Emit local variables those are used for helpers' parameters
  1926. foreach ($params as $param => $x) {
  1927. out($f,$m[1].$param.";\n");
  1928. }
  1929. if ($kind == ZEND_VM_KIND_HYBRID) {
  1930. out($f, "#endif\n");
  1931. }
  1932. }
  1933. if ($kind != ZEND_VM_KIND_CALL && $kind != ZEND_VM_KIND_HYBRID) {
  1934. out($f,"#ifdef ZEND_VM_FP_GLOBAL_REG\n");
  1935. out($f,$m[1]."register zend_execute_data *execute_data __asm__(ZEND_VM_FP_GLOBAL_REG) = ex;\n");
  1936. out($f,"#else\n");
  1937. out($f,$m[1]."zend_execute_data *execute_data = ex;\n");
  1938. out($f,"#endif\n");
  1939. } else {
  1940. out($f,"#if defined(ZEND_VM_IP_GLOBAL_REG) || defined(ZEND_VM_FP_GLOBAL_REG)\n");
  1941. out($f,$m[1]."struct {\n");
  1942. out($f,"#ifdef ZEND_VM_IP_GLOBAL_REG\n");
  1943. out($f,$m[1]."\tconst zend_op *orig_opline;\n");
  1944. out($f,"#endif\n");
  1945. out($f,"#ifdef ZEND_VM_FP_GLOBAL_REG\n");
  1946. out($f,$m[1]."\tzend_execute_data *orig_execute_data;\n");
  1947. out($f,"#ifdef ZEND_VM_HYBRID_JIT_RED_ZONE_SIZE\n");
  1948. out($f,$m[1]."\tchar hybrid_jit_red_zone[ZEND_VM_HYBRID_JIT_RED_ZONE_SIZE];\n");
  1949. out($f,"#endif\n");
  1950. out($f,"#endif\n");
  1951. out($f,$m[1]."} vm_stack_data;\n");
  1952. out($f,"#endif\n");
  1953. out($f,"#ifdef ZEND_VM_IP_GLOBAL_REG\n");
  1954. out($f,$m[1]."vm_stack_data.orig_opline = opline;\n");
  1955. out($f,"#endif\n");
  1956. out($f,"#ifdef ZEND_VM_FP_GLOBAL_REG\n");
  1957. out($f,$m[1]."vm_stack_data.orig_execute_data = execute_data;\n");
  1958. out($f,$m[1]."execute_data = ex;\n");
  1959. out($f,"#else\n");
  1960. out($f,$m[1]."zend_execute_data *execute_data = ex;\n");
  1961. out($f,"#endif\n");
  1962. }
  1963. break;
  1964. case "INTERNAL_LABELS":
  1965. if ($kind == ZEND_VM_KIND_GOTO || $kind == ZEND_VM_KIND_HYBRID) {
  1966. // Emit array of labels of opcode handlers and code for
  1967. // zend_opcode_handlers initialization
  1968. if ($kind == ZEND_VM_KIND_HYBRID) {
  1969. out($f,"#if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID)\n");
  1970. }
  1971. $prolog = $m[1];
  1972. out($f,$prolog."if (UNEXPECTED(execute_data == NULL)) {\n");
  1973. out($f,$prolog."\tstatic const void * const labels[] = {\n");
  1974. gen_labels($f, $spec, ($kind == ZEND_VM_KIND_HYBRID) ? ZEND_VM_KIND_GOTO : $kind, $prolog."\t\t", $specs);
  1975. out($f,$prolog."\t};\n");
  1976. out($f,$prolog."\tzend_opcode_handlers = (const void **) labels;\n");
  1977. out($f,$prolog."\tzend_handlers_count = sizeof(labels) / sizeof(void*);\n");
  1978. if ($kind == ZEND_VM_KIND_HYBRID) {
  1979. out($f,$prolog."\tmemset(&hybrid_halt_op, 0, sizeof(hybrid_halt_op));\n");
  1980. out($f,$prolog."\thybrid_halt_op.handler = (void*)&&HYBRID_HALT_LABEL;\n");
  1981. out($f,"#ifdef ZEND_VM_HYBRID_JIT_RED_ZONE_SIZE\n");
  1982. out($f,$prolog."\tmemset(vm_stack_data.hybrid_jit_red_zone, 0, ZEND_VM_HYBRID_JIT_RED_ZONE_SIZE);\n");
  1983. out($f,"#endif\n");
  1984. out($f,$prolog."\tif (zend_touch_vm_stack_data) {\n");
  1985. out($f,$prolog."\t\tzend_touch_vm_stack_data(&vm_stack_data);\n");
  1986. out($f,$prolog."\t}\n");
  1987. out($f,$prolog."\tgoto HYBRID_HALT_LABEL;\n");
  1988. } else {
  1989. out($f,$prolog."\treturn;\n");
  1990. }
  1991. out($f,$prolog."}\n");
  1992. if ($kind == ZEND_VM_KIND_HYBRID) {
  1993. out($f,"#endif\n");
  1994. }
  1995. } else {
  1996. skip_blanks($f, $m[1], $m[3]);
  1997. }
  1998. break;
  1999. case "ZEND_VM_CONTINUE_LABEL":
  2000. if ($kind == ZEND_VM_KIND_CALL || $kind == ZEND_VM_KIND_HYBRID) {
  2001. // Only SWITCH dispatch method use it
  2002. out($f,"#if !defined(ZEND_VM_FP_GLOBAL_REG) || !defined(ZEND_VM_IP_GLOBAL_REG)\n");
  2003. out($f,$m[1]."\tint ret;".$m[3]."\n");
  2004. out($f,"#endif\n");
  2005. } else if ($kind == ZEND_VM_KIND_SWITCH) {
  2006. // Only SWITCH dispatch method use it
  2007. out($f,"zend_vm_continue:".$m[3]."\n");
  2008. } else {
  2009. skip_blanks($f, $m[1], $m[3]);
  2010. }
  2011. break;
  2012. case "ZEND_VM_DISPATCH":
  2013. // Emit code that dispatches to opcode handler
  2014. switch ($kind) {
  2015. case ZEND_VM_KIND_SWITCH:
  2016. out($f, $m[1]."dispatch_handler = OPLINE->handler;\nzend_vm_dispatch:\n".$m[1]."switch ((int)(uintptr_t)dispatch_handler)".$m[3]."\n");
  2017. break;
  2018. case ZEND_VM_KIND_GOTO:
  2019. out($f, $m[1]."goto *(void**)(OPLINE->handler);".$m[3]."\n");
  2020. break;
  2021. case ZEND_VM_KIND_HYBRID:
  2022. out($f,"#if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID)\n");
  2023. out($f, $m[1]."HYBRID_SWITCH()".$m[3]."\n");
  2024. out($f,"#else\n");
  2025. case ZEND_VM_KIND_CALL:
  2026. out($f,"#if defined(ZEND_VM_FP_GLOBAL_REG) && defined(ZEND_VM_IP_GLOBAL_REG)\n");
  2027. out($f, $m[1]."((opcode_handler_t)OPLINE->handler)(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);\n");
  2028. out($f, $m[1]."if (UNEXPECTED(!OPLINE))".$m[3]."\n");
  2029. out($f,"#else\n");
  2030. out($f, $m[1]."if (UNEXPECTED((ret = ((opcode_handler_t)OPLINE->handler)(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU)) != 0))".$m[3]."\n");
  2031. out($f,"#endif\n");
  2032. if ($kind == ZEND_VM_KIND_HYBRID) {
  2033. out($f,"#endif\n");
  2034. }
  2035. break;
  2036. }
  2037. break;
  2038. case "INTERNAL_EXECUTOR":
  2039. if ($kind != ZEND_VM_KIND_CALL) {
  2040. // Emit executor code
  2041. if ($kind == ZEND_VM_KIND_HYBRID) {
  2042. out($f,"#if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID)\n");
  2043. }
  2044. gen_executor_code($f, $spec, $kind, $m[1], $switch_labels);
  2045. }
  2046. if ($kind == ZEND_VM_KIND_CALL || $kind == ZEND_VM_KIND_HYBRID) {
  2047. // Executor is defined as a set of functions
  2048. if ($kind == ZEND_VM_KIND_HYBRID) {
  2049. out($f,"#else\n");
  2050. }
  2051. out($f,
  2052. "#ifdef ZEND_VM_FP_GLOBAL_REG\n" .
  2053. $m[1]."execute_data = vm_stack_data.orig_execute_data;\n" .
  2054. "# ifdef ZEND_VM_IP_GLOBAL_REG\n" .
  2055. $m[1]."opline = vm_stack_data.orig_opline;\n" .
  2056. "# endif\n" .
  2057. $m[1]."return;\n" .
  2058. "#else\n" .
  2059. $m[1]."if (EXPECTED(ret > 0)) {\n" .
  2060. $m[1]."\texecute_data = EG(current_execute_data);\n".
  2061. $m[1]."\tZEND_VM_LOOP_INTERRUPT_CHECK();\n".
  2062. $m[1]."} else {\n" .
  2063. "# ifdef ZEND_VM_IP_GLOBAL_REG\n" .
  2064. $m[1]."\topline = vm_stack_data.orig_opline;\n" .
  2065. "# endif\n".
  2066. $m[1]."\treturn;\n".
  2067. $m[1]."}\n".
  2068. "#endif\n");
  2069. if ($kind == ZEND_VM_KIND_HYBRID) {
  2070. out($f,"#endif\n");
  2071. }
  2072. }
  2073. break;
  2074. case "EXTERNAL_EXECUTOR":
  2075. if ($kind == ZEND_VM_KIND_CALL) {
  2076. gen_executor_code($f, $spec, $kind, $m[1]);
  2077. }
  2078. break;
  2079. case "INITIALIZER_NAME":
  2080. out($f, $m[1].$initializer_name.$m[3]."\n");
  2081. break;
  2082. case "EXTERNAL_LABELS":
  2083. // Emit code that initializes zend_opcode_handlers array
  2084. $prolog = $m[1];
  2085. if ($kind == ZEND_VM_KIND_GOTO) {
  2086. // Labels are defined in the executor itself, so we call it
  2087. // with execute_data NULL and it sets zend_opcode_handlers array
  2088. out($f,$prolog."static const uint32_t specs[] = {\n");
  2089. gen_specs($f, $prolog."\t", $specs);
  2090. out($f,$prolog."};\n");
  2091. out($f,$prolog."zend_spec_handlers = specs;\n");
  2092. out($f,$prolog.$executor_name."_ex(NULL);\n");
  2093. } else {
  2094. out($f,$prolog."static const void * const labels[] = {\n");
  2095. gen_labels($f, $spec, ($kind == ZEND_VM_KIND_HYBRID) ? ZEND_VM_KIND_CALL : $kind, $prolog."\t", $specs, $switch_labels);
  2096. out($f,$prolog."};\n");
  2097. out($f,$prolog."static const uint32_t specs[] = {\n");
  2098. gen_specs($f, $prolog."\t", $specs);
  2099. out($f,$prolog."};\n");
  2100. if ($kind == ZEND_VM_KIND_HYBRID) {
  2101. out($f,"#if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID)\n");
  2102. out($f,$prolog."zend_opcode_handler_funcs = labels;\n");
  2103. out($f,$prolog."zend_spec_handlers = specs;\n");
  2104. out($f,$prolog.$executor_name."_ex(NULL);\n");
  2105. out($f,"#else\n");
  2106. }
  2107. out($f,$prolog."zend_opcode_handlers = labels;\n");
  2108. out($f,$prolog."zend_handlers_count = sizeof(labels) / sizeof(void*);\n");
  2109. out($f,$prolog."zend_spec_handlers = specs;\n");
  2110. if ($kind == ZEND_VM_KIND_HYBRID) {
  2111. out($f,"#endif\n");
  2112. }
  2113. }
  2114. break;
  2115. default:
  2116. die("ERROR: Unknown keyword ".$m[2]." in skeleton file.\n");
  2117. }
  2118. } else {
  2119. // Copy the line as is
  2120. out($f, $line);
  2121. }
  2122. }
  2123. }
  2124. function parse_operand_spec($def, $lineno, $str, &$flags) {
  2125. global $vm_op_decode;
  2126. $flags = 0;
  2127. $a = explode("|",$str);
  2128. foreach ($a as $val) {
  2129. if (isset($vm_op_decode[$val])) {
  2130. $flags |= $vm_op_decode[$val];
  2131. } else {
  2132. die("ERROR ($def:$lineno): Wrong operand type '$str'\n");
  2133. }
  2134. }
  2135. if (!($flags & ZEND_VM_OP_SPEC)) {
  2136. if (count($a) != 1) {
  2137. die("ERROR ($def:$lineno): Wrong operand type '$str'\n");
  2138. }
  2139. $a = array("ANY");
  2140. }
  2141. return array_flip($a);
  2142. }
  2143. function parse_ext_spec($def, $lineno, $str) {
  2144. global $vm_ext_decode;
  2145. $flags = 0;
  2146. $a = explode("|",$str);
  2147. foreach ($a as $val) {
  2148. if (isset($vm_ext_decode[$val])) {
  2149. $flags |= $vm_ext_decode[$val];
  2150. } else {
  2151. die("ERROR ($def:$lineno): Wrong extended_value type '$str'\n");
  2152. }
  2153. }
  2154. return $flags;
  2155. }
  2156. function parse_spec_rules($def, $lineno, $str) {
  2157. global $used_extra_spec;
  2158. $ret = array();
  2159. $a = explode(",", $str);
  2160. foreach ($a as $rule) {
  2161. $n = strpos($rule, "=");
  2162. if ($n !== false) {
  2163. $id = trim(substr($rule, 0, $n));
  2164. $val = trim(substr($rule, $n+1));
  2165. switch ($id) {
  2166. case "OP_DATA":
  2167. $ret["OP_DATA"] = parse_operand_spec($def, $lineno, $val, $devnull);
  2168. break;
  2169. default:
  2170. die("ERROR ($def:$lineno): Wrong specialization rules '$str'\n");
  2171. }
  2172. $used_extra_spec[$id] = 1;
  2173. } else {
  2174. switch ($rule) {
  2175. case "RETVAL":
  2176. $ret["RETVAL"] = array(0, 1);
  2177. break;
  2178. case "QUICK_ARG":
  2179. $ret["QUICK_ARG"] = array(0, 1);
  2180. break;
  2181. case "SMART_BRANCH":
  2182. $ret["SMART_BRANCH"] = array(0, 1, 2);
  2183. break;
  2184. case "NO_CONST_CONST":
  2185. $ret["NO_CONST_CONST"] = array(1);
  2186. break;
  2187. case "COMMUTATIVE":
  2188. $ret["COMMUTATIVE"] = array(1);
  2189. break;
  2190. case "ISSET":
  2191. $ret["ISSET"] = array(0, 1);
  2192. break;
  2193. case "OBSERVER":
  2194. $ret["OBSERVER"] = array(0, 1);
  2195. break;
  2196. default:
  2197. die("ERROR ($def:$lineno): Wrong specialization rules '$str'\n");
  2198. }
  2199. $used_extra_spec[$rule] = 1;
  2200. }
  2201. }
  2202. return $ret;
  2203. }
  2204. function gen_vm_opcodes_header(
  2205. array $opcodes, int $max_opcode, int $max_opcode_len, array $vm_op_flags
  2206. ): string {
  2207. $str = HEADER_TEXT;
  2208. $str .= "#ifndef ZEND_VM_OPCODES_H\n#define ZEND_VM_OPCODES_H\n\n";
  2209. $str .= "#define ZEND_VM_SPEC\t\t" . ZEND_VM_SPEC . "\n";
  2210. $str .= "#define ZEND_VM_LINES\t\t" . ZEND_VM_LINES . "\n";
  2211. $str .= "#define ZEND_VM_KIND_CALL\t" . ZEND_VM_KIND_CALL . "\n";
  2212. $str .= "#define ZEND_VM_KIND_SWITCH\t" . ZEND_VM_KIND_SWITCH . "\n";
  2213. $str .= "#define ZEND_VM_KIND_GOTO\t" . ZEND_VM_KIND_GOTO . "\n";
  2214. $str .= "#define ZEND_VM_KIND_HYBRID\t" . ZEND_VM_KIND_HYBRID . "\n";
  2215. if ($GLOBALS["vm_kind_name"][ZEND_VM_KIND] === "ZEND_VM_KIND_HYBRID") {
  2216. $str .= "/* HYBRID requires support for computed GOTO and global register variables*/\n";
  2217. $str .= "#if (defined(__GNUC__) && defined(HAVE_GCC_GLOBAL_REGS))\n";
  2218. $str .= "# define ZEND_VM_KIND\t\tZEND_VM_KIND_HYBRID\n";
  2219. $str .= "#else\n";
  2220. $str .= "# define ZEND_VM_KIND\t\tZEND_VM_KIND_CALL\n";
  2221. $str .= "#endif\n";
  2222. } else {
  2223. $str .= "#define ZEND_VM_KIND\t\t" . $GLOBALS["vm_kind_name"][ZEND_VM_KIND] . "\n";
  2224. }
  2225. $str .= "\n";
  2226. $str .= "#if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) && !defined(__SANITIZE_ADDRESS__)\n";
  2227. $str .= "# if ((defined(i386) && !defined(__PIC__)) || defined(__x86_64__) || defined(_M_X64))\n";
  2228. $str .= "# define ZEND_VM_HYBRID_JIT_RED_ZONE_SIZE 16\n";
  2229. $str .= "# endif\n";
  2230. $str .= "#endif\n";
  2231. $str .= "\n";
  2232. foreach ($vm_op_flags as $name => $val) {
  2233. $str .= sprintf("#define %-24s 0x%08x\n", $name, $val);
  2234. }
  2235. $str .= "#define ZEND_VM_OP1_FLAGS(flags) (flags & 0xff)\n";
  2236. $str .= "#define ZEND_VM_OP2_FLAGS(flags) ((flags >> 8) & 0xff)\n";
  2237. $str .= "\n";
  2238. $str .= "BEGIN_EXTERN_C()\n\n";
  2239. $str .= "ZEND_API const char* ZEND_FASTCALL zend_get_opcode_name(zend_uchar opcode);\n";
  2240. $str .= "ZEND_API uint32_t ZEND_FASTCALL zend_get_opcode_flags(zend_uchar opcode);\n";
  2241. $str .= "ZEND_API zend_uchar zend_get_opcode_id(const char *name, size_t length);\n\n";
  2242. $str .= "END_EXTERN_C()\n\n";
  2243. $code_len = strlen((string) $max_opcode);
  2244. foreach ($opcodes as $code => $dsc) {
  2245. $code = str_pad((string)$code, $code_len, " ", STR_PAD_LEFT);
  2246. $op = str_pad($dsc["op"], $max_opcode_len);
  2247. if ($code <= $max_opcode) {
  2248. $str .= "#define $op $code\n";
  2249. }
  2250. }
  2251. $code = str_pad((string)$max_opcode, $code_len, " ", STR_PAD_LEFT);
  2252. $op = str_pad("ZEND_VM_LAST_OPCODE", $max_opcode_len);
  2253. $str .= "\n#define $op $code\n";
  2254. $str .= "\n#endif\n";
  2255. return $str;
  2256. }
  2257. function gen_vm($def, $skel) {
  2258. global $definition_file, $skeleton_file, $executor_file,
  2259. $op_types, $list, $opcodes, $helpers, $params, $opnames,
  2260. $vm_op_flags, $used_extra_spec;
  2261. // Load definition file
  2262. $in = @file($def);
  2263. if (!$in) {
  2264. die("ERROR: Can not open definition file '$def'\n");
  2265. }
  2266. // We need absolute path to definition file to use it in #line directives
  2267. $definition_file = realpath($def);
  2268. // Load skeleton file
  2269. $skl = @file($skel);
  2270. if (!$skl) {
  2271. die("ERROR: Can not open skeleton file '$skel'\n");
  2272. }
  2273. // We need absolute path to skeleton file to use it in #line directives
  2274. $skeleton_file = realpath($skel);
  2275. // Parse definition file into tree
  2276. $lineno = 0;
  2277. $handler = null;
  2278. $helper = null;
  2279. $max_opcode_len = 0;
  2280. $max_opcode = 0;
  2281. $extra_num = 256;
  2282. foreach ($in as $line) {
  2283. ++$lineno;
  2284. if (strpos($line,"ZEND_VM_HANDLER(") === 0 ||
  2285. strpos($line,"ZEND_VM_INLINE_HANDLER(") === 0 ||
  2286. strpos($line,"ZEND_VM_HOT_HANDLER(") === 0 ||
  2287. strpos($line,"ZEND_VM_HOT_NOCONST_HANDLER(") === 0 ||
  2288. strpos($line,"ZEND_VM_HOT_NOCONSTCONST_HANDLER(") === 0 ||
  2289. strpos($line,"ZEND_VM_HOT_SEND_HANDLER(") === 0 ||
  2290. strpos($line,"ZEND_VM_HOT_OBJ_HANDLER(") === 0 ||
  2291. strpos($line,"ZEND_VM_COLD_HANDLER(") === 0 ||
  2292. strpos($line,"ZEND_VM_COLD_CONST_HANDLER(") === 0 ||
  2293. strpos($line,"ZEND_VM_COLD_CONSTCONST_HANDLER(") === 0) {
  2294. // Parsing opcode handler's definition
  2295. if (preg_match(
  2296. "/^ZEND_VM_(HOT_|INLINE_|HOT_OBJ_|HOT_SEND_|HOT_NOCONST_|HOT_NOCONSTCONST_|COLD_|COLD_CONST_|COLD_CONSTCONST_)?HANDLER\(\s*([0-9]+)\s*,\s*([A-Z_]+)\s*,\s*([A-Z_|]+)\s*,\s*([A-Z_|]+)\s*(,\s*([A-Z_|]+)\s*)?(,\s*SPEC\(([A-Z_|=,]+)\)\s*)?\)/",
  2297. $line,
  2298. $m) == 0) {
  2299. die("ERROR ($def:$lineno): Invalid ZEND_VM_HANDLER definition.\n");
  2300. }
  2301. $hot = !empty($m[1]) ? $m[1] : false;
  2302. $code = (int)$m[2];
  2303. $op = $m[3];
  2304. $len = strlen($op);
  2305. $op1 = parse_operand_spec($def, $lineno, $m[4], $flags1);
  2306. $op2 = parse_operand_spec($def, $lineno, $m[5], $flags2);
  2307. $flags = $flags1 | ($flags2 << 8);
  2308. if (!empty($m[7])) {
  2309. $flags |= parse_ext_spec($def, $lineno, $m[7]);
  2310. }
  2311. if ($len > $max_opcode_len) {
  2312. $max_opcode_len = $len;
  2313. }
  2314. if ($code > $max_opcode) {
  2315. $max_opcode = $code;
  2316. }
  2317. if (isset($opcodes[$code])) {
  2318. die("ERROR ($def:$lineno): Opcode with code '$code' is already defined.\n");
  2319. }
  2320. if (isset($opnames[$op])) {
  2321. die("ERROR ($def:$lineno): Opcode with name '$op' is already defined.\n");
  2322. }
  2323. $opcodes[$code] = array("op"=>$op,"op1"=>$op1,"op2"=>$op2,"code"=>"","flags"=>$flags,"hot"=>$hot);
  2324. if (isset($m[9])) {
  2325. $opcodes[$code]["spec"] = parse_spec_rules($def, $lineno, $m[9]);
  2326. if (isset($opcodes[$code]["spec"]["NO_CONST_CONST"])) {
  2327. $opcodes[$code]["flags"] |= $vm_op_flags["ZEND_VM_NO_CONST_CONST"];
  2328. }
  2329. if (isset($opcodes[$code]["spec"]["COMMUTATIVE"])) {
  2330. $opcodes[$code]["flags"] |= $vm_op_flags["ZEND_VM_COMMUTATIVE"];
  2331. }
  2332. }
  2333. $opnames[$op] = $code;
  2334. $handler = $code;
  2335. $helper = null;
  2336. $list[$lineno] = array("handler"=>$handler);
  2337. } else if (strpos($line,"ZEND_VM_TYPE_SPEC_HANDLER(") === 0 ||
  2338. strpos($line,"ZEND_VM_INLINE_TYPE_SPEC_HANDLER(") === 0 ||
  2339. strpos($line,"ZEND_VM_HOT_TYPE_SPEC_HANDLER(") === 0 ||
  2340. strpos($line,"ZEND_VM_HOT_NOCONST_TYPE_SPEC_HANDLER(") === 0 ||
  2341. strpos($line,"ZEND_VM_HOT_NOCONSTCONST_TYPE_SPEC_HANDLER(") === 0 ||
  2342. strpos($line,"ZEND_VM_HOT_SEND_TYPE_SPEC_HANDLER(") === 0 ||
  2343. strpos($line,"ZEND_VM_HOT_OBJ_TYPE_SPEC_HANDLER(") === 0) {
  2344. // Parsing opcode handler's definition
  2345. if (preg_match(
  2346. "/^ZEND_VM_(HOT_|INLINE_|HOT_OBJ_|HOT_SEND_|HOT_NOCONST_|HOT_NOCONSTCONST_)?TYPE_SPEC_HANDLER\(\s*([A-Z_|]+)\s*,\s*((?:[^(,]|\([^()]*|(?R)*\))*),\s*([A-Za-z_]+)\s*,\s*([A-Z_|]+)\s*,\s*([A-Z_|]+)\s*(,\s*([A-Z_|]+)\s*)?(,\s*SPEC\(([A-Z_|=,]+)\)\s*)?\)/",
  2347. $line,
  2348. $m) == 0) {
  2349. die("ERROR ($def:$lineno): Invalid ZEND_VM_TYPE_HANDLER_HANDLER definition.\n");
  2350. }
  2351. $hot = !empty($m[1]) ? $m[1] : false;
  2352. $orig_op_list = $m[2];
  2353. $code = $extra_num++;
  2354. foreach (explode('|', $orig_op_list) as $orig_op) {
  2355. if (!isset($opnames[$orig_op])) {
  2356. die("ERROR ($def:$lineno): Opcode with name '$orig_op' is not defined.\n");
  2357. }
  2358. $orig_code = $opnames[$orig_op];
  2359. $condition = $m[3];
  2360. $opcodes[$orig_code]['type_spec'][$code] = $condition;
  2361. }
  2362. $op = $m[4];
  2363. $op1 = parse_operand_spec($def, $lineno, $m[5], $flags1);
  2364. $op2 = parse_operand_spec($def, $lineno, $m[6], $flags2);
  2365. $flags = $flags1 | ($flags2 << 8);
  2366. if (!empty($m[8])) {
  2367. $flags |= parse_ext_spec($def, $lineno, $m[8]);
  2368. }
  2369. if (isset($opcodes[$code])) {
  2370. die("ERROR ($def:$lineno): Opcode with name '$code' is already defined.\n");
  2371. }
  2372. $used_extra_spec["TYPE"] = 1;
  2373. $opcodes[$code] = array("op"=>$op,"op1"=>$op1,"op2"=>$op2,"code"=>"","flags"=>$flags,"hot"=>$hot,"is_type_spec"=>true);
  2374. if (isset($m[10])) {
  2375. $opcodes[$code]["spec"] = parse_spec_rules($def, $lineno, $m[10]);
  2376. if (isset($opcodes[$code]["spec"]["NO_CONST_CONST"])) {
  2377. $opcodes[$code]["flags"] |= $vm_op_flags["ZEND_VM_NO_CONST_CONST"];
  2378. }
  2379. if (isset($opcodes[$code]["spec"]["COMMUTATIVE"])) {
  2380. $opcodes[$code]["flags"] |= $vm_op_flags["ZEND_VM_COMMUTATIVE"];
  2381. }
  2382. }
  2383. $opnames[$op] = $code;
  2384. $handler = $code;
  2385. $helper = null;
  2386. $list[$lineno] = array("handler"=>$handler);
  2387. } else if (strpos($line,"ZEND_VM_HELPER(") === 0 ||
  2388. strpos($line,"ZEND_VM_INLINE_HELPER(") === 0 ||
  2389. strpos($line,"ZEND_VM_COLD_HELPER(") === 0 ||
  2390. strpos($line,"ZEND_VM_HOT_HELPER(") === 0) {
  2391. // Parsing helper's definition
  2392. if (preg_match(
  2393. "/^ZEND_VM(_INLINE|_COLD|_HOT)?_HELPER\(\s*([A-Za-z_]+)\s*,\s*([A-Z_|]+)\s*,\s*([A-Z_|]+)\s*(?:,\s*SPEC\(([A-Z_|=,]+)\)\s*)?(?:,\s*([^)]*)\s*)?\)/",
  2394. $line,
  2395. $m) == 0) {
  2396. die("ERROR ($def:$lineno): Invalid ZEND_VM_HELPER definition.\n");
  2397. }
  2398. $inline = !empty($m[1]) && $m[1] === "_INLINE";
  2399. $cold = !empty($m[1]) && $m[1] === "_COLD";
  2400. $hot = !empty($m[1]) && $m[1] === "_HOT";
  2401. $helper = $m[2];
  2402. $op1 = parse_operand_spec($def, $lineno, $m[3], $flags1);
  2403. $op2 = parse_operand_spec($def, $lineno, $m[4], $flags2);
  2404. $param = isset($m[6]) ? $m[6] : null;
  2405. if (isset($helpers[$helper])) {
  2406. die("ERROR ($def:$lineno): Helper with name '$helper' is already defined.\n");
  2407. }
  2408. // Store parameters
  2409. if ((ZEND_VM_KIND == ZEND_VM_KIND_GOTO
  2410. || ZEND_VM_KIND == ZEND_VM_KIND_SWITCH
  2411. || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID && $hot))
  2412. && $param) {
  2413. foreach (explode(",", $param ) as $p) {
  2414. $p = trim($p);
  2415. if ($p !== "") {
  2416. $params[$p] = 1;
  2417. }
  2418. }
  2419. }
  2420. $helpers[$helper] = array("op1"=>$op1,"op2"=>$op2,"param"=>$param,"code"=>"","inline"=>$inline,"cold"=>$cold,"hot"=>$hot);
  2421. if (!empty($m[5])) {
  2422. $helpers[$helper]["spec"] = parse_spec_rules($def, $lineno, $m[5]);
  2423. }
  2424. $handler = null;
  2425. $list[$lineno] = array("helper"=>$helper);
  2426. } else if (strpos($line,"ZEND_VM_DEFINE_OP(") === 0) {
  2427. if (preg_match(
  2428. "/^ZEND_VM_DEFINE_OP\(\s*([0-9]+)\s*,\s*([A-Z_]+)\s*\);/",
  2429. $line,
  2430. $m) == 0) {
  2431. die("ERROR ($def:$lineno): Invalid ZEND_VM_DEFINE_OP definition.\n");
  2432. }
  2433. $code = (int)$m[1];
  2434. $op = $m[2];
  2435. $len = strlen($op);
  2436. if ($len > $max_opcode_len) {
  2437. $max_opcode_len = $len;
  2438. }
  2439. if ($code > $max_opcode) {
  2440. $max_opcode = $code;
  2441. }
  2442. if (isset($opcodes[$code])) {
  2443. die("ERROR ($def:$lineno): Opcode with code '$code' is already defined.\n");
  2444. }
  2445. if (isset($opnames[$op])) {
  2446. die("ERROR ($def:$lineno): Opcode with name '$op' is already defined.\n");
  2447. }
  2448. $opcodes[$code] = array("op"=>$op,"code"=>"");
  2449. $opnames[$op] = $code;
  2450. } else if ($handler !== null) {
  2451. // Add line of code to current opcode handler
  2452. $opcodes[$handler]["code"] .= $line;
  2453. } else if ($helper !== null) {
  2454. // Add line of code to current helper
  2455. $helpers[$helper]["code"] .= $line;
  2456. }
  2457. }
  2458. ksort($opcodes);
  2459. // Search for opcode handlers those are used by other opcode handlers
  2460. foreach ($opcodes as $dsc) {
  2461. if (preg_match("/^\s*{\s*ZEND_VM_DISPATCH_TO_HANDLER\(\s*([A-Z_]*)\s*\)\s*;\s*}\s*/", $dsc["code"], $m)) {
  2462. $op = $m[1];
  2463. if (!isset($opnames[$op])) {
  2464. die("ERROR ($def:$lineno): Opcode with name '$op' is not defined.\n");
  2465. }
  2466. $opcodes[$opnames[$dsc['op']]]['alias'] = $op;
  2467. if (!ZEND_VM_SPEC && ZEND_VM_KIND == ZEND_VM_KIND_SWITCH) {
  2468. $code = $opnames[$op];
  2469. $opcodes[$code]['use'] = 1;
  2470. }
  2471. } else if (preg_match_all("/ZEND_VM_DISPATCH_TO_HANDLER\(\s*([A-Z_]*)\s*\)/m", $dsc["code"], $mm, PREG_SET_ORDER)) {
  2472. foreach ($mm as $m) {
  2473. $op = $m[1];
  2474. if (!isset($opnames[$op])) {
  2475. die("ERROR ($def:$lineno): Opcode with name '$op' is not defined.\n");
  2476. }
  2477. $code = $opnames[$op];
  2478. $opcodes[$code]['use'] = 1;
  2479. }
  2480. }
  2481. }
  2482. // Generate opcode #defines (zend_vm_opcodes.h)
  2483. $str = gen_vm_opcodes_header($opcodes, $max_opcode, $max_opcode_len, $vm_op_flags);
  2484. write_file_if_changed(__DIR__ . "/zend_vm_opcodes.h", $str);
  2485. echo "zend_vm_opcodes.h generated successfully.\n";
  2486. // zend_vm_opcodes.c
  2487. $f = fopen(__DIR__ . "/zend_vm_opcodes.c", "w+") or die("ERROR: Cannot create zend_vm_opcodes.c\n");
  2488. // Insert header
  2489. out($f, HEADER_TEXT);
  2490. fputs($f,"#include <stdio.h>\n");
  2491. fputs($f,"#include <zend.h>\n");
  2492. fputs($f,"#include <zend_vm_opcodes.h>\n\n");
  2493. fputs($f,"static const char *zend_vm_opcodes_names[".($max_opcode + 1)."] = {\n");
  2494. for ($i = 0; $i <= $max_opcode; $i++) {
  2495. fputs($f,"\t".(isset($opcodes[$i]["op"])?'"'.$opcodes[$i]["op"].'"':"NULL").",\n");
  2496. }
  2497. fputs($f, "};\n\n");
  2498. fputs($f,"static uint32_t zend_vm_opcodes_flags[".($max_opcode + 1)."] = {\n");
  2499. for ($i = 0; $i <= $max_opcode; $i++) {
  2500. fprintf($f, "\t0x%08x,\n", isset($opcodes[$i]["flags"]) ? $opcodes[$i]["flags"] : 0);
  2501. }
  2502. fputs($f, "};\n\n");
  2503. fputs($f, "ZEND_API const char* ZEND_FASTCALL zend_get_opcode_name(zend_uchar opcode) {\n");
  2504. fputs($f, "\tif (UNEXPECTED(opcode > ZEND_VM_LAST_OPCODE)) {\n");
  2505. fputs($f, "\t\treturn NULL;\n");
  2506. fputs($f, "\t}\n");
  2507. fputs($f, "\treturn zend_vm_opcodes_names[opcode];\n");
  2508. fputs($f, "}\n");
  2509. fputs($f, "ZEND_API uint32_t ZEND_FASTCALL zend_get_opcode_flags(zend_uchar opcode) {\n");
  2510. fputs($f, "\tif (UNEXPECTED(opcode > ZEND_VM_LAST_OPCODE)) {\n");
  2511. fputs($f, "\t\topcode = ZEND_NOP;\n");
  2512. fputs($f, "\t}\n");
  2513. fputs($f, "\treturn zend_vm_opcodes_flags[opcode];\n");
  2514. fputs($f, "}\n");
  2515. fputs($f, "ZEND_API zend_uchar zend_get_opcode_id(const char *name, size_t length) {\n");
  2516. fputs($f, "\tzend_uchar opcode;\n");
  2517. fputs($f, "\tfor (opcode = 0; opcode < (sizeof(zend_vm_opcodes_names) / sizeof(zend_vm_opcodes_names[0])) - 1; opcode++) {\n");
  2518. fputs($f, "\t\tconst char *opcode_name = zend_vm_opcodes_names[opcode];\n");
  2519. fputs($f, "\t\tif (opcode_name && strncmp(opcode_name, name, length) == 0) {\n");
  2520. fputs($f, "\t\t\treturn opcode;\n");
  2521. fputs($f, "\t\t}\n");
  2522. fputs($f, "\t}\n");
  2523. fputs($f, "\treturn ZEND_VM_LAST_OPCODE + 1;\n");
  2524. fputs($f, "}\n");
  2525. fclose($f);
  2526. echo "zend_vm_opcodes.c generated successfully.\n";
  2527. // Generate zend_vm_execute.h
  2528. $f = fopen(__DIR__ . "/zend_vm_execute.h", "w+") or die("ERROR: Cannot create zend_vm_execute.h\n");
  2529. $executor_file = realpath(__DIR__ . "/zend_vm_execute.h");
  2530. // Insert header
  2531. out($f, HEADER_TEXT);
  2532. out($f, "#ifdef ZEND_WIN32\n");
  2533. // Suppress free_op1 warnings on Windows
  2534. out($f, "# pragma warning(disable : 4101)\n");
  2535. if (ZEND_VM_SPEC) {
  2536. // Suppress (<non-zero constant> || <expression>) warnings on windows
  2537. out($f, "# pragma warning(once : 6235)\n");
  2538. // Suppress (<zero> && <expression>) warnings on windows
  2539. out($f, "# pragma warning(once : 6237)\n");
  2540. // Suppress (<non-zero constant> && <expression>) warnings on windows
  2541. out($f, "# pragma warning(once : 6239)\n");
  2542. // Suppress (<expression> && <non-zero constant>) warnings on windows
  2543. out($f, "# pragma warning(once : 6240)\n");
  2544. // Suppress (<non-zero constant> || <non-zero constant>) warnings on windows
  2545. out($f, "# pragma warning(once : 6285)\n");
  2546. // Suppress (<non-zero constant> || <expression>) warnings on windows
  2547. out($f, "# pragma warning(once : 6286)\n");
  2548. // Suppress constant with constant comparison warnings on windows
  2549. out($f, "# pragma warning(once : 6326)\n");
  2550. }
  2551. out($f, "#endif\n");
  2552. // Support for ZEND_USER_OPCODE
  2553. out($f, "static user_opcode_handler_t zend_user_opcode_handlers[256] = {\n");
  2554. for ($i = 0; $i < 255; ++$i) {
  2555. out($f, "\t(user_opcode_handler_t)NULL,\n");
  2556. }
  2557. out($f, "\t(user_opcode_handler_t)NULL\n};\n\n");
  2558. out($f, "static zend_uchar zend_user_opcodes[256] = {");
  2559. for ($i = 0; $i < 255; ++$i) {
  2560. if ($i % 16 == 1) out($f, "\n\t");
  2561. out($f, "$i,");
  2562. }
  2563. out($f, "255\n};\n\n");
  2564. // Generate specialized executor
  2565. gen_executor($f, $skl, ZEND_VM_SPEC, ZEND_VM_KIND, "execute", "zend_vm_init");
  2566. out($f, "\n");
  2567. // Generate zend_vm_get_opcode_handler() function
  2568. out($f, "static uint32_t ZEND_FASTCALL zend_vm_get_opcode_handler_idx(uint32_t spec, const zend_op* op)\n");
  2569. out($f, "{\n");
  2570. if (!ZEND_VM_SPEC) {
  2571. out($f, "\treturn spec;\n");
  2572. } else {
  2573. out($f, "\tstatic const int zend_vm_decode[] = {\n");
  2574. out($f, "\t\t_UNUSED_CODE, /* 0 = IS_UNUSED */\n");
  2575. out($f, "\t\t_CONST_CODE, /* 1 = IS_CONST */\n");
  2576. out($f, "\t\t_TMP_CODE, /* 2 = IS_TMP_VAR */\n");
  2577. out($f, "\t\t_UNUSED_CODE, /* 3 */\n");
  2578. out($f, "\t\t_VAR_CODE, /* 4 = IS_VAR */\n");
  2579. out($f, "\t\t_UNUSED_CODE, /* 5 */\n");
  2580. out($f, "\t\t_UNUSED_CODE, /* 6 */\n");
  2581. out($f, "\t\t_UNUSED_CODE, /* 7 */\n");
  2582. out($f, "\t\t_CV_CODE /* 8 = IS_CV */\n");
  2583. out($f, "\t};\n");
  2584. out($f, "\tuint32_t offset = 0;\n");
  2585. out($f, "\tif (spec & SPEC_RULE_OP1) offset = offset * 5 + zend_vm_decode[op->op1_type];\n");
  2586. out($f, "\tif (spec & SPEC_RULE_OP2) offset = offset * 5 + zend_vm_decode[op->op2_type];\n");
  2587. if (isset($used_extra_spec["OP_DATA"]) ||
  2588. isset($used_extra_spec["RETVAL"]) ||
  2589. isset($used_extra_spec["QUICK_ARG"]) ||
  2590. isset($used_extra_spec["SMART_BRANCH"]) ||
  2591. isset($used_extra_spec["ISSET"]) ||
  2592. isset($used_extra_spec["OBSERVER"])) {
  2593. $else = "";
  2594. out($f, "\tif (spec & SPEC_EXTRA_MASK) {\n");
  2595. if (isset($used_extra_spec["RETVAL"])) {
  2596. out($f, "\t\t{$else}if (spec & SPEC_RULE_RETVAL) {\n");
  2597. out($f, "\t\t\toffset = offset * 2 + (op->result_type != IS_UNUSED);\n");
  2598. out($f, "\t\t\tif ((spec & SPEC_RULE_OBSERVER) && ZEND_OBSERVER_ENABLED) {\n");
  2599. out($f, "\t\t\t\toffset += 2;\n");
  2600. out($f, "\t\t\t}\n");
  2601. $else = "} else ";
  2602. }
  2603. if (isset($used_extra_spec["QUICK_ARG"])) {
  2604. out($f, "\t\t{$else}if (spec & SPEC_RULE_QUICK_ARG) {\n");
  2605. out($f, "\t\t\toffset = offset * 2 + (op->op2.num <= MAX_ARG_FLAG_NUM);\n");
  2606. $else = "} else ";
  2607. }
  2608. if (isset($used_extra_spec["OP_DATA"])) {
  2609. out($f, "\t\t{$else}if (spec & SPEC_RULE_OP_DATA) {\n");
  2610. out($f, "\t\t\toffset = offset * 5 + zend_vm_decode[(op + 1)->op1_type];\n");
  2611. $else = "} else ";
  2612. }
  2613. if (isset($used_extra_spec["ISSET"])) {
  2614. out($f, "\t\t{$else}if (spec & SPEC_RULE_ISSET) {\n");
  2615. out($f, "\t\t\toffset = offset * 2 + (op->extended_value & ZEND_ISEMPTY);\n");
  2616. $else = "} else ";
  2617. }
  2618. if (isset($used_extra_spec["SMART_BRANCH"])) {
  2619. out($f, "\t\t{$else}if (spec & SPEC_RULE_SMART_BRANCH) {\n");
  2620. out($f, "\t\t\toffset = offset * 3;\n");
  2621. out($f, "\t\t\tif (op->result_type == (IS_SMART_BRANCH_JMPZ|IS_TMP_VAR)) {\n");
  2622. out($f, "\t\t\t\toffset += 1;\n");
  2623. out($f, "\t\t\t} else if (op->result_type == (IS_SMART_BRANCH_JMPNZ|IS_TMP_VAR)) {\n");
  2624. out($f, "\t\t\t\toffset += 2;\n");
  2625. out($f, "\t\t\t}\n");
  2626. $else = "} else ";
  2627. }
  2628. if (isset($used_extra_spec["OBSERVER"])) {
  2629. out($f, "\t\t{$else}if (spec & SPEC_RULE_OBSERVER) {\n");
  2630. out($f, "\t\t\toffset = offset * 2;\n");
  2631. out($f, "\t\t\tif (ZEND_OBSERVER_ENABLED) {\n");
  2632. out($f, "\t\t\t\toffset += 1;\n");
  2633. out($f, "\t\t\t}\n");
  2634. $else = "} else ";
  2635. }
  2636. if ($else !== "") {
  2637. out($f, "\t\t}\n");
  2638. }
  2639. out($f, "\t}\n");
  2640. }
  2641. out($f, "\treturn (spec & SPEC_START_MASK) + offset;\n");
  2642. }
  2643. out($f, "}\n\n");
  2644. out($f, "#if (ZEND_VM_KIND != ZEND_VM_KIND_HYBRID) || !ZEND_VM_SPEC\n");
  2645. out($f, "static const void *zend_vm_get_opcode_handler(zend_uchar opcode, const zend_op* op)\n");
  2646. out($f, "{\n");
  2647. if (!ZEND_VM_SPEC) {
  2648. out($f, "\treturn zend_opcode_handlers[zend_vm_get_opcode_handler_idx(opcode, op)];\n");
  2649. } else {
  2650. out($f, "\treturn zend_opcode_handlers[zend_vm_get_opcode_handler_idx(zend_spec_handlers[opcode], op)];\n");
  2651. }
  2652. out($f, "}\n");
  2653. out($f, "#endif\n\n");
  2654. if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
  2655. // Generate zend_vm_get_opcode_handler_func() function
  2656. out($f, "#if ZEND_VM_KIND == ZEND_VM_KIND_HYBRID\n");
  2657. out($f,"static const void *zend_vm_get_opcode_handler_func(zend_uchar opcode, const zend_op* op)\n");
  2658. out($f, "{\n");
  2659. out($f, "\tuint32_t spec = zend_spec_handlers[opcode];\n");
  2660. if (!ZEND_VM_SPEC) {
  2661. out($f, "\treturn zend_opcode_handler_funcs[spec];\n");
  2662. } else {
  2663. out($f, "\treturn zend_opcode_handler_funcs[zend_vm_get_opcode_handler_idx(spec, op)];\n");
  2664. }
  2665. out($f, "}\n\n");
  2666. out($f, "#endif\n\n");
  2667. }
  2668. // Generate zend_vm_get_opcode_handler() function
  2669. out($f, "ZEND_API void ZEND_FASTCALL zend_vm_set_opcode_handler(zend_op* op)\n");
  2670. out($f, "{\n");
  2671. out($f, "\tzend_uchar opcode = zend_user_opcodes[op->opcode];\n");
  2672. if (!ZEND_VM_SPEC) {
  2673. out($f, "\top->handler = zend_opcode_handlers[zend_vm_get_opcode_handler_idx(opcode, op)];\n");
  2674. } else {
  2675. out($f, "\n");
  2676. out($f, "\tif (zend_spec_handlers[op->opcode] & SPEC_RULE_COMMUTATIVE) {\n");
  2677. out($f, "\t\tif (op->op1_type < op->op2_type) {\n");
  2678. out($f, "\t\t\tzend_swap_operands(op);\n");
  2679. out($f, "\t\t}\n");
  2680. out($f, "\t}\n");
  2681. out($f, "\top->handler = zend_opcode_handlers[zend_vm_get_opcode_handler_idx(zend_spec_handlers[opcode], op)];\n");
  2682. }
  2683. out($f, "}\n\n");
  2684. // Generate zend_vm_set_opcode_handler_ex() function
  2685. out($f, "ZEND_API void ZEND_FASTCALL zend_vm_set_opcode_handler_ex(zend_op* op, uint32_t op1_info, uint32_t op2_info, uint32_t res_info)\n");
  2686. out($f, "{\n");
  2687. out($f, "\tzend_uchar opcode = zend_user_opcodes[op->opcode];\n");
  2688. if (!ZEND_VM_SPEC) {
  2689. out($f, "\top->handler = zend_opcode_handlers[zend_vm_get_opcode_handler_idx(opcode, op)];\n");
  2690. } else {
  2691. out($f, "\tuint32_t spec = zend_spec_handlers[opcode];\n");
  2692. if (isset($used_extra_spec["TYPE"])) {
  2693. out($f, "\tswitch (opcode) {\n");
  2694. foreach ($opcodes as $code => $dsc) {
  2695. if (isset($dsc['type_spec'])) {
  2696. $orig_op = $dsc['op'];
  2697. out($f, "\t\tcase $orig_op:\n");
  2698. if (isset($dsc["spec"]["COMMUTATIVE"])) {
  2699. out($f, "\t\t\tif (op->op1_type < op->op2_type) {\n");
  2700. out($f, "\t\t\t\tzend_swap_operands(op);\n");
  2701. out($f, "\t\t\t}\n");
  2702. }
  2703. $first = true;
  2704. foreach ($dsc['type_spec'] as $code => $condition) {
  2705. $condition = format_condition($condition);
  2706. if ($first) {
  2707. out($f, "\t\t\tif $condition {\n");
  2708. $first = false;
  2709. } else {
  2710. out($f, "\t\t\t} else if $condition {\n");
  2711. }
  2712. $spec_dsc = $opcodes[$code];
  2713. if (isset($spec_dsc["spec"]["NO_CONST_CONST"])) {
  2714. out($f, "\t\t\t\tif (op->op1_type == IS_CONST && op->op2_type == IS_CONST) {\n");
  2715. out($f, "\t\t\t\t\tbreak;\n");
  2716. out($f, "\t\t\t\t}\n");
  2717. }
  2718. out($f, "\t\t\t\tspec = ${spec_dsc['spec_code']};\n");
  2719. if (isset($spec_dsc["spec"]["COMMUTATIVE"]) && !isset($dsc["spec"]["COMMUTATIVE"])) {
  2720. out($f, "\t\t\t\tif (op->op1_type < op->op2_type) {\n");
  2721. out($f, "\t\t\t\t\tzend_swap_operands(op);\n");
  2722. out($f, "\t\t\t\t}\n");
  2723. }
  2724. }
  2725. if (!$first) {
  2726. out($f, "\t\t\t}\n");
  2727. }
  2728. out($f, "\t\t\tbreak;\n");
  2729. }
  2730. }
  2731. $has_commutative = false;
  2732. foreach ($opcodes as $code => $dsc) {
  2733. if (!isset($dsc['is_type_spec']) &&
  2734. !isset($dsc['type_spec']) &&
  2735. isset($dsc["spec"]["COMMUTATIVE"])) {
  2736. $orig_op = $dsc['op'];
  2737. out($f, "\t\tcase $orig_op:\n");
  2738. $has_commutative = true;
  2739. }
  2740. }
  2741. if ($has_commutative) {
  2742. out($f, "\t\t\tif (op->op1_type < op->op2_type) {\n");
  2743. out($f, "\t\t\t\tzend_swap_operands(op);\n");
  2744. out($f, "\t\t\t}\n");
  2745. out($f, "\t\t\tbreak;\n");
  2746. out($f, "\t\tcase ZEND_USER_OPCODE:\n");
  2747. out($f, "\t\t\tif (zend_spec_handlers[op->opcode] & SPEC_RULE_COMMUTATIVE) {\n");
  2748. out($f, "\t\t\t\tif (op->op1_type < op->op2_type) {\n");
  2749. out($f, "\t\t\t\t\tzend_swap_operands(op);\n");
  2750. out($f, "\t\t\t\t}\n");
  2751. out($f, "\t\t\t}\n");
  2752. out($f, "\t\t\tbreak;\n");
  2753. }
  2754. out($f, "\t\tdefault:\n");
  2755. out($f, "\t\t\tbreak;\n");
  2756. out($f, "\t}\n");
  2757. }
  2758. out($f, "\top->handler = zend_opcode_handlers[zend_vm_get_opcode_handler_idx(spec, op)];\n");
  2759. }
  2760. out($f, "}\n\n");
  2761. // Generate zend_vm_call_opcode_handler() function
  2762. if (ZEND_VM_KIND == ZEND_VM_KIND_CALL || ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
  2763. out($f, "ZEND_API int ZEND_FASTCALL zend_vm_call_opcode_handler(zend_execute_data* ex)\n");
  2764. out($f, "{\n");
  2765. if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
  2766. out($f,"#if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID)\n");
  2767. out($f, "\topcode_handler_t handler;\n");
  2768. out($f,"#endif\n");
  2769. }
  2770. out($f, "\tint ret;\n");
  2771. out($f, "#ifdef ZEND_VM_IP_GLOBAL_REG\n");
  2772. out($f, "\tconst zend_op *orig_opline = opline;\n");
  2773. out($f, "#endif\n");
  2774. out($f, "#ifdef ZEND_VM_FP_GLOBAL_REG\n");
  2775. out($f, "\tzend_execute_data *orig_execute_data = execute_data;\n");
  2776. out($f, "\texecute_data = ex;\n");
  2777. out($f, "#else\n");
  2778. out($f, "\tzend_execute_data *execute_data = ex;\n");
  2779. out($f, "#endif\n");
  2780. out($f, "\n");
  2781. out($f, "\tLOAD_OPLINE();\n");
  2782. out($f,"#if defined(ZEND_VM_FP_GLOBAL_REG) && defined(ZEND_VM_IP_GLOBAL_REG)\n");
  2783. if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
  2784. out($f,"#if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID)\n");
  2785. out($f, "\thandler = (opcode_handler_t)zend_vm_get_opcode_handler_func(zend_user_opcodes[opline->opcode], opline);\n");
  2786. out($f, "\thandler(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);\n");
  2787. out($f, "\tif (EXPECTED(opline != &hybrid_halt_op)) {\n");
  2788. out($f,"#else\n");
  2789. }
  2790. out($f, "\t((opcode_handler_t)OPLINE->handler)(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);\n");
  2791. if (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID) {
  2792. out($f, "\tif (EXPECTED(opline)) {\n");
  2793. out($f,"#endif\n");
  2794. } else {
  2795. out($f, "\tif (EXPECTED(opline)) {\n");
  2796. }
  2797. out($f, "\t\tret = execute_data != ex ? (int)(execute_data->prev_execute_data != ex) + 1 : 0;\n");
  2798. out($f, "\t\tSAVE_OPLINE();\n");
  2799. out($f, "\t} else {\n");
  2800. out($f, "\t\tret = -1;\n");
  2801. out($f, "\t}\n");
  2802. out($f, "#else\n");
  2803. out($f, "\tret = ((opcode_handler_t)OPLINE->handler)(ZEND_OPCODE_HANDLER_ARGS_PASSTHRU);\n");
  2804. out($f, "\tSAVE_OPLINE();\n");
  2805. out($f, "#endif\n");
  2806. out($f, "#ifdef ZEND_VM_FP_GLOBAL_REG\n");
  2807. out($f, "\texecute_data = orig_execute_data;\n");
  2808. out($f, "#endif\n");
  2809. out($f, "#ifdef ZEND_VM_IP_GLOBAL_REG\n");
  2810. out($f, "\topline = orig_opline;\n");
  2811. out($f, "#endif\n");
  2812. out($f, "\treturn ret;\n");
  2813. out($f, "}\n\n");
  2814. } else {
  2815. out($f, "ZEND_API int ZEND_FASTCALL zend_vm_call_opcode_handler(zend_execute_data* ex)\n");
  2816. out($f, "{\n");
  2817. out($f, "\tzend_error_noreturn(E_CORE_ERROR, \"zend_vm_call_opcode_handler() is not supported\");\n");
  2818. out($f, "\treturn 0;\n");
  2819. out($f, "}\n\n");
  2820. }
  2821. fclose($f);
  2822. echo "zend_vm_execute.h generated successfully.\n";
  2823. }
  2824. function write_file_if_changed(string $filename, string $contents) {
  2825. if (file_exists($filename)) {
  2826. $orig_contents = file_get_contents($filename);
  2827. if ($orig_contents === $contents) {
  2828. // Unchanged, no need to write.
  2829. return;
  2830. }
  2831. }
  2832. file_put_contents($filename, $contents);
  2833. }
  2834. function usage() {
  2835. echo("\nUsage: php zend_vm_gen.php [options]\n".
  2836. "\nOptions:".
  2837. "\n --with-vm-kind=CALL|SWITCH|GOTO|HYBRID - select threading model (default is HYBRID)".
  2838. "\n --without-specializer - disable executor specialization".
  2839. "\n --with-lines - enable #line directives".
  2840. "\n\n");
  2841. }
  2842. // Parse arguments
  2843. for ($i = 1; $i < $argc; $i++) {
  2844. if (strpos($argv[$i],"--with-vm-kind=") === 0) {
  2845. $kind = substr($argv[$i], strlen("--with-vm-kind="));
  2846. switch ($kind) {
  2847. case "CALL":
  2848. define("ZEND_VM_KIND", ZEND_VM_KIND_CALL);
  2849. break;
  2850. case "SWITCH":
  2851. define("ZEND_VM_KIND", ZEND_VM_KIND_SWITCH);
  2852. break;
  2853. case "GOTO":
  2854. define("ZEND_VM_KIND", ZEND_VM_KIND_GOTO);
  2855. break;
  2856. case "HYBRID":
  2857. define("ZEND_VM_KIND", ZEND_VM_KIND_HYBRID);
  2858. break;
  2859. default:
  2860. echo("ERROR: Invalid vm kind '$kind'\n");
  2861. usage();
  2862. die();
  2863. }
  2864. } else if ($argv[$i] == "--without-specializer") {
  2865. // Disabling specialization
  2866. define("ZEND_VM_SPEC", 0);
  2867. } else if ($argv[$i] == "--with-lines") {
  2868. // Enabling debugging using original zend_vm_def.h
  2869. define("ZEND_VM_LINES", 1);
  2870. } else if ($argv[$i] == "--help") {
  2871. usage();
  2872. exit();
  2873. } else {
  2874. echo("ERROR: Invalid option '".$argv[$i]."'\n");
  2875. usage();
  2876. die();
  2877. }
  2878. }
  2879. // Using defaults
  2880. if (!defined("ZEND_VM_KIND")) {
  2881. // Using CALL threading by default
  2882. define("ZEND_VM_KIND", ZEND_VM_KIND_HYBRID);
  2883. }
  2884. if (!defined("ZEND_VM_SPEC")) {
  2885. // Using specialized executor by default
  2886. define("ZEND_VM_SPEC", 1);
  2887. }
  2888. if (!defined("ZEND_VM_LINES")) {
  2889. // Disabling #line directives
  2890. define("ZEND_VM_LINES", 0);
  2891. }
  2892. gen_vm(__DIR__ . "/zend_vm_def.h", __DIR__ . "/zend_vm_execute.skl");