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