2 * Mesa 3-D graphics library
4 * Copyright (C) 2005-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \file slang_preprocess.c
31 #include "main/imports.h"
32 #include "shader/grammar/grammar_mesa.h"
33 #include "slang_preprocess.h"
35 LONGSTRING
static const char *slang_pp_directives_syn
=
36 #include "library/slang_pp_directives_syn.h"
39 LONGSTRING
static const char *slang_pp_expression_syn
=
40 #include "library/slang_pp_expression_syn.h"
43 LONGSTRING
static const char *slang_pp_version_syn
=
44 #include "library/slang_pp_version_syn.h"
48 grammar_error_to_log (slang_info_log
*log
)
53 grammar_get_last_error ((byte
*) (buf
), sizeof (buf
), &pos
);
55 _mesa_snprintf(buf
, sizeof(buf
), "Preprocessor error");
57 slang_info_log_error (log
, buf
);
61 _slang_preprocess_version (const char *text
, GLuint
*version
, GLuint
*eaten
, slang_info_log
*log
)
67 id
= grammar_load_from_text ((const byte
*) (slang_pp_version_syn
));
69 grammar_error_to_log (log
);
73 if (!grammar_fast_check (id
, (const byte
*) (text
), &prod
, &size
, 8)) {
74 grammar_error_to_log (log
);
79 /* there can be multiple #version directives - grab the last one */
81 *version
= (GLuint
) (I
[0]) + (GLuint
) (I
[1]) * 100;
82 *eaten
= (GLuint
) (I
[2]) + ((GLuint
) (I
[3]) << 8) + ((GLuint
) (I
[4]) << 16) + ((GLuint
) (I
[5]) << 24);
85 grammar_alloc_free (prod
);
90 * The preprocessor does the following work.
91 * 1. Remove comments. Each comment block is replaced with a single space and if the
92 * block contains new-lines, they are preserved. This ensures that line numbers
93 * stay the same and if a comment block delimits two tokens, the are delitmited
94 * by the space after comment removal.
95 * 2. Remove preprocessor directives from the source string, checking their syntax and
96 * executing them if appropriate. Again, new-lines are preserved.
98 * 4. Tokenize the source string by ensuring there is at least one space between every
99 * two adjacent tokens.
102 #define PP_ANNOTATE 0
105 pp_annotate (slang_string
*output
, const char *fmt
, ...)
112 _mesa_vsprintf (buffer
, fmt
, va
);
114 slang_string_pushs (output
, buffer
, _mesa_strlen (buffer
));
122 * The expression is executed on a fixed-sized stack. The PUSH macro makes a runtime
123 * check if the stack is not overflown by too complex expressions. In that situation the
124 * GLSL preprocessor should report internal compiler error.
125 * The BINARYDIV makes a runtime check if the divider is not 0. If it is, it reports
129 #define EXECUTION_STACK_SIZE 1024
134 slang_info_log_error (elog, "internal compiler error: preprocessor execution stack overflow.");\
142 assert (sp < EXECUTION_STACK_SIZE);\
154 #define BINARYDIV(op)\
160 slang_info_log_error (elog, "division by zero in preprocessor expression.");\
175 #define OP_LOGICALOR 2
176 #define OP_LOGICALAND 3
181 #define OP_NOTEQUAL 8
182 #define OP_LESSEQUAL 9
183 #define OP_GREATEREQUAL 10
185 #define OP_GREATER 12
186 #define OP_LEFTSHIFT 13
187 #define OP_RIGHTSHIFT 14
189 #define OP_SUBTRACT 16
190 #define OP_MULTIPLY 17
192 #define OP_MODULUS 19
196 #define OP_COMPLEMENT 23
199 execute_expression (slang_string
*output
, const byte
*code
, GLuint
*pi
, GLint
*result
,
200 slang_info_log
*elog
)
203 GLint stack
[EXECUTION_STACK_SIZE
];
204 GLuint sp
= EXECUTION_STACK_SIZE
;
206 while (code
[i
] != OP_END
) {
210 PUSH(_mesa_atoi ((const char *) (&code
[i
])));
211 i
+= _mesa_strlen ((const char *) (&code
[i
])) + 1;
237 case OP_GREATEREQUAL
:
284 /* Write-back the index skipping the OP_END. */
287 /* There should be exactly one value left on the stack. This is our result. */
289 pp_annotate (output
, "%d ", *result
);
290 assert (sp
== EXECUTION_STACK_SIZE
);
295 * Function execute_expressions() executes up to 2 expressions. The second expression is there
296 * for the #line directive which takes 1 or 2 expressions that indicate line and file numbers.
297 * If it fails, it returns 0. If it succeeds, it returns the number of executed expressions.
301 #define EXP_EXPRESSION 1
304 execute_expressions (slang_string
*output
, grammar eid
, const byte
*expr
, GLint results
[2],
305 slang_info_log
*elog
)
309 GLuint size
, count
= 0;
311 success
= grammar_fast_check (eid
, expr
, &code
, &size
, 64);
315 while (code
[i
++] == EXP_EXPRESSION
) {
318 if (!execute_expression (output
, code
, &i
, &results
[count
], elog
)) {
324 grammar_alloc_free (code
);
327 slang_info_log_error (elog
, "syntax error in preprocessor expression.");\
333 * The pp_symbol structure is used to hold macro definitions and macro formal parameters. The
334 * pp_symbols strcture is a collection of pp_symbol. It is used both for storing macro formal
335 * parameters and all global macro definitions. Making this unification wastes some memory,
336 * becuse macro formal parameters don't need further lists of symbols. We lose 8 bytes per
337 * formal parameter here, but making this we can use the same code to substitute macro parameters
338 * as well as macros in the source string.
343 struct pp_symbol_
*symbols
;
348 pp_symbols_init (pp_symbols
*self
)
350 self
->symbols
= NULL
;
355 pp_symbols_free (pp_symbols
*);
357 typedef struct pp_symbol_
360 slang_string replacement
;
361 pp_symbols parameters
;
365 pp_symbol_init (pp_symbol
*self
)
367 slang_string_init (&self
->name
);
368 slang_string_init (&self
->replacement
);
369 pp_symbols_init (&self
->parameters
);
373 pp_symbol_free (pp_symbol
*self
)
375 slang_string_free (&self
->name
);
376 slang_string_free (&self
->replacement
);
377 pp_symbols_free (&self
->parameters
);
381 pp_symbol_reset (pp_symbol
*self
)
383 /* Leave symbol name intact. */
384 slang_string_reset (&self
->replacement
);
385 pp_symbols_free (&self
->parameters
);
386 pp_symbols_init (&self
->parameters
);
390 pp_symbols_free (pp_symbols
*self
)
394 for (i
= 0; i
< self
->count
; i
++)
395 pp_symbol_free (&self
->symbols
[i
]);
396 _mesa_free (self
->symbols
);
400 pp_symbols_push (pp_symbols
*self
)
402 self
->symbols
= (pp_symbol
*) (_mesa_realloc (self
->symbols
, self
->count
* sizeof (pp_symbol
),
403 (self
->count
+ 1) * sizeof (pp_symbol
)));
404 if (self
->symbols
== NULL
)
406 pp_symbol_init (&self
->symbols
[self
->count
]);
407 return &self
->symbols
[self
->count
++];
411 pp_symbols_erase (pp_symbols
*self
, pp_symbol
*symbol
)
413 assert (symbol
>= self
->symbols
&& symbol
< self
->symbols
+ self
->count
);
416 pp_symbol_free (symbol
);
417 if (symbol
< self
->symbols
+ self
->count
)
418 _mesa_memcpy (symbol
, symbol
+ 1, sizeof (pp_symbol
) * (self
->symbols
+ self
->count
- symbol
));
419 self
->symbols
= (pp_symbol
*) (_mesa_realloc (self
->symbols
, (self
->count
+ 1) * sizeof (pp_symbol
),
420 self
->count
* sizeof (pp_symbol
)));
421 return self
->symbols
!= NULL
;
425 pp_symbols_find (pp_symbols
*self
, const char *name
)
429 for (i
= 0; i
< self
->count
; i
++)
430 if (_mesa_strcmp (name
, slang_string_cstr (&self
->symbols
[i
].name
)) == 0)
431 return &self
->symbols
[i
];
436 * The condition context of a single #if/#else/#endif level. Those can be nested, so there
437 * is a stack of condition contexts.
438 * There is a special global context on the bottom of the stack. It is there to simplify
444 GLboolean current
; /* The condition value of this level. */
445 GLboolean effective
; /* The effective product of current condition, outer level conditions
446 * and position within #if-#else-#endif sections. */
447 GLboolean else_allowed
; /* TRUE if in #if-#else section, FALSE if in #else-#endif section
448 * and for global context. */
449 GLboolean endif_required
; /* FALSE for global context only. */
452 /* Should be enuff. */
453 #define CONDITION_STACK_SIZE 64
457 pp_cond_ctx stack
[CONDITION_STACK_SIZE
];
462 pp_cond_stack_push (pp_cond_stack
*self
, slang_info_log
*elog
)
464 if (self
->top
== self
->stack
) {
465 slang_info_log_error (elog
, "internal compiler error: preprocessor condition stack overflow.");
473 pp_cond_stack_reevaluate (pp_cond_stack
*self
)
475 /* There must be at least 2 conditions on the stack - one global and one being evaluated. */
476 assert (self
->top
<= &self
->stack
[CONDITION_STACK_SIZE
- 2]);
478 self
->top
->effective
= self
->top
->current
&& self
->top
[1].effective
;
483 * Extension enables through #extension directive.
484 * NOTE: Currently, only enable/disable state is stored.
488 GLboolean ARB_draw_buffers
;
489 GLboolean ARB_texture_rectangle
;
494 * Disable all extensions. Called at startup and on #extension all: disable.
497 pp_ext_disable_all(pp_ext
*self
)
499 _mesa_memset(self
, 0, sizeof(self
));
504 * Called during preprocessor initialization to set the initial enable/disable
505 * state of extensions.
508 pp_ext_init(pp_ext
*self
, const struct gl_extensions
*extensions
)
510 pp_ext_disable_all (self
);
511 self
->ARB_draw_buffers
= GL_TRUE
;
512 if (extensions
->NV_texture_rectangle
)
513 self
->ARB_texture_rectangle
= GL_TRUE
;
517 * Called in response to #extension directives to enable/disable
518 * the named extension.
521 pp_ext_set(pp_ext
*self
, const char *name
, GLboolean enable
)
523 if (_mesa_strcmp (name
, "GL_ARB_draw_buffers") == 0)
524 self
->ARB_draw_buffers
= enable
;
525 else if (_mesa_strcmp (name
, "GL_ARB_texture_rectangle") == 0)
526 self
->ARB_texture_rectangle
= enable
;
534 pp_pragmas_init(struct gl_sl_pragmas
*pragmas
)
536 pragmas
->Optimize
= GL_TRUE
;
537 pragmas
->Debug
= GL_FALSE
;
542 * Called in response to #pragma. For example, "#pragma debug(on)" would
543 * call this function as pp_pragma("debug", "on").
544 * \return GL_TRUE if pragma is valid, GL_FALSE if invalid
547 pp_pragma(struct gl_sl_pragmas
*pragmas
, const char *pragma
, const char *param
)
550 printf("#pragma %s %s\n", pragma
, param
);
552 if (_mesa_strcmp(pragma
, "optimize") == 0) {
554 return GL_FALSE
; /* missing required param */
555 if (_mesa_strcmp(param
, "on") == 0) {
556 pragmas
->Optimize
= GL_TRUE
;
558 else if (_mesa_strcmp(param
, "off") == 0) {
559 pragmas
->Optimize
= GL_FALSE
;
562 return GL_FALSE
; /* invalid param */
565 else if (_mesa_strcmp(pragma
, "debug") == 0) {
567 return GL_FALSE
; /* missing required param */
568 if (_mesa_strcmp(param
, "on") == 0) {
569 pragmas
->Debug
= GL_TRUE
;
571 else if (_mesa_strcmp(param
, "off") == 0) {
572 pragmas
->Debug
= GL_FALSE
;
575 return GL_FALSE
; /* invalid param */
578 /* all other pragmas are silently ignored */
584 * The state of preprocessor: current line, file and version number, list
585 * of all defined macros and the #if/#endif context.
594 slang_info_log
*elog
;
599 pp_state_init (pp_state
*self
, slang_info_log
*elog
,
600 const struct gl_extensions
*extensions
)
609 pp_symbols_init (&self
->symbols
);
610 pp_ext_init (&self
->ext
, extensions
);
613 /* Initialize condition stack and create the global context. */
614 self
->cond
.top
= &self
->cond
.stack
[CONDITION_STACK_SIZE
- 1];
615 self
->cond
.top
->current
= GL_TRUE
;
616 self
->cond
.top
->effective
= GL_TRUE
;
617 self
->cond
.top
->else_allowed
= GL_FALSE
;
618 self
->cond
.top
->endif_required
= GL_FALSE
;
622 pp_state_free (pp_state
*self
)
624 pp_symbols_free (&self
->symbols
);
627 #define IS_FIRST_ID_CHAR(x) (((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || (x) == '_')
628 #define IS_NEXT_ID_CHAR(x) (IS_FIRST_ID_CHAR(x) || ((x) >= '0' && (x) <= '9'))
629 #define IS_WHITE(x) ((x) == ' ' || (x) == '\n')
630 #define IS_NULL(x) ((x) == '\0')
632 #define SKIP_WHITE(x) do { while (IS_WHITE(*(x))) (x)++; } while (GL_FALSE)
636 slang_string
*output
;
642 expand_defined (expand_state
*e
, slang_string
*buffer
)
644 GLboolean in_paren
= GL_FALSE
;
647 /* Parse the optional opening parenthesis. */
648 SKIP_WHITE(e
->input
);
649 if (*e
->input
== '(') {
652 SKIP_WHITE(e
->input
);
656 if (!IS_FIRST_ID_CHAR(*e
->input
)) {
657 slang_info_log_error (e
->state
->elog
,
658 "preprocess error: identifier expected after operator 'defined'.");
661 slang_string_reset (buffer
);
662 slang_string_pushc (buffer
, *e
->input
++);
663 while (IS_NEXT_ID_CHAR(*e
->input
))
664 slang_string_pushc (buffer
, *e
->input
++);
665 id
= slang_string_cstr (buffer
);
667 /* Check if the operand is defined. Output 1 if it is defined, output 0 if not. */
668 if (pp_symbols_find (&e
->state
->symbols
, id
) == NULL
)
669 slang_string_pushs (e
->output
, " 0 ", 3);
671 slang_string_pushs (e
->output
, " 1 ", 3);
673 /* Parse the closing parentehesis if the opening one was there. */
675 SKIP_WHITE(e
->input
);
676 if (*e
->input
!= ')') {
677 slang_info_log_error (e
->state
->elog
, "preprocess error: ')' expected.");
681 SKIP_WHITE(e
->input
);
687 expand (expand_state
*, pp_symbols
*);
690 expand_symbol (expand_state
*e
, pp_symbol
*symbol
)
694 /* If the macro has some parameters, we need to parse them. */
695 if (symbol
->parameters
.count
!= 0) {
698 /* Parse the opening parenthesis. */
699 SKIP_WHITE(e
->input
);
700 if (*e
->input
!= '(') {
701 slang_info_log_error (e
->state
->elog
, "preprocess error: '(' expected.");
705 SKIP_WHITE(e
->input
);
707 /* Parse macro actual parameters. This can be anything, separated by a colon.
709 for (i
= 0; i
< symbol
->parameters
.count
; i
++) {
710 GLuint nested_paren_count
= 0; /* track number of nested parentheses */
712 if (*e
->input
== ')') {
713 slang_info_log_error (e
->state
->elog
, "preprocess error: unexpected ')'.");
717 /* Eat all characters up to the comma or closing parentheses. */
718 pp_symbol_reset (&symbol
->parameters
.symbols
[i
]);
719 while (!IS_NULL(*e
->input
)) {
720 /* Exit loop only when all nested parens have been eaten. */
721 if (nested_paren_count
== 0 && (*e
->input
== ',' || *e
->input
== ')'))
724 /* Actually count nested parens here. */
725 if (*e
->input
== '(')
726 nested_paren_count
++;
727 else if (*e
->input
== ')')
728 nested_paren_count
--;
730 slang_string_pushc (&symbol
->parameters
.symbols
[i
].replacement
, *e
->input
++);
733 /* If it was not the last paremeter, skip the comma. Otherwise, skip the
734 * closing parentheses. */
735 if (i
+ 1 == symbol
->parameters
.count
) {
736 /* This is the last paremeter - skip the closing parentheses. */
737 if (*e
->input
!= ')') {
738 slang_info_log_error (e
->state
->elog
, "preprocess error: ')' expected.");
742 SKIP_WHITE(e
->input
);
745 /* Skip the separating comma. */
746 if (*e
->input
!= ',') {
747 slang_info_log_error (e
->state
->elog
, "preprocess error: ',' expected.");
751 SKIP_WHITE(e
->input
);
756 /* Expand the macro. Use its parameters as a priority symbol list to expand
757 * macro parameters correctly. */
758 es
.output
= e
->output
;
759 es
.input
= slang_string_cstr (&symbol
->replacement
);
761 slang_string_pushc (e
->output
, ' ');
762 if (!expand (&es
, &symbol
->parameters
))
764 slang_string_pushc (e
->output
, ' ');
769 * Function expand() expands source text from <input> to <output>. The expansion is made using
770 * the list passed in <symbols> parameter. It allows us to expand macro formal parameters with
771 * actual parameters. The global list of symbols from pp state is used when doing a recursive
776 expand (expand_state
*e
, pp_symbols
*symbols
)
778 while (!IS_NULL(*e
->input
)) {
779 if (IS_FIRST_ID_CHAR(*e
->input
)) {
783 /* Parse the identifier. */
784 slang_string_init (&buffer
);
785 slang_string_pushc (&buffer
, *e
->input
++);
786 while (IS_NEXT_ID_CHAR(*e
->input
))
787 slang_string_pushc (&buffer
, *e
->input
++);
788 id
= slang_string_cstr (&buffer
);
790 /* Now check if the identifier is special in some way. The "defined" identifier is
791 * actually an operator that we must handle here and expand it either to " 0 " or " 1 ".
792 * The other identifiers start with "__" and we expand it to appropriate values
793 * taken from the preprocessor state. */
794 if (_mesa_strcmp (id
, "defined") == 0) {
795 if (!expand_defined (e
, &buffer
))
798 else if (_mesa_strcmp (id
, "__LINE__") == 0) {
799 slang_string_pushc (e
->output
, ' ');
800 slang_string_pushi (e
->output
, e
->state
->line
);
801 slang_string_pushc (e
->output
, ' ');
803 else if (_mesa_strcmp (id
, "__FILE__") == 0) {
804 slang_string_pushc (e
->output
, ' ');
805 slang_string_pushi (e
->output
, e
->state
->file
);
806 slang_string_pushc (e
->output
, ' ');
808 else if (_mesa_strcmp (id
, "__VERSION__") == 0) {
809 slang_string_pushc (e
->output
, ' ');
810 slang_string_pushi (e
->output
, e
->state
->version
);
811 slang_string_pushc (e
->output
, ' ');
814 else if (_mesa_strcmp (id
, "GL_ES") == 0 ||
815 _mesa_strcmp (id
, "GL_FRAGMENT_PRECISION_HIGH") == 0) {
816 slang_string_pushc (e
->output
, ' ');
817 slang_string_pushi (e
->output
, '1');
818 slang_string_pushc (e
->output
, ' ');
824 /* The list of symbols from <symbols> take precedence over the list from <state>.
825 * Note that in some cases this is the same list so avoid double look-up. */
826 symbol
= pp_symbols_find (symbols
, id
);
827 if (symbol
== NULL
&& symbols
!= &e
->state
->symbols
)
828 symbol
= pp_symbols_find (&e
->state
->symbols
, id
);
830 /* If the symbol was found, recursively expand its definition. */
831 if (symbol
!= NULL
) {
832 if (!expand_symbol (e
, symbol
)) {
833 slang_string_free (&buffer
);
838 slang_string_push (e
->output
, &buffer
);
841 slang_string_free (&buffer
);
843 else if (IS_WHITE(*e
->input
)) {
844 slang_string_pushc (e
->output
, *e
->input
++);
847 while (!IS_WHITE(*e
->input
) && !IS_NULL(*e
->input
) && !IS_FIRST_ID_CHAR(*e
->input
))
848 slang_string_pushc (e
->output
, *e
->input
++);
855 parse_if (slang_string
*output
, const byte
*prod
, GLuint
*pi
, GLint
*result
, pp_state
*state
,
861 text
= (const char *) (&prod
[*pi
]);
862 len
= _mesa_strlen (text
);
864 if (state
->cond
.top
->effective
) {
870 /* Expand the expression. */
871 slang_string_init (&expr
);
875 if (!expand (&es
, &state
->symbols
))
878 /* Execute the expression. */
879 count
= execute_expressions (output
, eid
, (const byte
*) (slang_string_cstr (&expr
)),
880 results
, state
->elog
);
881 slang_string_free (&expr
);
884 *result
= results
[0];
887 /* The directive is dead. */
895 #define ESCAPE_TOKEN 0
898 #define TOKEN_DEFINE 1
899 #define TOKEN_UNDEF 2
903 #define TOKEN_ENDIF 6
904 #define TOKEN_ERROR 7
905 #define TOKEN_PRAGMA 8
906 #define TOKEN_EXTENSION 9
907 #define TOKEN_LINE 10
910 #define PARAM_PARAMETER 1
912 #define BEHAVIOR_REQUIRE 1
913 #define BEHAVIOR_ENABLE 2
914 #define BEHAVIOR_WARN 3
915 #define BEHAVIOR_DISABLE 4
917 #define PRAGMA_NO_PARAM 0
918 #define PRAGMA_PARAM 1
922 preprocess_source (slang_string
*output
, const char *source
,
923 grammar pid
, grammar eid
,
924 slang_info_log
*elog
,
925 const struct gl_extensions
*extensions
,
926 struct gl_sl_pragmas
*pragmas
)
928 static const char *predefined
[] = {
934 "GL_FRAGMENT_PRECISION_HIGH",
942 if (!grammar_fast_check (pid
, (const byte
*) (source
), &prod
, &size
, 65536)) {
943 grammar_error_to_log (elog
);
947 pp_state_init (&state
, elog
, extensions
);
948 pp_pragmas_init (pragmas
);
950 /* add the predefined symbols to the symbol table */
951 for (i
= 0; predefined
[i
]; i
++) {
952 pp_symbol
*symbol
= NULL
;
953 symbol
= pp_symbols_push(&state
.symbols
);
955 slang_string_pushs(&symbol
->name
,
956 predefined
[i
], _mesa_strlen(predefined
[i
]));
961 if (prod
[i
] != ESCAPE_TOKEN
) {
962 if (state
.cond
.top
->effective
) {
966 /* Eat only one line of source code to expand it.
967 * FIXME: This approach has one drawback. If a macro with parameters spans across
968 * multiple lines, the preprocessor will raise an error. */
969 slang_string_init (&input
);
970 while (prod
[i
] != '\0' && prod
[i
] != '\n')
971 slang_string_pushc (&input
, prod
[i
++]);
973 slang_string_pushc (&input
, prod
[i
++]);
975 /* Increment line number. */
979 es
.input
= slang_string_cstr (&input
);
981 if (!expand (&es
, &state
.symbols
))
984 slang_string_free (&input
);
987 /* Condition stack is disabled - keep track on line numbers and output only newlines. */
988 if (prod
[i
] == '\n') {
990 /*pp_annotate (output, "%c", prod[i]);*/
993 /*pp_annotate (output, "%c", prod[i]);*/
1008 /* End of source string.
1009 * Check if all #ifs have been terminated by matching #endifs.
1010 * On condition stack there should be only the global condition context. */
1011 if (state
.cond
.top
->endif_required
) {
1012 slang_info_log_error (elog
, "end of source without matching #endif.");
1019 pp_symbol
*symbol
= NULL
;
1021 /* Parse macro name. */
1022 id
= (const char *) (&prod
[i
]);
1023 idlen
= _mesa_strlen (id
);
1024 if (state
.cond
.top
->effective
) {
1025 pp_annotate (output
, "// #define %s(", id
);
1027 /* If the symbol is already defined, override it. */
1028 symbol
= pp_symbols_find (&state
.symbols
, id
);
1029 if (symbol
== NULL
) {
1030 symbol
= pp_symbols_push (&state
.symbols
);
1033 slang_string_pushs (&symbol
->name
, id
, idlen
);
1036 pp_symbol_reset (symbol
);
1041 /* Parse optional macro parameters. */
1042 while (prod
[i
++] != PARAM_END
) {
1043 if (state
.cond
.top
->effective
) {
1046 id
= (const char *) (&prod
[i
]);
1047 idlen
= _mesa_strlen (id
);
1048 pp_annotate (output
, "%s, ", id
);
1049 param
= pp_symbols_push (&symbol
->parameters
);
1052 slang_string_pushs (¶m
->name
, id
, idlen
);
1057 /* Parse macro replacement. */
1058 id
= (const char *) (&prod
[i
]);
1059 idlen
= _mesa_strlen (id
);
1060 if (state
.cond
.top
->effective
) {
1061 pp_annotate (output
, ") %s", id
);
1062 slang_string_pushs (&symbol
->replacement
, id
, idlen
);
1069 id
= (const char *) (&prod
[i
]);
1070 i
+= _mesa_strlen (id
) + 1;
1071 if (state
.cond
.top
->effective
) {
1074 pp_annotate (output
, "// #undef %s", id
);
1075 /* Try to find symbol with given name and remove it. */
1076 symbol
= pp_symbols_find (&state
.symbols
, id
);
1078 if (!pp_symbols_erase (&state
.symbols
, symbol
))
1087 /* Parse #if expression end execute it. */
1088 pp_annotate (output
, "// #if ");
1089 if (!parse_if (output
, prod
, &i
, &result
, &state
, eid
))
1092 /* Push new condition on the stack. */
1093 if (!pp_cond_stack_push (&state
.cond
, state
.elog
))
1095 state
.cond
.top
->current
= result
? GL_TRUE
: GL_FALSE
;
1096 state
.cond
.top
->else_allowed
= GL_TRUE
;
1097 state
.cond
.top
->endif_required
= GL_TRUE
;
1098 pp_cond_stack_reevaluate (&state
.cond
);
1103 /* Check if #else is alloved here. */
1104 if (!state
.cond
.top
->else_allowed
) {
1105 slang_info_log_error (elog
, "#else without matching #if.");
1109 /* Negate current condition and reevaluate it. */
1110 state
.cond
.top
->current
= !state
.cond
.top
->current
;
1111 state
.cond
.top
->else_allowed
= GL_FALSE
;
1112 pp_cond_stack_reevaluate (&state
.cond
);
1113 if (state
.cond
.top
->effective
)
1114 pp_annotate (output
, "// #else");
1118 /* Check if #elif is alloved here. */
1119 if (!state
.cond
.top
->else_allowed
) {
1120 slang_info_log_error (elog
, "#elif without matching #if.");
1124 /* Negate current condition and reevaluate it. */
1125 state
.cond
.top
->current
= !state
.cond
.top
->current
;
1126 pp_cond_stack_reevaluate (&state
.cond
);
1128 if (state
.cond
.top
->effective
)
1129 pp_annotate (output
, "// #elif ");
1134 /* Parse #elif expression end execute it. */
1135 if (!parse_if (output
, prod
, &i
, &result
, &state
, eid
))
1138 /* Update current condition and reevaluate it. */
1139 state
.cond
.top
->current
= result
? GL_TRUE
: GL_FALSE
;
1140 pp_cond_stack_reevaluate (&state
.cond
);
1145 /* Check if #endif is alloved here. */
1146 if (!state
.cond
.top
->endif_required
) {
1147 slang_info_log_error (elog
, "#endif without matching #if.");
1151 /* Pop the condition off the stack. */
1153 if (state
.cond
.top
->effective
)
1154 pp_annotate (output
, "// #endif");
1157 case TOKEN_EXTENSION
:
1158 /* Parse the extension name. */
1159 id
= (const char *) (&prod
[i
]);
1160 i
+= _mesa_strlen (id
) + 1;
1161 if (state
.cond
.top
->effective
)
1162 pp_annotate (output
, "// #extension %s: ", id
);
1164 /* Parse and apply extension behavior. */
1165 if (state
.cond
.top
->effective
) {
1166 switch (prod
[i
++]) {
1168 case BEHAVIOR_REQUIRE
:
1169 pp_annotate (output
, "require");
1170 if (!pp_ext_set (&state
.ext
, id
, GL_TRUE
)) {
1171 if (_mesa_strcmp (id
, "all") == 0) {
1172 slang_info_log_error (elog
, "require: bad behavior for #extension all.");
1176 slang_info_log_error (elog
, "%s: required extension is not supported.", id
);
1182 case BEHAVIOR_ENABLE
:
1183 pp_annotate (output
, "enable");
1184 if (!pp_ext_set (&state
.ext
, id
, GL_TRUE
)) {
1185 if (_mesa_strcmp (id
, "all") == 0) {
1186 slang_info_log_error (elog
, "enable: bad behavior for #extension all.");
1190 slang_info_log_warning (elog
, "%s: enabled extension is not supported.", id
);
1196 pp_annotate (output
, "warn");
1197 if (!pp_ext_set (&state
.ext
, id
, GL_TRUE
)) {
1198 if (_mesa_strcmp (id
, "all") != 0) {
1199 slang_info_log_warning (elog
, "%s: enabled extension is not supported.", id
);
1204 case BEHAVIOR_DISABLE
:
1205 pp_annotate (output
, "disable");
1206 if (!pp_ext_set (&state
.ext
, id
, GL_FALSE
)) {
1207 if (_mesa_strcmp (id
, "all") == 0) {
1208 pp_ext_disable_all (&state
.ext
);
1211 slang_info_log_warning (elog
, "%s: disabled extension is not supported.", id
);
1225 const char *pragma
, *param
;
1227 pragma
= (const char *) (&prod
[i
]);
1228 i
+= _mesa_strlen(pragma
) + 1;
1229 have_param
= (prod
[i
++] == PRAGMA_PARAM
);
1231 param
= (const char *) (&prod
[i
]);
1232 i
+= _mesa_strlen(param
) + 1;
1237 pp_pragma(pragmas
, pragma
, param
);
1242 id
= (const char *) (&prod
[i
]);
1243 i
+= _mesa_strlen (id
) + 1;
1245 if (state
.cond
.top
->effective
) {
1246 slang_string buffer
;
1251 slang_string_init (&buffer
);
1253 es
.output
= &buffer
;
1256 if (!expand (&es
, &state
.symbols
))
1259 pp_annotate (output
, "// #line ");
1260 count
= execute_expressions (output
, eid
,
1261 (const byte
*) (slang_string_cstr (&buffer
)),
1262 results
, state
.elog
);
1263 slang_string_free (&buffer
);
1267 state
.line
= results
[0] - 1;
1269 state
.file
= results
[1];
1276 /* Check for missing #endifs. */
1277 if (state
.cond
.top
->endif_required
) {
1278 slang_info_log_error (elog
, "#endif expected but end of source found.");
1282 grammar_alloc_free(prod
);
1283 pp_state_free (&state
);
1287 grammar_alloc_free(prod
);
1288 pp_state_free (&state
);
1294 * Run preprocessor on source code.
1295 * \param extensions indicates which GL extensions are enabled
1296 * \param output the post-process results
1297 * \param input the input text
1298 * \param elog log to record warnings, errors
1299 * \return GL_TRUE for success, GL_FALSE for error
1302 _slang_preprocess_directives(slang_string
*output
,
1304 slang_info_log
*elog
,
1305 const struct gl_extensions
*extensions
,
1306 struct gl_sl_pragmas
*pragmas
)
1311 pid
= grammar_load_from_text ((const byte
*) (slang_pp_directives_syn
));
1313 grammar_error_to_log (elog
);
1316 eid
= grammar_load_from_text ((const byte
*) (slang_pp_expression_syn
));
1318 grammar_error_to_log (elog
);
1319 grammar_destroy (pid
);
1322 success
= preprocess_source (output
, input
, pid
, eid
, elog
, extensions
, pragmas
);
1323 grammar_destroy (eid
);
1324 grammar_destroy (pid
);