vbscript: Added '<>' expression 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_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
154 {
155     unsigned instr;
156     BSTR bstr;
157
158     bstr = alloc_bstr_arg(ctx, arg1);
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     instr_ptr(ctx, instr)->arg2.uint = arg2;
168     return S_OK;
169 }
170
171 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
172 {
173     unsigned arg_cnt = 0;
174     HRESULT hres;
175
176     while(args) {
177         hres = compile_expression(ctx, args);
178         if(FAILED(hres))
179             return hres;
180
181         arg_cnt++;
182         args = args->next;
183     }
184
185     *ret = arg_cnt;
186     return S_OK;
187 }
188
189 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
190 {
191     unsigned arg_cnt = 0;
192     HRESULT hres;
193
194     hres = compile_args(ctx, expr->args, &arg_cnt);
195     if(FAILED(hres))
196         return hres;
197
198     if(expr->obj_expr) {
199         FIXME("obj_expr not implemented\n");
200         hres = E_NOTIMPL;
201     }else {
202         hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
203     }
204
205     return hres;
206 }
207
208 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
209 {
210     HRESULT hres;
211
212     hres = compile_expression(ctx, expr->subexpr);
213     if(FAILED(hres))
214         return hres;
215
216     return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
217 }
218
219 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
220 {
221     HRESULT hres;
222
223     hres = compile_expression(ctx, expr->left);
224     if(FAILED(hres))
225         return hres;
226
227     hres = compile_expression(ctx, expr->right);
228     if(FAILED(hres))
229         return hres;
230
231     return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
232 }
233
234 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
235 {
236     switch(expr->type) {
237     case EXPR_ADD:
238         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
239     case EXPR_BOOL:
240         return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
241     case EXPR_CONCAT:
242         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
243     case EXPR_DOUBLE:
244         return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
245     case EXPR_EMPTY:
246         return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
247     case EXPR_EQUAL:
248         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
249     case EXPR_MEMBER:
250         return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
251     case EXPR_NEG:
252         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
253     case EXPR_NEQUAL:
254         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
255     case EXPR_NOT:
256         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
257     case EXPR_NULL:
258         return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
259     case EXPR_STRING:
260         return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
261     case EXPR_SUB:
262         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
263     case EXPR_USHORT:
264         return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
265     case EXPR_ULONG:
266         return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
267     default:
268         FIXME("Unimplemented expression type %d\n", expr->type);
269         return E_NOTIMPL;
270     }
271
272     return S_OK;
273 }
274
275 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
276 {
277     HRESULT hres;
278
279     while(stat) {
280         switch(stat->type) {
281         case STAT_CALL:
282             hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
283             break;
284         default:
285             FIXME("Unimplemented statement type %d\n", stat->type);
286             hres = E_NOTIMPL;
287         }
288
289         if(FAILED(hres))
290             return hres;
291         stat = stat->next;
292     }
293
294     return S_OK;
295 }
296
297 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
298 {
299     HRESULT hres;
300
301     func->code_off = ctx->instr_cnt;
302
303     hres = compile_statement(ctx, stat);
304     if(FAILED(hres))
305         return hres;
306
307     if(push_instr(ctx, OP_ret) == -1)
308         return E_OUTOFMEMORY;
309
310     return S_OK;
311 }
312
313 void release_vbscode(vbscode_t *code)
314 {
315     unsigned i;
316
317     list_remove(&code->entry);
318
319     for(i=0; i < code->bstr_cnt; i++)
320         SysFreeString(code->bstr_pool[i]);
321
322     vbsheap_free(&code->heap);
323
324     heap_free(code->bstr_pool);
325     heap_free(code->source);
326     heap_free(code->instrs);
327     heap_free(code);
328 }
329
330 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
331 {
332     vbscode_t *ret;
333
334     ret = heap_alloc(sizeof(*ret));
335     if(!ret)
336         return NULL;
337
338     ret->source = heap_strdupW(source);
339     if(!ret->source) {
340         heap_free(ret);
341         return NULL;
342     }
343
344     ret->instrs = heap_alloc(32*sizeof(instr_t));
345     if(!ret->instrs) {
346         release_vbscode(ret);
347         return NULL;
348     }
349
350     ctx->instr_cnt = 0;
351     ctx->instr_size = 32;
352     vbsheap_init(&ret->heap);
353
354     ret->option_explicit = ctx->parser.option_explicit;
355
356     ret->bstr_pool = NULL;
357     ret->bstr_pool_size = 0;
358     ret->bstr_cnt = 0;
359
360     ret->global_code.code_ctx = ret;
361
362     list_init(&ret->entry);
363     return ret;
364 }
365
366 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
367 {
368     compile_ctx_t ctx;
369     HRESULT hres;
370
371     hres = parse_script(&ctx.parser, src);
372     if(FAILED(hres))
373         return hres;
374
375     ctx.code = alloc_vbscode(&ctx, src);
376     if(!ctx.code)
377         return E_OUTOFMEMORY;
378
379     hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
380     if(FAILED(hres)) {
381         release_vbscode(ctx.code);
382         return hres;
383     }
384
385     list_add_tail(&script->code_list, &ctx.code->entry);
386     *ret = ctx.code;
387     return S_OK;
388 }