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);
36 dim_decl_t *dim_decls;
37 dynamic_var_t *global_vars;
40 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
42 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
44 return vbsheap_alloc(&vbscode->heap, size);
47 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
52 size = (strlenW(str)+1)*sizeof(WCHAR);
53 ret = compiler_alloc(vbscode, size);
55 memcpy(ret, str, size);
59 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
61 assert(id < ctx->instr_cnt);
62 return ctx->code->instrs + id;
65 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
67 assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
69 if(ctx->instr_size == ctx->instr_cnt) {
72 new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
76 ctx->code->instrs = new_instr;
80 ctx->code->instrs[ctx->instr_cnt].op = op;
81 return ctx->instr_cnt++;
84 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
88 ret = push_instr(ctx, op);
92 instr_ptr(ctx, ret)->arg1.lng = arg;
96 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
101 str = compiler_alloc_string(ctx->code, arg);
103 return E_OUTOFMEMORY;
105 instr = push_instr(ctx, op);
107 return E_OUTOFMEMORY;
109 instr_ptr(ctx, instr)->arg1.str = str;
113 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
118 d = compiler_alloc(ctx->code, sizeof(double));
120 return E_OUTOFMEMORY;
122 instr = push_instr(ctx, op);
124 return E_OUTOFMEMORY;
127 instr_ptr(ctx, instr)->arg1.dbl = d;
131 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
133 if(!ctx->code->bstr_pool_size) {
134 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
135 if(!ctx->code->bstr_pool)
137 ctx->code->bstr_pool_size = 8;
138 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
141 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
145 ctx->code->bstr_pool = new_pool;
146 ctx->code->bstr_pool_size *= 2;
149 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
150 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
153 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
156 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
161 bstr = alloc_bstr_arg(ctx, arg);
163 return E_OUTOFMEMORY;
165 instr = push_instr(ctx, op);
167 return E_OUTOFMEMORY;
169 instr_ptr(ctx, instr)->arg1.bstr = bstr;
173 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
178 bstr = alloc_bstr_arg(ctx, arg1);
180 return E_OUTOFMEMORY;
182 instr = push_instr(ctx, op);
184 return E_OUTOFMEMORY;
186 instr_ptr(ctx, instr)->arg1.bstr = bstr;
187 instr_ptr(ctx, instr)->arg2.uint = arg2;
191 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
193 unsigned arg_cnt = 0;
197 hres = compile_expression(ctx, args);
209 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
211 unsigned arg_cnt = 0;
214 hres = compile_args(ctx, expr->args, &arg_cnt);
219 FIXME("obj_expr not implemented\n");
222 hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
228 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
232 hres = compile_expression(ctx, expr->subexpr);
236 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
239 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
243 hres = compile_expression(ctx, expr->left);
247 hres = compile_expression(ctx, expr->right);
251 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
254 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
258 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
260 return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
262 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
264 return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
266 return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
268 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
270 return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
272 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
274 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
276 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
278 return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
280 return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
282 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
284 return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
286 return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
288 FIXME("Unimplemented expression type %d\n", expr->type);
295 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat)
299 hres = compile_expression(ctx, stat->value_expr);
303 if(stat->member_expr->args) {
304 FIXME("arguments support not implemented\n");
308 if(stat->member_expr->obj_expr) {
309 hres = compile_expression(ctx, stat->member_expr->obj_expr);
313 hres = push_instr_bstr(ctx, OP_assign_member, stat->member_expr->identifier);
315 hres = push_instr_bstr(ctx, OP_assign_ident, stat->member_expr->identifier);
321 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
323 dim_decl_t *dim_decl;
325 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
326 if(!strcmpiW(dim_decl->name, name))
333 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
335 dim_decl_t *dim_decl = stat->dim_decls;
338 if(lookup_dim_decls(ctx, dim_decl->name)) {
339 FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
345 dim_decl = dim_decl->next;
348 dim_decl->next = ctx->dim_decls;
349 ctx->dim_decls = stat->dim_decls;
353 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
360 hres = compile_assign_statement(ctx, (assign_statement_t*)stat);
363 hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
366 hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
369 FIXME("Unimplemented statement type %d\n", stat->type);
381 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
385 func->code_off = ctx->instr_cnt;
387 hres = compile_statement(ctx, stat);
391 if(push_instr(ctx, OP_ret) == -1)
392 return E_OUTOFMEMORY;
395 dim_decl_t *dim_decl;
396 dynamic_var_t *new_var;
398 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
399 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
401 return E_OUTOFMEMORY;
403 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
405 return E_OUTOFMEMORY;
407 V_VT(&new_var->v) = VT_EMPTY;
409 new_var->next = ctx->global_vars;
410 ctx->global_vars = new_var;
417 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
421 for(var = script->global_vars; var; var = var->next) {
422 if(!strcmpiW(var->name, identifier))
429 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
433 for(var = ctx->global_vars; var; var = var->next) {
434 if(lookup_script_identifier(script, var->name)) {
435 FIXME("%s: redefined\n", debugstr_w(var->name));
443 void release_vbscode(vbscode_t *code)
447 list_remove(&code->entry);
449 for(i=0; i < code->bstr_cnt; i++)
450 SysFreeString(code->bstr_pool[i]);
452 vbsheap_free(&code->heap);
454 heap_free(code->bstr_pool);
455 heap_free(code->source);
456 heap_free(code->instrs);
460 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
464 ret = heap_alloc(sizeof(*ret));
468 ret->source = heap_strdupW(source);
474 ret->instrs = heap_alloc(32*sizeof(instr_t));
476 release_vbscode(ret);
481 ctx->instr_size = 32;
482 vbsheap_init(&ret->heap);
484 ret->option_explicit = ctx->parser.option_explicit;
486 ret->bstr_pool = NULL;
487 ret->bstr_pool_size = 0;
490 ret->global_code.code_ctx = ret;
492 list_init(&ret->entry);
496 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
501 hres = parse_script(&ctx.parser, src);
505 ctx.code = alloc_vbscode(&ctx, src);
507 return E_OUTOFMEMORY;
509 ctx.global_vars = NULL;
510 ctx.dim_decls = NULL;
512 hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
514 release_vbscode(ctx.code);
518 hres = check_script_collisions(&ctx, script);
520 release_vbscode(ctx.code);
524 if(ctx.global_vars) {
527 for(var = ctx.global_vars; var->next; var = var->next);
529 var->next = script->global_vars;
530 script->global_vars = ctx.global_vars;
533 parser_release(&ctx.parser);
535 list_add_tail(&script->code_list, &ctx.code->entry);