r600g: add POW instruction
[mesa.git] / src / glsl / pp / sl_pp_define.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 <stdlib.h>
29 #include <string.h>
30 #include "sl_pp_context.h"
31 #include "sl_pp_macro.h"
32 #include "sl_pp_process.h"
33 #include "sl_pp_public.h"
34 #include "sl_pp_token.h"
35
36
37 static void
38 skip_whitespace(const struct sl_pp_token_info *input,
39 unsigned int *first,
40 unsigned int last)
41 {
42 while (*first < last && input[*first].token == SL_PP_WHITESPACE) {
43 (*first)++;
44 }
45 }
46
47
48 static int
49 _parse_formal_args(struct sl_pp_context *context,
50 const struct sl_pp_token_info *input,
51 unsigned int *first,
52 unsigned int last,
53 struct sl_pp_macro *macro)
54 {
55 struct sl_pp_macro_formal_arg **arg;
56
57 macro->num_args = 0;
58
59 skip_whitespace(input, first, last);
60 if (*first < last) {
61 if (input[*first].token == SL_PP_RPAREN) {
62 (*first)++;
63 return 0;
64 }
65 } else {
66 strcpy(context->error_msg, "expected either macro formal argument or `)'");
67 return -1;
68 }
69
70 arg = &macro->arg;
71
72 for (;;) {
73 if (*first < last && input[*first].token != SL_PP_IDENTIFIER) {
74 strcpy(context->error_msg, "expected macro formal argument");
75 return -1;
76 }
77
78 *arg = malloc(sizeof(struct sl_pp_macro_formal_arg));
79 if (!*arg) {
80 strcpy(context->error_msg, "out of memory");
81 return -1;
82 }
83
84 (**arg).name = input[*first].data.identifier;
85 (*first)++;
86
87 (**arg).next = NULL;
88 arg = &(**arg).next;
89
90 macro->num_args++;
91
92 skip_whitespace(input, first, last);
93 if (*first < last) {
94 if (input[*first].token == SL_PP_COMMA) {
95 (*first)++;
96 skip_whitespace(input, first, last);
97 } else if (input[*first].token == SL_PP_RPAREN) {
98 (*first)++;
99 return 0;
100 } else {
101 strcpy(context->error_msg, "expected either `,' or `)'");
102 return -1;
103 }
104 } else {
105 strcpy(context->error_msg, "expected either `,' or `)'");
106 return -1;
107 }
108 }
109
110 /* Should not gete here. */
111 }
112
113
114 int
115 sl_pp_process_define(struct sl_pp_context *context,
116 const struct sl_pp_token_info *input,
117 unsigned int first,
118 unsigned int last)
119 {
120 int macro_name = -1;
121 struct sl_pp_macro *macro;
122 unsigned int i;
123 unsigned int body_len;
124 unsigned int j;
125
126 if (first < last && input[first].token == SL_PP_IDENTIFIER) {
127 macro_name = input[first].data.identifier;
128 first++;
129 }
130 if (macro_name == -1) {
131 strcpy(context->error_msg, "expected macro name");
132 return -1;
133 }
134
135 /* Check for reserved macro names */
136 {
137 const char *name = sl_pp_context_cstr(context, macro_name);
138
139 if (strstr(name, "__")) {
140 strcpy(context->error_msg, "macro names containing `__' are reserved");
141 return 1;
142 }
143 if (name[0] == 'G' && name[1] == 'L' && name[2] == '_') {
144 strcpy(context->error_msg, "macro names prefixed with `GL_' are reserved");
145 return 1;
146 }
147 }
148
149 for (macro = context->macro; macro; macro = macro->next) {
150 if (macro->name == macro_name) {
151 break;
152 }
153 }
154
155 if (!macro) {
156 macro = sl_pp_macro_new();
157 if (!macro) {
158 strcpy(context->error_msg, "out of memory");
159 return -1;
160 }
161
162 *context->macro_tail = macro;
163 context->macro_tail = &macro->next;
164 } else {
165 sl_pp_macro_reset(macro);
166 }
167
168 macro->name = macro_name;
169
170 /*
171 * If there is no whitespace between macro name and left paren, a macro
172 * formal argument list follows. This is the only place where the presence
173 * of a whitespace matters and it's the only reason why we are dealing
174 * with whitespace at this level.
175 */
176 if (first < last && input[first].token == SL_PP_LPAREN) {
177 first++;
178 if (_parse_formal_args(context, input, &first, last, macro)) {
179 return -1;
180 }
181 }
182
183 /* Calculate body size, trim out whitespace, make room for EOF. */
184 body_len = 1;
185 for (i = first; i < last; i++) {
186 if (input[i].token != SL_PP_WHITESPACE) {
187 body_len++;
188 }
189 }
190
191 macro->body = malloc(sizeof(struct sl_pp_token_info) * body_len);
192 if (!macro->body) {
193 strcpy(context->error_msg, "out of memory");
194 return -1;
195 }
196
197 for (j = 0, i = first; i < last; i++) {
198 if (input[i].token != SL_PP_WHITESPACE) {
199 macro->body[j++] = input[i];
200 }
201 }
202 macro->body[j++].token = SL_PP_EOF;
203
204 return 0;
205 }
206
207
208 int
209 sl_pp_process_undef(struct sl_pp_context *context,
210 const struct sl_pp_token_info *input,
211 unsigned int first,
212 unsigned int last)
213 {
214 int macro_name = -1;
215 struct sl_pp_macro **pmacro;
216 struct sl_pp_macro *macro;
217
218 if (first < last && input[first].token == SL_PP_IDENTIFIER) {
219 macro_name = input[first].data.identifier;
220 }
221 if (macro_name == -1) {
222 return 0;
223 }
224
225 for (pmacro = &context->macro; *pmacro; pmacro = &(**pmacro).next) {
226 if ((**pmacro).name == macro_name) {
227 break;
228 }
229 }
230 if (!*pmacro) {
231 return 0;
232 }
233
234 macro = *pmacro;
235 *pmacro = macro->next;
236 macro->next = NULL;
237 sl_pp_macro_free(macro);
238
239 return 0;
240 }