vbscript: Add explicit cast to int for pointer difference type.
[wine.git] / dlls / vbscript / compile.c
blob60e4935335a98eff831f9e6927bea08f305a7815
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 sub_end_label;
43 unsigned func_end_label;
44 unsigned prop_end_label;
46 dim_decl_t *dim_decls;
47 dynamic_var_t *global_vars;
49 function_t *func;
50 function_t *funcs;
51 function_decl_t *func_decls;
53 class_desc_t *classes;
54 } compile_ctx_t;
56 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
57 static HRESULT compile_statement(compile_ctx_t*,statement_t*);
59 static const struct {
60 const char *op_str;
61 instr_arg_type_t arg1_type;
62 instr_arg_type_t arg2_type;
63 } instr_info[] = {
64 #define X(n,a,b,c) {#n,b,c},
65 OP_LIST
66 #undef X
69 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
71 switch(type) {
72 case ARG_STR:
73 case ARG_BSTR:
74 TRACE_(vbscript_disas)("\t%s", debugstr_w(arg->str));
75 break;
76 case ARG_INT:
77 TRACE_(vbscript_disas)("\t%d", arg->uint);
78 break;
79 case ARG_UINT:
80 case ARG_ADDR:
81 TRACE_(vbscript_disas)("\t%u", arg->uint);
82 break;
83 case ARG_DOUBLE:
84 TRACE_(vbscript_disas)("\t%lf", *arg->dbl);
85 break;
86 case ARG_NONE:
87 break;
88 default:
89 assert(0);
93 static void dump_code(compile_ctx_t *ctx)
95 instr_t *instr;
97 for(instr = ctx->code->instrs; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
98 TRACE_(vbscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
99 dump_instr_arg(instr_info[instr->op].arg1_type, &instr->arg1);
100 dump_instr_arg(instr_info[instr->op].arg2_type, &instr->arg2);
101 TRACE_(vbscript_disas)("\n");
105 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
107 return vbsheap_alloc(&vbscode->heap, size);
110 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
112 size_t size;
113 WCHAR *ret;
115 size = (strlenW(str)+1)*sizeof(WCHAR);
116 ret = compiler_alloc(vbscode, size);
117 if(ret)
118 memcpy(ret, str, size);
119 return ret;
122 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
124 assert(id < ctx->instr_cnt);
125 return ctx->code->instrs + id;
128 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
130 assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
132 if(ctx->instr_size == ctx->instr_cnt) {
133 instr_t *new_instr;
135 new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
136 if(!new_instr)
137 return -1;
139 ctx->code->instrs = new_instr;
140 ctx->instr_size *= 2;
143 ctx->code->instrs[ctx->instr_cnt].op = op;
144 return ctx->instr_cnt++;
147 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
149 unsigned ret;
151 ret = push_instr(ctx, op);
152 if(ret == -1)
153 return E_OUTOFMEMORY;
155 instr_ptr(ctx, ret)->arg1.lng = arg;
156 return S_OK;
159 static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
161 unsigned ret;
163 ret = push_instr(ctx, op);
164 if(ret == -1)
165 return E_OUTOFMEMORY;
167 instr_ptr(ctx, ret)->arg1.uint = arg;
168 return S_OK;
171 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
173 unsigned instr;
174 WCHAR *str;
176 str = compiler_alloc_string(ctx->code, arg);
177 if(!str)
178 return E_OUTOFMEMORY;
180 instr = push_instr(ctx, op);
181 if(instr == -1)
182 return E_OUTOFMEMORY;
184 instr_ptr(ctx, instr)->arg1.str = str;
185 return S_OK;
188 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
190 unsigned instr;
191 double *d;
193 d = compiler_alloc(ctx->code, sizeof(double));
194 if(!d)
195 return E_OUTOFMEMORY;
197 instr = push_instr(ctx, op);
198 if(instr == -1)
199 return E_OUTOFMEMORY;
201 *d = arg;
202 instr_ptr(ctx, instr)->arg1.dbl = d;
203 return S_OK;
206 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
208 if(!ctx->code->bstr_pool_size) {
209 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
210 if(!ctx->code->bstr_pool)
211 return NULL;
212 ctx->code->bstr_pool_size = 8;
213 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
214 BSTR *new_pool;
216 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
217 if(!new_pool)
218 return NULL;
220 ctx->code->bstr_pool = new_pool;
221 ctx->code->bstr_pool_size *= 2;
224 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
225 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
226 return NULL;
228 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
231 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
233 unsigned instr;
234 BSTR bstr;
236 bstr = alloc_bstr_arg(ctx, arg);
237 if(!bstr)
238 return E_OUTOFMEMORY;
240 instr = push_instr(ctx, op);
241 if(instr == -1)
242 return E_OUTOFMEMORY;
244 instr_ptr(ctx, instr)->arg1.bstr = bstr;
245 return S_OK;
248 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
250 unsigned instr;
251 BSTR bstr;
253 bstr = alloc_bstr_arg(ctx, arg1);
254 if(!bstr)
255 return E_OUTOFMEMORY;
257 instr = push_instr(ctx, op);
258 if(instr == -1)
259 return E_OUTOFMEMORY;
261 instr_ptr(ctx, instr)->arg1.bstr = bstr;
262 instr_ptr(ctx, instr)->arg2.uint = arg2;
263 return S_OK;
266 #define LABEL_FLAG 0x80000000
268 static unsigned alloc_label(compile_ctx_t *ctx)
270 if(!ctx->labels_size) {
271 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
272 if(!ctx->labels)
273 return -1;
274 ctx->labels_size = 8;
275 }else if(ctx->labels_size == ctx->labels_cnt) {
276 unsigned *new_labels;
278 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
279 if(!new_labels)
280 return -1;
282 ctx->labels = new_labels;
283 ctx->labels_size *= 2;
286 return ctx->labels_cnt++ | LABEL_FLAG;
289 static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
291 assert(label & LABEL_FLAG);
292 ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
295 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
297 unsigned arg_cnt = 0;
298 HRESULT hres;
300 while(args) {
301 hres = compile_expression(ctx, args);
302 if(FAILED(hres))
303 return hres;
305 arg_cnt++;
306 args = args->next;
309 *ret = arg_cnt;
310 return S_OK;
313 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
315 unsigned arg_cnt = 0;
316 HRESULT hres;
318 hres = compile_args(ctx, expr->args, &arg_cnt);
319 if(FAILED(hres))
320 return hres;
322 if(expr->obj_expr) {
323 hres = compile_expression(ctx, expr->obj_expr);
324 if(FAILED(hres))
325 return hres;
327 hres = push_instr_bstr_uint(ctx, ret_val ? OP_mcall : OP_mcallv, expr->identifier, arg_cnt);
328 }else {
329 hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
332 return hres;
335 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
337 HRESULT hres;
339 hres = compile_expression(ctx, expr->subexpr);
340 if(FAILED(hres))
341 return hres;
343 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
346 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
348 HRESULT hres;
350 hres = compile_expression(ctx, expr->left);
351 if(FAILED(hres))
352 return hres;
354 hres = compile_expression(ctx, expr->right);
355 if(FAILED(hres))
356 return hres;
358 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
361 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
363 switch(expr->type) {
364 case EXPR_ADD:
365 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
366 case EXPR_AND:
367 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
368 case EXPR_BOOL:
369 return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
370 case EXPR_CONCAT:
371 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
372 case EXPR_DIV:
373 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
374 case EXPR_DOUBLE:
375 return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
376 case EXPR_EMPTY:
377 return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
378 case EXPR_EQUAL:
379 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
380 case EXPR_EQV:
381 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eqv);
382 case EXPR_EXP:
383 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
384 case EXPR_IDIV:
385 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
386 case EXPR_IMP:
387 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_imp);
388 case EXPR_MEMBER:
389 return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
390 case EXPR_MOD:
391 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
392 case EXPR_MUL:
393 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
394 case EXPR_NEG:
395 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
396 case EXPR_NEQUAL:
397 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
398 case EXPR_NEW:
399 return push_instr_str(ctx, OP_new, ((string_expression_t*)expr)->value);
400 case EXPR_NOT:
401 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
402 case EXPR_NOTHING:
403 return push_instr(ctx, OP_nothing) != -1 ? S_OK : E_OUTOFMEMORY;
404 case EXPR_NULL:
405 return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
406 case EXPR_OR:
407 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
408 case EXPR_STRING:
409 return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
410 case EXPR_SUB:
411 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
412 case EXPR_USHORT:
413 return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
414 case EXPR_ULONG:
415 return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
416 case EXPR_XOR:
417 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
418 default:
419 FIXME("Unimplemented expression type %d\n", expr->type);
420 return E_NOTIMPL;
423 return S_OK;
426 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
428 unsigned cnd_jmp, endif_label = -1;
429 elseif_decl_t *elseif_decl;
430 HRESULT hres;
432 hres = compile_expression(ctx, stat->expr);
433 if(FAILED(hres))
434 return hres;
436 cnd_jmp = push_instr(ctx, OP_jmp_false);
437 if(cnd_jmp == -1)
438 return E_OUTOFMEMORY;
440 hres = compile_statement(ctx, stat->if_stat);
441 if(FAILED(hres))
442 return hres;
444 if(stat->else_stat || stat->elseifs) {
445 endif_label = alloc_label(ctx);
446 if(endif_label == -1)
447 return E_OUTOFMEMORY;
449 hres = push_instr_addr(ctx, OP_jmp, endif_label);
450 if(FAILED(hres))
451 return hres;
454 for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
455 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
457 hres = compile_expression(ctx, elseif_decl->expr);
458 if(FAILED(hres))
459 return hres;
461 cnd_jmp = push_instr(ctx, OP_jmp_false);
462 if(cnd_jmp == -1)
463 return E_OUTOFMEMORY;
465 hres = compile_statement(ctx, elseif_decl->stat);
466 if(FAILED(hres))
467 return hres;
469 hres = push_instr_addr(ctx, OP_jmp, endif_label);
470 if(FAILED(hres))
471 return hres;
474 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
476 if(stat->else_stat) {
477 hres = compile_statement(ctx, stat->else_stat);
478 if(FAILED(hres))
479 return hres;
482 if(endif_label != -1)
483 label_set_addr(ctx, endif_label);
484 return S_OK;
487 static HRESULT compile_while_statement(compile_ctx_t *ctx, while_statement_t *stat)
489 unsigned start_addr, prev_label;
490 unsigned jmp_end;
491 HRESULT hres;
493 start_addr = ctx->instr_cnt;
495 hres = compile_expression(ctx, stat->expr);
496 if(FAILED(hres))
497 return hres;
499 jmp_end = push_instr(ctx, stat->stat.type == STAT_UNTIL ? OP_jmp_true : OP_jmp_false);
500 if(jmp_end == -1)
501 return E_OUTOFMEMORY;
503 if(stat->stat.type != STAT_WHILE) {
504 prev_label = ctx->while_end_label;
505 if((ctx->while_end_label = alloc_label(ctx)) == -1)
506 return E_OUTOFMEMORY;
509 hres = compile_statement(ctx, stat->body);
510 if(FAILED(hres))
511 return hres;
513 hres = push_instr_addr(ctx, OP_jmp, start_addr);
514 if(FAILED(hres))
515 return hres;
517 instr_ptr(ctx, jmp_end)->arg1.uint = ctx->instr_cnt;
519 if(stat->stat.type != STAT_WHILE) {
520 label_set_addr(ctx, ctx->while_end_label);
521 ctx->while_end_label = prev_label;
524 return S_OK;
527 static HRESULT compile_dowhile_statement(compile_ctx_t *ctx, while_statement_t *stat)
529 unsigned start_addr, prev_label;
530 HRESULT hres;
532 start_addr = ctx->instr_cnt;
534 prev_label = ctx->while_end_label;
535 if((ctx->while_end_label = alloc_label(ctx)) == -1)
536 return E_OUTOFMEMORY;
538 hres = compile_statement(ctx, stat->body);
539 if(FAILED(hres))
540 return hres;
542 hres = compile_expression(ctx, stat->expr);
543 if(FAILED(hres))
544 return hres;
546 hres = push_instr_addr(ctx, stat->stat.type == STAT_DOUNTIL ? OP_jmp_false : OP_jmp_true, start_addr);
547 if(FAILED(hres))
548 return hres;
550 label_set_addr(ctx, ctx->while_end_label);
551 ctx->while_end_label = prev_label;
552 return S_OK;
555 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat, BOOL is_set)
557 HRESULT hres;
559 hres = compile_expression(ctx, stat->value_expr);
560 if(FAILED(hres))
561 return hres;
563 if(stat->member_expr->args) {
564 FIXME("arguments support not implemented\n");
565 return E_NOTIMPL;
568 if(stat->member_expr->obj_expr) {
569 hres = compile_expression(ctx, stat->member_expr->obj_expr);
570 if(FAILED(hres))
571 return hres;
573 hres = push_instr_bstr(ctx, is_set ? OP_set_member : OP_assign_member, stat->member_expr->identifier);
574 }else {
575 hres = push_instr_bstr(ctx, is_set ? OP_set_ident : OP_assign_ident, stat->member_expr->identifier);
578 return hres;
581 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
583 dim_decl_t *dim_decl;
585 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
586 if(!strcmpiW(dim_decl->name, name))
587 return TRUE;
590 return FALSE;
593 static BOOL lookup_args_name(compile_ctx_t *ctx, const WCHAR *name)
595 unsigned i;
597 for(i = 0; i < ctx->func->arg_cnt; i++) {
598 if(!strcmpiW(ctx->func->args[i].name, name))
599 return TRUE;
602 return FALSE;
605 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
607 dim_decl_t *dim_decl = stat->dim_decls;
609 while(1) {
610 if(lookup_dim_decls(ctx, dim_decl->name) || lookup_args_name(ctx, dim_decl->name)) {
611 FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
612 return E_FAIL;
615 if(!dim_decl->next)
616 break;
617 dim_decl = dim_decl->next;
620 dim_decl->next = ctx->dim_decls;
621 ctx->dim_decls = stat->dim_decls;
622 ctx->func->var_cnt++;
623 return S_OK;
626 static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
628 if(ctx->func != &ctx->code->global_code) {
629 FIXME("Function is not in the global code\n");
630 return E_FAIL;
633 stat->func_decl->next = ctx->func_decls;
634 ctx->func_decls = stat->func_decl;
635 return S_OK;
638 static HRESULT compile_exitdo_statement(compile_ctx_t *ctx)
640 if(ctx->while_end_label == -1) {
641 FIXME("Exit Do outside Do Loop\n");
642 return E_FAIL;
645 return push_instr_addr(ctx, OP_jmp, ctx->while_end_label);
648 static HRESULT compile_exitsub_statement(compile_ctx_t *ctx)
650 if(ctx->sub_end_label == -1) {
651 FIXME("Exit Sub outside Sub?\n");
652 return E_FAIL;
655 return push_instr_addr(ctx, OP_jmp, ctx->sub_end_label);
658 static HRESULT compile_exitfunc_statement(compile_ctx_t *ctx)
660 if(ctx->func_end_label == -1) {
661 FIXME("Exit Function outside Function?\n");
662 return E_FAIL;
665 return push_instr_addr(ctx, OP_jmp, ctx->func_end_label);
668 static HRESULT compile_exitprop_statement(compile_ctx_t *ctx)
670 if(ctx->prop_end_label == -1) {
671 FIXME("Exit Property outside Property?\n");
672 return E_FAIL;
675 return push_instr_addr(ctx, OP_jmp, ctx->prop_end_label);
678 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
680 HRESULT hres;
682 while(stat) {
683 switch(stat->type) {
684 case STAT_ASSIGN:
685 hres = compile_assign_statement(ctx, (assign_statement_t*)stat, FALSE);
686 break;
687 case STAT_CALL:
688 hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
689 break;
690 case STAT_DIM:
691 hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
692 break;
693 case STAT_DOWHILE:
694 case STAT_DOUNTIL:
695 hres = compile_dowhile_statement(ctx, (while_statement_t*)stat);
696 break;
697 case STAT_EXITDO:
698 hres = compile_exitdo_statement(ctx);
699 break;
700 case STAT_EXITFUNC:
701 hres = compile_exitfunc_statement(ctx);
702 break;
703 case STAT_EXITPROP:
704 hres = compile_exitprop_statement(ctx);
705 break;
706 case STAT_EXITSUB:
707 hres = compile_exitsub_statement(ctx);
708 break;
709 case STAT_FUNC:
710 hres = compile_function_statement(ctx, (function_statement_t*)stat);
711 break;
712 case STAT_IF:
713 hres = compile_if_statement(ctx, (if_statement_t*)stat);
714 break;
715 case STAT_SET:
716 hres = compile_assign_statement(ctx, (assign_statement_t*)stat, TRUE);
717 break;
718 case STAT_STOP:
719 hres = push_instr(ctx, OP_stop) == -1 ? E_OUTOFMEMORY : S_OK;
720 break;
721 case STAT_UNTIL:
722 case STAT_WHILE:
723 case STAT_WHILELOOP:
724 hres = compile_while_statement(ctx, (while_statement_t*)stat);
725 break;
726 default:
727 FIXME("Unimplemented statement type %d\n", stat->type);
728 hres = E_NOTIMPL;
731 if(FAILED(hres))
732 return hres;
733 stat = stat->next;
736 return S_OK;
739 static void resolve_labels(compile_ctx_t *ctx, unsigned off)
741 instr_t *instr;
743 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
744 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
745 assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
746 instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
748 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
751 ctx->labels_cnt = 0;
754 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
756 HRESULT hres;
758 func->code_off = ctx->instr_cnt;
760 ctx->while_end_label = -1;
761 ctx->sub_end_label = -1;
762 ctx->func_end_label = -1;
763 ctx->prop_end_label = -1;
765 switch(func->type) {
766 case FUNC_FUNCTION:
767 ctx->func_end_label = alloc_label(ctx);
768 if(ctx->func_end_label == -1)
769 return E_OUTOFMEMORY; /* FIXME ! */
770 break;
771 case FUNC_SUB:
772 ctx->sub_end_label = alloc_label(ctx);
773 if(ctx->sub_end_label == -1)
774 return E_OUTOFMEMORY;
775 break;
776 case FUNC_PROPGET:
777 case FUNC_PROPLET:
778 case FUNC_PROPSET:
779 case FUNC_DEFGET:
780 ctx->prop_end_label = alloc_label(ctx);
781 if(ctx->prop_end_label == -1)
782 return E_OUTOFMEMORY;
783 break;
784 case FUNC_GLOBAL:
785 break;
788 ctx->func = func;
789 ctx->dim_decls = NULL;
790 hres = compile_statement(ctx, stat);
791 ctx->func = NULL;
792 if(FAILED(hres))
793 return hres;
795 assert(ctx->while_end_label == -1);
797 if(ctx->sub_end_label != -1)
798 label_set_addr(ctx, ctx->sub_end_label);
799 if(ctx->func_end_label != -1)
800 label_set_addr(ctx, ctx->func_end_label);
801 if(ctx->prop_end_label != -1)
802 label_set_addr(ctx, ctx->prop_end_label);
804 if(push_instr(ctx, OP_ret) == -1)
805 return E_OUTOFMEMORY;
807 resolve_labels(ctx, func->code_off);
809 if(func->var_cnt) {
810 dim_decl_t *dim_decl;
812 if(func->type == FUNC_GLOBAL) {
813 dynamic_var_t *new_var;
815 func->var_cnt = 0;
817 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
818 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
819 if(!new_var)
820 return E_OUTOFMEMORY;
822 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
823 if(!new_var->name)
824 return E_OUTOFMEMORY;
826 V_VT(&new_var->v) = VT_EMPTY;
828 new_var->next = ctx->global_vars;
829 ctx->global_vars = new_var;
831 }else {
832 unsigned i;
834 func->vars = compiler_alloc(ctx->code, func->var_cnt * sizeof(var_desc_t));
835 if(!func->vars)
836 return E_OUTOFMEMORY;
838 for(dim_decl = ctx->dim_decls, i=0; dim_decl; dim_decl = dim_decl->next, i++) {
839 func->vars[i].name = compiler_alloc_string(ctx->code, dim_decl->name);
840 if(!func->vars[i].name)
841 return E_OUTOFMEMORY;
844 assert(i == func->var_cnt);
848 return S_OK;
851 static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
853 function_t *iter;
855 for(iter = ctx->funcs; iter; iter = iter->next) {
856 if(!strcmpiW(iter->name, name))
857 return TRUE;
860 return FALSE;
863 static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
865 function_t *func;
866 HRESULT hres;
868 if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name)) {
869 FIXME("%s: redefinition\n", debugstr_w(decl->name));
870 return E_FAIL;
873 func = compiler_alloc(ctx->code, sizeof(*func));
874 if(!func)
875 return E_OUTOFMEMORY;
877 func->name = compiler_alloc_string(ctx->code, decl->name);
878 if(!func->name)
879 return E_OUTOFMEMORY;
881 func->vars = NULL;
882 func->var_cnt = 0;
883 func->code_ctx = ctx->code;
884 func->type = decl->type;
885 func->is_public = decl->is_public;
887 func->arg_cnt = 0;
888 if(decl->args) {
889 arg_decl_t *arg;
890 unsigned i;
892 for(arg = decl->args; arg; arg = arg->next)
893 func->arg_cnt++;
895 func->args = compiler_alloc(ctx->code, func->arg_cnt * sizeof(arg_desc_t));
896 if(!func->args)
897 return E_OUTOFMEMORY;
899 for(i = 0, arg = decl->args; arg; arg = arg->next, i++) {
900 func->args[i].name = compiler_alloc_string(ctx->code, arg->name);
901 if(!func->args[i].name)
902 return E_OUTOFMEMORY;
903 func->args[i].by_ref = arg->by_ref;
905 }else {
906 func->args = NULL;
909 hres = compile_func(ctx, decl->body, func);
910 if(FAILED(hres))
911 return hres;
913 *ret = func;
914 return S_OK;
917 static BOOL lookup_class_name(compile_ctx_t *ctx, const WCHAR *name)
919 class_desc_t *iter;
921 for(iter = ctx->classes; iter; iter = iter->next) {
922 if(!strcmpiW(iter->name, name))
923 return TRUE;
926 return FALSE;
929 static HRESULT create_class_funcprop(compile_ctx_t *ctx, function_decl_t *func_decl, vbdisp_funcprop_desc_t *desc)
931 vbdisp_invoke_type_t invoke_type;
932 function_decl_t *funcprop_decl;
933 HRESULT hres;
935 desc->name = compiler_alloc_string(ctx->code, func_decl->name);
936 if(!desc->name)
937 return E_OUTOFMEMORY;
939 for(funcprop_decl = func_decl; funcprop_decl; funcprop_decl = funcprop_decl->next_prop_func) {
940 switch(funcprop_decl->type) {
941 case FUNC_FUNCTION:
942 case FUNC_SUB:
943 case FUNC_PROPGET:
944 case FUNC_DEFGET:
945 invoke_type = VBDISP_CALLGET;
946 break;
947 case FUNC_PROPLET:
948 invoke_type = VBDISP_LET;
949 break;
950 case FUNC_PROPSET:
951 invoke_type = VBDISP_SET;
952 break;
953 default:
954 assert(0);
957 assert(!desc->entries[invoke_type]);
959 if(funcprop_decl->is_public)
960 desc->is_public = TRUE;
962 hres = create_function(ctx, funcprop_decl, desc->entries+invoke_type);
963 if(FAILED(hres))
964 return hres;
967 return S_OK;
970 static BOOL lookup_class_funcs(class_desc_t *class_desc, const WCHAR *name)
972 unsigned i;
974 for(i=0; i < class_desc->func_cnt; i++) {
975 if(class_desc->funcs[i].name && !strcmpiW(class_desc->funcs[i].name, name))
976 return TRUE;
979 return FALSE;
982 static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl)
984 function_decl_t *func_decl, *func_prop_decl;
985 class_prop_decl_t *prop_decl;
986 class_desc_t *class_desc;
987 unsigned i;
988 HRESULT hres;
990 static const WCHAR class_initializeW[] = {'c','l','a','s','s','_','i','n','i','t','i','a','l','i','z','e',0};
991 static const WCHAR class_terminateW[] = {'c','l','a','s','s','_','t','e','r','m','i','n','a','t','e',0};
993 if(lookup_dim_decls(ctx, class_decl->name) || lookup_funcs_name(ctx, class_decl->name)
994 || lookup_class_name(ctx, class_decl->name)) {
995 FIXME("%s: redefinition\n", debugstr_w(class_decl->name));
996 return E_FAIL;
999 class_desc = compiler_alloc(ctx->code, sizeof(*class_desc));
1000 if(!class_desc)
1001 return E_OUTOFMEMORY;
1003 class_desc->name = compiler_alloc_string(ctx->code, class_decl->name);
1004 if(!class_desc->name)
1005 return E_OUTOFMEMORY;
1007 class_desc->func_cnt = 1; /* always allocate slot for default getter */
1008 class_desc->prop_cnt = 0;
1009 class_desc->class_initialize_id = 0;
1010 class_desc->class_terminate_id = 0;
1012 for(func_decl = class_decl->funcs; func_decl; func_decl = func_decl->next) {
1013 for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1014 if(func_prop_decl->type == FUNC_DEFGET)
1015 break;
1017 if(!func_prop_decl)
1018 class_desc->func_cnt++;
1021 class_desc->funcs = compiler_alloc(ctx->code, class_desc->func_cnt*sizeof(*class_desc->funcs));
1022 if(!class_desc->funcs)
1023 return E_OUTOFMEMORY;
1024 memset(class_desc->funcs, 0, class_desc->func_cnt*sizeof(*class_desc->funcs));
1026 for(func_decl = class_decl->funcs, i=1; func_decl; func_decl = func_decl->next, i++) {
1027 for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1028 if(func_prop_decl->type == FUNC_DEFGET) {
1029 i--;
1030 break;
1034 if(!strcmpiW(class_initializeW, func_decl->name)) {
1035 if(func_decl->type != FUNC_SUB) {
1036 FIXME("class initializer is not sub\n");
1037 return E_FAIL;
1040 class_desc->class_initialize_id = i;
1041 }else if(!strcmpiW(class_terminateW, func_decl->name)) {
1042 if(func_decl->type != FUNC_SUB) {
1043 FIXME("class terminator is not sub\n");
1044 return E_FAIL;
1047 class_desc->class_terminate_id = i;
1050 hres = create_class_funcprop(ctx, func_decl, class_desc->funcs + (func_prop_decl ? 0 : i));
1051 if(FAILED(hres))
1052 return hres;
1055 for(prop_decl = class_decl->props; prop_decl; prop_decl = prop_decl->next)
1056 class_desc->prop_cnt++;
1058 class_desc->props = compiler_alloc(ctx->code, class_desc->prop_cnt*sizeof(*class_desc->props));
1059 if(!class_desc->props)
1060 return E_OUTOFMEMORY;
1062 for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next, i++) {
1063 if(lookup_class_funcs(class_desc, prop_decl->name)) {
1064 FIXME("Property %s redefined\n", debugstr_w(prop_decl->name));
1065 return E_FAIL;
1068 class_desc->props[i].name = compiler_alloc_string(ctx->code, prop_decl->name);
1069 if(!class_desc->props[i].name)
1070 return E_OUTOFMEMORY;
1072 class_desc->props[i].is_public = prop_decl->is_public;
1075 class_desc->next = ctx->classes;
1076 ctx->classes = class_desc;
1077 return S_OK;
1080 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
1082 class_desc_t *class;
1083 dynamic_var_t *var;
1084 function_t *func;
1086 for(var = script->global_vars; var; var = var->next) {
1087 if(!strcmpiW(var->name, identifier))
1088 return TRUE;
1091 for(func = script->global_funcs; func; func = func->next) {
1092 if(!strcmpiW(func->name, identifier))
1093 return TRUE;
1096 for(class = script->classes; class; class = class->next) {
1097 if(!strcmpiW(class->name, identifier))
1098 return TRUE;
1101 return FALSE;
1104 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
1106 class_desc_t *class;
1107 dynamic_var_t *var;
1108 function_t *func;
1110 for(var = ctx->global_vars; var; var = var->next) {
1111 if(lookup_script_identifier(script, var->name)) {
1112 FIXME("%s: redefined\n", debugstr_w(var->name));
1113 return E_FAIL;
1117 for(func = ctx->funcs; func; func = func->next) {
1118 if(lookup_script_identifier(script, func->name)) {
1119 FIXME("%s: redefined\n", debugstr_w(func->name));
1120 return E_FAIL;
1124 for(class = ctx->classes; class; class = class->next) {
1125 if(lookup_script_identifier(script, class->name)) {
1126 FIXME("%s: redefined\n", debugstr_w(class->name));
1127 return E_FAIL;
1131 return S_OK;
1134 void release_vbscode(vbscode_t *code)
1136 unsigned i;
1138 list_remove(&code->entry);
1140 for(i=0; i < code->bstr_cnt; i++)
1141 SysFreeString(code->bstr_pool[i]);
1143 vbsheap_free(&code->heap);
1145 heap_free(code->bstr_pool);
1146 heap_free(code->source);
1147 heap_free(code->instrs);
1148 heap_free(code);
1151 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
1153 vbscode_t *ret;
1155 ret = heap_alloc(sizeof(*ret));
1156 if(!ret)
1157 return NULL;
1159 ret->source = heap_strdupW(source);
1160 if(!ret->source) {
1161 heap_free(ret);
1162 return NULL;
1165 ret->instrs = heap_alloc(32*sizeof(instr_t));
1166 if(!ret->instrs) {
1167 release_vbscode(ret);
1168 return NULL;
1171 ctx->instr_cnt = 0;
1172 ctx->instr_size = 32;
1173 vbsheap_init(&ret->heap);
1175 ret->option_explicit = ctx->parser.option_explicit;
1177 ret->bstr_pool = NULL;
1178 ret->bstr_pool_size = 0;
1179 ret->bstr_cnt = 0;
1180 ret->global_executed = FALSE;
1182 ret->global_code.type = FUNC_GLOBAL;
1183 ret->global_code.name = NULL;
1184 ret->global_code.code_ctx = ret;
1185 ret->global_code.vars = NULL;
1186 ret->global_code.var_cnt = 0;
1187 ret->global_code.arg_cnt = 0;
1188 ret->global_code.args = NULL;
1190 list_init(&ret->entry);
1191 return ret;
1194 static void release_compiler(compile_ctx_t *ctx)
1196 parser_release(&ctx->parser);
1197 heap_free(ctx->labels);
1198 if(ctx->code)
1199 release_vbscode(ctx->code);
1202 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
1204 function_t *new_func;
1205 function_decl_t *func_decl;
1206 class_decl_t *class_decl;
1207 compile_ctx_t ctx;
1208 vbscode_t *code;
1209 HRESULT hres;
1211 hres = parse_script(&ctx.parser, src);
1212 if(FAILED(hres))
1213 return hres;
1215 code = ctx.code = alloc_vbscode(&ctx, src);
1216 if(!ctx.code)
1217 return E_OUTOFMEMORY;
1219 ctx.funcs = NULL;
1220 ctx.func_decls = NULL;
1221 ctx.global_vars = NULL;
1222 ctx.dim_decls = NULL;
1223 ctx.classes = NULL;
1224 ctx.labels = NULL;
1225 ctx.labels_cnt = ctx.labels_size = 0;
1227 hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
1228 if(FAILED(hres)) {
1229 release_compiler(&ctx);
1230 return hres;
1233 for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
1234 hres = create_function(&ctx, func_decl, &new_func);
1235 if(FAILED(hres)) {
1236 release_compiler(&ctx);
1237 return hres;
1240 new_func->next = ctx.funcs;
1241 ctx.funcs = new_func;
1244 for(class_decl = ctx.parser.class_decls; class_decl; class_decl = class_decl->next) {
1245 hres = compile_class(&ctx, class_decl);
1246 if(FAILED(hres)) {
1247 release_compiler(&ctx);
1248 return hres;
1252 hres = check_script_collisions(&ctx, script);
1253 if(FAILED(hres)) {
1254 release_compiler(&ctx);
1255 return hres;
1258 if(ctx.global_vars) {
1259 dynamic_var_t *var;
1261 for(var = ctx.global_vars; var->next; var = var->next);
1263 var->next = script->global_vars;
1264 script->global_vars = ctx.global_vars;
1267 if(ctx.funcs) {
1268 for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);
1270 new_func->next = script->global_funcs;
1271 script->global_funcs = ctx.funcs;
1274 if(ctx.classes) {
1275 class_desc_t *class = ctx.classes;
1277 while(1) {
1278 class->ctx = script;
1279 if(!class->next)
1280 break;
1281 class = class->next;
1284 class->next = script->classes;
1285 script->classes = ctx.classes;
1288 if(TRACE_ON(vbscript_disas))
1289 dump_code(&ctx);
1291 ctx.code = NULL;
1292 release_compiler(&ctx);
1294 list_add_tail(&script->code_list, &code->entry);
1295 *ret = code;
1296 return S_OK;