162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
362306a36Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
462306a36Sopenharmony_ci * for more details.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * A small micro-assembler. It is intentionally kept simple, does only
762306a36Sopenharmony_ci * support a subset of instructions, and does not try to hide pipeline
862306a36Sopenharmony_ci * effects like branch delay slots.
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci * Copyright (C) 2004, 2005, 2006, 2008	 Thiemo Seufer
1162306a36Sopenharmony_ci * Copyright (C) 2005, 2007  Maciej W. Rozycki
1262306a36Sopenharmony_ci * Copyright (C) 2006  Ralf Baechle (ralf@linux-mips.org)
1362306a36Sopenharmony_ci * Copyright (C) 2012, 2013  MIPS Technologies, Inc.  All rights reserved.
1462306a36Sopenharmony_ci */
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#include <linux/kernel.h>
1762306a36Sopenharmony_ci#include <linux/types.h>
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#include <asm/inst.h>
2062306a36Sopenharmony_ci#include <asm/elf.h>
2162306a36Sopenharmony_ci#include <asm/bugs.h>
2262306a36Sopenharmony_ci#include <asm/uasm.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#define RS_MASK		0x1f
2562306a36Sopenharmony_ci#define RS_SH		21
2662306a36Sopenharmony_ci#define RT_MASK		0x1f
2762306a36Sopenharmony_ci#define RT_SH		16
2862306a36Sopenharmony_ci#define SCIMM_MASK	0xfffff
2962306a36Sopenharmony_ci#define SCIMM_SH	6
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci/* This macro sets the non-variable bits of an instruction. */
3262306a36Sopenharmony_ci#define M(a, b, c, d, e, f)					\
3362306a36Sopenharmony_ci	((a) << OP_SH						\
3462306a36Sopenharmony_ci	 | (b) << RS_SH						\
3562306a36Sopenharmony_ci	 | (c) << RT_SH						\
3662306a36Sopenharmony_ci	 | (d) << RD_SH						\
3762306a36Sopenharmony_ci	 | (e) << RE_SH						\
3862306a36Sopenharmony_ci	 | (f) << FUNC_SH)
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci/* This macro sets the non-variable bits of an R6 instruction. */
4162306a36Sopenharmony_ci#define M6(a, b, c, d, e)					\
4262306a36Sopenharmony_ci	((a) << OP_SH						\
4362306a36Sopenharmony_ci	 | (b) << RS_SH						\
4462306a36Sopenharmony_ci	 | (c) << RT_SH						\
4562306a36Sopenharmony_ci	 | (d) << SIMM9_SH					\
4662306a36Sopenharmony_ci	 | (e) << FUNC_SH)
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci#include "uasm.c"
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_cistatic const struct insn insn_table[insn_invalid] = {
5162306a36Sopenharmony_ci	[insn_addiu]	= {M(addiu_op, 0, 0, 0, 0, 0), RS | RT | SIMM},
5262306a36Sopenharmony_ci	[insn_addu]	= {M(spec_op, 0, 0, 0, 0, addu_op), RS | RT | RD},
5362306a36Sopenharmony_ci	[insn_and]	= {M(spec_op, 0, 0, 0, 0, and_op), RS | RT | RD},
5462306a36Sopenharmony_ci	[insn_andi]	= {M(andi_op, 0, 0, 0, 0, 0), RS | RT | UIMM},
5562306a36Sopenharmony_ci	[insn_bbit0]	= {M(lwc2_op, 0, 0, 0, 0, 0), RS | RT | BIMM},
5662306a36Sopenharmony_ci	[insn_bbit1]	= {M(swc2_op, 0, 0, 0, 0, 0), RS | RT | BIMM},
5762306a36Sopenharmony_ci	[insn_beq]	= {M(beq_op, 0, 0, 0, 0, 0), RS | RT | BIMM},
5862306a36Sopenharmony_ci	[insn_beql]	= {M(beql_op, 0, 0, 0, 0, 0), RS | RT | BIMM},
5962306a36Sopenharmony_ci	[insn_bgez]	= {M(bcond_op, 0, bgez_op, 0, 0, 0), RS | BIMM},
6062306a36Sopenharmony_ci	[insn_bgezl]	= {M(bcond_op, 0, bgezl_op, 0, 0, 0), RS | BIMM},
6162306a36Sopenharmony_ci	[insn_bgtz]	= {M(bgtz_op, 0, 0, 0, 0, 0), RS | BIMM},
6262306a36Sopenharmony_ci	[insn_blez]	= {M(blez_op, 0, 0, 0, 0, 0), RS | BIMM},
6362306a36Sopenharmony_ci	[insn_bltz]	= {M(bcond_op, 0, bltz_op, 0, 0, 0), RS | BIMM},
6462306a36Sopenharmony_ci	[insn_bltzl]	= {M(bcond_op, 0, bltzl_op, 0, 0, 0), RS | BIMM},
6562306a36Sopenharmony_ci	[insn_bne]	= {M(bne_op, 0, 0, 0, 0, 0), RS | RT | BIMM},
6662306a36Sopenharmony_ci	[insn_break]	= {M(spec_op, 0, 0, 0, 0, break_op), SCIMM},
6762306a36Sopenharmony_ci#ifndef CONFIG_CPU_MIPSR6
6862306a36Sopenharmony_ci	[insn_cache]	= {M(cache_op, 0, 0, 0, 0, 0),  RS | RT | SIMM},
6962306a36Sopenharmony_ci#else
7062306a36Sopenharmony_ci	[insn_cache]	= {M6(spec3_op, 0, 0, 0, cache6_op),  RS | RT | SIMM9},
7162306a36Sopenharmony_ci#endif
7262306a36Sopenharmony_ci	[insn_cfc1]	= {M(cop1_op, cfc_op, 0, 0, 0, 0), RT | RD},
7362306a36Sopenharmony_ci	[insn_cfcmsa]	= {M(msa_op, 0, msa_cfc_op, 0, 0, msa_elm_op), RD | RE},
7462306a36Sopenharmony_ci	[insn_ctc1]	= {M(cop1_op, ctc_op, 0, 0, 0, 0), RT | RD},
7562306a36Sopenharmony_ci	[insn_ctcmsa]	= {M(msa_op, 0, msa_ctc_op, 0, 0, msa_elm_op), RD | RE},
7662306a36Sopenharmony_ci	[insn_daddiu]	= {M(daddiu_op, 0, 0, 0, 0, 0), RS | RT | SIMM},
7762306a36Sopenharmony_ci	[insn_daddu]	= {M(spec_op, 0, 0, 0, 0, daddu_op), RS | RT | RD},
7862306a36Sopenharmony_ci	[insn_ddivu]	= {M(spec_op, 0, 0, 0, 0, ddivu_op), RS | RT},
7962306a36Sopenharmony_ci	[insn_ddivu_r6]	= {M(spec_op, 0, 0, 0, ddivu_ddivu6_op, ddivu_op),
8062306a36Sopenharmony_ci				RS | RT | RD},
8162306a36Sopenharmony_ci	[insn_di]	= {M(cop0_op, mfmc0_op, 0, 12, 0, 0), RT},
8262306a36Sopenharmony_ci	[insn_dins]	= {M(spec3_op, 0, 0, 0, 0, dins_op), RS | RT | RD | RE},
8362306a36Sopenharmony_ci	[insn_dinsm]	= {M(spec3_op, 0, 0, 0, 0, dinsm_op), RS | RT | RD | RE},
8462306a36Sopenharmony_ci	[insn_dinsu]	= {M(spec3_op, 0, 0, 0, 0, dinsu_op), RS | RT | RD | RE},
8562306a36Sopenharmony_ci	[insn_divu]	= {M(spec_op, 0, 0, 0, 0, divu_op), RS | RT},
8662306a36Sopenharmony_ci	[insn_divu_r6]	= {M(spec_op, 0, 0, 0, divu_divu6_op, divu_op),
8762306a36Sopenharmony_ci				RS | RT | RD},
8862306a36Sopenharmony_ci	[insn_dmfc0]	= {M(cop0_op, dmfc_op, 0, 0, 0, 0), RT | RD | SET},
8962306a36Sopenharmony_ci	[insn_dmodu]	= {M(spec_op, 0, 0, 0, ddivu_dmodu_op, ddivu_op),
9062306a36Sopenharmony_ci				RS | RT | RD},
9162306a36Sopenharmony_ci	[insn_dmtc0]	= {M(cop0_op, dmtc_op, 0, 0, 0, 0), RT | RD | SET},
9262306a36Sopenharmony_ci	[insn_dmultu]	= {M(spec_op, 0, 0, 0, 0, dmultu_op), RS | RT},
9362306a36Sopenharmony_ci	[insn_dmulu]	= {M(spec_op, 0, 0, 0, dmultu_dmulu_op, dmultu_op),
9462306a36Sopenharmony_ci				RS | RT | RD},
9562306a36Sopenharmony_ci	[insn_drotr]	= {M(spec_op, 1, 0, 0, 0, dsrl_op), RT | RD | RE},
9662306a36Sopenharmony_ci	[insn_drotr32]	= {M(spec_op, 1, 0, 0, 0, dsrl32_op), RT | RD | RE},
9762306a36Sopenharmony_ci	[insn_dsbh]	= {M(spec3_op, 0, 0, 0, dsbh_op, dbshfl_op), RT | RD},
9862306a36Sopenharmony_ci	[insn_dshd]	= {M(spec3_op, 0, 0, 0, dshd_op, dbshfl_op), RT | RD},
9962306a36Sopenharmony_ci	[insn_dsll]	= {M(spec_op, 0, 0, 0, 0, dsll_op), RT | RD | RE},
10062306a36Sopenharmony_ci	[insn_dsll32]	= {M(spec_op, 0, 0, 0, 0, dsll32_op), RT | RD | RE},
10162306a36Sopenharmony_ci	[insn_dsllv]	= {M(spec_op, 0, 0, 0, 0, dsllv_op),  RS | RT | RD},
10262306a36Sopenharmony_ci	[insn_dsra]	= {M(spec_op, 0, 0, 0, 0, dsra_op), RT | RD | RE},
10362306a36Sopenharmony_ci	[insn_dsra32]	= {M(spec_op, 0, 0, 0, 0, dsra32_op), RT | RD | RE},
10462306a36Sopenharmony_ci	[insn_dsrav]	= {M(spec_op, 0, 0, 0, 0, dsrav_op),  RS | RT | RD},
10562306a36Sopenharmony_ci	[insn_dsrl]	= {M(spec_op, 0, 0, 0, 0, dsrl_op), RT | RD | RE},
10662306a36Sopenharmony_ci	[insn_dsrl32]	= {M(spec_op, 0, 0, 0, 0, dsrl32_op), RT | RD | RE},
10762306a36Sopenharmony_ci	[insn_dsrlv]	= {M(spec_op, 0, 0, 0, 0, dsrlv_op),  RS | RT | RD},
10862306a36Sopenharmony_ci	[insn_dsubu]	= {M(spec_op, 0, 0, 0, 0, dsubu_op), RS | RT | RD},
10962306a36Sopenharmony_ci	[insn_eret]	= {M(cop0_op, cop_op, 0, 0, 0, eret_op),  0},
11062306a36Sopenharmony_ci	[insn_ext]	= {M(spec3_op, 0, 0, 0, 0, ext_op), RS | RT | RD | RE},
11162306a36Sopenharmony_ci	[insn_ins]	= {M(spec3_op, 0, 0, 0, 0, ins_op), RS | RT | RD | RE},
11262306a36Sopenharmony_ci	[insn_j]	= {M(j_op, 0, 0, 0, 0, 0),  JIMM},
11362306a36Sopenharmony_ci	[insn_jal]	= {M(jal_op, 0, 0, 0, 0, 0),	JIMM},
11462306a36Sopenharmony_ci	[insn_jalr]	= {M(spec_op, 0, 0, 0, 0, jalr_op), RS | RD},
11562306a36Sopenharmony_ci#ifndef CONFIG_CPU_MIPSR6
11662306a36Sopenharmony_ci	[insn_jr]	= {M(spec_op, 0, 0, 0, 0, jr_op),  RS},
11762306a36Sopenharmony_ci#else
11862306a36Sopenharmony_ci	[insn_jr]	= {M(spec_op, 0, 0, 0, 0, jalr_op),  RS},
11962306a36Sopenharmony_ci#endif
12062306a36Sopenharmony_ci	[insn_lb]	= {M(lb_op, 0, 0, 0, 0, 0), RS | RT | SIMM},
12162306a36Sopenharmony_ci	[insn_lbu]	= {M(lbu_op, 0, 0, 0, 0, 0), RS | RT | SIMM},
12262306a36Sopenharmony_ci	[insn_ld]	= {M(ld_op, 0, 0, 0, 0, 0),  RS | RT | SIMM},
12362306a36Sopenharmony_ci	[insn_lddir]	= {M(lwc2_op, 0, 0, 0, lddir_op, mult_op), RS | RT | RD},
12462306a36Sopenharmony_ci	[insn_ldpte]	= {M(lwc2_op, 0, 0, 0, ldpte_op, mult_op), RS | RD},
12562306a36Sopenharmony_ci	[insn_ldx]	= {M(spec3_op, 0, 0, 0, ldx_op, lx_op), RS | RT | RD},
12662306a36Sopenharmony_ci	[insn_lh]	= {M(lh_op, 0, 0, 0, 0, 0),  RS | RT | SIMM},
12762306a36Sopenharmony_ci	[insn_lhu]	= {M(lhu_op, 0, 0, 0, 0, 0),  RS | RT | SIMM},
12862306a36Sopenharmony_ci#ifndef CONFIG_CPU_MIPSR6
12962306a36Sopenharmony_ci	[insn_ll]	= {M(ll_op, 0, 0, 0, 0, 0),  RS | RT | SIMM},
13062306a36Sopenharmony_ci	[insn_lld]	= {M(lld_op, 0, 0, 0, 0, 0),	RS | RT | SIMM},
13162306a36Sopenharmony_ci#else
13262306a36Sopenharmony_ci	[insn_ll]	= {M6(spec3_op, 0, 0, 0, ll6_op),  RS | RT | SIMM9},
13362306a36Sopenharmony_ci	[insn_lld]	= {M6(spec3_op, 0, 0, 0, lld6_op),  RS | RT | SIMM9},
13462306a36Sopenharmony_ci#endif
13562306a36Sopenharmony_ci	[insn_lui]	= {M(lui_op, 0, 0, 0, 0, 0),	RT | SIMM},
13662306a36Sopenharmony_ci	[insn_lw]	= {M(lw_op, 0, 0, 0, 0, 0),  RS | RT | SIMM},
13762306a36Sopenharmony_ci	[insn_lwu]	= {M(lwu_op, 0, 0, 0, 0, 0),  RS | RT | SIMM},
13862306a36Sopenharmony_ci	[insn_lwx]	= {M(spec3_op, 0, 0, 0, lwx_op, lx_op), RS | RT | RD},
13962306a36Sopenharmony_ci	[insn_mfc0]	= {M(cop0_op, mfc_op, 0, 0, 0, 0),  RT | RD | SET},
14062306a36Sopenharmony_ci	[insn_mfhc0]	= {M(cop0_op, mfhc0_op, 0, 0, 0, 0),  RT | RD | SET},
14162306a36Sopenharmony_ci	[insn_mfhi]	= {M(spec_op, 0, 0, 0, 0, mfhi_op), RD},
14262306a36Sopenharmony_ci	[insn_mflo]	= {M(spec_op, 0, 0, 0, 0, mflo_op), RD},
14362306a36Sopenharmony_ci	[insn_modu]	= {M(spec_op, 0, 0, 0, divu_modu_op, divu_op),
14462306a36Sopenharmony_ci				RS | RT | RD},
14562306a36Sopenharmony_ci	[insn_movn]	= {M(spec_op, 0, 0, 0, 0, movn_op), RS | RT | RD},
14662306a36Sopenharmony_ci	[insn_movz]	= {M(spec_op, 0, 0, 0, 0, movz_op), RS | RT | RD},
14762306a36Sopenharmony_ci	[insn_mtc0]	= {M(cop0_op, mtc_op, 0, 0, 0, 0),  RT | RD | SET},
14862306a36Sopenharmony_ci	[insn_mthc0]	= {M(cop0_op, mthc0_op, 0, 0, 0, 0),  RT | RD | SET},
14962306a36Sopenharmony_ci	[insn_mthi]	= {M(spec_op, 0, 0, 0, 0, mthi_op), RS},
15062306a36Sopenharmony_ci	[insn_mtlo]	= {M(spec_op, 0, 0, 0, 0, mtlo_op), RS},
15162306a36Sopenharmony_ci	[insn_mulu]	= {M(spec_op, 0, 0, 0, multu_mulu_op, multu_op),
15262306a36Sopenharmony_ci				RS | RT | RD},
15362306a36Sopenharmony_ci	[insn_muhu]	= {M(spec_op, 0, 0, 0, multu_muhu_op, multu_op),
15462306a36Sopenharmony_ci				RS | RT | RD},
15562306a36Sopenharmony_ci#ifndef CONFIG_CPU_MIPSR6
15662306a36Sopenharmony_ci	[insn_mul]	= {M(spec2_op, 0, 0, 0, 0, mul_op), RS | RT | RD},
15762306a36Sopenharmony_ci#else
15862306a36Sopenharmony_ci	[insn_mul]	= {M(spec_op, 0, 0, 0, mult_mul_op, mult_op), RS | RT | RD},
15962306a36Sopenharmony_ci#endif
16062306a36Sopenharmony_ci	[insn_multu]	= {M(spec_op, 0, 0, 0, 0, multu_op), RS | RT},
16162306a36Sopenharmony_ci	[insn_nor]	= {M(spec_op, 0, 0, 0, 0, nor_op),  RS | RT | RD},
16262306a36Sopenharmony_ci	[insn_or]	= {M(spec_op, 0, 0, 0, 0, or_op),  RS | RT | RD},
16362306a36Sopenharmony_ci	[insn_ori]	= {M(ori_op, 0, 0, 0, 0, 0),	RS | RT | UIMM},
16462306a36Sopenharmony_ci#ifndef CONFIG_CPU_MIPSR6
16562306a36Sopenharmony_ci	[insn_pref]	= {M(pref_op, 0, 0, 0, 0, 0),  RS | RT | SIMM},
16662306a36Sopenharmony_ci#else
16762306a36Sopenharmony_ci	[insn_pref]	= {M6(spec3_op, 0, 0, 0, pref6_op),  RS | RT | SIMM9},
16862306a36Sopenharmony_ci#endif
16962306a36Sopenharmony_ci	[insn_rfe]	= {M(cop0_op, cop_op, 0, 0, 0, rfe_op),  0},
17062306a36Sopenharmony_ci	[insn_rotr]	= {M(spec_op, 1, 0, 0, 0, srl_op),  RT | RD | RE},
17162306a36Sopenharmony_ci	[insn_sb]	= {M(sb_op, 0, 0, 0, 0, 0),  RS | RT | SIMM},
17262306a36Sopenharmony_ci#ifndef CONFIG_CPU_MIPSR6
17362306a36Sopenharmony_ci	[insn_sc]	= {M(sc_op, 0, 0, 0, 0, 0),  RS | RT | SIMM},
17462306a36Sopenharmony_ci	[insn_scd]	= {M(scd_op, 0, 0, 0, 0, 0),	RS | RT | SIMM},
17562306a36Sopenharmony_ci#else
17662306a36Sopenharmony_ci	[insn_sc]	= {M6(spec3_op, 0, 0, 0, sc6_op),  RS | RT | SIMM9},
17762306a36Sopenharmony_ci	[insn_scd]	= {M6(spec3_op, 0, 0, 0, scd6_op),  RS | RT | SIMM9},
17862306a36Sopenharmony_ci#endif
17962306a36Sopenharmony_ci	[insn_sd]	= {M(sd_op, 0, 0, 0, 0, 0),  RS | RT | SIMM},
18062306a36Sopenharmony_ci	[insn_seleqz]	= {M(spec_op, 0, 0, 0, 0, seleqz_op), RS | RT | RD},
18162306a36Sopenharmony_ci	[insn_selnez]	= {M(spec_op, 0, 0, 0, 0, selnez_op), RS | RT | RD},
18262306a36Sopenharmony_ci	[insn_sh]	= {M(sh_op, 0, 0, 0, 0, 0),  RS | RT | SIMM},
18362306a36Sopenharmony_ci	[insn_sll]	= {M(spec_op, 0, 0, 0, 0, sll_op),  RT | RD | RE},
18462306a36Sopenharmony_ci	[insn_sllv]	= {M(spec_op, 0, 0, 0, 0, sllv_op),  RS | RT | RD},
18562306a36Sopenharmony_ci	[insn_slt]	= {M(spec_op, 0, 0, 0, 0, slt_op),  RS | RT | RD},
18662306a36Sopenharmony_ci	[insn_slti]	= {M(slti_op, 0, 0, 0, 0, 0), RS | RT | SIMM},
18762306a36Sopenharmony_ci	[insn_sltiu]	= {M(sltiu_op, 0, 0, 0, 0, 0), RS | RT | SIMM},
18862306a36Sopenharmony_ci	[insn_sltu]	= {M(spec_op, 0, 0, 0, 0, sltu_op), RS | RT | RD},
18962306a36Sopenharmony_ci	[insn_sra]	= {M(spec_op, 0, 0, 0, 0, sra_op),  RT | RD | RE},
19062306a36Sopenharmony_ci	[insn_srav]	= {M(spec_op, 0, 0, 0, 0, srav_op), RS | RT | RD},
19162306a36Sopenharmony_ci	[insn_srl]	= {M(spec_op, 0, 0, 0, 0, srl_op),  RT | RD | RE},
19262306a36Sopenharmony_ci	[insn_srlv]	= {M(spec_op, 0, 0, 0, 0, srlv_op),  RS | RT | RD},
19362306a36Sopenharmony_ci	[insn_subu]	= {M(spec_op, 0, 0, 0, 0, subu_op),	RS | RT | RD},
19462306a36Sopenharmony_ci	[insn_sw]	= {M(sw_op, 0, 0, 0, 0, 0),  RS | RT | SIMM},
19562306a36Sopenharmony_ci	[insn_sync]	= {M(spec_op, 0, 0, 0, 0, sync_op), RE},
19662306a36Sopenharmony_ci	[insn_syscall]	= {M(spec_op, 0, 0, 0, 0, syscall_op), SCIMM},
19762306a36Sopenharmony_ci	[insn_tlbp]	= {M(cop0_op, cop_op, 0, 0, 0, tlbp_op),  0},
19862306a36Sopenharmony_ci	[insn_tlbr]	= {M(cop0_op, cop_op, 0, 0, 0, tlbr_op),  0},
19962306a36Sopenharmony_ci	[insn_tlbwi]	= {M(cop0_op, cop_op, 0, 0, 0, tlbwi_op),  0},
20062306a36Sopenharmony_ci	[insn_tlbwr]	= {M(cop0_op, cop_op, 0, 0, 0, tlbwr_op),  0},
20162306a36Sopenharmony_ci	[insn_wait]	= {M(cop0_op, cop_op, 0, 0, 0, wait_op), SCIMM},
20262306a36Sopenharmony_ci	[insn_wsbh]	= {M(spec3_op, 0, 0, 0, wsbh_op, bshfl_op), RT | RD},
20362306a36Sopenharmony_ci	[insn_xor]	= {M(spec_op, 0, 0, 0, 0, xor_op),  RS | RT | RD},
20462306a36Sopenharmony_ci	[insn_xori]	= {M(xori_op, 0, 0, 0, 0, 0),  RS | RT | UIMM},
20562306a36Sopenharmony_ci	[insn_yield]	= {M(spec3_op, 0, 0, 0, 0, yield_op), RS | RD},
20662306a36Sopenharmony_ci};
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci#undef M
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_cistatic inline u32 build_bimm(s32 arg)
21162306a36Sopenharmony_ci{
21262306a36Sopenharmony_ci	WARN(arg > 0x1ffff || arg < -0x20000,
21362306a36Sopenharmony_ci	     KERN_WARNING "Micro-assembler field overflow\n");
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	WARN(arg & 0x3, KERN_WARNING "Invalid micro-assembler branch target\n");
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci	return ((arg < 0) ? (1 << 15) : 0) | ((arg >> 2) & 0x7fff);
21862306a36Sopenharmony_ci}
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_cistatic inline u32 build_jimm(u32 arg)
22162306a36Sopenharmony_ci{
22262306a36Sopenharmony_ci	WARN(arg & ~(JIMM_MASK << 2),
22362306a36Sopenharmony_ci	     KERN_WARNING "Micro-assembler field overflow\n");
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	return (arg >> 2) & JIMM_MASK;
22662306a36Sopenharmony_ci}
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci/*
22962306a36Sopenharmony_ci * The order of opcode arguments is implicitly left to right,
23062306a36Sopenharmony_ci * starting with RS and ending with FUNC or IMM.
23162306a36Sopenharmony_ci */
23262306a36Sopenharmony_cistatic void build_insn(u32 **buf, enum opcode opc, ...)
23362306a36Sopenharmony_ci{
23462306a36Sopenharmony_ci	const struct insn *ip;
23562306a36Sopenharmony_ci	va_list ap;
23662306a36Sopenharmony_ci	u32 op;
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci	if (opc < 0 || opc >= insn_invalid ||
23962306a36Sopenharmony_ci	    (opc == insn_daddiu && r4k_daddiu_bug()) ||
24062306a36Sopenharmony_ci	    (insn_table[opc].match == 0 && insn_table[opc].fields == 0))
24162306a36Sopenharmony_ci		panic("Unsupported Micro-assembler instruction %d", opc);
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci	ip = &insn_table[opc];
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	op = ip->match;
24662306a36Sopenharmony_ci	va_start(ap, opc);
24762306a36Sopenharmony_ci	if (ip->fields & RS)
24862306a36Sopenharmony_ci		op |= build_rs(va_arg(ap, u32));
24962306a36Sopenharmony_ci	if (ip->fields & RT)
25062306a36Sopenharmony_ci		op |= build_rt(va_arg(ap, u32));
25162306a36Sopenharmony_ci	if (ip->fields & RD)
25262306a36Sopenharmony_ci		op |= build_rd(va_arg(ap, u32));
25362306a36Sopenharmony_ci	if (ip->fields & RE)
25462306a36Sopenharmony_ci		op |= build_re(va_arg(ap, u32));
25562306a36Sopenharmony_ci	if (ip->fields & SIMM)
25662306a36Sopenharmony_ci		op |= build_simm(va_arg(ap, s32));
25762306a36Sopenharmony_ci	if (ip->fields & UIMM)
25862306a36Sopenharmony_ci		op |= build_uimm(va_arg(ap, u32));
25962306a36Sopenharmony_ci	if (ip->fields & BIMM)
26062306a36Sopenharmony_ci		op |= build_bimm(va_arg(ap, s32));
26162306a36Sopenharmony_ci	if (ip->fields & JIMM)
26262306a36Sopenharmony_ci		op |= build_jimm(va_arg(ap, u32));
26362306a36Sopenharmony_ci	if (ip->fields & FUNC)
26462306a36Sopenharmony_ci		op |= build_func(va_arg(ap, u32));
26562306a36Sopenharmony_ci	if (ip->fields & SET)
26662306a36Sopenharmony_ci		op |= build_set(va_arg(ap, u32));
26762306a36Sopenharmony_ci	if (ip->fields & SCIMM)
26862306a36Sopenharmony_ci		op |= build_scimm(va_arg(ap, u32));
26962306a36Sopenharmony_ci	if (ip->fields & SIMM9)
27062306a36Sopenharmony_ci		op |= build_scimm9(va_arg(ap, u32));
27162306a36Sopenharmony_ci	va_end(ap);
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	**buf = op;
27462306a36Sopenharmony_ci	(*buf)++;
27562306a36Sopenharmony_ci}
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_cistatic inline void
27862306a36Sopenharmony_ci__resolve_relocs(struct uasm_reloc *rel, struct uasm_label *lab)
27962306a36Sopenharmony_ci{
28062306a36Sopenharmony_ci	long laddr = (long)lab->addr;
28162306a36Sopenharmony_ci	long raddr = (long)rel->addr;
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci	switch (rel->type) {
28462306a36Sopenharmony_ci	case R_MIPS_PC16:
28562306a36Sopenharmony_ci		*rel->addr |= build_bimm(laddr - (raddr + 4));
28662306a36Sopenharmony_ci		break;
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci	default:
28962306a36Sopenharmony_ci		panic("Unsupported Micro-assembler relocation %d",
29062306a36Sopenharmony_ci		      rel->type);
29162306a36Sopenharmony_ci	}
29262306a36Sopenharmony_ci}
293