blob: 4adf30284813a93aa11143ddbfb433831327e329 [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,
Markos Chandras82488812014-04-16 13:49:57 +010052 insn_ext, insn_ins, insn_j, insn_jal, insn_jalr, insn_jr, insn_lb,
53 insn_ld, insn_ldx, insn_lh, insn_ll, insn_lld, insn_lui, insn_lw,
Steven J. Hille2965cd2014-11-13 09:52:02 -060054 insn_lwx, insn_mfc0, insn_mfhc0, insn_mfhi, insn_mflo, insn_mtc0,
55 insn_mthc0, insn_mul, insn_or, insn_ori, insn_pref, insn_rfe,
56 insn_rotr, insn_sc, insn_scd, insn_sd, insn_sll, insn_sllv, insn_slt,
57 insn_sltiu, insn_sltu, insn_sra, insn_srl, insn_srlv, insn_subu,
58 insn_sw, insn_sync, insn_syscall, insn_tlbp, insn_tlbr, insn_tlbwi,
59 insn_tlbwr, insn_wait, insn_wsbh, insn_xor, insn_xori, insn_yield,
Thiemo Seufere30ec452008-01-28 20:05:38 +000060};
61
62struct insn {
63 enum opcode opcode;
64 u32 match;
65 enum fields fields;
66};
67
Paul Gortmaker078a55f2013-06-18 13:38:59 +000068static inline u32 build_rs(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +000069{
David Daney8d662c82010-12-27 18:18:29 -080070 WARN(arg & ~RS_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +000071
72 return (arg & RS_MASK) << RS_SH;
73}
74
Paul Gortmaker078a55f2013-06-18 13:38:59 +000075static inline u32 build_rt(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +000076{
David Daney8d662c82010-12-27 18:18:29 -080077 WARN(arg & ~RT_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +000078
79 return (arg & RT_MASK) << RT_SH;
80}
81
Paul Gortmaker078a55f2013-06-18 13:38:59 +000082static inline u32 build_rd(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +000083{
David Daney8d662c82010-12-27 18:18:29 -080084 WARN(arg & ~RD_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +000085
86 return (arg & RD_MASK) << RD_SH;
87}
88
Paul Gortmaker078a55f2013-06-18 13:38:59 +000089static inline u32 build_re(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +000090{
David Daney8d662c82010-12-27 18:18:29 -080091 WARN(arg & ~RE_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +000092
93 return (arg & RE_MASK) << RE_SH;
94}
95
Paul Gortmaker078a55f2013-06-18 13:38:59 +000096static inline u32 build_simm(s32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +000097{
David Daney8d662c82010-12-27 18:18:29 -080098 WARN(arg > 0x7fff || arg < -0x8000,
99 KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +0000100
101 return arg & 0xffff;
102}
103
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000104static inline u32 build_uimm(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000105{
David Daney8d662c82010-12-27 18:18:29 -0800106 WARN(arg & ~IMM_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +0000107
108 return arg & IMM_MASK;
109}
110
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000111static inline u32 build_scimm(u32 arg)
David Daney58b9e222010-02-18 16:13:03 -0800112{
David Daney8d662c82010-12-27 18:18:29 -0800113 WARN(arg & ~SCIMM_MASK,
114 KERN_WARNING "Micro-assembler field overflow\n");
David Daney58b9e222010-02-18 16:13:03 -0800115
116 return (arg & SCIMM_MASK) << SCIMM_SH;
117}
118
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000119static inline u32 build_func(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000120{
David Daney8d662c82010-12-27 18:18:29 -0800121 WARN(arg & ~FUNC_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +0000122
123 return arg & FUNC_MASK;
124}
125
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000126static inline u32 build_set(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000127{
David Daney8d662c82010-12-27 18:18:29 -0800128 WARN(arg & ~SET_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +0000129
130 return arg & SET_MASK;
131}
132
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000133static void build_insn(u32 **buf, enum opcode opc, ...);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000134
135#define I_u1u2u3(op) \
136Ip_u1u2u3(op) \
137{ \
138 build_insn(buf, insn##op, a, b, c); \
David Daney22b07632010-07-23 18:41:43 -0700139} \
140UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000141
Markos Chandras9d987362014-06-23 10:38:44 +0100142#define I_s3s1s2(op) \
143Ip_s3s1s2(op) \
144{ \
145 build_insn(buf, insn##op, b, c, a); \
146} \
147UASM_EXPORT_SYMBOL(uasm_i##op);
148
Thiemo Seufere30ec452008-01-28 20:05:38 +0000149#define I_u2u1u3(op) \
150Ip_u2u1u3(op) \
151{ \
152 build_insn(buf, insn##op, b, a, c); \
David Daney22b07632010-07-23 18:41:43 -0700153} \
154UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000155
Markos Chandrasbeef8e02014-04-08 12:47:02 +0100156#define I_u3u2u1(op) \
157Ip_u3u2u1(op) \
158{ \
159 build_insn(buf, insn##op, c, b, a); \
160} \
161UASM_EXPORT_SYMBOL(uasm_i##op);
162
Thiemo Seufere30ec452008-01-28 20:05:38 +0000163#define I_u3u1u2(op) \
164Ip_u3u1u2(op) \
165{ \
166 build_insn(buf, insn##op, b, c, a); \
David Daney22b07632010-07-23 18:41:43 -0700167} \
168UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000169
170#define I_u1u2s3(op) \
171Ip_u1u2s3(op) \
172{ \
173 build_insn(buf, insn##op, a, b, c); \
David Daney22b07632010-07-23 18:41:43 -0700174} \
175UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000176
177#define I_u2s3u1(op) \
178Ip_u2s3u1(op) \
179{ \
180 build_insn(buf, insn##op, c, a, b); \
David Daney22b07632010-07-23 18:41:43 -0700181} \
182UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000183
184#define I_u2u1s3(op) \
185Ip_u2u1s3(op) \
186{ \
187 build_insn(buf, insn##op, b, a, c); \
David Daney22b07632010-07-23 18:41:43 -0700188} \
189UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000190
David Daney92078e02009-10-14 12:16:55 -0700191#define I_u2u1msbu3(op) \
192Ip_u2u1msbu3(op) \
193{ \
194 build_insn(buf, insn##op, b, a, c+d-1, c); \
David Daney22b07632010-07-23 18:41:43 -0700195} \
196UASM_EXPORT_SYMBOL(uasm_i##op);
David Daney92078e02009-10-14 12:16:55 -0700197
David Daneyc42aef02010-12-21 14:19:10 -0800198#define I_u2u1msb32u3(op) \
199Ip_u2u1msbu3(op) \
200{ \
201 build_insn(buf, insn##op, b, a, c+d-33, c); \
202} \
203UASM_EXPORT_SYMBOL(uasm_i##op);
204
Ralf Baechle70342282013-01-22 12:59:30 +0100205#define I_u2u1msbdu3(op) \
Steven J. Hille6de1a02012-07-12 17:21:31 +0000206Ip_u2u1msbu3(op) \
207{ \
208 build_insn(buf, insn##op, b, a, d-1, c); \
209} \
210UASM_EXPORT_SYMBOL(uasm_i##op);
211
Thiemo Seufere30ec452008-01-28 20:05:38 +0000212#define I_u1u2(op) \
213Ip_u1u2(op) \
214{ \
215 build_insn(buf, insn##op, a, b); \
David Daney22b07632010-07-23 18:41:43 -0700216} \
217UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000218
Paul Burtond674dd12014-03-04 15:12:36 +0000219#define I_u2u1(op) \
220Ip_u1u2(op) \
221{ \
222 build_insn(buf, insn##op, b, a); \
223} \
224UASM_EXPORT_SYMBOL(uasm_i##op);
225
Thiemo Seufere30ec452008-01-28 20:05:38 +0000226#define I_u1s2(op) \
227Ip_u1s2(op) \
228{ \
229 build_insn(buf, insn##op, a, b); \
David Daney22b07632010-07-23 18:41:43 -0700230} \
231UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000232
233#define I_u1(op) \
234Ip_u1(op) \
235{ \
236 build_insn(buf, insn##op, a); \
David Daney22b07632010-07-23 18:41:43 -0700237} \
238UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000239
240#define I_0(op) \
241Ip_0(op) \
242{ \
243 build_insn(buf, insn##op); \
David Daney22b07632010-07-23 18:41:43 -0700244} \
245UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000246
247I_u2u1s3(_addiu)
248I_u3u1u2(_addu)
249I_u2u1u3(_andi)
250I_u3u1u2(_and)
251I_u1u2s3(_beq)
252I_u1u2s3(_beql)
253I_u1s2(_bgez)
254I_u1s2(_bgezl)
255I_u1s2(_bltz)
256I_u1s2(_bltzl)
257I_u1u2s3(_bne)
Thiemo Seuferfb2a27e72008-02-18 19:32:49 +0000258I_u2s3u1(_cache)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000259I_u1u2u3(_dmfc0)
260I_u1u2u3(_dmtc0)
261I_u2u1s3(_daddiu)
262I_u3u1u2(_daddu)
Markos Chandras4c12a852014-04-08 12:47:06 +0100263I_u1u2(_divu)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000264I_u2u1u3(_dsll)
265I_u2u1u3(_dsll32)
266I_u2u1u3(_dsra)
267I_u2u1u3(_dsrl)
268I_u2u1u3(_dsrl32)
David Daney92078e02009-10-14 12:16:55 -0700269I_u2u1u3(_drotr)
David Daneyde6d5b552010-07-23 18:41:41 -0700270I_u2u1u3(_drotr32)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000271I_u3u1u2(_dsubu)
272I_0(_eret)
Steven J. Hille6de1a02012-07-12 17:21:31 +0000273I_u2u1msbdu3(_ext)
274I_u2u1msbu3(_ins)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000275I_u1(_j)
276I_u1(_jal)
Paul Burton49e9529b2014-03-16 12:58:05 +0000277I_u2u1(_jalr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000278I_u1(_jr)
Markos Chandras82488812014-04-16 13:49:57 +0100279I_u2s3u1(_lb)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000280I_u2s3u1(_ld)
Markos Chandrasd6b33142014-04-08 12:47:12 +0100281I_u2s3u1(_lh)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000282I_u2s3u1(_ll)
283I_u2s3u1(_lld)
284I_u1s2(_lui)
285I_u2s3u1(_lw)
286I_u1u2u3(_mfc0)
Steven J. Hille2965cd2014-11-13 09:52:02 -0600287I_u1u2u3(_mfhc0)
Markos Chandrasf3ec7a22014-04-08 12:47:07 +0100288I_u1(_mfhi)
Markos Chandras16d21a82014-04-14 15:42:31 +0100289I_u1(_mflo)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000290I_u1u2u3(_mtc0)
Steven J. Hille2965cd2014-11-13 09:52:02 -0600291I_u1u2u3(_mthc0)
Markos Chandrasa8e897a2014-04-08 12:47:13 +0100292I_u3u1u2(_mul)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000293I_u2u1u3(_ori)
Ralf Baechle58081842010-03-23 15:54:50 +0100294I_u3u1u2(_or)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000295I_0(_rfe)
296I_u2s3u1(_sc)
297I_u2s3u1(_scd)
298I_u2s3u1(_sd)
299I_u2u1u3(_sll)
Markos Chandrasbef581b2014-04-08 12:47:04 +0100300I_u3u2u1(_sllv)
Markos Chandras7682f9e2014-06-23 10:38:45 +0100301I_s3s1s2(_slt)
Markos Chandras390363e2014-04-08 12:47:09 +0100302I_u2u1s3(_sltiu)
Markos Chandrase8ef8682014-04-08 12:47:10 +0100303I_u3u1u2(_sltu)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000304I_u2u1u3(_sra)
305I_u2u1u3(_srl)
Markos Chandrasf31318f2014-04-08 12:47:05 +0100306I_u3u2u1(_srlv)
David Daney32546f32010-02-10 15:12:46 -0800307I_u2u1u3(_rotr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000308I_u3u1u2(_subu)
309I_u2s3u1(_sw)
Paul Burton729ff562013-12-24 03:49:45 +0000310I_u1(_sync)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000311I_0(_tlbp)
David Daney32546f32010-02-10 15:12:46 -0800312I_0(_tlbr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000313I_0(_tlbwi)
314I_0(_tlbwr)
Paul Burton53ed1382013-12-24 03:50:35 +0000315I_u1(_wait);
Markos Chandrasab9e4fa2014-04-08 12:47:11 +0100316I_u2u1(_wsbh)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000317I_u3u1u2(_xor)
318I_u2u1u3(_xori)
Paul Burtond674dd12014-03-04 15:12:36 +0000319I_u2u1(_yield)
David Daney92078e02009-10-14 12:16:55 -0700320I_u2u1msbu3(_dins);
David Daneyc42aef02010-12-21 14:19:10 -0800321I_u2u1msb32u3(_dinsm);
David Daney58b9e222010-02-18 16:13:03 -0800322I_u1(_syscall);
David Daney5b97c3f2010-07-23 18:41:42 -0700323I_u1u2s3(_bbit0);
324I_u1u2s3(_bbit1);
David Daneybb3d68c2010-12-27 18:07:56 -0800325I_u3u1u2(_lwx)
326I_u3u1u2(_ldx)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000327
David Daneyc9941152010-10-07 16:03:53 -0700328#ifdef CONFIG_CPU_CAVIUM_OCTEON
329#include <asm/octeon/octeon.h>
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000330void ISAFUNC(uasm_i_pref)(u32 **buf, unsigned int a, signed int b,
David Daneyc9941152010-10-07 16:03:53 -0700331 unsigned int c)
332{
333 if (OCTEON_IS_MODEL(OCTEON_CN63XX_PASS1_X) && a <= 24 && a != 5)
334 /*
335 * As per erratum Core-14449, replace prefetches 0-4,
336 * 6-24 with 'pref 28'.
337 */
338 build_insn(buf, insn_pref, c, 28, b);
339 else
340 build_insn(buf, insn_pref, c, a, b);
341}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600342UASM_EXPORT_SYMBOL(ISAFUNC(uasm_i_pref));
David Daneyc9941152010-10-07 16:03:53 -0700343#else
344I_u2s3u1(_pref)
345#endif
346
Thiemo Seufere30ec452008-01-28 20:05:38 +0000347/* Handle labels. */
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000348void ISAFUNC(uasm_build_label)(struct uasm_label **lab, u32 *addr, int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000349{
350 (*lab)->addr = addr;
351 (*lab)->lab = lid;
352 (*lab)++;
353}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600354UASM_EXPORT_SYMBOL(ISAFUNC(uasm_build_label));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000355
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000356int ISAFUNC(uasm_in_compat_space_p)(long addr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000357{
358 /* Is this address in 32bit compat space? */
359#ifdef CONFIG_64BIT
360 return (((addr) & 0xffffffff00000000L) == 0xffffffff00000000L);
361#else
362 return 1;
363#endif
364}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600365UASM_EXPORT_SYMBOL(ISAFUNC(uasm_in_compat_space_p));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000366
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000367static int uasm_rel_highest(long val)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000368{
369#ifdef CONFIG_64BIT
370 return ((((val + 0x800080008000L) >> 48) & 0xffff) ^ 0x8000) - 0x8000;
371#else
372 return 0;
373#endif
374}
375
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000376static int uasm_rel_higher(long val)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000377{
378#ifdef CONFIG_64BIT
379 return ((((val + 0x80008000L) >> 32) & 0xffff) ^ 0x8000) - 0x8000;
380#else
381 return 0;
382#endif
383}
384
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000385int ISAFUNC(uasm_rel_hi)(long val)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000386{
387 return ((((val + 0x8000L) >> 16) & 0xffff) ^ 0x8000) - 0x8000;
388}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600389UASM_EXPORT_SYMBOL(ISAFUNC(uasm_rel_hi));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000390
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000391int ISAFUNC(uasm_rel_lo)(long val)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000392{
393 return ((val & 0xffff) ^ 0x8000) - 0x8000;
394}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600395UASM_EXPORT_SYMBOL(ISAFUNC(uasm_rel_lo));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000396
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000397void ISAFUNC(UASM_i_LA_mostly)(u32 **buf, unsigned int rs, long addr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000398{
Steven J. Hillabc597f2013-02-05 16:52:01 -0600399 if (!ISAFUNC(uasm_in_compat_space_p)(addr)) {
400 ISAFUNC(uasm_i_lui)(buf, rs, uasm_rel_highest(addr));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000401 if (uasm_rel_higher(addr))
Steven J. Hillabc597f2013-02-05 16:52:01 -0600402 ISAFUNC(uasm_i_daddiu)(buf, rs, rs, uasm_rel_higher(addr));
403 if (ISAFUNC(uasm_rel_hi(addr))) {
404 ISAFUNC(uasm_i_dsll)(buf, rs, rs, 16);
405 ISAFUNC(uasm_i_daddiu)(buf, rs, rs,
406 ISAFUNC(uasm_rel_hi)(addr));
407 ISAFUNC(uasm_i_dsll)(buf, rs, rs, 16);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000408 } else
Steven J. Hillabc597f2013-02-05 16:52:01 -0600409 ISAFUNC(uasm_i_dsll32)(buf, rs, rs, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000410 } else
Steven J. Hillabc597f2013-02-05 16:52:01 -0600411 ISAFUNC(uasm_i_lui)(buf, rs, ISAFUNC(uasm_rel_hi(addr)));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000412}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600413UASM_EXPORT_SYMBOL(ISAFUNC(UASM_i_LA_mostly));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000414
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000415void ISAFUNC(UASM_i_LA)(u32 **buf, unsigned int rs, long addr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000416{
Steven J. Hillabc597f2013-02-05 16:52:01 -0600417 ISAFUNC(UASM_i_LA_mostly)(buf, rs, addr);
418 if (ISAFUNC(uasm_rel_lo(addr))) {
419 if (!ISAFUNC(uasm_in_compat_space_p)(addr))
420 ISAFUNC(uasm_i_daddiu)(buf, rs, rs,
421 ISAFUNC(uasm_rel_lo(addr)));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000422 else
Steven J. Hillabc597f2013-02-05 16:52:01 -0600423 ISAFUNC(uasm_i_addiu)(buf, rs, rs,
424 ISAFUNC(uasm_rel_lo(addr)));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000425 }
426}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600427UASM_EXPORT_SYMBOL(ISAFUNC(UASM_i_LA));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000428
429/* Handle relocations. */
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000430void ISAFUNC(uasm_r_mips_pc16)(struct uasm_reloc **rel, u32 *addr, int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000431{
432 (*rel)->addr = addr;
433 (*rel)->type = R_MIPS_PC16;
434 (*rel)->lab = lid;
435 (*rel)++;
436}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600437UASM_EXPORT_SYMBOL(ISAFUNC(uasm_r_mips_pc16));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000438
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000439static inline void __resolve_relocs(struct uasm_reloc *rel,
440 struct uasm_label *lab);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000441
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000442void ISAFUNC(uasm_resolve_relocs)(struct uasm_reloc *rel,
443 struct uasm_label *lab)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000444{
445 struct uasm_label *l;
446
447 for (; rel->lab != UASM_LABEL_INVALID; rel++)
448 for (l = lab; l->lab != UASM_LABEL_INVALID; l++)
449 if (rel->lab == l->lab)
450 __resolve_relocs(rel, l);
451}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600452UASM_EXPORT_SYMBOL(ISAFUNC(uasm_resolve_relocs));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000453
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000454void ISAFUNC(uasm_move_relocs)(struct uasm_reloc *rel, u32 *first, u32 *end,
455 long off)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000456{
457 for (; rel->lab != UASM_LABEL_INVALID; rel++)
458 if (rel->addr >= first && rel->addr < end)
459 rel->addr += off;
460}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600461UASM_EXPORT_SYMBOL(ISAFUNC(uasm_move_relocs));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000462
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000463void ISAFUNC(uasm_move_labels)(struct uasm_label *lab, u32 *first, u32 *end,
464 long off)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000465{
466 for (; lab->lab != UASM_LABEL_INVALID; lab++)
467 if (lab->addr >= first && lab->addr < end)
468 lab->addr += off;
469}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600470UASM_EXPORT_SYMBOL(ISAFUNC(uasm_move_labels));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000471
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000472void ISAFUNC(uasm_copy_handler)(struct uasm_reloc *rel, struct uasm_label *lab,
473 u32 *first, u32 *end, u32 *target)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000474{
475 long off = (long)(target - first);
476
477 memcpy(target, first, (end - first) * sizeof(u32));
478
Steven J. Hillabc597f2013-02-05 16:52:01 -0600479 ISAFUNC(uasm_move_relocs(rel, first, end, off));
480 ISAFUNC(uasm_move_labels(lab, first, end, off));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000481}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600482UASM_EXPORT_SYMBOL(ISAFUNC(uasm_copy_handler));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000483
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000484int ISAFUNC(uasm_insn_has_bdelay)(struct uasm_reloc *rel, u32 *addr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000485{
486 for (; rel->lab != UASM_LABEL_INVALID; rel++) {
487 if (rel->addr == addr
488 && (rel->type == R_MIPS_PC16
489 || rel->type == R_MIPS_26))
490 return 1;
491 }
492
493 return 0;
494}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600495UASM_EXPORT_SYMBOL(ISAFUNC(uasm_insn_has_bdelay));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000496
497/* Convenience functions for labeled branches. */
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000498void ISAFUNC(uasm_il_bltz)(u32 **p, struct uasm_reloc **r, unsigned int reg,
499 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000500{
501 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600502 ISAFUNC(uasm_i_bltz)(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000503}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600504UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_bltz));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000505
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000506void ISAFUNC(uasm_il_b)(u32 **p, struct uasm_reloc **r, int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000507{
508 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600509 ISAFUNC(uasm_i_b)(p, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000510}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600511UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_b));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000512
Paul Burton8dee5902013-12-24 03:51:39 +0000513void ISAFUNC(uasm_il_beq)(u32 **p, struct uasm_reloc **r, unsigned int r1,
514 unsigned int r2, int lid)
515{
516 uasm_r_mips_pc16(r, *p, lid);
517 ISAFUNC(uasm_i_beq)(p, r1, r2, 0);
518}
519UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_beq));
520
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000521void ISAFUNC(uasm_il_beqz)(u32 **p, struct uasm_reloc **r, unsigned int reg,
522 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000523{
524 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600525 ISAFUNC(uasm_i_beqz)(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000526}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600527UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_beqz));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000528
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000529void ISAFUNC(uasm_il_beqzl)(u32 **p, struct uasm_reloc **r, unsigned int reg,
530 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000531{
532 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600533 ISAFUNC(uasm_i_beqzl)(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000534}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600535UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_beqzl));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000536
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000537void ISAFUNC(uasm_il_bne)(u32 **p, struct uasm_reloc **r, unsigned int reg1,
538 unsigned int reg2, int lid)
Thiemo Seuferfb2a27e72008-02-18 19:32:49 +0000539{
540 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600541 ISAFUNC(uasm_i_bne)(p, reg1, reg2, 0);
Thiemo Seuferfb2a27e72008-02-18 19:32:49 +0000542}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600543UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_bne));
Thiemo Seuferfb2a27e72008-02-18 19:32:49 +0000544
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000545void ISAFUNC(uasm_il_bnez)(u32 **p, struct uasm_reloc **r, unsigned int reg,
546 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000547{
548 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600549 ISAFUNC(uasm_i_bnez)(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000550}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600551UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_bnez));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000552
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000553void ISAFUNC(uasm_il_bgezl)(u32 **p, struct uasm_reloc **r, unsigned int reg,
554 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000555{
556 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600557 ISAFUNC(uasm_i_bgezl)(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000558}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600559UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_bgezl));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000560
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000561void ISAFUNC(uasm_il_bgez)(u32 **p, struct uasm_reloc **r, unsigned int reg,
562 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000563{
564 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600565 ISAFUNC(uasm_i_bgez)(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000566}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600567UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_bgez));
David Daney5b97c3f2010-07-23 18:41:42 -0700568
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000569void ISAFUNC(uasm_il_bbit0)(u32 **p, struct uasm_reloc **r, unsigned int reg,
570 unsigned int bit, int lid)
David Daney5b97c3f2010-07-23 18:41:42 -0700571{
572 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600573 ISAFUNC(uasm_i_bbit0)(p, reg, bit, 0);
David Daney5b97c3f2010-07-23 18:41:42 -0700574}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600575UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_bbit0));
David Daney5b97c3f2010-07-23 18:41:42 -0700576
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000577void ISAFUNC(uasm_il_bbit1)(u32 **p, struct uasm_reloc **r, unsigned int reg,
578 unsigned int bit, int lid)
David Daney5b97c3f2010-07-23 18:41:42 -0700579{
580 uasm_r_mips_pc16(r, *p, lid);
Steven J. Hillabc597f2013-02-05 16:52:01 -0600581 ISAFUNC(uasm_i_bbit1)(p, reg, bit, 0);
David Daney5b97c3f2010-07-23 18:41:42 -0700582}
Steven J. Hillabc597f2013-02-05 16:52:01 -0600583UASM_EXPORT_SYMBOL(ISAFUNC(uasm_il_bbit1));