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