vbscript: Added assign statement compiler implementation.
[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 void *compiler_alloc(vbscode_t *vbscode, size_t size)
40 {
41     return vbsheap_alloc(&vbscode->heap, size);
42 }
43
44 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
45 {
46     size_t size;
47     WCHAR *ret;
48
49     size = (strlenW(str)+1)*sizeof(WCHAR);
50     ret = compiler_alloc(vbscode, size);
51     if(ret)
52         memcpy(ret, str, size);
53     return ret;
54 }
55
56 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
57 {
58     assert(id < ctx->instr_cnt);
59     return ctx->code->instrs + id;
60 }
61
62 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
63 {
64     assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
65
66     if(ctx->instr_size == ctx->instr_cnt) {
67         instr_t *new_instr;
68
69         new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
70         if(!new_instr)
71             return -1;
72
73         ctx->code->instrs = new_instr;
74         ctx->instr_size *= 2;
75     }
76
77     ctx->code->instrs[ctx->instr_cnt].op = op;
78     return ctx->instr_cnt++;
79 }
80
81 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
82 {
83     unsigned ret;
84
85     ret = push_instr(ctx, op);
86     if(ret == -1)
87         return E_OUTOFMEMORY;
88
89     instr_ptr(ctx, ret)->arg1.lng = arg;
90     return S_OK;
91 }
92
93 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
94 {
95     unsigned instr;
96     WCHAR *str;
97
98     str = compiler_alloc_string(ctx->code, arg);
99     if(!str)
100         return E_OUTOFMEMORY;
101
102     instr = push_instr(ctx, op);
103     if(instr == -1)
104         return E_OUTOFMEMORY;
105
106     instr_ptr(ctx, instr)->arg1.str = str;
107     return S_OK;
108 }
109
110 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
111 {
112     unsigned instr;
113     double *d;
114
115     d = compiler_alloc(ctx->code, sizeof(double));
116     if(!d)
117         return E_OUTOFMEMORY;
118
119     instr = push_instr(ctx, op);
120     if(instr == -1)
121         return E_OUTOFMEMORY;
122
123     *d = arg;
124     instr_ptr(ctx, instr)->arg1.dbl = d;
125     return S_OK;
126 }
127
128 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
129 {
130     if(!ctx->code->bstr_pool_size) {
131         ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
132         if(!ctx->code->bstr_pool)
133             return NULL;
134         ctx->code->bstr_pool_size = 8;
135     }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
136        BSTR *new_pool;
137
138         new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
139         if(!new_pool)
140             return NULL;
141
142         ctx->code->bstr_pool = new_pool;
143         ctx->code->bstr_pool_size *= 2;
144     }
145
146     ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
147     if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
148         return NULL;
149
150     return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
151 }
152
153 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
154 {
155     unsigned instr;
156     BSTR bstr;
157
158     bstr = alloc_bstr_arg(ctx, arg);
159     if(!bstr)
160         return E_OUTOFMEMORY;
161
162     instr = push_instr(ctx, op);
163     if(instr == -1)
164         return E_OUTOFMEMORY;
165
166     instr_ptr(ctx, instr)->arg1.bstr = bstr;
167     return S_OK;
168 }
169
170 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
171 {
172     unsigned instr;
173     BSTR bstr;
174
175     bstr = alloc_bstr_arg(ctx, arg1);
176     if(!bstr)
177         return E_OUTOFMEMORY;
178
179     instr = push_instr(ctx, op);
180     if(instr == -1)
181         return E_OUTOFMEMORY;
182
183     instr_ptr(ctx, instr)->arg1.bstr = bstr;
184     instr_ptr(ctx, instr)->arg2.uint = arg2;
185     return S_OK;
186 }
187
188 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
189 {
190     unsigned arg_cnt = 0;
191     HRESULT hres;
192
193     while(args) {
194         hres = compile_expression(ctx, args);
195         if(FAILED(hres))
196             return hres;
197
198         arg_cnt++;
199         args = args->next;
200     }
201
202     *ret = arg_cnt;
203     return S_OK;
204 }
205
206 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
207 {
208     unsigned arg_cnt = 0;
209     HRESULT hres;
210
211     hres = compile_args(ctx, expr->args, &arg_cnt);
212     if(FAILED(hres))
213         return hres;
214
215     if(expr->obj_expr) {
216         FIXME("obj_expr not implemented\n");
217         hres = E_NOTIMPL;
218     }else {
219         hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
220     }
221
222     return hres;
223 }
224
225 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
226 {
227     HRESULT hres;
228
229     hres = compile_expression(ctx, expr->subexpr);
230     if(FAILED(hres))
231         return hres;
232
233     return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
234 }
235
236 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
237 {
238     HRESULT hres;
239
240     hres = compile_expression(ctx, expr->left);
241     if(FAILED(hres))
242         return hres;
243
244     hres = compile_expression(ctx, expr->right);
245     if(FAILED(hres))
246         return hres;
247
248     return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
249 }
250
251 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
252 {
253     switch(expr->type) {
254     case EXPR_ADD:
255         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
256     case EXPR_BOOL:
257         return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
258     case EXPR_CONCAT:
259         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
260     case EXPR_DOUBLE:
261         return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
262     case EXPR_EMPTY:
263         return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
264     case EXPR_EQUAL:
265         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
266     case EXPR_MEMBER:
267         return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
268     case EXPR_NEG:
269         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
270     case EXPR_NEQUAL:
271         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
272     case EXPR_NOT:
273         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
274     case EXPR_NULL:
275         return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
276     case EXPR_STRING:
277         return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
278     case EXPR_SUB:
279         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
280     case EXPR_USHORT:
281         return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
282     case EXPR_ULONG:
283         return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
284     default:
285         FIXME("Unimplemented expression type %d\n", expr->type);
286         return E_NOTIMPL;
287     }
288
289     return S_OK;
290 }
291
292 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat)
293 {
294     HRESULT hres;
295
296     hres = compile_expression(ctx, stat->value_expr);
297     if(FAILED(hres))
298         return hres;
299
300     if(stat->member_expr->args) {
301         FIXME("arguments support not implemented\n");
302         return E_NOTIMPL;
303     }
304
305     if(stat->member_expr->obj_expr) {
306         FIXME("obj_expr not implemented\n");
307         hres = E_NOTIMPL;
308     }else {
309         hres = push_instr_bstr(ctx, OP_assign_ident, stat->member_expr->identifier);
310     }
311
312     return hres;
313 }
314
315 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
316 {
317     HRESULT hres;
318
319     while(stat) {
320         switch(stat->type) {
321         case STAT_ASSIGN:
322             hres = compile_assign_statement(ctx, (assign_statement_t*)stat);
323             break;
324         case STAT_CALL:
325             hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
326             break;
327         default:
328             FIXME("Unimplemented statement type %d\n", stat->type);
329             hres = E_NOTIMPL;
330         }
331
332         if(FAILED(hres))
333             return hres;
334         stat = stat->next;
335     }
336
337     return S_OK;
338 }
339
340 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
341 {
342     HRESULT hres;
343
344     func->code_off = ctx->instr_cnt;
345
346     hres = compile_statement(ctx, stat);
347     if(FAILED(hres))
348         return hres;
349
350     if(push_instr(ctx, OP_ret) == -1)
351         return E_OUTOFMEMORY;
352
353     return S_OK;
354 }
355
356 void release_vbscode(vbscode_t *code)
357 {
358     unsigned i;
359
360     list_remove(&code->entry);
361
362     for(i=0; i < code->bstr_cnt; i++)
363         SysFreeString(code->bstr_pool[i]);
364
365     vbsheap_free(&code->heap);
366
367     heap_free(code->bstr_pool);
368     heap_free(code->source);
369     heap_free(code->instrs);
370     heap_free(code);
371 }
372
373 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
374 {
375     vbscode_t *ret;
376
377     ret = heap_alloc(sizeof(*ret));
378     if(!ret)
379         return NULL;
380
381     ret->source = heap_strdupW(source);
382     if(!ret->source) {
383         heap_free(ret);
384         return NULL;
385     }
386
387     ret->instrs = heap_alloc(32*sizeof(instr_t));
388     if(!ret->instrs) {
389         release_vbscode(ret);
390         return NULL;
391     }
392
393     ctx->instr_cnt = 0;
394     ctx->instr_size = 32;
395     vbsheap_init(&ret->heap);
396
397     ret->option_explicit = ctx->parser.option_explicit;
398
399     ret->bstr_pool = NULL;
400     ret->bstr_pool_size = 0;
401     ret->bstr_cnt = 0;
402
403     ret->global_code.code_ctx = ret;
404
405     list_init(&ret->entry);
406     return ret;
407 }
408
409 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
410 {
411     compile_ctx_t ctx;
412     HRESULT hres;
413
414     hres = parse_script(&ctx.parser, src);
415     if(FAILED(hres))
416         return hres;
417
418     ctx.code = alloc_vbscode(&ctx, src);
419     if(!ctx.code)
420         return E_OUTOFMEMORY;
421
422     hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
423     if(FAILED(hres)) {
424         release_vbscode(ctx.code);
425         return hres;
426     }
427
428     list_add_tail(&script->code_list, &ctx.code->entry);
429     *ret = ctx.code;
430     return S_OK;
431 }