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_double(compiler_ctx_t
*ctx
, jsop_t op
, double arg
)
162 dbl
= compiler_alloc(ctx
->code
, sizeof(arg
));
164 return E_OUTOFMEMORY
;
167 instr
= push_instr(ctx
, op
);
169 return E_OUTOFMEMORY
;
171 instr_ptr(ctx
, instr
)->arg1
.dbl
= dbl
;
175 static HRESULT
push_instr_uint(compiler_ctx_t
*ctx
, jsop_t op
, unsigned arg
)
179 instr
= push_instr(ctx
, op
);
181 return E_OUTOFMEMORY
;
183 instr_ptr(ctx
, instr
)->arg1
.uint
= arg
;
187 static HRESULT
compile_binary_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
191 hres
= compile_expression(ctx
, expr
->expression1
);
195 hres
= compile_expression(ctx
, expr
->expression2
);
199 return push_instr(ctx
, op
) == -1 ? E_OUTOFMEMORY
: S_OK
;
202 static HRESULT
compile_unary_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
, jsop_t op
)
206 hres
= compile_expression(ctx
, expr
->expression
);
210 return push_instr(ctx
, op
) == -1 ? E_OUTOFMEMORY
: S_OK
;
213 /* ECMA-262 3rd Edition 11.14 */
214 static HRESULT
compile_comma_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
)
218 hres
= compile_expression(ctx
, expr
->expression1
);
222 if(push_instr(ctx
, OP_pop
) == -1)
223 return E_OUTOFMEMORY
;
225 return compile_expression(ctx
, expr
->expression2
);
228 /* ECMA-262 3rd Edition 11.11 */
229 static HRESULT
compile_logical_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
234 hres
= compile_expression(ctx
, expr
->expression1
);
238 instr
= push_instr(ctx
, op
);
240 return E_OUTOFMEMORY
;
242 hres
= compile_expression(ctx
, expr
->expression2
);
246 instr_ptr(ctx
, instr
)->arg1
.uint
= ctx
->code_off
;
250 /* ECMA-262 3rd Edition 11.12 */
251 static HRESULT
compile_conditional_expression(compiler_ctx_t
*ctx
, conditional_expression_t
*expr
)
253 unsigned jmp_false
, jmp_end
;
256 hres
= compile_expression(ctx
, expr
->expression
);
260 jmp_false
= push_instr(ctx
, OP_jmp_z
);
262 return E_OUTOFMEMORY
;
264 hres
= compile_expression(ctx
, expr
->true_expression
);
268 jmp_end
= push_instr(ctx
, OP_jmp
);
270 return E_OUTOFMEMORY
;
272 instr_ptr(ctx
, jmp_false
)->arg1
.uint
= ctx
->code_off
;
273 if(push_instr(ctx
, OP_pop
) == -1)
274 return E_OUTOFMEMORY
;
276 hres
= compile_expression(ctx
, expr
->false_expression
);
280 instr_ptr(ctx
, jmp_end
)->arg1
.uint
= ctx
->code_off
;
284 static HRESULT
compile_new_expression(compiler_ctx_t
*ctx
, call_expression_t
*expr
)
286 unsigned arg_cnt
= 0;
290 hres
= compile_expression(ctx
, expr
->expression
);
294 for(arg
= expr
->argument_list
; arg
; arg
= arg
->next
) {
295 hres
= compile_expression(ctx
, arg
->expr
);
301 return push_instr_int(ctx
, OP_new
, arg_cnt
);
304 static HRESULT
compile_interp_fallback(compiler_ctx_t
*ctx
, expression_t
*expr
)
308 instr
= push_instr(ctx
, OP_tree
);
310 return E_OUTOFMEMORY
;
312 instr_ptr(ctx
, instr
)->arg1
.expr
= expr
;
316 static HRESULT
compile_delete_expression(compiler_ctx_t
*ctx
, unary_expression_t
*expr
)
320 switch(expr
->expression
->type
) {
322 array_expression_t
*array_expr
= (array_expression_t
*)expr
->expression
;
324 hres
= compile_expression(ctx
, array_expr
->member_expr
);
328 hres
= compile_expression(ctx
, array_expr
->expression
);
332 if(push_instr(ctx
, OP_delete
) == -1)
333 return E_OUTOFMEMORY
;
337 member_expression_t
*member_expr
= (member_expression_t
*)expr
->expression
;
339 hres
= compile_expression(ctx
, member_expr
->expression
);
343 /* FIXME: Potential optimization */
344 hres
= push_instr_str(ctx
, OP_str
, member_expr
->identifier
);
348 if(push_instr(ctx
, OP_delete
) == -1)
349 return E_OUTOFMEMORY
;
353 expr
->expr
.eval
= delete_expression_eval
;
354 return compile_interp_fallback(ctx
, &expr
->expr
);
360 static HRESULT
compile_assign_expression(compiler_ctx_t
*ctx
, binary_expression_t
*expr
, jsop_t op
)
364 switch(expr
->expression1
->type
) {
366 identifier_expression_t
*ident_expr
= (identifier_expression_t
*)expr
->expression1
;
368 hres
= push_instr_bstr(ctx
, OP_identid
, ident_expr
->identifier
);
374 array_expression_t
*array_expr
= (array_expression_t
*)expr
->expression1
;
376 hres
= compile_expression(ctx
, array_expr
->member_expr
);
380 hres
= compile_expression(ctx
, array_expr
->expression
);
384 if(push_instr(ctx
, OP_memberid
) == -1)
385 return E_OUTOFMEMORY
;
389 member_expression_t
*member_expr
= (member_expression_t
*)expr
->expression1
;
391 hres
= compile_expression(ctx
, member_expr
->expression
);
395 /* FIXME: Potential optimization */
396 hres
= push_instr_str(ctx
, OP_str
, member_expr
->identifier
);
400 if(push_instr(ctx
, OP_memberid
) == -1)
401 return E_OUTOFMEMORY
;
405 hres
= compile_expression(ctx
, expr
->expression1
);
409 hres
= compile_expression(ctx
, expr
->expression2
);
413 if(op
!= OP_LAST
&& push_instr(ctx
, op
) == -1)
414 return E_OUTOFMEMORY
;
416 return push_instr_uint(ctx
, OP_throw
, JS_E_ILLEGAL_ASSIGN
);
419 if(op
!= OP_LAST
&& push_instr(ctx
, OP_refval
) == -1)
420 return E_OUTOFMEMORY
;
422 hres
= compile_expression(ctx
, expr
->expression2
);
426 if(op
!= OP_LAST
&& push_instr(ctx
, op
) == -1)
427 return E_OUTOFMEMORY
;
429 if(push_instr(ctx
, OP_assign
) == -1)
430 return E_OUTOFMEMORY
;
435 static HRESULT
compile_literal(compiler_ctx_t
*ctx
, literal_t
*literal
)
437 switch(literal
->type
) {
439 return push_instr_int(ctx
, OP_bool
, literal
->u
.bval
);
441 return push_instr_double(ctx
, OP_double
, literal
->u
.dval
);
443 return push_instr_int(ctx
, OP_int
, literal
->u
.lval
);
445 return push_instr(ctx
, OP_null
);
447 return push_instr_str(ctx
, OP_str
, literal
->u
.wstr
);
452 str
= compiler_alloc(ctx
->code
, (literal
->u
.regexp
.str_len
+1)*sizeof(WCHAR
));
454 return E_OUTOFMEMORY
;
455 memcpy(str
, literal
->u
.regexp
.str
, literal
->u
.regexp
.str_len
*sizeof(WCHAR
));
456 str
[literal
->u
.regexp
.str_len
] = 0;
458 instr
= push_instr(ctx
, OP_regexp
);
460 return E_OUTOFMEMORY
;
462 instr_ptr(ctx
, instr
)->arg1
.str
= str
;
463 instr_ptr(ctx
, instr
)->arg2
.lng
= literal
->u
.regexp
.flags
;
471 static HRESULT
compile_expression(compiler_ctx_t
*ctx
, expression_t
*expr
)
475 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_add
);
477 return compile_logical_expression(ctx
, (binary_expression_t
*)expr
, OP_jmp_z
);
479 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_LAST
);
481 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_add
);
483 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
485 return compile_assign_expression(ctx
, (binary_expression_t
*)expr
, OP_mul
);
487 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_bneg
);
489 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_or
);
491 return compile_comma_expression(ctx
, (binary_expression_t
*)expr
);
493 return compile_conditional_expression(ctx
, (conditional_expression_t
*)expr
);
495 return compile_delete_expression(ctx
, (unary_expression_t
*)expr
);
497 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_div
);
499 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_eq
);
501 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_eq2
);
503 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gt
);
505 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gteq
);
507 return push_instr_bstr(ctx
, OP_ident
, ((identifier_expression_t
*)expr
)->identifier
);
509 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_in
);
511 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lt
);
513 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lteq
);
515 return compile_literal(ctx
, ((literal_expression_t
*)expr
)->literal
);
517 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_neg
);
519 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_minus
);
521 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mod
);
523 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mul
);
525 return compile_new_expression(ctx
, (call_expression_t
*)expr
);
527 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_neq
);
529 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_neq2
);
531 return compile_logical_expression(ctx
, (binary_expression_t
*)expr
, OP_jmp_nz
);
533 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_tonum
);
535 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
537 return push_instr(ctx
, OP_this
) == -1 ? E_OUTOFMEMORY
: S_OK
;
539 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_void
);
541 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_xor
);
543 assert(expr
->eval
!= compiled_expression_eval
);
544 return compile_interp_fallback(ctx
, expr
);
550 void release_bytecode(bytecode_t
*code
)
554 for(i
=0; i
< code
->bstr_cnt
; i
++)
555 SysFreeString(code
->bstr_pool
[i
]);
557 jsheap_free(&code
->heap
);
558 heap_free(code
->bstr_pool
);
559 heap_free(code
->instrs
);
563 void release_compiler(compiler_ctx_t
*ctx
)
568 HRESULT
compile_subscript(parser_ctx_t
*parser
, expression_t
*expr
, unsigned *ret_off
)
573 parser
->code
= heap_alloc_zero(sizeof(bytecode_t
));
575 return E_OUTOFMEMORY
;
576 jsheap_init(&parser
->code
->heap
);
579 if(!parser
->compiler
) {
580 parser
->compiler
= heap_alloc_zero(sizeof(compiler_ctx_t
));
581 if(!parser
->compiler
)
582 return E_OUTOFMEMORY
;
584 parser
->compiler
->parser
= parser
;
585 parser
->compiler
->code
= parser
->code
;
588 *ret_off
= parser
->compiler
->code_off
;
589 hres
= compile_expression(parser
->compiler
, expr
);
593 return push_instr(parser
->compiler
, OP_ret
) == -1 ? E_OUTOFMEMORY
: S_OK
;