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);
29 typedef struct _statement_ctx_t {
35 unsigned continue_label;
37 struct _statement_ctx_t *next;
40 struct _compiler_ctx_t {
51 statement_ctx_t *stat_ctx;
56 instr_arg_type_t arg1_type;
57 instr_arg_type_t arg2_type;
59 #define X(n,a,b,c) {#n,b,c},
64 static HRESULT compile_expression(compiler_ctx_t*,expression_t*);
65 static HRESULT compile_statement(compiler_ctx_t*,statement_ctx_t*,statement_t*);
67 static inline void *compiler_alloc(bytecode_t *code, size_t size)
69 return jsheap_alloc(&code->heap, size);
72 static WCHAR *compiler_alloc_string(bytecode_t *code, const WCHAR *str)
77 size = (strlenW(str)+1)*sizeof(WCHAR);
78 ret = compiler_alloc(code, size);
80 memcpy(ret, str, size);
84 static BSTR compiler_alloc_bstr(compiler_ctx_t *ctx, const WCHAR *str)
86 if(!ctx->code->bstr_pool_size) {
87 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
88 if(!ctx->code->bstr_pool)
90 ctx->code->bstr_pool_size = 8;
91 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
94 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
98 ctx->code->bstr_pool = new_pool;
99 ctx->code->bstr_pool_size *= 2;
102 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
103 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
106 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
109 static unsigned push_instr(compiler_ctx_t *ctx, jsop_t op)
111 assert(ctx->code_size >= ctx->code_off);
113 if(!ctx->code_size) {
114 ctx->code->instrs = heap_alloc(64 * sizeof(instr_t));
115 if(!ctx->code->instrs)
118 }else if(ctx->code_size == ctx->code_off) {
121 new_instrs = heap_realloc(ctx->code->instrs, ctx->code_size*2*sizeof(instr_t));
125 ctx->code->instrs = new_instrs;
129 ctx->code->instrs[ctx->code_off].op = op;
130 return ctx->code_off++;
133 static inline instr_t *instr_ptr(compiler_ctx_t *ctx, unsigned off)
135 assert(off < ctx->code_off);
136 return ctx->code->instrs + off;
139 static HRESULT push_instr_int(compiler_ctx_t *ctx, jsop_t op, LONG arg)
143 instr = push_instr(ctx, op);
145 return E_OUTOFMEMORY;
147 instr_ptr(ctx, instr)->arg1.lng = arg;
151 static HRESULT push_instr_str(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
156 str = compiler_alloc_string(ctx->code, arg);
158 return E_OUTOFMEMORY;
160 instr = push_instr(ctx, op);
162 return E_OUTOFMEMORY;
164 instr_ptr(ctx, instr)->arg1.str = str;
168 static HRESULT push_instr_bstr(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
173 str = compiler_alloc_bstr(ctx, arg);
175 return E_OUTOFMEMORY;
177 instr = push_instr(ctx, op);
179 return E_OUTOFMEMORY;
181 instr_ptr(ctx, instr)->arg1.bstr = str;
185 static HRESULT push_instr_bstr_uint(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg1, unsigned arg2)
190 str = compiler_alloc_bstr(ctx, arg1);
192 return E_OUTOFMEMORY;
194 instr = push_instr(ctx, op);
196 return E_OUTOFMEMORY;
198 instr_ptr(ctx, instr)->arg1.bstr = str;
199 instr_ptr(ctx, instr)->arg2.uint = arg2;
203 static HRESULT push_instr_uint_str(compiler_ctx_t *ctx, jsop_t op, unsigned arg1, const WCHAR *arg2)
208 str = compiler_alloc_string(ctx->code, arg2);
210 return E_OUTOFMEMORY;
212 instr = push_instr(ctx, op);
214 return E_OUTOFMEMORY;
216 instr_ptr(ctx, instr)->arg1.uint = arg1;
217 instr_ptr(ctx, instr)->arg2.str = str;
221 static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
226 dbl = compiler_alloc(ctx->code, sizeof(arg));
228 return E_OUTOFMEMORY;
231 instr = push_instr(ctx, op);
233 return E_OUTOFMEMORY;
235 instr_ptr(ctx, instr)->arg1.dbl = dbl;
239 static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
243 instr = push_instr(ctx, op);
245 return E_OUTOFMEMORY;
247 instr_ptr(ctx, instr)->arg1.uint = arg;
251 static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
255 hres = compile_expression(ctx, expr->expression1);
259 hres = compile_expression(ctx, expr->expression2);
263 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
266 static HRESULT compile_unary_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op)
270 hres = compile_expression(ctx, expr->expression);
274 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
277 /* ECMA-262 3rd Edition 11.2.1 */
278 static HRESULT compile_member_expression(compiler_ctx_t *ctx, member_expression_t *expr)
282 hres = compile_expression(ctx, expr->expression);
286 return push_instr_bstr(ctx, OP_member, expr->identifier);
289 #define LABEL_FLAG 0x80000000
291 static unsigned alloc_label(compiler_ctx_t *ctx)
293 if(!ctx->labels_size) {
294 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
297 ctx->labels_size = 8;
298 }else if(ctx->labels_size == ctx->labels_cnt) {
299 unsigned *new_labels;
301 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
305 ctx->labels = new_labels;
306 ctx->labels_size *= 2;
309 return ctx->labels_cnt++ | LABEL_FLAG;
312 static void label_set_addr(compiler_ctx_t *ctx, unsigned label)
314 assert(label & LABEL_FLAG);
315 ctx->labels[label & ~LABEL_FLAG] = ctx->code_off;
318 static inline BOOL is_memberid_expr(expression_type_t type)
320 return type == EXPR_IDENT || type == EXPR_MEMBER || type == EXPR_ARRAY;
323 static HRESULT compile_memberid_expression(compiler_ctx_t *ctx, expression_t *expr, unsigned flags)
329 identifier_expression_t *ident_expr = (identifier_expression_t*)expr;
331 hres = push_instr_bstr_uint(ctx, OP_identid, ident_expr->identifier, flags);
335 binary_expression_t *array_expr = (binary_expression_t*)expr;
337 hres = compile_expression(ctx, array_expr->expression1);
341 hres = compile_expression(ctx, array_expr->expression2);
345 hres = push_instr_uint(ctx, OP_memberid, flags);
349 member_expression_t *member_expr = (member_expression_t*)expr;
351 hres = compile_expression(ctx, member_expr->expression);
355 /* FIXME: Potential optimization */
356 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
360 hres = push_instr_uint(ctx, OP_memberid, flags);
370 static HRESULT compile_increment_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op, int n)
374 if(!is_memberid_expr(expr->expression->type)) {
375 hres = compile_expression(ctx, expr->expression);
379 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
382 hres = compile_memberid_expression(ctx, expr->expression, fdexNameEnsure);
386 return push_instr_int(ctx, op, n);
389 /* ECMA-262 3rd Edition 11.14 */
390 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr)
394 hres = compile_expression(ctx, expr->expression1);
398 if(push_instr(ctx, OP_pop) == -1)
399 return E_OUTOFMEMORY;
401 return compile_expression(ctx, expr->expression2);
404 /* ECMA-262 3rd Edition 11.11 */
405 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
410 hres = compile_expression(ctx, expr->expression1);
414 instr = push_instr(ctx, op);
416 return E_OUTOFMEMORY;
418 hres = compile_expression(ctx, expr->expression2);
422 instr_ptr(ctx, instr)->arg1.uint = ctx->code_off;
426 /* ECMA-262 3rd Edition 11.12 */
427 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
429 unsigned jmp_false, jmp_end;
432 hres = compile_expression(ctx, expr->expression);
436 jmp_false = push_instr(ctx, OP_cnd_z);
438 return E_OUTOFMEMORY;
440 hres = compile_expression(ctx, expr->true_expression);
444 jmp_end = push_instr(ctx, OP_jmp);
446 return E_OUTOFMEMORY;
448 instr_ptr(ctx, jmp_false)->arg1.uint = ctx->code_off;
449 if(push_instr(ctx, OP_pop) == -1)
450 return E_OUTOFMEMORY;
452 hres = compile_expression(ctx, expr->false_expression);
456 instr_ptr(ctx, jmp_end)->arg1.uint = ctx->code_off;
460 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
462 unsigned arg_cnt = 0;
466 hres = compile_expression(ctx, expr->expression);
470 for(arg = expr->argument_list; arg; arg = arg->next) {
471 hres = compile_expression(ctx, arg->expr);
477 return push_instr_int(ctx, OP_new, arg_cnt);
480 static HRESULT compile_interp_fallback(compiler_ctx_t *ctx, statement_t *stat)
484 instr = push_instr(ctx, OP_tree);
486 return E_OUTOFMEMORY;
488 instr_ptr(ctx, instr)->arg1.stat = stat;
492 static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL *no_ret)
494 unsigned arg_cnt = 0;
500 if(is_memberid_expr(expr->expression->type)) {
502 hres = compile_memberid_expression(ctx, expr->expression, 0);
505 hres = compile_expression(ctx, expr->expression);
511 for(arg = expr->argument_list; arg; arg = arg->next) {
512 hres = compile_expression(ctx, arg->expr);
518 instr = push_instr(ctx, op);
520 return E_OUTOFMEMORY;
522 instr_ptr(ctx, instr)->arg1.uint = arg_cnt;
523 instr_ptr(ctx, instr)->arg2.lng = no_ret == NULL;
529 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
533 switch(expr->expression->type) {
535 binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
537 hres = compile_expression(ctx, array_expr->expression1);
541 hres = compile_expression(ctx, array_expr->expression2);
545 if(push_instr(ctx, OP_delete) == -1)
546 return E_OUTOFMEMORY;
550 member_expression_t *member_expr = (member_expression_t*)expr->expression;
552 hres = compile_expression(ctx, member_expr->expression);
556 /* FIXME: Potential optimization */
557 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
561 if(push_instr(ctx, OP_delete) == -1)
562 return E_OUTOFMEMORY;
566 return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
568 const WCHAR fixmeW[] = {'F','I','X','M','E',0};
570 WARN("invalid delete, unimplemented exception message\n");
572 hres = compile_expression(ctx, expr->expression);
576 return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, fixmeW);
583 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
587 if(!is_memberid_expr(expr->expression1->type)) {
588 hres = compile_expression(ctx, expr->expression1);
592 hres = compile_expression(ctx, expr->expression2);
596 if(op != OP_LAST && push_instr(ctx, op) == -1)
597 return E_OUTOFMEMORY;
599 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
602 hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
606 if(op != OP_LAST && push_instr(ctx, OP_refval) == -1)
607 return E_OUTOFMEMORY;
609 hres = compile_expression(ctx, expr->expression2);
613 if(op != OP_LAST && push_instr(ctx, op) == -1)
614 return E_OUTOFMEMORY;
616 if(push_instr(ctx, OP_assign) == -1)
617 return E_OUTOFMEMORY;
622 static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
627 if(is_memberid_expr(expr->expression->type)) {
628 if(expr->expression->type == EXPR_IDENT)
629 return push_instr_str(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
632 hres = compile_memberid_expression(ctx, expr->expression, 0);
635 hres = compile_expression(ctx, expr->expression);
640 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
643 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
645 switch(literal->type) {
647 return push_instr_int(ctx, OP_bool, literal->u.bval);
649 return push_instr_double(ctx, OP_double, literal->u.dval);
651 return push_instr_int(ctx, OP_int, literal->u.lval);
653 return push_instr(ctx, OP_null);
655 return push_instr_str(ctx, OP_str, literal->u.wstr);
660 str = compiler_alloc(ctx->code, (literal->u.regexp.str_len+1)*sizeof(WCHAR));
662 return E_OUTOFMEMORY;
663 memcpy(str, literal->u.regexp.str, literal->u.regexp.str_len*sizeof(WCHAR));
664 str[literal->u.regexp.str_len] = 0;
666 instr = push_instr(ctx, OP_regexp);
668 return E_OUTOFMEMORY;
670 instr_ptr(ctx, instr)->arg1.str = str;
671 instr_ptr(ctx, instr)->arg2.lng = literal->u.regexp.flags;
679 static HRESULT literal_as_bstr(compiler_ctx_t *ctx, literal_t *literal, BSTR *str)
681 switch(literal->type) {
683 *str = compiler_alloc_bstr(ctx, literal->u.wstr);
686 *str = int_to_bstr(literal->u.lval);
689 return double_to_bstr(literal->u.dval, str);
694 return *str ? S_OK : E_OUTOFMEMORY;
697 static HRESULT compile_array_literal(compiler_ctx_t *ctx, array_literal_expression_t *expr)
699 unsigned i, elem_cnt = expr->length;
700 array_element_t *iter;
703 for(iter = expr->element_list; iter; iter = iter->next) {
704 elem_cnt += iter->elision+1;
706 for(i=0; i < iter->elision; i++) {
707 if(push_instr(ctx, OP_undefined) == -1)
708 return E_OUTOFMEMORY;
711 hres = compile_expression(ctx, iter->expr);
716 for(i=0; i < expr->length; i++) {
717 if(push_instr(ctx, OP_undefined) == -1)
718 return E_OUTOFMEMORY;
721 return push_instr_uint(ctx, OP_carray, elem_cnt);
724 static HRESULT compile_object_literal(compiler_ctx_t *ctx, property_value_expression_t *expr)
731 if(push_instr(ctx, OP_new_obj) == -1)
732 return E_OUTOFMEMORY;
734 for(iter = expr->property_list; iter; iter = iter->next) {
735 hres = literal_as_bstr(ctx, iter->name, &name);
739 hres = compile_expression(ctx, iter->value);
743 instr = push_instr(ctx, OP_obj_prop);
745 return E_OUTOFMEMORY;
747 instr_ptr(ctx, instr)->arg1.bstr = name;
753 static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr)
757 /* FIXME: not exactly right */
759 return push_instr_bstr(ctx, OP_ident, expr->identifier);
761 instr = push_instr(ctx, OP_func);
763 return E_OUTOFMEMORY;
765 instr_ptr(ctx, instr)->arg1.func = expr;
769 static HRESULT compile_expression_noret(compiler_ctx_t *ctx, expression_t *expr, BOOL *no_ret)
773 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
775 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_z);
777 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
779 return compile_array_literal(ctx, (array_literal_expression_t*)expr);
781 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
783 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
785 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_and);
787 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
789 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
791 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
793 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
795 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
796 case EXPR_ASSIGNLSHIFT:
797 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_lshift);
798 case EXPR_ASSIGNRSHIFT:
799 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift);
800 case EXPR_ASSIGNRRSHIFT:
801 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
803 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
805 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
807 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
809 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
811 return compile_call_expression(ctx, (call_expression_t*)expr, no_ret);
813 return compile_comma_expression(ctx, (binary_expression_t*)expr);
815 return compile_conditional_expression(ctx, (conditional_expression_t*)expr);
817 return compile_delete_expression(ctx, (unary_expression_t*)expr);
819 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
821 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
823 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
825 return compile_function_expression(ctx, (function_expression_t*)expr);
827 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
829 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
831 return push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
833 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
834 case EXPR_INSTANCEOF:
835 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
837 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
839 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
841 return compile_literal(ctx, ((literal_expression_t*)expr)->literal);
843 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
845 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
847 return compile_member_expression(ctx, (member_expression_t*)expr);
849 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
851 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
853 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
855 return compile_new_expression(ctx, (call_expression_t*)expr);
857 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
859 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
861 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_nz);
863 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
865 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
867 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
869 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
871 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
873 return compile_object_literal(ctx, (property_value_expression_t*)expr);
875 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
877 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
879 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
881 return push_instr(ctx, OP_this) == -1 ? E_OUTOFMEMORY : S_OK;
883 return compile_typeof_expression(ctx, (unary_expression_t*)expr);
885 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
887 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
895 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr)
897 return compile_expression_noret(ctx, expr, NULL);
900 /* ECMA-262 3rd Edition 12.1 */
901 static HRESULT compile_block_statement(compiler_ctx_t *ctx, statement_t *iter)
905 /* FIXME: do it only if needed */
907 return push_instr(ctx, OP_undefined) == -1 ? E_OUTOFMEMORY : S_OK;
910 hres = compile_statement(ctx, NULL, iter);
918 if(push_instr(ctx, OP_pop) == -1)
919 return E_OUTOFMEMORY;
925 /* ECMA-262 3rd Edition 12.2 */
926 static HRESULT compile_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
928 variable_declaration_t *iter;
931 for(iter = list; iter; iter = iter->next) {
935 hres = compile_expression(ctx, iter->expr);
939 hres = push_instr_bstr(ctx, OP_var_set, iter->identifier);
947 /* ECMA-262 3rd Edition 12.2 */
948 static HRESULT compile_var_statement(compiler_ctx_t *ctx, var_statement_t *stat)
952 hres = compile_variable_list(ctx, stat->variable_list);
956 return push_instr(ctx, OP_undefined) == -1 ? E_OUTOFMEMORY : S_OK;
959 /* ECMA-262 3rd Edition 12.4 */
960 static HRESULT compile_expression_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
965 hres = compile_expression_noret(ctx, stat->expr, &no_ret);
969 /* FIXME: that's a big potential optimization */
970 if(no_ret && !push_instr(ctx, OP_undefined) == -1)
971 return E_OUTOFMEMORY;
976 /* ECMA-262 3rd Edition 12.5 */
977 static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat)
979 unsigned jmp_else, jmp_end;
982 hres = compile_expression(ctx, stat->expr);
986 jmp_else = push_instr(ctx, OP_jmp_z);
988 return E_OUTOFMEMORY;
990 hres = compile_statement(ctx, NULL, stat->if_stat);
994 jmp_end = push_instr(ctx, OP_jmp);
996 return E_OUTOFMEMORY;
998 instr_ptr(ctx, jmp_else)->arg1.uint = ctx->code_off;
1000 if(stat->else_stat) {
1001 hres = compile_statement(ctx, NULL, stat->else_stat);
1005 /* FIXME: We could sometimes avoid it */
1006 if(push_instr(ctx, OP_undefined) == -1)
1007 return E_OUTOFMEMORY;
1010 instr_ptr(ctx, jmp_end)->arg1.uint = ctx->code_off;
1014 /* ECMA-262 3rd Edition 12.6.2 */
1015 static HRESULT compile_while_statement(compiler_ctx_t *ctx, while_statement_t *stat)
1017 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1018 unsigned off_backup, jmp_off;
1021 off_backup = ctx->code_off;
1023 stat_ctx.break_label = alloc_label(ctx);
1024 if(stat_ctx.break_label == -1)
1025 return E_OUTOFMEMORY;
1027 stat_ctx.continue_label = alloc_label(ctx);
1028 if(stat_ctx.continue_label == -1)
1029 return E_OUTOFMEMORY;
1031 if(!stat->do_while) {
1033 if(push_instr(ctx, OP_undefined) == -1)
1034 return E_OUTOFMEMORY;
1036 jmp_off = ctx->code_off;
1037 label_set_addr(ctx, stat_ctx.continue_label);
1038 hres = compile_expression(ctx, stat->expr);
1042 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1046 if(push_instr(ctx, OP_pop) == -1)
1047 return E_OUTOFMEMORY;
1049 jmp_off = ctx->code_off;
1052 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1053 if(hres == E_NOTIMPL) {
1054 ctx->code_off = off_backup;
1055 stat->stat.eval = while_statement_eval;
1056 return compile_interp_fallback(ctx, &stat->stat);
1061 if(stat->do_while) {
1062 label_set_addr(ctx, stat_ctx.continue_label);
1063 hres = compile_expression(ctx, stat->expr);
1067 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1071 if(push_instr(ctx, OP_pop) == -1)
1072 return E_OUTOFMEMORY;
1075 hres = push_instr_uint(ctx, OP_jmp, jmp_off);
1079 label_set_addr(ctx, stat_ctx.break_label);
1083 /* ECMA-262 3rd Edition 12.6.3 */
1084 static HRESULT compile_for_statement(compiler_ctx_t *ctx, for_statement_t *stat)
1086 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1090 if(stat->variable_list) {
1091 hres = compile_variable_list(ctx, stat->variable_list);
1094 }else if(stat->begin_expr) {
1095 BOOL no_ret = FALSE;
1097 hres = compile_expression_noret(ctx, stat->begin_expr, &no_ret);
1100 if(!no_ret && push_instr(ctx, OP_pop) == -1)
1101 return E_OUTOFMEMORY;
1104 stat_ctx.break_label = alloc_label(ctx);
1105 if(stat_ctx.break_label == -1)
1106 return E_OUTOFMEMORY;
1108 stat_ctx.continue_label = alloc_label(ctx);
1109 if(stat_ctx.continue_label == -1)
1110 return E_OUTOFMEMORY;
1113 if(push_instr(ctx, OP_undefined) == -1)
1114 return E_OUTOFMEMORY;
1116 expr_off = ctx->code_off;
1119 hres = compile_expression(ctx, stat->expr);
1123 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1128 if(push_instr(ctx, OP_pop) == -1)
1129 return E_OUTOFMEMORY;
1131 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1135 label_set_addr(ctx, stat_ctx.continue_label);
1137 if(stat->end_expr) {
1138 BOOL no_ret = FALSE;
1140 hres = compile_expression_noret(ctx, stat->end_expr, &no_ret);
1144 if(!no_ret && push_instr(ctx, OP_pop) == -1)
1145 return E_OUTOFMEMORY;
1148 hres = push_instr_uint(ctx, OP_jmp, expr_off);
1152 label_set_addr(ctx, stat_ctx.break_label);
1156 /* ECMA-262 3rd Edition 12.6.4 */
1157 static HRESULT compile_forin_statement(compiler_ctx_t *ctx, forin_statement_t *stat)
1159 statement_ctx_t stat_ctx = {4, FALSE, FALSE};
1162 if(stat->variable) {
1163 hres = compile_variable_list(ctx, stat->variable);
1168 stat_ctx.break_label = alloc_label(ctx);
1169 if(stat_ctx.break_label == -1)
1170 return E_OUTOFMEMORY;
1172 stat_ctx.continue_label = alloc_label(ctx);
1173 if(stat_ctx.continue_label == -1)
1174 return E_OUTOFMEMORY;
1176 hres = compile_expression(ctx, stat->in_expr);
1180 if(stat->variable) {
1181 hres = push_instr_bstr_uint(ctx, OP_identid, stat->variable->identifier, fdexNameEnsure);
1184 }else if(is_memberid_expr(stat->expr->type)) {
1185 hres = compile_memberid_expression(ctx, stat->expr, fdexNameEnsure);
1189 hres = push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
1193 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1197 hres = push_instr_int(ctx, OP_int, DISPID_STARTENUM);
1202 if(push_instr(ctx, OP_undefined) == -1)
1203 return E_OUTOFMEMORY;
1205 label_set_addr(ctx, stat_ctx.continue_label);
1206 hres = push_instr_uint(ctx, OP_forin, stat_ctx.break_label);
1208 return E_OUTOFMEMORY;
1210 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1214 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.continue_label);
1218 label_set_addr(ctx, stat_ctx.break_label);
1222 static HRESULT pop_to_stat(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx)
1224 statement_ctx_t *iter = ctx->stat_ctx;
1225 unsigned stack_pop = 0;
1228 if(iter->using_scope && push_instr(ctx, OP_pop_scope) == -1)
1229 return E_OUTOFMEMORY;
1230 if(iter->using_except && push_instr(ctx, OP_pop_except) == -1)
1231 return E_OUTOFMEMORY;
1232 stack_pop += iter->stack_use;
1233 if(iter == stat_ctx)
1238 /* FIXME: optimize */
1239 while(stack_pop--) {
1240 if(push_instr(ctx, OP_pop) == -1)
1241 return E_OUTOFMEMORY;
1247 /* ECMA-262 3rd Edition 12.7 */
1248 static HRESULT compile_continue_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1250 statement_ctx_t *pop_ctx;
1253 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1254 if(pop_ctx->continue_label != -1)
1258 if(!pop_ctx || stat->identifier) {
1259 stat->stat.eval = continue_statement_eval;
1260 return compile_interp_fallback(ctx, &stat->stat);
1263 hres = pop_to_stat(ctx, pop_ctx);
1267 if(push_instr(ctx, OP_undefined) == -1)
1268 return E_OUTOFMEMORY;
1270 return push_instr_uint(ctx, OP_jmp, pop_ctx->continue_label);
1273 /* ECMA-262 3rd Edition 12.8 */
1274 static HRESULT compile_break_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1276 statement_ctx_t *pop_ctx;
1279 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1280 if(pop_ctx->break_label != -1)
1284 if(!pop_ctx || stat->identifier) {
1285 stat->stat.eval = break_statement_eval;
1286 return compile_interp_fallback(ctx, &stat->stat);
1289 hres = pop_to_stat(ctx, pop_ctx);
1293 if(push_instr(ctx, OP_undefined) == -1)
1294 return E_OUTOFMEMORY;
1296 return push_instr_uint(ctx, OP_jmp, pop_ctx->break_label);
1299 /* ECMA-262 3rd Edition 12.10 */
1300 static HRESULT compile_with_statement(compiler_ctx_t *ctx, with_statement_t *stat)
1302 statement_ctx_t stat_ctx = {0, TRUE, FALSE, -1, -1};
1305 hres = compile_expression(ctx, stat->expr);
1309 if(push_instr(ctx, OP_push_scope) == -1)
1310 return E_OUTOFMEMORY;
1312 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1316 if(push_instr(ctx, OP_pop_scope) == -1)
1317 return E_OUTOFMEMORY;
1322 /* ECMA-262 3rd Edition 12.13 */
1323 static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat)
1325 statement_ctx_t stat_ctx = {0, FALSE, FALSE, -1, -1};
1326 unsigned case_cnt = 0, *case_jmps, i, default_jmp;
1327 BOOL have_default = FALSE;
1328 statement_t *stat_iter;
1329 case_clausule_t *iter;
1330 unsigned off_backup;
1333 off_backup = ctx->code_off;
1335 hres = compile_expression(ctx, stat->expr);
1339 stat_ctx.break_label = alloc_label(ctx);
1340 if(stat_ctx.break_label == -1)
1341 return E_OUTOFMEMORY;
1343 for(iter = stat->case_list; iter; iter = iter->next) {
1348 case_jmps = heap_alloc(case_cnt * sizeof(*case_jmps));
1350 return E_OUTOFMEMORY;
1353 for(iter = stat->case_list; iter; iter = iter->next) {
1355 have_default = TRUE;
1359 hres = compile_expression(ctx, iter->expr);
1363 case_jmps[i] = push_instr(ctx, OP_case);
1364 if(case_jmps[i] == -1) {
1365 hres = E_OUTOFMEMORY;
1371 if(SUCCEEDED(hres)) {
1372 if(push_instr(ctx, OP_pop) != -1) {
1373 default_jmp = push_instr(ctx, OP_jmp);
1374 if(default_jmp == -1)
1375 hres = E_OUTOFMEMORY;
1377 hres = E_OUTOFMEMORY;
1382 heap_free(case_jmps);
1387 for(iter = stat->case_list; iter; iter = iter->next) {
1388 while(iter->next && iter->next->stat == iter->stat) {
1389 instr_ptr(ctx, iter->expr ? case_jmps[i++] : default_jmp)->arg1.uint = ctx->code_off;
1393 instr_ptr(ctx, iter->expr ? case_jmps[i++] : default_jmp)->arg1.uint = ctx->code_off;
1395 for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter); stat_iter = stat_iter->next) {
1396 hres = compile_statement(ctx, &stat_ctx, stat_iter);
1397 if(hres == E_NOTIMPL) {
1398 ctx->code_off = off_backup;
1399 stat->stat.eval = switch_statement_eval;
1400 return compile_interp_fallback(ctx, &stat->stat);
1405 if(stat_iter->next && push_instr(ctx, OP_pop) == -1) {
1406 hres = E_OUTOFMEMORY;
1414 heap_free(case_jmps);
1417 assert(i == case_cnt);
1420 instr_ptr(ctx, default_jmp)->arg1.uint = ctx->code_off;
1422 label_set_addr(ctx, stat_ctx.break_label);
1426 /* ECMA-262 3rd Edition 12.13 */
1427 static HRESULT compile_throw_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1431 hres = compile_expression(ctx, stat->expr);
1435 return push_instr(ctx, OP_throw) == -1 ? E_OUTOFMEMORY : S_OK;
1438 /* ECMA-262 3rd Edition 12.14 */
1439 static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat)
1441 statement_ctx_t try_ctx = {0, FALSE, TRUE, -1, -1}, catch_ctx = {0, TRUE, FALSE, -1, -1};
1442 statement_ctx_t finally_ctx = {2, FALSE, FALSE, -1, -1};
1443 unsigned off_backup, push_except;
1447 off_backup = ctx->code_off;
1449 push_except = push_instr(ctx, OP_push_except);
1450 if(push_except == -1)
1451 return E_OUTOFMEMORY;
1453 if(stat->catch_block) {
1454 ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier);
1456 return E_OUTOFMEMORY;
1461 instr_ptr(ctx, push_except)->arg2.bstr = ident;
1463 if(!stat->catch_block)
1464 try_ctx.stack_use = 2;
1466 hres = compile_statement(ctx, &try_ctx, stat->try_statement);
1467 if(hres == E_NOTIMPL) {
1468 ctx->code_off = off_backup;
1469 stat->stat.eval = try_statement_eval;
1470 return compile_interp_fallback(ctx, &stat->stat);
1475 if(push_instr(ctx, OP_pop_except) == -1)
1476 return E_OUTOFMEMORY;
1478 if(stat->catch_block) {
1479 unsigned jmp_finally;
1481 jmp_finally = push_instr(ctx, OP_jmp);
1482 if(jmp_finally == -1)
1483 return E_OUTOFMEMORY;
1485 instr_ptr(ctx, push_except)->arg1.uint = ctx->code_off;
1487 hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement);
1488 if(hres == E_NOTIMPL) {
1489 ctx->code_off = off_backup;
1490 stat->stat.eval = try_statement_eval;
1491 return compile_interp_fallback(ctx, &stat->stat);
1496 if(push_instr(ctx, OP_pop_scope) == -1)
1497 return E_OUTOFMEMORY;
1499 instr_ptr(ctx, jmp_finally)->arg1.uint = ctx->code_off;
1501 instr_ptr(ctx, push_except)->arg1.uint = ctx->code_off;
1504 if(stat->finally_statement) {
1506 if(push_instr(ctx, OP_pop) == -1)
1507 return E_OUTOFMEMORY;
1509 hres = compile_statement(ctx, stat->catch_block ? NULL : &finally_ctx, stat->finally_statement);
1510 if(hres == E_NOTIMPL) {
1511 ctx->code_off = off_backup;
1512 stat->stat.eval = try_statement_eval;
1513 return compile_interp_fallback(ctx, &stat->stat);
1518 if(!stat->catch_block && push_instr(ctx, OP_end_finally) == -1)
1519 return E_OUTOFMEMORY;
1525 static HRESULT compile_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1530 stat_ctx->next = ctx->stat_ctx;
1531 ctx->stat_ctx = stat_ctx;
1534 switch(stat->type) {
1536 hres = compile_block_statement(ctx, ((block_statement_t*)stat)->stat_list);
1539 hres = compile_break_statement(ctx, (branch_statement_t*)stat);
1542 hres = compile_continue_statement(ctx, (branch_statement_t*)stat);
1545 hres = push_instr(ctx, OP_undefined) == -1 ? E_OUTOFMEMORY : S_OK; /* FIXME */
1548 hres = compile_expression_statement(ctx, (expression_statement_t*)stat);
1551 hres = compile_for_statement(ctx, (for_statement_t*)stat);
1554 hres = compile_forin_statement(ctx, (forin_statement_t*)stat);
1557 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1560 hres = push_instr(ctx, OP_label) == -1 ? E_OUTOFMEMORY : S_OK; /* FIXME */
1563 hres = compile_switch_statement(ctx, (switch_statement_t*)stat);
1566 hres = compile_throw_statement(ctx, (expression_statement_t*)stat);
1569 hres = compile_try_statement(ctx, (try_statement_t*)stat);
1572 hres = compile_var_statement(ctx, (var_statement_t*)stat);
1575 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1578 hres = compile_with_statement(ctx, (with_statement_t*)stat);
1581 hres = compile_interp_fallback(ctx, stat);
1585 assert(ctx->stat_ctx == stat_ctx);
1586 ctx->stat_ctx = stat_ctx->next;
1592 static void resolve_labels(compiler_ctx_t *ctx, unsigned off)
1596 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
1597 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
1598 assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
1599 instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
1601 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
1604 ctx->labels_cnt = 0;
1607 void release_bytecode(bytecode_t *code)
1611 for(i=0; i < code->bstr_cnt; i++)
1612 SysFreeString(code->bstr_pool[i]);
1614 jsheap_free(&code->heap);
1615 heap_free(code->bstr_pool);
1616 heap_free(code->instrs);
1620 void release_compiler(compiler_ctx_t *ctx)
1625 static HRESULT init_compiler(parser_ctx_t *parser)
1628 parser->code = heap_alloc_zero(sizeof(bytecode_t));
1630 return E_OUTOFMEMORY;
1631 jsheap_init(&parser->code->heap);
1634 if(!parser->compiler) {
1635 parser->compiler = heap_alloc_zero(sizeof(compiler_ctx_t));
1636 if(!parser->compiler)
1637 return E_OUTOFMEMORY;
1639 parser->compiler->parser = parser;
1640 parser->compiler->code = parser->code;
1646 HRESULT compile_subscript(parser_ctx_t *parser, expression_t *expr, unsigned *ret_off)
1650 hres = init_compiler(parser);
1654 *ret_off = parser->compiler->code_off;
1655 hres = compile_expression(parser->compiler, expr);
1659 return push_instr(parser->compiler, OP_ret) == -1 ? E_OUTOFMEMORY : S_OK;
1662 HRESULT compile_subscript_stat(parser_ctx_t *parser, statement_t *stat, BOOL compile_block, unsigned *ret_off)
1668 hres = init_compiler(parser);
1672 *ret_off = parser->compiler->code_off;
1673 if(compile_block && stat->next)
1674 hres = compile_block_statement(parser->compiler, stat);
1676 hres = compile_statement(parser->compiler, NULL, stat);
1680 resolve_labels(parser->compiler, *ret_off);
1682 return push_instr(parser->compiler, OP_ret) == -1 ? E_OUTOFMEMORY : S_OK;