vbscript: Added interp_idiv 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_DOUBLE:
357         return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
358     case EXPR_EMPTY:
359         return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
360     case EXPR_EQUAL:
361         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
362     case EXPR_IDIV:
363         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
364     case EXPR_MEMBER:
365         return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
366     case EXPR_MOD:
367         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
368     case EXPR_NEG:
369         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
370     case EXPR_NEQUAL:
371         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
372     case EXPR_NOT:
373         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
374     case EXPR_NULL:
375         return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
376     case EXPR_STRING:
377         return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
378     case EXPR_SUB:
379         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
380     case EXPR_USHORT:
381         return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
382     case EXPR_ULONG:
383         return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
384     default:
385         FIXME("Unimplemented expression type %d\n", expr->type);
386         return E_NOTIMPL;
387     }
388
389     return S_OK;
390 }
391
392 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
393 {
394     unsigned cnd_jmp, endif_label = -1;
395     elseif_decl_t *elseif_decl;
396     HRESULT hres;
397
398     hres = compile_expression(ctx, stat->expr);
399     if(FAILED(hres))
400         return hres;
401
402     cnd_jmp = push_instr(ctx, OP_jmp_false);
403     if(cnd_jmp == -1)
404         return E_OUTOFMEMORY;
405
406     hres = compile_statement(ctx, stat->if_stat);
407     if(FAILED(hres))
408         return hres;
409
410     if(stat->else_stat || stat->elseifs) {
411         endif_label = alloc_label(ctx);
412         if(endif_label == -1)
413             return E_OUTOFMEMORY;
414
415         hres = push_instr_addr(ctx, OP_jmp, endif_label);
416         if(FAILED(hres))
417             return hres;
418     }
419
420     for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
421         instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
422
423         hres = compile_expression(ctx, elseif_decl->expr);
424         if(FAILED(hres))
425             return hres;
426
427         cnd_jmp = push_instr(ctx, OP_jmp_false);
428         if(cnd_jmp == -1)
429             return E_OUTOFMEMORY;
430
431         hres = compile_statement(ctx, elseif_decl->stat);
432         if(FAILED(hres))
433             return hres;
434
435         hres = push_instr_addr(ctx, OP_jmp, endif_label);
436         if(FAILED(hres))
437             return hres;
438     }
439
440     instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
441
442     if(stat->else_stat) {
443         hres = compile_statement(ctx, stat->else_stat);
444         if(FAILED(hres))
445             return hres;
446     }
447
448     if(endif_label != -1)
449         label_set_addr(ctx, endif_label);
450     return S_OK;
451 }
452
453 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat)
454 {
455     HRESULT hres;
456
457     hres = compile_expression(ctx, stat->value_expr);
458     if(FAILED(hres))
459         return hres;
460
461     if(stat->member_expr->args) {
462         FIXME("arguments support not implemented\n");
463         return E_NOTIMPL;
464     }
465
466     if(stat->member_expr->obj_expr) {
467         hres = compile_expression(ctx, stat->member_expr->obj_expr);
468         if(FAILED(hres))
469             return hres;
470
471         hres = push_instr_bstr(ctx, OP_assign_member, stat->member_expr->identifier);
472     }else {
473         hres = push_instr_bstr(ctx, OP_assign_ident, stat->member_expr->identifier);
474     }
475
476     return hres;
477 }
478
479 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
480 {
481     dim_decl_t *dim_decl;
482
483     for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
484         if(!strcmpiW(dim_decl->name, name))
485             return TRUE;
486     }
487
488     return FALSE;
489 }
490
491 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
492 {
493     dim_decl_t *dim_decl = stat->dim_decls;
494
495     while(1) {
496         if(lookup_dim_decls(ctx, dim_decl->name)) {
497             FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
498             return E_FAIL;
499         }
500
501         if(!dim_decl->next)
502             break;
503         dim_decl = dim_decl->next;
504     }
505
506     dim_decl->next = ctx->dim_decls;
507     ctx->dim_decls = stat->dim_decls;
508     return S_OK;
509 }
510
511 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
512 {
513     HRESULT hres;
514
515     while(stat) {
516         switch(stat->type) {
517         case STAT_ASSIGN:
518             hres = compile_assign_statement(ctx, (assign_statement_t*)stat);
519             break;
520         case STAT_CALL:
521             hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
522             break;
523         case STAT_DIM:
524             hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
525             break;
526         case STAT_IF:
527             hres = compile_if_statement(ctx, (if_statement_t*)stat);
528             break;
529         default:
530             FIXME("Unimplemented statement type %d\n", stat->type);
531             hres = E_NOTIMPL;
532         }
533
534         if(FAILED(hres))
535             return hres;
536         stat = stat->next;
537     }
538
539     return S_OK;
540 }
541
542 static void resolve_labels(compile_ctx_t *ctx)
543 {
544     instr_t *instr;
545
546     for(instr = ctx->code->instrs; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
547         if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
548             assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
549             instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
550         }
551         assert(instr_info[instr->op].arg2_type != ARG_ADDR);
552     }
553
554     ctx->labels_cnt = 0;
555 }
556
557 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
558 {
559     HRESULT hres;
560
561     func->code_off = ctx->instr_cnt;
562
563     hres = compile_statement(ctx, stat);
564     if(FAILED(hres))
565         return hres;
566
567     if(push_instr(ctx, OP_ret) == -1)
568         return E_OUTOFMEMORY;
569
570     resolve_labels(ctx);
571
572     if(ctx->dim_decls) {
573         dim_decl_t *dim_decl;
574         dynamic_var_t *new_var;
575
576         for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
577             new_var = compiler_alloc(ctx->code, sizeof(*new_var));
578             if(!new_var)
579                 return E_OUTOFMEMORY;
580
581             new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
582             if(!new_var->name)
583                 return E_OUTOFMEMORY;
584
585             V_VT(&new_var->v) = VT_EMPTY;
586
587             new_var->next = ctx->global_vars;
588             ctx->global_vars = new_var;
589         }
590     }
591
592     return S_OK;
593 }
594
595 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
596 {
597     dynamic_var_t *var;
598
599     for(var = script->global_vars; var; var = var->next) {
600         if(!strcmpiW(var->name, identifier))
601             return TRUE;
602     }
603
604     return FALSE;
605 }
606
607 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
608 {
609     dynamic_var_t *var;
610
611     for(var = ctx->global_vars; var; var = var->next) {
612         if(lookup_script_identifier(script, var->name)) {
613             FIXME("%s: redefined\n", debugstr_w(var->name));
614             return E_FAIL;
615         }
616     }
617
618     return S_OK;
619 }
620
621 void release_vbscode(vbscode_t *code)
622 {
623     unsigned i;
624
625     list_remove(&code->entry);
626
627     for(i=0; i < code->bstr_cnt; i++)
628         SysFreeString(code->bstr_pool[i]);
629
630     vbsheap_free(&code->heap);
631
632     heap_free(code->bstr_pool);
633     heap_free(code->source);
634     heap_free(code->instrs);
635     heap_free(code);
636 }
637
638 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
639 {
640     vbscode_t *ret;
641
642     ret = heap_alloc(sizeof(*ret));
643     if(!ret)
644         return NULL;
645
646     ret->source = heap_strdupW(source);
647     if(!ret->source) {
648         heap_free(ret);
649         return NULL;
650     }
651
652     ret->instrs = heap_alloc(32*sizeof(instr_t));
653     if(!ret->instrs) {
654         release_vbscode(ret);
655         return NULL;
656     }
657
658     ctx->instr_cnt = 0;
659     ctx->instr_size = 32;
660     vbsheap_init(&ret->heap);
661
662     ret->option_explicit = ctx->parser.option_explicit;
663
664     ret->bstr_pool = NULL;
665     ret->bstr_pool_size = 0;
666     ret->bstr_cnt = 0;
667
668     ret->global_code.code_ctx = ret;
669
670     list_init(&ret->entry);
671     return ret;
672 }
673
674 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
675 {
676     compile_ctx_t ctx;
677     HRESULT hres;
678
679     hres = parse_script(&ctx.parser, src);
680     if(FAILED(hres))
681         return hres;
682
683     ctx.code = alloc_vbscode(&ctx, src);
684     if(!ctx.code)
685         return E_OUTOFMEMORY;
686
687     ctx.global_vars = NULL;
688     ctx.dim_decls = NULL;
689     ctx.labels = NULL;
690     ctx.labels_cnt = ctx.labels_size = 0;
691
692     hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
693     if(FAILED(hres)) {
694         release_vbscode(ctx.code);
695         return hres;
696     }
697
698     hres = check_script_collisions(&ctx, script);
699     if(FAILED(hres)) {
700         release_vbscode(ctx.code);
701         return hres;
702     }
703
704     if(ctx.global_vars) {
705         dynamic_var_t *var;
706
707         for(var = ctx.global_vars; var->next; var = var->next);
708
709         var->next = script->global_vars;
710         script->global_vars = ctx.global_vars;
711     }
712
713     parser_release(&ctx.parser);
714
715     if(TRACE_ON(vbscript_disas))
716         dump_code(&ctx);
717
718     list_add_tail(&script->code_list, &ctx.code->entry);
719     *ret = ctx.code;
720     return S_OK;
721 }