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
26 #include "wine/rbtree.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
30 WINE_DECLARE_DEBUG_CHANNEL(jscript_disas
);
32 typedef struct _statement_ctx_t
{
38 unsigned continue_label
;
40 const labelled_statement_t
*labelled_stat
;
42 struct _statement_ctx_t
*next
;
46 struct wine_rb_entry entry
;
51 typedef struct _compiler_ctx_t
{
64 struct wine_rb_tree locals
;
67 statement_ctx_t
*stat_ctx
;
68 function_code_t
*func
;
72 function_expression_t
*func_head
;
73 function_expression_t
*func_tail
;
80 instr_arg_type_t arg1_type
;
81 instr_arg_type_t arg2_type
;
83 #define X(n,a,b,c) {#n,b,c},
88 static void dump_instr_arg(instr_arg_type_t type
, instr_arg_t
*arg
)
92 TRACE_(jscript_disas
)("\t%s", debugstr_jsstr(arg
->str
));
95 TRACE_(jscript_disas
)("\t%s", debugstr_wn(arg
->bstr
, SysStringLen(arg
->bstr
)));
98 TRACE_(jscript_disas
)("\t%d", arg
->uint
);
102 TRACE_(jscript_disas
)("\t%u", arg
->uint
);
111 static void dump_code(compiler_ctx_t
*ctx
, unsigned off
)
115 for(instr
= ctx
->code
->instrs
+off
; instr
< ctx
->code
->instrs
+ctx
->code_off
; instr
++) {
116 TRACE_(jscript_disas
)("%d:\t%s", (int)(instr
-ctx
->code
->instrs
), instr_info
[instr
->op
].op_str
);
117 if(instr_info
[instr
->op
].arg1_type
== ARG_DBL
) {
118 TRACE_(jscript_disas
)("\t%lf", instr
->u
.dbl
);
120 dump_instr_arg(instr_info
[instr
->op
].arg1_type
, instr
->u
.arg
);
121 dump_instr_arg(instr_info
[instr
->op
].arg2_type
, instr
->u
.arg
+1);
123 TRACE_(jscript_disas
)("\n");
127 static HRESULT
compile_expression(compiler_ctx_t
*,expression_t
*,BOOL
);
128 static HRESULT
compile_statement(compiler_ctx_t
*,statement_ctx_t
*,statement_t
*);
130 static inline void *compiler_alloc(bytecode_t
*code
, size_t size
)
132 return heap_pool_alloc(&code
->heap
, size
);
135 jsstr_t
*compiler_alloc_string_len(compiler_ctx_t
*ctx
, const WCHAR
*str
, unsigned len
)
139 if(!ctx
->code
->str_pool_size
) {
140 ctx
->code
->str_pool
= heap_alloc(8 * sizeof(jsstr_t
*));
141 if(!ctx
->code
->str_pool
)
143 ctx
->code
->str_pool_size
= 8;
144 }else if(ctx
->code
->str_pool_size
== ctx
->code
->str_cnt
) {
147 new_pool
= heap_realloc(ctx
->code
->str_pool
, ctx
->code
->str_pool_size
*2*sizeof(jsstr_t
*));
151 ctx
->code
->str_pool
= new_pool
;
152 ctx
->code
->str_pool_size
*= 2;
155 new_str
= jsstr_alloc_len(str
, len
);
159 ctx
->code
->str_pool
[ctx
->code
->str_cnt
++] = new_str
;
163 static jsstr_t
*compiler_alloc_string(compiler_ctx_t
*ctx
, const WCHAR
*str
)
165 return compiler_alloc_string_len(ctx
, str
, lstrlenW(str
));
168 static BOOL
ensure_bstr_slot(compiler_ctx_t
*ctx
)
170 if(!ctx
->code
->bstr_pool_size
) {
171 ctx
->code
->bstr_pool
= heap_alloc(8 * sizeof(BSTR
));
172 if(!ctx
->code
->bstr_pool
)
174 ctx
->code
->bstr_pool_size
= 8;
175 }else if(ctx
->code
->bstr_pool_size
== ctx
->code
->bstr_cnt
) {
178 new_pool
= heap_realloc(ctx
->code
->bstr_pool
, ctx
->code
->bstr_pool_size
*2*sizeof(BSTR
));
182 ctx
->code
->bstr_pool
= new_pool
;
183 ctx
->code
->bstr_pool_size
*= 2;
189 static BSTR
compiler_alloc_bstr(compiler_ctx_t
*ctx
, const WCHAR
*str
)
191 if(!ensure_bstr_slot(ctx
))
194 ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
] = SysAllocString(str
);
195 if(!ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
])
198 return ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
++];
201 static BSTR
compiler_alloc_bstr_len(compiler_ctx_t
*ctx
, const WCHAR
*str
, size_t len
)
203 if(!ensure_bstr_slot(ctx
))
206 ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
] = SysAllocStringLen(str
, len
);
207 if(!ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
])
210 return ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
++];
213 void set_compiler_loc(compiler_ctx_t
*ctx
, unsigned loc
)
218 static unsigned push_instr(compiler_ctx_t
*ctx
, jsop_t op
)
220 assert(ctx
->code_size
>= ctx
->code_off
);
222 if(ctx
->code_size
== ctx
->code_off
) {
225 new_instrs
= heap_realloc(ctx
->code
->instrs
, ctx
->code_size
*2*sizeof(instr_t
));
229 ctx
->code
->instrs
= new_instrs
;
233 ctx
->code
->instrs
[ctx
->code_off
].op
= op
;
234 ctx
->code
->instrs
[ctx
->code_off
].loc
= ctx
->loc
;
235 return ctx
->code_off
++;
238 static inline instr_t
*instr_ptr(compiler_ctx_t
*ctx
, unsigned off
)
240 assert(off
< ctx
->code_off
);
241 return ctx
->code
->instrs
+ off
;
244 static HRESULT
push_instr_int(compiler_ctx_t
*ctx
, jsop_t op
, LONG arg
)
248 instr
= push_instr(ctx
, op
);
250 return E_OUTOFMEMORY
;
252 instr_ptr(ctx
, instr
)->u
.arg
->lng
= arg
;
256 static HRESULT
push_instr_str(compiler_ctx_t
*ctx
, jsop_t op
, jsstr_t
*str
)
260 instr
= push_instr(ctx
, op
);
262 return E_OUTOFMEMORY
;
264 instr_ptr(ctx
, instr
)->u
.arg
->str
= str
;
268 static HRESULT
push_instr_str_uint(compiler_ctx_t
*ctx
, jsop_t op
, jsstr_t
*str
, unsigned arg2
)
272 instr
= push_instr(ctx
, op
);
274 return E_OUTOFMEMORY
;
276 instr_ptr(ctx
, instr
)->u
.arg
[0].str
= str
;
277 instr_ptr(ctx
, instr
)->u
.arg
[1].uint
= arg2
;
281 static HRESULT
push_instr_bstr(compiler_ctx_t
*ctx
, jsop_t op
, const WCHAR
*arg
)
286 str
= compiler_alloc_bstr(ctx
, arg
);
288 return E_OUTOFMEMORY
;
290 instr
= push_instr(ctx
, op
);
292 return E_OUTOFMEMORY
;
294 instr_ptr(ctx
, instr
)->u
.arg
->bstr
= str
;
298 static HRESULT
push_instr_bstr_uint(compiler_ctx_t
*ctx
, jsop_t op
, const WCHAR
*arg1
, unsigned arg2
)
303 str
= compiler_alloc_bstr(ctx
, arg1
);
305 return E_OUTOFMEMORY
;
307 instr
= push_instr(ctx
, op
);
309 return E_OUTOFMEMORY
;
311 instr_ptr(ctx
, instr
)->u
.arg
[0].bstr
= str
;
312 instr_ptr(ctx
, instr
)->u
.arg
[1].uint
= arg2
;
316 static HRESULT
push_instr_uint_str(compiler_ctx_t
*ctx
, jsop_t op
, unsigned arg1
, const WCHAR
*arg2
)
321 str
= compiler_alloc_string(ctx
, arg2
);
323 return E_OUTOFMEMORY
;
325 instr
= push_instr(ctx
, op
);
327 return E_OUTOFMEMORY
;
329 instr_ptr(ctx
, instr
)->u
.arg
[0].uint
= arg1
;
330 instr_ptr(ctx
, instr
)->u
.arg
[1].str
= str
;
334 static HRESULT
push_instr_double(compiler_ctx_t
*ctx
, jsop_t op
, double arg
)
338 instr
= push_instr(ctx
, op
);
340 return E_OUTOFMEMORY
;
342 instr_ptr(ctx
, instr
)->u
.dbl
= arg
;
346 static inline void set_arg_uint(compiler_ctx_t
*ctx
, unsigned instr
, unsigned arg
)
348 instr_ptr(ctx
, instr
)->u
.arg
->uint
= arg
;
351 static HRESULT
push_instr_uint(compiler_ctx_t
*ctx
, jsop_t op
, unsigned arg
)
355 instr
= push_instr(ctx
, op
);
357 return E_OUTOFMEMORY
;
359 set_arg_uint(ctx
, instr
, arg
);
363 static HRESULT
compile_binary_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
367 hres
= compile_expression(ctx
, expr
->expression1
, TRUE
);
371 hres
= compile_expression(ctx
, expr
->expression2
, TRUE
);
375 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
378 static HRESULT
compile_unary_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
, jsop_t op
)
382 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
386 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
389 /* ECMA-262 3rd Edition 11.2.1 */
390 static HRESULT
compile_member_expression(compiler_ctx_t
*ctx
, member_expression_t
*expr
)
394 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
398 return push_instr_bstr(ctx
, OP_member
, expr
->identifier
);
401 #define LABEL_FLAG 0x80000000
403 static unsigned alloc_label(compiler_ctx_t
*ctx
)
405 if(!ctx
->labels_size
) {
406 ctx
->labels
= heap_alloc(8 * sizeof(*ctx
->labels
));
409 ctx
->labels_size
= 8;
410 }else if(ctx
->labels_size
== ctx
->labels_cnt
) {
411 unsigned *new_labels
;
413 new_labels
= heap_realloc(ctx
->labels
, 2*ctx
->labels_size
*sizeof(*ctx
->labels
));
417 ctx
->labels
= new_labels
;
418 ctx
->labels_size
*= 2;
421 return ctx
->labels_cnt
++ | LABEL_FLAG
;
424 static void label_set_addr(compiler_ctx_t
*ctx
, unsigned label
)
426 assert(label
& LABEL_FLAG
);
427 ctx
->labels
[label
& ~LABEL_FLAG
] = ctx
->code_off
;
430 static inline BOOL
is_memberid_expr(expression_type_t type
)
432 return type
== EXPR_IDENT
|| type
== EXPR_MEMBER
|| type
== EXPR_ARRAY
;
435 static BOOL
bind_local(compiler_ctx_t
*ctx
, const WCHAR
*identifier
, int *ret_ref
)
437 statement_ctx_t
*iter
;
440 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
441 if(iter
->using_scope
)
445 ref
= lookup_local(ctx
->func
, identifier
);
453 static HRESULT
emit_identifier_ref(compiler_ctx_t
*ctx
, const WCHAR
*identifier
, unsigned flags
)
456 if(bind_local(ctx
, identifier
, &local_ref
))
457 return push_instr_int(ctx
, OP_local_ref
, local_ref
);
458 return push_instr_bstr_uint(ctx
, OP_identid
, identifier
, flags
);
461 static HRESULT
emit_identifier(compiler_ctx_t
*ctx
, const WCHAR
*identifier
)
464 if(bind_local(ctx
, identifier
, &local_ref
))
465 return push_instr_int(ctx
, OP_local
, local_ref
);
466 return push_instr_bstr(ctx
, OP_ident
, identifier
);
469 static HRESULT
compile_memberid_expression(compiler_ctx_t
*ctx
, expression_t
*expr
, unsigned flags
)
475 identifier_expression_t
*ident_expr
= (identifier_expression_t
*)expr
;
477 hres
= emit_identifier_ref(ctx
, ident_expr
->identifier
, flags
);
481 binary_expression_t
*array_expr
= (binary_expression_t
*)expr
;
483 hres
= compile_expression(ctx
, array_expr
->expression1
, TRUE
);
487 hres
= compile_expression(ctx
, array_expr
->expression2
, TRUE
);
491 hres
= push_instr_uint(ctx
, OP_memberid
, flags
);
495 member_expression_t
*member_expr
= (member_expression_t
*)expr
;
498 hres
= compile_expression(ctx
, member_expr
->expression
, TRUE
);
502 /* FIXME: Potential optimization */
503 jsstr
= compiler_alloc_string(ctx
, member_expr
->identifier
);
505 return E_OUTOFMEMORY
;
507 hres
= push_instr_str(ctx
, OP_str
, jsstr
);
511 hres
= push_instr_uint(ctx
, OP_memberid
, flags
);
520 static HRESULT
compile_increment_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
, jsop_t op
, int n
)
524 if(!is_memberid_expr(expr
->expression
->type
)) {
525 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
529 return push_instr_uint(ctx
, OP_throw_ref
, JS_E_ILLEGAL_ASSIGN
);
532 hres
= compile_memberid_expression(ctx
, expr
->expression
, fdexNameEnsure
);
536 return push_instr_int(ctx
, op
, n
);
539 /* ECMA-262 3rd Edition 11.14 */
540 static HRESULT
compile_comma_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, BOOL emit_ret
)
544 hres
= compile_expression(ctx
, expr
->expression1
, FALSE
);
548 return compile_expression(ctx
, expr
->expression2
, emit_ret
);
551 /* ECMA-262 3rd Edition 11.11 */
552 static HRESULT
compile_logical_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
557 hres
= compile_expression(ctx
, expr
->expression1
, TRUE
);
561 instr
= push_instr(ctx
, op
);
563 return E_OUTOFMEMORY
;
565 hres
= compile_expression(ctx
, expr
->expression2
, TRUE
);
569 set_arg_uint(ctx
, instr
, ctx
->code_off
);
573 /* ECMA-262 3rd Edition 11.12 */
574 static HRESULT
compile_conditional_expression(compiler_ctx_t
*ctx
, conditional_expression_t
*expr
)
576 unsigned jmp_false
, jmp_end
;
579 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
583 jmp_false
= push_instr(ctx
, OP_cnd_z
);
585 return E_OUTOFMEMORY
;
587 hres
= compile_expression(ctx
, expr
->true_expression
, TRUE
);
591 jmp_end
= push_instr(ctx
, OP_jmp
);
593 return E_OUTOFMEMORY
;
595 set_arg_uint(ctx
, jmp_false
, ctx
->code_off
);
596 hres
= push_instr_uint(ctx
, OP_pop
, 1);
600 hres
= compile_expression(ctx
, expr
->false_expression
, TRUE
);
604 set_arg_uint(ctx
, jmp_end
, ctx
->code_off
);
608 static HRESULT
compile_new_expression(compiler_ctx_t
*ctx
, call_expression_t
*expr
)
610 unsigned arg_cnt
= 0;
614 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
618 for(arg
= expr
->argument_list
; arg
; arg
= arg
->next
) {
619 hres
= compile_expression(ctx
, arg
->expr
, TRUE
);
625 hres
= push_instr_uint(ctx
, OP_new
, arg_cnt
);
629 hres
= push_instr_uint(ctx
, OP_pop
, arg_cnt
+1);
633 return push_instr(ctx
, OP_push_acc
) ? S_OK
: E_OUTOFMEMORY
;
636 static HRESULT
compile_call_expression(compiler_ctx_t
*ctx
, call_expression_t
*expr
, BOOL emit_ret
)
638 unsigned arg_cnt
= 0, extra_args
;
644 if(is_memberid_expr(expr
->expression
->type
)) {
647 hres
= compile_memberid_expression(ctx
, expr
->expression
, 0);
651 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
657 for(arg
= expr
->argument_list
; arg
; arg
= arg
->next
) {
658 hres
= compile_expression(ctx
, arg
->expr
, TRUE
);
664 instr
= push_instr(ctx
, op
);
666 return E_OUTOFMEMORY
;
668 instr_ptr(ctx
, instr
)->u
.arg
[0].uint
= arg_cnt
;
669 instr_ptr(ctx
, instr
)->u
.arg
[1].lng
= emit_ret
;
671 hres
= push_instr_uint(ctx
, OP_pop
, arg_cnt
+ extra_args
);
675 return !emit_ret
|| push_instr(ctx
, OP_push_acc
) ? S_OK
: E_OUTOFMEMORY
;
678 static HRESULT
compile_delete_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
)
682 switch(expr
->expression
->type
) {
684 binary_expression_t
*array_expr
= (binary_expression_t
*)expr
->expression
;
686 hres
= compile_expression(ctx
, array_expr
->expression1
, TRUE
);
690 hres
= compile_expression(ctx
, array_expr
->expression2
, TRUE
);
694 if(!push_instr(ctx
, OP_delete
))
695 return E_OUTOFMEMORY
;
699 member_expression_t
*member_expr
= (member_expression_t
*)expr
->expression
;
702 hres
= compile_expression(ctx
, member_expr
->expression
, TRUE
);
706 /* FIXME: Potential optimization */
707 jsstr
= compiler_alloc_string(ctx
, member_expr
->identifier
);
709 return E_OUTOFMEMORY
;
711 hres
= push_instr_str(ctx
, OP_str
, jsstr
);
715 if(!push_instr(ctx
, OP_delete
))
716 return E_OUTOFMEMORY
;
720 return push_instr_bstr(ctx
, OP_delete_ident
, ((identifier_expression_t
*)expr
->expression
)->identifier
);
722 WARN("invalid delete, unimplemented exception message\n");
724 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
728 return push_instr_uint_str(ctx
, OP_throw_type
, JS_E_INVALID_DELETE
, L
"FIXME");
735 static HRESULT
compile_assign_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
737 BOOL use_throw_path
= FALSE
;
738 unsigned arg_cnt
= 0;
741 if(expr
->expression1
->type
== EXPR_CALL
) {
742 call_expression_t
*call_expr
= (call_expression_t
*)expr
->expression1
;
745 if(is_memberid_expr(call_expr
->expression
->type
) && call_expr
->argument_list
) {
746 hres
= compile_memberid_expression(ctx
, call_expr
->expression
, fdexNameEnsure
);
750 for(arg
= call_expr
->argument_list
; arg
; arg
= arg
->next
) {
751 hres
= compile_expression(ctx
, arg
->expr
, TRUE
);
760 /* We need to call the functions twice: to get the value and to set it.
761 * JavaScript interpreted functions may to modify value on the stack,
762 * but assignment calls are allowed only on external functions, so we
763 * may reuse the stack here. */
764 instr
= push_instr(ctx
, OP_call_member
);
766 return E_OUTOFMEMORY
;
767 instr_ptr(ctx
, instr
)->u
.arg
[0].uint
= arg_cnt
;
768 instr_ptr(ctx
, instr
)->u
.arg
[1].lng
= 1;
770 if(!push_instr(ctx
, OP_push_acc
))
771 return E_OUTOFMEMORY
;
774 use_throw_path
= TRUE
;
776 }else if(is_memberid_expr(expr
->expression1
->type
)) {
777 hres
= compile_memberid_expression(ctx
, expr
->expression1
, fdexNameEnsure
);
780 if(op
!= OP_LAST
&& !push_instr(ctx
, OP_refval
))
781 return E_OUTOFMEMORY
;
783 use_throw_path
= TRUE
;
787 /* Illegal assignment: evaluate and throw */
788 hres
= compile_expression(ctx
, expr
->expression1
, TRUE
);
792 hres
= compile_expression(ctx
, expr
->expression2
, TRUE
);
796 if(op
!= OP_LAST
&& !push_instr(ctx
, op
))
797 return E_OUTOFMEMORY
;
799 return push_instr_uint(ctx
, OP_throw_ref
, JS_E_ILLEGAL_ASSIGN
);
802 hres
= compile_expression(ctx
, expr
->expression2
, TRUE
);
806 if(op
!= OP_LAST
&& !push_instr(ctx
, op
))
807 return E_OUTOFMEMORY
;
810 return push_instr_uint(ctx
, OP_assign_call
, arg_cnt
);
812 if(!push_instr(ctx
, OP_assign
))
813 return E_OUTOFMEMORY
;
818 static HRESULT
compile_typeof_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
)
823 if(is_memberid_expr(expr
->expression
->type
)) {
824 if(expr
->expression
->type
== EXPR_IDENT
)
825 return push_instr_bstr(ctx
, OP_typeofident
, ((identifier_expression_t
*)expr
->expression
)->identifier
);
828 hres
= compile_memberid_expression(ctx
, expr
->expression
, 0);
831 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
836 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
839 static HRESULT
compile_literal(compiler_ctx_t
*ctx
, literal_t
*literal
)
841 switch(literal
->type
) {
843 return push_instr_int(ctx
, OP_bool
, literal
->u
.bval
);
845 return push_instr_double(ctx
, OP_double
, literal
->u
.dval
);
847 return push_instr(ctx
, OP_null
) ? S_OK
: E_OUTOFMEMORY
;
849 return push_instr_str(ctx
, OP_str
, literal
->u
.str
);
851 return push_instr_str_uint(ctx
, OP_regexp
, literal
->u
.regexp
.str
, literal
->u
.regexp
.flags
);
857 static HRESULT
literal_as_string(compiler_ctx_t
*ctx
, literal_t
*literal
, jsstr_t
**str
)
859 switch(literal
->type
) {
861 *str
= literal
->u
.str
;
864 return double_to_string(literal
->u
.dval
, str
);
868 return *str
? S_OK
: E_OUTOFMEMORY
;
871 static HRESULT
compile_array_literal(compiler_ctx_t
*ctx
, array_literal_expression_t
*expr
)
874 array_element_t
*iter
;
875 unsigned array_instr
;
878 array_instr
= push_instr(ctx
, OP_carray
);
880 for(iter
= expr
->element_list
; iter
; iter
= iter
->next
) {
881 length
+= iter
->elision
;
883 hres
= compile_expression(ctx
, iter
->expr
, TRUE
);
887 hres
= push_instr_uint(ctx
, OP_carray_set
, length
);
894 instr_ptr(ctx
, array_instr
)->u
.arg
[0].uint
= length
+ expr
->length
;
898 static HRESULT
compile_object_literal(compiler_ctx_t
*ctx
, property_value_expression_t
*expr
)
900 property_definition_t
*iter
;
904 if(!push_instr(ctx
, OP_new_obj
))
905 return E_OUTOFMEMORY
;
907 for(iter
= expr
->property_list
; iter
; iter
= iter
->next
) {
908 hres
= literal_as_string(ctx
, iter
->name
, &name
);
912 hres
= compile_expression(ctx
, iter
->value
, TRUE
);
916 hres
= push_instr_str_uint(ctx
, OP_obj_prop
, name
, iter
->type
);
924 static HRESULT
compile_function_expression(compiler_ctx_t
*ctx
, function_expression_t
*expr
, BOOL emit_ret
)
926 return emit_ret
? push_instr_uint(ctx
, OP_func
, expr
->func_id
) : S_OK
;
929 static HRESULT
compile_expression(compiler_ctx_t
*ctx
, expression_t
*expr
, BOOL emit_ret
)
935 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_add
);
938 hres
= compile_logical_expression(ctx
, (binary_expression_t
*)expr
, OP_cnd_z
);
941 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_array
);
944 hres
= compile_array_literal(ctx
, (array_literal_expression_t
*)expr
);
947 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_LAST
);
950 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_add
);
953 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_and
);
956 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
959 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_mul
);
962 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_div
);
965 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_mod
);
968 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_or
);
970 case EXPR_ASSIGNLSHIFT
:
971 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_lshift
);
973 case EXPR_ASSIGNRSHIFT
:
974 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift
);
976 case EXPR_ASSIGNRRSHIFT
:
977 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift2
);
980 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_xor
);
983 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_and
);
986 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_bneg
);
989 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_or
);
992 return compile_call_expression(ctx
, (call_expression_t
*)expr
, emit_ret
);
994 return compile_comma_expression(ctx
, (binary_expression_t
*)expr
, emit_ret
);
996 hres
= compile_conditional_expression(ctx
, (conditional_expression_t
*)expr
);
999 hres
= compile_delete_expression(ctx
, (unary_expression_t
*)expr
);
1002 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_div
);
1005 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_eq
);
1008 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_eq2
);
1011 return compile_function_expression(ctx
, (function_expression_t
*)expr
, emit_ret
);
1013 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gt
);
1015 case EXPR_GREATEREQ
:
1016 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gteq
);
1019 hres
= emit_identifier(ctx
, ((identifier_expression_t
*)expr
)->identifier
);
1022 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_in
);
1024 case EXPR_INSTANCEOF
:
1025 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_instanceof
);
1028 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lt
);
1031 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lteq
);
1034 hres
= compile_literal(ctx
, ((literal_expression_t
*)expr
)->literal
);
1037 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_neg
);
1040 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lshift
);
1043 hres
= compile_member_expression(ctx
, (member_expression_t
*)expr
);
1046 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_minus
);
1049 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mod
);
1052 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mul
);
1055 hres
= compile_new_expression(ctx
, (call_expression_t
*)expr
);
1058 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_neq
);
1061 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_neq2
);
1064 hres
= compile_logical_expression(ctx
, (binary_expression_t
*)expr
, OP_cnd_nz
);
1067 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_tonum
);
1070 hres
= compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_postinc
, -1);
1073 hres
= compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_postinc
, 1);
1076 hres
= compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_preinc
, -1);
1079 hres
= compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_preinc
, 1);
1082 hres
= compile_object_literal(ctx
, (property_value_expression_t
*)expr
);
1085 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift
);
1088 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift2
);
1091 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
1094 return !emit_ret
|| push_instr(ctx
, OP_this
) ? S_OK
: E_OUTOFMEMORY
;
1096 hres
= compile_typeof_expression(ctx
, (unary_expression_t
*)expr
);
1099 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_void
);
1102 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_xor
);
1104 DEFAULT_UNREACHABLE
;
1110 return emit_ret
? S_OK
: push_instr_uint(ctx
, OP_pop
, 1);
1113 static inline BOOL
is_loop_statement(statement_type_t type
)
1115 return type
== STAT_FOR
|| type
== STAT_FORIN
|| type
== STAT_WHILE
;
1118 /* ECMA-262 3rd Edition 12.1 */
1119 static HRESULT
compile_block_statement(compiler_ctx_t
*ctx
, statement_t
*iter
)
1124 hres
= compile_statement(ctx
, NULL
, iter
);
1134 /* ECMA-262 3rd Edition 12.2 */
1135 static HRESULT
compile_variable_list(compiler_ctx_t
*ctx
, variable_declaration_t
*list
)
1137 variable_declaration_t
*iter
;
1140 assert(list
!= NULL
);
1142 for(iter
= list
; iter
; iter
= iter
->next
) {
1146 hres
= emit_identifier_ref(ctx
, iter
->identifier
, 0);
1150 hres
= compile_expression(ctx
, iter
->expr
, TRUE
);
1154 if(!push_instr(ctx
, OP_assign
))
1155 return E_OUTOFMEMORY
;
1157 hres
= push_instr_uint(ctx
, OP_pop
, 1);
1165 /* ECMA-262 3rd Edition 12.2 */
1166 static HRESULT
compile_var_statement(compiler_ctx_t
*ctx
, var_statement_t
*stat
)
1168 return compile_variable_list(ctx
, stat
->variable_list
);
1171 /* ECMA-262 3rd Edition 12.4 */
1172 static HRESULT
compile_expression_statement(compiler_ctx_t
*ctx
, expression_statement_t
*stat
)
1176 hres
= compile_expression(ctx
, stat
->expr
, ctx
->from_eval
);
1180 return !ctx
->from_eval
|| push_instr(ctx
, OP_setret
) ? S_OK
: E_OUTOFMEMORY
;
1183 /* ECMA-262 3rd Edition 12.5 */
1184 static HRESULT
compile_if_statement(compiler_ctx_t
*ctx
, if_statement_t
*stat
)
1189 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1193 jmp_else
= push_instr(ctx
, OP_jmp_z
);
1195 return E_OUTOFMEMORY
;
1197 hres
= compile_statement(ctx
, NULL
, stat
->if_stat
);
1201 if(stat
->else_stat
) {
1204 jmp_end
= push_instr(ctx
, OP_jmp
);
1206 return E_OUTOFMEMORY
;
1208 set_arg_uint(ctx
, jmp_else
, ctx
->code_off
);
1210 hres
= compile_statement(ctx
, NULL
, stat
->else_stat
);
1214 set_arg_uint(ctx
, jmp_end
, ctx
->code_off
);
1216 set_arg_uint(ctx
, jmp_else
, ctx
->code_off
);
1222 /* ECMA-262 3rd Edition 12.6.2 */
1223 static HRESULT
compile_while_statement(compiler_ctx_t
*ctx
, while_statement_t
*stat
)
1225 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
};
1229 stat_ctx
.break_label
= alloc_label(ctx
);
1230 if(!stat_ctx
.break_label
)
1231 return E_OUTOFMEMORY
;
1233 stat_ctx
.continue_label
= alloc_label(ctx
);
1234 if(!stat_ctx
.continue_label
)
1235 return E_OUTOFMEMORY
;
1237 jmp_off
= ctx
->code_off
;
1239 if(!stat
->do_while
) {
1240 label_set_addr(ctx
, stat_ctx
.continue_label
);
1241 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1245 hres
= push_instr_uint(ctx
, OP_jmp_z
, stat_ctx
.break_label
);
1250 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1254 set_compiler_loc(ctx
, stat
->stat
.loc
);
1255 if(stat
->do_while
) {
1256 label_set_addr(ctx
, stat_ctx
.continue_label
);
1257 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1261 hres
= push_instr_uint(ctx
, OP_jmp_z
, stat_ctx
.break_label
);
1266 hres
= push_instr_uint(ctx
, OP_jmp
, jmp_off
);
1270 label_set_addr(ctx
, stat_ctx
.break_label
);
1274 /* ECMA-262 3rd Edition 12.6.3 */
1275 static HRESULT
compile_for_statement(compiler_ctx_t
*ctx
, for_statement_t
*stat
)
1277 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
};
1281 if(stat
->variable_list
) {
1282 hres
= compile_variable_list(ctx
, stat
->variable_list
);
1285 }else if(stat
->begin_expr
) {
1286 hres
= compile_expression(ctx
, stat
->begin_expr
, FALSE
);
1291 stat_ctx
.break_label
= alloc_label(ctx
);
1292 if(!stat_ctx
.break_label
)
1293 return E_OUTOFMEMORY
;
1295 stat_ctx
.continue_label
= alloc_label(ctx
);
1296 if(!stat_ctx
.continue_label
)
1297 return E_OUTOFMEMORY
;
1299 expr_off
= ctx
->code_off
;
1302 set_compiler_loc(ctx
, stat
->expr_loc
);
1303 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1307 hres
= push_instr_uint(ctx
, OP_jmp_z
, stat_ctx
.break_label
);
1312 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1316 label_set_addr(ctx
, stat_ctx
.continue_label
);
1318 if(stat
->end_expr
) {
1319 set_compiler_loc(ctx
, stat
->end_loc
);
1320 hres
= compile_expression(ctx
, stat
->end_expr
, FALSE
);
1325 hres
= push_instr_uint(ctx
, OP_jmp
, expr_off
);
1329 label_set_addr(ctx
, stat_ctx
.break_label
);
1333 /* ECMA-262 3rd Edition 12.6.4 */
1334 static HRESULT
compile_forin_statement(compiler_ctx_t
*ctx
, forin_statement_t
*stat
)
1336 statement_ctx_t stat_ctx
= {4, FALSE
, FALSE
};
1339 if(stat
->variable
) {
1340 hres
= compile_variable_list(ctx
, stat
->variable
);
1345 stat_ctx
.break_label
= alloc_label(ctx
);
1346 if(!stat_ctx
.break_label
)
1347 return E_OUTOFMEMORY
;
1349 stat_ctx
.continue_label
= alloc_label(ctx
);
1350 if(!stat_ctx
.continue_label
)
1351 return E_OUTOFMEMORY
;
1353 hres
= compile_expression(ctx
, stat
->in_expr
, TRUE
);
1357 if(stat
->variable
) {
1358 hres
= emit_identifier_ref(ctx
, stat
->variable
->identifier
, fdexNameEnsure
);
1361 }else if(is_memberid_expr(stat
->expr
->type
)) {
1362 hres
= compile_memberid_expression(ctx
, stat
->expr
, fdexNameEnsure
);
1366 hres
= push_instr_uint(ctx
, OP_throw_ref
, JS_E_ILLEGAL_ASSIGN
);
1370 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1374 hres
= push_instr_int(ctx
, OP_int
, DISPID_STARTENUM
);
1378 label_set_addr(ctx
, stat_ctx
.continue_label
);
1379 hres
= push_instr_uint(ctx
, OP_forin
, stat_ctx
.break_label
);
1381 return E_OUTOFMEMORY
;
1383 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1387 hres
= push_instr_uint(ctx
, OP_jmp
, stat_ctx
.continue_label
);
1391 label_set_addr(ctx
, stat_ctx
.break_label
);
1395 static HRESULT
pop_to_stat(compiler_ctx_t
*ctx
, statement_ctx_t
*stat_ctx
)
1397 unsigned stack_pop
= 0;
1398 statement_ctx_t
*iter
;
1401 for(iter
= ctx
->stat_ctx
; iter
!= stat_ctx
; iter
= iter
->next
) {
1402 if(iter
->using_scope
&& !push_instr(ctx
, OP_pop_scope
))
1403 return E_OUTOFMEMORY
;
1404 if(iter
->using_except
) {
1406 hres
= push_instr_uint(ctx
, OP_pop
, stack_pop
);
1411 hres
= push_instr_uint(ctx
, OP_pop_except
, ctx
->code_off
+1);
1415 stack_pop
+= iter
->stack_use
;
1419 hres
= push_instr_uint(ctx
, OP_pop
, stack_pop
);
1427 /* ECMA-262 3rd Edition 12.7 */
1428 static HRESULT
compile_continue_statement(compiler_ctx_t
*ctx
, branch_statement_t
*stat
)
1430 statement_ctx_t
*pop_ctx
;
1433 if(stat
->identifier
) {
1434 statement_t
*label_stat
;
1435 statement_ctx_t
*iter
;
1439 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
1440 if(iter
->continue_label
)
1442 if(iter
->labelled_stat
&& !wcscmp(iter
->labelled_stat
->identifier
, stat
->identifier
))
1447 WARN("Label not found\n");
1448 return JS_E_LABEL_NOT_FOUND
;
1451 /* Labelled continue are allowed only on loops */
1452 for(label_stat
= iter
->labelled_stat
->statement
;
1453 label_stat
->type
== STAT_LABEL
;
1454 label_stat
= ((labelled_statement_t
*)label_stat
)->statement
);
1455 if(!is_loop_statement(label_stat
->type
)) {
1456 WARN("Label is not a loop\n");
1457 return JS_E_INVALID_CONTINUE
;
1460 assert(pop_ctx
!= NULL
);
1462 for(pop_ctx
= ctx
->stat_ctx
; pop_ctx
; pop_ctx
= pop_ctx
->next
) {
1463 if(pop_ctx
->continue_label
)
1468 WARN("continue outside loop\n");
1469 return JS_E_INVALID_CONTINUE
;
1473 hres
= pop_to_stat(ctx
, pop_ctx
);
1477 return push_instr_uint(ctx
, OP_jmp
, pop_ctx
->continue_label
);
1480 /* ECMA-262 3rd Edition 12.8 */
1481 static HRESULT
compile_break_statement(compiler_ctx_t
*ctx
, branch_statement_t
*stat
)
1483 statement_ctx_t
*pop_ctx
;
1486 if(stat
->identifier
) {
1487 for(pop_ctx
= ctx
->stat_ctx
; pop_ctx
; pop_ctx
= pop_ctx
->next
) {
1488 if(pop_ctx
->labelled_stat
&& !wcscmp(pop_ctx
->labelled_stat
->identifier
, stat
->identifier
)) {
1489 assert(pop_ctx
->break_label
);
1495 WARN("Label not found\n");
1496 return JS_E_LABEL_NOT_FOUND
;
1499 for(pop_ctx
= ctx
->stat_ctx
; pop_ctx
; pop_ctx
= pop_ctx
->next
) {
1500 if(pop_ctx
->break_label
&& !pop_ctx
->labelled_stat
)
1505 WARN("Break outside loop\n");
1506 return JS_E_INVALID_BREAK
;
1510 hres
= pop_to_stat(ctx
, pop_ctx
->next
);
1514 return push_instr_uint(ctx
, OP_jmp
, pop_ctx
->break_label
);
1517 /* ECMA-262 3rd Edition 12.9 */
1518 static HRESULT
compile_return_statement(compiler_ctx_t
*ctx
, expression_statement_t
*stat
)
1522 if(ctx
->from_eval
) {
1523 WARN("misplaced return statement\n");
1524 return JS_E_MISPLACED_RETURN
;
1528 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1531 if(!push_instr(ctx
, OP_setret
))
1532 return E_OUTOFMEMORY
;
1535 hres
= pop_to_stat(ctx
, NULL
);
1539 return push_instr_uint(ctx
, OP_ret
, !stat
->expr
);
1542 /* ECMA-262 3rd Edition 12.10 */
1543 static HRESULT
compile_with_statement(compiler_ctx_t
*ctx
, with_statement_t
*stat
)
1545 statement_ctx_t stat_ctx
= {0, TRUE
, FALSE
};
1548 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1552 if(!push_instr(ctx
, OP_push_scope
))
1553 return E_OUTOFMEMORY
;
1555 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1559 if(!push_instr(ctx
, OP_pop_scope
))
1560 return E_OUTOFMEMORY
;
1565 /* ECMA-262 3rd Edition 12.10 */
1566 static HRESULT
compile_labelled_statement(compiler_ctx_t
*ctx
, labelled_statement_t
*stat
)
1568 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
, 0, 0, stat
}, *iter
;
1571 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
1572 if(iter
->labelled_stat
&& !wcscmp(iter
->labelled_stat
->identifier
, stat
->identifier
)) {
1573 WARN("Label %s redefined\n", debugstr_w(stat
->identifier
));
1574 return JS_E_LABEL_REDEFINED
;
1578 /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1579 stat_ctx
.break_label
= alloc_label(ctx
);
1580 if(!stat_ctx
.break_label
)
1581 return E_OUTOFMEMORY
;
1583 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1587 label_set_addr(ctx
, stat_ctx
.break_label
);
1591 /* ECMA-262 3rd Edition 12.13 */
1592 static HRESULT
compile_switch_statement(compiler_ctx_t
*ctx
, switch_statement_t
*stat
)
1594 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
};
1595 unsigned case_cnt
= 0, *case_jmps
, i
, default_jmp
;
1596 BOOL have_default
= FALSE
;
1597 statement_t
*stat_iter
;
1598 case_clausule_t
*iter
;
1601 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1605 stat_ctx
.break_label
= alloc_label(ctx
);
1606 if(!stat_ctx
.break_label
)
1607 return E_OUTOFMEMORY
;
1609 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1614 case_jmps
= heap_alloc(case_cnt
* sizeof(*case_jmps
));
1616 return E_OUTOFMEMORY
;
1619 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1621 have_default
= TRUE
;
1625 set_compiler_loc(ctx
, iter
->loc
);
1626 hres
= compile_expression(ctx
, iter
->expr
, TRUE
);
1630 case_jmps
[i
] = push_instr(ctx
, OP_case
);
1632 hres
= E_OUTOFMEMORY
;
1638 if(SUCCEEDED(hres
)) {
1639 hres
= push_instr_uint(ctx
, OP_pop
, 1);
1640 if(SUCCEEDED(hres
)) {
1641 default_jmp
= push_instr(ctx
, OP_jmp
);
1643 hres
= E_OUTOFMEMORY
;
1648 heap_free(case_jmps
);
1653 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1654 while(iter
->next
&& iter
->next
->stat
== iter
->stat
) {
1655 set_arg_uint(ctx
, iter
->expr
? case_jmps
[i
++] : default_jmp
, ctx
->code_off
);
1659 set_arg_uint(ctx
, iter
->expr
? case_jmps
[i
++] : default_jmp
, ctx
->code_off
);
1661 for(stat_iter
= iter
->stat
; stat_iter
&& (!iter
->next
|| iter
->next
->stat
!= stat_iter
);
1662 stat_iter
= stat_iter
->next
) {
1663 hres
= compile_statement(ctx
, &stat_ctx
, stat_iter
);
1671 heap_free(case_jmps
);
1674 assert(i
== case_cnt
);
1677 hres
= push_instr_uint(ctx
, OP_jmp
, stat_ctx
.break_label
);
1680 set_arg_uint(ctx
, default_jmp
, ctx
->code_off
);
1683 label_set_addr(ctx
, stat_ctx
.break_label
);
1687 /* ECMA-262 3rd Edition 12.13 */
1688 static HRESULT
compile_throw_statement(compiler_ctx_t
*ctx
, expression_statement_t
*stat
)
1692 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1696 return push_instr(ctx
, OP_throw
) ? S_OK
: E_OUTOFMEMORY
;
1699 /* ECMA-262 3rd Edition 12.14 */
1700 static HRESULT
compile_try_statement(compiler_ctx_t
*ctx
, try_statement_t
*stat
)
1702 statement_ctx_t try_ctx
= {0, FALSE
, TRUE
}, finally_ctx
= {2, FALSE
, FALSE
};
1703 unsigned push_except
, finally_off
= 0, catch_off
= 0, pop_except
, catch_pop_except
= 0;
1707 push_except
= push_instr(ctx
, OP_push_except
);
1709 return E_OUTOFMEMORY
;
1711 if(stat
->catch_block
) {
1712 ident
= compiler_alloc_bstr(ctx
, stat
->catch_block
->identifier
);
1714 return E_OUTOFMEMORY
;
1719 hres
= compile_statement(ctx
, &try_ctx
, stat
->try_statement
);
1723 pop_except
= push_instr(ctx
, OP_pop_except
);
1725 return E_OUTOFMEMORY
;
1727 if(stat
->catch_block
) {
1728 statement_ctx_t catch_ctx
= {0, TRUE
, stat
->finally_statement
!= NULL
};
1730 if(stat
->finally_statement
)
1731 catch_ctx
.using_except
= TRUE
;
1733 catch_off
= ctx
->code_off
;
1735 hres
= push_instr_bstr(ctx
, OP_enter_catch
, ident
);
1739 hres
= compile_statement(ctx
, &catch_ctx
, stat
->catch_block
->statement
);
1743 if(!push_instr(ctx
, OP_pop_scope
))
1744 return E_OUTOFMEMORY
;
1746 if(stat
->finally_statement
) {
1747 catch_pop_except
= push_instr(ctx
, OP_pop_except
);
1748 if(!catch_pop_except
)
1749 return E_OUTOFMEMORY
;
1753 if(stat
->finally_statement
) {
1755 * finally block expects two elements on the stack, which may be:
1756 * - (true, return_addr) set by OP_pop_except, OP_end_finally jumps back to passed address
1757 * - (false, exception_value) set when unwinding an exception, which OP_end_finally rethrows
1759 finally_off
= ctx
->code_off
;
1760 hres
= compile_statement(ctx
, &finally_ctx
, stat
->finally_statement
);
1764 set_compiler_loc(ctx
, stat
->finally_loc
);
1765 if(!push_instr(ctx
, OP_end_finally
))
1766 return E_OUTOFMEMORY
;
1769 instr_ptr(ctx
, pop_except
)->u
.arg
[0].uint
= ctx
->code_off
;
1770 if(catch_pop_except
)
1771 instr_ptr(ctx
, catch_pop_except
)->u
.arg
[0].uint
= ctx
->code_off
;
1772 instr_ptr(ctx
, push_except
)->u
.arg
[0].uint
= catch_off
;
1773 instr_ptr(ctx
, push_except
)->u
.arg
[1].uint
= finally_off
;
1777 static HRESULT
compile_statement(compiler_ctx_t
*ctx
, statement_ctx_t
*stat_ctx
, statement_t
*stat
)
1782 stat_ctx
->next
= ctx
->stat_ctx
;
1783 ctx
->stat_ctx
= stat_ctx
;
1786 set_compiler_loc(ctx
, stat
->loc
);
1788 switch(stat
->type
) {
1790 hres
= compile_block_statement(ctx
, ((block_statement_t
*)stat
)->stat_list
);
1793 hres
= compile_break_statement(ctx
, (branch_statement_t
*)stat
);
1796 hres
= compile_continue_statement(ctx
, (branch_statement_t
*)stat
);
1803 hres
= compile_expression_statement(ctx
, (expression_statement_t
*)stat
);
1806 hres
= compile_for_statement(ctx
, (for_statement_t
*)stat
);
1809 hres
= compile_forin_statement(ctx
, (forin_statement_t
*)stat
);
1812 hres
= compile_if_statement(ctx
, (if_statement_t
*)stat
);
1815 hres
= compile_labelled_statement(ctx
, (labelled_statement_t
*)stat
);
1818 hres
= compile_return_statement(ctx
, (expression_statement_t
*)stat
);
1821 hres
= compile_switch_statement(ctx
, (switch_statement_t
*)stat
);
1824 hres
= compile_throw_statement(ctx
, (expression_statement_t
*)stat
);
1827 hres
= compile_try_statement(ctx
, (try_statement_t
*)stat
);
1830 hres
= compile_var_statement(ctx
, (var_statement_t
*)stat
);
1833 hres
= compile_while_statement(ctx
, (while_statement_t
*)stat
);
1836 hres
= compile_with_statement(ctx
, (with_statement_t
*)stat
);
1838 DEFAULT_UNREACHABLE
;
1842 assert(ctx
->stat_ctx
== stat_ctx
);
1843 ctx
->stat_ctx
= stat_ctx
->next
;
1849 static int function_local_cmp(const void *key
, const struct wine_rb_entry
*entry
)
1851 function_local_t
*local
= WINE_RB_ENTRY_VALUE(entry
, function_local_t
, entry
);
1852 return wcscmp(key
, local
->name
);
1855 static inline function_local_t
*find_local(compiler_ctx_t
*ctx
, const WCHAR
*name
)
1857 struct wine_rb_entry
*entry
= wine_rb_get(&ctx
->locals
, name
);
1858 return entry
? WINE_RB_ENTRY_VALUE(entry
, function_local_t
, entry
) : NULL
;
1861 static BOOL
alloc_local(compiler_ctx_t
*ctx
, BSTR name
, int ref
)
1863 function_local_t
*local
;
1865 local
= heap_pool_alloc(&ctx
->heap
, sizeof(*local
));
1871 wine_rb_put(&ctx
->locals
, name
, &local
->entry
);
1876 static BOOL
alloc_variable(compiler_ctx_t
*ctx
, const WCHAR
*name
)
1880 if(find_local(ctx
, name
))
1883 ident
= compiler_alloc_bstr(ctx
, name
);
1887 return alloc_local(ctx
, ident
, ctx
->func
->var_cnt
++);
1890 static HRESULT
visit_function_expression(compiler_ctx_t
*ctx
, function_expression_t
*expr
)
1892 expr
->func_id
= ctx
->func
->func_cnt
++;
1893 ctx
->func_tail
= ctx
->func_tail
? (ctx
->func_tail
->next
= expr
) : (ctx
->func_head
= expr
);
1895 return !expr
->identifier
|| expr
->event_target
|| alloc_variable(ctx
, expr
->identifier
)
1896 ? S_OK
: E_OUTOFMEMORY
;
1899 static HRESULT
visit_expression(compiler_ctx_t
*ctx
, expression_t
*expr
)
1901 HRESULT hres
= S_OK
;
1903 switch(expr
->type
) {
1908 case EXPR_ASSIGNADD
:
1909 case EXPR_ASSIGNAND
:
1910 case EXPR_ASSIGNSUB
:
1911 case EXPR_ASSIGNMUL
:
1912 case EXPR_ASSIGNDIV
:
1913 case EXPR_ASSIGNMOD
:
1915 case EXPR_ASSIGNLSHIFT
:
1916 case EXPR_ASSIGNRSHIFT
:
1917 case EXPR_ASSIGNRRSHIFT
:
1918 case EXPR_ASSIGNXOR
:
1926 case EXPR_GREATEREQ
:
1928 case EXPR_INSTANCEOF
:
1941 binary_expression_t
*binary_expr
= (binary_expression_t
*)expr
;
1943 hres
= visit_expression(ctx
, binary_expr
->expression1
);
1947 hres
= visit_expression(ctx
, binary_expr
->expression2
);
1961 hres
= visit_expression(ctx
, ((unary_expression_t
*)expr
)->expression
);
1967 case EXPR_ARRAYLIT
: {
1968 array_literal_expression_t
*array_expr
= (array_literal_expression_t
*)expr
;
1969 array_element_t
*iter
;
1971 for(iter
= array_expr
->element_list
; iter
; iter
= iter
->next
) {
1972 hres
= visit_expression(ctx
, iter
->expr
);
1980 call_expression_t
*call_expr
= (call_expression_t
*)expr
;
1983 hres
= visit_expression(ctx
, call_expr
->expression
);
1987 for(arg
= call_expr
->argument_list
; arg
; arg
= arg
->next
) {
1988 hres
= visit_expression(ctx
, arg
->expr
);
1995 conditional_expression_t
*cond_expr
= (conditional_expression_t
*)expr
;
1997 hres
= visit_expression(ctx
, cond_expr
->expression
);
2001 hres
= visit_expression(ctx
, cond_expr
->true_expression
);
2005 hres
= visit_expression(ctx
, cond_expr
->false_expression
);
2009 hres
= visit_function_expression(ctx
, (function_expression_t
*)expr
);
2012 hres
= visit_expression(ctx
, ((member_expression_t
*)expr
)->expression
);
2014 case EXPR_PROPVAL
: {
2015 property_definition_t
*iter
;
2016 for(iter
= ((property_value_expression_t
*)expr
)->property_list
; iter
; iter
= iter
->next
) {
2017 hres
= visit_expression(ctx
, iter
->value
);
2023 DEFAULT_UNREACHABLE
;
2029 static HRESULT
visit_variable_list(compiler_ctx_t
*ctx
, variable_declaration_t
*list
)
2031 variable_declaration_t
*iter
;
2034 for(iter
= list
; iter
; iter
= iter
->next
) {
2035 if(!alloc_variable(ctx
, iter
->identifier
))
2036 return E_OUTOFMEMORY
;
2039 hres
= visit_expression(ctx
, iter
->expr
);
2048 static HRESULT
visit_statement(compiler_ctx_t
*,statement_t
*);
2050 static HRESULT
visit_block_statement(compiler_ctx_t
*ctx
, statement_t
*iter
)
2055 hres
= visit_statement(ctx
, iter
);
2065 static HRESULT
visit_statement(compiler_ctx_t
*ctx
, statement_t
*stat
)
2067 HRESULT hres
= S_OK
;
2069 switch(stat
->type
) {
2071 hres
= visit_block_statement(ctx
, ((block_statement_t
*)stat
)->stat_list
);
2080 expression_statement_t
*expr_stat
= (expression_statement_t
*)stat
;
2082 hres
= visit_expression(ctx
, expr_stat
->expr
);
2086 for_statement_t
*for_stat
= (for_statement_t
*)stat
;
2088 if(for_stat
->variable_list
)
2089 hres
= visit_variable_list(ctx
, for_stat
->variable_list
);
2090 else if(for_stat
->begin_expr
)
2091 hres
= visit_expression(ctx
, for_stat
->begin_expr
);
2095 if(for_stat
->expr
) {
2096 hres
= visit_expression(ctx
, for_stat
->expr
);
2101 hres
= visit_statement(ctx
, for_stat
->statement
);
2105 if(for_stat
->end_expr
)
2106 hres
= visit_expression(ctx
, for_stat
->end_expr
);
2110 forin_statement_t
*forin_stat
= (forin_statement_t
*)stat
;
2112 if(forin_stat
->variable
) {
2113 hres
= visit_variable_list(ctx
, forin_stat
->variable
);
2118 hres
= visit_expression(ctx
, forin_stat
->in_expr
);
2122 if(forin_stat
->expr
) {
2123 hres
= visit_expression(ctx
, forin_stat
->expr
);
2128 hres
= visit_statement(ctx
, forin_stat
->statement
);
2132 if_statement_t
*if_stat
= (if_statement_t
*)stat
;
2134 hres
= visit_expression(ctx
, if_stat
->expr
);
2138 hres
= visit_statement(ctx
, if_stat
->if_stat
);
2142 if(if_stat
->else_stat
)
2143 hres
= visit_statement(ctx
, if_stat
->else_stat
);
2147 hres
= visit_statement(ctx
, ((labelled_statement_t
*)stat
)->statement
);
2150 switch_statement_t
*switch_stat
= (switch_statement_t
*)stat
;
2151 statement_t
*stat_iter
;
2152 case_clausule_t
*iter
;
2154 hres
= visit_expression(ctx
, switch_stat
->expr
);
2158 for(iter
= switch_stat
->case_list
; iter
; iter
= iter
->next
) {
2161 hres
= visit_expression(ctx
, iter
->expr
);
2166 for(iter
= switch_stat
->case_list
; iter
; iter
= iter
->next
) {
2167 while(iter
->next
&& iter
->next
->stat
== iter
->stat
)
2169 for(stat_iter
= iter
->stat
; stat_iter
&& (!iter
->next
|| iter
->next
->stat
!= stat_iter
);
2170 stat_iter
= stat_iter
->next
) {
2171 hres
= visit_statement(ctx
, stat_iter
);
2179 try_statement_t
*try_stat
= (try_statement_t
*)stat
;
2181 hres
= visit_statement(ctx
, try_stat
->try_statement
);
2185 if(try_stat
->catch_block
) {
2186 hres
= visit_statement(ctx
, try_stat
->catch_block
->statement
);
2191 if(try_stat
->finally_statement
)
2192 hres
= visit_statement(ctx
, try_stat
->finally_statement
);
2196 hres
= visit_variable_list(ctx
, ((var_statement_t
*)stat
)->variable_list
);
2199 while_statement_t
*while_stat
= (while_statement_t
*)stat
;
2201 hres
= visit_expression(ctx
, while_stat
->expr
);
2205 hres
= visit_statement(ctx
, while_stat
->statement
);
2209 with_statement_t
*with_stat
= (with_statement_t
*)stat
;
2211 hres
= visit_expression(ctx
, with_stat
->expr
);
2215 hres
= visit_statement(ctx
, with_stat
->statement
);
2218 DEFAULT_UNREACHABLE
;
2224 static void resolve_labels(compiler_ctx_t
*ctx
, unsigned off
)
2228 for(instr
= ctx
->code
->instrs
+off
; instr
< ctx
->code
->instrs
+ctx
->code_off
; instr
++) {
2229 if(instr_info
[instr
->op
].arg1_type
== ARG_ADDR
&& (instr
->u
.arg
->uint
& LABEL_FLAG
)) {
2230 assert((instr
->u
.arg
->uint
& ~LABEL_FLAG
) < ctx
->labels_cnt
);
2231 instr
->u
.arg
->uint
= ctx
->labels
[instr
->u
.arg
->uint
& ~LABEL_FLAG
];
2233 assert(instr_info
[instr
->op
].arg2_type
!= ARG_ADDR
);
2236 ctx
->labels_cnt
= 0;
2239 void release_bytecode(bytecode_t
*code
)
2246 for(i
=0; i
< code
->bstr_cnt
; i
++)
2247 SysFreeString(code
->bstr_pool
[i
]);
2248 for(i
=0; i
< code
->str_cnt
; i
++)
2249 jsstr_release(code
->str_pool
[i
]);
2251 if(code
->named_item
)
2252 release_named_item(code
->named_item
);
2253 heap_free(code
->source
);
2254 heap_pool_free(&code
->heap
);
2255 heap_free(code
->bstr_pool
);
2256 heap_free(code
->str_pool
);
2257 heap_free(code
->instrs
);
2261 static HRESULT
init_code(compiler_ctx_t
*compiler
, const WCHAR
*source
, UINT64 source_context
, unsigned start_line
)
2263 size_t len
= source
? lstrlenW(source
) : 0;
2266 return E_OUTOFMEMORY
;
2268 compiler
->code
= heap_alloc_zero(sizeof(bytecode_t
));
2270 return E_OUTOFMEMORY
;
2272 compiler
->code
->ref
= 1;
2273 compiler
->code
->source_context
= source_context
;
2274 compiler
->code
->start_line
= start_line
;
2275 heap_pool_init(&compiler
->code
->heap
);
2277 compiler
->code
->source
= heap_alloc((len
+ 1) * sizeof(WCHAR
));
2278 if(!compiler
->code
->source
) {
2279 release_bytecode(compiler
->code
);
2280 return E_OUTOFMEMORY
;
2283 memcpy(compiler
->code
->source
, source
, len
* sizeof(WCHAR
));
2284 compiler
->code
->source
[len
] = 0;
2286 compiler
->code
->instrs
= heap_alloc(64 * sizeof(instr_t
));
2287 if(!compiler
->code
->instrs
) {
2288 release_bytecode(compiler
->code
);
2289 return E_OUTOFMEMORY
;
2292 compiler
->code_size
= 64;
2293 compiler
->code_off
= 1;
2297 static HRESULT
compile_function(compiler_ctx_t
*ctx
, source_elements_t
*source
, function_expression_t
*func_expr
,
2298 BOOL from_eval
, function_code_t
*func
)
2300 function_expression_t
*iter
;
2301 function_local_t
*local
;
2307 func
->bytecode
= ctx
->code
;
2308 ctx
->func_head
= ctx
->func_tail
= NULL
;
2309 ctx
->from_eval
= from_eval
;
2311 ctx
->locals_cnt
= 0;
2312 wine_rb_init(&ctx
->locals
, function_local_cmp
);
2315 parameter_t
*param_iter
;
2317 if(func_expr
->identifier
) {
2318 func
->name
= compiler_alloc_bstr(ctx
, func_expr
->identifier
);
2320 return E_OUTOFMEMORY
;
2323 if(func_expr
->event_target
) {
2324 func
->event_target
= compiler_alloc_bstr(ctx
, func_expr
->event_target
);
2325 if(!func
->event_target
)
2326 return E_OUTOFMEMORY
;
2329 func
->source
= func_expr
->src_str
;
2330 func
->source_len
= func_expr
->src_len
;
2332 for(param_iter
= func_expr
->parameter_list
; param_iter
; param_iter
= param_iter
->next
)
2335 func
->params
= compiler_alloc(ctx
->code
, func
->param_cnt
* sizeof(*func
->params
));
2337 return E_OUTOFMEMORY
;
2339 for(param_iter
= func_expr
->parameter_list
, i
=0; param_iter
; param_iter
= param_iter
->next
, i
++) {
2340 func
->params
[i
] = compiler_alloc_bstr(ctx
, param_iter
->identifier
);
2341 if(!func
->params
[i
])
2342 return E_OUTOFMEMORY
;
2346 for(i
= 0; i
< func
->param_cnt
; i
++) {
2347 if(!find_local(ctx
, func
->params
[i
]) && !alloc_local(ctx
, func
->params
[i
], -i
-1))
2348 return E_OUTOFMEMORY
;
2351 hres
= visit_block_statement(ctx
, source
->statement
);
2355 func
->locals
= compiler_alloc(ctx
->code
, ctx
->locals_cnt
* sizeof(*func
->locals
));
2357 return E_OUTOFMEMORY
;
2358 func
->locals_cnt
= ctx
->locals_cnt
;
2360 func
->variables
= compiler_alloc(ctx
->code
, func
->var_cnt
* sizeof(*func
->variables
));
2361 if(!func
->variables
)
2362 return E_OUTOFMEMORY
;
2365 WINE_RB_FOR_EACH_ENTRY(local
, &ctx
->locals
, function_local_t
, entry
) {
2366 func
->locals
[i
].name
= local
->name
;
2367 func
->locals
[i
].ref
= local
->ref
;
2368 if(local
->ref
>= 0) {
2369 func
->variables
[local
->ref
].name
= local
->name
;
2370 func
->variables
[local
->ref
].func_id
= -1;
2374 assert(i
== ctx
->locals_cnt
);
2376 func
->funcs
= compiler_alloc(ctx
->code
, func
->func_cnt
* sizeof(*func
->funcs
));
2378 return E_OUTOFMEMORY
;
2379 memset(func
->funcs
, 0, func
->func_cnt
* sizeof(*func
->funcs
));
2381 off
= ctx
->code_off
;
2382 hres
= compile_block_statement(ctx
, source
->statement
);
2386 resolve_labels(ctx
, off
);
2388 hres
= push_instr_uint(ctx
, OP_ret
, !from_eval
);
2392 if(TRACE_ON(jscript_disas
))
2393 dump_code(ctx
, off
);
2395 func
->instr_off
= off
;
2397 for(iter
= ctx
->func_head
, i
=0; iter
; iter
= iter
->next
, i
++) {
2398 hres
= compile_function(ctx
, iter
->source_elements
, iter
, FALSE
, func
->funcs
+i
);
2402 TRACE("[%d] func %s\n", i
, debugstr_w(func
->funcs
[i
].name
));
2403 if(func
->funcs
[i
].name
&& !func
->funcs
[i
].event_target
) {
2404 local_ref_t
*local_ref
= lookup_local(func
, func
->funcs
[i
].name
);
2405 func
->funcs
[i
].local_ref
= local_ref
->ref
;
2406 TRACE("found ref %s %d for %s\n", debugstr_w(local_ref
->name
), local_ref
->ref
, debugstr_w(func
->funcs
[i
].name
));
2407 if(local_ref
->ref
>= 0)
2408 func
->variables
[local_ref
->ref
].func_id
= i
;
2412 assert(i
== func
->func_cnt
);
2417 static HRESULT
parse_arguments(compiler_ctx_t
*ctx
, const WCHAR
*args
, BSTR
*arg_array
, unsigned *args_size
)
2419 const WCHAR
*ptr
= args
, *ptr2
;
2420 unsigned arg_cnt
= 0;
2422 while(iswspace(*ptr
))
2431 if(!iswalpha(*ptr
) && *ptr
!= '_') {
2432 FIXME("expected alpha or '_': %s\n", debugstr_w(ptr
));
2437 while(iswalnum(*ptr
) || *ptr
== '_')
2440 if(*ptr
&& *ptr
!= ',' && !iswspace(*ptr
)) {
2441 FIXME("unexpected har %s\n", debugstr_w(ptr
));
2446 arg_array
[arg_cnt
] = compiler_alloc_bstr_len(ctx
, ptr2
, ptr
-ptr2
);
2447 if(!arg_array
[arg_cnt
])
2448 return E_OUTOFMEMORY
;
2452 while(iswspace(*ptr
))
2457 FIXME("expected ',': %s\n", debugstr_w(ptr
));
2462 while(iswspace(*ptr
))
2467 *args_size
= arg_cnt
;
2471 static HRESULT
compile_arguments(compiler_ctx_t
*ctx
, const WCHAR
*args
)
2475 hres
= parse_arguments(ctx
, args
, NULL
, &ctx
->code
->global_code
.param_cnt
);
2479 ctx
->code
->global_code
.params
= compiler_alloc(ctx
->code
,
2480 ctx
->code
->global_code
.param_cnt
* sizeof(*ctx
->code
->global_code
.params
));
2481 if(!ctx
->code
->global_code
.params
)
2482 return E_OUTOFMEMORY
;
2484 return parse_arguments(ctx
, args
, ctx
->code
->global_code
.params
, NULL
);
2487 HRESULT
compile_script(script_ctx_t
*ctx
, const WCHAR
*code
, UINT64 source_context
, unsigned start_line
,
2488 const WCHAR
*args
, const WCHAR
*delimiter
, BOOL from_eval
, BOOL use_decode
,
2489 named_item_t
*named_item
, bytecode_t
**ret
)
2491 compiler_ctx_t compiler
= {0};
2494 hres
= init_code(&compiler
, code
, source_context
, start_line
);
2499 hres
= compile_arguments(&compiler
, args
);
2505 hres
= decode_source(compiler
.code
->source
);
2507 WARN("Decoding failed\n");
2512 hres
= script_parse(ctx
, &compiler
, compiler
.code
, delimiter
, from_eval
, &compiler
.parser
);
2514 release_bytecode(compiler
.code
);
2518 heap_pool_init(&compiler
.heap
);
2519 hres
= compile_function(&compiler
, compiler
.parser
->source
, NULL
, from_eval
, &compiler
.code
->global_code
);
2520 heap_pool_free(&compiler
.heap
);
2521 parser_release(compiler
.parser
);
2523 if(hres
!= DISP_E_EXCEPTION
)
2524 throw_error(ctx
, hres
, NULL
);
2525 set_error_location(ctx
->ei
, compiler
.code
, compiler
.loc
, IDS_COMPILATION_ERROR
, NULL
);
2526 release_bytecode(compiler
.code
);
2527 return DISP_E_EXCEPTION
;
2531 compiler
.code
->named_item
= named_item
;
2535 *ret
= compiler
.code
;