2 * Copyright 2011 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28 WINE_DECLARE_DEBUG_CHANNEL(jscript_disas);
30 typedef struct _statement_ctx_t {
36 unsigned continue_label;
38 const labelled_statement_t *labelled_stat;
40 struct _statement_ctx_t *next;
43 struct _compiler_ctx_t {
54 statement_ctx_t *stat_ctx;
59 instr_arg_type_t arg1_type;
60 instr_arg_type_t arg2_type;
62 #define X(n,a,b,c) {#n,b,c},
67 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
71 TRACE_(jscript_disas)("\t%s", debugstr_w(arg->str));
74 TRACE_(jscript_disas)("\t%s", debugstr_wn(arg->bstr, SysStringLen(arg->bstr)));
77 TRACE_(jscript_disas)("\t%d", arg->uint);
81 TRACE_(jscript_disas)("\t%u", arg->uint);
84 TRACE_(jscript_disas)("\t%lf", *arg->dbl);
94 static void dump_code(compiler_ctx_t *ctx, unsigned off)
98 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
99 TRACE_(jscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
100 dump_instr_arg(instr_info[instr->op].arg1_type, &instr->arg1);
101 dump_instr_arg(instr_info[instr->op].arg2_type, &instr->arg2);
102 TRACE_(jscript_disas)("\n");
106 static HRESULT compile_expression(compiler_ctx_t*,expression_t*);
107 static HRESULT compile_statement(compiler_ctx_t*,statement_ctx_t*,statement_t*);
109 static inline void *compiler_alloc(bytecode_t *code, size_t size)
111 return jsheap_alloc(&code->heap, size);
114 static WCHAR *compiler_alloc_string(bytecode_t *code, const WCHAR *str)
119 size = (strlenW(str)+1)*sizeof(WCHAR);
120 ret = compiler_alloc(code, size);
122 memcpy(ret, str, size);
126 static BSTR compiler_alloc_bstr(compiler_ctx_t *ctx, const WCHAR *str)
128 if(!ctx->code->bstr_pool_size) {
129 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
130 if(!ctx->code->bstr_pool)
132 ctx->code->bstr_pool_size = 8;
133 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
136 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
140 ctx->code->bstr_pool = new_pool;
141 ctx->code->bstr_pool_size *= 2;
144 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
145 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
148 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
151 static unsigned push_instr(compiler_ctx_t *ctx, jsop_t op)
153 assert(ctx->code_size >= ctx->code_off);
155 if(ctx->code_size == ctx->code_off) {
158 new_instrs = heap_realloc(ctx->code->instrs, ctx->code_size*2*sizeof(instr_t));
162 ctx->code->instrs = new_instrs;
166 ctx->code->instrs[ctx->code_off].op = op;
167 return ctx->code_off++;
170 static inline instr_t *instr_ptr(compiler_ctx_t *ctx, unsigned off)
172 assert(off < ctx->code_off);
173 return ctx->code->instrs + off;
176 static HRESULT push_instr_int(compiler_ctx_t *ctx, jsop_t op, LONG arg)
180 instr = push_instr(ctx, op);
182 return E_OUTOFMEMORY;
184 instr_ptr(ctx, instr)->arg1.lng = arg;
188 static HRESULT push_instr_str(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
193 str = compiler_alloc_string(ctx->code, arg);
195 return E_OUTOFMEMORY;
197 instr = push_instr(ctx, op);
199 return E_OUTOFMEMORY;
201 instr_ptr(ctx, instr)->arg1.str = str;
205 static HRESULT push_instr_bstr(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
210 str = compiler_alloc_bstr(ctx, arg);
212 return E_OUTOFMEMORY;
214 instr = push_instr(ctx, op);
216 return E_OUTOFMEMORY;
218 instr_ptr(ctx, instr)->arg1.bstr = str;
222 static HRESULT push_instr_bstr_uint(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg1, unsigned arg2)
227 str = compiler_alloc_bstr(ctx, arg1);
229 return E_OUTOFMEMORY;
231 instr = push_instr(ctx, op);
233 return E_OUTOFMEMORY;
235 instr_ptr(ctx, instr)->arg1.bstr = str;
236 instr_ptr(ctx, instr)->arg2.uint = arg2;
240 static HRESULT push_instr_uint_str(compiler_ctx_t *ctx, jsop_t op, unsigned arg1, const WCHAR *arg2)
245 str = compiler_alloc_string(ctx->code, arg2);
247 return E_OUTOFMEMORY;
249 instr = push_instr(ctx, op);
251 return E_OUTOFMEMORY;
253 instr_ptr(ctx, instr)->arg1.uint = arg1;
254 instr_ptr(ctx, instr)->arg2.str = str;
258 static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
263 dbl = compiler_alloc(ctx->code, sizeof(arg));
265 return E_OUTOFMEMORY;
268 instr = push_instr(ctx, op);
270 return E_OUTOFMEMORY;
272 instr_ptr(ctx, instr)->arg1.dbl = dbl;
276 static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
280 instr = push_instr(ctx, op);
282 return E_OUTOFMEMORY;
284 instr_ptr(ctx, instr)->arg1.uint = arg;
288 static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
292 hres = compile_expression(ctx, expr->expression1);
296 hres = compile_expression(ctx, expr->expression2);
300 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
303 static HRESULT compile_unary_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op)
307 hres = compile_expression(ctx, expr->expression);
311 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
314 /* ECMA-262 3rd Edition 11.2.1 */
315 static HRESULT compile_member_expression(compiler_ctx_t *ctx, member_expression_t *expr)
319 hres = compile_expression(ctx, expr->expression);
323 return push_instr_bstr(ctx, OP_member, expr->identifier);
326 #define LABEL_FLAG 0x80000000
328 static unsigned alloc_label(compiler_ctx_t *ctx)
330 if(!ctx->labels_size) {
331 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
334 ctx->labels_size = 8;
335 }else if(ctx->labels_size == ctx->labels_cnt) {
336 unsigned *new_labels;
338 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
342 ctx->labels = new_labels;
343 ctx->labels_size *= 2;
346 return ctx->labels_cnt++ | LABEL_FLAG;
349 static void label_set_addr(compiler_ctx_t *ctx, unsigned label)
351 assert(label & LABEL_FLAG);
352 ctx->labels[label & ~LABEL_FLAG] = ctx->code_off;
355 static inline BOOL is_memberid_expr(expression_type_t type)
357 return type == EXPR_IDENT || type == EXPR_MEMBER || type == EXPR_ARRAY;
360 static HRESULT compile_memberid_expression(compiler_ctx_t *ctx, expression_t *expr, unsigned flags)
366 identifier_expression_t *ident_expr = (identifier_expression_t*)expr;
368 hres = push_instr_bstr_uint(ctx, OP_identid, ident_expr->identifier, flags);
372 binary_expression_t *array_expr = (binary_expression_t*)expr;
374 hres = compile_expression(ctx, array_expr->expression1);
378 hres = compile_expression(ctx, array_expr->expression2);
382 hres = push_instr_uint(ctx, OP_memberid, flags);
386 member_expression_t *member_expr = (member_expression_t*)expr;
388 hres = compile_expression(ctx, member_expr->expression);
392 /* FIXME: Potential optimization */
393 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
397 hres = push_instr_uint(ctx, OP_memberid, flags);
407 static HRESULT compile_increment_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op, int n)
411 if(!is_memberid_expr(expr->expression->type)) {
412 hres = compile_expression(ctx, expr->expression);
416 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
419 hres = compile_memberid_expression(ctx, expr->expression, fdexNameEnsure);
423 return push_instr_int(ctx, op, n);
426 /* ECMA-262 3rd Edition 11.14 */
427 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr)
431 hres = compile_expression(ctx, expr->expression1);
435 if(!push_instr(ctx, OP_pop))
436 return E_OUTOFMEMORY;
438 return compile_expression(ctx, expr->expression2);
441 /* ECMA-262 3rd Edition 11.11 */
442 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
447 hres = compile_expression(ctx, expr->expression1);
451 instr = push_instr(ctx, op);
453 return E_OUTOFMEMORY;
455 hres = compile_expression(ctx, expr->expression2);
459 instr_ptr(ctx, instr)->arg1.uint = ctx->code_off;
463 /* ECMA-262 3rd Edition 11.12 */
464 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
466 unsigned jmp_false, jmp_end;
469 hres = compile_expression(ctx, expr->expression);
473 jmp_false = push_instr(ctx, OP_cnd_z);
475 return E_OUTOFMEMORY;
477 hres = compile_expression(ctx, expr->true_expression);
481 jmp_end = push_instr(ctx, OP_jmp);
483 return E_OUTOFMEMORY;
485 instr_ptr(ctx, jmp_false)->arg1.uint = ctx->code_off;
486 if(!push_instr(ctx, OP_pop))
487 return E_OUTOFMEMORY;
489 hres = compile_expression(ctx, expr->false_expression);
493 instr_ptr(ctx, jmp_end)->arg1.uint = ctx->code_off;
497 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
499 unsigned arg_cnt = 0;
503 hres = compile_expression(ctx, expr->expression);
507 for(arg = expr->argument_list; arg; arg = arg->next) {
508 hres = compile_expression(ctx, arg->expr);
514 return push_instr_int(ctx, OP_new, arg_cnt);
517 static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL *no_ret)
519 unsigned arg_cnt = 0;
525 if(is_memberid_expr(expr->expression->type)) {
527 hres = compile_memberid_expression(ctx, expr->expression, 0);
530 hres = compile_expression(ctx, expr->expression);
536 for(arg = expr->argument_list; arg; arg = arg->next) {
537 hres = compile_expression(ctx, arg->expr);
543 instr = push_instr(ctx, op);
545 return E_OUTOFMEMORY;
547 instr_ptr(ctx, instr)->arg1.uint = arg_cnt;
548 instr_ptr(ctx, instr)->arg2.lng = no_ret == NULL;
554 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
558 switch(expr->expression->type) {
560 binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
562 hres = compile_expression(ctx, array_expr->expression1);
566 hres = compile_expression(ctx, array_expr->expression2);
570 if(!push_instr(ctx, OP_delete))
571 return E_OUTOFMEMORY;
575 member_expression_t *member_expr = (member_expression_t*)expr->expression;
577 hres = compile_expression(ctx, member_expr->expression);
581 /* FIXME: Potential optimization */
582 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
586 if(!push_instr(ctx, OP_delete))
587 return E_OUTOFMEMORY;
591 return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
593 const WCHAR fixmeW[] = {'F','I','X','M','E',0};
595 WARN("invalid delete, unimplemented exception message\n");
597 hres = compile_expression(ctx, expr->expression);
601 return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, fixmeW);
608 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
612 if(!is_memberid_expr(expr->expression1->type)) {
613 hres = compile_expression(ctx, expr->expression1);
617 hres = compile_expression(ctx, expr->expression2);
621 if(op != OP_LAST && !push_instr(ctx, op))
622 return E_OUTOFMEMORY;
624 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
627 hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
631 if(op != OP_LAST && !push_instr(ctx, OP_refval))
632 return E_OUTOFMEMORY;
634 hres = compile_expression(ctx, expr->expression2);
638 if(op != OP_LAST && !push_instr(ctx, op))
639 return E_OUTOFMEMORY;
641 if(!push_instr(ctx, OP_assign))
642 return E_OUTOFMEMORY;
647 static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
652 if(is_memberid_expr(expr->expression->type)) {
653 if(expr->expression->type == EXPR_IDENT)
654 return push_instr_str(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
657 hres = compile_memberid_expression(ctx, expr->expression, 0);
660 hres = compile_expression(ctx, expr->expression);
665 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
668 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
670 switch(literal->type) {
672 return push_instr_int(ctx, OP_bool, literal->u.bval);
674 return push_instr_double(ctx, OP_double, literal->u.dval);
676 return push_instr_int(ctx, OP_int, literal->u.lval);
678 return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
680 return push_instr_str(ctx, OP_str, literal->u.wstr);
685 str = compiler_alloc(ctx->code, (literal->u.regexp.str_len+1)*sizeof(WCHAR));
687 return E_OUTOFMEMORY;
688 memcpy(str, literal->u.regexp.str, literal->u.regexp.str_len*sizeof(WCHAR));
689 str[literal->u.regexp.str_len] = 0;
691 instr = push_instr(ctx, OP_regexp);
693 return E_OUTOFMEMORY;
695 instr_ptr(ctx, instr)->arg1.str = str;
696 instr_ptr(ctx, instr)->arg2.lng = literal->u.regexp.flags;
704 static HRESULT literal_as_bstr(compiler_ctx_t *ctx, literal_t *literal, BSTR *str)
706 switch(literal->type) {
708 *str = compiler_alloc_bstr(ctx, literal->u.wstr);
711 *str = int_to_bstr(literal->u.lval);
714 return double_to_bstr(literal->u.dval, str);
719 return *str ? S_OK : E_OUTOFMEMORY;
722 static HRESULT compile_array_literal(compiler_ctx_t *ctx, array_literal_expression_t *expr)
724 unsigned i, elem_cnt = expr->length;
725 array_element_t *iter;
728 for(iter = expr->element_list; iter; iter = iter->next) {
729 elem_cnt += iter->elision+1;
731 for(i=0; i < iter->elision; i++) {
732 if(!push_instr(ctx, OP_undefined))
733 return E_OUTOFMEMORY;
736 hres = compile_expression(ctx, iter->expr);
741 for(i=0; i < expr->length; i++) {
742 if(!push_instr(ctx, OP_undefined))
743 return E_OUTOFMEMORY;
746 return push_instr_uint(ctx, OP_carray, elem_cnt);
749 static HRESULT compile_object_literal(compiler_ctx_t *ctx, property_value_expression_t *expr)
756 if(!push_instr(ctx, OP_new_obj))
757 return E_OUTOFMEMORY;
759 for(iter = expr->property_list; iter; iter = iter->next) {
760 hres = literal_as_bstr(ctx, iter->name, &name);
764 hres = compile_expression(ctx, iter->value);
768 instr = push_instr(ctx, OP_obj_prop);
770 return E_OUTOFMEMORY;
772 instr_ptr(ctx, instr)->arg1.bstr = name;
778 static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr)
782 /* FIXME: not exactly right */
784 return push_instr_bstr(ctx, OP_ident, expr->identifier);
786 instr = push_instr(ctx, OP_func);
788 return E_OUTOFMEMORY;
790 instr_ptr(ctx, instr)->arg1.func = expr;
794 static HRESULT compile_expression_noret(compiler_ctx_t *ctx, expression_t *expr, BOOL *no_ret)
798 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
800 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_z);
802 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
804 return compile_array_literal(ctx, (array_literal_expression_t*)expr);
806 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
808 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
810 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_and);
812 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
814 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
816 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
818 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
820 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
821 case EXPR_ASSIGNLSHIFT:
822 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_lshift);
823 case EXPR_ASSIGNRSHIFT:
824 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift);
825 case EXPR_ASSIGNRRSHIFT:
826 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
828 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
830 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
832 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
834 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
836 return compile_call_expression(ctx, (call_expression_t*)expr, no_ret);
838 return compile_comma_expression(ctx, (binary_expression_t*)expr);
840 return compile_conditional_expression(ctx, (conditional_expression_t*)expr);
842 return compile_delete_expression(ctx, (unary_expression_t*)expr);
844 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
846 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
848 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
850 return compile_function_expression(ctx, (function_expression_t*)expr);
852 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
854 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
856 return push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
858 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
859 case EXPR_INSTANCEOF:
860 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
862 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
864 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
866 return compile_literal(ctx, ((literal_expression_t*)expr)->literal);
868 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
870 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
872 return compile_member_expression(ctx, (member_expression_t*)expr);
874 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
876 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
878 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
880 return compile_new_expression(ctx, (call_expression_t*)expr);
882 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
884 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
886 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_nz);
888 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
890 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
892 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
894 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
896 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
898 return compile_object_literal(ctx, (property_value_expression_t*)expr);
900 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
902 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
904 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
906 return push_instr(ctx, OP_this) ? S_OK : E_OUTOFMEMORY;
908 return compile_typeof_expression(ctx, (unary_expression_t*)expr);
910 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
912 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
920 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr)
922 return compile_expression_noret(ctx, expr, NULL);
925 static inline BOOL is_loop_statement(statement_type_t type)
927 return type == STAT_FOR || type == STAT_FORIN || type == STAT_WHILE;
930 /* ECMA-262 3rd Edition 12.1 */
931 static HRESULT compile_block_statement(compiler_ctx_t *ctx, statement_t *iter)
935 /* FIXME: do it only if needed */
937 return push_instr(ctx, OP_undefined) ? S_OK : E_OUTOFMEMORY;
940 hres = compile_statement(ctx, NULL, iter);
948 if(!push_instr(ctx, OP_pop))
949 return E_OUTOFMEMORY;
955 /* ECMA-262 3rd Edition 12.2 */
956 static HRESULT compile_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
958 variable_declaration_t *iter;
961 for(iter = list; iter; iter = iter->next) {
965 hres = compile_expression(ctx, iter->expr);
969 hres = push_instr_bstr(ctx, OP_var_set, iter->identifier);
977 /* ECMA-262 3rd Edition 12.2 */
978 static HRESULT compile_var_statement(compiler_ctx_t *ctx, var_statement_t *stat)
982 hres = compile_variable_list(ctx, stat->variable_list);
986 return push_instr(ctx, OP_undefined) ? S_OK : E_OUTOFMEMORY;
989 /* ECMA-262 3rd Edition 12.4 */
990 static HRESULT compile_expression_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
995 hres = compile_expression_noret(ctx, stat->expr, &no_ret);
999 /* FIXME: that's a big potential optimization */
1000 if(no_ret && !push_instr(ctx, OP_undefined))
1001 return E_OUTOFMEMORY;
1006 /* ECMA-262 3rd Edition 12.5 */
1007 static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat)
1009 unsigned jmp_else, jmp_end;
1012 hres = compile_expression(ctx, stat->expr);
1016 jmp_else = push_instr(ctx, OP_jmp_z);
1018 return E_OUTOFMEMORY;
1020 hres = compile_statement(ctx, NULL, stat->if_stat);
1024 jmp_end = push_instr(ctx, OP_jmp);
1026 return E_OUTOFMEMORY;
1028 instr_ptr(ctx, jmp_else)->arg1.uint = ctx->code_off;
1030 if(stat->else_stat) {
1031 hres = compile_statement(ctx, NULL, stat->else_stat);
1035 /* FIXME: We could sometimes avoid it */
1036 if(!push_instr(ctx, OP_undefined))
1037 return E_OUTOFMEMORY;
1040 instr_ptr(ctx, jmp_end)->arg1.uint = ctx->code_off;
1044 /* ECMA-262 3rd Edition 12.6.2 */
1045 static HRESULT compile_while_statement(compiler_ctx_t *ctx, while_statement_t *stat)
1047 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1051 stat_ctx.break_label = alloc_label(ctx);
1052 if(!stat_ctx.break_label)
1053 return E_OUTOFMEMORY;
1055 stat_ctx.continue_label = alloc_label(ctx);
1056 if(!stat_ctx.continue_label)
1057 return E_OUTOFMEMORY;
1059 if(!stat->do_while) {
1061 if(!push_instr(ctx, OP_undefined))
1062 return E_OUTOFMEMORY;
1064 jmp_off = ctx->code_off;
1065 label_set_addr(ctx, stat_ctx.continue_label);
1066 hres = compile_expression(ctx, stat->expr);
1070 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1074 if(!push_instr(ctx, OP_pop))
1075 return E_OUTOFMEMORY;
1077 jmp_off = ctx->code_off;
1080 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1084 if(stat->do_while) {
1085 label_set_addr(ctx, stat_ctx.continue_label);
1086 hres = compile_expression(ctx, stat->expr);
1090 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1094 if(!push_instr(ctx, OP_pop))
1095 return E_OUTOFMEMORY;
1098 hres = push_instr_uint(ctx, OP_jmp, jmp_off);
1102 label_set_addr(ctx, stat_ctx.break_label);
1106 /* ECMA-262 3rd Edition 12.6.3 */
1107 static HRESULT compile_for_statement(compiler_ctx_t *ctx, for_statement_t *stat)
1109 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1113 if(stat->variable_list) {
1114 hres = compile_variable_list(ctx, stat->variable_list);
1117 }else if(stat->begin_expr) {
1118 BOOL no_ret = FALSE;
1120 hres = compile_expression_noret(ctx, stat->begin_expr, &no_ret);
1123 if(!no_ret && !push_instr(ctx, OP_pop))
1124 return E_OUTOFMEMORY;
1127 stat_ctx.break_label = alloc_label(ctx);
1128 if(!stat_ctx.break_label)
1129 return E_OUTOFMEMORY;
1131 stat_ctx.continue_label = alloc_label(ctx);
1132 if(!stat_ctx.continue_label)
1133 return E_OUTOFMEMORY;
1136 if(!push_instr(ctx, OP_undefined))
1137 return E_OUTOFMEMORY;
1139 expr_off = ctx->code_off;
1142 hres = compile_expression(ctx, stat->expr);
1146 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1151 if(!push_instr(ctx, OP_pop))
1152 return E_OUTOFMEMORY;
1154 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1158 label_set_addr(ctx, stat_ctx.continue_label);
1160 if(stat->end_expr) {
1161 BOOL no_ret = FALSE;
1163 hres = compile_expression_noret(ctx, stat->end_expr, &no_ret);
1167 if(!no_ret && !push_instr(ctx, OP_pop))
1168 return E_OUTOFMEMORY;
1171 hres = push_instr_uint(ctx, OP_jmp, expr_off);
1175 label_set_addr(ctx, stat_ctx.break_label);
1179 /* ECMA-262 3rd Edition 12.6.4 */
1180 static HRESULT compile_forin_statement(compiler_ctx_t *ctx, forin_statement_t *stat)
1182 statement_ctx_t stat_ctx = {4, FALSE, FALSE};
1185 if(stat->variable) {
1186 hres = compile_variable_list(ctx, stat->variable);
1191 stat_ctx.break_label = alloc_label(ctx);
1192 if(!stat_ctx.break_label)
1193 return E_OUTOFMEMORY;
1195 stat_ctx.continue_label = alloc_label(ctx);
1196 if(!stat_ctx.continue_label)
1197 return E_OUTOFMEMORY;
1199 hres = compile_expression(ctx, stat->in_expr);
1203 if(stat->variable) {
1204 hres = push_instr_bstr_uint(ctx, OP_identid, stat->variable->identifier, fdexNameEnsure);
1207 }else if(is_memberid_expr(stat->expr->type)) {
1208 hres = compile_memberid_expression(ctx, stat->expr, fdexNameEnsure);
1212 hres = push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
1216 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1220 hres = push_instr_int(ctx, OP_int, DISPID_STARTENUM);
1225 if(!push_instr(ctx, OP_undefined))
1226 return E_OUTOFMEMORY;
1228 label_set_addr(ctx, stat_ctx.continue_label);
1229 hres = push_instr_uint(ctx, OP_forin, stat_ctx.break_label);
1231 return E_OUTOFMEMORY;
1233 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1237 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.continue_label);
1241 label_set_addr(ctx, stat_ctx.break_label);
1245 static HRESULT pop_to_stat(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx)
1247 unsigned stack_pop = 0;
1248 statement_ctx_t *iter;
1250 for(iter = ctx->stat_ctx; iter != stat_ctx; iter = iter->next) {
1251 if(iter->using_scope && !push_instr(ctx, OP_pop_scope))
1252 return E_OUTOFMEMORY;
1253 if(iter->using_except && !push_instr(ctx, OP_pop_except))
1254 return E_OUTOFMEMORY;
1255 stack_pop += iter->stack_use;
1258 /* FIXME: optimize */
1259 while(stack_pop--) {
1260 if(!push_instr(ctx, OP_pop))
1261 return E_OUTOFMEMORY;
1267 /* ECMA-262 3rd Edition 12.7 */
1268 static HRESULT compile_continue_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1270 statement_ctx_t *pop_ctx;
1273 if(stat->identifier) {
1274 statement_t *label_stat;
1275 statement_ctx_t *iter;
1279 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1280 if(iter->continue_label)
1282 if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier))
1287 WARN("Label not found\n");
1288 return JS_E_LABEL_NOT_FOUND;
1291 /* Labelled continue are allowed only on loops */
1292 for(label_stat = iter->labelled_stat->statement;
1293 label_stat->type == STAT_LABEL;
1294 label_stat = ((labelled_statement_t*)label_stat)->statement);
1295 if(!is_loop_statement(label_stat->type)) {
1296 WARN("Label is not a loop\n");
1297 return JS_E_INVALID_CONTINUE;
1300 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1301 if(pop_ctx->continue_label)
1306 WARN("continue outside loop\n");
1307 return JS_E_INVALID_CONTINUE;
1311 hres = pop_to_stat(ctx, pop_ctx);
1315 if(!push_instr(ctx, OP_undefined))
1316 return E_OUTOFMEMORY;
1318 return push_instr_uint(ctx, OP_jmp, pop_ctx->continue_label);
1321 /* ECMA-262 3rd Edition 12.8 */
1322 static HRESULT compile_break_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1324 statement_ctx_t *pop_ctx;
1327 if(stat->identifier) {
1328 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1329 if(pop_ctx->labelled_stat && !strcmpW(pop_ctx->labelled_stat->identifier, stat->identifier)) {
1330 assert(pop_ctx->break_label);
1336 WARN("Label not found\n");
1337 return JS_E_LABEL_NOT_FOUND;
1340 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1341 if(pop_ctx->break_label && !pop_ctx->labelled_stat)
1346 WARN("Break outside loop\n");
1347 return JS_E_INVALID_BREAK;
1351 hres = pop_to_stat(ctx, pop_ctx->next);
1355 if(!push_instr(ctx, OP_undefined))
1356 return E_OUTOFMEMORY;
1358 return push_instr_uint(ctx, OP_jmp, pop_ctx->break_label);
1361 /* ECMA-262 3rd Edition 12.9 */
1362 static HRESULT compile_return_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1366 hres = pop_to_stat(ctx, NULL);
1371 hres = compile_expression(ctx, stat->expr);
1376 return push_instr(ctx, OP_ret) ? S_OK : E_OUTOFMEMORY;
1379 /* ECMA-262 3rd Edition 12.10 */
1380 static HRESULT compile_with_statement(compiler_ctx_t *ctx, with_statement_t *stat)
1382 statement_ctx_t stat_ctx = {0, TRUE, FALSE};
1385 hres = compile_expression(ctx, stat->expr);
1389 if(!push_instr(ctx, OP_push_scope))
1390 return E_OUTOFMEMORY;
1392 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1396 if(!push_instr(ctx, OP_pop_scope))
1397 return E_OUTOFMEMORY;
1402 /* ECMA-262 3rd Edition 12.10 */
1403 static HRESULT compile_labelled_statement(compiler_ctx_t *ctx, labelled_statement_t *stat)
1405 statement_ctx_t stat_ctx = {0, FALSE, FALSE, 0, 0, stat}, *iter;
1408 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1409 if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier)) {
1410 WARN("Label %s redefined\n", debugstr_w(stat->identifier));
1411 return JS_E_LABEL_REDEFINED;
1415 /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1416 stat_ctx.break_label = alloc_label(ctx);
1417 if(!stat_ctx.break_label)
1418 return E_OUTOFMEMORY;
1420 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1424 label_set_addr(ctx, stat_ctx.break_label);
1428 /* ECMA-262 3rd Edition 12.13 */
1429 static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat)
1431 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1432 unsigned case_cnt = 0, *case_jmps, i, default_jmp;
1433 BOOL have_default = FALSE;
1434 statement_t *stat_iter;
1435 case_clausule_t *iter;
1438 hres = compile_expression(ctx, stat->expr);
1442 stat_ctx.break_label = alloc_label(ctx);
1443 if(!stat_ctx.break_label)
1444 return E_OUTOFMEMORY;
1446 for(iter = stat->case_list; iter; iter = iter->next) {
1451 case_jmps = heap_alloc(case_cnt * sizeof(*case_jmps));
1453 return E_OUTOFMEMORY;
1456 for(iter = stat->case_list; iter; iter = iter->next) {
1458 have_default = TRUE;
1462 hres = compile_expression(ctx, iter->expr);
1466 case_jmps[i] = push_instr(ctx, OP_case);
1468 hres = E_OUTOFMEMORY;
1474 if(SUCCEEDED(hres)) {
1475 if(push_instr(ctx, OP_pop)) {
1476 default_jmp = push_instr(ctx, OP_jmp);
1478 hres = E_OUTOFMEMORY;
1480 hres = E_OUTOFMEMORY;
1485 heap_free(case_jmps);
1490 for(iter = stat->case_list; iter; iter = iter->next) {
1491 while(iter->next && iter->next->stat == iter->stat) {
1492 instr_ptr(ctx, iter->expr ? case_jmps[i++] : default_jmp)->arg1.uint = ctx->code_off;
1496 instr_ptr(ctx, iter->expr ? case_jmps[i++] : default_jmp)->arg1.uint = ctx->code_off;
1498 for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter); stat_iter = stat_iter->next) {
1499 hres = compile_statement(ctx, &stat_ctx, stat_iter);
1503 if(stat_iter->next && !push_instr(ctx, OP_pop)) {
1504 hres = E_OUTOFMEMORY;
1512 heap_free(case_jmps);
1515 assert(i == case_cnt);
1518 instr_ptr(ctx, default_jmp)->arg1.uint = ctx->code_off;
1520 label_set_addr(ctx, stat_ctx.break_label);
1524 /* ECMA-262 3rd Edition 12.13 */
1525 static HRESULT compile_throw_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1529 hres = compile_expression(ctx, stat->expr);
1533 return push_instr(ctx, OP_throw) ? S_OK : E_OUTOFMEMORY;
1536 /* ECMA-262 3rd Edition 12.14 */
1537 static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat)
1539 statement_ctx_t try_ctx = {0, FALSE, TRUE}, catch_ctx = {0, TRUE, FALSE};
1540 statement_ctx_t finally_ctx = {2, FALSE, FALSE};
1541 unsigned push_except;
1545 push_except = push_instr(ctx, OP_push_except);
1547 return E_OUTOFMEMORY;
1549 if(stat->catch_block) {
1550 ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier);
1552 return E_OUTOFMEMORY;
1557 instr_ptr(ctx, push_except)->arg2.bstr = ident;
1559 if(!stat->catch_block)
1560 try_ctx.stack_use = 2;
1562 hres = compile_statement(ctx, &try_ctx, stat->try_statement);
1566 if(!push_instr(ctx, OP_pop_except))
1567 return E_OUTOFMEMORY;
1569 if(stat->catch_block) {
1570 unsigned jmp_finally;
1572 jmp_finally = push_instr(ctx, OP_jmp);
1574 return E_OUTOFMEMORY;
1576 instr_ptr(ctx, push_except)->arg1.uint = ctx->code_off;
1578 hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement);
1582 if(!push_instr(ctx, OP_pop_scope))
1583 return E_OUTOFMEMORY;
1585 instr_ptr(ctx, jmp_finally)->arg1.uint = ctx->code_off;
1587 instr_ptr(ctx, push_except)->arg1.uint = ctx->code_off;
1590 if(stat->finally_statement) {
1592 if(!push_instr(ctx, OP_pop))
1593 return E_OUTOFMEMORY;
1595 hres = compile_statement(ctx, stat->catch_block ? NULL : &finally_ctx, stat->finally_statement);
1599 if(!stat->catch_block && !push_instr(ctx, OP_end_finally))
1600 return E_OUTOFMEMORY;
1606 static HRESULT compile_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1611 stat_ctx->next = ctx->stat_ctx;
1612 ctx->stat_ctx = stat_ctx;
1615 switch(stat->type) {
1617 hres = compile_block_statement(ctx, ((block_statement_t*)stat)->stat_list);
1620 hres = compile_break_statement(ctx, (branch_statement_t*)stat);
1623 hres = compile_continue_statement(ctx, (branch_statement_t*)stat);
1626 hres = push_instr(ctx, OP_undefined) ? S_OK : E_OUTOFMEMORY; /* FIXME */
1629 hres = compile_expression_statement(ctx, (expression_statement_t*)stat);
1632 hres = compile_for_statement(ctx, (for_statement_t*)stat);
1635 hres = compile_forin_statement(ctx, (forin_statement_t*)stat);
1638 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1641 hres = compile_labelled_statement(ctx, (labelled_statement_t*)stat);
1644 hres = compile_return_statement(ctx, (expression_statement_t*)stat);
1647 hres = compile_switch_statement(ctx, (switch_statement_t*)stat);
1650 hres = compile_throw_statement(ctx, (expression_statement_t*)stat);
1653 hres = compile_try_statement(ctx, (try_statement_t*)stat);
1656 hres = compile_var_statement(ctx, (var_statement_t*)stat);
1659 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1662 hres = compile_with_statement(ctx, (with_statement_t*)stat);
1669 assert(ctx->stat_ctx == stat_ctx);
1670 ctx->stat_ctx = stat_ctx->next;
1676 static void resolve_labels(compiler_ctx_t *ctx, unsigned off)
1680 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
1681 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
1682 assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
1683 instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
1685 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
1688 ctx->labels_cnt = 0;
1691 void release_bytecode(bytecode_t *code)
1695 for(i=0; i < code->bstr_cnt; i++)
1696 SysFreeString(code->bstr_pool[i]);
1698 jsheap_free(&code->heap);
1699 heap_free(code->bstr_pool);
1700 heap_free(code->instrs);
1704 void release_compiler(compiler_ctx_t *ctx)
1709 static HRESULT init_compiler(parser_ctx_t *parser)
1711 compiler_ctx_t *compiler;
1713 if(parser->compiler)
1716 compiler = heap_alloc_zero(sizeof(*compiler));
1718 return E_OUTOFMEMORY;
1720 compiler->code = heap_alloc_zero(sizeof(bytecode_t));
1721 if(!compiler->code) {
1722 release_compiler(compiler);
1723 return E_OUTOFMEMORY;
1726 jsheap_init(&compiler->code->heap);
1728 compiler->code->instrs = heap_alloc(64 * sizeof(instr_t));
1729 if(!compiler->code->instrs) {
1730 release_bytecode(compiler->code);
1731 release_compiler(compiler);
1732 return E_OUTOFMEMORY;
1735 compiler->code_size = 64;
1736 compiler->code_off = 1;
1738 compiler->parser = parser;
1740 parser->code = compiler->code;
1741 parser->compiler = compiler;
1745 HRESULT compile_subscript_stat(parser_ctx_t *parser, statement_t *stat, BOOL from_eval, unsigned *ret_off)
1752 hres = init_compiler(parser);
1756 off = parser->compiler->code_off;
1758 hres = compile_block_statement(parser->compiler, stat);
1760 hres = compile_statement(parser->compiler, NULL, stat);
1764 resolve_labels(parser->compiler, off);
1766 if(!from_eval && !push_instr(parser->compiler, OP_pop))
1767 return E_OUTOFMEMORY;
1768 if(!push_instr(parser->compiler, OP_ret))
1769 return E_OUTOFMEMORY;
1771 if(TRACE_ON(jscript_disas))
1772 dump_code(parser->compiler, off);