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