2 * Tiny Code Generator for QEMU
4 * Copyright (c) 2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
60 static const int tcg_target_reg_alloc_order[TCG_TARGET_NB_REGS] = {
77 static const int tcg_target_call_iarg_regs[6] = {
86 static const int tcg_target_call_oarg_regs[2] = {
91 static void patch_reloc(uint8_t *code_ptr, int type,
92 tcg_target_long value)
96 if (value != (uint32_t)value)
98 *(uint32_t *)code_ptr = value;
105 /* maximum number of register used for input function arguments */
106 static inline int tcg_target_get_call_iarg_regs_count(int flags)
111 /* parse target specific constraints */
112 static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
119 case 'L': /* qemu_ld/st constraint */
120 ct->ct |= TCG_CT_REG;
121 tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
124 ct->ct |= TCG_CT_CONST_S11;
127 ct->ct |= TCG_CT_CONST_S13;
137 #define ABS(x) ((x) < 0? -(x) : (x))
138 /* test if a constant matches the constraint */
139 static inline int tcg_target_const_match(tcg_target_long val,
140 const TCGArgConstraint *arg_ct)
145 if (ct & TCG_CT_CONST)
147 else if ((ct & TCG_CT_CONST_S11) && ABS(val) == (ABS(val) & 0x3ff))
149 else if ((ct & TCG_CT_CONST_S13) && ABS(val) == (ABS(val) & 0xfff))
155 #define INSN_OP(x) ((x) << 30)
156 #define INSN_OP2(x) ((x) << 22)
157 #define INSN_OP3(x) ((x) << 19)
158 #define INSN_OPF(x) ((x) << 5)
159 #define INSN_RD(x) ((x) << 25)
160 #define INSN_RS1(x) ((x) << 14)
161 #define INSN_RS2(x) (x)
163 #define INSN_IMM13(x) ((1 << 13) | ((x) & 0x1fff))
165 #define INSN_COND(x, a) (((x) << 25) | ((a) << 29)
167 #define ARITH_ADD (INSN_OP(2) | INSN_OP3(0x00))
168 #define ARITH_AND (INSN_OP(2) | INSN_OP3(0x01))
169 #define ARITH_OR (INSN_OP(2) | INSN_OP3(0x02))
170 #define ARITH_XOR (INSN_OP(2) | INSN_OP3(0x03))
171 #define ARITH_SUB (INSN_OP(2) | INSN_OP3(0x08))
172 #define ARITH_ADDX (INSN_OP(2) | INSN_OP3(0x10))
173 #define ARITH_SUBX (INSN_OP(2) | INSN_OP3(0x0c))
174 #define ARITH_UMUL (INSN_OP(2) | INSN_OP3(0x0a))
175 #define ARITH_UDIV (INSN_OP(2) | INSN_OP3(0x0e))
176 #define ARITH_SDIV (INSN_OP(2) | INSN_OP3(0x0f))
177 #define ARITH_MULX (INSN_OP(2) | INSN_OP3(0x09))
178 #define ARITH_UDIVX (INSN_OP(2) | INSN_OP3(0x0d))
179 #define ARITH_SDIVX (INSN_OP(2) | INSN_OP3(0x2d))
181 #define SHIFT_SLL (INSN_OP(2) | INSN_OP3(0x25))
182 #define SHIFT_SRL (INSN_OP(2) | INSN_OP3(0x26))
183 #define SHIFT_SRA (INSN_OP(2) | INSN_OP3(0x27))
185 #define SHIFT_SLLX (INSN_OP(2) | INSN_OP3(0x25) | (1 << 12))
186 #define SHIFT_SRLX (INSN_OP(2) | INSN_OP3(0x26) | (1 << 12))
187 #define SHIFT_SRAX (INSN_OP(2) | INSN_OP3(0x27) | (1 << 12))
189 #define WRY (INSN_OP(2) | INSN_OP3(0x30))
190 #define JMPL (INSN_OP(2) | INSN_OP3(0x38))
191 #define SAVE (INSN_OP(2) | INSN_OP3(0x3c))
192 #define RESTORE (INSN_OP(2) | INSN_OP3(0x3d))
193 #define SETHI (INSN_OP(0) | INSN_OP2(0x4))
194 #define CALL INSN_OP(1)
195 #define LDUB (INSN_OP(3) | INSN_OP3(0x01))
196 #define LDSB (INSN_OP(3) | INSN_OP3(0x09))
197 #define LDUH (INSN_OP(3) | INSN_OP3(0x02))
198 #define LDSH (INSN_OP(3) | INSN_OP3(0x0a))
199 #define LDUW (INSN_OP(3) | INSN_OP3(0x00))
200 #define LDSW (INSN_OP(3) | INSN_OP3(0x08))
201 #define LDX (INSN_OP(3) | INSN_OP3(0x0b))
202 #define STB (INSN_OP(3) | INSN_OP3(0x05))
203 #define STH (INSN_OP(3) | INSN_OP3(0x06))
204 #define STW (INSN_OP(3) | INSN_OP3(0x04))
205 #define STX (INSN_OP(3) | INSN_OP3(0x0e))
207 static inline void tcg_out_mov(TCGContext *s, int ret, int arg)
209 tcg_out32(s, ARITH_OR | INSN_RD(ret) | INSN_RS1(arg) |
210 INSN_RS2(TCG_REG_G0));
213 static inline void tcg_out_movi(TCGContext *s, TCGType type,
214 int ret, tcg_target_long arg)
216 if (arg == (arg & 0xfff))
217 tcg_out32(s, ARITH_OR | INSN_RD(ret) | INSN_RS1(TCG_REG_G0) |
220 tcg_out32(s, SETHI | INSN_RD(ret) | ((arg & 0xfffffc00) >> 10));
222 tcg_out32(s, ARITH_OR | INSN_RD(ret) | INSN_RS1(ret) |
223 INSN_IMM13(arg & 0x3ff));
227 static inline void tcg_out_ld_raw(TCGContext *s, int ret,
230 tcg_out32(s, SETHI | INSN_RD(ret) | (((uint32_t)arg & 0xfffffc00) >> 10));
231 tcg_out32(s, LDUW | INSN_RD(ret) | INSN_RS1(ret) |
232 INSN_IMM13(arg & 0x3ff));
235 static inline void tcg_out_ldst(TCGContext *s, int ret, int addr, int offset, int op)
237 if (offset == (offset & 0xfff))
238 tcg_out32(s, op | INSN_RD(ret) | INSN_RS1(addr) |
241 fprintf(stderr, "unimplemented %s with offset %d\n", __func__, offset);
244 static inline void tcg_out_ld(TCGContext *s, int ret,
245 int arg1, tcg_target_long arg2)
247 fprintf(stderr, "unimplemented %s\n", __func__);
250 static inline void tcg_out_st(TCGContext *s, int arg,
251 int arg1, tcg_target_long arg2)
253 fprintf(stderr, "unimplemented %s\n", __func__);
256 static inline void tcg_out_arith(TCGContext *s, int rd, int rs1, int rs2,
259 tcg_out32(s, op | INSN_RD(rd) | INSN_RS1(rs1) |
263 static inline void tcg_out_arithi(TCGContext *s, int rd, int rs1, int offset,
266 tcg_out32(s, op | INSN_RD(rd) | INSN_RS1(rs1) |
270 static inline void tcg_out_sety(TCGContext *s, tcg_target_long val)
272 if (val == 0 || val == -1)
273 tcg_out32(s, WRY | INSN_IMM13(val));
275 fprintf(stderr, "unimplemented sety %ld\n", (long)val);
278 static inline void tcg_out_addi(TCGContext *s, int reg, tcg_target_long val)
281 if (val == (val & 0xfff))
282 tcg_out_arithi(s, reg, reg, val, ARITH_ADD);
284 fprintf(stderr, "unimplemented addi %ld\n", (long)val);
288 static inline void tcg_out_nop(TCGContext *s)
290 tcg_out32(s, SETHI | INSN_RD(TCG_REG_G0) | 0);
293 static inline void tcg_out_op(TCGContext *s, int opc, const TCGArg *args,
294 const int *const_args)
299 case INDEX_op_exit_tb:
300 tcg_out_movi(s, TCG_TYPE_TL, TCG_REG_O0, args[0]);
301 tcg_out32(s, JMPL | INSN_RD(TCG_REG_G0) | INSN_RS1(TCG_REG_O7) |
305 case INDEX_op_goto_tb:
306 if (s->tb_jmp_offset) {
307 /* direct jump method */
308 tcg_out32(s, CALL | 0);
309 s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
312 /* indirect jump method */
313 tcg_out_ld_raw(s, TCG_REG_O7, (tcg_target_long)(s->tb_next + args[0]));
314 tcg_out32(s, JMPL | INSN_RD(TCG_REG_O7) | INSN_RS1(TCG_REG_O7) |
315 INSN_RS2(TCG_REG_G0));
318 s->tb_next_offset[args[0]] = s->code_ptr - s->code_buf;
322 tcg_out32(s, CALL | ((((tcg_target_ulong)args[0]
323 - (tcg_target_ulong)s->code_ptr) >> 2)
327 tcg_out_ld_raw(s, TCG_REG_O7, (tcg_target_long)(s->tb_next + args[0]));
328 tcg_out32(s, JMPL | INSN_RD(TCG_REG_O7) | INSN_RS1(TCG_REG_O7) |
329 INSN_RS2(TCG_REG_G0));
334 fprintf(stderr, "unimplemented jmp\n");
337 fprintf(stderr, "unimplemented br\n");
339 case INDEX_op_movi_i32:
340 tcg_out_movi(s, TCG_TYPE_I32, args[0], (uint32_t)args[1]);
343 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
344 #define OP_32_64(x) \
345 glue(glue(case INDEX_op_, x), _i32:) \
346 glue(glue(case INDEX_op_, x), _i64:)
348 #define OP_32_64(x) \
349 glue(glue(case INDEX_op_, x), _i32:)
352 tcg_out_ldst(s, args[0], args[1], args[2], LDUB);
355 tcg_out_ldst(s, args[0], args[1], args[2], LDSB);
358 tcg_out_ldst(s, args[0], args[1], args[2], LDUH);
361 tcg_out_ldst(s, args[0], args[1], args[2], LDSH);
363 case INDEX_op_ld_i32:
364 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
365 case INDEX_op_ld_i32u_i64:
367 tcg_out_ldst(s, args[0], args[1], args[2], LDUW);
370 tcg_out_ldst(s, args[0], args[1], args[2], STB);
373 tcg_out_ldst(s, args[0], args[1], args[2], STH);
375 case INDEX_op_st_i32:
376 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
377 case INDEX_op_st_i32_i64:
379 tcg_out_ldst(s, args[0], args[1], args[2], STW);
393 case INDEX_op_shl_i32:
396 case INDEX_op_shr_i32:
399 case INDEX_op_sar_i32:
402 case INDEX_op_mul_i32:
409 tcg_out_arithi(s, args[0], args[1], args[2], c);
411 tcg_out_arith(s, args[0], args[1], args[2], c);
415 case INDEX_op_div2_i32:
416 #if defined(__sparc_v9__) || defined(__sparc_v8plus__)
424 case INDEX_op_divu2_i32:
425 #if defined(__sparc_v9__) || defined(__sparc_v8plus__)
434 case INDEX_op_brcond_i32:
435 fprintf(stderr, "unimplemented brcond\n");
438 case INDEX_op_qemu_ld8u:
439 fprintf(stderr, "unimplemented qld\n");
441 case INDEX_op_qemu_ld8s:
442 fprintf(stderr, "unimplemented qld\n");
444 case INDEX_op_qemu_ld16u:
445 fprintf(stderr, "unimplemented qld\n");
447 case INDEX_op_qemu_ld16s:
448 fprintf(stderr, "unimplemented qld\n");
450 case INDEX_op_qemu_ld32u:
451 fprintf(stderr, "unimplemented qld\n");
453 case INDEX_op_qemu_ld32s:
454 fprintf(stderr, "unimplemented qld\n");
456 case INDEX_op_qemu_st8:
457 fprintf(stderr, "unimplemented qst\n");
459 case INDEX_op_qemu_st16:
460 fprintf(stderr, "unimplemented qst\n");
462 case INDEX_op_qemu_st32:
463 fprintf(stderr, "unimplemented qst\n");
466 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
467 case INDEX_op_movi_i64:
468 tcg_out_movi(s, TCG_TYPE_I64, args[0], args[1]);
470 case INDEX_op_ld_i64:
471 tcg_out_ldst(s, args[0], args[1], args[2], LDX);
473 case INDEX_op_st_i64:
474 tcg_out_ldst(s, args[0], args[1], args[2], STX);
476 case INDEX_op_shl_i64:
479 case INDEX_op_shr_i64:
482 case INDEX_op_sar_i64:
485 case INDEX_op_mul_i64:
488 case INDEX_op_div2_i64:
491 case INDEX_op_divu2_i64:
495 case INDEX_op_brcond_i64:
496 fprintf(stderr, "unimplemented brcond\n");
498 case INDEX_op_qemu_ld64:
499 fprintf(stderr, "unimplemented qld\n");
501 case INDEX_op_qemu_st64:
502 fprintf(stderr, "unimplemented qst\n");
507 fprintf(stderr, "unknown opcode 0x%x\n", opc);
512 static const TCGTargetOpDef sparc_op_defs[] = {
513 { INDEX_op_exit_tb, { } },
514 { INDEX_op_goto_tb, { } },
515 { INDEX_op_call, { "ri" } },
516 { INDEX_op_jmp, { "ri" } },
517 { INDEX_op_br, { } },
519 { INDEX_op_mov_i32, { "r", "r" } },
520 { INDEX_op_movi_i32, { "r" } },
521 { INDEX_op_ld8u_i32, { "r", "r" } },
522 { INDEX_op_ld8s_i32, { "r", "r" } },
523 { INDEX_op_ld16u_i32, { "r", "r" } },
524 { INDEX_op_ld16s_i32, { "r", "r" } },
525 { INDEX_op_ld_i32, { "r", "r" } },
526 { INDEX_op_st8_i32, { "r", "r" } },
527 { INDEX_op_st16_i32, { "r", "r" } },
528 { INDEX_op_st_i32, { "r", "r" } },
530 { INDEX_op_add_i32, { "r", "0", "rJ" } },
531 { INDEX_op_mul_i32, { "r", "0", "rJ" } },
532 { INDEX_op_div2_i32, { "r", "r", "0", "1", "r" } },
533 { INDEX_op_divu2_i32, { "r", "r", "0", "1", "r" } },
534 { INDEX_op_sub_i32, { "r", "0", "rJ" } },
535 { INDEX_op_and_i32, { "r", "0", "rJ" } },
536 { INDEX_op_or_i32, { "r", "0", "rJ" } },
537 { INDEX_op_xor_i32, { "r", "0", "rJ" } },
539 { INDEX_op_shl_i32, { "r", "0", "rJ" } },
540 { INDEX_op_shr_i32, { "r", "0", "rJ" } },
541 { INDEX_op_sar_i32, { "r", "0", "rJ" } },
543 { INDEX_op_brcond_i32, { "r", "ri" } },
545 { INDEX_op_qemu_ld8u, { "r", "L" } },
546 { INDEX_op_qemu_ld8s, { "r", "L" } },
547 { INDEX_op_qemu_ld16u, { "r", "L" } },
548 { INDEX_op_qemu_ld16s, { "r", "L" } },
549 { INDEX_op_qemu_ld32u, { "r", "L" } },
550 { INDEX_op_qemu_ld32s, { "r", "L" } },
552 { INDEX_op_qemu_st8, { "L", "L" } },
553 { INDEX_op_qemu_st16, { "L", "L" } },
554 { INDEX_op_qemu_st32, { "L", "L" } },
556 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
557 { INDEX_op_mov_i64, { "r", "r" } },
558 { INDEX_op_movi_i64, { "r" } },
559 { INDEX_op_ld8u_i64, { "r", "r" } },
560 { INDEX_op_ld8s_i64, { "r", "r" } },
561 { INDEX_op_ld16u_i64, { "r", "r" } },
562 { INDEX_op_ld16s_i64, { "r", "r" } },
563 { INDEX_op_ld32u_i64, { "r", "r" } },
564 { INDEX_op_ld32s_i64, { "r", "r" } },
565 { INDEX_op_ld_i64, { "r", "r" } },
566 { INDEX_op_st8_i64, { "r", "r" } },
567 { INDEX_op_st16_i64, { "r", "r" } },
568 { INDEX_op_st32_i64, { "r", "r" } },
569 { INDEX_op_st_i64, { "r", "r" } },
571 { INDEX_op_add_i64, { "r", "0", "rJ" } },
572 { INDEX_op_mul_i64, { "r", "0", "rJ" } },
573 { INDEX_op_div2_i64, { "r", "r", "0", "1", "r" } },
574 { INDEX_op_divu2_i64, { "r", "r", "0", "1", "r" } },
575 { INDEX_op_sub_i64, { "r", "0", "rJ" } },
576 { INDEX_op_and_i64, { "r", "0", "rJ" } },
577 { INDEX_op_or_i64, { "r", "0", "rJ" } },
578 { INDEX_op_xor_i64, { "r", "0", "rJ" } },
580 { INDEX_op_shl_i64, { "r", "0", "rJ" } },
581 { INDEX_op_shr_i64, { "r", "0", "rJ" } },
582 { INDEX_op_sar_i64, { "r", "0", "rJ" } },
584 { INDEX_op_brcond_i64, { "r", "ri" } },
589 void tcg_target_init(TCGContext *s)
591 tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffffffff);
592 #if defined(__sparc_v9__) && !defined(__sparc_v8plus__)
593 tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I64], 0, 0xffffffff);
595 tcg_regset_set32(tcg_target_call_clobber_regs, 0,
605 tcg_regset_clear(s->reserved_regs);
606 tcg_regset_set_reg(s->reserved_regs, TCG_REG_G0);
607 tcg_regset_set_reg(s->reserved_regs, TCG_REG_I6);
608 tcg_regset_set_reg(s->reserved_regs, TCG_REG_I7);
609 tcg_regset_set_reg(s->reserved_regs, TCG_REG_O6);
610 tcg_regset_set_reg(s->reserved_regs, TCG_REG_O7);
611 tcg_add_target_add_op_defs(sparc_op_defs);