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);
37 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
39 assert(id < ctx->instr_cnt);
40 return ctx->code->instrs + id;
43 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
45 assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
47 if(ctx->instr_size == ctx->instr_cnt) {
50 new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
54 ctx->code->instrs = new_instr;
58 ctx->code->instrs[ctx->instr_cnt].op = op;
59 return ctx->instr_cnt++;
62 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
64 if(!ctx->code->bstr_pool_size) {
65 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
66 if(!ctx->code->bstr_pool)
68 ctx->code->bstr_pool_size = 8;
69 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
72 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
76 ctx->code->bstr_pool = new_pool;
77 ctx->code->bstr_pool_size *= 2;
80 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
81 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
84 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
87 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
92 bstr = alloc_bstr_arg(ctx, arg);
96 instr = push_instr(ctx, op);
100 instr_ptr(ctx, instr)->arg1.bstr = bstr;
104 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr)
109 FIXME("arguments not implemented\n");
114 FIXME("obj_expr not implemented\n");
117 hres = push_instr_bstr(ctx, OP_icallv, expr->identifier);
123 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
130 hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr);
133 FIXME("Unimplemented statement type %d\n", stat->type);
145 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
149 func->code_off = ctx->instr_cnt;
151 hres = compile_statement(ctx, stat);
155 if(push_instr(ctx, OP_ret) == -1)
156 return E_OUTOFMEMORY;
161 void release_vbscode(vbscode_t *code)
165 list_remove(&code->entry);
167 for(i=0; i < code->bstr_cnt; i++)
168 SysFreeString(code->bstr_pool[i]);
170 heap_free(code->bstr_pool);
171 heap_free(code->source);
172 heap_free(code->instrs);
176 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
180 ret = heap_alloc(sizeof(*ret));
184 ret->source = heap_strdupW(source);
190 ret->instrs = heap_alloc(32*sizeof(instr_t));
192 release_vbscode(ret);
197 ctx->instr_size = 32;
199 ret->bstr_pool = NULL;
200 ret->bstr_pool_size = 0;
203 ret->global_code.code_ctx = ret;
205 list_init(&ret->entry);
209 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
214 hres = parse_script(&ctx.parser, src);
218 ctx.code = alloc_vbscode(&ctx, src);
220 return E_OUTOFMEMORY;
222 hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
224 release_vbscode(ctx.code);
228 list_add_tail(&script->code_list, &ctx.code->entry);