vbscript: Added function invocation supprot to do_icall.
[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     dim_decl_t *dim_decls;
42     dynamic_var_t *global_vars;
43
44     function_t *func;
45     function_t *funcs;
46     function_decl_t *func_decls;
47 } compile_ctx_t;
48
49 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
50 static HRESULT compile_statement(compile_ctx_t*,statement_t*);
51
52 static const struct {
53     const char *op_str;
54     instr_arg_type_t arg1_type;
55     instr_arg_type_t arg2_type;
56 } instr_info[] = {
57 #define X(n,a,b,c) {#n,b,c},
58 OP_LIST
59 #undef X
60 };
61
62 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
63 {
64     switch(type) {
65     case ARG_STR:
66     case ARG_BSTR:
67         TRACE_(vbscript_disas)("\t%s", debugstr_w(arg->str));
68         break;
69     case ARG_INT:
70         TRACE_(vbscript_disas)("\t%d", arg->uint);
71         break;
72     case ARG_UINT:
73     case ARG_ADDR:
74         TRACE_(vbscript_disas)("\t%u", arg->uint);
75         break;
76     case ARG_DOUBLE:
77         TRACE_(vbscript_disas)("\t%lf", *arg->dbl);
78         break;
79     case ARG_NONE:
80         break;
81     default:
82         assert(0);
83     }
84 }
85
86 static void dump_code(compile_ctx_t *ctx)
87 {
88     instr_t *instr;
89
90     for(instr = ctx->code->instrs; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
91         TRACE_(vbscript_disas)("%d:\t%s", instr-ctx->code->instrs, instr_info[instr->op].op_str);
92         dump_instr_arg(instr_info[instr->op].arg1_type, &instr->arg1);
93         dump_instr_arg(instr_info[instr->op].arg2_type, &instr->arg2);
94         TRACE_(vbscript_disas)("\n");
95     }
96 }
97
98 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
99 {
100     return vbsheap_alloc(&vbscode->heap, size);
101 }
102
103 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
104 {
105     size_t size;
106     WCHAR *ret;
107
108     size = (strlenW(str)+1)*sizeof(WCHAR);
109     ret = compiler_alloc(vbscode, size);
110     if(ret)
111         memcpy(ret, str, size);
112     return ret;
113 }
114
115 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
116 {
117     assert(id < ctx->instr_cnt);
118     return ctx->code->instrs + id;
119 }
120
121 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
122 {
123     assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
124
125     if(ctx->instr_size == ctx->instr_cnt) {
126         instr_t *new_instr;
127
128         new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
129         if(!new_instr)
130             return -1;
131
132         ctx->code->instrs = new_instr;
133         ctx->instr_size *= 2;
134     }
135
136     ctx->code->instrs[ctx->instr_cnt].op = op;
137     return ctx->instr_cnt++;
138 }
139
140 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
141 {
142     unsigned ret;
143
144     ret = push_instr(ctx, op);
145     if(ret == -1)
146         return E_OUTOFMEMORY;
147
148     instr_ptr(ctx, ret)->arg1.lng = arg;
149     return S_OK;
150 }
151
152 static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
153 {
154     unsigned ret;
155
156     ret = push_instr(ctx, op);
157     if(ret == -1)
158         return E_OUTOFMEMORY;
159
160     instr_ptr(ctx, ret)->arg1.uint = arg;
161     return S_OK;
162 }
163
164 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
165 {
166     unsigned instr;
167     WCHAR *str;
168
169     str = compiler_alloc_string(ctx->code, arg);
170     if(!str)
171         return E_OUTOFMEMORY;
172
173     instr = push_instr(ctx, op);
174     if(instr == -1)
175         return E_OUTOFMEMORY;
176
177     instr_ptr(ctx, instr)->arg1.str = str;
178     return S_OK;
179 }
180
181 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
182 {
183     unsigned instr;
184     double *d;
185
186     d = compiler_alloc(ctx->code, sizeof(double));
187     if(!d)
188         return E_OUTOFMEMORY;
189
190     instr = push_instr(ctx, op);
191     if(instr == -1)
192         return E_OUTOFMEMORY;
193
194     *d = arg;
195     instr_ptr(ctx, instr)->arg1.dbl = d;
196     return S_OK;
197 }
198
199 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
200 {
201     if(!ctx->code->bstr_pool_size) {
202         ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
203         if(!ctx->code->bstr_pool)
204             return NULL;
205         ctx->code->bstr_pool_size = 8;
206     }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
207        BSTR *new_pool;
208
209         new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
210         if(!new_pool)
211             return NULL;
212
213         ctx->code->bstr_pool = new_pool;
214         ctx->code->bstr_pool_size *= 2;
215     }
216
217     ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
218     if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
219         return NULL;
220
221     return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
222 }
223
224 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
225 {
226     unsigned instr;
227     BSTR bstr;
228
229     bstr = alloc_bstr_arg(ctx, arg);
230     if(!bstr)
231         return E_OUTOFMEMORY;
232
233     instr = push_instr(ctx, op);
234     if(instr == -1)
235         return E_OUTOFMEMORY;
236
237     instr_ptr(ctx, instr)->arg1.bstr = bstr;
238     return S_OK;
239 }
240
241 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
242 {
243     unsigned instr;
244     BSTR bstr;
245
246     bstr = alloc_bstr_arg(ctx, arg1);
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     instr_ptr(ctx, instr)->arg2.uint = arg2;
256     return S_OK;
257 }
258
259 #define LABEL_FLAG 0x80000000
260
261 static unsigned alloc_label(compile_ctx_t *ctx)
262 {
263     if(!ctx->labels_size) {
264         ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
265         if(!ctx->labels)
266             return -1;
267         ctx->labels_size = 8;
268     }else if(ctx->labels_size == ctx->labels_cnt) {
269         unsigned *new_labels;
270
271         new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
272         if(!new_labels)
273             return -1;
274
275         ctx->labels = new_labels;
276         ctx->labels_size *= 2;
277     }
278
279     return ctx->labels_cnt++ | LABEL_FLAG;
280 }
281
282 static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
283 {
284     assert(label & LABEL_FLAG);
285     ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
286 }
287
288 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
289 {
290     unsigned arg_cnt = 0;
291     HRESULT hres;
292
293     while(args) {
294         hres = compile_expression(ctx, args);
295         if(FAILED(hres))
296             return hres;
297
298         arg_cnt++;
299         args = args->next;
300     }
301
302     *ret = arg_cnt;
303     return S_OK;
304 }
305
306 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
307 {
308     unsigned arg_cnt = 0;
309     HRESULT hres;
310
311     hres = compile_args(ctx, expr->args, &arg_cnt);
312     if(FAILED(hres))
313         return hres;
314
315     if(expr->obj_expr) {
316         FIXME("obj_expr not implemented\n");
317         hres = E_NOTIMPL;
318     }else {
319         hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
320     }
321
322     return hres;
323 }
324
325 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
326 {
327     HRESULT hres;
328
329     hres = compile_expression(ctx, expr->subexpr);
330     if(FAILED(hres))
331         return hres;
332
333     return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
334 }
335
336 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
337 {
338     HRESULT hres;
339
340     hres = compile_expression(ctx, expr->left);
341     if(FAILED(hres))
342         return hres;
343
344     hres = compile_expression(ctx, expr->right);
345     if(FAILED(hres))
346         return hres;
347
348     return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
349 }
350
351 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
352 {
353     switch(expr->type) {
354     case EXPR_ADD:
355         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
356     case EXPR_BOOL:
357         return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
358     case EXPR_CONCAT:
359         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
360     case EXPR_DIV:
361         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
362     case EXPR_DOUBLE:
363         return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
364     case EXPR_EMPTY:
365         return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
366     case EXPR_EQUAL:
367         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
368     case EXPR_EXP:
369         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
370     case EXPR_IDIV:
371         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
372     case EXPR_MEMBER:
373         return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
374     case EXPR_MOD:
375         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
376     case EXPR_MUL:
377         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
378     case EXPR_NEG:
379         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
380     case EXPR_NEQUAL:
381         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
382     case EXPR_NOT:
383         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
384     case EXPR_NULL:
385         return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
386     case EXPR_STRING:
387         return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
388     case EXPR_SUB:
389         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
390     case EXPR_USHORT:
391         return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
392     case EXPR_ULONG:
393         return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
394     default:
395         FIXME("Unimplemented expression type %d\n", expr->type);
396         return E_NOTIMPL;
397     }
398
399     return S_OK;
400 }
401
402 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
403 {
404     unsigned cnd_jmp, endif_label = -1;
405     elseif_decl_t *elseif_decl;
406     HRESULT hres;
407
408     hres = compile_expression(ctx, stat->expr);
409     if(FAILED(hres))
410         return hres;
411
412     cnd_jmp = push_instr(ctx, OP_jmp_false);
413     if(cnd_jmp == -1)
414         return E_OUTOFMEMORY;
415
416     hres = compile_statement(ctx, stat->if_stat);
417     if(FAILED(hres))
418         return hres;
419
420     if(stat->else_stat || stat->elseifs) {
421         endif_label = alloc_label(ctx);
422         if(endif_label == -1)
423             return E_OUTOFMEMORY;
424
425         hres = push_instr_addr(ctx, OP_jmp, endif_label);
426         if(FAILED(hres))
427             return hres;
428     }
429
430     for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
431         instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
432
433         hres = compile_expression(ctx, elseif_decl->expr);
434         if(FAILED(hres))
435             return hres;
436
437         cnd_jmp = push_instr(ctx, OP_jmp_false);
438         if(cnd_jmp == -1)
439             return E_OUTOFMEMORY;
440
441         hres = compile_statement(ctx, elseif_decl->stat);
442         if(FAILED(hres))
443             return hres;
444
445         hres = push_instr_addr(ctx, OP_jmp, endif_label);
446         if(FAILED(hres))
447             return hres;
448     }
449
450     instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
451
452     if(stat->else_stat) {
453         hres = compile_statement(ctx, stat->else_stat);
454         if(FAILED(hres))
455             return hres;
456     }
457
458     if(endif_label != -1)
459         label_set_addr(ctx, endif_label);
460     return S_OK;
461 }
462
463 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat)
464 {
465     HRESULT hres;
466
467     hres = compile_expression(ctx, stat->value_expr);
468     if(FAILED(hres))
469         return hres;
470
471     if(stat->member_expr->args) {
472         FIXME("arguments support not implemented\n");
473         return E_NOTIMPL;
474     }
475
476     if(stat->member_expr->obj_expr) {
477         hres = compile_expression(ctx, stat->member_expr->obj_expr);
478         if(FAILED(hres))
479             return hres;
480
481         hres = push_instr_bstr(ctx, OP_assign_member, stat->member_expr->identifier);
482     }else {
483         hres = push_instr_bstr(ctx, OP_assign_ident, stat->member_expr->identifier);
484     }
485
486     return hres;
487 }
488
489 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
490 {
491     dim_decl_t *dim_decl;
492
493     for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
494         if(!strcmpiW(dim_decl->name, name))
495             return TRUE;
496     }
497
498     return FALSE;
499 }
500
501 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
502 {
503     dim_decl_t *dim_decl = stat->dim_decls;
504
505     while(1) {
506         if(lookup_dim_decls(ctx, dim_decl->name)) {
507             FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
508             return E_FAIL;
509         }
510
511         if(!dim_decl->next)
512             break;
513         dim_decl = dim_decl->next;
514     }
515
516     dim_decl->next = ctx->dim_decls;
517     ctx->dim_decls = stat->dim_decls;
518     return S_OK;
519 }
520
521 static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
522 {
523     if(ctx->func != &ctx->code->global_code) {
524         FIXME("Function is not in the global code\n");
525         return E_FAIL;
526     }
527
528     stat->func_decl->next = ctx->func_decls;
529     ctx->func_decls = stat->func_decl;
530     return S_OK;
531 }
532
533 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
534 {
535     HRESULT hres;
536
537     while(stat) {
538         switch(stat->type) {
539         case STAT_ASSIGN:
540             hres = compile_assign_statement(ctx, (assign_statement_t*)stat);
541             break;
542         case STAT_CALL:
543             hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
544             break;
545         case STAT_DIM:
546             hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
547             break;
548         case STAT_FUNC:
549             hres = compile_function_statement(ctx, (function_statement_t*)stat);
550             break;
551         case STAT_IF:
552             hres = compile_if_statement(ctx, (if_statement_t*)stat);
553             break;
554         default:
555             FIXME("Unimplemented statement type %d\n", stat->type);
556             hres = E_NOTIMPL;
557         }
558
559         if(FAILED(hres))
560             return hres;
561         stat = stat->next;
562     }
563
564     return S_OK;
565 }
566
567 static void resolve_labels(compile_ctx_t *ctx, unsigned off)
568 {
569     instr_t *instr;
570
571     for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
572         if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
573             assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
574             instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
575         }
576         assert(instr_info[instr->op].arg2_type != ARG_ADDR);
577     }
578
579     ctx->labels_cnt = 0;
580 }
581
582 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
583 {
584     HRESULT hres;
585
586     func->code_off = ctx->instr_cnt;
587
588     ctx->func = func;
589     ctx->dim_decls = NULL;
590     hres = compile_statement(ctx, stat);
591     ctx->func = NULL;
592     if(FAILED(hres))
593         return hres;
594
595     if(push_instr(ctx, OP_ret) == -1)
596         return E_OUTOFMEMORY;
597
598     resolve_labels(ctx, func->code_off);
599
600     if(ctx->dim_decls) {
601         dim_decl_t *dim_decl;
602
603         if(func->type == FUNC_GLOBAL) {
604             dynamic_var_t *new_var;
605
606             for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
607                 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
608                 if(!new_var)
609                     return E_OUTOFMEMORY;
610
611                 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
612                 if(!new_var->name)
613                     return E_OUTOFMEMORY;
614
615                 V_VT(&new_var->v) = VT_EMPTY;
616
617                 new_var->next = ctx->global_vars;
618                 ctx->global_vars = new_var;
619             }
620         }else {
621             FIXME("var not implemented for functions\n");
622         }
623     }
624
625     return S_OK;
626 }
627
628 static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
629 {
630     function_t *iter;
631
632     for(iter = ctx->funcs; iter; iter = iter->next) {
633         if(!strcmpiW(iter->name, name))
634             return TRUE;
635     }
636
637     return FALSE;
638 }
639
640 static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
641 {
642     function_t *func;
643     HRESULT hres;
644
645     if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name)) {
646         FIXME("%s: redefinition\n", debugstr_w(decl->name));
647         return E_FAIL;
648     }
649
650     func = compiler_alloc(ctx->code, sizeof(*func));
651     if(!func)
652         return E_OUTOFMEMORY;
653
654     func->name = compiler_alloc_string(ctx->code, decl->name);
655     if(!func->name)
656         return E_OUTOFMEMORY;
657
658     func->code_ctx = ctx->code;
659     func->type = decl->type;
660
661     if(decl->args) {
662         FIXME("arguments not implemented\n");
663         return E_NOTIMPL;
664     }
665
666     hres = compile_func(ctx, decl->body, func);
667     if(FAILED(hres))
668         return hres;
669
670     *ret = func;
671     return S_OK;
672 }
673
674 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
675 {
676     dynamic_var_t *var;
677     function_t *func;
678
679     for(var = script->global_vars; var; var = var->next) {
680         if(!strcmpiW(var->name, identifier))
681             return TRUE;
682     }
683
684     for(func = script->global_funcs; func; func = func->next) {
685         if(!strcmpiW(func->name, identifier))
686             return TRUE;
687     }
688
689     return FALSE;
690 }
691
692 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
693 {
694     dynamic_var_t *var;
695     function_t *func;
696
697     for(var = ctx->global_vars; var; var = var->next) {
698         if(lookup_script_identifier(script, var->name)) {
699             FIXME("%s: redefined\n", debugstr_w(var->name));
700             return E_FAIL;
701         }
702     }
703
704     for(func = ctx->funcs; func; func = func->next) {
705         if(lookup_script_identifier(script, func->name)) {
706             FIXME("%s: redefined\n", debugstr_w(func->name));
707             return E_FAIL;
708         }
709     }
710
711     return S_OK;
712 }
713
714 void release_vbscode(vbscode_t *code)
715 {
716     unsigned i;
717
718     list_remove(&code->entry);
719
720     for(i=0; i < code->bstr_cnt; i++)
721         SysFreeString(code->bstr_pool[i]);
722
723     vbsheap_free(&code->heap);
724
725     heap_free(code->bstr_pool);
726     heap_free(code->source);
727     heap_free(code->instrs);
728     heap_free(code);
729 }
730
731 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
732 {
733     vbscode_t *ret;
734
735     ret = heap_alloc(sizeof(*ret));
736     if(!ret)
737         return NULL;
738
739     ret->source = heap_strdupW(source);
740     if(!ret->source) {
741         heap_free(ret);
742         return NULL;
743     }
744
745     ret->instrs = heap_alloc(32*sizeof(instr_t));
746     if(!ret->instrs) {
747         release_vbscode(ret);
748         return NULL;
749     }
750
751     ctx->instr_cnt = 0;
752     ctx->instr_size = 32;
753     vbsheap_init(&ret->heap);
754
755     ret->option_explicit = ctx->parser.option_explicit;
756
757     ret->bstr_pool = NULL;
758     ret->bstr_pool_size = 0;
759     ret->bstr_cnt = 0;
760
761     ret->global_code.type = FUNC_GLOBAL;
762     ret->global_code.name = NULL;
763     ret->global_code.code_ctx = ret;
764
765     list_init(&ret->entry);
766     return ret;
767 }
768
769 static void release_compiler(compile_ctx_t *ctx)
770 {
771     parser_release(&ctx->parser);
772     heap_free(ctx->labels);
773     if(ctx->code)
774         release_vbscode(ctx->code);
775 }
776
777 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
778 {
779     function_t *new_func;
780     function_decl_t *func_decl;
781     compile_ctx_t ctx;
782     vbscode_t *code;
783     HRESULT hres;
784
785     hres = parse_script(&ctx.parser, src);
786     if(FAILED(hres))
787         return hres;
788
789     code = ctx.code = alloc_vbscode(&ctx, src);
790     if(!ctx.code)
791         return E_OUTOFMEMORY;
792
793     ctx.funcs = NULL;
794     ctx.func_decls = NULL;
795     ctx.global_vars = NULL;
796     ctx.dim_decls = NULL;
797     ctx.labels = NULL;
798     ctx.labels_cnt = ctx.labels_size = 0;
799
800     hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
801     if(FAILED(hres)) {
802         release_compiler(&ctx);
803         return hres;
804     }
805
806     for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
807         hres = create_function(&ctx, func_decl, &new_func);
808         if(FAILED(hres)) {
809             release_compiler(&ctx);
810             return hres;
811         }
812
813         new_func->next = ctx.funcs;
814         ctx.funcs = new_func;
815     }
816
817     hres = check_script_collisions(&ctx, script);
818     if(FAILED(hres)) {
819         release_compiler(&ctx);
820         return hres;
821     }
822
823     if(ctx.global_vars) {
824         dynamic_var_t *var;
825
826         for(var = ctx.global_vars; var->next; var = var->next);
827
828         var->next = script->global_vars;
829         script->global_vars = ctx.global_vars;
830     }
831
832     if(ctx.funcs) {
833         for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);
834
835         new_func->next = script->global_funcs;
836         script->global_funcs = ctx.funcs;
837     }
838
839     if(TRACE_ON(vbscript_disas))
840         dump_code(&ctx);
841
842     ctx.code = NULL;
843     release_compiler(&ctx);
844
845     list_add_tail(&script->code_list, &code->entry);
846     *ret = code;
847     return S_OK;
848 }