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