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