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/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
29 WINE_DECLARE_DEBUG_CHANNEL(jscript_disas
);
31 typedef struct _statement_ctx_t
{
37 unsigned continue_label
;
39 const labelled_statement_t
*labelled_stat
;
41 struct _statement_ctx_t
*next
;
57 statement_ctx_t
*stat_ctx
;
58 function_code_t
*func
;
60 variable_declaration_t
*var_head
;
61 variable_declaration_t
*var_tail
;
63 function_expression_t
*func_head
;
64 function_expression_t
*func_tail
;
69 instr_arg_type_t arg1_type
;
70 instr_arg_type_t arg2_type
;
72 #define X(n,a,b,c) {#n,b,c},
77 static void dump_instr_arg(instr_arg_type_t type
, instr_arg_t
*arg
)
81 TRACE_(jscript_disas
)("\t%s", debugstr_jsstr(arg
->str
));
84 TRACE_(jscript_disas
)("\t%s", debugstr_wn(arg
->bstr
, SysStringLen(arg
->bstr
)));
87 TRACE_(jscript_disas
)("\t%d", arg
->uint
);
91 TRACE_(jscript_disas
)("\t%u", arg
->uint
);
100 static void dump_code(compiler_ctx_t
*ctx
, unsigned off
)
104 for(instr
= ctx
->code
->instrs
+off
; instr
< ctx
->code
->instrs
+ctx
->code_off
; instr
++) {
105 TRACE_(jscript_disas
)("%d:\t%s", (int)(instr
-ctx
->code
->instrs
), instr_info
[instr
->op
].op_str
);
106 if(instr_info
[instr
->op
].arg1_type
== ARG_DBL
) {
107 TRACE_(jscript_disas
)("\t%lf", instr
->u
.dbl
);
109 dump_instr_arg(instr_info
[instr
->op
].arg1_type
, instr
->u
.arg
);
110 dump_instr_arg(instr_info
[instr
->op
].arg2_type
, instr
->u
.arg
+1);
112 TRACE_(jscript_disas
)("\n");
116 static HRESULT
compile_expression(compiler_ctx_t
*,expression_t
*,BOOL
);
117 static HRESULT
compile_statement(compiler_ctx_t
*,statement_ctx_t
*,statement_t
*);
119 static inline void *compiler_alloc(bytecode_t
*code
, size_t size
)
121 return heap_pool_alloc(&code
->heap
, size
);
124 static jsstr_t
*compiler_alloc_string_len(compiler_ctx_t
*ctx
, const WCHAR
*str
, unsigned len
)
128 if(!ctx
->code
->str_pool_size
) {
129 ctx
->code
->str_pool
= heap_alloc(8 * sizeof(jsstr_t
*));
130 if(!ctx
->code
->str_pool
)
132 ctx
->code
->str_pool_size
= 8;
133 }else if(ctx
->code
->str_pool_size
== ctx
->code
->str_cnt
) {
136 new_pool
= heap_realloc(ctx
->code
->str_pool
, ctx
->code
->str_pool_size
*2*sizeof(jsstr_t
*));
140 ctx
->code
->str_pool
= new_pool
;
141 ctx
->code
->str_pool_size
*= 2;
144 new_str
= jsstr_alloc_len(str
, len
);
148 ctx
->code
->str_pool
[ctx
->code
->str_cnt
++] = new_str
;
152 static jsstr_t
*compiler_alloc_string(compiler_ctx_t
*ctx
, const WCHAR
*str
)
154 return compiler_alloc_string_len(ctx
, str
, strlenW(str
));
157 static BOOL
ensure_bstr_slot(compiler_ctx_t
*ctx
)
159 if(!ctx
->code
->bstr_pool_size
) {
160 ctx
->code
->bstr_pool
= heap_alloc(8 * sizeof(BSTR
));
161 if(!ctx
->code
->bstr_pool
)
163 ctx
->code
->bstr_pool_size
= 8;
164 }else if(ctx
->code
->bstr_pool_size
== ctx
->code
->bstr_cnt
) {
167 new_pool
= heap_realloc(ctx
->code
->bstr_pool
, ctx
->code
->bstr_pool_size
*2*sizeof(BSTR
));
171 ctx
->code
->bstr_pool
= new_pool
;
172 ctx
->code
->bstr_pool_size
*= 2;
178 static BSTR
compiler_alloc_bstr(compiler_ctx_t
*ctx
, const WCHAR
*str
)
180 if(!ensure_bstr_slot(ctx
))
183 ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
] = SysAllocString(str
);
184 if(!ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
])
187 return ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
++];
190 static BSTR
compiler_alloc_bstr_len(compiler_ctx_t
*ctx
, const WCHAR
*str
, size_t len
)
192 if(!ensure_bstr_slot(ctx
))
195 ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
] = SysAllocStringLen(str
, len
);
196 if(!ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
])
199 return ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
++];
202 static unsigned push_instr(compiler_ctx_t
*ctx
, jsop_t op
)
204 assert(ctx
->code_size
>= ctx
->code_off
);
206 if(ctx
->code_size
== ctx
->code_off
) {
209 new_instrs
= heap_realloc(ctx
->code
->instrs
, ctx
->code_size
*2*sizeof(instr_t
));
213 ctx
->code
->instrs
= new_instrs
;
217 ctx
->code
->instrs
[ctx
->code_off
].op
= op
;
218 return ctx
->code_off
++;
221 static inline instr_t
*instr_ptr(compiler_ctx_t
*ctx
, unsigned off
)
223 assert(off
< ctx
->code_off
);
224 return ctx
->code
->instrs
+ off
;
227 static HRESULT
push_instr_int(compiler_ctx_t
*ctx
, jsop_t op
, LONG arg
)
231 instr
= push_instr(ctx
, op
);
233 return E_OUTOFMEMORY
;
235 instr_ptr(ctx
, instr
)->u
.arg
->lng
= arg
;
239 static HRESULT
push_instr_str(compiler_ctx_t
*ctx
, jsop_t op
, const WCHAR
*arg
)
244 str
= compiler_alloc_string(ctx
, arg
);
246 return E_OUTOFMEMORY
;
248 instr
= push_instr(ctx
, op
);
250 return E_OUTOFMEMORY
;
252 instr_ptr(ctx
, instr
)->u
.arg
->str
= str
;
256 static HRESULT
push_instr_bstr(compiler_ctx_t
*ctx
, jsop_t op
, const WCHAR
*arg
)
261 str
= compiler_alloc_bstr(ctx
, arg
);
263 return E_OUTOFMEMORY
;
265 instr
= push_instr(ctx
, op
);
267 return E_OUTOFMEMORY
;
269 instr_ptr(ctx
, instr
)->u
.arg
->bstr
= str
;
273 static HRESULT
push_instr_bstr_uint(compiler_ctx_t
*ctx
, jsop_t op
, const WCHAR
*arg1
, unsigned arg2
)
278 str
= compiler_alloc_bstr(ctx
, arg1
);
280 return E_OUTOFMEMORY
;
282 instr
= push_instr(ctx
, op
);
284 return E_OUTOFMEMORY
;
286 instr_ptr(ctx
, instr
)->u
.arg
[0].bstr
= str
;
287 instr_ptr(ctx
, instr
)->u
.arg
[1].uint
= arg2
;
291 static HRESULT
push_instr_uint_str(compiler_ctx_t
*ctx
, jsop_t op
, unsigned arg1
, const WCHAR
*arg2
)
296 str
= compiler_alloc_string(ctx
, arg2
);
298 return E_OUTOFMEMORY
;
300 instr
= push_instr(ctx
, op
);
302 return E_OUTOFMEMORY
;
304 instr_ptr(ctx
, instr
)->u
.arg
[0].uint
= arg1
;
305 instr_ptr(ctx
, instr
)->u
.arg
[1].str
= str
;
309 static HRESULT
push_instr_double(compiler_ctx_t
*ctx
, jsop_t op
, double arg
)
313 instr
= push_instr(ctx
, op
);
315 return E_OUTOFMEMORY
;
317 instr_ptr(ctx
, instr
)->u
.dbl
= arg
;
321 static inline void set_arg_uint(compiler_ctx_t
*ctx
, unsigned instr
, unsigned arg
)
323 instr_ptr(ctx
, instr
)->u
.arg
->uint
= arg
;
326 static HRESULT
push_instr_uint(compiler_ctx_t
*ctx
, jsop_t op
, unsigned arg
)
330 instr
= push_instr(ctx
, op
);
332 return E_OUTOFMEMORY
;
334 set_arg_uint(ctx
, instr
, arg
);
338 static HRESULT
compile_binary_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
342 hres
= compile_expression(ctx
, expr
->expression1
, TRUE
);
346 hres
= compile_expression(ctx
, expr
->expression2
, TRUE
);
350 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
353 static HRESULT
compile_unary_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
, jsop_t op
)
357 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
361 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
364 /* ECMA-262 3rd Edition 11.2.1 */
365 static HRESULT
compile_member_expression(compiler_ctx_t
*ctx
, member_expression_t
*expr
)
369 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
373 return push_instr_bstr(ctx
, OP_member
, expr
->identifier
);
376 #define LABEL_FLAG 0x80000000
378 static unsigned alloc_label(compiler_ctx_t
*ctx
)
380 if(!ctx
->labels_size
) {
381 ctx
->labels
= heap_alloc(8 * sizeof(*ctx
->labels
));
384 ctx
->labels_size
= 8;
385 }else if(ctx
->labels_size
== ctx
->labels_cnt
) {
386 unsigned *new_labels
;
388 new_labels
= heap_realloc(ctx
->labels
, 2*ctx
->labels_size
*sizeof(*ctx
->labels
));
392 ctx
->labels
= new_labels
;
393 ctx
->labels_size
*= 2;
396 return ctx
->labels_cnt
++ | LABEL_FLAG
;
399 static void label_set_addr(compiler_ctx_t
*ctx
, unsigned label
)
401 assert(label
& LABEL_FLAG
);
402 ctx
->labels
[label
& ~LABEL_FLAG
] = ctx
->code_off
;
405 static inline BOOL
is_memberid_expr(expression_type_t type
)
407 return type
== EXPR_IDENT
|| type
== EXPR_MEMBER
|| type
== EXPR_ARRAY
;
410 static HRESULT
compile_memberid_expression(compiler_ctx_t
*ctx
, expression_t
*expr
, unsigned flags
)
416 identifier_expression_t
*ident_expr
= (identifier_expression_t
*)expr
;
418 hres
= push_instr_bstr_uint(ctx
, OP_identid
, ident_expr
->identifier
, flags
);
422 binary_expression_t
*array_expr
= (binary_expression_t
*)expr
;
424 hres
= compile_expression(ctx
, array_expr
->expression1
, TRUE
);
428 hres
= compile_expression(ctx
, array_expr
->expression2
, TRUE
);
432 hres
= push_instr_uint(ctx
, OP_memberid
, flags
);
436 member_expression_t
*member_expr
= (member_expression_t
*)expr
;
438 hres
= compile_expression(ctx
, member_expr
->expression
, TRUE
);
442 /* FIXME: Potential optimization */
443 hres
= push_instr_str(ctx
, OP_str
, member_expr
->identifier
);
447 hres
= push_instr_uint(ctx
, OP_memberid
, flags
);
456 static HRESULT
compile_increment_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
, jsop_t op
, int n
)
460 if(!is_memberid_expr(expr
->expression
->type
)) {
461 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
465 return push_instr_uint(ctx
, OP_throw_ref
, JS_E_ILLEGAL_ASSIGN
);
468 hres
= compile_memberid_expression(ctx
, expr
->expression
, fdexNameEnsure
);
472 return push_instr_int(ctx
, op
, n
);
475 /* ECMA-262 3rd Edition 11.14 */
476 static HRESULT
compile_comma_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, BOOL emit_ret
)
480 hres
= compile_expression(ctx
, expr
->expression1
, FALSE
);
484 return compile_expression(ctx
, expr
->expression2
, emit_ret
);
487 /* ECMA-262 3rd Edition 11.11 */
488 static HRESULT
compile_logical_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
493 hres
= compile_expression(ctx
, expr
->expression1
, TRUE
);
497 instr
= push_instr(ctx
, op
);
499 return E_OUTOFMEMORY
;
501 hres
= compile_expression(ctx
, expr
->expression2
, TRUE
);
505 set_arg_uint(ctx
, instr
, ctx
->code_off
);
509 /* ECMA-262 3rd Edition 11.12 */
510 static HRESULT
compile_conditional_expression(compiler_ctx_t
*ctx
, conditional_expression_t
*expr
)
512 unsigned jmp_false
, jmp_end
;
515 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
519 jmp_false
= push_instr(ctx
, OP_cnd_z
);
521 return E_OUTOFMEMORY
;
523 hres
= compile_expression(ctx
, expr
->true_expression
, TRUE
);
527 jmp_end
= push_instr(ctx
, OP_jmp
);
529 return E_OUTOFMEMORY
;
531 set_arg_uint(ctx
, jmp_false
, ctx
->code_off
);
532 hres
= push_instr_uint(ctx
, OP_pop
, 1);
536 hres
= compile_expression(ctx
, expr
->false_expression
, TRUE
);
540 set_arg_uint(ctx
, jmp_end
, ctx
->code_off
);
544 static HRESULT
compile_new_expression(compiler_ctx_t
*ctx
, call_expression_t
*expr
)
546 unsigned arg_cnt
= 0;
550 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
554 for(arg
= expr
->argument_list
; arg
; arg
= arg
->next
) {
555 hres
= compile_expression(ctx
, arg
->expr
, TRUE
);
561 hres
= push_instr_uint(ctx
, OP_new
, arg_cnt
);
565 hres
= push_instr_uint(ctx
, OP_pop
, arg_cnt
+1);
569 return push_instr(ctx
, OP_push_ret
) ? S_OK
: E_OUTOFMEMORY
;
572 static HRESULT
compile_call_expression(compiler_ctx_t
*ctx
, call_expression_t
*expr
, BOOL emit_ret
)
574 unsigned arg_cnt
= 0, extra_args
;
580 if(is_memberid_expr(expr
->expression
->type
)) {
583 hres
= compile_memberid_expression(ctx
, expr
->expression
, 0);
587 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
593 for(arg
= expr
->argument_list
; arg
; arg
= arg
->next
) {
594 hres
= compile_expression(ctx
, arg
->expr
, TRUE
);
600 instr
= push_instr(ctx
, op
);
602 return E_OUTOFMEMORY
;
604 instr_ptr(ctx
, instr
)->u
.arg
[0].uint
= arg_cnt
;
605 instr_ptr(ctx
, instr
)->u
.arg
[1].lng
= emit_ret
;
607 hres
= push_instr_uint(ctx
, OP_pop
, arg_cnt
+ extra_args
);
611 return !emit_ret
|| push_instr(ctx
, OP_push_ret
) ? S_OK
: E_OUTOFMEMORY
;
614 static HRESULT
compile_delete_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
)
618 switch(expr
->expression
->type
) {
620 binary_expression_t
*array_expr
= (binary_expression_t
*)expr
->expression
;
622 hres
= compile_expression(ctx
, array_expr
->expression1
, TRUE
);
626 hres
= compile_expression(ctx
, array_expr
->expression2
, TRUE
);
630 if(!push_instr(ctx
, OP_delete
))
631 return E_OUTOFMEMORY
;
635 member_expression_t
*member_expr
= (member_expression_t
*)expr
->expression
;
637 hres
= compile_expression(ctx
, member_expr
->expression
, TRUE
);
641 /* FIXME: Potential optimization */
642 hres
= push_instr_str(ctx
, OP_str
, member_expr
->identifier
);
646 if(!push_instr(ctx
, OP_delete
))
647 return E_OUTOFMEMORY
;
651 return push_instr_bstr(ctx
, OP_delete_ident
, ((identifier_expression_t
*)expr
->expression
)->identifier
);
653 const WCHAR fixmeW
[] = {'F','I','X','M','E',0};
655 WARN("invalid delete, unimplemented exception message\n");
657 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
661 return push_instr_uint_str(ctx
, OP_throw_type
, JS_E_INVALID_DELETE
, fixmeW
);
668 static HRESULT
compile_assign_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
670 BOOL use_throw_path
= FALSE
;
671 unsigned arg_cnt
= 0;
674 if(expr
->expression1
->type
== EXPR_CALL
) {
675 call_expression_t
*call_expr
= (call_expression_t
*)expr
->expression1
;
679 FIXME("op %d not supported on parametrized assign expressions\n", op
);
683 if(is_memberid_expr(call_expr
->expression
->type
) && call_expr
->argument_list
) {
684 hres
= compile_memberid_expression(ctx
, call_expr
->expression
, fdexNameEnsure
);
688 for(arg
= call_expr
->argument_list
; arg
; arg
= arg
->next
) {
689 hres
= compile_expression(ctx
, arg
->expr
, TRUE
);
695 use_throw_path
= TRUE
;
697 }else if(is_memberid_expr(expr
->expression1
->type
)) {
698 hres
= compile_memberid_expression(ctx
, expr
->expression1
, fdexNameEnsure
);
702 use_throw_path
= TRUE
;
706 /* Illegal assignment: evaluate and throw */
707 hres
= compile_expression(ctx
, expr
->expression1
, TRUE
);
711 hres
= compile_expression(ctx
, expr
->expression2
, TRUE
);
715 if(op
!= OP_LAST
&& !push_instr(ctx
, op
))
716 return E_OUTOFMEMORY
;
718 return push_instr_uint(ctx
, OP_throw_ref
, JS_E_ILLEGAL_ASSIGN
);
721 if(op
!= OP_LAST
&& !push_instr(ctx
, OP_refval
))
722 return E_OUTOFMEMORY
;
724 hres
= compile_expression(ctx
, expr
->expression2
, TRUE
);
728 if(op
!= OP_LAST
&& !push_instr(ctx
, op
))
729 return E_OUTOFMEMORY
;
732 return push_instr_uint(ctx
, OP_assign_call
, arg_cnt
);
734 if(!push_instr(ctx
, OP_assign
))
735 return E_OUTOFMEMORY
;
740 static HRESULT
compile_typeof_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
)
745 if(is_memberid_expr(expr
->expression
->type
)) {
746 if(expr
->expression
->type
== EXPR_IDENT
)
747 return push_instr_bstr(ctx
, OP_typeofident
, ((identifier_expression_t
*)expr
->expression
)->identifier
);
750 hres
= compile_memberid_expression(ctx
, expr
->expression
, 0);
753 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
758 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
761 static HRESULT
compile_literal(compiler_ctx_t
*ctx
, literal_t
*literal
)
763 switch(literal
->type
) {
765 return push_instr_int(ctx
, OP_bool
, literal
->u
.bval
);
767 return push_instr_double(ctx
, OP_double
, literal
->u
.dval
);
769 return push_instr(ctx
, OP_null
) ? S_OK
: E_OUTOFMEMORY
;
771 return push_instr_str(ctx
, OP_str
, literal
->u
.wstr
);
776 str
= compiler_alloc_string_len(ctx
, literal
->u
.regexp
.str
, literal
->u
.regexp
.str_len
);
778 return E_OUTOFMEMORY
;
780 instr
= push_instr(ctx
, OP_regexp
);
782 return E_OUTOFMEMORY
;
784 instr_ptr(ctx
, instr
)->u
.arg
[0].str
= str
;
785 instr_ptr(ctx
, instr
)->u
.arg
[1].uint
= literal
->u
.regexp
.flags
;
793 static HRESULT
literal_as_bstr(compiler_ctx_t
*ctx
, literal_t
*literal
, BSTR
*str
)
795 switch(literal
->type
) {
797 *str
= compiler_alloc_bstr(ctx
, literal
->u
.wstr
);
803 hres
= double_to_string(literal
->u
.dval
, &jsstr
);
807 *str
= compiler_alloc_bstr_len(ctx
, NULL
, jsstr_length(jsstr
));
809 jsstr_flush(jsstr
, *str
);
810 jsstr_release(jsstr
);
816 return *str
? S_OK
: E_OUTOFMEMORY
;
819 static HRESULT
compile_array_literal(compiler_ctx_t
*ctx
, array_literal_expression_t
*expr
)
821 unsigned i
, elem_cnt
= expr
->length
;
822 array_element_t
*iter
;
825 for(iter
= expr
->element_list
; iter
; iter
= iter
->next
) {
826 elem_cnt
+= iter
->elision
+1;
828 for(i
=0; i
< iter
->elision
; i
++) {
829 if(!push_instr(ctx
, OP_undefined
))
830 return E_OUTOFMEMORY
;
833 hres
= compile_expression(ctx
, iter
->expr
, TRUE
);
838 for(i
=0; i
< expr
->length
; i
++) {
839 if(!push_instr(ctx
, OP_undefined
))
840 return E_OUTOFMEMORY
;
843 return push_instr_uint(ctx
, OP_carray
, elem_cnt
);
846 static HRESULT
compile_object_literal(compiler_ctx_t
*ctx
, property_value_expression_t
*expr
)
853 if(!push_instr(ctx
, OP_new_obj
))
854 return E_OUTOFMEMORY
;
856 for(iter
= expr
->property_list
; iter
; iter
= iter
->next
) {
857 hres
= literal_as_bstr(ctx
, iter
->name
, &name
);
861 hres
= compile_expression(ctx
, iter
->value
, TRUE
);
865 instr
= push_instr(ctx
, OP_obj_prop
);
867 return E_OUTOFMEMORY
;
869 instr_ptr(ctx
, instr
)->u
.arg
->bstr
= name
;
875 static HRESULT
compile_function_expression(compiler_ctx_t
*ctx
, function_expression_t
*expr
, BOOL emit_ret
)
877 unsigned func_id
= ctx
->func
->func_cnt
++;
878 ctx
->func_tail
= ctx
->func_tail
? (ctx
->func_tail
->next
= expr
) : (ctx
->func_head
= expr
);
879 return emit_ret
? push_instr_uint(ctx
, OP_func
, func_id
) : S_OK
;
882 static HRESULT
compile_expression(compiler_ctx_t
*ctx
, expression_t
*expr
, BOOL emit_ret
)
888 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_add
);
891 hres
= compile_logical_expression(ctx
, (binary_expression_t
*)expr
, OP_cnd_z
);
894 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_array
);
897 hres
= compile_array_literal(ctx
, (array_literal_expression_t
*)expr
);
900 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_LAST
);
903 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_add
);
906 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_and
);
909 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
912 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_mul
);
915 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_div
);
918 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_mod
);
921 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_or
);
923 case EXPR_ASSIGNLSHIFT
:
924 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_lshift
);
926 case EXPR_ASSIGNRSHIFT
:
927 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift
);
929 case EXPR_ASSIGNRRSHIFT
:
930 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift2
);
933 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_xor
);
936 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_and
);
939 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_bneg
);
942 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_or
);
945 return compile_call_expression(ctx
, (call_expression_t
*)expr
, emit_ret
);
947 return compile_comma_expression(ctx
, (binary_expression_t
*)expr
, emit_ret
);
949 hres
= compile_conditional_expression(ctx
, (conditional_expression_t
*)expr
);
952 hres
= compile_delete_expression(ctx
, (unary_expression_t
*)expr
);
955 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_div
);
958 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_eq
);
961 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_eq2
);
964 return compile_function_expression(ctx
, (function_expression_t
*)expr
, emit_ret
);
966 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gt
);
969 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gteq
);
972 hres
= push_instr_bstr(ctx
, OP_ident
, ((identifier_expression_t
*)expr
)->identifier
);
975 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_in
);
977 case EXPR_INSTANCEOF
:
978 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_instanceof
);
981 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lt
);
984 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lteq
);
987 hres
= compile_literal(ctx
, ((literal_expression_t
*)expr
)->literal
);
990 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_neg
);
993 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lshift
);
996 hres
= compile_member_expression(ctx
, (member_expression_t
*)expr
);
999 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_minus
);
1002 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mod
);
1005 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mul
);
1008 hres
= compile_new_expression(ctx
, (call_expression_t
*)expr
);
1011 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_neq
);
1014 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_neq2
);
1017 hres
= compile_logical_expression(ctx
, (binary_expression_t
*)expr
, OP_cnd_nz
);
1020 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_tonum
);
1023 hres
= compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_postinc
, -1);
1026 hres
= compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_postinc
, 1);
1029 hres
= compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_preinc
, -1);
1032 hres
= compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_preinc
, 1);
1035 hres
= compile_object_literal(ctx
, (property_value_expression_t
*)expr
);
1038 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift
);
1041 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift2
);
1044 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
1047 return !emit_ret
|| push_instr(ctx
, OP_this
) ? S_OK
: E_OUTOFMEMORY
;
1049 hres
= compile_typeof_expression(ctx
, (unary_expression_t
*)expr
);
1052 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_void
);
1055 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_xor
);
1057 DEFAULT_UNREACHABLE
;
1063 return emit_ret
? S_OK
: push_instr_uint(ctx
, OP_pop
, 1);
1066 static inline BOOL
is_loop_statement(statement_type_t type
)
1068 return type
== STAT_FOR
|| type
== STAT_FORIN
|| type
== STAT_WHILE
;
1071 /* ECMA-262 3rd Edition 12.1 */
1072 static HRESULT
compile_block_statement(compiler_ctx_t
*ctx
, statement_t
*iter
)
1077 hres
= compile_statement(ctx
, NULL
, iter
);
1087 /* ECMA-262 3rd Edition 12.2 */
1088 static HRESULT
compile_variable_list(compiler_ctx_t
*ctx
, variable_declaration_t
*list
)
1090 variable_declaration_t
*iter
;
1093 assert(list
!= NULL
);
1096 ctx
->var_tail
->global_next
= list
;
1098 ctx
->var_head
= list
;
1100 for(iter
= list
; iter
; iter
= iter
->next
) {
1101 ctx
->func
->var_cnt
++;
1102 iter
->global_next
= iter
->next
;
1104 ctx
->var_tail
= iter
;
1109 hres
= compile_expression(ctx
, iter
->expr
, TRUE
);
1113 hres
= push_instr_bstr(ctx
, OP_var_set
, iter
->identifier
);
1121 /* ECMA-262 3rd Edition 12.2 */
1122 static HRESULT
compile_var_statement(compiler_ctx_t
*ctx
, var_statement_t
*stat
)
1124 return compile_variable_list(ctx
, stat
->variable_list
);
1127 /* ECMA-262 3rd Edition 12.4 */
1128 static HRESULT
compile_expression_statement(compiler_ctx_t
*ctx
, expression_statement_t
*stat
)
1132 hres
= compile_expression(ctx
, stat
->expr
, ctx
->from_eval
);
1136 return !ctx
->from_eval
|| push_instr(ctx
, OP_setret
) ? S_OK
: E_OUTOFMEMORY
;
1139 /* ECMA-262 3rd Edition 12.5 */
1140 static HRESULT
compile_if_statement(compiler_ctx_t
*ctx
, if_statement_t
*stat
)
1145 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1149 jmp_else
= push_instr(ctx
, OP_jmp_z
);
1151 return E_OUTOFMEMORY
;
1153 hres
= compile_statement(ctx
, NULL
, stat
->if_stat
);
1157 if(stat
->else_stat
) {
1160 jmp_end
= push_instr(ctx
, OP_jmp
);
1162 return E_OUTOFMEMORY
;
1164 set_arg_uint(ctx
, jmp_else
, ctx
->code_off
);
1166 hres
= compile_statement(ctx
, NULL
, stat
->else_stat
);
1170 set_arg_uint(ctx
, jmp_end
, ctx
->code_off
);
1172 set_arg_uint(ctx
, jmp_else
, ctx
->code_off
);
1178 /* ECMA-262 3rd Edition 12.6.2 */
1179 static HRESULT
compile_while_statement(compiler_ctx_t
*ctx
, while_statement_t
*stat
)
1181 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
};
1185 stat_ctx
.break_label
= alloc_label(ctx
);
1186 if(!stat_ctx
.break_label
)
1187 return E_OUTOFMEMORY
;
1189 stat_ctx
.continue_label
= alloc_label(ctx
);
1190 if(!stat_ctx
.continue_label
)
1191 return E_OUTOFMEMORY
;
1193 jmp_off
= ctx
->code_off
;
1195 if(!stat
->do_while
) {
1196 label_set_addr(ctx
, stat_ctx
.continue_label
);
1197 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1201 hres
= push_instr_uint(ctx
, OP_jmp_z
, stat_ctx
.break_label
);
1206 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1210 if(stat
->do_while
) {
1211 label_set_addr(ctx
, stat_ctx
.continue_label
);
1212 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1216 hres
= push_instr_uint(ctx
, OP_jmp_z
, stat_ctx
.break_label
);
1221 hres
= push_instr_uint(ctx
, OP_jmp
, jmp_off
);
1225 label_set_addr(ctx
, stat_ctx
.break_label
);
1229 /* ECMA-262 3rd Edition 12.6.3 */
1230 static HRESULT
compile_for_statement(compiler_ctx_t
*ctx
, for_statement_t
*stat
)
1232 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
};
1236 if(stat
->variable_list
) {
1237 hres
= compile_variable_list(ctx
, stat
->variable_list
);
1240 }else if(stat
->begin_expr
) {
1241 hres
= compile_expression(ctx
, stat
->begin_expr
, FALSE
);
1246 stat_ctx
.break_label
= alloc_label(ctx
);
1247 if(!stat_ctx
.break_label
)
1248 return E_OUTOFMEMORY
;
1250 stat_ctx
.continue_label
= alloc_label(ctx
);
1251 if(!stat_ctx
.continue_label
)
1252 return E_OUTOFMEMORY
;
1254 expr_off
= ctx
->code_off
;
1257 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1261 hres
= push_instr_uint(ctx
, OP_jmp_z
, stat_ctx
.break_label
);
1266 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1270 label_set_addr(ctx
, stat_ctx
.continue_label
);
1272 if(stat
->end_expr
) {
1273 hres
= compile_expression(ctx
, stat
->end_expr
, FALSE
);
1278 hres
= push_instr_uint(ctx
, OP_jmp
, expr_off
);
1282 label_set_addr(ctx
, stat_ctx
.break_label
);
1286 /* ECMA-262 3rd Edition 12.6.4 */
1287 static HRESULT
compile_forin_statement(compiler_ctx_t
*ctx
, forin_statement_t
*stat
)
1289 statement_ctx_t stat_ctx
= {4, FALSE
, FALSE
};
1292 if(stat
->variable
) {
1293 hres
= compile_variable_list(ctx
, stat
->variable
);
1298 stat_ctx
.break_label
= alloc_label(ctx
);
1299 if(!stat_ctx
.break_label
)
1300 return E_OUTOFMEMORY
;
1302 stat_ctx
.continue_label
= alloc_label(ctx
);
1303 if(!stat_ctx
.continue_label
)
1304 return E_OUTOFMEMORY
;
1306 hres
= compile_expression(ctx
, stat
->in_expr
, TRUE
);
1310 if(stat
->variable
) {
1311 hres
= push_instr_bstr_uint(ctx
, OP_identid
, stat
->variable
->identifier
, fdexNameEnsure
);
1314 }else if(is_memberid_expr(stat
->expr
->type
)) {
1315 hres
= compile_memberid_expression(ctx
, stat
->expr
, fdexNameEnsure
);
1319 hres
= push_instr_uint(ctx
, OP_throw_ref
, JS_E_ILLEGAL_ASSIGN
);
1323 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1327 hres
= push_instr_int(ctx
, OP_int
, DISPID_STARTENUM
);
1331 label_set_addr(ctx
, stat_ctx
.continue_label
);
1332 hres
= push_instr_uint(ctx
, OP_forin
, stat_ctx
.break_label
);
1334 return E_OUTOFMEMORY
;
1336 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1340 hres
= push_instr_uint(ctx
, OP_jmp
, stat_ctx
.continue_label
);
1344 label_set_addr(ctx
, stat_ctx
.break_label
);
1348 static HRESULT
pop_to_stat(compiler_ctx_t
*ctx
, BOOL var_stack
, BOOL scope_stack
, statement_ctx_t
*stat_ctx
)
1350 unsigned stack_pop
= 0;
1351 statement_ctx_t
*iter
;
1353 for(iter
= ctx
->stat_ctx
; iter
!= stat_ctx
; iter
= iter
->next
) {
1355 if(iter
->using_scope
&& !push_instr(ctx
, OP_pop_scope
))
1356 return E_OUTOFMEMORY
;
1357 if(iter
->using_except
&& !push_instr(ctx
, OP_pop_except
))
1358 return E_OUTOFMEMORY
;
1360 stack_pop
+= iter
->stack_use
;
1363 if(var_stack
&& stack_pop
) {
1366 hres
= push_instr_uint(ctx
, OP_pop
, stack_pop
);
1374 /* ECMA-262 3rd Edition 12.7 */
1375 static HRESULT
compile_continue_statement(compiler_ctx_t
*ctx
, branch_statement_t
*stat
)
1377 statement_ctx_t
*pop_ctx
;
1380 if(stat
->identifier
) {
1381 statement_t
*label_stat
;
1382 statement_ctx_t
*iter
;
1386 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
1387 if(iter
->continue_label
)
1389 if(iter
->labelled_stat
&& !strcmpW(iter
->labelled_stat
->identifier
, stat
->identifier
))
1394 WARN("Label not found\n");
1395 return JS_E_LABEL_NOT_FOUND
;
1398 /* Labelled continue are allowed only on loops */
1399 for(label_stat
= iter
->labelled_stat
->statement
;
1400 label_stat
->type
== STAT_LABEL
;
1401 label_stat
= ((labelled_statement_t
*)label_stat
)->statement
);
1402 if(!is_loop_statement(label_stat
->type
)) {
1403 WARN("Label is not a loop\n");
1404 return JS_E_INVALID_CONTINUE
;
1407 assert(pop_ctx
!= NULL
);
1409 for(pop_ctx
= ctx
->stat_ctx
; pop_ctx
; pop_ctx
= pop_ctx
->next
) {
1410 if(pop_ctx
->continue_label
)
1415 WARN("continue outside loop\n");
1416 return JS_E_INVALID_CONTINUE
;
1420 hres
= pop_to_stat(ctx
, TRUE
, TRUE
, pop_ctx
);
1424 return push_instr_uint(ctx
, OP_jmp
, pop_ctx
->continue_label
);
1427 /* ECMA-262 3rd Edition 12.8 */
1428 static HRESULT
compile_break_statement(compiler_ctx_t
*ctx
, branch_statement_t
*stat
)
1430 statement_ctx_t
*pop_ctx
;
1433 if(stat
->identifier
) {
1434 for(pop_ctx
= ctx
->stat_ctx
; pop_ctx
; pop_ctx
= pop_ctx
->next
) {
1435 if(pop_ctx
->labelled_stat
&& !strcmpW(pop_ctx
->labelled_stat
->identifier
, stat
->identifier
)) {
1436 assert(pop_ctx
->break_label
);
1442 WARN("Label not found\n");
1443 return JS_E_LABEL_NOT_FOUND
;
1446 for(pop_ctx
= ctx
->stat_ctx
; pop_ctx
; pop_ctx
= pop_ctx
->next
) {
1447 if(pop_ctx
->break_label
&& !pop_ctx
->labelled_stat
)
1452 WARN("Break outside loop\n");
1453 return JS_E_INVALID_BREAK
;
1457 hres
= pop_to_stat(ctx
, TRUE
, TRUE
, pop_ctx
->next
);
1461 return push_instr_uint(ctx
, OP_jmp
, pop_ctx
->break_label
);
1464 /* ECMA-262 3rd Edition 12.9 */
1465 static HRESULT
compile_return_statement(compiler_ctx_t
*ctx
, expression_statement_t
*stat
)
1469 if(ctx
->from_eval
) {
1470 WARN("misplaced return statement\n");
1471 return JS_E_MISPLACED_RETURN
;
1474 hres
= pop_to_stat(ctx
, TRUE
, FALSE
, NULL
);
1479 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1482 if(!push_instr(ctx
, OP_setret
))
1483 return E_OUTOFMEMORY
;
1486 hres
= pop_to_stat(ctx
, FALSE
, TRUE
, NULL
);
1490 return push_instr_uint(ctx
, OP_ret
, !stat
->expr
);
1493 /* ECMA-262 3rd Edition 12.10 */
1494 static HRESULT
compile_with_statement(compiler_ctx_t
*ctx
, with_statement_t
*stat
)
1496 statement_ctx_t stat_ctx
= {0, TRUE
, FALSE
};
1499 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1503 if(!push_instr(ctx
, OP_push_scope
))
1504 return E_OUTOFMEMORY
;
1506 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1510 if(!push_instr(ctx
, OP_pop_scope
))
1511 return E_OUTOFMEMORY
;
1516 /* ECMA-262 3rd Edition 12.10 */
1517 static HRESULT
compile_labelled_statement(compiler_ctx_t
*ctx
, labelled_statement_t
*stat
)
1519 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
, 0, 0, stat
}, *iter
;
1522 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
1523 if(iter
->labelled_stat
&& !strcmpW(iter
->labelled_stat
->identifier
, stat
->identifier
)) {
1524 WARN("Label %s redefined\n", debugstr_w(stat
->identifier
));
1525 return JS_E_LABEL_REDEFINED
;
1529 /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1530 stat_ctx
.break_label
= alloc_label(ctx
);
1531 if(!stat_ctx
.break_label
)
1532 return E_OUTOFMEMORY
;
1534 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1538 label_set_addr(ctx
, stat_ctx
.break_label
);
1542 /* ECMA-262 3rd Edition 12.13 */
1543 static HRESULT
compile_switch_statement(compiler_ctx_t
*ctx
, switch_statement_t
*stat
)
1545 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
};
1546 unsigned case_cnt
= 0, *case_jmps
, i
, default_jmp
;
1547 BOOL have_default
= FALSE
;
1548 statement_t
*stat_iter
;
1549 case_clausule_t
*iter
;
1552 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1556 stat_ctx
.break_label
= alloc_label(ctx
);
1557 if(!stat_ctx
.break_label
)
1558 return E_OUTOFMEMORY
;
1560 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1565 case_jmps
= heap_alloc(case_cnt
* sizeof(*case_jmps
));
1567 return E_OUTOFMEMORY
;
1570 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1572 have_default
= TRUE
;
1576 hres
= compile_expression(ctx
, iter
->expr
, TRUE
);
1580 case_jmps
[i
] = push_instr(ctx
, OP_case
);
1582 hres
= E_OUTOFMEMORY
;
1588 if(SUCCEEDED(hres
)) {
1589 hres
= push_instr_uint(ctx
, OP_pop
, 1);
1590 if(SUCCEEDED(hres
)) {
1591 default_jmp
= push_instr(ctx
, OP_jmp
);
1593 hres
= E_OUTOFMEMORY
;
1598 heap_free(case_jmps
);
1603 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1604 while(iter
->next
&& iter
->next
->stat
== iter
->stat
) {
1605 set_arg_uint(ctx
, iter
->expr
? case_jmps
[i
++] : default_jmp
, ctx
->code_off
);
1609 set_arg_uint(ctx
, iter
->expr
? case_jmps
[i
++] : default_jmp
, ctx
->code_off
);
1611 for(stat_iter
= iter
->stat
; stat_iter
&& (!iter
->next
|| iter
->next
->stat
!= stat_iter
);
1612 stat_iter
= stat_iter
->next
) {
1613 hres
= compile_statement(ctx
, &stat_ctx
, stat_iter
);
1621 heap_free(case_jmps
);
1624 assert(i
== case_cnt
);
1627 hres
= push_instr_uint(ctx
, OP_jmp
, stat_ctx
.break_label
);
1630 set_arg_uint(ctx
, default_jmp
, ctx
->code_off
);
1633 label_set_addr(ctx
, stat_ctx
.break_label
);
1637 /* ECMA-262 3rd Edition 12.13 */
1638 static HRESULT
compile_throw_statement(compiler_ctx_t
*ctx
, expression_statement_t
*stat
)
1642 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1646 return push_instr(ctx
, OP_throw
) ? S_OK
: E_OUTOFMEMORY
;
1649 /* ECMA-262 3rd Edition 12.14 */
1650 static HRESULT
compile_try_statement(compiler_ctx_t
*ctx
, try_statement_t
*stat
)
1652 statement_ctx_t try_ctx
= {0, FALSE
, TRUE
}, catch_ctx
= {0, TRUE
, FALSE
};
1653 statement_ctx_t finally_ctx
= {2, FALSE
, FALSE
};
1654 unsigned push_except
;
1658 push_except
= push_instr(ctx
, OP_push_except
);
1660 return E_OUTOFMEMORY
;
1662 if(stat
->catch_block
) {
1663 ident
= compiler_alloc_bstr(ctx
, stat
->catch_block
->identifier
);
1665 return E_OUTOFMEMORY
;
1670 instr_ptr(ctx
, push_except
)->u
.arg
[1].bstr
= ident
;
1672 if(!stat
->catch_block
)
1673 try_ctx
.stack_use
= 2;
1675 hres
= compile_statement(ctx
, &try_ctx
, stat
->try_statement
);
1679 if(!push_instr(ctx
, OP_pop_except
))
1680 return E_OUTOFMEMORY
;
1682 if(stat
->catch_block
) {
1683 unsigned jmp_finally
;
1685 jmp_finally
= push_instr(ctx
, OP_jmp
);
1687 return E_OUTOFMEMORY
;
1689 instr_ptr(ctx
, push_except
)->u
.arg
[0].uint
= ctx
->code_off
;
1691 hres
= compile_statement(ctx
, &catch_ctx
, stat
->catch_block
->statement
);
1695 if(!push_instr(ctx
, OP_pop_scope
))
1696 return E_OUTOFMEMORY
;
1698 set_arg_uint(ctx
, jmp_finally
, ctx
->code_off
);
1700 set_arg_uint(ctx
, push_except
, ctx
->code_off
);
1703 if(stat
->finally_statement
) {
1704 hres
= compile_statement(ctx
, stat
->catch_block
? NULL
: &finally_ctx
, stat
->finally_statement
);
1708 if(!stat
->catch_block
&& !push_instr(ctx
, OP_end_finally
))
1709 return E_OUTOFMEMORY
;
1715 static HRESULT
compile_statement(compiler_ctx_t
*ctx
, statement_ctx_t
*stat_ctx
, statement_t
*stat
)
1720 stat_ctx
->next
= ctx
->stat_ctx
;
1721 ctx
->stat_ctx
= stat_ctx
;
1724 switch(stat
->type
) {
1726 hres
= compile_block_statement(ctx
, ((block_statement_t
*)stat
)->stat_list
);
1729 hres
= compile_break_statement(ctx
, (branch_statement_t
*)stat
);
1732 hres
= compile_continue_statement(ctx
, (branch_statement_t
*)stat
);
1739 hres
= compile_expression_statement(ctx
, (expression_statement_t
*)stat
);
1742 hres
= compile_for_statement(ctx
, (for_statement_t
*)stat
);
1745 hres
= compile_forin_statement(ctx
, (forin_statement_t
*)stat
);
1748 hres
= compile_if_statement(ctx
, (if_statement_t
*)stat
);
1751 hres
= compile_labelled_statement(ctx
, (labelled_statement_t
*)stat
);
1754 hres
= compile_return_statement(ctx
, (expression_statement_t
*)stat
);
1757 hres
= compile_switch_statement(ctx
, (switch_statement_t
*)stat
);
1760 hres
= compile_throw_statement(ctx
, (expression_statement_t
*)stat
);
1763 hres
= compile_try_statement(ctx
, (try_statement_t
*)stat
);
1766 hres
= compile_var_statement(ctx
, (var_statement_t
*)stat
);
1769 hres
= compile_while_statement(ctx
, (while_statement_t
*)stat
);
1772 hres
= compile_with_statement(ctx
, (with_statement_t
*)stat
);
1774 DEFAULT_UNREACHABLE
;
1778 assert(ctx
->stat_ctx
== stat_ctx
);
1779 ctx
->stat_ctx
= stat_ctx
->next
;
1785 static void resolve_labels(compiler_ctx_t
*ctx
, unsigned off
)
1789 for(instr
= ctx
->code
->instrs
+off
; instr
< ctx
->code
->instrs
+ctx
->code_off
; instr
++) {
1790 if(instr_info
[instr
->op
].arg1_type
== ARG_ADDR
&& (instr
->u
.arg
->uint
& LABEL_FLAG
)) {
1791 assert((instr
->u
.arg
->uint
& ~LABEL_FLAG
) < ctx
->labels_cnt
);
1792 instr
->u
.arg
->uint
= ctx
->labels
[instr
->u
.arg
->uint
& ~LABEL_FLAG
];
1794 assert(instr_info
[instr
->op
].arg2_type
!= ARG_ADDR
);
1797 ctx
->labels_cnt
= 0;
1800 void release_bytecode(bytecode_t
*code
)
1807 for(i
=0; i
< code
->bstr_cnt
; i
++)
1808 SysFreeString(code
->bstr_pool
[i
]);
1809 for(i
=0; i
< code
->str_cnt
; i
++)
1810 jsstr_release(code
->str_pool
[i
]);
1812 heap_free(code
->source
);
1813 heap_pool_free(&code
->heap
);
1814 heap_free(code
->bstr_pool
);
1815 heap_free(code
->str_pool
);
1816 heap_free(code
->instrs
);
1820 static HRESULT
init_code(compiler_ctx_t
*compiler
, const WCHAR
*source
)
1822 compiler
->code
= heap_alloc_zero(sizeof(bytecode_t
));
1824 return E_OUTOFMEMORY
;
1826 compiler
->code
->ref
= 1;
1827 heap_pool_init(&compiler
->code
->heap
);
1829 compiler
->code
->source
= heap_strdupW(source
);
1830 if(!compiler
->code
->source
) {
1831 release_bytecode(compiler
->code
);
1832 return E_OUTOFMEMORY
;
1835 compiler
->code
->instrs
= heap_alloc(64 * sizeof(instr_t
));
1836 if(!compiler
->code
->instrs
) {
1837 release_bytecode(compiler
->code
);
1838 return E_OUTOFMEMORY
;
1841 compiler
->code_size
= 64;
1842 compiler
->code_off
= 1;
1846 static HRESULT
compile_function(compiler_ctx_t
*ctx
, source_elements_t
*source
, function_expression_t
*func_expr
,
1847 BOOL from_eval
, function_code_t
*func
)
1849 variable_declaration_t
*var_iter
;
1850 function_expression_t
*iter
;
1856 ctx
->var_head
= ctx
->var_tail
= NULL
;
1857 ctx
->func_head
= ctx
->func_tail
= NULL
;
1858 ctx
->from_eval
= from_eval
;
1860 off
= ctx
->code_off
;
1862 hres
= compile_block_statement(ctx
, source
->statement
);
1866 resolve_labels(ctx
, off
);
1868 hres
= push_instr_uint(ctx
, OP_ret
, !from_eval
);
1872 if(TRACE_ON(jscript_disas
))
1873 dump_code(ctx
, off
);
1875 func
->instr_off
= off
;
1878 if(func_expr
->identifier
) {
1879 func
->name
= compiler_alloc_bstr(ctx
, func_expr
->identifier
);
1881 return E_OUTOFMEMORY
;
1884 if(func_expr
->event_target
) {
1885 func
->event_target
= compiler_alloc_bstr(ctx
, func_expr
->event_target
);
1886 if(!func
->event_target
)
1887 return E_OUTOFMEMORY
;
1892 parameter_t
*param_iter
;
1894 func
->source
= func_expr
->src_str
;
1895 func
->source_len
= func_expr
->src_len
;
1897 for(param_iter
= func_expr
->parameter_list
; param_iter
; param_iter
= param_iter
->next
)
1900 func
->params
= compiler_alloc(ctx
->code
, func
->param_cnt
* sizeof(*func
->params
));
1902 return E_OUTOFMEMORY
;
1904 for(param_iter
= func_expr
->parameter_list
, i
=0; param_iter
; param_iter
= param_iter
->next
, i
++) {
1905 func
->params
[i
] = compiler_alloc_bstr(ctx
, param_iter
->identifier
);
1906 if(!func
->params
[i
])
1907 return E_OUTOFMEMORY
;
1911 func
->variables
= compiler_alloc(ctx
->code
, func
->var_cnt
* sizeof(*func
->variables
));
1912 if(!func
->variables
)
1913 return E_OUTOFMEMORY
;
1915 for(var_iter
= ctx
->var_head
, i
=0; var_iter
; var_iter
= var_iter
->global_next
, i
++) {
1916 func
->variables
[i
] = compiler_alloc_bstr(ctx
, var_iter
->identifier
);
1917 if(!func
->variables
[i
])
1918 return E_OUTOFMEMORY
;
1921 assert(i
== func
->var_cnt
);
1923 func
->funcs
= compiler_alloc(ctx
->code
, func
->func_cnt
* sizeof(*func
->funcs
));
1925 return E_OUTOFMEMORY
;
1926 memset(func
->funcs
, 0, func
->func_cnt
* sizeof(*func
->funcs
));
1928 for(iter
= ctx
->func_head
, i
=0; iter
; iter
= iter
->next
, i
++) {
1929 hres
= compile_function(ctx
, iter
->source_elements
, iter
, FALSE
, func
->funcs
+i
);
1934 assert(i
== func
->func_cnt
);
1939 static HRESULT
parse_arguments(compiler_ctx_t
*ctx
, const WCHAR
*args
, BSTR
*arg_array
, unsigned *args_size
)
1941 const WCHAR
*ptr
= args
, *ptr2
;
1942 unsigned arg_cnt
= 0;
1944 while(isspaceW(*ptr
))
1953 if(!isalphaW(*ptr
) && *ptr
!= '_') {
1954 FIXME("expected alpha or '_': %s\n", debugstr_w(ptr
));
1959 while(isalnumW(*ptr
) || *ptr
== '_')
1962 if(*ptr
&& *ptr
!= ',' && !isspaceW(*ptr
)) {
1963 FIXME("unexpected har %s\n", debugstr_w(ptr
));
1968 arg_array
[arg_cnt
] = compiler_alloc_bstr_len(ctx
, ptr2
, ptr
-ptr2
);
1969 if(!arg_array
[arg_cnt
])
1970 return E_OUTOFMEMORY
;
1974 while(isspaceW(*ptr
))
1979 FIXME("expected ',': %s\n", debugstr_w(ptr
));
1984 while(isspaceW(*ptr
))
1989 *args_size
= arg_cnt
;
1993 static HRESULT
compile_arguments(compiler_ctx_t
*ctx
, const WCHAR
*args
)
1997 hres
= parse_arguments(ctx
, args
, NULL
, &ctx
->code
->global_code
.param_cnt
);
2001 ctx
->code
->global_code
.params
= compiler_alloc(ctx
->code
,
2002 ctx
->code
->global_code
.param_cnt
* sizeof(*ctx
->code
->global_code
.params
));
2003 if(!ctx
->code
->global_code
.params
)
2004 return E_OUTOFMEMORY
;
2006 return parse_arguments(ctx
, args
, ctx
->code
->global_code
.params
, NULL
);
2009 HRESULT
compile_script(script_ctx_t
*ctx
, const WCHAR
*code
, const WCHAR
*args
, const WCHAR
*delimiter
,
2010 BOOL from_eval
, BOOL use_decode
, bytecode_t
**ret
)
2012 compiler_ctx_t compiler
= {0};
2015 hres
= init_code(&compiler
, code
);
2020 hres
= compile_arguments(&compiler
, args
);
2026 hres
= decode_source(compiler
.code
->source
);
2028 WARN("Decoding failed\n");
2033 hres
= script_parse(ctx
, compiler
.code
->source
, delimiter
, from_eval
, &compiler
.parser
);
2035 release_bytecode(compiler
.code
);
2039 hres
= compile_function(&compiler
, compiler
.parser
->source
, NULL
, from_eval
, &compiler
.code
->global_code
);
2040 parser_release(compiler
.parser
);
2042 release_bytecode(compiler
.code
);
2046 *ret
= compiler
.code
;