jscript: Use bytecode for '|=' expression.
[wine/multimedia.git] / dlls / jscript / compile.c
blob22d5a5f9d55ce19f34d659b236085867ec88e794
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_double(compiler_ctx_t *ctx, jsop_t op, double arg)
159 unsigned instr;
160 DOUBLE *dbl;
162 dbl = compiler_alloc(ctx->code, sizeof(arg));
163 if(!dbl)
164 return E_OUTOFMEMORY;
165 *dbl = arg;
167 instr = push_instr(ctx, op);
168 if(instr == -1)
169 return E_OUTOFMEMORY;
171 instr_ptr(ctx, instr)->arg1.dbl = dbl;
172 return S_OK;
175 static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
177 unsigned instr;
179 instr = push_instr(ctx, op);
180 if(instr == -1)
181 return E_OUTOFMEMORY;
183 instr_ptr(ctx, instr)->arg1.uint = arg;
184 return S_OK;
187 static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
189 HRESULT hres;
191 hres = compile_expression(ctx, expr->expression1);
192 if(FAILED(hres))
193 return hres;
195 hres = compile_expression(ctx, expr->expression2);
196 if(FAILED(hres))
197 return hres;
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)
204 HRESULT hres;
206 hres = compile_expression(ctx, expr->expression);
207 if(FAILED(hres))
208 return hres;
210 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
213 /* ECMA-262 3rd Edition 11.2.1 */
214 static HRESULT compile_member_expression(compiler_ctx_t *ctx, member_expression_t *expr)
216 HRESULT hres;
218 hres = compile_expression(ctx, expr->expression);
219 if(FAILED(hres))
220 return hres;
222 return push_instr_bstr(ctx, OP_member, expr->identifier);
225 /* ECMA-262 3rd Edition 11.14 */
226 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr)
228 HRESULT hres;
230 hres = compile_expression(ctx, expr->expression1);
231 if(FAILED(hres))
232 return hres;
234 if(push_instr(ctx, OP_pop) == -1)
235 return E_OUTOFMEMORY;
237 return compile_expression(ctx, expr->expression2);
240 /* ECMA-262 3rd Edition 11.11 */
241 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
243 unsigned instr;
244 HRESULT hres;
246 hres = compile_expression(ctx, expr->expression1);
247 if(FAILED(hres))
248 return hres;
250 instr = push_instr(ctx, op);
251 if(instr == -1)
252 return E_OUTOFMEMORY;
254 hres = compile_expression(ctx, expr->expression2);
255 if(FAILED(hres))
256 return hres;
258 instr_ptr(ctx, instr)->arg1.uint = ctx->code_off;
259 return S_OK;
262 /* ECMA-262 3rd Edition 11.12 */
263 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
265 unsigned jmp_false, jmp_end;
266 HRESULT hres;
268 hres = compile_expression(ctx, expr->expression);
269 if(FAILED(hres))
270 return hres;
272 jmp_false = push_instr(ctx, OP_jmp_z);
273 if(jmp_false == -1)
274 return E_OUTOFMEMORY;
276 hres = compile_expression(ctx, expr->true_expression);
277 if(FAILED(hres))
278 return hres;
280 jmp_end = push_instr(ctx, OP_jmp);
281 if(jmp_end == -1)
282 return E_OUTOFMEMORY;
284 instr_ptr(ctx, jmp_false)->arg1.uint = ctx->code_off;
285 if(push_instr(ctx, OP_pop) == -1)
286 return E_OUTOFMEMORY;
288 hres = compile_expression(ctx, expr->false_expression);
289 if(FAILED(hres))
290 return hres;
292 instr_ptr(ctx, jmp_end)->arg1.uint = ctx->code_off;
293 return S_OK;
296 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
298 unsigned arg_cnt = 0;
299 argument_t *arg;
300 HRESULT hres;
302 hres = compile_expression(ctx, expr->expression);
303 if(FAILED(hres))
304 return hres;
306 for(arg = expr->argument_list; arg; arg = arg->next) {
307 hres = compile_expression(ctx, arg->expr);
308 if(FAILED(hres))
309 return hres;
310 arg_cnt++;
313 return push_instr_int(ctx, OP_new, arg_cnt);
316 static HRESULT compile_interp_fallback(compiler_ctx_t *ctx, expression_t *expr)
318 unsigned instr;
320 instr = push_instr(ctx, OP_tree);
321 if(instr == -1)
322 return E_OUTOFMEMORY;
324 instr_ptr(ctx, instr)->arg1.expr = expr;
325 return S_OK;
328 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
330 HRESULT hres;
332 switch(expr->expression->type) {
333 case EXPR_ARRAY: {
334 array_expression_t *array_expr = (array_expression_t*)expr->expression;
336 hres = compile_expression(ctx, array_expr->member_expr);
337 if(FAILED(hres))
338 return hres;
340 hres = compile_expression(ctx, array_expr->expression);
341 if(FAILED(hres))
342 return hres;
344 if(push_instr(ctx, OP_delete) == -1)
345 return E_OUTOFMEMORY;
346 break;
348 case EXPR_MEMBER: {
349 member_expression_t *member_expr = (member_expression_t*)expr->expression;
351 hres = compile_expression(ctx, member_expr->expression);
352 if(FAILED(hres))
353 return hres;
355 /* FIXME: Potential optimization */
356 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
357 if(FAILED(hres))
358 return hres;
360 if(push_instr(ctx, OP_delete) == -1)
361 return E_OUTOFMEMORY;
362 break;
364 default:
365 expr->expr.eval = delete_expression_eval;
366 return compile_interp_fallback(ctx, &expr->expr);
369 return S_OK;
372 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
374 HRESULT hres;
376 switch(expr->expression1->type) {
377 case EXPR_IDENT: {
378 identifier_expression_t *ident_expr = (identifier_expression_t*)expr->expression1;
380 hres = push_instr_bstr(ctx, OP_identid, ident_expr->identifier);
381 if(FAILED(hres))
382 return hres;
383 break;
385 case EXPR_ARRAY: {
386 array_expression_t *array_expr = (array_expression_t*)expr->expression1;
388 hres = compile_expression(ctx, array_expr->member_expr);
389 if(FAILED(hres))
390 return hres;
392 hres = compile_expression(ctx, array_expr->expression);
393 if(FAILED(hres))
394 return hres;
396 if(push_instr(ctx, OP_memberid) == -1)
397 return E_OUTOFMEMORY;
398 break;
400 case EXPR_MEMBER: {
401 member_expression_t *member_expr = (member_expression_t*)expr->expression1;
403 hres = compile_expression(ctx, member_expr->expression);
404 if(FAILED(hres))
405 return hres;
407 /* FIXME: Potential optimization */
408 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
409 if(FAILED(hres))
410 return hres;
412 if(push_instr(ctx, OP_memberid) == -1)
413 return E_OUTOFMEMORY;
414 break;
416 default:
417 hres = compile_expression(ctx, expr->expression1);
418 if(FAILED(hres))
419 return hres;
421 hres = compile_expression(ctx, expr->expression2);
422 if(FAILED(hres))
423 return hres;
425 if(op != OP_LAST && push_instr(ctx, op) == -1)
426 return E_OUTOFMEMORY;
428 return push_instr_uint(ctx, OP_throw, JS_E_ILLEGAL_ASSIGN);
431 if(op != OP_LAST && push_instr(ctx, OP_refval) == -1)
432 return E_OUTOFMEMORY;
434 hres = compile_expression(ctx, expr->expression2);
435 if(FAILED(hres))
436 return hres;
438 if(op != OP_LAST && push_instr(ctx, op) == -1)
439 return E_OUTOFMEMORY;
441 if(push_instr(ctx, OP_assign) == -1)
442 return E_OUTOFMEMORY;
444 return S_OK;
447 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
449 switch(literal->type) {
450 case LT_BOOL:
451 return push_instr_int(ctx, OP_bool, literal->u.bval);
452 case LT_DOUBLE:
453 return push_instr_double(ctx, OP_double, literal->u.dval);
454 case LT_INT:
455 return push_instr_int(ctx, OP_int, literal->u.lval);
456 case LT_NULL:
457 return push_instr(ctx, OP_null);
458 case LT_STRING:
459 return push_instr_str(ctx, OP_str, literal->u.wstr);
460 case LT_REGEXP: {
461 unsigned instr;
462 WCHAR *str;
464 str = compiler_alloc(ctx->code, (literal->u.regexp.str_len+1)*sizeof(WCHAR));
465 if(!str)
466 return E_OUTOFMEMORY;
467 memcpy(str, literal->u.regexp.str, literal->u.regexp.str_len*sizeof(WCHAR));
468 str[literal->u.regexp.str_len] = 0;
470 instr = push_instr(ctx, OP_regexp);
471 if(instr == -1)
472 return E_OUTOFMEMORY;
474 instr_ptr(ctx, instr)->arg1.str = str;
475 instr_ptr(ctx, instr)->arg2.lng = literal->u.regexp.flags;
476 return S_OK;
478 default:
479 assert(0);
483 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr)
485 switch(expr->type) {
486 case EXPR_ADD:
487 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
488 case EXPR_AND:
489 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_jmp_z);
490 case EXPR_ASSIGN:
491 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
492 case EXPR_ASSIGNADD:
493 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
494 case EXPR_ASSIGNSUB:
495 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
496 case EXPR_ASSIGNMUL:
497 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
498 case EXPR_ASSIGNDIV:
499 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
500 case EXPR_ASSIGNMOD:
501 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
502 case EXPR_ASSIGNOR:
503 return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
504 case EXPR_BITNEG:
505 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
506 case EXPR_BOR:
507 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
508 case EXPR_COMMA:
509 return compile_comma_expression(ctx, (binary_expression_t*)expr);
510 case EXPR_COND:
511 return compile_conditional_expression(ctx, (conditional_expression_t*)expr);
512 case EXPR_DELETE:
513 return compile_delete_expression(ctx, (unary_expression_t*)expr);
514 case EXPR_DIV:
515 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
516 case EXPR_EQ:
517 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
518 case EXPR_EQEQ:
519 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
520 case EXPR_GREATER:
521 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
522 case EXPR_GREATEREQ:
523 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
524 case EXPR_IDENT:
525 return push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
526 case EXPR_IN:
527 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
528 case EXPR_LESS:
529 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
530 case EXPR_LESSEQ:
531 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
532 case EXPR_LITERAL:
533 return compile_literal(ctx, ((literal_expression_t*)expr)->literal);
534 case EXPR_LOGNEG:
535 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
536 case EXPR_MEMBER:
537 return compile_member_expression(ctx, (member_expression_t*)expr);
538 case EXPR_MINUS:
539 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
540 case EXPR_MOD:
541 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
542 case EXPR_MUL:
543 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
544 case EXPR_NEW:
545 return compile_new_expression(ctx, (call_expression_t*)expr);
546 case EXPR_NOTEQ:
547 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
548 case EXPR_NOTEQEQ:
549 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
550 case EXPR_OR:
551 return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_jmp_nz);
552 case EXPR_PLUS:
553 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
554 case EXPR_SUB:
555 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
556 case EXPR_THIS:
557 return push_instr(ctx, OP_this) == -1 ? E_OUTOFMEMORY : S_OK;
558 case EXPR_VOID:
559 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
560 case EXPR_BXOR:
561 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
562 default:
563 assert(expr->eval != compiled_expression_eval);
564 return compile_interp_fallback(ctx, expr);
567 return S_OK;
570 void release_bytecode(bytecode_t *code)
572 unsigned i;
574 for(i=0; i < code->bstr_cnt; i++)
575 SysFreeString(code->bstr_pool[i]);
577 jsheap_free(&code->heap);
578 heap_free(code->bstr_pool);
579 heap_free(code->instrs);
580 heap_free(code);
583 void release_compiler(compiler_ctx_t *ctx)
585 heap_free(ctx);
588 HRESULT compile_subscript(parser_ctx_t *parser, expression_t *expr, unsigned *ret_off)
590 HRESULT hres;
592 if(!parser->code) {
593 parser->code = heap_alloc_zero(sizeof(bytecode_t));
594 if(!parser->code)
595 return E_OUTOFMEMORY;
596 jsheap_init(&parser->code->heap);
599 if(!parser->compiler) {
600 parser->compiler = heap_alloc_zero(sizeof(compiler_ctx_t));
601 if(!parser->compiler)
602 return E_OUTOFMEMORY;
604 parser->compiler->parser = parser;
605 parser->compiler->code = parser->code;
608 *ret_off = parser->compiler->code_off;
609 hres = compile_expression(parser->compiler, expr);
610 if(FAILED(hres))
611 return hres;
613 return push_instr(parser->compiler, OP_ret) == -1 ? E_OUTOFMEMORY : S_OK;