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
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
28 WINE_DECLARE_DEBUG_CHANNEL(jscript_disas
);
30 typedef struct _statement_ctx_t
{
36 unsigned continue_label
;
38 const labelled_statement_t
*labelled_stat
;
40 struct _statement_ctx_t
*next
;
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
;
66 instr_arg_type_t arg1_type
;
67 instr_arg_type_t arg2_type
;
69 #define X(n,a,b,c) {#n,b,c},
74 static void dump_instr_arg(instr_arg_type_t type
, instr_arg_t
*arg
)
78 TRACE_(jscript_disas
)("\t%s", debugstr_w(arg
->str
));
81 TRACE_(jscript_disas
)("\t%s", debugstr_wn(arg
->bstr
, SysStringLen(arg
->bstr
)));
84 TRACE_(jscript_disas
)("\t%d", arg
->uint
);
88 TRACE_(jscript_disas
)("\t%u", arg
->uint
);
98 static void dump_code(compiler_ctx_t
*ctx
, unsigned off
)
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
);
107 dump_instr_arg(instr_info
[instr
->op
].arg1_type
, instr
->u
.arg
);
108 dump_instr_arg(instr_info
[instr
->op
].arg2_type
, instr
->u
.arg
+1);
110 TRACE_(jscript_disas
)("\n");
114 static HRESULT
compile_expression(compiler_ctx_t
*,expression_t
*);
115 static HRESULT
compile_statement(compiler_ctx_t
*,statement_ctx_t
*,statement_t
*);
117 static inline void *compiler_alloc(bytecode_t
*code
, size_t size
)
119 return jsheap_alloc(&code
->heap
, size
);
122 static WCHAR
*compiler_alloc_string(bytecode_t
*code
, const WCHAR
*str
)
127 size
= (strlenW(str
)+1)*sizeof(WCHAR
);
128 ret
= compiler_alloc(code
, size
);
130 memcpy(ret
, str
, size
);
134 static BOOL
ensure_bstr_slot(compiler_ctx_t
*ctx
)
136 if(!ctx
->code
->bstr_pool_size
) {
137 ctx
->code
->bstr_pool
= heap_alloc(8 * sizeof(BSTR
));
138 if(!ctx
->code
->bstr_pool
)
140 ctx
->code
->bstr_pool_size
= 8;
141 }else if(ctx
->code
->bstr_pool_size
== ctx
->code
->bstr_cnt
) {
144 new_pool
= heap_realloc(ctx
->code
->bstr_pool
, ctx
->code
->bstr_pool_size
*2*sizeof(BSTR
));
148 ctx
->code
->bstr_pool
= new_pool
;
149 ctx
->code
->bstr_pool_size
*= 2;
155 static BSTR
compiler_alloc_bstr(compiler_ctx_t
*ctx
, const WCHAR
*str
)
157 if(!ensure_bstr_slot(ctx
))
160 ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
] = SysAllocString(str
);
161 if(!ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
])
164 return ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
++];
167 static BSTR
compiler_alloc_bstr_len(compiler_ctx_t
*ctx
, const WCHAR
*str
, size_t len
)
169 if(!ensure_bstr_slot(ctx
))
172 ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
] = SysAllocStringLen(str
, len
);
173 if(!ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
])
176 return ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
++];
179 static unsigned push_instr(compiler_ctx_t
*ctx
, jsop_t op
)
181 assert(ctx
->code_size
>= ctx
->code_off
);
183 if(ctx
->code_size
== ctx
->code_off
) {
186 new_instrs
= heap_realloc(ctx
->code
->instrs
, ctx
->code_size
*2*sizeof(instr_t
));
190 ctx
->code
->instrs
= new_instrs
;
194 ctx
->code
->instrs
[ctx
->code_off
].op
= op
;
195 return ctx
->code_off
++;
198 static inline instr_t
*instr_ptr(compiler_ctx_t
*ctx
, unsigned off
)
200 assert(off
< ctx
->code_off
);
201 return ctx
->code
->instrs
+ off
;
204 static HRESULT
push_instr_int(compiler_ctx_t
*ctx
, jsop_t op
, LONG arg
)
208 instr
= push_instr(ctx
, op
);
210 return E_OUTOFMEMORY
;
212 instr_ptr(ctx
, instr
)->u
.arg
->lng
= arg
;
216 static HRESULT
push_instr_str(compiler_ctx_t
*ctx
, jsop_t op
, const WCHAR
*arg
)
221 str
= compiler_alloc_string(ctx
->code
, arg
);
223 return E_OUTOFMEMORY
;
225 instr
= push_instr(ctx
, op
);
227 return E_OUTOFMEMORY
;
229 instr_ptr(ctx
, instr
)->u
.arg
->str
= str
;
233 static HRESULT
push_instr_bstr(compiler_ctx_t
*ctx
, jsop_t op
, const WCHAR
*arg
)
238 str
= compiler_alloc_bstr(ctx
, arg
);
240 return E_OUTOFMEMORY
;
242 instr
= push_instr(ctx
, op
);
244 return E_OUTOFMEMORY
;
246 instr_ptr(ctx
, instr
)->u
.arg
->bstr
= str
;
250 static HRESULT
push_instr_bstr_uint(compiler_ctx_t
*ctx
, jsop_t op
, const WCHAR
*arg1
, unsigned arg2
)
255 str
= compiler_alloc_bstr(ctx
, arg1
);
257 return E_OUTOFMEMORY
;
259 instr
= push_instr(ctx
, op
);
261 return E_OUTOFMEMORY
;
263 instr_ptr(ctx
, instr
)->u
.arg
[0].bstr
= str
;
264 instr_ptr(ctx
, instr
)->u
.arg
[1].uint
= arg2
;
268 static HRESULT
push_instr_uint_str(compiler_ctx_t
*ctx
, jsop_t op
, unsigned arg1
, const WCHAR
*arg2
)
273 str
= compiler_alloc_string(ctx
->code
, arg2
);
275 return E_OUTOFMEMORY
;
277 instr
= push_instr(ctx
, op
);
279 return E_OUTOFMEMORY
;
281 instr_ptr(ctx
, instr
)->u
.arg
[0].uint
= arg1
;
282 instr_ptr(ctx
, instr
)->u
.arg
[1].str
= str
;
286 static HRESULT
push_instr_double(compiler_ctx_t
*ctx
, jsop_t op
, double arg
)
290 instr
= push_instr(ctx
, op
);
292 return E_OUTOFMEMORY
;
294 instr_ptr(ctx
, instr
)->u
.dbl
= arg
;
298 static inline void set_arg_uint(compiler_ctx_t
*ctx
, unsigned instr
, unsigned arg
)
300 instr_ptr(ctx
, instr
)->u
.arg
->uint
= arg
;
303 static HRESULT
push_instr_uint(compiler_ctx_t
*ctx
, jsop_t op
, unsigned arg
)
307 instr
= push_instr(ctx
, op
);
309 return E_OUTOFMEMORY
;
311 set_arg_uint(ctx
, instr
, arg
);
315 static HRESULT
compile_binary_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
319 hres
= compile_expression(ctx
, expr
->expression1
);
323 hres
= compile_expression(ctx
, expr
->expression2
);
327 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
330 static HRESULT
compile_unary_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
, jsop_t op
)
334 hres
= compile_expression(ctx
, expr
->expression
);
338 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
341 /* ECMA-262 3rd Edition 11.2.1 */
342 static HRESULT
compile_member_expression(compiler_ctx_t
*ctx
, member_expression_t
*expr
)
346 hres
= compile_expression(ctx
, expr
->expression
);
350 return push_instr_bstr(ctx
, OP_member
, expr
->identifier
);
353 #define LABEL_FLAG 0x80000000
355 static unsigned alloc_label(compiler_ctx_t
*ctx
)
357 if(!ctx
->labels_size
) {
358 ctx
->labels
= heap_alloc(8 * sizeof(*ctx
->labels
));
361 ctx
->labels_size
= 8;
362 }else if(ctx
->labels_size
== ctx
->labels_cnt
) {
363 unsigned *new_labels
;
365 new_labels
= heap_realloc(ctx
->labels
, 2*ctx
->labels_size
*sizeof(*ctx
->labels
));
369 ctx
->labels
= new_labels
;
370 ctx
->labels_size
*= 2;
373 return ctx
->labels_cnt
++ | LABEL_FLAG
;
376 static void label_set_addr(compiler_ctx_t
*ctx
, unsigned label
)
378 assert(label
& LABEL_FLAG
);
379 ctx
->labels
[label
& ~LABEL_FLAG
] = ctx
->code_off
;
382 static inline BOOL
is_memberid_expr(expression_type_t type
)
384 return type
== EXPR_IDENT
|| type
== EXPR_MEMBER
|| type
== EXPR_ARRAY
;
387 static HRESULT
compile_memberid_expression(compiler_ctx_t
*ctx
, expression_t
*expr
, unsigned flags
)
393 identifier_expression_t
*ident_expr
= (identifier_expression_t
*)expr
;
395 hres
= push_instr_bstr_uint(ctx
, OP_identid
, ident_expr
->identifier
, flags
);
399 binary_expression_t
*array_expr
= (binary_expression_t
*)expr
;
401 hres
= compile_expression(ctx
, array_expr
->expression1
);
405 hres
= compile_expression(ctx
, array_expr
->expression2
);
409 hres
= push_instr_uint(ctx
, OP_memberid
, flags
);
413 member_expression_t
*member_expr
= (member_expression_t
*)expr
;
415 hres
= compile_expression(ctx
, member_expr
->expression
);
419 /* FIXME: Potential optimization */
420 hres
= push_instr_str(ctx
, OP_str
, member_expr
->identifier
);
424 hres
= push_instr_uint(ctx
, OP_memberid
, flags
);
434 static HRESULT
compile_increment_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
, jsop_t op
, int n
)
438 if(!is_memberid_expr(expr
->expression
->type
)) {
439 hres
= compile_expression(ctx
, expr
->expression
);
443 return push_instr_uint(ctx
, OP_throw_ref
, JS_E_ILLEGAL_ASSIGN
);
446 hres
= compile_memberid_expression(ctx
, expr
->expression
, fdexNameEnsure
);
450 return push_instr_int(ctx
, op
, n
);
453 /* ECMA-262 3rd Edition 11.14 */
454 static HRESULT
compile_comma_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
)
458 hres
= compile_expression(ctx
, expr
->expression1
);
462 if(!push_instr(ctx
, OP_pop
))
463 return E_OUTOFMEMORY
;
465 return compile_expression(ctx
, expr
->expression2
);
468 /* ECMA-262 3rd Edition 11.11 */
469 static HRESULT
compile_logical_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
474 hres
= compile_expression(ctx
, expr
->expression1
);
478 instr
= push_instr(ctx
, op
);
480 return E_OUTOFMEMORY
;
482 hres
= compile_expression(ctx
, expr
->expression2
);
486 set_arg_uint(ctx
, instr
, ctx
->code_off
);
490 /* ECMA-262 3rd Edition 11.12 */
491 static HRESULT
compile_conditional_expression(compiler_ctx_t
*ctx
, conditional_expression_t
*expr
)
493 unsigned jmp_false
, jmp_end
;
496 hres
= compile_expression(ctx
, expr
->expression
);
500 jmp_false
= push_instr(ctx
, OP_cnd_z
);
502 return E_OUTOFMEMORY
;
504 hres
= compile_expression(ctx
, expr
->true_expression
);
508 jmp_end
= push_instr(ctx
, OP_jmp
);
510 return E_OUTOFMEMORY
;
512 set_arg_uint(ctx
, jmp_false
, ctx
->code_off
);
513 if(!push_instr(ctx
, OP_pop
))
514 return E_OUTOFMEMORY
;
516 hres
= compile_expression(ctx
, expr
->false_expression
);
520 set_arg_uint(ctx
, jmp_end
, ctx
->code_off
);
524 static HRESULT
compile_new_expression(compiler_ctx_t
*ctx
, call_expression_t
*expr
)
526 unsigned arg_cnt
= 0;
530 hres
= compile_expression(ctx
, expr
->expression
);
534 for(arg
= expr
->argument_list
; arg
; arg
= arg
->next
) {
535 hres
= compile_expression(ctx
, arg
->expr
);
541 return push_instr_uint(ctx
, OP_new
, arg_cnt
);
544 static HRESULT
compile_call_expression(compiler_ctx_t
*ctx
, call_expression_t
*expr
, BOOL
*no_ret
)
546 unsigned arg_cnt
= 0;
552 if(is_memberid_expr(expr
->expression
->type
)) {
554 hres
= compile_memberid_expression(ctx
, expr
->expression
, 0);
557 hres
= compile_expression(ctx
, expr
->expression
);
563 for(arg
= expr
->argument_list
; arg
; arg
= arg
->next
) {
564 hres
= compile_expression(ctx
, arg
->expr
);
570 instr
= push_instr(ctx
, op
);
572 return E_OUTOFMEMORY
;
574 instr_ptr(ctx
, instr
)->u
.arg
[0].uint
= arg_cnt
;
575 instr_ptr(ctx
, instr
)->u
.arg
[1].lng
= no_ret
== NULL
;
581 static HRESULT
compile_delete_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
)
585 switch(expr
->expression
->type
) {
587 binary_expression_t
*array_expr
= (binary_expression_t
*)expr
->expression
;
589 hres
= compile_expression(ctx
, array_expr
->expression1
);
593 hres
= compile_expression(ctx
, array_expr
->expression2
);
597 if(!push_instr(ctx
, OP_delete
))
598 return E_OUTOFMEMORY
;
602 member_expression_t
*member_expr
= (member_expression_t
*)expr
->expression
;
604 hres
= compile_expression(ctx
, member_expr
->expression
);
608 /* FIXME: Potential optimization */
609 hres
= push_instr_str(ctx
, OP_str
, member_expr
->identifier
);
613 if(!push_instr(ctx
, OP_delete
))
614 return E_OUTOFMEMORY
;
618 return push_instr_bstr(ctx
, OP_delete_ident
, ((identifier_expression_t
*)expr
->expression
)->identifier
);
620 const WCHAR fixmeW
[] = {'F','I','X','M','E',0};
622 WARN("invalid delete, unimplemented exception message\n");
624 hres
= compile_expression(ctx
, expr
->expression
);
628 return push_instr_uint_str(ctx
, OP_throw_type
, JS_E_INVALID_DELETE
, fixmeW
);
635 static HRESULT
compile_assign_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
637 BOOL use_throw_path
= FALSE
;
638 unsigned arg_cnt
= 0;
641 if(expr
->expression1
->type
== EXPR_CALL
) {
642 call_expression_t
*call_expr
= (call_expression_t
*)expr
->expression1
;
646 FIXME("op %d not supported on parametrized assign expressions\n", op
);
650 if(is_memberid_expr(call_expr
->expression
->type
) && call_expr
->argument_list
) {
651 hres
= compile_memberid_expression(ctx
, call_expr
->expression
, fdexNameEnsure
);
655 for(arg
= call_expr
->argument_list
; arg
; arg
= arg
->next
) {
656 hres
= compile_expression(ctx
, arg
->expr
);
662 use_throw_path
= TRUE
;
664 }else if(is_memberid_expr(expr
->expression1
->type
)) {
665 hres
= compile_memberid_expression(ctx
, expr
->expression1
, fdexNameEnsure
);
669 use_throw_path
= TRUE
;
673 /* Illegal assignment: evaluate and throw */
674 hres
= compile_expression(ctx
, expr
->expression1
);
678 hres
= compile_expression(ctx
, expr
->expression2
);
682 if(op
!= OP_LAST
&& !push_instr(ctx
, op
))
683 return E_OUTOFMEMORY
;
685 return push_instr_uint(ctx
, OP_throw_ref
, JS_E_ILLEGAL_ASSIGN
);
688 if(op
!= OP_LAST
&& !push_instr(ctx
, OP_refval
))
689 return E_OUTOFMEMORY
;
691 hres
= compile_expression(ctx
, expr
->expression2
);
695 if(op
!= OP_LAST
&& !push_instr(ctx
, op
))
696 return E_OUTOFMEMORY
;
699 return push_instr_uint(ctx
, OP_assign_call
, arg_cnt
);
701 if(!push_instr(ctx
, OP_assign
))
702 return E_OUTOFMEMORY
;
707 static HRESULT
compile_typeof_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
)
712 if(is_memberid_expr(expr
->expression
->type
)) {
713 if(expr
->expression
->type
== EXPR_IDENT
)
714 return push_instr_str(ctx
, OP_typeofident
, ((identifier_expression_t
*)expr
->expression
)->identifier
);
717 hres
= compile_memberid_expression(ctx
, expr
->expression
, 0);
720 hres
= compile_expression(ctx
, expr
->expression
);
725 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
728 static HRESULT
compile_literal(compiler_ctx_t
*ctx
, literal_t
*literal
)
730 switch(literal
->type
) {
732 return push_instr_int(ctx
, OP_bool
, literal
->u
.bval
);
734 return push_instr_double(ctx
, OP_double
, literal
->u
.dval
);
736 return push_instr(ctx
, OP_null
) ? S_OK
: E_OUTOFMEMORY
;
738 return push_instr_str(ctx
, OP_str
, literal
->u
.wstr
);
743 str
= compiler_alloc(ctx
->code
, (literal
->u
.regexp
.str_len
+1)*sizeof(WCHAR
));
745 return E_OUTOFMEMORY
;
746 memcpy(str
, literal
->u
.regexp
.str
, literal
->u
.regexp
.str_len
*sizeof(WCHAR
));
747 str
[literal
->u
.regexp
.str_len
] = 0;
749 instr
= push_instr(ctx
, OP_regexp
);
751 return E_OUTOFMEMORY
;
753 instr_ptr(ctx
, instr
)->u
.arg
[0].str
= str
;
754 instr_ptr(ctx
, instr
)->u
.arg
[1].uint
= literal
->u
.regexp
.flags
;
763 static HRESULT
literal_as_bstr(compiler_ctx_t
*ctx
, literal_t
*literal
, BSTR
*str
)
765 switch(literal
->type
) {
767 *str
= compiler_alloc_bstr(ctx
, literal
->u
.wstr
);
770 return double_to_bstr(literal
->u
.dval
, str
);
775 return *str
? S_OK
: E_OUTOFMEMORY
;
778 static HRESULT
compile_array_literal(compiler_ctx_t
*ctx
, array_literal_expression_t
*expr
)
780 unsigned i
, elem_cnt
= expr
->length
;
781 array_element_t
*iter
;
784 for(iter
= expr
->element_list
; iter
; iter
= iter
->next
) {
785 elem_cnt
+= iter
->elision
+1;
787 for(i
=0; i
< iter
->elision
; i
++) {
788 if(!push_instr(ctx
, OP_undefined
))
789 return E_OUTOFMEMORY
;
792 hres
= compile_expression(ctx
, iter
->expr
);
797 for(i
=0; i
< expr
->length
; i
++) {
798 if(!push_instr(ctx
, OP_undefined
))
799 return E_OUTOFMEMORY
;
802 return push_instr_uint(ctx
, OP_carray
, elem_cnt
);
805 static HRESULT
compile_object_literal(compiler_ctx_t
*ctx
, property_value_expression_t
*expr
)
812 if(!push_instr(ctx
, OP_new_obj
))
813 return E_OUTOFMEMORY
;
815 for(iter
= expr
->property_list
; iter
; iter
= iter
->next
) {
816 hres
= literal_as_bstr(ctx
, iter
->name
, &name
);
820 hres
= compile_expression(ctx
, iter
->value
);
824 instr
= push_instr(ctx
, OP_obj_prop
);
826 return E_OUTOFMEMORY
;
828 instr_ptr(ctx
, instr
)->u
.arg
->bstr
= name
;
834 static HRESULT
compile_function_expression(compiler_ctx_t
*ctx
, function_expression_t
*expr
)
836 ctx
->func_tail
= ctx
->func_tail
? (ctx
->func_tail
->next
= expr
) : (ctx
->func_head
= expr
);
838 /* FIXME: not exactly right */
839 if(expr
->identifier
) {
840 ctx
->func
->func_cnt
++;
841 return push_instr_bstr(ctx
, OP_ident
, expr
->identifier
);
844 return push_instr_uint(ctx
, OP_func
, ctx
->func
->func_cnt
++);
847 static HRESULT
compile_expression_noret(compiler_ctx_t
*ctx
, expression_t
*expr
, BOOL
*no_ret
)
851 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_add
);
853 return compile_logical_expression(ctx
, (binary_expression_t
*)expr
, OP_cnd_z
);
855 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_array
);
857 return compile_array_literal(ctx
, (array_literal_expression_t
*)expr
);
859 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_LAST
);
861 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_add
);
863 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_and
);
865 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
867 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_mul
);
869 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_div
);
871 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_mod
);
873 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_or
);
874 case EXPR_ASSIGNLSHIFT
:
875 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_lshift
);
876 case EXPR_ASSIGNRSHIFT
:
877 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift
);
878 case EXPR_ASSIGNRRSHIFT
:
879 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift2
);
881 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_xor
);
883 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_and
);
885 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_bneg
);
887 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_or
);
889 return compile_call_expression(ctx
, (call_expression_t
*)expr
, no_ret
);
891 return compile_comma_expression(ctx
, (binary_expression_t
*)expr
);
893 return compile_conditional_expression(ctx
, (conditional_expression_t
*)expr
);
895 return compile_delete_expression(ctx
, (unary_expression_t
*)expr
);
897 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_div
);
899 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_eq
);
901 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_eq2
);
903 return compile_function_expression(ctx
, (function_expression_t
*)expr
);
905 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gt
);
907 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gteq
);
909 return push_instr_bstr(ctx
, OP_ident
, ((identifier_expression_t
*)expr
)->identifier
);
911 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_in
);
912 case EXPR_INSTANCEOF
:
913 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_instanceof
);
915 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lt
);
917 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lteq
);
919 return compile_literal(ctx
, ((literal_expression_t
*)expr
)->literal
);
921 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_neg
);
923 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lshift
);
925 return compile_member_expression(ctx
, (member_expression_t
*)expr
);
927 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_minus
);
929 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mod
);
931 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mul
);
933 return compile_new_expression(ctx
, (call_expression_t
*)expr
);
935 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_neq
);
937 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_neq2
);
939 return compile_logical_expression(ctx
, (binary_expression_t
*)expr
, OP_cnd_nz
);
941 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_tonum
);
943 return compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_postinc
, -1);
945 return compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_postinc
, 1);
947 return compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_preinc
, -1);
949 return compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_preinc
, 1);
951 return compile_object_literal(ctx
, (property_value_expression_t
*)expr
);
953 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift
);
955 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift2
);
957 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
959 return push_instr(ctx
, OP_this
) ? S_OK
: E_OUTOFMEMORY
;
961 return compile_typeof_expression(ctx
, (unary_expression_t
*)expr
);
963 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_void
);
965 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_xor
);
973 static HRESULT
compile_expression(compiler_ctx_t
*ctx
, expression_t
*expr
)
975 return compile_expression_noret(ctx
, expr
, NULL
);
978 static inline BOOL
is_loop_statement(statement_type_t type
)
980 return type
== STAT_FOR
|| type
== STAT_FORIN
|| type
== STAT_WHILE
;
983 /* ECMA-262 3rd Edition 12.1 */
984 static HRESULT
compile_block_statement(compiler_ctx_t
*ctx
, statement_t
*iter
)
988 /* FIXME: do it only if needed */
990 return push_instr(ctx
, OP_undefined
) ? S_OK
: E_OUTOFMEMORY
;
993 hres
= compile_statement(ctx
, NULL
, iter
);
1001 if(!push_instr(ctx
, OP_pop
))
1002 return E_OUTOFMEMORY
;
1008 /* ECMA-262 3rd Edition 12.2 */
1009 static HRESULT
compile_variable_list(compiler_ctx_t
*ctx
, variable_declaration_t
*list
)
1011 variable_declaration_t
*iter
;
1014 assert(list
!= NULL
);
1017 ctx
->var_tail
->global_next
= list
;
1019 ctx
->var_head
= list
;
1021 for(iter
= list
; iter
; iter
= iter
->next
) {
1022 ctx
->func
->var_cnt
++;
1023 iter
->global_next
= iter
->next
;
1025 ctx
->var_tail
= iter
;
1030 hres
= compile_expression(ctx
, iter
->expr
);
1034 hres
= push_instr_bstr(ctx
, OP_var_set
, iter
->identifier
);
1042 /* ECMA-262 3rd Edition 12.2 */
1043 static HRESULT
compile_var_statement(compiler_ctx_t
*ctx
, var_statement_t
*stat
)
1047 hres
= compile_variable_list(ctx
, stat
->variable_list
);
1051 return push_instr(ctx
, OP_undefined
) ? S_OK
: E_OUTOFMEMORY
;
1054 /* ECMA-262 3rd Edition 12.4 */
1055 static HRESULT
compile_expression_statement(compiler_ctx_t
*ctx
, expression_statement_t
*stat
)
1057 BOOL no_ret
= FALSE
;
1060 hres
= compile_expression_noret(ctx
, stat
->expr
, &no_ret
);
1064 /* FIXME: that's a big potential optimization */
1065 if(no_ret
&& !push_instr(ctx
, OP_undefined
))
1066 return E_OUTOFMEMORY
;
1071 /* ECMA-262 3rd Edition 12.5 */
1072 static HRESULT
compile_if_statement(compiler_ctx_t
*ctx
, if_statement_t
*stat
)
1074 unsigned jmp_else
, jmp_end
;
1077 hres
= compile_expression(ctx
, stat
->expr
);
1081 jmp_else
= push_instr(ctx
, OP_jmp_z
);
1083 return E_OUTOFMEMORY
;
1085 hres
= compile_statement(ctx
, NULL
, stat
->if_stat
);
1089 jmp_end
= push_instr(ctx
, OP_jmp
);
1091 return E_OUTOFMEMORY
;
1093 set_arg_uint(ctx
, jmp_else
, ctx
->code_off
);
1095 if(stat
->else_stat
) {
1096 hres
= compile_statement(ctx
, NULL
, stat
->else_stat
);
1100 /* FIXME: We could sometimes avoid it */
1101 if(!push_instr(ctx
, OP_undefined
))
1102 return E_OUTOFMEMORY
;
1105 set_arg_uint(ctx
, jmp_end
, ctx
->code_off
);
1109 /* ECMA-262 3rd Edition 12.6.2 */
1110 static HRESULT
compile_while_statement(compiler_ctx_t
*ctx
, while_statement_t
*stat
)
1112 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
};
1116 stat_ctx
.break_label
= alloc_label(ctx
);
1117 if(!stat_ctx
.break_label
)
1118 return E_OUTOFMEMORY
;
1120 stat_ctx
.continue_label
= alloc_label(ctx
);
1121 if(!stat_ctx
.continue_label
)
1122 return E_OUTOFMEMORY
;
1124 if(!stat
->do_while
) {
1126 if(!push_instr(ctx
, OP_undefined
))
1127 return E_OUTOFMEMORY
;
1129 jmp_off
= ctx
->code_off
;
1130 label_set_addr(ctx
, stat_ctx
.continue_label
);
1131 hres
= compile_expression(ctx
, stat
->expr
);
1135 hres
= push_instr_uint(ctx
, OP_jmp_z
, stat_ctx
.break_label
);
1139 if(!push_instr(ctx
, OP_pop
))
1140 return E_OUTOFMEMORY
;
1142 jmp_off
= ctx
->code_off
;
1145 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1149 if(stat
->do_while
) {
1150 label_set_addr(ctx
, stat_ctx
.continue_label
);
1151 hres
= compile_expression(ctx
, stat
->expr
);
1155 hres
= push_instr_uint(ctx
, OP_jmp_z
, stat_ctx
.break_label
);
1159 if(!push_instr(ctx
, OP_pop
))
1160 return E_OUTOFMEMORY
;
1163 hres
= push_instr_uint(ctx
, OP_jmp
, jmp_off
);
1167 label_set_addr(ctx
, stat_ctx
.break_label
);
1171 /* ECMA-262 3rd Edition 12.6.3 */
1172 static HRESULT
compile_for_statement(compiler_ctx_t
*ctx
, for_statement_t
*stat
)
1174 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
};
1178 if(stat
->variable_list
) {
1179 hres
= compile_variable_list(ctx
, stat
->variable_list
);
1182 }else if(stat
->begin_expr
) {
1183 BOOL no_ret
= FALSE
;
1185 hres
= compile_expression_noret(ctx
, stat
->begin_expr
, &no_ret
);
1188 if(!no_ret
&& !push_instr(ctx
, OP_pop
))
1189 return E_OUTOFMEMORY
;
1192 stat_ctx
.break_label
= alloc_label(ctx
);
1193 if(!stat_ctx
.break_label
)
1194 return E_OUTOFMEMORY
;
1196 stat_ctx
.continue_label
= alloc_label(ctx
);
1197 if(!stat_ctx
.continue_label
)
1198 return E_OUTOFMEMORY
;
1201 if(!push_instr(ctx
, OP_undefined
))
1202 return E_OUTOFMEMORY
;
1204 expr_off
= ctx
->code_off
;
1207 hres
= compile_expression(ctx
, stat
->expr
);
1211 hres
= push_instr_uint(ctx
, OP_jmp_z
, stat_ctx
.break_label
);
1216 if(!push_instr(ctx
, OP_pop
))
1217 return E_OUTOFMEMORY
;
1219 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1223 label_set_addr(ctx
, stat_ctx
.continue_label
);
1225 if(stat
->end_expr
) {
1226 BOOL no_ret
= FALSE
;
1228 hres
= compile_expression_noret(ctx
, stat
->end_expr
, &no_ret
);
1232 if(!no_ret
&& !push_instr(ctx
, OP_pop
))
1233 return E_OUTOFMEMORY
;
1236 hres
= push_instr_uint(ctx
, OP_jmp
, expr_off
);
1240 label_set_addr(ctx
, stat_ctx
.break_label
);
1244 /* ECMA-262 3rd Edition 12.6.4 */
1245 static HRESULT
compile_forin_statement(compiler_ctx_t
*ctx
, forin_statement_t
*stat
)
1247 statement_ctx_t stat_ctx
= {4, FALSE
, FALSE
};
1250 if(stat
->variable
) {
1251 hres
= compile_variable_list(ctx
, stat
->variable
);
1256 stat_ctx
.break_label
= alloc_label(ctx
);
1257 if(!stat_ctx
.break_label
)
1258 return E_OUTOFMEMORY
;
1260 stat_ctx
.continue_label
= alloc_label(ctx
);
1261 if(!stat_ctx
.continue_label
)
1262 return E_OUTOFMEMORY
;
1264 hres
= compile_expression(ctx
, stat
->in_expr
);
1268 if(stat
->variable
) {
1269 hres
= push_instr_bstr_uint(ctx
, OP_identid
, stat
->variable
->identifier
, fdexNameEnsure
);
1272 }else if(is_memberid_expr(stat
->expr
->type
)) {
1273 hres
= compile_memberid_expression(ctx
, stat
->expr
, fdexNameEnsure
);
1277 hres
= push_instr_uint(ctx
, OP_throw_ref
, JS_E_ILLEGAL_ASSIGN
);
1281 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1285 hres
= push_instr_int(ctx
, OP_int
, DISPID_STARTENUM
);
1290 if(!push_instr(ctx
, OP_undefined
))
1291 return E_OUTOFMEMORY
;
1293 label_set_addr(ctx
, stat_ctx
.continue_label
);
1294 hres
= push_instr_uint(ctx
, OP_forin
, stat_ctx
.break_label
);
1296 return E_OUTOFMEMORY
;
1298 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1302 hres
= push_instr_uint(ctx
, OP_jmp
, stat_ctx
.continue_label
);
1306 label_set_addr(ctx
, stat_ctx
.break_label
);
1310 static HRESULT
pop_to_stat(compiler_ctx_t
*ctx
, BOOL var_stack
, BOOL scope_stack
, statement_ctx_t
*stat_ctx
)
1312 unsigned stack_pop
= 0;
1313 statement_ctx_t
*iter
;
1315 for(iter
= ctx
->stat_ctx
; iter
!= stat_ctx
; iter
= iter
->next
) {
1317 if(iter
->using_scope
&& !push_instr(ctx
, OP_pop_scope
))
1318 return E_OUTOFMEMORY
;
1319 if(iter
->using_except
&& !push_instr(ctx
, OP_pop_except
))
1320 return E_OUTOFMEMORY
;
1322 stack_pop
+= iter
->stack_use
;
1326 /* FIXME: optimize */
1327 while(stack_pop
--) {
1328 if(!push_instr(ctx
, OP_pop
))
1329 return E_OUTOFMEMORY
;
1337 /* ECMA-262 3rd Edition 12.7 */
1338 static HRESULT
compile_continue_statement(compiler_ctx_t
*ctx
, branch_statement_t
*stat
)
1340 statement_ctx_t
*pop_ctx
;
1343 if(stat
->identifier
) {
1344 statement_t
*label_stat
;
1345 statement_ctx_t
*iter
;
1349 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
1350 if(iter
->continue_label
)
1352 if(iter
->labelled_stat
&& !strcmpW(iter
->labelled_stat
->identifier
, stat
->identifier
))
1357 WARN("Label not found\n");
1358 return JS_E_LABEL_NOT_FOUND
;
1361 /* Labelled continue are allowed only on loops */
1362 for(label_stat
= iter
->labelled_stat
->statement
;
1363 label_stat
->type
== STAT_LABEL
;
1364 label_stat
= ((labelled_statement_t
*)label_stat
)->statement
);
1365 if(!is_loop_statement(label_stat
->type
)) {
1366 WARN("Label is not a loop\n");
1367 return JS_E_INVALID_CONTINUE
;
1370 for(pop_ctx
= ctx
->stat_ctx
; pop_ctx
; pop_ctx
= pop_ctx
->next
) {
1371 if(pop_ctx
->continue_label
)
1376 WARN("continue outside loop\n");
1377 return JS_E_INVALID_CONTINUE
;
1381 hres
= pop_to_stat(ctx
, TRUE
, TRUE
, pop_ctx
);
1385 if(!push_instr(ctx
, OP_undefined
))
1386 return E_OUTOFMEMORY
;
1388 return push_instr_uint(ctx
, OP_jmp
, pop_ctx
->continue_label
);
1391 /* ECMA-262 3rd Edition 12.8 */
1392 static HRESULT
compile_break_statement(compiler_ctx_t
*ctx
, branch_statement_t
*stat
)
1394 statement_ctx_t
*pop_ctx
;
1397 if(stat
->identifier
) {
1398 for(pop_ctx
= ctx
->stat_ctx
; pop_ctx
; pop_ctx
= pop_ctx
->next
) {
1399 if(pop_ctx
->labelled_stat
&& !strcmpW(pop_ctx
->labelled_stat
->identifier
, stat
->identifier
)) {
1400 assert(pop_ctx
->break_label
);
1406 WARN("Label not found\n");
1407 return JS_E_LABEL_NOT_FOUND
;
1410 for(pop_ctx
= ctx
->stat_ctx
; pop_ctx
; pop_ctx
= pop_ctx
->next
) {
1411 if(pop_ctx
->break_label
&& !pop_ctx
->labelled_stat
)
1416 WARN("Break outside loop\n");
1417 return JS_E_INVALID_BREAK
;
1421 hres
= pop_to_stat(ctx
, TRUE
, TRUE
, pop_ctx
->next
);
1425 if(!push_instr(ctx
, OP_undefined
))
1426 return E_OUTOFMEMORY
;
1428 return push_instr_uint(ctx
, OP_jmp
, pop_ctx
->break_label
);
1431 /* ECMA-262 3rd Edition 12.9 */
1432 static HRESULT
compile_return_statement(compiler_ctx_t
*ctx
, expression_statement_t
*stat
)
1436 hres
= pop_to_stat(ctx
, TRUE
, FALSE
, NULL
);
1441 hres
= compile_expression(ctx
, stat
->expr
);
1446 hres
= pop_to_stat(ctx
, FALSE
, TRUE
, NULL
);
1450 return push_instr(ctx
, OP_ret
) ? S_OK
: E_OUTOFMEMORY
;
1453 /* ECMA-262 3rd Edition 12.10 */
1454 static HRESULT
compile_with_statement(compiler_ctx_t
*ctx
, with_statement_t
*stat
)
1456 statement_ctx_t stat_ctx
= {0, TRUE
, FALSE
};
1459 hres
= compile_expression(ctx
, stat
->expr
);
1463 if(!push_instr(ctx
, OP_push_scope
))
1464 return E_OUTOFMEMORY
;
1466 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1470 if(!push_instr(ctx
, OP_pop_scope
))
1471 return E_OUTOFMEMORY
;
1476 /* ECMA-262 3rd Edition 12.10 */
1477 static HRESULT
compile_labelled_statement(compiler_ctx_t
*ctx
, labelled_statement_t
*stat
)
1479 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
, 0, 0, stat
}, *iter
;
1482 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
1483 if(iter
->labelled_stat
&& !strcmpW(iter
->labelled_stat
->identifier
, stat
->identifier
)) {
1484 WARN("Label %s redefined\n", debugstr_w(stat
->identifier
));
1485 return JS_E_LABEL_REDEFINED
;
1489 /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1490 stat_ctx
.break_label
= alloc_label(ctx
);
1491 if(!stat_ctx
.break_label
)
1492 return E_OUTOFMEMORY
;
1494 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1498 label_set_addr(ctx
, stat_ctx
.break_label
);
1502 /* ECMA-262 3rd Edition 12.13 */
1503 static HRESULT
compile_switch_statement(compiler_ctx_t
*ctx
, switch_statement_t
*stat
)
1505 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
};
1506 unsigned case_cnt
= 0, *case_jmps
, i
, default_jmp
;
1507 BOOL have_default
= FALSE
;
1508 statement_t
*stat_iter
;
1509 case_clausule_t
*iter
;
1512 hres
= compile_expression(ctx
, stat
->expr
);
1516 stat_ctx
.break_label
= alloc_label(ctx
);
1517 if(!stat_ctx
.break_label
)
1518 return E_OUTOFMEMORY
;
1520 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1525 case_jmps
= heap_alloc(case_cnt
* sizeof(*case_jmps
));
1527 return E_OUTOFMEMORY
;
1530 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1532 have_default
= TRUE
;
1536 hres
= compile_expression(ctx
, iter
->expr
);
1540 case_jmps
[i
] = push_instr(ctx
, OP_case
);
1542 hres
= E_OUTOFMEMORY
;
1548 if(SUCCEEDED(hres
)) {
1549 if(push_instr(ctx
, OP_pop
)) {
1550 default_jmp
= push_instr(ctx
, OP_jmp
);
1552 hres
= E_OUTOFMEMORY
;
1554 hres
= E_OUTOFMEMORY
;
1559 heap_free(case_jmps
);
1564 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1565 while(iter
->next
&& iter
->next
->stat
== iter
->stat
) {
1566 set_arg_uint(ctx
, iter
->expr
? case_jmps
[i
++] : default_jmp
, ctx
->code_off
);
1570 set_arg_uint(ctx
, iter
->expr
? case_jmps
[i
++] : default_jmp
, ctx
->code_off
);
1573 for(stat_iter
= iter
->stat
; stat_iter
&& (!iter
->next
|| iter
->next
->stat
!= stat_iter
);
1574 stat_iter
= stat_iter
->next
) {
1575 hres
= compile_statement(ctx
, &stat_ctx
, stat_iter
);
1579 if(stat_iter
->next
&& !push_instr(ctx
, OP_pop
)) {
1580 hres
= E_OUTOFMEMORY
;
1587 if(!push_instr(ctx
, OP_undefined
)) {
1588 hres
= E_OUTOFMEMORY
;
1594 heap_free(case_jmps
);
1597 assert(i
== case_cnt
);
1600 hres
= push_instr_uint(ctx
, OP_jmp
, stat_ctx
.break_label
);
1603 set_arg_uint(ctx
, default_jmp
, ctx
->code_off
);
1604 if(!push_instr(ctx
, OP_undefined
))
1605 return E_OUTOFMEMORY
;
1608 label_set_addr(ctx
, stat_ctx
.break_label
);
1612 /* ECMA-262 3rd Edition 12.13 */
1613 static HRESULT
compile_throw_statement(compiler_ctx_t
*ctx
, expression_statement_t
*stat
)
1617 hres
= compile_expression(ctx
, stat
->expr
);
1621 return push_instr(ctx
, OP_throw
) ? S_OK
: E_OUTOFMEMORY
;
1624 /* ECMA-262 3rd Edition 12.14 */
1625 static HRESULT
compile_try_statement(compiler_ctx_t
*ctx
, try_statement_t
*stat
)
1627 statement_ctx_t try_ctx
= {0, FALSE
, TRUE
}, catch_ctx
= {0, TRUE
, FALSE
};
1628 statement_ctx_t finally_ctx
= {2, FALSE
, FALSE
};
1629 unsigned push_except
;
1633 push_except
= push_instr(ctx
, OP_push_except
);
1635 return E_OUTOFMEMORY
;
1637 if(stat
->catch_block
) {
1638 ident
= compiler_alloc_bstr(ctx
, stat
->catch_block
->identifier
);
1640 return E_OUTOFMEMORY
;
1645 instr_ptr(ctx
, push_except
)->u
.arg
[1].bstr
= ident
;
1647 if(!stat
->catch_block
)
1648 try_ctx
.stack_use
= 2;
1650 hres
= compile_statement(ctx
, &try_ctx
, stat
->try_statement
);
1654 if(!push_instr(ctx
, OP_pop_except
))
1655 return E_OUTOFMEMORY
;
1657 if(stat
->catch_block
) {
1658 unsigned jmp_finally
;
1660 jmp_finally
= push_instr(ctx
, OP_jmp
);
1662 return E_OUTOFMEMORY
;
1664 instr_ptr(ctx
, push_except
)->u
.arg
[0].uint
= ctx
->code_off
;
1666 hres
= compile_statement(ctx
, &catch_ctx
, stat
->catch_block
->statement
);
1670 if(!push_instr(ctx
, OP_pop_scope
))
1671 return E_OUTOFMEMORY
;
1673 set_arg_uint(ctx
, jmp_finally
, ctx
->code_off
);
1675 set_arg_uint(ctx
, push_except
, ctx
->code_off
);
1678 if(stat
->finally_statement
) {
1680 if(!push_instr(ctx
, OP_pop
))
1681 return E_OUTOFMEMORY
;
1683 hres
= compile_statement(ctx
, stat
->catch_block
? NULL
: &finally_ctx
, stat
->finally_statement
);
1687 if(!stat
->catch_block
&& !push_instr(ctx
, OP_end_finally
))
1688 return E_OUTOFMEMORY
;
1694 static HRESULT
compile_statement(compiler_ctx_t
*ctx
, statement_ctx_t
*stat_ctx
, statement_t
*stat
)
1699 stat_ctx
->next
= ctx
->stat_ctx
;
1700 ctx
->stat_ctx
= stat_ctx
;
1703 switch(stat
->type
) {
1705 hres
= compile_block_statement(ctx
, ((block_statement_t
*)stat
)->stat_list
);
1708 hres
= compile_break_statement(ctx
, (branch_statement_t
*)stat
);
1711 hres
= compile_continue_statement(ctx
, (branch_statement_t
*)stat
);
1714 hres
= push_instr(ctx
, OP_undefined
) ? S_OK
: E_OUTOFMEMORY
; /* FIXME */
1717 hres
= compile_expression_statement(ctx
, (expression_statement_t
*)stat
);
1720 hres
= compile_for_statement(ctx
, (for_statement_t
*)stat
);
1723 hres
= compile_forin_statement(ctx
, (forin_statement_t
*)stat
);
1726 hres
= compile_if_statement(ctx
, (if_statement_t
*)stat
);
1729 hres
= compile_labelled_statement(ctx
, (labelled_statement_t
*)stat
);
1732 hres
= compile_return_statement(ctx
, (expression_statement_t
*)stat
);
1735 hres
= compile_switch_statement(ctx
, (switch_statement_t
*)stat
);
1738 hres
= compile_throw_statement(ctx
, (expression_statement_t
*)stat
);
1741 hres
= compile_try_statement(ctx
, (try_statement_t
*)stat
);
1744 hres
= compile_var_statement(ctx
, (var_statement_t
*)stat
);
1747 hres
= compile_while_statement(ctx
, (while_statement_t
*)stat
);
1750 hres
= compile_with_statement(ctx
, (with_statement_t
*)stat
);
1758 assert(ctx
->stat_ctx
== stat_ctx
);
1759 ctx
->stat_ctx
= stat_ctx
->next
;
1765 static void resolve_labels(compiler_ctx_t
*ctx
, unsigned off
)
1769 for(instr
= ctx
->code
->instrs
+off
; instr
< ctx
->code
->instrs
+ctx
->code_off
; instr
++) {
1770 if(instr_info
[instr
->op
].arg1_type
== ARG_ADDR
&& (instr
->u
.arg
->uint
& LABEL_FLAG
)) {
1771 assert((instr
->u
.arg
->uint
& ~LABEL_FLAG
) < ctx
->labels_cnt
);
1772 instr
->u
.arg
->uint
= ctx
->labels
[instr
->u
.arg
->uint
& ~LABEL_FLAG
];
1774 assert(instr_info
[instr
->op
].arg2_type
!= ARG_ADDR
);
1777 ctx
->labels_cnt
= 0;
1780 void release_bytecode(bytecode_t
*code
)
1787 for(i
=0; i
< code
->bstr_cnt
; i
++)
1788 SysFreeString(code
->bstr_pool
[i
]);
1790 heap_free(code
->source
);
1791 jsheap_free(&code
->heap
);
1792 heap_free(code
->bstr_pool
);
1793 heap_free(code
->instrs
);
1797 static HRESULT
init_code(compiler_ctx_t
*compiler
, const WCHAR
*source
)
1799 compiler
->code
= heap_alloc_zero(sizeof(bytecode_t
));
1801 return E_OUTOFMEMORY
;
1803 compiler
->code
->ref
= 1;
1804 jsheap_init(&compiler
->code
->heap
);
1806 compiler
->code
->source
= heap_strdupW(source
);
1807 if(!compiler
->code
->source
) {
1808 release_bytecode(compiler
->code
);
1809 return E_OUTOFMEMORY
;
1812 compiler
->code
->instrs
= heap_alloc(64 * sizeof(instr_t
));
1813 if(!compiler
->code
->instrs
) {
1814 release_bytecode(compiler
->code
);
1815 return E_OUTOFMEMORY
;
1818 compiler
->code_size
= 64;
1819 compiler
->code_off
= 1;
1823 static HRESULT
compile_function(compiler_ctx_t
*ctx
, source_elements_t
*source
, function_expression_t
*func_expr
,
1824 BOOL from_eval
, function_code_t
*func
)
1826 variable_declaration_t
*var_iter
;
1827 function_expression_t
*iter
;
1833 ctx
->var_head
= ctx
->var_tail
= NULL
;
1834 ctx
->func_head
= ctx
->func_tail
= NULL
;
1836 off
= ctx
->code_off
;
1838 hres
= compile_block_statement(ctx
, source
->statement
);
1842 resolve_labels(ctx
, off
);
1844 if(!from_eval
&& !push_instr(ctx
, OP_pop
))
1845 return E_OUTOFMEMORY
;
1846 if(!push_instr(ctx
, OP_ret
))
1847 return E_OUTOFMEMORY
;
1849 if(TRACE_ON(jscript_disas
))
1850 dump_code(ctx
, off
);
1852 func
->instr_off
= off
;
1854 if(func_expr
&& func_expr
->identifier
) {
1855 func
->name
= compiler_alloc_bstr(ctx
, func_expr
->identifier
);
1857 return E_OUTOFMEMORY
;
1861 parameter_t
*param_iter
;
1863 func
->source
= func_expr
->src_str
;
1864 func
->source_len
= func_expr
->src_len
;
1866 for(param_iter
= func_expr
->parameter_list
; param_iter
; param_iter
= param_iter
->next
)
1869 func
->params
= compiler_alloc(ctx
->code
, func
->param_cnt
* sizeof(*func
->params
));
1871 return E_OUTOFMEMORY
;
1873 for(param_iter
= func_expr
->parameter_list
, i
=0; param_iter
; param_iter
= param_iter
->next
, i
++) {
1874 func
->params
[i
] = compiler_alloc_bstr(ctx
, param_iter
->identifier
);
1875 if(!func
->params
[i
])
1876 return E_OUTOFMEMORY
;
1880 func
->variables
= compiler_alloc(ctx
->code
, func
->var_cnt
* sizeof(*func
->variables
));
1881 if(!func
->variables
)
1882 return E_OUTOFMEMORY
;
1884 for(var_iter
= ctx
->var_head
, i
=0; var_iter
; var_iter
= var_iter
->global_next
, i
++) {
1885 func
->variables
[i
] = compiler_alloc_bstr(ctx
, var_iter
->identifier
);
1886 if(!func
->variables
[i
])
1887 return E_OUTOFMEMORY
;
1890 assert(i
== func
->var_cnt
);
1892 func
->funcs
= compiler_alloc(ctx
->code
, func
->func_cnt
* sizeof(*func
->funcs
));
1894 return E_OUTOFMEMORY
;
1895 memset(func
->funcs
, 0, func
->func_cnt
* sizeof(*func
->funcs
));
1897 for(iter
= ctx
->func_head
, i
=0; iter
; iter
= iter
->next
, i
++) {
1898 hres
= compile_function(ctx
, iter
->source_elements
, iter
, FALSE
, func
->funcs
+i
);
1903 assert(i
== func
->func_cnt
);
1908 static HRESULT
parse_arguments(compiler_ctx_t
*ctx
, const WCHAR
*args
, BSTR
*arg_array
, unsigned *args_size
)
1910 const WCHAR
*ptr
= args
, *ptr2
;
1911 unsigned arg_cnt
= 0;
1913 while(isspaceW(*ptr
))
1922 if(!isalphaW(*ptr
) && *ptr
!= '_') {
1923 FIXME("expected alpha or '_': %s\n", debugstr_w(ptr
));
1928 while(isalnumW(*ptr
) || *ptr
== '_')
1931 if(*ptr
&& *ptr
!= ',' && !isspaceW(*ptr
)) {
1932 FIXME("unexpected har %s\n", debugstr_w(ptr
));
1937 arg_array
[arg_cnt
] = compiler_alloc_bstr_len(ctx
, ptr2
, ptr
-ptr2
);
1938 if(!arg_array
[arg_cnt
])
1939 return E_OUTOFMEMORY
;
1943 while(isspaceW(*ptr
))
1948 FIXME("expected ',': %s\n", debugstr_w(ptr
));
1953 while(isspaceW(*ptr
))
1958 *args_size
= arg_cnt
;
1962 static HRESULT
compile_arguments(compiler_ctx_t
*ctx
, const WCHAR
*args
)
1966 hres
= parse_arguments(ctx
, args
, NULL
, &ctx
->code
->global_code
.param_cnt
);
1970 ctx
->code
->global_code
.params
= compiler_alloc(ctx
->code
,
1971 ctx
->code
->global_code
.param_cnt
* sizeof(*ctx
->code
->global_code
.params
));
1972 if(!ctx
->code
->global_code
.params
)
1973 return E_OUTOFMEMORY
;
1975 return parse_arguments(ctx
, args
, ctx
->code
->global_code
.params
, NULL
);
1978 HRESULT
compile_script(script_ctx_t
*ctx
, const WCHAR
*code
, const WCHAR
*args
, const WCHAR
*delimiter
,
1979 BOOL from_eval
, BOOL use_decode
, bytecode_t
**ret
)
1981 compiler_ctx_t compiler
= {0};
1984 hres
= init_code(&compiler
, code
);
1989 hres
= compile_arguments(&compiler
, args
);
1995 hres
= decode_source(compiler
.code
->source
);
1997 WARN("Decoding failed\n");
2002 hres
= script_parse(ctx
, compiler
.code
->source
, delimiter
, from_eval
, &compiler
.parser
);
2004 release_bytecode(compiler
.code
);
2008 hres
= compile_function(&compiler
, compiler
.parser
->source
, NULL
, from_eval
, &compiler
.code
->global_code
);
2009 parser_release(compiler
.parser
);
2011 release_bytecode(compiler
.code
);
2015 *ret
= compiler
.code
;