* Sync up to trunk head (r65270).
[reactos.git] / dll / win32 / vbscript / 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 "vbscript.h"
20
21 WINE_DECLARE_DEBUG_CHANNEL(vbscript_disas);
22
23 typedef struct _statement_ctx_t {
24 unsigned stack_use;
25
26 unsigned while_end_label;
27 unsigned for_end_label;
28
29 struct _statement_ctx_t *next;
30 } statement_ctx_t;
31
32 typedef struct {
33 parser_ctx_t parser;
34
35 unsigned instr_cnt;
36 unsigned instr_size;
37 vbscode_t *code;
38
39 statement_ctx_t *stat_ctx;
40
41 unsigned *labels;
42 unsigned labels_size;
43 unsigned labels_cnt;
44
45 unsigned sub_end_label;
46 unsigned func_end_label;
47 unsigned prop_end_label;
48
49 dim_decl_t *dim_decls;
50 dim_decl_t *dim_decls_tail;
51 dynamic_var_t *global_vars;
52
53 const_decl_t *const_decls;
54 const_decl_t *global_consts;
55
56 function_t *func;
57 function_t *funcs;
58 function_decl_t *func_decls;
59
60 class_desc_t *classes;
61 } compile_ctx_t;
62
63 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
64 static HRESULT compile_statement(compile_ctx_t*,statement_ctx_t*,statement_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 case ARG_BSTR:
81 TRACE_(vbscript_disas)("\t%s", debugstr_w(arg->str));
82 break;
83 case ARG_INT:
84 TRACE_(vbscript_disas)("\t%d", arg->uint);
85 break;
86 case ARG_UINT:
87 case ARG_ADDR:
88 TRACE_(vbscript_disas)("\t%u", arg->uint);
89 break;
90 case ARG_DOUBLE:
91 TRACE_(vbscript_disas)("\t%lf", *arg->dbl);
92 break;
93 case ARG_NONE:
94 break;
95 DEFAULT_UNREACHABLE;
96 }
97 }
98
99 static void dump_code(compile_ctx_t *ctx)
100 {
101 instr_t *instr;
102
103 for(instr = ctx->code->instrs+1; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
104 assert(instr->op < OP_LAST);
105 TRACE_(vbscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
106 dump_instr_arg(instr_info[instr->op].arg1_type, &instr->arg1);
107 dump_instr_arg(instr_info[instr->op].arg2_type, &instr->arg2);
108 TRACE_(vbscript_disas)("\n");
109 }
110 }
111
112 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
113 {
114 return heap_pool_alloc(&vbscode->heap, size);
115 }
116
117 static inline void *compiler_alloc_zero(vbscode_t *vbscode, size_t size)
118 {
119 void *ret;
120
121 ret = heap_pool_alloc(&vbscode->heap, size);
122 if(ret)
123 memset(ret, 0, size);
124 return ret;
125 }
126
127 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
128 {
129 size_t size;
130 WCHAR *ret;
131
132 size = (strlenW(str)+1)*sizeof(WCHAR);
133 ret = compiler_alloc(vbscode, size);
134 if(ret)
135 memcpy(ret, str, size);
136 return ret;
137 }
138
139 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
140 {
141 assert(id < ctx->instr_cnt);
142 return ctx->code->instrs + id;
143 }
144
145 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
146 {
147 assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
148
149 if(ctx->instr_size == ctx->instr_cnt) {
150 instr_t *new_instr;
151
152 new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
153 if(!new_instr)
154 return 0;
155
156 ctx->code->instrs = new_instr;
157 ctx->instr_size *= 2;
158 }
159
160 ctx->code->instrs[ctx->instr_cnt].op = op;
161 return ctx->instr_cnt++;
162 }
163
164 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
165 {
166 unsigned ret;
167
168 ret = push_instr(ctx, op);
169 if(!ret)
170 return E_OUTOFMEMORY;
171
172 instr_ptr(ctx, ret)->arg1.lng = arg;
173 return S_OK;
174 }
175
176 static HRESULT push_instr_uint(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
177 {
178 unsigned ret;
179
180 ret = push_instr(ctx, op);
181 if(!ret)
182 return E_OUTOFMEMORY;
183
184 instr_ptr(ctx, ret)->arg1.uint = arg;
185 return S_OK;
186 }
187
188 static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
189 {
190 unsigned ret;
191
192 ret = push_instr(ctx, op);
193 if(!ret)
194 return E_OUTOFMEMORY;
195
196 instr_ptr(ctx, ret)->arg1.uint = arg;
197 return S_OK;
198 }
199
200 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
201 {
202 unsigned instr;
203 WCHAR *str;
204
205 str = compiler_alloc_string(ctx->code, arg);
206 if(!str)
207 return E_OUTOFMEMORY;
208
209 instr = push_instr(ctx, op);
210 if(!instr)
211 return E_OUTOFMEMORY;
212
213 instr_ptr(ctx, instr)->arg1.str = str;
214 return S_OK;
215 }
216
217 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
218 {
219 unsigned instr;
220 double *d;
221
222 d = compiler_alloc(ctx->code, sizeof(double));
223 if(!d)
224 return E_OUTOFMEMORY;
225
226 instr = push_instr(ctx, op);
227 if(!instr)
228 return E_OUTOFMEMORY;
229
230 *d = arg;
231 instr_ptr(ctx, instr)->arg1.dbl = d;
232 return S_OK;
233 }
234
235 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
236 {
237 if(!ctx->code->bstr_pool_size) {
238 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
239 if(!ctx->code->bstr_pool)
240 return NULL;
241 ctx->code->bstr_pool_size = 8;
242 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
243 BSTR *new_pool;
244
245 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
246 if(!new_pool)
247 return NULL;
248
249 ctx->code->bstr_pool = new_pool;
250 ctx->code->bstr_pool_size *= 2;
251 }
252
253 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
254 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
255 return NULL;
256
257 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
258 }
259
260 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
261 {
262 unsigned instr;
263 BSTR bstr;
264
265 bstr = alloc_bstr_arg(ctx, arg);
266 if(!bstr)
267 return E_OUTOFMEMORY;
268
269 instr = push_instr(ctx, op);
270 if(!instr)
271 return E_OUTOFMEMORY;
272
273 instr_ptr(ctx, instr)->arg1.bstr = bstr;
274 return S_OK;
275 }
276
277 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
278 {
279 unsigned instr;
280 BSTR bstr;
281
282 bstr = alloc_bstr_arg(ctx, arg1);
283 if(!bstr)
284 return E_OUTOFMEMORY;
285
286 instr = push_instr(ctx, op);
287 if(!instr)
288 return E_OUTOFMEMORY;
289
290 instr_ptr(ctx, instr)->arg1.bstr = bstr;
291 instr_ptr(ctx, instr)->arg2.uint = arg2;
292 return S_OK;
293 }
294
295 static HRESULT push_instr_uint_bstr(compile_ctx_t *ctx, vbsop_t op, unsigned arg1, const WCHAR *arg2)
296 {
297 unsigned instr;
298 BSTR bstr;
299
300 bstr = alloc_bstr_arg(ctx, arg2);
301 if(!bstr)
302 return E_OUTOFMEMORY;
303
304 instr = push_instr(ctx, op);
305 if(!instr)
306 return E_OUTOFMEMORY;
307
308 instr_ptr(ctx, instr)->arg1.uint = arg1;
309 instr_ptr(ctx, instr)->arg2.bstr = bstr;
310 return S_OK;
311 }
312
313 #define LABEL_FLAG 0x80000000
314
315 static unsigned alloc_label(compile_ctx_t *ctx)
316 {
317 if(!ctx->labels_size) {
318 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
319 if(!ctx->labels)
320 return 0;
321 ctx->labels_size = 8;
322 }else if(ctx->labels_size == ctx->labels_cnt) {
323 unsigned *new_labels;
324
325 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
326 if(!new_labels)
327 return 0;
328
329 ctx->labels = new_labels;
330 ctx->labels_size *= 2;
331 }
332
333 return ctx->labels_cnt++ | LABEL_FLAG;
334 }
335
336 static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
337 {
338 assert(label & LABEL_FLAG);
339 ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
340 }
341
342 static inline unsigned stack_offset(compile_ctx_t *ctx)
343 {
344 statement_ctx_t *iter;
345 unsigned ret = 0;
346
347 for(iter = ctx->stat_ctx; iter; iter = iter->next)
348 ret += iter->stack_use;
349
350 return ret;
351 }
352
353 static BOOL emit_catch_jmp(compile_ctx_t *ctx, unsigned stack_off, unsigned code_off)
354 {
355 unsigned code;
356
357 code = push_instr(ctx, OP_catch);
358 if(!code)
359 return FALSE;
360
361 instr_ptr(ctx, code)->arg1.uint = code_off;
362 instr_ptr(ctx, code)->arg2.uint = stack_off + stack_offset(ctx);
363 return TRUE;
364 }
365
366 static inline BOOL emit_catch(compile_ctx_t *ctx, unsigned off)
367 {
368 return emit_catch_jmp(ctx, off, ctx->instr_cnt);
369 }
370
371 static expression_t *lookup_const_decls(compile_ctx_t *ctx, const WCHAR *name, BOOL lookup_global)
372 {
373 const_decl_t *decl;
374
375 for(decl = ctx->const_decls; decl; decl = decl->next) {
376 if(!strcmpiW(decl->name, name))
377 return decl->value_expr;
378 }
379
380 if(!lookup_global)
381 return NULL;
382
383 for(decl = ctx->global_consts; decl; decl = decl->next) {
384 if(!strcmpiW(decl->name, name))
385 return decl->value_expr;
386 }
387
388 return NULL;
389 }
390
391 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
392 {
393 unsigned arg_cnt = 0;
394 HRESULT hres;
395
396 while(args) {
397 hres = compile_expression(ctx, args);
398 if(FAILED(hres))
399 return hres;
400
401 arg_cnt++;
402 args = args->next;
403 }
404
405 *ret = arg_cnt;
406 return S_OK;
407 }
408
409 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
410 {
411 unsigned arg_cnt = 0;
412 HRESULT hres;
413
414 if(ret_val && !expr->args) {
415 expression_t *const_expr;
416
417 const_expr = lookup_const_decls(ctx, expr->identifier, TRUE);
418 if(const_expr)
419 return compile_expression(ctx, const_expr);
420 }
421
422 hres = compile_args(ctx, expr->args, &arg_cnt);
423 if(FAILED(hres))
424 return hres;
425
426 if(expr->obj_expr) {
427 hres = compile_expression(ctx, expr->obj_expr);
428 if(FAILED(hres))
429 return hres;
430
431 hres = push_instr_bstr_uint(ctx, ret_val ? OP_mcall : OP_mcallv, expr->identifier, arg_cnt);
432 }else {
433 hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
434 }
435
436 return hres;
437 }
438
439 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
440 {
441 HRESULT hres;
442
443 hres = compile_expression(ctx, expr->subexpr);
444 if(FAILED(hres))
445 return hres;
446
447 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
448 }
449
450 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
451 {
452 HRESULT hres;
453
454 hres = compile_expression(ctx, expr->left);
455 if(FAILED(hres))
456 return hres;
457
458 hres = compile_expression(ctx, expr->right);
459 if(FAILED(hres))
460 return hres;
461
462 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
463 }
464
465 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
466 {
467 switch(expr->type) {
468 case EXPR_ADD:
469 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
470 case EXPR_AND:
471 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
472 case EXPR_BOOL:
473 return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
474 case EXPR_BRACKETS:
475 return compile_expression(ctx, ((unary_expression_t*)expr)->subexpr);
476 case EXPR_CONCAT:
477 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
478 case EXPR_DIV:
479 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
480 case EXPR_DOUBLE:
481 return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
482 case EXPR_EMPTY:
483 return push_instr(ctx, OP_empty) ? S_OK : E_OUTOFMEMORY;
484 case EXPR_EQUAL:
485 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
486 case EXPR_EQV:
487 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eqv);
488 case EXPR_EXP:
489 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
490 case EXPR_GT:
491 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
492 case EXPR_GTEQ:
493 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
494 case EXPR_IDIV:
495 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
496 case EXPR_IS:
497 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_is);
498 case EXPR_IMP:
499 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_imp);
500 case EXPR_LT:
501 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
502 case EXPR_LTEQ:
503 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
504 case EXPR_ME:
505 return push_instr(ctx, OP_me) ? S_OK : E_OUTOFMEMORY;
506 case EXPR_MEMBER:
507 return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
508 case EXPR_MOD:
509 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
510 case EXPR_MUL:
511 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
512 case EXPR_NEG:
513 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
514 case EXPR_NEQUAL:
515 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
516 case EXPR_NEW:
517 return push_instr_str(ctx, OP_new, ((string_expression_t*)expr)->value);
518 case EXPR_NOT:
519 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
520 case EXPR_NOTHING:
521 return push_instr(ctx, OP_nothing) ? S_OK : E_OUTOFMEMORY;
522 case EXPR_NULL:
523 return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
524 case EXPR_OR:
525 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
526 case EXPR_STRING:
527 return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
528 case EXPR_SUB:
529 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
530 case EXPR_USHORT:
531 return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
532 case EXPR_ULONG:
533 return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
534 case EXPR_XOR:
535 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
536 default:
537 FIXME("Unimplemented expression type %d\n", expr->type);
538 return E_NOTIMPL;
539 }
540
541 return S_OK;
542 }
543
544 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
545 {
546 unsigned cnd_jmp, endif_label = 0;
547 elseif_decl_t *elseif_decl;
548 HRESULT hres;
549
550 hres = compile_expression(ctx, stat->expr);
551 if(FAILED(hres))
552 return hres;
553
554 cnd_jmp = push_instr(ctx, OP_jmp_false);
555 if(!cnd_jmp)
556 return E_OUTOFMEMORY;
557
558 if(!emit_catch(ctx, 0))
559 return E_OUTOFMEMORY;
560
561 hres = compile_statement(ctx, NULL, stat->if_stat);
562 if(FAILED(hres))
563 return hres;
564
565 if(stat->else_stat || stat->elseifs) {
566 endif_label = alloc_label(ctx);
567 if(!endif_label)
568 return E_OUTOFMEMORY;
569
570 hres = push_instr_addr(ctx, OP_jmp, endif_label);
571 if(FAILED(hres))
572 return hres;
573 }
574
575 for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
576 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
577
578 hres = compile_expression(ctx, elseif_decl->expr);
579 if(FAILED(hres))
580 return hres;
581
582 cnd_jmp = push_instr(ctx, OP_jmp_false);
583 if(!cnd_jmp)
584 return E_OUTOFMEMORY;
585
586 if(!emit_catch(ctx, 0))
587 return E_OUTOFMEMORY;
588
589 hres = compile_statement(ctx, NULL, elseif_decl->stat);
590 if(FAILED(hres))
591 return hres;
592
593 hres = push_instr_addr(ctx, OP_jmp, endif_label);
594 if(FAILED(hres))
595 return hres;
596 }
597
598 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
599
600 if(stat->else_stat) {
601 hres = compile_statement(ctx, NULL, stat->else_stat);
602 if(FAILED(hres))
603 return hres;
604 }
605
606 if(endif_label)
607 label_set_addr(ctx, endif_label);
608 return S_OK;
609 }
610
611 static HRESULT compile_while_statement(compile_ctx_t *ctx, while_statement_t *stat)
612 {
613 statement_ctx_t stat_ctx = {0}, *loop_ctx;
614 unsigned start_addr;
615 unsigned jmp_end;
616 HRESULT hres;
617
618 start_addr = ctx->instr_cnt;
619
620 hres = compile_expression(ctx, stat->expr);
621 if(FAILED(hres))
622 return hres;
623
624 jmp_end = push_instr(ctx, stat->stat.type == STAT_UNTIL ? OP_jmp_true : OP_jmp_false);
625 if(!jmp_end)
626 return E_OUTOFMEMORY;
627
628 if(!emit_catch(ctx, 0))
629 return E_OUTOFMEMORY;
630
631 if(stat->stat.type == STAT_WHILE) {
632 loop_ctx = NULL;
633 }else {
634 if(!(stat_ctx.while_end_label = alloc_label(ctx)))
635 return E_OUTOFMEMORY;
636 loop_ctx = &stat_ctx;
637 }
638
639 hres = compile_statement(ctx, loop_ctx, stat->body);
640 if(FAILED(hres))
641 return hres;
642
643 hres = push_instr_addr(ctx, OP_jmp, start_addr);
644 if(FAILED(hres))
645 return hres;
646
647 instr_ptr(ctx, jmp_end)->arg1.uint = ctx->instr_cnt;
648
649 if(loop_ctx)
650 label_set_addr(ctx, stat_ctx.while_end_label);
651
652 return S_OK;
653 }
654
655 static HRESULT compile_dowhile_statement(compile_ctx_t *ctx, while_statement_t *stat)
656 {
657 statement_ctx_t loop_ctx = {0};
658 unsigned start_addr;
659 vbsop_t jmp_op;
660 HRESULT hres;
661
662 start_addr = ctx->instr_cnt;
663
664 if(!(loop_ctx.while_end_label = alloc_label(ctx)))
665 return E_OUTOFMEMORY;
666
667 hres = compile_statement(ctx, &loop_ctx, stat->body);
668 if(FAILED(hres))
669 return hres;
670
671 if(stat->expr) {
672 hres = compile_expression(ctx, stat->expr);
673 if(FAILED(hres))
674 return hres;
675
676 jmp_op = stat->stat.type == STAT_DOUNTIL ? OP_jmp_false : OP_jmp_true;
677 }else {
678 jmp_op = OP_jmp;
679 }
680
681 hres = push_instr_addr(ctx, jmp_op, start_addr);
682 if(FAILED(hres))
683 return hres;
684
685 label_set_addr(ctx, loop_ctx.while_end_label);
686
687 if(!emit_catch(ctx, 0))
688 return E_OUTOFMEMORY;
689
690 return S_OK;
691 }
692
693 static HRESULT compile_foreach_statement(compile_ctx_t *ctx, foreach_statement_t *stat)
694 {
695 statement_ctx_t loop_ctx = {1};
696 unsigned loop_start;
697 HRESULT hres;
698
699 /* Preserve a place on the stack in case we throw before having proper enum collection. */
700 if(!push_instr(ctx, OP_empty))
701 return E_OUTOFMEMORY;
702
703 hres = compile_expression(ctx, stat->group_expr);
704 if(FAILED(hres))
705 return hres;
706
707 if(!push_instr(ctx, OP_newenum))
708 return E_OUTOFMEMORY;
709
710 if(!(loop_ctx.for_end_label = alloc_label(ctx)))
711 return E_OUTOFMEMORY;
712
713 hres = push_instr_uint_bstr(ctx, OP_enumnext, loop_ctx.for_end_label, stat->identifier);
714 if(FAILED(hres))
715 return hres;
716
717 if(!emit_catch(ctx, 1))
718 return E_OUTOFMEMORY;
719
720 loop_start = ctx->instr_cnt;
721 hres = compile_statement(ctx, &loop_ctx, stat->body);
722 if(FAILED(hres))
723 return hres;
724
725 /* We need a separated enumnext here, because we need to jump out of the loop on exception. */
726 hres = push_instr_uint_bstr(ctx, OP_enumnext, loop_ctx.for_end_label, stat->identifier);
727 if(FAILED(hres))
728 return hres;
729
730 hres = push_instr_addr(ctx, OP_jmp, loop_start);
731 if(FAILED(hres))
732 return hres;
733
734 label_set_addr(ctx, loop_ctx.for_end_label);
735 return S_OK;
736 }
737
738 static HRESULT compile_forto_statement(compile_ctx_t *ctx, forto_statement_t *stat)
739 {
740 statement_ctx_t loop_ctx = {2};
741 unsigned step_instr, instr;
742 BSTR identifier;
743 HRESULT hres;
744
745 identifier = alloc_bstr_arg(ctx, stat->identifier);
746 if(!identifier)
747 return E_OUTOFMEMORY;
748
749 hres = compile_expression(ctx, stat->from_expr);
750 if(FAILED(hres))
751 return hres;
752
753 /* FIXME: Assign should happen after both expressions evaluation. */
754 instr = push_instr(ctx, OP_assign_ident);
755 if(!instr)
756 return E_OUTOFMEMORY;
757 instr_ptr(ctx, instr)->arg1.bstr = identifier;
758 instr_ptr(ctx, instr)->arg2.uint = 0;
759
760 hres = compile_expression(ctx, stat->to_expr);
761 if(FAILED(hres))
762 return hres;
763
764 if(!push_instr(ctx, OP_val))
765 return E_OUTOFMEMORY;
766
767 if(stat->step_expr) {
768 hres = compile_expression(ctx, stat->step_expr);
769 if(FAILED(hres))
770 return hres;
771
772 if(!push_instr(ctx, OP_val))
773 return E_OUTOFMEMORY;
774 }else {
775 hres = push_instr_int(ctx, OP_short, 1);
776 if(FAILED(hres))
777 return hres;
778 }
779
780 loop_ctx.for_end_label = alloc_label(ctx);
781 if(!loop_ctx.for_end_label)
782 return E_OUTOFMEMORY;
783
784 step_instr = push_instr(ctx, OP_step);
785 if(!step_instr)
786 return E_OUTOFMEMORY;
787 instr_ptr(ctx, step_instr)->arg2.bstr = identifier;
788 instr_ptr(ctx, step_instr)->arg1.uint = loop_ctx.for_end_label;
789
790 if(!emit_catch(ctx, 2))
791 return E_OUTOFMEMORY;
792
793 hres = compile_statement(ctx, &loop_ctx, stat->body);
794 if(FAILED(hres))
795 return hres;
796
797 /* FIXME: Error handling can't be done compatible with native using OP_incc here. */
798 instr = push_instr(ctx, OP_incc);
799 if(!instr)
800 return E_OUTOFMEMORY;
801 instr_ptr(ctx, instr)->arg1.bstr = identifier;
802
803 hres = push_instr_addr(ctx, OP_jmp, step_instr);
804 if(FAILED(hres))
805 return hres;
806
807 hres = push_instr_uint(ctx, OP_pop, 2);
808 if(FAILED(hres))
809 return hres;
810
811 label_set_addr(ctx, loop_ctx.for_end_label);
812
813 /* FIXME: reconsider after OP_incc fixup. */
814 if(!emit_catch(ctx, 0))
815 return E_OUTOFMEMORY;
816
817 return S_OK;
818 }
819
820 static HRESULT compile_select_statement(compile_ctx_t *ctx, select_statement_t *stat)
821 {
822 unsigned end_label, case_cnt = 0, *case_labels = NULL, i;
823 case_clausule_t *case_iter;
824 expression_t *expr_iter;
825 HRESULT hres;
826
827 hres = compile_expression(ctx, stat->expr);
828 if(FAILED(hres))
829 return hres;
830
831 if(!push_instr(ctx, OP_val))
832 return E_OUTOFMEMORY;
833
834 end_label = alloc_label(ctx);
835 if(!end_label)
836 return E_OUTOFMEMORY;
837
838 if(!emit_catch_jmp(ctx, 0, end_label))
839 return E_OUTOFMEMORY;
840
841 for(case_iter = stat->case_clausules; case_iter; case_iter = case_iter->next)
842 case_cnt++;
843
844 if(case_cnt) {
845 case_labels = heap_alloc(case_cnt*sizeof(*case_labels));
846 if(!case_labels)
847 return E_OUTOFMEMORY;
848 }
849
850 for(case_iter = stat->case_clausules, i=0; case_iter; case_iter = case_iter->next, i++) {
851 case_labels[i] = alloc_label(ctx);
852 if(!case_labels[i]) {
853 hres = E_OUTOFMEMORY;
854 break;
855 }
856
857 if(!case_iter->expr)
858 break;
859
860 for(expr_iter = case_iter->expr; expr_iter; expr_iter = expr_iter->next) {
861 hres = compile_expression(ctx, expr_iter);
862 if(FAILED(hres))
863 break;
864
865 hres = push_instr_addr(ctx, OP_case, case_labels[i]);
866 if(FAILED(hres))
867 break;
868
869 if(!emit_catch_jmp(ctx, 0, case_labels[i])) {
870 hres = E_OUTOFMEMORY;
871 break;
872 }
873 }
874 }
875
876 if(FAILED(hres)) {
877 heap_free(case_labels);
878 return hres;
879 }
880
881 hres = push_instr_uint(ctx, OP_pop, 1);
882 if(FAILED(hres)) {
883 heap_free(case_labels);
884 return hres;
885 }
886
887 hres = push_instr_addr(ctx, OP_jmp, case_iter ? case_labels[i] : end_label);
888 if(FAILED(hres)) {
889 heap_free(case_labels);
890 return hres;
891 }
892
893 for(case_iter = stat->case_clausules, i=0; case_iter; case_iter = case_iter->next, i++) {
894 label_set_addr(ctx, case_labels[i]);
895 hres = compile_statement(ctx, NULL, case_iter->stat);
896 if(FAILED(hres))
897 break;
898
899 if(!case_iter->next)
900 break;
901
902 hres = push_instr_addr(ctx, OP_jmp, end_label);
903 if(FAILED(hres))
904 break;
905 }
906
907 heap_free(case_labels);
908 if(FAILED(hres))
909 return hres;
910
911 label_set_addr(ctx, end_label);
912 return S_OK;
913 }
914
915 static HRESULT compile_assignment(compile_ctx_t *ctx, member_expression_t *member_expr, expression_t *value_expr, BOOL is_set)
916 {
917 unsigned args_cnt;
918 vbsop_t op;
919 HRESULT hres;
920
921 if(member_expr->obj_expr) {
922 hres = compile_expression(ctx, member_expr->obj_expr);
923 if(FAILED(hres))
924 return hres;
925
926 op = is_set ? OP_set_member : OP_assign_member;
927 }else {
928 op = is_set ? OP_set_ident : OP_assign_ident;
929 }
930
931 hres = compile_expression(ctx, value_expr);
932 if(FAILED(hres))
933 return hres;
934
935 hres = compile_args(ctx, member_expr->args, &args_cnt);
936 if(FAILED(hres))
937 return hres;
938
939 hres = push_instr_bstr_uint(ctx, op, member_expr->identifier, args_cnt);
940 if(FAILED(hres))
941 return hres;
942
943 if(!emit_catch(ctx, 0))
944 return E_OUTOFMEMORY;
945
946 return S_OK;
947 }
948
949 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat, BOOL is_set)
950 {
951 return compile_assignment(ctx, stat->member_expr, stat->value_expr, is_set);
952 }
953
954 static HRESULT compile_call_statement(compile_ctx_t *ctx, call_statement_t *stat)
955 {
956 HRESULT hres;
957
958 /* It's challenging for parser to distinguish parameterized assignment with one argument from call
959 * with equality expression argument, so we do it in compiler. */
960 if(!stat->is_strict && stat->expr->args && !stat->expr->args->next && stat->expr->args->type == EXPR_EQUAL) {
961 binary_expression_t *eqexpr = (binary_expression_t*)stat->expr->args;
962
963 if(eqexpr->left->type == EXPR_BRACKETS) {
964 member_expression_t new_member = *stat->expr;
965
966 WARN("converting call expr to assign expr\n");
967
968 new_member.args = ((unary_expression_t*)eqexpr->left)->subexpr;
969 return compile_assignment(ctx, &new_member, eqexpr->right, FALSE);
970 }
971 }
972
973 hres = compile_member_expression(ctx, stat->expr, FALSE);
974 if(FAILED(hres))
975 return hres;
976
977 if(!emit_catch(ctx, 0))
978 return E_OUTOFMEMORY;
979
980 return S_OK;
981 }
982
983 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
984 {
985 dim_decl_t *dim_decl;
986
987 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
988 if(!strcmpiW(dim_decl->name, name))
989 return TRUE;
990 }
991
992 return FALSE;
993 }
994
995 static BOOL lookup_args_name(compile_ctx_t *ctx, const WCHAR *name)
996 {
997 unsigned i;
998
999 for(i = 0; i < ctx->func->arg_cnt; i++) {
1000 if(!strcmpiW(ctx->func->args[i].name, name))
1001 return TRUE;
1002 }
1003
1004 return FALSE;
1005 }
1006
1007 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
1008 {
1009 dim_decl_t *dim_decl = stat->dim_decls;
1010
1011 while(1) {
1012 if(lookup_dim_decls(ctx, dim_decl->name) || lookup_args_name(ctx, dim_decl->name)
1013 || lookup_const_decls(ctx, dim_decl->name, FALSE)) {
1014 FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
1015 return E_FAIL;
1016 }
1017
1018 ctx->func->var_cnt++;
1019
1020 if(dim_decl->is_array) {
1021 HRESULT hres = push_instr_bstr_uint(ctx, OP_dim, dim_decl->name, ctx->func->array_cnt++);
1022 if(FAILED(hres))
1023 return hres;
1024
1025 if(!emit_catch(ctx, 0))
1026 return E_OUTOFMEMORY;
1027 }
1028
1029 if(!dim_decl->next)
1030 break;
1031 dim_decl = dim_decl->next;
1032 }
1033
1034 if(ctx->dim_decls_tail)
1035 ctx->dim_decls_tail->next = stat->dim_decls;
1036 else
1037 ctx->dim_decls = stat->dim_decls;
1038 ctx->dim_decls_tail = dim_decl;
1039 return S_OK;
1040 }
1041
1042 static HRESULT compile_const_statement(compile_ctx_t *ctx, const_statement_t *stat)
1043 {
1044 const_decl_t *decl, *next_decl = stat->decls;
1045
1046 do {
1047 decl = next_decl;
1048
1049 if(lookup_const_decls(ctx, decl->name, FALSE) || lookup_args_name(ctx, decl->name)
1050 || lookup_dim_decls(ctx, decl->name)) {
1051 FIXME("%s redefined\n", debugstr_w(decl->name));
1052 return E_FAIL;
1053 }
1054
1055 if(ctx->func->type == FUNC_GLOBAL) {
1056 HRESULT hres;
1057
1058 hres = compile_expression(ctx, decl->value_expr);
1059 if(FAILED(hres))
1060 return hres;
1061
1062 hres = push_instr_bstr(ctx, OP_const, decl->name);
1063 if(FAILED(hres))
1064 return hres;
1065
1066 if(!emit_catch(ctx, 0))
1067 return E_OUTOFMEMORY;
1068 }
1069
1070 next_decl = decl->next;
1071 decl->next = ctx->const_decls;
1072 ctx->const_decls = decl;
1073 } while(next_decl);
1074
1075 return S_OK;
1076 }
1077
1078 static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
1079 {
1080 if(ctx->func != &ctx->code->main_code) {
1081 FIXME("Function is not in the global code\n");
1082 return E_FAIL;
1083 }
1084
1085 stat->func_decl->next = ctx->func_decls;
1086 ctx->func_decls = stat->func_decl;
1087 return S_OK;
1088 }
1089
1090 static HRESULT compile_exitdo_statement(compile_ctx_t *ctx)
1091 {
1092 statement_ctx_t *iter;
1093 unsigned pop_cnt = 0;
1094
1095 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1096 pop_cnt += iter->stack_use;
1097 if(iter->while_end_label)
1098 break;
1099 }
1100 if(!iter) {
1101 FIXME("Exit Do outside Do Loop\n");
1102 return E_FAIL;
1103 }
1104
1105 if(pop_cnt) {
1106 HRESULT hres;
1107
1108 hres = push_instr_uint(ctx, OP_pop, pop_cnt);
1109 if(FAILED(hres))
1110 return hres;
1111 }
1112
1113 return push_instr_addr(ctx, OP_jmp, iter->while_end_label);
1114 }
1115
1116 static HRESULT compile_exitfor_statement(compile_ctx_t *ctx)
1117 {
1118 statement_ctx_t *iter;
1119 unsigned pop_cnt = 0;
1120
1121 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1122 pop_cnt += iter->stack_use;
1123 if(iter->for_end_label)
1124 break;
1125 }
1126 if(!iter) {
1127 FIXME("Exit For outside For loop\n");
1128 return E_FAIL;
1129 }
1130
1131 if(pop_cnt) {
1132 HRESULT hres;
1133
1134 hres = push_instr_uint(ctx, OP_pop, pop_cnt);
1135 if(FAILED(hres))
1136 return hres;
1137 }
1138
1139 return push_instr_addr(ctx, OP_jmp, iter->for_end_label);
1140 }
1141
1142 static HRESULT exit_label(compile_ctx_t *ctx, unsigned jmp_label)
1143 {
1144 unsigned pop_cnt = stack_offset(ctx);
1145
1146 if(pop_cnt) {
1147 HRESULT hres;
1148
1149 hres = push_instr_uint(ctx, OP_pop, pop_cnt);
1150 if(FAILED(hres))
1151 return hres;
1152 }
1153
1154 return push_instr_addr(ctx, OP_jmp, jmp_label);
1155 }
1156
1157 static HRESULT compile_exitsub_statement(compile_ctx_t *ctx)
1158 {
1159 if(!ctx->sub_end_label) {
1160 FIXME("Exit Sub outside Sub?\n");
1161 return E_FAIL;
1162 }
1163
1164 return exit_label(ctx, ctx->sub_end_label);
1165 }
1166
1167 static HRESULT compile_exitfunc_statement(compile_ctx_t *ctx)
1168 {
1169 if(!ctx->func_end_label) {
1170 FIXME("Exit Function outside Function?\n");
1171 return E_FAIL;
1172 }
1173
1174 return exit_label(ctx, ctx->func_end_label);
1175 }
1176
1177 static HRESULT compile_exitprop_statement(compile_ctx_t *ctx)
1178 {
1179 if(!ctx->prop_end_label) {
1180 FIXME("Exit Property outside Property?\n");
1181 return E_FAIL;
1182 }
1183
1184 return exit_label(ctx, ctx->prop_end_label);
1185 }
1186
1187 static HRESULT compile_onerror_statement(compile_ctx_t *ctx, onerror_statement_t *stat)
1188 {
1189 return push_instr_int(ctx, OP_errmode, stat->resume_next);
1190 }
1191
1192 static HRESULT compile_statement(compile_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1193 {
1194 HRESULT hres;
1195
1196 if(stat_ctx) {
1197 stat_ctx->next = ctx->stat_ctx;
1198 ctx->stat_ctx = stat_ctx;
1199 }
1200
1201 while(stat) {
1202 switch(stat->type) {
1203 case STAT_ASSIGN:
1204 hres = compile_assign_statement(ctx, (assign_statement_t*)stat, FALSE);
1205 break;
1206 case STAT_CALL:
1207 hres = compile_call_statement(ctx, (call_statement_t*)stat);
1208 break;
1209 case STAT_CONST:
1210 hres = compile_const_statement(ctx, (const_statement_t*)stat);
1211 break;
1212 case STAT_DIM:
1213 hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
1214 break;
1215 case STAT_DOWHILE:
1216 case STAT_DOUNTIL:
1217 hres = compile_dowhile_statement(ctx, (while_statement_t*)stat);
1218 break;
1219 case STAT_EXITDO:
1220 hres = compile_exitdo_statement(ctx);
1221 break;
1222 case STAT_EXITFOR:
1223 hres = compile_exitfor_statement(ctx);
1224 break;
1225 case STAT_EXITFUNC:
1226 hres = compile_exitfunc_statement(ctx);
1227 break;
1228 case STAT_EXITPROP:
1229 hres = compile_exitprop_statement(ctx);
1230 break;
1231 case STAT_EXITSUB:
1232 hres = compile_exitsub_statement(ctx);
1233 break;
1234 case STAT_FOREACH:
1235 hres = compile_foreach_statement(ctx, (foreach_statement_t*)stat);
1236 break;
1237 case STAT_FORTO:
1238 hres = compile_forto_statement(ctx, (forto_statement_t*)stat);
1239 break;
1240 case STAT_FUNC:
1241 hres = compile_function_statement(ctx, (function_statement_t*)stat);
1242 break;
1243 case STAT_IF:
1244 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1245 break;
1246 case STAT_ONERROR:
1247 hres = compile_onerror_statement(ctx, (onerror_statement_t*)stat);
1248 break;
1249 case STAT_SELECT:
1250 hres = compile_select_statement(ctx, (select_statement_t*)stat);
1251 break;
1252 case STAT_SET:
1253 hres = compile_assign_statement(ctx, (assign_statement_t*)stat, TRUE);
1254 break;
1255 case STAT_STOP:
1256 hres = push_instr(ctx, OP_stop) ? S_OK : E_OUTOFMEMORY;
1257 break;
1258 case STAT_UNTIL:
1259 case STAT_WHILE:
1260 case STAT_WHILELOOP:
1261 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1262 break;
1263 default:
1264 FIXME("Unimplemented statement type %d\n", stat->type);
1265 hres = E_NOTIMPL;
1266 }
1267
1268 if(FAILED(hres))
1269 return hres;
1270 stat = stat->next;
1271 }
1272
1273 if(stat_ctx) {
1274 assert(ctx->stat_ctx == stat_ctx);
1275 ctx->stat_ctx = stat_ctx->next;
1276 }
1277
1278 return S_OK;
1279 }
1280
1281 static void resolve_labels(compile_ctx_t *ctx, unsigned off)
1282 {
1283 instr_t *instr;
1284
1285 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
1286 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
1287 assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
1288 instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
1289 }
1290 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
1291 }
1292
1293 ctx->labels_cnt = 0;
1294 }
1295
1296 static HRESULT fill_array_desc(compile_ctx_t *ctx, dim_decl_t *dim_decl, array_desc_t *array_desc)
1297 {
1298 unsigned dim_cnt = 0, i;
1299 dim_list_t *iter;
1300
1301 for(iter = dim_decl->dims; iter; iter = iter->next)
1302 dim_cnt++;
1303
1304 array_desc->bounds = compiler_alloc(ctx->code, dim_cnt * sizeof(SAFEARRAYBOUND));
1305 if(!array_desc->bounds)
1306 return E_OUTOFMEMORY;
1307
1308 array_desc->dim_cnt = dim_cnt;
1309
1310 for(iter = dim_decl->dims, i=0; iter; iter = iter->next, i++) {
1311 array_desc->bounds[i].cElements = iter->val+1;
1312 array_desc->bounds[i].lLbound = 0;
1313 }
1314
1315 return S_OK;
1316 }
1317
1318 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
1319 {
1320 HRESULT hres;
1321
1322 func->code_off = ctx->instr_cnt;
1323
1324 ctx->sub_end_label = 0;
1325 ctx->func_end_label = 0;
1326 ctx->prop_end_label = 0;
1327
1328 switch(func->type) {
1329 case FUNC_FUNCTION:
1330 ctx->func_end_label = alloc_label(ctx);
1331 if(!ctx->func_end_label)
1332 return E_OUTOFMEMORY;
1333 break;
1334 case FUNC_SUB:
1335 ctx->sub_end_label = alloc_label(ctx);
1336 if(!ctx->sub_end_label)
1337 return E_OUTOFMEMORY;
1338 break;
1339 case FUNC_PROPGET:
1340 case FUNC_PROPLET:
1341 case FUNC_PROPSET:
1342 case FUNC_DEFGET:
1343 ctx->prop_end_label = alloc_label(ctx);
1344 if(!ctx->prop_end_label)
1345 return E_OUTOFMEMORY;
1346 break;
1347 case FUNC_GLOBAL:
1348 break;
1349 }
1350
1351 ctx->func = func;
1352 ctx->dim_decls = ctx->dim_decls_tail = NULL;
1353 ctx->const_decls = NULL;
1354 hres = compile_statement(ctx, NULL, stat);
1355 ctx->func = NULL;
1356 if(FAILED(hres))
1357 return hres;
1358
1359 if(ctx->sub_end_label)
1360 label_set_addr(ctx, ctx->sub_end_label);
1361 if(ctx->func_end_label)
1362 label_set_addr(ctx, ctx->func_end_label);
1363 if(ctx->prop_end_label)
1364 label_set_addr(ctx, ctx->prop_end_label);
1365
1366 if(!push_instr(ctx, OP_ret))
1367 return E_OUTOFMEMORY;
1368
1369 resolve_labels(ctx, func->code_off);
1370
1371 if(func->var_cnt) {
1372 dim_decl_t *dim_decl;
1373
1374 if(func->type == FUNC_GLOBAL) {
1375 dynamic_var_t *new_var;
1376
1377 func->var_cnt = 0;
1378
1379 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
1380 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
1381 if(!new_var)
1382 return E_OUTOFMEMORY;
1383
1384 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
1385 if(!new_var->name)
1386 return E_OUTOFMEMORY;
1387
1388 V_VT(&new_var->v) = VT_EMPTY;
1389 new_var->is_const = FALSE;
1390
1391 new_var->next = ctx->global_vars;
1392 ctx->global_vars = new_var;
1393 }
1394 }else {
1395 unsigned i;
1396
1397 func->vars = compiler_alloc(ctx->code, func->var_cnt * sizeof(var_desc_t));
1398 if(!func->vars)
1399 return E_OUTOFMEMORY;
1400
1401 for(dim_decl = ctx->dim_decls, i=0; dim_decl; dim_decl = dim_decl->next, i++) {
1402 func->vars[i].name = compiler_alloc_string(ctx->code, dim_decl->name);
1403 if(!func->vars[i].name)
1404 return E_OUTOFMEMORY;
1405 }
1406
1407 assert(i == func->var_cnt);
1408 }
1409 }
1410
1411 if(func->array_cnt) {
1412 unsigned array_id = 0;
1413 dim_decl_t *dim_decl;
1414
1415 func->array_descs = compiler_alloc(ctx->code, func->array_cnt * sizeof(array_desc_t));
1416 if(!func->array_descs)
1417 return E_OUTOFMEMORY;
1418
1419 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
1420 if(dim_decl->is_array) {
1421 hres = fill_array_desc(ctx, dim_decl, func->array_descs + array_id++);
1422 if(FAILED(hres))
1423 return hres;
1424 }
1425 }
1426
1427 assert(array_id == func->array_cnt);
1428 }
1429
1430 return S_OK;
1431 }
1432
1433 static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
1434 {
1435 function_t *iter;
1436
1437 for(iter = ctx->funcs; iter; iter = iter->next) {
1438 if(!strcmpiW(iter->name, name))
1439 return TRUE;
1440 }
1441
1442 return FALSE;
1443 }
1444
1445 static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
1446 {
1447 function_t *func;
1448 HRESULT hres;
1449
1450 if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name) || lookup_const_decls(ctx, decl->name, FALSE)) {
1451 FIXME("%s: redefinition\n", debugstr_w(decl->name));
1452 return E_FAIL;
1453 }
1454
1455 func = compiler_alloc(ctx->code, sizeof(*func));
1456 if(!func)
1457 return E_OUTOFMEMORY;
1458
1459 func->name = compiler_alloc_string(ctx->code, decl->name);
1460 if(!func->name)
1461 return E_OUTOFMEMORY;
1462
1463 func->vars = NULL;
1464 func->var_cnt = 0;
1465 func->array_cnt = 0;
1466 func->code_ctx = ctx->code;
1467 func->type = decl->type;
1468 func->is_public = decl->is_public;
1469
1470 func->arg_cnt = 0;
1471 if(decl->args) {
1472 arg_decl_t *arg;
1473 unsigned i;
1474
1475 for(arg = decl->args; arg; arg = arg->next)
1476 func->arg_cnt++;
1477
1478 func->args = compiler_alloc(ctx->code, func->arg_cnt * sizeof(arg_desc_t));
1479 if(!func->args)
1480 return E_OUTOFMEMORY;
1481
1482 for(i = 0, arg = decl->args; arg; arg = arg->next, i++) {
1483 func->args[i].name = compiler_alloc_string(ctx->code, arg->name);
1484 if(!func->args[i].name)
1485 return E_OUTOFMEMORY;
1486 func->args[i].by_ref = arg->by_ref;
1487 }
1488 }else {
1489 func->args = NULL;
1490 }
1491
1492 hres = compile_func(ctx, decl->body, func);
1493 if(FAILED(hres))
1494 return hres;
1495
1496 *ret = func;
1497 return S_OK;
1498 }
1499
1500 static BOOL lookup_class_name(compile_ctx_t *ctx, const WCHAR *name)
1501 {
1502 class_desc_t *iter;
1503
1504 for(iter = ctx->classes; iter; iter = iter->next) {
1505 if(!strcmpiW(iter->name, name))
1506 return TRUE;
1507 }
1508
1509 return FALSE;
1510 }
1511
1512 static HRESULT create_class_funcprop(compile_ctx_t *ctx, function_decl_t *func_decl, vbdisp_funcprop_desc_t *desc)
1513 {
1514 vbdisp_invoke_type_t invoke_type;
1515 function_decl_t *funcprop_decl;
1516 HRESULT hres;
1517
1518 desc->name = compiler_alloc_string(ctx->code, func_decl->name);
1519 if(!desc->name)
1520 return E_OUTOFMEMORY;
1521
1522 for(funcprop_decl = func_decl; funcprop_decl; funcprop_decl = funcprop_decl->next_prop_func) {
1523 switch(funcprop_decl->type) {
1524 case FUNC_FUNCTION:
1525 case FUNC_SUB:
1526 case FUNC_PROPGET:
1527 case FUNC_DEFGET:
1528 invoke_type = VBDISP_CALLGET;
1529 break;
1530 case FUNC_PROPLET:
1531 invoke_type = VBDISP_LET;
1532 break;
1533 case FUNC_PROPSET:
1534 invoke_type = VBDISP_SET;
1535 break;
1536 DEFAULT_UNREACHABLE;
1537 }
1538
1539 assert(!desc->entries[invoke_type]);
1540
1541 if(funcprop_decl->is_public)
1542 desc->is_public = TRUE;
1543
1544 hres = create_function(ctx, funcprop_decl, desc->entries+invoke_type);
1545 if(FAILED(hres))
1546 return hres;
1547 }
1548
1549 return S_OK;
1550 }
1551
1552 static BOOL lookup_class_funcs(class_desc_t *class_desc, const WCHAR *name)
1553 {
1554 unsigned i;
1555
1556 for(i=0; i < class_desc->func_cnt; i++) {
1557 if(class_desc->funcs[i].name && !strcmpiW(class_desc->funcs[i].name, name))
1558 return TRUE;
1559 }
1560
1561 return FALSE;
1562 }
1563
1564 static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl)
1565 {
1566 function_decl_t *func_decl, *func_prop_decl;
1567 class_desc_t *class_desc;
1568 dim_decl_t *prop_decl;
1569 unsigned i;
1570 HRESULT hres;
1571
1572 static const WCHAR class_initializeW[] = {'c','l','a','s','s','_','i','n','i','t','i','a','l','i','z','e',0};
1573 static const WCHAR class_terminateW[] = {'c','l','a','s','s','_','t','e','r','m','i','n','a','t','e',0};
1574
1575 if(lookup_dim_decls(ctx, class_decl->name) || lookup_funcs_name(ctx, class_decl->name)
1576 || lookup_const_decls(ctx, class_decl->name, FALSE) || lookup_class_name(ctx, class_decl->name)) {
1577 FIXME("%s: redefinition\n", debugstr_w(class_decl->name));
1578 return E_FAIL;
1579 }
1580
1581 class_desc = compiler_alloc_zero(ctx->code, sizeof(*class_desc));
1582 if(!class_desc)
1583 return E_OUTOFMEMORY;
1584
1585 class_desc->name = compiler_alloc_string(ctx->code, class_decl->name);
1586 if(!class_desc->name)
1587 return E_OUTOFMEMORY;
1588
1589 class_desc->func_cnt = 1; /* always allocate slot for default getter */
1590
1591 for(func_decl = class_decl->funcs; func_decl; func_decl = func_decl->next) {
1592 for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1593 if(func_prop_decl->type == FUNC_DEFGET)
1594 break;
1595 }
1596 if(!func_prop_decl)
1597 class_desc->func_cnt++;
1598 }
1599
1600 class_desc->funcs = compiler_alloc(ctx->code, class_desc->func_cnt*sizeof(*class_desc->funcs));
1601 if(!class_desc->funcs)
1602 return E_OUTOFMEMORY;
1603 memset(class_desc->funcs, 0, class_desc->func_cnt*sizeof(*class_desc->funcs));
1604
1605 for(func_decl = class_decl->funcs, i=1; func_decl; func_decl = func_decl->next, i++) {
1606 for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1607 if(func_prop_decl->type == FUNC_DEFGET) {
1608 i--;
1609 break;
1610 }
1611 }
1612
1613 if(!strcmpiW(class_initializeW, func_decl->name)) {
1614 if(func_decl->type != FUNC_SUB) {
1615 FIXME("class initializer is not sub\n");
1616 return E_FAIL;
1617 }
1618
1619 class_desc->class_initialize_id = i;
1620 }else if(!strcmpiW(class_terminateW, func_decl->name)) {
1621 if(func_decl->type != FUNC_SUB) {
1622 FIXME("class terminator is not sub\n");
1623 return E_FAIL;
1624 }
1625
1626 class_desc->class_terminate_id = i;
1627 }
1628
1629 hres = create_class_funcprop(ctx, func_decl, class_desc->funcs + (func_prop_decl ? 0 : i));
1630 if(FAILED(hres))
1631 return hres;
1632 }
1633
1634 for(prop_decl = class_decl->props; prop_decl; prop_decl = prop_decl->next)
1635 class_desc->prop_cnt++;
1636
1637 class_desc->props = compiler_alloc(ctx->code, class_desc->prop_cnt*sizeof(*class_desc->props));
1638 if(!class_desc->props)
1639 return E_OUTOFMEMORY;
1640
1641 for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next, i++) {
1642 if(lookup_class_funcs(class_desc, prop_decl->name)) {
1643 FIXME("Property %s redefined\n", debugstr_w(prop_decl->name));
1644 return E_FAIL;
1645 }
1646
1647 class_desc->props[i].name = compiler_alloc_string(ctx->code, prop_decl->name);
1648 if(!class_desc->props[i].name)
1649 return E_OUTOFMEMORY;
1650
1651 class_desc->props[i].is_public = prop_decl->is_public;
1652 class_desc->props[i].is_array = prop_decl->is_array;
1653
1654 if(prop_decl->is_array)
1655 class_desc->array_cnt++;
1656 }
1657
1658 if(class_desc->array_cnt) {
1659 class_desc->array_descs = compiler_alloc(ctx->code, class_desc->array_cnt*sizeof(*class_desc->array_descs));
1660 if(!class_desc->array_descs)
1661 return E_OUTOFMEMORY;
1662
1663 for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next) {
1664 if(prop_decl->is_array) {
1665 hres = fill_array_desc(ctx, prop_decl, class_desc->array_descs + i++);
1666 if(FAILED(hres))
1667 return hres;
1668 }
1669 }
1670 }
1671
1672 class_desc->next = ctx->classes;
1673 ctx->classes = class_desc;
1674 return S_OK;
1675 }
1676
1677 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
1678 {
1679 class_desc_t *class;
1680 dynamic_var_t *var;
1681 function_t *func;
1682
1683 for(var = script->global_vars; var; var = var->next) {
1684 if(!strcmpiW(var->name, identifier))
1685 return TRUE;
1686 }
1687
1688 for(func = script->global_funcs; func; func = func->next) {
1689 if(!strcmpiW(func->name, identifier))
1690 return TRUE;
1691 }
1692
1693 for(class = script->classes; class; class = class->next) {
1694 if(!strcmpiW(class->name, identifier))
1695 return TRUE;
1696 }
1697
1698 return FALSE;
1699 }
1700
1701 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
1702 {
1703 class_desc_t *class;
1704 dynamic_var_t *var;
1705 function_t *func;
1706
1707 for(var = ctx->global_vars; var; var = var->next) {
1708 if(lookup_script_identifier(script, var->name)) {
1709 FIXME("%s: redefined\n", debugstr_w(var->name));
1710 return E_FAIL;
1711 }
1712 }
1713
1714 for(func = ctx->funcs; func; func = func->next) {
1715 if(lookup_script_identifier(script, func->name)) {
1716 FIXME("%s: redefined\n", debugstr_w(func->name));
1717 return E_FAIL;
1718 }
1719 }
1720
1721 for(class = ctx->classes; class; class = class->next) {
1722 if(lookup_script_identifier(script, class->name)) {
1723 FIXME("%s: redefined\n", debugstr_w(class->name));
1724 return E_FAIL;
1725 }
1726 }
1727
1728 return S_OK;
1729 }
1730
1731 void release_vbscode(vbscode_t *code)
1732 {
1733 unsigned i;
1734
1735 list_remove(&code->entry);
1736
1737 for(i=0; i < code->bstr_cnt; i++)
1738 SysFreeString(code->bstr_pool[i]);
1739
1740 heap_pool_free(&code->heap);
1741
1742 heap_free(code->bstr_pool);
1743 heap_free(code->source);
1744 heap_free(code->instrs);
1745 heap_free(code);
1746 }
1747
1748 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
1749 {
1750 vbscode_t *ret;
1751
1752 ret = heap_alloc(sizeof(*ret));
1753 if(!ret)
1754 return NULL;
1755
1756 ret->source = heap_strdupW(source);
1757 if(!ret->source) {
1758 heap_free(ret);
1759 return NULL;
1760 }
1761
1762 ret->instrs = heap_alloc(32*sizeof(instr_t));
1763 if(!ret->instrs) {
1764 release_vbscode(ret);
1765 return NULL;
1766 }
1767
1768 ctx->instr_cnt = 1;
1769 ctx->instr_size = 32;
1770 heap_pool_init(&ret->heap);
1771
1772 ret->option_explicit = ctx->parser.option_explicit;
1773
1774 ret->bstr_pool = NULL;
1775 ret->bstr_pool_size = 0;
1776 ret->bstr_cnt = 0;
1777 ret->pending_exec = FALSE;
1778
1779 ret->main_code.type = FUNC_GLOBAL;
1780 ret->main_code.name = NULL;
1781 ret->main_code.code_ctx = ret;
1782 ret->main_code.vars = NULL;
1783 ret->main_code.var_cnt = 0;
1784 ret->main_code.array_cnt = 0;
1785 ret->main_code.arg_cnt = 0;
1786 ret->main_code.args = NULL;
1787
1788 list_init(&ret->entry);
1789 return ret;
1790 }
1791
1792 static void release_compiler(compile_ctx_t *ctx)
1793 {
1794 parser_release(&ctx->parser);
1795 heap_free(ctx->labels);
1796 if(ctx->code)
1797 release_vbscode(ctx->code);
1798 }
1799
1800 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, const WCHAR *delimiter, vbscode_t **ret)
1801 {
1802 function_t *new_func;
1803 function_decl_t *func_decl;
1804 class_decl_t *class_decl;
1805 compile_ctx_t ctx;
1806 vbscode_t *code;
1807 HRESULT hres;
1808
1809 hres = parse_script(&ctx.parser, src, delimiter);
1810 if(FAILED(hres))
1811 return hres;
1812
1813 code = ctx.code = alloc_vbscode(&ctx, src);
1814 if(!ctx.code)
1815 return E_OUTOFMEMORY;
1816
1817 ctx.funcs = NULL;
1818 ctx.func_decls = NULL;
1819 ctx.global_vars = NULL;
1820 ctx.classes = NULL;
1821 ctx.labels = NULL;
1822 ctx.global_consts = NULL;
1823 ctx.stat_ctx = NULL;
1824 ctx.labels_cnt = ctx.labels_size = 0;
1825
1826 hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->main_code);
1827 if(FAILED(hres)) {
1828 release_compiler(&ctx);
1829 return hres;
1830 }
1831
1832 ctx.global_consts = ctx.const_decls;
1833
1834 for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
1835 hres = create_function(&ctx, func_decl, &new_func);
1836 if(FAILED(hres)) {
1837 release_compiler(&ctx);
1838 return hres;
1839 }
1840
1841 new_func->next = ctx.funcs;
1842 ctx.funcs = new_func;
1843 }
1844
1845 for(class_decl = ctx.parser.class_decls; class_decl; class_decl = class_decl->next) {
1846 hres = compile_class(&ctx, class_decl);
1847 if(FAILED(hres)) {
1848 release_compiler(&ctx);
1849 return hres;
1850 }
1851 }
1852
1853 hres = check_script_collisions(&ctx, script);
1854 if(FAILED(hres)) {
1855 release_compiler(&ctx);
1856 return hres;
1857 }
1858
1859 if(ctx.global_vars) {
1860 dynamic_var_t *var;
1861
1862 for(var = ctx.global_vars; var->next; var = var->next);
1863
1864 var->next = script->global_vars;
1865 script->global_vars = ctx.global_vars;
1866 }
1867
1868 if(ctx.funcs) {
1869 for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);
1870
1871 new_func->next = script->global_funcs;
1872 script->global_funcs = ctx.funcs;
1873 }
1874
1875 if(ctx.classes) {
1876 class_desc_t *class = ctx.classes;
1877
1878 while(1) {
1879 class->ctx = script;
1880 if(!class->next)
1881 break;
1882 class = class->next;
1883 }
1884
1885 class->next = script->classes;
1886 script->classes = ctx.classes;
1887 }
1888
1889 if(TRACE_ON(vbscript_disas))
1890 dump_code(&ctx);
1891
1892 ctx.code = NULL;
1893 release_compiler(&ctx);
1894
1895 list_add_tail(&script->code_list, &code->entry);
1896 *ret = code;
1897 return S_OK;
1898 }