winegstreamer: Move seeking to the Unix library.
[wine.git] / dlls / jscript / compile.c
bloba1d1171e2ec50aea4d0aff1b1eddb0ae77026787
1 /*
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
19 #include <math.h>
20 #include <assert.h>
22 #include "jscript.h"
23 #include "engine.h"
24 #include "parser.h"
26 #include "wine/rbtree.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
30 WINE_DECLARE_DEBUG_CHANNEL(jscript_disas);
32 typedef struct _statement_ctx_t {
33 unsigned stack_use;
34 BOOL using_scope;
35 BOOL using_except;
37 unsigned break_label;
38 unsigned continue_label;
40 const labelled_statement_t *labelled_stat;
42 struct _statement_ctx_t *next;
43 } statement_ctx_t;
45 typedef struct {
46 struct wine_rb_entry entry;
47 BSTR name;
48 int ref;
49 } function_local_t;
51 typedef struct _compiler_ctx_t {
52 parser_ctx_t *parser;
53 bytecode_t *code;
55 BOOL from_eval;
57 unsigned code_off;
58 unsigned code_size;
60 unsigned *labels;
61 unsigned labels_size;
62 unsigned labels_cnt;
64 struct wine_rb_tree locals;
65 unsigned locals_cnt;
67 statement_ctx_t *stat_ctx;
68 function_code_t *func;
70 unsigned loc;
72 function_expression_t *func_head;
73 function_expression_t *func_tail;
75 heap_pool_t heap;
76 } compiler_ctx_t;
78 static const struct {
79 const char *op_str;
80 instr_arg_type_t arg1_type;
81 instr_arg_type_t arg2_type;
82 } instr_info[] = {
83 #define X(n,a,b,c) {#n,b,c},
84 OP_LIST
85 #undef X
88 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
90 switch(type) {
91 case ARG_STR:
92 TRACE_(jscript_disas)("\t%s", debugstr_jsstr(arg->str));
93 break;
94 case ARG_BSTR:
95 TRACE_(jscript_disas)("\t%s", debugstr_wn(arg->bstr, SysStringLen(arg->bstr)));
96 break;
97 case ARG_INT:
98 TRACE_(jscript_disas)("\t%d", arg->uint);
99 break;
100 case ARG_UINT:
101 case ARG_ADDR:
102 TRACE_(jscript_disas)("\t%u", arg->uint);
103 break;
104 case ARG_FUNC:
105 case ARG_NONE:
106 break;
107 DEFAULT_UNREACHABLE;
111 static void dump_code(compiler_ctx_t *ctx, unsigned off)
113 instr_t *instr;
115 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
116 TRACE_(jscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
117 if(instr_info[instr->op].arg1_type == ARG_DBL) {
118 TRACE_(jscript_disas)("\t%lf", instr->u.dbl);
119 }else {
120 dump_instr_arg(instr_info[instr->op].arg1_type, instr->u.arg);
121 dump_instr_arg(instr_info[instr->op].arg2_type, instr->u.arg+1);
123 TRACE_(jscript_disas)("\n");
127 static HRESULT compile_expression(compiler_ctx_t*,expression_t*,BOOL);
128 static HRESULT compile_statement(compiler_ctx_t*,statement_ctx_t*,statement_t*);
130 static inline void *compiler_alloc(bytecode_t *code, size_t size)
132 return heap_pool_alloc(&code->heap, size);
135 jsstr_t *compiler_alloc_string_len(compiler_ctx_t *ctx, const WCHAR *str, unsigned len)
137 jsstr_t *new_str;
139 if(!ctx->code->str_pool_size) {
140 ctx->code->str_pool = heap_alloc(8 * sizeof(jsstr_t*));
141 if(!ctx->code->str_pool)
142 return NULL;
143 ctx->code->str_pool_size = 8;
144 }else if(ctx->code->str_pool_size == ctx->code->str_cnt) {
145 jsstr_t **new_pool;
147 new_pool = heap_realloc(ctx->code->str_pool, ctx->code->str_pool_size*2*sizeof(jsstr_t*));
148 if(!new_pool)
149 return NULL;
151 ctx->code->str_pool = new_pool;
152 ctx->code->str_pool_size *= 2;
155 new_str = jsstr_alloc_len(str, len);
156 if(!new_str)
157 return NULL;
159 ctx->code->str_pool[ctx->code->str_cnt++] = new_str;
160 return new_str;
163 static jsstr_t *compiler_alloc_string(compiler_ctx_t *ctx, const WCHAR *str)
165 return compiler_alloc_string_len(ctx, str, lstrlenW(str));
168 static BOOL ensure_bstr_slot(compiler_ctx_t *ctx)
170 if(!ctx->code->bstr_pool_size) {
171 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
172 if(!ctx->code->bstr_pool)
173 return FALSE;
174 ctx->code->bstr_pool_size = 8;
175 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
176 BSTR *new_pool;
178 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
179 if(!new_pool)
180 return FALSE;
182 ctx->code->bstr_pool = new_pool;
183 ctx->code->bstr_pool_size *= 2;
186 return TRUE;
189 static BSTR compiler_alloc_bstr(compiler_ctx_t *ctx, const WCHAR *str)
191 if(!ensure_bstr_slot(ctx))
192 return NULL;
194 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
195 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
196 return NULL;
198 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
201 static BSTR compiler_alloc_bstr_len(compiler_ctx_t *ctx, const WCHAR *str, size_t len)
203 if(!ensure_bstr_slot(ctx))
204 return NULL;
206 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocStringLen(str, len);
207 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
208 return NULL;
210 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
213 void set_compiler_loc(compiler_ctx_t *ctx, unsigned loc)
215 ctx->loc = loc;
218 static unsigned push_instr(compiler_ctx_t *ctx, jsop_t op)
220 assert(ctx->code_size >= ctx->code_off);
222 if(ctx->code_size == ctx->code_off) {
223 instr_t *new_instrs;
225 new_instrs = heap_realloc(ctx->code->instrs, ctx->code_size*2*sizeof(instr_t));
226 if(!new_instrs)
227 return 0;
229 ctx->code->instrs = new_instrs;
230 ctx->code_size *= 2;
233 ctx->code->instrs[ctx->code_off].op = op;
234 ctx->code->instrs[ctx->code_off].loc = ctx->loc;
235 return ctx->code_off++;
238 static inline instr_t *instr_ptr(compiler_ctx_t *ctx, unsigned off)
240 assert(off < ctx->code_off);
241 return ctx->code->instrs + off;
244 static HRESULT push_instr_int(compiler_ctx_t *ctx, jsop_t op, LONG arg)
246 unsigned instr;
248 instr = push_instr(ctx, op);
249 if(!instr)
250 return E_OUTOFMEMORY;
252 instr_ptr(ctx, instr)->u.arg->lng = arg;
253 return S_OK;
256 static HRESULT push_instr_str(compiler_ctx_t *ctx, jsop_t op, jsstr_t *str)
258 unsigned instr;
260 instr = push_instr(ctx, op);
261 if(!instr)
262 return E_OUTOFMEMORY;
264 instr_ptr(ctx, instr)->u.arg->str = str;
265 return S_OK;
268 static HRESULT push_instr_str_uint(compiler_ctx_t *ctx, jsop_t op, jsstr_t *str, unsigned arg2)
270 unsigned instr;
272 instr = push_instr(ctx, op);
273 if(!instr)
274 return E_OUTOFMEMORY;
276 instr_ptr(ctx, instr)->u.arg[0].str = str;
277 instr_ptr(ctx, instr)->u.arg[1].uint = arg2;
278 return S_OK;
281 static HRESULT push_instr_bstr(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
283 unsigned instr;
284 WCHAR *str;
286 str = compiler_alloc_bstr(ctx, arg);
287 if(!str)
288 return E_OUTOFMEMORY;
290 instr = push_instr(ctx, op);
291 if(!instr)
292 return E_OUTOFMEMORY;
294 instr_ptr(ctx, instr)->u.arg->bstr = str;
295 return S_OK;
298 static HRESULT push_instr_bstr_uint(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg1, unsigned arg2)
300 unsigned instr;
301 WCHAR *str;
303 str = compiler_alloc_bstr(ctx, arg1);
304 if(!str)
305 return E_OUTOFMEMORY;
307 instr = push_instr(ctx, op);
308 if(!instr)
309 return E_OUTOFMEMORY;
311 instr_ptr(ctx, instr)->u.arg[0].bstr = str;
312 instr_ptr(ctx, instr)->u.arg[1].uint = arg2;
313 return S_OK;
316 static HRESULT push_instr_uint_str(compiler_ctx_t *ctx, jsop_t op, unsigned arg1, const WCHAR *arg2)
318 unsigned instr;
319 jsstr_t *str;
321 str = compiler_alloc_string(ctx, arg2);
322 if(!str)
323 return E_OUTOFMEMORY;
325 instr = push_instr(ctx, op);
326 if(!instr)
327 return E_OUTOFMEMORY;
329 instr_ptr(ctx, instr)->u.arg[0].uint = arg1;
330 instr_ptr(ctx, instr)->u.arg[1].str = str;
331 return S_OK;
334 static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
336 unsigned instr;
338 instr = push_instr(ctx, op);
339 if(!instr)
340 return E_OUTOFMEMORY;
342 instr_ptr(ctx, instr)->u.dbl = arg;
343 return S_OK;
346 static inline void set_arg_uint(compiler_ctx_t *ctx, unsigned instr, unsigned arg)
348 instr_ptr(ctx, instr)->u.arg->uint = arg;
351 static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
353 unsigned instr;
355 instr = push_instr(ctx, op);
356 if(!instr)
357 return E_OUTOFMEMORY;
359 set_arg_uint(ctx, instr, arg);
360 return S_OK;
363 static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
365 HRESULT hres;
367 hres = compile_expression(ctx, expr->expression1, TRUE);
368 if(FAILED(hres))
369 return hres;
371 hres = compile_expression(ctx, expr->expression2, TRUE);
372 if(FAILED(hres))
373 return hres;
375 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
378 static HRESULT compile_unary_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op)
380 HRESULT hres;
382 hres = compile_expression(ctx, expr->expression, TRUE);
383 if(FAILED(hres))
384 return hres;
386 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
389 /* ECMA-262 3rd Edition 11.2.1 */
390 static HRESULT compile_member_expression(compiler_ctx_t *ctx, member_expression_t *expr)
392 HRESULT hres;
394 hres = compile_expression(ctx, expr->expression, TRUE);
395 if(FAILED(hres))
396 return hres;
398 return push_instr_bstr(ctx, OP_member, expr->identifier);
401 #define LABEL_FLAG 0x80000000
403 static unsigned alloc_label(compiler_ctx_t *ctx)
405 if(!ctx->labels_size) {
406 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
407 if(!ctx->labels)
408 return 0;
409 ctx->labels_size = 8;
410 }else if(ctx->labels_size == ctx->labels_cnt) {
411 unsigned *new_labels;
413 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
414 if(!new_labels)
415 return 0;
417 ctx->labels = new_labels;
418 ctx->labels_size *= 2;
421 return ctx->labels_cnt++ | LABEL_FLAG;
424 static void label_set_addr(compiler_ctx_t *ctx, unsigned label)
426 assert(label & LABEL_FLAG);
427 ctx->labels[label & ~LABEL_FLAG] = ctx->code_off;
430 static inline BOOL is_memberid_expr(expression_type_t type)
432 return type == EXPR_IDENT || type == EXPR_MEMBER || type == EXPR_ARRAY;
435 static BOOL bind_local(compiler_ctx_t *ctx, const WCHAR *identifier, int *ret_ref)
437 statement_ctx_t *iter;
438 local_ref_t *ref;
440 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
441 if(iter->using_scope)
442 return FALSE;
445 ref = lookup_local(ctx->func, identifier);
446 if(!ref)
447 return FALSE;
449 *ret_ref = ref->ref;
450 return TRUE;
453 static HRESULT emit_identifier_ref(compiler_ctx_t *ctx, const WCHAR *identifier, unsigned flags)
455 int local_ref;
456 if(bind_local(ctx, identifier, &local_ref))
457 return push_instr_int(ctx, OP_local_ref, local_ref);
458 return push_instr_bstr_uint(ctx, OP_identid, identifier, flags);
461 static HRESULT emit_identifier(compiler_ctx_t *ctx, const WCHAR *identifier)
463 int local_ref;
464 if(bind_local(ctx, identifier, &local_ref))
465 return push_instr_int(ctx, OP_local, local_ref);
466 return push_instr_bstr(ctx, OP_ident, identifier);
469 static HRESULT compile_memberid_expression(compiler_ctx_t *ctx, expression_t *expr, unsigned flags)
471 HRESULT hres = S_OK;
473 switch(expr->type) {
474 case EXPR_IDENT: {
475 identifier_expression_t *ident_expr = (identifier_expression_t*)expr;
477 hres = emit_identifier_ref(ctx, ident_expr->identifier, flags);
478 break;
480 case EXPR_ARRAY: {
481 binary_expression_t *array_expr = (binary_expression_t*)expr;
483 hres = compile_expression(ctx, array_expr->expression1, TRUE);
484 if(FAILED(hres))
485 return hres;
487 hres = compile_expression(ctx, array_expr->expression2, TRUE);
488 if(FAILED(hres))
489 return hres;
491 hres = push_instr_uint(ctx, OP_memberid, flags);
492 break;
494 case EXPR_MEMBER: {
495 member_expression_t *member_expr = (member_expression_t*)expr;
496 jsstr_t *jsstr;
498 hres = compile_expression(ctx, member_expr->expression, TRUE);
499 if(FAILED(hres))
500 return hres;
502 /* FIXME: Potential optimization */
503 jsstr = compiler_alloc_string(ctx, member_expr->identifier);
504 if(!jsstr)
505 return E_OUTOFMEMORY;
507 hres = push_instr_str(ctx, OP_str, jsstr);
508 if(FAILED(hres))
509 return hres;
511 hres = push_instr_uint(ctx, OP_memberid, flags);
512 break;
514 DEFAULT_UNREACHABLE;
517 return hres;
520 static HRESULT compile_increment_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op, int n)
522 HRESULT hres;
524 if(!is_memberid_expr(expr->expression->type)) {
525 hres = compile_expression(ctx, expr->expression, TRUE);
526 if(FAILED(hres))
527 return hres;
529 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
532 hres = compile_memberid_expression(ctx, expr->expression, fdexNameEnsure);
533 if(FAILED(hres))
534 return hres;
536 return push_instr_int(ctx, op, n);
539 /* ECMA-262 3rd Edition 11.14 */
540 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr, BOOL emit_ret)
542 HRESULT hres;
544 hres = compile_expression(ctx, expr->expression1, FALSE);
545 if(FAILED(hres))
546 return hres;
548 return compile_expression(ctx, expr->expression2, emit_ret);
551 /* ECMA-262 3rd Edition 11.11 */
552 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
554 unsigned instr;
555 HRESULT hres;
557 hres = compile_expression(ctx, expr->expression1, TRUE);
558 if(FAILED(hres))
559 return hres;
561 instr = push_instr(ctx, op);
562 if(!instr)
563 return E_OUTOFMEMORY;
565 hres = compile_expression(ctx, expr->expression2, TRUE);
566 if(FAILED(hres))
567 return hres;
569 set_arg_uint(ctx, instr, ctx->code_off);
570 return S_OK;
573 /* ECMA-262 3rd Edition 11.12 */
574 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
576 unsigned jmp_false, jmp_end;
577 HRESULT hres;
579 hres = compile_expression(ctx, expr->expression, TRUE);
580 if(FAILED(hres))
581 return hres;
583 jmp_false = push_instr(ctx, OP_cnd_z);
584 if(!jmp_false)
585 return E_OUTOFMEMORY;
587 hres = compile_expression(ctx, expr->true_expression, TRUE);
588 if(FAILED(hres))
589 return hres;
591 jmp_end = push_instr(ctx, OP_jmp);
592 if(!jmp_end)
593 return E_OUTOFMEMORY;
595 set_arg_uint(ctx, jmp_false, ctx->code_off);
596 hres = push_instr_uint(ctx, OP_pop, 1);
597 if(FAILED(hres))
598 return hres;
600 hres = compile_expression(ctx, expr->false_expression, TRUE);
601 if(FAILED(hres))
602 return hres;
604 set_arg_uint(ctx, jmp_end, ctx->code_off);
605 return S_OK;
608 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
610 unsigned arg_cnt = 0;
611 argument_t *arg;
612 HRESULT hres;
614 hres = compile_expression(ctx, expr->expression, TRUE);
615 if(FAILED(hres))
616 return hres;
618 for(arg = expr->argument_list; arg; arg = arg->next) {
619 hres = compile_expression(ctx, arg->expr, TRUE);
620 if(FAILED(hres))
621 return hres;
622 arg_cnt++;
625 hres = push_instr_uint(ctx, OP_new, arg_cnt);
626 if(FAILED(hres))
627 return hres;
629 hres = push_instr_uint(ctx, OP_pop, arg_cnt+1);
630 if(FAILED(hres))
631 return hres;
633 return push_instr(ctx, OP_push_acc) ? S_OK : E_OUTOFMEMORY;
636 static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL emit_ret)
638 unsigned arg_cnt = 0, extra_args;
639 argument_t *arg;
640 unsigned instr;
641 jsop_t op;
642 HRESULT hres;
644 if(is_memberid_expr(expr->expression->type)) {
645 op = OP_call_member;
646 extra_args = 2;
647 hres = compile_memberid_expression(ctx, expr->expression, 0);
648 }else {
649 op = OP_call;
650 extra_args = 1;
651 hres = compile_expression(ctx, expr->expression, TRUE);
654 if(FAILED(hres))
655 return hres;
657 for(arg = expr->argument_list; arg; arg = arg->next) {
658 hres = compile_expression(ctx, arg->expr, TRUE);
659 if(FAILED(hres))
660 return hres;
661 arg_cnt++;
664 instr = push_instr(ctx, op);
665 if(!instr)
666 return E_OUTOFMEMORY;
668 instr_ptr(ctx, instr)->u.arg[0].uint = arg_cnt;
669 instr_ptr(ctx, instr)->u.arg[1].lng = emit_ret;
671 hres = push_instr_uint(ctx, OP_pop, arg_cnt + extra_args);
672 if(FAILED(hres))
673 return hres;
675 return !emit_ret || push_instr(ctx, OP_push_acc) ? S_OK : E_OUTOFMEMORY;
678 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
680 HRESULT hres;
682 switch(expr->expression->type) {
683 case EXPR_ARRAY: {
684 binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
686 hres = compile_expression(ctx, array_expr->expression1, TRUE);
687 if(FAILED(hres))
688 return hres;
690 hres = compile_expression(ctx, array_expr->expression2, TRUE);
691 if(FAILED(hres))
692 return hres;
694 if(!push_instr(ctx, OP_delete))
695 return E_OUTOFMEMORY;
696 break;
698 case EXPR_MEMBER: {
699 member_expression_t *member_expr = (member_expression_t*)expr->expression;
700 jsstr_t *jsstr;
702 hres = compile_expression(ctx, member_expr->expression, TRUE);
703 if(FAILED(hres))
704 return hres;
706 /* FIXME: Potential optimization */
707 jsstr = compiler_alloc_string(ctx, member_expr->identifier);
708 if(!jsstr)
709 return E_OUTOFMEMORY;
711 hres = push_instr_str(ctx, OP_str, jsstr);
712 if(FAILED(hres))
713 return hres;
715 if(!push_instr(ctx, OP_delete))
716 return E_OUTOFMEMORY;
717 break;
719 case EXPR_IDENT:
720 return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
721 default: {
722 WARN("invalid delete, unimplemented exception message\n");
724 hres = compile_expression(ctx, expr->expression, TRUE);
725 if(FAILED(hres))
726 return hres;
728 return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, L"FIXME");
732 return S_OK;
735 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
737 BOOL use_throw_path = FALSE;
738 unsigned arg_cnt = 0;
739 HRESULT hres;
741 if(expr->expression1->type == EXPR_CALL) {
742 call_expression_t *call_expr = (call_expression_t*)expr->expression1;
743 argument_t *arg;
745 if(is_memberid_expr(call_expr->expression->type) && call_expr->argument_list) {
746 hres = compile_memberid_expression(ctx, call_expr->expression, fdexNameEnsure);
747 if(FAILED(hres))
748 return hres;
750 for(arg = call_expr->argument_list; arg; arg = arg->next) {
751 hres = compile_expression(ctx, arg->expr, TRUE);
752 if(FAILED(hres))
753 return hres;
754 arg_cnt++;
757 if(op != OP_LAST) {
758 unsigned instr;
760 /* We need to call the functions twice: to get the value and to set it.
761 * JavaScript interpreted functions may to modify value on the stack,
762 * but assignment calls are allowed only on external functions, so we
763 * may reuse the stack here. */
764 instr = push_instr(ctx, OP_call_member);
765 if(!instr)
766 return E_OUTOFMEMORY;
767 instr_ptr(ctx, instr)->u.arg[0].uint = arg_cnt;
768 instr_ptr(ctx, instr)->u.arg[1].lng = 1;
770 if(!push_instr(ctx, OP_push_acc))
771 return E_OUTOFMEMORY;
773 }else {
774 use_throw_path = TRUE;
776 }else if(is_memberid_expr(expr->expression1->type)) {
777 hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
778 if(FAILED(hres))
779 return hres;
780 if(op != OP_LAST && !push_instr(ctx, OP_refval))
781 return E_OUTOFMEMORY;
782 }else {
783 use_throw_path = TRUE;
786 if(use_throw_path) {
787 /* Illegal assignment: evaluate and throw */
788 hres = compile_expression(ctx, expr->expression1, TRUE);
789 if(FAILED(hres))
790 return hres;
792 hres = compile_expression(ctx, expr->expression2, TRUE);
793 if(FAILED(hres))
794 return hres;
796 if(op != OP_LAST && !push_instr(ctx, op))
797 return E_OUTOFMEMORY;
799 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
802 hres = compile_expression(ctx, expr->expression2, TRUE);
803 if(FAILED(hres))
804 return hres;
806 if(op != OP_LAST && !push_instr(ctx, op))
807 return E_OUTOFMEMORY;
809 if(arg_cnt)
810 return push_instr_uint(ctx, OP_assign_call, arg_cnt);
812 if(!push_instr(ctx, OP_assign))
813 return E_OUTOFMEMORY;
815 return S_OK;
818 static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
820 jsop_t op;
821 HRESULT hres;
823 if(is_memberid_expr(expr->expression->type)) {
824 if(expr->expression->type == EXPR_IDENT)
825 return push_instr_bstr(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
827 op = OP_typeofid;
828 hres = compile_memberid_expression(ctx, expr->expression, 0);
829 }else {
830 op = OP_typeof;
831 hres = compile_expression(ctx, expr->expression, TRUE);
833 if(FAILED(hres))
834 return hres;
836 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
839 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
841 switch(literal->type) {
842 case LT_BOOL:
843 return push_instr_int(ctx, OP_bool, literal->u.bval);
844 case LT_DOUBLE:
845 return push_instr_double(ctx, OP_double, literal->u.dval);
846 case LT_NULL:
847 return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
848 case LT_STRING:
849 return push_instr_str(ctx, OP_str, literal->u.str);
850 case LT_REGEXP:
851 return push_instr_str_uint(ctx, OP_regexp, literal->u.regexp.str, literal->u.regexp.flags);
852 DEFAULT_UNREACHABLE;
854 return E_FAIL;
857 static HRESULT literal_as_string(compiler_ctx_t *ctx, literal_t *literal, jsstr_t **str)
859 switch(literal->type) {
860 case LT_STRING:
861 *str = literal->u.str;
862 break;
863 case LT_DOUBLE:
864 return double_to_string(literal->u.dval, str);
865 DEFAULT_UNREACHABLE;
868 return *str ? S_OK : E_OUTOFMEMORY;
871 static HRESULT compile_array_literal(compiler_ctx_t *ctx, array_literal_expression_t *expr)
873 unsigned length = 0;
874 array_element_t *iter;
875 unsigned array_instr;
876 HRESULT hres;
878 array_instr = push_instr(ctx, OP_carray);
880 for(iter = expr->element_list; iter; iter = iter->next) {
881 length += iter->elision;
883 hres = compile_expression(ctx, iter->expr, TRUE);
884 if(FAILED(hres))
885 return hres;
887 hres = push_instr_uint(ctx, OP_carray_set, length);
888 if(FAILED(hres))
889 return hres;
891 length++;
894 instr_ptr(ctx, array_instr)->u.arg[0].uint = length + expr->length;
895 return S_OK;
898 static HRESULT compile_object_literal(compiler_ctx_t *ctx, property_value_expression_t *expr)
900 property_definition_t *iter;
901 jsstr_t *name;
902 HRESULT hres;
904 if(!push_instr(ctx, OP_new_obj))
905 return E_OUTOFMEMORY;
907 for(iter = expr->property_list; iter; iter = iter->next) {
908 hres = literal_as_string(ctx, iter->name, &name);
909 if(FAILED(hres))
910 return hres;
912 hres = compile_expression(ctx, iter->value, TRUE);
913 if(FAILED(hres))
914 return hres;
916 hres = push_instr_str_uint(ctx, OP_obj_prop, name, iter->type);
917 if(FAILED(hres))
918 return hres;
921 return S_OK;
924 static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr, BOOL emit_ret)
926 return emit_ret ? push_instr_uint(ctx, OP_func, expr->func_id) : S_OK;
929 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr, BOOL emit_ret)
931 HRESULT hres;
933 switch(expr->type) {
934 case EXPR_ADD:
935 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
936 break;
937 case EXPR_AND:
938 hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_z);
939 break;
940 case EXPR_ARRAY:
941 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
942 break;
943 case EXPR_ARRAYLIT:
944 hres = compile_array_literal(ctx, (array_literal_expression_t*)expr);
945 break;
946 case EXPR_ASSIGN:
947 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
948 break;
949 case EXPR_ASSIGNADD:
950 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
951 break;
952 case EXPR_ASSIGNAND:
953 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_and);
954 break;
955 case EXPR_ASSIGNSUB:
956 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
957 break;
958 case EXPR_ASSIGNMUL:
959 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
960 break;
961 case EXPR_ASSIGNDIV:
962 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
963 break;
964 case EXPR_ASSIGNMOD:
965 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
966 break;
967 case EXPR_ASSIGNOR:
968 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
969 break;
970 case EXPR_ASSIGNLSHIFT:
971 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_lshift);
972 break;
973 case EXPR_ASSIGNRSHIFT:
974 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift);
975 break;
976 case EXPR_ASSIGNRRSHIFT:
977 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
978 break;
979 case EXPR_ASSIGNXOR:
980 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
981 break;
982 case EXPR_BAND:
983 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
984 break;
985 case EXPR_BITNEG:
986 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
987 break;
988 case EXPR_BOR:
989 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
990 break;
991 case EXPR_CALL:
992 return compile_call_expression(ctx, (call_expression_t*)expr, emit_ret);
993 case EXPR_COMMA:
994 return compile_comma_expression(ctx, (binary_expression_t*)expr, emit_ret);
995 case EXPR_COND:
996 hres = compile_conditional_expression(ctx, (conditional_expression_t*)expr);
997 break;
998 case EXPR_DELETE:
999 hres = compile_delete_expression(ctx, (unary_expression_t*)expr);
1000 break;
1001 case EXPR_DIV:
1002 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
1003 break;
1004 case EXPR_EQ:
1005 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
1006 break;
1007 case EXPR_EQEQ:
1008 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
1009 break;
1010 case EXPR_FUNC:
1011 return compile_function_expression(ctx, (function_expression_t*)expr, emit_ret);
1012 case EXPR_GREATER:
1013 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
1014 break;
1015 case EXPR_GREATEREQ:
1016 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
1017 break;
1018 case EXPR_IDENT:
1019 hres = emit_identifier(ctx, ((identifier_expression_t*)expr)->identifier);
1020 break;
1021 case EXPR_IN:
1022 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
1023 break;
1024 case EXPR_INSTANCEOF:
1025 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
1026 break;
1027 case EXPR_LESS:
1028 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
1029 break;
1030 case EXPR_LESSEQ:
1031 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
1032 break;
1033 case EXPR_LITERAL:
1034 hres = compile_literal(ctx, ((literal_expression_t*)expr)->literal);
1035 break;
1036 case EXPR_LOGNEG:
1037 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
1038 break;
1039 case EXPR_LSHIFT:
1040 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
1041 break;
1042 case EXPR_MEMBER:
1043 hres = compile_member_expression(ctx, (member_expression_t*)expr);
1044 break;
1045 case EXPR_MINUS:
1046 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
1047 break;
1048 case EXPR_MOD:
1049 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
1050 break;
1051 case EXPR_MUL:
1052 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
1053 break;
1054 case EXPR_NEW:
1055 hres = compile_new_expression(ctx, (call_expression_t*)expr);
1056 break;
1057 case EXPR_NOTEQ:
1058 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
1059 break;
1060 case EXPR_NOTEQEQ:
1061 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
1062 break;
1063 case EXPR_OR:
1064 hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_nz);
1065 break;
1066 case EXPR_PLUS:
1067 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
1068 break;
1069 case EXPR_POSTDEC:
1070 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
1071 break;
1072 case EXPR_POSTINC:
1073 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
1074 break;
1075 case EXPR_PREDEC:
1076 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
1077 break;
1078 case EXPR_PREINC:
1079 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
1080 break;
1081 case EXPR_PROPVAL:
1082 hres = compile_object_literal(ctx, (property_value_expression_t*)expr);
1083 break;
1084 case EXPR_RSHIFT:
1085 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
1086 break;
1087 case EXPR_RRSHIFT:
1088 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
1089 break;
1090 case EXPR_SUB:
1091 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
1092 break;
1093 case EXPR_THIS:
1094 return !emit_ret || push_instr(ctx, OP_this) ? S_OK : E_OUTOFMEMORY;
1095 case EXPR_TYPEOF:
1096 hres = compile_typeof_expression(ctx, (unary_expression_t*)expr);
1097 break;
1098 case EXPR_VOID:
1099 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
1100 break;
1101 case EXPR_BXOR:
1102 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
1103 break;
1104 DEFAULT_UNREACHABLE;
1107 if(FAILED(hres))
1108 return hres;
1110 return emit_ret ? S_OK : push_instr_uint(ctx, OP_pop, 1);
1113 static inline BOOL is_loop_statement(statement_type_t type)
1115 return type == STAT_FOR || type == STAT_FORIN || type == STAT_WHILE;
1118 /* ECMA-262 3rd Edition 12.1 */
1119 static HRESULT compile_block_statement(compiler_ctx_t *ctx, statement_t *iter)
1121 HRESULT hres;
1123 while(iter) {
1124 hres = compile_statement(ctx, NULL, iter);
1125 if(FAILED(hres))
1126 return hres;
1128 iter = iter->next;
1131 return S_OK;
1134 /* ECMA-262 3rd Edition 12.2 */
1135 static HRESULT compile_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
1137 variable_declaration_t *iter;
1138 HRESULT hres;
1140 assert(list != NULL);
1142 for(iter = list; iter; iter = iter->next) {
1143 if(!iter->expr)
1144 continue;
1146 hres = emit_identifier_ref(ctx, iter->identifier, 0);
1147 if(FAILED(hres))
1148 return hres;
1150 hres = compile_expression(ctx, iter->expr, TRUE);
1151 if(FAILED(hres))
1152 return hres;
1154 if(!push_instr(ctx, OP_assign))
1155 return E_OUTOFMEMORY;
1157 hres = push_instr_uint(ctx, OP_pop, 1);
1158 if(FAILED(hres))
1159 return hres;
1162 return S_OK;
1165 /* ECMA-262 3rd Edition 12.2 */
1166 static HRESULT compile_var_statement(compiler_ctx_t *ctx, var_statement_t *stat)
1168 return compile_variable_list(ctx, stat->variable_list);
1171 /* ECMA-262 3rd Edition 12.4 */
1172 static HRESULT compile_expression_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1174 HRESULT hres;
1176 hres = compile_expression(ctx, stat->expr, ctx->from_eval);
1177 if(FAILED(hres))
1178 return hres;
1180 return !ctx->from_eval || push_instr(ctx, OP_setret) ? S_OK : E_OUTOFMEMORY;
1183 /* ECMA-262 3rd Edition 12.5 */
1184 static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat)
1186 unsigned jmp_else;
1187 HRESULT hres;
1189 hres = compile_expression(ctx, stat->expr, TRUE);
1190 if(FAILED(hres))
1191 return hres;
1193 jmp_else = push_instr(ctx, OP_jmp_z);
1194 if(!jmp_else)
1195 return E_OUTOFMEMORY;
1197 hres = compile_statement(ctx, NULL, stat->if_stat);
1198 if(FAILED(hres))
1199 return hres;
1201 if(stat->else_stat) {
1202 unsigned jmp_end;
1204 jmp_end = push_instr(ctx, OP_jmp);
1205 if(!jmp_end)
1206 return E_OUTOFMEMORY;
1208 set_arg_uint(ctx, jmp_else, ctx->code_off);
1210 hres = compile_statement(ctx, NULL, stat->else_stat);
1211 if(FAILED(hres))
1212 return hres;
1214 set_arg_uint(ctx, jmp_end, ctx->code_off);
1215 }else {
1216 set_arg_uint(ctx, jmp_else, ctx->code_off);
1219 return S_OK;
1222 /* ECMA-262 3rd Edition 12.6.2 */
1223 static HRESULT compile_while_statement(compiler_ctx_t *ctx, while_statement_t *stat)
1225 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1226 unsigned jmp_off;
1227 HRESULT hres;
1229 stat_ctx.break_label = alloc_label(ctx);
1230 if(!stat_ctx.break_label)
1231 return E_OUTOFMEMORY;
1233 stat_ctx.continue_label = alloc_label(ctx);
1234 if(!stat_ctx.continue_label)
1235 return E_OUTOFMEMORY;
1237 jmp_off = ctx->code_off;
1239 if(!stat->do_while) {
1240 label_set_addr(ctx, stat_ctx.continue_label);
1241 hres = compile_expression(ctx, stat->expr, TRUE);
1242 if(FAILED(hres))
1243 return hres;
1245 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1246 if(FAILED(hres))
1247 return hres;
1250 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1251 if(FAILED(hres))
1252 return hres;
1254 set_compiler_loc(ctx, stat->stat.loc);
1255 if(stat->do_while) {
1256 label_set_addr(ctx, stat_ctx.continue_label);
1257 hres = compile_expression(ctx, stat->expr, TRUE);
1258 if(FAILED(hres))
1259 return hres;
1261 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1262 if(FAILED(hres))
1263 return hres;
1266 hres = push_instr_uint(ctx, OP_jmp, jmp_off);
1267 if(FAILED(hres))
1268 return hres;
1270 label_set_addr(ctx, stat_ctx.break_label);
1271 return S_OK;
1274 /* ECMA-262 3rd Edition 12.6.3 */
1275 static HRESULT compile_for_statement(compiler_ctx_t *ctx, for_statement_t *stat)
1277 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1278 unsigned expr_off;
1279 HRESULT hres;
1281 if(stat->variable_list) {
1282 hres = compile_variable_list(ctx, stat->variable_list);
1283 if(FAILED(hres))
1284 return hres;
1285 }else if(stat->begin_expr) {
1286 hres = compile_expression(ctx, stat->begin_expr, FALSE);
1287 if(FAILED(hres))
1288 return hres;
1291 stat_ctx.break_label = alloc_label(ctx);
1292 if(!stat_ctx.break_label)
1293 return E_OUTOFMEMORY;
1295 stat_ctx.continue_label = alloc_label(ctx);
1296 if(!stat_ctx.continue_label)
1297 return E_OUTOFMEMORY;
1299 expr_off = ctx->code_off;
1301 if(stat->expr) {
1302 set_compiler_loc(ctx, stat->expr_loc);
1303 hres = compile_expression(ctx, stat->expr, TRUE);
1304 if(FAILED(hres))
1305 return hres;
1307 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1308 if(FAILED(hres))
1309 return hres;
1312 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1313 if(FAILED(hres))
1314 return hres;
1316 label_set_addr(ctx, stat_ctx.continue_label);
1318 if(stat->end_expr) {
1319 set_compiler_loc(ctx, stat->end_loc);
1320 hres = compile_expression(ctx, stat->end_expr, FALSE);
1321 if(FAILED(hres))
1322 return hres;
1325 hres = push_instr_uint(ctx, OP_jmp, expr_off);
1326 if(FAILED(hres))
1327 return hres;
1329 label_set_addr(ctx, stat_ctx.break_label);
1330 return S_OK;
1333 /* ECMA-262 3rd Edition 12.6.4 */
1334 static HRESULT compile_forin_statement(compiler_ctx_t *ctx, forin_statement_t *stat)
1336 statement_ctx_t stat_ctx = {4, FALSE, FALSE};
1337 HRESULT hres;
1339 if(stat->variable) {
1340 hres = compile_variable_list(ctx, stat->variable);
1341 if(FAILED(hres))
1342 return hres;
1345 stat_ctx.break_label = alloc_label(ctx);
1346 if(!stat_ctx.break_label)
1347 return E_OUTOFMEMORY;
1349 stat_ctx.continue_label = alloc_label(ctx);
1350 if(!stat_ctx.continue_label)
1351 return E_OUTOFMEMORY;
1353 hres = compile_expression(ctx, stat->in_expr, TRUE);
1354 if(FAILED(hres))
1355 return hres;
1357 if(stat->variable) {
1358 hres = emit_identifier_ref(ctx, stat->variable->identifier, fdexNameEnsure);
1359 if(FAILED(hres))
1360 return hres;
1361 }else if(is_memberid_expr(stat->expr->type)) {
1362 hres = compile_memberid_expression(ctx, stat->expr, fdexNameEnsure);
1363 if(FAILED(hres))
1364 return hres;
1365 }else {
1366 hres = push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
1367 if(FAILED(hres))
1368 return hres;
1370 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1371 return S_OK;
1374 hres = push_instr_int(ctx, OP_int, DISPID_STARTENUM);
1375 if(FAILED(hres))
1376 return hres;
1378 label_set_addr(ctx, stat_ctx.continue_label);
1379 hres = push_instr_uint(ctx, OP_forin, stat_ctx.break_label);
1380 if(FAILED(hres))
1381 return E_OUTOFMEMORY;
1383 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1384 if(FAILED(hres))
1385 return hres;
1387 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.continue_label);
1388 if(FAILED(hres))
1389 return hres;
1391 label_set_addr(ctx, stat_ctx.break_label);
1392 return S_OK;
1395 static HRESULT pop_to_stat(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx)
1397 unsigned stack_pop = 0;
1398 statement_ctx_t *iter;
1399 HRESULT hres;
1401 for(iter = ctx->stat_ctx; iter != stat_ctx; iter = iter->next) {
1402 if(iter->using_scope && !push_instr(ctx, OP_pop_scope))
1403 return E_OUTOFMEMORY;
1404 if(iter->using_except) {
1405 if(stack_pop) {
1406 hres = push_instr_uint(ctx, OP_pop, stack_pop);
1407 if(FAILED(hres))
1408 return hres;
1409 stack_pop = 0;
1411 hres = push_instr_uint(ctx, OP_pop_except, ctx->code_off+1);
1412 if(FAILED(hres))
1413 return hres;
1415 stack_pop += iter->stack_use;
1418 if(stack_pop) {
1419 hres = push_instr_uint(ctx, OP_pop, stack_pop);
1420 if(FAILED(hres))
1421 return hres;
1424 return S_OK;
1427 /* ECMA-262 3rd Edition 12.7 */
1428 static HRESULT compile_continue_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1430 statement_ctx_t *pop_ctx;
1431 HRESULT hres;
1433 if(stat->identifier) {
1434 statement_t *label_stat;
1435 statement_ctx_t *iter;
1437 pop_ctx = NULL;
1439 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1440 if(iter->continue_label)
1441 pop_ctx = iter;
1442 if(iter->labelled_stat && !wcscmp(iter->labelled_stat->identifier, stat->identifier))
1443 break;
1446 if(!iter) {
1447 WARN("Label not found\n");
1448 return JS_E_LABEL_NOT_FOUND;
1451 /* Labelled continue are allowed only on loops */
1452 for(label_stat = iter->labelled_stat->statement;
1453 label_stat->type == STAT_LABEL;
1454 label_stat = ((labelled_statement_t*)label_stat)->statement);
1455 if(!is_loop_statement(label_stat->type)) {
1456 WARN("Label is not a loop\n");
1457 return JS_E_INVALID_CONTINUE;
1460 assert(pop_ctx != NULL);
1461 }else {
1462 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1463 if(pop_ctx->continue_label)
1464 break;
1467 if(!pop_ctx) {
1468 WARN("continue outside loop\n");
1469 return JS_E_INVALID_CONTINUE;
1473 hres = pop_to_stat(ctx, pop_ctx);
1474 if(FAILED(hres))
1475 return hres;
1477 return push_instr_uint(ctx, OP_jmp, pop_ctx->continue_label);
1480 /* ECMA-262 3rd Edition 12.8 */
1481 static HRESULT compile_break_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1483 statement_ctx_t *pop_ctx;
1484 HRESULT hres;
1486 if(stat->identifier) {
1487 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1488 if(pop_ctx->labelled_stat && !wcscmp(pop_ctx->labelled_stat->identifier, stat->identifier)) {
1489 assert(pop_ctx->break_label);
1490 break;
1494 if(!pop_ctx) {
1495 WARN("Label not found\n");
1496 return JS_E_LABEL_NOT_FOUND;
1498 }else {
1499 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1500 if(pop_ctx->break_label && !pop_ctx->labelled_stat)
1501 break;
1504 if(!pop_ctx) {
1505 WARN("Break outside loop\n");
1506 return JS_E_INVALID_BREAK;
1510 hres = pop_to_stat(ctx, pop_ctx->next);
1511 if(FAILED(hres))
1512 return hres;
1514 return push_instr_uint(ctx, OP_jmp, pop_ctx->break_label);
1517 /* ECMA-262 3rd Edition 12.9 */
1518 static HRESULT compile_return_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1520 HRESULT hres;
1522 if(ctx->from_eval) {
1523 WARN("misplaced return statement\n");
1524 return JS_E_MISPLACED_RETURN;
1527 if(stat->expr) {
1528 hres = compile_expression(ctx, stat->expr, TRUE);
1529 if(FAILED(hres))
1530 return hres;
1531 if(!push_instr(ctx, OP_setret))
1532 return E_OUTOFMEMORY;
1535 hres = pop_to_stat(ctx, NULL);
1536 if(FAILED(hres))
1537 return hres;
1539 return push_instr_uint(ctx, OP_ret, !stat->expr);
1542 /* ECMA-262 3rd Edition 12.10 */
1543 static HRESULT compile_with_statement(compiler_ctx_t *ctx, with_statement_t *stat)
1545 statement_ctx_t stat_ctx = {0, TRUE, FALSE};
1546 HRESULT hres;
1548 hres = compile_expression(ctx, stat->expr, TRUE);
1549 if(FAILED(hres))
1550 return hres;
1552 if(!push_instr(ctx, OP_push_scope))
1553 return E_OUTOFMEMORY;
1555 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1556 if(FAILED(hres))
1557 return hres;
1559 if(!push_instr(ctx, OP_pop_scope))
1560 return E_OUTOFMEMORY;
1562 return S_OK;
1565 /* ECMA-262 3rd Edition 12.10 */
1566 static HRESULT compile_labelled_statement(compiler_ctx_t *ctx, labelled_statement_t *stat)
1568 statement_ctx_t stat_ctx = {0, FALSE, FALSE, 0, 0, stat}, *iter;
1569 HRESULT hres;
1571 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1572 if(iter->labelled_stat && !wcscmp(iter->labelled_stat->identifier, stat->identifier)) {
1573 WARN("Label %s redefined\n", debugstr_w(stat->identifier));
1574 return JS_E_LABEL_REDEFINED;
1578 /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1579 stat_ctx.break_label = alloc_label(ctx);
1580 if(!stat_ctx.break_label)
1581 return E_OUTOFMEMORY;
1583 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1584 if(FAILED(hres))
1585 return hres;
1587 label_set_addr(ctx, stat_ctx.break_label);
1588 return S_OK;
1591 /* ECMA-262 3rd Edition 12.13 */
1592 static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat)
1594 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1595 unsigned case_cnt = 0, *case_jmps, i, default_jmp;
1596 BOOL have_default = FALSE;
1597 statement_t *stat_iter;
1598 case_clausule_t *iter;
1599 HRESULT hres;
1601 hres = compile_expression(ctx, stat->expr, TRUE);
1602 if(FAILED(hres))
1603 return hres;
1605 stat_ctx.break_label = alloc_label(ctx);
1606 if(!stat_ctx.break_label)
1607 return E_OUTOFMEMORY;
1609 for(iter = stat->case_list; iter; iter = iter->next) {
1610 if(iter->expr)
1611 case_cnt++;
1614 case_jmps = heap_alloc(case_cnt * sizeof(*case_jmps));
1615 if(!case_jmps)
1616 return E_OUTOFMEMORY;
1618 i = 0;
1619 for(iter = stat->case_list; iter; iter = iter->next) {
1620 if(!iter->expr) {
1621 have_default = TRUE;
1622 continue;
1625 set_compiler_loc(ctx, iter->loc);
1626 hres = compile_expression(ctx, iter->expr, TRUE);
1627 if(FAILED(hres))
1628 break;
1630 case_jmps[i] = push_instr(ctx, OP_case);
1631 if(!case_jmps[i]) {
1632 hres = E_OUTOFMEMORY;
1633 break;
1635 i++;
1638 if(SUCCEEDED(hres)) {
1639 hres = push_instr_uint(ctx, OP_pop, 1);
1640 if(SUCCEEDED(hres)) {
1641 default_jmp = push_instr(ctx, OP_jmp);
1642 if(!default_jmp)
1643 hres = E_OUTOFMEMORY;
1647 if(FAILED(hres)) {
1648 heap_free(case_jmps);
1649 return hres;
1652 i = 0;
1653 for(iter = stat->case_list; iter; iter = iter->next) {
1654 while(iter->next && iter->next->stat == iter->stat) {
1655 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1656 iter = iter->next;
1659 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1661 for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter);
1662 stat_iter = stat_iter->next) {
1663 hres = compile_statement(ctx, &stat_ctx, stat_iter);
1664 if(FAILED(hres))
1665 break;
1667 if(FAILED(hres))
1668 break;
1671 heap_free(case_jmps);
1672 if(FAILED(hres))
1673 return hres;
1674 assert(i == case_cnt);
1676 if(!have_default) {
1677 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.break_label);
1678 if(FAILED(hres))
1679 return hres;
1680 set_arg_uint(ctx, default_jmp, ctx->code_off);
1683 label_set_addr(ctx, stat_ctx.break_label);
1684 return S_OK;
1687 /* ECMA-262 3rd Edition 12.13 */
1688 static HRESULT compile_throw_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1690 HRESULT hres;
1692 hres = compile_expression(ctx, stat->expr, TRUE);
1693 if(FAILED(hres))
1694 return hres;
1696 return push_instr(ctx, OP_throw) ? S_OK : E_OUTOFMEMORY;
1699 /* ECMA-262 3rd Edition 12.14 */
1700 static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat)
1702 statement_ctx_t try_ctx = {0, FALSE, TRUE}, finally_ctx = {2, FALSE, FALSE};
1703 unsigned push_except, finally_off = 0, catch_off = 0, pop_except, catch_pop_except = 0;
1704 BSTR ident;
1705 HRESULT hres;
1707 push_except = push_instr(ctx, OP_push_except);
1708 if(!push_except)
1709 return E_OUTOFMEMORY;
1711 if(stat->catch_block) {
1712 ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier);
1713 if(!ident)
1714 return E_OUTOFMEMORY;
1715 }else {
1716 ident = NULL;
1719 hres = compile_statement(ctx, &try_ctx, stat->try_statement);
1720 if(FAILED(hres))
1721 return hres;
1723 pop_except = push_instr(ctx, OP_pop_except);
1724 if(!pop_except)
1725 return E_OUTOFMEMORY;
1727 if(stat->catch_block) {
1728 statement_ctx_t catch_ctx = {0, TRUE, stat->finally_statement != NULL};
1730 if(stat->finally_statement)
1731 catch_ctx.using_except = TRUE;
1733 catch_off = ctx->code_off;
1735 hres = push_instr_bstr(ctx, OP_enter_catch, ident);
1736 if(FAILED(hres))
1737 return hres;
1739 hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement);
1740 if(FAILED(hres))
1741 return hres;
1743 if(!push_instr(ctx, OP_pop_scope))
1744 return E_OUTOFMEMORY;
1746 if(stat->finally_statement) {
1747 catch_pop_except = push_instr(ctx, OP_pop_except);
1748 if(!catch_pop_except)
1749 return E_OUTOFMEMORY;
1753 if(stat->finally_statement) {
1755 * finally block expects two elements on the stack, which may be:
1756 * - (true, return_addr) set by OP_pop_except, OP_end_finally jumps back to passed address
1757 * - (false, exception_value) set when unwinding an exception, which OP_end_finally rethrows
1759 finally_off = ctx->code_off;
1760 hres = compile_statement(ctx, &finally_ctx, stat->finally_statement);
1761 if(FAILED(hres))
1762 return hres;
1764 set_compiler_loc(ctx, stat->finally_loc);
1765 if(!push_instr(ctx, OP_end_finally))
1766 return E_OUTOFMEMORY;
1769 instr_ptr(ctx, pop_except)->u.arg[0].uint = ctx->code_off;
1770 if(catch_pop_except)
1771 instr_ptr(ctx, catch_pop_except)->u.arg[0].uint = ctx->code_off;
1772 instr_ptr(ctx, push_except)->u.arg[0].uint = catch_off;
1773 instr_ptr(ctx, push_except)->u.arg[1].uint = finally_off;
1774 return S_OK;
1777 static HRESULT compile_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1779 HRESULT hres;
1781 if(stat_ctx) {
1782 stat_ctx->next = ctx->stat_ctx;
1783 ctx->stat_ctx = stat_ctx;
1786 set_compiler_loc(ctx, stat->loc);
1788 switch(stat->type) {
1789 case STAT_BLOCK:
1790 hres = compile_block_statement(ctx, ((block_statement_t*)stat)->stat_list);
1791 break;
1792 case STAT_BREAK:
1793 hres = compile_break_statement(ctx, (branch_statement_t*)stat);
1794 break;
1795 case STAT_CONTINUE:
1796 hres = compile_continue_statement(ctx, (branch_statement_t*)stat);
1797 break;
1798 case STAT_EMPTY:
1799 /* nothing to do */
1800 hres = S_OK;
1801 break;
1802 case STAT_EXPR:
1803 hres = compile_expression_statement(ctx, (expression_statement_t*)stat);
1804 break;
1805 case STAT_FOR:
1806 hres = compile_for_statement(ctx, (for_statement_t*)stat);
1807 break;
1808 case STAT_FORIN:
1809 hres = compile_forin_statement(ctx, (forin_statement_t*)stat);
1810 break;
1811 case STAT_IF:
1812 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1813 break;
1814 case STAT_LABEL:
1815 hres = compile_labelled_statement(ctx, (labelled_statement_t*)stat);
1816 break;
1817 case STAT_RETURN:
1818 hres = compile_return_statement(ctx, (expression_statement_t*)stat);
1819 break;
1820 case STAT_SWITCH:
1821 hres = compile_switch_statement(ctx, (switch_statement_t*)stat);
1822 break;
1823 case STAT_THROW:
1824 hres = compile_throw_statement(ctx, (expression_statement_t*)stat);
1825 break;
1826 case STAT_TRY:
1827 hres = compile_try_statement(ctx, (try_statement_t*)stat);
1828 break;
1829 case STAT_VAR:
1830 hres = compile_var_statement(ctx, (var_statement_t*)stat);
1831 break;
1832 case STAT_WHILE:
1833 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1834 break;
1835 case STAT_WITH:
1836 hres = compile_with_statement(ctx, (with_statement_t*)stat);
1837 break;
1838 DEFAULT_UNREACHABLE;
1841 if(stat_ctx) {
1842 assert(ctx->stat_ctx == stat_ctx);
1843 ctx->stat_ctx = stat_ctx->next;
1846 return hres;
1849 static int function_local_cmp(const void *key, const struct wine_rb_entry *entry)
1851 function_local_t *local = WINE_RB_ENTRY_VALUE(entry, function_local_t, entry);
1852 return wcscmp(key, local->name);
1855 static inline function_local_t *find_local(compiler_ctx_t *ctx, const WCHAR *name)
1857 struct wine_rb_entry *entry = wine_rb_get(&ctx->locals, name);
1858 return entry ? WINE_RB_ENTRY_VALUE(entry, function_local_t, entry) : NULL;
1861 static BOOL alloc_local(compiler_ctx_t *ctx, BSTR name, int ref)
1863 function_local_t *local;
1865 local = heap_pool_alloc(&ctx->heap, sizeof(*local));
1866 if(!local)
1867 return FALSE;
1869 local->name = name;
1870 local->ref = ref;
1871 wine_rb_put(&ctx->locals, name, &local->entry);
1872 ctx->locals_cnt++;
1873 return TRUE;
1876 static BOOL alloc_variable(compiler_ctx_t *ctx, const WCHAR *name)
1878 BSTR ident;
1880 if(find_local(ctx, name))
1881 return TRUE;
1883 ident = compiler_alloc_bstr(ctx, name);
1884 if(!ident)
1885 return FALSE;
1887 return alloc_local(ctx, ident, ctx->func->var_cnt++);
1890 static HRESULT visit_function_expression(compiler_ctx_t *ctx, function_expression_t *expr)
1892 expr->func_id = ctx->func->func_cnt++;
1893 ctx->func_tail = ctx->func_tail ? (ctx->func_tail->next = expr) : (ctx->func_head = expr);
1895 return !expr->identifier || expr->event_target || alloc_variable(ctx, expr->identifier)
1896 ? S_OK : E_OUTOFMEMORY;
1899 static HRESULT visit_expression(compiler_ctx_t *ctx, expression_t *expr)
1901 HRESULT hres = S_OK;
1903 switch(expr->type) {
1904 case EXPR_ADD:
1905 case EXPR_AND:
1906 case EXPR_ARRAY:
1907 case EXPR_ASSIGN:
1908 case EXPR_ASSIGNADD:
1909 case EXPR_ASSIGNAND:
1910 case EXPR_ASSIGNSUB:
1911 case EXPR_ASSIGNMUL:
1912 case EXPR_ASSIGNDIV:
1913 case EXPR_ASSIGNMOD:
1914 case EXPR_ASSIGNOR:
1915 case EXPR_ASSIGNLSHIFT:
1916 case EXPR_ASSIGNRSHIFT:
1917 case EXPR_ASSIGNRRSHIFT:
1918 case EXPR_ASSIGNXOR:
1919 case EXPR_BAND:
1920 case EXPR_BOR:
1921 case EXPR_COMMA:
1922 case EXPR_DIV:
1923 case EXPR_EQ:
1924 case EXPR_EQEQ:
1925 case EXPR_GREATER:
1926 case EXPR_GREATEREQ:
1927 case EXPR_IN:
1928 case EXPR_INSTANCEOF:
1929 case EXPR_LESS:
1930 case EXPR_LESSEQ:
1931 case EXPR_LSHIFT:
1932 case EXPR_MOD:
1933 case EXPR_MUL:
1934 case EXPR_NOTEQ:
1935 case EXPR_NOTEQEQ:
1936 case EXPR_OR:
1937 case EXPR_RSHIFT:
1938 case EXPR_RRSHIFT:
1939 case EXPR_SUB:
1940 case EXPR_BXOR: {
1941 binary_expression_t *binary_expr = (binary_expression_t*)expr;
1943 hres = visit_expression(ctx, binary_expr->expression1);
1944 if(FAILED(hres))
1945 return hres;
1947 hres = visit_expression(ctx, binary_expr->expression2);
1948 break;
1950 case EXPR_BITNEG:
1951 case EXPR_DELETE:
1952 case EXPR_LOGNEG:
1953 case EXPR_MINUS:
1954 case EXPR_PLUS:
1955 case EXPR_POSTDEC:
1956 case EXPR_POSTINC:
1957 case EXPR_PREDEC:
1958 case EXPR_PREINC:
1959 case EXPR_TYPEOF:
1960 case EXPR_VOID:
1961 hres = visit_expression(ctx, ((unary_expression_t*)expr)->expression);
1962 break;
1963 case EXPR_IDENT:
1964 case EXPR_LITERAL:
1965 case EXPR_THIS:
1966 break;
1967 case EXPR_ARRAYLIT: {
1968 array_literal_expression_t *array_expr = (array_literal_expression_t*)expr;
1969 array_element_t *iter;
1971 for(iter = array_expr->element_list; iter; iter = iter->next) {
1972 hres = visit_expression(ctx, iter->expr);
1973 if(FAILED(hres))
1974 return hres;
1976 break;
1978 case EXPR_CALL:
1979 case EXPR_NEW: {
1980 call_expression_t *call_expr = (call_expression_t*)expr;
1981 argument_t *arg;
1983 hres = visit_expression(ctx, call_expr->expression);
1984 if(FAILED(hres))
1985 return hres;
1987 for(arg = call_expr->argument_list; arg; arg = arg->next) {
1988 hres = visit_expression(ctx, arg->expr);
1989 if(FAILED(hres))
1990 return hres;
1992 break;
1994 case EXPR_COND: {
1995 conditional_expression_t *cond_expr = (conditional_expression_t*)expr;
1997 hres = visit_expression(ctx, cond_expr->expression);
1998 if(FAILED(hres))
1999 return hres;
2001 hres = visit_expression(ctx, cond_expr->true_expression);
2002 if(FAILED(hres))
2003 return hres;
2005 hres = visit_expression(ctx, cond_expr->false_expression);
2006 break;
2008 case EXPR_FUNC:
2009 hres = visit_function_expression(ctx, (function_expression_t*)expr);
2010 break;
2011 case EXPR_MEMBER:
2012 hres = visit_expression(ctx, ((member_expression_t*)expr)->expression);
2013 break;
2014 case EXPR_PROPVAL: {
2015 property_definition_t *iter;
2016 for(iter = ((property_value_expression_t*)expr)->property_list; iter; iter = iter->next) {
2017 hres = visit_expression(ctx, iter->value);
2018 if(FAILED(hres))
2019 return hres;
2021 break;
2023 DEFAULT_UNREACHABLE;
2026 return hres;
2029 static HRESULT visit_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
2031 variable_declaration_t *iter;
2032 HRESULT hres;
2034 for(iter = list; iter; iter = iter->next) {
2035 if(!alloc_variable(ctx, iter->identifier))
2036 return E_OUTOFMEMORY;
2038 if(iter->expr) {
2039 hres = visit_expression(ctx, iter->expr);
2040 if(FAILED(hres))
2041 return hres;
2045 return S_OK;
2048 static HRESULT visit_statement(compiler_ctx_t*,statement_t*);
2050 static HRESULT visit_block_statement(compiler_ctx_t *ctx, statement_t *iter)
2052 HRESULT hres;
2054 while(iter) {
2055 hres = visit_statement(ctx, iter);
2056 if(FAILED(hres))
2057 return hres;
2059 iter = iter->next;
2062 return S_OK;
2065 static HRESULT visit_statement(compiler_ctx_t *ctx, statement_t *stat)
2067 HRESULT hres = S_OK;
2069 switch(stat->type) {
2070 case STAT_BLOCK:
2071 hres = visit_block_statement(ctx, ((block_statement_t*)stat)->stat_list);
2072 break;
2073 case STAT_BREAK:
2074 case STAT_CONTINUE:
2075 case STAT_EMPTY:
2076 break;
2077 case STAT_EXPR:
2078 case STAT_RETURN:
2079 case STAT_THROW: {
2080 expression_statement_t *expr_stat = (expression_statement_t*)stat;
2081 if(expr_stat->expr)
2082 hres = visit_expression(ctx, expr_stat->expr);
2083 break;
2085 case STAT_FOR: {
2086 for_statement_t *for_stat = (for_statement_t*)stat;
2088 if(for_stat->variable_list)
2089 hres = visit_variable_list(ctx, for_stat->variable_list);
2090 else if(for_stat->begin_expr)
2091 hres = visit_expression(ctx, for_stat->begin_expr);
2092 if(FAILED(hres))
2093 break;
2095 if(for_stat->expr) {
2096 hres = visit_expression(ctx, for_stat->expr);
2097 if(FAILED(hres))
2098 break;
2101 hres = visit_statement(ctx, for_stat->statement);
2102 if(FAILED(hres))
2103 break;
2105 if(for_stat->end_expr)
2106 hres = visit_expression(ctx, for_stat->end_expr);
2107 break;
2109 case STAT_FORIN: {
2110 forin_statement_t *forin_stat = (forin_statement_t*)stat;
2112 if(forin_stat->variable) {
2113 hres = visit_variable_list(ctx, forin_stat->variable);
2114 if(FAILED(hres))
2115 break;
2118 hres = visit_expression(ctx, forin_stat->in_expr);
2119 if(FAILED(hres))
2120 return hres;
2122 if(forin_stat->expr) {
2123 hres = visit_expression(ctx, forin_stat->expr);
2124 if(FAILED(hres))
2125 return hres;
2128 hres = visit_statement(ctx, forin_stat->statement);
2129 break;
2131 case STAT_IF: {
2132 if_statement_t *if_stat = (if_statement_t*)stat;
2134 hres = visit_expression(ctx, if_stat->expr);
2135 if(FAILED(hres))
2136 return hres;
2138 hres = visit_statement(ctx, if_stat->if_stat);
2139 if(FAILED(hres))
2140 return hres;
2142 if(if_stat->else_stat)
2143 hres = visit_statement(ctx, if_stat->else_stat);
2144 break;
2146 case STAT_LABEL:
2147 hres = visit_statement(ctx, ((labelled_statement_t*)stat)->statement);
2148 break;
2149 case STAT_SWITCH: {
2150 switch_statement_t *switch_stat = (switch_statement_t*)stat;
2151 statement_t *stat_iter;
2152 case_clausule_t *iter;
2154 hres = visit_expression(ctx, switch_stat->expr);
2155 if(FAILED(hres))
2156 return hres;
2158 for(iter = switch_stat->case_list; iter; iter = iter->next) {
2159 if(!iter->expr)
2160 continue;
2161 hres = visit_expression(ctx, iter->expr);
2162 if(FAILED(hres))
2163 return hres;
2166 for(iter = switch_stat->case_list; iter; iter = iter->next) {
2167 while(iter->next && iter->next->stat == iter->stat)
2168 iter = iter->next;
2169 for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter);
2170 stat_iter = stat_iter->next) {
2171 hres = visit_statement(ctx, stat_iter);
2172 if(FAILED(hres))
2173 return hres;
2176 break;
2178 case STAT_TRY: {
2179 try_statement_t *try_stat = (try_statement_t*)stat;
2181 hres = visit_statement(ctx, try_stat->try_statement);
2182 if(FAILED(hres))
2183 return hres;
2185 if(try_stat->catch_block) {
2186 hres = visit_statement(ctx, try_stat->catch_block->statement);
2187 if(FAILED(hres))
2188 return hres;
2191 if(try_stat->finally_statement)
2192 hres = visit_statement(ctx, try_stat->finally_statement);
2193 break;
2195 case STAT_VAR:
2196 hres = visit_variable_list(ctx, ((var_statement_t*)stat)->variable_list);
2197 break;
2198 case STAT_WHILE: {
2199 while_statement_t *while_stat = (while_statement_t*)stat;
2201 hres = visit_expression(ctx, while_stat->expr);
2202 if(FAILED(hres))
2203 return hres;
2205 hres = visit_statement(ctx, while_stat->statement);
2206 break;
2208 case STAT_WITH: {
2209 with_statement_t *with_stat = (with_statement_t*)stat;
2211 hres = visit_expression(ctx, with_stat->expr);
2212 if(FAILED(hres))
2213 return hres;
2215 hres = visit_statement(ctx, with_stat->statement);
2216 break;
2218 DEFAULT_UNREACHABLE;
2221 return hres;
2224 static void resolve_labels(compiler_ctx_t *ctx, unsigned off)
2226 instr_t *instr;
2228 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
2229 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->u.arg->uint & LABEL_FLAG)) {
2230 assert((instr->u.arg->uint & ~LABEL_FLAG) < ctx->labels_cnt);
2231 instr->u.arg->uint = ctx->labels[instr->u.arg->uint & ~LABEL_FLAG];
2233 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
2236 ctx->labels_cnt = 0;
2239 void release_bytecode(bytecode_t *code)
2241 unsigned i;
2243 if(--code->ref)
2244 return;
2246 for(i=0; i < code->bstr_cnt; i++)
2247 SysFreeString(code->bstr_pool[i]);
2248 for(i=0; i < code->str_cnt; i++)
2249 jsstr_release(code->str_pool[i]);
2251 if(code->named_item)
2252 release_named_item(code->named_item);
2253 heap_free(code->source);
2254 heap_pool_free(&code->heap);
2255 heap_free(code->bstr_pool);
2256 heap_free(code->str_pool);
2257 heap_free(code->instrs);
2258 heap_free(code);
2261 static HRESULT init_code(compiler_ctx_t *compiler, const WCHAR *source, UINT64 source_context, unsigned start_line)
2263 size_t len = source ? lstrlenW(source) : 0;
2265 if(len > INT32_MAX)
2266 return E_OUTOFMEMORY;
2268 compiler->code = heap_alloc_zero(sizeof(bytecode_t));
2269 if(!compiler->code)
2270 return E_OUTOFMEMORY;
2272 compiler->code->ref = 1;
2273 compiler->code->source_context = source_context;
2274 compiler->code->start_line = start_line;
2275 heap_pool_init(&compiler->code->heap);
2277 compiler->code->source = heap_alloc((len + 1) * sizeof(WCHAR));
2278 if(!compiler->code->source) {
2279 release_bytecode(compiler->code);
2280 return E_OUTOFMEMORY;
2282 if(len)
2283 memcpy(compiler->code->source, source, len * sizeof(WCHAR));
2284 compiler->code->source[len] = 0;
2286 compiler->code->instrs = heap_alloc(64 * sizeof(instr_t));
2287 if(!compiler->code->instrs) {
2288 release_bytecode(compiler->code);
2289 return E_OUTOFMEMORY;
2292 compiler->code_size = 64;
2293 compiler->code_off = 1;
2294 return S_OK;
2297 static HRESULT compile_function(compiler_ctx_t *ctx, source_elements_t *source, function_expression_t *func_expr,
2298 BOOL from_eval, function_code_t *func)
2300 function_expression_t *iter;
2301 function_local_t *local;
2302 unsigned off, i;
2303 HRESULT hres;
2305 TRACE("\n");
2307 func->bytecode = ctx->code;
2308 ctx->func_head = ctx->func_tail = NULL;
2309 ctx->from_eval = from_eval;
2310 ctx->func = func;
2311 ctx->locals_cnt = 0;
2312 wine_rb_init(&ctx->locals, function_local_cmp);
2314 if(func_expr) {
2315 parameter_t *param_iter;
2317 if(func_expr->identifier) {
2318 func->name = compiler_alloc_bstr(ctx, func_expr->identifier);
2319 if(!func->name)
2320 return E_OUTOFMEMORY;
2323 if(func_expr->event_target) {
2324 func->event_target = compiler_alloc_bstr(ctx, func_expr->event_target);
2325 if(!func->event_target)
2326 return E_OUTOFMEMORY;
2329 func->source = func_expr->src_str;
2330 func->source_len = func_expr->src_len;
2332 for(param_iter = func_expr->parameter_list; param_iter; param_iter = param_iter->next)
2333 func->param_cnt++;
2335 func->params = compiler_alloc(ctx->code, func->param_cnt * sizeof(*func->params));
2336 if(!func->params)
2337 return E_OUTOFMEMORY;
2339 for(param_iter = func_expr->parameter_list, i=0; param_iter; param_iter = param_iter->next, i++) {
2340 func->params[i] = compiler_alloc_bstr(ctx, param_iter->identifier);
2341 if(!func->params[i])
2342 return E_OUTOFMEMORY;
2346 for(i = 0; i < func->param_cnt; i++) {
2347 if(!find_local(ctx, func->params[i]) && !alloc_local(ctx, func->params[i], -i-1))
2348 return E_OUTOFMEMORY;
2351 hres = visit_block_statement(ctx, source->statement);
2352 if(FAILED(hres))
2353 return hres;
2355 func->locals = compiler_alloc(ctx->code, ctx->locals_cnt * sizeof(*func->locals));
2356 if(!func->locals)
2357 return E_OUTOFMEMORY;
2358 func->locals_cnt = ctx->locals_cnt;
2360 func->variables = compiler_alloc(ctx->code, func->var_cnt * sizeof(*func->variables));
2361 if(!func->variables)
2362 return E_OUTOFMEMORY;
2364 i = 0;
2365 WINE_RB_FOR_EACH_ENTRY(local, &ctx->locals, function_local_t, entry) {
2366 func->locals[i].name = local->name;
2367 func->locals[i].ref = local->ref;
2368 if(local->ref >= 0) {
2369 func->variables[local->ref].name = local->name;
2370 func->variables[local->ref].func_id = -1;
2372 i++;
2374 assert(i == ctx->locals_cnt);
2376 func->funcs = compiler_alloc(ctx->code, func->func_cnt * sizeof(*func->funcs));
2377 if(!func->funcs)
2378 return E_OUTOFMEMORY;
2379 memset(func->funcs, 0, func->func_cnt * sizeof(*func->funcs));
2381 off = ctx->code_off;
2382 hres = compile_block_statement(ctx, source->statement);
2383 if(FAILED(hres))
2384 return hres;
2386 resolve_labels(ctx, off);
2388 hres = push_instr_uint(ctx, OP_ret, !from_eval);
2389 if(FAILED(hres))
2390 return hres;
2392 if(TRACE_ON(jscript_disas))
2393 dump_code(ctx, off);
2395 func->instr_off = off;
2397 for(iter = ctx->func_head, i=0; iter; iter = iter->next, i++) {
2398 hres = compile_function(ctx, iter->source_elements, iter, FALSE, func->funcs+i);
2399 if(FAILED(hres))
2400 return hres;
2402 TRACE("[%d] func %s\n", i, debugstr_w(func->funcs[i].name));
2403 if(func->funcs[i].name && !func->funcs[i].event_target) {
2404 local_ref_t *local_ref = lookup_local(func, func->funcs[i].name);
2405 func->funcs[i].local_ref = local_ref->ref;
2406 TRACE("found ref %s %d for %s\n", debugstr_w(local_ref->name), local_ref->ref, debugstr_w(func->funcs[i].name));
2407 if(local_ref->ref >= 0)
2408 func->variables[local_ref->ref].func_id = i;
2412 assert(i == func->func_cnt);
2414 return S_OK;
2417 static HRESULT parse_arguments(compiler_ctx_t *ctx, const WCHAR *args, BSTR *arg_array, unsigned *args_size)
2419 const WCHAR *ptr = args, *ptr2;
2420 unsigned arg_cnt = 0;
2422 while(iswspace(*ptr))
2423 ptr++;
2424 if(!*ptr) {
2425 if(args_size)
2426 *args_size = 0;
2427 return S_OK;
2430 while(1) {
2431 if(!iswalpha(*ptr) && *ptr != '_') {
2432 FIXME("expected alpha or '_': %s\n", debugstr_w(ptr));
2433 return E_FAIL;
2436 ptr2 = ptr;
2437 while(iswalnum(*ptr) || *ptr == '_')
2438 ptr++;
2440 if(*ptr && *ptr != ',' && !iswspace(*ptr)) {
2441 FIXME("unexpected har %s\n", debugstr_w(ptr));
2442 return E_FAIL;
2445 if(arg_array) {
2446 arg_array[arg_cnt] = compiler_alloc_bstr_len(ctx, ptr2, ptr-ptr2);
2447 if(!arg_array[arg_cnt])
2448 return E_OUTOFMEMORY;
2450 arg_cnt++;
2452 while(iswspace(*ptr))
2453 ptr++;
2454 if(!*ptr)
2455 break;
2456 if(*ptr != ',') {
2457 FIXME("expected ',': %s\n", debugstr_w(ptr));
2458 return E_FAIL;
2461 ptr++;
2462 while(iswspace(*ptr))
2463 ptr++;
2466 if(args_size)
2467 *args_size = arg_cnt;
2468 return S_OK;
2471 static HRESULT compile_arguments(compiler_ctx_t *ctx, const WCHAR *args)
2473 HRESULT hres;
2475 hres = parse_arguments(ctx, args, NULL, &ctx->code->global_code.param_cnt);
2476 if(FAILED(hres))
2477 return hres;
2479 ctx->code->global_code.params = compiler_alloc(ctx->code,
2480 ctx->code->global_code.param_cnt * sizeof(*ctx->code->global_code.params));
2481 if(!ctx->code->global_code.params)
2482 return E_OUTOFMEMORY;
2484 return parse_arguments(ctx, args, ctx->code->global_code.params, NULL);
2487 HRESULT compile_script(script_ctx_t *ctx, const WCHAR *code, UINT64 source_context, unsigned start_line,
2488 const WCHAR *args, const WCHAR *delimiter, BOOL from_eval, BOOL use_decode,
2489 named_item_t *named_item, bytecode_t **ret)
2491 compiler_ctx_t compiler = {0};
2492 HRESULT hres;
2494 hres = init_code(&compiler, code, source_context, start_line);
2495 if(FAILED(hres))
2496 return hres;
2498 if(args) {
2499 hres = compile_arguments(&compiler, args);
2500 if(FAILED(hres))
2501 return hres;
2504 if(use_decode) {
2505 hres = decode_source(compiler.code->source);
2506 if(FAILED(hres)) {
2507 WARN("Decoding failed\n");
2508 return hres;
2512 hres = script_parse(ctx, &compiler, compiler.code, delimiter, from_eval, &compiler.parser);
2513 if(FAILED(hres)) {
2514 release_bytecode(compiler.code);
2515 return hres;
2518 heap_pool_init(&compiler.heap);
2519 hres = compile_function(&compiler, compiler.parser->source, NULL, from_eval, &compiler.code->global_code);
2520 heap_pool_free(&compiler.heap);
2521 parser_release(compiler.parser);
2522 if(FAILED(hres)) {
2523 if(hres != DISP_E_EXCEPTION)
2524 throw_error(ctx, hres, NULL);
2525 set_error_location(ctx->ei, compiler.code, compiler.loc, IDS_COMPILATION_ERROR, NULL);
2526 release_bytecode(compiler.code);
2527 return DISP_E_EXCEPTION;
2530 if(named_item) {
2531 compiler.code->named_item = named_item;
2532 named_item->ref++;
2535 *ret = compiler.code;
2536 return S_OK;