2 * Copyright 2011 Jacek Caban for CodeWeavers
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.
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.
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
23 #include "parser.tab.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
28 WINE_DECLARE_DEBUG_CHANNEL(vbscript_disas);
41 unsigned sub_end_label;
43 dim_decl_t *dim_decls;
44 dynamic_var_t *global_vars;
48 function_decl_t *func_decls;
51 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
52 static HRESULT compile_statement(compile_ctx_t*,statement_t*);
56 instr_arg_type_t arg1_type;
57 instr_arg_type_t arg2_type;
59 #define X(n,a,b,c) {#n,b,c},
64 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
69 TRACE_(vbscript_disas)("\t%s", debugstr_w(arg->str));
72 TRACE_(vbscript_disas)("\t%d", arg->uint);
76 TRACE_(vbscript_disas)("\t%u", arg->uint);
79 TRACE_(vbscript_disas)("\t%lf", *arg->dbl);
88 static void dump_code(compile_ctx_t *ctx)
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");
100 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
102 return vbsheap_alloc(&vbscode->heap, size);
105 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
110 size = (strlenW(str)+1)*sizeof(WCHAR);
111 ret = compiler_alloc(vbscode, size);
113 memcpy(ret, str, size);
117 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
119 assert(id < ctx->instr_cnt);
120 return ctx->code->instrs + id;
123 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
125 assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
127 if(ctx->instr_size == ctx->instr_cnt) {
130 new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
134 ctx->code->instrs = new_instr;
135 ctx->instr_size *= 2;
138 ctx->code->instrs[ctx->instr_cnt].op = op;
139 return ctx->instr_cnt++;
142 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
146 ret = push_instr(ctx, op);
148 return E_OUTOFMEMORY;
150 instr_ptr(ctx, ret)->arg1.lng = arg;
154 static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
158 ret = push_instr(ctx, op);
160 return E_OUTOFMEMORY;
162 instr_ptr(ctx, ret)->arg1.uint = arg;
166 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
171 str = compiler_alloc_string(ctx->code, arg);
173 return E_OUTOFMEMORY;
175 instr = push_instr(ctx, op);
177 return E_OUTOFMEMORY;
179 instr_ptr(ctx, instr)->arg1.str = str;
183 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
188 d = compiler_alloc(ctx->code, sizeof(double));
190 return E_OUTOFMEMORY;
192 instr = push_instr(ctx, op);
194 return E_OUTOFMEMORY;
197 instr_ptr(ctx, instr)->arg1.dbl = d;
201 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
203 if(!ctx->code->bstr_pool_size) {
204 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
205 if(!ctx->code->bstr_pool)
207 ctx->code->bstr_pool_size = 8;
208 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
211 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
215 ctx->code->bstr_pool = new_pool;
216 ctx->code->bstr_pool_size *= 2;
219 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
220 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
223 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
226 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
231 bstr = alloc_bstr_arg(ctx, arg);
233 return E_OUTOFMEMORY;
235 instr = push_instr(ctx, op);
237 return E_OUTOFMEMORY;
239 instr_ptr(ctx, instr)->arg1.bstr = bstr;
243 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
248 bstr = alloc_bstr_arg(ctx, arg1);
250 return E_OUTOFMEMORY;
252 instr = push_instr(ctx, op);
254 return E_OUTOFMEMORY;
256 instr_ptr(ctx, instr)->arg1.bstr = bstr;
257 instr_ptr(ctx, instr)->arg2.uint = arg2;
261 #define LABEL_FLAG 0x80000000
263 static unsigned alloc_label(compile_ctx_t *ctx)
265 if(!ctx->labels_size) {
266 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
269 ctx->labels_size = 8;
270 }else if(ctx->labels_size == ctx->labels_cnt) {
271 unsigned *new_labels;
273 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
277 ctx->labels = new_labels;
278 ctx->labels_size *= 2;
281 return ctx->labels_cnt++ | LABEL_FLAG;
284 static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
286 assert(label & LABEL_FLAG);
287 ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
290 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
292 unsigned arg_cnt = 0;
296 hres = compile_expression(ctx, args);
308 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
310 unsigned arg_cnt = 0;
313 hres = compile_args(ctx, expr->args, &arg_cnt);
318 FIXME("obj_expr not implemented\n");
321 hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
327 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
331 hres = compile_expression(ctx, expr->subexpr);
335 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
338 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
342 hres = compile_expression(ctx, expr->left);
346 hres = compile_expression(ctx, expr->right);
350 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
353 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
357 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
359 return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
361 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
363 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
365 return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
367 return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
369 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
371 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
373 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
375 return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
377 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
379 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
381 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
383 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
385 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
387 return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
389 return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
391 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
393 return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
395 return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
397 FIXME("Unimplemented expression type %d\n", expr->type);
404 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
406 unsigned cnd_jmp, endif_label = -1;
407 elseif_decl_t *elseif_decl;
410 hres = compile_expression(ctx, stat->expr);
414 cnd_jmp = push_instr(ctx, OP_jmp_false);
416 return E_OUTOFMEMORY;
418 hres = compile_statement(ctx, stat->if_stat);
422 if(stat->else_stat || stat->elseifs) {
423 endif_label = alloc_label(ctx);
424 if(endif_label == -1)
425 return E_OUTOFMEMORY;
427 hres = push_instr_addr(ctx, OP_jmp, endif_label);
432 for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
433 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
435 hres = compile_expression(ctx, elseif_decl->expr);
439 cnd_jmp = push_instr(ctx, OP_jmp_false);
441 return E_OUTOFMEMORY;
443 hres = compile_statement(ctx, elseif_decl->stat);
447 hres = push_instr_addr(ctx, OP_jmp, endif_label);
452 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
454 if(stat->else_stat) {
455 hres = compile_statement(ctx, stat->else_stat);
460 if(endif_label != -1)
461 label_set_addr(ctx, endif_label);
465 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat)
469 hres = compile_expression(ctx, stat->value_expr);
473 if(stat->member_expr->args) {
474 FIXME("arguments support not implemented\n");
478 if(stat->member_expr->obj_expr) {
479 hres = compile_expression(ctx, stat->member_expr->obj_expr);
483 hres = push_instr_bstr(ctx, OP_assign_member, stat->member_expr->identifier);
485 hres = push_instr_bstr(ctx, OP_assign_ident, stat->member_expr->identifier);
491 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
493 dim_decl_t *dim_decl;
495 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
496 if(!strcmpiW(dim_decl->name, name))
503 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
505 dim_decl_t *dim_decl = stat->dim_decls;
508 if(lookup_dim_decls(ctx, dim_decl->name)) {
509 FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
515 dim_decl = dim_decl->next;
518 dim_decl->next = ctx->dim_decls;
519 ctx->dim_decls = stat->dim_decls;
523 static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
525 if(ctx->func != &ctx->code->global_code) {
526 FIXME("Function is not in the global code\n");
530 stat->func_decl->next = ctx->func_decls;
531 ctx->func_decls = stat->func_decl;
535 static HRESULT compile_exitsub_statement(compile_ctx_t *ctx)
537 if(ctx->sub_end_label == -1) {
538 FIXME("Exit Sub outside Sub?\n");
542 return push_instr_addr(ctx, OP_jmp, ctx->sub_end_label);
545 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
552 hres = compile_assign_statement(ctx, (assign_statement_t*)stat);
555 hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
558 hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
561 hres = compile_exitsub_statement(ctx);
564 hres = compile_function_statement(ctx, (function_statement_t*)stat);
567 hres = compile_if_statement(ctx, (if_statement_t*)stat);
570 FIXME("Unimplemented statement type %d\n", stat->type);
582 static void resolve_labels(compile_ctx_t *ctx, unsigned off)
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];
591 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
597 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
601 func->code_off = ctx->instr_cnt;
603 ctx->sub_end_label = -1;
607 ctx->sub_end_label = alloc_label(ctx);
608 if(ctx->sub_end_label == -1)
609 return E_OUTOFMEMORY;
616 ctx->dim_decls = NULL;
617 hres = compile_statement(ctx, stat);
622 if(ctx->sub_end_label != -1)
623 label_set_addr(ctx, ctx->sub_end_label);
625 if(push_instr(ctx, OP_ret) == -1)
626 return E_OUTOFMEMORY;
628 resolve_labels(ctx, func->code_off);
631 dim_decl_t *dim_decl;
633 if(func->type == FUNC_GLOBAL) {
634 dynamic_var_t *new_var;
636 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
637 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
639 return E_OUTOFMEMORY;
641 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
643 return E_OUTOFMEMORY;
645 V_VT(&new_var->v) = VT_EMPTY;
647 new_var->next = ctx->global_vars;
648 ctx->global_vars = new_var;
651 FIXME("var not implemented for functions\n");
658 static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
662 for(iter = ctx->funcs; iter; iter = iter->next) {
663 if(!strcmpiW(iter->name, name))
670 static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
675 if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name)) {
676 FIXME("%s: redefinition\n", debugstr_w(decl->name));
680 func = compiler_alloc(ctx->code, sizeof(*func));
682 return E_OUTOFMEMORY;
684 func->name = compiler_alloc_string(ctx->code, decl->name);
686 return E_OUTOFMEMORY;
688 func->code_ctx = ctx->code;
689 func->type = decl->type;
696 for(arg = decl->args; arg; arg = arg->next)
699 func->args = compiler_alloc(ctx->code, func->arg_cnt * sizeof(arg_desc_t));
701 return E_OUTOFMEMORY;
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;
713 hres = compile_func(ctx, decl->body, func);
721 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
726 for(var = script->global_vars; var; var = var->next) {
727 if(!strcmpiW(var->name, identifier))
731 for(func = script->global_funcs; func; func = func->next) {
732 if(!strcmpiW(func->name, identifier))
739 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
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));
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));
761 void release_vbscode(vbscode_t *code)
765 list_remove(&code->entry);
767 for(i=0; i < code->bstr_cnt; i++)
768 SysFreeString(code->bstr_pool[i]);
770 vbsheap_free(&code->heap);
772 heap_free(code->bstr_pool);
773 heap_free(code->source);
774 heap_free(code->instrs);
778 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
782 ret = heap_alloc(sizeof(*ret));
786 ret->source = heap_strdupW(source);
792 ret->instrs = heap_alloc(32*sizeof(instr_t));
794 release_vbscode(ret);
799 ctx->instr_size = 32;
800 vbsheap_init(&ret->heap);
802 ret->option_explicit = ctx->parser.option_explicit;
804 ret->bstr_pool = NULL;
805 ret->bstr_pool_size = 0;
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;
814 list_init(&ret->entry);
818 static void release_compiler(compile_ctx_t *ctx)
820 parser_release(&ctx->parser);
821 heap_free(ctx->labels);
823 release_vbscode(ctx->code);
826 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
828 function_t *new_func;
829 function_decl_t *func_decl;
834 hres = parse_script(&ctx.parser, src);
838 code = ctx.code = alloc_vbscode(&ctx, src);
840 return E_OUTOFMEMORY;
843 ctx.func_decls = NULL;
844 ctx.global_vars = NULL;
845 ctx.dim_decls = NULL;
847 ctx.labels_cnt = ctx.labels_size = 0;
849 hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
851 release_compiler(&ctx);
855 for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
856 hres = create_function(&ctx, func_decl, &new_func);
858 release_compiler(&ctx);
862 new_func->next = ctx.funcs;
863 ctx.funcs = new_func;
866 hres = check_script_collisions(&ctx, script);
868 release_compiler(&ctx);
872 if(ctx.global_vars) {
875 for(var = ctx.global_vars; var->next; var = var->next);
877 var->next = script->global_vars;
878 script->global_vars = ctx.global_vars;
882 for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);
884 new_func->next = script->global_funcs;
885 script->global_funcs = ctx.funcs;
888 if(TRACE_ON(vbscript_disas))
892 release_compiler(&ctx);
894 list_add_tail(&script->code_list, &code->entry);