2 * Mesa 3-D graphics library
5 * Copyright (C) 2005-2007 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_codegen.c
27 * Generate IR tree from AST.
34 *** The new_() functions return a new instance of a simple IR node.
35 *** The gen_() functions generate larger IR trees from the simple nodes.
40 #include "main/imports.h"
41 #include "main/macros.h"
42 #include "main/mtypes.h"
43 #include "shader/program.h"
44 #include "shader/prog_instruction.h"
45 #include "shader/prog_parameter.h"
46 #include "shader/prog_print.h"
47 #include "shader/prog_statevars.h"
48 #include "slang_typeinfo.h"
49 #include "slang_codegen.h"
50 #include "slang_compile.h"
51 #include "slang_label.h"
52 #include "slang_mem.h"
53 #include "slang_simplify.h"
54 #include "slang_emit.h"
55 #include "slang_vartable.h"
57 #include "slang_print.h"
60 static slang_ir_node
*
61 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
);
65 * Retrieves type information about an operation.
66 * Returns GL_TRUE on success.
67 * Returns GL_FALSE otherwise.
70 typeof_operation(const struct slang_assemble_ctx_
*A
,
74 return _slang_typeof_operation(op
, &A
->space
, ti
, A
->atoms
, A
->log
);
79 is_sampler_type(const slang_fully_specified_type
*t
)
81 switch (t
->specifier
.type
) {
82 case SLANG_SPEC_SAMPLER1D
:
83 case SLANG_SPEC_SAMPLER2D
:
84 case SLANG_SPEC_SAMPLER3D
:
85 case SLANG_SPEC_SAMPLERCUBE
:
86 case SLANG_SPEC_SAMPLER1DSHADOW
:
87 case SLANG_SPEC_SAMPLER2DSHADOW
:
88 case SLANG_SPEC_SAMPLER2DRECT
:
89 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
98 * Return the offset (in floats or ints) of the named field within
99 * the given struct. Return -1 if field not found.
100 * If field is NULL, return the size of the struct instead.
103 _slang_field_offset(const slang_type_specifier
*spec
, slang_atom field
)
107 for (i
= 0; i
< spec
->_struct
->fields
->num_variables
; i
++) {
108 const slang_variable
*v
= spec
->_struct
->fields
->variables
[i
];
109 const GLuint sz
= _slang_sizeof_type_specifier(&v
->type
.specifier
);
111 /* types larger than 1 float are register (4-float) aligned */
112 offset
= (offset
+ 3) & ~3;
114 if (field
&& v
->a_name
== field
) {
120 return -1; /* field not found */
122 return offset
; /* struct size */
127 * Return the size (in floats) of the given type specifier.
128 * If the size is greater than 4, the size should be a multiple of 4
129 * so that the correct number of 4-float registers are allocated.
130 * For example, a mat3x2 is size 12 because we want to store the
131 * 3 columns in 3 float[4] registers.
134 _slang_sizeof_type_specifier(const slang_type_specifier
*spec
)
137 switch (spec
->type
) {
138 case SLANG_SPEC_VOID
:
141 case SLANG_SPEC_BOOL
:
144 case SLANG_SPEC_BVEC2
:
147 case SLANG_SPEC_BVEC3
:
150 case SLANG_SPEC_BVEC4
:
156 case SLANG_SPEC_IVEC2
:
159 case SLANG_SPEC_IVEC3
:
162 case SLANG_SPEC_IVEC4
:
165 case SLANG_SPEC_FLOAT
:
168 case SLANG_SPEC_VEC2
:
171 case SLANG_SPEC_VEC3
:
174 case SLANG_SPEC_VEC4
:
177 case SLANG_SPEC_MAT2
:
178 sz
= 2 * 4; /* 2 columns (regs) */
180 case SLANG_SPEC_MAT3
:
183 case SLANG_SPEC_MAT4
:
186 case SLANG_SPEC_MAT23
:
187 sz
= 2 * 4; /* 2 columns (regs) */
189 case SLANG_SPEC_MAT32
:
190 sz
= 3 * 4; /* 3 columns (regs) */
192 case SLANG_SPEC_MAT24
:
195 case SLANG_SPEC_MAT42
:
196 sz
= 4 * 4; /* 4 columns (regs) */
198 case SLANG_SPEC_MAT34
:
201 case SLANG_SPEC_MAT43
:
202 sz
= 4 * 4; /* 4 columns (regs) */
204 case SLANG_SPEC_SAMPLER1D
:
205 case SLANG_SPEC_SAMPLER2D
:
206 case SLANG_SPEC_SAMPLER3D
:
207 case SLANG_SPEC_SAMPLERCUBE
:
208 case SLANG_SPEC_SAMPLER1DSHADOW
:
209 case SLANG_SPEC_SAMPLER2DSHADOW
:
210 case SLANG_SPEC_SAMPLER2DRECT
:
211 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
212 sz
= 1; /* a sampler is basically just an integer index */
214 case SLANG_SPEC_STRUCT
:
215 sz
= _slang_field_offset(spec
, 0); /* special use */
217 sz
= (sz
+ 3) & ~0x3; /* round up to multiple of four */
220 case SLANG_SPEC_ARRAY
:
221 sz
= _slang_sizeof_type_specifier(spec
->_array
);
224 _mesa_problem(NULL
, "Unexpected type in _slang_sizeof_type_specifier()");
229 /* if size is > 4, it should be a multiple of four */
230 assert((sz
& 0x3) == 0);
237 * Establish the binding between a slang_ir_node and a slang_variable.
238 * Then, allocate/attach a slang_ir_storage object to the IR node if needed.
239 * The IR node must be a IR_VAR or IR_VAR_DECL node.
240 * \param n the IR node
241 * \param var the variable to associate with the IR node
244 _slang_attach_storage(slang_ir_node
*n
, slang_variable
*var
)
248 assert(n
->Opcode
== IR_VAR
|| n
->Opcode
== IR_VAR_DECL
);
249 assert(!n
->Var
|| n
->Var
== var
);
254 /* need to setup storage */
255 if (n
->Var
&& n
->Var
->store
) {
256 /* node storage info = var storage info */
257 n
->Store
= n
->Var
->store
;
260 /* alloc new storage info */
261 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -7, -5);
263 printf("%s var=%s Store=%p Size=%d\n", __FUNCTION__
,
265 (void*) n
->Store
, n
->Store
->Size
);
268 n
->Var
->store
= n
->Store
;
269 assert(n
->Var
->store
);
276 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
277 * or -1 if the type is not a sampler.
280 sampler_to_texture_index(const slang_type_specifier_type type
)
283 case SLANG_SPEC_SAMPLER1D
:
284 return TEXTURE_1D_INDEX
;
285 case SLANG_SPEC_SAMPLER2D
:
286 return TEXTURE_2D_INDEX
;
287 case SLANG_SPEC_SAMPLER3D
:
288 return TEXTURE_3D_INDEX
;
289 case SLANG_SPEC_SAMPLERCUBE
:
290 return TEXTURE_CUBE_INDEX
;
291 case SLANG_SPEC_SAMPLER1DSHADOW
:
292 return TEXTURE_1D_INDEX
; /* XXX fix */
293 case SLANG_SPEC_SAMPLER2DSHADOW
:
294 return TEXTURE_2D_INDEX
; /* XXX fix */
295 case SLANG_SPEC_SAMPLER2DRECT
:
296 return TEXTURE_RECT_INDEX
;
297 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
298 return TEXTURE_RECT_INDEX
; /* XXX fix */
305 #define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
308 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
309 * a vertex or fragment program input variable. Return -1 if the input
311 * XXX return size too
314 _slang_input_index(const char *name
, GLenum target
, GLuint
*swizzleOut
)
321 static const struct input_info vertInputs
[] = {
322 { "gl_Vertex", VERT_ATTRIB_POS
, SWIZZLE_NOOP
},
323 { "gl_Normal", VERT_ATTRIB_NORMAL
, SWIZZLE_NOOP
},
324 { "gl_Color", VERT_ATTRIB_COLOR0
, SWIZZLE_NOOP
},
325 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
, SWIZZLE_NOOP
},
326 { "gl_FogCoord", VERT_ATTRIB_FOG
, SWIZZLE_XXXX
},
327 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
, SWIZZLE_NOOP
},
328 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
, SWIZZLE_NOOP
},
329 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
, SWIZZLE_NOOP
},
330 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
, SWIZZLE_NOOP
},
331 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
, SWIZZLE_NOOP
},
332 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
, SWIZZLE_NOOP
},
333 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
, SWIZZLE_NOOP
},
334 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
, SWIZZLE_NOOP
},
335 { NULL
, 0, SWIZZLE_NOOP
}
337 static const struct input_info fragInputs
[] = {
338 { "gl_FragCoord", FRAG_ATTRIB_WPOS
, SWIZZLE_NOOP
},
339 { "gl_Color", FRAG_ATTRIB_COL0
, SWIZZLE_NOOP
},
340 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
, SWIZZLE_NOOP
},
341 { "gl_TexCoord", FRAG_ATTRIB_TEX0
, SWIZZLE_NOOP
},
342 /* note: we're packing several quantities into the fogcoord vector */
343 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_XXXX
},
344 { "gl_FrontFacing", FRAG_ATTRIB_FOGC
, SWIZZLE_YYYY
}, /*XXX*/
345 { "gl_PointCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_ZWWW
},
346 { NULL
, 0, SWIZZLE_NOOP
}
349 const struct input_info
*inputs
350 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertInputs
: fragInputs
;
352 ASSERT(MAX_TEXTURE_UNITS
== 8); /* if this fails, fix vertInputs above */
354 for (i
= 0; inputs
[i
].Name
; i
++) {
355 if (strcmp(inputs
[i
].Name
, name
) == 0) {
357 *swizzleOut
= inputs
[i
].Swizzle
;
358 return inputs
[i
].Attrib
;
366 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
367 * a vertex or fragment program output variable. Return -1 for an invalid
371 _slang_output_index(const char *name
, GLenum target
)
377 static const struct output_info vertOutputs
[] = {
378 { "gl_Position", VERT_RESULT_HPOS
},
379 { "gl_FrontColor", VERT_RESULT_COL0
},
380 { "gl_BackColor", VERT_RESULT_BFC0
},
381 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
},
382 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
},
383 { "gl_TexCoord", VERT_RESULT_TEX0
},
384 { "gl_FogFragCoord", VERT_RESULT_FOGC
},
385 { "gl_PointSize", VERT_RESULT_PSIZ
},
388 static const struct output_info fragOutputs
[] = {
389 { "gl_FragColor", FRAG_RESULT_COLR
},
390 { "gl_FragDepth", FRAG_RESULT_DEPR
},
391 { "gl_FragData", FRAG_RESULT_DATA0
},
395 const struct output_info
*outputs
396 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertOutputs
: fragOutputs
;
398 for (i
= 0; outputs
[i
].Name
; i
++) {
399 if (strcmp(outputs
[i
].Name
, name
) == 0) {
401 return outputs
[i
].Attrib
;
409 /**********************************************************************/
413 * Map "_asm foo" to IR_FOO, etc.
418 slang_ir_opcode Opcode
;
419 GLuint HaveRetValue
, NumParams
;
423 static slang_asm_info AsmInfo
[] = {
425 { "vec4_add", IR_ADD
, 1, 2 },
426 { "vec4_subtract", IR_SUB
, 1, 2 },
427 { "vec4_multiply", IR_MUL
, 1, 2 },
428 { "vec4_dot", IR_DOT4
, 1, 2 },
429 { "vec3_dot", IR_DOT3
, 1, 2 },
430 { "vec3_cross", IR_CROSS
, 1, 2 },
431 { "vec4_lrp", IR_LRP
, 1, 3 },
432 { "vec4_min", IR_MIN
, 1, 2 },
433 { "vec4_max", IR_MAX
, 1, 2 },
434 { "vec4_clamp", IR_CLAMP
, 1, 3 },
435 { "vec4_seq", IR_SEQUAL
, 1, 2 },
436 { "vec4_sne", IR_SNEQUAL
, 1, 2 },
437 { "vec4_sge", IR_SGE
, 1, 2 },
438 { "vec4_sgt", IR_SGT
, 1, 2 },
439 { "vec4_sle", IR_SLE
, 1, 2 },
440 { "vec4_slt", IR_SLT
, 1, 2 },
442 { "vec4_move", IR_MOVE
, 1, 1 },
443 { "vec4_floor", IR_FLOOR
, 1, 1 },
444 { "vec4_frac", IR_FRAC
, 1, 1 },
445 { "vec4_abs", IR_ABS
, 1, 1 },
446 { "vec4_negate", IR_NEG
, 1, 1 },
447 { "vec4_ddx", IR_DDX
, 1, 1 },
448 { "vec4_ddy", IR_DDY
, 1, 1 },
449 /* float binary op */
450 { "float_power", IR_POW
, 1, 2 },
451 /* texture / sampler */
452 { "vec4_tex1d", IR_TEX
, 1, 2 },
453 { "vec4_texb1d", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
454 { "vec4_texp1d", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
455 { "vec4_tex2d", IR_TEX
, 1, 2 },
456 { "vec4_texb2d", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
457 { "vec4_texp2d", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
458 { "vec4_tex3d", IR_TEX
, 1, 2 },
459 { "vec4_texb3d", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
460 { "vec4_texp3d", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
461 { "vec4_texcube", IR_TEX
, 1, 2 }, /* cubemap */
462 { "vec4_tex_rect", IR_TEX
, 1, 2 }, /* rectangle */
463 { "vec4_texp_rect", IR_TEX
, 1, 2 },/* rectangle w/ projection */
466 { "ivec4_to_vec4", IR_I_TO_F
, 1, 1 }, /* int[4] to float[4] */
467 { "vec4_to_ivec4", IR_F_TO_I
, 1, 1 }, /* float[4] to int[4] */
468 { "float_exp", IR_EXP
, 1, 1 },
469 { "float_exp2", IR_EXP2
, 1, 1 },
470 { "float_log2", IR_LOG2
, 1, 1 },
471 { "float_rsq", IR_RSQ
, 1, 1 },
472 { "float_rcp", IR_RCP
, 1, 1 },
473 { "float_sine", IR_SIN
, 1, 1 },
474 { "float_cosine", IR_COS
, 1, 1 },
475 { "float_noise1", IR_NOISE1
, 1, 1},
476 { "float_noise2", IR_NOISE2
, 1, 1},
477 { "float_noise3", IR_NOISE3
, 1, 1},
478 { "float_noise4", IR_NOISE4
, 1, 1},
480 { NULL
, IR_NOP
, 0, 0 }
484 static slang_ir_node
*
485 new_node3(slang_ir_opcode op
,
486 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
488 slang_ir_node
*n
= (slang_ir_node
*) _slang_alloc(sizeof(slang_ir_node
));
494 n
->InstLocation
= -1;
499 static slang_ir_node
*
500 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
502 return new_node3(op
, c0
, c1
, NULL
);
505 static slang_ir_node
*
506 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
508 return new_node3(op
, c0
, NULL
, NULL
);
511 static slang_ir_node
*
512 new_node0(slang_ir_opcode op
)
514 return new_node3(op
, NULL
, NULL
, NULL
);
519 * Create sequence of two nodes.
521 static slang_ir_node
*
522 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
528 return new_node2(IR_SEQ
, left
, right
);
531 static slang_ir_node
*
532 new_label(slang_label
*label
)
534 slang_ir_node
*n
= new_node0(IR_LABEL
);
541 static slang_ir_node
*
542 new_float_literal(const float v
[4], GLuint size
)
544 slang_ir_node
*n
= new_node0(IR_FLOAT
);
546 COPY_4V(n
->Value
, v
);
547 /* allocate a storage object, but compute actual location (Index) later */
548 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
553 static slang_ir_node
*
554 new_not(slang_ir_node
*n
)
556 return new_node1(IR_NOT
, n
);
561 * Non-inlined function call.
563 static slang_ir_node
*
564 new_function_call(slang_ir_node
*code
, slang_label
*name
)
566 slang_ir_node
*n
= new_node1(IR_CALL
, code
);
575 * Unconditional jump.
577 static slang_ir_node
*
578 new_return(slang_label
*dest
)
580 slang_ir_node
*n
= new_node0(IR_RETURN
);
588 static slang_ir_node
*
589 new_loop(slang_ir_node
*body
)
591 return new_node1(IR_LOOP
, body
);
595 static slang_ir_node
*
596 new_break(slang_ir_node
*loopNode
)
598 slang_ir_node
*n
= new_node0(IR_BREAK
);
600 assert(loopNode
->Opcode
== IR_LOOP
);
602 /* insert this node at head of linked list */
603 n
->List
= loopNode
->List
;
611 * Make new IR_BREAK_IF_TRUE.
613 static slang_ir_node
*
614 new_break_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
618 assert(loopNode
->Opcode
== IR_LOOP
);
619 n
= new_node1(IR_BREAK_IF_TRUE
, cond
);
621 /* insert this node at head of linked list */
622 n
->List
= loopNode
->List
;
630 * Make new IR_CONT_IF_TRUE node.
632 static slang_ir_node
*
633 new_cont_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
637 assert(loopNode
->Opcode
== IR_LOOP
);
638 n
= new_node1(IR_CONT_IF_TRUE
, cond
);
640 /* insert this node at head of linked list */
641 n
->List
= loopNode
->List
;
648 static slang_ir_node
*
649 new_cond(slang_ir_node
*n
)
651 slang_ir_node
*c
= new_node1(IR_COND
, n
);
656 static slang_ir_node
*
657 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
659 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
664 * New IR_VAR node - a reference to a previously declared variable.
666 static slang_ir_node
*
667 new_var(slang_assemble_ctx
*A
, slang_variable
*var
)
673 assert(var
->declared
);
675 n
= new_node0(IR_VAR
);
677 _slang_attach_storage(n
, var
);
679 printf("new_var %s store=%p\n", (char*)name, (void*) n->Store);
687 * Check if the given function is really just a wrapper for a
688 * basic assembly instruction.
691 slang_is_asm_function(const slang_function
*fun
)
693 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
694 fun
->body
->num_children
== 1 &&
695 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
703 _slang_is_noop(const slang_operation
*oper
)
706 oper
->type
== SLANG_OPER_VOID
||
707 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
715 * Recursively search tree for a node of the given type.
717 static slang_operation
*
718 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
721 if (oper
->type
== type
)
723 for (i
= 0; i
< oper
->num_children
; i
++) {
724 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
733 * Count the number of operations of the given time rooted at 'oper'.
736 _slang_count_node_type(slang_operation
*oper
, slang_operation_type type
)
739 if (oper
->type
== type
) {
742 for (i
= 0; i
< oper
->num_children
; i
++) {
743 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
750 * Check if the 'return' statement found under 'oper' is a "tail return"
751 * that can be no-op'd. For example:
756 * return; // this is a no-op
759 * This is used when determining if a function can be inlined. If the
760 * 'return' is not the last statement, we can't inline the function since
761 * we still need the semantic behaviour of the 'return' but we don't want
762 * to accidentally return from the _calling_ function. We'd need to use an
763 * unconditional branch, but we don't have such a GPU instruction (not
767 _slang_is_tail_return(const slang_operation
*oper
)
769 GLuint k
= oper
->num_children
;
772 const slang_operation
*last
= &oper
->children
[k
- 1];
773 if (last
->type
== SLANG_OPER_RETURN
)
775 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
776 last
->type
== SLANG_OPER_LABEL
)
777 k
--; /* try prev child */
778 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
779 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
780 /* try sub-children */
781 return _slang_is_tail_return(last
);
791 slang_resolve_variable(slang_operation
*oper
)
793 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
794 oper
->var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
800 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
803 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
804 GLuint substCount
, slang_variable
**substOld
,
805 slang_operation
**substNew
, GLboolean isLHS
)
807 switch (oper
->type
) {
808 case SLANG_OPER_VARIABLE_DECL
:
810 slang_variable
*v
= _slang_variable_locate(oper
->locals
,
811 oper
->a_id
, GL_TRUE
);
813 if (v
->initializer
&& oper
->num_children
== 0) {
814 /* set child of oper to copy of initializer */
815 oper
->num_children
= 1;
816 oper
->children
= slang_operation_new(1);
817 slang_operation_copy(&oper
->children
[0], v
->initializer
);
819 if (oper
->num_children
== 1) {
820 /* the initializer */
821 slang_substitute(A
, &oper
->children
[0], substCount
,
822 substOld
, substNew
, GL_FALSE
);
826 case SLANG_OPER_IDENTIFIER
:
827 assert(oper
->num_children
== 0);
828 if (1/**!isLHS XXX FIX */) {
829 slang_atom id
= oper
->a_id
;
832 v
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
834 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
838 /* look for a substitution */
839 for (i
= 0; i
< substCount
; i
++) {
840 if (v
== substOld
[i
]) {
841 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
842 #if 0 /* DEBUG only */
843 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
844 assert(substNew
[i
]->var
);
845 assert(substNew
[i
]->var
->a_name
);
846 printf("Substitute %s with %s in id node %p\n",
847 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
851 printf("Substitute %s with %f in id node %p\n",
852 (char*)v
->a_name
, substNew
[i
]->literal
[0],
856 slang_operation_copy(oper
, substNew
[i
]);
863 case SLANG_OPER_RETURN
:
864 /* do return replacement here too */
865 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
866 if (oper
->num_children
== 1 && !_slang_is_noop(&oper
->children
[0])) {
872 * then do substitutions on the assignment.
874 slang_operation
*blockOper
, *assignOper
, *returnOper
;
876 /* check if function actually has a return type */
877 assert(A
->CurFunction
);
878 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
879 slang_info_log_error(A
->log
, "illegal return expression");
883 blockOper
= slang_operation_new(1);
884 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
885 blockOper
->num_children
= 2;
886 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
887 blockOper
->children
= slang_operation_new(2);
888 assignOper
= blockOper
->children
+ 0;
889 returnOper
= blockOper
->children
+ 1;
891 assignOper
->type
= SLANG_OPER_ASSIGN
;
892 assignOper
->num_children
= 2;
893 assignOper
->locals
->outer_scope
= blockOper
->locals
;
894 assignOper
->children
= slang_operation_new(2);
895 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
896 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
897 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
899 slang_operation_copy(&assignOper
->children
[1],
902 returnOper
->type
= SLANG_OPER_RETURN
; /* return w/ no value */
903 assert(returnOper
->num_children
== 0);
905 /* do substitutions on the "__retVal = expr" sub-tree */
906 slang_substitute(A
, assignOper
,
907 substCount
, substOld
, substNew
, GL_FALSE
);
909 /* install new code */
910 slang_operation_copy(oper
, blockOper
);
911 slang_operation_destruct(blockOper
);
914 /* check if return value was expected */
915 assert(A
->CurFunction
);
916 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
917 slang_info_log_error(A
->log
, "return statement requires an expression");
923 case SLANG_OPER_ASSIGN
:
924 case SLANG_OPER_SUBSCRIPT
:
926 * child[0] can't have substitutions but child[1] can.
928 slang_substitute(A
, &oper
->children
[0],
929 substCount
, substOld
, substNew
, GL_TRUE
);
930 slang_substitute(A
, &oper
->children
[1],
931 substCount
, substOld
, substNew
, GL_FALSE
);
933 case SLANG_OPER_FIELD
:
935 slang_substitute(A
, &oper
->children
[0],
936 substCount
, substOld
, substNew
, GL_TRUE
);
941 for (i
= 0; i
< oper
->num_children
; i
++)
942 slang_substitute(A
, &oper
->children
[i
],
943 substCount
, substOld
, substNew
, GL_FALSE
);
950 * Produce inline code for a call to an assembly instruction.
951 * This is typically used to compile a call to a built-in function like this:
953 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
955 * __asm vec4_lrp __retVal, a, y, x;
960 * r = mix(p1, p2, p3);
970 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
972 static slang_operation
*
973 slang_inline_asm_function(slang_assemble_ctx
*A
,
974 slang_function
*fun
, slang_operation
*oper
)
976 const GLuint numArgs
= oper
->num_children
;
978 slang_operation
*inlined
;
979 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
980 slang_variable
**substOld
;
981 slang_operation
**substNew
;
983 ASSERT(slang_is_asm_function(fun
));
984 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
987 printf("Inline %s as %s\n",
988 (char*) fun->header.a_name,
989 (char*) fun->body->children[0].a_id);
993 * We'll substitute formal params with actual args in the asm call.
995 substOld
= (slang_variable
**)
996 _slang_alloc(numArgs
* sizeof(slang_variable
*));
997 substNew
= (slang_operation
**)
998 _slang_alloc(numArgs
* sizeof(slang_operation
*));
999 for (i
= 0; i
< numArgs
; i
++) {
1000 substOld
[i
] = fun
->parameters
->variables
[i
];
1001 substNew
[i
] = oper
->children
+ i
;
1004 /* make a copy of the code to inline */
1005 inlined
= slang_operation_new(1);
1006 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
1008 /* get rid of the __retVal child */
1009 inlined
->num_children
--;
1010 for (i
= 0; i
< inlined
->num_children
; i
++) {
1011 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1015 /* now do formal->actual substitutions */
1016 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1018 _slang_free(substOld
);
1019 _slang_free(substNew
);
1022 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1023 (char *) fun
->header
.a_name
);
1024 slang_print_tree(inlined
, 3);
1025 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1033 * Inline the given function call operation.
1034 * Return a new slang_operation that corresponds to the inlined code.
1036 static slang_operation
*
1037 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1038 slang_operation
*oper
, slang_operation
*returnOper
)
1045 ParamMode
*paramMode
;
1046 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1047 const GLuint numArgs
= oper
->num_children
;
1048 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1049 slang_operation
*args
= oper
->children
;
1050 slang_operation
*inlined
, *top
;
1051 slang_variable
**substOld
;
1052 slang_operation
**substNew
;
1053 GLuint substCount
, numCopyIn
, i
;
1054 slang_function
*prevFunction
;
1055 slang_variable_scope
*newScope
= NULL
;
1058 prevFunction
= A
->CurFunction
;
1059 A
->CurFunction
= fun
;
1061 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1062 assert(fun
->param_count
== totalArgs
);
1064 /* allocate temporary arrays */
1065 paramMode
= (ParamMode
*)
1066 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1067 substOld
= (slang_variable
**)
1068 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1069 substNew
= (slang_operation
**)
1070 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1073 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1074 (char *) fun
->header
.a_name
,
1075 fun
->parameters
->num_variables
, numArgs
);
1078 if (haveRetValue
&& !returnOper
) {
1079 /* Create 3-child comma sequence for inlined code:
1080 * child[0]: declare __resultTmp
1081 * child[1]: inlined function body
1082 * child[2]: __resultTmp
1084 slang_operation
*commaSeq
;
1085 slang_operation
*declOper
= NULL
;
1086 slang_variable
*resultVar
;
1088 commaSeq
= slang_operation_new(1);
1089 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1090 assert(commaSeq
->locals
);
1091 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1092 commaSeq
->num_children
= 3;
1093 commaSeq
->children
= slang_operation_new(3);
1094 /* allocate the return var */
1095 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1097 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1098 (void*)commaSeq->locals, (char *) fun->header.a_name);
1101 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1102 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1103 resultVar
->isTemp
= GL_TRUE
;
1105 /* child[0] = __resultTmp declaration */
1106 declOper
= &commaSeq
->children
[0];
1107 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1108 declOper
->a_id
= resultVar
->a_name
;
1109 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1111 /* child[1] = function body */
1112 inlined
= &commaSeq
->children
[1];
1113 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1115 /* child[2] = __resultTmp reference */
1116 returnOper
= &commaSeq
->children
[2];
1117 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1118 returnOper
->a_id
= resultVar
->a_name
;
1119 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1124 top
= inlined
= slang_operation_new(1);
1125 /* XXXX this may be inappropriate!!!! */
1126 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1130 assert(inlined
->locals
);
1132 /* Examine the parameters, look for inout/out params, look for possible
1133 * substitutions, etc:
1134 * param type behaviour
1135 * in copy actual to local
1136 * const in substitute param with actual
1140 for (i
= 0; i
< totalArgs
; i
++) {
1141 slang_variable
*p
= fun
->parameters
->variables
[i
];
1143 printf("Param %d: %s %s \n", i,
1144 slang_type_qual_string(p->type.qualifier),
1145 (char *) p->a_name);
1147 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1148 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1149 /* an output param */
1150 slang_operation
*arg
;
1155 paramMode
[i
] = SUBST
;
1157 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1158 slang_resolve_variable(arg
);
1160 /* replace parameter 'p' with argument 'arg' */
1161 substOld
[substCount
] = p
;
1162 substNew
[substCount
] = arg
; /* will get copied */
1165 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1166 /* a constant input param */
1167 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1168 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
1169 /* replace all occurances of this parameter variable with the
1170 * actual argument variable or a literal.
1172 paramMode
[i
] = SUBST
;
1173 slang_resolve_variable(&args
[i
]);
1174 substOld
[substCount
] = p
;
1175 substNew
[substCount
] = &args
[i
]; /* will get copied */
1179 paramMode
[i
] = COPY_IN
;
1183 paramMode
[i
] = COPY_IN
;
1185 assert(paramMode
[i
]);
1188 /* actual code inlining: */
1189 slang_operation_copy(inlined
, fun
->body
);
1191 /*** XXX review this */
1192 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1193 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1194 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1197 printf("======================= orig body code ======================\n");
1198 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1199 slang_print_tree(fun
->body
, 8);
1200 printf("======================= copied code =========================\n");
1201 slang_print_tree(inlined
, 8);
1204 /* do parameter substitution in inlined code: */
1205 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1208 printf("======================= subst code ==========================\n");
1209 slang_print_tree(inlined
, 8);
1210 printf("=============================================================\n");
1213 /* New prolog statements: (inserted before the inlined code)
1214 * Copy the 'in' arguments.
1217 for (i
= 0; i
< numArgs
; i
++) {
1218 if (paramMode
[i
] == COPY_IN
) {
1219 slang_variable
*p
= fun
->parameters
->variables
[i
];
1220 /* declare parameter 'p' */
1221 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1225 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1226 assert(decl
->locals
);
1227 decl
->locals
->outer_scope
= inlined
->locals
;
1228 decl
->a_id
= p
->a_name
;
1229 decl
->num_children
= 1;
1230 decl
->children
= slang_operation_new(1);
1232 /* child[0] is the var's initializer */
1233 slang_operation_copy(&decl
->children
[0], args
+ i
);
1235 /* add parameter 'p' to the local variable scope here */
1237 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1238 pCopy
->type
= p
->type
;
1239 pCopy
->a_name
= p
->a_name
;
1240 pCopy
->array_len
= p
->array_len
;
1243 newScope
= inlined
->locals
;
1248 /* Now add copies of the function's local vars to the new variable scope */
1249 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1250 slang_variable
*p
= fun
->parameters
->variables
[i
];
1251 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1252 pCopy
->type
= p
->type
;
1253 pCopy
->a_name
= p
->a_name
;
1254 pCopy
->array_len
= p
->array_len
;
1258 /* New epilog statements:
1259 * 1. Create end of function label to jump to from return statements.
1260 * 2. Copy the 'out' parameter vars
1263 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1265 inlined
->num_children
);
1266 lab
->type
= SLANG_OPER_LABEL
;
1267 lab
->label
= A
->curFuncEndLabel
;
1270 for (i
= 0; i
< totalArgs
; i
++) {
1271 if (paramMode
[i
] == COPY_OUT
) {
1272 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1273 /* actualCallVar = outParam */
1274 /*if (i > 0 || !haveRetValue)*/
1275 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1277 inlined
->num_children
);
1278 ass
->type
= SLANG_OPER_ASSIGN
;
1279 ass
->num_children
= 2;
1280 ass
->locals
->outer_scope
= inlined
->locals
;
1281 ass
->children
= slang_operation_new(2);
1282 ass
->children
[0] = args
[i
]; /*XXX copy */
1283 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1284 ass
->children
[1].a_id
= p
->a_name
;
1285 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1289 _slang_free(paramMode
);
1290 _slang_free(substOld
);
1291 _slang_free(substNew
);
1293 /* Update scoping to use the new local vars instead of the
1294 * original function's vars. This is especially important
1295 * for nested inlining.
1298 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1301 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1302 (char *) fun
->header
.a_name
,
1303 fun
->parameters
->num_variables
, numArgs
);
1304 slang_print_tree(top
, 0);
1308 A
->CurFunction
= prevFunction
;
1314 static slang_ir_node
*
1315 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1316 slang_operation
*oper
, slang_operation
*dest
)
1319 slang_operation
*inlined
;
1320 slang_label
*prevFuncEndLabel
;
1323 prevFuncEndLabel
= A
->curFuncEndLabel
;
1324 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1325 A
->curFuncEndLabel
= _slang_label_new(name
);
1326 assert(A
->curFuncEndLabel
);
1328 if (slang_is_asm_function(fun
) && !dest
) {
1329 /* assemble assembly function - tree style */
1330 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1333 /* non-assembly function */
1334 /* We always generate an "inline-able" block of code here.
1336 * 1. insert the inline code
1337 * 2. Generate a call to the "inline" code as a subroutine
1341 slang_operation
*ret
= NULL
;
1343 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1347 ret
= _slang_find_node_type(inlined
, SLANG_OPER_RETURN
);
1349 /* check if this is a "tail" return */
1350 if (_slang_count_node_type(inlined
, SLANG_OPER_RETURN
) == 1 &&
1351 _slang_is_tail_return(inlined
)) {
1352 /* The only RETURN is the last stmt in the function, no-op it
1353 * and inline the function body.
1355 ret
->type
= SLANG_OPER_NONE
;
1358 slang_operation
*callOper
;
1359 /* The function we're calling has one or more 'return' statements.
1360 * So, we can't truly inline this function because we need to
1361 * implement 'return' with RET (and CAL).
1362 * Nevertheless, we performed "inlining" to make a new instance
1363 * of the function body to deal with static register allocation.
1365 * XXX check if there's one 'return' and if it's the very last
1366 * statement in the function - we can optimize that case.
1368 assert(inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1369 inlined
->type
== SLANG_OPER_SEQUENCE
);
1371 if (_slang_function_has_return_value(fun
) && !dest
) {
1372 assert(inlined
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1373 assert(inlined
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1374 callOper
= &inlined
->children
[1];
1379 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1380 callOper
->fun
= fun
;
1381 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1389 /* Replace the function call with the inlined block (or new CALL stmt) */
1390 slang_operation_destruct(oper
);
1392 _slang_free(inlined
);
1395 assert(inlined
->locals
);
1396 printf("*** Inlined code for call to %s:\n",
1397 (char*) fun
->header
.a_name
);
1398 slang_print_tree(oper
, 10);
1402 n
= _slang_gen_operation(A
, oper
);
1404 /*_slang_label_delete(A->curFuncEndLabel);*/
1405 A
->curFuncEndLabel
= prevFuncEndLabel
;
1411 static slang_asm_info
*
1412 slang_find_asm_info(const char *name
)
1415 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1416 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1425 * Return the default swizzle mask for accessing a variable of the
1426 * given size (in floats). If size = 1, comp is used to identify
1427 * which component [0..3] of the register holds the variable.
1430 _slang_var_swizzle(GLint size
, GLint comp
)
1434 return MAKE_SWIZZLE4(comp
, comp
, comp
, comp
);
1436 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_NIL
, SWIZZLE_NIL
);
1438 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_NIL
);
1440 return SWIZZLE_XYZW
;
1446 * Some write-masked assignments are simple, but others are hard.
1449 * v.xy = vec2(a, b);
1452 * v.zy = vec2(a, b);
1453 * this gets transformed/swizzled into:
1454 * v.zy = vec2(a, b).*yx* (* = don't care)
1455 * This function helps to determine simple vs. non-simple.
1458 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1460 switch (writemask
) {
1462 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1464 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1466 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1468 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1470 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1471 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1473 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1474 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1475 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1476 case WRITEMASK_XYZW
:
1477 return swizzle
== SWIZZLE_NOOP
;
1485 * Convert the given swizzle into a writemask. In some cases this
1486 * is trivial, in other cases, we'll need to also swizzle the right
1487 * hand side to put components in the right places.
1488 * See comment above for more info.
1489 * XXX this function could be simplified and should probably be renamed.
1490 * \param swizzle the incoming swizzle
1491 * \param writemaskOut returns the writemask
1492 * \param swizzleOut swizzle to apply to the right-hand-side
1493 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1496 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1497 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1499 GLuint mask
= 0x0, newSwizzle
[4];
1502 /* make new dst writemask, compute size */
1503 for (i
= 0; i
< 4; i
++) {
1504 const GLuint swz
= GET_SWZ(swizzle
, i
);
1505 if (swz
== SWIZZLE_NIL
) {
1509 assert(swz
>= 0 && swz
<= 3);
1511 if (swizzle
!= SWIZZLE_XXXX
&&
1512 swizzle
!= SWIZZLE_YYYY
&&
1513 swizzle
!= SWIZZLE_ZZZZ
&&
1514 swizzle
!= SWIZZLE_WWWW
&&
1515 (mask
& (1 << swz
))) {
1516 /* a channel can't be specified twice (ex: ".xyyz") */
1517 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1518 _mesa_swizzle_string(swizzle
, 0, 0));
1524 assert(mask
<= 0xf);
1525 size
= i
; /* number of components in mask/swizzle */
1527 *writemaskOut
= mask
;
1529 /* make new src swizzle, by inversion */
1530 for (i
= 0; i
< 4; i
++) {
1531 newSwizzle
[i
] = i
; /*identity*/
1533 for (i
= 0; i
< size
; i
++) {
1534 const GLuint swz
= GET_SWZ(swizzle
, i
);
1535 newSwizzle
[swz
] = i
;
1537 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1542 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1544 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1546 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1548 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1550 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1559 * Recursively traverse 'oper' to produce a swizzle mask in the event
1560 * of any vector subscripts and swizzle suffixes.
1561 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1564 resolve_swizzle(const slang_operation
*oper
)
1566 if (oper
->type
== SLANG_OPER_FIELD
) {
1567 /* writemask from .xyzw suffix */
1569 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1570 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1574 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1575 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1579 return SWIZZLE_XYZW
;
1581 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1582 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1583 /* writemask from [index] */
1584 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1585 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1590 swizzle
= SWIZZLE_XXXX
;
1593 swizzle
= SWIZZLE_YYYY
;
1596 swizzle
= SWIZZLE_ZZZZ
;
1599 swizzle
= SWIZZLE_WWWW
;
1602 swizzle
= SWIZZLE_XYZW
;
1604 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1608 return SWIZZLE_XYZW
;
1614 * Recursively descend through swizzle nodes to find the node's storage info.
1616 static slang_ir_storage
*
1617 get_store(const slang_ir_node
*n
)
1619 if (n
->Opcode
== IR_SWIZZLE
) {
1620 return get_store(n
->Children
[0]);
1628 * Generate IR tree for an asm instruction/operation such as:
1629 * __asm vec4_dot __retVal.x, v1, v2;
1631 static slang_ir_node
*
1632 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1633 slang_operation
*dest
)
1635 const slang_asm_info
*info
;
1636 slang_ir_node
*kids
[3], *n
;
1637 GLuint j
, firstOperand
;
1639 assert(oper
->type
== SLANG_OPER_ASM
);
1641 info
= slang_find_asm_info((char *) oper
->a_id
);
1643 _mesa_problem(NULL
, "undefined __asm function %s\n",
1644 (char *) oper
->a_id
);
1647 assert(info
->NumParams
<= 3);
1649 if (info
->NumParams
== oper
->num_children
) {
1650 /* Storage for result is not specified.
1651 * Children[0], [1], [2] are the operands.
1656 /* Storage for result (child[0]) is specified.
1657 * Children[1], [2], [3] are the operands.
1662 /* assemble child(ren) */
1663 kids
[0] = kids
[1] = kids
[2] = NULL
;
1664 for (j
= 0; j
< info
->NumParams
; j
++) {
1665 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1670 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1673 /* Setup n->Store to be a particular location. Otherwise, storage
1674 * for the result (a temporary) will be allocated later.
1676 slang_operation
*dest_oper
;
1679 dest_oper
= &oper
->children
[0];
1681 n0
= _slang_gen_operation(A
, dest_oper
);
1686 n
->Store
= n0
->Store
;
1688 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1698 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1701 for (i
= 0; i
< scope
->num_functions
; i
++) {
1702 slang_function
*f
= &scope
->functions
[i
];
1703 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1704 printf(" %s (%d args)\n", name
, f
->param_count
);
1707 if (scope
->outer_scope
)
1708 print_funcs(scope
->outer_scope
, name
);
1713 * Find a function of the given name, taking 'numArgs' arguments.
1714 * This is the function we'll try to call when there is no exact match
1715 * between function parameters and call arguments.
1717 * XXX we should really create a list of candidate functions and try
1720 static slang_function
*
1721 _slang_find_function_by_argc(slang_function_scope
*scope
,
1722 const char *name
, int numArgs
)
1726 for (i
= 0; i
< scope
->num_functions
; i
++) {
1727 slang_function
*f
= &scope
->functions
[i
];
1728 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1729 int haveRetValue
= _slang_function_has_return_value(f
);
1730 if (numArgs
== f
->param_count
- haveRetValue
)
1734 scope
= scope
->outer_scope
;
1741 static slang_function
*
1742 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
1745 slang_function
*maxFunc
= NULL
;
1750 for (i
= 0; i
< scope
->num_functions
; i
++) {
1751 slang_function
*f
= &scope
->functions
[i
];
1752 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1753 if (f
->param_count
> maxArgs
) {
1754 maxArgs
= f
->param_count
;
1759 scope
= scope
->outer_scope
;
1767 * Generate a new slang_function which is a constructor for a user-defined
1770 static slang_function
*
1771 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
1773 const GLint numFields
= str
->fields
->num_variables
;
1774 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1776 /* function header (name, return type) */
1777 fun
->header
.a_name
= str
->a_name
;
1778 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1779 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
1780 fun
->header
.type
.specifier
._struct
= str
;
1782 /* function parameters (= struct's fields) */
1785 for (i
= 0; i
< numFields
; i
++) {
1787 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1789 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1790 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
1791 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1793 fun
->param_count
= fun
->parameters
->num_variables
;
1796 /* Add __retVal to params */
1798 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1799 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1801 p
->a_name
= a_retVal
;
1802 p
->type
= fun
->header
.type
;
1803 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1807 /* function body is:
1817 slang_variable_scope
*scope
;
1818 slang_variable
*var
;
1821 fun
->body
= slang_operation_new(1);
1822 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1823 fun
->body
->num_children
= numFields
+ 2;
1824 fun
->body
->children
= slang_operation_new(numFields
+ 2);
1826 scope
= fun
->body
->locals
;
1827 scope
->outer_scope
= fun
->parameters
;
1829 /* create local var 't' */
1830 var
= slang_variable_scope_grow(scope
);
1831 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
1832 var
->type
= fun
->header
.type
;
1836 slang_operation
*decl
;
1838 decl
= &fun
->body
->children
[0];
1839 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1840 decl
->locals
= _slang_variable_scope_new(scope
);
1841 decl
->a_id
= var
->a_name
;
1844 /* assign params to fields of t */
1845 for (i
= 0; i
< numFields
; i
++) {
1846 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
1848 assign
->type
= SLANG_OPER_ASSIGN
;
1849 assign
->locals
= _slang_variable_scope_new(scope
);
1850 assign
->num_children
= 2;
1851 assign
->children
= slang_operation_new(2);
1854 slang_operation
*lhs
= &assign
->children
[0];
1856 lhs
->type
= SLANG_OPER_FIELD
;
1857 lhs
->locals
= _slang_variable_scope_new(scope
);
1858 lhs
->num_children
= 1;
1859 lhs
->children
= slang_operation_new(1);
1860 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1862 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1863 lhs
->children
[0].a_id
= var
->a_name
;
1864 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
1867 lhs
->children
[1].num_children
= 1;
1868 lhs
->children
[1].children
= slang_operation_new(1);
1869 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
1870 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
1871 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
1876 slang_operation
*rhs
= &assign
->children
[1];
1878 rhs
->type
= SLANG_OPER_IDENTIFIER
;
1879 rhs
->locals
= _slang_variable_scope_new(scope
);
1880 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1886 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
1888 ret
->type
= SLANG_OPER_RETURN
;
1889 ret
->locals
= _slang_variable_scope_new(scope
);
1890 ret
->num_children
= 1;
1891 ret
->children
= slang_operation_new(1);
1892 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1893 ret
->children
[0].a_id
= var
->a_name
;
1894 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
1898 slang_print_function(fun, 1);
1905 * Find/create a function (constructor) for the given structure name.
1907 static slang_function
*
1908 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
1911 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
1912 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
1913 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
1914 /* found a structure type that matches the function name */
1915 if (!str
->constructor
) {
1916 /* create the constructor function now */
1917 str
->constructor
= _slang_make_struct_constructor(A
, str
);
1919 return str
->constructor
;
1927 * Generate a new slang_function to satisfy a call to an array constructor.
1928 * Ex: float[3](1., 2., 3.)
1930 static slang_function
*
1931 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
1933 slang_type_specifier_type baseType
;
1934 slang_function
*fun
;
1937 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1941 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
1943 num_elements
= oper
->num_children
;
1945 /* function header, return type */
1947 fun
->header
.a_name
= oper
->a_id
;
1948 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1949 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
1950 fun
->header
.type
.specifier
._array
=
1951 slang_type_specifier_new(baseType
, NULL
, NULL
);
1952 fun
->header
.type
.array_len
= num_elements
;
1955 /* function parameters (= number of elements) */
1958 for (i
= 0; i
< num_elements
; i
++) {
1960 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1962 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1964 snprintf(name
, sizeof(name
), "p%d", i
);
1965 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
1966 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1967 p
->type
.specifier
.type
= baseType
;
1969 fun
->param_count
= fun
->parameters
->num_variables
;
1972 /* Add __retVal to params */
1974 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1975 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1977 p
->a_name
= a_retVal
;
1978 p
->type
= fun
->header
.type
;
1979 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1980 p
->type
.specifier
.type
= baseType
;
1984 /* function body is:
1994 slang_variable_scope
*scope
;
1995 slang_variable
*var
;
1998 fun
->body
= slang_operation_new(1);
1999 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2000 fun
->body
->num_children
= num_elements
+ 2;
2001 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
2003 scope
= fun
->body
->locals
;
2004 scope
->outer_scope
= fun
->parameters
;
2006 /* create local var 't' */
2007 var
= slang_variable_scope_grow(scope
);
2008 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2009 var
->type
= fun
->header
.type
;/*XXX copy*/
2013 slang_operation
*decl
;
2015 decl
= &fun
->body
->children
[0];
2016 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2017 decl
->locals
= _slang_variable_scope_new(scope
);
2018 decl
->a_id
= var
->a_name
;
2021 /* assign params to elements of t */
2022 for (i
= 0; i
< num_elements
; i
++) {
2023 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2025 assign
->type
= SLANG_OPER_ASSIGN
;
2026 assign
->locals
= _slang_variable_scope_new(scope
);
2027 assign
->num_children
= 2;
2028 assign
->children
= slang_operation_new(2);
2031 slang_operation
*lhs
= &assign
->children
[0];
2033 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2034 lhs
->locals
= _slang_variable_scope_new(scope
);
2035 lhs
->num_children
= 2;
2036 lhs
->children
= slang_operation_new(2);
2038 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2039 lhs
->children
[0].a_id
= var
->a_name
;
2040 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2042 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2043 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2047 slang_operation
*rhs
= &assign
->children
[1];
2049 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2050 rhs
->locals
= _slang_variable_scope_new(scope
);
2051 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2057 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2059 ret
->type
= SLANG_OPER_RETURN
;
2060 ret
->locals
= _slang_variable_scope_new(scope
);
2061 ret
->num_children
= 1;
2062 ret
->children
= slang_operation_new(1);
2063 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2064 ret
->children
[0].a_id
= var
->a_name
;
2065 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2070 slang_print_function(fun, 1);
2078 _slang_is_vec_mat_type(const char *name
)
2080 static const char *vecmat_types
[] = {
2081 "float", "int", "bool",
2082 "vec2", "vec3", "vec4",
2083 "ivec2", "ivec3", "ivec4",
2084 "bvec2", "bvec3", "bvec4",
2085 "mat2", "mat3", "mat4",
2086 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2090 for (i
= 0; vecmat_types
[i
]; i
++)
2091 if (_mesa_strcmp(name
, vecmat_types
[i
]) == 0)
2098 * Assemble a function call, given a particular function name.
2099 * \param name the function's name (operators like '*' are possible).
2101 static slang_ir_node
*
2102 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2103 slang_operation
*oper
, slang_operation
*dest
)
2105 slang_operation
*params
= oper
->children
;
2106 const GLuint param_count
= oper
->num_children
;
2108 slang_function
*fun
;
2111 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2112 if (atom
== SLANG_ATOM_NULL
)
2115 if (oper
->array_constructor
) {
2116 /* this needs special handling */
2117 fun
= _slang_make_array_constructor(A
, oper
);
2120 /* Try to find function by name and exact argument type matching */
2121 GLboolean error
= GL_FALSE
;
2122 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2123 &A
->space
, A
->atoms
, A
->log
, &error
);
2125 slang_info_log_error(A
->log
,
2126 "Function '%s' not found (check argument types)",
2133 /* Next, try locating a constructor function for a user-defined type */
2134 fun
= _slang_locate_struct_constructor(A
, name
);
2138 * At this point, some heuristics are used to try to find a function
2139 * that matches the calling signature by means of casting or "unrolling"
2143 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2144 /* Next, if this call looks like a vec() or mat() constructor call,
2145 * try "unwinding" the args to satisfy a constructor.
2147 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2149 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2150 slang_info_log_error(A
->log
,
2151 "Function '%s' not found (check argument types)",
2158 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2159 /* Next, try casting args to the types of the formal parameters */
2160 int numArgs
= oper
->num_children
;
2161 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2162 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2163 slang_info_log_error(A
->log
,
2164 "Function '%s' not found (check argument types)",
2172 slang_info_log_error(A
->log
,
2173 "Function '%s' not found (check argument types)",
2178 slang_info_log_error(A
->log
,
2179 "Function '%s' prototyped but not defined. "
2180 "Separate compilation units not supported.",
2185 /* type checking to be sure function's return type matches 'dest' type */
2189 slang_typeinfo_construct(&t0
);
2190 typeof_operation(A
, dest
, &t0
);
2192 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2193 slang_info_log_error(A
->log
,
2194 "Incompatible type returned by call to '%s'",
2200 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2202 if (n
&& !n
->Store
&& !dest
2203 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2204 /* setup n->Store for the result of the function call */
2205 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2206 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2207 /*printf("Alloc storage for function result, size %d \n", size);*/
2210 if (oper
->array_constructor
) {
2211 /* free the temporary array constructor function now */
2212 slang_function_destruct(fun
);
2219 static slang_ir_node
*
2220 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2222 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2224 slang_variable
*var
;
2226 /* NOTE: In GLSL 1.20, there's only one kind of method
2227 * call: array.length(). Anything else is an error.
2229 if (oper
->a_id
!= a_length
) {
2230 slang_info_log_error(A
->log
,
2231 "Undefined method call '%s'", (char *) oper
->a_id
);
2235 /* length() takes no arguments */
2236 if (oper
->num_children
> 0) {
2237 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2241 /* lookup the object/variable */
2242 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2243 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2244 slang_info_log_error(A
->log
,
2245 "Undefined object '%s'", (char *) oper
->a_obj
);
2249 /* Create a float/literal IR node encoding the array length */
2250 n
= new_node0(IR_FLOAT
);
2252 n
->Value
[0] = (float) var
->array_len
;
2253 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2260 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2262 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2263 oper
->type
== SLANG_OPER_LITERAL_INT
||
2264 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2265 if (oper
->literal
[0])
2271 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2272 oper
->num_children
== 1) {
2273 return _slang_is_constant_cond(&oper
->children
[0], value
);
2280 * Test if an operation is a scalar or boolean.
2283 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2285 slang_typeinfo type
;
2288 slang_typeinfo_construct(&type
);
2289 typeof_operation(A
, oper
, &type
);
2290 size
= _slang_sizeof_type_specifier(&type
.spec
);
2291 slang_typeinfo_destruct(&type
);
2297 * Test if an operation is boolean.
2300 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2302 slang_typeinfo type
;
2305 slang_typeinfo_construct(&type
);
2306 typeof_operation(A
, oper
, &type
);
2307 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2308 slang_typeinfo_destruct(&type
);
2314 * Generate loop code using high-level IR_LOOP instruction
2316 static slang_ir_node
*
2317 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2321 * BREAK if !expr (child[0])
2322 * body code (child[1])
2324 slang_ir_node
*prevLoop
, *loop
, *breakIf
, *body
;
2325 GLboolean isConst
, constTrue
;
2327 /* type-check expression */
2328 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2329 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2333 /* Check if loop condition is a constant */
2334 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2336 if (isConst
&& !constTrue
) {
2337 /* loop is never executed! */
2338 return new_node0(IR_NOP
);
2341 loop
= new_loop(NULL
);
2343 /* save old, push new loop */
2344 prevLoop
= A
->CurLoop
;
2347 if (isConst
&& constTrue
) {
2348 /* while(nonzero constant), no conditional break */
2353 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2354 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2356 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2357 loop
->Children
[0] = new_seq(breakIf
, body
);
2359 /* Do infinite loop detection */
2360 /* loop->List is head of linked list of break/continue nodes */
2361 if (!loop
->List
&& isConst
&& constTrue
) {
2362 /* infinite loop detected */
2363 A
->CurLoop
= prevLoop
; /* clean-up */
2364 slang_info_log_error(A
->log
, "Infinite loop detected!");
2368 /* pop loop, restore prev */
2369 A
->CurLoop
= prevLoop
;
2376 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2378 static slang_ir_node
*
2379 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2383 * body code (child[0])
2385 * BREAK if !expr (child[1])
2387 slang_ir_node
*prevLoop
, *loop
;
2388 GLboolean isConst
, constTrue
;
2390 /* type-check expression */
2391 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2392 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2396 loop
= new_loop(NULL
);
2398 /* save old, push new loop */
2399 prevLoop
= A
->CurLoop
;
2403 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
2405 /* Check if loop condition is a constant */
2406 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
2407 if (isConst
&& constTrue
) {
2408 /* do { } while(1) ==> no conditional break */
2409 loop
->Children
[1] = NULL
; /* no tail code */
2413 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2414 loop
->Children
[1] = new_break_if_true(A
->CurLoop
, cond
);
2417 /* XXX we should do infinite loop detection, as above */
2419 /* pop loop, restore prev */
2420 A
->CurLoop
= prevLoop
;
2427 * Generate for-loop using high-level IR_LOOP instruction.
2429 static slang_ir_node
*
2430 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2433 * init code (child[0])
2435 * BREAK if !expr (child[1])
2436 * body code (child[3])
2438 * incr code (child[2]) // XXX continue here
2440 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
2442 init
= _slang_gen_operation(A
, &oper
->children
[0]);
2443 loop
= new_loop(NULL
);
2445 /* save old, push new loop */
2446 prevLoop
= A
->CurLoop
;
2449 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2450 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2451 body
= _slang_gen_operation(A
, &oper
->children
[3]);
2452 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
2454 loop
->Children
[0] = new_seq(breakIf
, body
);
2455 loop
->Children
[1] = incr
; /* tail code */
2457 /* pop loop, restore prev */
2458 A
->CurLoop
= prevLoop
;
2460 return new_seq(init
, loop
);
2464 static slang_ir_node
*
2465 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2467 slang_ir_node
*n
, *loopNode
;
2468 assert(oper
->type
== SLANG_OPER_CONTINUE
);
2469 loopNode
= A
->CurLoop
;
2471 assert(loopNode
->Opcode
== IR_LOOP
);
2472 n
= new_node0(IR_CONT
);
2474 n
->Parent
= loopNode
;
2475 /* insert this node at head of linked list */
2476 n
->List
= loopNode
->List
;
2484 * Determine if the given operation is of a specific type.
2487 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
2489 if (oper
->type
== type
)
2491 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
2492 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
2493 oper
->num_children
== 1)
2494 return is_operation_type(&oper
->children
[0], type
);
2501 * Generate IR tree for an if/then/else conditional using high-level
2502 * IR_IF instruction.
2504 static slang_ir_node
*
2505 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2508 * eval expr (child[0])
2515 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
2516 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
2517 GLboolean isConst
, constTrue
;
2519 /* type-check expression */
2520 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2521 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
2525 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2526 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
2530 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2534 return _slang_gen_operation(A
, &oper
->children
[1]);
2537 /* if (false) ... */
2538 return _slang_gen_operation(A
, &oper
->children
[2]);
2542 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2543 cond
= new_cond(cond
);
2545 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
2546 && !haveElseClause
) {
2547 /* Special case: generate a conditional break */
2548 ifBody
= new_break_if_true(A
->CurLoop
, cond
);
2551 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
2552 && !haveElseClause
) {
2553 /* Special case: generate a conditional break */
2554 ifBody
= new_cont_if_true(A
->CurLoop
, cond
);
2559 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
2561 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
2564 ifNode
= new_if(cond
, ifBody
, elseBody
);
2571 static slang_ir_node
*
2572 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2576 assert(oper
->type
== SLANG_OPER_NOT
);
2578 /* type-check expression */
2579 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2580 slang_info_log_error(A
->log
,
2581 "scalar/boolean expression expected for '!'");
2585 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2593 static slang_ir_node
*
2594 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2596 slang_ir_node
*n1
, *n2
;
2598 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
2600 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
2601 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2602 slang_info_log_error(A
->log
,
2603 "scalar/boolean expressions expected for '^^'");
2607 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
2610 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
2613 return new_node2(IR_NOTEQUAL
, n1
, n2
);
2618 * Generate IR node for storage of a temporary of given size.
2620 static slang_ir_node
*
2621 _slang_gen_temporary(GLint size
)
2623 slang_ir_storage
*store
;
2624 slang_ir_node
*n
= NULL
;
2626 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
2628 n
= new_node0(IR_VAR_DECL
);
2641 * Generate IR node for allocating/declaring a variable.
2642 * \param initializer Optional initializer expression for the variable.
2644 static slang_ir_node
*
2645 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
2646 slang_operation
*initializer
)
2648 slang_ir_node
*varDecl
, *n
;
2649 slang_ir_storage
*store
;
2651 /*assert(!var->declared);*/
2652 var
->declared
= GL_TRUE
;
2654 varDecl
= new_node0(IR_VAR_DECL
);
2658 _slang_attach_storage(varDecl
, var
);
2660 assert(varDecl
->Store
== var
->store
);
2661 assert(varDecl
->Store
);
2662 assert(varDecl
->Store
->Index
< 0);
2665 assert(store
== varDecl
->Store
);
2667 /* determine GPU storage file */
2668 /* XXX if the variable is const, use PROGRAM_CONSTANT */
2669 if (is_sampler_type(&var
->type
)) {
2670 store
->File
= PROGRAM_SAMPLER
;
2673 store
->File
= PROGRAM_TEMPORARY
;
2676 store
->Size
= _slang_sizeof_type_specifier(&varDecl
->Var
->type
.specifier
);
2678 if (store
->Size
<= 0) {
2679 slang_info_log_error(A
->log
, "invalid declaration for '%s'",
2680 (char*) var
->a_name
);
2685 printf("%s var %p %s store=%p index=%d size=%d\n",
2686 __FUNCTION__
, (void *) var
, (char *) var
->a_name
,
2687 (void *) store
, store
->Index
, store
->Size
);
2690 if (var
->type
.array_len
> 0) {
2691 /* the type is an array, ex: float[4] x; */
2692 GLint sz
= (store
->Size
+ 3) & ~3;
2693 /* total size = element size * array length */
2694 sz
*= var
->type
.array_len
;
2698 if (var
->array_len
> 0) {
2699 /* this is an array */
2700 /* round up the element size to a multiple of 4 */
2701 GLint sz
= (store
->Size
+ 3) & ~3;
2702 /* total size = element size * array length */
2703 sz
*= var
->array_len
;
2707 /* if there's an initializer, generate IR for the expression */
2709 const char *varName
= (const char *) var
->a_name
;
2710 slang_ir_node
*varRef
, *init
;
2712 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
2713 /* if the variable is const, the initializer must be a const
2714 * expression as well.
2717 if (!_slang_is_constant_expr(initializer
)) {
2718 slang_info_log_error(A
->log
,
2719 "initializer for %s not constant", varName
);
2725 /* constant-folding, etc here */
2726 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
2728 /* IR for initializer */
2729 init
= _slang_gen_operation(A
, initializer
);
2733 /* IR for the variable we're initializing */
2734 varRef
= new_var(A
, var
);
2736 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
2740 /* XXX remove this when type checking is added above */
2741 if (init
->Store
&& varRef
->Store
->Size
!= init
->Store
->Size
) {
2742 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
2746 /* assign RHS to LHS */
2747 n
= new_node2(IR_COPY
, varRef
, init
);
2748 n
= new_seq(varDecl
, n
);
2751 /* no initializer */
2760 * Generate code for a selection expression: b ? x : y
2761 * XXX In some cases we could implement a selection expression
2762 * with an LRP instruction (use the boolean as the interpolant).
2763 * Otherwise, we use an IF/ELSE/ENDIF construct.
2765 static slang_ir_node
*
2766 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
2768 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
2769 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
2770 slang_typeinfo type0
, type1
, type2
;
2771 int size
, isBool
, isEqual
;
2773 assert(oper
->type
== SLANG_OPER_SELECT
);
2774 assert(oper
->num_children
== 3);
2776 /* type of children[0] must be boolean */
2777 slang_typeinfo_construct(&type0
);
2778 typeof_operation(A
, &oper
->children
[0], &type0
);
2779 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
2780 slang_typeinfo_destruct(&type0
);
2782 slang_info_log_error(A
->log
, "selector type is not boolean");
2786 slang_typeinfo_construct(&type1
);
2787 slang_typeinfo_construct(&type2
);
2788 typeof_operation(A
, &oper
->children
[1], &type1
);
2789 typeof_operation(A
, &oper
->children
[2], &type2
);
2790 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
2791 slang_typeinfo_destruct(&type1
);
2792 slang_typeinfo_destruct(&type2
);
2794 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
2798 /* size of x or y's type */
2799 size
= _slang_sizeof_type_specifier(&type1
.spec
);
2803 tmpDecl
= _slang_gen_temporary(size
);
2805 /* the condition (child 0) */
2806 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2807 cond
= new_cond(cond
);
2809 /* if-true body (child 1) */
2810 tmpVar
= new_node0(IR_VAR
);
2811 tmpVar
->Store
= tmpDecl
->Store
;
2812 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
2813 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
2815 /* if-false body (child 2) */
2816 tmpVar
= new_node0(IR_VAR
);
2817 tmpVar
->Store
= tmpDecl
->Store
;
2818 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
2819 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
2821 ifNode
= new_if(cond
, trueNode
, falseNode
);
2824 tmpVar
= new_node0(IR_VAR
);
2825 tmpVar
->Store
= tmpDecl
->Store
;
2827 tree
= new_seq(ifNode
, tmpVar
);
2828 tree
= new_seq(tmpDecl
, tree
);
2830 /*_slang_print_ir_tree(tree, 10);*/
2836 * Generate code for &&.
2838 static slang_ir_node
*
2839 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
2841 /* rewrite "a && b" as "a ? b : false" */
2842 slang_operation
*select
;
2845 select
= slang_operation_new(1);
2846 select
->type
= SLANG_OPER_SELECT
;
2847 select
->num_children
= 3;
2848 select
->children
= slang_operation_new(3);
2850 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
2851 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
2852 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
2853 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
2854 select
->children
[2].literal_size
= 1;
2856 n
= _slang_gen_select(A
, select
);
2862 * Generate code for ||.
2864 static slang_ir_node
*
2865 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
2867 /* rewrite "a || b" as "a ? true : b" */
2868 slang_operation
*select
;
2871 select
= slang_operation_new(1);
2872 select
->type
= SLANG_OPER_SELECT
;
2873 select
->num_children
= 3;
2874 select
->children
= slang_operation_new(3);
2876 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
2877 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
2878 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
2879 select
->children
[1].literal_size
= 1;
2880 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
2882 n
= _slang_gen_select(A
, select
);
2888 * Generate IR tree for a return statement.
2890 static slang_ir_node
*
2891 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
2893 const GLboolean haveReturnValue
2894 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
2896 /* error checking */
2897 assert(A
->CurFunction
);
2898 if (haveReturnValue
&&
2899 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
2900 slang_info_log_error(A
->log
, "illegal return expression");
2903 else if (!haveReturnValue
&&
2904 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2905 slang_info_log_error(A
->log
, "return statement requires an expression");
2909 if (!haveReturnValue
) {
2910 return new_return(A
->curFuncEndLabel
);
2918 * return; // goto __endOfFunction
2920 slang_operation
*assign
;
2921 slang_atom a_retVal
;
2924 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2930 _slang_variable_locate(oper
->locals
, a_retVal
, GL_TRUE
);
2932 /* trying to return a value in a void-valued function */
2938 assign
= slang_operation_new(1);
2939 assign
->type
= SLANG_OPER_ASSIGN
;
2940 assign
->num_children
= 2;
2941 assign
->children
= slang_operation_new(2);
2942 /* lhs (__retVal) */
2943 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2944 assign
->children
[0].a_id
= a_retVal
;
2945 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
2947 /* XXX we might be able to avoid this copy someday */
2948 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
2950 /* assemble the new code */
2951 n
= new_seq(_slang_gen_operation(A
, assign
),
2952 new_return(A
->curFuncEndLabel
));
2954 slang_operation_delete(assign
);
2961 * Determine if the given operation/expression is const-valued.
2964 _slang_is_constant_expr(const slang_operation
*oper
)
2966 slang_variable
*var
;
2969 switch (oper
->type
) {
2970 case SLANG_OPER_IDENTIFIER
:
2971 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
2972 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
2976 for (i
= 0; i
< oper
->num_children
; i
++) {
2977 if (!_slang_is_constant_expr(&oper
->children
[i
]))
2986 * Check if an assignment of type t1 to t0 is legal.
2987 * XXX more cases needed.
2990 _slang_assignment_compatible(slang_assemble_ctx
*A
,
2991 slang_operation
*op0
,
2992 slang_operation
*op1
)
2994 slang_typeinfo t0
, t1
;
2997 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
2998 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
3002 slang_typeinfo_construct(&t0
);
3003 typeof_operation(A
, op0
, &t0
);
3005 slang_typeinfo_construct(&t1
);
3006 typeof_operation(A
, op1
, &t1
);
3008 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
3009 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
3013 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
3018 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
3019 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
3020 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
3023 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
3024 t1
.spec
.type
== SLANG_SPEC_BOOL
)
3027 #if 0 /* not used just yet - causes problems elsewhere */
3028 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
3029 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3033 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3034 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3037 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3038 t1
.spec
.type
== SLANG_SPEC_INT
)
3047 * Generate IR tree for a local variable declaration.
3049 static slang_ir_node
*
3050 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
3052 const char *varName
= (char *) oper
->a_id
;
3053 slang_variable
*var
;
3054 slang_ir_node
*varDecl
;
3055 slang_operation
*initializer
;
3057 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
3058 assert(oper
->num_children
<= 1);
3060 /* lookup the variable by name */
3061 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3063 return NULL
; /* "shouldn't happen" */
3065 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3066 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
3067 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3068 /* can't declare attribute/uniform vars inside functions */
3069 slang_info_log_error(A
->log
,
3070 "local variable '%s' cannot be an attribute/uniform/varying",
3077 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
3082 /* check if the var has an initializer */
3083 if (oper
->num_children
> 0) {
3084 assert(oper
->num_children
== 1);
3085 initializer
= &oper
->children
[0];
3087 else if (var
->initializer
) {
3088 initializer
= var
->initializer
;
3095 /* check/compare var type and initializer type */
3096 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
3097 slang_info_log_error(A
->log
, "incompatible types in assignment");
3102 /* Generate IR node */
3103 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
3107 if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !initializer
) {
3108 slang_info_log_error(A
->log
,
3109 "const-qualified variable '%s' requires initializer",
3119 * Generate IR tree for a variable (such as in an expression).
3121 static slang_ir_node
*
3122 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
3124 /* If there's a variable associated with this oper (from inlining)
3125 * use it. Otherwise, use the oper's var id.
3127 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
3128 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
3129 slang_ir_node
*n
= new_var(A
, var
);
3131 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
3140 * Return the number of components actually named by the swizzle.
3141 * Recall that swizzles may have undefined/don't-care values.
3144 swizzle_size(GLuint swizzle
)
3147 for (i
= 0; i
< 4; i
++) {
3148 GLuint swz
= GET_SWZ(swizzle
, i
);
3149 size
+= (swz
>= 0 && swz
<= 3);
3155 static slang_ir_node
*
3156 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
3158 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
3162 n
->Store
= _slang_new_ir_storage_relative(0,
3163 swizzle_size(swizzle
),
3165 n
->Store
->Swizzle
= swizzle
;
3172 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
3174 while (store
->Parent
)
3175 store
= store
->Parent
;
3177 if (!(store
->File
== PROGRAM_OUTPUT
||
3178 store
->File
== PROGRAM_TEMPORARY
||
3179 (store
->File
== PROGRAM_VARYING
&&
3180 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
3190 * Generate IR tree for an assignment (=).
3192 static slang_ir_node
*
3193 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
3195 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
3196 /* Check that var is writeable */
3198 = _slang_variable_locate(oper
->children
[0].locals
,
3199 oper
->children
[0].a_id
, GL_TRUE
);
3201 slang_info_log_error(A
->log
, "undefined variable '%s'",
3202 (char *) oper
->children
[0].a_id
);
3205 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3206 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3207 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
3208 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
3209 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
3210 slang_info_log_error(A
->log
,
3211 "illegal assignment to read-only variable '%s'",
3212 (char *) oper
->children
[0].a_id
);
3217 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
3218 oper
->children
[1].type
== SLANG_OPER_CALL
) {
3219 /* Special case of: x = f(a, b)
3220 * Replace with f(a, b, x) (where x == hidden __retVal out param)
3222 * XXX this could be even more effective if we could accomodate
3223 * cases such as "v.x = f();" - would help with typical vertex
3227 n
= _slang_gen_function_call_name(A
,
3228 (const char *) oper
->children
[1].a_id
,
3229 &oper
->children
[1], &oper
->children
[0]);
3233 slang_ir_node
*n
, *lhs
, *rhs
;
3235 /* lhs and rhs type checking */
3236 if (!_slang_assignment_compatible(A
,
3238 &oper
->children
[1])) {
3239 slang_info_log_error(A
->log
, "incompatible types in assignment");
3243 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
3249 slang_info_log_error(A
->log
,
3250 "invalid left hand side for assignment");
3254 /* check that lhs is writable */
3255 if (!is_store_writable(A
, lhs
->Store
)) {
3256 slang_info_log_error(A
->log
,
3257 "illegal assignment to read-only l-value");
3261 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
3263 /* convert lhs swizzle into writemask */
3264 GLuint writemask
, newSwizzle
;
3265 if (!swizzle_to_writemask(A
, lhs
->Store
->Swizzle
,
3266 &writemask
, &newSwizzle
)) {
3267 /* Non-simple writemask, need to swizzle right hand side in
3268 * order to put components into the right place.
3270 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
3272 n
= new_node2(IR_COPY
, lhs
, rhs
);
3283 * Generate IR tree for referencing a field in a struct (or basic vector type)
3285 static slang_ir_node
*
3286 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
3290 /* type of struct */
3291 slang_typeinfo_construct(&ti
);
3292 typeof_operation(A
, &oper
->children
[0], &ti
);
3294 if (_slang_type_is_vector(ti
.spec
.type
)) {
3295 /* the field should be a swizzle */
3296 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
3300 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3301 slang_info_log_error(A
->log
, "Bad swizzle");
3304 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3309 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3310 /* create new parent node with swizzle */
3312 n
= _slang_gen_swizzle(n
, swizzle
);
3315 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
3316 || ti
.spec
.type
== SLANG_SPEC_INT
3317 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
3318 const GLuint rows
= 1;
3322 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3323 slang_info_log_error(A
->log
, "Bad swizzle");
3325 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3329 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3330 /* create new parent node with swizzle */
3331 n
= _slang_gen_swizzle(n
, swizzle
);
3335 /* the field is a structure member (base.field) */
3336 /* oper->children[0] is the base */
3337 /* oper->a_id is the field name */
3338 slang_ir_node
*base
, *n
;
3339 slang_typeinfo field_ti
;
3340 GLint fieldSize
, fieldOffset
= -1;
3343 slang_typeinfo_construct(&field_ti
);
3344 typeof_operation(A
, oper
, &field_ti
);
3346 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
3348 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
3350 if (fieldSize
== 0 || fieldOffset
< 0) {
3351 const char *structName
;
3352 if (ti
.spec
._struct
)
3353 structName
= (char *) ti
.spec
._struct
->a_name
;
3355 structName
= "unknown";
3356 slang_info_log_error(A
->log
,
3357 "\"%s\" is not a member of struct \"%s\"",
3358 (char *) oper
->a_id
, structName
);
3361 assert(fieldSize
>= 0);
3363 base
= _slang_gen_operation(A
, &oper
->children
[0]);
3365 /* error msg should have already been logged */
3369 n
= new_node1(IR_FIELD
, base
);
3373 n
->Field
= (char *) oper
->a_id
;
3375 /* Store the field's offset in storage->Index */
3376 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
3386 * Gen code for array indexing.
3388 static slang_ir_node
*
3389 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
3391 slang_typeinfo array_ti
;
3393 /* get array's type info */
3394 slang_typeinfo_construct(&array_ti
);
3395 typeof_operation(A
, &oper
->children
[0], &array_ti
);
3397 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
3398 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
3399 /* translate the index into a swizzle/writemask: "v.x=p" */
3400 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
3404 index
= (GLint
) oper
->children
[1].literal
[0];
3405 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
3406 index
>= (GLint
) max
) {
3407 slang_info_log_error(A
->log
, "Invalid array index for vector type");
3411 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3413 /* use swizzle to access the element */
3414 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
3418 n
= _slang_gen_swizzle(n
, swizzle
);
3424 /* conventional array */
3425 slang_typeinfo elem_ti
;
3426 slang_ir_node
*elem
, *array
, *index
;
3427 GLint elemSize
, arrayLen
;
3429 /* size of array element */
3430 slang_typeinfo_construct(&elem_ti
);
3431 typeof_operation(A
, oper
, &elem_ti
);
3432 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
3434 if (_slang_type_is_matrix(array_ti
.spec
.type
))
3435 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
3437 arrayLen
= array_ti
.array_len
;
3439 slang_typeinfo_destruct(&array_ti
);
3440 slang_typeinfo_destruct(&elem_ti
);
3442 if (elemSize
<= 0) {
3443 /* unknown var or type */
3444 slang_info_log_error(A
->log
, "Undefined variable or type");
3448 array
= _slang_gen_operation(A
, &oper
->children
[0]);
3449 index
= _slang_gen_operation(A
, &oper
->children
[1]);
3450 if (array
&& index
) {
3452 GLint constIndex
= -1;
3453 if (index
->Opcode
== IR_FLOAT
) {
3454 constIndex
= (int) index
->Value
[0];
3455 if (constIndex
< 0 || constIndex
>= arrayLen
) {
3456 slang_info_log_error(A
->log
,
3457 "Array index out of bounds (index=%d size=%d)",
3458 constIndex
, arrayLen
);
3459 _slang_free_ir_tree(array
);
3460 _slang_free_ir_tree(index
);
3465 if (!array
->Store
) {
3466 slang_info_log_error(A
->log
, "Invalid array");
3470 elem
= new_node2(IR_ELEMENT
, array
, index
);
3472 /* The storage info here will be updated during code emit */
3473 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
3474 array
->Store
->Index
,
3480 _slang_free_ir_tree(array
);
3481 _slang_free_ir_tree(index
);
3488 static slang_ir_node
*
3489 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
3490 slang_ir_opcode opcode
)
3492 slang_typeinfo t0
, t1
;
3495 slang_typeinfo_construct(&t0
);
3496 typeof_operation(A
, &oper
->children
[0], &t0
);
3498 slang_typeinfo_construct(&t1
);
3499 typeof_operation(A
, &oper
->children
[0], &t1
);
3501 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
3502 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
3503 slang_info_log_error(A
->log
, "Illegal array comparison");
3507 if (oper
->type
!= SLANG_OPER_EQUAL
&&
3508 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
3509 /* <, <=, >, >= can only be used with scalars */
3510 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
3511 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
3512 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
3513 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
3514 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
3519 n
= new_node2(opcode
,
3520 _slang_gen_operation(A
, &oper
->children
[0]),
3521 _slang_gen_operation(A
, &oper
->children
[1]));
3523 /* result is a bool (size 1) */
3524 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
3532 print_vars(slang_variable_scope
*s
)
3536 for (i
= 0; i
< s
->num_variables
; i
++) {
3538 (char*) s
->variables
[i
]->a_name
,
3539 s
->variables
[i
]->declared
);
3549 _slang_undeclare_vars(slang_variable_scope
*locals
)
3551 if (locals
->num_variables
> 0) {
3553 for (i
= 0; i
< locals
->num_variables
; i
++) {
3554 slang_variable
*v
= locals
->variables
[i
];
3555 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
3556 v
->declared
= GL_FALSE
;
3564 * Generate IR tree for a slang_operation (AST node)
3566 static slang_ir_node
*
3567 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
3569 switch (oper
->type
) {
3570 case SLANG_OPER_BLOCK_NEW_SCOPE
:
3574 _slang_push_var_table(A
->vartable
);
3576 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
3577 n
= _slang_gen_operation(A
, oper
);
3578 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
3580 _slang_pop_var_table(A
->vartable
);
3582 /*_slang_undeclare_vars(oper->locals);*/
3583 /*print_vars(oper->locals);*/
3586 n
= new_node1(IR_SCOPE
, n
);
3591 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
3592 /* list of operations */
3593 if (oper
->num_children
> 0)
3595 slang_ir_node
*n
, *tree
= NULL
;
3598 for (i
= 0; i
< oper
->num_children
; i
++) {
3599 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
3601 _slang_free_ir_tree(tree
);
3602 return NULL
; /* error must have occured */
3604 tree
= new_seq(tree
, n
);
3610 return new_node0(IR_NOP
);
3613 case SLANG_OPER_EXPRESSION
:
3614 return _slang_gen_operation(A
, &oper
->children
[0]);
3616 case SLANG_OPER_FOR
:
3617 return _slang_gen_for(A
, oper
);
3619 return _slang_gen_do(A
, oper
);
3620 case SLANG_OPER_WHILE
:
3621 return _slang_gen_while(A
, oper
);
3622 case SLANG_OPER_BREAK
:
3624 slang_info_log_error(A
->log
, "'break' not in loop");
3627 return new_break(A
->CurLoop
);
3628 case SLANG_OPER_CONTINUE
:
3630 slang_info_log_error(A
->log
, "'continue' not in loop");
3633 return _slang_gen_continue(A
, oper
);
3634 case SLANG_OPER_DISCARD
:
3635 return new_node0(IR_KILL
);
3637 case SLANG_OPER_EQUAL
:
3638 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
3639 case SLANG_OPER_NOTEQUAL
:
3640 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
3641 case SLANG_OPER_GREATER
:
3642 return _slang_gen_compare(A
, oper
, IR_SGT
);
3643 case SLANG_OPER_LESS
:
3644 return _slang_gen_compare(A
, oper
, IR_SLT
);
3645 case SLANG_OPER_GREATEREQUAL
:
3646 return _slang_gen_compare(A
, oper
, IR_SGE
);
3647 case SLANG_OPER_LESSEQUAL
:
3648 return _slang_gen_compare(A
, oper
, IR_SLE
);
3649 case SLANG_OPER_ADD
:
3652 assert(oper
->num_children
== 2);
3653 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
3656 case SLANG_OPER_SUBTRACT
:
3659 assert(oper
->num_children
== 2);
3660 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
3663 case SLANG_OPER_MULTIPLY
:
3666 assert(oper
->num_children
== 2);
3667 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
3670 case SLANG_OPER_DIVIDE
:
3673 assert(oper
->num_children
== 2);
3674 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
3677 case SLANG_OPER_MINUS
:
3680 assert(oper
->num_children
== 1);
3681 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
3684 case SLANG_OPER_PLUS
:
3685 /* +expr --> do nothing */
3686 return _slang_gen_operation(A
, &oper
->children
[0]);
3687 case SLANG_OPER_VARIABLE_DECL
:
3688 return _slang_gen_declaration(A
, oper
);
3689 case SLANG_OPER_ASSIGN
:
3690 return _slang_gen_assignment(A
, oper
);
3691 case SLANG_OPER_ADDASSIGN
:
3694 assert(oper
->num_children
== 2);
3695 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
3698 case SLANG_OPER_SUBASSIGN
:
3701 assert(oper
->num_children
== 2);
3702 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
3706 case SLANG_OPER_MULASSIGN
:
3709 assert(oper
->num_children
== 2);
3710 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
3713 case SLANG_OPER_DIVASSIGN
:
3716 assert(oper
->num_children
== 2);
3717 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
3720 case SLANG_OPER_LOGICALAND
:
3723 assert(oper
->num_children
== 2);
3724 n
= _slang_gen_logical_and(A
, oper
);
3727 case SLANG_OPER_LOGICALOR
:
3730 assert(oper
->num_children
== 2);
3731 n
= _slang_gen_logical_or(A
, oper
);
3734 case SLANG_OPER_LOGICALXOR
:
3735 return _slang_gen_xor(A
, oper
);
3736 case SLANG_OPER_NOT
:
3737 return _slang_gen_not(A
, oper
);
3738 case SLANG_OPER_SELECT
: /* b ? x : y */
3741 assert(oper
->num_children
== 3);
3742 n
= _slang_gen_select(A
, oper
);
3746 case SLANG_OPER_ASM
:
3747 return _slang_gen_asm(A
, oper
, NULL
);
3748 case SLANG_OPER_CALL
:
3749 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
3751 case SLANG_OPER_METHOD
:
3752 return _slang_gen_method_call(A
, oper
);
3753 case SLANG_OPER_RETURN
:
3754 return _slang_gen_return(A
, oper
);
3755 case SLANG_OPER_LABEL
:
3756 return new_label(oper
->label
);
3757 case SLANG_OPER_IDENTIFIER
:
3758 return _slang_gen_variable(A
, oper
);
3760 return _slang_gen_if(A
, oper
);
3761 case SLANG_OPER_FIELD
:
3762 return _slang_gen_struct_field(A
, oper
);
3763 case SLANG_OPER_SUBSCRIPT
:
3764 return _slang_gen_array_element(A
, oper
);
3765 case SLANG_OPER_LITERAL_FLOAT
:
3767 case SLANG_OPER_LITERAL_INT
:
3769 case SLANG_OPER_LITERAL_BOOL
:
3770 return new_float_literal(oper
->literal
, oper
->literal_size
);
3772 case SLANG_OPER_POSTINCREMENT
: /* var++ */
3775 assert(oper
->num_children
== 1);
3776 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
3779 case SLANG_OPER_POSTDECREMENT
: /* var-- */
3782 assert(oper
->num_children
== 1);
3783 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
3786 case SLANG_OPER_PREINCREMENT
: /* ++var */
3789 assert(oper
->num_children
== 1);
3790 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
3793 case SLANG_OPER_PREDECREMENT
: /* --var */
3796 assert(oper
->num_children
== 1);
3797 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
3801 case SLANG_OPER_NON_INLINED_CALL
:
3802 case SLANG_OPER_SEQUENCE
:
3804 slang_ir_node
*tree
= NULL
;
3806 for (i
= 0; i
< oper
->num_children
; i
++) {
3807 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
3808 tree
= new_seq(tree
, n
);
3810 tree
->Store
= n
->Store
;
3812 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
3813 tree
= new_function_call(tree
, oper
->label
);
3818 case SLANG_OPER_NONE
:
3819 case SLANG_OPER_VOID
:
3820 /* returning NULL here would generate an error */
3821 return new_node0(IR_NOP
);
3824 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
3826 return new_node0(IR_NOP
);
3834 * Compute total size of array give size of element, number of elements.
3837 array_size(GLint baseSize
, GLint arrayLen
)
3841 /* round up base type to multiple of 4 */
3842 total
= ((baseSize
+ 3) & ~0x3) * MAX2(arrayLen
, 1);
3852 * Called by compiler when a global variable has been parsed/compiled.
3853 * Here we examine the variable's type to determine what kind of register
3854 * storage will be used.
3856 * A uniform such as "gl_Position" will become the register specification
3857 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
3858 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
3860 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
3861 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
3862 * actual texture unit (as specified by the user calling glUniform1i()).
3865 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
3866 slang_unit_type type
)
3868 struct gl_program
*prog
= A
->program
;
3869 const char *varName
= (char *) var
->a_name
;
3870 GLboolean success
= GL_TRUE
;
3871 slang_ir_storage
*store
= NULL
;
3873 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3874 const GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
3875 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3877 if (texIndex
!= -1) {
3878 /* This is a texture sampler variable...
3879 * store->File = PROGRAM_SAMPLER
3880 * store->Index = sampler number (0..7, typically)
3881 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
3883 if (var
->initializer
) {
3884 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
3887 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
3888 /* disallow rect samplers */
3889 if (var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECT
||
3890 var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
) {
3891 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
3896 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
3897 store
= _slang_new_ir_storage(PROGRAM_SAMPLER
, sampNum
, texIndex
);
3899 if (dbg
) printf("SAMPLER ");
3901 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3902 /* Uniform variable */
3903 const GLint totalSize
= array_size(size
, var
->array_len
);
3904 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
3907 /* user-defined uniform */
3908 if (datatype
== GL_NONE
) {
3909 if (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
) {
3910 /* temporary work-around */
3911 GLenum datatype
= GL_FLOAT
;
3912 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
3913 totalSize
, datatype
, NULL
);
3914 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
3915 totalSize
, swizzle
);
3917 /* XXX what we need to do is unroll the struct into its
3918 * basic types, creating a uniform variable for each.
3926 * Should produce uniforms:
3927 * "f.a" (GL_FLOAT_VEC3)
3928 * "f.b" (GL_FLOAT_VEC4)
3931 if (var
->initializer
) {
3932 slang_info_log_error(A
->log
,
3933 "unsupported initializer for uniform '%s'", varName
);
3938 slang_info_log_error(A
->log
,
3939 "invalid datatype for uniform variable %s",
3946 const GLfloat
*initialValues
= NULL
;
3947 if (var
->initializer
) {
3948 _slang_simplify(var
->initializer
, &A
->space
, A
->atoms
);
3949 if (var
->initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3950 var
->initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3951 /* simple float/vector initializer */
3952 initialValues
= var
->initializer
->literal
;
3955 /* complex initializer */
3956 slang_info_log_error(A
->log
,
3957 "unsupported initializer for uniform '%s'", varName
);
3962 uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
3963 totalSize
, datatype
, initialValues
);
3964 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
3965 totalSize
, swizzle
);
3969 /* pre-defined uniform, like gl_ModelviewMatrix */
3970 /* We know it's a uniform, but don't allocate storage unless
3973 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
3974 totalSize
, swizzle
);
3976 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
3978 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
3979 const GLint totalSize
= array_size(size
, var
->array_len
);
3981 /* varyings must be float, vec or mat */
3982 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
3983 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
3984 slang_info_log_error(A
->log
,
3985 "varying '%s' must be float/vector/matrix",
3990 if (var
->initializer
) {
3991 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
3997 /* user-defined varying */
4003 if (var
->type
.centroid
== SLANG_CENTROID
)
4004 flags
|= PROG_PARAM_BIT_CENTROID
;
4005 if (var
->type
.variant
== SLANG_INVARIANT
)
4006 flags
|= PROG_PARAM_BIT_INVARIANT
;
4008 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
4010 swizzle
= _slang_var_swizzle(size
, 0);
4011 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
4012 totalSize
, swizzle
);
4015 /* pre-defined varying, like gl_Color or gl_TexCoord */
4016 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
4017 /* fragment program input */
4019 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4022 assert(index
< FRAG_ATTRIB_MAX
);
4023 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
4027 /* vertex program output */
4028 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4029 GLuint swizzle
= _slang_var_swizzle(size
, 0);
4031 assert(index
< VERT_RESULT_MAX
);
4032 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
4033 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
4036 if (dbg
) printf("V/F ");
4038 if (dbg
) printf("VARYING ");
4040 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
4043 /* attributes must be float, vec or mat */
4044 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
4045 slang_info_log_error(A
->log
,
4046 "attribute '%s' must be float/vector/matrix",
4052 /* user-defined vertex attribute */
4053 const GLint attr
= -1; /* unknown */
4054 swizzle
= _slang_var_swizzle(size
, 0);
4055 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
4056 size
, datatype
, attr
);
4058 index
= VERT_ATTRIB_GENERIC0
+ index
;
4061 /* pre-defined vertex attrib */
4062 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
4065 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4066 if (dbg
) printf("ATTRIB ");
4068 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
4069 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
4070 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4072 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4073 if (dbg
) printf("INPUT ");
4075 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
4076 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
4077 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4078 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
4081 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
4082 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
4083 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
4084 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
4086 if (dbg
) printf("OUTPUT ");
4088 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
4089 /* pre-defined global constant, like gl_MaxLights */
4090 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
4091 if (dbg
) printf("CONST ");
4094 /* ordinary variable (may be const) */
4097 /* IR node to declare the variable */
4098 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
4100 /* emit GPU instructions */
4101 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_FALSE
, A
->log
);
4103 _slang_free_ir_tree(n
);
4106 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
4107 store
? store
->Index
: -2);
4110 var
->store
= store
; /* save var's storage info */
4112 var
->declared
= GL_TRUE
;
4119 * Produce an IR tree from a function AST (fun->body).
4120 * Then call the code emitter to convert the IR tree into gl_program
4124 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
4127 GLboolean success
= GL_TRUE
;
4129 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
4130 /* we only really generate code for main, all other functions get
4131 * inlined or codegen'd upon an actual call.
4134 /* do some basic error checking though */
4135 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
4136 /* check that non-void functions actually return something */
4138 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
4140 slang_info_log_error(A
->log
,
4141 "function \"%s\" has no return statement",
4142 (char *) fun
->header
.a_name
);
4144 "function \"%s\" has no return statement\n",
4145 (char *) fun
->header
.a_name
);
4150 return GL_TRUE
; /* not an error */
4154 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
4155 slang_print_function(fun
, 1);
4158 /* should have been allocated earlier: */
4159 assert(A
->program
->Parameters
);
4160 assert(A
->program
->Varying
);
4161 assert(A
->vartable
);
4163 A
->CurFunction
= fun
;
4165 /* fold constant expressions, etc. */
4166 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
4169 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
4170 slang_print_function(fun
, 1);
4173 /* Create an end-of-function label */
4174 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
4176 /* push new vartable scope */
4177 _slang_push_var_table(A
->vartable
);
4179 /* Generate IR tree for the function body code */
4180 n
= _slang_gen_operation(A
, fun
->body
);
4182 n
= new_node1(IR_SCOPE
, n
);
4184 /* pop vartable, restore previous */
4185 _slang_pop_var_table(A
->vartable
);
4188 /* XXX record error */
4192 /* append an end-of-function-label to IR tree */
4193 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
4195 /*_slang_label_delete(A->curFuncEndLabel);*/
4196 A
->curFuncEndLabel
= NULL
;
4199 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
4200 slang_print_function(fun
, 1);
4203 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
4204 _slang_print_ir_tree(n
, 0);
4207 printf("************* End codegen function ************\n\n");
4210 /* Emit program instructions */
4211 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
, A
->log
);
4212 _slang_free_ir_tree(n
);
4214 /* free codegen context */
4216 _mesa_free(A->codegen);