2 * TCG Backend Data: constant pool.
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 typedef struct TCGLabelPoolData {
24 struct TCGLabelPoolData *next;
29 tcg_target_ulong data[];
33 static TCGLabelPoolData *new_pool_alloc(TCGContext *s, int nlong, int rtype,
34 tcg_insn_unit *label, intptr_t addend)
36 TCGLabelPoolData *n = tcg_malloc(sizeof(TCGLabelPoolData)
37 + sizeof(tcg_target_ulong) * nlong);
46 static void new_pool_insert(TCGContext *s, TCGLabelPoolData *n)
48 TCGLabelPoolData *i, **pp;
51 /* Insertion sort on the pool. */
52 for (pp = &s->pool_labels; (i = *pp) != NULL; pp = &i->next) {
53 if (nlong > i->nlong) {
56 if (nlong < i->nlong) {
59 if (memcmp(n->data, i->data, sizeof(tcg_target_ulong) * nlong) >= 0) {
67 /* The "usual" for generic integer code. */
68 static inline void new_pool_label(TCGContext *s, tcg_target_ulong d, int rtype,
69 tcg_insn_unit *label, intptr_t addend)
71 TCGLabelPoolData *n = new_pool_alloc(s, 1, rtype, label, addend);
73 new_pool_insert(s, n);
76 /* For v64 or v128, depending on the host. */
77 static inline void new_pool_l2(TCGContext *s, int rtype, tcg_insn_unit *label,
78 intptr_t addend, tcg_target_ulong d0,
81 TCGLabelPoolData *n = new_pool_alloc(s, 2, rtype, label, addend);
84 new_pool_insert(s, n);
87 /* For v128 or v256, depending on the host. */
88 static inline void new_pool_l4(TCGContext *s, int rtype, tcg_insn_unit *label,
89 intptr_t addend, tcg_target_ulong d0,
90 tcg_target_ulong d1, tcg_target_ulong d2,
93 TCGLabelPoolData *n = new_pool_alloc(s, 4, rtype, label, addend);
98 new_pool_insert(s, n);
101 /* For v256, for 32-bit host. */
102 static inline void new_pool_l8(TCGContext *s, int rtype, tcg_insn_unit *label,
103 intptr_t addend, tcg_target_ulong d0,
104 tcg_target_ulong d1, tcg_target_ulong d2,
105 tcg_target_ulong d3, tcg_target_ulong d4,
106 tcg_target_ulong d5, tcg_target_ulong d6,
109 TCGLabelPoolData *n = new_pool_alloc(s, 8, rtype, label, addend);
118 new_pool_insert(s, n);
121 /* To be provided by cpu/tcg-target.c.inc. */
122 static void tcg_out_nop_fill(tcg_insn_unit *p, int count);
124 static int tcg_out_pool_finalize(TCGContext *s)
126 TCGLabelPoolData *p = s->pool_labels;
127 TCGLabelPoolData *l = NULL;
134 /* ??? Round up to qemu_icache_linesize, but then do not round
135 again when allocating the next TranslationBlock structure. */
136 a = (void *)ROUND_UP((uintptr_t)s->code_ptr,
137 sizeof(tcg_target_ulong) * p->nlong);
138 tcg_out_nop_fill(s->code_ptr, (tcg_insn_unit *)a - s->code_ptr);
141 for (; p != NULL; p = p->next) {
142 size_t size = sizeof(tcg_target_ulong) * p->nlong;
145 if (!l || l->nlong != p->nlong || memcmp(l->data, p->data, size)) {
146 if (unlikely(a > s->code_gen_highwater)) {
149 memcpy(a, p->data, size);
154 value = (uintptr_t)tcg_splitwx_to_rx(a) - size;
155 if (!patch_reloc(p->label, p->rtype, value, p->addend)) {