wininet: Move freeing netconn into create_netconn_socket (Coverity).
[wine.git] / dlls / jscript / compile.c
blob2e18c509c0f9cd0e0109e017e3eeab39bb788218
1 /*
2 * Copyright 2011 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <math.h>
20 #include <assert.h>
22 #include "jscript.h"
23 #include "engine.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28 WINE_DECLARE_DEBUG_CHANNEL(jscript_disas);
30 typedef struct _statement_ctx_t {
31 unsigned stack_use;
32 BOOL using_scope;
33 BOOL using_except;
35 unsigned break_label;
36 unsigned continue_label;
38 const labelled_statement_t *labelled_stat;
40 struct _statement_ctx_t *next;
41 } statement_ctx_t;
43 typedef struct {
44 parser_ctx_t *parser;
45 bytecode_t *code;
47 unsigned code_off;
48 unsigned code_size;
50 unsigned *labels;
51 unsigned labels_size;
52 unsigned labels_cnt;
54 statement_ctx_t *stat_ctx;
55 function_code_t *func;
57 variable_declaration_t *var_head;
58 variable_declaration_t *var_tail;
60 function_expression_t *func_head;
61 function_expression_t *func_tail;
62 } compiler_ctx_t;
64 static const struct {
65 const char *op_str;
66 instr_arg_type_t arg1_type;
67 instr_arg_type_t arg2_type;
68 } instr_info[] = {
69 #define X(n,a,b,c) {#n,b,c},
70 OP_LIST
71 #undef X
74 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
76 switch(type) {
77 case ARG_STR:
78 TRACE_(jscript_disas)("\t%s", debugstr_jsstr(arg->str));
79 break;
80 case ARG_BSTR:
81 TRACE_(jscript_disas)("\t%s", debugstr_wn(arg->bstr, SysStringLen(arg->bstr)));
82 break;
83 case ARG_INT:
84 TRACE_(jscript_disas)("\t%d", arg->uint);
85 break;
86 case ARG_UINT:
87 case ARG_ADDR:
88 TRACE_(jscript_disas)("\t%u", arg->uint);
89 break;
90 case ARG_FUNC:
91 case ARG_NONE:
92 break;
93 default:
94 assert(0);
98 static void dump_code(compiler_ctx_t *ctx, unsigned off)
100 instr_t *instr;
102 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
103 TRACE_(jscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
104 if(instr_info[instr->op].arg1_type == ARG_DBL) {
105 TRACE_(jscript_disas)("\t%lf", instr->u.dbl);
106 }else {
107 dump_instr_arg(instr_info[instr->op].arg1_type, instr->u.arg);
108 dump_instr_arg(instr_info[instr->op].arg2_type, instr->u.arg+1);
110 TRACE_(jscript_disas)("\n");
114 static HRESULT compile_expression(compiler_ctx_t*,expression_t*);
115 static HRESULT compile_statement(compiler_ctx_t*,statement_ctx_t*,statement_t*);
117 static inline void *compiler_alloc(bytecode_t *code, size_t size)
119 return jsheap_alloc(&code->heap, size);
122 static jsstr_t *compiler_alloc_string_len(compiler_ctx_t *ctx, const WCHAR *str, unsigned len)
124 jsstr_t *new_str;
126 if(!ctx->code->str_pool_size) {
127 ctx->code->str_pool = heap_alloc(8 * sizeof(jsstr_t*));
128 if(!ctx->code->str_pool)
129 return NULL;
130 ctx->code->str_pool_size = 8;
131 }else if(ctx->code->str_pool_size == ctx->code->str_cnt) {
132 jsstr_t **new_pool;
134 new_pool = heap_realloc(ctx->code->str_pool, ctx->code->str_pool_size*2*sizeof(jsstr_t*));
135 if(!new_pool)
136 return NULL;
138 ctx->code->str_pool = new_pool;
139 ctx->code->str_pool_size *= 2;
142 new_str = jsstr_alloc_len(str, len);
143 if(!new_str)
144 return NULL;
146 ctx->code->str_pool[ctx->code->str_cnt++] = new_str;
147 return new_str;
150 static jsstr_t *compiler_alloc_string(compiler_ctx_t *ctx, const WCHAR *str)
152 return compiler_alloc_string_len(ctx, str, strlenW(str));
155 static BOOL ensure_bstr_slot(compiler_ctx_t *ctx)
157 if(!ctx->code->bstr_pool_size) {
158 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
159 if(!ctx->code->bstr_pool)
160 return FALSE;
161 ctx->code->bstr_pool_size = 8;
162 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
163 BSTR *new_pool;
165 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
166 if(!new_pool)
167 return FALSE;
169 ctx->code->bstr_pool = new_pool;
170 ctx->code->bstr_pool_size *= 2;
173 return TRUE;
176 static BSTR compiler_alloc_bstr(compiler_ctx_t *ctx, const WCHAR *str)
178 if(!ensure_bstr_slot(ctx))
179 return NULL;
181 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
182 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
183 return NULL;
185 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
188 static BSTR compiler_alloc_bstr_len(compiler_ctx_t *ctx, const WCHAR *str, size_t len)
190 if(!ensure_bstr_slot(ctx))
191 return NULL;
193 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocStringLen(str, len);
194 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
195 return NULL;
197 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
200 static unsigned push_instr(compiler_ctx_t *ctx, jsop_t op)
202 assert(ctx->code_size >= ctx->code_off);
204 if(ctx->code_size == ctx->code_off) {
205 instr_t *new_instrs;
207 new_instrs = heap_realloc(ctx->code->instrs, ctx->code_size*2*sizeof(instr_t));
208 if(!new_instrs)
209 return 0;
211 ctx->code->instrs = new_instrs;
212 ctx->code_size *= 2;
215 ctx->code->instrs[ctx->code_off].op = op;
216 return ctx->code_off++;
219 static inline instr_t *instr_ptr(compiler_ctx_t *ctx, unsigned off)
221 assert(off < ctx->code_off);
222 return ctx->code->instrs + off;
225 static HRESULT push_instr_int(compiler_ctx_t *ctx, jsop_t op, LONG arg)
227 unsigned instr;
229 instr = push_instr(ctx, op);
230 if(!instr)
231 return E_OUTOFMEMORY;
233 instr_ptr(ctx, instr)->u.arg->lng = arg;
234 return S_OK;
237 static HRESULT push_instr_str(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
239 unsigned instr;
240 jsstr_t *str;
242 str = compiler_alloc_string(ctx, arg);
243 if(!str)
244 return E_OUTOFMEMORY;
246 instr = push_instr(ctx, op);
247 if(!instr)
248 return E_OUTOFMEMORY;
250 instr_ptr(ctx, instr)->u.arg->str = str;
251 return S_OK;
254 static HRESULT push_instr_bstr(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
256 unsigned instr;
257 WCHAR *str;
259 str = compiler_alloc_bstr(ctx, arg);
260 if(!str)
261 return E_OUTOFMEMORY;
263 instr = push_instr(ctx, op);
264 if(!instr)
265 return E_OUTOFMEMORY;
267 instr_ptr(ctx, instr)->u.arg->bstr = str;
268 return S_OK;
271 static HRESULT push_instr_bstr_uint(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg1, unsigned arg2)
273 unsigned instr;
274 WCHAR *str;
276 str = compiler_alloc_bstr(ctx, arg1);
277 if(!str)
278 return E_OUTOFMEMORY;
280 instr = push_instr(ctx, op);
281 if(!instr)
282 return E_OUTOFMEMORY;
284 instr_ptr(ctx, instr)->u.arg[0].bstr = str;
285 instr_ptr(ctx, instr)->u.arg[1].uint = arg2;
286 return S_OK;
289 static HRESULT push_instr_uint_str(compiler_ctx_t *ctx, jsop_t op, unsigned arg1, const WCHAR *arg2)
291 unsigned instr;
292 jsstr_t *str;
294 str = compiler_alloc_string(ctx, arg2);
295 if(!str)
296 return E_OUTOFMEMORY;
298 instr = push_instr(ctx, op);
299 if(!instr)
300 return E_OUTOFMEMORY;
302 instr_ptr(ctx, instr)->u.arg[0].uint = arg1;
303 instr_ptr(ctx, instr)->u.arg[1].str = str;
304 return S_OK;
307 static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
309 unsigned instr;
311 instr = push_instr(ctx, op);
312 if(!instr)
313 return E_OUTOFMEMORY;
315 instr_ptr(ctx, instr)->u.dbl = arg;
316 return S_OK;
319 static inline void set_arg_uint(compiler_ctx_t *ctx, unsigned instr, unsigned arg)
321 instr_ptr(ctx, instr)->u.arg->uint = arg;
324 static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
326 unsigned instr;
328 instr = push_instr(ctx, op);
329 if(!instr)
330 return E_OUTOFMEMORY;
332 set_arg_uint(ctx, instr, arg);
333 return S_OK;
336 static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
338 HRESULT hres;
340 hres = compile_expression(ctx, expr->expression1);
341 if(FAILED(hres))
342 return hres;
344 hres = compile_expression(ctx, expr->expression2);
345 if(FAILED(hres))
346 return hres;
348 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
351 static HRESULT compile_unary_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op)
353 HRESULT hres;
355 hres = compile_expression(ctx, expr->expression);
356 if(FAILED(hres))
357 return hres;
359 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
362 /* ECMA-262 3rd Edition 11.2.1 */
363 static HRESULT compile_member_expression(compiler_ctx_t *ctx, member_expression_t *expr)
365 HRESULT hres;
367 hres = compile_expression(ctx, expr->expression);
368 if(FAILED(hres))
369 return hres;
371 return push_instr_bstr(ctx, OP_member, expr->identifier);
374 #define LABEL_FLAG 0x80000000
376 static unsigned alloc_label(compiler_ctx_t *ctx)
378 if(!ctx->labels_size) {
379 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
380 if(!ctx->labels)
381 return 0;
382 ctx->labels_size = 8;
383 }else if(ctx->labels_size == ctx->labels_cnt) {
384 unsigned *new_labels;
386 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
387 if(!new_labels)
388 return 0;
390 ctx->labels = new_labels;
391 ctx->labels_size *= 2;
394 return ctx->labels_cnt++ | LABEL_FLAG;
397 static void label_set_addr(compiler_ctx_t *ctx, unsigned label)
399 assert(label & LABEL_FLAG);
400 ctx->labels[label & ~LABEL_FLAG] = ctx->code_off;
403 static inline BOOL is_memberid_expr(expression_type_t type)
405 return type == EXPR_IDENT || type == EXPR_MEMBER || type == EXPR_ARRAY;
408 static HRESULT compile_memberid_expression(compiler_ctx_t *ctx, expression_t *expr, unsigned flags)
410 HRESULT hres = S_OK;
412 switch(expr->type) {
413 case EXPR_IDENT: {
414 identifier_expression_t *ident_expr = (identifier_expression_t*)expr;
416 hres = push_instr_bstr_uint(ctx, OP_identid, ident_expr->identifier, flags);
417 break;
419 case EXPR_ARRAY: {
420 binary_expression_t *array_expr = (binary_expression_t*)expr;
422 hres = compile_expression(ctx, array_expr->expression1);
423 if(FAILED(hres))
424 return hres;
426 hres = compile_expression(ctx, array_expr->expression2);
427 if(FAILED(hres))
428 return hres;
430 hres = push_instr_uint(ctx, OP_memberid, flags);
431 break;
433 case EXPR_MEMBER: {
434 member_expression_t *member_expr = (member_expression_t*)expr;
436 hres = compile_expression(ctx, member_expr->expression);
437 if(FAILED(hres))
438 return hres;
440 /* FIXME: Potential optimization */
441 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
442 if(FAILED(hres))
443 return hres;
445 hres = push_instr_uint(ctx, OP_memberid, flags);
446 break;
448 default:
449 assert(0);
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);
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)
477 HRESULT hres;
479 hres = compile_expression(ctx, expr->expression1);
480 if(FAILED(hres))
481 return hres;
483 if(!push_instr(ctx, OP_pop))
484 return E_OUTOFMEMORY;
486 return compile_expression(ctx, expr->expression2);
489 /* ECMA-262 3rd Edition 11.11 */
490 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
492 unsigned instr;
493 HRESULT hres;
495 hres = compile_expression(ctx, expr->expression1);
496 if(FAILED(hres))
497 return hres;
499 instr = push_instr(ctx, op);
500 if(!instr)
501 return E_OUTOFMEMORY;
503 hres = compile_expression(ctx, expr->expression2);
504 if(FAILED(hres))
505 return hres;
507 set_arg_uint(ctx, instr, ctx->code_off);
508 return S_OK;
511 /* ECMA-262 3rd Edition 11.12 */
512 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
514 unsigned jmp_false, jmp_end;
515 HRESULT hres;
517 hres = compile_expression(ctx, expr->expression);
518 if(FAILED(hres))
519 return hres;
521 jmp_false = push_instr(ctx, OP_cnd_z);
522 if(!jmp_false)
523 return E_OUTOFMEMORY;
525 hres = compile_expression(ctx, expr->true_expression);
526 if(FAILED(hres))
527 return hres;
529 jmp_end = push_instr(ctx, OP_jmp);
530 if(!jmp_end)
531 return E_OUTOFMEMORY;
533 set_arg_uint(ctx, jmp_false, ctx->code_off);
534 if(!push_instr(ctx, OP_pop))
535 return E_OUTOFMEMORY;
537 hres = compile_expression(ctx, expr->false_expression);
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);
552 if(FAILED(hres))
553 return hres;
555 for(arg = expr->argument_list; arg; arg = arg->next) {
556 hres = compile_expression(ctx, arg->expr);
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 *no_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);
581 if(FAILED(hres))
582 return hres;
584 for(arg = expr->argument_list; arg; arg = arg->next) {
585 hres = compile_expression(ctx, arg->expr);
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 = no_ret == NULL;
597 if(no_ret)
598 *no_ret = TRUE;
599 return S_OK;
602 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
604 HRESULT hres;
606 switch(expr->expression->type) {
607 case EXPR_ARRAY: {
608 binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
610 hres = compile_expression(ctx, array_expr->expression1);
611 if(FAILED(hres))
612 return hres;
614 hres = compile_expression(ctx, array_expr->expression2);
615 if(FAILED(hres))
616 return hres;
618 if(!push_instr(ctx, OP_delete))
619 return E_OUTOFMEMORY;
620 break;
622 case EXPR_MEMBER: {
623 member_expression_t *member_expr = (member_expression_t*)expr->expression;
625 hres = compile_expression(ctx, member_expr->expression);
626 if(FAILED(hres))
627 return hres;
629 /* FIXME: Potential optimization */
630 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
631 if(FAILED(hres))
632 return hres;
634 if(!push_instr(ctx, OP_delete))
635 return E_OUTOFMEMORY;
636 break;
638 case EXPR_IDENT:
639 return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
640 default: {
641 const WCHAR fixmeW[] = {'F','I','X','M','E',0};
643 WARN("invalid delete, unimplemented exception message\n");
645 hres = compile_expression(ctx, expr->expression);
646 if(FAILED(hres))
647 return hres;
649 return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, fixmeW);
653 return S_OK;
656 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
658 BOOL use_throw_path = FALSE;
659 unsigned arg_cnt = 0;
660 HRESULT hres;
662 if(expr->expression1->type == EXPR_CALL) {
663 call_expression_t *call_expr = (call_expression_t*)expr->expression1;
664 argument_t *arg;
666 if(op != OP_LAST) {
667 FIXME("op %d not supported on parametrized assign expressions\n", op);
668 return E_NOTIMPL;
671 if(is_memberid_expr(call_expr->expression->type) && call_expr->argument_list) {
672 hres = compile_memberid_expression(ctx, call_expr->expression, fdexNameEnsure);
673 if(FAILED(hres))
674 return hres;
676 for(arg = call_expr->argument_list; arg; arg = arg->next) {
677 hres = compile_expression(ctx, arg->expr);
678 if(FAILED(hres))
679 return hres;
680 arg_cnt++;
682 }else {
683 use_throw_path = TRUE;
685 }else if(is_memberid_expr(expr->expression1->type)) {
686 hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
687 if(FAILED(hres))
688 return hres;
689 }else {
690 use_throw_path = TRUE;
693 if(use_throw_path) {
694 /* Illegal assignment: evaluate and throw */
695 hres = compile_expression(ctx, expr->expression1);
696 if(FAILED(hres))
697 return hres;
699 hres = compile_expression(ctx, expr->expression2);
700 if(FAILED(hres))
701 return hres;
703 if(op != OP_LAST && !push_instr(ctx, op))
704 return E_OUTOFMEMORY;
706 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
709 if(op != OP_LAST && !push_instr(ctx, OP_refval))
710 return E_OUTOFMEMORY;
712 hres = compile_expression(ctx, expr->expression2);
713 if(FAILED(hres))
714 return hres;
716 if(op != OP_LAST && !push_instr(ctx, op))
717 return E_OUTOFMEMORY;
719 if(arg_cnt)
720 return push_instr_uint(ctx, OP_assign_call, arg_cnt);
722 if(!push_instr(ctx, OP_assign))
723 return E_OUTOFMEMORY;
725 return S_OK;
728 static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
730 jsop_t op;
731 HRESULT hres;
733 if(is_memberid_expr(expr->expression->type)) {
734 if(expr->expression->type == EXPR_IDENT)
735 return push_instr_bstr(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
737 op = OP_typeofid;
738 hres = compile_memberid_expression(ctx, expr->expression, 0);
739 }else {
740 op = OP_typeof;
741 hres = compile_expression(ctx, expr->expression);
743 if(FAILED(hres))
744 return hres;
746 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
749 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
751 switch(literal->type) {
752 case LT_BOOL:
753 return push_instr_int(ctx, OP_bool, literal->u.bval);
754 case LT_DOUBLE:
755 return push_instr_double(ctx, OP_double, literal->u.dval);
756 case LT_NULL:
757 return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
758 case LT_STRING:
759 return push_instr_str(ctx, OP_str, literal->u.wstr);
760 case LT_REGEXP: {
761 unsigned instr;
762 jsstr_t *str;
764 str = compiler_alloc_string_len(ctx, literal->u.regexp.str, literal->u.regexp.str_len);
765 if(!str)
766 return E_OUTOFMEMORY;
768 instr = push_instr(ctx, OP_regexp);
769 if(!instr)
770 return E_OUTOFMEMORY;
772 instr_ptr(ctx, instr)->u.arg[0].str = str;
773 instr_ptr(ctx, instr)->u.arg[1].uint = literal->u.regexp.flags;
774 return S_OK;
776 default:
777 assert(0);
778 return E_FAIL;
782 static HRESULT literal_as_bstr(compiler_ctx_t *ctx, literal_t *literal, BSTR *str)
784 switch(literal->type) {
785 case LT_STRING:
786 *str = compiler_alloc_bstr(ctx, literal->u.wstr);
787 break;
788 case LT_DOUBLE: {
789 jsstr_t *jsstr;
790 HRESULT hres;
792 hres = double_to_string(literal->u.dval, &jsstr);
793 if(FAILED(hres))
794 return hres;
796 *str = SysAllocStringLen(jsstr->str, jsstr_length(jsstr));
797 jsstr_release(jsstr);
798 break;
800 default:
801 assert(0);
804 return *str ? S_OK : E_OUTOFMEMORY;
807 static HRESULT compile_array_literal(compiler_ctx_t *ctx, array_literal_expression_t *expr)
809 unsigned i, elem_cnt = expr->length;
810 array_element_t *iter;
811 HRESULT hres;
813 for(iter = expr->element_list; iter; iter = iter->next) {
814 elem_cnt += iter->elision+1;
816 for(i=0; i < iter->elision; i++) {
817 if(!push_instr(ctx, OP_undefined))
818 return E_OUTOFMEMORY;
821 hres = compile_expression(ctx, iter->expr);
822 if(FAILED(hres))
823 return hres;
826 for(i=0; i < expr->length; i++) {
827 if(!push_instr(ctx, OP_undefined))
828 return E_OUTOFMEMORY;
831 return push_instr_uint(ctx, OP_carray, elem_cnt);
834 static HRESULT compile_object_literal(compiler_ctx_t *ctx, property_value_expression_t *expr)
836 prop_val_t *iter;
837 unsigned instr;
838 BSTR name;
839 HRESULT hres;
841 if(!push_instr(ctx, OP_new_obj))
842 return E_OUTOFMEMORY;
844 for(iter = expr->property_list; iter; iter = iter->next) {
845 hres = literal_as_bstr(ctx, iter->name, &name);
846 if(FAILED(hres))
847 return hres;
849 hres = compile_expression(ctx, iter->value);
850 if(FAILED(hres))
851 return hres;
853 instr = push_instr(ctx, OP_obj_prop);
854 if(!instr)
855 return E_OUTOFMEMORY;
857 instr_ptr(ctx, instr)->u.arg->bstr = name;
860 return S_OK;
863 static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr)
865 ctx->func_tail = ctx->func_tail ? (ctx->func_tail->next = expr) : (ctx->func_head = expr);
867 /* FIXME: not exactly right */
868 if(expr->identifier) {
869 ctx->func->func_cnt++;
870 return push_instr_bstr(ctx, OP_ident, expr->identifier);
873 return push_instr_uint(ctx, OP_func, ctx->func->func_cnt++);
876 static HRESULT compile_expression_noret(compiler_ctx_t *ctx, expression_t *expr, BOOL *no_ret)
878 switch(expr->type) {
879 case EXPR_ADD:
880 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
881 case EXPR_AND:
882 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_z);
883 case EXPR_ARRAY:
884 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
885 case EXPR_ARRAYLIT:
886 return compile_array_literal(ctx, (array_literal_expression_t*)expr);
887 case EXPR_ASSIGN:
888 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
889 case EXPR_ASSIGNADD:
890 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
891 case EXPR_ASSIGNAND:
892 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_and);
893 case EXPR_ASSIGNSUB:
894 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
895 case EXPR_ASSIGNMUL:
896 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
897 case EXPR_ASSIGNDIV:
898 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
899 case EXPR_ASSIGNMOD:
900 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
901 case EXPR_ASSIGNOR:
902 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
903 case EXPR_ASSIGNLSHIFT:
904 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_lshift);
905 case EXPR_ASSIGNRSHIFT:
906 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift);
907 case EXPR_ASSIGNRRSHIFT:
908 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
909 case EXPR_ASSIGNXOR:
910 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
911 case EXPR_BAND:
912 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
913 case EXPR_BITNEG:
914 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
915 case EXPR_BOR:
916 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
917 case EXPR_CALL:
918 return compile_call_expression(ctx, (call_expression_t*)expr, no_ret);
919 case EXPR_COMMA:
920 return compile_comma_expression(ctx, (binary_expression_t*)expr);
921 case EXPR_COND:
922 return compile_conditional_expression(ctx, (conditional_expression_t*)expr);
923 case EXPR_DELETE:
924 return compile_delete_expression(ctx, (unary_expression_t*)expr);
925 case EXPR_DIV:
926 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
927 case EXPR_EQ:
928 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
929 case EXPR_EQEQ:
930 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
931 case EXPR_FUNC:
932 return compile_function_expression(ctx, (function_expression_t*)expr);
933 case EXPR_GREATER:
934 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
935 case EXPR_GREATEREQ:
936 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
937 case EXPR_IDENT:
938 return push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
939 case EXPR_IN:
940 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
941 case EXPR_INSTANCEOF:
942 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
943 case EXPR_LESS:
944 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
945 case EXPR_LESSEQ:
946 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
947 case EXPR_LITERAL:
948 return compile_literal(ctx, ((literal_expression_t*)expr)->literal);
949 case EXPR_LOGNEG:
950 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
951 case EXPR_LSHIFT:
952 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
953 case EXPR_MEMBER:
954 return compile_member_expression(ctx, (member_expression_t*)expr);
955 case EXPR_MINUS:
956 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
957 case EXPR_MOD:
958 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
959 case EXPR_MUL:
960 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
961 case EXPR_NEW:
962 return compile_new_expression(ctx, (call_expression_t*)expr);
963 case EXPR_NOTEQ:
964 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
965 case EXPR_NOTEQEQ:
966 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
967 case EXPR_OR:
968 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_nz);
969 case EXPR_PLUS:
970 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
971 case EXPR_POSTDEC:
972 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
973 case EXPR_POSTINC:
974 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
975 case EXPR_PREDEC:
976 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
977 case EXPR_PREINC:
978 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
979 case EXPR_PROPVAL:
980 return compile_object_literal(ctx, (property_value_expression_t*)expr);
981 case EXPR_RSHIFT:
982 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
983 case EXPR_RRSHIFT:
984 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
985 case EXPR_SUB:
986 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
987 case EXPR_THIS:
988 return push_instr(ctx, OP_this) ? S_OK : E_OUTOFMEMORY;
989 case EXPR_TYPEOF:
990 return compile_typeof_expression(ctx, (unary_expression_t*)expr);
991 case EXPR_VOID:
992 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
993 case EXPR_BXOR:
994 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
995 default:
996 assert(0);
999 return S_OK;
1002 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr)
1004 return compile_expression_noret(ctx, expr, NULL);
1007 static inline BOOL is_loop_statement(statement_type_t type)
1009 return type == STAT_FOR || type == STAT_FORIN || type == STAT_WHILE;
1012 /* ECMA-262 3rd Edition 12.1 */
1013 static HRESULT compile_block_statement(compiler_ctx_t *ctx, statement_t *iter)
1015 HRESULT hres;
1017 /* FIXME: do it only if needed */
1018 if(!iter)
1019 return push_instr(ctx, OP_undefined) ? S_OK : E_OUTOFMEMORY;
1021 while(1) {
1022 hres = compile_statement(ctx, NULL, iter);
1023 if(FAILED(hres))
1024 return hres;
1026 iter = iter->next;
1027 if(!iter)
1028 break;
1030 if(!push_instr(ctx, OP_pop))
1031 return E_OUTOFMEMORY;
1034 return S_OK;
1037 /* ECMA-262 3rd Edition 12.2 */
1038 static HRESULT compile_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
1040 variable_declaration_t *iter;
1041 HRESULT hres;
1043 assert(list != NULL);
1045 if(ctx->var_tail)
1046 ctx->var_tail->global_next = list;
1047 else
1048 ctx->var_head = list;
1050 for(iter = list; iter; iter = iter->next) {
1051 ctx->func->var_cnt++;
1052 iter->global_next = iter->next;
1053 if(!iter->next)
1054 ctx->var_tail = iter;
1056 if(!iter->expr)
1057 continue;
1059 hres = compile_expression(ctx, iter->expr);
1060 if(FAILED(hres))
1061 return hres;
1063 hres = push_instr_bstr(ctx, OP_var_set, iter->identifier);
1064 if(FAILED(hres))
1065 return hres;
1068 return S_OK;
1071 /* ECMA-262 3rd Edition 12.2 */
1072 static HRESULT compile_var_statement(compiler_ctx_t *ctx, var_statement_t *stat)
1074 HRESULT hres;
1076 hres = compile_variable_list(ctx, stat->variable_list);
1077 if(FAILED(hres))
1078 return hres;
1080 return push_instr(ctx, OP_undefined) ? S_OK : E_OUTOFMEMORY;
1083 /* ECMA-262 3rd Edition 12.4 */
1084 static HRESULT compile_expression_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1086 BOOL no_ret = FALSE;
1087 HRESULT hres;
1089 hres = compile_expression_noret(ctx, stat->expr, &no_ret);
1090 if(FAILED(hres))
1091 return hres;
1093 /* FIXME: that's a big potential optimization */
1094 if(no_ret && !push_instr(ctx, OP_undefined))
1095 return E_OUTOFMEMORY;
1097 return S_OK;
1100 /* ECMA-262 3rd Edition 12.5 */
1101 static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat)
1103 unsigned jmp_else, jmp_end;
1104 HRESULT hres;
1106 hres = compile_expression(ctx, stat->expr);
1107 if(FAILED(hres))
1108 return hres;
1110 jmp_else = push_instr(ctx, OP_jmp_z);
1111 if(!jmp_else)
1112 return E_OUTOFMEMORY;
1114 hres = compile_statement(ctx, NULL, stat->if_stat);
1115 if(FAILED(hres))
1116 return hres;
1118 jmp_end = push_instr(ctx, OP_jmp);
1119 if(!jmp_end)
1120 return E_OUTOFMEMORY;
1122 set_arg_uint(ctx, jmp_else, ctx->code_off);
1124 if(stat->else_stat) {
1125 hres = compile_statement(ctx, NULL, stat->else_stat);
1126 if(FAILED(hres))
1127 return hres;
1128 }else {
1129 /* FIXME: We could sometimes avoid it */
1130 if(!push_instr(ctx, OP_undefined))
1131 return E_OUTOFMEMORY;
1134 set_arg_uint(ctx, jmp_end, ctx->code_off);
1135 return S_OK;
1138 /* ECMA-262 3rd Edition 12.6.2 */
1139 static HRESULT compile_while_statement(compiler_ctx_t *ctx, while_statement_t *stat)
1141 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1142 unsigned jmp_off;
1143 HRESULT hres;
1145 stat_ctx.break_label = alloc_label(ctx);
1146 if(!stat_ctx.break_label)
1147 return E_OUTOFMEMORY;
1149 stat_ctx.continue_label = alloc_label(ctx);
1150 if(!stat_ctx.continue_label)
1151 return E_OUTOFMEMORY;
1153 if(!stat->do_while) {
1154 /* FIXME: avoid */
1155 if(!push_instr(ctx, OP_undefined))
1156 return E_OUTOFMEMORY;
1158 jmp_off = ctx->code_off;
1159 label_set_addr(ctx, stat_ctx.continue_label);
1160 hres = compile_expression(ctx, stat->expr);
1161 if(FAILED(hres))
1162 return hres;
1164 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1165 if(FAILED(hres))
1166 return hres;
1168 if(!push_instr(ctx, OP_pop))
1169 return E_OUTOFMEMORY;
1170 }else {
1171 jmp_off = ctx->code_off;
1174 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1175 if(FAILED(hres))
1176 return hres;
1178 if(stat->do_while) {
1179 label_set_addr(ctx, stat_ctx.continue_label);
1180 hres = compile_expression(ctx, stat->expr);
1181 if(FAILED(hres))
1182 return hres;
1184 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1185 if(FAILED(hres))
1186 return hres;
1188 if(!push_instr(ctx, OP_pop))
1189 return E_OUTOFMEMORY;
1192 hres = push_instr_uint(ctx, OP_jmp, jmp_off);
1193 if(FAILED(hres))
1194 return hres;
1196 label_set_addr(ctx, stat_ctx.break_label);
1197 return S_OK;
1200 /* ECMA-262 3rd Edition 12.6.3 */
1201 static HRESULT compile_for_statement(compiler_ctx_t *ctx, for_statement_t *stat)
1203 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1204 unsigned expr_off;
1205 HRESULT hres;
1207 if(stat->variable_list) {
1208 hres = compile_variable_list(ctx, stat->variable_list);
1209 if(FAILED(hres))
1210 return hres;
1211 }else if(stat->begin_expr) {
1212 BOOL no_ret = FALSE;
1214 hres = compile_expression_noret(ctx, stat->begin_expr, &no_ret);
1215 if(FAILED(hres))
1216 return hres;
1217 if(!no_ret && !push_instr(ctx, OP_pop))
1218 return E_OUTOFMEMORY;
1221 stat_ctx.break_label = alloc_label(ctx);
1222 if(!stat_ctx.break_label)
1223 return E_OUTOFMEMORY;
1225 stat_ctx.continue_label = alloc_label(ctx);
1226 if(!stat_ctx.continue_label)
1227 return E_OUTOFMEMORY;
1229 /* FIXME: avoid */
1230 if(!push_instr(ctx, OP_undefined))
1231 return E_OUTOFMEMORY;
1233 expr_off = ctx->code_off;
1235 if(stat->expr) {
1236 hres = compile_expression(ctx, stat->expr);
1237 if(FAILED(hres))
1238 return hres;
1240 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1241 if(FAILED(hres))
1242 return hres;
1245 if(!push_instr(ctx, OP_pop))
1246 return E_OUTOFMEMORY;
1248 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1249 if(FAILED(hres))
1250 return hres;
1252 label_set_addr(ctx, stat_ctx.continue_label);
1254 if(stat->end_expr) {
1255 BOOL no_ret = FALSE;
1257 hres = compile_expression_noret(ctx, stat->end_expr, &no_ret);
1258 if(FAILED(hres))
1259 return hres;
1261 if(!no_ret && !push_instr(ctx, OP_pop))
1262 return E_OUTOFMEMORY;
1265 hres = push_instr_uint(ctx, OP_jmp, expr_off);
1266 if(FAILED(hres))
1267 return hres;
1269 label_set_addr(ctx, stat_ctx.break_label);
1270 return S_OK;
1273 /* ECMA-262 3rd Edition 12.6.4 */
1274 static HRESULT compile_forin_statement(compiler_ctx_t *ctx, forin_statement_t *stat)
1276 statement_ctx_t stat_ctx = {4, FALSE, FALSE};
1277 HRESULT hres;
1279 if(stat->variable) {
1280 hres = compile_variable_list(ctx, stat->variable);
1281 if(FAILED(hres))
1282 return hres;
1285 stat_ctx.break_label = alloc_label(ctx);
1286 if(!stat_ctx.break_label)
1287 return E_OUTOFMEMORY;
1289 stat_ctx.continue_label = alloc_label(ctx);
1290 if(!stat_ctx.continue_label)
1291 return E_OUTOFMEMORY;
1293 hres = compile_expression(ctx, stat->in_expr);
1294 if(FAILED(hres))
1295 return hres;
1297 if(stat->variable) {
1298 hres = push_instr_bstr_uint(ctx, OP_identid, stat->variable->identifier, fdexNameEnsure);
1299 if(FAILED(hres))
1300 return hres;
1301 }else if(is_memberid_expr(stat->expr->type)) {
1302 hres = compile_memberid_expression(ctx, stat->expr, fdexNameEnsure);
1303 if(FAILED(hres))
1304 return hres;
1305 }else {
1306 hres = push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
1307 if(FAILED(hres))
1308 return hres;
1310 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1311 return S_OK;
1314 hres = push_instr_int(ctx, OP_int, DISPID_STARTENUM);
1315 if(FAILED(hres))
1316 return hres;
1318 /* FIXME: avoid */
1319 if(!push_instr(ctx, OP_undefined))
1320 return E_OUTOFMEMORY;
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) {
1355 /* FIXME: optimize */
1356 while(stack_pop--) {
1357 if(!push_instr(ctx, OP_pop))
1358 return E_OUTOFMEMORY;
1363 return S_OK;
1366 /* ECMA-262 3rd Edition 12.7 */
1367 static HRESULT compile_continue_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1369 statement_ctx_t *pop_ctx;
1370 HRESULT hres;
1372 if(stat->identifier) {
1373 statement_t *label_stat;
1374 statement_ctx_t *iter;
1376 pop_ctx = NULL;
1378 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1379 if(iter->continue_label)
1380 pop_ctx = iter;
1381 if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier))
1382 break;
1385 if(!iter) {
1386 WARN("Label not found\n");
1387 return JS_E_LABEL_NOT_FOUND;
1390 /* Labelled continue are allowed only on loops */
1391 for(label_stat = iter->labelled_stat->statement;
1392 label_stat->type == STAT_LABEL;
1393 label_stat = ((labelled_statement_t*)label_stat)->statement);
1394 if(!is_loop_statement(label_stat->type)) {
1395 WARN("Label is not a loop\n");
1396 return JS_E_INVALID_CONTINUE;
1398 }else {
1399 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1400 if(pop_ctx->continue_label)
1401 break;
1404 if(!pop_ctx) {
1405 WARN("continue outside loop\n");
1406 return JS_E_INVALID_CONTINUE;
1410 hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx);
1411 if(FAILED(hres))
1412 return hres;
1414 if(!push_instr(ctx, OP_undefined))
1415 return E_OUTOFMEMORY;
1417 return push_instr_uint(ctx, OP_jmp, pop_ctx->continue_label);
1420 /* ECMA-262 3rd Edition 12.8 */
1421 static HRESULT compile_break_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1423 statement_ctx_t *pop_ctx;
1424 HRESULT hres;
1426 if(stat->identifier) {
1427 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1428 if(pop_ctx->labelled_stat && !strcmpW(pop_ctx->labelled_stat->identifier, stat->identifier)) {
1429 assert(pop_ctx->break_label);
1430 break;
1434 if(!pop_ctx) {
1435 WARN("Label not found\n");
1436 return JS_E_LABEL_NOT_FOUND;
1438 }else {
1439 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1440 if(pop_ctx->break_label && !pop_ctx->labelled_stat)
1441 break;
1444 if(!pop_ctx) {
1445 WARN("Break outside loop\n");
1446 return JS_E_INVALID_BREAK;
1450 hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx->next);
1451 if(FAILED(hres))
1452 return hres;
1454 if(!push_instr(ctx, OP_undefined))
1455 return E_OUTOFMEMORY;
1457 return push_instr_uint(ctx, OP_jmp, pop_ctx->break_label);
1460 /* ECMA-262 3rd Edition 12.9 */
1461 static HRESULT compile_return_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1463 HRESULT hres;
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);
1471 if(FAILED(hres))
1472 return hres;
1475 hres = pop_to_stat(ctx, FALSE, TRUE, NULL);
1476 if(FAILED(hres))
1477 return hres;
1479 return push_instr(ctx, OP_ret) ? S_OK : E_OUTOFMEMORY;
1482 /* ECMA-262 3rd Edition 12.10 */
1483 static HRESULT compile_with_statement(compiler_ctx_t *ctx, with_statement_t *stat)
1485 statement_ctx_t stat_ctx = {0, TRUE, FALSE};
1486 HRESULT hres;
1488 hres = compile_expression(ctx, stat->expr);
1489 if(FAILED(hres))
1490 return hres;
1492 if(!push_instr(ctx, OP_push_scope))
1493 return E_OUTOFMEMORY;
1495 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1496 if(FAILED(hres))
1497 return hres;
1499 if(!push_instr(ctx, OP_pop_scope))
1500 return E_OUTOFMEMORY;
1502 return S_OK;
1505 /* ECMA-262 3rd Edition 12.10 */
1506 static HRESULT compile_labelled_statement(compiler_ctx_t *ctx, labelled_statement_t *stat)
1508 statement_ctx_t stat_ctx = {0, FALSE, FALSE, 0, 0, stat}, *iter;
1509 HRESULT hres;
1511 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1512 if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier)) {
1513 WARN("Label %s redefined\n", debugstr_w(stat->identifier));
1514 return JS_E_LABEL_REDEFINED;
1518 /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1519 stat_ctx.break_label = alloc_label(ctx);
1520 if(!stat_ctx.break_label)
1521 return E_OUTOFMEMORY;
1523 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1524 if(FAILED(hres))
1525 return hres;
1527 label_set_addr(ctx, stat_ctx.break_label);
1528 return S_OK;
1531 /* ECMA-262 3rd Edition 12.13 */
1532 static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat)
1534 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1535 unsigned case_cnt = 0, *case_jmps, i, default_jmp;
1536 BOOL have_default = FALSE;
1537 statement_t *stat_iter;
1538 case_clausule_t *iter;
1539 HRESULT hres;
1541 hres = compile_expression(ctx, stat->expr);
1542 if(FAILED(hres))
1543 return hres;
1545 stat_ctx.break_label = alloc_label(ctx);
1546 if(!stat_ctx.break_label)
1547 return E_OUTOFMEMORY;
1549 for(iter = stat->case_list; iter; iter = iter->next) {
1550 if(iter->expr)
1551 case_cnt++;
1554 case_jmps = heap_alloc(case_cnt * sizeof(*case_jmps));
1555 if(!case_jmps)
1556 return E_OUTOFMEMORY;
1558 i = 0;
1559 for(iter = stat->case_list; iter; iter = iter->next) {
1560 if(!iter->expr) {
1561 have_default = TRUE;
1562 continue;
1565 hres = compile_expression(ctx, iter->expr);
1566 if(FAILED(hres))
1567 break;
1569 case_jmps[i] = push_instr(ctx, OP_case);
1570 if(!case_jmps[i]) {
1571 hres = E_OUTOFMEMORY;
1572 break;
1574 i++;
1577 if(SUCCEEDED(hres)) {
1578 if(push_instr(ctx, OP_pop)) {
1579 default_jmp = push_instr(ctx, OP_jmp);
1580 if(!default_jmp)
1581 hres = E_OUTOFMEMORY;
1582 }else {
1583 hres = E_OUTOFMEMORY;
1587 if(FAILED(hres)) {
1588 heap_free(case_jmps);
1589 return hres;
1592 i = 0;
1593 for(iter = stat->case_list; iter; iter = iter->next) {
1594 while(iter->next && iter->next->stat == iter->stat) {
1595 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1596 iter = iter->next;
1599 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1601 if(iter->stat) {
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(stat_iter->next && !push_instr(ctx, OP_pop)) {
1609 hres = E_OUTOFMEMORY;
1610 break;
1613 if(FAILED(hres))
1614 break;
1615 }else {
1616 if(!push_instr(ctx, OP_undefined)) {
1617 hres = E_OUTOFMEMORY;
1618 break;
1623 heap_free(case_jmps);
1624 if(FAILED(hres))
1625 return hres;
1626 assert(i == case_cnt);
1628 if(!have_default) {
1629 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.break_label);
1630 if(FAILED(hres))
1631 return hres;
1632 set_arg_uint(ctx, default_jmp, ctx->code_off);
1633 if(!push_instr(ctx, OP_undefined))
1634 return E_OUTOFMEMORY;
1637 label_set_addr(ctx, stat_ctx.break_label);
1638 return S_OK;
1641 /* ECMA-262 3rd Edition 12.13 */
1642 static HRESULT compile_throw_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1644 HRESULT hres;
1646 hres = compile_expression(ctx, stat->expr);
1647 if(FAILED(hres))
1648 return hres;
1650 return push_instr(ctx, OP_throw) ? S_OK : E_OUTOFMEMORY;
1653 /* ECMA-262 3rd Edition 12.14 */
1654 static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat)
1656 statement_ctx_t try_ctx = {0, FALSE, TRUE}, catch_ctx = {0, TRUE, FALSE};
1657 statement_ctx_t finally_ctx = {2, FALSE, FALSE};
1658 unsigned push_except;
1659 BSTR ident;
1660 HRESULT hres;
1662 push_except = push_instr(ctx, OP_push_except);
1663 if(!push_except)
1664 return E_OUTOFMEMORY;
1666 if(stat->catch_block) {
1667 ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier);
1668 if(!ident)
1669 return E_OUTOFMEMORY;
1670 }else {
1671 ident = NULL;
1674 instr_ptr(ctx, push_except)->u.arg[1].bstr = ident;
1676 if(!stat->catch_block)
1677 try_ctx.stack_use = 2;
1679 hres = compile_statement(ctx, &try_ctx, stat->try_statement);
1680 if(FAILED(hres))
1681 return hres;
1683 if(!push_instr(ctx, OP_pop_except))
1684 return E_OUTOFMEMORY;
1686 if(stat->catch_block) {
1687 unsigned jmp_finally;
1689 jmp_finally = push_instr(ctx, OP_jmp);
1690 if(!jmp_finally)
1691 return E_OUTOFMEMORY;
1693 instr_ptr(ctx, push_except)->u.arg[0].uint = ctx->code_off;
1695 hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement);
1696 if(FAILED(hres))
1697 return hres;
1699 if(!push_instr(ctx, OP_pop_scope))
1700 return E_OUTOFMEMORY;
1702 set_arg_uint(ctx, jmp_finally, ctx->code_off);
1703 }else {
1704 set_arg_uint(ctx, push_except, ctx->code_off);
1707 if(stat->finally_statement) {
1708 /* FIXME: avoid */
1709 if(!push_instr(ctx, OP_pop))
1710 return E_OUTOFMEMORY;
1712 hres = compile_statement(ctx, stat->catch_block ? NULL : &finally_ctx, stat->finally_statement);
1713 if(FAILED(hres))
1714 return hres;
1716 if(!stat->catch_block && !push_instr(ctx, OP_end_finally))
1717 return E_OUTOFMEMORY;
1720 return S_OK;
1723 static HRESULT compile_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1725 HRESULT hres;
1727 if(stat_ctx) {
1728 stat_ctx->next = ctx->stat_ctx;
1729 ctx->stat_ctx = stat_ctx;
1732 switch(stat->type) {
1733 case STAT_BLOCK:
1734 hres = compile_block_statement(ctx, ((block_statement_t*)stat)->stat_list);
1735 break;
1736 case STAT_BREAK:
1737 hres = compile_break_statement(ctx, (branch_statement_t*)stat);
1738 break;
1739 case STAT_CONTINUE:
1740 hres = compile_continue_statement(ctx, (branch_statement_t*)stat);
1741 break;
1742 case STAT_EMPTY:
1743 hres = push_instr(ctx, OP_undefined) ? S_OK : E_OUTOFMEMORY; /* FIXME */
1744 break;
1745 case STAT_EXPR:
1746 hres = compile_expression_statement(ctx, (expression_statement_t*)stat);
1747 break;
1748 case STAT_FOR:
1749 hres = compile_for_statement(ctx, (for_statement_t*)stat);
1750 break;
1751 case STAT_FORIN:
1752 hres = compile_forin_statement(ctx, (forin_statement_t*)stat);
1753 break;
1754 case STAT_IF:
1755 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1756 break;
1757 case STAT_LABEL:
1758 hres = compile_labelled_statement(ctx, (labelled_statement_t*)stat);
1759 break;
1760 case STAT_RETURN:
1761 hres = compile_return_statement(ctx, (expression_statement_t*)stat);
1762 break;
1763 case STAT_SWITCH:
1764 hres = compile_switch_statement(ctx, (switch_statement_t*)stat);
1765 break;
1766 case STAT_THROW:
1767 hres = compile_throw_statement(ctx, (expression_statement_t*)stat);
1768 break;
1769 case STAT_TRY:
1770 hres = compile_try_statement(ctx, (try_statement_t*)stat);
1771 break;
1772 case STAT_VAR:
1773 hres = compile_var_statement(ctx, (var_statement_t*)stat);
1774 break;
1775 case STAT_WHILE:
1776 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1777 break;
1778 case STAT_WITH:
1779 hres = compile_with_statement(ctx, (with_statement_t*)stat);
1780 break;
1781 default:
1782 assert(0);
1783 hres = E_FAIL;
1786 if(stat_ctx) {
1787 assert(ctx->stat_ctx == stat_ctx);
1788 ctx->stat_ctx = stat_ctx->next;
1791 return hres;
1794 static void resolve_labels(compiler_ctx_t *ctx, unsigned off)
1796 instr_t *instr;
1798 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
1799 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->u.arg->uint & LABEL_FLAG)) {
1800 assert((instr->u.arg->uint & ~LABEL_FLAG) < ctx->labels_cnt);
1801 instr->u.arg->uint = ctx->labels[instr->u.arg->uint & ~LABEL_FLAG];
1803 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
1806 ctx->labels_cnt = 0;
1809 void release_bytecode(bytecode_t *code)
1811 unsigned i;
1813 if(--code->ref)
1814 return;
1816 for(i=0; i < code->bstr_cnt; i++)
1817 SysFreeString(code->bstr_pool[i]);
1818 for(i=0; i < code->str_cnt; i++)
1819 jsstr_release(code->str_pool[i]);
1821 heap_free(code->source);
1822 jsheap_free(&code->heap);
1823 heap_free(code->bstr_pool);
1824 heap_free(code->str_pool);
1825 heap_free(code->instrs);
1826 heap_free(code);
1829 static HRESULT init_code(compiler_ctx_t *compiler, const WCHAR *source)
1831 compiler->code = heap_alloc_zero(sizeof(bytecode_t));
1832 if(!compiler->code)
1833 return E_OUTOFMEMORY;
1835 compiler->code->ref = 1;
1836 jsheap_init(&compiler->code->heap);
1838 compiler->code->source = heap_strdupW(source);
1839 if(!compiler->code->source) {
1840 release_bytecode(compiler->code);
1841 return E_OUTOFMEMORY;
1844 compiler->code->instrs = heap_alloc(64 * sizeof(instr_t));
1845 if(!compiler->code->instrs) {
1846 release_bytecode(compiler->code);
1847 return E_OUTOFMEMORY;
1850 compiler->code_size = 64;
1851 compiler->code_off = 1;
1852 return S_OK;
1855 static HRESULT compile_function(compiler_ctx_t *ctx, source_elements_t *source, function_expression_t *func_expr,
1856 BOOL from_eval, function_code_t *func)
1858 variable_declaration_t *var_iter;
1859 function_expression_t *iter;
1860 unsigned off, i;
1861 HRESULT hres;
1863 TRACE("\n");
1865 ctx->var_head = ctx->var_tail = NULL;
1866 ctx->func_head = ctx->func_tail = NULL;
1868 off = ctx->code_off;
1869 ctx->func = func;
1870 hres = compile_block_statement(ctx, source->statement);
1871 if(FAILED(hres))
1872 return hres;
1874 resolve_labels(ctx, off);
1876 if(!from_eval && !push_instr(ctx, OP_pop))
1877 return E_OUTOFMEMORY;
1878 if(!push_instr(ctx, OP_ret))
1879 return E_OUTOFMEMORY;
1881 if(TRACE_ON(jscript_disas))
1882 dump_code(ctx, off);
1884 func->instr_off = off;
1886 if(func_expr && func_expr->identifier) {
1887 func->name = compiler_alloc_bstr(ctx, func_expr->identifier);
1888 if(!func->name)
1889 return E_OUTOFMEMORY;
1892 if(func_expr) {
1893 parameter_t *param_iter;
1895 func->source = func_expr->src_str;
1896 func->source_len = func_expr->src_len;
1898 for(param_iter = func_expr->parameter_list; param_iter; param_iter = param_iter->next)
1899 func->param_cnt++;
1901 func->params = compiler_alloc(ctx->code, func->param_cnt * sizeof(*func->params));
1902 if(!func->params)
1903 return E_OUTOFMEMORY;
1905 for(param_iter = func_expr->parameter_list, i=0; param_iter; param_iter = param_iter->next, i++) {
1906 func->params[i] = compiler_alloc_bstr(ctx, param_iter->identifier);
1907 if(!func->params[i])
1908 return E_OUTOFMEMORY;
1912 func->variables = compiler_alloc(ctx->code, func->var_cnt * sizeof(*func->variables));
1913 if(!func->variables)
1914 return E_OUTOFMEMORY;
1916 for(var_iter = ctx->var_head, i=0; var_iter; var_iter = var_iter->global_next, i++) {
1917 func->variables[i] = compiler_alloc_bstr(ctx, var_iter->identifier);
1918 if(!func->variables[i])
1919 return E_OUTOFMEMORY;
1922 assert(i == func->var_cnt);
1924 func->funcs = compiler_alloc(ctx->code, func->func_cnt * sizeof(*func->funcs));
1925 if(!func->funcs)
1926 return E_OUTOFMEMORY;
1927 memset(func->funcs, 0, func->func_cnt * sizeof(*func->funcs));
1929 for(iter = ctx->func_head, i=0; iter; iter = iter->next, i++) {
1930 hres = compile_function(ctx, iter->source_elements, iter, FALSE, func->funcs+i);
1931 if(FAILED(hres))
1932 return hres;
1935 assert(i == func->func_cnt);
1937 return S_OK;
1940 static HRESULT parse_arguments(compiler_ctx_t *ctx, const WCHAR *args, BSTR *arg_array, unsigned *args_size)
1942 const WCHAR *ptr = args, *ptr2;
1943 unsigned arg_cnt = 0;
1945 while(isspaceW(*ptr))
1946 ptr++;
1947 if(!*ptr) {
1948 if(args_size)
1949 *args_size = 0;
1950 return S_OK;
1953 while(1) {
1954 if(!isalphaW(*ptr) && *ptr != '_') {
1955 FIXME("expected alpha or '_': %s\n", debugstr_w(ptr));
1956 return E_FAIL;
1959 ptr2 = ptr;
1960 while(isalnumW(*ptr) || *ptr == '_')
1961 ptr++;
1963 if(*ptr && *ptr != ',' && !isspaceW(*ptr)) {
1964 FIXME("unexpected har %s\n", debugstr_w(ptr));
1965 return E_FAIL;
1968 if(arg_array) {
1969 arg_array[arg_cnt] = compiler_alloc_bstr_len(ctx, ptr2, ptr-ptr2);
1970 if(!arg_array[arg_cnt])
1971 return E_OUTOFMEMORY;
1973 arg_cnt++;
1975 while(isspaceW(*ptr))
1976 ptr++;
1977 if(!*ptr)
1978 break;
1979 if(*ptr != ',') {
1980 FIXME("expected ',': %s\n", debugstr_w(ptr));
1981 return E_FAIL;
1984 ptr++;
1985 while(isspaceW(*ptr))
1986 ptr++;
1989 if(args_size)
1990 *args_size = arg_cnt;
1991 return S_OK;
1994 static HRESULT compile_arguments(compiler_ctx_t *ctx, const WCHAR *args)
1996 HRESULT hres;
1998 hres = parse_arguments(ctx, args, NULL, &ctx->code->global_code.param_cnt);
1999 if(FAILED(hres))
2000 return hres;
2002 ctx->code->global_code.params = compiler_alloc(ctx->code,
2003 ctx->code->global_code.param_cnt * sizeof(*ctx->code->global_code.params));
2004 if(!ctx->code->global_code.params)
2005 return E_OUTOFMEMORY;
2007 return parse_arguments(ctx, args, ctx->code->global_code.params, NULL);
2010 HRESULT compile_script(script_ctx_t *ctx, const WCHAR *code, const WCHAR *args, const WCHAR *delimiter,
2011 BOOL from_eval, BOOL use_decode, bytecode_t **ret)
2013 compiler_ctx_t compiler = {0};
2014 HRESULT hres;
2016 hres = init_code(&compiler, code);
2017 if(FAILED(hres))
2018 return hres;
2020 if(args) {
2021 hres = compile_arguments(&compiler, args);
2022 if(FAILED(hres))
2023 return hres;
2026 if(use_decode) {
2027 hres = decode_source(compiler.code->source);
2028 if(FAILED(hres)) {
2029 WARN("Decoding failed\n");
2030 return hres;
2034 hres = script_parse(ctx, compiler.code->source, delimiter, from_eval, &compiler.parser);
2035 if(FAILED(hres)) {
2036 release_bytecode(compiler.code);
2037 return hres;
2040 hres = compile_function(&compiler, compiler.parser->source, NULL, from_eval, &compiler.code->global_code);
2041 parser_release(compiler.parser);
2042 if(FAILED(hres)) {
2043 release_bytecode(compiler.code);
2044 return hres;
2047 *ret = compiler.code;
2048 return S_OK;