compile.c 50.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright 2011 Jacek Caban for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <assert.h>

#include "vbscript.h"
#include "parse.h"
#include "parser.tab.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
28
WINE_DECLARE_DEBUG_CHANNEL(vbscript_disas);
29

30 31 32 33 34 35 36 37 38
typedef struct _statement_ctx_t {
    unsigned stack_use;

    unsigned while_end_label;
    unsigned for_end_label;

    struct _statement_ctx_t *next;
} statement_ctx_t;

39 40 41 42 43 44
typedef struct {
    parser_ctx_t parser;

    unsigned instr_cnt;
    unsigned instr_size;
    vbscode_t *code;
45

46 47
    statement_ctx_t *stat_ctx;

48 49 50 51
    unsigned *labels;
    unsigned labels_size;
    unsigned labels_cnt;

52
    unsigned sub_end_label;
53
    unsigned func_end_label;
54
    unsigned prop_end_label;
55

56
    dim_decl_t *dim_decls;
57
    dim_decl_t *dim_decls_tail;
58
    dynamic_var_t *global_vars;
59

60 61 62
    const_decl_t *const_decls;
    const_decl_t *global_consts;

63 64 65
    function_t *func;
    function_t *funcs;
    function_decl_t *func_decls;
66 67

    class_desc_t *classes;
68 69
} compile_ctx_t;

70
static HRESULT compile_expression(compile_ctx_t*,expression_t*);
71
static HRESULT compile_statement(compile_ctx_t*,statement_ctx_t*,statement_t*);
72 73

static const struct {
74
    const char *op_str;
75 76 77
    instr_arg_type_t arg1_type;
    instr_arg_type_t arg2_type;
} instr_info[] = {
78
#define X(n,a,b,c) {#n,b,c},
79 80 81
OP_LIST
#undef X
};
82

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
{
    switch(type) {
    case ARG_STR:
    case ARG_BSTR:
        TRACE_(vbscript_disas)("\t%s", debugstr_w(arg->str));
        break;
    case ARG_INT:
        TRACE_(vbscript_disas)("\t%d", arg->uint);
        break;
    case ARG_UINT:
    case ARG_ADDR:
        TRACE_(vbscript_disas)("\t%u", arg->uint);
        break;
    case ARG_DOUBLE:
        TRACE_(vbscript_disas)("\t%lf", *arg->dbl);
        break;
    case ARG_NONE:
        break;
102
    DEFAULT_UNREACHABLE;
103 104 105 106 107 108 109
    }
}

static void dump_code(compile_ctx_t *ctx)
{
    instr_t *instr;

110 111
    for(instr = ctx->code->instrs+1; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
        assert(instr->op < OP_LAST);
112
        TRACE_(vbscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
113 114 115 116 117 118
        dump_instr_arg(instr_info[instr->op].arg1_type, &instr->arg1);
        dump_instr_arg(instr_info[instr->op].arg2_type, &instr->arg2);
        TRACE_(vbscript_disas)("\n");
    }
}

119 120
static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
{
121
    return heap_pool_alloc(&vbscode->heap, size);
122 123
}

124 125 126 127
static inline void *compiler_alloc_zero(vbscode_t *vbscode, size_t size)
{
    void *ret;

128
    ret = heap_pool_alloc(&vbscode->heap, size);
129 130 131 132 133
    if(ret)
        memset(ret, 0, size);
    return ret;
}

134 135 136 137 138 139 140 141 142 143 144 145
static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
{
    size_t size;
    WCHAR *ret;

    size = (strlenW(str)+1)*sizeof(WCHAR);
    ret = compiler_alloc(vbscode, size);
    if(ret)
        memcpy(ret, str, size);
    return ret;
}

146 147 148 149 150 151
static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
{
    assert(id < ctx->instr_cnt);
    return ctx->code->instrs + id;
}

152 153 154 155 156 157 158 159 160
static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
{
    assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);

    if(ctx->instr_size == ctx->instr_cnt) {
        instr_t *new_instr;

        new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
        if(!new_instr)
161
            return 0;
162 163 164 165 166 167 168 169 170

        ctx->code->instrs = new_instr;
        ctx->instr_size *= 2;
    }

    ctx->code->instrs[ctx->instr_cnt].op = op;
    return ctx->instr_cnt++;
}

171 172 173 174 175
static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
{
    unsigned ret;

    ret = push_instr(ctx, op);
176
    if(!ret)
177 178 179 180 181 182
        return E_OUTOFMEMORY;

    instr_ptr(ctx, ret)->arg1.lng = arg;
    return S_OK;
}

183 184 185 186 187
static HRESULT push_instr_uint(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
{
    unsigned ret;

    ret = push_instr(ctx, op);
188
    if(!ret)
189 190 191 192 193 194
        return E_OUTOFMEMORY;

    instr_ptr(ctx, ret)->arg1.uint = arg;
    return S_OK;
}

195 196 197 198 199
static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
{
    unsigned ret;

    ret = push_instr(ctx, op);
200
    if(!ret)
201 202 203 204 205 206
        return E_OUTOFMEMORY;

    instr_ptr(ctx, ret)->arg1.uint = arg;
    return S_OK;
}

207 208
static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
{
209 210
    unsigned instr;
    WCHAR *str;
211

212 213 214 215 216
    str = compiler_alloc_string(ctx->code, arg);
    if(!str)
        return E_OUTOFMEMORY;

    instr = push_instr(ctx, op);
217
    if(!instr)
218 219
        return E_OUTOFMEMORY;

220
    instr_ptr(ctx, instr)->arg1.str = str;
221 222 223
    return S_OK;
}

224 225 226 227 228 229 230 231 232 233
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);
234
    if(!instr)
235 236 237 238 239 240 241
        return E_OUTOFMEMORY;

    *d = arg;
    instr_ptr(ctx, instr)->arg1.dbl = d;
    return S_OK;
}

242 243 244 245 246 247 248 249
static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
{
    if(!ctx->code->bstr_pool_size) {
        ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
        if(!ctx->code->bstr_pool)
            return NULL;
        ctx->code->bstr_pool_size = 8;
    }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
250
        BSTR *new_pool;
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

        new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
        if(!new_pool)
            return NULL;

        ctx->code->bstr_pool = new_pool;
        ctx->code->bstr_pool_size *= 2;
    }

    ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
    if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
        return NULL;

    return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
}

267 268 269 270 271 272 273 274 275 276
static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
{
    unsigned instr;
    BSTR bstr;

    bstr = alloc_bstr_arg(ctx, arg);
    if(!bstr)
        return E_OUTOFMEMORY;

    instr = push_instr(ctx, op);
277
    if(!instr)
278 279 280 281 282 283
        return E_OUTOFMEMORY;

    instr_ptr(ctx, instr)->arg1.bstr = bstr;
    return S_OK;
}

284
static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
285
{
286
    unsigned instr;
287 288
    BSTR bstr;

289
    bstr = alloc_bstr_arg(ctx, arg1);
290 291
    if(!bstr)
        return E_OUTOFMEMORY;
292

293
    instr = push_instr(ctx, op);
294
    if(!instr)
295 296
        return E_OUTOFMEMORY;

297
    instr_ptr(ctx, instr)->arg1.bstr = bstr;
298
    instr_ptr(ctx, instr)->arg2.uint = arg2;
299 300 301
    return S_OK;
}

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
static HRESULT push_instr_uint_bstr(compile_ctx_t *ctx, vbsop_t op, unsigned arg1, const WCHAR *arg2)
{
    unsigned instr;
    BSTR bstr;

    bstr = alloc_bstr_arg(ctx, arg2);
    if(!bstr)
        return E_OUTOFMEMORY;

    instr = push_instr(ctx, op);
    if(!instr)
        return E_OUTOFMEMORY;

    instr_ptr(ctx, instr)->arg1.uint = arg1;
    instr_ptr(ctx, instr)->arg2.bstr = bstr;
    return S_OK;
}

320 321 322 323 324 325 326
#define LABEL_FLAG 0x80000000

static unsigned alloc_label(compile_ctx_t *ctx)
{
    if(!ctx->labels_size) {
        ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
        if(!ctx->labels)
327
            return 0;
328 329 330 331 332 333
        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)
334
            return 0;
335 336 337 338 339 340 341 342 343 344 345 346 347 348

        ctx->labels = new_labels;
        ctx->labels_size *= 2;
    }

    return ctx->labels_cnt++ | LABEL_FLAG;
}

static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
{
    assert(label & LABEL_FLAG);
    ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
}

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
static inline unsigned stack_offset(compile_ctx_t *ctx)
{
    statement_ctx_t *iter;
    unsigned ret = 0;

    for(iter = ctx->stat_ctx; iter; iter = iter->next)
        ret += iter->stack_use;

    return ret;
}

static BOOL emit_catch_jmp(compile_ctx_t *ctx, unsigned stack_off, unsigned code_off)
{
    unsigned code;

    code = push_instr(ctx, OP_catch);
    if(!code)
        return FALSE;

    instr_ptr(ctx, code)->arg1.uint = code_off;
    instr_ptr(ctx, code)->arg2.uint = stack_off + stack_offset(ctx);
    return TRUE;
}

static inline BOOL emit_catch(compile_ctx_t *ctx, unsigned off)
{
    return emit_catch_jmp(ctx, off, ctx->instr_cnt);
}

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
static expression_t *lookup_const_decls(compile_ctx_t *ctx, const WCHAR *name, BOOL lookup_global)
{
    const_decl_t *decl;

    for(decl = ctx->const_decls; decl; decl = decl->next) {
        if(!strcmpiW(decl->name, name))
            return decl->value_expr;
    }

    if(!lookup_global)
        return NULL;

    for(decl = ctx->global_consts; decl; decl = decl->next) {
        if(!strcmpiW(decl->name, name))
            return decl->value_expr;
    }

    return NULL;
}

398
static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
399
{
400
    unsigned arg_cnt = 0;
401 402
    HRESULT hres;

403 404 405 406 407 408 409
    while(args) {
        hres = compile_expression(ctx, args);
        if(FAILED(hres))
            return hres;

        arg_cnt++;
        args = args->next;
410 411
    }

412 413 414 415
    *ret = arg_cnt;
    return S_OK;
}

416
static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
417 418 419 420
{
    unsigned arg_cnt = 0;
    HRESULT hres;

421 422 423 424 425 426 427 428
    if(ret_val && !expr->args) {
        expression_t *const_expr;

        const_expr = lookup_const_decls(ctx, expr->identifier, TRUE);
        if(const_expr)
            return compile_expression(ctx, const_expr);
    }

429 430 431 432
    hres = compile_args(ctx, expr->args, &arg_cnt);
    if(FAILED(hres))
        return hres;

433
    if(expr->obj_expr) {
434 435 436 437 438
        hres = compile_expression(ctx, expr->obj_expr);
        if(FAILED(hres))
            return hres;

        hres = push_instr_bstr_uint(ctx, ret_val ? OP_mcall : OP_mcallv, expr->identifier, arg_cnt);
439
    }else {
440
        hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
441 442 443 444 445
    }

    return hres;
}

446 447 448 449 450 451 452 453
static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
{
    HRESULT hres;

    hres = compile_expression(ctx, expr->subexpr);
    if(FAILED(hres))
        return hres;

454
    return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
455 456
}

457 458 459 460 461 462 463 464 465 466 467 468
static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
{
    HRESULT hres;

    hres = compile_expression(ctx, expr->left);
    if(FAILED(hres))
        return hres;

    hres = compile_expression(ctx, expr->right);
    if(FAILED(hres))
        return hres;

469
    return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
470 471
}

472 473 474
static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
{
    switch(expr->type) {
475 476
    case EXPR_ADD:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
477 478
    case EXPR_AND:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
479 480
    case EXPR_BOOL:
        return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
481 482
    case EXPR_BRACKETS:
        return compile_expression(ctx, ((unary_expression_t*)expr)->subexpr);
483 484
    case EXPR_CONCAT:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
485 486
    case EXPR_DIV:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
487 488
    case EXPR_DOUBLE:
        return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
489
    case EXPR_EMPTY:
490
        return push_instr(ctx, OP_empty) ? S_OK : E_OUTOFMEMORY;
491 492
    case EXPR_EQUAL:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
493 494
    case EXPR_EQV:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eqv);
495 496
    case EXPR_EXP:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
497 498 499 500
    case EXPR_GT:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
    case EXPR_GTEQ:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
501 502
    case EXPR_IDIV:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
503 504
    case EXPR_IS:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_is);
505 506
    case EXPR_IMP:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_imp);
507 508 509 510
    case EXPR_LT:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
    case EXPR_LTEQ:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
511
    case EXPR_ME:
512
        return push_instr(ctx, OP_me) ? S_OK : E_OUTOFMEMORY;
513 514
    case EXPR_MEMBER:
        return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
515 516
    case EXPR_MOD:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
517 518
    case EXPR_MUL:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
519 520
    case EXPR_NEG:
        return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
521 522
    case EXPR_NEQUAL:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
523 524
    case EXPR_NEW:
        return push_instr_str(ctx, OP_new, ((string_expression_t*)expr)->value);
525 526
    case EXPR_NOARG:
        return push_instr_int(ctx, OP_hres, DISP_E_PARAMNOTFOUND);
527 528
    case EXPR_NOT:
        return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
529
    case EXPR_NOTHING:
530
        return push_instr(ctx, OP_nothing) ? S_OK : E_OUTOFMEMORY;
531
    case EXPR_NULL:
532
        return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
533 534
    case EXPR_OR:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
535 536
    case EXPR_STRING:
        return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
537 538
    case EXPR_SUB:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
539 540 541 542
    case EXPR_USHORT:
        return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
    case EXPR_ULONG:
        return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
543 544
    case EXPR_XOR:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
545 546 547 548 549 550 551 552
    default:
        FIXME("Unimplemented expression type %d\n", expr->type);
        return E_NOTIMPL;
    }

    return S_OK;
}

553 554
static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
{
555
    unsigned cnd_jmp, endif_label = 0;
556 557 558 559 560 561 562 563
    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);
564
    if(!cnd_jmp)
565 566
        return E_OUTOFMEMORY;

567 568 569
    if(!emit_catch(ctx, 0))
        return E_OUTOFMEMORY;

570
    hres = compile_statement(ctx, NULL, stat->if_stat);
571 572 573 574 575
    if(FAILED(hres))
        return hres;

    if(stat->else_stat || stat->elseifs) {
        endif_label = alloc_label(ctx);
576
        if(!endif_label)
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
            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);
592
        if(!cnd_jmp)
593 594
            return E_OUTOFMEMORY;

595 596 597
        if(!emit_catch(ctx, 0))
            return E_OUTOFMEMORY;

598
        hres = compile_statement(ctx, NULL, elseif_decl->stat);
599 600 601 602 603 604 605 606 607 608 609
        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) {
610
        hres = compile_statement(ctx, NULL, stat->else_stat);
611 612 613 614
        if(FAILED(hres))
            return hres;
    }

615
    if(endif_label)
616 617 618 619
        label_set_addr(ctx, endif_label);
    return S_OK;
}

620 621
static HRESULT compile_while_statement(compile_ctx_t *ctx, while_statement_t *stat)
{
622 623
    statement_ctx_t stat_ctx = {0}, *loop_ctx;
    unsigned start_addr;
624 625 626 627 628 629 630 631 632
    unsigned jmp_end;
    HRESULT hres;

    start_addr = ctx->instr_cnt;

    hres = compile_expression(ctx, stat->expr);
    if(FAILED(hres))
        return hres;

633
    jmp_end = push_instr(ctx, stat->stat.type == STAT_UNTIL ? OP_jmp_true : OP_jmp_false);
634
    if(!jmp_end)
635 636
        return E_OUTOFMEMORY;

637 638 639
    if(!emit_catch(ctx, 0))
        return E_OUTOFMEMORY;

640 641 642 643 644 645 646
    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;
    }
647

648
    hres = compile_statement(ctx, loop_ctx, stat->body);
649 650 651 652 653 654 655 656
    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;
657

658 659
    if(loop_ctx)
        label_set_addr(ctx, stat_ctx.while_end_label);
660

661 662 663
    return S_OK;
}

664 665
static HRESULT compile_dowhile_statement(compile_ctx_t *ctx, while_statement_t *stat)
{
666 667
    statement_ctx_t loop_ctx = {0};
    unsigned start_addr;
668
    vbsop_t jmp_op;
669 670 671 672
    HRESULT hres;

    start_addr = ctx->instr_cnt;

673
    if(!(loop_ctx.while_end_label = alloc_label(ctx)))
674 675
        return E_OUTOFMEMORY;

676
    hres = compile_statement(ctx, &loop_ctx, stat->body);
677 678 679
    if(FAILED(hres))
        return hres;

680 681 682 683 684 685 686 687 688
    if(stat->expr) {
        hres = compile_expression(ctx, stat->expr);
        if(FAILED(hres))
            return hres;

        jmp_op = stat->stat.type == STAT_DOUNTIL ? OP_jmp_false : OP_jmp_true;
    }else {
        jmp_op = OP_jmp;
    }
689

690
    hres = push_instr_addr(ctx, jmp_op, start_addr);
691 692 693
    if(FAILED(hres))
        return hres;

694
    label_set_addr(ctx, loop_ctx.while_end_label);
695 696 697 698

    if(!emit_catch(ctx, 0))
        return E_OUTOFMEMORY;

699 700 701
    return S_OK;
}

702 703
static HRESULT compile_foreach_statement(compile_ctx_t *ctx, foreach_statement_t *stat)
{
704 705
    statement_ctx_t loop_ctx = {1};
    unsigned loop_start;
706 707
    HRESULT hres;

708 709 710 711
    /* Preserve a place on the stack in case we throw before having proper enum collection. */
    if(!push_instr(ctx, OP_empty))
        return E_OUTOFMEMORY;

712 713 714 715 716 717 718
    hres = compile_expression(ctx, stat->group_expr);
    if(FAILED(hres))
        return hres;

    if(!push_instr(ctx, OP_newenum))
        return E_OUTOFMEMORY;

719
    if(!(loop_ctx.for_end_label = alloc_label(ctx)))
720 721
        return E_OUTOFMEMORY;

722
    hres = push_instr_uint_bstr(ctx, OP_enumnext, loop_ctx.for_end_label, stat->identifier);
723 724 725
    if(FAILED(hres))
        return hres;

726 727 728 729
    if(!emit_catch(ctx, 1))
        return E_OUTOFMEMORY;

    loop_start = ctx->instr_cnt;
730
    hres = compile_statement(ctx, &loop_ctx, stat->body);
731 732 733
    if(FAILED(hres))
        return hres;

734 735 736 737 738
    /* We need a separated enumnext here, because we need to jump out of the loop on exception. */
    hres = push_instr_uint_bstr(ctx, OP_enumnext, loop_ctx.for_end_label, stat->identifier);
    if(FAILED(hres))
        return hres;

739 740 741 742
    hres = push_instr_addr(ctx, OP_jmp, loop_start);
    if(FAILED(hres))
        return hres;

743
    label_set_addr(ctx, loop_ctx.for_end_label);
744
    return S_OK;
745 746
}

747 748
static HRESULT compile_forto_statement(compile_ctx_t *ctx, forto_statement_t *stat)
{
749
    statement_ctx_t loop_ctx = {2};
750
    unsigned step_instr, instr;
751 752 753 754 755 756 757 758 759 760 761
    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;

762
    /* FIXME: Assign should happen after both expressions evaluation. */
763
    instr = push_instr(ctx, OP_assign_ident);
764
    if(!instr)
765 766
        return E_OUTOFMEMORY;
    instr_ptr(ctx, instr)->arg1.bstr = identifier;
767
    instr_ptr(ctx, instr)->arg2.uint = 0;
768 769 770 771 772

    hres = compile_expression(ctx, stat->to_expr);
    if(FAILED(hres))
        return hres;

773
    if(!push_instr(ctx, OP_val))
774 775 776 777 778 779 780
        return E_OUTOFMEMORY;

    if(stat->step_expr) {
        hres = compile_expression(ctx, stat->step_expr);
        if(FAILED(hres))
            return hres;

781
        if(!push_instr(ctx, OP_val))
782 783 784 785 786 787 788
            return E_OUTOFMEMORY;
    }else {
        hres = push_instr_int(ctx, OP_short, 1);
        if(FAILED(hres))
            return hres;
    }

789 790
    loop_ctx.for_end_label = alloc_label(ctx);
    if(!loop_ctx.for_end_label)
791 792
        return E_OUTOFMEMORY;

793
    step_instr = push_instr(ctx, OP_step);
794
    if(!step_instr)
795 796
        return E_OUTOFMEMORY;
    instr_ptr(ctx, step_instr)->arg2.bstr = identifier;
797
    instr_ptr(ctx, step_instr)->arg1.uint = loop_ctx.for_end_label;
798

799 800 801
    if(!emit_catch(ctx, 2))
        return E_OUTOFMEMORY;

802
    hres = compile_statement(ctx, &loop_ctx, stat->body);
803 804 805
    if(FAILED(hres))
        return hres;

806
    /* FIXME: Error handling can't be done compatible with native using OP_incc here. */
807
    instr = push_instr(ctx, OP_incc);
808
    if(!instr)
809 810 811 812 813 814 815
        return E_OUTOFMEMORY;
    instr_ptr(ctx, instr)->arg1.bstr = identifier;

    hres = push_instr_addr(ctx, OP_jmp, step_instr);
    if(FAILED(hres))
        return hres;

816 817 818
    hres = push_instr_uint(ctx, OP_pop, 2);
    if(FAILED(hres))
        return hres;
819

820
    label_set_addr(ctx, loop_ctx.for_end_label);
821 822 823 824 825

    /* FIXME: reconsider after OP_incc fixup. */
    if(!emit_catch(ctx, 0))
        return E_OUTOFMEMORY;

826
    return S_OK;
827 828
}

829 830
static HRESULT compile_select_statement(compile_ctx_t *ctx, select_statement_t *stat)
{
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
    unsigned end_label, case_cnt = 0, *case_labels = NULL, i;
    case_clausule_t *case_iter;
    expression_t *expr_iter;
    HRESULT hres;

    hres = compile_expression(ctx, stat->expr);
    if(FAILED(hres))
        return hres;

    if(!push_instr(ctx, OP_val))
        return E_OUTOFMEMORY;

    end_label = alloc_label(ctx);
    if(!end_label)
        return E_OUTOFMEMORY;

847 848 849
    if(!emit_catch_jmp(ctx, 0, end_label))
        return E_OUTOFMEMORY;

850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
    for(case_iter = stat->case_clausules; case_iter; case_iter = case_iter->next)
        case_cnt++;

    if(case_cnt) {
        case_labels = heap_alloc(case_cnt*sizeof(*case_labels));
        if(!case_labels)
            return E_OUTOFMEMORY;
    }

    for(case_iter = stat->case_clausules, i=0; case_iter; case_iter = case_iter->next, i++) {
        case_labels[i] = alloc_label(ctx);
        if(!case_labels[i]) {
            hres = E_OUTOFMEMORY;
            break;
        }

        if(!case_iter->expr)
            break;

        for(expr_iter = case_iter->expr; expr_iter; expr_iter = expr_iter->next) {
            hres = compile_expression(ctx, expr_iter);
            if(FAILED(hres))
                break;

            hres = push_instr_addr(ctx, OP_case, case_labels[i]);
            if(FAILED(hres))
                break;
877

Jacek Caban's avatar
Jacek Caban committed
878 879 880 881
            if(!emit_catch_jmp(ctx, 0, case_labels[i])) {
                hres = E_OUTOFMEMORY;
                break;
            }
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
        }
    }

    if(FAILED(hres)) {
        heap_free(case_labels);
        return hres;
    }

    hres = push_instr_uint(ctx, OP_pop, 1);
    if(FAILED(hres)) {
        heap_free(case_labels);
        return hres;
    }

    hres = push_instr_addr(ctx, OP_jmp, case_iter ? case_labels[i] : end_label);
    if(FAILED(hres)) {
        heap_free(case_labels);
        return hres;
    }

    for(case_iter = stat->case_clausules, i=0; case_iter; case_iter = case_iter->next, i++) {
        label_set_addr(ctx, case_labels[i]);
        hres = compile_statement(ctx, NULL, case_iter->stat);
        if(FAILED(hres))
            break;

        if(!case_iter->next)
            break;

        hres = push_instr_addr(ctx, OP_jmp, end_label);
        if(FAILED(hres))
913
            break;
914 915 916 917 918 919 920 921
    }

    heap_free(case_labels);
    if(FAILED(hres))
        return hres;

    label_set_addr(ctx, end_label);
    return S_OK;
922 923
}

924
static HRESULT compile_assignment(compile_ctx_t *ctx, member_expression_t *member_expr, expression_t *value_expr, BOOL is_set)
925
{
926 927
    unsigned args_cnt;
    vbsop_t op;
928 929
    HRESULT hres;

930 931
    if(member_expr->obj_expr) {
        hres = compile_expression(ctx, member_expr->obj_expr);
932 933 934
        if(FAILED(hres))
            return hres;

935
        op = is_set ? OP_set_member : OP_assign_member;
936
    }else {
937
        op = is_set ? OP_set_ident : OP_assign_ident;
938 939
    }

940
    hres = compile_expression(ctx, value_expr);
941 942 943
    if(FAILED(hres))
        return hres;

944
    hres = compile_args(ctx, member_expr->args, &args_cnt);
945 946 947
    if(FAILED(hres))
        return hres;

948 949 950 951 952 953 954 955
    hres = push_instr_bstr_uint(ctx, op, member_expr->identifier, args_cnt);
    if(FAILED(hres))
        return hres;

    if(!emit_catch(ctx, 0))
        return E_OUTOFMEMORY;

    return S_OK;
956 957 958 959 960 961 962 963 964
}

static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat, BOOL is_set)
{
    return compile_assignment(ctx, stat->member_expr, stat->value_expr, is_set);
}

static HRESULT compile_call_statement(compile_ctx_t *ctx, call_statement_t *stat)
{
965 966
    HRESULT hres;

967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
    /* It's challenging for parser to distinguish parameterized assignment with one argument from call
     * with equality expression argument, so we do it in compiler. */
    if(!stat->is_strict && stat->expr->args && !stat->expr->args->next && stat->expr->args->type == EXPR_EQUAL) {
        binary_expression_t *eqexpr = (binary_expression_t*)stat->expr->args;

        if(eqexpr->left->type == EXPR_BRACKETS) {
            member_expression_t new_member = *stat->expr;

            WARN("converting call expr to assign expr\n");

            new_member.args = ((unary_expression_t*)eqexpr->left)->subexpr;
            return compile_assignment(ctx, &new_member, eqexpr->right, FALSE);
        }
    }

982 983 984 985 986 987 988 989
    hres = compile_member_expression(ctx, stat->expr, FALSE);
    if(FAILED(hres))
        return hres;

    if(!emit_catch(ctx, 0))
        return E_OUTOFMEMORY;

    return S_OK;
990 991
}

992 993 994 995 996 997 998 999 1000 1001 1002 1003
static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
{
    dim_decl_t *dim_decl;

    for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
        if(!strcmpiW(dim_decl->name, name))
            return TRUE;
    }

    return FALSE;
}

1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
static BOOL lookup_args_name(compile_ctx_t *ctx, const WCHAR *name)
{
    unsigned i;

    for(i = 0; i < ctx->func->arg_cnt; i++) {
        if(!strcmpiW(ctx->func->args[i].name, name))
            return TRUE;
    }

    return FALSE;
}

1016 1017 1018 1019 1020
static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
{
    dim_decl_t *dim_decl = stat->dim_decls;

    while(1) {
1021 1022
        if(lookup_dim_decls(ctx, dim_decl->name) || lookup_args_name(ctx, dim_decl->name)
           || lookup_const_decls(ctx, dim_decl->name, FALSE)) {
1023 1024 1025 1026
            FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
            return E_FAIL;
        }

1027
        ctx->func->var_cnt++;
1028 1029

        if(dim_decl->is_array) {
1030 1031 1032
            HRESULT hres = push_instr_bstr_uint(ctx, OP_dim, dim_decl->name, ctx->func->array_cnt++);
            if(FAILED(hres))
                return hres;
1033 1034 1035

            if(!emit_catch(ctx, 0))
                return E_OUTOFMEMORY;
1036 1037
        }

1038 1039 1040 1041 1042
        if(!dim_decl->next)
            break;
        dim_decl = dim_decl->next;
    }

1043 1044 1045 1046 1047
    if(ctx->dim_decls_tail)
        ctx->dim_decls_tail->next = stat->dim_decls;
    else
        ctx->dim_decls = stat->dim_decls;
    ctx->dim_decls_tail = dim_decl;
1048 1049 1050
    return S_OK;
}

1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
static HRESULT compile_const_statement(compile_ctx_t *ctx, const_statement_t *stat)
{
    const_decl_t *decl, *next_decl = stat->decls;

    do {
        decl = next_decl;

        if(lookup_const_decls(ctx, decl->name, FALSE) || lookup_args_name(ctx, decl->name)
                || lookup_dim_decls(ctx, decl->name)) {
            FIXME("%s redefined\n", debugstr_w(decl->name));
            return E_FAIL;
        }

        if(ctx->func->type == FUNC_GLOBAL) {
            HRESULT hres;

            hres = compile_expression(ctx, decl->value_expr);
            if(FAILED(hres))
                return hres;

            hres = push_instr_bstr(ctx, OP_const, decl->name);
            if(FAILED(hres))
                return hres;
1074 1075 1076

            if(!emit_catch(ctx, 0))
                return E_OUTOFMEMORY;
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
        }

        next_decl = decl->next;
        decl->next = ctx->const_decls;
        ctx->const_decls = decl;
    } while(next_decl);

    return S_OK;
}

1087 1088
static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
{
1089
    if(ctx->func != &ctx->code->main_code) {
1090 1091 1092 1093 1094 1095 1096 1097 1098
        FIXME("Function is not in the global code\n");
        return E_FAIL;
    }

    stat->func_decl->next = ctx->func_decls;
    ctx->func_decls = stat->func_decl;
    return S_OK;
}

1099 1100
static HRESULT compile_exitdo_statement(compile_ctx_t *ctx)
{
1101 1102 1103 1104 1105 1106 1107 1108 1109
    statement_ctx_t *iter;
    unsigned pop_cnt = 0;

    for(iter = ctx->stat_ctx; iter; iter = iter->next) {
        pop_cnt += iter->stack_use;
        if(iter->while_end_label)
            break;
    }
    if(!iter) {
1110 1111 1112 1113
        FIXME("Exit Do outside Do Loop\n");
        return E_FAIL;
    }

1114 1115 1116 1117 1118 1119 1120 1121 1122
    if(pop_cnt) {
        HRESULT hres;

        hres = push_instr_uint(ctx, OP_pop, pop_cnt);
        if(FAILED(hres))
            return hres;
    }

    return push_instr_addr(ctx, OP_jmp, iter->while_end_label);
1123 1124
}

1125 1126
static HRESULT compile_exitfor_statement(compile_ctx_t *ctx)
{
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
    statement_ctx_t *iter;
    unsigned pop_cnt = 0;

    for(iter = ctx->stat_ctx; iter; iter = iter->next) {
        pop_cnt += iter->stack_use;
        if(iter->for_end_label)
            break;
    }
    if(!iter) {
        FIXME("Exit For outside For loop\n");
1137 1138 1139
        return E_FAIL;
    }

1140 1141 1142 1143 1144 1145 1146 1147 1148
    if(pop_cnt) {
        HRESULT hres;

        hres = push_instr_uint(ctx, OP_pop, pop_cnt);
        if(FAILED(hres))
            return hres;
    }

    return push_instr_addr(ctx, OP_jmp, iter->for_end_label);
1149 1150
}

1151 1152
static HRESULT exit_label(compile_ctx_t *ctx, unsigned jmp_label)
{
1153
    unsigned pop_cnt = stack_offset(ctx);
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165

    if(pop_cnt) {
        HRESULT hres;

        hres = push_instr_uint(ctx, OP_pop, pop_cnt);
        if(FAILED(hres))
            return hres;
    }

    return push_instr_addr(ctx, OP_jmp, jmp_label);
}

1166 1167
static HRESULT compile_exitsub_statement(compile_ctx_t *ctx)
{
1168
    if(!ctx->sub_end_label) {
1169 1170 1171 1172
        FIXME("Exit Sub outside Sub?\n");
        return E_FAIL;
    }

1173
    return exit_label(ctx, ctx->sub_end_label);
1174 1175
}

1176 1177
static HRESULT compile_exitfunc_statement(compile_ctx_t *ctx)
{
1178
    if(!ctx->func_end_label) {
1179 1180 1181 1182
        FIXME("Exit Function outside Function?\n");
        return E_FAIL;
    }

1183
    return exit_label(ctx, ctx->func_end_label);
1184 1185
}

1186 1187
static HRESULT compile_exitprop_statement(compile_ctx_t *ctx)
{
1188
    if(!ctx->prop_end_label) {
1189 1190 1191 1192
        FIXME("Exit Property outside Property?\n");
        return E_FAIL;
    }

1193
    return exit_label(ctx, ctx->prop_end_label);
1194 1195
}

1196 1197 1198 1199 1200
static HRESULT compile_onerror_statement(compile_ctx_t *ctx, onerror_statement_t *stat)
{
    return push_instr_int(ctx, OP_errmode, stat->resume_next);
}

1201
static HRESULT compile_statement(compile_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1202 1203 1204
{
    HRESULT hres;

1205 1206 1207 1208 1209
    if(stat_ctx) {
        stat_ctx->next = ctx->stat_ctx;
        ctx->stat_ctx = stat_ctx;
    }

1210 1211
    while(stat) {
        switch(stat->type) {
1212
        case STAT_ASSIGN:
1213
            hres = compile_assign_statement(ctx, (assign_statement_t*)stat, FALSE);
1214
            break;
1215
        case STAT_CALL:
1216
            hres = compile_call_statement(ctx, (call_statement_t*)stat);
1217
            break;
1218 1219 1220
        case STAT_CONST:
            hres = compile_const_statement(ctx, (const_statement_t*)stat);
            break;
1221 1222 1223
        case STAT_DIM:
            hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
            break;
1224 1225 1226 1227
        case STAT_DOWHILE:
        case STAT_DOUNTIL:
            hres = compile_dowhile_statement(ctx, (while_statement_t*)stat);
            break;
1228 1229 1230
        case STAT_EXITDO:
            hres = compile_exitdo_statement(ctx);
            break;
1231 1232 1233
        case STAT_EXITFOR:
            hres = compile_exitfor_statement(ctx);
            break;
1234 1235 1236
        case STAT_EXITFUNC:
            hres = compile_exitfunc_statement(ctx);
            break;
1237 1238 1239
        case STAT_EXITPROP:
            hres = compile_exitprop_statement(ctx);
            break;
1240 1241 1242
        case STAT_EXITSUB:
            hres = compile_exitsub_statement(ctx);
            break;
1243 1244 1245
        case STAT_FOREACH:
            hres = compile_foreach_statement(ctx, (foreach_statement_t*)stat);
            break;
1246 1247 1248
        case STAT_FORTO:
            hres = compile_forto_statement(ctx, (forto_statement_t*)stat);
            break;
1249 1250 1251
        case STAT_FUNC:
            hres = compile_function_statement(ctx, (function_statement_t*)stat);
            break;
1252 1253 1254
        case STAT_IF:
            hres = compile_if_statement(ctx, (if_statement_t*)stat);
            break;
1255 1256 1257
        case STAT_ONERROR:
            hres = compile_onerror_statement(ctx, (onerror_statement_t*)stat);
            break;
1258 1259 1260
        case STAT_SELECT:
            hres = compile_select_statement(ctx, (select_statement_t*)stat);
            break;
1261 1262 1263
        case STAT_SET:
            hres = compile_assign_statement(ctx, (assign_statement_t*)stat, TRUE);
            break;
1264
        case STAT_STOP:
1265
            hres = push_instr(ctx, OP_stop) ? S_OK : E_OUTOFMEMORY;
1266
            break;
1267
        case STAT_UNTIL:
1268
        case STAT_WHILE:
1269
        case STAT_WHILELOOP:
1270 1271
            hres = compile_while_statement(ctx, (while_statement_t*)stat);
            break;
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
        default:
            FIXME("Unimplemented statement type %d\n", stat->type);
            hres = E_NOTIMPL;
        }

        if(FAILED(hres))
            return hres;
        stat = stat->next;
    }

1282 1283 1284 1285 1286
    if(stat_ctx) {
        assert(ctx->stat_ctx == stat_ctx);
        ctx->stat_ctx = stat_ctx->next;
    }

1287 1288 1289
    return S_OK;
}

1290
static void resolve_labels(compile_ctx_t *ctx, unsigned off)
1291 1292 1293
{
    instr_t *instr;

1294
    for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
        if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
            assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
            instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
        }
        assert(instr_info[instr->op].arg2_type != ARG_ADDR);
    }

    ctx->labels_cnt = 0;
}

1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
static HRESULT fill_array_desc(compile_ctx_t *ctx, dim_decl_t *dim_decl, array_desc_t *array_desc)
{
    unsigned dim_cnt = 0, i;
    dim_list_t *iter;

    for(iter = dim_decl->dims; iter; iter = iter->next)
        dim_cnt++;

    array_desc->bounds = compiler_alloc(ctx->code, dim_cnt * sizeof(SAFEARRAYBOUND));
    if(!array_desc->bounds)
        return E_OUTOFMEMORY;

    array_desc->dim_cnt = dim_cnt;

    for(iter = dim_decl->dims, i=0; iter; iter = iter->next, i++) {
        array_desc->bounds[i].cElements = iter->val+1;
        array_desc->bounds[i].lLbound = 0;
    }

    return S_OK;
}

1327 1328 1329 1330 1331 1332
static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
{
    HRESULT hres;

    func->code_off = ctx->instr_cnt;

1333 1334 1335
    ctx->sub_end_label = 0;
    ctx->func_end_label = 0;
    ctx->prop_end_label = 0;
1336 1337

    switch(func->type) {
1338 1339
    case FUNC_FUNCTION:
        ctx->func_end_label = alloc_label(ctx);
1340 1341
        if(!ctx->func_end_label)
            return E_OUTOFMEMORY;
1342
        break;
1343 1344
    case FUNC_SUB:
        ctx->sub_end_label = alloc_label(ctx);
1345
        if(!ctx->sub_end_label)
1346 1347
            return E_OUTOFMEMORY;
        break;
1348 1349 1350
    case FUNC_PROPGET:
    case FUNC_PROPLET:
    case FUNC_PROPSET:
1351
    case FUNC_DEFGET:
1352
        ctx->prop_end_label = alloc_label(ctx);
1353
        if(!ctx->prop_end_label)
1354 1355
            return E_OUTOFMEMORY;
        break;
1356 1357 1358 1359
    case FUNC_GLOBAL:
        break;
    }

1360
    ctx->func = func;
1361
    ctx->dim_decls = ctx->dim_decls_tail = NULL;
1362
    ctx->const_decls = NULL;
1363
    hres = compile_statement(ctx, NULL, stat);
1364
    ctx->func = NULL;
1365 1366 1367
    if(FAILED(hres))
        return hres;

1368
    if(ctx->sub_end_label)
1369
        label_set_addr(ctx, ctx->sub_end_label);
1370
    if(ctx->func_end_label)
1371
        label_set_addr(ctx, ctx->func_end_label);
1372
    if(ctx->prop_end_label)
1373
        label_set_addr(ctx, ctx->prop_end_label);
1374

1375
    if(!push_instr(ctx, OP_ret))
1376 1377
        return E_OUTOFMEMORY;

1378
    resolve_labels(ctx, func->code_off);
1379

1380
    if(func->var_cnt) {
1381 1382
        dim_decl_t *dim_decl;

1383 1384
        if(func->type == FUNC_GLOBAL) {
            dynamic_var_t *new_var;
1385

1386 1387
            func->var_cnt = 0;

1388 1389 1390 1391
            for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
                new_var = compiler_alloc(ctx->code, sizeof(*new_var));
                if(!new_var)
                    return E_OUTOFMEMORY;
1392

1393 1394 1395
                new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
                if(!new_var->name)
                    return E_OUTOFMEMORY;
1396

1397
                V_VT(&new_var->v) = VT_EMPTY;
1398
                new_var->is_const = FALSE;
1399 1400 1401 1402 1403

                new_var->next = ctx->global_vars;
                ctx->global_vars = new_var;
            }
        }else {
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
            unsigned i;

            func->vars = compiler_alloc(ctx->code, func->var_cnt * sizeof(var_desc_t));
            if(!func->vars)
                return E_OUTOFMEMORY;

            for(dim_decl = ctx->dim_decls, i=0; dim_decl; dim_decl = dim_decl->next, i++) {
                func->vars[i].name = compiler_alloc_string(ctx->code, dim_decl->name);
                if(!func->vars[i].name)
                    return E_OUTOFMEMORY;
            }

            assert(i == func->var_cnt);
1417 1418 1419
        }
    }

1420
    if(func->array_cnt) {
1421
        unsigned array_id = 0;
1422 1423 1424 1425 1426 1427 1428
        dim_decl_t *dim_decl;

        func->array_descs = compiler_alloc(ctx->code, func->array_cnt * sizeof(array_desc_t));
        if(!func->array_descs)
            return E_OUTOFMEMORY;

        for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
1429 1430 1431 1432
            if(dim_decl->is_array) {
                hres = fill_array_desc(ctx, dim_decl, func->array_descs + array_id++);
                if(FAILED(hres))
                    return hres;
1433 1434 1435 1436 1437 1438
            }
        }

        assert(array_id == func->array_cnt);
    }

1439 1440 1441
    return S_OK;
}

1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
{
    function_t *iter;

    for(iter = ctx->funcs; iter; iter = iter->next) {
        if(!strcmpiW(iter->name, name))
            return TRUE;
    }

    return FALSE;
}

static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
{
    function_t *func;
    HRESULT hres;

1459
    if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name) || lookup_const_decls(ctx, decl->name, FALSE)) {
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
        FIXME("%s: redefinition\n", debugstr_w(decl->name));
        return E_FAIL;
    }

    func = compiler_alloc(ctx->code, sizeof(*func));
    if(!func)
        return E_OUTOFMEMORY;

    func->name = compiler_alloc_string(ctx->code, decl->name);
    if(!func->name)
        return E_OUTOFMEMORY;

1472 1473
    func->vars = NULL;
    func->var_cnt = 0;
1474
    func->array_cnt = 0;
1475 1476
    func->code_ctx = ctx->code;
    func->type = decl->type;
1477
    func->is_public = decl->is_public;
1478

1479
    func->arg_cnt = 0;
1480
    if(decl->args) {
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
        arg_decl_t *arg;
        unsigned i;

        for(arg = decl->args; arg; arg = arg->next)
            func->arg_cnt++;

        func->args = compiler_alloc(ctx->code, func->arg_cnt * sizeof(arg_desc_t));
        if(!func->args)
            return E_OUTOFMEMORY;

        for(i = 0, arg = decl->args; arg; arg = arg->next, i++) {
            func->args[i].name = compiler_alloc_string(ctx->code, arg->name);
            if(!func->args[i].name)
                return E_OUTOFMEMORY;
            func->args[i].by_ref = arg->by_ref;
        }
    }else {
        func->args = NULL;
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
    }

    hres = compile_func(ctx, decl->body, func);
    if(FAILED(hres))
        return hres;

    *ret = func;
    return S_OK;
}

1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
static BOOL lookup_class_name(compile_ctx_t *ctx, const WCHAR *name)
{
    class_desc_t *iter;

    for(iter = ctx->classes; iter; iter = iter->next) {
        if(!strcmpiW(iter->name, name))
            return TRUE;
    }

    return FALSE;
}

1521 1522
static HRESULT create_class_funcprop(compile_ctx_t *ctx, function_decl_t *func_decl, vbdisp_funcprop_desc_t *desc)
{
1523 1524 1525 1526
    vbdisp_invoke_type_t invoke_type;
    function_decl_t *funcprop_decl;
    HRESULT hres;

1527 1528 1529 1530
    desc->name = compiler_alloc_string(ctx->code, func_decl->name);
    if(!desc->name)
        return E_OUTOFMEMORY;

1531 1532 1533 1534 1535
    for(funcprop_decl = func_decl; funcprop_decl; funcprop_decl = funcprop_decl->next_prop_func) {
        switch(funcprop_decl->type) {
        case FUNC_FUNCTION:
        case FUNC_SUB:
        case FUNC_PROPGET:
1536
        case FUNC_DEFGET:
1537 1538 1539 1540 1541 1542 1543 1544
            invoke_type = VBDISP_CALLGET;
            break;
        case FUNC_PROPLET:
            invoke_type = VBDISP_LET;
            break;
        case FUNC_PROPSET:
            invoke_type = VBDISP_SET;
            break;
1545
        DEFAULT_UNREACHABLE;
1546
        }
1547

1548
        assert(!desc->entries[invoke_type]);
1549

1550 1551 1552 1553 1554 1555 1556 1557 1558
        if(funcprop_decl->is_public)
            desc->is_public = TRUE;

        hres = create_function(ctx, funcprop_decl, desc->entries+invoke_type);
        if(FAILED(hres))
            return hres;
    }

    return S_OK;
1559 1560
}

1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
static BOOL lookup_class_funcs(class_desc_t *class_desc, const WCHAR *name)
{
    unsigned i;

    for(i=0; i < class_desc->func_cnt; i++) {
        if(class_desc->funcs[i].name && !strcmpiW(class_desc->funcs[i].name, name))
            return TRUE;
    }

    return FALSE;
}

1573 1574
static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl)
{
1575
    function_decl_t *func_decl, *func_prop_decl;
1576
    class_desc_t *class_desc;
1577
    dim_decl_t *prop_decl;
1578 1579
    unsigned i;
    HRESULT hres;
1580

1581
    static const WCHAR class_initializeW[] = {'c','l','a','s','s','_','i','n','i','t','i','a','l','i','z','e',0};
1582
    static const WCHAR class_terminateW[] = {'c','l','a','s','s','_','t','e','r','m','i','n','a','t','e',0};
1583

1584
    if(lookup_dim_decls(ctx, class_decl->name) || lookup_funcs_name(ctx, class_decl->name)
1585
            || lookup_const_decls(ctx, class_decl->name, FALSE) || lookup_class_name(ctx, class_decl->name)) {
1586 1587 1588 1589
        FIXME("%s: redefinition\n", debugstr_w(class_decl->name));
        return E_FAIL;
    }

1590
    class_desc = compiler_alloc_zero(ctx->code, sizeof(*class_desc));
1591 1592 1593 1594 1595 1596 1597
    if(!class_desc)
        return E_OUTOFMEMORY;

    class_desc->name = compiler_alloc_string(ctx->code, class_decl->name);
    if(!class_desc->name)
        return E_OUTOFMEMORY;

1598 1599
    class_desc->func_cnt = 1; /* always allocate slot for default getter */

1600 1601 1602 1603 1604 1605 1606 1607
    for(func_decl = class_decl->funcs; func_decl; func_decl = func_decl->next) {
        for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
            if(func_prop_decl->type == FUNC_DEFGET)
                break;
        }
        if(!func_prop_decl)
            class_desc->func_cnt++;
    }
1608 1609 1610 1611 1612 1613 1614

    class_desc->funcs = compiler_alloc(ctx->code, class_desc->func_cnt*sizeof(*class_desc->funcs));
    if(!class_desc->funcs)
        return E_OUTOFMEMORY;
    memset(class_desc->funcs, 0, class_desc->func_cnt*sizeof(*class_desc->funcs));

    for(func_decl = class_decl->funcs, i=1; func_decl; func_decl = func_decl->next, i++) {
1615 1616 1617 1618 1619 1620 1621
        for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
            if(func_prop_decl->type == FUNC_DEFGET) {
                i--;
                break;
            }
        }

1622 1623 1624 1625 1626 1627 1628
        if(!strcmpiW(class_initializeW, func_decl->name)) {
            if(func_decl->type != FUNC_SUB) {
                FIXME("class initializer is not sub\n");
                return E_FAIL;
            }

            class_desc->class_initialize_id = i;
1629 1630 1631 1632 1633 1634 1635
        }else  if(!strcmpiW(class_terminateW, func_decl->name)) {
            if(func_decl->type != FUNC_SUB) {
                FIXME("class terminator is not sub\n");
                return E_FAIL;
            }

            class_desc->class_terminate_id = i;
1636 1637
        }

1638
        hres = create_class_funcprop(ctx, func_decl, class_desc->funcs + (func_prop_decl ? 0 : i));
1639 1640 1641 1642
        if(FAILED(hres))
            return hres;
    }

1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
    for(prop_decl = class_decl->props; prop_decl; prop_decl = prop_decl->next)
        class_desc->prop_cnt++;

    class_desc->props = compiler_alloc(ctx->code, class_desc->prop_cnt*sizeof(*class_desc->props));
    if(!class_desc->props)
        return E_OUTOFMEMORY;

    for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next, i++) {
        if(lookup_class_funcs(class_desc, prop_decl->name)) {
            FIXME("Property %s redefined\n", debugstr_w(prop_decl->name));
            return E_FAIL;
        }

        class_desc->props[i].name = compiler_alloc_string(ctx->code, prop_decl->name);
        if(!class_desc->props[i].name)
            return E_OUTOFMEMORY;

        class_desc->props[i].is_public = prop_decl->is_public;
1661
        class_desc->props[i].is_array = prop_decl->is_array;
1662

1663
        if(prop_decl->is_array)
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
            class_desc->array_cnt++;
    }

    if(class_desc->array_cnt) {
        class_desc->array_descs = compiler_alloc(ctx->code, class_desc->array_cnt*sizeof(*class_desc->array_descs));
        if(!class_desc->array_descs)
            return E_OUTOFMEMORY;

        for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next) {
            if(prop_decl->is_array) {
                hres = fill_array_desc(ctx, prop_decl, class_desc->array_descs + i++);
                if(FAILED(hres))
                    return hres;
            }
        }
1679 1680
    }

1681 1682 1683 1684 1685
    class_desc->next = ctx->classes;
    ctx->classes = class_desc;
    return S_OK;
}

1686 1687
static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
{
1688
    class_desc_t *class;
1689
    dynamic_var_t *var;
1690
    function_t *func;
1691 1692 1693 1694 1695 1696

    for(var = script->global_vars; var; var = var->next) {
        if(!strcmpiW(var->name, identifier))
            return TRUE;
    }

1697 1698 1699 1700 1701
    for(func = script->global_funcs; func; func = func->next) {
        if(!strcmpiW(func->name, identifier))
            return TRUE;
    }

1702 1703 1704 1705 1706
    for(class = script->classes; class; class = class->next) {
        if(!strcmpiW(class->name, identifier))
            return TRUE;
    }

1707 1708 1709 1710 1711
    return FALSE;
}

static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
{
1712
    class_desc_t *class;
1713
    dynamic_var_t *var;
1714
    function_t *func;
1715 1716 1717 1718 1719 1720 1721 1722

    for(var = ctx->global_vars; var; var = var->next) {
        if(lookup_script_identifier(script, var->name)) {
            FIXME("%s: redefined\n", debugstr_w(var->name));
            return E_FAIL;
        }
    }

1723 1724 1725 1726 1727 1728 1729
    for(func = ctx->funcs; func; func = func->next) {
        if(lookup_script_identifier(script, func->name)) {
            FIXME("%s: redefined\n", debugstr_w(func->name));
            return E_FAIL;
        }
    }

1730 1731 1732 1733 1734 1735 1736
    for(class = ctx->classes; class; class = class->next) {
        if(lookup_script_identifier(script, class->name)) {
            FIXME("%s: redefined\n", debugstr_w(class->name));
            return E_FAIL;
        }
    }

1737 1738 1739 1740 1741
    return S_OK;
}

void release_vbscode(vbscode_t *code)
{
1742 1743
    unsigned i;

1744
    list_remove(&code->entry);
1745 1746 1747 1748

    for(i=0; i < code->bstr_cnt; i++)
        SysFreeString(code->bstr_pool[i]);

1749
    heap_pool_free(&code->heap);
1750

1751
    heap_free(code->bstr_pool);
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
    heap_free(code->source);
    heap_free(code->instrs);
    heap_free(code);
}

static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
{
    vbscode_t *ret;

    ret = heap_alloc(sizeof(*ret));
    if(!ret)
        return NULL;

    ret->source = heap_strdupW(source);
    if(!ret->source) {
        heap_free(ret);
        return NULL;
    }

    ret->instrs = heap_alloc(32*sizeof(instr_t));
    if(!ret->instrs) {
        release_vbscode(ret);
        return NULL;
    }

1777
    ctx->instr_cnt = 1;
1778
    ctx->instr_size = 32;
1779
    heap_pool_init(&ret->heap);
1780

1781 1782
    ret->option_explicit = ctx->parser.option_explicit;

1783 1784 1785
    ret->bstr_pool = NULL;
    ret->bstr_pool_size = 0;
    ret->bstr_cnt = 0;
1786
    ret->pending_exec = FALSE;
1787

1788 1789 1790 1791 1792
    ret->main_code.type = FUNC_GLOBAL;
    ret->main_code.name = NULL;
    ret->main_code.code_ctx = ret;
    ret->main_code.vars = NULL;
    ret->main_code.var_cnt = 0;
1793
    ret->main_code.array_cnt = 0;
1794 1795
    ret->main_code.arg_cnt = 0;
    ret->main_code.args = NULL;
1796 1797 1798 1799 1800

    list_init(&ret->entry);
    return ret;
}

1801 1802 1803 1804 1805 1806 1807 1808
static void release_compiler(compile_ctx_t *ctx)
{
    parser_release(&ctx->parser);
    heap_free(ctx->labels);
    if(ctx->code)
        release_vbscode(ctx->code);
}

1809
HRESULT compile_script(script_ctx_t *script, const WCHAR *src, const WCHAR *delimiter, vbscode_t **ret)
1810
{
1811 1812
    function_t *new_func;
    function_decl_t *func_decl;
1813
    class_decl_t *class_decl;
1814
    compile_ctx_t ctx;
1815
    vbscode_t *code;
1816 1817
    HRESULT hres;

1818
    hres = parse_script(&ctx.parser, src, delimiter);
1819 1820 1821
    if(FAILED(hres))
        return hres;

1822
    code = ctx.code = alloc_vbscode(&ctx, src);
1823 1824 1825
    if(!ctx.code)
        return E_OUTOFMEMORY;

1826 1827
    ctx.funcs = NULL;
    ctx.func_decls = NULL;
1828
    ctx.global_vars = NULL;
1829
    ctx.classes = NULL;
1830
    ctx.labels = NULL;
1831
    ctx.global_consts = NULL;
1832
    ctx.stat_ctx = NULL;
1833
    ctx.labels_cnt = ctx.labels_size = 0;
1834

1835
    hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->main_code);
1836
    if(FAILED(hres)) {
1837
        release_compiler(&ctx);
1838 1839 1840
        return hres;
    }

1841 1842
    ctx.global_consts = ctx.const_decls;

1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
    for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
        hres = create_function(&ctx, func_decl, &new_func);
        if(FAILED(hres)) {
            release_compiler(&ctx);
            return hres;
        }

        new_func->next = ctx.funcs;
        ctx.funcs = new_func;
    }

1854 1855 1856 1857 1858 1859 1860 1861
    for(class_decl = ctx.parser.class_decls; class_decl; class_decl = class_decl->next) {
        hres = compile_class(&ctx, class_decl);
        if(FAILED(hres)) {
            release_compiler(&ctx);
            return hres;
        }
    }

1862 1863
    hres = check_script_collisions(&ctx, script);
    if(FAILED(hres)) {
1864
        release_compiler(&ctx);
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
        return hres;
    }

    if(ctx.global_vars) {
        dynamic_var_t *var;

        for(var = ctx.global_vars; var->next; var = var->next);

        var->next = script->global_vars;
        script->global_vars = ctx.global_vars;
    }

1877 1878 1879 1880 1881 1882 1883
    if(ctx.funcs) {
        for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);

        new_func->next = script->global_funcs;
        script->global_funcs = ctx.funcs;
    }

1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
    if(ctx.classes) {
        class_desc_t *class = ctx.classes;

        while(1) {
            class->ctx = script;
            if(!class->next)
                break;
            class = class->next;
        }

        class->next = script->classes;
        script->classes = ctx.classes;
    }

1898 1899 1900
    if(TRACE_ON(vbscript_disas))
        dump_code(&ctx);

1901 1902 1903 1904 1905
    ctx.code = NULL;
    release_compiler(&ctx);

    list_add_tail(&script->code_list, &code->entry);
    *ret = code;
1906 1907
    return S_OK;
}