zend_opcode.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. | Dmitry Stogov <dmitry@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include <stdio.h>
  21. #include "zend.h"
  22. #include "zend_alloc.h"
  23. #include "zend_compile.h"
  24. #include "zend_extensions.h"
  25. #include "zend_API.h"
  26. #include "zend_sort.h"
  27. #include "zend_constants.h"
  28. #include "zend_vm.h"
  29. static void zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array)
  30. {
  31. if (extension->op_array_ctor) {
  32. extension->op_array_ctor(op_array);
  33. }
  34. }
  35. static void zend_extension_op_array_dtor_handler(zend_extension *extension, zend_op_array *op_array)
  36. {
  37. if (extension->op_array_dtor) {
  38. extension->op_array_dtor(op_array);
  39. }
  40. }
  41. void init_op_array(zend_op_array *op_array, zend_uchar type, int initial_ops_size)
  42. {
  43. op_array->type = type;
  44. op_array->arg_flags[0] = 0;
  45. op_array->arg_flags[1] = 0;
  46. op_array->arg_flags[2] = 0;
  47. op_array->refcount = (uint32_t *) emalloc(sizeof(uint32_t));
  48. *op_array->refcount = 1;
  49. op_array->last = 0;
  50. op_array->opcodes = emalloc(initial_ops_size * sizeof(zend_op));
  51. op_array->last_var = 0;
  52. op_array->vars = NULL;
  53. op_array->T = 0;
  54. op_array->function_name = NULL;
  55. op_array->filename = zend_string_copy(zend_get_compiled_filename());
  56. op_array->doc_comment = NULL;
  57. op_array->attributes = NULL;
  58. op_array->arg_info = NULL;
  59. op_array->num_args = 0;
  60. op_array->required_num_args = 0;
  61. op_array->scope = NULL;
  62. op_array->prototype = NULL;
  63. op_array->live_range = NULL;
  64. op_array->try_catch_array = NULL;
  65. op_array->last_live_range = 0;
  66. op_array->static_variables = NULL;
  67. ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
  68. op_array->last_try_catch = 0;
  69. op_array->fn_flags = 0;
  70. op_array->last_literal = 0;
  71. op_array->literals = NULL;
  72. op_array->num_dynamic_func_defs = 0;
  73. op_array->dynamic_func_defs = NULL;
  74. ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
  75. op_array->cache_size = zend_op_array_extension_handles * sizeof(void*);
  76. memset(op_array->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
  77. if (zend_extension_flags & ZEND_EXTENSIONS_HAVE_OP_ARRAY_CTOR) {
  78. zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_ctor_handler, op_array);
  79. }
  80. }
  81. ZEND_API void destroy_zend_function(zend_function *function)
  82. {
  83. zval tmp;
  84. ZVAL_PTR(&tmp, function);
  85. zend_function_dtor(&tmp);
  86. }
  87. ZEND_API void zend_type_release(zend_type type, bool persistent) {
  88. if (ZEND_TYPE_HAS_LIST(type)) {
  89. zend_type *list_type;
  90. ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) {
  91. if (ZEND_TYPE_HAS_NAME(*list_type)) {
  92. zend_string_release(ZEND_TYPE_NAME(*list_type));
  93. }
  94. } ZEND_TYPE_LIST_FOREACH_END();
  95. if (!ZEND_TYPE_USES_ARENA(type)) {
  96. pefree(ZEND_TYPE_LIST(type), persistent);
  97. }
  98. } else if (ZEND_TYPE_HAS_NAME(type)) {
  99. zend_string_release(ZEND_TYPE_NAME(type));
  100. }
  101. }
  102. void zend_free_internal_arg_info(zend_internal_function *function) {
  103. if ((function->fn_flags & (ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_HAS_TYPE_HINTS)) &&
  104. function->arg_info) {
  105. uint32_t i;
  106. uint32_t num_args = function->num_args + 1;
  107. zend_internal_arg_info *arg_info = function->arg_info - 1;
  108. if (function->fn_flags & ZEND_ACC_VARIADIC) {
  109. num_args++;
  110. }
  111. for (i = 0 ; i < num_args; i++) {
  112. zend_type_release(arg_info[i].type, /* persistent */ 1);
  113. }
  114. free(arg_info);
  115. }
  116. }
  117. ZEND_API void zend_function_dtor(zval *zv)
  118. {
  119. zend_function *function = Z_PTR_P(zv);
  120. if (function->type == ZEND_USER_FUNCTION) {
  121. ZEND_ASSERT(function->common.function_name);
  122. destroy_op_array(&function->op_array);
  123. /* op_arrays are allocated on arena, so we don't have to free them */
  124. } else {
  125. ZEND_ASSERT(function->type == ZEND_INTERNAL_FUNCTION);
  126. ZEND_ASSERT(function->common.function_name);
  127. zend_string_release_ex(function->common.function_name, 1);
  128. /* For methods this will be called explicitly. */
  129. if (!function->common.scope) {
  130. zend_free_internal_arg_info(&function->internal_function);
  131. if (function->common.attributes) {
  132. zend_hash_release(function->common.attributes);
  133. function->common.attributes = NULL;
  134. }
  135. }
  136. if (!(function->common.fn_flags & ZEND_ACC_ARENA_ALLOCATED)) {
  137. pefree(function, 1);
  138. }
  139. }
  140. }
  141. ZEND_API void zend_cleanup_internal_class_data(zend_class_entry *ce)
  142. {
  143. if (ZEND_MAP_PTR(ce->static_members_table) && CE_STATIC_MEMBERS(ce)) {
  144. zval *static_members = CE_STATIC_MEMBERS(ce);
  145. zval *p = static_members;
  146. zval *end = p + ce->default_static_members_count;
  147. ZEND_MAP_PTR_SET(ce->static_members_table, NULL);
  148. while (p != end) {
  149. if (UNEXPECTED(Z_ISREF_P(p))) {
  150. zend_property_info *prop_info;
  151. ZEND_REF_FOREACH_TYPE_SOURCES(Z_REF_P(p), prop_info) {
  152. if (prop_info->ce == ce && p - static_members == prop_info->offset) {
  153. ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(p), prop_info);
  154. break; /* stop iteration here, the array might be realloc()'ed */
  155. }
  156. } ZEND_REF_FOREACH_TYPE_SOURCES_END();
  157. }
  158. i_zval_ptr_dtor(p);
  159. p++;
  160. }
  161. efree(static_members);
  162. }
  163. }
  164. static void _destroy_zend_class_traits_info(zend_class_entry *ce)
  165. {
  166. uint32_t i;
  167. for (i = 0; i < ce->num_traits; i++) {
  168. zend_string_release_ex(ce->trait_names[i].name, 0);
  169. zend_string_release_ex(ce->trait_names[i].lc_name, 0);
  170. }
  171. efree(ce->trait_names);
  172. if (ce->trait_aliases) {
  173. i = 0;
  174. while (ce->trait_aliases[i]) {
  175. if (ce->trait_aliases[i]->trait_method.method_name) {
  176. zend_string_release_ex(ce->trait_aliases[i]->trait_method.method_name, 0);
  177. }
  178. if (ce->trait_aliases[i]->trait_method.class_name) {
  179. zend_string_release_ex(ce->trait_aliases[i]->trait_method.class_name, 0);
  180. }
  181. if (ce->trait_aliases[i]->alias) {
  182. zend_string_release_ex(ce->trait_aliases[i]->alias, 0);
  183. }
  184. efree(ce->trait_aliases[i]);
  185. i++;
  186. }
  187. efree(ce->trait_aliases);
  188. }
  189. if (ce->trait_precedences) {
  190. uint32_t j;
  191. i = 0;
  192. while (ce->trait_precedences[i]) {
  193. zend_string_release_ex(ce->trait_precedences[i]->trait_method.method_name, 0);
  194. zend_string_release_ex(ce->trait_precedences[i]->trait_method.class_name, 0);
  195. for (j = 0; j < ce->trait_precedences[i]->num_excludes; j++) {
  196. zend_string_release_ex(ce->trait_precedences[i]->exclude_class_names[j], 0);
  197. }
  198. efree(ce->trait_precedences[i]);
  199. i++;
  200. }
  201. efree(ce->trait_precedences);
  202. }
  203. }
  204. ZEND_API void zend_cleanup_mutable_class_data(zend_class_entry *ce)
  205. {
  206. zend_class_mutable_data *mutable_data = ZEND_MAP_PTR_GET_IMM(ce->mutable_data);
  207. if (mutable_data) {
  208. HashTable *constants_table;
  209. zval *p;
  210. constants_table = mutable_data->constants_table;
  211. if (constants_table && constants_table != &ce->constants_table) {
  212. zend_class_constant *c;
  213. ZEND_HASH_FOREACH_PTR(constants_table, c) {
  214. if (c->ce == ce || (Z_CONSTANT_FLAGS(c->value) & CONST_OWNED)) {
  215. zval_ptr_dtor_nogc(&c->value);
  216. }
  217. } ZEND_HASH_FOREACH_END();
  218. zend_hash_destroy(constants_table);
  219. mutable_data->constants_table = NULL;
  220. }
  221. p = mutable_data->default_properties_table;
  222. if (p && p != ce->default_properties_table) {
  223. zval *end = p + ce->default_properties_count;
  224. while (p < end) {
  225. zval_ptr_dtor_nogc(p);
  226. p++;
  227. }
  228. mutable_data->default_properties_table = NULL;
  229. }
  230. ZEND_MAP_PTR_SET_IMM(ce->mutable_data, NULL);
  231. }
  232. }
  233. ZEND_API void destroy_zend_class(zval *zv)
  234. {
  235. zend_property_info *prop_info;
  236. zend_class_entry *ce = Z_PTR_P(zv);
  237. zend_function *fn;
  238. if (ce->ce_flags & ZEND_ACC_IMMUTABLE) {
  239. return;
  240. }
  241. if (ce->ce_flags & ZEND_ACC_FILE_CACHED) {
  242. zend_class_constant *c;
  243. zval *p, *end;
  244. ZEND_HASH_FOREACH_PTR(&ce->constants_table, c) {
  245. if (c->ce == ce) {
  246. zval_ptr_dtor_nogc(&c->value);
  247. }
  248. } ZEND_HASH_FOREACH_END();
  249. p = ce->default_properties_table;
  250. end = p + ce->default_properties_count;
  251. while (p < end) {
  252. zval_ptr_dtor_nogc(p);
  253. p++;
  254. }
  255. return;
  256. }
  257. if (--ce->refcount > 0) {
  258. return;
  259. }
  260. switch (ce->type) {
  261. case ZEND_USER_CLASS:
  262. if (!(ce->ce_flags & ZEND_ACC_CACHED)) {
  263. if (ce->parent_name && !(ce->ce_flags & ZEND_ACC_RESOLVED_PARENT)) {
  264. zend_string_release_ex(ce->parent_name, 0);
  265. }
  266. zend_string_release_ex(ce->name, 0);
  267. zend_string_release_ex(ce->info.user.filename, 0);
  268. if (ce->info.user.doc_comment) {
  269. zend_string_release_ex(ce->info.user.doc_comment, 0);
  270. }
  271. if (ce->attributes) {
  272. zend_hash_release(ce->attributes);
  273. }
  274. if (ce->backed_enum_table) {
  275. zend_hash_release(ce->backed_enum_table);
  276. }
  277. if (ce->num_interfaces > 0 && !(ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES)) {
  278. uint32_t i;
  279. for (i = 0; i < ce->num_interfaces; i++) {
  280. zend_string_release_ex(ce->interface_names[i].name, 0);
  281. zend_string_release_ex(ce->interface_names[i].lc_name, 0);
  282. }
  283. efree(ce->interface_names);
  284. }
  285. if (ce->num_traits > 0) {
  286. _destroy_zend_class_traits_info(ce);
  287. }
  288. }
  289. if (ce->default_properties_table) {
  290. zval *p = ce->default_properties_table;
  291. zval *end = p + ce->default_properties_count;
  292. while (p != end) {
  293. i_zval_ptr_dtor(p);
  294. p++;
  295. }
  296. efree(ce->default_properties_table);
  297. }
  298. if (ce->default_static_members_table) {
  299. zval *p = ce->default_static_members_table;
  300. zval *end = p + ce->default_static_members_count;
  301. while (p != end) {
  302. ZEND_ASSERT(!Z_ISREF_P(p));
  303. i_zval_ptr_dtor(p);
  304. p++;
  305. }
  306. efree(ce->default_static_members_table);
  307. }
  308. ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop_info) {
  309. if (prop_info->ce == ce) {
  310. zend_string_release_ex(prop_info->name, 0);
  311. if (prop_info->doc_comment) {
  312. zend_string_release_ex(prop_info->doc_comment, 0);
  313. }
  314. if (prop_info->attributes) {
  315. zend_hash_release(prop_info->attributes);
  316. }
  317. zend_type_release(prop_info->type, /* persistent */ 0);
  318. }
  319. } ZEND_HASH_FOREACH_END();
  320. zend_hash_destroy(&ce->properties_info);
  321. zend_hash_destroy(&ce->function_table);
  322. if (zend_hash_num_elements(&ce->constants_table)) {
  323. zend_class_constant *c;
  324. ZEND_HASH_FOREACH_PTR(&ce->constants_table, c) {
  325. if (c->ce == ce || (Z_CONSTANT_FLAGS(c->value) & CONST_OWNED)) {
  326. zval_ptr_dtor_nogc(&c->value);
  327. if (c->doc_comment) {
  328. zend_string_release_ex(c->doc_comment, 0);
  329. }
  330. if (c->attributes) {
  331. zend_hash_release(c->attributes);
  332. }
  333. }
  334. } ZEND_HASH_FOREACH_END();
  335. }
  336. zend_hash_destroy(&ce->constants_table);
  337. if (ce->num_interfaces > 0 && (ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES)) {
  338. efree(ce->interfaces);
  339. }
  340. break;
  341. case ZEND_INTERNAL_CLASS:
  342. if (ce->backed_enum_table) {
  343. zend_hash_release(ce->backed_enum_table);
  344. }
  345. if (ce->default_properties_table) {
  346. zval *p = ce->default_properties_table;
  347. zval *end = p + ce->default_properties_count;
  348. while (p != end) {
  349. zval_internal_ptr_dtor(p);
  350. p++;
  351. }
  352. free(ce->default_properties_table);
  353. }
  354. if (ce->default_static_members_table) {
  355. zval *p = ce->default_static_members_table;
  356. zval *end = p + ce->default_static_members_count;
  357. while (p != end) {
  358. zval_internal_ptr_dtor(p);
  359. p++;
  360. }
  361. free(ce->default_static_members_table);
  362. }
  363. ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop_info) {
  364. if (prop_info->ce == ce) {
  365. zend_string_release(prop_info->name);
  366. zend_type_release(prop_info->type, /* persistent */ 1);
  367. free(prop_info);
  368. }
  369. } ZEND_HASH_FOREACH_END();
  370. zend_hash_destroy(&ce->properties_info);
  371. zend_string_release_ex(ce->name, 1);
  372. /* TODO: eliminate this loop for classes without functions with arg_info / attributes */
  373. ZEND_HASH_FOREACH_PTR(&ce->function_table, fn) {
  374. if (fn->common.scope == ce) {
  375. if (fn->common.fn_flags & (ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_HAS_TYPE_HINTS)) {
  376. zend_free_internal_arg_info(&fn->internal_function);
  377. }
  378. if (fn->common.attributes) {
  379. zend_hash_release(fn->common.attributes);
  380. fn->common.attributes = NULL;
  381. }
  382. }
  383. } ZEND_HASH_FOREACH_END();
  384. zend_hash_destroy(&ce->function_table);
  385. if (zend_hash_num_elements(&ce->constants_table)) {
  386. zend_class_constant *c;
  387. ZEND_HASH_FOREACH_PTR(&ce->constants_table, c) {
  388. if (c->ce == ce) {
  389. if (Z_TYPE(c->value) == IS_CONSTANT_AST) {
  390. /* We marked this as IMMUTABLE, but do need to free it when the
  391. * class is destroyed. */
  392. ZEND_ASSERT(Z_ASTVAL(c->value)->kind == ZEND_AST_CONST_ENUM_INIT);
  393. free(Z_AST(c->value));
  394. } else {
  395. zval_internal_ptr_dtor(&c->value);
  396. }
  397. if (c->doc_comment) {
  398. zend_string_release_ex(c->doc_comment, 1);
  399. }
  400. if (c->attributes) {
  401. zend_hash_release(c->attributes);
  402. }
  403. }
  404. free(c);
  405. } ZEND_HASH_FOREACH_END();
  406. zend_hash_destroy(&ce->constants_table);
  407. }
  408. if (ce->iterator_funcs_ptr) {
  409. free(ce->iterator_funcs_ptr);
  410. }
  411. if (ce->num_interfaces > 0) {
  412. free(ce->interfaces);
  413. }
  414. if (ce->properties_info_table) {
  415. free(ce->properties_info_table);
  416. }
  417. if (ce->attributes) {
  418. zend_hash_release(ce->attributes);
  419. }
  420. free(ce);
  421. break;
  422. }
  423. }
  424. void zend_class_add_ref(zval *zv)
  425. {
  426. zend_class_entry *ce = Z_PTR_P(zv);
  427. if (!(ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
  428. ce->refcount++;
  429. }
  430. }
  431. ZEND_API void zend_destroy_static_vars(zend_op_array *op_array)
  432. {
  433. if (ZEND_MAP_PTR(op_array->static_variables_ptr)) {
  434. HashTable *ht = ZEND_MAP_PTR_GET(op_array->static_variables_ptr);
  435. if (ht) {
  436. zend_array_destroy(ht);
  437. ZEND_MAP_PTR_SET(op_array->static_variables_ptr, NULL);
  438. }
  439. }
  440. }
  441. ZEND_API void destroy_op_array(zend_op_array *op_array)
  442. {
  443. uint32_t i;
  444. if ((op_array->fn_flags & ZEND_ACC_HEAP_RT_CACHE)
  445. && ZEND_MAP_PTR(op_array->run_time_cache)) {
  446. efree(ZEND_MAP_PTR(op_array->run_time_cache));
  447. }
  448. if (op_array->function_name) {
  449. zend_string_release_ex(op_array->function_name, 0);
  450. }
  451. if (!op_array->refcount || --(*op_array->refcount) > 0) {
  452. return;
  453. }
  454. efree_size(op_array->refcount, sizeof(*(op_array->refcount)));
  455. if (op_array->vars) {
  456. i = op_array->last_var;
  457. while (i > 0) {
  458. i--;
  459. zend_string_release_ex(op_array->vars[i], 0);
  460. }
  461. efree(op_array->vars);
  462. }
  463. if (op_array->literals) {
  464. zval *literal = op_array->literals;
  465. zval *end = literal + op_array->last_literal;
  466. while (literal < end) {
  467. zval_ptr_dtor_nogc(literal);
  468. literal++;
  469. }
  470. if (ZEND_USE_ABS_CONST_ADDR
  471. || !(op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO)) {
  472. efree(op_array->literals);
  473. }
  474. }
  475. efree(op_array->opcodes);
  476. zend_string_release_ex(op_array->filename, 0);
  477. if (op_array->doc_comment) {
  478. zend_string_release_ex(op_array->doc_comment, 0);
  479. }
  480. if (op_array->attributes) {
  481. zend_hash_release(op_array->attributes);
  482. }
  483. if (op_array->live_range) {
  484. efree(op_array->live_range);
  485. }
  486. if (op_array->try_catch_array) {
  487. efree(op_array->try_catch_array);
  488. }
  489. if (zend_extension_flags & ZEND_EXTENSIONS_HAVE_OP_ARRAY_DTOR) {
  490. if (op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) {
  491. zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_dtor_handler, op_array);
  492. }
  493. }
  494. if (op_array->arg_info) {
  495. uint32_t num_args = op_array->num_args;
  496. zend_arg_info *arg_info = op_array->arg_info;
  497. if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
  498. arg_info--;
  499. num_args++;
  500. }
  501. if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
  502. num_args++;
  503. }
  504. for (i = 0 ; i < num_args; i++) {
  505. if (arg_info[i].name) {
  506. zend_string_release_ex(arg_info[i].name, 0);
  507. }
  508. zend_type_release(arg_info[i].type, /* persistent */ 0);
  509. }
  510. efree(arg_info);
  511. }
  512. if (op_array->static_variables) {
  513. zend_array_destroy(op_array->static_variables);
  514. }
  515. if (op_array->num_dynamic_func_defs) {
  516. for (i = 0; i < op_array->num_dynamic_func_defs; i++) {
  517. /* Closures overwrite static_variables in their copy.
  518. * Make sure to destroy them when the prototype function is destroyed. */
  519. if (op_array->dynamic_func_defs[i]->static_variables
  520. && (op_array->dynamic_func_defs[i]->fn_flags & ZEND_ACC_CLOSURE)) {
  521. zend_array_destroy(op_array->dynamic_func_defs[i]->static_variables);
  522. op_array->dynamic_func_defs[i]->static_variables = NULL;
  523. }
  524. destroy_op_array(op_array->dynamic_func_defs[i]);
  525. }
  526. efree(op_array->dynamic_func_defs);
  527. }
  528. }
  529. static void zend_update_extended_stmts(zend_op_array *op_array)
  530. {
  531. zend_op *opline = op_array->opcodes, *end=opline+op_array->last;
  532. while (opline<end) {
  533. if (opline->opcode == ZEND_EXT_STMT) {
  534. if (opline+1<end) {
  535. if ((opline+1)->opcode == ZEND_EXT_STMT) {
  536. opline->opcode = ZEND_NOP;
  537. opline++;
  538. continue;
  539. }
  540. if (opline+1<end) {
  541. opline->lineno = (opline+1)->lineno;
  542. }
  543. } else {
  544. opline->opcode = ZEND_NOP;
  545. }
  546. }
  547. opline++;
  548. }
  549. }
  550. static void zend_extension_op_array_handler(zend_extension *extension, zend_op_array *op_array)
  551. {
  552. if (extension->op_array_handler) {
  553. extension->op_array_handler(op_array);
  554. }
  555. }
  556. static void zend_check_finally_breakout(zend_op_array *op_array, uint32_t op_num, uint32_t dst_num)
  557. {
  558. int i;
  559. for (i = 0; i < op_array->last_try_catch; i++) {
  560. if ((op_num < op_array->try_catch_array[i].finally_op ||
  561. op_num >= op_array->try_catch_array[i].finally_end)
  562. && (dst_num >= op_array->try_catch_array[i].finally_op &&
  563. dst_num <= op_array->try_catch_array[i].finally_end)) {
  564. CG(in_compilation) = 1;
  565. CG(active_op_array) = op_array;
  566. CG(zend_lineno) = op_array->opcodes[op_num].lineno;
  567. zend_error_noreturn(E_COMPILE_ERROR, "jump into a finally block is disallowed");
  568. } else if ((op_num >= op_array->try_catch_array[i].finally_op
  569. && op_num <= op_array->try_catch_array[i].finally_end)
  570. && (dst_num > op_array->try_catch_array[i].finally_end
  571. || dst_num < op_array->try_catch_array[i].finally_op)) {
  572. CG(in_compilation) = 1;
  573. CG(active_op_array) = op_array;
  574. CG(zend_lineno) = op_array->opcodes[op_num].lineno;
  575. zend_error_noreturn(E_COMPILE_ERROR, "jump out of a finally block is disallowed");
  576. }
  577. }
  578. }
  579. static uint32_t zend_get_brk_cont_target(const zend_op_array *op_array, const zend_op *opline) {
  580. int nest_levels = opline->op2.num;
  581. int array_offset = opline->op1.num;
  582. zend_brk_cont_element *jmp_to;
  583. do {
  584. jmp_to = &CG(context).brk_cont_array[array_offset];
  585. if (nest_levels > 1) {
  586. array_offset = jmp_to->parent;
  587. }
  588. } while (--nest_levels > 0);
  589. return opline->opcode == ZEND_BRK ? jmp_to->brk : jmp_to->cont;
  590. }
  591. static void emit_live_range_raw(
  592. zend_op_array *op_array, uint32_t var_num, uint32_t kind, uint32_t start, uint32_t end) {
  593. zend_live_range *range;
  594. op_array->last_live_range++;
  595. op_array->live_range = erealloc(op_array->live_range,
  596. sizeof(zend_live_range) * op_array->last_live_range);
  597. ZEND_ASSERT(start < end);
  598. range = &op_array->live_range[op_array->last_live_range - 1];
  599. range->var = EX_NUM_TO_VAR(op_array->last_var + var_num);
  600. range->var |= kind;
  601. range->start = start;
  602. range->end = end;
  603. }
  604. static void emit_live_range(
  605. zend_op_array *op_array, uint32_t var_num, uint32_t start, uint32_t end,
  606. zend_needs_live_range_cb needs_live_range) {
  607. zend_op *def_opline = &op_array->opcodes[start], *orig_def_opline = def_opline;
  608. zend_op *use_opline = &op_array->opcodes[end];
  609. uint32_t kind;
  610. switch (def_opline->opcode) {
  611. /* These should never be the first def. */
  612. case ZEND_ADD_ARRAY_ELEMENT:
  613. case ZEND_ADD_ARRAY_UNPACK:
  614. case ZEND_ROPE_ADD:
  615. ZEND_UNREACHABLE();
  616. return;
  617. /* Result is boolean, it doesn't have to be destroyed. */
  618. case ZEND_JMPZ_EX:
  619. case ZEND_JMPNZ_EX:
  620. case ZEND_BOOL:
  621. case ZEND_BOOL_NOT:
  622. /* Classes don't have to be destroyed. */
  623. case ZEND_FETCH_CLASS:
  624. case ZEND_DECLARE_ANON_CLASS:
  625. /* FAST_CALLs don't have to be destroyed. */
  626. case ZEND_FAST_CALL:
  627. return;
  628. case ZEND_BEGIN_SILENCE:
  629. kind = ZEND_LIVE_SILENCE;
  630. start++;
  631. break;
  632. case ZEND_ROPE_INIT:
  633. kind = ZEND_LIVE_ROPE;
  634. /* ROPE live ranges include the generating opcode. */
  635. def_opline--;
  636. break;
  637. case ZEND_FE_RESET_R:
  638. case ZEND_FE_RESET_RW:
  639. kind = ZEND_LIVE_LOOP;
  640. start++;
  641. break;
  642. /* Objects created via ZEND_NEW are only fully initialized
  643. * after the DO_FCALL (constructor call).
  644. * We are creating two live-ranges: ZEND_LINE_NEW for uninitialized
  645. * part, and ZEND_LIVE_TMPVAR for initialized.
  646. */
  647. case ZEND_NEW:
  648. {
  649. int level = 0;
  650. uint32_t orig_start = start;
  651. while (def_opline + 1 < use_opline) {
  652. def_opline++;
  653. start++;
  654. if (def_opline->opcode == ZEND_DO_FCALL) {
  655. if (level == 0) {
  656. break;
  657. }
  658. level--;
  659. } else {
  660. switch (def_opline->opcode) {
  661. case ZEND_INIT_FCALL:
  662. case ZEND_INIT_FCALL_BY_NAME:
  663. case ZEND_INIT_NS_FCALL_BY_NAME:
  664. case ZEND_INIT_DYNAMIC_CALL:
  665. case ZEND_INIT_USER_CALL:
  666. case ZEND_INIT_METHOD_CALL:
  667. case ZEND_INIT_STATIC_METHOD_CALL:
  668. case ZEND_NEW:
  669. level++;
  670. break;
  671. case ZEND_DO_ICALL:
  672. case ZEND_DO_UCALL:
  673. case ZEND_DO_FCALL_BY_NAME:
  674. level--;
  675. break;
  676. }
  677. }
  678. }
  679. emit_live_range_raw(op_array, var_num, ZEND_LIVE_NEW, orig_start + 1, start + 1);
  680. if (start + 1 == end) {
  681. /* Trivial live-range, no need to store it. */
  682. return;
  683. }
  684. }
  685. ZEND_FALLTHROUGH;
  686. default:
  687. start++;
  688. kind = ZEND_LIVE_TMPVAR;
  689. /* Check hook to determine whether a live range is necessary,
  690. * e.g. based on type info. */
  691. if (needs_live_range && !needs_live_range(op_array, orig_def_opline)) {
  692. return;
  693. }
  694. break;
  695. case ZEND_COPY_TMP:
  696. {
  697. /* COPY_TMP has a split live-range: One from the definition until the use in
  698. * "null" branch, and another from the start of the "non-null" branch to the
  699. * FREE opcode. */
  700. uint32_t rt_var_num = EX_NUM_TO_VAR(op_array->last_var + var_num);
  701. if (needs_live_range && !needs_live_range(op_array, orig_def_opline)) {
  702. return;
  703. }
  704. kind = ZEND_LIVE_TMPVAR;
  705. if (use_opline->opcode != ZEND_FREE) {
  706. /* This can happen if one branch of the coalesce has been optimized away.
  707. * In this case we should emit a normal live-range instead. */
  708. start++;
  709. break;
  710. }
  711. zend_op *block_start_op = use_opline;
  712. while ((block_start_op-1)->opcode == ZEND_FREE) {
  713. block_start_op--;
  714. }
  715. start = block_start_op - op_array->opcodes;
  716. if (start != end) {
  717. emit_live_range_raw(op_array, var_num, kind, start, end);
  718. }
  719. do {
  720. use_opline--;
  721. /* The use might have been optimized away, in which case we will hit the def
  722. * instead. */
  723. if (use_opline->opcode == ZEND_COPY_TMP && use_opline->result.var == rt_var_num) {
  724. return;
  725. }
  726. } while (!(
  727. ((use_opline->op1_type & (IS_TMP_VAR|IS_VAR)) && use_opline->op1.var == rt_var_num) ||
  728. ((use_opline->op2_type & (IS_TMP_VAR|IS_VAR)) && use_opline->op2.var == rt_var_num)
  729. ));
  730. start = def_opline + 1 - op_array->opcodes;
  731. end = use_opline - op_array->opcodes;
  732. emit_live_range_raw(op_array, var_num, kind, start, end);
  733. return;
  734. }
  735. }
  736. emit_live_range_raw(op_array, var_num, kind, start, end);
  737. }
  738. static bool is_fake_def(zend_op *opline) {
  739. /* These opcodes only modify the result, not create it. */
  740. return opline->opcode == ZEND_ROPE_ADD
  741. || opline->opcode == ZEND_ADD_ARRAY_ELEMENT
  742. || opline->opcode == ZEND_ADD_ARRAY_UNPACK;
  743. }
  744. static bool keeps_op1_alive(zend_op *opline) {
  745. /* These opcodes don't consume their OP1 operand,
  746. * it is later freed by something else. */
  747. if (opline->opcode == ZEND_CASE
  748. || opline->opcode == ZEND_CASE_STRICT
  749. || opline->opcode == ZEND_SWITCH_LONG
  750. || opline->opcode == ZEND_SWITCH_STRING
  751. || opline->opcode == ZEND_MATCH
  752. || opline->opcode == ZEND_FETCH_LIST_R
  753. || opline->opcode == ZEND_COPY_TMP) {
  754. return 1;
  755. }
  756. ZEND_ASSERT(opline->opcode != ZEND_FE_FETCH_R
  757. && opline->opcode != ZEND_FE_FETCH_RW
  758. && opline->opcode != ZEND_FETCH_LIST_W
  759. && opline->opcode != ZEND_VERIFY_RETURN_TYPE
  760. && opline->opcode != ZEND_BIND_LEXICAL
  761. && opline->opcode != ZEND_ROPE_ADD);
  762. return 0;
  763. }
  764. /* Live ranges must be sorted by increasing start opline */
  765. static int cmp_live_range(const zend_live_range *a, const zend_live_range *b) {
  766. return a->start - b->start;
  767. }
  768. static void swap_live_range(zend_live_range *a, zend_live_range *b) {
  769. uint32_t tmp;
  770. tmp = a->var;
  771. a->var = b->var;
  772. b->var = tmp;
  773. tmp = a->start;
  774. a->start = b->start;
  775. b->start = tmp;
  776. tmp = a->end;
  777. a->end = b->end;
  778. b->end = tmp;
  779. }
  780. static void zend_calc_live_ranges(
  781. zend_op_array *op_array, zend_needs_live_range_cb needs_live_range) {
  782. uint32_t opnum = op_array->last;
  783. zend_op *opline = &op_array->opcodes[opnum];
  784. ALLOCA_FLAG(use_heap)
  785. uint32_t var_offset = op_array->last_var;
  786. uint32_t *last_use = do_alloca(sizeof(uint32_t) * op_array->T, use_heap);
  787. memset(last_use, -1, sizeof(uint32_t) * op_array->T);
  788. ZEND_ASSERT(!op_array->live_range);
  789. while (opnum > 0) {
  790. opnum--;
  791. opline--;
  792. if ((opline->result_type & (IS_TMP_VAR|IS_VAR)) && !is_fake_def(opline)) {
  793. uint32_t var_num = EX_VAR_TO_NUM(opline->result.var) - var_offset;
  794. /* Defs without uses can occur for two reasons: Either because the result is
  795. * genuinely unused (e.g. omitted FREE opcode for an unused boolean result), or
  796. * because there are multiple defining opcodes (e.g. JMPZ_EX and QM_ASSIGN), in
  797. * which case the last one starts the live range. As such, we can simply ignore
  798. * missing uses here. */
  799. if (EXPECTED(last_use[var_num] != (uint32_t) -1)) {
  800. /* Skip trivial live-range */
  801. if (opnum + 1 != last_use[var_num]) {
  802. uint32_t num;
  803. #if 1
  804. /* OP_DATA uses only op1 operand */
  805. ZEND_ASSERT(opline->opcode != ZEND_OP_DATA);
  806. num = opnum;
  807. #else
  808. /* OP_DATA is really part of the previous opcode. */
  809. num = opnum - (opline->opcode == ZEND_OP_DATA);
  810. #endif
  811. emit_live_range(op_array, var_num, num, last_use[var_num], needs_live_range);
  812. }
  813. last_use[var_num] = (uint32_t) -1;
  814. }
  815. }
  816. if ((opline->op1_type & (IS_TMP_VAR|IS_VAR))) {
  817. uint32_t var_num = EX_VAR_TO_NUM(opline->op1.var) - var_offset;
  818. if (EXPECTED(last_use[var_num] == (uint32_t) -1)) {
  819. if (EXPECTED(!keeps_op1_alive(opline))) {
  820. /* OP_DATA is really part of the previous opcode. */
  821. last_use[var_num] = opnum - (opline->opcode == ZEND_OP_DATA);
  822. }
  823. }
  824. }
  825. if (opline->op2_type & (IS_TMP_VAR|IS_VAR)) {
  826. uint32_t var_num = EX_VAR_TO_NUM(opline->op2.var) - var_offset;
  827. if (UNEXPECTED(opline->opcode == ZEND_FE_FETCH_R
  828. || opline->opcode == ZEND_FE_FETCH_RW)) {
  829. /* OP2 of FE_FETCH is actually a def, not a use. */
  830. if (last_use[var_num] != (uint32_t) -1) {
  831. if (opnum + 1 != last_use[var_num]) {
  832. emit_live_range(
  833. op_array, var_num, opnum, last_use[var_num], needs_live_range);
  834. }
  835. last_use[var_num] = (uint32_t) -1;
  836. }
  837. } else if (EXPECTED(last_use[var_num] == (uint32_t) -1)) {
  838. #if 1
  839. /* OP_DATA uses only op1 operand */
  840. ZEND_ASSERT(opline->opcode != ZEND_OP_DATA);
  841. last_use[var_num] = opnum;
  842. #else
  843. /* OP_DATA is really part of the previous opcode. */
  844. last_use[var_num] = opnum - (opline->opcode == ZEND_OP_DATA);
  845. #endif
  846. }
  847. }
  848. }
  849. if (op_array->last_live_range > 1) {
  850. zend_live_range *r1 = op_array->live_range;
  851. zend_live_range *r2 = r1 + op_array->last_live_range - 1;
  852. /* In most cases we need just revert the array */
  853. while (r1 < r2) {
  854. swap_live_range(r1, r2);
  855. r1++;
  856. r2--;
  857. }
  858. r1 = op_array->live_range;
  859. r2 = r1 + op_array->last_live_range - 1;
  860. while (r1 < r2) {
  861. if (r1->start > (r1+1)->start) {
  862. zend_sort(r1, r2 - r1 + 1, sizeof(zend_live_range),
  863. (compare_func_t) cmp_live_range, (swap_func_t) swap_live_range);
  864. break;
  865. }
  866. r1++;
  867. }
  868. }
  869. free_alloca(last_use, use_heap);
  870. }
  871. ZEND_API void zend_recalc_live_ranges(
  872. zend_op_array *op_array, zend_needs_live_range_cb needs_live_range) {
  873. /* We assume that we never create live-ranges where there were none before. */
  874. ZEND_ASSERT(op_array->live_range);
  875. efree(op_array->live_range);
  876. op_array->live_range = NULL;
  877. op_array->last_live_range = 0;
  878. zend_calc_live_ranges(op_array, needs_live_range);
  879. }
  880. ZEND_API void pass_two(zend_op_array *op_array)
  881. {
  882. zend_op *opline, *end;
  883. if (!ZEND_USER_CODE(op_array->type)) {
  884. return;
  885. }
  886. if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) {
  887. zend_update_extended_stmts(op_array);
  888. }
  889. if (CG(compiler_options) & ZEND_COMPILE_HANDLE_OP_ARRAY) {
  890. if (zend_extension_flags & ZEND_EXTENSIONS_HAVE_OP_ARRAY_HANDLER) {
  891. zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_handler, op_array);
  892. }
  893. }
  894. if (CG(context).vars_size != op_array->last_var) {
  895. op_array->vars = (zend_string**) erealloc(op_array->vars, sizeof(zend_string*)*op_array->last_var);
  896. CG(context).vars_size = op_array->last_var;
  897. }
  898. #if ZEND_USE_ABS_CONST_ADDR
  899. if (CG(context).opcodes_size != op_array->last) {
  900. op_array->opcodes = (zend_op *) erealloc(op_array->opcodes, sizeof(zend_op)*op_array->last);
  901. CG(context).opcodes_size = op_array->last;
  902. }
  903. if (CG(context).literals_size != op_array->last_literal) {
  904. op_array->literals = (zval*)erealloc(op_array->literals, sizeof(zval) * op_array->last_literal);
  905. CG(context).literals_size = op_array->last_literal;
  906. }
  907. #else
  908. op_array->opcodes = (zend_op *) erealloc(op_array->opcodes,
  909. ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16) +
  910. sizeof(zval) * op_array->last_literal);
  911. if (op_array->literals) {
  912. memcpy(((char*)op_array->opcodes) + ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16),
  913. op_array->literals, sizeof(zval) * op_array->last_literal);
  914. efree(op_array->literals);
  915. op_array->literals = (zval*)(((char*)op_array->opcodes) + ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16));
  916. }
  917. CG(context).opcodes_size = op_array->last;
  918. CG(context).literals_size = op_array->last_literal;
  919. #endif
  920. /* Needs to be set directly after the opcode/literal reallocation, to ensure destruction
  921. * happens correctly if any of the following fixups generate a fatal error. */
  922. op_array->fn_flags |= ZEND_ACC_DONE_PASS_TWO;
  923. opline = op_array->opcodes;
  924. end = opline + op_array->last;
  925. while (opline < end) {
  926. switch (opline->opcode) {
  927. case ZEND_RECV_INIT:
  928. {
  929. zval *val = CT_CONSTANT(opline->op2);
  930. if (Z_TYPE_P(val) == IS_CONSTANT_AST) {
  931. uint32_t slot = ZEND_MM_ALIGNED_SIZE_EX(op_array->cache_size, 8);
  932. Z_CACHE_SLOT_P(val) = slot;
  933. op_array->cache_size += sizeof(zval);
  934. }
  935. }
  936. break;
  937. case ZEND_FAST_CALL:
  938. opline->op1.opline_num = op_array->try_catch_array[opline->op1.num].finally_op;
  939. ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op1);
  940. break;
  941. case ZEND_BRK:
  942. case ZEND_CONT:
  943. {
  944. uint32_t jmp_target = zend_get_brk_cont_target(op_array, opline);
  945. if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
  946. zend_check_finally_breakout(op_array, opline - op_array->opcodes, jmp_target);
  947. }
  948. opline->opcode = ZEND_JMP;
  949. opline->op1.opline_num = jmp_target;
  950. opline->op2.num = 0;
  951. ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op1);
  952. }
  953. break;
  954. case ZEND_GOTO:
  955. zend_resolve_goto_label(op_array, opline);
  956. if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
  957. zend_check_finally_breakout(op_array, opline - op_array->opcodes, opline->op1.opline_num);
  958. }
  959. ZEND_FALLTHROUGH;
  960. case ZEND_JMP:
  961. ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op1);
  962. break;
  963. case ZEND_JMPZNZ:
  964. /* absolute index to relative offset */
  965. opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, opline->extended_value);
  966. ZEND_FALLTHROUGH;
  967. case ZEND_JMPZ:
  968. case ZEND_JMPNZ:
  969. case ZEND_JMPZ_EX:
  970. case ZEND_JMPNZ_EX:
  971. case ZEND_JMP_SET:
  972. case ZEND_COALESCE:
  973. case ZEND_FE_RESET_R:
  974. case ZEND_FE_RESET_RW:
  975. case ZEND_JMP_NULL:
  976. ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op2);
  977. break;
  978. case ZEND_ASSERT_CHECK:
  979. {
  980. /* If result of assert is unused, result of check is unused as well */
  981. zend_op *call = &op_array->opcodes[opline->op2.opline_num - 1];
  982. if (call->opcode == ZEND_EXT_FCALL_END) {
  983. call--;
  984. }
  985. if (call->result_type == IS_UNUSED) {
  986. opline->result_type = IS_UNUSED;
  987. }
  988. ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op2);
  989. break;
  990. }
  991. case ZEND_FE_FETCH_R:
  992. case ZEND_FE_FETCH_RW:
  993. /* absolute index to relative offset */
  994. opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, opline->extended_value);
  995. break;
  996. case ZEND_CATCH:
  997. if (!(opline->extended_value & ZEND_LAST_CATCH)) {
  998. ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op2);
  999. }
  1000. break;
  1001. case ZEND_RETURN:
  1002. case ZEND_RETURN_BY_REF:
  1003. if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
  1004. opline->opcode = ZEND_GENERATOR_RETURN;
  1005. }
  1006. break;
  1007. case ZEND_SWITCH_LONG:
  1008. case ZEND_SWITCH_STRING:
  1009. case ZEND_MATCH:
  1010. {
  1011. /* absolute indexes to relative offsets */
  1012. HashTable *jumptable = Z_ARRVAL_P(CT_CONSTANT(opline->op2));
  1013. zval *zv;
  1014. ZEND_HASH_FOREACH_VAL(jumptable, zv) {
  1015. Z_LVAL_P(zv) = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, Z_LVAL_P(zv));
  1016. } ZEND_HASH_FOREACH_END();
  1017. opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, opline->extended_value);
  1018. break;
  1019. }
  1020. }
  1021. if (opline->op1_type == IS_CONST) {
  1022. ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, opline->op1);
  1023. } else if (opline->op1_type & (IS_VAR|IS_TMP_VAR)) {
  1024. opline->op1.var = EX_NUM_TO_VAR(op_array->last_var + opline->op1.var);
  1025. }
  1026. if (opline->op2_type == IS_CONST) {
  1027. ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, opline->op2);
  1028. } else if (opline->op2_type & (IS_VAR|IS_TMP_VAR)) {
  1029. opline->op2.var = EX_NUM_TO_VAR(op_array->last_var + opline->op2.var);
  1030. }
  1031. if (opline->result_type & (IS_VAR|IS_TMP_VAR)) {
  1032. opline->result.var = EX_NUM_TO_VAR(op_array->last_var + opline->result.var);
  1033. }
  1034. ZEND_VM_SET_OPCODE_HANDLER(opline);
  1035. opline++;
  1036. }
  1037. zend_calc_live_ranges(op_array, NULL);
  1038. return;
  1039. }
  1040. ZEND_API unary_op_type get_unary_op(int opcode)
  1041. {
  1042. switch (opcode) {
  1043. case ZEND_BW_NOT:
  1044. return (unary_op_type) bitwise_not_function;
  1045. case ZEND_BOOL_NOT:
  1046. return (unary_op_type) boolean_not_function;
  1047. default:
  1048. return (unary_op_type) NULL;
  1049. }
  1050. }
  1051. ZEND_API binary_op_type get_binary_op(int opcode)
  1052. {
  1053. switch (opcode) {
  1054. case ZEND_ADD:
  1055. return (binary_op_type) add_function;
  1056. case ZEND_SUB:
  1057. return (binary_op_type) sub_function;
  1058. case ZEND_MUL:
  1059. return (binary_op_type) mul_function;
  1060. case ZEND_POW:
  1061. return (binary_op_type) pow_function;
  1062. case ZEND_DIV:
  1063. return (binary_op_type) div_function;
  1064. case ZEND_MOD:
  1065. return (binary_op_type) mod_function;
  1066. case ZEND_SL:
  1067. return (binary_op_type) shift_left_function;
  1068. case ZEND_SR:
  1069. return (binary_op_type) shift_right_function;
  1070. case ZEND_FAST_CONCAT:
  1071. case ZEND_CONCAT:
  1072. return (binary_op_type) concat_function;
  1073. case ZEND_IS_IDENTICAL:
  1074. case ZEND_CASE_STRICT:
  1075. return (binary_op_type) is_identical_function;
  1076. case ZEND_IS_NOT_IDENTICAL:
  1077. return (binary_op_type) is_not_identical_function;
  1078. case ZEND_IS_EQUAL:
  1079. case ZEND_CASE:
  1080. return (binary_op_type) is_equal_function;
  1081. case ZEND_IS_NOT_EQUAL:
  1082. return (binary_op_type) is_not_equal_function;
  1083. case ZEND_IS_SMALLER:
  1084. return (binary_op_type) is_smaller_function;
  1085. case ZEND_IS_SMALLER_OR_EQUAL:
  1086. return (binary_op_type) is_smaller_or_equal_function;
  1087. case ZEND_SPACESHIP:
  1088. return (binary_op_type) compare_function;
  1089. case ZEND_BW_OR:
  1090. return (binary_op_type) bitwise_or_function;
  1091. case ZEND_BW_AND:
  1092. return (binary_op_type) bitwise_and_function;
  1093. case ZEND_BW_XOR:
  1094. return (binary_op_type) bitwise_xor_function;
  1095. case ZEND_BOOL_XOR:
  1096. return (binary_op_type) boolean_xor_function;
  1097. default:
  1098. ZEND_UNREACHABLE();
  1099. return (binary_op_type) NULL;
  1100. }
  1101. }