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