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
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
{
33 unsigned while_end_label
;
34 unsigned for_end_label
;
35 unsigned with_stack_offset
;
37 struct _statement_ctx_t
*next
;
48 statement_ctx_t
*stat_ctx
;
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
;
65 function_decl_t
*func_decls
;
68 static HRESULT
compile_expression(compile_ctx_t
*,expression_t
*);
69 static HRESULT
compile_statement(compile_ctx_t
*,statement_ctx_t
*,statement_t
*);
73 instr_arg_type_t arg1_type
;
74 instr_arg_type_t arg2_type
;
76 #define X(n,a,b,c) {#n,b,c},
81 static void dump_instr_arg(instr_arg_type_t type
, instr_arg_t
*arg
)
86 TRACE_(vbscript_disas
)("\t%s", debugstr_w(arg
->str
));
89 TRACE_(vbscript_disas
)("\t%d", arg
->uint
);
93 TRACE_(vbscript_disas
)("\t%u", arg
->uint
);
96 TRACE_(vbscript_disas
)("\t%lf", *arg
->dbl
);
104 static void dump_code(compile_ctx_t
*ctx
)
108 for(instr
= ctx
->code
->instrs
+1; instr
< ctx
->code
->instrs
+ctx
->instr_cnt
; instr
++) {
109 assert(instr
->op
< OP_LAST
);
110 TRACE_(vbscript_disas
)("%d:\t%s", (int)(instr
-ctx
->code
->instrs
), instr_info
[instr
->op
].op_str
);
111 dump_instr_arg(instr_info
[instr
->op
].arg1_type
, &instr
->arg1
);
112 dump_instr_arg(instr_info
[instr
->op
].arg2_type
, &instr
->arg2
);
113 TRACE_(vbscript_disas
)("\n");
117 static inline void *compiler_alloc(vbscode_t
*vbscode
, size_t size
)
119 return heap_pool_alloc(&vbscode
->heap
, size
);
122 static inline void *compiler_alloc_zero(vbscode_t
*vbscode
, size_t size
)
126 ret
= heap_pool_alloc(&vbscode
->heap
, size
);
128 memset(ret
, 0, size
);
132 static WCHAR
*compiler_alloc_string(vbscode_t
*vbscode
, const WCHAR
*str
)
137 size
= (lstrlenW(str
)+1)*sizeof(WCHAR
);
138 ret
= compiler_alloc(vbscode
, size
);
140 memcpy(ret
, str
, size
);
144 static inline instr_t
*instr_ptr(compile_ctx_t
*ctx
, unsigned id
)
146 assert(id
< ctx
->instr_cnt
);
147 return ctx
->code
->instrs
+ id
;
150 static unsigned push_instr(compile_ctx_t
*ctx
, vbsop_t op
)
152 assert(ctx
->instr_size
&& ctx
->instr_size
>= ctx
->instr_cnt
);
154 if(ctx
->instr_size
== ctx
->instr_cnt
) {
157 new_instr
= heap_realloc(ctx
->code
->instrs
, ctx
->instr_size
*2*sizeof(instr_t
));
161 ctx
->code
->instrs
= new_instr
;
162 ctx
->instr_size
*= 2;
165 ctx
->code
->instrs
[ctx
->instr_cnt
].op
= op
;
166 ctx
->code
->instrs
[ctx
->instr_cnt
].loc
= ctx
->loc
;
167 return ctx
->instr_cnt
++;
170 static HRESULT
push_instr_int(compile_ctx_t
*ctx
, vbsop_t op
, LONG arg
)
174 ret
= push_instr(ctx
, op
);
176 return E_OUTOFMEMORY
;
178 instr_ptr(ctx
, ret
)->arg1
.lng
= arg
;
182 static HRESULT
push_instr_uint(compile_ctx_t
*ctx
, vbsop_t op
, unsigned arg
)
186 ret
= push_instr(ctx
, op
);
188 return E_OUTOFMEMORY
;
190 instr_ptr(ctx
, ret
)->arg1
.uint
= arg
;
194 static HRESULT
push_instr_addr(compile_ctx_t
*ctx
, vbsop_t op
, unsigned arg
)
198 ret
= push_instr(ctx
, op
);
200 return E_OUTOFMEMORY
;
202 instr_ptr(ctx
, ret
)->arg1
.uint
= arg
;
206 static HRESULT
push_instr_str(compile_ctx_t
*ctx
, vbsop_t op
, const WCHAR
*arg
)
211 str
= compiler_alloc_string(ctx
->code
, arg
);
213 return E_OUTOFMEMORY
;
215 instr
= push_instr(ctx
, op
);
217 return E_OUTOFMEMORY
;
219 instr_ptr(ctx
, instr
)->arg1
.str
= str
;
223 static HRESULT
push_instr_double(compile_ctx_t
*ctx
, vbsop_t op
, double arg
)
228 d
= compiler_alloc(ctx
->code
, sizeof(double));
230 return E_OUTOFMEMORY
;
232 instr
= push_instr(ctx
, op
);
234 return E_OUTOFMEMORY
;
237 instr_ptr(ctx
, instr
)->arg1
.dbl
= d
;
241 static BSTR
alloc_bstr_arg(compile_ctx_t
*ctx
, const WCHAR
*str
)
243 if(!ctx
->code
->bstr_pool_size
) {
244 ctx
->code
->bstr_pool
= heap_alloc(8 * sizeof(BSTR
));
245 if(!ctx
->code
->bstr_pool
)
247 ctx
->code
->bstr_pool_size
= 8;
248 }else if(ctx
->code
->bstr_pool_size
== ctx
->code
->bstr_cnt
) {
251 new_pool
= heap_realloc(ctx
->code
->bstr_pool
, ctx
->code
->bstr_pool_size
*2*sizeof(BSTR
));
255 ctx
->code
->bstr_pool
= new_pool
;
256 ctx
->code
->bstr_pool_size
*= 2;
259 ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
] = SysAllocString(str
);
260 if(!ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
])
263 return ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
++];
266 static HRESULT
push_instr_bstr(compile_ctx_t
*ctx
, vbsop_t op
, const WCHAR
*arg
)
271 bstr
= alloc_bstr_arg(ctx
, arg
);
273 return E_OUTOFMEMORY
;
275 instr
= push_instr(ctx
, op
);
277 return E_OUTOFMEMORY
;
279 instr_ptr(ctx
, instr
)->arg1
.bstr
= bstr
;
283 static HRESULT
push_instr_bstr_uint(compile_ctx_t
*ctx
, vbsop_t op
, const WCHAR
*arg1
, unsigned arg2
)
288 bstr
= alloc_bstr_arg(ctx
, arg1
);
290 return E_OUTOFMEMORY
;
292 instr
= push_instr(ctx
, op
);
294 return E_OUTOFMEMORY
;
296 instr_ptr(ctx
, instr
)->arg1
.bstr
= bstr
;
297 instr_ptr(ctx
, instr
)->arg2
.uint
= arg2
;
301 static HRESULT
push_instr_uint_bstr(compile_ctx_t
*ctx
, vbsop_t op
, unsigned arg1
, const WCHAR
*arg2
)
306 bstr
= alloc_bstr_arg(ctx
, arg2
);
308 return E_OUTOFMEMORY
;
310 instr
= push_instr(ctx
, op
);
312 return E_OUTOFMEMORY
;
314 instr_ptr(ctx
, instr
)->arg1
.uint
= arg1
;
315 instr_ptr(ctx
, instr
)->arg2
.bstr
= bstr
;
319 #define LABEL_FLAG 0x80000000
321 static unsigned alloc_label(compile_ctx_t
*ctx
)
323 if(!ctx
->labels_size
) {
324 ctx
->labels
= heap_alloc(8 * sizeof(*ctx
->labels
));
327 ctx
->labels_size
= 8;
328 }else if(ctx
->labels_size
== ctx
->labels_cnt
) {
329 unsigned *new_labels
;
331 new_labels
= heap_realloc(ctx
->labels
, 2*ctx
->labels_size
*sizeof(*ctx
->labels
));
335 ctx
->labels
= new_labels
;
336 ctx
->labels_size
*= 2;
339 return ctx
->labels_cnt
++ | LABEL_FLAG
;
342 static inline void label_set_addr(compile_ctx_t
*ctx
, unsigned label
)
344 assert(label
& LABEL_FLAG
);
345 ctx
->labels
[label
& ~LABEL_FLAG
] = ctx
->instr_cnt
;
348 static inline unsigned stack_offset(compile_ctx_t
*ctx
)
350 statement_ctx_t
*iter
;
353 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
)
354 ret
+= iter
->stack_use
;
359 static BOOL
emit_catch_jmp(compile_ctx_t
*ctx
, unsigned stack_off
, unsigned code_off
)
363 code
= push_instr(ctx
, OP_catch
);
367 instr_ptr(ctx
, code
)->arg1
.uint
= code_off
;
368 instr_ptr(ctx
, code
)->arg2
.uint
= stack_off
+ stack_offset(ctx
);
372 static inline BOOL
emit_catch(compile_ctx_t
*ctx
, unsigned off
)
374 return emit_catch_jmp(ctx
, off
, ctx
->instr_cnt
);
377 static HRESULT
compile_error(script_ctx_t
*ctx
, compile_ctx_t
*compiler
, HRESULT error
)
379 if(error
== SCRIPT_E_REPORTED
)
383 ctx
->ei
.scode
= error
= map_hres(error
);
384 ctx
->ei
.bstrSource
= get_vbscript_string(VBS_COMPILE_ERROR
);
385 ctx
->ei
.bstrDescription
= get_vbscript_error_string(error
);
386 return report_script_error(ctx
, compiler
->code
, compiler
->loc
);
389 static expression_t
*lookup_const_decls(compile_ctx_t
*ctx
, const WCHAR
*name
, BOOL lookup_global
)
393 for(decl
= ctx
->const_decls
; decl
; decl
= decl
->next
) {
394 if(!wcsicmp(decl
->name
, name
))
395 return decl
->value_expr
;
401 for(decl
= ctx
->global_consts
; decl
; decl
= decl
->next
) {
402 if(!wcsicmp(decl
->name
, name
))
403 return decl
->value_expr
;
409 static HRESULT
compile_args(compile_ctx_t
*ctx
, expression_t
*args
, unsigned *ret
)
411 unsigned arg_cnt
= 0;
415 hres
= compile_expression(ctx
, args
);
419 if(args
->type
== EXPR_BRACKETS
&& !push_instr(ctx
, OP_deref
))
420 return E_OUTOFMEMORY
;
430 static HRESULT
compile_member_expression(compile_ctx_t
*ctx
, member_expression_t
*expr
, unsigned arg_cnt
, BOOL ret_val
)
434 if(ret_val
&& !arg_cnt
) {
435 expression_t
*const_expr
;
437 const_expr
= lookup_const_decls(ctx
, expr
->identifier
, TRUE
);
439 return compile_expression(ctx
, const_expr
);
443 hres
= compile_expression(ctx
, expr
->obj_expr
);
447 hres
= push_instr_bstr_uint(ctx
, ret_val
? OP_mcall
: OP_mcallv
, expr
->identifier
, arg_cnt
);
449 hres
= push_instr_bstr_uint(ctx
, ret_val
? OP_icall
: OP_icallv
, expr
->identifier
, arg_cnt
);
455 static HRESULT
compile_call_expression(compile_ctx_t
*ctx
, call_expression_t
*expr
, BOOL ret_val
)
457 unsigned arg_cnt
= 0;
461 hres
= compile_args(ctx
, expr
->args
, &arg_cnt
);
465 for(call
= expr
->call_expr
; call
->type
== EXPR_BRACKETS
; call
= ((unary_expression_t
*)call
)->subexpr
);
467 if(call
->type
== EXPR_MEMBER
)
468 return compile_member_expression(ctx
, (member_expression_t
*)call
, arg_cnt
, ret_val
);
470 hres
= compile_expression(ctx
, call
);
474 return push_instr_uint(ctx
, ret_val
? OP_vcall
: OP_vcallv
, arg_cnt
);
477 static HRESULT
compile_dot_expression(compile_ctx_t
*ctx
)
479 statement_ctx_t
*stat_ctx
;
481 for(stat_ctx
= ctx
->stat_ctx
; stat_ctx
; stat_ctx
= stat_ctx
->next
) {
482 if(!stat_ctx
->with_stack_offset
)
485 return push_instr_uint(ctx
, OP_stack
, stat_ctx
->with_stack_offset
- 1);
488 WARN("dot expression outside with statement\n");
489 return push_instr_uint(ctx
, OP_stack
, ~0);
492 static HRESULT
compile_unary_expression(compile_ctx_t
*ctx
, unary_expression_t
*expr
, vbsop_t op
)
496 hres
= compile_expression(ctx
, expr
->subexpr
);
500 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
503 static HRESULT
compile_binary_expression(compile_ctx_t
*ctx
, binary_expression_t
*expr
, vbsop_t op
)
507 hres
= compile_expression(ctx
, expr
->left
);
511 hres
= compile_expression(ctx
, expr
->right
);
515 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
518 static HRESULT
compile_expression(compile_ctx_t
*ctx
, expression_t
*expr
)
522 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_add
);
524 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_and
);
526 return push_instr_int(ctx
, OP_bool
, ((bool_expression_t
*)expr
)->value
);
528 return compile_expression(ctx
, ((unary_expression_t
*)expr
)->subexpr
);
530 return compile_call_expression(ctx
, (call_expression_t
*)expr
, TRUE
);
532 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_concat
);
534 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_div
);
536 return compile_dot_expression(ctx
);
538 return push_instr_double(ctx
, OP_double
, ((double_expression_t
*)expr
)->value
);
540 return push_instr(ctx
, OP_empty
) ? S_OK
: E_OUTOFMEMORY
;
542 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_equal
);
544 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_eqv
);
546 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_exp
);
548 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gt
);
550 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gteq
);
552 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_idiv
);
554 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_is
);
556 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_imp
);
558 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lt
);
560 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lteq
);
562 return push_instr(ctx
, OP_me
) ? S_OK
: E_OUTOFMEMORY
;
564 return compile_member_expression(ctx
, (member_expression_t
*)expr
, 0, TRUE
);
566 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mod
);
568 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mul
);
570 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_neg
);
572 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_nequal
);
574 return push_instr_str(ctx
, OP_new
, ((string_expression_t
*)expr
)->value
);
576 return push_instr_int(ctx
, OP_hres
, DISP_E_PARAMNOTFOUND
);
578 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_not
);
580 return push_instr(ctx
, OP_nothing
) ? S_OK
: E_OUTOFMEMORY
;
582 return push_instr(ctx
, OP_null
) ? S_OK
: E_OUTOFMEMORY
;
584 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_or
);
586 return push_instr_str(ctx
, OP_string
, ((string_expression_t
*)expr
)->value
);
588 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
590 return push_instr_int(ctx
, OP_int
, ((int_expression_t
*)expr
)->value
);
592 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_xor
);
594 FIXME("Unimplemented expression type %d\n", expr
->type
);
601 static HRESULT
compile_if_statement(compile_ctx_t
*ctx
, if_statement_t
*stat
)
603 unsigned cnd_jmp
, endif_label
= 0;
604 elseif_decl_t
*elseif_decl
;
607 hres
= compile_expression(ctx
, stat
->expr
);
611 cnd_jmp
= push_instr(ctx
, OP_jmp_false
);
613 return E_OUTOFMEMORY
;
615 if(!emit_catch(ctx
, 0))
616 return E_OUTOFMEMORY
;
618 hres
= compile_statement(ctx
, NULL
, stat
->if_stat
);
622 if(stat
->else_stat
|| stat
->elseifs
) {
623 endif_label
= alloc_label(ctx
);
625 return E_OUTOFMEMORY
;
627 hres
= push_instr_addr(ctx
, OP_jmp
, endif_label
);
632 for(elseif_decl
= stat
->elseifs
; elseif_decl
; elseif_decl
= elseif_decl
->next
) {
633 instr_ptr(ctx
, cnd_jmp
)->arg1
.uint
= ctx
->instr_cnt
;
635 ctx
->loc
= elseif_decl
->loc
;
637 hres
= compile_expression(ctx
, elseif_decl
->expr
);
641 cnd_jmp
= push_instr(ctx
, OP_jmp_false
);
643 return E_OUTOFMEMORY
;
645 if(!emit_catch(ctx
, 0))
646 return E_OUTOFMEMORY
;
648 hres
= compile_statement(ctx
, NULL
, elseif_decl
->stat
);
652 hres
= push_instr_addr(ctx
, OP_jmp
, endif_label
);
657 instr_ptr(ctx
, cnd_jmp
)->arg1
.uint
= ctx
->instr_cnt
;
659 if(stat
->else_stat
) {
660 hres
= compile_statement(ctx
, NULL
, stat
->else_stat
);
666 label_set_addr(ctx
, endif_label
);
670 static HRESULT
compile_while_statement(compile_ctx_t
*ctx
, while_statement_t
*stat
)
672 statement_ctx_t stat_ctx
= {0}, *loop_ctx
;
677 start_addr
= ctx
->instr_cnt
;
679 hres
= compile_expression(ctx
, stat
->expr
);
683 jmp_end
= push_instr(ctx
, stat
->stat
.type
== STAT_UNTIL
? OP_jmp_true
: OP_jmp_false
);
685 return E_OUTOFMEMORY
;
687 if(!emit_catch(ctx
, 0))
688 return E_OUTOFMEMORY
;
690 if(stat
->stat
.type
== STAT_WHILE
) {
693 if(!(stat_ctx
.while_end_label
= alloc_label(ctx
)))
694 return E_OUTOFMEMORY
;
695 loop_ctx
= &stat_ctx
;
698 hres
= compile_statement(ctx
, loop_ctx
, stat
->body
);
702 hres
= push_instr_addr(ctx
, OP_jmp
, start_addr
);
706 instr_ptr(ctx
, jmp_end
)->arg1
.uint
= ctx
->instr_cnt
;
709 label_set_addr(ctx
, stat_ctx
.while_end_label
);
714 static HRESULT
compile_dowhile_statement(compile_ctx_t
*ctx
, while_statement_t
*stat
)
716 statement_ctx_t loop_ctx
= {0};
721 start_addr
= ctx
->instr_cnt
;
723 if(!(loop_ctx
.while_end_label
= alloc_label(ctx
)))
724 return E_OUTOFMEMORY
;
726 hres
= compile_statement(ctx
, &loop_ctx
, stat
->body
);
730 ctx
->loc
= stat
->stat
.loc
;
732 hres
= compile_expression(ctx
, stat
->expr
);
736 jmp_op
= stat
->stat
.type
== STAT_DOUNTIL
? OP_jmp_false
: OP_jmp_true
;
741 hres
= push_instr_addr(ctx
, jmp_op
, start_addr
);
745 label_set_addr(ctx
, loop_ctx
.while_end_label
);
747 if(!emit_catch(ctx
, 0))
748 return E_OUTOFMEMORY
;
753 static HRESULT
compile_foreach_statement(compile_ctx_t
*ctx
, foreach_statement_t
*stat
)
755 statement_ctx_t loop_ctx
= {1};
759 /* Preserve a place on the stack in case we throw before having proper enum collection. */
760 if(!push_instr(ctx
, OP_empty
))
761 return E_OUTOFMEMORY
;
763 hres
= compile_expression(ctx
, stat
->group_expr
);
767 if(!push_instr(ctx
, OP_newenum
))
768 return E_OUTOFMEMORY
;
770 if(!(loop_ctx
.for_end_label
= alloc_label(ctx
)))
771 return E_OUTOFMEMORY
;
773 hres
= push_instr_uint_bstr(ctx
, OP_enumnext
, loop_ctx
.for_end_label
, stat
->identifier
);
777 if(!emit_catch(ctx
, 1))
778 return E_OUTOFMEMORY
;
780 loop_start
= ctx
->instr_cnt
;
781 hres
= compile_statement(ctx
, &loop_ctx
, stat
->body
);
785 /* We need a separated enumnext here, because we need to jump out of the loop on exception. */
786 ctx
->loc
= stat
->stat
.loc
;
787 hres
= push_instr_uint_bstr(ctx
, OP_enumnext
, loop_ctx
.for_end_label
, stat
->identifier
);
791 hres
= push_instr_addr(ctx
, OP_jmp
, loop_start
);
795 label_set_addr(ctx
, loop_ctx
.for_end_label
);
799 static HRESULT
compile_forto_statement(compile_ctx_t
*ctx
, forto_statement_t
*stat
)
801 statement_ctx_t loop_ctx
= {2};
802 unsigned step_instr
, instr
;
806 identifier
= alloc_bstr_arg(ctx
, stat
->identifier
);
808 return E_OUTOFMEMORY
;
810 hres
= compile_expression(ctx
, stat
->from_expr
);
814 /* FIXME: Assign should happen after both expressions evaluation. */
815 instr
= push_instr(ctx
, OP_assign_ident
);
817 return E_OUTOFMEMORY
;
818 instr_ptr(ctx
, instr
)->arg1
.bstr
= identifier
;
819 instr_ptr(ctx
, instr
)->arg2
.uint
= 0;
821 hres
= compile_expression(ctx
, stat
->to_expr
);
825 if(!push_instr(ctx
, OP_val
))
826 return E_OUTOFMEMORY
;
828 if(stat
->step_expr
) {
829 hres
= compile_expression(ctx
, stat
->step_expr
);
833 if(!push_instr(ctx
, OP_val
))
834 return E_OUTOFMEMORY
;
836 hres
= push_instr_int(ctx
, OP_int
, 1);
841 loop_ctx
.for_end_label
= alloc_label(ctx
);
842 if(!loop_ctx
.for_end_label
)
843 return E_OUTOFMEMORY
;
845 step_instr
= push_instr(ctx
, OP_step
);
847 return E_OUTOFMEMORY
;
848 instr_ptr(ctx
, step_instr
)->arg2
.bstr
= identifier
;
849 instr_ptr(ctx
, step_instr
)->arg1
.uint
= loop_ctx
.for_end_label
;
851 if(!emit_catch(ctx
, 2))
852 return E_OUTOFMEMORY
;
854 hres
= compile_statement(ctx
, &loop_ctx
, stat
->body
);
858 /* FIXME: Error handling can't be done compatible with native using OP_incc here. */
859 instr
= push_instr(ctx
, OP_incc
);
861 return E_OUTOFMEMORY
;
862 instr_ptr(ctx
, instr
)->arg1
.bstr
= identifier
;
864 hres
= push_instr_addr(ctx
, OP_jmp
, step_instr
);
868 hres
= push_instr_uint(ctx
, OP_pop
, 2);
872 label_set_addr(ctx
, loop_ctx
.for_end_label
);
874 /* FIXME: reconsider after OP_incc fixup. */
875 if(!emit_catch(ctx
, 0))
876 return E_OUTOFMEMORY
;
881 static HRESULT
compile_with_statement(compile_ctx_t
*ctx
, with_statement_t
*stat
)
883 statement_ctx_t with_ctx
= { 1 };
886 hres
= compile_expression(ctx
, stat
->expr
);
890 if(!emit_catch(ctx
, 1))
891 return E_OUTOFMEMORY
;
893 with_ctx
.with_stack_offset
= stack_offset(ctx
) + 1;
894 hres
= compile_statement(ctx
, &with_ctx
, stat
->body
);
898 return push_instr_uint(ctx
, OP_pop
, 1);
901 static HRESULT
compile_select_statement(compile_ctx_t
*ctx
, select_statement_t
*stat
)
903 unsigned end_label
, case_cnt
= 0, *case_labels
= NULL
, i
;
904 case_clausule_t
*case_iter
;
905 expression_t
*expr_iter
;
908 hres
= compile_expression(ctx
, stat
->expr
);
912 if(!push_instr(ctx
, OP_val
))
913 return E_OUTOFMEMORY
;
915 end_label
= alloc_label(ctx
);
917 return E_OUTOFMEMORY
;
919 if(!emit_catch_jmp(ctx
, 0, end_label
))
920 return E_OUTOFMEMORY
;
922 for(case_iter
= stat
->case_clausules
; case_iter
; case_iter
= case_iter
->next
)
926 case_labels
= heap_alloc(case_cnt
*sizeof(*case_labels
));
928 return E_OUTOFMEMORY
;
931 for(case_iter
= stat
->case_clausules
, i
=0; case_iter
; case_iter
= case_iter
->next
, i
++) {
932 case_labels
[i
] = alloc_label(ctx
);
933 if(!case_labels
[i
]) {
934 hres
= E_OUTOFMEMORY
;
941 for(expr_iter
= case_iter
->expr
; expr_iter
; expr_iter
= expr_iter
->next
) {
942 hres
= compile_expression(ctx
, expr_iter
);
946 hres
= push_instr_addr(ctx
, OP_case
, case_labels
[i
]);
950 if(!emit_catch_jmp(ctx
, 0, case_labels
[i
])) {
951 hres
= E_OUTOFMEMORY
;
958 heap_free(case_labels
);
962 hres
= push_instr_uint(ctx
, OP_pop
, 1);
964 heap_free(case_labels
);
968 hres
= push_instr_addr(ctx
, OP_jmp
, case_iter
? case_labels
[i
] : end_label
);
970 heap_free(case_labels
);
974 for(case_iter
= stat
->case_clausules
, i
=0; case_iter
; case_iter
= case_iter
->next
, i
++) {
975 label_set_addr(ctx
, case_labels
[i
]);
976 hres
= compile_statement(ctx
, NULL
, case_iter
->stat
);
983 hres
= push_instr_addr(ctx
, OP_jmp
, end_label
);
988 heap_free(case_labels
);
992 label_set_addr(ctx
, end_label
);
996 static HRESULT
compile_assignment(compile_ctx_t
*ctx
, expression_t
*left
, expression_t
*value_expr
, BOOL is_set
)
998 call_expression_t
*call_expr
= NULL
;
999 member_expression_t
*member_expr
;
1000 unsigned args_cnt
= 0;
1004 switch(left
->type
) {
1006 member_expr
= (member_expression_t
*)left
;
1009 call_expr
= (call_expression_t
*)left
;
1010 assert(call_expr
->call_expr
->type
== EXPR_MEMBER
);
1011 member_expr
= (member_expression_t
*)call_expr
->call_expr
;
1018 if(member_expr
->obj_expr
) {
1019 hres
= compile_expression(ctx
, member_expr
->obj_expr
);
1023 op
= is_set
? OP_set_member
: OP_assign_member
;
1025 op
= is_set
? OP_set_ident
: OP_assign_ident
;
1028 hres
= compile_expression(ctx
, value_expr
);
1033 hres
= compile_args(ctx
, call_expr
->args
, &args_cnt
);
1038 hres
= push_instr_bstr_uint(ctx
, op
, member_expr
->identifier
, args_cnt
);
1042 if(!emit_catch(ctx
, 0))
1043 return E_OUTOFMEMORY
;
1048 static HRESULT
compile_assign_statement(compile_ctx_t
*ctx
, assign_statement_t
*stat
, BOOL is_set
)
1050 return compile_assignment(ctx
, stat
->left_expr
, stat
->value_expr
, is_set
);
1053 static HRESULT
compile_call_statement(compile_ctx_t
*ctx
, call_statement_t
*stat
)
1057 hres
= compile_call_expression(ctx
, stat
->expr
, FALSE
);
1061 if(!emit_catch(ctx
, 0))
1062 return E_OUTOFMEMORY
;
1067 static BOOL
lookup_dim_decls(compile_ctx_t
*ctx
, const WCHAR
*name
)
1069 dim_decl_t
*dim_decl
;
1071 for(dim_decl
= ctx
->dim_decls
; dim_decl
; dim_decl
= dim_decl
->next
) {
1072 if(!wcsicmp(dim_decl
->name
, name
))
1079 static BOOL
lookup_args_name(compile_ctx_t
*ctx
, const WCHAR
*name
)
1083 for(i
= 0; i
< ctx
->func
->arg_cnt
; i
++) {
1084 if(!wcsicmp(ctx
->func
->args
[i
].name
, name
))
1091 static HRESULT
compile_dim_statement(compile_ctx_t
*ctx
, dim_statement_t
*stat
)
1093 dim_decl_t
*dim_decl
= stat
->dim_decls
;
1096 if(lookup_dim_decls(ctx
, dim_decl
->name
) || lookup_args_name(ctx
, dim_decl
->name
)
1097 || lookup_const_decls(ctx
, dim_decl
->name
, FALSE
)) {
1098 FIXME("dim %s name redefined\n", debugstr_w(dim_decl
->name
));
1102 ctx
->func
->var_cnt
++;
1104 if(dim_decl
->is_array
) {
1105 HRESULT hres
= push_instr_bstr_uint(ctx
, OP_dim
, dim_decl
->name
, ctx
->func
->array_cnt
++);
1109 if(!emit_catch(ctx
, 0))
1110 return E_OUTOFMEMORY
;
1115 dim_decl
= dim_decl
->next
;
1118 if(ctx
->dim_decls_tail
)
1119 ctx
->dim_decls_tail
->next
= stat
->dim_decls
;
1121 ctx
->dim_decls
= stat
->dim_decls
;
1122 ctx
->dim_decls_tail
= dim_decl
;
1126 static HRESULT
compile_redim_statement(compile_ctx_t
*ctx
, redim_statement_t
*stat
)
1131 hres
= compile_args(ctx
, stat
->dims
, &arg_cnt
);
1135 hres
= push_instr_bstr_uint(ctx
, stat
->preserve
? OP_redim_preserve
: OP_redim
, stat
->identifier
, arg_cnt
);
1139 if(!emit_catch(ctx
, 0))
1140 return E_OUTOFMEMORY
;
1145 static HRESULT
compile_const_statement(compile_ctx_t
*ctx
, const_statement_t
*stat
)
1147 const_decl_t
*decl
, *next_decl
= stat
->decls
;
1152 if(lookup_const_decls(ctx
, decl
->name
, FALSE
) || lookup_args_name(ctx
, decl
->name
)
1153 || lookup_dim_decls(ctx
, decl
->name
)) {
1154 FIXME("%s redefined\n", debugstr_w(decl
->name
));
1158 if(ctx
->func
->type
== FUNC_GLOBAL
) {
1161 hres
= compile_expression(ctx
, decl
->value_expr
);
1165 hres
= push_instr_bstr(ctx
, OP_const
, decl
->name
);
1169 if(!emit_catch(ctx
, 0))
1170 return E_OUTOFMEMORY
;
1173 next_decl
= decl
->next
;
1174 decl
->next
= ctx
->const_decls
;
1175 ctx
->const_decls
= decl
;
1181 static HRESULT
compile_function_statement(compile_ctx_t
*ctx
, function_statement_t
*stat
)
1183 if(ctx
->func
!= &ctx
->code
->main_code
) {
1184 FIXME("Function is not in the global code\n");
1188 stat
->func_decl
->next
= ctx
->func_decls
;
1189 ctx
->func_decls
= stat
->func_decl
;
1193 static HRESULT
compile_exitdo_statement(compile_ctx_t
*ctx
)
1195 statement_ctx_t
*iter
;
1196 unsigned pop_cnt
= 0;
1198 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
1199 pop_cnt
+= iter
->stack_use
;
1200 if(iter
->while_end_label
)
1204 FIXME("Exit Do outside Do Loop\n");
1211 hres
= push_instr_uint(ctx
, OP_pop
, pop_cnt
);
1216 return push_instr_addr(ctx
, OP_jmp
, iter
->while_end_label
);
1219 static HRESULT
compile_exitfor_statement(compile_ctx_t
*ctx
)
1221 statement_ctx_t
*iter
;
1222 unsigned pop_cnt
= 0;
1224 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
1225 pop_cnt
+= iter
->stack_use
;
1226 if(iter
->for_end_label
)
1230 FIXME("Exit For outside For loop\n");
1237 hres
= push_instr_uint(ctx
, OP_pop
, pop_cnt
);
1242 return push_instr_addr(ctx
, OP_jmp
, iter
->for_end_label
);
1245 static HRESULT
exit_label(compile_ctx_t
*ctx
, unsigned jmp_label
)
1247 unsigned pop_cnt
= stack_offset(ctx
);
1252 hres
= push_instr_uint(ctx
, OP_pop
, pop_cnt
);
1257 return push_instr_addr(ctx
, OP_jmp
, jmp_label
);
1260 static HRESULT
compile_exitsub_statement(compile_ctx_t
*ctx
)
1262 if(!ctx
->sub_end_label
) {
1263 FIXME("Exit Sub outside Sub?\n");
1267 return exit_label(ctx
, ctx
->sub_end_label
);
1270 static HRESULT
compile_exitfunc_statement(compile_ctx_t
*ctx
)
1272 if(!ctx
->func_end_label
) {
1273 FIXME("Exit Function outside Function?\n");
1277 return exit_label(ctx
, ctx
->func_end_label
);
1280 static HRESULT
compile_exitprop_statement(compile_ctx_t
*ctx
)
1282 if(!ctx
->prop_end_label
) {
1283 FIXME("Exit Property outside Property?\n");
1287 return exit_label(ctx
, ctx
->prop_end_label
);
1290 static HRESULT
compile_onerror_statement(compile_ctx_t
*ctx
, onerror_statement_t
*stat
)
1292 return push_instr_int(ctx
, OP_errmode
, stat
->resume_next
);
1295 static HRESULT
compile_retval_statement(compile_ctx_t
*ctx
, retval_statement_t
*stat
)
1299 hres
= compile_expression(ctx
, stat
->expr
);
1303 hres
= push_instr(ctx
, OP_retval
);
1310 static HRESULT
compile_statement(compile_ctx_t
*ctx
, statement_ctx_t
*stat_ctx
, statement_t
*stat
)
1315 stat_ctx
->next
= ctx
->stat_ctx
;
1316 ctx
->stat_ctx
= stat_ctx
;
1320 ctx
->loc
= stat
->loc
;
1322 switch(stat
->type
) {
1324 hres
= compile_assign_statement(ctx
, (assign_statement_t
*)stat
, FALSE
);
1327 hres
= compile_call_statement(ctx
, (call_statement_t
*)stat
);
1330 hres
= compile_const_statement(ctx
, (const_statement_t
*)stat
);
1333 hres
= compile_dim_statement(ctx
, (dim_statement_t
*)stat
);
1337 hres
= compile_dowhile_statement(ctx
, (while_statement_t
*)stat
);
1340 hres
= compile_exitdo_statement(ctx
);
1343 hres
= compile_exitfor_statement(ctx
);
1346 hres
= compile_exitfunc_statement(ctx
);
1349 hres
= compile_exitprop_statement(ctx
);
1352 hres
= compile_exitsub_statement(ctx
);
1355 hres
= compile_foreach_statement(ctx
, (foreach_statement_t
*)stat
);
1358 hres
= compile_forto_statement(ctx
, (forto_statement_t
*)stat
);
1361 hres
= compile_function_statement(ctx
, (function_statement_t
*)stat
);
1364 hres
= compile_if_statement(ctx
, (if_statement_t
*)stat
);
1367 hres
= compile_onerror_statement(ctx
, (onerror_statement_t
*)stat
);
1370 hres
= compile_redim_statement(ctx
, (redim_statement_t
*)stat
);
1373 hres
= compile_select_statement(ctx
, (select_statement_t
*)stat
);
1376 hres
= compile_assign_statement(ctx
, (assign_statement_t
*)stat
, TRUE
);
1379 hres
= push_instr(ctx
, OP_stop
) ? S_OK
: E_OUTOFMEMORY
;
1383 case STAT_WHILELOOP
:
1384 hres
= compile_while_statement(ctx
, (while_statement_t
*)stat
);
1387 hres
= compile_with_statement(ctx
, (with_statement_t
*)stat
);
1390 hres
= compile_retval_statement(ctx
, (retval_statement_t
*)stat
);
1393 FIXME("Unimplemented statement type %d\n", stat
->type
);
1403 assert(ctx
->stat_ctx
== stat_ctx
);
1404 ctx
->stat_ctx
= stat_ctx
->next
;
1410 static void resolve_labels(compile_ctx_t
*ctx
, unsigned off
)
1414 for(instr
= ctx
->code
->instrs
+off
; instr
< ctx
->code
->instrs
+ctx
->instr_cnt
; instr
++) {
1415 if(instr_info
[instr
->op
].arg1_type
== ARG_ADDR
&& (instr
->arg1
.uint
& LABEL_FLAG
)) {
1416 assert((instr
->arg1
.uint
& ~LABEL_FLAG
) < ctx
->labels_cnt
);
1417 instr
->arg1
.uint
= ctx
->labels
[instr
->arg1
.uint
& ~LABEL_FLAG
];
1419 assert(instr_info
[instr
->op
].arg2_type
!= ARG_ADDR
);
1422 ctx
->labels_cnt
= 0;
1425 static HRESULT
fill_array_desc(compile_ctx_t
*ctx
, dim_decl_t
*dim_decl
, array_desc_t
*array_desc
)
1427 unsigned dim_cnt
= 0, i
;
1430 for(iter
= dim_decl
->dims
; iter
; iter
= iter
->next
)
1433 array_desc
->bounds
= compiler_alloc(ctx
->code
, dim_cnt
* sizeof(SAFEARRAYBOUND
));
1434 if(!array_desc
->bounds
)
1435 return E_OUTOFMEMORY
;
1437 array_desc
->dim_cnt
= dim_cnt
;
1439 for(iter
= dim_decl
->dims
, i
=0; iter
; iter
= iter
->next
, i
++) {
1440 array_desc
->bounds
[i
].cElements
= iter
->val
+1;
1441 array_desc
->bounds
[i
].lLbound
= 0;
1447 static HRESULT
compile_func(compile_ctx_t
*ctx
, statement_t
*stat
, function_t
*func
)
1451 func
->code_off
= ctx
->instr_cnt
;
1453 ctx
->sub_end_label
= 0;
1454 ctx
->func_end_label
= 0;
1455 ctx
->prop_end_label
= 0;
1457 switch(func
->type
) {
1459 ctx
->func_end_label
= alloc_label(ctx
);
1460 if(!ctx
->func_end_label
)
1461 return E_OUTOFMEMORY
;
1464 ctx
->sub_end_label
= alloc_label(ctx
);
1465 if(!ctx
->sub_end_label
)
1466 return E_OUTOFMEMORY
;
1471 ctx
->prop_end_label
= alloc_label(ctx
);
1472 if(!ctx
->prop_end_label
)
1473 return E_OUTOFMEMORY
;
1480 ctx
->dim_decls
= ctx
->dim_decls_tail
= NULL
;
1481 ctx
->const_decls
= NULL
;
1482 hres
= compile_statement(ctx
, NULL
, stat
);
1487 if(ctx
->sub_end_label
)
1488 label_set_addr(ctx
, ctx
->sub_end_label
);
1489 if(ctx
->func_end_label
)
1490 label_set_addr(ctx
, ctx
->func_end_label
);
1491 if(ctx
->prop_end_label
)
1492 label_set_addr(ctx
, ctx
->prop_end_label
);
1494 if(!push_instr(ctx
, OP_ret
))
1495 return E_OUTOFMEMORY
;
1497 resolve_labels(ctx
, func
->code_off
);
1500 dim_decl_t
*dim_decl
;
1503 func
->vars
= compiler_alloc(ctx
->code
, func
->var_cnt
* sizeof(var_desc_t
));
1505 return E_OUTOFMEMORY
;
1507 for(dim_decl
= ctx
->dim_decls
, i
=0; dim_decl
; dim_decl
= dim_decl
->next
, i
++) {
1508 func
->vars
[i
].name
= compiler_alloc_string(ctx
->code
, dim_decl
->name
);
1509 if(!func
->vars
[i
].name
)
1510 return E_OUTOFMEMORY
;
1513 assert(i
== func
->var_cnt
);
1516 if(func
->array_cnt
) {
1517 unsigned array_id
= 0;
1518 dim_decl_t
*dim_decl
;
1520 func
->array_descs
= compiler_alloc(ctx
->code
, func
->array_cnt
* sizeof(array_desc_t
));
1521 if(!func
->array_descs
)
1522 return E_OUTOFMEMORY
;
1524 for(dim_decl
= ctx
->dim_decls
; dim_decl
; dim_decl
= dim_decl
->next
) {
1525 if(dim_decl
->is_array
) {
1526 hres
= fill_array_desc(ctx
, dim_decl
, func
->array_descs
+ array_id
++);
1532 assert(array_id
== func
->array_cnt
);
1538 static BOOL
lookup_funcs_name(compile_ctx_t
*ctx
, const WCHAR
*name
)
1542 for(iter
= ctx
->code
->funcs
; iter
; iter
= iter
->next
) {
1543 if(!wcsicmp(iter
->name
, name
))
1550 static HRESULT
create_function(compile_ctx_t
*ctx
, function_decl_t
*decl
, function_t
**ret
)
1555 if(lookup_dim_decls(ctx
, decl
->name
) || lookup_const_decls(ctx
, decl
->name
, FALSE
)) {
1556 FIXME("%s: redefinition\n", debugstr_w(decl
->name
));
1560 func
= compiler_alloc(ctx
->code
, sizeof(*func
));
1562 return E_OUTOFMEMORY
;
1564 func
->name
= compiler_alloc_string(ctx
->code
, decl
->name
);
1566 return E_OUTOFMEMORY
;
1570 func
->array_cnt
= 0;
1571 func
->code_ctx
= ctx
->code
;
1572 func
->type
= decl
->type
;
1573 func
->is_public
= decl
->is_public
;
1580 for(arg
= decl
->args
; arg
; arg
= arg
->next
)
1583 func
->args
= compiler_alloc(ctx
->code
, func
->arg_cnt
* sizeof(arg_desc_t
));
1585 return E_OUTOFMEMORY
;
1587 for(i
= 0, arg
= decl
->args
; arg
; arg
= arg
->next
, i
++) {
1588 func
->args
[i
].name
= compiler_alloc_string(ctx
->code
, arg
->name
);
1589 if(!func
->args
[i
].name
)
1590 return E_OUTOFMEMORY
;
1591 func
->args
[i
].by_ref
= arg
->by_ref
;
1597 hres
= compile_func(ctx
, decl
->body
, func
);
1605 static BOOL
lookup_class_name(compile_ctx_t
*ctx
, const WCHAR
*name
)
1609 for(iter
= ctx
->code
->classes
; iter
; iter
= iter
->next
) {
1610 if(!wcsicmp(iter
->name
, name
))
1617 static HRESULT
create_class_funcprop(compile_ctx_t
*ctx
, function_decl_t
*func_decl
, vbdisp_funcprop_desc_t
*desc
)
1619 vbdisp_invoke_type_t invoke_type
;
1620 function_decl_t
*funcprop_decl
;
1623 desc
->name
= compiler_alloc_string(ctx
->code
, func_decl
->name
);
1625 return E_OUTOFMEMORY
;
1627 for(funcprop_decl
= func_decl
; funcprop_decl
; funcprop_decl
= funcprop_decl
->next_prop_func
) {
1628 switch(funcprop_decl
->type
) {
1632 invoke_type
= VBDISP_CALLGET
;
1635 invoke_type
= VBDISP_LET
;
1638 invoke_type
= VBDISP_SET
;
1640 DEFAULT_UNREACHABLE
;
1643 assert(!desc
->entries
[invoke_type
]);
1645 if(funcprop_decl
->is_public
)
1646 desc
->is_public
= TRUE
;
1648 hres
= create_function(ctx
, funcprop_decl
, desc
->entries
+invoke_type
);
1656 static BOOL
lookup_class_funcs(class_desc_t
*class_desc
, const WCHAR
*name
)
1660 for(i
=0; i
< class_desc
->func_cnt
; i
++) {
1661 if(class_desc
->funcs
[i
].name
&& !wcsicmp(class_desc
->funcs
[i
].name
, name
))
1668 static HRESULT
compile_class(compile_ctx_t
*ctx
, class_decl_t
*class_decl
)
1670 function_decl_t
*func_decl
, *func_prop_decl
;
1671 BOOL is_default
, have_default
= FALSE
;
1672 class_desc_t
*class_desc
;
1673 dim_decl_t
*prop_decl
;
1677 if(lookup_dim_decls(ctx
, class_decl
->name
) || lookup_funcs_name(ctx
, class_decl
->name
)
1678 || lookup_const_decls(ctx
, class_decl
->name
, FALSE
) || lookup_class_name(ctx
, class_decl
->name
)) {
1679 FIXME("%s: redefinition\n", debugstr_w(class_decl
->name
));
1683 class_desc
= compiler_alloc_zero(ctx
->code
, sizeof(*class_desc
));
1685 return E_OUTOFMEMORY
;
1687 class_desc
->name
= compiler_alloc_string(ctx
->code
, class_decl
->name
);
1688 if(!class_desc
->name
)
1689 return E_OUTOFMEMORY
;
1691 class_desc
->func_cnt
= 1; /* always allocate slot for default getter or method */
1693 for(func_decl
= class_decl
->funcs
; func_decl
; func_decl
= func_decl
->next
) {
1695 for(func_prop_decl
= func_decl
; func_prop_decl
; func_prop_decl
= func_prop_decl
->next_prop_func
) {
1696 if(func_prop_decl
->is_default
) {
1698 FIXME("multiple default getters or methods\n");
1701 is_default
= have_default
= TRUE
;
1706 class_desc
->func_cnt
++;
1709 class_desc
->funcs
= compiler_alloc(ctx
->code
, class_desc
->func_cnt
*sizeof(*class_desc
->funcs
));
1710 if(!class_desc
->funcs
)
1711 return E_OUTOFMEMORY
;
1712 memset(class_desc
->funcs
, 0, class_desc
->func_cnt
*sizeof(*class_desc
->funcs
));
1714 for(func_decl
= class_decl
->funcs
, i
=1; func_decl
; func_decl
= func_decl
->next
, i
++) {
1715 for(func_prop_decl
= func_decl
; func_prop_decl
; func_prop_decl
= func_prop_decl
->next_prop_func
) {
1716 if(func_prop_decl
->is_default
) {
1722 if(!wcsicmp(L
"class_initialize", func_decl
->name
)) {
1723 if(func_decl
->type
!= FUNC_SUB
) {
1724 FIXME("class initializer is not sub\n");
1728 class_desc
->class_initialize_id
= i
;
1729 }else if(!wcsicmp(L
"class_terminate", func_decl
->name
)) {
1730 if(func_decl
->type
!= FUNC_SUB
) {
1731 FIXME("class terminator is not sub\n");
1735 class_desc
->class_terminate_id
= i
;
1738 hres
= create_class_funcprop(ctx
, func_decl
, class_desc
->funcs
+ (func_prop_decl
? 0 : i
));
1743 for(prop_decl
= class_decl
->props
; prop_decl
; prop_decl
= prop_decl
->next
)
1744 class_desc
->prop_cnt
++;
1746 class_desc
->props
= compiler_alloc(ctx
->code
, class_desc
->prop_cnt
*sizeof(*class_desc
->props
));
1747 if(!class_desc
->props
)
1748 return E_OUTOFMEMORY
;
1750 for(prop_decl
= class_decl
->props
, i
=0; prop_decl
; prop_decl
= prop_decl
->next
, i
++) {
1751 if(lookup_class_funcs(class_desc
, prop_decl
->name
)) {
1752 FIXME("Property %s redefined\n", debugstr_w(prop_decl
->name
));
1756 class_desc
->props
[i
].name
= compiler_alloc_string(ctx
->code
, prop_decl
->name
);
1757 if(!class_desc
->props
[i
].name
)
1758 return E_OUTOFMEMORY
;
1760 class_desc
->props
[i
].is_public
= prop_decl
->is_public
;
1761 class_desc
->props
[i
].is_array
= prop_decl
->is_array
;
1763 if(prop_decl
->is_array
)
1764 class_desc
->array_cnt
++;
1767 if(class_desc
->array_cnt
) {
1768 class_desc
->array_descs
= compiler_alloc(ctx
->code
, class_desc
->array_cnt
*sizeof(*class_desc
->array_descs
));
1769 if(!class_desc
->array_descs
)
1770 return E_OUTOFMEMORY
;
1772 for(prop_decl
= class_decl
->props
, i
=0; prop_decl
; prop_decl
= prop_decl
->next
) {
1773 if(prop_decl
->is_array
) {
1774 hres
= fill_array_desc(ctx
, prop_decl
, class_desc
->array_descs
+ i
++);
1781 class_desc
->next
= ctx
->code
->classes
;
1782 ctx
->code
->classes
= class_desc
;
1786 static BOOL
lookup_script_identifier(compile_ctx_t
*ctx
, script_ctx_t
*script
, const WCHAR
*identifier
)
1788 ScriptDisp
*contexts
[] = {
1789 ctx
->code
->named_item
? ctx
->code
->named_item
->script_obj
: NULL
,
1792 class_desc_t
*class;
1796 for(c
= 0; c
< ARRAY_SIZE(contexts
); c
++) {
1797 if(!contexts
[c
]) continue;
1799 for(i
= 0; i
< contexts
[c
]->global_vars_cnt
; i
++) {
1800 if(!wcsicmp(contexts
[c
]->global_vars
[i
]->name
, identifier
))
1804 for(i
= 0; i
< contexts
[c
]->global_funcs_cnt
; i
++) {
1805 if(!wcsicmp(contexts
[c
]->global_funcs
[i
]->name
, identifier
))
1809 for(class = contexts
[c
]->classes
; class; class = class->next
) {
1810 if(!wcsicmp(class->name
, identifier
))
1815 LIST_FOR_EACH_ENTRY(code
, &script
->code_list
, vbscode_t
, entry
) {
1816 unsigned var_cnt
= code
->main_code
.var_cnt
;
1817 var_desc_t
*vars
= code
->main_code
.vars
;
1820 if(!code
->pending_exec
|| (code
->named_item
&& code
->named_item
!= ctx
->code
->named_item
))
1823 for(i
= 0; i
< var_cnt
; i
++) {
1824 if(!wcsicmp(vars
[i
].name
, identifier
))
1828 for(func
= code
->funcs
; func
; func
= func
->next
) {
1829 if(!wcsicmp(func
->name
, identifier
))
1833 for(class = code
->classes
; class; class = class->next
) {
1834 if(!wcsicmp(class->name
, identifier
))
1842 static HRESULT
check_script_collisions(compile_ctx_t
*ctx
, script_ctx_t
*script
)
1844 unsigned i
, var_cnt
= ctx
->code
->main_code
.var_cnt
;
1845 var_desc_t
*vars
= ctx
->code
->main_code
.vars
;
1846 class_desc_t
*class;
1848 for(i
= 0; i
< var_cnt
; i
++) {
1849 if(lookup_script_identifier(ctx
, script
, vars
[i
].name
)) {
1850 FIXME("%s: redefined\n", debugstr_w(vars
[i
].name
));
1855 for(class = ctx
->code
->classes
; class; class = class->next
) {
1856 if(lookup_script_identifier(ctx
, script
, class->name
)) {
1857 FIXME("%s: redefined\n", debugstr_w(class->name
));
1865 void release_vbscode(vbscode_t
*code
)
1872 for(i
=0; i
< code
->bstr_cnt
; i
++)
1873 SysFreeString(code
->bstr_pool
[i
]);
1875 if(code
->named_item
)
1876 release_named_item(code
->named_item
);
1877 heap_pool_free(&code
->heap
);
1879 heap_free(code
->bstr_pool
);
1880 heap_free(code
->source
);
1881 heap_free(code
->instrs
);
1885 static vbscode_t
*alloc_vbscode(compile_ctx_t
*ctx
, const WCHAR
*source
, DWORD_PTR cookie
, unsigned start_line
)
1890 len
= source
? lstrlenW(source
) : 0;
1894 ret
= heap_alloc_zero(sizeof(*ret
));
1898 ret
->source
= heap_alloc((len
+ 1) * sizeof(WCHAR
));
1904 memcpy(ret
->source
, source
, len
* sizeof(WCHAR
));
1905 ret
->source
[len
] = 0;
1907 ret
->cookie
= cookie
;
1908 ret
->start_line
= start_line
;
1910 ret
->instrs
= heap_alloc(32*sizeof(instr_t
));
1912 release_vbscode(ret
);
1917 ctx
->instr_size
= 32;
1918 heap_pool_init(&ret
->heap
);
1920 ret
->main_code
.type
= FUNC_GLOBAL
;
1921 ret
->main_code
.code_ctx
= ret
;
1924 list_init(&ret
->entry
);
1928 static void release_compiler(compile_ctx_t
*ctx
)
1930 parser_release(&ctx
->parser
);
1931 heap_free(ctx
->labels
);
1933 release_vbscode(ctx
->code
);
1936 HRESULT
compile_script(script_ctx_t
*script
, const WCHAR
*src
, const WCHAR
*item_name
, const WCHAR
*delimiter
,
1937 DWORD_PTR cookie
, unsigned start_line
, DWORD flags
, vbscode_t
**ret
)
1939 function_decl_t
*func_decl
;
1940 named_item_t
*item
= NULL
;
1941 class_decl_t
*class_decl
;
1942 function_t
*new_func
;
1948 item
= lookup_named_item(script
, item_name
, 0);
1950 WARN("Unknown context %s\n", debugstr_w(item_name
));
1951 return E_INVALIDARG
;
1953 if(!item
->script_obj
) item
= NULL
;
1956 memset(&ctx
, 0, sizeof(ctx
));
1957 code
= ctx
.code
= alloc_vbscode(&ctx
, src
, cookie
, start_line
);
1959 return E_OUTOFMEMORY
;
1961 code
->named_item
= item
;
1965 hres
= parse_script(&ctx
.parser
, code
->source
, delimiter
, flags
);
1967 if(ctx
.parser
.error_loc
!= -1)
1968 ctx
.loc
= ctx
.parser
.error_loc
;
1969 hres
= compile_error(script
, &ctx
, hres
);
1970 release_vbscode(code
);
1974 hres
= compile_func(&ctx
, ctx
.parser
.stats
, &ctx
.code
->main_code
);
1976 hres
= compile_error(script
, &ctx
, hres
);
1977 release_compiler(&ctx
);
1981 code
->option_explicit
= ctx
.parser
.option_explicit
;
1982 ctx
.global_consts
= ctx
.const_decls
;
1983 code
->option_explicit
= ctx
.parser
.option_explicit
;
1986 for(func_decl
= ctx
.func_decls
; func_decl
; func_decl
= func_decl
->next
) {
1987 hres
= create_function(&ctx
, func_decl
, &new_func
);
1989 hres
= compile_error(script
, &ctx
, hres
);
1990 release_compiler(&ctx
);
1994 new_func
->next
= ctx
.code
->funcs
;
1995 ctx
.code
->funcs
= new_func
;
1998 for(class_decl
= ctx
.parser
.class_decls
; class_decl
; class_decl
= class_decl
->next
) {
1999 hres
= compile_class(&ctx
, class_decl
);
2001 hres
= compile_error(script
, &ctx
, hres
);
2002 release_compiler(&ctx
);
2007 hres
= check_script_collisions(&ctx
, script
);
2009 hres
= compile_error(script
, &ctx
, hres
);
2010 release_compiler(&ctx
);
2014 code
->is_persistent
= (flags
& SCRIPTTEXT_ISPERSISTENT
) != 0;
2016 if(TRACE_ON(vbscript_disas
))
2020 release_compiler(&ctx
);
2022 list_add_tail(&script
->code_list
, &code
->entry
);
2027 HRESULT
compile_procedure(script_ctx_t
*script
, const WCHAR
*src
, const WCHAR
*item_name
, const WCHAR
*delimiter
,
2028 DWORD_PTR cookie
, unsigned start_line
, DWORD flags
, class_desc_t
**ret
)
2034 hres
= compile_script(script
, src
, item_name
, delimiter
, cookie
, start_line
,
2035 flags
& ~SCRIPTTEXT_ISPERSISTENT
, &code
);
2039 if(!(desc
= compiler_alloc_zero(code
, sizeof(*desc
))))
2040 return E_OUTOFMEMORY
;
2041 if(!(desc
->funcs
= compiler_alloc_zero(code
, sizeof(*desc
->funcs
))))
2042 return E_OUTOFMEMORY
;
2046 desc
->funcs
->entries
[VBDISP_CALLGET
] = &code
->main_code
;