jscript: Use bytecode for function expression implementation.
[wine.git] / dlls / jscript / compile.c
blob9331910d1f6fafe5f6ef4923a3d024a75505992a
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_uint_str(compiler_ctx_t *ctx, jsop_t op, unsigned arg1, const WCHAR *arg2)
177 unsigned instr;
178 WCHAR *str;
180 str = compiler_alloc_string(ctx->code, arg2);
181 if(!str)
182 return E_OUTOFMEMORY;
184 instr = push_instr(ctx, op);
185 if(instr == -1)
186 return E_OUTOFMEMORY;
188 instr_ptr(ctx, instr)->arg1.uint = arg1;
189 instr_ptr(ctx, instr)->arg2.str = str;
190 return S_OK;
193 static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
195 unsigned instr;
196 DOUBLE *dbl;
198 dbl = compiler_alloc(ctx->code, sizeof(arg));
199 if(!dbl)
200 return E_OUTOFMEMORY;
201 *dbl = arg;
203 instr = push_instr(ctx, op);
204 if(instr == -1)
205 return E_OUTOFMEMORY;
207 instr_ptr(ctx, instr)->arg1.dbl = dbl;
208 return S_OK;
211 static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
213 unsigned instr;
215 instr = push_instr(ctx, op);
216 if(instr == -1)
217 return E_OUTOFMEMORY;
219 instr_ptr(ctx, instr)->arg1.uint = arg;
220 return S_OK;
223 static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
225 HRESULT hres;
227 hres = compile_expression(ctx, expr->expression1);
228 if(FAILED(hres))
229 return hres;
231 hres = compile_expression(ctx, expr->expression2);
232 if(FAILED(hres))
233 return hres;
235 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
238 static HRESULT compile_unary_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op)
240 HRESULT hres;
242 hres = compile_expression(ctx, expr->expression);
243 if(FAILED(hres))
244 return hres;
246 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
249 /* ECMA-262 3rd Edition 11.2.1 */
250 static HRESULT compile_member_expression(compiler_ctx_t *ctx, member_expression_t *expr)
252 HRESULT hres;
254 hres = compile_expression(ctx, expr->expression);
255 if(FAILED(hres))
256 return hres;
258 return push_instr_bstr(ctx, OP_member, expr->identifier);
261 static inline BOOL is_memberid_expr(expression_type_t type)
263 return type == EXPR_IDENT || type == EXPR_MEMBER || type == EXPR_ARRAY;
266 static HRESULT compile_memberid_expression(compiler_ctx_t *ctx, expression_t *expr, unsigned flags)
268 HRESULT hres = S_OK;
270 switch(expr->type) {
271 case EXPR_IDENT: {
272 identifier_expression_t *ident_expr = (identifier_expression_t*)expr;
274 hres = push_instr_bstr_uint(ctx, OP_identid, ident_expr->identifier, flags);
275 break;
277 case EXPR_ARRAY: {
278 binary_expression_t *array_expr = (binary_expression_t*)expr;
280 hres = compile_expression(ctx, array_expr->expression1);
281 if(FAILED(hres))
282 return hres;
284 hres = compile_expression(ctx, array_expr->expression2);
285 if(FAILED(hres))
286 return hres;
288 hres = push_instr_uint(ctx, OP_memberid, flags);
289 break;
291 case EXPR_MEMBER: {
292 member_expression_t *member_expr = (member_expression_t*)expr;
294 hres = compile_expression(ctx, member_expr->expression);
295 if(FAILED(hres))
296 return hres;
298 /* FIXME: Potential optimization */
299 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
300 if(FAILED(hres))
301 return hres;
303 hres = push_instr_uint(ctx, OP_memberid, flags);
304 break;
306 default:
307 assert(0);
310 return hres;
313 static HRESULT compile_increment_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op, int n)
315 HRESULT hres;
317 if(!is_memberid_expr(expr->expression->type)) {
318 hres = compile_expression(ctx, expr->expression);
319 if(FAILED(hres))
320 return hres;
322 return push_instr_uint(ctx, OP_throw, JS_E_ILLEGAL_ASSIGN);
325 hres = compile_memberid_expression(ctx, expr->expression, fdexNameEnsure);
326 if(FAILED(hres))
327 return hres;
329 return push_instr_int(ctx, op, n);
332 /* ECMA-262 3rd Edition 11.14 */
333 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr)
335 HRESULT hres;
337 hres = compile_expression(ctx, expr->expression1);
338 if(FAILED(hres))
339 return hres;
341 if(push_instr(ctx, OP_pop) == -1)
342 return E_OUTOFMEMORY;
344 return compile_expression(ctx, expr->expression2);
347 /* ECMA-262 3rd Edition 11.11 */
348 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
350 unsigned instr;
351 HRESULT hres;
353 hres = compile_expression(ctx, expr->expression1);
354 if(FAILED(hres))
355 return hres;
357 instr = push_instr(ctx, op);
358 if(instr == -1)
359 return E_OUTOFMEMORY;
361 hres = compile_expression(ctx, expr->expression2);
362 if(FAILED(hres))
363 return hres;
365 instr_ptr(ctx, instr)->arg1.uint = ctx->code_off;
366 return S_OK;
369 /* ECMA-262 3rd Edition 11.12 */
370 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
372 unsigned jmp_false, jmp_end;
373 HRESULT hres;
375 hres = compile_expression(ctx, expr->expression);
376 if(FAILED(hres))
377 return hres;
379 jmp_false = push_instr(ctx, OP_jmp_z);
380 if(jmp_false == -1)
381 return E_OUTOFMEMORY;
383 hres = compile_expression(ctx, expr->true_expression);
384 if(FAILED(hres))
385 return hres;
387 jmp_end = push_instr(ctx, OP_jmp);
388 if(jmp_end == -1)
389 return E_OUTOFMEMORY;
391 instr_ptr(ctx, jmp_false)->arg1.uint = ctx->code_off;
392 if(push_instr(ctx, OP_pop) == -1)
393 return E_OUTOFMEMORY;
395 hres = compile_expression(ctx, expr->false_expression);
396 if(FAILED(hres))
397 return hres;
399 instr_ptr(ctx, jmp_end)->arg1.uint = ctx->code_off;
400 return S_OK;
403 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
405 unsigned arg_cnt = 0;
406 argument_t *arg;
407 HRESULT hres;
409 hres = compile_expression(ctx, expr->expression);
410 if(FAILED(hres))
411 return hres;
413 for(arg = expr->argument_list; arg; arg = arg->next) {
414 hres = compile_expression(ctx, arg->expr);
415 if(FAILED(hres))
416 return hres;
417 arg_cnt++;
420 return push_instr_int(ctx, OP_new, arg_cnt);
423 static HRESULT compile_interp_fallback(compiler_ctx_t *ctx, expression_t *expr)
425 unsigned instr;
427 instr = push_instr(ctx, OP_tree);
428 if(instr == -1)
429 return E_OUTOFMEMORY;
431 instr_ptr(ctx, instr)->arg1.expr = expr;
432 return S_OK;
435 static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL *no_ret)
437 unsigned arg_cnt = 0;
438 argument_t *arg;
439 unsigned instr;
440 jsop_t op;
441 HRESULT hres;
443 if(is_memberid_expr(expr->expression->type)) {
444 op = OP_call_member;
445 hres = compile_memberid_expression(ctx, expr->expression, 0);
446 }else {
447 op = OP_call;
448 hres = compile_expression(ctx, expr->expression);
451 if(FAILED(hres))
452 return hres;
454 for(arg = expr->argument_list; arg; arg = arg->next) {
455 hres = compile_expression(ctx, arg->expr);
456 if(FAILED(hres))
457 return hres;
458 arg_cnt++;
461 instr = push_instr(ctx, op);
462 if(instr == -1)
463 return E_OUTOFMEMORY;
465 instr_ptr(ctx, instr)->arg1.uint = arg_cnt;
466 instr_ptr(ctx, instr)->arg2.lng = no_ret == NULL;
467 if(no_ret)
468 *no_ret = TRUE;
469 return S_OK;
472 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
474 HRESULT hres;
476 switch(expr->expression->type) {
477 case EXPR_ARRAY: {
478 binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
480 hres = compile_expression(ctx, array_expr->expression1);
481 if(FAILED(hres))
482 return hres;
484 hres = compile_expression(ctx, array_expr->expression2);
485 if(FAILED(hres))
486 return hres;
488 if(push_instr(ctx, OP_delete) == -1)
489 return E_OUTOFMEMORY;
490 break;
492 case EXPR_MEMBER: {
493 member_expression_t *member_expr = (member_expression_t*)expr->expression;
495 hres = compile_expression(ctx, member_expr->expression);
496 if(FAILED(hres))
497 return hres;
499 /* FIXME: Potential optimization */
500 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
501 if(FAILED(hres))
502 return hres;
504 if(push_instr(ctx, OP_delete) == -1)
505 return E_OUTOFMEMORY;
506 break;
508 case EXPR_IDENT:
509 return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
510 default: {
511 const WCHAR fixmeW[] = {'F','I','X','M','E',0};
513 WARN("invalid delete, unimplemented exception message\n");
515 hres = compile_expression(ctx, expr->expression);
516 if(FAILED(hres))
517 return hres;
519 return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, fixmeW);
523 return S_OK;
526 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
528 HRESULT hres;
530 if(!is_memberid_expr(expr->expression1->type)) {
531 hres = compile_expression(ctx, expr->expression1);
532 if(FAILED(hres))
533 return hres;
535 hres = compile_expression(ctx, expr->expression2);
536 if(FAILED(hres))
537 return hres;
539 if(op != OP_LAST && push_instr(ctx, op) == -1)
540 return E_OUTOFMEMORY;
542 return push_instr_uint(ctx, OP_throw, JS_E_ILLEGAL_ASSIGN);
545 hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
546 if(FAILED(hres))
547 return hres;
549 if(op != OP_LAST && push_instr(ctx, OP_refval) == -1)
550 return E_OUTOFMEMORY;
552 hres = compile_expression(ctx, expr->expression2);
553 if(FAILED(hres))
554 return hres;
556 if(op != OP_LAST && push_instr(ctx, op) == -1)
557 return E_OUTOFMEMORY;
559 if(push_instr(ctx, OP_assign) == -1)
560 return E_OUTOFMEMORY;
562 return S_OK;
565 static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
567 jsop_t op;
568 HRESULT hres;
570 if(is_memberid_expr(expr->expression->type)) {
571 if(expr->expression->type == EXPR_IDENT)
572 return push_instr_str(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
574 op = OP_typeofid;
575 hres = compile_memberid_expression(ctx, expr->expression, 0);
576 }else {
577 op = OP_typeof;
578 hres = compile_expression(ctx, expr->expression);
580 if(FAILED(hres))
581 return hres;
583 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
586 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
588 switch(literal->type) {
589 case LT_BOOL:
590 return push_instr_int(ctx, OP_bool, literal->u.bval);
591 case LT_DOUBLE:
592 return push_instr_double(ctx, OP_double, literal->u.dval);
593 case LT_INT:
594 return push_instr_int(ctx, OP_int, literal->u.lval);
595 case LT_NULL:
596 return push_instr(ctx, OP_null);
597 case LT_STRING:
598 return push_instr_str(ctx, OP_str, literal->u.wstr);
599 case LT_REGEXP: {
600 unsigned instr;
601 WCHAR *str;
603 str = compiler_alloc(ctx->code, (literal->u.regexp.str_len+1)*sizeof(WCHAR));
604 if(!str)
605 return E_OUTOFMEMORY;
606 memcpy(str, literal->u.regexp.str, literal->u.regexp.str_len*sizeof(WCHAR));
607 str[literal->u.regexp.str_len] = 0;
609 instr = push_instr(ctx, OP_regexp);
610 if(instr == -1)
611 return E_OUTOFMEMORY;
613 instr_ptr(ctx, instr)->arg1.str = str;
614 instr_ptr(ctx, instr)->arg2.lng = literal->u.regexp.flags;
615 return S_OK;
617 default:
618 assert(0);
622 static HRESULT compile_array_literal(compiler_ctx_t *ctx, array_literal_expression_t *expr)
624 unsigned i, elem_cnt = expr->length;
625 array_element_t *iter;
626 HRESULT hres;
628 for(iter = expr->element_list; iter; iter = iter->next) {
629 elem_cnt += iter->elision+1;
631 for(i=0; i < iter->elision; i++) {
632 if(push_instr(ctx, OP_undefined) == -1)
633 return E_OUTOFMEMORY;
636 hres = compile_expression(ctx, iter->expr);
637 if(FAILED(hres))
638 return hres;
641 for(i=0; i < expr->length; i++) {
642 if(push_instr(ctx, OP_undefined) == -1)
643 return E_OUTOFMEMORY;
646 return push_instr_uint(ctx, OP_carray, elem_cnt);
649 static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr)
651 unsigned instr;
653 /* FIXME: not exactly right */
654 if(expr->identifier)
655 return push_instr_bstr(ctx, OP_ident, expr->identifier);
657 instr = push_instr(ctx, OP_func);
658 if(instr == -1)
659 return E_OUTOFMEMORY;
661 instr_ptr(ctx, instr)->arg1.func = expr;
662 return S_OK;
665 static HRESULT compile_expression_noret(compiler_ctx_t *ctx, expression_t *expr, BOOL *no_ret)
667 switch(expr->type) {
668 case EXPR_ADD:
669 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
670 case EXPR_AND:
671 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_jmp_z);
672 case EXPR_ARRAY:
673 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
674 case EXPR_ARRAYLIT:
675 return compile_array_literal(ctx, (array_literal_expression_t*)expr);
676 case EXPR_ASSIGN:
677 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
678 case EXPR_ASSIGNADD:
679 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
680 case EXPR_ASSIGNAND:
681 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_and);
682 case EXPR_ASSIGNSUB:
683 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
684 case EXPR_ASSIGNMUL:
685 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
686 case EXPR_ASSIGNDIV:
687 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
688 case EXPR_ASSIGNMOD:
689 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
690 case EXPR_ASSIGNOR:
691 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
692 case EXPR_ASSIGNLSHIFT:
693 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_lshift);
694 case EXPR_ASSIGNRSHIFT:
695 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift);
696 case EXPR_ASSIGNRRSHIFT:
697 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
698 case EXPR_ASSIGNXOR:
699 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
700 case EXPR_BAND:
701 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
702 case EXPR_BITNEG:
703 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
704 case EXPR_BOR:
705 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
706 case EXPR_CALL:
707 return compile_call_expression(ctx, (call_expression_t*)expr, no_ret);
708 case EXPR_COMMA:
709 return compile_comma_expression(ctx, (binary_expression_t*)expr);
710 case EXPR_COND:
711 return compile_conditional_expression(ctx, (conditional_expression_t*)expr);
712 case EXPR_DELETE:
713 return compile_delete_expression(ctx, (unary_expression_t*)expr);
714 case EXPR_DIV:
715 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
716 case EXPR_EQ:
717 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
718 case EXPR_EQEQ:
719 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
720 case EXPR_FUNC:
721 return compile_function_expression(ctx, (function_expression_t*)expr);
722 case EXPR_GREATER:
723 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
724 case EXPR_GREATEREQ:
725 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
726 case EXPR_IDENT:
727 return push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
728 case EXPR_IN:
729 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
730 case EXPR_INSTANCEOF:
731 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
732 case EXPR_LESS:
733 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
734 case EXPR_LESSEQ:
735 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
736 case EXPR_LITERAL:
737 return compile_literal(ctx, ((literal_expression_t*)expr)->literal);
738 case EXPR_LOGNEG:
739 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
740 case EXPR_LSHIFT:
741 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
742 case EXPR_MEMBER:
743 return compile_member_expression(ctx, (member_expression_t*)expr);
744 case EXPR_MINUS:
745 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
746 case EXPR_MOD:
747 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
748 case EXPR_MUL:
749 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
750 case EXPR_NEW:
751 return compile_new_expression(ctx, (call_expression_t*)expr);
752 case EXPR_NOTEQ:
753 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
754 case EXPR_NOTEQEQ:
755 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
756 case EXPR_OR:
757 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_jmp_nz);
758 case EXPR_PLUS:
759 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
760 case EXPR_POSTDEC:
761 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
762 case EXPR_POSTINC:
763 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
764 case EXPR_PREDEC:
765 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
766 case EXPR_PREINC:
767 return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
768 case EXPR_RSHIFT:
769 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
770 case EXPR_RRSHIFT:
771 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
772 case EXPR_SUB:
773 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
774 case EXPR_THIS:
775 return push_instr(ctx, OP_this) == -1 ? E_OUTOFMEMORY : S_OK;
776 case EXPR_TYPEOF:
777 return compile_typeof_expression(ctx, (unary_expression_t*)expr);
778 case EXPR_VOID:
779 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
780 case EXPR_BXOR:
781 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
782 default:
783 assert(expr->eval != compiled_expression_eval);
784 return compile_interp_fallback(ctx, expr);
787 return S_OK;
790 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr)
792 return compile_expression_noret(ctx, expr, NULL);
795 void release_bytecode(bytecode_t *code)
797 unsigned i;
799 for(i=0; i < code->bstr_cnt; i++)
800 SysFreeString(code->bstr_pool[i]);
802 jsheap_free(&code->heap);
803 heap_free(code->bstr_pool);
804 heap_free(code->instrs);
805 heap_free(code);
808 void release_compiler(compiler_ctx_t *ctx)
810 heap_free(ctx);
813 HRESULT compile_subscript(parser_ctx_t *parser, expression_t *expr, BOOL do_ret, unsigned *ret_off)
815 BOOL no_ret = FALSE;
816 HRESULT hres;
818 if(!parser->code) {
819 parser->code = heap_alloc_zero(sizeof(bytecode_t));
820 if(!parser->code)
821 return E_OUTOFMEMORY;
822 jsheap_init(&parser->code->heap);
825 if(!parser->compiler) {
826 parser->compiler = heap_alloc_zero(sizeof(compiler_ctx_t));
827 if(!parser->compiler)
828 return E_OUTOFMEMORY;
830 parser->compiler->parser = parser;
831 parser->compiler->code = parser->code;
834 *ret_off = parser->compiler->code_off;
835 hres = compile_expression_noret(parser->compiler, expr, do_ret ? NULL : &no_ret);
836 if(FAILED(hres))
837 return hres;
839 return push_instr(parser->compiler, OP_ret) == -1 ? E_OUTOFMEMORY : S_OK;