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
)
672 slang_ir_node
*n
= new_node0(IR_VAR
);
674 _slang_attach_storage(n
, var
);
681 * Check if the given function is really just a wrapper for a
682 * basic assembly instruction.
685 slang_is_asm_function(const slang_function
*fun
)
687 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
688 fun
->body
->num_children
== 1 &&
689 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
697 _slang_is_noop(const slang_operation
*oper
)
700 oper
->type
== SLANG_OPER_VOID
||
701 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
709 * Recursively search tree for a node of the given type.
711 static slang_operation
*
712 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
715 if (oper
->type
== type
)
717 for (i
= 0; i
< oper
->num_children
; i
++) {
718 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
727 * Count the number of operations of the given time rooted at 'oper'.
730 _slang_count_node_type(slang_operation
*oper
, slang_operation_type type
)
733 if (oper
->type
== type
) {
736 for (i
= 0; i
< oper
->num_children
; i
++) {
737 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
744 * Check if the 'return' statement found under 'oper' is a "tail return"
745 * that can be no-op'd. For example:
750 * return; // this is a no-op
753 * This is used when determining if a function can be inlined. If the
754 * 'return' is not the last statement, we can't inline the function since
755 * we still need the semantic behaviour of the 'return' but we don't want
756 * to accidentally return from the _calling_ function. We'd need to use an
757 * unconditional branch, but we don't have such a GPU instruction (not
761 _slang_is_tail_return(const slang_operation
*oper
)
763 GLuint k
= oper
->num_children
;
766 const slang_operation
*last
= &oper
->children
[k
- 1];
767 if (last
->type
== SLANG_OPER_RETURN
)
769 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
770 last
->type
== SLANG_OPER_LABEL
)
771 k
--; /* try prev child */
772 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
773 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
774 /* try sub-children */
775 return _slang_is_tail_return(last
);
785 slang_resolve_variable(slang_operation
*oper
)
787 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
788 oper
->var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
794 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
797 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
798 GLuint substCount
, slang_variable
**substOld
,
799 slang_operation
**substNew
, GLboolean isLHS
)
801 switch (oper
->type
) {
802 case SLANG_OPER_VARIABLE_DECL
:
804 slang_variable
*v
= _slang_variable_locate(oper
->locals
,
805 oper
->a_id
, GL_TRUE
);
807 if (v
->initializer
&& oper
->num_children
== 0) {
808 /* set child of oper to copy of initializer */
809 oper
->num_children
= 1;
810 oper
->children
= slang_operation_new(1);
811 slang_operation_copy(&oper
->children
[0], v
->initializer
);
813 if (oper
->num_children
== 1) {
814 /* the initializer */
815 slang_substitute(A
, &oper
->children
[0], substCount
,
816 substOld
, substNew
, GL_FALSE
);
820 case SLANG_OPER_IDENTIFIER
:
821 assert(oper
->num_children
== 0);
822 if (1/**!isLHS XXX FIX */) {
823 slang_atom id
= oper
->a_id
;
826 v
= _slang_variable_locate(oper
->locals
, id
, GL_TRUE
);
828 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
832 /* look for a substitution */
833 for (i
= 0; i
< substCount
; i
++) {
834 if (v
== substOld
[i
]) {
835 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
836 #if 0 /* DEBUG only */
837 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
838 assert(substNew
[i
]->var
);
839 assert(substNew
[i
]->var
->a_name
);
840 printf("Substitute %s with %s in id node %p\n",
841 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
845 printf("Substitute %s with %f in id node %p\n",
846 (char*)v
->a_name
, substNew
[i
]->literal
[0],
850 slang_operation_copy(oper
, substNew
[i
]);
857 case SLANG_OPER_RETURN
:
858 /* do return replacement here too */
859 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
860 if (oper
->num_children
== 1 && !_slang_is_noop(&oper
->children
[0])) {
866 * then do substitutions on the assignment.
868 slang_operation
*blockOper
, *assignOper
, *returnOper
;
870 /* check if function actually has a return type */
871 assert(A
->CurFunction
);
872 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
873 slang_info_log_error(A
->log
, "illegal return expression");
877 blockOper
= slang_operation_new(1);
878 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
879 blockOper
->num_children
= 2;
880 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
881 blockOper
->children
= slang_operation_new(2);
882 assignOper
= blockOper
->children
+ 0;
883 returnOper
= blockOper
->children
+ 1;
885 assignOper
->type
= SLANG_OPER_ASSIGN
;
886 assignOper
->num_children
= 2;
887 assignOper
->locals
->outer_scope
= blockOper
->locals
;
888 assignOper
->children
= slang_operation_new(2);
889 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
890 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
891 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
893 slang_operation_copy(&assignOper
->children
[1],
896 returnOper
->type
= SLANG_OPER_RETURN
; /* return w/ no value */
897 assert(returnOper
->num_children
== 0);
899 /* do substitutions on the "__retVal = expr" sub-tree */
900 slang_substitute(A
, assignOper
,
901 substCount
, substOld
, substNew
, GL_FALSE
);
903 /* install new code */
904 slang_operation_copy(oper
, blockOper
);
905 slang_operation_destruct(blockOper
);
908 /* check if return value was expected */
909 assert(A
->CurFunction
);
910 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
911 slang_info_log_error(A
->log
, "return statement requires an expression");
917 case SLANG_OPER_ASSIGN
:
918 case SLANG_OPER_SUBSCRIPT
:
920 * child[0] can't have substitutions but child[1] can.
922 slang_substitute(A
, &oper
->children
[0],
923 substCount
, substOld
, substNew
, GL_TRUE
);
924 slang_substitute(A
, &oper
->children
[1],
925 substCount
, substOld
, substNew
, GL_FALSE
);
927 case SLANG_OPER_FIELD
:
929 slang_substitute(A
, &oper
->children
[0],
930 substCount
, substOld
, substNew
, GL_TRUE
);
935 for (i
= 0; i
< oper
->num_children
; i
++)
936 slang_substitute(A
, &oper
->children
[i
],
937 substCount
, substOld
, substNew
, GL_FALSE
);
944 * Produce inline code for a call to an assembly instruction.
945 * This is typically used to compile a call to a built-in function like this:
947 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
949 * __asm vec4_lrp __retVal, a, y, x;
954 * r = mix(p1, p2, p3);
964 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
966 static slang_operation
*
967 slang_inline_asm_function(slang_assemble_ctx
*A
,
968 slang_function
*fun
, slang_operation
*oper
)
970 const GLuint numArgs
= oper
->num_children
;
972 slang_operation
*inlined
;
973 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
974 slang_variable
**substOld
;
975 slang_operation
**substNew
;
977 ASSERT(slang_is_asm_function(fun
));
978 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
981 printf("Inline %s as %s\n",
982 (char*) fun->header.a_name,
983 (char*) fun->body->children[0].a_id);
987 * We'll substitute formal params with actual args in the asm call.
989 substOld
= (slang_variable
**)
990 _slang_alloc(numArgs
* sizeof(slang_variable
*));
991 substNew
= (slang_operation
**)
992 _slang_alloc(numArgs
* sizeof(slang_operation
*));
993 for (i
= 0; i
< numArgs
; i
++) {
994 substOld
[i
] = fun
->parameters
->variables
[i
];
995 substNew
[i
] = oper
->children
+ i
;
998 /* make a copy of the code to inline */
999 inlined
= slang_operation_new(1);
1000 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
1002 /* get rid of the __retVal child */
1003 inlined
->num_children
--;
1004 for (i
= 0; i
< inlined
->num_children
; i
++) {
1005 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1009 /* now do formal->actual substitutions */
1010 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1012 _slang_free(substOld
);
1013 _slang_free(substNew
);
1016 printf("+++++++++++++ inlined asm function %s +++++++++++++\n",
1017 (char *) fun
->header
.a_name
);
1018 slang_print_tree(inlined
, 3);
1019 printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
1027 * Inline the given function call operation.
1028 * Return a new slang_operation that corresponds to the inlined code.
1030 static slang_operation
*
1031 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1032 slang_operation
*oper
, slang_operation
*returnOper
)
1039 ParamMode
*paramMode
;
1040 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1041 const GLuint numArgs
= oper
->num_children
;
1042 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1043 slang_operation
*args
= oper
->children
;
1044 slang_operation
*inlined
, *top
;
1045 slang_variable
**substOld
;
1046 slang_operation
**substNew
;
1047 GLuint substCount
, numCopyIn
, i
;
1048 slang_function
*prevFunction
;
1049 slang_variable_scope
*newScope
= NULL
;
1052 prevFunction
= A
->CurFunction
;
1053 A
->CurFunction
= fun
;
1055 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1056 assert(fun
->param_count
== totalArgs
);
1058 /* allocate temporary arrays */
1059 paramMode
= (ParamMode
*)
1060 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1061 substOld
= (slang_variable
**)
1062 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1063 substNew
= (slang_operation
**)
1064 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1067 printf("\nInline call to %s (total vars=%d nparams=%d)\n",
1068 (char *) fun
->header
.a_name
,
1069 fun
->parameters
->num_variables
, numArgs
);
1072 if (haveRetValue
&& !returnOper
) {
1073 /* Create 3-child comma sequence for inlined code:
1074 * child[0]: declare __resultTmp
1075 * child[1]: inlined function body
1076 * child[2]: __resultTmp
1078 slang_operation
*commaSeq
;
1079 slang_operation
*declOper
= NULL
;
1080 slang_variable
*resultVar
;
1082 commaSeq
= slang_operation_new(1);
1083 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1084 assert(commaSeq
->locals
);
1085 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1086 commaSeq
->num_children
= 3;
1087 commaSeq
->children
= slang_operation_new(3);
1088 /* allocate the return var */
1089 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1091 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1092 (void*)commaSeq->locals, (char *) fun->header.a_name);
1095 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1096 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1097 resultVar
->isTemp
= GL_TRUE
;
1099 /* child[0] = __resultTmp declaration */
1100 declOper
= &commaSeq
->children
[0];
1101 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1102 declOper
->a_id
= resultVar
->a_name
;
1103 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1105 /* child[1] = function body */
1106 inlined
= &commaSeq
->children
[1];
1107 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1109 /* child[2] = __resultTmp reference */
1110 returnOper
= &commaSeq
->children
[2];
1111 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1112 returnOper
->a_id
= resultVar
->a_name
;
1113 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1118 top
= inlined
= slang_operation_new(1);
1119 /* XXXX this may be inappropriate!!!! */
1120 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1124 assert(inlined
->locals
);
1126 /* Examine the parameters, look for inout/out params, look for possible
1127 * substitutions, etc:
1128 * param type behaviour
1129 * in copy actual to local
1130 * const in substitute param with actual
1134 for (i
= 0; i
< totalArgs
; i
++) {
1135 slang_variable
*p
= fun
->parameters
->variables
[i
];
1137 printf("Param %d: %s %s \n", i,
1138 slang_type_qual_string(p->type.qualifier),
1139 (char *) p->a_name);
1141 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1142 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1143 /* an output param */
1144 slang_operation
*arg
;
1149 paramMode
[i
] = SUBST
;
1151 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1152 slang_resolve_variable(arg
);
1154 /* replace parameter 'p' with argument 'arg' */
1155 substOld
[substCount
] = p
;
1156 substNew
[substCount
] = arg
; /* will get copied */
1159 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1160 /* a constant input param */
1161 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1162 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
1163 /* replace all occurances of this parameter variable with the
1164 * actual argument variable or a literal.
1166 paramMode
[i
] = SUBST
;
1167 slang_resolve_variable(&args
[i
]);
1168 substOld
[substCount
] = p
;
1169 substNew
[substCount
] = &args
[i
]; /* will get copied */
1173 paramMode
[i
] = COPY_IN
;
1177 paramMode
[i
] = COPY_IN
;
1179 assert(paramMode
[i
]);
1182 /* actual code inlining: */
1183 slang_operation_copy(inlined
, fun
->body
);
1185 /*** XXX review this */
1186 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
1187 inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
);
1188 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1191 printf("======================= orig body code ======================\n");
1192 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1193 slang_print_tree(fun
->body
, 8);
1194 printf("======================= copied code =========================\n");
1195 slang_print_tree(inlined
, 8);
1198 /* do parameter substitution in inlined code: */
1199 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1202 printf("======================= subst code ==========================\n");
1203 slang_print_tree(inlined
, 8);
1204 printf("=============================================================\n");
1207 /* New prolog statements: (inserted before the inlined code)
1208 * Copy the 'in' arguments.
1211 for (i
= 0; i
< numArgs
; i
++) {
1212 if (paramMode
[i
] == COPY_IN
) {
1213 slang_variable
*p
= fun
->parameters
->variables
[i
];
1214 /* declare parameter 'p' */
1215 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1219 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1220 assert(decl
->locals
);
1221 decl
->locals
->outer_scope
= inlined
->locals
;
1222 decl
->a_id
= p
->a_name
;
1223 decl
->num_children
= 1;
1224 decl
->children
= slang_operation_new(1);
1226 /* child[0] is the var's initializer */
1227 slang_operation_copy(&decl
->children
[0], args
+ i
);
1229 /* add parameter 'p' to the local variable scope here */
1231 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1232 pCopy
->type
= p
->type
;
1233 pCopy
->a_name
= p
->a_name
;
1234 pCopy
->array_len
= p
->array_len
;
1237 newScope
= inlined
->locals
;
1242 /* Now add copies of the function's local vars to the new variable scope */
1243 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1244 slang_variable
*p
= fun
->parameters
->variables
[i
];
1245 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1246 pCopy
->type
= p
->type
;
1247 pCopy
->a_name
= p
->a_name
;
1248 pCopy
->array_len
= p
->array_len
;
1252 /* New epilog statements:
1253 * 1. Create end of function label to jump to from return statements.
1254 * 2. Copy the 'out' parameter vars
1257 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1259 inlined
->num_children
);
1260 lab
->type
= SLANG_OPER_LABEL
;
1261 lab
->label
= A
->curFuncEndLabel
;
1264 for (i
= 0; i
< totalArgs
; i
++) {
1265 if (paramMode
[i
] == COPY_OUT
) {
1266 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1267 /* actualCallVar = outParam */
1268 /*if (i > 0 || !haveRetValue)*/
1269 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1271 inlined
->num_children
);
1272 ass
->type
= SLANG_OPER_ASSIGN
;
1273 ass
->num_children
= 2;
1274 ass
->locals
->outer_scope
= inlined
->locals
;
1275 ass
->children
= slang_operation_new(2);
1276 ass
->children
[0] = args
[i
]; /*XXX copy */
1277 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1278 ass
->children
[1].a_id
= p
->a_name
;
1279 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1283 _slang_free(paramMode
);
1284 _slang_free(substOld
);
1285 _slang_free(substNew
);
1287 /* Update scoping to use the new local vars instead of the
1288 * original function's vars. This is especially important
1289 * for nested inlining.
1292 slang_replace_scope(inlined
, fun
->parameters
, newScope
);
1295 printf("Done Inline call to %s (total vars=%d nparams=%d)\n\n",
1296 (char *) fun
->header
.a_name
,
1297 fun
->parameters
->num_variables
, numArgs
);
1298 slang_print_tree(top
, 0);
1302 A
->CurFunction
= prevFunction
;
1308 static slang_ir_node
*
1309 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1310 slang_operation
*oper
, slang_operation
*dest
)
1313 slang_operation
*inlined
;
1314 slang_label
*prevFuncEndLabel
;
1317 prevFuncEndLabel
= A
->curFuncEndLabel
;
1318 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1319 A
->curFuncEndLabel
= _slang_label_new(name
);
1320 assert(A
->curFuncEndLabel
);
1322 if (slang_is_asm_function(fun
) && !dest
) {
1323 /* assemble assembly function - tree style */
1324 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1327 /* non-assembly function */
1328 /* We always generate an "inline-able" block of code here.
1330 * 1. insert the inline code
1331 * 2. Generate a call to the "inline" code as a subroutine
1335 slang_operation
*ret
= NULL
;
1337 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1341 ret
= _slang_find_node_type(inlined
, SLANG_OPER_RETURN
);
1343 /* check if this is a "tail" return */
1344 if (_slang_count_node_type(inlined
, SLANG_OPER_RETURN
) == 1 &&
1345 _slang_is_tail_return(inlined
)) {
1346 /* The only RETURN is the last stmt in the function, no-op it
1347 * and inline the function body.
1349 ret
->type
= SLANG_OPER_NONE
;
1352 slang_operation
*callOper
;
1353 /* The function we're calling has one or more 'return' statements.
1354 * So, we can't truly inline this function because we need to
1355 * implement 'return' with RET (and CAL).
1356 * Nevertheless, we performed "inlining" to make a new instance
1357 * of the function body to deal with static register allocation.
1359 * XXX check if there's one 'return' and if it's the very last
1360 * statement in the function - we can optimize that case.
1362 assert(inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1363 inlined
->type
== SLANG_OPER_SEQUENCE
);
1365 if (_slang_function_has_return_value(fun
) && !dest
) {
1366 assert(inlined
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1367 assert(inlined
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1368 callOper
= &inlined
->children
[1];
1373 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1374 callOper
->fun
= fun
;
1375 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1383 /* Replace the function call with the inlined block (or new CALL stmt) */
1384 slang_operation_destruct(oper
);
1386 _slang_free(inlined
);
1389 assert(inlined
->locals
);
1390 printf("*** Inlined code for call to %s:\n",
1391 (char*) fun
->header
.a_name
);
1392 slang_print_tree(oper
, 10);
1396 n
= _slang_gen_operation(A
, oper
);
1398 /*_slang_label_delete(A->curFuncEndLabel);*/
1399 A
->curFuncEndLabel
= prevFuncEndLabel
;
1405 static slang_asm_info
*
1406 slang_find_asm_info(const char *name
)
1409 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1410 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1419 * Return the default swizzle mask for accessing a variable of the
1420 * given size (in floats). If size = 1, comp is used to identify
1421 * which component [0..3] of the register holds the variable.
1424 _slang_var_swizzle(GLint size
, GLint comp
)
1428 return MAKE_SWIZZLE4(comp
, comp
, comp
, comp
);
1430 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_NIL
, SWIZZLE_NIL
);
1432 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_NIL
);
1434 return SWIZZLE_XYZW
;
1440 * Some write-masked assignments are simple, but others are hard.
1443 * v.xy = vec2(a, b);
1446 * v.zy = vec2(a, b);
1447 * this gets transformed/swizzled into:
1448 * v.zy = vec2(a, b).*yx* (* = don't care)
1449 * This function helps to determine simple vs. non-simple.
1452 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1454 switch (writemask
) {
1456 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1458 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1460 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1462 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1464 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1465 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1467 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1468 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1469 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1470 case WRITEMASK_XYZW
:
1471 return swizzle
== SWIZZLE_NOOP
;
1479 * Convert the given swizzle into a writemask. In some cases this
1480 * is trivial, in other cases, we'll need to also swizzle the right
1481 * hand side to put components in the right places.
1482 * See comment above for more info.
1483 * XXX this function could be simplified and should probably be renamed.
1484 * \param swizzle the incoming swizzle
1485 * \param writemaskOut returns the writemask
1486 * \param swizzleOut swizzle to apply to the right-hand-side
1487 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1490 swizzle_to_writemask(slang_assemble_ctx
*A
, GLuint swizzle
,
1491 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1493 GLuint mask
= 0x0, newSwizzle
[4];
1496 /* make new dst writemask, compute size */
1497 for (i
= 0; i
< 4; i
++) {
1498 const GLuint swz
= GET_SWZ(swizzle
, i
);
1499 if (swz
== SWIZZLE_NIL
) {
1503 assert(swz
>= 0 && swz
<= 3);
1505 if (swizzle
!= SWIZZLE_XXXX
&&
1506 swizzle
!= SWIZZLE_YYYY
&&
1507 swizzle
!= SWIZZLE_ZZZZ
&&
1508 swizzle
!= SWIZZLE_WWWW
&&
1509 (mask
& (1 << swz
))) {
1510 /* a channel can't be specified twice (ex: ".xyyz") */
1511 slang_info_log_error(A
->log
, "Invalid writemask '%s'",
1512 _mesa_swizzle_string(swizzle
, 0, 0));
1518 assert(mask
<= 0xf);
1519 size
= i
; /* number of components in mask/swizzle */
1521 *writemaskOut
= mask
;
1523 /* make new src swizzle, by inversion */
1524 for (i
= 0; i
< 4; i
++) {
1525 newSwizzle
[i
] = i
; /*identity*/
1527 for (i
= 0; i
< size
; i
++) {
1528 const GLuint swz
= GET_SWZ(swizzle
, i
);
1529 newSwizzle
[swz
] = i
;
1531 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1536 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1538 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1540 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1542 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1544 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1553 * Recursively traverse 'oper' to produce a swizzle mask in the event
1554 * of any vector subscripts and swizzle suffixes.
1555 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1558 resolve_swizzle(const slang_operation
*oper
)
1560 if (oper
->type
== SLANG_OPER_FIELD
) {
1561 /* writemask from .xyzw suffix */
1563 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1564 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1568 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1569 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1573 return SWIZZLE_XYZW
;
1575 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1576 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1577 /* writemask from [index] */
1578 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1579 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1584 swizzle
= SWIZZLE_XXXX
;
1587 swizzle
= SWIZZLE_YYYY
;
1590 swizzle
= SWIZZLE_ZZZZ
;
1593 swizzle
= SWIZZLE_WWWW
;
1596 swizzle
= SWIZZLE_XYZW
;
1598 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1602 return SWIZZLE_XYZW
;
1608 * Recursively descend through swizzle nodes to find the node's storage info.
1610 static slang_ir_storage
*
1611 get_store(const slang_ir_node
*n
)
1613 if (n
->Opcode
== IR_SWIZZLE
) {
1614 return get_store(n
->Children
[0]);
1622 * Generate IR tree for an asm instruction/operation such as:
1623 * __asm vec4_dot __retVal.x, v1, v2;
1625 static slang_ir_node
*
1626 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1627 slang_operation
*dest
)
1629 const slang_asm_info
*info
;
1630 slang_ir_node
*kids
[3], *n
;
1631 GLuint j
, firstOperand
;
1633 assert(oper
->type
== SLANG_OPER_ASM
);
1635 info
= slang_find_asm_info((char *) oper
->a_id
);
1637 _mesa_problem(NULL
, "undefined __asm function %s\n",
1638 (char *) oper
->a_id
);
1641 assert(info
->NumParams
<= 3);
1643 if (info
->NumParams
== oper
->num_children
) {
1644 /* Storage for result is not specified.
1645 * Children[0], [1], [2] are the operands.
1650 /* Storage for result (child[0]) is specified.
1651 * Children[1], [2], [3] are the operands.
1656 /* assemble child(ren) */
1657 kids
[0] = kids
[1] = kids
[2] = NULL
;
1658 for (j
= 0; j
< info
->NumParams
; j
++) {
1659 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1664 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1667 /* Setup n->Store to be a particular location. Otherwise, storage
1668 * for the result (a temporary) will be allocated later.
1670 slang_operation
*dest_oper
;
1673 dest_oper
= &oper
->children
[0];
1675 n0
= _slang_gen_operation(A
, dest_oper
);
1680 n
->Store
= n0
->Store
;
1682 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
|| n
->Store
->Parent
);
1692 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1695 for (i
= 0; i
< scope
->num_functions
; i
++) {
1696 slang_function
*f
= &scope
->functions
[i
];
1697 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1698 printf(" %s (%d args)\n", name
, f
->param_count
);
1701 if (scope
->outer_scope
)
1702 print_funcs(scope
->outer_scope
, name
);
1707 * Find a function of the given name, taking 'numArgs' arguments.
1708 * This is the function we'll try to call when there is no exact match
1709 * between function parameters and call arguments.
1711 * XXX we should really create a list of candidate functions and try
1714 static slang_function
*
1715 _slang_find_function_by_argc(slang_function_scope
*scope
,
1716 const char *name
, int numArgs
)
1720 for (i
= 0; i
< scope
->num_functions
; i
++) {
1721 slang_function
*f
= &scope
->functions
[i
];
1722 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1723 int haveRetValue
= _slang_function_has_return_value(f
);
1724 if (numArgs
== f
->param_count
- haveRetValue
)
1728 scope
= scope
->outer_scope
;
1735 static slang_function
*
1736 _slang_find_function_by_max_argc(slang_function_scope
*scope
,
1739 slang_function
*maxFunc
= NULL
;
1744 for (i
= 0; i
< scope
->num_functions
; i
++) {
1745 slang_function
*f
= &scope
->functions
[i
];
1746 if (strcmp(name
, (char*) f
->header
.a_name
) == 0) {
1747 if (f
->param_count
> maxArgs
) {
1748 maxArgs
= f
->param_count
;
1753 scope
= scope
->outer_scope
;
1761 * Generate a new slang_function which is a constructor for a user-defined
1764 static slang_function
*
1765 _slang_make_struct_constructor(slang_assemble_ctx
*A
, slang_struct
*str
)
1767 const GLint numFields
= str
->fields
->num_variables
;
1768 slang_function
*fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1770 /* function header (name, return type) */
1771 fun
->header
.a_name
= str
->a_name
;
1772 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1773 fun
->header
.type
.specifier
.type
= SLANG_SPEC_STRUCT
;
1774 fun
->header
.type
.specifier
._struct
= str
;
1776 /* function parameters (= struct's fields) */
1779 for (i
= 0; i
< numFields
; i
++) {
1781 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1783 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1784 *p
= *str
->fields
->variables
[i
]; /* copy the variable and type */
1785 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1787 fun
->param_count
= fun
->parameters
->num_variables
;
1790 /* Add __retVal to params */
1792 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1793 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1795 p
->a_name
= a_retVal
;
1796 p
->type
= fun
->header
.type
;
1797 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1801 /* function body is:
1811 slang_variable_scope
*scope
;
1812 slang_variable
*var
;
1815 fun
->body
= slang_operation_new(1);
1816 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1817 fun
->body
->num_children
= numFields
+ 2;
1818 fun
->body
->children
= slang_operation_new(numFields
+ 2);
1820 scope
= fun
->body
->locals
;
1821 scope
->outer_scope
= fun
->parameters
;
1823 /* create local var 't' */
1824 var
= slang_variable_scope_grow(scope
);
1825 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "t");
1826 var
->type
= fun
->header
.type
;
1830 slang_operation
*decl
;
1832 decl
= &fun
->body
->children
[0];
1833 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1834 decl
->locals
= _slang_variable_scope_new(scope
);
1835 decl
->a_id
= var
->a_name
;
1838 /* assign params to fields of t */
1839 for (i
= 0; i
< numFields
; i
++) {
1840 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
1842 assign
->type
= SLANG_OPER_ASSIGN
;
1843 assign
->locals
= _slang_variable_scope_new(scope
);
1844 assign
->num_children
= 2;
1845 assign
->children
= slang_operation_new(2);
1848 slang_operation
*lhs
= &assign
->children
[0];
1850 lhs
->type
= SLANG_OPER_FIELD
;
1851 lhs
->locals
= _slang_variable_scope_new(scope
);
1852 lhs
->num_children
= 1;
1853 lhs
->children
= slang_operation_new(1);
1854 lhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1856 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1857 lhs
->children
[0].a_id
= var
->a_name
;
1858 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
1861 lhs
->children
[1].num_children
= 1;
1862 lhs
->children
[1].children
= slang_operation_new(1);
1863 lhs
->children
[1].children
[0].type
= SLANG_OPER_IDENTIFIER
;
1864 lhs
->children
[1].children
[0].a_id
= str
->fields
->variables
[i
]->a_name
;
1865 lhs
->children
[1].children
->locals
= _slang_variable_scope_new(scope
);
1870 slang_operation
*rhs
= &assign
->children
[1];
1872 rhs
->type
= SLANG_OPER_IDENTIFIER
;
1873 rhs
->locals
= _slang_variable_scope_new(scope
);
1874 rhs
->a_id
= str
->fields
->variables
[i
]->a_name
;
1880 slang_operation
*ret
= &fun
->body
->children
[numFields
+ 1];
1882 ret
->type
= SLANG_OPER_RETURN
;
1883 ret
->locals
= _slang_variable_scope_new(scope
);
1884 ret
->num_children
= 1;
1885 ret
->children
= slang_operation_new(1);
1886 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1887 ret
->children
[0].a_id
= var
->a_name
;
1888 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
1892 slang_print_function(fun, 1);
1899 * Find/create a function (constructor) for the given structure name.
1901 static slang_function
*
1902 _slang_locate_struct_constructor(slang_assemble_ctx
*A
, const char *name
)
1905 for (i
= 0; i
< A
->space
.structs
->num_structs
; i
++) {
1906 slang_struct
*str
= &A
->space
.structs
->structs
[i
];
1907 if (strcmp(name
, (const char *) str
->a_name
) == 0) {
1908 /* found a structure type that matches the function name */
1909 if (!str
->constructor
) {
1910 /* create the constructor function now */
1911 str
->constructor
= _slang_make_struct_constructor(A
, str
);
1913 return str
->constructor
;
1921 * Generate a new slang_function to satisfy a call to an array constructor.
1922 * Ex: float[3](1., 2., 3.)
1924 static slang_function
*
1925 _slang_make_array_constructor(slang_assemble_ctx
*A
, slang_operation
*oper
)
1927 slang_type_specifier_type baseType
;
1928 slang_function
*fun
;
1931 fun
= slang_function_new(SLANG_FUNC_CONSTRUCTOR
);
1935 baseType
= slang_type_specifier_type_from_string((char *) oper
->a_id
);
1937 num_elements
= oper
->num_children
;
1939 /* function header, return type */
1941 fun
->header
.a_name
= oper
->a_id
;
1942 fun
->header
.type
.qualifier
= SLANG_QUAL_NONE
;
1943 fun
->header
.type
.specifier
.type
= SLANG_SPEC_ARRAY
;
1944 fun
->header
.type
.specifier
._array
=
1945 slang_type_specifier_new(baseType
, NULL
, NULL
);
1946 fun
->header
.type
.array_len
= num_elements
;
1949 /* function parameters (= number of elements) */
1952 for (i
= 0; i
< num_elements
; i
++) {
1954 printf("Field %d: %s\n", i, (char*) str->fields->variables[i]->a_name);
1956 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1958 snprintf(name
, sizeof(name
), "p%d", i
);
1959 p
->a_name
= slang_atom_pool_atom(A
->atoms
, name
);
1960 p
->type
.qualifier
= SLANG_QUAL_CONST
;
1961 p
->type
.specifier
.type
= baseType
;
1963 fun
->param_count
= fun
->parameters
->num_variables
;
1966 /* Add __retVal to params */
1968 slang_variable
*p
= slang_variable_scope_grow(fun
->parameters
);
1969 slang_atom a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1971 p
->a_name
= a_retVal
;
1972 p
->type
= fun
->header
.type
;
1973 p
->type
.qualifier
= SLANG_QUAL_OUT
;
1974 p
->type
.specifier
.type
= baseType
;
1978 /* function body is:
1988 slang_variable_scope
*scope
;
1989 slang_variable
*var
;
1992 fun
->body
= slang_operation_new(1);
1993 fun
->body
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1994 fun
->body
->num_children
= num_elements
+ 2;
1995 fun
->body
->children
= slang_operation_new(num_elements
+ 2);
1997 scope
= fun
->body
->locals
;
1998 scope
->outer_scope
= fun
->parameters
;
2000 /* create local var 't' */
2001 var
= slang_variable_scope_grow(scope
);
2002 var
->a_name
= slang_atom_pool_atom(A
->atoms
, "ttt");
2003 var
->type
= fun
->header
.type
;/*XXX copy*/
2007 slang_operation
*decl
;
2009 decl
= &fun
->body
->children
[0];
2010 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
2011 decl
->locals
= _slang_variable_scope_new(scope
);
2012 decl
->a_id
= var
->a_name
;
2015 /* assign params to elements of t */
2016 for (i
= 0; i
< num_elements
; i
++) {
2017 slang_operation
*assign
= &fun
->body
->children
[1 + i
];
2019 assign
->type
= SLANG_OPER_ASSIGN
;
2020 assign
->locals
= _slang_variable_scope_new(scope
);
2021 assign
->num_children
= 2;
2022 assign
->children
= slang_operation_new(2);
2025 slang_operation
*lhs
= &assign
->children
[0];
2027 lhs
->type
= SLANG_OPER_SUBSCRIPT
;
2028 lhs
->locals
= _slang_variable_scope_new(scope
);
2029 lhs
->num_children
= 2;
2030 lhs
->children
= slang_operation_new(2);
2032 lhs
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2033 lhs
->children
[0].a_id
= var
->a_name
;
2034 lhs
->children
[0].locals
= _slang_variable_scope_new(scope
);
2036 lhs
->children
[1].type
= SLANG_OPER_LITERAL_INT
;
2037 lhs
->children
[1].literal
[0] = (GLfloat
) i
;
2041 slang_operation
*rhs
= &assign
->children
[1];
2043 rhs
->type
= SLANG_OPER_IDENTIFIER
;
2044 rhs
->locals
= _slang_variable_scope_new(scope
);
2045 rhs
->a_id
= fun
->parameters
->variables
[i
]->a_name
;
2051 slang_operation
*ret
= &fun
->body
->children
[num_elements
+ 1];
2053 ret
->type
= SLANG_OPER_RETURN
;
2054 ret
->locals
= _slang_variable_scope_new(scope
);
2055 ret
->num_children
= 1;
2056 ret
->children
= slang_operation_new(1);
2057 ret
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2058 ret
->children
[0].a_id
= var
->a_name
;
2059 ret
->children
[0].locals
= _slang_variable_scope_new(scope
);
2064 slang_print_function(fun, 1);
2072 _slang_is_vec_mat_type(const char *name
)
2074 static const char *vecmat_types
[] = {
2075 "float", "int", "bool",
2076 "vec2", "vec3", "vec4",
2077 "ivec2", "ivec3", "ivec4",
2078 "bvec2", "bvec3", "bvec4",
2079 "mat2", "mat3", "mat4",
2080 "mat2x3", "mat2x4", "mat3x2", "mat3x4", "mat4x2", "mat4x3",
2084 for (i
= 0; vecmat_types
[i
]; i
++)
2085 if (_mesa_strcmp(name
, vecmat_types
[i
]) == 0)
2092 * Assemble a function call, given a particular function name.
2093 * \param name the function's name (operators like '*' are possible).
2095 static slang_ir_node
*
2096 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
2097 slang_operation
*oper
, slang_operation
*dest
)
2099 slang_operation
*params
= oper
->children
;
2100 const GLuint param_count
= oper
->num_children
;
2102 slang_function
*fun
;
2105 atom
= slang_atom_pool_atom(A
->atoms
, name
);
2106 if (atom
== SLANG_ATOM_NULL
)
2109 if (oper
->array_constructor
) {
2110 /* this needs special handling */
2111 fun
= _slang_make_array_constructor(A
, oper
);
2114 /* Try to find function by name and exact argument type matching */
2115 GLboolean error
= GL_FALSE
;
2116 fun
= _slang_function_locate(A
->space
.funcs
, atom
, params
, param_count
,
2117 &A
->space
, A
->atoms
, A
->log
, &error
);
2119 slang_info_log_error(A
->log
,
2120 "Function '%s' not found (check argument types)",
2127 /* Next, try locating a constructor function for a user-defined type */
2128 fun
= _slang_locate_struct_constructor(A
, name
);
2132 * At this point, some heuristics are used to try to find a function
2133 * that matches the calling signature by means of casting or "unrolling"
2137 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2138 /* Next, if this call looks like a vec() or mat() constructor call,
2139 * try "unwinding" the args to satisfy a constructor.
2141 fun
= _slang_find_function_by_max_argc(A
->space
.funcs
, name
);
2143 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2144 slang_info_log_error(A
->log
,
2145 "Function '%s' not found (check argument types)",
2152 if (!fun
&& _slang_is_vec_mat_type(name
)) {
2153 /* Next, try casting args to the types of the formal parameters */
2154 int numArgs
= oper
->num_children
;
2155 fun
= _slang_find_function_by_argc(A
->space
.funcs
, name
, numArgs
);
2156 if (!fun
|| !_slang_cast_func_params(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
2157 slang_info_log_error(A
->log
,
2158 "Function '%s' not found (check argument types)",
2166 slang_info_log_error(A
->log
,
2167 "Function '%s' not found (check argument types)",
2172 slang_info_log_error(A
->log
,
2173 "Function '%s' prototyped but not defined. "
2174 "Separate compilation units not supported.",
2179 /* type checking to be sure function's return type matches 'dest' type */
2183 slang_typeinfo_construct(&t0
);
2184 typeof_operation(A
, dest
, &t0
);
2186 if (!slang_type_specifier_equal(&t0
.spec
, &fun
->header
.type
.specifier
)) {
2187 slang_info_log_error(A
->log
,
2188 "Incompatible type returned by call to '%s'",
2194 n
= _slang_gen_function_call(A
, fun
, oper
, dest
);
2196 if (n
&& !n
->Store
&& !dest
2197 && fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2198 /* setup n->Store for the result of the function call */
2199 GLint size
= _slang_sizeof_type_specifier(&fun
->header
.type
.specifier
);
2200 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
2201 /*printf("Alloc storage for function result, size %d \n", size);*/
2204 if (oper
->array_constructor
) {
2205 /* free the temporary array constructor function now */
2206 slang_function_destruct(fun
);
2213 static slang_ir_node
*
2214 _slang_gen_method_call(slang_assemble_ctx
*A
, slang_operation
*oper
)
2216 slang_atom
*a_length
= slang_atom_pool_atom(A
->atoms
, "length");
2218 slang_variable
*var
;
2220 /* NOTE: In GLSL 1.20, there's only one kind of method
2221 * call: array.length(). Anything else is an error.
2223 if (oper
->a_id
!= a_length
) {
2224 slang_info_log_error(A
->log
,
2225 "Undefined method call '%s'", (char *) oper
->a_id
);
2229 /* length() takes no arguments */
2230 if (oper
->num_children
> 0) {
2231 slang_info_log_error(A
->log
, "Invalid arguments to length() method");
2235 /* lookup the object/variable */
2236 var
= _slang_variable_locate(oper
->locals
, oper
->a_obj
, GL_TRUE
);
2237 if (!var
|| var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
2238 slang_info_log_error(A
->log
,
2239 "Undefined object '%s'", (char *) oper
->a_obj
);
2243 /* Create a float/literal IR node encoding the array length */
2244 n
= new_node0(IR_FLOAT
);
2246 n
->Value
[0] = (float) var
->array_len
;
2247 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, 1);
2254 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
2256 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
2257 oper
->type
== SLANG_OPER_LITERAL_INT
||
2258 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
2259 if (oper
->literal
[0])
2265 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
2266 oper
->num_children
== 1) {
2267 return _slang_is_constant_cond(&oper
->children
[0], value
);
2274 * Test if an operation is a scalar or boolean.
2277 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2279 slang_typeinfo type
;
2282 slang_typeinfo_construct(&type
);
2283 typeof_operation(A
, oper
, &type
);
2284 size
= _slang_sizeof_type_specifier(&type
.spec
);
2285 slang_typeinfo_destruct(&type
);
2291 * Test if an operation is boolean.
2294 _slang_is_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
2296 slang_typeinfo type
;
2299 slang_typeinfo_construct(&type
);
2300 typeof_operation(A
, oper
, &type
);
2301 isBool
= (type
.spec
.type
== SLANG_SPEC_BOOL
);
2302 slang_typeinfo_destruct(&type
);
2308 * Generate loop code using high-level IR_LOOP instruction
2310 static slang_ir_node
*
2311 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2315 * BREAK if !expr (child[0])
2316 * body code (child[1])
2318 slang_ir_node
*prevLoop
, *loop
, *breakIf
, *body
;
2319 GLboolean isConst
, constTrue
;
2321 /* type-check expression */
2322 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2323 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
2327 /* Check if loop condition is a constant */
2328 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2330 if (isConst
&& !constTrue
) {
2331 /* loop is never executed! */
2332 return new_node0(IR_NOP
);
2335 loop
= new_loop(NULL
);
2337 /* save old, push new loop */
2338 prevLoop
= A
->CurLoop
;
2341 if (isConst
&& constTrue
) {
2342 /* while(nonzero constant), no conditional break */
2347 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
2348 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2350 body
= _slang_gen_operation(A
, &oper
->children
[1]);
2351 loop
->Children
[0] = new_seq(breakIf
, body
);
2353 /* Do infinite loop detection */
2354 /* loop->List is head of linked list of break/continue nodes */
2355 if (!loop
->List
&& isConst
&& constTrue
) {
2356 /* infinite loop detected */
2357 A
->CurLoop
= prevLoop
; /* clean-up */
2358 slang_info_log_error(A
->log
, "Infinite loop detected!");
2362 /* pop loop, restore prev */
2363 A
->CurLoop
= prevLoop
;
2370 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
2372 static slang_ir_node
*
2373 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2377 * body code (child[0])
2379 * BREAK if !expr (child[1])
2381 slang_ir_node
*prevLoop
, *loop
;
2382 GLboolean isConst
, constTrue
;
2384 /* type-check expression */
2385 if (!_slang_is_boolean(A
, &oper
->children
[1])) {
2386 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
2390 loop
= new_loop(NULL
);
2392 /* save old, push new loop */
2393 prevLoop
= A
->CurLoop
;
2397 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
2399 /* Check if loop condition is a constant */
2400 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
2401 if (isConst
&& constTrue
) {
2402 /* do { } while(1) ==> no conditional break */
2403 loop
->Children
[1] = NULL
; /* no tail code */
2407 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2408 loop
->Children
[1] = new_break_if_true(A
->CurLoop
, cond
);
2411 /* XXX we should do infinite loop detection, as above */
2413 /* pop loop, restore prev */
2414 A
->CurLoop
= prevLoop
;
2421 * Generate for-loop using high-level IR_LOOP instruction.
2423 static slang_ir_node
*
2424 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2427 * init code (child[0])
2429 * BREAK if !expr (child[1])
2430 * body code (child[3])
2432 * incr code (child[2]) // XXX continue here
2434 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
2436 init
= _slang_gen_operation(A
, &oper
->children
[0]);
2437 loop
= new_loop(NULL
);
2439 /* save old, push new loop */
2440 prevLoop
= A
->CurLoop
;
2443 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
2444 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
2445 body
= _slang_gen_operation(A
, &oper
->children
[3]);
2446 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
2448 loop
->Children
[0] = new_seq(breakIf
, body
);
2449 loop
->Children
[1] = incr
; /* tail code */
2451 /* pop loop, restore prev */
2452 A
->CurLoop
= prevLoop
;
2454 return new_seq(init
, loop
);
2458 static slang_ir_node
*
2459 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2461 slang_ir_node
*n
, *loopNode
;
2462 assert(oper
->type
== SLANG_OPER_CONTINUE
);
2463 loopNode
= A
->CurLoop
;
2465 assert(loopNode
->Opcode
== IR_LOOP
);
2466 n
= new_node0(IR_CONT
);
2468 n
->Parent
= loopNode
;
2469 /* insert this node at head of linked list */
2470 n
->List
= loopNode
->List
;
2478 * Determine if the given operation is of a specific type.
2481 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
2483 if (oper
->type
== type
)
2485 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
2486 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
2487 oper
->num_children
== 1)
2488 return is_operation_type(&oper
->children
[0], type
);
2495 * Generate IR tree for an if/then/else conditional using high-level
2496 * IR_IF instruction.
2498 static slang_ir_node
*
2499 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2502 * eval expr (child[0])
2509 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
2510 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
2511 GLboolean isConst
, constTrue
;
2513 /* type-check expression */
2514 if (!_slang_is_boolean(A
, &oper
->children
[0])) {
2515 slang_info_log_error(A
->log
, "boolean expression expected for 'if'");
2519 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2520 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
2524 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
2528 return _slang_gen_operation(A
, &oper
->children
[1]);
2531 /* if (false) ... */
2532 return _slang_gen_operation(A
, &oper
->children
[2]);
2536 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2537 cond
= new_cond(cond
);
2539 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)
2540 && !haveElseClause
) {
2541 /* Special case: generate a conditional break */
2542 ifBody
= new_break_if_true(A
->CurLoop
, cond
);
2545 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)
2546 && !haveElseClause
) {
2547 /* Special case: generate a conditional break */
2548 ifBody
= new_cont_if_true(A
->CurLoop
, cond
);
2553 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
2555 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
2558 ifNode
= new_if(cond
, ifBody
, elseBody
);
2565 static slang_ir_node
*
2566 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2570 assert(oper
->type
== SLANG_OPER_NOT
);
2572 /* type-check expression */
2573 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2574 slang_info_log_error(A
->log
,
2575 "scalar/boolean expression expected for '!'");
2579 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2587 static slang_ir_node
*
2588 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2590 slang_ir_node
*n1
, *n2
;
2592 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
2594 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
2595 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2596 slang_info_log_error(A
->log
,
2597 "scalar/boolean expressions expected for '^^'");
2601 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
2604 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
2607 return new_node2(IR_NOTEQUAL
, n1
, n2
);
2612 * Generate IR node for storage of a temporary of given size.
2614 static slang_ir_node
*
2615 _slang_gen_temporary(GLint size
)
2617 slang_ir_storage
*store
;
2618 slang_ir_node
*n
= NULL
;
2620 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
2622 n
= new_node0(IR_VAR_DECL
);
2635 * Generate program constants for an array.
2636 * Ex: const vec2[3] v = vec2[3](vec2(1,1), vec2(2,2), vec2(3,3));
2637 * This will allocate and initialize three vector constants, storing
2638 * the array in constant memory, not temporaries like a non-const array.
2639 * This can also be used for uniform array initializers.
2640 * \return GL_TRUE for success, GL_FALSE if failure (semantic error, etc).
2643 make_constant_array(slang_assemble_ctx
*A
,
2644 slang_variable
*var
,
2645 slang_operation
*initializer
)
2647 struct gl_program
*prog
= A
->program
;
2648 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
2649 const char *varName
= (char *) var
->a_name
;
2650 const GLuint numElements
= initializer
->num_children
;
2656 var
->store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -6, -6);
2658 size
= var
->store
->Size
;
2660 assert(var
->type
.qualifier
== SLANG_QUAL_CONST
||
2661 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
);
2662 assert(initializer
->type
== SLANG_OPER_CALL
);
2663 assert(initializer
->array_constructor
);
2665 values
= (GLfloat
*) _mesa_malloc(numElements
* 4 * sizeof(GLfloat
));
2667 /* convert constructor params into ordinary floats */
2668 for (i
= 0; i
< numElements
; i
++) {
2669 const slang_operation
*op
= &initializer
->children
[i
];
2670 if (op
->type
!= SLANG_OPER_LITERAL_FLOAT
) {
2671 /* unsupported type for this optimization */
2675 for (j
= 0; j
< op
->literal_size
; j
++) {
2676 values
[i
* 4 + j
] = op
->literal
[j
];
2678 for ( ; j
< 4; j
++) {
2679 values
[i
* 4 + j
] = 0.0f
;
2683 /* slightly different paths for constants vs. uniforms */
2684 if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2685 var
->store
->File
= PROGRAM_UNIFORM
;
2686 var
->store
->Index
= _mesa_add_uniform(prog
->Parameters
, varName
,
2687 size
, datatype
, values
);
2690 var
->store
->File
= PROGRAM_CONSTANT
;
2691 var
->store
->Index
= _mesa_add_named_constant(prog
->Parameters
, varName
,
2694 assert(var
->store
->Size
== size
);
2704 * Generate IR node for allocating/declaring a variable (either a local or
2706 * Generally, this involves allocating an slang_ir_storage instance for the
2707 * variable, choosing a register file (temporary, constant, etc).
2708 * For ordinary variables we do not yet allocate storage though. We do that
2709 * when we find the first actual use of the variable to avoid allocating temp
2710 * regs that will never get used.
2711 * At this time, uniforms are always allocated space in this function.
2713 * \param initializer Optional initializer expression for the variable.
2715 static slang_ir_node
*
2716 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
,
2717 slang_operation
*initializer
)
2719 const char *varName
= (const char *) var
->a_name
;
2720 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
2721 slang_ir_node
*varDecl
, *n
;
2722 slang_ir_storage
*store
;
2723 GLint size
, totalSize
; /* if array then totalSize > size */
2724 enum register_file file
;
2726 /*assert(!var->declared);*/
2727 var
->declared
= GL_TRUE
;
2729 /* determine GPU register file for simple cases */
2730 if (is_sampler_type(&var
->type
)) {
2731 file
= PROGRAM_SAMPLER
;
2733 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2734 file
= PROGRAM_UNIFORM
;
2737 file
= PROGRAM_TEMPORARY
;
2740 size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
2742 slang_info_log_error(A
->log
, "invalid declaration for '%s'", varName
);
2747 if (var
->type
.array_len
> 0) {
2748 /* the type is an array, ex: float[4] x; */
2749 GLint sz
= (totalSize
+ 3) & ~3;
2750 /* total size = element size * array length */
2751 sz
*= var
->type
.array_len
;
2755 if (var
->array_len
> 0) {
2756 /* this is an array, ex: float x[4]; */
2757 /* round up the element size to a multiple of 4 */
2758 GLint sz
= (totalSize
+ 3) & ~3;
2759 /* total size = element size * array length */
2760 sz
*= var
->array_len
;
2764 /* Allocate IR node for the declaration */
2765 varDecl
= new_node0(IR_VAR_DECL
);
2769 _slang_attach_storage(varDecl
, var
); /* undefined storage at first */
2771 assert(varDecl
->Store
== var
->store
);
2772 assert(varDecl
->Store
);
2773 assert(varDecl
->Store
->Index
< 0);
2776 assert(store
== varDecl
->Store
);
2779 /* Fill in storage fields which we now know. store->Index/Swizzle may be
2780 * set for some cases below. Otherwise, store->Index/Swizzle will be set
2784 store
->Size
= totalSize
;
2786 /* if there's an initializer, generate IR for the expression */
2788 slang_ir_node
*varRef
, *init
;
2790 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
2791 /* if the variable is const, the initializer must be a const
2792 * expression as well.
2795 if (!_slang_is_constant_expr(initializer
)) {
2796 slang_info_log_error(A
->log
,
2797 "initializer for %s not constant", varName
);
2803 /* IR for the variable we're initializing */
2804 varRef
= new_var(A
, var
);
2806 slang_info_log_error(A
->log
, "out of memory");
2810 /* constant-folding, etc here */
2811 _slang_simplify(initializer
, &A
->space
, A
->atoms
);
2813 /* look for simple constant-valued variables and uniforms */
2814 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
2815 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2817 if (initializer
->type
== SLANG_OPER_CALL
&&
2818 initializer
->array_constructor
) {
2819 /* array initializer */
2820 if (make_constant_array(A
, var
, initializer
))
2823 else if (initializer
->type
== SLANG_OPER_LITERAL_FLOAT
||
2824 initializer
->type
== SLANG_OPER_LITERAL_INT
) {
2825 /* simple float/vector initializer */
2826 if (store
->File
== PROGRAM_UNIFORM
) {
2827 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
,
2829 totalSize
, datatype
,
2830 initializer
->literal
);
2831 store
->Swizzle
= _slang_var_swizzle(size
, 0);
2836 store
->File
= PROGRAM_CONSTANT
;
2837 store
->Index
= _mesa_add_named_constant(A
->program
->Parameters
,
2839 initializer
->literal
,
2841 store
->Swizzle
= _slang_var_swizzle(size
, 0);
2848 /* IR for initializer */
2849 init
= _slang_gen_operation(A
, initializer
);
2853 /* XXX remove this when type checking is added above */
2854 if (init
->Store
&& init
->Store
->Size
!= totalSize
) {
2855 slang_info_log_error(A
->log
, "invalid assignment (wrong types)");
2859 /* assign RHS to LHS */
2860 n
= new_node2(IR_COPY
, varRef
, init
);
2861 n
= new_seq(varDecl
, n
);
2864 /* no initializer */
2868 if (store
->File
== PROGRAM_UNIFORM
&& store
->Index
< 0) {
2869 /* always need to allocate storage for uniforms at this point */
2870 store
->Index
= _mesa_add_uniform(A
->program
->Parameters
, varName
,
2871 totalSize
, datatype
, NULL
);
2872 store
->Swizzle
= _slang_var_swizzle(size
, 0);
2876 printf("%s var %p %s store=%p index=%d size=%d\n",
2877 __FUNCTION__
, (void *) var
, (char *) varName
,
2878 (void *) store
, store
->Index
, store
->Size
);
2886 * Generate code for a selection expression: b ? x : y
2887 * XXX In some cases we could implement a selection expression
2888 * with an LRP instruction (use the boolean as the interpolant).
2889 * Otherwise, we use an IF/ELSE/ENDIF construct.
2891 static slang_ir_node
*
2892 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
2894 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
2895 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
2896 slang_typeinfo type0
, type1
, type2
;
2897 int size
, isBool
, isEqual
;
2899 assert(oper
->type
== SLANG_OPER_SELECT
);
2900 assert(oper
->num_children
== 3);
2902 /* type of children[0] must be boolean */
2903 slang_typeinfo_construct(&type0
);
2904 typeof_operation(A
, &oper
->children
[0], &type0
);
2905 isBool
= (type0
.spec
.type
== SLANG_SPEC_BOOL
);
2906 slang_typeinfo_destruct(&type0
);
2908 slang_info_log_error(A
->log
, "selector type is not boolean");
2912 slang_typeinfo_construct(&type1
);
2913 slang_typeinfo_construct(&type2
);
2914 typeof_operation(A
, &oper
->children
[1], &type1
);
2915 typeof_operation(A
, &oper
->children
[2], &type2
);
2916 isEqual
= slang_type_specifier_equal(&type1
.spec
, &type2
.spec
);
2917 slang_typeinfo_destruct(&type1
);
2918 slang_typeinfo_destruct(&type2
);
2920 slang_info_log_error(A
->log
, "incompatible types for ?: operator");
2924 /* size of x or y's type */
2925 size
= _slang_sizeof_type_specifier(&type1
.spec
);
2929 tmpDecl
= _slang_gen_temporary(size
);
2931 /* the condition (child 0) */
2932 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2933 cond
= new_cond(cond
);
2935 /* if-true body (child 1) */
2936 tmpVar
= new_node0(IR_VAR
);
2937 tmpVar
->Store
= tmpDecl
->Store
;
2938 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
2939 trueNode
= new_node2(IR_COPY
, tmpVar
, trueExpr
);
2941 /* if-false body (child 2) */
2942 tmpVar
= new_node0(IR_VAR
);
2943 tmpVar
->Store
= tmpDecl
->Store
;
2944 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
2945 falseNode
= new_node2(IR_COPY
, tmpVar
, falseExpr
);
2947 ifNode
= new_if(cond
, trueNode
, falseNode
);
2950 tmpVar
= new_node0(IR_VAR
);
2951 tmpVar
->Store
= tmpDecl
->Store
;
2953 tree
= new_seq(ifNode
, tmpVar
);
2954 tree
= new_seq(tmpDecl
, tree
);
2956 /*_slang_print_ir_tree(tree, 10);*/
2962 * Generate code for &&.
2964 static slang_ir_node
*
2965 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
2967 /* rewrite "a && b" as "a ? b : false" */
2968 slang_operation
*select
;
2971 select
= slang_operation_new(1);
2972 select
->type
= SLANG_OPER_SELECT
;
2973 select
->num_children
= 3;
2974 select
->children
= slang_operation_new(3);
2976 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
2977 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
2978 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
2979 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
2980 select
->children
[2].literal_size
= 1;
2982 n
= _slang_gen_select(A
, select
);
2988 * Generate code for ||.
2990 static slang_ir_node
*
2991 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
2993 /* rewrite "a || b" as "a ? true : b" */
2994 slang_operation
*select
;
2997 select
= slang_operation_new(1);
2998 select
->type
= SLANG_OPER_SELECT
;
2999 select
->num_children
= 3;
3000 select
->children
= slang_operation_new(3);
3002 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
3003 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
3004 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
3005 select
->children
[1].literal_size
= 1;
3006 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
3008 n
= _slang_gen_select(A
, select
);
3014 * Generate IR tree for a return statement.
3016 static slang_ir_node
*
3017 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
3019 const GLboolean haveReturnValue
3020 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
3022 /* error checking */
3023 assert(A
->CurFunction
);
3024 if (haveReturnValue
&&
3025 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
3026 slang_info_log_error(A
->log
, "illegal return expression");
3029 else if (!haveReturnValue
&&
3030 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
3031 slang_info_log_error(A
->log
, "return statement requires an expression");
3035 if (!haveReturnValue
) {
3036 return new_return(A
->curFuncEndLabel
);
3044 * return; // goto __endOfFunction
3046 slang_operation
*assign
;
3047 slang_atom a_retVal
;
3050 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
3056 _slang_variable_locate(oper
->locals
, a_retVal
, GL_TRUE
);
3058 /* trying to return a value in a void-valued function */
3064 assign
= slang_operation_new(1);
3065 assign
->type
= SLANG_OPER_ASSIGN
;
3066 assign
->num_children
= 2;
3067 assign
->children
= slang_operation_new(2);
3068 /* lhs (__retVal) */
3069 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
3070 assign
->children
[0].a_id
= a_retVal
;
3071 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
3073 /* XXX we might be able to avoid this copy someday */
3074 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
3076 /* assemble the new code */
3077 n
= new_seq(_slang_gen_operation(A
, assign
),
3078 new_return(A
->curFuncEndLabel
));
3080 slang_operation_delete(assign
);
3087 * Determine if the given operation/expression is const-valued.
3090 _slang_is_constant_expr(const slang_operation
*oper
)
3092 slang_variable
*var
;
3095 switch (oper
->type
) {
3096 case SLANG_OPER_IDENTIFIER
:
3097 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3098 if (var
&& var
->type
.qualifier
== SLANG_QUAL_CONST
)
3102 for (i
= 0; i
< oper
->num_children
; i
++) {
3103 if (!_slang_is_constant_expr(&oper
->children
[i
]))
3112 * Check if an assignment of type t1 to t0 is legal.
3113 * XXX more cases needed.
3116 _slang_assignment_compatible(slang_assemble_ctx
*A
,
3117 slang_operation
*op0
,
3118 slang_operation
*op1
)
3120 slang_typeinfo t0
, t1
;
3123 if (op0
->type
== SLANG_OPER_POSTINCREMENT
||
3124 op0
->type
== SLANG_OPER_POSTDECREMENT
) {
3128 slang_typeinfo_construct(&t0
);
3129 typeof_operation(A
, op0
, &t0
);
3131 slang_typeinfo_construct(&t1
);
3132 typeof_operation(A
, op1
, &t1
);
3134 sz0
= _slang_sizeof_type_specifier(&t0
.spec
);
3135 sz1
= _slang_sizeof_type_specifier(&t1
.spec
);
3139 /*printf("assignment size mismatch %u vs %u\n", sz0, sz1);*/
3144 if (t0
.spec
.type
== SLANG_SPEC_STRUCT
&&
3145 t1
.spec
.type
== SLANG_SPEC_STRUCT
&&
3146 t0
.spec
._struct
->a_name
!= t1
.spec
._struct
->a_name
)
3149 if (t0
.spec
.type
== SLANG_SPEC_FLOAT
&&
3150 t1
.spec
.type
== SLANG_SPEC_BOOL
)
3153 #if 0 /* not used just yet - causes problems elsewhere */
3154 if (t0
.spec
.type
== SLANG_SPEC_INT
&&
3155 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3159 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3160 t1
.spec
.type
== SLANG_SPEC_FLOAT
)
3163 if (t0
.spec
.type
== SLANG_SPEC_BOOL
&&
3164 t1
.spec
.type
== SLANG_SPEC_INT
)
3172 * Generate IR tree for a local variable declaration.
3173 * Basically do some error checking and call _slang_gen_var_decl().
3175 static slang_ir_node
*
3176 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
3178 const char *varName
= (char *) oper
->a_id
;
3179 slang_variable
*var
;
3180 slang_ir_node
*varDecl
;
3181 slang_operation
*initializer
;
3183 assert(oper
->type
== SLANG_OPER_VARIABLE_DECL
);
3184 assert(oper
->num_children
<= 1);
3186 /* lookup the variable by name */
3187 var
= _slang_variable_locate(oper
->locals
, oper
->a_id
, GL_TRUE
);
3189 return NULL
; /* "shouldn't happen" */
3191 if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3192 var
->type
.qualifier
== SLANG_QUAL_VARYING
||
3193 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3194 /* can't declare attribute/uniform vars inside functions */
3195 slang_info_log_error(A
->log
,
3196 "local variable '%s' cannot be an attribute/uniform/varying",
3203 slang_info_log_error(A
->log
, "variable '%s' redeclared", varName
);
3208 /* check if the var has an initializer */
3209 if (oper
->num_children
> 0) {
3210 assert(oper
->num_children
== 1);
3211 initializer
= &oper
->children
[0];
3213 else if (var
->initializer
) {
3214 initializer
= var
->initializer
;
3221 /* check/compare var type and initializer type */
3222 if (!_slang_assignment_compatible(A
, oper
, initializer
)) {
3223 slang_info_log_error(A
->log
, "incompatible types in assignment");
3228 if (var
->type
.qualifier
== SLANG_QUAL_CONST
) {
3229 slang_info_log_error(A
->log
,
3230 "const-qualified variable '%s' requires initializer",
3236 /* Generate IR node */
3237 varDecl
= _slang_gen_var_decl(A
, var
, initializer
);
3246 * Generate IR tree for a reference to a variable (such as in an expression).
3247 * This is different from a variable declaration.
3249 static slang_ir_node
*
3250 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
3252 /* If there's a variable associated with this oper (from inlining)
3253 * use it. Otherwise, use the oper's var id.
3255 slang_atom name
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
3256 slang_variable
*var
= _slang_variable_locate(oper
->locals
, name
, GL_TRUE
);
3259 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) name
);
3262 assert(var
->declared
);
3263 n
= new_var(A
, var
);
3270 * Return the number of components actually named by the swizzle.
3271 * Recall that swizzles may have undefined/don't-care values.
3274 swizzle_size(GLuint swizzle
)
3277 for (i
= 0; i
< 4; i
++) {
3278 GLuint swz
= GET_SWZ(swizzle
, i
);
3279 size
+= (swz
>= 0 && swz
<= 3);
3285 static slang_ir_node
*
3286 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
3288 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
3292 n
->Store
= _slang_new_ir_storage_relative(0,
3293 swizzle_size(swizzle
),
3295 n
->Store
->Swizzle
= swizzle
;
3302 is_store_writable(const slang_assemble_ctx
*A
, const slang_ir_storage
*store
)
3304 while (store
->Parent
)
3305 store
= store
->Parent
;
3307 if (!(store
->File
== PROGRAM_OUTPUT
||
3308 store
->File
== PROGRAM_TEMPORARY
||
3309 (store
->File
== PROGRAM_VARYING
&&
3310 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
))) {
3320 * Generate IR tree for an assignment (=).
3322 static slang_ir_node
*
3323 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
3325 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
3326 /* Check that var is writeable */
3328 = _slang_variable_locate(oper
->children
[0].locals
,
3329 oper
->children
[0].a_id
, GL_TRUE
);
3331 slang_info_log_error(A
->log
, "undefined variable '%s'",
3332 (char *) oper
->children
[0].a_id
);
3335 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
3336 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
3337 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
3338 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
3339 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
3340 slang_info_log_error(A
->log
,
3341 "illegal assignment to read-only variable '%s'",
3342 (char *) oper
->children
[0].a_id
);
3347 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
3348 oper
->children
[1].type
== SLANG_OPER_CALL
) {
3349 /* Special case of: x = f(a, b)
3350 * Replace with f(a, b, x) (where x == hidden __retVal out param)
3352 * XXX this could be even more effective if we could accomodate
3353 * cases such as "v.x = f();" - would help with typical vertex
3357 n
= _slang_gen_function_call_name(A
,
3358 (const char *) oper
->children
[1].a_id
,
3359 &oper
->children
[1], &oper
->children
[0]);
3363 slang_ir_node
*n
, *lhs
, *rhs
;
3365 /* lhs and rhs type checking */
3366 if (!_slang_assignment_compatible(A
,
3368 &oper
->children
[1])) {
3369 slang_info_log_error(A
->log
, "incompatible types in assignment");
3373 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
3379 slang_info_log_error(A
->log
,
3380 "invalid left hand side for assignment");
3384 /* check that lhs is writable */
3385 if (!is_store_writable(A
, lhs
->Store
)) {
3386 slang_info_log_error(A
->log
,
3387 "illegal assignment to read-only l-value");
3391 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
3393 /* convert lhs swizzle into writemask */
3394 GLuint writemask
, newSwizzle
;
3395 if (!swizzle_to_writemask(A
, lhs
->Store
->Swizzle
,
3396 &writemask
, &newSwizzle
)) {
3397 /* Non-simple writemask, need to swizzle right hand side in
3398 * order to put components into the right place.
3400 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
3402 n
= new_node2(IR_COPY
, lhs
, rhs
);
3413 * Generate IR tree for referencing a field in a struct (or basic vector type)
3415 static slang_ir_node
*
3416 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
3420 /* type of struct */
3421 slang_typeinfo_construct(&ti
);
3422 typeof_operation(A
, &oper
->children
[0], &ti
);
3424 if (_slang_type_is_vector(ti
.spec
.type
)) {
3425 /* the field should be a swizzle */
3426 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
3430 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3431 slang_info_log_error(A
->log
, "Bad swizzle");
3434 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3439 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3440 /* create new parent node with swizzle */
3442 n
= _slang_gen_swizzle(n
, swizzle
);
3445 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
3446 || ti
.spec
.type
== SLANG_SPEC_INT
3447 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
3448 const GLuint rows
= 1;
3452 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
3453 slang_info_log_error(A
->log
, "Bad swizzle");
3455 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
3459 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3460 /* create new parent node with swizzle */
3461 n
= _slang_gen_swizzle(n
, swizzle
);
3465 /* the field is a structure member (base.field) */
3466 /* oper->children[0] is the base */
3467 /* oper->a_id is the field name */
3468 slang_ir_node
*base
, *n
;
3469 slang_typeinfo field_ti
;
3470 GLint fieldSize
, fieldOffset
= -1;
3473 slang_typeinfo_construct(&field_ti
);
3474 typeof_operation(A
, oper
, &field_ti
);
3476 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
3478 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
3480 if (fieldSize
== 0 || fieldOffset
< 0) {
3481 const char *structName
;
3482 if (ti
.spec
._struct
)
3483 structName
= (char *) ti
.spec
._struct
->a_name
;
3485 structName
= "unknown";
3486 slang_info_log_error(A
->log
,
3487 "\"%s\" is not a member of struct \"%s\"",
3488 (char *) oper
->a_id
, structName
);
3491 assert(fieldSize
>= 0);
3493 base
= _slang_gen_operation(A
, &oper
->children
[0]);
3495 /* error msg should have already been logged */
3499 n
= new_node1(IR_FIELD
, base
);
3503 n
->Field
= (char *) oper
->a_id
;
3505 /* Store the field's offset in storage->Index */
3506 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
3516 * Gen code for array indexing.
3518 static slang_ir_node
*
3519 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
3521 slang_typeinfo array_ti
;
3523 /* get array's type info */
3524 slang_typeinfo_construct(&array_ti
);
3525 typeof_operation(A
, &oper
->children
[0], &array_ti
);
3527 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
3528 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
3529 /* translate the index into a swizzle/writemask: "v.x=p" */
3530 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
3534 index
= (GLint
) oper
->children
[1].literal
[0];
3535 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
3536 index
>= (GLint
) max
) {
3537 slang_info_log_error(A
->log
, "Invalid array index for vector type");
3541 n
= _slang_gen_operation(A
, &oper
->children
[0]);
3543 /* use swizzle to access the element */
3544 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
3548 n
= _slang_gen_swizzle(n
, swizzle
);
3554 /* conventional array */
3555 slang_typeinfo elem_ti
;
3556 slang_ir_node
*elem
, *array
, *index
;
3557 GLint elemSize
, arrayLen
;
3559 /* size of array element */
3560 slang_typeinfo_construct(&elem_ti
);
3561 typeof_operation(A
, oper
, &elem_ti
);
3562 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
3564 if (_slang_type_is_matrix(array_ti
.spec
.type
))
3565 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
3567 arrayLen
= array_ti
.array_len
;
3569 slang_typeinfo_destruct(&array_ti
);
3570 slang_typeinfo_destruct(&elem_ti
);
3572 if (elemSize
<= 0) {
3573 /* unknown var or type */
3574 slang_info_log_error(A
->log
, "Undefined variable or type");
3578 array
= _slang_gen_operation(A
, &oper
->children
[0]);
3579 index
= _slang_gen_operation(A
, &oper
->children
[1]);
3580 if (array
&& index
) {
3582 GLint constIndex
= -1;
3583 if (index
->Opcode
== IR_FLOAT
) {
3584 constIndex
= (int) index
->Value
[0];
3585 if (constIndex
< 0 || constIndex
>= arrayLen
) {
3586 slang_info_log_error(A
->log
,
3587 "Array index out of bounds (index=%d size=%d)",
3588 constIndex
, arrayLen
);
3589 _slang_free_ir_tree(array
);
3590 _slang_free_ir_tree(index
);
3595 if (!array
->Store
) {
3596 slang_info_log_error(A
->log
, "Invalid array");
3600 elem
= new_node2(IR_ELEMENT
, array
, index
);
3602 /* The storage info here will be updated during code emit */
3603 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
3604 array
->Store
->Index
,
3610 _slang_free_ir_tree(array
);
3611 _slang_free_ir_tree(index
);
3618 static slang_ir_node
*
3619 _slang_gen_compare(slang_assemble_ctx
*A
, slang_operation
*oper
,
3620 slang_ir_opcode opcode
)
3622 slang_typeinfo t0
, t1
;
3625 slang_typeinfo_construct(&t0
);
3626 typeof_operation(A
, &oper
->children
[0], &t0
);
3628 slang_typeinfo_construct(&t1
);
3629 typeof_operation(A
, &oper
->children
[0], &t1
);
3631 if (t0
.spec
.type
== SLANG_SPEC_ARRAY
||
3632 t1
.spec
.type
== SLANG_SPEC_ARRAY
) {
3633 slang_info_log_error(A
->log
, "Illegal array comparison");
3637 if (oper
->type
!= SLANG_OPER_EQUAL
&&
3638 oper
->type
!= SLANG_OPER_NOTEQUAL
) {
3639 /* <, <=, >, >= can only be used with scalars */
3640 if ((t0
.spec
.type
!= SLANG_SPEC_INT
&&
3641 t0
.spec
.type
!= SLANG_SPEC_FLOAT
) ||
3642 (t1
.spec
.type
!= SLANG_SPEC_INT
&&
3643 t1
.spec
.type
!= SLANG_SPEC_FLOAT
)) {
3644 slang_info_log_error(A
->log
, "Incompatible type(s) for inequality operator");
3649 n
= new_node2(opcode
,
3650 _slang_gen_operation(A
, &oper
->children
[0]),
3651 _slang_gen_operation(A
, &oper
->children
[1]));
3653 /* result is a bool (size 1) */
3654 n
->Store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, 1);
3662 print_vars(slang_variable_scope
*s
)
3666 for (i
= 0; i
< s
->num_variables
; i
++) {
3668 (char*) s
->variables
[i
]->a_name
,
3669 s
->variables
[i
]->declared
);
3679 _slang_undeclare_vars(slang_variable_scope
*locals
)
3681 if (locals
->num_variables
> 0) {
3683 for (i
= 0; i
< locals
->num_variables
; i
++) {
3684 slang_variable
*v
= locals
->variables
[i
];
3685 printf("undeclare %s at %p\n", (char*) v
->a_name
, v
);
3686 v
->declared
= GL_FALSE
;
3694 * Generate IR tree for a slang_operation (AST node)
3696 static slang_ir_node
*
3697 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
3699 switch (oper
->type
) {
3700 case SLANG_OPER_BLOCK_NEW_SCOPE
:
3704 _slang_push_var_table(A
->vartable
);
3706 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
3707 n
= _slang_gen_operation(A
, oper
);
3708 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
3710 _slang_pop_var_table(A
->vartable
);
3712 /*_slang_undeclare_vars(oper->locals);*/
3713 /*print_vars(oper->locals);*/
3716 n
= new_node1(IR_SCOPE
, n
);
3721 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
3722 /* list of operations */
3723 if (oper
->num_children
> 0)
3725 slang_ir_node
*n
, *tree
= NULL
;
3728 for (i
= 0; i
< oper
->num_children
; i
++) {
3729 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
3731 _slang_free_ir_tree(tree
);
3732 return NULL
; /* error must have occured */
3734 tree
= new_seq(tree
, n
);
3740 return new_node0(IR_NOP
);
3743 case SLANG_OPER_EXPRESSION
:
3744 return _slang_gen_operation(A
, &oper
->children
[0]);
3746 case SLANG_OPER_FOR
:
3747 return _slang_gen_for(A
, oper
);
3749 return _slang_gen_do(A
, oper
);
3750 case SLANG_OPER_WHILE
:
3751 return _slang_gen_while(A
, oper
);
3752 case SLANG_OPER_BREAK
:
3754 slang_info_log_error(A
->log
, "'break' not in loop");
3757 return new_break(A
->CurLoop
);
3758 case SLANG_OPER_CONTINUE
:
3760 slang_info_log_error(A
->log
, "'continue' not in loop");
3763 return _slang_gen_continue(A
, oper
);
3764 case SLANG_OPER_DISCARD
:
3765 return new_node0(IR_KILL
);
3767 case SLANG_OPER_EQUAL
:
3768 return _slang_gen_compare(A
, oper
, IR_EQUAL
);
3769 case SLANG_OPER_NOTEQUAL
:
3770 return _slang_gen_compare(A
, oper
, IR_NOTEQUAL
);
3771 case SLANG_OPER_GREATER
:
3772 return _slang_gen_compare(A
, oper
, IR_SGT
);
3773 case SLANG_OPER_LESS
:
3774 return _slang_gen_compare(A
, oper
, IR_SLT
);
3775 case SLANG_OPER_GREATEREQUAL
:
3776 return _slang_gen_compare(A
, oper
, IR_SGE
);
3777 case SLANG_OPER_LESSEQUAL
:
3778 return _slang_gen_compare(A
, oper
, IR_SLE
);
3779 case SLANG_OPER_ADD
:
3782 assert(oper
->num_children
== 2);
3783 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
3786 case SLANG_OPER_SUBTRACT
:
3789 assert(oper
->num_children
== 2);
3790 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
3793 case SLANG_OPER_MULTIPLY
:
3796 assert(oper
->num_children
== 2);
3797 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
3800 case SLANG_OPER_DIVIDE
:
3803 assert(oper
->num_children
== 2);
3804 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
3807 case SLANG_OPER_MINUS
:
3810 assert(oper
->num_children
== 1);
3811 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
3814 case SLANG_OPER_PLUS
:
3815 /* +expr --> do nothing */
3816 return _slang_gen_operation(A
, &oper
->children
[0]);
3817 case SLANG_OPER_VARIABLE_DECL
:
3818 return _slang_gen_declaration(A
, oper
);
3819 case SLANG_OPER_ASSIGN
:
3820 return _slang_gen_assignment(A
, oper
);
3821 case SLANG_OPER_ADDASSIGN
:
3824 assert(oper
->num_children
== 2);
3825 n
= _slang_gen_function_call_name(A
, "+=", oper
, NULL
);
3828 case SLANG_OPER_SUBASSIGN
:
3831 assert(oper
->num_children
== 2);
3832 n
= _slang_gen_function_call_name(A
, "-=", oper
, NULL
);
3836 case SLANG_OPER_MULASSIGN
:
3839 assert(oper
->num_children
== 2);
3840 n
= _slang_gen_function_call_name(A
, "*=", oper
, NULL
);
3843 case SLANG_OPER_DIVASSIGN
:
3846 assert(oper
->num_children
== 2);
3847 n
= _slang_gen_function_call_name(A
, "/=", oper
, NULL
);
3850 case SLANG_OPER_LOGICALAND
:
3853 assert(oper
->num_children
== 2);
3854 n
= _slang_gen_logical_and(A
, oper
);
3857 case SLANG_OPER_LOGICALOR
:
3860 assert(oper
->num_children
== 2);
3861 n
= _slang_gen_logical_or(A
, oper
);
3864 case SLANG_OPER_LOGICALXOR
:
3865 return _slang_gen_xor(A
, oper
);
3866 case SLANG_OPER_NOT
:
3867 return _slang_gen_not(A
, oper
);
3868 case SLANG_OPER_SELECT
: /* b ? x : y */
3871 assert(oper
->num_children
== 3);
3872 n
= _slang_gen_select(A
, oper
);
3876 case SLANG_OPER_ASM
:
3877 return _slang_gen_asm(A
, oper
, NULL
);
3878 case SLANG_OPER_CALL
:
3879 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
3881 case SLANG_OPER_METHOD
:
3882 return _slang_gen_method_call(A
, oper
);
3883 case SLANG_OPER_RETURN
:
3884 return _slang_gen_return(A
, oper
);
3885 case SLANG_OPER_LABEL
:
3886 return new_label(oper
->label
);
3887 case SLANG_OPER_IDENTIFIER
:
3888 return _slang_gen_variable(A
, oper
);
3890 return _slang_gen_if(A
, oper
);
3891 case SLANG_OPER_FIELD
:
3892 return _slang_gen_struct_field(A
, oper
);
3893 case SLANG_OPER_SUBSCRIPT
:
3894 return _slang_gen_array_element(A
, oper
);
3895 case SLANG_OPER_LITERAL_FLOAT
:
3897 case SLANG_OPER_LITERAL_INT
:
3899 case SLANG_OPER_LITERAL_BOOL
:
3900 return new_float_literal(oper
->literal
, oper
->literal_size
);
3902 case SLANG_OPER_POSTINCREMENT
: /* var++ */
3905 assert(oper
->num_children
== 1);
3906 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
3909 case SLANG_OPER_POSTDECREMENT
: /* var-- */
3912 assert(oper
->num_children
== 1);
3913 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
3916 case SLANG_OPER_PREINCREMENT
: /* ++var */
3919 assert(oper
->num_children
== 1);
3920 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
3923 case SLANG_OPER_PREDECREMENT
: /* --var */
3926 assert(oper
->num_children
== 1);
3927 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
3931 case SLANG_OPER_NON_INLINED_CALL
:
3932 case SLANG_OPER_SEQUENCE
:
3934 slang_ir_node
*tree
= NULL
;
3936 for (i
= 0; i
< oper
->num_children
; i
++) {
3937 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
3938 tree
= new_seq(tree
, n
);
3940 tree
->Store
= n
->Store
;
3942 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
3943 tree
= new_function_call(tree
, oper
->label
);
3948 case SLANG_OPER_NONE
:
3949 case SLANG_OPER_VOID
:
3950 /* returning NULL here would generate an error */
3951 return new_node0(IR_NOP
);
3954 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
3956 return new_node0(IR_NOP
);
3964 * Compute total size of array give size of element, number of elements.
3967 array_size(GLint baseSize
, GLint arrayLen
)
3971 /* round up base type to multiple of 4 */
3972 total
= ((baseSize
+ 3) & ~0x3) * MAX2(arrayLen
, 1);
3982 * Called by compiler when a global variable has been parsed/compiled.
3983 * Here we examine the variable's type to determine what kind of register
3984 * storage will be used.
3986 * A uniform such as "gl_Position" will become the register specification
3987 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
3988 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
3990 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
3991 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
3992 * actual texture unit (as specified by the user calling glUniform1i()).
3995 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
3996 slang_unit_type type
)
3998 struct gl_program
*prog
= A
->program
;
3999 const char *varName
= (char *) var
->a_name
;
4000 GLboolean success
= GL_TRUE
;
4001 slang_ir_storage
*store
= NULL
;
4003 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
4004 const GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
4005 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
4007 if (texIndex
!= -1) {
4008 /* This is a texture sampler variable...
4009 * store->File = PROGRAM_SAMPLER
4010 * store->Index = sampler number (0..7, typically)
4011 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
4013 if (var
->initializer
) {
4014 slang_info_log_error(A
->log
, "illegal assignment to '%s'", varName
);
4017 #if FEATURE_es2_glsl /* XXX should use FEATURE_texture_rect */
4018 /* disallow rect samplers */
4019 if (var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECT
||
4020 var
->type
.specifier
.type
== SLANG_SPEC_SAMPLER2DRECTSHADOW
) {
4021 slang_info_log_error(A
->log
, "invalid sampler type for '%s'", varName
);
4026 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
4027 store
= _slang_new_ir_storage(PROGRAM_SAMPLER
, sampNum
, texIndex
);
4029 if (dbg
) printf("SAMPLER ");
4031 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
4032 /* Uniform variable */
4033 const GLint totalSize
= array_size(size
, var
->array_len
);
4034 const GLuint swizzle
= _slang_var_swizzle(totalSize
, 0);
4037 /* user-defined uniform */
4038 if (datatype
== GL_NONE
) {
4039 if (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
) {
4040 /* temporary work-around */
4041 GLenum datatype
= GL_FLOAT
;
4042 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
4043 totalSize
, datatype
, NULL
);
4044 store
= _slang_new_ir_storage_swz(PROGRAM_UNIFORM
, uniformLoc
,
4045 totalSize
, swizzle
);
4047 /* XXX what we need to do is unroll the struct into its
4048 * basic types, creating a uniform variable for each.
4056 * Should produce uniforms:
4057 * "f.a" (GL_FLOAT_VEC3)
4058 * "f.b" (GL_FLOAT_VEC4)
4061 if (var
->initializer
) {
4062 slang_info_log_error(A
->log
,
4063 "unsupported initializer for uniform '%s'", varName
);
4068 slang_info_log_error(A
->log
,
4069 "invalid datatype for uniform variable %s",
4075 /* non-struct uniform */
4076 if (!_slang_gen_var_decl(A
, var
, var
->initializer
))
4082 /* pre-defined uniform, like gl_ModelviewMatrix */
4083 /* We know it's a uniform, but don't allocate storage unless
4086 store
= _slang_new_ir_storage_swz(PROGRAM_STATE_VAR
, -1,
4087 totalSize
, swizzle
);
4089 if (dbg
) printf("UNIFORM (sz %d) ", totalSize
);
4091 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
4092 const GLint totalSize
= array_size(size
, var
->array_len
);
4094 /* varyings must be float, vec or mat */
4095 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
) &&
4096 var
->type
.specifier
.type
!= SLANG_SPEC_ARRAY
) {
4097 slang_info_log_error(A
->log
,
4098 "varying '%s' must be float/vector/matrix",
4103 if (var
->initializer
) {
4104 slang_info_log_error(A
->log
, "illegal initializer for varying '%s'",
4110 /* user-defined varying */
4116 if (var
->type
.centroid
== SLANG_CENTROID
)
4117 flags
|= PROG_PARAM_BIT_CENTROID
;
4118 if (var
->type
.variant
== SLANG_INVARIANT
)
4119 flags
|= PROG_PARAM_BIT_INVARIANT
;
4121 varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
,
4123 swizzle
= _slang_var_swizzle(size
, 0);
4124 store
= _slang_new_ir_storage_swz(PROGRAM_VARYING
, varyingLoc
,
4125 totalSize
, swizzle
);
4128 /* pre-defined varying, like gl_Color or gl_TexCoord */
4129 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
4130 /* fragment program input */
4132 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4135 assert(index
< FRAG_ATTRIB_MAX
);
4136 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
,
4140 /* vertex program output */
4141 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4142 GLuint swizzle
= _slang_var_swizzle(size
, 0);
4144 assert(index
< VERT_RESULT_MAX
);
4145 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
4146 store
= _slang_new_ir_storage_swz(PROGRAM_OUTPUT
, index
,
4149 if (dbg
) printf("V/F ");
4151 if (dbg
) printf("VARYING ");
4153 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
4156 /* attributes must be float, vec or mat */
4157 if (!_slang_type_is_float_vec_mat(var
->type
.specifier
.type
)) {
4158 slang_info_log_error(A
->log
,
4159 "attribute '%s' must be float/vector/matrix",
4165 /* user-defined vertex attribute */
4166 const GLint attr
= -1; /* unknown */
4167 swizzle
= _slang_var_swizzle(size
, 0);
4168 index
= _mesa_add_attribute(prog
->Attributes
, varName
,
4169 size
, datatype
, attr
);
4171 index
= VERT_ATTRIB_GENERIC0
+ index
;
4174 /* pre-defined vertex attrib */
4175 index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
, &swizzle
);
4178 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4179 if (dbg
) printf("ATTRIB ");
4181 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
4182 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
4183 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
4185 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
4186 if (dbg
) printf("INPUT ");
4188 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
4189 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
4190 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
4191 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
4194 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
4195 GLint specialSize
= 4; /* treat all fragment outputs as float[4] */
4196 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
4197 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, specialSize
);
4199 if (dbg
) printf("OUTPUT ");
4201 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
4202 /* pre-defined global constant, like gl_MaxLights */
4203 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
4204 if (dbg
) printf("CONST ");
4207 /* ordinary variable (may be const) */
4210 /* IR node to declare the variable */
4211 n
= _slang_gen_var_decl(A
, var
, var
->initializer
);
4213 /* emit GPU instructions */
4214 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_FALSE
, A
->log
);
4216 _slang_free_ir_tree(n
);
4219 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
4220 store
? store
->Index
: -2);
4223 var
->store
= store
; /* save var's storage info */
4225 var
->declared
= GL_TRUE
;
4232 * Produce an IR tree from a function AST (fun->body).
4233 * Then call the code emitter to convert the IR tree into gl_program
4237 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
4240 GLboolean success
= GL_TRUE
;
4242 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
4243 /* we only really generate code for main, all other functions get
4244 * inlined or codegen'd upon an actual call.
4247 /* do some basic error checking though */
4248 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
4249 /* check that non-void functions actually return something */
4251 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
4253 slang_info_log_error(A
->log
,
4254 "function \"%s\" has no return statement",
4255 (char *) fun
->header
.a_name
);
4257 "function \"%s\" has no return statement\n",
4258 (char *) fun
->header
.a_name
);
4263 return GL_TRUE
; /* not an error */
4267 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
4268 slang_print_function(fun
, 1);
4271 /* should have been allocated earlier: */
4272 assert(A
->program
->Parameters
);
4273 assert(A
->program
->Varying
);
4274 assert(A
->vartable
);
4276 A
->CurFunction
= fun
;
4278 /* fold constant expressions, etc. */
4279 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
4282 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
4283 slang_print_function(fun
, 1);
4286 /* Create an end-of-function label */
4287 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
4289 /* push new vartable scope */
4290 _slang_push_var_table(A
->vartable
);
4292 /* Generate IR tree for the function body code */
4293 n
= _slang_gen_operation(A
, fun
->body
);
4295 n
= new_node1(IR_SCOPE
, n
);
4297 /* pop vartable, restore previous */
4298 _slang_pop_var_table(A
->vartable
);
4301 /* XXX record error */
4305 /* append an end-of-function-label to IR tree */
4306 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
4308 /*_slang_label_delete(A->curFuncEndLabel);*/
4309 A
->curFuncEndLabel
= NULL
;
4312 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
4313 slang_print_function(fun
, 1);
4316 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
4317 _slang_print_ir_tree(n
, 0);
4320 printf("************* End codegen function ************\n\n");
4323 /* Emit program instructions */
4324 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
, A
->log
);
4325 _slang_free_ir_tree(n
);
4327 /* free codegen context */
4329 _mesa_free(A->codegen);