kernel32: Consolidate some duplicate variable declarations.
[wine/multimedia.git] / dlls / jscript / compile.c
blobd332087768ac3a5fea7dd5f12b99133bfed8d3ef
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 BOOL from_eval;
49 unsigned code_off;
50 unsigned code_size;
52 unsigned *labels;
53 unsigned labels_size;
54 unsigned labels_cnt;
56 statement_ctx_t *stat_ctx;
57 function_code_t *func;
59 variable_declaration_t *var_head;
60 variable_declaration_t *var_tail;
62 function_expression_t *func_head;
63 function_expression_t *func_tail;
64 } compiler_ctx_t;
66 static const struct {
67 const char *op_str;
68 instr_arg_type_t arg1_type;
69 instr_arg_type_t arg2_type;
70 } instr_info[] = {
71 #define X(n,a,b,c) {#n,b,c},
72 OP_LIST
73 #undef X
76 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
78 switch(type) {
79 case ARG_STR:
80 TRACE_(jscript_disas)("\t%s", debugstr_jsstr(arg->str));
81 break;
82 case ARG_BSTR:
83 TRACE_(jscript_disas)("\t%s", debugstr_wn(arg->bstr, SysStringLen(arg->bstr)));
84 break;
85 case ARG_INT:
86 TRACE_(jscript_disas)("\t%d", arg->uint);
87 break;
88 case ARG_UINT:
89 case ARG_ADDR:
90 TRACE_(jscript_disas)("\t%u", arg->uint);
91 break;
92 case ARG_FUNC:
93 case ARG_NONE:
94 break;
95 DEFAULT_UNREACHABLE;
99 static void dump_code(compiler_ctx_t *ctx, unsigned off)
101 instr_t *instr;
103 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
104 TRACE_(jscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
105 if(instr_info[instr->op].arg1_type == ARG_DBL) {
106 TRACE_(jscript_disas)("\t%lf", instr->u.dbl);
107 }else {
108 dump_instr_arg(instr_info[instr->op].arg1_type, instr->u.arg);
109 dump_instr_arg(instr_info[instr->op].arg2_type, instr->u.arg+1);
111 TRACE_(jscript_disas)("\n");
115 static HRESULT compile_expression(compiler_ctx_t*,expression_t*,BOOL);
116 static HRESULT compile_statement(compiler_ctx_t*,statement_ctx_t*,statement_t*);
118 static inline void *compiler_alloc(bytecode_t *code, size_t size)
120 return heap_pool_alloc(&code->heap, size);
123 static jsstr_t *compiler_alloc_string_len(compiler_ctx_t *ctx, const WCHAR *str, unsigned len)
125 jsstr_t *new_str;
127 if(!ctx->code->str_pool_size) {
128 ctx->code->str_pool = heap_alloc(8 * sizeof(jsstr_t*));
129 if(!ctx->code->str_pool)
130 return NULL;
131 ctx->code->str_pool_size = 8;
132 }else if(ctx->code->str_pool_size == ctx->code->str_cnt) {
133 jsstr_t **new_pool;
135 new_pool = heap_realloc(ctx->code->str_pool, ctx->code->str_pool_size*2*sizeof(jsstr_t*));
136 if(!new_pool)
137 return NULL;
139 ctx->code->str_pool = new_pool;
140 ctx->code->str_pool_size *= 2;
143 new_str = jsstr_alloc_len(str, len);
144 if(!new_str)
145 return NULL;
147 ctx->code->str_pool[ctx->code->str_cnt++] = new_str;
148 return new_str;
151 static jsstr_t *compiler_alloc_string(compiler_ctx_t *ctx, const WCHAR *str)
153 return compiler_alloc_string_len(ctx, str, strlenW(str));
156 static BOOL ensure_bstr_slot(compiler_ctx_t *ctx)
158 if(!ctx->code->bstr_pool_size) {
159 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
160 if(!ctx->code->bstr_pool)
161 return FALSE;
162 ctx->code->bstr_pool_size = 8;
163 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
164 BSTR *new_pool;
166 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
167 if(!new_pool)
168 return FALSE;
170 ctx->code->bstr_pool = new_pool;
171 ctx->code->bstr_pool_size *= 2;
174 return TRUE;
177 static BSTR compiler_alloc_bstr(compiler_ctx_t *ctx, const WCHAR *str)
179 if(!ensure_bstr_slot(ctx))
180 return NULL;
182 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
183 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
184 return NULL;
186 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
189 static BSTR compiler_alloc_bstr_len(compiler_ctx_t *ctx, const WCHAR *str, size_t len)
191 if(!ensure_bstr_slot(ctx))
192 return NULL;
194 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocStringLen(str, len);
195 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
196 return NULL;
198 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
201 static unsigned push_instr(compiler_ctx_t *ctx, jsop_t op)
203 assert(ctx->code_size >= ctx->code_off);
205 if(ctx->code_size == ctx->code_off) {
206 instr_t *new_instrs;
208 new_instrs = heap_realloc(ctx->code->instrs, ctx->code_size*2*sizeof(instr_t));
209 if(!new_instrs)
210 return 0;
212 ctx->code->instrs = new_instrs;
213 ctx->code_size *= 2;
216 ctx->code->instrs[ctx->code_off].op = op;
217 return ctx->code_off++;
220 static inline instr_t *instr_ptr(compiler_ctx_t *ctx, unsigned off)
222 assert(off < ctx->code_off);
223 return ctx->code->instrs + off;
226 static HRESULT push_instr_int(compiler_ctx_t *ctx, jsop_t op, LONG arg)
228 unsigned instr;
230 instr = push_instr(ctx, op);
231 if(!instr)
232 return E_OUTOFMEMORY;
234 instr_ptr(ctx, instr)->u.arg->lng = arg;
235 return S_OK;
238 static HRESULT push_instr_str(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
240 unsigned instr;
241 jsstr_t *str;
243 str = compiler_alloc_string(ctx, arg);
244 if(!str)
245 return E_OUTOFMEMORY;
247 instr = push_instr(ctx, op);
248 if(!instr)
249 return E_OUTOFMEMORY;
251 instr_ptr(ctx, instr)->u.arg->str = str;
252 return S_OK;
255 static HRESULT push_instr_bstr(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
257 unsigned instr;
258 WCHAR *str;
260 str = compiler_alloc_bstr(ctx, arg);
261 if(!str)
262 return E_OUTOFMEMORY;
264 instr = push_instr(ctx, op);
265 if(!instr)
266 return E_OUTOFMEMORY;
268 instr_ptr(ctx, instr)->u.arg->bstr = str;
269 return S_OK;
272 static HRESULT push_instr_bstr_uint(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg1, unsigned arg2)
274 unsigned instr;
275 WCHAR *str;
277 str = compiler_alloc_bstr(ctx, arg1);
278 if(!str)
279 return E_OUTOFMEMORY;
281 instr = push_instr(ctx, op);
282 if(!instr)
283 return E_OUTOFMEMORY;
285 instr_ptr(ctx, instr)->u.arg[0].bstr = str;
286 instr_ptr(ctx, instr)->u.arg[1].uint = arg2;
287 return S_OK;
290 static HRESULT push_instr_uint_str(compiler_ctx_t *ctx, jsop_t op, unsigned arg1, const WCHAR *arg2)
292 unsigned instr;
293 jsstr_t *str;
295 str = compiler_alloc_string(ctx, arg2);
296 if(!str)
297 return E_OUTOFMEMORY;
299 instr = push_instr(ctx, op);
300 if(!instr)
301 return E_OUTOFMEMORY;
303 instr_ptr(ctx, instr)->u.arg[0].uint = arg1;
304 instr_ptr(ctx, instr)->u.arg[1].str = str;
305 return S_OK;
308 static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
310 unsigned instr;
312 instr = push_instr(ctx, op);
313 if(!instr)
314 return E_OUTOFMEMORY;
316 instr_ptr(ctx, instr)->u.dbl = arg;
317 return S_OK;
320 static inline void set_arg_uint(compiler_ctx_t *ctx, unsigned instr, unsigned arg)
322 instr_ptr(ctx, instr)->u.arg->uint = arg;
325 static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
327 unsigned instr;
329 instr = push_instr(ctx, op);
330 if(!instr)
331 return E_OUTOFMEMORY;
333 set_arg_uint(ctx, instr, arg);
334 return S_OK;
337 static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
339 HRESULT hres;
341 hres = compile_expression(ctx, expr->expression1, TRUE);
342 if(FAILED(hres))
343 return hres;
345 hres = compile_expression(ctx, expr->expression2, TRUE);
346 if(FAILED(hres))
347 return hres;
349 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
352 static HRESULT compile_unary_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op)
354 HRESULT hres;
356 hres = compile_expression(ctx, expr->expression, TRUE);
357 if(FAILED(hres))
358 return hres;
360 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
363 /* ECMA-262 3rd Edition 11.2.1 */
364 static HRESULT compile_member_expression(compiler_ctx_t *ctx, member_expression_t *expr)
366 HRESULT hres;
368 hres = compile_expression(ctx, expr->expression, TRUE);
369 if(FAILED(hres))
370 return hres;
372 return push_instr_bstr(ctx, OP_member, expr->identifier);
375 #define LABEL_FLAG 0x80000000
377 static unsigned alloc_label(compiler_ctx_t *ctx)
379 if(!ctx->labels_size) {
380 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
381 if(!ctx->labels)
382 return 0;
383 ctx->labels_size = 8;
384 }else if(ctx->labels_size == ctx->labels_cnt) {
385 unsigned *new_labels;
387 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
388 if(!new_labels)
389 return 0;
391 ctx->labels = new_labels;
392 ctx->labels_size *= 2;
395 return ctx->labels_cnt++ | LABEL_FLAG;
398 static void label_set_addr(compiler_ctx_t *ctx, unsigned label)
400 assert(label & LABEL_FLAG);
401 ctx->labels[label & ~LABEL_FLAG] = ctx->code_off;
404 static inline BOOL is_memberid_expr(expression_type_t type)
406 return type == EXPR_IDENT || type == EXPR_MEMBER || type == EXPR_ARRAY;
409 static HRESULT compile_memberid_expression(compiler_ctx_t *ctx, expression_t *expr, unsigned flags)
411 HRESULT hres = S_OK;
413 switch(expr->type) {
414 case EXPR_IDENT: {
415 identifier_expression_t *ident_expr = (identifier_expression_t*)expr;
417 hres = push_instr_bstr_uint(ctx, OP_identid, ident_expr->identifier, flags);
418 break;
420 case EXPR_ARRAY: {
421 binary_expression_t *array_expr = (binary_expression_t*)expr;
423 hres = compile_expression(ctx, array_expr->expression1, TRUE);
424 if(FAILED(hres))
425 return hres;
427 hres = compile_expression(ctx, array_expr->expression2, TRUE);
428 if(FAILED(hres))
429 return hres;
431 hres = push_instr_uint(ctx, OP_memberid, flags);
432 break;
434 case EXPR_MEMBER: {
435 member_expression_t *member_expr = (member_expression_t*)expr;
437 hres = compile_expression(ctx, member_expr->expression, TRUE);
438 if(FAILED(hres))
439 return hres;
441 /* FIXME: Potential optimization */
442 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
443 if(FAILED(hres))
444 return hres;
446 hres = push_instr_uint(ctx, OP_memberid, flags);
447 break;
449 DEFAULT_UNREACHABLE;
452 return hres;
455 static HRESULT compile_increment_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op, int n)
457 HRESULT hres;
459 if(!is_memberid_expr(expr->expression->type)) {
460 hres = compile_expression(ctx, expr->expression, TRUE);
461 if(FAILED(hres))
462 return hres;
464 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
467 hres = compile_memberid_expression(ctx, expr->expression, fdexNameEnsure);
468 if(FAILED(hres))
469 return hres;
471 return push_instr_int(ctx, op, n);
474 /* ECMA-262 3rd Edition 11.14 */
475 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr, BOOL emit_ret)
477 HRESULT hres;
479 hres = compile_expression(ctx, expr->expression1, FALSE);
480 if(FAILED(hres))
481 return hres;
483 return compile_expression(ctx, expr->expression2, emit_ret);
486 /* ECMA-262 3rd Edition 11.11 */
487 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
489 unsigned instr;
490 HRESULT hres;
492 hres = compile_expression(ctx, expr->expression1, TRUE);
493 if(FAILED(hres))
494 return hres;
496 instr = push_instr(ctx, op);
497 if(!instr)
498 return E_OUTOFMEMORY;
500 hres = compile_expression(ctx, expr->expression2, TRUE);
501 if(FAILED(hres))
502 return hres;
504 set_arg_uint(ctx, instr, ctx->code_off);
505 return S_OK;
508 /* ECMA-262 3rd Edition 11.12 */
509 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
511 unsigned jmp_false, jmp_end;
512 HRESULT hres;
514 hres = compile_expression(ctx, expr->expression, TRUE);
515 if(FAILED(hres))
516 return hres;
518 jmp_false = push_instr(ctx, OP_cnd_z);
519 if(!jmp_false)
520 return E_OUTOFMEMORY;
522 hres = compile_expression(ctx, expr->true_expression, TRUE);
523 if(FAILED(hres))
524 return hres;
526 jmp_end = push_instr(ctx, OP_jmp);
527 if(!jmp_end)
528 return E_OUTOFMEMORY;
530 set_arg_uint(ctx, jmp_false, ctx->code_off);
531 hres = push_instr_uint(ctx, OP_pop, 1);
532 if(FAILED(hres))
533 return hres;
535 hres = compile_expression(ctx, expr->false_expression, TRUE);
536 if(FAILED(hres))
537 return hres;
539 set_arg_uint(ctx, jmp_end, ctx->code_off);
540 return S_OK;
543 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
545 unsigned arg_cnt = 0;
546 argument_t *arg;
547 HRESULT hres;
549 hres = compile_expression(ctx, expr->expression, TRUE);
550 if(FAILED(hres))
551 return hres;
553 for(arg = expr->argument_list; arg; arg = arg->next) {
554 hres = compile_expression(ctx, arg->expr, TRUE);
555 if(FAILED(hres))
556 return hres;
557 arg_cnt++;
560 return push_instr_uint(ctx, OP_new, arg_cnt);
563 static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL emit_ret)
565 unsigned arg_cnt = 0;
566 argument_t *arg;
567 unsigned instr;
568 jsop_t op;
569 HRESULT hres;
571 if(is_memberid_expr(expr->expression->type)) {
572 op = OP_call_member;
573 hres = compile_memberid_expression(ctx, expr->expression, 0);
574 }else {
575 op = OP_call;
576 hres = compile_expression(ctx, expr->expression, TRUE);
579 if(FAILED(hres))
580 return hres;
582 for(arg = expr->argument_list; arg; arg = arg->next) {
583 hres = compile_expression(ctx, arg->expr, TRUE);
584 if(FAILED(hres))
585 return hres;
586 arg_cnt++;
589 instr = push_instr(ctx, op);
590 if(!instr)
591 return E_OUTOFMEMORY;
593 instr_ptr(ctx, instr)->u.arg[0].uint = arg_cnt;
594 instr_ptr(ctx, instr)->u.arg[1].lng = emit_ret;
595 return S_OK;
598 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
600 HRESULT hres;
602 switch(expr->expression->type) {
603 case EXPR_ARRAY: {
604 binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
606 hres = compile_expression(ctx, array_expr->expression1, TRUE);
607 if(FAILED(hres))
608 return hres;
610 hres = compile_expression(ctx, array_expr->expression2, TRUE);
611 if(FAILED(hres))
612 return hres;
614 if(!push_instr(ctx, OP_delete))
615 return E_OUTOFMEMORY;
616 break;
618 case EXPR_MEMBER: {
619 member_expression_t *member_expr = (member_expression_t*)expr->expression;
621 hres = compile_expression(ctx, member_expr->expression, TRUE);
622 if(FAILED(hres))
623 return hres;
625 /* FIXME: Potential optimization */
626 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
627 if(FAILED(hres))
628 return hres;
630 if(!push_instr(ctx, OP_delete))
631 return E_OUTOFMEMORY;
632 break;
634 case EXPR_IDENT:
635 return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
636 default: {
637 const WCHAR fixmeW[] = {'F','I','X','M','E',0};
639 WARN("invalid delete, unimplemented exception message\n");
641 hres = compile_expression(ctx, expr->expression, TRUE);
642 if(FAILED(hres))
643 return hres;
645 return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, fixmeW);
649 return S_OK;
652 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
654 BOOL use_throw_path = FALSE;
655 unsigned arg_cnt = 0;
656 HRESULT hres;
658 if(expr->expression1->type == EXPR_CALL) {
659 call_expression_t *call_expr = (call_expression_t*)expr->expression1;
660 argument_t *arg;
662 if(op != OP_LAST) {
663 FIXME("op %d not supported on parametrized assign expressions\n", op);
664 return E_NOTIMPL;
667 if(is_memberid_expr(call_expr->expression->type) && call_expr->argument_list) {
668 hres = compile_memberid_expression(ctx, call_expr->expression, fdexNameEnsure);
669 if(FAILED(hres))
670 return hres;
672 for(arg = call_expr->argument_list; arg; arg = arg->next) {
673 hres = compile_expression(ctx, arg->expr, TRUE);
674 if(FAILED(hres))
675 return hres;
676 arg_cnt++;
678 }else {
679 use_throw_path = TRUE;
681 }else if(is_memberid_expr(expr->expression1->type)) {
682 hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
683 if(FAILED(hres))
684 return hres;
685 }else {
686 use_throw_path = TRUE;
689 if(use_throw_path) {
690 /* Illegal assignment: evaluate and throw */
691 hres = compile_expression(ctx, expr->expression1, TRUE);
692 if(FAILED(hres))
693 return hres;
695 hres = compile_expression(ctx, expr->expression2, TRUE);
696 if(FAILED(hres))
697 return hres;
699 if(op != OP_LAST && !push_instr(ctx, op))
700 return E_OUTOFMEMORY;
702 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
705 if(op != OP_LAST && !push_instr(ctx, OP_refval))
706 return E_OUTOFMEMORY;
708 hres = compile_expression(ctx, expr->expression2, TRUE);
709 if(FAILED(hres))
710 return hres;
712 if(op != OP_LAST && !push_instr(ctx, op))
713 return E_OUTOFMEMORY;
715 if(arg_cnt)
716 return push_instr_uint(ctx, OP_assign_call, arg_cnt);
718 if(!push_instr(ctx, OP_assign))
719 return E_OUTOFMEMORY;
721 return S_OK;
724 static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
726 jsop_t op;
727 HRESULT hres;
729 if(is_memberid_expr(expr->expression->type)) {
730 if(expr->expression->type == EXPR_IDENT)
731 return push_instr_bstr(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
733 op = OP_typeofid;
734 hres = compile_memberid_expression(ctx, expr->expression, 0);
735 }else {
736 op = OP_typeof;
737 hres = compile_expression(ctx, expr->expression, TRUE);
739 if(FAILED(hres))
740 return hres;
742 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
745 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
747 switch(literal->type) {
748 case LT_BOOL:
749 return push_instr_int(ctx, OP_bool, literal->u.bval);
750 case LT_DOUBLE:
751 return push_instr_double(ctx, OP_double, literal->u.dval);
752 case LT_NULL:
753 return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
754 case LT_STRING:
755 return push_instr_str(ctx, OP_str, literal->u.wstr);
756 case LT_REGEXP: {
757 unsigned instr;
758 jsstr_t *str;
760 str = compiler_alloc_string_len(ctx, literal->u.regexp.str, literal->u.regexp.str_len);
761 if(!str)
762 return E_OUTOFMEMORY;
764 instr = push_instr(ctx, OP_regexp);
765 if(!instr)
766 return E_OUTOFMEMORY;
768 instr_ptr(ctx, instr)->u.arg[0].str = str;
769 instr_ptr(ctx, instr)->u.arg[1].uint = literal->u.regexp.flags;
770 return S_OK;
772 DEFAULT_UNREACHABLE;
774 return E_FAIL;
777 static HRESULT literal_as_bstr(compiler_ctx_t *ctx, literal_t *literal, BSTR *str)
779 switch(literal->type) {
780 case LT_STRING:
781 *str = compiler_alloc_bstr(ctx, literal->u.wstr);
782 break;
783 case LT_DOUBLE: {
784 jsstr_t *jsstr;
785 HRESULT hres;
787 hres = double_to_string(literal->u.dval, &jsstr);
788 if(FAILED(hres))
789 return hres;
791 *str = compiler_alloc_bstr_len(ctx, NULL, jsstr_length(jsstr));
792 if(*str)
793 jsstr_flush(jsstr, *str);
794 jsstr_release(jsstr);
795 break;
797 DEFAULT_UNREACHABLE;
800 return *str ? S_OK : E_OUTOFMEMORY;
803 static HRESULT compile_array_literal(compiler_ctx_t *ctx, array_literal_expression_t *expr)
805 unsigned i, elem_cnt = expr->length;
806 array_element_t *iter;
807 HRESULT hres;
809 for(iter = expr->element_list; iter; iter = iter->next) {
810 elem_cnt += iter->elision+1;
812 for(i=0; i < iter->elision; i++) {
813 if(!push_instr(ctx, OP_undefined))
814 return E_OUTOFMEMORY;
817 hres = compile_expression(ctx, iter->expr, TRUE);
818 if(FAILED(hres))
819 return hres;
822 for(i=0; i < expr->length; i++) {
823 if(!push_instr(ctx, OP_undefined))
824 return E_OUTOFMEMORY;
827 return push_instr_uint(ctx, OP_carray, elem_cnt);
830 static HRESULT compile_object_literal(compiler_ctx_t *ctx, property_value_expression_t *expr)
832 prop_val_t *iter;
833 unsigned instr;
834 BSTR name;
835 HRESULT hres;
837 if(!push_instr(ctx, OP_new_obj))
838 return E_OUTOFMEMORY;
840 for(iter = expr->property_list; iter; iter = iter->next) {
841 hres = literal_as_bstr(ctx, iter->name, &name);
842 if(FAILED(hres))
843 return hres;
845 hres = compile_expression(ctx, iter->value, TRUE);
846 if(FAILED(hres))
847 return hres;
849 instr = push_instr(ctx, OP_obj_prop);
850 if(!instr)
851 return E_OUTOFMEMORY;
853 instr_ptr(ctx, instr)->u.arg->bstr = name;
856 return S_OK;
859 static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr)
861 ctx->func_tail = ctx->func_tail ? (ctx->func_tail->next = expr) : (ctx->func_head = expr);
863 /* FIXME: not exactly right */
864 if(expr->identifier) {
865 ctx->func->func_cnt++;
866 return push_instr_bstr(ctx, OP_ident, expr->identifier);
869 return push_instr_uint(ctx, OP_func, ctx->func->func_cnt++);
872 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr, BOOL emit_ret)
874 HRESULT hres;
876 switch(expr->type) {
877 case EXPR_ADD:
878 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
879 break;
880 case EXPR_AND:
881 hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_z);
882 break;
883 case EXPR_ARRAY:
884 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
885 break;
886 case EXPR_ARRAYLIT:
887 hres = compile_array_literal(ctx, (array_literal_expression_t*)expr);
888 break;
889 case EXPR_ASSIGN:
890 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
891 break;
892 case EXPR_ASSIGNADD:
893 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
894 break;
895 case EXPR_ASSIGNAND:
896 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_and);
897 break;
898 case EXPR_ASSIGNSUB:
899 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
900 break;
901 case EXPR_ASSIGNMUL:
902 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
903 break;
904 case EXPR_ASSIGNDIV:
905 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
906 break;
907 case EXPR_ASSIGNMOD:
908 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
909 break;
910 case EXPR_ASSIGNOR:
911 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
912 break;
913 case EXPR_ASSIGNLSHIFT:
914 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_lshift);
915 break;
916 case EXPR_ASSIGNRSHIFT:
917 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift);
918 break;
919 case EXPR_ASSIGNRRSHIFT:
920 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
921 break;
922 case EXPR_ASSIGNXOR:
923 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
924 break;
925 case EXPR_BAND:
926 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
927 break;
928 case EXPR_BITNEG:
929 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
930 break;
931 case EXPR_BOR:
932 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
933 break;
934 case EXPR_CALL:
935 return compile_call_expression(ctx, (call_expression_t*)expr, emit_ret);
936 case EXPR_COMMA:
937 return compile_comma_expression(ctx, (binary_expression_t*)expr, emit_ret);
938 case EXPR_COND:
939 hres = compile_conditional_expression(ctx, (conditional_expression_t*)expr);
940 break;
941 case EXPR_DELETE:
942 hres = compile_delete_expression(ctx, (unary_expression_t*)expr);
943 break;
944 case EXPR_DIV:
945 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
946 break;
947 case EXPR_EQ:
948 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
949 break;
950 case EXPR_EQEQ:
951 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
952 break;
953 case EXPR_FUNC:
954 hres = compile_function_expression(ctx, (function_expression_t*)expr);
955 break;
956 case EXPR_GREATER:
957 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
958 break;
959 case EXPR_GREATEREQ:
960 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
961 break;
962 case EXPR_IDENT:
963 hres = push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
964 break;
965 case EXPR_IN:
966 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
967 break;
968 case EXPR_INSTANCEOF:
969 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
970 break;
971 case EXPR_LESS:
972 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
973 break;
974 case EXPR_LESSEQ:
975 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
976 break;
977 case EXPR_LITERAL:
978 hres = compile_literal(ctx, ((literal_expression_t*)expr)->literal);
979 break;
980 case EXPR_LOGNEG:
981 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
982 break;
983 case EXPR_LSHIFT:
984 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
985 break;
986 case EXPR_MEMBER:
987 hres = compile_member_expression(ctx, (member_expression_t*)expr);
988 break;
989 case EXPR_MINUS:
990 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
991 break;
992 case EXPR_MOD:
993 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
994 break;
995 case EXPR_MUL:
996 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
997 break;
998 case EXPR_NEW:
999 hres = compile_new_expression(ctx, (call_expression_t*)expr);
1000 break;
1001 case EXPR_NOTEQ:
1002 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
1003 break;
1004 case EXPR_NOTEQEQ:
1005 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
1006 break;
1007 case EXPR_OR:
1008 hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_nz);
1009 break;
1010 case EXPR_PLUS:
1011 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
1012 break;
1013 case EXPR_POSTDEC:
1014 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
1015 break;
1016 case EXPR_POSTINC:
1017 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
1018 break;
1019 case EXPR_PREDEC:
1020 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
1021 break;
1022 case EXPR_PREINC:
1023 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
1024 break;
1025 case EXPR_PROPVAL:
1026 hres = compile_object_literal(ctx, (property_value_expression_t*)expr);
1027 break;
1028 case EXPR_RSHIFT:
1029 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
1030 break;
1031 case EXPR_RRSHIFT:
1032 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
1033 break;
1034 case EXPR_SUB:
1035 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
1036 break;
1037 case EXPR_THIS:
1038 return !emit_ret || push_instr(ctx, OP_this) ? S_OK : E_OUTOFMEMORY;
1039 case EXPR_TYPEOF:
1040 hres = compile_typeof_expression(ctx, (unary_expression_t*)expr);
1041 break;
1042 case EXPR_VOID:
1043 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
1044 break;
1045 case EXPR_BXOR:
1046 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
1047 break;
1048 DEFAULT_UNREACHABLE;
1051 if(FAILED(hres))
1052 return hres;
1054 return emit_ret ? S_OK : push_instr_uint(ctx, OP_pop, 1);
1057 static inline BOOL is_loop_statement(statement_type_t type)
1059 return type == STAT_FOR || type == STAT_FORIN || type == STAT_WHILE;
1062 /* ECMA-262 3rd Edition 12.1 */
1063 static HRESULT compile_block_statement(compiler_ctx_t *ctx, statement_t *iter)
1065 HRESULT hres;
1067 while(iter) {
1068 hres = compile_statement(ctx, NULL, iter);
1069 if(FAILED(hres))
1070 return hres;
1072 iter = iter->next;
1075 return S_OK;
1078 /* ECMA-262 3rd Edition 12.2 */
1079 static HRESULT compile_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
1081 variable_declaration_t *iter;
1082 HRESULT hres;
1084 assert(list != NULL);
1086 if(ctx->var_tail)
1087 ctx->var_tail->global_next = list;
1088 else
1089 ctx->var_head = list;
1091 for(iter = list; iter; iter = iter->next) {
1092 ctx->func->var_cnt++;
1093 iter->global_next = iter->next;
1094 if(!iter->next)
1095 ctx->var_tail = iter;
1097 if(!iter->expr)
1098 continue;
1100 hres = compile_expression(ctx, iter->expr, TRUE);
1101 if(FAILED(hres))
1102 return hres;
1104 hres = push_instr_bstr(ctx, OP_var_set, iter->identifier);
1105 if(FAILED(hres))
1106 return hres;
1109 return S_OK;
1112 /* ECMA-262 3rd Edition 12.2 */
1113 static HRESULT compile_var_statement(compiler_ctx_t *ctx, var_statement_t *stat)
1115 return compile_variable_list(ctx, stat->variable_list);
1118 /* ECMA-262 3rd Edition 12.4 */
1119 static HRESULT compile_expression_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1121 HRESULT hres;
1123 hres = compile_expression(ctx, stat->expr, ctx->from_eval);
1124 if(FAILED(hres))
1125 return hres;
1127 return !ctx->from_eval || push_instr(ctx, OP_setret) ? S_OK : E_OUTOFMEMORY;
1130 /* ECMA-262 3rd Edition 12.5 */
1131 static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat)
1133 unsigned jmp_else;
1134 HRESULT hres;
1136 hres = compile_expression(ctx, stat->expr, TRUE);
1137 if(FAILED(hres))
1138 return hres;
1140 jmp_else = push_instr(ctx, OP_jmp_z);
1141 if(!jmp_else)
1142 return E_OUTOFMEMORY;
1144 hres = compile_statement(ctx, NULL, stat->if_stat);
1145 if(FAILED(hres))
1146 return hres;
1148 if(stat->else_stat) {
1149 unsigned jmp_end;
1151 jmp_end = push_instr(ctx, OP_jmp);
1152 if(!jmp_end)
1153 return E_OUTOFMEMORY;
1155 set_arg_uint(ctx, jmp_else, ctx->code_off);
1157 hres = compile_statement(ctx, NULL, stat->else_stat);
1158 if(FAILED(hres))
1159 return hres;
1161 set_arg_uint(ctx, jmp_end, ctx->code_off);
1162 }else {
1163 set_arg_uint(ctx, jmp_else, ctx->code_off);
1166 return S_OK;
1169 /* ECMA-262 3rd Edition 12.6.2 */
1170 static HRESULT compile_while_statement(compiler_ctx_t *ctx, while_statement_t *stat)
1172 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1173 unsigned jmp_off;
1174 HRESULT hres;
1176 stat_ctx.break_label = alloc_label(ctx);
1177 if(!stat_ctx.break_label)
1178 return E_OUTOFMEMORY;
1180 stat_ctx.continue_label = alloc_label(ctx);
1181 if(!stat_ctx.continue_label)
1182 return E_OUTOFMEMORY;
1184 jmp_off = ctx->code_off;
1186 if(!stat->do_while) {
1187 label_set_addr(ctx, stat_ctx.continue_label);
1188 hres = compile_expression(ctx, stat->expr, TRUE);
1189 if(FAILED(hres))
1190 return hres;
1192 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1193 if(FAILED(hres))
1194 return hres;
1197 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1198 if(FAILED(hres))
1199 return hres;
1201 if(stat->do_while) {
1202 label_set_addr(ctx, stat_ctx.continue_label);
1203 hres = compile_expression(ctx, stat->expr, TRUE);
1204 if(FAILED(hres))
1205 return hres;
1207 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1208 if(FAILED(hres))
1209 return hres;
1212 hres = push_instr_uint(ctx, OP_jmp, jmp_off);
1213 if(FAILED(hres))
1214 return hres;
1216 label_set_addr(ctx, stat_ctx.break_label);
1217 return S_OK;
1220 /* ECMA-262 3rd Edition 12.6.3 */
1221 static HRESULT compile_for_statement(compiler_ctx_t *ctx, for_statement_t *stat)
1223 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1224 unsigned expr_off;
1225 HRESULT hres;
1227 if(stat->variable_list) {
1228 hres = compile_variable_list(ctx, stat->variable_list);
1229 if(FAILED(hres))
1230 return hres;
1231 }else if(stat->begin_expr) {
1232 hres = compile_expression(ctx, stat->begin_expr, FALSE);
1233 if(FAILED(hres))
1234 return hres;
1237 stat_ctx.break_label = alloc_label(ctx);
1238 if(!stat_ctx.break_label)
1239 return E_OUTOFMEMORY;
1241 stat_ctx.continue_label = alloc_label(ctx);
1242 if(!stat_ctx.continue_label)
1243 return E_OUTOFMEMORY;
1245 expr_off = ctx->code_off;
1247 if(stat->expr) {
1248 hres = compile_expression(ctx, stat->expr, TRUE);
1249 if(FAILED(hres))
1250 return hres;
1252 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1253 if(FAILED(hres))
1254 return hres;
1257 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1258 if(FAILED(hres))
1259 return hres;
1261 label_set_addr(ctx, stat_ctx.continue_label);
1263 if(stat->end_expr) {
1264 hres = compile_expression(ctx, stat->end_expr, FALSE);
1265 if(FAILED(hres))
1266 return hres;
1269 hres = push_instr_uint(ctx, OP_jmp, expr_off);
1270 if(FAILED(hres))
1271 return hres;
1273 label_set_addr(ctx, stat_ctx.break_label);
1274 return S_OK;
1277 /* ECMA-262 3rd Edition 12.6.4 */
1278 static HRESULT compile_forin_statement(compiler_ctx_t *ctx, forin_statement_t *stat)
1280 statement_ctx_t stat_ctx = {4, FALSE, FALSE};
1281 HRESULT hres;
1283 if(stat->variable) {
1284 hres = compile_variable_list(ctx, stat->variable);
1285 if(FAILED(hres))
1286 return hres;
1289 stat_ctx.break_label = alloc_label(ctx);
1290 if(!stat_ctx.break_label)
1291 return E_OUTOFMEMORY;
1293 stat_ctx.continue_label = alloc_label(ctx);
1294 if(!stat_ctx.continue_label)
1295 return E_OUTOFMEMORY;
1297 hres = compile_expression(ctx, stat->in_expr, TRUE);
1298 if(FAILED(hres))
1299 return hres;
1301 if(stat->variable) {
1302 hres = push_instr_bstr_uint(ctx, OP_identid, stat->variable->identifier, fdexNameEnsure);
1303 if(FAILED(hres))
1304 return hres;
1305 }else if(is_memberid_expr(stat->expr->type)) {
1306 hres = compile_memberid_expression(ctx, stat->expr, fdexNameEnsure);
1307 if(FAILED(hres))
1308 return hres;
1309 }else {
1310 hres = push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
1311 if(FAILED(hres))
1312 return hres;
1314 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1315 return S_OK;
1318 hres = push_instr_int(ctx, OP_int, DISPID_STARTENUM);
1319 if(FAILED(hres))
1320 return hres;
1322 label_set_addr(ctx, stat_ctx.continue_label);
1323 hres = push_instr_uint(ctx, OP_forin, stat_ctx.break_label);
1324 if(FAILED(hres))
1325 return E_OUTOFMEMORY;
1327 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1328 if(FAILED(hres))
1329 return hres;
1331 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.continue_label);
1332 if(FAILED(hres))
1333 return hres;
1335 label_set_addr(ctx, stat_ctx.break_label);
1336 return S_OK;
1339 static HRESULT pop_to_stat(compiler_ctx_t *ctx, BOOL var_stack, BOOL scope_stack, statement_ctx_t *stat_ctx)
1341 unsigned stack_pop = 0;
1342 statement_ctx_t *iter;
1344 for(iter = ctx->stat_ctx; iter != stat_ctx; iter = iter->next) {
1345 if(scope_stack) {
1346 if(iter->using_scope && !push_instr(ctx, OP_pop_scope))
1347 return E_OUTOFMEMORY;
1348 if(iter->using_except && !push_instr(ctx, OP_pop_except))
1349 return E_OUTOFMEMORY;
1351 stack_pop += iter->stack_use;
1354 if(var_stack && stack_pop) {
1355 HRESULT hres;
1357 hres = push_instr_uint(ctx, OP_pop, stack_pop);
1358 if(FAILED(hres))
1359 return hres;
1362 return S_OK;
1365 /* ECMA-262 3rd Edition 12.7 */
1366 static HRESULT compile_continue_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1368 statement_ctx_t *pop_ctx;
1369 HRESULT hres;
1371 if(stat->identifier) {
1372 statement_t *label_stat;
1373 statement_ctx_t *iter;
1375 pop_ctx = NULL;
1377 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1378 if(iter->continue_label)
1379 pop_ctx = iter;
1380 if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier))
1381 break;
1384 if(!iter) {
1385 WARN("Label not found\n");
1386 return JS_E_LABEL_NOT_FOUND;
1389 /* Labelled continue are allowed only on loops */
1390 for(label_stat = iter->labelled_stat->statement;
1391 label_stat->type == STAT_LABEL;
1392 label_stat = ((labelled_statement_t*)label_stat)->statement);
1393 if(!is_loop_statement(label_stat->type)) {
1394 WARN("Label is not a loop\n");
1395 return JS_E_INVALID_CONTINUE;
1398 assert(pop_ctx != NULL);
1399 }else {
1400 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1401 if(pop_ctx->continue_label)
1402 break;
1405 if(!pop_ctx) {
1406 WARN("continue outside loop\n");
1407 return JS_E_INVALID_CONTINUE;
1411 hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx);
1412 if(FAILED(hres))
1413 return hres;
1415 return push_instr_uint(ctx, OP_jmp, pop_ctx->continue_label);
1418 /* ECMA-262 3rd Edition 12.8 */
1419 static HRESULT compile_break_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1421 statement_ctx_t *pop_ctx;
1422 HRESULT hres;
1424 if(stat->identifier) {
1425 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1426 if(pop_ctx->labelled_stat && !strcmpW(pop_ctx->labelled_stat->identifier, stat->identifier)) {
1427 assert(pop_ctx->break_label);
1428 break;
1432 if(!pop_ctx) {
1433 WARN("Label not found\n");
1434 return JS_E_LABEL_NOT_FOUND;
1436 }else {
1437 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1438 if(pop_ctx->break_label && !pop_ctx->labelled_stat)
1439 break;
1442 if(!pop_ctx) {
1443 WARN("Break outside loop\n");
1444 return JS_E_INVALID_BREAK;
1448 hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx->next);
1449 if(FAILED(hres))
1450 return hres;
1452 return push_instr_uint(ctx, OP_jmp, pop_ctx->break_label);
1455 /* ECMA-262 3rd Edition 12.9 */
1456 static HRESULT compile_return_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1458 HRESULT hres;
1460 if(ctx->from_eval) {
1461 WARN("misplaced return statement\n");
1462 return JS_E_MISPLACED_RETURN;
1465 hres = pop_to_stat(ctx, TRUE, FALSE, NULL);
1466 if(FAILED(hres))
1467 return hres;
1469 if(stat->expr) {
1470 hres = compile_expression(ctx, stat->expr, TRUE);
1471 if(FAILED(hres))
1472 return hres;
1473 if(!push_instr(ctx, OP_setret))
1474 return E_OUTOFMEMORY;
1477 hres = pop_to_stat(ctx, FALSE, TRUE, NULL);
1478 if(FAILED(hres))
1479 return hres;
1481 return push_instr(ctx, OP_ret) ? S_OK : E_OUTOFMEMORY;
1484 /* ECMA-262 3rd Edition 12.10 */
1485 static HRESULT compile_with_statement(compiler_ctx_t *ctx, with_statement_t *stat)
1487 statement_ctx_t stat_ctx = {0, TRUE, FALSE};
1488 HRESULT hres;
1490 hres = compile_expression(ctx, stat->expr, TRUE);
1491 if(FAILED(hres))
1492 return hres;
1494 if(!push_instr(ctx, OP_push_scope))
1495 return E_OUTOFMEMORY;
1497 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1498 if(FAILED(hres))
1499 return hres;
1501 if(!push_instr(ctx, OP_pop_scope))
1502 return E_OUTOFMEMORY;
1504 return S_OK;
1507 /* ECMA-262 3rd Edition 12.10 */
1508 static HRESULT compile_labelled_statement(compiler_ctx_t *ctx, labelled_statement_t *stat)
1510 statement_ctx_t stat_ctx = {0, FALSE, FALSE, 0, 0, stat}, *iter;
1511 HRESULT hres;
1513 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1514 if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier)) {
1515 WARN("Label %s redefined\n", debugstr_w(stat->identifier));
1516 return JS_E_LABEL_REDEFINED;
1520 /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1521 stat_ctx.break_label = alloc_label(ctx);
1522 if(!stat_ctx.break_label)
1523 return E_OUTOFMEMORY;
1525 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1526 if(FAILED(hres))
1527 return hres;
1529 label_set_addr(ctx, stat_ctx.break_label);
1530 return S_OK;
1533 /* ECMA-262 3rd Edition 12.13 */
1534 static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat)
1536 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1537 unsigned case_cnt = 0, *case_jmps, i, default_jmp;
1538 BOOL have_default = FALSE;
1539 statement_t *stat_iter;
1540 case_clausule_t *iter;
1541 HRESULT hres;
1543 hres = compile_expression(ctx, stat->expr, TRUE);
1544 if(FAILED(hres))
1545 return hres;
1547 stat_ctx.break_label = alloc_label(ctx);
1548 if(!stat_ctx.break_label)
1549 return E_OUTOFMEMORY;
1551 for(iter = stat->case_list; iter; iter = iter->next) {
1552 if(iter->expr)
1553 case_cnt++;
1556 case_jmps = heap_alloc(case_cnt * sizeof(*case_jmps));
1557 if(!case_jmps)
1558 return E_OUTOFMEMORY;
1560 i = 0;
1561 for(iter = stat->case_list; iter; iter = iter->next) {
1562 if(!iter->expr) {
1563 have_default = TRUE;
1564 continue;
1567 hres = compile_expression(ctx, iter->expr, TRUE);
1568 if(FAILED(hres))
1569 break;
1571 case_jmps[i] = push_instr(ctx, OP_case);
1572 if(!case_jmps[i]) {
1573 hres = E_OUTOFMEMORY;
1574 break;
1576 i++;
1579 if(SUCCEEDED(hres)) {
1580 hres = push_instr_uint(ctx, OP_pop, 1);
1581 if(SUCCEEDED(hres)) {
1582 default_jmp = push_instr(ctx, OP_jmp);
1583 if(!default_jmp)
1584 hres = E_OUTOFMEMORY;
1588 if(FAILED(hres)) {
1589 heap_free(case_jmps);
1590 return hres;
1593 i = 0;
1594 for(iter = stat->case_list; iter; iter = iter->next) {
1595 while(iter->next && iter->next->stat == iter->stat) {
1596 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1597 iter = iter->next;
1600 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1602 for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter);
1603 stat_iter = stat_iter->next) {
1604 hres = compile_statement(ctx, &stat_ctx, stat_iter);
1605 if(FAILED(hres))
1606 break;
1608 if(FAILED(hres))
1609 break;
1612 heap_free(case_jmps);
1613 if(FAILED(hres))
1614 return hres;
1615 assert(i == case_cnt);
1617 if(!have_default) {
1618 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.break_label);
1619 if(FAILED(hres))
1620 return hres;
1621 set_arg_uint(ctx, default_jmp, ctx->code_off);
1624 label_set_addr(ctx, stat_ctx.break_label);
1625 return S_OK;
1628 /* ECMA-262 3rd Edition 12.13 */
1629 static HRESULT compile_throw_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1631 HRESULT hres;
1633 hres = compile_expression(ctx, stat->expr, TRUE);
1634 if(FAILED(hres))
1635 return hres;
1637 return push_instr(ctx, OP_throw) ? S_OK : E_OUTOFMEMORY;
1640 /* ECMA-262 3rd Edition 12.14 */
1641 static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat)
1643 statement_ctx_t try_ctx = {0, FALSE, TRUE}, catch_ctx = {0, TRUE, FALSE};
1644 statement_ctx_t finally_ctx = {2, FALSE, FALSE};
1645 unsigned push_except;
1646 BSTR ident;
1647 HRESULT hres;
1649 push_except = push_instr(ctx, OP_push_except);
1650 if(!push_except)
1651 return E_OUTOFMEMORY;
1653 if(stat->catch_block) {
1654 ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier);
1655 if(!ident)
1656 return E_OUTOFMEMORY;
1657 }else {
1658 ident = NULL;
1661 instr_ptr(ctx, push_except)->u.arg[1].bstr = ident;
1663 if(!stat->catch_block)
1664 try_ctx.stack_use = 2;
1666 hres = compile_statement(ctx, &try_ctx, stat->try_statement);
1667 if(FAILED(hres))
1668 return hres;
1670 if(!push_instr(ctx, OP_pop_except))
1671 return E_OUTOFMEMORY;
1673 if(stat->catch_block) {
1674 unsigned jmp_finally;
1676 jmp_finally = push_instr(ctx, OP_jmp);
1677 if(!jmp_finally)
1678 return E_OUTOFMEMORY;
1680 instr_ptr(ctx, push_except)->u.arg[0].uint = ctx->code_off;
1682 hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement);
1683 if(FAILED(hres))
1684 return hres;
1686 if(!push_instr(ctx, OP_pop_scope))
1687 return E_OUTOFMEMORY;
1689 set_arg_uint(ctx, jmp_finally, ctx->code_off);
1690 }else {
1691 set_arg_uint(ctx, push_except, ctx->code_off);
1694 if(stat->finally_statement) {
1695 hres = compile_statement(ctx, stat->catch_block ? NULL : &finally_ctx, stat->finally_statement);
1696 if(FAILED(hres))
1697 return hres;
1699 if(!stat->catch_block && !push_instr(ctx, OP_end_finally))
1700 return E_OUTOFMEMORY;
1703 return S_OK;
1706 static HRESULT compile_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1708 HRESULT hres;
1710 if(stat_ctx) {
1711 stat_ctx->next = ctx->stat_ctx;
1712 ctx->stat_ctx = stat_ctx;
1715 switch(stat->type) {
1716 case STAT_BLOCK:
1717 hres = compile_block_statement(ctx, ((block_statement_t*)stat)->stat_list);
1718 break;
1719 case STAT_BREAK:
1720 hres = compile_break_statement(ctx, (branch_statement_t*)stat);
1721 break;
1722 case STAT_CONTINUE:
1723 hres = compile_continue_statement(ctx, (branch_statement_t*)stat);
1724 break;
1725 case STAT_EMPTY:
1726 /* nothing to do */
1727 hres = S_OK;
1728 break;
1729 case STAT_EXPR:
1730 hres = compile_expression_statement(ctx, (expression_statement_t*)stat);
1731 break;
1732 case STAT_FOR:
1733 hres = compile_for_statement(ctx, (for_statement_t*)stat);
1734 break;
1735 case STAT_FORIN:
1736 hres = compile_forin_statement(ctx, (forin_statement_t*)stat);
1737 break;
1738 case STAT_IF:
1739 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1740 break;
1741 case STAT_LABEL:
1742 hres = compile_labelled_statement(ctx, (labelled_statement_t*)stat);
1743 break;
1744 case STAT_RETURN:
1745 hres = compile_return_statement(ctx, (expression_statement_t*)stat);
1746 break;
1747 case STAT_SWITCH:
1748 hres = compile_switch_statement(ctx, (switch_statement_t*)stat);
1749 break;
1750 case STAT_THROW:
1751 hres = compile_throw_statement(ctx, (expression_statement_t*)stat);
1752 break;
1753 case STAT_TRY:
1754 hres = compile_try_statement(ctx, (try_statement_t*)stat);
1755 break;
1756 case STAT_VAR:
1757 hres = compile_var_statement(ctx, (var_statement_t*)stat);
1758 break;
1759 case STAT_WHILE:
1760 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1761 break;
1762 case STAT_WITH:
1763 hres = compile_with_statement(ctx, (with_statement_t*)stat);
1764 break;
1765 DEFAULT_UNREACHABLE;
1768 if(stat_ctx) {
1769 assert(ctx->stat_ctx == stat_ctx);
1770 ctx->stat_ctx = stat_ctx->next;
1773 return hres;
1776 static void resolve_labels(compiler_ctx_t *ctx, unsigned off)
1778 instr_t *instr;
1780 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
1781 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->u.arg->uint & LABEL_FLAG)) {
1782 assert((instr->u.arg->uint & ~LABEL_FLAG) < ctx->labels_cnt);
1783 instr->u.arg->uint = ctx->labels[instr->u.arg->uint & ~LABEL_FLAG];
1785 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
1788 ctx->labels_cnt = 0;
1791 void release_bytecode(bytecode_t *code)
1793 unsigned i;
1795 if(--code->ref)
1796 return;
1798 for(i=0; i < code->bstr_cnt; i++)
1799 SysFreeString(code->bstr_pool[i]);
1800 for(i=0; i < code->str_cnt; i++)
1801 jsstr_release(code->str_pool[i]);
1803 heap_free(code->source);
1804 heap_pool_free(&code->heap);
1805 heap_free(code->bstr_pool);
1806 heap_free(code->str_pool);
1807 heap_free(code->instrs);
1808 heap_free(code);
1811 static HRESULT init_code(compiler_ctx_t *compiler, const WCHAR *source)
1813 compiler->code = heap_alloc_zero(sizeof(bytecode_t));
1814 if(!compiler->code)
1815 return E_OUTOFMEMORY;
1817 compiler->code->ref = 1;
1818 heap_pool_init(&compiler->code->heap);
1820 compiler->code->source = heap_strdupW(source);
1821 if(!compiler->code->source) {
1822 release_bytecode(compiler->code);
1823 return E_OUTOFMEMORY;
1826 compiler->code->instrs = heap_alloc(64 * sizeof(instr_t));
1827 if(!compiler->code->instrs) {
1828 release_bytecode(compiler->code);
1829 return E_OUTOFMEMORY;
1832 compiler->code_size = 64;
1833 compiler->code_off = 1;
1834 return S_OK;
1837 static HRESULT compile_function(compiler_ctx_t *ctx, source_elements_t *source, function_expression_t *func_expr,
1838 BOOL from_eval, function_code_t *func)
1840 variable_declaration_t *var_iter;
1841 function_expression_t *iter;
1842 unsigned off, i;
1843 HRESULT hres;
1845 TRACE("\n");
1847 ctx->var_head = ctx->var_tail = NULL;
1848 ctx->func_head = ctx->func_tail = NULL;
1849 ctx->from_eval = from_eval;
1851 off = ctx->code_off;
1852 ctx->func = func;
1853 hres = compile_block_statement(ctx, source->statement);
1854 if(FAILED(hres))
1855 return hres;
1857 resolve_labels(ctx, off);
1859 if(!push_instr(ctx, OP_ret))
1860 return E_OUTOFMEMORY;
1862 if(TRACE_ON(jscript_disas))
1863 dump_code(ctx, off);
1865 func->instr_off = off;
1867 if(func_expr && func_expr->identifier) {
1868 func->name = compiler_alloc_bstr(ctx, func_expr->identifier);
1869 if(!func->name)
1870 return E_OUTOFMEMORY;
1873 if(func_expr) {
1874 parameter_t *param_iter;
1876 func->source = func_expr->src_str;
1877 func->source_len = func_expr->src_len;
1879 for(param_iter = func_expr->parameter_list; param_iter; param_iter = param_iter->next)
1880 func->param_cnt++;
1882 func->params = compiler_alloc(ctx->code, func->param_cnt * sizeof(*func->params));
1883 if(!func->params)
1884 return E_OUTOFMEMORY;
1886 for(param_iter = func_expr->parameter_list, i=0; param_iter; param_iter = param_iter->next, i++) {
1887 func->params[i] = compiler_alloc_bstr(ctx, param_iter->identifier);
1888 if(!func->params[i])
1889 return E_OUTOFMEMORY;
1893 func->variables = compiler_alloc(ctx->code, func->var_cnt * sizeof(*func->variables));
1894 if(!func->variables)
1895 return E_OUTOFMEMORY;
1897 for(var_iter = ctx->var_head, i=0; var_iter; var_iter = var_iter->global_next, i++) {
1898 func->variables[i] = compiler_alloc_bstr(ctx, var_iter->identifier);
1899 if(!func->variables[i])
1900 return E_OUTOFMEMORY;
1903 assert(i == func->var_cnt);
1905 func->funcs = compiler_alloc(ctx->code, func->func_cnt * sizeof(*func->funcs));
1906 if(!func->funcs)
1907 return E_OUTOFMEMORY;
1908 memset(func->funcs, 0, func->func_cnt * sizeof(*func->funcs));
1910 for(iter = ctx->func_head, i=0; iter; iter = iter->next, i++) {
1911 hres = compile_function(ctx, iter->source_elements, iter, FALSE, func->funcs+i);
1912 if(FAILED(hres))
1913 return hres;
1916 assert(i == func->func_cnt);
1918 return S_OK;
1921 static HRESULT parse_arguments(compiler_ctx_t *ctx, const WCHAR *args, BSTR *arg_array, unsigned *args_size)
1923 const WCHAR *ptr = args, *ptr2;
1924 unsigned arg_cnt = 0;
1926 while(isspaceW(*ptr))
1927 ptr++;
1928 if(!*ptr) {
1929 if(args_size)
1930 *args_size = 0;
1931 return S_OK;
1934 while(1) {
1935 if(!isalphaW(*ptr) && *ptr != '_') {
1936 FIXME("expected alpha or '_': %s\n", debugstr_w(ptr));
1937 return E_FAIL;
1940 ptr2 = ptr;
1941 while(isalnumW(*ptr) || *ptr == '_')
1942 ptr++;
1944 if(*ptr && *ptr != ',' && !isspaceW(*ptr)) {
1945 FIXME("unexpected har %s\n", debugstr_w(ptr));
1946 return E_FAIL;
1949 if(arg_array) {
1950 arg_array[arg_cnt] = compiler_alloc_bstr_len(ctx, ptr2, ptr-ptr2);
1951 if(!arg_array[arg_cnt])
1952 return E_OUTOFMEMORY;
1954 arg_cnt++;
1956 while(isspaceW(*ptr))
1957 ptr++;
1958 if(!*ptr)
1959 break;
1960 if(*ptr != ',') {
1961 FIXME("expected ',': %s\n", debugstr_w(ptr));
1962 return E_FAIL;
1965 ptr++;
1966 while(isspaceW(*ptr))
1967 ptr++;
1970 if(args_size)
1971 *args_size = arg_cnt;
1972 return S_OK;
1975 static HRESULT compile_arguments(compiler_ctx_t *ctx, const WCHAR *args)
1977 HRESULT hres;
1979 hres = parse_arguments(ctx, args, NULL, &ctx->code->global_code.param_cnt);
1980 if(FAILED(hres))
1981 return hres;
1983 ctx->code->global_code.params = compiler_alloc(ctx->code,
1984 ctx->code->global_code.param_cnt * sizeof(*ctx->code->global_code.params));
1985 if(!ctx->code->global_code.params)
1986 return E_OUTOFMEMORY;
1988 return parse_arguments(ctx, args, ctx->code->global_code.params, NULL);
1991 HRESULT compile_script(script_ctx_t *ctx, const WCHAR *code, const WCHAR *args, const WCHAR *delimiter,
1992 BOOL from_eval, BOOL use_decode, bytecode_t **ret)
1994 compiler_ctx_t compiler = {0};
1995 HRESULT hres;
1997 hres = init_code(&compiler, code);
1998 if(FAILED(hres))
1999 return hres;
2001 if(args) {
2002 hres = compile_arguments(&compiler, args);
2003 if(FAILED(hres))
2004 return hres;
2007 if(use_decode) {
2008 hres = decode_source(compiler.code->source);
2009 if(FAILED(hres)) {
2010 WARN("Decoding failed\n");
2011 return hres;
2015 hres = script_parse(ctx, compiler.code->source, delimiter, from_eval, &compiler.parser);
2016 if(FAILED(hres)) {
2017 release_bytecode(compiler.code);
2018 return hres;
2021 hres = compile_function(&compiler, compiler.parser->source, NULL, from_eval, &compiler.code->global_code);
2022 parser_release(compiler.parser);
2023 if(FAILED(hres)) {
2024 release_bytecode(compiler.code);
2025 return hres;
2028 *ret = compiler.code;
2029 return S_OK;