2 * Copyright 2011 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
28 WINE_DECLARE_DEBUG_CHANNEL(jscript_disas
);
30 typedef struct _statement_ctx_t
{
36 unsigned continue_label
;
38 const labelled_statement_t
*labelled_stat
;
40 struct _statement_ctx_t
*next
;
54 statement_ctx_t
*stat_ctx
;
55 function_code_t
*func
;
57 variable_declaration_t
*var_head
;
58 variable_declaration_t
*var_tail
;
60 function_expression_t
*func_head
;
61 function_expression_t
*func_tail
;
66 instr_arg_type_t arg1_type
;
67 instr_arg_type_t arg2_type
;
69 #define X(n,a,b,c) {#n,b,c},
74 static void dump_instr_arg(instr_arg_type_t type
, instr_arg_t
*arg
)
78 TRACE_(jscript_disas
)("\t%s", debugstr_w(arg
->str
));
81 TRACE_(jscript_disas
)("\t%s", debugstr_wn(arg
->bstr
, SysStringLen(arg
->bstr
)));
84 TRACE_(jscript_disas
)("\t%d", arg
->uint
);
88 TRACE_(jscript_disas
)("\t%u", arg
->uint
);
98 static void dump_code(compiler_ctx_t
*ctx
, unsigned off
)
102 for(instr
= ctx
->code
->instrs
+off
; instr
< ctx
->code
->instrs
+ctx
->code_off
; instr
++) {
103 TRACE_(jscript_disas
)("%d:\t%s", (int)(instr
-ctx
->code
->instrs
), instr_info
[instr
->op
].op_str
);
104 if(instr_info
[instr
->op
].arg1_type
== ARG_DBL
) {
105 TRACE_(jscript_disas
)("\t%lf", instr
->u
.dbl
);
107 dump_instr_arg(instr_info
[instr
->op
].arg1_type
, instr
->u
.arg
);
108 dump_instr_arg(instr_info
[instr
->op
].arg2_type
, instr
->u
.arg
+1);
110 TRACE_(jscript_disas
)("\n");
114 static HRESULT
compile_expression(compiler_ctx_t
*,expression_t
*);
115 static HRESULT
compile_statement(compiler_ctx_t
*,statement_ctx_t
*,statement_t
*);
117 static inline void *compiler_alloc(bytecode_t
*code
, size_t size
)
119 return jsheap_alloc(&code
->heap
, size
);
122 static WCHAR
*compiler_alloc_string(bytecode_t
*code
, const WCHAR
*str
)
127 size
= (strlenW(str
)+1)*sizeof(WCHAR
);
128 ret
= compiler_alloc(code
, size
);
130 memcpy(ret
, str
, size
);
134 static BSTR
compiler_alloc_bstr(compiler_ctx_t
*ctx
, const WCHAR
*str
)
136 if(!ctx
->code
->bstr_pool_size
) {
137 ctx
->code
->bstr_pool
= heap_alloc(8 * sizeof(BSTR
));
138 if(!ctx
->code
->bstr_pool
)
140 ctx
->code
->bstr_pool_size
= 8;
141 }else if(ctx
->code
->bstr_pool_size
== ctx
->code
->bstr_cnt
) {
144 new_pool
= heap_realloc(ctx
->code
->bstr_pool
, ctx
->code
->bstr_pool_size
*2*sizeof(BSTR
));
148 ctx
->code
->bstr_pool
= new_pool
;
149 ctx
->code
->bstr_pool_size
*= 2;
152 ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
] = SysAllocString(str
);
153 if(!ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
])
156 return ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
++];
159 static unsigned push_instr(compiler_ctx_t
*ctx
, jsop_t op
)
161 assert(ctx
->code_size
>= ctx
->code_off
);
163 if(ctx
->code_size
== ctx
->code_off
) {
166 new_instrs
= heap_realloc(ctx
->code
->instrs
, ctx
->code_size
*2*sizeof(instr_t
));
170 ctx
->code
->instrs
= new_instrs
;
174 ctx
->code
->instrs
[ctx
->code_off
].op
= op
;
175 return ctx
->code_off
++;
178 static inline instr_t
*instr_ptr(compiler_ctx_t
*ctx
, unsigned off
)
180 assert(off
< ctx
->code_off
);
181 return ctx
->code
->instrs
+ off
;
184 static HRESULT
push_instr_int(compiler_ctx_t
*ctx
, jsop_t op
, LONG arg
)
188 instr
= push_instr(ctx
, op
);
190 return E_OUTOFMEMORY
;
192 instr_ptr(ctx
, instr
)->u
.arg
->lng
= arg
;
196 static HRESULT
push_instr_str(compiler_ctx_t
*ctx
, jsop_t op
, const WCHAR
*arg
)
201 str
= compiler_alloc_string(ctx
->code
, arg
);
203 return E_OUTOFMEMORY
;
205 instr
= push_instr(ctx
, op
);
207 return E_OUTOFMEMORY
;
209 instr_ptr(ctx
, instr
)->u
.arg
->str
= str
;
213 static HRESULT
push_instr_bstr(compiler_ctx_t
*ctx
, jsop_t op
, const WCHAR
*arg
)
218 str
= compiler_alloc_bstr(ctx
, arg
);
220 return E_OUTOFMEMORY
;
222 instr
= push_instr(ctx
, op
);
224 return E_OUTOFMEMORY
;
226 instr_ptr(ctx
, instr
)->u
.arg
->bstr
= str
;
230 static HRESULT
push_instr_bstr_uint(compiler_ctx_t
*ctx
, jsop_t op
, const WCHAR
*arg1
, unsigned arg2
)
235 str
= compiler_alloc_bstr(ctx
, arg1
);
237 return E_OUTOFMEMORY
;
239 instr
= push_instr(ctx
, op
);
241 return E_OUTOFMEMORY
;
243 instr_ptr(ctx
, instr
)->u
.arg
[0].bstr
= str
;
244 instr_ptr(ctx
, instr
)->u
.arg
[1].uint
= arg2
;
248 static HRESULT
push_instr_uint_str(compiler_ctx_t
*ctx
, jsop_t op
, unsigned arg1
, const WCHAR
*arg2
)
253 str
= compiler_alloc_string(ctx
->code
, arg2
);
255 return E_OUTOFMEMORY
;
257 instr
= push_instr(ctx
, op
);
259 return E_OUTOFMEMORY
;
261 instr_ptr(ctx
, instr
)->u
.arg
[0].uint
= arg1
;
262 instr_ptr(ctx
, instr
)->u
.arg
[1].str
= str
;
266 static HRESULT
push_instr_double(compiler_ctx_t
*ctx
, jsop_t op
, double arg
)
270 instr
= push_instr(ctx
, op
);
272 return E_OUTOFMEMORY
;
274 instr_ptr(ctx
, instr
)->u
.dbl
= arg
;
278 static inline void set_arg_uint(compiler_ctx_t
*ctx
, unsigned instr
, unsigned arg
)
280 instr_ptr(ctx
, instr
)->u
.arg
->uint
= arg
;
283 static HRESULT
push_instr_uint(compiler_ctx_t
*ctx
, jsop_t op
, unsigned arg
)
287 instr
= push_instr(ctx
, op
);
289 return E_OUTOFMEMORY
;
291 set_arg_uint(ctx
, instr
, arg
);
295 static HRESULT
compile_binary_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
299 hres
= compile_expression(ctx
, expr
->expression1
);
303 hres
= compile_expression(ctx
, expr
->expression2
);
307 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
310 static HRESULT
compile_unary_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
, jsop_t op
)
314 hres
= compile_expression(ctx
, expr
->expression
);
318 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
321 /* ECMA-262 3rd Edition 11.2.1 */
322 static HRESULT
compile_member_expression(compiler_ctx_t
*ctx
, member_expression_t
*expr
)
326 hres
= compile_expression(ctx
, expr
->expression
);
330 return push_instr_bstr(ctx
, OP_member
, expr
->identifier
);
333 #define LABEL_FLAG 0x80000000
335 static unsigned alloc_label(compiler_ctx_t
*ctx
)
337 if(!ctx
->labels_size
) {
338 ctx
->labels
= heap_alloc(8 * sizeof(*ctx
->labels
));
341 ctx
->labels_size
= 8;
342 }else if(ctx
->labels_size
== ctx
->labels_cnt
) {
343 unsigned *new_labels
;
345 new_labels
= heap_realloc(ctx
->labels
, 2*ctx
->labels_size
*sizeof(*ctx
->labels
));
349 ctx
->labels
= new_labels
;
350 ctx
->labels_size
*= 2;
353 return ctx
->labels_cnt
++ | LABEL_FLAG
;
356 static void label_set_addr(compiler_ctx_t
*ctx
, unsigned label
)
358 assert(label
& LABEL_FLAG
);
359 ctx
->labels
[label
& ~LABEL_FLAG
] = ctx
->code_off
;
362 static inline BOOL
is_memberid_expr(expression_type_t type
)
364 return type
== EXPR_IDENT
|| type
== EXPR_MEMBER
|| type
== EXPR_ARRAY
;
367 static HRESULT
compile_memberid_expression(compiler_ctx_t
*ctx
, expression_t
*expr
, unsigned flags
)
373 identifier_expression_t
*ident_expr
= (identifier_expression_t
*)expr
;
375 hres
= push_instr_bstr_uint(ctx
, OP_identid
, ident_expr
->identifier
, flags
);
379 binary_expression_t
*array_expr
= (binary_expression_t
*)expr
;
381 hres
= compile_expression(ctx
, array_expr
->expression1
);
385 hres
= compile_expression(ctx
, array_expr
->expression2
);
389 hres
= push_instr_uint(ctx
, OP_memberid
, flags
);
393 member_expression_t
*member_expr
= (member_expression_t
*)expr
;
395 hres
= compile_expression(ctx
, member_expr
->expression
);
399 /* FIXME: Potential optimization */
400 hres
= push_instr_str(ctx
, OP_str
, member_expr
->identifier
);
404 hres
= push_instr_uint(ctx
, OP_memberid
, flags
);
414 static HRESULT
compile_increment_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
, jsop_t op
, int n
)
418 if(!is_memberid_expr(expr
->expression
->type
)) {
419 hres
= compile_expression(ctx
, expr
->expression
);
423 return push_instr_uint(ctx
, OP_throw_ref
, JS_E_ILLEGAL_ASSIGN
);
426 hres
= compile_memberid_expression(ctx
, expr
->expression
, fdexNameEnsure
);
430 return push_instr_int(ctx
, op
, n
);
433 /* ECMA-262 3rd Edition 11.14 */
434 static HRESULT
compile_comma_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
)
438 hres
= compile_expression(ctx
, expr
->expression1
);
442 if(!push_instr(ctx
, OP_pop
))
443 return E_OUTOFMEMORY
;
445 return compile_expression(ctx
, expr
->expression2
);
448 /* ECMA-262 3rd Edition 11.11 */
449 static HRESULT
compile_logical_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
454 hres
= compile_expression(ctx
, expr
->expression1
);
458 instr
= push_instr(ctx
, op
);
460 return E_OUTOFMEMORY
;
462 hres
= compile_expression(ctx
, expr
->expression2
);
466 set_arg_uint(ctx
, instr
, ctx
->code_off
);
470 /* ECMA-262 3rd Edition 11.12 */
471 static HRESULT
compile_conditional_expression(compiler_ctx_t
*ctx
, conditional_expression_t
*expr
)
473 unsigned jmp_false
, jmp_end
;
476 hres
= compile_expression(ctx
, expr
->expression
);
480 jmp_false
= push_instr(ctx
, OP_cnd_z
);
482 return E_OUTOFMEMORY
;
484 hres
= compile_expression(ctx
, expr
->true_expression
);
488 jmp_end
= push_instr(ctx
, OP_jmp
);
490 return E_OUTOFMEMORY
;
492 set_arg_uint(ctx
, jmp_false
, ctx
->code_off
);
493 if(!push_instr(ctx
, OP_pop
))
494 return E_OUTOFMEMORY
;
496 hres
= compile_expression(ctx
, expr
->false_expression
);
500 set_arg_uint(ctx
, jmp_end
, ctx
->code_off
);
504 static HRESULT
compile_new_expression(compiler_ctx_t
*ctx
, call_expression_t
*expr
)
506 unsigned arg_cnt
= 0;
510 hres
= compile_expression(ctx
, expr
->expression
);
514 for(arg
= expr
->argument_list
; arg
; arg
= arg
->next
) {
515 hres
= compile_expression(ctx
, arg
->expr
);
521 return push_instr_uint(ctx
, OP_new
, arg_cnt
);
524 static HRESULT
compile_call_expression(compiler_ctx_t
*ctx
, call_expression_t
*expr
, BOOL
*no_ret
)
526 unsigned arg_cnt
= 0;
532 if(is_memberid_expr(expr
->expression
->type
)) {
534 hres
= compile_memberid_expression(ctx
, expr
->expression
, 0);
537 hres
= compile_expression(ctx
, expr
->expression
);
543 for(arg
= expr
->argument_list
; arg
; arg
= arg
->next
) {
544 hres
= compile_expression(ctx
, arg
->expr
);
550 instr
= push_instr(ctx
, op
);
552 return E_OUTOFMEMORY
;
554 instr_ptr(ctx
, instr
)->u
.arg
[0].uint
= arg_cnt
;
555 instr_ptr(ctx
, instr
)->u
.arg
[1].lng
= no_ret
== NULL
;
561 static HRESULT
compile_delete_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
)
565 switch(expr
->expression
->type
) {
567 binary_expression_t
*array_expr
= (binary_expression_t
*)expr
->expression
;
569 hres
= compile_expression(ctx
, array_expr
->expression1
);
573 hres
= compile_expression(ctx
, array_expr
->expression2
);
577 if(!push_instr(ctx
, OP_delete
))
578 return E_OUTOFMEMORY
;
582 member_expression_t
*member_expr
= (member_expression_t
*)expr
->expression
;
584 hres
= compile_expression(ctx
, member_expr
->expression
);
588 /* FIXME: Potential optimization */
589 hres
= push_instr_str(ctx
, OP_str
, member_expr
->identifier
);
593 if(!push_instr(ctx
, OP_delete
))
594 return E_OUTOFMEMORY
;
598 return push_instr_bstr(ctx
, OP_delete_ident
, ((identifier_expression_t
*)expr
->expression
)->identifier
);
600 const WCHAR fixmeW
[] = {'F','I','X','M','E',0};
602 WARN("invalid delete, unimplemented exception message\n");
604 hres
= compile_expression(ctx
, expr
->expression
);
608 return push_instr_uint_str(ctx
, OP_throw_type
, JS_E_INVALID_DELETE
, fixmeW
);
615 static HRESULT
compile_assign_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
617 BOOL use_throw_path
= FALSE
;
618 unsigned arg_cnt
= 0;
621 if(expr
->expression1
->type
== EXPR_CALL
) {
622 call_expression_t
*call_expr
= (call_expression_t
*)expr
->expression1
;
626 FIXME("op %d not supported on parametrized assign expressions\n", op
);
630 if(is_memberid_expr(call_expr
->expression
->type
) && call_expr
->argument_list
) {
631 hres
= compile_memberid_expression(ctx
, call_expr
->expression
, fdexNameEnsure
);
635 for(arg
= call_expr
->argument_list
; arg
; arg
= arg
->next
) {
636 hres
= compile_expression(ctx
, arg
->expr
);
642 use_throw_path
= TRUE
;
644 }else if(is_memberid_expr(expr
->expression1
->type
)) {
645 hres
= compile_memberid_expression(ctx
, expr
->expression1
, fdexNameEnsure
);
649 use_throw_path
= TRUE
;
653 /* Illegal assignment: evaluate and throw */
654 hres
= compile_expression(ctx
, expr
->expression1
);
658 hres
= compile_expression(ctx
, expr
->expression2
);
662 if(op
!= OP_LAST
&& !push_instr(ctx
, op
))
663 return E_OUTOFMEMORY
;
665 return push_instr_uint(ctx
, OP_throw_ref
, JS_E_ILLEGAL_ASSIGN
);
668 if(op
!= OP_LAST
&& !push_instr(ctx
, OP_refval
))
669 return E_OUTOFMEMORY
;
671 hres
= compile_expression(ctx
, expr
->expression2
);
675 if(op
!= OP_LAST
&& !push_instr(ctx
, op
))
676 return E_OUTOFMEMORY
;
679 return push_instr_uint(ctx
, OP_assign_call
, arg_cnt
);
681 if(!push_instr(ctx
, OP_assign
))
682 return E_OUTOFMEMORY
;
687 static HRESULT
compile_typeof_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
)
692 if(is_memberid_expr(expr
->expression
->type
)) {
693 if(expr
->expression
->type
== EXPR_IDENT
)
694 return push_instr_str(ctx
, OP_typeofident
, ((identifier_expression_t
*)expr
->expression
)->identifier
);
697 hres
= compile_memberid_expression(ctx
, expr
->expression
, 0);
700 hres
= compile_expression(ctx
, expr
->expression
);
705 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
708 static HRESULT
compile_literal(compiler_ctx_t
*ctx
, literal_t
*literal
)
710 switch(literal
->type
) {
712 return push_instr_int(ctx
, OP_bool
, literal
->u
.bval
);
714 return push_instr_double(ctx
, OP_double
, literal
->u
.dval
);
716 return push_instr(ctx
, OP_null
) ? S_OK
: E_OUTOFMEMORY
;
718 return push_instr_str(ctx
, OP_str
, literal
->u
.wstr
);
723 str
= compiler_alloc(ctx
->code
, (literal
->u
.regexp
.str_len
+1)*sizeof(WCHAR
));
725 return E_OUTOFMEMORY
;
726 memcpy(str
, literal
->u
.regexp
.str
, literal
->u
.regexp
.str_len
*sizeof(WCHAR
));
727 str
[literal
->u
.regexp
.str_len
] = 0;
729 instr
= push_instr(ctx
, OP_regexp
);
731 return E_OUTOFMEMORY
;
733 instr_ptr(ctx
, instr
)->u
.arg
[0].str
= str
;
734 instr_ptr(ctx
, instr
)->u
.arg
[1].uint
= literal
->u
.regexp
.flags
;
743 static HRESULT
literal_as_bstr(compiler_ctx_t
*ctx
, literal_t
*literal
, BSTR
*str
)
745 switch(literal
->type
) {
747 *str
= compiler_alloc_bstr(ctx
, literal
->u
.wstr
);
750 return double_to_bstr(literal
->u
.dval
, str
);
755 return *str
? S_OK
: E_OUTOFMEMORY
;
758 static HRESULT
compile_array_literal(compiler_ctx_t
*ctx
, array_literal_expression_t
*expr
)
760 unsigned i
, elem_cnt
= expr
->length
;
761 array_element_t
*iter
;
764 for(iter
= expr
->element_list
; iter
; iter
= iter
->next
) {
765 elem_cnt
+= iter
->elision
+1;
767 for(i
=0; i
< iter
->elision
; i
++) {
768 if(!push_instr(ctx
, OP_undefined
))
769 return E_OUTOFMEMORY
;
772 hres
= compile_expression(ctx
, iter
->expr
);
777 for(i
=0; i
< expr
->length
; i
++) {
778 if(!push_instr(ctx
, OP_undefined
))
779 return E_OUTOFMEMORY
;
782 return push_instr_uint(ctx
, OP_carray
, elem_cnt
);
785 static HRESULT
compile_object_literal(compiler_ctx_t
*ctx
, property_value_expression_t
*expr
)
792 if(!push_instr(ctx
, OP_new_obj
))
793 return E_OUTOFMEMORY
;
795 for(iter
= expr
->property_list
; iter
; iter
= iter
->next
) {
796 hres
= literal_as_bstr(ctx
, iter
->name
, &name
);
800 hres
= compile_expression(ctx
, iter
->value
);
804 instr
= push_instr(ctx
, OP_obj_prop
);
806 return E_OUTOFMEMORY
;
808 instr_ptr(ctx
, instr
)->u
.arg
->bstr
= name
;
814 static HRESULT
compile_function_expression(compiler_ctx_t
*ctx
, function_expression_t
*expr
)
816 ctx
->func_tail
= ctx
->func_tail
? (ctx
->func_tail
->next
= expr
) : (ctx
->func_head
= expr
);
818 /* FIXME: not exactly right */
819 if(expr
->identifier
) {
820 ctx
->func
->func_cnt
++;
821 return push_instr_bstr(ctx
, OP_ident
, expr
->identifier
);
824 return push_instr_uint(ctx
, OP_func
, ctx
->func
->func_cnt
++);
827 static HRESULT
compile_expression_noret(compiler_ctx_t
*ctx
, expression_t
*expr
, BOOL
*no_ret
)
831 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_add
);
833 return compile_logical_expression(ctx
, (binary_expression_t
*)expr
, OP_cnd_z
);
835 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_array
);
837 return compile_array_literal(ctx
, (array_literal_expression_t
*)expr
);
839 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_LAST
);
841 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_add
);
843 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_and
);
845 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
847 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_mul
);
849 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_div
);
851 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_mod
);
853 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_or
);
854 case EXPR_ASSIGNLSHIFT
:
855 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_lshift
);
856 case EXPR_ASSIGNRSHIFT
:
857 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift
);
858 case EXPR_ASSIGNRRSHIFT
:
859 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift2
);
861 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_xor
);
863 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_and
);
865 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_bneg
);
867 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_or
);
869 return compile_call_expression(ctx
, (call_expression_t
*)expr
, no_ret
);
871 return compile_comma_expression(ctx
, (binary_expression_t
*)expr
);
873 return compile_conditional_expression(ctx
, (conditional_expression_t
*)expr
);
875 return compile_delete_expression(ctx
, (unary_expression_t
*)expr
);
877 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_div
);
879 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_eq
);
881 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_eq2
);
883 return compile_function_expression(ctx
, (function_expression_t
*)expr
);
885 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gt
);
887 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gteq
);
889 return push_instr_bstr(ctx
, OP_ident
, ((identifier_expression_t
*)expr
)->identifier
);
891 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_in
);
892 case EXPR_INSTANCEOF
:
893 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_instanceof
);
895 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lt
);
897 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lteq
);
899 return compile_literal(ctx
, ((literal_expression_t
*)expr
)->literal
);
901 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_neg
);
903 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lshift
);
905 return compile_member_expression(ctx
, (member_expression_t
*)expr
);
907 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_minus
);
909 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mod
);
911 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mul
);
913 return compile_new_expression(ctx
, (call_expression_t
*)expr
);
915 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_neq
);
917 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_neq2
);
919 return compile_logical_expression(ctx
, (binary_expression_t
*)expr
, OP_cnd_nz
);
921 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_tonum
);
923 return compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_postinc
, -1);
925 return compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_postinc
, 1);
927 return compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_preinc
, -1);
929 return compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_preinc
, 1);
931 return compile_object_literal(ctx
, (property_value_expression_t
*)expr
);
933 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift
);
935 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift2
);
937 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
939 return push_instr(ctx
, OP_this
) ? S_OK
: E_OUTOFMEMORY
;
941 return compile_typeof_expression(ctx
, (unary_expression_t
*)expr
);
943 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_void
);
945 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_xor
);
953 static HRESULT
compile_expression(compiler_ctx_t
*ctx
, expression_t
*expr
)
955 return compile_expression_noret(ctx
, expr
, NULL
);
958 static inline BOOL
is_loop_statement(statement_type_t type
)
960 return type
== STAT_FOR
|| type
== STAT_FORIN
|| type
== STAT_WHILE
;
963 /* ECMA-262 3rd Edition 12.1 */
964 static HRESULT
compile_block_statement(compiler_ctx_t
*ctx
, statement_t
*iter
)
968 /* FIXME: do it only if needed */
970 return push_instr(ctx
, OP_undefined
) ? S_OK
: E_OUTOFMEMORY
;
973 hres
= compile_statement(ctx
, NULL
, iter
);
981 if(!push_instr(ctx
, OP_pop
))
982 return E_OUTOFMEMORY
;
988 /* ECMA-262 3rd Edition 12.2 */
989 static HRESULT
compile_variable_list(compiler_ctx_t
*ctx
, variable_declaration_t
*list
)
991 variable_declaration_t
*iter
;
994 assert(list
!= NULL
);
997 ctx
->var_tail
->global_next
= list
;
999 ctx
->var_head
= list
;
1001 for(iter
= list
; iter
; iter
= iter
->next
) {
1002 ctx
->func
->var_cnt
++;
1003 iter
->global_next
= iter
->next
;
1005 ctx
->var_tail
= iter
;
1010 hres
= compile_expression(ctx
, iter
->expr
);
1014 hres
= push_instr_bstr(ctx
, OP_var_set
, iter
->identifier
);
1022 /* ECMA-262 3rd Edition 12.2 */
1023 static HRESULT
compile_var_statement(compiler_ctx_t
*ctx
, var_statement_t
*stat
)
1027 hres
= compile_variable_list(ctx
, stat
->variable_list
);
1031 return push_instr(ctx
, OP_undefined
) ? S_OK
: E_OUTOFMEMORY
;
1034 /* ECMA-262 3rd Edition 12.4 */
1035 static HRESULT
compile_expression_statement(compiler_ctx_t
*ctx
, expression_statement_t
*stat
)
1037 BOOL no_ret
= FALSE
;
1040 hres
= compile_expression_noret(ctx
, stat
->expr
, &no_ret
);
1044 /* FIXME: that's a big potential optimization */
1045 if(no_ret
&& !push_instr(ctx
, OP_undefined
))
1046 return E_OUTOFMEMORY
;
1051 /* ECMA-262 3rd Edition 12.5 */
1052 static HRESULT
compile_if_statement(compiler_ctx_t
*ctx
, if_statement_t
*stat
)
1054 unsigned jmp_else
, jmp_end
;
1057 hres
= compile_expression(ctx
, stat
->expr
);
1061 jmp_else
= push_instr(ctx
, OP_jmp_z
);
1063 return E_OUTOFMEMORY
;
1065 hres
= compile_statement(ctx
, NULL
, stat
->if_stat
);
1069 jmp_end
= push_instr(ctx
, OP_jmp
);
1071 return E_OUTOFMEMORY
;
1073 set_arg_uint(ctx
, jmp_else
, ctx
->code_off
);
1075 if(stat
->else_stat
) {
1076 hres
= compile_statement(ctx
, NULL
, stat
->else_stat
);
1080 /* FIXME: We could sometimes avoid it */
1081 if(!push_instr(ctx
, OP_undefined
))
1082 return E_OUTOFMEMORY
;
1085 set_arg_uint(ctx
, jmp_end
, ctx
->code_off
);
1089 /* ECMA-262 3rd Edition 12.6.2 */
1090 static HRESULT
compile_while_statement(compiler_ctx_t
*ctx
, while_statement_t
*stat
)
1092 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
};
1096 stat_ctx
.break_label
= alloc_label(ctx
);
1097 if(!stat_ctx
.break_label
)
1098 return E_OUTOFMEMORY
;
1100 stat_ctx
.continue_label
= alloc_label(ctx
);
1101 if(!stat_ctx
.continue_label
)
1102 return E_OUTOFMEMORY
;
1104 if(!stat
->do_while
) {
1106 if(!push_instr(ctx
, OP_undefined
))
1107 return E_OUTOFMEMORY
;
1109 jmp_off
= ctx
->code_off
;
1110 label_set_addr(ctx
, stat_ctx
.continue_label
);
1111 hres
= compile_expression(ctx
, stat
->expr
);
1115 hres
= push_instr_uint(ctx
, OP_jmp_z
, stat_ctx
.break_label
);
1119 if(!push_instr(ctx
, OP_pop
))
1120 return E_OUTOFMEMORY
;
1122 jmp_off
= ctx
->code_off
;
1125 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1129 if(stat
->do_while
) {
1130 label_set_addr(ctx
, stat_ctx
.continue_label
);
1131 hres
= compile_expression(ctx
, stat
->expr
);
1135 hres
= push_instr_uint(ctx
, OP_jmp_z
, stat_ctx
.break_label
);
1139 if(!push_instr(ctx
, OP_pop
))
1140 return E_OUTOFMEMORY
;
1143 hres
= push_instr_uint(ctx
, OP_jmp
, jmp_off
);
1147 label_set_addr(ctx
, stat_ctx
.break_label
);
1151 /* ECMA-262 3rd Edition 12.6.3 */
1152 static HRESULT
compile_for_statement(compiler_ctx_t
*ctx
, for_statement_t
*stat
)
1154 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
};
1158 if(stat
->variable_list
) {
1159 hres
= compile_variable_list(ctx
, stat
->variable_list
);
1162 }else if(stat
->begin_expr
) {
1163 BOOL no_ret
= FALSE
;
1165 hres
= compile_expression_noret(ctx
, stat
->begin_expr
, &no_ret
);
1168 if(!no_ret
&& !push_instr(ctx
, OP_pop
))
1169 return E_OUTOFMEMORY
;
1172 stat_ctx
.break_label
= alloc_label(ctx
);
1173 if(!stat_ctx
.break_label
)
1174 return E_OUTOFMEMORY
;
1176 stat_ctx
.continue_label
= alloc_label(ctx
);
1177 if(!stat_ctx
.continue_label
)
1178 return E_OUTOFMEMORY
;
1181 if(!push_instr(ctx
, OP_undefined
))
1182 return E_OUTOFMEMORY
;
1184 expr_off
= ctx
->code_off
;
1187 hres
= compile_expression(ctx
, stat
->expr
);
1191 hres
= push_instr_uint(ctx
, OP_jmp_z
, stat_ctx
.break_label
);
1196 if(!push_instr(ctx
, OP_pop
))
1197 return E_OUTOFMEMORY
;
1199 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1203 label_set_addr(ctx
, stat_ctx
.continue_label
);
1205 if(stat
->end_expr
) {
1206 BOOL no_ret
= FALSE
;
1208 hres
= compile_expression_noret(ctx
, stat
->end_expr
, &no_ret
);
1212 if(!no_ret
&& !push_instr(ctx
, OP_pop
))
1213 return E_OUTOFMEMORY
;
1216 hres
= push_instr_uint(ctx
, OP_jmp
, expr_off
);
1220 label_set_addr(ctx
, stat_ctx
.break_label
);
1224 /* ECMA-262 3rd Edition 12.6.4 */
1225 static HRESULT
compile_forin_statement(compiler_ctx_t
*ctx
, forin_statement_t
*stat
)
1227 statement_ctx_t stat_ctx
= {4, FALSE
, FALSE
};
1230 if(stat
->variable
) {
1231 hres
= compile_variable_list(ctx
, stat
->variable
);
1236 stat_ctx
.break_label
= alloc_label(ctx
);
1237 if(!stat_ctx
.break_label
)
1238 return E_OUTOFMEMORY
;
1240 stat_ctx
.continue_label
= alloc_label(ctx
);
1241 if(!stat_ctx
.continue_label
)
1242 return E_OUTOFMEMORY
;
1244 hres
= compile_expression(ctx
, stat
->in_expr
);
1248 if(stat
->variable
) {
1249 hres
= push_instr_bstr_uint(ctx
, OP_identid
, stat
->variable
->identifier
, fdexNameEnsure
);
1252 }else if(is_memberid_expr(stat
->expr
->type
)) {
1253 hres
= compile_memberid_expression(ctx
, stat
->expr
, fdexNameEnsure
);
1257 hres
= push_instr_uint(ctx
, OP_throw_ref
, JS_E_ILLEGAL_ASSIGN
);
1261 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1265 hres
= push_instr_int(ctx
, OP_int
, DISPID_STARTENUM
);
1270 if(!push_instr(ctx
, OP_undefined
))
1271 return E_OUTOFMEMORY
;
1273 label_set_addr(ctx
, stat_ctx
.continue_label
);
1274 hres
= push_instr_uint(ctx
, OP_forin
, stat_ctx
.break_label
);
1276 return E_OUTOFMEMORY
;
1278 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1282 hres
= push_instr_uint(ctx
, OP_jmp
, stat_ctx
.continue_label
);
1286 label_set_addr(ctx
, stat_ctx
.break_label
);
1290 static HRESULT
pop_to_stat(compiler_ctx_t
*ctx
, statement_ctx_t
*stat_ctx
)
1292 unsigned stack_pop
= 0;
1293 statement_ctx_t
*iter
;
1295 for(iter
= ctx
->stat_ctx
; iter
!= stat_ctx
; iter
= iter
->next
) {
1296 if(iter
->using_scope
&& !push_instr(ctx
, OP_pop_scope
))
1297 return E_OUTOFMEMORY
;
1298 if(iter
->using_except
&& !push_instr(ctx
, OP_pop_except
))
1299 return E_OUTOFMEMORY
;
1300 stack_pop
+= iter
->stack_use
;
1303 /* FIXME: optimize */
1304 while(stack_pop
--) {
1305 if(!push_instr(ctx
, OP_pop
))
1306 return E_OUTOFMEMORY
;
1312 /* ECMA-262 3rd Edition 12.7 */
1313 static HRESULT
compile_continue_statement(compiler_ctx_t
*ctx
, branch_statement_t
*stat
)
1315 statement_ctx_t
*pop_ctx
;
1318 if(stat
->identifier
) {
1319 statement_t
*label_stat
;
1320 statement_ctx_t
*iter
;
1324 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
1325 if(iter
->continue_label
)
1327 if(iter
->labelled_stat
&& !strcmpW(iter
->labelled_stat
->identifier
, stat
->identifier
))
1332 WARN("Label not found\n");
1333 return JS_E_LABEL_NOT_FOUND
;
1336 /* Labelled continue are allowed only on loops */
1337 for(label_stat
= iter
->labelled_stat
->statement
;
1338 label_stat
->type
== STAT_LABEL
;
1339 label_stat
= ((labelled_statement_t
*)label_stat
)->statement
);
1340 if(!is_loop_statement(label_stat
->type
)) {
1341 WARN("Label is not a loop\n");
1342 return JS_E_INVALID_CONTINUE
;
1345 for(pop_ctx
= ctx
->stat_ctx
; pop_ctx
; pop_ctx
= pop_ctx
->next
) {
1346 if(pop_ctx
->continue_label
)
1351 WARN("continue outside loop\n");
1352 return JS_E_INVALID_CONTINUE
;
1356 hres
= pop_to_stat(ctx
, pop_ctx
);
1360 if(!push_instr(ctx
, OP_undefined
))
1361 return E_OUTOFMEMORY
;
1363 return push_instr_uint(ctx
, OP_jmp
, pop_ctx
->continue_label
);
1366 /* ECMA-262 3rd Edition 12.8 */
1367 static HRESULT
compile_break_statement(compiler_ctx_t
*ctx
, branch_statement_t
*stat
)
1369 statement_ctx_t
*pop_ctx
;
1372 if(stat
->identifier
) {
1373 for(pop_ctx
= ctx
->stat_ctx
; pop_ctx
; pop_ctx
= pop_ctx
->next
) {
1374 if(pop_ctx
->labelled_stat
&& !strcmpW(pop_ctx
->labelled_stat
->identifier
, stat
->identifier
)) {
1375 assert(pop_ctx
->break_label
);
1381 WARN("Label not found\n");
1382 return JS_E_LABEL_NOT_FOUND
;
1385 for(pop_ctx
= ctx
->stat_ctx
; pop_ctx
; pop_ctx
= pop_ctx
->next
) {
1386 if(pop_ctx
->break_label
&& !pop_ctx
->labelled_stat
)
1391 WARN("Break outside loop\n");
1392 return JS_E_INVALID_BREAK
;
1396 hres
= pop_to_stat(ctx
, pop_ctx
->next
);
1400 if(!push_instr(ctx
, OP_undefined
))
1401 return E_OUTOFMEMORY
;
1403 return push_instr_uint(ctx
, OP_jmp
, pop_ctx
->break_label
);
1406 /* ECMA-262 3rd Edition 12.9 */
1407 static HRESULT
compile_return_statement(compiler_ctx_t
*ctx
, expression_statement_t
*stat
)
1411 hres
= pop_to_stat(ctx
, NULL
);
1416 hres
= compile_expression(ctx
, stat
->expr
);
1421 return push_instr(ctx
, OP_ret
) ? S_OK
: E_OUTOFMEMORY
;
1424 /* ECMA-262 3rd Edition 12.10 */
1425 static HRESULT
compile_with_statement(compiler_ctx_t
*ctx
, with_statement_t
*stat
)
1427 statement_ctx_t stat_ctx
= {0, TRUE
, FALSE
};
1430 hres
= compile_expression(ctx
, stat
->expr
);
1434 if(!push_instr(ctx
, OP_push_scope
))
1435 return E_OUTOFMEMORY
;
1437 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1441 if(!push_instr(ctx
, OP_pop_scope
))
1442 return E_OUTOFMEMORY
;
1447 /* ECMA-262 3rd Edition 12.10 */
1448 static HRESULT
compile_labelled_statement(compiler_ctx_t
*ctx
, labelled_statement_t
*stat
)
1450 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
, 0, 0, stat
}, *iter
;
1453 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
1454 if(iter
->labelled_stat
&& !strcmpW(iter
->labelled_stat
->identifier
, stat
->identifier
)) {
1455 WARN("Label %s redefined\n", debugstr_w(stat
->identifier
));
1456 return JS_E_LABEL_REDEFINED
;
1460 /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1461 stat_ctx
.break_label
= alloc_label(ctx
);
1462 if(!stat_ctx
.break_label
)
1463 return E_OUTOFMEMORY
;
1465 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1469 label_set_addr(ctx
, stat_ctx
.break_label
);
1473 /* ECMA-262 3rd Edition 12.13 */
1474 static HRESULT
compile_switch_statement(compiler_ctx_t
*ctx
, switch_statement_t
*stat
)
1476 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
};
1477 unsigned case_cnt
= 0, *case_jmps
, i
, default_jmp
;
1478 BOOL have_default
= FALSE
;
1479 statement_t
*stat_iter
;
1480 case_clausule_t
*iter
;
1483 hres
= compile_expression(ctx
, stat
->expr
);
1487 stat_ctx
.break_label
= alloc_label(ctx
);
1488 if(!stat_ctx
.break_label
)
1489 return E_OUTOFMEMORY
;
1491 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1496 case_jmps
= heap_alloc(case_cnt
* sizeof(*case_jmps
));
1498 return E_OUTOFMEMORY
;
1501 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1503 have_default
= TRUE
;
1507 hres
= compile_expression(ctx
, iter
->expr
);
1511 case_jmps
[i
] = push_instr(ctx
, OP_case
);
1513 hres
= E_OUTOFMEMORY
;
1519 if(SUCCEEDED(hres
)) {
1520 if(push_instr(ctx
, OP_pop
)) {
1521 default_jmp
= push_instr(ctx
, OP_jmp
);
1523 hres
= E_OUTOFMEMORY
;
1525 hres
= E_OUTOFMEMORY
;
1530 heap_free(case_jmps
);
1535 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1536 while(iter
->next
&& iter
->next
->stat
== iter
->stat
) {
1537 set_arg_uint(ctx
, iter
->expr
? case_jmps
[i
++] : default_jmp
, ctx
->code_off
);
1541 set_arg_uint(ctx
, iter
->expr
? case_jmps
[i
++] : default_jmp
, ctx
->code_off
);
1543 for(stat_iter
= iter
->stat
; stat_iter
&& (!iter
->next
|| iter
->next
->stat
!= stat_iter
); stat_iter
= stat_iter
->next
) {
1544 hres
= compile_statement(ctx
, &stat_ctx
, stat_iter
);
1548 if(stat_iter
->next
&& !push_instr(ctx
, OP_pop
)) {
1549 hres
= E_OUTOFMEMORY
;
1557 heap_free(case_jmps
);
1560 assert(i
== case_cnt
);
1563 hres
= push_instr_uint(ctx
, OP_jmp
, stat_ctx
.break_label
);
1566 set_arg_uint(ctx
, default_jmp
, ctx
->code_off
);
1567 if(!push_instr(ctx
, OP_undefined
))
1568 return E_OUTOFMEMORY
;
1571 label_set_addr(ctx
, stat_ctx
.break_label
);
1575 /* ECMA-262 3rd Edition 12.13 */
1576 static HRESULT
compile_throw_statement(compiler_ctx_t
*ctx
, expression_statement_t
*stat
)
1580 hres
= compile_expression(ctx
, stat
->expr
);
1584 return push_instr(ctx
, OP_throw
) ? S_OK
: E_OUTOFMEMORY
;
1587 /* ECMA-262 3rd Edition 12.14 */
1588 static HRESULT
compile_try_statement(compiler_ctx_t
*ctx
, try_statement_t
*stat
)
1590 statement_ctx_t try_ctx
= {0, FALSE
, TRUE
}, catch_ctx
= {0, TRUE
, FALSE
};
1591 statement_ctx_t finally_ctx
= {2, FALSE
, FALSE
};
1592 unsigned push_except
;
1596 push_except
= push_instr(ctx
, OP_push_except
);
1598 return E_OUTOFMEMORY
;
1600 if(stat
->catch_block
) {
1601 ident
= compiler_alloc_bstr(ctx
, stat
->catch_block
->identifier
);
1603 return E_OUTOFMEMORY
;
1608 instr_ptr(ctx
, push_except
)->u
.arg
[1].bstr
= ident
;
1610 if(!stat
->catch_block
)
1611 try_ctx
.stack_use
= 2;
1613 hres
= compile_statement(ctx
, &try_ctx
, stat
->try_statement
);
1617 if(!push_instr(ctx
, OP_pop_except
))
1618 return E_OUTOFMEMORY
;
1620 if(stat
->catch_block
) {
1621 unsigned jmp_finally
;
1623 jmp_finally
= push_instr(ctx
, OP_jmp
);
1625 return E_OUTOFMEMORY
;
1627 instr_ptr(ctx
, push_except
)->u
.arg
[0].uint
= ctx
->code_off
;
1629 hres
= compile_statement(ctx
, &catch_ctx
, stat
->catch_block
->statement
);
1633 if(!push_instr(ctx
, OP_pop_scope
))
1634 return E_OUTOFMEMORY
;
1636 set_arg_uint(ctx
, jmp_finally
, ctx
->code_off
);
1638 set_arg_uint(ctx
, push_except
, ctx
->code_off
);
1641 if(stat
->finally_statement
) {
1643 if(!push_instr(ctx
, OP_pop
))
1644 return E_OUTOFMEMORY
;
1646 hres
= compile_statement(ctx
, stat
->catch_block
? NULL
: &finally_ctx
, stat
->finally_statement
);
1650 if(!stat
->catch_block
&& !push_instr(ctx
, OP_end_finally
))
1651 return E_OUTOFMEMORY
;
1657 static HRESULT
compile_statement(compiler_ctx_t
*ctx
, statement_ctx_t
*stat_ctx
, statement_t
*stat
)
1662 stat_ctx
->next
= ctx
->stat_ctx
;
1663 ctx
->stat_ctx
= stat_ctx
;
1666 switch(stat
->type
) {
1668 hres
= compile_block_statement(ctx
, ((block_statement_t
*)stat
)->stat_list
);
1671 hres
= compile_break_statement(ctx
, (branch_statement_t
*)stat
);
1674 hres
= compile_continue_statement(ctx
, (branch_statement_t
*)stat
);
1677 hres
= push_instr(ctx
, OP_undefined
) ? S_OK
: E_OUTOFMEMORY
; /* FIXME */
1680 hres
= compile_expression_statement(ctx
, (expression_statement_t
*)stat
);
1683 hres
= compile_for_statement(ctx
, (for_statement_t
*)stat
);
1686 hres
= compile_forin_statement(ctx
, (forin_statement_t
*)stat
);
1689 hres
= compile_if_statement(ctx
, (if_statement_t
*)stat
);
1692 hres
= compile_labelled_statement(ctx
, (labelled_statement_t
*)stat
);
1695 hres
= compile_return_statement(ctx
, (expression_statement_t
*)stat
);
1698 hres
= compile_switch_statement(ctx
, (switch_statement_t
*)stat
);
1701 hres
= compile_throw_statement(ctx
, (expression_statement_t
*)stat
);
1704 hres
= compile_try_statement(ctx
, (try_statement_t
*)stat
);
1707 hres
= compile_var_statement(ctx
, (var_statement_t
*)stat
);
1710 hres
= compile_while_statement(ctx
, (while_statement_t
*)stat
);
1713 hres
= compile_with_statement(ctx
, (with_statement_t
*)stat
);
1721 assert(ctx
->stat_ctx
== stat_ctx
);
1722 ctx
->stat_ctx
= stat_ctx
->next
;
1728 static void resolve_labels(compiler_ctx_t
*ctx
, unsigned off
)
1732 for(instr
= ctx
->code
->instrs
+off
; instr
< ctx
->code
->instrs
+ctx
->code_off
; instr
++) {
1733 if(instr_info
[instr
->op
].arg1_type
== ARG_ADDR
&& (instr
->u
.arg
->uint
& LABEL_FLAG
)) {
1734 assert((instr
->u
.arg
->uint
& ~LABEL_FLAG
) < ctx
->labels_cnt
);
1735 instr
->u
.arg
->uint
= ctx
->labels
[instr
->u
.arg
->uint
& ~LABEL_FLAG
];
1737 assert(instr_info
[instr
->op
].arg2_type
!= ARG_ADDR
);
1740 ctx
->labels_cnt
= 0;
1743 void release_bytecode(bytecode_t
*code
)
1750 for(i
=0; i
< code
->bstr_cnt
; i
++)
1751 SysFreeString(code
->bstr_pool
[i
]);
1753 heap_free(code
->source
);
1754 jsheap_free(&code
->heap
);
1755 heap_free(code
->bstr_pool
);
1756 heap_free(code
->instrs
);
1760 static HRESULT
init_code(compiler_ctx_t
*compiler
, const WCHAR
*source
)
1762 compiler
->code
= heap_alloc_zero(sizeof(bytecode_t
));
1764 return E_OUTOFMEMORY
;
1766 compiler
->code
->ref
= 1;
1767 jsheap_init(&compiler
->code
->heap
);
1769 compiler
->code
->source
= heap_strdupW(source
);
1770 if(!compiler
->code
->source
) {
1771 release_bytecode(compiler
->code
);
1772 return E_OUTOFMEMORY
;
1775 compiler
->code
->instrs
= heap_alloc(64 * sizeof(instr_t
));
1776 if(!compiler
->code
->instrs
) {
1777 release_bytecode(compiler
->code
);
1778 return E_OUTOFMEMORY
;
1781 compiler
->code_size
= 64;
1782 compiler
->code_off
= 1;
1786 static HRESULT
compile_function(compiler_ctx_t
*ctx
, source_elements_t
*source
, function_expression_t
*func_expr
,
1787 BOOL from_eval
, function_code_t
*func
)
1789 variable_declaration_t
*var_iter
;
1790 function_expression_t
*iter
;
1796 ctx
->var_head
= ctx
->var_tail
= NULL
;
1797 ctx
->func_head
= ctx
->func_tail
= NULL
;
1799 off
= ctx
->code_off
;
1801 hres
= compile_block_statement(ctx
, source
->statement
);
1805 resolve_labels(ctx
, off
);
1807 if(!from_eval
&& !push_instr(ctx
, OP_pop
))
1808 return E_OUTOFMEMORY
;
1809 if(!push_instr(ctx
, OP_ret
))
1810 return E_OUTOFMEMORY
;
1812 if(TRACE_ON(jscript_disas
))
1813 dump_code(ctx
, off
);
1815 func
->instr_off
= off
;
1817 if(func_expr
&& func_expr
->identifier
) {
1818 func
->name
= compiler_alloc_bstr(ctx
, func_expr
->identifier
);
1820 return E_OUTOFMEMORY
;
1824 parameter_t
*param_iter
;
1826 func
->source
= func_expr
->src_str
;
1827 func
->source_len
= func_expr
->src_len
;
1829 for(param_iter
= func_expr
->parameter_list
; param_iter
; param_iter
= param_iter
->next
)
1832 func
->params
= compiler_alloc(ctx
->code
, func
->param_cnt
* sizeof(*func
->params
));
1834 return E_OUTOFMEMORY
;
1836 for(param_iter
= func_expr
->parameter_list
, i
=0; param_iter
; param_iter
= param_iter
->next
, i
++) {
1837 func
->params
[i
] = compiler_alloc_bstr(ctx
, param_iter
->identifier
);
1838 if(!func
->params
[i
])
1839 return E_OUTOFMEMORY
;
1843 func
->variables
= compiler_alloc(ctx
->code
, func
->var_cnt
* sizeof(*func
->variables
));
1844 if(!func
->variables
)
1845 return E_OUTOFMEMORY
;
1847 for(var_iter
= ctx
->var_head
, i
=0; var_iter
; var_iter
= var_iter
->global_next
, i
++) {
1848 func
->variables
[i
] = compiler_alloc_bstr(ctx
, var_iter
->identifier
);
1849 if(!func
->variables
[i
])
1850 return E_OUTOFMEMORY
;
1853 assert(i
== func
->var_cnt
);
1855 func
->funcs
= compiler_alloc(ctx
->code
, func
->func_cnt
* sizeof(*func
->funcs
));
1857 return E_OUTOFMEMORY
;
1858 memset(func
->funcs
, 0, func
->func_cnt
* sizeof(*func
->funcs
));
1860 for(iter
= ctx
->func_head
, i
=0; iter
; iter
= iter
->next
, i
++) {
1861 hres
= compile_function(ctx
, iter
->source_elements
, iter
, FALSE
, func
->funcs
+i
);
1866 assert(i
== func
->func_cnt
);
1871 HRESULT
compile_script(script_ctx_t
*ctx
, const WCHAR
*code
, const WCHAR
*delimiter
, BOOL from_eval
, BOOL use_decode
,
1874 compiler_ctx_t compiler
= {0};
1877 hres
= init_code(&compiler
, code
);
1882 hres
= decode_source(compiler
.code
->source
);
1884 WARN("Decoding failed\n");
1889 hres
= script_parse(ctx
, compiler
.code
->source
, delimiter
, from_eval
, &compiler
.parser
);
1891 release_bytecode(compiler
.code
);
1895 hres
= compile_function(&compiler
, compiler
.parser
->source
, NULL
, from_eval
, &compiler
.code
->global_code
);
1896 parser_release(compiler
.parser
);
1898 release_bytecode(compiler
.code
);
1902 *ret
= compiler
.code
;