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 is_sampler_type(const slang_fully_specified_type
*t
)
67 switch (t
->specifier
.type
) {
68 case SLANG_SPEC_SAMPLER1D
:
69 case SLANG_SPEC_SAMPLER2D
:
70 case SLANG_SPEC_SAMPLER3D
:
71 case SLANG_SPEC_SAMPLERCUBE
:
72 case SLANG_SPEC_SAMPLER1DSHADOW
:
73 case SLANG_SPEC_SAMPLER2DSHADOW
:
74 case SLANG_SPEC_SAMPLER2DRECT
:
75 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
84 * Return the offset (in floats or ints) of the named field within
85 * the given struct. Return -1 if field not found.
86 * If field is NULL, return the size of the struct instead.
89 _slang_field_offset(const slang_type_specifier
*spec
, slang_atom field
)
93 for (i
= 0; i
< spec
->_struct
->fields
->num_variables
; i
++) {
94 const slang_variable
*v
= spec
->_struct
->fields
->variables
[i
];
95 const GLuint sz
= _slang_sizeof_type_specifier(&v
->type
.specifier
);
97 /* types larger than 1 float are register (4-float) aligned */
98 offset
= (offset
+ 3) & ~3;
100 if (field
&& v
->a_name
== field
) {
106 return -1; /* field not found */
108 return offset
; /* struct size */
113 * Return the size (in floats) of the given type specifier.
114 * If the size is greater than 4, the size should be a multiple of 4
115 * so that the correct number of 4-float registers are allocated.
116 * For example, a mat3x2 is size 12 because we want to store the
117 * 3 columns in 3 float[4] registers.
120 _slang_sizeof_type_specifier(const slang_type_specifier
*spec
)
123 switch (spec
->type
) {
124 case SLANG_SPEC_VOID
:
127 case SLANG_SPEC_BOOL
:
130 case SLANG_SPEC_BVEC2
:
133 case SLANG_SPEC_BVEC3
:
136 case SLANG_SPEC_BVEC4
:
142 case SLANG_SPEC_IVEC2
:
145 case SLANG_SPEC_IVEC3
:
148 case SLANG_SPEC_IVEC4
:
151 case SLANG_SPEC_FLOAT
:
154 case SLANG_SPEC_VEC2
:
157 case SLANG_SPEC_VEC3
:
160 case SLANG_SPEC_VEC4
:
163 case SLANG_SPEC_MAT2
:
164 sz
= 2 * 4; /* 2 columns (regs) */
166 case SLANG_SPEC_MAT3
:
169 case SLANG_SPEC_MAT4
:
172 case SLANG_SPEC_MAT23
:
173 sz
= 2 * 4; /* 2 columns (regs) */
175 case SLANG_SPEC_MAT32
:
176 sz
= 3 * 4; /* 3 columns (regs) */
178 case SLANG_SPEC_MAT24
:
181 case SLANG_SPEC_MAT42
:
182 sz
= 4 * 4; /* 4 columns (regs) */
184 case SLANG_SPEC_MAT34
:
187 case SLANG_SPEC_MAT43
:
188 sz
= 4 * 4; /* 4 columns (regs) */
190 case SLANG_SPEC_SAMPLER1D
:
191 case SLANG_SPEC_SAMPLER2D
:
192 case SLANG_SPEC_SAMPLER3D
:
193 case SLANG_SPEC_SAMPLERCUBE
:
194 case SLANG_SPEC_SAMPLER1DSHADOW
:
195 case SLANG_SPEC_SAMPLER2DSHADOW
:
196 case SLANG_SPEC_SAMPLER2DRECT
:
197 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
198 sz
= 1; /* a sampler is basically just an integer index */
200 case SLANG_SPEC_STRUCT
:
201 sz
= _slang_field_offset(spec
, 0); /* special use */
203 sz
= (sz
+ 3) & ~0x3; /* round up to multiple of four */
206 case SLANG_SPEC_ARRAY
:
207 sz
= _slang_sizeof_type_specifier(spec
->_array
);
210 _mesa_problem(NULL
, "Unexpected type in _slang_sizeof_type_specifier()");
215 /* if size is > 4, it should be a multiple of four */
216 assert((sz
& 0x3) == 0);
223 * Establish the binding between a slang_ir_node and a slang_variable.
224 * Then, allocate/attach a slang_ir_storage object to the IR node if needed.
225 * The IR node must be a IR_VAR or IR_VAR_DECL node.
226 * \param n the IR node
227 * \param var the variable to associate with the IR node
230 _slang_attach_storage(slang_ir_node
*n
, slang_variable
*var
)
234 assert(n
->Opcode
== IR_VAR
|| n
->Opcode
== IR_VAR_DECL
);
235 assert(!n
->Var
|| n
->Var
== var
);
240 /* need to setup storage */
241 if (n
->Var
&& n
->Var
->aux
) {
242 /* node storage info = var storage info */
243 n
->Store
= (slang_ir_storage
*) n
->Var
->aux
;
246 /* alloc new storage info */
247 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -7, -5);
249 printf("%s var=%s Store=%p Size=%d\n", __FUNCTION__
,
251 (void*) n
->Store
, n
->Store
->Size
);
254 n
->Var
->aux
= n
->Store
;
262 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
263 * or -1 if the type is not a sampler.
266 sampler_to_texture_index(const slang_type_specifier_type type
)
269 case SLANG_SPEC_SAMPLER1D
:
270 return TEXTURE_1D_INDEX
;
271 case SLANG_SPEC_SAMPLER2D
:
272 return TEXTURE_2D_INDEX
;
273 case SLANG_SPEC_SAMPLER3D
:
274 return TEXTURE_3D_INDEX
;
275 case SLANG_SPEC_SAMPLERCUBE
:
276 return TEXTURE_CUBE_INDEX
;
277 case SLANG_SPEC_SAMPLER1DSHADOW
:
278 return TEXTURE_1D_INDEX
; /* XXX fix */
279 case SLANG_SPEC_SAMPLER2DSHADOW
:
280 return TEXTURE_2D_INDEX
; /* XXX fix */
281 case SLANG_SPEC_SAMPLER2DRECT
:
282 return TEXTURE_RECT_INDEX
;
283 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
284 return TEXTURE_RECT_INDEX
; /* XXX fix */
291 #define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
294 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
295 * a vertex or fragment program input variable. Return -1 if the input
297 * XXX return size too
300 _slang_input_index(const char *name
, GLenum target
, GLuint
*swizzleOut
)
307 static const struct input_info vertInputs
[] = {
308 { "gl_Vertex", VERT_ATTRIB_POS
, SWIZZLE_NOOP
},
309 { "gl_Normal", VERT_ATTRIB_NORMAL
, SWIZZLE_NOOP
},
310 { "gl_Color", VERT_ATTRIB_COLOR0
, SWIZZLE_NOOP
},
311 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
, SWIZZLE_NOOP
},
312 { "gl_FogCoord", VERT_ATTRIB_FOG
, SWIZZLE_XXXX
},
313 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
, SWIZZLE_NOOP
},
314 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
, SWIZZLE_NOOP
},
315 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
, SWIZZLE_NOOP
},
316 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
, SWIZZLE_NOOP
},
317 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
, SWIZZLE_NOOP
},
318 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
, SWIZZLE_NOOP
},
319 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
, SWIZZLE_NOOP
},
320 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
, SWIZZLE_NOOP
},
321 { NULL
, 0, SWIZZLE_NOOP
}
323 static const struct input_info fragInputs
[] = {
324 { "gl_FragCoord", FRAG_ATTRIB_WPOS
, SWIZZLE_NOOP
},
325 { "gl_Color", FRAG_ATTRIB_COL0
, SWIZZLE_NOOP
},
326 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
, SWIZZLE_NOOP
},
327 { "gl_TexCoord", FRAG_ATTRIB_TEX0
, SWIZZLE_NOOP
},
328 /* note: we're packing several quantities into the fogcoord vector */
329 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_XXXX
},
330 { "gl_FrontFacing", FRAG_ATTRIB_FOGC
, SWIZZLE_YYYY
}, /*XXX*/
331 { "gl_PointCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_ZWWW
},
332 { NULL
, 0, SWIZZLE_NOOP
}
335 const struct input_info
*inputs
336 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertInputs
: fragInputs
;
338 ASSERT(MAX_TEXTURE_UNITS
== 8); /* if this fails, fix vertInputs above */
340 for (i
= 0; inputs
[i
].Name
; i
++) {
341 if (strcmp(inputs
[i
].Name
, name
) == 0) {
343 *swizzleOut
= inputs
[i
].Swizzle
;
344 return inputs
[i
].Attrib
;
352 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
353 * a vertex or fragment program output variable. Return -1 for an invalid
357 _slang_output_index(const char *name
, GLenum target
)
363 static const struct output_info vertOutputs
[] = {
364 { "gl_Position", VERT_RESULT_HPOS
},
365 { "gl_FrontColor", VERT_RESULT_COL0
},
366 { "gl_BackColor", VERT_RESULT_BFC0
},
367 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
},
368 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
},
369 { "gl_TexCoord", VERT_RESULT_TEX0
},
370 { "gl_FogFragCoord", VERT_RESULT_FOGC
},
371 { "gl_PointSize", VERT_RESULT_PSIZ
},
374 static const struct output_info fragOutputs
[] = {
375 { "gl_FragColor", FRAG_RESULT_COLR
},
376 { "gl_FragDepth", FRAG_RESULT_DEPR
},
377 { "gl_FragData", FRAG_RESULT_DATA0
},
381 const struct output_info
*outputs
382 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertOutputs
: fragOutputs
;
384 for (i
= 0; outputs
[i
].Name
; i
++) {
385 if (strcmp(outputs
[i
].Name
, name
) == 0) {
387 return outputs
[i
].Attrib
;
395 /**********************************************************************/
399 * Map "_asm foo" to IR_FOO, etc.
404 slang_ir_opcode Opcode
;
405 GLuint HaveRetValue
, NumParams
;
409 static slang_asm_info AsmInfo
[] = {
411 { "vec4_add", IR_ADD
, 1, 2 },
412 { "vec4_subtract", IR_SUB
, 1, 2 },
413 { "vec4_multiply", IR_MUL
, 1, 2 },
414 { "vec4_dot", IR_DOT4
, 1, 2 },
415 { "vec3_dot", IR_DOT3
, 1, 2 },
416 { "vec3_cross", IR_CROSS
, 1, 2 },
417 { "vec4_lrp", IR_LRP
, 1, 3 },
418 { "vec4_min", IR_MIN
, 1, 2 },
419 { "vec4_max", IR_MAX
, 1, 2 },
420 { "vec4_clamp", IR_CLAMP
, 1, 3 },
421 { "vec4_seq", IR_SEQUAL
, 1, 2 },
422 { "vec4_sne", IR_SNEQUAL
, 1, 2 },
423 { "vec4_sge", IR_SGE
, 1, 2 },
424 { "vec4_sgt", IR_SGT
, 1, 2 },
425 { "vec4_sle", IR_SLE
, 1, 2 },
426 { "vec4_slt", IR_SLT
, 1, 2 },
428 { "vec4_floor", IR_FLOOR
, 1, 1 },
429 { "vec4_frac", IR_FRAC
, 1, 1 },
430 { "vec4_abs", IR_ABS
, 1, 1 },
431 { "vec4_negate", IR_NEG
, 1, 1 },
432 { "vec4_ddx", IR_DDX
, 1, 1 },
433 { "vec4_ddy", IR_DDY
, 1, 1 },
434 /* float binary op */
435 { "float_power", IR_POW
, 1, 2 },
436 /* texture / sampler */
437 { "vec4_tex1d", IR_TEX
, 1, 2 },
438 { "vec4_texb1d", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
439 { "vec4_texp1d", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
440 { "vec4_tex2d", IR_TEX
, 1, 2 },
441 { "vec4_texb2d", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
442 { "vec4_texp2d", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
443 { "vec4_tex3d", IR_TEX
, 1, 2 },
444 { "vec4_texb3d", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
445 { "vec4_texp3d", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
446 { "vec4_texcube", IR_TEX
, 1, 2 }, /* cubemap */
447 { "vec4_tex_rect", IR_TEX
, 1, 2 }, /* rectangle */
448 { "vec4_texp_rect", IR_TEX
, 1, 2 },/* rectangle w/ projection */
451 { "int_to_float", IR_I_TO_F
, 1, 1 },
452 { "float_to_int", IR_F_TO_I
, 1, 1 },
453 { "float_exp", IR_EXP
, 1, 1 },
454 { "float_exp2", IR_EXP2
, 1, 1 },
455 { "float_log2", IR_LOG2
, 1, 1 },
456 { "float_rsq", IR_RSQ
, 1, 1 },
457 { "float_rcp", IR_RCP
, 1, 1 },
458 { "float_sine", IR_SIN
, 1, 1 },
459 { "float_cosine", IR_COS
, 1, 1 },
460 { "float_noise1", IR_NOISE1
, 1, 1},
461 { "float_noise2", IR_NOISE2
, 1, 1},
462 { "float_noise3", IR_NOISE3
, 1, 1},
463 { "float_noise4", IR_NOISE4
, 1, 1},
465 { NULL
, IR_NOP
, 0, 0 }
469 static slang_ir_node
*
470 new_node3(slang_ir_opcode op
,
471 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
473 slang_ir_node
*n
= (slang_ir_node
*) _slang_alloc(sizeof(slang_ir_node
));
479 n
->Writemask
= WRITEMASK_XYZW
;
480 n
->InstLocation
= -1;
485 static slang_ir_node
*
486 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
488 return new_node3(op
, c0
, c1
, NULL
);
491 static slang_ir_node
*
492 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
494 return new_node3(op
, c0
, NULL
, NULL
);
497 static slang_ir_node
*
498 new_node0(slang_ir_opcode op
)
500 return new_node3(op
, NULL
, NULL
, NULL
);
505 * Create sequence of two nodes.
507 static slang_ir_node
*
508 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
514 return new_node2(IR_SEQ
, left
, right
);
517 static slang_ir_node
*
518 new_label(slang_label
*label
)
520 slang_ir_node
*n
= new_node0(IR_LABEL
);
527 static slang_ir_node
*
528 new_float_literal(const float v
[4], GLuint size
)
530 slang_ir_node
*n
= new_node0(IR_FLOAT
);
532 COPY_4V(n
->Value
, v
);
533 /* allocate a storage object, but compute actual location (Index) later */
534 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
539 static slang_ir_node
*
540 new_not(slang_ir_node
*n
)
542 return new_node1(IR_NOT
, n
);
547 * Non-inlined function call.
549 static slang_ir_node
*
550 new_function_call(slang_ir_node
*code
, slang_label
*name
)
552 slang_ir_node
*n
= new_node1(IR_CALL
, code
);
561 * Unconditional jump.
563 static slang_ir_node
*
564 new_return(slang_label
*dest
)
566 slang_ir_node
*n
= new_node0(IR_RETURN
);
574 static slang_ir_node
*
575 new_loop(slang_ir_node
*body
)
577 return new_node1(IR_LOOP
, body
);
581 static slang_ir_node
*
582 new_break(slang_ir_node
*loopNode
)
584 slang_ir_node
*n
= new_node0(IR_BREAK
);
586 assert(loopNode
->Opcode
== IR_LOOP
);
588 /* insert this node at head of linked list */
589 n
->List
= loopNode
->List
;
597 * Make new IR_BREAK_IF_TRUE.
599 static slang_ir_node
*
600 new_break_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
604 assert(loopNode
->Opcode
== IR_LOOP
);
605 n
= new_node1(IR_BREAK_IF_TRUE
, cond
);
607 /* insert this node at head of linked list */
608 n
->List
= loopNode
->List
;
616 * Make new IR_CONT_IF_TRUE node.
618 static slang_ir_node
*
619 new_cont_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
623 assert(loopNode
->Opcode
== IR_LOOP
);
624 n
= new_node1(IR_CONT_IF_TRUE
, cond
);
626 /* insert this node at head of linked list */
627 n
->List
= loopNode
->List
;
634 static slang_ir_node
*
635 new_cond(slang_ir_node
*n
)
637 slang_ir_node
*c
= new_node1(IR_COND
, n
);
642 static slang_ir_node
*
643 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
645 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
650 * New IR_VAR node - a reference to a previously declared variable.
652 static slang_ir_node
*
653 new_var(slang_assemble_ctx
*A
, slang_operation
*oper
, slang_atom name
)
656 slang_variable
*var
= _slang_locate_variable(oper
->locals
, name
, GL_TRUE
);
660 assert(var
->declared
);
662 assert(!oper
->var
|| oper
->var
== var
);
664 n
= new_node0(IR_VAR
);
666 _slang_attach_storage(n
, var
);
668 printf("new_var %s store=%p\n", (char*)name, (void*) n->Store);
676 * Check if the given function is really just a wrapper for a
677 * basic assembly instruction.
680 slang_is_asm_function(const slang_function
*fun
)
682 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
683 fun
->body
->num_children
== 1 &&
684 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
692 _slang_is_noop(const slang_operation
*oper
)
695 oper
->type
== SLANG_OPER_VOID
||
696 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
704 * Recursively search tree for a node of the given type.
706 static slang_operation
*
707 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
710 if (oper
->type
== type
)
712 for (i
= 0; i
< oper
->num_children
; i
++) {
713 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
722 * Count the number of operations of the given time rooted at 'oper'.
725 _slang_count_node_type(slang_operation
*oper
, slang_operation_type type
)
728 if (oper
->type
== type
) {
731 for (i
= 0; i
< oper
->num_children
; i
++) {
732 count
+= _slang_count_node_type(&oper
->children
[i
], type
);
739 * Check if the 'return' statement found under 'oper' is a "tail return"
740 * that can be no-op'd. For example:
745 * return; // this is a no-op
748 * This is used when determining if a function can be inlined. If the
749 * 'return' is not the last statement, we can't inline the function since
750 * we still need the semantic behaviour of the 'return' but we don't want
751 * to accidentally return from the _calling_ function. We'd need to use an
752 * unconditional branch, but we don't have such a GPU instruction (not
756 _slang_is_tail_return(const slang_operation
*oper
)
758 GLuint k
= oper
->num_children
;
761 const slang_operation
*last
= &oper
->children
[k
- 1];
762 if (last
->type
== SLANG_OPER_RETURN
)
764 else if (last
->type
== SLANG_OPER_IDENTIFIER
||
765 last
->type
== SLANG_OPER_LABEL
)
766 k
--; /* try prev child */
767 else if (last
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
||
768 last
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
)
769 /* try sub-children */
770 return _slang_is_tail_return(last
);
780 slang_resolve_variable(slang_operation
*oper
)
782 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
783 oper
->var
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
789 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
792 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
793 GLuint substCount
, slang_variable
**substOld
,
794 slang_operation
**substNew
, GLboolean isLHS
)
796 switch (oper
->type
) {
797 case SLANG_OPER_VARIABLE_DECL
:
799 slang_variable
*v
= _slang_locate_variable(oper
->locals
,
800 oper
->a_id
, GL_TRUE
);
802 if (v
->initializer
&& oper
->num_children
== 0) {
803 /* set child of oper to copy of initializer */
804 oper
->num_children
= 1;
805 oper
->children
= slang_operation_new(1);
806 slang_operation_copy(&oper
->children
[0], v
->initializer
);
808 if (oper
->num_children
== 1) {
809 /* the initializer */
810 slang_substitute(A
, &oper
->children
[0], substCount
,
811 substOld
, substNew
, GL_FALSE
);
815 case SLANG_OPER_IDENTIFIER
:
816 assert(oper
->num_children
== 0);
817 if (1/**!isLHS XXX FIX */) {
818 slang_atom id
= oper
->a_id
;
821 v
= _slang_locate_variable(oper
->locals
, id
, GL_TRUE
);
823 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
827 /* look for a substitution */
828 for (i
= 0; i
< substCount
; i
++) {
829 if (v
== substOld
[i
]) {
830 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
831 #if 0 /* DEBUG only */
832 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
833 assert(substNew
[i
]->var
);
834 assert(substNew
[i
]->var
->a_name
);
835 printf("Substitute %s with %s in id node %p\n",
836 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
840 printf("Substitute %s with %f in id node %p\n",
841 (char*)v
->a_name
, substNew
[i
]->literal
[0],
845 slang_operation_copy(oper
, substNew
[i
]);
852 case SLANG_OPER_RETURN
:
853 /* do return replacement here too */
854 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
855 if (oper
->num_children
== 1 && !_slang_is_noop(&oper
->children
[0])) {
861 * then do substitutions on the assignment.
863 slang_operation
*blockOper
, *assignOper
, *returnOper
;
865 /* check if function actually has a return type */
866 assert(A
->CurFunction
);
867 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
868 slang_info_log_error(A
->log
, "illegal return expression");
872 blockOper
= slang_operation_new(1);
873 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
874 blockOper
->num_children
= 2;
875 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
876 blockOper
->children
= slang_operation_new(2);
877 assignOper
= blockOper
->children
+ 0;
878 returnOper
= blockOper
->children
+ 1;
880 assignOper
->type
= SLANG_OPER_ASSIGN
;
881 assignOper
->num_children
= 2;
882 assignOper
->locals
->outer_scope
= blockOper
->locals
;
883 assignOper
->children
= slang_operation_new(2);
884 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
885 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
886 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
888 slang_operation_copy(&assignOper
->children
[1],
891 returnOper
->type
= SLANG_OPER_RETURN
; /* return w/ no value */
892 assert(returnOper
->num_children
== 0);
894 /* do substitutions on the "__retVal = expr" sub-tree */
895 slang_substitute(A
, assignOper
,
896 substCount
, substOld
, substNew
, GL_FALSE
);
898 /* install new code */
899 slang_operation_copy(oper
, blockOper
);
900 slang_operation_destruct(blockOper
);
903 /* check if return value was expected */
904 assert(A
->CurFunction
);
905 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
906 slang_info_log_error(A
->log
, "return statement requires an expression");
912 case SLANG_OPER_ASSIGN
:
913 case SLANG_OPER_SUBSCRIPT
:
915 * child[0] can't have substitutions but child[1] can.
917 slang_substitute(A
, &oper
->children
[0],
918 substCount
, substOld
, substNew
, GL_TRUE
);
919 slang_substitute(A
, &oper
->children
[1],
920 substCount
, substOld
, substNew
, GL_FALSE
);
922 case SLANG_OPER_FIELD
:
924 slang_substitute(A
, &oper
->children
[0],
925 substCount
, substOld
, substNew
, GL_TRUE
);
930 for (i
= 0; i
< oper
->num_children
; i
++)
931 slang_substitute(A
, &oper
->children
[i
],
932 substCount
, substOld
, substNew
, GL_FALSE
);
939 * Produce inline code for a call to an assembly instruction.
940 * This is typically used to compile a call to a built-in function like this:
942 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
944 * __asm vec4_lrp __retVal, a, y, x;
949 * r = mix(p1, p2, p3);
959 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
961 static slang_operation
*
962 slang_inline_asm_function(slang_assemble_ctx
*A
,
963 slang_function
*fun
, slang_operation
*oper
)
965 const GLuint numArgs
= oper
->num_children
;
967 slang_operation
*inlined
;
968 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
969 slang_variable
**substOld
;
970 slang_operation
**substNew
;
972 ASSERT(slang_is_asm_function(fun
));
973 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
976 printf("Inline %s as %s\n",
977 (char*) fun->header.a_name,
978 (char*) fun->body->children[0].a_id);
982 * We'll substitute formal params with actual args in the asm call.
984 substOld
= (slang_variable
**)
985 _slang_alloc(numArgs
* sizeof(slang_variable
*));
986 substNew
= (slang_operation
**)
987 _slang_alloc(numArgs
* sizeof(slang_operation
*));
988 for (i
= 0; i
< numArgs
; i
++) {
989 substOld
[i
] = fun
->parameters
->variables
[i
];
990 substNew
[i
] = oper
->children
+ i
;
993 /* make a copy of the code to inline */
994 inlined
= slang_operation_new(1);
995 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
997 /* get rid of the __retVal child */
998 inlined
->num_children
--;
999 for (i
= 0; i
< inlined
->num_children
; i
++) {
1000 inlined
->children
[i
] = inlined
->children
[i
+ 1];
1004 /* now do formal->actual substitutions */
1005 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
1007 _slang_free(substOld
);
1008 _slang_free(substNew
);
1015 * Inline the given function call operation.
1016 * Return a new slang_operation that corresponds to the inlined code.
1018 static slang_operation
*
1019 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
1020 slang_operation
*oper
, slang_operation
*returnOper
)
1027 ParamMode
*paramMode
;
1028 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
1029 const GLuint numArgs
= oper
->num_children
;
1030 const GLuint totalArgs
= numArgs
+ haveRetValue
;
1031 slang_operation
*args
= oper
->children
;
1032 slang_operation
*inlined
, *top
;
1033 slang_variable
**substOld
;
1034 slang_operation
**substNew
;
1035 GLuint substCount
, numCopyIn
, i
;
1036 slang_function
*prevFunction
;
1039 prevFunction
= A
->CurFunction
;
1040 A
->CurFunction
= fun
;
1042 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
1043 assert(fun
->param_count
== totalArgs
);
1045 /* allocate temporary arrays */
1046 paramMode
= (ParamMode
*)
1047 _slang_alloc(totalArgs
* sizeof(ParamMode
));
1048 substOld
= (slang_variable
**)
1049 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
1050 substNew
= (slang_operation
**)
1051 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
1054 printf("Inline call to %s (total vars=%d nparams=%d)\n",
1055 (char *) fun
->header
.a_name
,
1056 fun
->parameters
->num_variables
, numArgs
);
1059 if (haveRetValue
&& !returnOper
) {
1060 /* Create 3-child comma sequence for inlined code:
1061 * child[0]: declare __resultTmp
1062 * child[1]: inlined function body
1063 * child[2]: __resultTmp
1065 slang_operation
*commaSeq
;
1066 slang_operation
*declOper
= NULL
;
1067 slang_variable
*resultVar
;
1069 commaSeq
= slang_operation_new(1);
1070 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
1071 assert(commaSeq
->locals
);
1072 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1073 commaSeq
->num_children
= 3;
1074 commaSeq
->children
= slang_operation_new(3);
1075 /* allocate the return var */
1076 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
1078 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
1079 (void*)commaSeq->locals, (char *) fun->header.a_name);
1082 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1083 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1084 resultVar
->isTemp
= GL_TRUE
;
1086 /* child[0] = __resultTmp declaration */
1087 declOper
= &commaSeq
->children
[0];
1088 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1089 declOper
->a_id
= resultVar
->a_name
;
1090 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1092 /* child[1] = function body */
1093 inlined
= &commaSeq
->children
[1];
1094 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1096 /* child[2] = __resultTmp reference */
1097 returnOper
= &commaSeq
->children
[2];
1098 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1099 returnOper
->a_id
= resultVar
->a_name
;
1100 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1105 top
= inlined
= slang_operation_new(1);
1106 /* XXXX this may be inappropriate!!!! */
1107 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1111 assert(inlined
->locals
);
1113 /* Examine the parameters, look for inout/out params, look for possible
1114 * substitutions, etc:
1115 * param type behaviour
1116 * in copy actual to local
1117 * const in substitute param with actual
1121 for (i
= 0; i
< totalArgs
; i
++) {
1122 slang_variable
*p
= fun
->parameters
->variables
[i
];
1124 printf("Param %d: %s %s \n", i,
1125 slang_type_qual_string(p->type.qualifier),
1126 (char *) p->a_name);
1128 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1129 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1130 /* an output param */
1131 slang_operation
*arg
;
1136 paramMode
[i
] = SUBST
;
1138 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1139 slang_resolve_variable(arg
);
1141 /* replace parameter 'p' with argument 'arg' */
1142 substOld
[substCount
] = p
;
1143 substNew
[substCount
] = arg
; /* will get copied */
1146 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1147 /* a constant input param */
1148 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1149 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
1150 /* replace all occurances of this parameter variable with the
1151 * actual argument variable or a literal.
1153 paramMode
[i
] = SUBST
;
1154 slang_resolve_variable(&args
[i
]);
1155 substOld
[substCount
] = p
;
1156 substNew
[substCount
] = &args
[i
]; /* will get copied */
1160 paramMode
[i
] = COPY_IN
;
1164 paramMode
[i
] = COPY_IN
;
1166 assert(paramMode
[i
]);
1169 /* actual code inlining: */
1170 slang_operation_copy(inlined
, fun
->body
);
1172 /*** XXX review this */
1173 assert(inlined
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
);
1174 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1177 printf("======================= orig body code ======================\n");
1178 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1179 slang_print_tree(fun
->body
, 8);
1180 printf("======================= copied code =========================\n");
1181 slang_print_tree(inlined
, 8);
1184 /* do parameter substitution in inlined code: */
1185 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1188 printf("======================= subst code ==========================\n");
1189 slang_print_tree(inlined
, 8);
1190 printf("=============================================================\n");
1193 /* New prolog statements: (inserted before the inlined code)
1194 * Copy the 'in' arguments.
1197 for (i
= 0; i
< numArgs
; i
++) {
1198 if (paramMode
[i
] == COPY_IN
) {
1199 slang_variable
*p
= fun
->parameters
->variables
[i
];
1200 /* declare parameter 'p' */
1201 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1205 printf("COPY_IN %s from expr\n", (char*)p->a_name);
1207 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1208 assert(decl
->locals
);
1209 decl
->locals
->outer_scope
= inlined
->locals
;
1210 decl
->a_id
= p
->a_name
;
1211 decl
->num_children
= 1;
1212 decl
->children
= slang_operation_new(1);
1214 /* child[0] is the var's initializer */
1215 slang_operation_copy(&decl
->children
[0], args
+ i
);
1221 /* Now add copies of the function's local vars to the new variable scope */
1222 for (i
= totalArgs
; i
< fun
->parameters
->num_variables
; i
++) {
1223 slang_variable
*p
= fun
->parameters
->variables
[i
];
1224 slang_variable
*pCopy
= slang_variable_scope_grow(inlined
->locals
);
1225 pCopy
->type
= p
->type
;
1226 pCopy
->a_name
= p
->a_name
;
1227 pCopy
->array_len
= p
->array_len
;
1231 /* New epilog statements:
1232 * 1. Create end of function label to jump to from return statements.
1233 * 2. Copy the 'out' parameter vars
1236 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1238 inlined
->num_children
);
1239 lab
->type
= SLANG_OPER_LABEL
;
1240 lab
->label
= A
->curFuncEndLabel
;
1243 for (i
= 0; i
< totalArgs
; i
++) {
1244 if (paramMode
[i
] == COPY_OUT
) {
1245 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1246 /* actualCallVar = outParam */
1247 /*if (i > 0 || !haveRetValue)*/
1248 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1250 inlined
->num_children
);
1251 ass
->type
= SLANG_OPER_ASSIGN
;
1252 ass
->num_children
= 2;
1253 ass
->locals
->outer_scope
= inlined
->locals
;
1254 ass
->children
= slang_operation_new(2);
1255 ass
->children
[0] = args
[i
]; /*XXX copy */
1256 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1257 ass
->children
[1].a_id
= p
->a_name
;
1258 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1262 _slang_free(paramMode
);
1263 _slang_free(substOld
);
1264 _slang_free(substNew
);
1267 printf("Done Inline call to %s (total vars=%d nparams=%d)\n",
1268 (char *) fun
->header
.a_name
,
1269 fun
->parameters
->num_variables
, numArgs
);
1270 slang_print_tree(top
, 0);
1274 A
->CurFunction
= prevFunction
;
1280 static slang_ir_node
*
1281 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1282 slang_operation
*oper
, slang_operation
*dest
)
1285 slang_operation
*inlined
;
1286 slang_label
*prevFuncEndLabel
;
1289 prevFuncEndLabel
= A
->curFuncEndLabel
;
1290 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1291 A
->curFuncEndLabel
= _slang_label_new(name
);
1292 assert(A
->curFuncEndLabel
);
1294 if (slang_is_asm_function(fun
) && !dest
) {
1295 /* assemble assembly function - tree style */
1296 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1299 /* non-assembly function */
1300 /* We always generate an "inline-able" block of code here.
1302 * 1. insert the inline code
1303 * 2. Generate a call to the "inline" code as a subroutine
1307 slang_operation
*ret
= NULL
;
1309 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1313 ret
= _slang_find_node_type(inlined
, SLANG_OPER_RETURN
);
1315 /* check if this is a "tail" return */
1316 if (_slang_count_node_type(inlined
, SLANG_OPER_RETURN
) == 1 &&
1317 _slang_is_tail_return(inlined
)) {
1318 /* The only RETURN is the last stmt in the function, no-op it
1319 * and inline the function body.
1321 ret
->type
= SLANG_OPER_NONE
;
1324 slang_operation
*callOper
;
1325 /* The function we're calling has one or more 'return' statements.
1326 * So, we can't truly inline this function because we need to
1327 * implement 'return' with RET (and CAL).
1328 * Nevertheless, we performed "inlining" to make a new instance
1329 * of the function body to deal with static register allocation.
1331 * XXX check if there's one 'return' and if it's the very last
1332 * statement in the function - we can optimize that case.
1334 assert(inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1335 inlined
->type
== SLANG_OPER_SEQUENCE
);
1337 if (_slang_function_has_return_value(fun
) && !dest
) {
1338 assert(inlined
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1339 assert(inlined
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1340 callOper
= &inlined
->children
[1];
1345 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1346 callOper
->fun
= fun
;
1347 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1355 /* Replace the function call with the inlined block (or new CALL stmt) */
1356 slang_operation_destruct(oper
);
1358 _slang_free(inlined
);
1361 assert(inlined
->locals
);
1362 printf("*** Inlined code for call to %s:\n",
1363 (char*) fun
->header
.a_name
);
1364 slang_print_tree(oper
, 10);
1368 n
= _slang_gen_operation(A
, oper
);
1370 /*_slang_label_delete(A->curFuncEndLabel);*/
1371 A
->curFuncEndLabel
= prevFuncEndLabel
;
1377 static slang_asm_info
*
1378 slang_find_asm_info(const char *name
)
1381 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1382 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1391 * Some write-masked assignments are simple, but others are hard.
1394 * v.xy = vec2(a, b);
1397 * v.zy = vec2(a, b);
1398 * this gets transformed/swizzled into:
1399 * v.zy = vec2(a, b).*yx* (* = don't care)
1400 * This function helps to determine simple vs. non-simple.
1403 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1405 switch (writemask
) {
1407 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1409 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1411 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1413 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1415 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1416 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1418 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1419 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1420 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1421 case WRITEMASK_XYZW
:
1422 return swizzle
== SWIZZLE_NOOP
;
1430 * Convert the given swizzle into a writemask. In some cases this
1431 * is trivial, in other cases, we'll need to also swizzle the right
1432 * hand side to put components in the right places.
1433 * \param swizzle the incoming swizzle
1434 * \param writemaskOut returns the writemask
1435 * \param swizzleOut swizzle to apply to the right-hand-side
1436 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1439 swizzle_to_writemask(GLuint swizzle
,
1440 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1442 GLuint mask
= 0x0, newSwizzle
[4];
1445 /* make new dst writemask, compute size */
1446 for (i
= 0; i
< 4; i
++) {
1447 const GLuint swz
= GET_SWZ(swizzle
, i
);
1448 if (swz
== SWIZZLE_NIL
) {
1452 assert(swz
>= 0 && swz
<= 3);
1455 assert(mask
<= 0xf);
1456 size
= i
; /* number of components in mask/swizzle */
1458 *writemaskOut
= mask
;
1460 /* make new src swizzle, by inversion */
1461 for (i
= 0; i
< 4; i
++) {
1462 newSwizzle
[i
] = i
; /*identity*/
1464 for (i
= 0; i
< size
; i
++) {
1465 const GLuint swz
= GET_SWZ(swizzle
, i
);
1466 newSwizzle
[swz
] = i
;
1468 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1473 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1475 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1477 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1479 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1481 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1490 * Recursively traverse 'oper' to produce a swizzle mask in the event
1491 * of any vector subscripts and swizzle suffixes.
1492 * Ex: for "vec4 v", "v[2].x" resolves to v.z
1495 resolve_swizzle(const slang_operation
*oper
)
1497 if (oper
->type
== SLANG_OPER_FIELD
) {
1498 /* writemask from .xyzw suffix */
1500 if (_slang_is_swizzle((char*) oper
->a_id
, 4, &swz
)) {
1501 GLuint swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
1505 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1506 GLuint s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1510 return SWIZZLE_XYZW
;
1512 else if (oper
->type
== SLANG_OPER_SUBSCRIPT
&&
1513 oper
->children
[1].type
== SLANG_OPER_LITERAL_INT
) {
1514 /* writemask from [index] */
1515 GLuint child_swizzle
= resolve_swizzle(&oper
->children
[0]);
1516 GLuint i
= (GLuint
) oper
->children
[1].literal
[0];
1521 swizzle
= SWIZZLE_XXXX
;
1524 swizzle
= SWIZZLE_YYYY
;
1527 swizzle
= SWIZZLE_ZZZZ
;
1530 swizzle
= SWIZZLE_WWWW
;
1533 swizzle
= SWIZZLE_XYZW
;
1535 s
= _slang_swizzle_swizzle(child_swizzle
, swizzle
);
1539 return SWIZZLE_XYZW
;
1545 * As above, but produce a writemask.
1548 resolve_writemask(const slang_operation
*oper
)
1550 GLuint swizzle
= resolve_swizzle(oper
);
1551 GLuint writemask
, swizzleOut
;
1552 swizzle_to_writemask(swizzle
, &writemask
, &swizzleOut
);
1558 * Recursively descend through swizzle nodes to find the node's storage info.
1560 static slang_ir_storage
*
1561 get_store(const slang_ir_node
*n
)
1563 if (n
->Opcode
== IR_SWIZZLE
) {
1564 return get_store(n
->Children
[0]);
1572 * Generate IR tree for an asm instruction/operation such as:
1573 * __asm vec4_dot __retVal.x, v1, v2;
1575 static slang_ir_node
*
1576 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1577 slang_operation
*dest
)
1579 const slang_asm_info
*info
;
1580 slang_ir_node
*kids
[3], *n
;
1581 GLuint j
, firstOperand
;
1583 assert(oper
->type
== SLANG_OPER_ASM
);
1585 info
= slang_find_asm_info((char *) oper
->a_id
);
1587 _mesa_problem(NULL
, "undefined __asm function %s\n",
1588 (char *) oper
->a_id
);
1591 assert(info
->NumParams
<= 3);
1593 if (info
->NumParams
== oper
->num_children
) {
1594 /* Storage for result is not specified.
1595 * Children[0], [1], [2] are the operands.
1600 /* Storage for result (child[0]) is specified.
1601 * Children[1], [2], [3] are the operands.
1606 /* assemble child(ren) */
1607 kids
[0] = kids
[1] = kids
[2] = NULL
;
1608 for (j
= 0; j
< info
->NumParams
; j
++) {
1609 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1614 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1617 /* Setup n->Store to be a particular location. Otherwise, storage
1618 * for the result (a temporary) will be allocated later.
1620 GLuint writemask
= WRITEMASK_XYZW
;
1621 slang_operation
*dest_oper
;
1624 dest_oper
= &oper
->children
[0];
1626 writemask
= resolve_writemask(dest_oper
);
1628 n0
= _slang_gen_operation(A
, dest_oper
);
1633 n
->Store
= get_store(n0
);
1634 n
->Writemask
= writemask
;
1636 assert(n
->Store
->File
!= PROGRAM_UNDEFINED
||
1647 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1650 for (i
= 0; i
< scope
->num_functions
; i
++) {
1651 slang_function
*f
= &scope
->functions
[i
];
1652 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1653 printf(" %s (%d args)\n", name
, f
->param_count
);
1656 if (scope
->outer_scope
)
1657 print_funcs(scope
->outer_scope
, name
);
1662 * Return first function in the scope that has the given name.
1663 * This is the function we'll try to call when there is no exact match
1664 * between function parameters and call arguments.
1666 * XXX we should really create a list of candidate functions and try
1669 static slang_function
*
1670 _slang_first_function(struct slang_function_scope_
*scope
, const char *name
)
1673 for (i
= 0; i
< scope
->num_functions
; i
++) {
1674 slang_function
*f
= &scope
->functions
[i
];
1675 if (strcmp(name
, (char*) f
->header
.a_name
) == 0)
1678 if (scope
->outer_scope
)
1679 return _slang_first_function(scope
->outer_scope
, name
);
1686 * Assemble a function call, given a particular function name.
1687 * \param name the function's name (operators like '*' are possible).
1689 static slang_ir_node
*
1690 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
1691 slang_operation
*oper
, slang_operation
*dest
)
1693 slang_operation
*params
= oper
->children
;
1694 const GLuint param_count
= oper
->num_children
;
1696 slang_function
*fun
;
1698 atom
= slang_atom_pool_atom(A
->atoms
, name
);
1699 if (atom
== SLANG_ATOM_NULL
)
1703 * Use 'name' to find the function to call
1705 fun
= _slang_locate_function(A
->space
.funcs
, atom
, params
, param_count
,
1706 &A
->space
, A
->atoms
, A
->log
);
1708 /* A function with exactly the right parameters/types was not found.
1709 * Try adapting the parameters.
1711 fun
= _slang_first_function(A
->space
.funcs
, name
);
1712 if (!fun
|| !_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
1713 slang_info_log_error(A
->log
, "Function '%s' not found (check argument types)", name
);
1719 return _slang_gen_function_call(A
, fun
, oper
, dest
);
1724 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
1726 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
1727 oper
->type
== SLANG_OPER_LITERAL_INT
||
1728 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
1729 if (oper
->literal
[0])
1735 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
1736 oper
->num_children
== 1) {
1737 return _slang_is_constant_cond(&oper
->children
[0], value
);
1744 * Test if an operation is a scalar or boolean.
1747 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
1749 slang_typeinfo type
;
1752 slang_typeinfo_construct(&type
);
1753 _slang_typeof_operation(A
, oper
, &type
);
1754 size
= _slang_sizeof_type_specifier(&type
.spec
);
1755 slang_typeinfo_destruct(&type
);
1761 * Generate loop code using high-level IR_LOOP instruction
1763 static slang_ir_node
*
1764 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1768 * BREAK if !expr (child[0])
1769 * body code (child[1])
1771 slang_ir_node
*prevLoop
, *loop
, *breakIf
, *body
;
1772 GLboolean isConst
, constTrue
;
1774 /* type-check expression */
1775 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
1776 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
1780 /* Check if loop condition is a constant */
1781 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
1783 if (isConst
&& !constTrue
) {
1784 /* loop is never executed! */
1785 return new_node0(IR_NOP
);
1788 loop
= new_loop(NULL
);
1790 /* save old, push new loop */
1791 prevLoop
= A
->CurLoop
;
1794 if (isConst
&& constTrue
) {
1795 /* while(nonzero constant), no conditional break */
1800 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
1801 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
1803 body
= _slang_gen_operation(A
, &oper
->children
[1]);
1804 loop
->Children
[0] = new_seq(breakIf
, body
);
1806 /* Do infinite loop detection */
1807 /* loop->List is head of linked list of break/continue nodes */
1808 if (!loop
->List
&& isConst
&& constTrue
) {
1809 /* infinite loop detected */
1810 A
->CurLoop
= prevLoop
; /* clean-up */
1811 slang_info_log_error(A
->log
, "Infinite loop detected!");
1815 /* pop loop, restore prev */
1816 A
->CurLoop
= prevLoop
;
1823 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
1825 static slang_ir_node
*
1826 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1830 * body code (child[0])
1832 * BREAK if !expr (child[1])
1834 slang_ir_node
*prevLoop
, *loop
;
1835 GLboolean isConst
, constTrue
;
1837 /* type-check expression */
1838 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[1])) {
1839 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
1843 loop
= new_loop(NULL
);
1845 /* save old, push new loop */
1846 prevLoop
= A
->CurLoop
;
1850 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
1852 /* Check if loop condition is a constant */
1853 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
1854 if (isConst
&& constTrue
) {
1855 /* do { } while(1) ==> no conditional break */
1856 loop
->Children
[1] = NULL
; /* no tail code */
1860 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
1861 loop
->Children
[1] = new_break_if_true(A
->CurLoop
, cond
);
1864 /* XXX we should do infinite loop detection, as above */
1866 /* pop loop, restore prev */
1867 A
->CurLoop
= prevLoop
;
1874 * Generate for-loop using high-level IR_LOOP instruction.
1876 static slang_ir_node
*
1877 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1880 * init code (child[0])
1882 * BREAK if !expr (child[1])
1883 * body code (child[3])
1885 * incr code (child[2]) // XXX continue here
1887 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
1889 init
= _slang_gen_operation(A
, &oper
->children
[0]);
1890 loop
= new_loop(NULL
);
1892 /* save old, push new loop */
1893 prevLoop
= A
->CurLoop
;
1896 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
1897 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
1898 body
= _slang_gen_operation(A
, &oper
->children
[3]);
1899 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
1901 loop
->Children
[0] = new_seq(breakIf
, body
);
1902 loop
->Children
[1] = incr
; /* tail code */
1904 /* pop loop, restore prev */
1905 A
->CurLoop
= prevLoop
;
1907 return new_seq(init
, loop
);
1911 static slang_ir_node
*
1912 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1914 slang_ir_node
*n
, *loopNode
;
1915 assert(oper
->type
== SLANG_OPER_CONTINUE
);
1916 loopNode
= A
->CurLoop
;
1918 assert(loopNode
->Opcode
== IR_LOOP
);
1919 n
= new_node0(IR_CONT
);
1921 n
->Parent
= loopNode
;
1922 /* insert this node at head of linked list */
1923 n
->List
= loopNode
->List
;
1931 * Determine if the given operation is of a specific type.
1934 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
1936 if (oper
->type
== type
)
1938 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1939 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
1940 oper
->num_children
== 1)
1941 return is_operation_type(&oper
->children
[0], type
);
1948 * Generate IR tree for an if/then/else conditional using high-level
1949 * IR_IF instruction.
1951 static slang_ir_node
*
1952 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1955 * eval expr (child[0])
1962 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
1963 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
1964 GLboolean isConst
, constTrue
;
1966 /* type-check expression */
1967 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
1968 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
1972 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
1976 return _slang_gen_operation(A
, &oper
->children
[1]);
1979 /* if (false) ... */
1980 return _slang_gen_operation(A
, &oper
->children
[2]);
1984 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1985 cond
= new_cond(cond
);
1987 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)) {
1988 /* Special case: generate a conditional break */
1989 ifBody
= new_break_if_true(A
->CurLoop
, cond
);
1990 if (haveElseClause
) {
1991 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1992 return new_seq(ifBody
, elseBody
);
1996 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)) {
1997 /* Special case: generate a conditional break */
1998 ifBody
= new_cont_if_true(A
->CurLoop
, cond
);
1999 if (haveElseClause
) {
2000 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
2001 return new_seq(ifBody
, elseBody
);
2007 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
2009 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
2012 ifNode
= new_if(cond
, ifBody
, elseBody
);
2019 static slang_ir_node
*
2020 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2024 assert(oper
->type
== SLANG_OPER_NOT
);
2026 /* type-check expression */
2027 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2028 slang_info_log_error(A
->log
,
2029 "scalar/boolean expression expected for '!'");
2033 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2041 static slang_ir_node
*
2042 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
2044 slang_ir_node
*n1
, *n2
;
2046 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
2048 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
2049 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
2050 slang_info_log_error(A
->log
,
2051 "scalar/boolean expressions expected for '^^'");
2055 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
2058 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
2061 return new_node2(IR_NOTEQUAL
, n1
, n2
);
2066 * Generate IR node for storage of a temporary of given size.
2068 static slang_ir_node
*
2069 _slang_gen_temporary(GLint size
)
2071 slang_ir_storage
*store
;
2072 slang_ir_node
*n
= NULL
;
2074 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -2, size
);
2076 n
= new_node0(IR_VAR_DECL
);
2089 * Generate IR node for allocating/declaring a variable.
2091 static slang_ir_node
*
2092 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
)
2096 /*assert(!var->declared);*/
2097 var
->declared
= GL_TRUE
;
2099 assert(!is_sampler_type(&var
->type
));
2101 n
= new_node0(IR_VAR_DECL
);
2103 _slang_attach_storage(n
, var
);
2105 assert(n
->Store
== var
->aux
);
2107 assert(n
->Store
->Index
< 0);
2109 n
->Store
->File
= PROGRAM_TEMPORARY
;
2110 n
->Store
->Size
= _slang_sizeof_type_specifier(&n
->Var
->type
.specifier
);
2113 printf("%s var %p %s store=%p index=%d size=%d\n",
2114 __FUNCTION__
, (void *) var
, (char *) var
->a_name
,
2115 (void *) n
->Store
, n
->Store
->Index
, n
->Store
->Size
);
2118 if (var
->array_len
> 0) {
2119 /* this is an array */
2120 /* round up element size to mult of 4 */
2121 GLint sz
= (n
->Store
->Size
+ 3) & ~3;
2122 /* mult by array size */
2123 sz
*= var
->array_len
;
2124 n
->Store
->Size
= sz
;
2127 assert(n
->Store
->Size
> 0);
2129 /* setup default swizzle for storing the variable */
2130 switch (n
->Store
->Size
) {
2132 n
->Store
->Swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
2133 SWIZZLE_NIL
, SWIZZLE_NIL
);
2136 n
->Store
->Swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
2137 SWIZZLE_Z
, SWIZZLE_NIL
);
2140 /* Note that float-sized vars may be allocated in any x/y/z/w
2141 * slot, but that won't be determined until code emit time.
2143 n
->Store
->Swizzle
= SWIZZLE_NOOP
;
2146 A
->program
->NumTemporaries
++; /* an approximation */
2153 * Generate code for a selection expression: b ? x : y
2154 * XXX In some cases we could implement a selection expression
2155 * with an LRP instruction (use the boolean as the interpolant).
2156 * Otherwise, we use an IF/ELSE/ENDIF construct.
2158 static slang_ir_node
*
2159 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
2161 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
2162 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
2163 slang_typeinfo type
;
2166 assert(oper
->type
== SLANG_OPER_SELECT
);
2167 assert(oper
->num_children
== 3);
2169 /* size of x or y's type */
2170 slang_typeinfo_construct(&type
);
2171 _slang_typeof_operation(A
, &oper
->children
[1], &type
);
2172 size
= _slang_sizeof_type_specifier(&type
.spec
);
2176 tmpDecl
= _slang_gen_temporary(size
);
2178 /* the condition (child 0) */
2179 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
2180 cond
= new_cond(cond
);
2182 /* if-true body (child 1) */
2183 tmpVar
= new_node0(IR_VAR
);
2184 tmpVar
->Store
= tmpDecl
->Store
;
2185 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
2186 trueNode
= new_node2(IR_MOVE
, tmpVar
, trueExpr
);
2188 /* if-false body (child 2) */
2189 tmpVar
= new_node0(IR_VAR
);
2190 tmpVar
->Store
= tmpDecl
->Store
;
2191 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
2192 falseNode
= new_node2(IR_MOVE
, tmpVar
, falseExpr
);
2194 ifNode
= new_if(cond
, trueNode
, falseNode
);
2197 tmpVar
= new_node0(IR_VAR
);
2198 tmpVar
->Store
= tmpDecl
->Store
;
2200 tree
= new_seq(ifNode
, tmpVar
);
2201 tree
= new_seq(tmpDecl
, tree
);
2203 /*_slang_print_ir_tree(tree, 10);*/
2209 * Generate code for &&.
2211 static slang_ir_node
*
2212 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
2214 /* rewrite "a && b" as "a ? b : false" */
2215 slang_operation
*select
;
2218 select
= slang_operation_new(1);
2219 select
->type
= SLANG_OPER_SELECT
;
2220 select
->num_children
= 3;
2221 select
->children
= slang_operation_new(3);
2223 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
2224 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
2225 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
2226 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
2227 select
->children
[2].literal_size
= 1;
2229 n
= _slang_gen_select(A
, select
);
2235 * Generate code for ||.
2237 static slang_ir_node
*
2238 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
2240 /* rewrite "a || b" as "a ? true : b" */
2241 slang_operation
*select
;
2244 select
= slang_operation_new(1);
2245 select
->type
= SLANG_OPER_SELECT
;
2246 select
->num_children
= 3;
2247 select
->children
= slang_operation_new(3);
2249 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
2250 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
2251 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
2252 select
->children
[1].literal_size
= 1;
2253 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
2255 n
= _slang_gen_select(A
, select
);
2261 * Generate IR tree for a return statement.
2263 static slang_ir_node
*
2264 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
2266 const GLboolean haveReturnValue
2267 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
2269 /* error checking */
2270 assert(A
->CurFunction
);
2271 if (haveReturnValue
&&
2272 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
2273 slang_info_log_error(A
->log
, "illegal return expression");
2276 else if (!haveReturnValue
&&
2277 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
2278 slang_info_log_error(A
->log
, "return statement requires an expression");
2282 if (!haveReturnValue
) {
2283 return new_return(A
->curFuncEndLabel
);
2291 * return; // goto __endOfFunction
2293 slang_operation
*assign
;
2294 slang_atom a_retVal
;
2297 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2303 = _slang_locate_variable(oper
->locals
, a_retVal
, GL_TRUE
);
2305 /* trying to return a value in a void-valued function */
2311 assign
= slang_operation_new(1);
2312 assign
->type
= SLANG_OPER_ASSIGN
;
2313 assign
->num_children
= 2;
2314 assign
->children
= slang_operation_new(2);
2315 /* lhs (__retVal) */
2316 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2317 assign
->children
[0].a_id
= a_retVal
;
2318 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
2320 /* XXX we might be able to avoid this copy someday */
2321 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
2323 /* assemble the new code */
2324 n
= new_seq(_slang_gen_operation(A
, assign
),
2325 new_return(A
->curFuncEndLabel
));
2327 slang_operation_delete(assign
);
2334 * Generate IR tree for a variable declaration.
2336 static slang_ir_node
*
2337 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
2340 slang_ir_node
*varDecl
;
2342 const char *varName
= (char *) oper
->a_id
;
2344 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
2346 v
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
2349 varDecl
= _slang_gen_var_decl(A
, v
);
2351 if (oper
->num_children
> 0) {
2352 /* child is initializer */
2353 slang_ir_node
*var
, *init
, *rhs
;
2354 assert(oper
->num_children
== 1);
2355 var
= new_var(A
, oper
, oper
->a_id
);
2357 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
2360 /* XXX make copy of this initializer? */
2361 rhs
= _slang_gen_operation(A
, &oper
->children
[0]);
2363 return NULL
; /* must have found an error */
2364 init
= new_node2(IR_MOVE
, var
, rhs
);
2365 /*assert(rhs->Opcode != IR_SEQ);*/
2366 n
= new_seq(varDecl
, init
);
2368 else if (v
->initializer
) {
2369 slang_ir_node
*var
, *init
, *rhs
;
2370 var
= new_var(A
, oper
, oper
->a_id
);
2372 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
2376 /* XXX make copy of this initializer? */
2378 slang_operation dup
;
2379 slang_operation_construct(&dup
);
2380 slang_operation_copy(&dup
, v
->initializer
);
2381 _slang_simplify(&dup
, &A
->space
, A
->atoms
);
2382 rhs
= _slang_gen_operation(A
, &dup
);
2385 _slang_simplify(v
->initializer
, &A
->space
, A
->atoms
);
2386 rhs
= _slang_gen_operation(A
, v
->initializer
);
2392 init
= new_node2(IR_MOVE
, var
, rhs
);
2394 assert(rhs->Opcode != IR_SEQ);
2396 n
= new_seq(varDecl
, init
);
2406 * Generate IR tree for a variable (such as in an expression).
2408 static slang_ir_node
*
2409 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
2411 /* If there's a variable associated with this oper (from inlining)
2412 * use it. Otherwise, use the oper's var id.
2414 slang_atom aVar
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
2415 slang_ir_node
*n
= new_var(A
, oper
, aVar
);
2417 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) aVar
);
2424 static slang_ir_node
*
2425 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
2427 /* XXX should rewrite this to use relative/Parent storage */
2428 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
2431 n
->Store
= _slang_new_ir_storage_swz(PROGRAM_UNDEFINED
, -1, -1, swizzle
);
2438 * Generate IR tree for an assignment (=).
2440 static slang_ir_node
*
2441 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
2443 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
2444 /* Check that var is writeable */
2446 = _slang_locate_variable(oper
->children
[0].locals
,
2447 oper
->children
[0].a_id
, GL_TRUE
);
2449 slang_info_log_error(A
->log
, "undefined variable '%s'",
2450 (char *) oper
->children
[0].a_id
);
2453 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
2454 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
2455 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
2456 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
2457 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
2458 slang_info_log_error(A
->log
,
2459 "illegal assignment to read-only variable '%s'",
2460 (char *) oper
->children
[0].a_id
);
2465 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
2466 oper
->children
[1].type
== SLANG_OPER_CALL
) {
2467 /* Special case of: x = f(a, b)
2468 * Replace with f(a, b, x) (where x == hidden __retVal out param)
2470 * XXX this could be even more effective if we could accomodate
2471 * cases such as "v.x = f();" - would help with typical vertex
2475 n
= _slang_gen_function_call_name(A
,
2476 (const char *) oper
->children
[1].a_id
,
2477 &oper
->children
[1], &oper
->children
[0]);
2481 slang_ir_node
*n
, *lhs
, *rhs
;
2482 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
2485 if (!(lhs
->Store
->File
== PROGRAM_OUTPUT
||
2486 lhs
->Store
->File
== PROGRAM_TEMPORARY
||
2487 (lhs
->Store
->File
== PROGRAM_VARYING
&&
2488 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
) ||
2489 lhs
->Store
->File
== PROGRAM_UNDEFINED
)) {
2490 slang_info_log_error(A
->log
,
2491 "illegal assignment to read-only l-value");
2496 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
2498 /* convert lhs swizzle into writemask */
2499 GLuint writemask
, newSwizzle
;
2500 if (!swizzle_to_writemask(lhs
->Store
->Swizzle
,
2501 &writemask
, &newSwizzle
)) {
2502 /* Non-simple writemask, need to swizzle right hand side in
2503 * order to put components into the right place.
2505 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
2507 n
= new_node2(IR_MOVE
, lhs
, rhs
);
2508 n
->Writemask
= writemask
;
2519 * Generate IR tree for referencing a field in a struct (or basic vector type)
2521 static slang_ir_node
*
2522 _slang_gen_struct_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
2526 /* type of struct */
2527 slang_typeinfo_construct(&ti
);
2528 _slang_typeof_operation(A
, &oper
->children
[0], &ti
);
2530 if (_slang_type_is_vector(ti
.spec
.type
)) {
2531 /* the field should be a swizzle */
2532 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
2536 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2537 slang_info_log_error(A
->log
, "Bad swizzle");
2539 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2544 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2545 /* create new parent node with swizzle */
2547 n
= _slang_gen_swizzle(n
, swizzle
);
2550 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
2551 || ti
.spec
.type
== SLANG_SPEC_INT
2552 || ti
.spec
.type
== SLANG_SPEC_BOOL
) {
2553 const GLuint rows
= 1;
2557 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2558 slang_info_log_error(A
->log
, "Bad swizzle");
2560 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2564 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2565 /* create new parent node with swizzle */
2566 n
= _slang_gen_swizzle(n
, swizzle
);
2570 /* the field is a structure member (base.field) */
2571 /* oper->children[0] is the base */
2572 /* oper->a_id is the field name */
2573 slang_ir_node
*base
, *n
;
2574 slang_typeinfo field_ti
;
2575 GLint fieldSize
, fieldOffset
= -1, swz
;
2578 slang_typeinfo_construct(&field_ti
);
2579 _slang_typeof_operation(A
, oper
, &field_ti
);
2581 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
2583 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
2585 if (fieldSize
== 0 || fieldOffset
< 0) {
2586 slang_info_log_error(A
->log
,
2587 "\"%s\" is not a member of struct \"%s\"",
2588 (char *) oper
->a_id
,
2589 (char *) ti
.spec
._struct
->a_name
);
2592 assert(fieldSize
>= 0);
2594 base
= _slang_gen_operation(A
, &oper
->children
[0]);
2596 /* error msg should have already been logged */
2600 n
= new_node1(IR_FIELD
, base
);
2605 /* setup the storage info for this node */
2606 swz
= fieldOffset
% 4;
2608 n
->Field
= (char *) oper
->a_id
;
2609 n
->Store
= _slang_new_ir_storage_relative(fieldOffset
/ 4,
2613 n
->Store
->Swizzle
= MAKE_SWIZZLE4(swz
, swz
, swz
, swz
);
2614 else if (fieldSize
== 2)
2615 n
->Store
->Swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
2616 SWIZZLE_NIL
, SWIZZLE_NIL
);
2617 else if (fieldSize
== 3)
2618 n
->Store
->Swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
2619 SWIZZLE_Z
, SWIZZLE_NIL
);
2627 * Gen code for array indexing.
2629 static slang_ir_node
*
2630 _slang_gen_array_element(slang_assemble_ctx
* A
, slang_operation
*oper
)
2632 slang_typeinfo array_ti
;
2634 /* get array's type info */
2635 slang_typeinfo_construct(&array_ti
);
2636 _slang_typeof_operation(A
, &oper
->children
[0], &array_ti
);
2638 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
2639 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
2640 /* translate the index into a swizzle/writemask: "v.x=p" */
2641 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
2645 index
= (GLint
) oper
->children
[1].literal
[0];
2646 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
2648 slang_info_log_error(A
->log
, "Invalid array index for vector type");
2652 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2654 /* use swizzle to access the element */
2655 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
2659 n
= _slang_gen_swizzle(n
, swizzle
);
2660 /*n->Store = _slang_clone_ir_storage_swz(n->Store, */
2661 n
->Writemask
= WRITEMASK_X
<< index
;
2666 /* conventional array */
2667 slang_typeinfo elem_ti
;
2668 slang_ir_node
*elem
, *array
, *index
;
2669 GLint elemSize
, arrayLen
;
2671 /* size of array element */
2672 slang_typeinfo_construct(&elem_ti
);
2673 _slang_typeof_operation(A
, oper
, &elem_ti
);
2674 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
2676 if (_slang_type_is_matrix(array_ti
.spec
.type
))
2677 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
2679 arrayLen
= array_ti
.array_len
;
2681 slang_typeinfo_destruct(&array_ti
);
2682 slang_typeinfo_destruct(&elem_ti
);
2684 if (elemSize
<= 0) {
2685 /* unknown var or type */
2686 slang_info_log_error(A
->log
, "Undefined variable or type");
2690 array
= _slang_gen_operation(A
, &oper
->children
[0]);
2691 index
= _slang_gen_operation(A
, &oper
->children
[1]);
2692 if (array
&& index
) {
2694 GLint constIndex
= 0;
2695 if (index
->Opcode
== IR_FLOAT
) {
2696 constIndex
= (int) index
->Value
[0];
2697 if (constIndex
< 0 || constIndex
>= arrayLen
) {
2698 slang_info_log_error(A
->log
,
2699 "Array index out of bounds (index=%d size=%d)",
2700 constIndex
, arrayLen
);
2701 _slang_free_ir_tree(array
);
2702 _slang_free_ir_tree(index
);
2707 elem
= new_node2(IR_ELEMENT
, array
, index
);
2708 elem
->Store
= _slang_new_ir_storage_relative(constIndex
,
2712 /* XXX try to do some array bounds checking here */
2716 _slang_free_ir_tree(array
);
2717 _slang_free_ir_tree(index
);
2725 * Generate IR tree for a slang_operation (AST node)
2727 static slang_ir_node
*
2728 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
2730 switch (oper
->type
) {
2731 case SLANG_OPER_BLOCK_NEW_SCOPE
:
2735 _slang_push_var_table(A
->vartable
);
2737 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
2738 n
= _slang_gen_operation(A
, oper
);
2739 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
2741 _slang_pop_var_table(A
->vartable
);
2744 n
= new_node1(IR_SCOPE
, n
);
2749 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
2750 /* list of operations */
2751 if (oper
->num_children
> 0)
2753 slang_ir_node
*n
, *tree
= NULL
;
2756 for (i
= 0; i
< oper
->num_children
; i
++) {
2757 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
2759 _slang_free_ir_tree(tree
);
2760 return NULL
; /* error must have occured */
2762 tree
= new_seq(tree
, n
);
2766 if (oper
->locals
->num_variables
> 0) {
2769 printf("\n****** Deallocate vars in scope!\n");
2771 for (i
= 0; i
< oper
->locals
->num_variables
; i
++) {
2772 slang_variable
*v
= oper
->locals
->variables
+ i
;
2774 slang_ir_storage
*store
= (slang_ir_storage
*) v
->aux
;
2776 printf(" Deallocate var %s\n", (char*) v->a_name);
2778 assert(store
->File
== PROGRAM_TEMPORARY
);
2779 assert(store
->Index
>= 0);
2780 _slang_free_temp(A
->vartable
, store
->Index
, store
->Size
);
2788 return new_node0(IR_NOP
);
2791 case SLANG_OPER_EXPRESSION
:
2792 return _slang_gen_operation(A
, &oper
->children
[0]);
2794 case SLANG_OPER_FOR
:
2795 return _slang_gen_for(A
, oper
);
2797 return _slang_gen_do(A
, oper
);
2798 case SLANG_OPER_WHILE
:
2799 return _slang_gen_while(A
, oper
);
2800 case SLANG_OPER_BREAK
:
2802 slang_info_log_error(A
->log
, "'break' not in loop");
2805 return new_break(A
->CurLoop
);
2806 case SLANG_OPER_CONTINUE
:
2808 slang_info_log_error(A
->log
, "'continue' not in loop");
2811 return _slang_gen_continue(A
, oper
);
2812 case SLANG_OPER_DISCARD
:
2813 return new_node0(IR_KILL
);
2815 case SLANG_OPER_EQUAL
:
2816 return new_node2(IR_EQUAL
,
2817 _slang_gen_operation(A
, &oper
->children
[0]),
2818 _slang_gen_operation(A
, &oper
->children
[1]));
2819 case SLANG_OPER_NOTEQUAL
:
2820 return new_node2(IR_NOTEQUAL
,
2821 _slang_gen_operation(A
, &oper
->children
[0]),
2822 _slang_gen_operation(A
, &oper
->children
[1]));
2823 case SLANG_OPER_GREATER
:
2824 return new_node2(IR_SGT
,
2825 _slang_gen_operation(A
, &oper
->children
[0]),
2826 _slang_gen_operation(A
, &oper
->children
[1]));
2827 case SLANG_OPER_LESS
:
2828 return new_node2(IR_SLT
,
2829 _slang_gen_operation(A
, &oper
->children
[0]),
2830 _slang_gen_operation(A
, &oper
->children
[1]));
2831 case SLANG_OPER_GREATEREQUAL
:
2832 return new_node2(IR_SGE
,
2833 _slang_gen_operation(A
, &oper
->children
[0]),
2834 _slang_gen_operation(A
, &oper
->children
[1]));
2835 case SLANG_OPER_LESSEQUAL
:
2836 return new_node2(IR_SLE
,
2837 _slang_gen_operation(A
, &oper
->children
[0]),
2838 _slang_gen_operation(A
, &oper
->children
[1]));
2839 case SLANG_OPER_ADD
:
2842 assert(oper
->num_children
== 2);
2843 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
2846 case SLANG_OPER_SUBTRACT
:
2849 assert(oper
->num_children
== 2);
2850 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
2853 case SLANG_OPER_MULTIPLY
:
2856 assert(oper
->num_children
== 2);
2857 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
2860 case SLANG_OPER_DIVIDE
:
2863 assert(oper
->num_children
== 2);
2864 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
2867 case SLANG_OPER_MINUS
:
2870 assert(oper
->num_children
== 1);
2871 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
2874 case SLANG_OPER_PLUS
:
2875 /* +expr --> do nothing */
2876 return _slang_gen_operation(A
, &oper
->children
[0]);
2877 case SLANG_OPER_VARIABLE_DECL
:
2878 return _slang_gen_declaration(A
, oper
);
2879 case SLANG_OPER_ASSIGN
:
2880 return _slang_gen_assignment(A
, oper
);
2881 case SLANG_OPER_ADDASSIGN
:
2884 assert(oper
->num_children
== 2);
2885 n
= _slang_gen_function_call_name(A
, "+=", oper
, &oper
->children
[0]);
2888 case SLANG_OPER_SUBASSIGN
:
2891 assert(oper
->num_children
== 2);
2892 n
= _slang_gen_function_call_name(A
, "-=", oper
, &oper
->children
[0]);
2896 case SLANG_OPER_MULASSIGN
:
2899 assert(oper
->num_children
== 2);
2900 n
= _slang_gen_function_call_name(A
, "*=", oper
, &oper
->children
[0]);
2903 case SLANG_OPER_DIVASSIGN
:
2906 assert(oper
->num_children
== 2);
2907 n
= _slang_gen_function_call_name(A
, "/=", oper
, &oper
->children
[0]);
2910 case SLANG_OPER_LOGICALAND
:
2913 assert(oper
->num_children
== 2);
2914 n
= _slang_gen_logical_and(A
, oper
);
2917 case SLANG_OPER_LOGICALOR
:
2920 assert(oper
->num_children
== 2);
2921 n
= _slang_gen_logical_or(A
, oper
);
2924 case SLANG_OPER_LOGICALXOR
:
2925 return _slang_gen_xor(A
, oper
);
2926 case SLANG_OPER_NOT
:
2927 return _slang_gen_not(A
, oper
);
2928 case SLANG_OPER_SELECT
: /* b ? x : y */
2931 assert(oper
->num_children
== 3);
2932 n
= _slang_gen_select(A
, oper
);
2936 case SLANG_OPER_ASM
:
2937 return _slang_gen_asm(A
, oper
, NULL
);
2938 case SLANG_OPER_CALL
:
2939 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
2941 case SLANG_OPER_RETURN
:
2942 return _slang_gen_return(A
, oper
);
2943 case SLANG_OPER_LABEL
:
2944 return new_label(oper
->label
);
2945 case SLANG_OPER_IDENTIFIER
:
2946 return _slang_gen_variable(A
, oper
);
2948 return _slang_gen_if(A
, oper
);
2949 case SLANG_OPER_FIELD
:
2950 return _slang_gen_struct_field(A
, oper
);
2951 case SLANG_OPER_SUBSCRIPT
:
2952 return _slang_gen_array_element(A
, oper
);
2953 case SLANG_OPER_LITERAL_FLOAT
:
2955 case SLANG_OPER_LITERAL_INT
:
2957 case SLANG_OPER_LITERAL_BOOL
:
2958 return new_float_literal(oper
->literal
, oper
->literal_size
);
2960 case SLANG_OPER_POSTINCREMENT
: /* var++ */
2963 assert(oper
->num_children
== 1);
2964 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
2967 case SLANG_OPER_POSTDECREMENT
: /* var-- */
2970 assert(oper
->num_children
== 1);
2971 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
2974 case SLANG_OPER_PREINCREMENT
: /* ++var */
2977 assert(oper
->num_children
== 1);
2978 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
2981 case SLANG_OPER_PREDECREMENT
: /* --var */
2984 assert(oper
->num_children
== 1);
2985 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
2989 case SLANG_OPER_NON_INLINED_CALL
:
2990 case SLANG_OPER_SEQUENCE
:
2992 slang_ir_node
*tree
= NULL
;
2994 for (i
= 0; i
< oper
->num_children
; i
++) {
2995 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
2996 tree
= new_seq(tree
, n
);
2998 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
2999 tree
= new_function_call(tree
, oper
->label
);
3004 case SLANG_OPER_NONE
:
3005 case SLANG_OPER_VOID
:
3006 /* returning NULL here would generate an error */
3007 return new_node0(IR_NOP
);
3010 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
3012 return new_node0(IR_NOP
);
3021 * Called by compiler when a global variable has been parsed/compiled.
3022 * Here we examine the variable's type to determine what kind of register
3023 * storage will be used.
3025 * A uniform such as "gl_Position" will become the register specification
3026 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
3027 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
3029 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
3030 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
3031 * actual texture unit (as specified by the user calling glUniform1i()).
3034 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
3035 slang_unit_type type
)
3037 struct gl_program
*prog
= A
->program
;
3038 const char *varName
= (char *) var
->a_name
;
3039 GLboolean success
= GL_TRUE
;
3040 slang_ir_storage
*store
= NULL
;
3042 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
3043 const GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
3045 if (texIndex
!= -1) {
3046 /* This is a texture sampler variable...
3047 * store->File = PROGRAM_SAMPLER
3048 * store->Index = sampler number (0..7, typically)
3049 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
3051 GLint sampNum
= _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
3052 store
= _slang_new_ir_storage(PROGRAM_SAMPLER
, sampNum
, texIndex
);
3053 if (dbg
) printf("SAMPLER ");
3055 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
3056 /* Uniform variable */
3057 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
)
3058 * MAX2(var
->array_len
, 1);
3060 /* user-defined uniform */
3061 if (datatype
== GL_NONE
) {
3062 if (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
) {
3063 _mesa_problem(NULL
, "user-declared uniform structs not supported yet");
3064 /* XXX what we need to do is unroll the struct into its
3065 * basic types, creating a uniform variable for each.
3073 * Should produce uniforms:
3074 * "f.a" (GL_FLOAT_VEC3)
3075 * "f.b" (GL_FLOAT_VEC4)
3079 slang_info_log_error(A
->log
,
3080 "invalid datatype for uniform variable %s",
3081 (char *) var
->a_name
);
3086 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
3088 store
= _slang_new_ir_storage(PROGRAM_UNIFORM
, uniformLoc
, size
);
3092 /* pre-defined uniform, like gl_ModelviewMatrix */
3093 /* We know it's a uniform, but don't allocate storage unless
3096 store
= _slang_new_ir_storage(PROGRAM_STATE_VAR
, -1, size
);
3098 if (dbg
) printf("UNIFORM (sz %d) ", size
);
3100 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
3101 const GLint size
= 4; /* XXX fix */
3103 /* user-defined varying */
3104 GLint varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
, size
);
3105 store
= _slang_new_ir_storage(PROGRAM_VARYING
, varyingLoc
, size
);
3108 /* pre-defined varying, like gl_Color or gl_TexCoord */
3109 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
3111 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
3114 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
3115 assert(index
< FRAG_ATTRIB_MAX
);
3118 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
3120 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
3121 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
3122 assert(index
< VERT_RESULT_MAX
);
3124 if (dbg
) printf("V/F ");
3126 if (dbg
) printf("VARYING ");
3128 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
3130 /* user-defined vertex attribute */
3131 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3132 const GLint attr
= -1; /* unknown */
3133 GLint index
= _mesa_add_attribute(prog
->Attributes
, varName
,
3136 store
= _slang_new_ir_storage(PROGRAM_INPUT
,
3137 VERT_ATTRIB_GENERIC0
+ index
, size
);
3140 /* pre-defined vertex attrib */
3142 GLint index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
,
3144 GLint size
= 4; /* XXX? */
3146 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
3148 if (dbg
) printf("ATTRIB ");
3150 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
3151 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
3152 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
3154 GLint size
= 4; /* XXX? */
3155 store
= _slang_new_ir_storage_swz(PROGRAM_INPUT
, index
, size
, swizzle
);
3156 if (dbg
) printf("INPUT ");
3158 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
3159 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
3160 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
3161 GLint size
= 4; /* XXX? */
3162 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
3165 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
3166 GLint size
= 4; /* XXX? */
3167 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
3168 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
3170 if (dbg
) printf("OUTPUT ");
3172 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
3173 /* pre-defined global constant, like gl_MaxLights */
3174 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3175 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
3176 if (dbg
) printf("CONST ");
3179 /* ordinary variable (may be const) */
3182 /* IR node to declare the variable */
3183 n
= _slang_gen_var_decl(A
, var
);
3185 /* IR code for the var's initializer, if present */
3186 if (var
->initializer
) {
3187 slang_ir_node
*lhs
, *rhs
, *init
;
3189 /* Generate IR_MOVE instruction to initialize the variable */
3190 lhs
= new_node0(IR_VAR
);
3192 lhs
->Store
= n
->Store
;
3194 /* constant folding, etc */
3195 _slang_simplify(var
->initializer
, &A
->space
, A
->atoms
);
3197 rhs
= _slang_gen_operation(A
, var
->initializer
);
3199 init
= new_node2(IR_MOVE
, lhs
, rhs
);
3200 n
= new_seq(n
, init
);
3203 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_FALSE
, A
->log
);
3205 _slang_free_ir_tree(n
);
3208 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
3209 store
? store
->Index
: -2);
3212 var
->aux
= store
; /* save var's storage info */
3214 var
->declared
= GL_TRUE
;
3221 * Produce an IR tree from a function AST (fun->body).
3222 * Then call the code emitter to convert the IR tree into gl_program
3226 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
3229 GLboolean success
= GL_TRUE
;
3231 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
3232 /* we only really generate code for main, all other functions get
3233 * inlined or codegen'd upon an actual call.
3236 /* do some basic error checking though */
3237 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
3238 /* check that non-void functions actually return something */
3240 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
3242 slang_info_log_error(A
->log
,
3243 "function \"%s\" has no return statement",
3244 (char *) fun
->header
.a_name
);
3246 "function \"%s\" has no return statement\n",
3247 (char *) fun
->header
.a_name
);
3252 return GL_TRUE
; /* not an error */
3256 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
3257 slang_print_function(fun
, 1);
3260 /* should have been allocated earlier: */
3261 assert(A
->program
->Parameters
);
3262 assert(A
->program
->Varying
);
3263 assert(A
->vartable
);
3265 A
->CurFunction
= fun
;
3267 /* fold constant expressions, etc. */
3268 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
3271 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
3272 slang_print_function(fun
, 1);
3275 /* Create an end-of-function label */
3276 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
3278 /* push new vartable scope */
3279 _slang_push_var_table(A
->vartable
);
3281 /* Generate IR tree for the function body code */
3282 n
= _slang_gen_operation(A
, fun
->body
);
3284 n
= new_node1(IR_SCOPE
, n
);
3286 /* pop vartable, restore previous */
3287 _slang_pop_var_table(A
->vartable
);
3290 /* XXX record error */
3294 /* append an end-of-function-label to IR tree */
3295 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
3297 /*_slang_label_delete(A->curFuncEndLabel);*/
3298 A
->curFuncEndLabel
= NULL
;
3301 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
3302 slang_print_function(fun
, 1);
3305 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
3306 _slang_print_ir_tree(n
, 0);
3309 printf("************* End codegen function ************\n\n");
3312 /* Emit program instructions */
3313 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
, A
->log
);
3314 _slang_free_ir_tree(n
);
3316 /* free codegen context */
3318 _mesa_free(A->codegen);