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
;
36 struct _statement_ctx_t
*next
;
46 statement_ctx_t
*stat_ctx
;
52 unsigned sub_end_label
;
53 unsigned func_end_label
;
54 unsigned prop_end_label
;
56 dim_decl_t
*dim_decls
;
57 dynamic_var_t
*global_vars
;
59 const_decl_t
*const_decls
;
60 const_decl_t
*global_consts
;
64 function_decl_t
*func_decls
;
66 class_desc_t
*classes
;
69 static HRESULT
compile_expression(compile_ctx_t
*,expression_t
*);
70 static HRESULT
compile_statement(compile_ctx_t
*,statement_ctx_t
*,statement_t
*);
74 instr_arg_type_t arg1_type
;
75 instr_arg_type_t arg2_type
;
77 #define X(n,a,b,c) {#n,b,c},
82 static void dump_instr_arg(instr_arg_type_t type
, instr_arg_t
*arg
)
87 TRACE_(vbscript_disas
)("\t%s", debugstr_w(arg
->str
));
90 TRACE_(vbscript_disas
)("\t%d", arg
->uint
);
94 TRACE_(vbscript_disas
)("\t%u", arg
->uint
);
97 TRACE_(vbscript_disas
)("\t%lf", *arg
->dbl
);
106 static void dump_code(compile_ctx_t
*ctx
)
110 for(instr
= ctx
->code
->instrs
+1; instr
< ctx
->code
->instrs
+ctx
->instr_cnt
; instr
++) {
111 assert(instr
->op
< OP_LAST
);
112 TRACE_(vbscript_disas
)("%d:\t%s", (int)(instr
-ctx
->code
->instrs
), instr_info
[instr
->op
].op_str
);
113 dump_instr_arg(instr_info
[instr
->op
].arg1_type
, &instr
->arg1
);
114 dump_instr_arg(instr_info
[instr
->op
].arg2_type
, &instr
->arg2
);
115 TRACE_(vbscript_disas
)("\n");
119 static inline void *compiler_alloc(vbscode_t
*vbscode
, size_t size
)
121 return vbsheap_alloc(&vbscode
->heap
, size
);
124 static inline void *compiler_alloc_zero(vbscode_t
*vbscode
, size_t size
)
128 ret
= vbsheap_alloc(&vbscode
->heap
, size
);
130 memset(ret
, 0, size
);
134 static WCHAR
*compiler_alloc_string(vbscode_t
*vbscode
, const WCHAR
*str
)
139 size
= (strlenW(str
)+1)*sizeof(WCHAR
);
140 ret
= compiler_alloc(vbscode
, size
);
142 memcpy(ret
, str
, size
);
146 static inline instr_t
*instr_ptr(compile_ctx_t
*ctx
, unsigned id
)
148 assert(id
< ctx
->instr_cnt
);
149 return ctx
->code
->instrs
+ id
;
152 static unsigned push_instr(compile_ctx_t
*ctx
, vbsop_t op
)
154 assert(ctx
->instr_size
&& ctx
->instr_size
>= ctx
->instr_cnt
);
156 if(ctx
->instr_size
== ctx
->instr_cnt
) {
159 new_instr
= heap_realloc(ctx
->code
->instrs
, ctx
->instr_size
*2*sizeof(instr_t
));
163 ctx
->code
->instrs
= new_instr
;
164 ctx
->instr_size
*= 2;
167 ctx
->code
->instrs
[ctx
->instr_cnt
].op
= op
;
168 return ctx
->instr_cnt
++;
171 static HRESULT
push_instr_int(compile_ctx_t
*ctx
, vbsop_t op
, LONG arg
)
175 ret
= push_instr(ctx
, op
);
177 return E_OUTOFMEMORY
;
179 instr_ptr(ctx
, ret
)->arg1
.lng
= arg
;
183 static HRESULT
push_instr_uint(compile_ctx_t
*ctx
, vbsop_t op
, unsigned arg
)
187 ret
= push_instr(ctx
, op
);
189 return E_OUTOFMEMORY
;
191 instr_ptr(ctx
, ret
)->arg1
.uint
= arg
;
195 static HRESULT
push_instr_addr(compile_ctx_t
*ctx
, vbsop_t op
, unsigned arg
)
199 ret
= push_instr(ctx
, op
);
201 return E_OUTOFMEMORY
;
203 instr_ptr(ctx
, ret
)->arg1
.uint
= arg
;
207 static HRESULT
push_instr_str(compile_ctx_t
*ctx
, vbsop_t op
, const WCHAR
*arg
)
212 str
= compiler_alloc_string(ctx
->code
, arg
);
214 return E_OUTOFMEMORY
;
216 instr
= push_instr(ctx
, op
);
218 return E_OUTOFMEMORY
;
220 instr_ptr(ctx
, instr
)->arg1
.str
= str
;
224 static HRESULT
push_instr_double(compile_ctx_t
*ctx
, vbsop_t op
, double arg
)
229 d
= compiler_alloc(ctx
->code
, sizeof(double));
231 return E_OUTOFMEMORY
;
233 instr
= push_instr(ctx
, op
);
235 return E_OUTOFMEMORY
;
238 instr_ptr(ctx
, instr
)->arg1
.dbl
= d
;
242 static BSTR
alloc_bstr_arg(compile_ctx_t
*ctx
, const WCHAR
*str
)
244 if(!ctx
->code
->bstr_pool_size
) {
245 ctx
->code
->bstr_pool
= heap_alloc(8 * sizeof(BSTR
));
246 if(!ctx
->code
->bstr_pool
)
248 ctx
->code
->bstr_pool_size
= 8;
249 }else if(ctx
->code
->bstr_pool_size
== ctx
->code
->bstr_cnt
) {
252 new_pool
= heap_realloc(ctx
->code
->bstr_pool
, ctx
->code
->bstr_pool_size
*2*sizeof(BSTR
));
256 ctx
->code
->bstr_pool
= new_pool
;
257 ctx
->code
->bstr_pool_size
*= 2;
260 ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
] = SysAllocString(str
);
261 if(!ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
])
264 return ctx
->code
->bstr_pool
[ctx
->code
->bstr_cnt
++];
267 static HRESULT
push_instr_bstr(compile_ctx_t
*ctx
, vbsop_t op
, const WCHAR
*arg
)
272 bstr
= alloc_bstr_arg(ctx
, arg
);
274 return E_OUTOFMEMORY
;
276 instr
= push_instr(ctx
, op
);
278 return E_OUTOFMEMORY
;
280 instr_ptr(ctx
, instr
)->arg1
.bstr
= bstr
;
284 static HRESULT
push_instr_bstr_uint(compile_ctx_t
*ctx
, vbsop_t op
, const WCHAR
*arg1
, unsigned arg2
)
289 bstr
= alloc_bstr_arg(ctx
, arg1
);
291 return E_OUTOFMEMORY
;
293 instr
= push_instr(ctx
, op
);
295 return E_OUTOFMEMORY
;
297 instr_ptr(ctx
, instr
)->arg1
.bstr
= bstr
;
298 instr_ptr(ctx
, instr
)->arg2
.uint
= arg2
;
302 static HRESULT
push_instr_uint_bstr(compile_ctx_t
*ctx
, vbsop_t op
, unsigned arg1
, const WCHAR
*arg2
)
307 bstr
= alloc_bstr_arg(ctx
, arg2
);
309 return E_OUTOFMEMORY
;
311 instr
= push_instr(ctx
, op
);
313 return E_OUTOFMEMORY
;
315 instr_ptr(ctx
, instr
)->arg1
.uint
= arg1
;
316 instr_ptr(ctx
, instr
)->arg2
.bstr
= bstr
;
320 #define LABEL_FLAG 0x80000000
322 static unsigned alloc_label(compile_ctx_t
*ctx
)
324 if(!ctx
->labels_size
) {
325 ctx
->labels
= heap_alloc(8 * sizeof(*ctx
->labels
));
328 ctx
->labels_size
= 8;
329 }else if(ctx
->labels_size
== ctx
->labels_cnt
) {
330 unsigned *new_labels
;
332 new_labels
= heap_realloc(ctx
->labels
, 2*ctx
->labels_size
*sizeof(*ctx
->labels
));
336 ctx
->labels
= new_labels
;
337 ctx
->labels_size
*= 2;
340 return ctx
->labels_cnt
++ | LABEL_FLAG
;
343 static inline void label_set_addr(compile_ctx_t
*ctx
, unsigned label
)
345 assert(label
& LABEL_FLAG
);
346 ctx
->labels
[label
& ~LABEL_FLAG
] = ctx
->instr_cnt
;
349 static expression_t
*lookup_const_decls(compile_ctx_t
*ctx
, const WCHAR
*name
, BOOL lookup_global
)
353 for(decl
= ctx
->const_decls
; decl
; decl
= decl
->next
) {
354 if(!strcmpiW(decl
->name
, name
))
355 return decl
->value_expr
;
361 for(decl
= ctx
->global_consts
; decl
; decl
= decl
->next
) {
362 if(!strcmpiW(decl
->name
, name
))
363 return decl
->value_expr
;
369 static HRESULT
compile_args(compile_ctx_t
*ctx
, expression_t
*args
, unsigned *ret
)
371 unsigned arg_cnt
= 0;
375 hres
= compile_expression(ctx
, args
);
387 static HRESULT
compile_member_expression(compile_ctx_t
*ctx
, member_expression_t
*expr
, BOOL ret_val
)
389 unsigned arg_cnt
= 0;
392 if(ret_val
&& !expr
->args
) {
393 expression_t
*const_expr
;
395 const_expr
= lookup_const_decls(ctx
, expr
->identifier
, TRUE
);
397 return compile_expression(ctx
, const_expr
);
400 hres
= compile_args(ctx
, expr
->args
, &arg_cnt
);
405 hres
= compile_expression(ctx
, expr
->obj_expr
);
409 hres
= push_instr_bstr_uint(ctx
, ret_val
? OP_mcall
: OP_mcallv
, expr
->identifier
, arg_cnt
);
411 hres
= push_instr_bstr_uint(ctx
, ret_val
? OP_icall
: OP_icallv
, expr
->identifier
, arg_cnt
);
417 static HRESULT
compile_unary_expression(compile_ctx_t
*ctx
, unary_expression_t
*expr
, vbsop_t op
)
421 hres
= compile_expression(ctx
, expr
->subexpr
);
425 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
428 static HRESULT
compile_binary_expression(compile_ctx_t
*ctx
, binary_expression_t
*expr
, vbsop_t op
)
432 hres
= compile_expression(ctx
, expr
->left
);
436 hres
= compile_expression(ctx
, expr
->right
);
440 return push_instr(ctx
, op
) ? S_OK
: E_OUTOFMEMORY
;
443 static HRESULT
compile_expression(compile_ctx_t
*ctx
, expression_t
*expr
)
447 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_add
);
449 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_and
);
451 return push_instr_int(ctx
, OP_bool
, ((bool_expression_t
*)expr
)->value
);
453 return compile_expression(ctx
, ((unary_expression_t
*)expr
)->subexpr
);
455 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_concat
);
457 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_div
);
459 return push_instr_double(ctx
, OP_double
, ((double_expression_t
*)expr
)->value
);
461 return push_instr(ctx
, OP_empty
) ? S_OK
: E_OUTOFMEMORY
;
463 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_equal
);
465 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_eqv
);
467 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_exp
);
469 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gt
);
471 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_gteq
);
473 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_idiv
);
475 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_is
);
477 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_imp
);
479 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lt
);
481 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_lteq
);
483 return push_instr(ctx
, OP_me
) ? S_OK
: E_OUTOFMEMORY
;
485 return compile_member_expression(ctx
, (member_expression_t
*)expr
, TRUE
);
487 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mod
);
489 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_mul
);
491 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_neg
);
493 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_nequal
);
495 return push_instr_str(ctx
, OP_new
, ((string_expression_t
*)expr
)->value
);
497 return compile_unary_expression(ctx
, (unary_expression_t
*)expr
, OP_not
);
499 return push_instr(ctx
, OP_nothing
) ? S_OK
: E_OUTOFMEMORY
;
501 return push_instr(ctx
, OP_null
) ? S_OK
: E_OUTOFMEMORY
;
503 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_or
);
505 return push_instr_str(ctx
, OP_string
, ((string_expression_t
*)expr
)->value
);
507 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_sub
);
509 return push_instr_int(ctx
, OP_short
, ((int_expression_t
*)expr
)->value
);
511 return push_instr_int(ctx
, OP_long
, ((int_expression_t
*)expr
)->value
);
513 return compile_binary_expression(ctx
, (binary_expression_t
*)expr
, OP_xor
);
515 FIXME("Unimplemented expression type %d\n", expr
->type
);
522 static HRESULT
compile_if_statement(compile_ctx_t
*ctx
, if_statement_t
*stat
)
524 unsigned cnd_jmp
, endif_label
= 0;
525 elseif_decl_t
*elseif_decl
;
528 hres
= compile_expression(ctx
, stat
->expr
);
532 cnd_jmp
= push_instr(ctx
, OP_jmp_false
);
534 return E_OUTOFMEMORY
;
536 hres
= compile_statement(ctx
, NULL
, stat
->if_stat
);
540 if(stat
->else_stat
|| stat
->elseifs
) {
541 endif_label
= alloc_label(ctx
);
543 return E_OUTOFMEMORY
;
545 hres
= push_instr_addr(ctx
, OP_jmp
, endif_label
);
550 for(elseif_decl
= stat
->elseifs
; elseif_decl
; elseif_decl
= elseif_decl
->next
) {
551 instr_ptr(ctx
, cnd_jmp
)->arg1
.uint
= ctx
->instr_cnt
;
553 hres
= compile_expression(ctx
, elseif_decl
->expr
);
557 cnd_jmp
= push_instr(ctx
, OP_jmp_false
);
559 return E_OUTOFMEMORY
;
561 hres
= compile_statement(ctx
, NULL
, elseif_decl
->stat
);
565 hres
= push_instr_addr(ctx
, OP_jmp
, endif_label
);
570 instr_ptr(ctx
, cnd_jmp
)->arg1
.uint
= ctx
->instr_cnt
;
572 if(stat
->else_stat
) {
573 hres
= compile_statement(ctx
, NULL
, stat
->else_stat
);
579 label_set_addr(ctx
, endif_label
);
583 static HRESULT
compile_while_statement(compile_ctx_t
*ctx
, while_statement_t
*stat
)
585 statement_ctx_t stat_ctx
= {0}, *loop_ctx
;
590 start_addr
= ctx
->instr_cnt
;
592 hres
= compile_expression(ctx
, stat
->expr
);
596 jmp_end
= push_instr(ctx
, stat
->stat
.type
== STAT_UNTIL
? OP_jmp_true
: OP_jmp_false
);
598 return E_OUTOFMEMORY
;
600 if(stat
->stat
.type
== STAT_WHILE
) {
603 if(!(stat_ctx
.while_end_label
= alloc_label(ctx
)))
604 return E_OUTOFMEMORY
;
605 loop_ctx
= &stat_ctx
;
608 hres
= compile_statement(ctx
, loop_ctx
, stat
->body
);
612 hres
= push_instr_addr(ctx
, OP_jmp
, start_addr
);
616 instr_ptr(ctx
, jmp_end
)->arg1
.uint
= ctx
->instr_cnt
;
619 label_set_addr(ctx
, stat_ctx
.while_end_label
);
624 static HRESULT
compile_dowhile_statement(compile_ctx_t
*ctx
, while_statement_t
*stat
)
626 statement_ctx_t loop_ctx
= {0};
630 start_addr
= ctx
->instr_cnt
;
632 if(!(loop_ctx
.while_end_label
= alloc_label(ctx
)))
633 return E_OUTOFMEMORY
;
635 hres
= compile_statement(ctx
, &loop_ctx
, stat
->body
);
639 hres
= compile_expression(ctx
, stat
->expr
);
643 hres
= push_instr_addr(ctx
, stat
->stat
.type
== STAT_DOUNTIL
? OP_jmp_false
: OP_jmp_true
, start_addr
);
647 label_set_addr(ctx
, loop_ctx
.while_end_label
);
651 static HRESULT
compile_foreach_statement(compile_ctx_t
*ctx
, foreach_statement_t
*stat
)
653 statement_ctx_t loop_ctx
= {1};
657 hres
= compile_expression(ctx
, stat
->group_expr
);
661 if(!push_instr(ctx
, OP_newenum
))
662 return E_OUTOFMEMORY
;
664 loop_start
= ctx
->instr_cnt
;
665 if(!(loop_ctx
.for_end_label
= alloc_label(ctx
)))
666 return E_OUTOFMEMORY
;
668 hres
= push_instr_uint_bstr(ctx
, OP_enumnext
, loop_ctx
.for_end_label
, stat
->identifier
);
672 hres
= compile_statement(ctx
, &loop_ctx
, stat
->body
);
676 hres
= push_instr_addr(ctx
, OP_jmp
, loop_start
);
680 label_set_addr(ctx
, loop_ctx
.for_end_label
);
684 static HRESULT
compile_forto_statement(compile_ctx_t
*ctx
, forto_statement_t
*stat
)
686 statement_ctx_t loop_ctx
= {2};
687 unsigned step_instr
, instr
;
691 identifier
= alloc_bstr_arg(ctx
, stat
->identifier
);
693 return E_OUTOFMEMORY
;
695 hres
= compile_expression(ctx
, stat
->from_expr
);
699 instr
= push_instr(ctx
, OP_assign_ident
);
701 return E_OUTOFMEMORY
;
702 instr_ptr(ctx
, instr
)->arg1
.bstr
= identifier
;
704 hres
= compile_expression(ctx
, stat
->to_expr
);
708 if(!push_instr(ctx
, OP_val
))
709 return E_OUTOFMEMORY
;
711 if(stat
->step_expr
) {
712 hres
= compile_expression(ctx
, stat
->step_expr
);
716 if(!push_instr(ctx
, OP_val
))
717 return E_OUTOFMEMORY
;
719 hres
= push_instr_int(ctx
, OP_short
, 1);
724 loop_ctx
.for_end_label
= alloc_label(ctx
);
725 if(!loop_ctx
.for_end_label
)
726 return E_OUTOFMEMORY
;
728 step_instr
= push_instr(ctx
, OP_step
);
730 return E_OUTOFMEMORY
;
731 instr_ptr(ctx
, step_instr
)->arg2
.bstr
= identifier
;
732 instr_ptr(ctx
, step_instr
)->arg1
.uint
= loop_ctx
.for_end_label
;
734 hres
= compile_statement(ctx
, &loop_ctx
, stat
->body
);
738 instr
= push_instr(ctx
, OP_incc
);
740 return E_OUTOFMEMORY
;
741 instr_ptr(ctx
, instr
)->arg1
.bstr
= identifier
;
743 hres
= push_instr_addr(ctx
, OP_jmp
, step_instr
);
747 hres
= push_instr_uint(ctx
, OP_pop
, 2);
751 label_set_addr(ctx
, loop_ctx
.for_end_label
);
755 static HRESULT
compile_select_statement(compile_ctx_t
*ctx
, select_statement_t
*stat
)
757 unsigned end_label
, case_cnt
= 0, *case_labels
= NULL
, i
;
758 case_clausule_t
*case_iter
;
759 expression_t
*expr_iter
;
762 hres
= compile_expression(ctx
, stat
->expr
);
766 if(!push_instr(ctx
, OP_val
))
767 return E_OUTOFMEMORY
;
769 end_label
= alloc_label(ctx
);
771 return E_OUTOFMEMORY
;
773 for(case_iter
= stat
->case_clausules
; case_iter
; case_iter
= case_iter
->next
)
777 case_labels
= heap_alloc(case_cnt
*sizeof(*case_labels
));
779 return E_OUTOFMEMORY
;
782 for(case_iter
= stat
->case_clausules
, i
=0; case_iter
; case_iter
= case_iter
->next
, i
++) {
783 case_labels
[i
] = alloc_label(ctx
);
784 if(!case_labels
[i
]) {
785 hres
= E_OUTOFMEMORY
;
792 for(expr_iter
= case_iter
->expr
; expr_iter
; expr_iter
= expr_iter
->next
) {
793 hres
= compile_expression(ctx
, expr_iter
);
797 hres
= push_instr_addr(ctx
, OP_case
, case_labels
[i
]);
804 heap_free(case_labels
);
808 hres
= push_instr_uint(ctx
, OP_pop
, 1);
810 heap_free(case_labels
);
814 hres
= push_instr_addr(ctx
, OP_jmp
, case_iter
? case_labels
[i
] : end_label
);
816 heap_free(case_labels
);
820 for(case_iter
= stat
->case_clausules
, i
=0; case_iter
; case_iter
= case_iter
->next
, i
++) {
821 label_set_addr(ctx
, case_labels
[i
]);
822 hres
= compile_statement(ctx
, NULL
, case_iter
->stat
);
829 hres
= push_instr_addr(ctx
, OP_jmp
, end_label
);
834 heap_free(case_labels
);
838 label_set_addr(ctx
, end_label
);
842 static HRESULT
compile_assignment(compile_ctx_t
*ctx
, member_expression_t
*member_expr
, expression_t
*value_expr
, BOOL is_set
)
848 if(member_expr
->obj_expr
) {
849 hres
= compile_expression(ctx
, member_expr
->obj_expr
);
853 op
= is_set
? OP_set_member
: OP_assign_member
;
855 op
= is_set
? OP_set_ident
: OP_assign_ident
;
858 hres
= compile_expression(ctx
, value_expr
);
862 hres
= compile_args(ctx
, member_expr
->args
, &args_cnt
);
866 return push_instr_bstr_uint(ctx
, op
, member_expr
->identifier
, args_cnt
);
869 static HRESULT
compile_assign_statement(compile_ctx_t
*ctx
, assign_statement_t
*stat
, BOOL is_set
)
871 return compile_assignment(ctx
, stat
->member_expr
, stat
->value_expr
, is_set
);
874 static HRESULT
compile_call_statement(compile_ctx_t
*ctx
, call_statement_t
*stat
)
876 /* It's challenging for parser to distinguish parameterized assignment with one argument from call
877 * with equality expression argument, so we do it in compiler. */
878 if(!stat
->is_strict
&& stat
->expr
->args
&& !stat
->expr
->args
->next
&& stat
->expr
->args
->type
== EXPR_EQUAL
) {
879 binary_expression_t
*eqexpr
= (binary_expression_t
*)stat
->expr
->args
;
881 if(eqexpr
->left
->type
== EXPR_BRACKETS
) {
882 member_expression_t new_member
= *stat
->expr
;
884 WARN("converting call expr to assign expr\n");
886 new_member
.args
= ((unary_expression_t
*)eqexpr
->left
)->subexpr
;
887 return compile_assignment(ctx
, &new_member
, eqexpr
->right
, FALSE
);
891 return compile_member_expression(ctx
, stat
->expr
, FALSE
);
894 static BOOL
lookup_dim_decls(compile_ctx_t
*ctx
, const WCHAR
*name
)
896 dim_decl_t
*dim_decl
;
898 for(dim_decl
= ctx
->dim_decls
; dim_decl
; dim_decl
= dim_decl
->next
) {
899 if(!strcmpiW(dim_decl
->name
, name
))
906 static BOOL
lookup_args_name(compile_ctx_t
*ctx
, const WCHAR
*name
)
910 for(i
= 0; i
< ctx
->func
->arg_cnt
; i
++) {
911 if(!strcmpiW(ctx
->func
->args
[i
].name
, name
))
918 static HRESULT
compile_dim_statement(compile_ctx_t
*ctx
, dim_statement_t
*stat
)
920 dim_decl_t
*dim_decl
= stat
->dim_decls
;
923 if(lookup_dim_decls(ctx
, dim_decl
->name
) || lookup_args_name(ctx
, dim_decl
->name
)
924 || lookup_const_decls(ctx
, dim_decl
->name
, FALSE
)) {
925 FIXME("dim %s name redefined\n", debugstr_w(dim_decl
->name
));
929 ctx
->func
->var_cnt
++;
932 dim_decl
= dim_decl
->next
;
935 dim_decl
->next
= ctx
->dim_decls
;
936 ctx
->dim_decls
= stat
->dim_decls
;
940 static HRESULT
compile_const_statement(compile_ctx_t
*ctx
, const_statement_t
*stat
)
942 const_decl_t
*decl
, *next_decl
= stat
->decls
;
947 if(lookup_const_decls(ctx
, decl
->name
, FALSE
) || lookup_args_name(ctx
, decl
->name
)
948 || lookup_dim_decls(ctx
, decl
->name
)) {
949 FIXME("%s redefined\n", debugstr_w(decl
->name
));
953 if(ctx
->func
->type
== FUNC_GLOBAL
) {
956 hres
= compile_expression(ctx
, decl
->value_expr
);
960 hres
= push_instr_bstr(ctx
, OP_const
, decl
->name
);
965 next_decl
= decl
->next
;
966 decl
->next
= ctx
->const_decls
;
967 ctx
->const_decls
= decl
;
973 static HRESULT
compile_function_statement(compile_ctx_t
*ctx
, function_statement_t
*stat
)
975 if(ctx
->func
!= &ctx
->code
->global_code
) {
976 FIXME("Function is not in the global code\n");
980 stat
->func_decl
->next
= ctx
->func_decls
;
981 ctx
->func_decls
= stat
->func_decl
;
985 static HRESULT
compile_exitdo_statement(compile_ctx_t
*ctx
)
987 statement_ctx_t
*iter
;
988 unsigned pop_cnt
= 0;
990 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
991 pop_cnt
+= iter
->stack_use
;
992 if(iter
->while_end_label
)
996 FIXME("Exit Do outside Do Loop\n");
1003 hres
= push_instr_uint(ctx
, OP_pop
, pop_cnt
);
1008 return push_instr_addr(ctx
, OP_jmp
, iter
->while_end_label
);
1011 static HRESULT
compile_exitfor_statement(compile_ctx_t
*ctx
)
1013 statement_ctx_t
*iter
;
1014 unsigned pop_cnt
= 0;
1016 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
) {
1017 pop_cnt
+= iter
->stack_use
;
1018 if(iter
->for_end_label
)
1022 FIXME("Exit For outside For loop\n");
1029 hres
= push_instr_uint(ctx
, OP_pop
, pop_cnt
);
1034 return push_instr_addr(ctx
, OP_jmp
, iter
->for_end_label
);
1037 static HRESULT
exit_label(compile_ctx_t
*ctx
, unsigned jmp_label
)
1039 statement_ctx_t
*iter
;
1040 unsigned pop_cnt
= 0;
1042 for(iter
= ctx
->stat_ctx
; iter
; iter
= iter
->next
)
1043 pop_cnt
+= iter
->stack_use
;
1048 hres
= push_instr_uint(ctx
, OP_pop
, pop_cnt
);
1053 return push_instr_addr(ctx
, OP_jmp
, jmp_label
);
1056 static HRESULT
compile_exitsub_statement(compile_ctx_t
*ctx
)
1058 if(!ctx
->sub_end_label
) {
1059 FIXME("Exit Sub outside Sub?\n");
1063 return exit_label(ctx
, ctx
->sub_end_label
);
1066 static HRESULT
compile_exitfunc_statement(compile_ctx_t
*ctx
)
1068 if(!ctx
->func_end_label
) {
1069 FIXME("Exit Function outside Function?\n");
1073 return exit_label(ctx
, ctx
->func_end_label
);
1076 static HRESULT
compile_exitprop_statement(compile_ctx_t
*ctx
)
1078 if(!ctx
->prop_end_label
) {
1079 FIXME("Exit Property outside Property?\n");
1083 return exit_label(ctx
, ctx
->prop_end_label
);
1086 static HRESULT
compile_onerror_statement(compile_ctx_t
*ctx
, onerror_statement_t
*stat
)
1088 return push_instr_int(ctx
, OP_errmode
, stat
->resume_next
);
1091 static HRESULT
compile_statement(compile_ctx_t
*ctx
, statement_ctx_t
*stat_ctx
, statement_t
*stat
)
1096 stat_ctx
->next
= ctx
->stat_ctx
;
1097 ctx
->stat_ctx
= stat_ctx
;
1101 switch(stat
->type
) {
1103 hres
= compile_assign_statement(ctx
, (assign_statement_t
*)stat
, FALSE
);
1106 hres
= compile_call_statement(ctx
, (call_statement_t
*)stat
);
1109 hres
= compile_const_statement(ctx
, (const_statement_t
*)stat
);
1112 hres
= compile_dim_statement(ctx
, (dim_statement_t
*)stat
);
1116 hres
= compile_dowhile_statement(ctx
, (while_statement_t
*)stat
);
1119 hres
= compile_exitdo_statement(ctx
);
1122 hres
= compile_exitfor_statement(ctx
);
1125 hres
= compile_exitfunc_statement(ctx
);
1128 hres
= compile_exitprop_statement(ctx
);
1131 hres
= compile_exitsub_statement(ctx
);
1134 hres
= compile_foreach_statement(ctx
, (foreach_statement_t
*)stat
);
1137 hres
= compile_forto_statement(ctx
, (forto_statement_t
*)stat
);
1140 hres
= compile_function_statement(ctx
, (function_statement_t
*)stat
);
1143 hres
= compile_if_statement(ctx
, (if_statement_t
*)stat
);
1146 hres
= compile_onerror_statement(ctx
, (onerror_statement_t
*)stat
);
1149 hres
= compile_select_statement(ctx
, (select_statement_t
*)stat
);
1152 hres
= compile_assign_statement(ctx
, (assign_statement_t
*)stat
, TRUE
);
1155 hres
= push_instr(ctx
, OP_stop
) ? S_OK
: E_OUTOFMEMORY
;
1159 case STAT_WHILELOOP
:
1160 hres
= compile_while_statement(ctx
, (while_statement_t
*)stat
);
1163 FIXME("Unimplemented statement type %d\n", stat
->type
);
1173 assert(ctx
->stat_ctx
== stat_ctx
);
1174 ctx
->stat_ctx
= stat_ctx
->next
;
1180 static void resolve_labels(compile_ctx_t
*ctx
, unsigned off
)
1184 for(instr
= ctx
->code
->instrs
+off
; instr
< ctx
->code
->instrs
+ctx
->instr_cnt
; instr
++) {
1185 if(instr_info
[instr
->op
].arg1_type
== ARG_ADDR
&& (instr
->arg1
.uint
& LABEL_FLAG
)) {
1186 assert((instr
->arg1
.uint
& ~LABEL_FLAG
) < ctx
->labels_cnt
);
1187 instr
->arg1
.uint
= ctx
->labels
[instr
->arg1
.uint
& ~LABEL_FLAG
];
1189 assert(instr_info
[instr
->op
].arg2_type
!= ARG_ADDR
);
1192 ctx
->labels_cnt
= 0;
1195 static HRESULT
compile_func(compile_ctx_t
*ctx
, statement_t
*stat
, function_t
*func
)
1199 func
->code_off
= ctx
->instr_cnt
;
1201 ctx
->sub_end_label
= 0;
1202 ctx
->func_end_label
= 0;
1203 ctx
->prop_end_label
= 0;
1205 switch(func
->type
) {
1207 ctx
->func_end_label
= alloc_label(ctx
);
1208 if(!ctx
->func_end_label
)
1209 return E_OUTOFMEMORY
;
1212 ctx
->sub_end_label
= alloc_label(ctx
);
1213 if(!ctx
->sub_end_label
)
1214 return E_OUTOFMEMORY
;
1220 ctx
->prop_end_label
= alloc_label(ctx
);
1221 if(!ctx
->prop_end_label
)
1222 return E_OUTOFMEMORY
;
1229 ctx
->dim_decls
= NULL
;
1230 ctx
->const_decls
= NULL
;
1231 hres
= compile_statement(ctx
, NULL
, stat
);
1236 if(ctx
->sub_end_label
)
1237 label_set_addr(ctx
, ctx
->sub_end_label
);
1238 if(ctx
->func_end_label
)
1239 label_set_addr(ctx
, ctx
->func_end_label
);
1240 if(ctx
->prop_end_label
)
1241 label_set_addr(ctx
, ctx
->prop_end_label
);
1243 if(!push_instr(ctx
, OP_ret
))
1244 return E_OUTOFMEMORY
;
1246 resolve_labels(ctx
, func
->code_off
);
1249 dim_decl_t
*dim_decl
;
1251 if(func
->type
== FUNC_GLOBAL
) {
1252 dynamic_var_t
*new_var
;
1256 for(dim_decl
= ctx
->dim_decls
; dim_decl
; dim_decl
= dim_decl
->next
) {
1257 new_var
= compiler_alloc(ctx
->code
, sizeof(*new_var
));
1259 return E_OUTOFMEMORY
;
1261 new_var
->name
= compiler_alloc_string(ctx
->code
, dim_decl
->name
);
1263 return E_OUTOFMEMORY
;
1265 V_VT(&new_var
->v
) = VT_EMPTY
;
1266 new_var
->is_const
= FALSE
;
1268 new_var
->next
= ctx
->global_vars
;
1269 ctx
->global_vars
= new_var
;
1274 func
->vars
= compiler_alloc(ctx
->code
, func
->var_cnt
* sizeof(var_desc_t
));
1276 return E_OUTOFMEMORY
;
1278 for(dim_decl
= ctx
->dim_decls
, i
=0; dim_decl
; dim_decl
= dim_decl
->next
, i
++) {
1279 func
->vars
[i
].name
= compiler_alloc_string(ctx
->code
, dim_decl
->name
);
1280 if(!func
->vars
[i
].name
)
1281 return E_OUTOFMEMORY
;
1284 assert(i
== func
->var_cnt
);
1291 static BOOL
lookup_funcs_name(compile_ctx_t
*ctx
, const WCHAR
*name
)
1295 for(iter
= ctx
->funcs
; iter
; iter
= iter
->next
) {
1296 if(!strcmpiW(iter
->name
, name
))
1303 static HRESULT
create_function(compile_ctx_t
*ctx
, function_decl_t
*decl
, function_t
**ret
)
1308 if(lookup_dim_decls(ctx
, decl
->name
) || lookup_funcs_name(ctx
, decl
->name
) || lookup_const_decls(ctx
, decl
->name
, FALSE
)) {
1309 FIXME("%s: redefinition\n", debugstr_w(decl
->name
));
1313 func
= compiler_alloc(ctx
->code
, sizeof(*func
));
1315 return E_OUTOFMEMORY
;
1317 func
->name
= compiler_alloc_string(ctx
->code
, decl
->name
);
1319 return E_OUTOFMEMORY
;
1323 func
->code_ctx
= ctx
->code
;
1324 func
->type
= decl
->type
;
1325 func
->is_public
= decl
->is_public
;
1332 for(arg
= decl
->args
; arg
; arg
= arg
->next
)
1335 func
->args
= compiler_alloc(ctx
->code
, func
->arg_cnt
* sizeof(arg_desc_t
));
1337 return E_OUTOFMEMORY
;
1339 for(i
= 0, arg
= decl
->args
; arg
; arg
= arg
->next
, i
++) {
1340 func
->args
[i
].name
= compiler_alloc_string(ctx
->code
, arg
->name
);
1341 if(!func
->args
[i
].name
)
1342 return E_OUTOFMEMORY
;
1343 func
->args
[i
].by_ref
= arg
->by_ref
;
1349 hres
= compile_func(ctx
, decl
->body
, func
);
1357 static BOOL
lookup_class_name(compile_ctx_t
*ctx
, const WCHAR
*name
)
1361 for(iter
= ctx
->classes
; iter
; iter
= iter
->next
) {
1362 if(!strcmpiW(iter
->name
, name
))
1369 static HRESULT
create_class_funcprop(compile_ctx_t
*ctx
, function_decl_t
*func_decl
, vbdisp_funcprop_desc_t
*desc
)
1371 vbdisp_invoke_type_t invoke_type
;
1372 function_decl_t
*funcprop_decl
;
1375 desc
->name
= compiler_alloc_string(ctx
->code
, func_decl
->name
);
1377 return E_OUTOFMEMORY
;
1379 for(funcprop_decl
= func_decl
; funcprop_decl
; funcprop_decl
= funcprop_decl
->next_prop_func
) {
1380 switch(funcprop_decl
->type
) {
1385 invoke_type
= VBDISP_CALLGET
;
1388 invoke_type
= VBDISP_LET
;
1391 invoke_type
= VBDISP_SET
;
1397 assert(!desc
->entries
[invoke_type
]);
1399 if(funcprop_decl
->is_public
)
1400 desc
->is_public
= TRUE
;
1402 hres
= create_function(ctx
, funcprop_decl
, desc
->entries
+invoke_type
);
1410 static BOOL
lookup_class_funcs(class_desc_t
*class_desc
, const WCHAR
*name
)
1414 for(i
=0; i
< class_desc
->func_cnt
; i
++) {
1415 if(class_desc
->funcs
[i
].name
&& !strcmpiW(class_desc
->funcs
[i
].name
, name
))
1422 static HRESULT
compile_class(compile_ctx_t
*ctx
, class_decl_t
*class_decl
)
1424 function_decl_t
*func_decl
, *func_prop_decl
;
1425 class_prop_decl_t
*prop_decl
;
1426 class_desc_t
*class_desc
;
1430 static const WCHAR class_initializeW
[] = {'c','l','a','s','s','_','i','n','i','t','i','a','l','i','z','e',0};
1431 static const WCHAR class_terminateW
[] = {'c','l','a','s','s','_','t','e','r','m','i','n','a','t','e',0};
1433 if(lookup_dim_decls(ctx
, class_decl
->name
) || lookup_funcs_name(ctx
, class_decl
->name
)
1434 || lookup_const_decls(ctx
, class_decl
->name
, FALSE
) || lookup_class_name(ctx
, class_decl
->name
)) {
1435 FIXME("%s: redefinition\n", debugstr_w(class_decl
->name
));
1439 class_desc
= compiler_alloc_zero(ctx
->code
, sizeof(*class_desc
));
1441 return E_OUTOFMEMORY
;
1443 class_desc
->name
= compiler_alloc_string(ctx
->code
, class_decl
->name
);
1444 if(!class_desc
->name
)
1445 return E_OUTOFMEMORY
;
1447 class_desc
->func_cnt
= 1; /* always allocate slot for default getter */
1449 for(func_decl
= class_decl
->funcs
; func_decl
; func_decl
= func_decl
->next
) {
1450 for(func_prop_decl
= func_decl
; func_prop_decl
; func_prop_decl
= func_prop_decl
->next_prop_func
) {
1451 if(func_prop_decl
->type
== FUNC_DEFGET
)
1455 class_desc
->func_cnt
++;
1458 class_desc
->funcs
= compiler_alloc(ctx
->code
, class_desc
->func_cnt
*sizeof(*class_desc
->funcs
));
1459 if(!class_desc
->funcs
)
1460 return E_OUTOFMEMORY
;
1461 memset(class_desc
->funcs
, 0, class_desc
->func_cnt
*sizeof(*class_desc
->funcs
));
1463 for(func_decl
= class_decl
->funcs
, i
=1; func_decl
; func_decl
= func_decl
->next
, i
++) {
1464 for(func_prop_decl
= func_decl
; func_prop_decl
; func_prop_decl
= func_prop_decl
->next_prop_func
) {
1465 if(func_prop_decl
->type
== FUNC_DEFGET
) {
1471 if(!strcmpiW(class_initializeW
, func_decl
->name
)) {
1472 if(func_decl
->type
!= FUNC_SUB
) {
1473 FIXME("class initializer is not sub\n");
1477 class_desc
->class_initialize_id
= i
;
1478 }else if(!strcmpiW(class_terminateW
, func_decl
->name
)) {
1479 if(func_decl
->type
!= FUNC_SUB
) {
1480 FIXME("class terminator is not sub\n");
1484 class_desc
->class_terminate_id
= i
;
1487 hres
= create_class_funcprop(ctx
, func_decl
, class_desc
->funcs
+ (func_prop_decl
? 0 : i
));
1492 for(prop_decl
= class_decl
->props
; prop_decl
; prop_decl
= prop_decl
->next
)
1493 class_desc
->prop_cnt
++;
1495 class_desc
->props
= compiler_alloc(ctx
->code
, class_desc
->prop_cnt
*sizeof(*class_desc
->props
));
1496 if(!class_desc
->props
)
1497 return E_OUTOFMEMORY
;
1499 for(prop_decl
= class_decl
->props
, i
=0; prop_decl
; prop_decl
= prop_decl
->next
, i
++) {
1500 if(lookup_class_funcs(class_desc
, prop_decl
->name
)) {
1501 FIXME("Property %s redefined\n", debugstr_w(prop_decl
->name
));
1505 class_desc
->props
[i
].name
= compiler_alloc_string(ctx
->code
, prop_decl
->name
);
1506 if(!class_desc
->props
[i
].name
)
1507 return E_OUTOFMEMORY
;
1509 class_desc
->props
[i
].is_public
= prop_decl
->is_public
;
1512 class_desc
->next
= ctx
->classes
;
1513 ctx
->classes
= class_desc
;
1517 static BOOL
lookup_script_identifier(script_ctx_t
*script
, const WCHAR
*identifier
)
1519 class_desc_t
*class;
1523 for(var
= script
->global_vars
; var
; var
= var
->next
) {
1524 if(!strcmpiW(var
->name
, identifier
))
1528 for(func
= script
->global_funcs
; func
; func
= func
->next
) {
1529 if(!strcmpiW(func
->name
, identifier
))
1533 for(class = script
->classes
; class; class = class->next
) {
1534 if(!strcmpiW(class->name
, identifier
))
1541 static HRESULT
check_script_collisions(compile_ctx_t
*ctx
, script_ctx_t
*script
)
1543 class_desc_t
*class;
1547 for(var
= ctx
->global_vars
; var
; var
= var
->next
) {
1548 if(lookup_script_identifier(script
, var
->name
)) {
1549 FIXME("%s: redefined\n", debugstr_w(var
->name
));
1554 for(func
= ctx
->funcs
; func
; func
= func
->next
) {
1555 if(lookup_script_identifier(script
, func
->name
)) {
1556 FIXME("%s: redefined\n", debugstr_w(func
->name
));
1561 for(class = ctx
->classes
; class; class = class->next
) {
1562 if(lookup_script_identifier(script
, class->name
)) {
1563 FIXME("%s: redefined\n", debugstr_w(class->name
));
1571 void release_vbscode(vbscode_t
*code
)
1575 list_remove(&code
->entry
);
1577 for(i
=0; i
< code
->bstr_cnt
; i
++)
1578 SysFreeString(code
->bstr_pool
[i
]);
1580 vbsheap_free(&code
->heap
);
1582 heap_free(code
->bstr_pool
);
1583 heap_free(code
->source
);
1584 heap_free(code
->instrs
);
1588 static vbscode_t
*alloc_vbscode(compile_ctx_t
*ctx
, const WCHAR
*source
)
1592 ret
= heap_alloc(sizeof(*ret
));
1596 ret
->source
= heap_strdupW(source
);
1602 ret
->instrs
= heap_alloc(32*sizeof(instr_t
));
1604 release_vbscode(ret
);
1609 ctx
->instr_size
= 32;
1610 vbsheap_init(&ret
->heap
);
1612 ret
->option_explicit
= ctx
->parser
.option_explicit
;
1614 ret
->bstr_pool
= NULL
;
1615 ret
->bstr_pool_size
= 0;
1617 ret
->global_executed
= FALSE
;
1619 ret
->global_code
.type
= FUNC_GLOBAL
;
1620 ret
->global_code
.name
= NULL
;
1621 ret
->global_code
.code_ctx
= ret
;
1622 ret
->global_code
.vars
= NULL
;
1623 ret
->global_code
.var_cnt
= 0;
1624 ret
->global_code
.arg_cnt
= 0;
1625 ret
->global_code
.args
= NULL
;
1627 list_init(&ret
->entry
);
1631 static void release_compiler(compile_ctx_t
*ctx
)
1633 parser_release(&ctx
->parser
);
1634 heap_free(ctx
->labels
);
1636 release_vbscode(ctx
->code
);
1639 HRESULT
compile_script(script_ctx_t
*script
, const WCHAR
*src
, vbscode_t
**ret
)
1641 function_t
*new_func
;
1642 function_decl_t
*func_decl
;
1643 class_decl_t
*class_decl
;
1648 hres
= parse_script(&ctx
.parser
, src
);
1652 code
= ctx
.code
= alloc_vbscode(&ctx
, src
);
1654 return E_OUTOFMEMORY
;
1657 ctx
.func_decls
= NULL
;
1658 ctx
.global_vars
= NULL
;
1659 ctx
.dim_decls
= NULL
;
1662 ctx
.global_consts
= NULL
;
1663 ctx
.stat_ctx
= NULL
;
1664 ctx
.labels_cnt
= ctx
.labels_size
= 0;
1666 hres
= compile_func(&ctx
, ctx
.parser
.stats
, &ctx
.code
->global_code
);
1668 release_compiler(&ctx
);
1672 ctx
.global_consts
= ctx
.const_decls
;
1674 for(func_decl
= ctx
.func_decls
; func_decl
; func_decl
= func_decl
->next
) {
1675 hres
= create_function(&ctx
, func_decl
, &new_func
);
1677 release_compiler(&ctx
);
1681 new_func
->next
= ctx
.funcs
;
1682 ctx
.funcs
= new_func
;
1685 for(class_decl
= ctx
.parser
.class_decls
; class_decl
; class_decl
= class_decl
->next
) {
1686 hres
= compile_class(&ctx
, class_decl
);
1688 release_compiler(&ctx
);
1693 hres
= check_script_collisions(&ctx
, script
);
1695 release_compiler(&ctx
);
1699 if(ctx
.global_vars
) {
1702 for(var
= ctx
.global_vars
; var
->next
; var
= var
->next
);
1704 var
->next
= script
->global_vars
;
1705 script
->global_vars
= ctx
.global_vars
;
1709 for(new_func
= ctx
.funcs
; new_func
->next
; new_func
= new_func
->next
);
1711 new_func
->next
= script
->global_funcs
;
1712 script
->global_funcs
= ctx
.funcs
;
1716 class_desc_t
*class = ctx
.classes
;
1719 class->ctx
= script
;
1722 class = class->next
;
1725 class->next
= script
->classes
;
1726 script
->classes
= ctx
.classes
;
1729 if(TRACE_ON(vbscript_disas
))
1733 release_compiler(&ctx
);
1735 list_add_tail(&script
->code_list
, &code
->entry
);