jscript: Pass msg as jsstr_t to create_error.
[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 heap_pool_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, BOOL emit_ret)
478 {
479     HRESULT hres;
480
481     hres = compile_expression(ctx, expr->expression1, FALSE);
482     if(FAILED(hres))
483         return hres;
484
485     return compile_expression(ctx, expr->expression2, emit_ret);
486 }
487
488 /* ECMA-262 3rd Edition    11.11 */
489 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
490 {
491     unsigned instr;
492     HRESULT hres;
493
494     hres = compile_expression(ctx, expr->expression1, TRUE);
495     if(FAILED(hres))
496         return hres;
497
498     instr = push_instr(ctx, op);
499     if(!instr)
500         return E_OUTOFMEMORY;
501
502     hres = compile_expression(ctx, expr->expression2, TRUE);
503     if(FAILED(hres))
504         return hres;
505
506     set_arg_uint(ctx, instr, ctx->code_off);
507     return S_OK;
508 }
509
510 /* ECMA-262 3rd Edition    11.12 */
511 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
512 {
513     unsigned jmp_false, jmp_end;
514     HRESULT hres;
515
516     hres = compile_expression(ctx, expr->expression, TRUE);
517     if(FAILED(hres))
518         return hres;
519
520     jmp_false = push_instr(ctx, OP_cnd_z);
521     if(!jmp_false)
522         return E_OUTOFMEMORY;
523
524     hres = compile_expression(ctx, expr->true_expression, TRUE);
525     if(FAILED(hres))
526         return hres;
527
528     jmp_end = push_instr(ctx, OP_jmp);
529     if(!jmp_end)
530         return E_OUTOFMEMORY;
531
532     set_arg_uint(ctx, jmp_false, ctx->code_off);
533     hres = push_instr_uint(ctx, OP_pop, 1);
534     if(FAILED(hres))
535         return hres;
536
537     hres = compile_expression(ctx, expr->false_expression, TRUE);
538     if(FAILED(hres))
539         return hres;
540
541     set_arg_uint(ctx, jmp_end, ctx->code_off);
542     return S_OK;
543 }
544
545 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
546 {
547     unsigned arg_cnt = 0;
548     argument_t *arg;
549     HRESULT hres;
550
551     hres = compile_expression(ctx, expr->expression, TRUE);
552     if(FAILED(hres))
553         return hres;
554
555     for(arg = expr->argument_list; arg; arg = arg->next) {
556         hres = compile_expression(ctx, arg->expr, TRUE);
557         if(FAILED(hres))
558             return hres;
559         arg_cnt++;
560     }
561
562     return push_instr_uint(ctx, OP_new, arg_cnt);
563 }
564
565 static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL emit_ret)
566 {
567     unsigned arg_cnt = 0;
568     argument_t *arg;
569     unsigned instr;
570     jsop_t op;
571     HRESULT hres;
572
573     if(is_memberid_expr(expr->expression->type)) {
574         op = OP_call_member;
575         hres = compile_memberid_expression(ctx, expr->expression, 0);
576     }else {
577         op = OP_call;
578         hres = compile_expression(ctx, expr->expression, TRUE);
579     }
580
581     if(FAILED(hres))
582         return hres;
583
584     for(arg = expr->argument_list; arg; arg = arg->next) {
585         hres = compile_expression(ctx, arg->expr, TRUE);
586         if(FAILED(hres))
587             return hres;
588         arg_cnt++;
589     }
590
591     instr = push_instr(ctx, op);
592     if(!instr)
593         return E_OUTOFMEMORY;
594
595     instr_ptr(ctx, instr)->u.arg[0].uint = arg_cnt;
596     instr_ptr(ctx, instr)->u.arg[1].lng = emit_ret;
597     return S_OK;
598 }
599
600 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
601 {
602     HRESULT hres;
603
604     switch(expr->expression->type) {
605     case EXPR_ARRAY: {
606         binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
607
608         hres = compile_expression(ctx, array_expr->expression1, TRUE);
609         if(FAILED(hres))
610             return hres;
611
612         hres = compile_expression(ctx, array_expr->expression2, TRUE);
613         if(FAILED(hres))
614             return hres;
615
616         if(!push_instr(ctx, OP_delete))
617             return E_OUTOFMEMORY;
618         break;
619     }
620     case EXPR_MEMBER: {
621         member_expression_t *member_expr = (member_expression_t*)expr->expression;
622
623         hres = compile_expression(ctx, member_expr->expression, TRUE);
624         if(FAILED(hres))
625             return hres;
626
627         /* FIXME: Potential optimization */
628         hres = push_instr_str(ctx, OP_str, member_expr->identifier);
629         if(FAILED(hres))
630             return hres;
631
632         if(!push_instr(ctx, OP_delete))
633             return E_OUTOFMEMORY;
634         break;
635     }
636     case EXPR_IDENT:
637         return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
638     default: {
639         const WCHAR fixmeW[] = {'F','I','X','M','E',0};
640
641         WARN("invalid delete, unimplemented exception message\n");
642
643         hres = compile_expression(ctx, expr->expression, TRUE);
644         if(FAILED(hres))
645             return hres;
646
647         return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, fixmeW);
648     }
649     }
650
651     return S_OK;
652 }
653
654 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
655 {
656     BOOL use_throw_path = FALSE;
657     unsigned arg_cnt = 0;
658     HRESULT hres;
659
660     if(expr->expression1->type == EXPR_CALL) {
661         call_expression_t *call_expr = (call_expression_t*)expr->expression1;
662         argument_t *arg;
663
664         if(op != OP_LAST) {
665             FIXME("op %d not supported on parametrized assign expressions\n", op);
666             return E_NOTIMPL;
667         }
668
669         if(is_memberid_expr(call_expr->expression->type) && call_expr->argument_list) {
670             hres = compile_memberid_expression(ctx, call_expr->expression, fdexNameEnsure);
671             if(FAILED(hres))
672                 return hres;
673
674             for(arg = call_expr->argument_list; arg; arg = arg->next) {
675                 hres = compile_expression(ctx, arg->expr, TRUE);
676                 if(FAILED(hres))
677                     return hres;
678                 arg_cnt++;
679             }
680         }else {
681             use_throw_path = TRUE;
682         }
683     }else if(is_memberid_expr(expr->expression1->type)) {
684         hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
685         if(FAILED(hres))
686             return hres;
687     }else {
688         use_throw_path = TRUE;
689     }
690
691     if(use_throw_path) {
692         /* Illegal assignment: evaluate and throw */
693         hres = compile_expression(ctx, expr->expression1, TRUE);
694         if(FAILED(hres))
695             return hres;
696
697         hres = compile_expression(ctx, expr->expression2, TRUE);
698         if(FAILED(hres))
699             return hres;
700
701         if(op != OP_LAST && !push_instr(ctx, op))
702             return E_OUTOFMEMORY;
703
704         return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
705     }
706
707     if(op != OP_LAST && !push_instr(ctx, OP_refval))
708         return E_OUTOFMEMORY;
709
710     hres = compile_expression(ctx, expr->expression2, TRUE);
711     if(FAILED(hres))
712         return hres;
713
714     if(op != OP_LAST && !push_instr(ctx, op))
715         return E_OUTOFMEMORY;
716
717     if(arg_cnt)
718         return push_instr_uint(ctx, OP_assign_call, arg_cnt);
719
720     if(!push_instr(ctx, OP_assign))
721         return E_OUTOFMEMORY;
722
723     return S_OK;
724 }
725
726 static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
727 {
728     jsop_t op;
729     HRESULT hres;
730
731     if(is_memberid_expr(expr->expression->type)) {
732         if(expr->expression->type == EXPR_IDENT)
733             return push_instr_bstr(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
734
735         op = OP_typeofid;
736         hres = compile_memberid_expression(ctx, expr->expression, 0);
737     }else {
738         op = OP_typeof;
739         hres = compile_expression(ctx, expr->expression, TRUE);
740     }
741     if(FAILED(hres))
742         return hres;
743
744     return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
745 }
746
747 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
748 {
749     switch(literal->type) {
750     case LT_BOOL:
751         return push_instr_int(ctx, OP_bool, literal->u.bval);
752     case LT_DOUBLE:
753         return push_instr_double(ctx, OP_double, literal->u.dval);
754     case LT_NULL:
755         return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
756     case LT_STRING:
757         return push_instr_str(ctx, OP_str, literal->u.wstr);
758     case LT_REGEXP: {
759         unsigned instr;
760         jsstr_t *str;
761
762         str = compiler_alloc_string_len(ctx, literal->u.regexp.str, literal->u.regexp.str_len);
763         if(!str)
764             return E_OUTOFMEMORY;
765
766         instr = push_instr(ctx, OP_regexp);
767         if(!instr)
768             return E_OUTOFMEMORY;
769
770         instr_ptr(ctx, instr)->u.arg[0].str = str;
771         instr_ptr(ctx, instr)->u.arg[1].uint = literal->u.regexp.flags;
772         return S_OK;
773     }
774     default:
775         assert(0);
776         return E_FAIL;
777     }
778 }
779
780 static HRESULT literal_as_bstr(compiler_ctx_t *ctx, literal_t *literal, BSTR *str)
781 {
782     switch(literal->type) {
783     case LT_STRING:
784         *str = compiler_alloc_bstr(ctx, literal->u.wstr);
785         break;
786     case LT_DOUBLE: {
787         jsstr_t *jsstr;
788         HRESULT hres;
789
790         hres = double_to_string(literal->u.dval, &jsstr);
791         if(FAILED(hres))
792             return hres;
793
794         *str = compiler_alloc_bstr_len(ctx, NULL, jsstr_length(jsstr));
795         if(*str)
796             jsstr_flush(jsstr, *str);
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         return compile_comma_expression(ctx, (binary_expression_t*)expr, emit_ret);
942     case EXPR_COND:
943         hres = compile_conditional_expression(ctx, (conditional_expression_t*)expr);
944         break;
945     case EXPR_DELETE:
946         hres = compile_delete_expression(ctx, (unary_expression_t*)expr);
947         break;
948     case EXPR_DIV:
949         hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
950         break;
951     case EXPR_EQ:
952         hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
953         break;
954     case EXPR_EQEQ:
955         hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
956         break;
957     case EXPR_FUNC:
958         hres = compile_function_expression(ctx, (function_expression_t*)expr);
959         break;
960     case EXPR_GREATER:
961         hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
962         break;
963     case EXPR_GREATEREQ:
964         hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
965         break;
966     case EXPR_IDENT:
967         hres = push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
968         break;
969     case EXPR_IN:
970         hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
971         break;
972     case EXPR_INSTANCEOF:
973         hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
974         break;
975     case EXPR_LESS:
976         hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
977         break;
978     case EXPR_LESSEQ:
979         hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
980         break;
981     case EXPR_LITERAL:
982         hres = compile_literal(ctx, ((literal_expression_t*)expr)->literal);
983         break;
984     case EXPR_LOGNEG:
985         hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
986         break;
987     case EXPR_LSHIFT:
988         hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
989         break;
990     case EXPR_MEMBER:
991         hres = compile_member_expression(ctx, (member_expression_t*)expr);
992         break;
993     case EXPR_MINUS:
994         hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
995         break;
996     case EXPR_MOD:
997         hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
998         break;
999     case EXPR_MUL:
1000         hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
1001         break;
1002     case EXPR_NEW:
1003         hres = compile_new_expression(ctx, (call_expression_t*)expr);
1004         break;
1005     case EXPR_NOTEQ:
1006         hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
1007         break;
1008     case EXPR_NOTEQEQ:
1009         hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
1010         break;
1011     case EXPR_OR:
1012         hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_nz);
1013         break;
1014     case EXPR_PLUS:
1015         hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
1016         break;
1017     case EXPR_POSTDEC:
1018         hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
1019         break;
1020     case EXPR_POSTINC:
1021         hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
1022         break;
1023     case EXPR_PREDEC:
1024         hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
1025         break;
1026     case EXPR_PREINC:
1027         hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
1028         break;
1029     case EXPR_PROPVAL:
1030         hres = compile_object_literal(ctx, (property_value_expression_t*)expr);
1031         break;
1032     case EXPR_RSHIFT:
1033         hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
1034         break;
1035     case EXPR_RRSHIFT:
1036         hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
1037         break;
1038     case EXPR_SUB:
1039         hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
1040         break;
1041     case EXPR_THIS:
1042         return !emit_ret || push_instr(ctx, OP_this) ? S_OK : E_OUTOFMEMORY;
1043     case EXPR_TYPEOF:
1044         hres = compile_typeof_expression(ctx, (unary_expression_t*)expr);
1045         break;
1046     case EXPR_VOID:
1047         hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
1048         break;
1049     case EXPR_BXOR:
1050         hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
1051         break;
1052     default:
1053         assert(0);
1054     }
1055
1056     if(FAILED(hres))
1057         return hres;
1058
1059     return emit_ret ? S_OK : push_instr_uint(ctx, OP_pop, 1);
1060 }
1061
1062 static inline BOOL is_loop_statement(statement_type_t type)
1063 {
1064     return type == STAT_FOR || type == STAT_FORIN || type == STAT_WHILE;
1065 }
1066
1067 /* ECMA-262 3rd Edition    12.1 */
1068 static HRESULT compile_block_statement(compiler_ctx_t *ctx, statement_t *iter)
1069 {
1070     HRESULT hres;
1071
1072     while(iter) {
1073         hres = compile_statement(ctx, NULL, iter);
1074         if(FAILED(hres))
1075             return hres;
1076
1077         iter = iter->next;
1078     }
1079
1080     return S_OK;
1081 }
1082
1083 /* ECMA-262 3rd Edition    12.2 */
1084 static HRESULT compile_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
1085 {
1086     variable_declaration_t *iter;
1087     HRESULT hres;
1088
1089     assert(list != NULL);
1090
1091     if(ctx->var_tail)
1092         ctx->var_tail->global_next = list;
1093     else
1094         ctx->var_head = list;
1095
1096     for(iter = list; iter; iter = iter->next) {
1097         ctx->func->var_cnt++;
1098         iter->global_next = iter->next;
1099         if(!iter->next)
1100             ctx->var_tail = iter;
1101
1102         if(!iter->expr)
1103             continue;
1104
1105         hres = compile_expression(ctx, iter->expr, TRUE);
1106         if(FAILED(hres))
1107             return hres;
1108
1109         hres = push_instr_bstr(ctx, OP_var_set, iter->identifier);
1110         if(FAILED(hres))
1111             return hres;
1112     }
1113
1114     return S_OK;
1115 }
1116
1117 /* ECMA-262 3rd Edition    12.2 */
1118 static HRESULT compile_var_statement(compiler_ctx_t *ctx, var_statement_t *stat)
1119 {
1120     return compile_variable_list(ctx, stat->variable_list);
1121 }
1122
1123 /* ECMA-262 3rd Edition    12.4 */
1124 static HRESULT compile_expression_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1125 {
1126     HRESULT hres;
1127
1128     hres = compile_expression(ctx, stat->expr, ctx->from_eval);
1129     if(FAILED(hres))
1130         return hres;
1131
1132     return !ctx->from_eval || push_instr(ctx, OP_setret) ? S_OK : E_OUTOFMEMORY;
1133 }
1134
1135 /* ECMA-262 3rd Edition    12.5 */
1136 static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat)
1137 {
1138     unsigned jmp_else;
1139     HRESULT hres;
1140
1141     hres = compile_expression(ctx, stat->expr, TRUE);
1142     if(FAILED(hres))
1143         return hres;
1144
1145     jmp_else = push_instr(ctx, OP_jmp_z);
1146     if(!jmp_else)
1147         return E_OUTOFMEMORY;
1148
1149     hres = compile_statement(ctx, NULL, stat->if_stat);
1150     if(FAILED(hres))
1151         return hres;
1152
1153     if(stat->else_stat) {
1154         unsigned jmp_end;
1155
1156         jmp_end = push_instr(ctx, OP_jmp);
1157         if(!jmp_end)
1158             return E_OUTOFMEMORY;
1159
1160         set_arg_uint(ctx, jmp_else, ctx->code_off);
1161
1162         hres = compile_statement(ctx, NULL, stat->else_stat);
1163         if(FAILED(hres))
1164             return hres;
1165
1166         set_arg_uint(ctx, jmp_end, ctx->code_off);
1167     }else {
1168         set_arg_uint(ctx, jmp_else, ctx->code_off);
1169     }
1170
1171     return S_OK;
1172 }
1173
1174 /* ECMA-262 3rd Edition    12.6.2 */
1175 static HRESULT compile_while_statement(compiler_ctx_t *ctx, while_statement_t *stat)
1176 {
1177     statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1178     unsigned jmp_off;
1179     HRESULT hres;
1180
1181     stat_ctx.break_label = alloc_label(ctx);
1182     if(!stat_ctx.break_label)
1183         return E_OUTOFMEMORY;
1184
1185     stat_ctx.continue_label = alloc_label(ctx);
1186     if(!stat_ctx.continue_label)
1187         return E_OUTOFMEMORY;
1188
1189     jmp_off = ctx->code_off;
1190
1191     if(!stat->do_while) {
1192         label_set_addr(ctx, stat_ctx.continue_label);
1193         hres = compile_expression(ctx, stat->expr, TRUE);
1194         if(FAILED(hres))
1195             return hres;
1196
1197         hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1198         if(FAILED(hres))
1199             return hres;
1200     }
1201
1202     hres = compile_statement(ctx, &stat_ctx, stat->statement);
1203     if(FAILED(hres))
1204         return hres;
1205
1206     if(stat->do_while) {
1207         label_set_addr(ctx, stat_ctx.continue_label);
1208         hres = compile_expression(ctx, stat->expr, TRUE);
1209         if(FAILED(hres))
1210             return hres;
1211
1212         hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1213         if(FAILED(hres))
1214             return hres;
1215     }
1216
1217     hres = push_instr_uint(ctx, OP_jmp, jmp_off);
1218     if(FAILED(hres))
1219         return hres;
1220
1221     label_set_addr(ctx, stat_ctx.break_label);
1222     return S_OK;
1223 }
1224
1225 /* ECMA-262 3rd Edition    12.6.3 */
1226 static HRESULT compile_for_statement(compiler_ctx_t *ctx, for_statement_t *stat)
1227 {
1228     statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1229     unsigned expr_off;
1230     HRESULT hres;
1231
1232     if(stat->variable_list) {
1233         hres = compile_variable_list(ctx, stat->variable_list);
1234         if(FAILED(hres))
1235             return hres;
1236     }else if(stat->begin_expr) {
1237         hres = compile_expression(ctx, stat->begin_expr, FALSE);
1238         if(FAILED(hres))
1239             return hres;
1240     }
1241
1242     stat_ctx.break_label = alloc_label(ctx);
1243     if(!stat_ctx.break_label)
1244         return E_OUTOFMEMORY;
1245
1246     stat_ctx.continue_label = alloc_label(ctx);
1247     if(!stat_ctx.continue_label)
1248         return E_OUTOFMEMORY;
1249
1250     expr_off = ctx->code_off;
1251
1252     if(stat->expr) {
1253         hres = compile_expression(ctx, stat->expr, TRUE);
1254         if(FAILED(hres))
1255             return hres;
1256
1257         hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1258         if(FAILED(hres))
1259             return hres;
1260     }
1261
1262     hres = compile_statement(ctx, &stat_ctx, stat->statement);
1263     if(FAILED(hres))
1264         return hres;
1265
1266     label_set_addr(ctx, stat_ctx.continue_label);
1267
1268     if(stat->end_expr) {
1269         hres = compile_expression(ctx, stat->end_expr, FALSE);
1270         if(FAILED(hres))
1271             return hres;
1272     }
1273
1274     hres = push_instr_uint(ctx, OP_jmp, expr_off);
1275     if(FAILED(hres))
1276         return hres;
1277
1278     label_set_addr(ctx, stat_ctx.break_label);
1279     return S_OK;
1280 }
1281
1282 /* ECMA-262 3rd Edition    12.6.4 */
1283 static HRESULT compile_forin_statement(compiler_ctx_t *ctx, forin_statement_t *stat)
1284 {
1285     statement_ctx_t stat_ctx = {4, FALSE, FALSE};
1286     HRESULT hres;
1287
1288     if(stat->variable) {
1289         hres = compile_variable_list(ctx, stat->variable);
1290         if(FAILED(hres))
1291             return hres;
1292     }
1293
1294     stat_ctx.break_label = alloc_label(ctx);
1295     if(!stat_ctx.break_label)
1296         return E_OUTOFMEMORY;
1297
1298     stat_ctx.continue_label = alloc_label(ctx);
1299     if(!stat_ctx.continue_label)
1300         return E_OUTOFMEMORY;
1301
1302     hres = compile_expression(ctx, stat->in_expr, TRUE);
1303     if(FAILED(hres))
1304         return hres;
1305
1306     if(stat->variable) {
1307         hres = push_instr_bstr_uint(ctx, OP_identid, stat->variable->identifier, fdexNameEnsure);
1308         if(FAILED(hres))
1309             return hres;
1310     }else if(is_memberid_expr(stat->expr->type)) {
1311         hres = compile_memberid_expression(ctx, stat->expr, fdexNameEnsure);
1312         if(FAILED(hres))
1313             return hres;
1314     }else {
1315         hres = push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
1316         if(FAILED(hres))
1317             return hres;
1318
1319         /* FIXME: compile statement anyways when we depend on compiler to check errors */
1320         return S_OK;
1321     }
1322
1323     hres = push_instr_int(ctx, OP_int, DISPID_STARTENUM);
1324     if(FAILED(hres))
1325         return hres;
1326
1327     label_set_addr(ctx, stat_ctx.continue_label);
1328     hres = push_instr_uint(ctx, OP_forin, stat_ctx.break_label);
1329     if(FAILED(hres))
1330         return E_OUTOFMEMORY;
1331
1332     hres = compile_statement(ctx, &stat_ctx, stat->statement);
1333     if(FAILED(hres))
1334         return hres;
1335
1336     hres = push_instr_uint(ctx, OP_jmp, stat_ctx.continue_label);
1337     if(FAILED(hres))
1338         return hres;
1339
1340     label_set_addr(ctx, stat_ctx.break_label);
1341     return S_OK;
1342 }
1343
1344 static HRESULT pop_to_stat(compiler_ctx_t *ctx, BOOL var_stack, BOOL scope_stack, statement_ctx_t *stat_ctx)
1345 {
1346     unsigned stack_pop = 0;
1347     statement_ctx_t *iter;
1348
1349     for(iter = ctx->stat_ctx; iter != stat_ctx; iter = iter->next) {
1350         if(scope_stack) {
1351             if(iter->using_scope && !push_instr(ctx, OP_pop_scope))
1352                 return E_OUTOFMEMORY;
1353             if(iter->using_except && !push_instr(ctx, OP_pop_except))
1354                 return E_OUTOFMEMORY;
1355         }
1356         stack_pop += iter->stack_use;
1357     }
1358
1359     if(var_stack && stack_pop) {
1360         HRESULT hres;
1361
1362         hres = push_instr_uint(ctx, OP_pop, stack_pop);
1363         if(FAILED(hres))
1364             return hres;
1365     }
1366
1367     return S_OK;
1368 }
1369
1370 /* ECMA-262 3rd Edition    12.7 */
1371 static HRESULT compile_continue_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1372 {
1373     statement_ctx_t *pop_ctx;
1374     HRESULT hres;
1375
1376     if(stat->identifier) {
1377         statement_t *label_stat;
1378         statement_ctx_t *iter;
1379
1380         pop_ctx = NULL;
1381
1382         for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1383             if(iter->continue_label)
1384                 pop_ctx = iter;
1385             if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier))
1386                 break;
1387         }
1388
1389         if(!iter) {
1390             WARN("Label not found\n");
1391             return JS_E_LABEL_NOT_FOUND;
1392         }
1393
1394         /* Labelled continue are allowed only on loops */
1395         for(label_stat = iter->labelled_stat->statement;
1396             label_stat->type == STAT_LABEL;
1397             label_stat = ((labelled_statement_t*)label_stat)->statement);
1398         if(!is_loop_statement(label_stat->type)) {
1399             WARN("Label is not a loop\n");
1400             return JS_E_INVALID_CONTINUE;
1401         }
1402
1403         assert(pop_ctx != NULL);
1404     }else {
1405         for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1406             if(pop_ctx->continue_label)
1407                 break;
1408         }
1409
1410         if(!pop_ctx) {
1411             WARN("continue outside loop\n");
1412             return JS_E_INVALID_CONTINUE;
1413         }
1414     }
1415
1416     hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx);
1417     if(FAILED(hres))
1418         return hres;
1419
1420     return push_instr_uint(ctx, OP_jmp, pop_ctx->continue_label);
1421 }
1422
1423 /* ECMA-262 3rd Edition    12.8 */
1424 static HRESULT compile_break_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1425 {
1426     statement_ctx_t *pop_ctx;
1427     HRESULT hres;
1428
1429     if(stat->identifier) {
1430         for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1431             if(pop_ctx->labelled_stat && !strcmpW(pop_ctx->labelled_stat->identifier, stat->identifier)) {
1432                 assert(pop_ctx->break_label);
1433                 break;
1434             }
1435         }
1436
1437         if(!pop_ctx) {
1438             WARN("Label not found\n");
1439             return JS_E_LABEL_NOT_FOUND;
1440         }
1441     }else {
1442         for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1443             if(pop_ctx->break_label && !pop_ctx->labelled_stat)
1444                 break;
1445         }
1446
1447         if(!pop_ctx) {
1448             WARN("Break outside loop\n");
1449             return JS_E_INVALID_BREAK;
1450         }
1451     }
1452
1453     hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx->next);
1454     if(FAILED(hres))
1455         return hres;
1456
1457     return push_instr_uint(ctx, OP_jmp, pop_ctx->break_label);
1458 }
1459
1460 /* ECMA-262 3rd Edition    12.9 */
1461 static HRESULT compile_return_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1462 {
1463     HRESULT hres;
1464
1465     if(ctx->from_eval) {
1466         WARN("misplaced return statement\n");
1467         return JS_E_MISPLACED_RETURN;
1468     }
1469
1470     hres = pop_to_stat(ctx, TRUE, FALSE, NULL);
1471     if(FAILED(hres))
1472         return hres;
1473
1474     if(stat->expr) {
1475         hres = compile_expression(ctx, stat->expr, TRUE);
1476         if(FAILED(hres))
1477             return hres;
1478         if(!push_instr(ctx, OP_setret))
1479             return E_OUTOFMEMORY;
1480     }
1481
1482     hres = pop_to_stat(ctx, FALSE, TRUE, NULL);
1483     if(FAILED(hres))
1484         return hres;
1485
1486     return push_instr(ctx, OP_ret) ? S_OK : E_OUTOFMEMORY;
1487 }
1488
1489 /* ECMA-262 3rd Edition    12.10 */
1490 static HRESULT compile_with_statement(compiler_ctx_t *ctx, with_statement_t *stat)
1491 {
1492     statement_ctx_t stat_ctx = {0, TRUE, FALSE};
1493     HRESULT hres;
1494
1495     hres = compile_expression(ctx, stat->expr, TRUE);
1496     if(FAILED(hres))
1497         return hres;
1498
1499     if(!push_instr(ctx, OP_push_scope))
1500         return E_OUTOFMEMORY;
1501
1502     hres = compile_statement(ctx, &stat_ctx, stat->statement);
1503     if(FAILED(hres))
1504         return hres;
1505
1506     if(!push_instr(ctx, OP_pop_scope))
1507         return E_OUTOFMEMORY;
1508
1509     return S_OK;
1510 }
1511
1512 /* ECMA-262 3rd Edition    12.10 */
1513 static HRESULT compile_labelled_statement(compiler_ctx_t *ctx, labelled_statement_t *stat)
1514 {
1515     statement_ctx_t stat_ctx = {0, FALSE, FALSE, 0, 0, stat}, *iter;
1516     HRESULT hres;
1517
1518     for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1519         if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier)) {
1520             WARN("Label %s redefined\n", debugstr_w(stat->identifier));
1521             return JS_E_LABEL_REDEFINED;
1522         }
1523     }
1524
1525     /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1526     stat_ctx.break_label = alloc_label(ctx);
1527     if(!stat_ctx.break_label)
1528         return E_OUTOFMEMORY;
1529
1530     hres = compile_statement(ctx, &stat_ctx, stat->statement);
1531     if(FAILED(hres))
1532         return hres;
1533
1534     label_set_addr(ctx, stat_ctx.break_label);
1535     return S_OK;
1536 }
1537
1538 /* ECMA-262 3rd Edition    12.13 */
1539 static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat)
1540 {
1541     statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1542     unsigned case_cnt = 0, *case_jmps, i, default_jmp;
1543     BOOL have_default = FALSE;
1544     statement_t *stat_iter;
1545     case_clausule_t *iter;
1546     HRESULT hres;
1547
1548     hres = compile_expression(ctx, stat->expr, TRUE);
1549     if(FAILED(hres))
1550         return hres;
1551
1552     stat_ctx.break_label = alloc_label(ctx);
1553     if(!stat_ctx.break_label)
1554         return E_OUTOFMEMORY;
1555
1556     for(iter = stat->case_list; iter; iter = iter->next) {
1557         if(iter->expr)
1558             case_cnt++;
1559     }
1560
1561     case_jmps = heap_alloc(case_cnt * sizeof(*case_jmps));
1562     if(!case_jmps)
1563         return E_OUTOFMEMORY;
1564
1565     i = 0;
1566     for(iter = stat->case_list; iter; iter = iter->next) {
1567         if(!iter->expr) {
1568             have_default = TRUE;
1569             continue;
1570         }
1571
1572         hres = compile_expression(ctx, iter->expr, TRUE);
1573         if(FAILED(hres))
1574             break;
1575
1576         case_jmps[i] = push_instr(ctx, OP_case);
1577         if(!case_jmps[i]) {
1578             hres = E_OUTOFMEMORY;
1579             break;
1580         }
1581         i++;
1582     }
1583
1584     if(SUCCEEDED(hres)) {
1585         hres = push_instr_uint(ctx, OP_pop, 1);
1586         if(SUCCEEDED(hres)) {
1587             default_jmp = push_instr(ctx, OP_jmp);
1588             if(!default_jmp)
1589                 hres = E_OUTOFMEMORY;
1590         }
1591     }
1592
1593     if(FAILED(hres)) {
1594         heap_free(case_jmps);
1595         return hres;
1596     }
1597
1598     i = 0;
1599     for(iter = stat->case_list; iter; iter = iter->next) {
1600         while(iter->next && iter->next->stat == iter->stat) {
1601             set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1602             iter = iter->next;
1603         }
1604
1605         set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1606
1607         for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter);
1608             stat_iter = stat_iter->next) {
1609             hres = compile_statement(ctx, &stat_ctx, stat_iter);
1610             if(FAILED(hres))
1611                 break;
1612         }
1613         if(FAILED(hres))
1614             break;
1615     }
1616
1617     heap_free(case_jmps);
1618     if(FAILED(hres))
1619         return hres;
1620     assert(i == case_cnt);
1621
1622     if(!have_default) {
1623         hres = push_instr_uint(ctx, OP_jmp, stat_ctx.break_label);
1624         if(FAILED(hres))
1625             return hres;
1626         set_arg_uint(ctx, default_jmp, ctx->code_off);
1627     }
1628
1629     label_set_addr(ctx, stat_ctx.break_label);
1630     return S_OK;
1631 }
1632
1633 /* ECMA-262 3rd Edition    12.13 */
1634 static HRESULT compile_throw_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1635 {
1636     HRESULT hres;
1637
1638     hres = compile_expression(ctx, stat->expr, TRUE);
1639     if(FAILED(hres))
1640         return hres;
1641
1642     return push_instr(ctx, OP_throw) ? S_OK : E_OUTOFMEMORY;
1643 }
1644
1645 /* ECMA-262 3rd Edition    12.14 */
1646 static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat)
1647 {
1648     statement_ctx_t try_ctx = {0, FALSE, TRUE}, catch_ctx = {0, TRUE, FALSE};
1649     statement_ctx_t finally_ctx = {2, FALSE, FALSE};
1650     unsigned push_except;
1651     BSTR ident;
1652     HRESULT hres;
1653
1654     push_except = push_instr(ctx, OP_push_except);
1655     if(!push_except)
1656         return E_OUTOFMEMORY;
1657
1658     if(stat->catch_block) {
1659         ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier);
1660         if(!ident)
1661             return E_OUTOFMEMORY;
1662     }else {
1663         ident = NULL;
1664     }
1665
1666     instr_ptr(ctx, push_except)->u.arg[1].bstr = ident;
1667
1668     if(!stat->catch_block)
1669         try_ctx.stack_use = 2;
1670
1671     hres = compile_statement(ctx, &try_ctx, stat->try_statement);
1672     if(FAILED(hres))
1673         return hres;
1674
1675     if(!push_instr(ctx, OP_pop_except))
1676         return E_OUTOFMEMORY;
1677
1678     if(stat->catch_block) {
1679         unsigned jmp_finally;
1680
1681         jmp_finally = push_instr(ctx, OP_jmp);
1682         if(!jmp_finally)
1683             return E_OUTOFMEMORY;
1684
1685         instr_ptr(ctx, push_except)->u.arg[0].uint = ctx->code_off;
1686
1687         hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement);
1688         if(FAILED(hres))
1689             return hres;
1690
1691         if(!push_instr(ctx, OP_pop_scope))
1692             return E_OUTOFMEMORY;
1693
1694         set_arg_uint(ctx, jmp_finally, ctx->code_off);
1695     }else {
1696         set_arg_uint(ctx, push_except, ctx->code_off);
1697     }
1698
1699     if(stat->finally_statement) {
1700         hres = compile_statement(ctx, stat->catch_block ? NULL : &finally_ctx, stat->finally_statement);
1701         if(FAILED(hres))
1702             return hres;
1703
1704         if(!stat->catch_block && !push_instr(ctx, OP_end_finally))
1705             return E_OUTOFMEMORY;
1706     }
1707
1708     return S_OK;
1709 }
1710
1711 static HRESULT compile_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1712 {
1713     HRESULT hres;
1714
1715     if(stat_ctx) {
1716         stat_ctx->next = ctx->stat_ctx;
1717         ctx->stat_ctx = stat_ctx;
1718     }
1719
1720     switch(stat->type) {
1721     case STAT_BLOCK:
1722         hres = compile_block_statement(ctx, ((block_statement_t*)stat)->stat_list);
1723         break;
1724     case STAT_BREAK:
1725         hres = compile_break_statement(ctx, (branch_statement_t*)stat);
1726         break;
1727     case STAT_CONTINUE:
1728         hres = compile_continue_statement(ctx, (branch_statement_t*)stat);
1729         break;
1730     case STAT_EMPTY:
1731         /* nothing to do */
1732         hres = S_OK;
1733         break;
1734     case STAT_EXPR:
1735         hres = compile_expression_statement(ctx, (expression_statement_t*)stat);
1736         break;
1737     case STAT_FOR:
1738         hres = compile_for_statement(ctx, (for_statement_t*)stat);
1739         break;
1740     case STAT_FORIN:
1741         hres = compile_forin_statement(ctx, (forin_statement_t*)stat);
1742         break;
1743     case STAT_IF:
1744         hres = compile_if_statement(ctx, (if_statement_t*)stat);
1745         break;
1746     case STAT_LABEL:
1747         hres = compile_labelled_statement(ctx, (labelled_statement_t*)stat);
1748         break;
1749     case STAT_RETURN:
1750         hres = compile_return_statement(ctx, (expression_statement_t*)stat);
1751         break;
1752     case STAT_SWITCH:
1753         hres = compile_switch_statement(ctx, (switch_statement_t*)stat);
1754         break;
1755     case STAT_THROW:
1756         hres = compile_throw_statement(ctx, (expression_statement_t*)stat);
1757         break;
1758     case STAT_TRY:
1759         hres = compile_try_statement(ctx, (try_statement_t*)stat);
1760         break;
1761     case STAT_VAR:
1762         hres = compile_var_statement(ctx, (var_statement_t*)stat);
1763         break;
1764     case STAT_WHILE:
1765         hres = compile_while_statement(ctx, (while_statement_t*)stat);
1766         break;
1767     case STAT_WITH:
1768         hres = compile_with_statement(ctx, (with_statement_t*)stat);
1769         break;
1770     default:
1771         assert(0);
1772         hres = E_FAIL;
1773     }
1774
1775     if(stat_ctx) {
1776         assert(ctx->stat_ctx == stat_ctx);
1777         ctx->stat_ctx = stat_ctx->next;
1778     }
1779
1780     return hres;
1781 }
1782
1783 static void resolve_labels(compiler_ctx_t *ctx, unsigned off)
1784 {
1785     instr_t *instr;
1786
1787     for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
1788         if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->u.arg->uint & LABEL_FLAG)) {
1789             assert((instr->u.arg->uint & ~LABEL_FLAG) < ctx->labels_cnt);
1790             instr->u.arg->uint = ctx->labels[instr->u.arg->uint & ~LABEL_FLAG];
1791         }
1792         assert(instr_info[instr->op].arg2_type != ARG_ADDR);
1793     }
1794
1795     ctx->labels_cnt = 0;
1796 }
1797
1798 void release_bytecode(bytecode_t *code)
1799 {
1800     unsigned i;
1801
1802     if(--code->ref)
1803         return;
1804
1805     for(i=0; i < code->bstr_cnt; i++)
1806         SysFreeString(code->bstr_pool[i]);
1807     for(i=0; i < code->str_cnt; i++)
1808         jsstr_release(code->str_pool[i]);
1809
1810     heap_free(code->source);
1811     heap_pool_free(&code->heap);
1812     heap_free(code->bstr_pool);
1813     heap_free(code->str_pool);
1814     heap_free(code->instrs);
1815     heap_free(code);
1816 }
1817
1818 static HRESULT init_code(compiler_ctx_t *compiler, const WCHAR *source)
1819 {
1820     compiler->code = heap_alloc_zero(sizeof(bytecode_t));
1821     if(!compiler->code)
1822         return E_OUTOFMEMORY;
1823
1824     compiler->code->ref = 1;
1825     heap_pool_init(&compiler->code->heap);
1826
1827     compiler->code->source = heap_strdupW(source);
1828     if(!compiler->code->source) {
1829         release_bytecode(compiler->code);
1830         return E_OUTOFMEMORY;
1831     }
1832
1833     compiler->code->instrs = heap_alloc(64 * sizeof(instr_t));
1834     if(!compiler->code->instrs) {
1835         release_bytecode(compiler->code);
1836         return E_OUTOFMEMORY;
1837     }
1838
1839     compiler->code_size = 64;
1840     compiler->code_off = 1;
1841     return S_OK;
1842 }
1843
1844 static HRESULT compile_function(compiler_ctx_t *ctx, source_elements_t *source, function_expression_t *func_expr,
1845         BOOL from_eval, function_code_t *func)
1846 {
1847     variable_declaration_t *var_iter;
1848     function_expression_t *iter;
1849     unsigned off, i;
1850     HRESULT hres;
1851
1852     TRACE("\n");
1853
1854     ctx->var_head = ctx->var_tail = NULL;
1855     ctx->func_head = ctx->func_tail = NULL;
1856     ctx->from_eval = from_eval;
1857
1858     off = ctx->code_off;
1859     ctx->func = func;
1860     hres = compile_block_statement(ctx, source->statement);
1861     if(FAILED(hres))
1862         return hres;
1863
1864     resolve_labels(ctx, off);
1865
1866     if(!push_instr(ctx, OP_ret))
1867         return E_OUTOFMEMORY;
1868
1869     if(TRACE_ON(jscript_disas))
1870         dump_code(ctx, off);
1871
1872     func->instr_off = off;
1873
1874     if(func_expr && func_expr->identifier) {
1875         func->name = compiler_alloc_bstr(ctx, func_expr->identifier);
1876         if(!func->name)
1877             return E_OUTOFMEMORY;
1878     }
1879
1880     if(func_expr) {
1881         parameter_t *param_iter;
1882
1883         func->source = func_expr->src_str;
1884         func->source_len = func_expr->src_len;
1885
1886         for(param_iter = func_expr->parameter_list; param_iter; param_iter = param_iter->next)
1887             func->param_cnt++;
1888
1889         func->params = compiler_alloc(ctx->code, func->param_cnt * sizeof(*func->params));
1890         if(!func->params)
1891             return E_OUTOFMEMORY;
1892
1893         for(param_iter = func_expr->parameter_list, i=0; param_iter; param_iter = param_iter->next, i++) {
1894             func->params[i] = compiler_alloc_bstr(ctx, param_iter->identifier);
1895             if(!func->params[i])
1896                 return E_OUTOFMEMORY;
1897         }
1898     }
1899
1900     func->variables = compiler_alloc(ctx->code, func->var_cnt * sizeof(*func->variables));
1901     if(!func->variables)
1902         return E_OUTOFMEMORY;
1903
1904     for(var_iter = ctx->var_head, i=0; var_iter; var_iter = var_iter->global_next, i++) {
1905         func->variables[i] = compiler_alloc_bstr(ctx, var_iter->identifier);
1906         if(!func->variables[i])
1907             return E_OUTOFMEMORY;
1908     }
1909
1910     assert(i == func->var_cnt);
1911
1912     func->funcs = compiler_alloc(ctx->code, func->func_cnt * sizeof(*func->funcs));
1913     if(!func->funcs)
1914         return E_OUTOFMEMORY;
1915     memset(func->funcs, 0, func->func_cnt * sizeof(*func->funcs));
1916
1917     for(iter = ctx->func_head, i=0; iter; iter = iter->next, i++) {
1918         hres = compile_function(ctx, iter->source_elements, iter, FALSE, func->funcs+i);
1919         if(FAILED(hres))
1920             return hres;
1921     }
1922
1923     assert(i == func->func_cnt);
1924
1925     return S_OK;
1926 }
1927
1928 static HRESULT parse_arguments(compiler_ctx_t *ctx, const WCHAR *args, BSTR *arg_array, unsigned *args_size)
1929 {
1930     const WCHAR *ptr = args, *ptr2;
1931     unsigned arg_cnt = 0;
1932
1933     while(isspaceW(*ptr))
1934         ptr++;
1935     if(!*ptr) {
1936         if(args_size)
1937             *args_size = 0;
1938         return S_OK;
1939     }
1940
1941     while(1) {
1942         if(!isalphaW(*ptr) && *ptr != '_') {
1943             FIXME("expected alpha or '_': %s\n", debugstr_w(ptr));
1944             return E_FAIL;
1945         }
1946
1947         ptr2 = ptr;
1948         while(isalnumW(*ptr) || *ptr == '_')
1949             ptr++;
1950
1951         if(*ptr && *ptr != ',' && !isspaceW(*ptr)) {
1952             FIXME("unexpected har %s\n", debugstr_w(ptr));
1953             return E_FAIL;
1954         }
1955
1956         if(arg_array) {
1957             arg_array[arg_cnt] = compiler_alloc_bstr_len(ctx, ptr2, ptr-ptr2);
1958             if(!arg_array[arg_cnt])
1959                 return E_OUTOFMEMORY;
1960         }
1961         arg_cnt++;
1962
1963         while(isspaceW(*ptr))
1964             ptr++;
1965         if(!*ptr)
1966             break;
1967         if(*ptr != ',') {
1968             FIXME("expected ',': %s\n", debugstr_w(ptr));
1969             return E_FAIL;
1970         }
1971
1972         ptr++;
1973         while(isspaceW(*ptr))
1974             ptr++;
1975     }
1976
1977     if(args_size)
1978         *args_size = arg_cnt;
1979     return S_OK;
1980 }
1981
1982 static HRESULT compile_arguments(compiler_ctx_t *ctx, const WCHAR *args)
1983 {
1984     HRESULT hres;
1985
1986     hres = parse_arguments(ctx, args, NULL, &ctx->code->global_code.param_cnt);
1987     if(FAILED(hres))
1988         return hres;
1989
1990     ctx->code->global_code.params = compiler_alloc(ctx->code,
1991             ctx->code->global_code.param_cnt * sizeof(*ctx->code->global_code.params));
1992     if(!ctx->code->global_code.params)
1993         return E_OUTOFMEMORY;
1994
1995     return parse_arguments(ctx, args, ctx->code->global_code.params, NULL);
1996 }
1997
1998 HRESULT compile_script(script_ctx_t *ctx, const WCHAR *code, const WCHAR *args, const WCHAR *delimiter,
1999         BOOL from_eval, BOOL use_decode, bytecode_t **ret)
2000 {
2001     compiler_ctx_t compiler = {0};
2002     HRESULT hres;
2003
2004     hres = init_code(&compiler, code);
2005     if(FAILED(hres))
2006         return hres;
2007
2008     if(args) {
2009         hres = compile_arguments(&compiler, args);
2010         if(FAILED(hres))
2011             return hres;
2012     }
2013
2014     if(use_decode) {
2015         hres = decode_source(compiler.code->source);
2016         if(FAILED(hres)) {
2017             WARN("Decoding failed\n");
2018             return hres;
2019         }
2020     }
2021
2022     hres = script_parse(ctx, compiler.code->source, delimiter, from_eval, &compiler.parser);
2023     if(FAILED(hres)) {
2024         release_bytecode(compiler.code);
2025         return hres;
2026     }
2027
2028     hres = compile_function(&compiler, compiler.parser->source, NULL, from_eval, &compiler.code->global_code);
2029     parser_release(compiler.parser);
2030     if(FAILED(hres)) {
2031         release_bytecode(compiler.code);
2032         return hres;
2033     }
2034
2035     *ret = compiler.code;
2036     return S_OK;
2037 }