vbscript: Added if statement compiler 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
29 typedef struct {
30     parser_ctx_t parser;
31
32     unsigned instr_cnt;
33     unsigned instr_size;
34     vbscode_t *code;
35
36     unsigned *labels;
37     unsigned labels_size;
38     unsigned labels_cnt;
39
40     dim_decl_t *dim_decls;
41     dynamic_var_t *global_vars;
42 } compile_ctx_t;
43
44 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
45 static HRESULT compile_statement(compile_ctx_t*,statement_t*);
46
47 static const struct {
48     instr_arg_type_t arg1_type;
49     instr_arg_type_t arg2_type;
50 } instr_info[] = {
51 #define X(n,a,b,c) {b,c},
52 OP_LIST
53 #undef X
54 };
55
56 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
57 {
58     return vbsheap_alloc(&vbscode->heap, size);
59 }
60
61 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
62 {
63     size_t size;
64     WCHAR *ret;
65
66     size = (strlenW(str)+1)*sizeof(WCHAR);
67     ret = compiler_alloc(vbscode, size);
68     if(ret)
69         memcpy(ret, str, size);
70     return ret;
71 }
72
73 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
74 {
75     assert(id < ctx->instr_cnt);
76     return ctx->code->instrs + id;
77 }
78
79 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
80 {
81     assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
82
83     if(ctx->instr_size == ctx->instr_cnt) {
84         instr_t *new_instr;
85
86         new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
87         if(!new_instr)
88             return -1;
89
90         ctx->code->instrs = new_instr;
91         ctx->instr_size *= 2;
92     }
93
94     ctx->code->instrs[ctx->instr_cnt].op = op;
95     return ctx->instr_cnt++;
96 }
97
98 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
99 {
100     unsigned ret;
101
102     ret = push_instr(ctx, op);
103     if(ret == -1)
104         return E_OUTOFMEMORY;
105
106     instr_ptr(ctx, ret)->arg1.lng = arg;
107     return S_OK;
108 }
109
110 static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
111 {
112     unsigned ret;
113
114     ret = push_instr(ctx, op);
115     if(ret == -1)
116         return E_OUTOFMEMORY;
117
118     instr_ptr(ctx, ret)->arg1.uint = arg;
119     return S_OK;
120 }
121
122 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
123 {
124     unsigned instr;
125     WCHAR *str;
126
127     str = compiler_alloc_string(ctx->code, arg);
128     if(!str)
129         return E_OUTOFMEMORY;
130
131     instr = push_instr(ctx, op);
132     if(instr == -1)
133         return E_OUTOFMEMORY;
134
135     instr_ptr(ctx, instr)->arg1.str = str;
136     return S_OK;
137 }
138
139 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
140 {
141     unsigned instr;
142     double *d;
143
144     d = compiler_alloc(ctx->code, sizeof(double));
145     if(!d)
146         return E_OUTOFMEMORY;
147
148     instr = push_instr(ctx, op);
149     if(instr == -1)
150         return E_OUTOFMEMORY;
151
152     *d = arg;
153     instr_ptr(ctx, instr)->arg1.dbl = d;
154     return S_OK;
155 }
156
157 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
158 {
159     if(!ctx->code->bstr_pool_size) {
160         ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
161         if(!ctx->code->bstr_pool)
162             return NULL;
163         ctx->code->bstr_pool_size = 8;
164     }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
165        BSTR *new_pool;
166
167         new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
168         if(!new_pool)
169             return NULL;
170
171         ctx->code->bstr_pool = new_pool;
172         ctx->code->bstr_pool_size *= 2;
173     }
174
175     ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
176     if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
177         return NULL;
178
179     return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
180 }
181
182 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
183 {
184     unsigned instr;
185     BSTR bstr;
186
187     bstr = alloc_bstr_arg(ctx, arg);
188     if(!bstr)
189         return E_OUTOFMEMORY;
190
191     instr = push_instr(ctx, op);
192     if(instr == -1)
193         return E_OUTOFMEMORY;
194
195     instr_ptr(ctx, instr)->arg1.bstr = bstr;
196     return S_OK;
197 }
198
199 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
200 {
201     unsigned instr;
202     BSTR bstr;
203
204     bstr = alloc_bstr_arg(ctx, arg1);
205     if(!bstr)
206         return E_OUTOFMEMORY;
207
208     instr = push_instr(ctx, op);
209     if(instr == -1)
210         return E_OUTOFMEMORY;
211
212     instr_ptr(ctx, instr)->arg1.bstr = bstr;
213     instr_ptr(ctx, instr)->arg2.uint = arg2;
214     return S_OK;
215 }
216
217 #define LABEL_FLAG 0x80000000
218
219 static unsigned alloc_label(compile_ctx_t *ctx)
220 {
221     if(!ctx->labels_size) {
222         ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
223         if(!ctx->labels)
224             return -1;
225         ctx->labels_size = 8;
226     }else if(ctx->labels_size == ctx->labels_cnt) {
227         unsigned *new_labels;
228
229         new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
230         if(!new_labels)
231             return -1;
232
233         ctx->labels = new_labels;
234         ctx->labels_size *= 2;
235     }
236
237     return ctx->labels_cnt++ | LABEL_FLAG;
238 }
239
240 static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
241 {
242     assert(label & LABEL_FLAG);
243     ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
244 }
245
246 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
247 {
248     unsigned arg_cnt = 0;
249     HRESULT hres;
250
251     while(args) {
252         hres = compile_expression(ctx, args);
253         if(FAILED(hres))
254             return hres;
255
256         arg_cnt++;
257         args = args->next;
258     }
259
260     *ret = arg_cnt;
261     return S_OK;
262 }
263
264 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
265 {
266     unsigned arg_cnt = 0;
267     HRESULT hres;
268
269     hres = compile_args(ctx, expr->args, &arg_cnt);
270     if(FAILED(hres))
271         return hres;
272
273     if(expr->obj_expr) {
274         FIXME("obj_expr not implemented\n");
275         hres = E_NOTIMPL;
276     }else {
277         hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
278     }
279
280     return hres;
281 }
282
283 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
284 {
285     HRESULT hres;
286
287     hres = compile_expression(ctx, expr->subexpr);
288     if(FAILED(hres))
289         return hres;
290
291     return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
292 }
293
294 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
295 {
296     HRESULT hres;
297
298     hres = compile_expression(ctx, expr->left);
299     if(FAILED(hres))
300         return hres;
301
302     hres = compile_expression(ctx, expr->right);
303     if(FAILED(hres))
304         return hres;
305
306     return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
307 }
308
309 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
310 {
311     switch(expr->type) {
312     case EXPR_ADD:
313         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
314     case EXPR_BOOL:
315         return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
316     case EXPR_CONCAT:
317         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
318     case EXPR_DOUBLE:
319         return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
320     case EXPR_EMPTY:
321         return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
322     case EXPR_EQUAL:
323         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
324     case EXPR_MEMBER:
325         return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
326     case EXPR_NEG:
327         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
328     case EXPR_NEQUAL:
329         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
330     case EXPR_NOT:
331         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
332     case EXPR_NULL:
333         return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
334     case EXPR_STRING:
335         return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
336     case EXPR_SUB:
337         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
338     case EXPR_USHORT:
339         return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
340     case EXPR_ULONG:
341         return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
342     default:
343         FIXME("Unimplemented expression type %d\n", expr->type);
344         return E_NOTIMPL;
345     }
346
347     return S_OK;
348 }
349
350 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
351 {
352     unsigned cnd_jmp, endif_label = -1;
353     elseif_decl_t *elseif_decl;
354     HRESULT hres;
355
356     hres = compile_expression(ctx, stat->expr);
357     if(FAILED(hres))
358         return hres;
359
360     cnd_jmp = push_instr(ctx, OP_jmp_false);
361     if(cnd_jmp == -1)
362         return E_OUTOFMEMORY;
363
364     hres = compile_statement(ctx, stat->if_stat);
365     if(FAILED(hres))
366         return hres;
367
368     if(stat->else_stat || stat->elseifs) {
369         endif_label = alloc_label(ctx);
370         if(endif_label == -1)
371             return E_OUTOFMEMORY;
372
373         hres = push_instr_addr(ctx, OP_jmp, endif_label);
374         if(FAILED(hres))
375             return hres;
376     }
377
378     for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
379         instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
380
381         hres = compile_expression(ctx, elseif_decl->expr);
382         if(FAILED(hres))
383             return hres;
384
385         cnd_jmp = push_instr(ctx, OP_jmp_false);
386         if(cnd_jmp == -1)
387             return E_OUTOFMEMORY;
388
389         hres = compile_statement(ctx, elseif_decl->stat);
390         if(FAILED(hres))
391             return hres;
392
393         hres = push_instr_addr(ctx, OP_jmp, endif_label);
394         if(FAILED(hres))
395             return hres;
396     }
397
398     instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
399
400     if(stat->else_stat) {
401         hres = compile_statement(ctx, stat->else_stat);
402         if(FAILED(hres))
403             return hres;
404     }
405
406     if(endif_label != -1)
407         label_set_addr(ctx, endif_label);
408     return S_OK;
409 }
410
411 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat)
412 {
413     HRESULT hres;
414
415     hres = compile_expression(ctx, stat->value_expr);
416     if(FAILED(hres))
417         return hres;
418
419     if(stat->member_expr->args) {
420         FIXME("arguments support not implemented\n");
421         return E_NOTIMPL;
422     }
423
424     if(stat->member_expr->obj_expr) {
425         hres = compile_expression(ctx, stat->member_expr->obj_expr);
426         if(FAILED(hres))
427             return hres;
428
429         hres = push_instr_bstr(ctx, OP_assign_member, stat->member_expr->identifier);
430     }else {
431         hres = push_instr_bstr(ctx, OP_assign_ident, stat->member_expr->identifier);
432     }
433
434     return hres;
435 }
436
437 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
438 {
439     dim_decl_t *dim_decl;
440
441     for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
442         if(!strcmpiW(dim_decl->name, name))
443             return TRUE;
444     }
445
446     return FALSE;
447 }
448
449 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
450 {
451     dim_decl_t *dim_decl = stat->dim_decls;
452
453     while(1) {
454         if(lookup_dim_decls(ctx, dim_decl->name)) {
455             FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
456             return E_FAIL;
457         }
458
459         if(!dim_decl->next)
460             break;
461         dim_decl = dim_decl->next;
462     }
463
464     dim_decl->next = ctx->dim_decls;
465     ctx->dim_decls = stat->dim_decls;
466     return S_OK;
467 }
468
469 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
470 {
471     HRESULT hres;
472
473     while(stat) {
474         switch(stat->type) {
475         case STAT_ASSIGN:
476             hres = compile_assign_statement(ctx, (assign_statement_t*)stat);
477             break;
478         case STAT_CALL:
479             hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
480             break;
481         case STAT_DIM:
482             hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
483             break;
484         case STAT_IF:
485             hres = compile_if_statement(ctx, (if_statement_t*)stat);
486             break;
487         default:
488             FIXME("Unimplemented statement type %d\n", stat->type);
489             hres = E_NOTIMPL;
490         }
491
492         if(FAILED(hres))
493             return hres;
494         stat = stat->next;
495     }
496
497     return S_OK;
498 }
499
500 static void resolve_labels(compile_ctx_t *ctx)
501 {
502     instr_t *instr;
503
504     for(instr = ctx->code->instrs; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
505         if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
506             assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
507             instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
508         }
509         assert(instr_info[instr->op].arg2_type != ARG_ADDR);
510     }
511
512     ctx->labels_cnt = 0;
513 }
514
515 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
516 {
517     HRESULT hres;
518
519     func->code_off = ctx->instr_cnt;
520
521     hres = compile_statement(ctx, stat);
522     if(FAILED(hres))
523         return hres;
524
525     if(push_instr(ctx, OP_ret) == -1)
526         return E_OUTOFMEMORY;
527
528     resolve_labels(ctx);
529
530     if(ctx->dim_decls) {
531         dim_decl_t *dim_decl;
532         dynamic_var_t *new_var;
533
534         for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
535             new_var = compiler_alloc(ctx->code, sizeof(*new_var));
536             if(!new_var)
537                 return E_OUTOFMEMORY;
538
539             new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
540             if(!new_var->name)
541                 return E_OUTOFMEMORY;
542
543             V_VT(&new_var->v) = VT_EMPTY;
544
545             new_var->next = ctx->global_vars;
546             ctx->global_vars = new_var;
547         }
548     }
549
550     return S_OK;
551 }
552
553 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
554 {
555     dynamic_var_t *var;
556
557     for(var = script->global_vars; var; var = var->next) {
558         if(!strcmpiW(var->name, identifier))
559             return TRUE;
560     }
561
562     return FALSE;
563 }
564
565 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
566 {
567     dynamic_var_t *var;
568
569     for(var = ctx->global_vars; var; var = var->next) {
570         if(lookup_script_identifier(script, var->name)) {
571             FIXME("%s: redefined\n", debugstr_w(var->name));
572             return E_FAIL;
573         }
574     }
575
576     return S_OK;
577 }
578
579 void release_vbscode(vbscode_t *code)
580 {
581     unsigned i;
582
583     list_remove(&code->entry);
584
585     for(i=0; i < code->bstr_cnt; i++)
586         SysFreeString(code->bstr_pool[i]);
587
588     vbsheap_free(&code->heap);
589
590     heap_free(code->bstr_pool);
591     heap_free(code->source);
592     heap_free(code->instrs);
593     heap_free(code);
594 }
595
596 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
597 {
598     vbscode_t *ret;
599
600     ret = heap_alloc(sizeof(*ret));
601     if(!ret)
602         return NULL;
603
604     ret->source = heap_strdupW(source);
605     if(!ret->source) {
606         heap_free(ret);
607         return NULL;
608     }
609
610     ret->instrs = heap_alloc(32*sizeof(instr_t));
611     if(!ret->instrs) {
612         release_vbscode(ret);
613         return NULL;
614     }
615
616     ctx->instr_cnt = 0;
617     ctx->instr_size = 32;
618     vbsheap_init(&ret->heap);
619
620     ret->option_explicit = ctx->parser.option_explicit;
621
622     ret->bstr_pool = NULL;
623     ret->bstr_pool_size = 0;
624     ret->bstr_cnt = 0;
625
626     ret->global_code.code_ctx = ret;
627
628     list_init(&ret->entry);
629     return ret;
630 }
631
632 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
633 {
634     compile_ctx_t ctx;
635     HRESULT hres;
636
637     hres = parse_script(&ctx.parser, src);
638     if(FAILED(hres))
639         return hres;
640
641     ctx.code = alloc_vbscode(&ctx, src);
642     if(!ctx.code)
643         return E_OUTOFMEMORY;
644
645     ctx.global_vars = NULL;
646     ctx.dim_decls = NULL;
647     ctx.labels = NULL;
648     ctx.labels_cnt = ctx.labels_size = 0;
649
650     hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
651     if(FAILED(hres)) {
652         release_vbscode(ctx.code);
653         return hres;
654     }
655
656     hres = check_script_collisions(&ctx, script);
657     if(FAILED(hres)) {
658         release_vbscode(ctx.code);
659         return hres;
660     }
661
662     if(ctx.global_vars) {
663         dynamic_var_t *var;
664
665         for(var = ctx.global_vars; var->next; var = var->next);
666
667         var->next = script->global_vars;
668         script->global_vars = ctx.global_vars;
669     }
670
671     parser_release(&ctx.parser);
672
673     list_add_tail(&script->code_list, &ctx.code->entry);
674     *ret = ctx.code;
675     return S_OK;
676 }