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) {
95 * return 1.0 / pow(base, -exp);
97 * result = pow(base, exp / 2);
98 * return result * result;
103 * square = sqrt(base);
104 * accumulate = square;
107 * while (fabs(mid - exp) > QC_POW_EPSILON) {
108 * square = sqrt(square);
111 * accumulate *= square;
114 * accumulate *= (1.0f / square);
116 * mid = (low + high) / 2;
121 ast_value *value = NULL;
122 ast_function *func = intrin_value(intrin, &value, "pow", TYPE_FLOAT);
124 /* prepare some calls for later */
125 ast_call *callpow1 = ast_call_new(intrin_ctx(intrin), (ast_expression*)value); /* for pow(base, -exp) */
126 ast_call *callpow2 = ast_call_new(intrin_ctx(intrin), (ast_expression*)value); /* for pow(vase, exp / 2) */
127 ast_call *callsqrt1 = ast_call_new(intrin_ctx(intrin), intrin_func_self(intrin, "sqrt", "pow")); /* for sqrt(base) */
128 ast_call *callsqrt2 = ast_call_new(intrin_ctx(intrin), intrin_func_self(intrin, "sqrt", "pow")); /* for sqrt(square) */
129 ast_call *callfabs = ast_call_new(intrin_ctx(intrin), intrin_func_self(intrin, "fabs", "pow")); /* for fabs(mid - exp) */
131 /* prepare some blocks for later */
132 ast_block *expgt1 = ast_block_new(intrin_ctx(intrin));
133 ast_block *midltexp = ast_block_new(intrin_ctx(intrin));
134 ast_block *midltexpelse = ast_block_new(intrin_ctx(intrin));
135 ast_block *whileblock = ast_block_new(intrin_ctx(intrin));
137 /* float pow(float base, float exp) */
138 ast_value *base = ast_value_new(intrin_ctx(intrin), "base", TYPE_FLOAT);
139 ast_value *exp = ast_value_new(intrin_ctx(intrin), "exp", TYPE_FLOAT);
141 ast_block *body = ast_block_new(intrin_ctx(intrin));
151 ast_value *result = ast_value_new(intrin_ctx(intrin), "result", TYPE_FLOAT);
152 ast_value *low = ast_value_new(intrin_ctx(intrin), "low", TYPE_FLOAT);
153 ast_value *high = ast_value_new(intrin_ctx(intrin), "high", TYPE_FLOAT);
154 ast_value *square = ast_value_new(intrin_ctx(intrin), "square", TYPE_FLOAT);
155 ast_value *accumulate = ast_value_new(intrin_ctx(intrin), "accumulate", TYPE_FLOAT);
156 ast_value *mid = ast_value_new(intrin_ctx(intrin), "mid", TYPE_FLOAT);
157 vec_push(body->locals, result);
158 vec_push(body->locals, low);
159 vec_push(body->locals, high);
160 vec_push(body->locals, square);
161 vec_push(body->locals, accumulate);
162 vec_push(body->locals, mid);
164 vec_push(value->expression.params, base);
165 vec_push(value->expression.params, exp);
171 vec_push(body->exprs,
172 (ast_expression*)ast_ifthen_new(
174 (ast_expression*)ast_binary_new(
177 (ast_expression*)exp,
178 (ast_expression*)intrin->fold->imm_float[0]
180 (ast_expression*)ast_return_new(
182 (ast_expression*)base
188 /* <callpow1> = pow(base, -exp) */
189 vec_push(callpow1->params, (ast_expression*)base);
190 vec_push(callpow1->params,
191 (ast_expression*)ast_unary_new(
200 * return 1.0 / <callpow1>;
202 vec_push(body->exprs,
203 (ast_expression*)ast_ifthen_new(
205 (ast_expression*)ast_binary_new(
208 (ast_expression*)exp,
209 (ast_expression*)intrin->fold->imm_float[0]
211 (ast_expression*)ast_return_new(
213 (ast_expression*)ast_binary_new(
216 (ast_expression*)intrin->fold->imm_float[1],
217 (ast_expression*)callpow1
224 /* <callpow2> = pow(base, exp / 2) */
225 vec_push(callpow2->params, (ast_expression*)base);
226 vec_push(callpow2->params,
227 (ast_expression*)ast_binary_new(
230 (ast_expression*)exp,
231 (ast_expression*)fold_constgen_float(intrin->fold, 2.0f)
237 * result = <callpow2>;
238 * return result * result;
241 vec_push(expgt1->exprs,
242 (ast_expression*)ast_store_new(
245 (ast_expression*)result,
246 (ast_expression*)callpow2
249 vec_push(expgt1->exprs,
250 (ast_expression*)ast_return_new(
252 (ast_expression*)ast_binary_new(
255 (ast_expression*)result,
256 (ast_expression*)result
266 vec_push(body->exprs,
267 (ast_expression*)ast_ifthen_new(
269 (ast_expression*)ast_binary_new(
272 (ast_expression*)exp,
273 (ast_expression*)intrin->fold->imm_float[1]
275 (ast_expression*)expgt1,
281 * <callsqrt1> = sqrt(base)
283 vec_push(callsqrt1->params, (ast_expression*)base);
288 * square = sqrt(base);
289 * accumulate = square;
292 vec_push(body->exprs,
293 (ast_expression*)ast_store_new(intrin_ctx(intrin),
295 (ast_expression*)low,
296 (ast_expression*)intrin->fold->imm_float[0]
299 vec_push(body->exprs,
300 (ast_expression*)ast_store_new(
303 (ast_expression*)high,
304 (ast_expression*)intrin->fold->imm_float[1]
307 vec_push(body->exprs,
308 (ast_expression*)ast_store_new(
311 (ast_expression*)square,
312 (ast_expression*)callsqrt1
315 vec_push(body->exprs,
316 (ast_expression*)ast_store_new(
319 (ast_expression*)accumulate,
320 (ast_expression*)square
323 vec_push(body->exprs,
324 (ast_expression*)ast_store_new(
327 (ast_expression*)mid,
328 (ast_expression*)ast_binary_new(
331 (ast_expression*)high,
332 (ast_expression*)fold_constgen_float(intrin->fold, 2.0f)
340 * accumulate *= square;
343 vec_push(midltexp->exprs,
344 (ast_expression*)ast_store_new(
347 (ast_expression*)low,
351 vec_push(midltexp->exprs,
352 (ast_expression*)ast_binstore_new(
356 (ast_expression*)accumulate,
357 (ast_expression*)square
364 * accumulate *= (1.0 / square);
367 vec_push(midltexpelse->exprs,
368 (ast_expression*)ast_store_new(
371 (ast_expression*)high,
375 vec_push(midltexpelse->exprs,
376 (ast_expression*)ast_binstore_new(
380 (ast_expression*)accumulate,
381 (ast_expression*)ast_binary_new(
384 (ast_expression*)intrin->fold->imm_float[1],
385 (ast_expression*)square
391 * <callsqrt2> = sqrt(square)
393 vec_push(callsqrt2->params, (ast_expression*)square);
397 * square = <callsqrt2>;
403 * mid = (low + high) / 2;
406 vec_push(whileblock->exprs,
407 (ast_expression*)ast_store_new(
410 (ast_expression*)square,
411 (ast_expression*)callsqrt2
414 vec_push(whileblock->exprs,
415 (ast_expression*)ast_ifthen_new(
417 (ast_expression*)ast_binary_new(
420 (ast_expression*)mid,
423 (ast_expression*)midltexp,
424 (ast_expression*)midltexpelse
427 vec_push(whileblock->exprs,
428 (ast_expression*)ast_store_new(
431 (ast_expression*)mid,
432 (ast_expression*)ast_binary_new(
435 (ast_expression*)ast_binary_new(
438 (ast_expression*)low,
439 (ast_expression*)high
441 (ast_expression*)fold_constgen_float(intrin->fold, 2.0f)
447 * <callabs> = fabs(mid - exp)
449 vec_push(callfabs->params,
450 (ast_expression*)ast_binary_new(
453 (ast_expression*)mid,
459 * while (<callfabs> > epsilon)
462 vec_push(body->exprs,
463 (ast_expression*)ast_loop_new(
468 (ast_expression*)ast_binary_new(
471 (ast_expression*)callfabs,
472 (ast_expression*)fold_constgen_float(intrin->fold, QC_POW_EPSILON)
480 /* increment expression */
483 (ast_expression*)whileblock
487 /* return midvalue */
488 vec_push(body->exprs,
489 (ast_expression*)ast_return_new(
491 (ast_expression*)accumulate
496 vec_push(func->blocks, body);
498 intrin_reg(intrin, value, func);
499 return (ast_expression*)value;
502 static ast_expression *intrin_mod(intrin_t *intrin) {
504 * float mod(float a, float b) {
506 * float sign = (div < 0.0f) ? -1 : 1;
507 * return a - b * sign * floor(sign * div);
510 ast_value *value = NULL;
511 ast_call *call = ast_call_new (intrin_ctx(intrin), intrin_func_self(intrin, "floor", "mod"));
512 ast_value *a = ast_value_new(intrin_ctx(intrin), "a", TYPE_FLOAT);
513 ast_value *b = ast_value_new(intrin_ctx(intrin), "b", TYPE_FLOAT);
514 ast_value *div = ast_value_new(intrin_ctx(intrin), "div", TYPE_FLOAT);
515 ast_value *sign = ast_value_new(intrin_ctx(intrin), "sign", TYPE_FLOAT);
516 ast_block *body = ast_block_new(intrin_ctx(intrin));
517 ast_function *func = intrin_value(intrin, &value, "mod", TYPE_FLOAT);
519 vec_push(value->expression.params, a);
520 vec_push(value->expression.params, b);
522 vec_push(body->locals, div);
523 vec_push(body->locals, sign);
526 vec_push(body->exprs,
527 (ast_expression*)ast_store_new(
530 (ast_expression*)div,
531 (ast_expression*)ast_binary_new(
540 /* sign = (div < 0.0f) ? -1 : 1; */
541 vec_push(body->exprs,
542 (ast_expression*)ast_store_new(
545 (ast_expression*)sign,
546 (ast_expression*)ast_ternary_new(
548 (ast_expression*)ast_binary_new(
551 (ast_expression*)div,
552 (ast_expression*)intrin->fold->imm_float[0]
554 (ast_expression*)intrin->fold->imm_float[2],
555 (ast_expression*)intrin->fold->imm_float[1]
560 /* floor(sign * div) */
561 vec_push(call->params,
562 (ast_expression*)ast_binary_new(
565 (ast_expression*)sign,
570 /* return a - b * sign * <call> */
571 vec_push(body->exprs,
572 (ast_expression*)ast_return_new(
574 (ast_expression*)ast_binary_new(
578 (ast_expression*)ast_binary_new(
582 (ast_expression*)ast_binary_new(
585 (ast_expression*)sign,
586 (ast_expression*)call
593 vec_push(func->blocks, body); /* {{{ body }}} */
594 intrin_reg(intrin, value, func);
596 return (ast_expression*)value;
599 static ast_expression *intrin_exp(intrin_t *intrin) {
601 * float exp(float x) {
602 * // mul 10 to round increments of 0.1f
603 * return floor((pow(QC_M_E, x) * 10) + 0.5) / 10;
606 ast_value *value = NULL;
607 ast_call *callpow = ast_call_new (intrin_ctx(intrin), intrin_func_self(intrin, "pow", "exp"));
608 ast_call *callfloor = ast_call_new (intrin_ctx(intrin), intrin_func_self(intrin, "floor", "exp"));
609 ast_value *arg1 = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
610 ast_block *body = ast_block_new(intrin_ctx(intrin));
611 ast_function *func = intrin_value(intrin, &value, "exp", TYPE_FLOAT);
613 vec_push(value->expression.params, arg1);
615 vec_push(callpow->params, (ast_expression*)fold_constgen_float(intrin->fold, QC_M_E));
616 vec_push(callpow->params, (ast_expression*)arg1);
617 vec_push(callfloor->params,
618 (ast_expression*)ast_binary_new(
621 (ast_expression*)ast_binary_new(
624 (ast_expression*)callpow,
625 (ast_expression*)fold_constgen_float(intrin->fold, 10.0f)
627 (ast_expression*)fold_constgen_float(intrin->fold, 0.5f)
631 /* return <callfloor> / 10.0f */
632 vec_push(body->exprs,
633 (ast_expression*)ast_return_new(
635 (ast_expression*)ast_binary_new(
638 (ast_expression*)callfloor,
639 (ast_expression*)fold_constgen_float(intrin->fold, 10.0f)
644 vec_push(func->blocks, body); /* {{{ body }}} */
646 intrin_reg(intrin, value, func);
647 return (ast_expression*)value;
650 static ast_expression *intrin_isnan(intrin_t *intrin) {
652 * float isnan(float x) {
656 * return (x != local);
659 ast_value *value = NULL;
660 ast_value *arg1 = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
661 ast_value *local = ast_value_new(intrin_ctx(intrin), "local", TYPE_FLOAT);
662 ast_block *body = ast_block_new(intrin_ctx(intrin));
663 ast_function *func = intrin_value(intrin, &value, "isnan", TYPE_FLOAT);
665 vec_push(body->locals, local);
666 vec_push(body->exprs,
667 (ast_expression*)ast_store_new(
670 (ast_expression*)local,
671 (ast_expression*)arg1
675 vec_push(body->exprs,
676 (ast_expression*)ast_return_new(
678 (ast_expression*)ast_binary_new(
681 (ast_expression*)arg1,
682 (ast_expression*)local
687 vec_push(value->expression.params, arg1);
688 vec_push(func->blocks, body);
690 intrin_reg(intrin, value, func);
692 return (ast_expression*)value;
695 static ast_expression *intrin_fabs(intrin_t *intrin) {
697 * float fabs(float x) {
698 * return x < 0 ? -x : x;
701 ast_value *value = NULL;
702 ast_value *arg1 = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
703 ast_block *body = ast_block_new(intrin_ctx(intrin));
704 ast_function *func = intrin_value(intrin, &value, "fabs", TYPE_FLOAT);
706 vec_push(body->exprs,
707 (ast_expression*)ast_return_new(
709 (ast_expression*)ast_ternary_new(
711 (ast_expression*)ast_binary_new(
714 (ast_expression*)arg1,
715 (ast_expression*)intrin->fold->imm_float[0]
717 (ast_expression*)ast_unary_new(
720 (ast_expression*)arg1
722 (ast_expression*)arg1
727 vec_push(value->expression.params, arg1);
728 vec_push(func->blocks, body);
730 intrin_reg(intrin, value, func);
732 return (ast_expression*)value;
736 * TODO: make static (and handle ast_type_string) here for the builtin
737 * instead of in SYA parse close.
739 ast_expression *intrin_debug_typestring(intrin_t *intrin) {
741 return (ast_expression*)0x1;
744 static const intrin_func_t intrinsics[] = {
745 {&intrin_exp, "__builtin_exp", "exp", 1},
746 {&intrin_mod, "__builtin_mod", "mod", 2},
747 {&intrin_pow, "__builtin_pow", "pow", 2},
748 {&intrin_isnan, "__builtin_isnan", "isnan", 1},
749 {&intrin_fabs, "__builtin_fabs", "fabs", 1},
750 {&intrin_debug_typestring, "__builtin_debug_typestring", "", 0},
751 {&intrin_nullfunc, "#nullfunc", "", 0}
754 static void intrin_error(intrin_t *intrin, const char *fmt, ...) {
757 vcompile_error(intrin->parser->lex->tok.ctx, fmt, ap);
762 intrin_t *intrin_init(parser_t *parser) {
763 intrin_t *intrin = (intrin_t*)mem_a(sizeof(intrin_t));
766 intrin->parser = parser;
767 intrin->fold = parser->fold;
768 intrin->intrinsics = NULL;
769 intrin->generated = NULL;
771 vec_append(intrin->intrinsics, GMQCC_ARRAY_COUNT(intrinsics), intrinsics);
773 /* populate with null pointers for tracking generation */
774 for (i = 0; i < GMQCC_ARRAY_COUNT(intrinsics); i++)
775 vec_push(intrin->generated, NULL);
780 void intrin_cleanup(intrin_t *intrin) {
781 vec_free(intrin->intrinsics);
782 vec_free(intrin->generated);
786 ast_expression *intrin_fold(intrin_t *intrin, ast_value *value, ast_expression **exprs) {
788 if (!value || !value->name)
790 for (i = 0; i < vec_size(intrin->intrinsics); i++)
791 if (!strcmp(value->name, intrin->intrinsics[i].name))
792 return (vec_size(exprs) != intrin->intrinsics[i].args)
794 : fold_intrin(intrin->fold, value->name + 10, exprs);
798 static GMQCC_INLINE ast_expression *intrin_func_try(intrin_t *intrin, size_t offset, const char *compare) {
800 for (i = 0; i < vec_size(intrin->intrinsics); i++) {
801 if (strcmp(*(char **)((char *)&intrin->intrinsics[i] + offset), compare))
803 if (intrin->generated[i])
804 return intrin->generated[i];
805 return intrin->generated[i] = intrin->intrinsics[i].intrin(intrin);
810 static ast_expression *intrin_func_self(intrin_t *intrin, const char *name, const char *from) {
812 ast_expression *find;
814 /* try current first */
815 if ((find = parser_find_global(intrin->parser, name)) && ((ast_value*)find)->expression.vtype == TYPE_FUNCTION)
816 for (i = 0; i < vec_size(intrin->parser->functions); ++i)
817 if (((ast_value*)find)->name && !strcmp(intrin->parser->functions[i]->name, ((ast_value*)find)->name) && intrin->parser->functions[i]->builtin < 0)
819 /* try name second */
820 if ((find = intrin_func_try(intrin, offsetof(intrin_func_t, name), name)))
822 /* try alias third */
823 if ((find = intrin_func_try(intrin, offsetof(intrin_func_t, alias), name)))
827 intrin_error(intrin, "need function `%s', compiler depends on it for `__builtin_%s'", name, from);
829 intrin_error(intrin, "need function `%s', compiler depends on it", name);
831 return intrin_func(intrin, "#nullfunc");
834 ast_expression *intrin_func(intrin_t *intrin, const char *name) {
835 return intrin_func_self(intrin, name, NULL);