2011-09-07 14:07:12 +02:00
|
|
|
/*
|
|
|
|
* 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);
|
2011-09-13 11:39:41 +02:00
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(vbscript_disas);
|
2011-09-07 14:07:12 +02:00
|
|
|
|
2012-07-03 19:17:07 +02:00
|
|
|
typedef struct _statement_ctx_t {
|
|
|
|
unsigned stack_use;
|
|
|
|
|
|
|
|
unsigned while_end_label;
|
|
|
|
unsigned for_end_label;
|
|
|
|
|
|
|
|
struct _statement_ctx_t *next;
|
|
|
|
} statement_ctx_t;
|
|
|
|
|
2011-09-07 14:07:12 +02:00
|
|
|
typedef struct {
|
|
|
|
parser_ctx_t parser;
|
|
|
|
|
|
|
|
unsigned instr_cnt;
|
|
|
|
unsigned instr_size;
|
|
|
|
vbscode_t *code;
|
2011-09-13 11:36:47 +02:00
|
|
|
|
2012-07-03 19:17:07 +02:00
|
|
|
statement_ctx_t *stat_ctx;
|
|
|
|
|
2011-09-13 11:38:38 +02:00
|
|
|
unsigned *labels;
|
|
|
|
unsigned labels_size;
|
|
|
|
unsigned labels_cnt;
|
|
|
|
|
2011-09-14 12:57:27 +02:00
|
|
|
unsigned sub_end_label;
|
2011-09-14 12:58:10 +02:00
|
|
|
unsigned func_end_label;
|
2011-09-16 13:28:29 +02:00
|
|
|
unsigned prop_end_label;
|
2011-09-14 12:57:27 +02:00
|
|
|
|
2011-09-13 11:36:47 +02:00
|
|
|
dim_decl_t *dim_decls;
|
2013-11-13 16:29:05 +01:00
|
|
|
dim_decl_t *dim_decls_tail;
|
2011-09-13 11:36:47 +02:00
|
|
|
dynamic_var_t *global_vars;
|
2011-09-14 12:55:07 +02:00
|
|
|
|
2011-09-21 14:03:38 +02:00
|
|
|
const_decl_t *const_decls;
|
|
|
|
const_decl_t *global_consts;
|
|
|
|
|
2011-09-14 12:55:07 +02:00
|
|
|
function_t *func;
|
|
|
|
function_t *funcs;
|
|
|
|
function_decl_t *func_decls;
|
2011-09-15 14:17:17 +02:00
|
|
|
|
|
|
|
class_desc_t *classes;
|
2011-09-07 14:07:12 +02:00
|
|
|
} compile_ctx_t;
|
|
|
|
|
2011-09-08 14:57:16 +02:00
|
|
|
static HRESULT compile_expression(compile_ctx_t*,expression_t*);
|
2012-07-03 19:17:07 +02:00
|
|
|
static HRESULT compile_statement(compile_ctx_t*,statement_ctx_t*,statement_t*);
|
2011-09-13 11:38:38 +02:00
|
|
|
|
|
|
|
static const struct {
|
2011-09-13 11:39:41 +02:00
|
|
|
const char *op_str;
|
2011-09-13 11:38:38 +02:00
|
|
|
instr_arg_type_t arg1_type;
|
|
|
|
instr_arg_type_t arg2_type;
|
|
|
|
} instr_info[] = {
|
2011-09-13 11:39:41 +02:00
|
|
|
#define X(n,a,b,c) {#n,b,c},
|
2011-09-13 11:38:38 +02:00
|
|
|
OP_LIST
|
|
|
|
#undef X
|
|
|
|
};
|
2011-09-08 14:57:16 +02:00
|
|
|
|
2011-09-13 11:39:41 +02:00
|
|
|
static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
|
|
|
|
{
|
|
|
|
switch(type) {
|
|
|
|
case ARG_STR:
|
|
|
|
case ARG_BSTR:
|
|
|
|
TRACE_(vbscript_disas)("\t%s", debugstr_w(arg->str));
|
|
|
|
break;
|
|
|
|
case ARG_INT:
|
|
|
|
TRACE_(vbscript_disas)("\t%d", arg->uint);
|
|
|
|
break;
|
|
|
|
case ARG_UINT:
|
|
|
|
case ARG_ADDR:
|
|
|
|
TRACE_(vbscript_disas)("\t%u", arg->uint);
|
|
|
|
break;
|
|
|
|
case ARG_DOUBLE:
|
|
|
|
TRACE_(vbscript_disas)("\t%lf", *arg->dbl);
|
|
|
|
break;
|
|
|
|
case ARG_NONE:
|
|
|
|
break;
|
2013-03-13 10:43:56 +01:00
|
|
|
DEFAULT_UNREACHABLE;
|
2011-09-13 11:39:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_code(compile_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
instr_t *instr;
|
|
|
|
|
2012-07-03 17:05:16 +02:00
|
|
|
for(instr = ctx->code->instrs+1; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
|
|
|
|
assert(instr->op < OP_LAST);
|
2011-09-18 13:40:33 +02:00
|
|
|
TRACE_(vbscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
|
2011-09-13 11:39:41 +02:00
|
|
|
dump_instr_arg(instr_info[instr->op].arg1_type, &instr->arg1);
|
|
|
|
dump_instr_arg(instr_info[instr->op].arg2_type, &instr->arg2);
|
|
|
|
TRACE_(vbscript_disas)("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 12:29:14 +02:00
|
|
|
static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
|
|
|
|
{
|
2013-02-15 15:11:13 +01:00
|
|
|
return heap_pool_alloc(&vbscode->heap, size);
|
2011-09-12 12:29:14 +02:00
|
|
|
}
|
|
|
|
|
2011-09-19 14:08:38 +02:00
|
|
|
static inline void *compiler_alloc_zero(vbscode_t *vbscode, size_t size)
|
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
|
2013-02-15 15:11:13 +01:00
|
|
|
ret = heap_pool_alloc(&vbscode->heap, size);
|
2011-09-19 14:08:38 +02:00
|
|
|
if(ret)
|
|
|
|
memset(ret, 0, size);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-09-12 12:29:14 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-09-07 14:08:47 +02:00
|
|
|
static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
|
|
|
|
{
|
|
|
|
assert(id < ctx->instr_cnt);
|
|
|
|
return ctx->code->instrs + id;
|
|
|
|
}
|
|
|
|
|
2011-09-07 14:07:12 +02:00
|
|
|
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)
|
2012-01-16 12:40:26 +01:00
|
|
|
return 0;
|
2011-09-07 14:07:12 +02:00
|
|
|
|
|
|
|
ctx->code->instrs = new_instr;
|
|
|
|
ctx->instr_size *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->code->instrs[ctx->instr_cnt].op = op;
|
|
|
|
return ctx->instr_cnt++;
|
|
|
|
}
|
|
|
|
|
2011-09-08 14:57:16 +02:00
|
|
|
static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
|
|
|
|
{
|
|
|
|
unsigned ret;
|
|
|
|
|
|
|
|
ret = push_instr(ctx, op);
|
2012-01-16 12:40:26 +01:00
|
|
|
if(!ret)
|
2011-09-08 14:57:16 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
instr_ptr(ctx, ret)->arg1.lng = arg;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-22 14:23:10 +02:00
|
|
|
static HRESULT push_instr_uint(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
|
|
|
|
{
|
|
|
|
unsigned ret;
|
|
|
|
|
|
|
|
ret = push_instr(ctx, op);
|
2012-01-16 12:40:26 +01:00
|
|
|
if(!ret)
|
2011-09-22 14:23:10 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
instr_ptr(ctx, ret)->arg1.uint = arg;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-13 11:38:38 +02:00
|
|
|
static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
|
|
|
|
{
|
|
|
|
unsigned ret;
|
|
|
|
|
|
|
|
ret = push_instr(ctx, op);
|
2012-01-16 12:40:26 +01:00
|
|
|
if(!ret)
|
2011-09-13 11:38:38 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
instr_ptr(ctx, ret)->arg1.uint = arg;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-08 14:57:31 +02:00
|
|
|
static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
|
|
|
|
{
|
2011-09-12 12:29:14 +02:00
|
|
|
unsigned instr;
|
|
|
|
WCHAR *str;
|
2011-09-08 14:57:31 +02:00
|
|
|
|
2011-09-12 12:29:14 +02:00
|
|
|
str = compiler_alloc_string(ctx->code, arg);
|
|
|
|
if(!str)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
instr = push_instr(ctx, op);
|
2012-01-16 12:40:26 +01:00
|
|
|
if(!instr)
|
2011-09-08 14:57:31 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2011-09-12 12:29:14 +02:00
|
|
|
instr_ptr(ctx, instr)->arg1.str = str;
|
2011-09-08 14:57:31 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-12 12:30:11 +02:00
|
|
|
static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
|
|
|
|
{
|
|
|
|
unsigned instr;
|
|
|
|
double *d;
|
|
|
|
|
|
|
|
d = compiler_alloc(ctx->code, sizeof(double));
|
|
|
|
if(!d)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
instr = push_instr(ctx, op);
|
2012-01-16 12:40:26 +01:00
|
|
|
if(!instr)
|
2011-09-12 12:30:11 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
*d = arg;
|
|
|
|
instr_ptr(ctx, instr)->arg1.dbl = d;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-08 14:54:23 +02:00
|
|
|
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) {
|
2011-09-16 13:30:39 +02:00
|
|
|
BSTR *new_pool;
|
2011-09-08 14:54:23 +02:00
|
|
|
|
|
|
|
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++];
|
|
|
|
}
|
|
|
|
|
2011-09-12 12:32:50 +02:00
|
|
|
static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
|
|
|
|
{
|
|
|
|
unsigned instr;
|
|
|
|
BSTR bstr;
|
|
|
|
|
|
|
|
bstr = alloc_bstr_arg(ctx, arg);
|
|
|
|
if(!bstr)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
instr = push_instr(ctx, op);
|
2012-01-16 12:40:26 +01:00
|
|
|
if(!instr)
|
2011-09-12 12:32:50 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
instr_ptr(ctx, instr)->arg1.bstr = bstr;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-08 14:57:16 +02:00
|
|
|
static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
|
2011-09-07 14:07:12 +02:00
|
|
|
{
|
2011-09-07 14:08:47 +02:00
|
|
|
unsigned instr;
|
2011-09-08 14:54:23 +02:00
|
|
|
BSTR bstr;
|
|
|
|
|
2011-09-08 14:57:16 +02:00
|
|
|
bstr = alloc_bstr_arg(ctx, arg1);
|
2011-09-08 14:54:23 +02:00
|
|
|
if(!bstr)
|
|
|
|
return E_OUTOFMEMORY;
|
2011-09-07 14:07:12 +02:00
|
|
|
|
2011-09-07 14:08:47 +02:00
|
|
|
instr = push_instr(ctx, op);
|
2012-01-16 12:40:26 +01:00
|
|
|
if(!instr)
|
2011-09-07 14:07:12 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2011-09-08 14:54:23 +02:00
|
|
|
instr_ptr(ctx, instr)->arg1.bstr = bstr;
|
2011-09-08 14:57:16 +02:00
|
|
|
instr_ptr(ctx, instr)->arg2.uint = arg2;
|
2011-09-07 14:08:47 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-03 17:05:32 +02:00
|
|
|
static HRESULT push_instr_uint_bstr(compile_ctx_t *ctx, vbsop_t op, unsigned arg1, const WCHAR *arg2)
|
|
|
|
{
|
|
|
|
unsigned instr;
|
|
|
|
BSTR bstr;
|
|
|
|
|
|
|
|
bstr = alloc_bstr_arg(ctx, arg2);
|
|
|
|
if(!bstr)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
instr = push_instr(ctx, op);
|
|
|
|
if(!instr)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
instr_ptr(ctx, instr)->arg1.uint = arg1;
|
|
|
|
instr_ptr(ctx, instr)->arg2.bstr = bstr;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-13 11:38:38 +02:00
|
|
|
#define LABEL_FLAG 0x80000000
|
|
|
|
|
|
|
|
static unsigned alloc_label(compile_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
if(!ctx->labels_size) {
|
|
|
|
ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
|
|
|
|
if(!ctx->labels)
|
2012-01-16 12:40:50 +01:00
|
|
|
return 0;
|
2011-09-13 11:38:38 +02:00
|
|
|
ctx->labels_size = 8;
|
|
|
|
}else if(ctx->labels_size == ctx->labels_cnt) {
|
|
|
|
unsigned *new_labels;
|
|
|
|
|
|
|
|
new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
|
|
|
|
if(!new_labels)
|
2012-01-16 12:40:50 +01:00
|
|
|
return 0;
|
2011-09-13 11:38:38 +02:00
|
|
|
|
|
|
|
ctx->labels = new_labels;
|
|
|
|
ctx->labels_size *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx->labels_cnt++ | LABEL_FLAG;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
|
|
|
|
{
|
|
|
|
assert(label & LABEL_FLAG);
|
|
|
|
ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
|
|
|
|
}
|
|
|
|
|
2011-09-21 14:03:38 +02:00
|
|
|
static expression_t *lookup_const_decls(compile_ctx_t *ctx, const WCHAR *name, BOOL lookup_global)
|
|
|
|
{
|
|
|
|
const_decl_t *decl;
|
|
|
|
|
|
|
|
for(decl = ctx->const_decls; decl; decl = decl->next) {
|
|
|
|
if(!strcmpiW(decl->name, name))
|
|
|
|
return decl->value_expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!lookup_global)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for(decl = ctx->global_consts; decl; decl = decl->next) {
|
|
|
|
if(!strcmpiW(decl->name, name))
|
|
|
|
return decl->value_expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-08 14:57:16 +02:00
|
|
|
static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
|
2011-09-07 14:08:47 +02:00
|
|
|
{
|
2011-09-08 14:57:16 +02:00
|
|
|
unsigned arg_cnt = 0;
|
2011-09-07 14:08:47 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
2011-09-08 14:57:16 +02:00
|
|
|
while(args) {
|
|
|
|
hres = compile_expression(ctx, args);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
arg_cnt++;
|
|
|
|
args = args->next;
|
2011-09-07 14:08:21 +02:00
|
|
|
}
|
|
|
|
|
2011-09-08 14:57:16 +02:00
|
|
|
*ret = arg_cnt;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-09 14:48:48 +02:00
|
|
|
static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
|
2011-09-08 14:57:16 +02:00
|
|
|
{
|
|
|
|
unsigned arg_cnt = 0;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2011-09-21 14:03:38 +02:00
|
|
|
if(ret_val && !expr->args) {
|
|
|
|
expression_t *const_expr;
|
|
|
|
|
|
|
|
const_expr = lookup_const_decls(ctx, expr->identifier, TRUE);
|
|
|
|
if(const_expr)
|
|
|
|
return compile_expression(ctx, const_expr);
|
|
|
|
}
|
|
|
|
|
2011-09-08 14:57:16 +02:00
|
|
|
hres = compile_args(ctx, expr->args, &arg_cnt);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2011-09-07 14:08:47 +02:00
|
|
|
if(expr->obj_expr) {
|
2011-09-15 14:19:41 +02:00
|
|
|
hres = compile_expression(ctx, expr->obj_expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
hres = push_instr_bstr_uint(ctx, ret_val ? OP_mcall : OP_mcallv, expr->identifier, arg_cnt);
|
2011-09-07 14:08:47 +02:00
|
|
|
}else {
|
2011-09-09 14:48:48 +02:00
|
|
|
hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
|
2011-09-07 14:08:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2011-09-09 14:47:45 +02:00
|
|
|
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;
|
|
|
|
|
2012-01-16 12:40:26 +01:00
|
|
|
return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
|
2011-09-09 14:47:45 +02:00
|
|
|
}
|
|
|
|
|
2011-09-09 14:48:25 +02:00
|
|
|
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;
|
|
|
|
|
2012-01-16 12:40:26 +01:00
|
|
|
return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
|
2011-09-09 14:48:25 +02:00
|
|
|
}
|
|
|
|
|
2011-09-08 14:57:16 +02:00
|
|
|
static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
|
|
|
|
{
|
|
|
|
switch(expr->type) {
|
2011-09-12 12:31:58 +02:00
|
|
|
case EXPR_ADD:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
|
2011-09-14 12:59:02 +02:00
|
|
|
case EXPR_AND:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
|
2011-09-08 14:57:16 +02:00
|
|
|
case EXPR_BOOL:
|
|
|
|
return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
|
2012-07-03 17:04:51 +02:00
|
|
|
case EXPR_BRACKETS:
|
|
|
|
return compile_expression(ctx, ((unary_expression_t*)expr)->subexpr);
|
2011-09-12 12:30:47 +02:00
|
|
|
case EXPR_CONCAT:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
|
2011-09-13 11:40:39 +02:00
|
|
|
case EXPR_DIV:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
|
2011-09-12 12:30:11 +02:00
|
|
|
case EXPR_DOUBLE:
|
|
|
|
return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
|
2011-09-09 14:49:26 +02:00
|
|
|
case EXPR_EMPTY:
|
2012-01-16 12:40:26 +01:00
|
|
|
return push_instr(ctx, OP_empty) ? S_OK : E_OUTOFMEMORY;
|
2011-09-09 14:48:25 +02:00
|
|
|
case EXPR_EQUAL:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
|
2011-09-14 12:59:49 +02:00
|
|
|
case EXPR_EQV:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eqv);
|
2011-09-13 11:41:15 +02:00
|
|
|
case EXPR_EXP:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
|
2011-09-19 14:07:12 +02:00
|
|
|
case EXPR_GT:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
|
|
|
|
case EXPR_GTEQ:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
|
2011-09-13 11:40:14 +02:00
|
|
|
case EXPR_IDIV:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
|
2011-09-19 14:08:02 +02:00
|
|
|
case EXPR_IS:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_is);
|
2011-09-14 12:59:49 +02:00
|
|
|
case EXPR_IMP:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_imp);
|
2011-09-19 14:07:12 +02:00
|
|
|
case EXPR_LT:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
|
|
|
|
case EXPR_LTEQ:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
|
2011-09-19 14:09:57 +02:00
|
|
|
case EXPR_ME:
|
2012-01-16 12:40:26 +01:00
|
|
|
return push_instr(ctx, OP_me) ? S_OK : E_OUTOFMEMORY;
|
2011-09-09 14:48:48 +02:00
|
|
|
case EXPR_MEMBER:
|
|
|
|
return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
|
2011-09-13 11:39:53 +02:00
|
|
|
case EXPR_MOD:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
|
2011-09-13 11:40:39 +02:00
|
|
|
case EXPR_MUL:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
|
2011-09-12 12:31:37 +02:00
|
|
|
case EXPR_NEG:
|
|
|
|
return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
|
2011-09-12 12:32:27 +02:00
|
|
|
case EXPR_NEQUAL:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
|
2011-09-15 14:18:12 +02:00
|
|
|
case EXPR_NEW:
|
|
|
|
return push_instr_str(ctx, OP_new, ((string_expression_t*)expr)->value);
|
2011-09-09 14:47:45 +02:00
|
|
|
case EXPR_NOT:
|
|
|
|
return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
|
2011-09-15 14:18:56 +02:00
|
|
|
case EXPR_NOTHING:
|
2012-01-16 12:40:26 +01:00
|
|
|
return push_instr(ctx, OP_nothing) ? S_OK : E_OUTOFMEMORY;
|
2011-09-09 14:49:36 +02:00
|
|
|
case EXPR_NULL:
|
2012-01-16 12:40:26 +01:00
|
|
|
return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
|
2011-09-14 12:59:26 +02:00
|
|
|
case EXPR_OR:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
|
2011-09-08 14:57:31 +02:00
|
|
|
case EXPR_STRING:
|
|
|
|
return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
|
2011-09-12 12:31:58 +02:00
|
|
|
case EXPR_SUB:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
|
2011-09-12 12:30:11 +02:00
|
|
|
case EXPR_USHORT:
|
|
|
|
return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
|
|
|
|
case EXPR_ULONG:
|
|
|
|
return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
|
2011-09-14 12:59:49 +02:00
|
|
|
case EXPR_XOR:
|
|
|
|
return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
|
2011-09-08 14:57:16 +02:00
|
|
|
default:
|
|
|
|
FIXME("Unimplemented expression type %d\n", expr->type);
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-13 11:38:38 +02:00
|
|
|
static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
|
|
|
|
{
|
2012-01-16 12:40:50 +01:00
|
|
|
unsigned cnd_jmp, endif_label = 0;
|
2011-09-13 11:38:38 +02:00
|
|
|
elseif_decl_t *elseif_decl;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
hres = compile_expression(ctx, stat->expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
cnd_jmp = push_instr(ctx, OP_jmp_false);
|
2012-01-16 12:40:26 +01:00
|
|
|
if(!cnd_jmp)
|
2011-09-13 11:38:38 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-07-03 19:17:07 +02:00
|
|
|
hres = compile_statement(ctx, NULL, stat->if_stat);
|
2011-09-13 11:38:38 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(stat->else_stat || stat->elseifs) {
|
|
|
|
endif_label = alloc_label(ctx);
|
2012-01-16 12:40:50 +01:00
|
|
|
if(!endif_label)
|
2011-09-13 11:38:38 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
hres = push_instr_addr(ctx, OP_jmp, endif_label);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
|
|
|
|
instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
|
|
|
|
|
|
|
|
hres = compile_expression(ctx, elseif_decl->expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
cnd_jmp = push_instr(ctx, OP_jmp_false);
|
2012-01-16 12:40:26 +01:00
|
|
|
if(!cnd_jmp)
|
2011-09-13 11:38:38 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-07-03 19:17:07 +02:00
|
|
|
hres = compile_statement(ctx, NULL, elseif_decl->stat);
|
2011-09-13 11:38:38 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
hres = push_instr_addr(ctx, OP_jmp, endif_label);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
|
|
|
|
|
|
|
|
if(stat->else_stat) {
|
2012-07-03 19:17:07 +02:00
|
|
|
hres = compile_statement(ctx, NULL, stat->else_stat);
|
2011-09-13 11:38:38 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2012-01-16 12:40:50 +01:00
|
|
|
if(endif_label)
|
2011-09-13 11:38:38 +02:00
|
|
|
label_set_addr(ctx, endif_label);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-16 13:29:59 +02:00
|
|
|
static HRESULT compile_while_statement(compile_ctx_t *ctx, while_statement_t *stat)
|
|
|
|
{
|
2012-07-03 19:17:07 +02:00
|
|
|
statement_ctx_t stat_ctx = {0}, *loop_ctx;
|
|
|
|
unsigned start_addr;
|
2011-09-16 13:29:59 +02:00
|
|
|
unsigned jmp_end;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
start_addr = ctx->instr_cnt;
|
|
|
|
|
|
|
|
hres = compile_expression(ctx, stat->expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2011-09-16 13:30:31 +02:00
|
|
|
jmp_end = push_instr(ctx, stat->stat.type == STAT_UNTIL ? OP_jmp_true : OP_jmp_false);
|
2012-01-16 12:40:26 +01:00
|
|
|
if(!jmp_end)
|
2011-09-16 13:29:59 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-07-03 19:17:07 +02:00
|
|
|
if(stat->stat.type == STAT_WHILE) {
|
|
|
|
loop_ctx = NULL;
|
|
|
|
}else {
|
|
|
|
if(!(stat_ctx.while_end_label = alloc_label(ctx)))
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
loop_ctx = &stat_ctx;
|
|
|
|
}
|
2011-09-16 13:30:20 +02:00
|
|
|
|
2012-07-03 19:17:07 +02:00
|
|
|
hres = compile_statement(ctx, loop_ctx, stat->body);
|
2011-09-16 13:29:59 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
hres = push_instr_addr(ctx, OP_jmp, start_addr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
instr_ptr(ctx, jmp_end)->arg1.uint = ctx->instr_cnt;
|
2011-09-16 13:30:20 +02:00
|
|
|
|
2012-07-03 19:17:07 +02:00
|
|
|
if(loop_ctx)
|
|
|
|
label_set_addr(ctx, stat_ctx.while_end_label);
|
2011-09-16 13:30:20 +02:00
|
|
|
|
2011-09-16 13:29:59 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-16 13:30:39 +02:00
|
|
|
static HRESULT compile_dowhile_statement(compile_ctx_t *ctx, while_statement_t *stat)
|
|
|
|
{
|
2012-07-03 19:17:07 +02:00
|
|
|
statement_ctx_t loop_ctx = {0};
|
|
|
|
unsigned start_addr;
|
2012-09-18 12:25:42 +02:00
|
|
|
vbsop_t jmp_op;
|
2011-09-16 13:30:39 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
start_addr = ctx->instr_cnt;
|
|
|
|
|
2012-07-03 19:17:07 +02:00
|
|
|
if(!(loop_ctx.while_end_label = alloc_label(ctx)))
|
2011-09-16 13:30:39 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-07-03 19:17:07 +02:00
|
|
|
hres = compile_statement(ctx, &loop_ctx, stat->body);
|
2011-09-16 13:30:39 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-09-18 12:25:42 +02:00
|
|
|
if(stat->expr) {
|
|
|
|
hres = compile_expression(ctx, stat->expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
jmp_op = stat->stat.type == STAT_DOUNTIL ? OP_jmp_false : OP_jmp_true;
|
|
|
|
}else {
|
|
|
|
jmp_op = OP_jmp;
|
|
|
|
}
|
2011-09-16 13:30:39 +02:00
|
|
|
|
2012-09-18 12:25:42 +02:00
|
|
|
hres = push_instr_addr(ctx, jmp_op, start_addr);
|
2011-09-16 13:30:39 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-07-03 19:17:07 +02:00
|
|
|
label_set_addr(ctx, loop_ctx.while_end_label);
|
2011-09-16 13:30:39 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-04 11:50:29 +01:00
|
|
|
static HRESULT compile_foreach_statement(compile_ctx_t *ctx, foreach_statement_t *stat)
|
|
|
|
{
|
2012-07-03 19:18:00 +02:00
|
|
|
statement_ctx_t loop_ctx = {1};
|
|
|
|
unsigned loop_start;
|
2012-07-03 17:05:32 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
hres = compile_expression(ctx, stat->group_expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(!push_instr(ctx, OP_newenum))
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
loop_start = ctx->instr_cnt;
|
2012-07-03 19:18:00 +02:00
|
|
|
if(!(loop_ctx.for_end_label = alloc_label(ctx)))
|
2012-07-03 17:05:32 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-07-03 19:18:00 +02:00
|
|
|
hres = push_instr_uint_bstr(ctx, OP_enumnext, loop_ctx.for_end_label, stat->identifier);
|
2012-07-03 17:05:32 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-07-03 19:18:00 +02:00
|
|
|
hres = compile_statement(ctx, &loop_ctx, stat->body);
|
2012-07-03 17:05:32 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
hres = push_instr_addr(ctx, OP_jmp, loop_start);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-07-03 19:18:00 +02:00
|
|
|
label_set_addr(ctx, loop_ctx.for_end_label);
|
2012-07-03 17:05:32 +02:00
|
|
|
return S_OK;
|
2012-01-04 11:50:29 +01:00
|
|
|
}
|
|
|
|
|
2011-09-22 14:23:10 +02:00
|
|
|
static HRESULT compile_forto_statement(compile_ctx_t *ctx, forto_statement_t *stat)
|
|
|
|
{
|
2012-07-03 19:17:23 +02:00
|
|
|
statement_ctx_t loop_ctx = {2};
|
2012-07-03 19:17:07 +02:00
|
|
|
unsigned step_instr, instr;
|
2011-09-22 14:23:10 +02:00
|
|
|
BSTR identifier;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
identifier = alloc_bstr_arg(ctx, stat->identifier);
|
|
|
|
if(!identifier)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
hres = compile_expression(ctx, stat->from_expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
instr = push_instr(ctx, OP_assign_ident);
|
2012-01-16 12:40:26 +01:00
|
|
|
if(!instr)
|
2011-09-22 14:23:10 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
instr_ptr(ctx, instr)->arg1.bstr = identifier;
|
2012-09-06 11:57:00 +02:00
|
|
|
instr_ptr(ctx, instr)->arg2.uint = 0;
|
2011-09-22 14:23:10 +02:00
|
|
|
|
|
|
|
hres = compile_expression(ctx, stat->to_expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-01-16 12:40:26 +01:00
|
|
|
if(!push_instr(ctx, OP_val))
|
2011-09-22 14:23:10 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
if(stat->step_expr) {
|
|
|
|
hres = compile_expression(ctx, stat->step_expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-01-16 12:40:26 +01:00
|
|
|
if(!push_instr(ctx, OP_val))
|
2011-09-22 14:23:10 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}else {
|
|
|
|
hres = push_instr_int(ctx, OP_short, 1);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2012-07-03 19:17:07 +02:00
|
|
|
loop_ctx.for_end_label = alloc_label(ctx);
|
|
|
|
if(!loop_ctx.for_end_label)
|
2011-09-22 14:24:44 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2011-09-22 14:23:10 +02:00
|
|
|
step_instr = push_instr(ctx, OP_step);
|
2012-01-16 12:40:26 +01:00
|
|
|
if(!step_instr)
|
2011-09-22 14:23:10 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
instr_ptr(ctx, step_instr)->arg2.bstr = identifier;
|
2012-07-03 19:17:07 +02:00
|
|
|
instr_ptr(ctx, step_instr)->arg1.uint = loop_ctx.for_end_label;
|
2011-09-22 14:23:10 +02:00
|
|
|
|
2012-07-03 19:17:07 +02:00
|
|
|
hres = compile_statement(ctx, &loop_ctx, stat->body);
|
2011-09-22 14:23:10 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
instr = push_instr(ctx, OP_incc);
|
2012-01-16 12:40:26 +01:00
|
|
|
if(!instr)
|
2011-09-22 14:23:10 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
instr_ptr(ctx, instr)->arg1.bstr = identifier;
|
|
|
|
|
|
|
|
hres = push_instr_addr(ctx, OP_jmp, step_instr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-07-03 19:17:23 +02:00
|
|
|
hres = push_instr_uint(ctx, OP_pop, 2);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
2011-09-22 14:23:10 +02:00
|
|
|
|
2012-07-03 19:17:23 +02:00
|
|
|
label_set_addr(ctx, loop_ctx.for_end_label);
|
|
|
|
return S_OK;
|
2011-09-22 14:23:10 +02:00
|
|
|
}
|
|
|
|
|
2012-07-20 16:28:32 +02:00
|
|
|
static HRESULT compile_select_statement(compile_ctx_t *ctx, select_statement_t *stat)
|
|
|
|
{
|
2012-07-20 16:28:45 +02:00
|
|
|
unsigned end_label, case_cnt = 0, *case_labels = NULL, i;
|
|
|
|
case_clausule_t *case_iter;
|
|
|
|
expression_t *expr_iter;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
hres = compile_expression(ctx, stat->expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(!push_instr(ctx, OP_val))
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
end_label = alloc_label(ctx);
|
|
|
|
if(!end_label)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
for(case_iter = stat->case_clausules; case_iter; case_iter = case_iter->next)
|
|
|
|
case_cnt++;
|
|
|
|
|
|
|
|
if(case_cnt) {
|
|
|
|
case_labels = heap_alloc(case_cnt*sizeof(*case_labels));
|
|
|
|
if(!case_labels)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(case_iter = stat->case_clausules, i=0; case_iter; case_iter = case_iter->next, i++) {
|
|
|
|
case_labels[i] = alloc_label(ctx);
|
|
|
|
if(!case_labels[i]) {
|
|
|
|
hres = E_OUTOFMEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!case_iter->expr)
|
|
|
|
break;
|
|
|
|
|
|
|
|
for(expr_iter = case_iter->expr; expr_iter; expr_iter = expr_iter->next) {
|
|
|
|
hres = compile_expression(ctx, expr_iter);
|
|
|
|
if(FAILED(hres))
|
|
|
|
break;
|
|
|
|
|
|
|
|
hres = push_instr_addr(ctx, OP_case, case_labels[i]);
|
|
|
|
if(FAILED(hres))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(FAILED(hres)) {
|
|
|
|
heap_free(case_labels);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
hres = push_instr_uint(ctx, OP_pop, 1);
|
|
|
|
if(FAILED(hres)) {
|
|
|
|
heap_free(case_labels);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
hres = push_instr_addr(ctx, OP_jmp, case_iter ? case_labels[i] : end_label);
|
|
|
|
if(FAILED(hres)) {
|
|
|
|
heap_free(case_labels);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(case_iter = stat->case_clausules, i=0; case_iter; case_iter = case_iter->next, i++) {
|
|
|
|
label_set_addr(ctx, case_labels[i]);
|
|
|
|
hres = compile_statement(ctx, NULL, case_iter->stat);
|
|
|
|
if(FAILED(hres))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(!case_iter->next)
|
|
|
|
break;
|
|
|
|
|
|
|
|
hres = push_instr_addr(ctx, OP_jmp, end_label);
|
|
|
|
if(FAILED(hres))
|
2012-10-30 20:07:10 +01:00
|
|
|
break;
|
2012-07-20 16:28:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
heap_free(case_labels);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
label_set_addr(ctx, end_label);
|
|
|
|
return S_OK;
|
2012-07-20 16:28:32 +02:00
|
|
|
}
|
|
|
|
|
2012-07-03 17:04:51 +02:00
|
|
|
static HRESULT compile_assignment(compile_ctx_t *ctx, member_expression_t *member_expr, expression_t *value_expr, BOOL is_set)
|
2011-09-12 12:32:50 +02:00
|
|
|
{
|
2011-12-27 16:17:21 +01:00
|
|
|
unsigned args_cnt;
|
|
|
|
vbsop_t op;
|
2011-09-12 12:32:50 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
2012-07-03 17:04:51 +02:00
|
|
|
if(member_expr->obj_expr) {
|
|
|
|
hres = compile_expression(ctx, member_expr->obj_expr);
|
2011-09-13 11:35:33 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2011-12-27 16:17:21 +01:00
|
|
|
op = is_set ? OP_set_member : OP_assign_member;
|
2011-09-12 12:32:50 +02:00
|
|
|
}else {
|
2011-12-27 16:17:21 +01:00
|
|
|
op = is_set ? OP_set_ident : OP_assign_ident;
|
2011-09-12 12:32:50 +02:00
|
|
|
}
|
|
|
|
|
2012-07-03 17:04:51 +02:00
|
|
|
hres = compile_expression(ctx, value_expr);
|
2012-03-26 11:53:08 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-07-03 17:04:51 +02:00
|
|
|
hres = compile_args(ctx, member_expr->args, &args_cnt);
|
2011-12-27 16:17:21 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-07-03 17:04:51 +02:00
|
|
|
return push_instr_bstr_uint(ctx, op, member_expr->identifier, args_cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat, BOOL is_set)
|
|
|
|
{
|
|
|
|
return compile_assignment(ctx, stat->member_expr, stat->value_expr, is_set);
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT compile_call_statement(compile_ctx_t *ctx, call_statement_t *stat)
|
|
|
|
{
|
|
|
|
/* It's challenging for parser to distinguish parameterized assignment with one argument from call
|
|
|
|
* with equality expression argument, so we do it in compiler. */
|
|
|
|
if(!stat->is_strict && stat->expr->args && !stat->expr->args->next && stat->expr->args->type == EXPR_EQUAL) {
|
|
|
|
binary_expression_t *eqexpr = (binary_expression_t*)stat->expr->args;
|
|
|
|
|
|
|
|
if(eqexpr->left->type == EXPR_BRACKETS) {
|
|
|
|
member_expression_t new_member = *stat->expr;
|
|
|
|
|
|
|
|
WARN("converting call expr to assign expr\n");
|
|
|
|
|
|
|
|
new_member.args = ((unary_expression_t*)eqexpr->left)->subexpr;
|
|
|
|
return compile_assignment(ctx, &new_member, eqexpr->right, FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return compile_member_expression(ctx, stat->expr, FALSE);
|
2011-09-12 12:32:50 +02:00
|
|
|
}
|
|
|
|
|
2011-09-13 11:36:47 +02:00
|
|
|
static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
|
|
|
|
{
|
|
|
|
dim_decl_t *dim_decl;
|
|
|
|
|
|
|
|
for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
|
|
|
|
if(!strcmpiW(dim_decl->name, name))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-09-14 12:57:37 +02:00
|
|
|
static BOOL lookup_args_name(compile_ctx_t *ctx, const WCHAR *name)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for(i = 0; i < ctx->func->arg_cnt; i++) {
|
|
|
|
if(!strcmpiW(ctx->func->args[i].name, name))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-09-13 11:36:47 +02:00
|
|
|
static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
|
|
|
|
{
|
|
|
|
dim_decl_t *dim_decl = stat->dim_decls;
|
|
|
|
|
|
|
|
while(1) {
|
2011-09-21 14:03:38 +02:00
|
|
|
if(lookup_dim_decls(ctx, dim_decl->name) || lookup_args_name(ctx, dim_decl->name)
|
|
|
|
|| lookup_const_decls(ctx, dim_decl->name, FALSE)) {
|
2011-09-13 11:36:47 +02:00
|
|
|
FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2011-10-24 12:30:48 +02:00
|
|
|
ctx->func->var_cnt++;
|
2013-11-13 16:28:55 +01:00
|
|
|
|
|
|
|
if(dim_decl->is_array) {
|
2013-11-13 16:29:18 +01:00
|
|
|
HRESULT hres = push_instr_bstr_uint(ctx, OP_dim, dim_decl->name, ctx->func->array_cnt++);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
2013-11-13 16:28:55 +01:00
|
|
|
}
|
|
|
|
|
2011-09-13 11:36:47 +02:00
|
|
|
if(!dim_decl->next)
|
|
|
|
break;
|
|
|
|
dim_decl = dim_decl->next;
|
|
|
|
}
|
|
|
|
|
2013-11-13 16:29:05 +01:00
|
|
|
if(ctx->dim_decls_tail)
|
|
|
|
ctx->dim_decls_tail->next = stat->dim_decls;
|
|
|
|
else
|
|
|
|
ctx->dim_decls = stat->dim_decls;
|
|
|
|
ctx->dim_decls_tail = dim_decl;
|
2011-09-13 11:36:47 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-21 14:03:38 +02:00
|
|
|
static HRESULT compile_const_statement(compile_ctx_t *ctx, const_statement_t *stat)
|
|
|
|
{
|
|
|
|
const_decl_t *decl, *next_decl = stat->decls;
|
|
|
|
|
|
|
|
do {
|
|
|
|
decl = next_decl;
|
|
|
|
|
|
|
|
if(lookup_const_decls(ctx, decl->name, FALSE) || lookup_args_name(ctx, decl->name)
|
|
|
|
|| lookup_dim_decls(ctx, decl->name)) {
|
|
|
|
FIXME("%s redefined\n", debugstr_w(decl->name));
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ctx->func->type == FUNC_GLOBAL) {
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
hres = compile_expression(ctx, decl->value_expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
hres = push_instr_bstr(ctx, OP_const, decl->name);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
next_decl = decl->next;
|
|
|
|
decl->next = ctx->const_decls;
|
|
|
|
ctx->const_decls = decl;
|
|
|
|
} while(next_decl);
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-14 12:55:07 +02:00
|
|
|
static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
|
|
|
|
{
|
2012-09-06 11:57:15 +02:00
|
|
|
if(ctx->func != &ctx->code->main_code) {
|
2011-09-14 12:55:07 +02:00
|
|
|
FIXME("Function is not in the global code\n");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
stat->func_decl->next = ctx->func_decls;
|
|
|
|
ctx->func_decls = stat->func_decl;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-16 13:30:20 +02:00
|
|
|
static HRESULT compile_exitdo_statement(compile_ctx_t *ctx)
|
|
|
|
{
|
2012-07-03 19:17:07 +02:00
|
|
|
statement_ctx_t *iter;
|
|
|
|
unsigned pop_cnt = 0;
|
|
|
|
|
|
|
|
for(iter = ctx->stat_ctx; iter; iter = iter->next) {
|
|
|
|
pop_cnt += iter->stack_use;
|
|
|
|
if(iter->while_end_label)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!iter) {
|
2011-09-16 13:30:20 +02:00
|
|
|
FIXME("Exit Do outside Do Loop\n");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2012-07-03 19:17:07 +02:00
|
|
|
if(pop_cnt) {
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
hres = push_instr_uint(ctx, OP_pop, pop_cnt);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
return push_instr_addr(ctx, OP_jmp, iter->while_end_label);
|
2011-09-16 13:30:20 +02:00
|
|
|
}
|
|
|
|
|
2011-09-22 14:24:44 +02:00
|
|
|
static HRESULT compile_exitfor_statement(compile_ctx_t *ctx)
|
|
|
|
{
|
2012-07-03 19:17:07 +02:00
|
|
|
statement_ctx_t *iter;
|
|
|
|
unsigned pop_cnt = 0;
|
|
|
|
|
|
|
|
for(iter = ctx->stat_ctx; iter; iter = iter->next) {
|
|
|
|
pop_cnt += iter->stack_use;
|
|
|
|
if(iter->for_end_label)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!iter) {
|
|
|
|
FIXME("Exit For outside For loop\n");
|
2011-09-22 14:24:44 +02:00
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2012-07-03 19:17:07 +02:00
|
|
|
if(pop_cnt) {
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
hres = push_instr_uint(ctx, OP_pop, pop_cnt);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
return push_instr_addr(ctx, OP_jmp, iter->for_end_label);
|
2011-09-22 14:24:44 +02:00
|
|
|
}
|
|
|
|
|
2012-07-03 19:17:47 +02:00
|
|
|
static HRESULT exit_label(compile_ctx_t *ctx, unsigned jmp_label)
|
|
|
|
{
|
|
|
|
statement_ctx_t *iter;
|
|
|
|
unsigned pop_cnt = 0;
|
|
|
|
|
|
|
|
for(iter = ctx->stat_ctx; iter; iter = iter->next)
|
|
|
|
pop_cnt += iter->stack_use;
|
|
|
|
|
|
|
|
if(pop_cnt) {
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
hres = push_instr_uint(ctx, OP_pop, pop_cnt);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
return push_instr_addr(ctx, OP_jmp, jmp_label);
|
|
|
|
}
|
|
|
|
|
2011-09-14 12:57:27 +02:00
|
|
|
static HRESULT compile_exitsub_statement(compile_ctx_t *ctx)
|
|
|
|
{
|
2012-01-16 12:40:50 +01:00
|
|
|
if(!ctx->sub_end_label) {
|
2011-09-14 12:57:27 +02:00
|
|
|
FIXME("Exit Sub outside Sub?\n");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2012-07-03 19:17:47 +02:00
|
|
|
return exit_label(ctx, ctx->sub_end_label);
|
2011-09-14 12:57:27 +02:00
|
|
|
}
|
|
|
|
|
2011-09-14 12:58:10 +02:00
|
|
|
static HRESULT compile_exitfunc_statement(compile_ctx_t *ctx)
|
|
|
|
{
|
2012-01-16 12:40:50 +01:00
|
|
|
if(!ctx->func_end_label) {
|
2011-09-14 12:58:10 +02:00
|
|
|
FIXME("Exit Function outside Function?\n");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2012-07-03 19:17:47 +02:00
|
|
|
return exit_label(ctx, ctx->func_end_label);
|
2011-09-14 12:58:10 +02:00
|
|
|
}
|
|
|
|
|
2011-09-16 13:28:29 +02:00
|
|
|
static HRESULT compile_exitprop_statement(compile_ctx_t *ctx)
|
|
|
|
{
|
2012-01-16 12:40:50 +01:00
|
|
|
if(!ctx->prop_end_label) {
|
2011-09-16 13:28:29 +02:00
|
|
|
FIXME("Exit Property outside Property?\n");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2012-07-03 19:17:47 +02:00
|
|
|
return exit_label(ctx, ctx->prop_end_label);
|
2011-09-16 13:28:29 +02:00
|
|
|
}
|
|
|
|
|
2011-09-19 14:10:19 +02:00
|
|
|
static HRESULT compile_onerror_statement(compile_ctx_t *ctx, onerror_statement_t *stat)
|
|
|
|
{
|
|
|
|
return push_instr_int(ctx, OP_errmode, stat->resume_next);
|
|
|
|
}
|
|
|
|
|
2012-07-03 19:17:07 +02:00
|
|
|
static HRESULT compile_statement(compile_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
|
2011-09-07 14:08:47 +02:00
|
|
|
{
|
|
|
|
HRESULT hres;
|
|
|
|
|
2012-07-03 19:17:07 +02:00
|
|
|
if(stat_ctx) {
|
|
|
|
stat_ctx->next = ctx->stat_ctx;
|
|
|
|
ctx->stat_ctx = stat_ctx;
|
|
|
|
}
|
|
|
|
|
2011-09-07 14:08:47 +02:00
|
|
|
while(stat) {
|
|
|
|
switch(stat->type) {
|
2011-09-12 12:32:50 +02:00
|
|
|
case STAT_ASSIGN:
|
2011-09-15 14:17:38 +02:00
|
|
|
hres = compile_assign_statement(ctx, (assign_statement_t*)stat, FALSE);
|
2011-09-12 12:32:50 +02:00
|
|
|
break;
|
2011-09-07 14:08:47 +02:00
|
|
|
case STAT_CALL:
|
2012-07-03 17:04:51 +02:00
|
|
|
hres = compile_call_statement(ctx, (call_statement_t*)stat);
|
2011-09-07 14:08:47 +02:00
|
|
|
break;
|
2011-09-21 14:03:38 +02:00
|
|
|
case STAT_CONST:
|
|
|
|
hres = compile_const_statement(ctx, (const_statement_t*)stat);
|
|
|
|
break;
|
2011-09-13 11:36:47 +02:00
|
|
|
case STAT_DIM:
|
|
|
|
hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
|
|
|
|
break;
|
2011-09-16 13:30:39 +02:00
|
|
|
case STAT_DOWHILE:
|
|
|
|
case STAT_DOUNTIL:
|
|
|
|
hres = compile_dowhile_statement(ctx, (while_statement_t*)stat);
|
|
|
|
break;
|
2011-09-16 13:30:20 +02:00
|
|
|
case STAT_EXITDO:
|
|
|
|
hres = compile_exitdo_statement(ctx);
|
|
|
|
break;
|
2011-09-22 14:24:44 +02:00
|
|
|
case STAT_EXITFOR:
|
|
|
|
hres = compile_exitfor_statement(ctx);
|
|
|
|
break;
|
2011-09-14 12:58:10 +02:00
|
|
|
case STAT_EXITFUNC:
|
|
|
|
hres = compile_exitfunc_statement(ctx);
|
|
|
|
break;
|
2011-09-16 13:28:29 +02:00
|
|
|
case STAT_EXITPROP:
|
|
|
|
hres = compile_exitprop_statement(ctx);
|
|
|
|
break;
|
2011-09-14 12:57:27 +02:00
|
|
|
case STAT_EXITSUB:
|
|
|
|
hres = compile_exitsub_statement(ctx);
|
|
|
|
break;
|
2012-01-04 11:50:29 +01:00
|
|
|
case STAT_FOREACH:
|
|
|
|
hres = compile_foreach_statement(ctx, (foreach_statement_t*)stat);
|
|
|
|
break;
|
2011-09-22 14:23:10 +02:00
|
|
|
case STAT_FORTO:
|
|
|
|
hres = compile_forto_statement(ctx, (forto_statement_t*)stat);
|
|
|
|
break;
|
2011-09-14 12:55:07 +02:00
|
|
|
case STAT_FUNC:
|
|
|
|
hres = compile_function_statement(ctx, (function_statement_t*)stat);
|
|
|
|
break;
|
2011-09-13 11:38:38 +02:00
|
|
|
case STAT_IF:
|
|
|
|
hres = compile_if_statement(ctx, (if_statement_t*)stat);
|
|
|
|
break;
|
2011-09-19 14:10:19 +02:00
|
|
|
case STAT_ONERROR:
|
|
|
|
hres = compile_onerror_statement(ctx, (onerror_statement_t*)stat);
|
|
|
|
break;
|
2012-07-20 16:28:32 +02:00
|
|
|
case STAT_SELECT:
|
|
|
|
hres = compile_select_statement(ctx, (select_statement_t*)stat);
|
|
|
|
break;
|
2011-09-15 14:17:38 +02:00
|
|
|
case STAT_SET:
|
|
|
|
hres = compile_assign_statement(ctx, (assign_statement_t*)stat, TRUE);
|
|
|
|
break;
|
2011-09-15 14:22:27 +02:00
|
|
|
case STAT_STOP:
|
2012-01-16 12:40:26 +01:00
|
|
|
hres = push_instr(ctx, OP_stop) ? S_OK : E_OUTOFMEMORY;
|
2011-09-15 14:22:27 +02:00
|
|
|
break;
|
2011-09-16 13:30:31 +02:00
|
|
|
case STAT_UNTIL:
|
2011-09-16 13:29:59 +02:00
|
|
|
case STAT_WHILE:
|
2011-09-16 13:30:09 +02:00
|
|
|
case STAT_WHILELOOP:
|
2011-09-16 13:29:59 +02:00
|
|
|
hres = compile_while_statement(ctx, (while_statement_t*)stat);
|
|
|
|
break;
|
2011-09-07 14:08:47 +02:00
|
|
|
default:
|
|
|
|
FIXME("Unimplemented statement type %d\n", stat->type);
|
|
|
|
hres = E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
stat = stat->next;
|
|
|
|
}
|
|
|
|
|
2012-07-03 19:17:07 +02:00
|
|
|
if(stat_ctx) {
|
|
|
|
assert(ctx->stat_ctx == stat_ctx);
|
|
|
|
ctx->stat_ctx = stat_ctx->next;
|
|
|
|
}
|
|
|
|
|
2011-09-07 14:08:47 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-14 12:55:07 +02:00
|
|
|
static void resolve_labels(compile_ctx_t *ctx, unsigned off)
|
2011-09-13 11:38:38 +02:00
|
|
|
{
|
|
|
|
instr_t *instr;
|
|
|
|
|
2011-09-14 12:55:07 +02:00
|
|
|
for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
|
2011-09-13 11:38:38 +02:00
|
|
|
if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
|
|
|
|
assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
|
|
|
|
instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
|
|
|
|
}
|
|
|
|
assert(instr_info[instr->op].arg2_type != ARG_ADDR);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->labels_cnt = 0;
|
|
|
|
}
|
|
|
|
|
2011-09-07 14:08:47 +02:00
|
|
|
static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
|
|
|
|
{
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
func->code_off = ctx->instr_cnt;
|
|
|
|
|
2012-01-16 12:40:50 +01:00
|
|
|
ctx->sub_end_label = 0;
|
|
|
|
ctx->func_end_label = 0;
|
|
|
|
ctx->prop_end_label = 0;
|
2011-09-14 12:57:27 +02:00
|
|
|
|
|
|
|
switch(func->type) {
|
2011-09-14 12:58:10 +02:00
|
|
|
case FUNC_FUNCTION:
|
|
|
|
ctx->func_end_label = alloc_label(ctx);
|
2012-01-16 12:40:50 +01:00
|
|
|
if(!ctx->func_end_label)
|
|
|
|
return E_OUTOFMEMORY;
|
2011-09-14 12:58:10 +02:00
|
|
|
break;
|
2011-09-14 12:57:27 +02:00
|
|
|
case FUNC_SUB:
|
|
|
|
ctx->sub_end_label = alloc_label(ctx);
|
2012-01-16 12:40:50 +01:00
|
|
|
if(!ctx->sub_end_label)
|
2011-09-14 12:57:27 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
break;
|
2011-09-16 13:28:00 +02:00
|
|
|
case FUNC_PROPGET:
|
|
|
|
case FUNC_PROPLET:
|
|
|
|
case FUNC_PROPSET:
|
2011-09-16 13:28:52 +02:00
|
|
|
case FUNC_DEFGET:
|
2011-09-16 13:28:29 +02:00
|
|
|
ctx->prop_end_label = alloc_label(ctx);
|
2012-01-16 12:40:50 +01:00
|
|
|
if(!ctx->prop_end_label)
|
2011-09-16 13:28:29 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
break;
|
2011-09-14 12:57:27 +02:00
|
|
|
case FUNC_GLOBAL:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-09-14 12:55:07 +02:00
|
|
|
ctx->func = func;
|
2013-11-13 16:29:05 +01:00
|
|
|
ctx->dim_decls = ctx->dim_decls_tail = NULL;
|
2011-09-21 14:03:38 +02:00
|
|
|
ctx->const_decls = NULL;
|
2012-07-03 19:17:07 +02:00
|
|
|
hres = compile_statement(ctx, NULL, stat);
|
2011-09-14 12:55:07 +02:00
|
|
|
ctx->func = NULL;
|
2011-09-07 14:08:47 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-01-16 12:40:50 +01:00
|
|
|
if(ctx->sub_end_label)
|
2011-09-14 12:57:27 +02:00
|
|
|
label_set_addr(ctx, ctx->sub_end_label);
|
2012-01-16 12:40:50 +01:00
|
|
|
if(ctx->func_end_label)
|
2011-09-14 12:58:10 +02:00
|
|
|
label_set_addr(ctx, ctx->func_end_label);
|
2012-01-16 12:40:50 +01:00
|
|
|
if(ctx->prop_end_label)
|
2011-09-16 13:28:29 +02:00
|
|
|
label_set_addr(ctx, ctx->prop_end_label);
|
2011-09-14 12:57:27 +02:00
|
|
|
|
2012-01-16 12:40:26 +01:00
|
|
|
if(!push_instr(ctx, OP_ret))
|
2011-09-07 14:08:47 +02:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2011-09-14 12:55:07 +02:00
|
|
|
resolve_labels(ctx, func->code_off);
|
2011-09-13 11:38:38 +02:00
|
|
|
|
2011-09-14 12:57:37 +02:00
|
|
|
if(func->var_cnt) {
|
2011-09-13 11:36:47 +02:00
|
|
|
dim_decl_t *dim_decl;
|
|
|
|
|
2011-09-14 12:55:07 +02:00
|
|
|
if(func->type == FUNC_GLOBAL) {
|
|
|
|
dynamic_var_t *new_var;
|
2011-09-13 11:36:47 +02:00
|
|
|
|
2011-09-14 12:57:37 +02:00
|
|
|
func->var_cnt = 0;
|
|
|
|
|
2011-09-14 12:55:07 +02:00
|
|
|
for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
|
|
|
|
new_var = compiler_alloc(ctx->code, sizeof(*new_var));
|
|
|
|
if(!new_var)
|
|
|
|
return E_OUTOFMEMORY;
|
2011-09-13 11:36:47 +02:00
|
|
|
|
2011-09-14 12:55:07 +02:00
|
|
|
new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
|
|
|
|
if(!new_var->name)
|
|
|
|
return E_OUTOFMEMORY;
|
2011-09-13 11:36:47 +02:00
|
|
|
|
2011-09-14 12:55:07 +02:00
|
|
|
V_VT(&new_var->v) = VT_EMPTY;
|
2011-09-21 14:04:16 +02:00
|
|
|
new_var->is_const = FALSE;
|
2011-09-14 12:55:07 +02:00
|
|
|
|
|
|
|
new_var->next = ctx->global_vars;
|
|
|
|
ctx->global_vars = new_var;
|
|
|
|
}
|
|
|
|
}else {
|
2011-09-14 12:57:37 +02:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
func->vars = compiler_alloc(ctx->code, func->var_cnt * sizeof(var_desc_t));
|
|
|
|
if(!func->vars)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
for(dim_decl = ctx->dim_decls, i=0; dim_decl; dim_decl = dim_decl->next, i++) {
|
|
|
|
func->vars[i].name = compiler_alloc_string(ctx->code, dim_decl->name);
|
|
|
|
if(!func->vars[i].name)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(i == func->var_cnt);
|
2011-09-13 11:36:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-13 16:29:18 +01:00
|
|
|
if(func->array_cnt) {
|
|
|
|
unsigned dim_cnt, array_id = 0;
|
|
|
|
dim_decl_t *dim_decl;
|
|
|
|
dim_list_t *iter;
|
|
|
|
|
|
|
|
func->array_descs = compiler_alloc(ctx->code, func->array_cnt * sizeof(array_desc_t));
|
|
|
|
if(!func->array_descs)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
|
|
|
|
if(!dim_decl->is_array)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dim_cnt = 0;
|
|
|
|
for(iter = dim_decl->dims; iter; iter = iter->next)
|
|
|
|
dim_cnt++;
|
|
|
|
|
|
|
|
func->array_descs[array_id].bounds = compiler_alloc(ctx->code, dim_cnt * sizeof(SAFEARRAYBOUND));
|
|
|
|
if(!func->array_descs[array_id].bounds)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
func->array_descs[array_id].dim_cnt = dim_cnt;
|
|
|
|
|
|
|
|
dim_cnt = 0;
|
|
|
|
for(iter = dim_decl->dims; iter; iter = iter->next) {
|
|
|
|
func->array_descs[array_id].bounds[dim_cnt].cElements = iter->val+1;
|
|
|
|
func->array_descs[array_id].bounds[dim_cnt++].lLbound = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
array_id++;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(array_id == func->array_cnt);
|
|
|
|
}
|
|
|
|
|
2011-09-13 11:36:47 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-14 12:55:07 +02:00
|
|
|
static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
|
|
|
|
{
|
|
|
|
function_t *iter;
|
|
|
|
|
|
|
|
for(iter = ctx->funcs; iter; iter = iter->next) {
|
|
|
|
if(!strcmpiW(iter->name, name))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
|
|
|
|
{
|
|
|
|
function_t *func;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2011-09-21 14:03:38 +02:00
|
|
|
if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name) || lookup_const_decls(ctx, decl->name, FALSE)) {
|
2011-09-14 12:55:07 +02:00
|
|
|
FIXME("%s: redefinition\n", debugstr_w(decl->name));
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
func = compiler_alloc(ctx->code, sizeof(*func));
|
|
|
|
if(!func)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
func->name = compiler_alloc_string(ctx->code, decl->name);
|
|
|
|
if(!func->name)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2011-09-14 12:57:37 +02:00
|
|
|
func->vars = NULL;
|
|
|
|
func->var_cnt = 0;
|
2013-11-13 16:29:18 +01:00
|
|
|
func->array_cnt = 0;
|
2011-09-14 12:55:07 +02:00
|
|
|
func->code_ctx = ctx->code;
|
|
|
|
func->type = decl->type;
|
2011-09-15 14:20:05 +02:00
|
|
|
func->is_public = decl->is_public;
|
2011-09-14 12:55:07 +02:00
|
|
|
|
2011-09-14 13:55:57 +02:00
|
|
|
func->arg_cnt = 0;
|
2011-09-14 12:55:07 +02:00
|
|
|
if(decl->args) {
|
2011-09-14 13:55:57 +02:00
|
|
|
arg_decl_t *arg;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for(arg = decl->args; arg; arg = arg->next)
|
|
|
|
func->arg_cnt++;
|
|
|
|
|
|
|
|
func->args = compiler_alloc(ctx->code, func->arg_cnt * sizeof(arg_desc_t));
|
|
|
|
if(!func->args)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
for(i = 0, arg = decl->args; arg; arg = arg->next, i++) {
|
|
|
|
func->args[i].name = compiler_alloc_string(ctx->code, arg->name);
|
|
|
|
if(!func->args[i].name)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
func->args[i].by_ref = arg->by_ref;
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
func->args = NULL;
|
2011-09-14 12:55:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hres = compile_func(ctx, decl->body, func);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
*ret = func;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-15 14:17:17 +02:00
|
|
|
static BOOL lookup_class_name(compile_ctx_t *ctx, const WCHAR *name)
|
|
|
|
{
|
|
|
|
class_desc_t *iter;
|
|
|
|
|
|
|
|
for(iter = ctx->classes; iter; iter = iter->next) {
|
|
|
|
if(!strcmpiW(iter->name, name))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-09-15 14:20:46 +02:00
|
|
|
static HRESULT create_class_funcprop(compile_ctx_t *ctx, function_decl_t *func_decl, vbdisp_funcprop_desc_t *desc)
|
|
|
|
{
|
2011-09-16 13:28:18 +02:00
|
|
|
vbdisp_invoke_type_t invoke_type;
|
|
|
|
function_decl_t *funcprop_decl;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2011-09-15 14:20:46 +02:00
|
|
|
desc->name = compiler_alloc_string(ctx->code, func_decl->name);
|
|
|
|
if(!desc->name)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2011-09-16 13:28:18 +02:00
|
|
|
for(funcprop_decl = func_decl; funcprop_decl; funcprop_decl = funcprop_decl->next_prop_func) {
|
|
|
|
switch(funcprop_decl->type) {
|
|
|
|
case FUNC_FUNCTION:
|
|
|
|
case FUNC_SUB:
|
|
|
|
case FUNC_PROPGET:
|
2011-09-16 13:28:52 +02:00
|
|
|
case FUNC_DEFGET:
|
2011-09-16 13:28:18 +02:00
|
|
|
invoke_type = VBDISP_CALLGET;
|
|
|
|
break;
|
|
|
|
case FUNC_PROPLET:
|
|
|
|
invoke_type = VBDISP_LET;
|
|
|
|
break;
|
|
|
|
case FUNC_PROPSET:
|
|
|
|
invoke_type = VBDISP_SET;
|
|
|
|
break;
|
2013-03-13 10:43:56 +01:00
|
|
|
DEFAULT_UNREACHABLE;
|
2011-09-16 13:28:18 +02:00
|
|
|
}
|
2011-09-15 14:20:46 +02:00
|
|
|
|
2011-09-16 13:28:18 +02:00
|
|
|
assert(!desc->entries[invoke_type]);
|
2011-09-15 14:20:46 +02:00
|
|
|
|
2011-09-16 13:28:18 +02:00
|
|
|
if(funcprop_decl->is_public)
|
|
|
|
desc->is_public = TRUE;
|
|
|
|
|
|
|
|
hres = create_function(ctx, funcprop_decl, desc->entries+invoke_type);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
2011-09-15 14:20:46 +02:00
|
|
|
}
|
|
|
|
|
2011-09-16 13:27:22 +02:00
|
|
|
static BOOL lookup_class_funcs(class_desc_t *class_desc, const WCHAR *name)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for(i=0; i < class_desc->func_cnt; i++) {
|
|
|
|
if(class_desc->funcs[i].name && !strcmpiW(class_desc->funcs[i].name, name))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-09-15 14:17:17 +02:00
|
|
|
static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl)
|
|
|
|
{
|
2011-09-16 13:28:52 +02:00
|
|
|
function_decl_t *func_decl, *func_prop_decl;
|
2011-09-16 13:27:22 +02:00
|
|
|
class_prop_decl_t *prop_decl;
|
2011-09-15 14:17:17 +02:00
|
|
|
class_desc_t *class_desc;
|
2011-09-15 14:20:46 +02:00
|
|
|
unsigned i;
|
|
|
|
HRESULT hres;
|
2011-09-15 14:17:17 +02:00
|
|
|
|
2011-09-16 13:29:25 +02:00
|
|
|
static const WCHAR class_initializeW[] = {'c','l','a','s','s','_','i','n','i','t','i','a','l','i','z','e',0};
|
2011-09-16 13:29:37 +02:00
|
|
|
static const WCHAR class_terminateW[] = {'c','l','a','s','s','_','t','e','r','m','i','n','a','t','e',0};
|
2011-09-16 13:29:25 +02:00
|
|
|
|
2011-09-15 14:17:17 +02:00
|
|
|
if(lookup_dim_decls(ctx, class_decl->name) || lookup_funcs_name(ctx, class_decl->name)
|
2011-09-21 14:03:38 +02:00
|
|
|
|| lookup_const_decls(ctx, class_decl->name, FALSE) || lookup_class_name(ctx, class_decl->name)) {
|
2011-09-15 14:17:17 +02:00
|
|
|
FIXME("%s: redefinition\n", debugstr_w(class_decl->name));
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2011-09-19 14:08:38 +02:00
|
|
|
class_desc = compiler_alloc_zero(ctx->code, sizeof(*class_desc));
|
2011-09-15 14:17:17 +02:00
|
|
|
if(!class_desc)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
class_desc->name = compiler_alloc_string(ctx->code, class_decl->name);
|
|
|
|
if(!class_desc->name)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2011-09-15 14:20:46 +02:00
|
|
|
class_desc->func_cnt = 1; /* always allocate slot for default getter */
|
|
|
|
|
2011-09-16 13:28:52 +02:00
|
|
|
for(func_decl = class_decl->funcs; func_decl; func_decl = func_decl->next) {
|
|
|
|
for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
|
|
|
|
if(func_prop_decl->type == FUNC_DEFGET)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!func_prop_decl)
|
|
|
|
class_desc->func_cnt++;
|
|
|
|
}
|
2011-09-15 14:20:46 +02:00
|
|
|
|
|
|
|
class_desc->funcs = compiler_alloc(ctx->code, class_desc->func_cnt*sizeof(*class_desc->funcs));
|
|
|
|
if(!class_desc->funcs)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
memset(class_desc->funcs, 0, class_desc->func_cnt*sizeof(*class_desc->funcs));
|
|
|
|
|
|
|
|
for(func_decl = class_decl->funcs, i=1; func_decl; func_decl = func_decl->next, i++) {
|
2011-09-16 13:28:52 +02:00
|
|
|
for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
|
|
|
|
if(func_prop_decl->type == FUNC_DEFGET) {
|
|
|
|
i--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-16 13:29:25 +02:00
|
|
|
if(!strcmpiW(class_initializeW, func_decl->name)) {
|
|
|
|
if(func_decl->type != FUNC_SUB) {
|
|
|
|
FIXME("class initializer is not sub\n");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
class_desc->class_initialize_id = i;
|
2011-09-16 13:29:37 +02:00
|
|
|
}else if(!strcmpiW(class_terminateW, func_decl->name)) {
|
|
|
|
if(func_decl->type != FUNC_SUB) {
|
|
|
|
FIXME("class terminator is not sub\n");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
class_desc->class_terminate_id = i;
|
2011-09-16 13:29:25 +02:00
|
|
|
}
|
|
|
|
|
2011-09-16 13:28:52 +02:00
|
|
|
hres = create_class_funcprop(ctx, func_decl, class_desc->funcs + (func_prop_decl ? 0 : i));
|
2011-09-15 14:20:46 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2011-09-16 13:27:22 +02:00
|
|
|
for(prop_decl = class_decl->props; prop_decl; prop_decl = prop_decl->next)
|
|
|
|
class_desc->prop_cnt++;
|
|
|
|
|
|
|
|
class_desc->props = compiler_alloc(ctx->code, class_desc->prop_cnt*sizeof(*class_desc->props));
|
|
|
|
if(!class_desc->props)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next, i++) {
|
|
|
|
if(lookup_class_funcs(class_desc, prop_decl->name)) {
|
|
|
|
FIXME("Property %s redefined\n", debugstr_w(prop_decl->name));
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
class_desc->props[i].name = compiler_alloc_string(ctx->code, prop_decl->name);
|
|
|
|
if(!class_desc->props[i].name)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
class_desc->props[i].is_public = prop_decl->is_public;
|
|
|
|
}
|
|
|
|
|
2011-09-15 14:17:17 +02:00
|
|
|
class_desc->next = ctx->classes;
|
|
|
|
ctx->classes = class_desc;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-13 11:36:47 +02:00
|
|
|
static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
|
|
|
|
{
|
2011-09-15 14:17:17 +02:00
|
|
|
class_desc_t *class;
|
2011-09-13 11:36:47 +02:00
|
|
|
dynamic_var_t *var;
|
2011-09-14 12:55:32 +02:00
|
|
|
function_t *func;
|
2011-09-13 11:36:47 +02:00
|
|
|
|
|
|
|
for(var = script->global_vars; var; var = var->next) {
|
|
|
|
if(!strcmpiW(var->name, identifier))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-09-14 12:55:32 +02:00
|
|
|
for(func = script->global_funcs; func; func = func->next) {
|
|
|
|
if(!strcmpiW(func->name, identifier))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-09-15 14:17:17 +02:00
|
|
|
for(class = script->classes; class; class = class->next) {
|
|
|
|
if(!strcmpiW(class->name, identifier))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-09-13 11:36:47 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
|
|
|
|
{
|
2011-09-15 14:17:17 +02:00
|
|
|
class_desc_t *class;
|
2011-09-13 11:36:47 +02:00
|
|
|
dynamic_var_t *var;
|
2011-09-14 12:55:32 +02:00
|
|
|
function_t *func;
|
2011-09-13 11:36:47 +02:00
|
|
|
|
|
|
|
for(var = ctx->global_vars; var; var = var->next) {
|
|
|
|
if(lookup_script_identifier(script, var->name)) {
|
|
|
|
FIXME("%s: redefined\n", debugstr_w(var->name));
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-14 12:55:32 +02:00
|
|
|
for(func = ctx->funcs; func; func = func->next) {
|
|
|
|
if(lookup_script_identifier(script, func->name)) {
|
|
|
|
FIXME("%s: redefined\n", debugstr_w(func->name));
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-15 14:17:17 +02:00
|
|
|
for(class = ctx->classes; class; class = class->next) {
|
|
|
|
if(lookup_script_identifier(script, class->name)) {
|
|
|
|
FIXME("%s: redefined\n", debugstr_w(class->name));
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-07 14:07:12 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void release_vbscode(vbscode_t *code)
|
|
|
|
{
|
2011-09-08 14:54:23 +02:00
|
|
|
unsigned i;
|
|
|
|
|
2011-09-07 14:07:12 +02:00
|
|
|
list_remove(&code->entry);
|
2011-09-08 14:54:23 +02:00
|
|
|
|
|
|
|
for(i=0; i < code->bstr_cnt; i++)
|
|
|
|
SysFreeString(code->bstr_pool[i]);
|
|
|
|
|
2013-02-15 15:11:13 +01:00
|
|
|
heap_pool_free(&code->heap);
|
2011-09-12 12:29:14 +02:00
|
|
|
|
2011-09-08 14:54:23 +02:00
|
|
|
heap_free(code->bstr_pool);
|
2011-09-07 14:07:12 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-01-16 12:40:26 +01:00
|
|
|
ctx->instr_cnt = 1;
|
2011-09-07 14:07:12 +02:00
|
|
|
ctx->instr_size = 32;
|
2013-02-15 15:11:13 +01:00
|
|
|
heap_pool_init(&ret->heap);
|
2011-09-07 14:07:12 +02:00
|
|
|
|
2011-09-09 14:47:00 +02:00
|
|
|
ret->option_explicit = ctx->parser.option_explicit;
|
|
|
|
|
2011-09-08 14:54:23 +02:00
|
|
|
ret->bstr_pool = NULL;
|
|
|
|
ret->bstr_pool_size = 0;
|
|
|
|
ret->bstr_cnt = 0;
|
2012-09-06 11:57:15 +02:00
|
|
|
ret->pending_exec = FALSE;
|
2011-09-08 14:54:23 +02:00
|
|
|
|
2012-09-06 11:57:15 +02:00
|
|
|
ret->main_code.type = FUNC_GLOBAL;
|
|
|
|
ret->main_code.name = NULL;
|
|
|
|
ret->main_code.code_ctx = ret;
|
|
|
|
ret->main_code.vars = NULL;
|
|
|
|
ret->main_code.var_cnt = 0;
|
2013-11-13 16:29:18 +01:00
|
|
|
ret->main_code.array_cnt = 0;
|
2012-09-06 11:57:15 +02:00
|
|
|
ret->main_code.arg_cnt = 0;
|
|
|
|
ret->main_code.args = NULL;
|
2011-09-07 14:07:12 +02:00
|
|
|
|
|
|
|
list_init(&ret->entry);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-09-14 12:55:07 +02:00
|
|
|
static void release_compiler(compile_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
parser_release(&ctx->parser);
|
|
|
|
heap_free(ctx->labels);
|
|
|
|
if(ctx->code)
|
|
|
|
release_vbscode(ctx->code);
|
|
|
|
}
|
|
|
|
|
2012-10-18 11:49:17 +02:00
|
|
|
HRESULT compile_script(script_ctx_t *script, const WCHAR *src, const WCHAR *delimiter, vbscode_t **ret)
|
2011-09-07 14:07:12 +02:00
|
|
|
{
|
2011-09-14 12:55:07 +02:00
|
|
|
function_t *new_func;
|
|
|
|
function_decl_t *func_decl;
|
2011-09-15 14:17:17 +02:00
|
|
|
class_decl_t *class_decl;
|
2011-09-07 14:07:12 +02:00
|
|
|
compile_ctx_t ctx;
|
2011-09-14 12:55:07 +02:00
|
|
|
vbscode_t *code;
|
2011-09-07 14:07:12 +02:00
|
|
|
HRESULT hres;
|
|
|
|
|
2012-10-18 11:49:17 +02:00
|
|
|
hres = parse_script(&ctx.parser, src, delimiter);
|
2011-09-07 14:07:12 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2011-09-14 12:55:07 +02:00
|
|
|
code = ctx.code = alloc_vbscode(&ctx, src);
|
2011-09-07 14:07:12 +02:00
|
|
|
if(!ctx.code)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2011-09-14 12:55:07 +02:00
|
|
|
ctx.funcs = NULL;
|
|
|
|
ctx.func_decls = NULL;
|
2011-09-13 11:36:47 +02:00
|
|
|
ctx.global_vars = NULL;
|
2011-09-15 14:17:17 +02:00
|
|
|
ctx.classes = NULL;
|
2011-09-13 11:38:38 +02:00
|
|
|
ctx.labels = NULL;
|
2011-09-21 14:03:38 +02:00
|
|
|
ctx.global_consts = NULL;
|
2012-07-03 19:17:07 +02:00
|
|
|
ctx.stat_ctx = NULL;
|
2011-09-13 11:38:38 +02:00
|
|
|
ctx.labels_cnt = ctx.labels_size = 0;
|
2011-09-13 11:36:47 +02:00
|
|
|
|
2012-09-06 11:57:15 +02:00
|
|
|
hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->main_code);
|
2011-09-07 14:07:12 +02:00
|
|
|
if(FAILED(hres)) {
|
2011-09-14 12:55:07 +02:00
|
|
|
release_compiler(&ctx);
|
2011-09-07 14:07:12 +02:00
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2011-09-21 14:03:38 +02:00
|
|
|
ctx.global_consts = ctx.const_decls;
|
|
|
|
|
2011-09-14 12:55:07 +02:00
|
|
|
for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
|
|
|
|
hres = create_function(&ctx, func_decl, &new_func);
|
|
|
|
if(FAILED(hres)) {
|
|
|
|
release_compiler(&ctx);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_func->next = ctx.funcs;
|
|
|
|
ctx.funcs = new_func;
|
|
|
|
}
|
|
|
|
|
2011-09-15 14:17:17 +02:00
|
|
|
for(class_decl = ctx.parser.class_decls; class_decl; class_decl = class_decl->next) {
|
|
|
|
hres = compile_class(&ctx, class_decl);
|
|
|
|
if(FAILED(hres)) {
|
|
|
|
release_compiler(&ctx);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-13 11:36:47 +02:00
|
|
|
hres = check_script_collisions(&ctx, script);
|
|
|
|
if(FAILED(hres)) {
|
2011-09-14 12:55:07 +02:00
|
|
|
release_compiler(&ctx);
|
2011-09-13 11:36:47 +02:00
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ctx.global_vars) {
|
|
|
|
dynamic_var_t *var;
|
|
|
|
|
|
|
|
for(var = ctx.global_vars; var->next; var = var->next);
|
|
|
|
|
|
|
|
var->next = script->global_vars;
|
|
|
|
script->global_vars = ctx.global_vars;
|
|
|
|
}
|
|
|
|
|
2011-09-14 12:55:32 +02:00
|
|
|
if(ctx.funcs) {
|
|
|
|
for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);
|
|
|
|
|
|
|
|
new_func->next = script->global_funcs;
|
|
|
|
script->global_funcs = ctx.funcs;
|
|
|
|
}
|
|
|
|
|
2011-09-15 14:17:17 +02:00
|
|
|
if(ctx.classes) {
|
|
|
|
class_desc_t *class = ctx.classes;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
class->ctx = script;
|
|
|
|
if(!class->next)
|
|
|
|
break;
|
|
|
|
class = class->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
class->next = script->classes;
|
|
|
|
script->classes = ctx.classes;
|
|
|
|
}
|
|
|
|
|
2011-09-13 11:39:41 +02:00
|
|
|
if(TRACE_ON(vbscript_disas))
|
|
|
|
dump_code(&ctx);
|
|
|
|
|
2011-09-14 12:55:07 +02:00
|
|
|
ctx.code = NULL;
|
|
|
|
release_compiler(&ctx);
|
|
|
|
|
|
|
|
list_add_tail(&script->code_list, &code->entry);
|
|
|
|
*ret = code;
|
2011-09-07 14:07:12 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|