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
32 #include "slang_compile.h"
36 * Init a slang_operation object
39 slang_operation_construct(slang_operation
* oper
)
41 oper
->type
= SLANG_OPER_NONE
;
42 oper
->children
= NULL
;
43 oper
->num_children
= 0;
44 oper
->literal
[0] = 0.0;
45 oper
->a_id
= SLANG_ATOM_NULL
;
46 oper
->locals
= _slang_variable_scope_new(NULL
);
47 if (oper
->locals
== NULL
)
49 _slang_variable_scope_ctr(oper
->locals
);
56 slang_operation_destruct(slang_operation
* oper
)
60 for (i
= 0; i
< oper
->num_children
; i
++)
61 slang_operation_destruct(oper
->children
+ i
);
62 slang_alloc_free(oper
->children
);
63 /*#define FREE_MEMORY*/
65 /* XXX revisit and fix memory coruption here ! */
66 slang_variable_scope_destruct(oper
->locals
);
67 slang_alloc_free(oper
->locals
);
69 oper
->children
= NULL
;
70 oper
->num_children
= 0;
75 * Recursively copy a slang_operation node.
76 * \return GL_TRUE for success, GL_FALSE if failure
79 slang_operation_copy(slang_operation
* x
, const slang_operation
* y
)
84 if (!slang_operation_construct(&z
))
87 z
.children
= (slang_operation
*)
88 slang_alloc_malloc(y
->num_children
* sizeof(slang_operation
));
89 if (z
.children
== NULL
) {
90 slang_operation_destruct(&z
);
93 for (z
.num_children
= 0; z
.num_children
< y
->num_children
;
95 if (!slang_operation_construct(&z
.children
[z
.num_children
])) {
96 slang_operation_destruct(&z
);
100 for (i
= 0; i
< z
.num_children
; i
++) {
101 if (!slang_operation_copy(&z
.children
[i
], &y
->children
[i
])) {
102 slang_operation_destruct(&z
);
106 z
.literal
[0] = y
->literal
[0];
107 z
.literal
[1] = y
->literal
[1];
108 z
.literal
[2] = y
->literal
[2];
109 z
.literal
[3] = y
->literal
[3];
110 z
.literal_size
= y
->literal_size
;
113 if (!slang_variable_scope_copy(z
.locals
, y
->locals
)) {
114 slang_operation_destruct(&z
);
122 slang_operation_destruct(x
);
129 slang_operation_new(GLuint count
)
132 = (slang_operation
*) _mesa_malloc(count
* sizeof(slang_operation
));
136 for (i
= 0; i
< count
; i
++)
137 slang_operation_construct(ops
+ i
);
144 * Delete operation and all children
147 slang_operation_delete(slang_operation
*oper
)
149 slang_operation_destruct(oper
);
155 slang_operation_grow(GLuint
*numChildren
, slang_operation
**children
)
157 slang_operation
*ops
;
159 ops
= (slang_operation
*)
160 slang_alloc_realloc(*children
,
161 *numChildren
* sizeof(slang_operation
),
162 (*numChildren
+ 1) * sizeof(slang_operation
));
164 slang_operation
*newOp
= ops
+ *numChildren
;
165 if (!slang_operation_construct(newOp
)) {
178 * Insert a new slang_operation into an array.
179 * \param numChildren pointer to current number of children (in/out)
180 * \param children address of array (in/out)
181 * \param pos position to insert
182 * \return pointer to the new operation
185 slang_operation_insert(GLuint
*numChildren
, slang_operation
**children
,
188 slang_operation
*ops
;
190 assert(pos
<= *numChildren
);
192 ops
= (slang_operation
*)
193 _mesa_malloc((*numChildren
+ 1) * sizeof(slang_operation
));
195 slang_operation
*newOp
;
198 _mesa_memcpy(ops
, *children
, pos
* sizeof(slang_operation
));
199 if (pos
< *numChildren
)
200 _mesa_memcpy(newOp
+ 1, (*children
) + pos
,
201 (*numChildren
- pos
) * sizeof(slang_operation
));
203 if (!slang_operation_construct(newOp
)) {