1 /*
2  * Copyright (C) 2020 Collabora, Ltd.
3  *
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:
10  *
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
13  * Software.
14  *
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 FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #ifndef __BI_PACK_HELPERS_H
25 #define __BI_PACK_HELPERS_H
26 
27 /* Helpers used by the autogenerated packing routines */
28 
29 #include "compiler.h"
30 
31 static inline void
bi_set_staging_register(bi_clause * clause,unsigned idx)32 bi_set_staging_register(bi_clause *clause, unsigned idx)
33 {
34         assert(idx & BIR_INDEX_REGISTER);
35         unsigned reg = idx & ~BIR_INDEX_REGISTER;
36         assert(reg <= 63);
37         clause->staging_register = reg;
38 }
39 
40 static inline void
bi_read_staging_register(bi_clause * clause,bi_instruction * ins)41 bi_read_staging_register(bi_clause *clause, bi_instruction *ins)
42 {
43         bi_set_staging_register(clause, ins->src[0]);
44 }
45 
46 static inline void
bi_write_staging_register(bi_clause * clause,bi_instruction * ins)47 bi_write_staging_register(bi_clause *clause, bi_instruction *ins)
48 {
49         bi_set_staging_register(clause, ins->dest);
50 }
51 
52 static inline enum bifrost_packed_src
bi_get_src_reg_slot(bi_registers * regs,unsigned src)53 bi_get_src_reg_slot(bi_registers *regs, unsigned src)
54 {
55         unsigned reg = src & ~BIR_INDEX_REGISTER;
56 
57         if (regs->slot[0] == reg && regs->enabled[0])
58                 return BIFROST_SRC_PORT0;
59         else if (regs->slot[1] == reg && regs->enabled[1])
60                 return BIFROST_SRC_PORT1;
61         else if (regs->slot[2] == reg && regs->slot23.slot2 == BIFROST_OP_READ)
62                 return BIFROST_SRC_PORT2;
63         else
64                 unreachable("Tried to access register with no port");
65 }
66 
67 /* Sources are usually strictly required, but in a few special cases they can
68  * be made optional with the value passed arbitrary. Check that here */
69 
70 static bool
bi_src_nullable(bi_instruction * ins,unsigned s)71 bi_src_nullable(bi_instruction *ins, unsigned s)
72 {
73         /* Z/S flags inferred */
74         if (ins->type == BI_ZS_EMIT && s < 2)
75                 return true;
76 
77         return false;
78 }
79 
80 static inline enum bifrost_packed_src
bi_get_src(bi_instruction * ins,bi_registers * regs,unsigned s)81 bi_get_src(bi_instruction *ins, bi_registers *regs, unsigned s)
82 {
83         unsigned src = ins->src[s];
84 
85         if (src & BIR_INDEX_REGISTER)
86                 return bi_get_src_reg_slot(regs, src);
87         else if (src & BIR_INDEX_PASS)
88                 return src & ~BIR_INDEX_PASS;
89         else if (!src && bi_src_nullable(ins, s))
90                 return BIFROST_SRC_STAGE;
91         else {
92 #ifndef NDEBUG
93                 bi_print_instruction(ins, stderr);
94 #endif
95                 unreachable("Unknown src in above instruction");
96         }
97 }
98 
99 #endif
100