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_statevars.h"
47 #include "slang_typeinfo.h"
48 #include "slang_codegen.h"
49 #include "slang_compile.h"
50 #include "slang_label.h"
51 #include "slang_mem.h"
52 #include "slang_simplify.h"
53 #include "slang_emit.h"
54 #include "slang_vartable.h"
56 #include "slang_print.h"
59 static slang_ir_node
*
60 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
);
64 is_sampler_type(const slang_fully_specified_type
*t
)
66 switch (t
->specifier
.type
) {
67 case SLANG_SPEC_SAMPLER1D
:
68 case SLANG_SPEC_SAMPLER2D
:
69 case SLANG_SPEC_SAMPLER3D
:
70 case SLANG_SPEC_SAMPLERCUBE
:
71 case SLANG_SPEC_SAMPLER1DSHADOW
:
72 case SLANG_SPEC_SAMPLER2DSHADOW
:
73 case SLANG_SPEC_SAMPLER2DRECT
:
74 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
83 * Return the offset (in floats or ints) of the named field within
84 * the given struct. Return -1 if field not found.
85 * If field is NULL, return the size of the struct instead.
88 _slang_field_offset(const slang_type_specifier
*spec
, slang_atom field
)
92 for (i
= 0; i
< spec
->_struct
->fields
->num_variables
; i
++) {
93 const slang_variable
*v
= spec
->_struct
->fields
->variables
[i
];
94 const GLuint sz
= _slang_sizeof_type_specifier(&v
->type
.specifier
);
96 /* types larger than 1 float are register (4-float) aligned */
97 offset
= (offset
+ 3) & ~3;
99 if (field
&& v
->a_name
== field
) {
105 return -1; /* field not found */
107 return offset
; /* struct size */
112 * Return the size (in floats) of the given type specifier.
113 * If the size is greater than 4, the size should be a multiple of 4
114 * so that the correct number of 4-float registers are allocated.
115 * For example, a mat3x2 is size 12 because we want to store the
116 * 3 columns in 3 float[4] registers.
119 _slang_sizeof_type_specifier(const slang_type_specifier
*spec
)
122 switch (spec
->type
) {
123 case SLANG_SPEC_VOID
:
126 case SLANG_SPEC_BOOL
:
129 case SLANG_SPEC_BVEC2
:
132 case SLANG_SPEC_BVEC3
:
135 case SLANG_SPEC_BVEC4
:
141 case SLANG_SPEC_IVEC2
:
144 case SLANG_SPEC_IVEC3
:
147 case SLANG_SPEC_IVEC4
:
150 case SLANG_SPEC_FLOAT
:
153 case SLANG_SPEC_VEC2
:
156 case SLANG_SPEC_VEC3
:
159 case SLANG_SPEC_VEC4
:
162 case SLANG_SPEC_MAT2
:
163 sz
= 2 * 4; /* 2 columns (regs) */
165 case SLANG_SPEC_MAT3
:
168 case SLANG_SPEC_MAT4
:
171 case SLANG_SPEC_MAT23
:
172 sz
= 2 * 4; /* 2 columns (regs) */
174 case SLANG_SPEC_MAT32
:
175 sz
= 3 * 4; /* 3 columns (regs) */
177 case SLANG_SPEC_MAT24
:
180 case SLANG_SPEC_MAT42
:
181 sz
= 4 * 4; /* 4 columns (regs) */
183 case SLANG_SPEC_MAT34
:
186 case SLANG_SPEC_MAT43
:
187 sz
= 4 * 4; /* 4 columns (regs) */
189 case SLANG_SPEC_SAMPLER1D
:
190 case SLANG_SPEC_SAMPLER2D
:
191 case SLANG_SPEC_SAMPLER3D
:
192 case SLANG_SPEC_SAMPLERCUBE
:
193 case SLANG_SPEC_SAMPLER1DSHADOW
:
194 case SLANG_SPEC_SAMPLER2DSHADOW
:
195 case SLANG_SPEC_SAMPLER2DRECT
:
196 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
197 sz
= 1; /* a sampler is basically just an integer index */
199 case SLANG_SPEC_STRUCT
:
200 sz
= _slang_field_offset(spec
, 0); /* special use */
202 sz
= (sz
+ 3) & ~0x3; /* round up to multiple of four */
205 case SLANG_SPEC_ARRAY
:
206 sz
= _slang_sizeof_type_specifier(spec
->_array
);
209 _mesa_problem(NULL
, "Unexpected type in _slang_sizeof_type_specifier()");
214 /* if size is > 4, it should be a multiple of four */
215 assert((sz
& 0x3) == 0);
222 * Establish the binding between a slang_ir_node and a slang_variable.
223 * Then, allocate/attach a slang_ir_storage object to the IR node if needed.
224 * The IR node must be a IR_VAR or IR_VAR_DECL node.
225 * \param n the IR node
226 * \param var the variable to associate with the IR node
229 _slang_attach_storage(slang_ir_node
*n
, slang_variable
*var
)
233 assert(n
->Opcode
== IR_VAR
|| n
->Opcode
== IR_VAR_DECL
);
234 assert(!n
->Var
|| n
->Var
== var
);
239 /* need to setup storage */
240 if (n
->Var
&& n
->Var
->aux
) {
241 /* node storage info = var storage info */
242 n
->Store
= (slang_ir_storage
*) n
->Var
->aux
;
245 /* alloc new storage info */
246 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -1, -5);
248 n
->Var
->aux
= n
->Store
;
256 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
257 * or -1 if the type is not a sampler.
260 sampler_to_texture_index(const slang_type_specifier_type type
)
263 case SLANG_SPEC_SAMPLER1D
:
264 return TEXTURE_1D_INDEX
;
265 case SLANG_SPEC_SAMPLER2D
:
266 return TEXTURE_2D_INDEX
;
267 case SLANG_SPEC_SAMPLER3D
:
268 return TEXTURE_3D_INDEX
;
269 case SLANG_SPEC_SAMPLERCUBE
:
270 return TEXTURE_CUBE_INDEX
;
271 case SLANG_SPEC_SAMPLER1DSHADOW
:
272 return TEXTURE_1D_INDEX
; /* XXX fix */
273 case SLANG_SPEC_SAMPLER2DSHADOW
:
274 return TEXTURE_2D_INDEX
; /* XXX fix */
275 case SLANG_SPEC_SAMPLER2DRECT
:
276 return TEXTURE_RECT_INDEX
;
277 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
278 return TEXTURE_RECT_INDEX
; /* XXX fix */
285 #define SWIZZLE_ZWWW MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
288 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
289 * a vertex or fragment program input variable. Return -1 if the input
291 * XXX return size too
294 _slang_input_index(const char *name
, GLenum target
, GLuint
*swizzleOut
)
301 static const struct input_info vertInputs
[] = {
302 { "gl_Vertex", VERT_ATTRIB_POS
, SWIZZLE_NOOP
},
303 { "gl_Normal", VERT_ATTRIB_NORMAL
, SWIZZLE_NOOP
},
304 { "gl_Color", VERT_ATTRIB_COLOR0
, SWIZZLE_NOOP
},
305 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
, SWIZZLE_NOOP
},
306 { "gl_FogCoord", VERT_ATTRIB_FOG
, SWIZZLE_XXXX
},
307 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
, SWIZZLE_NOOP
},
308 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
, SWIZZLE_NOOP
},
309 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
, SWIZZLE_NOOP
},
310 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
, SWIZZLE_NOOP
},
311 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
, SWIZZLE_NOOP
},
312 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
, SWIZZLE_NOOP
},
313 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
, SWIZZLE_NOOP
},
314 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
, SWIZZLE_NOOP
},
315 { NULL
, 0, SWIZZLE_NOOP
}
317 static const struct input_info fragInputs
[] = {
318 { "gl_FragCoord", FRAG_ATTRIB_WPOS
, SWIZZLE_NOOP
},
319 { "gl_Color", FRAG_ATTRIB_COL0
, SWIZZLE_NOOP
},
320 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
, SWIZZLE_NOOP
},
321 { "gl_TexCoord", FRAG_ATTRIB_TEX0
, SWIZZLE_NOOP
},
322 /* note: we're packing several quantities into the fogcoord vector */
323 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_XXXX
},
324 { "gl_FrontFacing", FRAG_ATTRIB_FOGC
, SWIZZLE_YYYY
}, /*XXX*/
325 { "gl_PointCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_ZWWW
},
326 { NULL
, 0, SWIZZLE_NOOP
}
329 const struct input_info
*inputs
330 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertInputs
: fragInputs
;
332 ASSERT(MAX_TEXTURE_UNITS
== 8); /* if this fails, fix vertInputs above */
334 for (i
= 0; inputs
[i
].Name
; i
++) {
335 if (strcmp(inputs
[i
].Name
, name
) == 0) {
337 *swizzleOut
= inputs
[i
].Swizzle
;
338 return inputs
[i
].Attrib
;
346 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
347 * a vertex or fragment program output variable. Return -1 for an invalid
351 _slang_output_index(const char *name
, GLenum target
)
357 static const struct output_info vertOutputs
[] = {
358 { "gl_Position", VERT_RESULT_HPOS
},
359 { "gl_FrontColor", VERT_RESULT_COL0
},
360 { "gl_BackColor", VERT_RESULT_BFC0
},
361 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
},
362 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
},
363 { "gl_TexCoord", VERT_RESULT_TEX0
},
364 { "gl_FogFragCoord", VERT_RESULT_FOGC
},
365 { "gl_PointSize", VERT_RESULT_PSIZ
},
368 static const struct output_info fragOutputs
[] = {
369 { "gl_FragColor", FRAG_RESULT_COLR
},
370 { "gl_FragDepth", FRAG_RESULT_DEPR
},
371 { "gl_FragData", FRAG_RESULT_DATA0
},
375 const struct output_info
*outputs
376 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertOutputs
: fragOutputs
;
378 for (i
= 0; outputs
[i
].Name
; i
++) {
379 if (strcmp(outputs
[i
].Name
, name
) == 0) {
381 return outputs
[i
].Attrib
;
389 /**********************************************************************/
393 * Map "_asm foo" to IR_FOO, etc.
398 slang_ir_opcode Opcode
;
399 GLuint HaveRetValue
, NumParams
;
403 static slang_asm_info AsmInfo
[] = {
405 { "vec4_add", IR_ADD
, 1, 2 },
406 { "vec4_subtract", IR_SUB
, 1, 2 },
407 { "vec4_multiply", IR_MUL
, 1, 2 },
408 { "vec4_dot", IR_DOT4
, 1, 2 },
409 { "vec3_dot", IR_DOT3
, 1, 2 },
410 { "vec3_cross", IR_CROSS
, 1, 2 },
411 { "vec4_lrp", IR_LRP
, 1, 3 },
412 { "vec4_min", IR_MIN
, 1, 2 },
413 { "vec4_max", IR_MAX
, 1, 2 },
414 { "vec4_clamp", IR_CLAMP
, 1, 3 },
415 { "vec4_seq", IR_SEQUAL
, 1, 2 },
416 { "vec4_sne", IR_SNEQUAL
, 1, 2 },
417 { "vec4_sge", IR_SGE
, 1, 2 },
418 { "vec4_sgt", IR_SGT
, 1, 2 },
419 { "vec4_sle", IR_SLE
, 1, 2 },
420 { "vec4_slt", IR_SLT
, 1, 2 },
422 { "vec4_floor", IR_FLOOR
, 1, 1 },
423 { "vec4_frac", IR_FRAC
, 1, 1 },
424 { "vec4_abs", IR_ABS
, 1, 1 },
425 { "vec4_negate", IR_NEG
, 1, 1 },
426 { "vec4_ddx", IR_DDX
, 1, 1 },
427 { "vec4_ddy", IR_DDY
, 1, 1 },
428 /* float binary op */
429 { "float_power", IR_POW
, 1, 2 },
430 /* texture / sampler */
431 { "vec4_tex1d", IR_TEX
, 1, 2 },
432 { "vec4_texb1d", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
433 { "vec4_texp1d", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
434 { "vec4_tex2d", IR_TEX
, 1, 2 },
435 { "vec4_texb2d", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
436 { "vec4_texp2d", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
437 { "vec4_tex3d", IR_TEX
, 1, 2 },
438 { "vec4_texb3d", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
439 { "vec4_texp3d", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
440 { "vec4_texcube", IR_TEX
, 1, 2 }, /* cubemap */
441 { "vec4_tex_rect", IR_TEX
, 1, 2 }, /* rectangle */
442 { "vec4_texp_rect", IR_TEX
, 1, 2 },/* rectangle w/ projection */
445 { "int_to_float", IR_I_TO_F
, 1, 1 },
446 { "float_to_int", IR_F_TO_I
, 1, 1 },
447 { "float_exp", IR_EXP
, 1, 1 },
448 { "float_exp2", IR_EXP2
, 1, 1 },
449 { "float_log2", IR_LOG2
, 1, 1 },
450 { "float_rsq", IR_RSQ
, 1, 1 },
451 { "float_rcp", IR_RCP
, 1, 1 },
452 { "float_sine", IR_SIN
, 1, 1 },
453 { "float_cosine", IR_COS
, 1, 1 },
454 { "float_noise1", IR_NOISE1
, 1, 1},
455 { "float_noise2", IR_NOISE2
, 1, 1},
456 { "float_noise3", IR_NOISE3
, 1, 1},
457 { "float_noise4", IR_NOISE4
, 1, 1},
459 { NULL
, IR_NOP
, 0, 0 }
463 static slang_ir_node
*
464 new_node3(slang_ir_opcode op
,
465 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
467 slang_ir_node
*n
= (slang_ir_node
*) _slang_alloc(sizeof(slang_ir_node
));
473 n
->Writemask
= WRITEMASK_XYZW
;
474 n
->InstLocation
= -1;
479 static slang_ir_node
*
480 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
482 return new_node3(op
, c0
, c1
, NULL
);
485 static slang_ir_node
*
486 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
488 return new_node3(op
, c0
, NULL
, NULL
);
491 static slang_ir_node
*
492 new_node0(slang_ir_opcode op
)
494 return new_node3(op
, NULL
, NULL
, NULL
);
499 * Create sequence of two nodes.
501 static slang_ir_node
*
502 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
508 return new_node2(IR_SEQ
, left
, right
);
511 static slang_ir_node
*
512 new_label(slang_label
*label
)
514 slang_ir_node
*n
= new_node0(IR_LABEL
);
521 static slang_ir_node
*
522 new_float_literal(const float v
[4], GLuint size
)
524 slang_ir_node
*n
= new_node0(IR_FLOAT
);
526 COPY_4V(n
->Value
, v
);
527 /* allocate a storage object, but compute actual location (Index) later */
528 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
533 static slang_ir_node
*
534 new_not(slang_ir_node
*n
)
536 return new_node1(IR_NOT
, n
);
541 * Non-inlined function call.
543 static slang_ir_node
*
544 new_function_call(slang_ir_node
*code
, slang_label
*name
)
546 slang_ir_node
*n
= new_node1(IR_CALL
, code
);
555 * Unconditional jump.
557 static slang_ir_node
*
558 new_return(slang_label
*dest
)
560 slang_ir_node
*n
= new_node0(IR_RETURN
);
568 static slang_ir_node
*
569 new_loop(slang_ir_node
*body
)
571 return new_node1(IR_LOOP
, body
);
575 static slang_ir_node
*
576 new_break(slang_ir_node
*loopNode
)
578 slang_ir_node
*n
= new_node0(IR_BREAK
);
580 assert(loopNode
->Opcode
== IR_LOOP
);
582 /* insert this node at head of linked list */
583 n
->List
= loopNode
->List
;
591 * Make new IR_BREAK_IF_TRUE.
593 static slang_ir_node
*
594 new_break_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
598 assert(loopNode
->Opcode
== IR_LOOP
);
599 n
= new_node1(IR_BREAK_IF_TRUE
, cond
);
601 /* insert this node at head of linked list */
602 n
->List
= loopNode
->List
;
610 * Make new IR_CONT_IF_TRUE node.
612 static slang_ir_node
*
613 new_cont_if_true(slang_ir_node
*loopNode
, slang_ir_node
*cond
)
617 assert(loopNode
->Opcode
== IR_LOOP
);
618 n
= new_node1(IR_CONT_IF_TRUE
, cond
);
620 /* insert this node at head of linked list */
621 n
->List
= loopNode
->List
;
628 static slang_ir_node
*
629 new_cond(slang_ir_node
*n
)
631 slang_ir_node
*c
= new_node1(IR_COND
, n
);
636 static slang_ir_node
*
637 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
639 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
644 * New IR_VAR node - a reference to a previously declared variable.
646 static slang_ir_node
*
647 new_var(slang_assemble_ctx
*A
, slang_operation
*oper
, slang_atom name
)
650 slang_variable
*var
= _slang_locate_variable(oper
->locals
, name
, GL_TRUE
);
654 assert(!oper
->var
|| oper
->var
== var
);
656 n
= new_node0(IR_VAR
);
658 _slang_attach_storage(n
, var
);
665 * Check if the given function is really just a wrapper for a
666 * basic assembly instruction.
669 slang_is_asm_function(const slang_function
*fun
)
671 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
672 fun
->body
->num_children
== 1 &&
673 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
681 _slang_is_noop(const slang_operation
*oper
)
684 oper
->type
== SLANG_OPER_VOID
||
685 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
693 * Recursively search tree for a node of the given type.
695 static slang_operation
*
696 _slang_find_node_type(slang_operation
*oper
, slang_operation_type type
)
699 if (oper
->type
== type
)
701 for (i
= 0; i
< oper
->num_children
; i
++) {
702 slang_operation
*p
= _slang_find_node_type(&oper
->children
[i
], type
);
711 slang_resolve_variable(slang_operation
*oper
)
713 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
714 oper
->var
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
720 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
723 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
724 GLuint substCount
, slang_variable
**substOld
,
725 slang_operation
**substNew
, GLboolean isLHS
)
727 switch (oper
->type
) {
728 case SLANG_OPER_VARIABLE_DECL
:
730 slang_variable
*v
= _slang_locate_variable(oper
->locals
,
731 oper
->a_id
, GL_TRUE
);
733 if (v
->initializer
&& oper
->num_children
== 0) {
734 /* set child of oper to copy of initializer */
735 oper
->num_children
= 1;
736 oper
->children
= slang_operation_new(1);
737 slang_operation_copy(&oper
->children
[0], v
->initializer
);
739 if (oper
->num_children
== 1) {
740 /* the initializer */
741 slang_substitute(A
, &oper
->children
[0], substCount
,
742 substOld
, substNew
, GL_FALSE
);
746 case SLANG_OPER_IDENTIFIER
:
747 assert(oper
->num_children
== 0);
748 if (1/**!isLHS XXX FIX */) {
749 slang_atom id
= oper
->a_id
;
752 v
= _slang_locate_variable(oper
->locals
, id
, GL_TRUE
);
754 _mesa_problem(NULL
, "var %s not found!\n", (char *) oper
->a_id
);
758 /* look for a substitution */
759 for (i
= 0; i
< substCount
; i
++) {
760 if (v
== substOld
[i
]) {
761 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
762 #if 0 /* DEBUG only */
763 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
764 assert(substNew
[i
]->var
);
765 assert(substNew
[i
]->var
->a_name
);
766 printf("Substitute %s with %s in id node %p\n",
767 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
771 printf("Substitute %s with %f in id node %p\n",
772 (char*)v
->a_name
, substNew
[i
]->literal
[0],
776 slang_operation_copy(oper
, substNew
[i
]);
783 case SLANG_OPER_RETURN
:
784 /* do return replacement here too */
785 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
786 if (oper
->num_children
== 1 && !_slang_is_noop(&oper
->children
[0])) {
792 * then do substitutions on the assignment.
794 slang_operation
*blockOper
, *assignOper
, *returnOper
;
796 /* check if function actually has a return type */
797 assert(A
->CurFunction
);
798 if (A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
799 slang_info_log_error(A
->log
, "illegal return expression");
803 blockOper
= slang_operation_new(1);
804 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
805 blockOper
->num_children
= 2;
806 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
807 blockOper
->children
= slang_operation_new(2);
808 assignOper
= blockOper
->children
+ 0;
809 returnOper
= blockOper
->children
+ 1;
811 assignOper
->type
= SLANG_OPER_ASSIGN
;
812 assignOper
->num_children
= 2;
813 assignOper
->locals
->outer_scope
= blockOper
->locals
;
814 assignOper
->children
= slang_operation_new(2);
815 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
816 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
817 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
819 slang_operation_copy(&assignOper
->children
[1],
822 returnOper
->type
= SLANG_OPER_RETURN
; /* return w/ no value */
823 assert(returnOper
->num_children
== 0);
825 /* do substitutions on the "__retVal = expr" sub-tree */
826 slang_substitute(A
, assignOper
,
827 substCount
, substOld
, substNew
, GL_FALSE
);
829 /* install new code */
830 slang_operation_copy(oper
, blockOper
);
831 slang_operation_destruct(blockOper
);
834 /* check if return value was expected */
835 assert(A
->CurFunction
);
836 if (A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
837 slang_info_log_error(A
->log
, "return statement requires an expression");
843 case SLANG_OPER_ASSIGN
:
844 case SLANG_OPER_SUBSCRIPT
:
846 * child[0] can't have substitutions but child[1] can.
848 slang_substitute(A
, &oper
->children
[0],
849 substCount
, substOld
, substNew
, GL_TRUE
);
850 slang_substitute(A
, &oper
->children
[1],
851 substCount
, substOld
, substNew
, GL_FALSE
);
853 case SLANG_OPER_FIELD
:
855 slang_substitute(A
, &oper
->children
[0],
856 substCount
, substOld
, substNew
, GL_TRUE
);
861 for (i
= 0; i
< oper
->num_children
; i
++)
862 slang_substitute(A
, &oper
->children
[i
],
863 substCount
, substOld
, substNew
, GL_FALSE
);
871 * Produce inline code for a call to an assembly instruction.
872 * This is typically used to compile a call to a built-in function like this:
874 * vec4 mix(const vec4 x, const vec4 y, const vec4 a)
876 * __asm vec4_lrp __retVal, a, y, x;
879 * We basically translate a SLANG_OPER_CALL into a SLANG_OPER_ASM.
881 static slang_operation
*
882 slang_inline_asm_function(slang_assemble_ctx
*A
,
883 slang_function
*fun
, slang_operation
*oper
)
885 const GLuint numArgs
= oper
->num_children
;
887 slang_operation
*inlined
;
888 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
889 slang_variable
**substOld
;
890 slang_operation
**substNew
;
892 ASSERT(slang_is_asm_function(fun
));
893 ASSERT(fun
->param_count
== numArgs
+ haveRetValue
);
896 printf("Inline %s as %s\n",
897 (char*) fun->header.a_name,
898 (char*) fun->body->children[0].a_id);
902 * We'll substitute formal params with actual args in the asm call.
904 substOld
= (slang_variable
**)
905 _slang_alloc(numArgs
* sizeof(slang_variable
*));
906 substNew
= (slang_operation
**)
907 _slang_alloc(numArgs
* sizeof(slang_operation
*));
908 for (i
= 0; i
< numArgs
; i
++) {
909 substOld
[i
] = fun
->parameters
->variables
[i
];
910 substNew
[i
] = oper
->children
+ i
;
913 /* make a copy of the code to inline */
914 inlined
= slang_operation_new(1);
915 slang_operation_copy(inlined
, &fun
->body
->children
[0]);
917 /* get rid of the __retVal child */
918 for (i
= 0; i
< numArgs
; i
++) {
919 inlined
->children
[i
] = inlined
->children
[i
+ 1];
921 inlined
->num_children
--;
924 /* now do formal->actual substitutions */
925 slang_substitute(A
, inlined
, numArgs
, substOld
, substNew
, GL_FALSE
);
927 _slang_free(substOld
);
928 _slang_free(substNew
);
935 * Inline the given function call operation.
936 * Return a new slang_operation that corresponds to the inlined code.
938 static slang_operation
*
939 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
940 slang_operation
*oper
, slang_operation
*returnOper
)
947 ParamMode
*paramMode
;
948 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
949 const GLuint numArgs
= oper
->num_children
;
950 const GLuint totalArgs
= numArgs
+ haveRetValue
;
951 slang_operation
*args
= oper
->children
;
952 slang_operation
*inlined
, *top
;
953 slang_variable
**substOld
;
954 slang_operation
**substNew
;
955 GLuint substCount
, numCopyIn
, i
;
956 slang_function
*prevFunction
;
959 prevFunction
= A
->CurFunction
;
960 A
->CurFunction
= fun
;
962 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
963 assert(fun
->param_count
== totalArgs
);
965 /* allocate temporary arrays */
966 paramMode
= (ParamMode
*)
967 _slang_alloc(totalArgs
* sizeof(ParamMode
));
968 substOld
= (slang_variable
**)
969 _slang_alloc(totalArgs
* sizeof(slang_variable
*));
970 substNew
= (slang_operation
**)
971 _slang_alloc(totalArgs
* sizeof(slang_operation
*));
974 printf("Inline call to %s (total vars=%d nparams=%d)\n",
975 (char *) fun
->header
.a_name
,
976 fun
->parameters
->num_variables
, numArgs
);
979 if (haveRetValue
&& !returnOper
) {
980 /* Create 3-child comma sequence for inlined code:
981 * child[0]: declare __resultTmp
982 * child[1]: inlined function body
983 * child[2]: __resultTmp
985 slang_operation
*commaSeq
;
986 slang_operation
*declOper
= NULL
;
987 slang_variable
*resultVar
;
989 commaSeq
= slang_operation_new(1);
990 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
991 assert(commaSeq
->locals
);
992 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
993 commaSeq
->num_children
= 3;
994 commaSeq
->children
= slang_operation_new(3);
995 /* allocate the return var */
996 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
998 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
999 (void*)commaSeq->locals, (char *) fun->header.a_name);
1002 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
1003 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
1004 resultVar
->isTemp
= GL_TRUE
;
1006 /* child[0] = __resultTmp declaration */
1007 declOper
= &commaSeq
->children
[0];
1008 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
1009 declOper
->a_id
= resultVar
->a_name
;
1010 declOper
->locals
->outer_scope
= commaSeq
->locals
;
1012 /* child[1] = function body */
1013 inlined
= &commaSeq
->children
[1];
1014 inlined
->locals
->outer_scope
= commaSeq
->locals
;
1016 /* child[2] = __resultTmp reference */
1017 returnOper
= &commaSeq
->children
[2];
1018 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
1019 returnOper
->a_id
= resultVar
->a_name
;
1020 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
1025 top
= inlined
= slang_operation_new(1);
1026 /* XXXX this may be inappropriate!!!! */
1027 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1031 assert(inlined
->locals
);
1033 /* Examine the parameters, look for inout/out params, look for possible
1034 * substitutions, etc:
1035 * param type behaviour
1036 * in copy actual to local
1037 * const in substitute param with actual
1041 for (i
= 0; i
< totalArgs
; i
++) {
1042 slang_variable
*p
= fun
->parameters
->variables
[i
];
1044 printf("Param %d: %s %s \n", i,
1045 slang_type_qual_string(p->type.qualifier),
1046 (char *) p->a_name);
1048 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
1049 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
1050 /* an output param */
1051 slang_operation
*arg
;
1056 paramMode
[i
] = SUBST
;
1058 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
1059 slang_resolve_variable(arg
);
1061 /* replace parameter 'p' with argument 'arg' */
1062 substOld
[substCount
] = p
;
1063 substNew
[substCount
] = arg
; /* will get copied */
1066 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
1067 /* a constant input param */
1068 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
1069 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
1070 /* replace all occurances of this parameter variable with the
1071 * actual argument variable or a literal.
1073 paramMode
[i
] = SUBST
;
1074 slang_resolve_variable(&args
[i
]);
1075 substOld
[substCount
] = p
;
1076 substNew
[substCount
] = &args
[i
]; /* will get copied */
1080 paramMode
[i
] = COPY_IN
;
1084 paramMode
[i
] = COPY_IN
;
1086 assert(paramMode
[i
]);
1089 /* actual code inlining: */
1090 slang_operation_copy(inlined
, fun
->body
);
1092 /*** XXX review this */
1093 assert(inlined
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
);
1094 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
1097 printf("======================= orig body code ======================\n");
1098 printf("=== params scope = %p\n", (void*) fun
->parameters
);
1099 slang_print_tree(fun
->body
, 8);
1100 printf("======================= copied code =========================\n");
1101 slang_print_tree(inlined
, 8);
1104 /* do parameter substitution in inlined code: */
1105 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
1108 printf("======================= subst code ==========================\n");
1109 slang_print_tree(inlined
, 8);
1110 printf("=============================================================\n");
1113 /* New prolog statements: (inserted before the inlined code)
1114 * Copy the 'in' arguments.
1117 for (i
= 0; i
< numArgs
; i
++) {
1118 if (paramMode
[i
] == COPY_IN
) {
1119 slang_variable
*p
= fun
->parameters
->variables
[i
];
1120 /* declare parameter 'p' */
1121 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
1125 printf("COPY_IN %s from expr\n", (char*)p->a_name);
1127 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
1128 assert(decl
->locals
);
1129 decl
->locals
->outer_scope
= inlined
->locals
;
1130 decl
->a_id
= p
->a_name
;
1131 decl
->num_children
= 1;
1132 decl
->children
= slang_operation_new(1);
1134 /* child[0] is the var's initializer */
1135 slang_operation_copy(&decl
->children
[0], args
+ i
);
1141 /* New epilog statements:
1142 * 1. Create end of function label to jump to from return statements.
1143 * 2. Copy the 'out' parameter vars
1146 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1148 inlined
->num_children
);
1149 lab
->type
= SLANG_OPER_LABEL
;
1150 lab
->label
= A
->curFuncEndLabel
;
1153 for (i
= 0; i
< totalArgs
; i
++) {
1154 if (paramMode
[i
] == COPY_OUT
) {
1155 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1156 /* actualCallVar = outParam */
1157 /*if (i > 0 || !haveRetValue)*/
1158 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1160 inlined
->num_children
);
1161 ass
->type
= SLANG_OPER_ASSIGN
;
1162 ass
->num_children
= 2;
1163 ass
->locals
->outer_scope
= inlined
->locals
;
1164 ass
->children
= slang_operation_new(2);
1165 ass
->children
[0] = args
[i
]; /*XXX copy */
1166 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1167 ass
->children
[1].a_id
= p
->a_name
;
1168 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1172 _slang_free(paramMode
);
1173 _slang_free(substOld
);
1174 _slang_free(substNew
);
1177 printf("Done Inline call to %s (total vars=%d nparams=%d)\n",
1178 (char *) fun
->header
.a_name
,
1179 fun
->parameters
->num_variables
, numArgs
);
1180 slang_print_tree(top
, 0);
1184 A
->CurFunction
= prevFunction
;
1190 static slang_ir_node
*
1191 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1192 slang_operation
*oper
, slang_operation
*dest
)
1195 slang_operation
*inlined
;
1196 slang_label
*prevFuncEndLabel
;
1199 prevFuncEndLabel
= A
->curFuncEndLabel
;
1200 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1201 A
->curFuncEndLabel
= _slang_label_new(name
);
1202 assert(A
->curFuncEndLabel
);
1204 if (slang_is_asm_function(fun
) && !dest
) {
1205 /* assemble assembly function - tree style */
1206 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1209 /* non-assembly function */
1210 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1211 if (inlined
&& _slang_find_node_type(inlined
, SLANG_OPER_RETURN
)) {
1212 slang_operation
*callOper
;
1213 /* The function we're calling has one or more 'return' statements.
1214 * So, we can't truly inline this function because we need to
1215 * implement 'return' with RET (and CAL).
1216 * Nevertheless, we performed "inlining" to make a new instance
1217 * of the function body to deal with static register allocation.
1219 * XXX check if there's one 'return' and if it's the very last
1220 * statement in the function - we can optimize that case.
1222 assert(inlined
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1223 inlined
->type
== SLANG_OPER_SEQUENCE
);
1224 if (_slang_function_has_return_value(fun
) && !dest
) {
1225 assert(inlined
->children
[0].type
== SLANG_OPER_VARIABLE_DECL
);
1226 assert(inlined
->children
[2].type
== SLANG_OPER_IDENTIFIER
);
1227 callOper
= &inlined
->children
[1];
1232 callOper
->type
= SLANG_OPER_NON_INLINED_CALL
;
1233 callOper
->fun
= fun
;
1234 callOper
->label
= _slang_label_new_unique((char*) fun
->header
.a_name
);
1241 /* Replace the function call with the inlined block */
1242 slang_operation_destruct(oper
);
1244 _slang_free(inlined
);
1247 assert(inlined
->locals
);
1248 printf("*** Inlined code for call to %s:\n",
1249 (char*) fun
->header
.a_name
);
1250 slang_print_tree(oper
, 10);
1254 n
= _slang_gen_operation(A
, oper
);
1256 /*_slang_label_delete(A->curFuncEndLabel);*/
1257 A
->curFuncEndLabel
= prevFuncEndLabel
;
1263 static slang_asm_info
*
1264 slang_find_asm_info(const char *name
)
1267 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1268 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1277 make_writemask(const char *field
)
1285 mask
|= WRITEMASK_X
;
1290 mask
|= WRITEMASK_Y
;
1295 mask
|= WRITEMASK_Z
;
1300 mask
|= WRITEMASK_W
;
1303 _mesa_problem(NULL
, "invalid writemask in make_writemask()");
1309 return WRITEMASK_XYZW
;
1316 * Generate IR tree for an asm instruction/operation such as:
1317 * __asm vec4_dot __retVal.x, v1, v2;
1319 static slang_ir_node
*
1320 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1321 slang_operation
*dest
)
1323 const slang_asm_info
*info
;
1324 slang_ir_node
*kids
[3], *n
;
1325 GLuint j
, firstOperand
;
1327 assert(oper
->type
== SLANG_OPER_ASM
);
1329 info
= slang_find_asm_info((char *) oper
->a_id
);
1331 _mesa_problem(NULL
, "undefined __asm function %s\n",
1332 (char *) oper
->a_id
);
1335 assert(info
->NumParams
<= 3);
1337 if (info
->NumParams
== oper
->num_children
) {
1338 /* Storage for result is not specified.
1339 * Children[0], [1] are the operands.
1344 /* Storage for result (child[0]) is specified.
1345 * Children[1], [2] are the operands.
1350 /* assemble child(ren) */
1351 kids
[0] = kids
[1] = kids
[2] = NULL
;
1352 for (j
= 0; j
< info
->NumParams
; j
++) {
1353 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1358 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1361 /* Setup n->Store to be a particular location. Otherwise, storage
1362 * for the result (a temporary) will be allocated later.
1364 GLuint writemask
= WRITEMASK_XYZW
;
1365 slang_operation
*dest_oper
;
1368 dest_oper
= &oper
->children
[0];
1369 while (dest_oper
->type
== SLANG_OPER_FIELD
) {
1371 writemask
&= make_writemask((char*) dest_oper
->a_id
);
1372 dest_oper
= &dest_oper
->children
[0];
1375 n0
= _slang_gen_operation(A
, dest_oper
);
1379 n
->Store
= n0
->Store
;
1380 n
->Writemask
= writemask
;
1390 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1393 for (i
= 0; i
< scope
->num_functions
; i
++) {
1394 slang_function
*f
= &scope
->functions
[i
];
1395 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1396 printf(" %s (%d args)\n", name
, f
->param_count
);
1399 if (scope
->outer_scope
)
1400 print_funcs(scope
->outer_scope
, name
);
1405 * Return first function in the scope that has the given name.
1406 * This is the function we'll try to call when there is no exact match
1407 * between function parameters and call arguments.
1409 * XXX we should really create a list of candidate functions and try
1412 static slang_function
*
1413 _slang_first_function(struct slang_function_scope_
*scope
, const char *name
)
1416 for (i
= 0; i
< scope
->num_functions
; i
++) {
1417 slang_function
*f
= &scope
->functions
[i
];
1418 if (strcmp(name
, (char*) f
->header
.a_name
) == 0)
1421 if (scope
->outer_scope
)
1422 return _slang_first_function(scope
->outer_scope
, name
);
1429 * Assemble a function call, given a particular function name.
1430 * \param name the function's name (operators like '*' are possible).
1432 static slang_ir_node
*
1433 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
1434 slang_operation
*oper
, slang_operation
*dest
)
1436 slang_operation
*params
= oper
->children
;
1437 const GLuint param_count
= oper
->num_children
;
1439 slang_function
*fun
;
1441 atom
= slang_atom_pool_atom(A
->atoms
, name
);
1442 if (atom
== SLANG_ATOM_NULL
)
1446 * Use 'name' to find the function to call
1448 fun
= _slang_locate_function(A
->space
.funcs
, atom
, params
, param_count
,
1449 &A
->space
, A
->atoms
, A
->log
);
1451 /* A function with exactly the right parameters/types was not found.
1452 * Try adapting the parameters.
1454 fun
= _slang_first_function(A
->space
.funcs
, name
);
1455 if (!fun
|| !_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
1456 slang_info_log_error(A
->log
, "Function '%s' not found (check argument types)", name
);
1462 return _slang_gen_function_call(A
, fun
, oper
, dest
);
1467 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
1469 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
1470 oper
->type
== SLANG_OPER_LITERAL_INT
||
1471 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
1472 if (oper
->literal
[0])
1478 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
1479 oper
->num_children
== 1) {
1480 return _slang_is_constant_cond(&oper
->children
[0], value
);
1487 * Test if an operation is a scalar or boolean.
1490 _slang_is_scalar_or_boolean(slang_assemble_ctx
*A
, slang_operation
*oper
)
1492 slang_typeinfo type
;
1495 slang_typeinfo_construct(&type
);
1496 _slang_typeof_operation(A
, oper
, &type
);
1497 size
= _slang_sizeof_type_specifier(&type
.spec
);
1498 slang_typeinfo_destruct(&type
);
1504 * Generate loop code using high-level IR_LOOP instruction
1506 static slang_ir_node
*
1507 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1511 * BREAK if !expr (child[0])
1512 * body code (child[1])
1514 slang_ir_node
*prevLoop
, *loop
, *breakIf
, *body
;
1515 GLboolean isConst
, constTrue
;
1517 /* type-check expression */
1518 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
1519 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'while'");
1523 /* Check if loop condition is a constant */
1524 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
1526 if (isConst
&& !constTrue
) {
1527 /* loop is never executed! */
1528 return new_node0(IR_NOP
);
1531 loop
= new_loop(NULL
);
1533 /* save old, push new loop */
1534 prevLoop
= A
->CurLoop
;
1537 if (isConst
&& constTrue
) {
1538 /* while(nonzero constant), no conditional break */
1543 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[0])));
1544 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
1546 body
= _slang_gen_operation(A
, &oper
->children
[1]);
1547 loop
->Children
[0] = new_seq(breakIf
, body
);
1549 /* Do infinite loop detection */
1550 /* loop->List is head of linked list of break/continue nodes */
1551 if (!loop
->List
&& isConst
&& constTrue
) {
1552 /* infinite loop detected */
1553 A
->CurLoop
= prevLoop
; /* clean-up */
1554 slang_info_log_error(A
->log
, "Infinite loop detected!");
1558 /* pop loop, restore prev */
1559 A
->CurLoop
= prevLoop
;
1566 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
1568 static slang_ir_node
*
1569 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1573 * body code (child[0])
1575 * BREAK if !expr (child[1])
1577 slang_ir_node
*prevLoop
, *loop
;
1578 GLboolean isConst
, constTrue
;
1580 /* type-check expression */
1581 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[1])) {
1582 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'do/while'");
1586 loop
= new_loop(NULL
);
1588 /* save old, push new loop */
1589 prevLoop
= A
->CurLoop
;
1593 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
1595 /* Check if loop condition is a constant */
1596 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
1597 if (isConst
&& constTrue
) {
1598 /* do { } while(1) ==> no conditional break */
1599 loop
->Children
[1] = NULL
; /* no tail code */
1603 = new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
1604 loop
->Children
[1] = new_break_if_true(A
->CurLoop
, cond
);
1607 /* XXX we should do infinite loop detection, as above */
1609 /* pop loop, restore prev */
1610 A
->CurLoop
= prevLoop
;
1617 * Generate for-loop using high-level IR_LOOP instruction.
1619 static slang_ir_node
*
1620 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1623 * init code (child[0])
1625 * BREAK if !expr (child[1])
1626 * body code (child[3])
1628 * incr code (child[2]) // XXX continue here
1630 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
1632 init
= _slang_gen_operation(A
, &oper
->children
[0]);
1633 loop
= new_loop(NULL
);
1635 /* save old, push new loop */
1636 prevLoop
= A
->CurLoop
;
1639 cond
= new_cond(new_not(_slang_gen_operation(A
, &oper
->children
[1])));
1640 breakIf
= new_break_if_true(A
->CurLoop
, cond
);
1641 body
= _slang_gen_operation(A
, &oper
->children
[3]);
1642 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
1644 loop
->Children
[0] = new_seq(breakIf
, body
);
1645 loop
->Children
[1] = incr
; /* tail code */
1647 /* pop loop, restore prev */
1648 A
->CurLoop
= prevLoop
;
1650 return new_seq(init
, loop
);
1654 static slang_ir_node
*
1655 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1657 slang_ir_node
*n
, *loopNode
;
1658 assert(oper
->type
== SLANG_OPER_CONTINUE
);
1659 loopNode
= A
->CurLoop
;
1661 assert(loopNode
->Opcode
== IR_LOOP
);
1662 n
= new_node0(IR_CONT
);
1664 n
->Parent
= loopNode
;
1665 /* insert this node at head of linked list */
1666 n
->List
= loopNode
->List
;
1674 * Determine if the given operation is of a specific type.
1677 is_operation_type(const slang_operation
*oper
, slang_operation_type type
)
1679 if (oper
->type
== type
)
1681 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1682 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
1683 oper
->num_children
== 1)
1684 return is_operation_type(&oper
->children
[0], type
);
1691 * Generate IR tree for an if/then/else conditional using high-level
1692 * IR_IF instruction.
1694 static slang_ir_node
*
1695 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1698 * eval expr (child[0])
1705 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
1706 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
1707 GLboolean isConst
, constTrue
;
1709 /* type-check expression */
1710 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
1711 slang_info_log_error(A
->log
, "scalar/boolean expression expected for 'if'");
1715 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
1719 return _slang_gen_operation(A
, &oper
->children
[1]);
1722 /* if (false) ... */
1723 return _slang_gen_operation(A
, &oper
->children
[2]);
1727 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1728 cond
= new_cond(cond
);
1730 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)) {
1731 /* Special case: generate a conditional break */
1732 ifBody
= new_break_if_true(A
->CurLoop
, cond
);
1733 if (haveElseClause
) {
1734 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1735 return new_seq(ifBody
, elseBody
);
1739 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)) {
1740 /* Special case: generate a conditional break */
1741 ifBody
= new_cont_if_true(A
->CurLoop
, cond
);
1742 if (haveElseClause
) {
1743 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1744 return new_seq(ifBody
, elseBody
);
1750 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
1752 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1755 ifNode
= new_if(cond
, ifBody
, elseBody
);
1762 static slang_ir_node
*
1763 _slang_gen_not(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1767 assert(oper
->type
== SLANG_OPER_NOT
);
1769 /* type-check expression */
1770 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
1771 slang_info_log_error(A
->log
,
1772 "scalar/boolean expression expected for '!'");
1776 n
= _slang_gen_operation(A
, &oper
->children
[0]);
1784 static slang_ir_node
*
1785 _slang_gen_xor(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1787 slang_ir_node
*n1
, *n2
;
1789 assert(oper
->type
== SLANG_OPER_LOGICALXOR
);
1791 if (!_slang_is_scalar_or_boolean(A
, &oper
->children
[0]) ||
1792 !_slang_is_scalar_or_boolean(A
, &oper
->children
[0])) {
1793 slang_info_log_error(A
->log
,
1794 "scalar/boolean expressions expected for '^^'");
1798 n1
= _slang_gen_operation(A
, &oper
->children
[0]);
1801 n2
= _slang_gen_operation(A
, &oper
->children
[1]);
1804 return new_node2(IR_NOTEQUAL
, n1
, n2
);
1809 * Generate IR node for storage of a temporary of given size.
1811 static slang_ir_node
*
1812 _slang_gen_temporary(GLint size
)
1814 slang_ir_storage
*store
;
1815 slang_ir_node
*n
= NULL
;
1817 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
1819 n
= new_node0(IR_VAR_DECL
);
1832 * Generate IR node for allocating/declaring a variable.
1834 static slang_ir_node
*
1835 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
)
1838 assert(!is_sampler_type(&var
->type
));
1839 n
= new_node0(IR_VAR_DECL
);
1841 _slang_attach_storage(n
, var
);
1844 assert(n
->Store
== var
->aux
);
1846 assert(n
->Store
->Index
< 0);
1848 n
->Store
->File
= PROGRAM_TEMPORARY
;
1849 n
->Store
->Size
= _slang_sizeof_type_specifier(&n
->Var
->type
.specifier
);
1850 A
->program
->NumTemporaries
++;
1851 assert(n
->Store
->Size
> 0);
1858 * Generate code for a selection expression: b ? x : y
1859 * XXX In some cases we could implement a selection expression
1860 * with an LRP instruction (use the boolean as the interpolant).
1861 * Otherwise, we use an IF/ELSE/ENDIF construct.
1863 static slang_ir_node
*
1864 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
1866 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
1867 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
1868 slang_typeinfo type
;
1871 assert(oper
->type
== SLANG_OPER_SELECT
);
1872 assert(oper
->num_children
== 3);
1874 /* size of x or y's type */
1875 slang_typeinfo_construct(&type
);
1876 _slang_typeof_operation(A
, &oper
->children
[1], &type
);
1877 size
= _slang_sizeof_type_specifier(&type
.spec
);
1881 tmpDecl
= _slang_gen_temporary(size
);
1883 /* the condition (child 0) */
1884 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1885 cond
= new_cond(cond
);
1887 /* if-true body (child 1) */
1888 tmpVar
= new_node0(IR_VAR
);
1889 tmpVar
->Store
= tmpDecl
->Store
;
1890 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
1891 trueNode
= new_node2(IR_MOVE
, tmpVar
, trueExpr
);
1893 /* if-false body (child 2) */
1894 tmpVar
= new_node0(IR_VAR
);
1895 tmpVar
->Store
= tmpDecl
->Store
;
1896 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
1897 falseNode
= new_node2(IR_MOVE
, tmpVar
, falseExpr
);
1899 ifNode
= new_if(cond
, trueNode
, falseNode
);
1902 tmpVar
= new_node0(IR_VAR
);
1903 tmpVar
->Store
= tmpDecl
->Store
;
1905 tree
= new_seq(ifNode
, tmpVar
);
1906 tree
= new_seq(tmpDecl
, tree
);
1908 /*_slang_print_ir_tree(tree, 10);*/
1914 * Generate code for &&.
1916 static slang_ir_node
*
1917 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
1919 /* rewrite "a && b" as "a ? b : false" */
1920 slang_operation
*select
;
1923 select
= slang_operation_new(1);
1924 select
->type
= SLANG_OPER_SELECT
;
1925 select
->num_children
= 3;
1926 select
->children
= slang_operation_new(3);
1928 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
1929 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
1930 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
1931 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
1932 select
->children
[2].literal_size
= 1;
1934 n
= _slang_gen_select(A
, select
);
1940 * Generate code for ||.
1942 static slang_ir_node
*
1943 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
1945 /* rewrite "a || b" as "a ? true : b" */
1946 slang_operation
*select
;
1949 select
= slang_operation_new(1);
1950 select
->type
= SLANG_OPER_SELECT
;
1951 select
->num_children
= 3;
1952 select
->children
= slang_operation_new(3);
1954 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
1955 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
1956 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
1957 select
->children
[1].literal_size
= 1;
1958 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
1960 n
= _slang_gen_select(A
, select
);
1966 * Generate IR tree for a return statement.
1968 static slang_ir_node
*
1969 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
1971 const GLboolean haveReturnValue
1972 = (oper
->num_children
== 1 && oper
->children
[0].type
!= SLANG_OPER_VOID
);
1974 /* error checking */
1975 assert(A
->CurFunction
);
1976 if (haveReturnValue
&&
1977 A
->CurFunction
->header
.type
.specifier
.type
== SLANG_SPEC_VOID
) {
1978 slang_info_log_error(A
->log
, "illegal return expression");
1981 else if (!haveReturnValue
&&
1982 A
->CurFunction
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
1983 slang_info_log_error(A
->log
, "return statement requires an expression");
1987 if (!haveReturnValue
) {
1988 return new_return(A
->curFuncEndLabel
);
1996 * return; // goto __endOfFunction
1998 slang_operation
*assign
;
1999 slang_atom a_retVal
;
2002 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
2008 = _slang_locate_variable(oper
->locals
, a_retVal
, GL_TRUE
);
2010 /* trying to return a value in a void-valued function */
2016 assign
= slang_operation_new(1);
2017 assign
->type
= SLANG_OPER_ASSIGN
;
2018 assign
->num_children
= 2;
2019 assign
->children
= slang_operation_new(2);
2020 /* lhs (__retVal) */
2021 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
2022 assign
->children
[0].a_id
= a_retVal
;
2023 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
2025 /* XXX we might be able to avoid this copy someday */
2026 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
2028 /* assemble the new code */
2029 n
= new_seq(_slang_gen_operation(A
, assign
),
2030 new_return(A
->curFuncEndLabel
));
2032 slang_operation_delete(assign
);
2039 * Generate IR tree for a variable declaration.
2041 static slang_ir_node
*
2042 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
2045 slang_ir_node
*varDecl
;
2047 const char *varName
= (char *) oper
->a_id
;
2049 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
2051 v
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
2054 varDecl
= _slang_gen_var_decl(A
, v
);
2056 if (oper
->num_children
> 0) {
2057 /* child is initializer */
2058 slang_ir_node
*var
, *init
, *rhs
;
2059 assert(oper
->num_children
== 1);
2060 var
= new_var(A
, oper
, oper
->a_id
);
2062 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
2065 /* XXX make copy of this initializer? */
2066 rhs
= _slang_gen_operation(A
, &oper
->children
[0]);
2068 return NULL
; /* must have found an error */
2069 init
= new_node2(IR_MOVE
, var
, rhs
);
2070 /*assert(rhs->Opcode != IR_SEQ);*/
2071 n
= new_seq(varDecl
, init
);
2073 else if (v
->initializer
) {
2074 slang_ir_node
*var
, *init
, *rhs
;
2075 var
= new_var(A
, oper
, oper
->a_id
);
2077 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
2081 /* XXX make copy of this initializer? */
2083 slang_operation dup
;
2084 slang_operation_construct(&dup
);
2085 slang_operation_copy(&dup
, v
->initializer
);
2086 _slang_simplify(&dup
, &A
->space
, A
->atoms
);
2087 rhs
= _slang_gen_operation(A
, &dup
);
2090 _slang_simplify(v
->initializer
, &A
->space
, A
->atoms
);
2091 rhs
= _slang_gen_operation(A
, v
->initializer
);
2097 init
= new_node2(IR_MOVE
, var
, rhs
);
2099 assert(rhs->Opcode != IR_SEQ);
2101 n
= new_seq(varDecl
, init
);
2111 * Generate IR tree for a variable (such as in an expression).
2113 static slang_ir_node
*
2114 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
2116 /* If there's a variable associated with this oper (from inlining)
2117 * use it. Otherwise, use the oper's var id.
2119 slang_atom aVar
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
2120 slang_ir_node
*n
= new_var(A
, oper
, aVar
);
2122 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) aVar
);
2130 * Some write-masked assignments are simple, but others are hard.
2133 * v.xy = vec2(a, b);
2136 * v.zy = vec2(a, b);
2137 * this gets transformed/swizzled into:
2138 * v.zy = vec2(a, b).*yx* (* = don't care)
2139 * This function helps to determine simple vs. non-simple.
2142 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
2144 switch (writemask
) {
2146 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
2148 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
2150 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
2152 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
2154 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
2155 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
2157 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
2158 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
2159 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
2160 case WRITEMASK_XYZW
:
2161 return swizzle
== SWIZZLE_NOOP
;
2169 * Convert the given swizzle into a writemask. In some cases this
2170 * is trivial, in other cases, we'll need to also swizzle the right
2171 * hand side to put components in the right places.
2172 * \param swizzle the incoming swizzle
2173 * \param writemaskOut returns the writemask
2174 * \param swizzleOut swizzle to apply to the right-hand-side
2175 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
2178 swizzle_to_writemask(GLuint swizzle
,
2179 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
2181 GLuint mask
= 0x0, newSwizzle
[4];
2184 /* make new dst writemask, compute size */
2185 for (i
= 0; i
< 4; i
++) {
2186 const GLuint swz
= GET_SWZ(swizzle
, i
);
2187 if (swz
== SWIZZLE_NIL
) {
2191 assert(swz
>= 0 && swz
<= 3);
2194 assert(mask
<= 0xf);
2195 size
= i
; /* number of components in mask/swizzle */
2197 *writemaskOut
= mask
;
2199 /* make new src swizzle, by inversion */
2200 for (i
= 0; i
< 4; i
++) {
2201 newSwizzle
[i
] = i
; /*identity*/
2203 for (i
= 0; i
< size
; i
++) {
2204 const GLuint swz
= GET_SWZ(swizzle
, i
);
2205 newSwizzle
[swz
] = i
;
2207 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
2212 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
2214 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
2216 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
2218 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
2220 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
2228 static slang_ir_node
*
2229 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
2231 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
2234 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -1, -1);
2235 n
->Store
->Swizzle
= swizzle
;
2242 * Generate IR tree for an assignment (=).
2244 static slang_ir_node
*
2245 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
2247 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
2248 /* Check that var is writeable */
2250 = _slang_locate_variable(oper
->children
[0].locals
,
2251 oper
->children
[0].a_id
, GL_TRUE
);
2253 slang_info_log_error(A
->log
, "undefined variable '%s'",
2254 (char *) oper
->children
[0].a_id
);
2257 if (var
->type
.qualifier
== SLANG_QUAL_CONST
||
2258 var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
||
2259 var
->type
.qualifier
== SLANG_QUAL_UNIFORM
||
2260 (var
->type
.qualifier
== SLANG_QUAL_VARYING
&&
2261 A
->program
->Target
== GL_FRAGMENT_PROGRAM_ARB
)) {
2262 slang_info_log_error(A
->log
,
2263 "illegal assignment to read-only variable '%s'",
2264 (char *) oper
->children
[0].a_id
);
2269 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
2270 oper
->children
[1].type
== SLANG_OPER_CALL
) {
2271 /* Special case of: x = f(a, b)
2272 * Replace with f(a, b, x) (where x == hidden __retVal out param)
2274 * XXX this could be even more effective if we could accomodate
2275 * cases such as "v.x = f();" - would help with typical vertex
2279 n
= _slang_gen_function_call_name(A
,
2280 (const char *) oper
->children
[1].a_id
,
2281 &oper
->children
[1], &oper
->children
[0]);
2285 slang_ir_node
*n
, *lhs
, *rhs
;
2286 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
2289 if (!(lhs
->Store
->File
== PROGRAM_OUTPUT
||
2290 lhs
->Store
->File
== PROGRAM_TEMPORARY
||
2291 (lhs
->Store
->File
== PROGRAM_VARYING
&&
2292 A
->program
->Target
== GL_VERTEX_PROGRAM_ARB
) ||
2293 lhs
->Store
->File
== PROGRAM_UNDEFINED
)) {
2294 slang_info_log_error(A
->log
,
2295 "illegal assignment to read-only l-value");
2300 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
2302 /* convert lhs swizzle into writemask */
2303 GLuint writemask
, newSwizzle
;
2304 if (!swizzle_to_writemask(lhs
->Store
->Swizzle
,
2305 &writemask
, &newSwizzle
)) {
2306 /* Non-simple writemask, need to swizzle right hand side in
2307 * order to put components into the right place.
2309 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
2311 n
= new_node2(IR_MOVE
, lhs
, rhs
);
2312 n
->Writemask
= writemask
;
2323 * Generate IR tree for referencing a field in a struct (or basic vector type)
2325 static slang_ir_node
*
2326 _slang_gen_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
2330 /* type of struct */
2331 slang_typeinfo_construct(&ti
);
2332 _slang_typeof_operation(A
, &oper
->children
[0], &ti
);
2334 if (_slang_type_is_vector(ti
.spec
.type
)) {
2335 /* the field should be a swizzle */
2336 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
2340 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2341 slang_info_log_error(A
->log
, "Bad swizzle");
2343 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2348 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2349 /* create new parent node with swizzle */
2351 n
= _slang_gen_swizzle(n
, swizzle
);
2354 else if ( ti
.spec
.type
== SLANG_SPEC_FLOAT
2355 || ti
.spec
.type
== SLANG_SPEC_INT
) {
2356 const GLuint rows
= 1;
2360 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2361 slang_info_log_error(A
->log
, "Bad swizzle");
2363 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2367 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2368 /* create new parent node with swizzle */
2369 n
= _slang_gen_swizzle(n
, swizzle
);
2373 /* the field is a structure member (base.field) */
2374 /* oper->children[0] is the base */
2375 /* oper->a_id is the field name */
2376 slang_ir_node
*base
, *n
;
2377 slang_typeinfo field_ti
;
2378 GLint fieldSize
, fieldOffset
= -1;
2380 slang_typeinfo_construct(&field_ti
);
2381 _slang_typeof_operation(A
, oper
, &field_ti
);
2383 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
2385 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
2387 if (fieldSize
== 0 || fieldOffset
< 0) {
2388 slang_info_log_error(A
->log
,
2389 "\"%s\" is not a member of struct \"%s\"",
2390 (char *) oper
->a_id
,
2391 (char *) ti
.spec
._struct
->a_name
);
2394 assert(fieldSize
>= 0);
2396 base
= _slang_gen_operation(A
, &oper
->children
[0]);
2398 /* error msg should have already been logged */
2402 n
= new_node1(IR_FIELD
, base
);
2404 n
->Field
= (char *) oper
->a_id
;
2405 n
->FieldOffset
= fieldOffset
;
2406 assert(n
->FieldOffset
>= 0);
2407 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
2414 _mesa_problem(NULL
, "glsl structs/fields not supported yet");
2422 * Gen code for array indexing.
2424 static slang_ir_node
*
2425 _slang_gen_subscript(slang_assemble_ctx
* A
, slang_operation
*oper
)
2427 slang_typeinfo array_ti
;
2429 /* get array's type info */
2430 slang_typeinfo_construct(&array_ti
);
2431 _slang_typeof_operation(A
, &oper
->children
[0], &array_ti
);
2433 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
2434 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
2435 /* translate the index into a swizzle/writemask: "v.x=p" */
2436 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
2440 index
= (GLint
) oper
->children
[1].literal
[0];
2441 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
2443 slang_info_log_error(A
->log
, "Invalid array index for vector type");
2447 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2449 /* use swizzle to access the element */
2450 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
2454 n
= _slang_gen_swizzle(n
, swizzle
);
2455 /*n->Store = _slang_clone_ir_storage_swz(n->Store, */
2456 n
->Writemask
= WRITEMASK_X
<< index
;
2461 /* conventional array */
2462 slang_typeinfo elem_ti
;
2463 slang_ir_node
*elem
, *array
, *index
;
2464 GLint elemSize
, arrayLen
;
2466 /* size of array element */
2467 slang_typeinfo_construct(&elem_ti
);
2468 _slang_typeof_operation(A
, oper
, &elem_ti
);
2469 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
2471 if (_slang_type_is_matrix(array_ti
.spec
.type
))
2472 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
2474 arrayLen
= array_ti
.array_len
;
2476 slang_typeinfo_destruct(&array_ti
);
2477 slang_typeinfo_destruct(&elem_ti
);
2479 if (elemSize
<= 0) {
2480 /* unknown var or type */
2481 slang_info_log_error(A
->log
, "Undefined variable or type");
2485 array
= _slang_gen_operation(A
, &oper
->children
[0]);
2486 index
= _slang_gen_operation(A
, &oper
->children
[1]);
2487 if (array
&& index
) {
2489 if (index
->Opcode
== IR_FLOAT
&&
2490 ((int) index
->Value
[0] < 0 ||
2491 (int) index
->Value
[0] >= arrayLen
)) {
2492 slang_info_log_error(A
->log
,
2493 "Array index out of bounds (index=%d size=%d)",
2494 (int) index
->Value
[0], arrayLen
);
2495 _slang_free_ir_tree(array
);
2496 _slang_free_ir_tree(index
);
2500 elem
= new_node2(IR_ELEMENT
, array
, index
);
2501 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
2502 array
->Store
->Index
,
2504 /* XXX try to do some array bounds checking here */
2508 _slang_free_ir_tree(array
);
2509 _slang_free_ir_tree(index
);
2517 * Look for expressions such as: gl_ModelviewMatrix * gl_Vertex
2518 * and replace with this: gl_Vertex * gl_ModelviewMatrixTranpose
2519 * Since matrices are stored in column-major order, the second form of
2520 * multiplication is much more efficient (just 4 dot products).
2523 _slang_check_matmul_optimization(slang_assemble_ctx
*A
, slang_operation
*oper
)
2525 static const struct {
2527 const char *tranpose
;
2529 {"gl_ModelViewMatrix", "gl_ModelViewMatrixTranspose"},
2530 {"gl_ProjectionMatrix", "gl_ProjectionMatrixTranspose"},
2531 {"gl_ModelViewProjectionMatrix", "gl_ModelViewProjectionMatrixTranspose"},
2532 {"gl_TextureMatrix", "gl_TextureMatrixTranspose"},
2533 {"gl_NormalMatrix", "__NormalMatrixTranspose"},
2537 assert(oper
->type
== SLANG_OPER_MULTIPLY
);
2538 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
2540 for (i
= 0; matrices
[i
].orig
; i
++) {
2541 if (oper
->children
[0].a_id
2542 == slang_atom_pool_atom(A
->atoms
, matrices
[i
].orig
)) {
2544 _mesa_printf("Replace %s with %s\n",
2545 matrices[i].orig, matrices[i].tranpose);
2547 assert(oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
);
2548 oper
->children
[0].a_id
2549 = slang_atom_pool_atom(A
->atoms
, matrices
[i
].tranpose
);
2550 /* finally, swap the operands */
2551 _slang_operation_swap(&oper
->children
[0], &oper
->children
[1]);
2560 * Generate IR tree for a slang_operation (AST node)
2562 static slang_ir_node
*
2563 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
2565 switch (oper
->type
) {
2566 case SLANG_OPER_BLOCK_NEW_SCOPE
:
2570 _slang_push_var_table(A
->vartable
);
2572 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
2573 n
= _slang_gen_operation(A
, oper
);
2574 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
2576 _slang_pop_var_table(A
->vartable
);
2579 n
= new_node1(IR_SCOPE
, n
);
2584 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
2585 /* list of operations */
2586 if (oper
->num_children
> 0)
2588 slang_ir_node
*n
, *tree
= NULL
;
2591 for (i
= 0; i
< oper
->num_children
; i
++) {
2592 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
2594 _slang_free_ir_tree(tree
);
2595 return NULL
; /* error must have occured */
2597 tree
= new_seq(tree
, n
);
2601 if (oper
->locals
->num_variables
> 0) {
2604 printf("\n****** Deallocate vars in scope!\n");
2606 for (i
= 0; i
< oper
->locals
->num_variables
; i
++) {
2607 slang_variable
*v
= oper
->locals
->variables
+ i
;
2609 slang_ir_storage
*store
= (slang_ir_storage
*) v
->aux
;
2611 printf(" Deallocate var %s\n", (char*) v->a_name);
2613 assert(store
->File
== PROGRAM_TEMPORARY
);
2614 assert(store
->Index
>= 0);
2615 _slang_free_temp(A
->vartable
, store
->Index
, store
->Size
);
2623 return new_node0(IR_NOP
);
2626 case SLANG_OPER_EXPRESSION
:
2627 return _slang_gen_operation(A
, &oper
->children
[0]);
2629 case SLANG_OPER_FOR
:
2630 return _slang_gen_for(A
, oper
);
2632 return _slang_gen_do(A
, oper
);
2633 case SLANG_OPER_WHILE
:
2634 return _slang_gen_while(A
, oper
);
2635 case SLANG_OPER_BREAK
:
2637 slang_info_log_error(A
->log
, "'break' not in loop");
2640 return new_break(A
->CurLoop
);
2641 case SLANG_OPER_CONTINUE
:
2643 slang_info_log_error(A
->log
, "'continue' not in loop");
2646 return _slang_gen_continue(A
, oper
);
2647 case SLANG_OPER_DISCARD
:
2648 return new_node0(IR_KILL
);
2650 case SLANG_OPER_EQUAL
:
2651 return new_node2(IR_EQUAL
,
2652 _slang_gen_operation(A
, &oper
->children
[0]),
2653 _slang_gen_operation(A
, &oper
->children
[1]));
2654 case SLANG_OPER_NOTEQUAL
:
2655 return new_node2(IR_NOTEQUAL
,
2656 _slang_gen_operation(A
, &oper
->children
[0]),
2657 _slang_gen_operation(A
, &oper
->children
[1]));
2658 case SLANG_OPER_GREATER
:
2659 return new_node2(IR_SGT
,
2660 _slang_gen_operation(A
, &oper
->children
[0]),
2661 _slang_gen_operation(A
, &oper
->children
[1]));
2662 case SLANG_OPER_LESS
:
2663 return new_node2(IR_SLT
,
2664 _slang_gen_operation(A
, &oper
->children
[0]),
2665 _slang_gen_operation(A
, &oper
->children
[1]));
2666 case SLANG_OPER_GREATEREQUAL
:
2667 return new_node2(IR_SGE
,
2668 _slang_gen_operation(A
, &oper
->children
[0]),
2669 _slang_gen_operation(A
, &oper
->children
[1]));
2670 case SLANG_OPER_LESSEQUAL
:
2671 return new_node2(IR_SLE
,
2672 _slang_gen_operation(A
, &oper
->children
[0]),
2673 _slang_gen_operation(A
, &oper
->children
[1]));
2674 case SLANG_OPER_ADD
:
2677 assert(oper
->num_children
== 2);
2678 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
2681 case SLANG_OPER_SUBTRACT
:
2684 assert(oper
->num_children
== 2);
2685 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
2688 case SLANG_OPER_MULTIPLY
:
2691 assert(oper
->num_children
== 2);
2692 _slang_check_matmul_optimization(A
, oper
);
2693 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
2696 case SLANG_OPER_DIVIDE
:
2699 assert(oper
->num_children
== 2);
2700 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
2703 case SLANG_OPER_MINUS
:
2706 assert(oper
->num_children
== 1);
2707 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
2710 case SLANG_OPER_PLUS
:
2711 /* +expr --> do nothing */
2712 return _slang_gen_operation(A
, &oper
->children
[0]);
2713 case SLANG_OPER_VARIABLE_DECL
:
2714 return _slang_gen_declaration(A
, oper
);
2715 case SLANG_OPER_ASSIGN
:
2716 return _slang_gen_assignment(A
, oper
);
2717 case SLANG_OPER_ADDASSIGN
:
2720 assert(oper
->num_children
== 2);
2721 n
= _slang_gen_function_call_name(A
, "+=", oper
, &oper
->children
[0]);
2724 case SLANG_OPER_SUBASSIGN
:
2727 assert(oper
->num_children
== 2);
2728 n
= _slang_gen_function_call_name(A
, "-=", oper
, &oper
->children
[0]);
2732 case SLANG_OPER_MULASSIGN
:
2735 assert(oper
->num_children
== 2);
2736 n
= _slang_gen_function_call_name(A
, "*=", oper
, &oper
->children
[0]);
2739 case SLANG_OPER_DIVASSIGN
:
2742 assert(oper
->num_children
== 2);
2743 n
= _slang_gen_function_call_name(A
, "/=", oper
, &oper
->children
[0]);
2746 case SLANG_OPER_LOGICALAND
:
2749 assert(oper
->num_children
== 2);
2750 n
= _slang_gen_logical_and(A
, oper
);
2753 case SLANG_OPER_LOGICALOR
:
2756 assert(oper
->num_children
== 2);
2757 n
= _slang_gen_logical_or(A
, oper
);
2760 case SLANG_OPER_LOGICALXOR
:
2761 return _slang_gen_xor(A
, oper
);
2762 case SLANG_OPER_NOT
:
2763 return _slang_gen_not(A
, oper
);
2764 case SLANG_OPER_SELECT
: /* b ? x : y */
2767 assert(oper
->num_children
== 3);
2768 n
= _slang_gen_select(A
, oper
);
2772 case SLANG_OPER_ASM
:
2773 return _slang_gen_asm(A
, oper
, NULL
);
2774 case SLANG_OPER_CALL
:
2775 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
2777 case SLANG_OPER_RETURN
:
2778 return _slang_gen_return(A
, oper
);
2779 case SLANG_OPER_LABEL
:
2780 return new_label(oper
->label
);
2781 case SLANG_OPER_IDENTIFIER
:
2782 return _slang_gen_variable(A
, oper
);
2784 return _slang_gen_if(A
, oper
);
2785 case SLANG_OPER_FIELD
:
2786 return _slang_gen_field(A
, oper
);
2787 case SLANG_OPER_SUBSCRIPT
:
2788 return _slang_gen_subscript(A
, oper
);
2789 case SLANG_OPER_LITERAL_FLOAT
:
2791 case SLANG_OPER_LITERAL_INT
:
2793 case SLANG_OPER_LITERAL_BOOL
:
2794 return new_float_literal(oper
->literal
, oper
->literal_size
);
2796 case SLANG_OPER_POSTINCREMENT
: /* var++ */
2799 assert(oper
->num_children
== 1);
2800 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
2803 case SLANG_OPER_POSTDECREMENT
: /* var-- */
2806 assert(oper
->num_children
== 1);
2807 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
2810 case SLANG_OPER_PREINCREMENT
: /* ++var */
2813 assert(oper
->num_children
== 1);
2814 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
2817 case SLANG_OPER_PREDECREMENT
: /* --var */
2820 assert(oper
->num_children
== 1);
2821 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
2825 case SLANG_OPER_NON_INLINED_CALL
:
2826 case SLANG_OPER_SEQUENCE
:
2828 slang_ir_node
*tree
= NULL
;
2830 for (i
= 0; i
< oper
->num_children
; i
++) {
2831 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
2832 tree
= new_seq(tree
, n
);
2834 if (oper
->type
== SLANG_OPER_NON_INLINED_CALL
) {
2835 tree
= new_function_call(tree
, oper
->label
);
2840 case SLANG_OPER_NONE
:
2841 case SLANG_OPER_VOID
:
2842 /* returning NULL here would generate an error */
2843 return new_node0(IR_NOP
);
2846 _mesa_problem(NULL
, "bad node type %d in _slang_gen_operation",
2848 return new_node0(IR_NOP
);
2857 * Called by compiler when a global variable has been parsed/compiled.
2858 * Here we examine the variable's type to determine what kind of register
2859 * storage will be used.
2861 * A uniform such as "gl_Position" will become the register specification
2862 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
2863 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
2865 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
2866 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
2867 * actual texture unit (as specified by the user calling glUniform1i()).
2870 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
2871 slang_unit_type type
)
2873 struct gl_program
*prog
= A
->program
;
2874 const char *varName
= (char *) var
->a_name
;
2875 GLboolean success
= GL_TRUE
;
2876 slang_ir_storage
*store
= NULL
;
2878 const GLenum datatype
= _slang_gltype_from_specifier(&var
->type
.specifier
);
2879 const GLint texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
2881 if (texIndex
!= -1) {
2883 * store->File = PROGRAM_SAMPLER
2884 * store->Index = sampler uniform location
2885 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
2887 GLint samplerUniform
2888 = _mesa_add_sampler(prog
->Parameters
, varName
, datatype
);
2889 store
= _slang_new_ir_storage(PROGRAM_SAMPLER
, samplerUniform
, texIndex
);
2890 if (dbg
) printf("SAMPLER ");
2892 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2893 /* Uniform variable */
2894 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
)
2895 * MAX2(var
->array_len
, 1);
2897 /* user-defined uniform */
2898 if (datatype
== GL_NONE
) {
2899 if (var
->type
.specifier
.type
== SLANG_SPEC_STRUCT
) {
2900 _mesa_problem(NULL
, "user-declared uniform structs not supported yet");
2901 /* XXX what we need to do is unroll the struct into its
2902 * basic types, creating a uniform variable for each.
2910 * Should produce uniforms:
2911 * "f.a" (GL_FLOAT_VEC3)
2912 * "f.b" (GL_FLOAT_VEC4)
2916 slang_info_log_error(A
->log
,
2917 "invalid datatype for uniform variable %s",
2918 (char *) var
->a_name
);
2923 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
,
2925 store
= _slang_new_ir_storage(PROGRAM_UNIFORM
, uniformLoc
, size
);
2929 /* pre-defined uniform, like gl_ModelviewMatrix */
2930 /* We know it's a uniform, but don't allocate storage unless
2933 store
= _slang_new_ir_storage(PROGRAM_STATE_VAR
, -1, size
);
2935 if (dbg
) printf("UNIFORM (sz %d) ", size
);
2937 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
2938 const GLint size
= 4; /* XXX fix */
2940 /* user-defined varying */
2941 GLint varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
, size
);
2942 store
= _slang_new_ir_storage(PROGRAM_VARYING
, varyingLoc
, size
);
2945 /* pre-defined varying, like gl_Color or gl_TexCoord */
2946 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
2948 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
2951 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2952 store
->Swizzle
= swizzle
;
2953 assert(index
< FRAG_ATTRIB_MAX
);
2956 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
2958 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
2959 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2960 assert(index
< VERT_RESULT_MAX
);
2962 if (dbg
) printf("V/F ");
2964 if (dbg
) printf("VARYING ");
2966 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
2968 /* user-defined vertex attribute */
2969 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
2970 const GLint attr
= -1; /* unknown */
2971 GLint index
= _mesa_add_attribute(prog
->Attributes
, varName
,
2974 store
= _slang_new_ir_storage(PROGRAM_INPUT
,
2975 VERT_ATTRIB_GENERIC0
+ index
, size
);
2978 /* pre-defined vertex attrib */
2980 GLint index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
,
2982 GLint size
= 4; /* XXX? */
2984 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2985 store
->Swizzle
= swizzle
;
2987 if (dbg
) printf("ATTRIB ");
2989 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
2990 GLuint swizzle
= SWIZZLE_XYZW
; /* silence compiler warning */
2991 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
2993 GLint size
= 4; /* XXX? */
2994 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2995 store
->Swizzle
= swizzle
;
2996 if (dbg
) printf("INPUT ");
2998 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
2999 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
3000 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
3001 GLint size
= 4; /* XXX? */
3002 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
3005 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
3006 GLint size
= 4; /* XXX? */
3007 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
3008 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
3010 if (dbg
) printf("OUTPUT ");
3012 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
3013 /* pre-defined global constant, like gl_MaxLights */
3014 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
3015 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
3016 if (dbg
) printf("CONST ");
3019 /* ordinary variable (may be const) */
3022 /* IR node to declare the variable */
3023 n
= _slang_gen_var_decl(A
, var
);
3025 /* IR code for the var's initializer, if present */
3026 if (var
->initializer
) {
3027 slang_ir_node
*lhs
, *rhs
, *init
;
3029 /* Generate IR_MOVE instruction to initialize the variable */
3030 lhs
= new_node0(IR_VAR
);
3032 lhs
->Store
= n
->Store
;
3034 /* constant folding, etc */
3035 _slang_simplify(var
->initializer
, &A
->space
, A
->atoms
);
3037 rhs
= _slang_gen_operation(A
, var
->initializer
);
3039 init
= new_node2(IR_MOVE
, lhs
, rhs
);
3040 n
= new_seq(n
, init
);
3043 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_FALSE
, A
->log
);
3045 _slang_free_ir_tree(n
);
3048 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
3049 store
? store
->Index
: -2);
3052 var
->aux
= store
; /* save var's storage info */
3059 * Produce an IR tree from a function AST (fun->body).
3060 * Then call the code emitter to convert the IR tree into gl_program
3064 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
3067 GLboolean success
= GL_TRUE
;
3069 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
3070 /* we only really generate code for main, all other functions get
3074 /* do some basic error checking though */
3075 if (fun
->header
.type
.specifier
.type
!= SLANG_SPEC_VOID
) {
3076 /* check that non-void functions actually return something */
3078 = _slang_find_node_type(fun
->body
, SLANG_OPER_RETURN
);
3080 slang_info_log_error(A
->log
,
3081 "function \"%s\" has no return statement",
3082 (char *) fun
->header
.a_name
);
3084 "function \"%s\" has no return statement\n",
3085 (char *) fun
->header
.a_name
);
3090 return GL_TRUE
; /* not an error */
3094 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
3095 slang_print_function(fun
, 1);
3098 /* should have been allocated earlier: */
3099 assert(A
->program
->Parameters
);
3100 assert(A
->program
->Varying
);
3101 assert(A
->vartable
);
3103 A
->CurFunction
= fun
;
3105 /* fold constant expressions, etc. */
3106 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
3109 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
3110 slang_print_function(fun
, 1);
3113 /* Create an end-of-function label */
3114 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
3116 /* push new vartable scope */
3117 _slang_push_var_table(A
->vartable
);
3119 /* Generate IR tree for the function body code */
3120 n
= _slang_gen_operation(A
, fun
->body
);
3122 n
= new_node1(IR_SCOPE
, n
);
3124 /* pop vartable, restore previous */
3125 _slang_pop_var_table(A
->vartable
);
3128 /* XXX record error */
3132 /* append an end-of-function-label to IR tree */
3133 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
3135 /*_slang_label_delete(A->curFuncEndLabel);*/
3136 A
->curFuncEndLabel
= NULL
;
3139 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
3140 slang_print_function(fun
, 1);
3143 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
3144 _slang_print_ir_tree(n
, 0);
3147 printf("************* End codegen function ************\n\n");
3150 /* Emit program instructions */
3151 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
, A
->log
);
3152 _slang_free_ir_tree(n
);
3154 /* free codegen context */
3156 _mesa_free(A->codegen);