r600g: add POW instruction
[mesa.git] / src / glsl / pp / sl_pp_extension.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "sl_pp_context.h"
32 #include "sl_pp_process.h"
33 #include "sl_pp_public.h"
34 #include "sl_pp_token.h"
35
36
37 /**
38 * Declare an extension to the preprocessor. This tells the preprocessor
39 * which extensions are supported by Mesa.
40 * The shader still needs to have a "#extension name: behavior" line to enable
41 * the extension.
42 */
43 int
44 sl_pp_context_add_extension(struct sl_pp_context *context,
45 const char *name)
46 {
47 struct sl_pp_extension ext;
48
49 if (context->num_extensions == SL_PP_MAX_EXTENSIONS) {
50 return -1;
51 }
52
53 ext.name = sl_pp_context_add_unique_str(context, name);
54 if (ext.name == -1) {
55 return -1;
56 }
57
58 context->extensions[context->num_extensions++] = ext;
59
60 assert(context->num_extensions <= sizeof(context->extensions));
61
62 return 0;
63 }
64
65
66 /**
67 * Process a "#extension name: behavior" directive.
68 */
69 int
70 sl_pp_process_extension(struct sl_pp_context *context,
71 const struct sl_pp_token_info *input,
72 unsigned int first,
73 unsigned int last,
74 struct sl_pp_process_state *state)
75 {
76 int extension_name = -1;
77 int behavior = -1;
78 struct sl_pp_token_info out;
79
80 /* Grab the extension name. */
81 if (first < last && input[first].token == SL_PP_IDENTIFIER) {
82 extension_name = input[first].data.identifier;
83 first++;
84 }
85 if (extension_name == -1) {
86 strcpy(context->error_msg, "expected identifier after `#extension'");
87 return -1;
88 }
89
90 /* Make sure the extension is supported. */
91 if (extension_name == context->dict.all) {
92 out.data.extension = extension_name;
93 } else {
94 unsigned int i;
95
96 out.data.extension = -1;
97 for (i = 0; i < context->num_extensions; i++) {
98 if (extension_name == context->extensions[i].name) {
99 out.data.extension = extension_name;
100 break;
101 }
102 }
103 }
104
105 /* Grab the colon separating the extension name and behavior. */
106 while (first < last && input[first].token == SL_PP_WHITESPACE) {
107 first++;
108 }
109 if (first < last && input[first].token == SL_PP_COLON) {
110 first++;
111 } else {
112 strcpy(context->error_msg, "expected `:' after extension name");
113 return -1;
114 }
115 while (first < last && input[first].token == SL_PP_WHITESPACE) {
116 first++;
117 }
118
119 /* Grab the behavior name. */
120 if (first < last && input[first].token == SL_PP_IDENTIFIER) {
121 behavior = input[first].data.identifier;
122 first++;
123 }
124 if (behavior == -1) {
125 strcpy(context->error_msg, "expected identifier after `:'");
126 return -1;
127 }
128
129 if (behavior == context->dict.require) {
130 if (out.data.extension == -1) {
131 strcpy(context->error_msg, "the required extension is not supported");
132 return -1;
133 }
134 if (out.data.extension == context->dict.all) {
135 strcpy(context->error_msg, "invalid behavior for `all' extension: `require'");
136 return -1;
137 }
138 out.token = SL_PP_EXTENSION_REQUIRE;
139 } else if (behavior == context->dict.enable) {
140 if (out.data.extension == -1) {
141 /* Warning: the extension cannot be enabled. */
142 return 0;
143 }
144 if (out.data.extension == context->dict.all) {
145 strcpy(context->error_msg, "invalid behavior for `all' extension: `enable'");
146 return -1;
147 }
148 out.token = SL_PP_EXTENSION_ENABLE;
149 } else if (behavior == context->dict.warn) {
150 if (out.data.extension == -1) {
151 /* Warning: the extension is not supported. */
152 return 0;
153 }
154 out.token = SL_PP_EXTENSION_WARN;
155 } else if (behavior == context->dict.disable) {
156 if (out.data.extension == -1) {
157 /* Warning: the extension is not supported. */
158 return 0;
159 }
160 out.token = SL_PP_EXTENSION_DISABLE;
161 } else {
162 strcpy(context->error_msg, "unrecognised behavior name");
163 return -1;
164 }
165
166 /* Grab the end of line. */
167 while (first < last && input[first].token == SL_PP_WHITESPACE) {
168 first++;
169 }
170 if (first < last) {
171 strcpy(context->error_msg, "expected end of line after behavior name");
172 return -1;
173 }
174
175 if (sl_pp_process_out(state, &out)) {
176 return -1;
177 }
178
179 return 0;
180 }