Added isnan compiler builtin, documented what each compiler builtin does as well.
[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     /*
83      * float pow(float x, float y) {
84      *   float local = 1.0f;
85      *   while (y > 0) {
86      *     while (!(y & 1)) {
87      *       y >>= 2;
88      *       x *=  x;
89      *     }
90      *     y--;
91      *     local *= x;
92      *   }
93      *   return local;
94      * } 
95      */
96     static ast_value *value = NULL;
97
98     if (!value) {
99         ast_value    *arg1  = ast_value_new(parser_ctx(parser), "x",     TYPE_FLOAT);
100         ast_value    *arg2  = ast_value_new(parser_ctx(parser), "y",     TYPE_FLOAT);
101         ast_value    *local = ast_value_new(parser_ctx(parser), "local", TYPE_FLOAT);
102         ast_block    *body  = ast_block_new(parser_ctx(parser));
103         ast_block    *l1b   = ast_block_new(parser_ctx(parser)); /* loop 1 body */
104         ast_block    *l2b   = ast_block_new(parser_ctx(parser)); /* looo 2 body */
105         ast_loop     *loop1 = NULL;
106         ast_loop     *loop2 = NULL;
107         ast_function *func  = NULL;
108
109         INTRIN_VAL(value, "pow", func, "<float>", TYPE_FLOAT);
110
111         /* arguments */
112         vec_push(value->expression.params, arg1);
113         vec_push(value->expression.params, arg2);
114
115         /* local */
116         vec_push(body->locals, local);
117
118         /* assignment to local of value 1.0f */
119         vec_push(body->exprs,
120             (ast_expression*)ast_store_new (
121                 parser_ctx(parser),
122                 INSTR_STORE_F,
123                 (ast_expression*)local,
124                 (ast_expression*)parser_const_float_1(parser)
125             )
126         );
127
128         /* y >>= 2 */
129         vec_push(l2b->exprs,
130             (ast_expression*)ast_binstore_new (
131                 parser_ctx(parser),
132                 INSTR_STORE_F,
133                 INSTR_MUL_F,
134                 (ast_expression*)arg2,
135                 (ast_expression*)parser_const_float(parser, 0.25f)
136             )
137         );
138
139         /* x *= x */
140         vec_push(l2b->exprs,
141             (ast_expression*)ast_binstore_new (
142                 parser_ctx(parser),
143                 INSTR_STORE_F,
144                 INSTR_MUL_F,
145                 (ast_expression*)arg1,
146                 (ast_expression*)arg1
147             )
148         );
149
150         /* while (!(y&1)) */
151         loop2 = ast_loop_new (
152             parser_ctx(parser),
153             NULL,
154             (ast_expression*)ast_binary_new (
155                 parser_ctx(parser),
156                 INSTR_AND,
157                 (ast_expression*)arg2,
158                 (ast_expression*)parser_const_float_1(parser)
159             ),
160             true, /* ! not */
161             NULL,
162             false,
163             NULL,
164             (ast_expression*)l2b
165         );
166
167         /* push nested loop into loop expressions */
168         vec_push(l1b->exprs, (ast_expression*)loop2);
169
170         /* y-- */
171         vec_push(l1b->exprs,
172             (ast_expression*)ast_binstore_new (
173                 parser_ctx(parser),
174                 INSTR_STORE_F,
175                 INSTR_SUB_F,
176                 (ast_expression*)arg2,
177                 (ast_expression*)parser_const_float_1(parser)
178             )
179         );
180         /* local *= x */
181         vec_push(l1b->exprs,
182             (ast_expression*)ast_binstore_new (
183                 parser_ctx(parser),
184                 INSTR_STORE_F,
185                 INSTR_MUL_F,
186                 (ast_expression*)local,
187                 (ast_expression*)arg1
188             )
189         );
190
191         /* while (y > 0) */
192         loop1 = ast_loop_new (
193             parser_ctx(parser),
194             NULL,
195             (ast_expression*)ast_binary_new (
196                 parser_ctx(parser),
197                 INSTR_GT,
198                 (ast_expression*)arg2,
199                 (ast_expression*)parser_const_float_0(parser)
200             ),
201             false,
202             NULL,
203             false,
204             NULL,
205             (ast_expression*)l1b
206         );
207
208         /* push the loop1 into the body for the function */
209         vec_push(body->exprs, (ast_expression*)loop1);
210
211         /* return local; */
212         vec_push(body->exprs,
213             (ast_expression*)ast_return_new (
214                 parser_ctx(parser),
215                 (ast_expression*)local
216             )
217         );
218
219         /* push block and register intrin for codegen */
220         vec_push(func->blocks, body);
221
222         INTRIN_REG(func, value);
223     }
224
225     return (ast_expression*)value;
226 }
227
228 ast_expression *intrin_mod(parser_t *parser) {
229     /*
230      * float mod(float x, float y) {
231      *   return x - y * floor(x / y);
232      * }
233      */    
234     static ast_value *value = NULL;
235
236     if (!value) {
237         ast_call     *call  = ast_call_new (parser_ctx(parser), intrin_func(parser, "floor"));
238         ast_value    *arg1  = ast_value_new(parser_ctx(parser), "x", TYPE_FLOAT);
239         ast_value    *arg2  = ast_value_new(parser_ctx(parser), "y", TYPE_FLOAT);
240         ast_block    *body  = ast_block_new(parser_ctx(parser));
241         ast_function *func  = NULL;
242
243         INTRIN_VAL(value, "mod", func, "<float>", TYPE_FLOAT);
244
245         /* floor(x/y) */
246         vec_push(call->params,
247             (ast_expression*)ast_binary_new (
248                 parser_ctx(parser),
249                 INSTR_DIV_F,
250                 (ast_expression*)arg1,
251                 (ast_expression*)arg2
252             )
253         );
254
255         vec_push(body->exprs,
256             (ast_expression*)ast_return_new(
257                 parser_ctx(parser),
258                 (ast_expression*)ast_binary_new(
259                     parser_ctx(parser),
260                     INSTR_SUB_F,
261                     (ast_expression*)arg1,
262                     (ast_expression*)ast_binary_new(
263                         parser_ctx(parser),
264                         INSTR_MUL_F,
265                         (ast_expression*)arg2,
266                         (ast_expression*)call
267                     )
268                 )
269             )
270         );
271
272         vec_push(value->expression.params, arg1); /* float x (for param) */
273         vec_push(value->expression.params, arg2); /* float y (for param) */
274
275         vec_push(func->blocks,            body); /* {{{ body }}} */
276
277         INTRIN_REG(func, value);
278     }
279
280     return (ast_expression*)value;
281 }
282
283 ast_expression *intrin_exp(parser_t *parser) {
284     /*
285      * float exp(float x) {
286      *     return pow(QC_M_E, x);
287      * }
288      */
289     static ast_value *value = NULL;
290
291     if (!value) {
292         ast_call     *call = ast_call_new    (parser_ctx(parser), intrin_func(parser, "pow"));
293         ast_value    *arg1 = ast_value_new   (parser_ctx(parser), "x", TYPE_FLOAT);
294         ast_block    *body = ast_block_new   (parser_ctx(parser));
295         ast_function *func = NULL;
296
297         INTRIN_VAL(value, "exp", func, "<float>", TYPE_FLOAT);
298
299         /* push arguments for params to call */
300         vec_push(call->params, (ast_expression*)parser_const_float(parser, QC_M_E));
301         vec_push(call->params, (ast_expression*)arg1);
302
303         /* return pow(QC_M_E, x) */
304         vec_push(body->exprs,
305             (ast_expression*)ast_return_new(
306                 parser_ctx(parser),
307                 (ast_expression*)call
308             )
309         );
310
311         vec_push(value->expression.params, arg1); /* float x (for param) */
312
313         vec_push(func->blocks,             body); /* {{{ body }}} */
314
315         INTRIN_REG(func, value);
316     }
317
318     return (ast_expression*)value;
319 }
320
321 ast_expression *intrin_isnan(parser_t *parser) {
322     /*
323      * float isnan(float x) {
324      *   float local;
325      *   local = x;
326      *
327      *   return (x != local);
328      * } 
329      */      
330     static ast_value *value = NULL;
331
332     if (!value) {
333         ast_value    *arg1   = ast_value_new (parser_ctx(parser), "x", TYPE_FLOAT);
334         ast_value    *local  = ast_value_new (parser_ctx(parser), "local", TYPE_FLOAT);
335         ast_block    *body   = ast_block_new (parser_ctx(parser));
336         ast_function *func   = NULL;
337     
338         INTRIN_VAL(value, "isnan", func, "<float>", TYPE_FLOAT);
339
340         vec_push(body->locals, local);
341         vec_push(body->exprs,
342             (ast_expression*)ast_store_new(
343                 parser_ctx(parser),
344                 INSTR_STORE_F,
345                 (ast_expression*)local,
346                 (ast_expression*)arg1
347             )
348         );
349
350         vec_push(body->exprs,
351             (ast_expression*)ast_return_new(
352                 parser_ctx(parser),
353                 (ast_expression*)ast_binary_new(
354                     parser_ctx(parser),
355                     INSTR_NE_F,
356                     (ast_expression*)arg1,
357                     (ast_expression*)local
358                 )
359             )
360         );
361
362         vec_push(value->expression.params, arg1);
363
364         vec_push(func->blocks, body);
365
366         INTRIN_REG(func, value);
367     }
368
369     return (ast_expression*)value;
370 }
371
372 static intrin_t intrinsics[] = {
373     {&intrin_exp,   "__builtin_exp",   "exp"},
374     {&intrin_mod,   "__builtin_mod",   "mod"},
375     {&intrin_pow,   "__builtin_pow",   "pow"},
376     {&intrin_isnan, "__builtin_isnan", "isnan"}
377 };
378
379 ast_expression *intrin_func(parser_t *parser, const char *name) {
380     static bool  init = false;
381     size_t       i    = 0;
382     void        *find;
383
384     /* register the intrinsics in the hashtable for O(1) lookup */
385     if (!init) {
386         for (i = 0; i < sizeof(intrinsics)/sizeof(*intrinsics); i++)
387             util_htset(intrin_intrinsics(), intrinsics[i].alias, &intrinsics[i]);
388
389         init = true; /* only once */
390     }
391
392     /*
393      * jesus fucking christ, Blub design something less fucking
394      * impossible to use, like a ast_is_builtin(ast_expression *), also
395      * use a hashtable :P 
396      */  
397     if ((find = (void*)parser_find_global(parser, name)) && ((ast_value*)find)->expression.vtype == TYPE_FUNCTION)
398         for (i = 0; i < vec_size(parser->functions); ++i)
399             if (((ast_value*)find)->name && !strcmp(parser->functions[i]->name, ((ast_value*)find)->name) && parser->functions[i]->builtin < 0)
400                 return find;
401
402     if ((find = util_htget(intrin_intrinsics(), name))) {
403         /* intrinsic is in table. This will "generate the function" so
404          * to speak (if it's not already generated).
405          */  
406         return ((intrin_t*)find)->intrin(parser);
407     }
408
409     parseerror(parser, "need function: `%s` compiler depends on it", name);
410     return NULL;
411 }