jscript: Use bytecode for pre-decrement 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_bstr_uint(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg1, unsigned arg2)
158 {
159     unsigned instr;
160     WCHAR *str;
161
162     str = compiler_alloc_bstr(ctx, arg1);
163     if(!str)
164         return E_OUTOFMEMORY;
165
166     instr = push_instr(ctx, op);
167     if(instr == -1)
168         return E_OUTOFMEMORY;
169
170     instr_ptr(ctx, instr)->arg1.bstr = str;
171     instr_ptr(ctx, instr)->arg2.uint = arg2;
172     return S_OK;
173 }
174
175 static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
176 {
177     unsigned instr;
178     DOUBLE *dbl;
179
180     dbl = compiler_alloc(ctx->code, sizeof(arg));
181     if(!dbl)
182         return E_OUTOFMEMORY;
183     *dbl = arg;
184
185     instr = push_instr(ctx, op);
186     if(instr == -1)
187         return E_OUTOFMEMORY;
188
189     instr_ptr(ctx, instr)->arg1.dbl = dbl;
190     return S_OK;
191 }
192
193 static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
194 {
195     unsigned instr;
196
197     instr = push_instr(ctx, op);
198     if(instr == -1)
199         return E_OUTOFMEMORY;
200
201     instr_ptr(ctx, instr)->arg1.uint = arg;
202     return S_OK;
203 }
204
205 static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
206 {
207     HRESULT hres;
208
209     hres = compile_expression(ctx, expr->expression1);
210     if(FAILED(hres))
211         return hres;
212
213     hres = compile_expression(ctx, expr->expression2);
214     if(FAILED(hres))
215         return hres;
216
217     return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
218 }
219
220 static HRESULT compile_unary_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op)
221 {
222     HRESULT hres;
223
224     hres = compile_expression(ctx, expr->expression);
225     if(FAILED(hres))
226         return hres;
227
228     return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
229 }
230
231 /* ECMA-262 3rd Edition    11.2.1 */
232 static HRESULT compile_member_expression(compiler_ctx_t *ctx, member_expression_t *expr)
233 {
234     HRESULT hres;
235
236     hres = compile_expression(ctx, expr->expression);
237     if(FAILED(hres))
238         return hres;
239
240     return push_instr_bstr(ctx, OP_member, expr->identifier);
241 }
242
243 static inline BOOL is_memberid_expr(expression_type_t type)
244 {
245     return type == EXPR_IDENT || type == EXPR_MEMBER || type == EXPR_ARRAY;
246 }
247
248 static HRESULT compile_memberid_expression(compiler_ctx_t *ctx, expression_t *expr, unsigned flags)
249 {
250     HRESULT hres = S_OK;
251
252     switch(expr->type) {
253     case EXPR_IDENT: {
254         identifier_expression_t *ident_expr = (identifier_expression_t*)expr;
255
256         hres = push_instr_bstr_uint(ctx, OP_identid, ident_expr->identifier, flags);
257         break;
258     }
259     case EXPR_ARRAY: {
260         binary_expression_t *array_expr = (binary_expression_t*)expr;
261
262         hres = compile_expression(ctx, array_expr->expression1);
263         if(FAILED(hres))
264             return hres;
265
266         hres = compile_expression(ctx, array_expr->expression2);
267         if(FAILED(hres))
268             return hres;
269
270         hres = push_instr_uint(ctx, OP_memberid, flags);
271         break;
272     }
273     case EXPR_MEMBER: {
274         member_expression_t *member_expr = (member_expression_t*)expr;
275
276         hres = compile_expression(ctx, member_expr->expression);
277         if(FAILED(hres))
278             return hres;
279
280         /* FIXME: Potential optimization */
281         hres = push_instr_str(ctx, OP_str, member_expr->identifier);
282         if(FAILED(hres))
283             return hres;
284
285         hres = push_instr_uint(ctx, OP_memberid, flags);
286         break;
287     }
288     default:
289         assert(0);
290     }
291
292     return hres;
293 }
294
295 static HRESULT compile_increment_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op, int n)
296 {
297     HRESULT hres;
298
299     if(!is_memberid_expr(expr->expression->type)) {
300         hres = compile_expression(ctx, expr->expression);
301         if(FAILED(hres))
302             return hres;
303
304         return push_instr_uint(ctx, OP_throw, JS_E_ILLEGAL_ASSIGN);
305     }
306
307     hres = compile_memberid_expression(ctx, expr->expression, fdexNameEnsure);
308     if(FAILED(hres))
309         return hres;
310
311     return push_instr_int(ctx, op, n);
312 }
313
314 /* ECMA-262 3rd Edition    11.14 */
315 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr)
316 {
317     HRESULT hres;
318
319     hres = compile_expression(ctx, expr->expression1);
320     if(FAILED(hres))
321         return hres;
322
323     if(push_instr(ctx, OP_pop) == -1)
324         return E_OUTOFMEMORY;
325
326     return compile_expression(ctx, expr->expression2);
327 }
328
329 /* ECMA-262 3rd Edition    11.11 */
330 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
331 {
332     unsigned instr;
333     HRESULT hres;
334
335     hres = compile_expression(ctx, expr->expression1);
336     if(FAILED(hres))
337         return hres;
338
339     instr = push_instr(ctx, op);
340     if(instr == -1)
341         return E_OUTOFMEMORY;
342
343     hres = compile_expression(ctx, expr->expression2);
344     if(FAILED(hres))
345         return hres;
346
347     instr_ptr(ctx, instr)->arg1.uint = ctx->code_off;
348     return S_OK;
349 }
350
351 /* ECMA-262 3rd Edition    11.12 */
352 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
353 {
354     unsigned jmp_false, jmp_end;
355     HRESULT hres;
356
357     hres = compile_expression(ctx, expr->expression);
358     if(FAILED(hres))
359         return hres;
360
361     jmp_false = push_instr(ctx, OP_jmp_z);
362     if(jmp_false == -1)
363         return E_OUTOFMEMORY;
364
365     hres = compile_expression(ctx, expr->true_expression);
366     if(FAILED(hres))
367         return hres;
368
369     jmp_end = push_instr(ctx, OP_jmp);
370     if(jmp_end == -1)
371         return E_OUTOFMEMORY;
372
373     instr_ptr(ctx, jmp_false)->arg1.uint = ctx->code_off;
374     if(push_instr(ctx, OP_pop) == -1)
375         return E_OUTOFMEMORY;
376
377     hres = compile_expression(ctx, expr->false_expression);
378     if(FAILED(hres))
379         return hres;
380
381     instr_ptr(ctx, jmp_end)->arg1.uint = ctx->code_off;
382     return S_OK;
383 }
384
385 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
386 {
387     unsigned arg_cnt = 0;
388     argument_t *arg;
389     HRESULT hres;
390
391     hres = compile_expression(ctx, expr->expression);
392     if(FAILED(hres))
393         return hres;
394
395     for(arg = expr->argument_list; arg; arg = arg->next) {
396         hres = compile_expression(ctx, arg->expr);
397         if(FAILED(hres))
398             return hres;
399         arg_cnt++;
400     }
401
402     return push_instr_int(ctx, OP_new, arg_cnt);
403 }
404
405 static HRESULT compile_interp_fallback(compiler_ctx_t *ctx, expression_t *expr)
406 {
407     unsigned instr;
408
409     instr = push_instr(ctx, OP_tree);
410     if(instr == -1)
411         return E_OUTOFMEMORY;
412
413     instr_ptr(ctx, instr)->arg1.expr = expr;
414     return S_OK;
415 }
416
417 static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL *no_ret)
418 {
419     unsigned arg_cnt = 0;
420     argument_t *arg;
421     unsigned instr;
422     jsop_t op;
423     HRESULT hres;
424
425     if(is_memberid_expr(expr->expression->type)) {
426         op = OP_call_member;
427         hres = compile_memberid_expression(ctx, expr->expression, 0);
428     }else {
429         op = OP_call;
430         hres = compile_expression(ctx, expr->expression);
431     }
432
433     if(FAILED(hres))
434         return hres;
435
436     for(arg = expr->argument_list; arg; arg = arg->next) {
437         hres = compile_expression(ctx, arg->expr);
438         if(FAILED(hres))
439             return hres;
440         arg_cnt++;
441     }
442
443     instr = push_instr(ctx, op);
444     if(instr == -1)
445         return E_OUTOFMEMORY;
446
447     instr_ptr(ctx, instr)->arg1.uint = arg_cnt;
448     instr_ptr(ctx, instr)->arg2.lng = no_ret == NULL;
449     if(no_ret)
450         *no_ret = TRUE;
451     return S_OK;
452 }
453
454 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
455 {
456     HRESULT hres;
457
458     switch(expr->expression->type) {
459     case EXPR_ARRAY: {
460         binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
461
462         hres = compile_expression(ctx, array_expr->expression1);
463         if(FAILED(hres))
464             return hres;
465
466         hres = compile_expression(ctx, array_expr->expression2);
467         if(FAILED(hres))
468             return hres;
469
470         if(push_instr(ctx, OP_delete) == -1)
471             return E_OUTOFMEMORY;
472         break;
473     }
474     case EXPR_MEMBER: {
475         member_expression_t *member_expr = (member_expression_t*)expr->expression;
476
477         hres = compile_expression(ctx, member_expr->expression);
478         if(FAILED(hres))
479             return hres;
480
481         /* FIXME: Potential optimization */
482         hres = push_instr_str(ctx, OP_str, member_expr->identifier);
483         if(FAILED(hres))
484             return hres;
485
486         if(push_instr(ctx, OP_delete) == -1)
487             return E_OUTOFMEMORY;
488         break;
489     }
490     default:
491         expr->expr.eval = delete_expression_eval;
492         return compile_interp_fallback(ctx, &expr->expr);
493     }
494
495     return S_OK;
496 }
497
498 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
499 {
500     HRESULT hres;
501
502     if(!is_memberid_expr(expr->expression1->type)) {
503         hres = compile_expression(ctx, expr->expression1);
504         if(FAILED(hres))
505             return hres;
506
507         hres = compile_expression(ctx, expr->expression2);
508         if(FAILED(hres))
509             return hres;
510
511         if(op != OP_LAST && push_instr(ctx, op) == -1)
512             return E_OUTOFMEMORY;
513
514         return push_instr_uint(ctx, OP_throw, JS_E_ILLEGAL_ASSIGN);
515     }
516
517     hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
518     if(FAILED(hres))
519         return hres;
520
521     if(op != OP_LAST && push_instr(ctx, OP_refval) == -1)
522         return E_OUTOFMEMORY;
523
524     hres = compile_expression(ctx, expr->expression2);
525     if(FAILED(hres))
526         return hres;
527
528     if(op != OP_LAST && push_instr(ctx, op) == -1)
529         return E_OUTOFMEMORY;
530
531     if(push_instr(ctx, OP_assign) == -1)
532         return E_OUTOFMEMORY;
533
534     return S_OK;
535 }
536
537 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
538 {
539     switch(literal->type) {
540     case LT_BOOL:
541         return push_instr_int(ctx, OP_bool, literal->u.bval);
542     case LT_DOUBLE:
543         return push_instr_double(ctx, OP_double, literal->u.dval);
544     case LT_INT:
545         return push_instr_int(ctx, OP_int, literal->u.lval);
546     case LT_NULL:
547         return push_instr(ctx, OP_null);
548     case LT_STRING:
549         return push_instr_str(ctx, OP_str, literal->u.wstr);
550     case LT_REGEXP: {
551         unsigned instr;
552         WCHAR *str;
553
554         str = compiler_alloc(ctx->code, (literal->u.regexp.str_len+1)*sizeof(WCHAR));
555         if(!str)
556             return E_OUTOFMEMORY;
557         memcpy(str, literal->u.regexp.str, literal->u.regexp.str_len*sizeof(WCHAR));
558         str[literal->u.regexp.str_len] = 0;
559
560         instr = push_instr(ctx, OP_regexp);
561         if(instr == -1)
562             return E_OUTOFMEMORY;
563
564         instr_ptr(ctx, instr)->arg1.str = str;
565         instr_ptr(ctx, instr)->arg2.lng = literal->u.regexp.flags;
566         return S_OK;
567     }
568     default:
569         assert(0);
570     }
571 }
572
573 static HRESULT compile_expression_noret(compiler_ctx_t *ctx, expression_t *expr, BOOL *no_ret)
574 {
575     switch(expr->type) {
576     case EXPR_ADD:
577         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
578     case EXPR_AND:
579         return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_jmp_z);
580     case EXPR_ARRAY:
581         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
582     case EXPR_ASSIGN:
583         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
584     case EXPR_ASSIGNADD:
585         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
586     case EXPR_ASSIGNSUB:
587         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
588     case EXPR_ASSIGNMUL:
589         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
590     case EXPR_ASSIGNDIV:
591         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
592     case EXPR_ASSIGNMOD:
593         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
594     case EXPR_ASSIGNOR:
595         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
596     case EXPR_ASSIGNXOR:
597         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
598     case EXPR_BITNEG:
599         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
600     case EXPR_BOR:
601         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
602     case EXPR_CALL:
603         return compile_call_expression(ctx, (call_expression_t*)expr, no_ret);
604     case EXPR_COMMA:
605         return compile_comma_expression(ctx, (binary_expression_t*)expr);
606     case EXPR_COND:
607         return compile_conditional_expression(ctx, (conditional_expression_t*)expr);
608     case EXPR_DELETE:
609         return compile_delete_expression(ctx, (unary_expression_t*)expr);
610     case EXPR_DIV:
611         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
612     case EXPR_EQ:
613         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
614     case EXPR_EQEQ:
615         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
616     case EXPR_GREATER:
617         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
618     case EXPR_GREATEREQ:
619         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
620     case EXPR_IDENT:
621         return push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
622     case EXPR_IN:
623         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
624     case EXPR_LESS:
625         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
626     case EXPR_LESSEQ:
627         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
628     case EXPR_LITERAL:
629         return compile_literal(ctx, ((literal_expression_t*)expr)->literal);
630     case EXPR_LOGNEG:
631         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
632     case EXPR_MEMBER:
633         return compile_member_expression(ctx, (member_expression_t*)expr);
634     case EXPR_MINUS:
635         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
636     case EXPR_MOD:
637         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
638     case EXPR_MUL:
639         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
640     case EXPR_NEW:
641         return compile_new_expression(ctx, (call_expression_t*)expr);
642     case EXPR_NOTEQ:
643         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
644     case EXPR_NOTEQEQ:
645         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
646     case EXPR_OR:
647         return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_jmp_nz);
648     case EXPR_PLUS:
649         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
650     case EXPR_POSTDEC:
651         return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
652     case EXPR_POSTINC:
653         return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
654     case EXPR_PREDEC:
655         return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
656     case EXPR_PREINC:
657         return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
658     case EXPR_SUB:
659         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
660     case EXPR_THIS:
661         return push_instr(ctx, OP_this) == -1 ? E_OUTOFMEMORY : S_OK;
662     case EXPR_VOID:
663         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
664     case EXPR_BXOR:
665         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
666     default:
667         assert(expr->eval != compiled_expression_eval);
668         return compile_interp_fallback(ctx, expr);
669     }
670
671     return S_OK;
672 }
673
674 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr)
675 {
676     return compile_expression_noret(ctx, expr, NULL);
677 }
678
679 void release_bytecode(bytecode_t *code)
680 {
681     unsigned i;
682
683     for(i=0; i < code->bstr_cnt; i++)
684         SysFreeString(code->bstr_pool[i]);
685
686     jsheap_free(&code->heap);
687     heap_free(code->bstr_pool);
688     heap_free(code->instrs);
689     heap_free(code);
690 }
691
692 void release_compiler(compiler_ctx_t *ctx)
693 {
694     heap_free(ctx);
695 }
696
697 HRESULT compile_subscript(parser_ctx_t *parser, expression_t *expr, BOOL do_ret, unsigned *ret_off)
698 {
699     BOOL no_ret = FALSE;
700     HRESULT hres;
701
702     if(!parser->code) {
703         parser->code = heap_alloc_zero(sizeof(bytecode_t));
704         if(!parser->code)
705             return E_OUTOFMEMORY;
706         jsheap_init(&parser->code->heap);
707     }
708
709     if(!parser->compiler) {
710         parser->compiler = heap_alloc_zero(sizeof(compiler_ctx_t));
711         if(!parser->compiler)
712             return E_OUTOFMEMORY;
713
714         parser->compiler->parser = parser;
715         parser->compiler->code = parser->code;
716     }
717
718     *ret_off = parser->compiler->code_off;
719     hres = compile_expression_noret(parser->compiler, expr, do_ret ? NULL : &no_ret);
720     if(FAILED(hres))
721         return hres;
722
723     return push_instr(parser->compiler, OP_ret) == -1 ? E_OUTOFMEMORY : S_OK;
724 }