blob: 678e41a57892848809c3240439d3b414d5888866 [file] [log] [blame]
Thiemo Seufere30ec452008-01-28 20:05:38 +00001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * A small micro-assembler. It is intentionally kept simple, does only
7 * support a subset of instructions, and does not try to hide pipeline
8 * effects like branch delay slots.
9 *
Ralf Baechle70342282013-01-22 12:59:30 +010010 * Copyright (C) 2004, 2005, 2006, 2008 Thiemo Seufer
Thiemo Seufere30ec452008-01-28 20:05:38 +000011 * Copyright (C) 2005, 2007 Maciej W. Rozycki
12 * Copyright (C) 2006 Ralf Baechle (ralf@linux-mips.org)
Steven J. Hillabc597f2013-02-05 16:52:01 -060013 * Copyright (C) 2012, 2013 MIPS Technologies, Inc. All rights reserved.
Thiemo Seufere30ec452008-01-28 20:05:38 +000014 */
15
Thiemo Seufere30ec452008-01-28 20:05:38 +000016enum fields {
17 RS = 0x001,
18 RT = 0x002,
19 RD = 0x004,
20 RE = 0x008,
21 SIMM = 0x010,
22 UIMM = 0x020,
23 BIMM = 0x040,
24 JIMM = 0x080,
25 FUNC = 0x100,
David Daney58b9e222010-02-18 16:13:03 -080026 SET = 0x200,
27 SCIMM = 0x400
Thiemo Seufere30ec452008-01-28 20:05:38 +000028};
29
30#define OP_MASK 0x3f
31#define OP_SH 26
Thiemo Seufere30ec452008-01-28 20:05:38 +000032#define RD_MASK 0x1f
33#define RD_SH 11
34#define RE_MASK 0x1f
35#define RE_SH 6
36#define IMM_MASK 0xffff
37#define IMM_SH 0
38#define JIMM_MASK 0x3ffffff
39#define JIMM_SH 0
40#define FUNC_MASK 0x3f
41#define FUNC_SH 0
42#define SET_MASK 0x7
43#define SET_SH 0
44
45enum opcode {
46 insn_invalid,
Steven J. Hill71a1c772012-06-19 19:59:29 +010047 insn_addiu, insn_addu, insn_and, insn_andi, insn_bbit0, insn_bbit1,
48 insn_beq, insn_beql, insn_bgez, insn_bgezl, insn_bltz, insn_bltzl,
49 insn_bne, insn_cache, insn_daddiu, insn_daddu, insn_dins, insn_dinsm,
Markos Chandras4c12a852014-04-08 12:47:06 +010050 insn_divu, insn_dmfc0, insn_dmtc0, insn_drotr, insn_drotr32, insn_dsll,
Steven J. Hill71a1c772012-06-19 19:59:29 +010051 insn_dsll32, insn_dsra, insn_dsrl, insn_dsrl32, insn_dsubu, insn_eret,
Paul Burton49e9529b2014-03-16 12:58:05 +000052 insn_ext, insn_ins, insn_j, insn_jal, insn_jalr, insn_jr, insn_ld,
53 insn_ldx, insn_ll, insn_lld, insn_lui, insn_lw, insn_lwx, insn_mfc0,
Markos Chandrasf3ec7a22014-04-08 12:47:07 +010054 insn_mfhi, insn_mtc0, insn_or, insn_ori, insn_pref, insn_rfe,
Markos Chandras390363e2014-04-08 12:47:09 +010055 insn_rotr, insn_sc, insn_scd, insn_sd, insn_sll, insn_sllv, insn_sltiu,
Markos Chandrase8ef8682014-04-08 12:47:10 +010056 insn_sltu, insn_sra, insn_srl, insn_srlv, insn_subu, insn_sw, insn_sync,
Markos Chandras390363e2014-04-08 12:47:09 +010057 insn_syscall, insn_tlbp, insn_tlbr, insn_tlbwi, insn_tlbwr, insn_wait,
Markos Chandrasab9e4fa2014-04-08 12:47:11 +010058 insn_wsbh, insn_xor, insn_xori, insn_yield,
Thiemo Seufere30ec452008-01-28 20:05:38 +000059};
60
61struct insn {
62 enum opcode opcode;
63 u32 match;
64 enum fields fields;
65};
66
Paul Gortmaker078a55f2013-06-18 13:38:59 +000067static inline u32 build_rs(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +000068{
David Daney8d662c82010-12-27 18:18:29 -080069 WARN(arg & ~RS_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +000070
71 return (arg & RS_MASK) << RS_SH;
72}
73
Paul Gortmaker078a55f2013-06-18 13:38:59 +000074static inline u32 build_rt(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +000075{
David Daney8d662c82010-12-27 18:18:29 -080076 WARN(arg & ~RT_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +000077
78 return (arg & RT_MASK) << RT_SH;
79}
80
Paul Gortmaker078a55f2013-06-18 13:38:59 +000081static inline u32 build_rd(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +000082{
David Daney8d662c82010-12-27 18:18:29 -080083 WARN(arg & ~RD_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +000084
85 return (arg & RD_MASK) << RD_SH;
86}
87
Paul Gortmaker078a55f2013-06-18 13:38:59 +000088static inline u32 build_re(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +000089{
David Daney8d662c82010-12-27 18:18:29 -080090 WARN(arg & ~RE_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +000091
92 return (arg & RE_MASK) << RE_SH;
93}
94
Paul Gortmaker078a55f2013-06-18 13:38:59 +000095static inline u32 build_simm(s32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +000096{
David Daney8d662c82010-12-27 18:18:29 -080097 WARN(arg > 0x7fff || arg < -0x8000,
98 KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +000099
100 return arg & 0xffff;
101}
102
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000103static inline u32 build_uimm(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000104{
David Daney8d662c82010-12-27 18:18:29 -0800105 WARN(arg & ~IMM_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +0000106
107 return arg & IMM_MASK;
108}
109
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000110static inline u32 build_scimm(u32 arg)
David Daney58b9e222010-02-18 16:13:03 -0800111{
David Daney8d662c82010-12-27 18:18:29 -0800112 WARN(arg & ~SCIMM_MASK,
113 KERN_WARNING "Micro-assembler field overflow\n");
David Daney58b9e222010-02-18 16:13:03 -0800114
115 return (arg & SCIMM_MASK) << SCIMM_SH;
116}
117
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000118static inline u32 build_func(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000119{
David Daney8d662c82010-12-27 18:18:29 -0800120 WARN(arg & ~FUNC_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +0000121
122 return arg & FUNC_MASK;
123}
124
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000125static inline u32 build_set(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000126{
David Daney8d662c82010-12-27 18:18:29 -0800127 WARN(arg & ~SET_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +0000128
129 return arg & SET_MASK;
130}
131
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000132static void build_insn(u32 **buf, enum opcode opc, ...);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000133
134#define I_u1u2u3(op) \
135Ip_u1u2u3(op) \
136{ \
137 build_insn(buf, insn##op, a, b, c); \
David Daney22b07632010-07-23 18:41:43 -0700138} \
139UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000140
141#define I_u2u1u3(op) \
142Ip_u2u1u3(op) \
143{ \
144 build_insn(buf, insn##op, b, a, c); \
David Daney22b07632010-07-23 18:41:43 -0700145} \
146UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000147
Markos Chandrasbeef8e02014-04-08 12:47:02 +0100148#define I_u3u2u1(op) \
149Ip_u3u2u1(op) \
150{ \
151 build_insn(buf, insn##op, c, b, a); \
152} \
153UASM_EXPORT_SYMBOL(uasm_i##op);
154
Thiemo Seufere30ec452008-01-28 20:05:38 +0000155#define I_u3u1u2(op) \
156Ip_u3u1u2(op) \
157{ \
158 build_insn(buf, insn##op, b, c, a); \
David Daney22b07632010-07-23 18:41:43 -0700159} \
160UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000161
162#define I_u1u2s3(op) \
163Ip_u1u2s3(op) \
164{ \
165 build_insn(buf, insn##op, a, b, c); \
David Daney22b07632010-07-23 18:41:43 -0700166} \
167UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000168
169#define I_u2s3u1(op) \
170Ip_u2s3u1(op) \
171{ \
172 build_insn(buf, insn##op, c, a, b); \
David Daney22b07632010-07-23 18:41:43 -0700173} \
174UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000175
176#define I_u2u1s3(op) \
177Ip_u2u1s3(op) \
178{ \
179 build_insn(buf, insn##op, b, a, c); \
David Daney22b07632010-07-23 18:41:43 -0700180} \
181UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000182
David Daney92078e02009-10-14 12:16:55 -0700183#define I_u2u1msbu3(op) \
184Ip_u2u1msbu3(op) \
185{ \
186 build_insn(buf, insn##op, b, a, c+d-1, c); \
David Daney22b07632010-07-23 18:41:43 -0700187} \
188UASM_EXPORT_SYMBOL(uasm_i##op);
David Daney92078e02009-10-14 12:16:55 -0700189
David Daneyc42aef02010-12-21 14:19:10 -0800190#define I_u2u1msb32u3(op) \
191Ip_u2u1msbu3(op) \
192{ \
193 build_insn(buf, insn##op, b, a, c+d-33, c); \
194} \
195UASM_EXPORT_SYMBOL(uasm_i##op);
196
Ralf Baechle70342282013-01-22 12:59:30 +0100197#define I_u2u1msbdu3(op) \
Steven J. Hille6de1a02012-07-12 17:21:31 +0000198Ip_u2u1msbu3(op) \
199{ \
200 build_insn(buf, insn##op, b, a, d-1, c); \
201} \
202UASM_EXPORT_SYMBOL(uasm_i##op);
203
Thiemo Seufere30ec452008-01-28 20:05:38 +0000204#define I_u1u2(op) \
205Ip_u1u2(op) \
206{ \
207 build_insn(buf, insn##op, a, b); \
David Daney22b07632010-07-23 18:41:43 -0700208} \
209UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000210
Paul Burtond674dd12014-03-04 15:12:36 +0000211#define I_u2u1(op) \
212Ip_u1u2(op) \
213{ \
214 build_insn(buf, insn##op, b, a); \
215} \
216UASM_EXPORT_SYMBOL(uasm_i##op);
217
Thiemo Seufere30ec452008-01-28 20:05:38 +0000218#define I_u1s2(op) \
219Ip_u1s2(op) \
220{ \
221 build_insn(buf, insn##op, a, b); \
David Daney22b07632010-07-23 18:41:43 -0700222} \
223UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000224
225#define I_u1(op) \
226Ip_u1(op) \
227{ \
228 build_insn(buf, insn##op, a); \
David Daney22b07632010-07-23 18:41:43 -0700229} \
230UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000231
232#define I_0(op) \
233Ip_0(op) \
234{ \
235 build_insn(buf, insn##op); \
David Daney22b07632010-07-23 18:41:43 -0700236} \
237UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000238
239I_u2u1s3(_addiu)
240I_u3u1u2(_addu)
241I_u2u1u3(_andi)
242I_u3u1u2(_and)
243I_u1u2s3(_beq)
244I_u1u2s3(_beql)
245I_u1s2(_bgez)
246I_u1s2(_bgezl)
247I_u1s2(_bltz)
248I_u1s2(_bltzl)
249I_u1u2s3(_bne)
Thiemo Seuferfb2a27e72008-02-18 19:32:49 +0000250I_u2s3u1(_cache)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000251I_u1u2u3(_dmfc0)
252I_u1u2u3(_dmtc0)
253I_u2u1s3(_daddiu)
254I_u3u1u2(_daddu)
Markos Chandras4c12a852014-04-08 12:47:06 +0100255I_u1u2(_divu)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000256I_u2u1u3(_dsll)
257I_u2u1u3(_dsll32)
258I_u2u1u3(_dsra)
259I_u2u1u3(_dsrl)
260I_u2u1u3(_dsrl32)
David Daney92078e02009-10-14 12:16:55 -0700261I_u2u1u3(_drotr)
David Daneyde6d5b552010-07-23 18:41:41 -0700262I_u2u1u3(_drotr32)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000263I_u3u1u2(_dsubu)
264I_0(_eret)
Steven J. Hille6de1a02012-07-12 17:21:31 +0000265I_u2u1msbdu3(_ext)
266I_u2u1msbu3(_ins)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000267I_u1(_j)
268I_u1(_jal)
Paul Burton49e9529b2014-03-16 12:58:05 +0000269I_u2u1(_jalr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000270I_u1(_jr)
271I_u2s3u1(_ld)
272I_u2s3u1(_ll)
273I_u2s3u1(_lld)
274I_u1s2(_lui)
275I_u2s3u1(_lw)
276I_u1u2u3(_mfc0)
Markos Chandrasf3ec7a22014-04-08 12:47:07 +0100277I_u1(_mfhi)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000278I_u1u2u3(_mtc0)
279I_u2u1u3(_ori)
Ralf Baechle58081842010-03-23 15:54:50 +0100280I_u3u1u2(_or)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000281I_0(_rfe)
282I_u2s3u1(_sc)
283I_u2s3u1(_scd)
284I_u2s3u1(_sd)
285I_u2u1u3(_sll)
Markos Chandrasbef581b2014-04-08 12:47:04 +0100286I_u3u2u1(_sllv)
Markos Chandras390363e2014-04-08 12:47:09 +0100287I_u2u1s3(_sltiu)
Markos Chandrase8ef8682014-04-08 12:47:10 +0100288I_u3u1u2(_sltu)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000289I_u2u1u3(_sra)
290I_u2u1u3(_srl)
Markos Chandrasf31318f2014-04-08 12:47:05 +0100291I_u3u2u1(_srlv)
David Daney32546f32010-02-10 15:12:46 -0800292I_u2u1u3(_rotr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000293I_u3u1u2(_subu)
294I_u2s3u1(_sw)
Paul Burton729ff562013-12-24 03:49:45 +0000295I_u1(_sync)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000296I_0(_tlbp)
David Daney32546f32010-02-10 15:12:46 -0800297I_0(_tlbr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000298I_0(_tlbwi)
299I_0(_tlbwr)
Paul Burton53ed1382013-12-24 03:50:35 +0000300I_u1(_wait);
Markos Chandrasab9e4fa2014-04-08 12:47:11 +0100301I_u2u1(_wsbh)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000302I_u3u1u2(_xor)
303I_u2u1u3(_xori)
Paul Burtond674dd12014-03-04 15:12:36 +0000304I_u2u1(_yield)
David Daney92078e02009-10-14 12:16:55 -0700305I_u2u1msbu3(_dins);
David Daneyc42aef02010-12-21 14:19:10 -0800306I_u2u1msb32u3(_dinsm);
David Daney58b9e222010-02-18 16:13:03 -0800307I_u1(_syscall);
David Daney5b97c3f2010-07-23 18:41:42 -0700308I_u1u2s3(_bbit0);
309I_u1u2s3(_bbit1);
David Daneybb3d68c2010-12-27 18:07:56 -0800310I_u3u1u2(_lwx)
311I_u3u1u2(_ldx)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000312
David Daneyc9941152010-10-07 16:03:53 -0700313#ifdef CONFIG_CPU_CAVIUM_OCTEON
314#include <asm/octeon/octeon.h>
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000315void ISAFUNC(uasm_i_pref)(u32 **buf, unsigned int a, signed int b,
David Daneyc9941152010-10-07 16:03:53 -0700316 unsigned int c)
317{
318 if (OCTEON_IS_MODEL(OCTEON_CN63XX_PASS1_X) && a <= 24 && a != 5)
319 /*
320 * As per erratum Core-14449, replace prefetches 0-4,
321 * 6-24 with 'pref 28'.
322 */
323 build_insn(buf, insn_pref, c, 28, b);
324 else
325 build_insn(buf, insn_pref, c, a, b);
326}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600327UASM_EXPORT_SYMBOL(ISAFUNC(uasm_i_pref));
David Daneyc9941152010-10-07 16:03:53 -0700328#else
329I_u2s3u1(_pref)
330#endif
331
Thiemo Seufere30ec452008-01-28 20:05:38 +0000332/* Handle labels. */
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000333void ISAFUNC(uasm_build_label)(struct uasm_label **lab, u32 *addr, int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000334{
335 (*lab)->addr = addr;
336 (*lab)->lab = lid;
337 (*lab)++;
338}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600339UASM_EXPORT_SYMBOL(ISAFUNC(uasm_build_label));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000340
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000341int ISAFUNC(uasm_in_compat_space_p)(long addr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000342{
343 /* Is this address in 32bit compat space? */
344#ifdef CONFIG_64BIT
345 return (((addr) & 0xffffffff00000000L) == 0xffffffff00000000L);
346#else
347 return 1;
348#endif
349}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600350UASM_EXPORT_SYMBOL(ISAFUNC(uasm_in_compat_space_p));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000351
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000352static int uasm_rel_highest(long val)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000353{
354#ifdef CONFIG_64BIT
355 return ((((val + 0x800080008000L) >> 48) & 0xffff) ^ 0x8000) - 0x8000;
356#else
357 return 0;
358#endif
359}
360
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000361static int uasm_rel_higher(long val)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000362{
363#ifdef CONFIG_64BIT
364 return ((((val + 0x80008000L) >> 32) & 0xffff) ^ 0x8000) - 0x8000;
365#else
366 return 0;
367#endif
368}
369
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000370int ISAFUNC(uasm_rel_hi)(long val)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000371{
372 return ((((val + 0x8000L) >> 16) & 0xffff) ^ 0x8000) - 0x8000;
373}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600374UASM_EXPORT_SYMBOL(ISAFUNC(uasm_rel_hi));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000375
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000376int ISAFUNC(uasm_rel_lo)(long val)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000377{
378 return ((val & 0xffff) ^ 0x8000) - 0x8000;
379}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600380UASM_EXPORT_SYMBOL(ISAFUNC(uasm_rel_lo));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000381
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000382void ISAFUNC(UASM_i_LA_mostly)(u32 **buf, unsigned int rs, long addr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000383{
Steven J. Hillabc597f2013-02-05 16:52:01 -0600384 if (!ISAFUNC(uasm_in_compat_space_p)(addr)) {
385 ISAFUNC(uasm_i_lui)(buf, rs, uasm_rel_highest(addr));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000386 if (uasm_rel_higher(addr))
Steven J. Hillabc597f2013-02-05 16:52:01 -0600387 ISAFUNC(uasm_i_daddiu)(buf, rs, rs, uasm_rel_higher(addr));
388 if (ISAFUNC(uasm_rel_hi(addr))) {
389 ISAFUNC(uasm_i_dsll)(buf, rs, rs, 16);
390 ISAFUNC(uasm_i_daddiu)(buf, rs, rs,
391 ISAFUNC(uasm_rel_hi)(addr));
392 ISAFUNC(uasm_i_dsll)(buf, rs, rs, 16);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000393 } else
Steven J. Hillabc597f2013-02-05 16:52:01 -0600394 ISAFUNC(uasm_i_dsll32)(buf, rs, rs, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000395 } else
Steven J. Hillabc597f2013-02-05 16:52:01 -0600396 ISAFUNC(uasm_i_lui)(buf, rs, ISAFUNC(uasm_rel_hi(addr)));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000397}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600398UASM_EXPORT_SYMBOL(ISAFUNC(UASM_i_LA_mostly));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000399
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000400void ISAFUNC(UASM_i_LA)(u32 **buf, unsigned int rs, long addr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000401{
Steven J. Hillabc597f2013-02-05 16:52:01 -0600402 ISAFUNC(UASM_i_LA_mostly)(buf, rs, addr);
403 if (ISAFUNC(uasm_rel_lo(addr))) {
404 if (!ISAFUNC(uasm_in_compat_space_p)(addr))
405 ISAFUNC(uasm_i_daddiu)(buf, rs, rs,
406 ISAFUNC(uasm_rel_lo(addr)));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000407 else
Steven J. Hillabc597f2013-02-05 16:52:01 -0600408 ISAFUNC(uasm_i_addiu)(buf, rs, rs,
409 ISAFUNC(uasm_rel_lo(addr)));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000410 }
411}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600412UASM_EXPORT_SYMBOL(ISAFUNC(UASM_i_LA));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000413
414/* Handle relocations. */
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000415void ISAFUNC(uasm_r_mips_pc16)(struct uasm_reloc **rel, u32 *addr, int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000416{
417 (*rel)->addr = addr;
418 (*rel)->type = R_MIPS_PC16;
419 (*rel)->lab = lid;
420 (*rel)++;
421}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600422UASM_EXPORT_SYMBOL(ISAFUNC(uasm_r_mips_pc16));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000423
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000424static inline void __resolve_relocs(struct uasm_reloc *rel,
425 struct uasm_label *lab);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000426
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000427void ISAFUNC(uasm_resolve_relocs)(struct uasm_reloc *rel,
428 struct uasm_label *lab)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000429{
430 struct uasm_label *l;
431
432 for (; rel->lab != UASM_LABEL_INVALID; rel++)
433 for (l = lab; l->lab != UASM_LABEL_INVALID; l++)
434 if (rel->lab == l->lab)
435 __resolve_relocs(rel, l);
436}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600437UASM_EXPORT_SYMBOL(ISAFUNC(uasm_resolve_relocs));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000438
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000439void ISAFUNC(uasm_move_relocs)(struct uasm_reloc *rel, u32 *first, u32 *end,
440 long off)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000441{
442 for (; rel->lab != UASM_LABEL_INVALID; rel++)
443 if (rel->addr >= first && rel->addr < end)
444 rel->addr += off;
445}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600446UASM_EXPORT_SYMBOL(ISAFUNC(uasm_move_relocs));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000447
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000448void ISAFUNC(uasm_move_labels)(struct uasm_label *lab, u32 *first, u32 *end,
449 long off)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000450{
451 for (; lab->lab != UASM_LABEL_INVALID; lab++)
452 if (lab->addr >= first && lab->addr < end)
453 lab->addr += off;
454}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600455UASM_EXPORT_SYMBOL(ISAFUNC(uasm_move_labels));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000456
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000457void ISAFUNC(uasm_copy_handler)(struct uasm_reloc *rel, struct uasm_label *lab,
458 u32 *first, u32 *end, u32 *target)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000459{
460 long off = (long)(target - first);
461
462 memcpy(target, first, (end - first) * sizeof(u32));
463
Steven J. Hillabc597f2013-02-05 16:52:01 -0600464 ISAFUNC(uasm_move_relocs(rel, first, end, off));
465 ISAFUNC(uasm_move_labels(lab, first, end, off));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000466}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600467UASM_EXPORT_SYMBOL(ISAFUNC(uasm_copy_handler));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000468
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000469int ISAFUNC(uasm_insn_has_bdelay)(struct uasm_reloc *rel, u32 *addr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000470{
471 for (; rel->lab != UASM_LABEL_INVALID; rel++) {
472 if (rel->addr == addr
473 && (rel->type == R_MIPS_PC16
474 || rel->type == R_MIPS_26))
475 return 1;
476 }
477
478 return 0;
479}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600480UASM_EXPORT_SYMBOL(ISAFUNC(uasm_insn_has_bdelay));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000481
482/* Convenience functions for labeled branches. */
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000483void ISAFUNC(uasm_il_bltz)(u32 **p, struct uasm_reloc **r, unsigned int reg,
484 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000485{
486 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600487 ISAFUNC(uasm_i_bltz)(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000488}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600489UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_bltz));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000490
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000491void ISAFUNC(uasm_il_b)(u32 **p, struct uasm_reloc **r, int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000492{
493 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600494 ISAFUNC(uasm_i_b)(p, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000495}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600496UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_b));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000497
Paul Burton8dee5902013-12-24 03:51:39 +0000498void ISAFUNC(uasm_il_beq)(u32 **p, struct uasm_reloc **r, unsigned int r1,
499 unsigned int r2, int lid)
500{
501 uasm_r_mips_pc16(r, *p, lid);
502 ISAFUNC(uasm_i_beq)(p, r1, r2, 0);
503}
504UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_beq));
505
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000506void ISAFUNC(uasm_il_beqz)(u32 **p, struct uasm_reloc **r, unsigned int reg,
507 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000508{
509 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600510 ISAFUNC(uasm_i_beqz)(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000511}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600512UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_beqz));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000513
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000514void ISAFUNC(uasm_il_beqzl)(u32 **p, struct uasm_reloc **r, unsigned int reg,
515 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000516{
517 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600518 ISAFUNC(uasm_i_beqzl)(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000519}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600520UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_beqzl));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000521
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000522void ISAFUNC(uasm_il_bne)(u32 **p, struct uasm_reloc **r, unsigned int reg1,
523 unsigned int reg2, int lid)
Thiemo Seuferfb2a27e72008-02-18 19:32:49 +0000524{
525 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600526 ISAFUNC(uasm_i_bne)(p, reg1, reg2, 0);
Thiemo Seuferfb2a27e72008-02-18 19:32:49 +0000527}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600528UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_bne));
Thiemo Seuferfb2a27e72008-02-18 19:32:49 +0000529
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000530void ISAFUNC(uasm_il_bnez)(u32 **p, struct uasm_reloc **r, unsigned int reg,
531 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000532{
533 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600534 ISAFUNC(uasm_i_bnez)(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000535}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600536UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_bnez));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000537
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000538void ISAFUNC(uasm_il_bgezl)(u32 **p, struct uasm_reloc **r, unsigned int reg,
539 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000540{
541 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600542 ISAFUNC(uasm_i_bgezl)(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000543}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600544UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_bgezl));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000545
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000546void ISAFUNC(uasm_il_bgez)(u32 **p, struct uasm_reloc **r, unsigned int reg,
547 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000548{
549 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600550 ISAFUNC(uasm_i_bgez)(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000551}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600552UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_bgez));
David Daney5b97c3f2010-07-23 18:41:42 -0700553
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000554void ISAFUNC(uasm_il_bbit0)(u32 **p, struct uasm_reloc **r, unsigned int reg,
555 unsigned int bit, int lid)
David Daney5b97c3f2010-07-23 18:41:42 -0700556{
557 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600558 ISAFUNC(uasm_i_bbit0)(p, reg, bit, 0);
David Daney5b97c3f2010-07-23 18:41:42 -0700559}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600560UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_bbit0));
David Daney5b97c3f2010-07-23 18:41:42 -0700561
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000562void ISAFUNC(uasm_il_bbit1)(u32 **p, struct uasm_reloc **r, unsigned int reg,
563 unsigned int bit, int lid)
David Daney5b97c3f2010-07-23 18:41:42 -0700564{
565 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600566 ISAFUNC(uasm_i_bbit1)(p, reg, bit, 0);
David Daney5b97c3f2010-07-23 18:41:42 -0700567}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600568UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_bbit1));