winmm: Restrict some MCI actions to the creating thread.
[wine.git] / dlls / jscript / compile.c
blob0c40d1f173f217864791f9728772a7a846c79b47
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/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
29 WINE_DECLARE_DEBUG_CHANNEL(jscript_disas);
31 typedef struct _statement_ctx_t {
32 unsigned stack_use;
33 BOOL using_scope;
34 BOOL using_except;
36 unsigned break_label;
37 unsigned continue_label;
39 const labelled_statement_t *labelled_stat;
41 struct _statement_ctx_t *next;
42 } statement_ctx_t;
44 typedef struct {
45 parser_ctx_t *parser;
46 bytecode_t *code;
48 BOOL from_eval;
50 unsigned code_off;
51 unsigned code_size;
53 unsigned *labels;
54 unsigned labels_size;
55 unsigned labels_cnt;
57 statement_ctx_t *stat_ctx;
58 function_code_t *func;
60 variable_declaration_t *var_head;
61 variable_declaration_t *var_tail;
63 function_expression_t *func_head;
64 function_expression_t *func_tail;
65 } compiler_ctx_t;
67 static const struct {
68 const char *op_str;
69 instr_arg_type_t arg1_type;
70 instr_arg_type_t arg2_type;
71 } instr_info[] = {
72 #define X(n,a,b,c) {#n,b,c},
73 OP_LIST
74 #undef X
77 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
79 switch(type) {
80 case ARG_STR:
81 TRACE_(jscript_disas)("\t%s", debugstr_jsstr(arg->str));
82 break;
83 case ARG_BSTR:
84 TRACE_(jscript_disas)("\t%s", debugstr_wn(arg->bstr, SysStringLen(arg->bstr)));
85 break;
86 case ARG_INT:
87 TRACE_(jscript_disas)("\t%d", arg->uint);
88 break;
89 case ARG_UINT:
90 case ARG_ADDR:
91 TRACE_(jscript_disas)("\t%u", arg->uint);
92 break;
93 case ARG_FUNC:
94 case ARG_NONE:
95 break;
96 DEFAULT_UNREACHABLE;
100 static void dump_code(compiler_ctx_t *ctx, unsigned off)
102 instr_t *instr;
104 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
105 TRACE_(jscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
106 if(instr_info[instr->op].arg1_type == ARG_DBL) {
107 TRACE_(jscript_disas)("\t%lf", instr->u.dbl);
108 }else {
109 dump_instr_arg(instr_info[instr->op].arg1_type, instr->u.arg);
110 dump_instr_arg(instr_info[instr->op].arg2_type, instr->u.arg+1);
112 TRACE_(jscript_disas)("\n");
116 static HRESULT compile_expression(compiler_ctx_t*,expression_t*,BOOL);
117 static HRESULT compile_statement(compiler_ctx_t*,statement_ctx_t*,statement_t*);
119 static inline void *compiler_alloc(bytecode_t *code, size_t size)
121 return heap_pool_alloc(&code->heap, size);
124 static jsstr_t *compiler_alloc_string_len(compiler_ctx_t *ctx, const WCHAR *str, unsigned len)
126 jsstr_t *new_str;
128 if(!ctx->code->str_pool_size) {
129 ctx->code->str_pool = heap_alloc(8 * sizeof(jsstr_t*));
130 if(!ctx->code->str_pool)
131 return NULL;
132 ctx->code->str_pool_size = 8;
133 }else if(ctx->code->str_pool_size == ctx->code->str_cnt) {
134 jsstr_t **new_pool;
136 new_pool = heap_realloc(ctx->code->str_pool, ctx->code->str_pool_size*2*sizeof(jsstr_t*));
137 if(!new_pool)
138 return NULL;
140 ctx->code->str_pool = new_pool;
141 ctx->code->str_pool_size *= 2;
144 new_str = jsstr_alloc_len(str, len);
145 if(!new_str)
146 return NULL;
148 ctx->code->str_pool[ctx->code->str_cnt++] = new_str;
149 return new_str;
152 static jsstr_t *compiler_alloc_string(compiler_ctx_t *ctx, const WCHAR *str)
154 return compiler_alloc_string_len(ctx, str, strlenW(str));
157 static BOOL ensure_bstr_slot(compiler_ctx_t *ctx)
159 if(!ctx->code->bstr_pool_size) {
160 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
161 if(!ctx->code->bstr_pool)
162 return FALSE;
163 ctx->code->bstr_pool_size = 8;
164 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
165 BSTR *new_pool;
167 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
168 if(!new_pool)
169 return FALSE;
171 ctx->code->bstr_pool = new_pool;
172 ctx->code->bstr_pool_size *= 2;
175 return TRUE;
178 static BSTR compiler_alloc_bstr(compiler_ctx_t *ctx, const WCHAR *str)
180 if(!ensure_bstr_slot(ctx))
181 return NULL;
183 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
184 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
185 return NULL;
187 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
190 static BSTR compiler_alloc_bstr_len(compiler_ctx_t *ctx, const WCHAR *str, size_t len)
192 if(!ensure_bstr_slot(ctx))
193 return NULL;
195 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocStringLen(str, len);
196 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
197 return NULL;
199 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
202 static unsigned push_instr(compiler_ctx_t *ctx, jsop_t op)
204 assert(ctx->code_size >= ctx->code_off);
206 if(ctx->code_size == ctx->code_off) {
207 instr_t *new_instrs;
209 new_instrs = heap_realloc(ctx->code->instrs, ctx->code_size*2*sizeof(instr_t));
210 if(!new_instrs)
211 return 0;
213 ctx->code->instrs = new_instrs;
214 ctx->code_size *= 2;
217 ctx->code->instrs[ctx->code_off].op = op;
218 return ctx->code_off++;
221 static inline instr_t *instr_ptr(compiler_ctx_t *ctx, unsigned off)
223 assert(off < ctx->code_off);
224 return ctx->code->instrs + off;
227 static HRESULT push_instr_int(compiler_ctx_t *ctx, jsop_t op, LONG arg)
229 unsigned instr;
231 instr = push_instr(ctx, op);
232 if(!instr)
233 return E_OUTOFMEMORY;
235 instr_ptr(ctx, instr)->u.arg->lng = arg;
236 return S_OK;
239 static HRESULT push_instr_str(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
241 unsigned instr;
242 jsstr_t *str;
244 str = compiler_alloc_string(ctx, arg);
245 if(!str)
246 return E_OUTOFMEMORY;
248 instr = push_instr(ctx, op);
249 if(!instr)
250 return E_OUTOFMEMORY;
252 instr_ptr(ctx, instr)->u.arg->str = str;
253 return S_OK;
256 static HRESULT push_instr_bstr(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
258 unsigned instr;
259 WCHAR *str;
261 str = compiler_alloc_bstr(ctx, arg);
262 if(!str)
263 return E_OUTOFMEMORY;
265 instr = push_instr(ctx, op);
266 if(!instr)
267 return E_OUTOFMEMORY;
269 instr_ptr(ctx, instr)->u.arg->bstr = str;
270 return S_OK;
273 static HRESULT push_instr_bstr_uint(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg1, unsigned arg2)
275 unsigned instr;
276 WCHAR *str;
278 str = compiler_alloc_bstr(ctx, arg1);
279 if(!str)
280 return E_OUTOFMEMORY;
282 instr = push_instr(ctx, op);
283 if(!instr)
284 return E_OUTOFMEMORY;
286 instr_ptr(ctx, instr)->u.arg[0].bstr = str;
287 instr_ptr(ctx, instr)->u.arg[1].uint = arg2;
288 return S_OK;
291 static HRESULT push_instr_uint_str(compiler_ctx_t *ctx, jsop_t op, unsigned arg1, const WCHAR *arg2)
293 unsigned instr;
294 jsstr_t *str;
296 str = compiler_alloc_string(ctx, arg2);
297 if(!str)
298 return E_OUTOFMEMORY;
300 instr = push_instr(ctx, op);
301 if(!instr)
302 return E_OUTOFMEMORY;
304 instr_ptr(ctx, instr)->u.arg[0].uint = arg1;
305 instr_ptr(ctx, instr)->u.arg[1].str = str;
306 return S_OK;
309 static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
311 unsigned instr;
313 instr = push_instr(ctx, op);
314 if(!instr)
315 return E_OUTOFMEMORY;
317 instr_ptr(ctx, instr)->u.dbl = arg;
318 return S_OK;
321 static inline void set_arg_uint(compiler_ctx_t *ctx, unsigned instr, unsigned arg)
323 instr_ptr(ctx, instr)->u.arg->uint = arg;
326 static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
328 unsigned instr;
330 instr = push_instr(ctx, op);
331 if(!instr)
332 return E_OUTOFMEMORY;
334 set_arg_uint(ctx, instr, arg);
335 return S_OK;
338 static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
340 HRESULT hres;
342 hres = compile_expression(ctx, expr->expression1, TRUE);
343 if(FAILED(hres))
344 return hres;
346 hres = compile_expression(ctx, expr->expression2, TRUE);
347 if(FAILED(hres))
348 return hres;
350 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
353 static HRESULT compile_unary_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op)
355 HRESULT hres;
357 hres = compile_expression(ctx, expr->expression, TRUE);
358 if(FAILED(hres))
359 return hres;
361 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
364 /* ECMA-262 3rd Edition 11.2.1 */
365 static HRESULT compile_member_expression(compiler_ctx_t *ctx, member_expression_t *expr)
367 HRESULT hres;
369 hres = compile_expression(ctx, expr->expression, TRUE);
370 if(FAILED(hres))
371 return hres;
373 return push_instr_bstr(ctx, OP_member, expr->identifier);
376 #define LABEL_FLAG 0x80000000
378 static unsigned alloc_label(compiler_ctx_t *ctx)
380 if(!ctx->labels_size) {
381 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
382 if(!ctx->labels)
383 return 0;
384 ctx->labels_size = 8;
385 }else if(ctx->labels_size == ctx->labels_cnt) {
386 unsigned *new_labels;
388 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
389 if(!new_labels)
390 return 0;
392 ctx->labels = new_labels;
393 ctx->labels_size *= 2;
396 return ctx->labels_cnt++ | LABEL_FLAG;
399 static void label_set_addr(compiler_ctx_t *ctx, unsigned label)
401 assert(label & LABEL_FLAG);
402 ctx->labels[label & ~LABEL_FLAG] = ctx->code_off;
405 static inline BOOL is_memberid_expr(expression_type_t type)
407 return type == EXPR_IDENT || type == EXPR_MEMBER || type == EXPR_ARRAY;
410 static HRESULT compile_memberid_expression(compiler_ctx_t *ctx, expression_t *expr, unsigned flags)
412 HRESULT hres = S_OK;
414 switch(expr->type) {
415 case EXPR_IDENT: {
416 identifier_expression_t *ident_expr = (identifier_expression_t*)expr;
418 hres = push_instr_bstr_uint(ctx, OP_identid, ident_expr->identifier, flags);
419 break;
421 case EXPR_ARRAY: {
422 binary_expression_t *array_expr = (binary_expression_t*)expr;
424 hres = compile_expression(ctx, array_expr->expression1, TRUE);
425 if(FAILED(hres))
426 return hres;
428 hres = compile_expression(ctx, array_expr->expression2, TRUE);
429 if(FAILED(hres))
430 return hres;
432 hres = push_instr_uint(ctx, OP_memberid, flags);
433 break;
435 case EXPR_MEMBER: {
436 member_expression_t *member_expr = (member_expression_t*)expr;
438 hres = compile_expression(ctx, member_expr->expression, TRUE);
439 if(FAILED(hres))
440 return hres;
442 /* FIXME: Potential optimization */
443 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
444 if(FAILED(hres))
445 return hres;
447 hres = push_instr_uint(ctx, OP_memberid, flags);
448 break;
450 DEFAULT_UNREACHABLE;
453 return hres;
456 static HRESULT compile_increment_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op, int n)
458 HRESULT hres;
460 if(!is_memberid_expr(expr->expression->type)) {
461 hres = compile_expression(ctx, expr->expression, TRUE);
462 if(FAILED(hres))
463 return hres;
465 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
468 hres = compile_memberid_expression(ctx, expr->expression, fdexNameEnsure);
469 if(FAILED(hres))
470 return hres;
472 return push_instr_int(ctx, op, n);
475 /* ECMA-262 3rd Edition 11.14 */
476 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr, BOOL emit_ret)
478 HRESULT hres;
480 hres = compile_expression(ctx, expr->expression1, FALSE);
481 if(FAILED(hres))
482 return hres;
484 return compile_expression(ctx, expr->expression2, emit_ret);
487 /* ECMA-262 3rd Edition 11.11 */
488 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
490 unsigned instr;
491 HRESULT hres;
493 hres = compile_expression(ctx, expr->expression1, TRUE);
494 if(FAILED(hres))
495 return hres;
497 instr = push_instr(ctx, op);
498 if(!instr)
499 return E_OUTOFMEMORY;
501 hres = compile_expression(ctx, expr->expression2, TRUE);
502 if(FAILED(hres))
503 return hres;
505 set_arg_uint(ctx, instr, ctx->code_off);
506 return S_OK;
509 /* ECMA-262 3rd Edition 11.12 */
510 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
512 unsigned jmp_false, jmp_end;
513 HRESULT hres;
515 hres = compile_expression(ctx, expr->expression, TRUE);
516 if(FAILED(hres))
517 return hres;
519 jmp_false = push_instr(ctx, OP_cnd_z);
520 if(!jmp_false)
521 return E_OUTOFMEMORY;
523 hres = compile_expression(ctx, expr->true_expression, TRUE);
524 if(FAILED(hres))
525 return hres;
527 jmp_end = push_instr(ctx, OP_jmp);
528 if(!jmp_end)
529 return E_OUTOFMEMORY;
531 set_arg_uint(ctx, jmp_false, ctx->code_off);
532 hres = push_instr_uint(ctx, OP_pop, 1);
533 if(FAILED(hres))
534 return hres;
536 hres = compile_expression(ctx, expr->false_expression, TRUE);
537 if(FAILED(hres))
538 return hres;
540 set_arg_uint(ctx, jmp_end, ctx->code_off);
541 return S_OK;
544 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
546 unsigned arg_cnt = 0;
547 argument_t *arg;
548 HRESULT hres;
550 hres = compile_expression(ctx, expr->expression, TRUE);
551 if(FAILED(hres))
552 return hres;
554 for(arg = expr->argument_list; arg; arg = arg->next) {
555 hres = compile_expression(ctx, arg->expr, TRUE);
556 if(FAILED(hres))
557 return hres;
558 arg_cnt++;
561 hres = push_instr_uint(ctx, OP_new, arg_cnt);
562 if(FAILED(hres))
563 return hres;
565 hres = push_instr_uint(ctx, OP_pop, arg_cnt+1);
566 if(FAILED(hres))
567 return hres;
569 return push_instr(ctx, OP_push_ret) ? S_OK : E_OUTOFMEMORY;
572 static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL emit_ret)
574 unsigned arg_cnt = 0, extra_args;
575 argument_t *arg;
576 unsigned instr;
577 jsop_t op;
578 HRESULT hres;
580 if(is_memberid_expr(expr->expression->type)) {
581 op = OP_call_member;
582 extra_args = 2;
583 hres = compile_memberid_expression(ctx, expr->expression, 0);
584 }else {
585 op = OP_call;
586 extra_args = 1;
587 hres = compile_expression(ctx, expr->expression, TRUE);
590 if(FAILED(hres))
591 return hres;
593 for(arg = expr->argument_list; arg; arg = arg->next) {
594 hres = compile_expression(ctx, arg->expr, TRUE);
595 if(FAILED(hres))
596 return hres;
597 arg_cnt++;
600 instr = push_instr(ctx, op);
601 if(!instr)
602 return E_OUTOFMEMORY;
604 instr_ptr(ctx, instr)->u.arg[0].uint = arg_cnt;
605 instr_ptr(ctx, instr)->u.arg[1].lng = emit_ret;
607 hres = push_instr_uint(ctx, OP_pop, arg_cnt + extra_args);
608 if(FAILED(hres))
609 return hres;
611 return !emit_ret || push_instr(ctx, OP_push_ret) ? S_OK : E_OUTOFMEMORY;
614 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
616 HRESULT hres;
618 switch(expr->expression->type) {
619 case EXPR_ARRAY: {
620 binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
622 hres = compile_expression(ctx, array_expr->expression1, TRUE);
623 if(FAILED(hres))
624 return hres;
626 hres = compile_expression(ctx, array_expr->expression2, TRUE);
627 if(FAILED(hres))
628 return hres;
630 if(!push_instr(ctx, OP_delete))
631 return E_OUTOFMEMORY;
632 break;
634 case EXPR_MEMBER: {
635 member_expression_t *member_expr = (member_expression_t*)expr->expression;
637 hres = compile_expression(ctx, member_expr->expression, TRUE);
638 if(FAILED(hres))
639 return hres;
641 /* FIXME: Potential optimization */
642 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
643 if(FAILED(hres))
644 return hres;
646 if(!push_instr(ctx, OP_delete))
647 return E_OUTOFMEMORY;
648 break;
650 case EXPR_IDENT:
651 return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
652 default: {
653 const WCHAR fixmeW[] = {'F','I','X','M','E',0};
655 WARN("invalid delete, unimplemented exception message\n");
657 hres = compile_expression(ctx, expr->expression, TRUE);
658 if(FAILED(hres))
659 return hres;
661 return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, fixmeW);
665 return S_OK;
668 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
670 BOOL use_throw_path = FALSE;
671 unsigned arg_cnt = 0;
672 HRESULT hres;
674 if(expr->expression1->type == EXPR_CALL) {
675 call_expression_t *call_expr = (call_expression_t*)expr->expression1;
676 argument_t *arg;
678 if(op != OP_LAST) {
679 FIXME("op %d not supported on parametrized assign expressions\n", op);
680 return E_NOTIMPL;
683 if(is_memberid_expr(call_expr->expression->type) && call_expr->argument_list) {
684 hres = compile_memberid_expression(ctx, call_expr->expression, fdexNameEnsure);
685 if(FAILED(hres))
686 return hres;
688 for(arg = call_expr->argument_list; arg; arg = arg->next) {
689 hres = compile_expression(ctx, arg->expr, TRUE);
690 if(FAILED(hres))
691 return hres;
692 arg_cnt++;
694 }else {
695 use_throw_path = TRUE;
697 }else if(is_memberid_expr(expr->expression1->type)) {
698 hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
699 if(FAILED(hres))
700 return hres;
701 }else {
702 use_throw_path = TRUE;
705 if(use_throw_path) {
706 /* Illegal assignment: evaluate and throw */
707 hres = compile_expression(ctx, expr->expression1, TRUE);
708 if(FAILED(hres))
709 return hres;
711 hres = compile_expression(ctx, expr->expression2, TRUE);
712 if(FAILED(hres))
713 return hres;
715 if(op != OP_LAST && !push_instr(ctx, op))
716 return E_OUTOFMEMORY;
718 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
721 if(op != OP_LAST && !push_instr(ctx, OP_refval))
722 return E_OUTOFMEMORY;
724 hres = compile_expression(ctx, expr->expression2, TRUE);
725 if(FAILED(hres))
726 return hres;
728 if(op != OP_LAST && !push_instr(ctx, op))
729 return E_OUTOFMEMORY;
731 if(arg_cnt)
732 return push_instr_uint(ctx, OP_assign_call, arg_cnt);
734 if(!push_instr(ctx, OP_assign))
735 return E_OUTOFMEMORY;
737 return S_OK;
740 static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
742 jsop_t op;
743 HRESULT hres;
745 if(is_memberid_expr(expr->expression->type)) {
746 if(expr->expression->type == EXPR_IDENT)
747 return push_instr_bstr(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
749 op = OP_typeofid;
750 hres = compile_memberid_expression(ctx, expr->expression, 0);
751 }else {
752 op = OP_typeof;
753 hres = compile_expression(ctx, expr->expression, TRUE);
755 if(FAILED(hres))
756 return hres;
758 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
761 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
763 switch(literal->type) {
764 case LT_BOOL:
765 return push_instr_int(ctx, OP_bool, literal->u.bval);
766 case LT_DOUBLE:
767 return push_instr_double(ctx, OP_double, literal->u.dval);
768 case LT_NULL:
769 return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
770 case LT_STRING:
771 return push_instr_str(ctx, OP_str, literal->u.wstr);
772 case LT_REGEXP: {
773 unsigned instr;
774 jsstr_t *str;
776 str = compiler_alloc_string_len(ctx, literal->u.regexp.str, literal->u.regexp.str_len);
777 if(!str)
778 return E_OUTOFMEMORY;
780 instr = push_instr(ctx, OP_regexp);
781 if(!instr)
782 return E_OUTOFMEMORY;
784 instr_ptr(ctx, instr)->u.arg[0].str = str;
785 instr_ptr(ctx, instr)->u.arg[1].uint = literal->u.regexp.flags;
786 return S_OK;
788 DEFAULT_UNREACHABLE;
790 return E_FAIL;
793 static HRESULT literal_as_bstr(compiler_ctx_t *ctx, literal_t *literal, BSTR *str)
795 switch(literal->type) {
796 case LT_STRING:
797 *str = compiler_alloc_bstr(ctx, literal->u.wstr);
798 break;
799 case LT_DOUBLE: {
800 jsstr_t *jsstr;
801 HRESULT hres;
803 hres = double_to_string(literal->u.dval, &jsstr);
804 if(FAILED(hres))
805 return hres;
807 *str = compiler_alloc_bstr_len(ctx, NULL, jsstr_length(jsstr));
808 if(*str)
809 jsstr_flush(jsstr, *str);
810 jsstr_release(jsstr);
811 break;
813 DEFAULT_UNREACHABLE;
816 return *str ? S_OK : E_OUTOFMEMORY;
819 static HRESULT compile_array_literal(compiler_ctx_t *ctx, array_literal_expression_t *expr)
821 unsigned i, elem_cnt = expr->length;
822 array_element_t *iter;
823 HRESULT hres;
825 for(iter = expr->element_list; iter; iter = iter->next) {
826 elem_cnt += iter->elision+1;
828 for(i=0; i < iter->elision; i++) {
829 if(!push_instr(ctx, OP_undefined))
830 return E_OUTOFMEMORY;
833 hres = compile_expression(ctx, iter->expr, TRUE);
834 if(FAILED(hres))
835 return hres;
838 for(i=0; i < expr->length; i++) {
839 if(!push_instr(ctx, OP_undefined))
840 return E_OUTOFMEMORY;
843 return push_instr_uint(ctx, OP_carray, elem_cnt);
846 static HRESULT compile_object_literal(compiler_ctx_t *ctx, property_value_expression_t *expr)
848 prop_val_t *iter;
849 unsigned instr;
850 BSTR name;
851 HRESULT hres;
853 if(!push_instr(ctx, OP_new_obj))
854 return E_OUTOFMEMORY;
856 for(iter = expr->property_list; iter; iter = iter->next) {
857 hres = literal_as_bstr(ctx, iter->name, &name);
858 if(FAILED(hres))
859 return hres;
861 hres = compile_expression(ctx, iter->value, TRUE);
862 if(FAILED(hres))
863 return hres;
865 instr = push_instr(ctx, OP_obj_prop);
866 if(!instr)
867 return E_OUTOFMEMORY;
869 instr_ptr(ctx, instr)->u.arg->bstr = name;
872 return S_OK;
875 static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr)
877 ctx->func_tail = ctx->func_tail ? (ctx->func_tail->next = expr) : (ctx->func_head = expr);
879 /* FIXME: not exactly right */
880 if(expr->identifier && !expr->event_target) {
881 ctx->func->func_cnt++;
882 return push_instr_bstr(ctx, OP_ident, expr->identifier);
885 return push_instr_uint(ctx, OP_func, ctx->func->func_cnt++);
888 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr, BOOL emit_ret)
890 HRESULT hres;
892 switch(expr->type) {
893 case EXPR_ADD:
894 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
895 break;
896 case EXPR_AND:
897 hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_z);
898 break;
899 case EXPR_ARRAY:
900 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
901 break;
902 case EXPR_ARRAYLIT:
903 hres = compile_array_literal(ctx, (array_literal_expression_t*)expr);
904 break;
905 case EXPR_ASSIGN:
906 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
907 break;
908 case EXPR_ASSIGNADD:
909 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
910 break;
911 case EXPR_ASSIGNAND:
912 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_and);
913 break;
914 case EXPR_ASSIGNSUB:
915 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
916 break;
917 case EXPR_ASSIGNMUL:
918 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
919 break;
920 case EXPR_ASSIGNDIV:
921 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
922 break;
923 case EXPR_ASSIGNMOD:
924 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
925 break;
926 case EXPR_ASSIGNOR:
927 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
928 break;
929 case EXPR_ASSIGNLSHIFT:
930 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_lshift);
931 break;
932 case EXPR_ASSIGNRSHIFT:
933 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift);
934 break;
935 case EXPR_ASSIGNRRSHIFT:
936 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
937 break;
938 case EXPR_ASSIGNXOR:
939 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
940 break;
941 case EXPR_BAND:
942 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
943 break;
944 case EXPR_BITNEG:
945 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
946 break;
947 case EXPR_BOR:
948 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
949 break;
950 case EXPR_CALL:
951 return compile_call_expression(ctx, (call_expression_t*)expr, emit_ret);
952 case EXPR_COMMA:
953 return compile_comma_expression(ctx, (binary_expression_t*)expr, emit_ret);
954 case EXPR_COND:
955 hres = compile_conditional_expression(ctx, (conditional_expression_t*)expr);
956 break;
957 case EXPR_DELETE:
958 hres = compile_delete_expression(ctx, (unary_expression_t*)expr);
959 break;
960 case EXPR_DIV:
961 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
962 break;
963 case EXPR_EQ:
964 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
965 break;
966 case EXPR_EQEQ:
967 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
968 break;
969 case EXPR_FUNC:
970 hres = compile_function_expression(ctx, (function_expression_t*)expr);
971 break;
972 case EXPR_GREATER:
973 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
974 break;
975 case EXPR_GREATEREQ:
976 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
977 break;
978 case EXPR_IDENT:
979 hres = push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
980 break;
981 case EXPR_IN:
982 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
983 break;
984 case EXPR_INSTANCEOF:
985 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
986 break;
987 case EXPR_LESS:
988 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
989 break;
990 case EXPR_LESSEQ:
991 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
992 break;
993 case EXPR_LITERAL:
994 hres = compile_literal(ctx, ((literal_expression_t*)expr)->literal);
995 break;
996 case EXPR_LOGNEG:
997 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
998 break;
999 case EXPR_LSHIFT:
1000 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
1001 break;
1002 case EXPR_MEMBER:
1003 hres = compile_member_expression(ctx, (member_expression_t*)expr);
1004 break;
1005 case EXPR_MINUS:
1006 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
1007 break;
1008 case EXPR_MOD:
1009 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
1010 break;
1011 case EXPR_MUL:
1012 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
1013 break;
1014 case EXPR_NEW:
1015 hres = compile_new_expression(ctx, (call_expression_t*)expr);
1016 break;
1017 case EXPR_NOTEQ:
1018 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
1019 break;
1020 case EXPR_NOTEQEQ:
1021 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
1022 break;
1023 case EXPR_OR:
1024 hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_nz);
1025 break;
1026 case EXPR_PLUS:
1027 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
1028 break;
1029 case EXPR_POSTDEC:
1030 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
1031 break;
1032 case EXPR_POSTINC:
1033 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
1034 break;
1035 case EXPR_PREDEC:
1036 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
1037 break;
1038 case EXPR_PREINC:
1039 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
1040 break;
1041 case EXPR_PROPVAL:
1042 hres = compile_object_literal(ctx, (property_value_expression_t*)expr);
1043 break;
1044 case EXPR_RSHIFT:
1045 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
1046 break;
1047 case EXPR_RRSHIFT:
1048 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
1049 break;
1050 case EXPR_SUB:
1051 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
1052 break;
1053 case EXPR_THIS:
1054 return !emit_ret || push_instr(ctx, OP_this) ? S_OK : E_OUTOFMEMORY;
1055 case EXPR_TYPEOF:
1056 hres = compile_typeof_expression(ctx, (unary_expression_t*)expr);
1057 break;
1058 case EXPR_VOID:
1059 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
1060 break;
1061 case EXPR_BXOR:
1062 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
1063 break;
1064 DEFAULT_UNREACHABLE;
1067 if(FAILED(hres))
1068 return hres;
1070 return emit_ret ? S_OK : push_instr_uint(ctx, OP_pop, 1);
1073 static inline BOOL is_loop_statement(statement_type_t type)
1075 return type == STAT_FOR || type == STAT_FORIN || type == STAT_WHILE;
1078 /* ECMA-262 3rd Edition 12.1 */
1079 static HRESULT compile_block_statement(compiler_ctx_t *ctx, statement_t *iter)
1081 HRESULT hres;
1083 while(iter) {
1084 hres = compile_statement(ctx, NULL, iter);
1085 if(FAILED(hres))
1086 return hres;
1088 iter = iter->next;
1091 return S_OK;
1094 /* ECMA-262 3rd Edition 12.2 */
1095 static HRESULT compile_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
1097 variable_declaration_t *iter;
1098 HRESULT hres;
1100 assert(list != NULL);
1102 if(ctx->var_tail)
1103 ctx->var_tail->global_next = list;
1104 else
1105 ctx->var_head = list;
1107 for(iter = list; iter; iter = iter->next) {
1108 ctx->func->var_cnt++;
1109 iter->global_next = iter->next;
1110 if(!iter->next)
1111 ctx->var_tail = iter;
1113 if(!iter->expr)
1114 continue;
1116 hres = compile_expression(ctx, iter->expr, TRUE);
1117 if(FAILED(hres))
1118 return hres;
1120 hres = push_instr_bstr(ctx, OP_var_set, iter->identifier);
1121 if(FAILED(hres))
1122 return hres;
1125 return S_OK;
1128 /* ECMA-262 3rd Edition 12.2 */
1129 static HRESULT compile_var_statement(compiler_ctx_t *ctx, var_statement_t *stat)
1131 return compile_variable_list(ctx, stat->variable_list);
1134 /* ECMA-262 3rd Edition 12.4 */
1135 static HRESULT compile_expression_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1137 HRESULT hres;
1139 hres = compile_expression(ctx, stat->expr, ctx->from_eval);
1140 if(FAILED(hres))
1141 return hres;
1143 return !ctx->from_eval || push_instr(ctx, OP_setret) ? S_OK : E_OUTOFMEMORY;
1146 /* ECMA-262 3rd Edition 12.5 */
1147 static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat)
1149 unsigned jmp_else;
1150 HRESULT hres;
1152 hres = compile_expression(ctx, stat->expr, TRUE);
1153 if(FAILED(hres))
1154 return hres;
1156 jmp_else = push_instr(ctx, OP_jmp_z);
1157 if(!jmp_else)
1158 return E_OUTOFMEMORY;
1160 hres = compile_statement(ctx, NULL, stat->if_stat);
1161 if(FAILED(hres))
1162 return hres;
1164 if(stat->else_stat) {
1165 unsigned jmp_end;
1167 jmp_end = push_instr(ctx, OP_jmp);
1168 if(!jmp_end)
1169 return E_OUTOFMEMORY;
1171 set_arg_uint(ctx, jmp_else, ctx->code_off);
1173 hres = compile_statement(ctx, NULL, stat->else_stat);
1174 if(FAILED(hres))
1175 return hres;
1177 set_arg_uint(ctx, jmp_end, ctx->code_off);
1178 }else {
1179 set_arg_uint(ctx, jmp_else, ctx->code_off);
1182 return S_OK;
1185 /* ECMA-262 3rd Edition 12.6.2 */
1186 static HRESULT compile_while_statement(compiler_ctx_t *ctx, while_statement_t *stat)
1188 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1189 unsigned jmp_off;
1190 HRESULT hres;
1192 stat_ctx.break_label = alloc_label(ctx);
1193 if(!stat_ctx.break_label)
1194 return E_OUTOFMEMORY;
1196 stat_ctx.continue_label = alloc_label(ctx);
1197 if(!stat_ctx.continue_label)
1198 return E_OUTOFMEMORY;
1200 jmp_off = ctx->code_off;
1202 if(!stat->do_while) {
1203 label_set_addr(ctx, stat_ctx.continue_label);
1204 hres = compile_expression(ctx, stat->expr, TRUE);
1205 if(FAILED(hres))
1206 return hres;
1208 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1209 if(FAILED(hres))
1210 return hres;
1213 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1214 if(FAILED(hres))
1215 return hres;
1217 if(stat->do_while) {
1218 label_set_addr(ctx, stat_ctx.continue_label);
1219 hres = compile_expression(ctx, stat->expr, TRUE);
1220 if(FAILED(hres))
1221 return hres;
1223 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1224 if(FAILED(hres))
1225 return hres;
1228 hres = push_instr_uint(ctx, OP_jmp, jmp_off);
1229 if(FAILED(hres))
1230 return hres;
1232 label_set_addr(ctx, stat_ctx.break_label);
1233 return S_OK;
1236 /* ECMA-262 3rd Edition 12.6.3 */
1237 static HRESULT compile_for_statement(compiler_ctx_t *ctx, for_statement_t *stat)
1239 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1240 unsigned expr_off;
1241 HRESULT hres;
1243 if(stat->variable_list) {
1244 hres = compile_variable_list(ctx, stat->variable_list);
1245 if(FAILED(hres))
1246 return hres;
1247 }else if(stat->begin_expr) {
1248 hres = compile_expression(ctx, stat->begin_expr, FALSE);
1249 if(FAILED(hres))
1250 return hres;
1253 stat_ctx.break_label = alloc_label(ctx);
1254 if(!stat_ctx.break_label)
1255 return E_OUTOFMEMORY;
1257 stat_ctx.continue_label = alloc_label(ctx);
1258 if(!stat_ctx.continue_label)
1259 return E_OUTOFMEMORY;
1261 expr_off = ctx->code_off;
1263 if(stat->expr) {
1264 hres = compile_expression(ctx, stat->expr, TRUE);
1265 if(FAILED(hres))
1266 return hres;
1268 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1269 if(FAILED(hres))
1270 return hres;
1273 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1274 if(FAILED(hres))
1275 return hres;
1277 label_set_addr(ctx, stat_ctx.continue_label);
1279 if(stat->end_expr) {
1280 hres = compile_expression(ctx, stat->end_expr, FALSE);
1281 if(FAILED(hres))
1282 return hres;
1285 hres = push_instr_uint(ctx, OP_jmp, expr_off);
1286 if(FAILED(hres))
1287 return hres;
1289 label_set_addr(ctx, stat_ctx.break_label);
1290 return S_OK;
1293 /* ECMA-262 3rd Edition 12.6.4 */
1294 static HRESULT compile_forin_statement(compiler_ctx_t *ctx, forin_statement_t *stat)
1296 statement_ctx_t stat_ctx = {4, FALSE, FALSE};
1297 HRESULT hres;
1299 if(stat->variable) {
1300 hres = compile_variable_list(ctx, stat->variable);
1301 if(FAILED(hres))
1302 return hres;
1305 stat_ctx.break_label = alloc_label(ctx);
1306 if(!stat_ctx.break_label)
1307 return E_OUTOFMEMORY;
1309 stat_ctx.continue_label = alloc_label(ctx);
1310 if(!stat_ctx.continue_label)
1311 return E_OUTOFMEMORY;
1313 hres = compile_expression(ctx, stat->in_expr, TRUE);
1314 if(FAILED(hres))
1315 return hres;
1317 if(stat->variable) {
1318 hres = push_instr_bstr_uint(ctx, OP_identid, stat->variable->identifier, fdexNameEnsure);
1319 if(FAILED(hres))
1320 return hres;
1321 }else if(is_memberid_expr(stat->expr->type)) {
1322 hres = compile_memberid_expression(ctx, stat->expr, fdexNameEnsure);
1323 if(FAILED(hres))
1324 return hres;
1325 }else {
1326 hres = push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
1327 if(FAILED(hres))
1328 return hres;
1330 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1331 return S_OK;
1334 hres = push_instr_int(ctx, OP_int, DISPID_STARTENUM);
1335 if(FAILED(hres))
1336 return hres;
1338 label_set_addr(ctx, stat_ctx.continue_label);
1339 hres = push_instr_uint(ctx, OP_forin, stat_ctx.break_label);
1340 if(FAILED(hres))
1341 return E_OUTOFMEMORY;
1343 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1344 if(FAILED(hres))
1345 return hres;
1347 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.continue_label);
1348 if(FAILED(hres))
1349 return hres;
1351 label_set_addr(ctx, stat_ctx.break_label);
1352 return S_OK;
1355 static HRESULT pop_to_stat(compiler_ctx_t *ctx, BOOL var_stack, BOOL scope_stack, statement_ctx_t *stat_ctx)
1357 unsigned stack_pop = 0;
1358 statement_ctx_t *iter;
1360 for(iter = ctx->stat_ctx; iter != stat_ctx; iter = iter->next) {
1361 if(scope_stack) {
1362 if(iter->using_scope && !push_instr(ctx, OP_pop_scope))
1363 return E_OUTOFMEMORY;
1364 if(iter->using_except && !push_instr(ctx, OP_pop_except))
1365 return E_OUTOFMEMORY;
1367 stack_pop += iter->stack_use;
1370 if(var_stack && stack_pop) {
1371 HRESULT hres;
1373 hres = push_instr_uint(ctx, OP_pop, stack_pop);
1374 if(FAILED(hres))
1375 return hres;
1378 return S_OK;
1381 /* ECMA-262 3rd Edition 12.7 */
1382 static HRESULT compile_continue_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1384 statement_ctx_t *pop_ctx;
1385 HRESULT hres;
1387 if(stat->identifier) {
1388 statement_t *label_stat;
1389 statement_ctx_t *iter;
1391 pop_ctx = NULL;
1393 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1394 if(iter->continue_label)
1395 pop_ctx = iter;
1396 if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier))
1397 break;
1400 if(!iter) {
1401 WARN("Label not found\n");
1402 return JS_E_LABEL_NOT_FOUND;
1405 /* Labelled continue are allowed only on loops */
1406 for(label_stat = iter->labelled_stat->statement;
1407 label_stat->type == STAT_LABEL;
1408 label_stat = ((labelled_statement_t*)label_stat)->statement);
1409 if(!is_loop_statement(label_stat->type)) {
1410 WARN("Label is not a loop\n");
1411 return JS_E_INVALID_CONTINUE;
1414 assert(pop_ctx != NULL);
1415 }else {
1416 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1417 if(pop_ctx->continue_label)
1418 break;
1421 if(!pop_ctx) {
1422 WARN("continue outside loop\n");
1423 return JS_E_INVALID_CONTINUE;
1427 hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx);
1428 if(FAILED(hres))
1429 return hres;
1431 return push_instr_uint(ctx, OP_jmp, pop_ctx->continue_label);
1434 /* ECMA-262 3rd Edition 12.8 */
1435 static HRESULT compile_break_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1437 statement_ctx_t *pop_ctx;
1438 HRESULT hres;
1440 if(stat->identifier) {
1441 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1442 if(pop_ctx->labelled_stat && !strcmpW(pop_ctx->labelled_stat->identifier, stat->identifier)) {
1443 assert(pop_ctx->break_label);
1444 break;
1448 if(!pop_ctx) {
1449 WARN("Label not found\n");
1450 return JS_E_LABEL_NOT_FOUND;
1452 }else {
1453 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1454 if(pop_ctx->break_label && !pop_ctx->labelled_stat)
1455 break;
1458 if(!pop_ctx) {
1459 WARN("Break outside loop\n");
1460 return JS_E_INVALID_BREAK;
1464 hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx->next);
1465 if(FAILED(hres))
1466 return hres;
1468 return push_instr_uint(ctx, OP_jmp, pop_ctx->break_label);
1471 /* ECMA-262 3rd Edition 12.9 */
1472 static HRESULT compile_return_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1474 HRESULT hres;
1476 if(ctx->from_eval) {
1477 WARN("misplaced return statement\n");
1478 return JS_E_MISPLACED_RETURN;
1481 hres = pop_to_stat(ctx, TRUE, FALSE, NULL);
1482 if(FAILED(hres))
1483 return hres;
1485 if(stat->expr) {
1486 hres = compile_expression(ctx, stat->expr, TRUE);
1487 if(FAILED(hres))
1488 return hres;
1489 if(!push_instr(ctx, OP_setret))
1490 return E_OUTOFMEMORY;
1493 hres = pop_to_stat(ctx, FALSE, TRUE, NULL);
1494 if(FAILED(hres))
1495 return hres;
1497 return push_instr_uint(ctx, OP_ret, !stat->expr);
1500 /* ECMA-262 3rd Edition 12.10 */
1501 static HRESULT compile_with_statement(compiler_ctx_t *ctx, with_statement_t *stat)
1503 statement_ctx_t stat_ctx = {0, TRUE, FALSE};
1504 HRESULT hres;
1506 hres = compile_expression(ctx, stat->expr, TRUE);
1507 if(FAILED(hres))
1508 return hres;
1510 if(!push_instr(ctx, OP_push_scope))
1511 return E_OUTOFMEMORY;
1513 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1514 if(FAILED(hres))
1515 return hres;
1517 if(!push_instr(ctx, OP_pop_scope))
1518 return E_OUTOFMEMORY;
1520 return S_OK;
1523 /* ECMA-262 3rd Edition 12.10 */
1524 static HRESULT compile_labelled_statement(compiler_ctx_t *ctx, labelled_statement_t *stat)
1526 statement_ctx_t stat_ctx = {0, FALSE, FALSE, 0, 0, stat}, *iter;
1527 HRESULT hres;
1529 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1530 if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier)) {
1531 WARN("Label %s redefined\n", debugstr_w(stat->identifier));
1532 return JS_E_LABEL_REDEFINED;
1536 /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1537 stat_ctx.break_label = alloc_label(ctx);
1538 if(!stat_ctx.break_label)
1539 return E_OUTOFMEMORY;
1541 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1542 if(FAILED(hres))
1543 return hres;
1545 label_set_addr(ctx, stat_ctx.break_label);
1546 return S_OK;
1549 /* ECMA-262 3rd Edition 12.13 */
1550 static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat)
1552 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1553 unsigned case_cnt = 0, *case_jmps, i, default_jmp;
1554 BOOL have_default = FALSE;
1555 statement_t *stat_iter;
1556 case_clausule_t *iter;
1557 HRESULT hres;
1559 hres = compile_expression(ctx, stat->expr, TRUE);
1560 if(FAILED(hres))
1561 return hres;
1563 stat_ctx.break_label = alloc_label(ctx);
1564 if(!stat_ctx.break_label)
1565 return E_OUTOFMEMORY;
1567 for(iter = stat->case_list; iter; iter = iter->next) {
1568 if(iter->expr)
1569 case_cnt++;
1572 case_jmps = heap_alloc(case_cnt * sizeof(*case_jmps));
1573 if(!case_jmps)
1574 return E_OUTOFMEMORY;
1576 i = 0;
1577 for(iter = stat->case_list; iter; iter = iter->next) {
1578 if(!iter->expr) {
1579 have_default = TRUE;
1580 continue;
1583 hres = compile_expression(ctx, iter->expr, TRUE);
1584 if(FAILED(hres))
1585 break;
1587 case_jmps[i] = push_instr(ctx, OP_case);
1588 if(!case_jmps[i]) {
1589 hres = E_OUTOFMEMORY;
1590 break;
1592 i++;
1595 if(SUCCEEDED(hres)) {
1596 hres = push_instr_uint(ctx, OP_pop, 1);
1597 if(SUCCEEDED(hres)) {
1598 default_jmp = push_instr(ctx, OP_jmp);
1599 if(!default_jmp)
1600 hres = E_OUTOFMEMORY;
1604 if(FAILED(hres)) {
1605 heap_free(case_jmps);
1606 return hres;
1609 i = 0;
1610 for(iter = stat->case_list; iter; iter = iter->next) {
1611 while(iter->next && iter->next->stat == iter->stat) {
1612 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1613 iter = iter->next;
1616 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1618 for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter);
1619 stat_iter = stat_iter->next) {
1620 hres = compile_statement(ctx, &stat_ctx, stat_iter);
1621 if(FAILED(hres))
1622 break;
1624 if(FAILED(hres))
1625 break;
1628 heap_free(case_jmps);
1629 if(FAILED(hres))
1630 return hres;
1631 assert(i == case_cnt);
1633 if(!have_default) {
1634 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.break_label);
1635 if(FAILED(hres))
1636 return hres;
1637 set_arg_uint(ctx, default_jmp, ctx->code_off);
1640 label_set_addr(ctx, stat_ctx.break_label);
1641 return S_OK;
1644 /* ECMA-262 3rd Edition 12.13 */
1645 static HRESULT compile_throw_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1647 HRESULT hres;
1649 hres = compile_expression(ctx, stat->expr, TRUE);
1650 if(FAILED(hres))
1651 return hres;
1653 return push_instr(ctx, OP_throw) ? S_OK : E_OUTOFMEMORY;
1656 /* ECMA-262 3rd Edition 12.14 */
1657 static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat)
1659 statement_ctx_t try_ctx = {0, FALSE, TRUE}, catch_ctx = {0, TRUE, FALSE};
1660 statement_ctx_t finally_ctx = {2, FALSE, FALSE};
1661 unsigned push_except;
1662 BSTR ident;
1663 HRESULT hres;
1665 push_except = push_instr(ctx, OP_push_except);
1666 if(!push_except)
1667 return E_OUTOFMEMORY;
1669 if(stat->catch_block) {
1670 ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier);
1671 if(!ident)
1672 return E_OUTOFMEMORY;
1673 }else {
1674 ident = NULL;
1677 instr_ptr(ctx, push_except)->u.arg[1].bstr = ident;
1679 if(!stat->catch_block)
1680 try_ctx.stack_use = 2;
1682 hres = compile_statement(ctx, &try_ctx, stat->try_statement);
1683 if(FAILED(hres))
1684 return hres;
1686 if(!push_instr(ctx, OP_pop_except))
1687 return E_OUTOFMEMORY;
1689 if(stat->catch_block) {
1690 unsigned jmp_finally;
1692 jmp_finally = push_instr(ctx, OP_jmp);
1693 if(!jmp_finally)
1694 return E_OUTOFMEMORY;
1696 instr_ptr(ctx, push_except)->u.arg[0].uint = ctx->code_off;
1698 hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement);
1699 if(FAILED(hres))
1700 return hres;
1702 if(!push_instr(ctx, OP_pop_scope))
1703 return E_OUTOFMEMORY;
1705 set_arg_uint(ctx, jmp_finally, ctx->code_off);
1706 }else {
1707 set_arg_uint(ctx, push_except, ctx->code_off);
1710 if(stat->finally_statement) {
1711 hres = compile_statement(ctx, stat->catch_block ? NULL : &finally_ctx, stat->finally_statement);
1712 if(FAILED(hres))
1713 return hres;
1715 if(!stat->catch_block && !push_instr(ctx, OP_end_finally))
1716 return E_OUTOFMEMORY;
1719 return S_OK;
1722 static HRESULT compile_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1724 HRESULT hres;
1726 if(stat_ctx) {
1727 stat_ctx->next = ctx->stat_ctx;
1728 ctx->stat_ctx = stat_ctx;
1731 switch(stat->type) {
1732 case STAT_BLOCK:
1733 hres = compile_block_statement(ctx, ((block_statement_t*)stat)->stat_list);
1734 break;
1735 case STAT_BREAK:
1736 hres = compile_break_statement(ctx, (branch_statement_t*)stat);
1737 break;
1738 case STAT_CONTINUE:
1739 hres = compile_continue_statement(ctx, (branch_statement_t*)stat);
1740 break;
1741 case STAT_EMPTY:
1742 /* nothing to do */
1743 hres = S_OK;
1744 break;
1745 case STAT_EXPR:
1746 hres = compile_expression_statement(ctx, (expression_statement_t*)stat);
1747 break;
1748 case STAT_FOR:
1749 hres = compile_for_statement(ctx, (for_statement_t*)stat);
1750 break;
1751 case STAT_FORIN:
1752 hres = compile_forin_statement(ctx, (forin_statement_t*)stat);
1753 break;
1754 case STAT_IF:
1755 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1756 break;
1757 case STAT_LABEL:
1758 hres = compile_labelled_statement(ctx, (labelled_statement_t*)stat);
1759 break;
1760 case STAT_RETURN:
1761 hres = compile_return_statement(ctx, (expression_statement_t*)stat);
1762 break;
1763 case STAT_SWITCH:
1764 hres = compile_switch_statement(ctx, (switch_statement_t*)stat);
1765 break;
1766 case STAT_THROW:
1767 hres = compile_throw_statement(ctx, (expression_statement_t*)stat);
1768 break;
1769 case STAT_TRY:
1770 hres = compile_try_statement(ctx, (try_statement_t*)stat);
1771 break;
1772 case STAT_VAR:
1773 hres = compile_var_statement(ctx, (var_statement_t*)stat);
1774 break;
1775 case STAT_WHILE:
1776 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1777 break;
1778 case STAT_WITH:
1779 hres = compile_with_statement(ctx, (with_statement_t*)stat);
1780 break;
1781 DEFAULT_UNREACHABLE;
1784 if(stat_ctx) {
1785 assert(ctx->stat_ctx == stat_ctx);
1786 ctx->stat_ctx = stat_ctx->next;
1789 return hres;
1792 static void resolve_labels(compiler_ctx_t *ctx, unsigned off)
1794 instr_t *instr;
1796 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
1797 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->u.arg->uint & LABEL_FLAG)) {
1798 assert((instr->u.arg->uint & ~LABEL_FLAG) < ctx->labels_cnt);
1799 instr->u.arg->uint = ctx->labels[instr->u.arg->uint & ~LABEL_FLAG];
1801 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
1804 ctx->labels_cnt = 0;
1807 void release_bytecode(bytecode_t *code)
1809 unsigned i;
1811 if(--code->ref)
1812 return;
1814 for(i=0; i < code->bstr_cnt; i++)
1815 SysFreeString(code->bstr_pool[i]);
1816 for(i=0; i < code->str_cnt; i++)
1817 jsstr_release(code->str_pool[i]);
1819 heap_free(code->source);
1820 heap_pool_free(&code->heap);
1821 heap_free(code->bstr_pool);
1822 heap_free(code->str_pool);
1823 heap_free(code->instrs);
1824 heap_free(code);
1827 static HRESULT init_code(compiler_ctx_t *compiler, const WCHAR *source)
1829 compiler->code = heap_alloc_zero(sizeof(bytecode_t));
1830 if(!compiler->code)
1831 return E_OUTOFMEMORY;
1833 compiler->code->ref = 1;
1834 heap_pool_init(&compiler->code->heap);
1836 compiler->code->source = heap_strdupW(source);
1837 if(!compiler->code->source) {
1838 release_bytecode(compiler->code);
1839 return E_OUTOFMEMORY;
1842 compiler->code->instrs = heap_alloc(64 * sizeof(instr_t));
1843 if(!compiler->code->instrs) {
1844 release_bytecode(compiler->code);
1845 return E_OUTOFMEMORY;
1848 compiler->code_size = 64;
1849 compiler->code_off = 1;
1850 return S_OK;
1853 static HRESULT compile_function(compiler_ctx_t *ctx, source_elements_t *source, function_expression_t *func_expr,
1854 BOOL from_eval, function_code_t *func)
1856 variable_declaration_t *var_iter;
1857 function_expression_t *iter;
1858 unsigned off, i;
1859 HRESULT hres;
1861 TRACE("\n");
1863 ctx->var_head = ctx->var_tail = NULL;
1864 ctx->func_head = ctx->func_tail = NULL;
1865 ctx->from_eval = from_eval;
1867 off = ctx->code_off;
1868 ctx->func = func;
1869 hres = compile_block_statement(ctx, source->statement);
1870 if(FAILED(hres))
1871 return hres;
1873 resolve_labels(ctx, off);
1875 hres = push_instr_uint(ctx, OP_ret, !from_eval);
1876 if(FAILED(hres))
1877 return hres;
1879 if(TRACE_ON(jscript_disas))
1880 dump_code(ctx, off);
1882 func->instr_off = off;
1884 if(func_expr) {
1885 if(func_expr->identifier) {
1886 func->name = compiler_alloc_bstr(ctx, func_expr->identifier);
1887 if(!func->name)
1888 return E_OUTOFMEMORY;
1891 if(func_expr->event_target) {
1892 func->event_target = compiler_alloc_bstr(ctx, func_expr->event_target);
1893 if(!func->event_target)
1894 return E_OUTOFMEMORY;
1898 if(func_expr) {
1899 parameter_t *param_iter;
1901 func->source = func_expr->src_str;
1902 func->source_len = func_expr->src_len;
1904 for(param_iter = func_expr->parameter_list; param_iter; param_iter = param_iter->next)
1905 func->param_cnt++;
1907 func->params = compiler_alloc(ctx->code, func->param_cnt * sizeof(*func->params));
1908 if(!func->params)
1909 return E_OUTOFMEMORY;
1911 for(param_iter = func_expr->parameter_list, i=0; param_iter; param_iter = param_iter->next, i++) {
1912 func->params[i] = compiler_alloc_bstr(ctx, param_iter->identifier);
1913 if(!func->params[i])
1914 return E_OUTOFMEMORY;
1918 func->variables = compiler_alloc(ctx->code, func->var_cnt * sizeof(*func->variables));
1919 if(!func->variables)
1920 return E_OUTOFMEMORY;
1922 for(var_iter = ctx->var_head, i=0; var_iter; var_iter = var_iter->global_next, i++) {
1923 func->variables[i] = compiler_alloc_bstr(ctx, var_iter->identifier);
1924 if(!func->variables[i])
1925 return E_OUTOFMEMORY;
1928 assert(i == func->var_cnt);
1930 func->funcs = compiler_alloc(ctx->code, func->func_cnt * sizeof(*func->funcs));
1931 if(!func->funcs)
1932 return E_OUTOFMEMORY;
1933 memset(func->funcs, 0, func->func_cnt * sizeof(*func->funcs));
1935 for(iter = ctx->func_head, i=0; iter; iter = iter->next, i++) {
1936 hres = compile_function(ctx, iter->source_elements, iter, FALSE, func->funcs+i);
1937 if(FAILED(hres))
1938 return hres;
1941 assert(i == func->func_cnt);
1943 return S_OK;
1946 static HRESULT parse_arguments(compiler_ctx_t *ctx, const WCHAR *args, BSTR *arg_array, unsigned *args_size)
1948 const WCHAR *ptr = args, *ptr2;
1949 unsigned arg_cnt = 0;
1951 while(isspaceW(*ptr))
1952 ptr++;
1953 if(!*ptr) {
1954 if(args_size)
1955 *args_size = 0;
1956 return S_OK;
1959 while(1) {
1960 if(!isalphaW(*ptr) && *ptr != '_') {
1961 FIXME("expected alpha or '_': %s\n", debugstr_w(ptr));
1962 return E_FAIL;
1965 ptr2 = ptr;
1966 while(isalnumW(*ptr) || *ptr == '_')
1967 ptr++;
1969 if(*ptr && *ptr != ',' && !isspaceW(*ptr)) {
1970 FIXME("unexpected har %s\n", debugstr_w(ptr));
1971 return E_FAIL;
1974 if(arg_array) {
1975 arg_array[arg_cnt] = compiler_alloc_bstr_len(ctx, ptr2, ptr-ptr2);
1976 if(!arg_array[arg_cnt])
1977 return E_OUTOFMEMORY;
1979 arg_cnt++;
1981 while(isspaceW(*ptr))
1982 ptr++;
1983 if(!*ptr)
1984 break;
1985 if(*ptr != ',') {
1986 FIXME("expected ',': %s\n", debugstr_w(ptr));
1987 return E_FAIL;
1990 ptr++;
1991 while(isspaceW(*ptr))
1992 ptr++;
1995 if(args_size)
1996 *args_size = arg_cnt;
1997 return S_OK;
2000 static HRESULT compile_arguments(compiler_ctx_t *ctx, const WCHAR *args)
2002 HRESULT hres;
2004 hres = parse_arguments(ctx, args, NULL, &ctx->code->global_code.param_cnt);
2005 if(FAILED(hres))
2006 return hres;
2008 ctx->code->global_code.params = compiler_alloc(ctx->code,
2009 ctx->code->global_code.param_cnt * sizeof(*ctx->code->global_code.params));
2010 if(!ctx->code->global_code.params)
2011 return E_OUTOFMEMORY;
2013 return parse_arguments(ctx, args, ctx->code->global_code.params, NULL);
2016 HRESULT compile_script(script_ctx_t *ctx, const WCHAR *code, const WCHAR *args, const WCHAR *delimiter,
2017 BOOL from_eval, BOOL use_decode, bytecode_t **ret)
2019 compiler_ctx_t compiler = {0};
2020 HRESULT hres;
2022 hres = init_code(&compiler, code);
2023 if(FAILED(hres))
2024 return hres;
2026 if(args) {
2027 hres = compile_arguments(&compiler, args);
2028 if(FAILED(hres))
2029 return hres;
2032 if(use_decode) {
2033 hres = decode_source(compiler.code->source);
2034 if(FAILED(hres)) {
2035 WARN("Decoding failed\n");
2036 return hres;
2040 hres = script_parse(ctx, compiler.code->source, delimiter, from_eval, &compiler.parser);
2041 if(FAILED(hres)) {
2042 release_bytecode(compiler.code);
2043 return hres;
2046 hres = compile_function(&compiler, compiler.parser->source, NULL, from_eval, &compiler.code->global_code);
2047 parser_release(compiler.parser);
2048 if(FAILED(hres)) {
2049 release_bytecode(compiler.code);
2050 return hres;
2053 *ret = compiler.code;
2054 return S_OK;