2 * Copyright (C) 2012, 2013
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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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
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.
35 #define intrin_ctx(I) parser_ctx((I)->parser)
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;
43 util_snprintf(buffer, sizeof(buffer), "__builtin_%s", name);
44 util_snprintf(stype, sizeof(stype), "<%s>", type_name[vtype]);
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;
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);
61 #define QC_M_E 2.718281828459045f
62 #define QC_POW_EPSILON 0.00001f
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.
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;
81 static ast_expression *intrin_pow(intrin_t *intrin) {
84 * float pow(float base, float exp) {
97 * return 1.0 / pow(base, -exp);
99 * result = pow(base, exp / 2);
100 * return result * result;
105 * square = sqrt(base);
106 * accumulate = square;
109 * while (fabs(mid - exp) > QC_POW_EPSILON) {
110 * square = sqrt(square);
113 * accumulate *= square;
116 * accumulate *= (1.0f / square);
118 * mid = (low + high) / 2;
123 ast_value *value = NULL;
124 ast_function *func = intrin_value(intrin, &value, "pow", TYPE_FLOAT);
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) */
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));
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);
143 ast_block *body = ast_block_new(intrin_ctx(intrin));
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);
166 vec_push(value->expression.params, base);
167 vec_push(value->expression.params, exp);
173 vec_push(body->exprs,
174 (ast_expression*)ast_ifthen_new(
176 (ast_expression*)ast_binary_new(
179 (ast_expression*)exp,
180 (ast_expression*)intrin->fold->imm_float[0]
182 (ast_expression*)ast_return_new(
184 (ast_expression*)intrin->fold->imm_float[1]
194 vec_push(body->exprs,
195 (ast_expression*)ast_ifthen_new(
197 (ast_expression*)ast_binary_new(
200 (ast_expression*)exp,
201 (ast_expression*)intrin->fold->imm_float[1]
203 (ast_expression*)ast_return_new(
205 (ast_expression*)base
211 /* <callpow1> = pow(base, -exp) */
212 vec_push(callpow1->params, (ast_expression*)base);
213 vec_push(callpow1->params,
214 (ast_expression*)ast_unary_new(
223 * return 1.0 / <callpow1>;
225 vec_push(body->exprs,
226 (ast_expression*)ast_ifthen_new(
228 (ast_expression*)ast_binary_new(
231 (ast_expression*)exp,
232 (ast_expression*)intrin->fold->imm_float[0]
234 (ast_expression*)ast_return_new(
236 (ast_expression*)ast_binary_new(
239 (ast_expression*)intrin->fold->imm_float[1],
240 (ast_expression*)callpow1
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(
253 (ast_expression*)exp,
254 (ast_expression*)fold_constgen_float(intrin->fold, 2.0f)
260 * result = <callpow2>;
261 * return result * result;
264 vec_push(expgt1->exprs,
265 (ast_expression*)ast_store_new(
268 (ast_expression*)result,
269 (ast_expression*)callpow2
272 vec_push(expgt1->exprs,
273 (ast_expression*)ast_return_new(
275 (ast_expression*)ast_binary_new(
278 (ast_expression*)result,
279 (ast_expression*)result
289 vec_push(body->exprs,
290 (ast_expression*)ast_ifthen_new(
292 (ast_expression*)ast_binary_new(
295 (ast_expression*)exp,
296 (ast_expression*)intrin->fold->imm_float[1]
298 (ast_expression*)expgt1,
304 * <callsqrt1> = sqrt(base)
306 vec_push(callsqrt1->params, (ast_expression*)base);
311 * square = sqrt(base);
312 * accumulate = square;
315 vec_push(body->exprs,
316 (ast_expression*)ast_store_new(intrin_ctx(intrin),
318 (ast_expression*)low,
319 (ast_expression*)intrin->fold->imm_float[0]
322 vec_push(body->exprs,
323 (ast_expression*)ast_store_new(
326 (ast_expression*)high,
327 (ast_expression*)intrin->fold->imm_float[1]
330 vec_push(body->exprs,
331 (ast_expression*)ast_store_new(
334 (ast_expression*)square,
335 (ast_expression*)callsqrt1
338 vec_push(body->exprs,
339 (ast_expression*)ast_store_new(
342 (ast_expression*)accumulate,
343 (ast_expression*)square
346 vec_push(body->exprs,
347 (ast_expression*)ast_store_new(
350 (ast_expression*)mid,
351 (ast_expression*)ast_binary_new(
354 (ast_expression*)high,
355 (ast_expression*)fold_constgen_float(intrin->fold, 2.0f)
363 * accumulate *= square;
366 vec_push(midltexp->exprs,
367 (ast_expression*)ast_store_new(
370 (ast_expression*)low,
374 vec_push(midltexp->exprs,
375 (ast_expression*)ast_binstore_new(
379 (ast_expression*)accumulate,
380 (ast_expression*)square
387 * accumulate *= (1.0 / square);
390 vec_push(midltexpelse->exprs,
391 (ast_expression*)ast_store_new(
394 (ast_expression*)high,
398 vec_push(midltexpelse->exprs,
399 (ast_expression*)ast_binstore_new(
403 (ast_expression*)accumulate,
404 (ast_expression*)ast_binary_new(
407 (ast_expression*)intrin->fold->imm_float[1],
408 (ast_expression*)square
414 * <callsqrt2> = sqrt(square)
416 vec_push(callsqrt2->params, (ast_expression*)square);
420 * square = <callsqrt2>;
426 * mid = (low + high) / 2;
429 vec_push(whileblock->exprs,
430 (ast_expression*)ast_store_new(
433 (ast_expression*)square,
434 (ast_expression*)callsqrt2
437 vec_push(whileblock->exprs,
438 (ast_expression*)ast_ifthen_new(
440 (ast_expression*)ast_binary_new(
443 (ast_expression*)mid,
446 (ast_expression*)midltexp,
447 (ast_expression*)midltexpelse
450 vec_push(whileblock->exprs,
451 (ast_expression*)ast_store_new(
454 (ast_expression*)mid,
455 (ast_expression*)ast_binary_new(
458 (ast_expression*)ast_binary_new(
461 (ast_expression*)low,
462 (ast_expression*)high
464 (ast_expression*)fold_constgen_float(intrin->fold, 2.0f)
470 * <callabs> = fabs(mid - exp)
472 vec_push(callfabs->params,
473 (ast_expression*)ast_binary_new(
476 (ast_expression*)mid,
482 * while (<callfabs> > epsilon)
485 vec_push(body->exprs,
486 (ast_expression*)ast_loop_new(
491 (ast_expression*)ast_binary_new(
494 (ast_expression*)callfabs,
495 (ast_expression*)fold_constgen_float(intrin->fold, QC_POW_EPSILON)
503 /* increment expression */
506 (ast_expression*)whileblock
510 /* return midvalue */
511 vec_push(body->exprs,
512 (ast_expression*)ast_return_new(
514 (ast_expression*)accumulate
519 vec_push(func->blocks, body);
521 intrin_reg(intrin, value, func);
522 return (ast_expression*)value;
525 static ast_expression *intrin_mod(intrin_t *intrin) {
527 * float mod(float a, float b) {
529 * float sign = (div < 0.0f) ? -1 : 1;
530 * return a - b * sign * floor(sign * div);
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);
542 vec_push(value->expression.params, a);
543 vec_push(value->expression.params, b);
545 vec_push(body->locals, div);
546 vec_push(body->locals, sign);
549 vec_push(body->exprs,
550 (ast_expression*)ast_store_new(
553 (ast_expression*)div,
554 (ast_expression*)ast_binary_new(
563 /* sign = (div < 0.0f) ? -1 : 1; */
564 vec_push(body->exprs,
565 (ast_expression*)ast_store_new(
568 (ast_expression*)sign,
569 (ast_expression*)ast_ternary_new(
571 (ast_expression*)ast_binary_new(
574 (ast_expression*)div,
575 (ast_expression*)intrin->fold->imm_float[0]
577 (ast_expression*)intrin->fold->imm_float[2],
578 (ast_expression*)intrin->fold->imm_float[1]
583 /* floor(sign * div) */
584 vec_push(call->params,
585 (ast_expression*)ast_binary_new(
588 (ast_expression*)sign,
593 /* return a - b * sign * <call> */
594 vec_push(body->exprs,
595 (ast_expression*)ast_return_new(
597 (ast_expression*)ast_binary_new(
601 (ast_expression*)ast_binary_new(
605 (ast_expression*)ast_binary_new(
608 (ast_expression*)sign,
609 (ast_expression*)call
616 vec_push(func->blocks, body); /* {{{ body }}} */
617 intrin_reg(intrin, value, func);
619 return (ast_expression*)value;
622 static ast_expression *intrin_exp(intrin_t *intrin) {
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;
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);
636 vec_push(value->expression.params, arg1);
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(
644 (ast_expression*)ast_binary_new(
647 (ast_expression*)callpow,
648 (ast_expression*)fold_constgen_float(intrin->fold, 10.0f)
650 (ast_expression*)fold_constgen_float(intrin->fold, 0.5f)
654 /* return <callfloor> / 10.0f */
655 vec_push(body->exprs,
656 (ast_expression*)ast_return_new(
658 (ast_expression*)ast_binary_new(
661 (ast_expression*)callfloor,
662 (ast_expression*)fold_constgen_float(intrin->fold, 10.0f)
667 vec_push(func->blocks, body); /* {{{ body }}} */
669 intrin_reg(intrin, value, func);
670 return (ast_expression*)value;
673 static ast_expression *intrin_exp2(intrin_t *intrin) {
675 * float exp2(float x) {
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);
685 vec_push(value->expression.params, arg1);
687 vec_push(callpow->params, (ast_expression*)fold_constgen_float(intrin->fold, 2.0f));
688 vec_push(callpow->params, (ast_expression*)arg1);
690 /* return <callpow> */
691 vec_push(body->exprs,
692 (ast_expression*)ast_return_new(
694 (ast_expression*)callpow
698 vec_push(func->blocks, body);
700 intrin_reg(intrin, value, func);
701 return (ast_expression*)value;
704 static ast_expression *intrin_isinf(intrin_t *intrin) {
706 * float isinf(float x) {
707 * return (x != 0.0) && (x + x == x);
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);
715 vec_push(body->exprs,
716 (ast_expression*)ast_return_new(
718 (ast_expression*)ast_binary_new(
721 (ast_expression*)ast_binary_new(
725 (ast_expression*)intrin->fold->imm_float[0]
727 (ast_expression*)ast_binary_new(
730 (ast_expression*)ast_binary_new(
742 vec_push(value->expression.params, x);
743 vec_push(func->blocks, body);
745 intrin_reg(intrin, value, func);
747 return (ast_expression*)value;
750 static ast_expression *intrin_isnan(intrin_t *intrin) {
752 * float isnan(float x) {
756 * return (x != local);
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);
765 vec_push(body->locals, local);
766 vec_push(body->exprs,
767 (ast_expression*)ast_store_new(
770 (ast_expression*)local,
771 (ast_expression*)arg1
775 vec_push(body->exprs,
776 (ast_expression*)ast_return_new(
778 (ast_expression*)ast_binary_new(
781 (ast_expression*)arg1,
782 (ast_expression*)local
787 vec_push(value->expression.params, arg1);
788 vec_push(func->blocks, body);
790 intrin_reg(intrin, value, func);
792 return (ast_expression*)value;
795 static ast_expression *intrin_fabs(intrin_t *intrin) {
797 * float fabs(float x) {
798 * return x < 0 ? -x : x;
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);
806 vec_push(body->exprs,
807 (ast_expression*)ast_return_new(
809 (ast_expression*)ast_ternary_new(
811 (ast_expression*)ast_binary_new(
814 (ast_expression*)arg1,
815 (ast_expression*)intrin->fold->imm_float[0]
817 (ast_expression*)ast_unary_new(
820 (ast_expression*)arg1
822 (ast_expression*)arg1
827 vec_push(value->expression.params, arg1);
828 vec_push(func->blocks, body);
830 intrin_reg(intrin, value, func);
832 return (ast_expression*)value;
836 * TODO: make static (and handle ast_type_string) here for the builtin
837 * instead of in SYA parse close.
839 ast_expression *intrin_debug_typestring(intrin_t *intrin) {
841 return (ast_expression*)0x1;
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}
856 static void intrin_error(intrin_t *intrin, const char *fmt, ...) {
859 vcompile_error(intrin->parser->lex->tok.ctx, fmt, ap);
864 intrin_t *intrin_init(parser_t *parser) {
865 intrin_t *intrin = (intrin_t*)mem_a(sizeof(intrin_t));
868 intrin->parser = parser;
869 intrin->fold = parser->fold;
870 intrin->intrinsics = NULL;
871 intrin->generated = NULL;
873 vec_append(intrin->intrinsics, GMQCC_ARRAY_COUNT(intrinsics), intrinsics);
875 /* populate with null pointers for tracking generation */
876 for (i = 0; i < GMQCC_ARRAY_COUNT(intrinsics); i++)
877 vec_push(intrin->generated, NULL);
882 void intrin_cleanup(intrin_t *intrin) {
883 vec_free(intrin->intrinsics);
884 vec_free(intrin->generated);
888 ast_expression *intrin_fold(intrin_t *intrin, ast_value *value, ast_expression **exprs) {
890 if (!value || !value->name)
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)
896 : fold_intrin(intrin->fold, value->name + 10, exprs);
900 static GMQCC_INLINE ast_expression *intrin_func_try(intrin_t *intrin, size_t offset, const char *compare) {
902 for (i = 0; i < vec_size(intrin->intrinsics); i++) {
903 if (strcmp(*(char **)((char *)&intrin->intrinsics[i] + offset), compare))
905 if (intrin->generated[i])
906 return intrin->generated[i];
907 return intrin->generated[i] = intrin->intrinsics[i].intrin(intrin);
912 static ast_expression *intrin_func_self(intrin_t *intrin, const char *name, const char *from) {
914 ast_expression *find;
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)
921 /* try name second */
922 if ((find = intrin_func_try(intrin, offsetof(intrin_func_t, name), name)))
924 /* try alias third */
925 if ((find = intrin_func_try(intrin, offsetof(intrin_func_t, alias), name)))
929 intrin_error(intrin, "need function `%s', compiler depends on it for `__builtin_%s'", name, from);
930 return intrin_func_self(intrin, "#nullfunc", NULL);
935 ast_expression *intrin_func(intrin_t *intrin, const char *name) {
936 return intrin_func_self(intrin, name, NULL);