jscript: Use bytecode for calls on identifier and member expressions.
[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         array_expression_t *array_expr = (array_expression_t*)expr;
261
262         hres = compile_expression(ctx, array_expr->member_expr);
263         if(FAILED(hres))
264             return hres;
265
266         hres = compile_expression(ctx, array_expr->expression);
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 /* ECMA-262 3rd Edition    11.14 */
296 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr)
297 {
298     HRESULT hres;
299
300     hres = compile_expression(ctx, expr->expression1);
301     if(FAILED(hres))
302         return hres;
303
304     if(push_instr(ctx, OP_pop) == -1)
305         return E_OUTOFMEMORY;
306
307     return compile_expression(ctx, expr->expression2);
308 }
309
310 /* ECMA-262 3rd Edition    11.11 */
311 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
312 {
313     unsigned instr;
314     HRESULT hres;
315
316     hres = compile_expression(ctx, expr->expression1);
317     if(FAILED(hres))
318         return hres;
319
320     instr = push_instr(ctx, op);
321     if(instr == -1)
322         return E_OUTOFMEMORY;
323
324     hres = compile_expression(ctx, expr->expression2);
325     if(FAILED(hres))
326         return hres;
327
328     instr_ptr(ctx, instr)->arg1.uint = ctx->code_off;
329     return S_OK;
330 }
331
332 /* ECMA-262 3rd Edition    11.12 */
333 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
334 {
335     unsigned jmp_false, jmp_end;
336     HRESULT hres;
337
338     hres = compile_expression(ctx, expr->expression);
339     if(FAILED(hres))
340         return hres;
341
342     jmp_false = push_instr(ctx, OP_jmp_z);
343     if(jmp_false == -1)
344         return E_OUTOFMEMORY;
345
346     hres = compile_expression(ctx, expr->true_expression);
347     if(FAILED(hres))
348         return hres;
349
350     jmp_end = push_instr(ctx, OP_jmp);
351     if(jmp_end == -1)
352         return E_OUTOFMEMORY;
353
354     instr_ptr(ctx, jmp_false)->arg1.uint = ctx->code_off;
355     if(push_instr(ctx, OP_pop) == -1)
356         return E_OUTOFMEMORY;
357
358     hres = compile_expression(ctx, expr->false_expression);
359     if(FAILED(hres))
360         return hres;
361
362     instr_ptr(ctx, jmp_end)->arg1.uint = ctx->code_off;
363     return S_OK;
364 }
365
366 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
367 {
368     unsigned arg_cnt = 0;
369     argument_t *arg;
370     HRESULT hres;
371
372     hres = compile_expression(ctx, expr->expression);
373     if(FAILED(hres))
374         return hres;
375
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++;
381     }
382
383     return push_instr_int(ctx, OP_new, arg_cnt);
384 }
385
386 static HRESULT compile_interp_fallback(compiler_ctx_t *ctx, expression_t *expr)
387 {
388     unsigned instr;
389
390     instr = push_instr(ctx, OP_tree);
391     if(instr == -1)
392         return E_OUTOFMEMORY;
393
394     instr_ptr(ctx, instr)->arg1.expr = expr;
395     return S_OK;
396 }
397
398 static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL *no_ret)
399 {
400     unsigned arg_cnt = 0;
401     argument_t *arg;
402     unsigned instr;
403     HRESULT hres;
404
405     if(!is_memberid_expr(expr->expression->type)) {
406         expr->expr.eval = call_expression_eval;
407         return compile_interp_fallback(ctx, &expr->expr);
408     }
409
410     hres = compile_memberid_expression(ctx, expr->expression, 0);
411     if(FAILED(hres))
412         return hres;
413
414     for(arg = expr->argument_list; arg; arg = arg->next) {
415         hres = compile_expression(ctx, arg->expr);
416         if(FAILED(hres))
417             return hres;
418         arg_cnt++;
419     }
420
421     instr = push_instr(ctx, OP_call_member);
422     if(instr == -1)
423         return E_OUTOFMEMORY;
424
425     instr_ptr(ctx, instr)->arg1.uint = arg_cnt;
426     instr_ptr(ctx, instr)->arg2.lng = no_ret == NULL;
427     if(no_ret)
428         *no_ret = TRUE;
429     return S_OK;
430 }
431
432 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
433 {
434     HRESULT hres;
435
436     switch(expr->expression->type) {
437     case EXPR_ARRAY: {
438         array_expression_t *array_expr = (array_expression_t*)expr->expression;
439
440         hres = compile_expression(ctx, array_expr->member_expr);
441         if(FAILED(hres))
442             return hres;
443
444         hres = compile_expression(ctx, array_expr->expression);
445         if(FAILED(hres))
446             return hres;
447
448         if(push_instr(ctx, OP_delete) == -1)
449             return E_OUTOFMEMORY;
450         break;
451     }
452     case EXPR_MEMBER: {
453         member_expression_t *member_expr = (member_expression_t*)expr->expression;
454
455         hres = compile_expression(ctx, member_expr->expression);
456         if(FAILED(hres))
457             return hres;
458
459         /* FIXME: Potential optimization */
460         hres = push_instr_str(ctx, OP_str, member_expr->identifier);
461         if(FAILED(hres))
462             return hres;
463
464         if(push_instr(ctx, OP_delete) == -1)
465             return E_OUTOFMEMORY;
466         break;
467     }
468     default:
469         expr->expr.eval = delete_expression_eval;
470         return compile_interp_fallback(ctx, &expr->expr);
471     }
472
473     return S_OK;
474 }
475
476 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
477 {
478     HRESULT hres;
479
480     if(!is_memberid_expr(expr->expression1->type)) {
481         hres = compile_expression(ctx, expr->expression1);
482         if(FAILED(hres))
483             return hres;
484
485         hres = compile_expression(ctx, expr->expression2);
486         if(FAILED(hres))
487             return hres;
488
489         if(op != OP_LAST && push_instr(ctx, op) == -1)
490             return E_OUTOFMEMORY;
491
492         return push_instr_uint(ctx, OP_throw, JS_E_ILLEGAL_ASSIGN);
493     }
494
495     hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
496     if(FAILED(hres))
497         return hres;
498
499     if(op != OP_LAST && push_instr(ctx, OP_refval) == -1)
500         return E_OUTOFMEMORY;
501
502     hres = compile_expression(ctx, expr->expression2);
503     if(FAILED(hres))
504         return hres;
505
506     if(op != OP_LAST && push_instr(ctx, op) == -1)
507         return E_OUTOFMEMORY;
508
509     if(push_instr(ctx, OP_assign) == -1)
510         return E_OUTOFMEMORY;
511
512     return S_OK;
513 }
514
515 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
516 {
517     switch(literal->type) {
518     case LT_BOOL:
519         return push_instr_int(ctx, OP_bool, literal->u.bval);
520     case LT_DOUBLE:
521         return push_instr_double(ctx, OP_double, literal->u.dval);
522     case LT_INT:
523         return push_instr_int(ctx, OP_int, literal->u.lval);
524     case LT_NULL:
525         return push_instr(ctx, OP_null);
526     case LT_STRING:
527         return push_instr_str(ctx, OP_str, literal->u.wstr);
528     case LT_REGEXP: {
529         unsigned instr;
530         WCHAR *str;
531
532         str = compiler_alloc(ctx->code, (literal->u.regexp.str_len+1)*sizeof(WCHAR));
533         if(!str)
534             return E_OUTOFMEMORY;
535         memcpy(str, literal->u.regexp.str, literal->u.regexp.str_len*sizeof(WCHAR));
536         str[literal->u.regexp.str_len] = 0;
537
538         instr = push_instr(ctx, OP_regexp);
539         if(instr == -1)
540             return E_OUTOFMEMORY;
541
542         instr_ptr(ctx, instr)->arg1.str = str;
543         instr_ptr(ctx, instr)->arg2.lng = literal->u.regexp.flags;
544         return S_OK;
545     }
546     default:
547         assert(0);
548     }
549 }
550
551 static HRESULT compile_expression_noret(compiler_ctx_t *ctx, expression_t *expr, BOOL *no_ret)
552 {
553     switch(expr->type) {
554     case EXPR_ADD:
555         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
556     case EXPR_AND:
557         return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_jmp_z);
558     case EXPR_ASSIGN:
559         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
560     case EXPR_ASSIGNADD:
561         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
562     case EXPR_ASSIGNSUB:
563         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
564     case EXPR_ASSIGNMUL:
565         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
566     case EXPR_ASSIGNDIV:
567         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
568     case EXPR_ASSIGNMOD:
569         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
570     case EXPR_ASSIGNOR:
571         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
572     case EXPR_ASSIGNXOR:
573         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
574     case EXPR_BITNEG:
575         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
576     case EXPR_BOR:
577         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
578     case EXPR_CALL:
579         return compile_call_expression(ctx, (call_expression_t*)expr, no_ret);
580     case EXPR_COMMA:
581         return compile_comma_expression(ctx, (binary_expression_t*)expr);
582     case EXPR_COND:
583         return compile_conditional_expression(ctx, (conditional_expression_t*)expr);
584     case EXPR_DELETE:
585         return compile_delete_expression(ctx, (unary_expression_t*)expr);
586     case EXPR_DIV:
587         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
588     case EXPR_EQ:
589         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
590     case EXPR_EQEQ:
591         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
592     case EXPR_GREATER:
593         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
594     case EXPR_GREATEREQ:
595         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
596     case EXPR_IDENT:
597         return push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
598     case EXPR_IN:
599         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
600     case EXPR_LESS:
601         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
602     case EXPR_LESSEQ:
603         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
604     case EXPR_LITERAL:
605         return compile_literal(ctx, ((literal_expression_t*)expr)->literal);
606     case EXPR_LOGNEG:
607         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
608     case EXPR_MEMBER:
609         return compile_member_expression(ctx, (member_expression_t*)expr);
610     case EXPR_MINUS:
611         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
612     case EXPR_MOD:
613         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
614     case EXPR_MUL:
615         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
616     case EXPR_NEW:
617         return compile_new_expression(ctx, (call_expression_t*)expr);
618     case EXPR_NOTEQ:
619         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
620     case EXPR_NOTEQEQ:
621         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
622     case EXPR_OR:
623         return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_jmp_nz);
624     case EXPR_PLUS:
625         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
626     case EXPR_SUB:
627         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
628     case EXPR_THIS:
629         return push_instr(ctx, OP_this) == -1 ? E_OUTOFMEMORY : S_OK;
630     case EXPR_VOID:
631         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
632     case EXPR_BXOR:
633         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
634     default:
635         assert(expr->eval != compiled_expression_eval);
636         return compile_interp_fallback(ctx, expr);
637     }
638
639     return S_OK;
640 }
641
642 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr)
643 {
644     return compile_expression_noret(ctx, expr, NULL);
645 }
646
647 void release_bytecode(bytecode_t *code)
648 {
649     unsigned i;
650
651     for(i=0; i < code->bstr_cnt; i++)
652         SysFreeString(code->bstr_pool[i]);
653
654     jsheap_free(&code->heap);
655     heap_free(code->bstr_pool);
656     heap_free(code->instrs);
657     heap_free(code);
658 }
659
660 void release_compiler(compiler_ctx_t *ctx)
661 {
662     heap_free(ctx);
663 }
664
665 HRESULT compile_subscript(parser_ctx_t *parser, expression_t *expr, BOOL do_ret, unsigned *ret_off)
666 {
667     BOOL no_ret = FALSE;
668     HRESULT hres;
669
670     if(!parser->code) {
671         parser->code = heap_alloc_zero(sizeof(bytecode_t));
672         if(!parser->code)
673             return E_OUTOFMEMORY;
674         jsheap_init(&parser->code->heap);
675     }
676
677     if(!parser->compiler) {
678         parser->compiler = heap_alloc_zero(sizeof(compiler_ctx_t));
679         if(!parser->compiler)
680             return E_OUTOFMEMORY;
681
682         parser->compiler->parser = parser;
683         parser->compiler->code = parser->code;
684     }
685
686     *ret_off = parser->compiler->code_off;
687     hres = compile_expression_noret(parser->compiler, expr, do_ret ? NULL : &no_ret);
688     if(FAILED(hres))
689         return hres;
690
691     return push_instr(parser->compiler, OP_ret) == -1 ? E_OUTOFMEMORY : S_OK;
692 }