mfplat/tests: Add a separate test function for MFGetPlaneSize().
[wine.git] / dlls / vbscript / compile.c
blob50c68a6e6b5ccf5463b4b3fd6c8a359a649efd22
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;
35 unsigned with_stack_offset;
37 struct _statement_ctx_t *next;
38 } statement_ctx_t;
40 typedef struct {
41 parser_ctx_t parser;
43 unsigned instr_cnt;
44 unsigned instr_size;
45 vbscode_t *code;
47 unsigned loc;
48 statement_ctx_t *stat_ctx;
50 unsigned *labels;
51 unsigned labels_size;
52 unsigned labels_cnt;
54 unsigned sub_end_label;
55 unsigned func_end_label;
56 unsigned prop_end_label;
58 dim_decl_t *dim_decls;
59 dim_decl_t *dim_decls_tail;
61 const_decl_t *const_decls;
62 const_decl_t *global_consts;
64 function_t *func;
65 function_decl_t *func_decls;
66 } compile_ctx_t;
68 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
69 static HRESULT compile_statement(compile_ctx_t*,statement_ctx_t*,statement_t*);
71 static const struct {
72 const char *op_str;
73 instr_arg_type_t arg1_type;
74 instr_arg_type_t arg2_type;
75 } instr_info[] = {
76 #define X(n,a,b,c) {#n,b,c},
77 OP_LIST
78 #undef X
81 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
83 switch(type) {
84 case ARG_STR:
85 case ARG_BSTR:
86 TRACE_(vbscript_disas)("\t%s", debugstr_w(arg->str));
87 break;
88 case ARG_INT:
89 TRACE_(vbscript_disas)("\t%d", arg->uint);
90 break;
91 case ARG_UINT:
92 case ARG_ADDR:
93 TRACE_(vbscript_disas)("\t%u", arg->uint);
94 break;
95 case ARG_DATE:
96 case ARG_DOUBLE:
97 TRACE_(vbscript_disas)("\t%lf", *arg->dbl);
98 break;
99 case ARG_NONE:
100 break;
101 DEFAULT_UNREACHABLE;
105 static void dump_code(compile_ctx_t *ctx)
107 instr_t *instr;
109 for(instr = ctx->code->instrs+1; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
110 assert(instr->op < OP_LAST);
111 TRACE_(vbscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
112 dump_instr_arg(instr_info[instr->op].arg1_type, &instr->arg1);
113 dump_instr_arg(instr_info[instr->op].arg2_type, &instr->arg2);
114 TRACE_(vbscript_disas)("\n");
118 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
120 return heap_pool_alloc(&vbscode->heap, size);
123 static inline void *compiler_alloc_zero(vbscode_t *vbscode, size_t size)
125 void *ret;
127 ret = heap_pool_alloc(&vbscode->heap, size);
128 if(ret)
129 memset(ret, 0, size);
130 return ret;
133 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
135 size_t size;
136 WCHAR *ret;
138 size = (lstrlenW(str)+1)*sizeof(WCHAR);
139 ret = compiler_alloc(vbscode, size);
140 if(ret)
141 memcpy(ret, str, size);
142 return ret;
145 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
147 assert(id < ctx->instr_cnt);
148 return ctx->code->instrs + id;
151 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
153 assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
155 if(ctx->instr_size == ctx->instr_cnt) {
156 instr_t *new_instr;
158 new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
159 if(!new_instr)
160 return 0;
162 ctx->code->instrs = new_instr;
163 ctx->instr_size *= 2;
166 ctx->code->instrs[ctx->instr_cnt].op = op;
167 ctx->code->instrs[ctx->instr_cnt].loc = ctx->loc;
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 HRESULT push_instr_date(compile_ctx_t *ctx, vbsop_t op, DATE arg)
244 unsigned instr;
245 DATE *d;
247 d = compiler_alloc(ctx->code, sizeof(DATE));
248 if(!d)
249 return E_OUTOFMEMORY;
251 instr = push_instr(ctx, op);
252 if(!instr)
253 return E_OUTOFMEMORY;
255 *d = arg;
256 instr_ptr(ctx, instr)->arg1.date = d;
257 return S_OK;
260 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
262 if(!ctx->code->bstr_pool_size) {
263 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
264 if(!ctx->code->bstr_pool)
265 return NULL;
266 ctx->code->bstr_pool_size = 8;
267 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
268 BSTR *new_pool;
270 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
271 if(!new_pool)
272 return NULL;
274 ctx->code->bstr_pool = new_pool;
275 ctx->code->bstr_pool_size *= 2;
278 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
279 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
280 return NULL;
282 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
285 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
287 unsigned instr;
288 BSTR bstr;
290 bstr = alloc_bstr_arg(ctx, arg);
291 if(!bstr)
292 return E_OUTOFMEMORY;
294 instr = push_instr(ctx, op);
295 if(!instr)
296 return E_OUTOFMEMORY;
298 instr_ptr(ctx, instr)->arg1.bstr = bstr;
299 return S_OK;
302 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
304 unsigned instr;
305 BSTR bstr;
307 bstr = alloc_bstr_arg(ctx, arg1);
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.bstr = bstr;
316 instr_ptr(ctx, instr)->arg2.uint = arg2;
317 return S_OK;
320 static HRESULT push_instr_uint_bstr(compile_ctx_t *ctx, vbsop_t op, unsigned arg1, const WCHAR *arg2)
322 unsigned instr;
323 BSTR bstr;
325 bstr = alloc_bstr_arg(ctx, arg2);
326 if(!bstr)
327 return E_OUTOFMEMORY;
329 instr = push_instr(ctx, op);
330 if(!instr)
331 return E_OUTOFMEMORY;
333 instr_ptr(ctx, instr)->arg1.uint = arg1;
334 instr_ptr(ctx, instr)->arg2.bstr = bstr;
335 return S_OK;
338 #define LABEL_FLAG 0x80000000
340 static unsigned alloc_label(compile_ctx_t *ctx)
342 if(!ctx->labels_size) {
343 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
344 if(!ctx->labels)
345 return 0;
346 ctx->labels_size = 8;
347 }else if(ctx->labels_size == ctx->labels_cnt) {
348 unsigned *new_labels;
350 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
351 if(!new_labels)
352 return 0;
354 ctx->labels = new_labels;
355 ctx->labels_size *= 2;
358 return ctx->labels_cnt++ | LABEL_FLAG;
361 static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
363 assert(label & LABEL_FLAG);
364 ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
367 static inline unsigned stack_offset(compile_ctx_t *ctx)
369 statement_ctx_t *iter;
370 unsigned ret = 0;
372 for(iter = ctx->stat_ctx; iter; iter = iter->next)
373 ret += iter->stack_use;
375 return ret;
378 static BOOL emit_catch_jmp(compile_ctx_t *ctx, unsigned stack_off, unsigned code_off)
380 unsigned code;
382 code = push_instr(ctx, OP_catch);
383 if(!code)
384 return FALSE;
386 instr_ptr(ctx, code)->arg1.uint = code_off;
387 instr_ptr(ctx, code)->arg2.uint = stack_off + stack_offset(ctx);
388 return TRUE;
391 static inline BOOL emit_catch(compile_ctx_t *ctx, unsigned off)
393 return emit_catch_jmp(ctx, off, ctx->instr_cnt);
396 static HRESULT compile_error(script_ctx_t *ctx, compile_ctx_t *compiler, HRESULT error)
398 if(error == SCRIPT_E_REPORTED)
399 return error;
401 clear_ei(&ctx->ei);
402 ctx->ei.scode = error = map_hres(error);
403 ctx->ei.bstrSource = get_vbscript_string(VBS_COMPILE_ERROR);
404 ctx->ei.bstrDescription = get_vbscript_error_string(error);
405 return report_script_error(ctx, compiler->code, compiler->loc);
408 static expression_t *lookup_const_decls(compile_ctx_t *ctx, const WCHAR *name, BOOL lookup_global)
410 const_decl_t *decl;
412 for(decl = ctx->const_decls; decl; decl = decl->next) {
413 if(!wcsicmp(decl->name, name))
414 return decl->value_expr;
417 if(!lookup_global)
418 return NULL;
420 for(decl = ctx->global_consts; decl; decl = decl->next) {
421 if(!wcsicmp(decl->name, name))
422 return decl->value_expr;
425 return NULL;
428 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
430 unsigned arg_cnt = 0;
431 HRESULT hres;
433 while(args) {
434 hres = compile_expression(ctx, args);
435 if(FAILED(hres))
436 return hres;
438 if(args->type == EXPR_BRACKETS && !push_instr(ctx, OP_deref))
439 return E_OUTOFMEMORY;
441 arg_cnt++;
442 args = args->next;
445 *ret = arg_cnt;
446 return S_OK;
449 static HRESULT compile_member_call_expression(compile_ctx_t *ctx, member_expression_t *expr,
450 unsigned arg_cnt, BOOL ret_val)
452 HRESULT hres;
454 if(ret_val && !arg_cnt) {
455 expression_t *const_expr;
457 const_expr = lookup_const_decls(ctx, expr->identifier, TRUE);
458 if(const_expr)
459 return compile_expression(ctx, const_expr);
462 if(expr->obj_expr) {
463 hres = compile_expression(ctx, expr->obj_expr);
464 if(FAILED(hres))
465 return hres;
467 hres = push_instr_bstr_uint(ctx, ret_val ? OP_mcall : OP_mcallv, expr->identifier, arg_cnt);
468 }else {
469 hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
472 return hres;
475 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr)
477 expression_t *const_expr;
479 if (expr->obj_expr) /* FIXME: we should probably have a dedicated opcode as well */
480 return compile_member_call_expression(ctx, expr, 0, TRUE);
482 const_expr = lookup_const_decls(ctx, expr->identifier, TRUE);
483 if(const_expr)
484 return compile_expression(ctx, const_expr);
486 return push_instr_bstr(ctx, OP_ident, expr->identifier);
489 static HRESULT compile_call_expression(compile_ctx_t *ctx, call_expression_t *expr, BOOL ret_val)
491 unsigned arg_cnt = 0;
492 expression_t *call;
493 HRESULT hres;
495 hres = compile_args(ctx, expr->args, &arg_cnt);
496 if(FAILED(hres))
497 return hres;
499 for(call = expr->call_expr; call->type == EXPR_BRACKETS; call = ((unary_expression_t*)call)->subexpr);
501 if(call->type == EXPR_MEMBER)
502 return compile_member_call_expression(ctx, (member_expression_t*)call, arg_cnt, ret_val);
504 hres = compile_expression(ctx, call);
505 if(FAILED(hres))
506 return hres;
508 return push_instr_uint(ctx, ret_val ? OP_vcall : OP_vcallv, arg_cnt);
511 static HRESULT compile_dot_expression(compile_ctx_t *ctx)
513 statement_ctx_t *stat_ctx;
515 for(stat_ctx = ctx->stat_ctx; stat_ctx; stat_ctx = stat_ctx->next) {
516 if(!stat_ctx->with_stack_offset)
517 continue;
519 return push_instr_uint(ctx, OP_stack, stat_ctx->with_stack_offset - 1);
522 WARN("dot expression outside with statement\n");
523 return push_instr_uint(ctx, OP_stack, ~0);
526 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
528 HRESULT hres;
530 hres = compile_expression(ctx, expr->subexpr);
531 if(FAILED(hres))
532 return hres;
534 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
537 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
539 HRESULT hres;
541 hres = compile_expression(ctx, expr->left);
542 if(FAILED(hres))
543 return hres;
545 hres = compile_expression(ctx, expr->right);
546 if(FAILED(hres))
547 return hres;
549 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
552 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
554 switch(expr->type) {
555 case EXPR_ADD:
556 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
557 case EXPR_AND:
558 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
559 case EXPR_BOOL:
560 return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
561 case EXPR_BRACKETS:
562 return compile_expression(ctx, ((unary_expression_t*)expr)->subexpr);
563 case EXPR_CALL:
564 return compile_call_expression(ctx, (call_expression_t*)expr, TRUE);
565 case EXPR_CONCAT:
566 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
567 case EXPR_DATE:
568 return push_instr_date(ctx, OP_date, ((date_expression_t*)expr)->value);
569 case EXPR_DIV:
570 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
571 case EXPR_DOT:
572 return compile_dot_expression(ctx);
573 case EXPR_DOUBLE:
574 return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
575 case EXPR_EMPTY:
576 return push_instr(ctx, OP_empty) ? S_OK : E_OUTOFMEMORY;
577 case EXPR_EQUAL:
578 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
579 case EXPR_EQV:
580 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eqv);
581 case EXPR_EXP:
582 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
583 case EXPR_GT:
584 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
585 case EXPR_GTEQ:
586 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
587 case EXPR_IDIV:
588 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
589 case EXPR_IS:
590 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_is);
591 case EXPR_IMP:
592 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_imp);
593 case EXPR_LT:
594 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
595 case EXPR_LTEQ:
596 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
597 case EXPR_ME:
598 return push_instr(ctx, OP_me) ? S_OK : E_OUTOFMEMORY;
599 case EXPR_MEMBER:
600 return compile_member_expression(ctx, (member_expression_t*)expr);
601 case EXPR_MOD:
602 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
603 case EXPR_MUL:
604 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
605 case EXPR_NEG:
606 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
607 case EXPR_NEQUAL:
608 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
609 case EXPR_NEW:
610 return push_instr_str(ctx, OP_new, ((string_expression_t*)expr)->value);
611 case EXPR_NOARG:
612 return push_instr_int(ctx, OP_hres, DISP_E_PARAMNOTFOUND);
613 case EXPR_NOT:
614 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
615 case EXPR_NOTHING:
616 return push_instr(ctx, OP_nothing) ? S_OK : E_OUTOFMEMORY;
617 case EXPR_NULL:
618 return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
619 case EXPR_OR:
620 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
621 case EXPR_STRING:
622 return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
623 case EXPR_SUB:
624 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
625 case EXPR_INT:
626 return push_instr_int(ctx, OP_int, ((int_expression_t*)expr)->value);
627 case EXPR_XOR:
628 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
629 default:
630 FIXME("Unimplemented expression type %d\n", expr->type);
631 return E_NOTIMPL;
634 return S_OK;
637 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
639 unsigned cnd_jmp, endif_label = 0;
640 elseif_decl_t *elseif_decl;
641 HRESULT hres;
643 hres = compile_expression(ctx, stat->expr);
644 if(FAILED(hres))
645 return hres;
647 cnd_jmp = push_instr(ctx, OP_jmp_false);
648 if(!cnd_jmp)
649 return E_OUTOFMEMORY;
651 if(!emit_catch(ctx, 0))
652 return E_OUTOFMEMORY;
654 hres = compile_statement(ctx, NULL, stat->if_stat);
655 if(FAILED(hres))
656 return hres;
658 if(stat->else_stat || stat->elseifs) {
659 endif_label = alloc_label(ctx);
660 if(!endif_label)
661 return E_OUTOFMEMORY;
663 hres = push_instr_addr(ctx, OP_jmp, endif_label);
664 if(FAILED(hres))
665 return hres;
668 for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
669 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
671 ctx->loc = elseif_decl->loc;
673 hres = compile_expression(ctx, elseif_decl->expr);
674 if(FAILED(hres))
675 return hres;
677 cnd_jmp = push_instr(ctx, OP_jmp_false);
678 if(!cnd_jmp)
679 return E_OUTOFMEMORY;
681 if(!emit_catch(ctx, 0))
682 return E_OUTOFMEMORY;
684 hres = compile_statement(ctx, NULL, elseif_decl->stat);
685 if(FAILED(hres))
686 return hres;
688 hres = push_instr_addr(ctx, OP_jmp, endif_label);
689 if(FAILED(hres))
690 return hres;
693 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
695 if(stat->else_stat) {
696 hres = compile_statement(ctx, NULL, stat->else_stat);
697 if(FAILED(hres))
698 return hres;
701 if(endif_label)
702 label_set_addr(ctx, endif_label);
703 return S_OK;
706 static HRESULT compile_while_statement(compile_ctx_t *ctx, while_statement_t *stat)
708 statement_ctx_t stat_ctx = {0}, *loop_ctx;
709 unsigned start_addr;
710 unsigned jmp_end;
711 HRESULT hres;
713 start_addr = ctx->instr_cnt;
715 hres = compile_expression(ctx, stat->expr);
716 if(FAILED(hres))
717 return hres;
719 jmp_end = push_instr(ctx, stat->stat.type == STAT_UNTIL ? OP_jmp_true : OP_jmp_false);
720 if(!jmp_end)
721 return E_OUTOFMEMORY;
723 if(!emit_catch(ctx, 0))
724 return E_OUTOFMEMORY;
726 if(stat->stat.type == STAT_WHILE) {
727 loop_ctx = NULL;
728 }else {
729 if(!(stat_ctx.while_end_label = alloc_label(ctx)))
730 return E_OUTOFMEMORY;
731 loop_ctx = &stat_ctx;
734 hres = compile_statement(ctx, loop_ctx, stat->body);
735 if(FAILED(hres))
736 return hres;
738 hres = push_instr_addr(ctx, OP_jmp, start_addr);
739 if(FAILED(hres))
740 return hres;
742 instr_ptr(ctx, jmp_end)->arg1.uint = ctx->instr_cnt;
744 if(loop_ctx)
745 label_set_addr(ctx, stat_ctx.while_end_label);
747 return S_OK;
750 static HRESULT compile_dowhile_statement(compile_ctx_t *ctx, while_statement_t *stat)
752 statement_ctx_t loop_ctx = {0};
753 unsigned start_addr;
754 vbsop_t jmp_op;
755 HRESULT hres;
757 start_addr = ctx->instr_cnt;
759 if(!(loop_ctx.while_end_label = alloc_label(ctx)))
760 return E_OUTOFMEMORY;
762 hres = compile_statement(ctx, &loop_ctx, stat->body);
763 if(FAILED(hres))
764 return hres;
766 ctx->loc = stat->stat.loc;
767 if(stat->expr) {
768 hres = compile_expression(ctx, stat->expr);
769 if(FAILED(hres))
770 return hres;
772 jmp_op = stat->stat.type == STAT_DOUNTIL ? OP_jmp_false : OP_jmp_true;
773 }else {
774 jmp_op = OP_jmp;
777 hres = push_instr_addr(ctx, jmp_op, start_addr);
778 if(FAILED(hres))
779 return hres;
781 label_set_addr(ctx, loop_ctx.while_end_label);
783 if(!emit_catch(ctx, 0))
784 return E_OUTOFMEMORY;
786 return S_OK;
789 static HRESULT compile_foreach_statement(compile_ctx_t *ctx, foreach_statement_t *stat)
791 statement_ctx_t loop_ctx = {1};
792 unsigned loop_start;
793 HRESULT hres;
795 /* Preserve a place on the stack in case we throw before having proper enum collection. */
796 if(!push_instr(ctx, OP_empty))
797 return E_OUTOFMEMORY;
799 hres = compile_expression(ctx, stat->group_expr);
800 if(FAILED(hres))
801 return hres;
803 if(!push_instr(ctx, OP_newenum))
804 return E_OUTOFMEMORY;
806 if(!(loop_ctx.for_end_label = alloc_label(ctx)))
807 return E_OUTOFMEMORY;
809 hres = push_instr_uint_bstr(ctx, OP_enumnext, loop_ctx.for_end_label, stat->identifier);
810 if(FAILED(hres))
811 return hres;
813 if(!emit_catch(ctx, 1))
814 return E_OUTOFMEMORY;
816 loop_start = ctx->instr_cnt;
817 hres = compile_statement(ctx, &loop_ctx, stat->body);
818 if(FAILED(hres))
819 return hres;
821 /* We need a separated enumnext here, because we need to jump out of the loop on exception. */
822 ctx->loc = stat->stat.loc;
823 hres = push_instr_uint_bstr(ctx, OP_enumnext, loop_ctx.for_end_label, stat->identifier);
824 if(FAILED(hres))
825 return hres;
827 hres = push_instr_addr(ctx, OP_jmp, loop_start);
828 if(FAILED(hres))
829 return hres;
831 label_set_addr(ctx, loop_ctx.for_end_label);
833 if(!emit_catch(ctx, 0))
834 return E_OUTOFMEMORY;
836 return S_OK;
839 static HRESULT compile_forto_statement(compile_ctx_t *ctx, forto_statement_t *stat)
841 statement_ctx_t loop_ctx = {2};
842 unsigned step_instr, instr;
843 BSTR identifier;
844 HRESULT hres;
846 identifier = alloc_bstr_arg(ctx, stat->identifier);
847 if(!identifier)
848 return E_OUTOFMEMORY;
850 hres = compile_expression(ctx, stat->from_expr);
851 if(FAILED(hres))
852 return hres;
854 /* FIXME: Assign should happen after both expressions evaluation. */
855 instr = push_instr(ctx, OP_assign_ident);
856 if(!instr)
857 return E_OUTOFMEMORY;
858 instr_ptr(ctx, instr)->arg1.bstr = identifier;
859 instr_ptr(ctx, instr)->arg2.uint = 0;
861 hres = compile_expression(ctx, stat->to_expr);
862 if(FAILED(hres))
863 return hres;
865 if(!push_instr(ctx, OP_val))
866 return E_OUTOFMEMORY;
868 if(stat->step_expr) {
869 hres = compile_expression(ctx, stat->step_expr);
870 if(FAILED(hres))
871 return hres;
873 if(!push_instr(ctx, OP_val))
874 return E_OUTOFMEMORY;
875 }else {
876 hres = push_instr_int(ctx, OP_int, 1);
877 if(FAILED(hres))
878 return hres;
881 loop_ctx.for_end_label = alloc_label(ctx);
882 if(!loop_ctx.for_end_label)
883 return E_OUTOFMEMORY;
885 step_instr = push_instr(ctx, OP_step);
886 if(!step_instr)
887 return E_OUTOFMEMORY;
888 instr_ptr(ctx, step_instr)->arg2.bstr = identifier;
889 instr_ptr(ctx, step_instr)->arg1.uint = loop_ctx.for_end_label;
891 if(!emit_catch(ctx, 2))
892 return E_OUTOFMEMORY;
894 hres = compile_statement(ctx, &loop_ctx, stat->body);
895 if(FAILED(hres))
896 return hres;
898 /* FIXME: Error handling can't be done compatible with native using OP_incc here. */
899 instr = push_instr(ctx, OP_incc);
900 if(!instr)
901 return E_OUTOFMEMORY;
902 instr_ptr(ctx, instr)->arg1.bstr = identifier;
904 hres = push_instr_addr(ctx, OP_jmp, step_instr);
905 if(FAILED(hres))
906 return hres;
908 hres = push_instr_uint(ctx, OP_pop, 2);
909 if(FAILED(hres))
910 return hres;
912 label_set_addr(ctx, loop_ctx.for_end_label);
914 /* FIXME: reconsider after OP_incc fixup. */
915 if(!emit_catch(ctx, 0))
916 return E_OUTOFMEMORY;
918 return S_OK;
921 static HRESULT compile_with_statement(compile_ctx_t *ctx, with_statement_t *stat)
923 statement_ctx_t with_ctx = { 1 };
924 HRESULT hres;
926 hres = compile_expression(ctx, stat->expr);
927 if(FAILED(hres))
928 return hres;
930 if(!emit_catch(ctx, 1))
931 return E_OUTOFMEMORY;
933 with_ctx.with_stack_offset = stack_offset(ctx) + 1;
934 hres = compile_statement(ctx, &with_ctx, stat->body);
935 if(FAILED(hres))
936 return hres;
938 return push_instr_uint(ctx, OP_pop, 1);
941 static HRESULT compile_select_statement(compile_ctx_t *ctx, select_statement_t *stat)
943 unsigned end_label, case_cnt = 0, *case_labels = NULL, i;
944 case_clausule_t *case_iter;
945 expression_t *expr_iter;
946 HRESULT hres;
948 hres = compile_expression(ctx, stat->expr);
949 if(FAILED(hres))
950 return hres;
952 if(!push_instr(ctx, OP_val))
953 return E_OUTOFMEMORY;
955 end_label = alloc_label(ctx);
956 if(!end_label)
957 return E_OUTOFMEMORY;
959 if(!emit_catch_jmp(ctx, 0, end_label))
960 return E_OUTOFMEMORY;
962 for(case_iter = stat->case_clausules; case_iter; case_iter = case_iter->next)
963 case_cnt++;
965 if(case_cnt) {
966 case_labels = heap_alloc(case_cnt*sizeof(*case_labels));
967 if(!case_labels)
968 return E_OUTOFMEMORY;
971 for(case_iter = stat->case_clausules, i=0; case_iter; case_iter = case_iter->next, i++) {
972 case_labels[i] = alloc_label(ctx);
973 if(!case_labels[i]) {
974 hres = E_OUTOFMEMORY;
975 break;
978 if(!case_iter->expr)
979 break;
981 for(expr_iter = case_iter->expr; expr_iter; expr_iter = expr_iter->next) {
982 hres = compile_expression(ctx, expr_iter);
983 if(FAILED(hres))
984 break;
986 hres = push_instr_addr(ctx, OP_case, case_labels[i]);
987 if(FAILED(hres))
988 break;
990 if(!emit_catch_jmp(ctx, 0, case_labels[i])) {
991 hres = E_OUTOFMEMORY;
992 break;
997 if(FAILED(hres)) {
998 heap_free(case_labels);
999 return hres;
1002 hres = push_instr_uint(ctx, OP_pop, 1);
1003 if(FAILED(hres)) {
1004 heap_free(case_labels);
1005 return hres;
1008 hres = push_instr_addr(ctx, OP_jmp, case_iter ? case_labels[i] : end_label);
1009 if(FAILED(hres)) {
1010 heap_free(case_labels);
1011 return hres;
1014 for(case_iter = stat->case_clausules, i=0; case_iter; case_iter = case_iter->next, i++) {
1015 label_set_addr(ctx, case_labels[i]);
1016 hres = compile_statement(ctx, NULL, case_iter->stat);
1017 if(FAILED(hres))
1018 break;
1020 if(!case_iter->next)
1021 break;
1023 hres = push_instr_addr(ctx, OP_jmp, end_label);
1024 if(FAILED(hres))
1025 break;
1028 heap_free(case_labels);
1029 if(FAILED(hres))
1030 return hres;
1032 label_set_addr(ctx, end_label);
1033 return S_OK;
1036 static HRESULT compile_assignment(compile_ctx_t *ctx, expression_t *left, expression_t *value_expr, BOOL is_set)
1038 call_expression_t *call_expr = NULL;
1039 member_expression_t *member_expr;
1040 unsigned args_cnt = 0;
1041 vbsop_t op;
1042 HRESULT hres;
1044 switch(left->type) {
1045 case EXPR_MEMBER:
1046 member_expr = (member_expression_t*)left;
1047 break;
1048 case EXPR_CALL:
1049 call_expr = (call_expression_t*)left;
1050 assert(call_expr->call_expr->type == EXPR_MEMBER);
1051 member_expr = (member_expression_t*)call_expr->call_expr;
1052 break;
1053 default:
1054 assert(0);
1055 return E_FAIL;
1058 if(member_expr->obj_expr) {
1059 hres = compile_expression(ctx, member_expr->obj_expr);
1060 if(FAILED(hres))
1061 return hres;
1063 op = is_set ? OP_set_member : OP_assign_member;
1064 }else {
1065 op = is_set ? OP_set_ident : OP_assign_ident;
1068 hres = compile_expression(ctx, value_expr);
1069 if(FAILED(hres))
1070 return hres;
1072 if(call_expr) {
1073 hres = compile_args(ctx, call_expr->args, &args_cnt);
1074 if(FAILED(hres))
1075 return hres;
1078 hres = push_instr_bstr_uint(ctx, op, member_expr->identifier, args_cnt);
1079 if(FAILED(hres))
1080 return hres;
1082 if(!emit_catch(ctx, 0))
1083 return E_OUTOFMEMORY;
1085 return S_OK;
1088 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat, BOOL is_set)
1090 return compile_assignment(ctx, stat->left_expr, stat->value_expr, is_set);
1093 static HRESULT compile_call_statement(compile_ctx_t *ctx, call_statement_t *stat)
1095 HRESULT hres;
1097 hres = compile_call_expression(ctx, stat->expr, FALSE);
1098 if(FAILED(hres))
1099 return hres;
1101 if(!emit_catch(ctx, 0))
1102 return E_OUTOFMEMORY;
1104 return S_OK;
1107 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
1109 dim_decl_t *dim_decl;
1111 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
1112 if(!wcsicmp(dim_decl->name, name))
1113 return TRUE;
1116 return FALSE;
1119 static BOOL lookup_args_name(compile_ctx_t *ctx, const WCHAR *name)
1121 unsigned i;
1123 for(i = 0; i < ctx->func->arg_cnt; i++) {
1124 if(!wcsicmp(ctx->func->args[i].name, name))
1125 return TRUE;
1128 return FALSE;
1131 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
1133 dim_decl_t *dim_decl = stat->dim_decls;
1135 while(1) {
1136 if(lookup_dim_decls(ctx, dim_decl->name) || lookup_args_name(ctx, dim_decl->name)
1137 || lookup_const_decls(ctx, dim_decl->name, FALSE)) {
1138 FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
1139 return E_FAIL;
1142 ctx->func->var_cnt++;
1144 if(dim_decl->is_array) {
1145 HRESULT hres = push_instr_bstr_uint(ctx, OP_dim, dim_decl->name, ctx->func->array_cnt++);
1146 if(FAILED(hres))
1147 return hres;
1149 if(!emit_catch(ctx, 0))
1150 return E_OUTOFMEMORY;
1153 if(!dim_decl->next)
1154 break;
1155 dim_decl = dim_decl->next;
1158 if(ctx->dim_decls_tail)
1159 ctx->dim_decls_tail->next = stat->dim_decls;
1160 else
1161 ctx->dim_decls = stat->dim_decls;
1162 ctx->dim_decls_tail = dim_decl;
1163 return S_OK;
1166 static HRESULT compile_redim_statement(compile_ctx_t *ctx, redim_statement_t *stat)
1168 unsigned arg_cnt;
1169 HRESULT hres;
1171 hres = compile_args(ctx, stat->dims, &arg_cnt);
1172 if(FAILED(hres))
1173 return hres;
1175 hres = push_instr_bstr_uint(ctx, stat->preserve ? OP_redim_preserve : OP_redim, stat->identifier, arg_cnt);
1176 if(FAILED(hres))
1177 return hres;
1179 if(!emit_catch(ctx, 0))
1180 return E_OUTOFMEMORY;
1182 return S_OK;
1185 static HRESULT compile_const_statement(compile_ctx_t *ctx, const_statement_t *stat)
1187 const_decl_t *decl, *next_decl = stat->decls;
1189 do {
1190 decl = next_decl;
1192 if(lookup_const_decls(ctx, decl->name, FALSE) || lookup_args_name(ctx, decl->name)
1193 || lookup_dim_decls(ctx, decl->name)) {
1194 FIXME("%s redefined\n", debugstr_w(decl->name));
1195 return E_FAIL;
1198 if(ctx->func->type == FUNC_GLOBAL) {
1199 HRESULT hres;
1201 hres = compile_expression(ctx, decl->value_expr);
1202 if(FAILED(hres))
1203 return hres;
1205 hres = push_instr_bstr(ctx, OP_const, decl->name);
1206 if(FAILED(hres))
1207 return hres;
1209 if(!emit_catch(ctx, 0))
1210 return E_OUTOFMEMORY;
1213 next_decl = decl->next;
1214 decl->next = ctx->const_decls;
1215 ctx->const_decls = decl;
1216 } while(next_decl);
1218 return S_OK;
1221 static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
1223 if(ctx->func != &ctx->code->main_code) {
1224 FIXME("Function is not in the global code\n");
1225 return E_FAIL;
1228 stat->func_decl->next = ctx->func_decls;
1229 ctx->func_decls = stat->func_decl;
1230 return S_OK;
1233 static HRESULT compile_exitdo_statement(compile_ctx_t *ctx)
1235 statement_ctx_t *iter;
1236 unsigned pop_cnt = 0;
1238 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1239 pop_cnt += iter->stack_use;
1240 if(iter->while_end_label)
1241 break;
1243 if(!iter) {
1244 FIXME("Exit Do outside Do Loop\n");
1245 return E_FAIL;
1248 if(pop_cnt) {
1249 HRESULT hres;
1251 hres = push_instr_uint(ctx, OP_pop, pop_cnt);
1252 if(FAILED(hres))
1253 return hres;
1256 return push_instr_addr(ctx, OP_jmp, iter->while_end_label);
1259 static HRESULT compile_exitfor_statement(compile_ctx_t *ctx)
1261 statement_ctx_t *iter;
1262 unsigned pop_cnt = 0;
1264 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1265 pop_cnt += iter->stack_use;
1266 if(iter->for_end_label)
1267 break;
1269 if(!iter) {
1270 FIXME("Exit For outside For loop\n");
1271 return E_FAIL;
1274 if(pop_cnt) {
1275 HRESULT hres;
1277 hres = push_instr_uint(ctx, OP_pop, pop_cnt);
1278 if(FAILED(hres))
1279 return hres;
1282 return push_instr_addr(ctx, OP_jmp, iter->for_end_label);
1285 static HRESULT exit_label(compile_ctx_t *ctx, unsigned jmp_label)
1287 unsigned pop_cnt = stack_offset(ctx);
1289 if(pop_cnt) {
1290 HRESULT hres;
1292 hres = push_instr_uint(ctx, OP_pop, pop_cnt);
1293 if(FAILED(hres))
1294 return hres;
1297 return push_instr_addr(ctx, OP_jmp, jmp_label);
1300 static HRESULT compile_exitsub_statement(compile_ctx_t *ctx)
1302 if(!ctx->sub_end_label) {
1303 FIXME("Exit Sub outside Sub?\n");
1304 return E_FAIL;
1307 return exit_label(ctx, ctx->sub_end_label);
1310 static HRESULT compile_exitfunc_statement(compile_ctx_t *ctx)
1312 if(!ctx->func_end_label) {
1313 FIXME("Exit Function outside Function?\n");
1314 return E_FAIL;
1317 return exit_label(ctx, ctx->func_end_label);
1320 static HRESULT compile_exitprop_statement(compile_ctx_t *ctx)
1322 if(!ctx->prop_end_label) {
1323 FIXME("Exit Property outside Property?\n");
1324 return E_FAIL;
1327 return exit_label(ctx, ctx->prop_end_label);
1330 static HRESULT compile_onerror_statement(compile_ctx_t *ctx, onerror_statement_t *stat)
1332 return push_instr_int(ctx, OP_errmode, stat->resume_next);
1335 static HRESULT compile_retval_statement(compile_ctx_t *ctx, retval_statement_t *stat)
1337 HRESULT hres;
1339 hres = compile_expression(ctx, stat->expr);
1340 if(FAILED(hres))
1341 return hres;
1343 hres = push_instr(ctx, OP_retval);
1344 if(FAILED(hres))
1345 return hres;
1347 return S_OK;
1350 static HRESULT compile_statement(compile_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1352 HRESULT hres;
1354 if(stat_ctx) {
1355 stat_ctx->next = ctx->stat_ctx;
1356 ctx->stat_ctx = stat_ctx;
1359 while(stat) {
1360 ctx->loc = stat->loc;
1362 switch(stat->type) {
1363 case STAT_ASSIGN:
1364 hres = compile_assign_statement(ctx, (assign_statement_t*)stat, FALSE);
1365 break;
1366 case STAT_CALL:
1367 hres = compile_call_statement(ctx, (call_statement_t*)stat);
1368 break;
1369 case STAT_CONST:
1370 hres = compile_const_statement(ctx, (const_statement_t*)stat);
1371 break;
1372 case STAT_DIM:
1373 hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
1374 break;
1375 case STAT_DOWHILE:
1376 case STAT_DOUNTIL:
1377 hres = compile_dowhile_statement(ctx, (while_statement_t*)stat);
1378 break;
1379 case STAT_EXITDO:
1380 hres = compile_exitdo_statement(ctx);
1381 break;
1382 case STAT_EXITFOR:
1383 hres = compile_exitfor_statement(ctx);
1384 break;
1385 case STAT_EXITFUNC:
1386 hres = compile_exitfunc_statement(ctx);
1387 break;
1388 case STAT_EXITPROP:
1389 hres = compile_exitprop_statement(ctx);
1390 break;
1391 case STAT_EXITSUB:
1392 hres = compile_exitsub_statement(ctx);
1393 break;
1394 case STAT_FOREACH:
1395 hres = compile_foreach_statement(ctx, (foreach_statement_t*)stat);
1396 break;
1397 case STAT_FORTO:
1398 hres = compile_forto_statement(ctx, (forto_statement_t*)stat);
1399 break;
1400 case STAT_FUNC:
1401 hres = compile_function_statement(ctx, (function_statement_t*)stat);
1402 break;
1403 case STAT_IF:
1404 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1405 break;
1406 case STAT_ONERROR:
1407 hres = compile_onerror_statement(ctx, (onerror_statement_t*)stat);
1408 break;
1409 case STAT_REDIM:
1410 hres = compile_redim_statement(ctx, (redim_statement_t*)stat);
1411 break;
1412 case STAT_SELECT:
1413 hres = compile_select_statement(ctx, (select_statement_t*)stat);
1414 break;
1415 case STAT_SET:
1416 hres = compile_assign_statement(ctx, (assign_statement_t*)stat, TRUE);
1417 break;
1418 case STAT_STOP:
1419 hres = push_instr(ctx, OP_stop) ? S_OK : E_OUTOFMEMORY;
1420 break;
1421 case STAT_UNTIL:
1422 case STAT_WHILE:
1423 case STAT_WHILELOOP:
1424 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1425 break;
1426 case STAT_WITH:
1427 hres = compile_with_statement(ctx, (with_statement_t*)stat);
1428 break;
1429 case STAT_RETVAL:
1430 hres = compile_retval_statement(ctx, (retval_statement_t*)stat);
1431 break;
1432 default:
1433 FIXME("Unimplemented statement type %d\n", stat->type);
1434 hres = E_NOTIMPL;
1437 if(FAILED(hres))
1438 return hres;
1439 stat = stat->next;
1442 if(stat_ctx) {
1443 assert(ctx->stat_ctx == stat_ctx);
1444 ctx->stat_ctx = stat_ctx->next;
1447 return S_OK;
1450 static void resolve_labels(compile_ctx_t *ctx, unsigned off)
1452 instr_t *instr;
1454 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
1455 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
1456 assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
1457 instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
1459 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
1462 ctx->labels_cnt = 0;
1465 static HRESULT fill_array_desc(compile_ctx_t *ctx, dim_decl_t *dim_decl, array_desc_t *array_desc)
1467 unsigned dim_cnt = 0, i;
1468 dim_list_t *iter;
1470 for(iter = dim_decl->dims; iter; iter = iter->next)
1471 dim_cnt++;
1473 array_desc->bounds = compiler_alloc(ctx->code, dim_cnt * sizeof(SAFEARRAYBOUND));
1474 if(!array_desc->bounds)
1475 return E_OUTOFMEMORY;
1477 array_desc->dim_cnt = dim_cnt;
1479 for(iter = dim_decl->dims, i=0; iter; iter = iter->next, i++) {
1480 array_desc->bounds[i].cElements = iter->val+1;
1481 array_desc->bounds[i].lLbound = 0;
1484 return S_OK;
1487 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
1489 HRESULT hres;
1491 func->code_off = ctx->instr_cnt;
1493 ctx->sub_end_label = 0;
1494 ctx->func_end_label = 0;
1495 ctx->prop_end_label = 0;
1497 switch(func->type) {
1498 case FUNC_FUNCTION:
1499 ctx->func_end_label = alloc_label(ctx);
1500 if(!ctx->func_end_label)
1501 return E_OUTOFMEMORY;
1502 break;
1503 case FUNC_SUB:
1504 ctx->sub_end_label = alloc_label(ctx);
1505 if(!ctx->sub_end_label)
1506 return E_OUTOFMEMORY;
1507 break;
1508 case FUNC_PROPGET:
1509 case FUNC_PROPLET:
1510 case FUNC_PROPSET:
1511 ctx->prop_end_label = alloc_label(ctx);
1512 if(!ctx->prop_end_label)
1513 return E_OUTOFMEMORY;
1514 break;
1515 case FUNC_GLOBAL:
1516 break;
1519 ctx->func = func;
1520 ctx->dim_decls = ctx->dim_decls_tail = NULL;
1521 ctx->const_decls = NULL;
1522 hres = compile_statement(ctx, NULL, stat);
1523 ctx->func = NULL;
1524 if(FAILED(hres))
1525 return hres;
1527 if(ctx->sub_end_label)
1528 label_set_addr(ctx, ctx->sub_end_label);
1529 if(ctx->func_end_label)
1530 label_set_addr(ctx, ctx->func_end_label);
1531 if(ctx->prop_end_label)
1532 label_set_addr(ctx, ctx->prop_end_label);
1534 if(!push_instr(ctx, OP_ret))
1535 return E_OUTOFMEMORY;
1537 resolve_labels(ctx, func->code_off);
1539 if(func->var_cnt) {
1540 dim_decl_t *dim_decl;
1541 unsigned i;
1543 func->vars = compiler_alloc(ctx->code, func->var_cnt * sizeof(var_desc_t));
1544 if(!func->vars)
1545 return E_OUTOFMEMORY;
1547 for(dim_decl = ctx->dim_decls, i=0; dim_decl; dim_decl = dim_decl->next, i++) {
1548 func->vars[i].name = compiler_alloc_string(ctx->code, dim_decl->name);
1549 if(!func->vars[i].name)
1550 return E_OUTOFMEMORY;
1553 assert(i == func->var_cnt);
1556 if(func->array_cnt) {
1557 unsigned array_id = 0;
1558 dim_decl_t *dim_decl;
1560 func->array_descs = compiler_alloc(ctx->code, func->array_cnt * sizeof(array_desc_t));
1561 if(!func->array_descs)
1562 return E_OUTOFMEMORY;
1564 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
1565 if(dim_decl->is_array) {
1566 hres = fill_array_desc(ctx, dim_decl, func->array_descs + array_id++);
1567 if(FAILED(hres))
1568 return hres;
1572 assert(array_id == func->array_cnt);
1575 return S_OK;
1578 static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
1580 function_t *iter;
1582 for(iter = ctx->code->funcs; iter; iter = iter->next) {
1583 if(!wcsicmp(iter->name, name))
1584 return TRUE;
1587 return FALSE;
1590 static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
1592 function_t *func;
1593 HRESULT hres;
1595 if(lookup_dim_decls(ctx, decl->name) || lookup_const_decls(ctx, decl->name, FALSE)) {
1596 FIXME("%s: redefinition\n", debugstr_w(decl->name));
1597 return E_FAIL;
1600 func = compiler_alloc(ctx->code, sizeof(*func));
1601 if(!func)
1602 return E_OUTOFMEMORY;
1604 func->name = compiler_alloc_string(ctx->code, decl->name);
1605 if(!func->name)
1606 return E_OUTOFMEMORY;
1608 func->vars = NULL;
1609 func->var_cnt = 0;
1610 func->array_cnt = 0;
1611 func->code_ctx = ctx->code;
1612 func->type = decl->type;
1613 func->is_public = decl->is_public;
1615 func->arg_cnt = 0;
1616 if(decl->args) {
1617 arg_decl_t *arg;
1618 unsigned i;
1620 for(arg = decl->args; arg; arg = arg->next)
1621 func->arg_cnt++;
1623 func->args = compiler_alloc(ctx->code, func->arg_cnt * sizeof(arg_desc_t));
1624 if(!func->args)
1625 return E_OUTOFMEMORY;
1627 for(i = 0, arg = decl->args; arg; arg = arg->next, i++) {
1628 func->args[i].name = compiler_alloc_string(ctx->code, arg->name);
1629 if(!func->args[i].name)
1630 return E_OUTOFMEMORY;
1631 func->args[i].by_ref = arg->by_ref;
1633 }else {
1634 func->args = NULL;
1637 hres = compile_func(ctx, decl->body, func);
1638 if(FAILED(hres))
1639 return hres;
1641 *ret = func;
1642 return S_OK;
1645 static BOOL lookup_class_name(compile_ctx_t *ctx, const WCHAR *name)
1647 class_desc_t *iter;
1649 for(iter = ctx->code->classes; iter; iter = iter->next) {
1650 if(!wcsicmp(iter->name, name))
1651 return TRUE;
1654 return FALSE;
1657 static HRESULT create_class_funcprop(compile_ctx_t *ctx, function_decl_t *func_decl, vbdisp_funcprop_desc_t *desc)
1659 vbdisp_invoke_type_t invoke_type;
1660 function_decl_t *funcprop_decl;
1661 HRESULT hres;
1663 desc->name = compiler_alloc_string(ctx->code, func_decl->name);
1664 if(!desc->name)
1665 return E_OUTOFMEMORY;
1667 for(funcprop_decl = func_decl; funcprop_decl; funcprop_decl = funcprop_decl->next_prop_func) {
1668 switch(funcprop_decl->type) {
1669 case FUNC_FUNCTION:
1670 case FUNC_SUB:
1671 case FUNC_PROPGET:
1672 invoke_type = VBDISP_CALLGET;
1673 break;
1674 case FUNC_PROPLET:
1675 invoke_type = VBDISP_LET;
1676 break;
1677 case FUNC_PROPSET:
1678 invoke_type = VBDISP_SET;
1679 break;
1680 DEFAULT_UNREACHABLE;
1683 assert(!desc->entries[invoke_type]);
1685 if(funcprop_decl->is_public)
1686 desc->is_public = TRUE;
1688 hres = create_function(ctx, funcprop_decl, desc->entries+invoke_type);
1689 if(FAILED(hres))
1690 return hres;
1693 return S_OK;
1696 static BOOL lookup_class_funcs(class_desc_t *class_desc, const WCHAR *name)
1698 unsigned i;
1700 for(i=0; i < class_desc->func_cnt; i++) {
1701 if(class_desc->funcs[i].name && !wcsicmp(class_desc->funcs[i].name, name))
1702 return TRUE;
1705 return FALSE;
1708 static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl)
1710 function_decl_t *func_decl, *func_prop_decl;
1711 BOOL is_default, have_default = FALSE;
1712 class_desc_t *class_desc;
1713 dim_decl_t *prop_decl;
1714 unsigned i;
1715 HRESULT hres;
1717 if(lookup_dim_decls(ctx, class_decl->name) || lookup_funcs_name(ctx, class_decl->name)
1718 || lookup_const_decls(ctx, class_decl->name, FALSE) || lookup_class_name(ctx, class_decl->name)) {
1719 FIXME("%s: redefinition\n", debugstr_w(class_decl->name));
1720 return E_FAIL;
1723 class_desc = compiler_alloc_zero(ctx->code, sizeof(*class_desc));
1724 if(!class_desc)
1725 return E_OUTOFMEMORY;
1727 class_desc->name = compiler_alloc_string(ctx->code, class_decl->name);
1728 if(!class_desc->name)
1729 return E_OUTOFMEMORY;
1731 class_desc->func_cnt = 1; /* always allocate slot for default getter or method */
1733 for(func_decl = class_decl->funcs; func_decl; func_decl = func_decl->next) {
1734 is_default = FALSE;
1735 for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1736 if(func_prop_decl->is_default) {
1737 if(have_default) {
1738 FIXME("multiple default getters or methods\n");
1739 return E_FAIL;
1741 is_default = have_default = TRUE;
1742 break;
1745 if(!is_default)
1746 class_desc->func_cnt++;
1749 class_desc->funcs = compiler_alloc(ctx->code, class_desc->func_cnt*sizeof(*class_desc->funcs));
1750 if(!class_desc->funcs)
1751 return E_OUTOFMEMORY;
1752 memset(class_desc->funcs, 0, class_desc->func_cnt*sizeof(*class_desc->funcs));
1754 for(func_decl = class_decl->funcs, i=1; func_decl; func_decl = func_decl->next, i++) {
1755 for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1756 if(func_prop_decl->is_default) {
1757 i--;
1758 break;
1762 if(!wcsicmp(L"class_initialize", func_decl->name)) {
1763 if(func_decl->type != FUNC_SUB) {
1764 FIXME("class initializer is not sub\n");
1765 return E_FAIL;
1768 class_desc->class_initialize_id = i;
1769 }else if(!wcsicmp(L"class_terminate", func_decl->name)) {
1770 if(func_decl->type != FUNC_SUB) {
1771 FIXME("class terminator is not sub\n");
1772 return E_FAIL;
1775 class_desc->class_terminate_id = i;
1778 hres = create_class_funcprop(ctx, func_decl, class_desc->funcs + (func_prop_decl ? 0 : i));
1779 if(FAILED(hres))
1780 return hres;
1783 for(prop_decl = class_decl->props; prop_decl; prop_decl = prop_decl->next)
1784 class_desc->prop_cnt++;
1786 class_desc->props = compiler_alloc(ctx->code, class_desc->prop_cnt*sizeof(*class_desc->props));
1787 if(!class_desc->props)
1788 return E_OUTOFMEMORY;
1790 for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next, i++) {
1791 if(lookup_class_funcs(class_desc, prop_decl->name)) {
1792 FIXME("Property %s redefined\n", debugstr_w(prop_decl->name));
1793 return E_FAIL;
1796 class_desc->props[i].name = compiler_alloc_string(ctx->code, prop_decl->name);
1797 if(!class_desc->props[i].name)
1798 return E_OUTOFMEMORY;
1800 class_desc->props[i].is_public = prop_decl->is_public;
1801 class_desc->props[i].is_array = prop_decl->is_array;
1803 if(prop_decl->is_array)
1804 class_desc->array_cnt++;
1807 if(class_desc->array_cnt) {
1808 class_desc->array_descs = compiler_alloc(ctx->code, class_desc->array_cnt*sizeof(*class_desc->array_descs));
1809 if(!class_desc->array_descs)
1810 return E_OUTOFMEMORY;
1812 for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next) {
1813 if(prop_decl->is_array) {
1814 hres = fill_array_desc(ctx, prop_decl, class_desc->array_descs + i++);
1815 if(FAILED(hres))
1816 return hres;
1821 class_desc->next = ctx->code->classes;
1822 ctx->code->classes = class_desc;
1823 return S_OK;
1826 static BOOL lookup_script_identifier(compile_ctx_t *ctx, script_ctx_t *script, const WCHAR *identifier)
1828 ScriptDisp *contexts[] = {
1829 ctx->code->named_item ? ctx->code->named_item->script_obj : NULL,
1830 script->script_obj
1832 class_desc_t *class;
1833 vbscode_t *code;
1834 unsigned c, i;
1836 for(c = 0; c < ARRAY_SIZE(contexts); c++) {
1837 if(!contexts[c]) continue;
1839 for(i = 0; i < contexts[c]->global_vars_cnt; i++) {
1840 if(!wcsicmp(contexts[c]->global_vars[i]->name, identifier))
1841 return TRUE;
1844 for(i = 0; i < contexts[c]->global_funcs_cnt; i++) {
1845 if(!wcsicmp(contexts[c]->global_funcs[i]->name, identifier))
1846 return TRUE;
1849 for(class = contexts[c]->classes; class; class = class->next) {
1850 if(!wcsicmp(class->name, identifier))
1851 return TRUE;
1855 LIST_FOR_EACH_ENTRY(code, &script->code_list, vbscode_t, entry) {
1856 unsigned var_cnt = code->main_code.var_cnt;
1857 var_desc_t *vars = code->main_code.vars;
1858 function_t *func;
1860 if(!code->pending_exec || (code->named_item && code->named_item != ctx->code->named_item))
1861 continue;
1863 for(i = 0; i < var_cnt; i++) {
1864 if(!wcsicmp(vars[i].name, identifier))
1865 return TRUE;
1868 for(func = code->funcs; func; func = func->next) {
1869 if(!wcsicmp(func->name, identifier))
1870 return TRUE;
1873 for(class = code->classes; class; class = class->next) {
1874 if(!wcsicmp(class->name, identifier))
1875 return TRUE;
1879 return FALSE;
1882 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
1884 unsigned i, var_cnt = ctx->code->main_code.var_cnt;
1885 var_desc_t *vars = ctx->code->main_code.vars;
1886 class_desc_t *class;
1888 for(i = 0; i < var_cnt; i++) {
1889 if(lookup_script_identifier(ctx, script, vars[i].name)) {
1890 FIXME("%s: redefined\n", debugstr_w(vars[i].name));
1891 return E_FAIL;
1895 for(class = ctx->code->classes; class; class = class->next) {
1896 if(lookup_script_identifier(ctx, script, class->name)) {
1897 FIXME("%s: redefined\n", debugstr_w(class->name));
1898 return E_FAIL;
1902 return S_OK;
1905 void release_vbscode(vbscode_t *code)
1907 unsigned i;
1909 if(--code->ref)
1910 return;
1912 for(i=0; i < code->bstr_cnt; i++)
1913 SysFreeString(code->bstr_pool[i]);
1915 if(code->named_item)
1916 release_named_item(code->named_item);
1917 heap_pool_free(&code->heap);
1919 heap_free(code->bstr_pool);
1920 heap_free(code->source);
1921 heap_free(code->instrs);
1922 heap_free(code);
1925 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source, DWORD_PTR cookie, unsigned start_line)
1927 vbscode_t *ret;
1928 size_t len;
1930 len = source ? lstrlenW(source) : 0;
1931 if(len > INT32_MAX)
1932 return NULL;
1934 ret = heap_alloc_zero(sizeof(*ret));
1935 if(!ret)
1936 return NULL;
1938 ret->source = heap_alloc((len + 1) * sizeof(WCHAR));
1939 if(!ret->source) {
1940 heap_free(ret);
1941 return NULL;
1943 if(len)
1944 memcpy(ret->source, source, len * sizeof(WCHAR));
1945 ret->source[len] = 0;
1947 ret->cookie = cookie;
1948 ret->start_line = start_line;
1950 ret->instrs = heap_alloc(32*sizeof(instr_t));
1951 if(!ret->instrs) {
1952 release_vbscode(ret);
1953 return NULL;
1956 ctx->instr_cnt = 1;
1957 ctx->instr_size = 32;
1958 heap_pool_init(&ret->heap);
1960 ret->main_code.type = FUNC_GLOBAL;
1961 ret->main_code.code_ctx = ret;
1962 ret->ref = 1;
1964 list_init(&ret->entry);
1965 return ret;
1968 static void release_compiler(compile_ctx_t *ctx)
1970 parser_release(&ctx->parser);
1971 heap_free(ctx->labels);
1972 if(ctx->code)
1973 release_vbscode(ctx->code);
1976 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, const WCHAR *item_name, const WCHAR *delimiter,
1977 DWORD_PTR cookie, unsigned start_line, DWORD flags, vbscode_t **ret)
1979 function_decl_t *func_decl;
1980 named_item_t *item = NULL;
1981 class_decl_t *class_decl;
1982 function_t *new_func;
1983 compile_ctx_t ctx;
1984 vbscode_t *code;
1985 HRESULT hres;
1987 if(item_name) {
1988 item = lookup_named_item(script, item_name, 0);
1989 if(!item) {
1990 WARN("Unknown context %s\n", debugstr_w(item_name));
1991 return E_INVALIDARG;
1993 if(!item->script_obj) item = NULL;
1996 memset(&ctx, 0, sizeof(ctx));
1997 code = ctx.code = alloc_vbscode(&ctx, src, cookie, start_line);
1998 if(!ctx.code)
1999 return E_OUTOFMEMORY;
2000 if(item) {
2001 code->named_item = item;
2002 item->ref++;
2005 ctx.parser.lcid = script->lcid;
2006 hres = parse_script(&ctx.parser, code->source, delimiter, flags);
2007 if(FAILED(hres)) {
2008 if(ctx.parser.error_loc != -1)
2009 ctx.loc = ctx.parser.error_loc;
2010 hres = compile_error(script, &ctx, hres);
2011 release_vbscode(code);
2012 return hres;
2015 hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->main_code);
2016 if(FAILED(hres)) {
2017 hres = compile_error(script, &ctx, hres);
2018 release_compiler(&ctx);
2019 return hres;
2022 code->option_explicit = ctx.parser.option_explicit;
2023 ctx.global_consts = ctx.const_decls;
2024 code->option_explicit = ctx.parser.option_explicit;
2027 for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
2028 hres = create_function(&ctx, func_decl, &new_func);
2029 if(FAILED(hres)) {
2030 hres = compile_error(script, &ctx, hres);
2031 release_compiler(&ctx);
2032 return hres;
2035 new_func->next = ctx.code->funcs;
2036 ctx.code->funcs = new_func;
2039 for(class_decl = ctx.parser.class_decls; class_decl; class_decl = class_decl->next) {
2040 hres = compile_class(&ctx, class_decl);
2041 if(FAILED(hres)) {
2042 hres = compile_error(script, &ctx, hres);
2043 release_compiler(&ctx);
2044 return hres;
2048 hres = check_script_collisions(&ctx, script);
2049 if(FAILED(hres)) {
2050 hres = compile_error(script, &ctx, hres);
2051 release_compiler(&ctx);
2052 return hres;
2055 code->is_persistent = (flags & SCRIPTTEXT_ISPERSISTENT) != 0;
2057 if(TRACE_ON(vbscript_disas))
2058 dump_code(&ctx);
2060 ctx.code = NULL;
2061 release_compiler(&ctx);
2063 list_add_tail(&script->code_list, &code->entry);
2064 *ret = code;
2065 return S_OK;
2068 HRESULT compile_procedure(script_ctx_t *script, const WCHAR *src, const WCHAR *item_name, const WCHAR *delimiter,
2069 DWORD_PTR cookie, unsigned start_line, DWORD flags, class_desc_t **ret)
2071 class_desc_t *desc;
2072 vbscode_t *code;
2073 HRESULT hres;
2075 hres = compile_script(script, src, item_name, delimiter, cookie, start_line,
2076 flags & ~SCRIPTTEXT_ISPERSISTENT, &code);
2077 if(FAILED(hres))
2078 return hres;
2080 if(!(desc = compiler_alloc_zero(code, sizeof(*desc))))
2081 return E_OUTOFMEMORY;
2082 if(!(desc->funcs = compiler_alloc_zero(code, sizeof(*desc->funcs))))
2083 return E_OUTOFMEMORY;
2085 desc->ctx = script;
2086 desc->func_cnt = 1;
2087 desc->funcs->entries[VBDISP_CALLGET] = &code->main_code;
2089 *ret = desc;
2090 return S_OK;