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
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.
34 ast_expression *(*intrin)(parser_t *);
39 ht intrin_intrinsics() {
40 static ht intrinsics = NULL;
42 intrinsics = util_htnew(PARSER_HT_SIZE);
47 void intrin_intrinsics_destroy() {
48 util_htdel(intrin_intrinsics());
51 #define INTRIN_VAL(VALUE, NAME, FUNC, STYPE, VTYPE) \
53 (VALUE) = ast_value_new ( \
58 (VALUE)->expression.next = (ast_expression*)ast_value_new ( \
63 (FUNC) = ast_function_new ( \
70 #define INTRIN_REG(FUNC, VALUE) \
72 vec_push(parser->functions, (FUNC)); \
73 vec_push(parser->globals, (ast_expression*)(VALUE)); \
77 ast_expression *intrin_func (parser_t *parser, const char *name);
79 #define QC_M_E 2.71828182845905
81 ast_expression *intrin_pow(parser_t *parser) {
82 static ast_value *value = NULL;
85 ast_value *arg1 = ast_value_new(parser_ctx(parser), "x", TYPE_FLOAT);
86 ast_value *arg2 = ast_value_new(parser_ctx(parser), "y", TYPE_FLOAT);
87 ast_value *local = ast_value_new(parser_ctx(parser), "local", TYPE_FLOAT);
88 ast_block *body = ast_block_new(parser_ctx(parser));
89 ast_block *l1b = ast_block_new(parser_ctx(parser)); /* loop 1 body */
90 ast_block *l2b = ast_block_new(parser_ctx(parser)); /* looo 2 body */
91 ast_loop *loop1 = NULL;
92 ast_loop *loop2 = NULL;
93 ast_function *func = NULL;
95 INTRIN_VAL(value, "pow", func, "<float>", TYPE_FLOAT);
98 vec_push(value->expression.params, arg1);
99 vec_push(value->expression.params, arg2);
102 vec_push(body->locals, local);
104 /* assignment to local of value 1.0f */
105 vec_push(body->exprs,
106 (ast_expression*)ast_store_new (
109 (ast_expression*)local,
110 (ast_expression*)parser_const_float_1(parser)
116 (ast_expression*)ast_binstore_new (
120 (ast_expression*)arg2,
121 (ast_expression*)parser_const_float(parser, 0.25f)
127 (ast_expression*)ast_binstore_new (
131 (ast_expression*)arg1,
132 (ast_expression*)arg1
137 loop2 = ast_loop_new (
140 (ast_expression*)ast_binary_new (
143 (ast_expression*)arg2,
144 (ast_expression*)parser_const_float_1(parser)
153 /* push nested loop into loop expressions */
154 vec_push(l1b->exprs, (ast_expression*)loop2);
158 (ast_expression*)ast_binstore_new (
162 (ast_expression*)arg2,
163 (ast_expression*)parser_const_float_1(parser)
168 (ast_expression*)ast_binstore_new (
172 (ast_expression*)local,
173 (ast_expression*)arg1
178 loop1 = ast_loop_new (
181 (ast_expression*)ast_binary_new (
184 (ast_expression*)arg2,
185 (ast_expression*)parser_const_float_0(parser)
194 /* push the loop1 into the body for the function */
195 vec_push(body->exprs, (ast_expression*)loop1);
198 vec_push(body->exprs,
199 (ast_expression*)ast_return_new (
201 (ast_expression*)local
205 /* push block and register intrin for codegen */
206 vec_push(func->blocks, body);
208 INTRIN_REG(func, value);
211 return (ast_expression*)value;
214 ast_expression *intrin_mod(parser_t *parser) {
215 static ast_value *value = NULL;
218 ast_call *call = ast_call_new (parser_ctx(parser), intrin_func(parser, "floor"));
219 ast_value *arg1 = ast_value_new(parser_ctx(parser), "x", TYPE_FLOAT);
220 ast_value *arg2 = ast_value_new(parser_ctx(parser), "y", TYPE_FLOAT);
221 ast_block *body = ast_block_new(parser_ctx(parser));
222 ast_function *func = NULL;
224 INTRIN_VAL(value, "mod", func, "<float>", TYPE_FLOAT);
227 vec_push(call->params,
228 (ast_expression*)ast_binary_new (
231 (ast_expression*)arg1,
232 (ast_expression*)arg2
236 vec_push(body->exprs,
237 (ast_expression*)ast_return_new(
239 (ast_expression*)ast_binary_new(
242 (ast_expression*)arg1,
243 (ast_expression*)ast_binary_new(
246 (ast_expression*)arg2,
247 (ast_expression*)call
253 vec_push(value->expression.params, arg1); /* float x (for param) */
254 vec_push(value->expression.params, arg2); /* float y (for param) */
256 vec_push(func->blocks, body); /* {{{ body }}} */
258 INTRIN_REG(func, value);
261 return (ast_expression*)value;
264 ast_expression *intrin_exp(parser_t *parser) {
266 * float __builtin_exp(float x) {
267 * return __builtin_pow(QC_M_E, x);
270 static ast_value *value = NULL;
273 ast_call *call = ast_call_new (parser_ctx(parser), intrin_func(parser, "pow"));
274 ast_value *arg1 = ast_value_new (parser_ctx(parser), "x", TYPE_FLOAT);
275 ast_block *body = ast_block_new (parser_ctx(parser));
276 ast_function *func = NULL;
278 INTRIN_VAL(value, "exp", func, "<float>", TYPE_FLOAT);
280 /* push arguments for params to call */
281 vec_push(call->params, (ast_expression*)parser_const_float(parser, QC_M_E));
282 vec_push(call->params, (ast_expression*)arg1);
284 /* return pow(QC_M_E, x) */
285 vec_push(body->exprs,
286 (ast_expression*)ast_return_new(
288 (ast_expression*)call
292 vec_push(value->expression.params, arg1); /* float x (for param) */
294 vec_push(func->blocks, body); /* {{{ body }}} */
296 INTRIN_REG(func, value);
299 return (ast_expression*)value;
302 static intrin_t intrinsics[] = {
303 {&intrin_exp, "__builtin_exp", "exp"},
304 {&intrin_mod, "__builtin_mod", "mod"},
305 {&intrin_pow, "__builtin_pow", "pow"}
308 ast_expression *intrin_func(parser_t *parser, const char *name) {
309 static bool init = false;
313 /* register the intrinsics in the hashtable for O(1) lookup */
315 for (i = 0; i < sizeof(intrinsics)/sizeof(*intrinsics); i++)
316 util_htset(intrin_intrinsics(), intrinsics[i].alias, &intrinsics[i]);
318 init = true; /* only once */
322 * jesus fucking christ, Blub design something less fucking
323 * impossible to use, like a ast_is_builtin(ast_expression *), also
326 if ((find = (void*)parser_find_global(parser, name)) && ((ast_value*)find)->expression.vtype == TYPE_FUNCTION)
327 for (i = 0; i < vec_size(parser->functions); ++i)
328 if (((ast_value*)find)->name && !strcmp(parser->functions[i]->name, ((ast_value*)find)->name) && parser->functions[i]->builtin < 0)
331 if ((find = util_htget(intrin_intrinsics(), name))) {
332 /* intrinsic is in table. This will "generate the function" so
333 * to speak (if it's not already generated).
335 return ((intrin_t*)find)->intrin(parser);
338 parseerror(parser, "need function: `%s` compiler depends on it", name);