]> Git Repo - qemu.git/blame - target/loongarch/translate.c
target/loongarch: Add floating point arithmetic instruction translation
[qemu.git] / target / loongarch / translate.c
CommitLineData
f8da88d7
SG
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * LoongArch emulation for QEMU - main translation routines.
4 *
5 * Copyright (c) 2021 Loongson Technology Corporation Limited
6 */
7
8#include "qemu/osdep.h"
9#include "cpu.h"
10#include "tcg/tcg-op.h"
11#include "exec/translator.h"
12#include "exec/helper-proto.h"
13#include "exec/helper-gen.h"
14
15#include "exec/translator.h"
16#include "exec/log.h"
17#include "qemu/qemu-print.h"
d578ca6c 18#include "fpu/softfloat.h"
f8da88d7
SG
19#include "translate.h"
20#include "internals.h"
21
22/* Global register indices */
23TCGv cpu_gpr[32], cpu_pc;
24static TCGv cpu_lladdr, cpu_llval;
25TCGv_i32 cpu_fcsr0;
26TCGv_i64 cpu_fpr[32];
27
28#define DISAS_STOP DISAS_TARGET_0
29
143d6785
SG
30static inline int plus_1(DisasContext *ctx, int x)
31{
32 return x + 1;
33}
34
bb79174d
SG
35static inline int shl_2(DisasContext *ctx, int x)
36{
37 return x << 2;
38}
39
d578ca6c
SG
40/*
41 * LoongArch the upper 32 bits are undefined ("can be any value").
42 * QEMU chooses to nanbox, because it is most likely to show guest bugs early.
43 */
44static void gen_nanbox_s(TCGv_i64 out, TCGv_i64 in)
45{
46 tcg_gen_ori_i64(out, in, MAKE_64BIT_MASK(32, 32));
47}
48
f8da88d7
SG
49void generate_exception(DisasContext *ctx, int excp)
50{
51 tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next);
52 gen_helper_raise_exception(cpu_env, tcg_constant_i32(excp));
53 ctx->base.is_jmp = DISAS_NORETURN;
54}
55
56static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
57{
58 if (translator_use_goto_tb(&ctx->base, dest)) {
59 tcg_gen_goto_tb(n);
60 tcg_gen_movi_tl(cpu_pc, dest);
61 tcg_gen_exit_tb(ctx->base.tb, n);
62 } else {
63 tcg_gen_movi_tl(cpu_pc, dest);
64 tcg_gen_lookup_and_goto_ptr();
65 }
66}
67
68static void loongarch_tr_init_disas_context(DisasContextBase *dcbase,
69 CPUState *cs)
70{
71 int64_t bound;
72 DisasContext *ctx = container_of(dcbase, DisasContext, base);
73
74 ctx->page_start = ctx->base.pc_first & TARGET_PAGE_MASK;
75 ctx->mem_idx = ctx->base.tb->flags;
76
77 /* Bound the number of insns to execute to those left on the page. */
78 bound = -(ctx->base.pc_first | TARGET_PAGE_MASK) / 4;
79 ctx->base.max_insns = MIN(ctx->base.max_insns, bound);
143d6785
SG
80
81 ctx->ntemp = 0;
82 memset(ctx->temp, 0, sizeof(ctx->temp));
83
84 ctx->zero = tcg_constant_tl(0);
f8da88d7
SG
85}
86
87static void loongarch_tr_tb_start(DisasContextBase *dcbase, CPUState *cs)
88{
89}
90
91static void loongarch_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
92{
93 DisasContext *ctx = container_of(dcbase, DisasContext, base);
94
95 tcg_gen_insn_start(ctx->base.pc_next);
96}
97
143d6785
SG
98/*
99 * Wrappers for getting reg values.
100 *
101 * The $zero register does not have cpu_gpr[0] allocated -- we supply the
102 * constant zero as a source, and an uninitialized sink as destination.
103 *
104 * Further, we may provide an extension for word operations.
105 */
106static TCGv temp_new(DisasContext *ctx)
107{
108 assert(ctx->ntemp < ARRAY_SIZE(ctx->temp));
109 return ctx->temp[ctx->ntemp++] = tcg_temp_new();
110}
111
112static TCGv gpr_src(DisasContext *ctx, int reg_num, DisasExtend src_ext)
113{
114 TCGv t;
115
116 if (reg_num == 0) {
117 return ctx->zero;
118 }
119
120 switch (src_ext) {
121 case EXT_NONE:
122 return cpu_gpr[reg_num];
123 case EXT_SIGN:
124 t = temp_new(ctx);
125 tcg_gen_ext32s_tl(t, cpu_gpr[reg_num]);
126 return t;
127 case EXT_ZERO:
128 t = temp_new(ctx);
129 tcg_gen_ext32u_tl(t, cpu_gpr[reg_num]);
130 return t;
131 }
132 g_assert_not_reached();
133}
134
135static TCGv gpr_dst(DisasContext *ctx, int reg_num, DisasExtend dst_ext)
136{
137 if (reg_num == 0 || dst_ext) {
138 return temp_new(ctx);
139 }
140 return cpu_gpr[reg_num];
141}
142
143static void gen_set_gpr(int reg_num, TCGv t, DisasExtend dst_ext)
144{
145 if (reg_num != 0) {
146 switch (dst_ext) {
147 case EXT_NONE:
148 tcg_gen_mov_tl(cpu_gpr[reg_num], t);
149 break;
150 case EXT_SIGN:
151 tcg_gen_ext32s_tl(cpu_gpr[reg_num], t);
152 break;
153 case EXT_ZERO:
154 tcg_gen_ext32u_tl(cpu_gpr[reg_num], t);
155 break;
156 default:
157 g_assert_not_reached();
158 }
159 }
160}
161
162#include "decode-insns.c.inc"
163#include "insn_trans/trans_arith.c.inc"
63cfcd47 164#include "insn_trans/trans_shift.c.inc"
ad08cb3f 165#include "insn_trans/trans_bit.c.inc"
bb79174d 166#include "insn_trans/trans_memory.c.inc"
94b02d57 167#include "insn_trans/trans_atomic.c.inc"
8708a04a 168#include "insn_trans/trans_extra.c.inc"
d578ca6c 169#include "insn_trans/trans_farith.c.inc"
143d6785 170
f8da88d7
SG
171static void loongarch_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
172{
173 CPULoongArchState *env = cs->env_ptr;
174 DisasContext *ctx = container_of(dcbase, DisasContext, base);
175
176 ctx->opcode = cpu_ldl_code(env, ctx->base.pc_next);
177
178 if (!decode(ctx, ctx->opcode)) {
179 qemu_log_mask(LOG_UNIMP, "Error: unknown opcode. "
180 TARGET_FMT_lx ": 0x%x\n",
181 ctx->base.pc_next, ctx->opcode);
182 generate_exception(ctx, EXCCODE_INE);
183 }
184
143d6785
SG
185 for (int i = ctx->ntemp - 1; i >= 0; --i) {
186 tcg_temp_free(ctx->temp[i]);
187 ctx->temp[i] = NULL;
188 }
189 ctx->ntemp = 0;
190
f8da88d7
SG
191 ctx->base.pc_next += 4;
192}
193
194static void loongarch_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs)
195{
196 DisasContext *ctx = container_of(dcbase, DisasContext, base);
197
198 switch (ctx->base.is_jmp) {
199 case DISAS_STOP:
200 tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next);
201 tcg_gen_lookup_and_goto_ptr();
202 break;
203 case DISAS_TOO_MANY:
204 gen_goto_tb(ctx, 0, ctx->base.pc_next);
205 break;
206 case DISAS_NORETURN:
207 break;
208 default:
209 g_assert_not_reached();
210 }
211}
212
213static void loongarch_tr_disas_log(const DisasContextBase *dcbase,
214 CPUState *cpu, FILE *logfile)
215{
216 qemu_log("IN: %s\n", lookup_symbol(dcbase->pc_first));
217 target_disas(logfile, cpu, dcbase->pc_first, dcbase->tb->size);
218}
219
220static const TranslatorOps loongarch_tr_ops = {
221 .init_disas_context = loongarch_tr_init_disas_context,
222 .tb_start = loongarch_tr_tb_start,
223 .insn_start = loongarch_tr_insn_start,
224 .translate_insn = loongarch_tr_translate_insn,
225 .tb_stop = loongarch_tr_tb_stop,
226 .disas_log = loongarch_tr_disas_log,
227};
228
229void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
230{
231 DisasContext ctx;
232
233 translator_loop(&loongarch_tr_ops, &ctx.base, cs, tb, max_insns);
234}
235
236void loongarch_translate_init(void)
237{
238 int i;
239
240 cpu_gpr[0] = NULL;
241 for (i = 1; i < 32; i++) {
242 cpu_gpr[i] = tcg_global_mem_new(cpu_env,
243 offsetof(CPULoongArchState, gpr[i]),
244 regnames[i]);
245 }
246
247 for (i = 0; i < 32; i++) {
248 int off = offsetof(CPULoongArchState, fpr[i]);
249 cpu_fpr[i] = tcg_global_mem_new_i64(cpu_env, off, fregnames[i]);
250 }
251
252 cpu_pc = tcg_global_mem_new(cpu_env, offsetof(CPULoongArchState, pc), "pc");
253 cpu_fcsr0 = tcg_global_mem_new_i32(cpu_env,
254 offsetof(CPULoongArchState, fcsr0), "fcsr0");
255 cpu_lladdr = tcg_global_mem_new(cpu_env,
256 offsetof(CPULoongArchState, lladdr), "lladdr");
257 cpu_llval = tcg_global_mem_new(cpu_env,
258 offsetof(CPULoongArchState, llval), "llval");
259}
260
261void restore_state_to_opc(CPULoongArchState *env, TranslationBlock *tb,
262 target_ulong *data)
263{
264 env->pc = data[0];
265}
This page took 0.050282 seconds and 4 git commands to generate.