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_VAL(VALUE, NAME, FUNC, STYPE, VTYPE) \
37 (VALUE) = ast_value_new ( \
38 parser_ctx(intrin->parser), \
42 (VALUE)->expression.next = (ast_expression*)ast_value_new ( \
43 parser_ctx(intrin->parser), \
47 (FUNC) = ast_function_new ( \
48 parser_ctx(intrin->parser), \
54 #define INTRIN_REG(FUNC, VALUE) \
56 vec_push(intrin->parser->functions, (FUNC)); \
57 vec_push(intrin->parser->globals, (ast_expression*)(VALUE)); \
60 #define QC_M_E 2.71828182845905
62 static ast_expression *intrin_pow (intrin_t *intrin) {
64 * float pow(float x, float y) {
77 static ast_value *value = NULL;
80 ast_value *arg1 = ast_value_new(parser_ctx(intrin->parser), "x", TYPE_FLOAT);
81 ast_value *arg2 = ast_value_new(parser_ctx(intrin->parser), "y", TYPE_FLOAT);
82 ast_value *local = ast_value_new(parser_ctx(intrin->parser), "local", TYPE_FLOAT);
83 ast_block *body = ast_block_new(parser_ctx(intrin->parser));
84 ast_block *l1b = ast_block_new(parser_ctx(intrin->parser)); /* loop 1 body */
85 ast_block *l2b = ast_block_new(parser_ctx(intrin->parser)); /* loop 2 body */
86 ast_loop *loop1 = NULL;
87 ast_loop *loop2 = NULL;
88 ast_function *func = NULL;
90 INTRIN_VAL(value, "pow", func, "<float>", TYPE_FLOAT);
93 vec_push(value->expression.params, arg1);
94 vec_push(value->expression.params, arg2);
97 vec_push(body->locals, local);
99 /* assignment to local of value 1.0f */
100 vec_push(body->exprs,
101 (ast_expression*)ast_store_new (
102 parser_ctx(intrin->parser),
104 (ast_expression*)local,
105 (ast_expression*)intrin->fold->imm_float[1] /* 1 == 1.0f */
111 (ast_expression*)ast_binstore_new (
112 parser_ctx(intrin->parser),
115 (ast_expression*)arg2,
116 (ast_expression*)fold_constgen_float(intrin->parser->fold, 0.25f)
122 (ast_expression*)ast_binstore_new (
123 parser_ctx(intrin->parser),
126 (ast_expression*)arg1,
127 (ast_expression*)arg1
132 loop2 = ast_loop_new (
133 parser_ctx(intrin->parser),
135 (ast_expression*)ast_binary_new (
136 parser_ctx(intrin->parser),
138 (ast_expression*)arg2,
139 (ast_expression*)intrin->fold->imm_float[1] /* 1 == 1.0f */
148 /* push nested loop into loop expressions */
149 vec_push(l1b->exprs, (ast_expression*)loop2);
153 (ast_expression*)ast_binstore_new (
154 parser_ctx(intrin->parser),
157 (ast_expression*)arg2,
158 (ast_expression*)intrin->fold->imm_float[1] /* 1 == 1.0f */
163 (ast_expression*)ast_binstore_new (
164 parser_ctx(intrin->parser),
167 (ast_expression*)local,
168 (ast_expression*)arg1
173 loop1 = ast_loop_new (
174 parser_ctx(intrin->parser),
176 (ast_expression*)ast_binary_new (
177 parser_ctx(intrin->parser),
179 (ast_expression*)arg2,
180 (ast_expression*)intrin->fold->imm_float[0] /* 0 == 0.0f */
189 /* push the loop1 into the body for the function */
190 vec_push(body->exprs, (ast_expression*)loop1);
193 vec_push(body->exprs,
194 (ast_expression*)ast_return_new (
195 parser_ctx(intrin->parser),
196 (ast_expression*)local
200 /* push block and register intrin for codegen */
201 vec_push(func->blocks, body);
203 INTRIN_REG(func, value);
206 return (ast_expression*)value;
209 static ast_expression *intrin_mod(intrin_t *intrin) {
211 * float mod(float x, float y) {
212 * return x - y * floor(x / y);
215 static ast_value *value = NULL;
218 ast_call *call = ast_call_new (parser_ctx(intrin->parser), intrin_func(intrin, "floor"));
219 ast_value *arg1 = ast_value_new(parser_ctx(intrin->parser), "x", TYPE_FLOAT);
220 ast_value *arg2 = ast_value_new(parser_ctx(intrin->parser), "y", TYPE_FLOAT);
221 ast_block *body = ast_block_new(parser_ctx(intrin->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 (
229 parser_ctx(intrin->parser),
231 (ast_expression*)arg1,
232 (ast_expression*)arg2
236 vec_push(body->exprs,
237 (ast_expression*)ast_return_new(
238 parser_ctx(intrin->parser),
239 (ast_expression*)ast_binary_new(
240 parser_ctx(intrin->parser),
242 (ast_expression*)arg1,
243 (ast_expression*)ast_binary_new(
244 parser_ctx(intrin->parser),
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 static ast_expression *intrin_exp(intrin_t *intrin) {
266 * float exp(float x) {
267 * return pow(QC_M_E, x);
270 static ast_value *value = NULL;
273 ast_call *call = ast_call_new (parser_ctx(intrin->parser), intrin_func(intrin, "pow"));
274 ast_value *arg1 = ast_value_new(parser_ctx(intrin->parser), "x", TYPE_FLOAT);
275 ast_block *body = ast_block_new(parser_ctx(intrin->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*)fold_constgen_float(intrin->fold, 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(
287 parser_ctx(intrin->parser),
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 ast_expression *intrin_isnan(intrin_t *intrin) {
304 * float isnan(float x) {
308 * return (x != local);
311 static ast_value *value = NULL;
314 ast_value *arg1 = ast_value_new(parser_ctx(intrin->parser), "x", TYPE_FLOAT);
315 ast_value *local = ast_value_new(parser_ctx(intrin->parser), "local", TYPE_FLOAT);
316 ast_block *body = ast_block_new(parser_ctx(intrin->parser));
317 ast_function *func = NULL;
319 INTRIN_VAL(value, "isnan", func, "<float>", TYPE_FLOAT);
321 vec_push(body->locals, local);
322 vec_push(body->exprs,
323 (ast_expression*)ast_store_new(
324 parser_ctx(intrin->parser),
326 (ast_expression*)local,
327 (ast_expression*)arg1
331 vec_push(body->exprs,
332 (ast_expression*)ast_return_new(
333 parser_ctx(intrin->parser),
334 (ast_expression*)ast_binary_new(
335 parser_ctx(intrin->parser),
337 (ast_expression*)arg1,
338 (ast_expression*)local
343 vec_push(value->expression.params, arg1);
344 vec_push(func->blocks, body);
346 INTRIN_REG(func, value);
349 return (ast_expression*)value;
355 * TODO: make static (and handle ast_type_string) here for the builtin
356 * instead of in SYA parse close.
358 ast_expression *intrin_debug_typestring(intrin_t *intrin) {
360 return (ast_expression*)0x1;
363 static const intrin_func_t intrinsics[] = {
364 {&intrin_exp, "__builtin_exp", "exp"},
365 {&intrin_mod, "__builtin_mod", "mod"},
366 {&intrin_pow, "__builtin_pow", "pow"},
367 {&intrin_isnan, "__builtin_isnan", "isnan"},
368 {&intrin_debug_typestring, "__builtin_debug_typestring", ""}
371 static void intrin_error(intrin_t *intrin, const char *fmt, ...) {
374 vcompile_error(intrin->parser->lex->tok.ctx, fmt, ap);
379 intrin_t *intrin_init(parser_t *parser) {
380 intrin_t *intrin = (intrin_t*)mem_a(sizeof(intrin_t));
381 intrin->parser = parser;
382 intrin->fold = parser->fold;
383 intrin->intrinsics = NULL;
385 vec_append(intrin->intrinsics, sizeof(intrinsics)/sizeof(*intrinsics), intrinsics);
390 void intrin_cleanup(intrin_t *intrin) {
391 vec_free(intrin->intrinsics);
395 ast_expression *intrin_func(intrin_t *intrin, const char *name) {
399 /* try current first */
400 if ((find = (void*)parser_find_global(intrin->parser, name)) && ((ast_value*)find)->expression.vtype == TYPE_FUNCTION)
401 for (i = 0; i < vec_size(intrin->parser->functions); ++i)
402 if (((ast_value*)find)->name && !strcmp(intrin->parser->functions[i]->name, ((ast_value*)find)->name) && intrin->parser->functions[i]->builtin < 0)
403 return (ast_expression*)find;
404 /* try name second */
405 for (i = 0; i < vec_size(intrin->intrinsics); i++)
406 if (!strcmp(intrin->intrinsics[i].name, name))
407 return intrin->intrinsics[i].intrin(intrin);
408 /* try alias third */
409 for (i = 0; i < vec_size(intrin->intrinsics); i++)
410 if (!strcmp(intrin->intrinsics[i].alias, name))
411 return intrin->intrinsics[i].intrin(intrin);
413 intrin_error(intrin, "need function: `%s` compiler depends on it", name);