Skip to content

Commit 3200983

Browse files
Youling Tangchenhuacai
authored andcommitted
LoongArch: Simplify larch_insn_gen_xxx implementation
Simplify larch_insn_gen_xxx implementation by reusing emit_xxx. Signed-off-by: Youling Tang <[email protected]> Signed-off-by: Huacai Chen <[email protected]>
1 parent 2959fce commit 3200983

File tree

2 files changed

+8
-38
lines changed

2 files changed

+8
-38
lines changed

arch/loongarch/include/asm/inst.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ static inline void emit_##NAME(union loongarch_instruction *insn, \
393393
}
394394

395395
DEF_EMIT_REG0I26_FORMAT(b, b_op)
396+
DEF_EMIT_REG0I26_FORMAT(bl, bl_op)
396397

397398
#define DEF_EMIT_REG1I20_FORMAT(NAME, OP) \
398399
static inline void emit_##NAME(union loongarch_instruction *insn, \

arch/loongarch/kernel/inst.c

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -58,47 +58,29 @@ u32 larch_insn_gen_nop(void)
5858
u32 larch_insn_gen_b(unsigned long pc, unsigned long dest)
5959
{
6060
long offset = dest - pc;
61-
unsigned int immediate_l, immediate_h;
6261
union loongarch_instruction insn;
6362

6463
if ((offset & 3) || offset < -SZ_128M || offset >= SZ_128M) {
6564
pr_warn("The generated b instruction is out of range.\n");
6665
return INSN_BREAK;
6766
}
6867

69-
offset >>= 2;
70-
71-
immediate_l = offset & 0xffff;
72-
offset >>= 16;
73-
immediate_h = offset & 0x3ff;
74-
75-
insn.reg0i26_format.opcode = b_op;
76-
insn.reg0i26_format.immediate_l = immediate_l;
77-
insn.reg0i26_format.immediate_h = immediate_h;
68+
emit_b(&insn, offset >> 2);
7869

7970
return insn.word;
8071
}
8172

8273
u32 larch_insn_gen_bl(unsigned long pc, unsigned long dest)
8374
{
8475
long offset = dest - pc;
85-
unsigned int immediate_l, immediate_h;
8676
union loongarch_instruction insn;
8777

8878
if ((offset & 3) || offset < -SZ_128M || offset >= SZ_128M) {
8979
pr_warn("The generated bl instruction is out of range.\n");
9080
return INSN_BREAK;
9181
}
9282

93-
offset >>= 2;
94-
95-
immediate_l = offset & 0xffff;
96-
offset >>= 16;
97-
immediate_h = offset & 0x3ff;
98-
99-
insn.reg0i26_format.opcode = bl_op;
100-
insn.reg0i26_format.immediate_l = immediate_l;
101-
insn.reg0i26_format.immediate_h = immediate_h;
83+
emit_bl(&insn, offset >> 2);
10284

10385
return insn.word;
10486
}
@@ -107,10 +89,7 @@ u32 larch_insn_gen_or(enum loongarch_gpr rd, enum loongarch_gpr rj, enum loongar
10789
{
10890
union loongarch_instruction insn;
10991

110-
insn.reg3_format.opcode = or_op;
111-
insn.reg3_format.rd = rd;
112-
insn.reg3_format.rj = rj;
113-
insn.reg3_format.rk = rk;
92+
emit_or(&insn, rd, rj, rk);
11493

11594
return insn.word;
11695
}
@@ -124,9 +103,7 @@ u32 larch_insn_gen_lu12iw(enum loongarch_gpr rd, int imm)
124103
{
125104
union loongarch_instruction insn;
126105

127-
insn.reg1i20_format.opcode = lu12iw_op;
128-
insn.reg1i20_format.rd = rd;
129-
insn.reg1i20_format.immediate = imm;
106+
emit_lu12iw(&insn, rd, imm);
130107

131108
return insn.word;
132109
}
@@ -135,9 +112,7 @@ u32 larch_insn_gen_lu32id(enum loongarch_gpr rd, int imm)
135112
{
136113
union loongarch_instruction insn;
137114

138-
insn.reg1i20_format.opcode = lu32id_op;
139-
insn.reg1i20_format.rd = rd;
140-
insn.reg1i20_format.immediate = imm;
115+
emit_lu32id(&insn, rd, imm);
141116

142117
return insn.word;
143118
}
@@ -146,10 +121,7 @@ u32 larch_insn_gen_lu52id(enum loongarch_gpr rd, enum loongarch_gpr rj, int imm)
146121
{
147122
union loongarch_instruction insn;
148123

149-
insn.reg2i12_format.opcode = lu52id_op;
150-
insn.reg2i12_format.rd = rd;
151-
insn.reg2i12_format.rj = rj;
152-
insn.reg2i12_format.immediate = imm;
124+
emit_lu52id(&insn, rd, rj, imm);
153125

154126
return insn.word;
155127
}
@@ -158,10 +130,7 @@ u32 larch_insn_gen_jirl(enum loongarch_gpr rd, enum loongarch_gpr rj, unsigned l
158130
{
159131
union loongarch_instruction insn;
160132

161-
insn.reg2i16_format.opcode = jirl_op;
162-
insn.reg2i16_format.rd = rd;
163-
insn.reg2i16_format.rj = rj;
164-
insn.reg2i16_format.immediate = (dest - pc) >> 2;
133+
emit_jirl(&insn, rj, rd, (dest - pc) >> 2);
165134

166135
return insn.word;
167136
}

0 commit comments

Comments
 (0)