static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat) { unsigned cnd_jmp, endif_label = -1; elseif_decl_t *elseif_decl; HRESULT hres; hres = compile_expression(ctx, stat->expr); if(FAILED(hres)) return hres; cnd_jmp = push_instr(ctx, OP_jmp_false); if(cnd_jmp == -1) return E_OUTOFMEMORY; hres = compile_statement(ctx, stat->if_stat); if(FAILED(hres)) return hres; if(stat->else_stat || stat->elseifs) { endif_label = alloc_label(ctx); if(endif_label == -1) return E_OUTOFMEMORY; hres = push_instr_addr(ctx, OP_jmp, endif_label); if(FAILED(hres)) return hres; } for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) { instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt; hres = compile_expression(ctx, elseif_decl->expr); if(FAILED(hres)) return hres; cnd_jmp = push_instr(ctx, OP_jmp_false); if(cnd_jmp == -1) return E_OUTOFMEMORY; hres = compile_statement(ctx, elseif_decl->stat); if(FAILED(hres)) return hres; hres = push_instr_addr(ctx, OP_jmp, endif_label); if(FAILED(hres)) return hres; } instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt; if(stat->else_stat) { hres = compile_statement(ctx, stat->else_stat); if(FAILED(hres)) return hres; } if(endif_label != -1) label_set_addr(ctx, endif_label); return S_OK; }
static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal) { switch(literal->type) { case LT_BOOL: return push_instr_int(ctx, OP_bool, literal->u.bval); case LT_DOUBLE: return push_instr_double(ctx, OP_double, literal->u.dval); case LT_INT: return push_instr_int(ctx, OP_int, literal->u.lval); case LT_NULL: return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY; case LT_STRING: return push_instr_str(ctx, OP_str, literal->u.wstr); case LT_REGEXP: { unsigned instr; WCHAR *str; str = compiler_alloc(ctx->code, (literal->u.regexp.str_len+1)*sizeof(WCHAR)); if(!str) return E_OUTOFMEMORY; memcpy(str, literal->u.regexp.str, literal->u.regexp.str_len*sizeof(WCHAR)); str[literal->u.regexp.str_len] = 0; instr = push_instr(ctx, OP_regexp); if(!instr) return E_OUTOFMEMORY; instr_ptr(ctx, instr)->arg1.str = str; instr_ptr(ctx, instr)->arg2.lng = literal->u.regexp.flags; return S_OK; } default: assert(0); } }
/* 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; hres = compile_expression(ctx, expr->expression); if(FAILED(hres)) return hres; jmp_false = push_instr(ctx, OP_cnd_z); if(!jmp_false) return E_OUTOFMEMORY; hres = compile_expression(ctx, expr->true_expression); if(FAILED(hres)) return hres; jmp_end = push_instr(ctx, OP_jmp); if(!jmp_end) return E_OUTOFMEMORY; instr_ptr(ctx, jmp_false)->arg1.uint = ctx->code_off; if(!push_instr(ctx, OP_pop)) return E_OUTOFMEMORY; hres = compile_expression(ctx, expr->false_expression); if(FAILED(hres)) return hres; instr_ptr(ctx, jmp_end)->arg1.uint = ctx->code_off; return S_OK; }
static HRESULT compile_object_literal(compiler_ctx_t *ctx, property_value_expression_t *expr) { prop_val_t *iter; unsigned instr; BSTR name; HRESULT hres; if(!push_instr(ctx, OP_new_obj)) 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; hres = compile_expression(ctx, iter->value); if(FAILED(hres)) return hres; instr = push_instr(ctx, OP_obj_prop); if(!instr) return E_OUTOFMEMORY; instr_ptr(ctx, instr)->arg1.bstr = name; } return S_OK; }
static HRESULT push_instr_uint_str(compiler_ctx_t *ctx, jsop_t op, unsigned arg1, const WCHAR *arg2) { unsigned instr; WCHAR *str; str = compiler_alloc_string(ctx->code, arg2); if(!str) return E_OUTOFMEMORY; instr = push_instr(ctx, op); if(!instr) return E_OUTOFMEMORY; instr_ptr(ctx, instr)->arg1.uint = arg1; instr_ptr(ctx, instr)->arg2.str = str; return S_OK; }
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); if(!instr) return E_OUTOFMEMORY; instr_ptr(ctx, instr)->arg1.bstr = str; instr_ptr(ctx, instr)->arg2.uint = arg2; return S_OK; }
static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2) { unsigned instr; BSTR bstr; bstr = alloc_bstr_arg(ctx, arg1); if(!bstr) return E_OUTOFMEMORY; instr = push_instr(ctx, op); if(instr == -1) return E_OUTOFMEMORY; instr_ptr(ctx, instr)->arg1.bstr = bstr; instr_ptr(ctx, instr)->arg2.uint = arg2; return S_OK; }
static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg) { unsigned instr; instr = push_instr(ctx, op); if(!instr) return E_OUTOFMEMORY; instr_ptr(ctx, instr)->arg1.uint = arg; return S_OK; }
static HRESULT push_instr_int(compiler_ctx_t *ctx, jsop_t op, LONG arg) { unsigned instr; instr = push_instr(ctx, op); if(instr == -1) return E_OUTOFMEMORY; instr_ptr(ctx, instr)->arg1.lng = arg; return S_OK; }
static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg) { unsigned ret; ret = push_instr(ctx, op); if(ret == -1) return E_OUTOFMEMORY; instr_ptr(ctx, ret)->arg1.uint = arg; return S_OK; }
static HRESULT compile_interp_fallback(compiler_ctx_t *ctx, expression_t *expr) { unsigned instr; instr = push_instr(ctx, OP_tree); if(instr == -1) return E_OUTOFMEMORY; instr_ptr(ctx, instr)->arg1.expr = expr; return S_OK; }
static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg) { unsigned ret; ret = push_instr(ctx, op); if(!ret) return E_OUTOFMEMORY; instr_ptr(ctx, ret)->arg1.lng = arg; return S_OK; }
/* ECMA-262 3rd Edition 12.5 */ static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat) { unsigned jmp_else, jmp_end; HRESULT hres; hres = compile_expression(ctx, stat->expr); if(FAILED(hres)) return hres; jmp_else = push_instr(ctx, OP_jmp_z); if(!jmp_else) return E_OUTOFMEMORY; hres = compile_statement(ctx, NULL, stat->if_stat); if(FAILED(hres)) return hres; jmp_end = push_instr(ctx, OP_jmp); if(!jmp_end) return E_OUTOFMEMORY; instr_ptr(ctx, jmp_else)->arg1.uint = ctx->code_off; if(stat->else_stat) { hres = compile_statement(ctx, NULL, stat->else_stat); if(FAILED(hres)) return hres; }else { /* FIXME: We could sometimes avoid it */ if(!push_instr(ctx, OP_undefined)) return E_OUTOFMEMORY; } instr_ptr(ctx, jmp_end)->arg1.uint = ctx->code_off; return S_OK; }
static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr) { unsigned instr; /* FIXME: not exactly right */ if(expr->identifier) return push_instr_bstr(ctx, OP_ident, expr->identifier); instr = push_instr(ctx, OP_func); if(!instr) return E_OUTOFMEMORY; instr_ptr(ctx, instr)->arg1.func = expr; return S_OK; }
static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL *no_ret) { unsigned arg_cnt = 0; argument_t *arg; unsigned instr; jsop_t op; HRESULT hres; if(is_memberid_expr(expr->expression->type)) { op = OP_call_member; hres = compile_memberid_expression(ctx, expr->expression, 0); }else { op = OP_call; hres = compile_expression(ctx, expr->expression); } if(FAILED(hres)) return hres; for(arg = expr->argument_list; arg; arg = arg->next) { hres = compile_expression(ctx, arg->expr); if(FAILED(hres)) return hres; arg_cnt++; } instr = push_instr(ctx, op); if(!instr) return E_OUTOFMEMORY; instr_ptr(ctx, instr)->arg1.uint = arg_cnt; instr_ptr(ctx, instr)->arg2.lng = no_ret == NULL; if(no_ret) *no_ret = TRUE; return S_OK; }
static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg) { unsigned instr; WCHAR *str; str = compiler_alloc_string(ctx->code, arg); if(!str) return E_OUTOFMEMORY; instr = push_instr(ctx, op); if(instr == -1) return E_OUTOFMEMORY; instr_ptr(ctx, instr)->arg1.str = str; return S_OK; }
static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg) { unsigned instr; double *d; d = compiler_alloc(ctx->code, sizeof(double)); if(!d) return E_OUTOFMEMORY; instr = push_instr(ctx, op); if(instr == -1) return E_OUTOFMEMORY; *d = arg; instr_ptr(ctx, instr)->arg1.dbl = d; return S_OK; }
static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg) { unsigned instr; DOUBLE *dbl; dbl = compiler_alloc(ctx->code, sizeof(arg)); if(!dbl) return E_OUTOFMEMORY; *dbl = arg; instr = push_instr(ctx, op); if(!instr) return E_OUTOFMEMORY; instr_ptr(ctx, instr)->arg1.dbl = dbl; return S_OK; }
/* 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; hres = compile_expression(ctx, expr->expression1); if(FAILED(hres)) return hres; instr = push_instr(ctx, op); if(!instr) return E_OUTOFMEMORY; hres = compile_expression(ctx, expr->expression2); if(FAILED(hres)) return hres; instr_ptr(ctx, instr)->arg1.uint = ctx->code_off; return S_OK; }
static HRESULT compile_while_statement(compile_ctx_t *ctx, while_statement_t *stat) { statement_ctx_t stat_ctx = {0}, *loop_ctx; unsigned start_addr; unsigned jmp_end; HRESULT hres; start_addr = ctx->instr_cnt; hres = compile_expression(ctx, stat->expr); if(FAILED(hres)) return hres; jmp_end = push_instr(ctx, stat->stat.type == STAT_UNTIL ? OP_jmp_true : OP_jmp_false); if(!jmp_end) return E_OUTOFMEMORY; if(stat->stat.type == STAT_WHILE) { loop_ctx = NULL; }else { if(!(stat_ctx.while_end_label = alloc_label(ctx))) return E_OUTOFMEMORY; loop_ctx = &stat_ctx; } hres = compile_statement(ctx, loop_ctx, stat->body); if(FAILED(hres)) return hres; hres = push_instr_addr(ctx, OP_jmp, start_addr); if(FAILED(hres)) return hres; instr_ptr(ctx, jmp_end)->arg1.uint = ctx->instr_cnt; if(loop_ctx) label_set_addr(ctx, stat_ctx.while_end_label); return S_OK; }
static HRESULT compile_while_statement(compile_ctx_t *ctx, while_statement_t *stat) { unsigned start_addr, prev_label; unsigned jmp_end; HRESULT hres; start_addr = ctx->instr_cnt; hres = compile_expression(ctx, stat->expr); if(FAILED(hres)) return hres; jmp_end = push_instr(ctx, stat->stat.type == STAT_UNTIL ? OP_jmp_true : OP_jmp_false); if(jmp_end == -1) return E_OUTOFMEMORY; prev_label = ctx->while_end_label; if(stat->stat.type != STAT_WHILE && (ctx->while_end_label = alloc_label(ctx)) == -1) return E_OUTOFMEMORY; hres = compile_statement(ctx, stat->body); if(FAILED(hres)) return hres; hres = push_instr_addr(ctx, OP_jmp, start_addr); if(FAILED(hres)) return hres; instr_ptr(ctx, jmp_end)->arg1.uint = ctx->instr_cnt; if(stat->stat.type != STAT_WHILE) { label_set_addr(ctx, ctx->while_end_label); ctx->while_end_label = prev_label; } return S_OK; }
/* ECMA-262 3rd Edition 12.13 */ static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat) { statement_ctx_t stat_ctx = {0, FALSE, FALSE}; unsigned case_cnt = 0, *case_jmps, i, default_jmp; BOOL have_default = FALSE; statement_t *stat_iter; case_clausule_t *iter; HRESULT hres; hres = compile_expression(ctx, stat->expr); if(FAILED(hres)) return hres; stat_ctx.break_label = alloc_label(ctx); if(!stat_ctx.break_label) return E_OUTOFMEMORY; 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; } hres = compile_expression(ctx, iter->expr); if(FAILED(hres)) break; case_jmps[i] = push_instr(ctx, OP_case); if(!case_jmps[i]) { hres = E_OUTOFMEMORY; break; } i++; } if(SUCCEEDED(hres)) { if(push_instr(ctx, OP_pop)) { default_jmp = push_instr(ctx, OP_jmp); if(!default_jmp) hres = E_OUTOFMEMORY; }else { 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) { instr_ptr(ctx, iter->expr ? case_jmps[i++] : default_jmp)->arg1.uint = ctx->code_off; iter = iter->next; } instr_ptr(ctx, iter->expr ? case_jmps[i++] : default_jmp)->arg1.uint = ctx->code_off; 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); if(FAILED(hres)) break; if(stat_iter->next && !push_instr(ctx, OP_pop)) { hres = E_OUTOFMEMORY; break; } } if(FAILED(hres)) break; } heap_free(case_jmps); if(FAILED(hres)) return hres; assert(i == case_cnt); if(!have_default) instr_ptr(ctx, default_jmp)->arg1.uint = ctx->code_off; label_set_addr(ctx, stat_ctx.break_label); return S_OK; }
/* ECMA-262 3rd Edition 12.14 */ static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat) { statement_ctx_t try_ctx = {0, FALSE, TRUE}, catch_ctx = {0, TRUE, FALSE}; statement_ctx_t finally_ctx = {2, FALSE, FALSE}; unsigned push_except; BSTR ident; HRESULT hres; push_except = push_instr(ctx, OP_push_except); if(!push_except) return E_OUTOFMEMORY; if(stat->catch_block) { ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier); if(!ident) return E_OUTOFMEMORY; }else { ident = NULL; } instr_ptr(ctx, push_except)->arg2.bstr = ident; if(!stat->catch_block) try_ctx.stack_use = 2; hres = compile_statement(ctx, &try_ctx, stat->try_statement); if(FAILED(hres)) return hres; if(!push_instr(ctx, OP_pop_except)) return E_OUTOFMEMORY; if(stat->catch_block) { unsigned jmp_finally; jmp_finally = push_instr(ctx, OP_jmp); if(!jmp_finally) return E_OUTOFMEMORY; instr_ptr(ctx, push_except)->arg1.uint = ctx->code_off; hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement); if(FAILED(hres)) return hres; if(!push_instr(ctx, OP_pop_scope)) return E_OUTOFMEMORY; instr_ptr(ctx, jmp_finally)->arg1.uint = ctx->code_off; }else { instr_ptr(ctx, push_except)->arg1.uint = ctx->code_off; } if(stat->finally_statement) { /* FIXME: avoid */ if(!push_instr(ctx, OP_pop)) return E_OUTOFMEMORY; hres = compile_statement(ctx, stat->catch_block ? NULL : &finally_ctx, stat->finally_statement); if(FAILED(hres)) return hres; if(!stat->catch_block && !push_instr(ctx, OP_end_finally)) return E_OUTOFMEMORY; } return S_OK; }
static HRESULT compile_forto_statement(compile_ctx_t *ctx, forto_statement_t *stat) { statement_ctx_t loop_ctx = {2}; unsigned step_instr, instr; BSTR identifier; HRESULT hres; identifier = alloc_bstr_arg(ctx, stat->identifier); if(!identifier) return E_OUTOFMEMORY; hres = compile_expression(ctx, stat->from_expr); if(FAILED(hres)) return hres; instr = push_instr(ctx, OP_assign_ident); if(!instr) return E_OUTOFMEMORY; instr_ptr(ctx, instr)->arg1.bstr = identifier; instr_ptr(ctx, instr)->arg2.uint = 0; hres = compile_expression(ctx, stat->to_expr); if(FAILED(hres)) return hres; if(!push_instr(ctx, OP_val)) return E_OUTOFMEMORY; if(stat->step_expr) { hres = compile_expression(ctx, stat->step_expr); if(FAILED(hres)) return hres; if(!push_instr(ctx, OP_val)) return E_OUTOFMEMORY; }else { hres = push_instr_int(ctx, OP_short, 1); if(FAILED(hres)) return hres; } loop_ctx.for_end_label = alloc_label(ctx); if(!loop_ctx.for_end_label) return E_OUTOFMEMORY; step_instr = push_instr(ctx, OP_step); if(!step_instr) return E_OUTOFMEMORY; instr_ptr(ctx, step_instr)->arg2.bstr = identifier; instr_ptr(ctx, step_instr)->arg1.uint = loop_ctx.for_end_label; hres = compile_statement(ctx, &loop_ctx, stat->body); if(FAILED(hres)) return hres; instr = push_instr(ctx, OP_incc); if(!instr) return E_OUTOFMEMORY; instr_ptr(ctx, instr)->arg1.bstr = identifier; hres = push_instr_addr(ctx, OP_jmp, step_instr); if(FAILED(hres)) return hres; hres = push_instr_uint(ctx, OP_pop, 2); if(FAILED(hres)) return hres; label_set_addr(ctx, loop_ctx.for_end_label); return S_OK; }