msvcrt: Add declaration for _sc[w]printf to header.
[wine.git] / dlls / jscript / compile.c
blob7099a6ac8b08c0a7466dad7e7637eb03a811320c
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 BOOL ensure_bstr_slot(compiler_ctx_t *ctx)
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 FALSE;
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 FALSE;
148 ctx->code->bstr_pool = new_pool;
149 ctx->code->bstr_pool_size *= 2;
152 return TRUE;
155 static BSTR compiler_alloc_bstr(compiler_ctx_t *ctx, const WCHAR *str)
157 if(!ensure_bstr_slot(ctx))
158 return NULL;
160 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
161 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
162 return NULL;
164 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
167 static BSTR compiler_alloc_bstr_len(compiler_ctx_t *ctx, const WCHAR *str, size_t len)
169 if(!ensure_bstr_slot(ctx))
170 return NULL;
172 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocStringLen(str, len);
173 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
174 return NULL;
176 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
179 static unsigned push_instr(compiler_ctx_t *ctx, jsop_t op)
181 assert(ctx->code_size >= ctx->code_off);
183 if(ctx->code_size == ctx->code_off) {
184 instr_t *new_instrs;
186 new_instrs = heap_realloc(ctx->code->instrs, ctx->code_size*2*sizeof(instr_t));
187 if(!new_instrs)
188 return 0;
190 ctx->code->instrs = new_instrs;
191 ctx->code_size *= 2;
194 ctx->code->instrs[ctx->code_off].op = op;
195 return ctx->code_off++;
198 static inline instr_t *instr_ptr(compiler_ctx_t *ctx, unsigned off)
200 assert(off < ctx->code_off);
201 return ctx->code->instrs + off;
204 static HRESULT push_instr_int(compiler_ctx_t *ctx, jsop_t op, LONG arg)
206 unsigned instr;
208 instr = push_instr(ctx, op);
209 if(!instr)
210 return E_OUTOFMEMORY;
212 instr_ptr(ctx, instr)->u.arg->lng = arg;
213 return S_OK;
216 static HRESULT push_instr_str(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
218 unsigned instr;
219 WCHAR *str;
221 str = compiler_alloc_string(ctx->code, arg);
222 if(!str)
223 return E_OUTOFMEMORY;
225 instr = push_instr(ctx, op);
226 if(!instr)
227 return E_OUTOFMEMORY;
229 instr_ptr(ctx, instr)->u.arg->str = str;
230 return S_OK;
233 static HRESULT push_instr_bstr(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
235 unsigned instr;
236 WCHAR *str;
238 str = compiler_alloc_bstr(ctx, arg);
239 if(!str)
240 return E_OUTOFMEMORY;
242 instr = push_instr(ctx, op);
243 if(!instr)
244 return E_OUTOFMEMORY;
246 instr_ptr(ctx, instr)->u.arg->bstr = str;
247 return S_OK;
250 static HRESULT push_instr_bstr_uint(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg1, unsigned arg2)
252 unsigned instr;
253 WCHAR *str;
255 str = compiler_alloc_bstr(ctx, arg1);
256 if(!str)
257 return E_OUTOFMEMORY;
259 instr = push_instr(ctx, op);
260 if(!instr)
261 return E_OUTOFMEMORY;
263 instr_ptr(ctx, instr)->u.arg[0].bstr = str;
264 instr_ptr(ctx, instr)->u.arg[1].uint = arg2;
265 return S_OK;
268 static HRESULT push_instr_uint_str(compiler_ctx_t *ctx, jsop_t op, unsigned arg1, const WCHAR *arg2)
270 unsigned instr;
271 WCHAR *str;
273 str = compiler_alloc_string(ctx->code, arg2);
274 if(!str)
275 return E_OUTOFMEMORY;
277 instr = push_instr(ctx, op);
278 if(!instr)
279 return E_OUTOFMEMORY;
281 instr_ptr(ctx, instr)->u.arg[0].uint = arg1;
282 instr_ptr(ctx, instr)->u.arg[1].str = str;
283 return S_OK;
286 static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
288 unsigned instr;
290 instr = push_instr(ctx, op);
291 if(!instr)
292 return E_OUTOFMEMORY;
294 instr_ptr(ctx, instr)->u.dbl = arg;
295 return S_OK;
298 static inline void set_arg_uint(compiler_ctx_t *ctx, unsigned instr, unsigned arg)
300 instr_ptr(ctx, instr)->u.arg->uint = arg;
303 static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
305 unsigned instr;
307 instr = push_instr(ctx, op);
308 if(!instr)
309 return E_OUTOFMEMORY;
311 set_arg_uint(ctx, instr, arg);
312 return S_OK;
315 static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
317 HRESULT hres;
319 hres = compile_expression(ctx, expr->expression1);
320 if(FAILED(hres))
321 return hres;
323 hres = compile_expression(ctx, expr->expression2);
324 if(FAILED(hres))
325 return hres;
327 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
330 static HRESULT compile_unary_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op)
332 HRESULT hres;
334 hres = compile_expression(ctx, expr->expression);
335 if(FAILED(hres))
336 return hres;
338 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
341 /* ECMA-262 3rd Edition 11.2.1 */
342 static HRESULT compile_member_expression(compiler_ctx_t *ctx, member_expression_t *expr)
344 HRESULT hres;
346 hres = compile_expression(ctx, expr->expression);
347 if(FAILED(hres))
348 return hres;
350 return push_instr_bstr(ctx, OP_member, expr->identifier);
353 #define LABEL_FLAG 0x80000000
355 static unsigned alloc_label(compiler_ctx_t *ctx)
357 if(!ctx->labels_size) {
358 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
359 if(!ctx->labels)
360 return 0;
361 ctx->labels_size = 8;
362 }else if(ctx->labels_size == ctx->labels_cnt) {
363 unsigned *new_labels;
365 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
366 if(!new_labels)
367 return 0;
369 ctx->labels = new_labels;
370 ctx->labels_size *= 2;
373 return ctx->labels_cnt++ | LABEL_FLAG;
376 static void label_set_addr(compiler_ctx_t *ctx, unsigned label)
378 assert(label & LABEL_FLAG);
379 ctx->labels[label & ~LABEL_FLAG] = ctx->code_off;
382 static inline BOOL is_memberid_expr(expression_type_t type)
384 return type == EXPR_IDENT || type == EXPR_MEMBER || type == EXPR_ARRAY;
387 static HRESULT compile_memberid_expression(compiler_ctx_t *ctx, expression_t *expr, unsigned flags)
389 HRESULT hres = S_OK;
391 switch(expr->type) {
392 case EXPR_IDENT: {
393 identifier_expression_t *ident_expr = (identifier_expression_t*)expr;
395 hres = push_instr_bstr_uint(ctx, OP_identid, ident_expr->identifier, flags);
396 break;
398 case EXPR_ARRAY: {
399 binary_expression_t *array_expr = (binary_expression_t*)expr;
401 hres = compile_expression(ctx, array_expr->expression1);
402 if(FAILED(hres))
403 return hres;
405 hres = compile_expression(ctx, array_expr->expression2);
406 if(FAILED(hres))
407 return hres;
409 hres = push_instr_uint(ctx, OP_memberid, flags);
410 break;
412 case EXPR_MEMBER: {
413 member_expression_t *member_expr = (member_expression_t*)expr;
415 hres = compile_expression(ctx, member_expr->expression);
416 if(FAILED(hres))
417 return hres;
419 /* FIXME: Potential optimization */
420 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
421 if(FAILED(hres))
422 return hres;
424 hres = push_instr_uint(ctx, OP_memberid, flags);
425 break;
427 default:
428 assert(0);
431 return hres;
434 static HRESULT compile_increment_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op, int n)
436 HRESULT hres;
438 if(!is_memberid_expr(expr->expression->type)) {
439 hres = compile_expression(ctx, expr->expression);
440 if(FAILED(hres))
441 return hres;
443 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
446 hres = compile_memberid_expression(ctx, expr->expression, fdexNameEnsure);
447 if(FAILED(hres))
448 return hres;
450 return push_instr_int(ctx, op, n);
453 /* ECMA-262 3rd Edition 11.14 */
454 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr)
456 HRESULT hres;
458 hres = compile_expression(ctx, expr->expression1);
459 if(FAILED(hres))
460 return hres;
462 if(!push_instr(ctx, OP_pop))
463 return E_OUTOFMEMORY;
465 return compile_expression(ctx, expr->expression2);
468 /* ECMA-262 3rd Edition 11.11 */
469 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
471 unsigned instr;
472 HRESULT hres;
474 hres = compile_expression(ctx, expr->expression1);
475 if(FAILED(hres))
476 return hres;
478 instr = push_instr(ctx, op);
479 if(!instr)
480 return E_OUTOFMEMORY;
482 hres = compile_expression(ctx, expr->expression2);
483 if(FAILED(hres))
484 return hres;
486 set_arg_uint(ctx, instr, ctx->code_off);
487 return S_OK;
490 /* ECMA-262 3rd Edition 11.12 */
491 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
493 unsigned jmp_false, jmp_end;
494 HRESULT hres;
496 hres = compile_expression(ctx, expr->expression);
497 if(FAILED(hres))
498 return hres;
500 jmp_false = push_instr(ctx, OP_cnd_z);
501 if(!jmp_false)
502 return E_OUTOFMEMORY;
504 hres = compile_expression(ctx, expr->true_expression);
505 if(FAILED(hres))
506 return hres;
508 jmp_end = push_instr(ctx, OP_jmp);
509 if(!jmp_end)
510 return E_OUTOFMEMORY;
512 set_arg_uint(ctx, jmp_false, ctx->code_off);
513 if(!push_instr(ctx, OP_pop))
514 return E_OUTOFMEMORY;
516 hres = compile_expression(ctx, expr->false_expression);
517 if(FAILED(hres))
518 return hres;
520 set_arg_uint(ctx, jmp_end, ctx->code_off);
521 return S_OK;
524 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
526 unsigned arg_cnt = 0;
527 argument_t *arg;
528 HRESULT hres;
530 hres = compile_expression(ctx, expr->expression);
531 if(FAILED(hres))
532 return hres;
534 for(arg = expr->argument_list; arg; arg = arg->next) {
535 hres = compile_expression(ctx, arg->expr);
536 if(FAILED(hres))
537 return hres;
538 arg_cnt++;
541 return push_instr_uint(ctx, OP_new, arg_cnt);
544 static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL *no_ret)
546 unsigned arg_cnt = 0;
547 argument_t *arg;
548 unsigned instr;
549 jsop_t op;
550 HRESULT hres;
552 if(is_memberid_expr(expr->expression->type)) {
553 op = OP_call_member;
554 hres = compile_memberid_expression(ctx, expr->expression, 0);
555 }else {
556 op = OP_call;
557 hres = compile_expression(ctx, expr->expression);
560 if(FAILED(hres))
561 return hres;
563 for(arg = expr->argument_list; arg; arg = arg->next) {
564 hres = compile_expression(ctx, arg->expr);
565 if(FAILED(hres))
566 return hres;
567 arg_cnt++;
570 instr = push_instr(ctx, op);
571 if(!instr)
572 return E_OUTOFMEMORY;
574 instr_ptr(ctx, instr)->u.arg[0].uint = arg_cnt;
575 instr_ptr(ctx, instr)->u.arg[1].lng = no_ret == NULL;
576 if(no_ret)
577 *no_ret = TRUE;
578 return S_OK;
581 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
583 HRESULT hres;
585 switch(expr->expression->type) {
586 case EXPR_ARRAY: {
587 binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
589 hres = compile_expression(ctx, array_expr->expression1);
590 if(FAILED(hres))
591 return hres;
593 hres = compile_expression(ctx, array_expr->expression2);
594 if(FAILED(hres))
595 return hres;
597 if(!push_instr(ctx, OP_delete))
598 return E_OUTOFMEMORY;
599 break;
601 case EXPR_MEMBER: {
602 member_expression_t *member_expr = (member_expression_t*)expr->expression;
604 hres = compile_expression(ctx, member_expr->expression);
605 if(FAILED(hres))
606 return hres;
608 /* FIXME: Potential optimization */
609 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
610 if(FAILED(hres))
611 return hres;
613 if(!push_instr(ctx, OP_delete))
614 return E_OUTOFMEMORY;
615 break;
617 case EXPR_IDENT:
618 return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
619 default: {
620 const WCHAR fixmeW[] = {'F','I','X','M','E',0};
622 WARN("invalid delete, unimplemented exception message\n");
624 hres = compile_expression(ctx, expr->expression);
625 if(FAILED(hres))
626 return hres;
628 return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, fixmeW);
632 return S_OK;
635 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
637 BOOL use_throw_path = FALSE;
638 unsigned arg_cnt = 0;
639 HRESULT hres;
641 if(expr->expression1->type == EXPR_CALL) {
642 call_expression_t *call_expr = (call_expression_t*)expr->expression1;
643 argument_t *arg;
645 if(op != OP_LAST) {
646 FIXME("op %d not supported on parametrized assign expressions\n", op);
647 return E_NOTIMPL;
650 if(is_memberid_expr(call_expr->expression->type) && call_expr->argument_list) {
651 hres = compile_memberid_expression(ctx, call_expr->expression, fdexNameEnsure);
652 if(FAILED(hres))
653 return hres;
655 for(arg = call_expr->argument_list; arg; arg = arg->next) {
656 hres = compile_expression(ctx, arg->expr);
657 if(FAILED(hres))
658 return hres;
659 arg_cnt++;
661 }else {
662 use_throw_path = TRUE;
664 }else if(is_memberid_expr(expr->expression1->type)) {
665 hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
666 if(FAILED(hres))
667 return hres;
668 }else {
669 use_throw_path = TRUE;
672 if(use_throw_path) {
673 /* Illegal assignment: evaluate and throw */
674 hres = compile_expression(ctx, expr->expression1);
675 if(FAILED(hres))
676 return hres;
678 hres = compile_expression(ctx, expr->expression2);
679 if(FAILED(hres))
680 return hres;
682 if(op != OP_LAST && !push_instr(ctx, op))
683 return E_OUTOFMEMORY;
685 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
688 if(op != OP_LAST && !push_instr(ctx, OP_refval))
689 return E_OUTOFMEMORY;
691 hres = compile_expression(ctx, expr->expression2);
692 if(FAILED(hres))
693 return hres;
695 if(op != OP_LAST && !push_instr(ctx, op))
696 return E_OUTOFMEMORY;
698 if(arg_cnt)
699 return push_instr_uint(ctx, OP_assign_call, arg_cnt);
701 if(!push_instr(ctx, OP_assign))
702 return E_OUTOFMEMORY;
704 return S_OK;
707 static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
709 jsop_t op;
710 HRESULT hres;
712 if(is_memberid_expr(expr->expression->type)) {
713 if(expr->expression->type == EXPR_IDENT)
714 return push_instr_str(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
716 op = OP_typeofid;
717 hres = compile_memberid_expression(ctx, expr->expression, 0);
718 }else {
719 op = OP_typeof;
720 hres = compile_expression(ctx, expr->expression);
722 if(FAILED(hres))
723 return hres;
725 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
728 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
730 switch(literal->type) {
731 case LT_BOOL:
732 return push_instr_int(ctx, OP_bool, literal->u.bval);
733 case LT_DOUBLE:
734 return push_instr_double(ctx, OP_double, literal->u.dval);
735 case LT_NULL:
736 return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
737 case LT_STRING:
738 return push_instr_str(ctx, OP_str, literal->u.wstr);
739 case LT_REGEXP: {
740 unsigned instr;
741 WCHAR *str;
743 str = compiler_alloc(ctx->code, (literal->u.regexp.str_len+1)*sizeof(WCHAR));
744 if(!str)
745 return E_OUTOFMEMORY;
746 memcpy(str, literal->u.regexp.str, literal->u.regexp.str_len*sizeof(WCHAR));
747 str[literal->u.regexp.str_len] = 0;
749 instr = push_instr(ctx, OP_regexp);
750 if(!instr)
751 return E_OUTOFMEMORY;
753 instr_ptr(ctx, instr)->u.arg[0].str = str;
754 instr_ptr(ctx, instr)->u.arg[1].uint = literal->u.regexp.flags;
755 return S_OK;
757 default:
758 assert(0);
759 return E_FAIL;
763 static HRESULT literal_as_bstr(compiler_ctx_t *ctx, literal_t *literal, BSTR *str)
765 switch(literal->type) {
766 case LT_STRING:
767 *str = compiler_alloc_bstr(ctx, literal->u.wstr);
768 break;
769 case LT_DOUBLE:
770 return double_to_bstr(literal->u.dval, str);
771 default:
772 assert(0);
775 return *str ? S_OK : E_OUTOFMEMORY;
778 static HRESULT compile_array_literal(compiler_ctx_t *ctx, array_literal_expression_t *expr)
780 unsigned i, elem_cnt = expr->length;
781 array_element_t *iter;
782 HRESULT hres;
784 for(iter = expr->element_list; iter; iter = iter->next) {
785 elem_cnt += iter->elision+1;
787 for(i=0; i < iter->elision; i++) {
788 if(!push_instr(ctx, OP_undefined))
789 return E_OUTOFMEMORY;
792 hres = compile_expression(ctx, iter->expr);
793 if(FAILED(hres))
794 return hres;
797 for(i=0; i < expr->length; i++) {
798 if(!push_instr(ctx, OP_undefined))
799 return E_OUTOFMEMORY;
802 return push_instr_uint(ctx, OP_carray, elem_cnt);
805 static HRESULT compile_object_literal(compiler_ctx_t *ctx, property_value_expression_t *expr)
807 prop_val_t *iter;
808 unsigned instr;
809 BSTR name;
810 HRESULT hres;
812 if(!push_instr(ctx, OP_new_obj))
813 return E_OUTOFMEMORY;
815 for(iter = expr->property_list; iter; iter = iter->next) {
816 hres = literal_as_bstr(ctx, iter->name, &name);
817 if(FAILED(hres))
818 return hres;
820 hres = compile_expression(ctx, iter->value);
821 if(FAILED(hres))
822 return hres;
824 instr = push_instr(ctx, OP_obj_prop);
825 if(!instr)
826 return E_OUTOFMEMORY;
828 instr_ptr(ctx, instr)->u.arg->bstr = name;
831 return S_OK;
834 static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr)
836 ctx->func_tail = ctx->func_tail ? (ctx->func_tail->next = expr) : (ctx->func_head = expr);
838 /* FIXME: not exactly right */
839 if(expr->identifier) {
840 ctx->func->func_cnt++;
841 return push_instr_bstr(ctx, OP_ident, expr->identifier);
844 return push_instr_uint(ctx, OP_func, ctx->func->func_cnt++);
847 static HRESULT compile_expression_noret(compiler_ctx_t *ctx, expression_t *expr, BOOL *no_ret)
849 switch(expr->type) {
850 case EXPR_ADD:
851 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
852 case EXPR_AND:
853 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_z);
854 case EXPR_ARRAY:
855 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
856 case EXPR_ARRAYLIT:
857 return compile_array_literal(ctx, (array_literal_expression_t*)expr);
858 case EXPR_ASSIGN:
859 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
860 case EXPR_ASSIGNADD:
861 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
862 case EXPR_ASSIGNAND:
863 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_and);
864 case EXPR_ASSIGNSUB:
865 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
866 case EXPR_ASSIGNMUL:
867 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
868 case EXPR_ASSIGNDIV:
869 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
870 case EXPR_ASSIGNMOD:
871 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
872 case EXPR_ASSIGNOR:
873 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
874 case EXPR_ASSIGNLSHIFT:
875 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_lshift);
876 case EXPR_ASSIGNRSHIFT:
877 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift);
878 case EXPR_ASSIGNRRSHIFT:
879 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
880 case EXPR_ASSIGNXOR:
881 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
882 case EXPR_BAND:
883 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
884 case EXPR_BITNEG:
885 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
886 case EXPR_BOR:
887 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
888 case EXPR_CALL:
889 return compile_call_expression(ctx, (call_expression_t*)expr, no_ret);
890 case EXPR_COMMA:
891 return compile_comma_expression(ctx, (binary_expression_t*)expr);
892 case EXPR_COND:
893 return compile_conditional_expression(ctx, (conditional_expression_t*)expr);
894 case EXPR_DELETE:
895 return compile_delete_expression(ctx, (unary_expression_t*)expr);
896 case EXPR_DIV:
897 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
898 case EXPR_EQ:
899 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
900 case EXPR_EQEQ:
901 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
902 case EXPR_FUNC:
903 return compile_function_expression(ctx, (function_expression_t*)expr);
904 case EXPR_GREATER:
905 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
906 case EXPR_GREATEREQ:
907 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
908 case EXPR_IDENT:
909 return push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
910 case EXPR_IN:
911 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
912 case EXPR_INSTANCEOF:
913 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
914 case EXPR_LESS:
915 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
916 case EXPR_LESSEQ:
917 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
918 case EXPR_LITERAL:
919 return compile_literal(ctx, ((literal_expression_t*)expr)->literal);
920 case EXPR_LOGNEG:
921 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
922 case EXPR_LSHIFT:
923 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
924 case EXPR_MEMBER:
925 return compile_member_expression(ctx, (member_expression_t*)expr);
926 case EXPR_MINUS:
927 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
928 case EXPR_MOD:
929 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
930 case EXPR_MUL:
931 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
932 case EXPR_NEW:
933 return compile_new_expression(ctx, (call_expression_t*)expr);
934 case EXPR_NOTEQ:
935 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
936 case EXPR_NOTEQEQ:
937 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
938 case EXPR_OR:
939 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_nz);
940 case EXPR_PLUS:
941 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
942 case EXPR_POSTDEC:
943 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
944 case EXPR_POSTINC:
945 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
946 case EXPR_PREDEC:
947 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
948 case EXPR_PREINC:
949 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
950 case EXPR_PROPVAL:
951 return compile_object_literal(ctx, (property_value_expression_t*)expr);
952 case EXPR_RSHIFT:
953 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
954 case EXPR_RRSHIFT:
955 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
956 case EXPR_SUB:
957 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
958 case EXPR_THIS:
959 return push_instr(ctx, OP_this) ? S_OK : E_OUTOFMEMORY;
960 case EXPR_TYPEOF:
961 return compile_typeof_expression(ctx, (unary_expression_t*)expr);
962 case EXPR_VOID:
963 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
964 case EXPR_BXOR:
965 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
966 default:
967 assert(0);
970 return S_OK;
973 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr)
975 return compile_expression_noret(ctx, expr, NULL);
978 static inline BOOL is_loop_statement(statement_type_t type)
980 return type == STAT_FOR || type == STAT_FORIN || type == STAT_WHILE;
983 /* ECMA-262 3rd Edition 12.1 */
984 static HRESULT compile_block_statement(compiler_ctx_t *ctx, statement_t *iter)
986 HRESULT hres;
988 /* FIXME: do it only if needed */
989 if(!iter)
990 return push_instr(ctx, OP_undefined) ? S_OK : E_OUTOFMEMORY;
992 while(1) {
993 hres = compile_statement(ctx, NULL, iter);
994 if(FAILED(hres))
995 return hres;
997 iter = iter->next;
998 if(!iter)
999 break;
1001 if(!push_instr(ctx, OP_pop))
1002 return E_OUTOFMEMORY;
1005 return S_OK;
1008 /* ECMA-262 3rd Edition 12.2 */
1009 static HRESULT compile_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
1011 variable_declaration_t *iter;
1012 HRESULT hres;
1014 assert(list != NULL);
1016 if(ctx->var_tail)
1017 ctx->var_tail->global_next = list;
1018 else
1019 ctx->var_head = list;
1021 for(iter = list; iter; iter = iter->next) {
1022 ctx->func->var_cnt++;
1023 iter->global_next = iter->next;
1024 if(!iter->next)
1025 ctx->var_tail = iter;
1027 if(!iter->expr)
1028 continue;
1030 hres = compile_expression(ctx, iter->expr);
1031 if(FAILED(hres))
1032 return hres;
1034 hres = push_instr_bstr(ctx, OP_var_set, iter->identifier);
1035 if(FAILED(hres))
1036 return hres;
1039 return S_OK;
1042 /* ECMA-262 3rd Edition 12.2 */
1043 static HRESULT compile_var_statement(compiler_ctx_t *ctx, var_statement_t *stat)
1045 HRESULT hres;
1047 hres = compile_variable_list(ctx, stat->variable_list);
1048 if(FAILED(hres))
1049 return hres;
1051 return push_instr(ctx, OP_undefined) ? S_OK : E_OUTOFMEMORY;
1054 /* ECMA-262 3rd Edition 12.4 */
1055 static HRESULT compile_expression_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1057 BOOL no_ret = FALSE;
1058 HRESULT hres;
1060 hres = compile_expression_noret(ctx, stat->expr, &no_ret);
1061 if(FAILED(hres))
1062 return hres;
1064 /* FIXME: that's a big potential optimization */
1065 if(no_ret && !push_instr(ctx, OP_undefined))
1066 return E_OUTOFMEMORY;
1068 return S_OK;
1071 /* ECMA-262 3rd Edition 12.5 */
1072 static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat)
1074 unsigned jmp_else, jmp_end;
1075 HRESULT hres;
1077 hres = compile_expression(ctx, stat->expr);
1078 if(FAILED(hres))
1079 return hres;
1081 jmp_else = push_instr(ctx, OP_jmp_z);
1082 if(!jmp_else)
1083 return E_OUTOFMEMORY;
1085 hres = compile_statement(ctx, NULL, stat->if_stat);
1086 if(FAILED(hres))
1087 return hres;
1089 jmp_end = push_instr(ctx, OP_jmp);
1090 if(!jmp_end)
1091 return E_OUTOFMEMORY;
1093 set_arg_uint(ctx, jmp_else, ctx->code_off);
1095 if(stat->else_stat) {
1096 hres = compile_statement(ctx, NULL, stat->else_stat);
1097 if(FAILED(hres))
1098 return hres;
1099 }else {
1100 /* FIXME: We could sometimes avoid it */
1101 if(!push_instr(ctx, OP_undefined))
1102 return E_OUTOFMEMORY;
1105 set_arg_uint(ctx, jmp_end, ctx->code_off);
1106 return S_OK;
1109 /* ECMA-262 3rd Edition 12.6.2 */
1110 static HRESULT compile_while_statement(compiler_ctx_t *ctx, while_statement_t *stat)
1112 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1113 unsigned jmp_off;
1114 HRESULT hres;
1116 stat_ctx.break_label = alloc_label(ctx);
1117 if(!stat_ctx.break_label)
1118 return E_OUTOFMEMORY;
1120 stat_ctx.continue_label = alloc_label(ctx);
1121 if(!stat_ctx.continue_label)
1122 return E_OUTOFMEMORY;
1124 if(!stat->do_while) {
1125 /* FIXME: avoid */
1126 if(!push_instr(ctx, OP_undefined))
1127 return E_OUTOFMEMORY;
1129 jmp_off = ctx->code_off;
1130 label_set_addr(ctx, stat_ctx.continue_label);
1131 hres = compile_expression(ctx, stat->expr);
1132 if(FAILED(hres))
1133 return hres;
1135 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1136 if(FAILED(hres))
1137 return hres;
1139 if(!push_instr(ctx, OP_pop))
1140 return E_OUTOFMEMORY;
1141 }else {
1142 jmp_off = ctx->code_off;
1145 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1146 if(FAILED(hres))
1147 return hres;
1149 if(stat->do_while) {
1150 label_set_addr(ctx, stat_ctx.continue_label);
1151 hres = compile_expression(ctx, stat->expr);
1152 if(FAILED(hres))
1153 return hres;
1155 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1156 if(FAILED(hres))
1157 return hres;
1159 if(!push_instr(ctx, OP_pop))
1160 return E_OUTOFMEMORY;
1163 hres = push_instr_uint(ctx, OP_jmp, jmp_off);
1164 if(FAILED(hres))
1165 return hres;
1167 label_set_addr(ctx, stat_ctx.break_label);
1168 return S_OK;
1171 /* ECMA-262 3rd Edition 12.6.3 */
1172 static HRESULT compile_for_statement(compiler_ctx_t *ctx, for_statement_t *stat)
1174 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1175 unsigned expr_off;
1176 HRESULT hres;
1178 if(stat->variable_list) {
1179 hres = compile_variable_list(ctx, stat->variable_list);
1180 if(FAILED(hres))
1181 return hres;
1182 }else if(stat->begin_expr) {
1183 BOOL no_ret = FALSE;
1185 hres = compile_expression_noret(ctx, stat->begin_expr, &no_ret);
1186 if(FAILED(hres))
1187 return hres;
1188 if(!no_ret && !push_instr(ctx, OP_pop))
1189 return E_OUTOFMEMORY;
1192 stat_ctx.break_label = alloc_label(ctx);
1193 if(!stat_ctx.break_label)
1194 return E_OUTOFMEMORY;
1196 stat_ctx.continue_label = alloc_label(ctx);
1197 if(!stat_ctx.continue_label)
1198 return E_OUTOFMEMORY;
1200 /* FIXME: avoid */
1201 if(!push_instr(ctx, OP_undefined))
1202 return E_OUTOFMEMORY;
1204 expr_off = ctx->code_off;
1206 if(stat->expr) {
1207 hres = compile_expression(ctx, stat->expr);
1208 if(FAILED(hres))
1209 return hres;
1211 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1212 if(FAILED(hres))
1213 return hres;
1216 if(!push_instr(ctx, OP_pop))
1217 return E_OUTOFMEMORY;
1219 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1220 if(FAILED(hres))
1221 return hres;
1223 label_set_addr(ctx, stat_ctx.continue_label);
1225 if(stat->end_expr) {
1226 BOOL no_ret = FALSE;
1228 hres = compile_expression_noret(ctx, stat->end_expr, &no_ret);
1229 if(FAILED(hres))
1230 return hres;
1232 if(!no_ret && !push_instr(ctx, OP_pop))
1233 return E_OUTOFMEMORY;
1236 hres = push_instr_uint(ctx, OP_jmp, expr_off);
1237 if(FAILED(hres))
1238 return hres;
1240 label_set_addr(ctx, stat_ctx.break_label);
1241 return S_OK;
1244 /* ECMA-262 3rd Edition 12.6.4 */
1245 static HRESULT compile_forin_statement(compiler_ctx_t *ctx, forin_statement_t *stat)
1247 statement_ctx_t stat_ctx = {4, FALSE, FALSE};
1248 HRESULT hres;
1250 if(stat->variable) {
1251 hres = compile_variable_list(ctx, stat->variable);
1252 if(FAILED(hres))
1253 return hres;
1256 stat_ctx.break_label = alloc_label(ctx);
1257 if(!stat_ctx.break_label)
1258 return E_OUTOFMEMORY;
1260 stat_ctx.continue_label = alloc_label(ctx);
1261 if(!stat_ctx.continue_label)
1262 return E_OUTOFMEMORY;
1264 hres = compile_expression(ctx, stat->in_expr);
1265 if(FAILED(hres))
1266 return hres;
1268 if(stat->variable) {
1269 hres = push_instr_bstr_uint(ctx, OP_identid, stat->variable->identifier, fdexNameEnsure);
1270 if(FAILED(hres))
1271 return hres;
1272 }else if(is_memberid_expr(stat->expr->type)) {
1273 hres = compile_memberid_expression(ctx, stat->expr, fdexNameEnsure);
1274 if(FAILED(hres))
1275 return hres;
1276 }else {
1277 hres = push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
1278 if(FAILED(hres))
1279 return hres;
1281 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1282 return S_OK;
1285 hres = push_instr_int(ctx, OP_int, DISPID_STARTENUM);
1286 if(FAILED(hres))
1287 return hres;
1289 /* FIXME: avoid */
1290 if(!push_instr(ctx, OP_undefined))
1291 return E_OUTOFMEMORY;
1293 label_set_addr(ctx, stat_ctx.continue_label);
1294 hres = push_instr_uint(ctx, OP_forin, stat_ctx.break_label);
1295 if(FAILED(hres))
1296 return E_OUTOFMEMORY;
1298 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1299 if(FAILED(hres))
1300 return hres;
1302 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.continue_label);
1303 if(FAILED(hres))
1304 return hres;
1306 label_set_addr(ctx, stat_ctx.break_label);
1307 return S_OK;
1310 static HRESULT pop_to_stat(compiler_ctx_t *ctx, BOOL var_stack, BOOL scope_stack, statement_ctx_t *stat_ctx)
1312 unsigned stack_pop = 0;
1313 statement_ctx_t *iter;
1315 for(iter = ctx->stat_ctx; iter != stat_ctx; iter = iter->next) {
1316 if(scope_stack) {
1317 if(iter->using_scope && !push_instr(ctx, OP_pop_scope))
1318 return E_OUTOFMEMORY;
1319 if(iter->using_except && !push_instr(ctx, OP_pop_except))
1320 return E_OUTOFMEMORY;
1322 stack_pop += iter->stack_use;
1325 if(var_stack) {
1326 /* FIXME: optimize */
1327 while(stack_pop--) {
1328 if(!push_instr(ctx, OP_pop))
1329 return E_OUTOFMEMORY;
1334 return S_OK;
1337 /* ECMA-262 3rd Edition 12.7 */
1338 static HRESULT compile_continue_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1340 statement_ctx_t *pop_ctx;
1341 HRESULT hres;
1343 if(stat->identifier) {
1344 statement_t *label_stat;
1345 statement_ctx_t *iter;
1347 pop_ctx = NULL;
1349 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1350 if(iter->continue_label)
1351 pop_ctx = iter;
1352 if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier))
1353 break;
1356 if(!iter) {
1357 WARN("Label not found\n");
1358 return JS_E_LABEL_NOT_FOUND;
1361 /* Labelled continue are allowed only on loops */
1362 for(label_stat = iter->labelled_stat->statement;
1363 label_stat->type == STAT_LABEL;
1364 label_stat = ((labelled_statement_t*)label_stat)->statement);
1365 if(!is_loop_statement(label_stat->type)) {
1366 WARN("Label is not a loop\n");
1367 return JS_E_INVALID_CONTINUE;
1369 }else {
1370 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1371 if(pop_ctx->continue_label)
1372 break;
1375 if(!pop_ctx) {
1376 WARN("continue outside loop\n");
1377 return JS_E_INVALID_CONTINUE;
1381 hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx);
1382 if(FAILED(hres))
1383 return hres;
1385 if(!push_instr(ctx, OP_undefined))
1386 return E_OUTOFMEMORY;
1388 return push_instr_uint(ctx, OP_jmp, pop_ctx->continue_label);
1391 /* ECMA-262 3rd Edition 12.8 */
1392 static HRESULT compile_break_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1394 statement_ctx_t *pop_ctx;
1395 HRESULT hres;
1397 if(stat->identifier) {
1398 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1399 if(pop_ctx->labelled_stat && !strcmpW(pop_ctx->labelled_stat->identifier, stat->identifier)) {
1400 assert(pop_ctx->break_label);
1401 break;
1405 if(!pop_ctx) {
1406 WARN("Label not found\n");
1407 return JS_E_LABEL_NOT_FOUND;
1409 }else {
1410 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1411 if(pop_ctx->break_label && !pop_ctx->labelled_stat)
1412 break;
1415 if(!pop_ctx) {
1416 WARN("Break outside loop\n");
1417 return JS_E_INVALID_BREAK;
1421 hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx->next);
1422 if(FAILED(hres))
1423 return hres;
1425 if(!push_instr(ctx, OP_undefined))
1426 return E_OUTOFMEMORY;
1428 return push_instr_uint(ctx, OP_jmp, pop_ctx->break_label);
1431 /* ECMA-262 3rd Edition 12.9 */
1432 static HRESULT compile_return_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1434 HRESULT hres;
1436 hres = pop_to_stat(ctx, TRUE, FALSE, NULL);
1437 if(FAILED(hres))
1438 return hres;
1440 if(stat->expr) {
1441 hres = compile_expression(ctx, stat->expr);
1442 if(FAILED(hres))
1443 return hres;
1446 hres = pop_to_stat(ctx, FALSE, TRUE, NULL);
1447 if(FAILED(hres))
1448 return hres;
1450 return push_instr(ctx, OP_ret) ? S_OK : E_OUTOFMEMORY;
1453 /* ECMA-262 3rd Edition 12.10 */
1454 static HRESULT compile_with_statement(compiler_ctx_t *ctx, with_statement_t *stat)
1456 statement_ctx_t stat_ctx = {0, TRUE, FALSE};
1457 HRESULT hres;
1459 hres = compile_expression(ctx, stat->expr);
1460 if(FAILED(hres))
1461 return hres;
1463 if(!push_instr(ctx, OP_push_scope))
1464 return E_OUTOFMEMORY;
1466 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1467 if(FAILED(hres))
1468 return hres;
1470 if(!push_instr(ctx, OP_pop_scope))
1471 return E_OUTOFMEMORY;
1473 return S_OK;
1476 /* ECMA-262 3rd Edition 12.10 */
1477 static HRESULT compile_labelled_statement(compiler_ctx_t *ctx, labelled_statement_t *stat)
1479 statement_ctx_t stat_ctx = {0, FALSE, FALSE, 0, 0, stat}, *iter;
1480 HRESULT hres;
1482 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1483 if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier)) {
1484 WARN("Label %s redefined\n", debugstr_w(stat->identifier));
1485 return JS_E_LABEL_REDEFINED;
1489 /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1490 stat_ctx.break_label = alloc_label(ctx);
1491 if(!stat_ctx.break_label)
1492 return E_OUTOFMEMORY;
1494 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1495 if(FAILED(hres))
1496 return hres;
1498 label_set_addr(ctx, stat_ctx.break_label);
1499 return S_OK;
1502 /* ECMA-262 3rd Edition 12.13 */
1503 static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat)
1505 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1506 unsigned case_cnt = 0, *case_jmps, i, default_jmp;
1507 BOOL have_default = FALSE;
1508 statement_t *stat_iter;
1509 case_clausule_t *iter;
1510 HRESULT hres;
1512 hres = compile_expression(ctx, stat->expr);
1513 if(FAILED(hres))
1514 return hres;
1516 stat_ctx.break_label = alloc_label(ctx);
1517 if(!stat_ctx.break_label)
1518 return E_OUTOFMEMORY;
1520 for(iter = stat->case_list; iter; iter = iter->next) {
1521 if(iter->expr)
1522 case_cnt++;
1525 case_jmps = heap_alloc(case_cnt * sizeof(*case_jmps));
1526 if(!case_jmps)
1527 return E_OUTOFMEMORY;
1529 i = 0;
1530 for(iter = stat->case_list; iter; iter = iter->next) {
1531 if(!iter->expr) {
1532 have_default = TRUE;
1533 continue;
1536 hres = compile_expression(ctx, iter->expr);
1537 if(FAILED(hres))
1538 break;
1540 case_jmps[i] = push_instr(ctx, OP_case);
1541 if(!case_jmps[i]) {
1542 hres = E_OUTOFMEMORY;
1543 break;
1545 i++;
1548 if(SUCCEEDED(hres)) {
1549 if(push_instr(ctx, OP_pop)) {
1550 default_jmp = push_instr(ctx, OP_jmp);
1551 if(!default_jmp)
1552 hres = E_OUTOFMEMORY;
1553 }else {
1554 hres = E_OUTOFMEMORY;
1558 if(FAILED(hres)) {
1559 heap_free(case_jmps);
1560 return hres;
1563 i = 0;
1564 for(iter = stat->case_list; iter; iter = iter->next) {
1565 while(iter->next && iter->next->stat == iter->stat) {
1566 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1567 iter = iter->next;
1570 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1572 if(iter->stat) {
1573 for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter);
1574 stat_iter = stat_iter->next) {
1575 hres = compile_statement(ctx, &stat_ctx, stat_iter);
1576 if(FAILED(hres))
1577 break;
1579 if(stat_iter->next && !push_instr(ctx, OP_pop)) {
1580 hres = E_OUTOFMEMORY;
1581 break;
1584 if(FAILED(hres))
1585 break;
1586 }else {
1587 if(!push_instr(ctx, OP_undefined)) {
1588 hres = E_OUTOFMEMORY;
1589 break;
1594 heap_free(case_jmps);
1595 if(FAILED(hres))
1596 return hres;
1597 assert(i == case_cnt);
1599 if(!have_default) {
1600 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.break_label);
1601 if(FAILED(hres))
1602 return hres;
1603 set_arg_uint(ctx, default_jmp, ctx->code_off);
1604 if(!push_instr(ctx, OP_undefined))
1605 return E_OUTOFMEMORY;
1608 label_set_addr(ctx, stat_ctx.break_label);
1609 return S_OK;
1612 /* ECMA-262 3rd Edition 12.13 */
1613 static HRESULT compile_throw_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1615 HRESULT hres;
1617 hres = compile_expression(ctx, stat->expr);
1618 if(FAILED(hres))
1619 return hres;
1621 return push_instr(ctx, OP_throw) ? S_OK : E_OUTOFMEMORY;
1624 /* ECMA-262 3rd Edition 12.14 */
1625 static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat)
1627 statement_ctx_t try_ctx = {0, FALSE, TRUE}, catch_ctx = {0, TRUE, FALSE};
1628 statement_ctx_t finally_ctx = {2, FALSE, FALSE};
1629 unsigned push_except;
1630 BSTR ident;
1631 HRESULT hres;
1633 push_except = push_instr(ctx, OP_push_except);
1634 if(!push_except)
1635 return E_OUTOFMEMORY;
1637 if(stat->catch_block) {
1638 ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier);
1639 if(!ident)
1640 return E_OUTOFMEMORY;
1641 }else {
1642 ident = NULL;
1645 instr_ptr(ctx, push_except)->u.arg[1].bstr = ident;
1647 if(!stat->catch_block)
1648 try_ctx.stack_use = 2;
1650 hres = compile_statement(ctx, &try_ctx, stat->try_statement);
1651 if(FAILED(hres))
1652 return hres;
1654 if(!push_instr(ctx, OP_pop_except))
1655 return E_OUTOFMEMORY;
1657 if(stat->catch_block) {
1658 unsigned jmp_finally;
1660 jmp_finally = push_instr(ctx, OP_jmp);
1661 if(!jmp_finally)
1662 return E_OUTOFMEMORY;
1664 instr_ptr(ctx, push_except)->u.arg[0].uint = ctx->code_off;
1666 hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement);
1667 if(FAILED(hres))
1668 return hres;
1670 if(!push_instr(ctx, OP_pop_scope))
1671 return E_OUTOFMEMORY;
1673 set_arg_uint(ctx, jmp_finally, ctx->code_off);
1674 }else {
1675 set_arg_uint(ctx, push_except, ctx->code_off);
1678 if(stat->finally_statement) {
1679 /* FIXME: avoid */
1680 if(!push_instr(ctx, OP_pop))
1681 return E_OUTOFMEMORY;
1683 hres = compile_statement(ctx, stat->catch_block ? NULL : &finally_ctx, stat->finally_statement);
1684 if(FAILED(hres))
1685 return hres;
1687 if(!stat->catch_block && !push_instr(ctx, OP_end_finally))
1688 return E_OUTOFMEMORY;
1691 return S_OK;
1694 static HRESULT compile_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1696 HRESULT hres;
1698 if(stat_ctx) {
1699 stat_ctx->next = ctx->stat_ctx;
1700 ctx->stat_ctx = stat_ctx;
1703 switch(stat->type) {
1704 case STAT_BLOCK:
1705 hres = compile_block_statement(ctx, ((block_statement_t*)stat)->stat_list);
1706 break;
1707 case STAT_BREAK:
1708 hres = compile_break_statement(ctx, (branch_statement_t*)stat);
1709 break;
1710 case STAT_CONTINUE:
1711 hres = compile_continue_statement(ctx, (branch_statement_t*)stat);
1712 break;
1713 case STAT_EMPTY:
1714 hres = push_instr(ctx, OP_undefined) ? S_OK : E_OUTOFMEMORY; /* FIXME */
1715 break;
1716 case STAT_EXPR:
1717 hres = compile_expression_statement(ctx, (expression_statement_t*)stat);
1718 break;
1719 case STAT_FOR:
1720 hres = compile_for_statement(ctx, (for_statement_t*)stat);
1721 break;
1722 case STAT_FORIN:
1723 hres = compile_forin_statement(ctx, (forin_statement_t*)stat);
1724 break;
1725 case STAT_IF:
1726 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1727 break;
1728 case STAT_LABEL:
1729 hres = compile_labelled_statement(ctx, (labelled_statement_t*)stat);
1730 break;
1731 case STAT_RETURN:
1732 hres = compile_return_statement(ctx, (expression_statement_t*)stat);
1733 break;
1734 case STAT_SWITCH:
1735 hres = compile_switch_statement(ctx, (switch_statement_t*)stat);
1736 break;
1737 case STAT_THROW:
1738 hres = compile_throw_statement(ctx, (expression_statement_t*)stat);
1739 break;
1740 case STAT_TRY:
1741 hres = compile_try_statement(ctx, (try_statement_t*)stat);
1742 break;
1743 case STAT_VAR:
1744 hres = compile_var_statement(ctx, (var_statement_t*)stat);
1745 break;
1746 case STAT_WHILE:
1747 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1748 break;
1749 case STAT_WITH:
1750 hres = compile_with_statement(ctx, (with_statement_t*)stat);
1751 break;
1752 default:
1753 assert(0);
1754 hres = E_FAIL;
1757 if(stat_ctx) {
1758 assert(ctx->stat_ctx == stat_ctx);
1759 ctx->stat_ctx = stat_ctx->next;
1762 return hres;
1765 static void resolve_labels(compiler_ctx_t *ctx, unsigned off)
1767 instr_t *instr;
1769 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
1770 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->u.arg->uint & LABEL_FLAG)) {
1771 assert((instr->u.arg->uint & ~LABEL_FLAG) < ctx->labels_cnt);
1772 instr->u.arg->uint = ctx->labels[instr->u.arg->uint & ~LABEL_FLAG];
1774 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
1777 ctx->labels_cnt = 0;
1780 void release_bytecode(bytecode_t *code)
1782 unsigned i;
1784 if(--code->ref)
1785 return;
1787 for(i=0; i < code->bstr_cnt; i++)
1788 SysFreeString(code->bstr_pool[i]);
1790 heap_free(code->source);
1791 jsheap_free(&code->heap);
1792 heap_free(code->bstr_pool);
1793 heap_free(code->instrs);
1794 heap_free(code);
1797 static HRESULT init_code(compiler_ctx_t *compiler, const WCHAR *source)
1799 compiler->code = heap_alloc_zero(sizeof(bytecode_t));
1800 if(!compiler->code)
1801 return E_OUTOFMEMORY;
1803 compiler->code->ref = 1;
1804 jsheap_init(&compiler->code->heap);
1806 compiler->code->source = heap_strdupW(source);
1807 if(!compiler->code->source) {
1808 release_bytecode(compiler->code);
1809 return E_OUTOFMEMORY;
1812 compiler->code->instrs = heap_alloc(64 * sizeof(instr_t));
1813 if(!compiler->code->instrs) {
1814 release_bytecode(compiler->code);
1815 return E_OUTOFMEMORY;
1818 compiler->code_size = 64;
1819 compiler->code_off = 1;
1820 return S_OK;
1823 static HRESULT compile_function(compiler_ctx_t *ctx, source_elements_t *source, function_expression_t *func_expr,
1824 BOOL from_eval, function_code_t *func)
1826 variable_declaration_t *var_iter;
1827 function_expression_t *iter;
1828 unsigned off, i;
1829 HRESULT hres;
1831 TRACE("\n");
1833 ctx->var_head = ctx->var_tail = NULL;
1834 ctx->func_head = ctx->func_tail = NULL;
1836 off = ctx->code_off;
1837 ctx->func = func;
1838 hres = compile_block_statement(ctx, source->statement);
1839 if(FAILED(hres))
1840 return hres;
1842 resolve_labels(ctx, off);
1844 if(!from_eval && !push_instr(ctx, OP_pop))
1845 return E_OUTOFMEMORY;
1846 if(!push_instr(ctx, OP_ret))
1847 return E_OUTOFMEMORY;
1849 if(TRACE_ON(jscript_disas))
1850 dump_code(ctx, off);
1852 func->instr_off = off;
1854 if(func_expr && func_expr->identifier) {
1855 func->name = compiler_alloc_bstr(ctx, func_expr->identifier);
1856 if(!func->name)
1857 return E_OUTOFMEMORY;
1860 if(func_expr) {
1861 parameter_t *param_iter;
1863 func->source = func_expr->src_str;
1864 func->source_len = func_expr->src_len;
1866 for(param_iter = func_expr->parameter_list; param_iter; param_iter = param_iter->next)
1867 func->param_cnt++;
1869 func->params = compiler_alloc(ctx->code, func->param_cnt * sizeof(*func->params));
1870 if(!func->params)
1871 return E_OUTOFMEMORY;
1873 for(param_iter = func_expr->parameter_list, i=0; param_iter; param_iter = param_iter->next, i++) {
1874 func->params[i] = compiler_alloc_bstr(ctx, param_iter->identifier);
1875 if(!func->params[i])
1876 return E_OUTOFMEMORY;
1880 func->variables = compiler_alloc(ctx->code, func->var_cnt * sizeof(*func->variables));
1881 if(!func->variables)
1882 return E_OUTOFMEMORY;
1884 for(var_iter = ctx->var_head, i=0; var_iter; var_iter = var_iter->global_next, i++) {
1885 func->variables[i] = compiler_alloc_bstr(ctx, var_iter->identifier);
1886 if(!func->variables[i])
1887 return E_OUTOFMEMORY;
1890 assert(i == func->var_cnt);
1892 func->funcs = compiler_alloc(ctx->code, func->func_cnt * sizeof(*func->funcs));
1893 if(!func->funcs)
1894 return E_OUTOFMEMORY;
1895 memset(func->funcs, 0, func->func_cnt * sizeof(*func->funcs));
1897 for(iter = ctx->func_head, i=0; iter; iter = iter->next, i++) {
1898 hres = compile_function(ctx, iter->source_elements, iter, FALSE, func->funcs+i);
1899 if(FAILED(hres))
1900 return hres;
1903 assert(i == func->func_cnt);
1905 return S_OK;
1908 static HRESULT parse_arguments(compiler_ctx_t *ctx, const WCHAR *args, BSTR *arg_array, unsigned *args_size)
1910 const WCHAR *ptr = args, *ptr2;
1911 unsigned arg_cnt = 0;
1913 while(isspaceW(*ptr))
1914 ptr++;
1915 if(!*ptr) {
1916 if(args_size)
1917 *args_size = 0;
1918 return S_OK;
1921 while(1) {
1922 if(!isalphaW(*ptr) && *ptr != '_') {
1923 FIXME("expected alpha or '_': %s\n", debugstr_w(ptr));
1924 return E_FAIL;
1927 ptr2 = ptr;
1928 while(isalnumW(*ptr) || *ptr == '_')
1929 ptr++;
1931 if(*ptr && *ptr != ',' && !isspaceW(*ptr)) {
1932 FIXME("unexpected har %s\n", debugstr_w(ptr));
1933 return E_FAIL;
1936 if(arg_array) {
1937 arg_array[arg_cnt] = compiler_alloc_bstr_len(ctx, ptr2, ptr-ptr2);
1938 if(!arg_array[arg_cnt])
1939 return E_OUTOFMEMORY;
1941 arg_cnt++;
1943 while(isspaceW(*ptr))
1944 ptr++;
1945 if(!*ptr)
1946 break;
1947 if(*ptr != ',') {
1948 FIXME("expected ',': %s\n", debugstr_w(ptr));
1949 return E_FAIL;
1952 ptr++;
1953 while(isspaceW(*ptr))
1954 ptr++;
1957 if(args_size)
1958 *args_size = arg_cnt;
1959 return S_OK;
1962 static HRESULT compile_arguments(compiler_ctx_t *ctx, const WCHAR *args)
1964 HRESULT hres;
1966 hres = parse_arguments(ctx, args, NULL, &ctx->code->global_code.param_cnt);
1967 if(FAILED(hres))
1968 return hres;
1970 ctx->code->global_code.params = compiler_alloc(ctx->code,
1971 ctx->code->global_code.param_cnt * sizeof(*ctx->code->global_code.params));
1972 if(!ctx->code->global_code.params)
1973 return E_OUTOFMEMORY;
1975 return parse_arguments(ctx, args, ctx->code->global_code.params, NULL);
1978 HRESULT compile_script(script_ctx_t *ctx, const WCHAR *code, const WCHAR *args, const WCHAR *delimiter,
1979 BOOL from_eval, BOOL use_decode, bytecode_t **ret)
1981 compiler_ctx_t compiler = {0};
1982 HRESULT hres;
1984 hres = init_code(&compiler, code);
1985 if(FAILED(hres))
1986 return hres;
1988 if(args) {
1989 hres = compile_arguments(&compiler, args);
1990 if(FAILED(hres))
1991 return hres;
1994 if(use_decode) {
1995 hres = decode_source(compiler.code->source);
1996 if(FAILED(hres)) {
1997 WARN("Decoding failed\n");
1998 return hres;
2002 hres = script_parse(ctx, compiler.code->source, delimiter, from_eval, &compiler.parser);
2003 if(FAILED(hres)) {
2004 release_bytecode(compiler.code);
2005 return hres;
2008 hres = compile_function(&compiler, compiler.parser->source, NULL, from_eval, &compiler.code->global_code);
2009 parser_release(compiler.parser);
2010 if(FAILED(hres)) {
2011 release_bytecode(compiler.code);
2012 return hres;
2015 *ret = compiler.code;
2016 return S_OK;