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