dwrite: Implement CreateFontFaceFromHdc().
[wine.git] / dlls / jscript / compile.c
blobf056be1527588bbc78e9995dd5f35cb205a014b1
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"
24 #include "parser.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
29 WINE_DECLARE_DEBUG_CHANNEL(jscript_disas);
31 typedef struct _statement_ctx_t {
32 unsigned stack_use;
33 BOOL using_scope;
34 BOOL using_except;
36 unsigned break_label;
37 unsigned continue_label;
39 const labelled_statement_t *labelled_stat;
41 struct _statement_ctx_t *next;
42 } statement_ctx_t;
44 typedef struct {
45 parser_ctx_t *parser;
46 bytecode_t *code;
48 BOOL from_eval;
50 unsigned code_off;
51 unsigned code_size;
53 unsigned *labels;
54 unsigned labels_size;
55 unsigned labels_cnt;
57 statement_ctx_t *stat_ctx;
58 function_code_t *func;
60 variable_declaration_t *var_head;
61 variable_declaration_t *var_tail;
63 function_expression_t *func_head;
64 function_expression_t *func_tail;
65 } compiler_ctx_t;
67 static const struct {
68 const char *op_str;
69 instr_arg_type_t arg1_type;
70 instr_arg_type_t arg2_type;
71 } instr_info[] = {
72 #define X(n,a,b,c) {#n,b,c},
73 OP_LIST
74 #undef X
77 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
79 switch(type) {
80 case ARG_STR:
81 TRACE_(jscript_disas)("\t%s", debugstr_jsstr(arg->str));
82 break;
83 case ARG_BSTR:
84 TRACE_(jscript_disas)("\t%s", debugstr_wn(arg->bstr, SysStringLen(arg->bstr)));
85 break;
86 case ARG_INT:
87 TRACE_(jscript_disas)("\t%d", arg->uint);
88 break;
89 case ARG_UINT:
90 case ARG_ADDR:
91 TRACE_(jscript_disas)("\t%u", arg->uint);
92 break;
93 case ARG_FUNC:
94 case ARG_NONE:
95 break;
96 DEFAULT_UNREACHABLE;
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 heap_pool_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_UNREACHABLE;
453 return hres;
456 static HRESULT compile_increment_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op, int n)
458 HRESULT hres;
460 if(!is_memberid_expr(expr->expression->type)) {
461 hres = compile_expression(ctx, expr->expression, TRUE);
462 if(FAILED(hres))
463 return hres;
465 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
468 hres = compile_memberid_expression(ctx, expr->expression, fdexNameEnsure);
469 if(FAILED(hres))
470 return hres;
472 return push_instr_int(ctx, op, n);
475 /* ECMA-262 3rd Edition 11.14 */
476 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr, BOOL emit_ret)
478 HRESULT hres;
480 hres = compile_expression(ctx, expr->expression1, FALSE);
481 if(FAILED(hres))
482 return hres;
484 return compile_expression(ctx, expr->expression2, emit_ret);
487 /* ECMA-262 3rd Edition 11.11 */
488 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
490 unsigned instr;
491 HRESULT hres;
493 hres = compile_expression(ctx, expr->expression1, TRUE);
494 if(FAILED(hres))
495 return hres;
497 instr = push_instr(ctx, op);
498 if(!instr)
499 return E_OUTOFMEMORY;
501 hres = compile_expression(ctx, expr->expression2, TRUE);
502 if(FAILED(hres))
503 return hres;
505 set_arg_uint(ctx, instr, ctx->code_off);
506 return S_OK;
509 /* ECMA-262 3rd Edition 11.12 */
510 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
512 unsigned jmp_false, jmp_end;
513 HRESULT hres;
515 hres = compile_expression(ctx, expr->expression, TRUE);
516 if(FAILED(hres))
517 return hres;
519 jmp_false = push_instr(ctx, OP_cnd_z);
520 if(!jmp_false)
521 return E_OUTOFMEMORY;
523 hres = compile_expression(ctx, expr->true_expression, TRUE);
524 if(FAILED(hres))
525 return hres;
527 jmp_end = push_instr(ctx, OP_jmp);
528 if(!jmp_end)
529 return E_OUTOFMEMORY;
531 set_arg_uint(ctx, jmp_false, ctx->code_off);
532 hres = push_instr_uint(ctx, OP_pop, 1);
533 if(FAILED(hres))
534 return hres;
536 hres = compile_expression(ctx, expr->false_expression, TRUE);
537 if(FAILED(hres))
538 return hres;
540 set_arg_uint(ctx, jmp_end, ctx->code_off);
541 return S_OK;
544 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
546 unsigned arg_cnt = 0;
547 argument_t *arg;
548 HRESULT hres;
550 hres = compile_expression(ctx, expr->expression, TRUE);
551 if(FAILED(hres))
552 return hres;
554 for(arg = expr->argument_list; arg; arg = arg->next) {
555 hres = compile_expression(ctx, arg->expr, TRUE);
556 if(FAILED(hres))
557 return hres;
558 arg_cnt++;
561 return push_instr_uint(ctx, OP_new, arg_cnt);
564 static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL emit_ret)
566 unsigned arg_cnt = 0;
567 argument_t *arg;
568 unsigned instr;
569 jsop_t op;
570 HRESULT hres;
572 if(is_memberid_expr(expr->expression->type)) {
573 op = OP_call_member;
574 hres = compile_memberid_expression(ctx, expr->expression, 0);
575 }else {
576 op = OP_call;
577 hres = compile_expression(ctx, expr->expression, TRUE);
580 if(FAILED(hres))
581 return hres;
583 for(arg = expr->argument_list; arg; arg = arg->next) {
584 hres = compile_expression(ctx, arg->expr, TRUE);
585 if(FAILED(hres))
586 return hres;
587 arg_cnt++;
590 instr = push_instr(ctx, op);
591 if(!instr)
592 return E_OUTOFMEMORY;
594 instr_ptr(ctx, instr)->u.arg[0].uint = arg_cnt;
595 instr_ptr(ctx, instr)->u.arg[1].lng = emit_ret;
596 return S_OK;
599 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
601 HRESULT hres;
603 switch(expr->expression->type) {
604 case EXPR_ARRAY: {
605 binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
607 hres = compile_expression(ctx, array_expr->expression1, TRUE);
608 if(FAILED(hres))
609 return hres;
611 hres = compile_expression(ctx, array_expr->expression2, TRUE);
612 if(FAILED(hres))
613 return hres;
615 if(!push_instr(ctx, OP_delete))
616 return E_OUTOFMEMORY;
617 break;
619 case EXPR_MEMBER: {
620 member_expression_t *member_expr = (member_expression_t*)expr->expression;
622 hres = compile_expression(ctx, member_expr->expression, TRUE);
623 if(FAILED(hres))
624 return hres;
626 /* FIXME: Potential optimization */
627 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
628 if(FAILED(hres))
629 return hres;
631 if(!push_instr(ctx, OP_delete))
632 return E_OUTOFMEMORY;
633 break;
635 case EXPR_IDENT:
636 return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
637 default: {
638 const WCHAR fixmeW[] = {'F','I','X','M','E',0};
640 WARN("invalid delete, unimplemented exception message\n");
642 hres = compile_expression(ctx, expr->expression, TRUE);
643 if(FAILED(hres))
644 return hres;
646 return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, fixmeW);
650 return S_OK;
653 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
655 BOOL use_throw_path = FALSE;
656 unsigned arg_cnt = 0;
657 HRESULT hres;
659 if(expr->expression1->type == EXPR_CALL) {
660 call_expression_t *call_expr = (call_expression_t*)expr->expression1;
661 argument_t *arg;
663 if(op != OP_LAST) {
664 FIXME("op %d not supported on parametrized assign expressions\n", op);
665 return E_NOTIMPL;
668 if(is_memberid_expr(call_expr->expression->type) && call_expr->argument_list) {
669 hres = compile_memberid_expression(ctx, call_expr->expression, fdexNameEnsure);
670 if(FAILED(hres))
671 return hres;
673 for(arg = call_expr->argument_list; arg; arg = arg->next) {
674 hres = compile_expression(ctx, arg->expr, TRUE);
675 if(FAILED(hres))
676 return hres;
677 arg_cnt++;
679 }else {
680 use_throw_path = TRUE;
682 }else if(is_memberid_expr(expr->expression1->type)) {
683 hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
684 if(FAILED(hres))
685 return hres;
686 }else {
687 use_throw_path = TRUE;
690 if(use_throw_path) {
691 /* Illegal assignment: evaluate and throw */
692 hres = compile_expression(ctx, expr->expression1, TRUE);
693 if(FAILED(hres))
694 return hres;
696 hres = compile_expression(ctx, expr->expression2, TRUE);
697 if(FAILED(hres))
698 return hres;
700 if(op != OP_LAST && !push_instr(ctx, op))
701 return E_OUTOFMEMORY;
703 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
706 if(op != OP_LAST && !push_instr(ctx, OP_refval))
707 return E_OUTOFMEMORY;
709 hres = compile_expression(ctx, expr->expression2, TRUE);
710 if(FAILED(hres))
711 return hres;
713 if(op != OP_LAST && !push_instr(ctx, op))
714 return E_OUTOFMEMORY;
716 if(arg_cnt)
717 return push_instr_uint(ctx, OP_assign_call, arg_cnt);
719 if(!push_instr(ctx, OP_assign))
720 return E_OUTOFMEMORY;
722 return S_OK;
725 static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
727 jsop_t op;
728 HRESULT hres;
730 if(is_memberid_expr(expr->expression->type)) {
731 if(expr->expression->type == EXPR_IDENT)
732 return push_instr_bstr(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
734 op = OP_typeofid;
735 hres = compile_memberid_expression(ctx, expr->expression, 0);
736 }else {
737 op = OP_typeof;
738 hres = compile_expression(ctx, expr->expression, TRUE);
740 if(FAILED(hres))
741 return hres;
743 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
746 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
748 switch(literal->type) {
749 case LT_BOOL:
750 return push_instr_int(ctx, OP_bool, literal->u.bval);
751 case LT_DOUBLE:
752 return push_instr_double(ctx, OP_double, literal->u.dval);
753 case LT_NULL:
754 return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
755 case LT_STRING:
756 return push_instr_str(ctx, OP_str, literal->u.wstr);
757 case LT_REGEXP: {
758 unsigned instr;
759 jsstr_t *str;
761 str = compiler_alloc_string_len(ctx, literal->u.regexp.str, literal->u.regexp.str_len);
762 if(!str)
763 return E_OUTOFMEMORY;
765 instr = push_instr(ctx, OP_regexp);
766 if(!instr)
767 return E_OUTOFMEMORY;
769 instr_ptr(ctx, instr)->u.arg[0].str = str;
770 instr_ptr(ctx, instr)->u.arg[1].uint = literal->u.regexp.flags;
771 return S_OK;
773 DEFAULT_UNREACHABLE;
775 return E_FAIL;
778 static HRESULT literal_as_bstr(compiler_ctx_t *ctx, literal_t *literal, BSTR *str)
780 switch(literal->type) {
781 case LT_STRING:
782 *str = compiler_alloc_bstr(ctx, literal->u.wstr);
783 break;
784 case LT_DOUBLE: {
785 jsstr_t *jsstr;
786 HRESULT hres;
788 hres = double_to_string(literal->u.dval, &jsstr);
789 if(FAILED(hres))
790 return hres;
792 *str = compiler_alloc_bstr_len(ctx, NULL, jsstr_length(jsstr));
793 if(*str)
794 jsstr_flush(jsstr, *str);
795 jsstr_release(jsstr);
796 break;
798 DEFAULT_UNREACHABLE;
801 return *str ? S_OK : E_OUTOFMEMORY;
804 static HRESULT compile_array_literal(compiler_ctx_t *ctx, array_literal_expression_t *expr)
806 unsigned i, elem_cnt = expr->length;
807 array_element_t *iter;
808 HRESULT hres;
810 for(iter = expr->element_list; iter; iter = iter->next) {
811 elem_cnt += iter->elision+1;
813 for(i=0; i < iter->elision; i++) {
814 if(!push_instr(ctx, OP_undefined))
815 return E_OUTOFMEMORY;
818 hres = compile_expression(ctx, iter->expr, TRUE);
819 if(FAILED(hres))
820 return hres;
823 for(i=0; i < expr->length; i++) {
824 if(!push_instr(ctx, OP_undefined))
825 return E_OUTOFMEMORY;
828 return push_instr_uint(ctx, OP_carray, elem_cnt);
831 static HRESULT compile_object_literal(compiler_ctx_t *ctx, property_value_expression_t *expr)
833 prop_val_t *iter;
834 unsigned instr;
835 BSTR name;
836 HRESULT hres;
838 if(!push_instr(ctx, OP_new_obj))
839 return E_OUTOFMEMORY;
841 for(iter = expr->property_list; iter; iter = iter->next) {
842 hres = literal_as_bstr(ctx, iter->name, &name);
843 if(FAILED(hres))
844 return hres;
846 hres = compile_expression(ctx, iter->value, TRUE);
847 if(FAILED(hres))
848 return hres;
850 instr = push_instr(ctx, OP_obj_prop);
851 if(!instr)
852 return E_OUTOFMEMORY;
854 instr_ptr(ctx, instr)->u.arg->bstr = name;
857 return S_OK;
860 static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr)
862 ctx->func_tail = ctx->func_tail ? (ctx->func_tail->next = expr) : (ctx->func_head = expr);
864 /* FIXME: not exactly right */
865 if(expr->identifier) {
866 ctx->func->func_cnt++;
867 return push_instr_bstr(ctx, OP_ident, expr->identifier);
870 return push_instr_uint(ctx, OP_func, ctx->func->func_cnt++);
873 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr, BOOL emit_ret)
875 HRESULT hres;
877 switch(expr->type) {
878 case EXPR_ADD:
879 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
880 break;
881 case EXPR_AND:
882 hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_z);
883 break;
884 case EXPR_ARRAY:
885 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
886 break;
887 case EXPR_ARRAYLIT:
888 hres = compile_array_literal(ctx, (array_literal_expression_t*)expr);
889 break;
890 case EXPR_ASSIGN:
891 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
892 break;
893 case EXPR_ASSIGNADD:
894 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
895 break;
896 case EXPR_ASSIGNAND:
897 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_and);
898 break;
899 case EXPR_ASSIGNSUB:
900 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
901 break;
902 case EXPR_ASSIGNMUL:
903 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
904 break;
905 case EXPR_ASSIGNDIV:
906 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
907 break;
908 case EXPR_ASSIGNMOD:
909 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
910 break;
911 case EXPR_ASSIGNOR:
912 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
913 break;
914 case EXPR_ASSIGNLSHIFT:
915 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_lshift);
916 break;
917 case EXPR_ASSIGNRSHIFT:
918 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift);
919 break;
920 case EXPR_ASSIGNRRSHIFT:
921 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
922 break;
923 case EXPR_ASSIGNXOR:
924 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
925 break;
926 case EXPR_BAND:
927 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
928 break;
929 case EXPR_BITNEG:
930 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
931 break;
932 case EXPR_BOR:
933 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
934 break;
935 case EXPR_CALL:
936 return compile_call_expression(ctx, (call_expression_t*)expr, emit_ret);
937 case EXPR_COMMA:
938 return compile_comma_expression(ctx, (binary_expression_t*)expr, emit_ret);
939 case EXPR_COND:
940 hres = compile_conditional_expression(ctx, (conditional_expression_t*)expr);
941 break;
942 case EXPR_DELETE:
943 hres = compile_delete_expression(ctx, (unary_expression_t*)expr);
944 break;
945 case EXPR_DIV:
946 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
947 break;
948 case EXPR_EQ:
949 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
950 break;
951 case EXPR_EQEQ:
952 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
953 break;
954 case EXPR_FUNC:
955 hres = compile_function_expression(ctx, (function_expression_t*)expr);
956 break;
957 case EXPR_GREATER:
958 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
959 break;
960 case EXPR_GREATEREQ:
961 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
962 break;
963 case EXPR_IDENT:
964 hres = push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
965 break;
966 case EXPR_IN:
967 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
968 break;
969 case EXPR_INSTANCEOF:
970 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
971 break;
972 case EXPR_LESS:
973 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
974 break;
975 case EXPR_LESSEQ:
976 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
977 break;
978 case EXPR_LITERAL:
979 hres = compile_literal(ctx, ((literal_expression_t*)expr)->literal);
980 break;
981 case EXPR_LOGNEG:
982 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
983 break;
984 case EXPR_LSHIFT:
985 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
986 break;
987 case EXPR_MEMBER:
988 hres = compile_member_expression(ctx, (member_expression_t*)expr);
989 break;
990 case EXPR_MINUS:
991 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
992 break;
993 case EXPR_MOD:
994 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
995 break;
996 case EXPR_MUL:
997 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
998 break;
999 case EXPR_NEW:
1000 hres = compile_new_expression(ctx, (call_expression_t*)expr);
1001 break;
1002 case EXPR_NOTEQ:
1003 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
1004 break;
1005 case EXPR_NOTEQEQ:
1006 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
1007 break;
1008 case EXPR_OR:
1009 hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_nz);
1010 break;
1011 case EXPR_PLUS:
1012 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
1013 break;
1014 case EXPR_POSTDEC:
1015 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
1016 break;
1017 case EXPR_POSTINC:
1018 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
1019 break;
1020 case EXPR_PREDEC:
1021 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
1022 break;
1023 case EXPR_PREINC:
1024 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
1025 break;
1026 case EXPR_PROPVAL:
1027 hres = compile_object_literal(ctx, (property_value_expression_t*)expr);
1028 break;
1029 case EXPR_RSHIFT:
1030 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
1031 break;
1032 case EXPR_RRSHIFT:
1033 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
1034 break;
1035 case EXPR_SUB:
1036 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
1037 break;
1038 case EXPR_THIS:
1039 return !emit_ret || push_instr(ctx, OP_this) ? S_OK : E_OUTOFMEMORY;
1040 case EXPR_TYPEOF:
1041 hres = compile_typeof_expression(ctx, (unary_expression_t*)expr);
1042 break;
1043 case EXPR_VOID:
1044 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
1045 break;
1046 case EXPR_BXOR:
1047 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
1048 break;
1049 DEFAULT_UNREACHABLE;
1052 if(FAILED(hres))
1053 return hres;
1055 return emit_ret ? S_OK : push_instr_uint(ctx, OP_pop, 1);
1058 static inline BOOL is_loop_statement(statement_type_t type)
1060 return type == STAT_FOR || type == STAT_FORIN || type == STAT_WHILE;
1063 /* ECMA-262 3rd Edition 12.1 */
1064 static HRESULT compile_block_statement(compiler_ctx_t *ctx, statement_t *iter)
1066 HRESULT hres;
1068 while(iter) {
1069 hres = compile_statement(ctx, NULL, iter);
1070 if(FAILED(hres))
1071 return hres;
1073 iter = iter->next;
1076 return S_OK;
1079 /* ECMA-262 3rd Edition 12.2 */
1080 static HRESULT compile_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
1082 variable_declaration_t *iter;
1083 HRESULT hres;
1085 assert(list != NULL);
1087 if(ctx->var_tail)
1088 ctx->var_tail->global_next = list;
1089 else
1090 ctx->var_head = list;
1092 for(iter = list; iter; iter = iter->next) {
1093 ctx->func->var_cnt++;
1094 iter->global_next = iter->next;
1095 if(!iter->next)
1096 ctx->var_tail = iter;
1098 if(!iter->expr)
1099 continue;
1101 hres = compile_expression(ctx, iter->expr, TRUE);
1102 if(FAILED(hres))
1103 return hres;
1105 hres = push_instr_bstr(ctx, OP_var_set, iter->identifier);
1106 if(FAILED(hres))
1107 return hres;
1110 return S_OK;
1113 /* ECMA-262 3rd Edition 12.2 */
1114 static HRESULT compile_var_statement(compiler_ctx_t *ctx, var_statement_t *stat)
1116 return compile_variable_list(ctx, stat->variable_list);
1119 /* ECMA-262 3rd Edition 12.4 */
1120 static HRESULT compile_expression_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1122 HRESULT hres;
1124 hres = compile_expression(ctx, stat->expr, ctx->from_eval);
1125 if(FAILED(hres))
1126 return hres;
1128 return !ctx->from_eval || push_instr(ctx, OP_setret) ? S_OK : E_OUTOFMEMORY;
1131 /* ECMA-262 3rd Edition 12.5 */
1132 static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat)
1134 unsigned jmp_else;
1135 HRESULT hres;
1137 hres = compile_expression(ctx, stat->expr, TRUE);
1138 if(FAILED(hres))
1139 return hres;
1141 jmp_else = push_instr(ctx, OP_jmp_z);
1142 if(!jmp_else)
1143 return E_OUTOFMEMORY;
1145 hres = compile_statement(ctx, NULL, stat->if_stat);
1146 if(FAILED(hres))
1147 return hres;
1149 if(stat->else_stat) {
1150 unsigned jmp_end;
1152 jmp_end = push_instr(ctx, OP_jmp);
1153 if(!jmp_end)
1154 return E_OUTOFMEMORY;
1156 set_arg_uint(ctx, jmp_else, ctx->code_off);
1158 hres = compile_statement(ctx, NULL, stat->else_stat);
1159 if(FAILED(hres))
1160 return hres;
1162 set_arg_uint(ctx, jmp_end, ctx->code_off);
1163 }else {
1164 set_arg_uint(ctx, jmp_else, ctx->code_off);
1167 return S_OK;
1170 /* ECMA-262 3rd Edition 12.6.2 */
1171 static HRESULT compile_while_statement(compiler_ctx_t *ctx, while_statement_t *stat)
1173 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1174 unsigned jmp_off;
1175 HRESULT hres;
1177 stat_ctx.break_label = alloc_label(ctx);
1178 if(!stat_ctx.break_label)
1179 return E_OUTOFMEMORY;
1181 stat_ctx.continue_label = alloc_label(ctx);
1182 if(!stat_ctx.continue_label)
1183 return E_OUTOFMEMORY;
1185 jmp_off = ctx->code_off;
1187 if(!stat->do_while) {
1188 label_set_addr(ctx, stat_ctx.continue_label);
1189 hres = compile_expression(ctx, stat->expr, TRUE);
1190 if(FAILED(hres))
1191 return hres;
1193 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1194 if(FAILED(hres))
1195 return hres;
1198 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1199 if(FAILED(hres))
1200 return hres;
1202 if(stat->do_while) {
1203 label_set_addr(ctx, stat_ctx.continue_label);
1204 hres = compile_expression(ctx, stat->expr, TRUE);
1205 if(FAILED(hres))
1206 return hres;
1208 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1209 if(FAILED(hres))
1210 return hres;
1213 hres = push_instr_uint(ctx, OP_jmp, jmp_off);
1214 if(FAILED(hres))
1215 return hres;
1217 label_set_addr(ctx, stat_ctx.break_label);
1218 return S_OK;
1221 /* ECMA-262 3rd Edition 12.6.3 */
1222 static HRESULT compile_for_statement(compiler_ctx_t *ctx, for_statement_t *stat)
1224 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1225 unsigned expr_off;
1226 HRESULT hres;
1228 if(stat->variable_list) {
1229 hres = compile_variable_list(ctx, stat->variable_list);
1230 if(FAILED(hres))
1231 return hres;
1232 }else if(stat->begin_expr) {
1233 hres = compile_expression(ctx, stat->begin_expr, FALSE);
1234 if(FAILED(hres))
1235 return hres;
1238 stat_ctx.break_label = alloc_label(ctx);
1239 if(!stat_ctx.break_label)
1240 return E_OUTOFMEMORY;
1242 stat_ctx.continue_label = alloc_label(ctx);
1243 if(!stat_ctx.continue_label)
1244 return E_OUTOFMEMORY;
1246 expr_off = ctx->code_off;
1248 if(stat->expr) {
1249 hres = compile_expression(ctx, stat->expr, TRUE);
1250 if(FAILED(hres))
1251 return hres;
1253 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1254 if(FAILED(hres))
1255 return hres;
1258 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1259 if(FAILED(hres))
1260 return hres;
1262 label_set_addr(ctx, stat_ctx.continue_label);
1264 if(stat->end_expr) {
1265 hres = compile_expression(ctx, stat->end_expr, FALSE);
1266 if(FAILED(hres))
1267 return hres;
1270 hres = push_instr_uint(ctx, OP_jmp, expr_off);
1271 if(FAILED(hres))
1272 return hres;
1274 label_set_addr(ctx, stat_ctx.break_label);
1275 return S_OK;
1278 /* ECMA-262 3rd Edition 12.6.4 */
1279 static HRESULT compile_forin_statement(compiler_ctx_t *ctx, forin_statement_t *stat)
1281 statement_ctx_t stat_ctx = {4, FALSE, FALSE};
1282 HRESULT hres;
1284 if(stat->variable) {
1285 hres = compile_variable_list(ctx, stat->variable);
1286 if(FAILED(hres))
1287 return hres;
1290 stat_ctx.break_label = alloc_label(ctx);
1291 if(!stat_ctx.break_label)
1292 return E_OUTOFMEMORY;
1294 stat_ctx.continue_label = alloc_label(ctx);
1295 if(!stat_ctx.continue_label)
1296 return E_OUTOFMEMORY;
1298 hres = compile_expression(ctx, stat->in_expr, TRUE);
1299 if(FAILED(hres))
1300 return hres;
1302 if(stat->variable) {
1303 hres = push_instr_bstr_uint(ctx, OP_identid, stat->variable->identifier, fdexNameEnsure);
1304 if(FAILED(hres))
1305 return hres;
1306 }else if(is_memberid_expr(stat->expr->type)) {
1307 hres = compile_memberid_expression(ctx, stat->expr, fdexNameEnsure);
1308 if(FAILED(hres))
1309 return hres;
1310 }else {
1311 hres = push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
1312 if(FAILED(hres))
1313 return hres;
1315 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1316 return S_OK;
1319 hres = push_instr_int(ctx, OP_int, DISPID_STARTENUM);
1320 if(FAILED(hres))
1321 return hres;
1323 label_set_addr(ctx, stat_ctx.continue_label);
1324 hres = push_instr_uint(ctx, OP_forin, stat_ctx.break_label);
1325 if(FAILED(hres))
1326 return E_OUTOFMEMORY;
1328 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1329 if(FAILED(hres))
1330 return hres;
1332 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.continue_label);
1333 if(FAILED(hres))
1334 return hres;
1336 label_set_addr(ctx, stat_ctx.break_label);
1337 return S_OK;
1340 static HRESULT pop_to_stat(compiler_ctx_t *ctx, BOOL var_stack, BOOL scope_stack, statement_ctx_t *stat_ctx)
1342 unsigned stack_pop = 0;
1343 statement_ctx_t *iter;
1345 for(iter = ctx->stat_ctx; iter != stat_ctx; iter = iter->next) {
1346 if(scope_stack) {
1347 if(iter->using_scope && !push_instr(ctx, OP_pop_scope))
1348 return E_OUTOFMEMORY;
1349 if(iter->using_except && !push_instr(ctx, OP_pop_except))
1350 return E_OUTOFMEMORY;
1352 stack_pop += iter->stack_use;
1355 if(var_stack && stack_pop) {
1356 HRESULT hres;
1358 hres = push_instr_uint(ctx, OP_pop, stack_pop);
1359 if(FAILED(hres))
1360 return hres;
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;
1399 assert(pop_ctx != NULL);
1400 }else {
1401 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1402 if(pop_ctx->continue_label)
1403 break;
1406 if(!pop_ctx) {
1407 WARN("continue outside loop\n");
1408 return JS_E_INVALID_CONTINUE;
1412 hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx);
1413 if(FAILED(hres))
1414 return hres;
1416 return push_instr_uint(ctx, OP_jmp, pop_ctx->continue_label);
1419 /* ECMA-262 3rd Edition 12.8 */
1420 static HRESULT compile_break_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1422 statement_ctx_t *pop_ctx;
1423 HRESULT hres;
1425 if(stat->identifier) {
1426 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1427 if(pop_ctx->labelled_stat && !strcmpW(pop_ctx->labelled_stat->identifier, stat->identifier)) {
1428 assert(pop_ctx->break_label);
1429 break;
1433 if(!pop_ctx) {
1434 WARN("Label not found\n");
1435 return JS_E_LABEL_NOT_FOUND;
1437 }else {
1438 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1439 if(pop_ctx->break_label && !pop_ctx->labelled_stat)
1440 break;
1443 if(!pop_ctx) {
1444 WARN("Break outside loop\n");
1445 return JS_E_INVALID_BREAK;
1449 hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx->next);
1450 if(FAILED(hres))
1451 return hres;
1453 return push_instr_uint(ctx, OP_jmp, pop_ctx->break_label);
1456 /* ECMA-262 3rd Edition 12.9 */
1457 static HRESULT compile_return_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1459 HRESULT hres;
1461 if(ctx->from_eval) {
1462 WARN("misplaced return statement\n");
1463 return JS_E_MISPLACED_RETURN;
1466 hres = pop_to_stat(ctx, TRUE, FALSE, NULL);
1467 if(FAILED(hres))
1468 return hres;
1470 if(stat->expr) {
1471 hres = compile_expression(ctx, stat->expr, TRUE);
1472 if(FAILED(hres))
1473 return hres;
1474 if(!push_instr(ctx, OP_setret))
1475 return E_OUTOFMEMORY;
1478 hres = pop_to_stat(ctx, FALSE, TRUE, NULL);
1479 if(FAILED(hres))
1480 return hres;
1482 return push_instr(ctx, OP_ret) ? S_OK : E_OUTOFMEMORY;
1485 /* ECMA-262 3rd Edition 12.10 */
1486 static HRESULT compile_with_statement(compiler_ctx_t *ctx, with_statement_t *stat)
1488 statement_ctx_t stat_ctx = {0, TRUE, FALSE};
1489 HRESULT hres;
1491 hres = compile_expression(ctx, stat->expr, TRUE);
1492 if(FAILED(hres))
1493 return hres;
1495 if(!push_instr(ctx, OP_push_scope))
1496 return E_OUTOFMEMORY;
1498 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1499 if(FAILED(hres))
1500 return hres;
1502 if(!push_instr(ctx, OP_pop_scope))
1503 return E_OUTOFMEMORY;
1505 return S_OK;
1508 /* ECMA-262 3rd Edition 12.10 */
1509 static HRESULT compile_labelled_statement(compiler_ctx_t *ctx, labelled_statement_t *stat)
1511 statement_ctx_t stat_ctx = {0, FALSE, FALSE, 0, 0, stat}, *iter;
1512 HRESULT hres;
1514 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1515 if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier)) {
1516 WARN("Label %s redefined\n", debugstr_w(stat->identifier));
1517 return JS_E_LABEL_REDEFINED;
1521 /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1522 stat_ctx.break_label = alloc_label(ctx);
1523 if(!stat_ctx.break_label)
1524 return E_OUTOFMEMORY;
1526 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1527 if(FAILED(hres))
1528 return hres;
1530 label_set_addr(ctx, stat_ctx.break_label);
1531 return S_OK;
1534 /* ECMA-262 3rd Edition 12.13 */
1535 static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat)
1537 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1538 unsigned case_cnt = 0, *case_jmps, i, default_jmp;
1539 BOOL have_default = FALSE;
1540 statement_t *stat_iter;
1541 case_clausule_t *iter;
1542 HRESULT hres;
1544 hres = compile_expression(ctx, stat->expr, TRUE);
1545 if(FAILED(hres))
1546 return hres;
1548 stat_ctx.break_label = alloc_label(ctx);
1549 if(!stat_ctx.break_label)
1550 return E_OUTOFMEMORY;
1552 for(iter = stat->case_list; iter; iter = iter->next) {
1553 if(iter->expr)
1554 case_cnt++;
1557 case_jmps = heap_alloc(case_cnt * sizeof(*case_jmps));
1558 if(!case_jmps)
1559 return E_OUTOFMEMORY;
1561 i = 0;
1562 for(iter = stat->case_list; iter; iter = iter->next) {
1563 if(!iter->expr) {
1564 have_default = TRUE;
1565 continue;
1568 hres = compile_expression(ctx, iter->expr, TRUE);
1569 if(FAILED(hres))
1570 break;
1572 case_jmps[i] = push_instr(ctx, OP_case);
1573 if(!case_jmps[i]) {
1574 hres = E_OUTOFMEMORY;
1575 break;
1577 i++;
1580 if(SUCCEEDED(hres)) {
1581 hres = push_instr_uint(ctx, OP_pop, 1);
1582 if(SUCCEEDED(hres)) {
1583 default_jmp = push_instr(ctx, OP_jmp);
1584 if(!default_jmp)
1585 hres = E_OUTOFMEMORY;
1589 if(FAILED(hres)) {
1590 heap_free(case_jmps);
1591 return hres;
1594 i = 0;
1595 for(iter = stat->case_list; iter; iter = iter->next) {
1596 while(iter->next && iter->next->stat == iter->stat) {
1597 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1598 iter = iter->next;
1601 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1603 for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter);
1604 stat_iter = stat_iter->next) {
1605 hres = compile_statement(ctx, &stat_ctx, stat_iter);
1606 if(FAILED(hres))
1607 break;
1609 if(FAILED(hres))
1610 break;
1613 heap_free(case_jmps);
1614 if(FAILED(hres))
1615 return hres;
1616 assert(i == case_cnt);
1618 if(!have_default) {
1619 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.break_label);
1620 if(FAILED(hres))
1621 return hres;
1622 set_arg_uint(ctx, default_jmp, ctx->code_off);
1625 label_set_addr(ctx, stat_ctx.break_label);
1626 return S_OK;
1629 /* ECMA-262 3rd Edition 12.13 */
1630 static HRESULT compile_throw_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1632 HRESULT hres;
1634 hres = compile_expression(ctx, stat->expr, TRUE);
1635 if(FAILED(hres))
1636 return hres;
1638 return push_instr(ctx, OP_throw) ? S_OK : E_OUTOFMEMORY;
1641 /* ECMA-262 3rd Edition 12.14 */
1642 static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat)
1644 statement_ctx_t try_ctx = {0, FALSE, TRUE}, catch_ctx = {0, TRUE, FALSE};
1645 statement_ctx_t finally_ctx = {2, FALSE, FALSE};
1646 unsigned push_except;
1647 BSTR ident;
1648 HRESULT hres;
1650 push_except = push_instr(ctx, OP_push_except);
1651 if(!push_except)
1652 return E_OUTOFMEMORY;
1654 if(stat->catch_block) {
1655 ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier);
1656 if(!ident)
1657 return E_OUTOFMEMORY;
1658 }else {
1659 ident = NULL;
1662 instr_ptr(ctx, push_except)->u.arg[1].bstr = ident;
1664 if(!stat->catch_block)
1665 try_ctx.stack_use = 2;
1667 hres = compile_statement(ctx, &try_ctx, stat->try_statement);
1668 if(FAILED(hres))
1669 return hres;
1671 if(!push_instr(ctx, OP_pop_except))
1672 return E_OUTOFMEMORY;
1674 if(stat->catch_block) {
1675 unsigned jmp_finally;
1677 jmp_finally = push_instr(ctx, OP_jmp);
1678 if(!jmp_finally)
1679 return E_OUTOFMEMORY;
1681 instr_ptr(ctx, push_except)->u.arg[0].uint = ctx->code_off;
1683 hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement);
1684 if(FAILED(hres))
1685 return hres;
1687 if(!push_instr(ctx, OP_pop_scope))
1688 return E_OUTOFMEMORY;
1690 set_arg_uint(ctx, jmp_finally, ctx->code_off);
1691 }else {
1692 set_arg_uint(ctx, push_except, ctx->code_off);
1695 if(stat->finally_statement) {
1696 hres = compile_statement(ctx, stat->catch_block ? NULL : &finally_ctx, stat->finally_statement);
1697 if(FAILED(hres))
1698 return hres;
1700 if(!stat->catch_block && !push_instr(ctx, OP_end_finally))
1701 return E_OUTOFMEMORY;
1704 return S_OK;
1707 static HRESULT compile_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1709 HRESULT hres;
1711 if(stat_ctx) {
1712 stat_ctx->next = ctx->stat_ctx;
1713 ctx->stat_ctx = stat_ctx;
1716 switch(stat->type) {
1717 case STAT_BLOCK:
1718 hres = compile_block_statement(ctx, ((block_statement_t*)stat)->stat_list);
1719 break;
1720 case STAT_BREAK:
1721 hres = compile_break_statement(ctx, (branch_statement_t*)stat);
1722 break;
1723 case STAT_CONTINUE:
1724 hres = compile_continue_statement(ctx, (branch_statement_t*)stat);
1725 break;
1726 case STAT_EMPTY:
1727 /* nothing to do */
1728 hres = S_OK;
1729 break;
1730 case STAT_EXPR:
1731 hres = compile_expression_statement(ctx, (expression_statement_t*)stat);
1732 break;
1733 case STAT_FOR:
1734 hres = compile_for_statement(ctx, (for_statement_t*)stat);
1735 break;
1736 case STAT_FORIN:
1737 hres = compile_forin_statement(ctx, (forin_statement_t*)stat);
1738 break;
1739 case STAT_IF:
1740 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1741 break;
1742 case STAT_LABEL:
1743 hres = compile_labelled_statement(ctx, (labelled_statement_t*)stat);
1744 break;
1745 case STAT_RETURN:
1746 hres = compile_return_statement(ctx, (expression_statement_t*)stat);
1747 break;
1748 case STAT_SWITCH:
1749 hres = compile_switch_statement(ctx, (switch_statement_t*)stat);
1750 break;
1751 case STAT_THROW:
1752 hres = compile_throw_statement(ctx, (expression_statement_t*)stat);
1753 break;
1754 case STAT_TRY:
1755 hres = compile_try_statement(ctx, (try_statement_t*)stat);
1756 break;
1757 case STAT_VAR:
1758 hres = compile_var_statement(ctx, (var_statement_t*)stat);
1759 break;
1760 case STAT_WHILE:
1761 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1762 break;
1763 case STAT_WITH:
1764 hres = compile_with_statement(ctx, (with_statement_t*)stat);
1765 break;
1766 DEFAULT_UNREACHABLE;
1769 if(stat_ctx) {
1770 assert(ctx->stat_ctx == stat_ctx);
1771 ctx->stat_ctx = stat_ctx->next;
1774 return hres;
1777 static void resolve_labels(compiler_ctx_t *ctx, unsigned off)
1779 instr_t *instr;
1781 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
1782 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->u.arg->uint & LABEL_FLAG)) {
1783 assert((instr->u.arg->uint & ~LABEL_FLAG) < ctx->labels_cnt);
1784 instr->u.arg->uint = ctx->labels[instr->u.arg->uint & ~LABEL_FLAG];
1786 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
1789 ctx->labels_cnt = 0;
1792 void release_bytecode(bytecode_t *code)
1794 unsigned i;
1796 if(--code->ref)
1797 return;
1799 for(i=0; i < code->bstr_cnt; i++)
1800 SysFreeString(code->bstr_pool[i]);
1801 for(i=0; i < code->str_cnt; i++)
1802 jsstr_release(code->str_pool[i]);
1804 heap_free(code->source);
1805 heap_pool_free(&code->heap);
1806 heap_free(code->bstr_pool);
1807 heap_free(code->str_pool);
1808 heap_free(code->instrs);
1809 heap_free(code);
1812 static HRESULT init_code(compiler_ctx_t *compiler, const WCHAR *source)
1814 compiler->code = heap_alloc_zero(sizeof(bytecode_t));
1815 if(!compiler->code)
1816 return E_OUTOFMEMORY;
1818 compiler->code->ref = 1;
1819 heap_pool_init(&compiler->code->heap);
1821 compiler->code->source = heap_strdupW(source);
1822 if(!compiler->code->source) {
1823 release_bytecode(compiler->code);
1824 return E_OUTOFMEMORY;
1827 compiler->code->instrs = heap_alloc(64 * sizeof(instr_t));
1828 if(!compiler->code->instrs) {
1829 release_bytecode(compiler->code);
1830 return E_OUTOFMEMORY;
1833 compiler->code_size = 64;
1834 compiler->code_off = 1;
1835 return S_OK;
1838 static HRESULT compile_function(compiler_ctx_t *ctx, source_elements_t *source, function_expression_t *func_expr,
1839 BOOL from_eval, function_code_t *func)
1841 variable_declaration_t *var_iter;
1842 function_expression_t *iter;
1843 unsigned off, i;
1844 HRESULT hres;
1846 TRACE("\n");
1848 ctx->var_head = ctx->var_tail = NULL;
1849 ctx->func_head = ctx->func_tail = NULL;
1850 ctx->from_eval = from_eval;
1852 off = ctx->code_off;
1853 ctx->func = func;
1854 hres = compile_block_statement(ctx, source->statement);
1855 if(FAILED(hres))
1856 return hres;
1858 resolve_labels(ctx, off);
1860 if(!push_instr(ctx, OP_ret))
1861 return E_OUTOFMEMORY;
1863 if(TRACE_ON(jscript_disas))
1864 dump_code(ctx, off);
1866 func->instr_off = off;
1868 if(func_expr && func_expr->identifier) {
1869 func->name = compiler_alloc_bstr(ctx, func_expr->identifier);
1870 if(!func->name)
1871 return E_OUTOFMEMORY;
1874 if(func_expr) {
1875 parameter_t *param_iter;
1877 func->source = func_expr->src_str;
1878 func->source_len = func_expr->src_len;
1880 for(param_iter = func_expr->parameter_list; param_iter; param_iter = param_iter->next)
1881 func->param_cnt++;
1883 func->params = compiler_alloc(ctx->code, func->param_cnt * sizeof(*func->params));
1884 if(!func->params)
1885 return E_OUTOFMEMORY;
1887 for(param_iter = func_expr->parameter_list, i=0; param_iter; param_iter = param_iter->next, i++) {
1888 func->params[i] = compiler_alloc_bstr(ctx, param_iter->identifier);
1889 if(!func->params[i])
1890 return E_OUTOFMEMORY;
1894 func->variables = compiler_alloc(ctx->code, func->var_cnt * sizeof(*func->variables));
1895 if(!func->variables)
1896 return E_OUTOFMEMORY;
1898 for(var_iter = ctx->var_head, i=0; var_iter; var_iter = var_iter->global_next, i++) {
1899 func->variables[i] = compiler_alloc_bstr(ctx, var_iter->identifier);
1900 if(!func->variables[i])
1901 return E_OUTOFMEMORY;
1904 assert(i == func->var_cnt);
1906 func->funcs = compiler_alloc(ctx->code, func->func_cnt * sizeof(*func->funcs));
1907 if(!func->funcs)
1908 return E_OUTOFMEMORY;
1909 memset(func->funcs, 0, func->func_cnt * sizeof(*func->funcs));
1911 for(iter = ctx->func_head, i=0; iter; iter = iter->next, i++) {
1912 hres = compile_function(ctx, iter->source_elements, iter, FALSE, func->funcs+i);
1913 if(FAILED(hres))
1914 return hres;
1917 assert(i == func->func_cnt);
1919 return S_OK;
1922 static HRESULT parse_arguments(compiler_ctx_t *ctx, const WCHAR *args, BSTR *arg_array, unsigned *args_size)
1924 const WCHAR *ptr = args, *ptr2;
1925 unsigned arg_cnt = 0;
1927 while(isspaceW(*ptr))
1928 ptr++;
1929 if(!*ptr) {
1930 if(args_size)
1931 *args_size = 0;
1932 return S_OK;
1935 while(1) {
1936 if(!isalphaW(*ptr) && *ptr != '_') {
1937 FIXME("expected alpha or '_': %s\n", debugstr_w(ptr));
1938 return E_FAIL;
1941 ptr2 = ptr;
1942 while(isalnumW(*ptr) || *ptr == '_')
1943 ptr++;
1945 if(*ptr && *ptr != ',' && !isspaceW(*ptr)) {
1946 FIXME("unexpected har %s\n", debugstr_w(ptr));
1947 return E_FAIL;
1950 if(arg_array) {
1951 arg_array[arg_cnt] = compiler_alloc_bstr_len(ctx, ptr2, ptr-ptr2);
1952 if(!arg_array[arg_cnt])
1953 return E_OUTOFMEMORY;
1955 arg_cnt++;
1957 while(isspaceW(*ptr))
1958 ptr++;
1959 if(!*ptr)
1960 break;
1961 if(*ptr != ',') {
1962 FIXME("expected ',': %s\n", debugstr_w(ptr));
1963 return E_FAIL;
1966 ptr++;
1967 while(isspaceW(*ptr))
1968 ptr++;
1971 if(args_size)
1972 *args_size = arg_cnt;
1973 return S_OK;
1976 static HRESULT compile_arguments(compiler_ctx_t *ctx, const WCHAR *args)
1978 HRESULT hres;
1980 hres = parse_arguments(ctx, args, NULL, &ctx->code->global_code.param_cnt);
1981 if(FAILED(hres))
1982 return hres;
1984 ctx->code->global_code.params = compiler_alloc(ctx->code,
1985 ctx->code->global_code.param_cnt * sizeof(*ctx->code->global_code.params));
1986 if(!ctx->code->global_code.params)
1987 return E_OUTOFMEMORY;
1989 return parse_arguments(ctx, args, ctx->code->global_code.params, NULL);
1992 HRESULT compile_script(script_ctx_t *ctx, const WCHAR *code, const WCHAR *args, const WCHAR *delimiter,
1993 BOOL from_eval, BOOL use_decode, bytecode_t **ret)
1995 compiler_ctx_t compiler = {0};
1996 HRESULT hres;
1998 hres = init_code(&compiler, code);
1999 if(FAILED(hres))
2000 return hres;
2002 if(args) {
2003 hres = compile_arguments(&compiler, args);
2004 if(FAILED(hres))
2005 return hres;
2008 if(use_decode) {
2009 hres = decode_source(compiler.code->source);
2010 if(FAILED(hres)) {
2011 WARN("Decoding failed\n");
2012 return hres;
2016 hres = script_parse(ctx, compiler.code->source, delimiter, from_eval, &compiler.parser);
2017 if(FAILED(hres)) {
2018 release_bytecode(compiler.code);
2019 return hres;
2022 hres = compile_function(&compiler, compiler.parser->source, NULL, from_eval, &compiler.code->global_code);
2023 parser_release(compiler.parser);
2024 if(FAILED(hres)) {
2025 release_bytecode(compiler.code);
2026 return hres;
2029 *ret = compiler.code;
2030 return S_OK;