d3d8: Get rid of the format switching code in d3d8_device_CopyRects().
[wine.git] / dlls / vbscript / compile.c
blob73f06154efe14bd7ae245fd6aead103abae8a250
1 /*
2 * Copyright 2011 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <assert.h>
21 #include "vbscript.h"
22 #include "parse.h"
23 #include "parser.tab.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
28 WINE_DECLARE_DEBUG_CHANNEL(vbscript_disas);
30 typedef struct _statement_ctx_t {
31 unsigned stack_use;
33 unsigned while_end_label;
34 unsigned for_end_label;
36 struct _statement_ctx_t *next;
37 } statement_ctx_t;
39 typedef struct {
40 parser_ctx_t parser;
42 unsigned instr_cnt;
43 unsigned instr_size;
44 vbscode_t *code;
46 statement_ctx_t *stat_ctx;
48 unsigned *labels;
49 unsigned labels_size;
50 unsigned labels_cnt;
52 unsigned sub_end_label;
53 unsigned func_end_label;
54 unsigned prop_end_label;
56 dim_decl_t *dim_decls;
57 dim_decl_t *dim_decls_tail;
58 dynamic_var_t *global_vars;
60 const_decl_t *const_decls;
61 const_decl_t *global_consts;
63 function_t *func;
64 function_t *funcs;
65 function_decl_t *func_decls;
67 class_desc_t *classes;
68 } 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*);
73 static const struct {
74 const char *op_str;
75 instr_arg_type_t arg1_type;
76 instr_arg_type_t arg2_type;
77 } instr_info[] = {
78 #define X(n,a,b,c) {#n,b,c},
79 OP_LIST
80 #undef X
83 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
85 switch(type) {
86 case ARG_STR:
87 case ARG_BSTR:
88 TRACE_(vbscript_disas)("\t%s", debugstr_w(arg->str));
89 break;
90 case ARG_INT:
91 TRACE_(vbscript_disas)("\t%d", arg->uint);
92 break;
93 case ARG_UINT:
94 case ARG_ADDR:
95 TRACE_(vbscript_disas)("\t%u", arg->uint);
96 break;
97 case ARG_DOUBLE:
98 TRACE_(vbscript_disas)("\t%lf", *arg->dbl);
99 break;
100 case ARG_NONE:
101 break;
102 DEFAULT_UNREACHABLE;
106 static void dump_code(compile_ctx_t *ctx)
108 instr_t *instr;
110 for(instr = ctx->code->instrs+1; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
111 assert(instr->op < OP_LAST);
112 TRACE_(vbscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
113 dump_instr_arg(instr_info[instr->op].arg1_type, &instr->arg1);
114 dump_instr_arg(instr_info[instr->op].arg2_type, &instr->arg2);
115 TRACE_(vbscript_disas)("\n");
119 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
121 return heap_pool_alloc(&vbscode->heap, size);
124 static inline void *compiler_alloc_zero(vbscode_t *vbscode, size_t size)
126 void *ret;
128 ret = heap_pool_alloc(&vbscode->heap, size);
129 if(ret)
130 memset(ret, 0, size);
131 return ret;
134 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
136 size_t size;
137 WCHAR *ret;
139 size = (strlenW(str)+1)*sizeof(WCHAR);
140 ret = compiler_alloc(vbscode, size);
141 if(ret)
142 memcpy(ret, str, size);
143 return ret;
146 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
148 assert(id < ctx->instr_cnt);
149 return ctx->code->instrs + id;
152 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
154 assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
156 if(ctx->instr_size == ctx->instr_cnt) {
157 instr_t *new_instr;
159 new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
160 if(!new_instr)
161 return 0;
163 ctx->code->instrs = new_instr;
164 ctx->instr_size *= 2;
167 ctx->code->instrs[ctx->instr_cnt].op = op;
168 return ctx->instr_cnt++;
171 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
173 unsigned ret;
175 ret = push_instr(ctx, op);
176 if(!ret)
177 return E_OUTOFMEMORY;
179 instr_ptr(ctx, ret)->arg1.lng = arg;
180 return S_OK;
183 static HRESULT push_instr_uint(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
185 unsigned ret;
187 ret = push_instr(ctx, op);
188 if(!ret)
189 return E_OUTOFMEMORY;
191 instr_ptr(ctx, ret)->arg1.uint = arg;
192 return S_OK;
195 static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
197 unsigned ret;
199 ret = push_instr(ctx, op);
200 if(!ret)
201 return E_OUTOFMEMORY;
203 instr_ptr(ctx, ret)->arg1.uint = arg;
204 return S_OK;
207 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
209 unsigned instr;
210 WCHAR *str;
212 str = compiler_alloc_string(ctx->code, arg);
213 if(!str)
214 return E_OUTOFMEMORY;
216 instr = push_instr(ctx, op);
217 if(!instr)
218 return E_OUTOFMEMORY;
220 instr_ptr(ctx, instr)->arg1.str = str;
221 return S_OK;
224 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
226 unsigned instr;
227 double *d;
229 d = compiler_alloc(ctx->code, sizeof(double));
230 if(!d)
231 return E_OUTOFMEMORY;
233 instr = push_instr(ctx, op);
234 if(!instr)
235 return E_OUTOFMEMORY;
237 *d = arg;
238 instr_ptr(ctx, instr)->arg1.dbl = d;
239 return S_OK;
242 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
244 if(!ctx->code->bstr_pool_size) {
245 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
246 if(!ctx->code->bstr_pool)
247 return NULL;
248 ctx->code->bstr_pool_size = 8;
249 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
250 BSTR *new_pool;
252 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
253 if(!new_pool)
254 return NULL;
256 ctx->code->bstr_pool = new_pool;
257 ctx->code->bstr_pool_size *= 2;
260 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
261 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
262 return NULL;
264 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
267 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
269 unsigned instr;
270 BSTR bstr;
272 bstr = alloc_bstr_arg(ctx, arg);
273 if(!bstr)
274 return E_OUTOFMEMORY;
276 instr = push_instr(ctx, op);
277 if(!instr)
278 return E_OUTOFMEMORY;
280 instr_ptr(ctx, instr)->arg1.bstr = bstr;
281 return S_OK;
284 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
286 unsigned instr;
287 BSTR bstr;
289 bstr = alloc_bstr_arg(ctx, arg1);
290 if(!bstr)
291 return E_OUTOFMEMORY;
293 instr = push_instr(ctx, op);
294 if(!instr)
295 return E_OUTOFMEMORY;
297 instr_ptr(ctx, instr)->arg1.bstr = bstr;
298 instr_ptr(ctx, instr)->arg2.uint = arg2;
299 return S_OK;
302 static HRESULT push_instr_uint_bstr(compile_ctx_t *ctx, vbsop_t op, unsigned arg1, const WCHAR *arg2)
304 unsigned instr;
305 BSTR bstr;
307 bstr = alloc_bstr_arg(ctx, arg2);
308 if(!bstr)
309 return E_OUTOFMEMORY;
311 instr = push_instr(ctx, op);
312 if(!instr)
313 return E_OUTOFMEMORY;
315 instr_ptr(ctx, instr)->arg1.uint = arg1;
316 instr_ptr(ctx, instr)->arg2.bstr = bstr;
317 return S_OK;
320 #define LABEL_FLAG 0x80000000
322 static unsigned alloc_label(compile_ctx_t *ctx)
324 if(!ctx->labels_size) {
325 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
326 if(!ctx->labels)
327 return 0;
328 ctx->labels_size = 8;
329 }else if(ctx->labels_size == ctx->labels_cnt) {
330 unsigned *new_labels;
332 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
333 if(!new_labels)
334 return 0;
336 ctx->labels = new_labels;
337 ctx->labels_size *= 2;
340 return ctx->labels_cnt++ | LABEL_FLAG;
343 static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
345 assert(label & LABEL_FLAG);
346 ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
349 static inline unsigned stack_offset(compile_ctx_t *ctx)
351 statement_ctx_t *iter;
352 unsigned ret = 0;
354 for(iter = ctx->stat_ctx; iter; iter = iter->next)
355 ret += iter->stack_use;
357 return ret;
360 static BOOL emit_catch_jmp(compile_ctx_t *ctx, unsigned stack_off, unsigned code_off)
362 unsigned code;
364 code = push_instr(ctx, OP_catch);
365 if(!code)
366 return FALSE;
368 instr_ptr(ctx, code)->arg1.uint = code_off;
369 instr_ptr(ctx, code)->arg2.uint = stack_off + stack_offset(ctx);
370 return TRUE;
373 static inline BOOL emit_catch(compile_ctx_t *ctx, unsigned off)
375 return emit_catch_jmp(ctx, off, ctx->instr_cnt);
378 static expression_t *lookup_const_decls(compile_ctx_t *ctx, const WCHAR *name, BOOL lookup_global)
380 const_decl_t *decl;
382 for(decl = ctx->const_decls; decl; decl = decl->next) {
383 if(!strcmpiW(decl->name, name))
384 return decl->value_expr;
387 if(!lookup_global)
388 return NULL;
390 for(decl = ctx->global_consts; decl; decl = decl->next) {
391 if(!strcmpiW(decl->name, name))
392 return decl->value_expr;
395 return NULL;
398 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
400 unsigned arg_cnt = 0;
401 HRESULT hres;
403 while(args) {
404 hres = compile_expression(ctx, args);
405 if(FAILED(hres))
406 return hres;
408 arg_cnt++;
409 args = args->next;
412 *ret = arg_cnt;
413 return S_OK;
416 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
418 unsigned arg_cnt = 0;
419 HRESULT hres;
421 if(ret_val && !expr->args) {
422 expression_t *const_expr;
424 const_expr = lookup_const_decls(ctx, expr->identifier, TRUE);
425 if(const_expr)
426 return compile_expression(ctx, const_expr);
429 hres = compile_args(ctx, expr->args, &arg_cnt);
430 if(FAILED(hres))
431 return hres;
433 if(expr->obj_expr) {
434 hres = compile_expression(ctx, expr->obj_expr);
435 if(FAILED(hres))
436 return hres;
438 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);
443 return hres;
446 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
448 HRESULT hres;
450 hres = compile_expression(ctx, expr->subexpr);
451 if(FAILED(hres))
452 return hres;
454 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
457 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
459 HRESULT hres;
461 hres = compile_expression(ctx, expr->left);
462 if(FAILED(hres))
463 return hres;
465 hres = compile_expression(ctx, expr->right);
466 if(FAILED(hres))
467 return hres;
469 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
472 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
474 switch(expr->type) {
475 case EXPR_ADD:
476 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
477 case EXPR_AND:
478 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
479 case EXPR_BOOL:
480 return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
481 case EXPR_BRACKETS:
482 return compile_expression(ctx, ((unary_expression_t*)expr)->subexpr);
483 case EXPR_CONCAT:
484 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
485 case EXPR_DIV:
486 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
487 case EXPR_DOUBLE:
488 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 case EXPR_EQUAL:
492 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
493 case EXPR_EQV:
494 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eqv);
495 case EXPR_EXP:
496 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
497 case EXPR_GT:
498 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
499 case EXPR_GTEQ:
500 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
501 case EXPR_IDIV:
502 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
503 case EXPR_IS:
504 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_is);
505 case EXPR_IMP:
506 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_imp);
507 case EXPR_LT:
508 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
509 case EXPR_LTEQ:
510 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 case EXPR_MEMBER:
514 return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
515 case EXPR_MOD:
516 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
517 case EXPR_MUL:
518 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
519 case EXPR_NEG:
520 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
521 case EXPR_NEQUAL:
522 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
523 case EXPR_NEW:
524 return push_instr_str(ctx, OP_new, ((string_expression_t*)expr)->value);
525 case EXPR_NOT:
526 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
527 case EXPR_NOTHING:
528 return push_instr(ctx, OP_nothing) ? S_OK : E_OUTOFMEMORY;
529 case EXPR_NULL:
530 return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
531 case EXPR_OR:
532 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
533 case EXPR_STRING:
534 return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
535 case EXPR_SUB:
536 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
537 case EXPR_USHORT:
538 return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
539 case EXPR_ULONG:
540 return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
541 case EXPR_XOR:
542 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
543 default:
544 FIXME("Unimplemented expression type %d\n", expr->type);
545 return E_NOTIMPL;
548 return S_OK;
551 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
553 unsigned cnd_jmp, endif_label = 0;
554 elseif_decl_t *elseif_decl;
555 HRESULT hres;
557 hres = compile_expression(ctx, stat->expr);
558 if(FAILED(hres))
559 return hres;
561 cnd_jmp = push_instr(ctx, OP_jmp_false);
562 if(!cnd_jmp)
563 return E_OUTOFMEMORY;
565 if(!emit_catch(ctx, 0))
566 return E_OUTOFMEMORY;
568 hres = compile_statement(ctx, NULL, stat->if_stat);
569 if(FAILED(hres))
570 return hres;
572 if(stat->else_stat || stat->elseifs) {
573 endif_label = alloc_label(ctx);
574 if(!endif_label)
575 return E_OUTOFMEMORY;
577 hres = push_instr_addr(ctx, OP_jmp, endif_label);
578 if(FAILED(hres))
579 return hres;
582 for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
583 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
585 hres = compile_expression(ctx, elseif_decl->expr);
586 if(FAILED(hres))
587 return hres;
589 cnd_jmp = push_instr(ctx, OP_jmp_false);
590 if(!cnd_jmp)
591 return E_OUTOFMEMORY;
593 if(!emit_catch(ctx, 0))
594 return E_OUTOFMEMORY;
596 hres = compile_statement(ctx, NULL, elseif_decl->stat);
597 if(FAILED(hres))
598 return hres;
600 hres = push_instr_addr(ctx, OP_jmp, endif_label);
601 if(FAILED(hres))
602 return hres;
605 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
607 if(stat->else_stat) {
608 hres = compile_statement(ctx, NULL, stat->else_stat);
609 if(FAILED(hres))
610 return hres;
613 if(endif_label)
614 label_set_addr(ctx, endif_label);
615 return S_OK;
618 static HRESULT compile_while_statement(compile_ctx_t *ctx, while_statement_t *stat)
620 statement_ctx_t stat_ctx = {0}, *loop_ctx;
621 unsigned start_addr;
622 unsigned jmp_end;
623 HRESULT hres;
625 start_addr = ctx->instr_cnt;
627 hres = compile_expression(ctx, stat->expr);
628 if(FAILED(hres))
629 return hres;
631 jmp_end = push_instr(ctx, stat->stat.type == STAT_UNTIL ? OP_jmp_true : OP_jmp_false);
632 if(!jmp_end)
633 return E_OUTOFMEMORY;
635 if(!emit_catch(ctx, 0))
636 return E_OUTOFMEMORY;
638 if(stat->stat.type == STAT_WHILE) {
639 loop_ctx = NULL;
640 }else {
641 if(!(stat_ctx.while_end_label = alloc_label(ctx)))
642 return E_OUTOFMEMORY;
643 loop_ctx = &stat_ctx;
646 hres = compile_statement(ctx, loop_ctx, stat->body);
647 if(FAILED(hres))
648 return hres;
650 hres = push_instr_addr(ctx, OP_jmp, start_addr);
651 if(FAILED(hres))
652 return hres;
654 instr_ptr(ctx, jmp_end)->arg1.uint = ctx->instr_cnt;
656 if(loop_ctx)
657 label_set_addr(ctx, stat_ctx.while_end_label);
659 return S_OK;
662 static HRESULT compile_dowhile_statement(compile_ctx_t *ctx, while_statement_t *stat)
664 statement_ctx_t loop_ctx = {0};
665 unsigned start_addr;
666 vbsop_t jmp_op;
667 HRESULT hres;
669 start_addr = ctx->instr_cnt;
671 if(!(loop_ctx.while_end_label = alloc_label(ctx)))
672 return E_OUTOFMEMORY;
674 hres = compile_statement(ctx, &loop_ctx, stat->body);
675 if(FAILED(hres))
676 return hres;
678 if(stat->expr) {
679 hres = compile_expression(ctx, stat->expr);
680 if(FAILED(hres))
681 return hres;
683 jmp_op = stat->stat.type == STAT_DOUNTIL ? OP_jmp_false : OP_jmp_true;
684 }else {
685 jmp_op = OP_jmp;
688 hres = push_instr_addr(ctx, jmp_op, start_addr);
689 if(FAILED(hres))
690 return hres;
692 label_set_addr(ctx, loop_ctx.while_end_label);
694 if(!emit_catch(ctx, 0))
695 return E_OUTOFMEMORY;
697 return S_OK;
700 static HRESULT compile_foreach_statement(compile_ctx_t *ctx, foreach_statement_t *stat)
702 statement_ctx_t loop_ctx = {1};
703 unsigned loop_start;
704 HRESULT hres;
706 /* Preserve a place on the stack in case we throw before having proper enum collection. */
707 if(!push_instr(ctx, OP_empty))
708 return E_OUTOFMEMORY;
710 hres = compile_expression(ctx, stat->group_expr);
711 if(FAILED(hres))
712 return hres;
714 if(!push_instr(ctx, OP_newenum))
715 return E_OUTOFMEMORY;
717 if(!(loop_ctx.for_end_label = alloc_label(ctx)))
718 return E_OUTOFMEMORY;
720 hres = push_instr_uint_bstr(ctx, OP_enumnext, loop_ctx.for_end_label, stat->identifier);
721 if(FAILED(hres))
722 return hres;
724 if(!emit_catch(ctx, 1))
725 return E_OUTOFMEMORY;
727 loop_start = ctx->instr_cnt;
728 hres = compile_statement(ctx, &loop_ctx, stat->body);
729 if(FAILED(hres))
730 return hres;
732 /* We need a separated enumnext here, because we need to jump out of the loop on exception. */
733 hres = push_instr_uint_bstr(ctx, OP_enumnext, loop_ctx.for_end_label, stat->identifier);
734 if(FAILED(hres))
735 return hres;
737 hres = push_instr_addr(ctx, OP_jmp, loop_start);
738 if(FAILED(hres))
739 return hres;
741 label_set_addr(ctx, loop_ctx.for_end_label);
742 return S_OK;
745 static HRESULT compile_forto_statement(compile_ctx_t *ctx, forto_statement_t *stat)
747 statement_ctx_t loop_ctx = {2};
748 unsigned step_instr, instr;
749 BSTR identifier;
750 HRESULT hres;
752 identifier = alloc_bstr_arg(ctx, stat->identifier);
753 if(!identifier)
754 return E_OUTOFMEMORY;
756 hres = compile_expression(ctx, stat->from_expr);
757 if(FAILED(hres))
758 return hres;
760 /* FIXME: Assign should happen after both expressions evaluation. */
761 instr = push_instr(ctx, OP_assign_ident);
762 if(!instr)
763 return E_OUTOFMEMORY;
764 instr_ptr(ctx, instr)->arg1.bstr = identifier;
765 instr_ptr(ctx, instr)->arg2.uint = 0;
767 hres = compile_expression(ctx, stat->to_expr);
768 if(FAILED(hres))
769 return hres;
771 if(!push_instr(ctx, OP_val))
772 return E_OUTOFMEMORY;
774 if(stat->step_expr) {
775 hres = compile_expression(ctx, stat->step_expr);
776 if(FAILED(hres))
777 return hres;
779 if(!push_instr(ctx, OP_val))
780 return E_OUTOFMEMORY;
781 }else {
782 hres = push_instr_int(ctx, OP_short, 1);
783 if(FAILED(hres))
784 return hres;
787 loop_ctx.for_end_label = alloc_label(ctx);
788 if(!loop_ctx.for_end_label)
789 return E_OUTOFMEMORY;
791 step_instr = push_instr(ctx, OP_step);
792 if(!step_instr)
793 return E_OUTOFMEMORY;
794 instr_ptr(ctx, step_instr)->arg2.bstr = identifier;
795 instr_ptr(ctx, step_instr)->arg1.uint = loop_ctx.for_end_label;
797 if(!emit_catch(ctx, 2))
798 return E_OUTOFMEMORY;
800 hres = compile_statement(ctx, &loop_ctx, stat->body);
801 if(FAILED(hres))
802 return hres;
804 /* FIXME: Error handling can't be done compatible with native using OP_incc here. */
805 instr = push_instr(ctx, OP_incc);
806 if(!instr)
807 return E_OUTOFMEMORY;
808 instr_ptr(ctx, instr)->arg1.bstr = identifier;
810 hres = push_instr_addr(ctx, OP_jmp, step_instr);
811 if(FAILED(hres))
812 return hres;
814 hres = push_instr_uint(ctx, OP_pop, 2);
815 if(FAILED(hres))
816 return hres;
818 label_set_addr(ctx, loop_ctx.for_end_label);
820 /* FIXME: reconsider after OP_incc fixup. */
821 if(!emit_catch(ctx, 0))
822 return E_OUTOFMEMORY;
824 return S_OK;
827 static HRESULT compile_select_statement(compile_ctx_t *ctx, select_statement_t *stat)
829 unsigned end_label, case_cnt = 0, *case_labels = NULL, i;
830 case_clausule_t *case_iter;
831 expression_t *expr_iter;
832 HRESULT hres;
834 hres = compile_expression(ctx, stat->expr);
835 if(FAILED(hres))
836 return hres;
838 if(!push_instr(ctx, OP_val))
839 return E_OUTOFMEMORY;
841 end_label = alloc_label(ctx);
842 if(!end_label)
843 return E_OUTOFMEMORY;
845 if(!emit_catch_jmp(ctx, 0, end_label))
846 return E_OUTOFMEMORY;
848 for(case_iter = stat->case_clausules; case_iter; case_iter = case_iter->next)
849 case_cnt++;
851 if(case_cnt) {
852 case_labels = heap_alloc(case_cnt*sizeof(*case_labels));
853 if(!case_labels)
854 return E_OUTOFMEMORY;
857 for(case_iter = stat->case_clausules, i=0; case_iter; case_iter = case_iter->next, i++) {
858 case_labels[i] = alloc_label(ctx);
859 if(!case_labels[i]) {
860 hres = E_OUTOFMEMORY;
861 break;
864 if(!case_iter->expr)
865 break;
867 for(expr_iter = case_iter->expr; expr_iter; expr_iter = expr_iter->next) {
868 hres = compile_expression(ctx, expr_iter);
869 if(FAILED(hres))
870 break;
872 hres = push_instr_addr(ctx, OP_case, case_labels[i]);
873 if(FAILED(hres))
874 break;
876 if(!emit_catch_jmp(ctx, 0, case_labels[i])) {
877 hres = E_OUTOFMEMORY;
878 break;
883 if(FAILED(hres)) {
884 heap_free(case_labels);
885 return hres;
888 hres = push_instr_uint(ctx, OP_pop, 1);
889 if(FAILED(hres)) {
890 heap_free(case_labels);
891 return hres;
894 hres = push_instr_addr(ctx, OP_jmp, case_iter ? case_labels[i] : end_label);
895 if(FAILED(hres)) {
896 heap_free(case_labels);
897 return hres;
900 for(case_iter = stat->case_clausules, i=0; case_iter; case_iter = case_iter->next, i++) {
901 label_set_addr(ctx, case_labels[i]);
902 hres = compile_statement(ctx, NULL, case_iter->stat);
903 if(FAILED(hres))
904 break;
906 if(!case_iter->next)
907 break;
909 hres = push_instr_addr(ctx, OP_jmp, end_label);
910 if(FAILED(hres))
911 break;
914 heap_free(case_labels);
915 if(FAILED(hres))
916 return hres;
918 label_set_addr(ctx, end_label);
919 return S_OK;
922 static HRESULT compile_assignment(compile_ctx_t *ctx, member_expression_t *member_expr, expression_t *value_expr, BOOL is_set)
924 unsigned args_cnt;
925 vbsop_t op;
926 HRESULT hres;
928 if(member_expr->obj_expr) {
929 hres = compile_expression(ctx, member_expr->obj_expr);
930 if(FAILED(hres))
931 return hres;
933 op = is_set ? OP_set_member : OP_assign_member;
934 }else {
935 op = is_set ? OP_set_ident : OP_assign_ident;
938 hres = compile_expression(ctx, value_expr);
939 if(FAILED(hres))
940 return hres;
942 hres = compile_args(ctx, member_expr->args, &args_cnt);
943 if(FAILED(hres))
944 return hres;
946 hres = push_instr_bstr_uint(ctx, op, member_expr->identifier, args_cnt);
947 if(FAILED(hres))
948 return hres;
950 if(!emit_catch(ctx, 0))
951 return E_OUTOFMEMORY;
953 return S_OK;
956 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat, BOOL is_set)
958 return compile_assignment(ctx, stat->member_expr, stat->value_expr, is_set);
961 static HRESULT compile_call_statement(compile_ctx_t *ctx, call_statement_t *stat)
963 HRESULT hres;
965 /* It's challenging for parser to distinguish parameterized assignment with one argument from call
966 * with equality expression argument, so we do it in compiler. */
967 if(!stat->is_strict && stat->expr->args && !stat->expr->args->next && stat->expr->args->type == EXPR_EQUAL) {
968 binary_expression_t *eqexpr = (binary_expression_t*)stat->expr->args;
970 if(eqexpr->left->type == EXPR_BRACKETS) {
971 member_expression_t new_member = *stat->expr;
973 WARN("converting call expr to assign expr\n");
975 new_member.args = ((unary_expression_t*)eqexpr->left)->subexpr;
976 return compile_assignment(ctx, &new_member, eqexpr->right, FALSE);
980 hres = compile_member_expression(ctx, stat->expr, FALSE);
981 if(FAILED(hres))
982 return hres;
984 if(!emit_catch(ctx, 0))
985 return E_OUTOFMEMORY;
987 return S_OK;
990 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
992 dim_decl_t *dim_decl;
994 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
995 if(!strcmpiW(dim_decl->name, name))
996 return TRUE;
999 return FALSE;
1002 static BOOL lookup_args_name(compile_ctx_t *ctx, const WCHAR *name)
1004 unsigned i;
1006 for(i = 0; i < ctx->func->arg_cnt; i++) {
1007 if(!strcmpiW(ctx->func->args[i].name, name))
1008 return TRUE;
1011 return FALSE;
1014 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
1016 dim_decl_t *dim_decl = stat->dim_decls;
1018 while(1) {
1019 if(lookup_dim_decls(ctx, dim_decl->name) || lookup_args_name(ctx, dim_decl->name)
1020 || lookup_const_decls(ctx, dim_decl->name, FALSE)) {
1021 FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
1022 return E_FAIL;
1025 ctx->func->var_cnt++;
1027 if(dim_decl->is_array) {
1028 HRESULT hres = push_instr_bstr_uint(ctx, OP_dim, dim_decl->name, ctx->func->array_cnt++);
1029 if(FAILED(hres))
1030 return hres;
1032 if(!emit_catch(ctx, 0))
1033 return E_OUTOFMEMORY;
1036 if(!dim_decl->next)
1037 break;
1038 dim_decl = dim_decl->next;
1041 if(ctx->dim_decls_tail)
1042 ctx->dim_decls_tail->next = stat->dim_decls;
1043 else
1044 ctx->dim_decls = stat->dim_decls;
1045 ctx->dim_decls_tail = dim_decl;
1046 return S_OK;
1049 static HRESULT compile_const_statement(compile_ctx_t *ctx, const_statement_t *stat)
1051 const_decl_t *decl, *next_decl = stat->decls;
1053 do {
1054 decl = next_decl;
1056 if(lookup_const_decls(ctx, decl->name, FALSE) || lookup_args_name(ctx, decl->name)
1057 || lookup_dim_decls(ctx, decl->name)) {
1058 FIXME("%s redefined\n", debugstr_w(decl->name));
1059 return E_FAIL;
1062 if(ctx->func->type == FUNC_GLOBAL) {
1063 HRESULT hres;
1065 hres = compile_expression(ctx, decl->value_expr);
1066 if(FAILED(hres))
1067 return hres;
1069 hres = push_instr_bstr(ctx, OP_const, decl->name);
1070 if(FAILED(hres))
1071 return hres;
1073 if(!emit_catch(ctx, 0))
1074 return E_OUTOFMEMORY;
1077 next_decl = decl->next;
1078 decl->next = ctx->const_decls;
1079 ctx->const_decls = decl;
1080 } while(next_decl);
1082 return S_OK;
1085 static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
1087 if(ctx->func != &ctx->code->main_code) {
1088 FIXME("Function is not in the global code\n");
1089 return E_FAIL;
1092 stat->func_decl->next = ctx->func_decls;
1093 ctx->func_decls = stat->func_decl;
1094 return S_OK;
1097 static HRESULT compile_exitdo_statement(compile_ctx_t *ctx)
1099 statement_ctx_t *iter;
1100 unsigned pop_cnt = 0;
1102 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1103 pop_cnt += iter->stack_use;
1104 if(iter->while_end_label)
1105 break;
1107 if(!iter) {
1108 FIXME("Exit Do outside Do Loop\n");
1109 return E_FAIL;
1112 if(pop_cnt) {
1113 HRESULT hres;
1115 hres = push_instr_uint(ctx, OP_pop, pop_cnt);
1116 if(FAILED(hres))
1117 return hres;
1120 return push_instr_addr(ctx, OP_jmp, iter->while_end_label);
1123 static HRESULT compile_exitfor_statement(compile_ctx_t *ctx)
1125 statement_ctx_t *iter;
1126 unsigned pop_cnt = 0;
1128 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1129 pop_cnt += iter->stack_use;
1130 if(iter->for_end_label)
1131 break;
1133 if(!iter) {
1134 FIXME("Exit For outside For loop\n");
1135 return E_FAIL;
1138 if(pop_cnt) {
1139 HRESULT hres;
1141 hres = push_instr_uint(ctx, OP_pop, pop_cnt);
1142 if(FAILED(hres))
1143 return hres;
1146 return push_instr_addr(ctx, OP_jmp, iter->for_end_label);
1149 static HRESULT exit_label(compile_ctx_t *ctx, unsigned jmp_label)
1151 unsigned pop_cnt = stack_offset(ctx);
1153 if(pop_cnt) {
1154 HRESULT hres;
1156 hres = push_instr_uint(ctx, OP_pop, pop_cnt);
1157 if(FAILED(hres))
1158 return hres;
1161 return push_instr_addr(ctx, OP_jmp, jmp_label);
1164 static HRESULT compile_exitsub_statement(compile_ctx_t *ctx)
1166 if(!ctx->sub_end_label) {
1167 FIXME("Exit Sub outside Sub?\n");
1168 return E_FAIL;
1171 return exit_label(ctx, ctx->sub_end_label);
1174 static HRESULT compile_exitfunc_statement(compile_ctx_t *ctx)
1176 if(!ctx->func_end_label) {
1177 FIXME("Exit Function outside Function?\n");
1178 return E_FAIL;
1181 return exit_label(ctx, ctx->func_end_label);
1184 static HRESULT compile_exitprop_statement(compile_ctx_t *ctx)
1186 if(!ctx->prop_end_label) {
1187 FIXME("Exit Property outside Property?\n");
1188 return E_FAIL;
1191 return exit_label(ctx, ctx->prop_end_label);
1194 static HRESULT compile_onerror_statement(compile_ctx_t *ctx, onerror_statement_t *stat)
1196 return push_instr_int(ctx, OP_errmode, stat->resume_next);
1199 static HRESULT compile_statement(compile_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1201 HRESULT hres;
1203 if(stat_ctx) {
1204 stat_ctx->next = ctx->stat_ctx;
1205 ctx->stat_ctx = stat_ctx;
1208 while(stat) {
1209 switch(stat->type) {
1210 case STAT_ASSIGN:
1211 hres = compile_assign_statement(ctx, (assign_statement_t*)stat, FALSE);
1212 break;
1213 case STAT_CALL:
1214 hres = compile_call_statement(ctx, (call_statement_t*)stat);
1215 break;
1216 case STAT_CONST:
1217 hres = compile_const_statement(ctx, (const_statement_t*)stat);
1218 break;
1219 case STAT_DIM:
1220 hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
1221 break;
1222 case STAT_DOWHILE:
1223 case STAT_DOUNTIL:
1224 hres = compile_dowhile_statement(ctx, (while_statement_t*)stat);
1225 break;
1226 case STAT_EXITDO:
1227 hres = compile_exitdo_statement(ctx);
1228 break;
1229 case STAT_EXITFOR:
1230 hres = compile_exitfor_statement(ctx);
1231 break;
1232 case STAT_EXITFUNC:
1233 hres = compile_exitfunc_statement(ctx);
1234 break;
1235 case STAT_EXITPROP:
1236 hres = compile_exitprop_statement(ctx);
1237 break;
1238 case STAT_EXITSUB:
1239 hres = compile_exitsub_statement(ctx);
1240 break;
1241 case STAT_FOREACH:
1242 hres = compile_foreach_statement(ctx, (foreach_statement_t*)stat);
1243 break;
1244 case STAT_FORTO:
1245 hres = compile_forto_statement(ctx, (forto_statement_t*)stat);
1246 break;
1247 case STAT_FUNC:
1248 hres = compile_function_statement(ctx, (function_statement_t*)stat);
1249 break;
1250 case STAT_IF:
1251 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1252 break;
1253 case STAT_ONERROR:
1254 hres = compile_onerror_statement(ctx, (onerror_statement_t*)stat);
1255 break;
1256 case STAT_SELECT:
1257 hres = compile_select_statement(ctx, (select_statement_t*)stat);
1258 break;
1259 case STAT_SET:
1260 hres = compile_assign_statement(ctx, (assign_statement_t*)stat, TRUE);
1261 break;
1262 case STAT_STOP:
1263 hres = push_instr(ctx, OP_stop) ? S_OK : E_OUTOFMEMORY;
1264 break;
1265 case STAT_UNTIL:
1266 case STAT_WHILE:
1267 case STAT_WHILELOOP:
1268 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1269 break;
1270 default:
1271 FIXME("Unimplemented statement type %d\n", stat->type);
1272 hres = E_NOTIMPL;
1275 if(FAILED(hres))
1276 return hres;
1277 stat = stat->next;
1280 if(stat_ctx) {
1281 assert(ctx->stat_ctx == stat_ctx);
1282 ctx->stat_ctx = stat_ctx->next;
1285 return S_OK;
1288 static void resolve_labels(compile_ctx_t *ctx, unsigned off)
1290 instr_t *instr;
1292 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
1293 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
1294 assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
1295 instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
1297 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
1300 ctx->labels_cnt = 0;
1303 static HRESULT fill_array_desc(compile_ctx_t *ctx, dim_decl_t *dim_decl, array_desc_t *array_desc)
1305 unsigned dim_cnt = 0, i;
1306 dim_list_t *iter;
1308 for(iter = dim_decl->dims; iter; iter = iter->next)
1309 dim_cnt++;
1311 array_desc->bounds = compiler_alloc(ctx->code, dim_cnt * sizeof(SAFEARRAYBOUND));
1312 if(!array_desc->bounds)
1313 return E_OUTOFMEMORY;
1315 array_desc->dim_cnt = dim_cnt;
1317 for(iter = dim_decl->dims, i=0; iter; iter = iter->next, i++) {
1318 array_desc->bounds[i].cElements = iter->val+1;
1319 array_desc->bounds[i].lLbound = 0;
1322 return S_OK;
1325 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
1327 HRESULT hres;
1329 func->code_off = ctx->instr_cnt;
1331 ctx->sub_end_label = 0;
1332 ctx->func_end_label = 0;
1333 ctx->prop_end_label = 0;
1335 switch(func->type) {
1336 case FUNC_FUNCTION:
1337 ctx->func_end_label = alloc_label(ctx);
1338 if(!ctx->func_end_label)
1339 return E_OUTOFMEMORY;
1340 break;
1341 case FUNC_SUB:
1342 ctx->sub_end_label = alloc_label(ctx);
1343 if(!ctx->sub_end_label)
1344 return E_OUTOFMEMORY;
1345 break;
1346 case FUNC_PROPGET:
1347 case FUNC_PROPLET:
1348 case FUNC_PROPSET:
1349 case FUNC_DEFGET:
1350 ctx->prop_end_label = alloc_label(ctx);
1351 if(!ctx->prop_end_label)
1352 return E_OUTOFMEMORY;
1353 break;
1354 case FUNC_GLOBAL:
1355 break;
1358 ctx->func = func;
1359 ctx->dim_decls = ctx->dim_decls_tail = NULL;
1360 ctx->const_decls = NULL;
1361 hres = compile_statement(ctx, NULL, stat);
1362 ctx->func = NULL;
1363 if(FAILED(hres))
1364 return hres;
1366 if(ctx->sub_end_label)
1367 label_set_addr(ctx, ctx->sub_end_label);
1368 if(ctx->func_end_label)
1369 label_set_addr(ctx, ctx->func_end_label);
1370 if(ctx->prop_end_label)
1371 label_set_addr(ctx, ctx->prop_end_label);
1373 if(!push_instr(ctx, OP_ret))
1374 return E_OUTOFMEMORY;
1376 resolve_labels(ctx, func->code_off);
1378 if(func->var_cnt) {
1379 dim_decl_t *dim_decl;
1381 if(func->type == FUNC_GLOBAL) {
1382 dynamic_var_t *new_var;
1384 func->var_cnt = 0;
1386 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
1387 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
1388 if(!new_var)
1389 return E_OUTOFMEMORY;
1391 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
1392 if(!new_var->name)
1393 return E_OUTOFMEMORY;
1395 V_VT(&new_var->v) = VT_EMPTY;
1396 new_var->is_const = FALSE;
1398 new_var->next = ctx->global_vars;
1399 ctx->global_vars = new_var;
1401 }else {
1402 unsigned i;
1404 func->vars = compiler_alloc(ctx->code, func->var_cnt * sizeof(var_desc_t));
1405 if(!func->vars)
1406 return E_OUTOFMEMORY;
1408 for(dim_decl = ctx->dim_decls, i=0; dim_decl; dim_decl = dim_decl->next, i++) {
1409 func->vars[i].name = compiler_alloc_string(ctx->code, dim_decl->name);
1410 if(!func->vars[i].name)
1411 return E_OUTOFMEMORY;
1414 assert(i == func->var_cnt);
1418 if(func->array_cnt) {
1419 unsigned array_id = 0;
1420 dim_decl_t *dim_decl;
1422 func->array_descs = compiler_alloc(ctx->code, func->array_cnt * sizeof(array_desc_t));
1423 if(!func->array_descs)
1424 return E_OUTOFMEMORY;
1426 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
1427 if(dim_decl->is_array) {
1428 hres = fill_array_desc(ctx, dim_decl, func->array_descs + array_id++);
1429 if(FAILED(hres))
1430 return hres;
1434 assert(array_id == func->array_cnt);
1437 return S_OK;
1440 static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
1442 function_t *iter;
1444 for(iter = ctx->funcs; iter; iter = iter->next) {
1445 if(!strcmpiW(iter->name, name))
1446 return TRUE;
1449 return FALSE;
1452 static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
1454 function_t *func;
1455 HRESULT hres;
1457 if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name) || lookup_const_decls(ctx, decl->name, FALSE)) {
1458 FIXME("%s: redefinition\n", debugstr_w(decl->name));
1459 return E_FAIL;
1462 func = compiler_alloc(ctx->code, sizeof(*func));
1463 if(!func)
1464 return E_OUTOFMEMORY;
1466 func->name = compiler_alloc_string(ctx->code, decl->name);
1467 if(!func->name)
1468 return E_OUTOFMEMORY;
1470 func->vars = NULL;
1471 func->var_cnt = 0;
1472 func->array_cnt = 0;
1473 func->code_ctx = ctx->code;
1474 func->type = decl->type;
1475 func->is_public = decl->is_public;
1477 func->arg_cnt = 0;
1478 if(decl->args) {
1479 arg_decl_t *arg;
1480 unsigned i;
1482 for(arg = decl->args; arg; arg = arg->next)
1483 func->arg_cnt++;
1485 func->args = compiler_alloc(ctx->code, func->arg_cnt * sizeof(arg_desc_t));
1486 if(!func->args)
1487 return E_OUTOFMEMORY;
1489 for(i = 0, arg = decl->args; arg; arg = arg->next, i++) {
1490 func->args[i].name = compiler_alloc_string(ctx->code, arg->name);
1491 if(!func->args[i].name)
1492 return E_OUTOFMEMORY;
1493 func->args[i].by_ref = arg->by_ref;
1495 }else {
1496 func->args = NULL;
1499 hres = compile_func(ctx, decl->body, func);
1500 if(FAILED(hres))
1501 return hres;
1503 *ret = func;
1504 return S_OK;
1507 static BOOL lookup_class_name(compile_ctx_t *ctx, const WCHAR *name)
1509 class_desc_t *iter;
1511 for(iter = ctx->classes; iter; iter = iter->next) {
1512 if(!strcmpiW(iter->name, name))
1513 return TRUE;
1516 return FALSE;
1519 static HRESULT create_class_funcprop(compile_ctx_t *ctx, function_decl_t *func_decl, vbdisp_funcprop_desc_t *desc)
1521 vbdisp_invoke_type_t invoke_type;
1522 function_decl_t *funcprop_decl;
1523 HRESULT hres;
1525 desc->name = compiler_alloc_string(ctx->code, func_decl->name);
1526 if(!desc->name)
1527 return E_OUTOFMEMORY;
1529 for(funcprop_decl = func_decl; funcprop_decl; funcprop_decl = funcprop_decl->next_prop_func) {
1530 switch(funcprop_decl->type) {
1531 case FUNC_FUNCTION:
1532 case FUNC_SUB:
1533 case FUNC_PROPGET:
1534 case FUNC_DEFGET:
1535 invoke_type = VBDISP_CALLGET;
1536 break;
1537 case FUNC_PROPLET:
1538 invoke_type = VBDISP_LET;
1539 break;
1540 case FUNC_PROPSET:
1541 invoke_type = VBDISP_SET;
1542 break;
1543 DEFAULT_UNREACHABLE;
1546 assert(!desc->entries[invoke_type]);
1548 if(funcprop_decl->is_public)
1549 desc->is_public = TRUE;
1551 hres = create_function(ctx, funcprop_decl, desc->entries+invoke_type);
1552 if(FAILED(hres))
1553 return hres;
1556 return S_OK;
1559 static BOOL lookup_class_funcs(class_desc_t *class_desc, const WCHAR *name)
1561 unsigned i;
1563 for(i=0; i < class_desc->func_cnt; i++) {
1564 if(class_desc->funcs[i].name && !strcmpiW(class_desc->funcs[i].name, name))
1565 return TRUE;
1568 return FALSE;
1571 static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl)
1573 function_decl_t *func_decl, *func_prop_decl;
1574 class_desc_t *class_desc;
1575 dim_decl_t *prop_decl;
1576 unsigned i;
1577 HRESULT hres;
1579 static const WCHAR class_initializeW[] = {'c','l','a','s','s','_','i','n','i','t','i','a','l','i','z','e',0};
1580 static const WCHAR class_terminateW[] = {'c','l','a','s','s','_','t','e','r','m','i','n','a','t','e',0};
1582 if(lookup_dim_decls(ctx, class_decl->name) || lookup_funcs_name(ctx, class_decl->name)
1583 || lookup_const_decls(ctx, class_decl->name, FALSE) || lookup_class_name(ctx, class_decl->name)) {
1584 FIXME("%s: redefinition\n", debugstr_w(class_decl->name));
1585 return E_FAIL;
1588 class_desc = compiler_alloc_zero(ctx->code, sizeof(*class_desc));
1589 if(!class_desc)
1590 return E_OUTOFMEMORY;
1592 class_desc->name = compiler_alloc_string(ctx->code, class_decl->name);
1593 if(!class_desc->name)
1594 return E_OUTOFMEMORY;
1596 class_desc->func_cnt = 1; /* always allocate slot for default getter */
1598 for(func_decl = class_decl->funcs; func_decl; func_decl = func_decl->next) {
1599 for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1600 if(func_prop_decl->type == FUNC_DEFGET)
1601 break;
1603 if(!func_prop_decl)
1604 class_desc->func_cnt++;
1607 class_desc->funcs = compiler_alloc(ctx->code, class_desc->func_cnt*sizeof(*class_desc->funcs));
1608 if(!class_desc->funcs)
1609 return E_OUTOFMEMORY;
1610 memset(class_desc->funcs, 0, class_desc->func_cnt*sizeof(*class_desc->funcs));
1612 for(func_decl = class_decl->funcs, i=1; func_decl; func_decl = func_decl->next, i++) {
1613 for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1614 if(func_prop_decl->type == FUNC_DEFGET) {
1615 i--;
1616 break;
1620 if(!strcmpiW(class_initializeW, func_decl->name)) {
1621 if(func_decl->type != FUNC_SUB) {
1622 FIXME("class initializer is not sub\n");
1623 return E_FAIL;
1626 class_desc->class_initialize_id = i;
1627 }else if(!strcmpiW(class_terminateW, func_decl->name)) {
1628 if(func_decl->type != FUNC_SUB) {
1629 FIXME("class terminator is not sub\n");
1630 return E_FAIL;
1633 class_desc->class_terminate_id = i;
1636 hres = create_class_funcprop(ctx, func_decl, class_desc->funcs + (func_prop_decl ? 0 : i));
1637 if(FAILED(hres))
1638 return hres;
1641 for(prop_decl = class_decl->props; prop_decl; prop_decl = prop_decl->next)
1642 class_desc->prop_cnt++;
1644 class_desc->props = compiler_alloc(ctx->code, class_desc->prop_cnt*sizeof(*class_desc->props));
1645 if(!class_desc->props)
1646 return E_OUTOFMEMORY;
1648 for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next, i++) {
1649 if(lookup_class_funcs(class_desc, prop_decl->name)) {
1650 FIXME("Property %s redefined\n", debugstr_w(prop_decl->name));
1651 return E_FAIL;
1654 class_desc->props[i].name = compiler_alloc_string(ctx->code, prop_decl->name);
1655 if(!class_desc->props[i].name)
1656 return E_OUTOFMEMORY;
1658 class_desc->props[i].is_public = prop_decl->is_public;
1659 class_desc->props[i].is_array = prop_decl->is_array;
1661 if(prop_decl->is_array)
1662 class_desc->array_cnt++;
1665 if(class_desc->array_cnt) {
1666 class_desc->array_descs = compiler_alloc(ctx->code, class_desc->array_cnt*sizeof(*class_desc->array_descs));
1667 if(!class_desc->array_descs)
1668 return E_OUTOFMEMORY;
1670 for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next) {
1671 if(prop_decl->is_array) {
1672 hres = fill_array_desc(ctx, prop_decl, class_desc->array_descs + i++);
1673 if(FAILED(hres))
1674 return hres;
1679 class_desc->next = ctx->classes;
1680 ctx->classes = class_desc;
1681 return S_OK;
1684 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
1686 class_desc_t *class;
1687 dynamic_var_t *var;
1688 function_t *func;
1690 for(var = script->global_vars; var; var = var->next) {
1691 if(!strcmpiW(var->name, identifier))
1692 return TRUE;
1695 for(func = script->global_funcs; func; func = func->next) {
1696 if(!strcmpiW(func->name, identifier))
1697 return TRUE;
1700 for(class = script->classes; class; class = class->next) {
1701 if(!strcmpiW(class->name, identifier))
1702 return TRUE;
1705 return FALSE;
1708 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
1710 class_desc_t *class;
1711 dynamic_var_t *var;
1712 function_t *func;
1714 for(var = ctx->global_vars; var; var = var->next) {
1715 if(lookup_script_identifier(script, var->name)) {
1716 FIXME("%s: redefined\n", debugstr_w(var->name));
1717 return E_FAIL;
1721 for(func = ctx->funcs; func; func = func->next) {
1722 if(lookup_script_identifier(script, func->name)) {
1723 FIXME("%s: redefined\n", debugstr_w(func->name));
1724 return E_FAIL;
1728 for(class = ctx->classes; class; class = class->next) {
1729 if(lookup_script_identifier(script, class->name)) {
1730 FIXME("%s: redefined\n", debugstr_w(class->name));
1731 return E_FAIL;
1735 return S_OK;
1738 void release_vbscode(vbscode_t *code)
1740 unsigned i;
1742 list_remove(&code->entry);
1744 for(i=0; i < code->bstr_cnt; i++)
1745 SysFreeString(code->bstr_pool[i]);
1747 heap_pool_free(&code->heap);
1749 heap_free(code->bstr_pool);
1750 heap_free(code->source);
1751 heap_free(code->instrs);
1752 heap_free(code);
1755 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
1757 vbscode_t *ret;
1759 ret = heap_alloc(sizeof(*ret));
1760 if(!ret)
1761 return NULL;
1763 ret->source = heap_strdupW(source);
1764 if(!ret->source) {
1765 heap_free(ret);
1766 return NULL;
1769 ret->instrs = heap_alloc(32*sizeof(instr_t));
1770 if(!ret->instrs) {
1771 release_vbscode(ret);
1772 return NULL;
1775 ctx->instr_cnt = 1;
1776 ctx->instr_size = 32;
1777 heap_pool_init(&ret->heap);
1779 ret->option_explicit = ctx->parser.option_explicit;
1781 ret->bstr_pool = NULL;
1782 ret->bstr_pool_size = 0;
1783 ret->bstr_cnt = 0;
1784 ret->pending_exec = FALSE;
1786 ret->main_code.type = FUNC_GLOBAL;
1787 ret->main_code.name = NULL;
1788 ret->main_code.code_ctx = ret;
1789 ret->main_code.vars = NULL;
1790 ret->main_code.var_cnt = 0;
1791 ret->main_code.array_cnt = 0;
1792 ret->main_code.arg_cnt = 0;
1793 ret->main_code.args = NULL;
1795 list_init(&ret->entry);
1796 return ret;
1799 static void release_compiler(compile_ctx_t *ctx)
1801 parser_release(&ctx->parser);
1802 heap_free(ctx->labels);
1803 if(ctx->code)
1804 release_vbscode(ctx->code);
1807 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, const WCHAR *delimiter, vbscode_t **ret)
1809 function_t *new_func;
1810 function_decl_t *func_decl;
1811 class_decl_t *class_decl;
1812 compile_ctx_t ctx;
1813 vbscode_t *code;
1814 HRESULT hres;
1816 hres = parse_script(&ctx.parser, src, delimiter);
1817 if(FAILED(hres))
1818 return hres;
1820 code = ctx.code = alloc_vbscode(&ctx, src);
1821 if(!ctx.code)
1822 return E_OUTOFMEMORY;
1824 ctx.funcs = NULL;
1825 ctx.func_decls = NULL;
1826 ctx.global_vars = NULL;
1827 ctx.classes = NULL;
1828 ctx.labels = NULL;
1829 ctx.global_consts = NULL;
1830 ctx.stat_ctx = NULL;
1831 ctx.labels_cnt = ctx.labels_size = 0;
1833 hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->main_code);
1834 if(FAILED(hres)) {
1835 release_compiler(&ctx);
1836 return hres;
1839 ctx.global_consts = ctx.const_decls;
1841 for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
1842 hres = create_function(&ctx, func_decl, &new_func);
1843 if(FAILED(hres)) {
1844 release_compiler(&ctx);
1845 return hres;
1848 new_func->next = ctx.funcs;
1849 ctx.funcs = new_func;
1852 for(class_decl = ctx.parser.class_decls; class_decl; class_decl = class_decl->next) {
1853 hres = compile_class(&ctx, class_decl);
1854 if(FAILED(hres)) {
1855 release_compiler(&ctx);
1856 return hres;
1860 hres = check_script_collisions(&ctx, script);
1861 if(FAILED(hres)) {
1862 release_compiler(&ctx);
1863 return hres;
1866 if(ctx.global_vars) {
1867 dynamic_var_t *var;
1869 for(var = ctx.global_vars; var->next; var = var->next);
1871 var->next = script->global_vars;
1872 script->global_vars = ctx.global_vars;
1875 if(ctx.funcs) {
1876 for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);
1878 new_func->next = script->global_funcs;
1879 script->global_funcs = ctx.funcs;
1882 if(ctx.classes) {
1883 class_desc_t *class = ctx.classes;
1885 while(1) {
1886 class->ctx = script;
1887 if(!class->next)
1888 break;
1889 class = class->next;
1892 class->next = script->classes;
1893 script->classes = ctx.classes;
1896 if(TRACE_ON(vbscript_disas))
1897 dump_code(&ctx);
1899 ctx.code = NULL;
1900 release_compiler(&ctx);
1902 list_add_tail(&script->code_list, &code->entry);
1903 *ret = code;
1904 return S_OK;