wineqtdecoder: Recreate audio extraction session when we run out of frames.
[wine.git] / dlls / jscript / compile.c
blobd3deccdbc3faf56046043e0ca350c46dbf2397d0
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"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28 WINE_DECLARE_DEBUG_CHANNEL(jscript_disas);
30 typedef struct _statement_ctx_t {
31 unsigned stack_use;
32 BOOL using_scope;
33 BOOL using_except;
35 unsigned break_label;
36 unsigned continue_label;
38 const labelled_statement_t *labelled_stat;
40 struct _statement_ctx_t *next;
41 } statement_ctx_t;
43 typedef struct {
44 parser_ctx_t *parser;
45 bytecode_t *code;
47 unsigned code_off;
48 unsigned code_size;
50 unsigned *labels;
51 unsigned labels_size;
52 unsigned labels_cnt;
54 statement_ctx_t *stat_ctx;
55 function_code_t *func;
57 variable_declaration_t *var_head;
58 variable_declaration_t *var_tail;
60 function_expression_t *func_head;
61 function_expression_t *func_tail;
62 } compiler_ctx_t;
64 static const struct {
65 const char *op_str;
66 instr_arg_type_t arg1_type;
67 instr_arg_type_t arg2_type;
68 } instr_info[] = {
69 #define X(n,a,b,c) {#n,b,c},
70 OP_LIST
71 #undef X
74 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
76 switch(type) {
77 case ARG_STR:
78 TRACE_(jscript_disas)("\t%s", debugstr_w(arg->str));
79 break;
80 case ARG_BSTR:
81 TRACE_(jscript_disas)("\t%s", debugstr_wn(arg->bstr, SysStringLen(arg->bstr)));
82 break;
83 case ARG_INT:
84 TRACE_(jscript_disas)("\t%d", arg->uint);
85 break;
86 case ARG_UINT:
87 case ARG_ADDR:
88 TRACE_(jscript_disas)("\t%u", arg->uint);
89 break;
90 case ARG_FUNC:
91 case ARG_NONE:
92 break;
93 default:
94 assert(0);
98 static void dump_code(compiler_ctx_t *ctx, unsigned off)
100 instr_t *instr;
102 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
103 TRACE_(jscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
104 if(instr_info[instr->op].arg1_type == ARG_DBL) {
105 TRACE_(jscript_disas)("\t%lf", instr->u.dbl);
106 }else {
107 dump_instr_arg(instr_info[instr->op].arg1_type, instr->u.arg);
108 dump_instr_arg(instr_info[instr->op].arg2_type, instr->u.arg+1);
110 TRACE_(jscript_disas)("\n");
114 static HRESULT compile_expression(compiler_ctx_t*,expression_t*);
115 static HRESULT compile_statement(compiler_ctx_t*,statement_ctx_t*,statement_t*);
117 static inline void *compiler_alloc(bytecode_t *code, size_t size)
119 return jsheap_alloc(&code->heap, size);
122 static WCHAR *compiler_alloc_string(bytecode_t *code, const WCHAR *str)
124 size_t size;
125 WCHAR *ret;
127 size = (strlenW(str)+1)*sizeof(WCHAR);
128 ret = compiler_alloc(code, size);
129 if(ret)
130 memcpy(ret, str, size);
131 return ret;
134 static BSTR compiler_alloc_bstr(compiler_ctx_t *ctx, const WCHAR *str)
136 if(!ctx->code->bstr_pool_size) {
137 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
138 if(!ctx->code->bstr_pool)
139 return NULL;
140 ctx->code->bstr_pool_size = 8;
141 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
142 BSTR *new_pool;
144 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
145 if(!new_pool)
146 return NULL;
148 ctx->code->bstr_pool = new_pool;
149 ctx->code->bstr_pool_size *= 2;
152 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
153 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
154 return NULL;
156 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
159 static unsigned push_instr(compiler_ctx_t *ctx, jsop_t op)
161 assert(ctx->code_size >= ctx->code_off);
163 if(ctx->code_size == ctx->code_off) {
164 instr_t *new_instrs;
166 new_instrs = heap_realloc(ctx->code->instrs, ctx->code_size*2*sizeof(instr_t));
167 if(!new_instrs)
168 return 0;
170 ctx->code->instrs = new_instrs;
171 ctx->code_size *= 2;
174 ctx->code->instrs[ctx->code_off].op = op;
175 return ctx->code_off++;
178 static inline instr_t *instr_ptr(compiler_ctx_t *ctx, unsigned off)
180 assert(off < ctx->code_off);
181 return ctx->code->instrs + off;
184 static HRESULT push_instr_int(compiler_ctx_t *ctx, jsop_t op, LONG arg)
186 unsigned instr;
188 instr = push_instr(ctx, op);
189 if(!instr)
190 return E_OUTOFMEMORY;
192 instr_ptr(ctx, instr)->u.arg->lng = arg;
193 return S_OK;
196 static HRESULT push_instr_str(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
198 unsigned instr;
199 WCHAR *str;
201 str = compiler_alloc_string(ctx->code, arg);
202 if(!str)
203 return E_OUTOFMEMORY;
205 instr = push_instr(ctx, op);
206 if(!instr)
207 return E_OUTOFMEMORY;
209 instr_ptr(ctx, instr)->u.arg->str = str;
210 return S_OK;
213 static HRESULT push_instr_bstr(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
215 unsigned instr;
216 WCHAR *str;
218 str = compiler_alloc_bstr(ctx, arg);
219 if(!str)
220 return E_OUTOFMEMORY;
222 instr = push_instr(ctx, op);
223 if(!instr)
224 return E_OUTOFMEMORY;
226 instr_ptr(ctx, instr)->u.arg->bstr = str;
227 return S_OK;
230 static HRESULT push_instr_bstr_uint(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg1, unsigned arg2)
232 unsigned instr;
233 WCHAR *str;
235 str = compiler_alloc_bstr(ctx, arg1);
236 if(!str)
237 return E_OUTOFMEMORY;
239 instr = push_instr(ctx, op);
240 if(!instr)
241 return E_OUTOFMEMORY;
243 instr_ptr(ctx, instr)->u.arg[0].bstr = str;
244 instr_ptr(ctx, instr)->u.arg[1].uint = arg2;
245 return S_OK;
248 static HRESULT push_instr_uint_str(compiler_ctx_t *ctx, jsop_t op, unsigned arg1, const WCHAR *arg2)
250 unsigned instr;
251 WCHAR *str;
253 str = compiler_alloc_string(ctx->code, arg2);
254 if(!str)
255 return E_OUTOFMEMORY;
257 instr = push_instr(ctx, op);
258 if(!instr)
259 return E_OUTOFMEMORY;
261 instr_ptr(ctx, instr)->u.arg[0].uint = arg1;
262 instr_ptr(ctx, instr)->u.arg[1].str = str;
263 return S_OK;
266 static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
268 unsigned instr;
270 instr = push_instr(ctx, op);
271 if(!instr)
272 return E_OUTOFMEMORY;
274 instr_ptr(ctx, instr)->u.dbl = arg;
275 return S_OK;
278 static inline void set_arg_uint(compiler_ctx_t *ctx, unsigned instr, unsigned arg)
280 instr_ptr(ctx, instr)->u.arg->uint = arg;
283 static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
285 unsigned instr;
287 instr = push_instr(ctx, op);
288 if(!instr)
289 return E_OUTOFMEMORY;
291 set_arg_uint(ctx, instr, arg);
292 return S_OK;
295 static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
297 HRESULT hres;
299 hres = compile_expression(ctx, expr->expression1);
300 if(FAILED(hres))
301 return hres;
303 hres = compile_expression(ctx, expr->expression2);
304 if(FAILED(hres))
305 return hres;
307 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
310 static HRESULT compile_unary_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op)
312 HRESULT hres;
314 hres = compile_expression(ctx, expr->expression);
315 if(FAILED(hres))
316 return hres;
318 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
321 /* ECMA-262 3rd Edition 11.2.1 */
322 static HRESULT compile_member_expression(compiler_ctx_t *ctx, member_expression_t *expr)
324 HRESULT hres;
326 hres = compile_expression(ctx, expr->expression);
327 if(FAILED(hres))
328 return hres;
330 return push_instr_bstr(ctx, OP_member, expr->identifier);
333 #define LABEL_FLAG 0x80000000
335 static unsigned alloc_label(compiler_ctx_t *ctx)
337 if(!ctx->labels_size) {
338 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
339 if(!ctx->labels)
340 return 0;
341 ctx->labels_size = 8;
342 }else if(ctx->labels_size == ctx->labels_cnt) {
343 unsigned *new_labels;
345 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
346 if(!new_labels)
347 return 0;
349 ctx->labels = new_labels;
350 ctx->labels_size *= 2;
353 return ctx->labels_cnt++ | LABEL_FLAG;
356 static void label_set_addr(compiler_ctx_t *ctx, unsigned label)
358 assert(label & LABEL_FLAG);
359 ctx->labels[label & ~LABEL_FLAG] = ctx->code_off;
362 static inline BOOL is_memberid_expr(expression_type_t type)
364 return type == EXPR_IDENT || type == EXPR_MEMBER || type == EXPR_ARRAY;
367 static HRESULT compile_memberid_expression(compiler_ctx_t *ctx, expression_t *expr, unsigned flags)
369 HRESULT hres = S_OK;
371 switch(expr->type) {
372 case EXPR_IDENT: {
373 identifier_expression_t *ident_expr = (identifier_expression_t*)expr;
375 hres = push_instr_bstr_uint(ctx, OP_identid, ident_expr->identifier, flags);
376 break;
378 case EXPR_ARRAY: {
379 binary_expression_t *array_expr = (binary_expression_t*)expr;
381 hres = compile_expression(ctx, array_expr->expression1);
382 if(FAILED(hres))
383 return hres;
385 hres = compile_expression(ctx, array_expr->expression2);
386 if(FAILED(hres))
387 return hres;
389 hres = push_instr_uint(ctx, OP_memberid, flags);
390 break;
392 case EXPR_MEMBER: {
393 member_expression_t *member_expr = (member_expression_t*)expr;
395 hres = compile_expression(ctx, member_expr->expression);
396 if(FAILED(hres))
397 return hres;
399 /* FIXME: Potential optimization */
400 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
401 if(FAILED(hres))
402 return hres;
404 hres = push_instr_uint(ctx, OP_memberid, flags);
405 break;
407 default:
408 assert(0);
411 return hres;
414 static HRESULT compile_increment_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op, int n)
416 HRESULT hres;
418 if(!is_memberid_expr(expr->expression->type)) {
419 hres = compile_expression(ctx, expr->expression);
420 if(FAILED(hres))
421 return hres;
423 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
426 hres = compile_memberid_expression(ctx, expr->expression, fdexNameEnsure);
427 if(FAILED(hres))
428 return hres;
430 return push_instr_int(ctx, op, n);
433 /* ECMA-262 3rd Edition 11.14 */
434 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr)
436 HRESULT hres;
438 hres = compile_expression(ctx, expr->expression1);
439 if(FAILED(hres))
440 return hres;
442 if(!push_instr(ctx, OP_pop))
443 return E_OUTOFMEMORY;
445 return compile_expression(ctx, expr->expression2);
448 /* ECMA-262 3rd Edition 11.11 */
449 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
451 unsigned instr;
452 HRESULT hres;
454 hres = compile_expression(ctx, expr->expression1);
455 if(FAILED(hres))
456 return hres;
458 instr = push_instr(ctx, op);
459 if(!instr)
460 return E_OUTOFMEMORY;
462 hres = compile_expression(ctx, expr->expression2);
463 if(FAILED(hres))
464 return hres;
466 set_arg_uint(ctx, instr, ctx->code_off);
467 return S_OK;
470 /* ECMA-262 3rd Edition 11.12 */
471 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
473 unsigned jmp_false, jmp_end;
474 HRESULT hres;
476 hres = compile_expression(ctx, expr->expression);
477 if(FAILED(hres))
478 return hres;
480 jmp_false = push_instr(ctx, OP_cnd_z);
481 if(!jmp_false)
482 return E_OUTOFMEMORY;
484 hres = compile_expression(ctx, expr->true_expression);
485 if(FAILED(hres))
486 return hres;
488 jmp_end = push_instr(ctx, OP_jmp);
489 if(!jmp_end)
490 return E_OUTOFMEMORY;
492 set_arg_uint(ctx, jmp_false, ctx->code_off);
493 if(!push_instr(ctx, OP_pop))
494 return E_OUTOFMEMORY;
496 hres = compile_expression(ctx, expr->false_expression);
497 if(FAILED(hres))
498 return hres;
500 set_arg_uint(ctx, jmp_end, ctx->code_off);
501 return S_OK;
504 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
506 unsigned arg_cnt = 0;
507 argument_t *arg;
508 HRESULT hres;
510 hres = compile_expression(ctx, expr->expression);
511 if(FAILED(hres))
512 return hres;
514 for(arg = expr->argument_list; arg; arg = arg->next) {
515 hres = compile_expression(ctx, arg->expr);
516 if(FAILED(hres))
517 return hres;
518 arg_cnt++;
521 return push_instr_uint(ctx, OP_new, arg_cnt);
524 static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL *no_ret)
526 unsigned arg_cnt = 0;
527 argument_t *arg;
528 unsigned instr;
529 jsop_t op;
530 HRESULT hres;
532 if(is_memberid_expr(expr->expression->type)) {
533 op = OP_call_member;
534 hres = compile_memberid_expression(ctx, expr->expression, 0);
535 }else {
536 op = OP_call;
537 hres = compile_expression(ctx, expr->expression);
540 if(FAILED(hres))
541 return hres;
543 for(arg = expr->argument_list; arg; arg = arg->next) {
544 hres = compile_expression(ctx, arg->expr);
545 if(FAILED(hres))
546 return hres;
547 arg_cnt++;
550 instr = push_instr(ctx, op);
551 if(!instr)
552 return E_OUTOFMEMORY;
554 instr_ptr(ctx, instr)->u.arg[0].uint = arg_cnt;
555 instr_ptr(ctx, instr)->u.arg[1].lng = no_ret == NULL;
556 if(no_ret)
557 *no_ret = TRUE;
558 return S_OK;
561 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
563 HRESULT hres;
565 switch(expr->expression->type) {
566 case EXPR_ARRAY: {
567 binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
569 hres = compile_expression(ctx, array_expr->expression1);
570 if(FAILED(hres))
571 return hres;
573 hres = compile_expression(ctx, array_expr->expression2);
574 if(FAILED(hres))
575 return hres;
577 if(!push_instr(ctx, OP_delete))
578 return E_OUTOFMEMORY;
579 break;
581 case EXPR_MEMBER: {
582 member_expression_t *member_expr = (member_expression_t*)expr->expression;
584 hres = compile_expression(ctx, member_expr->expression);
585 if(FAILED(hres))
586 return hres;
588 /* FIXME: Potential optimization */
589 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
590 if(FAILED(hres))
591 return hres;
593 if(!push_instr(ctx, OP_delete))
594 return E_OUTOFMEMORY;
595 break;
597 case EXPR_IDENT:
598 return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
599 default: {
600 const WCHAR fixmeW[] = {'F','I','X','M','E',0};
602 WARN("invalid delete, unimplemented exception message\n");
604 hres = compile_expression(ctx, expr->expression);
605 if(FAILED(hres))
606 return hres;
608 return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, fixmeW);
612 return S_OK;
615 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
617 BOOL use_throw_path = FALSE;
618 unsigned arg_cnt = 0;
619 HRESULT hres;
621 if(expr->expression1->type == EXPR_CALL) {
622 call_expression_t *call_expr = (call_expression_t*)expr->expression1;
623 argument_t *arg;
625 if(op != OP_LAST) {
626 FIXME("op %d not supported on parametrized assign expressions\n", op);
627 return E_NOTIMPL;
630 if(is_memberid_expr(call_expr->expression->type) && call_expr->argument_list) {
631 hres = compile_memberid_expression(ctx, call_expr->expression, fdexNameEnsure);
632 if(FAILED(hres))
633 return hres;
635 for(arg = call_expr->argument_list; arg; arg = arg->next) {
636 hres = compile_expression(ctx, arg->expr);
637 if(FAILED(hres))
638 return hres;
639 arg_cnt++;
641 }else {
642 use_throw_path = TRUE;
644 }else if(is_memberid_expr(expr->expression1->type)) {
645 hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
646 if(FAILED(hres))
647 return hres;
648 }else {
649 use_throw_path = TRUE;
652 if(use_throw_path) {
653 /* Illegal assignment: evaluate and throw */
654 hres = compile_expression(ctx, expr->expression1);
655 if(FAILED(hres))
656 return hres;
658 hres = compile_expression(ctx, expr->expression2);
659 if(FAILED(hres))
660 return hres;
662 if(op != OP_LAST && !push_instr(ctx, op))
663 return E_OUTOFMEMORY;
665 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
668 if(op != OP_LAST && !push_instr(ctx, OP_refval))
669 return E_OUTOFMEMORY;
671 hres = compile_expression(ctx, expr->expression2);
672 if(FAILED(hres))
673 return hres;
675 if(op != OP_LAST && !push_instr(ctx, op))
676 return E_OUTOFMEMORY;
678 if(arg_cnt)
679 return push_instr_uint(ctx, OP_assign_call, arg_cnt);
681 if(!push_instr(ctx, OP_assign))
682 return E_OUTOFMEMORY;
684 return S_OK;
687 static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
689 jsop_t op;
690 HRESULT hres;
692 if(is_memberid_expr(expr->expression->type)) {
693 if(expr->expression->type == EXPR_IDENT)
694 return push_instr_str(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
696 op = OP_typeofid;
697 hres = compile_memberid_expression(ctx, expr->expression, 0);
698 }else {
699 op = OP_typeof;
700 hres = compile_expression(ctx, expr->expression);
702 if(FAILED(hres))
703 return hres;
705 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
708 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
710 switch(literal->type) {
711 case LT_BOOL:
712 return push_instr_int(ctx, OP_bool, literal->u.bval);
713 case LT_DOUBLE:
714 return push_instr_double(ctx, OP_double, literal->u.dval);
715 case LT_INT:
716 return push_instr_int(ctx, OP_int, literal->u.lval);
717 case LT_NULL:
718 return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
719 case LT_STRING:
720 return push_instr_str(ctx, OP_str, literal->u.wstr);
721 case LT_REGEXP: {
722 unsigned instr;
723 WCHAR *str;
725 str = compiler_alloc(ctx->code, (literal->u.regexp.str_len+1)*sizeof(WCHAR));
726 if(!str)
727 return E_OUTOFMEMORY;
728 memcpy(str, literal->u.regexp.str, literal->u.regexp.str_len*sizeof(WCHAR));
729 str[literal->u.regexp.str_len] = 0;
731 instr = push_instr(ctx, OP_regexp);
732 if(!instr)
733 return E_OUTOFMEMORY;
735 instr_ptr(ctx, instr)->u.arg[0].str = str;
736 instr_ptr(ctx, instr)->u.arg[1].uint = literal->u.regexp.flags;
737 return S_OK;
739 default:
740 assert(0);
741 return E_FAIL;
745 static HRESULT literal_as_bstr(compiler_ctx_t *ctx, literal_t *literal, BSTR *str)
747 switch(literal->type) {
748 case LT_STRING:
749 *str = compiler_alloc_bstr(ctx, literal->u.wstr);
750 break;
751 case LT_INT:
752 *str = int_to_bstr(literal->u.lval);
753 break;
754 case LT_DOUBLE:
755 return double_to_bstr(literal->u.dval, str);
756 default:
757 assert(0);
760 return *str ? S_OK : E_OUTOFMEMORY;
763 static HRESULT compile_array_literal(compiler_ctx_t *ctx, array_literal_expression_t *expr)
765 unsigned i, elem_cnt = expr->length;
766 array_element_t *iter;
767 HRESULT hres;
769 for(iter = expr->element_list; iter; iter = iter->next) {
770 elem_cnt += iter->elision+1;
772 for(i=0; i < iter->elision; i++) {
773 if(!push_instr(ctx, OP_undefined))
774 return E_OUTOFMEMORY;
777 hres = compile_expression(ctx, iter->expr);
778 if(FAILED(hres))
779 return hres;
782 for(i=0; i < expr->length; i++) {
783 if(!push_instr(ctx, OP_undefined))
784 return E_OUTOFMEMORY;
787 return push_instr_uint(ctx, OP_carray, elem_cnt);
790 static HRESULT compile_object_literal(compiler_ctx_t *ctx, property_value_expression_t *expr)
792 prop_val_t *iter;
793 unsigned instr;
794 BSTR name;
795 HRESULT hres;
797 if(!push_instr(ctx, OP_new_obj))
798 return E_OUTOFMEMORY;
800 for(iter = expr->property_list; iter; iter = iter->next) {
801 hres = literal_as_bstr(ctx, iter->name, &name);
802 if(FAILED(hres))
803 return hres;
805 hres = compile_expression(ctx, iter->value);
806 if(FAILED(hres))
807 return hres;
809 instr = push_instr(ctx, OP_obj_prop);
810 if(!instr)
811 return E_OUTOFMEMORY;
813 instr_ptr(ctx, instr)->u.arg->bstr = name;
816 return S_OK;
819 static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr)
821 ctx->func_tail = ctx->func_tail ? (ctx->func_tail->next = expr) : (ctx->func_head = expr);
823 /* FIXME: not exactly right */
824 if(expr->identifier) {
825 ctx->func->func_cnt++;
826 return push_instr_bstr(ctx, OP_ident, expr->identifier);
829 return push_instr_uint(ctx, OP_func, ctx->func->func_cnt++);
832 static HRESULT compile_expression_noret(compiler_ctx_t *ctx, expression_t *expr, BOOL *no_ret)
834 switch(expr->type) {
835 case EXPR_ADD:
836 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
837 case EXPR_AND:
838 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_z);
839 case EXPR_ARRAY:
840 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
841 case EXPR_ARRAYLIT:
842 return compile_array_literal(ctx, (array_literal_expression_t*)expr);
843 case EXPR_ASSIGN:
844 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
845 case EXPR_ASSIGNADD:
846 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
847 case EXPR_ASSIGNAND:
848 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_and);
849 case EXPR_ASSIGNSUB:
850 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
851 case EXPR_ASSIGNMUL:
852 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
853 case EXPR_ASSIGNDIV:
854 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
855 case EXPR_ASSIGNMOD:
856 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
857 case EXPR_ASSIGNOR:
858 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
859 case EXPR_ASSIGNLSHIFT:
860 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_lshift);
861 case EXPR_ASSIGNRSHIFT:
862 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift);
863 case EXPR_ASSIGNRRSHIFT:
864 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
865 case EXPR_ASSIGNXOR:
866 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
867 case EXPR_BAND:
868 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
869 case EXPR_BITNEG:
870 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
871 case EXPR_BOR:
872 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
873 case EXPR_CALL:
874 return compile_call_expression(ctx, (call_expression_t*)expr, no_ret);
875 case EXPR_COMMA:
876 return compile_comma_expression(ctx, (binary_expression_t*)expr);
877 case EXPR_COND:
878 return compile_conditional_expression(ctx, (conditional_expression_t*)expr);
879 case EXPR_DELETE:
880 return compile_delete_expression(ctx, (unary_expression_t*)expr);
881 case EXPR_DIV:
882 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
883 case EXPR_EQ:
884 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
885 case EXPR_EQEQ:
886 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
887 case EXPR_FUNC:
888 return compile_function_expression(ctx, (function_expression_t*)expr);
889 case EXPR_GREATER:
890 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
891 case EXPR_GREATEREQ:
892 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
893 case EXPR_IDENT:
894 return push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
895 case EXPR_IN:
896 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
897 case EXPR_INSTANCEOF:
898 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
899 case EXPR_LESS:
900 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
901 case EXPR_LESSEQ:
902 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
903 case EXPR_LITERAL:
904 return compile_literal(ctx, ((literal_expression_t*)expr)->literal);
905 case EXPR_LOGNEG:
906 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
907 case EXPR_LSHIFT:
908 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
909 case EXPR_MEMBER:
910 return compile_member_expression(ctx, (member_expression_t*)expr);
911 case EXPR_MINUS:
912 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
913 case EXPR_MOD:
914 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
915 case EXPR_MUL:
916 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
917 case EXPR_NEW:
918 return compile_new_expression(ctx, (call_expression_t*)expr);
919 case EXPR_NOTEQ:
920 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
921 case EXPR_NOTEQEQ:
922 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
923 case EXPR_OR:
924 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_nz);
925 case EXPR_PLUS:
926 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
927 case EXPR_POSTDEC:
928 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
929 case EXPR_POSTINC:
930 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
931 case EXPR_PREDEC:
932 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
933 case EXPR_PREINC:
934 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
935 case EXPR_PROPVAL:
936 return compile_object_literal(ctx, (property_value_expression_t*)expr);
937 case EXPR_RSHIFT:
938 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
939 case EXPR_RRSHIFT:
940 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
941 case EXPR_SUB:
942 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
943 case EXPR_THIS:
944 return push_instr(ctx, OP_this) ? S_OK : E_OUTOFMEMORY;
945 case EXPR_TYPEOF:
946 return compile_typeof_expression(ctx, (unary_expression_t*)expr);
947 case EXPR_VOID:
948 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
949 case EXPR_BXOR:
950 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
951 default:
952 assert(0);
955 return S_OK;
958 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr)
960 return compile_expression_noret(ctx, expr, NULL);
963 static inline BOOL is_loop_statement(statement_type_t type)
965 return type == STAT_FOR || type == STAT_FORIN || type == STAT_WHILE;
968 /* ECMA-262 3rd Edition 12.1 */
969 static HRESULT compile_block_statement(compiler_ctx_t *ctx, statement_t *iter)
971 HRESULT hres;
973 /* FIXME: do it only if needed */
974 if(!iter)
975 return push_instr(ctx, OP_undefined) ? S_OK : E_OUTOFMEMORY;
977 while(1) {
978 hres = compile_statement(ctx, NULL, iter);
979 if(FAILED(hres))
980 return hres;
982 iter = iter->next;
983 if(!iter)
984 break;
986 if(!push_instr(ctx, OP_pop))
987 return E_OUTOFMEMORY;
990 return S_OK;
993 /* ECMA-262 3rd Edition 12.2 */
994 static HRESULT compile_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
996 variable_declaration_t *iter;
997 HRESULT hres;
999 assert(list != NULL);
1001 if(ctx->var_tail)
1002 ctx->var_tail->global_next = list;
1003 else
1004 ctx->var_head = list;
1006 for(iter = list; iter; iter = iter->next) {
1007 ctx->func->var_cnt++;
1008 iter->global_next = iter->next;
1009 if(!iter->next)
1010 ctx->var_tail = iter;
1012 if(!iter->expr)
1013 continue;
1015 hres = compile_expression(ctx, iter->expr);
1016 if(FAILED(hres))
1017 return hres;
1019 hres = push_instr_bstr(ctx, OP_var_set, iter->identifier);
1020 if(FAILED(hres))
1021 return hres;
1024 return S_OK;
1027 /* ECMA-262 3rd Edition 12.2 */
1028 static HRESULT compile_var_statement(compiler_ctx_t *ctx, var_statement_t *stat)
1030 HRESULT hres;
1032 hres = compile_variable_list(ctx, stat->variable_list);
1033 if(FAILED(hres))
1034 return hres;
1036 return push_instr(ctx, OP_undefined) ? S_OK : E_OUTOFMEMORY;
1039 /* ECMA-262 3rd Edition 12.4 */
1040 static HRESULT compile_expression_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1042 BOOL no_ret = FALSE;
1043 HRESULT hres;
1045 hres = compile_expression_noret(ctx, stat->expr, &no_ret);
1046 if(FAILED(hres))
1047 return hres;
1049 /* FIXME: that's a big potential optimization */
1050 if(no_ret && !push_instr(ctx, OP_undefined))
1051 return E_OUTOFMEMORY;
1053 return S_OK;
1056 /* ECMA-262 3rd Edition 12.5 */
1057 static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat)
1059 unsigned jmp_else, jmp_end;
1060 HRESULT hres;
1062 hres = compile_expression(ctx, stat->expr);
1063 if(FAILED(hres))
1064 return hres;
1066 jmp_else = push_instr(ctx, OP_jmp_z);
1067 if(!jmp_else)
1068 return E_OUTOFMEMORY;
1070 hres = compile_statement(ctx, NULL, stat->if_stat);
1071 if(FAILED(hres))
1072 return hres;
1074 jmp_end = push_instr(ctx, OP_jmp);
1075 if(!jmp_end)
1076 return E_OUTOFMEMORY;
1078 set_arg_uint(ctx, jmp_else, ctx->code_off);
1080 if(stat->else_stat) {
1081 hres = compile_statement(ctx, NULL, stat->else_stat);
1082 if(FAILED(hres))
1083 return hres;
1084 }else {
1085 /* FIXME: We could sometimes avoid it */
1086 if(!push_instr(ctx, OP_undefined))
1087 return E_OUTOFMEMORY;
1090 set_arg_uint(ctx, jmp_end, ctx->code_off);
1091 return S_OK;
1094 /* ECMA-262 3rd Edition 12.6.2 */
1095 static HRESULT compile_while_statement(compiler_ctx_t *ctx, while_statement_t *stat)
1097 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1098 unsigned jmp_off;
1099 HRESULT hres;
1101 stat_ctx.break_label = alloc_label(ctx);
1102 if(!stat_ctx.break_label)
1103 return E_OUTOFMEMORY;
1105 stat_ctx.continue_label = alloc_label(ctx);
1106 if(!stat_ctx.continue_label)
1107 return E_OUTOFMEMORY;
1109 if(!stat->do_while) {
1110 /* FIXME: avoid */
1111 if(!push_instr(ctx, OP_undefined))
1112 return E_OUTOFMEMORY;
1114 jmp_off = ctx->code_off;
1115 label_set_addr(ctx, stat_ctx.continue_label);
1116 hres = compile_expression(ctx, stat->expr);
1117 if(FAILED(hres))
1118 return hres;
1120 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1121 if(FAILED(hres))
1122 return hres;
1124 if(!push_instr(ctx, OP_pop))
1125 return E_OUTOFMEMORY;
1126 }else {
1127 jmp_off = ctx->code_off;
1130 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1131 if(FAILED(hres))
1132 return hres;
1134 if(stat->do_while) {
1135 label_set_addr(ctx, stat_ctx.continue_label);
1136 hres = compile_expression(ctx, stat->expr);
1137 if(FAILED(hres))
1138 return hres;
1140 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1141 if(FAILED(hres))
1142 return hres;
1144 if(!push_instr(ctx, OP_pop))
1145 return E_OUTOFMEMORY;
1148 hres = push_instr_uint(ctx, OP_jmp, jmp_off);
1149 if(FAILED(hres))
1150 return hres;
1152 label_set_addr(ctx, stat_ctx.break_label);
1153 return S_OK;
1156 /* ECMA-262 3rd Edition 12.6.3 */
1157 static HRESULT compile_for_statement(compiler_ctx_t *ctx, for_statement_t *stat)
1159 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1160 unsigned expr_off;
1161 HRESULT hres;
1163 if(stat->variable_list) {
1164 hres = compile_variable_list(ctx, stat->variable_list);
1165 if(FAILED(hres))
1166 return hres;
1167 }else if(stat->begin_expr) {
1168 BOOL no_ret = FALSE;
1170 hres = compile_expression_noret(ctx, stat->begin_expr, &no_ret);
1171 if(FAILED(hres))
1172 return hres;
1173 if(!no_ret && !push_instr(ctx, OP_pop))
1174 return E_OUTOFMEMORY;
1177 stat_ctx.break_label = alloc_label(ctx);
1178 if(!stat_ctx.break_label)
1179 return E_OUTOFMEMORY;
1181 stat_ctx.continue_label = alloc_label(ctx);
1182 if(!stat_ctx.continue_label)
1183 return E_OUTOFMEMORY;
1185 /* FIXME: avoid */
1186 if(!push_instr(ctx, OP_undefined))
1187 return E_OUTOFMEMORY;
1189 expr_off = ctx->code_off;
1191 if(stat->expr) {
1192 hres = compile_expression(ctx, stat->expr);
1193 if(FAILED(hres))
1194 return hres;
1196 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1197 if(FAILED(hres))
1198 return hres;
1201 if(!push_instr(ctx, OP_pop))
1202 return E_OUTOFMEMORY;
1204 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1205 if(FAILED(hres))
1206 return hres;
1208 label_set_addr(ctx, stat_ctx.continue_label);
1210 if(stat->end_expr) {
1211 BOOL no_ret = FALSE;
1213 hres = compile_expression_noret(ctx, stat->end_expr, &no_ret);
1214 if(FAILED(hres))
1215 return hres;
1217 if(!no_ret && !push_instr(ctx, OP_pop))
1218 return E_OUTOFMEMORY;
1221 hres = push_instr_uint(ctx, OP_jmp, expr_off);
1222 if(FAILED(hres))
1223 return hres;
1225 label_set_addr(ctx, stat_ctx.break_label);
1226 return S_OK;
1229 /* ECMA-262 3rd Edition 12.6.4 */
1230 static HRESULT compile_forin_statement(compiler_ctx_t *ctx, forin_statement_t *stat)
1232 statement_ctx_t stat_ctx = {4, FALSE, FALSE};
1233 HRESULT hres;
1235 if(stat->variable) {
1236 hres = compile_variable_list(ctx, stat->variable);
1237 if(FAILED(hres))
1238 return hres;
1241 stat_ctx.break_label = alloc_label(ctx);
1242 if(!stat_ctx.break_label)
1243 return E_OUTOFMEMORY;
1245 stat_ctx.continue_label = alloc_label(ctx);
1246 if(!stat_ctx.continue_label)
1247 return E_OUTOFMEMORY;
1249 hres = compile_expression(ctx, stat->in_expr);
1250 if(FAILED(hres))
1251 return hres;
1253 if(stat->variable) {
1254 hres = push_instr_bstr_uint(ctx, OP_identid, stat->variable->identifier, fdexNameEnsure);
1255 if(FAILED(hres))
1256 return hres;
1257 }else if(is_memberid_expr(stat->expr->type)) {
1258 hres = compile_memberid_expression(ctx, stat->expr, fdexNameEnsure);
1259 if(FAILED(hres))
1260 return hres;
1261 }else {
1262 hres = push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
1263 if(FAILED(hres))
1264 return hres;
1266 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1267 return S_OK;
1270 hres = push_instr_int(ctx, OP_int, DISPID_STARTENUM);
1271 if(FAILED(hres))
1272 return hres;
1274 /* FIXME: avoid */
1275 if(!push_instr(ctx, OP_undefined))
1276 return E_OUTOFMEMORY;
1278 label_set_addr(ctx, stat_ctx.continue_label);
1279 hres = push_instr_uint(ctx, OP_forin, stat_ctx.break_label);
1280 if(FAILED(hres))
1281 return E_OUTOFMEMORY;
1283 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1284 if(FAILED(hres))
1285 return hres;
1287 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.continue_label);
1288 if(FAILED(hres))
1289 return hres;
1291 label_set_addr(ctx, stat_ctx.break_label);
1292 return S_OK;
1295 static HRESULT pop_to_stat(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx)
1297 unsigned stack_pop = 0;
1298 statement_ctx_t *iter;
1300 for(iter = ctx->stat_ctx; iter != stat_ctx; iter = iter->next) {
1301 if(iter->using_scope && !push_instr(ctx, OP_pop_scope))
1302 return E_OUTOFMEMORY;
1303 if(iter->using_except && !push_instr(ctx, OP_pop_except))
1304 return E_OUTOFMEMORY;
1305 stack_pop += iter->stack_use;
1308 /* FIXME: optimize */
1309 while(stack_pop--) {
1310 if(!push_instr(ctx, OP_pop))
1311 return E_OUTOFMEMORY;
1314 return S_OK;
1317 /* ECMA-262 3rd Edition 12.7 */
1318 static HRESULT compile_continue_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1320 statement_ctx_t *pop_ctx;
1321 HRESULT hres;
1323 if(stat->identifier) {
1324 statement_t *label_stat;
1325 statement_ctx_t *iter;
1327 pop_ctx = NULL;
1329 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1330 if(iter->continue_label)
1331 pop_ctx = iter;
1332 if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier))
1333 break;
1336 if(!iter) {
1337 WARN("Label not found\n");
1338 return JS_E_LABEL_NOT_FOUND;
1341 /* Labelled continue are allowed only on loops */
1342 for(label_stat = iter->labelled_stat->statement;
1343 label_stat->type == STAT_LABEL;
1344 label_stat = ((labelled_statement_t*)label_stat)->statement);
1345 if(!is_loop_statement(label_stat->type)) {
1346 WARN("Label is not a loop\n");
1347 return JS_E_INVALID_CONTINUE;
1349 }else {
1350 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1351 if(pop_ctx->continue_label)
1352 break;
1355 if(!pop_ctx) {
1356 WARN("continue outside loop\n");
1357 return JS_E_INVALID_CONTINUE;
1361 hres = pop_to_stat(ctx, pop_ctx);
1362 if(FAILED(hres))
1363 return hres;
1365 if(!push_instr(ctx, OP_undefined))
1366 return E_OUTOFMEMORY;
1368 return push_instr_uint(ctx, OP_jmp, pop_ctx->continue_label);
1371 /* ECMA-262 3rd Edition 12.8 */
1372 static HRESULT compile_break_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1374 statement_ctx_t *pop_ctx;
1375 HRESULT hres;
1377 if(stat->identifier) {
1378 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1379 if(pop_ctx->labelled_stat && !strcmpW(pop_ctx->labelled_stat->identifier, stat->identifier)) {
1380 assert(pop_ctx->break_label);
1381 break;
1385 if(!pop_ctx) {
1386 WARN("Label not found\n");
1387 return JS_E_LABEL_NOT_FOUND;
1389 }else {
1390 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1391 if(pop_ctx->break_label && !pop_ctx->labelled_stat)
1392 break;
1395 if(!pop_ctx) {
1396 WARN("Break outside loop\n");
1397 return JS_E_INVALID_BREAK;
1401 hres = pop_to_stat(ctx, pop_ctx->next);
1402 if(FAILED(hres))
1403 return hres;
1405 if(!push_instr(ctx, OP_undefined))
1406 return E_OUTOFMEMORY;
1408 return push_instr_uint(ctx, OP_jmp, pop_ctx->break_label);
1411 /* ECMA-262 3rd Edition 12.9 */
1412 static HRESULT compile_return_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1414 HRESULT hres;
1416 hres = pop_to_stat(ctx, NULL);
1417 if(FAILED(hres))
1418 return hres;
1420 if(stat->expr) {
1421 hres = compile_expression(ctx, stat->expr);
1422 if(FAILED(hres))
1423 return hres;
1426 return push_instr(ctx, OP_ret) ? S_OK : E_OUTOFMEMORY;
1429 /* ECMA-262 3rd Edition 12.10 */
1430 static HRESULT compile_with_statement(compiler_ctx_t *ctx, with_statement_t *stat)
1432 statement_ctx_t stat_ctx = {0, TRUE, FALSE};
1433 HRESULT hres;
1435 hres = compile_expression(ctx, stat->expr);
1436 if(FAILED(hres))
1437 return hres;
1439 if(!push_instr(ctx, OP_push_scope))
1440 return E_OUTOFMEMORY;
1442 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1443 if(FAILED(hres))
1444 return hres;
1446 if(!push_instr(ctx, OP_pop_scope))
1447 return E_OUTOFMEMORY;
1449 return S_OK;
1452 /* ECMA-262 3rd Edition 12.10 */
1453 static HRESULT compile_labelled_statement(compiler_ctx_t *ctx, labelled_statement_t *stat)
1455 statement_ctx_t stat_ctx = {0, FALSE, FALSE, 0, 0, stat}, *iter;
1456 HRESULT hres;
1458 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1459 if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier)) {
1460 WARN("Label %s redefined\n", debugstr_w(stat->identifier));
1461 return JS_E_LABEL_REDEFINED;
1465 /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1466 stat_ctx.break_label = alloc_label(ctx);
1467 if(!stat_ctx.break_label)
1468 return E_OUTOFMEMORY;
1470 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1471 if(FAILED(hres))
1472 return hres;
1474 label_set_addr(ctx, stat_ctx.break_label);
1475 return S_OK;
1478 /* ECMA-262 3rd Edition 12.13 */
1479 static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat)
1481 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1482 unsigned case_cnt = 0, *case_jmps, i, default_jmp;
1483 BOOL have_default = FALSE;
1484 statement_t *stat_iter;
1485 case_clausule_t *iter;
1486 HRESULT hres;
1488 hres = compile_expression(ctx, stat->expr);
1489 if(FAILED(hres))
1490 return hres;
1492 stat_ctx.break_label = alloc_label(ctx);
1493 if(!stat_ctx.break_label)
1494 return E_OUTOFMEMORY;
1496 for(iter = stat->case_list; iter; iter = iter->next) {
1497 if(iter->expr)
1498 case_cnt++;
1501 case_jmps = heap_alloc(case_cnt * sizeof(*case_jmps));
1502 if(!case_jmps)
1503 return E_OUTOFMEMORY;
1505 i = 0;
1506 for(iter = stat->case_list; iter; iter = iter->next) {
1507 if(!iter->expr) {
1508 have_default = TRUE;
1509 continue;
1512 hres = compile_expression(ctx, iter->expr);
1513 if(FAILED(hres))
1514 break;
1516 case_jmps[i] = push_instr(ctx, OP_case);
1517 if(!case_jmps[i]) {
1518 hres = E_OUTOFMEMORY;
1519 break;
1521 i++;
1524 if(SUCCEEDED(hres)) {
1525 if(push_instr(ctx, OP_pop)) {
1526 default_jmp = push_instr(ctx, OP_jmp);
1527 if(!default_jmp)
1528 hres = E_OUTOFMEMORY;
1529 }else {
1530 hres = E_OUTOFMEMORY;
1534 if(FAILED(hres)) {
1535 heap_free(case_jmps);
1536 return hres;
1539 i = 0;
1540 for(iter = stat->case_list; iter; iter = iter->next) {
1541 while(iter->next && iter->next->stat == iter->stat) {
1542 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1543 iter = iter->next;
1546 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1548 for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter); stat_iter = stat_iter->next) {
1549 hres = compile_statement(ctx, &stat_ctx, stat_iter);
1550 if(FAILED(hres))
1551 break;
1553 if(stat_iter->next && !push_instr(ctx, OP_pop)) {
1554 hres = E_OUTOFMEMORY;
1555 break;
1558 if(FAILED(hres))
1559 break;
1562 heap_free(case_jmps);
1563 if(FAILED(hres))
1564 return hres;
1565 assert(i == case_cnt);
1567 if(!have_default) {
1568 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.break_label);
1569 if(FAILED(hres))
1570 return hres;
1571 set_arg_uint(ctx, default_jmp, ctx->code_off);
1572 if(!push_instr(ctx, OP_undefined))
1573 return E_OUTOFMEMORY;
1576 label_set_addr(ctx, stat_ctx.break_label);
1577 return S_OK;
1580 /* ECMA-262 3rd Edition 12.13 */
1581 static HRESULT compile_throw_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1583 HRESULT hres;
1585 hres = compile_expression(ctx, stat->expr);
1586 if(FAILED(hres))
1587 return hres;
1589 return push_instr(ctx, OP_throw) ? S_OK : E_OUTOFMEMORY;
1592 /* ECMA-262 3rd Edition 12.14 */
1593 static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat)
1595 statement_ctx_t try_ctx = {0, FALSE, TRUE}, catch_ctx = {0, TRUE, FALSE};
1596 statement_ctx_t finally_ctx = {2, FALSE, FALSE};
1597 unsigned push_except;
1598 BSTR ident;
1599 HRESULT hres;
1601 push_except = push_instr(ctx, OP_push_except);
1602 if(!push_except)
1603 return E_OUTOFMEMORY;
1605 if(stat->catch_block) {
1606 ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier);
1607 if(!ident)
1608 return E_OUTOFMEMORY;
1609 }else {
1610 ident = NULL;
1613 instr_ptr(ctx, push_except)->u.arg[1].bstr = ident;
1615 if(!stat->catch_block)
1616 try_ctx.stack_use = 2;
1618 hres = compile_statement(ctx, &try_ctx, stat->try_statement);
1619 if(FAILED(hres))
1620 return hres;
1622 if(!push_instr(ctx, OP_pop_except))
1623 return E_OUTOFMEMORY;
1625 if(stat->catch_block) {
1626 unsigned jmp_finally;
1628 jmp_finally = push_instr(ctx, OP_jmp);
1629 if(!jmp_finally)
1630 return E_OUTOFMEMORY;
1632 instr_ptr(ctx, push_except)->u.arg[0].uint = ctx->code_off;
1634 hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement);
1635 if(FAILED(hres))
1636 return hres;
1638 if(!push_instr(ctx, OP_pop_scope))
1639 return E_OUTOFMEMORY;
1641 set_arg_uint(ctx, jmp_finally, ctx->code_off);
1642 }else {
1643 set_arg_uint(ctx, push_except, ctx->code_off);
1646 if(stat->finally_statement) {
1647 /* FIXME: avoid */
1648 if(!push_instr(ctx, OP_pop))
1649 return E_OUTOFMEMORY;
1651 hres = compile_statement(ctx, stat->catch_block ? NULL : &finally_ctx, stat->finally_statement);
1652 if(FAILED(hres))
1653 return hres;
1655 if(!stat->catch_block && !push_instr(ctx, OP_end_finally))
1656 return E_OUTOFMEMORY;
1659 return S_OK;
1662 static HRESULT compile_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1664 HRESULT hres;
1666 if(stat_ctx) {
1667 stat_ctx->next = ctx->stat_ctx;
1668 ctx->stat_ctx = stat_ctx;
1671 switch(stat->type) {
1672 case STAT_BLOCK:
1673 hres = compile_block_statement(ctx, ((block_statement_t*)stat)->stat_list);
1674 break;
1675 case STAT_BREAK:
1676 hres = compile_break_statement(ctx, (branch_statement_t*)stat);
1677 break;
1678 case STAT_CONTINUE:
1679 hres = compile_continue_statement(ctx, (branch_statement_t*)stat);
1680 break;
1681 case STAT_EMPTY:
1682 hres = push_instr(ctx, OP_undefined) ? S_OK : E_OUTOFMEMORY; /* FIXME */
1683 break;
1684 case STAT_EXPR:
1685 hres = compile_expression_statement(ctx, (expression_statement_t*)stat);
1686 break;
1687 case STAT_FOR:
1688 hres = compile_for_statement(ctx, (for_statement_t*)stat);
1689 break;
1690 case STAT_FORIN:
1691 hres = compile_forin_statement(ctx, (forin_statement_t*)stat);
1692 break;
1693 case STAT_IF:
1694 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1695 break;
1696 case STAT_LABEL:
1697 hres = compile_labelled_statement(ctx, (labelled_statement_t*)stat);
1698 break;
1699 case STAT_RETURN:
1700 hres = compile_return_statement(ctx, (expression_statement_t*)stat);
1701 break;
1702 case STAT_SWITCH:
1703 hres = compile_switch_statement(ctx, (switch_statement_t*)stat);
1704 break;
1705 case STAT_THROW:
1706 hres = compile_throw_statement(ctx, (expression_statement_t*)stat);
1707 break;
1708 case STAT_TRY:
1709 hres = compile_try_statement(ctx, (try_statement_t*)stat);
1710 break;
1711 case STAT_VAR:
1712 hres = compile_var_statement(ctx, (var_statement_t*)stat);
1713 break;
1714 case STAT_WHILE:
1715 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1716 break;
1717 case STAT_WITH:
1718 hres = compile_with_statement(ctx, (with_statement_t*)stat);
1719 break;
1720 default:
1721 assert(0);
1722 hres = E_FAIL;
1725 if(stat_ctx) {
1726 assert(ctx->stat_ctx == stat_ctx);
1727 ctx->stat_ctx = stat_ctx->next;
1730 return hres;
1733 static void resolve_labels(compiler_ctx_t *ctx, unsigned off)
1735 instr_t *instr;
1737 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
1738 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->u.arg->uint & LABEL_FLAG)) {
1739 assert((instr->u.arg->uint & ~LABEL_FLAG) < ctx->labels_cnt);
1740 instr->u.arg->uint = ctx->labels[instr->u.arg->uint & ~LABEL_FLAG];
1742 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
1745 ctx->labels_cnt = 0;
1748 void release_bytecode(bytecode_t *code)
1750 unsigned i;
1752 if(--code->ref)
1753 return;
1755 for(i=0; i < code->bstr_cnt; i++)
1756 SysFreeString(code->bstr_pool[i]);
1758 heap_free(code->source);
1759 jsheap_free(&code->heap);
1760 heap_free(code->bstr_pool);
1761 heap_free(code->instrs);
1762 heap_free(code);
1765 static HRESULT init_code(compiler_ctx_t *compiler, const WCHAR *source)
1767 compiler->code = heap_alloc_zero(sizeof(bytecode_t));
1768 if(!compiler->code)
1769 return E_OUTOFMEMORY;
1771 compiler->code->ref = 1;
1772 jsheap_init(&compiler->code->heap);
1774 compiler->code->source = heap_strdupW(source);
1775 if(!compiler->code->source) {
1776 release_bytecode(compiler->code);
1777 return E_OUTOFMEMORY;
1780 compiler->code->instrs = heap_alloc(64 * sizeof(instr_t));
1781 if(!compiler->code->instrs) {
1782 release_bytecode(compiler->code);
1783 return E_OUTOFMEMORY;
1786 compiler->code_size = 64;
1787 compiler->code_off = 1;
1788 return S_OK;
1791 static HRESULT compile_function(compiler_ctx_t *ctx, source_elements_t *source, function_expression_t *func_expr,
1792 BOOL from_eval, function_code_t *func)
1794 variable_declaration_t *var_iter;
1795 function_expression_t *iter;
1796 unsigned off, i;
1797 HRESULT hres;
1799 TRACE("\n");
1801 ctx->var_head = ctx->var_tail = NULL;
1802 ctx->func_head = ctx->func_tail = NULL;
1804 off = ctx->code_off;
1805 ctx->func = func;
1806 hres = compile_block_statement(ctx, source->statement);
1807 if(FAILED(hres))
1808 return hres;
1810 resolve_labels(ctx, off);
1812 if(!from_eval && !push_instr(ctx, OP_pop))
1813 return E_OUTOFMEMORY;
1814 if(!push_instr(ctx, OP_ret))
1815 return E_OUTOFMEMORY;
1817 if(TRACE_ON(jscript_disas))
1818 dump_code(ctx, off);
1820 func->instr_off = off;
1822 if(func_expr && func_expr->identifier) {
1823 func->name = compiler_alloc_bstr(ctx, func_expr->identifier);
1824 if(!func->name)
1825 return E_OUTOFMEMORY;
1828 if(func_expr) {
1829 parameter_t *param_iter;
1831 func->source = func_expr->src_str;
1832 func->source_len = func_expr->src_len;
1834 for(param_iter = func_expr->parameter_list; param_iter; param_iter = param_iter->next)
1835 func->param_cnt++;
1837 func->params = compiler_alloc(ctx->code, func->param_cnt * sizeof(*func->params));
1838 if(!func->params)
1839 return E_OUTOFMEMORY;
1841 for(param_iter = func_expr->parameter_list, i=0; param_iter; param_iter = param_iter->next, i++) {
1842 func->params[i] = compiler_alloc_bstr(ctx, param_iter->identifier);
1843 if(!func->params[i])
1844 return E_OUTOFMEMORY;
1848 func->variables = compiler_alloc(ctx->code, func->var_cnt * sizeof(*func->variables));
1849 if(!func->variables)
1850 return E_OUTOFMEMORY;
1852 for(var_iter = ctx->var_head, i=0; var_iter; var_iter = var_iter->global_next, i++) {
1853 func->variables[i] = compiler_alloc_bstr(ctx, var_iter->identifier);
1854 if(!func->variables[i])
1855 return E_OUTOFMEMORY;
1858 assert(i == func->var_cnt);
1860 func->funcs = compiler_alloc(ctx->code, func->func_cnt * sizeof(*func->funcs));
1861 if(!func->funcs)
1862 return E_OUTOFMEMORY;
1863 memset(func->funcs, 0, func->func_cnt * sizeof(*func->funcs));
1865 for(iter = ctx->func_head, i=0; iter; iter = iter->next, i++) {
1866 hres = compile_function(ctx, iter->source_elements, iter, FALSE, func->funcs+i);
1867 if(FAILED(hres))
1868 return hres;
1871 assert(i == func->func_cnt);
1873 return S_OK;
1876 HRESULT compile_script(script_ctx_t *ctx, const WCHAR *code, const WCHAR *delimiter, BOOL from_eval, BOOL use_decode,
1877 bytecode_t **ret)
1879 compiler_ctx_t compiler = {0};
1880 HRESULT hres;
1882 hres = init_code(&compiler, code);
1883 if(FAILED(hres))
1884 return hres;
1886 if(use_decode) {
1887 hres = decode_source(compiler.code->source);
1888 if(FAILED(hres)) {
1889 WARN("Decoding failed\n");
1890 return hres;
1894 hres = script_parse(ctx, compiler.code->source, delimiter, from_eval, &compiler.parser);
1895 if(FAILED(hres)) {
1896 release_bytecode(compiler.code);
1897 return hres;
1900 hres = compile_function(&compiler, compiler.parser->source, NULL, from_eval, &compiler.code->global_code);
1901 parser_release(compiler.parser);
1902 if(FAILED(hres)) {
1903 release_bytecode(compiler.code);
1904 return hres;
1907 *ret = compiler.code;
1908 return S_OK;