netstat: Implement -a option.
[wine.git] / dlls / jscript / compile.c
blobc37ec3115ae1effd7cb53f88ec03a6f3c35e680e
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:
96 assert(0);
100 static void dump_code(compiler_ctx_t *ctx, unsigned off)
102 instr_t *instr;
104 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
105 TRACE_(jscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
106 if(instr_info[instr->op].arg1_type == ARG_DBL) {
107 TRACE_(jscript_disas)("\t%lf", instr->u.dbl);
108 }else {
109 dump_instr_arg(instr_info[instr->op].arg1_type, instr->u.arg);
110 dump_instr_arg(instr_info[instr->op].arg2_type, instr->u.arg+1);
112 TRACE_(jscript_disas)("\n");
116 static HRESULT compile_expression(compiler_ctx_t*,expression_t*,BOOL);
117 static HRESULT compile_statement(compiler_ctx_t*,statement_ctx_t*,statement_t*);
119 static inline void *compiler_alloc(bytecode_t *code, size_t size)
121 return jsheap_alloc(&code->heap, size);
124 static jsstr_t *compiler_alloc_string_len(compiler_ctx_t *ctx, const WCHAR *str, unsigned len)
126 jsstr_t *new_str;
128 if(!ctx->code->str_pool_size) {
129 ctx->code->str_pool = heap_alloc(8 * sizeof(jsstr_t*));
130 if(!ctx->code->str_pool)
131 return NULL;
132 ctx->code->str_pool_size = 8;
133 }else if(ctx->code->str_pool_size == ctx->code->str_cnt) {
134 jsstr_t **new_pool;
136 new_pool = heap_realloc(ctx->code->str_pool, ctx->code->str_pool_size*2*sizeof(jsstr_t*));
137 if(!new_pool)
138 return NULL;
140 ctx->code->str_pool = new_pool;
141 ctx->code->str_pool_size *= 2;
144 new_str = jsstr_alloc_len(str, len);
145 if(!new_str)
146 return NULL;
148 ctx->code->str_pool[ctx->code->str_cnt++] = new_str;
149 return new_str;
152 static jsstr_t *compiler_alloc_string(compiler_ctx_t *ctx, const WCHAR *str)
154 return compiler_alloc_string_len(ctx, str, strlenW(str));
157 static BOOL ensure_bstr_slot(compiler_ctx_t *ctx)
159 if(!ctx->code->bstr_pool_size) {
160 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
161 if(!ctx->code->bstr_pool)
162 return FALSE;
163 ctx->code->bstr_pool_size = 8;
164 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
165 BSTR *new_pool;
167 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
168 if(!new_pool)
169 return FALSE;
171 ctx->code->bstr_pool = new_pool;
172 ctx->code->bstr_pool_size *= 2;
175 return TRUE;
178 static BSTR compiler_alloc_bstr(compiler_ctx_t *ctx, const WCHAR *str)
180 if(!ensure_bstr_slot(ctx))
181 return NULL;
183 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
184 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
185 return NULL;
187 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
190 static BSTR compiler_alloc_bstr_len(compiler_ctx_t *ctx, const WCHAR *str, size_t len)
192 if(!ensure_bstr_slot(ctx))
193 return NULL;
195 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocStringLen(str, len);
196 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
197 return NULL;
199 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
202 static unsigned push_instr(compiler_ctx_t *ctx, jsop_t op)
204 assert(ctx->code_size >= ctx->code_off);
206 if(ctx->code_size == ctx->code_off) {
207 instr_t *new_instrs;
209 new_instrs = heap_realloc(ctx->code->instrs, ctx->code_size*2*sizeof(instr_t));
210 if(!new_instrs)
211 return 0;
213 ctx->code->instrs = new_instrs;
214 ctx->code_size *= 2;
217 ctx->code->instrs[ctx->code_off].op = op;
218 return ctx->code_off++;
221 static inline instr_t *instr_ptr(compiler_ctx_t *ctx, unsigned off)
223 assert(off < ctx->code_off);
224 return ctx->code->instrs + off;
227 static HRESULT push_instr_int(compiler_ctx_t *ctx, jsop_t op, LONG arg)
229 unsigned instr;
231 instr = push_instr(ctx, op);
232 if(!instr)
233 return E_OUTOFMEMORY;
235 instr_ptr(ctx, instr)->u.arg->lng = arg;
236 return S_OK;
239 static HRESULT push_instr_str(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
241 unsigned instr;
242 jsstr_t *str;
244 str = compiler_alloc_string(ctx, arg);
245 if(!str)
246 return E_OUTOFMEMORY;
248 instr = push_instr(ctx, op);
249 if(!instr)
250 return E_OUTOFMEMORY;
252 instr_ptr(ctx, instr)->u.arg->str = str;
253 return S_OK;
256 static HRESULT push_instr_bstr(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
258 unsigned instr;
259 WCHAR *str;
261 str = compiler_alloc_bstr(ctx, arg);
262 if(!str)
263 return E_OUTOFMEMORY;
265 instr = push_instr(ctx, op);
266 if(!instr)
267 return E_OUTOFMEMORY;
269 instr_ptr(ctx, instr)->u.arg->bstr = str;
270 return S_OK;
273 static HRESULT push_instr_bstr_uint(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg1, unsigned arg2)
275 unsigned instr;
276 WCHAR *str;
278 str = compiler_alloc_bstr(ctx, arg1);
279 if(!str)
280 return E_OUTOFMEMORY;
282 instr = push_instr(ctx, op);
283 if(!instr)
284 return E_OUTOFMEMORY;
286 instr_ptr(ctx, instr)->u.arg[0].bstr = str;
287 instr_ptr(ctx, instr)->u.arg[1].uint = arg2;
288 return S_OK;
291 static HRESULT push_instr_uint_str(compiler_ctx_t *ctx, jsop_t op, unsigned arg1, const WCHAR *arg2)
293 unsigned instr;
294 jsstr_t *str;
296 str = compiler_alloc_string(ctx, arg2);
297 if(!str)
298 return E_OUTOFMEMORY;
300 instr = push_instr(ctx, op);
301 if(!instr)
302 return E_OUTOFMEMORY;
304 instr_ptr(ctx, instr)->u.arg[0].uint = arg1;
305 instr_ptr(ctx, instr)->u.arg[1].str = str;
306 return S_OK;
309 static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
311 unsigned instr;
313 instr = push_instr(ctx, op);
314 if(!instr)
315 return E_OUTOFMEMORY;
317 instr_ptr(ctx, instr)->u.dbl = arg;
318 return S_OK;
321 static inline void set_arg_uint(compiler_ctx_t *ctx, unsigned instr, unsigned arg)
323 instr_ptr(ctx, instr)->u.arg->uint = arg;
326 static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
328 unsigned instr;
330 instr = push_instr(ctx, op);
331 if(!instr)
332 return E_OUTOFMEMORY;
334 set_arg_uint(ctx, instr, arg);
335 return S_OK;
338 static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
340 HRESULT hres;
342 hres = compile_expression(ctx, expr->expression1, TRUE);
343 if(FAILED(hres))
344 return hres;
346 hres = compile_expression(ctx, expr->expression2, TRUE);
347 if(FAILED(hres))
348 return hres;
350 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
353 static HRESULT compile_unary_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op)
355 HRESULT hres;
357 hres = compile_expression(ctx, expr->expression, TRUE);
358 if(FAILED(hres))
359 return hres;
361 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
364 /* ECMA-262 3rd Edition 11.2.1 */
365 static HRESULT compile_member_expression(compiler_ctx_t *ctx, member_expression_t *expr)
367 HRESULT hres;
369 hres = compile_expression(ctx, expr->expression, TRUE);
370 if(FAILED(hres))
371 return hres;
373 return push_instr_bstr(ctx, OP_member, expr->identifier);
376 #define LABEL_FLAG 0x80000000
378 static unsigned alloc_label(compiler_ctx_t *ctx)
380 if(!ctx->labels_size) {
381 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
382 if(!ctx->labels)
383 return 0;
384 ctx->labels_size = 8;
385 }else if(ctx->labels_size == ctx->labels_cnt) {
386 unsigned *new_labels;
388 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
389 if(!new_labels)
390 return 0;
392 ctx->labels = new_labels;
393 ctx->labels_size *= 2;
396 return ctx->labels_cnt++ | LABEL_FLAG;
399 static void label_set_addr(compiler_ctx_t *ctx, unsigned label)
401 assert(label & LABEL_FLAG);
402 ctx->labels[label & ~LABEL_FLAG] = ctx->code_off;
405 static inline BOOL is_memberid_expr(expression_type_t type)
407 return type == EXPR_IDENT || type == EXPR_MEMBER || type == EXPR_ARRAY;
410 static HRESULT compile_memberid_expression(compiler_ctx_t *ctx, expression_t *expr, unsigned flags)
412 HRESULT hres = S_OK;
414 switch(expr->type) {
415 case EXPR_IDENT: {
416 identifier_expression_t *ident_expr = (identifier_expression_t*)expr;
418 hres = push_instr_bstr_uint(ctx, OP_identid, ident_expr->identifier, flags);
419 break;
421 case EXPR_ARRAY: {
422 binary_expression_t *array_expr = (binary_expression_t*)expr;
424 hres = compile_expression(ctx, array_expr->expression1, TRUE);
425 if(FAILED(hres))
426 return hres;
428 hres = compile_expression(ctx, array_expr->expression2, TRUE);
429 if(FAILED(hres))
430 return hres;
432 hres = push_instr_uint(ctx, OP_memberid, flags);
433 break;
435 case EXPR_MEMBER: {
436 member_expression_t *member_expr = (member_expression_t*)expr;
438 hres = compile_expression(ctx, member_expr->expression, TRUE);
439 if(FAILED(hres))
440 return hres;
442 /* FIXME: Potential optimization */
443 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
444 if(FAILED(hres))
445 return hres;
447 hres = push_instr_uint(ctx, OP_memberid, flags);
448 break;
450 default:
451 assert(0);
454 return hres;
457 static HRESULT compile_increment_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op, int n)
459 HRESULT hres;
461 if(!is_memberid_expr(expr->expression->type)) {
462 hres = compile_expression(ctx, expr->expression, TRUE);
463 if(FAILED(hres))
464 return hres;
466 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
469 hres = compile_memberid_expression(ctx, expr->expression, fdexNameEnsure);
470 if(FAILED(hres))
471 return hres;
473 return push_instr_int(ctx, op, n);
476 /* ECMA-262 3rd Edition 11.14 */
477 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr, BOOL emit_ret)
479 HRESULT hres;
481 hres = compile_expression(ctx, expr->expression1, FALSE);
482 if(FAILED(hres))
483 return hres;
485 return compile_expression(ctx, expr->expression2, emit_ret);
488 /* ECMA-262 3rd Edition 11.11 */
489 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
491 unsigned instr;
492 HRESULT hres;
494 hres = compile_expression(ctx, expr->expression1, TRUE);
495 if(FAILED(hres))
496 return hres;
498 instr = push_instr(ctx, op);
499 if(!instr)
500 return E_OUTOFMEMORY;
502 hres = compile_expression(ctx, expr->expression2, TRUE);
503 if(FAILED(hres))
504 return hres;
506 set_arg_uint(ctx, instr, ctx->code_off);
507 return S_OK;
510 /* ECMA-262 3rd Edition 11.12 */
511 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
513 unsigned jmp_false, jmp_end;
514 HRESULT hres;
516 hres = compile_expression(ctx, expr->expression, TRUE);
517 if(FAILED(hres))
518 return hres;
520 jmp_false = push_instr(ctx, OP_cnd_z);
521 if(!jmp_false)
522 return E_OUTOFMEMORY;
524 hres = compile_expression(ctx, expr->true_expression, TRUE);
525 if(FAILED(hres))
526 return hres;
528 jmp_end = push_instr(ctx, OP_jmp);
529 if(!jmp_end)
530 return E_OUTOFMEMORY;
532 set_arg_uint(ctx, jmp_false, ctx->code_off);
533 hres = push_instr_uint(ctx, OP_pop, 1);
534 if(FAILED(hres))
535 return hres;
537 hres = compile_expression(ctx, expr->false_expression, TRUE);
538 if(FAILED(hres))
539 return hres;
541 set_arg_uint(ctx, jmp_end, ctx->code_off);
542 return S_OK;
545 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
547 unsigned arg_cnt = 0;
548 argument_t *arg;
549 HRESULT hres;
551 hres = compile_expression(ctx, expr->expression, TRUE);
552 if(FAILED(hres))
553 return hres;
555 for(arg = expr->argument_list; arg; arg = arg->next) {
556 hres = compile_expression(ctx, arg->expr, TRUE);
557 if(FAILED(hres))
558 return hres;
559 arg_cnt++;
562 return push_instr_uint(ctx, OP_new, arg_cnt);
565 static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL emit_ret)
567 unsigned arg_cnt = 0;
568 argument_t *arg;
569 unsigned instr;
570 jsop_t op;
571 HRESULT hres;
573 if(is_memberid_expr(expr->expression->type)) {
574 op = OP_call_member;
575 hres = compile_memberid_expression(ctx, expr->expression, 0);
576 }else {
577 op = OP_call;
578 hres = compile_expression(ctx, expr->expression, TRUE);
581 if(FAILED(hres))
582 return hres;
584 for(arg = expr->argument_list; arg; arg = arg->next) {
585 hres = compile_expression(ctx, arg->expr, TRUE);
586 if(FAILED(hres))
587 return hres;
588 arg_cnt++;
591 instr = push_instr(ctx, op);
592 if(!instr)
593 return E_OUTOFMEMORY;
595 instr_ptr(ctx, instr)->u.arg[0].uint = arg_cnt;
596 instr_ptr(ctx, instr)->u.arg[1].lng = emit_ret;
597 return S_OK;
600 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
602 HRESULT hres;
604 switch(expr->expression->type) {
605 case EXPR_ARRAY: {
606 binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
608 hres = compile_expression(ctx, array_expr->expression1, TRUE);
609 if(FAILED(hres))
610 return hres;
612 hres = compile_expression(ctx, array_expr->expression2, TRUE);
613 if(FAILED(hres))
614 return hres;
616 if(!push_instr(ctx, OP_delete))
617 return E_OUTOFMEMORY;
618 break;
620 case EXPR_MEMBER: {
621 member_expression_t *member_expr = (member_expression_t*)expr->expression;
623 hres = compile_expression(ctx, member_expr->expression, TRUE);
624 if(FAILED(hres))
625 return hres;
627 /* FIXME: Potential optimization */
628 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
629 if(FAILED(hres))
630 return hres;
632 if(!push_instr(ctx, OP_delete))
633 return E_OUTOFMEMORY;
634 break;
636 case EXPR_IDENT:
637 return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
638 default: {
639 const WCHAR fixmeW[] = {'F','I','X','M','E',0};
641 WARN("invalid delete, unimplemented exception message\n");
643 hres = compile_expression(ctx, expr->expression, TRUE);
644 if(FAILED(hres))
645 return hres;
647 return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, fixmeW);
651 return S_OK;
654 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
656 BOOL use_throw_path = FALSE;
657 unsigned arg_cnt = 0;
658 HRESULT hres;
660 if(expr->expression1->type == EXPR_CALL) {
661 call_expression_t *call_expr = (call_expression_t*)expr->expression1;
662 argument_t *arg;
664 if(op != OP_LAST) {
665 FIXME("op %d not supported on parametrized assign expressions\n", op);
666 return E_NOTIMPL;
669 if(is_memberid_expr(call_expr->expression->type) && call_expr->argument_list) {
670 hres = compile_memberid_expression(ctx, call_expr->expression, fdexNameEnsure);
671 if(FAILED(hres))
672 return hres;
674 for(arg = call_expr->argument_list; arg; arg = arg->next) {
675 hres = compile_expression(ctx, arg->expr, TRUE);
676 if(FAILED(hres))
677 return hres;
678 arg_cnt++;
680 }else {
681 use_throw_path = TRUE;
683 }else if(is_memberid_expr(expr->expression1->type)) {
684 hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
685 if(FAILED(hres))
686 return hres;
687 }else {
688 use_throw_path = TRUE;
691 if(use_throw_path) {
692 /* Illegal assignment: evaluate and throw */
693 hres = compile_expression(ctx, expr->expression1, TRUE);
694 if(FAILED(hres))
695 return hres;
697 hres = compile_expression(ctx, expr->expression2, TRUE);
698 if(FAILED(hres))
699 return hres;
701 if(op != OP_LAST && !push_instr(ctx, op))
702 return E_OUTOFMEMORY;
704 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
707 if(op != OP_LAST && !push_instr(ctx, OP_refval))
708 return E_OUTOFMEMORY;
710 hres = compile_expression(ctx, expr->expression2, TRUE);
711 if(FAILED(hres))
712 return hres;
714 if(op != OP_LAST && !push_instr(ctx, op))
715 return E_OUTOFMEMORY;
717 if(arg_cnt)
718 return push_instr_uint(ctx, OP_assign_call, arg_cnt);
720 if(!push_instr(ctx, OP_assign))
721 return E_OUTOFMEMORY;
723 return S_OK;
726 static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
728 jsop_t op;
729 HRESULT hres;
731 if(is_memberid_expr(expr->expression->type)) {
732 if(expr->expression->type == EXPR_IDENT)
733 return push_instr_bstr(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
735 op = OP_typeofid;
736 hres = compile_memberid_expression(ctx, expr->expression, 0);
737 }else {
738 op = OP_typeof;
739 hres = compile_expression(ctx, expr->expression, TRUE);
741 if(FAILED(hres))
742 return hres;
744 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
747 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
749 switch(literal->type) {
750 case LT_BOOL:
751 return push_instr_int(ctx, OP_bool, literal->u.bval);
752 case LT_DOUBLE:
753 return push_instr_double(ctx, OP_double, literal->u.dval);
754 case LT_NULL:
755 return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
756 case LT_STRING:
757 return push_instr_str(ctx, OP_str, literal->u.wstr);
758 case LT_REGEXP: {
759 unsigned instr;
760 jsstr_t *str;
762 str = compiler_alloc_string_len(ctx, literal->u.regexp.str, literal->u.regexp.str_len);
763 if(!str)
764 return E_OUTOFMEMORY;
766 instr = push_instr(ctx, OP_regexp);
767 if(!instr)
768 return E_OUTOFMEMORY;
770 instr_ptr(ctx, instr)->u.arg[0].str = str;
771 instr_ptr(ctx, instr)->u.arg[1].uint = literal->u.regexp.flags;
772 return S_OK;
774 default:
775 assert(0);
776 return E_FAIL;
780 static HRESULT literal_as_bstr(compiler_ctx_t *ctx, literal_t *literal, BSTR *str)
782 switch(literal->type) {
783 case LT_STRING:
784 *str = compiler_alloc_bstr(ctx, literal->u.wstr);
785 break;
786 case LT_DOUBLE: {
787 jsstr_t *jsstr;
788 HRESULT hres;
790 hres = double_to_string(literal->u.dval, &jsstr);
791 if(FAILED(hres))
792 return hres;
794 *str = SysAllocStringLen(jsstr->str, jsstr_length(jsstr));
795 jsstr_release(jsstr);
796 break;
798 default:
799 assert(0);
802 return *str ? S_OK : E_OUTOFMEMORY;
805 static HRESULT compile_array_literal(compiler_ctx_t *ctx, array_literal_expression_t *expr)
807 unsigned i, elem_cnt = expr->length;
808 array_element_t *iter;
809 HRESULT hres;
811 for(iter = expr->element_list; iter; iter = iter->next) {
812 elem_cnt += iter->elision+1;
814 for(i=0; i < iter->elision; i++) {
815 if(!push_instr(ctx, OP_undefined))
816 return E_OUTOFMEMORY;
819 hres = compile_expression(ctx, iter->expr, TRUE);
820 if(FAILED(hres))
821 return hres;
824 for(i=0; i < expr->length; i++) {
825 if(!push_instr(ctx, OP_undefined))
826 return E_OUTOFMEMORY;
829 return push_instr_uint(ctx, OP_carray, elem_cnt);
832 static HRESULT compile_object_literal(compiler_ctx_t *ctx, property_value_expression_t *expr)
834 prop_val_t *iter;
835 unsigned instr;
836 BSTR name;
837 HRESULT hres;
839 if(!push_instr(ctx, OP_new_obj))
840 return E_OUTOFMEMORY;
842 for(iter = expr->property_list; iter; iter = iter->next) {
843 hres = literal_as_bstr(ctx, iter->name, &name);
844 if(FAILED(hres))
845 return hres;
847 hres = compile_expression(ctx, iter->value, TRUE);
848 if(FAILED(hres))
849 return hres;
851 instr = push_instr(ctx, OP_obj_prop);
852 if(!instr)
853 return E_OUTOFMEMORY;
855 instr_ptr(ctx, instr)->u.arg->bstr = name;
858 return S_OK;
861 static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr)
863 ctx->func_tail = ctx->func_tail ? (ctx->func_tail->next = expr) : (ctx->func_head = expr);
865 /* FIXME: not exactly right */
866 if(expr->identifier) {
867 ctx->func->func_cnt++;
868 return push_instr_bstr(ctx, OP_ident, expr->identifier);
871 return push_instr_uint(ctx, OP_func, ctx->func->func_cnt++);
874 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr, BOOL emit_ret)
876 HRESULT hres;
878 switch(expr->type) {
879 case EXPR_ADD:
880 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
881 break;
882 case EXPR_AND:
883 hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_z);
884 break;
885 case EXPR_ARRAY:
886 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
887 break;
888 case EXPR_ARRAYLIT:
889 hres = compile_array_literal(ctx, (array_literal_expression_t*)expr);
890 break;
891 case EXPR_ASSIGN:
892 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
893 break;
894 case EXPR_ASSIGNADD:
895 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
896 break;
897 case EXPR_ASSIGNAND:
898 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_and);
899 break;
900 case EXPR_ASSIGNSUB:
901 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
902 break;
903 case EXPR_ASSIGNMUL:
904 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
905 break;
906 case EXPR_ASSIGNDIV:
907 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
908 break;
909 case EXPR_ASSIGNMOD:
910 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
911 break;
912 case EXPR_ASSIGNOR:
913 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
914 break;
915 case EXPR_ASSIGNLSHIFT:
916 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_lshift);
917 break;
918 case EXPR_ASSIGNRSHIFT:
919 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift);
920 break;
921 case EXPR_ASSIGNRRSHIFT:
922 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
923 break;
924 case EXPR_ASSIGNXOR:
925 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
926 break;
927 case EXPR_BAND:
928 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
929 break;
930 case EXPR_BITNEG:
931 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
932 break;
933 case EXPR_BOR:
934 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
935 break;
936 case EXPR_CALL:
937 return compile_call_expression(ctx, (call_expression_t*)expr, emit_ret);
938 case EXPR_COMMA:
939 return compile_comma_expression(ctx, (binary_expression_t*)expr, emit_ret);
940 case EXPR_COND:
941 hres = compile_conditional_expression(ctx, (conditional_expression_t*)expr);
942 break;
943 case EXPR_DELETE:
944 hres = compile_delete_expression(ctx, (unary_expression_t*)expr);
945 break;
946 case EXPR_DIV:
947 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
948 break;
949 case EXPR_EQ:
950 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
951 break;
952 case EXPR_EQEQ:
953 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
954 break;
955 case EXPR_FUNC:
956 hres = compile_function_expression(ctx, (function_expression_t*)expr);
957 break;
958 case EXPR_GREATER:
959 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
960 break;
961 case EXPR_GREATEREQ:
962 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
963 break;
964 case EXPR_IDENT:
965 hres = push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
966 break;
967 case EXPR_IN:
968 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
969 break;
970 case EXPR_INSTANCEOF:
971 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
972 break;
973 case EXPR_LESS:
974 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
975 break;
976 case EXPR_LESSEQ:
977 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
978 break;
979 case EXPR_LITERAL:
980 hres = compile_literal(ctx, ((literal_expression_t*)expr)->literal);
981 break;
982 case EXPR_LOGNEG:
983 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
984 break;
985 case EXPR_LSHIFT:
986 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
987 break;
988 case EXPR_MEMBER:
989 hres = compile_member_expression(ctx, (member_expression_t*)expr);
990 break;
991 case EXPR_MINUS:
992 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
993 break;
994 case EXPR_MOD:
995 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
996 break;
997 case EXPR_MUL:
998 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
999 break;
1000 case EXPR_NEW:
1001 hres = compile_new_expression(ctx, (call_expression_t*)expr);
1002 break;
1003 case EXPR_NOTEQ:
1004 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
1005 break;
1006 case EXPR_NOTEQEQ:
1007 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
1008 break;
1009 case EXPR_OR:
1010 hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_nz);
1011 break;
1012 case EXPR_PLUS:
1013 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
1014 break;
1015 case EXPR_POSTDEC:
1016 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
1017 break;
1018 case EXPR_POSTINC:
1019 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
1020 break;
1021 case EXPR_PREDEC:
1022 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
1023 break;
1024 case EXPR_PREINC:
1025 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
1026 break;
1027 case EXPR_PROPVAL:
1028 hres = compile_object_literal(ctx, (property_value_expression_t*)expr);
1029 break;
1030 case EXPR_RSHIFT:
1031 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
1032 break;
1033 case EXPR_RRSHIFT:
1034 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
1035 break;
1036 case EXPR_SUB:
1037 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
1038 break;
1039 case EXPR_THIS:
1040 return !emit_ret || push_instr(ctx, OP_this) ? S_OK : E_OUTOFMEMORY;
1041 case EXPR_TYPEOF:
1042 hres = compile_typeof_expression(ctx, (unary_expression_t*)expr);
1043 break;
1044 case EXPR_VOID:
1045 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
1046 break;
1047 case EXPR_BXOR:
1048 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
1049 break;
1050 default:
1051 assert(0);
1054 if(FAILED(hres))
1055 return hres;
1057 return emit_ret ? S_OK : push_instr_uint(ctx, OP_pop, 1);
1060 static inline BOOL is_loop_statement(statement_type_t type)
1062 return type == STAT_FOR || type == STAT_FORIN || type == STAT_WHILE;
1065 /* ECMA-262 3rd Edition 12.1 */
1066 static HRESULT compile_block_statement(compiler_ctx_t *ctx, statement_t *iter)
1068 HRESULT hres;
1070 while(iter) {
1071 hres = compile_statement(ctx, NULL, iter);
1072 if(FAILED(hres))
1073 return hres;
1075 iter = iter->next;
1078 return S_OK;
1081 /* ECMA-262 3rd Edition 12.2 */
1082 static HRESULT compile_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
1084 variable_declaration_t *iter;
1085 HRESULT hres;
1087 assert(list != NULL);
1089 if(ctx->var_tail)
1090 ctx->var_tail->global_next = list;
1091 else
1092 ctx->var_head = list;
1094 for(iter = list; iter; iter = iter->next) {
1095 ctx->func->var_cnt++;
1096 iter->global_next = iter->next;
1097 if(!iter->next)
1098 ctx->var_tail = iter;
1100 if(!iter->expr)
1101 continue;
1103 hres = compile_expression(ctx, iter->expr, TRUE);
1104 if(FAILED(hres))
1105 return hres;
1107 hres = push_instr_bstr(ctx, OP_var_set, iter->identifier);
1108 if(FAILED(hres))
1109 return hres;
1112 return S_OK;
1115 /* ECMA-262 3rd Edition 12.2 */
1116 static HRESULT compile_var_statement(compiler_ctx_t *ctx, var_statement_t *stat)
1118 return compile_variable_list(ctx, stat->variable_list);
1121 /* ECMA-262 3rd Edition 12.4 */
1122 static HRESULT compile_expression_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1124 HRESULT hres;
1126 hres = compile_expression(ctx, stat->expr, ctx->from_eval);
1127 if(FAILED(hres))
1128 return hres;
1130 return !ctx->from_eval || push_instr(ctx, OP_setret) ? S_OK : E_OUTOFMEMORY;
1133 /* ECMA-262 3rd Edition 12.5 */
1134 static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat)
1136 unsigned jmp_else;
1137 HRESULT hres;
1139 hres = compile_expression(ctx, stat->expr, TRUE);
1140 if(FAILED(hres))
1141 return hres;
1143 jmp_else = push_instr(ctx, OP_jmp_z);
1144 if(!jmp_else)
1145 return E_OUTOFMEMORY;
1147 hres = compile_statement(ctx, NULL, stat->if_stat);
1148 if(FAILED(hres))
1149 return hres;
1151 if(stat->else_stat) {
1152 unsigned jmp_end;
1154 jmp_end = push_instr(ctx, OP_jmp);
1155 if(!jmp_end)
1156 return E_OUTOFMEMORY;
1158 set_arg_uint(ctx, jmp_else, ctx->code_off);
1160 hres = compile_statement(ctx, NULL, stat->else_stat);
1161 if(FAILED(hres))
1162 return hres;
1164 set_arg_uint(ctx, jmp_end, ctx->code_off);
1165 }else {
1166 set_arg_uint(ctx, jmp_else, ctx->code_off);
1169 return S_OK;
1172 /* ECMA-262 3rd Edition 12.6.2 */
1173 static HRESULT compile_while_statement(compiler_ctx_t *ctx, while_statement_t *stat)
1175 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1176 unsigned jmp_off;
1177 HRESULT hres;
1179 stat_ctx.break_label = alloc_label(ctx);
1180 if(!stat_ctx.break_label)
1181 return E_OUTOFMEMORY;
1183 stat_ctx.continue_label = alloc_label(ctx);
1184 if(!stat_ctx.continue_label)
1185 return E_OUTOFMEMORY;
1187 jmp_off = ctx->code_off;
1189 if(!stat->do_while) {
1190 label_set_addr(ctx, stat_ctx.continue_label);
1191 hres = compile_expression(ctx, stat->expr, TRUE);
1192 if(FAILED(hres))
1193 return hres;
1195 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1196 if(FAILED(hres))
1197 return hres;
1200 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1201 if(FAILED(hres))
1202 return hres;
1204 if(stat->do_while) {
1205 label_set_addr(ctx, stat_ctx.continue_label);
1206 hres = compile_expression(ctx, stat->expr, TRUE);
1207 if(FAILED(hres))
1208 return hres;
1210 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1211 if(FAILED(hres))
1212 return hres;
1215 hres = push_instr_uint(ctx, OP_jmp, jmp_off);
1216 if(FAILED(hres))
1217 return hres;
1219 label_set_addr(ctx, stat_ctx.break_label);
1220 return S_OK;
1223 /* ECMA-262 3rd Edition 12.6.3 */
1224 static HRESULT compile_for_statement(compiler_ctx_t *ctx, for_statement_t *stat)
1226 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1227 unsigned expr_off;
1228 HRESULT hres;
1230 if(stat->variable_list) {
1231 hres = compile_variable_list(ctx, stat->variable_list);
1232 if(FAILED(hres))
1233 return hres;
1234 }else if(stat->begin_expr) {
1235 hres = compile_expression(ctx, stat->begin_expr, FALSE);
1236 if(FAILED(hres))
1237 return hres;
1240 stat_ctx.break_label = alloc_label(ctx);
1241 if(!stat_ctx.break_label)
1242 return E_OUTOFMEMORY;
1244 stat_ctx.continue_label = alloc_label(ctx);
1245 if(!stat_ctx.continue_label)
1246 return E_OUTOFMEMORY;
1248 expr_off = ctx->code_off;
1250 if(stat->expr) {
1251 hres = compile_expression(ctx, stat->expr, TRUE);
1252 if(FAILED(hres))
1253 return hres;
1255 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1256 if(FAILED(hres))
1257 return hres;
1260 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1261 if(FAILED(hres))
1262 return hres;
1264 label_set_addr(ctx, stat_ctx.continue_label);
1266 if(stat->end_expr) {
1267 hres = compile_expression(ctx, stat->end_expr, FALSE);
1268 if(FAILED(hres))
1269 return hres;
1272 hres = push_instr_uint(ctx, OP_jmp, expr_off);
1273 if(FAILED(hres))
1274 return hres;
1276 label_set_addr(ctx, stat_ctx.break_label);
1277 return S_OK;
1280 /* ECMA-262 3rd Edition 12.6.4 */
1281 static HRESULT compile_forin_statement(compiler_ctx_t *ctx, forin_statement_t *stat)
1283 statement_ctx_t stat_ctx = {4, FALSE, FALSE};
1284 HRESULT hres;
1286 if(stat->variable) {
1287 hres = compile_variable_list(ctx, stat->variable);
1288 if(FAILED(hres))
1289 return hres;
1292 stat_ctx.break_label = alloc_label(ctx);
1293 if(!stat_ctx.break_label)
1294 return E_OUTOFMEMORY;
1296 stat_ctx.continue_label = alloc_label(ctx);
1297 if(!stat_ctx.continue_label)
1298 return E_OUTOFMEMORY;
1300 hres = compile_expression(ctx, stat->in_expr, TRUE);
1301 if(FAILED(hres))
1302 return hres;
1304 if(stat->variable) {
1305 hres = push_instr_bstr_uint(ctx, OP_identid, stat->variable->identifier, fdexNameEnsure);
1306 if(FAILED(hres))
1307 return hres;
1308 }else if(is_memberid_expr(stat->expr->type)) {
1309 hres = compile_memberid_expression(ctx, stat->expr, fdexNameEnsure);
1310 if(FAILED(hres))
1311 return hres;
1312 }else {
1313 hres = push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
1314 if(FAILED(hres))
1315 return hres;
1317 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1318 return S_OK;
1321 hres = push_instr_int(ctx, OP_int, DISPID_STARTENUM);
1322 if(FAILED(hres))
1323 return hres;
1325 label_set_addr(ctx, stat_ctx.continue_label);
1326 hres = push_instr_uint(ctx, OP_forin, stat_ctx.break_label);
1327 if(FAILED(hres))
1328 return E_OUTOFMEMORY;
1330 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1331 if(FAILED(hres))
1332 return hres;
1334 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.continue_label);
1335 if(FAILED(hres))
1336 return hres;
1338 label_set_addr(ctx, stat_ctx.break_label);
1339 return S_OK;
1342 static HRESULT pop_to_stat(compiler_ctx_t *ctx, BOOL var_stack, BOOL scope_stack, statement_ctx_t *stat_ctx)
1344 unsigned stack_pop = 0;
1345 statement_ctx_t *iter;
1347 for(iter = ctx->stat_ctx; iter != stat_ctx; iter = iter->next) {
1348 if(scope_stack) {
1349 if(iter->using_scope && !push_instr(ctx, OP_pop_scope))
1350 return E_OUTOFMEMORY;
1351 if(iter->using_except && !push_instr(ctx, OP_pop_except))
1352 return E_OUTOFMEMORY;
1354 stack_pop += iter->stack_use;
1357 if(var_stack && stack_pop) {
1358 HRESULT hres;
1360 hres = push_instr_uint(ctx, OP_pop, stack_pop);
1361 if(FAILED(hres))
1362 return hres;
1365 return S_OK;
1368 /* ECMA-262 3rd Edition 12.7 */
1369 static HRESULT compile_continue_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1371 statement_ctx_t *pop_ctx;
1372 HRESULT hres;
1374 if(stat->identifier) {
1375 statement_t *label_stat;
1376 statement_ctx_t *iter;
1378 pop_ctx = NULL;
1380 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1381 if(iter->continue_label)
1382 pop_ctx = iter;
1383 if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier))
1384 break;
1387 if(!iter) {
1388 WARN("Label not found\n");
1389 return JS_E_LABEL_NOT_FOUND;
1392 /* Labelled continue are allowed only on loops */
1393 for(label_stat = iter->labelled_stat->statement;
1394 label_stat->type == STAT_LABEL;
1395 label_stat = ((labelled_statement_t*)label_stat)->statement);
1396 if(!is_loop_statement(label_stat->type)) {
1397 WARN("Label is not a loop\n");
1398 return JS_E_INVALID_CONTINUE;
1401 assert(pop_ctx != NULL);
1402 }else {
1403 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1404 if(pop_ctx->continue_label)
1405 break;
1408 if(!pop_ctx) {
1409 WARN("continue outside loop\n");
1410 return JS_E_INVALID_CONTINUE;
1414 hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx);
1415 if(FAILED(hres))
1416 return hres;
1418 return push_instr_uint(ctx, OP_jmp, pop_ctx->continue_label);
1421 /* ECMA-262 3rd Edition 12.8 */
1422 static HRESULT compile_break_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1424 statement_ctx_t *pop_ctx;
1425 HRESULT hres;
1427 if(stat->identifier) {
1428 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1429 if(pop_ctx->labelled_stat && !strcmpW(pop_ctx->labelled_stat->identifier, stat->identifier)) {
1430 assert(pop_ctx->break_label);
1431 break;
1435 if(!pop_ctx) {
1436 WARN("Label not found\n");
1437 return JS_E_LABEL_NOT_FOUND;
1439 }else {
1440 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1441 if(pop_ctx->break_label && !pop_ctx->labelled_stat)
1442 break;
1445 if(!pop_ctx) {
1446 WARN("Break outside loop\n");
1447 return JS_E_INVALID_BREAK;
1451 hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx->next);
1452 if(FAILED(hres))
1453 return hres;
1455 return push_instr_uint(ctx, OP_jmp, pop_ctx->break_label);
1458 /* ECMA-262 3rd Edition 12.9 */
1459 static HRESULT compile_return_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1461 HRESULT hres;
1463 if(ctx->from_eval) {
1464 WARN("misplaced return statement\n");
1465 return JS_E_MISPLACED_RETURN;
1468 hres = pop_to_stat(ctx, TRUE, FALSE, NULL);
1469 if(FAILED(hres))
1470 return hres;
1472 if(stat->expr) {
1473 hres = compile_expression(ctx, stat->expr, TRUE);
1474 if(FAILED(hres))
1475 return hres;
1476 if(!push_instr(ctx, OP_setret))
1477 return E_OUTOFMEMORY;
1480 hres = pop_to_stat(ctx, FALSE, TRUE, NULL);
1481 if(FAILED(hres))
1482 return hres;
1484 return push_instr(ctx, OP_ret) ? S_OK : E_OUTOFMEMORY;
1487 /* ECMA-262 3rd Edition 12.10 */
1488 static HRESULT compile_with_statement(compiler_ctx_t *ctx, with_statement_t *stat)
1490 statement_ctx_t stat_ctx = {0, TRUE, FALSE};
1491 HRESULT hres;
1493 hres = compile_expression(ctx, stat->expr, TRUE);
1494 if(FAILED(hres))
1495 return hres;
1497 if(!push_instr(ctx, OP_push_scope))
1498 return E_OUTOFMEMORY;
1500 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1501 if(FAILED(hres))
1502 return hres;
1504 if(!push_instr(ctx, OP_pop_scope))
1505 return E_OUTOFMEMORY;
1507 return S_OK;
1510 /* ECMA-262 3rd Edition 12.10 */
1511 static HRESULT compile_labelled_statement(compiler_ctx_t *ctx, labelled_statement_t *stat)
1513 statement_ctx_t stat_ctx = {0, FALSE, FALSE, 0, 0, stat}, *iter;
1514 HRESULT hres;
1516 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1517 if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier)) {
1518 WARN("Label %s redefined\n", debugstr_w(stat->identifier));
1519 return JS_E_LABEL_REDEFINED;
1523 /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1524 stat_ctx.break_label = alloc_label(ctx);
1525 if(!stat_ctx.break_label)
1526 return E_OUTOFMEMORY;
1528 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1529 if(FAILED(hres))
1530 return hres;
1532 label_set_addr(ctx, stat_ctx.break_label);
1533 return S_OK;
1536 /* ECMA-262 3rd Edition 12.13 */
1537 static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat)
1539 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1540 unsigned case_cnt = 0, *case_jmps, i, default_jmp;
1541 BOOL have_default = FALSE;
1542 statement_t *stat_iter;
1543 case_clausule_t *iter;
1544 HRESULT hres;
1546 hres = compile_expression(ctx, stat->expr, TRUE);
1547 if(FAILED(hres))
1548 return hres;
1550 stat_ctx.break_label = alloc_label(ctx);
1551 if(!stat_ctx.break_label)
1552 return E_OUTOFMEMORY;
1554 for(iter = stat->case_list; iter; iter = iter->next) {
1555 if(iter->expr)
1556 case_cnt++;
1559 case_jmps = heap_alloc(case_cnt * sizeof(*case_jmps));
1560 if(!case_jmps)
1561 return E_OUTOFMEMORY;
1563 i = 0;
1564 for(iter = stat->case_list; iter; iter = iter->next) {
1565 if(!iter->expr) {
1566 have_default = TRUE;
1567 continue;
1570 hres = compile_expression(ctx, iter->expr, TRUE);
1571 if(FAILED(hres))
1572 break;
1574 case_jmps[i] = push_instr(ctx, OP_case);
1575 if(!case_jmps[i]) {
1576 hres = E_OUTOFMEMORY;
1577 break;
1579 i++;
1582 if(SUCCEEDED(hres)) {
1583 hres = push_instr_uint(ctx, OP_pop, 1);
1584 if(SUCCEEDED(hres)) {
1585 default_jmp = push_instr(ctx, OP_jmp);
1586 if(!default_jmp)
1587 hres = E_OUTOFMEMORY;
1591 if(FAILED(hres)) {
1592 heap_free(case_jmps);
1593 return hres;
1596 i = 0;
1597 for(iter = stat->case_list; iter; iter = iter->next) {
1598 while(iter->next && iter->next->stat == iter->stat) {
1599 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1600 iter = iter->next;
1603 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1605 for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter);
1606 stat_iter = stat_iter->next) {
1607 hres = compile_statement(ctx, &stat_ctx, stat_iter);
1608 if(FAILED(hres))
1609 break;
1611 if(FAILED(hres))
1612 break;
1615 heap_free(case_jmps);
1616 if(FAILED(hres))
1617 return hres;
1618 assert(i == case_cnt);
1620 if(!have_default) {
1621 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.break_label);
1622 if(FAILED(hres))
1623 return hres;
1624 set_arg_uint(ctx, default_jmp, ctx->code_off);
1627 label_set_addr(ctx, stat_ctx.break_label);
1628 return S_OK;
1631 /* ECMA-262 3rd Edition 12.13 */
1632 static HRESULT compile_throw_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1634 HRESULT hres;
1636 hres = compile_expression(ctx, stat->expr, TRUE);
1637 if(FAILED(hres))
1638 return hres;
1640 return push_instr(ctx, OP_throw) ? S_OK : E_OUTOFMEMORY;
1643 /* ECMA-262 3rd Edition 12.14 */
1644 static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat)
1646 statement_ctx_t try_ctx = {0, FALSE, TRUE}, catch_ctx = {0, TRUE, FALSE};
1647 statement_ctx_t finally_ctx = {2, FALSE, FALSE};
1648 unsigned push_except;
1649 BSTR ident;
1650 HRESULT hres;
1652 push_except = push_instr(ctx, OP_push_except);
1653 if(!push_except)
1654 return E_OUTOFMEMORY;
1656 if(stat->catch_block) {
1657 ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier);
1658 if(!ident)
1659 return E_OUTOFMEMORY;
1660 }else {
1661 ident = NULL;
1664 instr_ptr(ctx, push_except)->u.arg[1].bstr = ident;
1666 if(!stat->catch_block)
1667 try_ctx.stack_use = 2;
1669 hres = compile_statement(ctx, &try_ctx, stat->try_statement);
1670 if(FAILED(hres))
1671 return hres;
1673 if(!push_instr(ctx, OP_pop_except))
1674 return E_OUTOFMEMORY;
1676 if(stat->catch_block) {
1677 unsigned jmp_finally;
1679 jmp_finally = push_instr(ctx, OP_jmp);
1680 if(!jmp_finally)
1681 return E_OUTOFMEMORY;
1683 instr_ptr(ctx, push_except)->u.arg[0].uint = ctx->code_off;
1685 hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement);
1686 if(FAILED(hres))
1687 return hres;
1689 if(!push_instr(ctx, OP_pop_scope))
1690 return E_OUTOFMEMORY;
1692 set_arg_uint(ctx, jmp_finally, ctx->code_off);
1693 }else {
1694 set_arg_uint(ctx, push_except, ctx->code_off);
1697 if(stat->finally_statement) {
1698 hres = compile_statement(ctx, stat->catch_block ? NULL : &finally_ctx, stat->finally_statement);
1699 if(FAILED(hres))
1700 return hres;
1702 if(!stat->catch_block && !push_instr(ctx, OP_end_finally))
1703 return E_OUTOFMEMORY;
1706 return S_OK;
1709 static HRESULT compile_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1711 HRESULT hres;
1713 if(stat_ctx) {
1714 stat_ctx->next = ctx->stat_ctx;
1715 ctx->stat_ctx = stat_ctx;
1718 switch(stat->type) {
1719 case STAT_BLOCK:
1720 hres = compile_block_statement(ctx, ((block_statement_t*)stat)->stat_list);
1721 break;
1722 case STAT_BREAK:
1723 hres = compile_break_statement(ctx, (branch_statement_t*)stat);
1724 break;
1725 case STAT_CONTINUE:
1726 hres = compile_continue_statement(ctx, (branch_statement_t*)stat);
1727 break;
1728 case STAT_EMPTY:
1729 /* nothing to do */
1730 hres = S_OK;
1731 break;
1732 case STAT_EXPR:
1733 hres = compile_expression_statement(ctx, (expression_statement_t*)stat);
1734 break;
1735 case STAT_FOR:
1736 hres = compile_for_statement(ctx, (for_statement_t*)stat);
1737 break;
1738 case STAT_FORIN:
1739 hres = compile_forin_statement(ctx, (forin_statement_t*)stat);
1740 break;
1741 case STAT_IF:
1742 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1743 break;
1744 case STAT_LABEL:
1745 hres = compile_labelled_statement(ctx, (labelled_statement_t*)stat);
1746 break;
1747 case STAT_RETURN:
1748 hres = compile_return_statement(ctx, (expression_statement_t*)stat);
1749 break;
1750 case STAT_SWITCH:
1751 hres = compile_switch_statement(ctx, (switch_statement_t*)stat);
1752 break;
1753 case STAT_THROW:
1754 hres = compile_throw_statement(ctx, (expression_statement_t*)stat);
1755 break;
1756 case STAT_TRY:
1757 hres = compile_try_statement(ctx, (try_statement_t*)stat);
1758 break;
1759 case STAT_VAR:
1760 hres = compile_var_statement(ctx, (var_statement_t*)stat);
1761 break;
1762 case STAT_WHILE:
1763 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1764 break;
1765 case STAT_WITH:
1766 hres = compile_with_statement(ctx, (with_statement_t*)stat);
1767 break;
1768 default:
1769 assert(0);
1770 hres = E_FAIL;
1773 if(stat_ctx) {
1774 assert(ctx->stat_ctx == stat_ctx);
1775 ctx->stat_ctx = stat_ctx->next;
1778 return hres;
1781 static void resolve_labels(compiler_ctx_t *ctx, unsigned off)
1783 instr_t *instr;
1785 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
1786 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->u.arg->uint & LABEL_FLAG)) {
1787 assert((instr->u.arg->uint & ~LABEL_FLAG) < ctx->labels_cnt);
1788 instr->u.arg->uint = ctx->labels[instr->u.arg->uint & ~LABEL_FLAG];
1790 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
1793 ctx->labels_cnt = 0;
1796 void release_bytecode(bytecode_t *code)
1798 unsigned i;
1800 if(--code->ref)
1801 return;
1803 for(i=0; i < code->bstr_cnt; i++)
1804 SysFreeString(code->bstr_pool[i]);
1805 for(i=0; i < code->str_cnt; i++)
1806 jsstr_release(code->str_pool[i]);
1808 heap_free(code->source);
1809 jsheap_free(&code->heap);
1810 heap_free(code->bstr_pool);
1811 heap_free(code->str_pool);
1812 heap_free(code->instrs);
1813 heap_free(code);
1816 static HRESULT init_code(compiler_ctx_t *compiler, const WCHAR *source)
1818 compiler->code = heap_alloc_zero(sizeof(bytecode_t));
1819 if(!compiler->code)
1820 return E_OUTOFMEMORY;
1822 compiler->code->ref = 1;
1823 jsheap_init(&compiler->code->heap);
1825 compiler->code->source = heap_strdupW(source);
1826 if(!compiler->code->source) {
1827 release_bytecode(compiler->code);
1828 return E_OUTOFMEMORY;
1831 compiler->code->instrs = heap_alloc(64 * sizeof(instr_t));
1832 if(!compiler->code->instrs) {
1833 release_bytecode(compiler->code);
1834 return E_OUTOFMEMORY;
1837 compiler->code_size = 64;
1838 compiler->code_off = 1;
1839 return S_OK;
1842 static HRESULT compile_function(compiler_ctx_t *ctx, source_elements_t *source, function_expression_t *func_expr,
1843 BOOL from_eval, function_code_t *func)
1845 variable_declaration_t *var_iter;
1846 function_expression_t *iter;
1847 unsigned off, i;
1848 HRESULT hres;
1850 TRACE("\n");
1852 ctx->var_head = ctx->var_tail = NULL;
1853 ctx->func_head = ctx->func_tail = NULL;
1854 ctx->from_eval = from_eval;
1856 off = ctx->code_off;
1857 ctx->func = func;
1858 hres = compile_block_statement(ctx, source->statement);
1859 if(FAILED(hres))
1860 return hres;
1862 resolve_labels(ctx, off);
1864 if(!push_instr(ctx, OP_ret))
1865 return E_OUTOFMEMORY;
1867 if(TRACE_ON(jscript_disas))
1868 dump_code(ctx, off);
1870 func->instr_off = off;
1872 if(func_expr && func_expr->identifier) {
1873 func->name = compiler_alloc_bstr(ctx, func_expr->identifier);
1874 if(!func->name)
1875 return E_OUTOFMEMORY;
1878 if(func_expr) {
1879 parameter_t *param_iter;
1881 func->source = func_expr->src_str;
1882 func->source_len = func_expr->src_len;
1884 for(param_iter = func_expr->parameter_list; param_iter; param_iter = param_iter->next)
1885 func->param_cnt++;
1887 func->params = compiler_alloc(ctx->code, func->param_cnt * sizeof(*func->params));
1888 if(!func->params)
1889 return E_OUTOFMEMORY;
1891 for(param_iter = func_expr->parameter_list, i=0; param_iter; param_iter = param_iter->next, i++) {
1892 func->params[i] = compiler_alloc_bstr(ctx, param_iter->identifier);
1893 if(!func->params[i])
1894 return E_OUTOFMEMORY;
1898 func->variables = compiler_alloc(ctx->code, func->var_cnt * sizeof(*func->variables));
1899 if(!func->variables)
1900 return E_OUTOFMEMORY;
1902 for(var_iter = ctx->var_head, i=0; var_iter; var_iter = var_iter->global_next, i++) {
1903 func->variables[i] = compiler_alloc_bstr(ctx, var_iter->identifier);
1904 if(!func->variables[i])
1905 return E_OUTOFMEMORY;
1908 assert(i == func->var_cnt);
1910 func->funcs = compiler_alloc(ctx->code, func->func_cnt * sizeof(*func->funcs));
1911 if(!func->funcs)
1912 return E_OUTOFMEMORY;
1913 memset(func->funcs, 0, func->func_cnt * sizeof(*func->funcs));
1915 for(iter = ctx->func_head, i=0; iter; iter = iter->next, i++) {
1916 hres = compile_function(ctx, iter->source_elements, iter, FALSE, func->funcs+i);
1917 if(FAILED(hres))
1918 return hres;
1921 assert(i == func->func_cnt);
1923 return S_OK;
1926 static HRESULT parse_arguments(compiler_ctx_t *ctx, const WCHAR *args, BSTR *arg_array, unsigned *args_size)
1928 const WCHAR *ptr = args, *ptr2;
1929 unsigned arg_cnt = 0;
1931 while(isspaceW(*ptr))
1932 ptr++;
1933 if(!*ptr) {
1934 if(args_size)
1935 *args_size = 0;
1936 return S_OK;
1939 while(1) {
1940 if(!isalphaW(*ptr) && *ptr != '_') {
1941 FIXME("expected alpha or '_': %s\n", debugstr_w(ptr));
1942 return E_FAIL;
1945 ptr2 = ptr;
1946 while(isalnumW(*ptr) || *ptr == '_')
1947 ptr++;
1949 if(*ptr && *ptr != ',' && !isspaceW(*ptr)) {
1950 FIXME("unexpected har %s\n", debugstr_w(ptr));
1951 return E_FAIL;
1954 if(arg_array) {
1955 arg_array[arg_cnt] = compiler_alloc_bstr_len(ctx, ptr2, ptr-ptr2);
1956 if(!arg_array[arg_cnt])
1957 return E_OUTOFMEMORY;
1959 arg_cnt++;
1961 while(isspaceW(*ptr))
1962 ptr++;
1963 if(!*ptr)
1964 break;
1965 if(*ptr != ',') {
1966 FIXME("expected ',': %s\n", debugstr_w(ptr));
1967 return E_FAIL;
1970 ptr++;
1971 while(isspaceW(*ptr))
1972 ptr++;
1975 if(args_size)
1976 *args_size = arg_cnt;
1977 return S_OK;
1980 static HRESULT compile_arguments(compiler_ctx_t *ctx, const WCHAR *args)
1982 HRESULT hres;
1984 hres = parse_arguments(ctx, args, NULL, &ctx->code->global_code.param_cnt);
1985 if(FAILED(hres))
1986 return hres;
1988 ctx->code->global_code.params = compiler_alloc(ctx->code,
1989 ctx->code->global_code.param_cnt * sizeof(*ctx->code->global_code.params));
1990 if(!ctx->code->global_code.params)
1991 return E_OUTOFMEMORY;
1993 return parse_arguments(ctx, args, ctx->code->global_code.params, NULL);
1996 HRESULT compile_script(script_ctx_t *ctx, const WCHAR *code, const WCHAR *args, const WCHAR *delimiter,
1997 BOOL from_eval, BOOL use_decode, bytecode_t **ret)
1999 compiler_ctx_t compiler = {0};
2000 HRESULT hres;
2002 hres = init_code(&compiler, code);
2003 if(FAILED(hres))
2004 return hres;
2006 if(args) {
2007 hres = compile_arguments(&compiler, args);
2008 if(FAILED(hres))
2009 return hres;
2012 if(use_decode) {
2013 hres = decode_source(compiler.code->source);
2014 if(FAILED(hres)) {
2015 WARN("Decoding failed\n");
2016 return hres;
2020 hres = script_parse(ctx, compiler.code->source, delimiter, from_eval, &compiler.parser);
2021 if(FAILED(hres)) {
2022 release_bytecode(compiler.code);
2023 return hres;
2026 hres = compile_function(&compiler, compiler.parser->source, NULL, from_eval, &compiler.code->global_code);
2027 parser_release(compiler.parser);
2028 if(FAILED(hres)) {
2029 release_bytecode(compiler.code);
2030 return hres;
2033 *ret = compiler.code;
2034 return S_OK;