jscript: Use bytecode for all call expressions.
[wine/multimedia.git] / dlls / jscript / compile.c
bloba476155276eb6e7f6c1361bbcd7ab756bfc774c8
1 /*
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
19 #include <math.h>
20 #include <assert.h>
22 #include "jscript.h"
23 #include "engine.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
29 struct _compiler_ctx_t {
30 parser_ctx_t *parser;
31 bytecode_t *code;
33 unsigned code_off;
34 unsigned code_size;
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)
46 size_t size;
47 WCHAR *ret;
49 size = (strlenW(str)+1)*sizeof(WCHAR);
50 ret = compiler_alloc(code, size);
51 if(ret)
52 memcpy(ret, str, size);
53 return ret;
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)
61 return NULL;
62 ctx->code->bstr_pool_size = 8;
63 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
64 BSTR *new_pool;
66 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
67 if(!new_pool)
68 return NULL;
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])
76 return NULL;
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);
85 if(!ctx->code_size) {
86 ctx->code->instrs = heap_alloc(64 * sizeof(instr_t));
87 if(!ctx->code->instrs)
88 return -1;
89 ctx->code_size = 64;
90 }else if(ctx->code_size == ctx->code_off) {
91 instr_t *new_instrs;
93 new_instrs = heap_realloc(ctx->code->instrs, ctx->code_size*2*sizeof(instr_t));
94 if(!new_instrs)
95 return -1;
97 ctx->code->instrs = new_instrs;
98 ctx->code_size *= 2;
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)
113 unsigned instr;
115 instr = push_instr(ctx, op);
116 if(instr == -1)
117 return E_OUTOFMEMORY;
119 instr_ptr(ctx, instr)->arg1.lng = arg;
120 return S_OK;
123 static HRESULT push_instr_str(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
125 unsigned instr;
126 WCHAR *str;
128 str = compiler_alloc_string(ctx->code, arg);
129 if(!str)
130 return E_OUTOFMEMORY;
132 instr = push_instr(ctx, op);
133 if(instr == -1)
134 return E_OUTOFMEMORY;
136 instr_ptr(ctx, instr)->arg1.str = str;
137 return S_OK;
140 static HRESULT push_instr_bstr(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
142 unsigned instr;
143 WCHAR *str;
145 str = compiler_alloc_bstr(ctx, arg);
146 if(!str)
147 return E_OUTOFMEMORY;
149 instr = push_instr(ctx, op);
150 if(instr == -1)
151 return E_OUTOFMEMORY;
153 instr_ptr(ctx, instr)->arg1.bstr = str;
154 return S_OK;
157 static HRESULT push_instr_bstr_uint(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg1, unsigned arg2)
159 unsigned instr;
160 WCHAR *str;
162 str = compiler_alloc_bstr(ctx, arg1);
163 if(!str)
164 return E_OUTOFMEMORY;
166 instr = push_instr(ctx, op);
167 if(instr == -1)
168 return E_OUTOFMEMORY;
170 instr_ptr(ctx, instr)->arg1.bstr = str;
171 instr_ptr(ctx, instr)->arg2.uint = arg2;
172 return S_OK;
175 static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
177 unsigned instr;
178 DOUBLE *dbl;
180 dbl = compiler_alloc(ctx->code, sizeof(arg));
181 if(!dbl)
182 return E_OUTOFMEMORY;
183 *dbl = arg;
185 instr = push_instr(ctx, op);
186 if(instr == -1)
187 return E_OUTOFMEMORY;
189 instr_ptr(ctx, instr)->arg1.dbl = dbl;
190 return S_OK;
193 static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
195 unsigned instr;
197 instr = push_instr(ctx, op);
198 if(instr == -1)
199 return E_OUTOFMEMORY;
201 instr_ptr(ctx, instr)->arg1.uint = arg;
202 return S_OK;
205 static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
207 HRESULT hres;
209 hres = compile_expression(ctx, expr->expression1);
210 if(FAILED(hres))
211 return hres;
213 hres = compile_expression(ctx, expr->expression2);
214 if(FAILED(hres))
215 return hres;
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)
222 HRESULT hres;
224 hres = compile_expression(ctx, expr->expression);
225 if(FAILED(hres))
226 return hres;
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)
234 HRESULT hres;
236 hres = compile_expression(ctx, expr->expression);
237 if(FAILED(hres))
238 return hres;
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)
250 HRESULT hres = S_OK;
252 switch(expr->type) {
253 case EXPR_IDENT: {
254 identifier_expression_t *ident_expr = (identifier_expression_t*)expr;
256 hres = push_instr_bstr_uint(ctx, OP_identid, ident_expr->identifier, flags);
257 break;
259 case EXPR_ARRAY: {
260 array_expression_t *array_expr = (array_expression_t*)expr;
262 hres = compile_expression(ctx, array_expr->member_expr);
263 if(FAILED(hres))
264 return hres;
266 hres = compile_expression(ctx, array_expr->expression);
267 if(FAILED(hres))
268 return hres;
270 hres = push_instr_uint(ctx, OP_memberid, flags);
271 break;
273 case EXPR_MEMBER: {
274 member_expression_t *member_expr = (member_expression_t*)expr;
276 hres = compile_expression(ctx, member_expr->expression);
277 if(FAILED(hres))
278 return hres;
280 /* FIXME: Potential optimization */
281 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
282 if(FAILED(hres))
283 return hres;
285 hres = push_instr_uint(ctx, OP_memberid, flags);
286 break;
288 default:
289 assert(0);
292 return hres;
295 /* ECMA-262 3rd Edition 11.14 */
296 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr)
298 HRESULT hres;
300 hres = compile_expression(ctx, expr->expression1);
301 if(FAILED(hres))
302 return hres;
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)
313 unsigned instr;
314 HRESULT hres;
316 hres = compile_expression(ctx, expr->expression1);
317 if(FAILED(hres))
318 return hres;
320 instr = push_instr(ctx, op);
321 if(instr == -1)
322 return E_OUTOFMEMORY;
324 hres = compile_expression(ctx, expr->expression2);
325 if(FAILED(hres))
326 return hres;
328 instr_ptr(ctx, instr)->arg1.uint = ctx->code_off;
329 return S_OK;
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;
336 HRESULT hres;
338 hres = compile_expression(ctx, expr->expression);
339 if(FAILED(hres))
340 return hres;
342 jmp_false = push_instr(ctx, OP_jmp_z);
343 if(jmp_false == -1)
344 return E_OUTOFMEMORY;
346 hres = compile_expression(ctx, expr->true_expression);
347 if(FAILED(hres))
348 return hres;
350 jmp_end = push_instr(ctx, OP_jmp);
351 if(jmp_end == -1)
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);
359 if(FAILED(hres))
360 return hres;
362 instr_ptr(ctx, jmp_end)->arg1.uint = ctx->code_off;
363 return S_OK;
366 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
368 unsigned arg_cnt = 0;
369 argument_t *arg;
370 HRESULT hres;
372 hres = compile_expression(ctx, expr->expression);
373 if(FAILED(hres))
374 return hres;
376 for(arg = expr->argument_list; arg; arg = arg->next) {
377 hres = compile_expression(ctx, arg->expr);
378 if(FAILED(hres))
379 return hres;
380 arg_cnt++;
383 return push_instr_int(ctx, OP_new, arg_cnt);
386 static HRESULT compile_interp_fallback(compiler_ctx_t *ctx, expression_t *expr)
388 unsigned instr;
390 instr = push_instr(ctx, OP_tree);
391 if(instr == -1)
392 return E_OUTOFMEMORY;
394 instr_ptr(ctx, instr)->arg1.expr = expr;
395 return S_OK;
398 static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL *no_ret)
400 unsigned arg_cnt = 0;
401 argument_t *arg;
402 unsigned instr;
403 jsop_t op;
404 HRESULT hres;
406 if(is_memberid_expr(expr->expression->type)) {
407 op = OP_call_member;
408 hres = compile_memberid_expression(ctx, expr->expression, 0);
409 }else {
410 op = OP_call;
411 hres = compile_expression(ctx, expr->expression);
414 if(FAILED(hres))
415 return hres;
417 for(arg = expr->argument_list; arg; arg = arg->next) {
418 hres = compile_expression(ctx, arg->expr);
419 if(FAILED(hres))
420 return hres;
421 arg_cnt++;
424 instr = push_instr(ctx, op);
425 if(instr == -1)
426 return E_OUTOFMEMORY;
428 instr_ptr(ctx, instr)->arg1.uint = arg_cnt;
429 instr_ptr(ctx, instr)->arg2.lng = no_ret == NULL;
430 if(no_ret)
431 *no_ret = TRUE;
432 return S_OK;
435 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
437 HRESULT hres;
439 switch(expr->expression->type) {
440 case EXPR_ARRAY: {
441 array_expression_t *array_expr = (array_expression_t*)expr->expression;
443 hres = compile_expression(ctx, array_expr->member_expr);
444 if(FAILED(hres))
445 return hres;
447 hres = compile_expression(ctx, array_expr->expression);
448 if(FAILED(hres))
449 return hres;
451 if(push_instr(ctx, OP_delete) == -1)
452 return E_OUTOFMEMORY;
453 break;
455 case EXPR_MEMBER: {
456 member_expression_t *member_expr = (member_expression_t*)expr->expression;
458 hres = compile_expression(ctx, member_expr->expression);
459 if(FAILED(hres))
460 return hres;
462 /* FIXME: Potential optimization */
463 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
464 if(FAILED(hres))
465 return hres;
467 if(push_instr(ctx, OP_delete) == -1)
468 return E_OUTOFMEMORY;
469 break;
471 default:
472 expr->expr.eval = delete_expression_eval;
473 return compile_interp_fallback(ctx, &expr->expr);
476 return S_OK;
479 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
481 HRESULT hres;
483 if(!is_memberid_expr(expr->expression1->type)) {
484 hres = compile_expression(ctx, expr->expression1);
485 if(FAILED(hres))
486 return hres;
488 hres = compile_expression(ctx, expr->expression2);
489 if(FAILED(hres))
490 return hres;
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);
499 if(FAILED(hres))
500 return hres;
502 if(op != OP_LAST && push_instr(ctx, OP_refval) == -1)
503 return E_OUTOFMEMORY;
505 hres = compile_expression(ctx, expr->expression2);
506 if(FAILED(hres))
507 return hres;
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;
515 return S_OK;
518 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
520 switch(literal->type) {
521 case LT_BOOL:
522 return push_instr_int(ctx, OP_bool, literal->u.bval);
523 case LT_DOUBLE:
524 return push_instr_double(ctx, OP_double, literal->u.dval);
525 case LT_INT:
526 return push_instr_int(ctx, OP_int, literal->u.lval);
527 case LT_NULL:
528 return push_instr(ctx, OP_null);
529 case LT_STRING:
530 return push_instr_str(ctx, OP_str, literal->u.wstr);
531 case LT_REGEXP: {
532 unsigned instr;
533 WCHAR *str;
535 str = compiler_alloc(ctx->code, (literal->u.regexp.str_len+1)*sizeof(WCHAR));
536 if(!str)
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);
542 if(instr == -1)
543 return E_OUTOFMEMORY;
545 instr_ptr(ctx, instr)->arg1.str = str;
546 instr_ptr(ctx, instr)->arg2.lng = literal->u.regexp.flags;
547 return S_OK;
549 default:
550 assert(0);
554 static HRESULT compile_expression_noret(compiler_ctx_t *ctx, expression_t *expr, BOOL *no_ret)
556 switch(expr->type) {
557 case EXPR_ADD:
558 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
559 case EXPR_AND:
560 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_jmp_z);
561 case EXPR_ASSIGN:
562 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
563 case EXPR_ASSIGNADD:
564 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
565 case EXPR_ASSIGNSUB:
566 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
567 case EXPR_ASSIGNMUL:
568 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
569 case EXPR_ASSIGNDIV:
570 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
571 case EXPR_ASSIGNMOD:
572 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
573 case EXPR_ASSIGNOR:
574 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
575 case EXPR_ASSIGNXOR:
576 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
577 case EXPR_BITNEG:
578 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
579 case EXPR_BOR:
580 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
581 case EXPR_CALL:
582 return compile_call_expression(ctx, (call_expression_t*)expr, no_ret);
583 case EXPR_COMMA:
584 return compile_comma_expression(ctx, (binary_expression_t*)expr);
585 case EXPR_COND:
586 return compile_conditional_expression(ctx, (conditional_expression_t*)expr);
587 case EXPR_DELETE:
588 return compile_delete_expression(ctx, (unary_expression_t*)expr);
589 case EXPR_DIV:
590 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
591 case EXPR_EQ:
592 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
593 case EXPR_EQEQ:
594 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
595 case EXPR_GREATER:
596 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
597 case EXPR_GREATEREQ:
598 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
599 case EXPR_IDENT:
600 return push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
601 case EXPR_IN:
602 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
603 case EXPR_LESS:
604 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
605 case EXPR_LESSEQ:
606 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
607 case EXPR_LITERAL:
608 return compile_literal(ctx, ((literal_expression_t*)expr)->literal);
609 case EXPR_LOGNEG:
610 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
611 case EXPR_MEMBER:
612 return compile_member_expression(ctx, (member_expression_t*)expr);
613 case EXPR_MINUS:
614 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
615 case EXPR_MOD:
616 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
617 case EXPR_MUL:
618 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
619 case EXPR_NEW:
620 return compile_new_expression(ctx, (call_expression_t*)expr);
621 case EXPR_NOTEQ:
622 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
623 case EXPR_NOTEQEQ:
624 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
625 case EXPR_OR:
626 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_jmp_nz);
627 case EXPR_PLUS:
628 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
629 case EXPR_SUB:
630 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
631 case EXPR_THIS:
632 return push_instr(ctx, OP_this) == -1 ? E_OUTOFMEMORY : S_OK;
633 case EXPR_VOID:
634 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
635 case EXPR_BXOR:
636 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
637 default:
638 assert(expr->eval != compiled_expression_eval);
639 return compile_interp_fallback(ctx, expr);
642 return S_OK;
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)
652 unsigned i;
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);
660 heap_free(code);
663 void release_compiler(compiler_ctx_t *ctx)
665 heap_free(ctx);
668 HRESULT compile_subscript(parser_ctx_t *parser, expression_t *expr, BOOL do_ret, unsigned *ret_off)
670 BOOL no_ret = FALSE;
671 HRESULT hres;
673 if(!parser->code) {
674 parser->code = heap_alloc_zero(sizeof(bytecode_t));
675 if(!parser->code)
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);
691 if(FAILED(hres))
692 return hres;
694 return push_instr(parser->compiler, OP_ret) == -1 ? E_OUTOFMEMORY : S_OK;