Debian Codegraph @

utils/mojoshader-compiler.c

raw symbols SCIP
1 /**
2 * MojoShader; generate shader programs from bytecode of compiled
3 * Direct3D shaders.
4 *
5 * Please see the file LICENSE.txt in the source's root directory.
6 *
7 * This file written by Ryan C. Gordon.
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <assert.h>
14
15 #include "mojoshader.h"
16
17 #ifdef _WIN32
18 #define snprintf _snprintf // !!! FIXME: not a safe replacement!
19 #endif
20
21 static const char **include_paths = NULL;
22 static unsigned int include_path_count = 0;
23
24 #define MOJOSHADER_DEBUG_MALLOC 0
25
26 #if MOJOSHADER_DEBUG_MALLOC
27 static void *Malloc(int len, void *d)
28 {
29 void *ptr = malloc(len + sizeof (int));
30 int *store = (int *) ptr;
31 printf("malloc() %d bytes (%p)\n", len, ptr);
32 if (ptr == NULL) return NULL;
33 *store = len;
34 return (void *) (store + 1);
35 } // Malloc
36
37
38 static void Free(void *_ptr, void *d)
39 {
40 int *ptr = (((int *) _ptr) - 1);
41 int len = *ptr;
42 printf("free() %d bytes (%p)\n", len, ptr);
43 free(ptr);
44 } // Free
45 #else
46 #define Malloc NULL
47 #define Free NULL
48 #endif
49
50
51 static void fail(const char *err)
52 {
53 printf("%s.\n", err);
54 exit(1);
55 } // fail
56
57 static void print_unroll_attr(FILE *io, const int unroll)
58 {
59 // -1 means "unroll at compiler's discretion",
60 // -2 means user didn't specify the attribute.
61 switch (unroll)
62 {
63 case 0:
64 fprintf(io, "[loop] ");
65 break;
66 case -1:
67 fprintf(io, "[unroll] ");
68 break;
69 case -2:
70 /* no-op. */
71 break;
72 default:
73 assert(unroll > 0);
74 fprintf(io, "[unroll(%d)] ", unroll);
75 break;
76 } // case
77 } // print_unroll_attr
78
79 static void print_ast_datatype(FILE *io, const MOJOSHADER_astDataType *dt)
80 {
81 int i;
82
83 if (dt == NULL)
84 return;
85
86 switch (dt->type)
87 {
88 case MOJOSHADER_AST_DATATYPE_BOOL:
89 fprintf(io, "bool");
90 return;
91 case MOJOSHADER_AST_DATATYPE_INT:
92 fprintf(io, "int");
93 return;
94 case MOJOSHADER_AST_DATATYPE_UINT:
95 fprintf(io, "uint");
96 return;
97 case MOJOSHADER_AST_DATATYPE_FLOAT:
98 fprintf(io, "float");
99 return;
100 case MOJOSHADER_AST_DATATYPE_FLOAT_SNORM:
101 fprintf(io, "snorm float");
102 return;
103 case MOJOSHADER_AST_DATATYPE_FLOAT_UNORM:
104 fprintf(io, "unorm float");
105 return;
106 case MOJOSHADER_AST_DATATYPE_HALF:
107 fprintf(io, "half");
108 return;
109 case MOJOSHADER_AST_DATATYPE_DOUBLE:
110 fprintf(io, "double");
111 return;
112 case MOJOSHADER_AST_DATATYPE_STRING:
113 fprintf(io, "string");
114 return;
115 case MOJOSHADER_AST_DATATYPE_SAMPLER_1D:
116 fprintf(io, "sampler1D");
117 return;
118 case MOJOSHADER_AST_DATATYPE_SAMPLER_2D:
119 fprintf(io, "sampler2D");
120 return;
121 case MOJOSHADER_AST_DATATYPE_SAMPLER_3D:
122 fprintf(io, "sampler3D");
123 return;
124 case MOJOSHADER_AST_DATATYPE_SAMPLER_CUBE:
125 fprintf(io, "samplerCUBE");
126 return;
127 case MOJOSHADER_AST_DATATYPE_SAMPLER_STATE:
128 fprintf(io, "sampler_state");
129 return;
130 case MOJOSHADER_AST_DATATYPE_SAMPLER_COMPARISON_STATE:
131 fprintf(io, "SamplerComparisonState");
132 return;
133
134 case MOJOSHADER_AST_DATATYPE_STRUCT:
135 fprintf(io, "struct { ");
136 for (i = 0; i < dt->structure.member_count; i++)
137 {
138 print_ast_datatype(io, dt->structure.members[i].datatype);
139 fprintf(io, " %s; ", dt->structure.members[i].identifier);
140 } // for
141 fprintf(io, "}");
142 return;
143
144 case MOJOSHADER_AST_DATATYPE_ARRAY:
145 print_ast_datatype(io, dt->array.base);
146 if (dt->array.elements < 0)
147 fprintf(io, "[]");
148 else
149 fprintf(io, "[%d]", dt->array.elements);
150 return;
151
152 case MOJOSHADER_AST_DATATYPE_VECTOR:
153 fprintf(io, "vector<");
154 print_ast_datatype(io, dt->vector.base);
155 fprintf(io, ",%d>", dt->vector.elements);
156 return;
157
158 case MOJOSHADER_AST_DATATYPE_MATRIX:
159 fprintf(io, "matrix<");
160 print_ast_datatype(io, dt->matrix.base);
161 fprintf(io, ",%d,%d>", dt->matrix.rows, dt->matrix.columns);
162 return;
163
164 case MOJOSHADER_AST_DATATYPE_BUFFER:
165 fprintf(io, "buffer<");
166 print_ast_datatype(io, dt->buffer.base);
167 fprintf(io, ">");
168 return;
169
170 case MOJOSHADER_AST_DATATYPE_USER:
171 fprintf(io, "%s", dt->user.name);
172 return;
173
174 // this should only appear if we did semantic analysis on the AST,
175 // so we only print the return value here.
176 case MOJOSHADER_AST_DATATYPE_FUNCTION:
177 if (!dt->function.retval)
178 fprintf(io, "void");
179 else
180 print_ast_datatype(io, dt->function.retval);
181 return;
182
183 //case MOJOSHADER_AST_DATATYPE_NONE:
184 default:
185 assert(0 && "Unexpected datatype.");
186 return;
187 } // switch
188 } // print_ast_datatype
189
190 // !!! FIXME: this screws up on order of operations.
191 static void print_ast(FILE *io, const int substmt, const void *_ast)
192 {
193 const MOJOSHADER_astNode *ast = (const MOJOSHADER_astNode *) _ast;
194 const char *nl = substmt ? "" : "\n";
195 int typeint = 0;
196 static int indent = 0;
197 int isblock = 0;
198 int i;
199
200 // These _HAVE_ to be in the same order as MOJOSHADER_astNodeType!
201 static const char *binary[] =
202 {
203 ",", "*", "/", "%", "+", "-", "<<", ">>", "<", ">", "<=", ">=", "==",
204 "!=", "&", "^", "|", "&&", "||", "=", "*=", "/=", "%=", "+=", "-=",
205 "<<=", ">>=", "&=", "^=", "|="
206 };
207
208 static const char *pre_unary[] = { "++", "--", "-", "~", "!" };
209 static const char *post_unary[] = { "++", "--" };
210 static const char *simple_stmt[] = { "", "break", "continue", "discard" };
211 static const char *inpmod[] = { "", "in ", "out ", "in out ", "uniform " };
212 static const char *fnstorage[] = { "", "inline " };
213
214 static const char *interpmod[] = {
215 "", " linear", " centroid", " nointerpolation",
216 " noperspective", " sample"
217 };
218
219 if (!ast) return;
220
221 typeint = (int) ast->ast.type;
222
223 #define DO_INDENT do { \
224 if (!substmt) { for (i = 0; i < indent; i++) fprintf(io, " "); } \
225 } while (0)
226
227 switch (ast->ast.type)
228 {
229 case MOJOSHADER_AST_OP_PREINCREMENT:
230 case MOJOSHADER_AST_OP_PREDECREMENT:
231 case MOJOSHADER_AST_OP_NEGATE:
232 case MOJOSHADER_AST_OP_COMPLEMENT:
233 case MOJOSHADER_AST_OP_NOT:
234 fprintf(io, "%s", pre_unary[(typeint-MOJOSHADER_AST_OP_START_RANGE_UNARY)-1]);
235 print_ast(io, 0, ast->unary.operand);
236 break;
237
238 case MOJOSHADER_AST_OP_POSTINCREMENT:
239 case MOJOSHADER_AST_OP_POSTDECREMENT:
240 print_ast(io, 0, ast->unary.operand);
241 fprintf(io, "%s", post_unary[typeint-MOJOSHADER_AST_OP_POSTINCREMENT]);
242 break;
243
244 case MOJOSHADER_AST_OP_MULTIPLY:
245 case MOJOSHADER_AST_OP_DIVIDE:
246 case MOJOSHADER_AST_OP_MODULO:
247 case MOJOSHADER_AST_OP_ADD:
248 case MOJOSHADER_AST_OP_SUBTRACT:
249 case MOJOSHADER_AST_OP_LSHIFT:
250 case MOJOSHADER_AST_OP_RSHIFT:
251 case MOJOSHADER_AST_OP_LESSTHAN:
252 case MOJOSHADER_AST_OP_GREATERTHAN:
253 case MOJOSHADER_AST_OP_LESSTHANOREQUAL:
254 case MOJOSHADER_AST_OP_GREATERTHANOREQUAL:
255 case MOJOSHADER_AST_OP_EQUAL:
256 case MOJOSHADER_AST_OP_NOTEQUAL:
257 case MOJOSHADER_AST_OP_BINARYAND:
258 case MOJOSHADER_AST_OP_BINARYXOR:
259 case MOJOSHADER_AST_OP_BINARYOR:
260 case MOJOSHADER_AST_OP_LOGICALAND:
261 case MOJOSHADER_AST_OP_LOGICALOR:
262 case MOJOSHADER_AST_OP_ASSIGN:
263 case MOJOSHADER_AST_OP_MULASSIGN:
264 case MOJOSHADER_AST_OP_DIVASSIGN:
265 case MOJOSHADER_AST_OP_MODASSIGN:
266 case MOJOSHADER_AST_OP_ADDASSIGN:
267 case MOJOSHADER_AST_OP_SUBASSIGN:
268 case MOJOSHADER_AST_OP_LSHIFTASSIGN:
269 case MOJOSHADER_AST_OP_RSHIFTASSIGN:
270 case MOJOSHADER_AST_OP_ANDASSIGN:
271 case MOJOSHADER_AST_OP_XORASSIGN:
272 case MOJOSHADER_AST_OP_ORASSIGN:
273 case MOJOSHADER_AST_OP_COMMA:
274 print_ast(io, 0, ast->binary.left);
275 if (ast->ast.type != MOJOSHADER_AST_OP_COMMA)
276 fprintf(io, " "); // no space before the comma.
277 fprintf(io, "%s ", binary[
278 (typeint - MOJOSHADER_AST_OP_START_RANGE_BINARY) - 1]);
279 print_ast(io, 0, ast->binary.right);
280 break;
281
282 case MOJOSHADER_AST_OP_DEREF_ARRAY:
283 print_ast(io, 0, ast->binary.left);
284 fprintf(io, "[");
285 print_ast(io, 0, ast->binary.right);
286 fprintf(io, "]");
287 break;
288
289 case MOJOSHADER_AST_OP_DEREF_STRUCT:
290 print_ast(io, 0, ast->derefstruct.identifier);
291 fprintf(io, ".");
292 fprintf(io, "%s", ast->derefstruct.member);
293 break;
294
295 case MOJOSHADER_AST_OP_CONDITIONAL:
296 print_ast(io, 0, ast->ternary.left);
297 fprintf(io, " ? ");
298 print_ast(io, 0, ast->ternary.center);
299 fprintf(io, " : ");
300 print_ast(io, 0, ast->ternary.right);
301 break;
302
303 case MOJOSHADER_AST_OP_IDENTIFIER:
304 fprintf(io, "%s", ast->identifier.identifier);
305 break;
306
307 case MOJOSHADER_AST_OP_INT_LITERAL:
308 fprintf(io, "%d", ast->intliteral.value);
309 break;
310
311 case MOJOSHADER_AST_OP_FLOAT_LITERAL:
312 {
313 const float f = ast->floatliteral.value;
314 const long long flr = (long long) f;
315 if (((float) flr) == f)
316 fprintf(io, "%lld.0", flr);
317 else
318 fprintf(io, "%.16g", f);
319 break;
320 } // case
321
322 case MOJOSHADER_AST_OP_STRING_LITERAL:
323 fprintf(io, "\"%s\"", ast->stringliteral.string);
324 break;
325
326 case MOJOSHADER_AST_OP_BOOLEAN_LITERAL:
327 fprintf(io, "%s", ast->boolliteral.value ? "true" : "false");
328 break;
329
330 case MOJOSHADER_AST_ARGUMENTS:
331 print_ast(io, 0, ast->arguments.argument);
332 if (ast->arguments.next != NULL)
333 {
334 fprintf(io, ", ");
335 print_ast(io, 0, ast->arguments.next);
336 } // if
337 break;
338
339 case MOJOSHADER_AST_OP_CALLFUNC:
340 print_ast(io, 0, ast->callfunc.identifier);
341 fprintf(io, "(");
342 print_ast(io, 0, ast->callfunc.args);
343 fprintf(io, ")");
344 break;
345
346 case MOJOSHADER_AST_OP_CONSTRUCTOR:
347 print_ast_datatype(io, ast->constructor.datatype);
348 fprintf(io, "(");
349 print_ast(io, 0, ast->constructor.args);
350 fprintf(io, ")");
351 break;
352
353 case MOJOSHADER_AST_OP_CAST:
354 fprintf(io, "(");
355 print_ast_datatype(io, ast->cast.datatype);
356 fprintf(io, ") (");
357 print_ast(io, 0, ast->cast.operand);
358 fprintf(io, ")");
359 break;
360
361 case MOJOSHADER_AST_STATEMENT_EXPRESSION:
362 DO_INDENT;
363 print_ast(io, 0, ast->exprstmt.expr); // !!! FIXME: This is named badly...
364 fprintf(io, ";%s", nl);
365 print_ast(io, 0, ast->exprstmt.next);
366 break;
367
368 case MOJOSHADER_AST_STATEMENT_IF:
369 DO_INDENT;
370 fprintf(io, "if (");
371 print_ast(io, 0, ast->ifstmt.expr);
372 fprintf(io, ")\n");
373 isblock = ast->ifstmt.statement->ast.type == MOJOSHADER_AST_STATEMENT_BLOCK;
374 if (!isblock) indent++;
375 print_ast(io, 0, ast->ifstmt.statement);
376 if (!isblock) indent--;
377 print_ast(io, 0, ast->ifstmt.next);
378 break;
379
380 case MOJOSHADER_AST_STATEMENT_TYPEDEF:
381 DO_INDENT;
382 print_ast(io, 1, ast->typedefstmt.type_info);
383 fprintf(io, "%s", nl);
384 print_ast(io, 0, ast->typedefstmt.next);
385 break;
386
387 case MOJOSHADER_AST_STATEMENT_SWITCH:
388 DO_INDENT;
389 switch ( ast->switchstmt.attributes )
390 {
391 case MOJOSHADER_AST_SWITCHATTR_NONE: break;
392 case MOJOSHADER_AST_SWITCHATTR_FLATTEN: fprintf(io, "[flatten] "); break;
393 case MOJOSHADER_AST_SWITCHATTR_BRANCH: fprintf(io, "[branch] "); break;
394 case MOJOSHADER_AST_SWITCHATTR_FORCECASE: fprintf(io, "[forcecase] "); break;
395 case MOJOSHADER_AST_SWITCHATTR_CALL: fprintf(io, "[call] "); break;
396 } // switch
397
398 fprintf(io, "switch (");
399 print_ast(io, 0, ast->switchstmt.expr);
400 fprintf(io, ")\n");
401 DO_INDENT;
402 fprintf(io, "{\n");
403 indent++;
404 print_ast(io, 0, ast->switchstmt.cases);
405 indent--;
406 fprintf(io, "\n");
407 DO_INDENT;
408 fprintf(io, "}\n");
409 print_ast(io, 0, ast->switchstmt.next);
410 break;
411
412 case MOJOSHADER_AST_SWITCH_CASE:
413 DO_INDENT;
414 fprintf(io, "case ");
415 print_ast(io, 0, ast->cases.expr);
416 fprintf(io, ":\n");
417 isblock = ast->cases.statement->ast.type == MOJOSHADER_AST_STATEMENT_BLOCK;
418 if (!isblock) indent++;
419 print_ast(io, 0, ast->cases.statement);
420 if (!isblock) indent--;
421 print_ast(io, 0, ast->cases.next);
422 break;
423
424 case MOJOSHADER_AST_STATEMENT_STRUCT:
425 DO_INDENT;
426 print_ast(io, 0, ast->structstmt.struct_info);
427 fprintf(io, ";%s%s", nl, nl); // always space these out.
428 print_ast(io, 0, ast->structstmt.next);
429 break;
430
431 case MOJOSHADER_AST_STATEMENT_VARDECL:
432 DO_INDENT;
433 print_ast(io, 1, ast->vardeclstmt.declaration);
434 fprintf(io, ";%s", nl);
435 print_ast(io, 0, ast->vardeclstmt.next);
436 break;
437
438 case MOJOSHADER_AST_STATEMENT_BLOCK:
439 DO_INDENT;
440 fprintf(io, "{\n");
441 indent++;
442 print_ast(io, 0, ast->blockstmt.statements);
443 indent--;
444 DO_INDENT;
445 fprintf(io, "}\n");
446 print_ast(io, 0, ast->blockstmt.next);
447 break;
448
449 case MOJOSHADER_AST_STATEMENT_FOR:
450 DO_INDENT;
451 print_unroll_attr(io, ast->forstmt.unroll);
452 fprintf(io, "for (");
453 print_ast(io, 1, ast->forstmt.var_decl);
454 print_ast(io, 1, ast->forstmt.initializer);
455 fprintf(io, "; ");
456 print_ast(io, 1, ast->forstmt.looptest);
457 fprintf(io, "; ");
458 print_ast(io, 1, ast->forstmt.counter);
459
460 fprintf(io, ")\n");
461 isblock = ast->forstmt.statement->ast.type == MOJOSHADER_AST_STATEMENT_BLOCK;
462 if (!isblock) indent++;
463 print_ast(io, 0, ast->forstmt.statement);
464 if (!isblock) indent--;
465
466 print_ast(io, 0, ast->forstmt.next);
467 break;
468
469 case MOJOSHADER_AST_STATEMENT_DO:
470 DO_INDENT;
471 print_unroll_attr(io, ast->dostmt.unroll);
472 fprintf(io, "do\n");
473
474 isblock = ast->dostmt.statement->ast.type == MOJOSHADER_AST_STATEMENT_BLOCK;
475 if (!isblock) indent++;
476 print_ast(io, 0, ast->dostmt.statement);
477 if (!isblock) indent--;
478
479 DO_INDENT;
480 fprintf(io, "while (");
481 print_ast(io, 0, ast->dostmt.expr);
482 fprintf(io, ");\n");
483
484 print_ast(io, 0, ast->dostmt.next);
485 break;
486
487 case MOJOSHADER_AST_STATEMENT_WHILE:
488 DO_INDENT;
489 print_unroll_attr(io, ast->whilestmt.unroll);
490 fprintf(io, "while (");
491 print_ast(io, 0, ast->whilestmt.expr);
492 fprintf(io, ")\n");
493
494 isblock = ast->whilestmt.statement->ast.type == MOJOSHADER_AST_STATEMENT_BLOCK;
495 if (!isblock) indent++;
496 print_ast(io, 0, ast->whilestmt.statement);
497 if (!isblock) indent--;
498
499 print_ast(io, 0, ast->whilestmt.next);
500 break;
501
502 case MOJOSHADER_AST_STATEMENT_RETURN:
503 DO_INDENT;
504 fprintf(io, "return");
505 if (ast->returnstmt.expr)
506 {
507 fprintf(io, " ");
508 print_ast(io, 0, ast->returnstmt.expr);
509 } // if
510 fprintf(io, ";%s", nl);
511 print_ast(io, 0, ast->returnstmt.next);
512 break;
513
514 case MOJOSHADER_AST_STATEMENT_EMPTY:
515 case MOJOSHADER_AST_STATEMENT_BREAK:
516 case MOJOSHADER_AST_STATEMENT_CONTINUE:
517 case MOJOSHADER_AST_STATEMENT_DISCARD:
518 DO_INDENT;
519 fprintf(io, "%s;%s",
520 simple_stmt[(typeint-MOJOSHADER_AST_STATEMENT_START_RANGE)-1],
521 nl);
522 print_ast(io, 0, ast->stmt.next);
523 break;
524
525 case MOJOSHADER_AST_COMPUNIT_FUNCTION:
526 DO_INDENT;
527 print_ast(io, 0, ast->funcunit.declaration);
528 if (ast->funcunit.definition == NULL)
529 fprintf(io, ";%s", nl);
530 else
531 {
532 fprintf(io, "%s", nl);
533 print_ast(io, 0, ast->funcunit.definition);
534 fprintf(io, "%s", nl);
535 } // else
536 print_ast(io, 0, ast->funcunit.next);
537 break;
538
539 case MOJOSHADER_AST_COMPUNIT_TYPEDEF:
540 DO_INDENT;
541 print_ast(io, 0, ast->typedefunit.type_info);
542 fprintf(io, "%s", nl);
543 print_ast(io, 0, ast->typedefunit.next);
544 break;
545
546 case MOJOSHADER_AST_COMPUNIT_STRUCT:
547 DO_INDENT;
548 print_ast(io, 0, ast->structunit.struct_info);
549 fprintf(io, ";%s%s", nl, nl); // always space these out.
550 print_ast(io, 0, ast->structunit.next);
551 break;
552
553 case MOJOSHADER_AST_COMPUNIT_VARIABLE:
554 DO_INDENT;
555 print_ast(io, 1, ast->varunit.declaration);
556 fprintf(io, ";%s", nl);
557 if (ast->varunit.next &&
558 ast->varunit.next->ast.type!=MOJOSHADER_AST_COMPUNIT_VARIABLE)
559 {
560 fprintf(io, "%s", nl); // group vars together, and space out other things.
561 } // if
562 print_ast(io, 0, ast->varunit.next);
563 break;
564
565 case MOJOSHADER_AST_SCALAR_OR_ARRAY:
566 fprintf(io, "%s", ast->soa.identifier);
567 if (ast->soa.isarray)
568 {
569 fprintf(io, "[");
570 print_ast(io, 0, ast->soa.dimension);
571 fprintf(io, "]");
572 } // if
573 break;
574
575 case MOJOSHADER_AST_TYPEDEF:
576 DO_INDENT;
577 fprintf(io, "typedef %s", ast->typdef.isconst ? "const " : "");
578 print_ast_datatype(io, ast->typdef.datatype);
579 fprintf(io, " ");
580 print_ast(io, 0, ast->typdef.details);
581 fprintf(io, ";%s", nl);
582 break;
583
584 case MOJOSHADER_AST_FUNCTION_PARAMS:
585 fprintf(io, "%s", inpmod[(int) ast->params.input_modifier]);
586 print_ast_datatype(io, ast->params.datatype);
587 fprintf(io, " %s", ast->params.identifier);
588 if (ast->params.semantic)
589 fprintf(io, " : %s", ast->params.semantic);
590 fprintf(io, "%s", interpmod[(int) ast->params.interpolation_modifier]);
591
592 if (ast->params.initializer)
593 {
594 fprintf(io, " = ");
595 print_ast(io, 0, ast->params.initializer);
596 } // if
597
598 if (ast->params.next)
599 {
600 fprintf(io, ", ");
601 print_ast(io, 0, ast->params.next);
602 } // if
603 break;
604
605 case MOJOSHADER_AST_FUNCTION_SIGNATURE:
606 fprintf(io, "%s", fnstorage[(int) ast->funcsig.storage_class]);
607 if (ast->funcsig.datatype)
608 print_ast_datatype(io, ast->funcsig.datatype);
609 else
610 fprintf(io, "void");
611 fprintf(io, " %s(", ast->funcsig.identifier);
612 print_ast(io, 0, ast->funcsig.params);
613 fprintf(io, ")");
614 if (ast->funcsig.semantic)
615 fprintf(io, " : %s", ast->funcsig.semantic);
616 break;
617
618 case MOJOSHADER_AST_STRUCT_DECLARATION:
619 fprintf(io, "struct %s\n", ast->structdecl.name);
620 DO_INDENT;
621 fprintf(io, "{\n");
622 indent++;
623 print_ast(io, 0, ast->structdecl.members);
624 indent--;
625 DO_INDENT;
626 fprintf(io, "}");
627 break;
628
629 case MOJOSHADER_AST_STRUCT_MEMBER:
630 DO_INDENT;
631 fprintf(io, "%s", interpmod[(int)ast->structmembers.interpolation_mod]);
632 print_ast_datatype(io, ast->structmembers.datatype);
633 fprintf(io, " ");
634 print_ast(io, 0, ast->structmembers.details);
635 if (ast->structmembers.semantic)
636 fprintf(io, " : %s", ast->structmembers.semantic);
637 fprintf(io, ";%s", nl);
638 print_ast(io, 0, ast->structmembers.next);
639 break;
640
641 case MOJOSHADER_AST_VARIABLE_DECLARATION:
642 DO_INDENT;
643 if (ast->vardecl.attributes & MOJOSHADER_AST_VARATTR_EXTERN)
644 fprintf(io, "extern ");
645 if (ast->vardecl.attributes & MOJOSHADER_AST_VARATTR_NOINTERPOLATION)
646 fprintf(io, "nointerpolation ");
647 if (ast->vardecl.attributes & MOJOSHADER_AST_VARATTR_SHARED)
648 fprintf(io, "shared");
649 if (ast->vardecl.attributes & MOJOSHADER_AST_VARATTR_STATIC)
650 fprintf(io, "static ");
651 if (ast->vardecl.attributes & MOJOSHADER_AST_VARATTR_UNIFORM)
652 fprintf(io, "uniform ");
653 if (ast->vardecl.attributes & MOJOSHADER_AST_VARATTR_VOLATILE)
654 fprintf(io, "nointerpolation ");
655 if (ast->vardecl.attributes & MOJOSHADER_AST_VARATTR_CONST)
656 fprintf(io, "const ");
657 if (ast->vardecl.attributes & MOJOSHADER_AST_VARATTR_ROWMAJOR)
658 fprintf(io, "rowmajor ");
659 if (ast->vardecl.attributes & MOJOSHADER_AST_VARATTR_COLUMNMAJOR)
660 fprintf(io, "columnmajor ");
661
662 if (ast->vardecl.datatype)
663 print_ast_datatype(io, ast->vardecl.datatype);
664 else
665 print_ast(io, 0, ast->vardecl.anonymous_datatype);
666 fprintf(io, " ");
667 print_ast(io, 0, ast->vardecl.details);
668 if (ast->vardecl.semantic)
669 fprintf(io, " : %s", ast->vardecl.semantic);
670 if (ast->vardecl.annotations)
671 {
672 fprintf(io, " ");
673 print_ast(io, 0, ast->vardecl.annotations);
674 } // if
675 if (ast->vardecl.initializer != NULL)
676 {
677 fprintf(io, " = ");
678 print_ast(io, 0, ast->vardecl.initializer);
679 } // if
680 print_ast(io, 0, ast->vardecl.lowlevel);
681
682 if (ast->vardecl.next == NULL)
683 fprintf(io, "%s", nl);
684 else
685 {
686 const int attr = ast->vardecl.next->attributes;
687 fprintf(io, ", ");
688 ast->vardecl.next->attributes = 0;
689 print_ast(io, 1, ast->vardecl.next);
690 ast->vardecl.next->attributes = attr;
691 } // if
692 break;
693
694 case MOJOSHADER_AST_PACK_OFFSET:
695 fprintf(io, " : packoffset(%s%s%s)", ast->packoffset.ident1,
696 ast->packoffset.ident2 ? "." : "",
697 ast->packoffset.ident2 ? ast->packoffset.ident2 : "");
698 break;
699
700 case MOJOSHADER_AST_VARIABLE_LOWLEVEL:
701 print_ast(io, 0, ast->varlowlevel.packoffset);
702 if (ast->varlowlevel.register_name)
703 fprintf(io, " : register(%s)", ast->varlowlevel.register_name);
704 break;
705
706 case MOJOSHADER_AST_ANNOTATION:
707 {
708 const MOJOSHADER_astAnnotations *a = &ast->annotations;
709 fprintf(io, "<");
710 while (a)
711 {
712 fprintf(io, " ");
713 print_ast_datatype(io, a->datatype);
714 if (a->initializer != NULL)
715 {
716 fprintf(io, " = ");
717 print_ast(io, 0, a->initializer);
718 } // if
719 if (a->next)
720 fprintf(io, ",");
721 a = a->next;
722 } // while
723 fprintf(io, " >");
724 break;
725 } // case
726
727 default:
728 assert(0 && "unexpected type");
729 break;
730 } // switch
731
732 #undef DO_INDENT
733 } // print_ast
734
735
736 static int open_include(MOJOSHADER_includeType inctype, const char *fname,
737 const char *parent, const char **outdata,
738 unsigned int *outbytes, MOJOSHADER_malloc m,
739 MOJOSHADER_free f, void *d)
740 {
741 int i;
742 for (i = 0; i < include_path_count; i++)
743 {
744 const char *path = include_paths[i];
745 const size_t len = strlen(path) + strlen(fname) + 2;
746 char *buf = (char *) m(len, d);
747 if (buf == NULL)
748 return 0;
749
750 snprintf(buf, len, "%s/%s", path, fname);
751 FILE *io = fopen(buf, "rb");
752 f(buf, d);
753 if (io == NULL)
754 continue;
755
756 if (fseek(io, 0, SEEK_END) != -1)
757 {
758 const long fsize = ftell(io);
759 if ((fsize == -1) || (fseek(io, 0, SEEK_SET) == -1))
760 {
761 fclose(io);
762 return 0;
763 } // if
764
765 char *data = (char *) m(fsize, d);
766 if (data == NULL)
767 {
768 fclose(io);
769 return 0;
770 } // if
771
772 if (fread(data, fsize, 1, io) != 1)
773 {
774 f(data, d);
775 fclose(io);
776 return 0;
777 } // if
778
779 fclose(io);
780 *outdata = data;
781 *outbytes = (unsigned int) fsize;
782 return 1;
783 } // if
784 } // for
785
786 return 0;
787 } // open_include
788
789
790 static void close_include(const char *data, MOJOSHADER_malloc m,
791 MOJOSHADER_free f, void *d)
792 {
793 f((void *) data, d);
794 } // close_include
795
796
797 static int preprocess(const char *fname, const char *buf, int len,
798 const char *outfile,
799 const MOJOSHADER_preprocessorDefine *defs,
800 unsigned int defcount, FILE *io)
801 {
802 const MOJOSHADER_preprocessData *pd;
803 int retval = 0;
804
805 pd = MOJOSHADER_preprocess(fname, buf, len, defs, defcount, open_include,
806 close_include, Malloc, Free, NULL);
807
808 if (pd->error_count > 0)
809 {
810 int i;
811 for (i = 0; i < pd->error_count; i++)
812 {
813 fprintf(stderr, "%s:%d: ERROR: %s\n",
814 pd->errors[i].filename ? pd->errors[i].filename : "???",
815 pd->errors[i].error_position,
816 pd->errors[i].error);
817 } // for
818 } // if
819 else
820 {
821 if (pd->output != NULL)
822 {
823 const int len = pd->output_len;
824 if ((len) && (fwrite(pd->output, len, 1, io) != 1))
825 printf(" ... fwrite('%s') failed.\n", outfile);
826 else if ((outfile != NULL) && (fclose(io) == EOF))
827 printf(" ... fclose('%s') failed.\n", outfile);
828 else
829 retval = 1;
830 } // if
831 } // else
832 MOJOSHADER_freePreprocessData(pd);
833
834 return retval;
835 } // preprocess
836
837
838 static int assemble(const char *fname, const char *buf, int len,
839 const char *outfile,
840 const MOJOSHADER_preprocessorDefine *defs,
841 unsigned int defcount, FILE *io)
842 {
843 const MOJOSHADER_parseData *pd;
844 int retval = 0;
845
846 pd = MOJOSHADER_assemble(fname, buf, len, NULL, 0, NULL, 0,
847 defs, defcount, open_include, close_include,
848 Malloc, Free, NULL);
849
850 if (pd->error_count > 0)
851 {
852 int i;
853 for (i = 0; i < pd->error_count; i++)
854 {
855 fprintf(stderr, "%s:%d: ERROR: %s\n",
856 pd->errors[i].filename ? pd->errors[i].filename : "???",
857 pd->errors[i].error_position,
858 pd->errors[i].error);
859 } // for
860 } // if
861 else
862 {
863 if (pd->output != NULL)
864 {
865 const int len = pd->output_len;
866 if ((len) && (fwrite(pd->output, len, 1, io) != 1))
867 printf(" ... fwrite('%s') failed.\n", outfile);
868 else if ((outfile != NULL) && (fclose(io) == EOF))
869 printf(" ... fclose('%s') failed.\n", outfile);
870 else
871 retval = 1;
872 } // if
873 } // else
874 MOJOSHADER_freeParseData(pd);
875
876 return retval;
877 } // assemble
878
879 static int ast(const char *fname, const char *buf, int len,
880 const char *outfile, const MOJOSHADER_preprocessorDefine *defs,
881 unsigned int defcount, FILE *io)
882 {
883 const MOJOSHADER_astData *ad;
884 int retval = 0;
885
886 ad = MOJOSHADER_parseAst(MOJOSHADER_SRC_PROFILE_HLSL_PS_1_1, // !!! FIXME
887 fname, buf, len, defs, defcount,
888 open_include, close_include, Malloc, Free, NULL);
889
890 if (ad->error_count > 0)
891 {
892 int i;
893 for (i = 0; i < ad->error_count; i++)
894 {
895 fprintf(stderr, "%s:%d: ERROR: %s\n",
896 ad->errors[i].filename ? ad->errors[i].filename : "???",
897 ad->errors[i].error_position,
898 ad->errors[i].error);
899 } // for
900 } // if
901 else
902 {
903 print_ast(io, 0, ad->ast);
904 if ((outfile != NULL) && (fclose(io) == EOF))
905 printf(" ... fclose('%s') failed.\n", outfile);
906 else
907 retval = 1;
908 } // else
909 MOJOSHADER_freeAstData(ad);
910
911 return retval;
912 } // ast
913
914 static int compile(const char *fname, const char *buf, int len,
915 const char *outfile,
916 const MOJOSHADER_preprocessorDefine *defs,
917 unsigned int defcount, FILE *io)
918 {
919 // !!! FIXME: write me.
920 //const MOJOSHADER_parseData *pd;
921 //int retval = 0;
922
923 MOJOSHADER_compile(MOJOSHADER_SRC_PROFILE_HLSL_PS_1_1, // !!! FIXME
924 fname, buf, len, defs, defcount,
925 open_include, close_include,
926 Malloc, Free, NULL);
927 return 1;
928 } // compile
929
930 typedef enum
931 {
932 ACTION_UNKNOWN,
933 ACTION_VERSION,
934 ACTION_PREPROCESS,
935 ACTION_ASSEMBLE,
936 ACTION_AST,
937 ACTION_COMPILE,
938 } Action;
939
940
941 int main(int argc, char **argv)
942 {
943 Action action = ACTION_UNKNOWN;
944 int retval = 1;
945 const char *infile = NULL;
946 const char *outfile = NULL;
947 int i;
948
949 MOJOSHADER_preprocessorDefine *defs = NULL;
950 unsigned int defcount = 0;
951
952 include_paths = (const char **) malloc(sizeof (char *));
953 include_paths[0] = ".";
954 include_path_count = 1;
955
956 // !!! FIXME: clean this up.
957 for (i = 1; i < argc; i++)
958 {
959 const char *arg = argv[i];
960
961 if (strcmp(arg, "-P") == 0)
962 {
963 if ((action != ACTION_UNKNOWN) && (action != ACTION_PREPROCESS))
964 fail("Multiple actions specified");
965 action = ACTION_PREPROCESS;
966 } // if
967
968 else if (strcmp(arg, "-A") == 0)
969 {
970 if ((action != ACTION_UNKNOWN) && (action != ACTION_ASSEMBLE))
971 fail("Multiple actions specified");
972 action = ACTION_ASSEMBLE;
973 } // else if
974
975 else if (strcmp(arg, "-T") == 0)
976 {
977 if ((action != ACTION_UNKNOWN) && (action != ACTION_AST))
978 fail("Multiple actions specified");
979 action = ACTION_AST;
980 } // else if
981
982 else if (strcmp(arg, "-C") == 0)
983 {
984 if ((action != ACTION_UNKNOWN) && (action != ACTION_COMPILE))
985 fail("Multiple actions specified");
986 action = ACTION_COMPILE;
987 } // else if
988
989 else if ((strcmp(arg, "-V") == 0) || (strcmp(arg, "--version") == 0))
990 {
991 if ((action != ACTION_UNKNOWN) && (action != ACTION_VERSION))
992 fail("Multiple actions specified");
993 action = ACTION_VERSION;
994 } // else if
995
996 else if (strcmp(arg, "-o") == 0)
997 {
998 if (outfile != NULL)
999 fail("multiple output files specified");
1000
1001 arg = argv[++i];
1002 if (arg == NULL)
1003 fail("no filename after '-o'");
1004 outfile = arg;
1005 } // if
1006
1007 else if (strcmp(arg, "-I") == 0)
1008 {
1009 arg = argv[++i];
1010 if (arg == NULL)
1011 fail("no path after '-I'");
1012
1013 include_paths = (const char **) realloc(include_paths,
1014 (include_path_count+1) * sizeof (char *));
1015 include_paths[include_path_count] = arg;
1016 include_path_count++;
1017 } // if
1018
1019 else if (strncmp(arg, "-D", 2) == 0)
1020 {
1021 arg += 2;
1022 char *ident = strdup(arg);
1023 char *ptr = strchr(ident, '=');
1024 const char *val = "";
1025 if (ptr)
1026 {
1027 *ptr = '\0';
1028 val = ptr+1;
1029 } // if
1030
1031 defs = (MOJOSHADER_preprocessorDefine *) realloc(defs,
1032 (defcount+1) * sizeof (MOJOSHADER_preprocessorDefine));
1033 defs[defcount].identifier = ident;
1034 defs[defcount].definition = val;
1035 defcount++;
1036 } // else if
1037
1038 else
1039 {
1040 if (infile != NULL)
1041 fail("multiple input files specified");
1042 infile = arg;
1043 } // else
1044 } // for
1045
1046 if (action == ACTION_UNKNOWN)
1047 action = ACTION_ASSEMBLE;
1048
1049 if (action == ACTION_VERSION)
1050 {
1051 printf("mojoshader-compiler, changeset %s\n", MOJOSHADER_CHANGESET);
1052 return 0;
1053 } // if
1054
1055 if (infile == NULL)
1056 fail("no input file specified");
1057
1058 FILE *io = fopen(infile, "rb");
1059 if (io == NULL)
1060 fail("failed to open input file");
1061
1062 fseek(io, 0, SEEK_END);
1063 long fsize = ftell(io);
1064 fseek(io, 0, SEEK_SET);
1065 if (fsize == -1)
1066 fsize = 1000000;
1067 char *buf = (char *) malloc(fsize);
1068 const int rc = fread(buf, 1, fsize, io);
1069 fclose(io);
1070 if (rc == EOF)
1071 fail("failed to read input file");
1072
1073 FILE *outio = outfile ? fopen(outfile, "wb") : stdout;
1074 if (outio == NULL)
1075 fail("failed to open output file");
1076
1077
1078 if (action == ACTION_PREPROCESS)
1079 retval = (!preprocess(infile, buf, rc, outfile, defs, defcount, outio));
1080 else if (action == ACTION_ASSEMBLE)
1081 retval = (!assemble(infile, buf, rc, outfile, defs, defcount, outio));
1082 else if (action == ACTION_AST)
1083 retval = (!ast(infile, buf, rc, outfile, defs, defcount, outio));
1084 else if (action == ACTION_COMPILE)
1085 retval = (!compile(infile, buf, rc, outfile, defs, defcount, outio));
1086
1087 if ((retval != 0) && (outfile != NULL))
1088 remove(outfile);
1089
1090 free(buf);
1091
1092 for (i = 0; i < defcount; i++)
1093 free((void *) defs[i].identifier);
1094 free(defs);
1095
1096 free(include_paths);
1097
1098 return retval;
1099 } // main
1100
1101 // end of mojoshader-compiler.c ...
1102