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
21 WINE_DECLARE_DEBUG_CHANNEL(jscript_disas
);
23 typedef struct _statement_ctx_t
{
29 unsigned continue_label
;
31 const labelled_statement_t
*labelled_stat
;
33 struct _statement_ctx_t
*next
;
49 statement_ctx_t
*stat_ctx
;
50 function_code_t
*func
;
52 variable_declaration_t
*var_head
;
53 variable_declaration_t
*var_tail
;
55 function_expression_t
*func_head
;
56 function_expression_t
*func_tail
;
61 instr_arg_type_t arg1_type
;
62 instr_arg_type_t arg2_type
;
64 #define X(n,a,b,c) {#n,b,c},
69 static void dump_instr_arg(instr_arg_type_t type
, instr_arg_t
*arg
)
73 TRACE_(jscript_disas
)("\t%s", debugstr_jsstr(arg
->str
));
76 TRACE_(jscript_disas
)("\t%s", debugstr_wn(arg
->bstr
, SysStringLen(arg
->bstr
)));
79 TRACE_(jscript_disas
)("\t%d", arg
->uint
);
83 TRACE_(jscript_disas
)("\t%u", arg
->uint
);
92 static void dump_code(compiler_ctx_t
*ctx
, unsigned off
)
96 for(instr
= ctx
->code
->instrs
+off
; instr
< ctx
->code
->instrs
+ctx
->code_off
; instr
++) {
97 TRACE_(jscript_disas
)("%d:\t%s", (int)(instr
-ctx
->code
->instrs
), instr_info
[instr
->op
].op_str
);
98 if(instr_info
[instr
->op
].arg1_type
== ARG_DBL
) {
99 TRACE_(jscript_disas
)("\t%lf", instr
->u
.dbl
);
101 dump_instr_arg(instr_info
[instr
->op
].arg1_type
, instr
->u
.arg
);
102 dump_instr_arg(instr_info
[instr
->op
].arg2_type
, instr
->u
.arg
+1);
104 TRACE_(jscript_disas
)("\n");
108 static HRESULT
compile_expression(compiler_ctx_t
*,expression_t
*,BOOL
);
109 static HRESULT
compile_statement(compiler_ctx_t
*,statement_ctx_t
*,statement_t
*);
111 static inline void *compiler_alloc(bytecode_t
*code
, size_t size
)
113 return heap_pool_alloc(&code
->heap
, size
);
116 static jsstr_t
*compiler_alloc_string_len(compiler_ctx_t
*ctx
, const WCHAR
*str
, unsigned len
)
120 if(!ctx
->code
->str_pool_size
) {
121 ctx
->code
->str_pool
= heap_alloc(8 * sizeof(jsstr_t
*));
122 if(!ctx
->code
->str_pool
)
124 ctx
->code
->str_pool_size
= 8;
125 }else if(ctx
->code
->str_pool_size
== ctx
->code
->str_cnt
) {
128 new_pool
= heap_realloc(ctx
->code
->str_pool
, ctx
->code
->str_pool_size
*2*sizeof(jsstr_t
*));
132 ctx
->code
->str_pool
= new_pool
;
133 ctx
->code
->str_pool_size
*= 2;
136 new_str
= jsstr_alloc_len(str
, len
);
140 ctx
->code
->str_pool
[ctx
->code
->str_cnt
++] = new_str
;
144 static jsstr_t
*compiler_alloc_string(compiler_ctx_t
*ctx
, const WCHAR
*str
)
146 return compiler_alloc_string_len(ctx
, str
, strlenW(str
));
149 static BOOL
ensure_bstr_slot(compiler_ctx_t
*ctx
)
151 if(!ctx
->code
->bstr_pool_size
) {
152 ctx
->code
->bstr_pool
= heap_alloc(8 * sizeof(BSTR
));
153 if(!ctx
->code
->bstr_pool
)
155 ctx
->code
->bstr_pool_size
= 8;
156 }else if(ctx
->code
->bstr_pool_size
== ctx
->code
->bstr_cnt
) {
159 new_pool
= heap_realloc(ctx
->code
->bstr_pool
, ctx
->code
->bstr_pool_size
*2*sizeof(BSTR
));
163 ctx
->code
->bstr_pool
= new_pool
;
164 ctx
->code
->bstr_pool_size
*= 2;
170 static BSTR
compiler_alloc_bstr(compiler_ctx_t
*ctx
, const WCHAR
*str
)
172 if(!ensure_bstr_slot(ctx
))
175 ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
] = SysAllocString(str
);
176 if(!ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
])
179 return ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
++];
182 static BSTR
compiler_alloc_bstr_len(compiler_ctx_t
*ctx
, const WCHAR
*str
, size_t len
)
184 if(!ensure_bstr_slot(ctx
))
187 ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
] = SysAllocStringLen(str
, len
);
188 if(!ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
])
191 return ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
++];
194 static unsigned push_instr(compiler_ctx_t
*ctx
, jsop_t op
)
196 assert(ctx
->code_size
>= ctx
->code_off
);
198 if(ctx
->code_size
== ctx
->code_off
) {
201 new_instrs
= heap_realloc(ctx
->code
->instrs
, ctx
->code_size
*2*sizeof(instr_t
));
205 ctx
->code
->instrs
= new_instrs
;
209 ctx
->code
->instrs
[ctx
->code_off
].op
= op
;
210 return ctx
->code_off
++;
213 static inline instr_t
*instr_ptr(compiler_ctx_t
*ctx
, unsigned off
)
215 assert(off
< ctx
->code_off
);
216 return ctx
->code
->instrs
+ off
;
219 static HRESULT
push_instr_int(compiler_ctx_t
*ctx
, jsop_t op
, LONG arg
)
223 instr
= push_instr(ctx
, op
);
225 return E_OUTOFMEMORY
;
227 instr_ptr(ctx
, instr
)->u
.arg
->lng
= arg
;
231 static HRESULT
push_instr_str(compiler_ctx_t
*ctx
, jsop_t op
, const WCHAR
*arg
)
236 str
= compiler_alloc_string(ctx
, arg
);
238 return E_OUTOFMEMORY
;
240 instr
= push_instr(ctx
, op
);
242 return E_OUTOFMEMORY
;
244 instr_ptr(ctx
, instr
)->u
.arg
->str
= str
;
248 static HRESULT
push_instr_bstr(compiler_ctx_t
*ctx
, jsop_t op
, const WCHAR
*arg
)
253 str
= compiler_alloc_bstr(ctx
, arg
);
255 return E_OUTOFMEMORY
;
257 instr
= push_instr(ctx
, op
);
259 return E_OUTOFMEMORY
;
261 instr_ptr(ctx
, instr
)->u
.arg
->bstr
= str
;
265 static HRESULT
push_instr_bstr_uint(compiler_ctx_t
*ctx
, jsop_t op
, const WCHAR
*arg1
, unsigned arg2
)
270 str
= compiler_alloc_bstr(ctx
, arg1
);
272 return E_OUTOFMEMORY
;
274 instr
= push_instr(ctx
, op
);
276 return E_OUTOFMEMORY
;
278 instr_ptr(ctx
, instr
)->u
.arg
[0].bstr
= str
;
279 instr_ptr(ctx
, instr
)->u
.arg
[1].uint
= arg2
;
283 static HRESULT
push_instr_uint_str(compiler_ctx_t
*ctx
, jsop_t op
, unsigned arg1
, const WCHAR
*arg2
)
288 str
= compiler_alloc_string(ctx
, arg2
);
290 return E_OUTOFMEMORY
;
292 instr
= push_instr(ctx
, op
);
294 return E_OUTOFMEMORY
;
296 instr_ptr(ctx
, instr
)->u
.arg
[0].uint
= arg1
;
297 instr_ptr(ctx
, instr
)->u
.arg
[1].str
= str
;
301 static HRESULT
push_instr_double(compiler_ctx_t
*ctx
, jsop_t op
, double arg
)
305 instr
= push_instr(ctx
, op
);
307 return E_OUTOFMEMORY
;
309 instr_ptr(ctx
, instr
)->u
.dbl
= arg
;
313 static inline void set_arg_uint(compiler_ctx_t
*ctx
, unsigned instr
, unsigned arg
)
315 instr_ptr(ctx
, instr
)->u
.arg
->uint
= arg
;
318 static HRESULT
push_instr_uint(compiler_ctx_t
*ctx
, jsop_t op
, unsigned arg
)
322 instr
= push_instr(ctx
, op
);
324 return E_OUTOFMEMORY
;
326 set_arg_uint(ctx
, instr
, arg
);
330 static HRESULT
compile_binary_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
334 hres
= compile_expression(ctx
, expr
->expression1
, TRUE
);
338 hres
= compile_expression(ctx
, expr
->expression2
, TRUE
);
342 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
345 static HRESULT
compile_unary_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
, jsop_t op
)
349 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
353 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
356 /* ECMA-262 3rd Edition 11.2.1 */
357 static HRESULT
compile_member_expression(compiler_ctx_t
*ctx
, member_expression_t
*expr
)
361 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
365 return push_instr_bstr(ctx
, OP_member
, expr
->identifier
);
368 #define LABEL_FLAG 0x80000000
370 static unsigned alloc_label(compiler_ctx_t
*ctx
)
372 if(!ctx
->labels_size
) {
373 ctx
->labels
= heap_alloc(8 * sizeof(*ctx
->labels
));
376 ctx
->labels_size
= 8;
377 }else if(ctx
->labels_size
== ctx
->labels_cnt
) {
378 unsigned *new_labels
;
380 new_labels
= heap_realloc(ctx
->labels
, 2*ctx
->labels_size
*sizeof(*ctx
->labels
));
384 ctx
->labels
= new_labels
;
385 ctx
->labels_size
*= 2;
388 return ctx
->labels_cnt
++ | LABEL_FLAG
;
391 static void label_set_addr(compiler_ctx_t
*ctx
, unsigned label
)
393 assert(label
& LABEL_FLAG
);
394 ctx
->labels
[label
& ~LABEL_FLAG
] = ctx
->code_off
;
397 static inline BOOL
is_memberid_expr(expression_type_t type
)
399 return type
== EXPR_IDENT
|| type
== EXPR_MEMBER
|| type
== EXPR_ARRAY
;
402 static HRESULT
compile_memberid_expression(compiler_ctx_t
*ctx
, expression_t
*expr
, unsigned flags
)
408 identifier_expression_t
*ident_expr
= (identifier_expression_t
*)expr
;
410 hres
= push_instr_bstr_uint(ctx
, OP_identid
, ident_expr
->identifier
, flags
);
414 binary_expression_t
*array_expr
= (binary_expression_t
*)expr
;
416 hres
= compile_expression(ctx
, array_expr
->expression1
, TRUE
);
420 hres
= compile_expression(ctx
, array_expr
->expression2
, TRUE
);
424 hres
= push_instr_uint(ctx
, OP_memberid
, flags
);
428 member_expression_t
*member_expr
= (member_expression_t
*)expr
;
430 hres
= compile_expression(ctx
, member_expr
->expression
, TRUE
);
434 /* FIXME: Potential optimization */
435 hres
= push_instr_str(ctx
, OP_str
, member_expr
->identifier
);
439 hres
= push_instr_uint(ctx
, OP_memberid
, flags
);
448 static HRESULT
compile_increment_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
, jsop_t op
, int n
)
452 if(!is_memberid_expr(expr
->expression
->type
)) {
453 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
457 return push_instr_uint(ctx
, OP_throw_ref
, JS_E_ILLEGAL_ASSIGN
);
460 hres
= compile_memberid_expression(ctx
, expr
->expression
, fdexNameEnsure
);
464 return push_instr_int(ctx
, op
, n
);
467 /* ECMA-262 3rd Edition 11.14 */
468 static HRESULT
compile_comma_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, BOOL emit_ret
)
472 hres
= compile_expression(ctx
, expr
->expression1
, FALSE
);
476 return compile_expression(ctx
, expr
->expression2
, emit_ret
);
479 /* ECMA-262 3rd Edition 11.11 */
480 static HRESULT
compile_logical_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
485 hres
= compile_expression(ctx
, expr
->expression1
, TRUE
);
489 instr
= push_instr(ctx
, op
);
491 return E_OUTOFMEMORY
;
493 hres
= compile_expression(ctx
, expr
->expression2
, TRUE
);
497 set_arg_uint(ctx
, instr
, ctx
->code_off
);
501 /* ECMA-262 3rd Edition 11.12 */
502 static HRESULT
compile_conditional_expression(compiler_ctx_t
*ctx
, conditional_expression_t
*expr
)
504 unsigned jmp_false
, jmp_end
;
507 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
511 jmp_false
= push_instr(ctx
, OP_cnd_z
);
513 return E_OUTOFMEMORY
;
515 hres
= compile_expression(ctx
, expr
->true_expression
, TRUE
);
519 jmp_end
= push_instr(ctx
, OP_jmp
);
521 return E_OUTOFMEMORY
;
523 set_arg_uint(ctx
, jmp_false
, ctx
->code_off
);
524 hres
= push_instr_uint(ctx
, OP_pop
, 1);
528 hres
= compile_expression(ctx
, expr
->false_expression
, TRUE
);
532 set_arg_uint(ctx
, jmp_end
, ctx
->code_off
);
536 static HRESULT
compile_new_expression(compiler_ctx_t
*ctx
, call_expression_t
*expr
)
538 unsigned arg_cnt
= 0;
542 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
546 for(arg
= expr
->argument_list
; arg
; arg
= arg
->next
) {
547 hres
= compile_expression(ctx
, arg
->expr
, TRUE
);
553 return push_instr_uint(ctx
, OP_new
, arg_cnt
);
556 static HRESULT
compile_call_expression(compiler_ctx_t
*ctx
, call_expression_t
*expr
, BOOL emit_ret
)
558 unsigned arg_cnt
= 0;
564 if(is_memberid_expr(expr
->expression
->type
)) {
566 hres
= compile_memberid_expression(ctx
, expr
->expression
, 0);
569 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
575 for(arg
= expr
->argument_list
; arg
; arg
= arg
->next
) {
576 hres
= compile_expression(ctx
, arg
->expr
, TRUE
);
582 instr
= push_instr(ctx
, op
);
584 return E_OUTOFMEMORY
;
586 instr_ptr(ctx
, instr
)->u
.arg
[0].uint
= arg_cnt
;
587 instr_ptr(ctx
, instr
)->u
.arg
[1].lng
= emit_ret
;
591 static HRESULT
compile_delete_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
)
595 switch(expr
->expression
->type
) {
597 binary_expression_t
*array_expr
= (binary_expression_t
*)expr
->expression
;
599 hres
= compile_expression(ctx
, array_expr
->expression1
, TRUE
);
603 hres
= compile_expression(ctx
, array_expr
->expression2
, TRUE
);
607 if(!push_instr(ctx
, OP_delete
))
608 return E_OUTOFMEMORY
;
612 member_expression_t
*member_expr
= (member_expression_t
*)expr
->expression
;
614 hres
= compile_expression(ctx
, member_expr
->expression
, TRUE
);
618 /* FIXME: Potential optimization */
619 hres
= push_instr_str(ctx
, OP_str
, member_expr
->identifier
);
623 if(!push_instr(ctx
, OP_delete
))
624 return E_OUTOFMEMORY
;
628 return push_instr_bstr(ctx
, OP_delete_ident
, ((identifier_expression_t
*)expr
->expression
)->identifier
);
630 const WCHAR fixmeW
[] = {'F','I','X','M','E',0};
632 WARN("invalid delete, unimplemented exception message\n");
634 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
638 return push_instr_uint_str(ctx
, OP_throw_type
, JS_E_INVALID_DELETE
, fixmeW
);
645 static HRESULT
compile_assign_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
647 BOOL use_throw_path
= FALSE
;
648 unsigned arg_cnt
= 0;
651 if(expr
->expression1
->type
== EXPR_CALL
) {
652 call_expression_t
*call_expr
= (call_expression_t
*)expr
->expression1
;
656 FIXME("op %d not supported on parametrized assign expressions\n", op
);
660 if(is_memberid_expr(call_expr
->expression
->type
) && call_expr
->argument_list
) {
661 hres
= compile_memberid_expression(ctx
, call_expr
->expression
, fdexNameEnsure
);
665 for(arg
= call_expr
->argument_list
; arg
; arg
= arg
->next
) {
666 hres
= compile_expression(ctx
, arg
->expr
, TRUE
);
672 use_throw_path
= TRUE
;
674 }else if(is_memberid_expr(expr
->expression1
->type
)) {
675 hres
= compile_memberid_expression(ctx
, expr
->expression1
, fdexNameEnsure
);
679 use_throw_path
= TRUE
;
683 /* Illegal assignment: evaluate and throw */
684 hres
= compile_expression(ctx
, expr
->expression1
, TRUE
);
688 hres
= compile_expression(ctx
, expr
->expression2
, TRUE
);
692 if(op
!= OP_LAST
&& !push_instr(ctx
, op
))
693 return E_OUTOFMEMORY
;
695 return push_instr_uint(ctx
, OP_throw_ref
, JS_E_ILLEGAL_ASSIGN
);
698 if(op
!= OP_LAST
&& !push_instr(ctx
, OP_refval
))
699 return E_OUTOFMEMORY
;
701 hres
= compile_expression(ctx
, expr
->expression2
, TRUE
);
705 if(op
!= OP_LAST
&& !push_instr(ctx
, op
))
706 return E_OUTOFMEMORY
;
709 return push_instr_uint(ctx
, OP_assign_call
, arg_cnt
);
711 if(!push_instr(ctx
, OP_assign
))
712 return E_OUTOFMEMORY
;
717 static HRESULT
compile_typeof_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
)
722 if(is_memberid_expr(expr
->expression
->type
)) {
723 if(expr
->expression
->type
== EXPR_IDENT
)
724 return push_instr_bstr(ctx
, OP_typeofident
, ((identifier_expression_t
*)expr
->expression
)->identifier
);
727 hres
= compile_memberid_expression(ctx
, expr
->expression
, 0);
730 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
735 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
738 static HRESULT
compile_literal(compiler_ctx_t
*ctx
, literal_t
*literal
)
740 switch(literal
->type
) {
742 return push_instr_int(ctx
, OP_bool
, literal
->u
.bval
);
744 return push_instr_double(ctx
, OP_double
, literal
->u
.dval
);
746 return push_instr(ctx
, OP_null
) ? S_OK
: E_OUTOFMEMORY
;
748 return push_instr_str(ctx
, OP_str
, literal
->u
.wstr
);
753 str
= compiler_alloc_string_len(ctx
, literal
->u
.regexp
.str
, literal
->u
.regexp
.str_len
);
755 return E_OUTOFMEMORY
;
757 instr
= push_instr(ctx
, OP_regexp
);
759 return E_OUTOFMEMORY
;
761 instr_ptr(ctx
, instr
)->u
.arg
[0].str
= str
;
762 instr_ptr(ctx
, instr
)->u
.arg
[1].uint
= literal
->u
.regexp
.flags
;
770 static HRESULT
literal_as_bstr(compiler_ctx_t
*ctx
, literal_t
*literal
, BSTR
*str
)
772 switch(literal
->type
) {
774 *str
= compiler_alloc_bstr(ctx
, literal
->u
.wstr
);
780 hres
= double_to_string(literal
->u
.dval
, &jsstr
);
784 *str
= compiler_alloc_bstr_len(ctx
, NULL
, jsstr_length(jsstr
));
786 jsstr_flush(jsstr
, *str
);
787 jsstr_release(jsstr
);
793 return *str
? S_OK
: E_OUTOFMEMORY
;
796 static HRESULT
compile_array_literal(compiler_ctx_t
*ctx
, array_literal_expression_t
*expr
)
798 unsigned i
, elem_cnt
= expr
->length
;
799 array_element_t
*iter
;
802 for(iter
= expr
->element_list
; iter
; iter
= iter
->next
) {
803 elem_cnt
+= iter
->elision
+1;
805 for(i
=0; i
< iter
->elision
; i
++) {
806 if(!push_instr(ctx
, OP_undefined
))
807 return E_OUTOFMEMORY
;
810 hres
= compile_expression(ctx
, iter
->expr
, TRUE
);
815 for(i
=0; i
< expr
->length
; i
++) {
816 if(!push_instr(ctx
, OP_undefined
))
817 return E_OUTOFMEMORY
;
820 return push_instr_uint(ctx
, OP_carray
, elem_cnt
);
823 static HRESULT
compile_object_literal(compiler_ctx_t
*ctx
, property_value_expression_t
*expr
)
830 if(!push_instr(ctx
, OP_new_obj
))
831 return E_OUTOFMEMORY
;
833 for(iter
= expr
->property_list
; iter
; iter
= iter
->next
) {
834 hres
= literal_as_bstr(ctx
, iter
->name
, &name
);
838 hres
= compile_expression(ctx
, iter
->value
, TRUE
);
842 instr
= push_instr(ctx
, OP_obj_prop
);
844 return E_OUTOFMEMORY
;
846 instr_ptr(ctx
, instr
)->u
.arg
->bstr
= name
;
852 static HRESULT
compile_function_expression(compiler_ctx_t
*ctx
, function_expression_t
*expr
)
854 ctx
->func_tail
= ctx
->func_tail
? (ctx
->func_tail
->next
= expr
) : (ctx
->func_head
= expr
);
856 /* FIXME: not exactly right */
857 if(expr
->identifier
) {
858 ctx
->func
->func_cnt
++;
859 return push_instr_bstr(ctx
, OP_ident
, expr
->identifier
);
862 return push_instr_uint(ctx
, OP_func
, ctx
->func
->func_cnt
++);
865 static HRESULT
compile_expression(compiler_ctx_t
*ctx
, expression_t
*expr
, BOOL emit_ret
)
871 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_add
);
874 hres
= compile_logical_expression(ctx
, (binary_expression_t
*)expr
, OP_cnd_z
);
877 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_array
);
880 hres
= compile_array_literal(ctx
, (array_literal_expression_t
*)expr
);
883 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_LAST
);
886 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_add
);
889 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_and
);
892 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
895 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_mul
);
898 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_div
);
901 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_mod
);
904 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_or
);
906 case EXPR_ASSIGNLSHIFT
:
907 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_lshift
);
909 case EXPR_ASSIGNRSHIFT
:
910 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift
);
912 case EXPR_ASSIGNRRSHIFT
:
913 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift2
);
916 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_xor
);
919 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_and
);
922 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_bneg
);
925 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_or
);
928 return compile_call_expression(ctx
, (call_expression_t
*)expr
, emit_ret
);
930 return compile_comma_expression(ctx
, (binary_expression_t
*)expr
, emit_ret
);
932 hres
= compile_conditional_expression(ctx
, (conditional_expression_t
*)expr
);
935 hres
= compile_delete_expression(ctx
, (unary_expression_t
*)expr
);
938 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_div
);
941 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_eq
);
944 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_eq2
);
947 hres
= compile_function_expression(ctx
, (function_expression_t
*)expr
);
950 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gt
);
953 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gteq
);
956 hres
= push_instr_bstr(ctx
, OP_ident
, ((identifier_expression_t
*)expr
)->identifier
);
959 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_in
);
961 case EXPR_INSTANCEOF
:
962 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_instanceof
);
965 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lt
);
968 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lteq
);
971 hres
= compile_literal(ctx
, ((literal_expression_t
*)expr
)->literal
);
974 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_neg
);
977 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lshift
);
980 hres
= compile_member_expression(ctx
, (member_expression_t
*)expr
);
983 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_minus
);
986 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mod
);
989 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mul
);
992 hres
= compile_new_expression(ctx
, (call_expression_t
*)expr
);
995 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_neq
);
998 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_neq2
);
1001 hres
= compile_logical_expression(ctx
, (binary_expression_t
*)expr
, OP_cnd_nz
);
1004 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_tonum
);
1007 hres
= compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_postinc
, -1);
1010 hres
= compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_postinc
, 1);
1013 hres
= compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_preinc
, -1);
1016 hres
= compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_preinc
, 1);
1019 hres
= compile_object_literal(ctx
, (property_value_expression_t
*)expr
);
1022 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift
);
1025 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift2
);
1028 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
1031 return !emit_ret
|| push_instr(ctx
, OP_this
) ? S_OK
: E_OUTOFMEMORY
;
1033 hres
= compile_typeof_expression(ctx
, (unary_expression_t
*)expr
);
1036 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_void
);
1039 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_xor
);
1041 DEFAULT_UNREACHABLE
;
1047 return emit_ret
? S_OK
: push_instr_uint(ctx
, OP_pop
, 1);
1050 static inline BOOL
is_loop_statement(statement_type_t type
)
1052 return type
== STAT_FOR
|| type
== STAT_FORIN
|| type
== STAT_WHILE
;
1055 /* ECMA-262 3rd Edition 12.1 */
1056 static HRESULT
compile_block_statement(compiler_ctx_t
*ctx
, statement_t
*iter
)
1061 hres
= compile_statement(ctx
, NULL
, iter
);
1071 /* ECMA-262 3rd Edition 12.2 */
1072 static HRESULT
compile_variable_list(compiler_ctx_t
*ctx
, variable_declaration_t
*list
)
1074 variable_declaration_t
*iter
;
1077 assert(list
!= NULL
);
1080 ctx
->var_tail
->global_next
= list
;
1082 ctx
->var_head
= list
;
1084 for(iter
= list
; iter
; iter
= iter
->next
) {
1085 ctx
->func
->var_cnt
++;
1086 iter
->global_next
= iter
->next
;
1088 ctx
->var_tail
= iter
;
1093 hres
= compile_expression(ctx
, iter
->expr
, TRUE
);
1097 hres
= push_instr_bstr(ctx
, OP_var_set
, iter
->identifier
);
1105 /* ECMA-262 3rd Edition 12.2 */
1106 static HRESULT
compile_var_statement(compiler_ctx_t
*ctx
, var_statement_t
*stat
)
1108 return compile_variable_list(ctx
, stat
->variable_list
);
1111 /* ECMA-262 3rd Edition 12.4 */
1112 static HRESULT
compile_expression_statement(compiler_ctx_t
*ctx
, expression_statement_t
*stat
)
1116 hres
= compile_expression(ctx
, stat
->expr
, ctx
->from_eval
);
1120 return !ctx
->from_eval
|| push_instr(ctx
, OP_setret
) ? S_OK
: E_OUTOFMEMORY
;
1123 /* ECMA-262 3rd Edition 12.5 */
1124 static HRESULT
compile_if_statement(compiler_ctx_t
*ctx
, if_statement_t
*stat
)
1129 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1133 jmp_else
= push_instr(ctx
, OP_jmp_z
);
1135 return E_OUTOFMEMORY
;
1137 hres
= compile_statement(ctx
, NULL
, stat
->if_stat
);
1141 if(stat
->else_stat
) {
1144 jmp_end
= push_instr(ctx
, OP_jmp
);
1146 return E_OUTOFMEMORY
;
1148 set_arg_uint(ctx
, jmp_else
, ctx
->code_off
);
1150 hres
= compile_statement(ctx
, NULL
, stat
->else_stat
);
1154 set_arg_uint(ctx
, jmp_end
, ctx
->code_off
);
1156 set_arg_uint(ctx
, jmp_else
, ctx
->code_off
);
1162 /* ECMA-262 3rd Edition 12.6.2 */
1163 static HRESULT
compile_while_statement(compiler_ctx_t
*ctx
, while_statement_t
*stat
)
1165 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
};
1169 stat_ctx
.break_label
= alloc_label(ctx
);
1170 if(!stat_ctx
.break_label
)
1171 return E_OUTOFMEMORY
;
1173 stat_ctx
.continue_label
= alloc_label(ctx
);
1174 if(!stat_ctx
.continue_label
)
1175 return E_OUTOFMEMORY
;
1177 jmp_off
= ctx
->code_off
;
1179 if(!stat
->do_while
) {
1180 label_set_addr(ctx
, stat_ctx
.continue_label
);
1181 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1185 hres
= push_instr_uint(ctx
, OP_jmp_z
, stat_ctx
.break_label
);
1190 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1194 if(stat
->do_while
) {
1195 label_set_addr(ctx
, stat_ctx
.continue_label
);
1196 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1200 hres
= push_instr_uint(ctx
, OP_jmp_z
, stat_ctx
.break_label
);
1205 hres
= push_instr_uint(ctx
, OP_jmp
, jmp_off
);
1209 label_set_addr(ctx
, stat_ctx
.break_label
);
1213 /* ECMA-262 3rd Edition 12.6.3 */
1214 static HRESULT
compile_for_statement(compiler_ctx_t
*ctx
, for_statement_t
*stat
)
1216 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
};
1220 if(stat
->variable_list
) {
1221 hres
= compile_variable_list(ctx
, stat
->variable_list
);
1224 }else if(stat
->begin_expr
) {
1225 hres
= compile_expression(ctx
, stat
->begin_expr
, FALSE
);
1230 stat_ctx
.break_label
= alloc_label(ctx
);
1231 if(!stat_ctx
.break_label
)
1232 return E_OUTOFMEMORY
;
1234 stat_ctx
.continue_label
= alloc_label(ctx
);
1235 if(!stat_ctx
.continue_label
)
1236 return E_OUTOFMEMORY
;
1238 expr_off
= ctx
->code_off
;
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 label_set_addr(ctx
, stat_ctx
.continue_label
);
1256 if(stat
->end_expr
) {
1257 hres
= compile_expression(ctx
, stat
->end_expr
, FALSE
);
1262 hres
= push_instr_uint(ctx
, OP_jmp
, expr_off
);
1266 label_set_addr(ctx
, stat_ctx
.break_label
);
1270 /* ECMA-262 3rd Edition 12.6.4 */
1271 static HRESULT
compile_forin_statement(compiler_ctx_t
*ctx
, forin_statement_t
*stat
)
1273 statement_ctx_t stat_ctx
= {4, FALSE
, FALSE
};
1276 if(stat
->variable
) {
1277 hres
= compile_variable_list(ctx
, stat
->variable
);
1282 stat_ctx
.break_label
= alloc_label(ctx
);
1283 if(!stat_ctx
.break_label
)
1284 return E_OUTOFMEMORY
;
1286 stat_ctx
.continue_label
= alloc_label(ctx
);
1287 if(!stat_ctx
.continue_label
)
1288 return E_OUTOFMEMORY
;
1290 hres
= compile_expression(ctx
, stat
->in_expr
, TRUE
);
1294 if(stat
->variable
) {
1295 hres
= push_instr_bstr_uint(ctx
, OP_identid
, stat
->variable
->identifier
, fdexNameEnsure
);
1298 }else if(is_memberid_expr(stat
->expr
->type
)) {
1299 hres
= compile_memberid_expression(ctx
, stat
->expr
, fdexNameEnsure
);
1303 hres
= push_instr_uint(ctx
, OP_throw_ref
, JS_E_ILLEGAL_ASSIGN
);
1307 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1311 hres
= push_instr_int(ctx
, OP_int
, DISPID_STARTENUM
);
1315 label_set_addr(ctx
, stat_ctx
.continue_label
);
1316 hres
= push_instr_uint(ctx
, OP_forin
, stat_ctx
.break_label
);
1318 return E_OUTOFMEMORY
;
1320 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1324 hres
= push_instr_uint(ctx
, OP_jmp
, stat_ctx
.continue_label
);
1328 label_set_addr(ctx
, stat_ctx
.break_label
);
1332 static HRESULT
pop_to_stat(compiler_ctx_t
*ctx
, BOOL var_stack
, BOOL scope_stack
, statement_ctx_t
*stat_ctx
)
1334 unsigned stack_pop
= 0;
1335 statement_ctx_t
*iter
;
1337 for(iter
= ctx
->stat_ctx
; iter
!= stat_ctx
; iter
= iter
->next
) {
1339 if(iter
->using_scope
&& !push_instr(ctx
, OP_pop_scope
))
1340 return E_OUTOFMEMORY
;
1341 if(iter
->using_except
&& !push_instr(ctx
, OP_pop_except
))
1342 return E_OUTOFMEMORY
;
1344 stack_pop
+= iter
->stack_use
;
1347 if(var_stack
&& stack_pop
) {
1350 hres
= push_instr_uint(ctx
, OP_pop
, stack_pop
);
1358 /* ECMA-262 3rd Edition 12.7 */
1359 static HRESULT
compile_continue_statement(compiler_ctx_t
*ctx
, branch_statement_t
*stat
)
1361 statement_ctx_t
*pop_ctx
;
1364 if(stat
->identifier
) {
1365 statement_t
*label_stat
;
1366 statement_ctx_t
*iter
;
1370 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
1371 if(iter
->continue_label
)
1373 if(iter
->labelled_stat
&& !strcmpW(iter
->labelled_stat
->identifier
, stat
->identifier
))
1378 WARN("Label not found\n");
1379 return JS_E_LABEL_NOT_FOUND
;
1382 /* Labelled continue are allowed only on loops */
1383 for(label_stat
= iter
->labelled_stat
->statement
;
1384 label_stat
->type
== STAT_LABEL
;
1385 label_stat
= ((labelled_statement_t
*)label_stat
)->statement
);
1386 if(!is_loop_statement(label_stat
->type
)) {
1387 WARN("Label is not a loop\n");
1388 return JS_E_INVALID_CONTINUE
;
1391 assert(pop_ctx
!= NULL
);
1393 for(pop_ctx
= ctx
->stat_ctx
; pop_ctx
; pop_ctx
= pop_ctx
->next
) {
1394 if(pop_ctx
->continue_label
)
1399 WARN("continue outside loop\n");
1400 return JS_E_INVALID_CONTINUE
;
1404 hres
= pop_to_stat(ctx
, TRUE
, TRUE
, pop_ctx
);
1408 return push_instr_uint(ctx
, OP_jmp
, pop_ctx
->continue_label
);
1411 /* ECMA-262 3rd Edition 12.8 */
1412 static HRESULT
compile_break_statement(compiler_ctx_t
*ctx
, branch_statement_t
*stat
)
1414 statement_ctx_t
*pop_ctx
;
1417 if(stat
->identifier
) {
1418 for(pop_ctx
= ctx
->stat_ctx
; pop_ctx
; pop_ctx
= pop_ctx
->next
) {
1419 if(pop_ctx
->labelled_stat
&& !strcmpW(pop_ctx
->labelled_stat
->identifier
, stat
->identifier
)) {
1420 assert(pop_ctx
->break_label
);
1426 WARN("Label not found\n");
1427 return JS_E_LABEL_NOT_FOUND
;
1430 for(pop_ctx
= ctx
->stat_ctx
; pop_ctx
; pop_ctx
= pop_ctx
->next
) {
1431 if(pop_ctx
->break_label
&& !pop_ctx
->labelled_stat
)
1436 WARN("Break outside loop\n");
1437 return JS_E_INVALID_BREAK
;
1441 hres
= pop_to_stat(ctx
, TRUE
, TRUE
, pop_ctx
->next
);
1445 return push_instr_uint(ctx
, OP_jmp
, pop_ctx
->break_label
);
1448 /* ECMA-262 3rd Edition 12.9 */
1449 static HRESULT
compile_return_statement(compiler_ctx_t
*ctx
, expression_statement_t
*stat
)
1453 if(ctx
->from_eval
) {
1454 WARN("misplaced return statement\n");
1455 return JS_E_MISPLACED_RETURN
;
1458 hres
= pop_to_stat(ctx
, TRUE
, FALSE
, NULL
);
1463 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1466 if(!push_instr(ctx
, OP_setret
))
1467 return E_OUTOFMEMORY
;
1470 hres
= pop_to_stat(ctx
, FALSE
, TRUE
, NULL
);
1474 return push_instr(ctx
, OP_ret
) ? S_OK
: E_OUTOFMEMORY
;
1477 /* ECMA-262 3rd Edition 12.10 */
1478 static HRESULT
compile_with_statement(compiler_ctx_t
*ctx
, with_statement_t
*stat
)
1480 statement_ctx_t stat_ctx
= {0, TRUE
, FALSE
};
1483 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1487 if(!push_instr(ctx
, OP_push_scope
))
1488 return E_OUTOFMEMORY
;
1490 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1494 if(!push_instr(ctx
, OP_pop_scope
))
1495 return E_OUTOFMEMORY
;
1500 /* ECMA-262 3rd Edition 12.10 */
1501 static HRESULT
compile_labelled_statement(compiler_ctx_t
*ctx
, labelled_statement_t
*stat
)
1503 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
, 0, 0, stat
}, *iter
;
1506 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
1507 if(iter
->labelled_stat
&& !strcmpW(iter
->labelled_stat
->identifier
, stat
->identifier
)) {
1508 WARN("Label %s redefined\n", debugstr_w(stat
->identifier
));
1509 return JS_E_LABEL_REDEFINED
;
1513 /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1514 stat_ctx
.break_label
= alloc_label(ctx
);
1515 if(!stat_ctx
.break_label
)
1516 return E_OUTOFMEMORY
;
1518 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1522 label_set_addr(ctx
, stat_ctx
.break_label
);
1526 /* ECMA-262 3rd Edition 12.13 */
1527 static HRESULT
compile_switch_statement(compiler_ctx_t
*ctx
, switch_statement_t
*stat
)
1529 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
};
1530 unsigned case_cnt
= 0, *case_jmps
, i
, default_jmp
;
1531 BOOL have_default
= FALSE
;
1532 statement_t
*stat_iter
;
1533 case_clausule_t
*iter
;
1536 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1540 stat_ctx
.break_label
= alloc_label(ctx
);
1541 if(!stat_ctx
.break_label
)
1542 return E_OUTOFMEMORY
;
1544 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1549 case_jmps
= heap_alloc(case_cnt
* sizeof(*case_jmps
));
1551 return E_OUTOFMEMORY
;
1554 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1556 have_default
= TRUE
;
1560 hres
= compile_expression(ctx
, iter
->expr
, TRUE
);
1564 case_jmps
[i
] = push_instr(ctx
, OP_case
);
1566 hres
= E_OUTOFMEMORY
;
1572 if(SUCCEEDED(hres
)) {
1573 hres
= push_instr_uint(ctx
, OP_pop
, 1);
1574 if(SUCCEEDED(hres
)) {
1575 default_jmp
= push_instr(ctx
, OP_jmp
);
1577 hres
= E_OUTOFMEMORY
;
1582 heap_free(case_jmps
);
1587 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1588 while(iter
->next
&& iter
->next
->stat
== iter
->stat
) {
1589 set_arg_uint(ctx
, iter
->expr
? case_jmps
[i
++] : default_jmp
, ctx
->code_off
);
1593 set_arg_uint(ctx
, iter
->expr
? case_jmps
[i
++] : default_jmp
, ctx
->code_off
);
1595 for(stat_iter
= iter
->stat
; stat_iter
&& (!iter
->next
|| iter
->next
->stat
!= stat_iter
);
1596 stat_iter
= stat_iter
->next
) {
1597 hres
= compile_statement(ctx
, &stat_ctx
, stat_iter
);
1605 heap_free(case_jmps
);
1608 assert(i
== case_cnt
);
1611 hres
= push_instr_uint(ctx
, OP_jmp
, stat_ctx
.break_label
);
1614 set_arg_uint(ctx
, default_jmp
, ctx
->code_off
);
1617 label_set_addr(ctx
, stat_ctx
.break_label
);
1621 /* ECMA-262 3rd Edition 12.13 */
1622 static HRESULT
compile_throw_statement(compiler_ctx_t
*ctx
, expression_statement_t
*stat
)
1626 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1630 return push_instr(ctx
, OP_throw
) ? S_OK
: E_OUTOFMEMORY
;
1633 /* ECMA-262 3rd Edition 12.14 */
1634 static HRESULT
compile_try_statement(compiler_ctx_t
*ctx
, try_statement_t
*stat
)
1636 statement_ctx_t try_ctx
= {0, FALSE
, TRUE
}, catch_ctx
= {0, TRUE
, FALSE
};
1637 statement_ctx_t finally_ctx
= {2, FALSE
, FALSE
};
1638 unsigned push_except
;
1642 push_except
= push_instr(ctx
, OP_push_except
);
1644 return E_OUTOFMEMORY
;
1646 if(stat
->catch_block
) {
1647 ident
= compiler_alloc_bstr(ctx
, stat
->catch_block
->identifier
);
1649 return E_OUTOFMEMORY
;
1654 instr_ptr(ctx
, push_except
)->u
.arg
[1].bstr
= ident
;
1656 if(!stat
->catch_block
)
1657 try_ctx
.stack_use
= 2;
1659 hres
= compile_statement(ctx
, &try_ctx
, stat
->try_statement
);
1663 if(!push_instr(ctx
, OP_pop_except
))
1664 return E_OUTOFMEMORY
;
1666 if(stat
->catch_block
) {
1667 unsigned jmp_finally
;
1669 jmp_finally
= push_instr(ctx
, OP_jmp
);
1671 return E_OUTOFMEMORY
;
1673 instr_ptr(ctx
, push_except
)->u
.arg
[0].uint
= ctx
->code_off
;
1675 hres
= compile_statement(ctx
, &catch_ctx
, stat
->catch_block
->statement
);
1679 if(!push_instr(ctx
, OP_pop_scope
))
1680 return E_OUTOFMEMORY
;
1682 set_arg_uint(ctx
, jmp_finally
, ctx
->code_off
);
1684 set_arg_uint(ctx
, push_except
, ctx
->code_off
);
1687 if(stat
->finally_statement
) {
1688 hres
= compile_statement(ctx
, stat
->catch_block
? NULL
: &finally_ctx
, stat
->finally_statement
);
1692 if(!stat
->catch_block
&& !push_instr(ctx
, OP_end_finally
))
1693 return E_OUTOFMEMORY
;
1699 static HRESULT
compile_statement(compiler_ctx_t
*ctx
, statement_ctx_t
*stat_ctx
, statement_t
*stat
)
1704 stat_ctx
->next
= ctx
->stat_ctx
;
1705 ctx
->stat_ctx
= stat_ctx
;
1708 switch(stat
->type
) {
1710 hres
= compile_block_statement(ctx
, ((block_statement_t
*)stat
)->stat_list
);
1713 hres
= compile_break_statement(ctx
, (branch_statement_t
*)stat
);
1716 hres
= compile_continue_statement(ctx
, (branch_statement_t
*)stat
);
1723 hres
= compile_expression_statement(ctx
, (expression_statement_t
*)stat
);
1726 hres
= compile_for_statement(ctx
, (for_statement_t
*)stat
);
1729 hres
= compile_forin_statement(ctx
, (forin_statement_t
*)stat
);
1732 hres
= compile_if_statement(ctx
, (if_statement_t
*)stat
);
1735 hres
= compile_labelled_statement(ctx
, (labelled_statement_t
*)stat
);
1738 hres
= compile_return_statement(ctx
, (expression_statement_t
*)stat
);
1741 hres
= compile_switch_statement(ctx
, (switch_statement_t
*)stat
);
1744 hres
= compile_throw_statement(ctx
, (expression_statement_t
*)stat
);
1747 hres
= compile_try_statement(ctx
, (try_statement_t
*)stat
);
1750 hres
= compile_var_statement(ctx
, (var_statement_t
*)stat
);
1753 hres
= compile_while_statement(ctx
, (while_statement_t
*)stat
);
1756 hres
= compile_with_statement(ctx
, (with_statement_t
*)stat
);
1758 DEFAULT_UNREACHABLE
;
1762 assert(ctx
->stat_ctx
== stat_ctx
);
1763 ctx
->stat_ctx
= stat_ctx
->next
;
1769 static void resolve_labels(compiler_ctx_t
*ctx
, unsigned off
)
1773 for(instr
= ctx
->code
->instrs
+off
; instr
< ctx
->code
->instrs
+ctx
->code_off
; instr
++) {
1774 if(instr_info
[instr
->op
].arg1_type
== ARG_ADDR
&& (instr
->u
.arg
->uint
& LABEL_FLAG
)) {
1775 assert((instr
->u
.arg
->uint
& ~LABEL_FLAG
) < ctx
->labels_cnt
);
1776 instr
->u
.arg
->uint
= ctx
->labels
[instr
->u
.arg
->uint
& ~LABEL_FLAG
];
1778 assert(instr_info
[instr
->op
].arg2_type
!= ARG_ADDR
);
1781 ctx
->labels_cnt
= 0;
1784 void release_bytecode(bytecode_t
*code
)
1791 for(i
=0; i
< code
->bstr_cnt
; i
++)
1792 SysFreeString(code
->bstr_pool
[i
]);
1793 for(i
=0; i
< code
->str_cnt
; i
++)
1794 jsstr_release(code
->str_pool
[i
]);
1796 heap_free(code
->source
);
1797 heap_pool_free(&code
->heap
);
1798 heap_free(code
->bstr_pool
);
1799 heap_free(code
->str_pool
);
1800 heap_free(code
->instrs
);
1804 static HRESULT
init_code(compiler_ctx_t
*compiler
, const WCHAR
*source
)
1806 compiler
->code
= heap_alloc_zero(sizeof(bytecode_t
));
1808 return E_OUTOFMEMORY
;
1810 compiler
->code
->ref
= 1;
1811 heap_pool_init(&compiler
->code
->heap
);
1813 compiler
->code
->source
= heap_strdupW(source
);
1814 if(!compiler
->code
->source
) {
1815 release_bytecode(compiler
->code
);
1816 return E_OUTOFMEMORY
;
1819 compiler
->code
->instrs
= heap_alloc(64 * sizeof(instr_t
));
1820 if(!compiler
->code
->instrs
) {
1821 release_bytecode(compiler
->code
);
1822 return E_OUTOFMEMORY
;
1825 compiler
->code_size
= 64;
1826 compiler
->code_off
= 1;
1830 static HRESULT
compile_function(compiler_ctx_t
*ctx
, source_elements_t
*source
, function_expression_t
*func_expr
,
1831 BOOL from_eval
, function_code_t
*func
)
1833 variable_declaration_t
*var_iter
;
1834 function_expression_t
*iter
;
1840 ctx
->var_head
= ctx
->var_tail
= NULL
;
1841 ctx
->func_head
= ctx
->func_tail
= NULL
;
1842 ctx
->from_eval
= from_eval
;
1844 off
= ctx
->code_off
;
1846 hres
= compile_block_statement(ctx
, source
->statement
);
1850 resolve_labels(ctx
, off
);
1852 if(!push_instr(ctx
, OP_ret
))
1853 return E_OUTOFMEMORY
;
1855 if(TRACE_ON(jscript_disas
))
1856 dump_code(ctx
, off
);
1858 func
->instr_off
= off
;
1860 if(func_expr
&& func_expr
->identifier
) {
1861 func
->name
= compiler_alloc_bstr(ctx
, func_expr
->identifier
);
1863 return E_OUTOFMEMORY
;
1867 parameter_t
*param_iter
;
1869 func
->source
= func_expr
->src_str
;
1870 func
->source_len
= func_expr
->src_len
;
1872 for(param_iter
= func_expr
->parameter_list
; param_iter
; param_iter
= param_iter
->next
)
1875 func
->params
= compiler_alloc(ctx
->code
, func
->param_cnt
* sizeof(*func
->params
));
1877 return E_OUTOFMEMORY
;
1879 for(param_iter
= func_expr
->parameter_list
, i
=0; param_iter
; param_iter
= param_iter
->next
, i
++) {
1880 func
->params
[i
] = compiler_alloc_bstr(ctx
, param_iter
->identifier
);
1881 if(!func
->params
[i
])
1882 return E_OUTOFMEMORY
;
1886 func
->variables
= compiler_alloc(ctx
->code
, func
->var_cnt
* sizeof(*func
->variables
));
1887 if(!func
->variables
)
1888 return E_OUTOFMEMORY
;
1890 for(var_iter
= ctx
->var_head
, i
=0; var_iter
; var_iter
= var_iter
->global_next
, i
++) {
1891 func
->variables
[i
] = compiler_alloc_bstr(ctx
, var_iter
->identifier
);
1892 if(!func
->variables
[i
])
1893 return E_OUTOFMEMORY
;
1896 assert(i
== func
->var_cnt
);
1898 func
->funcs
= compiler_alloc(ctx
->code
, func
->func_cnt
* sizeof(*func
->funcs
));
1900 return E_OUTOFMEMORY
;
1901 memset(func
->funcs
, 0, func
->func_cnt
* sizeof(*func
->funcs
));
1903 for(iter
= ctx
->func_head
, i
=0; iter
; iter
= iter
->next
, i
++) {
1904 hres
= compile_function(ctx
, iter
->source_elements
, iter
, FALSE
, func
->funcs
+i
);
1909 assert(i
== func
->func_cnt
);
1914 static HRESULT
parse_arguments(compiler_ctx_t
*ctx
, const WCHAR
*args
, BSTR
*arg_array
, unsigned *args_size
)
1916 const WCHAR
*ptr
= args
, *ptr2
;
1917 unsigned arg_cnt
= 0;
1919 while(isspaceW(*ptr
))
1928 if(!isalphaW(*ptr
) && *ptr
!= '_') {
1929 FIXME("expected alpha or '_': %s\n", debugstr_w(ptr
));
1934 while(isalnumW(*ptr
) || *ptr
== '_')
1937 if(*ptr
&& *ptr
!= ',' && !isspaceW(*ptr
)) {
1938 FIXME("unexpected har %s\n", debugstr_w(ptr
));
1943 arg_array
[arg_cnt
] = compiler_alloc_bstr_len(ctx
, ptr2
, ptr
-ptr2
);
1944 if(!arg_array
[arg_cnt
])
1945 return E_OUTOFMEMORY
;
1949 while(isspaceW(*ptr
))
1954 FIXME("expected ',': %s\n", debugstr_w(ptr
));
1959 while(isspaceW(*ptr
))
1964 *args_size
= arg_cnt
;
1968 static HRESULT
compile_arguments(compiler_ctx_t
*ctx
, const WCHAR
*args
)
1972 hres
= parse_arguments(ctx
, args
, NULL
, &ctx
->code
->global_code
.param_cnt
);
1976 ctx
->code
->global_code
.params
= compiler_alloc(ctx
->code
,
1977 ctx
->code
->global_code
.param_cnt
* sizeof(*ctx
->code
->global_code
.params
));
1978 if(!ctx
->code
->global_code
.params
)
1979 return E_OUTOFMEMORY
;
1981 return parse_arguments(ctx
, args
, ctx
->code
->global_code
.params
, NULL
);
1984 HRESULT
compile_script(script_ctx_t
*ctx
, const WCHAR
*code
, const WCHAR
*args
, const WCHAR
*delimiter
,
1985 BOOL from_eval
, BOOL use_decode
, bytecode_t
**ret
)
1987 compiler_ctx_t compiler
= {0};
1990 hres
= init_code(&compiler
, code
);
1995 hres
= compile_arguments(&compiler
, args
);
2001 hres
= decode_source(compiler
.code
->source
);
2003 WARN("Decoding failed\n");
2008 hres
= script_parse(ctx
, compiler
.code
->source
, delimiter
, from_eval
, &compiler
.parser
);
2010 release_bytecode(compiler
.code
);
2014 hres
= compile_function(&compiler
, compiler
.parser
->source
, NULL
, from_eval
, &compiler
.code
->global_code
);
2015 parser_release(compiler
.parser
);
2017 release_bytecode(compiler
.code
);
2021 *ret
= compiler
.code
;