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 dim_decl_t *dim_decls;
42 dynamic_var_t *global_vars;
46 function_decl_t *func_decls;
49 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
50 static HRESULT compile_statement(compile_ctx_t*,statement_t*);
54 instr_arg_type_t arg1_type;
55 instr_arg_type_t arg2_type;
57 #define X(n,a,b,c) {#n,b,c},
62 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
67 TRACE_(vbscript_disas)("\t%s", debugstr_w(arg->str));
70 TRACE_(vbscript_disas)("\t%d", arg->uint);
74 TRACE_(vbscript_disas)("\t%u", arg->uint);
77 TRACE_(vbscript_disas)("\t%lf", *arg->dbl);
86 static void dump_code(compile_ctx_t *ctx)
90 for(instr = ctx->code->instrs; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
91 TRACE_(vbscript_disas)("%d:\t%s", instr-ctx->code->instrs, instr_info[instr->op].op_str);
92 dump_instr_arg(instr_info[instr->op].arg1_type, &instr->arg1);
93 dump_instr_arg(instr_info[instr->op].arg2_type, &instr->arg2);
94 TRACE_(vbscript_disas)("\n");
98 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
100 return vbsheap_alloc(&vbscode->heap, size);
103 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
108 size = (strlenW(str)+1)*sizeof(WCHAR);
109 ret = compiler_alloc(vbscode, size);
111 memcpy(ret, str, size);
115 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
117 assert(id < ctx->instr_cnt);
118 return ctx->code->instrs + id;
121 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
123 assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
125 if(ctx->instr_size == ctx->instr_cnt) {
128 new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
132 ctx->code->instrs = new_instr;
133 ctx->instr_size *= 2;
136 ctx->code->instrs[ctx->instr_cnt].op = op;
137 return ctx->instr_cnt++;
140 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
144 ret = push_instr(ctx, op);
146 return E_OUTOFMEMORY;
148 instr_ptr(ctx, ret)->arg1.lng = arg;
152 static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
156 ret = push_instr(ctx, op);
158 return E_OUTOFMEMORY;
160 instr_ptr(ctx, ret)->arg1.uint = arg;
164 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
169 str = compiler_alloc_string(ctx->code, arg);
171 return E_OUTOFMEMORY;
173 instr = push_instr(ctx, op);
175 return E_OUTOFMEMORY;
177 instr_ptr(ctx, instr)->arg1.str = str;
181 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
186 d = compiler_alloc(ctx->code, sizeof(double));
188 return E_OUTOFMEMORY;
190 instr = push_instr(ctx, op);
192 return E_OUTOFMEMORY;
195 instr_ptr(ctx, instr)->arg1.dbl = d;
199 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
201 if(!ctx->code->bstr_pool_size) {
202 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
203 if(!ctx->code->bstr_pool)
205 ctx->code->bstr_pool_size = 8;
206 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
209 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
213 ctx->code->bstr_pool = new_pool;
214 ctx->code->bstr_pool_size *= 2;
217 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
218 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
221 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
224 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
229 bstr = alloc_bstr_arg(ctx, arg);
231 return E_OUTOFMEMORY;
233 instr = push_instr(ctx, op);
235 return E_OUTOFMEMORY;
237 instr_ptr(ctx, instr)->arg1.bstr = bstr;
241 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
246 bstr = alloc_bstr_arg(ctx, arg1);
248 return E_OUTOFMEMORY;
250 instr = push_instr(ctx, op);
252 return E_OUTOFMEMORY;
254 instr_ptr(ctx, instr)->arg1.bstr = bstr;
255 instr_ptr(ctx, instr)->arg2.uint = arg2;
259 #define LABEL_FLAG 0x80000000
261 static unsigned alloc_label(compile_ctx_t *ctx)
263 if(!ctx->labels_size) {
264 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
267 ctx->labels_size = 8;
268 }else if(ctx->labels_size == ctx->labels_cnt) {
269 unsigned *new_labels;
271 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
275 ctx->labels = new_labels;
276 ctx->labels_size *= 2;
279 return ctx->labels_cnt++ | LABEL_FLAG;
282 static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
284 assert(label & LABEL_FLAG);
285 ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
288 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
290 unsigned arg_cnt = 0;
294 hres = compile_expression(ctx, args);
306 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
308 unsigned arg_cnt = 0;
311 hres = compile_args(ctx, expr->args, &arg_cnt);
316 FIXME("obj_expr not implemented\n");
319 hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
325 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
329 hres = compile_expression(ctx, expr->subexpr);
333 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
336 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
340 hres = compile_expression(ctx, expr->left);
344 hres = compile_expression(ctx, expr->right);
348 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
351 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
355 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
357 return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
359 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
361 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
363 return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
365 return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
367 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
369 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
371 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
373 return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
375 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
377 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
379 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
381 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
383 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
385 return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
387 return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
389 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
391 return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
393 return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
395 FIXME("Unimplemented expression type %d\n", expr->type);
402 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
404 unsigned cnd_jmp, endif_label = -1;
405 elseif_decl_t *elseif_decl;
408 hres = compile_expression(ctx, stat->expr);
412 cnd_jmp = push_instr(ctx, OP_jmp_false);
414 return E_OUTOFMEMORY;
416 hres = compile_statement(ctx, stat->if_stat);
420 if(stat->else_stat || stat->elseifs) {
421 endif_label = alloc_label(ctx);
422 if(endif_label == -1)
423 return E_OUTOFMEMORY;
425 hres = push_instr_addr(ctx, OP_jmp, endif_label);
430 for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
431 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
433 hres = compile_expression(ctx, elseif_decl->expr);
437 cnd_jmp = push_instr(ctx, OP_jmp_false);
439 return E_OUTOFMEMORY;
441 hres = compile_statement(ctx, elseif_decl->stat);
445 hres = push_instr_addr(ctx, OP_jmp, endif_label);
450 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
452 if(stat->else_stat) {
453 hres = compile_statement(ctx, stat->else_stat);
458 if(endif_label != -1)
459 label_set_addr(ctx, endif_label);
463 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat)
467 hres = compile_expression(ctx, stat->value_expr);
471 if(stat->member_expr->args) {
472 FIXME("arguments support not implemented\n");
476 if(stat->member_expr->obj_expr) {
477 hres = compile_expression(ctx, stat->member_expr->obj_expr);
481 hres = push_instr_bstr(ctx, OP_assign_member, stat->member_expr->identifier);
483 hres = push_instr_bstr(ctx, OP_assign_ident, stat->member_expr->identifier);
489 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
491 dim_decl_t *dim_decl;
493 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
494 if(!strcmpiW(dim_decl->name, name))
501 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
503 dim_decl_t *dim_decl = stat->dim_decls;
506 if(lookup_dim_decls(ctx, dim_decl->name)) {
507 FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
513 dim_decl = dim_decl->next;
516 dim_decl->next = ctx->dim_decls;
517 ctx->dim_decls = stat->dim_decls;
521 static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
523 if(ctx->func != &ctx->code->global_code) {
524 FIXME("Function is not in the global code\n");
528 stat->func_decl->next = ctx->func_decls;
529 ctx->func_decls = stat->func_decl;
533 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
540 hres = compile_assign_statement(ctx, (assign_statement_t*)stat);
543 hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
546 hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
549 hres = compile_function_statement(ctx, (function_statement_t*)stat);
552 hres = compile_if_statement(ctx, (if_statement_t*)stat);
555 FIXME("Unimplemented statement type %d\n", stat->type);
567 static void resolve_labels(compile_ctx_t *ctx, unsigned off)
571 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
572 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
573 assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
574 instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
576 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
582 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
586 func->code_off = ctx->instr_cnt;
589 ctx->dim_decls = NULL;
590 hres = compile_statement(ctx, stat);
595 if(push_instr(ctx, OP_ret) == -1)
596 return E_OUTOFMEMORY;
598 resolve_labels(ctx, func->code_off);
601 dim_decl_t *dim_decl;
603 if(func->type == FUNC_GLOBAL) {
604 dynamic_var_t *new_var;
606 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
607 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
609 return E_OUTOFMEMORY;
611 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
613 return E_OUTOFMEMORY;
615 V_VT(&new_var->v) = VT_EMPTY;
617 new_var->next = ctx->global_vars;
618 ctx->global_vars = new_var;
621 FIXME("var not implemented for functions\n");
628 static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
632 for(iter = ctx->funcs; iter; iter = iter->next) {
633 if(!strcmpiW(iter->name, name))
640 static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
645 if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name)) {
646 FIXME("%s: redefinition\n", debugstr_w(decl->name));
650 func = compiler_alloc(ctx->code, sizeof(*func));
652 return E_OUTOFMEMORY;
654 func->name = compiler_alloc_string(ctx->code, decl->name);
656 return E_OUTOFMEMORY;
658 func->code_ctx = ctx->code;
659 func->type = decl->type;
666 for(arg = decl->args; arg; arg = arg->next)
669 func->args = compiler_alloc(ctx->code, func->arg_cnt * sizeof(arg_desc_t));
671 return E_OUTOFMEMORY;
673 for(i = 0, arg = decl->args; arg; arg = arg->next, i++) {
674 func->args[i].name = compiler_alloc_string(ctx->code, arg->name);
675 if(!func->args[i].name)
676 return E_OUTOFMEMORY;
677 func->args[i].by_ref = arg->by_ref;
683 hres = compile_func(ctx, decl->body, func);
691 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
696 for(var = script->global_vars; var; var = var->next) {
697 if(!strcmpiW(var->name, identifier))
701 for(func = script->global_funcs; func; func = func->next) {
702 if(!strcmpiW(func->name, identifier))
709 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
714 for(var = ctx->global_vars; var; var = var->next) {
715 if(lookup_script_identifier(script, var->name)) {
716 FIXME("%s: redefined\n", debugstr_w(var->name));
721 for(func = ctx->funcs; func; func = func->next) {
722 if(lookup_script_identifier(script, func->name)) {
723 FIXME("%s: redefined\n", debugstr_w(func->name));
731 void release_vbscode(vbscode_t *code)
735 list_remove(&code->entry);
737 for(i=0; i < code->bstr_cnt; i++)
738 SysFreeString(code->bstr_pool[i]);
740 vbsheap_free(&code->heap);
742 heap_free(code->bstr_pool);
743 heap_free(code->source);
744 heap_free(code->instrs);
748 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
752 ret = heap_alloc(sizeof(*ret));
756 ret->source = heap_strdupW(source);
762 ret->instrs = heap_alloc(32*sizeof(instr_t));
764 release_vbscode(ret);
769 ctx->instr_size = 32;
770 vbsheap_init(&ret->heap);
772 ret->option_explicit = ctx->parser.option_explicit;
774 ret->bstr_pool = NULL;
775 ret->bstr_pool_size = 0;
778 ret->global_code.type = FUNC_GLOBAL;
779 ret->global_code.name = NULL;
780 ret->global_code.code_ctx = ret;
781 ret->global_code.arg_cnt = 0;
782 ret->global_code.args = NULL;
784 list_init(&ret->entry);
788 static void release_compiler(compile_ctx_t *ctx)
790 parser_release(&ctx->parser);
791 heap_free(ctx->labels);
793 release_vbscode(ctx->code);
796 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
798 function_t *new_func;
799 function_decl_t *func_decl;
804 hres = parse_script(&ctx.parser, src);
808 code = ctx.code = alloc_vbscode(&ctx, src);
810 return E_OUTOFMEMORY;
813 ctx.func_decls = NULL;
814 ctx.global_vars = NULL;
815 ctx.dim_decls = NULL;
817 ctx.labels_cnt = ctx.labels_size = 0;
819 hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
821 release_compiler(&ctx);
825 for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
826 hres = create_function(&ctx, func_decl, &new_func);
828 release_compiler(&ctx);
832 new_func->next = ctx.funcs;
833 ctx.funcs = new_func;
836 hres = check_script_collisions(&ctx, script);
838 release_compiler(&ctx);
842 if(ctx.global_vars) {
845 for(var = ctx.global_vars; var->next; var = var->next);
847 var->next = script->global_vars;
848 script->global_vars = ctx.global_vars;
852 for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);
854 new_func->next = script->global_funcs;
855 script->global_funcs = ctx.funcs;
858 if(TRACE_ON(vbscript_disas))
862 release_compiler(&ctx);
864 list_add_tail(&script->code_list, &code->entry);