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 #define INTRIN_VAL(VALUE, NAME, FUNC, STYPE, VTYPE) \
49 (VALUE) = ast_value_new ( \
54 (VALUE)->expression.next = (ast_expression*)ast_value_new ( \
59 (FUNC) = ast_function_new ( \
66 #define INTRIN_REG(FUNC, VALUE) \
68 vec_push(parser->functions, (FUNC)); \
69 vec_push(parser->globals, (ast_expression*)(VALUE)); \
73 ast_expression *intrin_func (parser_t *parser, const char *name);
75 #define QC_M_E 2.71828182845905
77 ast_expression *intrin_pow(parser_t *parser) {
79 * float pow(float x, float y) {
92 static ast_value *value = NULL;
95 ast_value *arg1 = ast_value_new(parser_ctx(parser), "x", TYPE_FLOAT);
96 ast_value *arg2 = ast_value_new(parser_ctx(parser), "y", TYPE_FLOAT);
97 ast_value *local = ast_value_new(parser_ctx(parser), "local", TYPE_FLOAT);
98 ast_block *body = ast_block_new(parser_ctx(parser));
99 ast_block *l1b = ast_block_new(parser_ctx(parser)); /* loop 1 body */
100 ast_block *l2b = ast_block_new(parser_ctx(parser)); /* looo 2 body */
101 ast_loop *loop1 = NULL;
102 ast_loop *loop2 = NULL;
103 ast_function *func = NULL;
105 INTRIN_VAL(value, "pow", func, "<float>", TYPE_FLOAT);
108 vec_push(value->expression.params, arg1);
109 vec_push(value->expression.params, arg2);
112 vec_push(body->locals, local);
114 /* assignment to local of value 1.0f */
115 vec_push(body->exprs,
116 (ast_expression*)ast_store_new (
119 (ast_expression*)local,
120 (ast_expression*)parser_const_float_1(parser)
126 (ast_expression*)ast_binstore_new (
130 (ast_expression*)arg2,
131 (ast_expression*)parser_const_float(parser, 0.25f)
137 (ast_expression*)ast_binstore_new (
141 (ast_expression*)arg1,
142 (ast_expression*)arg1
147 loop2 = ast_loop_new (
150 (ast_expression*)ast_binary_new (
153 (ast_expression*)arg2,
154 (ast_expression*)parser_const_float_1(parser)
163 /* push nested loop into loop expressions */
164 vec_push(l1b->exprs, (ast_expression*)loop2);
168 (ast_expression*)ast_binstore_new (
172 (ast_expression*)arg2,
173 (ast_expression*)parser_const_float_1(parser)
178 (ast_expression*)ast_binstore_new (
182 (ast_expression*)local,
183 (ast_expression*)arg1
188 loop1 = ast_loop_new (
191 (ast_expression*)ast_binary_new (
194 (ast_expression*)arg2,
195 (ast_expression*)parser_const_float_0(parser)
204 /* push the loop1 into the body for the function */
205 vec_push(body->exprs, (ast_expression*)loop1);
208 vec_push(body->exprs,
209 (ast_expression*)ast_return_new (
211 (ast_expression*)local
215 /* push block and register intrin for codegen */
216 vec_push(func->blocks, body);
218 INTRIN_REG(func, value);
221 return (ast_expression*)value;
224 ast_expression *intrin_mod(parser_t *parser) {
226 * float mod(float x, float y) {
227 * return x - y * floor(x / y);
230 static ast_value *value = NULL;
233 ast_call *call = ast_call_new (parser_ctx(parser), intrin_func(parser, "floor"));
234 ast_value *arg1 = ast_value_new(parser_ctx(parser), "x", TYPE_FLOAT);
235 ast_value *arg2 = ast_value_new(parser_ctx(parser), "y", TYPE_FLOAT);
236 ast_block *body = ast_block_new(parser_ctx(parser));
237 ast_function *func = NULL;
239 INTRIN_VAL(value, "mod", func, "<float>", TYPE_FLOAT);
242 vec_push(call->params,
243 (ast_expression*)ast_binary_new (
246 (ast_expression*)arg1,
247 (ast_expression*)arg2
251 vec_push(body->exprs,
252 (ast_expression*)ast_return_new(
254 (ast_expression*)ast_binary_new(
257 (ast_expression*)arg1,
258 (ast_expression*)ast_binary_new(
261 (ast_expression*)arg2,
262 (ast_expression*)call
268 vec_push(value->expression.params, arg1); /* float x (for param) */
269 vec_push(value->expression.params, arg2); /* float y (for param) */
271 vec_push(func->blocks, body); /* {{{ body }}} */
273 INTRIN_REG(func, value);
276 return (ast_expression*)value;
279 ast_expression *intrin_exp(parser_t *parser) {
281 * float exp(float x) {
282 * return pow(QC_M_E, x);
285 static ast_value *value = NULL;
288 ast_call *call = ast_call_new (parser_ctx(parser), intrin_func(parser, "pow"));
289 ast_value *arg1 = ast_value_new (parser_ctx(parser), "x", TYPE_FLOAT);
290 ast_block *body = ast_block_new (parser_ctx(parser));
291 ast_function *func = NULL;
293 INTRIN_VAL(value, "exp", func, "<float>", TYPE_FLOAT);
295 /* push arguments for params to call */
296 vec_push(call->params, (ast_expression*)parser_const_float(parser, QC_M_E));
297 vec_push(call->params, (ast_expression*)arg1);
299 /* return pow(QC_M_E, x) */
300 vec_push(body->exprs,
301 (ast_expression*)ast_return_new(
303 (ast_expression*)call
307 vec_push(value->expression.params, arg1); /* float x (for param) */
309 vec_push(func->blocks, body); /* {{{ body }}} */
311 INTRIN_REG(func, value);
314 return (ast_expression*)value;
317 ast_expression *intrin_isnan(parser_t *parser) {
319 * float isnan(float x) {
323 * return (x != local);
326 static ast_value *value = NULL;
329 ast_value *arg1 = ast_value_new (parser_ctx(parser), "x", TYPE_FLOAT);
330 ast_value *local = ast_value_new (parser_ctx(parser), "local", TYPE_FLOAT);
331 ast_block *body = ast_block_new (parser_ctx(parser));
332 ast_function *func = NULL;
334 INTRIN_VAL(value, "isnan", func, "<float>", TYPE_FLOAT);
336 vec_push(body->locals, local);
337 vec_push(body->exprs,
338 (ast_expression*)ast_store_new(
341 (ast_expression*)local,
342 (ast_expression*)arg1
346 vec_push(body->exprs,
347 (ast_expression*)ast_return_new(
349 (ast_expression*)ast_binary_new(
352 (ast_expression*)arg1,
353 (ast_expression*)local
358 vec_push(value->expression.params, arg1);
360 vec_push(func->blocks, body);
362 INTRIN_REG(func, value);
365 return (ast_expression*)value;
368 static intrin_t intrinsics[] = {
369 {&intrin_exp, "__builtin_exp", "exp"},
370 {&intrin_mod, "__builtin_mod", "mod"},
371 {&intrin_pow, "__builtin_pow", "pow"},
372 {&intrin_isnan, "__builtin_isnan", "isnan"}
375 void intrin_intrinsics_destroy(parser_t *parser) {
378 util_htdel(intrin_intrinsics());
380 for (i = 0; i < sizeof(intrinsics)/sizeof(intrin_t); i++)
381 ast_value_delete( (ast_value*) intrinsics[i].intrin(parser));
386 ast_expression *intrin_func(parser_t *parser, const char *name) {
387 static bool init = false;
391 /* register the intrinsics in the hashtable for O(1) lookup */
393 for (i = 0; i < sizeof(intrinsics)/sizeof(*intrinsics); i++)
394 util_htset(intrin_intrinsics(), intrinsics[i].alias, &intrinsics[i]);
396 init = true; /* only once */
400 * jesus fucking christ, Blub design something less fucking
401 * impossible to use, like a ast_is_builtin(ast_expression *), also
404 if ((find = (void*)parser_find_global(parser, name)) && ((ast_value*)find)->expression.vtype == TYPE_FUNCTION)
405 for (i = 0; i < vec_size(parser->functions); ++i)
406 if (((ast_value*)find)->name && !strcmp(parser->functions[i]->name, ((ast_value*)find)->name) && parser->functions[i]->builtin < 0)
409 if ((find = util_htget(intrin_intrinsics(), name))) {
410 /* intrinsic is in table. This will "generate the function" so
411 * to speak (if it's not already generated).
413 return ((intrin_t*)find)->intrin(parser);
416 parseerror(parser, "need function: `%s` compiler depends on it", name);