vbscript: Moved creating new dynamic variable to separated function.
[wine] / dlls / 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 <assert.h>
20
21 #include "vbscript.h"
22 #include "parse.h"
23 #include "parser.tab.h"
24
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
28 WINE_DECLARE_DEBUG_CHANNEL(vbscript_disas);
29
30 typedef struct {
31     parser_ctx_t parser;
32
33     unsigned instr_cnt;
34     unsigned instr_size;
35     vbscode_t *code;
36
37     unsigned *labels;
38     unsigned labels_size;
39     unsigned labels_cnt;
40
41     unsigned while_end_label;
42     unsigned sub_end_label;
43     unsigned func_end_label;
44     unsigned prop_end_label;
45
46     dim_decl_t *dim_decls;
47     dynamic_var_t *global_vars;
48
49     const_decl_t *const_decls;
50     const_decl_t *global_consts;
51
52     function_t *func;
53     function_t *funcs;
54     function_decl_t *func_decls;
55
56     class_desc_t *classes;
57 } compile_ctx_t;
58
59 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
60 static HRESULT compile_statement(compile_ctx_t*,statement_t*);
61
62 static const struct {
63     const char *op_str;
64     instr_arg_type_t arg1_type;
65     instr_arg_type_t arg2_type;
66 } instr_info[] = {
67 #define X(n,a,b,c) {#n,b,c},
68 OP_LIST
69 #undef X
70 };
71
72 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
73 {
74     switch(type) {
75     case ARG_STR:
76     case ARG_BSTR:
77         TRACE_(vbscript_disas)("\t%s", debugstr_w(arg->str));
78         break;
79     case ARG_INT:
80         TRACE_(vbscript_disas)("\t%d", arg->uint);
81         break;
82     case ARG_UINT:
83     case ARG_ADDR:
84         TRACE_(vbscript_disas)("\t%u", arg->uint);
85         break;
86     case ARG_DOUBLE:
87         TRACE_(vbscript_disas)("\t%lf", *arg->dbl);
88         break;
89     case ARG_NONE:
90         break;
91     default:
92         assert(0);
93     }
94 }
95
96 static void dump_code(compile_ctx_t *ctx)
97 {
98     instr_t *instr;
99
100     for(instr = ctx->code->instrs; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
101         TRACE_(vbscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
102         dump_instr_arg(instr_info[instr->op].arg1_type, &instr->arg1);
103         dump_instr_arg(instr_info[instr->op].arg2_type, &instr->arg2);
104         TRACE_(vbscript_disas)("\n");
105     }
106 }
107
108 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
109 {
110     return vbsheap_alloc(&vbscode->heap, size);
111 }
112
113 static inline void *compiler_alloc_zero(vbscode_t *vbscode, size_t size)
114 {
115     void *ret;
116
117     ret = vbsheap_alloc(&vbscode->heap, size);
118     if(ret)
119         memset(ret, 0, size);
120     return ret;
121 }
122
123 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
124 {
125     size_t size;
126     WCHAR *ret;
127
128     size = (strlenW(str)+1)*sizeof(WCHAR);
129     ret = compiler_alloc(vbscode, size);
130     if(ret)
131         memcpy(ret, str, size);
132     return ret;
133 }
134
135 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
136 {
137     assert(id < ctx->instr_cnt);
138     return ctx->code->instrs + id;
139 }
140
141 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
142 {
143     assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
144
145     if(ctx->instr_size == ctx->instr_cnt) {
146         instr_t *new_instr;
147
148         new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
149         if(!new_instr)
150             return -1;
151
152         ctx->code->instrs = new_instr;
153         ctx->instr_size *= 2;
154     }
155
156     ctx->code->instrs[ctx->instr_cnt].op = op;
157     return ctx->instr_cnt++;
158 }
159
160 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
161 {
162     unsigned ret;
163
164     ret = push_instr(ctx, op);
165     if(ret == -1)
166         return E_OUTOFMEMORY;
167
168     instr_ptr(ctx, ret)->arg1.lng = arg;
169     return S_OK;
170 }
171
172 static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
173 {
174     unsigned ret;
175
176     ret = push_instr(ctx, op);
177     if(ret == -1)
178         return E_OUTOFMEMORY;
179
180     instr_ptr(ctx, ret)->arg1.uint = arg;
181     return S_OK;
182 }
183
184 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
185 {
186     unsigned instr;
187     WCHAR *str;
188
189     str = compiler_alloc_string(ctx->code, arg);
190     if(!str)
191         return E_OUTOFMEMORY;
192
193     instr = push_instr(ctx, op);
194     if(instr == -1)
195         return E_OUTOFMEMORY;
196
197     instr_ptr(ctx, instr)->arg1.str = str;
198     return S_OK;
199 }
200
201 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
202 {
203     unsigned instr;
204     double *d;
205
206     d = compiler_alloc(ctx->code, sizeof(double));
207     if(!d)
208         return E_OUTOFMEMORY;
209
210     instr = push_instr(ctx, op);
211     if(instr == -1)
212         return E_OUTOFMEMORY;
213
214     *d = arg;
215     instr_ptr(ctx, instr)->arg1.dbl = d;
216     return S_OK;
217 }
218
219 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
220 {
221     if(!ctx->code->bstr_pool_size) {
222         ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
223         if(!ctx->code->bstr_pool)
224             return NULL;
225         ctx->code->bstr_pool_size = 8;
226     }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
227         BSTR *new_pool;
228
229         new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
230         if(!new_pool)
231             return NULL;
232
233         ctx->code->bstr_pool = new_pool;
234         ctx->code->bstr_pool_size *= 2;
235     }
236
237     ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
238     if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
239         return NULL;
240
241     return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
242 }
243
244 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
245 {
246     unsigned instr;
247     BSTR bstr;
248
249     bstr = alloc_bstr_arg(ctx, arg);
250     if(!bstr)
251         return E_OUTOFMEMORY;
252
253     instr = push_instr(ctx, op);
254     if(instr == -1)
255         return E_OUTOFMEMORY;
256
257     instr_ptr(ctx, instr)->arg1.bstr = bstr;
258     return S_OK;
259 }
260
261 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
262 {
263     unsigned instr;
264     BSTR bstr;
265
266     bstr = alloc_bstr_arg(ctx, arg1);
267     if(!bstr)
268         return E_OUTOFMEMORY;
269
270     instr = push_instr(ctx, op);
271     if(instr == -1)
272         return E_OUTOFMEMORY;
273
274     instr_ptr(ctx, instr)->arg1.bstr = bstr;
275     instr_ptr(ctx, instr)->arg2.uint = arg2;
276     return S_OK;
277 }
278
279 #define LABEL_FLAG 0x80000000
280
281 static unsigned alloc_label(compile_ctx_t *ctx)
282 {
283     if(!ctx->labels_size) {
284         ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
285         if(!ctx->labels)
286             return -1;
287         ctx->labels_size = 8;
288     }else if(ctx->labels_size == ctx->labels_cnt) {
289         unsigned *new_labels;
290
291         new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
292         if(!new_labels)
293             return -1;
294
295         ctx->labels = new_labels;
296         ctx->labels_size *= 2;
297     }
298
299     return ctx->labels_cnt++ | LABEL_FLAG;
300 }
301
302 static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
303 {
304     assert(label & LABEL_FLAG);
305     ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
306 }
307
308 static expression_t *lookup_const_decls(compile_ctx_t *ctx, const WCHAR *name, BOOL lookup_global)
309 {
310     const_decl_t *decl;
311
312     for(decl = ctx->const_decls; decl; decl = decl->next) {
313         if(!strcmpiW(decl->name, name))
314             return decl->value_expr;
315     }
316
317     if(!lookup_global)
318         return NULL;
319
320     for(decl = ctx->global_consts; decl; decl = decl->next) {
321         if(!strcmpiW(decl->name, name))
322             return decl->value_expr;
323     }
324
325     return NULL;
326 }
327
328 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
329 {
330     unsigned arg_cnt = 0;
331     HRESULT hres;
332
333     while(args) {
334         hres = compile_expression(ctx, args);
335         if(FAILED(hres))
336             return hres;
337
338         arg_cnt++;
339         args = args->next;
340     }
341
342     *ret = arg_cnt;
343     return S_OK;
344 }
345
346 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
347 {
348     unsigned arg_cnt = 0;
349     HRESULT hres;
350
351     if(ret_val && !expr->args) {
352         expression_t *const_expr;
353
354         const_expr = lookup_const_decls(ctx, expr->identifier, TRUE);
355         if(const_expr)
356             return compile_expression(ctx, const_expr);
357     }
358
359     hres = compile_args(ctx, expr->args, &arg_cnt);
360     if(FAILED(hres))
361         return hres;
362
363     if(expr->obj_expr) {
364         hres = compile_expression(ctx, expr->obj_expr);
365         if(FAILED(hres))
366             return hres;
367
368         hres = push_instr_bstr_uint(ctx, ret_val ? OP_mcall : OP_mcallv, expr->identifier, arg_cnt);
369     }else {
370         hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
371     }
372
373     return hres;
374 }
375
376 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
377 {
378     HRESULT hres;
379
380     hres = compile_expression(ctx, expr->subexpr);
381     if(FAILED(hres))
382         return hres;
383
384     return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
385 }
386
387 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
388 {
389     HRESULT hres;
390
391     hres = compile_expression(ctx, expr->left);
392     if(FAILED(hres))
393         return hres;
394
395     hres = compile_expression(ctx, expr->right);
396     if(FAILED(hres))
397         return hres;
398
399     return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
400 }
401
402 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
403 {
404     switch(expr->type) {
405     case EXPR_ADD:
406         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
407     case EXPR_AND:
408         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
409     case EXPR_BOOL:
410         return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
411     case EXPR_CONCAT:
412         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
413     case EXPR_DIV:
414         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
415     case EXPR_DOUBLE:
416         return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
417     case EXPR_EMPTY:
418         return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
419     case EXPR_EQUAL:
420         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
421     case EXPR_EQV:
422         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eqv);
423     case EXPR_EXP:
424         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
425     case EXPR_GT:
426         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
427     case EXPR_GTEQ:
428         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
429     case EXPR_IDIV:
430         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
431     case EXPR_IS:
432         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_is);
433     case EXPR_IMP:
434         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_imp);
435     case EXPR_LT:
436         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
437     case EXPR_LTEQ:
438         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
439     case EXPR_ME:
440         return push_instr(ctx, OP_me) != -1 ? S_OK : E_OUTOFMEMORY;
441     case EXPR_MEMBER:
442         return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
443     case EXPR_MOD:
444         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
445     case EXPR_MUL:
446         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
447     case EXPR_NEG:
448         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
449     case EXPR_NEQUAL:
450         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
451     case EXPR_NEW:
452         return push_instr_str(ctx, OP_new, ((string_expression_t*)expr)->value);
453     case EXPR_NOT:
454         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
455     case EXPR_NOTHING:
456         return push_instr(ctx, OP_nothing) != -1 ? S_OK : E_OUTOFMEMORY;
457     case EXPR_NULL:
458         return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
459     case EXPR_OR:
460         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
461     case EXPR_STRING:
462         return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
463     case EXPR_SUB:
464         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
465     case EXPR_USHORT:
466         return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
467     case EXPR_ULONG:
468         return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
469     case EXPR_XOR:
470         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
471     default:
472         FIXME("Unimplemented expression type %d\n", expr->type);
473         return E_NOTIMPL;
474     }
475
476     return S_OK;
477 }
478
479 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
480 {
481     unsigned cnd_jmp, endif_label = -1;
482     elseif_decl_t *elseif_decl;
483     HRESULT hres;
484
485     hres = compile_expression(ctx, stat->expr);
486     if(FAILED(hres))
487         return hres;
488
489     cnd_jmp = push_instr(ctx, OP_jmp_false);
490     if(cnd_jmp == -1)
491         return E_OUTOFMEMORY;
492
493     hres = compile_statement(ctx, stat->if_stat);
494     if(FAILED(hres))
495         return hres;
496
497     if(stat->else_stat || stat->elseifs) {
498         endif_label = alloc_label(ctx);
499         if(endif_label == -1)
500             return E_OUTOFMEMORY;
501
502         hres = push_instr_addr(ctx, OP_jmp, endif_label);
503         if(FAILED(hres))
504             return hres;
505     }
506
507     for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
508         instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
509
510         hres = compile_expression(ctx, elseif_decl->expr);
511         if(FAILED(hres))
512             return hres;
513
514         cnd_jmp = push_instr(ctx, OP_jmp_false);
515         if(cnd_jmp == -1)
516             return E_OUTOFMEMORY;
517
518         hres = compile_statement(ctx, elseif_decl->stat);
519         if(FAILED(hres))
520             return hres;
521
522         hres = push_instr_addr(ctx, OP_jmp, endif_label);
523         if(FAILED(hres))
524             return hres;
525     }
526
527     instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
528
529     if(stat->else_stat) {
530         hres = compile_statement(ctx, stat->else_stat);
531         if(FAILED(hres))
532             return hres;
533     }
534
535     if(endif_label != -1)
536         label_set_addr(ctx, endif_label);
537     return S_OK;
538 }
539
540 static HRESULT compile_while_statement(compile_ctx_t *ctx, while_statement_t *stat)
541 {
542     unsigned start_addr, prev_label;
543     unsigned jmp_end;
544     HRESULT hres;
545
546     start_addr = ctx->instr_cnt;
547
548     hres = compile_expression(ctx, stat->expr);
549     if(FAILED(hres))
550         return hres;
551
552     jmp_end = push_instr(ctx, stat->stat.type == STAT_UNTIL ? OP_jmp_true : OP_jmp_false);
553     if(jmp_end == -1)
554         return E_OUTOFMEMORY;
555
556     prev_label = ctx->while_end_label;
557     if(stat->stat.type != STAT_WHILE && (ctx->while_end_label = alloc_label(ctx)) == -1)
558         return E_OUTOFMEMORY;
559
560     hres = compile_statement(ctx, stat->body);
561     if(FAILED(hres))
562         return hres;
563
564     hres = push_instr_addr(ctx, OP_jmp, start_addr);
565     if(FAILED(hres))
566         return hres;
567
568     instr_ptr(ctx, jmp_end)->arg1.uint = ctx->instr_cnt;
569
570     if(stat->stat.type != STAT_WHILE) {
571         label_set_addr(ctx, ctx->while_end_label);
572         ctx->while_end_label = prev_label;
573     }
574
575     return S_OK;
576 }
577
578 static HRESULT compile_dowhile_statement(compile_ctx_t *ctx, while_statement_t *stat)
579 {
580     unsigned start_addr, prev_label;
581     HRESULT hres;
582
583     start_addr = ctx->instr_cnt;
584
585     prev_label = ctx->while_end_label;
586     if((ctx->while_end_label = alloc_label(ctx)) == -1)
587         return E_OUTOFMEMORY;
588
589     hres = compile_statement(ctx, stat->body);
590     if(FAILED(hres))
591         return hres;
592
593     hres = compile_expression(ctx, stat->expr);
594     if(FAILED(hres))
595         return hres;
596
597     hres = push_instr_addr(ctx, stat->stat.type == STAT_DOUNTIL ? OP_jmp_false : OP_jmp_true, start_addr);
598     if(FAILED(hres))
599         return hres;
600
601     label_set_addr(ctx, ctx->while_end_label);
602     ctx->while_end_label = prev_label;
603     return S_OK;
604 }
605
606 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat, BOOL is_set)
607 {
608     HRESULT hres;
609
610     hres = compile_expression(ctx, stat->value_expr);
611     if(FAILED(hres))
612         return hres;
613
614     if(stat->member_expr->args) {
615         FIXME("arguments support not implemented\n");
616         return E_NOTIMPL;
617     }
618
619     if(stat->member_expr->obj_expr) {
620         hres = compile_expression(ctx, stat->member_expr->obj_expr);
621         if(FAILED(hres))
622             return hres;
623
624         hres = push_instr_bstr(ctx, is_set ? OP_set_member : OP_assign_member, stat->member_expr->identifier);
625     }else {
626         hres = push_instr_bstr(ctx, is_set ? OP_set_ident : OP_assign_ident, stat->member_expr->identifier);
627     }
628
629     return hres;
630 }
631
632 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
633 {
634     dim_decl_t *dim_decl;
635
636     for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
637         if(!strcmpiW(dim_decl->name, name))
638             return TRUE;
639     }
640
641     return FALSE;
642 }
643
644 static BOOL lookup_args_name(compile_ctx_t *ctx, const WCHAR *name)
645 {
646     unsigned i;
647
648     for(i = 0; i < ctx->func->arg_cnt; i++) {
649         if(!strcmpiW(ctx->func->args[i].name, name))
650             return TRUE;
651     }
652
653     return FALSE;
654 }
655
656 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
657 {
658     dim_decl_t *dim_decl = stat->dim_decls;
659
660     while(1) {
661         if(lookup_dim_decls(ctx, dim_decl->name) || lookup_args_name(ctx, dim_decl->name)
662            || lookup_const_decls(ctx, dim_decl->name, FALSE)) {
663             FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
664             return E_FAIL;
665         }
666
667         if(!dim_decl->next)
668             break;
669         dim_decl = dim_decl->next;
670     }
671
672     dim_decl->next = ctx->dim_decls;
673     ctx->dim_decls = stat->dim_decls;
674     ctx->func->var_cnt++;
675     return S_OK;
676 }
677
678 static HRESULT compile_const_statement(compile_ctx_t *ctx, const_statement_t *stat)
679 {
680     const_decl_t *decl, *next_decl = stat->decls;
681
682     do {
683         decl = next_decl;
684
685         if(lookup_const_decls(ctx, decl->name, FALSE) || lookup_args_name(ctx, decl->name)
686                 || lookup_dim_decls(ctx, decl->name)) {
687             FIXME("%s redefined\n", debugstr_w(decl->name));
688             return E_FAIL;
689         }
690
691         if(ctx->func->type == FUNC_GLOBAL) {
692             HRESULT hres;
693
694             hres = compile_expression(ctx, decl->value_expr);
695             if(FAILED(hres))
696                 return hres;
697
698             hres = push_instr_bstr(ctx, OP_const, decl->name);
699             if(FAILED(hres))
700                 return hres;
701         }
702
703         next_decl = decl->next;
704         decl->next = ctx->const_decls;
705         ctx->const_decls = decl;
706     } while(next_decl);
707
708     return S_OK;
709 }
710
711 static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
712 {
713     if(ctx->func != &ctx->code->global_code) {
714         FIXME("Function is not in the global code\n");
715         return E_FAIL;
716     }
717
718     stat->func_decl->next = ctx->func_decls;
719     ctx->func_decls = stat->func_decl;
720     return S_OK;
721 }
722
723 static HRESULT compile_exitdo_statement(compile_ctx_t *ctx)
724 {
725     if(ctx->while_end_label == -1) {
726         FIXME("Exit Do outside Do Loop\n");
727         return E_FAIL;
728     }
729
730     return push_instr_addr(ctx, OP_jmp, ctx->while_end_label);
731 }
732
733 static HRESULT compile_exitsub_statement(compile_ctx_t *ctx)
734 {
735     if(ctx->sub_end_label == -1) {
736         FIXME("Exit Sub outside Sub?\n");
737         return E_FAIL;
738     }
739
740     return push_instr_addr(ctx, OP_jmp, ctx->sub_end_label);
741 }
742
743 static HRESULT compile_exitfunc_statement(compile_ctx_t *ctx)
744 {
745     if(ctx->func_end_label == -1) {
746         FIXME("Exit Function outside Function?\n");
747         return E_FAIL;
748     }
749
750     return push_instr_addr(ctx, OP_jmp, ctx->func_end_label);
751 }
752
753 static HRESULT compile_exitprop_statement(compile_ctx_t *ctx)
754 {
755     if(ctx->prop_end_label == -1) {
756         FIXME("Exit Property outside Property?\n");
757         return E_FAIL;
758     }
759
760     return push_instr_addr(ctx, OP_jmp, ctx->prop_end_label);
761 }
762
763 static HRESULT compile_onerror_statement(compile_ctx_t *ctx, onerror_statement_t *stat)
764 {
765     return push_instr_int(ctx, OP_errmode, stat->resume_next);
766 }
767
768 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
769 {
770     HRESULT hres;
771
772     while(stat) {
773         switch(stat->type) {
774         case STAT_ASSIGN:
775             hres = compile_assign_statement(ctx, (assign_statement_t*)stat, FALSE);
776             break;
777         case STAT_CALL:
778             hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
779             break;
780         case STAT_CONST:
781             hres = compile_const_statement(ctx, (const_statement_t*)stat);
782             break;
783         case STAT_DIM:
784             hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
785             break;
786         case STAT_DOWHILE:
787         case STAT_DOUNTIL:
788             hres = compile_dowhile_statement(ctx, (while_statement_t*)stat);
789             break;
790         case STAT_EXITDO:
791             hres = compile_exitdo_statement(ctx);
792             break;
793         case STAT_EXITFUNC:
794             hres = compile_exitfunc_statement(ctx);
795             break;
796         case STAT_EXITPROP:
797             hres = compile_exitprop_statement(ctx);
798             break;
799         case STAT_EXITSUB:
800             hres = compile_exitsub_statement(ctx);
801             break;
802         case STAT_FUNC:
803             hres = compile_function_statement(ctx, (function_statement_t*)stat);
804             break;
805         case STAT_IF:
806             hres = compile_if_statement(ctx, (if_statement_t*)stat);
807             break;
808         case STAT_ONERROR:
809             hres = compile_onerror_statement(ctx, (onerror_statement_t*)stat);
810             break;
811         case STAT_SET:
812             hres = compile_assign_statement(ctx, (assign_statement_t*)stat, TRUE);
813             break;
814         case STAT_STOP:
815             hres = push_instr(ctx, OP_stop) == -1 ? E_OUTOFMEMORY : S_OK;
816             break;
817         case STAT_UNTIL:
818         case STAT_WHILE:
819         case STAT_WHILELOOP:
820             hres = compile_while_statement(ctx, (while_statement_t*)stat);
821             break;
822         default:
823             FIXME("Unimplemented statement type %d\n", stat->type);
824             hres = E_NOTIMPL;
825         }
826
827         if(FAILED(hres))
828             return hres;
829         stat = stat->next;
830     }
831
832     return S_OK;
833 }
834
835 static void resolve_labels(compile_ctx_t *ctx, unsigned off)
836 {
837     instr_t *instr;
838
839     for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
840         if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
841             assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
842             instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
843         }
844         assert(instr_info[instr->op].arg2_type != ARG_ADDR);
845     }
846
847     ctx->labels_cnt = 0;
848 }
849
850 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
851 {
852     HRESULT hres;
853
854     func->code_off = ctx->instr_cnt;
855
856     ctx->while_end_label = -1;
857     ctx->sub_end_label = -1;
858     ctx->func_end_label = -1;
859     ctx->prop_end_label = -1;
860
861     switch(func->type) {
862     case FUNC_FUNCTION:
863         ctx->func_end_label = alloc_label(ctx);
864         if(ctx->func_end_label == -1)
865             return E_OUTOFMEMORY; /* FIXME ! */
866         break;
867     case FUNC_SUB:
868         ctx->sub_end_label = alloc_label(ctx);
869         if(ctx->sub_end_label == -1)
870             return E_OUTOFMEMORY;
871         break;
872     case FUNC_PROPGET:
873     case FUNC_PROPLET:
874     case FUNC_PROPSET:
875     case FUNC_DEFGET:
876         ctx->prop_end_label = alloc_label(ctx);
877         if(ctx->prop_end_label == -1)
878             return E_OUTOFMEMORY;
879         break;
880     case FUNC_GLOBAL:
881         break;
882     }
883
884     ctx->func = func;
885     ctx->dim_decls = NULL;
886     ctx->const_decls = NULL;
887     hres = compile_statement(ctx, stat);
888     ctx->func = NULL;
889     if(FAILED(hres))
890         return hres;
891
892     assert(ctx->while_end_label == -1);
893
894     if(ctx->sub_end_label != -1)
895         label_set_addr(ctx, ctx->sub_end_label);
896     if(ctx->func_end_label != -1)
897         label_set_addr(ctx, ctx->func_end_label);
898     if(ctx->prop_end_label != -1)
899         label_set_addr(ctx, ctx->prop_end_label);
900
901     if(push_instr(ctx, OP_ret) == -1)
902         return E_OUTOFMEMORY;
903
904     resolve_labels(ctx, func->code_off);
905
906     if(func->var_cnt) {
907         dim_decl_t *dim_decl;
908
909         if(func->type == FUNC_GLOBAL) {
910             dynamic_var_t *new_var;
911
912             func->var_cnt = 0;
913
914             for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
915                 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
916                 if(!new_var)
917                     return E_OUTOFMEMORY;
918
919                 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
920                 if(!new_var->name)
921                     return E_OUTOFMEMORY;
922
923                 V_VT(&new_var->v) = VT_EMPTY;
924
925                 new_var->next = ctx->global_vars;
926                 ctx->global_vars = new_var;
927             }
928         }else {
929             unsigned i;
930
931             func->vars = compiler_alloc(ctx->code, func->var_cnt * sizeof(var_desc_t));
932             if(!func->vars)
933                 return E_OUTOFMEMORY;
934
935             for(dim_decl = ctx->dim_decls, i=0; dim_decl; dim_decl = dim_decl->next, i++) {
936                 func->vars[i].name = compiler_alloc_string(ctx->code, dim_decl->name);
937                 if(!func->vars[i].name)
938                     return E_OUTOFMEMORY;
939             }
940
941             assert(i == func->var_cnt);
942         }
943     }
944
945     return S_OK;
946 }
947
948 static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
949 {
950     function_t *iter;
951
952     for(iter = ctx->funcs; iter; iter = iter->next) {
953         if(!strcmpiW(iter->name, name))
954             return TRUE;
955     }
956
957     return FALSE;
958 }
959
960 static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
961 {
962     function_t *func;
963     HRESULT hres;
964
965     if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name) || lookup_const_decls(ctx, decl->name, FALSE)) {
966         FIXME("%s: redefinition\n", debugstr_w(decl->name));
967         return E_FAIL;
968     }
969
970     func = compiler_alloc(ctx->code, sizeof(*func));
971     if(!func)
972         return E_OUTOFMEMORY;
973
974     func->name = compiler_alloc_string(ctx->code, decl->name);
975     if(!func->name)
976         return E_OUTOFMEMORY;
977
978     func->vars = NULL;
979     func->var_cnt = 0;
980     func->code_ctx = ctx->code;
981     func->type = decl->type;
982     func->is_public = decl->is_public;
983
984     func->arg_cnt = 0;
985     if(decl->args) {
986         arg_decl_t *arg;
987         unsigned i;
988
989         for(arg = decl->args; arg; arg = arg->next)
990             func->arg_cnt++;
991
992         func->args = compiler_alloc(ctx->code, func->arg_cnt * sizeof(arg_desc_t));
993         if(!func->args)
994             return E_OUTOFMEMORY;
995
996         for(i = 0, arg = decl->args; arg; arg = arg->next, i++) {
997             func->args[i].name = compiler_alloc_string(ctx->code, arg->name);
998             if(!func->args[i].name)
999                 return E_OUTOFMEMORY;
1000             func->args[i].by_ref = arg->by_ref;
1001         }
1002     }else {
1003         func->args = NULL;
1004     }
1005
1006     hres = compile_func(ctx, decl->body, func);
1007     if(FAILED(hres))
1008         return hres;
1009
1010     *ret = func;
1011     return S_OK;
1012 }
1013
1014 static BOOL lookup_class_name(compile_ctx_t *ctx, const WCHAR *name)
1015 {
1016     class_desc_t *iter;
1017
1018     for(iter = ctx->classes; iter; iter = iter->next) {
1019         if(!strcmpiW(iter->name, name))
1020             return TRUE;
1021     }
1022
1023     return FALSE;
1024 }
1025
1026 static HRESULT create_class_funcprop(compile_ctx_t *ctx, function_decl_t *func_decl, vbdisp_funcprop_desc_t *desc)
1027 {
1028     vbdisp_invoke_type_t invoke_type;
1029     function_decl_t *funcprop_decl;
1030     HRESULT hres;
1031
1032     desc->name = compiler_alloc_string(ctx->code, func_decl->name);
1033     if(!desc->name)
1034         return E_OUTOFMEMORY;
1035
1036     for(funcprop_decl = func_decl; funcprop_decl; funcprop_decl = funcprop_decl->next_prop_func) {
1037         switch(funcprop_decl->type) {
1038         case FUNC_FUNCTION:
1039         case FUNC_SUB:
1040         case FUNC_PROPGET:
1041         case FUNC_DEFGET:
1042             invoke_type = VBDISP_CALLGET;
1043             break;
1044         case FUNC_PROPLET:
1045             invoke_type = VBDISP_LET;
1046             break;
1047         case FUNC_PROPSET:
1048             invoke_type = VBDISP_SET;
1049             break;
1050         default:
1051             assert(0);
1052         }
1053
1054         assert(!desc->entries[invoke_type]);
1055
1056         if(funcprop_decl->is_public)
1057             desc->is_public = TRUE;
1058
1059         hres = create_function(ctx, funcprop_decl, desc->entries+invoke_type);
1060         if(FAILED(hres))
1061             return hres;
1062     }
1063
1064     return S_OK;
1065 }
1066
1067 static BOOL lookup_class_funcs(class_desc_t *class_desc, const WCHAR *name)
1068 {
1069     unsigned i;
1070
1071     for(i=0; i < class_desc->func_cnt; i++) {
1072         if(class_desc->funcs[i].name && !strcmpiW(class_desc->funcs[i].name, name))
1073             return TRUE;
1074     }
1075
1076     return FALSE;
1077 }
1078
1079 static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl)
1080 {
1081     function_decl_t *func_decl, *func_prop_decl;
1082     class_prop_decl_t *prop_decl;
1083     class_desc_t *class_desc;
1084     unsigned i;
1085     HRESULT hres;
1086
1087     static const WCHAR class_initializeW[] = {'c','l','a','s','s','_','i','n','i','t','i','a','l','i','z','e',0};
1088     static const WCHAR class_terminateW[] = {'c','l','a','s','s','_','t','e','r','m','i','n','a','t','e',0};
1089
1090     if(lookup_dim_decls(ctx, class_decl->name) || lookup_funcs_name(ctx, class_decl->name)
1091             || lookup_const_decls(ctx, class_decl->name, FALSE) || lookup_class_name(ctx, class_decl->name)) {
1092         FIXME("%s: redefinition\n", debugstr_w(class_decl->name));
1093         return E_FAIL;
1094     }
1095
1096     class_desc = compiler_alloc_zero(ctx->code, sizeof(*class_desc));
1097     if(!class_desc)
1098         return E_OUTOFMEMORY;
1099
1100     class_desc->name = compiler_alloc_string(ctx->code, class_decl->name);
1101     if(!class_desc->name)
1102         return E_OUTOFMEMORY;
1103
1104     class_desc->func_cnt = 1; /* always allocate slot for default getter */
1105
1106     for(func_decl = class_decl->funcs; func_decl; func_decl = func_decl->next) {
1107         for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1108             if(func_prop_decl->type == FUNC_DEFGET)
1109                 break;
1110         }
1111         if(!func_prop_decl)
1112             class_desc->func_cnt++;
1113     }
1114
1115     class_desc->funcs = compiler_alloc(ctx->code, class_desc->func_cnt*sizeof(*class_desc->funcs));
1116     if(!class_desc->funcs)
1117         return E_OUTOFMEMORY;
1118     memset(class_desc->funcs, 0, class_desc->func_cnt*sizeof(*class_desc->funcs));
1119
1120     for(func_decl = class_decl->funcs, i=1; func_decl; func_decl = func_decl->next, i++) {
1121         for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1122             if(func_prop_decl->type == FUNC_DEFGET) {
1123                 i--;
1124                 break;
1125             }
1126         }
1127
1128         if(!strcmpiW(class_initializeW, func_decl->name)) {
1129             if(func_decl->type != FUNC_SUB) {
1130                 FIXME("class initializer is not sub\n");
1131                 return E_FAIL;
1132             }
1133
1134             class_desc->class_initialize_id = i;
1135         }else  if(!strcmpiW(class_terminateW, func_decl->name)) {
1136             if(func_decl->type != FUNC_SUB) {
1137                 FIXME("class terminator is not sub\n");
1138                 return E_FAIL;
1139             }
1140
1141             class_desc->class_terminate_id = i;
1142         }
1143
1144         hres = create_class_funcprop(ctx, func_decl, class_desc->funcs + (func_prop_decl ? 0 : i));
1145         if(FAILED(hres))
1146             return hres;
1147     }
1148
1149     for(prop_decl = class_decl->props; prop_decl; prop_decl = prop_decl->next)
1150         class_desc->prop_cnt++;
1151
1152     class_desc->props = compiler_alloc(ctx->code, class_desc->prop_cnt*sizeof(*class_desc->props));
1153     if(!class_desc->props)
1154         return E_OUTOFMEMORY;
1155
1156     for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next, i++) {
1157         if(lookup_class_funcs(class_desc, prop_decl->name)) {
1158             FIXME("Property %s redefined\n", debugstr_w(prop_decl->name));
1159             return E_FAIL;
1160         }
1161
1162         class_desc->props[i].name = compiler_alloc_string(ctx->code, prop_decl->name);
1163         if(!class_desc->props[i].name)
1164             return E_OUTOFMEMORY;
1165
1166         class_desc->props[i].is_public = prop_decl->is_public;
1167     }
1168
1169     class_desc->next = ctx->classes;
1170     ctx->classes = class_desc;
1171     return S_OK;
1172 }
1173
1174 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
1175 {
1176     class_desc_t *class;
1177     dynamic_var_t *var;
1178     function_t *func;
1179
1180     for(var = script->global_vars; var; var = var->next) {
1181         if(!strcmpiW(var->name, identifier))
1182             return TRUE;
1183     }
1184
1185     for(func = script->global_funcs; func; func = func->next) {
1186         if(!strcmpiW(func->name, identifier))
1187             return TRUE;
1188     }
1189
1190     for(class = script->classes; class; class = class->next) {
1191         if(!strcmpiW(class->name, identifier))
1192             return TRUE;
1193     }
1194
1195     return FALSE;
1196 }
1197
1198 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
1199 {
1200     class_desc_t *class;
1201     dynamic_var_t *var;
1202     function_t *func;
1203
1204     for(var = ctx->global_vars; var; var = var->next) {
1205         if(lookup_script_identifier(script, var->name)) {
1206             FIXME("%s: redefined\n", debugstr_w(var->name));
1207             return E_FAIL;
1208         }
1209     }
1210
1211     for(func = ctx->funcs; func; func = func->next) {
1212         if(lookup_script_identifier(script, func->name)) {
1213             FIXME("%s: redefined\n", debugstr_w(func->name));
1214             return E_FAIL;
1215         }
1216     }
1217
1218     for(class = ctx->classes; class; class = class->next) {
1219         if(lookup_script_identifier(script, class->name)) {
1220             FIXME("%s: redefined\n", debugstr_w(class->name));
1221             return E_FAIL;
1222         }
1223     }
1224
1225     return S_OK;
1226 }
1227
1228 void release_vbscode(vbscode_t *code)
1229 {
1230     unsigned i;
1231
1232     list_remove(&code->entry);
1233
1234     for(i=0; i < code->bstr_cnt; i++)
1235         SysFreeString(code->bstr_pool[i]);
1236
1237     vbsheap_free(&code->heap);
1238
1239     heap_free(code->bstr_pool);
1240     heap_free(code->source);
1241     heap_free(code->instrs);
1242     heap_free(code);
1243 }
1244
1245 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
1246 {
1247     vbscode_t *ret;
1248
1249     ret = heap_alloc(sizeof(*ret));
1250     if(!ret)
1251         return NULL;
1252
1253     ret->source = heap_strdupW(source);
1254     if(!ret->source) {
1255         heap_free(ret);
1256         return NULL;
1257     }
1258
1259     ret->instrs = heap_alloc(32*sizeof(instr_t));
1260     if(!ret->instrs) {
1261         release_vbscode(ret);
1262         return NULL;
1263     }
1264
1265     ctx->instr_cnt = 0;
1266     ctx->instr_size = 32;
1267     vbsheap_init(&ret->heap);
1268
1269     ret->option_explicit = ctx->parser.option_explicit;
1270
1271     ret->bstr_pool = NULL;
1272     ret->bstr_pool_size = 0;
1273     ret->bstr_cnt = 0;
1274     ret->global_executed = FALSE;
1275
1276     ret->global_code.type = FUNC_GLOBAL;
1277     ret->global_code.name = NULL;
1278     ret->global_code.code_ctx = ret;
1279     ret->global_code.vars = NULL;
1280     ret->global_code.var_cnt = 0;
1281     ret->global_code.arg_cnt = 0;
1282     ret->global_code.args = NULL;
1283
1284     list_init(&ret->entry);
1285     return ret;
1286 }
1287
1288 static void release_compiler(compile_ctx_t *ctx)
1289 {
1290     parser_release(&ctx->parser);
1291     heap_free(ctx->labels);
1292     if(ctx->code)
1293         release_vbscode(ctx->code);
1294 }
1295
1296 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
1297 {
1298     function_t *new_func;
1299     function_decl_t *func_decl;
1300     class_decl_t *class_decl;
1301     compile_ctx_t ctx;
1302     vbscode_t *code;
1303     HRESULT hres;
1304
1305     hres = parse_script(&ctx.parser, src);
1306     if(FAILED(hres))
1307         return hres;
1308
1309     code = ctx.code = alloc_vbscode(&ctx, src);
1310     if(!ctx.code)
1311         return E_OUTOFMEMORY;
1312
1313     ctx.funcs = NULL;
1314     ctx.func_decls = NULL;
1315     ctx.global_vars = NULL;
1316     ctx.dim_decls = NULL;
1317     ctx.classes = NULL;
1318     ctx.labels = NULL;
1319     ctx.global_consts = NULL;
1320     ctx.labels_cnt = ctx.labels_size = 0;
1321
1322     hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
1323     if(FAILED(hres)) {
1324         release_compiler(&ctx);
1325         return hres;
1326     }
1327
1328     ctx.global_consts = ctx.const_decls;
1329
1330     for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
1331         hres = create_function(&ctx, func_decl, &new_func);
1332         if(FAILED(hres)) {
1333             release_compiler(&ctx);
1334             return hres;
1335         }
1336
1337         new_func->next = ctx.funcs;
1338         ctx.funcs = new_func;
1339     }
1340
1341     for(class_decl = ctx.parser.class_decls; class_decl; class_decl = class_decl->next) {
1342         hres = compile_class(&ctx, class_decl);
1343         if(FAILED(hres)) {
1344             release_compiler(&ctx);
1345             return hres;
1346         }
1347     }
1348
1349     hres = check_script_collisions(&ctx, script);
1350     if(FAILED(hres)) {
1351         release_compiler(&ctx);
1352         return hres;
1353     }
1354
1355     if(ctx.global_vars) {
1356         dynamic_var_t *var;
1357
1358         for(var = ctx.global_vars; var->next; var = var->next);
1359
1360         var->next = script->global_vars;
1361         script->global_vars = ctx.global_vars;
1362     }
1363
1364     if(ctx.funcs) {
1365         for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);
1366
1367         new_func->next = script->global_funcs;
1368         script->global_funcs = ctx.funcs;
1369     }
1370
1371     if(ctx.classes) {
1372         class_desc_t *class = ctx.classes;
1373
1374         while(1) {
1375             class->ctx = script;
1376             if(!class->next)
1377                 break;
1378             class = class->next;
1379         }
1380
1381         class->next = script->classes;
1382         script->classes = ctx.classes;
1383     }
1384
1385     if(TRACE_ON(vbscript_disas))
1386         dump_code(&ctx);
1387
1388     ctx.code = NULL;
1389     release_compiler(&ctx);
1390
1391     list_add_tail(&script->code_list, &code->entry);
1392     *ret = code;
1393     return S_OK;
1394 }