zend_vm_gen.php 111 KB

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