2 * Copyright 2011 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/rbtree.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
30 WINE_DECLARE_DEBUG_CHANNEL(jscript_disas
);
32 typedef struct _statement_ctx_t
{
38 unsigned continue_label
;
40 const labelled_statement_t
*labelled_stat
;
42 unsigned int scope_index
;
44 BOOL scope_has_functions
;
45 struct _statement_ctx_t
*next
;
49 struct wine_rb_entry entry
;
54 typedef struct _compiler_ctx_t
{
69 struct wine_rb_tree locals
;
70 unsigned int locals_cnt
;
71 unsigned int *ref_index
;
74 unsigned local_scope_count
;
75 unsigned local_scope_size
;
77 statement_ctx_t
*stat_ctx
;
78 function_code_t
*func
;
82 function_expression_t
*func_head
;
83 function_expression_t
*func_tail
;
84 function_expression_t
*current_function_expr
;
91 instr_arg_type_t arg1_type
;
92 instr_arg_type_t arg2_type
;
94 #define X(n,a,b,c) {#n,b,c},
99 static void dump_instr_arg(instr_arg_type_t type
, instr_arg_t
*arg
)
103 TRACE_(jscript_disas
)("\t%s", debugstr_jsstr(arg
->str
));
106 TRACE_(jscript_disas
)("\t%s", debugstr_wn(arg
->bstr
, SysStringLen(arg
->bstr
)));
109 TRACE_(jscript_disas
)("\t%d", arg
->uint
);
113 TRACE_(jscript_disas
)("\t%u", arg
->uint
);
122 static void dump_code(compiler_ctx_t
*ctx
, unsigned off
)
126 for(instr
= ctx
->code
->instrs
+off
; instr
< ctx
->code
->instrs
+ctx
->code_off
; instr
++) {
127 TRACE_(jscript_disas
)("%d:\t%s", (int)(instr
-ctx
->code
->instrs
), instr_info
[instr
->op
].op_str
);
128 if(instr_info
[instr
->op
].arg1_type
== ARG_DBL
) {
129 TRACE_(jscript_disas
)("\t%lf", instr
->u
.dbl
);
131 dump_instr_arg(instr_info
[instr
->op
].arg1_type
, instr
->u
.arg
);
132 dump_instr_arg(instr_info
[instr
->op
].arg2_type
, instr
->u
.arg
+1);
134 TRACE_(jscript_disas
)("\n");
138 static HRESULT
compile_expression(compiler_ctx_t
*,expression_t
*,BOOL
);
139 static HRESULT
compile_statement(compiler_ctx_t
*,statement_ctx_t
*,statement_t
*);
141 static int function_local_cmp(const void *key
, const struct wine_rb_entry
*entry
)
143 function_local_t
*local
= WINE_RB_ENTRY_VALUE(entry
, function_local_t
, entry
);
144 return wcscmp(key
, local
->name
);
147 static BOOL
alloc_local_scope(compiler_ctx_t
*ctx
, unsigned int *scope_index
)
149 unsigned int scope
, new_size
;
152 scope
= ctx
->local_scope_count
++;
153 if (scope
== ctx
->local_scope_size
)
155 new_size
= max(1, ctx
->local_scope_size
* 2);
156 if (!(new_alloc
= realloc(ctx
->local_scopes
, new_size
* sizeof(*ctx
->local_scopes
))))
158 ctx
->local_scopes
= new_alloc
;
159 ctx
->local_scope_size
= new_size
;
162 ctx
->local_scopes
[scope
].locals_cnt
= 0;
163 ctx
->local_scopes
[scope
].ref_index
= scope_index
;
164 wine_rb_init(&ctx
->local_scopes
[scope
].locals
, function_local_cmp
);
165 *scope_index
= scope
;
170 static void remove_local_scope(compiler_ctx_t
*ctx
, unsigned int scope_index
)
174 assert(scope_index
< ctx
->local_scope_count
);
175 --ctx
->local_scope_count
;
176 assert(scope_index
== *ctx
->local_scopes
[scope_index
].ref_index
);
177 *ctx
->local_scopes
[scope_index
].ref_index
= 0;
178 memmove(&ctx
->local_scopes
[scope_index
], &ctx
->local_scopes
[scope_index
+ 1],
179 sizeof(*ctx
->local_scopes
) * (ctx
->local_scope_count
- scope_index
));
180 for (i
= scope_index
; i
< ctx
->local_scope_count
; ++i
)
181 --*ctx
->local_scopes
[i
].ref_index
;
184 static inline void *compiler_alloc(bytecode_t
*code
, size_t size
)
186 return heap_pool_alloc(&code
->heap
, size
);
189 jsstr_t
*compiler_alloc_string_len(compiler_ctx_t
*ctx
, const WCHAR
*str
, unsigned len
)
193 if(!ctx
->code
->str_pool_size
) {
194 ctx
->code
->str_pool
= malloc(8 * sizeof(jsstr_t
*));
195 if(!ctx
->code
->str_pool
)
197 ctx
->code
->str_pool_size
= 8;
198 }else if(ctx
->code
->str_pool_size
== ctx
->code
->str_cnt
) {
201 new_pool
= realloc(ctx
->code
->str_pool
, ctx
->code
->str_pool_size
*2*sizeof(jsstr_t
*));
205 ctx
->code
->str_pool
= new_pool
;
206 ctx
->code
->str_pool_size
*= 2;
209 new_str
= jsstr_alloc_len(str
, len
);
213 ctx
->code
->str_pool
[ctx
->code
->str_cnt
++] = new_str
;
217 static jsstr_t
*compiler_alloc_string(compiler_ctx_t
*ctx
, const WCHAR
*str
)
219 return compiler_alloc_string_len(ctx
, str
, lstrlenW(str
));
222 static BOOL
ensure_bstr_slot(compiler_ctx_t
*ctx
)
224 if(!ctx
->code
->bstr_pool_size
) {
225 ctx
->code
->bstr_pool
= malloc(8 * sizeof(BSTR
));
226 if(!ctx
->code
->bstr_pool
)
228 ctx
->code
->bstr_pool_size
= 8;
229 }else if(ctx
->code
->bstr_pool_size
== ctx
->code
->bstr_cnt
) {
232 new_pool
= realloc(ctx
->code
->bstr_pool
, ctx
->code
->bstr_pool_size
*2*sizeof(BSTR
));
236 ctx
->code
->bstr_pool
= new_pool
;
237 ctx
->code
->bstr_pool_size
*= 2;
243 static BSTR
compiler_alloc_bstr(compiler_ctx_t
*ctx
, const WCHAR
*str
)
245 if(!ensure_bstr_slot(ctx
))
248 ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
] = SysAllocString(str
);
249 if(!ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
])
252 return ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
++];
255 static BSTR
compiler_alloc_bstr_len(compiler_ctx_t
*ctx
, const WCHAR
*str
, size_t len
)
257 if(!ensure_bstr_slot(ctx
))
260 ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
] = SysAllocStringLen(str
, len
);
261 if(!ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
])
264 return ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
++];
267 void set_compiler_loc(compiler_ctx_t
*ctx
, unsigned loc
)
272 static unsigned push_instr(compiler_ctx_t
*ctx
, jsop_t op
)
274 assert(ctx
->code_size
>= ctx
->code_off
);
276 if(ctx
->code_size
== ctx
->code_off
) {
279 new_instrs
= realloc(ctx
->code
->instrs
, ctx
->code_size
*2*sizeof(instr_t
));
283 ctx
->code
->instrs
= new_instrs
;
287 ctx
->code
->instrs
[ctx
->code_off
].op
= op
;
288 ctx
->code
->instrs
[ctx
->code_off
].loc
= ctx
->loc
;
289 return ctx
->code_off
++;
292 static inline instr_t
*instr_ptr(compiler_ctx_t
*ctx
, unsigned off
)
294 assert(off
< ctx
->code_off
);
295 return ctx
->code
->instrs
+ off
;
298 static HRESULT
push_instr_int(compiler_ctx_t
*ctx
, jsop_t op
, LONG arg
)
302 instr
= push_instr(ctx
, op
);
304 return E_OUTOFMEMORY
;
306 instr_ptr(ctx
, instr
)->u
.arg
->lng
= arg
;
310 static HRESULT
push_instr_str(compiler_ctx_t
*ctx
, jsop_t op
, jsstr_t
*str
)
314 instr
= push_instr(ctx
, op
);
316 return E_OUTOFMEMORY
;
318 instr_ptr(ctx
, instr
)->u
.arg
->str
= str
;
322 static HRESULT
push_instr_str_uint(compiler_ctx_t
*ctx
, jsop_t op
, jsstr_t
*str
, unsigned arg2
)
326 instr
= push_instr(ctx
, op
);
328 return E_OUTOFMEMORY
;
330 instr_ptr(ctx
, instr
)->u
.arg
[0].str
= str
;
331 instr_ptr(ctx
, instr
)->u
.arg
[1].uint
= arg2
;
335 static HRESULT
push_instr_bstr(compiler_ctx_t
*ctx
, jsop_t op
, const WCHAR
*arg
)
340 str
= compiler_alloc_bstr(ctx
, arg
);
342 return E_OUTOFMEMORY
;
344 instr
= push_instr(ctx
, op
);
346 return E_OUTOFMEMORY
;
348 instr_ptr(ctx
, instr
)->u
.arg
->bstr
= str
;
352 static HRESULT
push_instr_bstr_uint(compiler_ctx_t
*ctx
, jsop_t op
, const WCHAR
*arg1
, unsigned arg2
)
357 str
= compiler_alloc_bstr(ctx
, arg1
);
359 return E_OUTOFMEMORY
;
361 instr
= push_instr(ctx
, op
);
363 return E_OUTOFMEMORY
;
365 instr_ptr(ctx
, instr
)->u
.arg
[0].bstr
= str
;
366 instr_ptr(ctx
, instr
)->u
.arg
[1].uint
= arg2
;
370 static HRESULT
push_instr_uint_str(compiler_ctx_t
*ctx
, jsop_t op
, unsigned arg1
, const WCHAR
*arg2
)
375 str
= compiler_alloc_string(ctx
, arg2
);
377 return E_OUTOFMEMORY
;
379 instr
= push_instr(ctx
, op
);
381 return E_OUTOFMEMORY
;
383 instr_ptr(ctx
, instr
)->u
.arg
[0].uint
= arg1
;
384 instr_ptr(ctx
, instr
)->u
.arg
[1].str
= str
;
388 static HRESULT
push_instr_double(compiler_ctx_t
*ctx
, jsop_t op
, double arg
)
392 instr
= push_instr(ctx
, op
);
394 return E_OUTOFMEMORY
;
396 instr_ptr(ctx
, instr
)->u
.dbl
= arg
;
400 static inline void set_arg_uint(compiler_ctx_t
*ctx
, unsigned instr
, unsigned arg
)
402 instr_ptr(ctx
, instr
)->u
.arg
->uint
= arg
;
405 static HRESULT
push_instr_uint(compiler_ctx_t
*ctx
, jsop_t op
, unsigned arg
)
409 instr
= push_instr(ctx
, op
);
411 return E_OUTOFMEMORY
;
413 set_arg_uint(ctx
, instr
, arg
);
417 static HRESULT
compile_binary_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
421 hres
= compile_expression(ctx
, expr
->expression1
, TRUE
);
425 hres
= compile_expression(ctx
, expr
->expression2
, TRUE
);
429 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
432 static HRESULT
compile_unary_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
, jsop_t op
)
436 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
440 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
443 /* ECMA-262 3rd Edition 11.2.1 */
444 static HRESULT
compile_member_expression(compiler_ctx_t
*ctx
, member_expression_t
*expr
)
448 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
452 return push_instr_bstr(ctx
, OP_member
, expr
->identifier
);
455 #define LABEL_FLAG 0x80000000
457 static unsigned alloc_label(compiler_ctx_t
*ctx
)
459 if(!ctx
->labels_size
) {
460 ctx
->labels
= malloc(8 * sizeof(*ctx
->labels
));
463 ctx
->labels_size
= 8;
464 }else if(ctx
->labels_size
== ctx
->labels_cnt
) {
465 unsigned *new_labels
;
467 new_labels
= realloc(ctx
->labels
, 2*ctx
->labels_size
*sizeof(*ctx
->labels
));
471 ctx
->labels
= new_labels
;
472 ctx
->labels_size
*= 2;
475 return ctx
->labels_cnt
++ | LABEL_FLAG
;
478 static void label_set_addr(compiler_ctx_t
*ctx
, unsigned label
)
480 assert(label
& LABEL_FLAG
);
481 ctx
->labels
[label
& ~LABEL_FLAG
] = ctx
->code_off
;
484 static inline BOOL
is_memberid_expr(expression_type_t type
)
486 return type
== EXPR_IDENT
|| type
== EXPR_MEMBER
|| type
== EXPR_ARRAY
;
489 static BOOL
bind_local(compiler_ctx_t
*ctx
, const WCHAR
*identifier
, int *ret_ref
)
491 statement_ctx_t
*iter
;
494 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
495 if(iter
->using_scope
)
497 if (!iter
->block_scope
)
500 if ((ref
= lookup_local(ctx
->func
, identifier
, iter
->scope_index
)))
508 ref
= lookup_local(ctx
->func
, identifier
, 0);
516 static HRESULT
emit_identifier_ref(compiler_ctx_t
*ctx
, const WCHAR
*identifier
, unsigned flags
)
519 if(bind_local(ctx
, identifier
, &local_ref
))
520 return push_instr_int(ctx
, OP_local_ref
, local_ref
);
521 return push_instr_bstr_uint(ctx
, OP_identid
, identifier
, flags
);
524 static HRESULT
emit_identifier(compiler_ctx_t
*ctx
, const WCHAR
*identifier
)
527 if(bind_local(ctx
, identifier
, &local_ref
))
528 return push_instr_int(ctx
, OP_local
, local_ref
);
529 return push_instr_bstr(ctx
, OP_ident
, identifier
);
532 static HRESULT
emit_member_expression(compiler_ctx_t
*ctx
, expression_t
*expr
)
536 if(expr
->type
== EXPR_ARRAY
) {
537 binary_expression_t
*array_expr
= (binary_expression_t
*)expr
;
539 hres
= compile_expression(ctx
, array_expr
->expression1
, TRUE
);
543 hres
= compile_expression(ctx
, array_expr
->expression2
, TRUE
);
547 if(!push_instr(ctx
, OP_to_string
))
548 return E_OUTOFMEMORY
;
550 member_expression_t
*member_expr
= (member_expression_t
*)expr
;
553 assert(expr
->type
== EXPR_MEMBER
);
555 hres
= compile_expression(ctx
, member_expr
->expression
, TRUE
);
559 jsstr
= compiler_alloc_string(ctx
, member_expr
->identifier
);
561 return E_OUTOFMEMORY
;
563 hres
= push_instr_str(ctx
, OP_str
, jsstr
);
571 static void push_compiler_statement_ctx(compiler_ctx_t
*ctx
, statement_ctx_t
*stat_ctx
)
575 stat_ctx
->next
= ctx
->stat_ctx
;
576 ctx
->stat_ctx
= stat_ctx
;
580 static void pop_compiler_statement_ctx(compiler_ctx_t
*ctx
, statement_ctx_t
*stat_ctx
)
584 assert(ctx
->stat_ctx
== stat_ctx
);
585 ctx
->stat_ctx
= stat_ctx
->next
;
589 static HRESULT
compile_memberid_expression(compiler_ctx_t
*ctx
, expression_t
*expr
, unsigned flags
)
593 if(expr
->type
== EXPR_IDENT
) {
594 identifier_expression_t
*ident_expr
= (identifier_expression_t
*)expr
;
595 return emit_identifier_ref(ctx
, ident_expr
->identifier
, flags
);
598 hres
= emit_member_expression(ctx
, expr
);
602 return push_instr_uint(ctx
, OP_memberid
, flags
);
605 static HRESULT
compile_increment_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
, jsop_t op
, int n
)
609 if(!is_memberid_expr(expr
->expression
->type
)) {
610 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
614 return push_instr_uint(ctx
, OP_throw_ref
, JS_E_ILLEGAL_ASSIGN
);
617 hres
= compile_memberid_expression(ctx
, expr
->expression
, fdexNameEnsure
);
621 return push_instr_int(ctx
, op
, n
);
624 /* ECMA-262 3rd Edition 11.14 */
625 static HRESULT
compile_comma_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, BOOL emit_ret
)
629 hres
= compile_expression(ctx
, expr
->expression1
, FALSE
);
633 return compile_expression(ctx
, expr
->expression2
, emit_ret
);
636 /* ECMA-262 3rd Edition 11.11 */
637 static HRESULT
compile_logical_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
642 hres
= compile_expression(ctx
, expr
->expression1
, TRUE
);
646 instr
= push_instr(ctx
, op
);
648 return E_OUTOFMEMORY
;
650 hres
= compile_expression(ctx
, expr
->expression2
, TRUE
);
654 set_arg_uint(ctx
, instr
, ctx
->code_off
);
658 /* ECMA-262 3rd Edition 11.12 */
659 static HRESULT
compile_conditional_expression(compiler_ctx_t
*ctx
, conditional_expression_t
*expr
)
661 unsigned jmp_false
, jmp_end
;
664 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
668 jmp_false
= push_instr(ctx
, OP_cnd_z
);
670 return E_OUTOFMEMORY
;
672 hres
= compile_expression(ctx
, expr
->true_expression
, TRUE
);
676 jmp_end
= push_instr(ctx
, OP_jmp
);
678 return E_OUTOFMEMORY
;
680 set_arg_uint(ctx
, jmp_false
, ctx
->code_off
);
681 hres
= push_instr_uint(ctx
, OP_pop
, 1);
685 hres
= compile_expression(ctx
, expr
->false_expression
, TRUE
);
689 set_arg_uint(ctx
, jmp_end
, ctx
->code_off
);
693 static HRESULT
compile_new_expression(compiler_ctx_t
*ctx
, call_expression_t
*expr
)
695 unsigned arg_cnt
= 0;
699 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
703 for(arg
= expr
->argument_list
; arg
; arg
= arg
->next
) {
704 hres
= compile_expression(ctx
, arg
->expr
, TRUE
);
710 hres
= push_instr_uint(ctx
, OP_new
, arg_cnt
);
714 hres
= push_instr_uint(ctx
, OP_pop
, arg_cnt
+1);
718 return push_instr(ctx
, OP_push_acc
) ? S_OK
: E_OUTOFMEMORY
;
721 static HRESULT
compile_call_expression(compiler_ctx_t
*ctx
, call_expression_t
*expr
, BOOL emit_ret
)
723 unsigned arg_cnt
= 0, extra_args
= 0;
729 if(is_memberid_expr(expr
->expression
->type
)) {
730 if(expr
->expression
->type
== EXPR_IDENT
&& !wcscmp(((identifier_expression_t
*)expr
->expression
)->identifier
, L
"eval"))
735 hres
= compile_memberid_expression(ctx
, expr
->expression
, 0);
740 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
746 for(arg
= expr
->argument_list
; arg
; arg
= arg
->next
) {
747 hres
= compile_expression(ctx
, arg
->expr
, TRUE
);
753 instr
= push_instr(ctx
, op
);
755 return E_OUTOFMEMORY
;
757 instr_ptr(ctx
, instr
)->u
.arg
[0].uint
= arg_cnt
;
758 instr_ptr(ctx
, instr
)->u
.arg
[1].lng
= emit_ret
;
760 hres
= push_instr_uint(ctx
, OP_pop
, arg_cnt
+ extra_args
);
764 return !emit_ret
|| push_instr(ctx
, OP_push_acc
) ? S_OK
: E_OUTOFMEMORY
;
767 static HRESULT
compile_delete_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
)
771 switch(expr
->expression
->type
) {
773 binary_expression_t
*array_expr
= (binary_expression_t
*)expr
->expression
;
775 hres
= compile_expression(ctx
, array_expr
->expression1
, TRUE
);
779 hres
= compile_expression(ctx
, array_expr
->expression2
, TRUE
);
783 if(!push_instr(ctx
, OP_delete
))
784 return E_OUTOFMEMORY
;
788 member_expression_t
*member_expr
= (member_expression_t
*)expr
->expression
;
791 hres
= compile_expression(ctx
, member_expr
->expression
, TRUE
);
795 /* FIXME: Potential optimization */
796 jsstr
= compiler_alloc_string(ctx
, member_expr
->identifier
);
798 return E_OUTOFMEMORY
;
800 hres
= push_instr_str(ctx
, OP_str
, jsstr
);
804 if(!push_instr(ctx
, OP_delete
))
805 return E_OUTOFMEMORY
;
809 return push_instr_bstr(ctx
, OP_delete_ident
, ((identifier_expression_t
*)expr
->expression
)->identifier
);
811 WARN("invalid delete, unimplemented exception message\n");
813 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
817 return push_instr_uint_str(ctx
, OP_throw_type
, JS_E_INVALID_DELETE
, L
"FIXME");
824 static HRESULT
compile_assign_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
826 jsop_t assign_op
= OP_throw_ref
;
827 unsigned arg_cnt
= 0;
830 if(expr
->expression1
->type
== EXPR_CALL
) {
831 call_expression_t
*call_expr
= (call_expression_t
*)expr
->expression1
;
834 if(is_memberid_expr(call_expr
->expression
->type
) && call_expr
->argument_list
) {
835 hres
= compile_memberid_expression(ctx
, call_expr
->expression
, fdexNameEnsure
);
839 for(arg
= call_expr
->argument_list
; arg
; arg
= arg
->next
) {
840 hres
= compile_expression(ctx
, arg
->expr
, TRUE
);
849 /* We need to call the functions twice: to get the value and to set it.
850 * JavaScript interpreted functions may to modify value on the stack,
851 * but assignment calls are allowed only on external functions, so we
852 * may reuse the stack here. */
853 instr
= push_instr(ctx
, OP_call_member
);
855 return E_OUTOFMEMORY
;
856 instr_ptr(ctx
, instr
)->u
.arg
[0].uint
= arg_cnt
;
857 instr_ptr(ctx
, instr
)->u
.arg
[1].lng
= 1;
859 if(!push_instr(ctx
, OP_push_acc
))
860 return E_OUTOFMEMORY
;
862 assign_op
= OP_assign_call
;
864 }else if(is_memberid_expr(expr
->expression1
->type
)) {
865 if(op
!= OP_LAST
|| expr
->expression1
->type
== EXPR_IDENT
) {
866 hres
= compile_memberid_expression(ctx
, expr
->expression1
, fdexNameEnsure
);
869 if(op
!= OP_LAST
&& !push_instr(ctx
, OP_refval
))
870 return E_OUTOFMEMORY
;
871 assign_op
= OP_assign
;
873 hres
= emit_member_expression(ctx
, expr
->expression1
);
876 assign_op
= OP_set_member
;
880 if(assign_op
== OP_throw_ref
) {
881 /* Illegal assignment: evaluate and throw */
882 hres
= compile_expression(ctx
, expr
->expression1
, TRUE
);
885 arg_cnt
= JS_E_ILLEGAL_ASSIGN
;
888 hres
= compile_expression(ctx
, expr
->expression2
, TRUE
);
892 if(op
!= OP_LAST
&& !push_instr(ctx
, op
))
893 return E_OUTOFMEMORY
;
895 return push_instr_uint(ctx
, assign_op
, arg_cnt
);
898 static HRESULT
compile_typeof_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
)
903 if(is_memberid_expr(expr
->expression
->type
)) {
904 if(expr
->expression
->type
== EXPR_IDENT
)
905 return push_instr_bstr(ctx
, OP_typeofident
, ((identifier_expression_t
*)expr
->expression
)->identifier
);
908 hres
= compile_memberid_expression(ctx
, expr
->expression
, 0);
911 hres
= compile_expression(ctx
, expr
->expression
, TRUE
);
916 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
919 static HRESULT
compile_literal(compiler_ctx_t
*ctx
, literal_t
*literal
)
921 switch(literal
->type
) {
923 return push_instr_int(ctx
, OP_bool
, literal
->u
.bval
);
925 return push_instr_double(ctx
, OP_double
, literal
->u
.dval
);
927 return push_instr(ctx
, OP_null
) ? S_OK
: E_OUTOFMEMORY
;
929 return push_instr_str(ctx
, OP_str
, literal
->u
.str
);
931 return push_instr_str_uint(ctx
, OP_regexp
, literal
->u
.regexp
.str
, literal
->u
.regexp
.flags
);
937 static HRESULT
literal_as_string(compiler_ctx_t
*ctx
, literal_t
*literal
, jsstr_t
**str
)
939 switch(literal
->type
) {
941 *str
= literal
->u
.str
;
944 return double_to_string(literal
->u
.dval
, str
);
948 return *str
? S_OK
: E_OUTOFMEMORY
;
951 static HRESULT
compile_array_literal(compiler_ctx_t
*ctx
, array_literal_expression_t
*expr
)
954 array_element_t
*iter
;
955 unsigned array_instr
;
958 array_instr
= push_instr(ctx
, OP_carray
);
960 for(iter
= expr
->element_list
; iter
; iter
= iter
->next
) {
961 length
+= iter
->elision
;
963 hres
= compile_expression(ctx
, iter
->expr
, TRUE
);
967 hres
= push_instr_uint(ctx
, OP_carray_set
, length
);
974 instr_ptr(ctx
, array_instr
)->u
.arg
[0].uint
= length
+ expr
->length
;
978 static HRESULT
compile_object_literal(compiler_ctx_t
*ctx
, property_value_expression_t
*expr
)
980 property_definition_t
*iter
;
984 if(!push_instr(ctx
, OP_new_obj
))
985 return E_OUTOFMEMORY
;
987 for(iter
= expr
->property_list
; iter
; iter
= iter
->next
) {
988 hres
= literal_as_string(ctx
, iter
->name
, &name
);
992 hres
= compile_expression(ctx
, iter
->value
, TRUE
);
996 hres
= push_instr_str_uint(ctx
, OP_obj_prop
, name
, iter
->type
);
1004 static HRESULT
compile_function_expression(compiler_ctx_t
*ctx
, function_expression_t
*expr
, BOOL emit_ret
)
1006 statement_ctx_t
*stat_ctx
;
1008 assert(ctx
->current_function_expr
);
1010 for(stat_ctx
= ctx
->stat_ctx
; stat_ctx
; stat_ctx
= stat_ctx
->next
)
1012 if(stat_ctx
->block_scope
)
1015 ctx
->current_function_expr
->scope_index
= stat_ctx
? stat_ctx
->scope_index
: 0;
1016 ctx
->current_function_expr
= ctx
->current_function_expr
->next
;
1018 return emit_ret
? push_instr_uint(ctx
, OP_func
, expr
->func_id
) : S_OK
;
1021 static HRESULT
compile_expression(compiler_ctx_t
*ctx
, expression_t
*expr
, BOOL emit_ret
)
1025 switch(expr
->type
) {
1027 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_add
);
1030 hres
= compile_logical_expression(ctx
, (binary_expression_t
*)expr
, OP_cnd_z
);
1033 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_array
);
1036 hres
= compile_array_literal(ctx
, (array_literal_expression_t
*)expr
);
1039 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_LAST
);
1041 case EXPR_ASSIGNADD
:
1042 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_add
);
1044 case EXPR_ASSIGNAND
:
1045 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_and
);
1047 case EXPR_ASSIGNSUB
:
1048 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
1050 case EXPR_ASSIGNMUL
:
1051 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_mul
);
1053 case EXPR_ASSIGNDIV
:
1054 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_div
);
1056 case EXPR_ASSIGNMOD
:
1057 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_mod
);
1060 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_or
);
1062 case EXPR_ASSIGNLSHIFT
:
1063 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_lshift
);
1065 case EXPR_ASSIGNRSHIFT
:
1066 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift
);
1068 case EXPR_ASSIGNRRSHIFT
:
1069 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift2
);
1071 case EXPR_ASSIGNXOR
:
1072 hres
= compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_xor
);
1075 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_and
);
1078 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_bneg
);
1081 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_or
);
1084 return compile_call_expression(ctx
, (call_expression_t
*)expr
, emit_ret
);
1086 return compile_comma_expression(ctx
, (binary_expression_t
*)expr
, emit_ret
);
1088 hres
= compile_conditional_expression(ctx
, (conditional_expression_t
*)expr
);
1091 hres
= compile_delete_expression(ctx
, (unary_expression_t
*)expr
);
1094 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_div
);
1097 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_eq
);
1100 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_eq2
);
1103 return compile_function_expression(ctx
, (function_expression_t
*)expr
, emit_ret
);
1105 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gt
);
1107 case EXPR_GREATEREQ
:
1108 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gteq
);
1111 hres
= emit_identifier(ctx
, ((identifier_expression_t
*)expr
)->identifier
);
1114 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_in
);
1116 case EXPR_INSTANCEOF
:
1117 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_instanceof
);
1120 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lt
);
1123 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lteq
);
1126 hres
= compile_literal(ctx
, ((literal_expression_t
*)expr
)->literal
);
1129 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_neg
);
1132 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lshift
);
1135 hres
= compile_member_expression(ctx
, (member_expression_t
*)expr
);
1138 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_minus
);
1141 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mod
);
1144 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mul
);
1147 hres
= compile_new_expression(ctx
, (call_expression_t
*)expr
);
1150 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_neq
);
1153 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_neq2
);
1156 hres
= compile_logical_expression(ctx
, (binary_expression_t
*)expr
, OP_cnd_nz
);
1159 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_tonum
);
1162 hres
= compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_postinc
, -1);
1165 hres
= compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_postinc
, 1);
1168 hres
= compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_preinc
, -1);
1171 hres
= compile_increment_expression(ctx
, (unary_expression_t
*)expr
, OP_preinc
, 1);
1174 hres
= compile_object_literal(ctx
, (property_value_expression_t
*)expr
);
1177 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift
);
1180 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_rshift2
);
1183 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
1186 return !emit_ret
|| push_instr(ctx
, OP_this
) ? S_OK
: E_OUTOFMEMORY
;
1188 hres
= compile_typeof_expression(ctx
, (unary_expression_t
*)expr
);
1191 hres
= compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_void
);
1194 hres
= compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_xor
);
1196 DEFAULT_UNREACHABLE
;
1202 return emit_ret
? S_OK
: push_instr_uint(ctx
, OP_pop
, 1);
1205 static inline BOOL
is_loop_statement(statement_type_t type
)
1207 return type
== STAT_FOR
|| type
== STAT_FORIN
|| type
== STAT_WHILE
;
1210 /* ECMA-262 3rd Edition 12.1 */
1211 static HRESULT
compile_block_statement(compiler_ctx_t
*ctx
, block_statement_t
*block
, statement_t
*iter
)
1213 statement_ctx_t stat_ctx
= {0, TRUE
};
1217 needs_scope
= block
&& block
->scope_index
;
1220 if(FAILED(hres
= push_instr_uint(ctx
, OP_push_block_scope
, block
->scope_index
)))
1223 stat_ctx
.scope_index
= block
->scope_index
;
1224 stat_ctx
.block_scope
= TRUE
;
1228 hres
= compile_statement(ctx
, needs_scope
? &stat_ctx
: NULL
, iter
);
1235 if(needs_scope
&& !push_instr(ctx
, OP_pop_scope
))
1236 return E_OUTOFMEMORY
;
1241 /* ECMA-262 3rd Edition 12.2 */
1242 static HRESULT
compile_variable_list(compiler_ctx_t
*ctx
, variable_declaration_t
*list
)
1244 variable_declaration_t
*iter
;
1247 assert(list
!= NULL
);
1249 for(iter
= list
; iter
; iter
= iter
->next
) {
1254 FIXME("Constant variables are not supported.\n");
1256 hres
= emit_identifier_ref(ctx
, iter
->identifier
, 0);
1260 hres
= compile_expression(ctx
, iter
->expr
, TRUE
);
1264 if(!push_instr(ctx
, OP_assign
))
1265 return E_OUTOFMEMORY
;
1267 hres
= push_instr_uint(ctx
, OP_pop
, 1);
1275 /* ECMA-262 3rd Edition 12.2 */
1276 static HRESULT
compile_var_statement(compiler_ctx_t
*ctx
, var_statement_t
*stat
)
1278 return compile_variable_list(ctx
, stat
->variable_list
);
1281 /* ECMA-262 3rd Edition 12.4 */
1282 static HRESULT
compile_expression_statement(compiler_ctx_t
*ctx
, expression_statement_t
*stat
)
1286 hres
= compile_expression(ctx
, stat
->expr
, ctx
->from_eval
);
1290 return !ctx
->from_eval
|| push_instr(ctx
, OP_setret
) ? S_OK
: E_OUTOFMEMORY
;
1293 /* ECMA-262 3rd Edition 12.5 */
1294 static HRESULT
compile_if_statement(compiler_ctx_t
*ctx
, if_statement_t
*stat
)
1299 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1303 jmp_else
= push_instr(ctx
, OP_jmp_z
);
1305 return E_OUTOFMEMORY
;
1307 hres
= compile_statement(ctx
, NULL
, stat
->if_stat
);
1311 if(stat
->else_stat
) {
1314 jmp_end
= push_instr(ctx
, OP_jmp
);
1316 return E_OUTOFMEMORY
;
1318 set_arg_uint(ctx
, jmp_else
, ctx
->code_off
);
1320 hres
= compile_statement(ctx
, NULL
, stat
->else_stat
);
1324 set_arg_uint(ctx
, jmp_end
, ctx
->code_off
);
1326 set_arg_uint(ctx
, jmp_else
, ctx
->code_off
);
1332 /* ECMA-262 3rd Edition 12.6.2 */
1333 static HRESULT
compile_while_statement(compiler_ctx_t
*ctx
, while_statement_t
*stat
)
1335 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
};
1339 stat_ctx
.break_label
= alloc_label(ctx
);
1340 if(!stat_ctx
.break_label
)
1341 return E_OUTOFMEMORY
;
1343 stat_ctx
.continue_label
= alloc_label(ctx
);
1344 if(!stat_ctx
.continue_label
)
1345 return E_OUTOFMEMORY
;
1347 jmp_off
= ctx
->code_off
;
1349 if(!stat
->do_while
) {
1350 label_set_addr(ctx
, stat_ctx
.continue_label
);
1351 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1355 hres
= push_instr_uint(ctx
, OP_jmp_z
, stat_ctx
.break_label
);
1360 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1364 set_compiler_loc(ctx
, stat
->stat
.loc
);
1365 if(stat
->do_while
) {
1366 label_set_addr(ctx
, stat_ctx
.continue_label
);
1367 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1371 hres
= push_instr_uint(ctx
, OP_jmp_z
, stat_ctx
.break_label
);
1376 hres
= push_instr_uint(ctx
, OP_jmp
, jmp_off
);
1380 label_set_addr(ctx
, stat_ctx
.break_label
);
1384 /* ECMA-262 10th Edition 13.7.4 */
1385 static HRESULT
compile_for_statement(compiler_ctx_t
*ctx
, for_statement_t
*stat
)
1387 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
};
1388 statement_ctx_t scope_stat_ctx
= {0, TRUE
};
1392 if (stat
->scope_index
)
1394 if(FAILED(hres
= push_instr_uint(ctx
, OP_push_block_scope
, stat
->scope_index
)))
1397 scope_stat_ctx
.scope_index
= stat
->scope_index
;
1398 scope_stat_ctx
.block_scope
= TRUE
;
1399 push_compiler_statement_ctx(ctx
, &scope_stat_ctx
);
1402 if(stat
->variable_list
) {
1403 hres
= compile_variable_list(ctx
, stat
->variable_list
);
1406 }else if(stat
->begin_expr
) {
1407 hres
= compile_expression(ctx
, stat
->begin_expr
, FALSE
);
1412 stat_ctx
.break_label
= alloc_label(ctx
);
1413 if(!stat_ctx
.break_label
)
1415 hres
= E_OUTOFMEMORY
;
1419 stat_ctx
.continue_label
= alloc_label(ctx
);
1420 if(!stat_ctx
.continue_label
)
1422 hres
= E_OUTOFMEMORY
;
1425 expr_off
= ctx
->code_off
;
1428 set_compiler_loc(ctx
, stat
->expr_loc
);
1429 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1433 hres
= push_instr_uint(ctx
, OP_jmp_z
, stat_ctx
.break_label
);
1438 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1442 label_set_addr(ctx
, stat_ctx
.continue_label
);
1444 if(stat
->end_expr
) {
1445 set_compiler_loc(ctx
, stat
->end_loc
);
1446 hres
= compile_expression(ctx
, stat
->end_expr
, FALSE
);
1451 hres
= push_instr_uint(ctx
, OP_jmp
, expr_off
);
1455 label_set_addr(ctx
, stat_ctx
.break_label
);
1458 if (stat
->scope_index
)
1460 pop_compiler_statement_ctx(ctx
, &scope_stat_ctx
);
1461 if(SUCCEEDED(hres
) && !push_instr(ctx
, OP_pop_scope
))
1462 return E_OUTOFMEMORY
;
1467 /* ECMA-262 3rd Edition 12.6.4 */
1468 static HRESULT
compile_forin_statement(compiler_ctx_t
*ctx
, forin_statement_t
*stat
)
1470 statement_ctx_t stat_ctx
= {4, FALSE
, FALSE
};
1473 if(stat
->variable
) {
1474 hres
= compile_variable_list(ctx
, stat
->variable
);
1479 stat_ctx
.break_label
= alloc_label(ctx
);
1480 if(!stat_ctx
.break_label
)
1481 return E_OUTOFMEMORY
;
1483 stat_ctx
.continue_label
= alloc_label(ctx
);
1484 if(!stat_ctx
.continue_label
)
1485 return E_OUTOFMEMORY
;
1487 hres
= compile_expression(ctx
, stat
->in_expr
, TRUE
);
1491 if(stat
->variable
) {
1492 hres
= emit_identifier_ref(ctx
, stat
->variable
->identifier
, fdexNameEnsure
);
1495 }else if(is_memberid_expr(stat
->expr
->type
)) {
1496 hres
= compile_memberid_expression(ctx
, stat
->expr
, fdexNameEnsure
);
1500 hres
= push_instr_uint(ctx
, OP_throw_ref
, JS_E_ILLEGAL_ASSIGN
);
1504 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1508 hres
= push_instr_int(ctx
, OP_int
, DISPID_STARTENUM
);
1512 label_set_addr(ctx
, stat_ctx
.continue_label
);
1513 hres
= push_instr_uint(ctx
, OP_forin
, stat_ctx
.break_label
);
1515 return E_OUTOFMEMORY
;
1517 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1521 hres
= push_instr_uint(ctx
, OP_jmp
, stat_ctx
.continue_label
);
1525 label_set_addr(ctx
, stat_ctx
.break_label
);
1529 static HRESULT
pop_to_stat(compiler_ctx_t
*ctx
, statement_ctx_t
*stat_ctx
)
1531 unsigned stack_pop
= 0;
1532 statement_ctx_t
*iter
;
1535 for(iter
= ctx
->stat_ctx
; iter
!= stat_ctx
; iter
= iter
->next
) {
1536 if(iter
->using_scope
&& !push_instr(ctx
, OP_pop_scope
))
1537 return E_OUTOFMEMORY
;
1538 if(iter
->using_except
) {
1540 hres
= push_instr_uint(ctx
, OP_pop
, stack_pop
);
1545 hres
= push_instr_uint(ctx
, OP_pop_except
, ctx
->code_off
+1);
1549 stack_pop
+= iter
->stack_use
;
1553 hres
= push_instr_uint(ctx
, OP_pop
, stack_pop
);
1561 /* ECMA-262 3rd Edition 12.7 */
1562 static HRESULT
compile_continue_statement(compiler_ctx_t
*ctx
, branch_statement_t
*stat
)
1564 statement_ctx_t
*pop_ctx
;
1567 if(stat
->identifier
) {
1568 statement_t
*label_stat
;
1569 statement_ctx_t
*iter
;
1573 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
1574 if(iter
->continue_label
)
1576 if(iter
->labelled_stat
&& !wcscmp(iter
->labelled_stat
->identifier
, stat
->identifier
))
1581 WARN("Label not found\n");
1582 return JS_E_LABEL_NOT_FOUND
;
1585 /* Labelled continue are allowed only on loops */
1586 for(label_stat
= iter
->labelled_stat
->statement
;
1587 label_stat
->type
== STAT_LABEL
;
1588 label_stat
= ((labelled_statement_t
*)label_stat
)->statement
);
1589 if(!is_loop_statement(label_stat
->type
)) {
1590 WARN("Label is not a loop\n");
1591 return JS_E_INVALID_CONTINUE
;
1594 assert(pop_ctx
!= NULL
);
1596 for(pop_ctx
= ctx
->stat_ctx
; pop_ctx
; pop_ctx
= pop_ctx
->next
) {
1597 if(pop_ctx
->continue_label
)
1602 WARN("continue outside loop\n");
1603 return JS_E_INVALID_CONTINUE
;
1607 hres
= pop_to_stat(ctx
, pop_ctx
);
1611 return push_instr_uint(ctx
, OP_jmp
, pop_ctx
->continue_label
);
1614 /* ECMA-262 3rd Edition 12.8 */
1615 static HRESULT
compile_break_statement(compiler_ctx_t
*ctx
, branch_statement_t
*stat
)
1617 statement_ctx_t
*pop_ctx
;
1620 if(stat
->identifier
) {
1621 for(pop_ctx
= ctx
->stat_ctx
; pop_ctx
; pop_ctx
= pop_ctx
->next
) {
1622 if(pop_ctx
->labelled_stat
&& !wcscmp(pop_ctx
->labelled_stat
->identifier
, stat
->identifier
)) {
1623 assert(pop_ctx
->break_label
);
1629 WARN("Label not found\n");
1630 return JS_E_LABEL_NOT_FOUND
;
1633 for(pop_ctx
= ctx
->stat_ctx
; pop_ctx
; pop_ctx
= pop_ctx
->next
) {
1634 if(pop_ctx
->break_label
&& !pop_ctx
->labelled_stat
)
1639 WARN("Break outside loop\n");
1640 return JS_E_INVALID_BREAK
;
1644 hres
= pop_to_stat(ctx
, pop_ctx
->next
);
1648 return push_instr_uint(ctx
, OP_jmp
, pop_ctx
->break_label
);
1651 /* ECMA-262 3rd Edition 12.9 */
1652 static HRESULT
compile_return_statement(compiler_ctx_t
*ctx
, expression_statement_t
*stat
)
1656 if(ctx
->from_eval
) {
1657 WARN("misplaced return statement\n");
1658 return JS_E_MISPLACED_RETURN
;
1662 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1665 if(!push_instr(ctx
, OP_setret
))
1666 return E_OUTOFMEMORY
;
1669 hres
= pop_to_stat(ctx
, NULL
);
1673 return push_instr_uint(ctx
, OP_ret
, !stat
->expr
);
1676 /* ECMA-262 3rd Edition 12.10 */
1677 static HRESULT
compile_with_statement(compiler_ctx_t
*ctx
, with_statement_t
*stat
)
1679 statement_ctx_t stat_ctx
= {0, TRUE
, FALSE
};
1682 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1686 if(!push_instr(ctx
, OP_push_with_scope
))
1687 return E_OUTOFMEMORY
;
1689 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1693 if(!push_instr(ctx
, OP_pop_scope
))
1694 return E_OUTOFMEMORY
;
1699 /* ECMA-262 3rd Edition 12.10 */
1700 static HRESULT
compile_labelled_statement(compiler_ctx_t
*ctx
, labelled_statement_t
*stat
)
1702 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
, 0, 0, stat
}, *iter
;
1705 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
1706 if(iter
->labelled_stat
&& !wcscmp(iter
->labelled_stat
->identifier
, stat
->identifier
)) {
1707 WARN("Label %s redefined\n", debugstr_w(stat
->identifier
));
1708 return JS_E_LABEL_REDEFINED
;
1712 /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1713 stat_ctx
.break_label
= alloc_label(ctx
);
1714 if(!stat_ctx
.break_label
)
1715 return E_OUTOFMEMORY
;
1717 hres
= compile_statement(ctx
, &stat_ctx
, stat
->statement
);
1721 label_set_addr(ctx
, stat_ctx
.break_label
);
1725 /* ECMA-262 3rd Edition 12.13 */
1726 static HRESULT
compile_switch_statement(compiler_ctx_t
*ctx
, switch_statement_t
*stat
)
1728 statement_ctx_t stat_ctx
= {0, FALSE
, FALSE
};
1729 unsigned case_cnt
= 0, *case_jmps
, i
, default_jmp
;
1730 BOOL have_default
= FALSE
;
1731 statement_t
*stat_iter
;
1732 case_clausule_t
*iter
;
1735 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1739 stat_ctx
.break_label
= alloc_label(ctx
);
1740 if(!stat_ctx
.break_label
)
1741 return E_OUTOFMEMORY
;
1743 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1748 case_jmps
= malloc(case_cnt
* sizeof(*case_jmps
));
1750 return E_OUTOFMEMORY
;
1753 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1755 have_default
= TRUE
;
1759 set_compiler_loc(ctx
, iter
->loc
);
1760 hres
= compile_expression(ctx
, iter
->expr
, TRUE
);
1764 case_jmps
[i
] = push_instr(ctx
, OP_case
);
1766 hres
= E_OUTOFMEMORY
;
1772 if(SUCCEEDED(hres
)) {
1773 hres
= push_instr_uint(ctx
, OP_pop
, 1);
1774 if(SUCCEEDED(hres
)) {
1775 default_jmp
= push_instr(ctx
, OP_jmp
);
1777 hres
= E_OUTOFMEMORY
;
1787 for(iter
= stat
->case_list
; iter
; iter
= iter
->next
) {
1788 while(iter
->next
&& iter
->next
->stat
== iter
->stat
) {
1789 set_arg_uint(ctx
, iter
->expr
? case_jmps
[i
++] : default_jmp
, ctx
->code_off
);
1793 set_arg_uint(ctx
, iter
->expr
? case_jmps
[i
++] : default_jmp
, ctx
->code_off
);
1795 for(stat_iter
= iter
->stat
; stat_iter
&& (!iter
->next
|| iter
->next
->stat
!= stat_iter
);
1796 stat_iter
= stat_iter
->next
) {
1797 hres
= compile_statement(ctx
, &stat_ctx
, stat_iter
);
1808 assert(i
== case_cnt
);
1811 hres
= push_instr_uint(ctx
, OP_jmp
, stat_ctx
.break_label
);
1814 set_arg_uint(ctx
, default_jmp
, ctx
->code_off
);
1817 label_set_addr(ctx
, stat_ctx
.break_label
);
1821 /* ECMA-262 3rd Edition 12.13 */
1822 static HRESULT
compile_throw_statement(compiler_ctx_t
*ctx
, expression_statement_t
*stat
)
1826 hres
= compile_expression(ctx
, stat
->expr
, TRUE
);
1830 return push_instr(ctx
, OP_throw
) ? S_OK
: E_OUTOFMEMORY
;
1833 /* ECMA-262 3rd Edition 12.14 */
1834 static HRESULT
compile_try_statement(compiler_ctx_t
*ctx
, try_statement_t
*stat
)
1836 statement_ctx_t try_ctx
= {0, FALSE
, TRUE
}, finally_ctx
= {2, FALSE
, FALSE
};
1837 unsigned push_except
, finally_off
= 0, catch_off
= 0, pop_except
, catch_pop_except
= 0;
1841 push_except
= push_instr(ctx
, OP_push_except
);
1843 return E_OUTOFMEMORY
;
1845 if(stat
->catch_block
) {
1846 ident
= compiler_alloc_bstr(ctx
, stat
->catch_block
->identifier
);
1848 return E_OUTOFMEMORY
;
1853 hres
= compile_statement(ctx
, &try_ctx
, stat
->try_statement
);
1857 pop_except
= push_instr(ctx
, OP_pop_except
);
1859 return E_OUTOFMEMORY
;
1861 if(stat
->catch_block
) {
1862 statement_ctx_t catch_ctx
= {0, TRUE
, stat
->finally_statement
!= NULL
};
1864 if(stat
->finally_statement
)
1865 catch_ctx
.using_except
= TRUE
;
1867 catch_off
= ctx
->code_off
;
1869 hres
= push_instr_bstr(ctx
, OP_enter_catch
, ident
);
1873 hres
= compile_statement(ctx
, &catch_ctx
, stat
->catch_block
->statement
);
1877 if(!push_instr(ctx
, OP_pop_scope
))
1878 return E_OUTOFMEMORY
;
1880 if(stat
->finally_statement
) {
1881 catch_pop_except
= push_instr(ctx
, OP_pop_except
);
1882 if(!catch_pop_except
)
1883 return E_OUTOFMEMORY
;
1887 if(stat
->finally_statement
) {
1889 * finally block expects two elements on the stack, which may be:
1890 * - (true, return_addr) set by OP_pop_except, OP_end_finally jumps back to passed address
1891 * - (false, exception_value) set when unwinding an exception, which OP_end_finally rethrows
1893 finally_off
= ctx
->code_off
;
1894 hres
= compile_statement(ctx
, &finally_ctx
, stat
->finally_statement
);
1898 set_compiler_loc(ctx
, stat
->finally_loc
);
1899 if(!push_instr(ctx
, OP_end_finally
))
1900 return E_OUTOFMEMORY
;
1903 instr_ptr(ctx
, pop_except
)->u
.arg
[0].uint
= ctx
->code_off
;
1904 if(catch_pop_except
)
1905 instr_ptr(ctx
, catch_pop_except
)->u
.arg
[0].uint
= ctx
->code_off
;
1906 instr_ptr(ctx
, push_except
)->u
.arg
[0].uint
= catch_off
;
1907 instr_ptr(ctx
, push_except
)->u
.arg
[1].uint
= finally_off
;
1911 static HRESULT
compile_statement(compiler_ctx_t
*ctx
, statement_ctx_t
*stat_ctx
, statement_t
*stat
)
1915 push_compiler_statement_ctx(ctx
, stat_ctx
);
1917 set_compiler_loc(ctx
, stat
->loc
);
1919 switch(stat
->type
) {
1921 hres
= compile_block_statement(ctx
, (block_statement_t
*)stat
, ((block_statement_t
*)stat
)->stat_list
);
1924 hres
= compile_break_statement(ctx
, (branch_statement_t
*)stat
);
1927 hres
= compile_continue_statement(ctx
, (branch_statement_t
*)stat
);
1934 hres
= compile_expression_statement(ctx
, (expression_statement_t
*)stat
);
1937 hres
= compile_for_statement(ctx
, (for_statement_t
*)stat
);
1940 hres
= compile_forin_statement(ctx
, (forin_statement_t
*)stat
);
1943 hres
= compile_if_statement(ctx
, (if_statement_t
*)stat
);
1946 hres
= compile_labelled_statement(ctx
, (labelled_statement_t
*)stat
);
1949 hres
= compile_return_statement(ctx
, (expression_statement_t
*)stat
);
1952 hres
= compile_switch_statement(ctx
, (switch_statement_t
*)stat
);
1955 hres
= compile_throw_statement(ctx
, (expression_statement_t
*)stat
);
1958 hres
= compile_try_statement(ctx
, (try_statement_t
*)stat
);
1961 hres
= compile_var_statement(ctx
, (var_statement_t
*)stat
);
1964 hres
= compile_while_statement(ctx
, (while_statement_t
*)stat
);
1967 hres
= compile_with_statement(ctx
, (with_statement_t
*)stat
);
1969 DEFAULT_UNREACHABLE
;
1972 pop_compiler_statement_ctx(ctx
, stat_ctx
);
1977 static inline function_local_t
*find_local(compiler_ctx_t
*ctx
, const WCHAR
*name
, unsigned int scope
)
1979 struct wine_rb_entry
*entry
= wine_rb_get(&ctx
->local_scopes
[scope
].locals
, name
);
1980 return entry
? WINE_RB_ENTRY_VALUE(entry
, function_local_t
, entry
) : NULL
;
1983 static BOOL
alloc_local(compiler_ctx_t
*ctx
, BSTR name
, int ref
, unsigned int scope
)
1985 function_local_t
*local
;
1987 local
= heap_pool_alloc(&ctx
->heap
, sizeof(*local
));
1993 wine_rb_put(&ctx
->local_scopes
[scope
].locals
, name
, &local
->entry
);
1994 ctx
->local_scopes
[scope
].locals_cnt
++;
1998 static BOOL
alloc_variable(compiler_ctx_t
*ctx
, const WCHAR
*name
, unsigned int scope
)
2002 if(find_local(ctx
, name
, scope
))
2005 ident
= compiler_alloc_bstr(ctx
, name
);
2009 return alloc_local(ctx
, ident
, ctx
->func
->var_cnt
++, scope
);
2012 static HRESULT
visit_function_expression(compiler_ctx_t
*ctx
, function_expression_t
*expr
)
2014 statement_ctx_t
*stat_ctx
;
2016 expr
->func_id
= ctx
->func
->func_cnt
++;
2017 ctx
->func_tail
= ctx
->func_tail
? (ctx
->func_tail
->next
= expr
) : (ctx
->func_head
= expr
);
2019 if(!expr
->identifier
|| expr
->event_target
)
2022 for (stat_ctx
= ctx
->stat_ctx
; stat_ctx
; stat_ctx
= stat_ctx
->next
)
2024 if (stat_ctx
->block_scope
)
2026 stat_ctx
->scope_has_functions
= TRUE
;
2031 if(!expr
->is_statement
&& ctx
->parser
->script
->version
>= SCRIPTLANGUAGEVERSION_ES5
)
2034 return alloc_variable(ctx
, expr
->identifier
, stat_ctx
? stat_ctx
->scope_index
: 0) ? S_OK
: E_OUTOFMEMORY
;
2037 static HRESULT
visit_expression(compiler_ctx_t
*ctx
, expression_t
*expr
)
2039 HRESULT hres
= S_OK
;
2041 switch(expr
->type
) {
2046 case EXPR_ASSIGNADD
:
2047 case EXPR_ASSIGNAND
:
2048 case EXPR_ASSIGNSUB
:
2049 case EXPR_ASSIGNMUL
:
2050 case EXPR_ASSIGNDIV
:
2051 case EXPR_ASSIGNMOD
:
2053 case EXPR_ASSIGNLSHIFT
:
2054 case EXPR_ASSIGNRSHIFT
:
2055 case EXPR_ASSIGNRRSHIFT
:
2056 case EXPR_ASSIGNXOR
:
2064 case EXPR_GREATEREQ
:
2066 case EXPR_INSTANCEOF
:
2079 binary_expression_t
*binary_expr
= (binary_expression_t
*)expr
;
2081 hres
= visit_expression(ctx
, binary_expr
->expression1
);
2085 hres
= visit_expression(ctx
, binary_expr
->expression2
);
2099 hres
= visit_expression(ctx
, ((unary_expression_t
*)expr
)->expression
);
2105 case EXPR_ARRAYLIT
: {
2106 array_literal_expression_t
*array_expr
= (array_literal_expression_t
*)expr
;
2107 array_element_t
*iter
;
2109 for(iter
= array_expr
->element_list
; iter
; iter
= iter
->next
) {
2110 hres
= visit_expression(ctx
, iter
->expr
);
2118 call_expression_t
*call_expr
= (call_expression_t
*)expr
;
2121 hres
= visit_expression(ctx
, call_expr
->expression
);
2125 for(arg
= call_expr
->argument_list
; arg
; arg
= arg
->next
) {
2126 hres
= visit_expression(ctx
, arg
->expr
);
2133 conditional_expression_t
*cond_expr
= (conditional_expression_t
*)expr
;
2135 hres
= visit_expression(ctx
, cond_expr
->expression
);
2139 hres
= visit_expression(ctx
, cond_expr
->true_expression
);
2143 hres
= visit_expression(ctx
, cond_expr
->false_expression
);
2147 hres
= visit_function_expression(ctx
, (function_expression_t
*)expr
);
2150 hres
= visit_expression(ctx
, ((member_expression_t
*)expr
)->expression
);
2152 case EXPR_PROPVAL
: {
2153 property_definition_t
*iter
;
2154 for(iter
= ((property_value_expression_t
*)expr
)->property_list
; iter
; iter
= iter
->next
) {
2155 hres
= visit_expression(ctx
, iter
->value
);
2161 DEFAULT_UNREACHABLE
;
2167 static HRESULT
visit_variable_list(compiler_ctx_t
*ctx
, variable_declaration_t
*list
)
2169 variable_declaration_t
*iter
;
2170 statement_ctx_t
*stat_ctx
;
2173 for(iter
= list
; iter
; iter
= iter
->next
) {
2174 for (stat_ctx
= ctx
->stat_ctx
; stat_ctx
; stat_ctx
= stat_ctx
->next
)
2176 if (stat_ctx
->block_scope
)
2180 if(!alloc_variable(ctx
, iter
->identifier
, iter
->block_scope
&& stat_ctx
? stat_ctx
->scope_index
: 0))
2181 return E_OUTOFMEMORY
;
2184 hres
= visit_expression(ctx
, iter
->expr
);
2193 static HRESULT
visit_statement(compiler_ctx_t
*,statement_ctx_t
*,statement_t
*);
2195 static HRESULT
visit_block_statement(compiler_ctx_t
*ctx
, block_statement_t
*block
, statement_t
*iter
)
2197 statement_ctx_t stat_ctx
= {0, TRUE
};
2201 needs_scope
= block
&& ctx
->parser
->script
->version
>= SCRIPTLANGUAGEVERSION_ES5
;
2204 if (!alloc_local_scope(ctx
, &block
->scope_index
))
2205 return E_OUTOFMEMORY
;
2207 stat_ctx
.scope_index
= block
->scope_index
;
2208 stat_ctx
.block_scope
= TRUE
;
2212 hres
= visit_statement(ctx
, needs_scope
? &stat_ctx
: NULL
, iter
);
2219 if (needs_scope
&& !(ctx
->local_scopes
[stat_ctx
.scope_index
].locals_cnt
|| stat_ctx
.scope_has_functions
))
2220 remove_local_scope(ctx
, block
->scope_index
);
2225 static HRESULT
visit_statement(compiler_ctx_t
*ctx
, statement_ctx_t
*stat_ctx
, statement_t
*stat
)
2227 HRESULT hres
= S_OK
;
2229 push_compiler_statement_ctx(ctx
, stat_ctx
);
2231 switch(stat
->type
) {
2233 hres
= visit_block_statement(ctx
, (block_statement_t
*)stat
, ((block_statement_t
*)stat
)->stat_list
);
2240 expression_statement_t
*expr_stat
= (expression_statement_t
*)stat
;
2241 if(expr_stat
->expr
) {
2242 if(expr_stat
->expr
->type
== EXPR_FUNC
)
2243 ((function_expression_t
*)expr_stat
->expr
)->is_statement
= TRUE
;
2244 hres
= visit_expression(ctx
, expr_stat
->expr
);
2250 expression_statement_t
*expr_stat
= (expression_statement_t
*)stat
;
2252 hres
= visit_expression(ctx
, expr_stat
->expr
);
2256 statement_ctx_t stat_ctx_data
= {0, TRUE
}, *stat_ctx
= NULL
;
2257 for_statement_t
*for_stat
= (for_statement_t
*)stat
;
2259 if(for_stat
->variable_list
)
2261 variable_declaration_t
*var
;
2263 for(var
= for_stat
->variable_list
; var
; var
= var
->next
)
2265 if (var
->block_scope
)
2267 stat_ctx
= &stat_ctx_data
;
2274 if (!alloc_local_scope(ctx
, &for_stat
->scope_index
))
2276 hres
= E_OUTOFMEMORY
;
2279 stat_ctx
->scope_index
= for_stat
->scope_index
;
2280 stat_ctx
->block_scope
= TRUE
;
2281 push_compiler_statement_ctx(ctx
, stat_ctx
);
2283 hres
= visit_variable_list(ctx
, for_stat
->variable_list
);
2285 else if(for_stat
->begin_expr
)
2286 hres
= visit_expression(ctx
, for_stat
->begin_expr
);
2289 pop_compiler_statement_ctx(ctx
, stat_ctx
);
2293 if(for_stat
->expr
) {
2294 hres
= visit_expression(ctx
, for_stat
->expr
);
2297 pop_compiler_statement_ctx(ctx
, stat_ctx
);
2302 hres
= visit_statement(ctx
, NULL
, for_stat
->statement
);
2305 pop_compiler_statement_ctx(ctx
, stat_ctx
);
2308 if(for_stat
->end_expr
)
2309 hres
= visit_expression(ctx
, for_stat
->end_expr
);
2310 pop_compiler_statement_ctx(ctx
, stat_ctx
);
2314 forin_statement_t
*forin_stat
= (forin_statement_t
*)stat
;
2316 if(forin_stat
->variable
) {
2317 hres
= visit_variable_list(ctx
, forin_stat
->variable
);
2322 hres
= visit_expression(ctx
, forin_stat
->in_expr
);
2326 if(forin_stat
->expr
) {
2327 hres
= visit_expression(ctx
, forin_stat
->expr
);
2332 hres
= visit_statement(ctx
, NULL
, forin_stat
->statement
);
2336 if_statement_t
*if_stat
= (if_statement_t
*)stat
;
2338 hres
= visit_expression(ctx
, if_stat
->expr
);
2342 hres
= visit_statement(ctx
, NULL
, if_stat
->if_stat
);
2346 if(if_stat
->else_stat
)
2347 hres
= visit_statement(ctx
, NULL
, if_stat
->else_stat
);
2351 hres
= visit_statement(ctx
, NULL
, ((labelled_statement_t
*)stat
)->statement
);
2354 switch_statement_t
*switch_stat
= (switch_statement_t
*)stat
;
2355 statement_t
*stat_iter
;
2356 case_clausule_t
*iter
;
2358 hres
= visit_expression(ctx
, switch_stat
->expr
);
2362 for(iter
= switch_stat
->case_list
; iter
; iter
= iter
->next
) {
2365 hres
= visit_expression(ctx
, iter
->expr
);
2370 for(iter
= switch_stat
->case_list
; iter
; iter
= iter
->next
) {
2371 while(iter
->next
&& iter
->next
->stat
== iter
->stat
)
2373 for(stat_iter
= iter
->stat
; stat_iter
&& (!iter
->next
|| iter
->next
->stat
!= stat_iter
);
2374 stat_iter
= stat_iter
->next
) {
2375 hres
= visit_statement(ctx
, NULL
, stat_iter
);
2383 try_statement_t
*try_stat
= (try_statement_t
*)stat
;
2385 hres
= visit_statement(ctx
, NULL
, try_stat
->try_statement
);
2389 if(try_stat
->catch_block
) {
2390 hres
= visit_statement(ctx
, NULL
, try_stat
->catch_block
->statement
);
2395 if(try_stat
->finally_statement
)
2396 hres
= visit_statement(ctx
, NULL
, try_stat
->finally_statement
);
2400 hres
= visit_variable_list(ctx
, ((var_statement_t
*)stat
)->variable_list
);
2403 while_statement_t
*while_stat
= (while_statement_t
*)stat
;
2405 hres
= visit_expression(ctx
, while_stat
->expr
);
2409 hres
= visit_statement(ctx
, NULL
, while_stat
->statement
);
2413 with_statement_t
*with_stat
= (with_statement_t
*)stat
;
2415 hres
= visit_expression(ctx
, with_stat
->expr
);
2419 hres
= visit_statement(ctx
, NULL
, with_stat
->statement
);
2422 DEFAULT_UNREACHABLE
;
2425 pop_compiler_statement_ctx(ctx
, stat_ctx
);
2430 static void resolve_labels(compiler_ctx_t
*ctx
, unsigned off
)
2434 for(instr
= ctx
->code
->instrs
+off
; instr
< ctx
->code
->instrs
+ctx
->code_off
; instr
++) {
2435 if(instr_info
[instr
->op
].arg1_type
== ARG_ADDR
&& (instr
->u
.arg
->uint
& LABEL_FLAG
)) {
2436 assert((instr
->u
.arg
->uint
& ~LABEL_FLAG
) < ctx
->labels_cnt
);
2437 instr
->u
.arg
->uint
= ctx
->labels
[instr
->u
.arg
->uint
& ~LABEL_FLAG
];
2439 assert(instr_info
[instr
->op
].arg2_type
!= ARG_ADDR
);
2442 ctx
->labels_cnt
= 0;
2445 unsigned get_location_line(bytecode_t
*code
, unsigned loc
, unsigned *char_pos
)
2447 unsigned line
= code
->start_line
;
2448 const WCHAR
*nl
, *p
;
2450 for(nl
= p
= code
->source
; p
< code
->source
+ loc
; p
++) {
2451 if(*p
!= '\n') continue;
2455 *char_pos
= loc
- (nl
- code
->source
);
2459 void release_bytecode(bytecode_t
*code
)
2466 for(i
=0; i
< code
->bstr_cnt
; i
++)
2467 SysFreeString(code
->bstr_pool
[i
]);
2468 for(i
=0; i
< code
->str_cnt
; i
++)
2469 jsstr_release(code
->str_pool
[i
]);
2471 if(code
->named_item
)
2472 release_named_item(code
->named_item
);
2474 heap_pool_free(&code
->heap
);
2475 free(code
->bstr_pool
);
2476 free(code
->str_pool
);
2481 static HRESULT
init_code(compiler_ctx_t
*compiler
, const WCHAR
*source
, UINT64 source_context
, unsigned start_line
)
2483 size_t len
= source
? lstrlenW(source
) : 0;
2486 return E_OUTOFMEMORY
;
2488 compiler
->code
= calloc(1, sizeof(bytecode_t
));
2490 return E_OUTOFMEMORY
;
2492 compiler
->code
->ref
= 1;
2493 compiler
->code
->source_context
= source_context
;
2494 compiler
->code
->start_line
= start_line
;
2495 heap_pool_init(&compiler
->code
->heap
);
2497 compiler
->code
->source
= malloc((len
+ 1) * sizeof(WCHAR
));
2498 if(!compiler
->code
->source
) {
2499 release_bytecode(compiler
->code
);
2500 return E_OUTOFMEMORY
;
2503 memcpy(compiler
->code
->source
, source
, len
* sizeof(WCHAR
));
2504 compiler
->code
->source
[len
] = 0;
2506 compiler
->code
->instrs
= malloc(64 * sizeof(instr_t
));
2507 if(!compiler
->code
->instrs
) {
2508 release_bytecode(compiler
->code
);
2509 return E_OUTOFMEMORY
;
2512 compiler
->code_size
= 64;
2513 compiler
->code_off
= 1;
2517 static HRESULT
compile_function(compiler_ctx_t
*ctx
, statement_t
*source
, function_expression_t
*func_expr
,
2518 BOOL from_eval
, function_code_t
*func
)
2520 function_expression_t
*iter
;
2521 function_local_t
*local
;
2522 unsigned off
, i
, scope
;
2527 func
->bytecode
= ctx
->code
;
2528 func
->local_ref
= INVALID_LOCAL_REF
;
2529 func
->scope_index
= 0;
2530 ctx
->func_head
= ctx
->func_tail
= NULL
;
2531 ctx
->from_eval
= from_eval
;
2533 ctx
->local_scope_count
= 0;
2534 if (!alloc_local_scope(ctx
, &scope
))
2535 return E_OUTOFMEMORY
;
2539 parameter_t
*param_iter
;
2541 if(func_expr
->identifier
) {
2542 func
->name
= compiler_alloc_bstr(ctx
, func_expr
->identifier
);
2544 return E_OUTOFMEMORY
;
2547 if(func_expr
->event_target
) {
2548 func
->event_target
= compiler_alloc_bstr(ctx
, func_expr
->event_target
);
2549 if(!func
->event_target
)
2550 return E_OUTOFMEMORY
;
2553 func
->source
= func_expr
->src_str
;
2554 func
->source_len
= func_expr
->src_len
;
2556 for(param_iter
= func_expr
->parameter_list
; param_iter
; param_iter
= param_iter
->next
)
2559 func
->params
= compiler_alloc(ctx
->code
, func
->param_cnt
* sizeof(*func
->params
));
2561 return E_OUTOFMEMORY
;
2563 for(param_iter
= func_expr
->parameter_list
, i
=0; param_iter
; param_iter
= param_iter
->next
, i
++) {
2564 func
->params
[i
] = compiler_alloc_bstr(ctx
, param_iter
->identifier
);
2565 if(!func
->params
[i
])
2566 return E_OUTOFMEMORY
;
2570 for(i
= func
->param_cnt
; i
--;) {
2571 if(!find_local(ctx
, func
->params
[i
], 0) && !alloc_local(ctx
, func
->params
[i
], -i
-1, 0))
2572 return E_OUTOFMEMORY
;
2575 hres
= visit_block_statement(ctx
, NULL
, source
);
2579 func
->local_scope_count
= ctx
->local_scope_count
;
2580 func
->local_scopes
= compiler_alloc(ctx
->code
, func
->local_scope_count
* sizeof(*func
->local_scopes
));
2581 if(!func
->local_scopes
)
2582 return E_OUTOFMEMORY
;
2584 func
->variables
= compiler_alloc(ctx
->code
, func
->var_cnt
* sizeof(*func
->variables
));
2585 if(!func
->variables
)
2586 return E_OUTOFMEMORY
;
2588 for (scope
= 0; scope
< func
->local_scope_count
; ++scope
)
2590 func
->local_scopes
[scope
].locals
= compiler_alloc(ctx
->code
,
2591 ctx
->local_scopes
[scope
].locals_cnt
* sizeof(*func
->local_scopes
[scope
].locals
));
2592 if(!func
->local_scopes
[scope
].locals
)
2593 return E_OUTOFMEMORY
;
2594 func
->local_scopes
[scope
].locals_cnt
= ctx
->local_scopes
[scope
].locals_cnt
;
2597 WINE_RB_FOR_EACH_ENTRY(local
, &ctx
->local_scopes
[scope
].locals
, function_local_t
, entry
) {
2598 func
->local_scopes
[scope
].locals
[i
].name
= local
->name
;
2599 func
->local_scopes
[scope
].locals
[i
].ref
= local
->ref
;
2600 if(local
->ref
>= 0) {
2601 func
->variables
[local
->ref
].name
= local
->name
;
2602 func
->variables
[local
->ref
].func_id
= -1;
2606 assert(i
== ctx
->local_scopes
[scope
].locals_cnt
);
2609 func
->funcs
= compiler_alloc(ctx
->code
, func
->func_cnt
* sizeof(*func
->funcs
));
2611 return E_OUTOFMEMORY
;
2612 memset(func
->funcs
, 0, func
->func_cnt
* sizeof(*func
->funcs
));
2614 ctx
->current_function_expr
= ctx
->func_head
;
2615 off
= ctx
->code_off
;
2616 hres
= compile_block_statement(ctx
, NULL
, source
);
2620 resolve_labels(ctx
, off
);
2622 hres
= push_instr_uint(ctx
, OP_ret
, !from_eval
);
2626 if(TRACE_ON(jscript_disas
))
2627 dump_code(ctx
, off
);
2629 func
->instr_off
= off
;
2631 for(iter
= ctx
->func_head
, i
=0; iter
; iter
= iter
->next
, i
++) {
2632 hres
= compile_function(ctx
, iter
->statement_list
, iter
, FALSE
, func
->funcs
+i
);
2636 func
->funcs
[i
].scope_index
= iter
->scope_index
;
2638 TRACE("[%d] func %s, scope_index %u\n", i
, debugstr_w(func
->funcs
[i
].name
), iter
->scope_index
);
2639 if((ctx
->parser
->script
->version
< SCRIPTLANGUAGEVERSION_ES5
|| iter
->is_statement
) &&
2640 func
->funcs
[i
].name
&& !func
->funcs
[i
].event_target
) {
2641 local_ref_t
*local_ref
= lookup_local(func
, func
->funcs
[i
].name
, func
->funcs
[i
].scope_index
);
2643 func
->funcs
[i
].local_ref
= local_ref
->ref
;
2644 TRACE("found ref %s %d for %s\n", debugstr_w(local_ref
->name
), local_ref
->ref
, debugstr_w(func
->funcs
[i
].name
));
2645 if(local_ref
->ref
>= 0)
2646 func
->variables
[local_ref
->ref
].func_id
= i
;
2650 assert(i
== func
->func_cnt
);
2655 static HRESULT
parse_arguments(compiler_ctx_t
*ctx
, const WCHAR
*args
, BSTR
*arg_array
, unsigned *args_size
)
2657 const WCHAR
*ptr
= args
, *ptr2
;
2658 unsigned arg_cnt
= 0;
2660 while(iswspace(*ptr
))
2669 if(!iswalpha(*ptr
) && *ptr
!= '_') {
2670 FIXME("expected alpha or '_': %s\n", debugstr_w(ptr
));
2675 while(iswalnum(*ptr
) || *ptr
== '_')
2678 if(*ptr
&& *ptr
!= ',' && !iswspace(*ptr
)) {
2679 FIXME("unexpected har %s\n", debugstr_w(ptr
));
2684 arg_array
[arg_cnt
] = compiler_alloc_bstr_len(ctx
, ptr2
, ptr
-ptr2
);
2685 if(!arg_array
[arg_cnt
])
2686 return E_OUTOFMEMORY
;
2690 while(iswspace(*ptr
))
2695 FIXME("expected ',': %s\n", debugstr_w(ptr
));
2700 while(iswspace(*ptr
))
2705 *args_size
= arg_cnt
;
2709 static HRESULT
compile_arguments(compiler_ctx_t
*ctx
, const WCHAR
*args
)
2713 hres
= parse_arguments(ctx
, args
, NULL
, &ctx
->code
->global_code
.param_cnt
);
2717 ctx
->code
->global_code
.params
= compiler_alloc(ctx
->code
,
2718 ctx
->code
->global_code
.param_cnt
* sizeof(*ctx
->code
->global_code
.params
));
2719 if(!ctx
->code
->global_code
.params
)
2720 return E_OUTOFMEMORY
;
2722 return parse_arguments(ctx
, args
, ctx
->code
->global_code
.params
, NULL
);
2725 HRESULT
compile_script(script_ctx_t
*ctx
, const WCHAR
*code
, UINT64 source_context
, unsigned start_line
,
2726 const WCHAR
*args
, const WCHAR
*delimiter
, BOOL from_eval
, BOOL use_decode
,
2727 named_item_t
*named_item
, bytecode_t
**ret
)
2729 compiler_ctx_t compiler
= {0};
2732 hres
= init_code(&compiler
, code
, source_context
, start_line
);
2737 hres
= compile_arguments(&compiler
, args
);
2743 hres
= decode_source(compiler
.code
->source
);
2745 WARN("Decoding failed\n");
2750 hres
= script_parse(ctx
, &compiler
, compiler
.code
, delimiter
, from_eval
, &compiler
.parser
);
2752 release_bytecode(compiler
.code
);
2756 heap_pool_init(&compiler
.heap
);
2757 hres
= compile_function(&compiler
, compiler
.parser
->source
, NULL
, from_eval
, &compiler
.code
->global_code
);
2758 free(compiler
.local_scopes
);
2759 heap_pool_free(&compiler
.heap
);
2760 parser_release(compiler
.parser
);
2762 if(hres
!= DISP_E_EXCEPTION
)
2763 throw_error(ctx
, hres
, NULL
);
2764 set_error_location(ctx
->ei
, compiler
.code
, compiler
.loc
, IDS_COMPILATION_ERROR
, NULL
);
2765 release_bytecode(compiler
.code
);
2766 return DISP_E_EXCEPTION
;
2770 compiler
.code
->named_item
= named_item
;
2774 *ret
= compiler
.code
;