Fix comments
[xonotic/gmqcc.git] / intrin.c
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 #include <string.h>
24 #include "parser.h"
25
26 /*
27  * Provides all the "intrinsics" / "builtins" for GMQCC. These can do
28  * a few things, they can provide fall back implementations for math
29  * functions if the definitions don't exist for some given engine. Or
30  * then can determine definitions for existing builtins, and simply
31  * wrap back to them instead.  This is like a "portable" intrface that
32  * is entered when -fintrin is used (causing all existing builtins to
33  * be ignored by the compiler and instead interface through here.
34  */
35 #define intrin_ctx(I) parser_ctx((I)->parser)
36
37 static GMQCC_INLINE ast_function *intrin_value(intrin_t *intrin, ast_value **out, const char *name, qcint_t vtype) {
38     ast_value    *value = NULL;
39     ast_function *func  = NULL;
40     char          buffer[1024];
41     char          stype [1024];
42
43     util_snprintf(buffer, sizeof(buffer), "__builtin_%s", name);
44     util_snprintf(stype,  sizeof(stype),   "<%s>",        type_name[vtype]);
45
46     value                    = ast_value_new(intrin_ctx(intrin), buffer, TYPE_FUNCTION);
47     value->intrinsic         = true;
48     value->expression.next   = (ast_expression*)ast_value_new(intrin_ctx(intrin), stype, vtype);
49     func                     = ast_function_new(intrin_ctx(intrin), buffer, value);
50     value->expression.flags |= AST_FLAG_ERASEABLE;
51
52     *out = value;
53     return func;
54 }
55
56 static GMQCC_INLINE void intrin_reg(intrin_t *intrin, ast_value *const value, ast_function *const func) {
57     vec_push(intrin->parser->functions, func);
58     vec_push(intrin->parser->globals,   (ast_expression*)value);
59 }
60
61 #define QC_M_E         2.718281828459045f
62 #define QC_POW_EPSILON 0.00001f
63
64 /*
65  * since some intrinsics depend on each other there is the possibility
66  * that an intrinsic will fail to get a 'depended' function that a
67  * builtin needs, causing some dependency in the chain to have a NULL
68  * function. This will cause a segmentation fault at code generation,
69  * even though an error was raised. To contiue to allow it (instead
70  * of stopping compilation right away). We need to return from the
71  * parser, before compilation stops after all the collected errors.
72  */
73 static ast_expression *intrin_func_self(intrin_t *intrin, const char *name, const char *from);
74 static ast_expression *intrin_nullfunc(intrin_t *intrin) {
75     ast_value    *value = NULL;
76     ast_function *func  = intrin_value(intrin, &value, NULL, TYPE_VOID);
77     intrin_reg(intrin, value, func);
78     return (ast_expression*)value;
79 }
80
81 static ast_expression *intrin_pow(intrin_t *intrin) {
82     /*
83      *
84      * float pow(float base, float exp) {
85      *     float result;
86      *     float low;
87      *     float high;
88      *     float mid;
89      *     float square;
90      *     float accumulate;
91      *
92      *     if (exp == 0.0)
93      *         return 1;
94      *     if (exp == 1.0)
95      *         return base;
96      *     if (exp < 0)
97      *         return 1.0 / pow(base, -exp);
98      *     if (exp >= 1) {
99      *         result = pow(base, exp / 2);
100      *         return result * result;
101      *     }
102      *
103      *     low        = 0.0f;
104      *     high       = 1.0f;
105      *     square     = sqrt(base);
106      *     accumulate = square;
107      *     mid        = high / 2.0f
108      *
109      *     while (fabs(mid - exp) > QC_POW_EPSILON) {
110      *         square = sqrt(square);
111      *         if (mid < exp) {
112      *             low         = mid;
113      *             accumulate *= square;
114      *         } else {
115      *             high        = mid;
116      *             accumulate *= (1.0f / square);
117      *         }
118      *         mid = (low + high) / 2;
119      *     }
120      *     return accumulate;
121      * }
122      */
123     ast_value    *value = NULL;
124     ast_function *func = intrin_value(intrin, &value, "pow", TYPE_FLOAT);
125
126     /* prepare some calls for later */
127     ast_call *callpow1  = ast_call_new(intrin_ctx(intrin), (ast_expression*)value);                  /* for pow(base, -exp)    */
128     ast_call *callpow2  = ast_call_new(intrin_ctx(intrin), (ast_expression*)value);                  /* for pow(vase, exp / 2) */
129     ast_call *callsqrt1 = ast_call_new(intrin_ctx(intrin), intrin_func_self(intrin, "sqrt", "pow")); /* for sqrt(base)         */
130     ast_call *callsqrt2 = ast_call_new(intrin_ctx(intrin), intrin_func_self(intrin, "sqrt", "pow")); /* for sqrt(square)       */
131     ast_call *callfabs  = ast_call_new(intrin_ctx(intrin), intrin_func_self(intrin, "fabs", "pow")); /* for fabs(mid - exp)    */
132
133     /* prepare some blocks for later */
134     ast_block *expgt1       = ast_block_new(intrin_ctx(intrin));
135     ast_block *midltexp     = ast_block_new(intrin_ctx(intrin));
136     ast_block *midltexpelse = ast_block_new(intrin_ctx(intrin));
137     ast_block *whileblock   = ast_block_new(intrin_ctx(intrin));
138
139     /* float pow(float base, float exp) */
140     ast_value    *base = ast_value_new(intrin_ctx(intrin), "base", TYPE_FLOAT);
141     ast_value    *exp  = ast_value_new(intrin_ctx(intrin), "exp",  TYPE_FLOAT);
142     /* { */
143     ast_block    *body = ast_block_new(intrin_ctx(intrin));
144
145     /*
146      * float result;
147      * float low;
148      * float high;
149      * float square;
150      * float accumulate;
151      * float mid;
152      */
153     ast_value *result     = ast_value_new(intrin_ctx(intrin), "result",     TYPE_FLOAT);
154     ast_value *low        = ast_value_new(intrin_ctx(intrin), "low",        TYPE_FLOAT);
155     ast_value *high       = ast_value_new(intrin_ctx(intrin), "high",       TYPE_FLOAT);
156     ast_value *square     = ast_value_new(intrin_ctx(intrin), "square",     TYPE_FLOAT);
157     ast_value *accumulate = ast_value_new(intrin_ctx(intrin), "accumulate", TYPE_FLOAT);
158     ast_value *mid        = ast_value_new(intrin_ctx(intrin), "mid",        TYPE_FLOAT);
159     vec_push(body->locals, result);
160     vec_push(body->locals, low);
161     vec_push(body->locals, high);
162     vec_push(body->locals, square);
163     vec_push(body->locals, accumulate);
164     vec_push(body->locals, mid);
165
166     vec_push(value->expression.params, base);
167     vec_push(value->expression.params, exp);
168
169     /*
170      * if (exp == 0.0)
171      *     return 1;
172      */
173     vec_push(body->exprs,
174         (ast_expression*)ast_ifthen_new(
175             intrin_ctx(intrin),
176             (ast_expression*)ast_binary_new(
177                 intrin_ctx(intrin),
178                 INSTR_EQ_F,
179                 (ast_expression*)exp,
180                 (ast_expression*)intrin->fold->imm_float[0]
181             ),
182             (ast_expression*)ast_return_new(
183                 intrin_ctx(intrin),
184                 (ast_expression*)intrin->fold->imm_float[1]
185             ),
186             NULL
187         )
188     );
189
190     /*
191      * if (exp == 1.0)
192      *     return base;
193      */
194     vec_push(body->exprs,
195         (ast_expression*)ast_ifthen_new(
196             intrin_ctx(intrin),
197             (ast_expression*)ast_binary_new(
198                 intrin_ctx(intrin),
199                 INSTR_EQ_F,
200                 (ast_expression*)exp,
201                 (ast_expression*)intrin->fold->imm_float[1]
202             ),
203             (ast_expression*)ast_return_new(
204                 intrin_ctx(intrin),
205                 (ast_expression*)base
206             ),
207             NULL
208         )
209     );
210
211     /* <callpow1> = pow(base, -exp) */
212     vec_push(callpow1->params, (ast_expression*)base);
213     vec_push(callpow1->params,
214         (ast_expression*)ast_unary_new(
215             intrin_ctx(intrin),
216             VINSTR_NEG_F,
217             (ast_expression*)exp
218         )
219     );
220
221     /*
222      * if (exp < 0)
223      *     return 1.0 / <callpow1>;
224      */
225     vec_push(body->exprs,
226         (ast_expression*)ast_ifthen_new(
227             intrin_ctx(intrin),
228             (ast_expression*)ast_binary_new(
229                 intrin_ctx(intrin),
230                 INSTR_LT,
231                 (ast_expression*)exp,
232                 (ast_expression*)intrin->fold->imm_float[0]
233             ),
234             (ast_expression*)ast_return_new(
235                 intrin_ctx(intrin),
236                 (ast_expression*)ast_binary_new(
237                     intrin_ctx(intrin),
238                     INSTR_DIV_F,
239                     (ast_expression*)intrin->fold->imm_float[1],
240                     (ast_expression*)callpow1
241                 )
242             ),
243             NULL
244         )
245     );
246
247     /* <callpow2> = pow(base, exp / 2) */
248     vec_push(callpow2->params, (ast_expression*)base);
249     vec_push(callpow2->params,
250         (ast_expression*)ast_binary_new(
251             intrin_ctx(intrin),
252             INSTR_DIV_F,
253             (ast_expression*)exp,
254             (ast_expression*)fold_constgen_float(intrin->fold, 2.0f)
255         )
256     );
257
258     /*
259      * <expgt1> = {
260      *     result = <callpow2>;
261      *     return result * result;
262      * }
263      */
264     vec_push(expgt1->exprs,
265         (ast_expression*)ast_store_new(
266             intrin_ctx(intrin),
267             INSTR_STORE_F,
268             (ast_expression*)result,
269             (ast_expression*)callpow2
270         )
271     );
272     vec_push(expgt1->exprs,
273         (ast_expression*)ast_return_new(
274             intrin_ctx(intrin),
275             (ast_expression*)ast_binary_new(
276                 intrin_ctx(intrin),
277                 INSTR_MUL_F,
278                 (ast_expression*)result,
279                 (ast_expression*)result
280             )
281         )
282     );
283
284     /*
285      * if (exp >= 1) {
286      *     <expgt1>
287      * }
288      */
289     vec_push(body->exprs,
290         (ast_expression*)ast_ifthen_new(
291             intrin_ctx(intrin),
292             (ast_expression*)ast_binary_new(
293                 intrin_ctx(intrin),
294                 INSTR_GE,
295                 (ast_expression*)exp,
296                 (ast_expression*)intrin->fold->imm_float[1]
297             ),
298             (ast_expression*)expgt1,
299             NULL
300         )
301     );
302
303     /*
304      * <callsqrt1> = sqrt(base)
305      */
306     vec_push(callsqrt1->params, (ast_expression*)base);
307
308     /*
309      * low        = 0.0f;
310      * high       = 1.0f;
311      * square     = sqrt(base);
312      * accumulate = square;
313      * mid        = high / 2.0f;
314      */
315     vec_push(body->exprs,
316         (ast_expression*)ast_store_new(intrin_ctx(intrin),
317             INSTR_STORE_F,
318             (ast_expression*)low,
319             (ast_expression*)intrin->fold->imm_float[0]
320         )
321     );
322     vec_push(body->exprs,
323         (ast_expression*)ast_store_new(
324             intrin_ctx(intrin),
325             INSTR_STORE_F,
326             (ast_expression*)high,
327             (ast_expression*)intrin->fold->imm_float[1]
328         )
329     );
330     vec_push(body->exprs,
331         (ast_expression*)ast_store_new(
332             intrin_ctx(intrin),
333             INSTR_STORE_F,
334             (ast_expression*)square,
335             (ast_expression*)callsqrt1
336         )
337     );
338     vec_push(body->exprs,
339         (ast_expression*)ast_store_new(
340             intrin_ctx(intrin),
341             INSTR_STORE_F,
342             (ast_expression*)accumulate,
343             (ast_expression*)square
344         )
345     );
346     vec_push(body->exprs,
347         (ast_expression*)ast_store_new(
348             intrin_ctx(intrin),
349             INSTR_STORE_F,
350             (ast_expression*)mid,
351             (ast_expression*)ast_binary_new(
352                 intrin_ctx(intrin),
353                 INSTR_DIV_F,
354                 (ast_expression*)high,
355                 (ast_expression*)fold_constgen_float(intrin->fold, 2.0f)
356             )
357         )
358     );
359
360     /*
361      * <midltexp> = {
362      *     low         = mid;
363      *     accumulate *= square;
364      * }
365      */
366     vec_push(midltexp->exprs,
367         (ast_expression*)ast_store_new(
368             intrin_ctx(intrin),
369             INSTR_STORE_F,
370             (ast_expression*)low,
371             (ast_expression*)mid
372         )
373     );
374     vec_push(midltexp->exprs,
375         (ast_expression*)ast_binstore_new(
376             intrin_ctx(intrin),
377             INSTR_STORE_F,
378             INSTR_MUL_F,
379             (ast_expression*)accumulate,
380             (ast_expression*)square
381         )
382     );
383
384     /*
385      * <midltexpelse> = {
386      *     high        = mid;
387      *     accumulate *= (1.0 / square);
388      * }
389      */
390     vec_push(midltexpelse->exprs,
391         (ast_expression*)ast_store_new(
392             intrin_ctx(intrin),
393             INSTR_STORE_F,
394             (ast_expression*)high,
395             (ast_expression*)mid
396         )
397     );
398     vec_push(midltexpelse->exprs,
399         (ast_expression*)ast_binstore_new(
400             intrin_ctx(intrin),
401             INSTR_STORE_F,
402             INSTR_MUL_F,
403             (ast_expression*)accumulate,
404             (ast_expression*)ast_binary_new(
405                 intrin_ctx(intrin),
406                 INSTR_DIV_F,
407                 (ast_expression*)intrin->fold->imm_float[1],
408                 (ast_expression*)square
409             )
410         )
411     );
412
413     /*
414      * <callsqrt2> = sqrt(square)
415      */
416     vec_push(callsqrt2->params, (ast_expression*)square);
417
418     /*
419      * <whileblock> = {
420      *     square = <callsqrt2>;
421      *     if (mid < exp)
422      *          <midltexp>;
423      *     else
424      *          <midltexpelse>;
425      *
426      *     mid = (low + high) / 2;
427      * }
428      */
429     vec_push(whileblock->exprs,
430         (ast_expression*)ast_store_new(
431             intrin_ctx(intrin),
432             INSTR_STORE_F,
433             (ast_expression*)square,
434             (ast_expression*)callsqrt2
435         )
436     );
437     vec_push(whileblock->exprs,
438         (ast_expression*)ast_ifthen_new(
439             intrin_ctx(intrin),
440             (ast_expression*)ast_binary_new(
441                 intrin_ctx(intrin),
442                 INSTR_LT,
443                 (ast_expression*)mid,
444                 (ast_expression*)exp
445             ),
446             (ast_expression*)midltexp,
447             (ast_expression*)midltexpelse
448         )
449     );
450     vec_push(whileblock->exprs,
451         (ast_expression*)ast_store_new(
452             intrin_ctx(intrin),
453             INSTR_STORE_F,
454             (ast_expression*)mid,
455             (ast_expression*)ast_binary_new(
456                 intrin_ctx(intrin),
457                 INSTR_DIV_F,
458                 (ast_expression*)ast_binary_new(
459                     intrin_ctx(intrin),
460                     INSTR_ADD_F,
461                     (ast_expression*)low,
462                     (ast_expression*)high
463                 ),
464                 (ast_expression*)fold_constgen_float(intrin->fold, 2.0f)
465             )
466         )
467     );
468
469     /*
470      * <callabs> = fabs(mid - exp)
471      */
472     vec_push(callfabs->params,
473         (ast_expression*)ast_binary_new(
474             intrin_ctx(intrin),
475             INSTR_SUB_F,
476             (ast_expression*)mid,
477             (ast_expression*)exp
478         )
479     );
480
481     /*
482      * while (<callfabs>  > epsilon)
483      *     <whileblock>
484      */
485     vec_push(body->exprs,
486         (ast_expression*)ast_loop_new(
487             intrin_ctx(intrin),
488             /* init */
489             NULL,
490             /* pre condition */
491             (ast_expression*)ast_binary_new(
492                 intrin_ctx(intrin),
493                 INSTR_GT,
494                 (ast_expression*)callfabs,
495                 (ast_expression*)fold_constgen_float(intrin->fold, QC_POW_EPSILON)
496             ),
497             /* pre not */
498             false,
499             /* post condition */
500             NULL,
501             /* post not */
502             false,
503             /* increment expression */
504             NULL,
505             /* code block */
506             (ast_expression*)whileblock
507         )
508     );
509
510     /* return accumulate */
511     vec_push(body->exprs,
512         (ast_expression*)ast_return_new(
513             intrin_ctx(intrin),
514             (ast_expression*)accumulate
515         )
516     );
517
518     /* } */
519     vec_push(func->blocks, body);
520
521     intrin_reg(intrin, value, func);
522     return (ast_expression*)value;
523 }
524
525 static ast_expression *intrin_mod(intrin_t *intrin) {
526     /*
527      * float mod(float a, float b) {
528      *     float div = a / b;
529      *     float sign = (div < 0.0f) ? -1 : 1;
530      *     return a - b * sign * floor(sign * div);
531      * }
532      */
533     ast_value    *value = NULL;
534     ast_call     *call  = ast_call_new (intrin_ctx(intrin), intrin_func_self(intrin, "floor", "mod"));
535     ast_value    *a     = ast_value_new(intrin_ctx(intrin), "a",    TYPE_FLOAT);
536     ast_value    *b     = ast_value_new(intrin_ctx(intrin), "b",    TYPE_FLOAT);
537     ast_value    *div   = ast_value_new(intrin_ctx(intrin), "div",  TYPE_FLOAT);
538     ast_value    *sign  = ast_value_new(intrin_ctx(intrin), "sign", TYPE_FLOAT);
539     ast_block    *body  = ast_block_new(intrin_ctx(intrin));
540     ast_function *func  = intrin_value(intrin, &value, "mod", TYPE_FLOAT);
541
542     vec_push(value->expression.params, a);
543     vec_push(value->expression.params, b);
544
545     vec_push(body->locals, div);
546     vec_push(body->locals, sign);
547
548     /* div = a / b; */
549     vec_push(body->exprs,
550         (ast_expression*)ast_store_new(
551             intrin_ctx(intrin),
552             INSTR_STORE_F,
553             (ast_expression*)div,
554             (ast_expression*)ast_binary_new(
555                 intrin_ctx(intrin),
556                 INSTR_DIV_F,
557                 (ast_expression*)a,
558                 (ast_expression*)b
559             )
560         )
561     );
562
563     /* sign = (div < 0.0f) ? -1 : 1; */
564     vec_push(body->exprs,
565         (ast_expression*)ast_store_new(
566             intrin_ctx(intrin),
567             INSTR_STORE_F,
568             (ast_expression*)sign,
569             (ast_expression*)ast_ternary_new(
570                 intrin_ctx(intrin),
571                 (ast_expression*)ast_binary_new(
572                     intrin_ctx(intrin),
573                     INSTR_LT,
574                     (ast_expression*)div,
575                     (ast_expression*)intrin->fold->imm_float[0]
576                 ),
577                 (ast_expression*)intrin->fold->imm_float[2],
578                 (ast_expression*)intrin->fold->imm_float[1]
579             )
580         )
581     );
582
583     /* floor(sign * div) */
584     vec_push(call->params,
585         (ast_expression*)ast_binary_new(
586             intrin_ctx(intrin),
587             INSTR_MUL_F,
588             (ast_expression*)sign,
589             (ast_expression*)div
590         )
591     );
592
593     /* return a - b * sign * <call> */
594     vec_push(body->exprs,
595         (ast_expression*)ast_return_new(
596             intrin_ctx(intrin),
597             (ast_expression*)ast_binary_new(
598                 intrin_ctx(intrin),
599                 INSTR_SUB_F,
600                 (ast_expression*)a,
601                 (ast_expression*)ast_binary_new(
602                     intrin_ctx(intrin),
603                     INSTR_MUL_F,
604                     (ast_expression*)b,
605                     (ast_expression*)ast_binary_new(
606                         intrin_ctx(intrin),
607                         INSTR_MUL_F,
608                         (ast_expression*)sign,
609                         (ast_expression*)call
610                     )
611                 )
612             )
613         )
614     );
615
616     vec_push(func->blocks, body); /* {{{ body }}} */
617     intrin_reg(intrin, value, func);
618
619     return (ast_expression*)value;
620 }
621
622 static ast_expression *intrin_exp(intrin_t *intrin) {
623     /*
624      * float exp(float x) {
625      *     // mul 10 to round increments of 0.1f
626      *     return floor((pow(QC_M_E, x) * 10) + 0.5) / 10;
627      * }
628      */
629     ast_value    *value     = NULL;
630     ast_call     *callpow   = ast_call_new (intrin_ctx(intrin), intrin_func_self(intrin, "pow", "exp"));
631     ast_call     *callfloor = ast_call_new (intrin_ctx(intrin), intrin_func_self(intrin, "floor", "exp"));
632     ast_value    *arg1      = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
633     ast_block    *body      = ast_block_new(intrin_ctx(intrin));
634     ast_function *func      = intrin_value(intrin, &value, "exp", TYPE_FLOAT);
635
636     vec_push(value->expression.params, arg1);
637
638     vec_push(callpow->params, (ast_expression*)fold_constgen_float(intrin->fold, QC_M_E));
639     vec_push(callpow->params, (ast_expression*)arg1);
640     vec_push(callfloor->params,
641         (ast_expression*)ast_binary_new(
642             intrin_ctx(intrin),
643             INSTR_ADD_F,
644             (ast_expression*)ast_binary_new(
645                 intrin_ctx(intrin),
646                 INSTR_MUL_F,
647                 (ast_expression*)callpow,
648                 (ast_expression*)fold_constgen_float(intrin->fold, 10.0f)
649             ),
650             (ast_expression*)fold_constgen_float(intrin->fold, 0.5f)
651         )
652     );
653
654     /* return <callfloor> / 10.0f */
655     vec_push(body->exprs,
656         (ast_expression*)ast_return_new(
657             intrin_ctx(intrin),
658             (ast_expression*)ast_binary_new(
659                 intrin_ctx(intrin),
660                 INSTR_DIV_F,
661                 (ast_expression*)callfloor,
662                 (ast_expression*)fold_constgen_float(intrin->fold, 10.0f)
663             )
664         )
665     );
666
667     vec_push(func->blocks, body); /* {{{ body }}} */
668
669     intrin_reg(intrin, value, func);
670     return (ast_expression*)value;
671 }
672
673 static ast_expression *intrin_exp2(intrin_t *intrin) {
674     /*
675      * float exp2(float x) {
676      *     return pow(2, x);
677      * }
678      */
679     ast_value    *value     = NULL;
680     ast_call     *callpow   = ast_call_new (intrin_ctx(intrin), intrin_func_self(intrin, "pow", "exp2"));
681     ast_value    *arg1      = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
682     ast_block    *body      = ast_block_new(intrin_ctx(intrin));
683     ast_function *func      = intrin_value(intrin, &value, "exp2", TYPE_FLOAT);
684
685     vec_push(value->expression.params, arg1);
686
687     vec_push(callpow->params, (ast_expression*)fold_constgen_float(intrin->fold, 2.0f));
688     vec_push(callpow->params, (ast_expression*)arg1);
689
690     /* return <callpow> */
691     vec_push(body->exprs,
692         (ast_expression*)ast_return_new(
693             intrin_ctx(intrin),
694             (ast_expression*)callpow
695         )
696     );
697
698     vec_push(func->blocks, body);
699
700     intrin_reg(intrin, value, func);
701     return (ast_expression*)value;
702 }
703
704 static ast_expression *intrin_isinf(intrin_t *intrin) {
705     /*
706      * float isinf(float x) {
707      *     return (x != 0.0) && (x + x == x);
708      * }
709      */
710     ast_value    *value = NULL;
711     ast_value    *x     = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
712     ast_block    *body  = ast_block_new(intrin_ctx(intrin));
713     ast_function *func  = intrin_value(intrin, &value, "isinf", TYPE_FLOAT);
714
715     vec_push(body->exprs,
716         (ast_expression*)ast_return_new(
717             intrin_ctx(intrin),
718             (ast_expression*)ast_binary_new(
719                 intrin_ctx(intrin),
720                 INSTR_AND,
721                 (ast_expression*)ast_binary_new(
722                     intrin_ctx(intrin),
723                     INSTR_NE_F,
724                     (ast_expression*)x,
725                     (ast_expression*)intrin->fold->imm_float[0]
726                 ),
727                 (ast_expression*)ast_binary_new(
728                     intrin_ctx(intrin),
729                     INSTR_EQ_F,
730                     (ast_expression*)ast_binary_new(
731                         intrin_ctx(intrin),
732                         INSTR_ADD_F,
733                         (ast_expression*)x,
734                         (ast_expression*)x
735                     ),
736                     (ast_expression*)x
737                 )
738             )
739         )
740     );
741
742     vec_push(value->expression.params, x);
743     vec_push(func->blocks, body);
744
745     intrin_reg(intrin, value, func);
746
747     return (ast_expression*)value;
748 }
749
750 static ast_expression *intrin_isnan(intrin_t *intrin) {
751     /*
752      * float isnan(float x) {
753      *   float local;
754      *   local = x;
755      *
756      *   return (x != local);
757      * }
758      */
759     ast_value    *value  = NULL;
760     ast_value    *arg1   = ast_value_new(intrin_ctx(intrin), "x",     TYPE_FLOAT);
761     ast_value    *local  = ast_value_new(intrin_ctx(intrin), "local", TYPE_FLOAT);
762     ast_block    *body   = ast_block_new(intrin_ctx(intrin));
763     ast_function *func   = intrin_value(intrin, &value, "isnan", TYPE_FLOAT);
764
765     vec_push(body->locals, local);
766     vec_push(body->exprs,
767         (ast_expression*)ast_store_new(
768             intrin_ctx(intrin),
769             INSTR_STORE_F,
770             (ast_expression*)local,
771             (ast_expression*)arg1
772         )
773     );
774
775     vec_push(body->exprs,
776         (ast_expression*)ast_return_new(
777             intrin_ctx(intrin),
778             (ast_expression*)ast_binary_new(
779                 intrin_ctx(intrin),
780                 INSTR_NE_F,
781                 (ast_expression*)arg1,
782                 (ast_expression*)local
783             )
784         )
785     );
786
787     vec_push(value->expression.params, arg1);
788     vec_push(func->blocks, body);
789
790     intrin_reg(intrin, value, func);
791
792     return (ast_expression*)value;
793 }
794
795 static ast_expression *intrin_fabs(intrin_t *intrin) {
796     /*
797      * float fabs(float x) {
798      *     return x < 0 ? -x : x;
799      * }
800      */
801     ast_value    *value  = NULL;
802     ast_value    *arg1   = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
803     ast_block    *body   = ast_block_new(intrin_ctx(intrin));
804     ast_function *func   = intrin_value(intrin, &value, "fabs", TYPE_FLOAT);
805
806     vec_push(body->exprs,
807         (ast_expression*)ast_return_new(
808             intrin_ctx(intrin),
809             (ast_expression*)ast_ternary_new(
810                 intrin_ctx(intrin),
811                 (ast_expression*)ast_binary_new(
812                     intrin_ctx(intrin),
813                     INSTR_LE,
814                     (ast_expression*)arg1,
815                     (ast_expression*)intrin->fold->imm_float[0]
816                 ),
817                 (ast_expression*)ast_unary_new(
818                     intrin_ctx(intrin),
819                     VINSTR_NEG_F,
820                     (ast_expression*)arg1
821                 ),
822                 (ast_expression*)arg1
823             )
824         )
825     );
826
827     vec_push(value->expression.params, arg1);
828     vec_push(func->blocks, body);
829
830     intrin_reg(intrin, value, func);
831
832     return (ast_expression*)value;
833 }
834
835 /*
836  * TODO: make static (and handle ast_type_string) here for the builtin
837  * instead of in SYA parse close.
838  */
839 ast_expression *intrin_debug_typestring(intrin_t *intrin) {
840     (void)intrin;
841     return (ast_expression*)0x1;
842 }
843
844 static const intrin_func_t intrinsics[] = {
845     {&intrin_exp,              "__builtin_exp",              "exp",      1},
846     {&intrin_exp2,             "__builtin_exp2",             "exp2",     1},
847     {&intrin_mod,              "__builtin_mod",              "mod",      2},
848     {&intrin_pow,              "__builtin_pow",              "pow",      2},
849     {&intrin_isnan,            "__builtin_isnan",            "isnan",    1},
850     {&intrin_isinf,            "__builtin_isinf",            "isinf",    1},
851     {&intrin_fabs,             "__builtin_fabs",             "fabs",     1},
852     {&intrin_debug_typestring, "__builtin_debug_typestring", "",         0},
853     {&intrin_nullfunc,         "#nullfunc",                  "",         0}
854 };
855
856 static void intrin_error(intrin_t *intrin, const char *fmt, ...) {
857     va_list ap;
858     va_start(ap, fmt);
859     vcompile_error(intrin->parser->lex->tok.ctx, fmt, ap);
860     va_end(ap);
861 }
862
863 /* exposed */
864 intrin_t *intrin_init(parser_t *parser) {
865     intrin_t *intrin = (intrin_t*)mem_a(sizeof(intrin_t));
866     size_t    i;
867
868     intrin->parser     = parser;
869     intrin->fold       = parser->fold;
870     intrin->intrinsics = NULL;
871     intrin->generated  = NULL;
872
873     vec_append(intrin->intrinsics, GMQCC_ARRAY_COUNT(intrinsics), intrinsics);
874
875     /* populate with null pointers for tracking generation */
876     for (i = 0; i < GMQCC_ARRAY_COUNT(intrinsics); i++)
877         vec_push(intrin->generated, NULL);
878
879     return intrin;
880 }
881
882 void intrin_cleanup(intrin_t *intrin) {
883     vec_free(intrin->intrinsics);
884     vec_free(intrin->generated);
885     mem_d(intrin);
886 }
887
888 ast_expression *intrin_fold(intrin_t *intrin, ast_value *value, ast_expression **exprs) {
889     size_t i;
890     if (!value || !value->name)
891         return NULL;
892     for (i = 0; i < vec_size(intrin->intrinsics); i++)
893         if (!strcmp(value->name, intrin->intrinsics[i].name))
894             return (vec_size(exprs) != intrin->intrinsics[i].args)
895                         ? NULL
896                         : fold_intrin(intrin->fold, value->name + 10, exprs);
897     return NULL;
898 }
899
900 static GMQCC_INLINE ast_expression *intrin_func_try(intrin_t *intrin, size_t offset, const char *compare) {
901     size_t i;
902     for (i = 0; i < vec_size(intrin->intrinsics); i++) {
903         if (strcmp(*(char **)((char *)&intrin->intrinsics[i] + offset), compare))
904             continue;
905         if (intrin->generated[i])
906             return intrin->generated[i];
907         return intrin->generated[i] = intrin->intrinsics[i].intrin(intrin);
908     }
909     return NULL;
910 }
911
912 static ast_expression *intrin_func_self(intrin_t *intrin, const char *name, const char *from) {
913     size_t           i;
914     ast_expression  *find;
915
916     /* try current first */
917     if ((find = parser_find_global(intrin->parser, name)) && ((ast_value*)find)->expression.vtype == TYPE_FUNCTION)
918         for (i = 0; i < vec_size(intrin->parser->functions); ++i)
919             if (((ast_value*)find)->name && !strcmp(intrin->parser->functions[i]->name, ((ast_value*)find)->name) && intrin->parser->functions[i]->builtin < 0)
920                 return find;
921     /* try name second */
922     if ((find = intrin_func_try(intrin, offsetof(intrin_func_t, name),  name)))
923         return find;
924     /* try alias third */
925     if ((find = intrin_func_try(intrin, offsetof(intrin_func_t, alias), name)))
926         return find;
927
928     if (from) {
929         intrin_error(intrin, "need function `%s', compiler depends on it for `__builtin_%s'", name, from);
930         return intrin_func_self(intrin, "#nullfunc", NULL);
931     }
932     return NULL;
933 }
934
935 ast_expression *intrin_func(intrin_t *intrin, const char *name) {
936     return intrin_func_self(intrin, name, NULL);
937 }