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