2 * Copyright © 2015 Red Hat
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 * \file lower_subroutine.cpp
27 * lowers subroutines to an if ladder.
30 #include "glsl_types.h"
31 #include "glsl_parser_extras.h"
33 #include "ir_builder.h"
35 using namespace ir_builder
;
38 class lower_subroutine_visitor
: public ir_hierarchical_visitor
{
40 lower_subroutine_visitor(struct _mesa_glsl_parse_state
*state
)
43 this->progress
= false;
46 ir_visitor_status
visit_leave(ir_call
*);
48 struct _mesa_glsl_parse_state
*state
;
54 lower_subroutine(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
56 lower_subroutine_visitor
v(state
);
57 visit_list_elements(&v
, instructions
);
62 lower_subroutine_visitor::visit_leave(ir_call
*ir
)
65 return visit_continue
;
67 void *mem_ctx
= ralloc_parent(ir
);
68 ir_if
*last_branch
= NULL
;
69 ir_dereference_variable
*return_deref
= ir
->return_deref
;
71 for (int s
= this->state
->num_subroutines
- 1; s
>= 0; s
--) {
73 ir_constant
*lc
= new(mem_ctx
)ir_constant(s
);
74 ir_function
*fn
= this->state
->subroutines
[s
];
75 bool is_compat
= false;
77 for (int i
= 0; i
< fn
->num_subroutine_types
; i
++) {
78 if (ir
->sub_var
->type
->without_array() == fn
->subroutine_types
[i
]) {
83 if (is_compat
== false)
86 if (ir
->array_idx
!= NULL
)
87 var
= new(mem_ctx
) ir_dereference_array(ir
->sub_var
, ir
->array_idx
->clone(mem_ctx
, NULL
));
89 var
= new(mem_ctx
) ir_dereference_variable(ir
->sub_var
);
91 ir_function_signature
*sub_sig
=
92 fn
->exact_matching_signature(this->state
,
93 &ir
->actual_parameters
);
95 ir_call
*new_call
= new(mem_ctx
) ir_call(sub_sig
, return_deref
, &ir
->actual_parameters
);
97 last_branch
= if_tree(equal(subr_to_int(var
), lc
), new_call
);
99 last_branch
= if_tree(equal(subr_to_int(var
), lc
), new_call
, last_branch
);
101 if (return_deref
&& s
> 0)
102 return_deref
= return_deref
->clone(mem_ctx
, NULL
);
105 ir
->insert_before(last_branch
);
108 return visit_continue
;