2 * Copyright 2011 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
29 struct _compiler_ctx_t
{
37 static HRESULT
compile_expression(compiler_ctx_t
*,expression_t
*);
39 static inline void *compiler_alloc(bytecode_t
*code
, size_t size
)
41 return jsheap_alloc(&code
->heap
, size
);
44 static WCHAR
*compiler_alloc_string(bytecode_t
*code
, const WCHAR
*str
)
49 size
= (strlenW(str
)+1)*sizeof(WCHAR
);
50 ret
= compiler_alloc(code
, size
);
52 memcpy(ret
, str
, size
);
56 static BSTR
compiler_alloc_bstr(compiler_ctx_t
*ctx
, const WCHAR
*str
)
58 if(!ctx
->code
->bstr_pool_size
) {
59 ctx
->code
->bstr_pool
= heap_alloc(8 * sizeof(BSTR
));
60 if(!ctx
->code
->bstr_pool
)
62 ctx
->code
->bstr_pool_size
= 8;
63 }else if(ctx
->code
->bstr_pool_size
== ctx
->code
->bstr_cnt
) {
66 new_pool
= heap_realloc(ctx
->code
->bstr_pool
, ctx
->code
->bstr_pool_size
*2*sizeof(BSTR
));
70 ctx
->code
->bstr_pool
= new_pool
;
71 ctx
->code
->bstr_pool_size
*= 2;
74 ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
] = SysAllocString(str
);
75 if(!ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
])
78 return ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
++];
81 static unsigned push_instr(compiler_ctx_t
*ctx
, jsop_t op
)
83 assert(ctx
->code_size
>= ctx
->code_off
);
86 ctx
->code
->instrs
= heap_alloc(64 * sizeof(instr_t
));
87 if(!ctx
->code
->instrs
)
90 }else if(ctx
->code_size
== ctx
->code_off
) {
93 new_instrs
= heap_realloc(ctx
->code
->instrs
, ctx
->code_size
*2*sizeof(instr_t
));
97 ctx
->code
->instrs
= new_instrs
;
101 ctx
->code
->instrs
[ctx
->code_off
].op
= op
;
102 return ctx
->code_off
++;
105 static inline instr_t
*instr_ptr(compiler_ctx_t
*ctx
, unsigned off
)
107 assert(off
< ctx
->code_off
);
108 return ctx
->code
->instrs
+ off
;
111 static HRESULT
push_instr_int(compiler_ctx_t
*ctx
, jsop_t op
, LONG arg
)
115 instr
= push_instr(ctx
, op
);
117 return E_OUTOFMEMORY
;
119 instr_ptr(ctx
, instr
)->arg1
.lng
= arg
;
123 static HRESULT
push_instr_str(compiler_ctx_t
*ctx
, jsop_t op
, const WCHAR
*arg
)
128 str
= compiler_alloc_string(ctx
->code
, arg
);
130 return E_OUTOFMEMORY
;
132 instr
= push_instr(ctx
, op
);
134 return E_OUTOFMEMORY
;
136 instr_ptr(ctx
, instr
)->arg1
.str
= str
;
140 static HRESULT
push_instr_bstr(compiler_ctx_t
*ctx
, jsop_t op
, const WCHAR
*arg
)
145 str
= compiler_alloc_bstr(ctx
, arg
);
147 return E_OUTOFMEMORY
;
149 instr
= push_instr(ctx
, op
);
151 return E_OUTOFMEMORY
;
153 instr_ptr(ctx
, instr
)->arg1
.bstr
= str
;
157 static HRESULT
push_instr_bstr_uint(compiler_ctx_t
*ctx
, jsop_t op
, const WCHAR
*arg1
, unsigned arg2
)
162 str
= compiler_alloc_bstr(ctx
, arg1
);
164 return E_OUTOFMEMORY
;
166 instr
= push_instr(ctx
, op
);
168 return E_OUTOFMEMORY
;
170 instr_ptr(ctx
, instr
)->arg1
.bstr
= str
;
171 instr_ptr(ctx
, instr
)->arg2
.uint
= arg2
;
175 static HRESULT
push_instr_double(compiler_ctx_t
*ctx
, jsop_t op
, double arg
)
180 dbl
= compiler_alloc(ctx
->code
, sizeof(arg
));
182 return E_OUTOFMEMORY
;
185 instr
= push_instr(ctx
, op
);
187 return E_OUTOFMEMORY
;
189 instr_ptr(ctx
, instr
)->arg1
.dbl
= dbl
;
193 static HRESULT
push_instr_uint(compiler_ctx_t
*ctx
, jsop_t op
, unsigned arg
)
197 instr
= push_instr(ctx
, op
);
199 return E_OUTOFMEMORY
;
201 instr_ptr(ctx
, instr
)->arg1
.uint
= arg
;
205 static HRESULT
compile_binary_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
209 hres
= compile_expression(ctx
, expr
->expression1
);
213 hres
= compile_expression(ctx
, expr
->expression2
);
217 return push_instr(ctx
, op
) == -1 ? E_OUTOFMEMORY
: S_OK
;
220 static HRESULT
compile_unary_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
, jsop_t op
)
224 hres
= compile_expression(ctx
, expr
->expression
);
228 return push_instr(ctx
, op
) == -1 ? E_OUTOFMEMORY
: S_OK
;
231 /* ECMA-262 3rd Edition 11.2.1 */
232 static HRESULT
compile_member_expression(compiler_ctx_t
*ctx
, member_expression_t
*expr
)
236 hres
= compile_expression(ctx
, expr
->expression
);
240 return push_instr_bstr(ctx
, OP_member
, expr
->identifier
);
243 static inline BOOL
is_memberid_expr(expression_type_t type
)
245 return type
== EXPR_IDENT
|| type
== EXPR_MEMBER
|| type
== EXPR_ARRAY
;
248 static HRESULT
compile_memberid_expression(compiler_ctx_t
*ctx
, expression_t
*expr
, unsigned flags
)
254 identifier_expression_t
*ident_expr
= (identifier_expression_t
*)expr
;
256 hres
= push_instr_bstr_uint(ctx
, OP_identid
, ident_expr
->identifier
, flags
);
260 array_expression_t
*array_expr
= (array_expression_t
*)expr
;
262 hres
= compile_expression(ctx
, array_expr
->member_expr
);
266 hres
= compile_expression(ctx
, array_expr
->expression
);
270 hres
= push_instr_uint(ctx
, OP_memberid
, flags
);
274 member_expression_t
*member_expr
= (member_expression_t
*)expr
;
276 hres
= compile_expression(ctx
, member_expr
->expression
);
280 /* FIXME: Potential optimization */
281 hres
= push_instr_str(ctx
, OP_str
, member_expr
->identifier
);
285 hres
= push_instr_uint(ctx
, OP_memberid
, flags
);
295 /* ECMA-262 3rd Edition 11.14 */
296 static HRESULT
compile_comma_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
)
300 hres
= compile_expression(ctx
, expr
->expression1
);
304 if(push_instr(ctx
, OP_pop
) == -1)
305 return E_OUTOFMEMORY
;
307 return compile_expression(ctx
, expr
->expression2
);
310 /* ECMA-262 3rd Edition 11.11 */
311 static HRESULT
compile_logical_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
316 hres
= compile_expression(ctx
, expr
->expression1
);
320 instr
= push_instr(ctx
, op
);
322 return E_OUTOFMEMORY
;
324 hres
= compile_expression(ctx
, expr
->expression2
);
328 instr_ptr(ctx
, instr
)->arg1
.uint
= ctx
->code_off
;
332 /* ECMA-262 3rd Edition 11.12 */
333 static HRESULT
compile_conditional_expression(compiler_ctx_t
*ctx
, conditional_expression_t
*expr
)
335 unsigned jmp_false
, jmp_end
;
338 hres
= compile_expression(ctx
, expr
->expression
);
342 jmp_false
= push_instr(ctx
, OP_jmp_z
);
344 return E_OUTOFMEMORY
;
346 hres
= compile_expression(ctx
, expr
->true_expression
);
350 jmp_end
= push_instr(ctx
, OP_jmp
);
352 return E_OUTOFMEMORY
;
354 instr_ptr(ctx
, jmp_false
)->arg1
.uint
= ctx
->code_off
;
355 if(push_instr(ctx
, OP_pop
) == -1)
356 return E_OUTOFMEMORY
;
358 hres
= compile_expression(ctx
, expr
->false_expression
);
362 instr_ptr(ctx
, jmp_end
)->arg1
.uint
= ctx
->code_off
;
366 static HRESULT
compile_new_expression(compiler_ctx_t
*ctx
, call_expression_t
*expr
)
368 unsigned arg_cnt
= 0;
372 hres
= compile_expression(ctx
, expr
->expression
);
376 for(arg
= expr
->argument_list
; arg
; arg
= arg
->next
) {
377 hres
= compile_expression(ctx
, arg
->expr
);
383 return push_instr_int(ctx
, OP_new
, arg_cnt
);
386 static HRESULT
compile_interp_fallback(compiler_ctx_t
*ctx
, expression_t
*expr
)
390 instr
= push_instr(ctx
, OP_tree
);
392 return E_OUTOFMEMORY
;
394 instr_ptr(ctx
, instr
)->arg1
.expr
= expr
;
398 static HRESULT
compile_call_expression(compiler_ctx_t
*ctx
, call_expression_t
*expr
, BOOL
*no_ret
)
400 unsigned arg_cnt
= 0;
406 if(is_memberid_expr(expr
->expression
->type
)) {
408 hres
= compile_memberid_expression(ctx
, expr
->expression
, 0);
411 hres
= compile_expression(ctx
, expr
->expression
);
417 for(arg
= expr
->argument_list
; arg
; arg
= arg
->next
) {
418 hres
= compile_expression(ctx
, arg
->expr
);
424 instr
= push_instr(ctx
, op
);
426 return E_OUTOFMEMORY
;
428 instr_ptr(ctx
, instr
)->arg1
.uint
= arg_cnt
;
429 instr_ptr(ctx
, instr
)->arg2
.lng
= no_ret
== NULL
;
435 static HRESULT
compile_delete_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
)
439 switch(expr
->expression
->type
) {
441 array_expression_t
*array_expr
= (array_expression_t
*)expr
->expression
;
443 hres
= compile_expression(ctx
, array_expr
->member_expr
);
447 hres
= compile_expression(ctx
, array_expr
->expression
);
451 if(push_instr(ctx
, OP_delete
) == -1)
452 return E_OUTOFMEMORY
;
456 member_expression_t
*member_expr
= (member_expression_t
*)expr
->expression
;
458 hres
= compile_expression(ctx
, member_expr
->expression
);
462 /* FIXME: Potential optimization */
463 hres
= push_instr_str(ctx
, OP_str
, member_expr
->identifier
);
467 if(push_instr(ctx
, OP_delete
) == -1)
468 return E_OUTOFMEMORY
;
472 expr
->expr
.eval
= delete_expression_eval
;
473 return compile_interp_fallback(ctx
, &expr
->expr
);
479 static HRESULT
compile_assign_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
483 if(!is_memberid_expr(expr
->expression1
->type
)) {
484 hres
= compile_expression(ctx
, expr
->expression1
);
488 hres
= compile_expression(ctx
, expr
->expression2
);
492 if(op
!= OP_LAST
&& push_instr(ctx
, op
) == -1)
493 return E_OUTOFMEMORY
;
495 return push_instr_uint(ctx
, OP_throw
, JS_E_ILLEGAL_ASSIGN
);
498 hres
= compile_memberid_expression(ctx
, expr
->expression1
, fdexNameEnsure
);
502 if(op
!= OP_LAST
&& push_instr(ctx
, OP_refval
) == -1)
503 return E_OUTOFMEMORY
;
505 hres
= compile_expression(ctx
, expr
->expression2
);
509 if(op
!= OP_LAST
&& push_instr(ctx
, op
) == -1)
510 return E_OUTOFMEMORY
;
512 if(push_instr(ctx
, OP_assign
) == -1)
513 return E_OUTOFMEMORY
;
518 static HRESULT
compile_literal(compiler_ctx_t
*ctx
, literal_t
*literal
)
520 switch(literal
->type
) {
522 return push_instr_int(ctx
, OP_bool
, literal
->u
.bval
);
524 return push_instr_double(ctx
, OP_double
, literal
->u
.dval
);
526 return push_instr_int(ctx
, OP_int
, literal
->u
.lval
);
528 return push_instr(ctx
, OP_null
);
530 return push_instr_str(ctx
, OP_str
, literal
->u
.wstr
);
535 str
= compiler_alloc(ctx
->code
, (literal
->u
.regexp
.str_len
+1)*sizeof(WCHAR
));
537 return E_OUTOFMEMORY
;
538 memcpy(str
, literal
->u
.regexp
.str
, literal
->u
.regexp
.str_len
*sizeof(WCHAR
));
539 str
[literal
->u
.regexp
.str_len
] = 0;
541 instr
= push_instr(ctx
, OP_regexp
);
543 return E_OUTOFMEMORY
;
545 instr_ptr(ctx
, instr
)->arg1
.str
= str
;
546 instr_ptr(ctx
, instr
)->arg2
.lng
= literal
->u
.regexp
.flags
;
554 static HRESULT
compile_expression_noret(compiler_ctx_t
*ctx
, expression_t
*expr
, BOOL
*no_ret
)
558 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_add
);
560 return compile_logical_expression(ctx
, (binary_expression_t
*)expr
, OP_jmp_z
);
562 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_LAST
);
564 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_add
);
566 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
568 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_mul
);
570 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_div
);
572 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_mod
);
574 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_or
);
576 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_xor
);
578 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_bneg
);
580 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_or
);
582 return compile_call_expression(ctx
, (call_expression_t
*)expr
, no_ret
);
584 return compile_comma_expression(ctx
, (binary_expression_t
*)expr
);
586 return compile_conditional_expression(ctx
, (conditional_expression_t
*)expr
);
588 return compile_delete_expression(ctx
, (unary_expression_t
*)expr
);
590 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_div
);
592 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_eq
);
594 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_eq2
);
596 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gt
);
598 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gteq
);
600 return push_instr_bstr(ctx
, OP_ident
, ((identifier_expression_t
*)expr
)->identifier
);
602 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_in
);
604 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lt
);
606 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lteq
);
608 return compile_literal(ctx
, ((literal_expression_t
*)expr
)->literal
);
610 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_neg
);
612 return compile_member_expression(ctx
, (member_expression_t
*)expr
);
614 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_minus
);
616 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mod
);
618 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mul
);
620 return compile_new_expression(ctx
, (call_expression_t
*)expr
);
622 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_neq
);
624 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_neq2
);
626 return compile_logical_expression(ctx
, (binary_expression_t
*)expr
, OP_jmp_nz
);
628 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_tonum
);
630 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
632 return push_instr(ctx
, OP_this
) == -1 ? E_OUTOFMEMORY
: S_OK
;
634 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_void
);
636 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_xor
);
638 assert(expr
->eval
!= compiled_expression_eval
);
639 return compile_interp_fallback(ctx
, expr
);
645 static HRESULT
compile_expression(compiler_ctx_t
*ctx
, expression_t
*expr
)
647 return compile_expression_noret(ctx
, expr
, NULL
);
650 void release_bytecode(bytecode_t
*code
)
654 for(i
=0; i
< code
->bstr_cnt
; i
++)
655 SysFreeString(code
->bstr_pool
[i
]);
657 jsheap_free(&code
->heap
);
658 heap_free(code
->bstr_pool
);
659 heap_free(code
->instrs
);
663 void release_compiler(compiler_ctx_t
*ctx
)
668 HRESULT
compile_subscript(parser_ctx_t
*parser
, expression_t
*expr
, BOOL do_ret
, unsigned *ret_off
)
674 parser
->code
= heap_alloc_zero(sizeof(bytecode_t
));
676 return E_OUTOFMEMORY
;
677 jsheap_init(&parser
->code
->heap
);
680 if(!parser
->compiler
) {
681 parser
->compiler
= heap_alloc_zero(sizeof(compiler_ctx_t
));
682 if(!parser
->compiler
)
683 return E_OUTOFMEMORY
;
685 parser
->compiler
->parser
= parser
;
686 parser
->compiler
->code
= parser
->code
;
689 *ret_off
= parser
->compiler
->code_off
;
690 hres
= compile_expression_noret(parser
->compiler
, expr
, do_ret
? NULL
: &no_ret
);
694 return push_instr(parser
->compiler
, OP_ret
) == -1 ? E_OUTOFMEMORY
: S_OK
;