jscript: Use bytecode for '>=' expression implementation.
[wine] / dlls / jscript / compile.c
1 /*
2  * Copyright 2011 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <math.h>
20 #include <assert.h>
21
22 #include "jscript.h"
23 #include "engine.h"
24
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28
29 struct _compiler_ctx_t {
30     parser_ctx_t *parser;
31     bytecode_t *code;
32
33     unsigned code_off;
34     unsigned code_size;
35 };
36
37 static HRESULT compile_expression(compiler_ctx_t*,expression_t*);
38
39 static inline void *compiler_alloc(bytecode_t *code, size_t size)
40 {
41     return jsheap_alloc(&code->heap, size);
42 }
43
44 static WCHAR *compiler_alloc_string(bytecode_t *code, const WCHAR *str)
45 {
46     size_t size;
47     WCHAR *ret;
48
49     size = (strlenW(str)+1)*sizeof(WCHAR);
50     ret = compiler_alloc(code, size);
51     if(ret)
52         memcpy(ret, str, size);
53     return ret;
54 }
55
56 static BSTR compiler_alloc_bstr(compiler_ctx_t *ctx, const WCHAR *str)
57 {
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;
65
66         new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
67         if(!new_pool)
68             return NULL;
69
70         ctx->code->bstr_pool = new_pool;
71         ctx->code->bstr_pool_size *= 2;
72     }
73
74     ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
75     if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
76         return NULL;
77
78     return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
79 }
80
81 static unsigned push_instr(compiler_ctx_t *ctx, jsop_t op)
82 {
83     assert(ctx->code_size >= ctx->code_off);
84
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;
92
93         new_instrs = heap_realloc(ctx->code->instrs, ctx->code_size*2*sizeof(instr_t));
94         if(!new_instrs)
95             return -1;
96
97         ctx->code->instrs = new_instrs;
98         ctx->code_size *= 2;
99     }
100
101     ctx->code->instrs[ctx->code_off].op = op;
102     return ctx->code_off++;
103 }
104
105 static inline instr_t *instr_ptr(compiler_ctx_t *ctx, unsigned off)
106 {
107     assert(off < ctx->code_off);
108     return ctx->code->instrs + off;
109 }
110
111 static HRESULT push_instr_int(compiler_ctx_t *ctx, jsop_t op, LONG arg)
112 {
113     unsigned instr;
114
115     instr = push_instr(ctx, op);
116     if(instr == -1)
117         return E_OUTOFMEMORY;
118
119     instr_ptr(ctx, instr)->arg1.lng = arg;
120     return S_OK;
121 }
122
123 static HRESULT push_instr_str(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
124 {
125     unsigned instr;
126     WCHAR *str;
127
128     str = compiler_alloc_string(ctx->code, arg);
129     if(!str)
130         return E_OUTOFMEMORY;
131
132     instr = push_instr(ctx, op);
133     if(instr == -1)
134         return E_OUTOFMEMORY;
135
136     instr_ptr(ctx, instr)->arg1.str = str;
137     return S_OK;
138 }
139
140 static HRESULT push_instr_bstr(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
141 {
142     unsigned instr;
143     WCHAR *str;
144
145     str = compiler_alloc_bstr(ctx, arg);
146     if(!str)
147         return E_OUTOFMEMORY;
148
149     instr = push_instr(ctx, op);
150     if(instr == -1)
151         return E_OUTOFMEMORY;
152
153     instr_ptr(ctx, instr)->arg1.bstr = str;
154     return S_OK;
155 }
156
157 static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
158 {
159     unsigned instr;
160     DOUBLE *dbl;
161
162     dbl = compiler_alloc(ctx->code, sizeof(arg));
163     if(!dbl)
164         return E_OUTOFMEMORY;
165     *dbl = arg;
166
167     instr = push_instr(ctx, op);
168     if(instr == -1)
169         return E_OUTOFMEMORY;
170
171     instr_ptr(ctx, instr)->arg1.dbl = dbl;
172     return S_OK;
173 }
174
175 static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
176 {
177     HRESULT hres;
178
179     hres = compile_expression(ctx, expr->expression1);
180     if(FAILED(hres))
181         return hres;
182
183     hres = compile_expression(ctx, expr->expression2);
184     if(FAILED(hres))
185         return hres;
186
187     return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
188 }
189
190 static HRESULT compile_unary_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op)
191 {
192     HRESULT hres;
193
194     hres = compile_expression(ctx, expr->expression);
195     if(FAILED(hres))
196         return hres;
197
198     return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
199 }
200
201 /* ECMA-262 3rd Edition    11.14 */
202 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr)
203 {
204     HRESULT hres;
205
206     hres = compile_expression(ctx, expr->expression1);
207     if(FAILED(hres))
208         return hres;
209
210     if(push_instr(ctx, OP_pop) == -1)
211         return E_OUTOFMEMORY;
212
213     return compile_expression(ctx, expr->expression2);
214 }
215
216 /* ECMA-262 3rd Edition    11.11 */
217 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
218 {
219     unsigned instr;
220     HRESULT hres;
221
222     hres = compile_expression(ctx, expr->expression1);
223     if(FAILED(hres))
224         return hres;
225
226     instr = push_instr(ctx, op);
227     if(instr == -1)
228         return E_OUTOFMEMORY;
229
230     hres = compile_expression(ctx, expr->expression2);
231     if(FAILED(hres))
232         return hres;
233
234     instr_ptr(ctx, instr)->arg1.uint = ctx->code_off;
235     return S_OK;
236 }
237
238 /* ECMA-262 3rd Edition    11.12 */
239 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
240 {
241     unsigned jmp_false, jmp_end;
242     HRESULT hres;
243
244     hres = compile_expression(ctx, expr->expression);
245     if(FAILED(hres))
246         return hres;
247
248     jmp_false = push_instr(ctx, OP_jmp_z);
249     if(jmp_false == -1)
250         return E_OUTOFMEMORY;
251
252     hres = compile_expression(ctx, expr->true_expression);
253     if(FAILED(hres))
254         return hres;
255
256     jmp_end = push_instr(ctx, OP_jmp);
257     if(jmp_end == -1)
258         return E_OUTOFMEMORY;
259
260     instr_ptr(ctx, jmp_false)->arg1.uint = ctx->code_off;
261     if(push_instr(ctx, OP_pop) == -1)
262         return E_OUTOFMEMORY;
263
264     hres = compile_expression(ctx, expr->false_expression);
265     if(FAILED(hres))
266         return hres;
267
268     instr_ptr(ctx, jmp_end)->arg1.uint = ctx->code_off;
269     return S_OK;
270 }
271
272 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
273 {
274     unsigned arg_cnt = 0;
275     argument_t *arg;
276     HRESULT hres;
277
278     hres = compile_expression(ctx, expr->expression);
279     if(FAILED(hres))
280         return hres;
281
282     for(arg = expr->argument_list; arg; arg = arg->next) {
283         hres = compile_expression(ctx, arg->expr);
284         if(FAILED(hres))
285             return hres;
286         arg_cnt++;
287     }
288
289     return push_instr_int(ctx, OP_new, arg_cnt);
290 }
291
292 static HRESULT compile_interp_fallback(compiler_ctx_t *ctx, expression_t *expr)
293 {
294     unsigned instr;
295
296     instr = push_instr(ctx, OP_tree);
297     if(instr == -1)
298         return E_OUTOFMEMORY;
299
300     instr_ptr(ctx, instr)->arg1.expr = expr;
301     return S_OK;
302 }
303
304 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
305 {
306     HRESULT hres;
307
308     switch(expr->expression->type) {
309     case EXPR_ARRAY: {
310         array_expression_t *array_expr = (array_expression_t*)expr->expression;
311
312         hres = compile_expression(ctx, array_expr->member_expr);
313         if(FAILED(hres))
314             return hres;
315
316         hres = compile_expression(ctx, array_expr->expression);
317         if(FAILED(hres))
318             return hres;
319
320         if(push_instr(ctx, OP_delete) == -1)
321             return E_OUTOFMEMORY;
322         break;
323     }
324     case EXPR_MEMBER: {
325         member_expression_t *member_expr = (member_expression_t*)expr->expression;
326
327         hres = compile_expression(ctx, member_expr->expression);
328         if(FAILED(hres))
329             return hres;
330
331         /* FIXME: Potential optimization */
332         hres = push_instr_str(ctx, OP_str, member_expr->identifier);
333         if(FAILED(hres))
334             return hres;
335
336         if(push_instr(ctx, OP_delete) == -1)
337             return E_OUTOFMEMORY;
338         break;
339     }
340     default:
341         expr->expr.eval = delete_expression_eval;
342         return compile_interp_fallback(ctx, &expr->expr);
343     }
344
345     return S_OK;
346 }
347
348 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
349 {
350     switch(literal->type) {
351     case LT_BOOL:
352         return push_instr_int(ctx, OP_bool, literal->u.bval);
353     case LT_DOUBLE:
354         return push_instr_double(ctx, OP_double, literal->u.dval);
355     case LT_INT:
356         return push_instr_int(ctx, OP_int, literal->u.lval);
357     case LT_NULL:
358         return push_instr(ctx, OP_null);
359     case LT_STRING:
360         return push_instr_str(ctx, OP_str, literal->u.wstr);
361     case LT_REGEXP: {
362         unsigned instr;
363         WCHAR *str;
364
365         str = compiler_alloc(ctx->code, (literal->u.regexp.str_len+1)*sizeof(WCHAR));
366         if(!str)
367             return E_OUTOFMEMORY;
368         memcpy(str, literal->u.regexp.str, literal->u.regexp.str_len*sizeof(WCHAR));
369         str[literal->u.regexp.str_len] = 0;
370
371         instr = push_instr(ctx, OP_regexp);
372         if(instr == -1)
373             return E_OUTOFMEMORY;
374
375         instr_ptr(ctx, instr)->arg1.str = str;
376         instr_ptr(ctx, instr)->arg2.lng = literal->u.regexp.flags;
377         return S_OK;
378     }
379     default:
380         assert(0);
381     }
382 }
383
384 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr)
385 {
386     switch(expr->type) {
387     case EXPR_AND:
388         return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_jmp_z);
389     case EXPR_ADD:
390         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
391     case EXPR_BITNEG:
392         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
393     case EXPR_BOR:
394         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
395     case EXPR_COMMA:
396         return compile_comma_expression(ctx, (binary_expression_t*)expr);
397     case EXPR_COND:
398         return compile_conditional_expression(ctx, (conditional_expression_t*)expr);
399     case EXPR_DELETE:
400         return compile_delete_expression(ctx, (unary_expression_t*)expr);
401     case EXPR_DIV:
402         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
403     case EXPR_EQ:
404         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
405     case EXPR_EQEQ:
406         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
407     case EXPR_GREATER:
408         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
409     case EXPR_GREATEREQ:
410         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
411     case EXPR_IDENT:
412         return push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
413     case EXPR_IN:
414         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
415     case EXPR_LESS:
416         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
417     case EXPR_LESSEQ:
418         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
419     case EXPR_LITERAL:
420         return compile_literal(ctx, ((literal_expression_t*)expr)->literal);
421     case EXPR_LOGNEG:
422         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
423     case EXPR_MINUS:
424         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
425     case EXPR_MOD:
426         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
427     case EXPR_MUL:
428         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
429     case EXPR_NEW:
430         return compile_new_expression(ctx, (call_expression_t*)expr);
431     case EXPR_NOTEQ:
432         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
433     case EXPR_NOTEQEQ:
434         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
435     case EXPR_OR:
436         return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_jmp_nz);
437     case EXPR_PLUS:
438         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
439     case EXPR_SUB:
440         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
441     case EXPR_THIS:
442         return push_instr(ctx, OP_this) == -1 ? E_OUTOFMEMORY : S_OK;
443     case EXPR_VOID:
444         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
445     case EXPR_BXOR:
446         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
447     default:
448         assert(expr->eval != compiled_expression_eval);
449         return compile_interp_fallback(ctx, expr);
450     }
451
452     return S_OK;
453 }
454
455 void release_bytecode(bytecode_t *code)
456 {
457     unsigned i;
458
459     for(i=0; i < code->bstr_cnt; i++)
460         SysFreeString(code->bstr_pool[i]);
461
462     jsheap_free(&code->heap);
463     heap_free(code->bstr_pool);
464     heap_free(code->instrs);
465     heap_free(code);
466 }
467
468 void release_compiler(compiler_ctx_t *ctx)
469 {
470     heap_free(ctx);
471 }
472
473 HRESULT compile_subscript(parser_ctx_t *parser, expression_t *expr, unsigned *ret_off)
474 {
475     HRESULT hres;
476
477     if(!parser->code) {
478         parser->code = heap_alloc_zero(sizeof(bytecode_t));
479         if(!parser->code)
480             return E_OUTOFMEMORY;
481         jsheap_init(&parser->code->heap);
482     }
483
484     if(!parser->compiler) {
485         parser->compiler = heap_alloc_zero(sizeof(compiler_ctx_t));
486         if(!parser->compiler)
487             return E_OUTOFMEMORY;
488
489         parser->compiler->parser = parser;
490         parser->compiler->code = parser->code;
491     }
492
493     *ret_off = parser->compiler->code_off;
494     hres = compile_expression(parser->compiler, expr);
495     if(FAILED(hres))
496         return hres;
497
498     return push_instr(parser->compiler, OP_ret) == -1 ? E_OUTOFMEMORY : S_OK;
499 }