2 * Mesa 3-D graphics library
4 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
5 * Copyright (C) 2008 VMware, Inc. 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
)
669 slang_ir_node
*n
= new_node0(IR_VAR
);
671 _slang_attach_storage(n
, var
);
678 * Check if the given function is really just a wrapper for a
679 * basic assembly instruction.
682 slang_is_asm_function(const slang_function
*fun
)
684 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
685 fun
->body
->num_children
== 1 &&
686 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
694 _slang_is_noop(const slang_operation
*oper
)
697 oper
->type
== SLANG_OPER_VOID
||
698 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
706 * Recursively search tree for a node of the given type.
708 static slang_operation
*
709 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
712 if (oper
->type
== type
)
714 for (i
= 0; i
< oper
->num_children
; i
++) {
715 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
724 * Count the number of operations of the given time rooted at 'oper'.
727 _slang_count_node_type(slang_operation
*oper
, slang_operation_type type
)
730 if (oper
->type
== type
) {
733 for (i
= 0; i
< oper
->num_children
; i
++) {
734 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
741 * Check if the 'return' statement found under 'oper' is a "tail return"
742 * that can be no-op'd. For example:
747 * return; // this is a no-op
750 * This is used when determining if a function can be inlined. If the
751 * 'return' is not the last statement, we can't inline the function since
752 * we still need the semantic behaviour of the 'return' but we don't want
753 * to accidentally return from the _calling_ function. We'd need to use an
754 * unconditional branch, but we don't have such a GPU instruction (not
758 _slang_is_tail_return(const slang_operation
*oper
)
760 GLuint k
= oper
->num_children
;
763 const slang_operation
*last
= &oper
->children
[k
- 1];
764 if (last
->type
== SLANG_OPER_RETURN
)
766 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
767 last
->type
== SLANG_OPER_LABEL
)
768 k
--; /* try prev child */
769 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
770 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
771 /* try sub-children */
772 return _slang_is_tail_return(last
);
782 slang_resolve_variable(slang_operation
*oper
)
784 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
785 oper
->var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
791 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
794 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
795 GLuint substCount
, slang_variable
**substOld
,
796 slang_operation
**substNew
, GLboolean isLHS
)
798 switch (oper
->type
) {
799 case SLANG_OPER_VARIABLE_DECL
:
801 slang_variable
*v
= _slang_variable_locate(oper
->locals
,
802 oper
->a_id
, GL_TRUE
);
804 if (v
->initializer
&& oper
->num_children
== 0) {
805 /* set child of oper to copy of initializer */
806 oper
->num_children
= 1;
807 oper
->children
= slang_operation_new(1);
808 slang_operation_copy(&oper
->children
[0], v
->initializer
);
810 if (oper
->num_children
== 1) {
811 /* the initializer */
812 slang_substitute(A
, &oper
->children
[0], substCount
,
813 substOld
, substNew
, GL_FALSE
);
817 case SLANG_OPER_IDENTIFIER
:
818 assert(oper
->num_children
== 0);
819 if (1/**!isLHS XXX FIX */) {
820 slang_atom id
= oper
->a_id
;
823 v
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
825 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
829 /* look for a substitution */
830 for (i
= 0; i
< substCount
; i
++) {
831 if (v
== substOld
[i
]) {
832 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
833 #if 0 /* DEBUG only */
834 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
835 assert(substNew
[i
]->var
);
836 assert(substNew
[i
]->var
->a_name
);
837 printf("Substitute %s with %s in id node %p\n",
838 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
842 printf("Substitute %s with %f in id node %p\n",
843 (char*)v
->a_name
, substNew
[i
]->literal
[0],
847 slang_operation_copy(oper
, substNew
[i
]);
854 case SLANG_OPER_RETURN
:
855 /* do return replacement here too */
856 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
857 if (oper
->num_children
== 1 && !_slang_is_noop(&oper
->children
[0])) {
863 * then do substitutions on the assignment.
865 slang_operation
*blockOper
, *assignOper
, *returnOper
;
867 /* check if function actually has a return type */
868 assert(A
->CurFunction
);
869 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
870 slang_info_log_error(A
->log
, "illegal return expression");
874 blockOper
= slang_operation_new(1);
875 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
876 blockOper
->num_children
= 2;
877 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
878 blockOper
->children
= slang_operation_new(2);
879 assignOper
= blockOper
->children
+ 0;
880 returnOper
= blockOper
->children
+ 1;
882 assignOper
->type
= SLANG_OPER_ASSIGN
;
883 assignOper
->num_children
= 2;
884 assignOper
->locals
->outer_scope
= blockOper
->locals
;
885 assignOper
->children
= slang_operation_new(2);
886 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
887 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
888 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
890 slang_operation_copy(&assignOper
->children
[1],
893 returnOper
->type
= SLANG_OPER_RETURN
; /* return w/ no value */
894 assert(returnOper
->num_children
== 0);
896 /* do substitutions on the "__retVal = expr" sub-tree */
897 slang_substitute(A
, assignOper
,
898 substCount
, substOld
, substNew
, GL_FALSE
);
900 /* install new code */
901 slang_operation_copy(oper
, blockOper
);
902 slang_operation_destruct(blockOper
);
905 /* check if return value was expected */
906 assert(A
->CurFunction
);
907 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
908 slang_info_log_error(A
->log
, "return statement requires an expression");
914 case SLANG_OPER_ASSIGN
:
915 case SLANG_OPER_SUBSCRIPT
:
917 * child[0] can't have substitutions but child[1] can.
919 slang_substitute(A
, &oper
->children
[0],
920 substCount
, substOld
, substNew
, GL_TRUE
);
921 slang_substitute(A
, &oper
->children
[1],
922 substCount
, substOld
, substNew
, GL_FALSE
);
924 case SLANG_OPER_FIELD
:
926 slang_substitute(A
, &oper
->children
[0],
927 substCount
, substOld
, substNew
, GL_TRUE
);
932 for (i
= 0; i
< oper
->num_children
; i
++)
933 slang_substitute(A
, &oper
->children
[i
],
934 substCount
, substOld
, substNew
, GL_FALSE
);
941 * Produce inline code for a call to an assembly instruction.
942 * This is typically used to compile a call to a built-in function like this:
944 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
946 * __asm vec4_lrp __retVal, a, y, x;
951 * r = mix(p1, p2, p3);
961 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
963 static slang_operation
*
964 slang_inline_asm_function(slang_assemble_ctx
*A
,
965 slang_function
*fun
, slang_operation
*oper
)
967 const GLuint numArgs
= oper
->num_children
;
969 slang_operation
*inlined
;
970 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
971 slang_variable
**substOld
;
972 slang_operation
**substNew
;
974 ASSERT(slang_is_asm_function(fun
));
975 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
978 printf("Inline %s as %s\n",
979 (char*) fun->header.a_name,
980 (char*) fun->body->children[0].a_id);
984 * We'll substitute formal params with actual args in the asm call.
986 substOld
= (slang_variable
**)
987 _slang_alloc(numArgs
* sizeof(slang_variable
*));
988 substNew
= (slang_operation
**)
989 _slang_alloc(numArgs
* sizeof(slang_operation
*));
990 for (i
= 0; i
< numArgs
; i
++) {
991 substOld
[i
] = fun
->parameters
->variables
[i
];
992 substNew
[i
] = oper
->children
+ i
;
995 /* make a copy of the code to inline */
996 inlined
= slang_operation_new(1);
997 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
999 /* get rid of the __retVal child */
1000 inlined
->num_children
--;
1001 for (i
= 0; i
< inlined
->num_children
; i
++) {
1002 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1006 /* now do formal->actual substitutions */
1007 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1009 _slang_free(substOld
);
1010 _slang_free(substNew
);
1013 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1014 (char *) fun
->header
.a_name
);
1015 slang_print_tree(inlined
, 3);
1016 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1024 * Inline the given function call operation.
1025 * Return a new slang_operation that corresponds to the inlined code.
1027 static slang_operation
*
1028 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1029 slang_operation
*oper
, slang_operation
*returnOper
)
1036 ParamMode
*paramMode
;
1037 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1038 const GLuint numArgs
= oper
->num_children
;
1039 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1040 slang_operation
*args
= oper
->children
;
1041 slang_operation
*inlined
, *top
;
1042 slang_variable
**substOld
;
1043 slang_operation
**substNew
;
1044 GLuint substCount
, numCopyIn
, i
;
1045 slang_function
*prevFunction
;
1046 slang_variable_scope
*newScope
= NULL
;
1049 prevFunction
= A
->CurFunction
;
1050 A
->CurFunction
= fun
;
1052 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1053 assert(fun
->param_count
== totalArgs
);
1055 /* allocate temporary arrays */
1056 paramMode
= (ParamMode
*)
1057 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1058 substOld
= (slang_variable
**)
1059 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1060 substNew
= (slang_operation
**)
1061 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1064 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1065 (char *) fun
->header
.a_name
,
1066 fun
->parameters
->num_variables
, numArgs
);
1069 if (haveRetValue
&& !returnOper
) {
1070 /* Create 3-child comma sequence for inlined code:
1071 * child[0]: declare __resultTmp
1072 * child[1]: inlined function body
1073 * child[2]: __resultTmp
1075 slang_operation
*commaSeq
;
1076 slang_operation
*declOper
= NULL
;
1077 slang_variable
*resultVar
;
1079 commaSeq
= slang_operation_new(1);
1080 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1081 assert(commaSeq
->locals
);
1082 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1083 commaSeq
->num_children
= 3;
1084 commaSeq
->children
= slang_operation_new(3);
1085 /* allocate the return var */
1086 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1088 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1089 (void*)commaSeq->locals, (char *) fun->header.a_name);
1092 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1093 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1094 resultVar
->isTemp
= GL_TRUE
;
1096 /* child[0] = __resultTmp declaration */
1097 declOper
= &commaSeq
->children
[0];
1098 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1099 declOper
->a_id
= resultVar
->a_name
;
1100 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1102 /* child[1] = function body */
1103 inlined
= &commaSeq
->children
[1];
1104 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1106 /* child[2] = __resultTmp reference */
1107 returnOper
= &commaSeq
->children
[2];
1108 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1109 returnOper
->a_id
= resultVar
->a_name
;
1110 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1115 top
= inlined
= slang_operation_new(1);
1116 /* XXXX this may be inappropriate!!!! */
1117 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1121 assert(inlined
->locals
);
1123 /* Examine the parameters, look for inout/out params, look for possible
1124 * substitutions, etc:
1125 * param type behaviour
1126 * in copy actual to local
1127 * const in substitute param with actual
1131 for (i
= 0; i
< totalArgs
; i
++) {
1132 slang_variable
*p
= fun
->parameters
->variables
[i
];
1134 printf("Param %d: %s %s \n", i,
1135 slang_type_qual_string(p->type.qualifier),
1136 (char *) p->a_name);
1138 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1139 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1140 /* an output param */
1141 slang_operation
*arg
;
1146 paramMode
[i
] = SUBST
;
1148 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1149 slang_resolve_variable(arg
);
1151 /* replace parameter 'p' with argument 'arg' */
1152 substOld
[substCount
] = p
;
1153 substNew
[substCount
] = arg
; /* will get copied */
1156 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1157 /* a constant input param */
1158 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1159 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
1160 /* replace all occurances of this parameter variable with the
1161 * actual argument variable or a literal.
1163 paramMode
[i
] = SUBST
;
1164 slang_resolve_variable(&args
[i
]);
1165 substOld
[substCount
] = p
;
1166 substNew
[substCount
] = &args
[i
]; /* will get copied */
1170 paramMode
[i
] = COPY_IN
;
1174 paramMode
[i
] = COPY_IN
;
1176 assert(paramMode
[i
]);
1179 /* actual code inlining: */
1180 slang_operation_copy(inlined
, fun
->body
);
1182 /*** XXX review this */
1183 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1184 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1185 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1188 printf("======================= orig body code ======================\n");
1189 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1190 slang_print_tree(fun
->body
, 8);
1191 printf("======================= copied code =========================\n");
1192 slang_print_tree(inlined
, 8);
1195 /* do parameter substitution in inlined code: */
1196 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1199 printf("======================= subst code ==========================\n");
1200 slang_print_tree(inlined
, 8);
1201 printf("=============================================================\n");
1204 /* New prolog statements: (inserted before the inlined code)
1205 * Copy the 'in' arguments.
1208 for (i
= 0; i
< numArgs
; i
++) {
1209 if (paramMode
[i
] == COPY_IN
) {
1210 slang_variable
*p
= fun
->parameters
->variables
[i
];
1211 /* declare parameter 'p' */
1212 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1216 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1217 assert(decl
->locals
);
1218 decl
->locals
->outer_scope
= inlined
->locals
;
1219 decl
->a_id
= p
->a_name
;
1220 decl
->num_children
= 1;
1221 decl
->children
= slang_operation_new(1);
1223 /* child[0] is the var's initializer */
1224 slang_operation_copy(&decl
->children
[0], args
+ i
);
1226 /* add parameter 'p' to the local variable scope here */
1228 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1229 pCopy
->type
= p
->type
;
1230 pCopy
->a_name
= p
->a_name
;
1231 pCopy
->array_len
= p
->array_len
;
1234 newScope
= inlined
->locals
;
1239 /* Now add copies of the function's local vars to the new variable scope */
1240 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1241 slang_variable
*p
= fun
->parameters
->variables
[i
];
1242 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1243 pCopy
->type
= p
->type
;
1244 pCopy
->a_name
= p
->a_name
;
1245 pCopy
->array_len
= p
->array_len
;
1249 /* New epilog statements:
1250 * 1. Create end of function label to jump to from return statements.
1251 * 2. Copy the 'out' parameter vars
1254 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1256 inlined
->num_children
);
1257 lab
->type
= SLANG_OPER_LABEL
;
1258 lab
->label
= A
->curFuncEndLabel
;
1261 for (i
= 0; i
< totalArgs
; i
++) {
1262 if (paramMode
[i
] == COPY_OUT
) {
1263 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1264 /* actualCallVar = outParam */
1265 /*if (i > 0 || !haveRetValue)*/
1266 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1268 inlined
->num_children
);
1269 ass
->type
= SLANG_OPER_ASSIGN
;
1270 ass
->num_children
= 2;
1271 ass
->locals
->outer_scope
= inlined
->locals
;
1272 ass
->children
= slang_operation_new(2);
1273 ass
->children
[0] = args
[i
]; /*XXX copy */
1274 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1275 ass
->children
[1].a_id
= p
->a_name
;
1276 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1280 _slang_free(paramMode
);
1281 _slang_free(substOld
);
1282 _slang_free(substNew
);
1284 /* Update scoping to use the new local vars instead of the
1285 * original function's vars. This is especially important
1286 * for nested inlining.
1289 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1292 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1293 (char *) fun
->header
.a_name
,
1294 fun
->parameters
->num_variables
, numArgs
);
1295 slang_print_tree(top
, 0);
1299 A
->CurFunction
= prevFunction
;
1305 static slang_ir_node
*
1306 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1307 slang_operation
*oper
, slang_operation
*dest
)
1310 slang_operation
*inlined
;
1311 slang_label
*prevFuncEndLabel
;
1314 prevFuncEndLabel
= A
->curFuncEndLabel
;
1315 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1316 A
->curFuncEndLabel
= _slang_label_new(name
);
1317 assert(A
->curFuncEndLabel
);
1319 if (slang_is_asm_function(fun
) && !dest
) {
1320 /* assemble assembly function - tree style */
1321 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1324 /* non-assembly function */
1325 /* We always generate an "inline-able" block of code here.
1327 * 1. insert the inline code
1328 * 2. Generate a call to the "inline" code as a subroutine
1332 slang_operation
*ret
= NULL
;
1334 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1338 ret
= _slang_find_node_type(inlined
, SLANG_OPER_RETURN
);
1340 /* check if this is a "tail" return */
1341 if (_slang_count_node_type(inlined
, SLANG_OPER_RETURN
) == 1 &&
1342 _slang_is_tail_return(inlined
)) {
1343 /* The only RETURN is the last stmt in the function, no-op it
1344 * and inline the function body.
1346 ret
->type
= SLANG_OPER_NONE
;
1349 slang_operation
*callOper
;
1350 /* The function we're calling has one or more 'return' statements.
1351 * So, we can't truly inline this function because we need to
1352 * implement 'return' with RET (and CAL).
1353 * Nevertheless, we performed "inlining" to make a new instance
1354 * of the function body to deal with static register allocation.
1356 * XXX check if there's one 'return' and if it's the very last
1357 * statement in the function - we can optimize that case.
1359 assert(inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1360 inlined
->type
== SLANG_OPER_SEQUENCE
);
1362 if (_slang_function_has_return_value(fun
) && !dest
) {
1363 assert(inlined
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1364 assert(inlined
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1365 callOper
= &inlined
->children
[1];
1370 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1371 callOper
->fun
= fun
;
1372 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1380 /* Replace the function call with the inlined block (or new CALL stmt) */
1381 slang_operation_destruct(oper
);
1383 _slang_free(inlined
);
1386 assert(inlined
->locals
);
1387 printf("*** Inlined code for call to %s:\n",
1388 (char*) fun
->header
.a_name
);
1389 slang_print_tree(oper
, 10);
1393 n
= _slang_gen_operation(A
, oper
);
1395 /*_slang_label_delete(A->curFuncEndLabel);*/
1396 A
->curFuncEndLabel
= prevFuncEndLabel
;
1402 static slang_asm_info
*
1403 slang_find_asm_info(const char *name
)
1406 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1407 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1416 * Return the default swizzle mask for accessing a variable of the
1417 * given size (in floats). If size = 1, comp is used to identify
1418 * which component [0..3] of the register holds the variable.
1421 _slang_var_swizzle(GLint size
, GLint comp
)
1425 return MAKE_SWIZZLE4(comp
, comp
, comp
, comp
);
1427 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_NIL
, SWIZZLE_NIL
);
1429 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_NIL
);
1431 return SWIZZLE_XYZW
;
1437 * Some write-masked assignments are simple, but others are hard.
1440 * v.xy = vec2(a, b);
1443 * v.zy = vec2(a, b);
1444 * this gets transformed/swizzled into:
1445 * v.zy = vec2(a, b).*yx* (* = don't care)
1446 * This function helps to determine simple vs. non-simple.
1449 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1451 switch (writemask
) {
1453 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1455 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1457 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1459 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1461 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1462 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1464 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1465 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1466 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1467 case WRITEMASK_XYZW
:
1468 return swizzle
== SWIZZLE_NOOP
;
1476 * Convert the given swizzle into a writemask. In some cases this
1477 * is trivial, in other cases, we'll need to also swizzle the right
1478 * hand side to put components in the right places.
1479 * See comment above for more info.
1480 * XXX this function could be simplified and should probably be renamed.
1481 * \param swizzle the incoming swizzle
1482 * \param writemaskOut returns the writemask
1483 * \param swizzleOut swizzle to apply to the right-hand-side
1484 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1487 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1488 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1490 GLuint mask
= 0x0, newSwizzle
[4];
1493 /* make new dst writemask, compute size */
1494 for (i
= 0; i
< 4; i
++) {
1495 const GLuint swz
= GET_SWZ(swizzle
, i
);
1496 if (swz
== SWIZZLE_NIL
) {
1500 assert(swz
>= 0 && swz
<= 3);
1502 if (swizzle
!= SWIZZLE_XXXX
&&
1503 swizzle
!= SWIZZLE_YYYY
&&
1504 swizzle
!= SWIZZLE_ZZZZ
&&
1505 swizzle
!= SWIZZLE_WWWW
&&
1506 (mask
& (1 << swz
))) {
1507 /* a channel can't be specified twice (ex: ".xyyz") */
1508 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1509 _mesa_swizzle_string(swizzle
, 0, 0));
1515 assert(mask
<= 0xf);
1516 size
= i
; /* number of components in mask/swizzle */
1518 *writemaskOut
= mask
;
1520 /* make new src swizzle, by inversion */
1521 for (i
= 0; i
< 4; i
++) {
1522 newSwizzle
[i
] = i
; /*identity*/
1524 for (i
= 0; i
< size
; i
++) {
1525 const GLuint swz
= GET_SWZ(swizzle
, i
);
1526 newSwizzle
[swz
] = i
;
1528 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1533 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1535 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1537 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1539 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1541 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1550 * Recursively traverse 'oper' to produce a swizzle mask in the event
1551 * of any vector subscripts and swizzle suffixes.
1552 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1555 resolve_swizzle(const slang_operation
*oper
)
1557 if (oper
->type
== SLANG_OPER_FIELD
) {
1558 /* writemask from .xyzw suffix */
1560 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1561 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1565 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1566 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1570 return SWIZZLE_XYZW
;
1572 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1573 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1574 /* writemask from [index] */
1575 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1576 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1581 swizzle
= SWIZZLE_XXXX
;
1584 swizzle
= SWIZZLE_YYYY
;
1587 swizzle
= SWIZZLE_ZZZZ
;
1590 swizzle
= SWIZZLE_WWWW
;
1593 swizzle
= SWIZZLE_XYZW
;
1595 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1599 return SWIZZLE_XYZW
;
1605 * Recursively descend through swizzle nodes to find the node's storage info.
1607 static slang_ir_storage
*
1608 get_store(const slang_ir_node
*n
)
1610 if (n
->Opcode
== IR_SWIZZLE
) {
1611 return get_store(n
->Children
[0]);
1619 * Generate IR tree for an asm instruction/operation such as:
1620 * __asm vec4_dot __retVal.x, v1, v2;
1622 static slang_ir_node
*
1623 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1624 slang_operation
*dest
)
1626 const slang_asm_info
*info
;
1627 slang_ir_node
*kids
[3], *n
;
1628 GLuint j
, firstOperand
;
1630 assert(oper
->type
== SLANG_OPER_ASM
);
1632 info
= slang_find_asm_info((char *) oper
->a_id
);
1634 _mesa_problem(NULL
, "undefined __asm function %s\n",
1635 (char *) oper
->a_id
);
1638 assert(info
->NumParams
<= 3);
1640 if (info
->NumParams
== oper
->num_children
) {
1641 /* Storage for result is not specified.
1642 * Children[0], [1], [2] are the operands.
1647 /* Storage for result (child[0]) is specified.
1648 * Children[1], [2], [3] are the operands.
1653 /* assemble child(ren) */
1654 kids
[0] = kids
[1] = kids
[2] = NULL
;
1655 for (j
= 0; j
< info
->NumParams
; j
++) {
1656 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1661 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1664 /* Setup n->Store to be a particular location. Otherwise, storage
1665 * for the result (a temporary) will be allocated later.
1667 slang_operation
*dest_oper
;
1670 dest_oper
= &oper
->children
[0];
1672 n0
= _slang_gen_operation(A
, dest_oper
);
1677 n
->Store
= n0
->Store
;
1679 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1689 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1692 for (i
= 0; i
< scope
->num_functions
; i
++) {
1693 slang_function
*f
= &scope
->functions
[i
];
1694 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1695 printf(" %s (%d args)\n", name
, f
->param_count
);
1698 if (scope
->outer_scope
)
1699 print_funcs(scope
->outer_scope
, name
);
1704 * Find a function of the given name, taking 'numArgs' arguments.
1705 * This is the function we'll try to call when there is no exact match
1706 * between function parameters and call arguments.
1708 * XXX we should really create a list of candidate functions and try
1711 static slang_function
*
1712 _slang_find_function_by_argc(slang_function_scope
*scope
,
1713 const char *name
, int numArgs
)
1717 for (i
= 0; i
< scope
->num_functions
; i
++) {
1718 slang_function
*f
= &scope
->functions
[i
];
1719 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1720 int haveRetValue
= _slang_function_has_return_value(f
);
1721 if (numArgs
== f
->param_count
- haveRetValue
)
1725 scope
= scope
->outer_scope
;
1732 static slang_function
*
1733 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
1736 slang_function
*maxFunc
= NULL
;
1741 for (i
= 0; i
< scope
->num_functions
; i
++) {
1742 slang_function
*f
= &scope
->functions
[i
];
1743 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1744 if (f
->param_count
> maxArgs
) {
1745 maxArgs
= f
->param_count
;
1750 scope
= scope
->outer_scope
;
1758 * Generate a new slang_function which is a constructor for a user-defined
1761 static slang_function
*
1762 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
1764 const GLint numFields
= str
->fields
->num_variables
;
1765 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1767 /* function header (name, return type) */
1768 fun
->header
.a_name
= str
->a_name
;
1769 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1770 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
1771 fun
->header
.type
.specifier
._struct
= str
;
1773 /* function parameters (= struct's fields) */
1776 for (i
= 0; i
< numFields
; i
++) {
1778 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1780 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1781 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
1782 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1784 fun
->param_count
= fun
->parameters
->num_variables
;
1787 /* Add __retVal to params */
1789 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1790 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1792 p
->a_name
= a_retVal
;
1793 p
->type
= fun
->header
.type
;
1794 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1798 /* function body is:
1808 slang_variable_scope
*scope
;
1809 slang_variable
*var
;
1812 fun
->body
= slang_operation_new(1);
1813 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1814 fun
->body
->num_children
= numFields
+ 2;
1815 fun
->body
->children
= slang_operation_new(numFields
+ 2);
1817 scope
= fun
->body
->locals
;
1818 scope
->outer_scope
= fun
->parameters
;
1820 /* create local var 't' */
1821 var
= slang_variable_scope_grow(scope
);
1822 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
1823 var
->type
= fun
->header
.type
;
1827 slang_operation
*decl
;
1829 decl
= &fun
->body
->children
[0];
1830 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1831 decl
->locals
= _slang_variable_scope_new(scope
);
1832 decl
->a_id
= var
->a_name
;
1835 /* assign params to fields of t */
1836 for (i
= 0; i
< numFields
; i
++) {
1837 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
1839 assign
->type
= SLANG_OPER_ASSIGN
;
1840 assign
->locals
= _slang_variable_scope_new(scope
);
1841 assign
->num_children
= 2;
1842 assign
->children
= slang_operation_new(2);
1845 slang_operation
*lhs
= &assign
->children
[0];
1847 lhs
->type
= SLANG_OPER_FIELD
;
1848 lhs
->locals
= _slang_variable_scope_new(scope
);
1849 lhs
->num_children
= 1;
1850 lhs
->children
= slang_operation_new(1);
1851 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1853 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1854 lhs
->children
[0].a_id
= var
->a_name
;
1855 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
1858 lhs
->children
[1].num_children
= 1;
1859 lhs
->children
[1].children
= slang_operation_new(1);
1860 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
1861 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
1862 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
1867 slang_operation
*rhs
= &assign
->children
[1];
1869 rhs
->type
= SLANG_OPER_IDENTIFIER
;
1870 rhs
->locals
= _slang_variable_scope_new(scope
);
1871 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1877 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
1879 ret
->type
= SLANG_OPER_RETURN
;
1880 ret
->locals
= _slang_variable_scope_new(scope
);
1881 ret
->num_children
= 1;
1882 ret
->children
= slang_operation_new(1);
1883 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1884 ret
->children
[0].a_id
= var
->a_name
;
1885 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
1889 slang_print_function(fun, 1);
1896 * Find/create a function (constructor) for the given structure name.
1898 static slang_function
*
1899 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
1902 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
1903 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
1904 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
1905 /* found a structure type that matches the function name */
1906 if (!str
->constructor
) {
1907 /* create the constructor function now */
1908 str
->constructor
= _slang_make_struct_constructor(A
, str
);
1910 return str
->constructor
;
1918 * Generate a new slang_function to satisfy a call to an array constructor.
1919 * Ex: float[3](1., 2., 3.)
1921 static slang_function
*
1922 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
1924 slang_type_specifier_type baseType
;
1925 slang_function
*fun
;
1928 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1932 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
1934 num_elements
= oper
->num_children
;
1936 /* function header, return type */
1938 fun
->header
.a_name
= oper
->a_id
;
1939 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1940 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
1941 fun
->header
.type
.specifier
._array
=
1942 slang_type_specifier_new(baseType
, NULL
, NULL
);
1943 fun
->header
.type
.array_len
= num_elements
;
1946 /* function parameters (= number of elements) */
1949 for (i
= 0; i
< num_elements
; i
++) {
1951 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1953 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1955 snprintf(name
, sizeof(name
), "p%d", i
);
1956 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
1957 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1958 p
->type
.specifier
.type
= baseType
;
1960 fun
->param_count
= fun
->parameters
->num_variables
;
1963 /* Add __retVal to params */
1965 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1966 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1968 p
->a_name
= a_retVal
;
1969 p
->type
= fun
->header
.type
;
1970 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1971 p
->type
.specifier
.type
= baseType
;
1975 /* function body is:
1985 slang_variable_scope
*scope
;
1986 slang_variable
*var
;
1989 fun
->body
= slang_operation_new(1);
1990 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1991 fun
->body
->num_children
= num_elements
+ 2;
1992 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
1994 scope
= fun
->body
->locals
;
1995 scope
->outer_scope
= fun
->parameters
;
1997 /* create local var 't' */
1998 var
= slang_variable_scope_grow(scope
);
1999 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2000 var
->type
= fun
->header
.type
;/*XXX copy*/
2004 slang_operation
*decl
;
2006 decl
= &fun
->body
->children
[0];
2007 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2008 decl
->locals
= _slang_variable_scope_new(scope
);
2009 decl
->a_id
= var
->a_name
;
2012 /* assign params to elements of t */
2013 for (i
= 0; i
< num_elements
; i
++) {
2014 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2016 assign
->type
= SLANG_OPER_ASSIGN
;
2017 assign
->locals
= _slang_variable_scope_new(scope
);
2018 assign
->num_children
= 2;
2019 assign
->children
= slang_operation_new(2);
2022 slang_operation
*lhs
= &assign
->children
[0];
2024 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2025 lhs
->locals
= _slang_variable_scope_new(scope
);
2026 lhs
->num_children
= 2;
2027 lhs
->children
= slang_operation_new(2);
2029 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2030 lhs
->children
[0].a_id
= var
->a_name
;
2031 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2033 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2034 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2038 slang_operation
*rhs
= &assign
->children
[1];
2040 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2041 rhs
->locals
= _slang_variable_scope_new(scope
);
2042 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2048 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2050 ret
->type
= SLANG_OPER_RETURN
;
2051 ret
->locals
= _slang_variable_scope_new(scope
);
2052 ret
->num_children
= 1;
2053 ret
->children
= slang_operation_new(1);
2054 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2055 ret
->children
[0].a_id
= var
->a_name
;
2056 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2061 slang_print_function(fun, 1);
2069 _slang_is_vec_mat_type(const char *name
)
2071 static const char *vecmat_types
[] = {
2072 "float", "int", "bool",
2073 "vec2", "vec3", "vec4",
2074 "ivec2", "ivec3", "ivec4",
2075 "bvec2", "bvec3", "bvec4",
2076 "mat2", "mat3", "mat4",
2077 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2081 for (i
= 0; vecmat_types
[i
]; i
++)
2082 if (_mesa_strcmp(name
, vecmat_types
[i
]) == 0)
2089 * Assemble a function call, given a particular function name.
2090 * \param name the function's name (operators like '*' are possible).
2092 static slang_ir_node
*
2093 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2094 slang_operation
*oper
, slang_operation
*dest
)
2096 slang_operation
*params
= oper
->children
;
2097 const GLuint param_count
= oper
->num_children
;
2099 slang_function
*fun
;
2102 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2103 if (atom
== SLANG_ATOM_NULL
)
2106 if (oper
->array_constructor
) {
2107 /* this needs special handling */
2108 fun
= _slang_make_array_constructor(A
, oper
);
2111 /* Try to find function by name and exact argument type matching */
2112 GLboolean error
= GL_FALSE
;
2113 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2114 &A
->space
, A
->atoms
, A
->log
, &error
);
2116 slang_info_log_error(A
->log
,
2117 "Function '%s' not found (check argument types)",
2124 /* Next, try locating a constructor function for a user-defined type */
2125 fun
= _slang_locate_struct_constructor(A
, name
);
2129 * At this point, some heuristics are used to try to find a function
2130 * that matches the calling signature by means of casting or "unrolling"
2134 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2135 /* Next, if this call looks like a vec() or mat() constructor call,
2136 * try "unwinding" the args to satisfy a constructor.
2138 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2140 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2141 slang_info_log_error(A
->log
,
2142 "Function '%s' not found (check argument types)",
2149 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2150 /* Next, try casting args to the types of the formal parameters */
2151 int numArgs
= oper
->num_children
;
2152 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2153 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2154 slang_info_log_error(A
->log
,
2155 "Function '%s' not found (check argument types)",
2163 slang_info_log_error(A
->log
,
2164 "Function '%s' not found (check argument types)",
2169 slang_info_log_error(A
->log
,
2170 "Function '%s' prototyped but not defined. "
2171 "Separate compilation units not supported.",
2176 /* type checking to be sure function's return type matches 'dest' type */
2180 slang_typeinfo_construct(&t0
);
2181 typeof_operation(A
, dest
, &t0
);
2183 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2184 slang_info_log_error(A
->log
,
2185 "Incompatible type returned by call to '%s'",
2191 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2193 if (n
&& !n
->Store
&& !dest
2194 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2195 /* setup n->Store for the result of the function call */
2196 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2197 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2198 /*printf("Alloc storage for function result, size %d \n", size);*/
2201 if (oper
->array_constructor
) {
2202 /* free the temporary array constructor function now */
2203 slang_function_destruct(fun
);
2210 static slang_ir_node
*
2211 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2213 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2215 slang_variable
*var
;
2217 /* NOTE: In GLSL 1.20, there's only one kind of method
2218 * call: array.length(). Anything else is an error.
2220 if (oper
->a_id
!= a_length
) {
2221 slang_info_log_error(A
->log
,
2222 "Undefined method call '%s'", (char *) oper
->a_id
);
2226 /* length() takes no arguments */
2227 if (oper
->num_children
> 0) {
2228 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2232 /* lookup the object/variable */
2233 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2234 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2235 slang_info_log_error(A
->log
,
2236 "Undefined object '%s'", (char *) oper
->a_obj
);
2240 /* Create a float/literal IR node encoding the array length */
2241 n
= new_node0(IR_FLOAT
);
2243 n
->Value
[0] = (float) var
->array_len
;
2244 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2251 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2253 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2254 oper
->type
== SLANG_OPER_LITERAL_INT
||
2255 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2256 if (oper
->literal
[0])
2262 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2263 oper
->num_children
== 1) {
2264 return _slang_is_constant_cond(&oper
->children
[0], value
);
2271 * Test if an operation is a scalar or boolean.
2274 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2276 slang_typeinfo type
;
2279 slang_typeinfo_construct(&type
);
2280 typeof_operation(A
, oper
, &type
);
2281 size
= _slang_sizeof_type_specifier(&type
.spec
);
2282 slang_typeinfo_destruct(&type
);
2288 * Test if an operation is boolean.
2291 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2293 slang_typeinfo type
;
2296 slang_typeinfo_construct(&type
);
2297 typeof_operation(A
, oper
, &type
);
2298 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2299 slang_typeinfo_destruct(&type
);
2305 * Generate loop code using high-level IR_LOOP instruction
2307 static slang_ir_node
*
2308 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2312 * BREAK if !expr (child[0])
2313 * body code (child[1])
2315 slang_ir_node
*prevLoop
, *loop
, *breakIf
, *body
;
2316 GLboolean isConst
, constTrue
;
2318 /* type-check expression */
2319 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2320 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2324 /* Check if loop condition is a constant */
2325 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2327 if (isConst
&& !constTrue
) {
2328 /* loop is never executed! */
2329 return new_node0(IR_NOP
);
2332 loop
= new_loop(NULL
);
2334 /* save old, push new loop */
2335 prevLoop
= A
->CurLoop
;
2338 if (isConst
&& constTrue
) {
2339 /* while(nonzero constant), no conditional break */
2344 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2345 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2347 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2348 loop
->Children
[0] = new_seq(breakIf
, body
);
2350 /* Do infinite loop detection */
2351 /* loop->List is head of linked list of break/continue nodes */
2352 if (!loop
->List
&& isConst
&& constTrue
) {
2353 /* infinite loop detected */
2354 A
->CurLoop
= prevLoop
; /* clean-up */
2355 slang_info_log_error(A
->log
, "Infinite loop detected!");
2359 /* pop loop, restore prev */
2360 A
->CurLoop
= prevLoop
;
2367 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2369 static slang_ir_node
*
2370 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2374 * body code (child[0])
2376 * BREAK if !expr (child[1])
2378 slang_ir_node
*prevLoop
, *loop
;
2379 GLboolean isConst
, constTrue
;
2381 /* type-check expression */
2382 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2383 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2387 loop
= new_loop(NULL
);
2389 /* save old, push new loop */
2390 prevLoop
= A
->CurLoop
;
2394 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
2396 /* Check if loop condition is a constant */
2397 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
2398 if (isConst
&& constTrue
) {
2399 /* do { } while(1) ==> no conditional break */
2400 loop
->Children
[1] = NULL
; /* no tail code */
2404 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2405 loop
->Children
[1] = new_break_if_true(A
->CurLoop
, cond
);
2408 /* XXX we should do infinite loop detection, as above */
2410 /* pop loop, restore prev */
2411 A
->CurLoop
= prevLoop
;
2418 * Generate for-loop using high-level IR_LOOP instruction.
2420 static slang_ir_node
*
2421 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2424 * init code (child[0])
2426 * BREAK if !expr (child[1])
2427 * body code (child[3])
2429 * incr code (child[2]) // XXX continue here
2431 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
2433 init
= _slang_gen_operation(A
, &oper
->children
[0]);
2434 loop
= new_loop(NULL
);
2436 /* save old, push new loop */
2437 prevLoop
= A
->CurLoop
;
2440 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2441 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2442 body
= _slang_gen_operation(A
, &oper
->children
[3]);
2443 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
2445 loop
->Children
[0] = new_seq(breakIf
, body
);
2446 loop
->Children
[1] = incr
; /* tail code */
2448 /* pop loop, restore prev */
2449 A
->CurLoop
= prevLoop
;
2451 return new_seq(init
, loop
);
2455 static slang_ir_node
*
2456 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2458 slang_ir_node
*n
, *loopNode
;
2459 assert(oper
->type
== SLANG_OPER_CONTINUE
);
2460 loopNode
= A
->CurLoop
;
2462 assert(loopNode
->Opcode
== IR_LOOP
);
2463 n
= new_node0(IR_CONT
);
2465 n
->Parent
= loopNode
;
2466 /* insert this node at head of linked list */
2467 n
->List
= loopNode
->List
;
2475 * Determine if the given operation is of a specific type.
2478 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
2480 if (oper
->type
== type
)
2482 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
2483 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
2484 oper
->num_children
== 1)
2485 return is_operation_type(&oper
->children
[0], type
);
2492 * Generate IR tree for an if/then/else conditional using high-level
2493 * IR_IF instruction.
2495 static slang_ir_node
*
2496 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2499 * eval expr (child[0])
2506 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
2507 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
2508 GLboolean isConst
, constTrue
;
2510 /* type-check expression */
2511 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2512 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
2516 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2517 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
2521 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2525 return _slang_gen_operation(A
, &oper
->children
[1]);
2528 /* if (false) ... */
2529 return _slang_gen_operation(A
, &oper
->children
[2]);
2533 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2534 cond
= new_cond(cond
);
2536 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
2537 && !haveElseClause
) {
2538 /* Special case: generate a conditional break */
2539 ifBody
= new_break_if_true(A
->CurLoop
, cond
);
2542 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
2543 && !haveElseClause
) {
2544 /* Special case: generate a conditional break */
2545 ifBody
= new_cont_if_true(A
->CurLoop
, cond
);
2550 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
2552 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
2555 ifNode
= new_if(cond
, ifBody
, elseBody
);
2562 static slang_ir_node
*
2563 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2567 assert(oper
->type
== SLANG_OPER_NOT
);
2569 /* type-check expression */
2570 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2571 slang_info_log_error(A
->log
,
2572 "scalar/boolean expression expected for '!'");
2576 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2584 static slang_ir_node
*
2585 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2587 slang_ir_node
*n1
, *n2
;
2589 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
2591 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
2592 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2593 slang_info_log_error(A
->log
,
2594 "scalar/boolean expressions expected for '^^'");
2598 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
2601 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
2604 return new_node2(IR_NOTEQUAL
, n1
, n2
);
2609 * Generate IR node for storage of a temporary of given size.
2611 static slang_ir_node
*
2612 _slang_gen_temporary(GLint size
)
2614 slang_ir_storage
*store
;
2615 slang_ir_node
*n
= NULL
;
2617 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
2619 n
= new_node0(IR_VAR_DECL
);
2632 * Generate program constants for an array.
2633 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
2634 * This will allocate and initialize three vector constants, storing
2635 * the array in constant memory, not temporaries like a non-const array.
2636 * This can also be used for uniform array initializers.
2637 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
2640 make_constant_array(slang_assemble_ctx
*A
,
2641 slang_variable
*var
,
2642 slang_operation
*initializer
)
2644 struct gl_program
*prog
= A
->program
;
2645 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
2646 const char *varName
= (char *) var
->a_name
;
2647 const GLuint numElements
= initializer
->num_children
;
2653 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
2655 size
= var
->store
->Size
;
2657 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
2658 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
2659 assert(initializer
->type
== SLANG_OPER_CALL
);
2660 assert(initializer
->array_constructor
);
2662 values
= (GLfloat
*) _mesa_malloc(numElements
* 4 * sizeof(GLfloat
));
2664 /* convert constructor params into ordinary floats */
2665 for (i
= 0; i
< numElements
; i
++) {
2666 const slang_operation
*op
= &initializer
->children
[i
];
2667 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
2668 /* unsupported type for this optimization */
2672 for (j
= 0; j
< op
->literal_size
; j
++) {
2673 values
[i
* 4 + j
] = op
->literal
[j
];
2675 for ( ; j
< 4; j
++) {
2676 values
[i
* 4 + j
] = 0.0f
;
2680 /* slightly different paths for constants vs. uniforms */
2681 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2682 var
->store
->File
= PROGRAM_UNIFORM
;
2683 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
2684 size
, datatype
, values
);
2687 var
->store
->File
= PROGRAM_CONSTANT
;
2688 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
2691 assert(var
->store
->Size
== size
);
2701 * Generate IR node for allocating/declaring a variable (either a local or
2703 * Generally, this involves allocating an slang_ir_storage instance for the
2704 * variable, choosing a register file (temporary, constant, etc).
2705 * For ordinary variables we do not yet allocate storage though. We do that
2706 * when we find the first actual use of the variable to avoid allocating temp
2707 * regs that will never get used.
2708 * At this time, uniforms are always allocated space in this function.
2710 * \param initializer Optional initializer expression for the variable.
2712 static slang_ir_node
*
2713 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
2714 slang_operation
*initializer
)
2716 const char *varName
= (const char *) var
->a_name
;
2717 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
2718 slang_ir_node
*varDecl
, *n
;
2719 slang_ir_storage
*store
;
2720 GLint size
, totalSize
; /* if array then totalSize > size */
2721 enum register_file file
;
2723 /*assert(!var->declared);*/
2724 var
->declared
= GL_TRUE
;
2726 /* determine GPU register file for simple cases */
2727 if (is_sampler_type(&var
->type
)) {
2728 file
= PROGRAM_SAMPLER
;
2730 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2731 file
= PROGRAM_UNIFORM
;
2734 file
= PROGRAM_TEMPORARY
;
2737 size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
2739 slang_info_log_error(A
->log
, "invalid declaration for '%s'", varName
);
2744 if (var
->type
.array_len
> 0) {
2745 /* the type is an array, ex: float[4] x; */
2746 GLint sz
= (totalSize
+ 3) & ~3;
2747 /* total size = element size * array length */
2748 sz
*= var
->type
.array_len
;
2752 if (var
->array_len
> 0) {
2753 /* this is an array, ex: float x[4]; */
2754 /* round up the element size to a multiple of 4 */
2755 GLint sz
= (totalSize
+ 3) & ~3;
2756 /* total size = element size * array length */
2757 sz
*= var
->array_len
;
2761 /* Allocate IR node for the declaration */
2762 varDecl
= new_node0(IR_VAR_DECL
);
2766 _slang_attach_storage(varDecl
, var
); /* undefined storage at first */
2768 assert(varDecl
->Store
== var
->store
);
2769 assert(varDecl
->Store
);
2770 assert(varDecl
->Store
->Index
< 0);
2773 assert(store
== varDecl
->Store
);
2776 /* Fill in storage fields which we now know. store->Index/Swizzle may be
2777 * set for some cases below. Otherwise, store->Index/Swizzle will be set
2781 store
->Size
= totalSize
;
2783 /* if there's an initializer, generate IR for the expression */
2785 slang_ir_node
*varRef
, *init
;
2787 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
2788 /* if the variable is const, the initializer must be a const
2789 * expression as well.
2792 if (!_slang_is_constant_expr(initializer
)) {
2793 slang_info_log_error(A
->log
,
2794 "initializer for %s not constant", varName
);
2800 /* IR for the variable we're initializing */
2801 varRef
= new_var(A
, var
);
2803 slang_info_log_error(A
->log
, "out of memory");
2807 /* constant-folding, etc here */
2808 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
2810 /* look for simple constant-valued variables and uniforms */
2811 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
2812 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2814 if (initializer
->type
== SLANG_OPER_CALL
&&
2815 initializer
->array_constructor
) {
2816 /* array initializer */
2817 if (make_constant_array(A
, var
, initializer
))
2820 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
2821 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
2822 /* simple float/vector initializer */
2823 if (store
->File
== PROGRAM_UNIFORM
) {
2824 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
2826 totalSize
, datatype
,
2827 initializer
->literal
);
2828 store
->Swizzle
= _slang_var_swizzle(size
, 0);
2833 store
->File
= PROGRAM_CONSTANT
;
2834 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
2836 initializer
->literal
,
2838 store
->Swizzle
= _slang_var_swizzle(size
, 0);
2845 /* IR for initializer */
2846 init
= _slang_gen_operation(A
, initializer
);
2850 /* XXX remove this when type checking is added above */
2851 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
2852 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
2856 /* assign RHS to LHS */
2857 n
= new_node2(IR_COPY
, varRef
, init
);
2858 n
= new_seq(varDecl
, n
);
2861 /* no initializer */
2865 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
2866 /* always need to allocate storage for uniforms at this point */
2867 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
2868 totalSize
, datatype
, NULL
);
2869 store
->Swizzle
= _slang_var_swizzle(size
, 0);
2873 printf("%s var %p %s store=%p index=%d size=%d\n",
2874 __FUNCTION__
, (void *) var
, (char *) varName
,
2875 (void *) store
, store
->Index
, store
->Size
);
2883 * Generate code for a selection expression: b ? x : y
2884 * XXX In some cases we could implement a selection expression
2885 * with an LRP instruction (use the boolean as the interpolant).
2886 * Otherwise, we use an IF/ELSE/ENDIF construct.
2888 static slang_ir_node
*
2889 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
2891 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
2892 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
2893 slang_typeinfo type0
, type1
, type2
;
2894 int size
, isBool
, isEqual
;
2896 assert(oper
->type
== SLANG_OPER_SELECT
);
2897 assert(oper
->num_children
== 3);
2899 /* type of children[0] must be boolean */
2900 slang_typeinfo_construct(&type0
);
2901 typeof_operation(A
, &oper
->children
[0], &type0
);
2902 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
2903 slang_typeinfo_destruct(&type0
);
2905 slang_info_log_error(A
->log
, "selector type is not boolean");
2909 slang_typeinfo_construct(&type1
);
2910 slang_typeinfo_construct(&type2
);
2911 typeof_operation(A
, &oper
->children
[1], &type1
);
2912 typeof_operation(A
, &oper
->children
[2], &type2
);
2913 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
2914 slang_typeinfo_destruct(&type1
);
2915 slang_typeinfo_destruct(&type2
);
2917 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
2921 /* size of x or y's type */
2922 size
= _slang_sizeof_type_specifier(&type1
.spec
);
2926 tmpDecl
= _slang_gen_temporary(size
);
2928 /* the condition (child 0) */
2929 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2930 cond
= new_cond(cond
);
2932 /* if-true body (child 1) */
2933 tmpVar
= new_node0(IR_VAR
);
2934 tmpVar
->Store
= tmpDecl
->Store
;
2935 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
2936 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
2938 /* if-false body (child 2) */
2939 tmpVar
= new_node0(IR_VAR
);
2940 tmpVar
->Store
= tmpDecl
->Store
;
2941 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
2942 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
2944 ifNode
= new_if(cond
, trueNode
, falseNode
);
2947 tmpVar
= new_node0(IR_VAR
);
2948 tmpVar
->Store
= tmpDecl
->Store
;
2950 tree
= new_seq(ifNode
, tmpVar
);
2951 tree
= new_seq(tmpDecl
, tree
);
2953 /*_slang_print_ir_tree(tree, 10);*/
2959 * Generate code for &&.
2961 static slang_ir_node
*
2962 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
2964 /* rewrite "a && b" as "a ? b : false" */
2965 slang_operation
*select
;
2968 select
= slang_operation_new(1);
2969 select
->type
= SLANG_OPER_SELECT
;
2970 select
->num_children
= 3;
2971 select
->children
= slang_operation_new(3);
2973 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
2974 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
2975 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
2976 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
2977 select
->children
[2].literal_size
= 1;
2979 n
= _slang_gen_select(A
, select
);
2985 * Generate code for ||.
2987 static slang_ir_node
*
2988 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
2990 /* rewrite "a || b" as "a ? true : b" */
2991 slang_operation
*select
;
2994 select
= slang_operation_new(1);
2995 select
->type
= SLANG_OPER_SELECT
;
2996 select
->num_children
= 3;
2997 select
->children
= slang_operation_new(3);
2999 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3000 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
3001 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
3002 select
->children
[1].literal_size
= 1;
3003 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
3005 n
= _slang_gen_select(A
, select
);
3011 * Generate IR tree for a return statement.
3013 static slang_ir_node
*
3014 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
3016 const GLboolean haveReturnValue
3017 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
3019 /* error checking */
3020 assert(A
->CurFunction
);
3021 if (haveReturnValue
&&
3022 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
3023 slang_info_log_error(A
->log
, "illegal return expression");
3026 else if (!haveReturnValue
&&
3027 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
3028 slang_info_log_error(A
->log
, "return statement requires an expression");
3032 if (!haveReturnValue
) {
3033 return new_return(A
->curFuncEndLabel
);
3041 * return; // goto __endOfFunction
3043 slang_operation
*assign
;
3044 slang_atom a_retVal
;
3047 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
3053 _slang_variable_locate(oper
->locals
, a_retVal
, GL_TRUE
);
3055 /* trying to return a value in a void-valued function */
3061 assign
= slang_operation_new(1);
3062 assign
->type
= SLANG_OPER_ASSIGN
;
3063 assign
->num_children
= 2;
3064 assign
->children
= slang_operation_new(2);
3065 /* lhs (__retVal) */
3066 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
3067 assign
->children
[0].a_id
= a_retVal
;
3068 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
3070 /* XXX we might be able to avoid this copy someday */
3071 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
3073 /* assemble the new code */
3074 n
= new_seq(_slang_gen_operation(A
, assign
),
3075 new_return(A
->curFuncEndLabel
));
3077 slang_operation_delete(assign
);
3084 * Determine if the given operation/expression is const-valued.
3087 _slang_is_constant_expr(const slang_operation
*oper
)
3089 slang_variable
*var
;
3092 switch (oper
->type
) {
3093 case SLANG_OPER_IDENTIFIER
:
3094 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3095 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
3099 for (i
= 0; i
< oper
->num_children
; i
++) {
3100 if (!_slang_is_constant_expr(&oper
->children
[i
]))
3109 * Check if an assignment of type t1 to t0 is legal.
3110 * XXX more cases needed.
3113 _slang_assignment_compatible(slang_assemble_ctx
*A
,
3114 slang_operation
*op0
,
3115 slang_operation
*op1
)
3117 slang_typeinfo t0
, t1
;
3120 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
3121 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
3125 slang_typeinfo_construct(&t0
);
3126 typeof_operation(A
, op0
, &t0
);
3128 slang_typeinfo_construct(&t1
);
3129 typeof_operation(A
, op1
, &t1
);
3131 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
3132 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
3136 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
3141 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
3142 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
3143 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
3146 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
3147 t1
.spec
.type
== SLANG_SPEC_BOOL
)
3150 #if 0 /* not used just yet - causes problems elsewhere */
3151 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
3152 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3156 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3157 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3160 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3161 t1
.spec
.type
== SLANG_SPEC_INT
)
3169 * Generate IR tree for a local variable declaration.
3170 * Basically do some error checking and call _slang_gen_var_decl().
3172 static slang_ir_node
*
3173 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
3175 const char *varName
= (char *) oper
->a_id
;
3176 slang_variable
*var
;
3177 slang_ir_node
*varDecl
;
3178 slang_operation
*initializer
;
3180 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
3181 assert(oper
->num_children
<= 1);
3183 /* lookup the variable by name */
3184 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3186 return NULL
; /* "shouldn't happen" */
3188 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3189 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
3190 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3191 /* can't declare attribute/uniform vars inside functions */
3192 slang_info_log_error(A
->log
,
3193 "local variable '%s' cannot be an attribute/uniform/varying",
3200 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
3205 /* check if the var has an initializer */
3206 if (oper
->num_children
> 0) {
3207 assert(oper
->num_children
== 1);
3208 initializer
= &oper
->children
[0];
3210 else if (var
->initializer
) {
3211 initializer
= var
->initializer
;
3218 /* check/compare var type and initializer type */
3219 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
3220 slang_info_log_error(A
->log
, "incompatible types in assignment");
3225 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3226 slang_info_log_error(A
->log
,
3227 "const-qualified variable '%s' requires initializer",
3233 /* Generate IR node */
3234 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
3243 * Generate IR tree for a reference to a variable (such as in an expression).
3244 * This is different from a variable declaration.
3246 static slang_ir_node
*
3247 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
3249 /* If there's a variable associated with this oper (from inlining)
3250 * use it. Otherwise, use the oper's var id.
3252 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
3253 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
3256 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
3259 assert(var
->declared
);
3260 n
= new_var(A
, var
);
3267 * Return the number of components actually named by the swizzle.
3268 * Recall that swizzles may have undefined/don't-care values.
3271 swizzle_size(GLuint swizzle
)
3274 for (i
= 0; i
< 4; i
++) {
3275 GLuint swz
= GET_SWZ(swizzle
, i
);
3276 size
+= (swz
>= 0 && swz
<= 3);
3282 static slang_ir_node
*
3283 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
3285 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
3289 n
->Store
= _slang_new_ir_storage_relative(0,
3290 swizzle_size(swizzle
),
3292 n
->Store
->Swizzle
= swizzle
;
3299 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
3301 while (store
->Parent
)
3302 store
= store
->Parent
;
3304 if (!(store
->File
== PROGRAM_OUTPUT
||
3305 store
->File
== PROGRAM_TEMPORARY
||
3306 (store
->File
== PROGRAM_VARYING
&&
3307 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
3317 * Generate IR tree for an assignment (=).
3319 static slang_ir_node
*
3320 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
3322 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
3323 /* Check that var is writeable */
3325 = _slang_variable_locate(oper
->children
[0].locals
,
3326 oper
->children
[0].a_id
, GL_TRUE
);
3328 slang_info_log_error(A
->log
, "undefined variable '%s'",
3329 (char *) oper
->children
[0].a_id
);
3332 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3333 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3334 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
3335 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
3336 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
3337 slang_info_log_error(A
->log
,
3338 "illegal assignment to read-only variable '%s'",
3339 (char *) oper
->children
[0].a_id
);
3344 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
3345 oper
->children
[1].type
== SLANG_OPER_CALL
) {
3346 /* Special case of: x = f(a, b)
3347 * Replace with f(a, b, x) (where x == hidden __retVal out param)
3349 * XXX this could be even more effective if we could accomodate
3350 * cases such as "v.x = f();" - would help with typical vertex
3354 n
= _slang_gen_function_call_name(A
,
3355 (const char *) oper
->children
[1].a_id
,
3356 &oper
->children
[1], &oper
->children
[0]);
3360 slang_ir_node
*n
, *lhs
, *rhs
;
3362 /* lhs and rhs type checking */
3363 if (!_slang_assignment_compatible(A
,
3365 &oper
->children
[1])) {
3366 slang_info_log_error(A
->log
, "incompatible types in assignment");
3370 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
3376 slang_info_log_error(A
->log
,
3377 "invalid left hand side for assignment");
3381 /* check that lhs is writable */
3382 if (!is_store_writable(A
, lhs
->Store
)) {
3383 slang_info_log_error(A
->log
,
3384 "illegal assignment to read-only l-value");
3388 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
3390 /* convert lhs swizzle into writemask */
3391 GLuint writemask
, newSwizzle
;
3392 if (!swizzle_to_writemask(A
, lhs
->Store
->Swizzle
,
3393 &writemask
, &newSwizzle
)) {
3394 /* Non-simple writemask, need to swizzle right hand side in
3395 * order to put components into the right place.
3397 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
3399 n
= new_node2(IR_COPY
, lhs
, rhs
);
3410 * Generate IR tree for referencing a field in a struct (or basic vector type)
3412 static slang_ir_node
*
3413 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
3417 /* type of struct */
3418 slang_typeinfo_construct(&ti
);
3419 typeof_operation(A
, &oper
->children
[0], &ti
);
3421 if (_slang_type_is_vector(ti
.spec
.type
)) {
3422 /* the field should be a swizzle */
3423 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
3427 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3428 slang_info_log_error(A
->log
, "Bad swizzle");
3431 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3436 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3437 /* create new parent node with swizzle */
3439 n
= _slang_gen_swizzle(n
, swizzle
);
3442 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
3443 || ti
.spec
.type
== SLANG_SPEC_INT
3444 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
3445 const GLuint rows
= 1;
3449 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3450 slang_info_log_error(A
->log
, "Bad swizzle");
3452 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3456 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3457 /* create new parent node with swizzle */
3458 n
= _slang_gen_swizzle(n
, swizzle
);
3462 /* the field is a structure member (base.field) */
3463 /* oper->children[0] is the base */
3464 /* oper->a_id is the field name */
3465 slang_ir_node
*base
, *n
;
3466 slang_typeinfo field_ti
;
3467 GLint fieldSize
, fieldOffset
= -1;
3470 slang_typeinfo_construct(&field_ti
);
3471 typeof_operation(A
, oper
, &field_ti
);
3473 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
3475 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
3477 if (fieldSize
== 0 || fieldOffset
< 0) {
3478 const char *structName
;
3479 if (ti
.spec
._struct
)
3480 structName
= (char *) ti
.spec
._struct
->a_name
;
3482 structName
= "unknown";
3483 slang_info_log_error(A
->log
,
3484 "\"%s\" is not a member of struct \"%s\"",
3485 (char *) oper
->a_id
, structName
);
3488 assert(fieldSize
>= 0);
3490 base
= _slang_gen_operation(A
, &oper
->children
[0]);
3492 /* error msg should have already been logged */
3496 n
= new_node1(IR_FIELD
, base
);
3500 n
->Field
= (char *) oper
->a_id
;
3502 /* Store the field's offset in storage->Index */
3503 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
3513 * Gen code for array indexing.
3515 static slang_ir_node
*
3516 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
3518 slang_typeinfo array_ti
;
3520 /* get array's type info */
3521 slang_typeinfo_construct(&array_ti
);
3522 typeof_operation(A
, &oper
->children
[0], &array_ti
);
3524 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
3525 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
3526 /* translate the index into a swizzle/writemask: "v.x=p" */
3527 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
3531 index
= (GLint
) oper
->children
[1].literal
[0];
3532 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
3533 index
>= (GLint
) max
) {
3534 slang_info_log_error(A
->log
, "Invalid array index for vector type");
3538 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3540 /* use swizzle to access the element */
3541 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
3545 n
= _slang_gen_swizzle(n
, swizzle
);
3551 /* conventional array */
3552 slang_typeinfo elem_ti
;
3553 slang_ir_node
*elem
, *array
, *index
;
3554 GLint elemSize
, arrayLen
;
3556 /* size of array element */
3557 slang_typeinfo_construct(&elem_ti
);
3558 typeof_operation(A
, oper
, &elem_ti
);
3559 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
3561 if (_slang_type_is_matrix(array_ti
.spec
.type
))
3562 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
3564 arrayLen
= array_ti
.array_len
;
3566 slang_typeinfo_destruct(&array_ti
);
3567 slang_typeinfo_destruct(&elem_ti
);
3569 if (elemSize
<= 0) {
3570 /* unknown var or type */
3571 slang_info_log_error(A
->log
, "Undefined variable or type");
3575 array
= _slang_gen_operation(A
, &oper
->children
[0]);
3576 index
= _slang_gen_operation(A
, &oper
->children
[1]);
3577 if (array
&& index
) {
3579 GLint constIndex
= -1;
3580 if (index
->Opcode
== IR_FLOAT
) {
3581 constIndex
= (int) index
->Value
[0];
3582 if (constIndex
< 0 || constIndex
>= arrayLen
) {
3583 slang_info_log_error(A
->log
,
3584 "Array index out of bounds (index=%d size=%d)",
3585 constIndex
, arrayLen
);
3586 _slang_free_ir_tree(array
);
3587 _slang_free_ir_tree(index
);
3592 if (!array
->Store
) {
3593 slang_info_log_error(A
->log
, "Invalid array");
3597 elem
= new_node2(IR_ELEMENT
, array
, index
);
3599 /* The storage info here will be updated during code emit */
3600 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
3601 array
->Store
->Index
,
3607 _slang_free_ir_tree(array
);
3608 _slang_free_ir_tree(index
);
3615 static slang_ir_node
*
3616 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
3617 slang_ir_opcode opcode
)
3619 slang_typeinfo t0
, t1
;
3622 slang_typeinfo_construct(&t0
);
3623 typeof_operation(A
, &oper
->children
[0], &t0
);
3625 slang_typeinfo_construct(&t1
);
3626 typeof_operation(A
, &oper
->children
[0], &t1
);
3628 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
3629 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
3630 slang_info_log_error(A
->log
, "Illegal array comparison");
3634 if (oper
->type
!= SLANG_OPER_EQUAL
&&
3635 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
3636 /* <, <=, >, >= can only be used with scalars */
3637 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
3638 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
3639 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
3640 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
3641 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
3646 n
= new_node2(opcode
,
3647 _slang_gen_operation(A
, &oper
->children
[0]),
3648 _slang_gen_operation(A
, &oper
->children
[1]));
3650 /* result is a bool (size 1) */
3651 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
3659 print_vars(slang_variable_scope
*s
)
3663 for (i
= 0; i
< s
->num_variables
; i
++) {
3665 (char*) s
->variables
[i
]->a_name
,
3666 s
->variables
[i
]->declared
);
3676 _slang_undeclare_vars(slang_variable_scope
*locals
)
3678 if (locals
->num_variables
> 0) {
3680 for (i
= 0; i
< locals
->num_variables
; i
++) {
3681 slang_variable
*v
= locals
->variables
[i
];
3682 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
3683 v
->declared
= GL_FALSE
;
3691 * Generate IR tree for a slang_operation (AST node)
3693 static slang_ir_node
*
3694 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
3696 switch (oper
->type
) {
3697 case SLANG_OPER_BLOCK_NEW_SCOPE
:
3701 _slang_push_var_table(A
->vartable
);
3703 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
3704 n
= _slang_gen_operation(A
, oper
);
3705 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
3707 _slang_pop_var_table(A
->vartable
);
3709 /*_slang_undeclare_vars(oper->locals);*/
3710 /*print_vars(oper->locals);*/
3713 n
= new_node1(IR_SCOPE
, n
);
3718 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
3719 /* list of operations */
3720 if (oper
->num_children
> 0)
3722 slang_ir_node
*n
, *tree
= NULL
;
3725 for (i
= 0; i
< oper
->num_children
; i
++) {
3726 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
3728 _slang_free_ir_tree(tree
);
3729 return NULL
; /* error must have occured */
3731 tree
= new_seq(tree
, n
);
3737 return new_node0(IR_NOP
);
3740 case SLANG_OPER_EXPRESSION
:
3741 return _slang_gen_operation(A
, &oper
->children
[0]);
3743 case SLANG_OPER_FOR
:
3744 return _slang_gen_for(A
, oper
);
3746 return _slang_gen_do(A
, oper
);
3747 case SLANG_OPER_WHILE
:
3748 return _slang_gen_while(A
, oper
);
3749 case SLANG_OPER_BREAK
:
3751 slang_info_log_error(A
->log
, "'break' not in loop");
3754 return new_break(A
->CurLoop
);
3755 case SLANG_OPER_CONTINUE
:
3757 slang_info_log_error(A
->log
, "'continue' not in loop");
3760 return _slang_gen_continue(A
, oper
);
3761 case SLANG_OPER_DISCARD
:
3762 return new_node0(IR_KILL
);
3764 case SLANG_OPER_EQUAL
:
3765 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
3766 case SLANG_OPER_NOTEQUAL
:
3767 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
3768 case SLANG_OPER_GREATER
:
3769 return _slang_gen_compare(A
, oper
, IR_SGT
);
3770 case SLANG_OPER_LESS
:
3771 return _slang_gen_compare(A
, oper
, IR_SLT
);
3772 case SLANG_OPER_GREATEREQUAL
:
3773 return _slang_gen_compare(A
, oper
, IR_SGE
);
3774 case SLANG_OPER_LESSEQUAL
:
3775 return _slang_gen_compare(A
, oper
, IR_SLE
);
3776 case SLANG_OPER_ADD
:
3779 assert(oper
->num_children
== 2);
3780 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
3783 case SLANG_OPER_SUBTRACT
:
3786 assert(oper
->num_children
== 2);
3787 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
3790 case SLANG_OPER_MULTIPLY
:
3793 assert(oper
->num_children
== 2);
3794 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
3797 case SLANG_OPER_DIVIDE
:
3800 assert(oper
->num_children
== 2);
3801 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
3804 case SLANG_OPER_MINUS
:
3807 assert(oper
->num_children
== 1);
3808 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
3811 case SLANG_OPER_PLUS
:
3812 /* +expr --> do nothing */
3813 return _slang_gen_operation(A
, &oper
->children
[0]);
3814 case SLANG_OPER_VARIABLE_DECL
:
3815 return _slang_gen_declaration(A
, oper
);
3816 case SLANG_OPER_ASSIGN
:
3817 return _slang_gen_assignment(A
, oper
);
3818 case SLANG_OPER_ADDASSIGN
:
3821 assert(oper
->num_children
== 2);
3822 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
3825 case SLANG_OPER_SUBASSIGN
:
3828 assert(oper
->num_children
== 2);
3829 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
3833 case SLANG_OPER_MULASSIGN
:
3836 assert(oper
->num_children
== 2);
3837 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
3840 case SLANG_OPER_DIVASSIGN
:
3843 assert(oper
->num_children
== 2);
3844 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
3847 case SLANG_OPER_LOGICALAND
:
3850 assert(oper
->num_children
== 2);
3851 n
= _slang_gen_logical_and(A
, oper
);
3854 case SLANG_OPER_LOGICALOR
:
3857 assert(oper
->num_children
== 2);
3858 n
= _slang_gen_logical_or(A
, oper
);
3861 case SLANG_OPER_LOGICALXOR
:
3862 return _slang_gen_xor(A
, oper
);
3863 case SLANG_OPER_NOT
:
3864 return _slang_gen_not(A
, oper
);
3865 case SLANG_OPER_SELECT
: /* b ? x : y */
3868 assert(oper
->num_children
== 3);
3869 n
= _slang_gen_select(A
, oper
);
3873 case SLANG_OPER_ASM
:
3874 return _slang_gen_asm(A
, oper
, NULL
);
3875 case SLANG_OPER_CALL
:
3876 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
3878 case SLANG_OPER_METHOD
:
3879 return _slang_gen_method_call(A
, oper
);
3880 case SLANG_OPER_RETURN
:
3881 return _slang_gen_return(A
, oper
);
3882 case SLANG_OPER_LABEL
:
3883 return new_label(oper
->label
);
3884 case SLANG_OPER_IDENTIFIER
:
3885 return _slang_gen_variable(A
, oper
);
3887 return _slang_gen_if(A
, oper
);
3888 case SLANG_OPER_FIELD
:
3889 return _slang_gen_struct_field(A
, oper
);
3890 case SLANG_OPER_SUBSCRIPT
:
3891 return _slang_gen_array_element(A
, oper
);
3892 case SLANG_OPER_LITERAL_FLOAT
:
3894 case SLANG_OPER_LITERAL_INT
:
3896 case SLANG_OPER_LITERAL_BOOL
:
3897 return new_float_literal(oper
->literal
, oper
->literal_size
);
3899 case SLANG_OPER_POSTINCREMENT
: /* var++ */
3902 assert(oper
->num_children
== 1);
3903 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
3906 case SLANG_OPER_POSTDECREMENT
: /* var-- */
3909 assert(oper
->num_children
== 1);
3910 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
3913 case SLANG_OPER_PREINCREMENT
: /* ++var */
3916 assert(oper
->num_children
== 1);
3917 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
3920 case SLANG_OPER_PREDECREMENT
: /* --var */
3923 assert(oper
->num_children
== 1);
3924 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
3928 case SLANG_OPER_NON_INLINED_CALL
:
3929 case SLANG_OPER_SEQUENCE
:
3931 slang_ir_node
*tree
= NULL
;
3933 for (i
= 0; i
< oper
->num_children
; i
++) {
3934 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
3935 tree
= new_seq(tree
, n
);
3937 tree
->Store
= n
->Store
;
3939 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
3940 tree
= new_function_call(tree
, oper
->label
);
3945 case SLANG_OPER_NONE
:
3946 case SLANG_OPER_VOID
:
3947 /* returning NULL here would generate an error */
3948 return new_node0(IR_NOP
);
3951 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
3953 return new_node0(IR_NOP
);
3961 * Compute total size of array give size of element, number of elements.
3964 array_size(GLint baseSize
, GLint arrayLen
)
3968 /* round up base type to multiple of 4 */
3969 total
= ((baseSize
+ 3) & ~0x3) * MAX2(arrayLen
, 1);
3979 * Called by compiler when a global variable has been parsed/compiled.
3980 * Here we examine the variable's type to determine what kind of register
3981 * storage will be used.
3983 * A uniform such as "gl_Position" will become the register specification
3984 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
3985 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
3987 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
3988 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
3989 * actual texture unit (as specified by the user calling glUniform1i()).
3992 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
3993 slang_unit_type type
)
3995 struct gl_program
*prog
= A
->program
;
3996 const char *varName
= (char *) var
->a_name
;
3997 GLboolean success
= GL_TRUE
;
3998 slang_ir_storage
*store
= NULL
;
4000 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
4001 const GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
4002 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
4004 if (texIndex
!= -1) {
4005 /* This is a texture sampler variable...
4006 * store->File = PROGRAM_SAMPLER
4007 * store->Index = sampler number (0..7, typically)
4008 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
4010 if (var
->initializer
) {
4011 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
4014 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
4015 /* disallow rect samplers */
4016 if (var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECT
||
4017 var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
) {
4018 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
4023 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
4024 store
= _slang_new_ir_storage(PROGRAM_SAMPLER
, sampNum
, texIndex
);
4026 if (dbg
) printf("SAMPLER ");
4028 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4029 /* Uniform variable */
4030 const GLint totalSize
= array_size(size
, var
->array_len
);
4031 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
4034 /* user-defined uniform */
4035 if (datatype
== GL_NONE
) {
4036 if (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
) {
4037 /* temporary work-around */
4038 GLenum datatype
= GL_FLOAT
;
4039 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
4040 totalSize
, datatype
, NULL
);
4041 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
4042 totalSize
, swizzle
);
4044 /* XXX what we need to do is unroll the struct into its
4045 * basic types, creating a uniform variable for each.
4053 * Should produce uniforms:
4054 * "f.a" (GL_FLOAT_VEC3)
4055 * "f.b" (GL_FLOAT_VEC4)
4058 if (var
->initializer
) {
4059 slang_info_log_error(A
->log
,
4060 "unsupported initializer for uniform '%s'", varName
);
4065 slang_info_log_error(A
->log
,
4066 "invalid datatype for uniform variable %s",
4072 /* non-struct uniform */
4073 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
4079 /* pre-defined uniform, like gl_ModelviewMatrix */
4080 /* We know it's a uniform, but don't allocate storage unless
4083 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
4084 totalSize
, swizzle
);
4086 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
4088 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
4089 const GLint totalSize
= array_size(size
, var
->array_len
);
4091 /* varyings must be float, vec or mat */
4092 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
4093 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
4094 slang_info_log_error(A
->log
,
4095 "varying '%s' must be float/vector/matrix",
4100 if (var
->initializer
) {
4101 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
4107 /* user-defined varying */
4113 if (var
->type
.centroid
== SLANG_CENTROID
)
4114 flags
|= PROG_PARAM_BIT_CENTROID
;
4115 if (var
->type
.variant
== SLANG_INVARIANT
)
4116 flags
|= PROG_PARAM_BIT_INVARIANT
;
4118 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
4120 swizzle
= _slang_var_swizzle(size
, 0);
4121 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
4122 totalSize
, swizzle
);
4125 /* pre-defined varying, like gl_Color or gl_TexCoord */
4126 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
4127 /* fragment program input */
4129 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4132 assert(index
< FRAG_ATTRIB_MAX
);
4133 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
4137 /* vertex program output */
4138 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4139 GLuint swizzle
= _slang_var_swizzle(size
, 0);
4141 assert(index
< VERT_RESULT_MAX
);
4142 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
4143 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
4146 if (dbg
) printf("V/F ");
4148 if (dbg
) printf("VARYING ");
4150 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
4153 /* attributes must be float, vec or mat */
4154 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
4155 slang_info_log_error(A
->log
,
4156 "attribute '%s' must be float/vector/matrix",
4162 /* user-defined vertex attribute */
4163 const GLint attr
= -1; /* unknown */
4164 swizzle
= _slang_var_swizzle(size
, 0);
4165 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
4166 size
, datatype
, attr
);
4168 index
= VERT_ATTRIB_GENERIC0
+ index
;
4171 /* pre-defined vertex attrib */
4172 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
4175 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4176 if (dbg
) printf("ATTRIB ");
4178 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
4179 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
4180 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4182 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4183 if (dbg
) printf("INPUT ");
4185 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
4186 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
4187 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4188 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
4191 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
4192 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
4193 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
4194 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
4196 if (dbg
) printf("OUTPUT ");
4198 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
4199 /* pre-defined global constant, like gl_MaxLights */
4200 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
4201 if (dbg
) printf("CONST ");
4204 /* ordinary variable (may be const) */
4207 /* IR node to declare the variable */
4208 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
4210 /* emit GPU instructions */
4211 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_FALSE
, A
->log
);
4213 _slang_free_ir_tree(n
);
4216 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
4217 store
? store
->Index
: -2);
4220 var
->store
= store
; /* save var's storage info */
4222 var
->declared
= GL_TRUE
;
4229 * Produce an IR tree from a function AST (fun->body).
4230 * Then call the code emitter to convert the IR tree into gl_program
4234 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
4237 GLboolean success
= GL_TRUE
;
4239 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
4240 /* we only really generate code for main, all other functions get
4241 * inlined or codegen'd upon an actual call.
4244 /* do some basic error checking though */
4245 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
4246 /* check that non-void functions actually return something */
4248 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
4250 slang_info_log_error(A
->log
,
4251 "function \"%s\" has no return statement",
4252 (char *) fun
->header
.a_name
);
4254 "function \"%s\" has no return statement\n",
4255 (char *) fun
->header
.a_name
);
4260 return GL_TRUE
; /* not an error */
4264 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
4265 slang_print_function(fun
, 1);
4268 /* should have been allocated earlier: */
4269 assert(A
->program
->Parameters
);
4270 assert(A
->program
->Varying
);
4271 assert(A
->vartable
);
4273 A
->CurFunction
= fun
;
4275 /* fold constant expressions, etc. */
4276 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
4279 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
4280 slang_print_function(fun
, 1);
4283 /* Create an end-of-function label */
4284 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
4286 /* push new vartable scope */
4287 _slang_push_var_table(A
->vartable
);
4289 /* Generate IR tree for the function body code */
4290 n
= _slang_gen_operation(A
, fun
->body
);
4292 n
= new_node1(IR_SCOPE
, n
);
4294 /* pop vartable, restore previous */
4295 _slang_pop_var_table(A
->vartable
);
4298 /* XXX record error */
4302 /* append an end-of-function-label to IR tree */
4303 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
4305 /*_slang_label_delete(A->curFuncEndLabel);*/
4306 A
->curFuncEndLabel
= NULL
;
4309 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
4310 slang_print_function(fun
, 1);
4313 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
4314 _slang_print_ir_tree(n
, 0);
4317 printf("************* End codegen function ************\n\n");
4320 /* Emit program instructions */
4321 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
, A
->log
);
4322 _slang_free_ir_tree(n
);
4324 /* free codegen context */
4326 _mesa_free(A->codegen);