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 push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
359 return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
361 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
363 return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
365 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
367 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
369 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
371 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
373 return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
375 return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
377 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
379 return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
381 return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
383 FIXME("Unimplemented expression type %d\n", expr->type);
390 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
392 unsigned cnd_jmp, endif_label = -1;
393 elseif_decl_t *elseif_decl;
396 hres = compile_expression(ctx, stat->expr);
400 cnd_jmp = push_instr(ctx, OP_jmp_false);
402 return E_OUTOFMEMORY;
404 hres = compile_statement(ctx, stat->if_stat);
408 if(stat->else_stat || stat->elseifs) {
409 endif_label = alloc_label(ctx);
410 if(endif_label == -1)
411 return E_OUTOFMEMORY;
413 hres = push_instr_addr(ctx, OP_jmp, endif_label);
418 for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
419 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
421 hres = compile_expression(ctx, elseif_decl->expr);
425 cnd_jmp = push_instr(ctx, OP_jmp_false);
427 return E_OUTOFMEMORY;
429 hres = compile_statement(ctx, elseif_decl->stat);
433 hres = push_instr_addr(ctx, OP_jmp, endif_label);
438 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
440 if(stat->else_stat) {
441 hres = compile_statement(ctx, stat->else_stat);
446 if(endif_label != -1)
447 label_set_addr(ctx, endif_label);
451 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat)
455 hres = compile_expression(ctx, stat->value_expr);
459 if(stat->member_expr->args) {
460 FIXME("arguments support not implemented\n");
464 if(stat->member_expr->obj_expr) {
465 hres = compile_expression(ctx, stat->member_expr->obj_expr);
469 hres = push_instr_bstr(ctx, OP_assign_member, stat->member_expr->identifier);
471 hres = push_instr_bstr(ctx, OP_assign_ident, stat->member_expr->identifier);
477 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
479 dim_decl_t *dim_decl;
481 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
482 if(!strcmpiW(dim_decl->name, name))
489 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
491 dim_decl_t *dim_decl = stat->dim_decls;
494 if(lookup_dim_decls(ctx, dim_decl->name)) {
495 FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
501 dim_decl = dim_decl->next;
504 dim_decl->next = ctx->dim_decls;
505 ctx->dim_decls = stat->dim_decls;
509 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
516 hres = compile_assign_statement(ctx, (assign_statement_t*)stat);
519 hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
522 hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
525 hres = compile_if_statement(ctx, (if_statement_t*)stat);
528 FIXME("Unimplemented statement type %d\n", stat->type);
540 static void resolve_labels(compile_ctx_t *ctx)
544 for(instr = ctx->code->instrs; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
545 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
546 assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
547 instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
549 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
555 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
559 func->code_off = ctx->instr_cnt;
561 hres = compile_statement(ctx, stat);
565 if(push_instr(ctx, OP_ret) == -1)
566 return E_OUTOFMEMORY;
571 dim_decl_t *dim_decl;
572 dynamic_var_t *new_var;
574 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
575 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
577 return E_OUTOFMEMORY;
579 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
581 return E_OUTOFMEMORY;
583 V_VT(&new_var->v) = VT_EMPTY;
585 new_var->next = ctx->global_vars;
586 ctx->global_vars = new_var;
593 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
597 for(var = script->global_vars; var; var = var->next) {
598 if(!strcmpiW(var->name, identifier))
605 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
609 for(var = ctx->global_vars; var; var = var->next) {
610 if(lookup_script_identifier(script, var->name)) {
611 FIXME("%s: redefined\n", debugstr_w(var->name));
619 void release_vbscode(vbscode_t *code)
623 list_remove(&code->entry);
625 for(i=0; i < code->bstr_cnt; i++)
626 SysFreeString(code->bstr_pool[i]);
628 vbsheap_free(&code->heap);
630 heap_free(code->bstr_pool);
631 heap_free(code->source);
632 heap_free(code->instrs);
636 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
640 ret = heap_alloc(sizeof(*ret));
644 ret->source = heap_strdupW(source);
650 ret->instrs = heap_alloc(32*sizeof(instr_t));
652 release_vbscode(ret);
657 ctx->instr_size = 32;
658 vbsheap_init(&ret->heap);
660 ret->option_explicit = ctx->parser.option_explicit;
662 ret->bstr_pool = NULL;
663 ret->bstr_pool_size = 0;
666 ret->global_code.code_ctx = ret;
668 list_init(&ret->entry);
672 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
677 hres = parse_script(&ctx.parser, src);
681 ctx.code = alloc_vbscode(&ctx, src);
683 return E_OUTOFMEMORY;
685 ctx.global_vars = NULL;
686 ctx.dim_decls = NULL;
688 ctx.labels_cnt = ctx.labels_size = 0;
690 hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
692 release_vbscode(ctx.code);
696 hres = check_script_collisions(&ctx, script);
698 release_vbscode(ctx.code);
702 if(ctx.global_vars) {
705 for(var = ctx.global_vars; var->next; var = var->next);
707 var->next = script->global_vars;
708 script->global_vars = ctx.global_vars;
711 parser_release(&ctx.parser);
713 if(TRACE_ON(vbscript_disas))
716 list_add_tail(&script->code_list, &ctx.code->entry);