vbscript: Added compiler support for boolean literals.
[wine] / dlls / vbscript / compile.c
1 /*
2  * Copyright 2011 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <assert.h>
20
21 #include "vbscript.h"
22 #include "parse.h"
23 #include "parser.tab.h"
24
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
28
29 typedef struct {
30     parser_ctx_t parser;
31
32     unsigned instr_cnt;
33     unsigned instr_size;
34     vbscode_t *code;
35 } compile_ctx_t;
36
37 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
38
39 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
40 {
41     assert(id < ctx->instr_cnt);
42     return ctx->code->instrs + id;
43 }
44
45 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
46 {
47     assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
48
49     if(ctx->instr_size == ctx->instr_cnt) {
50         instr_t *new_instr;
51
52         new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
53         if(!new_instr)
54             return -1;
55
56         ctx->code->instrs = new_instr;
57         ctx->instr_size *= 2;
58     }
59
60     ctx->code->instrs[ctx->instr_cnt].op = op;
61     return ctx->instr_cnt++;
62 }
63
64 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
65 {
66     unsigned ret;
67
68     ret = push_instr(ctx, op);
69     if(ret == -1)
70         return E_OUTOFMEMORY;
71
72     instr_ptr(ctx, ret)->arg1.lng = arg;
73     return S_OK;
74 }
75
76 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
77 {
78     if(!ctx->code->bstr_pool_size) {
79         ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
80         if(!ctx->code->bstr_pool)
81             return NULL;
82         ctx->code->bstr_pool_size = 8;
83     }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
84        BSTR *new_pool;
85
86         new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
87         if(!new_pool)
88             return NULL;
89
90         ctx->code->bstr_pool = new_pool;
91         ctx->code->bstr_pool_size *= 2;
92     }
93
94     ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
95     if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
96         return NULL;
97
98     return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
99 }
100
101 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
102 {
103     unsigned instr;
104     BSTR bstr;
105
106     bstr = alloc_bstr_arg(ctx, arg1);
107     if(!bstr)
108         return E_OUTOFMEMORY;
109
110     instr = push_instr(ctx, op);
111     if(instr == -1)
112         return E_OUTOFMEMORY;
113
114     instr_ptr(ctx, instr)->arg1.bstr = bstr;
115     instr_ptr(ctx, instr)->arg2.uint = arg2;
116     return S_OK;
117 }
118
119 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
120 {
121     unsigned arg_cnt = 0;
122     HRESULT hres;
123
124     while(args) {
125         hres = compile_expression(ctx, args);
126         if(FAILED(hres))
127             return hres;
128
129         arg_cnt++;
130         args = args->next;
131     }
132
133     *ret = arg_cnt;
134     return S_OK;
135 }
136
137 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr)
138 {
139     unsigned arg_cnt = 0;
140     HRESULT hres;
141
142     hres = compile_args(ctx, expr->args, &arg_cnt);
143     if(FAILED(hres))
144         return hres;
145
146     if(expr->obj_expr) {
147         FIXME("obj_expr not implemented\n");
148         hres = E_NOTIMPL;
149     }else {
150         hres = push_instr_bstr_uint(ctx, OP_icallv, expr->identifier, arg_cnt);
151     }
152
153     return hres;
154 }
155
156 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
157 {
158     switch(expr->type) {
159     case EXPR_BOOL:
160         return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
161     default:
162         FIXME("Unimplemented expression type %d\n", expr->type);
163         return E_NOTIMPL;
164     }
165
166     return S_OK;
167 }
168
169 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
170 {
171     HRESULT hres;
172
173     while(stat) {
174         switch(stat->type) {
175         case STAT_CALL:
176             hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr);
177             break;
178         default:
179             FIXME("Unimplemented statement type %d\n", stat->type);
180             hres = E_NOTIMPL;
181         }
182
183         if(FAILED(hres))
184             return hres;
185         stat = stat->next;
186     }
187
188     return S_OK;
189 }
190
191 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
192 {
193     HRESULT hres;
194
195     func->code_off = ctx->instr_cnt;
196
197     hres = compile_statement(ctx, stat);
198     if(FAILED(hres))
199         return hres;
200
201     if(push_instr(ctx, OP_ret) == -1)
202         return E_OUTOFMEMORY;
203
204     return S_OK;
205 }
206
207 void release_vbscode(vbscode_t *code)
208 {
209     unsigned i;
210
211     list_remove(&code->entry);
212
213     for(i=0; i < code->bstr_cnt; i++)
214         SysFreeString(code->bstr_pool[i]);
215
216     heap_free(code->bstr_pool);
217     heap_free(code->source);
218     heap_free(code->instrs);
219     heap_free(code);
220 }
221
222 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
223 {
224     vbscode_t *ret;
225
226     ret = heap_alloc(sizeof(*ret));
227     if(!ret)
228         return NULL;
229
230     ret->source = heap_strdupW(source);
231     if(!ret->source) {
232         heap_free(ret);
233         return NULL;
234     }
235
236     ret->instrs = heap_alloc(32*sizeof(instr_t));
237     if(!ret->instrs) {
238         release_vbscode(ret);
239         return NULL;
240     }
241
242     ctx->instr_cnt = 0;
243     ctx->instr_size = 32;
244
245     ret->bstr_pool = NULL;
246     ret->bstr_pool_size = 0;
247     ret->bstr_cnt = 0;
248
249     ret->global_code.code_ctx = ret;
250
251     list_init(&ret->entry);
252     return ret;
253 }
254
255 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
256 {
257     compile_ctx_t ctx;
258     HRESULT hres;
259
260     hres = parse_script(&ctx.parser, src);
261     if(FAILED(hres))
262         return hres;
263
264     ctx.code = alloc_vbscode(&ctx, src);
265     if(!ctx.code)
266         return E_OUTOFMEMORY;
267
268     hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
269     if(FAILED(hres)) {
270         release_vbscode(ctx.code);
271         return hres;
272     }
273
274     list_add_tail(&script->code_list, &ctx.code->entry);
275     *ret = ctx.code;
276     return S_OK;
277 }