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 struct _statement_ctx_t *next;
41 struct _compiler_ctx_t {
52 statement_ctx_t *stat_ctx;
57 instr_arg_type_t arg1_type;
58 instr_arg_type_t arg2_type;
60 #define X(n,a,b,c) {#n,b,c},
65 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
69 TRACE_(jscript_disas)("\t%s", debugstr_w(arg->str));
72 TRACE_(jscript_disas)("\t%s", debugstr_wn(arg->bstr, SysStringLen(arg->bstr)));
75 TRACE_(jscript_disas)("\t%d", arg->uint);
79 TRACE_(jscript_disas)("\t%u", arg->uint);
82 TRACE_(jscript_disas)("\t%lf", *arg->dbl);
92 static void dump_code(compiler_ctx_t *ctx, unsigned off)
96 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
97 TRACE_(jscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
98 dump_instr_arg(instr_info[instr->op].arg1_type, &instr->arg1);
99 dump_instr_arg(instr_info[instr->op].arg2_type, &instr->arg2);
100 TRACE_(jscript_disas)("\n");
104 static HRESULT compile_expression(compiler_ctx_t*,expression_t*);
105 static HRESULT compile_statement(compiler_ctx_t*,statement_ctx_t*,statement_t*);
107 static inline void *compiler_alloc(bytecode_t *code, size_t size)
109 return jsheap_alloc(&code->heap, size);
112 static WCHAR *compiler_alloc_string(bytecode_t *code, const WCHAR *str)
117 size = (strlenW(str)+1)*sizeof(WCHAR);
118 ret = compiler_alloc(code, size);
120 memcpy(ret, str, size);
124 static BSTR compiler_alloc_bstr(compiler_ctx_t *ctx, const WCHAR *str)
126 if(!ctx->code->bstr_pool_size) {
127 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
128 if(!ctx->code->bstr_pool)
130 ctx->code->bstr_pool_size = 8;
131 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
134 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
138 ctx->code->bstr_pool = new_pool;
139 ctx->code->bstr_pool_size *= 2;
142 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
143 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
146 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
149 static unsigned push_instr(compiler_ctx_t *ctx, jsop_t op)
151 assert(ctx->code_size >= ctx->code_off);
153 if(ctx->code_size == ctx->code_off) {
156 new_instrs = heap_realloc(ctx->code->instrs, ctx->code_size*2*sizeof(instr_t));
160 ctx->code->instrs = new_instrs;
164 ctx->code->instrs[ctx->code_off].op = op;
165 return ctx->code_off++;
168 static inline instr_t *instr_ptr(compiler_ctx_t *ctx, unsigned off)
170 assert(off < ctx->code_off);
171 return ctx->code->instrs + off;
174 static HRESULT push_instr_int(compiler_ctx_t *ctx, jsop_t op, LONG arg)
178 instr = push_instr(ctx, op);
180 return E_OUTOFMEMORY;
182 instr_ptr(ctx, instr)->arg1.lng = arg;
186 static HRESULT push_instr_str(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
191 str = compiler_alloc_string(ctx->code, arg);
193 return E_OUTOFMEMORY;
195 instr = push_instr(ctx, op);
197 return E_OUTOFMEMORY;
199 instr_ptr(ctx, instr)->arg1.str = str;
203 static HRESULT push_instr_bstr(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
208 str = compiler_alloc_bstr(ctx, arg);
210 return E_OUTOFMEMORY;
212 instr = push_instr(ctx, op);
214 return E_OUTOFMEMORY;
216 instr_ptr(ctx, instr)->arg1.bstr = str;
220 static HRESULT push_instr_bstr_uint(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg1, unsigned arg2)
225 str = compiler_alloc_bstr(ctx, arg1);
227 return E_OUTOFMEMORY;
229 instr = push_instr(ctx, op);
231 return E_OUTOFMEMORY;
233 instr_ptr(ctx, instr)->arg1.bstr = str;
234 instr_ptr(ctx, instr)->arg2.uint = arg2;
238 static HRESULT push_instr_uint_str(compiler_ctx_t *ctx, jsop_t op, unsigned arg1, const WCHAR *arg2)
243 str = compiler_alloc_string(ctx->code, arg2);
245 return E_OUTOFMEMORY;
247 instr = push_instr(ctx, op);
249 return E_OUTOFMEMORY;
251 instr_ptr(ctx, instr)->arg1.uint = arg1;
252 instr_ptr(ctx, instr)->arg2.str = str;
256 static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
261 dbl = compiler_alloc(ctx->code, sizeof(arg));
263 return E_OUTOFMEMORY;
266 instr = push_instr(ctx, op);
268 return E_OUTOFMEMORY;
270 instr_ptr(ctx, instr)->arg1.dbl = dbl;
274 static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
278 instr = push_instr(ctx, op);
280 return E_OUTOFMEMORY;
282 instr_ptr(ctx, instr)->arg1.uint = arg;
286 static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
290 hres = compile_expression(ctx, expr->expression1);
294 hres = compile_expression(ctx, expr->expression2);
298 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
301 static HRESULT compile_unary_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op)
305 hres = compile_expression(ctx, expr->expression);
309 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
312 /* ECMA-262 3rd Edition 11.2.1 */
313 static HRESULT compile_member_expression(compiler_ctx_t *ctx, member_expression_t *expr)
317 hres = compile_expression(ctx, expr->expression);
321 return push_instr_bstr(ctx, OP_member, expr->identifier);
324 #define LABEL_FLAG 0x80000000
326 static unsigned alloc_label(compiler_ctx_t *ctx)
328 if(!ctx->labels_size) {
329 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
332 ctx->labels_size = 8;
333 }else if(ctx->labels_size == ctx->labels_cnt) {
334 unsigned *new_labels;
336 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
340 ctx->labels = new_labels;
341 ctx->labels_size *= 2;
344 return ctx->labels_cnt++ | LABEL_FLAG;
347 static void label_set_addr(compiler_ctx_t *ctx, unsigned label)
349 assert(label & LABEL_FLAG);
350 ctx->labels[label & ~LABEL_FLAG] = ctx->code_off;
353 static inline BOOL is_memberid_expr(expression_type_t type)
355 return type == EXPR_IDENT || type == EXPR_MEMBER || type == EXPR_ARRAY;
358 static HRESULT compile_memberid_expression(compiler_ctx_t *ctx, expression_t *expr, unsigned flags)
364 identifier_expression_t *ident_expr = (identifier_expression_t*)expr;
366 hres = push_instr_bstr_uint(ctx, OP_identid, ident_expr->identifier, flags);
370 binary_expression_t *array_expr = (binary_expression_t*)expr;
372 hres = compile_expression(ctx, array_expr->expression1);
376 hres = compile_expression(ctx, array_expr->expression2);
380 hres = push_instr_uint(ctx, OP_memberid, flags);
384 member_expression_t *member_expr = (member_expression_t*)expr;
386 hres = compile_expression(ctx, member_expr->expression);
390 /* FIXME: Potential optimization */
391 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
395 hres = push_instr_uint(ctx, OP_memberid, flags);
405 static HRESULT compile_increment_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op, int n)
409 if(!is_memberid_expr(expr->expression->type)) {
410 hres = compile_expression(ctx, expr->expression);
414 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
417 hres = compile_memberid_expression(ctx, expr->expression, fdexNameEnsure);
421 return push_instr_int(ctx, op, n);
424 /* ECMA-262 3rd Edition 11.14 */
425 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr)
429 hres = compile_expression(ctx, expr->expression1);
433 if(push_instr(ctx, OP_pop) == -1)
434 return E_OUTOFMEMORY;
436 return compile_expression(ctx, expr->expression2);
439 /* ECMA-262 3rd Edition 11.11 */
440 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
445 hres = compile_expression(ctx, expr->expression1);
449 instr = push_instr(ctx, op);
451 return E_OUTOFMEMORY;
453 hres = compile_expression(ctx, expr->expression2);
457 instr_ptr(ctx, instr)->arg1.uint = ctx->code_off;
461 /* ECMA-262 3rd Edition 11.12 */
462 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
464 unsigned jmp_false, jmp_end;
467 hres = compile_expression(ctx, expr->expression);
471 jmp_false = push_instr(ctx, OP_cnd_z);
473 return E_OUTOFMEMORY;
475 hres = compile_expression(ctx, expr->true_expression);
479 jmp_end = push_instr(ctx, OP_jmp);
481 return E_OUTOFMEMORY;
483 instr_ptr(ctx, jmp_false)->arg1.uint = ctx->code_off;
484 if(push_instr(ctx, OP_pop) == -1)
485 return E_OUTOFMEMORY;
487 hres = compile_expression(ctx, expr->false_expression);
491 instr_ptr(ctx, jmp_end)->arg1.uint = ctx->code_off;
495 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
497 unsigned arg_cnt = 0;
501 hres = compile_expression(ctx, expr->expression);
505 for(arg = expr->argument_list; arg; arg = arg->next) {
506 hres = compile_expression(ctx, arg->expr);
512 return push_instr_int(ctx, OP_new, arg_cnt);
515 static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL *no_ret)
517 unsigned arg_cnt = 0;
523 if(is_memberid_expr(expr->expression->type)) {
525 hres = compile_memberid_expression(ctx, expr->expression, 0);
528 hres = compile_expression(ctx, expr->expression);
534 for(arg = expr->argument_list; arg; arg = arg->next) {
535 hres = compile_expression(ctx, arg->expr);
541 instr = push_instr(ctx, op);
543 return E_OUTOFMEMORY;
545 instr_ptr(ctx, instr)->arg1.uint = arg_cnt;
546 instr_ptr(ctx, instr)->arg2.lng = no_ret == NULL;
552 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
556 switch(expr->expression->type) {
558 binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
560 hres = compile_expression(ctx, array_expr->expression1);
564 hres = compile_expression(ctx, array_expr->expression2);
568 if(push_instr(ctx, OP_delete) == -1)
569 return E_OUTOFMEMORY;
573 member_expression_t *member_expr = (member_expression_t*)expr->expression;
575 hres = compile_expression(ctx, member_expr->expression);
579 /* FIXME: Potential optimization */
580 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
584 if(push_instr(ctx, OP_delete) == -1)
585 return E_OUTOFMEMORY;
589 return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
591 const WCHAR fixmeW[] = {'F','I','X','M','E',0};
593 WARN("invalid delete, unimplemented exception message\n");
595 hres = compile_expression(ctx, expr->expression);
599 return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, fixmeW);
606 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
610 if(!is_memberid_expr(expr->expression1->type)) {
611 hres = compile_expression(ctx, expr->expression1);
615 hres = compile_expression(ctx, expr->expression2);
619 if(op != OP_LAST && push_instr(ctx, op) == -1)
620 return E_OUTOFMEMORY;
622 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
625 hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
629 if(op != OP_LAST && push_instr(ctx, OP_refval) == -1)
630 return E_OUTOFMEMORY;
632 hres = compile_expression(ctx, expr->expression2);
636 if(op != OP_LAST && push_instr(ctx, op) == -1)
637 return E_OUTOFMEMORY;
639 if(push_instr(ctx, OP_assign) == -1)
640 return E_OUTOFMEMORY;
645 static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
650 if(is_memberid_expr(expr->expression->type)) {
651 if(expr->expression->type == EXPR_IDENT)
652 return push_instr_str(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
655 hres = compile_memberid_expression(ctx, expr->expression, 0);
658 hres = compile_expression(ctx, expr->expression);
663 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
666 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
668 switch(literal->type) {
670 return push_instr_int(ctx, OP_bool, literal->u.bval);
672 return push_instr_double(ctx, OP_double, literal->u.dval);
674 return push_instr_int(ctx, OP_int, literal->u.lval);
676 return push_instr(ctx, OP_null);
678 return push_instr_str(ctx, OP_str, literal->u.wstr);
683 str = compiler_alloc(ctx->code, (literal->u.regexp.str_len+1)*sizeof(WCHAR));
685 return E_OUTOFMEMORY;
686 memcpy(str, literal->u.regexp.str, literal->u.regexp.str_len*sizeof(WCHAR));
687 str[literal->u.regexp.str_len] = 0;
689 instr = push_instr(ctx, OP_regexp);
691 return E_OUTOFMEMORY;
693 instr_ptr(ctx, instr)->arg1.str = str;
694 instr_ptr(ctx, instr)->arg2.lng = literal->u.regexp.flags;
702 static HRESULT literal_as_bstr(compiler_ctx_t *ctx, literal_t *literal, BSTR *str)
704 switch(literal->type) {
706 *str = compiler_alloc_bstr(ctx, literal->u.wstr);
709 *str = int_to_bstr(literal->u.lval);
712 return double_to_bstr(literal->u.dval, str);
717 return *str ? S_OK : E_OUTOFMEMORY;
720 static HRESULT compile_array_literal(compiler_ctx_t *ctx, array_literal_expression_t *expr)
722 unsigned i, elem_cnt = expr->length;
723 array_element_t *iter;
726 for(iter = expr->element_list; iter; iter = iter->next) {
727 elem_cnt += iter->elision+1;
729 for(i=0; i < iter->elision; i++) {
730 if(push_instr(ctx, OP_undefined) == -1)
731 return E_OUTOFMEMORY;
734 hres = compile_expression(ctx, iter->expr);
739 for(i=0; i < expr->length; i++) {
740 if(push_instr(ctx, OP_undefined) == -1)
741 return E_OUTOFMEMORY;
744 return push_instr_uint(ctx, OP_carray, elem_cnt);
747 static HRESULT compile_object_literal(compiler_ctx_t *ctx, property_value_expression_t *expr)
754 if(push_instr(ctx, OP_new_obj) == -1)
755 return E_OUTOFMEMORY;
757 for(iter = expr->property_list; iter; iter = iter->next) {
758 hres = literal_as_bstr(ctx, iter->name, &name);
762 hres = compile_expression(ctx, iter->value);
766 instr = push_instr(ctx, OP_obj_prop);
768 return E_OUTOFMEMORY;
770 instr_ptr(ctx, instr)->arg1.bstr = name;
776 static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr)
780 /* FIXME: not exactly right */
782 return push_instr_bstr(ctx, OP_ident, expr->identifier);
784 instr = push_instr(ctx, OP_func);
786 return E_OUTOFMEMORY;
788 instr_ptr(ctx, instr)->arg1.func = expr;
792 static HRESULT compile_expression_noret(compiler_ctx_t *ctx, expression_t *expr, BOOL *no_ret)
796 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
798 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_z);
800 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
802 return compile_array_literal(ctx, (array_literal_expression_t*)expr);
804 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
806 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
808 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_and);
810 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
812 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
814 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
816 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
818 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
819 case EXPR_ASSIGNLSHIFT:
820 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_lshift);
821 case EXPR_ASSIGNRSHIFT:
822 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift);
823 case EXPR_ASSIGNRRSHIFT:
824 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
826 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
828 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
830 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
832 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
834 return compile_call_expression(ctx, (call_expression_t*)expr, no_ret);
836 return compile_comma_expression(ctx, (binary_expression_t*)expr);
838 return compile_conditional_expression(ctx, (conditional_expression_t*)expr);
840 return compile_delete_expression(ctx, (unary_expression_t*)expr);
842 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
844 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
846 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
848 return compile_function_expression(ctx, (function_expression_t*)expr);
850 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
852 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
854 return push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
856 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
857 case EXPR_INSTANCEOF:
858 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
860 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
862 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
864 return compile_literal(ctx, ((literal_expression_t*)expr)->literal);
866 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
868 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
870 return compile_member_expression(ctx, (member_expression_t*)expr);
872 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
874 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
876 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
878 return compile_new_expression(ctx, (call_expression_t*)expr);
880 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
882 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
884 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_nz);
886 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
888 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
890 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
892 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
894 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
896 return compile_object_literal(ctx, (property_value_expression_t*)expr);
898 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
900 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
902 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
904 return push_instr(ctx, OP_this) == -1 ? E_OUTOFMEMORY : S_OK;
906 return compile_typeof_expression(ctx, (unary_expression_t*)expr);
908 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
910 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
918 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr)
920 return compile_expression_noret(ctx, expr, NULL);
923 /* ECMA-262 3rd Edition 12.1 */
924 static HRESULT compile_block_statement(compiler_ctx_t *ctx, statement_t *iter)
928 /* FIXME: do it only if needed */
930 return push_instr(ctx, OP_undefined) == -1 ? E_OUTOFMEMORY : S_OK;
933 hres = compile_statement(ctx, NULL, iter);
941 if(push_instr(ctx, OP_pop) == -1)
942 return E_OUTOFMEMORY;
948 /* ECMA-262 3rd Edition 12.2 */
949 static HRESULT compile_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
951 variable_declaration_t *iter;
954 for(iter = list; iter; iter = iter->next) {
958 hres = compile_expression(ctx, iter->expr);
962 hres = push_instr_bstr(ctx, OP_var_set, iter->identifier);
970 /* ECMA-262 3rd Edition 12.2 */
971 static HRESULT compile_var_statement(compiler_ctx_t *ctx, var_statement_t *stat)
975 hres = compile_variable_list(ctx, stat->variable_list);
979 return push_instr(ctx, OP_undefined) == -1 ? E_OUTOFMEMORY : S_OK;
982 /* ECMA-262 3rd Edition 12.4 */
983 static HRESULT compile_expression_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
988 hres = compile_expression_noret(ctx, stat->expr, &no_ret);
992 /* FIXME: that's a big potential optimization */
993 if(no_ret && !push_instr(ctx, OP_undefined) == -1)
994 return E_OUTOFMEMORY;
999 /* ECMA-262 3rd Edition 12.5 */
1000 static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat)
1002 unsigned jmp_else, jmp_end;
1005 hres = compile_expression(ctx, stat->expr);
1009 jmp_else = push_instr(ctx, OP_jmp_z);
1011 return E_OUTOFMEMORY;
1013 hres = compile_statement(ctx, NULL, stat->if_stat);
1017 jmp_end = push_instr(ctx, OP_jmp);
1019 return E_OUTOFMEMORY;
1021 instr_ptr(ctx, jmp_else)->arg1.uint = ctx->code_off;
1023 if(stat->else_stat) {
1024 hres = compile_statement(ctx, NULL, stat->else_stat);
1028 /* FIXME: We could sometimes avoid it */
1029 if(push_instr(ctx, OP_undefined) == -1)
1030 return E_OUTOFMEMORY;
1033 instr_ptr(ctx, jmp_end)->arg1.uint = ctx->code_off;
1037 /* ECMA-262 3rd Edition 12.6.2 */
1038 static HRESULT compile_while_statement(compiler_ctx_t *ctx, while_statement_t *stat)
1040 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1044 stat_ctx.break_label = alloc_label(ctx);
1045 if(stat_ctx.break_label == -1)
1046 return E_OUTOFMEMORY;
1048 stat_ctx.continue_label = alloc_label(ctx);
1049 if(stat_ctx.continue_label == -1)
1050 return E_OUTOFMEMORY;
1052 if(!stat->do_while) {
1054 if(push_instr(ctx, OP_undefined) == -1)
1055 return E_OUTOFMEMORY;
1057 jmp_off = ctx->code_off;
1058 label_set_addr(ctx, stat_ctx.continue_label);
1059 hres = compile_expression(ctx, stat->expr);
1063 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1067 if(push_instr(ctx, OP_pop) == -1)
1068 return E_OUTOFMEMORY;
1070 jmp_off = ctx->code_off;
1073 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1077 if(stat->do_while) {
1078 label_set_addr(ctx, stat_ctx.continue_label);
1079 hres = compile_expression(ctx, stat->expr);
1083 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1087 if(push_instr(ctx, OP_pop) == -1)
1088 return E_OUTOFMEMORY;
1091 hres = push_instr_uint(ctx, OP_jmp, jmp_off);
1095 label_set_addr(ctx, stat_ctx.break_label);
1099 /* ECMA-262 3rd Edition 12.6.3 */
1100 static HRESULT compile_for_statement(compiler_ctx_t *ctx, for_statement_t *stat)
1102 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1106 if(stat->variable_list) {
1107 hres = compile_variable_list(ctx, stat->variable_list);
1110 }else if(stat->begin_expr) {
1111 BOOL no_ret = FALSE;
1113 hres = compile_expression_noret(ctx, stat->begin_expr, &no_ret);
1116 if(!no_ret && push_instr(ctx, OP_pop) == -1)
1117 return E_OUTOFMEMORY;
1120 stat_ctx.break_label = alloc_label(ctx);
1121 if(stat_ctx.break_label == -1)
1122 return E_OUTOFMEMORY;
1124 stat_ctx.continue_label = alloc_label(ctx);
1125 if(stat_ctx.continue_label == -1)
1126 return E_OUTOFMEMORY;
1129 if(push_instr(ctx, OP_undefined) == -1)
1130 return E_OUTOFMEMORY;
1132 expr_off = ctx->code_off;
1135 hres = compile_expression(ctx, stat->expr);
1139 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1144 if(push_instr(ctx, OP_pop) == -1)
1145 return E_OUTOFMEMORY;
1147 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1151 label_set_addr(ctx, stat_ctx.continue_label);
1153 if(stat->end_expr) {
1154 BOOL no_ret = FALSE;
1156 hres = compile_expression_noret(ctx, stat->end_expr, &no_ret);
1160 if(!no_ret && push_instr(ctx, OP_pop) == -1)
1161 return E_OUTOFMEMORY;
1164 hres = push_instr_uint(ctx, OP_jmp, expr_off);
1168 label_set_addr(ctx, stat_ctx.break_label);
1172 /* ECMA-262 3rd Edition 12.6.4 */
1173 static HRESULT compile_forin_statement(compiler_ctx_t *ctx, forin_statement_t *stat)
1175 statement_ctx_t stat_ctx = {4, FALSE, FALSE};
1178 if(stat->variable) {
1179 hres = compile_variable_list(ctx, stat->variable);
1184 stat_ctx.break_label = alloc_label(ctx);
1185 if(stat_ctx.break_label == -1)
1186 return E_OUTOFMEMORY;
1188 stat_ctx.continue_label = alloc_label(ctx);
1189 if(stat_ctx.continue_label == -1)
1190 return E_OUTOFMEMORY;
1192 hres = compile_expression(ctx, stat->in_expr);
1196 if(stat->variable) {
1197 hres = push_instr_bstr_uint(ctx, OP_identid, stat->variable->identifier, fdexNameEnsure);
1200 }else if(is_memberid_expr(stat->expr->type)) {
1201 hres = compile_memberid_expression(ctx, stat->expr, fdexNameEnsure);
1205 hres = push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
1209 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1213 hres = push_instr_int(ctx, OP_int, DISPID_STARTENUM);
1218 if(push_instr(ctx, OP_undefined) == -1)
1219 return E_OUTOFMEMORY;
1221 label_set_addr(ctx, stat_ctx.continue_label);
1222 hres = push_instr_uint(ctx, OP_forin, stat_ctx.break_label);
1224 return E_OUTOFMEMORY;
1226 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1230 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.continue_label);
1234 label_set_addr(ctx, stat_ctx.break_label);
1238 static HRESULT pop_to_stat(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx)
1240 statement_ctx_t *iter = ctx->stat_ctx;
1241 unsigned stack_pop = 0;
1244 if(iter->using_scope && push_instr(ctx, OP_pop_scope) == -1)
1245 return E_OUTOFMEMORY;
1246 if(iter->using_except && push_instr(ctx, OP_pop_except) == -1)
1247 return E_OUTOFMEMORY;
1248 stack_pop += iter->stack_use;
1249 if(iter == stat_ctx)
1254 /* FIXME: optimize */
1255 while(stack_pop--) {
1256 if(push_instr(ctx, OP_pop) == -1)
1257 return E_OUTOFMEMORY;
1263 /* ECMA-262 3rd Edition 12.7 */
1264 static HRESULT compile_continue_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1266 statement_ctx_t *pop_ctx;
1269 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1270 if(pop_ctx->continue_label != -1)
1275 WARN("continue outside loop\n");
1276 return JS_E_INVALID_CONTINUE;
1279 if(stat->identifier)
1280 return push_instr(ctx, OP_label) == -1 ? E_OUTOFMEMORY : S_OK; /* FIXME */
1282 hres = pop_to_stat(ctx, pop_ctx);
1286 if(push_instr(ctx, OP_undefined) == -1)
1287 return E_OUTOFMEMORY;
1289 return push_instr_uint(ctx, OP_jmp, pop_ctx->continue_label);
1292 /* ECMA-262 3rd Edition 12.8 */
1293 static HRESULT compile_break_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1295 statement_ctx_t *pop_ctx;
1298 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1299 if(pop_ctx->break_label != -1)
1304 WARN("Break outside loop\n");
1305 return JS_E_INVALID_BREAK;
1308 if(stat->identifier)
1309 return push_instr(ctx, OP_label) == -1 ? E_OUTOFMEMORY : S_OK; /* FIXME */
1311 hres = pop_to_stat(ctx, pop_ctx);
1315 if(push_instr(ctx, OP_undefined) == -1)
1316 return E_OUTOFMEMORY;
1318 return push_instr_uint(ctx, OP_jmp, pop_ctx->break_label);
1321 /* ECMA-262 3rd Edition 12.9 */
1322 static HRESULT compile_return_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1326 hres = pop_to_stat(ctx, NULL);
1331 hres = compile_expression(ctx, stat->expr);
1336 return push_instr(ctx, OP_ret) == -1 ? E_OUTOFMEMORY : S_OK;
1339 /* ECMA-262 3rd Edition 12.10 */
1340 static HRESULT compile_with_statement(compiler_ctx_t *ctx, with_statement_t *stat)
1342 statement_ctx_t stat_ctx = {0, TRUE, FALSE, -1, -1};
1345 hres = compile_expression(ctx, stat->expr);
1349 if(push_instr(ctx, OP_push_scope) == -1)
1350 return E_OUTOFMEMORY;
1352 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1356 if(push_instr(ctx, OP_pop_scope) == -1)
1357 return E_OUTOFMEMORY;
1362 /* ECMA-262 3rd Edition 12.13 */
1363 static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat)
1365 statement_ctx_t stat_ctx = {0, FALSE, FALSE, -1, -1};
1366 unsigned case_cnt = 0, *case_jmps, i, default_jmp;
1367 BOOL have_default = FALSE;
1368 statement_t *stat_iter;
1369 case_clausule_t *iter;
1372 hres = compile_expression(ctx, stat->expr);
1376 stat_ctx.break_label = alloc_label(ctx);
1377 if(stat_ctx.break_label == -1)
1378 return E_OUTOFMEMORY;
1380 for(iter = stat->case_list; iter; iter = iter->next) {
1385 case_jmps = heap_alloc(case_cnt * sizeof(*case_jmps));
1387 return E_OUTOFMEMORY;
1390 for(iter = stat->case_list; iter; iter = iter->next) {
1392 have_default = TRUE;
1396 hres = compile_expression(ctx, iter->expr);
1400 case_jmps[i] = push_instr(ctx, OP_case);
1401 if(case_jmps[i] == -1) {
1402 hres = E_OUTOFMEMORY;
1408 if(SUCCEEDED(hres)) {
1409 if(push_instr(ctx, OP_pop) != -1) {
1410 default_jmp = push_instr(ctx, OP_jmp);
1411 if(default_jmp == -1)
1412 hres = E_OUTOFMEMORY;
1414 hres = E_OUTOFMEMORY;
1419 heap_free(case_jmps);
1424 for(iter = stat->case_list; iter; iter = iter->next) {
1425 while(iter->next && iter->next->stat == iter->stat) {
1426 instr_ptr(ctx, iter->expr ? case_jmps[i++] : default_jmp)->arg1.uint = ctx->code_off;
1430 instr_ptr(ctx, iter->expr ? case_jmps[i++] : default_jmp)->arg1.uint = ctx->code_off;
1432 for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter); stat_iter = stat_iter->next) {
1433 hres = compile_statement(ctx, &stat_ctx, stat_iter);
1437 if(stat_iter->next && push_instr(ctx, OP_pop) == -1) {
1438 hres = E_OUTOFMEMORY;
1446 heap_free(case_jmps);
1449 assert(i == case_cnt);
1452 instr_ptr(ctx, default_jmp)->arg1.uint = ctx->code_off;
1454 label_set_addr(ctx, stat_ctx.break_label);
1458 /* ECMA-262 3rd Edition 12.13 */
1459 static HRESULT compile_throw_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1463 hres = compile_expression(ctx, stat->expr);
1467 return push_instr(ctx, OP_throw) == -1 ? E_OUTOFMEMORY : S_OK;
1470 /* ECMA-262 3rd Edition 12.14 */
1471 static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat)
1473 statement_ctx_t try_ctx = {0, FALSE, TRUE, -1, -1}, catch_ctx = {0, TRUE, FALSE, -1, -1};
1474 statement_ctx_t finally_ctx = {2, FALSE, FALSE, -1, -1};
1475 unsigned push_except;
1479 push_except = push_instr(ctx, OP_push_except);
1480 if(push_except == -1)
1481 return E_OUTOFMEMORY;
1483 if(stat->catch_block) {
1484 ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier);
1486 return E_OUTOFMEMORY;
1491 instr_ptr(ctx, push_except)->arg2.bstr = ident;
1493 if(!stat->catch_block)
1494 try_ctx.stack_use = 2;
1496 hres = compile_statement(ctx, &try_ctx, stat->try_statement);
1500 if(push_instr(ctx, OP_pop_except) == -1)
1501 return E_OUTOFMEMORY;
1503 if(stat->catch_block) {
1504 unsigned jmp_finally;
1506 jmp_finally = push_instr(ctx, OP_jmp);
1507 if(jmp_finally == -1)
1508 return E_OUTOFMEMORY;
1510 instr_ptr(ctx, push_except)->arg1.uint = ctx->code_off;
1512 hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement);
1516 if(push_instr(ctx, OP_pop_scope) == -1)
1517 return E_OUTOFMEMORY;
1519 instr_ptr(ctx, jmp_finally)->arg1.uint = ctx->code_off;
1521 instr_ptr(ctx, push_except)->arg1.uint = ctx->code_off;
1524 if(stat->finally_statement) {
1526 if(push_instr(ctx, OP_pop) == -1)
1527 return E_OUTOFMEMORY;
1529 hres = compile_statement(ctx, stat->catch_block ? NULL : &finally_ctx, stat->finally_statement);
1533 if(!stat->catch_block && push_instr(ctx, OP_end_finally) == -1)
1534 return E_OUTOFMEMORY;
1540 static HRESULT compile_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1545 stat_ctx->next = ctx->stat_ctx;
1546 ctx->stat_ctx = stat_ctx;
1549 switch(stat->type) {
1551 hres = compile_block_statement(ctx, ((block_statement_t*)stat)->stat_list);
1554 hres = compile_break_statement(ctx, (branch_statement_t*)stat);
1557 hres = compile_continue_statement(ctx, (branch_statement_t*)stat);
1560 hres = push_instr(ctx, OP_undefined) == -1 ? E_OUTOFMEMORY : S_OK; /* FIXME */
1563 hres = compile_expression_statement(ctx, (expression_statement_t*)stat);
1566 hres = compile_for_statement(ctx, (for_statement_t*)stat);
1569 hres = compile_forin_statement(ctx, (forin_statement_t*)stat);
1572 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1575 hres = push_instr(ctx, OP_label) == -1 ? E_OUTOFMEMORY : S_OK; /* FIXME */
1578 hres = compile_return_statement(ctx, (expression_statement_t*)stat);
1581 hres = compile_switch_statement(ctx, (switch_statement_t*)stat);
1584 hres = compile_throw_statement(ctx, (expression_statement_t*)stat);
1587 hres = compile_try_statement(ctx, (try_statement_t*)stat);
1590 hres = compile_var_statement(ctx, (var_statement_t*)stat);
1593 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1596 hres = compile_with_statement(ctx, (with_statement_t*)stat);
1603 assert(ctx->stat_ctx == stat_ctx);
1604 ctx->stat_ctx = stat_ctx->next;
1610 static void resolve_labels(compiler_ctx_t *ctx, unsigned off)
1614 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
1615 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
1616 assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
1617 instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
1619 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
1622 ctx->labels_cnt = 0;
1625 void release_bytecode(bytecode_t *code)
1629 for(i=0; i < code->bstr_cnt; i++)
1630 SysFreeString(code->bstr_pool[i]);
1632 jsheap_free(&code->heap);
1633 heap_free(code->bstr_pool);
1634 heap_free(code->instrs);
1638 void release_compiler(compiler_ctx_t *ctx)
1643 static HRESULT init_compiler(parser_ctx_t *parser)
1645 compiler_ctx_t *compiler;
1647 if(parser->compiler)
1650 compiler = heap_alloc_zero(sizeof(*compiler));
1652 return E_OUTOFMEMORY;
1654 compiler->code = heap_alloc_zero(sizeof(bytecode_t));
1655 if(!compiler->code) {
1656 release_compiler(compiler);
1657 return E_OUTOFMEMORY;
1660 jsheap_init(&compiler->code->heap);
1662 compiler->code->instrs = heap_alloc(64 * sizeof(instr_t));
1663 if(!compiler->code->instrs) {
1664 release_bytecode(compiler->code);
1665 release_compiler(compiler);
1666 return E_OUTOFMEMORY;
1669 compiler->code_size = 64;
1671 compiler->parser = parser;
1673 parser->code = compiler->code;
1674 parser->compiler = compiler;
1678 HRESULT compile_subscript_stat(parser_ctx_t *parser, statement_t *stat, BOOL from_eval, unsigned *ret_off)
1685 hres = init_compiler(parser);
1689 off = parser->compiler->code_off;
1691 hres = compile_block_statement(parser->compiler, stat);
1693 hres = compile_statement(parser->compiler, NULL, stat);
1697 resolve_labels(parser->compiler, off);
1699 if(!from_eval && push_instr(parser->compiler, OP_pop) == -1)
1700 return E_OUTOFMEMORY;
1701 if(push_instr(parser->compiler, OP_ret) == -1)
1702 return E_OUTOFMEMORY;
1704 if(TRACE_ON(jscript_disas))
1705 dump_code(parser->compiler, off);