wow64: Add thunks for the I/O completion syscalls.
[wine.git] / dlls / jscript / compile.c
blob4672a8c31f15da1a4689a76e40399488c1a9679c
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 unsigned int scope_index;
43 BOOL block_scope;
44 BOOL scope_has_functions;
45 struct _statement_ctx_t *next;
46 } statement_ctx_t;
48 typedef struct {
49 struct wine_rb_entry entry;
50 BSTR name;
51 int ref;
52 } function_local_t;
54 typedef struct _compiler_ctx_t {
55 parser_ctx_t *parser;
56 bytecode_t *code;
58 BOOL from_eval;
60 unsigned code_off;
61 unsigned code_size;
63 unsigned *labels;
64 unsigned labels_size;
65 unsigned labels_cnt;
67 struct
69 struct wine_rb_tree locals;
70 unsigned int locals_cnt;
71 unsigned int *ref_index;
73 *local_scopes;
74 unsigned local_scope_count;
75 unsigned local_scope_size;
77 statement_ctx_t *stat_ctx;
78 function_code_t *func;
80 unsigned loc;
82 function_expression_t *func_head;
83 function_expression_t *func_tail;
84 function_expression_t *current_function_expr;
86 heap_pool_t heap;
87 } compiler_ctx_t;
89 static const struct {
90 const char *op_str;
91 instr_arg_type_t arg1_type;
92 instr_arg_type_t arg2_type;
93 } instr_info[] = {
94 #define X(n,a,b,c) {#n,b,c},
95 OP_LIST
96 #undef X
99 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
101 switch(type) {
102 case ARG_STR:
103 TRACE_(jscript_disas)("\t%s", debugstr_jsstr(arg->str));
104 break;
105 case ARG_BSTR:
106 TRACE_(jscript_disas)("\t%s", debugstr_wn(arg->bstr, SysStringLen(arg->bstr)));
107 break;
108 case ARG_INT:
109 TRACE_(jscript_disas)("\t%d", arg->uint);
110 break;
111 case ARG_UINT:
112 case ARG_ADDR:
113 TRACE_(jscript_disas)("\t%u", arg->uint);
114 break;
115 case ARG_FUNC:
116 case ARG_NONE:
117 break;
118 DEFAULT_UNREACHABLE;
122 static void dump_code(compiler_ctx_t *ctx, unsigned off)
124 instr_t *instr;
126 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
127 TRACE_(jscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
128 if(instr_info[instr->op].arg1_type == ARG_DBL) {
129 TRACE_(jscript_disas)("\t%lf", instr->u.dbl);
130 }else {
131 dump_instr_arg(instr_info[instr->op].arg1_type, instr->u.arg);
132 dump_instr_arg(instr_info[instr->op].arg2_type, instr->u.arg+1);
134 TRACE_(jscript_disas)("\n");
138 static HRESULT compile_expression(compiler_ctx_t*,expression_t*,BOOL);
139 static HRESULT compile_statement(compiler_ctx_t*,statement_ctx_t*,statement_t*);
141 static int function_local_cmp(const void *key, const struct wine_rb_entry *entry)
143 function_local_t *local = WINE_RB_ENTRY_VALUE(entry, function_local_t, entry);
144 return wcscmp(key, local->name);
147 static BOOL alloc_local_scope(compiler_ctx_t *ctx, unsigned int *scope_index)
149 unsigned int scope, new_size;
150 void *new_alloc;
152 scope = ctx->local_scope_count++;
153 if (scope == ctx->local_scope_size)
155 new_size = max(1, ctx->local_scope_size * 2);
156 if (!(new_alloc = heap_realloc(ctx->local_scopes, new_size * sizeof(*ctx->local_scopes))))
157 return FALSE;
158 ctx->local_scopes = new_alloc;
159 ctx->local_scope_size = new_size;
162 ctx->local_scopes[scope].locals_cnt = 0;
163 ctx->local_scopes[scope].ref_index = scope_index;
164 wine_rb_init(&ctx->local_scopes[scope].locals, function_local_cmp);
165 *scope_index = scope;
167 return TRUE;
170 static void remove_local_scope(compiler_ctx_t *ctx, unsigned int scope_index)
172 unsigned int i;
174 assert(scope_index < ctx->local_scope_count);
175 --ctx->local_scope_count;
176 assert(scope_index == *ctx->local_scopes[scope_index].ref_index);
177 *ctx->local_scopes[scope_index].ref_index = 0;
178 memmove(&ctx->local_scopes[scope_index], &ctx->local_scopes[scope_index + 1],
179 sizeof(*ctx->local_scopes) * (ctx->local_scope_count - scope_index));
180 for (i = scope_index; i < ctx->local_scope_count; ++i)
181 --*ctx->local_scopes[i].ref_index;
184 static inline void *compiler_alloc(bytecode_t *code, size_t size)
186 return heap_pool_alloc(&code->heap, size);
189 jsstr_t *compiler_alloc_string_len(compiler_ctx_t *ctx, const WCHAR *str, unsigned len)
191 jsstr_t *new_str;
193 if(!ctx->code->str_pool_size) {
194 ctx->code->str_pool = heap_alloc(8 * sizeof(jsstr_t*));
195 if(!ctx->code->str_pool)
196 return NULL;
197 ctx->code->str_pool_size = 8;
198 }else if(ctx->code->str_pool_size == ctx->code->str_cnt) {
199 jsstr_t **new_pool;
201 new_pool = heap_realloc(ctx->code->str_pool, ctx->code->str_pool_size*2*sizeof(jsstr_t*));
202 if(!new_pool)
203 return NULL;
205 ctx->code->str_pool = new_pool;
206 ctx->code->str_pool_size *= 2;
209 new_str = jsstr_alloc_len(str, len);
210 if(!new_str)
211 return NULL;
213 ctx->code->str_pool[ctx->code->str_cnt++] = new_str;
214 return new_str;
217 static jsstr_t *compiler_alloc_string(compiler_ctx_t *ctx, const WCHAR *str)
219 return compiler_alloc_string_len(ctx, str, lstrlenW(str));
222 static BOOL ensure_bstr_slot(compiler_ctx_t *ctx)
224 if(!ctx->code->bstr_pool_size) {
225 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
226 if(!ctx->code->bstr_pool)
227 return FALSE;
228 ctx->code->bstr_pool_size = 8;
229 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
230 BSTR *new_pool;
232 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
233 if(!new_pool)
234 return FALSE;
236 ctx->code->bstr_pool = new_pool;
237 ctx->code->bstr_pool_size *= 2;
240 return TRUE;
243 static BSTR compiler_alloc_bstr(compiler_ctx_t *ctx, const WCHAR *str)
245 if(!ensure_bstr_slot(ctx))
246 return NULL;
248 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
249 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
250 return NULL;
252 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
255 static BSTR compiler_alloc_bstr_len(compiler_ctx_t *ctx, const WCHAR *str, size_t len)
257 if(!ensure_bstr_slot(ctx))
258 return NULL;
260 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocStringLen(str, len);
261 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
262 return NULL;
264 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
267 void set_compiler_loc(compiler_ctx_t *ctx, unsigned loc)
269 ctx->loc = loc;
272 static unsigned push_instr(compiler_ctx_t *ctx, jsop_t op)
274 assert(ctx->code_size >= ctx->code_off);
276 if(ctx->code_size == ctx->code_off) {
277 instr_t *new_instrs;
279 new_instrs = heap_realloc(ctx->code->instrs, ctx->code_size*2*sizeof(instr_t));
280 if(!new_instrs)
281 return 0;
283 ctx->code->instrs = new_instrs;
284 ctx->code_size *= 2;
287 ctx->code->instrs[ctx->code_off].op = op;
288 ctx->code->instrs[ctx->code_off].loc = ctx->loc;
289 return ctx->code_off++;
292 static inline instr_t *instr_ptr(compiler_ctx_t *ctx, unsigned off)
294 assert(off < ctx->code_off);
295 return ctx->code->instrs + off;
298 static HRESULT push_instr_int(compiler_ctx_t *ctx, jsop_t op, LONG arg)
300 unsigned instr;
302 instr = push_instr(ctx, op);
303 if(!instr)
304 return E_OUTOFMEMORY;
306 instr_ptr(ctx, instr)->u.arg->lng = arg;
307 return S_OK;
310 static HRESULT push_instr_str(compiler_ctx_t *ctx, jsop_t op, jsstr_t *str)
312 unsigned instr;
314 instr = push_instr(ctx, op);
315 if(!instr)
316 return E_OUTOFMEMORY;
318 instr_ptr(ctx, instr)->u.arg->str = str;
319 return S_OK;
322 static HRESULT push_instr_str_uint(compiler_ctx_t *ctx, jsop_t op, jsstr_t *str, unsigned arg2)
324 unsigned instr;
326 instr = push_instr(ctx, op);
327 if(!instr)
328 return E_OUTOFMEMORY;
330 instr_ptr(ctx, instr)->u.arg[0].str = str;
331 instr_ptr(ctx, instr)->u.arg[1].uint = arg2;
332 return S_OK;
335 static HRESULT push_instr_bstr(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
337 unsigned instr;
338 WCHAR *str;
340 str = compiler_alloc_bstr(ctx, arg);
341 if(!str)
342 return E_OUTOFMEMORY;
344 instr = push_instr(ctx, op);
345 if(!instr)
346 return E_OUTOFMEMORY;
348 instr_ptr(ctx, instr)->u.arg->bstr = str;
349 return S_OK;
352 static HRESULT push_instr_bstr_uint(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg1, unsigned arg2)
354 unsigned instr;
355 WCHAR *str;
357 str = compiler_alloc_bstr(ctx, arg1);
358 if(!str)
359 return E_OUTOFMEMORY;
361 instr = push_instr(ctx, op);
362 if(!instr)
363 return E_OUTOFMEMORY;
365 instr_ptr(ctx, instr)->u.arg[0].bstr = str;
366 instr_ptr(ctx, instr)->u.arg[1].uint = arg2;
367 return S_OK;
370 static HRESULT push_instr_uint_str(compiler_ctx_t *ctx, jsop_t op, unsigned arg1, const WCHAR *arg2)
372 unsigned instr;
373 jsstr_t *str;
375 str = compiler_alloc_string(ctx, arg2);
376 if(!str)
377 return E_OUTOFMEMORY;
379 instr = push_instr(ctx, op);
380 if(!instr)
381 return E_OUTOFMEMORY;
383 instr_ptr(ctx, instr)->u.arg[0].uint = arg1;
384 instr_ptr(ctx, instr)->u.arg[1].str = str;
385 return S_OK;
388 static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
390 unsigned instr;
392 instr = push_instr(ctx, op);
393 if(!instr)
394 return E_OUTOFMEMORY;
396 instr_ptr(ctx, instr)->u.dbl = arg;
397 return S_OK;
400 static inline void set_arg_uint(compiler_ctx_t *ctx, unsigned instr, unsigned arg)
402 instr_ptr(ctx, instr)->u.arg->uint = arg;
405 static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
407 unsigned instr;
409 instr = push_instr(ctx, op);
410 if(!instr)
411 return E_OUTOFMEMORY;
413 set_arg_uint(ctx, instr, arg);
414 return S_OK;
417 static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
419 HRESULT hres;
421 hres = compile_expression(ctx, expr->expression1, TRUE);
422 if(FAILED(hres))
423 return hres;
425 hres = compile_expression(ctx, expr->expression2, TRUE);
426 if(FAILED(hres))
427 return hres;
429 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
432 static HRESULT compile_unary_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op)
434 HRESULT hres;
436 hres = compile_expression(ctx, expr->expression, TRUE);
437 if(FAILED(hres))
438 return hres;
440 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
443 /* ECMA-262 3rd Edition 11.2.1 */
444 static HRESULT compile_member_expression(compiler_ctx_t *ctx, member_expression_t *expr)
446 HRESULT hres;
448 hres = compile_expression(ctx, expr->expression, TRUE);
449 if(FAILED(hres))
450 return hres;
452 return push_instr_bstr(ctx, OP_member, expr->identifier);
455 #define LABEL_FLAG 0x80000000
457 static unsigned alloc_label(compiler_ctx_t *ctx)
459 if(!ctx->labels_size) {
460 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
461 if(!ctx->labels)
462 return 0;
463 ctx->labels_size = 8;
464 }else if(ctx->labels_size == ctx->labels_cnt) {
465 unsigned *new_labels;
467 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
468 if(!new_labels)
469 return 0;
471 ctx->labels = new_labels;
472 ctx->labels_size *= 2;
475 return ctx->labels_cnt++ | LABEL_FLAG;
478 static void label_set_addr(compiler_ctx_t *ctx, unsigned label)
480 assert(label & LABEL_FLAG);
481 ctx->labels[label & ~LABEL_FLAG] = ctx->code_off;
484 static inline BOOL is_memberid_expr(expression_type_t type)
486 return type == EXPR_IDENT || type == EXPR_MEMBER || type == EXPR_ARRAY;
489 static BOOL bind_local(compiler_ctx_t *ctx, const WCHAR *identifier, int *ret_ref)
491 statement_ctx_t *iter;
492 local_ref_t *ref;
494 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
495 if(iter->using_scope)
497 if (!iter->block_scope)
498 return FALSE;
500 if ((ref = lookup_local(ctx->func, identifier, iter->scope_index)))
502 *ret_ref = ref->ref;
503 return TRUE;
508 ref = lookup_local(ctx->func, identifier, 0);
509 if(!ref)
510 return FALSE;
512 *ret_ref = ref->ref;
513 return TRUE;
516 static HRESULT emit_identifier_ref(compiler_ctx_t *ctx, const WCHAR *identifier, unsigned flags)
518 int local_ref;
519 if(bind_local(ctx, identifier, &local_ref))
520 return push_instr_int(ctx, OP_local_ref, local_ref);
521 return push_instr_bstr_uint(ctx, OP_identid, identifier, flags);
524 static HRESULT emit_identifier(compiler_ctx_t *ctx, const WCHAR *identifier)
526 int local_ref;
527 if(bind_local(ctx, identifier, &local_ref))
528 return push_instr_int(ctx, OP_local, local_ref);
529 return push_instr_bstr(ctx, OP_ident, identifier);
532 static HRESULT emit_member_expression(compiler_ctx_t *ctx, expression_t *expr)
534 HRESULT hres;
536 if(expr->type == EXPR_ARRAY) {
537 binary_expression_t *array_expr = (binary_expression_t*)expr;
539 hres = compile_expression(ctx, array_expr->expression1, TRUE);
540 if(FAILED(hres))
541 return hres;
543 hres = compile_expression(ctx, array_expr->expression2, TRUE);
544 if(FAILED(hres))
545 return hres;
547 if(!push_instr(ctx, OP_to_string))
548 return E_OUTOFMEMORY;
549 }else {
550 member_expression_t *member_expr = (member_expression_t*)expr;
551 jsstr_t *jsstr;
553 assert(expr->type == EXPR_MEMBER);
555 hres = compile_expression(ctx, member_expr->expression, TRUE);
556 if(FAILED(hres))
557 return hres;
559 jsstr = compiler_alloc_string(ctx, member_expr->identifier);
560 if(!jsstr)
561 return E_OUTOFMEMORY;
563 hres = push_instr_str(ctx, OP_str, jsstr);
564 if(FAILED(hres))
565 return hres;
568 return S_OK;
571 static void push_compiler_statement_ctx(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx)
573 if (stat_ctx)
575 stat_ctx->next = ctx->stat_ctx;
576 ctx->stat_ctx = stat_ctx;
580 static void pop_compiler_statement_ctx(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx)
582 if (stat_ctx)
584 assert(ctx->stat_ctx == stat_ctx);
585 ctx->stat_ctx = stat_ctx->next;
589 static HRESULT compile_memberid_expression(compiler_ctx_t *ctx, expression_t *expr, unsigned flags)
591 HRESULT hres;
593 if(expr->type == EXPR_IDENT) {
594 identifier_expression_t *ident_expr = (identifier_expression_t*)expr;
595 return emit_identifier_ref(ctx, ident_expr->identifier, flags);
598 hres = emit_member_expression(ctx, expr);
599 if(FAILED(hres))
600 return hres;
602 return push_instr_uint(ctx, OP_memberid, flags);
605 static HRESULT compile_increment_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op, int n)
607 HRESULT hres;
609 if(!is_memberid_expr(expr->expression->type)) {
610 hres = compile_expression(ctx, expr->expression, TRUE);
611 if(FAILED(hres))
612 return hres;
614 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
617 hres = compile_memberid_expression(ctx, expr->expression, fdexNameEnsure);
618 if(FAILED(hres))
619 return hres;
621 return push_instr_int(ctx, op, n);
624 /* ECMA-262 3rd Edition 11.14 */
625 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr, BOOL emit_ret)
627 HRESULT hres;
629 hres = compile_expression(ctx, expr->expression1, FALSE);
630 if(FAILED(hres))
631 return hres;
633 return compile_expression(ctx, expr->expression2, emit_ret);
636 /* ECMA-262 3rd Edition 11.11 */
637 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
639 unsigned instr;
640 HRESULT hres;
642 hres = compile_expression(ctx, expr->expression1, TRUE);
643 if(FAILED(hres))
644 return hres;
646 instr = push_instr(ctx, op);
647 if(!instr)
648 return E_OUTOFMEMORY;
650 hres = compile_expression(ctx, expr->expression2, TRUE);
651 if(FAILED(hres))
652 return hres;
654 set_arg_uint(ctx, instr, ctx->code_off);
655 return S_OK;
658 /* ECMA-262 3rd Edition 11.12 */
659 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
661 unsigned jmp_false, jmp_end;
662 HRESULT hres;
664 hres = compile_expression(ctx, expr->expression, TRUE);
665 if(FAILED(hres))
666 return hres;
668 jmp_false = push_instr(ctx, OP_cnd_z);
669 if(!jmp_false)
670 return E_OUTOFMEMORY;
672 hres = compile_expression(ctx, expr->true_expression, TRUE);
673 if(FAILED(hres))
674 return hres;
676 jmp_end = push_instr(ctx, OP_jmp);
677 if(!jmp_end)
678 return E_OUTOFMEMORY;
680 set_arg_uint(ctx, jmp_false, ctx->code_off);
681 hres = push_instr_uint(ctx, OP_pop, 1);
682 if(FAILED(hres))
683 return hres;
685 hres = compile_expression(ctx, expr->false_expression, TRUE);
686 if(FAILED(hres))
687 return hres;
689 set_arg_uint(ctx, jmp_end, ctx->code_off);
690 return S_OK;
693 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
695 unsigned arg_cnt = 0;
696 argument_t *arg;
697 HRESULT hres;
699 hres = compile_expression(ctx, expr->expression, TRUE);
700 if(FAILED(hres))
701 return hres;
703 for(arg = expr->argument_list; arg; arg = arg->next) {
704 hres = compile_expression(ctx, arg->expr, TRUE);
705 if(FAILED(hres))
706 return hres;
707 arg_cnt++;
710 hres = push_instr_uint(ctx, OP_new, arg_cnt);
711 if(FAILED(hres))
712 return hres;
714 hres = push_instr_uint(ctx, OP_pop, arg_cnt+1);
715 if(FAILED(hres))
716 return hres;
718 return push_instr(ctx, OP_push_acc) ? S_OK : E_OUTOFMEMORY;
721 static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL emit_ret)
723 unsigned arg_cnt = 0, extra_args;
724 argument_t *arg;
725 unsigned instr;
726 jsop_t op;
727 HRESULT hres;
729 if(is_memberid_expr(expr->expression->type)) {
730 op = OP_call_member;
731 extra_args = 2;
732 hres = compile_memberid_expression(ctx, expr->expression, 0);
733 }else {
734 op = OP_call;
735 extra_args = 1;
736 hres = compile_expression(ctx, expr->expression, TRUE);
739 if(FAILED(hres))
740 return hres;
742 for(arg = expr->argument_list; arg; arg = arg->next) {
743 hres = compile_expression(ctx, arg->expr, TRUE);
744 if(FAILED(hres))
745 return hres;
746 arg_cnt++;
749 instr = push_instr(ctx, op);
750 if(!instr)
751 return E_OUTOFMEMORY;
753 instr_ptr(ctx, instr)->u.arg[0].uint = arg_cnt;
754 instr_ptr(ctx, instr)->u.arg[1].lng = emit_ret;
756 hres = push_instr_uint(ctx, OP_pop, arg_cnt + extra_args);
757 if(FAILED(hres))
758 return hres;
760 return !emit_ret || push_instr(ctx, OP_push_acc) ? S_OK : E_OUTOFMEMORY;
763 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
765 HRESULT hres;
767 switch(expr->expression->type) {
768 case EXPR_ARRAY: {
769 binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
771 hres = compile_expression(ctx, array_expr->expression1, TRUE);
772 if(FAILED(hres))
773 return hres;
775 hres = compile_expression(ctx, array_expr->expression2, TRUE);
776 if(FAILED(hres))
777 return hres;
779 if(!push_instr(ctx, OP_delete))
780 return E_OUTOFMEMORY;
781 break;
783 case EXPR_MEMBER: {
784 member_expression_t *member_expr = (member_expression_t*)expr->expression;
785 jsstr_t *jsstr;
787 hres = compile_expression(ctx, member_expr->expression, TRUE);
788 if(FAILED(hres))
789 return hres;
791 /* FIXME: Potential optimization */
792 jsstr = compiler_alloc_string(ctx, member_expr->identifier);
793 if(!jsstr)
794 return E_OUTOFMEMORY;
796 hres = push_instr_str(ctx, OP_str, jsstr);
797 if(FAILED(hres))
798 return hres;
800 if(!push_instr(ctx, OP_delete))
801 return E_OUTOFMEMORY;
802 break;
804 case EXPR_IDENT:
805 return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
806 default: {
807 WARN("invalid delete, unimplemented exception message\n");
809 hres = compile_expression(ctx, expr->expression, TRUE);
810 if(FAILED(hres))
811 return hres;
813 return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, L"FIXME");
817 return S_OK;
820 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
822 jsop_t assign_op = OP_throw_ref;
823 unsigned arg_cnt = 0;
824 HRESULT hres;
826 if(expr->expression1->type == EXPR_CALL) {
827 call_expression_t *call_expr = (call_expression_t*)expr->expression1;
828 argument_t *arg;
830 if(is_memberid_expr(call_expr->expression->type) && call_expr->argument_list) {
831 hres = compile_memberid_expression(ctx, call_expr->expression, fdexNameEnsure);
832 if(FAILED(hres))
833 return hres;
835 for(arg = call_expr->argument_list; arg; arg = arg->next) {
836 hres = compile_expression(ctx, arg->expr, TRUE);
837 if(FAILED(hres))
838 return hres;
839 arg_cnt++;
842 if(op != OP_LAST) {
843 unsigned instr;
845 /* We need to call the functions twice: to get the value and to set it.
846 * JavaScript interpreted functions may to modify value on the stack,
847 * but assignment calls are allowed only on external functions, so we
848 * may reuse the stack here. */
849 instr = push_instr(ctx, OP_call_member);
850 if(!instr)
851 return E_OUTOFMEMORY;
852 instr_ptr(ctx, instr)->u.arg[0].uint = arg_cnt;
853 instr_ptr(ctx, instr)->u.arg[1].lng = 1;
855 if(!push_instr(ctx, OP_push_acc))
856 return E_OUTOFMEMORY;
858 assign_op = OP_assign_call;
860 }else if(is_memberid_expr(expr->expression1->type)) {
861 if(op != OP_LAST || expr->expression1->type == EXPR_IDENT) {
862 hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
863 if(FAILED(hres))
864 return hres;
865 if(op != OP_LAST && !push_instr(ctx, OP_refval))
866 return E_OUTOFMEMORY;
867 assign_op = OP_assign;
868 }else {
869 hres = emit_member_expression(ctx, expr->expression1);
870 if(FAILED(hres))
871 return hres;
872 assign_op = OP_set_member;
876 if(assign_op == OP_throw_ref) {
877 /* Illegal assignment: evaluate and throw */
878 hres = compile_expression(ctx, expr->expression1, TRUE);
879 if(FAILED(hres))
880 return hres;
881 arg_cnt = JS_E_ILLEGAL_ASSIGN;
884 hres = compile_expression(ctx, expr->expression2, TRUE);
885 if(FAILED(hres))
886 return hres;
888 if(op != OP_LAST && !push_instr(ctx, op))
889 return E_OUTOFMEMORY;
891 return push_instr_uint(ctx, assign_op, arg_cnt);
894 static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
896 jsop_t op;
897 HRESULT hres;
899 if(is_memberid_expr(expr->expression->type)) {
900 if(expr->expression->type == EXPR_IDENT)
901 return push_instr_bstr(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
903 op = OP_typeofid;
904 hres = compile_memberid_expression(ctx, expr->expression, 0);
905 }else {
906 op = OP_typeof;
907 hres = compile_expression(ctx, expr->expression, TRUE);
909 if(FAILED(hres))
910 return hres;
912 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
915 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
917 switch(literal->type) {
918 case LT_BOOL:
919 return push_instr_int(ctx, OP_bool, literal->u.bval);
920 case LT_DOUBLE:
921 return push_instr_double(ctx, OP_double, literal->u.dval);
922 case LT_NULL:
923 return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
924 case LT_STRING:
925 return push_instr_str(ctx, OP_str, literal->u.str);
926 case LT_REGEXP:
927 return push_instr_str_uint(ctx, OP_regexp, literal->u.regexp.str, literal->u.regexp.flags);
928 DEFAULT_UNREACHABLE;
930 return E_FAIL;
933 static HRESULT literal_as_string(compiler_ctx_t *ctx, literal_t *literal, jsstr_t **str)
935 switch(literal->type) {
936 case LT_STRING:
937 *str = literal->u.str;
938 break;
939 case LT_DOUBLE:
940 return double_to_string(literal->u.dval, str);
941 DEFAULT_UNREACHABLE;
944 return *str ? S_OK : E_OUTOFMEMORY;
947 static HRESULT compile_array_literal(compiler_ctx_t *ctx, array_literal_expression_t *expr)
949 unsigned length = 0;
950 array_element_t *iter;
951 unsigned array_instr;
952 HRESULT hres;
954 array_instr = push_instr(ctx, OP_carray);
956 for(iter = expr->element_list; iter; iter = iter->next) {
957 length += iter->elision;
959 hres = compile_expression(ctx, iter->expr, TRUE);
960 if(FAILED(hres))
961 return hres;
963 hres = push_instr_uint(ctx, OP_carray_set, length);
964 if(FAILED(hres))
965 return hres;
967 length++;
970 instr_ptr(ctx, array_instr)->u.arg[0].uint = length + expr->length;
971 return S_OK;
974 static HRESULT compile_object_literal(compiler_ctx_t *ctx, property_value_expression_t *expr)
976 property_definition_t *iter;
977 jsstr_t *name;
978 HRESULT hres;
980 if(!push_instr(ctx, OP_new_obj))
981 return E_OUTOFMEMORY;
983 for(iter = expr->property_list; iter; iter = iter->next) {
984 hres = literal_as_string(ctx, iter->name, &name);
985 if(FAILED(hres))
986 return hres;
988 hres = compile_expression(ctx, iter->value, TRUE);
989 if(FAILED(hres))
990 return hres;
992 hres = push_instr_str_uint(ctx, OP_obj_prop, name, iter->type);
993 if(FAILED(hres))
994 return hres;
997 return S_OK;
1000 static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr, BOOL emit_ret)
1002 statement_ctx_t *stat_ctx;
1004 assert(ctx->current_function_expr);
1006 for(stat_ctx = ctx->stat_ctx; stat_ctx; stat_ctx = stat_ctx->next)
1008 if(stat_ctx->block_scope)
1009 break;
1011 ctx->current_function_expr->scope_index = stat_ctx ? stat_ctx->scope_index : 0;
1012 ctx->current_function_expr = ctx->current_function_expr->next;
1014 return emit_ret ? push_instr_uint(ctx, OP_func, expr->func_id) : S_OK;
1017 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr, BOOL emit_ret)
1019 HRESULT hres;
1021 switch(expr->type) {
1022 case EXPR_ADD:
1023 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
1024 break;
1025 case EXPR_AND:
1026 hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_z);
1027 break;
1028 case EXPR_ARRAY:
1029 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
1030 break;
1031 case EXPR_ARRAYLIT:
1032 hres = compile_array_literal(ctx, (array_literal_expression_t*)expr);
1033 break;
1034 case EXPR_ASSIGN:
1035 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
1036 break;
1037 case EXPR_ASSIGNADD:
1038 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
1039 break;
1040 case EXPR_ASSIGNAND:
1041 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_and);
1042 break;
1043 case EXPR_ASSIGNSUB:
1044 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
1045 break;
1046 case EXPR_ASSIGNMUL:
1047 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
1048 break;
1049 case EXPR_ASSIGNDIV:
1050 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
1051 break;
1052 case EXPR_ASSIGNMOD:
1053 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
1054 break;
1055 case EXPR_ASSIGNOR:
1056 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
1057 break;
1058 case EXPR_ASSIGNLSHIFT:
1059 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_lshift);
1060 break;
1061 case EXPR_ASSIGNRSHIFT:
1062 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift);
1063 break;
1064 case EXPR_ASSIGNRRSHIFT:
1065 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
1066 break;
1067 case EXPR_ASSIGNXOR:
1068 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
1069 break;
1070 case EXPR_BAND:
1071 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
1072 break;
1073 case EXPR_BITNEG:
1074 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
1075 break;
1076 case EXPR_BOR:
1077 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
1078 break;
1079 case EXPR_CALL:
1080 return compile_call_expression(ctx, (call_expression_t*)expr, emit_ret);
1081 case EXPR_COMMA:
1082 return compile_comma_expression(ctx, (binary_expression_t*)expr, emit_ret);
1083 case EXPR_COND:
1084 hres = compile_conditional_expression(ctx, (conditional_expression_t*)expr);
1085 break;
1086 case EXPR_DELETE:
1087 hres = compile_delete_expression(ctx, (unary_expression_t*)expr);
1088 break;
1089 case EXPR_DIV:
1090 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
1091 break;
1092 case EXPR_EQ:
1093 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
1094 break;
1095 case EXPR_EQEQ:
1096 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
1097 break;
1098 case EXPR_FUNC:
1099 return compile_function_expression(ctx, (function_expression_t*)expr, emit_ret);
1100 case EXPR_GREATER:
1101 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
1102 break;
1103 case EXPR_GREATEREQ:
1104 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
1105 break;
1106 case EXPR_IDENT:
1107 hres = emit_identifier(ctx, ((identifier_expression_t*)expr)->identifier);
1108 break;
1109 case EXPR_IN:
1110 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
1111 break;
1112 case EXPR_INSTANCEOF:
1113 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
1114 break;
1115 case EXPR_LESS:
1116 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
1117 break;
1118 case EXPR_LESSEQ:
1119 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
1120 break;
1121 case EXPR_LITERAL:
1122 hres = compile_literal(ctx, ((literal_expression_t*)expr)->literal);
1123 break;
1124 case EXPR_LOGNEG:
1125 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
1126 break;
1127 case EXPR_LSHIFT:
1128 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
1129 break;
1130 case EXPR_MEMBER:
1131 hres = compile_member_expression(ctx, (member_expression_t*)expr);
1132 break;
1133 case EXPR_MINUS:
1134 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
1135 break;
1136 case EXPR_MOD:
1137 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
1138 break;
1139 case EXPR_MUL:
1140 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
1141 break;
1142 case EXPR_NEW:
1143 hres = compile_new_expression(ctx, (call_expression_t*)expr);
1144 break;
1145 case EXPR_NOTEQ:
1146 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
1147 break;
1148 case EXPR_NOTEQEQ:
1149 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
1150 break;
1151 case EXPR_OR:
1152 hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_nz);
1153 break;
1154 case EXPR_PLUS:
1155 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
1156 break;
1157 case EXPR_POSTDEC:
1158 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
1159 break;
1160 case EXPR_POSTINC:
1161 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
1162 break;
1163 case EXPR_PREDEC:
1164 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
1165 break;
1166 case EXPR_PREINC:
1167 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
1168 break;
1169 case EXPR_PROPVAL:
1170 hres = compile_object_literal(ctx, (property_value_expression_t*)expr);
1171 break;
1172 case EXPR_RSHIFT:
1173 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
1174 break;
1175 case EXPR_RRSHIFT:
1176 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
1177 break;
1178 case EXPR_SUB:
1179 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
1180 break;
1181 case EXPR_THIS:
1182 return !emit_ret || push_instr(ctx, OP_this) ? S_OK : E_OUTOFMEMORY;
1183 case EXPR_TYPEOF:
1184 hres = compile_typeof_expression(ctx, (unary_expression_t*)expr);
1185 break;
1186 case EXPR_VOID:
1187 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
1188 break;
1189 case EXPR_BXOR:
1190 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
1191 break;
1192 DEFAULT_UNREACHABLE;
1195 if(FAILED(hres))
1196 return hres;
1198 return emit_ret ? S_OK : push_instr_uint(ctx, OP_pop, 1);
1201 static inline BOOL is_loop_statement(statement_type_t type)
1203 return type == STAT_FOR || type == STAT_FORIN || type == STAT_WHILE;
1206 /* ECMA-262 3rd Edition 12.1 */
1207 static HRESULT compile_block_statement(compiler_ctx_t *ctx, block_statement_t *block, statement_t *iter)
1209 statement_ctx_t stat_ctx = {0, TRUE};
1210 BOOL needs_scope;
1211 HRESULT hres;
1213 needs_scope = block && block->scope_index;
1214 if (needs_scope)
1216 if(FAILED(hres = push_instr_uint(ctx, OP_push_block_scope, block->scope_index)))
1217 return hres;
1219 stat_ctx.scope_index = block->scope_index;
1220 stat_ctx.block_scope = TRUE;
1223 while(iter) {
1224 hres = compile_statement(ctx, needs_scope ? &stat_ctx : NULL, iter);
1225 if(FAILED(hres))
1226 return hres;
1228 iter = iter->next;
1231 if(needs_scope && !push_instr(ctx, OP_pop_scope))
1232 return E_OUTOFMEMORY;
1234 return S_OK;
1237 /* ECMA-262 3rd Edition 12.2 */
1238 static HRESULT compile_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
1240 variable_declaration_t *iter;
1241 HRESULT hres;
1243 assert(list != NULL);
1245 for(iter = list; iter; iter = iter->next) {
1246 if(!iter->expr)
1247 continue;
1249 if (iter->constant)
1250 FIXME("Constant variables are not supported.\n");
1252 hres = emit_identifier_ref(ctx, iter->identifier, 0);
1253 if(FAILED(hres))
1254 return hres;
1256 hres = compile_expression(ctx, iter->expr, TRUE);
1257 if(FAILED(hres))
1258 return hres;
1260 if(!push_instr(ctx, OP_assign))
1261 return E_OUTOFMEMORY;
1263 hres = push_instr_uint(ctx, OP_pop, 1);
1264 if(FAILED(hres))
1265 return hres;
1268 return S_OK;
1271 /* ECMA-262 3rd Edition 12.2 */
1272 static HRESULT compile_var_statement(compiler_ctx_t *ctx, var_statement_t *stat)
1274 return compile_variable_list(ctx, stat->variable_list);
1277 /* ECMA-262 3rd Edition 12.4 */
1278 static HRESULT compile_expression_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1280 HRESULT hres;
1282 hres = compile_expression(ctx, stat->expr, ctx->from_eval);
1283 if(FAILED(hres))
1284 return hres;
1286 return !ctx->from_eval || push_instr(ctx, OP_setret) ? S_OK : E_OUTOFMEMORY;
1289 /* ECMA-262 3rd Edition 12.5 */
1290 static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat)
1292 unsigned jmp_else;
1293 HRESULT hres;
1295 hres = compile_expression(ctx, stat->expr, TRUE);
1296 if(FAILED(hres))
1297 return hres;
1299 jmp_else = push_instr(ctx, OP_jmp_z);
1300 if(!jmp_else)
1301 return E_OUTOFMEMORY;
1303 hres = compile_statement(ctx, NULL, stat->if_stat);
1304 if(FAILED(hres))
1305 return hres;
1307 if(stat->else_stat) {
1308 unsigned jmp_end;
1310 jmp_end = push_instr(ctx, OP_jmp);
1311 if(!jmp_end)
1312 return E_OUTOFMEMORY;
1314 set_arg_uint(ctx, jmp_else, ctx->code_off);
1316 hres = compile_statement(ctx, NULL, stat->else_stat);
1317 if(FAILED(hres))
1318 return hres;
1320 set_arg_uint(ctx, jmp_end, ctx->code_off);
1321 }else {
1322 set_arg_uint(ctx, jmp_else, ctx->code_off);
1325 return S_OK;
1328 /* ECMA-262 3rd Edition 12.6.2 */
1329 static HRESULT compile_while_statement(compiler_ctx_t *ctx, while_statement_t *stat)
1331 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1332 unsigned jmp_off;
1333 HRESULT hres;
1335 stat_ctx.break_label = alloc_label(ctx);
1336 if(!stat_ctx.break_label)
1337 return E_OUTOFMEMORY;
1339 stat_ctx.continue_label = alloc_label(ctx);
1340 if(!stat_ctx.continue_label)
1341 return E_OUTOFMEMORY;
1343 jmp_off = ctx->code_off;
1345 if(!stat->do_while) {
1346 label_set_addr(ctx, stat_ctx.continue_label);
1347 hres = compile_expression(ctx, stat->expr, TRUE);
1348 if(FAILED(hres))
1349 return hres;
1351 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1352 if(FAILED(hres))
1353 return hres;
1356 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1357 if(FAILED(hres))
1358 return hres;
1360 set_compiler_loc(ctx, stat->stat.loc);
1361 if(stat->do_while) {
1362 label_set_addr(ctx, stat_ctx.continue_label);
1363 hres = compile_expression(ctx, stat->expr, TRUE);
1364 if(FAILED(hres))
1365 return hres;
1367 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1368 if(FAILED(hres))
1369 return hres;
1372 hres = push_instr_uint(ctx, OP_jmp, jmp_off);
1373 if(FAILED(hres))
1374 return hres;
1376 label_set_addr(ctx, stat_ctx.break_label);
1377 return S_OK;
1380 /* ECMA-262 10th Edition 13.7.4 */
1381 static HRESULT compile_for_statement(compiler_ctx_t *ctx, for_statement_t *stat)
1383 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1384 statement_ctx_t scope_stat_ctx = {0, TRUE};
1385 unsigned expr_off;
1386 HRESULT hres;
1388 if (stat->scope_index)
1390 if(FAILED(hres = push_instr_uint(ctx, OP_push_block_scope, stat->scope_index)))
1391 return hres;
1393 scope_stat_ctx.scope_index = stat->scope_index;
1394 scope_stat_ctx.block_scope = TRUE;
1395 push_compiler_statement_ctx(ctx, &scope_stat_ctx);
1398 if(stat->variable_list) {
1399 hres = compile_variable_list(ctx, stat->variable_list);
1400 if(FAILED(hres))
1401 goto done;
1402 }else if(stat->begin_expr) {
1403 hres = compile_expression(ctx, stat->begin_expr, FALSE);
1404 if(FAILED(hres))
1405 goto done;
1408 stat_ctx.break_label = alloc_label(ctx);
1409 if(!stat_ctx.break_label)
1411 hres = E_OUTOFMEMORY;
1412 goto done;
1415 stat_ctx.continue_label = alloc_label(ctx);
1416 if(!stat_ctx.continue_label)
1418 hres = E_OUTOFMEMORY;
1419 goto done;
1421 expr_off = ctx->code_off;
1423 if(stat->expr) {
1424 set_compiler_loc(ctx, stat->expr_loc);
1425 hres = compile_expression(ctx, stat->expr, TRUE);
1426 if(FAILED(hres))
1427 goto done;
1429 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1430 if(FAILED(hres))
1431 goto done;
1434 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1435 if(FAILED(hres))
1436 goto done;
1438 label_set_addr(ctx, stat_ctx.continue_label);
1440 if(stat->end_expr) {
1441 set_compiler_loc(ctx, stat->end_loc);
1442 hres = compile_expression(ctx, stat->end_expr, FALSE);
1443 if(FAILED(hres))
1444 goto done;
1447 hres = push_instr_uint(ctx, OP_jmp, expr_off);
1448 if(FAILED(hres))
1449 goto done;
1451 label_set_addr(ctx, stat_ctx.break_label);
1452 hres = S_OK;
1453 done:
1454 if (stat->scope_index)
1456 pop_compiler_statement_ctx(ctx, &scope_stat_ctx);
1457 if(SUCCEEDED(hres) && !push_instr(ctx, OP_pop_scope))
1458 return E_OUTOFMEMORY;
1460 return hres;
1463 /* ECMA-262 3rd Edition 12.6.4 */
1464 static HRESULT compile_forin_statement(compiler_ctx_t *ctx, forin_statement_t *stat)
1466 statement_ctx_t stat_ctx = {4, FALSE, FALSE};
1467 HRESULT hres;
1469 if(stat->variable) {
1470 hres = compile_variable_list(ctx, stat->variable);
1471 if(FAILED(hres))
1472 return hres;
1475 stat_ctx.break_label = alloc_label(ctx);
1476 if(!stat_ctx.break_label)
1477 return E_OUTOFMEMORY;
1479 stat_ctx.continue_label = alloc_label(ctx);
1480 if(!stat_ctx.continue_label)
1481 return E_OUTOFMEMORY;
1483 hres = compile_expression(ctx, stat->in_expr, TRUE);
1484 if(FAILED(hres))
1485 return hres;
1487 if(stat->variable) {
1488 hres = emit_identifier_ref(ctx, stat->variable->identifier, fdexNameEnsure);
1489 if(FAILED(hres))
1490 return hres;
1491 }else if(is_memberid_expr(stat->expr->type)) {
1492 hres = compile_memberid_expression(ctx, stat->expr, fdexNameEnsure);
1493 if(FAILED(hres))
1494 return hres;
1495 }else {
1496 hres = push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
1497 if(FAILED(hres))
1498 return hres;
1500 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1501 return S_OK;
1504 hres = push_instr_int(ctx, OP_int, DISPID_STARTENUM);
1505 if(FAILED(hres))
1506 return hres;
1508 label_set_addr(ctx, stat_ctx.continue_label);
1509 hres = push_instr_uint(ctx, OP_forin, stat_ctx.break_label);
1510 if(FAILED(hres))
1511 return E_OUTOFMEMORY;
1513 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1514 if(FAILED(hres))
1515 return hres;
1517 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.continue_label);
1518 if(FAILED(hres))
1519 return hres;
1521 label_set_addr(ctx, stat_ctx.break_label);
1522 return S_OK;
1525 static HRESULT pop_to_stat(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx)
1527 unsigned stack_pop = 0;
1528 statement_ctx_t *iter;
1529 HRESULT hres;
1531 for(iter = ctx->stat_ctx; iter != stat_ctx; iter = iter->next) {
1532 if(iter->using_scope && !push_instr(ctx, OP_pop_scope))
1533 return E_OUTOFMEMORY;
1534 if(iter->using_except) {
1535 if(stack_pop) {
1536 hres = push_instr_uint(ctx, OP_pop, stack_pop);
1537 if(FAILED(hres))
1538 return hres;
1539 stack_pop = 0;
1541 hres = push_instr_uint(ctx, OP_pop_except, ctx->code_off+1);
1542 if(FAILED(hres))
1543 return hres;
1545 stack_pop += iter->stack_use;
1548 if(stack_pop) {
1549 hres = push_instr_uint(ctx, OP_pop, stack_pop);
1550 if(FAILED(hres))
1551 return hres;
1554 return S_OK;
1557 /* ECMA-262 3rd Edition 12.7 */
1558 static HRESULT compile_continue_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1560 statement_ctx_t *pop_ctx;
1561 HRESULT hres;
1563 if(stat->identifier) {
1564 statement_t *label_stat;
1565 statement_ctx_t *iter;
1567 pop_ctx = NULL;
1569 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1570 if(iter->continue_label)
1571 pop_ctx = iter;
1572 if(iter->labelled_stat && !wcscmp(iter->labelled_stat->identifier, stat->identifier))
1573 break;
1576 if(!iter) {
1577 WARN("Label not found\n");
1578 return JS_E_LABEL_NOT_FOUND;
1581 /* Labelled continue are allowed only on loops */
1582 for(label_stat = iter->labelled_stat->statement;
1583 label_stat->type == STAT_LABEL;
1584 label_stat = ((labelled_statement_t*)label_stat)->statement);
1585 if(!is_loop_statement(label_stat->type)) {
1586 WARN("Label is not a loop\n");
1587 return JS_E_INVALID_CONTINUE;
1590 assert(pop_ctx != NULL);
1591 }else {
1592 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1593 if(pop_ctx->continue_label)
1594 break;
1597 if(!pop_ctx) {
1598 WARN("continue outside loop\n");
1599 return JS_E_INVALID_CONTINUE;
1603 hres = pop_to_stat(ctx, pop_ctx);
1604 if(FAILED(hres))
1605 return hres;
1607 return push_instr_uint(ctx, OP_jmp, pop_ctx->continue_label);
1610 /* ECMA-262 3rd Edition 12.8 */
1611 static HRESULT compile_break_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1613 statement_ctx_t *pop_ctx;
1614 HRESULT hres;
1616 if(stat->identifier) {
1617 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1618 if(pop_ctx->labelled_stat && !wcscmp(pop_ctx->labelled_stat->identifier, stat->identifier)) {
1619 assert(pop_ctx->break_label);
1620 break;
1624 if(!pop_ctx) {
1625 WARN("Label not found\n");
1626 return JS_E_LABEL_NOT_FOUND;
1628 }else {
1629 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1630 if(pop_ctx->break_label && !pop_ctx->labelled_stat)
1631 break;
1634 if(!pop_ctx) {
1635 WARN("Break outside loop\n");
1636 return JS_E_INVALID_BREAK;
1640 hres = pop_to_stat(ctx, pop_ctx->next);
1641 if(FAILED(hres))
1642 return hres;
1644 return push_instr_uint(ctx, OP_jmp, pop_ctx->break_label);
1647 /* ECMA-262 3rd Edition 12.9 */
1648 static HRESULT compile_return_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1650 HRESULT hres;
1652 if(ctx->from_eval) {
1653 WARN("misplaced return statement\n");
1654 return JS_E_MISPLACED_RETURN;
1657 if(stat->expr) {
1658 hres = compile_expression(ctx, stat->expr, TRUE);
1659 if(FAILED(hres))
1660 return hres;
1661 if(!push_instr(ctx, OP_setret))
1662 return E_OUTOFMEMORY;
1665 hres = pop_to_stat(ctx, NULL);
1666 if(FAILED(hres))
1667 return hres;
1669 return push_instr_uint(ctx, OP_ret, !stat->expr);
1672 /* ECMA-262 3rd Edition 12.10 */
1673 static HRESULT compile_with_statement(compiler_ctx_t *ctx, with_statement_t *stat)
1675 statement_ctx_t stat_ctx = {0, TRUE, FALSE};
1676 HRESULT hres;
1678 hres = compile_expression(ctx, stat->expr, TRUE);
1679 if(FAILED(hres))
1680 return hres;
1682 if(!push_instr(ctx, OP_push_with_scope))
1683 return E_OUTOFMEMORY;
1685 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1686 if(FAILED(hres))
1687 return hres;
1689 if(!push_instr(ctx, OP_pop_scope))
1690 return E_OUTOFMEMORY;
1692 return S_OK;
1695 /* ECMA-262 3rd Edition 12.10 */
1696 static HRESULT compile_labelled_statement(compiler_ctx_t *ctx, labelled_statement_t *stat)
1698 statement_ctx_t stat_ctx = {0, FALSE, FALSE, 0, 0, stat}, *iter;
1699 HRESULT hres;
1701 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1702 if(iter->labelled_stat && !wcscmp(iter->labelled_stat->identifier, stat->identifier)) {
1703 WARN("Label %s redefined\n", debugstr_w(stat->identifier));
1704 return JS_E_LABEL_REDEFINED;
1708 /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1709 stat_ctx.break_label = alloc_label(ctx);
1710 if(!stat_ctx.break_label)
1711 return E_OUTOFMEMORY;
1713 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1714 if(FAILED(hres))
1715 return hres;
1717 label_set_addr(ctx, stat_ctx.break_label);
1718 return S_OK;
1721 /* ECMA-262 3rd Edition 12.13 */
1722 static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat)
1724 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1725 unsigned case_cnt = 0, *case_jmps, i, default_jmp;
1726 BOOL have_default = FALSE;
1727 statement_t *stat_iter;
1728 case_clausule_t *iter;
1729 HRESULT hres;
1731 hres = compile_expression(ctx, stat->expr, TRUE);
1732 if(FAILED(hres))
1733 return hres;
1735 stat_ctx.break_label = alloc_label(ctx);
1736 if(!stat_ctx.break_label)
1737 return E_OUTOFMEMORY;
1739 for(iter = stat->case_list; iter; iter = iter->next) {
1740 if(iter->expr)
1741 case_cnt++;
1744 case_jmps = heap_alloc(case_cnt * sizeof(*case_jmps));
1745 if(!case_jmps)
1746 return E_OUTOFMEMORY;
1748 i = 0;
1749 for(iter = stat->case_list; iter; iter = iter->next) {
1750 if(!iter->expr) {
1751 have_default = TRUE;
1752 continue;
1755 set_compiler_loc(ctx, iter->loc);
1756 hres = compile_expression(ctx, iter->expr, TRUE);
1757 if(FAILED(hres))
1758 break;
1760 case_jmps[i] = push_instr(ctx, OP_case);
1761 if(!case_jmps[i]) {
1762 hres = E_OUTOFMEMORY;
1763 break;
1765 i++;
1768 if(SUCCEEDED(hres)) {
1769 hres = push_instr_uint(ctx, OP_pop, 1);
1770 if(SUCCEEDED(hres)) {
1771 default_jmp = push_instr(ctx, OP_jmp);
1772 if(!default_jmp)
1773 hres = E_OUTOFMEMORY;
1777 if(FAILED(hres)) {
1778 heap_free(case_jmps);
1779 return hres;
1782 i = 0;
1783 for(iter = stat->case_list; iter; iter = iter->next) {
1784 while(iter->next && iter->next->stat == iter->stat) {
1785 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1786 iter = iter->next;
1789 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1791 for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter);
1792 stat_iter = stat_iter->next) {
1793 hres = compile_statement(ctx, &stat_ctx, stat_iter);
1794 if(FAILED(hres))
1795 break;
1797 if(FAILED(hres))
1798 break;
1801 heap_free(case_jmps);
1802 if(FAILED(hres))
1803 return hres;
1804 assert(i == case_cnt);
1806 if(!have_default) {
1807 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.break_label);
1808 if(FAILED(hres))
1809 return hres;
1810 set_arg_uint(ctx, default_jmp, ctx->code_off);
1813 label_set_addr(ctx, stat_ctx.break_label);
1814 return S_OK;
1817 /* ECMA-262 3rd Edition 12.13 */
1818 static HRESULT compile_throw_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1820 HRESULT hres;
1822 hres = compile_expression(ctx, stat->expr, TRUE);
1823 if(FAILED(hres))
1824 return hres;
1826 return push_instr(ctx, OP_throw) ? S_OK : E_OUTOFMEMORY;
1829 /* ECMA-262 3rd Edition 12.14 */
1830 static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat)
1832 statement_ctx_t try_ctx = {0, FALSE, TRUE}, finally_ctx = {2, FALSE, FALSE};
1833 unsigned push_except, finally_off = 0, catch_off = 0, pop_except, catch_pop_except = 0;
1834 BSTR ident;
1835 HRESULT hres;
1837 push_except = push_instr(ctx, OP_push_except);
1838 if(!push_except)
1839 return E_OUTOFMEMORY;
1841 if(stat->catch_block) {
1842 ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier);
1843 if(!ident)
1844 return E_OUTOFMEMORY;
1845 }else {
1846 ident = NULL;
1849 hres = compile_statement(ctx, &try_ctx, stat->try_statement);
1850 if(FAILED(hres))
1851 return hres;
1853 pop_except = push_instr(ctx, OP_pop_except);
1854 if(!pop_except)
1855 return E_OUTOFMEMORY;
1857 if(stat->catch_block) {
1858 statement_ctx_t catch_ctx = {0, TRUE, stat->finally_statement != NULL};
1860 if(stat->finally_statement)
1861 catch_ctx.using_except = TRUE;
1863 catch_off = ctx->code_off;
1865 hres = push_instr_bstr(ctx, OP_enter_catch, ident);
1866 if(FAILED(hres))
1867 return hres;
1869 hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement);
1870 if(FAILED(hres))
1871 return hres;
1873 if(!push_instr(ctx, OP_pop_scope))
1874 return E_OUTOFMEMORY;
1876 if(stat->finally_statement) {
1877 catch_pop_except = push_instr(ctx, OP_pop_except);
1878 if(!catch_pop_except)
1879 return E_OUTOFMEMORY;
1883 if(stat->finally_statement) {
1885 * finally block expects two elements on the stack, which may be:
1886 * - (true, return_addr) set by OP_pop_except, OP_end_finally jumps back to passed address
1887 * - (false, exception_value) set when unwinding an exception, which OP_end_finally rethrows
1889 finally_off = ctx->code_off;
1890 hres = compile_statement(ctx, &finally_ctx, stat->finally_statement);
1891 if(FAILED(hres))
1892 return hres;
1894 set_compiler_loc(ctx, stat->finally_loc);
1895 if(!push_instr(ctx, OP_end_finally))
1896 return E_OUTOFMEMORY;
1899 instr_ptr(ctx, pop_except)->u.arg[0].uint = ctx->code_off;
1900 if(catch_pop_except)
1901 instr_ptr(ctx, catch_pop_except)->u.arg[0].uint = ctx->code_off;
1902 instr_ptr(ctx, push_except)->u.arg[0].uint = catch_off;
1903 instr_ptr(ctx, push_except)->u.arg[1].uint = finally_off;
1904 return S_OK;
1907 static HRESULT compile_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1909 HRESULT hres;
1911 push_compiler_statement_ctx(ctx, stat_ctx);
1913 set_compiler_loc(ctx, stat->loc);
1915 switch(stat->type) {
1916 case STAT_BLOCK:
1917 hres = compile_block_statement(ctx, (block_statement_t*)stat, ((block_statement_t*)stat)->stat_list);
1918 break;
1919 case STAT_BREAK:
1920 hres = compile_break_statement(ctx, (branch_statement_t*)stat);
1921 break;
1922 case STAT_CONTINUE:
1923 hres = compile_continue_statement(ctx, (branch_statement_t*)stat);
1924 break;
1925 case STAT_EMPTY:
1926 /* nothing to do */
1927 hres = S_OK;
1928 break;
1929 case STAT_EXPR:
1930 hres = compile_expression_statement(ctx, (expression_statement_t*)stat);
1931 break;
1932 case STAT_FOR:
1933 hres = compile_for_statement(ctx, (for_statement_t*)stat);
1934 break;
1935 case STAT_FORIN:
1936 hres = compile_forin_statement(ctx, (forin_statement_t*)stat);
1937 break;
1938 case STAT_IF:
1939 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1940 break;
1941 case STAT_LABEL:
1942 hres = compile_labelled_statement(ctx, (labelled_statement_t*)stat);
1943 break;
1944 case STAT_RETURN:
1945 hres = compile_return_statement(ctx, (expression_statement_t*)stat);
1946 break;
1947 case STAT_SWITCH:
1948 hres = compile_switch_statement(ctx, (switch_statement_t*)stat);
1949 break;
1950 case STAT_THROW:
1951 hres = compile_throw_statement(ctx, (expression_statement_t*)stat);
1952 break;
1953 case STAT_TRY:
1954 hres = compile_try_statement(ctx, (try_statement_t*)stat);
1955 break;
1956 case STAT_VAR:
1957 hres = compile_var_statement(ctx, (var_statement_t*)stat);
1958 break;
1959 case STAT_WHILE:
1960 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1961 break;
1962 case STAT_WITH:
1963 hres = compile_with_statement(ctx, (with_statement_t*)stat);
1964 break;
1965 DEFAULT_UNREACHABLE;
1968 pop_compiler_statement_ctx(ctx, stat_ctx);
1970 return hres;
1973 static inline function_local_t *find_local(compiler_ctx_t *ctx, const WCHAR *name, unsigned int scope)
1975 struct wine_rb_entry *entry = wine_rb_get(&ctx->local_scopes[scope].locals, name);
1976 return entry ? WINE_RB_ENTRY_VALUE(entry, function_local_t, entry) : NULL;
1979 static BOOL alloc_local(compiler_ctx_t *ctx, BSTR name, int ref, unsigned int scope)
1981 function_local_t *local;
1983 local = heap_pool_alloc(&ctx->heap, sizeof(*local));
1984 if(!local)
1985 return FALSE;
1987 local->name = name;
1988 local->ref = ref;
1989 wine_rb_put(&ctx->local_scopes[scope].locals, name, &local->entry);
1990 ctx->local_scopes[scope].locals_cnt++;
1991 return TRUE;
1994 static BOOL alloc_variable(compiler_ctx_t *ctx, const WCHAR *name, unsigned int scope)
1996 BSTR ident;
1998 if(find_local(ctx, name, scope))
1999 return TRUE;
2001 ident = compiler_alloc_bstr(ctx, name);
2002 if(!ident)
2003 return FALSE;
2005 return alloc_local(ctx, ident, ctx->func->var_cnt++, scope);
2008 static HRESULT visit_function_expression(compiler_ctx_t *ctx, function_expression_t *expr)
2010 statement_ctx_t *stat_ctx;
2012 expr->func_id = ctx->func->func_cnt++;
2013 ctx->func_tail = ctx->func_tail ? (ctx->func_tail->next = expr) : (ctx->func_head = expr);
2015 if(!expr->identifier || expr->event_target)
2016 return S_OK;
2018 for (stat_ctx = ctx->stat_ctx; stat_ctx; stat_ctx = stat_ctx->next)
2020 if (stat_ctx->block_scope)
2022 stat_ctx->scope_has_functions = TRUE;
2023 break;
2027 if(!expr->is_statement && ctx->parser->script->version >= SCRIPTLANGUAGEVERSION_ES5)
2028 return S_OK;
2030 return alloc_variable(ctx, expr->identifier, stat_ctx ? stat_ctx->scope_index : 0) ? S_OK : E_OUTOFMEMORY;
2033 static HRESULT visit_expression(compiler_ctx_t *ctx, expression_t *expr)
2035 HRESULT hres = S_OK;
2037 switch(expr->type) {
2038 case EXPR_ADD:
2039 case EXPR_AND:
2040 case EXPR_ARRAY:
2041 case EXPR_ASSIGN:
2042 case EXPR_ASSIGNADD:
2043 case EXPR_ASSIGNAND:
2044 case EXPR_ASSIGNSUB:
2045 case EXPR_ASSIGNMUL:
2046 case EXPR_ASSIGNDIV:
2047 case EXPR_ASSIGNMOD:
2048 case EXPR_ASSIGNOR:
2049 case EXPR_ASSIGNLSHIFT:
2050 case EXPR_ASSIGNRSHIFT:
2051 case EXPR_ASSIGNRRSHIFT:
2052 case EXPR_ASSIGNXOR:
2053 case EXPR_BAND:
2054 case EXPR_BOR:
2055 case EXPR_COMMA:
2056 case EXPR_DIV:
2057 case EXPR_EQ:
2058 case EXPR_EQEQ:
2059 case EXPR_GREATER:
2060 case EXPR_GREATEREQ:
2061 case EXPR_IN:
2062 case EXPR_INSTANCEOF:
2063 case EXPR_LESS:
2064 case EXPR_LESSEQ:
2065 case EXPR_LSHIFT:
2066 case EXPR_MOD:
2067 case EXPR_MUL:
2068 case EXPR_NOTEQ:
2069 case EXPR_NOTEQEQ:
2070 case EXPR_OR:
2071 case EXPR_RSHIFT:
2072 case EXPR_RRSHIFT:
2073 case EXPR_SUB:
2074 case EXPR_BXOR: {
2075 binary_expression_t *binary_expr = (binary_expression_t*)expr;
2077 hres = visit_expression(ctx, binary_expr->expression1);
2078 if(FAILED(hres))
2079 return hres;
2081 hres = visit_expression(ctx, binary_expr->expression2);
2082 break;
2084 case EXPR_BITNEG:
2085 case EXPR_DELETE:
2086 case EXPR_LOGNEG:
2087 case EXPR_MINUS:
2088 case EXPR_PLUS:
2089 case EXPR_POSTDEC:
2090 case EXPR_POSTINC:
2091 case EXPR_PREDEC:
2092 case EXPR_PREINC:
2093 case EXPR_TYPEOF:
2094 case EXPR_VOID:
2095 hres = visit_expression(ctx, ((unary_expression_t*)expr)->expression);
2096 break;
2097 case EXPR_IDENT:
2098 case EXPR_LITERAL:
2099 case EXPR_THIS:
2100 break;
2101 case EXPR_ARRAYLIT: {
2102 array_literal_expression_t *array_expr = (array_literal_expression_t*)expr;
2103 array_element_t *iter;
2105 for(iter = array_expr->element_list; iter; iter = iter->next) {
2106 hres = visit_expression(ctx, iter->expr);
2107 if(FAILED(hres))
2108 return hres;
2110 break;
2112 case EXPR_CALL:
2113 case EXPR_NEW: {
2114 call_expression_t *call_expr = (call_expression_t*)expr;
2115 argument_t *arg;
2117 hres = visit_expression(ctx, call_expr->expression);
2118 if(FAILED(hres))
2119 return hres;
2121 for(arg = call_expr->argument_list; arg; arg = arg->next) {
2122 hres = visit_expression(ctx, arg->expr);
2123 if(FAILED(hres))
2124 return hres;
2126 break;
2128 case EXPR_COND: {
2129 conditional_expression_t *cond_expr = (conditional_expression_t*)expr;
2131 hres = visit_expression(ctx, cond_expr->expression);
2132 if(FAILED(hres))
2133 return hres;
2135 hres = visit_expression(ctx, cond_expr->true_expression);
2136 if(FAILED(hres))
2137 return hres;
2139 hres = visit_expression(ctx, cond_expr->false_expression);
2140 break;
2142 case EXPR_FUNC:
2143 hres = visit_function_expression(ctx, (function_expression_t*)expr);
2144 break;
2145 case EXPR_MEMBER:
2146 hres = visit_expression(ctx, ((member_expression_t*)expr)->expression);
2147 break;
2148 case EXPR_PROPVAL: {
2149 property_definition_t *iter;
2150 for(iter = ((property_value_expression_t*)expr)->property_list; iter; iter = iter->next) {
2151 hres = visit_expression(ctx, iter->value);
2152 if(FAILED(hres))
2153 return hres;
2155 break;
2157 DEFAULT_UNREACHABLE;
2160 return hres;
2163 static HRESULT visit_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
2165 variable_declaration_t *iter;
2166 statement_ctx_t *stat_ctx;
2167 HRESULT hres;
2169 for(iter = list; iter; iter = iter->next) {
2170 for (stat_ctx = ctx->stat_ctx; stat_ctx; stat_ctx = stat_ctx->next)
2172 if (stat_ctx->block_scope)
2173 break;
2176 if(!alloc_variable(ctx, iter->identifier, iter->block_scope && stat_ctx ? stat_ctx->scope_index : 0))
2177 return E_OUTOFMEMORY;
2179 if(iter->expr) {
2180 hres = visit_expression(ctx, iter->expr);
2181 if(FAILED(hres))
2182 return hres;
2186 return S_OK;
2189 static HRESULT visit_statement(compiler_ctx_t*,statement_ctx_t *,statement_t*);
2191 static HRESULT visit_block_statement(compiler_ctx_t *ctx, block_statement_t *block, statement_t *iter)
2193 statement_ctx_t stat_ctx = {0, TRUE};
2194 BOOL needs_scope;
2195 HRESULT hres;
2197 needs_scope = block && ctx->parser->script->version >= SCRIPTLANGUAGEVERSION_ES5;
2198 if (needs_scope)
2200 if (!alloc_local_scope(ctx, &block->scope_index))
2201 return E_OUTOFMEMORY;
2203 stat_ctx.scope_index = block->scope_index;
2204 stat_ctx.block_scope = TRUE;
2207 while(iter) {
2208 hres = visit_statement(ctx, needs_scope ? &stat_ctx : NULL, iter);
2209 if(FAILED(hres))
2210 return hres;
2212 iter = iter->next;
2215 if (needs_scope && !(ctx->local_scopes[stat_ctx.scope_index].locals_cnt || stat_ctx.scope_has_functions))
2216 remove_local_scope(ctx, block->scope_index);
2218 return S_OK;
2221 static HRESULT visit_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
2223 HRESULT hres = S_OK;
2225 push_compiler_statement_ctx(ctx, stat_ctx);
2227 switch(stat->type) {
2228 case STAT_BLOCK:
2229 hres = visit_block_statement(ctx, (block_statement_t*)stat, ((block_statement_t*)stat)->stat_list);
2230 break;
2231 case STAT_BREAK:
2232 case STAT_CONTINUE:
2233 case STAT_EMPTY:
2234 break;
2235 case STAT_EXPR: {
2236 expression_statement_t *expr_stat = (expression_statement_t*)stat;
2237 if(expr_stat->expr) {
2238 if(expr_stat->expr->type == EXPR_FUNC)
2239 ((function_expression_t*)expr_stat->expr)->is_statement = TRUE;
2240 hres = visit_expression(ctx, expr_stat->expr);
2242 break;
2244 case STAT_RETURN:
2245 case STAT_THROW: {
2246 expression_statement_t *expr_stat = (expression_statement_t*)stat;
2247 if(expr_stat->expr)
2248 hres = visit_expression(ctx, expr_stat->expr);
2249 break;
2251 case STAT_FOR: {
2252 statement_ctx_t stat_ctx_data = {0, TRUE}, *stat_ctx = NULL;
2253 for_statement_t *for_stat = (for_statement_t*)stat;
2255 if(for_stat->variable_list)
2257 variable_declaration_t *var;
2259 for(var = for_stat->variable_list; var; var = var->next)
2261 if (var->block_scope)
2263 stat_ctx = &stat_ctx_data;
2264 break;
2268 if (stat_ctx)
2270 if (!alloc_local_scope(ctx, &for_stat->scope_index))
2272 hres = E_OUTOFMEMORY;
2273 break;
2275 stat_ctx->scope_index = for_stat->scope_index;
2276 stat_ctx->block_scope = TRUE;
2277 push_compiler_statement_ctx(ctx, stat_ctx);
2279 hres = visit_variable_list(ctx, for_stat->variable_list);
2281 else if(for_stat->begin_expr)
2282 hres = visit_expression(ctx, for_stat->begin_expr);
2283 if(FAILED(hres))
2285 pop_compiler_statement_ctx(ctx, stat_ctx);
2286 break;
2289 if(for_stat->expr) {
2290 hres = visit_expression(ctx, for_stat->expr);
2291 if(FAILED(hres))
2293 pop_compiler_statement_ctx(ctx, stat_ctx);
2294 break;
2298 hres = visit_statement(ctx, NULL, for_stat->statement);
2299 if(FAILED(hres))
2301 pop_compiler_statement_ctx(ctx, stat_ctx);
2302 break;
2304 if(for_stat->end_expr)
2305 hres = visit_expression(ctx, for_stat->end_expr);
2306 pop_compiler_statement_ctx(ctx, stat_ctx);
2307 break;
2309 case STAT_FORIN: {
2310 forin_statement_t *forin_stat = (forin_statement_t*)stat;
2312 if(forin_stat->variable) {
2313 hres = visit_variable_list(ctx, forin_stat->variable);
2314 if(FAILED(hres))
2315 break;
2318 hres = visit_expression(ctx, forin_stat->in_expr);
2319 if(FAILED(hres))
2320 return hres;
2322 if(forin_stat->expr) {
2323 hres = visit_expression(ctx, forin_stat->expr);
2324 if(FAILED(hres))
2325 return hres;
2328 hres = visit_statement(ctx, NULL, forin_stat->statement);
2329 break;
2331 case STAT_IF: {
2332 if_statement_t *if_stat = (if_statement_t*)stat;
2334 hres = visit_expression(ctx, if_stat->expr);
2335 if(FAILED(hres))
2336 return hres;
2338 hres = visit_statement(ctx, NULL, if_stat->if_stat);
2339 if(FAILED(hres))
2340 return hres;
2342 if(if_stat->else_stat)
2343 hres = visit_statement(ctx, NULL, if_stat->else_stat);
2344 break;
2346 case STAT_LABEL:
2347 hres = visit_statement(ctx, NULL, ((labelled_statement_t*)stat)->statement);
2348 break;
2349 case STAT_SWITCH: {
2350 switch_statement_t *switch_stat = (switch_statement_t*)stat;
2351 statement_t *stat_iter;
2352 case_clausule_t *iter;
2354 hres = visit_expression(ctx, switch_stat->expr);
2355 if(FAILED(hres))
2356 return hres;
2358 for(iter = switch_stat->case_list; iter; iter = iter->next) {
2359 if(!iter->expr)
2360 continue;
2361 hres = visit_expression(ctx, iter->expr);
2362 if(FAILED(hres))
2363 return hres;
2366 for(iter = switch_stat->case_list; iter; iter = iter->next) {
2367 while(iter->next && iter->next->stat == iter->stat)
2368 iter = iter->next;
2369 for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter);
2370 stat_iter = stat_iter->next) {
2371 hres = visit_statement(ctx, NULL, stat_iter);
2372 if(FAILED(hres))
2373 return hres;
2376 break;
2378 case STAT_TRY: {
2379 try_statement_t *try_stat = (try_statement_t*)stat;
2381 hres = visit_statement(ctx, NULL, try_stat->try_statement);
2382 if(FAILED(hres))
2383 return hres;
2385 if(try_stat->catch_block) {
2386 hres = visit_statement(ctx, NULL, try_stat->catch_block->statement);
2387 if(FAILED(hres))
2388 return hres;
2391 if(try_stat->finally_statement)
2392 hres = visit_statement(ctx, NULL, try_stat->finally_statement);
2393 break;
2395 case STAT_VAR:
2396 hres = visit_variable_list(ctx, ((var_statement_t*)stat)->variable_list);
2397 break;
2398 case STAT_WHILE: {
2399 while_statement_t *while_stat = (while_statement_t*)stat;
2401 hres = visit_expression(ctx, while_stat->expr);
2402 if(FAILED(hres))
2403 return hres;
2405 hres = visit_statement(ctx, NULL, while_stat->statement);
2406 break;
2408 case STAT_WITH: {
2409 with_statement_t *with_stat = (with_statement_t*)stat;
2411 hres = visit_expression(ctx, with_stat->expr);
2412 if(FAILED(hres))
2413 return hres;
2415 hres = visit_statement(ctx, NULL, with_stat->statement);
2416 break;
2418 DEFAULT_UNREACHABLE;
2421 pop_compiler_statement_ctx(ctx, stat_ctx);
2423 return hres;
2426 static void resolve_labels(compiler_ctx_t *ctx, unsigned off)
2428 instr_t *instr;
2430 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
2431 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->u.arg->uint & LABEL_FLAG)) {
2432 assert((instr->u.arg->uint & ~LABEL_FLAG) < ctx->labels_cnt);
2433 instr->u.arg->uint = ctx->labels[instr->u.arg->uint & ~LABEL_FLAG];
2435 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
2438 ctx->labels_cnt = 0;
2441 unsigned get_location_line(bytecode_t *code, unsigned loc, unsigned *char_pos)
2443 unsigned line = code->start_line;
2444 const WCHAR *nl, *p;
2446 for(nl = p = code->source; p < code->source + loc; p++) {
2447 if(*p != '\n') continue;
2448 line++;
2449 nl = p + 1;
2451 *char_pos = loc - (nl - code->source);
2452 return line;
2455 void release_bytecode(bytecode_t *code)
2457 unsigned i;
2459 if(--code->ref)
2460 return;
2462 for(i=0; i < code->bstr_cnt; i++)
2463 SysFreeString(code->bstr_pool[i]);
2464 for(i=0; i < code->str_cnt; i++)
2465 jsstr_release(code->str_pool[i]);
2467 if(code->named_item)
2468 release_named_item(code->named_item);
2469 heap_free(code->source);
2470 heap_pool_free(&code->heap);
2471 heap_free(code->bstr_pool);
2472 heap_free(code->str_pool);
2473 heap_free(code->instrs);
2474 heap_free(code);
2477 static HRESULT init_code(compiler_ctx_t *compiler, const WCHAR *source, UINT64 source_context, unsigned start_line)
2479 size_t len = source ? lstrlenW(source) : 0;
2481 if(len > INT32_MAX)
2482 return E_OUTOFMEMORY;
2484 compiler->code = heap_alloc_zero(sizeof(bytecode_t));
2485 if(!compiler->code)
2486 return E_OUTOFMEMORY;
2488 compiler->code->ref = 1;
2489 compiler->code->source_context = source_context;
2490 compiler->code->start_line = start_line;
2491 heap_pool_init(&compiler->code->heap);
2493 compiler->code->source = heap_alloc((len + 1) * sizeof(WCHAR));
2494 if(!compiler->code->source) {
2495 release_bytecode(compiler->code);
2496 return E_OUTOFMEMORY;
2498 if(len)
2499 memcpy(compiler->code->source, source, len * sizeof(WCHAR));
2500 compiler->code->source[len] = 0;
2502 compiler->code->instrs = heap_alloc(64 * sizeof(instr_t));
2503 if(!compiler->code->instrs) {
2504 release_bytecode(compiler->code);
2505 return E_OUTOFMEMORY;
2508 compiler->code_size = 64;
2509 compiler->code_off = 1;
2510 return S_OK;
2513 static HRESULT compile_function(compiler_ctx_t *ctx, statement_list_t *source, function_expression_t *func_expr,
2514 BOOL from_eval, function_code_t *func)
2516 function_expression_t *iter;
2517 function_local_t *local;
2518 unsigned off, i, scope;
2519 HRESULT hres;
2521 TRACE("\n");
2523 func->bytecode = ctx->code;
2524 func->local_ref = INVALID_LOCAL_REF;
2525 func->scope_index = 0;
2526 ctx->func_head = ctx->func_tail = NULL;
2527 ctx->from_eval = from_eval;
2528 ctx->func = func;
2529 ctx->local_scope_count = 0;
2530 if (!alloc_local_scope(ctx, &scope))
2531 return E_OUTOFMEMORY;
2532 assert(!scope);
2534 if(func_expr) {
2535 parameter_t *param_iter;
2537 if(func_expr->identifier) {
2538 func->name = compiler_alloc_bstr(ctx, func_expr->identifier);
2539 if(!func->name)
2540 return E_OUTOFMEMORY;
2543 if(func_expr->event_target) {
2544 func->event_target = compiler_alloc_bstr(ctx, func_expr->event_target);
2545 if(!func->event_target)
2546 return E_OUTOFMEMORY;
2549 func->source = func_expr->src_str;
2550 func->source_len = func_expr->src_len;
2552 for(param_iter = func_expr->parameter_list; param_iter; param_iter = param_iter->next)
2553 func->param_cnt++;
2555 func->params = compiler_alloc(ctx->code, func->param_cnt * sizeof(*func->params));
2556 if(!func->params)
2557 return E_OUTOFMEMORY;
2559 for(param_iter = func_expr->parameter_list, i=0; param_iter; param_iter = param_iter->next, i++) {
2560 func->params[i] = compiler_alloc_bstr(ctx, param_iter->identifier);
2561 if(!func->params[i])
2562 return E_OUTOFMEMORY;
2566 for(i = 0; i < func->param_cnt; i++) {
2567 if(!find_local(ctx, func->params[i], 0) && !alloc_local(ctx, func->params[i], -i-1, 0))
2568 return E_OUTOFMEMORY;
2571 hres = visit_block_statement(ctx, NULL, source ? source->head : NULL);
2572 if(FAILED(hres))
2573 return hres;
2575 func->local_scope_count = ctx->local_scope_count;
2576 func->local_scopes = compiler_alloc(ctx->code, func->local_scope_count * sizeof(*func->local_scopes));
2577 if(!func->local_scopes)
2578 return E_OUTOFMEMORY;
2580 func->variables = compiler_alloc(ctx->code, func->var_cnt * sizeof(*func->variables));
2581 if(!func->variables)
2582 return E_OUTOFMEMORY;
2584 for (scope = 0; scope < func->local_scope_count; ++scope)
2586 func->local_scopes[scope].locals = compiler_alloc(ctx->code,
2587 ctx->local_scopes[scope].locals_cnt * sizeof(*func->local_scopes[scope].locals));
2588 if(!func->local_scopes[scope].locals)
2589 return E_OUTOFMEMORY;
2590 func->local_scopes[scope].locals_cnt = ctx->local_scopes[scope].locals_cnt;
2592 i = 0;
2593 WINE_RB_FOR_EACH_ENTRY(local, &ctx->local_scopes[scope].locals, function_local_t, entry) {
2594 func->local_scopes[scope].locals[i].name = local->name;
2595 func->local_scopes[scope].locals[i].ref = local->ref;
2596 if(local->ref >= 0) {
2597 func->variables[local->ref].name = local->name;
2598 func->variables[local->ref].func_id = -1;
2600 i++;
2602 assert(i == ctx->local_scopes[scope].locals_cnt);
2605 func->funcs = compiler_alloc(ctx->code, func->func_cnt * sizeof(*func->funcs));
2606 if(!func->funcs)
2607 return E_OUTOFMEMORY;
2608 memset(func->funcs, 0, func->func_cnt * sizeof(*func->funcs));
2610 ctx->current_function_expr = ctx->func_head;
2611 off = ctx->code_off;
2612 hres = compile_block_statement(ctx, NULL, source ? source->head : NULL);
2613 if(FAILED(hres))
2614 return hres;
2616 resolve_labels(ctx, off);
2618 hres = push_instr_uint(ctx, OP_ret, !from_eval);
2619 if(FAILED(hres))
2620 return hres;
2622 if(TRACE_ON(jscript_disas))
2623 dump_code(ctx, off);
2625 func->instr_off = off;
2627 for(iter = ctx->func_head, i=0; iter; iter = iter->next, i++) {
2628 hres = compile_function(ctx, iter->statement_list, iter, FALSE, func->funcs+i);
2629 if(FAILED(hres))
2630 return hres;
2632 func->funcs[i].scope_index = iter->scope_index;
2634 TRACE("[%d] func %s, scope_index %u\n", i, debugstr_w(func->funcs[i].name), iter->scope_index);
2635 if((ctx->parser->script->version < SCRIPTLANGUAGEVERSION_ES5 || iter->is_statement) &&
2636 func->funcs[i].name && !func->funcs[i].event_target) {
2637 local_ref_t *local_ref = lookup_local(func, func->funcs[i].name, func->funcs[i].scope_index);
2639 func->funcs[i].local_ref = local_ref->ref;
2640 TRACE("found ref %s %d for %s\n", debugstr_w(local_ref->name), local_ref->ref, debugstr_w(func->funcs[i].name));
2641 if(local_ref->ref >= 0)
2642 func->variables[local_ref->ref].func_id = i;
2646 assert(i == func->func_cnt);
2648 return S_OK;
2651 static HRESULT parse_arguments(compiler_ctx_t *ctx, const WCHAR *args, BSTR *arg_array, unsigned *args_size)
2653 const WCHAR *ptr = args, *ptr2;
2654 unsigned arg_cnt = 0;
2656 while(iswspace(*ptr))
2657 ptr++;
2658 if(!*ptr) {
2659 if(args_size)
2660 *args_size = 0;
2661 return S_OK;
2664 while(1) {
2665 if(!iswalpha(*ptr) && *ptr != '_') {
2666 FIXME("expected alpha or '_': %s\n", debugstr_w(ptr));
2667 return E_FAIL;
2670 ptr2 = ptr;
2671 while(iswalnum(*ptr) || *ptr == '_')
2672 ptr++;
2674 if(*ptr && *ptr != ',' && !iswspace(*ptr)) {
2675 FIXME("unexpected har %s\n", debugstr_w(ptr));
2676 return E_FAIL;
2679 if(arg_array) {
2680 arg_array[arg_cnt] = compiler_alloc_bstr_len(ctx, ptr2, ptr-ptr2);
2681 if(!arg_array[arg_cnt])
2682 return E_OUTOFMEMORY;
2684 arg_cnt++;
2686 while(iswspace(*ptr))
2687 ptr++;
2688 if(!*ptr)
2689 break;
2690 if(*ptr != ',') {
2691 FIXME("expected ',': %s\n", debugstr_w(ptr));
2692 return E_FAIL;
2695 ptr++;
2696 while(iswspace(*ptr))
2697 ptr++;
2700 if(args_size)
2701 *args_size = arg_cnt;
2702 return S_OK;
2705 static HRESULT compile_arguments(compiler_ctx_t *ctx, const WCHAR *args)
2707 HRESULT hres;
2709 hres = parse_arguments(ctx, args, NULL, &ctx->code->global_code.param_cnt);
2710 if(FAILED(hres))
2711 return hres;
2713 ctx->code->global_code.params = compiler_alloc(ctx->code,
2714 ctx->code->global_code.param_cnt * sizeof(*ctx->code->global_code.params));
2715 if(!ctx->code->global_code.params)
2716 return E_OUTOFMEMORY;
2718 return parse_arguments(ctx, args, ctx->code->global_code.params, NULL);
2721 HRESULT compile_script(script_ctx_t *ctx, const WCHAR *code, UINT64 source_context, unsigned start_line,
2722 const WCHAR *args, const WCHAR *delimiter, BOOL from_eval, BOOL use_decode,
2723 named_item_t *named_item, bytecode_t **ret)
2725 compiler_ctx_t compiler = {0};
2726 HRESULT hres;
2728 hres = init_code(&compiler, code, source_context, start_line);
2729 if(FAILED(hres))
2730 return hres;
2732 if(args) {
2733 hres = compile_arguments(&compiler, args);
2734 if(FAILED(hres))
2735 return hres;
2738 if(use_decode) {
2739 hres = decode_source(compiler.code->source);
2740 if(FAILED(hres)) {
2741 WARN("Decoding failed\n");
2742 return hres;
2746 hres = script_parse(ctx, &compiler, compiler.code, delimiter, from_eval, &compiler.parser);
2747 if(FAILED(hres)) {
2748 release_bytecode(compiler.code);
2749 return hres;
2752 heap_pool_init(&compiler.heap);
2753 hres = compile_function(&compiler, compiler.parser->source, NULL, from_eval, &compiler.code->global_code);
2754 heap_free(compiler.local_scopes);
2755 heap_pool_free(&compiler.heap);
2756 parser_release(compiler.parser);
2757 if(FAILED(hres)) {
2758 if(hres != DISP_E_EXCEPTION)
2759 throw_error(ctx, hres, NULL);
2760 set_error_location(ctx->ei, compiler.code, compiler.loc, IDS_COMPILATION_ERROR, NULL);
2761 release_bytecode(compiler.code);
2762 return DISP_E_EXCEPTION;
2765 if(named_item) {
2766 compiler.code->named_item = named_item;
2767 named_item->ref++;
2770 *ret = compiler.code;
2771 return S_OK;