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 compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_not
);
521 return push_instr(ctx
, OP_nothing
) ? S_OK
: E_OUTOFMEMORY
;
523 return push_instr(ctx
, OP_null
) ? S_OK
: E_OUTOFMEMORY
;
525 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_or
);
527 return push_instr_str(ctx
, OP_string
, ((string_expression_t
*)expr
)->value
);
529 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
531 return push_instr_int(ctx
, OP_short
, ((int_expression_t
*)expr
)->value
);
533 return push_instr_int(ctx
, OP_long
, ((int_expression_t
*)expr
)->value
);
535 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_xor
);
537 FIXME("Unimplemented expression type %d\n", expr
->type
);
544 static HRESULT
compile_if_statement(compile_ctx_t
*ctx
, if_statement_t
*stat
)
546 unsigned cnd_jmp
, endif_label
= 0;
547 elseif_decl_t
*elseif_decl
;
550 hres
= compile_expression(ctx
, stat
->expr
);
554 cnd_jmp
= push_instr(ctx
, OP_jmp_false
);
556 return E_OUTOFMEMORY
;
558 if(!emit_catch(ctx
, 0))
559 return E_OUTOFMEMORY
;
561 hres
= compile_statement(ctx
, NULL
, stat
->if_stat
);
565 if(stat
->else_stat
|| stat
->elseifs
) {
566 endif_label
= alloc_label(ctx
);
568 return E_OUTOFMEMORY
;
570 hres
= push_instr_addr(ctx
, OP_jmp
, endif_label
);
575 for(elseif_decl
= stat
->elseifs
; elseif_decl
; elseif_decl
= elseif_decl
->next
) {
576 instr_ptr(ctx
, cnd_jmp
)->arg1
.uint
= ctx
->instr_cnt
;
578 hres
= compile_expression(ctx
, elseif_decl
->expr
);
582 cnd_jmp
= push_instr(ctx
, OP_jmp_false
);
584 return E_OUTOFMEMORY
;
586 if(!emit_catch(ctx
, 0))
587 return E_OUTOFMEMORY
;
589 hres
= compile_statement(ctx
, NULL
, elseif_decl
->stat
);
593 hres
= push_instr_addr(ctx
, OP_jmp
, endif_label
);
598 instr_ptr(ctx
, cnd_jmp
)->arg1
.uint
= ctx
->instr_cnt
;
600 if(stat
->else_stat
) {
601 hres
= compile_statement(ctx
, NULL
, stat
->else_stat
);
607 label_set_addr(ctx
, endif_label
);
611 static HRESULT
compile_while_statement(compile_ctx_t
*ctx
, while_statement_t
*stat
)
613 statement_ctx_t stat_ctx
= {0}, *loop_ctx
;
618 start_addr
= ctx
->instr_cnt
;
620 hres
= compile_expression(ctx
, stat
->expr
);
624 jmp_end
= push_instr(ctx
, stat
->stat
.type
== STAT_UNTIL
? OP_jmp_true
: OP_jmp_false
);
626 return E_OUTOFMEMORY
;
628 if(!emit_catch(ctx
, 0))
629 return E_OUTOFMEMORY
;
631 if(stat
->stat
.type
== STAT_WHILE
) {
634 if(!(stat_ctx
.while_end_label
= alloc_label(ctx
)))
635 return E_OUTOFMEMORY
;
636 loop_ctx
= &stat_ctx
;
639 hres
= compile_statement(ctx
, loop_ctx
, stat
->body
);
643 hres
= push_instr_addr(ctx
, OP_jmp
, start_addr
);
647 instr_ptr(ctx
, jmp_end
)->arg1
.uint
= ctx
->instr_cnt
;
650 label_set_addr(ctx
, stat_ctx
.while_end_label
);
655 static HRESULT
compile_dowhile_statement(compile_ctx_t
*ctx
, while_statement_t
*stat
)
657 statement_ctx_t loop_ctx
= {0};
662 start_addr
= ctx
->instr_cnt
;
664 if(!(loop_ctx
.while_end_label
= alloc_label(ctx
)))
665 return E_OUTOFMEMORY
;
667 hres
= compile_statement(ctx
, &loop_ctx
, stat
->body
);
672 hres
= compile_expression(ctx
, stat
->expr
);
676 jmp_op
= stat
->stat
.type
== STAT_DOUNTIL
? OP_jmp_false
: OP_jmp_true
;
681 hres
= push_instr_addr(ctx
, jmp_op
, start_addr
);
685 label_set_addr(ctx
, loop_ctx
.while_end_label
);
687 if(!emit_catch(ctx
, 0))
688 return E_OUTOFMEMORY
;
693 static HRESULT
compile_foreach_statement(compile_ctx_t
*ctx
, foreach_statement_t
*stat
)
695 statement_ctx_t loop_ctx
= {1};
699 /* Preserve a place on the stack in case we throw before having proper enum collection. */
700 if(!push_instr(ctx
, OP_empty
))
701 return E_OUTOFMEMORY
;
703 hres
= compile_expression(ctx
, stat
->group_expr
);
707 if(!push_instr(ctx
, OP_newenum
))
708 return E_OUTOFMEMORY
;
710 if(!(loop_ctx
.for_end_label
= alloc_label(ctx
)))
711 return E_OUTOFMEMORY
;
713 hres
= push_instr_uint_bstr(ctx
, OP_enumnext
, loop_ctx
.for_end_label
, stat
->identifier
);
717 if(!emit_catch(ctx
, 1))
718 return E_OUTOFMEMORY
;
720 loop_start
= ctx
->instr_cnt
;
721 hres
= compile_statement(ctx
, &loop_ctx
, stat
->body
);
725 /* We need a separated enumnext here, because we need to jump out of the loop on exception. */
726 hres
= push_instr_uint_bstr(ctx
, OP_enumnext
, loop_ctx
.for_end_label
, stat
->identifier
);
730 hres
= push_instr_addr(ctx
, OP_jmp
, loop_start
);
734 label_set_addr(ctx
, loop_ctx
.for_end_label
);
738 static HRESULT
compile_forto_statement(compile_ctx_t
*ctx
, forto_statement_t
*stat
)
740 statement_ctx_t loop_ctx
= {2};
741 unsigned step_instr
, instr
;
745 identifier
= alloc_bstr_arg(ctx
, stat
->identifier
);
747 return E_OUTOFMEMORY
;
749 hres
= compile_expression(ctx
, stat
->from_expr
);
753 /* FIXME: Assign should happen after both expressions evaluation. */
754 instr
= push_instr(ctx
, OP_assign_ident
);
756 return E_OUTOFMEMORY
;
757 instr_ptr(ctx
, instr
)->arg1
.bstr
= identifier
;
758 instr_ptr(ctx
, instr
)->arg2
.uint
= 0;
760 hres
= compile_expression(ctx
, stat
->to_expr
);
764 if(!push_instr(ctx
, OP_val
))
765 return E_OUTOFMEMORY
;
767 if(stat
->step_expr
) {
768 hres
= compile_expression(ctx
, stat
->step_expr
);
772 if(!push_instr(ctx
, OP_val
))
773 return E_OUTOFMEMORY
;
775 hres
= push_instr_int(ctx
, OP_short
, 1);
780 loop_ctx
.for_end_label
= alloc_label(ctx
);
781 if(!loop_ctx
.for_end_label
)
782 return E_OUTOFMEMORY
;
784 step_instr
= push_instr(ctx
, OP_step
);
786 return E_OUTOFMEMORY
;
787 instr_ptr(ctx
, step_instr
)->arg2
.bstr
= identifier
;
788 instr_ptr(ctx
, step_instr
)->arg1
.uint
= loop_ctx
.for_end_label
;
790 if(!emit_catch(ctx
, 2))
791 return E_OUTOFMEMORY
;
793 hres
= compile_statement(ctx
, &loop_ctx
, stat
->body
);
797 /* FIXME: Error handling can't be done compatible with native using OP_incc here. */
798 instr
= push_instr(ctx
, OP_incc
);
800 return E_OUTOFMEMORY
;
801 instr_ptr(ctx
, instr
)->arg1
.bstr
= identifier
;
803 hres
= push_instr_addr(ctx
, OP_jmp
, step_instr
);
807 hres
= push_instr_uint(ctx
, OP_pop
, 2);
811 label_set_addr(ctx
, loop_ctx
.for_end_label
);
813 /* FIXME: reconsider after OP_incc fixup. */
814 if(!emit_catch(ctx
, 0))
815 return E_OUTOFMEMORY
;
820 static HRESULT
compile_select_statement(compile_ctx_t
*ctx
, select_statement_t
*stat
)
822 unsigned end_label
, case_cnt
= 0, *case_labels
= NULL
, i
;
823 case_clausule_t
*case_iter
;
824 expression_t
*expr_iter
;
827 hres
= compile_expression(ctx
, stat
->expr
);
831 if(!push_instr(ctx
, OP_val
))
832 return E_OUTOFMEMORY
;
834 end_label
= alloc_label(ctx
);
836 return E_OUTOFMEMORY
;
838 if(!emit_catch_jmp(ctx
, 0, end_label
))
839 return E_OUTOFMEMORY
;
841 for(case_iter
= stat
->case_clausules
; case_iter
; case_iter
= case_iter
->next
)
845 case_labels
= heap_alloc(case_cnt
*sizeof(*case_labels
));
847 return E_OUTOFMEMORY
;
850 for(case_iter
= stat
->case_clausules
, i
=0; case_iter
; case_iter
= case_iter
->next
, i
++) {
851 case_labels
[i
] = alloc_label(ctx
);
852 if(!case_labels
[i
]) {
853 hres
= E_OUTOFMEMORY
;
860 for(expr_iter
= case_iter
->expr
; expr_iter
; expr_iter
= expr_iter
->next
) {
861 hres
= compile_expression(ctx
, expr_iter
);
865 hres
= push_instr_addr(ctx
, OP_case
, case_labels
[i
]);
869 if(!emit_catch_jmp(ctx
, 0, case_labels
[i
])) {
870 hres
= E_OUTOFMEMORY
;
877 heap_free(case_labels
);
881 hres
= push_instr_uint(ctx
, OP_pop
, 1);
883 heap_free(case_labels
);
887 hres
= push_instr_addr(ctx
, OP_jmp
, case_iter
? case_labels
[i
] : end_label
);
889 heap_free(case_labels
);
893 for(case_iter
= stat
->case_clausules
, i
=0; case_iter
; case_iter
= case_iter
->next
, i
++) {
894 label_set_addr(ctx
, case_labels
[i
]);
895 hres
= compile_statement(ctx
, NULL
, case_iter
->stat
);
902 hres
= push_instr_addr(ctx
, OP_jmp
, end_label
);
907 heap_free(case_labels
);
911 label_set_addr(ctx
, end_label
);
915 static HRESULT
compile_assignment(compile_ctx_t
*ctx
, member_expression_t
*member_expr
, expression_t
*value_expr
, BOOL is_set
)
921 if(member_expr
->obj_expr
) {
922 hres
= compile_expression(ctx
, member_expr
->obj_expr
);
926 op
= is_set
? OP_set_member
: OP_assign_member
;
928 op
= is_set
? OP_set_ident
: OP_assign_ident
;
931 hres
= compile_expression(ctx
, value_expr
);
935 hres
= compile_args(ctx
, member_expr
->args
, &args_cnt
);
939 hres
= push_instr_bstr_uint(ctx
, op
, member_expr
->identifier
, args_cnt
);
943 if(!emit_catch(ctx
, 0))
944 return E_OUTOFMEMORY
;
949 static HRESULT
compile_assign_statement(compile_ctx_t
*ctx
, assign_statement_t
*stat
, BOOL is_set
)
951 return compile_assignment(ctx
, stat
->member_expr
, stat
->value_expr
, is_set
);
954 static HRESULT
compile_call_statement(compile_ctx_t
*ctx
, call_statement_t
*stat
)
958 /* It's challenging for parser to distinguish parameterized assignment with one argument from call
959 * with equality expression argument, so we do it in compiler. */
960 if(!stat
->is_strict
&& stat
->expr
->args
&& !stat
->expr
->args
->next
&& stat
->expr
->args
->type
== EXPR_EQUAL
) {
961 binary_expression_t
*eqexpr
= (binary_expression_t
*)stat
->expr
->args
;
963 if(eqexpr
->left
->type
== EXPR_BRACKETS
) {
964 member_expression_t new_member
= *stat
->expr
;
966 WARN("converting call expr to assign expr\n");
968 new_member
.args
= ((unary_expression_t
*)eqexpr
->left
)->subexpr
;
969 return compile_assignment(ctx
, &new_member
, eqexpr
->right
, FALSE
);
973 hres
= compile_member_expression(ctx
, stat
->expr
, FALSE
);
977 if(!emit_catch(ctx
, 0))
978 return E_OUTOFMEMORY
;
983 static BOOL
lookup_dim_decls(compile_ctx_t
*ctx
, const WCHAR
*name
)
985 dim_decl_t
*dim_decl
;
987 for(dim_decl
= ctx
->dim_decls
; dim_decl
; dim_decl
= dim_decl
->next
) {
988 if(!strcmpiW(dim_decl
->name
, name
))
995 static BOOL
lookup_args_name(compile_ctx_t
*ctx
, const WCHAR
*name
)
999 for(i
= 0; i
< ctx
->func
->arg_cnt
; i
++) {
1000 if(!strcmpiW(ctx
->func
->args
[i
].name
, name
))
1007 static HRESULT
compile_dim_statement(compile_ctx_t
*ctx
, dim_statement_t
*stat
)
1009 dim_decl_t
*dim_decl
= stat
->dim_decls
;
1012 if(lookup_dim_decls(ctx
, dim_decl
->name
) || lookup_args_name(ctx
, dim_decl
->name
)
1013 || lookup_const_decls(ctx
, dim_decl
->name
, FALSE
)) {
1014 FIXME("dim %s name redefined\n", debugstr_w(dim_decl
->name
));
1018 ctx
->func
->var_cnt
++;
1020 if(dim_decl
->is_array
) {
1021 HRESULT hres
= push_instr_bstr_uint(ctx
, OP_dim
, dim_decl
->name
, ctx
->func
->array_cnt
++);
1025 if(!emit_catch(ctx
, 0))
1026 return E_OUTOFMEMORY
;
1031 dim_decl
= dim_decl
->next
;
1034 if(ctx
->dim_decls_tail
)
1035 ctx
->dim_decls_tail
->next
= stat
->dim_decls
;
1037 ctx
->dim_decls
= stat
->dim_decls
;
1038 ctx
->dim_decls_tail
= dim_decl
;
1042 static HRESULT
compile_const_statement(compile_ctx_t
*ctx
, const_statement_t
*stat
)
1044 const_decl_t
*decl
, *next_decl
= stat
->decls
;
1049 if(lookup_const_decls(ctx
, decl
->name
, FALSE
) || lookup_args_name(ctx
, decl
->name
)
1050 || lookup_dim_decls(ctx
, decl
->name
)) {
1051 FIXME("%s redefined\n", debugstr_w(decl
->name
));
1055 if(ctx
->func
->type
== FUNC_GLOBAL
) {
1058 hres
= compile_expression(ctx
, decl
->value_expr
);
1062 hres
= push_instr_bstr(ctx
, OP_const
, decl
->name
);
1066 if(!emit_catch(ctx
, 0))
1067 return E_OUTOFMEMORY
;
1070 next_decl
= decl
->next
;
1071 decl
->next
= ctx
->const_decls
;
1072 ctx
->const_decls
= decl
;
1078 static HRESULT
compile_function_statement(compile_ctx_t
*ctx
, function_statement_t
*stat
)
1080 if(ctx
->func
!= &ctx
->code
->main_code
) {
1081 FIXME("Function is not in the global code\n");
1085 stat
->func_decl
->next
= ctx
->func_decls
;
1086 ctx
->func_decls
= stat
->func_decl
;
1090 static HRESULT
compile_exitdo_statement(compile_ctx_t
*ctx
)
1092 statement_ctx_t
*iter
;
1093 unsigned pop_cnt
= 0;
1095 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
1096 pop_cnt
+= iter
->stack_use
;
1097 if(iter
->while_end_label
)
1101 FIXME("Exit Do outside Do Loop\n");
1108 hres
= push_instr_uint(ctx
, OP_pop
, pop_cnt
);
1113 return push_instr_addr(ctx
, OP_jmp
, iter
->while_end_label
);
1116 static HRESULT
compile_exitfor_statement(compile_ctx_t
*ctx
)
1118 statement_ctx_t
*iter
;
1119 unsigned pop_cnt
= 0;
1121 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
1122 pop_cnt
+= iter
->stack_use
;
1123 if(iter
->for_end_label
)
1127 FIXME("Exit For outside For loop\n");
1134 hres
= push_instr_uint(ctx
, OP_pop
, pop_cnt
);
1139 return push_instr_addr(ctx
, OP_jmp
, iter
->for_end_label
);
1142 static HRESULT
exit_label(compile_ctx_t
*ctx
, unsigned jmp_label
)
1144 unsigned pop_cnt
= stack_offset(ctx
);
1149 hres
= push_instr_uint(ctx
, OP_pop
, pop_cnt
);
1154 return push_instr_addr(ctx
, OP_jmp
, jmp_label
);
1157 static HRESULT
compile_exitsub_statement(compile_ctx_t
*ctx
)
1159 if(!ctx
->sub_end_label
) {
1160 FIXME("Exit Sub outside Sub?\n");
1164 return exit_label(ctx
, ctx
->sub_end_label
);
1167 static HRESULT
compile_exitfunc_statement(compile_ctx_t
*ctx
)
1169 if(!ctx
->func_end_label
) {
1170 FIXME("Exit Function outside Function?\n");
1174 return exit_label(ctx
, ctx
->func_end_label
);
1177 static HRESULT
compile_exitprop_statement(compile_ctx_t
*ctx
)
1179 if(!ctx
->prop_end_label
) {
1180 FIXME("Exit Property outside Property?\n");
1184 return exit_label(ctx
, ctx
->prop_end_label
);
1187 static HRESULT
compile_onerror_statement(compile_ctx_t
*ctx
, onerror_statement_t
*stat
)
1189 return push_instr_int(ctx
, OP_errmode
, stat
->resume_next
);
1192 static HRESULT
compile_statement(compile_ctx_t
*ctx
, statement_ctx_t
*stat_ctx
, statement_t
*stat
)
1197 stat_ctx
->next
= ctx
->stat_ctx
;
1198 ctx
->stat_ctx
= stat_ctx
;
1202 switch(stat
->type
) {
1204 hres
= compile_assign_statement(ctx
, (assign_statement_t
*)stat
, FALSE
);
1207 hres
= compile_call_statement(ctx
, (call_statement_t
*)stat
);
1210 hres
= compile_const_statement(ctx
, (const_statement_t
*)stat
);
1213 hres
= compile_dim_statement(ctx
, (dim_statement_t
*)stat
);
1217 hres
= compile_dowhile_statement(ctx
, (while_statement_t
*)stat
);
1220 hres
= compile_exitdo_statement(ctx
);
1223 hres
= compile_exitfor_statement(ctx
);
1226 hres
= compile_exitfunc_statement(ctx
);
1229 hres
= compile_exitprop_statement(ctx
);
1232 hres
= compile_exitsub_statement(ctx
);
1235 hres
= compile_foreach_statement(ctx
, (foreach_statement_t
*)stat
);
1238 hres
= compile_forto_statement(ctx
, (forto_statement_t
*)stat
);
1241 hres
= compile_function_statement(ctx
, (function_statement_t
*)stat
);
1244 hres
= compile_if_statement(ctx
, (if_statement_t
*)stat
);
1247 hres
= compile_onerror_statement(ctx
, (onerror_statement_t
*)stat
);
1250 hres
= compile_select_statement(ctx
, (select_statement_t
*)stat
);
1253 hres
= compile_assign_statement(ctx
, (assign_statement_t
*)stat
, TRUE
);
1256 hres
= push_instr(ctx
, OP_stop
) ? S_OK
: E_OUTOFMEMORY
;
1260 case STAT_WHILELOOP
:
1261 hres
= compile_while_statement(ctx
, (while_statement_t
*)stat
);
1264 FIXME("Unimplemented statement type %d\n", stat
->type
);
1274 assert(ctx
->stat_ctx
== stat_ctx
);
1275 ctx
->stat_ctx
= stat_ctx
->next
;
1281 static void resolve_labels(compile_ctx_t
*ctx
, unsigned off
)
1285 for(instr
= ctx
->code
->instrs
+off
; instr
< ctx
->code
->instrs
+ctx
->instr_cnt
; instr
++) {
1286 if(instr_info
[instr
->op
].arg1_type
== ARG_ADDR
&& (instr
->arg1
.uint
& LABEL_FLAG
)) {
1287 assert((instr
->arg1
.uint
& ~LABEL_FLAG
) < ctx
->labels_cnt
);
1288 instr
->arg1
.uint
= ctx
->labels
[instr
->arg1
.uint
& ~LABEL_FLAG
];
1290 assert(instr_info
[instr
->op
].arg2_type
!= ARG_ADDR
);
1293 ctx
->labels_cnt
= 0;
1296 static HRESULT
fill_array_desc(compile_ctx_t
*ctx
, dim_decl_t
*dim_decl
, array_desc_t
*array_desc
)
1298 unsigned dim_cnt
= 0, i
;
1301 for(iter
= dim_decl
->dims
; iter
; iter
= iter
->next
)
1304 array_desc
->bounds
= compiler_alloc(ctx
->code
, dim_cnt
* sizeof(SAFEARRAYBOUND
));
1305 if(!array_desc
->bounds
)
1306 return E_OUTOFMEMORY
;
1308 array_desc
->dim_cnt
= dim_cnt
;
1310 for(iter
= dim_decl
->dims
, i
=0; iter
; iter
= iter
->next
, i
++) {
1311 array_desc
->bounds
[i
].cElements
= iter
->val
+1;
1312 array_desc
->bounds
[i
].lLbound
= 0;
1318 static HRESULT
compile_func(compile_ctx_t
*ctx
, statement_t
*stat
, function_t
*func
)
1322 func
->code_off
= ctx
->instr_cnt
;
1324 ctx
->sub_end_label
= 0;
1325 ctx
->func_end_label
= 0;
1326 ctx
->prop_end_label
= 0;
1328 switch(func
->type
) {
1330 ctx
->func_end_label
= alloc_label(ctx
);
1331 if(!ctx
->func_end_label
)
1332 return E_OUTOFMEMORY
;
1335 ctx
->sub_end_label
= alloc_label(ctx
);
1336 if(!ctx
->sub_end_label
)
1337 return E_OUTOFMEMORY
;
1343 ctx
->prop_end_label
= alloc_label(ctx
);
1344 if(!ctx
->prop_end_label
)
1345 return E_OUTOFMEMORY
;
1352 ctx
->dim_decls
= ctx
->dim_decls_tail
= NULL
;
1353 ctx
->const_decls
= NULL
;
1354 hres
= compile_statement(ctx
, NULL
, stat
);
1359 if(ctx
->sub_end_label
)
1360 label_set_addr(ctx
, ctx
->sub_end_label
);
1361 if(ctx
->func_end_label
)
1362 label_set_addr(ctx
, ctx
->func_end_label
);
1363 if(ctx
->prop_end_label
)
1364 label_set_addr(ctx
, ctx
->prop_end_label
);
1366 if(!push_instr(ctx
, OP_ret
))
1367 return E_OUTOFMEMORY
;
1369 resolve_labels(ctx
, func
->code_off
);
1372 dim_decl_t
*dim_decl
;
1374 if(func
->type
== FUNC_GLOBAL
) {
1375 dynamic_var_t
*new_var
;
1379 for(dim_decl
= ctx
->dim_decls
; dim_decl
; dim_decl
= dim_decl
->next
) {
1380 new_var
= compiler_alloc(ctx
->code
, sizeof(*new_var
));
1382 return E_OUTOFMEMORY
;
1384 new_var
->name
= compiler_alloc_string(ctx
->code
, dim_decl
->name
);
1386 return E_OUTOFMEMORY
;
1388 V_VT(&new_var
->v
) = VT_EMPTY
;
1389 new_var
->is_const
= FALSE
;
1391 new_var
->next
= ctx
->global_vars
;
1392 ctx
->global_vars
= new_var
;
1397 func
->vars
= compiler_alloc(ctx
->code
, func
->var_cnt
* sizeof(var_desc_t
));
1399 return E_OUTOFMEMORY
;
1401 for(dim_decl
= ctx
->dim_decls
, i
=0; dim_decl
; dim_decl
= dim_decl
->next
, i
++) {
1402 func
->vars
[i
].name
= compiler_alloc_string(ctx
->code
, dim_decl
->name
);
1403 if(!func
->vars
[i
].name
)
1404 return E_OUTOFMEMORY
;
1407 assert(i
== func
->var_cnt
);
1411 if(func
->array_cnt
) {
1412 unsigned array_id
= 0;
1413 dim_decl_t
*dim_decl
;
1415 func
->array_descs
= compiler_alloc(ctx
->code
, func
->array_cnt
* sizeof(array_desc_t
));
1416 if(!func
->array_descs
)
1417 return E_OUTOFMEMORY
;
1419 for(dim_decl
= ctx
->dim_decls
; dim_decl
; dim_decl
= dim_decl
->next
) {
1420 if(dim_decl
->is_array
) {
1421 hres
= fill_array_desc(ctx
, dim_decl
, func
->array_descs
+ array_id
++);
1427 assert(array_id
== func
->array_cnt
);
1433 static BOOL
lookup_funcs_name(compile_ctx_t
*ctx
, const WCHAR
*name
)
1437 for(iter
= ctx
->funcs
; iter
; iter
= iter
->next
) {
1438 if(!strcmpiW(iter
->name
, name
))
1445 static HRESULT
create_function(compile_ctx_t
*ctx
, function_decl_t
*decl
, function_t
**ret
)
1450 if(lookup_dim_decls(ctx
, decl
->name
) || lookup_funcs_name(ctx
, decl
->name
) || lookup_const_decls(ctx
, decl
->name
, FALSE
)) {
1451 FIXME("%s: redefinition\n", debugstr_w(decl
->name
));
1455 func
= compiler_alloc(ctx
->code
, sizeof(*func
));
1457 return E_OUTOFMEMORY
;
1459 func
->name
= compiler_alloc_string(ctx
->code
, decl
->name
);
1461 return E_OUTOFMEMORY
;
1465 func
->array_cnt
= 0;
1466 func
->code_ctx
= ctx
->code
;
1467 func
->type
= decl
->type
;
1468 func
->is_public
= decl
->is_public
;
1475 for(arg
= decl
->args
; arg
; arg
= arg
->next
)
1478 func
->args
= compiler_alloc(ctx
->code
, func
->arg_cnt
* sizeof(arg_desc_t
));
1480 return E_OUTOFMEMORY
;
1482 for(i
= 0, arg
= decl
->args
; arg
; arg
= arg
->next
, i
++) {
1483 func
->args
[i
].name
= compiler_alloc_string(ctx
->code
, arg
->name
);
1484 if(!func
->args
[i
].name
)
1485 return E_OUTOFMEMORY
;
1486 func
->args
[i
].by_ref
= arg
->by_ref
;
1492 hres
= compile_func(ctx
, decl
->body
, func
);
1500 static BOOL
lookup_class_name(compile_ctx_t
*ctx
, const WCHAR
*name
)
1504 for(iter
= ctx
->classes
; iter
; iter
= iter
->next
) {
1505 if(!strcmpiW(iter
->name
, name
))
1512 static HRESULT
create_class_funcprop(compile_ctx_t
*ctx
, function_decl_t
*func_decl
, vbdisp_funcprop_desc_t
*desc
)
1514 vbdisp_invoke_type_t invoke_type
;
1515 function_decl_t
*funcprop_decl
;
1518 desc
->name
= compiler_alloc_string(ctx
->code
, func_decl
->name
);
1520 return E_OUTOFMEMORY
;
1522 for(funcprop_decl
= func_decl
; funcprop_decl
; funcprop_decl
= funcprop_decl
->next_prop_func
) {
1523 switch(funcprop_decl
->type
) {
1528 invoke_type
= VBDISP_CALLGET
;
1531 invoke_type
= VBDISP_LET
;
1534 invoke_type
= VBDISP_SET
;
1536 DEFAULT_UNREACHABLE
;
1539 assert(!desc
->entries
[invoke_type
]);
1541 if(funcprop_decl
->is_public
)
1542 desc
->is_public
= TRUE
;
1544 hres
= create_function(ctx
, funcprop_decl
, desc
->entries
+invoke_type
);
1552 static BOOL
lookup_class_funcs(class_desc_t
*class_desc
, const WCHAR
*name
)
1556 for(i
=0; i
< class_desc
->func_cnt
; i
++) {
1557 if(class_desc
->funcs
[i
].name
&& !strcmpiW(class_desc
->funcs
[i
].name
, name
))
1564 static HRESULT
compile_class(compile_ctx_t
*ctx
, class_decl_t
*class_decl
)
1566 function_decl_t
*func_decl
, *func_prop_decl
;
1567 class_desc_t
*class_desc
;
1568 dim_decl_t
*prop_decl
;
1572 static const WCHAR class_initializeW
[] = {'c','l','a','s','s','_','i','n','i','t','i','a','l','i','z','e',0};
1573 static const WCHAR class_terminateW
[] = {'c','l','a','s','s','_','t','e','r','m','i','n','a','t','e',0};
1575 if(lookup_dim_decls(ctx
, class_decl
->name
) || lookup_funcs_name(ctx
, class_decl
->name
)
1576 || lookup_const_decls(ctx
, class_decl
->name
, FALSE
) || lookup_class_name(ctx
, class_decl
->name
)) {
1577 FIXME("%s: redefinition\n", debugstr_w(class_decl
->name
));
1581 class_desc
= compiler_alloc_zero(ctx
->code
, sizeof(*class_desc
));
1583 return E_OUTOFMEMORY
;
1585 class_desc
->name
= compiler_alloc_string(ctx
->code
, class_decl
->name
);
1586 if(!class_desc
->name
)
1587 return E_OUTOFMEMORY
;
1589 class_desc
->func_cnt
= 1; /* always allocate slot for default getter */
1591 for(func_decl
= class_decl
->funcs
; func_decl
; func_decl
= func_decl
->next
) {
1592 for(func_prop_decl
= func_decl
; func_prop_decl
; func_prop_decl
= func_prop_decl
->next_prop_func
) {
1593 if(func_prop_decl
->type
== FUNC_DEFGET
)
1597 class_desc
->func_cnt
++;
1600 class_desc
->funcs
= compiler_alloc(ctx
->code
, class_desc
->func_cnt
*sizeof(*class_desc
->funcs
));
1601 if(!class_desc
->funcs
)
1602 return E_OUTOFMEMORY
;
1603 memset(class_desc
->funcs
, 0, class_desc
->func_cnt
*sizeof(*class_desc
->funcs
));
1605 for(func_decl
= class_decl
->funcs
, i
=1; func_decl
; func_decl
= func_decl
->next
, i
++) {
1606 for(func_prop_decl
= func_decl
; func_prop_decl
; func_prop_decl
= func_prop_decl
->next_prop_func
) {
1607 if(func_prop_decl
->type
== FUNC_DEFGET
) {
1613 if(!strcmpiW(class_initializeW
, func_decl
->name
)) {
1614 if(func_decl
->type
!= FUNC_SUB
) {
1615 FIXME("class initializer is not sub\n");
1619 class_desc
->class_initialize_id
= i
;
1620 }else if(!strcmpiW(class_terminateW
, func_decl
->name
)) {
1621 if(func_decl
->type
!= FUNC_SUB
) {
1622 FIXME("class terminator is not sub\n");
1626 class_desc
->class_terminate_id
= i
;
1629 hres
= create_class_funcprop(ctx
, func_decl
, class_desc
->funcs
+ (func_prop_decl
? 0 : i
));
1634 for(prop_decl
= class_decl
->props
; prop_decl
; prop_decl
= prop_decl
->next
)
1635 class_desc
->prop_cnt
++;
1637 class_desc
->props
= compiler_alloc(ctx
->code
, class_desc
->prop_cnt
*sizeof(*class_desc
->props
));
1638 if(!class_desc
->props
)
1639 return E_OUTOFMEMORY
;
1641 for(prop_decl
= class_decl
->props
, i
=0; prop_decl
; prop_decl
= prop_decl
->next
, i
++) {
1642 if(lookup_class_funcs(class_desc
, prop_decl
->name
)) {
1643 FIXME("Property %s redefined\n", debugstr_w(prop_decl
->name
));
1647 class_desc
->props
[i
].name
= compiler_alloc_string(ctx
->code
, prop_decl
->name
);
1648 if(!class_desc
->props
[i
].name
)
1649 return E_OUTOFMEMORY
;
1651 class_desc
->props
[i
].is_public
= prop_decl
->is_public
;
1653 if(prop_decl
->is_array
) {
1654 class_desc
->props
[i
].is_array
= TRUE
;
1655 class_desc
->array_cnt
++;
1659 if(class_desc
->array_cnt
) {
1660 class_desc
->array_descs
= compiler_alloc(ctx
->code
, class_desc
->array_cnt
*sizeof(*class_desc
->array_descs
));
1661 if(!class_desc
->array_descs
)
1662 return E_OUTOFMEMORY
;
1664 for(prop_decl
= class_decl
->props
, i
=0; prop_decl
; prop_decl
= prop_decl
->next
) {
1665 if(prop_decl
->is_array
) {
1666 hres
= fill_array_desc(ctx
, prop_decl
, class_desc
->array_descs
+ i
++);
1673 class_desc
->next
= ctx
->classes
;
1674 ctx
->classes
= class_desc
;
1678 static BOOL
lookup_script_identifier(script_ctx_t
*script
, const WCHAR
*identifier
)
1680 class_desc_t
*class;
1684 for(var
= script
->global_vars
; var
; var
= var
->next
) {
1685 if(!strcmpiW(var
->name
, identifier
))
1689 for(func
= script
->global_funcs
; func
; func
= func
->next
) {
1690 if(!strcmpiW(func
->name
, identifier
))
1694 for(class = script
->classes
; class; class = class->next
) {
1695 if(!strcmpiW(class->name
, identifier
))
1702 static HRESULT
check_script_collisions(compile_ctx_t
*ctx
, script_ctx_t
*script
)
1704 class_desc_t
*class;
1708 for(var
= ctx
->global_vars
; var
; var
= var
->next
) {
1709 if(lookup_script_identifier(script
, var
->name
)) {
1710 FIXME("%s: redefined\n", debugstr_w(var
->name
));
1715 for(func
= ctx
->funcs
; func
; func
= func
->next
) {
1716 if(lookup_script_identifier(script
, func
->name
)) {
1717 FIXME("%s: redefined\n", debugstr_w(func
->name
));
1722 for(class = ctx
->classes
; class; class = class->next
) {
1723 if(lookup_script_identifier(script
, class->name
)) {
1724 FIXME("%s: redefined\n", debugstr_w(class->name
));
1732 void release_vbscode(vbscode_t
*code
)
1736 list_remove(&code
->entry
);
1738 for(i
=0; i
< code
->bstr_cnt
; i
++)
1739 SysFreeString(code
->bstr_pool
[i
]);
1741 heap_pool_free(&code
->heap
);
1743 heap_free(code
->bstr_pool
);
1744 heap_free(code
->source
);
1745 heap_free(code
->instrs
);
1749 static vbscode_t
*alloc_vbscode(compile_ctx_t
*ctx
, const WCHAR
*source
)
1753 ret
= heap_alloc(sizeof(*ret
));
1757 ret
->source
= heap_strdupW(source
);
1763 ret
->instrs
= heap_alloc(32*sizeof(instr_t
));
1765 release_vbscode(ret
);
1770 ctx
->instr_size
= 32;
1771 heap_pool_init(&ret
->heap
);
1773 ret
->option_explicit
= ctx
->parser
.option_explicit
;
1775 ret
->bstr_pool
= NULL
;
1776 ret
->bstr_pool_size
= 0;
1778 ret
->pending_exec
= FALSE
;
1780 ret
->main_code
.type
= FUNC_GLOBAL
;
1781 ret
->main_code
.name
= NULL
;
1782 ret
->main_code
.code_ctx
= ret
;
1783 ret
->main_code
.vars
= NULL
;
1784 ret
->main_code
.var_cnt
= 0;
1785 ret
->main_code
.array_cnt
= 0;
1786 ret
->main_code
.arg_cnt
= 0;
1787 ret
->main_code
.args
= NULL
;
1789 list_init(&ret
->entry
);
1793 static void release_compiler(compile_ctx_t
*ctx
)
1795 parser_release(&ctx
->parser
);
1796 heap_free(ctx
->labels
);
1798 release_vbscode(ctx
->code
);
1801 HRESULT
compile_script(script_ctx_t
*script
, const WCHAR
*src
, const WCHAR
*delimiter
, vbscode_t
**ret
)
1803 function_t
*new_func
;
1804 function_decl_t
*func_decl
;
1805 class_decl_t
*class_decl
;
1810 hres
= parse_script(&ctx
.parser
, src
, delimiter
);
1814 code
= ctx
.code
= alloc_vbscode(&ctx
, src
);
1816 return E_OUTOFMEMORY
;
1819 ctx
.func_decls
= NULL
;
1820 ctx
.global_vars
= NULL
;
1823 ctx
.global_consts
= NULL
;
1824 ctx
.stat_ctx
= NULL
;
1825 ctx
.labels_cnt
= ctx
.labels_size
= 0;
1827 hres
= compile_func(&ctx
, ctx
.parser
.stats
, &ctx
.code
->main_code
);
1829 release_compiler(&ctx
);
1833 ctx
.global_consts
= ctx
.const_decls
;
1835 for(func_decl
= ctx
.func_decls
; func_decl
; func_decl
= func_decl
->next
) {
1836 hres
= create_function(&ctx
, func_decl
, &new_func
);
1838 release_compiler(&ctx
);
1842 new_func
->next
= ctx
.funcs
;
1843 ctx
.funcs
= new_func
;
1846 for(class_decl
= ctx
.parser
.class_decls
; class_decl
; class_decl
= class_decl
->next
) {
1847 hres
= compile_class(&ctx
, class_decl
);
1849 release_compiler(&ctx
);
1854 hres
= check_script_collisions(&ctx
, script
);
1856 release_compiler(&ctx
);
1860 if(ctx
.global_vars
) {
1863 for(var
= ctx
.global_vars
; var
->next
; var
= var
->next
);
1865 var
->next
= script
->global_vars
;
1866 script
->global_vars
= ctx
.global_vars
;
1870 for(new_func
= ctx
.funcs
; new_func
->next
; new_func
= new_func
->next
);
1872 new_func
->next
= script
->global_funcs
;
1873 script
->global_funcs
= ctx
.funcs
;
1877 class_desc_t
*class = ctx
.classes
;
1880 class->ctx
= script
;
1883 class = class->next
;
1886 class->next
= script
->classes
;
1887 script
->classes
= ctx
.classes
;
1890 if(TRACE_ON(vbscript_disas
))
1894 release_compiler(&ctx
);
1896 list_add_tail(&script
->code_list
, &code
->entry
);