jscript: Use bytecode for '>>=' expression.
[wine/multimedia.git] / dlls / vbscript / compile.c
blob9b3f3e67b49f63497769c1adbc59db69ca95f12e
1 /*
2 * Copyright 2011 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <assert.h>
21 #include "vbscript.h"
22 #include "parse.h"
23 #include "parser.tab.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
28 WINE_DECLARE_DEBUG_CHANNEL(vbscript_disas);
30 typedef struct {
31 parser_ctx_t parser;
33 unsigned instr_cnt;
34 unsigned instr_size;
35 vbscode_t *code;
37 unsigned *labels;
38 unsigned labels_size;
39 unsigned labels_cnt;
41 unsigned while_end_label;
42 unsigned for_end_label;
43 unsigned sub_end_label;
44 unsigned func_end_label;
45 unsigned prop_end_label;
47 dim_decl_t *dim_decls;
48 dynamic_var_t *global_vars;
50 const_decl_t *const_decls;
51 const_decl_t *global_consts;
53 function_t *func;
54 function_t *funcs;
55 function_decl_t *func_decls;
57 class_desc_t *classes;
58 } compile_ctx_t;
60 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
61 static HRESULT compile_statement(compile_ctx_t*,statement_t*);
63 static const struct {
64 const char *op_str;
65 instr_arg_type_t arg1_type;
66 instr_arg_type_t arg2_type;
67 } instr_info[] = {
68 #define X(n,a,b,c) {#n,b,c},
69 OP_LIST
70 #undef X
73 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
75 switch(type) {
76 case ARG_STR:
77 case ARG_BSTR:
78 TRACE_(vbscript_disas)("\t%s", debugstr_w(arg->str));
79 break;
80 case ARG_INT:
81 TRACE_(vbscript_disas)("\t%d", arg->uint);
82 break;
83 case ARG_UINT:
84 case ARG_ADDR:
85 TRACE_(vbscript_disas)("\t%u", arg->uint);
86 break;
87 case ARG_DOUBLE:
88 TRACE_(vbscript_disas)("\t%lf", *arg->dbl);
89 break;
90 case ARG_NONE:
91 break;
92 default:
93 assert(0);
97 static void dump_code(compile_ctx_t *ctx)
99 instr_t *instr;
101 for(instr = ctx->code->instrs; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
102 TRACE_(vbscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
103 dump_instr_arg(instr_info[instr->op].arg1_type, &instr->arg1);
104 dump_instr_arg(instr_info[instr->op].arg2_type, &instr->arg2);
105 TRACE_(vbscript_disas)("\n");
109 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
111 return vbsheap_alloc(&vbscode->heap, size);
114 static inline void *compiler_alloc_zero(vbscode_t *vbscode, size_t size)
116 void *ret;
118 ret = vbsheap_alloc(&vbscode->heap, size);
119 if(ret)
120 memset(ret, 0, size);
121 return ret;
124 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
126 size_t size;
127 WCHAR *ret;
129 size = (strlenW(str)+1)*sizeof(WCHAR);
130 ret = compiler_alloc(vbscode, size);
131 if(ret)
132 memcpy(ret, str, size);
133 return ret;
136 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
138 assert(id < ctx->instr_cnt);
139 return ctx->code->instrs + id;
142 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
144 assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
146 if(ctx->instr_size == ctx->instr_cnt) {
147 instr_t *new_instr;
149 new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
150 if(!new_instr)
151 return -1;
153 ctx->code->instrs = new_instr;
154 ctx->instr_size *= 2;
157 ctx->code->instrs[ctx->instr_cnt].op = op;
158 return ctx->instr_cnt++;
161 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
163 unsigned ret;
165 ret = push_instr(ctx, op);
166 if(ret == -1)
167 return E_OUTOFMEMORY;
169 instr_ptr(ctx, ret)->arg1.lng = arg;
170 return S_OK;
173 static HRESULT push_instr_uint(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
175 unsigned ret;
177 ret = push_instr(ctx, op);
178 if(ret == -1)
179 return E_OUTOFMEMORY;
181 instr_ptr(ctx, ret)->arg1.uint = arg;
182 return S_OK;
185 static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
187 unsigned ret;
189 ret = push_instr(ctx, op);
190 if(ret == -1)
191 return E_OUTOFMEMORY;
193 instr_ptr(ctx, ret)->arg1.uint = arg;
194 return S_OK;
197 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
199 unsigned instr;
200 WCHAR *str;
202 str = compiler_alloc_string(ctx->code, arg);
203 if(!str)
204 return E_OUTOFMEMORY;
206 instr = push_instr(ctx, op);
207 if(instr == -1)
208 return E_OUTOFMEMORY;
210 instr_ptr(ctx, instr)->arg1.str = str;
211 return S_OK;
214 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
216 unsigned instr;
217 double *d;
219 d = compiler_alloc(ctx->code, sizeof(double));
220 if(!d)
221 return E_OUTOFMEMORY;
223 instr = push_instr(ctx, op);
224 if(instr == -1)
225 return E_OUTOFMEMORY;
227 *d = arg;
228 instr_ptr(ctx, instr)->arg1.dbl = d;
229 return S_OK;
232 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
234 if(!ctx->code->bstr_pool_size) {
235 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
236 if(!ctx->code->bstr_pool)
237 return NULL;
238 ctx->code->bstr_pool_size = 8;
239 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
240 BSTR *new_pool;
242 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
243 if(!new_pool)
244 return NULL;
246 ctx->code->bstr_pool = new_pool;
247 ctx->code->bstr_pool_size *= 2;
250 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
251 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
252 return NULL;
254 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
257 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
259 unsigned instr;
260 BSTR bstr;
262 bstr = alloc_bstr_arg(ctx, arg);
263 if(!bstr)
264 return E_OUTOFMEMORY;
266 instr = push_instr(ctx, op);
267 if(instr == -1)
268 return E_OUTOFMEMORY;
270 instr_ptr(ctx, instr)->arg1.bstr = bstr;
271 return S_OK;
274 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
276 unsigned instr;
277 BSTR bstr;
279 bstr = alloc_bstr_arg(ctx, arg1);
280 if(!bstr)
281 return E_OUTOFMEMORY;
283 instr = push_instr(ctx, op);
284 if(instr == -1)
285 return E_OUTOFMEMORY;
287 instr_ptr(ctx, instr)->arg1.bstr = bstr;
288 instr_ptr(ctx, instr)->arg2.uint = arg2;
289 return S_OK;
292 #define LABEL_FLAG 0x80000000
294 static unsigned alloc_label(compile_ctx_t *ctx)
296 if(!ctx->labels_size) {
297 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
298 if(!ctx->labels)
299 return -1;
300 ctx->labels_size = 8;
301 }else if(ctx->labels_size == ctx->labels_cnt) {
302 unsigned *new_labels;
304 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
305 if(!new_labels)
306 return -1;
308 ctx->labels = new_labels;
309 ctx->labels_size *= 2;
312 return ctx->labels_cnt++ | LABEL_FLAG;
315 static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
317 assert(label & LABEL_FLAG);
318 ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
321 static expression_t *lookup_const_decls(compile_ctx_t *ctx, const WCHAR *name, BOOL lookup_global)
323 const_decl_t *decl;
325 for(decl = ctx->const_decls; decl; decl = decl->next) {
326 if(!strcmpiW(decl->name, name))
327 return decl->value_expr;
330 if(!lookup_global)
331 return NULL;
333 for(decl = ctx->global_consts; decl; decl = decl->next) {
334 if(!strcmpiW(decl->name, name))
335 return decl->value_expr;
338 return NULL;
341 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
343 unsigned arg_cnt = 0;
344 HRESULT hres;
346 while(args) {
347 hres = compile_expression(ctx, args);
348 if(FAILED(hres))
349 return hres;
351 arg_cnt++;
352 args = args->next;
355 *ret = arg_cnt;
356 return S_OK;
359 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
361 unsigned arg_cnt = 0;
362 HRESULT hres;
364 if(ret_val && !expr->args) {
365 expression_t *const_expr;
367 const_expr = lookup_const_decls(ctx, expr->identifier, TRUE);
368 if(const_expr)
369 return compile_expression(ctx, const_expr);
372 hres = compile_args(ctx, expr->args, &arg_cnt);
373 if(FAILED(hres))
374 return hres;
376 if(expr->obj_expr) {
377 hres = compile_expression(ctx, expr->obj_expr);
378 if(FAILED(hres))
379 return hres;
381 hres = push_instr_bstr_uint(ctx, ret_val ? OP_mcall : OP_mcallv, expr->identifier, arg_cnt);
382 }else {
383 hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
386 return hres;
389 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
391 HRESULT hres;
393 hres = compile_expression(ctx, expr->subexpr);
394 if(FAILED(hres))
395 return hres;
397 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
400 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
402 HRESULT hres;
404 hres = compile_expression(ctx, expr->left);
405 if(FAILED(hres))
406 return hres;
408 hres = compile_expression(ctx, expr->right);
409 if(FAILED(hres))
410 return hres;
412 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
415 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
417 switch(expr->type) {
418 case EXPR_ADD:
419 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
420 case EXPR_AND:
421 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
422 case EXPR_BOOL:
423 return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
424 case EXPR_CONCAT:
425 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
426 case EXPR_DIV:
427 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
428 case EXPR_DOUBLE:
429 return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
430 case EXPR_EMPTY:
431 return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
432 case EXPR_EQUAL:
433 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
434 case EXPR_EQV:
435 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eqv);
436 case EXPR_EXP:
437 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
438 case EXPR_GT:
439 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
440 case EXPR_GTEQ:
441 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
442 case EXPR_IDIV:
443 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
444 case EXPR_IS:
445 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_is);
446 case EXPR_IMP:
447 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_imp);
448 case EXPR_LT:
449 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
450 case EXPR_LTEQ:
451 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
452 case EXPR_ME:
453 return push_instr(ctx, OP_me) != -1 ? S_OK : E_OUTOFMEMORY;
454 case EXPR_MEMBER:
455 return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
456 case EXPR_MOD:
457 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
458 case EXPR_MUL:
459 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
460 case EXPR_NEG:
461 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
462 case EXPR_NEQUAL:
463 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
464 case EXPR_NEW:
465 return push_instr_str(ctx, OP_new, ((string_expression_t*)expr)->value);
466 case EXPR_NOT:
467 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
468 case EXPR_NOTHING:
469 return push_instr(ctx, OP_nothing) != -1 ? S_OK : E_OUTOFMEMORY;
470 case EXPR_NULL:
471 return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
472 case EXPR_OR:
473 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
474 case EXPR_STRING:
475 return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
476 case EXPR_SUB:
477 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
478 case EXPR_USHORT:
479 return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
480 case EXPR_ULONG:
481 return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
482 case EXPR_XOR:
483 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
484 default:
485 FIXME("Unimplemented expression type %d\n", expr->type);
486 return E_NOTIMPL;
489 return S_OK;
492 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
494 unsigned cnd_jmp, endif_label = -1;
495 elseif_decl_t *elseif_decl;
496 HRESULT hres;
498 hres = compile_expression(ctx, stat->expr);
499 if(FAILED(hres))
500 return hres;
502 cnd_jmp = push_instr(ctx, OP_jmp_false);
503 if(cnd_jmp == -1)
504 return E_OUTOFMEMORY;
506 hres = compile_statement(ctx, stat->if_stat);
507 if(FAILED(hres))
508 return hres;
510 if(stat->else_stat || stat->elseifs) {
511 endif_label = alloc_label(ctx);
512 if(endif_label == -1)
513 return E_OUTOFMEMORY;
515 hres = push_instr_addr(ctx, OP_jmp, endif_label);
516 if(FAILED(hres))
517 return hres;
520 for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
521 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
523 hres = compile_expression(ctx, elseif_decl->expr);
524 if(FAILED(hres))
525 return hres;
527 cnd_jmp = push_instr(ctx, OP_jmp_false);
528 if(cnd_jmp == -1)
529 return E_OUTOFMEMORY;
531 hres = compile_statement(ctx, elseif_decl->stat);
532 if(FAILED(hres))
533 return hres;
535 hres = push_instr_addr(ctx, OP_jmp, endif_label);
536 if(FAILED(hres))
537 return hres;
540 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
542 if(stat->else_stat) {
543 hres = compile_statement(ctx, stat->else_stat);
544 if(FAILED(hres))
545 return hres;
548 if(endif_label != -1)
549 label_set_addr(ctx, endif_label);
550 return S_OK;
553 static HRESULT compile_while_statement(compile_ctx_t *ctx, while_statement_t *stat)
555 unsigned start_addr, prev_label;
556 unsigned jmp_end;
557 HRESULT hres;
559 start_addr = ctx->instr_cnt;
561 hres = compile_expression(ctx, stat->expr);
562 if(FAILED(hres))
563 return hres;
565 jmp_end = push_instr(ctx, stat->stat.type == STAT_UNTIL ? OP_jmp_true : OP_jmp_false);
566 if(jmp_end == -1)
567 return E_OUTOFMEMORY;
569 prev_label = ctx->while_end_label;
570 if(stat->stat.type != STAT_WHILE && (ctx->while_end_label = alloc_label(ctx)) == -1)
571 return E_OUTOFMEMORY;
573 hres = compile_statement(ctx, stat->body);
574 if(FAILED(hres))
575 return hres;
577 hres = push_instr_addr(ctx, OP_jmp, start_addr);
578 if(FAILED(hres))
579 return hres;
581 instr_ptr(ctx, jmp_end)->arg1.uint = ctx->instr_cnt;
583 if(stat->stat.type != STAT_WHILE) {
584 label_set_addr(ctx, ctx->while_end_label);
585 ctx->while_end_label = prev_label;
588 return S_OK;
591 static HRESULT compile_dowhile_statement(compile_ctx_t *ctx, while_statement_t *stat)
593 unsigned start_addr, prev_label;
594 HRESULT hres;
596 start_addr = ctx->instr_cnt;
598 prev_label = ctx->while_end_label;
599 if((ctx->while_end_label = alloc_label(ctx)) == -1)
600 return E_OUTOFMEMORY;
602 hres = compile_statement(ctx, stat->body);
603 if(FAILED(hres))
604 return hres;
606 hres = compile_expression(ctx, stat->expr);
607 if(FAILED(hres))
608 return hres;
610 hres = push_instr_addr(ctx, stat->stat.type == STAT_DOUNTIL ? OP_jmp_false : OP_jmp_true, start_addr);
611 if(FAILED(hres))
612 return hres;
614 label_set_addr(ctx, ctx->while_end_label);
615 ctx->while_end_label = prev_label;
616 return S_OK;
619 static HRESULT compile_forto_statement(compile_ctx_t *ctx, forto_statement_t *stat)
621 unsigned step_instr, instr, prev_label;
622 BSTR identifier;
623 HRESULT hres;
625 identifier = alloc_bstr_arg(ctx, stat->identifier);
626 if(!identifier)
627 return E_OUTOFMEMORY;
629 hres = compile_expression(ctx, stat->from_expr);
630 if(FAILED(hres))
631 return hres;
633 instr = push_instr(ctx, OP_assign_ident);
634 if(instr == -1)
635 return E_OUTOFMEMORY;
636 instr_ptr(ctx, instr)->arg1.bstr = identifier;
638 hres = compile_expression(ctx, stat->to_expr);
639 if(FAILED(hres))
640 return hres;
642 if(push_instr(ctx, OP_val) == -1)
643 return E_OUTOFMEMORY;
645 if(stat->step_expr) {
646 hres = compile_expression(ctx, stat->step_expr);
647 if(FAILED(hres))
648 return hres;
650 if(push_instr(ctx, OP_val) == -1)
651 return E_OUTOFMEMORY;
652 }else {
653 hres = push_instr_int(ctx, OP_short, 1);
654 if(FAILED(hres))
655 return hres;
658 prev_label = ctx->for_end_label;
659 ctx->for_end_label = alloc_label(ctx);
660 if(ctx->for_end_label == -1)
661 return E_OUTOFMEMORY;
663 step_instr = push_instr(ctx, OP_step);
664 if(step_instr == -1)
665 return E_OUTOFMEMORY;
666 instr_ptr(ctx, step_instr)->arg2.bstr = identifier;
667 instr_ptr(ctx, step_instr)->arg1.uint = ctx->for_end_label;
669 hres = compile_statement(ctx, stat->body);
670 if(FAILED(hres))
671 return hres;
673 instr = push_instr(ctx, OP_incc);
674 if(instr == -1)
675 return E_OUTOFMEMORY;
676 instr_ptr(ctx, instr)->arg1.bstr = identifier;
678 hres = push_instr_addr(ctx, OP_jmp, step_instr);
679 if(FAILED(hres))
680 return hres;
682 label_set_addr(ctx, ctx->for_end_label);
683 ctx->for_end_label = prev_label;
685 return push_instr_uint(ctx, OP_pop, 2);
688 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat, BOOL is_set)
690 HRESULT hres;
692 hres = compile_expression(ctx, stat->value_expr);
693 if(FAILED(hres))
694 return hres;
696 if(stat->member_expr->args) {
697 FIXME("arguments support not implemented\n");
698 return E_NOTIMPL;
701 if(stat->member_expr->obj_expr) {
702 hres = compile_expression(ctx, stat->member_expr->obj_expr);
703 if(FAILED(hres))
704 return hres;
706 hres = push_instr_bstr(ctx, is_set ? OP_set_member : OP_assign_member, stat->member_expr->identifier);
707 }else {
708 hres = push_instr_bstr(ctx, is_set ? OP_set_ident : OP_assign_ident, stat->member_expr->identifier);
711 return hres;
714 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
716 dim_decl_t *dim_decl;
718 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
719 if(!strcmpiW(dim_decl->name, name))
720 return TRUE;
723 return FALSE;
726 static BOOL lookup_args_name(compile_ctx_t *ctx, const WCHAR *name)
728 unsigned i;
730 for(i = 0; i < ctx->func->arg_cnt; i++) {
731 if(!strcmpiW(ctx->func->args[i].name, name))
732 return TRUE;
735 return FALSE;
738 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
740 dim_decl_t *dim_decl = stat->dim_decls;
742 while(1) {
743 if(lookup_dim_decls(ctx, dim_decl->name) || lookup_args_name(ctx, dim_decl->name)
744 || lookup_const_decls(ctx, dim_decl->name, FALSE)) {
745 FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
746 return E_FAIL;
749 ctx->func->var_cnt++;
750 if(!dim_decl->next)
751 break;
752 dim_decl = dim_decl->next;
755 dim_decl->next = ctx->dim_decls;
756 ctx->dim_decls = stat->dim_decls;
757 return S_OK;
760 static HRESULT compile_const_statement(compile_ctx_t *ctx, const_statement_t *stat)
762 const_decl_t *decl, *next_decl = stat->decls;
764 do {
765 decl = next_decl;
767 if(lookup_const_decls(ctx, decl->name, FALSE) || lookup_args_name(ctx, decl->name)
768 || lookup_dim_decls(ctx, decl->name)) {
769 FIXME("%s redefined\n", debugstr_w(decl->name));
770 return E_FAIL;
773 if(ctx->func->type == FUNC_GLOBAL) {
774 HRESULT hres;
776 hres = compile_expression(ctx, decl->value_expr);
777 if(FAILED(hres))
778 return hres;
780 hres = push_instr_bstr(ctx, OP_const, decl->name);
781 if(FAILED(hres))
782 return hres;
785 next_decl = decl->next;
786 decl->next = ctx->const_decls;
787 ctx->const_decls = decl;
788 } while(next_decl);
790 return S_OK;
793 static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
795 if(ctx->func != &ctx->code->global_code) {
796 FIXME("Function is not in the global code\n");
797 return E_FAIL;
800 stat->func_decl->next = ctx->func_decls;
801 ctx->func_decls = stat->func_decl;
802 return S_OK;
805 static HRESULT compile_exitdo_statement(compile_ctx_t *ctx)
807 if(ctx->while_end_label == -1) {
808 FIXME("Exit Do outside Do Loop\n");
809 return E_FAIL;
812 return push_instr_addr(ctx, OP_jmp, ctx->while_end_label);
815 static HRESULT compile_exitfor_statement(compile_ctx_t *ctx)
817 if(ctx->for_end_label == -1) {
818 FIXME("Exit For outside For Loop\n");
819 return E_FAIL;
822 return push_instr_addr(ctx, OP_jmp, ctx->for_end_label);
825 static HRESULT compile_exitsub_statement(compile_ctx_t *ctx)
827 if(ctx->sub_end_label == -1) {
828 FIXME("Exit Sub outside Sub?\n");
829 return E_FAIL;
832 return push_instr_addr(ctx, OP_jmp, ctx->sub_end_label);
835 static HRESULT compile_exitfunc_statement(compile_ctx_t *ctx)
837 if(ctx->func_end_label == -1) {
838 FIXME("Exit Function outside Function?\n");
839 return E_FAIL;
842 return push_instr_addr(ctx, OP_jmp, ctx->func_end_label);
845 static HRESULT compile_exitprop_statement(compile_ctx_t *ctx)
847 if(ctx->prop_end_label == -1) {
848 FIXME("Exit Property outside Property?\n");
849 return E_FAIL;
852 return push_instr_addr(ctx, OP_jmp, ctx->prop_end_label);
855 static HRESULT compile_onerror_statement(compile_ctx_t *ctx, onerror_statement_t *stat)
857 return push_instr_int(ctx, OP_errmode, stat->resume_next);
860 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
862 HRESULT hres;
864 while(stat) {
865 switch(stat->type) {
866 case STAT_ASSIGN:
867 hres = compile_assign_statement(ctx, (assign_statement_t*)stat, FALSE);
868 break;
869 case STAT_CALL:
870 hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
871 break;
872 case STAT_CONST:
873 hres = compile_const_statement(ctx, (const_statement_t*)stat);
874 break;
875 case STAT_DIM:
876 hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
877 break;
878 case STAT_DOWHILE:
879 case STAT_DOUNTIL:
880 hres = compile_dowhile_statement(ctx, (while_statement_t*)stat);
881 break;
882 case STAT_EXITDO:
883 hres = compile_exitdo_statement(ctx);
884 break;
885 case STAT_EXITFOR:
886 hres = compile_exitfor_statement(ctx);
887 break;
888 case STAT_EXITFUNC:
889 hres = compile_exitfunc_statement(ctx);
890 break;
891 case STAT_EXITPROP:
892 hres = compile_exitprop_statement(ctx);
893 break;
894 case STAT_EXITSUB:
895 hres = compile_exitsub_statement(ctx);
896 break;
897 case STAT_FORTO:
898 hres = compile_forto_statement(ctx, (forto_statement_t*)stat);
899 break;
900 case STAT_FUNC:
901 hres = compile_function_statement(ctx, (function_statement_t*)stat);
902 break;
903 case STAT_IF:
904 hres = compile_if_statement(ctx, (if_statement_t*)stat);
905 break;
906 case STAT_ONERROR:
907 hres = compile_onerror_statement(ctx, (onerror_statement_t*)stat);
908 break;
909 case STAT_SET:
910 hres = compile_assign_statement(ctx, (assign_statement_t*)stat, TRUE);
911 break;
912 case STAT_STOP:
913 hres = push_instr(ctx, OP_stop) == -1 ? E_OUTOFMEMORY : S_OK;
914 break;
915 case STAT_UNTIL:
916 case STAT_WHILE:
917 case STAT_WHILELOOP:
918 hres = compile_while_statement(ctx, (while_statement_t*)stat);
919 break;
920 default:
921 FIXME("Unimplemented statement type %d\n", stat->type);
922 hres = E_NOTIMPL;
925 if(FAILED(hres))
926 return hres;
927 stat = stat->next;
930 return S_OK;
933 static void resolve_labels(compile_ctx_t *ctx, unsigned off)
935 instr_t *instr;
937 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
938 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
939 assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
940 instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
942 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
945 ctx->labels_cnt = 0;
948 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
950 HRESULT hres;
952 func->code_off = ctx->instr_cnt;
954 ctx->while_end_label = -1;
955 ctx->for_end_label = -1;
956 ctx->sub_end_label = -1;
957 ctx->func_end_label = -1;
958 ctx->prop_end_label = -1;
960 switch(func->type) {
961 case FUNC_FUNCTION:
962 ctx->func_end_label = alloc_label(ctx);
963 if(ctx->func_end_label == -1)
964 return E_OUTOFMEMORY; /* FIXME ! */
965 break;
966 case FUNC_SUB:
967 ctx->sub_end_label = alloc_label(ctx);
968 if(ctx->sub_end_label == -1)
969 return E_OUTOFMEMORY;
970 break;
971 case FUNC_PROPGET:
972 case FUNC_PROPLET:
973 case FUNC_PROPSET:
974 case FUNC_DEFGET:
975 ctx->prop_end_label = alloc_label(ctx);
976 if(ctx->prop_end_label == -1)
977 return E_OUTOFMEMORY;
978 break;
979 case FUNC_GLOBAL:
980 break;
983 ctx->func = func;
984 ctx->dim_decls = NULL;
985 ctx->const_decls = NULL;
986 hres = compile_statement(ctx, stat);
987 ctx->func = NULL;
988 if(FAILED(hres))
989 return hres;
991 assert(ctx->while_end_label == -1);
992 assert(ctx->for_end_label == -1);
994 if(ctx->sub_end_label != -1)
995 label_set_addr(ctx, ctx->sub_end_label);
996 if(ctx->func_end_label != -1)
997 label_set_addr(ctx, ctx->func_end_label);
998 if(ctx->prop_end_label != -1)
999 label_set_addr(ctx, ctx->prop_end_label);
1001 if(push_instr(ctx, OP_ret) == -1)
1002 return E_OUTOFMEMORY;
1004 resolve_labels(ctx, func->code_off);
1006 if(func->var_cnt) {
1007 dim_decl_t *dim_decl;
1009 if(func->type == FUNC_GLOBAL) {
1010 dynamic_var_t *new_var;
1012 func->var_cnt = 0;
1014 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
1015 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
1016 if(!new_var)
1017 return E_OUTOFMEMORY;
1019 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
1020 if(!new_var->name)
1021 return E_OUTOFMEMORY;
1023 V_VT(&new_var->v) = VT_EMPTY;
1024 new_var->is_const = FALSE;
1026 new_var->next = ctx->global_vars;
1027 ctx->global_vars = new_var;
1029 }else {
1030 unsigned i;
1032 func->vars = compiler_alloc(ctx->code, func->var_cnt * sizeof(var_desc_t));
1033 if(!func->vars)
1034 return E_OUTOFMEMORY;
1036 for(dim_decl = ctx->dim_decls, i=0; dim_decl; dim_decl = dim_decl->next, i++) {
1037 func->vars[i].name = compiler_alloc_string(ctx->code, dim_decl->name);
1038 if(!func->vars[i].name)
1039 return E_OUTOFMEMORY;
1042 assert(i == func->var_cnt);
1046 return S_OK;
1049 static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
1051 function_t *iter;
1053 for(iter = ctx->funcs; iter; iter = iter->next) {
1054 if(!strcmpiW(iter->name, name))
1055 return TRUE;
1058 return FALSE;
1061 static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
1063 function_t *func;
1064 HRESULT hres;
1066 if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name) || lookup_const_decls(ctx, decl->name, FALSE)) {
1067 FIXME("%s: redefinition\n", debugstr_w(decl->name));
1068 return E_FAIL;
1071 func = compiler_alloc(ctx->code, sizeof(*func));
1072 if(!func)
1073 return E_OUTOFMEMORY;
1075 func->name = compiler_alloc_string(ctx->code, decl->name);
1076 if(!func->name)
1077 return E_OUTOFMEMORY;
1079 func->vars = NULL;
1080 func->var_cnt = 0;
1081 func->code_ctx = ctx->code;
1082 func->type = decl->type;
1083 func->is_public = decl->is_public;
1085 func->arg_cnt = 0;
1086 if(decl->args) {
1087 arg_decl_t *arg;
1088 unsigned i;
1090 for(arg = decl->args; arg; arg = arg->next)
1091 func->arg_cnt++;
1093 func->args = compiler_alloc(ctx->code, func->arg_cnt * sizeof(arg_desc_t));
1094 if(!func->args)
1095 return E_OUTOFMEMORY;
1097 for(i = 0, arg = decl->args; arg; arg = arg->next, i++) {
1098 func->args[i].name = compiler_alloc_string(ctx->code, arg->name);
1099 if(!func->args[i].name)
1100 return E_OUTOFMEMORY;
1101 func->args[i].by_ref = arg->by_ref;
1103 }else {
1104 func->args = NULL;
1107 hres = compile_func(ctx, decl->body, func);
1108 if(FAILED(hres))
1109 return hres;
1111 *ret = func;
1112 return S_OK;
1115 static BOOL lookup_class_name(compile_ctx_t *ctx, const WCHAR *name)
1117 class_desc_t *iter;
1119 for(iter = ctx->classes; iter; iter = iter->next) {
1120 if(!strcmpiW(iter->name, name))
1121 return TRUE;
1124 return FALSE;
1127 static HRESULT create_class_funcprop(compile_ctx_t *ctx, function_decl_t *func_decl, vbdisp_funcprop_desc_t *desc)
1129 vbdisp_invoke_type_t invoke_type;
1130 function_decl_t *funcprop_decl;
1131 HRESULT hres;
1133 desc->name = compiler_alloc_string(ctx->code, func_decl->name);
1134 if(!desc->name)
1135 return E_OUTOFMEMORY;
1137 for(funcprop_decl = func_decl; funcprop_decl; funcprop_decl = funcprop_decl->next_prop_func) {
1138 switch(funcprop_decl->type) {
1139 case FUNC_FUNCTION:
1140 case FUNC_SUB:
1141 case FUNC_PROPGET:
1142 case FUNC_DEFGET:
1143 invoke_type = VBDISP_CALLGET;
1144 break;
1145 case FUNC_PROPLET:
1146 invoke_type = VBDISP_LET;
1147 break;
1148 case FUNC_PROPSET:
1149 invoke_type = VBDISP_SET;
1150 break;
1151 default:
1152 assert(0);
1155 assert(!desc->entries[invoke_type]);
1157 if(funcprop_decl->is_public)
1158 desc->is_public = TRUE;
1160 hres = create_function(ctx, funcprop_decl, desc->entries+invoke_type);
1161 if(FAILED(hres))
1162 return hres;
1165 return S_OK;
1168 static BOOL lookup_class_funcs(class_desc_t *class_desc, const WCHAR *name)
1170 unsigned i;
1172 for(i=0; i < class_desc->func_cnt; i++) {
1173 if(class_desc->funcs[i].name && !strcmpiW(class_desc->funcs[i].name, name))
1174 return TRUE;
1177 return FALSE;
1180 static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl)
1182 function_decl_t *func_decl, *func_prop_decl;
1183 class_prop_decl_t *prop_decl;
1184 class_desc_t *class_desc;
1185 unsigned i;
1186 HRESULT hres;
1188 static const WCHAR class_initializeW[] = {'c','l','a','s','s','_','i','n','i','t','i','a','l','i','z','e',0};
1189 static const WCHAR class_terminateW[] = {'c','l','a','s','s','_','t','e','r','m','i','n','a','t','e',0};
1191 if(lookup_dim_decls(ctx, class_decl->name) || lookup_funcs_name(ctx, class_decl->name)
1192 || lookup_const_decls(ctx, class_decl->name, FALSE) || lookup_class_name(ctx, class_decl->name)) {
1193 FIXME("%s: redefinition\n", debugstr_w(class_decl->name));
1194 return E_FAIL;
1197 class_desc = compiler_alloc_zero(ctx->code, sizeof(*class_desc));
1198 if(!class_desc)
1199 return E_OUTOFMEMORY;
1201 class_desc->name = compiler_alloc_string(ctx->code, class_decl->name);
1202 if(!class_desc->name)
1203 return E_OUTOFMEMORY;
1205 class_desc->func_cnt = 1; /* always allocate slot for default getter */
1207 for(func_decl = class_decl->funcs; func_decl; func_decl = func_decl->next) {
1208 for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1209 if(func_prop_decl->type == FUNC_DEFGET)
1210 break;
1212 if(!func_prop_decl)
1213 class_desc->func_cnt++;
1216 class_desc->funcs = compiler_alloc(ctx->code, class_desc->func_cnt*sizeof(*class_desc->funcs));
1217 if(!class_desc->funcs)
1218 return E_OUTOFMEMORY;
1219 memset(class_desc->funcs, 0, class_desc->func_cnt*sizeof(*class_desc->funcs));
1221 for(func_decl = class_decl->funcs, i=1; func_decl; func_decl = func_decl->next, i++) {
1222 for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1223 if(func_prop_decl->type == FUNC_DEFGET) {
1224 i--;
1225 break;
1229 if(!strcmpiW(class_initializeW, func_decl->name)) {
1230 if(func_decl->type != FUNC_SUB) {
1231 FIXME("class initializer is not sub\n");
1232 return E_FAIL;
1235 class_desc->class_initialize_id = i;
1236 }else if(!strcmpiW(class_terminateW, func_decl->name)) {
1237 if(func_decl->type != FUNC_SUB) {
1238 FIXME("class terminator is not sub\n");
1239 return E_FAIL;
1242 class_desc->class_terminate_id = i;
1245 hres = create_class_funcprop(ctx, func_decl, class_desc->funcs + (func_prop_decl ? 0 : i));
1246 if(FAILED(hres))
1247 return hres;
1250 for(prop_decl = class_decl->props; prop_decl; prop_decl = prop_decl->next)
1251 class_desc->prop_cnt++;
1253 class_desc->props = compiler_alloc(ctx->code, class_desc->prop_cnt*sizeof(*class_desc->props));
1254 if(!class_desc->props)
1255 return E_OUTOFMEMORY;
1257 for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next, i++) {
1258 if(lookup_class_funcs(class_desc, prop_decl->name)) {
1259 FIXME("Property %s redefined\n", debugstr_w(prop_decl->name));
1260 return E_FAIL;
1263 class_desc->props[i].name = compiler_alloc_string(ctx->code, prop_decl->name);
1264 if(!class_desc->props[i].name)
1265 return E_OUTOFMEMORY;
1267 class_desc->props[i].is_public = prop_decl->is_public;
1270 class_desc->next = ctx->classes;
1271 ctx->classes = class_desc;
1272 return S_OK;
1275 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
1277 class_desc_t *class;
1278 dynamic_var_t *var;
1279 function_t *func;
1281 for(var = script->global_vars; var; var = var->next) {
1282 if(!strcmpiW(var->name, identifier))
1283 return TRUE;
1286 for(func = script->global_funcs; func; func = func->next) {
1287 if(!strcmpiW(func->name, identifier))
1288 return TRUE;
1291 for(class = script->classes; class; class = class->next) {
1292 if(!strcmpiW(class->name, identifier))
1293 return TRUE;
1296 return FALSE;
1299 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
1301 class_desc_t *class;
1302 dynamic_var_t *var;
1303 function_t *func;
1305 for(var = ctx->global_vars; var; var = var->next) {
1306 if(lookup_script_identifier(script, var->name)) {
1307 FIXME("%s: redefined\n", debugstr_w(var->name));
1308 return E_FAIL;
1312 for(func = ctx->funcs; func; func = func->next) {
1313 if(lookup_script_identifier(script, func->name)) {
1314 FIXME("%s: redefined\n", debugstr_w(func->name));
1315 return E_FAIL;
1319 for(class = ctx->classes; class; class = class->next) {
1320 if(lookup_script_identifier(script, class->name)) {
1321 FIXME("%s: redefined\n", debugstr_w(class->name));
1322 return E_FAIL;
1326 return S_OK;
1329 void release_vbscode(vbscode_t *code)
1331 unsigned i;
1333 list_remove(&code->entry);
1335 for(i=0; i < code->bstr_cnt; i++)
1336 SysFreeString(code->bstr_pool[i]);
1338 vbsheap_free(&code->heap);
1340 heap_free(code->bstr_pool);
1341 heap_free(code->source);
1342 heap_free(code->instrs);
1343 heap_free(code);
1346 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
1348 vbscode_t *ret;
1350 ret = heap_alloc(sizeof(*ret));
1351 if(!ret)
1352 return NULL;
1354 ret->source = heap_strdupW(source);
1355 if(!ret->source) {
1356 heap_free(ret);
1357 return NULL;
1360 ret->instrs = heap_alloc(32*sizeof(instr_t));
1361 if(!ret->instrs) {
1362 release_vbscode(ret);
1363 return NULL;
1366 ctx->instr_cnt = 0;
1367 ctx->instr_size = 32;
1368 vbsheap_init(&ret->heap);
1370 ret->option_explicit = ctx->parser.option_explicit;
1372 ret->bstr_pool = NULL;
1373 ret->bstr_pool_size = 0;
1374 ret->bstr_cnt = 0;
1375 ret->global_executed = FALSE;
1377 ret->global_code.type = FUNC_GLOBAL;
1378 ret->global_code.name = NULL;
1379 ret->global_code.code_ctx = ret;
1380 ret->global_code.vars = NULL;
1381 ret->global_code.var_cnt = 0;
1382 ret->global_code.arg_cnt = 0;
1383 ret->global_code.args = NULL;
1385 list_init(&ret->entry);
1386 return ret;
1389 static void release_compiler(compile_ctx_t *ctx)
1391 parser_release(&ctx->parser);
1392 heap_free(ctx->labels);
1393 if(ctx->code)
1394 release_vbscode(ctx->code);
1397 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
1399 function_t *new_func;
1400 function_decl_t *func_decl;
1401 class_decl_t *class_decl;
1402 compile_ctx_t ctx;
1403 vbscode_t *code;
1404 HRESULT hres;
1406 hres = parse_script(&ctx.parser, src);
1407 if(FAILED(hres))
1408 return hres;
1410 code = ctx.code = alloc_vbscode(&ctx, src);
1411 if(!ctx.code)
1412 return E_OUTOFMEMORY;
1414 ctx.funcs = NULL;
1415 ctx.func_decls = NULL;
1416 ctx.global_vars = NULL;
1417 ctx.dim_decls = NULL;
1418 ctx.classes = NULL;
1419 ctx.labels = NULL;
1420 ctx.global_consts = NULL;
1421 ctx.labels_cnt = ctx.labels_size = 0;
1423 hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
1424 if(FAILED(hres)) {
1425 release_compiler(&ctx);
1426 return hres;
1429 ctx.global_consts = ctx.const_decls;
1431 for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
1432 hres = create_function(&ctx, func_decl, &new_func);
1433 if(FAILED(hres)) {
1434 release_compiler(&ctx);
1435 return hres;
1438 new_func->next = ctx.funcs;
1439 ctx.funcs = new_func;
1442 for(class_decl = ctx.parser.class_decls; class_decl; class_decl = class_decl->next) {
1443 hres = compile_class(&ctx, class_decl);
1444 if(FAILED(hres)) {
1445 release_compiler(&ctx);
1446 return hres;
1450 hres = check_script_collisions(&ctx, script);
1451 if(FAILED(hres)) {
1452 release_compiler(&ctx);
1453 return hres;
1456 if(ctx.global_vars) {
1457 dynamic_var_t *var;
1459 for(var = ctx.global_vars; var->next; var = var->next);
1461 var->next = script->global_vars;
1462 script->global_vars = ctx.global_vars;
1465 if(ctx.funcs) {
1466 for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);
1468 new_func->next = script->global_funcs;
1469 script->global_funcs = ctx.funcs;
1472 if(ctx.classes) {
1473 class_desc_t *class = ctx.classes;
1475 while(1) {
1476 class->ctx = script;
1477 if(!class->next)
1478 break;
1479 class = class->next;
1482 class->next = script->classes;
1483 script->classes = ctx.classes;
1486 if(TRACE_ON(vbscript_disas))
1487 dump_code(&ctx);
1489 ctx.code = NULL;
1490 release_compiler(&ctx);
1492 list_add_tail(&script->code_list, &code->entry);
1493 *ret = code;
1494 return S_OK;