2 * Mesa 3-D graphics library
5 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \file slang_codegen.c
27 * Generate IR tree from AST.
34 *** The new_() functions return a new instance of a simple IR node.
35 *** The gen_() functions generate larger IR trees from the simple nodes.
40 #include "main/imports.h"
41 #include "main/macros.h"
42 #include "main/mtypes.h"
43 #include "shader/program.h"
44 #include "shader/prog_instruction.h"
45 #include "shader/prog_parameter.h"
46 #include "shader/prog_print.h"
47 #include "shader/prog_statevars.h"
48 #include "slang_typeinfo.h"
49 #include "slang_codegen.h"
50 #include "slang_compile.h"
51 #include "slang_label.h"
52 #include "slang_mem.h"
53 #include "slang_simplify.h"
54 #include "slang_emit.h"
55 #include "slang_vartable.h"
57 #include "slang_print.h"
60 static slang_ir_node
*
61 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
);
65 * Retrieves type information about an operation.
66 * Returns GL_TRUE on success.
67 * Returns GL_FALSE otherwise.
70 typeof_operation(const struct slang_assemble_ctx_
*A
,
74 return _slang_typeof_operation(op
, &A
->space
, ti
, A
->atoms
, A
->log
);
79 is_sampler_type(const slang_fully_specified_type
*t
)
81 switch (t
->specifier
.type
) {
82 case SLANG_SPEC_SAMPLER1D
:
83 case SLANG_SPEC_SAMPLER2D
:
84 case SLANG_SPEC_SAMPLER3D
:
85 case SLANG_SPEC_SAMPLERCUBE
:
86 case SLANG_SPEC_SAMPLER1DSHADOW
:
87 case SLANG_SPEC_SAMPLER2DSHADOW
:
88 case SLANG_SPEC_SAMPLER2DRECT
:
89 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
98 * Return the offset (in floats or ints) of the named field within
99 * the given struct. Return -1 if field not found.
100 * If field is NULL, return the size of the struct instead.
103 _slang_field_offset(const slang_type_specifier
*spec
, slang_atom field
)
107 for (i
= 0; i
< spec
->_struct
->fields
->num_variables
; i
++) {
108 const slang_variable
*v
= spec
->_struct
->fields
->variables
[i
];
109 const GLuint sz
= _slang_sizeof_type_specifier(&v
->type
.specifier
);
111 /* types larger than 1 float are register (4-float) aligned */
112 offset
= (offset
+ 3) & ~3;
114 if (field
&& v
->a_name
== field
) {
120 return -1; /* field not found */
122 return offset
; /* struct size */
127 * Return the size (in floats) of the given type specifier.
128 * If the size is greater than 4, the size should be a multiple of 4
129 * so that the correct number of 4-float registers are allocated.
130 * For example, a mat3x2 is size 12 because we want to store the
131 * 3 columns in 3 float[4] registers.
134 _slang_sizeof_type_specifier(const slang_type_specifier
*spec
)
137 switch (spec
->type
) {
138 case SLANG_SPEC_VOID
:
141 case SLANG_SPEC_BOOL
:
144 case SLANG_SPEC_BVEC2
:
147 case SLANG_SPEC_BVEC3
:
150 case SLANG_SPEC_BVEC4
:
156 case SLANG_SPEC_IVEC2
:
159 case SLANG_SPEC_IVEC3
:
162 case SLANG_SPEC_IVEC4
:
165 case SLANG_SPEC_FLOAT
:
168 case SLANG_SPEC_VEC2
:
171 case SLANG_SPEC_VEC3
:
174 case SLANG_SPEC_VEC4
:
177 case SLANG_SPEC_MAT2
:
178 sz
= 2 * 4; /* 2 columns (regs) */
180 case SLANG_SPEC_MAT3
:
183 case SLANG_SPEC_MAT4
:
186 case SLANG_SPEC_MAT23
:
187 sz
= 2 * 4; /* 2 columns (regs) */
189 case SLANG_SPEC_MAT32
:
190 sz
= 3 * 4; /* 3 columns (regs) */
192 case SLANG_SPEC_MAT24
:
195 case SLANG_SPEC_MAT42
:
196 sz
= 4 * 4; /* 4 columns (regs) */
198 case SLANG_SPEC_MAT34
:
201 case SLANG_SPEC_MAT43
:
202 sz
= 4 * 4; /* 4 columns (regs) */
204 case SLANG_SPEC_SAMPLER1D
:
205 case SLANG_SPEC_SAMPLER2D
:
206 case SLANG_SPEC_SAMPLER3D
:
207 case SLANG_SPEC_SAMPLERCUBE
:
208 case SLANG_SPEC_SAMPLER1DSHADOW
:
209 case SLANG_SPEC_SAMPLER2DSHADOW
:
210 case SLANG_SPEC_SAMPLER2DRECT
:
211 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
212 sz
= 1; /* a sampler is basically just an integer index */
214 case SLANG_SPEC_STRUCT
:
215 sz
= _slang_field_offset(spec
, 0); /* special use */
217 sz
= (sz
+ 3) & ~0x3; /* round up to multiple of four */
220 case SLANG_SPEC_ARRAY
:
221 sz
= _slang_sizeof_type_specifier(spec
->_array
);
224 _mesa_problem(NULL
, "Unexpected type in _slang_sizeof_type_specifier()");
229 /* if size is > 4, it should be a multiple of four */
230 assert((sz
& 0x3) == 0);
237 * Establish the binding between a slang_ir_node and a slang_variable.
238 * Then, allocate/attach a slang_ir_storage object to the IR node if needed.
239 * The IR node must be a IR_VAR or IR_VAR_DECL node.
240 * \param n the IR node
241 * \param var the variable to associate with the IR node
244 _slang_attach_storage(slang_ir_node
*n
, slang_variable
*var
)
248 assert(n
->Opcode
== IR_VAR
|| n
->Opcode
== IR_VAR_DECL
);
249 assert(!n
->Var
|| n
->Var
== var
);
254 /* need to setup storage */
255 if (n
->Var
&& n
->Var
->store
) {
256 /* node storage info = var storage info */
257 n
->Store
= n
->Var
->store
;
260 /* alloc new storage info */
261 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -7, -5);
263 printf("%s var=%s Store=%p Size=%d\n", __FUNCTION__
,
265 (void*) n
->Store
, n
->Store
->Size
);
268 n
->Var
->store
= n
->Store
;
269 assert(n
->Var
->store
);
276 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
277 * or -1 if the type is not a sampler.
280 sampler_to_texture_index(const slang_type_specifier_type type
)
283 case SLANG_SPEC_SAMPLER1D
:
284 return TEXTURE_1D_INDEX
;
285 case SLANG_SPEC_SAMPLER2D
:
286 return TEXTURE_2D_INDEX
;
287 case SLANG_SPEC_SAMPLER3D
:
288 return TEXTURE_3D_INDEX
;
289 case SLANG_SPEC_SAMPLERCUBE
:
290 return TEXTURE_CUBE_INDEX
;
291 case SLANG_SPEC_SAMPLER1DSHADOW
:
292 return TEXTURE_1D_INDEX
; /* XXX fix */
293 case SLANG_SPEC_SAMPLER2DSHADOW
:
294 return TEXTURE_2D_INDEX
; /* XXX fix */
295 case SLANG_SPEC_SAMPLER2DRECT
:
296 return TEXTURE_RECT_INDEX
;
297 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
298 return TEXTURE_RECT_INDEX
; /* XXX fix */
305 #define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
308 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
309 * a vertex or fragment program input variable. Return -1 if the input
311 * XXX return size too
314 _slang_input_index(const char *name
, GLenum target
, GLuint
*swizzleOut
)
321 static const struct input_info vertInputs
[] = {
322 { "gl_Vertex", VERT_ATTRIB_POS
, SWIZZLE_NOOP
},
323 { "gl_Normal", VERT_ATTRIB_NORMAL
, SWIZZLE_NOOP
},
324 { "gl_Color", VERT_ATTRIB_COLOR0
, SWIZZLE_NOOP
},
325 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
, SWIZZLE_NOOP
},
326 { "gl_FogCoord", VERT_ATTRIB_FOG
, SWIZZLE_XXXX
},
327 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
, SWIZZLE_NOOP
},
328 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
, SWIZZLE_NOOP
},
329 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
, SWIZZLE_NOOP
},
330 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
, SWIZZLE_NOOP
},
331 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
, SWIZZLE_NOOP
},
332 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
, SWIZZLE_NOOP
},
333 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
, SWIZZLE_NOOP
},
334 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
, SWIZZLE_NOOP
},
335 { NULL
, 0, SWIZZLE_NOOP
}
337 static const struct input_info fragInputs
[] = {
338 { "gl_FragCoord", FRAG_ATTRIB_WPOS
, SWIZZLE_NOOP
},
339 { "gl_Color", FRAG_ATTRIB_COL0
, SWIZZLE_NOOP
},
340 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
, SWIZZLE_NOOP
},
341 { "gl_TexCoord", FRAG_ATTRIB_TEX0
, SWIZZLE_NOOP
},
342 /* note: we're packing several quantities into the fogcoord vector */
343 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_XXXX
},
344 { "gl_FrontFacing", FRAG_ATTRIB_FOGC
, SWIZZLE_YYYY
}, /*XXX*/
345 { "gl_PointCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_ZWWW
},
346 { NULL
, 0, SWIZZLE_NOOP
}
349 const struct input_info
*inputs
350 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertInputs
: fragInputs
;
352 ASSERT(MAX_TEXTURE_UNITS
== 8); /* if this fails, fix vertInputs above */
354 for (i
= 0; inputs
[i
].Name
; i
++) {
355 if (strcmp(inputs
[i
].Name
, name
) == 0) {
357 *swizzleOut
= inputs
[i
].Swizzle
;
358 return inputs
[i
].Attrib
;
366 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
367 * a vertex or fragment program output variable. Return -1 for an invalid
371 _slang_output_index(const char *name
, GLenum target
)
377 static const struct output_info vertOutputs
[] = {
378 { "gl_Position", VERT_RESULT_HPOS
},
379 { "gl_FrontColor", VERT_RESULT_COL0
},
380 { "gl_BackColor", VERT_RESULT_BFC0
},
381 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
},
382 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
},
383 { "gl_TexCoord", VERT_RESULT_TEX0
},
384 { "gl_FogFragCoord", VERT_RESULT_FOGC
},
385 { "gl_PointSize", VERT_RESULT_PSIZ
},
388 static const struct output_info fragOutputs
[] = {
389 { "gl_FragColor", FRAG_RESULT_COLR
},
390 { "gl_FragDepth", FRAG_RESULT_DEPR
},
391 { "gl_FragData", FRAG_RESULT_DATA0
},
395 const struct output_info
*outputs
396 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertOutputs
: fragOutputs
;
398 for (i
= 0; outputs
[i
].Name
; i
++) {
399 if (strcmp(outputs
[i
].Name
, name
) == 0) {
401 return outputs
[i
].Attrib
;
409 /**********************************************************************/
413 * Map "_asm foo" to IR_FOO, etc.
418 slang_ir_opcode Opcode
;
419 GLuint HaveRetValue
, NumParams
;
423 static slang_asm_info AsmInfo
[] = {
425 { "vec4_add", IR_ADD
, 1, 2 },
426 { "vec4_subtract", IR_SUB
, 1, 2 },
427 { "vec4_multiply", IR_MUL
, 1, 2 },
428 { "vec4_dot", IR_DOT4
, 1, 2 },
429 { "vec3_dot", IR_DOT3
, 1, 2 },
430 { "vec2_dot", IR_DOT2
, 1, 2 },
431 { "vec3_nrm", IR_NRM3
, 1, 1 },
432 { "vec4_nrm", IR_NRM4
, 1, 1 },
433 { "vec3_cross", IR_CROSS
, 1, 2 },
434 { "vec4_lrp", IR_LRP
, 1, 3 },
435 { "vec4_min", IR_MIN
, 1, 2 },
436 { "vec4_max", IR_MAX
, 1, 2 },
437 { "vec4_clamp", IR_CLAMP
, 1, 3 },
438 { "vec4_seq", IR_SEQUAL
, 1, 2 },
439 { "vec4_sne", IR_SNEQUAL
, 1, 2 },
440 { "vec4_sge", IR_SGE
, 1, 2 },
441 { "vec4_sgt", IR_SGT
, 1, 2 },
442 { "vec4_sle", IR_SLE
, 1, 2 },
443 { "vec4_slt", IR_SLT
, 1, 2 },
445 { "vec4_move", IR_MOVE
, 1, 1 },
446 { "vec4_floor", IR_FLOOR
, 1, 1 },
447 { "vec4_frac", IR_FRAC
, 1, 1 },
448 { "vec4_abs", IR_ABS
, 1, 1 },
449 { "vec4_negate", IR_NEG
, 1, 1 },
450 { "vec4_ddx", IR_DDX
, 1, 1 },
451 { "vec4_ddy", IR_DDY
, 1, 1 },
452 /* float binary op */
453 { "float_power", IR_POW
, 1, 2 },
454 /* texture / sampler */
455 { "vec4_tex1d", IR_TEX
, 1, 2 },
456 { "vec4_texb1d", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
457 { "vec4_texp1d", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
458 { "vec4_tex2d", IR_TEX
, 1, 2 },
459 { "vec4_texb2d", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
460 { "vec4_texp2d", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
461 { "vec4_tex3d", IR_TEX
, 1, 2 },
462 { "vec4_texb3d", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
463 { "vec4_texp3d", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
464 { "vec4_texcube", IR_TEX
, 1, 2 }, /* cubemap */
465 { "vec4_tex_rect", IR_TEX
, 1, 2 }, /* rectangle */
466 { "vec4_texp_rect", IR_TEX
, 1, 2 },/* rectangle w/ projection */
469 { "ivec4_to_vec4", IR_I_TO_F
, 1, 1 }, /* int[4] to float[4] */
470 { "vec4_to_ivec4", IR_F_TO_I
, 1, 1 }, /* float[4] to int[4] */
471 { "float_exp", IR_EXP
, 1, 1 },
472 { "float_exp2", IR_EXP2
, 1, 1 },
473 { "float_log2", IR_LOG2
, 1, 1 },
474 { "float_rsq", IR_RSQ
, 1, 1 },
475 { "float_rcp", IR_RCP
, 1, 1 },
476 { "float_sine", IR_SIN
, 1, 1 },
477 { "float_cosine", IR_COS
, 1, 1 },
478 { "float_noise1", IR_NOISE1
, 1, 1},
479 { "float_noise2", IR_NOISE2
, 1, 1},
480 { "float_noise3", IR_NOISE3
, 1, 1},
481 { "float_noise4", IR_NOISE4
, 1, 1},
483 { NULL
, IR_NOP
, 0, 0 }
487 static slang_ir_node
*
488 new_node3(slang_ir_opcode op
,
489 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
491 slang_ir_node
*n
= (slang_ir_node
*) _slang_alloc(sizeof(slang_ir_node
));
497 n
->InstLocation
= -1;
502 static slang_ir_node
*
503 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
505 return new_node3(op
, c0
, c1
, NULL
);
508 static slang_ir_node
*
509 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
511 return new_node3(op
, c0
, NULL
, NULL
);
514 static slang_ir_node
*
515 new_node0(slang_ir_opcode op
)
517 return new_node3(op
, NULL
, NULL
, NULL
);
522 * Create sequence of two nodes.
524 static slang_ir_node
*
525 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
531 return new_node2(IR_SEQ
, left
, right
);
534 static slang_ir_node
*
535 new_label(slang_label
*label
)
537 slang_ir_node
*n
= new_node0(IR_LABEL
);
544 static slang_ir_node
*
545 new_float_literal(const float v
[4], GLuint size
)
547 slang_ir_node
*n
= new_node0(IR_FLOAT
);
549 COPY_4V(n
->Value
, v
);
550 /* allocate a storage object, but compute actual location (Index) later */
551 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
556 static slang_ir_node
*
557 new_not(slang_ir_node
*n
)
559 return new_node1(IR_NOT
, n
);
564 * Non-inlined function call.
566 static slang_ir_node
*
567 new_function_call(slang_ir_node
*code
, slang_label
*name
)
569 slang_ir_node
*n
= new_node1(IR_CALL
, code
);
578 * Unconditional jump.
580 static slang_ir_node
*
581 new_return(slang_label
*dest
)
583 slang_ir_node
*n
= new_node0(IR_RETURN
);
591 static slang_ir_node
*
592 new_loop(slang_ir_node
*body
)
594 return new_node1(IR_LOOP
, body
);
598 static slang_ir_node
*
599 new_break(slang_ir_node
*loopNode
)
601 slang_ir_node
*n
= new_node0(IR_BREAK
);
603 assert(loopNode
->Opcode
== IR_LOOP
);
605 /* insert this node at head of linked list */
606 n
->List
= loopNode
->List
;
614 * Make new IR_BREAK_IF_TRUE.
616 static slang_ir_node
*
617 new_break_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
621 assert(loopNode
->Opcode
== IR_LOOP
);
622 n
= new_node1(IR_BREAK_IF_TRUE
, cond
);
624 /* insert this node at head of linked list */
625 n
->List
= loopNode
->List
;
633 * Make new IR_CONT_IF_TRUE node.
635 static slang_ir_node
*
636 new_cont_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
640 assert(loopNode
->Opcode
== IR_LOOP
);
641 n
= new_node1(IR_CONT_IF_TRUE
, cond
);
643 /* insert this node at head of linked list */
644 n
->List
= loopNode
->List
;
651 static slang_ir_node
*
652 new_cond(slang_ir_node
*n
)
654 slang_ir_node
*c
= new_node1(IR_COND
, n
);
659 static slang_ir_node
*
660 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
662 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
667 * New IR_VAR node - a reference to a previously declared variable.
669 static slang_ir_node
*
670 new_var(slang_assemble_ctx
*A
, slang_variable
*var
)
676 assert(var
->declared
);
678 n
= new_node0(IR_VAR
);
680 _slang_attach_storage(n
, var
);
682 printf("new_var %s store=%p\n", (char*)name, (void*) n->Store);
690 * Check if the given function is really just a wrapper for a
691 * basic assembly instruction.
694 slang_is_asm_function(const slang_function
*fun
)
696 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
697 fun
->body
->num_children
== 1 &&
698 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
706 _slang_is_noop(const slang_operation
*oper
)
709 oper
->type
== SLANG_OPER_VOID
||
710 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
718 * Recursively search tree for a node of the given type.
720 static slang_operation
*
721 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
724 if (oper
->type
== type
)
726 for (i
= 0; i
< oper
->num_children
; i
++) {
727 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
736 * Count the number of operations of the given time rooted at 'oper'.
739 _slang_count_node_type(slang_operation
*oper
, slang_operation_type type
)
742 if (oper
->type
== type
) {
745 for (i
= 0; i
< oper
->num_children
; i
++) {
746 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
753 * Check if the 'return' statement found under 'oper' is a "tail return"
754 * that can be no-op'd. For example:
759 * return; // this is a no-op
762 * This is used when determining if a function can be inlined. If the
763 * 'return' is not the last statement, we can't inline the function since
764 * we still need the semantic behaviour of the 'return' but we don't want
765 * to accidentally return from the _calling_ function. We'd need to use an
766 * unconditional branch, but we don't have such a GPU instruction (not
770 _slang_is_tail_return(const slang_operation
*oper
)
772 GLuint k
= oper
->num_children
;
775 const slang_operation
*last
= &oper
->children
[k
- 1];
776 if (last
->type
== SLANG_OPER_RETURN
)
778 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
779 last
->type
== SLANG_OPER_LABEL
)
780 k
--; /* try prev child */
781 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
782 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
783 /* try sub-children */
784 return _slang_is_tail_return(last
);
794 slang_resolve_variable(slang_operation
*oper
)
796 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
797 oper
->var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
803 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
806 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
807 GLuint substCount
, slang_variable
**substOld
,
808 slang_operation
**substNew
, GLboolean isLHS
)
810 switch (oper
->type
) {
811 case SLANG_OPER_VARIABLE_DECL
:
813 slang_variable
*v
= _slang_variable_locate(oper
->locals
,
814 oper
->a_id
, GL_TRUE
);
816 if (v
->initializer
&& oper
->num_children
== 0) {
817 /* set child of oper to copy of initializer */
818 oper
->num_children
= 1;
819 oper
->children
= slang_operation_new(1);
820 slang_operation_copy(&oper
->children
[0], v
->initializer
);
822 if (oper
->num_children
== 1) {
823 /* the initializer */
824 slang_substitute(A
, &oper
->children
[0], substCount
,
825 substOld
, substNew
, GL_FALSE
);
829 case SLANG_OPER_IDENTIFIER
:
830 assert(oper
->num_children
== 0);
831 if (1/**!isLHS XXX FIX */) {
832 slang_atom id
= oper
->a_id
;
835 v
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
837 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
841 /* look for a substitution */
842 for (i
= 0; i
< substCount
; i
++) {
843 if (v
== substOld
[i
]) {
844 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
845 #if 0 /* DEBUG only */
846 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
847 assert(substNew
[i
]->var
);
848 assert(substNew
[i
]->var
->a_name
);
849 printf("Substitute %s with %s in id node %p\n",
850 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
854 printf("Substitute %s with %f in id node %p\n",
855 (char*)v
->a_name
, substNew
[i
]->literal
[0],
859 slang_operation_copy(oper
, substNew
[i
]);
866 case SLANG_OPER_RETURN
:
867 /* do return replacement here too */
868 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
869 if (oper
->num_children
== 1 && !_slang_is_noop(&oper
->children
[0])) {
875 * then do substitutions on the assignment.
877 slang_operation
*blockOper
, *assignOper
, *returnOper
;
879 /* check if function actually has a return type */
880 assert(A
->CurFunction
);
881 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
882 slang_info_log_error(A
->log
, "illegal return expression");
886 blockOper
= slang_operation_new(1);
887 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
888 blockOper
->num_children
= 2;
889 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
890 blockOper
->children
= slang_operation_new(2);
891 assignOper
= blockOper
->children
+ 0;
892 returnOper
= blockOper
->children
+ 1;
894 assignOper
->type
= SLANG_OPER_ASSIGN
;
895 assignOper
->num_children
= 2;
896 assignOper
->locals
->outer_scope
= blockOper
->locals
;
897 assignOper
->children
= slang_operation_new(2);
898 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
899 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
900 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
902 slang_operation_copy(&assignOper
->children
[1],
905 returnOper
->type
= SLANG_OPER_RETURN
; /* return w/ no value */
906 assert(returnOper
->num_children
== 0);
908 /* do substitutions on the "__retVal = expr" sub-tree */
909 slang_substitute(A
, assignOper
,
910 substCount
, substOld
, substNew
, GL_FALSE
);
912 /* install new code */
913 slang_operation_copy(oper
, blockOper
);
914 slang_operation_destruct(blockOper
);
917 /* check if return value was expected */
918 assert(A
->CurFunction
);
919 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
920 slang_info_log_error(A
->log
, "return statement requires an expression");
926 case SLANG_OPER_ASSIGN
:
927 case SLANG_OPER_SUBSCRIPT
:
929 * child[0] can't have substitutions but child[1] can.
931 slang_substitute(A
, &oper
->children
[0],
932 substCount
, substOld
, substNew
, GL_TRUE
);
933 slang_substitute(A
, &oper
->children
[1],
934 substCount
, substOld
, substNew
, GL_FALSE
);
936 case SLANG_OPER_FIELD
:
938 slang_substitute(A
, &oper
->children
[0],
939 substCount
, substOld
, substNew
, GL_TRUE
);
944 for (i
= 0; i
< oper
->num_children
; i
++)
945 slang_substitute(A
, &oper
->children
[i
],
946 substCount
, substOld
, substNew
, GL_FALSE
);
953 * Produce inline code for a call to an assembly instruction.
954 * This is typically used to compile a call to a built-in function like this:
956 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
958 * __asm vec4_lrp __retVal, a, y, x;
963 * r = mix(p1, p2, p3);
973 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
975 static slang_operation
*
976 slang_inline_asm_function(slang_assemble_ctx
*A
,
977 slang_function
*fun
, slang_operation
*oper
)
979 const GLuint numArgs
= oper
->num_children
;
981 slang_operation
*inlined
;
982 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
983 slang_variable
**substOld
;
984 slang_operation
**substNew
;
986 ASSERT(slang_is_asm_function(fun
));
987 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
990 printf("Inline %s as %s\n",
991 (char*) fun->header.a_name,
992 (char*) fun->body->children[0].a_id);
996 * We'll substitute formal params with actual args in the asm call.
998 substOld
= (slang_variable
**)
999 _slang_alloc(numArgs
* sizeof(slang_variable
*));
1000 substNew
= (slang_operation
**)
1001 _slang_alloc(numArgs
* sizeof(slang_operation
*));
1002 for (i
= 0; i
< numArgs
; i
++) {
1003 substOld
[i
] = fun
->parameters
->variables
[i
];
1004 substNew
[i
] = oper
->children
+ i
;
1007 /* make a copy of the code to inline */
1008 inlined
= slang_operation_new(1);
1009 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
1011 /* get rid of the __retVal child */
1012 inlined
->num_children
--;
1013 for (i
= 0; i
< inlined
->num_children
; i
++) {
1014 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1018 /* now do formal->actual substitutions */
1019 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1021 _slang_free(substOld
);
1022 _slang_free(substNew
);
1025 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1026 (char *) fun
->header
.a_name
);
1027 slang_print_tree(inlined
, 3);
1028 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1036 * Inline the given function call operation.
1037 * Return a new slang_operation that corresponds to the inlined code.
1039 static slang_operation
*
1040 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1041 slang_operation
*oper
, slang_operation
*returnOper
)
1048 ParamMode
*paramMode
;
1049 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1050 const GLuint numArgs
= oper
->num_children
;
1051 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1052 slang_operation
*args
= oper
->children
;
1053 slang_operation
*inlined
, *top
;
1054 slang_variable
**substOld
;
1055 slang_operation
**substNew
;
1056 GLuint substCount
, numCopyIn
, i
;
1057 slang_function
*prevFunction
;
1058 slang_variable_scope
*newScope
= NULL
;
1061 prevFunction
= A
->CurFunction
;
1062 A
->CurFunction
= fun
;
1064 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1065 assert(fun
->param_count
== totalArgs
);
1067 /* allocate temporary arrays */
1068 paramMode
= (ParamMode
*)
1069 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1070 substOld
= (slang_variable
**)
1071 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1072 substNew
= (slang_operation
**)
1073 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1076 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1077 (char *) fun
->header
.a_name
,
1078 fun
->parameters
->num_variables
, numArgs
);
1081 if (haveRetValue
&& !returnOper
) {
1082 /* Create 3-child comma sequence for inlined code:
1083 * child[0]: declare __resultTmp
1084 * child[1]: inlined function body
1085 * child[2]: __resultTmp
1087 slang_operation
*commaSeq
;
1088 slang_operation
*declOper
= NULL
;
1089 slang_variable
*resultVar
;
1091 commaSeq
= slang_operation_new(1);
1092 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1093 assert(commaSeq
->locals
);
1094 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1095 commaSeq
->num_children
= 3;
1096 commaSeq
->children
= slang_operation_new(3);
1097 /* allocate the return var */
1098 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1100 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1101 (void*)commaSeq->locals, (char *) fun->header.a_name);
1104 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1105 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1106 resultVar
->isTemp
= GL_TRUE
;
1108 /* child[0] = __resultTmp declaration */
1109 declOper
= &commaSeq
->children
[0];
1110 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1111 declOper
->a_id
= resultVar
->a_name
;
1112 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1114 /* child[1] = function body */
1115 inlined
= &commaSeq
->children
[1];
1116 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1118 /* child[2] = __resultTmp reference */
1119 returnOper
= &commaSeq
->children
[2];
1120 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1121 returnOper
->a_id
= resultVar
->a_name
;
1122 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1127 top
= inlined
= slang_operation_new(1);
1128 /* XXXX this may be inappropriate!!!! */
1129 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1133 assert(inlined
->locals
);
1135 /* Examine the parameters, look for inout/out params, look for possible
1136 * substitutions, etc:
1137 * param type behaviour
1138 * in copy actual to local
1139 * const in substitute param with actual
1143 for (i
= 0; i
< totalArgs
; i
++) {
1144 slang_variable
*p
= fun
->parameters
->variables
[i
];
1146 printf("Param %d: %s %s \n", i,
1147 slang_type_qual_string(p->type.qualifier),
1148 (char *) p->a_name);
1150 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1151 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1152 /* an output param */
1153 slang_operation
*arg
;
1158 paramMode
[i
] = SUBST
;
1160 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1161 slang_resolve_variable(arg
);
1163 /* replace parameter 'p' with argument 'arg' */
1164 substOld
[substCount
] = p
;
1165 substNew
[substCount
] = arg
; /* will get copied */
1168 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1169 /* a constant input param */
1170 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1171 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
1172 /* replace all occurances of this parameter variable with the
1173 * actual argument variable or a literal.
1175 paramMode
[i
] = SUBST
;
1176 slang_resolve_variable(&args
[i
]);
1177 substOld
[substCount
] = p
;
1178 substNew
[substCount
] = &args
[i
]; /* will get copied */
1182 paramMode
[i
] = COPY_IN
;
1186 paramMode
[i
] = COPY_IN
;
1188 assert(paramMode
[i
]);
1191 /* actual code inlining: */
1192 slang_operation_copy(inlined
, fun
->body
);
1194 /*** XXX review this */
1195 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1196 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1197 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1200 printf("======================= orig body code ======================\n");
1201 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1202 slang_print_tree(fun
->body
, 8);
1203 printf("======================= copied code =========================\n");
1204 slang_print_tree(inlined
, 8);
1207 /* do parameter substitution in inlined code: */
1208 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1211 printf("======================= subst code ==========================\n");
1212 slang_print_tree(inlined
, 8);
1213 printf("=============================================================\n");
1216 /* New prolog statements: (inserted before the inlined code)
1217 * Copy the 'in' arguments.
1220 for (i
= 0; i
< numArgs
; i
++) {
1221 if (paramMode
[i
] == COPY_IN
) {
1222 slang_variable
*p
= fun
->parameters
->variables
[i
];
1223 /* declare parameter 'p' */
1224 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1228 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1229 assert(decl
->locals
);
1230 decl
->locals
->outer_scope
= inlined
->locals
;
1231 decl
->a_id
= p
->a_name
;
1232 decl
->num_children
= 1;
1233 decl
->children
= slang_operation_new(1);
1235 /* child[0] is the var's initializer */
1236 slang_operation_copy(&decl
->children
[0], args
+ i
);
1238 /* add parameter 'p' to the local variable scope here */
1240 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1241 pCopy
->type
= p
->type
;
1242 pCopy
->a_name
= p
->a_name
;
1243 pCopy
->array_len
= p
->array_len
;
1246 newScope
= inlined
->locals
;
1251 /* Now add copies of the function's local vars to the new variable scope */
1252 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1253 slang_variable
*p
= fun
->parameters
->variables
[i
];
1254 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1255 pCopy
->type
= p
->type
;
1256 pCopy
->a_name
= p
->a_name
;
1257 pCopy
->array_len
= p
->array_len
;
1261 /* New epilog statements:
1262 * 1. Create end of function label to jump to from return statements.
1263 * 2. Copy the 'out' parameter vars
1266 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1268 inlined
->num_children
);
1269 lab
->type
= SLANG_OPER_LABEL
;
1270 lab
->label
= A
->curFuncEndLabel
;
1273 for (i
= 0; i
< totalArgs
; i
++) {
1274 if (paramMode
[i
] == COPY_OUT
) {
1275 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1276 /* actualCallVar = outParam */
1277 /*if (i > 0 || !haveRetValue)*/
1278 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1280 inlined
->num_children
);
1281 ass
->type
= SLANG_OPER_ASSIGN
;
1282 ass
->num_children
= 2;
1283 ass
->locals
->outer_scope
= inlined
->locals
;
1284 ass
->children
= slang_operation_new(2);
1285 ass
->children
[0] = args
[i
]; /*XXX copy */
1286 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1287 ass
->children
[1].a_id
= p
->a_name
;
1288 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1292 _slang_free(paramMode
);
1293 _slang_free(substOld
);
1294 _slang_free(substNew
);
1296 /* Update scoping to use the new local vars instead of the
1297 * original function's vars. This is especially important
1298 * for nested inlining.
1301 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1304 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1305 (char *) fun
->header
.a_name
,
1306 fun
->parameters
->num_variables
, numArgs
);
1307 slang_print_tree(top
, 0);
1311 A
->CurFunction
= prevFunction
;
1317 static slang_ir_node
*
1318 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1319 slang_operation
*oper
, slang_operation
*dest
)
1322 slang_operation
*inlined
;
1323 slang_label
*prevFuncEndLabel
;
1326 prevFuncEndLabel
= A
->curFuncEndLabel
;
1327 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1328 A
->curFuncEndLabel
= _slang_label_new(name
);
1329 assert(A
->curFuncEndLabel
);
1331 if (slang_is_asm_function(fun
) && !dest
) {
1332 /* assemble assembly function - tree style */
1333 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1336 /* non-assembly function */
1337 /* We always generate an "inline-able" block of code here.
1339 * 1. insert the inline code
1340 * 2. Generate a call to the "inline" code as a subroutine
1344 slang_operation
*ret
= NULL
;
1346 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1350 ret
= _slang_find_node_type(inlined
, SLANG_OPER_RETURN
);
1352 /* check if this is a "tail" return */
1353 if (_slang_count_node_type(inlined
, SLANG_OPER_RETURN
) == 1 &&
1354 _slang_is_tail_return(inlined
)) {
1355 /* The only RETURN is the last stmt in the function, no-op it
1356 * and inline the function body.
1358 ret
->type
= SLANG_OPER_NONE
;
1361 slang_operation
*callOper
;
1362 /* The function we're calling has one or more 'return' statements.
1363 * So, we can't truly inline this function because we need to
1364 * implement 'return' with RET (and CAL).
1365 * Nevertheless, we performed "inlining" to make a new instance
1366 * of the function body to deal with static register allocation.
1368 * XXX check if there's one 'return' and if it's the very last
1369 * statement in the function - we can optimize that case.
1371 assert(inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1372 inlined
->type
== SLANG_OPER_SEQUENCE
);
1374 if (_slang_function_has_return_value(fun
) && !dest
) {
1375 assert(inlined
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1376 assert(inlined
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1377 callOper
= &inlined
->children
[1];
1382 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1383 callOper
->fun
= fun
;
1384 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1392 /* Replace the function call with the inlined block (or new CALL stmt) */
1393 slang_operation_destruct(oper
);
1395 _slang_free(inlined
);
1398 assert(inlined
->locals
);
1399 printf("*** Inlined code for call to %s:\n",
1400 (char*) fun
->header
.a_name
);
1401 slang_print_tree(oper
, 10);
1405 n
= _slang_gen_operation(A
, oper
);
1407 /*_slang_label_delete(A->curFuncEndLabel);*/
1408 A
->curFuncEndLabel
= prevFuncEndLabel
;
1414 static slang_asm_info
*
1415 slang_find_asm_info(const char *name
)
1418 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1419 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1428 * Return the default swizzle mask for accessing a variable of the
1429 * given size (in floats). If size = 1, comp is used to identify
1430 * which component [0..3] of the register holds the variable.
1433 _slang_var_swizzle(GLint size
, GLint comp
)
1437 return MAKE_SWIZZLE4(comp
, comp
, comp
, comp
);
1439 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_NIL
, SWIZZLE_NIL
);
1441 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_NIL
);
1443 return SWIZZLE_XYZW
;
1449 * Some write-masked assignments are simple, but others are hard.
1452 * v.xy = vec2(a, b);
1455 * v.zy = vec2(a, b);
1456 * this gets transformed/swizzled into:
1457 * v.zy = vec2(a, b).*yx* (* = don't care)
1458 * This function helps to determine simple vs. non-simple.
1461 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1463 switch (writemask
) {
1465 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1467 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1469 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1471 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1473 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1474 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1476 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1477 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1478 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1479 case WRITEMASK_XYZW
:
1480 return swizzle
== SWIZZLE_NOOP
;
1488 * Convert the given swizzle into a writemask. In some cases this
1489 * is trivial, in other cases, we'll need to also swizzle the right
1490 * hand side to put components in the right places.
1491 * See comment above for more info.
1492 * XXX this function could be simplified and should probably be renamed.
1493 * \param swizzle the incoming swizzle
1494 * \param writemaskOut returns the writemask
1495 * \param swizzleOut swizzle to apply to the right-hand-side
1496 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1499 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1500 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1502 GLuint mask
= 0x0, newSwizzle
[4];
1505 /* make new dst writemask, compute size */
1506 for (i
= 0; i
< 4; i
++) {
1507 const GLuint swz
= GET_SWZ(swizzle
, i
);
1508 if (swz
== SWIZZLE_NIL
) {
1512 assert(swz
>= 0 && swz
<= 3);
1514 if (swizzle
!= SWIZZLE_XXXX
&&
1515 swizzle
!= SWIZZLE_YYYY
&&
1516 swizzle
!= SWIZZLE_ZZZZ
&&
1517 swizzle
!= SWIZZLE_WWWW
&&
1518 (mask
& (1 << swz
))) {
1519 /* a channel can't be specified twice (ex: ".xyyz") */
1520 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1521 _mesa_swizzle_string(swizzle
, 0, 0));
1527 assert(mask
<= 0xf);
1528 size
= i
; /* number of components in mask/swizzle */
1530 *writemaskOut
= mask
;
1532 /* make new src swizzle, by inversion */
1533 for (i
= 0; i
< 4; i
++) {
1534 newSwizzle
[i
] = i
; /*identity*/
1536 for (i
= 0; i
< size
; i
++) {
1537 const GLuint swz
= GET_SWZ(swizzle
, i
);
1538 newSwizzle
[swz
] = i
;
1540 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1545 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1547 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1549 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1551 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1553 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1562 * Recursively traverse 'oper' to produce a swizzle mask in the event
1563 * of any vector subscripts and swizzle suffixes.
1564 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1567 resolve_swizzle(const slang_operation
*oper
)
1569 if (oper
->type
== SLANG_OPER_FIELD
) {
1570 /* writemask from .xyzw suffix */
1572 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1573 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1577 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1578 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1582 return SWIZZLE_XYZW
;
1584 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1585 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1586 /* writemask from [index] */
1587 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1588 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1593 swizzle
= SWIZZLE_XXXX
;
1596 swizzle
= SWIZZLE_YYYY
;
1599 swizzle
= SWIZZLE_ZZZZ
;
1602 swizzle
= SWIZZLE_WWWW
;
1605 swizzle
= SWIZZLE_XYZW
;
1607 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1611 return SWIZZLE_XYZW
;
1617 * Recursively descend through swizzle nodes to find the node's storage info.
1619 static slang_ir_storage
*
1620 get_store(const slang_ir_node
*n
)
1622 if (n
->Opcode
== IR_SWIZZLE
) {
1623 return get_store(n
->Children
[0]);
1631 * Generate IR tree for an asm instruction/operation such as:
1632 * __asm vec4_dot __retVal.x, v1, v2;
1634 static slang_ir_node
*
1635 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1636 slang_operation
*dest
)
1638 const slang_asm_info
*info
;
1639 slang_ir_node
*kids
[3], *n
;
1640 GLuint j
, firstOperand
;
1642 assert(oper
->type
== SLANG_OPER_ASM
);
1644 info
= slang_find_asm_info((char *) oper
->a_id
);
1646 _mesa_problem(NULL
, "undefined __asm function %s\n",
1647 (char *) oper
->a_id
);
1650 assert(info
->NumParams
<= 3);
1652 if (info
->NumParams
== oper
->num_children
) {
1653 /* Storage for result is not specified.
1654 * Children[0], [1], [2] are the operands.
1659 /* Storage for result (child[0]) is specified.
1660 * Children[1], [2], [3] are the operands.
1665 /* assemble child(ren) */
1666 kids
[0] = kids
[1] = kids
[2] = NULL
;
1667 for (j
= 0; j
< info
->NumParams
; j
++) {
1668 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1673 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1676 /* Setup n->Store to be a particular location. Otherwise, storage
1677 * for the result (a temporary) will be allocated later.
1679 slang_operation
*dest_oper
;
1682 dest_oper
= &oper
->children
[0];
1684 n0
= _slang_gen_operation(A
, dest_oper
);
1689 n
->Store
= n0
->Store
;
1691 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1701 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1704 for (i
= 0; i
< scope
->num_functions
; i
++) {
1705 slang_function
*f
= &scope
->functions
[i
];
1706 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1707 printf(" %s (%d args)\n", name
, f
->param_count
);
1710 if (scope
->outer_scope
)
1711 print_funcs(scope
->outer_scope
, name
);
1716 * Find a function of the given name, taking 'numArgs' arguments.
1717 * This is the function we'll try to call when there is no exact match
1718 * between function parameters and call arguments.
1720 * XXX we should really create a list of candidate functions and try
1723 static slang_function
*
1724 _slang_find_function_by_argc(slang_function_scope
*scope
,
1725 const char *name
, int numArgs
)
1729 for (i
= 0; i
< scope
->num_functions
; i
++) {
1730 slang_function
*f
= &scope
->functions
[i
];
1731 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1732 int haveRetValue
= _slang_function_has_return_value(f
);
1733 if (numArgs
== f
->param_count
- haveRetValue
)
1737 scope
= scope
->outer_scope
;
1744 static slang_function
*
1745 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
1748 slang_function
*maxFunc
= NULL
;
1753 for (i
= 0; i
< scope
->num_functions
; i
++) {
1754 slang_function
*f
= &scope
->functions
[i
];
1755 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1756 if (f
->param_count
> maxArgs
) {
1757 maxArgs
= f
->param_count
;
1762 scope
= scope
->outer_scope
;
1770 * Generate a new slang_function which is a constructor for a user-defined
1773 static slang_function
*
1774 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
1776 const GLint numFields
= str
->fields
->num_variables
;
1777 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1779 /* function header (name, return type) */
1780 fun
->header
.a_name
= str
->a_name
;
1781 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1782 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
1783 fun
->header
.type
.specifier
._struct
= str
;
1785 /* function parameters (= struct's fields) */
1788 for (i
= 0; i
< numFields
; i
++) {
1790 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1792 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1793 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
1794 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1796 fun
->param_count
= fun
->parameters
->num_variables
;
1799 /* Add __retVal to params */
1801 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1802 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1804 p
->a_name
= a_retVal
;
1805 p
->type
= fun
->header
.type
;
1806 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1810 /* function body is:
1820 slang_variable_scope
*scope
;
1821 slang_variable
*var
;
1824 fun
->body
= slang_operation_new(1);
1825 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1826 fun
->body
->num_children
= numFields
+ 2;
1827 fun
->body
->children
= slang_operation_new(numFields
+ 2);
1829 scope
= fun
->body
->locals
;
1830 scope
->outer_scope
= fun
->parameters
;
1832 /* create local var 't' */
1833 var
= slang_variable_scope_grow(scope
);
1834 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
1835 var
->type
= fun
->header
.type
;
1839 slang_operation
*decl
;
1841 decl
= &fun
->body
->children
[0];
1842 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1843 decl
->locals
= _slang_variable_scope_new(scope
);
1844 decl
->a_id
= var
->a_name
;
1847 /* assign params to fields of t */
1848 for (i
= 0; i
< numFields
; i
++) {
1849 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
1851 assign
->type
= SLANG_OPER_ASSIGN
;
1852 assign
->locals
= _slang_variable_scope_new(scope
);
1853 assign
->num_children
= 2;
1854 assign
->children
= slang_operation_new(2);
1857 slang_operation
*lhs
= &assign
->children
[0];
1859 lhs
->type
= SLANG_OPER_FIELD
;
1860 lhs
->locals
= _slang_variable_scope_new(scope
);
1861 lhs
->num_children
= 1;
1862 lhs
->children
= slang_operation_new(1);
1863 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1865 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1866 lhs
->children
[0].a_id
= var
->a_name
;
1867 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
1870 lhs
->children
[1].num_children
= 1;
1871 lhs
->children
[1].children
= slang_operation_new(1);
1872 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
1873 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
1874 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
1879 slang_operation
*rhs
= &assign
->children
[1];
1881 rhs
->type
= SLANG_OPER_IDENTIFIER
;
1882 rhs
->locals
= _slang_variable_scope_new(scope
);
1883 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1889 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
1891 ret
->type
= SLANG_OPER_RETURN
;
1892 ret
->locals
= _slang_variable_scope_new(scope
);
1893 ret
->num_children
= 1;
1894 ret
->children
= slang_operation_new(1);
1895 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1896 ret
->children
[0].a_id
= var
->a_name
;
1897 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
1901 slang_print_function(fun, 1);
1908 * Find/create a function (constructor) for the given structure name.
1910 static slang_function
*
1911 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
1914 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
1915 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
1916 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
1917 /* found a structure type that matches the function name */
1918 if (!str
->constructor
) {
1919 /* create the constructor function now */
1920 str
->constructor
= _slang_make_struct_constructor(A
, str
);
1922 return str
->constructor
;
1930 * Generate a new slang_function to satisfy a call to an array constructor.
1931 * Ex: float[3](1., 2., 3.)
1933 static slang_function
*
1934 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
1936 slang_type_specifier_type baseType
;
1937 slang_function
*fun
;
1940 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1944 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
1946 num_elements
= oper
->num_children
;
1948 /* function header, return type */
1950 fun
->header
.a_name
= oper
->a_id
;
1951 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1952 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
1953 fun
->header
.type
.specifier
._array
=
1954 slang_type_specifier_new(baseType
, NULL
, NULL
);
1955 fun
->header
.type
.array_len
= num_elements
;
1958 /* function parameters (= number of elements) */
1961 for (i
= 0; i
< num_elements
; i
++) {
1963 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1965 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1967 snprintf(name
, sizeof(name
), "p%d", i
);
1968 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
1969 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1970 p
->type
.specifier
.type
= baseType
;
1972 fun
->param_count
= fun
->parameters
->num_variables
;
1975 /* Add __retVal to params */
1977 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1978 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1980 p
->a_name
= a_retVal
;
1981 p
->type
= fun
->header
.type
;
1982 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1983 p
->type
.specifier
.type
= baseType
;
1987 /* function body is:
1997 slang_variable_scope
*scope
;
1998 slang_variable
*var
;
2001 fun
->body
= slang_operation_new(1);
2002 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
2003 fun
->body
->num_children
= num_elements
+ 2;
2004 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
2006 scope
= fun
->body
->locals
;
2007 scope
->outer_scope
= fun
->parameters
;
2009 /* create local var 't' */
2010 var
= slang_variable_scope_grow(scope
);
2011 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2012 var
->type
= fun
->header
.type
;/*XXX copy*/
2016 slang_operation
*decl
;
2018 decl
= &fun
->body
->children
[0];
2019 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2020 decl
->locals
= _slang_variable_scope_new(scope
);
2021 decl
->a_id
= var
->a_name
;
2024 /* assign params to elements of t */
2025 for (i
= 0; i
< num_elements
; i
++) {
2026 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2028 assign
->type
= SLANG_OPER_ASSIGN
;
2029 assign
->locals
= _slang_variable_scope_new(scope
);
2030 assign
->num_children
= 2;
2031 assign
->children
= slang_operation_new(2);
2034 slang_operation
*lhs
= &assign
->children
[0];
2036 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2037 lhs
->locals
= _slang_variable_scope_new(scope
);
2038 lhs
->num_children
= 2;
2039 lhs
->children
= slang_operation_new(2);
2041 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2042 lhs
->children
[0].a_id
= var
->a_name
;
2043 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2045 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2046 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2050 slang_operation
*rhs
= &assign
->children
[1];
2052 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2053 rhs
->locals
= _slang_variable_scope_new(scope
);
2054 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2060 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2062 ret
->type
= SLANG_OPER_RETURN
;
2063 ret
->locals
= _slang_variable_scope_new(scope
);
2064 ret
->num_children
= 1;
2065 ret
->children
= slang_operation_new(1);
2066 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2067 ret
->children
[0].a_id
= var
->a_name
;
2068 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2073 slang_print_function(fun, 1);
2081 _slang_is_vec_mat_type(const char *name
)
2083 static const char *vecmat_types
[] = {
2084 "float", "int", "bool",
2085 "vec2", "vec3", "vec4",
2086 "ivec2", "ivec3", "ivec4",
2087 "bvec2", "bvec3", "bvec4",
2088 "mat2", "mat3", "mat4",
2089 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2093 for (i
= 0; vecmat_types
[i
]; i
++)
2094 if (_mesa_strcmp(name
, vecmat_types
[i
]) == 0)
2101 * Assemble a function call, given a particular function name.
2102 * \param name the function's name (operators like '*' are possible).
2104 static slang_ir_node
*
2105 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2106 slang_operation
*oper
, slang_operation
*dest
)
2108 slang_operation
*params
= oper
->children
;
2109 const GLuint param_count
= oper
->num_children
;
2111 slang_function
*fun
;
2114 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2115 if (atom
== SLANG_ATOM_NULL
)
2118 if (oper
->array_constructor
) {
2119 /* this needs special handling */
2120 fun
= _slang_make_array_constructor(A
, oper
);
2123 /* Try to find function by name and exact argument type matching */
2124 GLboolean error
= GL_FALSE
;
2125 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2126 &A
->space
, A
->atoms
, A
->log
, &error
);
2128 slang_info_log_error(A
->log
,
2129 "Function '%s' not found (check argument types)",
2136 /* Next, try locating a constructor function for a user-defined type */
2137 fun
= _slang_locate_struct_constructor(A
, name
);
2141 * At this point, some heuristics are used to try to find a function
2142 * that matches the calling signature by means of casting or "unrolling"
2146 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2147 /* Next, if this call looks like a vec() or mat() constructor call,
2148 * try "unwinding" the args to satisfy a constructor.
2150 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2152 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2153 slang_info_log_error(A
->log
,
2154 "Function '%s' not found (check argument types)",
2161 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2162 /* Next, try casting args to the types of the formal parameters */
2163 int numArgs
= oper
->num_children
;
2164 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2165 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2166 slang_info_log_error(A
->log
,
2167 "Function '%s' not found (check argument types)",
2175 slang_info_log_error(A
->log
,
2176 "Function '%s' not found (check argument types)",
2181 slang_info_log_error(A
->log
,
2182 "Function '%s' prototyped but not defined. "
2183 "Separate compilation units not supported.",
2188 /* type checking to be sure function's return type matches 'dest' type */
2192 slang_typeinfo_construct(&t0
);
2193 typeof_operation(A
, dest
, &t0
);
2195 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2196 slang_info_log_error(A
->log
,
2197 "Incompatible type returned by call to '%s'",
2203 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2205 if (n
&& !n
->Store
&& !dest
2206 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2207 /* setup n->Store for the result of the function call */
2208 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2209 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2210 /*printf("Alloc storage for function result, size %d \n", size);*/
2213 if (oper
->array_constructor
) {
2214 /* free the temporary array constructor function now */
2215 slang_function_destruct(fun
);
2222 static slang_ir_node
*
2223 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2225 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2227 slang_variable
*var
;
2229 /* NOTE: In GLSL 1.20, there's only one kind of method
2230 * call: array.length(). Anything else is an error.
2232 if (oper
->a_id
!= a_length
) {
2233 slang_info_log_error(A
->log
,
2234 "Undefined method call '%s'", (char *) oper
->a_id
);
2238 /* length() takes no arguments */
2239 if (oper
->num_children
> 0) {
2240 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2244 /* lookup the object/variable */
2245 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2246 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2247 slang_info_log_error(A
->log
,
2248 "Undefined object '%s'", (char *) oper
->a_obj
);
2252 /* Create a float/literal IR node encoding the array length */
2253 n
= new_node0(IR_FLOAT
);
2255 n
->Value
[0] = (float) var
->array_len
;
2256 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2263 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2265 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2266 oper
->type
== SLANG_OPER_LITERAL_INT
||
2267 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2268 if (oper
->literal
[0])
2274 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2275 oper
->num_children
== 1) {
2276 return _slang_is_constant_cond(&oper
->children
[0], value
);
2283 * Test if an operation is a scalar or boolean.
2286 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2288 slang_typeinfo type
;
2291 slang_typeinfo_construct(&type
);
2292 typeof_operation(A
, oper
, &type
);
2293 size
= _slang_sizeof_type_specifier(&type
.spec
);
2294 slang_typeinfo_destruct(&type
);
2300 * Test if an operation is boolean.
2303 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2305 slang_typeinfo type
;
2308 slang_typeinfo_construct(&type
);
2309 typeof_operation(A
, oper
, &type
);
2310 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2311 slang_typeinfo_destruct(&type
);
2317 * Generate loop code using high-level IR_LOOP instruction
2319 static slang_ir_node
*
2320 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2324 * BREAK if !expr (child[0])
2325 * body code (child[1])
2327 slang_ir_node
*prevLoop
, *loop
, *breakIf
, *body
;
2328 GLboolean isConst
, constTrue
;
2330 /* type-check expression */
2331 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2332 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2336 /* Check if loop condition is a constant */
2337 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2339 if (isConst
&& !constTrue
) {
2340 /* loop is never executed! */
2341 return new_node0(IR_NOP
);
2344 loop
= new_loop(NULL
);
2346 /* save old, push new loop */
2347 prevLoop
= A
->CurLoop
;
2350 if (isConst
&& constTrue
) {
2351 /* while(nonzero constant), no conditional break */
2356 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2357 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2359 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2360 loop
->Children
[0] = new_seq(breakIf
, body
);
2362 /* Do infinite loop detection */
2363 /* loop->List is head of linked list of break/continue nodes */
2364 if (!loop
->List
&& isConst
&& constTrue
) {
2365 /* infinite loop detected */
2366 A
->CurLoop
= prevLoop
; /* clean-up */
2367 slang_info_log_error(A
->log
, "Infinite loop detected!");
2371 /* pop loop, restore prev */
2372 A
->CurLoop
= prevLoop
;
2379 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2381 static slang_ir_node
*
2382 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2386 * body code (child[0])
2388 * BREAK if !expr (child[1])
2390 slang_ir_node
*prevLoop
, *loop
;
2391 GLboolean isConst
, constTrue
;
2393 /* type-check expression */
2394 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2395 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2399 loop
= new_loop(NULL
);
2401 /* save old, push new loop */
2402 prevLoop
= A
->CurLoop
;
2406 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
2408 /* Check if loop condition is a constant */
2409 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
2410 if (isConst
&& constTrue
) {
2411 /* do { } while(1) ==> no conditional break */
2412 loop
->Children
[1] = NULL
; /* no tail code */
2416 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2417 loop
->Children
[1] = new_break_if_true(A
->CurLoop
, cond
);
2420 /* XXX we should do infinite loop detection, as above */
2422 /* pop loop, restore prev */
2423 A
->CurLoop
= prevLoop
;
2430 * Generate for-loop using high-level IR_LOOP instruction.
2432 static slang_ir_node
*
2433 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2436 * init code (child[0])
2438 * BREAK if !expr (child[1])
2439 * body code (child[3])
2441 * incr code (child[2]) // XXX continue here
2443 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
2445 init
= _slang_gen_operation(A
, &oper
->children
[0]);
2446 loop
= new_loop(NULL
);
2448 /* save old, push new loop */
2449 prevLoop
= A
->CurLoop
;
2452 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2453 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2454 body
= _slang_gen_operation(A
, &oper
->children
[3]);
2455 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
2457 loop
->Children
[0] = new_seq(breakIf
, body
);
2458 loop
->Children
[1] = incr
; /* tail code */
2460 /* pop loop, restore prev */
2461 A
->CurLoop
= prevLoop
;
2463 return new_seq(init
, loop
);
2467 static slang_ir_node
*
2468 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2470 slang_ir_node
*n
, *loopNode
;
2471 assert(oper
->type
== SLANG_OPER_CONTINUE
);
2472 loopNode
= A
->CurLoop
;
2474 assert(loopNode
->Opcode
== IR_LOOP
);
2475 n
= new_node0(IR_CONT
);
2477 n
->Parent
= loopNode
;
2478 /* insert this node at head of linked list */
2479 n
->List
= loopNode
->List
;
2487 * Determine if the given operation is of a specific type.
2490 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
2492 if (oper
->type
== type
)
2494 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
2495 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
2496 oper
->num_children
== 1)
2497 return is_operation_type(&oper
->children
[0], type
);
2504 * Generate IR tree for an if/then/else conditional using high-level
2505 * IR_IF instruction.
2507 static slang_ir_node
*
2508 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2511 * eval expr (child[0])
2518 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
2519 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
2520 GLboolean isConst
, constTrue
;
2522 /* type-check expression */
2523 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2524 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
2528 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2529 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
2533 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2537 return _slang_gen_operation(A
, &oper
->children
[1]);
2540 /* if (false) ... */
2541 return _slang_gen_operation(A
, &oper
->children
[2]);
2545 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2546 cond
= new_cond(cond
);
2548 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
2549 && !haveElseClause
) {
2550 /* Special case: generate a conditional break */
2551 ifBody
= new_break_if_true(A
->CurLoop
, cond
);
2554 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
2555 && !haveElseClause
) {
2556 /* Special case: generate a conditional break */
2557 ifBody
= new_cont_if_true(A
->CurLoop
, cond
);
2562 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
2564 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
2567 ifNode
= new_if(cond
, ifBody
, elseBody
);
2574 static slang_ir_node
*
2575 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2579 assert(oper
->type
== SLANG_OPER_NOT
);
2581 /* type-check expression */
2582 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2583 slang_info_log_error(A
->log
,
2584 "scalar/boolean expression expected for '!'");
2588 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2596 static slang_ir_node
*
2597 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2599 slang_ir_node
*n1
, *n2
;
2601 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
2603 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
2604 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2605 slang_info_log_error(A
->log
,
2606 "scalar/boolean expressions expected for '^^'");
2610 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
2613 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
2616 return new_node2(IR_NOTEQUAL
, n1
, n2
);
2621 * Generate IR node for storage of a temporary of given size.
2623 static slang_ir_node
*
2624 _slang_gen_temporary(GLint size
)
2626 slang_ir_storage
*store
;
2627 slang_ir_node
*n
= NULL
;
2629 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
2631 n
= new_node0(IR_VAR_DECL
);
2644 * Generate program constants for an array.
2645 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
2646 * This will allocate and initialize three vector constants, storing
2647 * the array in constant memory, not temporaries like a non-const array.
2648 * This can also be used for uniform array initializers.
2649 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
2652 make_constant_array(slang_assemble_ctx
*A
,
2653 slang_variable
*var
,
2654 slang_operation
*initializer
)
2656 struct gl_program
*prog
= A
->program
;
2657 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
2658 const char *varName
= (char *) var
->a_name
;
2659 const GLuint numElements
= initializer
->num_children
;
2665 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
2667 size
= var
->store
->Size
;
2669 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
2670 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
2671 assert(initializer
->type
== SLANG_OPER_CALL
);
2672 assert(initializer
->array_constructor
);
2674 values
= (GLfloat
*) _mesa_malloc(numElements
* 4 * sizeof(GLfloat
));
2676 /* convert constructor params into ordinary floats */
2677 for (i
= 0; i
< numElements
; i
++) {
2678 const slang_operation
*op
= &initializer
->children
[i
];
2679 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
2680 /* unsupported type for this optimization */
2684 for (j
= 0; j
< op
->literal_size
; j
++) {
2685 values
[i
* 4 + j
] = op
->literal
[j
];
2687 for ( ; j
< 4; j
++) {
2688 values
[i
* 4 + j
] = 0.0f
;
2692 /* slightly different paths for constants vs. uniforms */
2693 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2694 var
->store
->File
= PROGRAM_UNIFORM
;
2695 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
2696 size
, datatype
, values
);
2699 var
->store
->File
= PROGRAM_CONSTANT
;
2700 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
2703 assert(var
->store
->Size
== size
);
2713 * Generate IR node for allocating/declaring a variable (either a local or
2715 * Generally, this involves allocating slang_ir_storage for the variable,
2716 * choosing a register file (temporary, constant, etc). For ordinary
2717 * variables we do not yet allocate storage though. We do that when we
2718 * find the first actual use of the variable to avoid allocating temp regs
2719 * that will never get used.
2720 * At this time, uniforms are always allocated space in this function.
2722 * \param initializer Optional initializer expression for the variable.
2724 static slang_ir_node
*
2725 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
2726 slang_operation
*initializer
)
2728 const char *varName
= (const char *) var
->a_name
;
2729 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
2730 slang_ir_node
*varDecl
, *n
;
2731 slang_ir_storage
*store
;
2733 /*assert(!var->declared);*/
2734 var
->declared
= GL_TRUE
;
2736 varDecl
= new_node0(IR_VAR_DECL
);
2740 _slang_attach_storage(varDecl
, var
);
2742 assert(varDecl
->Store
== var
->store
);
2743 assert(varDecl
->Store
);
2744 assert(varDecl
->Store
->Index
< 0);
2747 assert(store
== varDecl
->Store
);
2749 /* determine GPU register file for simple cases */
2750 if (is_sampler_type(&var
->type
)) {
2751 store
->File
= PROGRAM_SAMPLER
;
2753 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2754 store
->File
= PROGRAM_UNIFORM
;
2757 store
->File
= PROGRAM_TEMPORARY
;
2760 store
->Size
= _slang_sizeof_type_specifier(&varDecl
->Var
->type
.specifier
);
2761 if (store
->Size
<= 0) {
2762 slang_info_log_error(A
->log
, "invalid declaration for '%s'",
2763 (char*) var
->a_name
);
2767 if (var
->type
.array_len
> 0) {
2768 /* the type is an array, ex: float[4] x; */
2769 GLint sz
= (store
->Size
+ 3) & ~3;
2770 /* total size = element size * array length */
2771 sz
*= var
->type
.array_len
;
2775 if (var
->array_len
> 0) {
2776 /* this is an array */
2777 /* round up the element size to a multiple of 4 */
2778 GLint sz
= (store
->Size
+ 3) & ~3;
2779 /* total size = element size * array length */
2780 sz
*= var
->array_len
;
2784 /* if there's an initializer, generate IR for the expression */
2786 slang_ir_node
*varRef
, *init
;
2788 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
2789 /* if the variable is const, the initializer must be a const
2790 * expression as well.
2793 if (!_slang_is_constant_expr(initializer
)) {
2794 slang_info_log_error(A
->log
,
2795 "initializer for %s not constant", varName
);
2801 /* IR for the variable we're initializing */
2802 varRef
= new_var(A
, var
);
2804 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
2808 /* constant-folding, etc here */
2809 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
2811 /* look for simple constant-valued variables and uniforms */
2812 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
2813 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2815 if (initializer
->type
== SLANG_OPER_CALL
&&
2816 initializer
->array_constructor
) {
2817 /* array initializer */
2818 make_constant_array(A
, var
, initializer
);
2821 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
2822 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
2823 /* simple float/vector initializer */
2824 if (store
->File
== PROGRAM_UNIFORM
) {
2825 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
2827 store
->Size
, datatype
,
2828 initializer
->literal
);
2829 /* XXX fix store->Swizzle here */
2834 store
->File
= PROGRAM_CONSTANT
;
2835 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
2837 initializer
->literal
,
2839 /* XXX fix swizzle here */
2846 /* IR for initializer */
2847 init
= _slang_gen_operation(A
, initializer
);
2851 /* XXX remove this when type checking is added above */
2852 if (init
->Store
&& varRef
->Store
->Size
!= init
->Store
->Size
) {
2853 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
2857 /* assign RHS to LHS */
2858 n
= new_node2(IR_COPY
, varRef
, init
);
2859 n
= new_seq(varDecl
, n
);
2862 /* no initializer */
2866 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
2868 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
2869 store
->Size
, datatype
, NULL
);
2870 store
->Swizzle
= _slang_var_swizzle(store
->Size
, 0);
2874 printf("%s var %p %s store=%p index=%d size=%d\n",
2875 __FUNCTION__
, (void *) var
, (char *) varName
,
2876 (void *) store
, store
->Index
, store
->Size
);
2884 * Generate code for a selection expression: b ? x : y
2885 * XXX In some cases we could implement a selection expression
2886 * with an LRP instruction (use the boolean as the interpolant).
2887 * Otherwise, we use an IF/ELSE/ENDIF construct.
2889 static slang_ir_node
*
2890 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
2892 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
2893 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
2894 slang_typeinfo type0
, type1
, type2
;
2895 int size
, isBool
, isEqual
;
2897 assert(oper
->type
== SLANG_OPER_SELECT
);
2898 assert(oper
->num_children
== 3);
2900 /* type of children[0] must be boolean */
2901 slang_typeinfo_construct(&type0
);
2902 typeof_operation(A
, &oper
->children
[0], &type0
);
2903 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
2904 slang_typeinfo_destruct(&type0
);
2906 slang_info_log_error(A
->log
, "selector type is not boolean");
2910 slang_typeinfo_construct(&type1
);
2911 slang_typeinfo_construct(&type2
);
2912 typeof_operation(A
, &oper
->children
[1], &type1
);
2913 typeof_operation(A
, &oper
->children
[2], &type2
);
2914 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
2915 slang_typeinfo_destruct(&type1
);
2916 slang_typeinfo_destruct(&type2
);
2918 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
2922 /* size of x or y's type */
2923 size
= _slang_sizeof_type_specifier(&type1
.spec
);
2927 tmpDecl
= _slang_gen_temporary(size
);
2929 /* the condition (child 0) */
2930 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2931 cond
= new_cond(cond
);
2933 /* if-true body (child 1) */
2934 tmpVar
= new_node0(IR_VAR
);
2935 tmpVar
->Store
= tmpDecl
->Store
;
2936 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
2937 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
2939 /* if-false body (child 2) */
2940 tmpVar
= new_node0(IR_VAR
);
2941 tmpVar
->Store
= tmpDecl
->Store
;
2942 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
2943 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
2945 ifNode
= new_if(cond
, trueNode
, falseNode
);
2948 tmpVar
= new_node0(IR_VAR
);
2949 tmpVar
->Store
= tmpDecl
->Store
;
2951 tree
= new_seq(ifNode
, tmpVar
);
2952 tree
= new_seq(tmpDecl
, tree
);
2954 /*_slang_print_ir_tree(tree, 10);*/
2960 * Generate code for &&.
2962 static slang_ir_node
*
2963 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
2965 /* rewrite "a && b" as "a ? b : false" */
2966 slang_operation
*select
;
2969 select
= slang_operation_new(1);
2970 select
->type
= SLANG_OPER_SELECT
;
2971 select
->num_children
= 3;
2972 select
->children
= slang_operation_new(3);
2974 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
2975 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
2976 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
2977 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
2978 select
->children
[2].literal_size
= 1;
2980 n
= _slang_gen_select(A
, select
);
2986 * Generate code for ||.
2988 static slang_ir_node
*
2989 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
2991 /* rewrite "a || b" as "a ? true : b" */
2992 slang_operation
*select
;
2995 select
= slang_operation_new(1);
2996 select
->type
= SLANG_OPER_SELECT
;
2997 select
->num_children
= 3;
2998 select
->children
= slang_operation_new(3);
3000 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3001 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
3002 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
3003 select
->children
[1].literal_size
= 1;
3004 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
3006 n
= _slang_gen_select(A
, select
);
3012 * Generate IR tree for a return statement.
3014 static slang_ir_node
*
3015 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
3017 const GLboolean haveReturnValue
3018 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
3020 /* error checking */
3021 assert(A
->CurFunction
);
3022 if (haveReturnValue
&&
3023 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
3024 slang_info_log_error(A
->log
, "illegal return expression");
3027 else if (!haveReturnValue
&&
3028 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
3029 slang_info_log_error(A
->log
, "return statement requires an expression");
3033 if (!haveReturnValue
) {
3034 return new_return(A
->curFuncEndLabel
);
3042 * return; // goto __endOfFunction
3044 slang_operation
*assign
;
3045 slang_atom a_retVal
;
3048 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
3054 _slang_variable_locate(oper
->locals
, a_retVal
, GL_TRUE
);
3056 /* trying to return a value in a void-valued function */
3062 assign
= slang_operation_new(1);
3063 assign
->type
= SLANG_OPER_ASSIGN
;
3064 assign
->num_children
= 2;
3065 assign
->children
= slang_operation_new(2);
3066 /* lhs (__retVal) */
3067 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
3068 assign
->children
[0].a_id
= a_retVal
;
3069 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
3071 /* XXX we might be able to avoid this copy someday */
3072 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
3074 /* assemble the new code */
3075 n
= new_seq(_slang_gen_operation(A
, assign
),
3076 new_return(A
->curFuncEndLabel
));
3078 slang_operation_delete(assign
);
3085 * Determine if the given operation/expression is const-valued.
3088 _slang_is_constant_expr(const slang_operation
*oper
)
3090 slang_variable
*var
;
3093 switch (oper
->type
) {
3094 case SLANG_OPER_IDENTIFIER
:
3095 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3096 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
3100 for (i
= 0; i
< oper
->num_children
; i
++) {
3101 if (!_slang_is_constant_expr(&oper
->children
[i
]))
3110 * Check if an assignment of type t1 to t0 is legal.
3111 * XXX more cases needed.
3114 _slang_assignment_compatible(slang_assemble_ctx
*A
,
3115 slang_operation
*op0
,
3116 slang_operation
*op1
)
3118 slang_typeinfo t0
, t1
;
3121 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
3122 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
3126 slang_typeinfo_construct(&t0
);
3127 typeof_operation(A
, op0
, &t0
);
3129 slang_typeinfo_construct(&t1
);
3130 typeof_operation(A
, op1
, &t1
);
3132 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
3133 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
3137 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
3142 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
3143 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
3144 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
3147 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
3148 t1
.spec
.type
== SLANG_SPEC_BOOL
)
3151 #if 0 /* not used just yet - causes problems elsewhere */
3152 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
3153 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3157 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3158 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3161 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3162 t1
.spec
.type
== SLANG_SPEC_INT
)
3171 * Generate IR tree for a local variable declaration.
3173 static slang_ir_node
*
3174 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
3176 const char *varName
= (char *) oper
->a_id
;
3177 slang_variable
*var
;
3178 slang_ir_node
*varDecl
;
3179 slang_operation
*initializer
;
3181 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
3182 assert(oper
->num_children
<= 1);
3184 /* lookup the variable by name */
3185 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3187 return NULL
; /* "shouldn't happen" */
3189 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3190 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
3191 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3192 /* can't declare attribute/uniform vars inside functions */
3193 slang_info_log_error(A
->log
,
3194 "local variable '%s' cannot be an attribute/uniform/varying",
3201 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
3206 /* check if the var has an initializer */
3207 if (oper
->num_children
> 0) {
3208 assert(oper
->num_children
== 1);
3209 initializer
= &oper
->children
[0];
3211 else if (var
->initializer
) {
3212 initializer
= var
->initializer
;
3219 /* check/compare var type and initializer type */
3220 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
3221 slang_info_log_error(A
->log
, "incompatible types in assignment");
3226 /* Generate IR node */
3227 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
3231 if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !initializer
) {
3232 slang_info_log_error(A
->log
,
3233 "const-qualified variable '%s' requires initializer",
3243 * Generate IR tree for a variable (such as in an expression).
3245 static slang_ir_node
*
3246 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
3248 /* If there's a variable associated with this oper (from inlining)
3249 * use it. Otherwise, use the oper's var id.
3251 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
3252 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
3253 slang_ir_node
*n
= new_var(A
, var
);
3255 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
3264 * Return the number of components actually named by the swizzle.
3265 * Recall that swizzles may have undefined/don't-care values.
3268 swizzle_size(GLuint swizzle
)
3271 for (i
= 0; i
< 4; i
++) {
3272 GLuint swz
= GET_SWZ(swizzle
, i
);
3273 size
+= (swz
>= 0 && swz
<= 3);
3279 static slang_ir_node
*
3280 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
3282 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
3286 n
->Store
= _slang_new_ir_storage_relative(0,
3287 swizzle_size(swizzle
),
3289 n
->Store
->Swizzle
= swizzle
;
3296 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
3298 while (store
->Parent
)
3299 store
= store
->Parent
;
3301 if (!(store
->File
== PROGRAM_OUTPUT
||
3302 store
->File
== PROGRAM_TEMPORARY
||
3303 (store
->File
== PROGRAM_VARYING
&&
3304 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
3314 * Generate IR tree for an assignment (=).
3316 static slang_ir_node
*
3317 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
3319 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
3320 /* Check that var is writeable */
3322 = _slang_variable_locate(oper
->children
[0].locals
,
3323 oper
->children
[0].a_id
, GL_TRUE
);
3325 slang_info_log_error(A
->log
, "undefined variable '%s'",
3326 (char *) oper
->children
[0].a_id
);
3329 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3330 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3331 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
3332 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
3333 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
3334 slang_info_log_error(A
->log
,
3335 "illegal assignment to read-only variable '%s'",
3336 (char *) oper
->children
[0].a_id
);
3341 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
3342 oper
->children
[1].type
== SLANG_OPER_CALL
) {
3343 /* Special case of: x = f(a, b)
3344 * Replace with f(a, b, x) (where x == hidden __retVal out param)
3346 * XXX this could be even more effective if we could accomodate
3347 * cases such as "v.x = f();" - would help with typical vertex
3351 n
= _slang_gen_function_call_name(A
,
3352 (const char *) oper
->children
[1].a_id
,
3353 &oper
->children
[1], &oper
->children
[0]);
3357 slang_ir_node
*n
, *lhs
, *rhs
;
3359 /* lhs and rhs type checking */
3360 if (!_slang_assignment_compatible(A
,
3362 &oper
->children
[1])) {
3363 slang_info_log_error(A
->log
, "incompatible types in assignment");
3367 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
3373 slang_info_log_error(A
->log
,
3374 "invalid left hand side for assignment");
3378 /* check that lhs is writable */
3379 if (!is_store_writable(A
, lhs
->Store
)) {
3380 slang_info_log_error(A
->log
,
3381 "illegal assignment to read-only l-value");
3385 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
3387 /* convert lhs swizzle into writemask */
3388 GLuint writemask
, newSwizzle
;
3389 if (!swizzle_to_writemask(A
, lhs
->Store
->Swizzle
,
3390 &writemask
, &newSwizzle
)) {
3391 /* Non-simple writemask, need to swizzle right hand side in
3392 * order to put components into the right place.
3394 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
3396 n
= new_node2(IR_COPY
, lhs
, rhs
);
3407 * Generate IR tree for referencing a field in a struct (or basic vector type)
3409 static slang_ir_node
*
3410 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
3414 /* type of struct */
3415 slang_typeinfo_construct(&ti
);
3416 typeof_operation(A
, &oper
->children
[0], &ti
);
3418 if (_slang_type_is_vector(ti
.spec
.type
)) {
3419 /* the field should be a swizzle */
3420 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
3424 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3425 slang_info_log_error(A
->log
, "Bad swizzle");
3428 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3433 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3434 /* create new parent node with swizzle */
3436 n
= _slang_gen_swizzle(n
, swizzle
);
3439 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
3440 || ti
.spec
.type
== SLANG_SPEC_INT
3441 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
3442 const GLuint rows
= 1;
3446 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3447 slang_info_log_error(A
->log
, "Bad swizzle");
3449 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3453 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3454 /* create new parent node with swizzle */
3455 n
= _slang_gen_swizzle(n
, swizzle
);
3459 /* the field is a structure member (base.field) */
3460 /* oper->children[0] is the base */
3461 /* oper->a_id is the field name */
3462 slang_ir_node
*base
, *n
;
3463 slang_typeinfo field_ti
;
3464 GLint fieldSize
, fieldOffset
= -1;
3467 slang_typeinfo_construct(&field_ti
);
3468 typeof_operation(A
, oper
, &field_ti
);
3470 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
3472 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
3474 if (fieldSize
== 0 || fieldOffset
< 0) {
3475 const char *structName
;
3476 if (ti
.spec
._struct
)
3477 structName
= (char *) ti
.spec
._struct
->a_name
;
3479 structName
= "unknown";
3480 slang_info_log_error(A
->log
,
3481 "\"%s\" is not a member of struct \"%s\"",
3482 (char *) oper
->a_id
, structName
);
3485 assert(fieldSize
>= 0);
3487 base
= _slang_gen_operation(A
, &oper
->children
[0]);
3489 /* error msg should have already been logged */
3493 n
= new_node1(IR_FIELD
, base
);
3497 n
->Field
= (char *) oper
->a_id
;
3499 /* Store the field's offset in storage->Index */
3500 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
3510 * Gen code for array indexing.
3512 static slang_ir_node
*
3513 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
3515 slang_typeinfo array_ti
;
3517 /* get array's type info */
3518 slang_typeinfo_construct(&array_ti
);
3519 typeof_operation(A
, &oper
->children
[0], &array_ti
);
3521 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
3522 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
3523 /* translate the index into a swizzle/writemask: "v.x=p" */
3524 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
3528 index
= (GLint
) oper
->children
[1].literal
[0];
3529 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
3530 index
>= (GLint
) max
) {
3531 slang_info_log_error(A
->log
, "Invalid array index for vector type");
3535 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3537 /* use swizzle to access the element */
3538 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
3542 n
= _slang_gen_swizzle(n
, swizzle
);
3548 /* conventional array */
3549 slang_typeinfo elem_ti
;
3550 slang_ir_node
*elem
, *array
, *index
;
3551 GLint elemSize
, arrayLen
;
3553 /* size of array element */
3554 slang_typeinfo_construct(&elem_ti
);
3555 typeof_operation(A
, oper
, &elem_ti
);
3556 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
3558 if (_slang_type_is_matrix(array_ti
.spec
.type
))
3559 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
3561 arrayLen
= array_ti
.array_len
;
3563 slang_typeinfo_destruct(&array_ti
);
3564 slang_typeinfo_destruct(&elem_ti
);
3566 if (elemSize
<= 0) {
3567 /* unknown var or type */
3568 slang_info_log_error(A
->log
, "Undefined variable or type");
3572 array
= _slang_gen_operation(A
, &oper
->children
[0]);
3573 index
= _slang_gen_operation(A
, &oper
->children
[1]);
3574 if (array
&& index
) {
3576 GLint constIndex
= -1;
3577 if (index
->Opcode
== IR_FLOAT
) {
3578 constIndex
= (int) index
->Value
[0];
3579 if (constIndex
< 0 || constIndex
>= arrayLen
) {
3580 slang_info_log_error(A
->log
,
3581 "Array index out of bounds (index=%d size=%d)",
3582 constIndex
, arrayLen
);
3583 _slang_free_ir_tree(array
);
3584 _slang_free_ir_tree(index
);
3589 if (!array
->Store
) {
3590 slang_info_log_error(A
->log
, "Invalid array");
3594 elem
= new_node2(IR_ELEMENT
, array
, index
);
3596 /* The storage info here will be updated during code emit */
3597 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
3598 array
->Store
->Index
,
3604 _slang_free_ir_tree(array
);
3605 _slang_free_ir_tree(index
);
3612 static slang_ir_node
*
3613 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
3614 slang_ir_opcode opcode
)
3616 slang_typeinfo t0
, t1
;
3619 slang_typeinfo_construct(&t0
);
3620 typeof_operation(A
, &oper
->children
[0], &t0
);
3622 slang_typeinfo_construct(&t1
);
3623 typeof_operation(A
, &oper
->children
[0], &t1
);
3625 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
3626 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
3627 slang_info_log_error(A
->log
, "Illegal array comparison");
3631 if (oper
->type
!= SLANG_OPER_EQUAL
&&
3632 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
3633 /* <, <=, >, >= can only be used with scalars */
3634 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
3635 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
3636 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
3637 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
3638 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
3643 n
= new_node2(opcode
,
3644 _slang_gen_operation(A
, &oper
->children
[0]),
3645 _slang_gen_operation(A
, &oper
->children
[1]));
3647 /* result is a bool (size 1) */
3648 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
3656 print_vars(slang_variable_scope
*s
)
3660 for (i
= 0; i
< s
->num_variables
; i
++) {
3662 (char*) s
->variables
[i
]->a_name
,
3663 s
->variables
[i
]->declared
);
3673 _slang_undeclare_vars(slang_variable_scope
*locals
)
3675 if (locals
->num_variables
> 0) {
3677 for (i
= 0; i
< locals
->num_variables
; i
++) {
3678 slang_variable
*v
= locals
->variables
[i
];
3679 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
3680 v
->declared
= GL_FALSE
;
3688 * Generate IR tree for a slang_operation (AST node)
3690 static slang_ir_node
*
3691 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
3693 switch (oper
->type
) {
3694 case SLANG_OPER_BLOCK_NEW_SCOPE
:
3698 _slang_push_var_table(A
->vartable
);
3700 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
3701 n
= _slang_gen_operation(A
, oper
);
3702 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
3704 _slang_pop_var_table(A
->vartable
);
3706 /*_slang_undeclare_vars(oper->locals);*/
3707 /*print_vars(oper->locals);*/
3710 n
= new_node1(IR_SCOPE
, n
);
3715 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
3716 /* list of operations */
3717 if (oper
->num_children
> 0)
3719 slang_ir_node
*n
, *tree
= NULL
;
3722 for (i
= 0; i
< oper
->num_children
; i
++) {
3723 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
3725 _slang_free_ir_tree(tree
);
3726 return NULL
; /* error must have occured */
3728 tree
= new_seq(tree
, n
);
3734 return new_node0(IR_NOP
);
3737 case SLANG_OPER_EXPRESSION
:
3738 return _slang_gen_operation(A
, &oper
->children
[0]);
3740 case SLANG_OPER_FOR
:
3741 return _slang_gen_for(A
, oper
);
3743 return _slang_gen_do(A
, oper
);
3744 case SLANG_OPER_WHILE
:
3745 return _slang_gen_while(A
, oper
);
3746 case SLANG_OPER_BREAK
:
3748 slang_info_log_error(A
->log
, "'break' not in loop");
3751 return new_break(A
->CurLoop
);
3752 case SLANG_OPER_CONTINUE
:
3754 slang_info_log_error(A
->log
, "'continue' not in loop");
3757 return _slang_gen_continue(A
, oper
);
3758 case SLANG_OPER_DISCARD
:
3759 return new_node0(IR_KILL
);
3761 case SLANG_OPER_EQUAL
:
3762 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
3763 case SLANG_OPER_NOTEQUAL
:
3764 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
3765 case SLANG_OPER_GREATER
:
3766 return _slang_gen_compare(A
, oper
, IR_SGT
);
3767 case SLANG_OPER_LESS
:
3768 return _slang_gen_compare(A
, oper
, IR_SLT
);
3769 case SLANG_OPER_GREATEREQUAL
:
3770 return _slang_gen_compare(A
, oper
, IR_SGE
);
3771 case SLANG_OPER_LESSEQUAL
:
3772 return _slang_gen_compare(A
, oper
, IR_SLE
);
3773 case SLANG_OPER_ADD
:
3776 assert(oper
->num_children
== 2);
3777 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
3780 case SLANG_OPER_SUBTRACT
:
3783 assert(oper
->num_children
== 2);
3784 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
3787 case SLANG_OPER_MULTIPLY
:
3790 assert(oper
->num_children
== 2);
3791 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
3794 case SLANG_OPER_DIVIDE
:
3797 assert(oper
->num_children
== 2);
3798 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
3801 case SLANG_OPER_MINUS
:
3804 assert(oper
->num_children
== 1);
3805 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
3808 case SLANG_OPER_PLUS
:
3809 /* +expr --> do nothing */
3810 return _slang_gen_operation(A
, &oper
->children
[0]);
3811 case SLANG_OPER_VARIABLE_DECL
:
3812 return _slang_gen_declaration(A
, oper
);
3813 case SLANG_OPER_ASSIGN
:
3814 return _slang_gen_assignment(A
, oper
);
3815 case SLANG_OPER_ADDASSIGN
:
3818 assert(oper
->num_children
== 2);
3819 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
3822 case SLANG_OPER_SUBASSIGN
:
3825 assert(oper
->num_children
== 2);
3826 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
3830 case SLANG_OPER_MULASSIGN
:
3833 assert(oper
->num_children
== 2);
3834 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
3837 case SLANG_OPER_DIVASSIGN
:
3840 assert(oper
->num_children
== 2);
3841 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
3844 case SLANG_OPER_LOGICALAND
:
3847 assert(oper
->num_children
== 2);
3848 n
= _slang_gen_logical_and(A
, oper
);
3851 case SLANG_OPER_LOGICALOR
:
3854 assert(oper
->num_children
== 2);
3855 n
= _slang_gen_logical_or(A
, oper
);
3858 case SLANG_OPER_LOGICALXOR
:
3859 return _slang_gen_xor(A
, oper
);
3860 case SLANG_OPER_NOT
:
3861 return _slang_gen_not(A
, oper
);
3862 case SLANG_OPER_SELECT
: /* b ? x : y */
3865 assert(oper
->num_children
== 3);
3866 n
= _slang_gen_select(A
, oper
);
3870 case SLANG_OPER_ASM
:
3871 return _slang_gen_asm(A
, oper
, NULL
);
3872 case SLANG_OPER_CALL
:
3873 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
3875 case SLANG_OPER_METHOD
:
3876 return _slang_gen_method_call(A
, oper
);
3877 case SLANG_OPER_RETURN
:
3878 return _slang_gen_return(A
, oper
);
3879 case SLANG_OPER_LABEL
:
3880 return new_label(oper
->label
);
3881 case SLANG_OPER_IDENTIFIER
:
3882 return _slang_gen_variable(A
, oper
);
3884 return _slang_gen_if(A
, oper
);
3885 case SLANG_OPER_FIELD
:
3886 return _slang_gen_struct_field(A
, oper
);
3887 case SLANG_OPER_SUBSCRIPT
:
3888 return _slang_gen_array_element(A
, oper
);
3889 case SLANG_OPER_LITERAL_FLOAT
:
3891 case SLANG_OPER_LITERAL_INT
:
3893 case SLANG_OPER_LITERAL_BOOL
:
3894 return new_float_literal(oper
->literal
, oper
->literal_size
);
3896 case SLANG_OPER_POSTINCREMENT
: /* var++ */
3899 assert(oper
->num_children
== 1);
3900 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
3903 case SLANG_OPER_POSTDECREMENT
: /* var-- */
3906 assert(oper
->num_children
== 1);
3907 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
3910 case SLANG_OPER_PREINCREMENT
: /* ++var */
3913 assert(oper
->num_children
== 1);
3914 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
3917 case SLANG_OPER_PREDECREMENT
: /* --var */
3920 assert(oper
->num_children
== 1);
3921 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
3925 case SLANG_OPER_NON_INLINED_CALL
:
3926 case SLANG_OPER_SEQUENCE
:
3928 slang_ir_node
*tree
= NULL
;
3930 for (i
= 0; i
< oper
->num_children
; i
++) {
3931 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
3932 tree
= new_seq(tree
, n
);
3934 tree
->Store
= n
->Store
;
3936 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
3937 tree
= new_function_call(tree
, oper
->label
);
3942 case SLANG_OPER_NONE
:
3943 case SLANG_OPER_VOID
:
3944 /* returning NULL here would generate an error */
3945 return new_node0(IR_NOP
);
3948 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
3950 return new_node0(IR_NOP
);
3958 * Compute total size of array give size of element, number of elements.
3961 array_size(GLint baseSize
, GLint arrayLen
)
3965 /* round up base type to multiple of 4 */
3966 total
= ((baseSize
+ 3) & ~0x3) * MAX2(arrayLen
, 1);
3976 * Called by compiler when a global variable has been parsed/compiled.
3977 * Here we examine the variable's type to determine what kind of register
3978 * storage will be used.
3980 * A uniform such as "gl_Position" will become the register specification
3981 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
3982 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
3984 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
3985 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
3986 * actual texture unit (as specified by the user calling glUniform1i()).
3989 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
3990 slang_unit_type type
)
3992 struct gl_program
*prog
= A
->program
;
3993 const char *varName
= (char *) var
->a_name
;
3994 GLboolean success
= GL_TRUE
;
3995 slang_ir_storage
*store
= NULL
;
3997 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3998 const GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
3999 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
4001 if (texIndex
!= -1) {
4002 /* This is a texture sampler variable...
4003 * store->File = PROGRAM_SAMPLER
4004 * store->Index = sampler number (0..7, typically)
4005 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
4007 if (var
->initializer
) {
4008 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
4011 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
4012 /* disallow rect samplers */
4013 if (var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECT
||
4014 var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
) {
4015 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
4020 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
4021 store
= _slang_new_ir_storage(PROGRAM_SAMPLER
, sampNum
, texIndex
);
4023 if (dbg
) printf("SAMPLER ");
4025 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4026 /* Uniform variable */
4027 const GLint totalSize
= array_size(size
, var
->array_len
);
4028 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
4031 /* user-defined uniform */
4032 if (datatype
== GL_NONE
) {
4033 if (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
) {
4034 /* temporary work-around */
4035 GLenum datatype
= GL_FLOAT
;
4036 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
4037 totalSize
, datatype
, NULL
);
4038 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
4039 totalSize
, swizzle
);
4041 /* XXX what we need to do is unroll the struct into its
4042 * basic types, creating a uniform variable for each.
4050 * Should produce uniforms:
4051 * "f.a" (GL_FLOAT_VEC3)
4052 * "f.b" (GL_FLOAT_VEC4)
4055 if (var
->initializer
) {
4056 slang_info_log_error(A
->log
,
4057 "unsupported initializer for uniform '%s'", varName
);
4062 slang_info_log_error(A
->log
,
4063 "invalid datatype for uniform variable %s",
4069 /* non-struct uniform */
4070 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
4076 /* pre-defined uniform, like gl_ModelviewMatrix */
4077 /* We know it's a uniform, but don't allocate storage unless
4080 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
4081 totalSize
, swizzle
);
4083 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
4085 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
4086 const GLint totalSize
= array_size(size
, var
->array_len
);
4088 /* varyings must be float, vec or mat */
4089 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
4090 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
4091 slang_info_log_error(A
->log
,
4092 "varying '%s' must be float/vector/matrix",
4097 if (var
->initializer
) {
4098 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
4104 /* user-defined varying */
4110 if (var
->type
.centroid
== SLANG_CENTROID
)
4111 flags
|= PROG_PARAM_BIT_CENTROID
;
4112 if (var
->type
.variant
== SLANG_INVARIANT
)
4113 flags
|= PROG_PARAM_BIT_INVARIANT
;
4115 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
4117 swizzle
= _slang_var_swizzle(size
, 0);
4118 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
4119 totalSize
, swizzle
);
4122 /* pre-defined varying, like gl_Color or gl_TexCoord */
4123 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
4124 /* fragment program input */
4126 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4129 assert(index
< FRAG_ATTRIB_MAX
);
4130 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
4134 /* vertex program output */
4135 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4136 GLuint swizzle
= _slang_var_swizzle(size
, 0);
4138 assert(index
< VERT_RESULT_MAX
);
4139 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
4140 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
4143 if (dbg
) printf("V/F ");
4145 if (dbg
) printf("VARYING ");
4147 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
4150 /* attributes must be float, vec or mat */
4151 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
4152 slang_info_log_error(A
->log
,
4153 "attribute '%s' must be float/vector/matrix",
4159 /* user-defined vertex attribute */
4160 const GLint attr
= -1; /* unknown */
4161 swizzle
= _slang_var_swizzle(size
, 0);
4162 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
4163 size
, datatype
, attr
);
4165 index
= VERT_ATTRIB_GENERIC0
+ index
;
4168 /* pre-defined vertex attrib */
4169 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
4172 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4173 if (dbg
) printf("ATTRIB ");
4175 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
4176 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
4177 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4179 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4180 if (dbg
) printf("INPUT ");
4182 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
4183 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
4184 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4185 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
4188 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
4189 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
4190 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
4191 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
4193 if (dbg
) printf("OUTPUT ");
4195 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
4196 /* pre-defined global constant, like gl_MaxLights */
4197 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
4198 if (dbg
) printf("CONST ");
4201 /* ordinary variable (may be const) */
4204 /* IR node to declare the variable */
4205 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
4207 /* emit GPU instructions */
4208 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_FALSE
, A
->log
);
4210 _slang_free_ir_tree(n
);
4213 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
4214 store
? store
->Index
: -2);
4217 var
->store
= store
; /* save var's storage info */
4219 var
->declared
= GL_TRUE
;
4226 * Produce an IR tree from a function AST (fun->body).
4227 * Then call the code emitter to convert the IR tree into gl_program
4231 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
4234 GLboolean success
= GL_TRUE
;
4236 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
4237 /* we only really generate code for main, all other functions get
4238 * inlined or codegen'd upon an actual call.
4241 /* do some basic error checking though */
4242 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
4243 /* check that non-void functions actually return something */
4245 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
4247 slang_info_log_error(A
->log
,
4248 "function \"%s\" has no return statement",
4249 (char *) fun
->header
.a_name
);
4251 "function \"%s\" has no return statement\n",
4252 (char *) fun
->header
.a_name
);
4257 return GL_TRUE
; /* not an error */
4261 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
4262 slang_print_function(fun
, 1);
4265 /* should have been allocated earlier: */
4266 assert(A
->program
->Parameters
);
4267 assert(A
->program
->Varying
);
4268 assert(A
->vartable
);
4270 A
->CurFunction
= fun
;
4272 /* fold constant expressions, etc. */
4273 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
4276 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
4277 slang_print_function(fun
, 1);
4280 /* Create an end-of-function label */
4281 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
4283 /* push new vartable scope */
4284 _slang_push_var_table(A
->vartable
);
4286 /* Generate IR tree for the function body code */
4287 n
= _slang_gen_operation(A
, fun
->body
);
4289 n
= new_node1(IR_SCOPE
, n
);
4291 /* pop vartable, restore previous */
4292 _slang_pop_var_table(A
->vartable
);
4295 /* XXX record error */
4299 /* append an end-of-function-label to IR tree */
4300 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
4302 /*_slang_label_delete(A->curFuncEndLabel);*/
4303 A
->curFuncEndLabel
= NULL
;
4306 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
4307 slang_print_function(fun
, 1);
4310 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
4311 _slang_print_ir_tree(n
, 0);
4314 printf("************* End codegen function ************\n\n");
4317 /* Emit program instructions */
4318 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
, A
->log
);
4319 _slang_free_ir_tree(n
);
4321 /* free codegen context */
4323 _mesa_free(A->codegen);