]> git.proxmox.com Git - mirror_qemu.git/blob - target/hexagon/genptr.c
Hexagon (target/hexagon) Analyze packet before generating TCG
[mirror_qemu.git] / target / hexagon / genptr.c
1 /*
2 * Copyright(c) 2019-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "qemu/osdep.h"
19 #include "cpu.h"
20 #include "internal.h"
21 #include "tcg/tcg-op.h"
22 #include "tcg/tcg-op-gvec.h"
23 #include "insn.h"
24 #include "opcodes.h"
25 #include "translate.h"
26 #define QEMU_GENERATE /* Used internally by macros.h */
27 #include "macros.h"
28 #include "mmvec/macros.h"
29 #undef QEMU_GENERATE
30 #include "gen_tcg.h"
31 #include "gen_tcg_hvx.h"
32 #include "genptr.h"
33
34 TCGv gen_read_reg(TCGv result, int num)
35 {
36 tcg_gen_mov_tl(result, hex_gpr[num]);
37 return result;
38 }
39
40 TCGv gen_read_preg(TCGv pred, uint8_t num)
41 {
42 tcg_gen_mov_tl(pred, hex_pred[num]);
43 return pred;
44 }
45
46 #define IMMUTABLE (~0)
47
48 static const target_ulong reg_immut_masks[TOTAL_PER_THREAD_REGS] = {
49 [HEX_REG_USR] = 0xc13000c0,
50 [HEX_REG_PC] = IMMUTABLE,
51 [HEX_REG_GP] = 0x3f,
52 [HEX_REG_UPCYCLELO] = IMMUTABLE,
53 [HEX_REG_UPCYCLEHI] = IMMUTABLE,
54 [HEX_REG_UTIMERLO] = IMMUTABLE,
55 [HEX_REG_UTIMERHI] = IMMUTABLE,
56 };
57
58 static inline void gen_masked_reg_write(TCGv new_val, TCGv cur_val,
59 target_ulong reg_mask)
60 {
61 if (reg_mask) {
62 TCGv tmp = tcg_temp_new();
63
64 /* new_val = (new_val & ~reg_mask) | (cur_val & reg_mask) */
65 tcg_gen_andi_tl(new_val, new_val, ~reg_mask);
66 tcg_gen_andi_tl(tmp, cur_val, reg_mask);
67 tcg_gen_or_tl(new_val, new_val, tmp);
68 }
69 }
70
71 static inline void gen_log_predicated_reg_write(int rnum, TCGv val,
72 uint32_t slot)
73 {
74 TCGv zero = tcg_constant_tl(0);
75 TCGv slot_mask = tcg_temp_new();
76
77 tcg_gen_andi_tl(slot_mask, hex_slot_cancelled, 1 << slot);
78 tcg_gen_movcond_tl(TCG_COND_EQ, hex_new_value[rnum], slot_mask, zero,
79 val, hex_new_value[rnum]);
80 if (HEX_DEBUG) {
81 /*
82 * Do this so HELPER(debug_commit_end) will know
83 *
84 * Note that slot_mask indicates the value is not written
85 * (i.e., slot was cancelled), so we create a true/false value before
86 * or'ing with hex_reg_written[rnum].
87 */
88 tcg_gen_setcond_tl(TCG_COND_EQ, slot_mask, slot_mask, zero);
89 tcg_gen_or_tl(hex_reg_written[rnum], hex_reg_written[rnum], slot_mask);
90 }
91 }
92
93 void gen_log_reg_write(int rnum, TCGv val)
94 {
95 const target_ulong reg_mask = reg_immut_masks[rnum];
96
97 gen_masked_reg_write(val, hex_gpr[rnum], reg_mask);
98 tcg_gen_mov_tl(hex_new_value[rnum], val);
99 if (HEX_DEBUG) {
100 /* Do this so HELPER(debug_commit_end) will know */
101 tcg_gen_movi_tl(hex_reg_written[rnum], 1);
102 }
103 }
104
105 static void gen_log_predicated_reg_write_pair(int rnum, TCGv_i64 val,
106 uint32_t slot)
107 {
108 TCGv val32 = tcg_temp_new();
109 TCGv zero = tcg_constant_tl(0);
110 TCGv slot_mask = tcg_temp_new();
111
112 tcg_gen_andi_tl(slot_mask, hex_slot_cancelled, 1 << slot);
113 /* Low word */
114 tcg_gen_extrl_i64_i32(val32, val);
115 tcg_gen_movcond_tl(TCG_COND_EQ, hex_new_value[rnum],
116 slot_mask, zero,
117 val32, hex_new_value[rnum]);
118 /* High word */
119 tcg_gen_extrh_i64_i32(val32, val);
120 tcg_gen_movcond_tl(TCG_COND_EQ, hex_new_value[rnum + 1],
121 slot_mask, zero,
122 val32, hex_new_value[rnum + 1]);
123 if (HEX_DEBUG) {
124 /*
125 * Do this so HELPER(debug_commit_end) will know
126 *
127 * Note that slot_mask indicates the value is not written
128 * (i.e., slot was cancelled), so we create a true/false value before
129 * or'ing with hex_reg_written[rnum].
130 */
131 tcg_gen_setcond_tl(TCG_COND_EQ, slot_mask, slot_mask, zero);
132 tcg_gen_or_tl(hex_reg_written[rnum], hex_reg_written[rnum], slot_mask);
133 tcg_gen_or_tl(hex_reg_written[rnum + 1], hex_reg_written[rnum + 1],
134 slot_mask);
135 }
136 }
137
138 static void gen_log_reg_write_pair(int rnum, TCGv_i64 val)
139 {
140 const target_ulong reg_mask_low = reg_immut_masks[rnum];
141 const target_ulong reg_mask_high = reg_immut_masks[rnum + 1];
142 TCGv val32 = tcg_temp_new();
143
144 /* Low word */
145 tcg_gen_extrl_i64_i32(val32, val);
146 gen_masked_reg_write(val32, hex_gpr[rnum], reg_mask_low);
147 tcg_gen_mov_tl(hex_new_value[rnum], val32);
148 if (HEX_DEBUG) {
149 /* Do this so HELPER(debug_commit_end) will know */
150 tcg_gen_movi_tl(hex_reg_written[rnum], 1);
151 }
152
153 /* High word */
154 tcg_gen_extrh_i64_i32(val32, val);
155 gen_masked_reg_write(val32, hex_gpr[rnum + 1], reg_mask_high);
156 tcg_gen_mov_tl(hex_new_value[rnum + 1], val32);
157 if (HEX_DEBUG) {
158 /* Do this so HELPER(debug_commit_end) will know */
159 tcg_gen_movi_tl(hex_reg_written[rnum + 1], 1);
160 }
161 }
162
163 void gen_log_pred_write(DisasContext *ctx, int pnum, TCGv val)
164 {
165 TCGv base_val = tcg_temp_new();
166
167 tcg_gen_andi_tl(base_val, val, 0xff);
168
169 /*
170 * Section 6.1.3 of the Hexagon V67 Programmer's Reference Manual
171 *
172 * Multiple writes to the same preg are and'ed together
173 * If this is the first predicate write in the packet, do a
174 * straight assignment. Otherwise, do an and.
175 */
176 if (!test_bit(pnum, ctx->pregs_written)) {
177 tcg_gen_mov_tl(hex_new_pred_value[pnum], base_val);
178 } else {
179 tcg_gen_and_tl(hex_new_pred_value[pnum],
180 hex_new_pred_value[pnum], base_val);
181 }
182 tcg_gen_ori_tl(hex_pred_written, hex_pred_written, 1 << pnum);
183 set_bit(pnum, ctx->pregs_written);
184 }
185
186 static inline void gen_read_p3_0(TCGv control_reg)
187 {
188 tcg_gen_movi_tl(control_reg, 0);
189 for (int i = 0; i < NUM_PREGS; i++) {
190 tcg_gen_deposit_tl(control_reg, control_reg, hex_pred[i], i * 8, 8);
191 }
192 }
193
194 /*
195 * Certain control registers require special handling on read
196 * HEX_REG_P3_0_ALIASED aliased to the predicate registers
197 * -> concat the 4 predicate registers together
198 * HEX_REG_PC actual value stored in DisasContext
199 * -> assign from ctx->base.pc_next
200 * HEX_REG_QEMU_*_CNT changes in current TB in DisasContext
201 * -> add current TB changes to existing reg value
202 */
203 static inline void gen_read_ctrl_reg(DisasContext *ctx, const int reg_num,
204 TCGv dest)
205 {
206 if (reg_num == HEX_REG_P3_0_ALIASED) {
207 gen_read_p3_0(dest);
208 } else if (reg_num == HEX_REG_PC) {
209 tcg_gen_movi_tl(dest, ctx->base.pc_next);
210 } else if (reg_num == HEX_REG_QEMU_PKT_CNT) {
211 tcg_gen_addi_tl(dest, hex_gpr[HEX_REG_QEMU_PKT_CNT],
212 ctx->num_packets);
213 } else if (reg_num == HEX_REG_QEMU_INSN_CNT) {
214 tcg_gen_addi_tl(dest, hex_gpr[HEX_REG_QEMU_INSN_CNT],
215 ctx->num_insns);
216 } else if (reg_num == HEX_REG_QEMU_HVX_CNT) {
217 tcg_gen_addi_tl(dest, hex_gpr[HEX_REG_QEMU_HVX_CNT],
218 ctx->num_hvx_insns);
219 } else {
220 tcg_gen_mov_tl(dest, hex_gpr[reg_num]);
221 }
222 }
223
224 static inline void gen_read_ctrl_reg_pair(DisasContext *ctx, const int reg_num,
225 TCGv_i64 dest)
226 {
227 if (reg_num == HEX_REG_P3_0_ALIASED) {
228 TCGv p3_0 = tcg_temp_new();
229 gen_read_p3_0(p3_0);
230 tcg_gen_concat_i32_i64(dest, p3_0, hex_gpr[reg_num + 1]);
231 } else if (reg_num == HEX_REG_PC - 1) {
232 TCGv pc = tcg_constant_tl(ctx->base.pc_next);
233 tcg_gen_concat_i32_i64(dest, hex_gpr[reg_num], pc);
234 } else if (reg_num == HEX_REG_QEMU_PKT_CNT) {
235 TCGv pkt_cnt = tcg_temp_new();
236 TCGv insn_cnt = tcg_temp_new();
237 tcg_gen_addi_tl(pkt_cnt, hex_gpr[HEX_REG_QEMU_PKT_CNT],
238 ctx->num_packets);
239 tcg_gen_addi_tl(insn_cnt, hex_gpr[HEX_REG_QEMU_INSN_CNT],
240 ctx->num_insns);
241 tcg_gen_concat_i32_i64(dest, pkt_cnt, insn_cnt);
242 } else if (reg_num == HEX_REG_QEMU_HVX_CNT) {
243 TCGv hvx_cnt = tcg_temp_new();
244 tcg_gen_addi_tl(hvx_cnt, hex_gpr[HEX_REG_QEMU_HVX_CNT],
245 ctx->num_hvx_insns);
246 tcg_gen_concat_i32_i64(dest, hvx_cnt, hex_gpr[reg_num + 1]);
247 } else {
248 tcg_gen_concat_i32_i64(dest,
249 hex_gpr[reg_num],
250 hex_gpr[reg_num + 1]);
251 }
252 }
253
254 static void gen_write_p3_0(DisasContext *ctx, TCGv control_reg)
255 {
256 TCGv hex_p8 = tcg_temp_new();
257 for (int i = 0; i < NUM_PREGS; i++) {
258 tcg_gen_extract_tl(hex_p8, control_reg, i * 8, 8);
259 gen_log_pred_write(ctx, i, hex_p8);
260 }
261 }
262
263 /*
264 * Certain control registers require special handling on write
265 * HEX_REG_P3_0_ALIASED aliased to the predicate registers
266 * -> break the value across 4 predicate registers
267 * HEX_REG_QEMU_*_CNT changes in current TB in DisasContext
268 * -> clear the changes
269 */
270 static inline void gen_write_ctrl_reg(DisasContext *ctx, int reg_num,
271 TCGv val)
272 {
273 if (reg_num == HEX_REG_P3_0_ALIASED) {
274 gen_write_p3_0(ctx, val);
275 } else {
276 gen_log_reg_write(reg_num, val);
277 if (reg_num == HEX_REG_QEMU_PKT_CNT) {
278 ctx->num_packets = 0;
279 }
280 if (reg_num == HEX_REG_QEMU_INSN_CNT) {
281 ctx->num_insns = 0;
282 }
283 if (reg_num == HEX_REG_QEMU_HVX_CNT) {
284 ctx->num_hvx_insns = 0;
285 }
286 }
287 }
288
289 static inline void gen_write_ctrl_reg_pair(DisasContext *ctx, int reg_num,
290 TCGv_i64 val)
291 {
292 if (reg_num == HEX_REG_P3_0_ALIASED) {
293 TCGv val32 = tcg_temp_new();
294 tcg_gen_extrl_i64_i32(val32, val);
295 gen_write_p3_0(ctx, val32);
296 tcg_gen_extrh_i64_i32(val32, val);
297 gen_log_reg_write(reg_num + 1, val32);
298 } else {
299 gen_log_reg_write_pair(reg_num, val);
300 if (reg_num == HEX_REG_QEMU_PKT_CNT) {
301 ctx->num_packets = 0;
302 ctx->num_insns = 0;
303 }
304 if (reg_num == HEX_REG_QEMU_HVX_CNT) {
305 ctx->num_hvx_insns = 0;
306 }
307 }
308 }
309
310 TCGv gen_get_byte(TCGv result, int N, TCGv src, bool sign)
311 {
312 if (sign) {
313 tcg_gen_sextract_tl(result, src, N * 8, 8);
314 } else {
315 tcg_gen_extract_tl(result, src, N * 8, 8);
316 }
317 return result;
318 }
319
320 TCGv gen_get_byte_i64(TCGv result, int N, TCGv_i64 src, bool sign)
321 {
322 TCGv_i64 res64 = tcg_temp_new_i64();
323 if (sign) {
324 tcg_gen_sextract_i64(res64, src, N * 8, 8);
325 } else {
326 tcg_gen_extract_i64(res64, src, N * 8, 8);
327 }
328 tcg_gen_extrl_i64_i32(result, res64);
329
330 return result;
331 }
332
333 TCGv gen_get_half(TCGv result, int N, TCGv src, bool sign)
334 {
335 if (sign) {
336 tcg_gen_sextract_tl(result, src, N * 16, 16);
337 } else {
338 tcg_gen_extract_tl(result, src, N * 16, 16);
339 }
340 return result;
341 }
342
343 void gen_set_half(int N, TCGv result, TCGv src)
344 {
345 tcg_gen_deposit_tl(result, result, src, N * 16, 16);
346 }
347
348 void gen_set_half_i64(int N, TCGv_i64 result, TCGv src)
349 {
350 TCGv_i64 src64 = tcg_temp_new_i64();
351 tcg_gen_extu_i32_i64(src64, src);
352 tcg_gen_deposit_i64(result, result, src64, N * 16, 16);
353 }
354
355 void gen_set_byte_i64(int N, TCGv_i64 result, TCGv src)
356 {
357 TCGv_i64 src64 = tcg_temp_new_i64();
358 tcg_gen_extu_i32_i64(src64, src);
359 tcg_gen_deposit_i64(result, result, src64, N * 8, 8);
360 }
361
362 static inline void gen_load_locked4u(TCGv dest, TCGv vaddr, int mem_index)
363 {
364 tcg_gen_qemu_ld32u(dest, vaddr, mem_index);
365 tcg_gen_mov_tl(hex_llsc_addr, vaddr);
366 tcg_gen_mov_tl(hex_llsc_val, dest);
367 }
368
369 static inline void gen_load_locked8u(TCGv_i64 dest, TCGv vaddr, int mem_index)
370 {
371 tcg_gen_qemu_ld64(dest, vaddr, mem_index);
372 tcg_gen_mov_tl(hex_llsc_addr, vaddr);
373 tcg_gen_mov_i64(hex_llsc_val_i64, dest);
374 }
375
376 static inline void gen_store_conditional4(DisasContext *ctx,
377 TCGv pred, TCGv vaddr, TCGv src)
378 {
379 TCGLabel *fail = gen_new_label();
380 TCGLabel *done = gen_new_label();
381 TCGv one, zero, tmp;
382
383 tcg_gen_brcond_tl(TCG_COND_NE, vaddr, hex_llsc_addr, fail);
384
385 one = tcg_constant_tl(0xff);
386 zero = tcg_constant_tl(0);
387 tmp = tcg_temp_new();
388 tcg_gen_atomic_cmpxchg_tl(tmp, hex_llsc_addr, hex_llsc_val, src,
389 ctx->mem_idx, MO_32);
390 tcg_gen_movcond_tl(TCG_COND_EQ, pred, tmp, hex_llsc_val,
391 one, zero);
392 tcg_gen_br(done);
393
394 gen_set_label(fail);
395 tcg_gen_movi_tl(pred, 0);
396
397 gen_set_label(done);
398 tcg_gen_movi_tl(hex_llsc_addr, ~0);
399 }
400
401 static inline void gen_store_conditional8(DisasContext *ctx,
402 TCGv pred, TCGv vaddr, TCGv_i64 src)
403 {
404 TCGLabel *fail = gen_new_label();
405 TCGLabel *done = gen_new_label();
406 TCGv_i64 one, zero, tmp;
407
408 tcg_gen_brcond_tl(TCG_COND_NE, vaddr, hex_llsc_addr, fail);
409
410 one = tcg_constant_i64(0xff);
411 zero = tcg_constant_i64(0);
412 tmp = tcg_temp_new_i64();
413 tcg_gen_atomic_cmpxchg_i64(tmp, hex_llsc_addr, hex_llsc_val_i64, src,
414 ctx->mem_idx, MO_64);
415 tcg_gen_movcond_i64(TCG_COND_EQ, tmp, tmp, hex_llsc_val_i64,
416 one, zero);
417 tcg_gen_extrl_i64_i32(pred, tmp);
418 tcg_gen_br(done);
419
420 gen_set_label(fail);
421 tcg_gen_movi_tl(pred, 0);
422
423 gen_set_label(done);
424 tcg_gen_movi_tl(hex_llsc_addr, ~0);
425 }
426
427 void gen_store32(TCGv vaddr, TCGv src, int width, uint32_t slot)
428 {
429 tcg_gen_mov_tl(hex_store_addr[slot], vaddr);
430 tcg_gen_movi_tl(hex_store_width[slot], width);
431 tcg_gen_mov_tl(hex_store_val32[slot], src);
432 }
433
434 void gen_store1(TCGv_env cpu_env, TCGv vaddr, TCGv src, uint32_t slot)
435 {
436 gen_store32(vaddr, src, 1, slot);
437 }
438
439 void gen_store1i(TCGv_env cpu_env, TCGv vaddr, int32_t src, uint32_t slot)
440 {
441 TCGv tmp = tcg_constant_tl(src);
442 gen_store1(cpu_env, vaddr, tmp, slot);
443 }
444
445 void gen_store2(TCGv_env cpu_env, TCGv vaddr, TCGv src, uint32_t slot)
446 {
447 gen_store32(vaddr, src, 2, slot);
448 }
449
450 void gen_store2i(TCGv_env cpu_env, TCGv vaddr, int32_t src, uint32_t slot)
451 {
452 TCGv tmp = tcg_constant_tl(src);
453 gen_store2(cpu_env, vaddr, tmp, slot);
454 }
455
456 void gen_store4(TCGv_env cpu_env, TCGv vaddr, TCGv src, uint32_t slot)
457 {
458 gen_store32(vaddr, src, 4, slot);
459 }
460
461 void gen_store4i(TCGv_env cpu_env, TCGv vaddr, int32_t src, uint32_t slot)
462 {
463 TCGv tmp = tcg_constant_tl(src);
464 gen_store4(cpu_env, vaddr, tmp, slot);
465 }
466
467 void gen_store8(TCGv_env cpu_env, TCGv vaddr, TCGv_i64 src, uint32_t slot)
468 {
469 tcg_gen_mov_tl(hex_store_addr[slot], vaddr);
470 tcg_gen_movi_tl(hex_store_width[slot], 8);
471 tcg_gen_mov_i64(hex_store_val64[slot], src);
472 }
473
474 void gen_store8i(TCGv_env cpu_env, TCGv vaddr, int64_t src, uint32_t slot)
475 {
476 TCGv_i64 tmp = tcg_constant_i64(src);
477 gen_store8(cpu_env, vaddr, tmp, slot);
478 }
479
480 TCGv gen_8bitsof(TCGv result, TCGv value)
481 {
482 TCGv zero = tcg_constant_tl(0);
483 TCGv ones = tcg_constant_tl(0xff);
484 tcg_gen_movcond_tl(TCG_COND_NE, result, value, zero, ones, zero);
485
486 return result;
487 }
488
489 static void gen_write_new_pc_addr(DisasContext *ctx, TCGv addr,
490 TCGCond cond, TCGv pred)
491 {
492 TCGLabel *pred_false = NULL;
493 if (cond != TCG_COND_ALWAYS) {
494 pred_false = gen_new_label();
495 tcg_gen_brcondi_tl(cond, pred, 0, pred_false);
496 }
497
498 if (ctx->pkt->pkt_has_multi_cof) {
499 /* If there are multiple branches in a packet, ignore the second one */
500 tcg_gen_movcond_tl(TCG_COND_NE, hex_gpr[HEX_REG_PC],
501 hex_branch_taken, tcg_constant_tl(0),
502 hex_gpr[HEX_REG_PC], addr);
503 tcg_gen_movi_tl(hex_branch_taken, 1);
504 } else {
505 tcg_gen_mov_tl(hex_gpr[HEX_REG_PC], addr);
506 }
507
508 if (cond != TCG_COND_ALWAYS) {
509 gen_set_label(pred_false);
510 }
511 }
512
513 static void gen_write_new_pc_pcrel(DisasContext *ctx, int pc_off,
514 TCGCond cond, TCGv pred)
515 {
516 target_ulong dest = ctx->pkt->pc + pc_off;
517 if (ctx->pkt->pkt_has_multi_cof) {
518 gen_write_new_pc_addr(ctx, tcg_constant_tl(dest), cond, pred);
519 } else {
520 /* Defer this jump to the end of the TB */
521 ctx->branch_cond = TCG_COND_ALWAYS;
522 if (pred != NULL) {
523 ctx->branch_cond = cond;
524 tcg_gen_mov_tl(hex_branch_taken, pred);
525 }
526 ctx->branch_dest = dest;
527 }
528 }
529
530 void gen_set_usr_field(int field, TCGv val)
531 {
532 tcg_gen_deposit_tl(hex_new_value[HEX_REG_USR], hex_new_value[HEX_REG_USR],
533 val,
534 reg_field_info[field].offset,
535 reg_field_info[field].width);
536 }
537
538 void gen_set_usr_fieldi(int field, int x)
539 {
540 if (reg_field_info[field].width == 1) {
541 target_ulong bit = 1 << reg_field_info[field].offset;
542 if ((x & 1) == 1) {
543 tcg_gen_ori_tl(hex_new_value[HEX_REG_USR],
544 hex_new_value[HEX_REG_USR],
545 bit);
546 } else {
547 tcg_gen_andi_tl(hex_new_value[HEX_REG_USR],
548 hex_new_value[HEX_REG_USR],
549 ~bit);
550 }
551 } else {
552 TCGv val = tcg_constant_tl(x);
553 gen_set_usr_field(field, val);
554 }
555 }
556
557 static void gen_compare(TCGCond cond, TCGv res, TCGv arg1, TCGv arg2)
558 {
559 TCGv one = tcg_constant_tl(0xff);
560 TCGv zero = tcg_constant_tl(0);
561
562 tcg_gen_movcond_tl(cond, res, arg1, arg2, one, zero);
563 }
564
565 static void gen_cond_jumpr(DisasContext *ctx, TCGv dst_pc,
566 TCGCond cond, TCGv pred)
567 {
568 gen_write_new_pc_addr(ctx, dst_pc, cond, pred);
569 }
570
571 static void gen_cond_jumpr31(DisasContext *ctx, TCGCond cond, TCGv pred)
572 {
573 TCGv LSB = tcg_temp_new();
574 tcg_gen_andi_tl(LSB, pred, 1);
575 gen_cond_jumpr(ctx, hex_gpr[HEX_REG_LR], cond, LSB);
576 }
577
578 static void gen_cond_jump(DisasContext *ctx, TCGCond cond, TCGv pred,
579 int pc_off)
580 {
581 gen_write_new_pc_pcrel(ctx, pc_off, cond, pred);
582 }
583
584 static void gen_cmpnd_cmp_jmp(DisasContext *ctx,
585 int pnum, TCGCond cond1, TCGv arg1, TCGv arg2,
586 TCGCond cond2, int pc_off)
587 {
588 if (ctx->insn->part1) {
589 TCGv pred = tcg_temp_new();
590 gen_compare(cond1, pred, arg1, arg2);
591 gen_log_pred_write(ctx, pnum, pred);
592 } else {
593 TCGv pred = tcg_temp_new();
594 tcg_gen_mov_tl(pred, hex_new_pred_value[pnum]);
595 gen_cond_jump(ctx, cond2, pred, pc_off);
596 }
597 }
598
599 static void gen_cmpnd_cmp_jmp_t(DisasContext *ctx,
600 int pnum, TCGCond cond, TCGv arg1, TCGv arg2,
601 int pc_off)
602 {
603 gen_cmpnd_cmp_jmp(ctx, pnum, cond, arg1, arg2, TCG_COND_EQ, pc_off);
604 }
605
606 static void gen_cmpnd_cmp_jmp_f(DisasContext *ctx,
607 int pnum, TCGCond cond, TCGv arg1, TCGv arg2,
608 int pc_off)
609 {
610 gen_cmpnd_cmp_jmp(ctx, pnum, cond, arg1, arg2, TCG_COND_NE, pc_off);
611 }
612
613 static void gen_cmpnd_cmpi_jmp_t(DisasContext *ctx,
614 int pnum, TCGCond cond, TCGv arg1, int arg2,
615 int pc_off)
616 {
617 TCGv tmp = tcg_constant_tl(arg2);
618 gen_cmpnd_cmp_jmp(ctx, pnum, cond, arg1, tmp, TCG_COND_EQ, pc_off);
619 }
620
621 static void gen_cmpnd_cmpi_jmp_f(DisasContext *ctx,
622 int pnum, TCGCond cond, TCGv arg1, int arg2,
623 int pc_off)
624 {
625 TCGv tmp = tcg_constant_tl(arg2);
626 gen_cmpnd_cmp_jmp(ctx, pnum, cond, arg1, tmp, TCG_COND_NE, pc_off);
627 }
628
629 static void gen_cmpnd_cmp_n1_jmp_t(DisasContext *ctx, int pnum, TCGCond cond,
630 TCGv arg, int pc_off)
631 {
632 gen_cmpnd_cmpi_jmp_t(ctx, pnum, cond, arg, -1, pc_off);
633 }
634
635 static void gen_cmpnd_cmp_n1_jmp_f(DisasContext *ctx, int pnum, TCGCond cond,
636 TCGv arg, int pc_off)
637 {
638 gen_cmpnd_cmpi_jmp_f(ctx, pnum, cond, arg, -1, pc_off);
639 }
640
641 static void gen_cmpnd_tstbit0_jmp(DisasContext *ctx,
642 int pnum, TCGv arg, TCGCond cond, int pc_off)
643 {
644 if (ctx->insn->part1) {
645 TCGv pred = tcg_temp_new();
646 tcg_gen_andi_tl(pred, arg, 1);
647 gen_8bitsof(pred, pred);
648 gen_log_pred_write(ctx, pnum, pred);
649 } else {
650 TCGv pred = tcg_temp_new();
651 tcg_gen_mov_tl(pred, hex_new_pred_value[pnum]);
652 gen_cond_jump(ctx, cond, pred, pc_off);
653 }
654 }
655
656 static void gen_testbit0_jumpnv(DisasContext *ctx,
657 TCGv arg, TCGCond cond, int pc_off)
658 {
659 TCGv pred = tcg_temp_new();
660 tcg_gen_andi_tl(pred, arg, 1);
661 gen_cond_jump(ctx, cond, pred, pc_off);
662 }
663
664 static void gen_jump(DisasContext *ctx, int pc_off)
665 {
666 gen_write_new_pc_pcrel(ctx, pc_off, TCG_COND_ALWAYS, NULL);
667 }
668
669 static void gen_jumpr(DisasContext *ctx, TCGv new_pc)
670 {
671 gen_write_new_pc_addr(ctx, new_pc, TCG_COND_ALWAYS, NULL);
672 }
673
674 static void gen_call(DisasContext *ctx, int pc_off)
675 {
676 TCGv next_PC =
677 tcg_constant_tl(ctx->pkt->pc + ctx->pkt->encod_pkt_size_in_bytes);
678 gen_log_reg_write(HEX_REG_LR, next_PC);
679 gen_write_new_pc_pcrel(ctx, pc_off, TCG_COND_ALWAYS, NULL);
680 }
681
682 static void gen_callr(DisasContext *ctx, TCGv new_pc)
683 {
684 TCGv next_PC = tcg_constant_tl(ctx->next_PC);
685 gen_log_reg_write(HEX_REG_LR, next_PC);
686 gen_write_new_pc_addr(ctx, new_pc, TCG_COND_ALWAYS, NULL);
687 }
688
689 static void gen_cond_call(DisasContext *ctx, TCGv pred,
690 TCGCond cond, int pc_off)
691 {
692 TCGv next_PC;
693 TCGv lsb = tcg_temp_new();
694 TCGLabel *skip = gen_new_label();
695 tcg_gen_andi_tl(lsb, pred, 1);
696 gen_write_new_pc_pcrel(ctx, pc_off, cond, lsb);
697 tcg_gen_brcondi_tl(cond, lsb, 0, skip);
698 next_PC =
699 tcg_constant_tl(ctx->pkt->pc + ctx->pkt->encod_pkt_size_in_bytes);
700 gen_log_reg_write(HEX_REG_LR, next_PC);
701 gen_set_label(skip);
702 }
703
704 static void gen_cond_callr(DisasContext *ctx,
705 TCGCond cond, TCGv pred, TCGv new_pc)
706 {
707 TCGv lsb = tcg_temp_new();
708 TCGLabel *skip = gen_new_label();
709 tcg_gen_andi_tl(lsb, pred, 1);
710 tcg_gen_brcondi_tl(cond, lsb, 0, skip);
711 gen_callr(ctx, new_pc);
712 gen_set_label(skip);
713 }
714
715 /* frame ^= (int64_t)FRAMEKEY << 32 */
716 static void gen_frame_unscramble(TCGv_i64 frame)
717 {
718 TCGv_i64 framekey = tcg_temp_new_i64();
719 tcg_gen_extu_i32_i64(framekey, hex_gpr[HEX_REG_FRAMEKEY]);
720 tcg_gen_shli_i64(framekey, framekey, 32);
721 tcg_gen_xor_i64(frame, frame, framekey);
722 }
723
724 static void gen_load_frame(DisasContext *ctx, TCGv_i64 frame, TCGv EA)
725 {
726 Insn *insn = ctx->insn; /* Needed for CHECK_NOSHUF */
727 CHECK_NOSHUF(EA, 8);
728 tcg_gen_qemu_ld64(frame, EA, ctx->mem_idx);
729 }
730
731 static void gen_return_base(DisasContext *ctx, TCGv_i64 dst, TCGv src,
732 TCGv r29)
733 {
734 /*
735 * frame = *src
736 * dst = frame_unscramble(frame)
737 * SP = src + 8
738 * PC = dst.w[1]
739 */
740 TCGv_i64 frame = tcg_temp_new_i64();
741 TCGv r31 = tcg_temp_new();
742
743 gen_load_frame(ctx, frame, src);
744 gen_frame_unscramble(frame);
745 tcg_gen_mov_i64(dst, frame);
746 tcg_gen_addi_tl(r29, src, 8);
747 tcg_gen_extrh_i64_i32(r31, dst);
748 gen_jumpr(ctx, r31);
749 }
750
751 static void gen_return(DisasContext *ctx, TCGv_i64 dst, TCGv src)
752 {
753 TCGv r29 = tcg_temp_new();
754 gen_return_base(ctx, dst, src, r29);
755 gen_log_reg_write(HEX_REG_SP, r29);
756 }
757
758 /* if (pred) dst = dealloc_return(src):raw */
759 static void gen_cond_return(DisasContext *ctx, TCGv_i64 dst, TCGv src,
760 TCGv pred, TCGCond cond)
761 {
762 TCGv LSB = tcg_temp_new();
763 TCGv mask = tcg_temp_new();
764 TCGv r29 = tcg_temp_new();
765 TCGLabel *skip = gen_new_label();
766 tcg_gen_andi_tl(LSB, pred, 1);
767
768 /* Initialize the results in case the predicate is false */
769 tcg_gen_movi_i64(dst, 0);
770 tcg_gen_movi_tl(r29, 0);
771
772 /* Set the bit in hex_slot_cancelled if the predicate is flase */
773 tcg_gen_movi_tl(mask, 1 << ctx->insn->slot);
774 tcg_gen_or_tl(mask, hex_slot_cancelled, mask);
775 tcg_gen_movcond_tl(cond, hex_slot_cancelled, LSB, tcg_constant_tl(0),
776 mask, hex_slot_cancelled);
777
778 tcg_gen_brcondi_tl(cond, LSB, 0, skip);
779 gen_return_base(ctx, dst, src, r29);
780 gen_set_label(skip);
781 gen_log_predicated_reg_write(HEX_REG_SP, r29, ctx->insn->slot);
782 }
783
784 /* sub-instruction version (no RddV, so handle it manually) */
785 static void gen_cond_return_subinsn(DisasContext *ctx, TCGCond cond, TCGv pred)
786 {
787 TCGv_i64 RddV = tcg_temp_new_i64();
788 gen_cond_return(ctx, RddV, hex_gpr[HEX_REG_FP], pred, cond);
789 gen_log_predicated_reg_write_pair(HEX_REG_FP, RddV, ctx->insn->slot);
790 }
791
792 static void gen_endloop0(DisasContext *ctx)
793 {
794 TCGv lpcfg = tcg_temp_new();
795
796 GET_USR_FIELD(USR_LPCFG, lpcfg);
797
798 /*
799 * if (lpcfg == 1) {
800 * hex_new_pred_value[3] = 0xff;
801 * hex_pred_written |= 1 << 3;
802 * }
803 */
804 TCGLabel *label1 = gen_new_label();
805 tcg_gen_brcondi_tl(TCG_COND_NE, lpcfg, 1, label1);
806 {
807 tcg_gen_movi_tl(hex_new_pred_value[3], 0xff);
808 tcg_gen_ori_tl(hex_pred_written, hex_pred_written, 1 << 3);
809 }
810 gen_set_label(label1);
811
812 /*
813 * if (lpcfg) {
814 * SET_USR_FIELD(USR_LPCFG, lpcfg - 1);
815 * }
816 */
817 TCGLabel *label2 = gen_new_label();
818 tcg_gen_brcondi_tl(TCG_COND_EQ, lpcfg, 0, label2);
819 {
820 tcg_gen_subi_tl(lpcfg, lpcfg, 1);
821 SET_USR_FIELD(USR_LPCFG, lpcfg);
822 }
823 gen_set_label(label2);
824
825 /*
826 * If we're in a tight loop, we'll do this at the end of the TB to take
827 * advantage of direct block chaining.
828 */
829 if (!ctx->is_tight_loop) {
830 /*
831 * if (hex_gpr[HEX_REG_LC0] > 1) {
832 * PC = hex_gpr[HEX_REG_SA0];
833 * hex_new_value[HEX_REG_LC0] = hex_gpr[HEX_REG_LC0] - 1;
834 * }
835 */
836 TCGLabel *label3 = gen_new_label();
837 tcg_gen_brcondi_tl(TCG_COND_LEU, hex_gpr[HEX_REG_LC0], 1, label3);
838 {
839 gen_jumpr(ctx, hex_gpr[HEX_REG_SA0]);
840 tcg_gen_subi_tl(hex_new_value[HEX_REG_LC0],
841 hex_gpr[HEX_REG_LC0], 1);
842 }
843 gen_set_label(label3);
844 }
845 }
846
847 static void gen_endloop1(DisasContext *ctx)
848 {
849 /*
850 * if (hex_gpr[HEX_REG_LC1] > 1) {
851 * PC = hex_gpr[HEX_REG_SA1];
852 * hex_new_value[HEX_REG_LC1] = hex_gpr[HEX_REG_LC1] - 1;
853 * }
854 */
855 TCGLabel *label = gen_new_label();
856 tcg_gen_brcondi_tl(TCG_COND_LEU, hex_gpr[HEX_REG_LC1], 1, label);
857 {
858 gen_jumpr(ctx, hex_gpr[HEX_REG_SA1]);
859 tcg_gen_subi_tl(hex_new_value[HEX_REG_LC1], hex_gpr[HEX_REG_LC1], 1);
860 }
861 gen_set_label(label);
862 }
863
864 static void gen_endloop01(DisasContext *ctx)
865 {
866 TCGv lpcfg = tcg_temp_new();
867 TCGLabel *label1 = gen_new_label();
868 TCGLabel *label2 = gen_new_label();
869 TCGLabel *label3 = gen_new_label();
870 TCGLabel *done = gen_new_label();
871
872 GET_USR_FIELD(USR_LPCFG, lpcfg);
873
874 /*
875 * if (lpcfg == 1) {
876 * hex_new_pred_value[3] = 0xff;
877 * hex_pred_written |= 1 << 3;
878 * }
879 */
880 tcg_gen_brcondi_tl(TCG_COND_NE, lpcfg, 1, label1);
881 {
882 tcg_gen_movi_tl(hex_new_pred_value[3], 0xff);
883 tcg_gen_ori_tl(hex_pred_written, hex_pred_written, 1 << 3);
884 }
885 gen_set_label(label1);
886
887 /*
888 * if (lpcfg) {
889 * SET_USR_FIELD(USR_LPCFG, lpcfg - 1);
890 * }
891 */
892 tcg_gen_brcondi_tl(TCG_COND_EQ, lpcfg, 0, label2);
893 {
894 tcg_gen_subi_tl(lpcfg, lpcfg, 1);
895 SET_USR_FIELD(USR_LPCFG, lpcfg);
896 }
897 gen_set_label(label2);
898
899 /*
900 * if (hex_gpr[HEX_REG_LC0] > 1) {
901 * PC = hex_gpr[HEX_REG_SA0];
902 * hex_new_value[HEX_REG_LC0] = hex_gpr[HEX_REG_LC0] - 1;
903 * } else {
904 * if (hex_gpr[HEX_REG_LC1] > 1) {
905 * hex_next_pc = hex_gpr[HEX_REG_SA1];
906 * hex_new_value[HEX_REG_LC1] = hex_gpr[HEX_REG_LC1] - 1;
907 * }
908 * }
909 */
910 tcg_gen_brcondi_tl(TCG_COND_LEU, hex_gpr[HEX_REG_LC0], 1, label3);
911 {
912 gen_jumpr(ctx, hex_gpr[HEX_REG_SA0]);
913 tcg_gen_subi_tl(hex_new_value[HEX_REG_LC0], hex_gpr[HEX_REG_LC0], 1);
914 tcg_gen_br(done);
915 }
916 gen_set_label(label3);
917 tcg_gen_brcondi_tl(TCG_COND_LEU, hex_gpr[HEX_REG_LC1], 1, done);
918 {
919 gen_jumpr(ctx, hex_gpr[HEX_REG_SA1]);
920 tcg_gen_subi_tl(hex_new_value[HEX_REG_LC1], hex_gpr[HEX_REG_LC1], 1);
921 }
922 gen_set_label(done);
923 }
924
925 static void gen_cmp_jumpnv(DisasContext *ctx,
926 TCGCond cond, TCGv val, TCGv src, int pc_off)
927 {
928 TCGv pred = tcg_temp_new();
929 tcg_gen_setcond_tl(cond, pred, val, src);
930 gen_cond_jump(ctx, TCG_COND_EQ, pred, pc_off);
931 }
932
933 static void gen_cmpi_jumpnv(DisasContext *ctx,
934 TCGCond cond, TCGv val, int src, int pc_off)
935 {
936 TCGv pred = tcg_temp_new();
937 tcg_gen_setcondi_tl(cond, pred, val, src);
938 gen_cond_jump(ctx, TCG_COND_EQ, pred, pc_off);
939 }
940
941 /* Shift left with saturation */
942 static void gen_shl_sat(TCGv dst, TCGv src, TCGv shift_amt)
943 {
944 TCGv sh32 = tcg_temp_new();
945 TCGv dst_sar = tcg_temp_new();
946 TCGv ovf = tcg_temp_new();
947 TCGv satval = tcg_temp_new();
948 TCGv min = tcg_constant_tl(0x80000000);
949 TCGv max = tcg_constant_tl(0x7fffffff);
950
951 /*
952 * Possible values for shift_amt are 0 .. 64
953 * We need special handling for values above 31
954 *
955 * sh32 = shift & 31;
956 * dst = sh32 == shift ? src : 0;
957 * dst <<= sh32;
958 * dst_sar = dst >> sh32;
959 * satval = src < 0 ? min : max;
960 * if (dst_asr != src) {
961 * usr.OVF |= 1;
962 * dst = satval;
963 * }
964 */
965
966 tcg_gen_andi_tl(sh32, shift_amt, 31);
967 tcg_gen_movcond_tl(TCG_COND_EQ, dst, sh32, shift_amt,
968 src, tcg_constant_tl(0));
969 tcg_gen_shl_tl(dst, dst, sh32);
970 tcg_gen_sar_tl(dst_sar, dst, sh32);
971 tcg_gen_movcond_tl(TCG_COND_LT, satval, src, tcg_constant_tl(0), min, max);
972
973 tcg_gen_setcond_tl(TCG_COND_NE, ovf, dst_sar, src);
974 tcg_gen_shli_tl(ovf, ovf, reg_field_info[USR_OVF].offset);
975 tcg_gen_or_tl(hex_new_value[HEX_REG_USR], hex_new_value[HEX_REG_USR], ovf);
976
977 tcg_gen_movcond_tl(TCG_COND_EQ, dst, dst_sar, src, dst, satval);
978 }
979
980 static void gen_sar(TCGv dst, TCGv src, TCGv shift_amt)
981 {
982 /*
983 * Shift arithmetic right
984 * Robust when shift_amt is >31 bits
985 */
986 TCGv tmp = tcg_temp_new();
987 tcg_gen_umin_tl(tmp, shift_amt, tcg_constant_tl(31));
988 tcg_gen_sar_tl(dst, src, tmp);
989 }
990
991 /* Bidirectional shift right with saturation */
992 static void gen_asr_r_r_sat(TCGv RdV, TCGv RsV, TCGv RtV)
993 {
994 TCGv shift_amt = tcg_temp_new();
995 TCGLabel *positive = gen_new_label();
996 TCGLabel *done = gen_new_label();
997
998 tcg_gen_sextract_i32(shift_amt, RtV, 0, 7);
999 tcg_gen_brcondi_tl(TCG_COND_GE, shift_amt, 0, positive);
1000
1001 /* Negative shift amount => shift left */
1002 tcg_gen_neg_tl(shift_amt, shift_amt);
1003 gen_shl_sat(RdV, RsV, shift_amt);
1004 tcg_gen_br(done);
1005
1006 gen_set_label(positive);
1007 /* Positive shift amount => shift right */
1008 gen_sar(RdV, RsV, shift_amt);
1009
1010 gen_set_label(done);
1011 }
1012
1013 /* Bidirectional shift left with saturation */
1014 static void gen_asl_r_r_sat(TCGv RdV, TCGv RsV, TCGv RtV)
1015 {
1016 TCGv shift_amt = tcg_temp_new();
1017 TCGLabel *positive = gen_new_label();
1018 TCGLabel *done = gen_new_label();
1019
1020 tcg_gen_sextract_i32(shift_amt, RtV, 0, 7);
1021 tcg_gen_brcondi_tl(TCG_COND_GE, shift_amt, 0, positive);
1022
1023 /* Negative shift amount => shift right */
1024 tcg_gen_neg_tl(shift_amt, shift_amt);
1025 gen_sar(RdV, RsV, shift_amt);
1026 tcg_gen_br(done);
1027
1028 gen_set_label(positive);
1029 /* Positive shift amount => shift left */
1030 gen_shl_sat(RdV, RsV, shift_amt);
1031
1032 gen_set_label(done);
1033 }
1034
1035 static intptr_t vreg_src_off(DisasContext *ctx, int num)
1036 {
1037 intptr_t offset = offsetof(CPUHexagonState, VRegs[num]);
1038
1039 if (test_bit(num, ctx->vregs_select)) {
1040 offset = ctx_future_vreg_off(ctx, num, 1, false);
1041 }
1042 if (test_bit(num, ctx->vregs_updated_tmp)) {
1043 offset = ctx_tmp_vreg_off(ctx, num, 1, false);
1044 }
1045 return offset;
1046 }
1047
1048 static void gen_log_vreg_write(DisasContext *ctx, intptr_t srcoff, int num,
1049 VRegWriteType type, int slot_num,
1050 bool is_predicated)
1051 {
1052 TCGLabel *label_end = NULL;
1053 intptr_t dstoff;
1054
1055 if (is_predicated) {
1056 TCGv cancelled = tcg_temp_new();
1057 label_end = gen_new_label();
1058
1059 /* Don't do anything if the slot was cancelled */
1060 tcg_gen_extract_tl(cancelled, hex_slot_cancelled, slot_num, 1);
1061 tcg_gen_brcondi_tl(TCG_COND_NE, cancelled, 0, label_end);
1062 }
1063
1064 if (type != EXT_TMP) {
1065 dstoff = ctx_future_vreg_off(ctx, num, 1, true);
1066 tcg_gen_gvec_mov(MO_64, dstoff, srcoff,
1067 sizeof(MMVector), sizeof(MMVector));
1068 tcg_gen_ori_tl(hex_VRegs_updated, hex_VRegs_updated, 1 << num);
1069 } else {
1070 dstoff = ctx_tmp_vreg_off(ctx, num, 1, false);
1071 tcg_gen_gvec_mov(MO_64, dstoff, srcoff,
1072 sizeof(MMVector), sizeof(MMVector));
1073 }
1074
1075 if (is_predicated) {
1076 gen_set_label(label_end);
1077 }
1078 }
1079
1080 static void gen_log_vreg_write_pair(DisasContext *ctx, intptr_t srcoff, int num,
1081 VRegWriteType type, int slot_num,
1082 bool is_predicated)
1083 {
1084 gen_log_vreg_write(ctx, srcoff, num ^ 0, type, slot_num, is_predicated);
1085 srcoff += sizeof(MMVector);
1086 gen_log_vreg_write(ctx, srcoff, num ^ 1, type, slot_num, is_predicated);
1087 }
1088
1089 static void gen_log_qreg_write(intptr_t srcoff, int num, int vnew,
1090 int slot_num, bool is_predicated)
1091 {
1092 TCGLabel *label_end = NULL;
1093 intptr_t dstoff;
1094
1095 if (is_predicated) {
1096 TCGv cancelled = tcg_temp_new();
1097 label_end = gen_new_label();
1098
1099 /* Don't do anything if the slot was cancelled */
1100 tcg_gen_extract_tl(cancelled, hex_slot_cancelled, slot_num, 1);
1101 tcg_gen_brcondi_tl(TCG_COND_NE, cancelled, 0, label_end);
1102 }
1103
1104 dstoff = offsetof(CPUHexagonState, future_QRegs[num]);
1105 tcg_gen_gvec_mov(MO_64, dstoff, srcoff, sizeof(MMQReg), sizeof(MMQReg));
1106
1107 if (is_predicated) {
1108 tcg_gen_ori_tl(hex_QRegs_updated, hex_QRegs_updated, 1 << num);
1109 gen_set_label(label_end);
1110 }
1111 }
1112
1113 static void gen_vreg_load(DisasContext *ctx, intptr_t dstoff, TCGv src,
1114 bool aligned)
1115 {
1116 TCGv_i64 tmp = tcg_temp_new_i64();
1117 if (aligned) {
1118 tcg_gen_andi_tl(src, src, ~((int32_t)sizeof(MMVector) - 1));
1119 }
1120 for (int i = 0; i < sizeof(MMVector) / 8; i++) {
1121 tcg_gen_qemu_ld64(tmp, src, ctx->mem_idx);
1122 tcg_gen_addi_tl(src, src, 8);
1123 tcg_gen_st_i64(tmp, cpu_env, dstoff + i * 8);
1124 }
1125 }
1126
1127 static void gen_vreg_store(DisasContext *ctx, TCGv EA, intptr_t srcoff,
1128 int slot, bool aligned)
1129 {
1130 intptr_t dstoff = offsetof(CPUHexagonState, vstore[slot].data);
1131 intptr_t maskoff = offsetof(CPUHexagonState, vstore[slot].mask);
1132
1133 if (is_gather_store_insn(ctx)) {
1134 TCGv sl = tcg_constant_tl(slot);
1135 gen_helper_gather_store(cpu_env, EA, sl);
1136 return;
1137 }
1138
1139 tcg_gen_movi_tl(hex_vstore_pending[slot], 1);
1140 if (aligned) {
1141 tcg_gen_andi_tl(hex_vstore_addr[slot], EA,
1142 ~((int32_t)sizeof(MMVector) - 1));
1143 } else {
1144 tcg_gen_mov_tl(hex_vstore_addr[slot], EA);
1145 }
1146 tcg_gen_movi_tl(hex_vstore_size[slot], sizeof(MMVector));
1147
1148 /* Copy the data to the vstore buffer */
1149 tcg_gen_gvec_mov(MO_64, dstoff, srcoff, sizeof(MMVector), sizeof(MMVector));
1150 /* Set the mask to all 1's */
1151 tcg_gen_gvec_dup_imm(MO_64, maskoff, sizeof(MMQReg), sizeof(MMQReg), ~0LL);
1152 }
1153
1154 static void gen_vreg_masked_store(DisasContext *ctx, TCGv EA, intptr_t srcoff,
1155 intptr_t bitsoff, int slot, bool invert)
1156 {
1157 intptr_t dstoff = offsetof(CPUHexagonState, vstore[slot].data);
1158 intptr_t maskoff = offsetof(CPUHexagonState, vstore[slot].mask);
1159
1160 tcg_gen_movi_tl(hex_vstore_pending[slot], 1);
1161 tcg_gen_andi_tl(hex_vstore_addr[slot], EA,
1162 ~((int32_t)sizeof(MMVector) - 1));
1163 tcg_gen_movi_tl(hex_vstore_size[slot], sizeof(MMVector));
1164
1165 /* Copy the data to the vstore buffer */
1166 tcg_gen_gvec_mov(MO_64, dstoff, srcoff, sizeof(MMVector), sizeof(MMVector));
1167 /* Copy the mask */
1168 tcg_gen_gvec_mov(MO_64, maskoff, bitsoff, sizeof(MMQReg), sizeof(MMQReg));
1169 if (invert) {
1170 tcg_gen_gvec_not(MO_64, maskoff, maskoff,
1171 sizeof(MMQReg), sizeof(MMQReg));
1172 }
1173 }
1174
1175 static void vec_to_qvec(size_t size, intptr_t dstoff, intptr_t srcoff)
1176 {
1177 TCGv_i64 tmp = tcg_temp_new_i64();
1178 TCGv_i64 word = tcg_temp_new_i64();
1179 TCGv_i64 bits = tcg_temp_new_i64();
1180 TCGv_i64 mask = tcg_temp_new_i64();
1181 TCGv_i64 zero = tcg_constant_i64(0);
1182 TCGv_i64 ones = tcg_constant_i64(~0);
1183
1184 for (int i = 0; i < sizeof(MMVector) / 8; i++) {
1185 tcg_gen_ld_i64(tmp, cpu_env, srcoff + i * 8);
1186 tcg_gen_movi_i64(mask, 0);
1187
1188 for (int j = 0; j < 8; j += size) {
1189 tcg_gen_extract_i64(word, tmp, j * 8, size * 8);
1190 tcg_gen_movcond_i64(TCG_COND_NE, bits, word, zero, ones, zero);
1191 tcg_gen_deposit_i64(mask, mask, bits, j, size);
1192 }
1193
1194 tcg_gen_st8_i64(mask, cpu_env, dstoff + i);
1195 }
1196 }
1197
1198 void probe_noshuf_load(TCGv va, int s, int mi)
1199 {
1200 TCGv size = tcg_constant_tl(s);
1201 TCGv mem_idx = tcg_constant_tl(mi);
1202 gen_helper_probe_noshuf_load(cpu_env, va, size, mem_idx);
1203 }
1204
1205 /*
1206 * Note: Since this function might branch, `val` is
1207 * required to be a `tcg_temp_local`.
1208 */
1209 void gen_set_usr_field_if(int field, TCGv val)
1210 {
1211 /* Sets the USR field if `val` is non-zero */
1212 if (reg_field_info[field].width == 1) {
1213 TCGv tmp = tcg_temp_new();
1214 tcg_gen_extract_tl(tmp, val, 0, reg_field_info[field].width);
1215 tcg_gen_shli_tl(tmp, tmp, reg_field_info[field].offset);
1216 tcg_gen_or_tl(hex_new_value[HEX_REG_USR],
1217 hex_new_value[HEX_REG_USR],
1218 tmp);
1219 } else {
1220 TCGLabel *skip_label = gen_new_label();
1221 tcg_gen_brcondi_tl(TCG_COND_EQ, val, 0, skip_label);
1222 gen_set_usr_field(field, val);
1223 gen_set_label(skip_label);
1224 }
1225 }
1226
1227 void gen_sat_i32(TCGv dest, TCGv source, int width)
1228 {
1229 TCGv max_val = tcg_constant_tl((1 << (width - 1)) - 1);
1230 TCGv min_val = tcg_constant_tl(-(1 << (width - 1)));
1231 tcg_gen_smin_tl(dest, source, max_val);
1232 tcg_gen_smax_tl(dest, dest, min_val);
1233 }
1234
1235 void gen_sat_i32_ovfl(TCGv ovfl, TCGv dest, TCGv source, int width)
1236 {
1237 gen_sat_i32(dest, source, width);
1238 tcg_gen_setcond_tl(TCG_COND_NE, ovfl, source, dest);
1239 }
1240
1241 void gen_satu_i32(TCGv dest, TCGv source, int width)
1242 {
1243 TCGv max_val = tcg_constant_tl((1 << width) - 1);
1244 TCGv zero = tcg_constant_tl(0);
1245 tcg_gen_movcond_tl(TCG_COND_GTU, dest, source, max_val, max_val, source);
1246 tcg_gen_movcond_tl(TCG_COND_LT, dest, source, zero, zero, dest);
1247 }
1248
1249 void gen_satu_i32_ovfl(TCGv ovfl, TCGv dest, TCGv source, int width)
1250 {
1251 gen_satu_i32(dest, source, width);
1252 tcg_gen_setcond_tl(TCG_COND_NE, ovfl, source, dest);
1253 }
1254
1255 void gen_sat_i64(TCGv_i64 dest, TCGv_i64 source, int width)
1256 {
1257 TCGv_i64 max_val = tcg_constant_i64((1LL << (width - 1)) - 1LL);
1258 TCGv_i64 min_val = tcg_constant_i64(-(1LL << (width - 1)));
1259 tcg_gen_smin_i64(dest, source, max_val);
1260 tcg_gen_smax_i64(dest, dest, min_val);
1261 }
1262
1263 void gen_sat_i64_ovfl(TCGv ovfl, TCGv_i64 dest, TCGv_i64 source, int width)
1264 {
1265 TCGv_i64 ovfl_64;
1266 gen_sat_i64(dest, source, width);
1267 ovfl_64 = tcg_temp_new_i64();
1268 tcg_gen_setcond_i64(TCG_COND_NE, ovfl_64, dest, source);
1269 tcg_gen_trunc_i64_tl(ovfl, ovfl_64);
1270 }
1271
1272 void gen_satu_i64(TCGv_i64 dest, TCGv_i64 source, int width)
1273 {
1274 TCGv_i64 max_val = tcg_constant_i64((1LL << width) - 1LL);
1275 TCGv_i64 zero = tcg_constant_i64(0);
1276 tcg_gen_movcond_i64(TCG_COND_GTU, dest, source, max_val, max_val, source);
1277 tcg_gen_movcond_i64(TCG_COND_LT, dest, source, zero, zero, dest);
1278 }
1279
1280 void gen_satu_i64_ovfl(TCGv ovfl, TCGv_i64 dest, TCGv_i64 source, int width)
1281 {
1282 TCGv_i64 ovfl_64;
1283 gen_satu_i64(dest, source, width);
1284 ovfl_64 = tcg_temp_new_i64();
1285 tcg_gen_setcond_i64(TCG_COND_NE, ovfl_64, dest, source);
1286 tcg_gen_trunc_i64_tl(ovfl, ovfl_64);
1287 }
1288
1289 /* Implements the fADDSAT64 macro in TCG */
1290 void gen_add_sat_i64(TCGv_i64 ret, TCGv_i64 a, TCGv_i64 b)
1291 {
1292 TCGv_i64 sum = tcg_temp_new_i64();
1293 TCGv_i64 xor = tcg_temp_new_i64();
1294 TCGv_i64 cond1 = tcg_temp_new_i64();
1295 TCGv_i64 cond2 = tcg_temp_new_i64();
1296 TCGv_i64 cond3 = tcg_temp_new_i64();
1297 TCGv_i64 mask = tcg_constant_i64(0x8000000000000000ULL);
1298 TCGv_i64 max_pos = tcg_constant_i64(0x7FFFFFFFFFFFFFFFLL);
1299 TCGv_i64 max_neg = tcg_constant_i64(0x8000000000000000LL);
1300 TCGv_i64 zero = tcg_constant_i64(0);
1301 TCGLabel *no_ovfl_label = gen_new_label();
1302 TCGLabel *ovfl_label = gen_new_label();
1303 TCGLabel *ret_label = gen_new_label();
1304
1305 tcg_gen_add_i64(sum, a, b);
1306 tcg_gen_xor_i64(xor, a, b);
1307
1308 /* if (xor & mask) */
1309 tcg_gen_and_i64(cond1, xor, mask);
1310 tcg_gen_brcondi_i64(TCG_COND_NE, cond1, 0, no_ovfl_label);
1311
1312 /* else if ((a ^ sum) & mask) */
1313 tcg_gen_xor_i64(cond2, a, sum);
1314 tcg_gen_and_i64(cond2, cond2, mask);
1315 tcg_gen_brcondi_i64(TCG_COND_NE, cond2, 0, ovfl_label);
1316 /* fallthrough to no_ovfl_label branch */
1317
1318 /* if branch */
1319 gen_set_label(no_ovfl_label);
1320 tcg_gen_mov_i64(ret, sum);
1321 tcg_gen_br(ret_label);
1322
1323 /* else if branch */
1324 gen_set_label(ovfl_label);
1325 tcg_gen_and_i64(cond3, sum, mask);
1326 tcg_gen_movcond_i64(TCG_COND_NE, ret, cond3, zero, max_pos, max_neg);
1327 SET_USR_FIELD(USR_OVF, 1);
1328
1329 gen_set_label(ret_label);
1330 }
1331
1332 #include "tcg_funcs_generated.c.inc"
1333 #include "tcg_func_table_generated.c.inc"