2 * Mesa 3-D graphics library
5 * Copyright (C) 2005-2006 Brian Paul 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_compile_operation.c
27 * slang front-end compiler
31 #include "main/imports.h"
32 #include "slang_compile.h"
33 #include "slang_mem.h"
37 * Init a slang_operation object
40 slang_operation_construct(slang_operation
* oper
)
42 oper
->type
= SLANG_OPER_NONE
;
43 oper
->children
= NULL
;
44 oper
->num_children
= 0;
45 oper
->literal
[0] = 0.0;
46 oper
->literal_size
= 1;
47 oper
->array_constructor
= GL_FALSE
;
48 oper
->a_id
= SLANG_ATOM_NULL
;
49 oper
->locals
= _slang_variable_scope_new(NULL
);
50 if (oper
->locals
== NULL
)
52 _slang_variable_scope_ctr(oper
->locals
);
59 slang_operation_destruct(slang_operation
* oper
)
63 for (i
= 0; i
< oper
->num_children
; i
++)
64 slang_operation_destruct(oper
->children
+ i
);
65 _slang_free(oper
->children
);
66 slang_variable_scope_destruct(oper
->locals
);
67 _slang_free(oper
->locals
);
68 oper
->children
= NULL
;
69 oper
->num_children
= 0;
75 * Recursively traverse 'oper', replacing occurances of 'oldScope' with
76 * 'newScope' in the oper->locals->outer_scope field.
79 slang_replace_scope(slang_operation
*oper
,
80 slang_variable_scope
*oldScope
,
81 slang_variable_scope
*newScope
)
85 if (oper
->locals
!= newScope
&&
86 oper
->locals
->outer_scope
== oldScope
) {
87 /* found. replace old w/ new */
88 oper
->locals
->outer_scope
= newScope
;
91 if (oper
->type
== SLANG_OPER_VARIABLE_DECL
) {
92 /* search/replace in the initializer */
94 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
95 if (var
&& var
->initializer
) {
96 slang_replace_scope(var
->initializer
, oldScope
, newScope
);
100 /* search/replace in children */
101 for (i
= 0; i
< oper
->num_children
; i
++) {
102 slang_replace_scope(&oper
->children
[i
], oldScope
, newScope
);
108 * Recursively copy a slang_operation node.
109 * \param x copy target
110 * \param y copy source
111 * \return GL_TRUE for success, GL_FALSE if failure
114 slang_operation_copy(slang_operation
* x
, const slang_operation
* y
)
119 if (!slang_operation_construct(&z
))
122 z
.children
= (slang_operation
*)
123 _slang_alloc(y
->num_children
* sizeof(slang_operation
));
124 if (z
.children
== NULL
) {
125 slang_operation_destruct(&z
);
128 for (z
.num_children
= 0; z
.num_children
< y
->num_children
;
130 if (!slang_operation_construct(&z
.children
[z
.num_children
])) {
131 slang_operation_destruct(&z
);
135 for (i
= 0; i
< z
.num_children
; i
++) {
136 if (!slang_operation_copy(&z
.children
[i
], &y
->children
[i
])) {
137 slang_operation_destruct(&z
);
141 z
.literal
[0] = y
->literal
[0];
142 z
.literal
[1] = y
->literal
[1];
143 z
.literal
[2] = y
->literal
[2];
144 z
.literal
[3] = y
->literal
[3];
145 z
.literal_size
= y
->literal_size
;
146 assert(y
->literal_size
>= 1);
147 assert(y
->literal_size
<= 4);
150 if (!slang_variable_scope_copy(z
.locals
, y
->locals
)) {
151 slang_operation_destruct(&z
);
159 slang_operation_destruct(x
);
162 /* If this operation declares a new scope, we need to make sure
163 * all children point to it, not the original operation's scope!
165 if (x
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
166 x
->type
== SLANG_OPER_WHILE
||
167 x
->type
== SLANG_OPER_FOR
) {
168 slang_replace_scope(x
, y
->locals
, x
->locals
);
176 slang_operation_new(GLuint count
)
179 = (slang_operation
*) _slang_alloc(count
* sizeof(slang_operation
));
183 for (i
= 0; i
< count
; i
++)
184 slang_operation_construct(ops
+ i
);
191 * Delete operation and all children
194 slang_operation_delete(slang_operation
*oper
)
196 slang_operation_destruct(oper
);
202 slang_operation_grow(GLuint
*numChildren
, slang_operation
**children
)
204 slang_operation
*ops
;
206 ops
= (slang_operation
*)
207 _slang_realloc(*children
,
208 *numChildren
* sizeof(slang_operation
),
209 (*numChildren
+ 1) * sizeof(slang_operation
));
211 slang_operation
*newOp
= ops
+ *numChildren
;
212 if (!slang_operation_construct(newOp
)) {
225 * Insert a new slang_operation into an array.
226 * \param numElements pointer to current array size (in/out)
227 * \param array address of the array (in/out)
228 * \param pos position to insert new element
229 * \return pointer to the new operation/element
232 slang_operation_insert(GLuint
*numElements
, slang_operation
**array
,
235 slang_operation
*ops
;
237 assert(pos
<= *numElements
);
239 ops
= (slang_operation
*)
240 _slang_alloc((*numElements
+ 1) * sizeof(slang_operation
));
242 slang_operation
*newOp
;
245 _mesa_memcpy(ops
, *array
, pos
* sizeof(slang_operation
));
246 if (pos
< *numElements
)
247 _mesa_memcpy(newOp
+ 1, (*array
) + pos
,
248 (*numElements
- pos
) * sizeof(slang_operation
));
250 if (!slang_operation_construct(newOp
)) {
267 _slang_operation_swap(slang_operation
*oper0
, slang_operation
*oper1
)
269 slang_operation tmp
= *oper0
;