39ba79acc518098f4e249d3cb8b43b3b3de75855
[reactos.git] / reactos / dll / win32 / 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 "jscript.h"
20
21 WINE_DECLARE_DEBUG_CHANNEL(jscript_disas);
22
23 typedef struct _statement_ctx_t {
24 unsigned stack_use;
25 BOOL using_scope;
26 BOOL using_except;
27
28 unsigned break_label;
29 unsigned continue_label;
30
31 const labelled_statement_t *labelled_stat;
32
33 struct _statement_ctx_t *next;
34 } statement_ctx_t;
35
36 typedef struct {
37 parser_ctx_t *parser;
38 bytecode_t *code;
39
40 BOOL from_eval;
41
42 unsigned code_off;
43 unsigned code_size;
44
45 unsigned *labels;
46 unsigned labels_size;
47 unsigned labels_cnt;
48
49 statement_ctx_t *stat_ctx;
50 function_code_t *func;
51
52 variable_declaration_t *var_head;
53 variable_declaration_t *var_tail;
54
55 function_expression_t *func_head;
56 function_expression_t *func_tail;
57 } compiler_ctx_t;
58
59 static const struct {
60 const char *op_str;
61 instr_arg_type_t arg1_type;
62 instr_arg_type_t arg2_type;
63 } instr_info[] = {
64 #define X(n,a,b,c) {#n,b,c},
65 OP_LIST
66 #undef X
67 };
68
69 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
70 {
71 switch(type) {
72 case ARG_STR:
73 TRACE_(jscript_disas)("\t%s", debugstr_jsstr(arg->str));
74 break;
75 case ARG_BSTR:
76 TRACE_(jscript_disas)("\t%s", debugstr_wn(arg->bstr, SysStringLen(arg->bstr)));
77 break;
78 case ARG_INT:
79 TRACE_(jscript_disas)("\t%d", arg->uint);
80 break;
81 case ARG_UINT:
82 case ARG_ADDR:
83 TRACE_(jscript_disas)("\t%u", arg->uint);
84 break;
85 case ARG_FUNC:
86 case ARG_NONE:
87 break;
88 DEFAULT_UNREACHABLE;
89 }
90 }
91
92 static void dump_code(compiler_ctx_t *ctx, unsigned off)
93 {
94 instr_t *instr;
95
96 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
97 TRACE_(jscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
98 if(instr_info[instr->op].arg1_type == ARG_DBL) {
99 TRACE_(jscript_disas)("\t%lf", instr->u.dbl);
100 }else {
101 dump_instr_arg(instr_info[instr->op].arg1_type, instr->u.arg);
102 dump_instr_arg(instr_info[instr->op].arg2_type, instr->u.arg+1);
103 }
104 TRACE_(jscript_disas)("\n");
105 }
106 }
107
108 static HRESULT compile_expression(compiler_ctx_t*,expression_t*,BOOL);
109 static HRESULT compile_statement(compiler_ctx_t*,statement_ctx_t*,statement_t*);
110
111 static inline void *compiler_alloc(bytecode_t *code, size_t size)
112 {
113 return heap_pool_alloc(&code->heap, size);
114 }
115
116 static jsstr_t *compiler_alloc_string_len(compiler_ctx_t *ctx, const WCHAR *str, unsigned len)
117 {
118 jsstr_t *new_str;
119
120 if(!ctx->code->str_pool_size) {
121 ctx->code->str_pool = heap_alloc(8 * sizeof(jsstr_t*));
122 if(!ctx->code->str_pool)
123 return NULL;
124 ctx->code->str_pool_size = 8;
125 }else if(ctx->code->str_pool_size == ctx->code->str_cnt) {
126 jsstr_t **new_pool;
127
128 new_pool = heap_realloc(ctx->code->str_pool, ctx->code->str_pool_size*2*sizeof(jsstr_t*));
129 if(!new_pool)
130 return NULL;
131
132 ctx->code->str_pool = new_pool;
133 ctx->code->str_pool_size *= 2;
134 }
135
136 new_str = jsstr_alloc_len(str, len);
137 if(!new_str)
138 return NULL;
139
140 ctx->code->str_pool[ctx->code->str_cnt++] = new_str;
141 return new_str;
142 }
143
144 static jsstr_t *compiler_alloc_string(compiler_ctx_t *ctx, const WCHAR *str)
145 {
146 return compiler_alloc_string_len(ctx, str, strlenW(str));
147 }
148
149 static BOOL ensure_bstr_slot(compiler_ctx_t *ctx)
150 {
151 if(!ctx->code->bstr_pool_size) {
152 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
153 if(!ctx->code->bstr_pool)
154 return FALSE;
155 ctx->code->bstr_pool_size = 8;
156 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
157 BSTR *new_pool;
158
159 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
160 if(!new_pool)
161 return FALSE;
162
163 ctx->code->bstr_pool = new_pool;
164 ctx->code->bstr_pool_size *= 2;
165 }
166
167 return TRUE;
168 }
169
170 static BSTR compiler_alloc_bstr(compiler_ctx_t *ctx, const WCHAR *str)
171 {
172 if(!ensure_bstr_slot(ctx))
173 return NULL;
174
175 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
176 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
177 return NULL;
178
179 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
180 }
181
182 static BSTR compiler_alloc_bstr_len(compiler_ctx_t *ctx, const WCHAR *str, size_t len)
183 {
184 if(!ensure_bstr_slot(ctx))
185 return NULL;
186
187 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocStringLen(str, len);
188 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
189 return NULL;
190
191 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
192 }
193
194 static unsigned push_instr(compiler_ctx_t *ctx, jsop_t op)
195 {
196 assert(ctx->code_size >= ctx->code_off);
197
198 if(ctx->code_size == ctx->code_off) {
199 instr_t *new_instrs;
200
201 new_instrs = heap_realloc(ctx->code->instrs, ctx->code_size*2*sizeof(instr_t));
202 if(!new_instrs)
203 return 0;
204
205 ctx->code->instrs = new_instrs;
206 ctx->code_size *= 2;
207 }
208
209 ctx->code->instrs[ctx->code_off].op = op;
210 return ctx->code_off++;
211 }
212
213 static inline instr_t *instr_ptr(compiler_ctx_t *ctx, unsigned off)
214 {
215 assert(off < ctx->code_off);
216 return ctx->code->instrs + off;
217 }
218
219 static HRESULT push_instr_int(compiler_ctx_t *ctx, jsop_t op, LONG arg)
220 {
221 unsigned instr;
222
223 instr = push_instr(ctx, op);
224 if(!instr)
225 return E_OUTOFMEMORY;
226
227 instr_ptr(ctx, instr)->u.arg->lng = arg;
228 return S_OK;
229 }
230
231 static HRESULT push_instr_str(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
232 {
233 unsigned instr;
234 jsstr_t *str;
235
236 str = compiler_alloc_string(ctx, arg);
237 if(!str)
238 return E_OUTOFMEMORY;
239
240 instr = push_instr(ctx, op);
241 if(!instr)
242 return E_OUTOFMEMORY;
243
244 instr_ptr(ctx, instr)->u.arg->str = str;
245 return S_OK;
246 }
247
248 static HRESULT push_instr_bstr(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
249 {
250 unsigned instr;
251 WCHAR *str;
252
253 str = compiler_alloc_bstr(ctx, arg);
254 if(!str)
255 return E_OUTOFMEMORY;
256
257 instr = push_instr(ctx, op);
258 if(!instr)
259 return E_OUTOFMEMORY;
260
261 instr_ptr(ctx, instr)->u.arg->bstr = str;
262 return S_OK;
263 }
264
265 static HRESULT push_instr_bstr_uint(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg1, unsigned arg2)
266 {
267 unsigned instr;
268 WCHAR *str;
269
270 str = compiler_alloc_bstr(ctx, arg1);
271 if(!str)
272 return E_OUTOFMEMORY;
273
274 instr = push_instr(ctx, op);
275 if(!instr)
276 return E_OUTOFMEMORY;
277
278 instr_ptr(ctx, instr)->u.arg[0].bstr = str;
279 instr_ptr(ctx, instr)->u.arg[1].uint = arg2;
280 return S_OK;
281 }
282
283 static HRESULT push_instr_uint_str(compiler_ctx_t *ctx, jsop_t op, unsigned arg1, const WCHAR *arg2)
284 {
285 unsigned instr;
286 jsstr_t *str;
287
288 str = compiler_alloc_string(ctx, arg2);
289 if(!str)
290 return E_OUTOFMEMORY;
291
292 instr = push_instr(ctx, op);
293 if(!instr)
294 return E_OUTOFMEMORY;
295
296 instr_ptr(ctx, instr)->u.arg[0].uint = arg1;
297 instr_ptr(ctx, instr)->u.arg[1].str = str;
298 return S_OK;
299 }
300
301 static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
302 {
303 unsigned instr;
304
305 instr = push_instr(ctx, op);
306 if(!instr)
307 return E_OUTOFMEMORY;
308
309 instr_ptr(ctx, instr)->u.dbl = arg;
310 return S_OK;
311 }
312
313 static inline void set_arg_uint(compiler_ctx_t *ctx, unsigned instr, unsigned arg)
314 {
315 instr_ptr(ctx, instr)->u.arg->uint = arg;
316 }
317
318 static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
319 {
320 unsigned instr;
321
322 instr = push_instr(ctx, op);
323 if(!instr)
324 return E_OUTOFMEMORY;
325
326 set_arg_uint(ctx, instr, arg);
327 return S_OK;
328 }
329
330 static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
331 {
332 HRESULT hres;
333
334 hres = compile_expression(ctx, expr->expression1, TRUE);
335 if(FAILED(hres))
336 return hres;
337
338 hres = compile_expression(ctx, expr->expression2, TRUE);
339 if(FAILED(hres))
340 return hres;
341
342 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
343 }
344
345 static HRESULT compile_unary_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op)
346 {
347 HRESULT hres;
348
349 hres = compile_expression(ctx, expr->expression, TRUE);
350 if(FAILED(hres))
351 return hres;
352
353 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
354 }
355
356 /* ECMA-262 3rd Edition 11.2.1 */
357 static HRESULT compile_member_expression(compiler_ctx_t *ctx, member_expression_t *expr)
358 {
359 HRESULT hres;
360
361 hres = compile_expression(ctx, expr->expression, TRUE);
362 if(FAILED(hres))
363 return hres;
364
365 return push_instr_bstr(ctx, OP_member, expr->identifier);
366 }
367
368 #define LABEL_FLAG 0x80000000
369
370 static unsigned alloc_label(compiler_ctx_t *ctx)
371 {
372 if(!ctx->labels_size) {
373 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
374 if(!ctx->labels)
375 return 0;
376 ctx->labels_size = 8;
377 }else if(ctx->labels_size == ctx->labels_cnt) {
378 unsigned *new_labels;
379
380 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
381 if(!new_labels)
382 return 0;
383
384 ctx->labels = new_labels;
385 ctx->labels_size *= 2;
386 }
387
388 return ctx->labels_cnt++ | LABEL_FLAG;
389 }
390
391 static void label_set_addr(compiler_ctx_t *ctx, unsigned label)
392 {
393 assert(label & LABEL_FLAG);
394 ctx->labels[label & ~LABEL_FLAG] = ctx->code_off;
395 }
396
397 static inline BOOL is_memberid_expr(expression_type_t type)
398 {
399 return type == EXPR_IDENT || type == EXPR_MEMBER || type == EXPR_ARRAY;
400 }
401
402 static HRESULT compile_memberid_expression(compiler_ctx_t *ctx, expression_t *expr, unsigned flags)
403 {
404 HRESULT hres = S_OK;
405
406 switch(expr->type) {
407 case EXPR_IDENT: {
408 identifier_expression_t *ident_expr = (identifier_expression_t*)expr;
409
410 hres = push_instr_bstr_uint(ctx, OP_identid, ident_expr->identifier, flags);
411 break;
412 }
413 case EXPR_ARRAY: {
414 binary_expression_t *array_expr = (binary_expression_t*)expr;
415
416 hres = compile_expression(ctx, array_expr->expression1, TRUE);
417 if(FAILED(hres))
418 return hres;
419
420 hres = compile_expression(ctx, array_expr->expression2, TRUE);
421 if(FAILED(hres))
422 return hres;
423
424 hres = push_instr_uint(ctx, OP_memberid, flags);
425 break;
426 }
427 case EXPR_MEMBER: {
428 member_expression_t *member_expr = (member_expression_t*)expr;
429
430 hres = compile_expression(ctx, member_expr->expression, TRUE);
431 if(FAILED(hres))
432 return hres;
433
434 /* FIXME: Potential optimization */
435 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
436 if(FAILED(hres))
437 return hres;
438
439 hres = push_instr_uint(ctx, OP_memberid, flags);
440 break;
441 }
442 DEFAULT_UNREACHABLE;
443 }
444
445 return hres;
446 }
447
448 static HRESULT compile_increment_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op, int n)
449 {
450 HRESULT hres;
451
452 if(!is_memberid_expr(expr->expression->type)) {
453 hres = compile_expression(ctx, expr->expression, TRUE);
454 if(FAILED(hres))
455 return hres;
456
457 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
458 }
459
460 hres = compile_memberid_expression(ctx, expr->expression, fdexNameEnsure);
461 if(FAILED(hres))
462 return hres;
463
464 return push_instr_int(ctx, op, n);
465 }
466
467 /* ECMA-262 3rd Edition 11.14 */
468 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr, BOOL emit_ret)
469 {
470 HRESULT hres;
471
472 hres = compile_expression(ctx, expr->expression1, FALSE);
473 if(FAILED(hres))
474 return hres;
475
476 return compile_expression(ctx, expr->expression2, emit_ret);
477 }
478
479 /* ECMA-262 3rd Edition 11.11 */
480 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
481 {
482 unsigned instr;
483 HRESULT hres;
484
485 hres = compile_expression(ctx, expr->expression1, TRUE);
486 if(FAILED(hres))
487 return hres;
488
489 instr = push_instr(ctx, op);
490 if(!instr)
491 return E_OUTOFMEMORY;
492
493 hres = compile_expression(ctx, expr->expression2, TRUE);
494 if(FAILED(hres))
495 return hres;
496
497 set_arg_uint(ctx, instr, ctx->code_off);
498 return S_OK;
499 }
500
501 /* ECMA-262 3rd Edition 11.12 */
502 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
503 {
504 unsigned jmp_false, jmp_end;
505 HRESULT hres;
506
507 hres = compile_expression(ctx, expr->expression, TRUE);
508 if(FAILED(hres))
509 return hres;
510
511 jmp_false = push_instr(ctx, OP_cnd_z);
512 if(!jmp_false)
513 return E_OUTOFMEMORY;
514
515 hres = compile_expression(ctx, expr->true_expression, TRUE);
516 if(FAILED(hres))
517 return hres;
518
519 jmp_end = push_instr(ctx, OP_jmp);
520 if(!jmp_end)
521 return E_OUTOFMEMORY;
522
523 set_arg_uint(ctx, jmp_false, ctx->code_off);
524 hres = push_instr_uint(ctx, OP_pop, 1);
525 if(FAILED(hres))
526 return hres;
527
528 hres = compile_expression(ctx, expr->false_expression, TRUE);
529 if(FAILED(hres))
530 return hres;
531
532 set_arg_uint(ctx, jmp_end, ctx->code_off);
533 return S_OK;
534 }
535
536 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
537 {
538 unsigned arg_cnt = 0;
539 argument_t *arg;
540 HRESULT hres;
541
542 hres = compile_expression(ctx, expr->expression, TRUE);
543 if(FAILED(hres))
544 return hres;
545
546 for(arg = expr->argument_list; arg; arg = arg->next) {
547 hres = compile_expression(ctx, arg->expr, TRUE);
548 if(FAILED(hres))
549 return hres;
550 arg_cnt++;
551 }
552
553 hres = push_instr_uint(ctx, OP_new, arg_cnt);
554 if(FAILED(hres))
555 return hres;
556
557 hres = push_instr_uint(ctx, OP_pop, arg_cnt+1);
558 if(FAILED(hres))
559 return hres;
560
561 return push_instr(ctx, OP_push_ret) ? S_OK : E_OUTOFMEMORY;
562 }
563
564 static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL emit_ret)
565 {
566 unsigned arg_cnt = 0, extra_args;
567 argument_t *arg;
568 unsigned instr;
569 jsop_t op;
570 HRESULT hres;
571
572 if(is_memberid_expr(expr->expression->type)) {
573 op = OP_call_member;
574 extra_args = 2;
575 hres = compile_memberid_expression(ctx, expr->expression, 0);
576 }else {
577 op = OP_call;
578 extra_args = 1;
579 hres = compile_expression(ctx, expr->expression, TRUE);
580 }
581
582 if(FAILED(hres))
583 return hres;
584
585 for(arg = expr->argument_list; arg; arg = arg->next) {
586 hres = compile_expression(ctx, arg->expr, TRUE);
587 if(FAILED(hres))
588 return hres;
589 arg_cnt++;
590 }
591
592 instr = push_instr(ctx, op);
593 if(!instr)
594 return E_OUTOFMEMORY;
595
596 instr_ptr(ctx, instr)->u.arg[0].uint = arg_cnt;
597 instr_ptr(ctx, instr)->u.arg[1].lng = emit_ret;
598
599 hres = push_instr_uint(ctx, OP_pop, arg_cnt + extra_args);
600 if(FAILED(hres))
601 return hres;
602
603 return !emit_ret || push_instr(ctx, OP_push_ret) ? S_OK : E_OUTOFMEMORY;
604 }
605
606 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
607 {
608 HRESULT hres;
609
610 switch(expr->expression->type) {
611 case EXPR_ARRAY: {
612 binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
613
614 hres = compile_expression(ctx, array_expr->expression1, TRUE);
615 if(FAILED(hres))
616 return hres;
617
618 hres = compile_expression(ctx, array_expr->expression2, TRUE);
619 if(FAILED(hres))
620 return hres;
621
622 if(!push_instr(ctx, OP_delete))
623 return E_OUTOFMEMORY;
624 break;
625 }
626 case EXPR_MEMBER: {
627 member_expression_t *member_expr = (member_expression_t*)expr->expression;
628
629 hres = compile_expression(ctx, member_expr->expression, TRUE);
630 if(FAILED(hres))
631 return hres;
632
633 /* FIXME: Potential optimization */
634 hres = push_instr_str(ctx, OP_str, member_expr->identifier);
635 if(FAILED(hres))
636 return hres;
637
638 if(!push_instr(ctx, OP_delete))
639 return E_OUTOFMEMORY;
640 break;
641 }
642 case EXPR_IDENT:
643 return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
644 default: {
645 const WCHAR fixmeW[] = {'F','I','X','M','E',0};
646
647 WARN("invalid delete, unimplemented exception message\n");
648
649 hres = compile_expression(ctx, expr->expression, TRUE);
650 if(FAILED(hres))
651 return hres;
652
653 return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, fixmeW);
654 }
655 }
656
657 return S_OK;
658 }
659
660 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
661 {
662 BOOL use_throw_path = FALSE;
663 unsigned arg_cnt = 0;
664 HRESULT hres;
665
666 if(expr->expression1->type == EXPR_CALL) {
667 call_expression_t *call_expr = (call_expression_t*)expr->expression1;
668 argument_t *arg;
669
670 if(op != OP_LAST) {
671 FIXME("op %d not supported on parametrized assign expressions\n", op);
672 return E_NOTIMPL;
673 }
674
675 if(is_memberid_expr(call_expr->expression->type) && call_expr->argument_list) {
676 hres = compile_memberid_expression(ctx, call_expr->expression, fdexNameEnsure);
677 if(FAILED(hres))
678 return hres;
679
680 for(arg = call_expr->argument_list; arg; arg = arg->next) {
681 hres = compile_expression(ctx, arg->expr, TRUE);
682 if(FAILED(hres))
683 return hres;
684 arg_cnt++;
685 }
686 }else {
687 use_throw_path = TRUE;
688 }
689 }else if(is_memberid_expr(expr->expression1->type)) {
690 hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
691 if(FAILED(hres))
692 return hres;
693 }else {
694 use_throw_path = TRUE;
695 }
696
697 if(use_throw_path) {
698 /* Illegal assignment: evaluate and throw */
699 hres = compile_expression(ctx, expr->expression1, TRUE);
700 if(FAILED(hres))
701 return hres;
702
703 hres = compile_expression(ctx, expr->expression2, TRUE);
704 if(FAILED(hres))
705 return hres;
706
707 if(op != OP_LAST && !push_instr(ctx, op))
708 return E_OUTOFMEMORY;
709
710 return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
711 }
712
713 if(op != OP_LAST && !push_instr(ctx, OP_refval))
714 return E_OUTOFMEMORY;
715
716 hres = compile_expression(ctx, expr->expression2, TRUE);
717 if(FAILED(hres))
718 return hres;
719
720 if(op != OP_LAST && !push_instr(ctx, op))
721 return E_OUTOFMEMORY;
722
723 if(arg_cnt)
724 return push_instr_uint(ctx, OP_assign_call, arg_cnt);
725
726 if(!push_instr(ctx, OP_assign))
727 return E_OUTOFMEMORY;
728
729 return S_OK;
730 }
731
732 static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
733 {
734 jsop_t op;
735 HRESULT hres;
736
737 if(is_memberid_expr(expr->expression->type)) {
738 if(expr->expression->type == EXPR_IDENT)
739 return push_instr_bstr(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
740
741 op = OP_typeofid;
742 hres = compile_memberid_expression(ctx, expr->expression, 0);
743 }else {
744 op = OP_typeof;
745 hres = compile_expression(ctx, expr->expression, TRUE);
746 }
747 if(FAILED(hres))
748 return hres;
749
750 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
751 }
752
753 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
754 {
755 switch(literal->type) {
756 case LT_BOOL:
757 return push_instr_int(ctx, OP_bool, literal->u.bval);
758 case LT_DOUBLE:
759 return push_instr_double(ctx, OP_double, literal->u.dval);
760 case LT_NULL:
761 return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
762 case LT_STRING:
763 return push_instr_str(ctx, OP_str, literal->u.wstr);
764 case LT_REGEXP: {
765 unsigned instr;
766 jsstr_t *str;
767
768 str = compiler_alloc_string_len(ctx, literal->u.regexp.str, literal->u.regexp.str_len);
769 if(!str)
770 return E_OUTOFMEMORY;
771
772 instr = push_instr(ctx, OP_regexp);
773 if(!instr)
774 return E_OUTOFMEMORY;
775
776 instr_ptr(ctx, instr)->u.arg[0].str = str;
777 instr_ptr(ctx, instr)->u.arg[1].uint = literal->u.regexp.flags;
778 return S_OK;
779 }
780 DEFAULT_UNREACHABLE;
781 }
782 return E_FAIL;
783 }
784
785 static HRESULT literal_as_bstr(compiler_ctx_t *ctx, literal_t *literal, BSTR *str)
786 {
787 switch(literal->type) {
788 case LT_STRING:
789 *str = compiler_alloc_bstr(ctx, literal->u.wstr);
790 break;
791 case LT_DOUBLE: {
792 jsstr_t *jsstr;
793 HRESULT hres;
794
795 hres = double_to_string(literal->u.dval, &jsstr);
796 if(FAILED(hres))
797 return hres;
798
799 *str = compiler_alloc_bstr_len(ctx, NULL, jsstr_length(jsstr));
800 if(*str)
801 jsstr_flush(jsstr, *str);
802 jsstr_release(jsstr);
803 break;
804 }
805 DEFAULT_UNREACHABLE;
806 }
807
808 return *str ? S_OK : E_OUTOFMEMORY;
809 }
810
811 static HRESULT compile_array_literal(compiler_ctx_t *ctx, array_literal_expression_t *expr)
812 {
813 unsigned i, elem_cnt = expr->length;
814 array_element_t *iter;
815 HRESULT hres;
816
817 for(iter = expr->element_list; iter; iter = iter->next) {
818 elem_cnt += iter->elision+1;
819
820 for(i=0; i < iter->elision; i++) {
821 if(!push_instr(ctx, OP_undefined))
822 return E_OUTOFMEMORY;
823 }
824
825 hres = compile_expression(ctx, iter->expr, TRUE);
826 if(FAILED(hres))
827 return hres;
828 }
829
830 for(i=0; i < expr->length; i++) {
831 if(!push_instr(ctx, OP_undefined))
832 return E_OUTOFMEMORY;
833 }
834
835 return push_instr_uint(ctx, OP_carray, elem_cnt);
836 }
837
838 static HRESULT compile_object_literal(compiler_ctx_t *ctx, property_value_expression_t *expr)
839 {
840 prop_val_t *iter;
841 unsigned instr;
842 BSTR name;
843 HRESULT hres;
844
845 if(!push_instr(ctx, OP_new_obj))
846 return E_OUTOFMEMORY;
847
848 for(iter = expr->property_list; iter; iter = iter->next) {
849 hres = literal_as_bstr(ctx, iter->name, &name);
850 if(FAILED(hres))
851 return hres;
852
853 hres = compile_expression(ctx, iter->value, TRUE);
854 if(FAILED(hres))
855 return hres;
856
857 instr = push_instr(ctx, OP_obj_prop);
858 if(!instr)
859 return E_OUTOFMEMORY;
860
861 instr_ptr(ctx, instr)->u.arg->bstr = name;
862 }
863
864 return S_OK;
865 }
866
867 static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr, BOOL emit_ret)
868 {
869 unsigned func_id = ctx->func->func_cnt++;
870 ctx->func_tail = ctx->func_tail ? (ctx->func_tail->next = expr) : (ctx->func_head = expr);
871 return emit_ret ? push_instr_uint(ctx, OP_func, func_id) : S_OK;
872 }
873
874 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr, BOOL emit_ret)
875 {
876 HRESULT hres;
877
878 switch(expr->type) {
879 case EXPR_ADD:
880 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
881 break;
882 case EXPR_AND:
883 hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_z);
884 break;
885 case EXPR_ARRAY:
886 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
887 break;
888 case EXPR_ARRAYLIT:
889 hres = compile_array_literal(ctx, (array_literal_expression_t*)expr);
890 break;
891 case EXPR_ASSIGN:
892 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
893 break;
894 case EXPR_ASSIGNADD:
895 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
896 break;
897 case EXPR_ASSIGNAND:
898 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_and);
899 break;
900 case EXPR_ASSIGNSUB:
901 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
902 break;
903 case EXPR_ASSIGNMUL:
904 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
905 break;
906 case EXPR_ASSIGNDIV:
907 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
908 break;
909 case EXPR_ASSIGNMOD:
910 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
911 break;
912 case EXPR_ASSIGNOR:
913 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
914 break;
915 case EXPR_ASSIGNLSHIFT:
916 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_lshift);
917 break;
918 case EXPR_ASSIGNRSHIFT:
919 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift);
920 break;
921 case EXPR_ASSIGNRRSHIFT:
922 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
923 break;
924 case EXPR_ASSIGNXOR:
925 hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
926 break;
927 case EXPR_BAND:
928 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
929 break;
930 case EXPR_BITNEG:
931 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
932 break;
933 case EXPR_BOR:
934 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
935 break;
936 case EXPR_CALL:
937 return compile_call_expression(ctx, (call_expression_t*)expr, emit_ret);
938 case EXPR_COMMA:
939 return compile_comma_expression(ctx, (binary_expression_t*)expr, emit_ret);
940 case EXPR_COND:
941 hres = compile_conditional_expression(ctx, (conditional_expression_t*)expr);
942 break;
943 case EXPR_DELETE:
944 hres = compile_delete_expression(ctx, (unary_expression_t*)expr);
945 break;
946 case EXPR_DIV:
947 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
948 break;
949 case EXPR_EQ:
950 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
951 break;
952 case EXPR_EQEQ:
953 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
954 break;
955 case EXPR_FUNC:
956 return compile_function_expression(ctx, (function_expression_t*)expr, emit_ret);
957 case EXPR_GREATER:
958 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
959 break;
960 case EXPR_GREATEREQ:
961 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
962 break;
963 case EXPR_IDENT:
964 hres = push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
965 break;
966 case EXPR_IN:
967 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
968 break;
969 case EXPR_INSTANCEOF:
970 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
971 break;
972 case EXPR_LESS:
973 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
974 break;
975 case EXPR_LESSEQ:
976 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
977 break;
978 case EXPR_LITERAL:
979 hres = compile_literal(ctx, ((literal_expression_t*)expr)->literal);
980 break;
981 case EXPR_LOGNEG:
982 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
983 break;
984 case EXPR_LSHIFT:
985 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
986 break;
987 case EXPR_MEMBER:
988 hres = compile_member_expression(ctx, (member_expression_t*)expr);
989 break;
990 case EXPR_MINUS:
991 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
992 break;
993 case EXPR_MOD:
994 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
995 break;
996 case EXPR_MUL:
997 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
998 break;
999 case EXPR_NEW:
1000 hres = compile_new_expression(ctx, (call_expression_t*)expr);
1001 break;
1002 case EXPR_NOTEQ:
1003 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
1004 break;
1005 case EXPR_NOTEQEQ:
1006 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
1007 break;
1008 case EXPR_OR:
1009 hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_nz);
1010 break;
1011 case EXPR_PLUS:
1012 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
1013 break;
1014 case EXPR_POSTDEC:
1015 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
1016 break;
1017 case EXPR_POSTINC:
1018 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
1019 break;
1020 case EXPR_PREDEC:
1021 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
1022 break;
1023 case EXPR_PREINC:
1024 hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
1025 break;
1026 case EXPR_PROPVAL:
1027 hres = compile_object_literal(ctx, (property_value_expression_t*)expr);
1028 break;
1029 case EXPR_RSHIFT:
1030 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
1031 break;
1032 case EXPR_RRSHIFT:
1033 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
1034 break;
1035 case EXPR_SUB:
1036 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
1037 break;
1038 case EXPR_THIS:
1039 return !emit_ret || push_instr(ctx, OP_this) ? S_OK : E_OUTOFMEMORY;
1040 case EXPR_TYPEOF:
1041 hres = compile_typeof_expression(ctx, (unary_expression_t*)expr);
1042 break;
1043 case EXPR_VOID:
1044 hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
1045 break;
1046 case EXPR_BXOR:
1047 hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
1048 break;
1049 DEFAULT_UNREACHABLE;
1050 }
1051
1052 if(FAILED(hres))
1053 return hres;
1054
1055 return emit_ret ? S_OK : push_instr_uint(ctx, OP_pop, 1);
1056 }
1057
1058 static inline BOOL is_loop_statement(statement_type_t type)
1059 {
1060 return type == STAT_FOR || type == STAT_FORIN || type == STAT_WHILE;
1061 }
1062
1063 /* ECMA-262 3rd Edition 12.1 */
1064 static HRESULT compile_block_statement(compiler_ctx_t *ctx, statement_t *iter)
1065 {
1066 HRESULT hres;
1067
1068 while(iter) {
1069 hres = compile_statement(ctx, NULL, iter);
1070 if(FAILED(hres))
1071 return hres;
1072
1073 iter = iter->next;
1074 }
1075
1076 return S_OK;
1077 }
1078
1079 /* ECMA-262 3rd Edition 12.2 */
1080 static HRESULT compile_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
1081 {
1082 variable_declaration_t *iter;
1083 HRESULT hres;
1084
1085 assert(list != NULL);
1086
1087 if(ctx->var_tail)
1088 ctx->var_tail->global_next = list;
1089 else
1090 ctx->var_head = list;
1091
1092 for(iter = list; iter; iter = iter->next) {
1093 ctx->func->var_cnt++;
1094 iter->global_next = iter->next;
1095 if(!iter->next)
1096 ctx->var_tail = iter;
1097
1098 if(!iter->expr)
1099 continue;
1100
1101 hres = compile_expression(ctx, iter->expr, TRUE);
1102 if(FAILED(hres))
1103 return hres;
1104
1105 hres = push_instr_bstr(ctx, OP_var_set, iter->identifier);
1106 if(FAILED(hres))
1107 return hres;
1108 }
1109
1110 return S_OK;
1111 }
1112
1113 /* ECMA-262 3rd Edition 12.2 */
1114 static HRESULT compile_var_statement(compiler_ctx_t *ctx, var_statement_t *stat)
1115 {
1116 return compile_variable_list(ctx, stat->variable_list);
1117 }
1118
1119 /* ECMA-262 3rd Edition 12.4 */
1120 static HRESULT compile_expression_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1121 {
1122 HRESULT hres;
1123
1124 hres = compile_expression(ctx, stat->expr, ctx->from_eval);
1125 if(FAILED(hres))
1126 return hres;
1127
1128 return !ctx->from_eval || push_instr(ctx, OP_setret) ? S_OK : E_OUTOFMEMORY;
1129 }
1130
1131 /* ECMA-262 3rd Edition 12.5 */
1132 static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat)
1133 {
1134 unsigned jmp_else;
1135 HRESULT hres;
1136
1137 hres = compile_expression(ctx, stat->expr, TRUE);
1138 if(FAILED(hres))
1139 return hres;
1140
1141 jmp_else = push_instr(ctx, OP_jmp_z);
1142 if(!jmp_else)
1143 return E_OUTOFMEMORY;
1144
1145 hres = compile_statement(ctx, NULL, stat->if_stat);
1146 if(FAILED(hres))
1147 return hres;
1148
1149 if(stat->else_stat) {
1150 unsigned jmp_end;
1151
1152 jmp_end = push_instr(ctx, OP_jmp);
1153 if(!jmp_end)
1154 return E_OUTOFMEMORY;
1155
1156 set_arg_uint(ctx, jmp_else, ctx->code_off);
1157
1158 hres = compile_statement(ctx, NULL, stat->else_stat);
1159 if(FAILED(hres))
1160 return hres;
1161
1162 set_arg_uint(ctx, jmp_end, ctx->code_off);
1163 }else {
1164 set_arg_uint(ctx, jmp_else, ctx->code_off);
1165 }
1166
1167 return S_OK;
1168 }
1169
1170 /* ECMA-262 3rd Edition 12.6.2 */
1171 static HRESULT compile_while_statement(compiler_ctx_t *ctx, while_statement_t *stat)
1172 {
1173 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1174 unsigned jmp_off;
1175 HRESULT hres;
1176
1177 stat_ctx.break_label = alloc_label(ctx);
1178 if(!stat_ctx.break_label)
1179 return E_OUTOFMEMORY;
1180
1181 stat_ctx.continue_label = alloc_label(ctx);
1182 if(!stat_ctx.continue_label)
1183 return E_OUTOFMEMORY;
1184
1185 jmp_off = ctx->code_off;
1186
1187 if(!stat->do_while) {
1188 label_set_addr(ctx, stat_ctx.continue_label);
1189 hres = compile_expression(ctx, stat->expr, TRUE);
1190 if(FAILED(hres))
1191 return hres;
1192
1193 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1194 if(FAILED(hres))
1195 return hres;
1196 }
1197
1198 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1199 if(FAILED(hres))
1200 return hres;
1201
1202 if(stat->do_while) {
1203 label_set_addr(ctx, stat_ctx.continue_label);
1204 hres = compile_expression(ctx, stat->expr, TRUE);
1205 if(FAILED(hres))
1206 return hres;
1207
1208 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1209 if(FAILED(hres))
1210 return hres;
1211 }
1212
1213 hres = push_instr_uint(ctx, OP_jmp, jmp_off);
1214 if(FAILED(hres))
1215 return hres;
1216
1217 label_set_addr(ctx, stat_ctx.break_label);
1218 return S_OK;
1219 }
1220
1221 /* ECMA-262 3rd Edition 12.6.3 */
1222 static HRESULT compile_for_statement(compiler_ctx_t *ctx, for_statement_t *stat)
1223 {
1224 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1225 unsigned expr_off;
1226 HRESULT hres;
1227
1228 if(stat->variable_list) {
1229 hres = compile_variable_list(ctx, stat->variable_list);
1230 if(FAILED(hres))
1231 return hres;
1232 }else if(stat->begin_expr) {
1233 hres = compile_expression(ctx, stat->begin_expr, FALSE);
1234 if(FAILED(hres))
1235 return hres;
1236 }
1237
1238 stat_ctx.break_label = alloc_label(ctx);
1239 if(!stat_ctx.break_label)
1240 return E_OUTOFMEMORY;
1241
1242 stat_ctx.continue_label = alloc_label(ctx);
1243 if(!stat_ctx.continue_label)
1244 return E_OUTOFMEMORY;
1245
1246 expr_off = ctx->code_off;
1247
1248 if(stat->expr) {
1249 hres = compile_expression(ctx, stat->expr, TRUE);
1250 if(FAILED(hres))
1251 return hres;
1252
1253 hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1254 if(FAILED(hres))
1255 return hres;
1256 }
1257
1258 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1259 if(FAILED(hres))
1260 return hres;
1261
1262 label_set_addr(ctx, stat_ctx.continue_label);
1263
1264 if(stat->end_expr) {
1265 hres = compile_expression(ctx, stat->end_expr, FALSE);
1266 if(FAILED(hres))
1267 return hres;
1268 }
1269
1270 hres = push_instr_uint(ctx, OP_jmp, expr_off);
1271 if(FAILED(hres))
1272 return hres;
1273
1274 label_set_addr(ctx, stat_ctx.break_label);
1275 return S_OK;
1276 }
1277
1278 /* ECMA-262 3rd Edition 12.6.4 */
1279 static HRESULT compile_forin_statement(compiler_ctx_t *ctx, forin_statement_t *stat)
1280 {
1281 statement_ctx_t stat_ctx = {4, FALSE, FALSE};
1282 HRESULT hres;
1283
1284 if(stat->variable) {
1285 hres = compile_variable_list(ctx, stat->variable);
1286 if(FAILED(hres))
1287 return hres;
1288 }
1289
1290 stat_ctx.break_label = alloc_label(ctx);
1291 if(!stat_ctx.break_label)
1292 return E_OUTOFMEMORY;
1293
1294 stat_ctx.continue_label = alloc_label(ctx);
1295 if(!stat_ctx.continue_label)
1296 return E_OUTOFMEMORY;
1297
1298 hres = compile_expression(ctx, stat->in_expr, TRUE);
1299 if(FAILED(hres))
1300 return hres;
1301
1302 if(stat->variable) {
1303 hres = push_instr_bstr_uint(ctx, OP_identid, stat->variable->identifier, fdexNameEnsure);
1304 if(FAILED(hres))
1305 return hres;
1306 }else if(is_memberid_expr(stat->expr->type)) {
1307 hres = compile_memberid_expression(ctx, stat->expr, fdexNameEnsure);
1308 if(FAILED(hres))
1309 return hres;
1310 }else {
1311 hres = push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
1312 if(FAILED(hres))
1313 return hres;
1314
1315 /* FIXME: compile statement anyways when we depend on compiler to check errors */
1316 return S_OK;
1317 }
1318
1319 hres = push_instr_int(ctx, OP_int, DISPID_STARTENUM);
1320 if(FAILED(hres))
1321 return hres;
1322
1323 label_set_addr(ctx, stat_ctx.continue_label);
1324 hres = push_instr_uint(ctx, OP_forin, stat_ctx.break_label);
1325 if(FAILED(hres))
1326 return E_OUTOFMEMORY;
1327
1328 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1329 if(FAILED(hres))
1330 return hres;
1331
1332 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.continue_label);
1333 if(FAILED(hres))
1334 return hres;
1335
1336 label_set_addr(ctx, stat_ctx.break_label);
1337 return S_OK;
1338 }
1339
1340 static HRESULT pop_to_stat(compiler_ctx_t *ctx, BOOL var_stack, BOOL scope_stack, statement_ctx_t *stat_ctx)
1341 {
1342 unsigned stack_pop = 0;
1343 statement_ctx_t *iter;
1344
1345 for(iter = ctx->stat_ctx; iter != stat_ctx; iter = iter->next) {
1346 if(scope_stack) {
1347 if(iter->using_scope && !push_instr(ctx, OP_pop_scope))
1348 return E_OUTOFMEMORY;
1349 if(iter->using_except && !push_instr(ctx, OP_pop_except))
1350 return E_OUTOFMEMORY;
1351 }
1352 stack_pop += iter->stack_use;
1353 }
1354
1355 if(var_stack && stack_pop) {
1356 HRESULT hres;
1357
1358 hres = push_instr_uint(ctx, OP_pop, stack_pop);
1359 if(FAILED(hres))
1360 return hres;
1361 }
1362
1363 return S_OK;
1364 }
1365
1366 /* ECMA-262 3rd Edition 12.7 */
1367 static HRESULT compile_continue_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1368 {
1369 statement_ctx_t *pop_ctx;
1370 HRESULT hres;
1371
1372 if(stat->identifier) {
1373 statement_t *label_stat;
1374 statement_ctx_t *iter;
1375
1376 pop_ctx = NULL;
1377
1378 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1379 if(iter->continue_label)
1380 pop_ctx = iter;
1381 if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier))
1382 break;
1383 }
1384
1385 if(!iter) {
1386 WARN("Label not found\n");
1387 return JS_E_LABEL_NOT_FOUND;
1388 }
1389
1390 /* Labelled continue are allowed only on loops */
1391 for(label_stat = iter->labelled_stat->statement;
1392 label_stat->type == STAT_LABEL;
1393 label_stat = ((labelled_statement_t*)label_stat)->statement);
1394 if(!is_loop_statement(label_stat->type)) {
1395 WARN("Label is not a loop\n");
1396 return JS_E_INVALID_CONTINUE;
1397 }
1398
1399 assert(pop_ctx != NULL);
1400 }else {
1401 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1402 if(pop_ctx->continue_label)
1403 break;
1404 }
1405
1406 if(!pop_ctx) {
1407 WARN("continue outside loop\n");
1408 return JS_E_INVALID_CONTINUE;
1409 }
1410 }
1411
1412 hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx);
1413 if(FAILED(hres))
1414 return hres;
1415
1416 return push_instr_uint(ctx, OP_jmp, pop_ctx->continue_label);
1417 }
1418
1419 /* ECMA-262 3rd Edition 12.8 */
1420 static HRESULT compile_break_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1421 {
1422 statement_ctx_t *pop_ctx;
1423 HRESULT hres;
1424
1425 if(stat->identifier) {
1426 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1427 if(pop_ctx->labelled_stat && !strcmpW(pop_ctx->labelled_stat->identifier, stat->identifier)) {
1428 assert(pop_ctx->break_label);
1429 break;
1430 }
1431 }
1432
1433 if(!pop_ctx) {
1434 WARN("Label not found\n");
1435 return JS_E_LABEL_NOT_FOUND;
1436 }
1437 }else {
1438 for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1439 if(pop_ctx->break_label && !pop_ctx->labelled_stat)
1440 break;
1441 }
1442
1443 if(!pop_ctx) {
1444 WARN("Break outside loop\n");
1445 return JS_E_INVALID_BREAK;
1446 }
1447 }
1448
1449 hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx->next);
1450 if(FAILED(hres))
1451 return hres;
1452
1453 return push_instr_uint(ctx, OP_jmp, pop_ctx->break_label);
1454 }
1455
1456 /* ECMA-262 3rd Edition 12.9 */
1457 static HRESULT compile_return_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1458 {
1459 HRESULT hres;
1460
1461 if(ctx->from_eval) {
1462 WARN("misplaced return statement\n");
1463 return JS_E_MISPLACED_RETURN;
1464 }
1465
1466 hres = pop_to_stat(ctx, TRUE, FALSE, NULL);
1467 if(FAILED(hres))
1468 return hres;
1469
1470 if(stat->expr) {
1471 hres = compile_expression(ctx, stat->expr, TRUE);
1472 if(FAILED(hres))
1473 return hres;
1474 if(!push_instr(ctx, OP_setret))
1475 return E_OUTOFMEMORY;
1476 }
1477
1478 hres = pop_to_stat(ctx, FALSE, TRUE, NULL);
1479 if(FAILED(hres))
1480 return hres;
1481
1482 return push_instr_uint(ctx, OP_ret, !stat->expr);
1483 }
1484
1485 /* ECMA-262 3rd Edition 12.10 */
1486 static HRESULT compile_with_statement(compiler_ctx_t *ctx, with_statement_t *stat)
1487 {
1488 statement_ctx_t stat_ctx = {0, TRUE, FALSE};
1489 HRESULT hres;
1490
1491 hres = compile_expression(ctx, stat->expr, TRUE);
1492 if(FAILED(hres))
1493 return hres;
1494
1495 if(!push_instr(ctx, OP_push_scope))
1496 return E_OUTOFMEMORY;
1497
1498 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1499 if(FAILED(hres))
1500 return hres;
1501
1502 if(!push_instr(ctx, OP_pop_scope))
1503 return E_OUTOFMEMORY;
1504
1505 return S_OK;
1506 }
1507
1508 /* ECMA-262 3rd Edition 12.10 */
1509 static HRESULT compile_labelled_statement(compiler_ctx_t *ctx, labelled_statement_t *stat)
1510 {
1511 statement_ctx_t stat_ctx = {0, FALSE, FALSE, 0, 0, stat}, *iter;
1512 HRESULT hres;
1513
1514 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1515 if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier)) {
1516 WARN("Label %s redefined\n", debugstr_w(stat->identifier));
1517 return JS_E_LABEL_REDEFINED;
1518 }
1519 }
1520
1521 /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1522 stat_ctx.break_label = alloc_label(ctx);
1523 if(!stat_ctx.break_label)
1524 return E_OUTOFMEMORY;
1525
1526 hres = compile_statement(ctx, &stat_ctx, stat->statement);
1527 if(FAILED(hres))
1528 return hres;
1529
1530 label_set_addr(ctx, stat_ctx.break_label);
1531 return S_OK;
1532 }
1533
1534 /* ECMA-262 3rd Edition 12.13 */
1535 static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat)
1536 {
1537 statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1538 unsigned case_cnt = 0, *case_jmps, i, default_jmp;
1539 BOOL have_default = FALSE;
1540 statement_t *stat_iter;
1541 case_clausule_t *iter;
1542 HRESULT hres;
1543
1544 hres = compile_expression(ctx, stat->expr, TRUE);
1545 if(FAILED(hres))
1546 return hres;
1547
1548 stat_ctx.break_label = alloc_label(ctx);
1549 if(!stat_ctx.break_label)
1550 return E_OUTOFMEMORY;
1551
1552 for(iter = stat->case_list; iter; iter = iter->next) {
1553 if(iter->expr)
1554 case_cnt++;
1555 }
1556
1557 case_jmps = heap_alloc(case_cnt * sizeof(*case_jmps));
1558 if(!case_jmps)
1559 return E_OUTOFMEMORY;
1560
1561 i = 0;
1562 for(iter = stat->case_list; iter; iter = iter->next) {
1563 if(!iter->expr) {
1564 have_default = TRUE;
1565 continue;
1566 }
1567
1568 hres = compile_expression(ctx, iter->expr, TRUE);
1569 if(FAILED(hres))
1570 break;
1571
1572 case_jmps[i] = push_instr(ctx, OP_case);
1573 if(!case_jmps[i]) {
1574 hres = E_OUTOFMEMORY;
1575 break;
1576 }
1577 i++;
1578 }
1579
1580 if(SUCCEEDED(hres)) {
1581 hres = push_instr_uint(ctx, OP_pop, 1);
1582 if(SUCCEEDED(hres)) {
1583 default_jmp = push_instr(ctx, OP_jmp);
1584 if(!default_jmp)
1585 hres = E_OUTOFMEMORY;
1586 }
1587 }
1588
1589 if(FAILED(hres)) {
1590 heap_free(case_jmps);
1591 return hres;
1592 }
1593
1594 i = 0;
1595 for(iter = stat->case_list; iter; iter = iter->next) {
1596 while(iter->next && iter->next->stat == iter->stat) {
1597 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1598 iter = iter->next;
1599 }
1600
1601 set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1602
1603 for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter);
1604 stat_iter = stat_iter->next) {
1605 hres = compile_statement(ctx, &stat_ctx, stat_iter);
1606 if(FAILED(hres))
1607 break;
1608 }
1609 if(FAILED(hres))
1610 break;
1611 }
1612
1613 heap_free(case_jmps);
1614 if(FAILED(hres))
1615 return hres;
1616 assert(i == case_cnt);
1617
1618 if(!have_default) {
1619 hres = push_instr_uint(ctx, OP_jmp, stat_ctx.break_label);
1620 if(FAILED(hres))
1621 return hres;
1622 set_arg_uint(ctx, default_jmp, ctx->code_off);
1623 }
1624
1625 label_set_addr(ctx, stat_ctx.break_label);
1626 return S_OK;
1627 }
1628
1629 /* ECMA-262 3rd Edition 12.13 */
1630 static HRESULT compile_throw_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1631 {
1632 HRESULT hres;
1633
1634 hres = compile_expression(ctx, stat->expr, TRUE);
1635 if(FAILED(hres))
1636 return hres;
1637
1638 return push_instr(ctx, OP_throw) ? S_OK : E_OUTOFMEMORY;
1639 }
1640
1641 /* ECMA-262 3rd Edition 12.14 */
1642 static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat)
1643 {
1644 statement_ctx_t try_ctx = {0, FALSE, TRUE}, catch_ctx = {0, TRUE, FALSE};
1645 statement_ctx_t finally_ctx = {2, FALSE, FALSE};
1646 unsigned push_except;
1647 BSTR ident;
1648 HRESULT hres;
1649
1650 push_except = push_instr(ctx, OP_push_except);
1651 if(!push_except)
1652 return E_OUTOFMEMORY;
1653
1654 if(stat->catch_block) {
1655 ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier);
1656 if(!ident)
1657 return E_OUTOFMEMORY;
1658 }else {
1659 ident = NULL;
1660 }
1661
1662 instr_ptr(ctx, push_except)->u.arg[1].bstr = ident;
1663
1664 if(!stat->catch_block)
1665 try_ctx.stack_use = 2;
1666
1667 hres = compile_statement(ctx, &try_ctx, stat->try_statement);
1668 if(FAILED(hres))
1669 return hres;
1670
1671 if(!push_instr(ctx, OP_pop_except))
1672 return E_OUTOFMEMORY;
1673
1674 if(stat->catch_block) {
1675 unsigned jmp_finally;
1676
1677 jmp_finally = push_instr(ctx, OP_jmp);
1678 if(!jmp_finally)
1679 return E_OUTOFMEMORY;
1680
1681 instr_ptr(ctx, push_except)->u.arg[0].uint = ctx->code_off;
1682
1683 hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement);
1684 if(FAILED(hres))
1685 return hres;
1686
1687 if(!push_instr(ctx, OP_pop_scope))
1688 return E_OUTOFMEMORY;
1689
1690 set_arg_uint(ctx, jmp_finally, ctx->code_off);
1691 }else {
1692 set_arg_uint(ctx, push_except, ctx->code_off);
1693 }
1694
1695 if(stat->finally_statement) {
1696 hres = compile_statement(ctx, stat->catch_block ? NULL : &finally_ctx, stat->finally_statement);
1697 if(FAILED(hres))
1698 return hres;
1699
1700 if(!stat->catch_block && !push_instr(ctx, OP_end_finally))
1701 return E_OUTOFMEMORY;
1702 }
1703
1704 return S_OK;
1705 }
1706
1707 static HRESULT compile_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1708 {
1709 HRESULT hres;
1710
1711 if(stat_ctx) {
1712 stat_ctx->next = ctx->stat_ctx;
1713 ctx->stat_ctx = stat_ctx;
1714 }
1715
1716 switch(stat->type) {
1717 case STAT_BLOCK:
1718 hres = compile_block_statement(ctx, ((block_statement_t*)stat)->stat_list);
1719 break;
1720 case STAT_BREAK:
1721 hres = compile_break_statement(ctx, (branch_statement_t*)stat);
1722 break;
1723 case STAT_CONTINUE:
1724 hres = compile_continue_statement(ctx, (branch_statement_t*)stat);
1725 break;
1726 case STAT_EMPTY:
1727 /* nothing to do */
1728 hres = S_OK;
1729 break;
1730 case STAT_EXPR:
1731 hres = compile_expression_statement(ctx, (expression_statement_t*)stat);
1732 break;
1733 case STAT_FOR:
1734 hres = compile_for_statement(ctx, (for_statement_t*)stat);
1735 break;
1736 case STAT_FORIN:
1737 hres = compile_forin_statement(ctx, (forin_statement_t*)stat);
1738 break;
1739 case STAT_IF:
1740 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1741 break;
1742 case STAT_LABEL:
1743 hres = compile_labelled_statement(ctx, (labelled_statement_t*)stat);
1744 break;
1745 case STAT_RETURN:
1746 hres = compile_return_statement(ctx, (expression_statement_t*)stat);
1747 break;
1748 case STAT_SWITCH:
1749 hres = compile_switch_statement(ctx, (switch_statement_t*)stat);
1750 break;
1751 case STAT_THROW:
1752 hres = compile_throw_statement(ctx, (expression_statement_t*)stat);
1753 break;
1754 case STAT_TRY:
1755 hres = compile_try_statement(ctx, (try_statement_t*)stat);
1756 break;
1757 case STAT_VAR:
1758 hres = compile_var_statement(ctx, (var_statement_t*)stat);
1759 break;
1760 case STAT_WHILE:
1761 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1762 break;
1763 case STAT_WITH:
1764 hres = compile_with_statement(ctx, (with_statement_t*)stat);
1765 break;
1766 DEFAULT_UNREACHABLE;
1767 }
1768
1769 if(stat_ctx) {
1770 assert(ctx->stat_ctx == stat_ctx);
1771 ctx->stat_ctx = stat_ctx->next;
1772 }
1773
1774 return hres;
1775 }
1776
1777 static void resolve_labels(compiler_ctx_t *ctx, unsigned off)
1778 {
1779 instr_t *instr;
1780
1781 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
1782 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->u.arg->uint & LABEL_FLAG)) {
1783 assert((instr->u.arg->uint & ~LABEL_FLAG) < ctx->labels_cnt);
1784 instr->u.arg->uint = ctx->labels[instr->u.arg->uint & ~LABEL_FLAG];
1785 }
1786 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
1787 }
1788
1789 ctx->labels_cnt = 0;
1790 }
1791
1792 void release_bytecode(bytecode_t *code)
1793 {
1794 unsigned i;
1795
1796 if(--code->ref)
1797 return;
1798
1799 for(i=0; i < code->bstr_cnt; i++)
1800 SysFreeString(code->bstr_pool[i]);
1801 for(i=0; i < code->str_cnt; i++)
1802 jsstr_release(code->str_pool[i]);
1803
1804 heap_free(code->source);
1805 heap_pool_free(&code->heap);
1806 heap_free(code->bstr_pool);
1807 heap_free(code->str_pool);
1808 heap_free(code->instrs);
1809 heap_free(code);
1810 }
1811
1812 static HRESULT init_code(compiler_ctx_t *compiler, const WCHAR *source)
1813 {
1814 compiler->code = heap_alloc_zero(sizeof(bytecode_t));
1815 if(!compiler->code)
1816 return E_OUTOFMEMORY;
1817
1818 compiler->code->ref = 1;
1819 heap_pool_init(&compiler->code->heap);
1820
1821 compiler->code->source = heap_strdupW(source);
1822 if(!compiler->code->source) {
1823 release_bytecode(compiler->code);
1824 return E_OUTOFMEMORY;
1825 }
1826
1827 compiler->code->instrs = heap_alloc(64 * sizeof(instr_t));
1828 if(!compiler->code->instrs) {
1829 release_bytecode(compiler->code);
1830 return E_OUTOFMEMORY;
1831 }
1832
1833 compiler->code_size = 64;
1834 compiler->code_off = 1;
1835 return S_OK;
1836 }
1837
1838 static HRESULT compile_function(compiler_ctx_t *ctx, source_elements_t *source, function_expression_t *func_expr,
1839 BOOL from_eval, function_code_t *func)
1840 {
1841 variable_declaration_t *var_iter;
1842 function_expression_t *iter;
1843 unsigned off, i;
1844 HRESULT hres;
1845
1846 TRACE("\n");
1847
1848 ctx->var_head = ctx->var_tail = NULL;
1849 ctx->func_head = ctx->func_tail = NULL;
1850 ctx->from_eval = from_eval;
1851
1852 off = ctx->code_off;
1853 ctx->func = func;
1854 hres = compile_block_statement(ctx, source->statement);
1855 if(FAILED(hres))
1856 return hres;
1857
1858 resolve_labels(ctx, off);
1859
1860 hres = push_instr_uint(ctx, OP_ret, !from_eval);
1861 if(FAILED(hres))
1862 return hres;
1863
1864 if(TRACE_ON(jscript_disas))
1865 dump_code(ctx, off);
1866
1867 func->instr_off = off;
1868
1869 if(func_expr) {
1870 if(func_expr->identifier) {
1871 func->name = compiler_alloc_bstr(ctx, func_expr->identifier);
1872 if(!func->name)
1873 return E_OUTOFMEMORY;
1874 }
1875
1876 if(func_expr->event_target) {
1877 func->event_target = compiler_alloc_bstr(ctx, func_expr->event_target);
1878 if(!func->event_target)
1879 return E_OUTOFMEMORY;
1880 }
1881 }
1882
1883 if(func_expr) {
1884 parameter_t *param_iter;
1885
1886 func->source = func_expr->src_str;
1887 func->source_len = func_expr->src_len;
1888
1889 for(param_iter = func_expr->parameter_list; param_iter; param_iter = param_iter->next)
1890 func->param_cnt++;
1891
1892 func->params = compiler_alloc(ctx->code, func->param_cnt * sizeof(*func->params));
1893 if(!func->params)
1894 return E_OUTOFMEMORY;
1895
1896 for(param_iter = func_expr->parameter_list, i=0; param_iter; param_iter = param_iter->next, i++) {
1897 func->params[i] = compiler_alloc_bstr(ctx, param_iter->identifier);
1898 if(!func->params[i])
1899 return E_OUTOFMEMORY;
1900 }
1901 }
1902
1903 func->variables = compiler_alloc(ctx->code, func->var_cnt * sizeof(*func->variables));
1904 if(!func->variables)
1905 return E_OUTOFMEMORY;
1906
1907 for(var_iter = ctx->var_head, i=0; var_iter; var_iter = var_iter->global_next, i++) {
1908 func->variables[i] = compiler_alloc_bstr(ctx, var_iter->identifier);
1909 if(!func->variables[i])
1910 return E_OUTOFMEMORY;
1911 }
1912
1913 assert(i == func->var_cnt);
1914
1915 func->funcs = compiler_alloc(ctx->code, func->func_cnt * sizeof(*func->funcs));
1916 if(!func->funcs)
1917 return E_OUTOFMEMORY;
1918 memset(func->funcs, 0, func->func_cnt * sizeof(*func->funcs));
1919
1920 for(iter = ctx->func_head, i=0; iter; iter = iter->next, i++) {
1921 hres = compile_function(ctx, iter->source_elements, iter, FALSE, func->funcs+i);
1922 if(FAILED(hres))
1923 return hres;
1924 }
1925
1926 assert(i == func->func_cnt);
1927
1928 return S_OK;
1929 }
1930
1931 static HRESULT parse_arguments(compiler_ctx_t *ctx, const WCHAR *args, BSTR *arg_array, unsigned *args_size)
1932 {
1933 const WCHAR *ptr = args, *ptr2;
1934 unsigned arg_cnt = 0;
1935
1936 while(isspaceW(*ptr))
1937 ptr++;
1938 if(!*ptr) {
1939 if(args_size)
1940 *args_size = 0;
1941 return S_OK;
1942 }
1943
1944 while(1) {
1945 if(!isalphaW(*ptr) && *ptr != '_') {
1946 FIXME("expected alpha or '_': %s\n", debugstr_w(ptr));
1947 return E_FAIL;
1948 }
1949
1950 ptr2 = ptr;
1951 while(isalnumW(*ptr) || *ptr == '_')
1952 ptr++;
1953
1954 if(*ptr && *ptr != ',' && !isspaceW(*ptr)) {
1955 FIXME("unexpected har %s\n", debugstr_w(ptr));
1956 return E_FAIL;
1957 }
1958
1959 if(arg_array) {
1960 arg_array[arg_cnt] = compiler_alloc_bstr_len(ctx, ptr2, ptr-ptr2);
1961 if(!arg_array[arg_cnt])
1962 return E_OUTOFMEMORY;
1963 }
1964 arg_cnt++;
1965
1966 while(isspaceW(*ptr))
1967 ptr++;
1968 if(!*ptr)
1969 break;
1970 if(*ptr != ',') {
1971 FIXME("expected ',': %s\n", debugstr_w(ptr));
1972 return E_FAIL;
1973 }
1974
1975 ptr++;
1976 while(isspaceW(*ptr))
1977 ptr++;
1978 }
1979
1980 if(args_size)
1981 *args_size = arg_cnt;
1982 return S_OK;
1983 }
1984
1985 static HRESULT compile_arguments(compiler_ctx_t *ctx, const WCHAR *args)
1986 {
1987 HRESULT hres;
1988
1989 hres = parse_arguments(ctx, args, NULL, &ctx->code->global_code.param_cnt);
1990 if(FAILED(hres))
1991 return hres;
1992
1993 ctx->code->global_code.params = compiler_alloc(ctx->code,
1994 ctx->code->global_code.param_cnt * sizeof(*ctx->code->global_code.params));
1995 if(!ctx->code->global_code.params)
1996 return E_OUTOFMEMORY;
1997
1998 return parse_arguments(ctx, args, ctx->code->global_code.params, NULL);
1999 }
2000
2001 HRESULT compile_script(script_ctx_t *ctx, const WCHAR *code, const WCHAR *args, const WCHAR *delimiter,
2002 BOOL from_eval, BOOL use_decode, bytecode_t **ret)
2003 {
2004 compiler_ctx_t compiler = {0};
2005 HRESULT hres;
2006
2007 hres = init_code(&compiler, code);
2008 if(FAILED(hres))
2009 return hres;
2010
2011 if(args) {
2012 hres = compile_arguments(&compiler, args);
2013 if(FAILED(hres))
2014 return hres;
2015 }
2016
2017 if(use_decode) {
2018 hres = decode_source(compiler.code->source);
2019 if(FAILED(hres)) {
2020 WARN("Decoding failed\n");
2021 return hres;
2022 }
2023 }
2024
2025 hres = script_parse(ctx, compiler.code->source, delimiter, from_eval, &compiler.parser);
2026 if(FAILED(hres)) {
2027 release_bytecode(compiler.code);
2028 return hres;
2029 }
2030
2031 hres = compile_function(&compiler, compiler.parser->source, NULL, from_eval, &compiler.code->global_code);
2032 parser_release(compiler.parser);
2033 if(FAILED(hres)) {
2034 release_bytecode(compiler.code);
2035 return hres;
2036 }
2037
2038 *ret = compiler.code;
2039 return S_OK;
2040 }