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;
42 unsigned func_end_label;
44 dim_decl_t *dim_decls;
45 dynamic_var_t *global_vars;
49 function_decl_t *func_decls;
52 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
53 static HRESULT compile_statement(compile_ctx_t*,statement_t*);
57 instr_arg_type_t arg1_type;
58 instr_arg_type_t arg2_type;
60 #define X(n,a,b,c) {#n,b,c},
65 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
70 TRACE_(vbscript_disas)("\t%s", debugstr_w(arg->str));
73 TRACE_(vbscript_disas)("\t%d", arg->uint);
77 TRACE_(vbscript_disas)("\t%u", arg->uint);
80 TRACE_(vbscript_disas)("\t%lf", *arg->dbl);
89 static void dump_code(compile_ctx_t *ctx)
93 for(instr = ctx->code->instrs; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
94 TRACE_(vbscript_disas)("%d:\t%s", instr-ctx->code->instrs, instr_info[instr->op].op_str);
95 dump_instr_arg(instr_info[instr->op].arg1_type, &instr->arg1);
96 dump_instr_arg(instr_info[instr->op].arg2_type, &instr->arg2);
97 TRACE_(vbscript_disas)("\n");
101 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
103 return vbsheap_alloc(&vbscode->heap, size);
106 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
111 size = (strlenW(str)+1)*sizeof(WCHAR);
112 ret = compiler_alloc(vbscode, size);
114 memcpy(ret, str, size);
118 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
120 assert(id < ctx->instr_cnt);
121 return ctx->code->instrs + id;
124 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
126 assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
128 if(ctx->instr_size == ctx->instr_cnt) {
131 new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
135 ctx->code->instrs = new_instr;
136 ctx->instr_size *= 2;
139 ctx->code->instrs[ctx->instr_cnt].op = op;
140 return ctx->instr_cnt++;
143 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
147 ret = push_instr(ctx, op);
149 return E_OUTOFMEMORY;
151 instr_ptr(ctx, ret)->arg1.lng = arg;
155 static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
159 ret = push_instr(ctx, op);
161 return E_OUTOFMEMORY;
163 instr_ptr(ctx, ret)->arg1.uint = arg;
167 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
172 str = compiler_alloc_string(ctx->code, arg);
174 return E_OUTOFMEMORY;
176 instr = push_instr(ctx, op);
178 return E_OUTOFMEMORY;
180 instr_ptr(ctx, instr)->arg1.str = str;
184 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
189 d = compiler_alloc(ctx->code, sizeof(double));
191 return E_OUTOFMEMORY;
193 instr = push_instr(ctx, op);
195 return E_OUTOFMEMORY;
198 instr_ptr(ctx, instr)->arg1.dbl = d;
202 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
204 if(!ctx->code->bstr_pool_size) {
205 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
206 if(!ctx->code->bstr_pool)
208 ctx->code->bstr_pool_size = 8;
209 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
212 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
216 ctx->code->bstr_pool = new_pool;
217 ctx->code->bstr_pool_size *= 2;
220 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
221 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
224 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
227 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
232 bstr = alloc_bstr_arg(ctx, arg);
234 return E_OUTOFMEMORY;
236 instr = push_instr(ctx, op);
238 return E_OUTOFMEMORY;
240 instr_ptr(ctx, instr)->arg1.bstr = bstr;
244 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
249 bstr = alloc_bstr_arg(ctx, arg1);
251 return E_OUTOFMEMORY;
253 instr = push_instr(ctx, op);
255 return E_OUTOFMEMORY;
257 instr_ptr(ctx, instr)->arg1.bstr = bstr;
258 instr_ptr(ctx, instr)->arg2.uint = arg2;
262 #define LABEL_FLAG 0x80000000
264 static unsigned alloc_label(compile_ctx_t *ctx)
266 if(!ctx->labels_size) {
267 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
270 ctx->labels_size = 8;
271 }else if(ctx->labels_size == ctx->labels_cnt) {
272 unsigned *new_labels;
274 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
278 ctx->labels = new_labels;
279 ctx->labels_size *= 2;
282 return ctx->labels_cnt++ | LABEL_FLAG;
285 static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
287 assert(label & LABEL_FLAG);
288 ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
291 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
293 unsigned arg_cnt = 0;
297 hres = compile_expression(ctx, args);
309 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
311 unsigned arg_cnt = 0;
314 hres = compile_args(ctx, expr->args, &arg_cnt);
319 FIXME("obj_expr not implemented\n");
322 hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
328 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
332 hres = compile_expression(ctx, expr->subexpr);
336 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
339 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
343 hres = compile_expression(ctx, expr->left);
347 hres = compile_expression(ctx, expr->right);
351 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
354 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
358 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
360 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
362 return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
364 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
366 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
368 return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
370 return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
372 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
374 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eqv);
376 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
378 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
380 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_imp);
382 return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
384 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
386 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
388 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
390 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
392 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
394 return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
396 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
398 return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
400 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
402 return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
404 return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
406 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
408 FIXME("Unimplemented expression type %d\n", expr->type);
415 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
417 unsigned cnd_jmp, endif_label = -1;
418 elseif_decl_t *elseif_decl;
421 hres = compile_expression(ctx, stat->expr);
425 cnd_jmp = push_instr(ctx, OP_jmp_false);
427 return E_OUTOFMEMORY;
429 hres = compile_statement(ctx, stat->if_stat);
433 if(stat->else_stat || stat->elseifs) {
434 endif_label = alloc_label(ctx);
435 if(endif_label == -1)
436 return E_OUTOFMEMORY;
438 hres = push_instr_addr(ctx, OP_jmp, endif_label);
443 for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
444 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
446 hres = compile_expression(ctx, elseif_decl->expr);
450 cnd_jmp = push_instr(ctx, OP_jmp_false);
452 return E_OUTOFMEMORY;
454 hres = compile_statement(ctx, elseif_decl->stat);
458 hres = push_instr_addr(ctx, OP_jmp, endif_label);
463 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
465 if(stat->else_stat) {
466 hres = compile_statement(ctx, stat->else_stat);
471 if(endif_label != -1)
472 label_set_addr(ctx, endif_label);
476 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat)
480 hres = compile_expression(ctx, stat->value_expr);
484 if(stat->member_expr->args) {
485 FIXME("arguments support not implemented\n");
489 if(stat->member_expr->obj_expr) {
490 hres = compile_expression(ctx, stat->member_expr->obj_expr);
494 hres = push_instr_bstr(ctx, OP_assign_member, stat->member_expr->identifier);
496 hres = push_instr_bstr(ctx, OP_assign_ident, stat->member_expr->identifier);
502 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
504 dim_decl_t *dim_decl;
506 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
507 if(!strcmpiW(dim_decl->name, name))
514 static BOOL lookup_args_name(compile_ctx_t *ctx, const WCHAR *name)
518 for(i = 0; i < ctx->func->arg_cnt; i++) {
519 if(!strcmpiW(ctx->func->args[i].name, name))
526 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
528 dim_decl_t *dim_decl = stat->dim_decls;
531 if(lookup_dim_decls(ctx, dim_decl->name) || lookup_args_name(ctx, dim_decl->name)) {
532 FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
538 dim_decl = dim_decl->next;
541 dim_decl->next = ctx->dim_decls;
542 ctx->dim_decls = stat->dim_decls;
543 ctx->func->var_cnt++;
547 static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
549 if(ctx->func != &ctx->code->global_code) {
550 FIXME("Function is not in the global code\n");
554 stat->func_decl->next = ctx->func_decls;
555 ctx->func_decls = stat->func_decl;
559 static HRESULT compile_exitsub_statement(compile_ctx_t *ctx)
561 if(ctx->sub_end_label == -1) {
562 FIXME("Exit Sub outside Sub?\n");
566 return push_instr_addr(ctx, OP_jmp, ctx->sub_end_label);
569 static HRESULT compile_exitfunc_statement(compile_ctx_t *ctx)
571 if(ctx->func_end_label == -1) {
572 FIXME("Exit Function outside Function?\n");
576 return push_instr_addr(ctx, OP_jmp, ctx->func_end_label);
579 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
586 hres = compile_assign_statement(ctx, (assign_statement_t*)stat);
589 hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
592 hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
595 hres = compile_exitfunc_statement(ctx);
598 hres = compile_exitsub_statement(ctx);
601 hres = compile_function_statement(ctx, (function_statement_t*)stat);
604 hres = compile_if_statement(ctx, (if_statement_t*)stat);
607 FIXME("Unimplemented statement type %d\n", stat->type);
619 static void resolve_labels(compile_ctx_t *ctx, unsigned off)
623 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
624 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
625 assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
626 instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
628 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
634 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
638 func->code_off = ctx->instr_cnt;
640 ctx->sub_end_label = -1;
641 ctx->func_end_label = -1;
645 ctx->func_end_label = alloc_label(ctx);
646 if(ctx->func_end_label == -1)
647 return E_OUTOFMEMORY; /* FIXME ! */
650 ctx->sub_end_label = alloc_label(ctx);
651 if(ctx->sub_end_label == -1)
652 return E_OUTOFMEMORY;
659 ctx->dim_decls = NULL;
660 hres = compile_statement(ctx, stat);
665 if(ctx->sub_end_label != -1)
666 label_set_addr(ctx, ctx->sub_end_label);
667 if(ctx->func_end_label != -1)
668 label_set_addr(ctx, ctx->func_end_label);
670 if(push_instr(ctx, OP_ret) == -1)
671 return E_OUTOFMEMORY;
673 resolve_labels(ctx, func->code_off);
676 dim_decl_t *dim_decl;
678 if(func->type == FUNC_GLOBAL) {
679 dynamic_var_t *new_var;
683 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
684 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
686 return E_OUTOFMEMORY;
688 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
690 return E_OUTOFMEMORY;
692 V_VT(&new_var->v) = VT_EMPTY;
694 new_var->next = ctx->global_vars;
695 ctx->global_vars = new_var;
700 func->vars = compiler_alloc(ctx->code, func->var_cnt * sizeof(var_desc_t));
702 return E_OUTOFMEMORY;
704 for(dim_decl = ctx->dim_decls, i=0; dim_decl; dim_decl = dim_decl->next, i++) {
705 func->vars[i].name = compiler_alloc_string(ctx->code, dim_decl->name);
706 if(!func->vars[i].name)
707 return E_OUTOFMEMORY;
710 assert(i == func->var_cnt);
717 static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
721 for(iter = ctx->funcs; iter; iter = iter->next) {
722 if(!strcmpiW(iter->name, name))
729 static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
734 if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name)) {
735 FIXME("%s: redefinition\n", debugstr_w(decl->name));
739 func = compiler_alloc(ctx->code, sizeof(*func));
741 return E_OUTOFMEMORY;
743 func->name = compiler_alloc_string(ctx->code, decl->name);
745 return E_OUTOFMEMORY;
749 func->code_ctx = ctx->code;
750 func->type = decl->type;
757 for(arg = decl->args; arg; arg = arg->next)
760 func->args = compiler_alloc(ctx->code, func->arg_cnt * sizeof(arg_desc_t));
762 return E_OUTOFMEMORY;
764 for(i = 0, arg = decl->args; arg; arg = arg->next, i++) {
765 func->args[i].name = compiler_alloc_string(ctx->code, arg->name);
766 if(!func->args[i].name)
767 return E_OUTOFMEMORY;
768 func->args[i].by_ref = arg->by_ref;
774 hres = compile_func(ctx, decl->body, func);
782 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
787 for(var = script->global_vars; var; var = var->next) {
788 if(!strcmpiW(var->name, identifier))
792 for(func = script->global_funcs; func; func = func->next) {
793 if(!strcmpiW(func->name, identifier))
800 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
805 for(var = ctx->global_vars; var; var = var->next) {
806 if(lookup_script_identifier(script, var->name)) {
807 FIXME("%s: redefined\n", debugstr_w(var->name));
812 for(func = ctx->funcs; func; func = func->next) {
813 if(lookup_script_identifier(script, func->name)) {
814 FIXME("%s: redefined\n", debugstr_w(func->name));
822 void release_vbscode(vbscode_t *code)
826 list_remove(&code->entry);
828 for(i=0; i < code->bstr_cnt; i++)
829 SysFreeString(code->bstr_pool[i]);
831 vbsheap_free(&code->heap);
833 heap_free(code->bstr_pool);
834 heap_free(code->source);
835 heap_free(code->instrs);
839 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
843 ret = heap_alloc(sizeof(*ret));
847 ret->source = heap_strdupW(source);
853 ret->instrs = heap_alloc(32*sizeof(instr_t));
855 release_vbscode(ret);
860 ctx->instr_size = 32;
861 vbsheap_init(&ret->heap);
863 ret->option_explicit = ctx->parser.option_explicit;
865 ret->bstr_pool = NULL;
866 ret->bstr_pool_size = 0;
868 ret->global_executed = FALSE;
870 ret->global_code.type = FUNC_GLOBAL;
871 ret->global_code.name = NULL;
872 ret->global_code.code_ctx = ret;
873 ret->global_code.vars = NULL;
874 ret->global_code.var_cnt = 0;
875 ret->global_code.arg_cnt = 0;
876 ret->global_code.args = NULL;
878 list_init(&ret->entry);
882 static void release_compiler(compile_ctx_t *ctx)
884 parser_release(&ctx->parser);
885 heap_free(ctx->labels);
887 release_vbscode(ctx->code);
890 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
892 function_t *new_func;
893 function_decl_t *func_decl;
898 hres = parse_script(&ctx.parser, src);
902 code = ctx.code = alloc_vbscode(&ctx, src);
904 return E_OUTOFMEMORY;
907 ctx.func_decls = NULL;
908 ctx.global_vars = NULL;
909 ctx.dim_decls = NULL;
911 ctx.labels_cnt = ctx.labels_size = 0;
913 hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
915 release_compiler(&ctx);
919 for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
920 hres = create_function(&ctx, func_decl, &new_func);
922 release_compiler(&ctx);
926 new_func->next = ctx.funcs;
927 ctx.funcs = new_func;
930 hres = check_script_collisions(&ctx, script);
932 release_compiler(&ctx);
936 if(ctx.global_vars) {
939 for(var = ctx.global_vars; var->next; var = var->next);
941 var->next = script->global_vars;
942 script->global_vars = ctx.global_vars;
946 for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);
948 new_func->next = script->global_funcs;
949 script->global_funcs = ctx.funcs;
952 if(TRACE_ON(vbscript_disas))
956 release_compiler(&ctx);
958 list_add_tail(&script->code_list, &code->entry);