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 varRef
= new_var(A
, var
);
2714 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
2718 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
2719 /* if the variable is const, the initializer must be a const
2720 * expression as well.
2723 if (!_slang_is_constant_expr(initializer
)) {
2724 slang_info_log_error(A
->log
,
2725 "initializer for %s not constant", varName
);
2731 /* constant-folding, etc here */
2732 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
2734 init
= _slang_gen_operation(A
, initializer
);
2738 /*assert(init->Store);*/
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 n
= new_node2(IR_COPY
, varRef
, init
);
2747 n
= new_seq(varDecl
, n
);
2750 /* no initializer */
2759 * Generate code for a selection expression: b ? x : y
2760 * XXX In some cases we could implement a selection expression
2761 * with an LRP instruction (use the boolean as the interpolant).
2762 * Otherwise, we use an IF/ELSE/ENDIF construct.
2764 static slang_ir_node
*
2765 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
2767 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
2768 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
2769 slang_typeinfo type0
, type1
, type2
;
2770 int size
, isBool
, isEqual
;
2772 assert(oper
->type
== SLANG_OPER_SELECT
);
2773 assert(oper
->num_children
== 3);
2775 /* type of children[0] must be boolean */
2776 slang_typeinfo_construct(&type0
);
2777 typeof_operation(A
, &oper
->children
[0], &type0
);
2778 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
2779 slang_typeinfo_destruct(&type0
);
2781 slang_info_log_error(A
->log
, "selector type is not boolean");
2785 slang_typeinfo_construct(&type1
);
2786 slang_typeinfo_construct(&type2
);
2787 typeof_operation(A
, &oper
->children
[1], &type1
);
2788 typeof_operation(A
, &oper
->children
[2], &type2
);
2789 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
2790 slang_typeinfo_destruct(&type1
);
2791 slang_typeinfo_destruct(&type2
);
2793 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
2797 /* size of x or y's type */
2798 size
= _slang_sizeof_type_specifier(&type1
.spec
);
2802 tmpDecl
= _slang_gen_temporary(size
);
2804 /* the condition (child 0) */
2805 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2806 cond
= new_cond(cond
);
2808 /* if-true body (child 1) */
2809 tmpVar
= new_node0(IR_VAR
);
2810 tmpVar
->Store
= tmpDecl
->Store
;
2811 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
2812 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
2814 /* if-false body (child 2) */
2815 tmpVar
= new_node0(IR_VAR
);
2816 tmpVar
->Store
= tmpDecl
->Store
;
2817 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
2818 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
2820 ifNode
= new_if(cond
, trueNode
, falseNode
);
2823 tmpVar
= new_node0(IR_VAR
);
2824 tmpVar
->Store
= tmpDecl
->Store
;
2826 tree
= new_seq(ifNode
, tmpVar
);
2827 tree
= new_seq(tmpDecl
, tree
);
2829 /*_slang_print_ir_tree(tree, 10);*/
2835 * Generate code for &&.
2837 static slang_ir_node
*
2838 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
2840 /* rewrite "a && b" as "a ? b : false" */
2841 slang_operation
*select
;
2844 select
= slang_operation_new(1);
2845 select
->type
= SLANG_OPER_SELECT
;
2846 select
->num_children
= 3;
2847 select
->children
= slang_operation_new(3);
2849 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
2850 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
2851 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
2852 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
2853 select
->children
[2].literal_size
= 1;
2855 n
= _slang_gen_select(A
, select
);
2861 * Generate code for ||.
2863 static slang_ir_node
*
2864 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
2866 /* rewrite "a || b" as "a ? true : b" */
2867 slang_operation
*select
;
2870 select
= slang_operation_new(1);
2871 select
->type
= SLANG_OPER_SELECT
;
2872 select
->num_children
= 3;
2873 select
->children
= slang_operation_new(3);
2875 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
2876 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
2877 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
2878 select
->children
[1].literal_size
= 1;
2879 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
2881 n
= _slang_gen_select(A
, select
);
2887 * Generate IR tree for a return statement.
2889 static slang_ir_node
*
2890 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
2892 const GLboolean haveReturnValue
2893 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
2895 /* error checking */
2896 assert(A
->CurFunction
);
2897 if (haveReturnValue
&&
2898 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
2899 slang_info_log_error(A
->log
, "illegal return expression");
2902 else if (!haveReturnValue
&&
2903 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2904 slang_info_log_error(A
->log
, "return statement requires an expression");
2908 if (!haveReturnValue
) {
2909 return new_return(A
->curFuncEndLabel
);
2917 * return; // goto __endOfFunction
2919 slang_operation
*assign
;
2920 slang_atom a_retVal
;
2923 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2929 _slang_variable_locate(oper
->locals
, a_retVal
, GL_TRUE
);
2931 /* trying to return a value in a void-valued function */
2937 assign
= slang_operation_new(1);
2938 assign
->type
= SLANG_OPER_ASSIGN
;
2939 assign
->num_children
= 2;
2940 assign
->children
= slang_operation_new(2);
2941 /* lhs (__retVal) */
2942 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2943 assign
->children
[0].a_id
= a_retVal
;
2944 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
2946 /* XXX we might be able to avoid this copy someday */
2947 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
2949 /* assemble the new code */
2950 n
= new_seq(_slang_gen_operation(A
, assign
),
2951 new_return(A
->curFuncEndLabel
));
2953 slang_operation_delete(assign
);
2960 * Determine if the given operation/expression is const-valued.
2963 _slang_is_constant_expr(const slang_operation
*oper
)
2965 slang_variable
*var
;
2968 switch (oper
->type
) {
2969 case SLANG_OPER_IDENTIFIER
:
2970 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
2971 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
2975 for (i
= 0; i
< oper
->num_children
; i
++) {
2976 if (!_slang_is_constant_expr(&oper
->children
[i
]))
2985 * Check if an assignment of type t1 to t0 is legal.
2986 * XXX more cases needed.
2989 _slang_assignment_compatible(slang_assemble_ctx
*A
,
2990 slang_operation
*op0
,
2991 slang_operation
*op1
)
2993 slang_typeinfo t0
, t1
;
2996 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
2997 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
3001 slang_typeinfo_construct(&t0
);
3002 typeof_operation(A
, op0
, &t0
);
3004 slang_typeinfo_construct(&t1
);
3005 typeof_operation(A
, op1
, &t1
);
3007 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
3008 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
3012 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
3017 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
3018 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
3019 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
3022 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
3023 t1
.spec
.type
== SLANG_SPEC_BOOL
)
3026 #if 0 /* not used just yet - causes problems elsewhere */
3027 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
3028 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3032 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3033 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3036 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3037 t1
.spec
.type
== SLANG_SPEC_INT
)
3046 * Generate IR tree for a local variable declaration.
3048 static slang_ir_node
*
3049 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
3051 const char *varName
= (char *) oper
->a_id
;
3052 slang_variable
*var
;
3053 slang_ir_node
*varDecl
;
3054 slang_operation
*initializer
;
3056 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
3057 assert(oper
->num_children
<= 1);
3059 /* lookup the variable by name */
3060 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3062 return NULL
; /* "shouldn't happen" */
3064 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3065 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
3066 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3067 /* can't declare attribute/uniform vars inside functions */
3068 slang_info_log_error(A
->log
,
3069 "local variable '%s' cannot be an attribute/uniform/varying",
3076 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
3081 /* check if the var has an initializer */
3082 if (oper
->num_children
> 0) {
3083 assert(oper
->num_children
== 1);
3084 initializer
= &oper
->children
[0];
3086 else if (var
->initializer
) {
3087 initializer
= var
->initializer
;
3094 /* check/compare var type and initializer type */
3095 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
3096 slang_info_log_error(A
->log
, "incompatible types in assignment");
3101 /* Generate IR node */
3102 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
3106 if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !initializer
) {
3107 slang_info_log_error(A
->log
,
3108 "const-qualified variable '%s' requires initializer",
3118 * Generate IR tree for a variable (such as in an expression).
3120 static slang_ir_node
*
3121 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
3123 /* If there's a variable associated with this oper (from inlining)
3124 * use it. Otherwise, use the oper's var id.
3126 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
3127 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
3128 slang_ir_node
*n
= new_var(A
, var
);
3130 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
3139 * Return the number of components actually named by the swizzle.
3140 * Recall that swizzles may have undefined/don't-care values.
3143 swizzle_size(GLuint swizzle
)
3146 for (i
= 0; i
< 4; i
++) {
3147 GLuint swz
= GET_SWZ(swizzle
, i
);
3148 size
+= (swz
>= 0 && swz
<= 3);
3154 static slang_ir_node
*
3155 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
3157 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
3161 n
->Store
= _slang_new_ir_storage_relative(0,
3162 swizzle_size(swizzle
),
3164 n
->Store
->Swizzle
= swizzle
;
3171 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
3173 while (store
->Parent
)
3174 store
= store
->Parent
;
3176 if (!(store
->File
== PROGRAM_OUTPUT
||
3177 store
->File
== PROGRAM_TEMPORARY
||
3178 (store
->File
== PROGRAM_VARYING
&&
3179 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
3189 * Generate IR tree for an assignment (=).
3191 static slang_ir_node
*
3192 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
3194 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
3195 /* Check that var is writeable */
3197 = _slang_variable_locate(oper
->children
[0].locals
,
3198 oper
->children
[0].a_id
, GL_TRUE
);
3200 slang_info_log_error(A
->log
, "undefined variable '%s'",
3201 (char *) oper
->children
[0].a_id
);
3204 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3205 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3206 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
3207 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
3208 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
3209 slang_info_log_error(A
->log
,
3210 "illegal assignment to read-only variable '%s'",
3211 (char *) oper
->children
[0].a_id
);
3216 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
3217 oper
->children
[1].type
== SLANG_OPER_CALL
) {
3218 /* Special case of: x = f(a, b)
3219 * Replace with f(a, b, x) (where x == hidden __retVal out param)
3221 * XXX this could be even more effective if we could accomodate
3222 * cases such as "v.x = f();" - would help with typical vertex
3226 n
= _slang_gen_function_call_name(A
,
3227 (const char *) oper
->children
[1].a_id
,
3228 &oper
->children
[1], &oper
->children
[0]);
3232 slang_ir_node
*n
, *lhs
, *rhs
;
3234 /* lhs and rhs type checking */
3235 if (!_slang_assignment_compatible(A
,
3237 &oper
->children
[1])) {
3238 slang_info_log_error(A
->log
, "incompatible types in assignment");
3242 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
3248 slang_info_log_error(A
->log
,
3249 "invalid left hand side for assignment");
3253 /* check that lhs is writable */
3254 if (!is_store_writable(A
, lhs
->Store
)) {
3255 slang_info_log_error(A
->log
,
3256 "illegal assignment to read-only l-value");
3260 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
3262 /* convert lhs swizzle into writemask */
3263 GLuint writemask
, newSwizzle
;
3264 if (!swizzle_to_writemask(A
, lhs
->Store
->Swizzle
,
3265 &writemask
, &newSwizzle
)) {
3266 /* Non-simple writemask, need to swizzle right hand side in
3267 * order to put components into the right place.
3269 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
3271 n
= new_node2(IR_COPY
, lhs
, rhs
);
3282 * Generate IR tree for referencing a field in a struct (or basic vector type)
3284 static slang_ir_node
*
3285 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
3289 /* type of struct */
3290 slang_typeinfo_construct(&ti
);
3291 typeof_operation(A
, &oper
->children
[0], &ti
);
3293 if (_slang_type_is_vector(ti
.spec
.type
)) {
3294 /* the field should be a swizzle */
3295 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
3299 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3300 slang_info_log_error(A
->log
, "Bad swizzle");
3303 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3308 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3309 /* create new parent node with swizzle */
3311 n
= _slang_gen_swizzle(n
, swizzle
);
3314 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
3315 || ti
.spec
.type
== SLANG_SPEC_INT
3316 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
3317 const GLuint rows
= 1;
3321 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3322 slang_info_log_error(A
->log
, "Bad swizzle");
3324 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3328 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3329 /* create new parent node with swizzle */
3330 n
= _slang_gen_swizzle(n
, swizzle
);
3334 /* the field is a structure member (base.field) */
3335 /* oper->children[0] is the base */
3336 /* oper->a_id is the field name */
3337 slang_ir_node
*base
, *n
;
3338 slang_typeinfo field_ti
;
3339 GLint fieldSize
, fieldOffset
= -1;
3342 slang_typeinfo_construct(&field_ti
);
3343 typeof_operation(A
, oper
, &field_ti
);
3345 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
3347 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
3349 if (fieldSize
== 0 || fieldOffset
< 0) {
3350 const char *structName
;
3351 if (ti
.spec
._struct
)
3352 structName
= (char *) ti
.spec
._struct
->a_name
;
3354 structName
= "unknown";
3355 slang_info_log_error(A
->log
,
3356 "\"%s\" is not a member of struct \"%s\"",
3357 (char *) oper
->a_id
, structName
);
3360 assert(fieldSize
>= 0);
3362 base
= _slang_gen_operation(A
, &oper
->children
[0]);
3364 /* error msg should have already been logged */
3368 n
= new_node1(IR_FIELD
, base
);
3372 n
->Field
= (char *) oper
->a_id
;
3374 /* Store the field's offset in storage->Index */
3375 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
3385 * Gen code for array indexing.
3387 static slang_ir_node
*
3388 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
3390 slang_typeinfo array_ti
;
3392 /* get array's type info */
3393 slang_typeinfo_construct(&array_ti
);
3394 typeof_operation(A
, &oper
->children
[0], &array_ti
);
3396 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
3397 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
3398 /* translate the index into a swizzle/writemask: "v.x=p" */
3399 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
3403 index
= (GLint
) oper
->children
[1].literal
[0];
3404 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
3405 index
>= (GLint
) max
) {
3406 slang_info_log_error(A
->log
, "Invalid array index for vector type");
3410 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3412 /* use swizzle to access the element */
3413 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
3417 n
= _slang_gen_swizzle(n
, swizzle
);
3423 /* conventional array */
3424 slang_typeinfo elem_ti
;
3425 slang_ir_node
*elem
, *array
, *index
;
3426 GLint elemSize
, arrayLen
;
3428 /* size of array element */
3429 slang_typeinfo_construct(&elem_ti
);
3430 typeof_operation(A
, oper
, &elem_ti
);
3431 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
3433 if (_slang_type_is_matrix(array_ti
.spec
.type
))
3434 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
3436 arrayLen
= array_ti
.array_len
;
3438 slang_typeinfo_destruct(&array_ti
);
3439 slang_typeinfo_destruct(&elem_ti
);
3441 if (elemSize
<= 0) {
3442 /* unknown var or type */
3443 slang_info_log_error(A
->log
, "Undefined variable or type");
3447 array
= _slang_gen_operation(A
, &oper
->children
[0]);
3448 index
= _slang_gen_operation(A
, &oper
->children
[1]);
3449 if (array
&& index
) {
3451 GLint constIndex
= -1;
3452 if (index
->Opcode
== IR_FLOAT
) {
3453 constIndex
= (int) index
->Value
[0];
3454 if (constIndex
< 0 || constIndex
>= arrayLen
) {
3455 slang_info_log_error(A
->log
,
3456 "Array index out of bounds (index=%d size=%d)",
3457 constIndex
, arrayLen
);
3458 _slang_free_ir_tree(array
);
3459 _slang_free_ir_tree(index
);
3464 if (!array
->Store
) {
3465 slang_info_log_error(A
->log
, "Invalid array");
3469 elem
= new_node2(IR_ELEMENT
, array
, index
);
3471 /* The storage info here will be updated during code emit */
3472 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
3473 array
->Store
->Index
,
3479 _slang_free_ir_tree(array
);
3480 _slang_free_ir_tree(index
);
3487 static slang_ir_node
*
3488 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
3489 slang_ir_opcode opcode
)
3491 slang_typeinfo t0
, t1
;
3494 slang_typeinfo_construct(&t0
);
3495 typeof_operation(A
, &oper
->children
[0], &t0
);
3497 slang_typeinfo_construct(&t1
);
3498 typeof_operation(A
, &oper
->children
[0], &t1
);
3500 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
3501 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
3502 slang_info_log_error(A
->log
, "Illegal array comparison");
3506 if (oper
->type
!= SLANG_OPER_EQUAL
&&
3507 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
3508 /* <, <=, >, >= can only be used with scalars */
3509 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
3510 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
3511 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
3512 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
3513 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
3518 n
= new_node2(opcode
,
3519 _slang_gen_operation(A
, &oper
->children
[0]),
3520 _slang_gen_operation(A
, &oper
->children
[1]));
3522 /* result is a bool (size 1) */
3523 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
3531 print_vars(slang_variable_scope
*s
)
3535 for (i
= 0; i
< s
->num_variables
; i
++) {
3537 (char*) s
->variables
[i
]->a_name
,
3538 s
->variables
[i
]->declared
);
3548 _slang_undeclare_vars(slang_variable_scope
*locals
)
3550 if (locals
->num_variables
> 0) {
3552 for (i
= 0; i
< locals
->num_variables
; i
++) {
3553 slang_variable
*v
= locals
->variables
[i
];
3554 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
3555 v
->declared
= GL_FALSE
;
3563 * Generate IR tree for a slang_operation (AST node)
3565 static slang_ir_node
*
3566 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
3568 switch (oper
->type
) {
3569 case SLANG_OPER_BLOCK_NEW_SCOPE
:
3573 _slang_push_var_table(A
->vartable
);
3575 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
3576 n
= _slang_gen_operation(A
, oper
);
3577 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
3579 _slang_pop_var_table(A
->vartable
);
3581 /*_slang_undeclare_vars(oper->locals);*/
3582 /*print_vars(oper->locals);*/
3585 n
= new_node1(IR_SCOPE
, n
);
3590 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
3591 /* list of operations */
3592 if (oper
->num_children
> 0)
3594 slang_ir_node
*n
, *tree
= NULL
;
3597 for (i
= 0; i
< oper
->num_children
; i
++) {
3598 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
3600 _slang_free_ir_tree(tree
);
3601 return NULL
; /* error must have occured */
3603 tree
= new_seq(tree
, n
);
3609 return new_node0(IR_NOP
);
3612 case SLANG_OPER_EXPRESSION
:
3613 return _slang_gen_operation(A
, &oper
->children
[0]);
3615 case SLANG_OPER_FOR
:
3616 return _slang_gen_for(A
, oper
);
3618 return _slang_gen_do(A
, oper
);
3619 case SLANG_OPER_WHILE
:
3620 return _slang_gen_while(A
, oper
);
3621 case SLANG_OPER_BREAK
:
3623 slang_info_log_error(A
->log
, "'break' not in loop");
3626 return new_break(A
->CurLoop
);
3627 case SLANG_OPER_CONTINUE
:
3629 slang_info_log_error(A
->log
, "'continue' not in loop");
3632 return _slang_gen_continue(A
, oper
);
3633 case SLANG_OPER_DISCARD
:
3634 return new_node0(IR_KILL
);
3636 case SLANG_OPER_EQUAL
:
3637 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
3638 case SLANG_OPER_NOTEQUAL
:
3639 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
3640 case SLANG_OPER_GREATER
:
3641 return _slang_gen_compare(A
, oper
, IR_SGT
);
3642 case SLANG_OPER_LESS
:
3643 return _slang_gen_compare(A
, oper
, IR_SLT
);
3644 case SLANG_OPER_GREATEREQUAL
:
3645 return _slang_gen_compare(A
, oper
, IR_SGE
);
3646 case SLANG_OPER_LESSEQUAL
:
3647 return _slang_gen_compare(A
, oper
, IR_SLE
);
3648 case SLANG_OPER_ADD
:
3651 assert(oper
->num_children
== 2);
3652 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
3655 case SLANG_OPER_SUBTRACT
:
3658 assert(oper
->num_children
== 2);
3659 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
3662 case SLANG_OPER_MULTIPLY
:
3665 assert(oper
->num_children
== 2);
3666 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
3669 case SLANG_OPER_DIVIDE
:
3672 assert(oper
->num_children
== 2);
3673 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
3676 case SLANG_OPER_MINUS
:
3679 assert(oper
->num_children
== 1);
3680 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
3683 case SLANG_OPER_PLUS
:
3684 /* +expr --> do nothing */
3685 return _slang_gen_operation(A
, &oper
->children
[0]);
3686 case SLANG_OPER_VARIABLE_DECL
:
3687 return _slang_gen_declaration(A
, oper
);
3688 case SLANG_OPER_ASSIGN
:
3689 return _slang_gen_assignment(A
, oper
);
3690 case SLANG_OPER_ADDASSIGN
:
3693 assert(oper
->num_children
== 2);
3694 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
3697 case SLANG_OPER_SUBASSIGN
:
3700 assert(oper
->num_children
== 2);
3701 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
3705 case SLANG_OPER_MULASSIGN
:
3708 assert(oper
->num_children
== 2);
3709 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
3712 case SLANG_OPER_DIVASSIGN
:
3715 assert(oper
->num_children
== 2);
3716 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
3719 case SLANG_OPER_LOGICALAND
:
3722 assert(oper
->num_children
== 2);
3723 n
= _slang_gen_logical_and(A
, oper
);
3726 case SLANG_OPER_LOGICALOR
:
3729 assert(oper
->num_children
== 2);
3730 n
= _slang_gen_logical_or(A
, oper
);
3733 case SLANG_OPER_LOGICALXOR
:
3734 return _slang_gen_xor(A
, oper
);
3735 case SLANG_OPER_NOT
:
3736 return _slang_gen_not(A
, oper
);
3737 case SLANG_OPER_SELECT
: /* b ? x : y */
3740 assert(oper
->num_children
== 3);
3741 n
= _slang_gen_select(A
, oper
);
3745 case SLANG_OPER_ASM
:
3746 return _slang_gen_asm(A
, oper
, NULL
);
3747 case SLANG_OPER_CALL
:
3748 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
3750 case SLANG_OPER_METHOD
:
3751 return _slang_gen_method_call(A
, oper
);
3752 case SLANG_OPER_RETURN
:
3753 return _slang_gen_return(A
, oper
);
3754 case SLANG_OPER_LABEL
:
3755 return new_label(oper
->label
);
3756 case SLANG_OPER_IDENTIFIER
:
3757 return _slang_gen_variable(A
, oper
);
3759 return _slang_gen_if(A
, oper
);
3760 case SLANG_OPER_FIELD
:
3761 return _slang_gen_struct_field(A
, oper
);
3762 case SLANG_OPER_SUBSCRIPT
:
3763 return _slang_gen_array_element(A
, oper
);
3764 case SLANG_OPER_LITERAL_FLOAT
:
3766 case SLANG_OPER_LITERAL_INT
:
3768 case SLANG_OPER_LITERAL_BOOL
:
3769 return new_float_literal(oper
->literal
, oper
->literal_size
);
3771 case SLANG_OPER_POSTINCREMENT
: /* var++ */
3774 assert(oper
->num_children
== 1);
3775 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
3778 case SLANG_OPER_POSTDECREMENT
: /* var-- */
3781 assert(oper
->num_children
== 1);
3782 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
3785 case SLANG_OPER_PREINCREMENT
: /* ++var */
3788 assert(oper
->num_children
== 1);
3789 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
3792 case SLANG_OPER_PREDECREMENT
: /* --var */
3795 assert(oper
->num_children
== 1);
3796 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
3800 case SLANG_OPER_NON_INLINED_CALL
:
3801 case SLANG_OPER_SEQUENCE
:
3803 slang_ir_node
*tree
= NULL
;
3805 for (i
= 0; i
< oper
->num_children
; i
++) {
3806 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
3807 tree
= new_seq(tree
, n
);
3809 tree
->Store
= n
->Store
;
3811 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
3812 tree
= new_function_call(tree
, oper
->label
);
3817 case SLANG_OPER_NONE
:
3818 case SLANG_OPER_VOID
:
3819 /* returning NULL here would generate an error */
3820 return new_node0(IR_NOP
);
3823 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
3825 return new_node0(IR_NOP
);
3833 * Compute total size of array give size of element, number of elements.
3836 array_size(GLint baseSize
, GLint arrayLen
)
3840 /* round up base type to multiple of 4 */
3841 total
= ((baseSize
+ 3) & ~0x3) * MAX2(arrayLen
, 1);
3851 * Called by compiler when a global variable has been parsed/compiled.
3852 * Here we examine the variable's type to determine what kind of register
3853 * storage will be used.
3855 * A uniform such as "gl_Position" will become the register specification
3856 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
3857 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
3859 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
3860 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
3861 * actual texture unit (as specified by the user calling glUniform1i()).
3864 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
3865 slang_unit_type type
)
3867 struct gl_program
*prog
= A
->program
;
3868 const char *varName
= (char *) var
->a_name
;
3869 GLboolean success
= GL_TRUE
;
3870 slang_ir_storage
*store
= NULL
;
3872 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3873 const GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
3874 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3876 if (texIndex
!= -1) {
3877 /* This is a texture sampler variable...
3878 * store->File = PROGRAM_SAMPLER
3879 * store->Index = sampler number (0..7, typically)
3880 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
3882 if (var
->initializer
) {
3883 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
3886 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
3887 /* disallow rect samplers */
3888 if (var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECT
||
3889 var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
) {
3890 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
3895 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
3896 store
= _slang_new_ir_storage(PROGRAM_SAMPLER
, sampNum
, texIndex
);
3898 if (dbg
) printf("SAMPLER ");
3900 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3901 /* Uniform variable */
3902 const GLint totalSize
= array_size(size
, var
->array_len
);
3903 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
3906 /* user-defined uniform */
3907 if (datatype
== GL_NONE
) {
3908 if (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
) {
3909 /* temporary work-around */
3910 GLenum datatype
= GL_FLOAT
;
3911 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
3912 totalSize
, datatype
, NULL
);
3913 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
3914 totalSize
, swizzle
);
3916 /* XXX what we need to do is unroll the struct into its
3917 * basic types, creating a uniform variable for each.
3925 * Should produce uniforms:
3926 * "f.a" (GL_FLOAT_VEC3)
3927 * "f.b" (GL_FLOAT_VEC4)
3930 if (var
->initializer
) {
3931 slang_info_log_error(A
->log
,
3932 "unsupported initializer for uniform '%s'", varName
);
3937 slang_info_log_error(A
->log
,
3938 "invalid datatype for uniform variable %s",
3945 const GLfloat
*initialValues
= NULL
;
3946 if (var
->initializer
) {
3947 _slang_simplify(var
->initializer
, &A
->space
, A
->atoms
);
3948 if (var
->initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
3949 var
->initializer
->type
== SLANG_OPER_LITERAL_INT
) {
3950 /* simple float/vector initializer */
3951 initialValues
= var
->initializer
->literal
;
3954 /* complex initializer */
3955 slang_info_log_error(A
->log
,
3956 "unsupported initializer for uniform '%s'", varName
);
3961 uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
3962 totalSize
, datatype
, initialValues
);
3963 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
3964 totalSize
, swizzle
);
3968 /* pre-defined uniform, like gl_ModelviewMatrix */
3969 /* We know it's a uniform, but don't allocate storage unless
3972 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
3973 totalSize
, swizzle
);
3975 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
3977 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
3978 const GLint totalSize
= array_size(size
, var
->array_len
);
3980 /* varyings must be float, vec or mat */
3981 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
3982 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
3983 slang_info_log_error(A
->log
,
3984 "varying '%s' must be float/vector/matrix",
3989 if (var
->initializer
) {
3990 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
3996 /* user-defined varying */
4002 if (var
->type
.centroid
== SLANG_CENTROID
)
4003 flags
|= PROG_PARAM_BIT_CENTROID
;
4004 if (var
->type
.variant
== SLANG_INVARIANT
)
4005 flags
|= PROG_PARAM_BIT_INVARIANT
;
4007 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
4009 swizzle
= _slang_var_swizzle(size
, 0);
4010 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
4011 totalSize
, swizzle
);
4014 /* pre-defined varying, like gl_Color or gl_TexCoord */
4015 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
4016 /* fragment program input */
4018 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4021 assert(index
< FRAG_ATTRIB_MAX
);
4022 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
4026 /* vertex program output */
4027 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4028 GLuint swizzle
= _slang_var_swizzle(size
, 0);
4030 assert(index
< VERT_RESULT_MAX
);
4031 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
4032 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
4035 if (dbg
) printf("V/F ");
4037 if (dbg
) printf("VARYING ");
4039 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
4042 /* attributes must be float, vec or mat */
4043 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
4044 slang_info_log_error(A
->log
,
4045 "attribute '%s' must be float/vector/matrix",
4051 /* user-defined vertex attribute */
4052 const GLint attr
= -1; /* unknown */
4053 swizzle
= _slang_var_swizzle(size
, 0);
4054 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
4055 size
, datatype
, attr
);
4057 index
= VERT_ATTRIB_GENERIC0
+ index
;
4060 /* pre-defined vertex attrib */
4061 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
4064 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4065 if (dbg
) printf("ATTRIB ");
4067 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
4068 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
4069 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4071 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4072 if (dbg
) printf("INPUT ");
4074 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
4075 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
4076 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4077 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
4080 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
4081 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
4082 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
4083 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
4085 if (dbg
) printf("OUTPUT ");
4087 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
4088 /* pre-defined global constant, like gl_MaxLights */
4089 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
4090 if (dbg
) printf("CONST ");
4093 /* ordinary variable (may be const) */
4096 /* IR node to declare the variable */
4097 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
4099 /* emit GPU instructions */
4100 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_FALSE
, A
->log
);
4102 _slang_free_ir_tree(n
);
4105 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
4106 store
? store
->Index
: -2);
4109 var
->store
= store
; /* save var's storage info */
4111 var
->declared
= GL_TRUE
;
4118 * Produce an IR tree from a function AST (fun->body).
4119 * Then call the code emitter to convert the IR tree into gl_program
4123 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
4126 GLboolean success
= GL_TRUE
;
4128 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
4129 /* we only really generate code for main, all other functions get
4130 * inlined or codegen'd upon an actual call.
4133 /* do some basic error checking though */
4134 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
4135 /* check that non-void functions actually return something */
4137 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
4139 slang_info_log_error(A
->log
,
4140 "function \"%s\" has no return statement",
4141 (char *) fun
->header
.a_name
);
4143 "function \"%s\" has no return statement\n",
4144 (char *) fun
->header
.a_name
);
4149 return GL_TRUE
; /* not an error */
4153 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
4154 slang_print_function(fun
, 1);
4157 /* should have been allocated earlier: */
4158 assert(A
->program
->Parameters
);
4159 assert(A
->program
->Varying
);
4160 assert(A
->vartable
);
4162 A
->CurFunction
= fun
;
4164 /* fold constant expressions, etc. */
4165 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
4168 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
4169 slang_print_function(fun
, 1);
4172 /* Create an end-of-function label */
4173 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
4175 /* push new vartable scope */
4176 _slang_push_var_table(A
->vartable
);
4178 /* Generate IR tree for the function body code */
4179 n
= _slang_gen_operation(A
, fun
->body
);
4181 n
= new_node1(IR_SCOPE
, n
);
4183 /* pop vartable, restore previous */
4184 _slang_pop_var_table(A
->vartable
);
4187 /* XXX record error */
4191 /* append an end-of-function-label to IR tree */
4192 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
4194 /*_slang_label_delete(A->curFuncEndLabel);*/
4195 A
->curFuncEndLabel
= NULL
;
4198 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
4199 slang_print_function(fun
, 1);
4202 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
4203 _slang_print_ir_tree(n
, 0);
4206 printf("************* End codegen function ************\n\n");
4209 /* Emit program instructions */
4210 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
, A
->log
);
4211 _slang_free_ir_tree(n
);
4213 /* free codegen context */
4215 _mesa_free(A->codegen);