jscript: Always use bytecode for with statement.
[wine/multimedia.git] / dlls / jscript / compile.c
blob0f1e3af0a52f1f6ff2222ed73dda06481ad7070e
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);
29 typedef struct _statement_ctx_t {
30 unsigned stack_use;
31 BOOL using_scope;
32 BOOL using_except;
34 unsigned break_label;
35 unsigned continue_label;
37 struct _statement_ctx_t *next;
38 } statement_ctx_t;
40 struct _compiler_ctx_t {
41 parser_ctx_t *parser;
42 bytecode_t *code;
44 unsigned code_off;
45 unsigned code_size;
47 unsigned *labels;
48 unsigned labels_size;
49 unsigned labels_cnt;
51 statement_ctx_t *stat_ctx;
54 static const struct {
55 const char *op_str;
56 instr_arg_type_t arg1_type;
57 instr_arg_type_t arg2_type;
58 } instr_info[] = {
59 #define X(n,a,b,c) {#n,b,c},
60 OP_LIST
61 #undef X
64 static HRESULT compile_expression(compiler_ctx_t*,expression_t*);
65 static HRESULT compile_statement(compiler_ctx_t*,statement_ctx_t*,statement_t*);
67 static inline void *compiler_alloc(bytecode_t *code, size_t size)
69 return jsheap_alloc(&code->heap, size);
72 static WCHAR *compiler_alloc_string(bytecode_t *code, const WCHAR *str)
74 size_t size;
75 WCHAR *ret;
77 size = (strlenW(str)+1)*sizeof(WCHAR);
78 ret = compiler_alloc(code, size);
79 if(ret)
80 memcpy(ret, str, size);
81 return ret;
84 static BSTR compiler_alloc_bstr(compiler_ctx_t *ctx, const WCHAR *str)
86 if(!ctx->code->bstr_pool_size) {
87 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
88 if(!ctx->code->bstr_pool)
89 return NULL;
90 ctx->code->bstr_pool_size = 8;
91 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
92 BSTR *new_pool;
94 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
95 if(!new_pool)
96 return NULL;
98 ctx->code->bstr_pool = new_pool;
99 ctx->code->bstr_pool_size *= 2;
102 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
103 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
104 return NULL;
106 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
109 static unsigned push_instr(compiler_ctx_t *ctx, jsop_t op)
111 assert(ctx->code_size >= ctx->code_off);
113 if(!ctx->code_size) {
114 ctx->code->instrs = heap_alloc(64 * sizeof(instr_t));
115 if(!ctx->code->instrs)
116 return -1;
117 ctx->code_size = 64;
118 }else if(ctx->code_size == ctx->code_off) {
119 instr_t *new_instrs;
121 new_instrs = heap_realloc(ctx->code->instrs, ctx->code_size*2*sizeof(instr_t));
122 if(!new_instrs)
123 return -1;
125 ctx->code->instrs = new_instrs;
126 ctx->code_size *= 2;
129 ctx->code->instrs[ctx->code_off].op = op;
130 return ctx->code_off++;
133 static inline instr_t *instr_ptr(compiler_ctx_t *ctx, unsigned off)
135 assert(off < ctx->code_off);
136 return ctx->code->instrs + off;
139 static HRESULT push_instr_int(compiler_ctx_t *ctx, jsop_t op, LONG arg)
141 unsigned instr;
143 instr = push_instr(ctx, op);
144 if(instr == -1)
145 return E_OUTOFMEMORY;
147 instr_ptr(ctx, instr)->arg1.lng = arg;
148 return S_OK;
151 static HRESULT push_instr_str(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
153 unsigned instr;
154 WCHAR *str;
156 str = compiler_alloc_string(ctx->code, arg);
157 if(!str)
158 return E_OUTOFMEMORY;
160 instr = push_instr(ctx, op);
161 if(instr == -1)
162 return E_OUTOFMEMORY;
164 instr_ptr(ctx, instr)->arg1.str = str;
165 return S_OK;
168 static HRESULT push_instr_bstr(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
170 unsigned instr;
171 WCHAR *str;
173 str = compiler_alloc_bstr(ctx, arg);
174 if(!str)
175 return E_OUTOFMEMORY;
177 instr = push_instr(ctx, op);
178 if(instr == -1)
179 return E_OUTOFMEMORY;
181 instr_ptr(ctx, instr)->arg1.bstr = str;
182 return S_OK;
185 static HRESULT push_instr_bstr_uint(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg1, unsigned arg2)
187 unsigned instr;
188 WCHAR *str;
190 str = compiler_alloc_bstr(ctx, arg1);
191 if(!str)
192 return E_OUTOFMEMORY;
194 instr = push_instr(ctx, op);
195 if(instr == -1)
196 return E_OUTOFMEMORY;
198 instr_ptr(ctx, instr)->arg1.bstr = str;
199 instr_ptr(ctx, instr)->arg2.uint = arg2;
200 return S_OK;
203 static HRESULT push_instr_uint_str(compiler_ctx_t *ctx, jsop_t op, unsigned arg1, const WCHAR *arg2)
205 unsigned instr;
206 WCHAR *str;
208 str = compiler_alloc_string(ctx->code, arg2);
209 if(!str)
210 return E_OUTOFMEMORY;
212 instr = push_instr(ctx, op);
213 if(instr == -1)
214 return E_OUTOFMEMORY;
216 instr_ptr(ctx, instr)->arg1.uint = arg1;
217 instr_ptr(ctx, instr)->arg2.str = str;
218 return S_OK;
221 static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
223 unsigned instr;
224 DOUBLE *dbl;
226 dbl = compiler_alloc(ctx->code, sizeof(arg));
227 if(!dbl)
228 return E_OUTOFMEMORY;
229 *dbl = arg;
231 instr = push_instr(ctx, op);
232 if(instr == -1)
233 return E_OUTOFMEMORY;
235 instr_ptr(ctx, instr)->arg1.dbl = dbl;
236 return S_OK;
239 static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
241 unsigned instr;
243 instr = push_instr(ctx, op);
244 if(instr == -1)
245 return E_OUTOFMEMORY;
247 instr_ptr(ctx, instr)->arg1.uint = arg;
248 return S_OK;
251 static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
253 HRESULT hres;
255 hres = compile_expression(ctx, expr->expression1);
256 if(FAILED(hres))
257 return hres;
259 hres = compile_expression(ctx, expr->expression2);
260 if(FAILED(hres))
261 return hres;
263 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
266 static HRESULT compile_unary_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op)
268 HRESULT hres;
270 hres = compile_expression(ctx, expr->expression);
271 if(FAILED(hres))
272 return hres;
274 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
277 /* ECMA-262 3rd Edition 11.2.1 */
278 static HRESULT compile_member_expression(compiler_ctx_t *ctx, member_expression_t *expr)
280 HRESULT hres;
282 hres = compile_expression(ctx, expr->expression);
283 if(FAILED(hres))
284 return hres;
286 return push_instr_bstr(ctx, OP_member, expr->identifier);
289 #define LABEL_FLAG 0x80000000
291 static unsigned alloc_label(compiler_ctx_t *ctx)
293 if(!ctx->labels_size) {
294 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
295 if(!ctx->labels)
296 return -1;
297 ctx->labels_size = 8;
298 }else if(ctx->labels_size == ctx->labels_cnt) {
299 unsigned *new_labels;
301 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
302 if(!new_labels)
303 return -1;
305 ctx->labels = new_labels;
306 ctx->labels_size *= 2;
309 return ctx->labels_cnt++ | LABEL_FLAG;
312 static void label_set_addr(compiler_ctx_t *ctx, unsigned label)
314 assert(label & LABEL_FLAG);
315 ctx->labels[label & ~LABEL_FLAG] = ctx->code_off;
318 static inline BOOL is_memberid_expr(expression_type_t type)
320 return type == EXPR_IDENT || type == EXPR_MEMBER || type == EXPR_ARRAY;
323 static HRESULT compile_memberid_expression(compiler_ctx_t *ctx, expression_t *expr, unsigned flags)
325 HRESULT hres = S_OK;
327 switch(expr->type) {
328 case EXPR_IDENT: {
329 identifier_expression_t *ident_expr = (identifier_expression_t*)expr;
331 hres = push_instr_bstr_uint(ctx, OP_identid, ident_expr->identifier, flags);
332 break;
334 case EXPR_ARRAY: {
335 binary_expression_t *array_expr = (binary_expression_t*)expr;
337 hres = compile_expression(ctx, array_expr->expression1);
338 if(FAILED(hres))
339 return hres;
341 hres = compile_expression(ctx, array_expr->expression2);
342 if(FAILED(hres))
343 return hres;
345 hres = push_instr_uint(ctx, OP_memberid, flags);
346 break;
348 case EXPR_MEMBER: {
349 member_expression_t *member_expr = (member_expression_t*)expr;
351 hres = compile_expression(ctx, member_expr->expression);
352 if(FAILED(hres))
353 return hres;
355 /* FIXME: Potential optimization */
356 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
357 if(FAILED(hres))
358 return hres;
360 hres = push_instr_uint(ctx, OP_memberid, flags);
361 break;
363 default:
364 assert(0);
367 return hres;
370 static HRESULT compile_increment_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op, int n)
372 HRESULT hres;
374 if(!is_memberid_expr(expr->expression->type)) {
375 hres = compile_expression(ctx, expr->expression);
376 if(FAILED(hres))
377 return hres;
379 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
382 hres = compile_memberid_expression(ctx, expr->expression, fdexNameEnsure);
383 if(FAILED(hres))
384 return hres;
386 return push_instr_int(ctx, op, n);
389 /* ECMA-262 3rd Edition 11.14 */
390 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr)
392 HRESULT hres;
394 hres = compile_expression(ctx, expr->expression1);
395 if(FAILED(hres))
396 return hres;
398 if(push_instr(ctx, OP_pop) == -1)
399 return E_OUTOFMEMORY;
401 return compile_expression(ctx, expr->expression2);
404 /* ECMA-262 3rd Edition 11.11 */
405 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
407 unsigned instr;
408 HRESULT hres;
410 hres = compile_expression(ctx, expr->expression1);
411 if(FAILED(hres))
412 return hres;
414 instr = push_instr(ctx, op);
415 if(instr == -1)
416 return E_OUTOFMEMORY;
418 hres = compile_expression(ctx, expr->expression2);
419 if(FAILED(hres))
420 return hres;
422 instr_ptr(ctx, instr)->arg1.uint = ctx->code_off;
423 return S_OK;
426 /* ECMA-262 3rd Edition 11.12 */
427 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
429 unsigned jmp_false, jmp_end;
430 HRESULT hres;
432 hres = compile_expression(ctx, expr->expression);
433 if(FAILED(hres))
434 return hres;
436 jmp_false = push_instr(ctx, OP_cnd_z);
437 if(jmp_false == -1)
438 return E_OUTOFMEMORY;
440 hres = compile_expression(ctx, expr->true_expression);
441 if(FAILED(hres))
442 return hres;
444 jmp_end = push_instr(ctx, OP_jmp);
445 if(jmp_end == -1)
446 return E_OUTOFMEMORY;
448 instr_ptr(ctx, jmp_false)->arg1.uint = ctx->code_off;
449 if(push_instr(ctx, OP_pop) == -1)
450 return E_OUTOFMEMORY;
452 hres = compile_expression(ctx, expr->false_expression);
453 if(FAILED(hres))
454 return hres;
456 instr_ptr(ctx, jmp_end)->arg1.uint = ctx->code_off;
457 return S_OK;
460 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
462 unsigned arg_cnt = 0;
463 argument_t *arg;
464 HRESULT hres;
466 hres = compile_expression(ctx, expr->expression);
467 if(FAILED(hres))
468 return hres;
470 for(arg = expr->argument_list; arg; arg = arg->next) {
471 hres = compile_expression(ctx, arg->expr);
472 if(FAILED(hres))
473 return hres;
474 arg_cnt++;
477 return push_instr_int(ctx, OP_new, arg_cnt);
480 static HRESULT compile_interp_fallback(compiler_ctx_t *ctx, statement_t *stat)
482 unsigned instr;
484 instr = push_instr(ctx, OP_tree);
485 if(instr == -1)
486 return E_OUTOFMEMORY;
488 instr_ptr(ctx, instr)->arg1.stat = stat;
489 return S_OK;
492 static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL *no_ret)
494 unsigned arg_cnt = 0;
495 argument_t *arg;
496 unsigned instr;
497 jsop_t op;
498 HRESULT hres;
500 if(is_memberid_expr(expr->expression->type)) {
501 op = OP_call_member;
502 hres = compile_memberid_expression(ctx, expr->expression, 0);
503 }else {
504 op = OP_call;
505 hres = compile_expression(ctx, expr->expression);
508 if(FAILED(hres))
509 return hres;
511 for(arg = expr->argument_list; arg; arg = arg->next) {
512 hres = compile_expression(ctx, arg->expr);
513 if(FAILED(hres))
514 return hres;
515 arg_cnt++;
518 instr = push_instr(ctx, op);
519 if(instr == -1)
520 return E_OUTOFMEMORY;
522 instr_ptr(ctx, instr)->arg1.uint = arg_cnt;
523 instr_ptr(ctx, instr)->arg2.lng = no_ret == NULL;
524 if(no_ret)
525 *no_ret = TRUE;
526 return S_OK;
529 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
531 HRESULT hres;
533 switch(expr->expression->type) {
534 case EXPR_ARRAY: {
535 binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
537 hres = compile_expression(ctx, array_expr->expression1);
538 if(FAILED(hres))
539 return hres;
541 hres = compile_expression(ctx, array_expr->expression2);
542 if(FAILED(hres))
543 return hres;
545 if(push_instr(ctx, OP_delete) == -1)
546 return E_OUTOFMEMORY;
547 break;
549 case EXPR_MEMBER: {
550 member_expression_t *member_expr = (member_expression_t*)expr->expression;
552 hres = compile_expression(ctx, member_expr->expression);
553 if(FAILED(hres))
554 return hres;
556 /* FIXME: Potential optimization */
557 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
558 if(FAILED(hres))
559 return hres;
561 if(push_instr(ctx, OP_delete) == -1)
562 return E_OUTOFMEMORY;
563 break;
565 case EXPR_IDENT:
566 return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
567 default: {
568 const WCHAR fixmeW[] = {'F','I','X','M','E',0};
570 WARN("invalid delete, unimplemented exception message\n");
572 hres = compile_expression(ctx, expr->expression);
573 if(FAILED(hres))
574 return hres;
576 return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, fixmeW);
580 return S_OK;
583 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
585 HRESULT hres;
587 if(!is_memberid_expr(expr->expression1->type)) {
588 hres = compile_expression(ctx, expr->expression1);
589 if(FAILED(hres))
590 return hres;
592 hres = compile_expression(ctx, expr->expression2);
593 if(FAILED(hres))
594 return hres;
596 if(op != OP_LAST && push_instr(ctx, op) == -1)
597 return E_OUTOFMEMORY;
599 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
602 hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
603 if(FAILED(hres))
604 return hres;
606 if(op != OP_LAST && push_instr(ctx, OP_refval) == -1)
607 return E_OUTOFMEMORY;
609 hres = compile_expression(ctx, expr->expression2);
610 if(FAILED(hres))
611 return hres;
613 if(op != OP_LAST && push_instr(ctx, op) == -1)
614 return E_OUTOFMEMORY;
616 if(push_instr(ctx, OP_assign) == -1)
617 return E_OUTOFMEMORY;
619 return S_OK;
622 static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
624 jsop_t op;
625 HRESULT hres;
627 if(is_memberid_expr(expr->expression->type)) {
628 if(expr->expression->type == EXPR_IDENT)
629 return push_instr_str(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
631 op = OP_typeofid;
632 hres = compile_memberid_expression(ctx, expr->expression, 0);
633 }else {
634 op = OP_typeof;
635 hres = compile_expression(ctx, expr->expression);
637 if(FAILED(hres))
638 return hres;
640 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
643 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
645 switch(literal->type) {
646 case LT_BOOL:
647 return push_instr_int(ctx, OP_bool, literal->u.bval);
648 case LT_DOUBLE:
649 return push_instr_double(ctx, OP_double, literal->u.dval);
650 case LT_INT:
651 return push_instr_int(ctx, OP_int, literal->u.lval);
652 case LT_NULL:
653 return push_instr(ctx, OP_null);
654 case LT_STRING:
655 return push_instr_str(ctx, OP_str, literal->u.wstr);
656 case LT_REGEXP: {
657 unsigned instr;
658 WCHAR *str;
660 str = compiler_alloc(ctx->code, (literal->u.regexp.str_len+1)*sizeof(WCHAR));
661 if(!str)
662 return E_OUTOFMEMORY;
663 memcpy(str, literal->u.regexp.str, literal->u.regexp.str_len*sizeof(WCHAR));
664 str[literal->u.regexp.str_len] = 0;
666 instr = push_instr(ctx, OP_regexp);
667 if(instr == -1)
668 return E_OUTOFMEMORY;
670 instr_ptr(ctx, instr)->arg1.str = str;
671 instr_ptr(ctx, instr)->arg2.lng = literal->u.regexp.flags;
672 return S_OK;
674 default:
675 assert(0);
679 static HRESULT literal_as_bstr(compiler_ctx_t *ctx, literal_t *literal, BSTR *str)
681 switch(literal->type) {
682 case LT_STRING:
683 *str = compiler_alloc_bstr(ctx, literal->u.wstr);
684 break;
685 case LT_INT:
686 *str = int_to_bstr(literal->u.lval);
687 break;
688 case LT_DOUBLE:
689 return double_to_bstr(literal->u.dval, str);
690 default:
691 assert(0);
694 return *str ? S_OK : E_OUTOFMEMORY;
697 static HRESULT compile_array_literal(compiler_ctx_t *ctx, array_literal_expression_t *expr)
699 unsigned i, elem_cnt = expr->length;
700 array_element_t *iter;
701 HRESULT hres;
703 for(iter = expr->element_list; iter; iter = iter->next) {
704 elem_cnt += iter->elision+1;
706 for(i=0; i < iter->elision; i++) {
707 if(push_instr(ctx, OP_undefined) == -1)
708 return E_OUTOFMEMORY;
711 hres = compile_expression(ctx, iter->expr);
712 if(FAILED(hres))
713 return hres;
716 for(i=0; i < expr->length; i++) {
717 if(push_instr(ctx, OP_undefined) == -1)
718 return E_OUTOFMEMORY;
721 return push_instr_uint(ctx, OP_carray, elem_cnt);
724 static HRESULT compile_object_literal(compiler_ctx_t *ctx, property_value_expression_t *expr)
726 prop_val_t *iter;
727 unsigned instr;
728 BSTR name;
729 HRESULT hres;
731 if(push_instr(ctx, OP_new_obj) == -1)
732 return E_OUTOFMEMORY;
734 for(iter = expr->property_list; iter; iter = iter->next) {
735 hres = literal_as_bstr(ctx, iter->name, &name);
736 if(FAILED(hres))
737 return hres;
739 hres = compile_expression(ctx, iter->value);
740 if(FAILED(hres))
741 return hres;
743 instr = push_instr(ctx, OP_obj_prop);
744 if(instr == -1)
745 return E_OUTOFMEMORY;
747 instr_ptr(ctx, instr)->arg1.bstr = name;
750 return S_OK;
753 static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr)
755 unsigned instr;
757 /* FIXME: not exactly right */
758 if(expr->identifier)
759 return push_instr_bstr(ctx, OP_ident, expr->identifier);
761 instr = push_instr(ctx, OP_func);
762 if(instr == -1)
763 return E_OUTOFMEMORY;
765 instr_ptr(ctx, instr)->arg1.func = expr;
766 return S_OK;
769 static HRESULT compile_expression_noret(compiler_ctx_t *ctx, expression_t *expr, BOOL *no_ret)
771 switch(expr->type) {
772 case EXPR_ADD:
773 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
774 case EXPR_AND:
775 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_z);
776 case EXPR_ARRAY:
777 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
778 case EXPR_ARRAYLIT:
779 return compile_array_literal(ctx, (array_literal_expression_t*)expr);
780 case EXPR_ASSIGN:
781 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
782 case EXPR_ASSIGNADD:
783 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
784 case EXPR_ASSIGNAND:
785 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_and);
786 case EXPR_ASSIGNSUB:
787 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
788 case EXPR_ASSIGNMUL:
789 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
790 case EXPR_ASSIGNDIV:
791 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
792 case EXPR_ASSIGNMOD:
793 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
794 case EXPR_ASSIGNOR:
795 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
796 case EXPR_ASSIGNLSHIFT:
797 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_lshift);
798 case EXPR_ASSIGNRSHIFT:
799 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift);
800 case EXPR_ASSIGNRRSHIFT:
801 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
802 case EXPR_ASSIGNXOR:
803 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
804 case EXPR_BAND:
805 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
806 case EXPR_BITNEG:
807 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
808 case EXPR_BOR:
809 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
810 case EXPR_CALL:
811 return compile_call_expression(ctx, (call_expression_t*)expr, no_ret);
812 case EXPR_COMMA:
813 return compile_comma_expression(ctx, (binary_expression_t*)expr);
814 case EXPR_COND:
815 return compile_conditional_expression(ctx, (conditional_expression_t*)expr);
816 case EXPR_DELETE:
817 return compile_delete_expression(ctx, (unary_expression_t*)expr);
818 case EXPR_DIV:
819 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
820 case EXPR_EQ:
821 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
822 case EXPR_EQEQ:
823 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
824 case EXPR_FUNC:
825 return compile_function_expression(ctx, (function_expression_t*)expr);
826 case EXPR_GREATER:
827 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
828 case EXPR_GREATEREQ:
829 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
830 case EXPR_IDENT:
831 return push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
832 case EXPR_IN:
833 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
834 case EXPR_INSTANCEOF:
835 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
836 case EXPR_LESS:
837 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
838 case EXPR_LESSEQ:
839 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
840 case EXPR_LITERAL:
841 return compile_literal(ctx, ((literal_expression_t*)expr)->literal);
842 case EXPR_LOGNEG:
843 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
844 case EXPR_LSHIFT:
845 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
846 case EXPR_MEMBER:
847 return compile_member_expression(ctx, (member_expression_t*)expr);
848 case EXPR_MINUS:
849 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
850 case EXPR_MOD:
851 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
852 case EXPR_MUL:
853 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
854 case EXPR_NEW:
855 return compile_new_expression(ctx, (call_expression_t*)expr);
856 case EXPR_NOTEQ:
857 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
858 case EXPR_NOTEQEQ:
859 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
860 case EXPR_OR:
861 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_nz);
862 case EXPR_PLUS:
863 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
864 case EXPR_POSTDEC:
865 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
866 case EXPR_POSTINC:
867 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
868 case EXPR_PREDEC:
869 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
870 case EXPR_PREINC:
871 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
872 case EXPR_PROPVAL:
873 return compile_object_literal(ctx, (property_value_expression_t*)expr);
874 case EXPR_RSHIFT:
875 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
876 case EXPR_RRSHIFT:
877 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
878 case EXPR_SUB:
879 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
880 case EXPR_THIS:
881 return push_instr(ctx, OP_this) == -1 ? E_OUTOFMEMORY : S_OK;
882 case EXPR_TYPEOF:
883 return compile_typeof_expression(ctx, (unary_expression_t*)expr);
884 case EXPR_VOID:
885 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
886 case EXPR_BXOR:
887 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
888 default:
889 assert(0);
892 return S_OK;
895 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr)
897 return compile_expression_noret(ctx, expr, NULL);
900 /* ECMA-262 3rd Edition 12.1 */
901 static HRESULT compile_block_statement(compiler_ctx_t *ctx, statement_t *iter)
903 HRESULT hres;
905 /* FIXME: do it only if needed */
906 if(!iter)
907 return push_instr(ctx, OP_undefined) == -1 ? E_OUTOFMEMORY : S_OK;
909 while(1) {
910 hres = compile_statement(ctx, NULL, iter);
911 if(FAILED(hres))
912 return hres;
914 iter = iter->next;
915 if(!iter)
916 break;
918 if(push_instr(ctx, OP_pop) == -1)
919 return E_OUTOFMEMORY;
922 return S_OK;
925 /* ECMA-262 3rd Edition 12.2 */
926 static HRESULT compile_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
928 variable_declaration_t *iter;
929 HRESULT hres;
931 for(iter = list; iter; iter = iter->next) {
932 if(!iter->expr)
933 continue;
935 hres = compile_expression(ctx, iter->expr);
936 if(FAILED(hres))
937 return hres;
939 hres = push_instr_bstr(ctx, OP_var_set, iter->identifier);
940 if(FAILED(hres))
941 return hres;
944 return S_OK;
947 /* ECMA-262 3rd Edition 12.2 */
948 static HRESULT compile_var_statement(compiler_ctx_t *ctx, var_statement_t *stat)
950 HRESULT hres;
952 hres = compile_variable_list(ctx, stat->variable_list);
953 if(FAILED(hres))
954 return hres;
956 return push_instr(ctx, OP_undefined) == -1 ? E_OUTOFMEMORY : S_OK;
959 /* ECMA-262 3rd Edition 12.4 */
960 static HRESULT compile_expression_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
962 BOOL no_ret = FALSE;
963 HRESULT hres;
965 hres = compile_expression_noret(ctx, stat->expr, &no_ret);
966 if(FAILED(hres))
967 return hres;
969 /* FIXME: that's a big potential optimization */
970 if(no_ret && !push_instr(ctx, OP_undefined) == -1)
971 return E_OUTOFMEMORY;
973 return S_OK;
976 /* ECMA-262 3rd Edition 12.5 */
977 static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat)
979 unsigned jmp_else, jmp_end;
980 HRESULT hres;
982 hres = compile_expression(ctx, stat->expr);
983 if(FAILED(hres))
984 return hres;
986 jmp_else = push_instr(ctx, OP_jmp_z);
987 if(jmp_else == -1)
988 return E_OUTOFMEMORY;
990 hres = compile_statement(ctx, NULL, stat->if_stat);
991 if(FAILED(hres))
992 return hres;
994 jmp_end = push_instr(ctx, OP_jmp);
995 if(jmp_end == -1)
996 return E_OUTOFMEMORY;
998 instr_ptr(ctx, jmp_else)->arg1.uint = ctx->code_off;
1000 if(stat->else_stat) {
1001 hres = compile_statement(ctx, NULL, stat->else_stat);
1002 if(FAILED(hres))
1003 return hres;
1004 }else {
1005 /* FIXME: We could sometimes avoid it */
1006 if(push_instr(ctx, OP_undefined) == -1)
1007 return E_OUTOFMEMORY;
1010 instr_ptr(ctx, jmp_end)->arg1.uint = ctx->code_off;
1011 return S_OK;
1014 /* ECMA-262 3rd Edition 12.6.2 */
1015 static HRESULT compile_while_statement(compiler_ctx_t *ctx, while_statement_t *stat)
1017 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1018 unsigned off_backup, jmp_off;
1019 HRESULT hres;
1021 off_backup = ctx->code_off;
1023 stat_ctx.break_label = alloc_label(ctx);
1024 if(stat_ctx.break_label == -1)
1025 return E_OUTOFMEMORY;
1027 stat_ctx.continue_label = alloc_label(ctx);
1028 if(stat_ctx.continue_label == -1)
1029 return E_OUTOFMEMORY;
1031 if(!stat->do_while) {
1032 /* FIXME: avoid */
1033 if(push_instr(ctx, OP_undefined) == -1)
1034 return E_OUTOFMEMORY;
1036 jmp_off = ctx->code_off;
1037 label_set_addr(ctx, stat_ctx.continue_label);
1038 hres = compile_expression(ctx, stat->expr);
1039 if(FAILED(hres))
1040 return hres;
1042 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1043 if(FAILED(hres))
1044 return hres;
1046 if(push_instr(ctx, OP_pop) == -1)
1047 return E_OUTOFMEMORY;
1048 }else {
1049 jmp_off = ctx->code_off;
1052 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1053 if(hres == E_NOTIMPL) {
1054 ctx->code_off = off_backup;
1055 stat->stat.eval = while_statement_eval;
1056 return compile_interp_fallback(ctx, &stat->stat);
1058 if(FAILED(hres))
1059 return hres;
1061 if(stat->do_while) {
1062 label_set_addr(ctx, stat_ctx.continue_label);
1063 hres = compile_expression(ctx, stat->expr);
1064 if(FAILED(hres))
1065 return hres;
1067 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1068 if(FAILED(hres))
1069 return hres;
1071 if(push_instr(ctx, OP_pop) == -1)
1072 return E_OUTOFMEMORY;
1075 hres = push_instr_uint(ctx, OP_jmp, jmp_off);
1076 if(FAILED(hres))
1077 return hres;
1079 label_set_addr(ctx, stat_ctx.break_label);
1080 return S_OK;
1083 /* ECMA-262 3rd Edition 12.6.3 */
1084 static HRESULT compile_for_statement(compiler_ctx_t *ctx, for_statement_t *stat)
1086 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1087 unsigned off_backup, expr_off;
1088 HRESULT hres;
1090 off_backup = ctx->code_off;
1092 if(stat->variable_list) {
1093 hres = compile_variable_list(ctx, stat->variable_list);
1094 if(FAILED(hres))
1095 return hres;
1096 }else if(stat->begin_expr) {
1097 BOOL no_ret = FALSE;
1099 hres = compile_expression_noret(ctx, stat->begin_expr, &no_ret);
1100 if(FAILED(hres))
1101 return hres;
1102 if(!no_ret && push_instr(ctx, OP_pop) == -1)
1103 return E_OUTOFMEMORY;
1106 stat_ctx.break_label = alloc_label(ctx);
1107 if(stat_ctx.break_label == -1)
1108 return E_OUTOFMEMORY;
1110 stat_ctx.continue_label = alloc_label(ctx);
1111 if(stat_ctx.continue_label == -1)
1112 return E_OUTOFMEMORY;
1114 /* FIXME: avoid */
1115 if(push_instr(ctx, OP_undefined) == -1)
1116 return E_OUTOFMEMORY;
1118 expr_off = ctx->code_off;
1120 if(stat->expr) {
1121 hres = compile_expression(ctx, stat->expr);
1122 if(FAILED(hres))
1123 return hres;
1125 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1126 if(FAILED(hres))
1127 return hres;
1130 if(push_instr(ctx, OP_pop) == -1)
1131 return E_OUTOFMEMORY;
1133 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1134 if(hres == E_NOTIMPL) {
1135 ctx->code_off = off_backup;
1136 stat->stat.eval = for_statement_eval;
1137 return compile_interp_fallback(ctx, &stat->stat);
1139 if(FAILED(hres))
1140 return hres;
1142 label_set_addr(ctx, stat_ctx.continue_label);
1144 if(stat->end_expr) {
1145 BOOL no_ret = FALSE;
1147 hres = compile_expression_noret(ctx, stat->end_expr, &no_ret);
1148 if(FAILED(hres))
1149 return hres;
1151 if(!no_ret && push_instr(ctx, OP_pop) == -1)
1152 return E_OUTOFMEMORY;
1155 hres = push_instr_uint(ctx, OP_jmp, expr_off);
1156 if(FAILED(hres))
1157 return hres;
1159 label_set_addr(ctx, stat_ctx.break_label);
1160 return S_OK;
1163 /* ECMA-262 3rd Edition 12.6.4 */
1164 static HRESULT compile_forin_statement(compiler_ctx_t *ctx, forin_statement_t *stat)
1166 statement_ctx_t stat_ctx = {4, FALSE, FALSE};
1167 unsigned off_backup = ctx->code_off;
1168 HRESULT hres;
1170 if(stat->variable) {
1171 hres = compile_variable_list(ctx, stat->variable);
1172 if(FAILED(hres))
1173 return hres;
1176 stat_ctx.break_label = alloc_label(ctx);
1177 if(stat_ctx.break_label == -1)
1178 return E_OUTOFMEMORY;
1180 stat_ctx.continue_label = alloc_label(ctx);
1181 if(stat_ctx.continue_label == -1)
1182 return E_OUTOFMEMORY;
1184 hres = compile_expression(ctx, stat->in_expr);
1185 if(FAILED(hres))
1186 return hres;
1188 if(stat->variable) {
1189 hres = push_instr_bstr_uint(ctx, OP_identid, stat->variable->identifier, fdexNameEnsure);
1190 if(FAILED(hres))
1191 return hres;
1192 }else if(is_memberid_expr(stat->expr->type)) {
1193 hres = compile_memberid_expression(ctx, stat->expr, fdexNameEnsure);
1194 if(FAILED(hres))
1195 return hres;
1196 }else {
1197 hres = push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
1198 if(FAILED(hres))
1199 return hres;
1201 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1202 return S_OK;
1205 hres = push_instr_int(ctx, OP_int, DISPID_STARTENUM);
1206 if(FAILED(hres))
1207 return hres;
1209 /* FIXME: avoid */
1210 if(push_instr(ctx, OP_undefined) == -1)
1211 return E_OUTOFMEMORY;
1213 label_set_addr(ctx, stat_ctx.continue_label);
1214 hres = push_instr_uint(ctx, OP_forin, stat_ctx.break_label);
1215 if(FAILED(hres))
1216 return E_OUTOFMEMORY;
1218 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1219 if(hres == E_NOTIMPL) {
1220 ctx->code_off = off_backup;
1221 stat->stat.eval = forin_statement_eval;
1222 return compile_interp_fallback(ctx, &stat->stat);
1224 if(FAILED(hres))
1225 return hres;
1227 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.continue_label);
1228 if(FAILED(hres))
1229 return hres;
1231 label_set_addr(ctx, stat_ctx.break_label);
1232 return S_OK;
1235 static HRESULT pop_to_stat(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx)
1237 statement_ctx_t *iter = ctx->stat_ctx;
1238 unsigned stack_pop = 0;
1240 while(1) {
1241 if(iter->using_scope && push_instr(ctx, OP_pop_scope) == -1)
1242 return E_OUTOFMEMORY;
1243 if(iter->using_except && push_instr(ctx, OP_pop_except) == -1)
1244 return E_OUTOFMEMORY;
1245 stack_pop += iter->stack_use;
1246 if(iter == stat_ctx)
1247 break;
1248 iter = iter->next;
1251 /* FIXME: optimize */
1252 while(stack_pop--) {
1253 if(push_instr(ctx, OP_pop) == -1)
1254 return E_OUTOFMEMORY;
1257 return S_OK;
1260 /* ECMA-262 3rd Edition 12.7 */
1261 static HRESULT compile_continue_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1263 statement_ctx_t *pop_ctx;
1264 HRESULT hres;
1266 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1267 if(pop_ctx->continue_label != -1)
1268 break;
1271 if(!pop_ctx || stat->identifier) {
1272 stat->stat.eval = continue_statement_eval;
1273 return compile_interp_fallback(ctx, &stat->stat);
1276 hres = pop_to_stat(ctx, pop_ctx);
1277 if(FAILED(hres))
1278 return hres;
1280 if(push_instr(ctx, OP_undefined) == -1)
1281 return E_OUTOFMEMORY;
1283 return push_instr_uint(ctx, OP_jmp, pop_ctx->continue_label);
1286 /* ECMA-262 3rd Edition 12.8 */
1287 static HRESULT compile_break_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1289 statement_ctx_t *pop_ctx;
1290 HRESULT hres;
1292 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1293 if(pop_ctx->break_label != -1)
1294 break;
1297 if(!pop_ctx || stat->identifier) {
1298 stat->stat.eval = break_statement_eval;
1299 return compile_interp_fallback(ctx, &stat->stat);
1302 hres = pop_to_stat(ctx, pop_ctx);
1303 if(FAILED(hres))
1304 return hres;
1306 if(push_instr(ctx, OP_undefined) == -1)
1307 return E_OUTOFMEMORY;
1309 return push_instr_uint(ctx, OP_jmp, pop_ctx->break_label);
1312 /* ECMA-262 3rd Edition 12.10 */
1313 static HRESULT compile_with_statement(compiler_ctx_t *ctx, with_statement_t *stat)
1315 statement_ctx_t stat_ctx = {0, TRUE, FALSE, -1, -1};
1316 HRESULT hres;
1318 hres = compile_expression(ctx, stat->expr);
1319 if(FAILED(hres))
1320 return hres;
1322 if(push_instr(ctx, OP_push_scope) == -1)
1323 return E_OUTOFMEMORY;
1325 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1326 if(FAILED(hres))
1327 return hres;
1329 if(push_instr(ctx, OP_pop_scope) == -1)
1330 return E_OUTOFMEMORY;
1332 return S_OK;
1335 /* ECMA-262 3rd Edition 12.13 */
1336 static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat)
1338 statement_ctx_t stat_ctx = {0, FALSE, FALSE, -1, -1};
1339 unsigned case_cnt = 0, *case_jmps, i, default_jmp;
1340 BOOL have_default = FALSE;
1341 statement_t *stat_iter;
1342 case_clausule_t *iter;
1343 unsigned off_backup;
1344 HRESULT hres;
1346 off_backup = ctx->code_off;
1348 hres = compile_expression(ctx, stat->expr);
1349 if(FAILED(hres))
1350 return hres;
1352 stat_ctx.break_label = alloc_label(ctx);
1353 if(stat_ctx.break_label == -1)
1354 return E_OUTOFMEMORY;
1356 for(iter = stat->case_list; iter; iter = iter->next) {
1357 if(iter->expr)
1358 case_cnt++;
1361 case_jmps = heap_alloc(case_cnt * sizeof(*case_jmps));
1362 if(!case_jmps)
1363 return E_OUTOFMEMORY;
1365 i = 0;
1366 for(iter = stat->case_list; iter; iter = iter->next) {
1367 if(!iter->expr) {
1368 have_default = TRUE;
1369 continue;
1372 hres = compile_expression(ctx, iter->expr);
1373 if(FAILED(hres))
1374 break;
1376 case_jmps[i] = push_instr(ctx, OP_case);
1377 if(case_jmps[i] == -1) {
1378 hres = E_OUTOFMEMORY;
1379 break;
1381 i++;
1384 if(SUCCEEDED(hres)) {
1385 if(push_instr(ctx, OP_pop) != -1) {
1386 default_jmp = push_instr(ctx, OP_jmp);
1387 if(default_jmp == -1)
1388 hres = E_OUTOFMEMORY;
1389 }else {
1390 hres = E_OUTOFMEMORY;
1394 if(FAILED(hres)) {
1395 heap_free(case_jmps);
1396 return hres;
1399 i = 0;
1400 for(iter = stat->case_list; iter; iter = iter->next) {
1401 while(iter->next && iter->next->stat == iter->stat) {
1402 instr_ptr(ctx, iter->expr ? case_jmps[i++] : default_jmp)->arg1.uint = ctx->code_off;
1403 iter = iter->next;
1406 instr_ptr(ctx, iter->expr ? case_jmps[i++] : default_jmp)->arg1.uint = ctx->code_off;
1408 for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter); stat_iter = stat_iter->next) {
1409 hres = compile_statement(ctx, &stat_ctx, stat_iter);
1410 if(hres == E_NOTIMPL) {
1411 ctx->code_off = off_backup;
1412 stat->stat.eval = switch_statement_eval;
1413 return compile_interp_fallback(ctx, &stat->stat);
1415 if(FAILED(hres))
1416 break;
1418 if(stat_iter->next && push_instr(ctx, OP_pop) == -1) {
1419 hres = E_OUTOFMEMORY;
1420 break;
1423 if(FAILED(hres))
1424 break;
1427 heap_free(case_jmps);
1428 if(FAILED(hres))
1429 return hres;
1430 assert(i == case_cnt);
1432 if(!have_default)
1433 instr_ptr(ctx, default_jmp)->arg1.uint = ctx->code_off;
1435 label_set_addr(ctx, stat_ctx.break_label);
1436 return S_OK;
1439 /* ECMA-262 3rd Edition 12.13 */
1440 static HRESULT compile_throw_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1442 HRESULT hres;
1444 hres = compile_expression(ctx, stat->expr);
1445 if(FAILED(hres))
1446 return hres;
1448 return push_instr(ctx, OP_throw) == -1 ? E_OUTOFMEMORY : S_OK;
1451 /* ECMA-262 3rd Edition 12.14 */
1452 static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat)
1454 statement_ctx_t try_ctx = {0, FALSE, TRUE, -1, -1}, catch_ctx = {0, TRUE, FALSE, -1, -1};
1455 statement_ctx_t finally_ctx = {2, FALSE, FALSE, -1, -1};
1456 unsigned off_backup, push_except;
1457 BSTR ident;
1458 HRESULT hres;
1460 off_backup = ctx->code_off;
1462 push_except = push_instr(ctx, OP_push_except);
1463 if(push_except == -1)
1464 return E_OUTOFMEMORY;
1466 if(stat->catch_block) {
1467 ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier);
1468 if(!ident)
1469 return E_OUTOFMEMORY;
1470 }else {
1471 ident = NULL;
1474 instr_ptr(ctx, push_except)->arg2.bstr = ident;
1476 if(!stat->catch_block)
1477 try_ctx.stack_use = 2;
1479 hres = compile_statement(ctx, &try_ctx, stat->try_statement);
1480 if(hres == E_NOTIMPL) {
1481 ctx->code_off = off_backup;
1482 stat->stat.eval = try_statement_eval;
1483 return compile_interp_fallback(ctx, &stat->stat);
1485 if(FAILED(hres))
1486 return hres;
1488 if(push_instr(ctx, OP_pop_except) == -1)
1489 return E_OUTOFMEMORY;
1491 if(stat->catch_block) {
1492 unsigned jmp_finally;
1494 jmp_finally = push_instr(ctx, OP_jmp);
1495 if(jmp_finally == -1)
1496 return E_OUTOFMEMORY;
1498 instr_ptr(ctx, push_except)->arg1.uint = ctx->code_off;
1500 hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement);
1501 if(hres == E_NOTIMPL) {
1502 ctx->code_off = off_backup;
1503 stat->stat.eval = try_statement_eval;
1504 return compile_interp_fallback(ctx, &stat->stat);
1506 if(FAILED(hres))
1507 return hres;
1509 if(push_instr(ctx, OP_pop_scope) == -1)
1510 return E_OUTOFMEMORY;
1512 instr_ptr(ctx, jmp_finally)->arg1.uint = ctx->code_off;
1513 }else {
1514 instr_ptr(ctx, push_except)->arg1.uint = ctx->code_off;
1517 if(stat->finally_statement) {
1518 /* FIXME: avoid */
1519 if(push_instr(ctx, OP_pop) == -1)
1520 return E_OUTOFMEMORY;
1522 hres = compile_statement(ctx, stat->catch_block ? NULL : &finally_ctx, stat->finally_statement);
1523 if(hres == E_NOTIMPL) {
1524 ctx->code_off = off_backup;
1525 stat->stat.eval = try_statement_eval;
1526 return compile_interp_fallback(ctx, &stat->stat);
1528 if(FAILED(hres))
1529 return hres;
1531 if(!stat->catch_block && push_instr(ctx, OP_end_finally) == -1)
1532 return E_OUTOFMEMORY;
1535 return S_OK;
1538 static HRESULT compile_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1540 HRESULT hres;
1542 if(stat_ctx) {
1543 stat_ctx->next = ctx->stat_ctx;
1544 ctx->stat_ctx = stat_ctx;
1547 switch(stat->type) {
1548 case STAT_BLOCK:
1549 hres = compile_block_statement(ctx, ((block_statement_t*)stat)->stat_list);
1550 break;
1551 case STAT_BREAK:
1552 hres = compile_break_statement(ctx, (branch_statement_t*)stat);
1553 break;
1554 case STAT_CONTINUE:
1555 hres = compile_continue_statement(ctx, (branch_statement_t*)stat);
1556 break;
1557 case STAT_EMPTY:
1558 hres = push_instr(ctx, OP_undefined) == -1 ? E_OUTOFMEMORY : S_OK; /* FIXME */
1559 break;
1560 case STAT_EXPR:
1561 hres = compile_expression_statement(ctx, (expression_statement_t*)stat);
1562 break;
1563 case STAT_FOR:
1564 hres = compile_for_statement(ctx, (for_statement_t*)stat);
1565 break;
1566 case STAT_FORIN:
1567 hres = compile_forin_statement(ctx, (forin_statement_t*)stat);
1568 break;
1569 case STAT_IF:
1570 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1571 break;
1572 case STAT_LABEL:
1573 hres = push_instr(ctx, OP_label) == -1 ? E_OUTOFMEMORY : S_OK; /* FIXME */
1574 break;
1575 case STAT_SWITCH:
1576 hres = compile_switch_statement(ctx, (switch_statement_t*)stat);
1577 break;
1578 case STAT_THROW:
1579 hres = compile_throw_statement(ctx, (expression_statement_t*)stat);
1580 break;
1581 case STAT_TRY:
1582 hres = compile_try_statement(ctx, (try_statement_t*)stat);
1583 break;
1584 case STAT_VAR:
1585 hres = compile_var_statement(ctx, (var_statement_t*)stat);
1586 break;
1587 case STAT_WHILE:
1588 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1589 break;
1590 case STAT_WITH:
1591 hres = compile_with_statement(ctx, (with_statement_t*)stat);
1592 break;
1593 default:
1594 hres = compile_interp_fallback(ctx, stat);
1597 if(stat_ctx) {
1598 assert(ctx->stat_ctx == stat_ctx);
1599 ctx->stat_ctx = stat_ctx->next;
1602 return hres;
1605 static void resolve_labels(compiler_ctx_t *ctx, unsigned off)
1607 instr_t *instr;
1609 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
1610 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
1611 assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
1612 instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
1614 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
1617 ctx->labels_cnt = 0;
1620 void release_bytecode(bytecode_t *code)
1622 unsigned i;
1624 for(i=0; i < code->bstr_cnt; i++)
1625 SysFreeString(code->bstr_pool[i]);
1627 jsheap_free(&code->heap);
1628 heap_free(code->bstr_pool);
1629 heap_free(code->instrs);
1630 heap_free(code);
1633 void release_compiler(compiler_ctx_t *ctx)
1635 heap_free(ctx);
1638 static HRESULT init_compiler(parser_ctx_t *parser)
1640 if(!parser->code) {
1641 parser->code = heap_alloc_zero(sizeof(bytecode_t));
1642 if(!parser->code)
1643 return E_OUTOFMEMORY;
1644 jsheap_init(&parser->code->heap);
1647 if(!parser->compiler) {
1648 parser->compiler = heap_alloc_zero(sizeof(compiler_ctx_t));
1649 if(!parser->compiler)
1650 return E_OUTOFMEMORY;
1652 parser->compiler->parser = parser;
1653 parser->compiler->code = parser->code;
1656 return S_OK;
1659 HRESULT compile_subscript(parser_ctx_t *parser, expression_t *expr, unsigned *ret_off)
1661 HRESULT hres;
1663 hres = init_compiler(parser);
1664 if(FAILED(hres))
1665 return hres;
1667 *ret_off = parser->compiler->code_off;
1668 hres = compile_expression(parser->compiler, expr);
1669 if(FAILED(hres))
1670 return hres;
1672 return push_instr(parser->compiler, OP_ret) == -1 ? E_OUTOFMEMORY : S_OK;
1675 HRESULT compile_subscript_stat(parser_ctx_t *parser, statement_t *stat, BOOL compile_block, unsigned *ret_off)
1677 HRESULT hres;
1679 TRACE("\n");
1681 hres = init_compiler(parser);
1682 if(FAILED(hres))
1683 return hres;
1685 *ret_off = parser->compiler->code_off;
1686 if(compile_block && stat->next)
1687 hres = compile_block_statement(parser->compiler, stat);
1688 else
1689 hres = compile_statement(parser->compiler, NULL, stat);
1690 if(FAILED(hres))
1691 return hres;
1693 resolve_labels(parser->compiler, *ret_off);
1695 return push_instr(parser->compiler, OP_ret) == -1 ? E_OUTOFMEMORY : S_OK;