wine/dlls/vbscript/compile.c
2011-09-12 14:03:21 +02:00

355 lines
8.5 KiB
C

/*
* Copyright 2011 Jacek Caban for CodeWeavers
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <assert.h>
#include "vbscript.h"
#include "parse.h"
#include "parser.tab.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
typedef struct {
parser_ctx_t parser;
unsigned instr_cnt;
unsigned instr_size;
vbscode_t *code;
} compile_ctx_t;
static HRESULT compile_expression(compile_ctx_t*,expression_t*);
static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
{
return vbsheap_alloc(&vbscode->heap, size);
}
static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
{
size_t size;
WCHAR *ret;
size = (strlenW(str)+1)*sizeof(WCHAR);
ret = compiler_alloc(vbscode, size);
if(ret)
memcpy(ret, str, size);
return ret;
}
static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
{
assert(id < ctx->instr_cnt);
return ctx->code->instrs + id;
}
static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
{
assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
if(ctx->instr_size == ctx->instr_cnt) {
instr_t *new_instr;
new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
if(!new_instr)
return -1;
ctx->code->instrs = new_instr;
ctx->instr_size *= 2;
}
ctx->code->instrs[ctx->instr_cnt].op = op;
return ctx->instr_cnt++;
}
static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
{
unsigned ret;
ret = push_instr(ctx, op);
if(ret == -1)
return E_OUTOFMEMORY;
instr_ptr(ctx, ret)->arg1.lng = arg;
return S_OK;
}
static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
{
unsigned instr;
WCHAR *str;
str = compiler_alloc_string(ctx->code, arg);
if(!str)
return E_OUTOFMEMORY;
instr = push_instr(ctx, op);
if(instr == -1)
return E_OUTOFMEMORY;
instr_ptr(ctx, instr)->arg1.str = str;
return S_OK;
}
static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
{
if(!ctx->code->bstr_pool_size) {
ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
if(!ctx->code->bstr_pool)
return NULL;
ctx->code->bstr_pool_size = 8;
}else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
BSTR *new_pool;
new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
if(!new_pool)
return NULL;
ctx->code->bstr_pool = new_pool;
ctx->code->bstr_pool_size *= 2;
}
ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
return NULL;
return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
}
static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
{
unsigned instr;
BSTR bstr;
bstr = alloc_bstr_arg(ctx, arg1);
if(!bstr)
return E_OUTOFMEMORY;
instr = push_instr(ctx, op);
if(instr == -1)
return E_OUTOFMEMORY;
instr_ptr(ctx, instr)->arg1.bstr = bstr;
instr_ptr(ctx, instr)->arg2.uint = arg2;
return S_OK;
}
static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
{
unsigned arg_cnt = 0;
HRESULT hres;
while(args) {
hres = compile_expression(ctx, args);
if(FAILED(hres))
return hres;
arg_cnt++;
args = args->next;
}
*ret = arg_cnt;
return S_OK;
}
static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
{
unsigned arg_cnt = 0;
HRESULT hres;
hres = compile_args(ctx, expr->args, &arg_cnt);
if(FAILED(hres))
return hres;
if(expr->obj_expr) {
FIXME("obj_expr not implemented\n");
hres = E_NOTIMPL;
}else {
hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
}
return hres;
}
static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
{
HRESULT hres;
hres = compile_expression(ctx, expr->subexpr);
if(FAILED(hres))
return hres;
return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
}
static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
{
HRESULT hres;
hres = compile_expression(ctx, expr->left);
if(FAILED(hres))
return hres;
hres = compile_expression(ctx, expr->right);
if(FAILED(hres))
return hres;
return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
}
static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
{
switch(expr->type) {
case EXPR_BOOL:
return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
case EXPR_EMPTY:
return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
case EXPR_EQUAL:
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
case EXPR_MEMBER:
return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
case EXPR_NOT:
return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
case EXPR_NULL:
return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
case EXPR_STRING:
return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
default:
FIXME("Unimplemented expression type %d\n", expr->type);
return E_NOTIMPL;
}
return S_OK;
}
static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
{
HRESULT hres;
while(stat) {
switch(stat->type) {
case STAT_CALL:
hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
break;
default:
FIXME("Unimplemented statement type %d\n", stat->type);
hres = E_NOTIMPL;
}
if(FAILED(hres))
return hres;
stat = stat->next;
}
return S_OK;
}
static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
{
HRESULT hres;
func->code_off = ctx->instr_cnt;
hres = compile_statement(ctx, stat);
if(FAILED(hres))
return hres;
if(push_instr(ctx, OP_ret) == -1)
return E_OUTOFMEMORY;
return S_OK;
}
void release_vbscode(vbscode_t *code)
{
unsigned i;
list_remove(&code->entry);
for(i=0; i < code->bstr_cnt; i++)
SysFreeString(code->bstr_pool[i]);
vbsheap_free(&code->heap);
heap_free(code->bstr_pool);
heap_free(code->source);
heap_free(code->instrs);
heap_free(code);
}
static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
{
vbscode_t *ret;
ret = heap_alloc(sizeof(*ret));
if(!ret)
return NULL;
ret->source = heap_strdupW(source);
if(!ret->source) {
heap_free(ret);
return NULL;
}
ret->instrs = heap_alloc(32*sizeof(instr_t));
if(!ret->instrs) {
release_vbscode(ret);
return NULL;
}
ctx->instr_cnt = 0;
ctx->instr_size = 32;
vbsheap_init(&ret->heap);
ret->option_explicit = ctx->parser.option_explicit;
ret->bstr_pool = NULL;
ret->bstr_pool_size = 0;
ret->bstr_cnt = 0;
ret->global_code.code_ctx = ret;
list_init(&ret->entry);
return ret;
}
HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
{
compile_ctx_t ctx;
HRESULT hres;
hres = parse_script(&ctx.parser, src);
if(FAILED(hres))
return hres;
ctx.code = alloc_vbscode(&ctx, src);
if(!ctx.code)
return E_OUTOFMEMORY;
hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
if(FAILED(hres)) {
release_vbscode(ctx.code);
return hres;
}
list_add_tail(&script->code_list, &ctx.code->entry);
*ret = ctx.code;
return S_OK;
}