windows.networking.hostname/tests: Check if passed HSTRING is duplicated.
[wine.git] / dlls / jscript / compile.c
blob2d2c2c9f40adf4aa2ab6250e6e643f99933f045d
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 = 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 = malloc(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 = 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 = malloc(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 = 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 = 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 = malloc(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 = 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 = 0;
724 HRESULT hres = S_OK;
725 argument_t *arg;
726 unsigned instr;
727 jsop_t op;
729 if(is_memberid_expr(expr->expression->type)) {
730 if(expr->expression->type == EXPR_IDENT && !wcscmp(((identifier_expression_t*)expr->expression)->identifier, L"eval"))
731 op = OP_call_eval;
732 else {
733 op = OP_call_member;
734 extra_args = 2;
735 hres = compile_memberid_expression(ctx, expr->expression, 0);
737 }else {
738 op = OP_call;
739 extra_args = 1;
740 hres = compile_expression(ctx, expr->expression, TRUE);
743 if(FAILED(hres))
744 return hres;
746 for(arg = expr->argument_list; arg; arg = arg->next) {
747 hres = compile_expression(ctx, arg->expr, TRUE);
748 if(FAILED(hres))
749 return hres;
750 arg_cnt++;
753 instr = push_instr(ctx, op);
754 if(!instr)
755 return E_OUTOFMEMORY;
757 instr_ptr(ctx, instr)->u.arg[0].uint = arg_cnt;
758 instr_ptr(ctx, instr)->u.arg[1].lng = emit_ret;
760 hres = push_instr_uint(ctx, OP_pop, arg_cnt + extra_args);
761 if(FAILED(hres))
762 return hres;
764 return !emit_ret || push_instr(ctx, OP_push_acc) ? S_OK : E_OUTOFMEMORY;
767 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
769 HRESULT hres;
771 switch(expr->expression->type) {
772 case EXPR_ARRAY: {
773 binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
775 hres = compile_expression(ctx, array_expr->expression1, TRUE);
776 if(FAILED(hres))
777 return hres;
779 hres = compile_expression(ctx, array_expr->expression2, TRUE);
780 if(FAILED(hres))
781 return hres;
783 if(!push_instr(ctx, OP_delete))
784 return E_OUTOFMEMORY;
785 break;
787 case EXPR_MEMBER: {
788 member_expression_t *member_expr = (member_expression_t*)expr->expression;
789 jsstr_t *jsstr;
791 hres = compile_expression(ctx, member_expr->expression, TRUE);
792 if(FAILED(hres))
793 return hres;
795 /* FIXME: Potential optimization */
796 jsstr = compiler_alloc_string(ctx, member_expr->identifier);
797 if(!jsstr)
798 return E_OUTOFMEMORY;
800 hres = push_instr_str(ctx, OP_str, jsstr);
801 if(FAILED(hres))
802 return hres;
804 if(!push_instr(ctx, OP_delete))
805 return E_OUTOFMEMORY;
806 break;
808 case EXPR_IDENT:
809 return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
810 default: {
811 WARN("invalid delete, unimplemented exception message\n");
813 hres = compile_expression(ctx, expr->expression, TRUE);
814 if(FAILED(hres))
815 return hres;
817 return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, L"FIXME");
821 return S_OK;
824 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
826 jsop_t assign_op = OP_throw_ref;
827 unsigned arg_cnt = 0;
828 HRESULT hres;
830 if(expr->expression1->type == EXPR_CALL) {
831 call_expression_t *call_expr = (call_expression_t*)expr->expression1;
832 argument_t *arg;
834 if(is_memberid_expr(call_expr->expression->type) && call_expr->argument_list) {
835 hres = compile_memberid_expression(ctx, call_expr->expression, fdexNameEnsure);
836 if(FAILED(hres))
837 return hres;
839 for(arg = call_expr->argument_list; arg; arg = arg->next) {
840 hres = compile_expression(ctx, arg->expr, TRUE);
841 if(FAILED(hres))
842 return hres;
843 arg_cnt++;
846 if(op != OP_LAST) {
847 unsigned instr;
849 /* We need to call the functions twice: to get the value and to set it.
850 * JavaScript interpreted functions may to modify value on the stack,
851 * but assignment calls are allowed only on external functions, so we
852 * may reuse the stack here. */
853 instr = push_instr(ctx, OP_call_member);
854 if(!instr)
855 return E_OUTOFMEMORY;
856 instr_ptr(ctx, instr)->u.arg[0].uint = arg_cnt;
857 instr_ptr(ctx, instr)->u.arg[1].lng = 1;
859 if(!push_instr(ctx, OP_push_acc))
860 return E_OUTOFMEMORY;
862 assign_op = OP_assign_call;
864 }else if(is_memberid_expr(expr->expression1->type)) {
865 if(op != OP_LAST || expr->expression1->type == EXPR_IDENT) {
866 hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
867 if(FAILED(hres))
868 return hres;
869 if(op != OP_LAST && !push_instr(ctx, OP_refval))
870 return E_OUTOFMEMORY;
871 assign_op = OP_assign;
872 }else {
873 hres = emit_member_expression(ctx, expr->expression1);
874 if(FAILED(hres))
875 return hres;
876 assign_op = OP_set_member;
880 if(assign_op == OP_throw_ref) {
881 /* Illegal assignment: evaluate and throw */
882 hres = compile_expression(ctx, expr->expression1, TRUE);
883 if(FAILED(hres))
884 return hres;
885 arg_cnt = JS_E_ILLEGAL_ASSIGN;
888 hres = compile_expression(ctx, expr->expression2, TRUE);
889 if(FAILED(hres))
890 return hres;
892 if(op != OP_LAST && !push_instr(ctx, op))
893 return E_OUTOFMEMORY;
895 return push_instr_uint(ctx, assign_op, arg_cnt);
898 static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
900 jsop_t op;
901 HRESULT hres;
903 if(is_memberid_expr(expr->expression->type)) {
904 if(expr->expression->type == EXPR_IDENT)
905 return push_instr_bstr(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
907 op = OP_typeofid;
908 hres = compile_memberid_expression(ctx, expr->expression, 0);
909 }else {
910 op = OP_typeof;
911 hres = compile_expression(ctx, expr->expression, TRUE);
913 if(FAILED(hres))
914 return hres;
916 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
919 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
921 switch(literal->type) {
922 case LT_BOOL:
923 return push_instr_int(ctx, OP_bool, literal->u.bval);
924 case LT_DOUBLE:
925 return push_instr_double(ctx, OP_double, literal->u.dval);
926 case LT_NULL:
927 return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
928 case LT_STRING:
929 return push_instr_str(ctx, OP_str, literal->u.str);
930 case LT_REGEXP:
931 return push_instr_str_uint(ctx, OP_regexp, literal->u.regexp.str, literal->u.regexp.flags);
932 DEFAULT_UNREACHABLE;
934 return E_FAIL;
937 static HRESULT literal_as_string(compiler_ctx_t *ctx, literal_t *literal, jsstr_t **str)
939 switch(literal->type) {
940 case LT_STRING:
941 *str = literal->u.str;
942 break;
943 case LT_DOUBLE:
944 return double_to_string(literal->u.dval, str);
945 DEFAULT_UNREACHABLE;
948 return *str ? S_OK : E_OUTOFMEMORY;
951 static HRESULT compile_array_literal(compiler_ctx_t *ctx, array_literal_expression_t *expr)
953 unsigned length = 0;
954 array_element_t *iter;
955 unsigned array_instr;
956 HRESULT hres;
958 array_instr = push_instr(ctx, OP_carray);
960 for(iter = expr->element_list; iter; iter = iter->next) {
961 length += iter->elision;
963 hres = compile_expression(ctx, iter->expr, TRUE);
964 if(FAILED(hres))
965 return hres;
967 hres = push_instr_uint(ctx, OP_carray_set, length);
968 if(FAILED(hres))
969 return hres;
971 length++;
974 instr_ptr(ctx, array_instr)->u.arg[0].uint = length + expr->length;
975 return S_OK;
978 static HRESULT compile_object_literal(compiler_ctx_t *ctx, property_value_expression_t *expr)
980 property_definition_t *iter;
981 jsstr_t *name;
982 HRESULT hres;
984 if(!push_instr(ctx, OP_new_obj))
985 return E_OUTOFMEMORY;
987 for(iter = expr->property_list; iter; iter = iter->next) {
988 hres = literal_as_string(ctx, iter->name, &name);
989 if(FAILED(hres))
990 return hres;
992 hres = compile_expression(ctx, iter->value, TRUE);
993 if(FAILED(hres))
994 return hres;
996 hres = push_instr_str_uint(ctx, OP_obj_prop, name, iter->type);
997 if(FAILED(hres))
998 return hres;
1001 return S_OK;
1004 static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr, BOOL emit_ret)
1006 statement_ctx_t *stat_ctx;
1008 assert(ctx->current_function_expr);
1010 for(stat_ctx = ctx->stat_ctx; stat_ctx; stat_ctx = stat_ctx->next)
1012 if(stat_ctx->block_scope)
1013 break;
1015 ctx->current_function_expr->scope_index = stat_ctx ? stat_ctx->scope_index : 0;
1016 ctx->current_function_expr = ctx->current_function_expr->next;
1018 return emit_ret ? push_instr_uint(ctx, OP_func, expr->func_id) : S_OK;
1021 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr, BOOL emit_ret)
1023 HRESULT hres;
1025 switch(expr->type) {
1026 case EXPR_ADD:
1027 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
1028 break;
1029 case EXPR_AND:
1030 hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_z);
1031 break;
1032 case EXPR_ARRAY:
1033 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
1034 break;
1035 case EXPR_ARRAYLIT:
1036 hres = compile_array_literal(ctx, (array_literal_expression_t*)expr);
1037 break;
1038 case EXPR_ASSIGN:
1039 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
1040 break;
1041 case EXPR_ASSIGNADD:
1042 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
1043 break;
1044 case EXPR_ASSIGNAND:
1045 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_and);
1046 break;
1047 case EXPR_ASSIGNSUB:
1048 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
1049 break;
1050 case EXPR_ASSIGNMUL:
1051 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
1052 break;
1053 case EXPR_ASSIGNDIV:
1054 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
1055 break;
1056 case EXPR_ASSIGNMOD:
1057 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
1058 break;
1059 case EXPR_ASSIGNOR:
1060 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
1061 break;
1062 case EXPR_ASSIGNLSHIFT:
1063 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_lshift);
1064 break;
1065 case EXPR_ASSIGNRSHIFT:
1066 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift);
1067 break;
1068 case EXPR_ASSIGNRRSHIFT:
1069 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
1070 break;
1071 case EXPR_ASSIGNXOR:
1072 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
1073 break;
1074 case EXPR_BAND:
1075 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
1076 break;
1077 case EXPR_BITNEG:
1078 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
1079 break;
1080 case EXPR_BOR:
1081 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
1082 break;
1083 case EXPR_CALL:
1084 return compile_call_expression(ctx, (call_expression_t*)expr, emit_ret);
1085 case EXPR_COMMA:
1086 return compile_comma_expression(ctx, (binary_expression_t*)expr, emit_ret);
1087 case EXPR_COND:
1088 hres = compile_conditional_expression(ctx, (conditional_expression_t*)expr);
1089 break;
1090 case EXPR_DELETE:
1091 hres = compile_delete_expression(ctx, (unary_expression_t*)expr);
1092 break;
1093 case EXPR_DIV:
1094 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
1095 break;
1096 case EXPR_EQ:
1097 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
1098 break;
1099 case EXPR_EQEQ:
1100 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
1101 break;
1102 case EXPR_FUNC:
1103 return compile_function_expression(ctx, (function_expression_t*)expr, emit_ret);
1104 case EXPR_GREATER:
1105 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
1106 break;
1107 case EXPR_GREATEREQ:
1108 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
1109 break;
1110 case EXPR_IDENT:
1111 hres = emit_identifier(ctx, ((identifier_expression_t*)expr)->identifier);
1112 break;
1113 case EXPR_IN:
1114 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
1115 break;
1116 case EXPR_INSTANCEOF:
1117 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
1118 break;
1119 case EXPR_LESS:
1120 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
1121 break;
1122 case EXPR_LESSEQ:
1123 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
1124 break;
1125 case EXPR_LITERAL:
1126 hres = compile_literal(ctx, ((literal_expression_t*)expr)->literal);
1127 break;
1128 case EXPR_LOGNEG:
1129 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
1130 break;
1131 case EXPR_LSHIFT:
1132 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
1133 break;
1134 case EXPR_MEMBER:
1135 hres = compile_member_expression(ctx, (member_expression_t*)expr);
1136 break;
1137 case EXPR_MINUS:
1138 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
1139 break;
1140 case EXPR_MOD:
1141 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
1142 break;
1143 case EXPR_MUL:
1144 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
1145 break;
1146 case EXPR_NEW:
1147 hres = compile_new_expression(ctx, (call_expression_t*)expr);
1148 break;
1149 case EXPR_NOTEQ:
1150 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
1151 break;
1152 case EXPR_NOTEQEQ:
1153 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
1154 break;
1155 case EXPR_OR:
1156 hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_nz);
1157 break;
1158 case EXPR_PLUS:
1159 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
1160 break;
1161 case EXPR_POSTDEC:
1162 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
1163 break;
1164 case EXPR_POSTINC:
1165 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
1166 break;
1167 case EXPR_PREDEC:
1168 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
1169 break;
1170 case EXPR_PREINC:
1171 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
1172 break;
1173 case EXPR_PROPVAL:
1174 hres = compile_object_literal(ctx, (property_value_expression_t*)expr);
1175 break;
1176 case EXPR_RSHIFT:
1177 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
1178 break;
1179 case EXPR_RRSHIFT:
1180 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
1181 break;
1182 case EXPR_SUB:
1183 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
1184 break;
1185 case EXPR_THIS:
1186 return !emit_ret || push_instr(ctx, OP_this) ? S_OK : E_OUTOFMEMORY;
1187 case EXPR_TYPEOF:
1188 hres = compile_typeof_expression(ctx, (unary_expression_t*)expr);
1189 break;
1190 case EXPR_VOID:
1191 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
1192 break;
1193 case EXPR_BXOR:
1194 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
1195 break;
1196 DEFAULT_UNREACHABLE;
1199 if(FAILED(hres))
1200 return hres;
1202 return emit_ret ? S_OK : push_instr_uint(ctx, OP_pop, 1);
1205 static inline BOOL is_loop_statement(statement_type_t type)
1207 return type == STAT_FOR || type == STAT_FORIN || type == STAT_WHILE;
1210 /* ECMA-262 3rd Edition 12.1 */
1211 static HRESULT compile_block_statement(compiler_ctx_t *ctx, block_statement_t *block, statement_t *iter)
1213 statement_ctx_t stat_ctx = {0, TRUE};
1214 BOOL needs_scope;
1215 HRESULT hres;
1217 needs_scope = block && block->scope_index;
1218 if (needs_scope)
1220 if(FAILED(hres = push_instr_uint(ctx, OP_push_block_scope, block->scope_index)))
1221 return hres;
1223 stat_ctx.scope_index = block->scope_index;
1224 stat_ctx.block_scope = TRUE;
1227 while(iter) {
1228 hres = compile_statement(ctx, needs_scope ? &stat_ctx : NULL, iter);
1229 if(FAILED(hres))
1230 return hres;
1232 iter = iter->next;
1235 if(needs_scope && !push_instr(ctx, OP_pop_scope))
1236 return E_OUTOFMEMORY;
1238 return S_OK;
1241 /* ECMA-262 3rd Edition 12.2 */
1242 static HRESULT compile_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
1244 variable_declaration_t *iter;
1245 HRESULT hres;
1247 assert(list != NULL);
1249 for(iter = list; iter; iter = iter->next) {
1250 if(!iter->expr)
1251 continue;
1253 if (iter->constant)
1254 FIXME("Constant variables are not supported.\n");
1256 hres = emit_identifier_ref(ctx, iter->identifier, 0);
1257 if(FAILED(hres))
1258 return hres;
1260 hres = compile_expression(ctx, iter->expr, TRUE);
1261 if(FAILED(hres))
1262 return hres;
1264 if(!push_instr(ctx, OP_assign))
1265 return E_OUTOFMEMORY;
1267 hres = push_instr_uint(ctx, OP_pop, 1);
1268 if(FAILED(hres))
1269 return hres;
1272 return S_OK;
1275 /* ECMA-262 3rd Edition 12.2 */
1276 static HRESULT compile_var_statement(compiler_ctx_t *ctx, var_statement_t *stat)
1278 return compile_variable_list(ctx, stat->variable_list);
1281 /* ECMA-262 3rd Edition 12.4 */
1282 static HRESULT compile_expression_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1284 HRESULT hres;
1286 hres = compile_expression(ctx, stat->expr, ctx->from_eval);
1287 if(FAILED(hres))
1288 return hres;
1290 return !ctx->from_eval || push_instr(ctx, OP_setret) ? S_OK : E_OUTOFMEMORY;
1293 /* ECMA-262 3rd Edition 12.5 */
1294 static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat)
1296 unsigned jmp_else;
1297 HRESULT hres;
1299 hres = compile_expression(ctx, stat->expr, TRUE);
1300 if(FAILED(hres))
1301 return hres;
1303 jmp_else = push_instr(ctx, OP_jmp_z);
1304 if(!jmp_else)
1305 return E_OUTOFMEMORY;
1307 hres = compile_statement(ctx, NULL, stat->if_stat);
1308 if(FAILED(hres))
1309 return hres;
1311 if(stat->else_stat) {
1312 unsigned jmp_end;
1314 jmp_end = push_instr(ctx, OP_jmp);
1315 if(!jmp_end)
1316 return E_OUTOFMEMORY;
1318 set_arg_uint(ctx, jmp_else, ctx->code_off);
1320 hres = compile_statement(ctx, NULL, stat->else_stat);
1321 if(FAILED(hres))
1322 return hres;
1324 set_arg_uint(ctx, jmp_end, ctx->code_off);
1325 }else {
1326 set_arg_uint(ctx, jmp_else, ctx->code_off);
1329 return S_OK;
1332 /* ECMA-262 3rd Edition 12.6.2 */
1333 static HRESULT compile_while_statement(compiler_ctx_t *ctx, while_statement_t *stat)
1335 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1336 unsigned jmp_off;
1337 HRESULT hres;
1339 stat_ctx.break_label = alloc_label(ctx);
1340 if(!stat_ctx.break_label)
1341 return E_OUTOFMEMORY;
1343 stat_ctx.continue_label = alloc_label(ctx);
1344 if(!stat_ctx.continue_label)
1345 return E_OUTOFMEMORY;
1347 jmp_off = ctx->code_off;
1349 if(!stat->do_while) {
1350 label_set_addr(ctx, stat_ctx.continue_label);
1351 hres = compile_expression(ctx, stat->expr, TRUE);
1352 if(FAILED(hres))
1353 return hres;
1355 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1356 if(FAILED(hres))
1357 return hres;
1360 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1361 if(FAILED(hres))
1362 return hres;
1364 set_compiler_loc(ctx, stat->stat.loc);
1365 if(stat->do_while) {
1366 label_set_addr(ctx, stat_ctx.continue_label);
1367 hres = compile_expression(ctx, stat->expr, TRUE);
1368 if(FAILED(hres))
1369 return hres;
1371 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1372 if(FAILED(hres))
1373 return hres;
1376 hres = push_instr_uint(ctx, OP_jmp, jmp_off);
1377 if(FAILED(hres))
1378 return hres;
1380 label_set_addr(ctx, stat_ctx.break_label);
1381 return S_OK;
1384 /* ECMA-262 10th Edition 13.7.4 */
1385 static HRESULT compile_for_statement(compiler_ctx_t *ctx, for_statement_t *stat)
1387 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1388 statement_ctx_t scope_stat_ctx = {0, TRUE};
1389 unsigned expr_off;
1390 HRESULT hres;
1392 if (stat->scope_index)
1394 if(FAILED(hres = push_instr_uint(ctx, OP_push_block_scope, stat->scope_index)))
1395 return hres;
1397 scope_stat_ctx.scope_index = stat->scope_index;
1398 scope_stat_ctx.block_scope = TRUE;
1399 push_compiler_statement_ctx(ctx, &scope_stat_ctx);
1402 if(stat->variable_list) {
1403 hres = compile_variable_list(ctx, stat->variable_list);
1404 if(FAILED(hres))
1405 goto done;
1406 }else if(stat->begin_expr) {
1407 hres = compile_expression(ctx, stat->begin_expr, FALSE);
1408 if(FAILED(hres))
1409 goto done;
1412 stat_ctx.break_label = alloc_label(ctx);
1413 if(!stat_ctx.break_label)
1415 hres = E_OUTOFMEMORY;
1416 goto done;
1419 stat_ctx.continue_label = alloc_label(ctx);
1420 if(!stat_ctx.continue_label)
1422 hres = E_OUTOFMEMORY;
1423 goto done;
1425 expr_off = ctx->code_off;
1427 if(stat->expr) {
1428 set_compiler_loc(ctx, stat->expr_loc);
1429 hres = compile_expression(ctx, stat->expr, TRUE);
1430 if(FAILED(hres))
1431 goto done;
1433 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1434 if(FAILED(hres))
1435 goto done;
1438 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1439 if(FAILED(hres))
1440 goto done;
1442 label_set_addr(ctx, stat_ctx.continue_label);
1444 if(stat->end_expr) {
1445 set_compiler_loc(ctx, stat->end_loc);
1446 hres = compile_expression(ctx, stat->end_expr, FALSE);
1447 if(FAILED(hres))
1448 goto done;
1451 hres = push_instr_uint(ctx, OP_jmp, expr_off);
1452 if(FAILED(hres))
1453 goto done;
1455 label_set_addr(ctx, stat_ctx.break_label);
1456 hres = S_OK;
1457 done:
1458 if (stat->scope_index)
1460 pop_compiler_statement_ctx(ctx, &scope_stat_ctx);
1461 if(SUCCEEDED(hres) && !push_instr(ctx, OP_pop_scope))
1462 return E_OUTOFMEMORY;
1464 return hres;
1467 /* ECMA-262 3rd Edition 12.6.4 */
1468 static HRESULT compile_forin_statement(compiler_ctx_t *ctx, forin_statement_t *stat)
1470 statement_ctx_t stat_ctx = {4, FALSE, FALSE};
1471 HRESULT hres;
1473 if(stat->variable) {
1474 hres = compile_variable_list(ctx, stat->variable);
1475 if(FAILED(hres))
1476 return hres;
1479 stat_ctx.break_label = alloc_label(ctx);
1480 if(!stat_ctx.break_label)
1481 return E_OUTOFMEMORY;
1483 stat_ctx.continue_label = alloc_label(ctx);
1484 if(!stat_ctx.continue_label)
1485 return E_OUTOFMEMORY;
1487 hres = compile_expression(ctx, stat->in_expr, TRUE);
1488 if(FAILED(hres))
1489 return hres;
1491 if(stat->variable) {
1492 hres = emit_identifier_ref(ctx, stat->variable->identifier, fdexNameEnsure);
1493 if(FAILED(hres))
1494 return hres;
1495 }else if(is_memberid_expr(stat->expr->type)) {
1496 hres = compile_memberid_expression(ctx, stat->expr, fdexNameEnsure);
1497 if(FAILED(hres))
1498 return hres;
1499 }else {
1500 hres = push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
1501 if(FAILED(hres))
1502 return hres;
1504 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1505 return S_OK;
1508 hres = push_instr_int(ctx, OP_int, DISPID_STARTENUM);
1509 if(FAILED(hres))
1510 return hres;
1512 label_set_addr(ctx, stat_ctx.continue_label);
1513 hres = push_instr_uint(ctx, OP_forin, stat_ctx.break_label);
1514 if(FAILED(hres))
1515 return E_OUTOFMEMORY;
1517 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1518 if(FAILED(hres))
1519 return hres;
1521 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.continue_label);
1522 if(FAILED(hres))
1523 return hres;
1525 label_set_addr(ctx, stat_ctx.break_label);
1526 return S_OK;
1529 static HRESULT pop_to_stat(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx)
1531 unsigned stack_pop = 0;
1532 statement_ctx_t *iter;
1533 HRESULT hres;
1535 for(iter = ctx->stat_ctx; iter != stat_ctx; iter = iter->next) {
1536 if(iter->using_scope && !push_instr(ctx, OP_pop_scope))
1537 return E_OUTOFMEMORY;
1538 if(iter->using_except) {
1539 if(stack_pop) {
1540 hres = push_instr_uint(ctx, OP_pop, stack_pop);
1541 if(FAILED(hres))
1542 return hres;
1543 stack_pop = 0;
1545 hres = push_instr_uint(ctx, OP_pop_except, ctx->code_off+1);
1546 if(FAILED(hres))
1547 return hres;
1549 stack_pop += iter->stack_use;
1552 if(stack_pop) {
1553 hres = push_instr_uint(ctx, OP_pop, stack_pop);
1554 if(FAILED(hres))
1555 return hres;
1558 return S_OK;
1561 /* ECMA-262 3rd Edition 12.7 */
1562 static HRESULT compile_continue_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1564 statement_ctx_t *pop_ctx;
1565 HRESULT hres;
1567 if(stat->identifier) {
1568 statement_t *label_stat;
1569 statement_ctx_t *iter;
1571 pop_ctx = NULL;
1573 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1574 if(iter->continue_label)
1575 pop_ctx = iter;
1576 if(iter->labelled_stat && !wcscmp(iter->labelled_stat->identifier, stat->identifier))
1577 break;
1580 if(!iter) {
1581 WARN("Label not found\n");
1582 return JS_E_LABEL_NOT_FOUND;
1585 /* Labelled continue are allowed only on loops */
1586 for(label_stat = iter->labelled_stat->statement;
1587 label_stat->type == STAT_LABEL;
1588 label_stat = ((labelled_statement_t*)label_stat)->statement);
1589 if(!is_loop_statement(label_stat->type)) {
1590 WARN("Label is not a loop\n");
1591 return JS_E_INVALID_CONTINUE;
1594 assert(pop_ctx != NULL);
1595 }else {
1596 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1597 if(pop_ctx->continue_label)
1598 break;
1601 if(!pop_ctx) {
1602 WARN("continue outside loop\n");
1603 return JS_E_INVALID_CONTINUE;
1607 hres = pop_to_stat(ctx, pop_ctx);
1608 if(FAILED(hres))
1609 return hres;
1611 return push_instr_uint(ctx, OP_jmp, pop_ctx->continue_label);
1614 /* ECMA-262 3rd Edition 12.8 */
1615 static HRESULT compile_break_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1617 statement_ctx_t *pop_ctx;
1618 HRESULT hres;
1620 if(stat->identifier) {
1621 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1622 if(pop_ctx->labelled_stat && !wcscmp(pop_ctx->labelled_stat->identifier, stat->identifier)) {
1623 assert(pop_ctx->break_label);
1624 break;
1628 if(!pop_ctx) {
1629 WARN("Label not found\n");
1630 return JS_E_LABEL_NOT_FOUND;
1632 }else {
1633 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1634 if(pop_ctx->break_label && !pop_ctx->labelled_stat)
1635 break;
1638 if(!pop_ctx) {
1639 WARN("Break outside loop\n");
1640 return JS_E_INVALID_BREAK;
1644 hres = pop_to_stat(ctx, pop_ctx->next);
1645 if(FAILED(hres))
1646 return hres;
1648 return push_instr_uint(ctx, OP_jmp, pop_ctx->break_label);
1651 /* ECMA-262 3rd Edition 12.9 */
1652 static HRESULT compile_return_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1654 HRESULT hres;
1656 if(ctx->from_eval) {
1657 WARN("misplaced return statement\n");
1658 return JS_E_MISPLACED_RETURN;
1661 if(stat->expr) {
1662 hres = compile_expression(ctx, stat->expr, TRUE);
1663 if(FAILED(hres))
1664 return hres;
1665 if(!push_instr(ctx, OP_setret))
1666 return E_OUTOFMEMORY;
1669 hres = pop_to_stat(ctx, NULL);
1670 if(FAILED(hres))
1671 return hres;
1673 return push_instr_uint(ctx, OP_ret, !stat->expr);
1676 /* ECMA-262 3rd Edition 12.10 */
1677 static HRESULT compile_with_statement(compiler_ctx_t *ctx, with_statement_t *stat)
1679 statement_ctx_t stat_ctx = {0, TRUE, FALSE};
1680 HRESULT hres;
1682 hres = compile_expression(ctx, stat->expr, TRUE);
1683 if(FAILED(hres))
1684 return hres;
1686 if(!push_instr(ctx, OP_push_with_scope))
1687 return E_OUTOFMEMORY;
1689 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1690 if(FAILED(hres))
1691 return hres;
1693 if(!push_instr(ctx, OP_pop_scope))
1694 return E_OUTOFMEMORY;
1696 return S_OK;
1699 /* ECMA-262 3rd Edition 12.10 */
1700 static HRESULT compile_labelled_statement(compiler_ctx_t *ctx, labelled_statement_t *stat)
1702 statement_ctx_t stat_ctx = {0, FALSE, FALSE, 0, 0, stat}, *iter;
1703 HRESULT hres;
1705 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1706 if(iter->labelled_stat && !wcscmp(iter->labelled_stat->identifier, stat->identifier)) {
1707 WARN("Label %s redefined\n", debugstr_w(stat->identifier));
1708 return JS_E_LABEL_REDEFINED;
1712 /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1713 stat_ctx.break_label = alloc_label(ctx);
1714 if(!stat_ctx.break_label)
1715 return E_OUTOFMEMORY;
1717 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1718 if(FAILED(hres))
1719 return hres;
1721 label_set_addr(ctx, stat_ctx.break_label);
1722 return S_OK;
1725 /* ECMA-262 3rd Edition 12.13 */
1726 static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat)
1728 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1729 unsigned case_cnt = 0, *case_jmps, i, default_jmp;
1730 BOOL have_default = FALSE;
1731 statement_t *stat_iter;
1732 case_clausule_t *iter;
1733 HRESULT hres;
1735 hres = compile_expression(ctx, stat->expr, TRUE);
1736 if(FAILED(hres))
1737 return hres;
1739 stat_ctx.break_label = alloc_label(ctx);
1740 if(!stat_ctx.break_label)
1741 return E_OUTOFMEMORY;
1743 for(iter = stat->case_list; iter; iter = iter->next) {
1744 if(iter->expr)
1745 case_cnt++;
1748 case_jmps = malloc(case_cnt * sizeof(*case_jmps));
1749 if(!case_jmps)
1750 return E_OUTOFMEMORY;
1752 i = 0;
1753 for(iter = stat->case_list; iter; iter = iter->next) {
1754 if(!iter->expr) {
1755 have_default = TRUE;
1756 continue;
1759 set_compiler_loc(ctx, iter->loc);
1760 hres = compile_expression(ctx, iter->expr, TRUE);
1761 if(FAILED(hres))
1762 break;
1764 case_jmps[i] = push_instr(ctx, OP_case);
1765 if(!case_jmps[i]) {
1766 hres = E_OUTOFMEMORY;
1767 break;
1769 i++;
1772 if(SUCCEEDED(hres)) {
1773 hres = push_instr_uint(ctx, OP_pop, 1);
1774 if(SUCCEEDED(hres)) {
1775 default_jmp = push_instr(ctx, OP_jmp);
1776 if(!default_jmp)
1777 hres = E_OUTOFMEMORY;
1781 if(FAILED(hres)) {
1782 free(case_jmps);
1783 return hres;
1786 i = 0;
1787 for(iter = stat->case_list; iter; iter = iter->next) {
1788 while(iter->next && iter->next->stat == iter->stat) {
1789 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1790 iter = iter->next;
1793 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1795 for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter);
1796 stat_iter = stat_iter->next) {
1797 hres = compile_statement(ctx, &stat_ctx, stat_iter);
1798 if(FAILED(hres))
1799 break;
1801 if(FAILED(hres))
1802 break;
1805 free(case_jmps);
1806 if(FAILED(hres))
1807 return hres;
1808 assert(i == case_cnt);
1810 if(!have_default) {
1811 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.break_label);
1812 if(FAILED(hres))
1813 return hres;
1814 set_arg_uint(ctx, default_jmp, ctx->code_off);
1817 label_set_addr(ctx, stat_ctx.break_label);
1818 return S_OK;
1821 /* ECMA-262 3rd Edition 12.13 */
1822 static HRESULT compile_throw_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1824 HRESULT hres;
1826 hres = compile_expression(ctx, stat->expr, TRUE);
1827 if(FAILED(hres))
1828 return hres;
1830 return push_instr(ctx, OP_throw) ? S_OK : E_OUTOFMEMORY;
1833 /* ECMA-262 3rd Edition 12.14 */
1834 static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat)
1836 statement_ctx_t try_ctx = {0, FALSE, TRUE}, finally_ctx = {2, FALSE, FALSE};
1837 unsigned push_except, finally_off = 0, catch_off = 0, pop_except, catch_pop_except = 0;
1838 BSTR ident;
1839 HRESULT hres;
1841 push_except = push_instr(ctx, OP_push_except);
1842 if(!push_except)
1843 return E_OUTOFMEMORY;
1845 if(stat->catch_block) {
1846 ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier);
1847 if(!ident)
1848 return E_OUTOFMEMORY;
1849 }else {
1850 ident = NULL;
1853 hres = compile_statement(ctx, &try_ctx, stat->try_statement);
1854 if(FAILED(hres))
1855 return hres;
1857 pop_except = push_instr(ctx, OP_pop_except);
1858 if(!pop_except)
1859 return E_OUTOFMEMORY;
1861 if(stat->catch_block) {
1862 statement_ctx_t catch_ctx = {0, TRUE, stat->finally_statement != NULL};
1864 if(stat->finally_statement)
1865 catch_ctx.using_except = TRUE;
1867 catch_off = ctx->code_off;
1869 hres = push_instr_bstr(ctx, OP_enter_catch, ident);
1870 if(FAILED(hres))
1871 return hres;
1873 hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement);
1874 if(FAILED(hres))
1875 return hres;
1877 if(!push_instr(ctx, OP_pop_scope))
1878 return E_OUTOFMEMORY;
1880 if(stat->finally_statement) {
1881 catch_pop_except = push_instr(ctx, OP_pop_except);
1882 if(!catch_pop_except)
1883 return E_OUTOFMEMORY;
1887 if(stat->finally_statement) {
1889 * finally block expects two elements on the stack, which may be:
1890 * - (true, return_addr) set by OP_pop_except, OP_end_finally jumps back to passed address
1891 * - (false, exception_value) set when unwinding an exception, which OP_end_finally rethrows
1893 finally_off = ctx->code_off;
1894 hres = compile_statement(ctx, &finally_ctx, stat->finally_statement);
1895 if(FAILED(hres))
1896 return hres;
1898 set_compiler_loc(ctx, stat->finally_loc);
1899 if(!push_instr(ctx, OP_end_finally))
1900 return E_OUTOFMEMORY;
1903 instr_ptr(ctx, pop_except)->u.arg[0].uint = ctx->code_off;
1904 if(catch_pop_except)
1905 instr_ptr(ctx, catch_pop_except)->u.arg[0].uint = ctx->code_off;
1906 instr_ptr(ctx, push_except)->u.arg[0].uint = catch_off;
1907 instr_ptr(ctx, push_except)->u.arg[1].uint = finally_off;
1908 return S_OK;
1911 static HRESULT compile_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1913 HRESULT hres;
1915 push_compiler_statement_ctx(ctx, stat_ctx);
1917 set_compiler_loc(ctx, stat->loc);
1919 switch(stat->type) {
1920 case STAT_BLOCK:
1921 hres = compile_block_statement(ctx, (block_statement_t*)stat, ((block_statement_t*)stat)->stat_list);
1922 break;
1923 case STAT_BREAK:
1924 hres = compile_break_statement(ctx, (branch_statement_t*)stat);
1925 break;
1926 case STAT_CONTINUE:
1927 hres = compile_continue_statement(ctx, (branch_statement_t*)stat);
1928 break;
1929 case STAT_EMPTY:
1930 /* nothing to do */
1931 hres = S_OK;
1932 break;
1933 case STAT_EXPR:
1934 hres = compile_expression_statement(ctx, (expression_statement_t*)stat);
1935 break;
1936 case STAT_FOR:
1937 hres = compile_for_statement(ctx, (for_statement_t*)stat);
1938 break;
1939 case STAT_FORIN:
1940 hres = compile_forin_statement(ctx, (forin_statement_t*)stat);
1941 break;
1942 case STAT_IF:
1943 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1944 break;
1945 case STAT_LABEL:
1946 hres = compile_labelled_statement(ctx, (labelled_statement_t*)stat);
1947 break;
1948 case STAT_RETURN:
1949 hres = compile_return_statement(ctx, (expression_statement_t*)stat);
1950 break;
1951 case STAT_SWITCH:
1952 hres = compile_switch_statement(ctx, (switch_statement_t*)stat);
1953 break;
1954 case STAT_THROW:
1955 hres = compile_throw_statement(ctx, (expression_statement_t*)stat);
1956 break;
1957 case STAT_TRY:
1958 hres = compile_try_statement(ctx, (try_statement_t*)stat);
1959 break;
1960 case STAT_VAR:
1961 hres = compile_var_statement(ctx, (var_statement_t*)stat);
1962 break;
1963 case STAT_WHILE:
1964 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1965 break;
1966 case STAT_WITH:
1967 hres = compile_with_statement(ctx, (with_statement_t*)stat);
1968 break;
1969 DEFAULT_UNREACHABLE;
1972 pop_compiler_statement_ctx(ctx, stat_ctx);
1974 return hres;
1977 static inline function_local_t *find_local(compiler_ctx_t *ctx, const WCHAR *name, unsigned int scope)
1979 struct wine_rb_entry *entry = wine_rb_get(&ctx->local_scopes[scope].locals, name);
1980 return entry ? WINE_RB_ENTRY_VALUE(entry, function_local_t, entry) : NULL;
1983 static BOOL alloc_local(compiler_ctx_t *ctx, BSTR name, int ref, unsigned int scope)
1985 function_local_t *local;
1987 local = heap_pool_alloc(&ctx->heap, sizeof(*local));
1988 if(!local)
1989 return FALSE;
1991 local->name = name;
1992 local->ref = ref;
1993 wine_rb_put(&ctx->local_scopes[scope].locals, name, &local->entry);
1994 ctx->local_scopes[scope].locals_cnt++;
1995 return TRUE;
1998 static BOOL alloc_variable(compiler_ctx_t *ctx, const WCHAR *name, unsigned int scope)
2000 BSTR ident;
2002 if(find_local(ctx, name, scope))
2003 return TRUE;
2005 ident = compiler_alloc_bstr(ctx, name);
2006 if(!ident)
2007 return FALSE;
2009 return alloc_local(ctx, ident, ctx->func->var_cnt++, scope);
2012 static HRESULT visit_function_expression(compiler_ctx_t *ctx, function_expression_t *expr)
2014 statement_ctx_t *stat_ctx;
2016 expr->func_id = ctx->func->func_cnt++;
2017 ctx->func_tail = ctx->func_tail ? (ctx->func_tail->next = expr) : (ctx->func_head = expr);
2019 if(!expr->identifier || expr->event_target)
2020 return S_OK;
2022 for (stat_ctx = ctx->stat_ctx; stat_ctx; stat_ctx = stat_ctx->next)
2024 if (stat_ctx->block_scope)
2026 stat_ctx->scope_has_functions = TRUE;
2027 break;
2031 if(!expr->is_statement && ctx->parser->script->version >= SCRIPTLANGUAGEVERSION_ES5)
2032 return S_OK;
2034 return alloc_variable(ctx, expr->identifier, stat_ctx ? stat_ctx->scope_index : 0) ? S_OK : E_OUTOFMEMORY;
2037 static HRESULT visit_expression(compiler_ctx_t *ctx, expression_t *expr)
2039 HRESULT hres = S_OK;
2041 switch(expr->type) {
2042 case EXPR_ADD:
2043 case EXPR_AND:
2044 case EXPR_ARRAY:
2045 case EXPR_ASSIGN:
2046 case EXPR_ASSIGNADD:
2047 case EXPR_ASSIGNAND:
2048 case EXPR_ASSIGNSUB:
2049 case EXPR_ASSIGNMUL:
2050 case EXPR_ASSIGNDIV:
2051 case EXPR_ASSIGNMOD:
2052 case EXPR_ASSIGNOR:
2053 case EXPR_ASSIGNLSHIFT:
2054 case EXPR_ASSIGNRSHIFT:
2055 case EXPR_ASSIGNRRSHIFT:
2056 case EXPR_ASSIGNXOR:
2057 case EXPR_BAND:
2058 case EXPR_BOR:
2059 case EXPR_COMMA:
2060 case EXPR_DIV:
2061 case EXPR_EQ:
2062 case EXPR_EQEQ:
2063 case EXPR_GREATER:
2064 case EXPR_GREATEREQ:
2065 case EXPR_IN:
2066 case EXPR_INSTANCEOF:
2067 case EXPR_LESS:
2068 case EXPR_LESSEQ:
2069 case EXPR_LSHIFT:
2070 case EXPR_MOD:
2071 case EXPR_MUL:
2072 case EXPR_NOTEQ:
2073 case EXPR_NOTEQEQ:
2074 case EXPR_OR:
2075 case EXPR_RSHIFT:
2076 case EXPR_RRSHIFT:
2077 case EXPR_SUB:
2078 case EXPR_BXOR: {
2079 binary_expression_t *binary_expr = (binary_expression_t*)expr;
2081 hres = visit_expression(ctx, binary_expr->expression1);
2082 if(FAILED(hres))
2083 return hres;
2085 hres = visit_expression(ctx, binary_expr->expression2);
2086 break;
2088 case EXPR_BITNEG:
2089 case EXPR_DELETE:
2090 case EXPR_LOGNEG:
2091 case EXPR_MINUS:
2092 case EXPR_PLUS:
2093 case EXPR_POSTDEC:
2094 case EXPR_POSTINC:
2095 case EXPR_PREDEC:
2096 case EXPR_PREINC:
2097 case EXPR_TYPEOF:
2098 case EXPR_VOID:
2099 hres = visit_expression(ctx, ((unary_expression_t*)expr)->expression);
2100 break;
2101 case EXPR_IDENT:
2102 case EXPR_LITERAL:
2103 case EXPR_THIS:
2104 break;
2105 case EXPR_ARRAYLIT: {
2106 array_literal_expression_t *array_expr = (array_literal_expression_t*)expr;
2107 array_element_t *iter;
2109 for(iter = array_expr->element_list; iter; iter = iter->next) {
2110 hres = visit_expression(ctx, iter->expr);
2111 if(FAILED(hres))
2112 return hres;
2114 break;
2116 case EXPR_CALL:
2117 case EXPR_NEW: {
2118 call_expression_t *call_expr = (call_expression_t*)expr;
2119 argument_t *arg;
2121 hres = visit_expression(ctx, call_expr->expression);
2122 if(FAILED(hres))
2123 return hres;
2125 for(arg = call_expr->argument_list; arg; arg = arg->next) {
2126 hres = visit_expression(ctx, arg->expr);
2127 if(FAILED(hres))
2128 return hres;
2130 break;
2132 case EXPR_COND: {
2133 conditional_expression_t *cond_expr = (conditional_expression_t*)expr;
2135 hres = visit_expression(ctx, cond_expr->expression);
2136 if(FAILED(hres))
2137 return hres;
2139 hres = visit_expression(ctx, cond_expr->true_expression);
2140 if(FAILED(hres))
2141 return hres;
2143 hres = visit_expression(ctx, cond_expr->false_expression);
2144 break;
2146 case EXPR_FUNC:
2147 hres = visit_function_expression(ctx, (function_expression_t*)expr);
2148 break;
2149 case EXPR_MEMBER:
2150 hres = visit_expression(ctx, ((member_expression_t*)expr)->expression);
2151 break;
2152 case EXPR_PROPVAL: {
2153 property_definition_t *iter;
2154 for(iter = ((property_value_expression_t*)expr)->property_list; iter; iter = iter->next) {
2155 hres = visit_expression(ctx, iter->value);
2156 if(FAILED(hres))
2157 return hres;
2159 break;
2161 DEFAULT_UNREACHABLE;
2164 return hres;
2167 static HRESULT visit_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
2169 variable_declaration_t *iter;
2170 statement_ctx_t *stat_ctx;
2171 HRESULT hres;
2173 for(iter = list; iter; iter = iter->next) {
2174 for (stat_ctx = ctx->stat_ctx; stat_ctx; stat_ctx = stat_ctx->next)
2176 if (stat_ctx->block_scope)
2177 break;
2180 if(!alloc_variable(ctx, iter->identifier, iter->block_scope && stat_ctx ? stat_ctx->scope_index : 0))
2181 return E_OUTOFMEMORY;
2183 if(iter->expr) {
2184 hres = visit_expression(ctx, iter->expr);
2185 if(FAILED(hres))
2186 return hres;
2190 return S_OK;
2193 static HRESULT visit_statement(compiler_ctx_t*,statement_ctx_t *,statement_t*);
2195 static HRESULT visit_block_statement(compiler_ctx_t *ctx, block_statement_t *block, statement_t *iter)
2197 statement_ctx_t stat_ctx = {0, TRUE};
2198 BOOL needs_scope;
2199 HRESULT hres;
2201 needs_scope = block && ctx->parser->script->version >= SCRIPTLANGUAGEVERSION_ES5;
2202 if (needs_scope)
2204 if (!alloc_local_scope(ctx, &block->scope_index))
2205 return E_OUTOFMEMORY;
2207 stat_ctx.scope_index = block->scope_index;
2208 stat_ctx.block_scope = TRUE;
2211 while(iter) {
2212 hres = visit_statement(ctx, needs_scope ? &stat_ctx : NULL, iter);
2213 if(FAILED(hres))
2214 return hres;
2216 iter = iter->next;
2219 if (needs_scope && !(ctx->local_scopes[stat_ctx.scope_index].locals_cnt || stat_ctx.scope_has_functions))
2220 remove_local_scope(ctx, block->scope_index);
2222 return S_OK;
2225 static HRESULT visit_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
2227 HRESULT hres = S_OK;
2229 push_compiler_statement_ctx(ctx, stat_ctx);
2231 switch(stat->type) {
2232 case STAT_BLOCK:
2233 hres = visit_block_statement(ctx, (block_statement_t*)stat, ((block_statement_t*)stat)->stat_list);
2234 break;
2235 case STAT_BREAK:
2236 case STAT_CONTINUE:
2237 case STAT_EMPTY:
2238 break;
2239 case STAT_EXPR: {
2240 expression_statement_t *expr_stat = (expression_statement_t*)stat;
2241 if(expr_stat->expr) {
2242 if(expr_stat->expr->type == EXPR_FUNC)
2243 ((function_expression_t*)expr_stat->expr)->is_statement = TRUE;
2244 hres = visit_expression(ctx, expr_stat->expr);
2246 break;
2248 case STAT_RETURN:
2249 case STAT_THROW: {
2250 expression_statement_t *expr_stat = (expression_statement_t*)stat;
2251 if(expr_stat->expr)
2252 hres = visit_expression(ctx, expr_stat->expr);
2253 break;
2255 case STAT_FOR: {
2256 statement_ctx_t stat_ctx_data = {0, TRUE}, *stat_ctx = NULL;
2257 for_statement_t *for_stat = (for_statement_t*)stat;
2259 if(for_stat->variable_list)
2261 variable_declaration_t *var;
2263 for(var = for_stat->variable_list; var; var = var->next)
2265 if (var->block_scope)
2267 stat_ctx = &stat_ctx_data;
2268 break;
2272 if (stat_ctx)
2274 if (!alloc_local_scope(ctx, &for_stat->scope_index))
2276 hres = E_OUTOFMEMORY;
2277 break;
2279 stat_ctx->scope_index = for_stat->scope_index;
2280 stat_ctx->block_scope = TRUE;
2281 push_compiler_statement_ctx(ctx, stat_ctx);
2283 hres = visit_variable_list(ctx, for_stat->variable_list);
2285 else if(for_stat->begin_expr)
2286 hres = visit_expression(ctx, for_stat->begin_expr);
2287 if(FAILED(hres))
2289 pop_compiler_statement_ctx(ctx, stat_ctx);
2290 break;
2293 if(for_stat->expr) {
2294 hres = visit_expression(ctx, for_stat->expr);
2295 if(FAILED(hres))
2297 pop_compiler_statement_ctx(ctx, stat_ctx);
2298 break;
2302 hres = visit_statement(ctx, NULL, for_stat->statement);
2303 if(FAILED(hres))
2305 pop_compiler_statement_ctx(ctx, stat_ctx);
2306 break;
2308 if(for_stat->end_expr)
2309 hres = visit_expression(ctx, for_stat->end_expr);
2310 pop_compiler_statement_ctx(ctx, stat_ctx);
2311 break;
2313 case STAT_FORIN: {
2314 forin_statement_t *forin_stat = (forin_statement_t*)stat;
2316 if(forin_stat->variable) {
2317 hres = visit_variable_list(ctx, forin_stat->variable);
2318 if(FAILED(hres))
2319 break;
2322 hres = visit_expression(ctx, forin_stat->in_expr);
2323 if(FAILED(hres))
2324 return hres;
2326 if(forin_stat->expr) {
2327 hres = visit_expression(ctx, forin_stat->expr);
2328 if(FAILED(hres))
2329 return hres;
2332 hres = visit_statement(ctx, NULL, forin_stat->statement);
2333 break;
2335 case STAT_IF: {
2336 if_statement_t *if_stat = (if_statement_t*)stat;
2338 hres = visit_expression(ctx, if_stat->expr);
2339 if(FAILED(hres))
2340 return hres;
2342 hres = visit_statement(ctx, NULL, if_stat->if_stat);
2343 if(FAILED(hres))
2344 return hres;
2346 if(if_stat->else_stat)
2347 hres = visit_statement(ctx, NULL, if_stat->else_stat);
2348 break;
2350 case STAT_LABEL:
2351 hres = visit_statement(ctx, NULL, ((labelled_statement_t*)stat)->statement);
2352 break;
2353 case STAT_SWITCH: {
2354 switch_statement_t *switch_stat = (switch_statement_t*)stat;
2355 statement_t *stat_iter;
2356 case_clausule_t *iter;
2358 hres = visit_expression(ctx, switch_stat->expr);
2359 if(FAILED(hres))
2360 return hres;
2362 for(iter = switch_stat->case_list; iter; iter = iter->next) {
2363 if(!iter->expr)
2364 continue;
2365 hres = visit_expression(ctx, iter->expr);
2366 if(FAILED(hres))
2367 return hres;
2370 for(iter = switch_stat->case_list; iter; iter = iter->next) {
2371 while(iter->next && iter->next->stat == iter->stat)
2372 iter = iter->next;
2373 for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter);
2374 stat_iter = stat_iter->next) {
2375 hres = visit_statement(ctx, NULL, stat_iter);
2376 if(FAILED(hres))
2377 return hres;
2380 break;
2382 case STAT_TRY: {
2383 try_statement_t *try_stat = (try_statement_t*)stat;
2385 hres = visit_statement(ctx, NULL, try_stat->try_statement);
2386 if(FAILED(hres))
2387 return hres;
2389 if(try_stat->catch_block) {
2390 hres = visit_statement(ctx, NULL, try_stat->catch_block->statement);
2391 if(FAILED(hres))
2392 return hres;
2395 if(try_stat->finally_statement)
2396 hres = visit_statement(ctx, NULL, try_stat->finally_statement);
2397 break;
2399 case STAT_VAR:
2400 hres = visit_variable_list(ctx, ((var_statement_t*)stat)->variable_list);
2401 break;
2402 case STAT_WHILE: {
2403 while_statement_t *while_stat = (while_statement_t*)stat;
2405 hres = visit_expression(ctx, while_stat->expr);
2406 if(FAILED(hres))
2407 return hres;
2409 hres = visit_statement(ctx, NULL, while_stat->statement);
2410 break;
2412 case STAT_WITH: {
2413 with_statement_t *with_stat = (with_statement_t*)stat;
2415 hres = visit_expression(ctx, with_stat->expr);
2416 if(FAILED(hres))
2417 return hres;
2419 hres = visit_statement(ctx, NULL, with_stat->statement);
2420 break;
2422 DEFAULT_UNREACHABLE;
2425 pop_compiler_statement_ctx(ctx, stat_ctx);
2427 return hres;
2430 static void resolve_labels(compiler_ctx_t *ctx, unsigned off)
2432 instr_t *instr;
2434 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
2435 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->u.arg->uint & LABEL_FLAG)) {
2436 assert((instr->u.arg->uint & ~LABEL_FLAG) < ctx->labels_cnt);
2437 instr->u.arg->uint = ctx->labels[instr->u.arg->uint & ~LABEL_FLAG];
2439 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
2442 ctx->labels_cnt = 0;
2445 unsigned get_location_line(bytecode_t *code, unsigned loc, unsigned *char_pos)
2447 unsigned line = code->start_line;
2448 const WCHAR *nl, *p;
2450 for(nl = p = code->source; p < code->source + loc; p++) {
2451 if(*p != '\n') continue;
2452 line++;
2453 nl = p + 1;
2455 *char_pos = loc - (nl - code->source);
2456 return line;
2459 void release_bytecode(bytecode_t *code)
2461 unsigned i;
2463 if(--code->ref)
2464 return;
2466 for(i=0; i < code->bstr_cnt; i++)
2467 SysFreeString(code->bstr_pool[i]);
2468 for(i=0; i < code->str_cnt; i++)
2469 jsstr_release(code->str_pool[i]);
2471 if(code->named_item)
2472 release_named_item(code->named_item);
2473 free(code->source);
2474 heap_pool_free(&code->heap);
2475 free(code->bstr_pool);
2476 free(code->str_pool);
2477 free(code->instrs);
2478 free(code);
2481 static HRESULT init_code(compiler_ctx_t *compiler, const WCHAR *source, UINT64 source_context, unsigned start_line)
2483 size_t len = source ? lstrlenW(source) : 0;
2485 if(len > INT32_MAX)
2486 return E_OUTOFMEMORY;
2488 compiler->code = calloc(1, sizeof(bytecode_t));
2489 if(!compiler->code)
2490 return E_OUTOFMEMORY;
2492 compiler->code->ref = 1;
2493 compiler->code->source_context = source_context;
2494 compiler->code->start_line = start_line;
2495 heap_pool_init(&compiler->code->heap);
2497 compiler->code->source = malloc((len + 1) * sizeof(WCHAR));
2498 if(!compiler->code->source) {
2499 release_bytecode(compiler->code);
2500 return E_OUTOFMEMORY;
2502 if(len)
2503 memcpy(compiler->code->source, source, len * sizeof(WCHAR));
2504 compiler->code->source[len] = 0;
2506 compiler->code->instrs = malloc(64 * sizeof(instr_t));
2507 if(!compiler->code->instrs) {
2508 release_bytecode(compiler->code);
2509 return E_OUTOFMEMORY;
2512 compiler->code_size = 64;
2513 compiler->code_off = 1;
2514 return S_OK;
2517 static HRESULT compile_function(compiler_ctx_t *ctx, statement_t *source, function_expression_t *func_expr,
2518 BOOL from_eval, function_code_t *func)
2520 function_expression_t *iter;
2521 function_local_t *local;
2522 unsigned off, i, scope;
2523 HRESULT hres;
2525 TRACE("\n");
2527 func->bytecode = ctx->code;
2528 func->local_ref = INVALID_LOCAL_REF;
2529 func->scope_index = 0;
2530 ctx->func_head = ctx->func_tail = NULL;
2531 ctx->from_eval = from_eval;
2532 ctx->func = func;
2533 ctx->local_scope_count = 0;
2534 if (!alloc_local_scope(ctx, &scope))
2535 return E_OUTOFMEMORY;
2536 assert(!scope);
2538 if(func_expr) {
2539 parameter_t *param_iter;
2541 if(func_expr->identifier) {
2542 func->name = compiler_alloc_bstr(ctx, func_expr->identifier);
2543 if(!func->name)
2544 return E_OUTOFMEMORY;
2547 if(func_expr->event_target) {
2548 func->event_target = compiler_alloc_bstr(ctx, func_expr->event_target);
2549 if(!func->event_target)
2550 return E_OUTOFMEMORY;
2553 func->source = func_expr->src_str;
2554 func->source_len = func_expr->src_len;
2556 for(param_iter = func_expr->parameter_list; param_iter; param_iter = param_iter->next)
2557 func->param_cnt++;
2559 func->params = compiler_alloc(ctx->code, func->param_cnt * sizeof(*func->params));
2560 if(!func->params)
2561 return E_OUTOFMEMORY;
2563 for(param_iter = func_expr->parameter_list, i=0; param_iter; param_iter = param_iter->next, i++) {
2564 func->params[i] = compiler_alloc_bstr(ctx, param_iter->identifier);
2565 if(!func->params[i])
2566 return E_OUTOFMEMORY;
2570 for(i = func->param_cnt; i--;) {
2571 if(!find_local(ctx, func->params[i], 0) && !alloc_local(ctx, func->params[i], -i-1, 0))
2572 return E_OUTOFMEMORY;
2575 hres = visit_block_statement(ctx, NULL, source);
2576 if(FAILED(hres))
2577 return hres;
2579 func->local_scope_count = ctx->local_scope_count;
2580 func->local_scopes = compiler_alloc(ctx->code, func->local_scope_count * sizeof(*func->local_scopes));
2581 if(!func->local_scopes)
2582 return E_OUTOFMEMORY;
2584 func->variables = compiler_alloc(ctx->code, func->var_cnt * sizeof(*func->variables));
2585 if(!func->variables)
2586 return E_OUTOFMEMORY;
2588 for (scope = 0; scope < func->local_scope_count; ++scope)
2590 func->local_scopes[scope].locals = compiler_alloc(ctx->code,
2591 ctx->local_scopes[scope].locals_cnt * sizeof(*func->local_scopes[scope].locals));
2592 if(!func->local_scopes[scope].locals)
2593 return E_OUTOFMEMORY;
2594 func->local_scopes[scope].locals_cnt = ctx->local_scopes[scope].locals_cnt;
2596 i = 0;
2597 WINE_RB_FOR_EACH_ENTRY(local, &ctx->local_scopes[scope].locals, function_local_t, entry) {
2598 func->local_scopes[scope].locals[i].name = local->name;
2599 func->local_scopes[scope].locals[i].ref = local->ref;
2600 if(local->ref >= 0) {
2601 func->variables[local->ref].name = local->name;
2602 func->variables[local->ref].func_id = -1;
2604 i++;
2606 assert(i == ctx->local_scopes[scope].locals_cnt);
2609 func->funcs = compiler_alloc(ctx->code, func->func_cnt * sizeof(*func->funcs));
2610 if(!func->funcs)
2611 return E_OUTOFMEMORY;
2612 memset(func->funcs, 0, func->func_cnt * sizeof(*func->funcs));
2614 ctx->current_function_expr = ctx->func_head;
2615 off = ctx->code_off;
2616 hres = compile_block_statement(ctx, NULL, source);
2617 if(FAILED(hres))
2618 return hres;
2620 resolve_labels(ctx, off);
2622 hres = push_instr_uint(ctx, OP_ret, !from_eval);
2623 if(FAILED(hres))
2624 return hres;
2626 if(TRACE_ON(jscript_disas))
2627 dump_code(ctx, off);
2629 func->instr_off = off;
2631 for(iter = ctx->func_head, i=0; iter; iter = iter->next, i++) {
2632 hres = compile_function(ctx, iter->statement_list, iter, FALSE, func->funcs+i);
2633 if(FAILED(hres))
2634 return hres;
2636 func->funcs[i].scope_index = iter->scope_index;
2638 TRACE("[%d] func %s, scope_index %u\n", i, debugstr_w(func->funcs[i].name), iter->scope_index);
2639 if((ctx->parser->script->version < SCRIPTLANGUAGEVERSION_ES5 || iter->is_statement) &&
2640 func->funcs[i].name && !func->funcs[i].event_target) {
2641 local_ref_t *local_ref = lookup_local(func, func->funcs[i].name, func->funcs[i].scope_index);
2643 func->funcs[i].local_ref = local_ref->ref;
2644 TRACE("found ref %s %d for %s\n", debugstr_w(local_ref->name), local_ref->ref, debugstr_w(func->funcs[i].name));
2645 if(local_ref->ref >= 0)
2646 func->variables[local_ref->ref].func_id = i;
2650 assert(i == func->func_cnt);
2652 return S_OK;
2655 static HRESULT parse_arguments(compiler_ctx_t *ctx, const WCHAR *args, BSTR *arg_array, unsigned *args_size)
2657 const WCHAR *ptr = args, *ptr2;
2658 unsigned arg_cnt = 0;
2660 while(iswspace(*ptr))
2661 ptr++;
2662 if(!*ptr) {
2663 if(args_size)
2664 *args_size = 0;
2665 return S_OK;
2668 while(1) {
2669 if(!iswalpha(*ptr) && *ptr != '_') {
2670 FIXME("expected alpha or '_': %s\n", debugstr_w(ptr));
2671 return E_FAIL;
2674 ptr2 = ptr;
2675 while(iswalnum(*ptr) || *ptr == '_')
2676 ptr++;
2678 if(*ptr && *ptr != ',' && !iswspace(*ptr)) {
2679 FIXME("unexpected har %s\n", debugstr_w(ptr));
2680 return E_FAIL;
2683 if(arg_array) {
2684 arg_array[arg_cnt] = compiler_alloc_bstr_len(ctx, ptr2, ptr-ptr2);
2685 if(!arg_array[arg_cnt])
2686 return E_OUTOFMEMORY;
2688 arg_cnt++;
2690 while(iswspace(*ptr))
2691 ptr++;
2692 if(!*ptr)
2693 break;
2694 if(*ptr != ',') {
2695 FIXME("expected ',': %s\n", debugstr_w(ptr));
2696 return E_FAIL;
2699 ptr++;
2700 while(iswspace(*ptr))
2701 ptr++;
2704 if(args_size)
2705 *args_size = arg_cnt;
2706 return S_OK;
2709 static HRESULT compile_arguments(compiler_ctx_t *ctx, const WCHAR *args)
2711 HRESULT hres;
2713 hres = parse_arguments(ctx, args, NULL, &ctx->code->global_code.param_cnt);
2714 if(FAILED(hres))
2715 return hres;
2717 ctx->code->global_code.params = compiler_alloc(ctx->code,
2718 ctx->code->global_code.param_cnt * sizeof(*ctx->code->global_code.params));
2719 if(!ctx->code->global_code.params)
2720 return E_OUTOFMEMORY;
2722 return parse_arguments(ctx, args, ctx->code->global_code.params, NULL);
2725 HRESULT compile_script(script_ctx_t *ctx, const WCHAR *code, UINT64 source_context, unsigned start_line,
2726 const WCHAR *args, const WCHAR *delimiter, BOOL from_eval, BOOL use_decode,
2727 named_item_t *named_item, bytecode_t **ret)
2729 compiler_ctx_t compiler = {0};
2730 HRESULT hres;
2732 hres = init_code(&compiler, code, source_context, start_line);
2733 if(FAILED(hres))
2734 return hres;
2736 if(args) {
2737 hres = compile_arguments(&compiler, args);
2738 if(FAILED(hres))
2739 return hres;
2742 if(use_decode) {
2743 hres = decode_source(compiler.code->source);
2744 if(FAILED(hres)) {
2745 WARN("Decoding failed\n");
2746 return hres;
2750 hres = script_parse(ctx, &compiler, compiler.code, delimiter, from_eval, &compiler.parser);
2751 if(FAILED(hres)) {
2752 release_bytecode(compiler.code);
2753 return hres;
2756 heap_pool_init(&compiler.heap);
2757 hres = compile_function(&compiler, compiler.parser->source, NULL, from_eval, &compiler.code->global_code);
2758 free(compiler.local_scopes);
2759 heap_pool_free(&compiler.heap);
2760 parser_release(compiler.parser);
2761 if(FAILED(hres)) {
2762 if(hres != DISP_E_EXCEPTION)
2763 throw_error(ctx, hres, NULL);
2764 set_error_location(ctx->ei, compiler.code, compiler.loc, IDS_COMPILATION_ERROR, NULL);
2765 release_bytecode(compiler.code);
2766 return DISP_E_EXCEPTION;
2769 if(named_item) {
2770 compiler.code->named_item = named_item;
2771 named_item->ref++;
2774 *ret = compiler.code;
2775 return S_OK;