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
64 static ast_expression *intrin_pow (intrin_t *intrin) {
67 * float pow(float base, float exp) {
78 * return 1.0 / pow(base, -exp);
80 * result = pow(base, exp / 2);
81 * return result * result;
86 * square = sqrt(base);
87 * accumulate = square;
90 * while (fabs(mid - exp) > QC_POW_EPSILON) {
91 * square = sqrt(square);
94 * accumulate *= square;
97 * accumulate *= (1.0f / square);
99 * mid = (low + high) / 2;
104 ast_value *value = NULL;
105 ast_function *func = intrin_value(intrin, &value, "pow", TYPE_FLOAT);
107 /* prepare some calls for later */
108 ast_call *callpow1 = ast_call_new(intrin_ctx(intrin), (ast_expression*)value); /* for pow(base, -exp) */
109 ast_call *callpow2 = ast_call_new(intrin_ctx(intrin), (ast_expression*)value); /* for pow(vase, exp / 2) */
110 ast_call *callsqrt1 = ast_call_new(intrin_ctx(intrin), intrin_func(intrin, "sqrt")); /* for sqrt(base) */
111 ast_call *callsqrt2 = ast_call_new(intrin_ctx(intrin), intrin_func(intrin, "sqrt")); /* for sqrt(square) */
112 ast_call *callfabs = ast_call_new(intrin_ctx(intrin), intrin_func(intrin, "fabs")); /* for fabs(mid - exp) */
114 /* prepare some blocks for later */
115 ast_block *expgt1 = ast_block_new(intrin_ctx(intrin));
116 ast_block *midltexp = ast_block_new(intrin_ctx(intrin));
117 ast_block *midltexpelse = ast_block_new(intrin_ctx(intrin));
118 ast_block *whileblock = ast_block_new(intrin_ctx(intrin));
120 /* float pow(float base, float exp) */
121 ast_value *base = ast_value_new(intrin_ctx(intrin), "base", TYPE_FLOAT);
122 ast_value *exp = ast_value_new(intrin_ctx(intrin), "exp", TYPE_FLOAT);
124 ast_block *body = ast_block_new(intrin_ctx(intrin));
134 ast_value *result = ast_value_new(intrin_ctx(intrin), "result", TYPE_FLOAT);
135 ast_value *low = ast_value_new(intrin_ctx(intrin), "low", TYPE_FLOAT);
136 ast_value *high = ast_value_new(intrin_ctx(intrin), "high", TYPE_FLOAT);
137 ast_value *square = ast_value_new(intrin_ctx(intrin), "square", TYPE_FLOAT);
138 ast_value *accumulate = ast_value_new(intrin_ctx(intrin), "accumulate", TYPE_FLOAT);
139 ast_value *mid = ast_value_new(intrin_ctx(intrin), "mid", TYPE_FLOAT);
140 vec_push(body->locals, result);
141 vec_push(body->locals, low);
142 vec_push(body->locals, high);
143 vec_push(body->locals, square);
144 vec_push(body->locals, accumulate);
145 vec_push(body->locals, mid);
147 vec_push(value->expression.params, base);
148 vec_push(value->expression.params, exp);
154 vec_push(body->exprs,
155 (ast_expression*)ast_ifthen_new(
157 (ast_expression*)ast_binary_new(
160 (ast_expression*)exp,
161 (ast_expression*)intrin->fold->imm_float[0]
163 (ast_expression*)ast_return_new(
165 (ast_expression*)base
171 /* <callpow1> = pow(base, -exp) */
172 vec_push(callpow1->params, (ast_expression*)base);
173 vec_push(callpow1->params,
174 (ast_expression*)ast_unary_new(
183 * return 1.0 / <callpow1>;
185 vec_push(body->exprs,
186 (ast_expression*)ast_ifthen_new(
188 (ast_expression*)ast_binary_new(
191 (ast_expression*)exp,
192 (ast_expression*)intrin->fold->imm_float[0]
194 (ast_expression*)ast_return_new(
196 (ast_expression*)ast_binary_new(
199 (ast_expression*)intrin->fold->imm_float[1],
200 (ast_expression*)callpow1
207 /* <callpow2> = pow(base, exp / 2) */
208 vec_push(callpow2->params, (ast_expression*)base);
209 vec_push(callpow2->params,
210 (ast_expression*)ast_binary_new(
213 (ast_expression*)exp,
214 (ast_expression*)fold_constgen_float(intrin->fold, 2.0f)
220 * result = <callpow2>;
221 * return result * result;
224 vec_push(expgt1->exprs,
225 (ast_expression*)ast_store_new(
228 (ast_expression*)result,
229 (ast_expression*)callpow2
232 vec_push(expgt1->exprs,
233 (ast_expression*)ast_return_new(
235 (ast_expression*)ast_binary_new(
238 (ast_expression*)result,
239 (ast_expression*)result
249 vec_push(body->exprs,
250 (ast_expression*)ast_ifthen_new(
252 (ast_expression*)ast_binary_new(
255 (ast_expression*)exp,
256 (ast_expression*)intrin->fold->imm_float[1]
258 (ast_expression*)expgt1,
264 * <callsqrt1> = sqrt(base)
266 vec_push(callsqrt1->params, (ast_expression*)base);
271 * square = sqrt(base);
272 * accumulate = square;
275 vec_push(body->exprs,
276 (ast_expression*)ast_store_new(intrin_ctx(intrin),
278 (ast_expression*)low,
279 (ast_expression*)intrin->fold->imm_float[0]
282 vec_push(body->exprs,
283 (ast_expression*)ast_store_new(
286 (ast_expression*)high,
287 (ast_expression*)intrin->fold->imm_float[1]
290 vec_push(body->exprs,
291 (ast_expression*)ast_store_new(
294 (ast_expression*)square,
295 (ast_expression*)callsqrt1
298 vec_push(body->exprs,
299 (ast_expression*)ast_store_new(
302 (ast_expression*)accumulate,
303 (ast_expression*)square
306 vec_push(body->exprs,
307 (ast_expression*)ast_store_new(
310 (ast_expression*)mid,
311 (ast_expression*)ast_binary_new(
314 (ast_expression*)high,
315 (ast_expression*)fold_constgen_float(intrin->fold, 2.0f)
323 * accumulate *= square;
326 vec_push(midltexp->exprs,
327 (ast_expression*)ast_store_new(
330 (ast_expression*)low,
334 vec_push(midltexp->exprs,
335 (ast_expression*)ast_binstore_new(
339 (ast_expression*)accumulate,
340 (ast_expression*)square
347 * accumulate *= (1.0 / square);
350 vec_push(midltexpelse->exprs,
351 (ast_expression*)ast_store_new(
354 (ast_expression*)high,
358 vec_push(midltexpelse->exprs,
359 (ast_expression*)ast_binstore_new(
363 (ast_expression*)accumulate,
364 (ast_expression*)ast_binary_new(
367 (ast_expression*)intrin->fold->imm_float[1],
368 (ast_expression*)square
374 * <callsqrt2> = sqrt(square)
376 vec_push(callsqrt2->params, (ast_expression*)square);
380 * square = <callsqrt2>;
386 * mid = (low + high) / 2;
389 vec_push(whileblock->exprs,
390 (ast_expression*)ast_store_new(
393 (ast_expression*)square,
394 (ast_expression*)callsqrt2
397 vec_push(whileblock->exprs,
398 (ast_expression*)ast_ifthen_new(
400 (ast_expression*)ast_binary_new(
403 (ast_expression*)mid,
406 (ast_expression*)midltexp,
407 (ast_expression*)midltexpelse
410 vec_push(whileblock->exprs,
411 (ast_expression*)ast_store_new(
414 (ast_expression*)mid,
415 (ast_expression*)ast_binary_new(
418 (ast_expression*)ast_binary_new(
421 (ast_expression*)low,
422 (ast_expression*)high
424 (ast_expression*)fold_constgen_float(intrin->fold, 2.0f)
430 * <callabs> = fabs(mid - exp)
432 vec_push(callfabs->params,
433 (ast_expression*)ast_binary_new(
436 (ast_expression*)mid,
442 * while (<callfabs> > epsilon)
445 vec_push(body->exprs,
446 (ast_expression*)ast_loop_new(
451 (ast_expression*)ast_binary_new(
454 (ast_expression*)callfabs,
455 (ast_expression*)fold_constgen_float(intrin->fold, QC_POW_EPSILON)
463 /* increment expression */
466 (ast_expression*)whileblock
470 /* return midvalue */
471 vec_push(body->exprs,
472 (ast_expression*)ast_return_new(
474 (ast_expression*)accumulate
479 vec_push(func->blocks, body);
481 intrin_reg(intrin, value, func);
482 return (ast_expression*)value;
485 static ast_expression *intrin_mod(intrin_t *intrin) {
487 * float mod(float a, float b) {
489 * float sign = (div < 0.0f) ? -1 : 1;
490 * return a - b * sign * floor(sign * div);
493 ast_value *value = NULL;
494 ast_call *call = ast_call_new (intrin_ctx(intrin), intrin_func(intrin, "floor"));
495 ast_value *a = ast_value_new(intrin_ctx(intrin), "a", TYPE_FLOAT);
496 ast_value *b = ast_value_new(intrin_ctx(intrin), "b", TYPE_FLOAT);
497 ast_value *div = ast_value_new(intrin_ctx(intrin), "div", TYPE_FLOAT);
498 ast_value *sign = ast_value_new(intrin_ctx(intrin), "sign", TYPE_FLOAT);
499 ast_block *body = ast_block_new(intrin_ctx(intrin));
500 ast_function *func = intrin_value(intrin, &value, "mod", TYPE_FLOAT);
502 vec_push(value->expression.params, a);
503 vec_push(value->expression.params, b);
505 vec_push(body->locals, div);
506 vec_push(body->locals, sign);
509 vec_push(body->exprs,
510 (ast_expression*)ast_store_new(
513 (ast_expression*)div,
514 (ast_expression*)ast_binary_new(
523 /* sign = (div < 0.0f) ? -1 : 1; */
524 vec_push(body->exprs,
525 (ast_expression*)ast_store_new(
528 (ast_expression*)sign,
529 (ast_expression*)ast_ternary_new(
531 (ast_expression*)ast_binary_new(
534 (ast_expression*)div,
535 (ast_expression*)intrin->fold->imm_float[0]
537 (ast_expression*)intrin->fold->imm_float[2],
538 (ast_expression*)intrin->fold->imm_float[1]
543 /* floor(sign * div) */
544 vec_push(call->params,
545 (ast_expression*)ast_binary_new(
548 (ast_expression*)sign,
553 /* return a - b * sign * <call> */
554 vec_push(body->exprs,
555 (ast_expression*)ast_return_new(
557 (ast_expression*)ast_binary_new(
561 (ast_expression*)ast_binary_new(
565 (ast_expression*)ast_binary_new(
568 (ast_expression*)sign,
569 (ast_expression*)call
576 vec_push(func->blocks, body); /* {{{ body }}} */
577 intrin_reg(intrin, value, func);
579 return (ast_expression*)value;
582 static ast_expression *intrin_exp(intrin_t *intrin) {
584 * float exp(float x) {
585 * // mul 10 to round increments of 0.1f
586 * return floor((pow(QC_M_E, x) * 10) + 0.5) / 10;
589 ast_value *value = NULL;
590 ast_call *callpow = ast_call_new (intrin_ctx(intrin), intrin_func(intrin, "pow"));
591 ast_call *callfloor = ast_call_new (intrin_ctx(intrin), intrin_func(intrin, "floor"));
592 ast_value *arg1 = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
593 ast_block *body = ast_block_new(intrin_ctx(intrin));
594 ast_function *func = intrin_value(intrin, &value, "exp", TYPE_FLOAT);
596 vec_push(value->expression.params, arg1);
598 vec_push(callpow->params, (ast_expression*)fold_constgen_float(intrin->fold, QC_M_E));
599 vec_push(callpow->params, (ast_expression*)arg1);
600 vec_push(callfloor->params,
601 (ast_expression*)ast_binary_new(
604 (ast_expression*)ast_binary_new(
607 (ast_expression*)callpow,
608 (ast_expression*)fold_constgen_float(intrin->fold, 10.0f)
610 (ast_expression*)fold_constgen_float(intrin->fold, 0.5f)
614 /* return <callfloor> / 10.0f */
615 vec_push(body->exprs,
616 (ast_expression*)ast_return_new(
618 (ast_expression*)ast_binary_new(
621 (ast_expression*)callfloor,
622 (ast_expression*)fold_constgen_float(intrin->fold, 10.0f)
627 vec_push(func->blocks, body); /* {{{ body }}} */
629 intrin_reg(intrin, value, func);
630 return (ast_expression*)value;
633 static ast_expression *intrin_isnan(intrin_t *intrin) {
635 * float isnan(float x) {
639 * return (x != local);
642 ast_value *value = NULL;
643 ast_value *arg1 = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
644 ast_value *local = ast_value_new(intrin_ctx(intrin), "local", TYPE_FLOAT);
645 ast_block *body = ast_block_new(intrin_ctx(intrin));
646 ast_function *func = intrin_value(intrin, &value, "isnan", TYPE_FLOAT);
648 vec_push(body->locals, local);
649 vec_push(body->exprs,
650 (ast_expression*)ast_store_new(
653 (ast_expression*)local,
654 (ast_expression*)arg1
658 vec_push(body->exprs,
659 (ast_expression*)ast_return_new(
661 (ast_expression*)ast_binary_new(
664 (ast_expression*)arg1,
665 (ast_expression*)local
670 vec_push(value->expression.params, arg1);
671 vec_push(func->blocks, body);
673 intrin_reg(intrin, value, func);
675 return (ast_expression*)value;
678 static ast_expression *intrin_fabs(intrin_t *intrin) {
680 * float fabs(float x) {
681 * return x < 0 ? -x : x;
684 ast_value *value = NULL;
685 ast_value *arg1 = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
686 ast_block *body = ast_block_new(intrin_ctx(intrin));
687 ast_function *func = intrin_value(intrin, &value, "fabs", TYPE_FLOAT);
689 vec_push(body->exprs,
690 (ast_expression*)ast_return_new(
692 (ast_expression*)ast_ternary_new(
694 (ast_expression*)ast_binary_new(
697 (ast_expression*)arg1,
698 (ast_expression*)intrin->fold->imm_float[0]
700 (ast_expression*)ast_unary_new(
703 (ast_expression*)arg1
705 (ast_expression*)arg1
710 vec_push(value->expression.params, arg1);
711 vec_push(func->blocks, body);
713 intrin_reg(intrin, value, func);
715 return (ast_expression*)value;
719 * TODO: make static (and handle ast_type_string) here for the builtin
720 * instead of in SYA parse close.
722 ast_expression *intrin_debug_typestring(intrin_t *intrin) {
724 return (ast_expression*)0x1;
727 static const intrin_func_t intrinsics[] = {
728 {&intrin_exp, "__builtin_exp", "exp", 1},
729 {&intrin_mod, "__builtin_mod", "mod", 2},
730 {&intrin_pow, "__builtin_pow", "pow", 2},
731 {&intrin_isnan, "__builtin_isnan", "isnan", 1},
732 {&intrin_fabs, "__builtin_fabs", "fabs", 1},
733 {&intrin_debug_typestring, "__builtin_debug_typestring", "", 0}
736 static void intrin_error(intrin_t *intrin, const char *fmt, ...) {
739 vcompile_error(intrin->parser->lex->tok.ctx, fmt, ap);
744 intrin_t *intrin_init(parser_t *parser) {
745 intrin_t *intrin = (intrin_t*)mem_a(sizeof(intrin_t));
748 intrin->parser = parser;
749 intrin->fold = parser->fold;
750 intrin->intrinsics = NULL;
751 intrin->generated = NULL;
753 vec_append(intrin->intrinsics, GMQCC_ARRAY_COUNT(intrinsics), intrinsics);
755 /* populate with null pointers for tracking generation */
756 for (i = 0; i < GMQCC_ARRAY_COUNT(intrinsics); i++)
757 vec_push(intrin->generated, NULL);
762 void intrin_cleanup(intrin_t *intrin) {
763 vec_free(intrin->intrinsics);
764 vec_free(intrin->generated);
768 ast_expression *intrin_fold(intrin_t *intrin, ast_value *value, ast_expression **exprs) {
770 if (!value || !value->name)
772 for (i = 0; i < vec_size(intrin->intrinsics); i++)
773 if (!strcmp(value->name, intrin->intrinsics[i].name))
774 return (vec_size(exprs) != intrin->intrinsics[i].args)
776 : fold_intrin(intrin->fold, value->name + 10, exprs);
780 static GMQCC_INLINE ast_expression *intrin_func_try(intrin_t *intrin, size_t offset, const char *compare) {
782 for (i = 0; i < vec_size(intrin->intrinsics); i++) {
783 if (strcmp(*(char **)((char *)&intrin->intrinsics[i] + offset), compare))
785 if (intrin->generated[i])
786 return intrin->generated[i];
787 return intrin->generated[i] = intrin->intrinsics[i].intrin(intrin);
792 ast_expression *intrin_func(intrin_t *intrin, const char *name) {
794 ast_expression *find;
796 /* try current first */
797 if ((find = parser_find_global(intrin->parser, name)) && ((ast_value*)find)->expression.vtype == TYPE_FUNCTION)
798 for (i = 0; i < vec_size(intrin->parser->functions); ++i)
799 if (((ast_value*)find)->name && !strcmp(intrin->parser->functions[i]->name, ((ast_value*)find)->name) && intrin->parser->functions[i]->builtin < 0)
801 /* try name second */
802 if ((find = intrin_func_try(intrin, offsetof(intrin_func_t, name), name)))
804 /* try alias third */
805 if ((find = intrin_func_try(intrin, offsetof(intrin_func_t, alias), name)))
808 intrin_error(intrin, "need function: `%s` compiler depends on it", name);