compile.c 40.4 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

typedef struct {
    parser_ctx_t parser;

    unsigned instr_cnt;
    unsigned instr_size;
    vbscode_t *code;
36

37 38 39 40
    unsigned *labels;
    unsigned labels_size;
    unsigned labels_cnt;

41
    unsigned while_end_label;
42
    unsigned for_end_label;
43
    unsigned sub_end_label;
44
    unsigned func_end_label;
45
    unsigned prop_end_label;
46

47 48
    dim_decl_t *dim_decls;
    dynamic_var_t *global_vars;
49

50 51 52
    const_decl_t *const_decls;
    const_decl_t *global_consts;

53 54 55
    function_t *func;
    function_t *funcs;
    function_decl_t *func_decls;
56 57

    class_desc_t *classes;
58 59
} compile_ctx_t;

60
static HRESULT compile_expression(compile_ctx_t*,expression_t*);
61 62 63
static HRESULT compile_statement(compile_ctx_t*,statement_t*);

static const struct {
64
    const char *op_str;
65 66 67
    instr_arg_type_t arg1_type;
    instr_arg_type_t arg2_type;
} instr_info[] = {
68
#define X(n,a,b,c) {#n,b,c},
69 70 71
OP_LIST
#undef X
};
72

73 74 75 76 77 78 79 80 81 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;
    default:
        assert(0);
    }
}

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

    for(instr = ctx->code->instrs; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
102
        TRACE_(vbscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
103 104 105 106 107 108
        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");
    }
}

109 110 111 112 113
static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
{
    return vbsheap_alloc(&vbscode->heap, size);
}

114 115 116 117 118 119 120 121 122 123
static inline void *compiler_alloc_zero(vbscode_t *vbscode, size_t size)
{
    void *ret;

    ret = vbsheap_alloc(&vbscode->heap, size);
    if(ret)
        memset(ret, 0, size);
    return ret;
}

124 125 126 127 128 129 130 131 132 133 134 135
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;
}

136 137 138 139 140 141
static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
{
    assert(id < ctx->instr_cnt);
    return ctx->code->instrs + id;
}

142 143 144 145 146 147 148 149 150
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)
151
            return 0;
152 153 154 155 156 157 158 159 160

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

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

161 162 163 164 165
static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
{
    unsigned ret;

    ret = push_instr(ctx, op);
166
    if(!ret)
167 168 169 170 171 172
        return E_OUTOFMEMORY;

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

173 174 175 176 177
static HRESULT push_instr_uint(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
{
    unsigned ret;

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

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

185 186 187 188 189
static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
{
    unsigned ret;

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

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

197 198
static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
{
199 200
    unsigned instr;
    WCHAR *str;
201

202 203 204 205 206
    str = compiler_alloc_string(ctx->code, arg);
    if(!str)
        return E_OUTOFMEMORY;

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

210
    instr_ptr(ctx, instr)->arg1.str = str;
211 212 213
    return S_OK;
}

214 215 216 217 218 219 220 221 222 223
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);
224
    if(!instr)
225 226 227 228 229 230 231
        return E_OUTOFMEMORY;

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

232 233 234 235 236 237 238 239
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) {
240
        BSTR *new_pool;
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

        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++];
}

257 258 259 260 261 262 263 264 265 266
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);
267
    if(!instr)
268 269 270 271 272 273
        return E_OUTOFMEMORY;

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

274
static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
275
{
276
    unsigned instr;
277 278
    BSTR bstr;

279
    bstr = alloc_bstr_arg(ctx, arg1);
280 281
    if(!bstr)
        return E_OUTOFMEMORY;
282

283
    instr = push_instr(ctx, op);
284
    if(!instr)
285 286
        return E_OUTOFMEMORY;

287
    instr_ptr(ctx, instr)->arg1.bstr = bstr;
288
    instr_ptr(ctx, instr)->arg2.uint = arg2;
289 290 291
    return S_OK;
}

292 293 294 295 296 297 298
#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)
299
            return 0;
300 301 302 303 304 305
        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)
306
            return 0;
307 308 309 310 311 312 313 314 315 316 317 318 319 320

        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;
}

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
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;
}

341
static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
342
{
343
    unsigned arg_cnt = 0;
344 345
    HRESULT hres;

346 347 348 349 350 351 352
    while(args) {
        hres = compile_expression(ctx, args);
        if(FAILED(hres))
            return hres;

        arg_cnt++;
        args = args->next;
353 354
    }

355 356 357 358
    *ret = arg_cnt;
    return S_OK;
}

359
static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
360 361 362 363
{
    unsigned arg_cnt = 0;
    HRESULT hres;

364 365 366 367 368 369 370 371
    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);
    }

372 373 374 375
    hres = compile_args(ctx, expr->args, &arg_cnt);
    if(FAILED(hres))
        return hres;

376
    if(expr->obj_expr) {
377 378 379 380 381
        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);
382
    }else {
383
        hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
384 385 386 387 388
    }

    return hres;
}

389 390 391 392 393 394 395 396
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;

397
    return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
398 399
}

400 401 402 403 404 405 406 407 408 409 410 411
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;

412
    return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
413 414
}

415 416 417
static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
{
    switch(expr->type) {
418 419
    case EXPR_ADD:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
420 421
    case EXPR_AND:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
422 423
    case EXPR_BOOL:
        return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
424 425
    case EXPR_CONCAT:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
426 427
    case EXPR_DIV:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
428 429
    case EXPR_DOUBLE:
        return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
430
    case EXPR_EMPTY:
431
        return push_instr(ctx, OP_empty) ? S_OK : E_OUTOFMEMORY;
432 433
    case EXPR_EQUAL:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
434 435
    case EXPR_EQV:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eqv);
436 437
    case EXPR_EXP:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
438 439 440 441
    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);
442 443
    case EXPR_IDIV:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
444 445
    case EXPR_IS:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_is);
446 447
    case EXPR_IMP:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_imp);
448 449 450 451
    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);
452
    case EXPR_ME:
453
        return push_instr(ctx, OP_me) ? S_OK : E_OUTOFMEMORY;
454 455
    case EXPR_MEMBER:
        return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
456 457
    case EXPR_MOD:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
458 459
    case EXPR_MUL:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
460 461
    case EXPR_NEG:
        return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
462 463
    case EXPR_NEQUAL:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
464 465
    case EXPR_NEW:
        return push_instr_str(ctx, OP_new, ((string_expression_t*)expr)->value);
466 467
    case EXPR_NOT:
        return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
468
    case EXPR_NOTHING:
469
        return push_instr(ctx, OP_nothing) ? S_OK : E_OUTOFMEMORY;
470
    case EXPR_NULL:
471
        return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
472 473
    case EXPR_OR:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
474 475
    case EXPR_STRING:
        return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
476 477
    case EXPR_SUB:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
478 479 480 481
    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);
482 483
    case EXPR_XOR:
        return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
484 485 486 487 488 489 490 491
    default:
        FIXME("Unimplemented expression type %d\n", expr->type);
        return E_NOTIMPL;
    }

    return S_OK;
}

492 493
static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
{
494
    unsigned cnd_jmp, endif_label = 0;
495 496 497 498 499 500 501 502
    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);
503
    if(!cnd_jmp)
504 505 506 507 508 509 510 511
        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);
512
        if(!endif_label)
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
            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);
528
        if(!cnd_jmp)
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
            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;
    }

548
    if(endif_label)
549 550 551 552
        label_set_addr(ctx, endif_label);
    return S_OK;
}

553 554
static HRESULT compile_while_statement(compile_ctx_t *ctx, while_statement_t *stat)
{
555
    unsigned start_addr, prev_label;
556 557 558 559 560 561 562 563 564
    unsigned jmp_end;
    HRESULT hres;

    start_addr = ctx->instr_cnt;

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

565
    jmp_end = push_instr(ctx, stat->stat.type == STAT_UNTIL ? OP_jmp_true : OP_jmp_false);
566
    if(!jmp_end)
567 568
        return E_OUTOFMEMORY;

569
    prev_label = ctx->while_end_label;
570
    if(stat->stat.type != STAT_WHILE && !(ctx->while_end_label = alloc_label(ctx)))
571
        return E_OUTOFMEMORY;
572

573 574 575 576 577 578 579 580 581
    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;
582 583 584 585 586 587

    if(stat->stat.type != STAT_WHILE) {
        label_set_addr(ctx, ctx->while_end_label);
        ctx->while_end_label = prev_label;
    }

588 589 590
    return S_OK;
}

591 592 593 594 595 596 597 598
static HRESULT compile_dowhile_statement(compile_ctx_t *ctx, while_statement_t *stat)
{
    unsigned start_addr, prev_label;
    HRESULT hres;

    start_addr = ctx->instr_cnt;

    prev_label = ctx->while_end_label;
599
    if(!(ctx->while_end_label = alloc_label(ctx)))
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
        return E_OUTOFMEMORY;

    hres = compile_statement(ctx, stat->body);
    if(FAILED(hres))
        return hres;

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

    hres = push_instr_addr(ctx, stat->stat.type == STAT_DOUNTIL ? OP_jmp_false : OP_jmp_true, start_addr);
    if(FAILED(hres))
        return hres;

    label_set_addr(ctx, ctx->while_end_label);
    ctx->while_end_label = prev_label;
    return S_OK;
}

619 620 621 622 623 624
static HRESULT compile_foreach_statement(compile_ctx_t *ctx, foreach_statement_t *stat)
{
    FIXME("for each loop not implemented\n");
    return E_NOTIMPL;
}

625 626
static HRESULT compile_forto_statement(compile_ctx_t *ctx, forto_statement_t *stat)
{
627
    unsigned step_instr, instr, prev_label;
628 629 630 631 632 633 634 635 636 637 638 639
    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);
640
    if(!instr)
641 642 643 644 645 646 647
        return E_OUTOFMEMORY;
    instr_ptr(ctx, instr)->arg1.bstr = identifier;

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

648
    if(!push_instr(ctx, OP_val))
649 650 651 652 653 654 655
        return E_OUTOFMEMORY;

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

656
        if(!push_instr(ctx, OP_val))
657 658 659 660 661 662 663
            return E_OUTOFMEMORY;
    }else {
        hres = push_instr_int(ctx, OP_short, 1);
        if(FAILED(hres))
            return hres;
    }

664 665
    prev_label = ctx->for_end_label;
    ctx->for_end_label = alloc_label(ctx);
666
    if(!ctx->for_end_label)
667 668
        return E_OUTOFMEMORY;

669
    step_instr = push_instr(ctx, OP_step);
670
    if(!step_instr)
671 672
        return E_OUTOFMEMORY;
    instr_ptr(ctx, step_instr)->arg2.bstr = identifier;
673
    instr_ptr(ctx, step_instr)->arg1.uint = ctx->for_end_label;
674 675 676 677 678 679

    hres = compile_statement(ctx, stat->body);
    if(FAILED(hres))
        return hres;

    instr = push_instr(ctx, OP_incc);
680
    if(!instr)
681 682 683 684 685 686 687
        return E_OUTOFMEMORY;
    instr_ptr(ctx, instr)->arg1.bstr = identifier;

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

688 689
    label_set_addr(ctx, ctx->for_end_label);
    ctx->for_end_label = prev_label;
690 691 692 693

    return push_instr_uint(ctx, OP_pop, 2);
}

694
static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat, BOOL is_set)
695
{
696 697
    unsigned args_cnt;
    vbsop_t op;
698 699 700
    HRESULT hres;

    if(stat->member_expr->obj_expr) {
701 702 703 704
        hres = compile_expression(ctx, stat->member_expr->obj_expr);
        if(FAILED(hres))
            return hres;

705
        op = is_set ? OP_set_member : OP_assign_member;
706
    }else {
707
        op = is_set ? OP_set_ident : OP_assign_ident;
708 709
    }

710 711 712 713
    hres = compile_expression(ctx, stat->value_expr);
    if(FAILED(hres))
        return hres;

714 715 716 717 718
    hres = compile_args(ctx, stat->member_expr->args, &args_cnt);
    if(FAILED(hres))
        return hres;

    return push_instr_bstr_uint(ctx, op, stat->member_expr->identifier, args_cnt);
719 720
}

721 722 723 724 725 726 727 728 729 730 731 732
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;
}

733 734 735 736 737 738 739 740 741 742 743 744
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;
}

745 746 747 748 749
static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
{
    dim_decl_t *dim_decl = stat->dim_decls;

    while(1) {
750 751
        if(lookup_dim_decls(ctx, dim_decl->name) || lookup_args_name(ctx, dim_decl->name)
           || lookup_const_decls(ctx, dim_decl->name, FALSE)) {
752 753 754 755
            FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
            return E_FAIL;
        }

756
        ctx->func->var_cnt++;
757 758 759 760 761 762 763 764 765 766
        if(!dim_decl->next)
            break;
        dim_decl = dim_decl->next;
    }

    dim_decl->next = ctx->dim_decls;
    ctx->dim_decls = stat->dim_decls;
    return S_OK;
}

767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
static HRESULT compile_const_statement(compile_ctx_t *ctx, const_statement_t *stat)
{
    const_decl_t *decl, *next_decl = stat->decls;

    do {
        decl = next_decl;

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

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

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

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

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

    return S_OK;
}

800 801 802 803 804 805 806 807 808 809 810 811
static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
{
    if(ctx->func != &ctx->code->global_code) {
        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;
}

812 813
static HRESULT compile_exitdo_statement(compile_ctx_t *ctx)
{
814
    if(!ctx->while_end_label) {
815 816 817 818 819 820 821
        FIXME("Exit Do outside Do Loop\n");
        return E_FAIL;
    }

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

822 823
static HRESULT compile_exitfor_statement(compile_ctx_t *ctx)
{
824
    if(!ctx->for_end_label) {
825 826 827 828 829 830 831
        FIXME("Exit For outside For Loop\n");
        return E_FAIL;
    }

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

832 833
static HRESULT compile_exitsub_statement(compile_ctx_t *ctx)
{
834
    if(!ctx->sub_end_label) {
835 836 837 838 839 840 841
        FIXME("Exit Sub outside Sub?\n");
        return E_FAIL;
    }

    return push_instr_addr(ctx, OP_jmp, ctx->sub_end_label);
}

842 843
static HRESULT compile_exitfunc_statement(compile_ctx_t *ctx)
{
844
    if(!ctx->func_end_label) {
845 846 847 848 849 850 851
        FIXME("Exit Function outside Function?\n");
        return E_FAIL;
    }

    return push_instr_addr(ctx, OP_jmp, ctx->func_end_label);
}

852 853
static HRESULT compile_exitprop_statement(compile_ctx_t *ctx)
{
854
    if(!ctx->prop_end_label) {
855 856 857 858 859 860 861
        FIXME("Exit Property outside Property?\n");
        return E_FAIL;
    }

    return push_instr_addr(ctx, OP_jmp, ctx->prop_end_label);
}

862 863 864 865 866
static HRESULT compile_onerror_statement(compile_ctx_t *ctx, onerror_statement_t *stat)
{
    return push_instr_int(ctx, OP_errmode, stat->resume_next);
}

867 868 869 870 871 872
static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
{
    HRESULT hres;

    while(stat) {
        switch(stat->type) {
873
        case STAT_ASSIGN:
874
            hres = compile_assign_statement(ctx, (assign_statement_t*)stat, FALSE);
875
            break;
876
        case STAT_CALL:
877
            hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
878
            break;
879 880 881
        case STAT_CONST:
            hres = compile_const_statement(ctx, (const_statement_t*)stat);
            break;
882 883 884
        case STAT_DIM:
            hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
            break;
885 886 887 888
        case STAT_DOWHILE:
        case STAT_DOUNTIL:
            hres = compile_dowhile_statement(ctx, (while_statement_t*)stat);
            break;
889 890 891
        case STAT_EXITDO:
            hres = compile_exitdo_statement(ctx);
            break;
892 893 894
        case STAT_EXITFOR:
            hres = compile_exitfor_statement(ctx);
            break;
895 896 897
        case STAT_EXITFUNC:
            hres = compile_exitfunc_statement(ctx);
            break;
898 899 900
        case STAT_EXITPROP:
            hres = compile_exitprop_statement(ctx);
            break;
901 902 903
        case STAT_EXITSUB:
            hres = compile_exitsub_statement(ctx);
            break;
904 905 906
        case STAT_FOREACH:
            hres = compile_foreach_statement(ctx, (foreach_statement_t*)stat);
            break;
907 908 909
        case STAT_FORTO:
            hres = compile_forto_statement(ctx, (forto_statement_t*)stat);
            break;
910 911 912
        case STAT_FUNC:
            hres = compile_function_statement(ctx, (function_statement_t*)stat);
            break;
913 914 915
        case STAT_IF:
            hres = compile_if_statement(ctx, (if_statement_t*)stat);
            break;
916 917 918
        case STAT_ONERROR:
            hres = compile_onerror_statement(ctx, (onerror_statement_t*)stat);
            break;
919 920 921
        case STAT_SET:
            hres = compile_assign_statement(ctx, (assign_statement_t*)stat, TRUE);
            break;
922
        case STAT_STOP:
923
            hres = push_instr(ctx, OP_stop) ? S_OK : E_OUTOFMEMORY;
924
            break;
925
        case STAT_UNTIL:
926
        case STAT_WHILE:
927
        case STAT_WHILELOOP:
928 929
            hres = compile_while_statement(ctx, (while_statement_t*)stat);
            break;
930 931 932 933 934 935 936 937 938 939 940 941 942
        default:
            FIXME("Unimplemented statement type %d\n", stat->type);
            hres = E_NOTIMPL;
        }

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

    return S_OK;
}

943
static void resolve_labels(compile_ctx_t *ctx, unsigned off)
944 945 946
{
    instr_t *instr;

947
    for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
948 949 950 951 952 953 954 955 956 957
        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;
}

958 959 960 961 962 963
static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
{
    HRESULT hres;

    func->code_off = ctx->instr_cnt;

964 965 966 967 968
    ctx->while_end_label = 0;
    ctx->for_end_label = 0;
    ctx->sub_end_label = 0;
    ctx->func_end_label = 0;
    ctx->prop_end_label = 0;
969 970

    switch(func->type) {
971 972
    case FUNC_FUNCTION:
        ctx->func_end_label = alloc_label(ctx);
973 974
        if(!ctx->func_end_label)
            return E_OUTOFMEMORY;
975
        break;
976 977
    case FUNC_SUB:
        ctx->sub_end_label = alloc_label(ctx);
978
        if(!ctx->sub_end_label)
979 980
            return E_OUTOFMEMORY;
        break;
981 982 983
    case FUNC_PROPGET:
    case FUNC_PROPLET:
    case FUNC_PROPSET:
984
    case FUNC_DEFGET:
985
        ctx->prop_end_label = alloc_label(ctx);
986
        if(!ctx->prop_end_label)
987 988
            return E_OUTOFMEMORY;
        break;
989 990 991 992
    case FUNC_GLOBAL:
        break;
    }

993 994
    ctx->func = func;
    ctx->dim_decls = NULL;
995
    ctx->const_decls = NULL;
996
    hres = compile_statement(ctx, stat);
997
    ctx->func = NULL;
998 999 1000
    if(FAILED(hres))
        return hres;

1001 1002
    assert(!ctx->while_end_label);
    assert(!ctx->for_end_label);
1003

1004
    if(ctx->sub_end_label)
1005
        label_set_addr(ctx, ctx->sub_end_label);
1006
    if(ctx->func_end_label)
1007
        label_set_addr(ctx, ctx->func_end_label);
1008
    if(ctx->prop_end_label)
1009
        label_set_addr(ctx, ctx->prop_end_label);
1010

1011
    if(!push_instr(ctx, OP_ret))
1012 1013
        return E_OUTOFMEMORY;

1014
    resolve_labels(ctx, func->code_off);
1015

1016
    if(func->var_cnt) {
1017 1018
        dim_decl_t *dim_decl;

1019 1020
        if(func->type == FUNC_GLOBAL) {
            dynamic_var_t *new_var;
1021

1022 1023
            func->var_cnt = 0;

1024 1025 1026 1027
            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;
1028

1029 1030 1031
                new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
                if(!new_var->name)
                    return E_OUTOFMEMORY;
1032

1033
                V_VT(&new_var->v) = VT_EMPTY;
1034
                new_var->is_const = FALSE;
1035 1036 1037 1038 1039

                new_var->next = ctx->global_vars;
                ctx->global_vars = new_var;
            }
        }else {
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
            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);
1053 1054 1055 1056 1057 1058
        }
    }

    return S_OK;
}

1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
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;

1076
    if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name) || lookup_const_decls(ctx, decl->name, FALSE)) {
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
        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;

1089 1090
    func->vars = NULL;
    func->var_cnt = 0;
1091 1092
    func->code_ctx = ctx->code;
    func->type = decl->type;
1093
    func->is_public = decl->is_public;
1094

1095
    func->arg_cnt = 0;
1096
    if(decl->args) {
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
        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;
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
    }

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

    *ret = func;
    return S_OK;
}

1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
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;
}

1137 1138
static HRESULT create_class_funcprop(compile_ctx_t *ctx, function_decl_t *func_decl, vbdisp_funcprop_desc_t *desc)
{
1139 1140 1141 1142
    vbdisp_invoke_type_t invoke_type;
    function_decl_t *funcprop_decl;
    HRESULT hres;

1143 1144 1145 1146
    desc->name = compiler_alloc_string(ctx->code, func_decl->name);
    if(!desc->name)
        return E_OUTOFMEMORY;

1147 1148 1149 1150 1151
    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:
1152
        case FUNC_DEFGET:
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
            invoke_type = VBDISP_CALLGET;
            break;
        case FUNC_PROPLET:
            invoke_type = VBDISP_LET;
            break;
        case FUNC_PROPSET:
            invoke_type = VBDISP_SET;
            break;
        default:
            assert(0);
        }
1164

1165
        assert(!desc->entries[invoke_type]);
1166

1167 1168 1169 1170 1171 1172 1173 1174 1175
        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;
1176 1177
}

1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
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;
}

1190 1191
static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl)
{
1192
    function_decl_t *func_decl, *func_prop_decl;
1193
    class_prop_decl_t *prop_decl;
1194
    class_desc_t *class_desc;
1195 1196
    unsigned i;
    HRESULT hres;
1197

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

1201
    if(lookup_dim_decls(ctx, class_decl->name) || lookup_funcs_name(ctx, class_decl->name)
1202
            || lookup_const_decls(ctx, class_decl->name, FALSE) || lookup_class_name(ctx, class_decl->name)) {
1203 1204 1205 1206
        FIXME("%s: redefinition\n", debugstr_w(class_decl->name));
        return E_FAIL;
    }

1207
    class_desc = compiler_alloc_zero(ctx->code, sizeof(*class_desc));
1208 1209 1210 1211 1212 1213 1214
    if(!class_desc)
        return E_OUTOFMEMORY;

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

1215 1216
    class_desc->func_cnt = 1; /* always allocate slot for default getter */

1217 1218 1219 1220 1221 1222 1223 1224
    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++;
    }
1225 1226 1227 1228 1229 1230 1231

    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++) {
1232 1233 1234 1235 1236 1237 1238
        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;
            }
        }

1239 1240 1241 1242 1243 1244 1245
        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;
1246 1247 1248 1249 1250 1251 1252
        }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;
1253 1254
        }

1255
        hres = create_class_funcprop(ctx, func_decl, class_desc->funcs + (func_prop_decl ? 0 : i));
1256 1257 1258 1259
        if(FAILED(hres))
            return hres;
    }

1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
    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;
    }

1280 1281 1282 1283 1284
    class_desc->next = ctx->classes;
    ctx->classes = class_desc;
    return S_OK;
}

1285 1286
static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
{
1287
    class_desc_t *class;
1288
    dynamic_var_t *var;
1289
    function_t *func;
1290 1291 1292 1293 1294 1295

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

1296 1297 1298 1299 1300
    for(func = script->global_funcs; func; func = func->next) {
        if(!strcmpiW(func->name, identifier))
            return TRUE;
    }

1301 1302 1303 1304 1305
    for(class = script->classes; class; class = class->next) {
        if(!strcmpiW(class->name, identifier))
            return TRUE;
    }

1306 1307 1308 1309 1310
    return FALSE;
}

static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
{
1311
    class_desc_t *class;
1312
    dynamic_var_t *var;
1313
    function_t *func;
1314 1315 1316 1317 1318 1319 1320 1321

    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;
        }
    }

1322 1323 1324 1325 1326 1327 1328
    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;
        }
    }

1329 1330 1331 1332 1333 1334 1335
    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;
        }
    }

1336 1337 1338 1339 1340
    return S_OK;
}

void release_vbscode(vbscode_t *code)
{
1341 1342
    unsigned i;

1343
    list_remove(&code->entry);
1344 1345 1346 1347

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

1348 1349
    vbsheap_free(&code->heap);

1350
    heap_free(code->bstr_pool);
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
    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;
    }

1376
    ctx->instr_cnt = 1;
1377
    ctx->instr_size = 32;
1378
    vbsheap_init(&ret->heap);
1379

1380 1381
    ret->option_explicit = ctx->parser.option_explicit;

1382 1383 1384
    ret->bstr_pool = NULL;
    ret->bstr_pool_size = 0;
    ret->bstr_cnt = 0;
1385
    ret->global_executed = FALSE;
1386

1387 1388
    ret->global_code.type = FUNC_GLOBAL;
    ret->global_code.name = NULL;
1389
    ret->global_code.code_ctx = ret;
1390 1391
    ret->global_code.vars = NULL;
    ret->global_code.var_cnt = 0;
1392 1393
    ret->global_code.arg_cnt = 0;
    ret->global_code.args = NULL;
1394 1395 1396 1397 1398

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

1399 1400 1401 1402 1403 1404 1405 1406
static void release_compiler(compile_ctx_t *ctx)
{
    parser_release(&ctx->parser);
    heap_free(ctx->labels);
    if(ctx->code)
        release_vbscode(ctx->code);
}

1407 1408
HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
{
1409 1410
    function_t *new_func;
    function_decl_t *func_decl;
1411
    class_decl_t *class_decl;
1412
    compile_ctx_t ctx;
1413
    vbscode_t *code;
1414 1415 1416 1417 1418 1419
    HRESULT hres;

    hres = parse_script(&ctx.parser, src);
    if(FAILED(hres))
        return hres;

1420
    code = ctx.code = alloc_vbscode(&ctx, src);
1421 1422 1423
    if(!ctx.code)
        return E_OUTOFMEMORY;

1424 1425
    ctx.funcs = NULL;
    ctx.func_decls = NULL;
1426 1427
    ctx.global_vars = NULL;
    ctx.dim_decls = NULL;
1428
    ctx.classes = NULL;
1429
    ctx.labels = NULL;
1430
    ctx.global_consts = NULL;
1431
    ctx.labels_cnt = ctx.labels_size = 0;
1432

1433
    hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
1434
    if(FAILED(hres)) {
1435
        release_compiler(&ctx);
1436 1437 1438
        return hres;
    }

1439 1440
    ctx.global_consts = ctx.const_decls;

1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
    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;
    }

1452 1453 1454 1455 1456 1457 1458 1459
    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;
        }
    }

1460 1461
    hres = check_script_collisions(&ctx, script);
    if(FAILED(hres)) {
1462
        release_compiler(&ctx);
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
        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;
    }

1475 1476 1477 1478 1479 1480 1481
    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;
    }

1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
    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;
    }

1496 1497 1498
    if(TRACE_ON(vbscript_disas))
        dump_code(&ctx);

1499 1500 1501 1502 1503
    ctx.code = NULL;
    release_compiler(&ctx);

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