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;
45 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
46 static HRESULT compile_statement(compile_ctx_t*,statement_t*);
50 instr_arg_type_t arg1_type;
51 instr_arg_type_t arg2_type;
53 #define X(n,a,b,c) {#n,b,c},
58 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
63 TRACE_(vbscript_disas)("\t%s", debugstr_w(arg->str));
66 TRACE_(vbscript_disas)("\t%d", arg->uint);
70 TRACE_(vbscript_disas)("\t%u", arg->uint);
73 TRACE_(vbscript_disas)("\t%lf", *arg->dbl);
82 static void dump_code(compile_ctx_t *ctx)
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");
94 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
96 return vbsheap_alloc(&vbscode->heap, size);
99 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
104 size = (strlenW(str)+1)*sizeof(WCHAR);
105 ret = compiler_alloc(vbscode, size);
107 memcpy(ret, str, size);
111 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
113 assert(id < ctx->instr_cnt);
114 return ctx->code->instrs + id;
117 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
119 assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
121 if(ctx->instr_size == ctx->instr_cnt) {
124 new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
128 ctx->code->instrs = new_instr;
129 ctx->instr_size *= 2;
132 ctx->code->instrs[ctx->instr_cnt].op = op;
133 return ctx->instr_cnt++;
136 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
140 ret = push_instr(ctx, op);
142 return E_OUTOFMEMORY;
144 instr_ptr(ctx, ret)->arg1.lng = arg;
148 static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
152 ret = push_instr(ctx, op);
154 return E_OUTOFMEMORY;
156 instr_ptr(ctx, ret)->arg1.uint = arg;
160 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
165 str = compiler_alloc_string(ctx->code, arg);
167 return E_OUTOFMEMORY;
169 instr = push_instr(ctx, op);
171 return E_OUTOFMEMORY;
173 instr_ptr(ctx, instr)->arg1.str = str;
177 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
182 d = compiler_alloc(ctx->code, sizeof(double));
184 return E_OUTOFMEMORY;
186 instr = push_instr(ctx, op);
188 return E_OUTOFMEMORY;
191 instr_ptr(ctx, instr)->arg1.dbl = d;
195 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
197 if(!ctx->code->bstr_pool_size) {
198 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
199 if(!ctx->code->bstr_pool)
201 ctx->code->bstr_pool_size = 8;
202 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
205 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
209 ctx->code->bstr_pool = new_pool;
210 ctx->code->bstr_pool_size *= 2;
213 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
214 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
217 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
220 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
225 bstr = alloc_bstr_arg(ctx, arg);
227 return E_OUTOFMEMORY;
229 instr = push_instr(ctx, op);
231 return E_OUTOFMEMORY;
233 instr_ptr(ctx, instr)->arg1.bstr = bstr;
237 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
242 bstr = alloc_bstr_arg(ctx, arg1);
244 return E_OUTOFMEMORY;
246 instr = push_instr(ctx, op);
248 return E_OUTOFMEMORY;
250 instr_ptr(ctx, instr)->arg1.bstr = bstr;
251 instr_ptr(ctx, instr)->arg2.uint = arg2;
255 #define LABEL_FLAG 0x80000000
257 static unsigned alloc_label(compile_ctx_t *ctx)
259 if(!ctx->labels_size) {
260 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
263 ctx->labels_size = 8;
264 }else if(ctx->labels_size == ctx->labels_cnt) {
265 unsigned *new_labels;
267 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
271 ctx->labels = new_labels;
272 ctx->labels_size *= 2;
275 return ctx->labels_cnt++ | LABEL_FLAG;
278 static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
280 assert(label & LABEL_FLAG);
281 ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
284 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
286 unsigned arg_cnt = 0;
290 hres = compile_expression(ctx, args);
302 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
304 unsigned arg_cnt = 0;
307 hres = compile_args(ctx, expr->args, &arg_cnt);
312 FIXME("obj_expr not implemented\n");
315 hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
321 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
325 hres = compile_expression(ctx, expr->subexpr);
329 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
332 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
336 hres = compile_expression(ctx, expr->left);
340 hres = compile_expression(ctx, expr->right);
344 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
347 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
351 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
353 return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
355 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
357 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
359 return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
361 return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
363 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
365 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
367 return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
369 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
371 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
373 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
375 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
377 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
379 return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
381 return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
383 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
385 return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
387 return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
389 FIXME("Unimplemented expression type %d\n", expr->type);
396 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
398 unsigned cnd_jmp, endif_label = -1;
399 elseif_decl_t *elseif_decl;
402 hres = compile_expression(ctx, stat->expr);
406 cnd_jmp = push_instr(ctx, OP_jmp_false);
408 return E_OUTOFMEMORY;
410 hres = compile_statement(ctx, stat->if_stat);
414 if(stat->else_stat || stat->elseifs) {
415 endif_label = alloc_label(ctx);
416 if(endif_label == -1)
417 return E_OUTOFMEMORY;
419 hres = push_instr_addr(ctx, OP_jmp, endif_label);
424 for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
425 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
427 hres = compile_expression(ctx, elseif_decl->expr);
431 cnd_jmp = push_instr(ctx, OP_jmp_false);
433 return E_OUTOFMEMORY;
435 hres = compile_statement(ctx, elseif_decl->stat);
439 hres = push_instr_addr(ctx, OP_jmp, endif_label);
444 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
446 if(stat->else_stat) {
447 hres = compile_statement(ctx, stat->else_stat);
452 if(endif_label != -1)
453 label_set_addr(ctx, endif_label);
457 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat)
461 hres = compile_expression(ctx, stat->value_expr);
465 if(stat->member_expr->args) {
466 FIXME("arguments support not implemented\n");
470 if(stat->member_expr->obj_expr) {
471 hres = compile_expression(ctx, stat->member_expr->obj_expr);
475 hres = push_instr_bstr(ctx, OP_assign_member, stat->member_expr->identifier);
477 hres = push_instr_bstr(ctx, OP_assign_ident, stat->member_expr->identifier);
483 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
485 dim_decl_t *dim_decl;
487 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
488 if(!strcmpiW(dim_decl->name, name))
495 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
497 dim_decl_t *dim_decl = stat->dim_decls;
500 if(lookup_dim_decls(ctx, dim_decl->name)) {
501 FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
507 dim_decl = dim_decl->next;
510 dim_decl->next = ctx->dim_decls;
511 ctx->dim_decls = stat->dim_decls;
515 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
522 hres = compile_assign_statement(ctx, (assign_statement_t*)stat);
525 hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
528 hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
531 hres = compile_if_statement(ctx, (if_statement_t*)stat);
534 FIXME("Unimplemented statement type %d\n", stat->type);
546 static void resolve_labels(compile_ctx_t *ctx)
550 for(instr = ctx->code->instrs; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
551 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
552 assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
553 instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
555 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
561 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
565 func->code_off = ctx->instr_cnt;
567 hres = compile_statement(ctx, stat);
571 if(push_instr(ctx, OP_ret) == -1)
572 return E_OUTOFMEMORY;
577 dim_decl_t *dim_decl;
578 dynamic_var_t *new_var;
580 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
581 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
583 return E_OUTOFMEMORY;
585 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
587 return E_OUTOFMEMORY;
589 V_VT(&new_var->v) = VT_EMPTY;
591 new_var->next = ctx->global_vars;
592 ctx->global_vars = new_var;
599 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
603 for(var = script->global_vars; var; var = var->next) {
604 if(!strcmpiW(var->name, identifier))
611 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
615 for(var = ctx->global_vars; var; var = var->next) {
616 if(lookup_script_identifier(script, var->name)) {
617 FIXME("%s: redefined\n", debugstr_w(var->name));
625 void release_vbscode(vbscode_t *code)
629 list_remove(&code->entry);
631 for(i=0; i < code->bstr_cnt; i++)
632 SysFreeString(code->bstr_pool[i]);
634 vbsheap_free(&code->heap);
636 heap_free(code->bstr_pool);
637 heap_free(code->source);
638 heap_free(code->instrs);
642 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
646 ret = heap_alloc(sizeof(*ret));
650 ret->source = heap_strdupW(source);
656 ret->instrs = heap_alloc(32*sizeof(instr_t));
658 release_vbscode(ret);
663 ctx->instr_size = 32;
664 vbsheap_init(&ret->heap);
666 ret->option_explicit = ctx->parser.option_explicit;
668 ret->bstr_pool = NULL;
669 ret->bstr_pool_size = 0;
672 ret->global_code.code_ctx = ret;
674 list_init(&ret->entry);
678 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
683 hres = parse_script(&ctx.parser, src);
687 ctx.code = alloc_vbscode(&ctx, src);
689 return E_OUTOFMEMORY;
691 ctx.global_vars = NULL;
692 ctx.dim_decls = NULL;
694 ctx.labels_cnt = ctx.labels_size = 0;
696 hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
698 release_vbscode(ctx.code);
702 hres = check_script_collisions(&ctx, script);
704 release_vbscode(ctx.code);
708 if(ctx.global_vars) {
711 for(var = ctx.global_vars; var->next; var = var->next);
713 var->next = script->global_vars;
714 script->global_vars = ctx.global_vars;
717 parser_release(&ctx.parser);
719 if(TRACE_ON(vbscript_disas))
722 list_add_tail(&script->code_list, &ctx.code->entry);