zend_opcode.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2016 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@zend.com> |
  16. | Zeev Suraski <zeev@zend.com> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  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_vm.h"
  27. static void zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
  28. {
  29. if (extension->op_array_ctor) {
  30. extension->op_array_ctor(op_array);
  31. }
  32. }
  33. static void zend_extension_op_array_dtor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
  34. {
  35. if (extension->op_array_dtor) {
  36. extension->op_array_dtor(op_array);
  37. }
  38. }
  39. static void op_array_alloc_ops(zend_op_array *op_array, zend_uint size)
  40. {
  41. op_array->opcodes = erealloc(op_array->opcodes, size * sizeof(zend_op));
  42. }
  43. void init_op_array(zend_op_array *op_array, zend_uchar type, int initial_ops_size TSRMLS_DC)
  44. {
  45. op_array->type = type;
  46. if (CG(interactive)) {
  47. /* We must avoid a realloc() on the op_array in interactive mode, since pointers to constants
  48. * will become invalid
  49. */
  50. initial_ops_size = INITIAL_INTERACTIVE_OP_ARRAY_SIZE;
  51. }
  52. op_array->refcount = (zend_uint *) emalloc(sizeof(zend_uint));
  53. *op_array->refcount = 1;
  54. op_array->last = 0;
  55. op_array->opcodes = NULL;
  56. op_array_alloc_ops(op_array, initial_ops_size);
  57. op_array->last_var = 0;
  58. op_array->vars = NULL;
  59. op_array->T = 0;
  60. op_array->nested_calls = 0;
  61. op_array->used_stack = 0;
  62. op_array->function_name = NULL;
  63. op_array->filename = zend_get_compiled_filename(TSRMLS_C);
  64. op_array->doc_comment = NULL;
  65. op_array->doc_comment_len = 0;
  66. op_array->arg_info = NULL;
  67. op_array->num_args = 0;
  68. op_array->required_num_args = 0;
  69. op_array->scope = NULL;
  70. op_array->brk_cont_array = NULL;
  71. op_array->try_catch_array = NULL;
  72. op_array->last_brk_cont = 0;
  73. op_array->static_variables = NULL;
  74. op_array->last_try_catch = 0;
  75. op_array->has_finally_block = 0;
  76. op_array->this_var = -1;
  77. op_array->fn_flags = CG(interactive)?ZEND_ACC_INTERACTIVE:0;
  78. op_array->early_binding = -1;
  79. op_array->last_literal = 0;
  80. op_array->literals = NULL;
  81. op_array->run_time_cache = NULL;
  82. op_array->last_cache_slot = 0;
  83. memset(op_array->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
  84. zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_ctor_handler, op_array TSRMLS_CC);
  85. }
  86. ZEND_API void destroy_zend_function(zend_function *function TSRMLS_DC)
  87. {
  88. switch (function->type) {
  89. case ZEND_USER_FUNCTION:
  90. destroy_op_array((zend_op_array *) function TSRMLS_CC);
  91. break;
  92. case ZEND_INTERNAL_FUNCTION:
  93. /* do nothing */
  94. break;
  95. }
  96. }
  97. ZEND_API void zend_function_dtor(zend_function *function)
  98. {
  99. TSRMLS_FETCH();
  100. destroy_zend_function(function TSRMLS_CC);
  101. }
  102. static void zend_cleanup_op_array_data(zend_op_array *op_array)
  103. {
  104. if (op_array->static_variables) {
  105. zend_hash_clean(op_array->static_variables);
  106. }
  107. }
  108. ZEND_API int zend_cleanup_function_data(zend_function *function TSRMLS_DC)
  109. {
  110. if (function->type == ZEND_USER_FUNCTION) {
  111. zend_cleanup_op_array_data((zend_op_array *) function);
  112. return ZEND_HASH_APPLY_KEEP;
  113. } else {
  114. return ZEND_HASH_APPLY_STOP;
  115. }
  116. }
  117. ZEND_API int zend_cleanup_function_data_full(zend_function *function TSRMLS_DC)
  118. {
  119. if (function->type == ZEND_USER_FUNCTION) {
  120. zend_cleanup_op_array_data((zend_op_array *) function);
  121. }
  122. return 0;
  123. }
  124. static inline void cleanup_user_class_data(zend_class_entry *ce TSRMLS_DC)
  125. {
  126. /* Clean all parts that can contain run-time data */
  127. /* Note that only run-time accessed data need to be cleaned up, pre-defined data can
  128. not contain objects and thus are not probelmatic */
  129. if (ce->ce_flags & ZEND_HAS_STATIC_IN_METHODS) {
  130. zend_hash_apply(&ce->function_table, (apply_func_t) zend_cleanup_function_data_full TSRMLS_CC);
  131. }
  132. if (ce->static_members_table) {
  133. zval **static_members = ce->static_members_table;
  134. int count = ce->default_static_members_count;
  135. int i;
  136. ce->default_static_members_count = 0;
  137. ce->default_static_members_table = ce->static_members_table = NULL;
  138. for (i = 0; i < count; i++) {
  139. zval_ptr_dtor(&static_members[i]);
  140. }
  141. efree(static_members);
  142. }
  143. }
  144. static inline void cleanup_internal_class_data(zend_class_entry *ce TSRMLS_DC)
  145. {
  146. if (CE_STATIC_MEMBERS(ce)) {
  147. int i;
  148. for (i = 0; i < ce->default_static_members_count; i++) {
  149. zval_ptr_dtor(&CE_STATIC_MEMBERS(ce)[i]);
  150. }
  151. efree(CE_STATIC_MEMBERS(ce));
  152. #ifdef ZTS
  153. CG(static_members_table)[(zend_intptr_t)(ce->static_members_table)] = NULL;
  154. #else
  155. ce->static_members_table = NULL;
  156. #endif
  157. }
  158. }
  159. ZEND_API void zend_cleanup_internal_class_data(zend_class_entry *ce TSRMLS_DC)
  160. {
  161. cleanup_internal_class_data(ce TSRMLS_CC);
  162. }
  163. ZEND_API int zend_cleanup_user_class_data(zend_class_entry **pce TSRMLS_DC)
  164. {
  165. if ((*pce)->type == ZEND_USER_CLASS) {
  166. cleanup_user_class_data(*pce TSRMLS_CC);
  167. return ZEND_HASH_APPLY_KEEP;
  168. } else {
  169. return ZEND_HASH_APPLY_STOP;
  170. }
  171. }
  172. ZEND_API int zend_cleanup_class_data(zend_class_entry **pce TSRMLS_DC)
  173. {
  174. if ((*pce)->type == ZEND_USER_CLASS) {
  175. cleanup_user_class_data(*pce TSRMLS_CC);
  176. } else {
  177. cleanup_internal_class_data(*pce TSRMLS_CC);
  178. }
  179. return 0;
  180. }
  181. void _destroy_zend_class_traits_info(zend_class_entry *ce)
  182. {
  183. if (ce->num_traits > 0 && ce->traits) {
  184. efree(ce->traits);
  185. }
  186. if (ce->trait_aliases) {
  187. size_t i = 0;
  188. while (ce->trait_aliases[i]) {
  189. if (ce->trait_aliases[i]->trait_method) {
  190. if (ce->trait_aliases[i]->trait_method->method_name) {
  191. efree((char*)ce->trait_aliases[i]->trait_method->method_name);
  192. }
  193. if (ce->trait_aliases[i]->trait_method->class_name) {
  194. efree((char*)ce->trait_aliases[i]->trait_method->class_name);
  195. }
  196. efree(ce->trait_aliases[i]->trait_method);
  197. }
  198. if (ce->trait_aliases[i]->alias) {
  199. efree((char*)ce->trait_aliases[i]->alias);
  200. }
  201. efree(ce->trait_aliases[i]);
  202. i++;
  203. }
  204. efree(ce->trait_aliases);
  205. }
  206. if (ce->trait_precedences) {
  207. size_t i = 0;
  208. while (ce->trait_precedences[i]) {
  209. efree((char*)ce->trait_precedences[i]->trait_method->method_name);
  210. efree((char*)ce->trait_precedences[i]->trait_method->class_name);
  211. efree(ce->trait_precedences[i]->trait_method);
  212. if (ce->trait_precedences[i]->exclude_from_classes) {
  213. zend_uint j = 0;
  214. zend_trait_precedence *cur_precedence = ce->trait_precedences[i];
  215. while (cur_precedence->exclude_from_classes[j]) {
  216. efree(cur_precedence->exclude_from_classes[j]);
  217. j++;
  218. }
  219. efree(ce->trait_precedences[i]->exclude_from_classes);
  220. }
  221. efree(ce->trait_precedences[i]);
  222. i++;
  223. }
  224. efree(ce->trait_precedences);
  225. }
  226. }
  227. ZEND_API void destroy_zend_class(zend_class_entry **pce)
  228. {
  229. zend_class_entry *ce = *pce;
  230. if (--ce->refcount > 0) {
  231. return;
  232. }
  233. switch (ce->type) {
  234. case ZEND_USER_CLASS:
  235. if (ce->default_properties_table) {
  236. int i;
  237. for (i = 0; i < ce->default_properties_count; i++) {
  238. if (ce->default_properties_table[i]) {
  239. zval_ptr_dtor(&ce->default_properties_table[i]);
  240. }
  241. }
  242. efree(ce->default_properties_table);
  243. }
  244. if (ce->default_static_members_table) {
  245. int i;
  246. for (i = 0; i < ce->default_static_members_count; i++) {
  247. if (ce->default_static_members_table[i]) {
  248. zval_ptr_dtor(&ce->default_static_members_table[i]);
  249. }
  250. }
  251. efree(ce->default_static_members_table);
  252. }
  253. zend_hash_destroy(&ce->properties_info);
  254. str_efree(ce->name);
  255. zend_hash_destroy(&ce->function_table);
  256. zend_hash_destroy(&ce->constants_table);
  257. if (ce->num_interfaces > 0 && ce->interfaces) {
  258. efree(ce->interfaces);
  259. }
  260. if (ce->info.user.doc_comment) {
  261. efree((char*)ce->info.user.doc_comment);
  262. }
  263. _destroy_zend_class_traits_info(ce);
  264. efree(ce);
  265. break;
  266. case ZEND_INTERNAL_CLASS:
  267. if (ce->default_properties_table) {
  268. int i;
  269. for (i = 0; i < ce->default_properties_count; i++) {
  270. if (ce->default_properties_table[i]) {
  271. zval_internal_ptr_dtor(&ce->default_properties_table[i]);
  272. }
  273. }
  274. free(ce->default_properties_table);
  275. }
  276. if (ce->default_static_members_table) {
  277. int i;
  278. for (i = 0; i < ce->default_static_members_count; i++) {
  279. zval_internal_ptr_dtor(&ce->default_static_members_table[i]);
  280. }
  281. free(ce->default_static_members_table);
  282. }
  283. zend_hash_destroy(&ce->properties_info);
  284. str_free(ce->name);
  285. zend_hash_destroy(&ce->function_table);
  286. zend_hash_destroy(&ce->constants_table);
  287. if (ce->num_interfaces > 0) {
  288. free(ce->interfaces);
  289. }
  290. free(ce);
  291. break;
  292. }
  293. }
  294. void zend_class_add_ref(zend_class_entry **ce)
  295. {
  296. (*ce)->refcount++;
  297. }
  298. ZEND_API void destroy_op_array(zend_op_array *op_array TSRMLS_DC)
  299. {
  300. zend_literal *literal = op_array->literals;
  301. zend_literal *end;
  302. zend_uint i;
  303. if (op_array->static_variables) {
  304. zend_hash_destroy(op_array->static_variables);
  305. FREE_HASHTABLE(op_array->static_variables);
  306. }
  307. if (op_array->run_time_cache) {
  308. efree(op_array->run_time_cache);
  309. }
  310. if (--(*op_array->refcount)>0) {
  311. return;
  312. }
  313. efree(op_array->refcount);
  314. if (op_array->vars) {
  315. i = op_array->last_var;
  316. while (i > 0) {
  317. i--;
  318. str_efree(op_array->vars[i].name);
  319. }
  320. efree(op_array->vars);
  321. }
  322. if (literal) {
  323. end = literal + op_array->last_literal;
  324. while (literal < end) {
  325. zval_dtor(&literal->constant);
  326. literal++;
  327. }
  328. efree(op_array->literals);
  329. }
  330. efree(op_array->opcodes);
  331. if (op_array->function_name) {
  332. efree((char*)op_array->function_name);
  333. }
  334. if (op_array->doc_comment) {
  335. efree((char*)op_array->doc_comment);
  336. }
  337. if (op_array->brk_cont_array) {
  338. efree(op_array->brk_cont_array);
  339. }
  340. if (op_array->try_catch_array) {
  341. efree(op_array->try_catch_array);
  342. }
  343. if (op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) {
  344. zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_dtor_handler, op_array TSRMLS_CC);
  345. }
  346. if (op_array->arg_info) {
  347. for (i=0; i<op_array->num_args; i++) {
  348. str_efree(op_array->arg_info[i].name);
  349. if (op_array->arg_info[i].class_name) {
  350. str_efree(op_array->arg_info[i].class_name);
  351. }
  352. }
  353. efree(op_array->arg_info);
  354. }
  355. }
  356. void init_op(zend_op *op TSRMLS_DC)
  357. {
  358. memset(op, 0, sizeof(zend_op));
  359. op->lineno = CG(zend_lineno);
  360. SET_UNUSED(op->result);
  361. }
  362. zend_op *get_next_op(zend_op_array *op_array TSRMLS_DC)
  363. {
  364. zend_uint next_op_num = op_array->last++;
  365. zend_op *next_op;
  366. if (next_op_num >= CG(context).opcodes_size) {
  367. if (op_array->fn_flags & ZEND_ACC_INTERACTIVE) {
  368. /* we messed up */
  369. zend_printf("Ran out of opcode space!\n"
  370. "You should probably consider writing this huge script into a file!\n");
  371. zend_bailout();
  372. }
  373. CG(context).opcodes_size *= 4;
  374. op_array_alloc_ops(op_array, CG(context).opcodes_size);
  375. }
  376. next_op = &(op_array->opcodes[next_op_num]);
  377. init_op(next_op TSRMLS_CC);
  378. return next_op;
  379. }
  380. int get_next_op_number(zend_op_array *op_array)
  381. {
  382. return op_array->last;
  383. }
  384. zend_brk_cont_element *get_next_brk_cont_element(zend_op_array *op_array)
  385. {
  386. op_array->last_brk_cont++;
  387. op_array->brk_cont_array = erealloc(op_array->brk_cont_array, sizeof(zend_brk_cont_element)*op_array->last_brk_cont);
  388. return &op_array->brk_cont_array[op_array->last_brk_cont-1];
  389. }
  390. static void zend_update_extended_info(zend_op_array *op_array TSRMLS_DC)
  391. {
  392. zend_op *opline = op_array->opcodes, *end=opline+op_array->last;
  393. while (opline<end) {
  394. if (opline->opcode == ZEND_EXT_STMT) {
  395. if (opline+1<end) {
  396. if ((opline+1)->opcode == ZEND_EXT_STMT) {
  397. opline->opcode = ZEND_NOP;
  398. opline++;
  399. continue;
  400. }
  401. if (opline+1<end) {
  402. opline->lineno = (opline+1)->lineno;
  403. }
  404. } else {
  405. opline->opcode = ZEND_NOP;
  406. }
  407. }
  408. opline++;
  409. }
  410. }
  411. static void zend_extension_op_array_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
  412. {
  413. if (extension->op_array_handler) {
  414. extension->op_array_handler(op_array);
  415. }
  416. }
  417. static void zend_check_finally_breakout(zend_op_array *op_array, zend_uint op_num, zend_uint dst_num TSRMLS_DC)
  418. {
  419. zend_uint i;
  420. for (i = 0; i < op_array->last_try_catch; i++) {
  421. if ((op_num < op_array->try_catch_array[i].finally_op ||
  422. op_num >= op_array->try_catch_array[i].finally_end)
  423. && (dst_num >= op_array->try_catch_array[i].finally_op &&
  424. dst_num <= op_array->try_catch_array[i].finally_end)) {
  425. CG(in_compilation) = 1;
  426. CG(active_op_array) = op_array;
  427. CG(zend_lineno) = op_array->opcodes[op_num].lineno;
  428. zend_error_noreturn(E_COMPILE_ERROR, "jump into a finally block is disallowed");
  429. } else if ((op_num >= op_array->try_catch_array[i].finally_op
  430. && op_num <= op_array->try_catch_array[i].finally_end)
  431. && (dst_num > op_array->try_catch_array[i].finally_end
  432. || dst_num < op_array->try_catch_array[i].finally_op)) {
  433. CG(in_compilation) = 1;
  434. CG(active_op_array) = op_array;
  435. CG(zend_lineno) = op_array->opcodes[op_num].lineno;
  436. zend_error_noreturn(E_COMPILE_ERROR, "jump out of a finally block is disallowed");
  437. }
  438. }
  439. }
  440. static void zend_adjust_fast_call(zend_op_array *op_array, zend_uint fast_call, zend_uint start, zend_uint end TSRMLS_DC)
  441. {
  442. int i;
  443. zend_uint op_num = 0;
  444. for (i = 0; i < op_array->last_try_catch; i++) {
  445. if (op_array->try_catch_array[i].finally_op > start
  446. && op_array->try_catch_array[i].finally_end < end) {
  447. op_num = op_array->try_catch_array[i].finally_op;
  448. start = op_array->try_catch_array[i].finally_end;
  449. }
  450. }
  451. if (op_num) {
  452. /* Must be ZEND_FAST_CALL */
  453. ZEND_ASSERT(op_array->opcodes[op_num - 2].opcode == ZEND_FAST_CALL);
  454. op_array->opcodes[op_num - 2].extended_value = ZEND_FAST_CALL_FROM_FINALLY;
  455. op_array->opcodes[op_num - 2].op2.opline_num = fast_call;
  456. }
  457. }
  458. static void zend_resolve_fast_call(zend_op_array *op_array, zend_uint fast_call, zend_uint op_num TSRMLS_DC)
  459. {
  460. int i;
  461. zend_uint finally_op_num = 0;
  462. for (i = 0; i < op_array->last_try_catch; i++) {
  463. if (op_num >= op_array->try_catch_array[i].finally_op
  464. && op_num < op_array->try_catch_array[i].finally_end) {
  465. finally_op_num = op_array->try_catch_array[i].finally_op;
  466. }
  467. }
  468. if (finally_op_num) {
  469. /* Must be ZEND_FAST_CALL */
  470. ZEND_ASSERT(op_array->opcodes[finally_op_num - 2].opcode == ZEND_FAST_CALL);
  471. if (op_array->opcodes[fast_call].extended_value == 0) {
  472. op_array->opcodes[fast_call].extended_value = ZEND_FAST_CALL_FROM_FINALLY;
  473. op_array->opcodes[fast_call].op2.opline_num = finally_op_num - 2;
  474. }
  475. }
  476. }
  477. static void zend_resolve_finally_call(zend_op_array *op_array, zend_uint op_num, zend_uint dst_num TSRMLS_DC)
  478. {
  479. zend_uint start_op;
  480. zend_op *opline;
  481. zend_uint i = op_array->last_try_catch;
  482. if (dst_num != (zend_uint)-1) {
  483. zend_check_finally_breakout(op_array, op_num, dst_num TSRMLS_CC);
  484. }
  485. /* the backward order is mater */
  486. while (i > 0) {
  487. i--;
  488. if (op_array->try_catch_array[i].finally_op &&
  489. op_num >= op_array->try_catch_array[i].try_op &&
  490. op_num < op_array->try_catch_array[i].finally_op - 1 &&
  491. (dst_num < op_array->try_catch_array[i].try_op ||
  492. dst_num > op_array->try_catch_array[i].finally_end)) {
  493. /* we have a jump out of try block that needs executing finally */
  494. /* generate a FAST_CALL to finally block */
  495. start_op = get_next_op_number(op_array);
  496. opline = get_next_op(op_array TSRMLS_CC);
  497. opline->opcode = ZEND_FAST_CALL;
  498. SET_UNUSED(opline->op1);
  499. SET_UNUSED(opline->op2);
  500. zend_adjust_fast_call(op_array, start_op,
  501. op_array->try_catch_array[i].finally_op,
  502. op_array->try_catch_array[i].finally_end TSRMLS_CC);
  503. if (op_array->try_catch_array[i].catch_op) {
  504. opline->extended_value = ZEND_FAST_CALL_FROM_CATCH;
  505. opline->op2.opline_num = op_array->try_catch_array[i].catch_op;
  506. opline->op1.opline_num = get_next_op_number(op_array);
  507. /* generate a FAST_CALL to hole CALL_FROM_FINALLY */
  508. opline = get_next_op(op_array TSRMLS_CC);
  509. opline->opcode = ZEND_FAST_CALL;
  510. SET_UNUSED(opline->op1);
  511. SET_UNUSED(opline->op2);
  512. zend_resolve_fast_call(op_array, start_op + 1, op_array->try_catch_array[i].finally_op - 2 TSRMLS_CC);
  513. } else {
  514. zend_resolve_fast_call(op_array, start_op, op_array->try_catch_array[i].finally_op - 2 TSRMLS_CC);
  515. }
  516. opline->op1.opline_num = op_array->try_catch_array[i].finally_op;
  517. /* generate a sequence of FAST_CALL to upward finally block */
  518. while (i > 0) {
  519. i--;
  520. if (op_array->try_catch_array[i].finally_op &&
  521. op_num >= op_array->try_catch_array[i].try_op &&
  522. op_num < op_array->try_catch_array[i].finally_op - 1 &&
  523. (dst_num < op_array->try_catch_array[i].try_op ||
  524. dst_num > op_array->try_catch_array[i].finally_end)) {
  525. opline = get_next_op(op_array TSRMLS_CC);
  526. opline->opcode = ZEND_FAST_CALL;
  527. SET_UNUSED(opline->op1);
  528. SET_UNUSED(opline->op2);
  529. opline->op1.opline_num = op_array->try_catch_array[i].finally_op;
  530. }
  531. }
  532. /* Finish the sequence with original opcode */
  533. opline = get_next_op(op_array TSRMLS_CC);
  534. *opline = op_array->opcodes[op_num];
  535. /* Replace original opcode with jump to this sequence */
  536. opline = op_array->opcodes + op_num;
  537. opline->opcode = ZEND_JMP;
  538. SET_UNUSED(opline->op1);
  539. SET_UNUSED(opline->op2);
  540. opline->op1.opline_num = start_op;
  541. break;
  542. }
  543. }
  544. }
  545. static void zend_resolve_finally_ret(zend_op_array *op_array, zend_uint op_num TSRMLS_DC)
  546. {
  547. int i;
  548. zend_uint catch_op_num = 0, finally_op_num = 0;
  549. for (i = 0; i < op_array->last_try_catch; i++) {
  550. if (op_array->try_catch_array[i].try_op > op_num) {
  551. break;
  552. }
  553. if (op_num < op_array->try_catch_array[i].finally_op) {
  554. finally_op_num = op_array->try_catch_array[i].finally_op;
  555. }
  556. if (op_num < op_array->try_catch_array[i].catch_op) {
  557. catch_op_num = op_array->try_catch_array[i].catch_op;
  558. }
  559. }
  560. if (finally_op_num && (!catch_op_num || catch_op_num >= finally_op_num)) {
  561. /* in case of unhandled exception return to upward finally block */
  562. op_array->opcodes[op_num].extended_value = ZEND_FAST_RET_TO_FINALLY;
  563. op_array->opcodes[op_num].op2.opline_num = finally_op_num;
  564. } else if (catch_op_num) {
  565. /* in case of unhandled exception return to upward catch block */
  566. op_array->opcodes[op_num].extended_value = ZEND_FAST_RET_TO_CATCH;
  567. op_array->opcodes[op_num].op2.opline_num = catch_op_num;
  568. }
  569. }
  570. static void zend_resolve_finally_calls(zend_op_array *op_array TSRMLS_DC)
  571. {
  572. zend_uint i, j;
  573. zend_op *opline;
  574. for (i = 0, j = op_array->last; i < j; i++) {
  575. opline = op_array->opcodes + i;
  576. switch (opline->opcode) {
  577. case ZEND_RETURN:
  578. case ZEND_RETURN_BY_REF:
  579. case ZEND_GENERATOR_RETURN:
  580. zend_resolve_finally_call(op_array, i, (zend_uint)-1 TSRMLS_CC);
  581. break;
  582. case ZEND_BRK:
  583. case ZEND_CONT:
  584. {
  585. int nest_levels, array_offset;
  586. zend_brk_cont_element *jmp_to;
  587. nest_levels = Z_LVAL(op_array->literals[opline->op2.constant].constant);
  588. if ((array_offset = opline->op1.opline_num) != -1) {
  589. do {
  590. jmp_to = &op_array->brk_cont_array[array_offset];
  591. if (nest_levels > 1) {
  592. array_offset = jmp_to->parent;
  593. }
  594. } while (--nest_levels > 0);
  595. zend_resolve_finally_call(op_array, i, opline->opcode == ZEND_BRK ? jmp_to->brk : jmp_to->cont TSRMLS_CC);
  596. break;
  597. }
  598. }
  599. case ZEND_GOTO:
  600. if (Z_TYPE(op_array->literals[opline->op2.constant].constant) != IS_LONG) {
  601. zend_uint num = opline->op2.constant;
  602. opline->op2.zv = &op_array->literals[opline->op2.constant].constant;
  603. zend_resolve_goto_label(op_array, opline, 1 TSRMLS_CC);
  604. opline->op2.constant = num;
  605. }
  606. /* break omitted intentionally */
  607. case ZEND_JMP:
  608. zend_resolve_finally_call(op_array, i, opline->op1.opline_num TSRMLS_CC);
  609. break;
  610. case ZEND_FAST_CALL:
  611. zend_resolve_fast_call(op_array, i, i TSRMLS_CC);
  612. break;
  613. case ZEND_FAST_RET:
  614. zend_resolve_finally_ret(op_array, i TSRMLS_CC);
  615. break;
  616. default:
  617. break;
  618. }
  619. }
  620. }
  621. ZEND_API int pass_two(zend_op_array *op_array TSRMLS_DC)
  622. {
  623. zend_op *opline, *end;
  624. if (op_array->type!=ZEND_USER_FUNCTION && op_array->type!=ZEND_EVAL_CODE) {
  625. return 0;
  626. }
  627. if (op_array->has_finally_block) {
  628. zend_resolve_finally_calls(op_array TSRMLS_CC);
  629. }
  630. if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO) {
  631. zend_update_extended_info(op_array TSRMLS_CC);
  632. }
  633. if (CG(compiler_options) & ZEND_COMPILE_HANDLE_OP_ARRAY) {
  634. zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_handler, op_array TSRMLS_CC);
  635. }
  636. if (!(op_array->fn_flags & ZEND_ACC_INTERACTIVE) && CG(context).vars_size != op_array->last_var) {
  637. op_array->vars = (zend_compiled_variable *) erealloc(op_array->vars, sizeof(zend_compiled_variable)*op_array->last_var);
  638. CG(context).vars_size = op_array->last_var;
  639. }
  640. if (!(op_array->fn_flags & ZEND_ACC_INTERACTIVE) && CG(context).opcodes_size != op_array->last) {
  641. op_array->opcodes = (zend_op *) erealloc(op_array->opcodes, sizeof(zend_op)*op_array->last);
  642. CG(context).opcodes_size = op_array->last;
  643. }
  644. if (!(op_array->fn_flags & ZEND_ACC_INTERACTIVE) && CG(context).literals_size != op_array->last_literal) {
  645. op_array->literals = (zend_literal*)erealloc(op_array->literals, sizeof(zend_literal) * op_array->last_literal);
  646. CG(context).literals_size = op_array->last_literal;
  647. }
  648. opline = op_array->opcodes;
  649. end = opline + op_array->last;
  650. while (opline < end) {
  651. if (opline->op1_type == IS_CONST) {
  652. opline->op1.zv = &op_array->literals[opline->op1.constant].constant;
  653. }
  654. if (opline->op2_type == IS_CONST) {
  655. opline->op2.zv = &op_array->literals[opline->op2.constant].constant;
  656. }
  657. switch (opline->opcode) {
  658. case ZEND_GOTO:
  659. if (Z_TYPE_P(opline->op2.zv) != IS_LONG) {
  660. zend_resolve_goto_label(op_array, opline, 1 TSRMLS_CC);
  661. }
  662. /* break omitted intentionally */
  663. case ZEND_JMP:
  664. case ZEND_FAST_CALL:
  665. opline->op1.jmp_addr = &op_array->opcodes[opline->op1.opline_num];
  666. break;
  667. case ZEND_JMPZ:
  668. case ZEND_JMPNZ:
  669. case ZEND_JMPZ_EX:
  670. case ZEND_JMPNZ_EX:
  671. case ZEND_JMP_SET:
  672. case ZEND_JMP_SET_VAR:
  673. opline->op2.jmp_addr = &op_array->opcodes[opline->op2.opline_num];
  674. break;
  675. case ZEND_RETURN:
  676. case ZEND_RETURN_BY_REF:
  677. if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
  678. if (opline->op1_type != IS_CONST || Z_TYPE_P(opline->op1.zv) != IS_NULL) {
  679. CG(zend_lineno) = opline->lineno;
  680. zend_error_noreturn(E_COMPILE_ERROR, "Generators cannot return values using \"return\"");
  681. }
  682. opline->opcode = ZEND_GENERATOR_RETURN;
  683. }
  684. break;
  685. }
  686. ZEND_VM_SET_OPCODE_HANDLER(opline);
  687. opline++;
  688. }
  689. op_array->fn_flags |= ZEND_ACC_DONE_PASS_TWO;
  690. return 0;
  691. }
  692. int print_class(zend_class_entry *class_entry TSRMLS_DC)
  693. {
  694. printf("Class %s:\n", class_entry->name);
  695. zend_hash_apply(&class_entry->function_table, (apply_func_t) pass_two TSRMLS_CC);
  696. printf("End of class %s.\n\n", class_entry->name);
  697. return 0;
  698. }
  699. ZEND_API unary_op_type get_unary_op(int opcode)
  700. {
  701. switch (opcode) {
  702. case ZEND_BW_NOT:
  703. return (unary_op_type) bitwise_not_function;
  704. break;
  705. case ZEND_BOOL_NOT:
  706. return (unary_op_type) boolean_not_function;
  707. break;
  708. default:
  709. return (unary_op_type) NULL;
  710. break;
  711. }
  712. }
  713. ZEND_API binary_op_type get_binary_op(int opcode)
  714. {
  715. switch (opcode) {
  716. case ZEND_ADD:
  717. case ZEND_ASSIGN_ADD:
  718. return (binary_op_type) add_function;
  719. break;
  720. case ZEND_SUB:
  721. case ZEND_ASSIGN_SUB:
  722. return (binary_op_type) sub_function;
  723. break;
  724. case ZEND_MUL:
  725. case ZEND_ASSIGN_MUL:
  726. return (binary_op_type) mul_function;
  727. break;
  728. case ZEND_POW:
  729. return (binary_op_type) pow_function;
  730. break;
  731. case ZEND_DIV:
  732. case ZEND_ASSIGN_DIV:
  733. return (binary_op_type) div_function;
  734. break;
  735. case ZEND_MOD:
  736. case ZEND_ASSIGN_MOD:
  737. return (binary_op_type) mod_function;
  738. break;
  739. case ZEND_SL:
  740. case ZEND_ASSIGN_SL:
  741. return (binary_op_type) shift_left_function;
  742. break;
  743. case ZEND_SR:
  744. case ZEND_ASSIGN_SR:
  745. return (binary_op_type) shift_right_function;
  746. break;
  747. case ZEND_CONCAT:
  748. case ZEND_ASSIGN_CONCAT:
  749. return (binary_op_type) concat_function;
  750. break;
  751. case ZEND_IS_IDENTICAL:
  752. return (binary_op_type) is_identical_function;
  753. break;
  754. case ZEND_IS_NOT_IDENTICAL:
  755. return (binary_op_type) is_not_identical_function;
  756. break;
  757. case ZEND_IS_EQUAL:
  758. return (binary_op_type) is_equal_function;
  759. break;
  760. case ZEND_IS_NOT_EQUAL:
  761. return (binary_op_type) is_not_equal_function;
  762. break;
  763. case ZEND_IS_SMALLER:
  764. return (binary_op_type) is_smaller_function;
  765. break;
  766. case ZEND_IS_SMALLER_OR_EQUAL:
  767. return (binary_op_type) is_smaller_or_equal_function;
  768. break;
  769. case ZEND_BW_OR:
  770. case ZEND_ASSIGN_BW_OR:
  771. return (binary_op_type) bitwise_or_function;
  772. break;
  773. case ZEND_BW_AND:
  774. case ZEND_ASSIGN_BW_AND:
  775. return (binary_op_type) bitwise_and_function;
  776. break;
  777. case ZEND_BW_XOR:
  778. case ZEND_ASSIGN_BW_XOR:
  779. return (binary_op_type) bitwise_xor_function;
  780. break;
  781. case ZEND_BOOL_XOR:
  782. return (binary_op_type) boolean_xor_function;
  783. break;
  784. default:
  785. return (binary_op_type) NULL;
  786. break;
  787. }
  788. }
  789. /*
  790. * Local variables:
  791. * tab-width: 4
  792. * c-basic-offset: 4
  793. * indent-tabs-mode: t
  794. * End:
  795. */