1 /*
2  * Copyright (C) 2019  Free Software Foundation, Inc.
3  *
4  * This file is part of GNU lightning.
5  *
6  * GNU lightning is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published
8  * by the Free Software Foundation; either version 3, or (at your option)
9  * any later version.
10  *
11  * GNU lightning is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14  * License for more details.
15  *
16  * Authors:
17  *	Paulo Cesar Pereira de Andrade
18  */
19 
20 #if PROTO
21 #define _ZERO_REGNO		0
22 #define _RA_REGNO		1
23 #define _SP_REGNO		2
24 #define _FP_REGNO		8
25 typedef union {
26 #  define ui			jit_uint32_t
27     struct  {
28 	ui opcode	: 7;
29 	ui rd		: 5;
30 	ui funct3	: 3;
31 	ui rs1		: 5;
32 	ui rs2		: 5;
33 	ui funct7	: 7;
34     } R;
35     struct  {
36 	ui opcode	: 7;
37 	ui rd		: 5;
38 	ui funct3	: 3;
39 	ui rs1		: 5;
40 	ui rs2		: 5;
41 	ui funct2	: 2;
42 	ui rs3		: 5;
43     } R4;
44     struct  {
45 	ui opcode	: 7;
46 	ui rd		: 5;
47 	ui funct3	: 3;
48 	ui rs1		: 5;
49 	ui imm11_0	: 12;
50     } I;
51 #  if __WORDSIZE == 64
52     struct  {
53 	ui opcode	: 7;
54 	ui rd		: 5;
55 	ui funct3	: 3;
56 	ui rs1		: 5;
57 	ui shamt	: 6;
58 	ui imm6_0	: 6;
59     } IS;
60 #  endif
61     struct  {
62 	ui opcode	: 7;
63 	ui imm4_0	: 5;
64 	ui funct3	: 3;
65 	ui rs1		: 5;
66 	ui rs2		: 5;
67 	ui imm11_5	: 7;
68     } S;
69     struct  {
70 	ui opcode	: 7;
71 	ui imm11	: 1;
72 	ui imm4_1	: 4;
73 	ui funct3	: 3;
74 	ui rs1		: 5;
75 	ui rs2		: 5;
76 	ui imm10_5	: 6;
77 	ui imm12	: 1;
78     } B;
79     struct  {
80 	ui opcode	: 7;
81 	ui rd		: 5;
82 	ui imm12_31	: 20;
83     } U;
84     struct  {
85 	ui opcode	: 7;
86 	ui rd		: 5;
87 	ui imm19_12	: 8;
88 	ui imm11	: 1;
89 	ui imm10_1	: 10;
90 	ui imm20	: 1;
91     } J;
92     jit_int32_t		w;
93 #  undef ui
94 } instr_t;
95 #  define ii(i)				*_jit->pc.ui++ = i
96 /* FIXME could jit_rewind_prolog() to only use extra 64 bytes
97  * if a variadic jit function that have variadic arguments in
98  * registers */
99 #  define stack_framesize		(200 + 64)
100 #  define ldr(r0, r1)			ldr_l(r0, r1)
101 #  define ldi(r0, im)			ldi_l(r0, im)
102 #  define ldxr(r0, r1, r2)		ldxr_l(r0, r1, r2)
103 #  define ldxi(r0, r1, im)		ldxi_l(r0, r1, im)
104 #  define str(r0, r1)			str_l(r0, r1)
105 #  define sti(im, r0)			sti_l(im, r0)
106 #  define stxr(r0, r1, r2)		stxr_l(r0, r1, r2)
107 #  define stxi(im, r0, r1)		stxi_l(im, r0, r1)
108 #  define simm6_p(im)			((im) <= 31 && (im) >= -32)
109 #  define simm12_p(im)			((im) <= 2047 && (im) >= -2048)
110 #  define simm20_p(im)			((im) <= 524287 && (im) >= -524288)
111 #  define simm32_p(im)			((im) <= 2147483647LL && (im) >= -2147483648LL)
112 
113 /*
114  * RV32I Base Instruction Set
115  */
116 #  define LUI(rd, imm)			Utype(55, rd, imm)
117 #  define AUIPC(rd, imm)		Utype(23, rd, imm)
118 #  define JAL(rd, imm)			Jtype(111, rd, imm)
119 #  define JALR(rd, rs1, imm)		Itype(103, rd, 0, rs1, imm)
120 #  define BEQ(rs1, rs2, imm)		Btype(99, 0, rs1, rs2, imm)
121 #  define BNE(rs1, rs2, imm)		Btype(99, 1, rs1, rs2, imm)
122 #  define BLT(rs1, rs2, imm)		Btype(99, 4, rs1, rs2, imm)
123 #  define BGE(rs1, rs2, imm)		Btype(99, 5, rs1, rs2, imm)
124 #  define BLTU(rs1, rs2, imm)		Btype(99, 6, rs1, rs2, imm)
125 #  define BGEU(rs1, rs2, imm)		Btype(99, 7, rs1, rs2, imm)
126 #  define LB(rd, rs1, imm)		Itype(3, rd, 0, rs1, imm)
127 #  define LH(rd, rs1, imm)		Itype(3, rd, 1, rs1, imm)
128 #  define LW(rd, rs1, imm)		Itype(3, rd, 2, rs1, imm)
129 #  define LBU(rd, rs1, imm)		Itype(3, rd, 4, rs1, imm)
130 #  define LHU(rd, rs1, imm)		Itype(3, rd, 5, rs1, imm)
131 #  define SB(rs1, rs2, imm)		Stype(35, 0, rs1, rs2, imm)
132 #  define SH(rs1, rs2, imm)		Stype(35, 1, rs1, rs2, imm)
133 #  define SW(rs1, rs2, imm)		Stype(35, 2, rs1, rs2, imm)
134 #  define ADDI(rd, rs1, imm)		Itype(19, rd, 0, rs1, imm)
135 #  define SLTI(rd, rs1, imm)		Itype(19, rd, 2, rs1, imm)
136 #  define SLTIU(rd, rs1, imm)		Itype(19, rd, 3, rs1, imm)
137 #  define XORI(rd, rs1, imm)		Itype(19, rd, 4, rs1, imm)
138 #  define ORI(rd, rs1, imm)		Itype(19, rd, 6, rs1, imm)
139 #  define ANDI(rd, rs1, imm)		Itype(19, rd, 7, rs1, imm)
140 #  if __WORDSIZE == 32
141 #    define SLLI(rd, rs1, imm)		Rtype(19, rd, 1, rs1, imm, 0)
142 #    define SRLI(rd, rs1, imm)		Rtype(19, rd, 5, rs1, imm, 0)
143 #    define SRAI(rd, rs1, imm)		Rtype(19, rd, 5, rs1, imm, 32)
144 #  endif
145 #  define ADD(rd, rs1, rs2)		Rtype(51, rd, 0, rs1, rs2, 0)
146 #  define SUB(rd, rs1, rs2)		Rtype(51, rd, 0, rs1, rs2, 32)
147 #  define SLL(rd, rs1, rs2)		Rtype(51, rd, 1, rs1, rs2, 0)
148 #  define SLT(rd, rs1, rs2)		Rtype(51, rd, 2, rs1, rs2, 0)
149 #  define SLTU(rd, rs1, rs2)		Rtype(51, rd, 3, rs1, rs2, 0)
150 #  define XOR(rd, rs1, rs2)		Rtype(51, rd, 4, rs1, rs2, 0)
151 #  define SRL(rd, rs1, rs2)		Rtype(51, rd, 5, rs1, rs2, 0)
152 #  define SRA(rd, rs1, rs2)		Rtype(51, rd, 5, rs1, rs2, 32)
153 #  define OR(rd, rs1, rs2)		Rtype(51, rd, 6, rs1, rs2, 0)
154 #  define AND(rd, rs1, rs2)		Rtype(51, rd, 7, rs1, rs2, 0)
155 #  define FENCE(imm)			Itype(15, 0, 0, 0, im)
156 #  define FENCE_I(imm)			Itype(15, 0, 1, 0, im)
157 #  define ECALL()			Itype(115, 0, 0, 0, 0)
158 #  define EBREAK()			Itype(115, 0, 0, 0, 1)
159 #  define CSRRW(rd, rs1, csr)		Itype(115, rd, 1, rs1, csr)
160 #  define CSRRS(rd, rs1, csr)		Itype(115, rd, 2, rs1, csr)
161 #  define CSRRC(rd, rs1, csr)		Itype(115, rd, 3, rs1, csr)
162 #  define CSRRWI(rd, zimm, csr)		Itype(115, rd, 5, zimm, csr)
163 #  define CSRRSI(rd, zimm, csr)		Itype(115, rd, 6, zimm, csr)
164 #  define CSRRCI(rd, zimm, csr)		Itype(115, rd, 7, zimm, csr)
165 /*
166  * RV64I Base Instruction Set (in addition to RV32I)
167  */
168 #  define LWU(rd, rs1, imm)		Itype(3, rd, 6, rs1, imm)
169 #  define LD(rd, rs1, imm)		Itype(3, rd, 3, rs1, imm)
170 #  define SD(rs1, rs2, imm)		Stype(35, 3, rs1, rs2, imm)
171 #  if __WORDSIZE == 64
172 #    define SLLI(rd, rs1, sh)		IStype(19, rd, 1, rs1, sh, 0)
173 #    define SRLI(rd, rs1, sh)		IStype(19, rd, 5, rs1, sh, 0)
174 #    define SRAI(rd, rs1, sh)		IStype(19, rd, 5, rs1, sh, 16)
175 #  endif
176 #  define ADDIW(rd, rs1, imm)		Itype(27, rd, 0, rs1, imm)
177 #  define SLLIW(rd, rs1, imm)		Rtype(27, rd, 1, rs1, imm, 0)
178 #  define SRLIW(rd, rs1, imm)		Rtype(27, rd, 3, rs1, imm, 0)
179 #  define SRAIW(rd, rs1, imm)		Rtype(27, rd, 3, rs1, imm, 32)
180 #  define ADDW(rd, rs1, imm)		Rtype(59, rd, 0, rs1, imm, 0)
181 #  define SUBW(rd, rs1, imm)		Rtype(59, rd, 0, rs1, imm, 32)
182 #  define SLLW(rd, rs1, imm)		Rtype(59, rd, 1, rs1, imm, 0)
183 #  define SRLW(rd, rs1, imm)		Rtype(59, rd, 5, rs1, imm, 0)
184 #  define SRAW(rd, rs1, imm)		Rtype(59, rd, 5, rs1, imm, 32)
185 /*
186  * RV32M Standard Extension
187  */
188 #  define MUL(rd, rs1, rs2)		Rtype(51, rd, 0, rs1, rs2, 1)
189 #  define MULH(rd, rs1, rs2)		Rtype(51, rd, 1, rs1, rs2, 1)
190 #  define MULHSU(rd, rs1, rs2)		Rtype(51, rd, 2, rs1, rs2, 1)
191 #  define MULHU(rd, rs1, rs2)		Rtype(51, rd, 3, rs1, rs2, 1)
192 #  define DIV(rd, rs1, rs2)		Rtype(51, rd, 4, rs1, rs2, 1)
193 #  define DIVU(rd, rs1, rs2)		Rtype(51, rd, 5, rs1, rs2, 1)
194 #  define REM(rd, rs1, rs2)		Rtype(51, rd, 6, rs1, rs2, 1)
195 #  define REMU(rd, rs1, rs2)		Rtype(51, rd, 7, rs1, rs2, 1)
196 /*
197  * RV64M Standard Extension (in addition to RV32M)
198  */
199 #  define MULW(rd, rs1, rs2)		Rtype(59, rd, 0, rs1, rs2, 1)
200 #  define DIVW(rd, rs1, rs2)		Rtype(59, rd, 4, rs1, rs2, 1)
201 #  define DIVUW(rd, rs1, rs2)		Rtype(59, rd, 5, rs1, rs2, 1)
202 #  define REMW(rd, rs1, rs2)		Rtype(59, rd, 6, rs1, rs2, 1)
203 #  define REMUW(rd, rs1, rs2)		Rtype(59, rd, 7, rs1, rs2, 1)
204 /*
205  * RV32A Standard Extension
206  */
207 #  define LR_W(rd, rs1)			R4type(47, rd, 2, rs1, 0, 0, 2)
208 #  define SC_W(rd, rs1, rs2)		R4type(47, rd, 2, rs1, rs2, 0, 3)
209 #  define AMOSWAP_W(rd, rs1, rs2)	R4type(47, rd, 2, rs1, rs2, 0, 1)
210 #  define AMOADD_W(rd, rs1, rs2)	R4type(47, rd, 2, rs1, rs2, 0, 0)
211 #  define AMOXOR_W(rd, rs1, rs2)	R4type(47, rd, 2, rs1, rs2, 0, 4)
212 #  define AMOAND_W(rd, rs1, rs2)	R4type(47, rd, 2, rs1, rs2, 0, 12)
213 #  define AMOOR_W(rd, rs1, rs2)		R4type(47, rd, 2, rs1, rs2, 0, 8)
214 #  define AMOMIN_W(rd, rs1, rs2)	R4type(47, rd, 2, rs1, rs2, 0, 16)
215 #  define AMOMAX_W(rd, rs1, rs2)	R4type(47, rd, 2, rs1, rs2, 0, 20)
216 #  define AMOMINU_W(rd, rs1, rs2)	R4type(47, rd, 2, rs1, rs2, 0, 24)
217 #  define AMOMAXU_W(rd, rs1, rs2)	R4type(47, rd, 2, rs1, rs2, 0, 28)
218 /*
219  * RV64A Standard Extension (in addition to RV32A)
220  */
221 #  define LR_D(rd, rs1)			R4type(47, rd, 3, rs1, 0, 0, 2)
222 #  define SC_D(rd, rs1, rs2)		R4type(47, rd, 3, rs1, rs2, 0, 3)
223 #  define AMOSWAP_D(rd, rs1, rs2)	R4type(47, rd, 3, rs1, rs2, 0, 1)
224 #  define AMOADD_D(rd, rs1, rs2)	R4type(47, rd, 3, rs1, rs2, 0, 0)
225 #  define AMOXOR_D(rd, rs1, rs2)	R4type(47, rd, 3, rs1, rs2, 0, 4)
226 #  define AMOAND_D(rd, rs1, rs2)	R4type(47, rd, 3, rs1, rs2, 0, 12)
227 #  define AMOOR_D(rd, rs1, rs2)		R4type(47, rd, 3, rs1, rs2, 0, 8)
228 #  define AMOMIN_D(rd, rs1, rs2)	R4type(47, rd, 3, rs1, rs2, 0, 16)
229 #  define AMOMAX_D(rd, rs1, rs2)	R4type(47, rd, 3, rs1, rs2, 0, 20)
230 #  define AMOMINU_D(rd, rs1, rs2)	R4type(47, rd, 3, rs1, rs2, 0, 24)
231 #  define AMOMAXU_D(rd, rs1, rs2)	R4type(47, rd, 3, rs1, rs2, 0, 28)
232 /*
233  * Pseudo Instructions
234  */
235 #  define NOP()				ADDI(_ZERO_REGNO, _ZERO_REGNO, 0)
236 #  define MV(r0, r1)			ADDI(r0, r1, 0)
237 #  define NOT(r0, r1)			XORI(r0, r1, -1)
238 #  define NEG(r0, r1)			SUB(r0, _ZERO_REGNO, r1)
239 #  define NEGW(r0, r1)			SUBW(r0, _ZERO_REGNO, r1)
240 #  define SEXT_W(r0, r1)		ADDIW(r0, r1, 0)
241 #  define RET()				JALR(0, 1, 0)
242 
243 /*
244  * Enconding functions
245  */
246 #  define Rtype(op, rd, fct, rs1, rs2, fct2)			\
247 	_Rtype(_jit, op, rd, fct, rs1, rs2, fct2)
248 static void _Rtype(jit_state_t*, jit_int32_t, jit_int32_t,
249 		   jit_int32_t, jit_int32_t, jit_int32_t, jit_int32_t);
250 #  define R4type(op, rd, fct, rs1,rs2,fct2,rs3)			\
251 	_R4type(_jit, op, rd, fct, rs1, rs2, fct2, rs3)
252 static void _R4type(jit_state_t*, jit_int32_t, jit_int32_t, jit_int32_t,
253 		    jit_int32_t, jit_int32_t, jit_int32_t, jit_int32_t);
254 #  define Itype(op, rd, fct, rs1, imm)				\
255 	_Itype(_jit, op, rd, fct, rs1, imm)
256 static void _Itype(jit_state_t*, jit_int32_t, jit_int32_t,
257 		   jit_int32_t, jit_int32_t, jit_int32_t);
258 #  if __WORDSIZE == 64
259 #  define IStype(op, rd, fct, rs1, sh, imm)			\
260 	_IStype(_jit, op, rd, fct, rs1, sh, imm)
261 static void _IStype(jit_state_t*, jit_int32_t, jit_int32_t,
262 		   jit_int32_t, jit_int32_t, jit_int32_t,jit_int32_t);
263 #  endif
264 #  define Stype(op, fct, rs1, rs2, imm)				\
265 	_Stype(_jit, op, fct, rs1, rs2, imm)
266 static void _Stype(jit_state_t*, jit_int32_t, jit_int32_t,
267 		   jit_int32_t, jit_int32_t, jit_int32_t);
268 #  define Btype(op, fct, rs1, rs2, imm)				\
269 	_Btype(_jit, op, fct, rs1, rs2, imm)
270 static void _Btype(jit_state_t*, jit_int32_t, jit_int32_t,
271 		   jit_int32_t, jit_int32_t, jit_int32_t);
272 #  define Utype(op, rd, imm)		_Utype(_jit, op, rd, imm)
273 static void _Utype(jit_state_t*, jit_int32_t, jit_int32_t, jit_int32_t);
274 #  define Jtype(op, rd, imm)		_Jtype(_jit, op, rd, imm)
275 static void _Jtype(jit_state_t*, jit_int32_t, jit_int32_t, jit_int32_t);
276 /*
277  * Lightning instructions
278  */
279 #  define nop(im)			_nop(_jit, im)
280 static void _nop(jit_state_t*, jit_int32_t);
281 #  define addr(r0, r1, r2)		ADD(r0, r1, r2)
282 #  define addi(r0, r1, im)		_addi(_jit, r0, r1, im)
283 static void _addi(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
284 #  define addcr(r0, r1, r2)		_addcr(_jit, r0, r1, r2)
285 static void _addcr(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
286 #  define addci(r0, r1, im)		_addci(_jit, r0, r1, im)
287 static void _addci(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
288 #  define addxr(r0, r1, r2)		_addxr(_jit, r0, r1, r2)
289 static void _addxr(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
290 #  define addxi(r0, r1, im)		_addxi(_jit, r0, r1, im)
291 static void _addxi(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
292 #  define subr(r0, r1, r2)		SUB(r0, r1, r2)
293 #  define subi(r0, r1, im)		_subi(_jit, r0, r1, im)
294 static void _subi(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
295 #  define subcr(r0, r1, r2)		_subcr(_jit, r0, r1, r2)
296 static void _subcr(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
297 #  define subci(r0, r1, im)		_subci(_jit, r0, r1, im)
298 static void _subci(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
299 #  define subxr(r0, r1, r2)		_subxr(_jit, r0, r1, r2)
300 static void _subxr(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
301 #  define subxi(r0, r1, im)		_subxi(_jit, r0, r1, im)
302 static void _subxi(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
303 #  define rsbi(r0, r1, im)		_rsbi(_jit, r0, r1, im)
304 static void _rsbi(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
305 #  define mulr(r0, r1, r2)		MUL(r0, r1, r2)
306 #  define muli(r0, r1, im)		_muli(_jit, r0, r1, im)
307 static void _muli(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
308 #  define divr(r0, r1, r2)		DIV(r0, r1, r2)
309 #  define divi(r0, r1, im)		_divi(_jit, r0, r1, im)
310 static void _divi(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
311 #  define divr_u(r0, r1, r2)		DIVU(r0, r1, r2)
312 #  define divi_u(r0, r1, im)		_divi_u(_jit, r0, r1, im)
313 static void _divi_u(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
314 #  define remr(r0, r1, r2)		REM(r0, r1, r2)
315 #  define remi(r0, r1, im)		_remi(_jit, r0, r1, im)
316 static void _remi(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
317 #  define remr_u(r0, r1, r2)		REMU(r0, r1, r2)
318 #  define remi_u(r0, r1, im)		_remi_u(_jit, r0, r1, im)
319 static void _remi_u(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
320 #  define qmulr(r0, r1, r2, r3)		_qmulr(_jit,r0,r1,r2,r3)
321 static void _qmulr(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t);
322 #  define qmuli(r0, r1, r2, i0)		_qmuli(_jit,r0,r1,r2,i0)
323 static void _qmuli(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t,jit_word_t);
324 #  define qmulr_u(r0, r1, r2, r3)	_qmulr_u(_jit,r0,r1,r2,r3)
325 static void _qmulr_u(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t);
326 #  define qmuli_u(r0, r1, r2, i0)	_qmuli_u(_jit,r0,r1,r2,i0)
327 static void _qmuli_u(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t,jit_word_t);
328 static void _iqdivr(jit_state_t*,jit_bool_t,
329 		    jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t);
330 #  define qdivr(r0,r1,r2,r3)		_iqdivr(_jit,1,r0,r1,r2,r3)
331 #  define qdivr_u(r0,r1,r2,r3)		_iqdivr(_jit,0,r0,r1,r2,r3)
332 static void _iqdivr(jit_state_t*,jit_bool_t,
333 		    jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t);
334 #  define qdivi(r0,r1,r2,i0)		_qdivi(_jit,r0,r1,r2,i0)
335 static void _qdivi(jit_state_t*,jit_int32_t,
336 		   jit_int32_t,jit_int32_t,jit_word_t);
337 #  define qdivi_u(r0,r1,r2,i0)		_qdivi_u(_jit,r0,r1,r2,i0)
338 static void _qdivi_u(jit_state_t*,jit_int32_t,
339 		     jit_int32_t,jit_int32_t,jit_word_t);
340 #  define lshr(r0, r1, r2)		SLL(r0, r1, r2)
341 #  define lshi(r0, r1, im)		_lshi(_jit, r0, r1, im)
342 static void _lshi(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
343 #  define rshr(r0, r1, r2)		SRA(r0, r1, r2)
344 #  define rshi(r0, r1, im)		_rshi(_jit, r0, r1, im)
345 static void _rshi(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
346 #  define rshr_u(r0, r1, r2)		SRL(r0, r1, r2)
347 #  define rshi_u(r0, r1, im)		_rshi_u(_jit, r0, r1, im)
348 static void _rshi_u(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
349 #  define negr(r0, r1)			NEG(r0, r1)
350 #  define comr(r0, r1)			NOT(r0, r1)
351 #  define andr(r0, r1, r2)		AND(r0, r1, r2)
352 #  define andi(r0, r1, im)		_andi(_jit, r0, r1, im)
353 static void _andi(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
354 #  define orr(r0, r1, r2)		OR(r0, r1, r2)
355 #  define ori(r0, r1, im)		_ori(_jit, r0, r1, im)
356 static void _ori(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
357 #  define xorr(r0, r1, r2)		XOR(r0, r1, r2)
358 #  define xori(r0, r1, im)		_xori(_jit, r0, r1, im)
359 static void _xori(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
360 #  define ldr_c(r0, r1)			LB(r0, r1, 0)
361 #  define ldi_c(r0, im)			_ldi_c(_jit, r0, im)
362 static void _ldi_c(jit_state_t*,jit_int32_t,jit_word_t);
363 #  define ldr_uc(r0, r1)		LBU(r0, r1, 0)
364 #  define ldi_uc(r0, im)		_ldi_uc(_jit, r0, im)
365 static void _ldi_uc(jit_state_t*,jit_int32_t,jit_word_t);
366 #  define ldr_s(r0, r1)			LH(r0, r1, 0)
367 #  define ldi_s(r0, im)			_ldi_s(_jit, r0, im)
368 static void _ldi_s(jit_state_t*,jit_int32_t,jit_word_t);
369 #  define ldr_us(r0, r1)		LHU(r0, r1, 0)
370 #  define ldi_us(r0, im)		_ldi_us(_jit, r0, im)
371 static void _ldi_us(jit_state_t*,jit_int32_t,jit_word_t);
372 #  define ldr_i(r0, r1)			LW(r0, r1, 0)
373 #  define ldi_i(r0, im)			_ldi_i(_jit, r0, im)
374 static void _ldi_i(jit_state_t*,jit_int32_t,jit_word_t);
375 #  define ldr_ui(r0, r1)		LWU(r0, r1, 0)
376 #  define ldi_ui(r0, im)		_ldi_ui(_jit, r0, im)
377 static void _ldi_ui(jit_state_t*,jit_int32_t,jit_word_t);
378 #  define ldr_l(r0, r1)			LD(r0, r1, 0)
379 #  define ldi_l(r0, im)			_ldi_l(_jit, r0, im)
380 static void _ldi_l(jit_state_t*,jit_int32_t,jit_word_t);
381 #  define ldxr_c(r0, r1, r2)		_ldxr_c(_jit, r0, r1, r2)
382 static void _ldxr_c(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
383 #  define ldxi_c(r0, r1, im)		_ldxi_c(_jit, r0, r1, im)
384 static void _ldxi_c(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
385 #  define ldxr_uc(r0, r1, r2)		_ldxr_uc(_jit, r0, r1, r2)
386 static void _ldxr_uc(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
387 #  define ldxi_uc(r0, r1, im)		_ldxi_uc(_jit, r0, r1, im)
388 static void _ldxi_uc(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
389 #  define ldxr_s(r0, r1, r2)		_ldxr_s(_jit, r0, r1, r2)
390 static void _ldxr_s(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
391 #  define ldxi_s(r0, r1, im)		_ldxi_s(_jit, r0, r1, im)
392 static void _ldxi_s(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
393 #  define ldxr_us(r0, r1, r2)		_ldxr_us(_jit, r0, r1, r2)
394 static void _ldxr_us(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
395 #  define ldxi_us(r0, r1, im)		_ldxi_us(_jit, r0, r1, im)
396 static void _ldxi_us(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
397 #  define ldxr_i(r0, r1, r2)		_ldxr_i(_jit, r0, r1, r2)
398 static void _ldxr_i(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
399 #  define ldxi_i(r0, r1, im)		_ldxi_i(_jit, r0, r1, im)
400 static void _ldxi_i(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
401 #  define ldxr_ui(r0, r1, r2)		_ldxr_ui(_jit, r0, r1, r2)
402 static void _ldxr_ui(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
403 #  define ldxi_ui(r0, r1, im)		_ldxi_ui(_jit, r0, r1, im)
404 static void _ldxi_ui(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
405 #  define ldxr_l(r0, r1, r2)		_ldxr_l(_jit, r0, r1, r2)
406 static void _ldxr_l(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
407 #  define ldxi_l(r0, r1, im)		_ldxi_l(_jit, r0, r1, im)
408 static void _ldxi_l(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
409 #  define str_c(r0, r1)			SB(r0, r1, 0)
410 #  define sti_c(im, r0)			_sti_c(_jit, im, r0)
411 static void _sti_c(jit_state_t*,jit_word_t,jit_int32_t);
412 #  define str_s(r0, r1)			SH(r0, r1, 0)
413 #  define sti_s(im, r0)			_sti_s(_jit, im, r0)
414 static void _sti_s(jit_state_t*,jit_word_t,jit_int32_t);
415 #  define str_i(r0, r1)			SW(r0, r1, 0)
416 #  define sti_i(im, r0)			_sti_i(_jit, im, r0)
417 static void _sti_i(jit_state_t*,jit_word_t,jit_int32_t);
418 #  define str_l(r0, r1)			SD(r0, r1, 0)
419 #  define sti_l(im, r0)			_sti_l(_jit, im, r0)
420 static void _sti_l(jit_state_t*,jit_word_t,jit_int32_t);
421 #  define stxr_c(r0, r1, r2)		_stxr_c(_jit, r0, r1, r2)
422 static void _stxr_c(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
423 #  define stxi_c(i0, r0, r1)		_stxi_c(_jit, i0, r0, r1)
424 static void _stxi_c(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
425 #  define stxr_s(r0, r1, r2)		_stxr_s(_jit, r0, r1, r2)
426 static void _stxr_s(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
427 #  define stxi_s(i0, r0, r1)		_stxi_s(_jit, i0, r0, r1)
428 static void _stxi_s(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
429 #  define stxr_i(r0, r1, r2)		_stxr_i(_jit, r0, r1, r2)
430 static void _stxr_i(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
431 #  define stxi_i(i0, r0, r1)		_stxi_i(_jit, i0, r0, r1)
432 static void _stxi_i(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
433 #  define stxr_l(r0, r1, r2)		_stxr_l(_jit, r0, r1, r2)
434 static void _stxr_l(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
435 #  define stxi_l(i0, r0, r1)		_stxi_l(_jit, i0, r0, r1)
436 static void _stxi_l(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
437 #  define htonr_us(r0, r1)		_htonr_us(_jit, r0, r1)
438 static void _htonr_us(jit_state_t*,jit_int32_t,jit_int32_t);
439 #  define htonr_ui(r0, r1)		_htonr_ui(_jit, r0, r1)
440 static void _htonr_ui(jit_state_t*,jit_int32_t,jit_int32_t);
441 #  define htonr_ul(r0, r1)		_htonr_ul(_jit, r0, r1)
442 static void _htonr_ul(jit_state_t*,jit_int32_t,jit_int32_t);
443 #  define extr_c(r0, r1)		_extr_c(_jit, r0, r1)
444 static void _extr_c(jit_state_t*,jit_int32_t,jit_int32_t);
445 #  define extr_uc(r0, r1)		andi(r0, r1, 0xff)
446 #  define extr_s(r0, r1)		_extr_s(_jit, r0, r1)
447 static void _extr_s(jit_state_t*,jit_int32_t,jit_int32_t);
448 #  define extr_us(r0, r1)		_extr_us(_jit, r0, r1)
449 static void _extr_us(jit_state_t*,jit_int32_t,jit_int32_t);
450 #  define extr_i(r0, r1)		SEXT_W(r0, r1)
451 #  define extr_ui(r0, r1)		_extr_ui(_jit, r0, r1)
452 static void _extr_ui(jit_state_t*,jit_int32_t,jit_int32_t);
453 #  define movr(r0, r1)			MV(r0, r1)
454 #  define movi(r0, im)			_movi(_jit, r0, im)
455 static void _movi(jit_state_t*,jit_int32_t,jit_word_t);
456 #  define movi_p(r0, im)		_movi_p(_jit, r0, im)
457 static jit_word_t _movi_p(jit_state_t*,jit_int32_t,jit_word_t);
458 #  define ltr(r0, r1, r2)		SLT(r0, r1, r2)
459 #  define lti(r0, r1, im)		_lti(_jit, r0, r1, im)
460 static void _lti(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
461 #  define ltr_u(r0, r1, r2)		SLTU(r0, r1, r2)
462 #  define lti_u(r0, r1, im)		_lti_u(_jit, r0, r1, im)
463 static void _lti_u(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
464 #  define ler(r0, r1, r2)		_ler(_jit, r0, r1, r2)
465 static void _ler(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
466 #  define lei(r0, r1, im)		_lei(_jit, r0, r1, im)
467 static void _lei(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
468 #  define ler_u(r0, r1, r2)		_ler_u(_jit, r0, r1, r2)
469 static void _ler_u(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
470 #  define lei_u(r0, r1, im)		_lei_u(_jit, r0, r1, im)
471 static void _lei_u(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
472 #  define eqr(r0, r1, r2)		_eqr(_jit, r0, r1, r2)
473 static void _eqr(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
474 #  define eqi(r0, r1, im)		_eqi(_jit, r0, r1, im)
475 static void _eqi(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
476 #  define ger(r0, r1, r2)		_ger(_jit, r0, r1, r2)
477 static void _ger(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
478 #  define gei(r0, r1, r2)		_gei(_jit, r0, r1, r2)
479 static void _gei(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
480 #  define ger_u(r0, r1, r2)		_ger_u(_jit, r0, r1, r2)
481 static void _ger_u(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
482 #  define gei_u(r0, r1, im)		_gei_u(_jit, r0, r1, im)
483 static void _gei_u(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
484 #  define gtr(r0, r1, r2)		SLT(r0, r2, r1)
485 #  define gti(r0, r1, im)		_gti(_jit, r0, r1, im)
486 static void _gti(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
487 #  define gtr_u(r0, r1, r2)		SLTU(r0, r2, r1)
488 #  define gti_u(r0, r1, im)		_gti_u(_jit, r0, r1, im)
489 static void _gti_u(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
490 #  define ner(r0, r1, r2)		_ner(_jit, r0, r1, r2)
491 static void _ner(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t);
492 #  define nei(r0, r1, im)		_nei(_jit, r0, r1, im)
493 static void _nei(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
494 #  define bltr(br, r0, r1)		_bltr(_jit, br, r0, r1)
495 static jit_word_t _bltr(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
496 #  define blti(br, r0, im)		_blti(_jit, br, r0, im)
497 static jit_word_t _blti(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
498 #  define bltr_u(br, r0, r1)		_bltr_u(_jit, br, r0, r1)
499 static jit_word_t _bltr_u(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
500 #  define blti_u(br, r0, im)		_blti_u(_jit, br, r0, im)
501 static jit_word_t _blti_u(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
502 #  define bler(br, r0, r1)		_bler(_jit, br, r0, r1)
503 static jit_word_t _bler(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
504 #  define blei(br, r0, im)		_blei(_jit, br, r0, im)
505 static jit_word_t _blei(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
506 #  define bler_u(br, r0, r1)		_bler_u(_jit, br, r0, r1)
507 static jit_word_t _bler_u(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
508 #  define blei_u(br, r0, im)		_blei_u(_jit, br, r0, im)
509 static jit_word_t _blei_u(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
510 #  define beqr(br, r0, r1)		_beqr(_jit, br, r0, r1)
511 static jit_word_t _beqr(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
512 #  define beqi(br, r0, im)		_beqi(_jit, br, r0, im)
513 static jit_word_t _beqi(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
514 #  define bger(br, r0, r1)		_bger(_jit, br, r0, r1)
515 static jit_word_t _bger(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
516 #  define bgei(br, r0, im)		_bgei(_jit, br, r0, im)
517 static jit_word_t _bgei(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
518 #  define bger_u(br, r0, r1)		_bger_u(_jit, br, r0, r1)
519 static jit_word_t _bger_u(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
520 #  define bgei_u(br, r0, im)		_bgei_u(_jit, br, r0, im)
521 static jit_word_t _bgei_u(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
522 #  define bgtr(br, r0, r1)		_bgtr(_jit, br, r0, r1)
523 static jit_word_t _bgtr(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
524 #  define bgti(br, r0, im)		_bgti(_jit, br, r0, im)
525 static jit_word_t _bgti(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
526 #  define bgtr_u(br, r0, r1)		_bgtr_u(_jit, br, r0, r1)
527 static jit_word_t _bgtr_u(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
528 #  define bgti_u(br, r0, im)		_bgti_u(_jit, br, r0, im)
529 static jit_word_t _bgti_u(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
530 #  define bner(br, r0, r1)		_bner(_jit, br, r0, r1)
531 static jit_word_t _bner(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
532 #  define bnei(br, r0, im)		_bnei(_jit, br, r0, im)
533 static jit_word_t _bnei(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
534 #  define boaddr(br, r0, r1)		_boaddr(_jit, br, r0, r1)
535 #  define boaddi(br, r0, im)		_boaddi(_jit, br, r0, im)
536 static jit_word_t _boaddr(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
537 static jit_word_t _boaddi(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
538 #  define boaddr_u(br, r0, r1)		_boaddr_u(_jit, br, r0, r1)
539 #  define boaddi_u(br, r0, im)		_boaddi_u(_jit, br, r0, im)
540 static jit_word_t _boaddr_u(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
541 static jit_word_t _boaddi_u(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
542 #  define bxaddr(br, r0, r1)		_bxaddr(_jit, br, r0, r1)
543 #  define bxaddi(br, r0, im)		_bxaddi(_jit, br, r0, im)
544 static jit_word_t _bxaddr(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
545 static jit_word_t _bxaddi(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
546 #  define bxaddr_u(br, r0, r1)		_bxaddr_u(_jit, br, r0, r1)
547 #  define bxaddi_u(br, r0, im)		_bxaddi_u(_jit, br, r0, im)
548 static jit_word_t _bxaddr_u(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
549 static jit_word_t _bxaddi_u(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
550 #  define bosubr(br, r0, r1)		_bosubr(_jit, br, r0, r1)
551 #  define bosubi(br, r0, im)		_bosubi(_jit, br, r0, im)
552 static jit_word_t _bosubr(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
553 static jit_word_t _bosubi(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
554 #  define bosubr_u(br, r0, r1)		_bosubr_u(_jit, br, r0, r1)
555 #  define bosubi_u(br, r0, im)		_bosubi_u(_jit, br, r0, im)
556 static jit_word_t _bosubr_u(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
557 static jit_word_t _bosubi_u(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
558 #  define bxsubr(br, r0, r1)		_bxsubr(_jit, br, r0, r1)
559 #  define bxsubi(br, r0, im)		_bxsubi(_jit, br, r0, im)
560 static jit_word_t _bxsubr(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
561 static jit_word_t _bxsubi(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
562 #  define bxsubr_u(br, r0, r1)		_bxsubr_u(_jit, br, r0, r1)
563 #  define bxsubi_u(br, r0, im)		_bxsubi_u(_jit, br, r0, im)
564 static jit_word_t _bxsubr_u(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
565 static jit_word_t _bxsubi_u(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
566 #  define bmsr(br, r0, r1)		_bmsr(_jit, br, r0, r1)
567 #  define bmsi(br, r0, im)		_bmsi(_jit, br, r0, im)
568 static jit_word_t _bmsr(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
569 static jit_word_t _bmsi(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
570 #  define bmcr(br, r0, r1)		_bmcr(_jit, br, r0, r1)
571 #  define bmci(br, r0, im)		_bmci(_jit, br, r0, im)
572 static jit_word_t _bmcr(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
573 static jit_word_t _bmci(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
574 #  define jmpr(r0)			JALR(_ZERO_REGNO, r0, 0)
575 #  define jmpi(im)			_jmpi(_jit, im)
576 static void _jmpi(jit_state_t*,jit_word_t);
577 #  define jmpi_p(im)			_jmpi_p(_jit, im)
578 static jit_word_t _jmpi_p(jit_state_t*,jit_word_t);
579 #  define callr(r0)			JALR(_RA_REGNO, r0, 0)
580 #  define calli(im)			_calli(_jit, im)
581 static void _calli(jit_state_t*,jit_word_t);
582 #  define calli_p(im)		_calli_p(_jit, im)
583 static jit_word_t _calli_p(jit_state_t*,jit_word_t);
584 #  define prolog(i0)			_prolog(_jit,i0)
585 static void _prolog(jit_state_t*,jit_node_t*);
586 #  define epilog(i0)			_epilog(_jit,i0)
587 static void _epilog(jit_state_t*,jit_node_t*);
588 #  define vastart(r0)			_vastart(_jit, r0)
589 static void _vastart(jit_state_t*, jit_int32_t);
590 #  define vaarg(r0, r1)			_vaarg(_jit, r0, r1)
591 static void _vaarg(jit_state_t*, jit_int32_t, jit_int32_t);
592 #define patch_abs(instr,label)		_patch_at(_jit,instr,label)
593 #define patch_at(instr,label)		_patch_at(_jit,instr,label)
594 static void _patch_at(jit_state_t*,jit_word_t,jit_word_t);
595 #endif		/* PROTO */
596 
597 #if CODE
598 static void
_Rtype(jit_state_t * _jit,jit_int32_t op,jit_int32_t rd,jit_int32_t fct,jit_int32_t rs1,jit_int32_t rs2,jit_int32_t fct2)599 _Rtype(jit_state_t *_jit, jit_int32_t op, jit_int32_t rd,
600        jit_int32_t fct, jit_int32_t rs1, jit_int32_t rs2, jit_int32_t fct2)
601 {
602     instr_t	i;
603     assert(!(op   & ~0x7f));
604     assert(!(rd   & ~0x1f));
605     assert(!(fct  & ~0x07));
606     assert(!(rs1  & ~0x1f));
607     assert(!(rs2  & ~0x1f));
608     assert(!(fct2 & ~0x7f));
609     i.R.opcode	= op;
610     i.R.rd	= rd;
611     i.R.funct3	= fct;
612     i.R.rs1	= rs1;
613     i.R.rs2	= rs2;
614     i.R.funct7	= fct2;
615     ii(i.w);
616 }
617 
618 static void
_R4type(jit_state_t * _jit,jit_int32_t op,jit_int32_t rd,jit_int32_t fct,jit_int32_t rs1,jit_int32_t rs2,jit_int32_t fct2,jit_int32_t rs3)619 _R4type(jit_state_t *_jit, jit_int32_t op, jit_int32_t rd, jit_int32_t fct,
620 	jit_int32_t rs1, jit_int32_t rs2, jit_int32_t fct2, jit_int32_t rs3)
621 {
622     instr_t	i;
623     assert(!(op   & ~0x7f));
624     assert(!(rd   & ~0x1f));
625     assert(!(fct  & ~0x07));
626     assert(!(rs1  & ~0x1f));
627     assert(!(rs2  & ~0x1f));
628     assert(!(fct2 & ~0x03));
629     assert(!(rs3  & ~0x1f));
630     i.R4.opcode	= op;
631     i.R4.rd	= rd;
632     i.R4.funct3	= fct;
633     i.R4.rs1	= rs1;
634     i.R4.rs2	= rs2;
635     i.R4.funct2	= fct2;
636     i.R4.rs3	= rs3;
637     ii(i.w);
638 }
639 
640 static void
_Itype(jit_state_t * _jit,jit_int32_t op,jit_int32_t rd,jit_int32_t fct,jit_int32_t rs1,jit_int32_t imm)641 _Itype(jit_state_t *_jit, jit_int32_t op, jit_int32_t rd,
642        jit_int32_t fct, jit_int32_t rs1, jit_int32_t imm)
643 {
644     instr_t	i;
645     assert(!(op  &  ~0x7f));
646     assert(!(rd  &  ~0x1f));
647     assert(!(fct &  ~0x07));
648     assert(!(rs1 &  ~0x1f));
649     assert(simm12_p(imm));
650     i.I.opcode	= op;
651     i.I.rd	= rd;
652     i.I.funct3	= fct;
653     i.I.rs1	= rs1;
654     i.I.imm11_0	= imm;
655     ii(i.w);
656 }
657 
658 #  if __WORDSIZE == 64
659 static void
_IStype(jit_state_t * _jit,jit_int32_t op,jit_int32_t rd,jit_int32_t fct,jit_int32_t rs1,jit_int32_t sh,jit_int32_t imm)660 _IStype(jit_state_t *_jit, jit_int32_t op, jit_int32_t rd,
661        jit_int32_t fct, jit_int32_t rs1, jit_int32_t sh, jit_int32_t imm)
662 {
663     instr_t	i;
664     assert(!(op  &  ~0x7f));
665     assert(!(rd  &  ~0x1f));
666     assert(!(fct &  ~0x07));
667     assert(!(rs1 &  ~0x1f));
668     assert(!(sh  &  ~0x3f));
669     assert(simm6_p(imm));
670     i.IS.opcode	= op;
671     i.IS.rd	= rd;
672     i.IS.funct3	= fct;
673     i.IS.rs1	= rs1;
674     i.IS.shamt	= sh;
675     i.IS.imm6_0 = imm;
676     ii(i.w);
677 }
678 #  endif
679 
680 static void
_Stype(jit_state_t * _jit,jit_int32_t op,jit_int32_t fct,jit_int32_t rs1,jit_int32_t rs2,jit_int32_t imm)681 _Stype(jit_state_t *_jit, jit_int32_t op, jit_int32_t fct,
682        jit_int32_t rs1, jit_int32_t rs2, jit_int32_t imm)
683 {
684     instr_t	i;
685     assert(!(op  &  ~0x7f));
686     assert(!(fct &  ~0x07));
687     assert(!(rs1 &  ~0x1f));
688     assert(!(rs2 &  ~0x1f));
689     assert(simm12_p(imm));
690     i.S.opcode	= op;
691     i.S.imm4_0	= imm & 0x1f;
692     i.S.funct3	= fct;
693     i.S.rs1	= rs1;
694     i.S.rs2	= rs2;
695     i.S.imm11_5	= (imm >> 5) & 0x7f;
696     ii(i.w);
697 }
698 
699 static void
_Btype(jit_state_t * _jit,jit_int32_t op,jit_int32_t fct,jit_int32_t rs1,jit_int32_t rs2,jit_int32_t imm)700 _Btype(jit_state_t *_jit, jit_int32_t op, jit_int32_t fct,
701        jit_int32_t rs1, jit_int32_t rs2, jit_int32_t imm)
702 {
703     instr_t	i;
704     assert(!(op  & ~0x7f));
705     assert(!(fct & ~0x07));
706     assert(!(rs1 & ~0x1f));
707     assert(!(rs2 & ~0x1f));
708     assert(!(imm & 1) && simm12_p(imm));
709     i.B.opcode	= op;
710     i.B.imm11	= (imm >> 11) & 0x1;
711     i.B.imm4_1	= (imm >>  1) & 0xf;
712     i.B.funct3	= fct;
713     i.B.rs1	= rs1;
714     i.B.rs2	= rs2;
715     i.B.imm10_5	= (imm >>  5) & 0x3f;
716     i.B.imm12	= (imm >> 12) & 0x1;
717     ii(i.w);
718 }
719 
720 static void
_Utype(jit_state_t * _jit,jit_int32_t op,jit_int32_t rd,jit_int32_t imm)721 _Utype(jit_state_t *_jit, jit_int32_t op, jit_int32_t rd, jit_int32_t imm)
722 {
723     instr_t	i;
724     assert(!(op	& ~0x7f));
725     assert(!(rd	& ~0x1f));
726     assert(simm20_p(imm));
727     i.U.opcode	= op;
728     i.U.rd	= rd;
729     i.U.imm12_31= imm;
730     ii(i.w);
731 }
732 
733 static void
_Jtype(jit_state_t * _jit,jit_int32_t op,jit_int32_t rd,jit_int32_t imm)734 _Jtype(jit_state_t *_jit, jit_int32_t op, jit_int32_t rd, jit_int32_t imm)
735 {
736     instr_t	i;
737     assert(!(op & ~0x7f));
738     assert(!(rd & ~0x1f));
739     assert(!(imm & 1) && imm <= 1048575 && imm >= -1048576);
740     i.J.opcode	= op;
741     i.J.rd	= rd;
742     i.J.imm19_12= (imm >> 12) &  0xff;
743     i.J.imm11	= (imm >> 11) &   0x1;
744     i.J.imm10_1	= (imm >>  1) & 0x3ff;
745     i.J.imm20	= (imm >> 20) &   0x1;
746     ii(i.w);
747 }
748 
749 static void
_nop(jit_state_t * _jit,jit_int32_t im)750 _nop(jit_state_t *_jit, jit_int32_t im)
751 {
752     for (; im > 0; im -= 4)
753 	NOP();
754     assert(im == 0);
755 }
756 
757 static void
_addi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)758 _addi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
759 {
760     if (simm12_p(i0))
761 	ADDI(r0, r1, i0);
762     else {
763 	jit_int32_t	t0;
764 	t0 = jit_get_reg(jit_class_gpr);
765 	movi(rn(t0), i0);
766 	addr(r0, r1, rn(t0));
767 	jit_unget_reg(t0);
768     }
769 }
770 
771 static void
_addcr(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)772 _addcr(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_int32_t r2)
773 {
774     jit_int32_t		t0;
775     if (jit_carry == _NOREG)
776 	jit_carry = jit_get_reg(jit_class_gpr);
777     if (r0 == r1) {
778 	t0 = jit_get_reg(jit_class_gpr);
779 	addr(rn(t0), r1, r2);
780 	SLTU(rn(jit_carry), rn(t0), r1);
781 	movr(r0, rn(t0));
782 	jit_unget_reg(t0);
783     }
784     else {
785 	addr(r0, r1, r2);
786 	SLTU(rn(jit_carry), r0, r1);
787     }
788 }
789 
790 static void
_addci(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)791 _addci(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
792 {
793     jit_int32_t		t0;
794     if (jit_carry == _NOREG)
795 	jit_carry = jit_get_reg(jit_class_gpr);
796     if (r0 == r1) {
797 	t0 = jit_get_reg(jit_class_gpr);
798 	addi(rn(t0), r1, i0);
799 	SLTU(rn(jit_carry), rn(t0), r1);
800 	movr(r0, rn(t0));
801 	jit_unget_reg(t0);
802     }
803     else {
804 	addi(r0, r1, i0);
805 	SLTU(rn(jit_carry), r0, r1);
806     }
807 }
808 
809 static void
_addxr(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)810 _addxr(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_int32_t r2)
811 {
812     jit_int32_t		t0;
813     assert(jit_carry != _NOREG);
814     t0 = jit_get_reg(jit_class_gpr);
815     movr(rn(t0), rn(jit_carry));
816     addcr(r0, r1, r2);
817     addcr(r0, r0, rn(t0));
818     jit_unget_reg(t0);
819 }
820 
821 static void
_addxi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)822 _addxi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
823 {
824     jit_int32_t		t0;
825     assert(jit_carry != _NOREG);
826     t0 = jit_get_reg(jit_class_gpr);
827     movr(rn(t0), rn(jit_carry));
828     addci(r0, r1, i0);
829     addcr(r0, r0, rn(t0));
830     jit_unget_reg(t0);
831 }
832 
833 static void
_subi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)834 _subi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
835 {
836     if (simm12_p(-i0))
837 	ADDI(r0, r1, -i0);
838     else {
839 	jit_int32_t	t0;
840 	t0 = jit_get_reg(jit_class_gpr);
841 	movi(rn(t0), i0);
842 	subr(r0, r1, rn(t0));
843 	jit_unget_reg(t0);
844     }
845 }
846 
847 static void
_subcr(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)848 _subcr(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_int32_t r2)
849 {
850     jit_int32_t		t0;
851     if (jit_carry == _NOREG)
852 	jit_carry = jit_get_reg(jit_class_gpr);
853     if (r0 == r1) {
854 	t0 = jit_get_reg(jit_class_gpr);
855 	subr(rn(t0), r1, r2);
856 	SLTU(rn(jit_carry), r1, rn(t0));
857 	movr(r0, rn(t0));
858 	jit_unget_reg(t0);
859     }
860     else {
861 	subr(r0, r1, r2);
862 	SLTU(rn(jit_carry), r1, r0);
863     }
864 }
865 
866 static void
_subci(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)867 _subci(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
868 {
869     jit_int32_t		t0;
870     if (jit_carry == _NOREG)
871 	jit_carry = jit_get_reg(jit_class_gpr);
872     if (r0 == r1) {
873 	t0 = jit_get_reg(jit_class_gpr);
874 	subi(rn(t0), r1, i0);
875 	SLTU(rn(jit_carry), r1, rn(t0));
876 	movr(r0, rn(t0));
877 	jit_unget_reg(t0);
878     }
879     else {
880 	subi(r0, r1, i0);
881 	SLTU(rn(jit_carry), r1, r0);
882     }
883 }
884 
885 static void
_subxr(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)886 _subxr(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_int32_t r2)
887 {
888     jit_int32_t		t0;
889     assert(jit_carry != _NOREG);
890     t0 = jit_get_reg(jit_class_gpr);
891     movr(rn(t0), rn(jit_carry));
892     subcr(r0, r1, r2);
893     subcr(r0, r0, rn(t0));
894     jit_unget_reg(t0);
895 }
896 
897 static void
_subxi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)898 _subxi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
899 {
900     jit_int32_t		t0;
901     assert(jit_carry != _NOREG);
902     t0 = jit_get_reg(jit_class_gpr);
903     movr(rn(t0), rn(jit_carry));
904     subci(r0, r1, i0);
905     subcr(r0, r0, rn(t0));
906     jit_unget_reg(t0);
907 }
908 
909 static void
_rsbi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)910 _rsbi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
911 {
912     subi(r0, r1, i0);
913     negr(r0, r0);
914 }
915 
916 static void
_muli(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)917 _muli(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
918 {
919     jit_int32_t		t0;
920     t0 = jit_get_reg(jit_class_gpr);
921     movi(rn(t0), i0);
922     mulr(r0, r1, rn(t0));
923     jit_unget_reg(t0);
924 }
925 
926 static void
_divi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)927 _divi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
928 {
929     jit_int32_t		t0;
930     t0 = jit_get_reg(jit_class_gpr);
931     movi(rn(t0), i0);
932     divr(r0, r1, rn(t0));
933     jit_unget_reg(t0);
934 }
935 
936 static void
_divi_u(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)937 _divi_u(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
938 {
939     jit_int32_t		t0;
940     t0 = jit_get_reg(jit_class_gpr);
941     movi(rn(t0), i0);
942     divr_u(r0, r1, rn(t0));
943     jit_unget_reg(t0);
944 }
945 
946 static void
_remi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)947 _remi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
948 {
949     jit_int32_t		t0;
950     t0 = jit_get_reg(jit_class_gpr);
951     movi(rn(t0), i0);
952     remr(r0, r1, rn(t0));
953     jit_unget_reg(t0);
954 }
955 
956 static void
_remi_u(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)957 _remi_u(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
958 {
959     jit_int32_t		t0;
960     t0 = jit_get_reg(jit_class_gpr);
961     movi(rn(t0), i0);
962     remr_u(r0, r1, rn(t0));
963     jit_unget_reg(t0);
964 }
965 
966 static void
_qmulr(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2,jit_int32_t r3)967 _qmulr(jit_state_t *_jit, jit_int32_t r0,
968        jit_int32_t r1, jit_int32_t r2, jit_int32_t r3)
969 {
970     jit_int32_t		t0;
971     if (r0 == r2 || r0 == r3) {
972 	t0 = jit_get_reg(jit_class_gpr);
973 	mulr(rn(t0), r2, r3);
974     }
975     else
976 	mulr(r0, r2, r3);
977     MULH(r1, r2, r3);
978     if (r0 == r2 || r0 == r3) {
979 	movr(r0, rn(t0));
980 	jit_unget_reg(t0);
981     }
982 }
983 
984 static void
_qmuli(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2,jit_word_t i0)985 _qmuli(jit_state_t *_jit, jit_int32_t r0,
986        jit_int32_t r1, jit_int32_t r2, jit_word_t i0)
987 {
988     jit_int32_t		t0;
989     t0 = jit_get_reg(jit_class_gpr);
990     movi(rn(t0), i0);
991     qmulr(r0, r1, r2, rn(t0));
992     jit_unget_reg(t0);
993 }
994 
995 static void
_qmulr_u(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2,jit_int32_t r3)996 _qmulr_u(jit_state_t *_jit, jit_int32_t r0,
997 	 jit_int32_t r1, jit_int32_t r2, jit_int32_t r3)
998 {
999     jit_int32_t		t0;
1000     if (r0 == r2 || r0 == r3) {
1001 	t0 = jit_get_reg(jit_class_gpr);
1002 	mulr(rn(t0), r2, r3);
1003     }
1004     else
1005 	mulr(r0, r2, r3);
1006     MULHU(r1, r2, r3);
1007     if (r0 == r2 || r0 == r3) {
1008 	movr(r0, rn(t0));
1009 	jit_unget_reg(t0);
1010     }
1011 }
1012 
1013 static void
_qmuli_u(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2,jit_word_t i0)1014 _qmuli_u(jit_state_t *_jit, jit_int32_t r0,
1015 	 jit_int32_t r1, jit_int32_t r2, jit_word_t i0)
1016 {
1017     jit_int32_t		t0;
1018     t0 = jit_get_reg(jit_class_gpr);
1019     movi(rn(t0), i0);
1020     qmulr_u(r0, r1, r2, rn(t0));
1021     jit_unget_reg(t0);
1022 }
1023 
1024 static void
_iqdivr(jit_state_t * _jit,jit_bool_t sign,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2,jit_int32_t r3)1025 _iqdivr(jit_state_t *_jit, jit_bool_t sign,
1026 	jit_int32_t r0, jit_int32_t r1, jit_int32_t r2, jit_int32_t r3)
1027 {
1028     jit_int32_t		sv0, rg0;
1029     jit_int32_t		sv1, rg1;
1030     if (r0 == r2 || r0 == r3) {
1031 	sv0 = jit_get_reg(jit_class_gpr);
1032 	rg0 = rn(sv0);
1033     }
1034     else
1035 	rg0 = r0;
1036     if (r1 == r2 || r1 == r3) {
1037 	sv1 = jit_get_reg(jit_class_gpr);
1038 	rg1 = rn(sv1);
1039     }
1040     else
1041 	rg1 = r1;
1042     if (sign)
1043 	divr(rg0, r2, r3);
1044     else
1045 	divr_u(rg0, r2, r3);
1046     mulr(rg1, r3, rg0);
1047     subr(rg1, r2, rg1);
1048     if (rg0 != r0) {
1049 	movr(r0, rg0);
1050 	jit_unget_reg(sv0);
1051     }
1052     if (rg1 != r1) {
1053 	movr(r1, rg1);
1054 	jit_unget_reg(sv1);
1055     }
1056 }
1057 
1058 static void
_qdivi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2,jit_word_t i0)1059 _qdivi(jit_state_t *_jit, jit_int32_t r0,
1060        jit_int32_t r1, jit_int32_t r2, jit_word_t i0)
1061 {
1062     jit_int32_t		t0;
1063     t0 = jit_get_reg(jit_class_gpr);
1064     movi(rn(t0), i0);
1065     qdivr(r0, r1, r2, rn(t0));
1066     jit_unget_reg(t0);
1067 }
1068 
1069 static void
_qdivi_u(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2,jit_word_t i0)1070 _qdivi_u(jit_state_t *_jit, jit_int32_t r0,
1071 	 jit_int32_t r1, jit_int32_t r2, jit_word_t i0)
1072 {
1073     jit_int32_t		t0;
1074     t0 = jit_get_reg(jit_class_gpr);
1075     movi(rn(t0), i0);
1076     qdivr_u(r0, r1, r2, rn(t0));
1077     jit_unget_reg(t0);
1078 }
1079 
1080 static void
_lshi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1081 _lshi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1082 {
1083     if (i0 == 0)
1084 	movr(r0, r1);
1085     else {
1086 	assert(i0 > 0 && i0 < 64);
1087 	SLLI(r0, r1, i0);
1088     }
1089 }
1090 
1091 static void
_rshi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1092 _rshi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1093 {
1094     if (i0 == 0)
1095 	movr(r0, r1);
1096     else {
1097 	assert(i0 > 0 && i0 < 64);
1098 	SRAI(r0, r1, i0);
1099     }
1100 }
1101 
1102 static void
_rshi_u(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1103 _rshi_u(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1104 {
1105     if (i0 == 0)
1106 	movr(r0, r1);
1107     else {
1108 	assert(i0 > 0 && i0 < 64);
1109 	SRLI(r0, r1, i0);
1110     }
1111 }
1112 
1113 static void
_andi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1114 _andi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1115 {
1116     if (simm12_p(i0))
1117 	ANDI(r0, r1, i0);
1118     else {
1119 	jit_int32_t	t0;
1120 	t0 = jit_get_reg(jit_class_gpr);
1121 	movi(rn(t0), i0);
1122 	andr(r0, r1, rn(t0));
1123 	jit_unget_reg(t0);
1124     }
1125 }
1126 
1127 static void
_ori(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1128 _ori(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1129 {
1130     if (simm12_p(i0))
1131 	ORI(r0, r1, i0);
1132     else {
1133 	jit_int32_t	t0;
1134 	t0 = jit_get_reg(jit_class_gpr);
1135 	movi(rn(t0), i0);
1136 	orr(r0, r1, rn(t0));
1137 	jit_unget_reg(t0);
1138     }
1139 }
1140 
1141 static void
_xori(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1142 _xori(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1143 {
1144     if (simm12_p(i0))
1145 	XORI(r0, r1, i0);
1146     else {
1147 	jit_int32_t	t0;
1148 	t0 = jit_get_reg(jit_class_gpr);
1149 	movi(rn(t0), i0);
1150 	xorr(r0, r1, rn(t0));
1151 	jit_unget_reg(t0);
1152     }
1153 }
1154 
1155 #  define DEFLD(T,O)							\
1156 static void								\
1157 _ldi_##T(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)		\
1158 {									\
1159     if (simm12_p(i0))							\
1160 	L##O(r0, _ZERO_REGNO, i0);					\
1161     else {								\
1162 	jit_int32_t	t0;						\
1163 	t0 = jit_get_reg(jit_class_gpr);				\
1164 	movi(rn(t0), i0);						\
1165 	ldr_##T(r0, rn(t0));						\
1166 	jit_unget_reg(t0);						\
1167     }									\
1168 }									\
1169 									\
1170 static void								\
1171 _ldxr_##T(jit_state_t *_jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)\
1172 {									\
1173     jit_int32_t	t0;							\
1174     t0 = jit_get_reg(jit_class_gpr);					\
1175     addr(rn(t0), r1, r2);						\
1176     ldr_##T(r0, rn(t0));						\
1177     jit_unget_reg(t0);							\
1178 }									\
1179 									\
1180 static void								\
1181 _ldxi_##T(jit_state_t *_jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)\
1182 {									\
1183     if (simm12_p(i0))							\
1184 	L##O(r0, r1, i0);						\
1185     else {								\
1186 	jit_int32_t	t0;						\
1187 	t0 = jit_get_reg(jit_class_gpr);				\
1188 	addi(rn(t0), r1, i0);						\
1189 	ldr_##T(r0, rn(t0));						\
1190 	jit_unget_reg(t0);						\
1191     }									\
1192 }
1193 
DEFLD(c,B)1194 DEFLD(c,B)
1195 DEFLD(uc,BU)
1196 DEFLD(s,H)
1197 DEFLD(us,HU)
1198 DEFLD(i,W)
1199 DEFLD(ui,WU)
1200 DEFLD(l,D)
1201 
1202 #  define DEFST(T, O)							\
1203 static void								\
1204 _sti_##T(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0)		\
1205 {									\
1206     if (simm12_p(i0))							\
1207 	S##O(_ZERO_REGNO, r0, i0);					\
1208     else {								\
1209 	jit_int32_t	t0;						\
1210 	t0 = jit_get_reg(jit_class_gpr);				\
1211 	movi(rn(t0), i0);						\
1212 	str_##T(rn(t0), r0);						\
1213 	jit_unget_reg(t0);						\
1214     }									\
1215 }									\
1216 									\
1217 static void								\
1218 _stxr_##T(jit_state_t *_jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)\
1219 {									\
1220     jit_int32_t	t0;							\
1221     t0 = jit_get_reg(jit_class_gpr);					\
1222     addr(rn(t0), r0, r1);						\
1223     str_##T(rn(t0), r2);						\
1224     jit_unget_reg(t0);							\
1225 }									\
1226 									\
1227 static void								\
1228 _stxi_##T(jit_state_t *_jit,jit_word_t i0,jit_int32_t r0,jit_int32_t r1)\
1229 {									\
1230     if (simm12_p(i0))							\
1231 	S##O(r0, r1, i0);						\
1232     else {								\
1233 	jit_int32_t	t0;						\
1234 	t0 = jit_get_reg(jit_class_gpr);				\
1235 	addi(rn(t0), r0, i0);						\
1236 	str_##T(rn(t0), r1);						\
1237 	jit_unget_reg(t0);						\
1238     }									\
1239 }
1240 
1241 DEFST(c, B)
1242 DEFST(s, H)
1243 DEFST(i, W)
1244 DEFST(l, D)
1245 
1246 static void
1247 _htonr_us(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1)
1248 {
1249     jit_int32_t		t0;
1250     t0 = jit_get_reg(jit_class_gpr);
1251     rshi(rn(t0), r1, 8);
1252     andi(r0, r1, 0xff);
1253     andi(rn(t0), rn(t0), 0xff);
1254     lshi(r0, r0, 8);
1255     orr(r0, r0, rn(t0));
1256     jit_unget_reg(t0);
1257 }
1258 
1259 static void
_htonr_ui(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1)1260 _htonr_ui(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1)
1261 {
1262     jit_int32_t		t0;
1263     jit_int32_t		t1;
1264     jit_int32_t		t2;
1265     t0 = jit_get_reg(jit_class_gpr);
1266     t1 = jit_get_reg(jit_class_gpr);
1267     t2 = jit_get_reg(jit_class_gpr);
1268     rshi(rn(t0), r1, 24);
1269     rshi(rn(t1), r1, 16);
1270     rshi(rn(t2), r1,  8);
1271     andi(rn(t0), rn(t0), 0xff);
1272     andi(rn(t1), rn(t1), 0xff);
1273     andi(rn(t2), rn(t2), 0xff);
1274     andi(r0, r1, 0xff);
1275     lshi(r0, r0, 24);
1276     lshi(rn(t1), rn(t1), 8);
1277     orr(r0, r0, rn(t0));
1278     lshi(rn(t2), rn(t2), 16);
1279     orr(r0, r0, rn(t1));
1280     orr(r0, r0, rn(t2));
1281     jit_unget_reg(t2);
1282     jit_unget_reg(t1);
1283     jit_unget_reg(t0);
1284 }
1285 
1286 static void
_htonr_ul(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1)1287 _htonr_ul(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1)
1288 {
1289     jit_int32_t		t0;
1290     t0 = jit_get_reg(jit_class_gpr);
1291     rshi_u(rn(t0), r1, 32);
1292     htonr_ui(r0, r1);
1293     htonr_ui(rn(t0), rn(t0));
1294     lshi(r0, r0, 32);
1295     orr(r0, r0, rn(t0));
1296     jit_unget_reg(t0);
1297 }
1298 
1299 static void
_extr_c(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1)1300 _extr_c(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1)
1301 {
1302     lshi(r0, r1, 56);
1303     rshi(r0, r0, 56);
1304 }
1305 
1306 static void
_extr_s(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1)1307 _extr_s(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1)
1308 {
1309     lshi(r0, r1, 48);
1310     rshi(r0, r0, 48);
1311 }
1312 
1313 static void
_extr_us(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1)1314 _extr_us(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1)
1315 {
1316     lshi(r0, r1, 48);
1317     rshi_u(r0, r0, 48);
1318 }
1319 
1320 static void
_extr_ui(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1)1321 _extr_ui(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1)
1322 {
1323     lshi(r0, r1, 32);
1324     rshi_u(r0, r0, 32);
1325 }
1326 
1327 static void
_movi(jit_state_t * _jit,jit_int32_t r0,jit_word_t i0)1328 _movi(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
1329 {
1330     if (simm32_p(i0)) {
1331 	jit_int32_t	lo = (jit_int32_t)i0 << 20 >> 20;
1332 	jit_int32_t	hi = i0 - lo;
1333 	if (hi) {
1334 	    LUI(r0, hi >> 12);
1335 	    if (lo)
1336 		ADDIW(r0, r0, lo);
1337 	}
1338 	else
1339 	    ADDIW(r0, _ZERO_REGNO, lo);
1340     }
1341     else {
1342 	jit_int32_t	lo = i0 << 32 >> 32;
1343 	jit_word_t	hi = i0 - lo;
1344 	jit_int32_t	t0 = jit_get_reg(jit_class_gpr);
1345 	movi(rn(t0), (jit_int32_t)(hi >> 32));
1346 	movi(r0, lo);
1347 	lshi(rn(t0), rn(t0), 32);
1348 	addr(r0, r0, rn(t0));
1349 	jit_unget_reg(t0);
1350     }
1351 }
1352 
1353 static jit_word_t
_movi_p(jit_state_t * _jit,jit_int32_t r0,jit_word_t i0)1354 _movi_p(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
1355 {
1356     jit_word_t		w;
1357     jit_int32_t		t0;
1358     jit_int32_t		ww = i0 << 32 >> 32;
1359     jit_int32_t		lo = ww << 20 >> 20;
1360     jit_int32_t		hi = ww - lo;
1361     w = _jit->pc.w;
1362     t0 = jit_get_reg(jit_class_gpr);
1363     LUI(r0, hi >> 12);
1364     ADDIW(r0, r0, lo);
1365     ww = i0 >> 32;
1366     lo = ww << 20 >> 20;
1367     hi = ww - lo;
1368     LUI(rn(t0), hi >> 12);
1369     ADDIW(rn(t0), rn(t0), lo);
1370     SLLI(rn(t0), rn(t0), 32);
1371     ADD(r0, r0, rn(t0));
1372     jit_unget_reg(t0);
1373     return (w);
1374 }
1375 
1376 static void
_lti(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1377 _lti(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1378 {
1379     if (simm12_p(i0))
1380 	SLTI(r0, r1, i0);
1381     else {
1382 	jit_int32_t	t0;
1383 	t0 = jit_get_reg(jit_class_gpr);
1384 	movi(r0, i0);
1385 	ltr(r0, r1, rn(t0));
1386 	jit_unget_reg(t0);
1387     }
1388 }
1389 
1390 static void
_lti_u(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1391 _lti_u(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1392 {
1393     if (simm12_p(i0))
1394 	SLTIU(r0, r1, i0);
1395     else {
1396 	jit_int32_t	t0;
1397 	t0 = jit_get_reg(jit_class_gpr);
1398 	movi(r0, i0);
1399 	ltr_u(r0, r1, rn(t0));
1400 	jit_unget_reg(t0);
1401     }
1402 }
1403 
1404 static void
_ler(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)1405 _ler(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_int32_t r2)
1406 {
1407     SLT(r0, r2, r1);
1408     XORI(r0, r0, 1);
1409 }
1410 
1411 static void
_lei(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1412 _lei(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1413 {
1414     jit_int32_t		t0;
1415     if (i0 == 0) {
1416 	SLT(r0, _ZERO_REGNO, r1);
1417 	XORI(r0, r0, 1);
1418     }
1419     else {
1420 	t0 = jit_get_reg(jit_class_gpr);
1421 	movi(rn(t0), i0);
1422 	ler(r0, r1, rn(t0));
1423 	jit_unget_reg(t0);
1424     }
1425 }
1426 
1427 static void
_ler_u(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)1428 _ler_u(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_int32_t r2)
1429 {
1430     SLTU(r0, r2, r1);
1431     XORI(r0, r0, 1);
1432 }
1433 
1434 static void
_lei_u(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1435 _lei_u(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1436 {
1437     jit_int32_t		t0;
1438     if (i0 == 0) {
1439 	SLTU(r0, _ZERO_REGNO, r1);
1440 	XORI(r0, r0, 1);
1441     }
1442     else {
1443 	t0 = jit_get_reg(jit_class_gpr);
1444 	movi(rn(t0), i0);
1445 	ler_u(r0, r1, rn(t0));
1446 	jit_unget_reg(t0);
1447     }
1448 }
1449 
1450 static void
_eqr(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)1451 _eqr(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_int32_t r2)
1452 {
1453     subr(r0, r1, r2);
1454     SLTU(r0, _ZERO_REGNO, r0);
1455     XORI(r0, r0, 1);
1456 }
1457 
1458 static void
_eqi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1459 _eqi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1460 {
1461     if (i0) {
1462 	subi(r0, r1, i0);
1463 	SLTU(r0, _ZERO_REGNO, r0);
1464     }
1465     else
1466 	SLTU(r0, _ZERO_REGNO, r1);
1467     XORI(r0, r0, 1);
1468 }
1469 
1470 static void
_ger(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)1471 _ger(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_int32_t r2)
1472 {
1473     SLT(r0, r1, r2);
1474     XORI(r0, r0, 1);
1475 }
1476 
1477 static void
_gei(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1478 _gei(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1479 {
1480     jit_int32_t		t0;
1481     t0 = jit_get_reg(jit_class_gpr);
1482     movi(rn(t0), i0);
1483     ger(r0, r1, rn(t0));
1484     jit_unget_reg(t0);
1485 }
1486 
1487 static void
_ger_u(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)1488 _ger_u(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_int32_t r2)
1489 {
1490     SLTU(r0, r1, r2);
1491     XORI(r0, r0, 1);
1492 }
1493 
1494 static void
_gei_u(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1495 _gei_u(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1496 {
1497     jit_int32_t		t0;
1498     t0 = jit_get_reg(jit_class_gpr);
1499     movi(rn(t0), i0);
1500     ger_u(r0, r1, rn(t0));
1501     jit_unget_reg(t0);
1502 }
1503 
1504 static void
_gti(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1505 _gti(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1506 {
1507     jit_int32_t	t0;
1508     t0 = jit_get_reg(jit_class_gpr);
1509     movi(r0, i0);
1510     ltr(r0, rn(t0), r1);
1511     jit_unget_reg(t0);
1512 }
1513 
1514 static void
_gti_u(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1515 _gti_u(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1516 {
1517     jit_int32_t	t0;
1518     t0 = jit_get_reg(jit_class_gpr);
1519     movi(r0, i0);
1520     ltr_u(r0, rn(t0), r1);
1521     jit_unget_reg(t0);
1522 }
1523 
1524 static void
_ner(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)1525 _ner(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_int32_t r2)
1526 {
1527     subr(r0, r1, r2);
1528     SLTU(r0, _ZERO_REGNO, r0);
1529 }
1530 
1531 static void
_nei(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1532 _nei(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1533 {
1534     if (i0) {
1535 	subi(r0, r1, i0);
1536 	SLTU(r0, _ZERO_REGNO, r0);
1537     }
1538     else
1539 	SLTU(r0, _ZERO_REGNO, r1);
1540 }
1541 
1542 static jit_word_t
_bltr(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)1543 _bltr(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
1544 {
1545     jit_word_t		w;
1546     w = _jit->pc.w;
1547     BLT(r0, r1, br - w);
1548     return (w);
1549 }
1550 
1551 static jit_word_t
_blti(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)1552 _blti(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
1553 {
1554     jit_word_t		w;
1555     jit_reg_t		t0;
1556     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1557     movi(rn(t0), i0);
1558     w = bltr(br, r0, rn(t0));
1559     jit_unget_reg(t0);
1560     return (w);
1561 }
1562 
1563 static jit_word_t
_bltr_u(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)1564 _bltr_u(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
1565 {
1566     jit_word_t		w;
1567     w = _jit->pc.w;
1568     BLTU(r0, r1, br - w);
1569     return (w);
1570 }
1571 
1572 static jit_word_t
_blti_u(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)1573 _blti_u(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
1574 {
1575     jit_word_t		w;
1576     jit_reg_t		t0;
1577     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1578     movi(rn(t0), i0);
1579     w = bltr_u(br, r0, rn(t0));
1580     jit_unget_reg(t0);
1581     return (w);
1582 }
1583 
1584 static jit_word_t
_bler(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)1585 _bler(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
1586 {
1587     jit_word_t		w;
1588     w = _jit->pc.w;
1589     BGE(r1, r0, br - w);
1590     return (w);
1591 }
1592 
1593 static jit_word_t
_blei(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)1594 _blei(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
1595 {
1596     jit_word_t		w;
1597     jit_reg_t		t0;
1598     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1599     movi(rn(t0), i0);
1600     w = bler(br, r0, rn(t0));
1601     jit_unget_reg(t0);
1602     return (w);
1603 }
1604 
1605 static jit_word_t
_bler_u(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)1606 _bler_u(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
1607 {
1608     jit_word_t		w;
1609     w = _jit->pc.w;
1610     BGEU(r1, r0, br - w);
1611     return (w);
1612 }
1613 
1614 static jit_word_t
_blei_u(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)1615 _blei_u(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
1616 {
1617     jit_word_t		w;
1618     jit_reg_t		t0;
1619     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1620     movi(rn(t0), i0);
1621     w = bler_u(br, r0, rn(t0));
1622     jit_unget_reg(t0);
1623     return (w);
1624 }
1625 
1626 static jit_word_t
_beqr(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)1627 _beqr(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
1628 {
1629     jit_word_t		w;
1630     w = _jit->pc.w;
1631     BEQ(r1, r0, br - w);
1632     return (w);
1633 }
1634 
1635 static jit_word_t
_beqi(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)1636 _beqi(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
1637 {
1638     jit_word_t		w;
1639     jit_reg_t		t0;
1640     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1641     movi(rn(t0), i0);
1642     w = beqr(br, r0, rn(t0));
1643     jit_unget_reg(t0);
1644     return (w);
1645 }
1646 
1647 static jit_word_t
_bger(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)1648 _bger(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
1649 {
1650     jit_word_t		w;
1651     w = _jit->pc.w;
1652     BGE(r0, r1, br - w);
1653     return (w);
1654 }
1655 
1656 static jit_word_t
_bgei(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)1657 _bgei(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
1658 {
1659     jit_word_t		w;
1660     jit_reg_t		t0;
1661     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1662     movi(rn(t0), i0);
1663     w = bger(br, r0, rn(t0));
1664     jit_unget_reg(t0);
1665     return (w);
1666 }
1667 
1668 static jit_word_t
_bger_u(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)1669 _bger_u(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
1670 {
1671     jit_word_t		w;
1672     w = _jit->pc.w;
1673     BGEU(r0, r1, br - w);
1674     return (w);
1675 }
1676 
1677 static jit_word_t
_bgei_u(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)1678 _bgei_u(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
1679 {
1680     jit_word_t		w;
1681     jit_reg_t		t0;
1682     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1683     movi(rn(t0), i0);
1684     w = bger_u(br, r0, rn(t0));
1685     jit_unget_reg(t0);
1686     return (w);
1687 }
1688 
1689 static jit_word_t
_bgtr(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)1690 _bgtr(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
1691 {
1692     jit_word_t		w;
1693     w = _jit->pc.w;
1694     BLT(r1, r0, br - w);
1695     return (w);
1696 }
1697 
1698 static jit_word_t
_bgti(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)1699 _bgti(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
1700 {
1701     jit_word_t		w;
1702     jit_reg_t		t0;
1703     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1704     movi(rn(t0), i0);
1705     w = bgtr(br, r0, rn(t0));
1706     jit_unget_reg(t0);
1707     return (w);
1708 }
1709 
1710 static jit_word_t
_bgtr_u(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)1711 _bgtr_u(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
1712 {
1713     jit_word_t		w;
1714     w = _jit->pc.w;
1715     BLTU(r1, r0, br - w);
1716     return (w);
1717 }
1718 
1719 static jit_word_t
_bgti_u(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)1720 _bgti_u(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
1721 {
1722     jit_word_t		w;
1723     jit_reg_t		t0;
1724     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1725     movi(rn(t0), i0);
1726     w = bgtr_u(br, r0, rn(t0));
1727     jit_unget_reg(t0);
1728     return (w);
1729 }
1730 
1731 static jit_word_t
_bner(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)1732 _bner(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
1733 {
1734     jit_word_t		w;
1735     w = _jit->pc.w;
1736     BNE(r1, r0, br - w);
1737     return (w);
1738 }
1739 
1740 static jit_word_t
_bnei(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)1741 _bnei(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
1742 {
1743     jit_word_t		w;
1744     jit_reg_t		t0;
1745     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1746     movi(rn(t0), i0);
1747     w = bner(br, r0, rn(t0));
1748     jit_unget_reg(t0);
1749     return (w);
1750 }
1751 
1752 static jit_word_t
_boaddr(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)1753 _boaddr(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
1754 {
1755     jit_word_t		w, jal;
1756     jit_int32_t		t0, t1;
1757     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1758     t1 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1759     /* t0 = r1 < 0; */
1760     SLT(rn(t0), r1, _ZERO_REGNO);
1761     /* t1 = r0 */
1762     movr(rn(t1), r0);
1763     /* r0 = r0 + r1 */
1764     addr(r0, r0, r1);
1765     /* overflow = r1 < 0 ? t1 < r0 : r0 < t1 */
1766     w = _jit->pc.w;
1767     BNE(rn(t0), _ZERO_REGNO, 0);
1768     /* r1 >= 0 */
1769     SLT(rn(t1), r0, rn(t1));
1770     jal = _jit->pc.w;
1771     JAL(_ZERO_REGNO, 0);
1772     /* r1 < 0 */
1773     patch_at(w, _jit->pc.w);
1774     SLT(rn(t1), rn(t1), r0);
1775     /**/
1776     patch_at(jal, _jit->pc.w);
1777     w = _jit->pc.w;
1778     BNE(rn(t1), _ZERO_REGNO, br - w);
1779     jit_unget_reg(t1);
1780     jit_unget_reg(t0);
1781     return (w);
1782 }
1783 
1784 static jit_word_t
_boaddi(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)1785 _boaddi(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
1786 {
1787     jit_word_t		w;
1788     jit_int32_t		t0;
1789     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1790     movi(rn(t0), i0);
1791     w = boaddr(br, r0, rn(t0));
1792     jit_unget_reg(t0);
1793     return (w);
1794 }
1795 
1796 static jit_word_t
_boaddr_u(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)1797 _boaddr_u(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
1798 {
1799     jit_word_t		w;
1800     jit_int32_t		t0, t1;
1801     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1802     t1 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1803     addr(rn(t0), r0, r1);
1804     SLTU(rn(t1), rn(t0), r0);
1805     movr(r0, rn(t0));
1806     w = _jit->pc.w;
1807     BNE(_ZERO_REGNO, rn(t1), br - w);
1808     jit_unget_reg(t1);
1809     jit_unget_reg(t0);
1810     return (w);
1811 }
1812 
1813 static jit_word_t
_boaddi_u(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)1814 _boaddi_u(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
1815 {
1816     jit_word_t		w;
1817     jit_int32_t		t0;
1818     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1819     movi(rn(t0), i0);
1820     w = boaddr_u(br, r0, rn(t0));
1821     jit_unget_reg(t0);
1822     return (w);
1823 }
1824 
1825 static jit_word_t
_bxaddr(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)1826 _bxaddr(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
1827 {
1828     jit_word_t		w, jal;
1829     jit_int32_t		t0, t1;
1830     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1831     t1 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1832     /* t0 = r1 < 0; */
1833     SLT(rn(t0), r1, _ZERO_REGNO);
1834     /* t1 = r0 */
1835     movr(rn(t1), r0);
1836     /* r0 = r0 + r1 */
1837     addr(r0, r0, r1);
1838     /* overflow = r1 < 0 ? t1 < r0 : r0 < t1 */
1839     w = _jit->pc.w;
1840     BNE(rn(t0), _ZERO_REGNO, 0);
1841     /* r1 >= 0 */
1842     SLT(rn(t1), r0, rn(t1));
1843     jal = _jit->pc.w;
1844     JAL(_ZERO_REGNO, 0);
1845     /* r1 < 0 */
1846     patch_at(w, _jit->pc.w);
1847     SLT(rn(t1), rn(t1), r0);
1848     /**/
1849     patch_at(jal, _jit->pc.w);
1850     w = _jit->pc.w;
1851     BEQ(rn(t1), _ZERO_REGNO, br - w);
1852     jit_unget_reg(t1);
1853     jit_unget_reg(t0);
1854     return (w);
1855 }
1856 
1857 static jit_word_t
_bxaddi(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)1858 _bxaddi(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
1859 {
1860     jit_word_t		w;
1861     jit_int32_t		t0;
1862     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1863     movi(rn(t0), i0);
1864     w = bxaddr(br, r0, rn(t0));
1865     jit_unget_reg(t0);
1866     return (w);
1867 }
1868 
1869 static jit_word_t
_bxaddr_u(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)1870 _bxaddr_u(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
1871 {
1872     jit_word_t		w;
1873     jit_int32_t		t0, t1;
1874     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1875     t1 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1876     addr(rn(t0), r0, r1);
1877     SLTU(rn(t1), rn(t0), r0);
1878     movr(r0, rn(t0));
1879     w = _jit->pc.w;
1880     BEQ(_ZERO_REGNO, rn(t1), br - w);
1881     jit_unget_reg(t1);
1882     jit_unget_reg(t0);
1883     return (w);
1884 }
1885 
1886 static jit_word_t
_bxaddi_u(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)1887 _bxaddi_u(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
1888 {
1889     jit_word_t		w;
1890     jit_int32_t		t0;
1891     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1892     movi(rn(t0), i0);
1893     w = bxaddr_u(br, r0, rn(t0));
1894     jit_unget_reg(t0);
1895     return (w);
1896 }
1897 
1898 static jit_word_t
_bosubr(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)1899 _bosubr(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
1900 {
1901     jit_word_t		w, jal;
1902     jit_int32_t		t0, t1;
1903     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1904     t1 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1905     /* t0 = 0 < r1; */
1906     SLT(rn(t0), _ZERO_REGNO, r1);
1907     /* t1 = r0 */
1908     movr(rn(t1), r0);
1909     /* r0 = r0 - r1 */
1910     subr(r0, r0, r1);
1911     /* overflow = r1 < 0 ? t1 < r0 : r0 < t1 */
1912     w = _jit->pc.w;
1913     BNE(rn(t0), _ZERO_REGNO, 0);
1914     /* r1 >= 0 */
1915     SLT(rn(t1), r0, rn(t1));
1916     jal = _jit->pc.w;
1917     JAL(_ZERO_REGNO, 0);
1918     /* r1 < 0 */
1919     patch_at(w, _jit->pc.w);
1920     SLT(rn(t1), rn(t1), r0);
1921     /**/
1922     patch_at(jal, _jit->pc.w);
1923     w = _jit->pc.w;
1924     BNE(rn(t1), _ZERO_REGNO, br - w);
1925     jit_unget_reg(t1);
1926     jit_unget_reg(t0);
1927     return (w);
1928 }
1929 
1930 static jit_word_t
_bosubi(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)1931 _bosubi(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
1932 {
1933     jit_word_t		w;
1934     jit_int32_t		t0;
1935     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1936     movi(rn(t0), i0);
1937     w = bosubr(br, r0, rn(t0));
1938     jit_unget_reg(t0);
1939     return (w);
1940 }
1941 
1942 static jit_word_t
_bosubr_u(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)1943 _bosubr_u(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
1944 {
1945     jit_word_t		w;
1946     jit_int32_t		t0, t1;
1947     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1948     t1 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1949     subr(rn(t0), r0, r1);
1950     SLTU(rn(t1), r0, rn(t0));
1951     movr(r0, rn(t0));
1952     w = _jit->pc.w;
1953     BNE(_ZERO_REGNO, rn(t1), br - w);
1954     jit_unget_reg(t1);
1955     jit_unget_reg(t0);
1956     return (w);
1957 }
1958 
1959 static jit_word_t
_bosubi_u(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)1960 _bosubi_u(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
1961 {
1962     jit_word_t		w;
1963     jit_int32_t		t0;
1964     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1965     movi(rn(t0), i0);
1966     w = bosubr_u(br, r0, rn(t0));
1967     jit_unget_reg(t0);
1968     return (w);
1969 }
1970 
1971 static jit_word_t
_bxsubr(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)1972 _bxsubr(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
1973 {
1974     jit_word_t		w, jal;
1975     jit_int32_t		t0, t1;
1976     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1977     t1 = jit_get_reg(jit_class_gpr|jit_class_nospill);
1978     /* t0 = r1 < 0; */
1979     SLT(rn(t0), _ZERO_REGNO, r1);
1980     /* t1 = r0 */
1981     movr(rn(t1), r0);
1982     /* r0 = r0 - r1 */
1983     subr(r0, r0, r1);
1984     /* overflow = r1 < 0 ? t1 < r0 : r0 < t1 */
1985     w = _jit->pc.w;
1986     BNE(rn(t0), _ZERO_REGNO, 0);
1987     /* r1 >= 0 */
1988     SLT(rn(t1), r0, rn(t1));
1989     jal = _jit->pc.w;
1990     JAL(_ZERO_REGNO, 0);
1991     /* r1 < 0 */
1992     patch_at(w, _jit->pc.w);
1993     SLT(rn(t1), rn(t1), r0);
1994     /**/
1995     patch_at(jal, _jit->pc.w);
1996     w = _jit->pc.w;
1997     BEQ(rn(t1), _ZERO_REGNO, br - w);
1998     jit_unget_reg(t1);
1999     jit_unget_reg(t0);
2000     return (w);
2001 }
2002 
2003 static jit_word_t
_bxsubi(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)2004 _bxsubi(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
2005 {
2006     jit_word_t		w;
2007     jit_int32_t		t0;
2008     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
2009     movi(rn(t0), i0);
2010     w = bxsubr(br, r0, rn(t0));
2011     jit_unget_reg(t0);
2012     return (w);
2013 }
2014 
2015 static jit_word_t
_bxsubr_u(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)2016 _bxsubr_u(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
2017 {
2018     jit_word_t		w;
2019     jit_int32_t		t0, t1;
2020     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
2021     t1 = jit_get_reg(jit_class_gpr|jit_class_nospill);
2022     subr(rn(t0), r0, r1);
2023     SLTU(rn(t1), r0, rn(t0));
2024     movr(r0, rn(t0));
2025     w = _jit->pc.w;
2026     BEQ(_ZERO_REGNO, rn(t1), br - w);
2027     jit_unget_reg(t1);
2028     jit_unget_reg(t0);
2029     return (w);
2030 }
2031 
2032 static jit_word_t
_bxsubi_u(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)2033 _bxsubi_u(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
2034 {
2035     jit_word_t		w;
2036     jit_int32_t		t0;
2037     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
2038     movi(rn(t0), i0);
2039     w = bxsubr_u(br, r0, rn(t0));
2040     jit_unget_reg(t0);
2041     return (w);
2042 }
2043 
2044 static jit_word_t
_bmsr(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)2045 _bmsr(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
2046 {
2047     jit_word_t		w;
2048     jit_int32_t		t0;
2049     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
2050     AND(rn(t0), r0, r1);
2051     w = _jit->pc.w;
2052     BNE(_ZERO_REGNO, rn(t0), br - w);
2053     jit_unget_reg(t0);
2054     return (w);
2055 }
2056 
2057 static jit_word_t
_bmsi(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)2058 _bmsi(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
2059 {
2060     jit_word_t		w;
2061     jit_int32_t		t0;
2062     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
2063     movi(rn(t0), i0);
2064     w = bmsr(br, r0, rn(t0));
2065     jit_unget_reg(t0);
2066     return (w);
2067 }
2068 
2069 static jit_word_t
_bmcr(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_int32_t r1)2070 _bmcr(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_int32_t r1)
2071 {
2072     jit_word_t		w;
2073     jit_int32_t		t0;
2074     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
2075     AND(rn(t0), r0, r1);
2076     w = _jit->pc.w;
2077     BEQ(_ZERO_REGNO, rn(t0), br - w);
2078     jit_unget_reg(t0);
2079     return (w);
2080 }
2081 
2082 static jit_word_t
_bmci(jit_state_t * _jit,jit_word_t br,jit_int32_t r0,jit_word_t i0)2083 _bmci(jit_state_t *_jit, jit_word_t br, jit_int32_t r0, jit_word_t i0)
2084 {
2085     jit_word_t		w;
2086     jit_int32_t		t0;
2087     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
2088     movi(rn(t0), i0);
2089     w = bmcr(br, r0, rn(t0));
2090     jit_unget_reg(t0);
2091     return (w);
2092 }
2093 
2094 static void
_jmpi(jit_state_t * _jit,jit_word_t i0)2095 _jmpi(jit_state_t *_jit, jit_word_t i0)
2096 {
2097     jit_int32_t		t0;
2098     jit_word_t		dsp;
2099     dsp = i0 - _jit->pc.w;
2100     if (simm20_p(dsp))
2101 	JAL(_ZERO_REGNO, dsp);
2102     else {
2103 	t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
2104 	movi(rn(t0), i0);
2105 	jmpr(rn(t0));
2106 	jit_unget_reg(t0);
2107     }
2108 }
2109 
2110 static jit_word_t
_jmpi_p(jit_state_t * _jit,jit_word_t i0)2111 _jmpi_p(jit_state_t *_jit, jit_word_t i0)
2112 {
2113     jit_word_t		w;
2114     jit_int32_t		t0;
2115     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
2116     w = movi_p(rn(t0), i0);
2117     jmpr(rn(t0));
2118     jit_unget_reg(t0);
2119     return (w);
2120 }
2121 
2122 static void
_calli(jit_state_t * _jit,jit_word_t i0)2123 _calli(jit_state_t *_jit, jit_word_t i0)
2124 {
2125     jit_int32_t		t0;
2126     jit_word_t		dsp;
2127     dsp = i0 - _jit->pc.w;
2128     if (simm20_p(dsp))
2129 	JAL(_RA_REGNO, dsp);
2130     else {
2131 	t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
2132 	movi(rn(t0), i0);
2133 	callr(rn(t0));
2134 	jit_unget_reg(t0);
2135     }
2136 }
2137 
2138 static jit_word_t
_calli_p(jit_state_t * _jit,jit_word_t i0)2139 _calli_p(jit_state_t *_jit, jit_word_t i0)
2140 {
2141     jit_word_t		w;
2142     jit_int32_t		t0;
2143     t0 = jit_get_reg(jit_class_gpr|jit_class_nospill);
2144     w = movi_p(rn(t0), i0);
2145     callr(rn(t0));
2146     jit_unget_reg(t0);
2147     return (w);
2148 }
2149 
2150 static void
_prolog(jit_state_t * _jit,jit_node_t * node)2151 _prolog(jit_state_t *_jit, jit_node_t *node)
2152 {
2153     jit_int32_t		reg;
2154     if (_jitc->function->define_frame || _jitc->function->assume_frame) {
2155 	jit_int32_t	frame = -_jitc->function->frame;
2156 	assert(_jitc->function->self.aoff >= frame);
2157 	if (_jitc->function->assume_frame)
2158 	    return;
2159 	_jitc->function->self.aoff = frame;
2160     }
2161     if (_jitc->function->allocar)
2162 	_jitc->function->self.aoff &= -16;
2163     _jitc->function->stack = ((_jitc->function->self.alen -
2164 			      /* align stack at 16 bytes */
2165 			      _jitc->function->self.aoff) + 15) & -16;
2166     subi(_SP_REGNO, _SP_REGNO, stack_framesize);
2167     stxi(0, _SP_REGNO, _RA_REGNO);
2168     stxi(8, _SP_REGNO, _FP_REGNO);
2169     if (jit_regset_tstbit(&_jitc->function->regset, _S1))
2170 	stxi(16, _SP_REGNO, 9);
2171     if (jit_regset_tstbit(&_jitc->function->regset, _S2))
2172 	stxi(24, _SP_REGNO, 18);
2173     if (jit_regset_tstbit(&_jitc->function->regset, _S3))
2174 	stxi(32, _SP_REGNO, 19);
2175     if (jit_regset_tstbit(&_jitc->function->regset, _S4))
2176 	stxi(40, _SP_REGNO, 20);
2177     if (jit_regset_tstbit(&_jitc->function->regset, _S5))
2178 	stxi(48, _SP_REGNO, 21);
2179     if (jit_regset_tstbit(&_jitc->function->regset, _S6))
2180 	stxi(56, _SP_REGNO, 22);
2181     if (jit_regset_tstbit(&_jitc->function->regset, _S7))
2182 	stxi(64, _SP_REGNO, 23);
2183     if (jit_regset_tstbit(&_jitc->function->regset, _S8))
2184 	stxi(72, _SP_REGNO, 24);
2185     if (jit_regset_tstbit(&_jitc->function->regset, _S9))
2186 	stxi(80, _SP_REGNO, 25);
2187     if (jit_regset_tstbit(&_jitc->function->regset, _S10))
2188 	stxi(88, _SP_REGNO, 26);
2189     if (jit_regset_tstbit(&_jitc->function->regset, _S11))
2190 	stxi(96, _SP_REGNO, 27);
2191     if (jit_regset_tstbit(&_jitc->function->regset, _FS0))
2192 	stxi_d(104, _SP_REGNO, 8);
2193     if (jit_regset_tstbit(&_jitc->function->regset, _FS1))
2194 	stxi_d(112, _SP_REGNO, 9);
2195     if (jit_regset_tstbit(&_jitc->function->regset, _FS2))
2196 	stxi_d(120, _SP_REGNO, 18);
2197     if (jit_regset_tstbit(&_jitc->function->regset, _FS3))
2198 	stxi_d(128, _SP_REGNO, 19);
2199     if (jit_regset_tstbit(&_jitc->function->regset, _FS4))
2200 	stxi_d(136, _SP_REGNO, 20);
2201     if (jit_regset_tstbit(&_jitc->function->regset, _FS5))
2202 	stxi_d(144, _SP_REGNO, 21);
2203     if (jit_regset_tstbit(&_jitc->function->regset, _FS6))
2204 	stxi_d(152, _SP_REGNO, 22);
2205     if (jit_regset_tstbit(&_jitc->function->regset, _FS7))
2206 	stxi_d(160, _SP_REGNO, 23);
2207     if (jit_regset_tstbit(&_jitc->function->regset, _FS8))
2208 	stxi_d(168, _SP_REGNO, 24);
2209     if (jit_regset_tstbit(&_jitc->function->regset, _FS9))
2210 	stxi_d(176, _SP_REGNO, 25);
2211     if (jit_regset_tstbit(&_jitc->function->regset, _FS10))
2212 	stxi_d(184, _SP_REGNO, 26);
2213     if (jit_regset_tstbit(&_jitc->function->regset, _FS11))
2214 	stxi_d(192, _SP_REGNO, 27);
2215     movr(_FP_REGNO, _SP_REGNO);
2216     if (_jitc->function->stack)
2217 	subi(_SP_REGNO, _SP_REGNO, _jitc->function->stack);
2218     if (_jitc->function->allocar) {
2219 	reg = jit_get_reg(jit_class_gpr);
2220 	movi(rn(reg), _jitc->function->self.aoff);
2221 	stxi_i(_jitc->function->aoffoff, _FP_REGNO, rn(reg));
2222 	jit_unget_reg(reg);
2223     }
2224     if (_jitc->function->self.call & jit_call_varargs) {
2225 	for (reg = _jitc->function->vagp; jit_arg_reg_p(reg); ++reg)
2226 	    stxi(stack_framesize - ((8 - reg) * 8),
2227 		 _FP_REGNO, rn(JIT_RA0 - reg));
2228     }
2229 }
2230 
2231 static void
_epilog(jit_state_t * _jit,jit_node_t * node)2232 _epilog(jit_state_t *_jit, jit_node_t *node)
2233 {
2234     if (_jitc->function->assume_frame)
2235 	return;
2236     movr(_SP_REGNO, _FP_REGNO);
2237     ldxi(_RA_REGNO, _SP_REGNO, 0);
2238     ldxi(_FP_REGNO, _SP_REGNO, 8);
2239     if (jit_regset_tstbit(&_jitc->function->regset, _S1))
2240 	ldxi(9, _SP_REGNO, 16);
2241     if (jit_regset_tstbit(&_jitc->function->regset, _S2))
2242 	ldxi(18, _SP_REGNO, 24);
2243     if (jit_regset_tstbit(&_jitc->function->regset, _S3))
2244 	ldxi(19, _SP_REGNO, 32);
2245     if (jit_regset_tstbit(&_jitc->function->regset, _S4))
2246 	ldxi(20, _SP_REGNO, 40);
2247     if (jit_regset_tstbit(&_jitc->function->regset, _S5))
2248 	ldxi(21, _SP_REGNO, 48);
2249     if (jit_regset_tstbit(&_jitc->function->regset, _S6))
2250 	ldxi(22, _SP_REGNO, 56);
2251     if (jit_regset_tstbit(&_jitc->function->regset, _S7))
2252 	ldxi(23, _SP_REGNO, 64);
2253     if (jit_regset_tstbit(&_jitc->function->regset, _S8))
2254 	ldxi(24, _SP_REGNO, 72);
2255     if (jit_regset_tstbit(&_jitc->function->regset, _S9))
2256 	ldxi(25, _SP_REGNO, 80);
2257     if (jit_regset_tstbit(&_jitc->function->regset, _S10))
2258 	ldxi(26, _SP_REGNO, 88);
2259     if (jit_regset_tstbit(&_jitc->function->regset, _S11))
2260 	ldxi(27, _SP_REGNO, 96);
2261     if (jit_regset_tstbit(&_jitc->function->regset, _FS0))
2262 	ldxi_d(8, _SP_REGNO, 104);
2263     if (jit_regset_tstbit(&_jitc->function->regset, _FS1))
2264 	ldxi_d(9, _SP_REGNO, 112);
2265     if (jit_regset_tstbit(&_jitc->function->regset, _FS2))
2266 	ldxi_d(18, _SP_REGNO, 120);
2267     if (jit_regset_tstbit(&_jitc->function->regset, _FS3))
2268 	ldxi_d(19, _SP_REGNO, 128);
2269     if (jit_regset_tstbit(&_jitc->function->regset, _FS4))
2270 	ldxi_d(20, _SP_REGNO, 136);
2271     if (jit_regset_tstbit(&_jitc->function->regset, _FS5))
2272 	ldxi_d(21, _SP_REGNO, 144);
2273     if (jit_regset_tstbit(&_jitc->function->regset, _FS6))
2274 	ldxi_d(22, _SP_REGNO, 152);
2275     if (jit_regset_tstbit(&_jitc->function->regset, _FS7))
2276 	ldxi_d(23, _SP_REGNO, 160);
2277     if (jit_regset_tstbit(&_jitc->function->regset, _FS8))
2278 	ldxi_d(24, _SP_REGNO, 168);
2279     if (jit_regset_tstbit(&_jitc->function->regset, _FS9))
2280 	ldxi_d(25, _SP_REGNO, 176);
2281     if (jit_regset_tstbit(&_jitc->function->regset, _FS10))
2282 	ldxi_d(26, _SP_REGNO, 184);
2283     if (jit_regset_tstbit(&_jitc->function->regset, _FS11))
2284 	ldxi_d(27, _SP_REGNO, 192);
2285     addi(_SP_REGNO, _SP_REGNO, stack_framesize);
2286     RET();
2287 }
2288 
2289 static void
_vastart(jit_state_t * _jit,jit_int32_t r0)2290 _vastart(jit_state_t *_jit, jit_int32_t r0)
2291 {
2292     assert(_jitc->function->self.call & jit_call_varargs);
2293     /* Initialize va_list to the first stack argument. */
2294     if (jit_arg_reg_p(_jitc->function->vagp))
2295 	addi(r0, _FP_REGNO, stack_framesize - ((8 - _jitc->function->vagp) * 8));
2296     else
2297 	addi(r0, _FP_REGNO, _jitc->function->self.size);
2298 }
2299 
2300 static void
_vaarg(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1)2301 _vaarg(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1)
2302 {
2303     assert(_jitc->function->self.call & jit_call_varargs);
2304     /* Load argument. */
2305     ldr(r0, r1);
2306     /* Update va_list. */
2307     addi(r1, r1, sizeof(jit_word_t));
2308 }
2309 
2310 static void
_patch_at(jit_state_t * _jit,jit_word_t instr,jit_word_t label)2311 _patch_at(jit_state_t *_jit, jit_word_t instr, jit_word_t label)
2312 {
2313     instr_t		 i;
2314     union {
2315 	jit_int32_t	*i;
2316 	jit_word_t	 w;
2317     } u;
2318     u.w = instr;
2319     i.w = u.i[0];
2320     /* movi_p? */
2321     if (i.U.opcode == 55) {					/* LUI */
2322 	jit_int32_t	ww = label << 32 >> 32;
2323 	jit_int32_t	lo = ww << 20 >> 20;
2324 	jit_int32_t	hi = ww - lo;
2325 	i.U.imm12_31 = hi >> 12;
2326 	u.i[0] = i.w;
2327 	i.w = u.i[1];
2328 	if (i.I.opcode == 27 && i.I.funct3 == 0) {		/* ADDIW */
2329 	    i.I.imm11_0 = lo & 0xfff;
2330 	    u.i[1] = i.w;
2331 	    i.w = u.i[2];
2332 	    if (i.U.opcode == 55) {				/* LUI */
2333 		ww = label >> 32;
2334 		lo = ww << 20 >> 20;
2335 		hi = ww - lo;
2336 		i.U.imm12_31 = hi >> 12;
2337 		u.i[2] = i.w;
2338 		i.w = u.i[3];
2339 		if (i.I.opcode == 27 && i.I.funct3 == 0) {	/* ADDIW */
2340 		    i.I.imm11_0 = lo & 0xfff;
2341 		    u.i[3] = i.w;
2342 		    i.w = u.i[4];
2343 		    assert(i.IS.opcode == 19);			/* SLLI */
2344 		    assert(i.IS.shamt == 32);
2345 		    i.w = u.i[5];
2346 		    assert(i.R.opcode == 51);			/* ADD */
2347 		}
2348 		else
2349 		    abort();
2350 	    }
2351 	    else
2352 		abort();
2353 	}
2354 	else
2355 	    abort();
2356     }
2357     /* b{lt,le,eq,ge,gt,ne}{,_u}? */
2358     else if (i.B.opcode == 99) {		/* B{EQ,NE,LT,GE,LTU,GEU} */
2359 	jit_word_t jmp = label - instr;
2360 	assert(simm12_p(jmp));
2361 	i.B.imm11	= (jmp >> 11) & 0x1;
2362 	i.B.imm4_1	= (jmp >> 1) & 0xf;
2363 	i.B.imm10_5	= (jmp >> 5) & 0x3f;
2364 	i.B.imm12	= (jmp >> 12) & 0x1;
2365 	u.i[0] = i.w;
2366     }
2367     else if (i.J.opcode == 111) {		/* JAL */
2368 	jit_word_t jmp = label - instr;
2369 	i.J.imm19_12	= (jmp >> 12) &  0xff;
2370 	i.J.imm11	= (jmp >> 11) &   0x1;
2371 	i.J.imm10_1	= (jmp >>  1) & 0x3ff;
2372 	i.J.imm20	= (jmp >> 20) &   0x1;
2373 	u.i[0] = i.w;
2374     }
2375     else
2376 	abort();
2377 }
2378 #endif		/* CODE */
2379