fix comment
[xonotic/gmqcc.git] / intrin.h
1 /*
2  * Copyright (C) 2012, 2013
3  *     Dale Weiler
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23
24 /*
25  * Provides all the "intrinsics" / "builtins" for GMQCC. These can do
26  * a few things, they can provide fall back implementations for math
27  * functions if the definitions don't exist for some given engine. Or
28  * then can determine definitions for existing builtins, and simply
29  * wrap back to them instead.  This is like a "portable" intrface that
30  * is entered when -fintrin is used (causing all existing builtins to
31  * be ignored by the compiler and instead interface through here.
32  */
33 typedef struct {
34     ast_expression *(*intrin)(parser_t *);
35     const char       *name;
36     const char       *alias;
37 } intrin_t;
38
39 ht intrin_intrinsics() {
40     static ht intrinsics = NULL;
41     if (!intrinsics)
42         intrinsics = util_htnew(PARSER_HT_SIZE);
43
44     return intrinsics;
45 }
46
47 void intrin_intrinsics_destroy() {
48     util_htdel(intrin_intrinsics());
49 }
50
51 #define INTRIN_VAL(VALUE, NAME, FUNC, STYPE, VTYPE)                   \
52     do {                                                              \
53         (VALUE) = ast_value_new (                                     \
54             parser_ctx(parser),                                       \
55             "__builtin_" NAME,                                        \
56             TYPE_FUNCTION                                             \
57         );                                                            \
58         (VALUE)->expression.next = (ast_expression*)ast_value_new (   \
59             parser_ctx(parser),                                       \
60             STYPE,                                                    \
61             VTYPE                                                     \
62         );                                                            \
63         (FUNC) = ast_function_new (                                   \
64             parser_ctx(parser),                                       \
65             "__builtin_" NAME,                                        \
66             (VALUE)                                                   \
67         );                                                            \
68     } while (0)
69
70 #define INTRIN_REG(FUNC, VALUE)                                       \
71     do {                                                              \
72         vec_push(parser->functions, (FUNC));                          \
73         vec_push(parser->globals,   (ast_expression*)(VALUE));        \
74     } while (0)
75
76
77 ast_expression *intrin_func (parser_t *parser, const char *name);
78
79 #define QC_M_E 2.71828182845905
80
81 ast_expression *intrin_pow(parser_t *parser) {
82     static ast_value *value = NULL;
83
84     if (!value) {
85         ast_value    *arg1  = ast_value_new(parser_ctx(parser), "x",     TYPE_FLOAT);
86         ast_value    *arg2  = ast_value_new(parser_ctx(parser), "y",     TYPE_FLOAT);
87         ast_value    *local = ast_value_new(parser_ctx(parser), "local", TYPE_FLOAT);
88         ast_block    *body  = ast_block_new(parser_ctx(parser));
89         ast_block    *l1b   = ast_block_new(parser_ctx(parser)); /* loop 1 body */
90         ast_block    *l2b   = ast_block_new(parser_ctx(parser)); /* looo 2 body */
91         ast_loop     *loop1 = NULL;
92         ast_loop     *loop2 = NULL;
93         ast_function *func  = NULL;
94
95         INTRIN_VAL(value, "pow", func, "<float>", TYPE_FLOAT);
96
97         /* arguments */
98         vec_push(value->expression.params, arg1);
99         vec_push(value->expression.params, arg2);
100
101         /* local */
102         vec_push(body->locals, local);
103
104         /* assignment to local of value 1.0f */
105         vec_push(body->exprs,
106             (ast_expression*)ast_store_new (
107                 parser_ctx(parser),
108                 INSTR_STORE_F,
109                 (ast_expression*)local,
110                 (ast_expression*)parser_const_float_1(parser)
111             )
112         );
113
114         /* y >>= 2 */
115         vec_push(l2b->exprs,
116             (ast_expression*)ast_binstore_new (
117                 parser_ctx(parser),
118                 INSTR_STORE_F,
119                 INSTR_MUL_F,
120                 (ast_expression*)arg2,
121                 (ast_expression*)parser_const_float(parser, 0.25f)
122             )
123         );
124
125         /* x *= x */
126         vec_push(l2b->exprs,
127             (ast_expression*)ast_binstore_new (
128                 parser_ctx(parser),
129                 INSTR_STORE_F,
130                 INSTR_MUL_F,
131                 (ast_expression*)arg1,
132                 (ast_expression*)arg1
133             )
134         );
135
136         /* while (!(y&1)) */
137         loop2 = ast_loop_new (
138             parser_ctx(parser),
139             NULL,
140             (ast_expression*)ast_binary_new (
141                 parser_ctx(parser),
142                 INSTR_AND,
143                 (ast_expression*)arg2,
144                 (ast_expression*)parser_const_float_1(parser)
145             ),
146             true, /* ! not */
147             NULL,
148             false,
149             NULL,
150             (ast_expression*)l2b
151         );
152
153         /* push nested loop into loop expressions */
154         vec_push(l1b->exprs, (ast_expression*)loop2);
155
156         /* y-- */
157         vec_push(l1b->exprs,
158             (ast_expression*)ast_binstore_new (
159                 parser_ctx(parser),
160                 INSTR_STORE_F,
161                 INSTR_SUB_F,
162                 (ast_expression*)arg2,
163                 (ast_expression*)parser_const_float_1(parser)
164             )
165         );
166         /* local *= x */
167         vec_push(l1b->exprs,
168             (ast_expression*)ast_binstore_new (
169                 parser_ctx(parser),
170                 INSTR_STORE_F,
171                 INSTR_MUL_F,
172                 (ast_expression*)local,
173                 (ast_expression*)arg1
174             )
175         );
176
177         /* while (y > 0) */
178         loop1 = ast_loop_new (
179             parser_ctx(parser),
180             NULL,
181             (ast_expression*)ast_binary_new (
182                 parser_ctx(parser),
183                 INSTR_GT,
184                 (ast_expression*)arg2,
185                 (ast_expression*)parser_const_float_0(parser)
186             ),
187             false,
188             NULL,
189             false,
190             NULL,
191             (ast_expression*)l1b
192         );
193
194         /* push the loop1 into the body for the function */
195         vec_push(body->exprs, (ast_expression*)loop1);
196
197         /* return local; */
198         vec_push(body->exprs,
199             (ast_expression*)ast_return_new (
200                 parser_ctx(parser),
201                 (ast_expression*)local
202             )
203         );
204
205         /* push block and register intrin for codegen */
206         vec_push(func->blocks, body);
207
208         INTRIN_REG(func, value);
209     }
210
211     return (ast_expression*)value;
212 }
213
214 ast_expression *intrin_mod(parser_t *parser) {
215     static ast_value *value = NULL;
216
217     if (!value) {
218         ast_call     *call  = ast_call_new (parser_ctx(parser), intrin_func(parser, "floor"));
219         ast_value    *arg1  = ast_value_new(parser_ctx(parser), "x", TYPE_FLOAT);
220         ast_value    *arg2  = ast_value_new(parser_ctx(parser), "y", TYPE_FLOAT);
221         ast_block    *body  = ast_block_new(parser_ctx(parser));
222         ast_function *func  = NULL;
223
224         INTRIN_VAL(value, "mod", func, "<float>", TYPE_FLOAT);
225
226         /* floor(x/y) */
227         vec_push(call->params,
228             (ast_expression*)ast_binary_new (
229                 parser_ctx(parser),
230                 INSTR_DIV_F,
231                 (ast_expression*)arg1,
232                 (ast_expression*)arg2
233             )
234         );
235
236         vec_push(body->exprs,
237             (ast_expression*)ast_return_new(
238                 parser_ctx(parser),
239                 (ast_expression*)ast_binary_new(
240                     parser_ctx(parser),
241                     INSTR_SUB_F,
242                     (ast_expression*)arg1,
243                     (ast_expression*)ast_binary_new(
244                         parser_ctx(parser),
245                         INSTR_MUL_F,
246                         (ast_expression*)arg2,
247                         (ast_expression*)call
248                     )
249                 )
250             )
251         );
252
253         vec_push(value->expression.params, arg1); /* float x (for param) */
254         vec_push(value->expression.params, arg2); /* float y (for param) */
255
256         vec_push(func->blocks,            body); /* {{{ body }}} */
257
258         INTRIN_REG(func, value);
259     }
260
261     return (ast_expression*)value;
262 }
263
264 ast_expression *intrin_exp(parser_t *parser) {
265     /*
266      * float __builtin_exp(float x) {
267      *     return __builtin_pow(QC_M_E, x);
268      * }
269      */
270     static ast_value *value = NULL;
271
272     if (!value) {
273         ast_call     *call = ast_call_new    (parser_ctx(parser), intrin_func(parser, "pow"));
274         ast_value    *arg1 = ast_value_new   (parser_ctx(parser), "x", TYPE_FLOAT);
275         ast_block    *body = ast_block_new   (parser_ctx(parser));
276         ast_function *func = NULL;
277
278         INTRIN_VAL(value, "exp", func, "<float>", TYPE_FLOAT);
279
280         /* push arguments for params to call */
281         vec_push(call->params, (ast_expression*)parser_const_float(parser, QC_M_E));
282         vec_push(call->params, (ast_expression*)arg1);
283
284         /* return pow(QC_M_E, x) */
285         vec_push(body->exprs,
286             (ast_expression*)ast_return_new(
287                 parser_ctx(parser),
288                 (ast_expression*)call
289             )
290         );
291
292         vec_push(value->expression.params, arg1); /* float x (for param) */
293
294         vec_push(func->blocks,             body); /* {{{ body }}} */
295
296         INTRIN_REG(func, value);
297     }
298
299     return (ast_expression*)value;
300 }
301
302 static intrin_t intrinsics[] = {
303     {&intrin_exp, "__builtin_exp", "exp"},
304     {&intrin_mod, "__builtin_mod", "mod"},
305     {&intrin_pow, "__builtin_pow", "pow"}
306 };
307
308 ast_expression *intrin_func(parser_t *parser, const char *name) {
309     static bool  init = false;
310     size_t       i    = 0;
311     void        *find;
312
313     /* register the intrinsics in the hashtable for O(1) lookup */
314     if (!init) {
315         for (i = 0; i < sizeof(intrinsics)/sizeof(*intrinsics); i++)
316             util_htset(intrin_intrinsics(), intrinsics[i].alias, &intrinsics[i]);
317
318         init = true; /* only once */
319     }
320
321     /*
322      * jesus fucking christ, Blub design something less fucking
323      * impossible to use, like a ast_is_builtin(ast_expression *), also
324      * use a hashtable :P 
325      */  
326     if ((find = (void*)parser_find_global(parser, name)) && ((ast_value*)find)->expression.vtype == TYPE_FUNCTION)
327         for (i = 0; i < vec_size(parser->functions); ++i)
328             if (((ast_value*)find)->name && !strcmp(parser->functions[i]->name, ((ast_value*)find)->name) && parser->functions[i]->builtin < 0)
329                 return find;
330
331     if ((find = util_htget(intrin_intrinsics(), name))) {
332         /* intrinsic is in table. This will "generate the function" so
333          * to speak (if it's not already generated).
334          */  
335         return ((intrin_t*)find)->intrin(parser);
336     }
337
338     parseerror(parser, "need function: `%s` compiler depends on it", name);
339     return NULL;
340 }