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