2011-11-18 14:09:44 +01: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 <math.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "jscript.h"
|
|
|
|
#include "engine.h"
|
2014-09-30 16:51:14 +02:00
|
|
|
#include "parser.h"
|
2011-11-18 14:09:44 +01:00
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(jscript);
|
2011-12-29 11:09:51 +01:00
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(jscript_disas);
|
2011-11-18 14:09:44 +01:00
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
typedef struct _statement_ctx_t {
|
|
|
|
unsigned stack_use;
|
|
|
|
BOOL using_scope;
|
|
|
|
BOOL using_except;
|
|
|
|
|
|
|
|
unsigned break_label;
|
2011-12-28 12:06:55 +01:00
|
|
|
unsigned continue_label;
|
2011-12-28 12:06:41 +01:00
|
|
|
|
2012-01-17 12:37:06 +01:00
|
|
|
const labelled_statement_t *labelled_stat;
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
struct _statement_ctx_t *next;
|
|
|
|
} statement_ctx_t;
|
|
|
|
|
2012-03-12 19:23:19 +01:00
|
|
|
typedef struct {
|
2011-11-18 14:09:44 +01:00
|
|
|
parser_ctx_t *parser;
|
|
|
|
bytecode_t *code;
|
|
|
|
|
2012-12-12 13:12:04 +01:00
|
|
|
BOOL from_eval;
|
|
|
|
|
2011-11-18 14:09:44 +01:00
|
|
|
unsigned code_off;
|
|
|
|
unsigned code_size;
|
2011-12-27 11:16:02 +01:00
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
unsigned *labels;
|
|
|
|
unsigned labels_size;
|
|
|
|
unsigned labels_cnt;
|
|
|
|
|
2016-08-03 16:27:35 +02:00
|
|
|
local_ref_t *locals_buf;
|
|
|
|
unsigned locals_buf_size;
|
|
|
|
unsigned locals_cnt;
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
statement_ctx_t *stat_ctx;
|
2012-04-24 17:39:09 +02:00
|
|
|
function_code_t *func;
|
2012-04-25 11:26:21 +02:00
|
|
|
|
2012-04-26 16:17:31 +02:00
|
|
|
function_expression_t *func_head;
|
|
|
|
function_expression_t *func_tail;
|
2012-03-12 19:23:19 +01:00
|
|
|
} compiler_ctx_t;
|
2011-11-18 14:09:44 +01:00
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
static const struct {
|
|
|
|
const char *op_str;
|
|
|
|
instr_arg_type_t arg1_type;
|
|
|
|
instr_arg_type_t arg2_type;
|
|
|
|
} instr_info[] = {
|
|
|
|
#define X(n,a,b,c) {#n,b,c},
|
|
|
|
OP_LIST
|
|
|
|
#undef X
|
|
|
|
};
|
|
|
|
|
2011-12-29 11:09:51 +01:00
|
|
|
static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
|
|
|
|
{
|
|
|
|
switch(type) {
|
|
|
|
case ARG_STR:
|
2012-10-11 12:17:17 +02:00
|
|
|
TRACE_(jscript_disas)("\t%s", debugstr_jsstr(arg->str));
|
2011-12-29 11:09:51 +01:00
|
|
|
break;
|
|
|
|
case ARG_BSTR:
|
|
|
|
TRACE_(jscript_disas)("\t%s", debugstr_wn(arg->bstr, SysStringLen(arg->bstr)));
|
|
|
|
break;
|
|
|
|
case ARG_INT:
|
|
|
|
TRACE_(jscript_disas)("\t%d", arg->uint);
|
|
|
|
break;
|
|
|
|
case ARG_UINT:
|
|
|
|
case ARG_ADDR:
|
|
|
|
TRACE_(jscript_disas)("\t%u", arg->uint);
|
|
|
|
break;
|
|
|
|
case ARG_FUNC:
|
|
|
|
case ARG_NONE:
|
|
|
|
break;
|
2013-03-13 10:44:07 +01:00
|
|
|
DEFAULT_UNREACHABLE;
|
2011-12-29 11:09:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_code(compiler_ctx_t *ctx, unsigned off)
|
|
|
|
{
|
|
|
|
instr_t *instr;
|
|
|
|
|
|
|
|
for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
|
|
|
|
TRACE_(jscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
|
2012-05-12 16:22:37 +02:00
|
|
|
if(instr_info[instr->op].arg1_type == ARG_DBL) {
|
|
|
|
TRACE_(jscript_disas)("\t%lf", instr->u.dbl);
|
|
|
|
}else {
|
|
|
|
dump_instr_arg(instr_info[instr->op].arg1_type, instr->u.arg);
|
|
|
|
dump_instr_arg(instr_info[instr->op].arg2_type, instr->u.arg+1);
|
|
|
|
}
|
2011-12-29 11:09:51 +01:00
|
|
|
TRACE_(jscript_disas)("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
static HRESULT compile_expression(compiler_ctx_t*,expression_t*,BOOL);
|
2011-12-28 12:06:41 +01:00
|
|
|
static HRESULT compile_statement(compiler_ctx_t*,statement_ctx_t*,statement_t*);
|
2011-11-18 14:09:44 +01:00
|
|
|
|
2011-11-23 12:14:15 +01:00
|
|
|
static inline void *compiler_alloc(bytecode_t *code, size_t size)
|
|
|
|
{
|
2013-02-15 14:04:14 +01:00
|
|
|
return heap_pool_alloc(&code->heap, size);
|
2011-11-23 12:14:15 +01:00
|
|
|
}
|
|
|
|
|
2012-10-11 12:17:17 +02:00
|
|
|
static jsstr_t *compiler_alloc_string_len(compiler_ctx_t *ctx, const WCHAR *str, unsigned len)
|
2011-11-23 12:14:15 +01:00
|
|
|
{
|
2012-10-11 12:17:17 +02:00
|
|
|
jsstr_t *new_str;
|
|
|
|
|
|
|
|
if(!ctx->code->str_pool_size) {
|
|
|
|
ctx->code->str_pool = heap_alloc(8 * sizeof(jsstr_t*));
|
|
|
|
if(!ctx->code->str_pool)
|
|
|
|
return NULL;
|
|
|
|
ctx->code->str_pool_size = 8;
|
|
|
|
}else if(ctx->code->str_pool_size == ctx->code->str_cnt) {
|
|
|
|
jsstr_t **new_pool;
|
|
|
|
|
|
|
|
new_pool = heap_realloc(ctx->code->str_pool, ctx->code->str_pool_size*2*sizeof(jsstr_t*));
|
|
|
|
if(!new_pool)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ctx->code->str_pool = new_pool;
|
|
|
|
ctx->code->str_pool_size *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_str = jsstr_alloc_len(str, len);
|
|
|
|
if(!new_str)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ctx->code->str_pool[ctx->code->str_cnt++] = new_str;
|
|
|
|
return new_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static jsstr_t *compiler_alloc_string(compiler_ctx_t *ctx, const WCHAR *str)
|
|
|
|
{
|
|
|
|
return compiler_alloc_string_len(ctx, str, strlenW(str));
|
2011-11-23 12:14:15 +01:00
|
|
|
}
|
|
|
|
|
2012-10-04 14:58:05 +02:00
|
|
|
static BOOL ensure_bstr_slot(compiler_ctx_t *ctx)
|
2011-11-28 12:08:54 +01:00
|
|
|
{
|
|
|
|
if(!ctx->code->bstr_pool_size) {
|
|
|
|
ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
|
|
|
|
if(!ctx->code->bstr_pool)
|
2012-10-04 14:58:05 +02:00
|
|
|
return FALSE;
|
2011-11-28 12:08:54 +01:00
|
|
|
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)
|
2012-10-04 14:58:05 +02:00
|
|
|
return FALSE;
|
2011-11-28 12:08:54 +01:00
|
|
|
|
|
|
|
ctx->code->bstr_pool = new_pool;
|
|
|
|
ctx->code->bstr_pool_size *= 2;
|
|
|
|
}
|
|
|
|
|
2012-10-04 14:58:05 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BSTR compiler_alloc_bstr(compiler_ctx_t *ctx, const WCHAR *str)
|
|
|
|
{
|
|
|
|
if(!ensure_bstr_slot(ctx))
|
|
|
|
return NULL;
|
|
|
|
|
2011-11-28 12:08:54 +01:00
|
|
|
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++];
|
|
|
|
}
|
|
|
|
|
2012-10-04 14:58:05 +02:00
|
|
|
static BSTR compiler_alloc_bstr_len(compiler_ctx_t *ctx, const WCHAR *str, size_t len)
|
|
|
|
{
|
|
|
|
if(!ensure_bstr_slot(ctx))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocStringLen(str, len);
|
|
|
|
if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
|
|
|
|
}
|
|
|
|
|
2011-11-18 14:09:44 +01:00
|
|
|
static unsigned push_instr(compiler_ctx_t *ctx, jsop_t op)
|
|
|
|
{
|
|
|
|
assert(ctx->code_size >= ctx->code_off);
|
|
|
|
|
2012-01-02 11:45:54 +01:00
|
|
|
if(ctx->code_size == ctx->code_off) {
|
2011-11-18 14:09:44 +01:00
|
|
|
instr_t *new_instrs;
|
|
|
|
|
|
|
|
new_instrs = heap_realloc(ctx->code->instrs, ctx->code_size*2*sizeof(instr_t));
|
|
|
|
if(!new_instrs)
|
2012-01-02 11:46:11 +01:00
|
|
|
return 0;
|
2011-11-18 14:09:44 +01:00
|
|
|
|
|
|
|
ctx->code->instrs = new_instrs;
|
|
|
|
ctx->code_size *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->code->instrs[ctx->code_off].op = op;
|
|
|
|
return ctx->code_off++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline instr_t *instr_ptr(compiler_ctx_t *ctx, unsigned off)
|
|
|
|
{
|
|
|
|
assert(off < ctx->code_off);
|
|
|
|
return ctx->code->instrs + off;
|
|
|
|
}
|
|
|
|
|
2011-11-23 12:13:46 +01:00
|
|
|
static HRESULT push_instr_int(compiler_ctx_t *ctx, jsop_t op, LONG arg)
|
|
|
|
{
|
|
|
|
unsigned instr;
|
|
|
|
|
|
|
|
instr = push_instr(ctx, op);
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!instr)
|
2011-11-23 12:13:46 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-05-12 16:22:37 +02:00
|
|
|
instr_ptr(ctx, instr)->u.arg->lng = arg;
|
2011-11-23 12:13:46 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-23 12:14:15 +01:00
|
|
|
static HRESULT push_instr_str(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
|
|
|
|
{
|
|
|
|
unsigned instr;
|
2012-10-11 12:17:17 +02:00
|
|
|
jsstr_t *str;
|
2011-11-23 12:14:15 +01:00
|
|
|
|
2012-10-11 12:17:17 +02:00
|
|
|
str = compiler_alloc_string(ctx, arg);
|
2011-11-23 12:14:15 +01:00
|
|
|
if(!str)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
instr = push_instr(ctx, op);
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!instr)
|
2011-11-23 12:14:15 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-05-12 16:22:37 +02:00
|
|
|
instr_ptr(ctx, instr)->u.arg->str = str;
|
2011-11-23 12:14:15 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-28 12:08:54 +01:00
|
|
|
static HRESULT push_instr_bstr(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
|
|
|
|
{
|
|
|
|
unsigned instr;
|
|
|
|
WCHAR *str;
|
|
|
|
|
|
|
|
str = compiler_alloc_bstr(ctx, arg);
|
|
|
|
if(!str)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
instr = push_instr(ctx, op);
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!instr)
|
2011-11-28 12:08:54 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-05-12 16:22:37 +02:00
|
|
|
instr_ptr(ctx, instr)->u.arg->bstr = str;
|
2011-11-28 12:08:54 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-07 11:00:20 +01:00
|
|
|
static HRESULT push_instr_bstr_uint(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg1, unsigned arg2)
|
|
|
|
{
|
|
|
|
unsigned instr;
|
|
|
|
WCHAR *str;
|
|
|
|
|
|
|
|
str = compiler_alloc_bstr(ctx, arg1);
|
|
|
|
if(!str)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
instr = push_instr(ctx, op);
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!instr)
|
2011-12-07 11:00:20 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-05-12 16:22:37 +02:00
|
|
|
instr_ptr(ctx, instr)->u.arg[0].bstr = str;
|
|
|
|
instr_ptr(ctx, instr)->u.arg[1].uint = arg2;
|
2011-12-07 11:00:20 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-15 15:43:01 +01:00
|
|
|
static HRESULT push_instr_uint_str(compiler_ctx_t *ctx, jsop_t op, unsigned arg1, const WCHAR *arg2)
|
|
|
|
{
|
|
|
|
unsigned instr;
|
2012-10-11 12:17:17 +02:00
|
|
|
jsstr_t *str;
|
2011-12-15 15:43:01 +01:00
|
|
|
|
2012-10-11 12:17:17 +02:00
|
|
|
str = compiler_alloc_string(ctx, arg2);
|
2011-12-15 15:43:01 +01:00
|
|
|
if(!str)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
instr = push_instr(ctx, op);
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!instr)
|
2011-12-15 15:43:01 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-05-12 16:22:37 +02:00
|
|
|
instr_ptr(ctx, instr)->u.arg[0].uint = arg1;
|
|
|
|
instr_ptr(ctx, instr)->u.arg[1].str = str;
|
2011-12-15 15:43:01 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-23 12:14:31 +01:00
|
|
|
static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
|
|
|
|
{
|
|
|
|
unsigned instr;
|
|
|
|
|
|
|
|
instr = push_instr(ctx, op);
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!instr)
|
2011-11-23 12:14:31 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-05-12 16:22:37 +02:00
|
|
|
instr_ptr(ctx, instr)->u.dbl = arg;
|
2011-11-23 12:14:31 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-12 16:22:37 +02:00
|
|
|
static inline void set_arg_uint(compiler_ctx_t *ctx, unsigned instr, unsigned arg)
|
|
|
|
{
|
|
|
|
instr_ptr(ctx, instr)->u.arg->uint = arg;
|
|
|
|
}
|
|
|
|
|
2011-12-05 11:12:32 +01:00
|
|
|
static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
|
|
|
|
{
|
|
|
|
unsigned instr;
|
|
|
|
|
|
|
|
instr = push_instr(ctx, op);
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!instr)
|
2011-12-05 11:12:32 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-05-12 16:22:37 +02:00
|
|
|
set_arg_uint(ctx, instr, arg);
|
2011-12-05 11:12:32 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-18 14:09:44 +01:00
|
|
|
static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
|
|
|
|
{
|
|
|
|
HRESULT hres;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, expr->expression1, TRUE);
|
2011-11-18 14:09:44 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, expr->expression2, TRUE);
|
2011-11-18 14:09:44 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-01-02 11:46:11 +01:00
|
|
|
return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
|
2011-11-18 14:09:44 +01:00
|
|
|
}
|
|
|
|
|
2011-11-18 14:10:40 +01:00
|
|
|
static HRESULT compile_unary_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op)
|
|
|
|
{
|
|
|
|
HRESULT hres;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, expr->expression, TRUE);
|
2011-11-18 14:10:40 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-01-02 11:46:11 +01:00
|
|
|
return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
|
2011-11-18 14:10:40 +01:00
|
|
|
}
|
|
|
|
|
2011-12-06 10:42:09 +01:00
|
|
|
/* ECMA-262 3rd Edition 11.2.1 */
|
|
|
|
static HRESULT compile_member_expression(compiler_ctx_t *ctx, member_expression_t *expr)
|
|
|
|
{
|
|
|
|
HRESULT hres;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, expr->expression, TRUE);
|
2011-12-06 10:42:09 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
return push_instr_bstr(ctx, OP_member, expr->identifier);
|
|
|
|
}
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
#define LABEL_FLAG 0x80000000
|
|
|
|
|
|
|
|
static unsigned alloc_label(compiler_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
if(!ctx->labels_size) {
|
|
|
|
ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
|
|
|
|
if(!ctx->labels)
|
2012-01-02 11:46:33 +01:00
|
|
|
return 0;
|
2011-12-28 12:06:41 +01: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-02 11:46:33 +01:00
|
|
|
return 0;
|
2011-12-28 12:06:41 +01:00
|
|
|
|
|
|
|
ctx->labels = new_labels;
|
|
|
|
ctx->labels_size *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx->labels_cnt++ | LABEL_FLAG;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void label_set_addr(compiler_ctx_t *ctx, unsigned label)
|
|
|
|
{
|
|
|
|
assert(label & LABEL_FLAG);
|
|
|
|
ctx->labels[label & ~LABEL_FLAG] = ctx->code_off;
|
|
|
|
}
|
|
|
|
|
2011-12-07 11:00:20 +01:00
|
|
|
static inline BOOL is_memberid_expr(expression_type_t type)
|
|
|
|
{
|
|
|
|
return type == EXPR_IDENT || type == EXPR_MEMBER || type == EXPR_ARRAY;
|
|
|
|
}
|
|
|
|
|
2016-08-04 13:40:37 +02:00
|
|
|
static BOOL bind_local(compiler_ctx_t *ctx, const WCHAR *identifier, int *ret_ref)
|
|
|
|
{
|
|
|
|
statement_ctx_t *iter;
|
|
|
|
local_ref_t *ref;
|
|
|
|
|
|
|
|
for(iter = ctx->stat_ctx; iter; iter = iter->next) {
|
|
|
|
if(iter->using_scope)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref = lookup_local(ctx->func, identifier);
|
|
|
|
if(!ref)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
*ret_ref = ref->ref;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT emit_identifier_ref(compiler_ctx_t *ctx, const WCHAR *identifier, unsigned flags)
|
|
|
|
{
|
|
|
|
int local_ref;
|
|
|
|
if(bind_local(ctx, identifier, &local_ref))
|
|
|
|
return push_instr_int(ctx, OP_local_ref, local_ref);
|
|
|
|
return push_instr_bstr_uint(ctx, OP_identid, identifier, flags);
|
|
|
|
}
|
|
|
|
|
2016-08-04 13:40:44 +02:00
|
|
|
static HRESULT emit_identifier(compiler_ctx_t *ctx, const WCHAR *identifier)
|
|
|
|
{
|
|
|
|
int local_ref;
|
|
|
|
if(bind_local(ctx, identifier, &local_ref))
|
|
|
|
return push_instr_int(ctx, OP_local, local_ref);
|
|
|
|
return push_instr_bstr(ctx, OP_ident, identifier);
|
|
|
|
}
|
|
|
|
|
2011-12-07 11:00:20 +01:00
|
|
|
static HRESULT compile_memberid_expression(compiler_ctx_t *ctx, expression_t *expr, unsigned flags)
|
|
|
|
{
|
|
|
|
HRESULT hres = S_OK;
|
|
|
|
|
|
|
|
switch(expr->type) {
|
|
|
|
case EXPR_IDENT: {
|
|
|
|
identifier_expression_t *ident_expr = (identifier_expression_t*)expr;
|
|
|
|
|
2016-08-04 13:40:37 +02:00
|
|
|
hres = emit_identifier_ref(ctx, ident_expr->identifier, flags);
|
2011-12-07 11:00:20 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case EXPR_ARRAY: {
|
2011-12-07 11:01:40 +01:00
|
|
|
binary_expression_t *array_expr = (binary_expression_t*)expr;
|
2011-12-07 11:00:20 +01:00
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, array_expr->expression1, TRUE);
|
2011-12-07 11:00:20 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, array_expr->expression2, TRUE);
|
2011-12-07 11:00:20 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
hres = push_instr_uint(ctx, OP_memberid, flags);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case EXPR_MEMBER: {
|
|
|
|
member_expression_t *member_expr = (member_expression_t*)expr;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, member_expr->expression, TRUE);
|
2011-12-07 11:00:20 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
/* FIXME: Potential optimization */
|
|
|
|
hres = push_instr_str(ctx, OP_str, member_expr->identifier);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
hres = push_instr_uint(ctx, OP_memberid, flags);
|
|
|
|
break;
|
|
|
|
}
|
2013-03-13 10:44:07 +01:00
|
|
|
DEFAULT_UNREACHABLE;
|
2011-12-07 11:00:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2011-12-08 12:02:08 +01:00
|
|
|
static HRESULT compile_increment_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op, int n)
|
2011-12-07 11:01:53 +01:00
|
|
|
{
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
if(!is_memberid_expr(expr->expression->type)) {
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, expr->expression, TRUE);
|
2011-12-07 11:01:53 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2011-12-28 12:06:03 +01:00
|
|
|
return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
|
2011-12-07 11:01:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
hres = compile_memberid_expression(ctx, expr->expression, fdexNameEnsure);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2011-12-08 12:02:08 +01:00
|
|
|
return push_instr_int(ctx, op, n);
|
2011-12-07 11:01:53 +01:00
|
|
|
}
|
|
|
|
|
2011-11-25 12:05:50 +01:00
|
|
|
/* ECMA-262 3rd Edition 11.14 */
|
2012-12-14 11:06:28 +01:00
|
|
|
static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr, BOOL emit_ret)
|
2011-11-25 12:05:50 +01:00
|
|
|
{
|
|
|
|
HRESULT hres;
|
|
|
|
|
2012-12-14 11:06:28 +01:00
|
|
|
hres = compile_expression(ctx, expr->expression1, FALSE);
|
2011-11-25 12:05:50 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-12-14 11:06:28 +01:00
|
|
|
return compile_expression(ctx, expr->expression2, emit_ret);
|
2011-11-25 12:05:50 +01:00
|
|
|
}
|
|
|
|
|
2011-11-28 12:03:57 +01:00
|
|
|
/* ECMA-262 3rd Edition 11.11 */
|
|
|
|
static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
|
|
|
|
{
|
|
|
|
unsigned instr;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, expr->expression1, TRUE);
|
2011-11-28 12:03:57 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
instr = push_instr(ctx, op);
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!instr)
|
2011-11-28 12:03:57 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, expr->expression2, TRUE);
|
2011-11-28 12:03:57 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-05-12 16:22:37 +02:00
|
|
|
set_arg_uint(ctx, instr, ctx->code_off);
|
2011-11-28 12:03:57 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-28 12:04:51 +01:00
|
|
|
/* ECMA-262 3rd Edition 11.12 */
|
|
|
|
static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
|
|
|
|
{
|
|
|
|
unsigned jmp_false, jmp_end;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, expr->expression, TRUE);
|
2011-11-28 12:04:51 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2011-12-20 11:47:23 +01:00
|
|
|
jmp_false = push_instr(ctx, OP_cnd_z);
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!jmp_false)
|
2011-11-28 12:04:51 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, expr->true_expression, TRUE);
|
2011-11-28 12:04:51 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
jmp_end = push_instr(ctx, OP_jmp);
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!jmp_end)
|
2011-11-28 12:04:51 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-05-12 16:22:37 +02:00
|
|
|
set_arg_uint(ctx, jmp_false, ctx->code_off);
|
2012-12-14 11:06:43 +01:00
|
|
|
hres = push_instr_uint(ctx, OP_pop, 1);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
2011-11-28 12:04:51 +01:00
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, expr->false_expression, TRUE);
|
2011-11-28 12:04:51 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-05-12 16:22:37 +02:00
|
|
|
set_arg_uint(ctx, jmp_end, ctx->code_off);
|
2011-11-28 12:04:51 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-28 12:05:07 +01:00
|
|
|
static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
|
|
|
|
{
|
|
|
|
unsigned arg_cnt = 0;
|
|
|
|
argument_t *arg;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, expr->expression, TRUE);
|
2011-11-28 12:05:07 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
for(arg = expr->argument_list; arg; arg = arg->next) {
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, arg->expr, TRUE);
|
2011-11-28 12:05:07 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
arg_cnt++;
|
|
|
|
}
|
|
|
|
|
2016-03-28 17:49:10 +02:00
|
|
|
hres = push_instr_uint(ctx, OP_new, arg_cnt);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
hres = push_instr_uint(ctx, OP_pop, arg_cnt+1);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
return push_instr(ctx, OP_push_ret) ? S_OK : E_OUTOFMEMORY;
|
2011-11-28 12:05:07 +01:00
|
|
|
}
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL emit_ret)
|
2011-12-07 11:00:20 +01:00
|
|
|
{
|
2016-03-28 17:49:24 +02:00
|
|
|
unsigned arg_cnt = 0, extra_args;
|
2011-12-07 11:00:20 +01:00
|
|
|
argument_t *arg;
|
|
|
|
unsigned instr;
|
2011-12-07 11:00:44 +01:00
|
|
|
jsop_t op;
|
2011-12-07 11:00:20 +01:00
|
|
|
HRESULT hres;
|
|
|
|
|
2011-12-07 11:00:44 +01:00
|
|
|
if(is_memberid_expr(expr->expression->type)) {
|
|
|
|
op = OP_call_member;
|
2016-03-28 17:49:24 +02:00
|
|
|
extra_args = 2;
|
2011-12-07 11:00:44 +01:00
|
|
|
hres = compile_memberid_expression(ctx, expr->expression, 0);
|
|
|
|
}else {
|
|
|
|
op = OP_call;
|
2016-03-28 17:49:24 +02:00
|
|
|
extra_args = 1;
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, expr->expression, TRUE);
|
2011-12-07 11:00:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
for(arg = expr->argument_list; arg; arg = arg->next) {
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, arg->expr, TRUE);
|
2011-12-07 11:00:20 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
arg_cnt++;
|
|
|
|
}
|
|
|
|
|
2011-12-07 11:00:44 +01:00
|
|
|
instr = push_instr(ctx, op);
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!instr)
|
2011-12-07 11:00:20 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-05-12 16:22:37 +02:00
|
|
|
instr_ptr(ctx, instr)->u.arg[0].uint = arg_cnt;
|
2012-12-13 12:08:59 +01:00
|
|
|
instr_ptr(ctx, instr)->u.arg[1].lng = emit_ret;
|
2016-03-28 17:49:24 +02:00
|
|
|
|
|
|
|
hres = push_instr_uint(ctx, OP_pop, arg_cnt + extra_args);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
return !emit_ret || push_instr(ctx, OP_push_ret) ? S_OK : E_OUTOFMEMORY;
|
2011-12-07 11:00:20 +01:00
|
|
|
}
|
|
|
|
|
2011-11-30 10:14:22 +01:00
|
|
|
static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
|
|
|
|
{
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
switch(expr->expression->type) {
|
|
|
|
case EXPR_ARRAY: {
|
2011-12-07 11:01:40 +01:00
|
|
|
binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
|
2011-11-30 10:14:22 +01:00
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, array_expr->expression1, TRUE);
|
2011-11-30 10:14:22 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, array_expr->expression2, TRUE);
|
2011-11-30 10:14:22 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!push_instr(ctx, OP_delete))
|
2011-11-30 10:14:22 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
break;
|
|
|
|
}
|
2011-11-30 10:14:33 +01:00
|
|
|
case EXPR_MEMBER: {
|
|
|
|
member_expression_t *member_expr = (member_expression_t*)expr->expression;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, member_expr->expression, TRUE);
|
2011-11-30 10:14:33 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
/* FIXME: Potential optimization */
|
|
|
|
hres = push_instr_str(ctx, OP_str, member_expr->identifier);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!push_instr(ctx, OP_delete))
|
2011-11-30 10:14:33 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
break;
|
|
|
|
}
|
2011-12-15 15:42:27 +01:00
|
|
|
case EXPR_IDENT:
|
|
|
|
return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
|
2011-12-15 15:43:01 +01:00
|
|
|
default: {
|
|
|
|
const WCHAR fixmeW[] = {'F','I','X','M','E',0};
|
|
|
|
|
|
|
|
WARN("invalid delete, unimplemented exception message\n");
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, expr->expression, TRUE);
|
2011-12-15 15:43:01 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, fixmeW);
|
|
|
|
}
|
2011-11-30 10:14:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-05 11:13:02 +01:00
|
|
|
static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
|
2011-12-05 11:11:29 +01:00
|
|
|
{
|
2012-04-17 16:25:58 +02:00
|
|
|
BOOL use_throw_path = FALSE;
|
|
|
|
unsigned arg_cnt = 0;
|
2011-12-05 11:11:29 +01:00
|
|
|
HRESULT hres;
|
|
|
|
|
2012-04-17 16:25:58 +02:00
|
|
|
if(expr->expression1->type == EXPR_CALL) {
|
|
|
|
call_expression_t *call_expr = (call_expression_t*)expr->expression1;
|
|
|
|
argument_t *arg;
|
|
|
|
|
|
|
|
if(op != OP_LAST) {
|
|
|
|
FIXME("op %d not supported on parametrized assign expressions\n", op);
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_memberid_expr(call_expr->expression->type) && call_expr->argument_list) {
|
|
|
|
hres = compile_memberid_expression(ctx, call_expr->expression, fdexNameEnsure);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
for(arg = call_expr->argument_list; arg; arg = arg->next) {
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, arg->expr, TRUE);
|
2012-04-17 16:25:58 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
arg_cnt++;
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
use_throw_path = TRUE;
|
|
|
|
}
|
|
|
|
}else if(is_memberid_expr(expr->expression1->type)) {
|
|
|
|
hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}else {
|
|
|
|
use_throw_path = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(use_throw_path) {
|
|
|
|
/* Illegal assignment: evaluate and throw */
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, expr->expression1, TRUE);
|
2011-12-05 11:12:32 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, expr->expression2, TRUE);
|
2011-12-05 11:12:32 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-01-02 11:46:11 +01:00
|
|
|
if(op != OP_LAST && !push_instr(ctx, op))
|
2011-12-05 11:13:02 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2011-12-28 12:06:03 +01:00
|
|
|
return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
|
2011-12-05 11:11:29 +01:00
|
|
|
}
|
|
|
|
|
2012-01-02 11:46:11 +01:00
|
|
|
if(op != OP_LAST && !push_instr(ctx, OP_refval))
|
2011-12-05 11:13:02 +01:00
|
|
|
return E_OUTOFMEMORY;
|
2011-12-05 11:11:29 +01:00
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, expr->expression2, TRUE);
|
2011-12-05 11:11:29 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-01-02 11:46:11 +01:00
|
|
|
if(op != OP_LAST && !push_instr(ctx, op))
|
2011-12-05 11:13:02 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-04-17 16:25:58 +02:00
|
|
|
if(arg_cnt)
|
|
|
|
return push_instr_uint(ctx, OP_assign_call, arg_cnt);
|
|
|
|
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!push_instr(ctx, OP_assign))
|
2011-12-05 11:11:29 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-13 11:57:11 +01:00
|
|
|
static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
|
|
|
|
{
|
|
|
|
jsop_t op;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
if(is_memberid_expr(expr->expression->type)) {
|
|
|
|
if(expr->expression->type == EXPR_IDENT)
|
2012-10-11 12:17:17 +02:00
|
|
|
return push_instr_bstr(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
|
2011-12-13 11:57:11 +01:00
|
|
|
|
|
|
|
op = OP_typeofid;
|
|
|
|
hres = compile_memberid_expression(ctx, expr->expression, 0);
|
|
|
|
}else {
|
|
|
|
op = OP_typeof;
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, expr->expression, TRUE);
|
2011-12-13 11:57:11 +01:00
|
|
|
}
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-01-02 11:46:11 +01:00
|
|
|
return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
|
2011-12-13 11:57:11 +01:00
|
|
|
}
|
|
|
|
|
2011-11-24 14:23:39 +01:00
|
|
|
static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
|
2011-11-23 12:13:46 +01:00
|
|
|
{
|
|
|
|
switch(literal->type) {
|
2011-11-23 12:13:57 +01:00
|
|
|
case LT_BOOL:
|
|
|
|
return push_instr_int(ctx, OP_bool, literal->u.bval);
|
2011-11-23 12:14:31 +01:00
|
|
|
case LT_DOUBLE:
|
|
|
|
return push_instr_double(ctx, OP_double, literal->u.dval);
|
2011-11-24 14:23:26 +01:00
|
|
|
case LT_NULL:
|
2012-01-02 11:46:11 +01:00
|
|
|
return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
|
2011-11-23 12:14:15 +01:00
|
|
|
case LT_STRING:
|
|
|
|
return push_instr_str(ctx, OP_str, literal->u.wstr);
|
2011-11-24 14:23:39 +01:00
|
|
|
case LT_REGEXP: {
|
|
|
|
unsigned instr;
|
2012-10-11 12:17:17 +02:00
|
|
|
jsstr_t *str;
|
2011-11-24 14:23:39 +01:00
|
|
|
|
2012-10-11 12:17:17 +02:00
|
|
|
str = compiler_alloc_string_len(ctx, literal->u.regexp.str, literal->u.regexp.str_len);
|
2011-11-24 14:23:39 +01:00
|
|
|
if(!str)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
instr = push_instr(ctx, OP_regexp);
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!instr)
|
2011-11-24 14:23:39 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-05-12 16:22:37 +02:00
|
|
|
instr_ptr(ctx, instr)->u.arg[0].str = str;
|
|
|
|
instr_ptr(ctx, instr)->u.arg[1].uint = literal->u.regexp.flags;
|
2011-11-24 14:23:39 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
2013-03-13 10:44:07 +01:00
|
|
|
DEFAULT_UNREACHABLE;
|
2011-11-23 12:13:46 +01:00
|
|
|
}
|
2013-03-16 07:53:06 +01:00
|
|
|
return E_FAIL;
|
2011-11-23 12:13:46 +01:00
|
|
|
}
|
|
|
|
|
2011-12-16 11:43:31 +01:00
|
|
|
static HRESULT literal_as_bstr(compiler_ctx_t *ctx, literal_t *literal, BSTR *str)
|
|
|
|
{
|
|
|
|
switch(literal->type) {
|
|
|
|
case LT_STRING:
|
|
|
|
*str = compiler_alloc_bstr(ctx, literal->u.wstr);
|
|
|
|
break;
|
2012-10-11 12:16:01 +02:00
|
|
|
case LT_DOUBLE: {
|
|
|
|
jsstr_t *jsstr;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
hres = double_to_string(literal->u.dval, &jsstr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2013-03-07 11:03:34 +01:00
|
|
|
*str = compiler_alloc_bstr_len(ctx, NULL, jsstr_length(jsstr));
|
|
|
|
if(*str)
|
|
|
|
jsstr_flush(jsstr, *str);
|
2012-10-11 12:16:01 +02:00
|
|
|
jsstr_release(jsstr);
|
|
|
|
break;
|
|
|
|
}
|
2013-03-13 10:44:07 +01:00
|
|
|
DEFAULT_UNREACHABLE;
|
2011-12-16 11:43:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return *str ? S_OK : E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
2011-12-08 12:02:40 +01:00
|
|
|
static HRESULT compile_array_literal(compiler_ctx_t *ctx, array_literal_expression_t *expr)
|
|
|
|
{
|
|
|
|
unsigned i, elem_cnt = expr->length;
|
|
|
|
array_element_t *iter;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
for(iter = expr->element_list; iter; iter = iter->next) {
|
|
|
|
elem_cnt += iter->elision+1;
|
|
|
|
|
|
|
|
for(i=0; i < iter->elision; i++) {
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!push_instr(ctx, OP_undefined))
|
2011-12-08 12:02:40 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, iter->expr, TRUE);
|
2011-12-08 12:02:40 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0; i < expr->length; i++) {
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!push_instr(ctx, OP_undefined))
|
2011-12-08 12:02:40 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return push_instr_uint(ctx, OP_carray, elem_cnt);
|
|
|
|
}
|
|
|
|
|
2011-12-16 11:43:31 +01:00
|
|
|
static HRESULT compile_object_literal(compiler_ctx_t *ctx, property_value_expression_t *expr)
|
|
|
|
{
|
|
|
|
prop_val_t *iter;
|
|
|
|
unsigned instr;
|
|
|
|
BSTR name;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!push_instr(ctx, OP_new_obj))
|
2011-12-16 11:43:31 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
for(iter = expr->property_list; iter; iter = iter->next) {
|
|
|
|
hres = literal_as_bstr(ctx, iter->name, &name);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, iter->value, TRUE);
|
2011-12-16 11:43:31 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
instr = push_instr(ctx, OP_obj_prop);
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!instr)
|
2011-12-16 11:43:31 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-05-12 16:22:37 +02:00
|
|
|
instr_ptr(ctx, instr)->u.arg->bstr = name;
|
2011-12-16 11:43:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2016-05-06 12:56:35 +02:00
|
|
|
static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr, BOOL emit_ret)
|
2011-12-16 11:42:57 +01:00
|
|
|
{
|
2016-08-04 13:39:36 +02:00
|
|
|
return emit_ret ? push_instr_uint(ctx, OP_func, expr->func_id) : S_OK;
|
2011-12-16 11:42:57 +01:00
|
|
|
}
|
2011-12-08 12:02:40 +01:00
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr, BOOL emit_ret)
|
2011-11-18 14:09:44 +01:00
|
|
|
{
|
2012-12-13 12:08:59 +01:00
|
|
|
HRESULT hres;
|
|
|
|
|
2011-11-18 14:09:44 +01:00
|
|
|
switch(expr->type) {
|
2011-11-22 16:31:44 +01:00
|
|
|
case EXPR_ADD:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
|
|
|
|
break;
|
2011-12-05 11:11:29 +01:00
|
|
|
case EXPR_AND:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_z);
|
|
|
|
break;
|
2011-12-07 11:01:13 +01:00
|
|
|
case EXPR_ARRAY:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
|
|
|
|
break;
|
2011-12-08 12:02:40 +01:00
|
|
|
case EXPR_ARRAYLIT:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_array_literal(ctx, (array_literal_expression_t*)expr);
|
|
|
|
break;
|
2011-12-05 11:11:29 +01:00
|
|
|
case EXPR_ASSIGN:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
|
|
|
|
break;
|
2011-12-05 11:13:02 +01:00
|
|
|
case EXPR_ASSIGNADD:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
|
|
|
|
break;
|
2011-12-08 12:03:09 +01:00
|
|
|
case EXPR_ASSIGNAND:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_and);
|
|
|
|
break;
|
2011-12-05 11:13:14 +01:00
|
|
|
case EXPR_ASSIGNSUB:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
|
|
|
|
break;
|
2011-12-06 10:41:34 +01:00
|
|
|
case EXPR_ASSIGNMUL:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
|
|
|
|
break;
|
2011-12-06 10:41:45 +01:00
|
|
|
case EXPR_ASSIGNDIV:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
|
|
|
|
break;
|
2011-12-06 10:41:57 +01:00
|
|
|
case EXPR_ASSIGNMOD:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
|
|
|
|
break;
|
2011-12-06 10:42:21 +01:00
|
|
|
case EXPR_ASSIGNOR:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
|
|
|
|
break;
|
2011-12-09 11:05:08 +01:00
|
|
|
case EXPR_ASSIGNLSHIFT:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_lshift);
|
|
|
|
break;
|
2011-12-08 12:03:35 +01:00
|
|
|
case EXPR_ASSIGNRSHIFT:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift);
|
|
|
|
break;
|
2011-12-09 11:04:45 +01:00
|
|
|
case EXPR_ASSIGNRRSHIFT:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
|
|
|
|
break;
|
2011-12-06 10:42:30 +01:00
|
|
|
case EXPR_ASSIGNXOR:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
|
|
|
|
break;
|
2011-12-08 12:02:53 +01:00
|
|
|
case EXPR_BAND:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
|
|
|
|
break;
|
2011-11-21 15:35:15 +01:00
|
|
|
case EXPR_BITNEG:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
|
|
|
|
break;
|
2011-11-29 11:09:35 +01:00
|
|
|
case EXPR_BOR:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
|
|
|
|
break;
|
2011-12-07 11:00:20 +01:00
|
|
|
case EXPR_CALL:
|
2012-12-13 12:08:59 +01:00
|
|
|
return compile_call_expression(ctx, (call_expression_t*)expr, emit_ret);
|
2011-11-25 12:05:50 +01:00
|
|
|
case EXPR_COMMA:
|
2012-12-14 11:06:28 +01:00
|
|
|
return compile_comma_expression(ctx, (binary_expression_t*)expr, emit_ret);
|
2011-11-28 12:04:51 +01:00
|
|
|
case EXPR_COND:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_conditional_expression(ctx, (conditional_expression_t*)expr);
|
|
|
|
break;
|
2011-11-30 10:14:22 +01:00
|
|
|
case EXPR_DELETE:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_delete_expression(ctx, (unary_expression_t*)expr);
|
|
|
|
break;
|
2011-11-29 11:09:04 +01:00
|
|
|
case EXPR_DIV:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
|
|
|
|
break;
|
2011-11-25 12:07:30 +01:00
|
|
|
case EXPR_EQ:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
|
|
|
|
break;
|
2011-11-18 14:09:44 +01:00
|
|
|
case EXPR_EQEQ:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
|
|
|
|
break;
|
2011-12-16 11:42:57 +01:00
|
|
|
case EXPR_FUNC:
|
2016-05-06 12:56:35 +02:00
|
|
|
return compile_function_expression(ctx, (function_expression_t*)expr, emit_ret);
|
2011-12-01 13:23:16 +01:00
|
|
|
case EXPR_GREATER:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
|
|
|
|
break;
|
2011-12-01 13:23:36 +01:00
|
|
|
case EXPR_GREATEREQ:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
|
|
|
|
break;
|
2011-11-28 12:08:54 +01:00
|
|
|
case EXPR_IDENT:
|
2016-08-04 13:40:44 +02:00
|
|
|
hres = emit_identifier(ctx, ((identifier_expression_t*)expr)->identifier);
|
2012-12-13 12:08:59 +01:00
|
|
|
break;
|
2011-11-23 12:13:35 +01:00
|
|
|
case EXPR_IN:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
|
|
|
|
break;
|
2011-12-12 14:43:54 +01:00
|
|
|
case EXPR_INSTANCEOF:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
|
|
|
|
break;
|
2011-12-01 13:22:52 +01:00
|
|
|
case EXPR_LESS:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
|
|
|
|
break;
|
2011-12-01 13:23:03 +01:00
|
|
|
case EXPR_LESSEQ:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
|
|
|
|
break;
|
2011-11-23 12:13:46 +01:00
|
|
|
case EXPR_LITERAL:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_literal(ctx, ((literal_expression_t*)expr)->literal);
|
|
|
|
break;
|
2011-11-18 14:10:40 +01:00
|
|
|
case EXPR_LOGNEG:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
|
|
|
|
break;
|
2011-12-09 11:04:57 +01:00
|
|
|
case EXPR_LSHIFT:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
|
|
|
|
break;
|
2011-12-06 10:42:09 +01:00
|
|
|
case EXPR_MEMBER:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_member_expression(ctx, (member_expression_t*)expr);
|
|
|
|
break;
|
2011-11-25 12:06:30 +01:00
|
|
|
case EXPR_MINUS:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
|
|
|
|
break;
|
2011-11-29 11:09:20 +01:00
|
|
|
case EXPR_MOD:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
|
|
|
|
break;
|
2011-11-29 11:08:54 +01:00
|
|
|
case EXPR_MUL:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
|
|
|
|
break;
|
2011-11-28 12:05:07 +01:00
|
|
|
case EXPR_NEW:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_new_expression(ctx, (call_expression_t*)expr);
|
|
|
|
break;
|
2011-11-25 12:07:30 +01:00
|
|
|
case EXPR_NOTEQ:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
|
|
|
|
break;
|
2011-11-18 14:10:17 +01:00
|
|
|
case EXPR_NOTEQEQ:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
|
|
|
|
break;
|
2011-11-28 12:03:57 +01:00
|
|
|
case EXPR_OR:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_nz);
|
|
|
|
break;
|
2011-11-21 15:35:41 +01:00
|
|
|
case EXPR_PLUS:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
|
|
|
|
break;
|
2011-12-07 11:02:07 +01:00
|
|
|
case EXPR_POSTDEC:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
|
|
|
|
break;
|
2011-12-07 11:01:53 +01:00
|
|
|
case EXPR_POSTINC:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
|
|
|
|
break;
|
2011-12-08 12:02:23 +01:00
|
|
|
case EXPR_PREDEC:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
|
|
|
|
break;
|
2011-12-08 12:02:08 +01:00
|
|
|
case EXPR_PREINC:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
|
|
|
|
break;
|
2011-12-16 11:43:31 +01:00
|
|
|
case EXPR_PROPVAL:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_object_literal(ctx, (property_value_expression_t*)expr);
|
|
|
|
break;
|
2011-12-08 12:03:22 +01:00
|
|
|
case EXPR_RSHIFT:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
|
|
|
|
break;
|
2011-12-09 11:04:24 +01:00
|
|
|
case EXPR_RRSHIFT:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
|
|
|
|
break;
|
2011-11-25 12:06:05 +01:00
|
|
|
case EXPR_SUB:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
|
|
|
|
break;
|
2011-11-24 14:24:14 +01:00
|
|
|
case EXPR_THIS:
|
2012-12-13 12:08:59 +01:00
|
|
|
return !emit_ret || push_instr(ctx, OP_this) ? S_OK : E_OUTOFMEMORY;
|
2011-12-13 11:57:11 +01:00
|
|
|
case EXPR_TYPEOF:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_typeof_expression(ctx, (unary_expression_t*)expr);
|
|
|
|
break;
|
2011-11-24 14:24:45 +01:00
|
|
|
case EXPR_VOID:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
|
|
|
|
break;
|
2011-11-29 11:09:49 +01:00
|
|
|
case EXPR_BXOR:
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
|
|
|
|
break;
|
2013-03-13 10:44:07 +01:00
|
|
|
DEFAULT_UNREACHABLE;
|
2011-11-18 14:09:44 +01:00
|
|
|
}
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
2011-11-18 14:09:44 +01:00
|
|
|
|
2012-12-14 11:06:43 +01:00
|
|
|
return emit_ret ? S_OK : push_instr_uint(ctx, OP_pop, 1);
|
2011-12-07 11:00:20 +01:00
|
|
|
}
|
|
|
|
|
2012-01-17 12:41:54 +01:00
|
|
|
static inline BOOL is_loop_statement(statement_type_t type)
|
|
|
|
{
|
|
|
|
return type == STAT_FOR || type == STAT_FORIN || type == STAT_WHILE;
|
|
|
|
}
|
|
|
|
|
2011-12-20 11:45:37 +01:00
|
|
|
/* ECMA-262 3rd Edition 12.1 */
|
2011-12-19 14:58:42 +01:00
|
|
|
static HRESULT compile_block_statement(compiler_ctx_t *ctx, statement_t *iter)
|
|
|
|
{
|
|
|
|
HRESULT hres;
|
|
|
|
|
2012-12-13 12:08:34 +01:00
|
|
|
while(iter) {
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_statement(ctx, NULL, iter);
|
2011-12-19 14:58:42 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
iter = iter->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-20 11:45:37 +01:00
|
|
|
/* ECMA-262 3rd Edition 12.2 */
|
2011-12-27 11:16:13 +01:00
|
|
|
static HRESULT compile_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
|
2011-12-20 11:45:37 +01:00
|
|
|
{
|
|
|
|
variable_declaration_t *iter;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2012-04-25 11:26:21 +02:00
|
|
|
assert(list != NULL);
|
|
|
|
|
2011-12-27 11:16:13 +01:00
|
|
|
for(iter = list; iter; iter = iter->next) {
|
2011-12-20 11:45:37 +01:00
|
|
|
if(!iter->expr)
|
|
|
|
continue;
|
|
|
|
|
2016-08-04 13:40:37 +02:00
|
|
|
hres = emit_identifier_ref(ctx, iter->identifier, 0);
|
2016-08-04 13:39:53 +02:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, iter->expr, TRUE);
|
2011-12-20 11:45:37 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2016-08-04 13:39:53 +02:00
|
|
|
if(!push_instr(ctx, OP_assign))
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
hres = push_instr_uint(ctx, OP_pop, 1);
|
2011-12-20 11:45:37 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2011-12-27 11:16:13 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ECMA-262 3rd Edition 12.2 */
|
|
|
|
static HRESULT compile_var_statement(compiler_ctx_t *ctx, var_statement_t *stat)
|
|
|
|
{
|
2012-12-13 12:08:34 +01:00
|
|
|
return compile_variable_list(ctx, stat->variable_list);
|
2011-12-20 11:45:37 +01:00
|
|
|
}
|
|
|
|
|
2011-12-19 14:59:34 +01:00
|
|
|
/* ECMA-262 3rd Edition 12.4 */
|
|
|
|
static HRESULT compile_expression_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
|
|
|
|
{
|
|
|
|
HRESULT hres;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, stat->expr, ctx->from_eval);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
2011-12-19 14:59:34 +01:00
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
return !ctx->from_eval || push_instr(ctx, OP_setret) ? S_OK : E_OUTOFMEMORY;
|
2011-12-19 14:59:34 +01:00
|
|
|
}
|
|
|
|
|
2011-12-19 14:59:51 +01:00
|
|
|
/* ECMA-262 3rd Edition 12.5 */
|
|
|
|
static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat)
|
|
|
|
{
|
2012-12-13 12:08:34 +01:00
|
|
|
unsigned jmp_else;
|
2011-12-19 14:59:51 +01:00
|
|
|
HRESULT hres;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, stat->expr, TRUE);
|
2011-12-19 14:59:51 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2011-12-20 11:47:37 +01:00
|
|
|
jmp_else = push_instr(ctx, OP_jmp_z);
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!jmp_else)
|
2011-12-19 14:59:51 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_statement(ctx, NULL, stat->if_stat);
|
2011-12-19 14:59:51 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-12-13 12:08:34 +01:00
|
|
|
if(stat->else_stat) {
|
|
|
|
unsigned jmp_end;
|
|
|
|
|
|
|
|
jmp_end = push_instr(ctx, OP_jmp);
|
|
|
|
if(!jmp_end)
|
|
|
|
return E_OUTOFMEMORY;
|
2011-12-19 14:59:51 +01:00
|
|
|
|
2012-12-13 12:08:34 +01:00
|
|
|
set_arg_uint(ctx, jmp_else, ctx->code_off);
|
2011-12-19 14:59:51 +01:00
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_statement(ctx, NULL, stat->else_stat);
|
2011-12-19 14:59:51 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
2012-12-13 12:08:34 +01:00
|
|
|
|
|
|
|
set_arg_uint(ctx, jmp_end, ctx->code_off);
|
2011-12-19 14:59:51 +01:00
|
|
|
}else {
|
2012-12-13 12:08:34 +01:00
|
|
|
set_arg_uint(ctx, jmp_else, ctx->code_off);
|
2011-12-19 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-27 11:16:02 +01:00
|
|
|
/* ECMA-262 3rd Edition 12.6.2 */
|
|
|
|
static HRESULT compile_while_statement(compiler_ctx_t *ctx, while_statement_t *stat)
|
|
|
|
{
|
2011-12-28 12:06:41 +01:00
|
|
|
statement_ctx_t stat_ctx = {0, FALSE, FALSE};
|
2011-12-29 11:07:49 +01:00
|
|
|
unsigned jmp_off;
|
2011-12-27 11:16:02 +01:00
|
|
|
HRESULT hres;
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
stat_ctx.break_label = alloc_label(ctx);
|
2012-01-02 11:46:33 +01:00
|
|
|
if(!stat_ctx.break_label)
|
2011-12-28 12:06:41 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2011-12-28 12:06:55 +01:00
|
|
|
stat_ctx.continue_label = alloc_label(ctx);
|
2012-01-02 11:46:33 +01:00
|
|
|
if(!stat_ctx.continue_label)
|
2011-12-28 12:06:55 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-12-13 12:08:34 +01:00
|
|
|
jmp_off = ctx->code_off;
|
2011-12-27 11:16:02 +01:00
|
|
|
|
2012-12-13 12:08:34 +01:00
|
|
|
if(!stat->do_while) {
|
2011-12-28 12:06:55 +01:00
|
|
|
label_set_addr(ctx, stat_ctx.continue_label);
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, stat->expr, TRUE);
|
2011-12-27 11:16:02 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
2011-12-27 11:16:02 +01:00
|
|
|
}
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_statement(ctx, &stat_ctx, stat->statement);
|
2011-12-27 11:16:02 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(stat->do_while) {
|
2011-12-28 12:06:55 +01:00
|
|
|
label_set_addr(ctx, stat_ctx.continue_label);
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, stat->expr, TRUE);
|
2011-12-27 11:16:02 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
2011-12-27 11:16:02 +01:00
|
|
|
}
|
2011-12-28 12:06:41 +01:00
|
|
|
|
2011-12-27 11:16:02 +01:00
|
|
|
hres = push_instr_uint(ctx, OP_jmp, jmp_off);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
label_set_addr(ctx, stat_ctx.break_label);
|
2011-12-27 11:16:02 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-27 11:16:13 +01:00
|
|
|
/* ECMA-262 3rd Edition 12.6.3 */
|
|
|
|
static HRESULT compile_for_statement(compiler_ctx_t *ctx, for_statement_t *stat)
|
|
|
|
{
|
2011-12-28 12:06:41 +01:00
|
|
|
statement_ctx_t stat_ctx = {0, FALSE, FALSE};
|
2011-12-29 11:06:53 +01:00
|
|
|
unsigned expr_off;
|
2011-12-27 11:16:13 +01:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
if(stat->variable_list) {
|
|
|
|
hres = compile_variable_list(ctx, stat->variable_list);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}else if(stat->begin_expr) {
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, stat->begin_expr, FALSE);
|
2011-12-27 11:16:13 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
stat_ctx.break_label = alloc_label(ctx);
|
2012-01-02 11:46:33 +01:00
|
|
|
if(!stat_ctx.break_label)
|
2011-12-28 12:06:41 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2011-12-28 12:06:55 +01:00
|
|
|
stat_ctx.continue_label = alloc_label(ctx);
|
2012-01-02 11:46:33 +01:00
|
|
|
if(!stat_ctx.continue_label)
|
2011-12-28 12:06:55 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2011-12-27 11:16:13 +01:00
|
|
|
expr_off = ctx->code_off;
|
|
|
|
|
|
|
|
if(stat->expr) {
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, stat->expr, TRUE);
|
2011-12-27 11:16:13 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
2011-12-27 11:16:13 +01:00
|
|
|
}
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_statement(ctx, &stat_ctx, stat->statement);
|
2011-12-27 11:16:13 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2011-12-28 12:06:55 +01:00
|
|
|
label_set_addr(ctx, stat_ctx.continue_label);
|
|
|
|
|
2011-12-27 11:16:13 +01:00
|
|
|
if(stat->end_expr) {
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, stat->end_expr, FALSE);
|
2011-12-27 11:16:13 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
hres = push_instr_uint(ctx, OP_jmp, expr_off);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
label_set_addr(ctx, stat_ctx.break_label);
|
2011-12-27 11:16:13 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-28 12:05:53 +01:00
|
|
|
/* ECMA-262 3rd Edition 12.6.4 */
|
|
|
|
static HRESULT compile_forin_statement(compiler_ctx_t *ctx, forin_statement_t *stat)
|
|
|
|
{
|
2011-12-28 12:06:41 +01:00
|
|
|
statement_ctx_t stat_ctx = {4, FALSE, FALSE};
|
2011-12-28 12:05:53 +01:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
if(stat->variable) {
|
|
|
|
hres = compile_variable_list(ctx, stat->variable);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
stat_ctx.break_label = alloc_label(ctx);
|
2012-01-02 11:46:33 +01:00
|
|
|
if(!stat_ctx.break_label)
|
2011-12-28 12:06:41 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2011-12-28 12:06:55 +01:00
|
|
|
stat_ctx.continue_label = alloc_label(ctx);
|
2012-01-02 11:46:33 +01:00
|
|
|
if(!stat_ctx.continue_label)
|
2011-12-28 12:06:55 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, stat->in_expr, TRUE);
|
2011-12-28 12:05:53 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(stat->variable) {
|
2016-08-04 13:40:37 +02:00
|
|
|
hres = emit_identifier_ref(ctx, stat->variable->identifier, fdexNameEnsure);
|
2011-12-28 12:05:53 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}else if(is_memberid_expr(stat->expr->type)) {
|
|
|
|
hres = compile_memberid_expression(ctx, stat->expr, fdexNameEnsure);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}else {
|
2011-12-28 12:06:03 +01:00
|
|
|
hres = push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
|
2011-12-28 12:05:53 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
/* FIXME: compile statement anyways when we depend on compiler to check errors */
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
hres = push_instr_int(ctx, OP_int, DISPID_STARTENUM);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2011-12-28 12:06:55 +01:00
|
|
|
label_set_addr(ctx, stat_ctx.continue_label);
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = push_instr_uint(ctx, OP_forin, stat_ctx.break_label);
|
|
|
|
if(FAILED(hres))
|
2011-12-28 12:05:53 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_statement(ctx, &stat_ctx, stat->statement);
|
2011-12-28 12:05:53 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2011-12-28 12:06:55 +01:00
|
|
|
hres = push_instr_uint(ctx, OP_jmp, stat_ctx.continue_label);
|
2011-12-28 12:05:53 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
label_set_addr(ctx, stat_ctx.break_label);
|
2011-12-28 12:05:53 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-13 14:34:11 +02:00
|
|
|
static HRESULT pop_to_stat(compiler_ctx_t *ctx, BOOL var_stack, BOOL scope_stack, statement_ctx_t *stat_ctx)
|
2011-12-28 12:06:55 +01:00
|
|
|
{
|
|
|
|
unsigned stack_pop = 0;
|
2012-01-10 16:10:29 +01:00
|
|
|
statement_ctx_t *iter;
|
2011-12-28 12:06:55 +01:00
|
|
|
|
2012-01-10 16:10:29 +01:00
|
|
|
for(iter = ctx->stat_ctx; iter != stat_ctx; iter = iter->next) {
|
2012-09-13 14:34:11 +02:00
|
|
|
if(scope_stack) {
|
|
|
|
if(iter->using_scope && !push_instr(ctx, OP_pop_scope))
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
if(iter->using_except && !push_instr(ctx, OP_pop_except))
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
2011-12-28 12:06:55 +01:00
|
|
|
stack_pop += iter->stack_use;
|
|
|
|
}
|
|
|
|
|
2012-12-14 11:06:43 +01:00
|
|
|
if(var_stack && stack_pop) {
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
hres = push_instr_uint(ctx, OP_pop, stack_pop);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
2011-12-28 12:06:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ECMA-262 3rd Edition 12.7 */
|
|
|
|
static HRESULT compile_continue_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
|
|
|
|
{
|
|
|
|
statement_ctx_t *pop_ctx;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2012-01-17 12:41:54 +01:00
|
|
|
if(stat->identifier) {
|
|
|
|
statement_t *label_stat;
|
|
|
|
statement_ctx_t *iter;
|
2011-12-28 12:06:55 +01:00
|
|
|
|
2012-01-17 12:41:54 +01:00
|
|
|
pop_ctx = NULL;
|
|
|
|
|
|
|
|
for(iter = ctx->stat_ctx; iter; iter = iter->next) {
|
|
|
|
if(iter->continue_label)
|
|
|
|
pop_ctx = iter;
|
|
|
|
if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier))
|
|
|
|
break;
|
|
|
|
}
|
2011-12-30 11:13:55 +01:00
|
|
|
|
2012-01-17 12:41:54 +01:00
|
|
|
if(!iter) {
|
|
|
|
WARN("Label not found\n");
|
|
|
|
return JS_E_LABEL_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Labelled continue are allowed only on loops */
|
|
|
|
for(label_stat = iter->labelled_stat->statement;
|
|
|
|
label_stat->type == STAT_LABEL;
|
|
|
|
label_stat = ((labelled_statement_t*)label_stat)->statement);
|
|
|
|
if(!is_loop_statement(label_stat->type)) {
|
|
|
|
WARN("Label is not a loop\n");
|
|
|
|
return JS_E_INVALID_CONTINUE;
|
|
|
|
}
|
2012-11-27 12:29:06 +01:00
|
|
|
|
|
|
|
assert(pop_ctx != NULL);
|
2012-01-17 12:41:54 +01:00
|
|
|
}else {
|
|
|
|
for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
|
|
|
|
if(pop_ctx->continue_label)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!pop_ctx) {
|
|
|
|
WARN("continue outside loop\n");
|
|
|
|
return JS_E_INVALID_CONTINUE;
|
|
|
|
}
|
|
|
|
}
|
2011-12-28 12:06:55 +01:00
|
|
|
|
2012-09-13 14:34:11 +02:00
|
|
|
hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx);
|
2011-12-28 12:06:55 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
return push_instr_uint(ctx, OP_jmp, pop_ctx->continue_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ECMA-262 3rd Edition 12.8 */
|
|
|
|
static HRESULT compile_break_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
|
|
|
|
{
|
|
|
|
statement_ctx_t *pop_ctx;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2012-01-17 12:41:42 +01:00
|
|
|
if(stat->identifier) {
|
|
|
|
for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
|
|
|
|
if(pop_ctx->labelled_stat && !strcmpW(pop_ctx->labelled_stat->identifier, stat->identifier)) {
|
|
|
|
assert(pop_ctx->break_label);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-12-28 12:06:55 +01:00
|
|
|
|
2012-01-17 12:41:42 +01:00
|
|
|
if(!pop_ctx) {
|
|
|
|
WARN("Label not found\n");
|
|
|
|
return JS_E_LABEL_NOT_FOUND;
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
|
|
|
|
if(pop_ctx->break_label && !pop_ctx->labelled_stat)
|
|
|
|
break;
|
|
|
|
}
|
2011-12-30 11:13:44 +01:00
|
|
|
|
2012-01-17 12:41:42 +01:00
|
|
|
if(!pop_ctx) {
|
|
|
|
WARN("Break outside loop\n");
|
|
|
|
return JS_E_INVALID_BREAK;
|
|
|
|
}
|
|
|
|
}
|
2011-12-28 12:06:55 +01:00
|
|
|
|
2012-09-13 14:34:11 +02:00
|
|
|
hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx->next);
|
2011-12-28 12:06:55 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
return push_instr_uint(ctx, OP_jmp, pop_ctx->break_label);
|
|
|
|
}
|
|
|
|
|
2011-12-29 11:08:34 +01:00
|
|
|
/* ECMA-262 3rd Edition 12.9 */
|
|
|
|
static HRESULT compile_return_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
|
|
|
|
{
|
2011-12-30 11:15:09 +01:00
|
|
|
HRESULT hres;
|
|
|
|
|
2012-12-12 13:12:04 +01:00
|
|
|
if(ctx->from_eval) {
|
|
|
|
WARN("misplaced return statement\n");
|
|
|
|
return JS_E_MISPLACED_RETURN;
|
|
|
|
}
|
|
|
|
|
2012-09-13 14:34:11 +02:00
|
|
|
hres = pop_to_stat(ctx, TRUE, FALSE, NULL);
|
2011-12-30 11:15:09 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
if(stat->expr) {
|
|
|
|
hres = compile_expression(ctx, stat->expr, TRUE);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
if(!push_instr(ctx, OP_setret))
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
2011-12-30 11:15:09 +01:00
|
|
|
|
2012-09-13 14:34:11 +02:00
|
|
|
hres = pop_to_stat(ctx, FALSE, TRUE, NULL);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2016-03-28 17:48:57 +02:00
|
|
|
return push_instr_uint(ctx, OP_ret, !stat->expr);
|
2011-12-29 11:08:34 +01:00
|
|
|
}
|
|
|
|
|
2011-12-27 11:16:26 +01:00
|
|
|
/* ECMA-262 3rd Edition 12.10 */
|
|
|
|
static HRESULT compile_with_statement(compiler_ctx_t *ctx, with_statement_t *stat)
|
|
|
|
{
|
2012-01-02 11:46:33 +01:00
|
|
|
statement_ctx_t stat_ctx = {0, TRUE, FALSE};
|
2011-12-27 11:16:26 +01:00
|
|
|
HRESULT hres;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, stat->expr, TRUE);
|
2011-12-27 11:16:26 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!push_instr(ctx, OP_push_scope))
|
2011-12-27 11:16:26 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_statement(ctx, &stat_ctx, stat->statement);
|
2011-12-27 11:16:26 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!push_instr(ctx, OP_pop_scope))
|
2011-12-27 11:16:26 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-17 12:37:06 +01:00
|
|
|
/* ECMA-262 3rd Edition 12.10 */
|
|
|
|
static HRESULT compile_labelled_statement(compiler_ctx_t *ctx, labelled_statement_t *stat)
|
|
|
|
{
|
|
|
|
statement_ctx_t stat_ctx = {0, FALSE, FALSE, 0, 0, stat}, *iter;
|
2012-01-17 12:41:42 +01:00
|
|
|
HRESULT hres;
|
2012-01-17 12:37:06 +01:00
|
|
|
|
|
|
|
for(iter = ctx->stat_ctx; iter; iter = iter->next) {
|
|
|
|
if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier)) {
|
|
|
|
WARN("Label %s redefined\n", debugstr_w(stat->identifier));
|
|
|
|
return JS_E_LABEL_REDEFINED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-17 12:41:42 +01:00
|
|
|
/* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
|
|
|
|
stat_ctx.break_label = alloc_label(ctx);
|
|
|
|
if(!stat_ctx.break_label)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
hres = compile_statement(ctx, &stat_ctx, stat->statement);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
label_set_addr(ctx, stat_ctx.break_label);
|
|
|
|
return S_OK;
|
2012-01-17 12:37:06 +01:00
|
|
|
}
|
|
|
|
|
2011-12-27 11:16:40 +01:00
|
|
|
/* ECMA-262 3rd Edition 12.13 */
|
|
|
|
static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat)
|
|
|
|
{
|
2012-01-02 11:46:33 +01:00
|
|
|
statement_ctx_t stat_ctx = {0, FALSE, FALSE};
|
2011-12-27 11:16:40 +01:00
|
|
|
unsigned case_cnt = 0, *case_jmps, i, default_jmp;
|
|
|
|
BOOL have_default = FALSE;
|
|
|
|
statement_t *stat_iter;
|
|
|
|
case_clausule_t *iter;
|
|
|
|
HRESULT hres;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, stat->expr, TRUE);
|
2011-12-27 11:16:40 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
stat_ctx.break_label = alloc_label(ctx);
|
2012-01-02 11:46:33 +01:00
|
|
|
if(!stat_ctx.break_label)
|
2011-12-28 12:06:41 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2011-12-27 11:16:40 +01:00
|
|
|
for(iter = stat->case_list; iter; iter = iter->next) {
|
|
|
|
if(iter->expr)
|
|
|
|
case_cnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
case_jmps = heap_alloc(case_cnt * sizeof(*case_jmps));
|
|
|
|
if(!case_jmps)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
for(iter = stat->case_list; iter; iter = iter->next) {
|
|
|
|
if(!iter->expr) {
|
|
|
|
have_default = TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, iter->expr, TRUE);
|
2011-12-27 11:16:40 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
break;
|
|
|
|
|
|
|
|
case_jmps[i] = push_instr(ctx, OP_case);
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!case_jmps[i]) {
|
2011-12-27 11:16:40 +01:00
|
|
|
hres = E_OUTOFMEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(SUCCEEDED(hres)) {
|
2012-12-14 11:06:43 +01:00
|
|
|
hres = push_instr_uint(ctx, OP_pop, 1);
|
|
|
|
if(SUCCEEDED(hres)) {
|
2011-12-27 11:16:40 +01:00
|
|
|
default_jmp = push_instr(ctx, OP_jmp);
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!default_jmp)
|
2011-12-27 11:16:40 +01:00
|
|
|
hres = E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(FAILED(hres)) {
|
|
|
|
heap_free(case_jmps);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
for(iter = stat->case_list; iter; iter = iter->next) {
|
|
|
|
while(iter->next && iter->next->stat == iter->stat) {
|
2012-05-12 16:22:37 +02:00
|
|
|
set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
|
2011-12-27 11:16:40 +01:00
|
|
|
iter = iter->next;
|
|
|
|
}
|
|
|
|
|
2012-05-12 16:22:37 +02:00
|
|
|
set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
|
2011-12-27 11:16:40 +01:00
|
|
|
|
2012-12-13 12:08:34 +01:00
|
|
|
for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter);
|
|
|
|
stat_iter = stat_iter->next) {
|
|
|
|
hres = compile_statement(ctx, &stat_ctx, stat_iter);
|
2011-12-27 11:16:40 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
break;
|
|
|
|
}
|
2012-12-13 12:08:34 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
break;
|
2011-12-27 11:16:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
heap_free(case_jmps);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
assert(i == case_cnt);
|
|
|
|
|
2012-04-02 13:45:23 +02:00
|
|
|
if(!have_default) {
|
2012-04-16 11:25:25 +02:00
|
|
|
hres = push_instr_uint(ctx, OP_jmp, stat_ctx.break_label);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
2012-05-12 16:22:37 +02:00
|
|
|
set_arg_uint(ctx, default_jmp, ctx->code_off);
|
2012-04-02 13:45:23 +02:00
|
|
|
}
|
2011-12-27 11:16:40 +01:00
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
label_set_addr(ctx, stat_ctx.break_label);
|
2011-12-27 11:16:40 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-28 12:06:17 +01:00
|
|
|
/* ECMA-262 3rd Edition 12.13 */
|
|
|
|
static HRESULT compile_throw_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
|
|
|
|
{
|
|
|
|
HRESULT hres;
|
|
|
|
|
2012-12-13 12:08:59 +01:00
|
|
|
hres = compile_expression(ctx, stat->expr, TRUE);
|
2011-12-28 12:06:17 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-01-02 11:46:11 +01:00
|
|
|
return push_instr(ctx, OP_throw) ? S_OK : E_OUTOFMEMORY;
|
2011-12-28 12:06:17 +01:00
|
|
|
}
|
|
|
|
|
2011-12-28 12:06:28 +01:00
|
|
|
/* ECMA-262 3rd Edition 12.14 */
|
|
|
|
static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat)
|
|
|
|
{
|
2012-01-02 11:46:33 +01:00
|
|
|
statement_ctx_t try_ctx = {0, FALSE, TRUE}, catch_ctx = {0, TRUE, FALSE};
|
|
|
|
statement_ctx_t finally_ctx = {2, FALSE, FALSE};
|
2011-12-29 11:07:22 +01:00
|
|
|
unsigned push_except;
|
2011-12-28 12:06:28 +01:00
|
|
|
BSTR ident;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
push_except = push_instr(ctx, OP_push_except);
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!push_except)
|
2011-12-28 12:06:28 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
if(stat->catch_block) {
|
|
|
|
ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier);
|
|
|
|
if(!ident)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}else {
|
|
|
|
ident = NULL;
|
|
|
|
}
|
|
|
|
|
2012-05-12 16:22:37 +02:00
|
|
|
instr_ptr(ctx, push_except)->u.arg[1].bstr = ident;
|
2011-12-28 12:06:28 +01:00
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
if(!stat->catch_block)
|
|
|
|
try_ctx.stack_use = 2;
|
|
|
|
|
|
|
|
hres = compile_statement(ctx, &try_ctx, stat->try_statement);
|
2011-12-28 12:06:28 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!push_instr(ctx, OP_pop_except))
|
2011-12-28 12:06:28 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
if(stat->catch_block) {
|
|
|
|
unsigned jmp_finally;
|
|
|
|
|
|
|
|
jmp_finally = push_instr(ctx, OP_jmp);
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!jmp_finally)
|
2011-12-28 12:06:28 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-05-12 16:22:37 +02:00
|
|
|
instr_ptr(ctx, push_except)->u.arg[0].uint = ctx->code_off;
|
2011-12-28 12:06:28 +01:00
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement);
|
2011-12-28 12:06:28 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!push_instr(ctx, OP_pop_scope))
|
2011-12-28 12:06:28 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-05-12 16:22:37 +02:00
|
|
|
set_arg_uint(ctx, jmp_finally, ctx->code_off);
|
2011-12-28 12:06:28 +01:00
|
|
|
}else {
|
2012-05-12 16:22:37 +02:00
|
|
|
set_arg_uint(ctx, push_except, ctx->code_off);
|
2011-12-28 12:06:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(stat->finally_statement) {
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_statement(ctx, stat->catch_block ? NULL : &finally_ctx, stat->finally_statement);
|
2011-12-28 12:06:28 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-01-02 11:46:11 +01:00
|
|
|
if(!stat->catch_block && !push_instr(ctx, OP_end_finally))
|
2011-12-28 12:06:28 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
static HRESULT compile_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
|
2011-12-19 14:58:42 +01:00
|
|
|
{
|
2011-12-28 12:06:41 +01:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
if(stat_ctx) {
|
|
|
|
stat_ctx->next = ctx->stat_ctx;
|
|
|
|
ctx->stat_ctx = stat_ctx;
|
|
|
|
}
|
|
|
|
|
2011-12-19 14:58:42 +01:00
|
|
|
switch(stat->type) {
|
|
|
|
case STAT_BLOCK:
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_block_statement(ctx, ((block_statement_t*)stat)->stat_list);
|
|
|
|
break;
|
2011-12-28 12:06:55 +01:00
|
|
|
case STAT_BREAK:
|
|
|
|
hres = compile_break_statement(ctx, (branch_statement_t*)stat);
|
|
|
|
break;
|
|
|
|
case STAT_CONTINUE:
|
|
|
|
hres = compile_continue_statement(ctx, (branch_statement_t*)stat);
|
|
|
|
break;
|
2011-12-19 15:00:06 +01:00
|
|
|
case STAT_EMPTY:
|
2012-12-13 12:08:34 +01:00
|
|
|
/* nothing to do */
|
|
|
|
hres = S_OK;
|
2011-12-28 12:06:41 +01:00
|
|
|
break;
|
2011-12-19 14:59:34 +01:00
|
|
|
case STAT_EXPR:
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_expression_statement(ctx, (expression_statement_t*)stat);
|
|
|
|
break;
|
2011-12-27 11:16:13 +01:00
|
|
|
case STAT_FOR:
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_for_statement(ctx, (for_statement_t*)stat);
|
|
|
|
break;
|
2011-12-28 12:05:53 +01:00
|
|
|
case STAT_FORIN:
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_forin_statement(ctx, (forin_statement_t*)stat);
|
|
|
|
break;
|
2011-12-19 14:59:51 +01:00
|
|
|
case STAT_IF:
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_if_statement(ctx, (if_statement_t*)stat);
|
|
|
|
break;
|
2011-12-27 11:16:51 +01:00
|
|
|
case STAT_LABEL:
|
2012-01-17 12:37:06 +01:00
|
|
|
hres = compile_labelled_statement(ctx, (labelled_statement_t*)stat);
|
2011-12-28 12:06:41 +01:00
|
|
|
break;
|
2011-12-29 11:08:34 +01:00
|
|
|
case STAT_RETURN:
|
|
|
|
hres = compile_return_statement(ctx, (expression_statement_t*)stat);
|
|
|
|
break;
|
2011-12-27 11:16:40 +01:00
|
|
|
case STAT_SWITCH:
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_switch_statement(ctx, (switch_statement_t*)stat);
|
|
|
|
break;
|
2011-12-28 12:06:17 +01:00
|
|
|
case STAT_THROW:
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_throw_statement(ctx, (expression_statement_t*)stat);
|
|
|
|
break;
|
2011-12-28 12:06:28 +01:00
|
|
|
case STAT_TRY:
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_try_statement(ctx, (try_statement_t*)stat);
|
|
|
|
break;
|
2011-12-20 11:45:37 +01:00
|
|
|
case STAT_VAR:
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_var_statement(ctx, (var_statement_t*)stat);
|
|
|
|
break;
|
2011-12-27 11:16:02 +01:00
|
|
|
case STAT_WHILE:
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_while_statement(ctx, (while_statement_t*)stat);
|
|
|
|
break;
|
2011-12-27 11:16:26 +01:00
|
|
|
case STAT_WITH:
|
2011-12-28 12:06:41 +01:00
|
|
|
hres = compile_with_statement(ctx, (with_statement_t*)stat);
|
|
|
|
break;
|
2013-03-13 10:44:07 +01:00
|
|
|
DEFAULT_UNREACHABLE;
|
2011-12-19 14:58:42 +01:00
|
|
|
}
|
2011-12-28 12:06:41 +01:00
|
|
|
|
|
|
|
if(stat_ctx) {
|
|
|
|
assert(ctx->stat_ctx == stat_ctx);
|
|
|
|
ctx->stat_ctx = stat_ctx->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2016-08-03 16:27:35 +02:00
|
|
|
static int local_cmp(const void *key, const void *ref)
|
|
|
|
{
|
|
|
|
return strcmpW((const WCHAR*)key, ((const local_ref_t*)ref)->name);
|
|
|
|
}
|
|
|
|
|
2016-08-04 13:39:36 +02:00
|
|
|
static inline local_ref_t *find_local(compiler_ctx_t *ctx, const WCHAR *name)
|
2016-08-03 16:27:35 +02:00
|
|
|
{
|
|
|
|
return bsearch(name, ctx->locals_buf, ctx->locals_cnt, sizeof(*ctx->locals_buf), local_cmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL alloc_local(compiler_ctx_t *ctx, BSTR name, int ref)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
if(!ctx->locals_buf_size) {
|
|
|
|
ctx->locals_buf = heap_alloc(4 * sizeof(*ctx->locals_buf));
|
|
|
|
if(!ctx->locals_buf)
|
|
|
|
return FALSE;
|
|
|
|
ctx->locals_buf_size = 4;
|
|
|
|
}else if(ctx->locals_buf_size == ctx->locals_cnt) {
|
|
|
|
local_ref_t *new_buf = heap_realloc(ctx->locals_buf, ctx->locals_buf_size * 2 * sizeof(*ctx->locals_buf));
|
|
|
|
if(!new_buf)
|
|
|
|
return FALSE;
|
|
|
|
ctx->locals_buf = new_buf;
|
|
|
|
ctx->locals_buf_size *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0; i < ctx->locals_cnt; i++) {
|
|
|
|
if(strcmpW(ctx->locals_buf[i].name, name) > 0) {
|
|
|
|
memmove(ctx->locals_buf + i+1, ctx->locals_buf + i, (ctx->locals_cnt - i) * sizeof(*ctx->locals_buf));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->locals_buf[i].name = name;
|
|
|
|
ctx->locals_buf[i].ref = ref;
|
|
|
|
ctx->locals_cnt++;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-08-04 13:39:36 +02:00
|
|
|
static BOOL alloc_variable(compiler_ctx_t *ctx, const WCHAR *name)
|
|
|
|
{
|
|
|
|
BSTR ident;
|
|
|
|
|
|
|
|
if(find_local(ctx, name))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
ident = compiler_alloc_bstr(ctx, name);
|
|
|
|
if(!ident)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return alloc_local(ctx, ident, ctx->func->var_cnt++);
|
|
|
|
}
|
|
|
|
|
2016-08-04 13:39:46 +02:00
|
|
|
static BOOL visit_function_expression(compiler_ctx_t *ctx, function_expression_t *expr)
|
2016-08-04 13:39:36 +02:00
|
|
|
{
|
|
|
|
expr->func_id = ctx->func->func_cnt++;
|
|
|
|
ctx->func_tail = ctx->func_tail ? (ctx->func_tail->next = expr) : (ctx->func_head = expr);
|
2016-08-04 13:39:46 +02:00
|
|
|
|
|
|
|
return !expr->identifier || expr->event_target || alloc_variable(ctx, expr->identifier);
|
2016-08-04 13:39:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT visit_expression(compiler_ctx_t *ctx, expression_t *expr)
|
|
|
|
{
|
|
|
|
HRESULT hres = S_OK;
|
|
|
|
|
|
|
|
switch(expr->type) {
|
|
|
|
case EXPR_ADD:
|
|
|
|
case EXPR_AND:
|
|
|
|
case EXPR_ARRAY:
|
|
|
|
case EXPR_ASSIGN:
|
|
|
|
case EXPR_ASSIGNADD:
|
|
|
|
case EXPR_ASSIGNAND:
|
|
|
|
case EXPR_ASSIGNSUB:
|
|
|
|
case EXPR_ASSIGNMUL:
|
|
|
|
case EXPR_ASSIGNDIV:
|
|
|
|
case EXPR_ASSIGNMOD:
|
|
|
|
case EXPR_ASSIGNOR:
|
|
|
|
case EXPR_ASSIGNLSHIFT:
|
|
|
|
case EXPR_ASSIGNRSHIFT:
|
|
|
|
case EXPR_ASSIGNRRSHIFT:
|
|
|
|
case EXPR_ASSIGNXOR:
|
|
|
|
case EXPR_BAND:
|
|
|
|
case EXPR_BOR:
|
|
|
|
case EXPR_COMMA:
|
|
|
|
case EXPR_DIV:
|
|
|
|
case EXPR_EQ:
|
|
|
|
case EXPR_EQEQ:
|
|
|
|
case EXPR_GREATER:
|
|
|
|
case EXPR_GREATEREQ:
|
|
|
|
case EXPR_IN:
|
|
|
|
case EXPR_INSTANCEOF:
|
|
|
|
case EXPR_LESS:
|
|
|
|
case EXPR_LESSEQ:
|
|
|
|
case EXPR_LSHIFT:
|
|
|
|
case EXPR_MOD:
|
|
|
|
case EXPR_MUL:
|
|
|
|
case EXPR_NOTEQ:
|
|
|
|
case EXPR_NOTEQEQ:
|
|
|
|
case EXPR_OR:
|
|
|
|
case EXPR_RSHIFT:
|
|
|
|
case EXPR_RRSHIFT:
|
|
|
|
case EXPR_SUB:
|
|
|
|
case EXPR_BXOR: {
|
|
|
|
binary_expression_t *binary_expr = (binary_expression_t*)expr;
|
|
|
|
|
|
|
|
hres = visit_expression(ctx, binary_expr->expression1);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
hres = visit_expression(ctx, binary_expr->expression2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case EXPR_BITNEG:
|
|
|
|
case EXPR_DELETE:
|
|
|
|
case EXPR_LOGNEG:
|
|
|
|
case EXPR_MINUS:
|
|
|
|
case EXPR_PLUS:
|
|
|
|
case EXPR_POSTDEC:
|
|
|
|
case EXPR_POSTINC:
|
|
|
|
case EXPR_PREDEC:
|
|
|
|
case EXPR_PREINC:
|
|
|
|
case EXPR_TYPEOF:
|
|
|
|
case EXPR_VOID:
|
|
|
|
hres = visit_expression(ctx, ((unary_expression_t*)expr)->expression);
|
|
|
|
break;
|
|
|
|
case EXPR_IDENT:
|
|
|
|
case EXPR_LITERAL:
|
|
|
|
case EXPR_THIS:
|
|
|
|
break;
|
|
|
|
case EXPR_ARRAYLIT: {
|
|
|
|
array_literal_expression_t *array_expr = (array_literal_expression_t*)expr;
|
|
|
|
array_element_t *iter;
|
|
|
|
|
|
|
|
for(iter = array_expr->element_list; iter; iter = iter->next) {
|
|
|
|
hres = visit_expression(ctx, iter->expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case EXPR_CALL:
|
|
|
|
case EXPR_NEW: {
|
|
|
|
call_expression_t *call_expr = (call_expression_t*)expr;
|
|
|
|
argument_t *arg;
|
|
|
|
|
|
|
|
hres = visit_expression(ctx, call_expr->expression);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
for(arg = call_expr->argument_list; arg; arg = arg->next) {
|
|
|
|
hres = visit_expression(ctx, arg->expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case EXPR_COND: {
|
|
|
|
conditional_expression_t *cond_expr = (conditional_expression_t*)expr;
|
|
|
|
|
|
|
|
hres = visit_expression(ctx, cond_expr->expression);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
hres = visit_expression(ctx, cond_expr->true_expression);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
hres = visit_expression(ctx, cond_expr->false_expression);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case EXPR_FUNC:
|
|
|
|
visit_function_expression(ctx, (function_expression_t*)expr);
|
|
|
|
break;
|
|
|
|
case EXPR_MEMBER:
|
|
|
|
hres = visit_expression(ctx, ((member_expression_t*)expr)->expression);
|
|
|
|
break;
|
|
|
|
case EXPR_PROPVAL: {
|
|
|
|
prop_val_t *iter;
|
|
|
|
for(iter = ((property_value_expression_t*)expr)->property_list; iter; iter = iter->next) {
|
|
|
|
hres = visit_expression(ctx, iter->value);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
DEFAULT_UNREACHABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT visit_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
|
|
|
|
{
|
|
|
|
variable_declaration_t *iter;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
for(iter = list; iter; iter = iter->next) {
|
|
|
|
if(!alloc_variable(ctx, iter->identifier))
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
if(iter->expr) {
|
|
|
|
hres = visit_expression(ctx, iter->expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT visit_statement(compiler_ctx_t*,statement_t*);
|
|
|
|
|
|
|
|
static HRESULT visit_block_statement(compiler_ctx_t *ctx, statement_t *iter)
|
|
|
|
{
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
while(iter) {
|
|
|
|
hres = visit_statement(ctx, iter);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
iter = iter->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT visit_statement(compiler_ctx_t *ctx, statement_t *stat)
|
|
|
|
{
|
|
|
|
HRESULT hres = S_OK;
|
|
|
|
|
|
|
|
switch(stat->type) {
|
|
|
|
case STAT_BLOCK:
|
|
|
|
hres = visit_block_statement(ctx, ((block_statement_t*)stat)->stat_list);
|
|
|
|
break;
|
|
|
|
case STAT_BREAK:
|
|
|
|
case STAT_CONTINUE:
|
|
|
|
case STAT_EMPTY:
|
|
|
|
break;
|
|
|
|
case STAT_EXPR:
|
|
|
|
case STAT_RETURN:
|
|
|
|
case STAT_THROW: {
|
|
|
|
expression_statement_t *expr_stat = (expression_statement_t*)stat;
|
|
|
|
if(expr_stat->expr)
|
|
|
|
hres = visit_expression(ctx, expr_stat->expr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case STAT_FOR: {
|
|
|
|
for_statement_t *for_stat = (for_statement_t*)stat;
|
|
|
|
|
|
|
|
if(for_stat->variable_list)
|
|
|
|
hres = visit_variable_list(ctx, for_stat->variable_list);
|
|
|
|
else if(for_stat->begin_expr)
|
|
|
|
hres = visit_expression(ctx, for_stat->begin_expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(for_stat->expr) {
|
|
|
|
hres = visit_expression(ctx, for_stat->expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
hres = visit_statement(ctx, for_stat->statement);
|
|
|
|
if(FAILED(hres))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(for_stat->end_expr)
|
|
|
|
hres = visit_expression(ctx, for_stat->end_expr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case STAT_FORIN: {
|
|
|
|
forin_statement_t *forin_stat = (forin_statement_t*)stat;
|
|
|
|
|
|
|
|
if(forin_stat->variable) {
|
|
|
|
hres = visit_variable_list(ctx, forin_stat->variable);
|
|
|
|
if(FAILED(hres))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
hres = visit_expression(ctx, forin_stat->in_expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(forin_stat->expr) {
|
|
|
|
hres = visit_expression(ctx, forin_stat->expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
hres = visit_statement(ctx, forin_stat->statement);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case STAT_IF: {
|
|
|
|
if_statement_t *if_stat = (if_statement_t*)stat;
|
|
|
|
|
|
|
|
hres = visit_expression(ctx, if_stat->expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
hres = visit_statement(ctx, if_stat->if_stat);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(if_stat->else_stat)
|
|
|
|
hres = visit_statement(ctx, if_stat->else_stat);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case STAT_LABEL:
|
|
|
|
hres = visit_statement(ctx, ((labelled_statement_t*)stat)->statement);
|
|
|
|
break;
|
|
|
|
case STAT_SWITCH: {
|
|
|
|
switch_statement_t *switch_stat = (switch_statement_t*)stat;
|
|
|
|
statement_t *stat_iter;
|
|
|
|
case_clausule_t *iter;
|
|
|
|
|
|
|
|
hres = visit_expression(ctx, switch_stat->expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
for(iter = switch_stat->case_list; iter; iter = iter->next) {
|
|
|
|
if(!iter->expr)
|
|
|
|
continue;
|
|
|
|
hres = visit_expression(ctx, iter->expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(iter = switch_stat->case_list; iter; iter = iter->next) {
|
|
|
|
while(iter->next && iter->next->stat == iter->stat)
|
|
|
|
iter = iter->next;
|
|
|
|
for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter);
|
|
|
|
stat_iter = stat_iter->next) {
|
|
|
|
hres = visit_statement(ctx, stat_iter);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case STAT_TRY: {
|
|
|
|
try_statement_t *try_stat = (try_statement_t*)stat;
|
|
|
|
|
|
|
|
hres = visit_statement(ctx, try_stat->try_statement);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(try_stat->catch_block) {
|
|
|
|
hres = visit_statement(ctx, try_stat->catch_block->statement);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(try_stat->finally_statement)
|
|
|
|
hres = visit_statement(ctx, try_stat->finally_statement);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case STAT_VAR:
|
|
|
|
hres = visit_variable_list(ctx, ((var_statement_t*)stat)->variable_list);
|
|
|
|
break;
|
|
|
|
case STAT_WHILE: {
|
|
|
|
while_statement_t *while_stat = (while_statement_t*)stat;
|
|
|
|
|
|
|
|
hres = visit_expression(ctx, while_stat->expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
hres = visit_statement(ctx, while_stat->statement);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case STAT_WITH: {
|
|
|
|
with_statement_t *with_stat = (with_statement_t*)stat;
|
|
|
|
|
|
|
|
hres = visit_expression(ctx, with_stat->expr);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
hres = visit_statement(ctx, with_stat->statement);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
DEFAULT_UNREACHABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2011-12-28 12:06:41 +01:00
|
|
|
static void resolve_labels(compiler_ctx_t *ctx, unsigned off)
|
|
|
|
{
|
|
|
|
instr_t *instr;
|
|
|
|
|
|
|
|
for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
|
2012-05-12 16:22:37 +02:00
|
|
|
if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->u.arg->uint & LABEL_FLAG)) {
|
|
|
|
assert((instr->u.arg->uint & ~LABEL_FLAG) < ctx->labels_cnt);
|
|
|
|
instr->u.arg->uint = ctx->labels[instr->u.arg->uint & ~LABEL_FLAG];
|
2011-12-28 12:06:41 +01:00
|
|
|
}
|
|
|
|
assert(instr_info[instr->op].arg2_type != ARG_ADDR);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->labels_cnt = 0;
|
2011-12-19 14:58:42 +01:00
|
|
|
}
|
|
|
|
|
2011-11-18 14:09:44 +01:00
|
|
|
void release_bytecode(bytecode_t *code)
|
|
|
|
{
|
2011-11-28 12:08:54 +01:00
|
|
|
unsigned i;
|
|
|
|
|
2012-03-12 19:24:22 +01:00
|
|
|
if(--code->ref)
|
|
|
|
return;
|
|
|
|
|
2011-11-28 12:08:54 +01:00
|
|
|
for(i=0; i < code->bstr_cnt; i++)
|
|
|
|
SysFreeString(code->bstr_pool[i]);
|
2012-10-11 12:17:17 +02:00
|
|
|
for(i=0; i < code->str_cnt; i++)
|
|
|
|
jsstr_release(code->str_pool[i]);
|
2011-11-28 12:08:54 +01:00
|
|
|
|
2012-03-26 11:41:38 +02:00
|
|
|
heap_free(code->source);
|
2013-02-15 14:04:14 +01:00
|
|
|
heap_pool_free(&code->heap);
|
2011-11-28 12:08:54 +01:00
|
|
|
heap_free(code->bstr_pool);
|
2012-10-11 12:17:17 +02:00
|
|
|
heap_free(code->str_pool);
|
2011-11-18 14:09:44 +01:00
|
|
|
heap_free(code->instrs);
|
|
|
|
heap_free(code);
|
|
|
|
}
|
|
|
|
|
2012-03-26 11:41:38 +02:00
|
|
|
static HRESULT init_code(compiler_ctx_t *compiler, const WCHAR *source)
|
2011-11-18 14:09:44 +01:00
|
|
|
{
|
2012-01-02 11:45:54 +01:00
|
|
|
compiler->code = heap_alloc_zero(sizeof(bytecode_t));
|
2012-03-12 19:23:19 +01:00
|
|
|
if(!compiler->code)
|
2012-01-02 11:45:54 +01:00
|
|
|
return E_OUTOFMEMORY;
|
2011-11-18 14:09:44 +01:00
|
|
|
|
2012-03-12 19:24:22 +01:00
|
|
|
compiler->code->ref = 1;
|
2013-02-15 14:04:14 +01:00
|
|
|
heap_pool_init(&compiler->code->heap);
|
2011-11-18 14:09:44 +01:00
|
|
|
|
2012-03-26 11:41:38 +02:00
|
|
|
compiler->code->source = heap_strdupW(source);
|
|
|
|
if(!compiler->code->source) {
|
|
|
|
release_bytecode(compiler->code);
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
2012-01-02 11:45:54 +01:00
|
|
|
compiler->code->instrs = heap_alloc(64 * sizeof(instr_t));
|
|
|
|
if(!compiler->code->instrs) {
|
|
|
|
release_bytecode(compiler->code);
|
|
|
|
return E_OUTOFMEMORY;
|
2011-11-18 14:09:44 +01:00
|
|
|
}
|
|
|
|
|
2012-01-02 11:45:54 +01:00
|
|
|
compiler->code_size = 64;
|
2012-01-02 11:46:11 +01:00
|
|
|
compiler->code_off = 1;
|
2011-12-19 14:58:42 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-24 17:39:25 +02:00
|
|
|
static HRESULT compile_function(compiler_ctx_t *ctx, source_elements_t *source, function_expression_t *func_expr,
|
|
|
|
BOOL from_eval, function_code_t *func)
|
2011-12-19 14:58:42 +01:00
|
|
|
{
|
2012-04-26 16:17:31 +02:00
|
|
|
function_expression_t *iter;
|
2016-08-04 13:39:36 +02:00
|
|
|
unsigned off, i, j;
|
2011-12-19 14:58:42 +01:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-04-26 16:17:31 +02:00
|
|
|
ctx->func_head = ctx->func_tail = NULL;
|
2012-12-12 13:12:04 +01:00
|
|
|
ctx->from_eval = from_eval;
|
2016-08-03 16:27:35 +02:00
|
|
|
ctx->func = func;
|
|
|
|
ctx->locals_cnt = 0;
|
|
|
|
|
|
|
|
if(func_expr) {
|
|
|
|
parameter_t *param_iter;
|
|
|
|
|
2016-08-04 13:39:46 +02:00
|
|
|
if(func_expr->identifier) {
|
|
|
|
func->name = compiler_alloc_bstr(ctx, func_expr->identifier);
|
|
|
|
if(!func->name)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(func_expr->event_target) {
|
|
|
|
func->event_target = compiler_alloc_bstr(ctx, func_expr->event_target);
|
|
|
|
if(!func->event_target)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
2016-08-03 16:27:35 +02:00
|
|
|
func->source = func_expr->src_str;
|
|
|
|
func->source_len = func_expr->src_len;
|
|
|
|
|
|
|
|
for(param_iter = func_expr->parameter_list; param_iter; param_iter = param_iter->next)
|
|
|
|
func->param_cnt++;
|
|
|
|
|
|
|
|
func->params = compiler_alloc(ctx->code, func->param_cnt * sizeof(*func->params));
|
|
|
|
if(!func->params)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
for(param_iter = func_expr->parameter_list, i=0; param_iter; param_iter = param_iter->next, i++) {
|
|
|
|
func->params[i] = compiler_alloc_bstr(ctx, param_iter->identifier);
|
|
|
|
if(!func->params[i])
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0; i < func->param_cnt; i++) {
|
|
|
|
if(!find_local(ctx, func->params[i]) && !alloc_local(ctx, func->params[i], -i-1))
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
2012-04-25 11:26:21 +02:00
|
|
|
|
2016-08-04 13:39:36 +02:00
|
|
|
hres = visit_block_statement(ctx, source->statement);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
func->locals = compiler_alloc(ctx->code, ctx->locals_cnt * sizeof(*func->locals));
|
|
|
|
if(!func->locals)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
func->locals_cnt = ctx->locals_cnt;
|
|
|
|
memcpy(func->locals, ctx->locals_buf, func->locals_cnt * sizeof(*func->locals));
|
|
|
|
|
2012-04-25 11:26:21 +02:00
|
|
|
func->variables = compiler_alloc(ctx->code, func->var_cnt * sizeof(*func->variables));
|
|
|
|
if(!func->variables)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2016-08-04 13:39:36 +02:00
|
|
|
for(i = 0, j = 0; i < func->locals_cnt; i++) {
|
|
|
|
if(func->locals[i].ref < 0)
|
|
|
|
continue; /* skip arguments */
|
2016-08-04 13:39:46 +02:00
|
|
|
func->variables[func->locals[i].ref].name = func->locals[i].name;
|
|
|
|
func->variables[func->locals[i].ref].func_id = -1;
|
2016-08-04 13:39:36 +02:00
|
|
|
j++;
|
2012-04-25 11:26:21 +02:00
|
|
|
}
|
|
|
|
|
2016-08-04 13:39:36 +02:00
|
|
|
assert(j == func->var_cnt);
|
2016-08-03 16:27:35 +02:00
|
|
|
|
2012-04-25 11:25:44 +02:00
|
|
|
func->funcs = compiler_alloc(ctx->code, func->func_cnt * sizeof(*func->funcs));
|
2012-04-24 17:39:09 +02:00
|
|
|
if(!func->funcs)
|
|
|
|
return E_OUTOFMEMORY;
|
2012-04-25 11:25:44 +02:00
|
|
|
memset(func->funcs, 0, func->func_cnt * sizeof(*func->funcs));
|
2012-03-12 19:22:36 +01:00
|
|
|
|
2016-08-04 13:39:46 +02:00
|
|
|
off = ctx->code_off;
|
|
|
|
hres = compile_block_statement(ctx, source->statement);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
resolve_labels(ctx, off);
|
|
|
|
|
|
|
|
hres = push_instr_uint(ctx, OP_ret, !from_eval);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
if(TRACE_ON(jscript_disas))
|
|
|
|
dump_code(ctx, off);
|
|
|
|
|
|
|
|
func->instr_off = off;
|
|
|
|
|
2012-04-26 16:17:31 +02:00
|
|
|
for(iter = ctx->func_head, i=0; iter; iter = iter->next, i++) {
|
|
|
|
hres = compile_function(ctx, iter->source_elements, iter, FALSE, func->funcs+i);
|
2012-03-12 19:22:36 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
2016-08-04 13:39:46 +02:00
|
|
|
|
|
|
|
TRACE("[%d] func %s\n", i, debugstr_w(func->funcs[i].name));
|
|
|
|
if(func->funcs[i].name && !func->funcs[i].event_target) {
|
|
|
|
local_ref_t *local_ref = lookup_local(func, func->funcs[i].name);
|
|
|
|
func->funcs[i].local_ref = local_ref->ref;
|
|
|
|
TRACE("found ref %s %d for %s\n", debugstr_w(local_ref->name), local_ref->ref, debugstr_w(func->funcs[i].name));
|
|
|
|
if(local_ref->ref >= 0)
|
|
|
|
func->variables[local_ref->ref].func_id = i;
|
|
|
|
}
|
2012-03-12 19:22:36 +01:00
|
|
|
}
|
2011-12-29 11:09:51 +01:00
|
|
|
|
2012-04-24 17:39:09 +02:00
|
|
|
assert(i == func->func_cnt);
|
2012-04-25 10:47:56 +02:00
|
|
|
|
2011-12-29 11:09:51 +01:00
|
|
|
return S_OK;
|
2011-12-19 14:58:42 +01:00
|
|
|
}
|
2012-03-12 19:22:36 +01:00
|
|
|
|
2012-10-04 14:58:05 +02:00
|
|
|
static HRESULT parse_arguments(compiler_ctx_t *ctx, const WCHAR *args, BSTR *arg_array, unsigned *args_size)
|
|
|
|
{
|
|
|
|
const WCHAR *ptr = args, *ptr2;
|
|
|
|
unsigned arg_cnt = 0;
|
|
|
|
|
|
|
|
while(isspaceW(*ptr))
|
|
|
|
ptr++;
|
|
|
|
if(!*ptr) {
|
|
|
|
if(args_size)
|
|
|
|
*args_size = 0;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
if(!isalphaW(*ptr) && *ptr != '_') {
|
|
|
|
FIXME("expected alpha or '_': %s\n", debugstr_w(ptr));
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr2 = ptr;
|
|
|
|
while(isalnumW(*ptr) || *ptr == '_')
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
if(*ptr && *ptr != ',' && !isspaceW(*ptr)) {
|
|
|
|
FIXME("unexpected har %s\n", debugstr_w(ptr));
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(arg_array) {
|
|
|
|
arg_array[arg_cnt] = compiler_alloc_bstr_len(ctx, ptr2, ptr-ptr2);
|
|
|
|
if(!arg_array[arg_cnt])
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
arg_cnt++;
|
|
|
|
|
|
|
|
while(isspaceW(*ptr))
|
|
|
|
ptr++;
|
|
|
|
if(!*ptr)
|
|
|
|
break;
|
|
|
|
if(*ptr != ',') {
|
|
|
|
FIXME("expected ',': %s\n", debugstr_w(ptr));
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr++;
|
|
|
|
while(isspaceW(*ptr))
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(args_size)
|
|
|
|
*args_size = arg_cnt;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT compile_arguments(compiler_ctx_t *ctx, const WCHAR *args)
|
|
|
|
{
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
hres = parse_arguments(ctx, args, NULL, &ctx->code->global_code.param_cnt);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
|
|
|
ctx->code->global_code.params = compiler_alloc(ctx->code,
|
|
|
|
ctx->code->global_code.param_cnt * sizeof(*ctx->code->global_code.params));
|
|
|
|
if(!ctx->code->global_code.params)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
return parse_arguments(ctx, args, ctx->code->global_code.params, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT compile_script(script_ctx_t *ctx, const WCHAR *code, const WCHAR *args, const WCHAR *delimiter,
|
|
|
|
BOOL from_eval, BOOL use_decode, bytecode_t **ret)
|
2012-03-12 19:22:36 +01:00
|
|
|
{
|
2012-03-12 19:23:19 +01:00
|
|
|
compiler_ctx_t compiler = {0};
|
2012-03-12 19:22:36 +01:00
|
|
|
HRESULT hres;
|
|
|
|
|
2012-03-26 11:41:38 +02:00
|
|
|
hres = init_code(&compiler, code);
|
2012-03-12 19:22:36 +01:00
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
|
2012-10-04 14:58:05 +02:00
|
|
|
if(args) {
|
|
|
|
hres = compile_arguments(&compiler, args);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2012-03-26 11:42:51 +02:00
|
|
|
if(use_decode) {
|
|
|
|
hres = decode_source(compiler.code->source);
|
|
|
|
if(FAILED(hres)) {
|
|
|
|
WARN("Decoding failed\n");
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-26 11:41:38 +02:00
|
|
|
hres = script_parse(ctx, compiler.code->source, delimiter, from_eval, &compiler.parser);
|
2012-03-12 19:24:22 +01:00
|
|
|
if(FAILED(hres)) {
|
2012-03-26 11:41:38 +02:00
|
|
|
release_bytecode(compiler.code);
|
2012-03-12 19:24:22 +01:00
|
|
|
return hres;
|
2012-03-12 19:23:19 +01:00
|
|
|
}
|
|
|
|
|
2012-04-24 17:39:25 +02:00
|
|
|
hres = compile_function(&compiler, compiler.parser->source, NULL, from_eval, &compiler.code->global_code);
|
2012-04-25 11:26:12 +02:00
|
|
|
parser_release(compiler.parser);
|
2016-08-03 16:27:35 +02:00
|
|
|
heap_free(compiler.locals_buf);
|
2012-03-12 19:22:59 +01:00
|
|
|
if(FAILED(hres)) {
|
2012-03-12 19:24:22 +01:00
|
|
|
release_bytecode(compiler.code);
|
2012-03-12 19:22:59 +01:00
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2012-03-12 19:24:22 +01:00
|
|
|
*ret = compiler.code;
|
2012-03-12 19:22:59 +01:00
|
|
|
return S_OK;
|
2012-03-12 19:22:36 +01:00
|
|
|
}
|