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
21 WINE_DECLARE_DEBUG_CHANNEL(vbscript_disas
);
23 typedef struct _statement_ctx_t
{
26 unsigned while_end_label
;
27 unsigned for_end_label
;
29 struct _statement_ctx_t
*next
;
39 statement_ctx_t
*stat_ctx
;
45 unsigned sub_end_label
;
46 unsigned func_end_label
;
47 unsigned prop_end_label
;
49 dim_decl_t
*dim_decls
;
50 dim_decl_t
*dim_decls_tail
;
51 dynamic_var_t
*global_vars
;
53 const_decl_t
*const_decls
;
54 const_decl_t
*global_consts
;
58 function_decl_t
*func_decls
;
60 class_desc_t
*classes
;
63 static HRESULT
compile_expression(compile_ctx_t
*,expression_t
*);
64 static HRESULT
compile_statement(compile_ctx_t
*,statement_ctx_t
*,statement_t
*);
68 instr_arg_type_t arg1_type
;
69 instr_arg_type_t arg2_type
;
71 #define X(n,a,b,c) {#n,b,c},
76 static void dump_instr_arg(instr_arg_type_t type
, instr_arg_t
*arg
)
81 TRACE_(vbscript_disas
)("\t%s", debugstr_w(arg
->str
));
84 TRACE_(vbscript_disas
)("\t%d", arg
->uint
);
88 TRACE_(vbscript_disas
)("\t%u", arg
->uint
);
91 TRACE_(vbscript_disas
)("\t%lf", *arg
->dbl
);
99 static void dump_code(compile_ctx_t
*ctx
)
103 for(instr
= ctx
->code
->instrs
+1; instr
< ctx
->code
->instrs
+ctx
->instr_cnt
; instr
++) {
104 assert(instr
->op
< OP_LAST
);
105 TRACE_(vbscript_disas
)("%d:\t%s", (int)(instr
-ctx
->code
->instrs
), instr_info
[instr
->op
].op_str
);
106 dump_instr_arg(instr_info
[instr
->op
].arg1_type
, &instr
->arg1
);
107 dump_instr_arg(instr_info
[instr
->op
].arg2_type
, &instr
->arg2
);
108 TRACE_(vbscript_disas
)("\n");
112 static inline void *compiler_alloc(vbscode_t
*vbscode
, size_t size
)
114 return heap_pool_alloc(&vbscode
->heap
, size
);
117 static inline void *compiler_alloc_zero(vbscode_t
*vbscode
, size_t size
)
121 ret
= heap_pool_alloc(&vbscode
->heap
, size
);
123 memset(ret
, 0, size
);
127 static WCHAR
*compiler_alloc_string(vbscode_t
*vbscode
, const WCHAR
*str
)
132 size
= (strlenW(str
)+1)*sizeof(WCHAR
);
133 ret
= compiler_alloc(vbscode
, size
);
135 memcpy(ret
, str
, size
);
139 static inline instr_t
*instr_ptr(compile_ctx_t
*ctx
, unsigned id
)
141 assert(id
< ctx
->instr_cnt
);
142 return ctx
->code
->instrs
+ id
;
145 static unsigned push_instr(compile_ctx_t
*ctx
, vbsop_t op
)
147 assert(ctx
->instr_size
&& ctx
->instr_size
>= ctx
->instr_cnt
);
149 if(ctx
->instr_size
== ctx
->instr_cnt
) {
152 new_instr
= heap_realloc(ctx
->code
->instrs
, ctx
->instr_size
*2*sizeof(instr_t
));
156 ctx
->code
->instrs
= new_instr
;
157 ctx
->instr_size
*= 2;
160 ctx
->code
->instrs
[ctx
->instr_cnt
].op
= op
;
161 return ctx
->instr_cnt
++;
164 static HRESULT
push_instr_int(compile_ctx_t
*ctx
, vbsop_t op
, LONG arg
)
168 ret
= push_instr(ctx
, op
);
170 return E_OUTOFMEMORY
;
172 instr_ptr(ctx
, ret
)->arg1
.lng
= arg
;
176 static HRESULT
push_instr_uint(compile_ctx_t
*ctx
, vbsop_t op
, unsigned arg
)
180 ret
= push_instr(ctx
, op
);
182 return E_OUTOFMEMORY
;
184 instr_ptr(ctx
, ret
)->arg1
.uint
= arg
;
188 static HRESULT
push_instr_addr(compile_ctx_t
*ctx
, vbsop_t op
, unsigned arg
)
192 ret
= push_instr(ctx
, op
);
194 return E_OUTOFMEMORY
;
196 instr_ptr(ctx
, ret
)->arg1
.uint
= arg
;
200 static HRESULT
push_instr_str(compile_ctx_t
*ctx
, vbsop_t op
, const WCHAR
*arg
)
205 str
= compiler_alloc_string(ctx
->code
, arg
);
207 return E_OUTOFMEMORY
;
209 instr
= push_instr(ctx
, op
);
211 return E_OUTOFMEMORY
;
213 instr_ptr(ctx
, instr
)->arg1
.str
= str
;
217 static HRESULT
push_instr_double(compile_ctx_t
*ctx
, vbsop_t op
, double arg
)
222 d
= compiler_alloc(ctx
->code
, sizeof(double));
224 return E_OUTOFMEMORY
;
226 instr
= push_instr(ctx
, op
);
228 return E_OUTOFMEMORY
;
231 instr_ptr(ctx
, instr
)->arg1
.dbl
= d
;
235 static BSTR
alloc_bstr_arg(compile_ctx_t
*ctx
, const WCHAR
*str
)
237 if(!ctx
->code
->bstr_pool_size
) {
238 ctx
->code
->bstr_pool
= heap_alloc(8 * sizeof(BSTR
));
239 if(!ctx
->code
->bstr_pool
)
241 ctx
->code
->bstr_pool_size
= 8;
242 }else if(ctx
->code
->bstr_pool_size
== ctx
->code
->bstr_cnt
) {
245 new_pool
= heap_realloc(ctx
->code
->bstr_pool
, ctx
->code
->bstr_pool_size
*2*sizeof(BSTR
));
249 ctx
->code
->bstr_pool
= new_pool
;
250 ctx
->code
->bstr_pool_size
*= 2;
253 ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
] = SysAllocString(str
);
254 if(!ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
])
257 return ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
++];
260 static HRESULT
push_instr_bstr(compile_ctx_t
*ctx
, vbsop_t op
, const WCHAR
*arg
)
265 bstr
= alloc_bstr_arg(ctx
, arg
);
267 return E_OUTOFMEMORY
;
269 instr
= push_instr(ctx
, op
);
271 return E_OUTOFMEMORY
;
273 instr_ptr(ctx
, instr
)->arg1
.bstr
= bstr
;
277 static HRESULT
push_instr_bstr_uint(compile_ctx_t
*ctx
, vbsop_t op
, const WCHAR
*arg1
, unsigned arg2
)
282 bstr
= alloc_bstr_arg(ctx
, arg1
);
284 return E_OUTOFMEMORY
;
286 instr
= push_instr(ctx
, op
);
288 return E_OUTOFMEMORY
;
290 instr_ptr(ctx
, instr
)->arg1
.bstr
= bstr
;
291 instr_ptr(ctx
, instr
)->arg2
.uint
= arg2
;
295 static HRESULT
push_instr_uint_bstr(compile_ctx_t
*ctx
, vbsop_t op
, unsigned arg1
, const WCHAR
*arg2
)
300 bstr
= alloc_bstr_arg(ctx
, arg2
);
302 return E_OUTOFMEMORY
;
304 instr
= push_instr(ctx
, op
);
306 return E_OUTOFMEMORY
;
308 instr_ptr(ctx
, instr
)->arg1
.uint
= arg1
;
309 instr_ptr(ctx
, instr
)->arg2
.bstr
= bstr
;
313 #define LABEL_FLAG 0x80000000
315 static unsigned alloc_label(compile_ctx_t
*ctx
)
317 if(!ctx
->labels_size
) {
318 ctx
->labels
= heap_alloc(8 * sizeof(*ctx
->labels
));
321 ctx
->labels_size
= 8;
322 }else if(ctx
->labels_size
== ctx
->labels_cnt
) {
323 unsigned *new_labels
;
325 new_labels
= heap_realloc(ctx
->labels
, 2*ctx
->labels_size
*sizeof(*ctx
->labels
));
329 ctx
->labels
= new_labels
;
330 ctx
->labels_size
*= 2;
333 return ctx
->labels_cnt
++ | LABEL_FLAG
;
336 static inline void label_set_addr(compile_ctx_t
*ctx
, unsigned label
)
338 assert(label
& LABEL_FLAG
);
339 ctx
->labels
[label
& ~LABEL_FLAG
] = ctx
->instr_cnt
;
342 static inline unsigned stack_offset(compile_ctx_t
*ctx
)
344 statement_ctx_t
*iter
;
347 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
)
348 ret
+= iter
->stack_use
;
353 static BOOL
emit_catch_jmp(compile_ctx_t
*ctx
, unsigned stack_off
, unsigned code_off
)
357 code
= push_instr(ctx
, OP_catch
);
361 instr_ptr(ctx
, code
)->arg1
.uint
= code_off
;
362 instr_ptr(ctx
, code
)->arg2
.uint
= stack_off
+ stack_offset(ctx
);
366 static inline BOOL
emit_catch(compile_ctx_t
*ctx
, unsigned off
)
368 return emit_catch_jmp(ctx
, off
, ctx
->instr_cnt
);
371 static expression_t
*lookup_const_decls(compile_ctx_t
*ctx
, const WCHAR
*name
, BOOL lookup_global
)
375 for(decl
= ctx
->const_decls
; decl
; decl
= decl
->next
) {
376 if(!strcmpiW(decl
->name
, name
))
377 return decl
->value_expr
;
383 for(decl
= ctx
->global_consts
; decl
; decl
= decl
->next
) {
384 if(!strcmpiW(decl
->name
, name
))
385 return decl
->value_expr
;
391 static HRESULT
compile_args(compile_ctx_t
*ctx
, expression_t
*args
, unsigned *ret
)
393 unsigned arg_cnt
= 0;
397 hres
= compile_expression(ctx
, args
);
409 static HRESULT
compile_member_expression(compile_ctx_t
*ctx
, member_expression_t
*expr
, BOOL ret_val
)
411 unsigned arg_cnt
= 0;
414 if(ret_val
&& !expr
->args
) {
415 expression_t
*const_expr
;
417 const_expr
= lookup_const_decls(ctx
, expr
->identifier
, TRUE
);
419 return compile_expression(ctx
, const_expr
);
422 hres
= compile_args(ctx
, expr
->args
, &arg_cnt
);
427 hres
= compile_expression(ctx
, expr
->obj_expr
);
431 hres
= push_instr_bstr_uint(ctx
, ret_val
? OP_mcall
: OP_mcallv
, expr
->identifier
, arg_cnt
);
433 hres
= push_instr_bstr_uint(ctx
, ret_val
? OP_icall
: OP_icallv
, expr
->identifier
, arg_cnt
);
439 static HRESULT
compile_unary_expression(compile_ctx_t
*ctx
, unary_expression_t
*expr
, vbsop_t op
)
443 hres
= compile_expression(ctx
, expr
->subexpr
);
447 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
450 static HRESULT
compile_binary_expression(compile_ctx_t
*ctx
, binary_expression_t
*expr
, vbsop_t op
)
454 hres
= compile_expression(ctx
, expr
->left
);
458 hres
= compile_expression(ctx
, expr
->right
);
462 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
465 static HRESULT
compile_expression(compile_ctx_t
*ctx
, expression_t
*expr
)
469 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_add
);
471 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_and
);
473 return push_instr_int(ctx
, OP_bool
, ((bool_expression_t
*)expr
)->value
);
475 return compile_expression(ctx
, ((unary_expression_t
*)expr
)->subexpr
);
477 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_concat
);
479 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_div
);
481 return push_instr_double(ctx
, OP_double
, ((double_expression_t
*)expr
)->value
);
483 return push_instr(ctx
, OP_empty
) ? S_OK
: E_OUTOFMEMORY
;
485 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_equal
);
487 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_eqv
);
489 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_exp
);
491 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gt
);
493 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gteq
);
495 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_idiv
);
497 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_is
);
499 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_imp
);
501 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lt
);
503 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lteq
);
505 return push_instr(ctx
, OP_me
) ? S_OK
: E_OUTOFMEMORY
;
507 return compile_member_expression(ctx
, (member_expression_t
*)expr
, TRUE
);
509 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mod
);
511 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mul
);
513 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_neg
);
515 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_nequal
);
517 return push_instr_str(ctx
, OP_new
, ((string_expression_t
*)expr
)->value
);
519 return push_instr_int(ctx
, OP_hres
, DISP_E_PARAMNOTFOUND
);
521 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_not
);
523 return push_instr(ctx
, OP_nothing
) ? S_OK
: E_OUTOFMEMORY
;
525 return push_instr(ctx
, OP_null
) ? S_OK
: E_OUTOFMEMORY
;
527 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_or
);
529 return push_instr_str(ctx
, OP_string
, ((string_expression_t
*)expr
)->value
);
531 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
533 return push_instr_int(ctx
, OP_short
, ((int_expression_t
*)expr
)->value
);
535 return push_instr_int(ctx
, OP_long
, ((int_expression_t
*)expr
)->value
);
537 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_xor
);
539 FIXME("Unimplemented expression type %d\n", expr
->type
);
546 static HRESULT
compile_if_statement(compile_ctx_t
*ctx
, if_statement_t
*stat
)
548 unsigned cnd_jmp
, endif_label
= 0;
549 elseif_decl_t
*elseif_decl
;
552 hres
= compile_expression(ctx
, stat
->expr
);
556 cnd_jmp
= push_instr(ctx
, OP_jmp_false
);
558 return E_OUTOFMEMORY
;
560 if(!emit_catch(ctx
, 0))
561 return E_OUTOFMEMORY
;
563 hres
= compile_statement(ctx
, NULL
, stat
->if_stat
);
567 if(stat
->else_stat
|| stat
->elseifs
) {
568 endif_label
= alloc_label(ctx
);
570 return E_OUTOFMEMORY
;
572 hres
= push_instr_addr(ctx
, OP_jmp
, endif_label
);
577 for(elseif_decl
= stat
->elseifs
; elseif_decl
; elseif_decl
= elseif_decl
->next
) {
578 instr_ptr(ctx
, cnd_jmp
)->arg1
.uint
= ctx
->instr_cnt
;
580 hres
= compile_expression(ctx
, elseif_decl
->expr
);
584 cnd_jmp
= push_instr(ctx
, OP_jmp_false
);
586 return E_OUTOFMEMORY
;
588 if(!emit_catch(ctx
, 0))
589 return E_OUTOFMEMORY
;
591 hres
= compile_statement(ctx
, NULL
, elseif_decl
->stat
);
595 hres
= push_instr_addr(ctx
, OP_jmp
, endif_label
);
600 instr_ptr(ctx
, cnd_jmp
)->arg1
.uint
= ctx
->instr_cnt
;
602 if(stat
->else_stat
) {
603 hres
= compile_statement(ctx
, NULL
, stat
->else_stat
);
609 label_set_addr(ctx
, endif_label
);
613 static HRESULT
compile_while_statement(compile_ctx_t
*ctx
, while_statement_t
*stat
)
615 statement_ctx_t stat_ctx
= {0}, *loop_ctx
;
620 start_addr
= ctx
->instr_cnt
;
622 hres
= compile_expression(ctx
, stat
->expr
);
626 jmp_end
= push_instr(ctx
, stat
->stat
.type
== STAT_UNTIL
? OP_jmp_true
: OP_jmp_false
);
628 return E_OUTOFMEMORY
;
630 if(!emit_catch(ctx
, 0))
631 return E_OUTOFMEMORY
;
633 if(stat
->stat
.type
== STAT_WHILE
) {
636 if(!(stat_ctx
.while_end_label
= alloc_label(ctx
)))
637 return E_OUTOFMEMORY
;
638 loop_ctx
= &stat_ctx
;
641 hres
= compile_statement(ctx
, loop_ctx
, stat
->body
);
645 hres
= push_instr_addr(ctx
, OP_jmp
, start_addr
);
649 instr_ptr(ctx
, jmp_end
)->arg1
.uint
= ctx
->instr_cnt
;
652 label_set_addr(ctx
, stat_ctx
.while_end_label
);
657 static HRESULT
compile_dowhile_statement(compile_ctx_t
*ctx
, while_statement_t
*stat
)
659 statement_ctx_t loop_ctx
= {0};
664 start_addr
= ctx
->instr_cnt
;
666 if(!(loop_ctx
.while_end_label
= alloc_label(ctx
)))
667 return E_OUTOFMEMORY
;
669 hres
= compile_statement(ctx
, &loop_ctx
, stat
->body
);
674 hres
= compile_expression(ctx
, stat
->expr
);
678 jmp_op
= stat
->stat
.type
== STAT_DOUNTIL
? OP_jmp_false
: OP_jmp_true
;
683 hres
= push_instr_addr(ctx
, jmp_op
, start_addr
);
687 label_set_addr(ctx
, loop_ctx
.while_end_label
);
689 if(!emit_catch(ctx
, 0))
690 return E_OUTOFMEMORY
;
695 static HRESULT
compile_foreach_statement(compile_ctx_t
*ctx
, foreach_statement_t
*stat
)
697 statement_ctx_t loop_ctx
= {1};
701 /* Preserve a place on the stack in case we throw before having proper enum collection. */
702 if(!push_instr(ctx
, OP_empty
))
703 return E_OUTOFMEMORY
;
705 hres
= compile_expression(ctx
, stat
->group_expr
);
709 if(!push_instr(ctx
, OP_newenum
))
710 return E_OUTOFMEMORY
;
712 if(!(loop_ctx
.for_end_label
= alloc_label(ctx
)))
713 return E_OUTOFMEMORY
;
715 hres
= push_instr_uint_bstr(ctx
, OP_enumnext
, loop_ctx
.for_end_label
, stat
->identifier
);
719 if(!emit_catch(ctx
, 1))
720 return E_OUTOFMEMORY
;
722 loop_start
= ctx
->instr_cnt
;
723 hres
= compile_statement(ctx
, &loop_ctx
, stat
->body
);
727 /* We need a separated enumnext here, because we need to jump out of the loop on exception. */
728 hres
= push_instr_uint_bstr(ctx
, OP_enumnext
, loop_ctx
.for_end_label
, stat
->identifier
);
732 hres
= push_instr_addr(ctx
, OP_jmp
, loop_start
);
736 label_set_addr(ctx
, loop_ctx
.for_end_label
);
740 static HRESULT
compile_forto_statement(compile_ctx_t
*ctx
, forto_statement_t
*stat
)
742 statement_ctx_t loop_ctx
= {2};
743 unsigned step_instr
, instr
;
747 identifier
= alloc_bstr_arg(ctx
, stat
->identifier
);
749 return E_OUTOFMEMORY
;
751 hres
= compile_expression(ctx
, stat
->from_expr
);
755 /* FIXME: Assign should happen after both expressions evaluation. */
756 instr
= push_instr(ctx
, OP_assign_ident
);
758 return E_OUTOFMEMORY
;
759 instr_ptr(ctx
, instr
)->arg1
.bstr
= identifier
;
760 instr_ptr(ctx
, instr
)->arg2
.uint
= 0;
762 hres
= compile_expression(ctx
, stat
->to_expr
);
766 if(!push_instr(ctx
, OP_val
))
767 return E_OUTOFMEMORY
;
769 if(stat
->step_expr
) {
770 hres
= compile_expression(ctx
, stat
->step_expr
);
774 if(!push_instr(ctx
, OP_val
))
775 return E_OUTOFMEMORY
;
777 hres
= push_instr_int(ctx
, OP_short
, 1);
782 loop_ctx
.for_end_label
= alloc_label(ctx
);
783 if(!loop_ctx
.for_end_label
)
784 return E_OUTOFMEMORY
;
786 step_instr
= push_instr(ctx
, OP_step
);
788 return E_OUTOFMEMORY
;
789 instr_ptr(ctx
, step_instr
)->arg2
.bstr
= identifier
;
790 instr_ptr(ctx
, step_instr
)->arg1
.uint
= loop_ctx
.for_end_label
;
792 if(!emit_catch(ctx
, 2))
793 return E_OUTOFMEMORY
;
795 hres
= compile_statement(ctx
, &loop_ctx
, stat
->body
);
799 /* FIXME: Error handling can't be done compatible with native using OP_incc here. */
800 instr
= push_instr(ctx
, OP_incc
);
802 return E_OUTOFMEMORY
;
803 instr_ptr(ctx
, instr
)->arg1
.bstr
= identifier
;
805 hres
= push_instr_addr(ctx
, OP_jmp
, step_instr
);
809 hres
= push_instr_uint(ctx
, OP_pop
, 2);
813 label_set_addr(ctx
, loop_ctx
.for_end_label
);
815 /* FIXME: reconsider after OP_incc fixup. */
816 if(!emit_catch(ctx
, 0))
817 return E_OUTOFMEMORY
;
822 static HRESULT
compile_select_statement(compile_ctx_t
*ctx
, select_statement_t
*stat
)
824 unsigned end_label
, case_cnt
= 0, *case_labels
= NULL
, i
;
825 case_clausule_t
*case_iter
;
826 expression_t
*expr_iter
;
829 hres
= compile_expression(ctx
, stat
->expr
);
833 if(!push_instr(ctx
, OP_val
))
834 return E_OUTOFMEMORY
;
836 end_label
= alloc_label(ctx
);
838 return E_OUTOFMEMORY
;
840 if(!emit_catch_jmp(ctx
, 0, end_label
))
841 return E_OUTOFMEMORY
;
843 for(case_iter
= stat
->case_clausules
; case_iter
; case_iter
= case_iter
->next
)
847 case_labels
= heap_alloc(case_cnt
*sizeof(*case_labels
));
849 return E_OUTOFMEMORY
;
852 for(case_iter
= stat
->case_clausules
, i
=0; case_iter
; case_iter
= case_iter
->next
, i
++) {
853 case_labels
[i
] = alloc_label(ctx
);
854 if(!case_labels
[i
]) {
855 hres
= E_OUTOFMEMORY
;
862 for(expr_iter
= case_iter
->expr
; expr_iter
; expr_iter
= expr_iter
->next
) {
863 hres
= compile_expression(ctx
, expr_iter
);
867 hres
= push_instr_addr(ctx
, OP_case
, case_labels
[i
]);
871 if(!emit_catch_jmp(ctx
, 0, case_labels
[i
])) {
872 hres
= E_OUTOFMEMORY
;
879 heap_free(case_labels
);
883 hres
= push_instr_uint(ctx
, OP_pop
, 1);
885 heap_free(case_labels
);
889 hres
= push_instr_addr(ctx
, OP_jmp
, case_iter
? case_labels
[i
] : end_label
);
891 heap_free(case_labels
);
895 for(case_iter
= stat
->case_clausules
, i
=0; case_iter
; case_iter
= case_iter
->next
, i
++) {
896 label_set_addr(ctx
, case_labels
[i
]);
897 hres
= compile_statement(ctx
, NULL
, case_iter
->stat
);
904 hres
= push_instr_addr(ctx
, OP_jmp
, end_label
);
909 heap_free(case_labels
);
913 label_set_addr(ctx
, end_label
);
917 static HRESULT
compile_assignment(compile_ctx_t
*ctx
, member_expression_t
*member_expr
, expression_t
*value_expr
, BOOL is_set
)
923 if(member_expr
->obj_expr
) {
924 hres
= compile_expression(ctx
, member_expr
->obj_expr
);
928 op
= is_set
? OP_set_member
: OP_assign_member
;
930 op
= is_set
? OP_set_ident
: OP_assign_ident
;
933 hres
= compile_expression(ctx
, value_expr
);
937 hres
= compile_args(ctx
, member_expr
->args
, &args_cnt
);
941 hres
= push_instr_bstr_uint(ctx
, op
, member_expr
->identifier
, args_cnt
);
945 if(!emit_catch(ctx
, 0))
946 return E_OUTOFMEMORY
;
951 static HRESULT
compile_assign_statement(compile_ctx_t
*ctx
, assign_statement_t
*stat
, BOOL is_set
)
953 return compile_assignment(ctx
, stat
->member_expr
, stat
->value_expr
, is_set
);
956 static HRESULT
compile_call_statement(compile_ctx_t
*ctx
, call_statement_t
*stat
)
960 /* It's challenging for parser to distinguish parameterized assignment with one argument from call
961 * with equality expression argument, so we do it in compiler. */
962 if(!stat
->is_strict
&& stat
->expr
->args
&& !stat
->expr
->args
->next
&& stat
->expr
->args
->type
== EXPR_EQUAL
) {
963 binary_expression_t
*eqexpr
= (binary_expression_t
*)stat
->expr
->args
;
965 if(eqexpr
->left
->type
== EXPR_BRACKETS
) {
966 member_expression_t new_member
= *stat
->expr
;
968 WARN("converting call expr to assign expr\n");
970 new_member
.args
= ((unary_expression_t
*)eqexpr
->left
)->subexpr
;
971 return compile_assignment(ctx
, &new_member
, eqexpr
->right
, FALSE
);
975 hres
= compile_member_expression(ctx
, stat
->expr
, FALSE
);
979 if(!emit_catch(ctx
, 0))
980 return E_OUTOFMEMORY
;
985 static BOOL
lookup_dim_decls(compile_ctx_t
*ctx
, const WCHAR
*name
)
987 dim_decl_t
*dim_decl
;
989 for(dim_decl
= ctx
->dim_decls
; dim_decl
; dim_decl
= dim_decl
->next
) {
990 if(!strcmpiW(dim_decl
->name
, name
))
997 static BOOL
lookup_args_name(compile_ctx_t
*ctx
, const WCHAR
*name
)
1001 for(i
= 0; i
< ctx
->func
->arg_cnt
; i
++) {
1002 if(!strcmpiW(ctx
->func
->args
[i
].name
, name
))
1009 static HRESULT
compile_dim_statement(compile_ctx_t
*ctx
, dim_statement_t
*stat
)
1011 dim_decl_t
*dim_decl
= stat
->dim_decls
;
1014 if(lookup_dim_decls(ctx
, dim_decl
->name
) || lookup_args_name(ctx
, dim_decl
->name
)
1015 || lookup_const_decls(ctx
, dim_decl
->name
, FALSE
)) {
1016 FIXME("dim %s name redefined\n", debugstr_w(dim_decl
->name
));
1020 ctx
->func
->var_cnt
++;
1022 if(dim_decl
->is_array
) {
1023 HRESULT hres
= push_instr_bstr_uint(ctx
, OP_dim
, dim_decl
->name
, ctx
->func
->array_cnt
++);
1027 if(!emit_catch(ctx
, 0))
1028 return E_OUTOFMEMORY
;
1033 dim_decl
= dim_decl
->next
;
1036 if(ctx
->dim_decls_tail
)
1037 ctx
->dim_decls_tail
->next
= stat
->dim_decls
;
1039 ctx
->dim_decls
= stat
->dim_decls
;
1040 ctx
->dim_decls_tail
= dim_decl
;
1044 static HRESULT
compile_const_statement(compile_ctx_t
*ctx
, const_statement_t
*stat
)
1046 const_decl_t
*decl
, *next_decl
= stat
->decls
;
1051 if(lookup_const_decls(ctx
, decl
->name
, FALSE
) || lookup_args_name(ctx
, decl
->name
)
1052 || lookup_dim_decls(ctx
, decl
->name
)) {
1053 FIXME("%s redefined\n", debugstr_w(decl
->name
));
1057 if(ctx
->func
->type
== FUNC_GLOBAL
) {
1060 hres
= compile_expression(ctx
, decl
->value_expr
);
1064 hres
= push_instr_bstr(ctx
, OP_const
, decl
->name
);
1068 if(!emit_catch(ctx
, 0))
1069 return E_OUTOFMEMORY
;
1072 next_decl
= decl
->next
;
1073 decl
->next
= ctx
->const_decls
;
1074 ctx
->const_decls
= decl
;
1080 static HRESULT
compile_function_statement(compile_ctx_t
*ctx
, function_statement_t
*stat
)
1082 if(ctx
->func
!= &ctx
->code
->main_code
) {
1083 FIXME("Function is not in the global code\n");
1087 stat
->func_decl
->next
= ctx
->func_decls
;
1088 ctx
->func_decls
= stat
->func_decl
;
1092 static HRESULT
compile_exitdo_statement(compile_ctx_t
*ctx
)
1094 statement_ctx_t
*iter
;
1095 unsigned pop_cnt
= 0;
1097 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
1098 pop_cnt
+= iter
->stack_use
;
1099 if(iter
->while_end_label
)
1103 FIXME("Exit Do outside Do Loop\n");
1110 hres
= push_instr_uint(ctx
, OP_pop
, pop_cnt
);
1115 return push_instr_addr(ctx
, OP_jmp
, iter
->while_end_label
);
1118 static HRESULT
compile_exitfor_statement(compile_ctx_t
*ctx
)
1120 statement_ctx_t
*iter
;
1121 unsigned pop_cnt
= 0;
1123 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
1124 pop_cnt
+= iter
->stack_use
;
1125 if(iter
->for_end_label
)
1129 FIXME("Exit For outside For loop\n");
1136 hres
= push_instr_uint(ctx
, OP_pop
, pop_cnt
);
1141 return push_instr_addr(ctx
, OP_jmp
, iter
->for_end_label
);
1144 static HRESULT
exit_label(compile_ctx_t
*ctx
, unsigned jmp_label
)
1146 unsigned pop_cnt
= stack_offset(ctx
);
1151 hres
= push_instr_uint(ctx
, OP_pop
, pop_cnt
);
1156 return push_instr_addr(ctx
, OP_jmp
, jmp_label
);
1159 static HRESULT
compile_exitsub_statement(compile_ctx_t
*ctx
)
1161 if(!ctx
->sub_end_label
) {
1162 FIXME("Exit Sub outside Sub?\n");
1166 return exit_label(ctx
, ctx
->sub_end_label
);
1169 static HRESULT
compile_exitfunc_statement(compile_ctx_t
*ctx
)
1171 if(!ctx
->func_end_label
) {
1172 FIXME("Exit Function outside Function?\n");
1176 return exit_label(ctx
, ctx
->func_end_label
);
1179 static HRESULT
compile_exitprop_statement(compile_ctx_t
*ctx
)
1181 if(!ctx
->prop_end_label
) {
1182 FIXME("Exit Property outside Property?\n");
1186 return exit_label(ctx
, ctx
->prop_end_label
);
1189 static HRESULT
compile_onerror_statement(compile_ctx_t
*ctx
, onerror_statement_t
*stat
)
1191 return push_instr_int(ctx
, OP_errmode
, stat
->resume_next
);
1194 static HRESULT
compile_statement(compile_ctx_t
*ctx
, statement_ctx_t
*stat_ctx
, statement_t
*stat
)
1199 stat_ctx
->next
= ctx
->stat_ctx
;
1200 ctx
->stat_ctx
= stat_ctx
;
1204 switch(stat
->type
) {
1206 hres
= compile_assign_statement(ctx
, (assign_statement_t
*)stat
, FALSE
);
1209 hres
= compile_call_statement(ctx
, (call_statement_t
*)stat
);
1212 hres
= compile_const_statement(ctx
, (const_statement_t
*)stat
);
1215 hres
= compile_dim_statement(ctx
, (dim_statement_t
*)stat
);
1219 hres
= compile_dowhile_statement(ctx
, (while_statement_t
*)stat
);
1222 hres
= compile_exitdo_statement(ctx
);
1225 hres
= compile_exitfor_statement(ctx
);
1228 hres
= compile_exitfunc_statement(ctx
);
1231 hres
= compile_exitprop_statement(ctx
);
1234 hres
= compile_exitsub_statement(ctx
);
1237 hres
= compile_foreach_statement(ctx
, (foreach_statement_t
*)stat
);
1240 hres
= compile_forto_statement(ctx
, (forto_statement_t
*)stat
);
1243 hres
= compile_function_statement(ctx
, (function_statement_t
*)stat
);
1246 hres
= compile_if_statement(ctx
, (if_statement_t
*)stat
);
1249 hres
= compile_onerror_statement(ctx
, (onerror_statement_t
*)stat
);
1252 hres
= compile_select_statement(ctx
, (select_statement_t
*)stat
);
1255 hres
= compile_assign_statement(ctx
, (assign_statement_t
*)stat
, TRUE
);
1258 hres
= push_instr(ctx
, OP_stop
) ? S_OK
: E_OUTOFMEMORY
;
1262 case STAT_WHILELOOP
:
1263 hres
= compile_while_statement(ctx
, (while_statement_t
*)stat
);
1266 FIXME("Unimplemented statement type %d\n", stat
->type
);
1276 assert(ctx
->stat_ctx
== stat_ctx
);
1277 ctx
->stat_ctx
= stat_ctx
->next
;
1283 static void resolve_labels(compile_ctx_t
*ctx
, unsigned off
)
1287 for(instr
= ctx
->code
->instrs
+off
; instr
< ctx
->code
->instrs
+ctx
->instr_cnt
; instr
++) {
1288 if(instr_info
[instr
->op
].arg1_type
== ARG_ADDR
&& (instr
->arg1
.uint
& LABEL_FLAG
)) {
1289 assert((instr
->arg1
.uint
& ~LABEL_FLAG
) < ctx
->labels_cnt
);
1290 instr
->arg1
.uint
= ctx
->labels
[instr
->arg1
.uint
& ~LABEL_FLAG
];
1292 assert(instr_info
[instr
->op
].arg2_type
!= ARG_ADDR
);
1295 ctx
->labels_cnt
= 0;
1298 static HRESULT
fill_array_desc(compile_ctx_t
*ctx
, dim_decl_t
*dim_decl
, array_desc_t
*array_desc
)
1300 unsigned dim_cnt
= 0, i
;
1303 for(iter
= dim_decl
->dims
; iter
; iter
= iter
->next
)
1306 array_desc
->bounds
= compiler_alloc(ctx
->code
, dim_cnt
* sizeof(SAFEARRAYBOUND
));
1307 if(!array_desc
->bounds
)
1308 return E_OUTOFMEMORY
;
1310 array_desc
->dim_cnt
= dim_cnt
;
1312 for(iter
= dim_decl
->dims
, i
=0; iter
; iter
= iter
->next
, i
++) {
1313 array_desc
->bounds
[i
].cElements
= iter
->val
+1;
1314 array_desc
->bounds
[i
].lLbound
= 0;
1320 static HRESULT
compile_func(compile_ctx_t
*ctx
, statement_t
*stat
, function_t
*func
)
1324 func
->code_off
= ctx
->instr_cnt
;
1326 ctx
->sub_end_label
= 0;
1327 ctx
->func_end_label
= 0;
1328 ctx
->prop_end_label
= 0;
1330 switch(func
->type
) {
1332 ctx
->func_end_label
= alloc_label(ctx
);
1333 if(!ctx
->func_end_label
)
1334 return E_OUTOFMEMORY
;
1337 ctx
->sub_end_label
= alloc_label(ctx
);
1338 if(!ctx
->sub_end_label
)
1339 return E_OUTOFMEMORY
;
1345 ctx
->prop_end_label
= alloc_label(ctx
);
1346 if(!ctx
->prop_end_label
)
1347 return E_OUTOFMEMORY
;
1354 ctx
->dim_decls
= ctx
->dim_decls_tail
= NULL
;
1355 ctx
->const_decls
= NULL
;
1356 hres
= compile_statement(ctx
, NULL
, stat
);
1361 if(ctx
->sub_end_label
)
1362 label_set_addr(ctx
, ctx
->sub_end_label
);
1363 if(ctx
->func_end_label
)
1364 label_set_addr(ctx
, ctx
->func_end_label
);
1365 if(ctx
->prop_end_label
)
1366 label_set_addr(ctx
, ctx
->prop_end_label
);
1368 if(!push_instr(ctx
, OP_ret
))
1369 return E_OUTOFMEMORY
;
1371 resolve_labels(ctx
, func
->code_off
);
1374 dim_decl_t
*dim_decl
;
1376 if(func
->type
== FUNC_GLOBAL
) {
1377 dynamic_var_t
*new_var
;
1381 for(dim_decl
= ctx
->dim_decls
; dim_decl
; dim_decl
= dim_decl
->next
) {
1382 new_var
= compiler_alloc(ctx
->code
, sizeof(*new_var
));
1384 return E_OUTOFMEMORY
;
1386 new_var
->name
= compiler_alloc_string(ctx
->code
, dim_decl
->name
);
1388 return E_OUTOFMEMORY
;
1390 V_VT(&new_var
->v
) = VT_EMPTY
;
1391 new_var
->is_const
= FALSE
;
1393 new_var
->next
= ctx
->global_vars
;
1394 ctx
->global_vars
= new_var
;
1399 func
->vars
= compiler_alloc(ctx
->code
, func
->var_cnt
* sizeof(var_desc_t
));
1401 return E_OUTOFMEMORY
;
1403 for(dim_decl
= ctx
->dim_decls
, i
=0; dim_decl
; dim_decl
= dim_decl
->next
, i
++) {
1404 func
->vars
[i
].name
= compiler_alloc_string(ctx
->code
, dim_decl
->name
);
1405 if(!func
->vars
[i
].name
)
1406 return E_OUTOFMEMORY
;
1409 assert(i
== func
->var_cnt
);
1413 if(func
->array_cnt
) {
1414 unsigned array_id
= 0;
1415 dim_decl_t
*dim_decl
;
1417 func
->array_descs
= compiler_alloc(ctx
->code
, func
->array_cnt
* sizeof(array_desc_t
));
1418 if(!func
->array_descs
)
1419 return E_OUTOFMEMORY
;
1421 for(dim_decl
= ctx
->dim_decls
; dim_decl
; dim_decl
= dim_decl
->next
) {
1422 if(dim_decl
->is_array
) {
1423 hres
= fill_array_desc(ctx
, dim_decl
, func
->array_descs
+ array_id
++);
1429 assert(array_id
== func
->array_cnt
);
1435 static BOOL
lookup_funcs_name(compile_ctx_t
*ctx
, const WCHAR
*name
)
1439 for(iter
= ctx
->funcs
; iter
; iter
= iter
->next
) {
1440 if(!strcmpiW(iter
->name
, name
))
1447 static HRESULT
create_function(compile_ctx_t
*ctx
, function_decl_t
*decl
, function_t
**ret
)
1452 if(lookup_dim_decls(ctx
, decl
->name
) || lookup_funcs_name(ctx
, decl
->name
) || lookup_const_decls(ctx
, decl
->name
, FALSE
)) {
1453 FIXME("%s: redefinition\n", debugstr_w(decl
->name
));
1457 func
= compiler_alloc(ctx
->code
, sizeof(*func
));
1459 return E_OUTOFMEMORY
;
1461 func
->name
= compiler_alloc_string(ctx
->code
, decl
->name
);
1463 return E_OUTOFMEMORY
;
1467 func
->array_cnt
= 0;
1468 func
->code_ctx
= ctx
->code
;
1469 func
->type
= decl
->type
;
1470 func
->is_public
= decl
->is_public
;
1477 for(arg
= decl
->args
; arg
; arg
= arg
->next
)
1480 func
->args
= compiler_alloc(ctx
->code
, func
->arg_cnt
* sizeof(arg_desc_t
));
1482 return E_OUTOFMEMORY
;
1484 for(i
= 0, arg
= decl
->args
; arg
; arg
= arg
->next
, i
++) {
1485 func
->args
[i
].name
= compiler_alloc_string(ctx
->code
, arg
->name
);
1486 if(!func
->args
[i
].name
)
1487 return E_OUTOFMEMORY
;
1488 func
->args
[i
].by_ref
= arg
->by_ref
;
1494 hres
= compile_func(ctx
, decl
->body
, func
);
1502 static BOOL
lookup_class_name(compile_ctx_t
*ctx
, const WCHAR
*name
)
1506 for(iter
= ctx
->classes
; iter
; iter
= iter
->next
) {
1507 if(!strcmpiW(iter
->name
, name
))
1514 static HRESULT
create_class_funcprop(compile_ctx_t
*ctx
, function_decl_t
*func_decl
, vbdisp_funcprop_desc_t
*desc
)
1516 vbdisp_invoke_type_t invoke_type
;
1517 function_decl_t
*funcprop_decl
;
1520 desc
->name
= compiler_alloc_string(ctx
->code
, func_decl
->name
);
1522 return E_OUTOFMEMORY
;
1524 for(funcprop_decl
= func_decl
; funcprop_decl
; funcprop_decl
= funcprop_decl
->next_prop_func
) {
1525 switch(funcprop_decl
->type
) {
1530 invoke_type
= VBDISP_CALLGET
;
1533 invoke_type
= VBDISP_LET
;
1536 invoke_type
= VBDISP_SET
;
1538 DEFAULT_UNREACHABLE
;
1541 assert(!desc
->entries
[invoke_type
]);
1543 if(funcprop_decl
->is_public
)
1544 desc
->is_public
= TRUE
;
1546 hres
= create_function(ctx
, funcprop_decl
, desc
->entries
+invoke_type
);
1554 static BOOL
lookup_class_funcs(class_desc_t
*class_desc
, const WCHAR
*name
)
1558 for(i
=0; i
< class_desc
->func_cnt
; i
++) {
1559 if(class_desc
->funcs
[i
].name
&& !strcmpiW(class_desc
->funcs
[i
].name
, name
))
1566 static HRESULT
compile_class(compile_ctx_t
*ctx
, class_decl_t
*class_decl
)
1568 function_decl_t
*func_decl
, *func_prop_decl
;
1569 class_desc_t
*class_desc
;
1570 dim_decl_t
*prop_decl
;
1574 static const WCHAR class_initializeW
[] = {'c','l','a','s','s','_','i','n','i','t','i','a','l','i','z','e',0};
1575 static const WCHAR class_terminateW
[] = {'c','l','a','s','s','_','t','e','r','m','i','n','a','t','e',0};
1577 if(lookup_dim_decls(ctx
, class_decl
->name
) || lookup_funcs_name(ctx
, class_decl
->name
)
1578 || lookup_const_decls(ctx
, class_decl
->name
, FALSE
) || lookup_class_name(ctx
, class_decl
->name
)) {
1579 FIXME("%s: redefinition\n", debugstr_w(class_decl
->name
));
1583 class_desc
= compiler_alloc_zero(ctx
->code
, sizeof(*class_desc
));
1585 return E_OUTOFMEMORY
;
1587 class_desc
->name
= compiler_alloc_string(ctx
->code
, class_decl
->name
);
1588 if(!class_desc
->name
)
1589 return E_OUTOFMEMORY
;
1591 class_desc
->func_cnt
= 1; /* always allocate slot for default getter */
1593 for(func_decl
= class_decl
->funcs
; func_decl
; func_decl
= func_decl
->next
) {
1594 for(func_prop_decl
= func_decl
; func_prop_decl
; func_prop_decl
= func_prop_decl
->next_prop_func
) {
1595 if(func_prop_decl
->type
== FUNC_DEFGET
)
1599 class_desc
->func_cnt
++;
1602 class_desc
->funcs
= compiler_alloc(ctx
->code
, class_desc
->func_cnt
*sizeof(*class_desc
->funcs
));
1603 if(!class_desc
->funcs
)
1604 return E_OUTOFMEMORY
;
1605 memset(class_desc
->funcs
, 0, class_desc
->func_cnt
*sizeof(*class_desc
->funcs
));
1607 for(func_decl
= class_decl
->funcs
, i
=1; func_decl
; func_decl
= func_decl
->next
, i
++) {
1608 for(func_prop_decl
= func_decl
; func_prop_decl
; func_prop_decl
= func_prop_decl
->next_prop_func
) {
1609 if(func_prop_decl
->type
== FUNC_DEFGET
) {
1615 if(!strcmpiW(class_initializeW
, func_decl
->name
)) {
1616 if(func_decl
->type
!= FUNC_SUB
) {
1617 FIXME("class initializer is not sub\n");
1621 class_desc
->class_initialize_id
= i
;
1622 }else if(!strcmpiW(class_terminateW
, func_decl
->name
)) {
1623 if(func_decl
->type
!= FUNC_SUB
) {
1624 FIXME("class terminator is not sub\n");
1628 class_desc
->class_terminate_id
= i
;
1631 hres
= create_class_funcprop(ctx
, func_decl
, class_desc
->funcs
+ (func_prop_decl
? 0 : i
));
1636 for(prop_decl
= class_decl
->props
; prop_decl
; prop_decl
= prop_decl
->next
)
1637 class_desc
->prop_cnt
++;
1639 class_desc
->props
= compiler_alloc(ctx
->code
, class_desc
->prop_cnt
*sizeof(*class_desc
->props
));
1640 if(!class_desc
->props
)
1641 return E_OUTOFMEMORY
;
1643 for(prop_decl
= class_decl
->props
, i
=0; prop_decl
; prop_decl
= prop_decl
->next
, i
++) {
1644 if(lookup_class_funcs(class_desc
, prop_decl
->name
)) {
1645 FIXME("Property %s redefined\n", debugstr_w(prop_decl
->name
));
1649 class_desc
->props
[i
].name
= compiler_alloc_string(ctx
->code
, prop_decl
->name
);
1650 if(!class_desc
->props
[i
].name
)
1651 return E_OUTOFMEMORY
;
1653 class_desc
->props
[i
].is_public
= prop_decl
->is_public
;
1654 class_desc
->props
[i
].is_array
= prop_decl
->is_array
;
1656 if(prop_decl
->is_array
)
1657 class_desc
->array_cnt
++;
1660 if(class_desc
->array_cnt
) {
1661 class_desc
->array_descs
= compiler_alloc(ctx
->code
, class_desc
->array_cnt
*sizeof(*class_desc
->array_descs
));
1662 if(!class_desc
->array_descs
)
1663 return E_OUTOFMEMORY
;
1665 for(prop_decl
= class_decl
->props
, i
=0; prop_decl
; prop_decl
= prop_decl
->next
) {
1666 if(prop_decl
->is_array
) {
1667 hres
= fill_array_desc(ctx
, prop_decl
, class_desc
->array_descs
+ i
++);
1674 class_desc
->next
= ctx
->classes
;
1675 ctx
->classes
= class_desc
;
1679 static BOOL
lookup_script_identifier(script_ctx_t
*script
, const WCHAR
*identifier
)
1681 class_desc_t
*class;
1685 for(var
= script
->global_vars
; var
; var
= var
->next
) {
1686 if(!strcmpiW(var
->name
, identifier
))
1690 for(func
= script
->global_funcs
; func
; func
= func
->next
) {
1691 if(!strcmpiW(func
->name
, identifier
))
1695 for(class = script
->classes
; class; class = class->next
) {
1696 if(!strcmpiW(class->name
, identifier
))
1703 static HRESULT
check_script_collisions(compile_ctx_t
*ctx
, script_ctx_t
*script
)
1705 class_desc_t
*class;
1709 for(var
= ctx
->global_vars
; var
; var
= var
->next
) {
1710 if(lookup_script_identifier(script
, var
->name
)) {
1711 FIXME("%s: redefined\n", debugstr_w(var
->name
));
1716 for(func
= ctx
->funcs
; func
; func
= func
->next
) {
1717 if(lookup_script_identifier(script
, func
->name
)) {
1718 FIXME("%s: redefined\n", debugstr_w(func
->name
));
1723 for(class = ctx
->classes
; class; class = class->next
) {
1724 if(lookup_script_identifier(script
, class->name
)) {
1725 FIXME("%s: redefined\n", debugstr_w(class->name
));
1733 void release_vbscode(vbscode_t
*code
)
1737 list_remove(&code
->entry
);
1739 for(i
=0; i
< code
->bstr_cnt
; i
++)
1740 SysFreeString(code
->bstr_pool
[i
]);
1742 heap_pool_free(&code
->heap
);
1744 heap_free(code
->bstr_pool
);
1745 heap_free(code
->source
);
1746 heap_free(code
->instrs
);
1750 static vbscode_t
*alloc_vbscode(compile_ctx_t
*ctx
, const WCHAR
*source
)
1754 ret
= heap_alloc(sizeof(*ret
));
1758 ret
->source
= heap_strdupW(source
);
1764 ret
->instrs
= heap_alloc(32*sizeof(instr_t
));
1766 release_vbscode(ret
);
1771 ctx
->instr_size
= 32;
1772 heap_pool_init(&ret
->heap
);
1774 ret
->option_explicit
= ctx
->parser
.option_explicit
;
1776 ret
->bstr_pool
= NULL
;
1777 ret
->bstr_pool_size
= 0;
1779 ret
->pending_exec
= FALSE
;
1781 ret
->main_code
.type
= FUNC_GLOBAL
;
1782 ret
->main_code
.name
= NULL
;
1783 ret
->main_code
.code_ctx
= ret
;
1784 ret
->main_code
.vars
= NULL
;
1785 ret
->main_code
.var_cnt
= 0;
1786 ret
->main_code
.array_cnt
= 0;
1787 ret
->main_code
.arg_cnt
= 0;
1788 ret
->main_code
.args
= NULL
;
1790 list_init(&ret
->entry
);
1794 static void release_compiler(compile_ctx_t
*ctx
)
1796 parser_release(&ctx
->parser
);
1797 heap_free(ctx
->labels
);
1799 release_vbscode(ctx
->code
);
1802 HRESULT
compile_script(script_ctx_t
*script
, const WCHAR
*src
, const WCHAR
*delimiter
, vbscode_t
**ret
)
1804 function_t
*new_func
;
1805 function_decl_t
*func_decl
;
1806 class_decl_t
*class_decl
;
1811 hres
= parse_script(&ctx
.parser
, src
, delimiter
);
1815 code
= ctx
.code
= alloc_vbscode(&ctx
, src
);
1817 return E_OUTOFMEMORY
;
1820 ctx
.func_decls
= NULL
;
1821 ctx
.global_vars
= NULL
;
1824 ctx
.global_consts
= NULL
;
1825 ctx
.stat_ctx
= NULL
;
1826 ctx
.labels_cnt
= ctx
.labels_size
= 0;
1828 hres
= compile_func(&ctx
, ctx
.parser
.stats
, &ctx
.code
->main_code
);
1830 release_compiler(&ctx
);
1834 ctx
.global_consts
= ctx
.const_decls
;
1836 for(func_decl
= ctx
.func_decls
; func_decl
; func_decl
= func_decl
->next
) {
1837 hres
= create_function(&ctx
, func_decl
, &new_func
);
1839 release_compiler(&ctx
);
1843 new_func
->next
= ctx
.funcs
;
1844 ctx
.funcs
= new_func
;
1847 for(class_decl
= ctx
.parser
.class_decls
; class_decl
; class_decl
= class_decl
->next
) {
1848 hres
= compile_class(&ctx
, class_decl
);
1850 release_compiler(&ctx
);
1855 hres
= check_script_collisions(&ctx
, script
);
1857 release_compiler(&ctx
);
1861 if(ctx
.global_vars
) {
1864 for(var
= ctx
.global_vars
; var
->next
; var
= var
->next
);
1866 var
->next
= script
->global_vars
;
1867 script
->global_vars
= ctx
.global_vars
;
1871 for(new_func
= ctx
.funcs
; new_func
->next
; new_func
= new_func
->next
);
1873 new_func
->next
= script
->global_funcs
;
1874 script
->global_funcs
= ctx
.funcs
;
1878 class_desc_t
*class = ctx
.classes
;
1881 class->ctx
= script
;
1884 class = class->next
;
1887 class->next
= script
->classes
;
1888 script
->classes
= ctx
.classes
;
1891 if(TRACE_ON(vbscript_disas
))
1895 release_compiler(&ctx
);
1897 list_add_tail(&script
->code_list
, &code
->entry
);