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);
40 dim_decl_t *dim_decls;
41 dynamic_var_t *global_vars;
44 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
45 static HRESULT compile_statement(compile_ctx_t*,statement_t*);
48 instr_arg_type_t arg1_type;
49 instr_arg_type_t arg2_type;
51 #define X(n,a,b,c) {b,c},
56 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
58 return vbsheap_alloc(&vbscode->heap, size);
61 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
66 size = (strlenW(str)+1)*sizeof(WCHAR);
67 ret = compiler_alloc(vbscode, size);
69 memcpy(ret, str, size);
73 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
75 assert(id < ctx->instr_cnt);
76 return ctx->code->instrs + id;
79 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
81 assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
83 if(ctx->instr_size == ctx->instr_cnt) {
86 new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
90 ctx->code->instrs = new_instr;
94 ctx->code->instrs[ctx->instr_cnt].op = op;
95 return ctx->instr_cnt++;
98 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
102 ret = push_instr(ctx, op);
104 return E_OUTOFMEMORY;
106 instr_ptr(ctx, ret)->arg1.lng = arg;
110 static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
114 ret = push_instr(ctx, op);
116 return E_OUTOFMEMORY;
118 instr_ptr(ctx, ret)->arg1.uint = arg;
122 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
127 str = compiler_alloc_string(ctx->code, arg);
129 return E_OUTOFMEMORY;
131 instr = push_instr(ctx, op);
133 return E_OUTOFMEMORY;
135 instr_ptr(ctx, instr)->arg1.str = str;
139 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
144 d = compiler_alloc(ctx->code, sizeof(double));
146 return E_OUTOFMEMORY;
148 instr = push_instr(ctx, op);
150 return E_OUTOFMEMORY;
153 instr_ptr(ctx, instr)->arg1.dbl = d;
157 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
159 if(!ctx->code->bstr_pool_size) {
160 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
161 if(!ctx->code->bstr_pool)
163 ctx->code->bstr_pool_size = 8;
164 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
167 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
171 ctx->code->bstr_pool = new_pool;
172 ctx->code->bstr_pool_size *= 2;
175 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
176 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
179 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
182 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
187 bstr = alloc_bstr_arg(ctx, arg);
189 return E_OUTOFMEMORY;
191 instr = push_instr(ctx, op);
193 return E_OUTOFMEMORY;
195 instr_ptr(ctx, instr)->arg1.bstr = bstr;
199 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
204 bstr = alloc_bstr_arg(ctx, arg1);
206 return E_OUTOFMEMORY;
208 instr = push_instr(ctx, op);
210 return E_OUTOFMEMORY;
212 instr_ptr(ctx, instr)->arg1.bstr = bstr;
213 instr_ptr(ctx, instr)->arg2.uint = arg2;
217 #define LABEL_FLAG 0x80000000
219 static unsigned alloc_label(compile_ctx_t *ctx)
221 if(!ctx->labels_size) {
222 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
225 ctx->labels_size = 8;
226 }else if(ctx->labels_size == ctx->labels_cnt) {
227 unsigned *new_labels;
229 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
233 ctx->labels = new_labels;
234 ctx->labels_size *= 2;
237 return ctx->labels_cnt++ | LABEL_FLAG;
240 static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
242 assert(label & LABEL_FLAG);
243 ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
246 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
248 unsigned arg_cnt = 0;
252 hres = compile_expression(ctx, args);
264 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
266 unsigned arg_cnt = 0;
269 hres = compile_args(ctx, expr->args, &arg_cnt);
274 FIXME("obj_expr not implemented\n");
277 hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
283 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
287 hres = compile_expression(ctx, expr->subexpr);
291 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
294 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
298 hres = compile_expression(ctx, expr->left);
302 hres = compile_expression(ctx, expr->right);
306 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
309 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
313 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
315 return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
317 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
319 return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
321 return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
323 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
325 return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
327 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
329 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
331 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
333 return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
335 return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
337 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
339 return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
341 return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
343 FIXME("Unimplemented expression type %d\n", expr->type);
350 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
352 unsigned cnd_jmp, endif_label = -1;
353 elseif_decl_t *elseif_decl;
356 hres = compile_expression(ctx, stat->expr);
360 cnd_jmp = push_instr(ctx, OP_jmp_false);
362 return E_OUTOFMEMORY;
364 hres = compile_statement(ctx, stat->if_stat);
368 if(stat->else_stat || stat->elseifs) {
369 endif_label = alloc_label(ctx);
370 if(endif_label == -1)
371 return E_OUTOFMEMORY;
373 hres = push_instr_addr(ctx, OP_jmp, endif_label);
378 for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
379 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
381 hres = compile_expression(ctx, elseif_decl->expr);
385 cnd_jmp = push_instr(ctx, OP_jmp_false);
387 return E_OUTOFMEMORY;
389 hres = compile_statement(ctx, elseif_decl->stat);
393 hres = push_instr_addr(ctx, OP_jmp, endif_label);
398 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
400 if(stat->else_stat) {
401 hres = compile_statement(ctx, stat->else_stat);
406 if(endif_label != -1)
407 label_set_addr(ctx, endif_label);
411 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat)
415 hres = compile_expression(ctx, stat->value_expr);
419 if(stat->member_expr->args) {
420 FIXME("arguments support not implemented\n");
424 if(stat->member_expr->obj_expr) {
425 hres = compile_expression(ctx, stat->member_expr->obj_expr);
429 hres = push_instr_bstr(ctx, OP_assign_member, stat->member_expr->identifier);
431 hres = push_instr_bstr(ctx, OP_assign_ident, stat->member_expr->identifier);
437 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
439 dim_decl_t *dim_decl;
441 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
442 if(!strcmpiW(dim_decl->name, name))
449 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
451 dim_decl_t *dim_decl = stat->dim_decls;
454 if(lookup_dim_decls(ctx, dim_decl->name)) {
455 FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
461 dim_decl = dim_decl->next;
464 dim_decl->next = ctx->dim_decls;
465 ctx->dim_decls = stat->dim_decls;
469 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
476 hres = compile_assign_statement(ctx, (assign_statement_t*)stat);
479 hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
482 hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
485 hres = compile_if_statement(ctx, (if_statement_t*)stat);
488 FIXME("Unimplemented statement type %d\n", stat->type);
500 static void resolve_labels(compile_ctx_t *ctx)
504 for(instr = ctx->code->instrs; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
505 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
506 assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
507 instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
509 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
515 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
519 func->code_off = ctx->instr_cnt;
521 hres = compile_statement(ctx, stat);
525 if(push_instr(ctx, OP_ret) == -1)
526 return E_OUTOFMEMORY;
531 dim_decl_t *dim_decl;
532 dynamic_var_t *new_var;
534 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
535 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
537 return E_OUTOFMEMORY;
539 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
541 return E_OUTOFMEMORY;
543 V_VT(&new_var->v) = VT_EMPTY;
545 new_var->next = ctx->global_vars;
546 ctx->global_vars = new_var;
553 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
557 for(var = script->global_vars; var; var = var->next) {
558 if(!strcmpiW(var->name, identifier))
565 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
569 for(var = ctx->global_vars; var; var = var->next) {
570 if(lookup_script_identifier(script, var->name)) {
571 FIXME("%s: redefined\n", debugstr_w(var->name));
579 void release_vbscode(vbscode_t *code)
583 list_remove(&code->entry);
585 for(i=0; i < code->bstr_cnt; i++)
586 SysFreeString(code->bstr_pool[i]);
588 vbsheap_free(&code->heap);
590 heap_free(code->bstr_pool);
591 heap_free(code->source);
592 heap_free(code->instrs);
596 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
600 ret = heap_alloc(sizeof(*ret));
604 ret->source = heap_strdupW(source);
610 ret->instrs = heap_alloc(32*sizeof(instr_t));
612 release_vbscode(ret);
617 ctx->instr_size = 32;
618 vbsheap_init(&ret->heap);
620 ret->option_explicit = ctx->parser.option_explicit;
622 ret->bstr_pool = NULL;
623 ret->bstr_pool_size = 0;
626 ret->global_code.code_ctx = ret;
628 list_init(&ret->entry);
632 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
637 hres = parse_script(&ctx.parser, src);
641 ctx.code = alloc_vbscode(&ctx, src);
643 return E_OUTOFMEMORY;
645 ctx.global_vars = NULL;
646 ctx.dim_decls = NULL;
648 ctx.labels_cnt = ctx.labels_size = 0;
650 hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
652 release_vbscode(ctx.code);
656 hres = check_script_collisions(&ctx, script);
658 release_vbscode(ctx.code);
662 if(ctx.global_vars) {
665 for(var = ctx.global_vars; var->next; var = var->next);
667 var->next = script->global_vars;
668 script->global_vars = ctx.global_vars;
671 parser_release(&ctx.parser);
673 list_add_tail(&script->code_list, &ctx.code->entry);