1 /*
2  * Copyright (C) 2013-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 _G2_REGNO				0x02
22 #  define _G3_REGNO				0x03
23 #  define _G4_REGNO				0x04
24 #  define _O0_REGNO				0x08
25 #  define _O1_REGNO				0x09
26 #  define _SP_REGNO				0x0e
27 #  define _FP_REGNO				0x1e
28 #  define _O7_REGNO				0x0f
29 #  define _L0_REGNO				0x10
30 #  define _L1_REGNO				0x11
31 #  define _L2_REGNO				0x12
32 #  define _L3_REGNO				0x13
33 #  define _L4_REGNO				0x14
34 #  define _L5_REGNO				0x15
35 #  define _L6_REGNO				0x16
36 #  define _L7_REGNO				0x17
37 #  define _I7_REGNO				0x1f
38 /*
39  *					- previous stack frame
40  * fp	----
41  * fp-	local variables (in lightning, 8 bytes space for float conversion)
42  * fp-	alloca
43  * sp+	stack arguments
44  * sp+	6 words to save register arguments
45  * sp+	1 word for hidden address of aggregate return value (32 bits only)
46  * sp+	16 words for in and local registers
47  * sp	----
48  *	decreasing memory address	- next stack frame (not yet allocated)
49  */
50 #  define stack_framesize			((16 + (__WORDSIZE == 32) + 6) * sizeof(jit_word_t))
51 typedef union {
52     struct {				jit_uint32_t b: 2;	} op;
53     struct {	jit_uint32_t _: 2;	jit_uint32_t b: 1;	} a;
54     struct {	jit_uint32_t _: 2;	jit_uint32_t b: 5;	} rd;
55     struct {	jit_uint32_t _: 2;	jit_uint32_t b: 30;	} disp30;
56     struct {	jit_uint32_t _: 3;	jit_uint32_t b: 4;	} cond;
57     struct {	jit_uint32_t _: 7;	jit_uint32_t b: 3;	} op2;
58     struct {	jit_uint32_t _: 7;	jit_uint32_t b: 6;	} op3;
59     struct {	jit_uint32_t _: 10;	jit_uint32_t b: 1;	} cc1;
60     struct {	jit_uint32_t _: 10;	jit_uint32_t b: 22;	} imm22;
61     struct {	jit_uint32_t _: 10;	jit_uint32_t b: 22;	} disp22;
62     struct {	jit_uint32_t _: 11;	jit_uint32_t b: 1;	} cc0;
63     struct {	jit_uint32_t _: 12;	jit_uint32_t b: 1;	} p;
64     struct {	jit_uint32_t _: 13;	jit_uint32_t b: 19;	} disp19;
65     struct {	jit_uint32_t _: 13;	jit_uint32_t b: 5;	} rs1;
66     struct {	jit_uint32_t _: 18;	jit_uint32_t b: 1;	} i;
67     struct {	jit_uint32_t _: 18;	jit_uint32_t b: 9;	} opf;
68     struct {	jit_uint32_t _: 19;	jit_uint32_t b: 1;	} x;
69     struct {	jit_uint32_t _: 19;	jit_uint32_t b: 8;	} asi;
70     struct {	jit_uint32_t _: 19;	jit_uint32_t b: 6;	} res;
71     struct {	jit_uint32_t _: 19;	jit_uint32_t b: 13;	} simm13;
72     struct {	jit_uint32_t _: 20;	jit_uint32_t b: 7;	} asix;
73     struct {	jit_uint32_t _: 20;	jit_uint32_t b: 6;	} asis;
74     struct {	jit_uint32_t _: 26;	jit_uint32_t b: 6;	} shim;
75     struct {	jit_uint32_t _: 25;	jit_uint32_t b: 7;	} imm7;
76     struct {	jit_uint32_t _: 27;	jit_uint32_t b: 5;	} rs2;
77     jit_int32_t							  v;
78 } jit_instr_t;
79 #  define ii(i)				*_jit->pc.ui++ = i
80 #  define s7_p(imm)			((imm) <= 63 && (imm) >= -64)
81 #  define s13_p(imm)			((imm) <= 4095 && (imm) >= -4096)
82 #  define s19_p(imm)			((imm) <= 262143 && (imm) >= -262144)
83 #  define s22_p(imm)			((imm) <= 2097151 && (imm) >= -20971512)
84 #  define s30_p(imm)			((imm) <= 536870911 && (imm) >= -536870912)
85 #  define f1(op, disp30)		_f1(_jit, op, disp30)
86 static void _f1(jit_state_t*,jit_int32_t,jit_int32_t);
87 #  define f2r(op, rd, op2, imm22)	_f2r(_jit, op, rd, op2, imm22)
88 static void _f2r(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t);
89 #  define f2b(op, a, cond, op2, disp22)	_f2b(_jit, op, a, cond, op2, disp22)
90 static void
91 _f2b(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t);
92 #  if __WORDSIZE == 64
93 #    define f2bp(op,a,cond,op2,cc1,cc0,p,disp19)				\
94 	_f2bp(_jit,op,a,cond,op2,cc1,cc0,p,disp19)
95 static void
96 _f2bp(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t,
97       jit_int32_t,jit_int32_t,jit_int32_t);
98 #  endif
99 #  define f3r(op, rd, op3, rs1, rs2)	_f3r(_jit, op, rd, op3, rs1, rs2)
100 static void _f3r(jit_state_t*,
101 		 jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t);
102 #  if __WORDSIZE == 64
103 #  define f3rx(op, rd, op3, rs1, rs2)	_f3rx(_jit, op, rd, op3, rs1, rs2)
104 static void _f3rx(jit_state_t*,
105 		  jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t);
106 #  endif
107 #  define f3i(op, rd, op3, rs1, simm13)	_f3i(_jit, op, rd, op3, rs1, simm13)
108 static void _f3i(jit_state_t*,
109 		 jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t);
110 #  define f3s(op, rd, op3, rs1, simm13)	_f3s(_jit, op, rd, op3, rs1, simm13)
111 static void _f3s(jit_state_t*,
112 		 jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t);
113 #  define f3t(cond, rs1, i, ri)		_f3t(_jit, cond, rs1, i, ri)
114 static void _f3t(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t)
115     maybe_unused;
116 #  define f3a(op, rd, op3, rs1, rs2)	_f3a(_jit, op, rd, op3, rs1, asi, rs2)
117 static void _f3a(jit_state_t*,jit_int32_t,
118 		 jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t)
119     maybe_unused;
120 #  define LDSB(rs1, rs2, rd)		f3r(3, rd, 9, rs1, rs2)
121 #  define LDSBI(rs1, imm, rd)		f3i(3, rd, 9, rs1, imm)
122 #  define LDSH(rs1, rs2, rd)		f3r(3, rd, 10, rs1, rs2)
123 #  define LDSHI(rs1, imm, rd)		f3i(3, rd, 10, rs1, imm)
124 #  define LDUB(rs1, rs2, rd)		f3r(3, rd, 1, rs1, rs2)
125 #  define LDUBI(rs1, imm, rd)		f3i(3, rd, 1, rs1, imm)
126 #  define LDUH(rs1, rs2, rd)		f3r(3, rd, 2, rs1, rs2)
127 #  define LDUHI(rs1, imm, rd)		f3i(3, rd, 2, rs1, imm)
128 #  if __WORDSIZE == 32
129 #    define LD(rs1, rs2, rd)		f3r(3, rd, 0, rs1, rs2)
130 #    define LDI(rs1, imm, rd)		f3i(3, rd, 0, rs1, imm)
131 #    define LDD(rs1, rs2, rd)		f3r(3, rd, 3, rs1, rs2)
132 #    define LDDI(rs1, imm, rd)		f3i(3, rd, 3, rs1, imm)
133 #  else
134 #    define LDSW(rs1, rs2, rd)		f3r(3, rd, 8, rs1, rs2)
135 #    define LDSWI(rs1, imm, rd)		f3i(3, rd, 8, rs1, imm)
136 #    define LDUW(rs1, rs2, rd)		f3r(3, rd, 0, rs1, rs2)
137 #    define LDUWI(rs1, imm, rd)		f3i(3, rd, 0, rs1, imm)
138 #    define LDX(rs1, rs2, rd)		f3r(3, rd, 11, rs1, rs2)
139 #    define LDXI(rs1, imm, rd)		f3i(3, rd, 11, rs1, imm)
140 #  endif
141 #  define LDSBA(rs1, rs2, asi, rd)	f3a(3, rd, 25, rs1, asi, rs2)
142 #  define LDSHA(rs1, rs2, asi, rd)	f3a(3, rd, 26, rs1, asi, rs2)
143 #  define LDUBA(rs1, rs2, asi, rd)	f3a(3, rd, 17, rs1, asi, rs2)
144 #  define LDUHA(rs1, rs2, asi, rd)	f3a(3, rd, 18, rs1, asi, rs2)
145 #  if __WORDSIZE == 32
146 #    define LDA(rs1, rs2, asi, rd)	f3a(3, rd, 16, rs1, asi, rs2)
147 #    define LDDA(rs1, rs2, asi, rd)	f3a(3, rd, 19, rs1, asi, rs2)
148 #  else
149 #    define LDSWA(rs1, rs2, asi, rd)	f3a(3, rd, 24, rs1, asi, rs2)
150 #    define LDUWA(rs1, rs2, asi, rd)	f3a(3, rd, 16, rs1, asi, rs2)
151 #    define LDXA(rs1, rs2, asi, rd)	f3a(3, rd, 27, rs1, asi, rs2)
152 #  endif
153 #  define LDC(rs1, rs2, rd)		f3r(3, rd, 48, rs1, rs2)
154 #  define LDCI(rs1, imm, rd)		f3i(3, rd, 48, rs1, imm)
155 #  define LDDC(rs1, rs2, rd)		f3r(3, rd, 51, rs1, rs2)
156 #  define LDDCI(rs1, imm, rd)		f3i(3, rd, 51, rs1, imm)
157 #  define LDCSR(rs1, rs2, rd)		f3r(3, rd, 49, rs1, rs2)
158 #  define LDCSRI(rs1, imm, rd)		f3i(3, rd, 49, rs1, imm)
159 #  define STB(rd, rs1, rs2)		f3r(3, rd, 5, rs1, rs2)
160 #  define STBI(rd, rs1, imm)		f3i(3, rd, 5, rs1, imm)
161 #  define STH(rd, rs1, rs2)		f3r(3, rd, 6, rs1, rs2)
162 #  define STHI(rd, rs1, imm)		f3i(3, rd, 6, rs1, imm)
163 #  if __WORDSIZE == 32
164 #    define ST(rd, rs1, rs2)		f3r(3, rd, 4, rs1, rs2)
165 #    define STI(rd, rs1, imm)		f3i(3, rd, 4, rs1, imm)
166 #    define STD(rrd, s1, rs2)		f3r(3, rd, 7, rs1, rs2)
167 #    define STDI(rd, rs1, imm)		f3i(3, rd, 7, rs1, imm)
168 #  else
169 #    define STW(rd, rs1, rs2)		f3r(3, rd, 4, rs1, rs2)
170 #    define STWI(rd, rs1, imm)		f3i(3, rd, 4, rs1, imm)
171 #    define STX(rd, rs1, rs2)		f3r(3, rd, 14, rs1, rs2)
172 #    define STXI(rd, rs1, imm)		f3i(3, rd, 14, rs1, imm)
173 #  endif
174 #  define STBA(rd, rs1, rs2)		f3a(3, rd, 21, rs1, asi, rs2)
175 #  define STHA(rd, rs1, rs2)		f3a(3, rd, 22, rs1, asi, rs2)
176 #  if __WORDSIZE == 32
177 #    define STA(rd, rs1, rs2)		f3a(3, rd, 20, rs1, asi, rs2)
178 #    define STDA(rd, rs1, rs2)		f3a(3, rd, 23, rs1, asi, rs2)
179 #  else
180 #    define STWA(rd, rs1, rs2)		f3a(3, rd, 20, rs1, asi, rs2)
181 #    define STXA(rd, rs1, rs2)		f3a(3, rd, 30, rs1, asi, rs2)
182 #  endif
183 #  define STC(rd, rs1, rs2)		f3r(3, rd, 52, rs1, rs2)
184 #  define STCI(rd, rs1, imm)		f3i(3, rd, 52, rs1, imm)
185 #  define STDC(rd, rs1, rs2)		f3r(3, rd, 55, rs1, rs2)
186 #  define STDCI(rd, rs1, imm)		f3i(3, rd, 55, rs1, imm)
187 #  define STCSR(rd, rs1, rs2)		f3r(3, rd, 53, rs1, rs2)
188 #  define STCSRI(rd, rs1, imm)		f3i(3, rd, 53, rs1, imm)
189 #  define STDCQ(rd, rs1, rs2)		f3r(3, rd, 54, rs1, rs2)
190 #  define STDCQI(rd, rs1, imm)		f3i(3, rd, 54, rs1, imm)
191 #  define LDSTUB(rs1, rs2, rd)		f3r(3, rd, 13, rs1, rs2)
192 #  define LDSTUBI(rs1, imm, rd)		f3r(3, rd, 13, rs1, imm)
193 #  define LDSTUBA(rs1, rs2, asi, rd)	f3a(3, rd, 21, rs1, asi, rs2)
194 #  define SWAP(rs1, rs2, rd)		f3r(3, rd, 15, rs1, rs2)
195 #  define SWAPI(rs1, imm, rd)		f3r(3, rd, 15, rs1, imm)
196 #  define SWAPA(rs1, rs2, asi, rd)	f3a(3, rd, 23, rs1, asi, rs2)
197 #  define NOP()				SETHI(0, 0)
198 #  define HI(im)			((im) >> 10)
199 #  define LO(im)			((im) & 0x3ff)
200 #  define SETHI(im, rd)			f2r(0, rd, 4, im)
201 #  define AND(rs1, rs2, rd)		f3r(2, rd, 1, rs1, rs2)
202 #  define ANDI(rs1, imm, rd)		f3i(2, rd, 1, rs1, imm)
203 #  define ANDcc(rs1, rs2, rd)		f3r(2, rd, 17, rs1, rs2)
204 #  define ANDIcc(rs1, imm, rd)		f3i(2, rd, 17, rs1, imm)
205 #  define BTST(rs1, rs2)		ANDcc(rs1, rs2, 0)
206 #  define BTSTI(rs1, imm)		ANDIcc(rs1, imm, 0)
207 #  define ANDN(rs1, rs2, rd)		f3r(2, rd, 5, rs1, rs2)
208 #  define ANDNI(rs1, imm, rd)		f3i(2, rd, 5, rs1, imm)
209 #  define ANDNcc(rs1, rs2, rd)		f3r(2, rd, 21, rs1, rs2)
210 #  define ANDNIcc(rs1, imm, rd)		f3i(2, rd, 21, rs1, imm)
211 #  define OR(rs1, rs2, rd)		f3r(2, rd, 2, rs1, rs2)
212 #  define ORI(rs1, imm, rd)		f3i(2, rd, 2, rs1, imm)
213 #  define ORcc(rs1, rs2, rd)		f3r(2, rd, 18, rs1, rs2)
214 #  define ORIcc(rs1, imm, rd)		f3i(2, rd, 18, rs1, imm)
215 #  define ORN(rs1, rs2, rd)		f3r(2, rd, 6, rs1, rs2)
216 #  define ORNI(rs1, imm, rd)		f3i(2, rd, 6, rs1, imm)
217 #  define ORNcc(rs1, rs2, rd)		f3r(2, rd, 22, rs1, rs2)
218 #  define ORNIcc(rs1, imm, rd)		f3i(2, rd, 22, rs1, imm)
219 #  define XOR(rs1, rs2, rd)		f3r(2, rd, 3, rs1, rs2)
220 #  define XORI(rs1, imm, rd)		f3i(2, rd, 3, rs1, imm)
221 #  define XORcc(rs1, rs2, rd)		f3r(2, rd, 19, rs1, rs2)
222 #  define XORIcc(rs1, imm, rd)		f3i(2, rd, 19, rs1, imm)
223 #  define XNOR(rs1, rs2, rd)		f3r(2, rd, 7, rs1, rs2)
224 #  define XNORI(rs1, imm, rd)		f3i(2, rd, 7, rs1, imm)
225 #  define XNORcc(rs1, rs2, rd)		f3r(2, rd, 23, rs1, rs2)
226 #  define XNORIcc(rs1, imm, rd)		f3i(2, rd, 23, rs1, imm)
227 #  define SLL(rs1, rs2, rd)		f3r(2, rd, 37, rs1, rs2)
228 #  define SLLI(rs1, imm, rd)		f3i(2, rd, 37, rs1, imm)
229 #  define SRL(rs1, rs2, rd)		f3r(2, rd, 38, rs1, rs2)
230 #  define SRLI(rs1, imm, rd)		f3i(2, rd, 38, rs1, imm)
231 #  define SRA(rs1, rs2, rd)		f3r(2, rd, 39, rs1, rs2)
232 #  define SRAI(rs1, imm, rd)		f3i(2, rd, 39, rs1, imm)
233 #  if __WORDSIZE == 64
234 #    define SLLX(rs1, rs2, rd)		f3rx(2, rd, 37, rs1, rs2)
235 #    define SLLXI(rs1, imm, rd)		f3s(2, rd, 37, rs1, imm)
236 #    define SRLX(rs1, rs2, rd)		f3rx(2, rd, 38, rs1, rs2)
237 #    define SRLXI(rs1, imm, rd)		f3s(2, rd, 38, rs1, imm)
238 #    define SRAX(rs1, rs2, rd)		f3rx(2, rd, 39, rs1, rs2)
239 #    define SRAXI(rs1, imm, rd)		f3s(2, rd, 39, rs1, imm)
240 #  endif
241 #  define ADD(rs1, rs2, rd)		f3r(2, rd, 0, rs1, rs2)
242 #  define ADDI(rs1, imm, rd)		f3i(2, rd, 0, rs1, imm)
243 #  define ADDcc(rs1, rs2, rd)		f3r(2, rd, 16, rs1, rs2)
244 #  define ADDIcc(rs1, imm, rd)		f3i(2, rd, 16, rs1, imm)
245 #  define ADDX(rs1, rs2, rd)		f3r(2, rd, 8, rs1, rs2)
246 #  define ADDXI(rs1, imm, rd)		f3i(2, rd, 8, rs1, imm)
247 #  define ADDXcc(rs1, rs2, rd)		f3r(2, rd, 24, rs1, rs2)
248 #  define ADDXIcc(rs1, imm, rd)		f3i(2, rd, 24, rs1, imm)
249 #  define TADDcc(rs1, rs2, rd)		f3r(2, rd, 32, rs1, rs2)
250 #  define TADDIcc(rs1, imm, rd)		f3i(2, rd, 32, rs1, imm)
251 #  define TADDccTV(rs1, rs2, rd)	f3r(2, rd, 34, rs1, rs2)
252 #  define TADDIccTV(rs1, imm, rd)	f3i(2, rd, 34, rs1, imm)
253 #  define SUB(rs1, rs2, rd)		f3r(2, rd, 4, rs1, rs2)
254 #  define NEG(rs1, rd)			SUB(0, rs1, rd)
255 #  define SUBI(rs1, imm, rd)		f3i(2, rd, 4, rs1, imm)
256 #  define SUBcc(rs1, rs2, rd)		f3r(2, rd, 20, rs1, rs2)
257 #  define SUBIcc(rs1, imm, rd)		f3i(2, rd, 20, rs1, imm)
258 #  define CMP(rs1, rs2)			SUBcc(rs1, rs2, 0)
259 #  define CMPI(rs1, imm)		SUBIcc(rs1, imm, 0)
260 #  define SUBX(rs1, rs2, rd)		f3r(2, rd, 12, rs1, rs2)
261 #  define SUBXI(rs1, imm, rd)		f3i(2, rd, 12, rs1, imm)
262 #  define SUBXcc(rs1, rs2, rd)		f3r(2, rd, 28, rs1, rs2)
263 #  define SUBXIcc(rs1, imm, rd)		f3i(2, rd, 28, rs1, imm)
264 #  define TSUBcc(rs1, rs2, rd)		f3r(2, rd, 33, rs1, rs2)
265 #  define TDADDIcc(rs1, imm, rd)	f3i(2, rd, 33, rs1, imm)
266 #  define TSUBccTV(rs1, rs2, rd)	f3r(2, rd, 35, rs1, rs2)
267 #  define TSUBIccTV(rs1, imm, rd)	f3i(2, rd, 35, rs1, imm)
268 #  define MULScc(rs1, rs2, rd)		f3r(2, rd, 36, rs1, rs2)
269 #  define MULSIcc(rs1, imm, rd)		f3i(2, rd, 36, rs1, imm)
270 #  define UMUL(rs1, rs2, rd)		f3r(2, rd, 10, rs1, rs2)
271 #  define UMULI(rs1, imm, rd)		f3i(2, rd, 10, rs1, imm)
272 #  define SMUL(rs1, rs2, rd)		f3r(2, rd, 11, rs1, rs2)
273 #  define SMULI(rs1, imm, rd)		f3i(2, rd, 11, rs1, imm)
274 #  define UMULcc(rs1, rs2, rd)		f3r(2, rd, 26, rs1, rs2)
275 #  define UMULIcc(rs1, imm, rd)		f3i(2, rd, 26, rs1, imm)
276 #  define SMULcc(rs1, rs2, rd)		f3r(2, rd, 27, rs1, rs2)
277 #  define SMULIcc(rs1, imm, rd)		f3i(2, rd, 27, rs1, imm)
278 #  define UDIV(rs1, rs2, rd)		f3r(2, rd, 14, rs1, rs2)
279 #  define UDIVI(rs1, imm, rd)		f3i(2, rd, 14, rs1, imm)
280 #  define SDIV(rs1, rs2, rd)		f3r(2, rd, 15, rs1, rs2)
281 #  define SDIVI(rs1, imm, rd)		f3i(2, rd, 15, rs1, imm)
282 #  define UDIVcc(rs1, rs2, rd)		f3r(2, rd, 30, rs1, rs2)
283 #  define UDIVIcc(rs1, imm, rd)		f3i(2, rd, 30, rs1, imm)
284 #  define SDIVcc(rs1, rs2, rd)		f3r(2, rd, 31, rs1, rs2)
285 #  define SDIVIcc(rs1, imm, rd)		f3i(2, rd, 31, rs1, imm)
286 #  if __WORDSIZE == 64
287 #    define MULX(rs1, rs2, rd)		f3r(2, rd, 9, rs1, rs2)
288 #    define MULXI(rs1, imm, rd)		f3i(2, rd, 9, rs1, imm)
289 #    define SDIVX(rs1, rs2, rd)		f3r(2, rd, 45, rs1, rs2)
290 #    define SDIVXI(rs1, imm, rd)	f3i(2, rd, 45, rs1, imm)
291 #    define UDIVX(rs1, rs2, rd)		f3r(2, rd, 13, rs1, rs2)
292 #    define UDIVXI(rs1, imm, rd)	f3i(2, rd, 13, rs1, imm)
293 #  endif
294 #  define SAVE(rs1, rs2, rd)		f3r(2, rd, 60, rs1, rs2)
295 #  define SAVEI(rs1, imm, rd)		f3i(2, rd, 60, rs1, imm)
296 #  define RESTORE(rs1, rs2, rd)		f3r(2, rd, 61, rs1, rs2)
297 #  define RESTOREI(rs1, imm, rd)	f3i(2, rd, 61, rs1, imm)
298 #  define SPARC_BA			8	/* always */
299 #  define SPARC_BN			0	/* never */
300 #  define SPARC_BNE			9	/* not equal - not Z */
301 #  define SPARC_BNZ			SPARC_BNE
302 #  define SPARC_BE			1	/* equal - Z */
303 #  define SPARC_BZ			SPARC_BE
304 #  define SPARC_BG			10	/* greater - not (Z or (N xor V)) */
305 #  define SPARC_BLE			2	/* less or equal - Z or (N xor V) */
306 #  define SPARC_BGE			11	/* greater or equal - not (N xor V) */
307 #  define SPARC_BL			3	/* less - N xor V */
308 #  define SPARC_BGU			12	/* greater unsigned - not (C or Z) */
309 #  define SPARC_BLEU			4	/* less or equal unsigned - C or Z */
310 #  define SPARC_BCC			13	/* carry clear - not C */
311 #  define SPARC_BGEU			SPARC_BCC
312 #  define SPARC_BCS			5	/* carry set - C */
313 #  define SPARC_BLU			SPARC_BCS
314 #  define SPARC_BPOS			14	/* positive - not N */
315 #  define SPARC_BNEG			6	/* negative - N */
316 #  define SPARC_BVC			15	/* overflow clear - not V */
317 #  define SPARC_BVS			7	/* overflow set - V */
318 /* Preferred BPcc integer branch opcodes */
319 #  if __WORDSIZE == 64
320 #    define SPARC_BPA			8	/* always - 1 */
321 #    define SPARC_BPN			0	/* never - 0 */
322 #    define SPARC_BPNE			9	/* not equal - not Z */
323 #    define SPARC_BPE			1	/* equal - Z */
324 #    define SPARC_BPG			10	/* greater - not (Z or (N xor V)) */
325 #    define SPARC_BPLE			2	/* less or equal - Z or (N xor V) */
326 #    define SPARC_BPGE			11	/* greater or equal - not (N xor V) */
327 #    define SPARC_BPL			3	/* less - N xor V */
328 #    define SPARC_BPGU			12	/* greater unsigned - not (C or V) */
329 #    define SPARC_BPLEU			4	/* less or equal unsigned  - C or Z */
330 #    define SPARC_BPCC			13	/* carry clear (greater than or equal, unsigned) - not C */
331 #    define SPARC_BPCS			5	/* carry set (less than, unsigned) - C */
332 #    define SPARC_BPPOS			14	/* positive - not N */
333 #    define SPARC_BPNEG			6	/* negative - N */
334 #    define SPARC_BPVC			15	/* overflow clear - not V */
335 #    define SPARC_BPVS			7	/* overflow set - V */
336 #  endif
337 #  define B(cc, imm)			f2b(0, 0, cc, 2, imm)
338 #  define Ba(cc, imm)			f2b(0, 1, cc, 2, imm)
339 #  define BA(imm)			B(SPARC_BA, imm)
340 #  define BAa(imm)			Ba(SPARC_BA, imm)
341 #  define BN(imm)			B(SPARC_BN, imm)
342 #  define BNa(imm)			Ba(SPARC_BN, imm)
343 #  define BNE(imm)			B(SPARC_BNE, imm)
344 #  define BNEa(imm)			Ba(SPARC_BNE, imm)
345 #  define BNZ(imm)			BNE(imm)
346 #  define BNZa(imm)			BNEa(imm)
347 #  define BE(imm)			B(SPARC_BE, imm)
348 #  define BEa(imm)			Ba(SPARC_BE, imm)
349 #  define BZ(imm)			BE(imm)
350 #  define BZa(imm)			BEa(imm)
351 #  define BG(imm)			B(SPARC_BG, imm)
352 #  define BGa(imm)			Ba(SPARC_BG, imm)
353 #  define BLE(imm)			B(SPARC_BLE, imm)
354 #  define BLEa(imm)			Ba(SPARC_BLE, imm)
355 #  define BGE(imm)			B(SPARC_BGE, imm)
356 #  define BGEa(imm)			Ba(SPARC_BGE, imm)
357 #  define BL(imm)			B(SPARC_BL, imm)
358 #  define BLa(imm)			Ba(SPARC_BL, imm)
359 #  define BGU(imm)			B(SPARC_BGU, imm)
360 #  define BGUa(imm)			Ba(SPARC_BGU, imm)
361 #  define BLEU(imm)			B(SPARC_BLEU, imm)
362 #  define BLEUa(imm)			Ba(SPARC_BLEU, imm)
363 #  define BCC(imm)			B(SPARC_BCC, imm)
364 #  define BCCa(imm)			Ba(SPARC_BCC, imm)
365 #  define BGEU(imm)			BCC(imm)
366 #  define BGEUa(imm)			BCCa(imm)
367 #  define BCS(imm)			B(SPARC_BCS, imm)
368 #  define BCSa(imm)			Ba(SPARC_BCS, imm)
369 #  define BLU(imm)			BCS(imm)
370 #  define BLUa(imm)			BCSa(imm)
371 #  define BPOS(imm)			B(SPARC_BPOS, imm)
372 #  define BPOSa(imm)			Ba(SPARC_BPOS, imm)
373 #  define BNEG(imm)			B(SPARC_BNEG, imm)
374 #  define BNEGa(imm)			Ba(SPARC_BNEG, imm)
375 #  define BVC(imm)			B(SPARC_BVC, imm)
376 #  define BVCa(imm)			Ba(SPARC_BVC, imm)
377 #  define BVS(imm)			B(SPARC_BVS, imm)
378 #  define BVSa(imm)			Ba(SPARC_BVS, imm)
379 #  if __WORDSIZE == 64
380 #    define BPccap(cc,a,cc1, cc2,p,imm)	f2bp(0, a, cc, 1, cc1, cc0, p, imm)
381 #    define BPap(cc, imm)		f2bp(0, 1, cc, 1, 1, 0, p, imm)
382 #    define BPa(cc, imm)		f2bp(0, 1, cc, 1, 1, 0, 1, imm)
383 #    define BP(cc, imm)			f2bp(0, 0, cc, 1, 1, 0, 1, imm)
384 #    define BPA(imm)			BP(SPARC_BPA, imm)
385 #    define BPN(imm)			BP(SPARC_BPN, imm)
386 #    define BNPE(imm)			BP(SPARC_BPNE, imm)
387 #    define BPE(imm)			BP(SPARC_BPE, imm)
388 #    define BPG(imm)			BP(SPARC_BPG, imm)
389 #    define BPLE(imm)			BP(SPARC_BPLE, imm)
390 #    define BPGE(imm)			BP(SPARC_BPGE, imm)
391 #    define BPL(imm)			BP(SPARC_BPL, imm)
392 #    define BPGU(imm)			BP(SPARC_BPGU, imm)
393 #    define BPLEU(imm)			BP(SPARC_BPLEU, imm)
394 #    define BPCC(imm)			BP(SPARC_BPCC, imm)
395 #    define BPCS(imm)			BP(SPARC_BPCS, imm)
396 #    define BPPOS(imm)			BP(SPARC_BPPOS, imm)
397 #    define BPNEG(imm)			BP(SPARC_BPNEG, imm)
398 #    define BPVC(imm)			BP(SPARC_BPVC, imm)
399 #    define BPVS(imm)			BP(SPARC_BPVS, imm)
400 #  endif
401 #  define SPARC_CBA			8	/* always */
402 #  define SPARC_CBN			0	/* never */
403 #  define SPARC_CB3			7	/* 3 */
404 #  define SPARC_CB2			6	/* 2 */
405 #  define SPARC_CB23			5	/* 2 or 3 */
406 #  define SPARC_CB1			4	/* 1 */
407 #  define SPARC_CB13			3	/* 1 or 3 */
408 #  define SPARC_CB12			2	/* 1 or 2 */
409 #  define SPARC_CB123			1	/* 1 or 2 or 3 */
410 #  define SPARC_CB0			9	/* 0 */
411 #  define SPARC_CB03			10	/* 0 or 3 */
412 #  define SPARC_CB02			11	/* 0 or 2 */
413 #  define SPARC_CB023			12	/* 0 or 2 or 3 */
414 #  define SPARC_CB01			13	/* 0 or 1 */
415 #  define SPARC_CB013			14	/* 0 or 1 or 3 */
416 #  define SPARC_CB012			15	/* 0 or 1 or 2 */
417 #  define CB(cc, imm)			f2b(0, 0, cc, 7, imm)
418 #  define CBa(cc, imm)			f2b(0, 1, cc, 7, imm)
419 #  define CBA(imm)			CB(SPARC_CBA, imm)
420 #  define CBAa(imm)			CBa(SPARC_CBA, imm)
421 #  define CBN(imm)			CB(SPARC_CBN, imm)
422 #  define CBNa(imm)			CBa(SPARC_CBN, imm)
423 #  define CB3(imm)			CB(SPARC_CB3, imm)
424 #  define CB3a(imm)			CBa(SPARC_CB3, imm)
425 #  define CB2(imm)			CB(SPARC_CB2, imm)
426 #  define CB2a(imm)			CBa(SPARC_CB2, imm)
427 #  define CB23(imm)			CB(SPARC_CB23, imm)
428 #  define CB23a(imm)			CBa(SPARC_CB23, imm)
429 #  define CB1(imm)			CB(SPARC_CB1, imm)
430 #  define CB1a(imm)			CBa(SPARC_CB1, imm)
431 #  define CB13(imm)			CB(SPARC_CB13, imm)
432 #  define CB13a(imm)			CBa(SPARC_CB13, imm)
433 #  define CB12(imm)			CB(SPARC_CB12, imm)
434 #  define CB12a(imm)			CBa(SPARC_CB12, imm)
435 #  define CB123(imm)			CB(SPARC_CB123, imm)
436 #  define CB123a(imm)			CBa(SPARC_CB123, imm)
437 #  define CB0(imm)			CB(SPARC_CB0, imm)
438 #  define CB0a(imm)			CBa(SPARC_CB0, imm)
439 #  define CB03(imm)			CB(SPARC_CB03, imm)
440 #  define CB03a(imm)			CBa(SPARC_CB03, imm)
441 #  define CB02(imm)			CB(SPARC_CB02, imm)
442 #  define CB02a(imm)			CBa(SPARC_CB02, imm)
443 #  define CB023(imm)			CB(SPARC_CB103, imm)
444 #  define CB023a(imm)			CBa(SPARC_CB023, imm)
445 #  define CB01(imm)			CB(SPARC_CB01, imm)
446 #  define CB01a(imm)			CBa(SPARC_CB01, imm)
447 #  define CB013(imm)			CB(SPARC_CB013, imm)
448 #  define CB013a(imm)			CBa(SPARC_CB013, imm)
449 #  define CB012(imm)			CB(SPARC_CB012, imm)
450 #  define CB012a(imm)			CBa(SPARC_CB012, imm)
451 #  define CALLI(imm)			f1(1, imm)
452 #  define CALL(r0)			JMPL(_O7_REGNO, r0, 0)
453 #  define RETL()			JMPLI(0, _O7_REGNO, 8)
454 #  define RET()				JMPLI(0, _I7_REGNO, 8)
455 #  define JMPL(rd, rs1, rs2)		f3r(2, rd, 56, rs1, rs2)
456 #  define JMPLI(rd, rs1, imm)		f3i(2, rd, 56, rs1, imm)
457 #  define RETT(rs1, rs2)		f3r(2, 0, 57, rs1, rs2)
458 #  define RETTI(rs1, imm)		f3i(2, 0, 57, rs1, imm)
459 #  define SPARC_TA			8	/* always */
460 #  define SPARC_TN			0	/* never */
461 #  define SPARC_TNE			9	/* not equal - not Z */
462 #  define SPARC_TNZ			SPARC_BNE
463 #  define SPARC_TE			1	/* equal - Z */
464 #  define SPARC_TZ			SPARC_BE
465 #  define SPARC_TG			10	/* greater - not (Z or (N xor V)) */
466 #  define SPARC_TLE			2	/* less or equal - Z or (N xor V) */
467 #  define SPARC_TGE			11	/* greater or equal - not (N xor V) */
468 #  define SPARC_TL			3	/* less - N xor V */
469 #  define SPARC_TGU			12	/* greater unsigned - not (C or Z) */
470 #  define SPARC_TLEU			4	/* less or equal unsigned - C or Z */
471 #  define SPARC_TCC			13	/* carry clear - not C */
472 #  define SPARC_TGEU			SPARC_BCC
473 #  define SPARC_TCS			5	/* carry set - C */
474 #  define SPARC_TLU			SPARC_BCS
475 #  define SPARC_TPOS			14	/* positive - not N */
476 #  define SPARC_TNEG			6	/* negative - N */
477 #  define SPARC_TVC			15	/* overflow clear - not V */
478 #  define SPARC_TVS			7	/* overflow set - V */
479 #  define T(cc, rs1, rs2)		f3t(cc, rs1, 0, rs2)
480 #  define TI(cc, rs1, imm)		f3t(cc, rs1, 1, imm)
481 #  define TA(rs1, rs2)			T(SPARC_TA, rs1, rs2)
482 #  define TAI(rs1, imm)			TI(SPARC_TA, rs1, imm)
483 #  define TN(rs1, rs2)			T(SPARC_TN, rs1, rs2)
484 #  define TNI(rs1, imm)			TI(SPARC_TN, rs1, imm)
485 #  define TNE(rs1, rs2)			T(SPARC_TNE, rs1, rs2)
486 #  define TNEI(rs1, imm)		TI(SPARC_TNE, rs1, imm)
487 #  define TNZ(rs1, rs2)			TNE(rs1, rs2)
488 #  define TNZI(rs1, imm)		TNEI(rs1, imm)
489 #  define TE(rs1, rs2)			T(SPARC_TE, rs1, rs2)
490 #  define TEI(rs1, imm)			TI(SPARC_TE, rs1, imm)
491 #  define TZ(rs1, rs2)			TE(rs1, rs2)
492 #  define TZI(rs1, imm)			TEI(rs1, imm)
493 #  define TG(rs1, rs2)			T(SPARC_TG, rs1, rs2)
494 #  define TGI(rs1, imm)			TI(SPARC_TG, rs1, imm)
495 #  define TLE(rs1, rs2)			T(SPARC_TLE, rs1, rs2)
496 #  define TLEI(rs1, imm)		TI(SPARC_TLE, rs1, imm)
497 #  define TGE(rs1, rs2)			T(SPARC_TGE, rs1, rs2)
498 #  define TGEI(rs1, imm)		TI(SPARC_TGE, rs1, imm)
499 #  define TL(rs1, rs2)			T(SPARC_TL, rs1, rs2)
500 #  define TLI(rs1, imm)			TI(SPARC_TL, rs1, imm)
501 #  define TGU(rs1, rs2)			T(SPARC_TGU, rs1, rs2)
502 #  define TGUI(rs1, imm)		TI(SPARC_TGU, rs1, imm)
503 #  define TLEU(rs1, rs2)		T(SPARC_TLEU, rs1, rs2)
504 #  define TLEUI(rs1, imm)		TI(SPARC_TLEU, rs1, imm)
505 #  define TCC(rs1, rs2)			T(SPARC_TCC, rs1, rs2)
506 #  define TCCI(rs1, imm)		TI(SPARC_TCC, rs1, imm)
507 #  define TGEU(rs1, rs2)		TCC(rs1, rs2)
508 #  define TGEUI(rs1, imm)		TCCI(rs1, imm)
509 #  define TCS(rs1, rs2)			T(SPARC_TCC, rs1, rs2)
510 #  define TCSI(rs1, imm)		TI(SPARC_TCC, rs1, imm)
511 #  define TLU(rs1, rs2)			TCS(rs1, rs2)
512 #  define TLUI(rs1, imm)		TCSI(rs1, imm)
513 #  define TPOS(rs1, rs2)		T(SPARC_TPOS, rs1, rs2)
514 #  define TPOSI(rs1, imm)		TI(SPARC_TPOS, rs1, imm)
515 #  define TNEG(rs1, rs2)		T(SPARC_TNEG, rs1, rs2)
516 #  define TNEGI(rs1, imm)		TI(SPARC_TNEG, rs1, imm)
517 #  define TVC(rs1, rs2)			T(SPARC_TVC, rs1, rs2)
518 #  define TVCI(rs1, imm)		TI(SPARC_TVC, rs1, imm)
519 #  define TVS(rs1, rs2)			T(SPARC_TVS, rs1, rs2)
520 #  define TVSI(rs1, imm)		TI(SPARC_TVS, rs1, imm)
521 #  define RDY(rd)			f3r(2, rd, 40, 0, 0)
522 #  define RDASR(rs1, rd)		f3r(2, rd, 40, rs1, 0)
523 #  define RDPSR(rd)			f3r(2, rd, 41, 0, 0)
524 #  define RDWIM(rd)			f3r(2, rd, 42, 0, 0)
525 #  define RDTBR(rd)			f3r(2, rd, 43, 0, 0)
526 #  define WRY(rs1, rs2)			f3r(2, 0, 48, rs1, rs2)
527 #  define WRYI(rs1, imm)		f3i(2, 0, 48, rs1, imm)
528 #  define WRASR(rs1, rs2, rd)		f3r(2, rd, 48, rs1, rs2)
529 #  define WRASRI(rs1, imm, rd)		f3i(2, rd, 48, rs1, imm)
530 #  define WRPSR(rs1, rs2, rd)		f3r(2, rd, 49, rs1, rs2)
531 #  define WRPSRI(rs1, imm, rd)		f3i(2, rd, 49, rs1, imm)
532 #  define WRWIM(rs1, rs2, rd)		f3r(2, rd, 50, rs1, rs2)
533 #  define WRWIMI(rs1, imm, rd)		f3i(2, rd, 50, rs1, imm)
534 #  define WRTBR(rs1, rs2, rd)		f3r(2, rd, 51, rs1, rs2)
535 #  define WRTBRI(rs1, imm, rd)		f3i(2, rd, 51, rs1, imm)
536 #  define STBAR()			f3i(2, 0, 40, 15, 0)
537 #  define UNIMP(imm)			f2r(0, 0, 0, imm)
538 #  define FLUSH(rs1, rs2)		f3r(2, 0, 59, rs1, rs2)
539 #  define FLUSHI(rs1, im)		f3i(2, 0, 59, rs1, imm)
540 #  define nop(i0)			_nop(_jit, i0)
541 static void _nop(jit_state_t*, jit_int32_t);
542 #  define movr(r0, r1)			_movr(_jit, r0, r1)
543 static void _movr(jit_state_t*, jit_int32_t, jit_int32_t);
544 #  define movi(r0, i0)			_movi(_jit, r0, i0)
545 static void _movi(jit_state_t*, jit_int32_t, jit_word_t);
546 #  define movi_p(r0, i0)		_movi_p(_jit, r0, i0)
547 static jit_word_t _movi_p(jit_state_t*, jit_int32_t, jit_word_t);
548 #  define comr(r0, r1)			XNOR(r1, 0, r0)
549 #  define negr(r0, r1)			NEG(r1, r0)
550 #  define addr(r0, r1, r2)		ADD(r1, r2, r0)
551 #  define addi(r0, r1, i0)		_addi(_jit, r0, r1, i0)
552 static void _addi(jit_state_t*, jit_int32_t, jit_int32_t, jit_word_t);
553 #  if __WORDSIZE == 32
554 #    define addcr(r0, r1, r2)		ADDcc(r1, r2, r0)
555 #  else
556 #    define addcr(r0, r1, r2)		_addcr(_jit, r0, r1, r2)
557 static void _addcr(jit_state_t*, jit_int32_t, jit_int32_t, jit_int32_t);
558 #  endif
559 #  define addci(r0, r1, i0)		_addci(_jit, r0, r1, i0)
560 static void _addci(jit_state_t*, jit_int32_t, jit_int32_t, jit_word_t);
561 #  if __WORDSIZE == 32
562 #    define addxr(r0, r1, r2)		ADDXcc(r1, r2, r0)
563 #  else
564 #    define addxr(r0, r1, r2)		_addxr(_jit, r0, r1, r2)
565 static void _addxr(jit_state_t*, jit_int32_t, jit_int32_t, jit_int32_t);
566 #  endif
567 #  define addxi(r0, r1, i0)		_addxi(_jit, r0, r1, i0)
568 static void _addxi(jit_state_t*, jit_int32_t, jit_int32_t, jit_word_t);
569 #  define subr(r0, r1, r2)		SUB(r1, r2, r0)
570 #  define subi(r0, r1, i0)		_subi(_jit, r0, r1, i0)
571 static void _subi(jit_state_t*, jit_int32_t, jit_int32_t, jit_word_t);
572 #  if __WORDSIZE == 32
573 #    define subcr(r0, r1, r2)		SUBcc(r1, r2, r0)
574 #  else
575 #    define subcr(r0, r1, r2)		_subcr(_jit, r0, r1, r2)
576 static void _subcr(jit_state_t*, jit_int32_t, jit_int32_t, jit_int32_t);
577 #  endif
578 #  define subci(r0, r1, i0)		_subci(_jit, r0, r1, i0)
579 static void _subci(jit_state_t*, jit_int32_t, jit_int32_t, jit_word_t);
580 #  if __WORDSIZE == 32
581 #    define subxr(r0, r1, r2)		SUBXcc(r1, r2, r0)
582 #  else
583 #    define subxr(r0, r1, r2)		_subxr(_jit, r0, r1, r2)
584 static void _subxr(jit_state_t*, jit_int32_t, jit_int32_t, jit_int32_t);
585 #  endif
586 #  define subxi(r0, r1, i0)		_subxi(_jit, r0, r1, i0)
587 static void _subxi(jit_state_t*, jit_int32_t, jit_int32_t, jit_word_t);
588 #  define rsbi(r0, r1, i0)		_rsbi(_jit, r0, r1, i0)
589 static void _rsbi(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
590 #  if __WORDSIZE == 32
591 #    define mulr(r0, r1, r2)		UMUL(r1, r2, r0)
592 #  else
593 #    define mulr(r0, r1, r2)		MULX(r1, r2, r0)
594 #  endif
595 #  define muli(r0, r1, i0)		_muli(_jit, r0, r1, i0)
596 static void _muli(jit_state_t*, jit_int32_t, jit_int32_t, jit_word_t);
597 #  if __WORDSIZE == 32
598 #    define qmulr(r0,r1,r2,r3)		iqmulr(r0,r1,r2,r3,1)
599 #    define qmulr_u(r0,r1,r2,r3)	iqmulr(r0,r1,r2,r3,0)
600 #    define iqmulr(r0,r1,r2,r3,cc)	_iqmulr(_jit,r0,r1,r2,r3,cc)
601 static void _iqmulr(jit_state_t*,jit_int32_t,jit_int32_t,
602 		    jit_int32_t,jit_int32_t,jit_bool_t);
603 #  define qmuli(r0,r1,r2,i0)		iqmuli(r0,r1,r2,i0,1)
604 #  define qmuli_u(r0,r1,r2,i0)		iqmuli(r0,r1,r2,i0,0)
605 #  define iqmuli(r0,r1,r2,i0,cc)	_iqmuli(_jit,r0,r1,r2,i0,cc)
606 static void _iqmuli(jit_state_t*,jit_int32_t,jit_int32_t,
607 		    jit_int32_t,jit_word_t,jit_bool_t);
608 #  else
609 #    define qmulr(r0,r1,r2,r3)		_qmulr(_jit,r0,r1,r2,r3)
610 static void _qmulr(jit_state_t*,jit_int32_t,jit_int32_t,
611 		   jit_int32_t,jit_int32_t);
612 #  define qmuli(r0,r1,r2,i0)		_qmuli(_jit,r0,r1,r2,i0)
613 static void _qmuli(jit_state_t*,jit_int32_t,jit_int32_t,
614 		   jit_int32_t,jit_word_t);
615 #    define qmulr_u(r0,r1,r2,r3)	_qmulr_u(_jit,r0,r1,r2,r3)
616 static void _qmulr_u(jit_state_t*,jit_int32_t,jit_int32_t,
617 		     jit_int32_t,jit_int32_t);
618 #  define qmuli_u(r0,r1,r2,i0)		_qmuli_u(_jit,r0,r1,r2,i0)
619 static void _qmuli_u(jit_state_t*,jit_int32_t,jit_int32_t,
620 		     jit_int32_t,jit_word_t);
621 #  endif
622 #  define divr(r0, r1, r2)		_divr(_jit, r0, r1, r2)
623 static void _divr(jit_state_t*, jit_int32_t, jit_int32_t, jit_int32_t);
624 #  define divi(r0, r1, i0)		_divi(_jit, r0, r1, i0)
625 static void _divi(jit_state_t*, jit_int32_t, jit_int32_t, jit_word_t);
626 #  define divr_u(r0, r1, r2)		_divr_u(_jit, r0, r1, r2)
627 static void _divr_u(jit_state_t*, jit_int32_t, jit_int32_t, jit_int32_t);
628 #  define divi_u(r0, r1, i0)		_divi_u(_jit, r0, r1, i0)
629 static void _divi_u(jit_state_t*, jit_int32_t, jit_int32_t, jit_word_t);
630 #  define qdivr(r0,r1,r2,r3)		iqdivr(r0,r1,r2,r3,1)
631 #  define qdivr_u(r0,r1,r2,r3)		iqdivr(r0,r1,r2,r3,0)
632 #  define iqdivr(r0,r1,r2,r3,cc)	_iqdivr(_jit,r0,r1,r2,r3,cc)
633 static void _iqdivr(jit_state_t*,jit_int32_t,jit_int32_t,
634 		    jit_int32_t,jit_int32_t,jit_bool_t);
635 #  define qdivi(r0,r1,r2,i0)		iqdivi(r0,r1,r2,i0,1)
636 #  define qdivi_u(r0,r1,r2,i0)		iqdivi(r0,r1,r2,i0,0)
637 #  define iqdivi(r0,r1,r2,i0,cc)	_iqdivi(_jit,r0,r1,r2,i0,cc)
638 static void _iqdivi(jit_state_t*,jit_int32_t,jit_int32_t,
639 		    jit_int32_t,jit_word_t,jit_bool_t);
640 #  define remr(r0, r1, r2)		_remr(_jit, r0, r1, r2)
641 static void _remr(jit_state_t*, jit_int32_t, jit_int32_t, jit_int32_t);
642 #  define remi(r0, r1, i0)		_remi(_jit, r0, r1, i0)
643 static void _remi(jit_state_t*, jit_int32_t, jit_int32_t, jit_word_t);
644 #  define remr_u(r0, r1, r2)		_remr_u(_jit, r0, r1, r2)
645 static void _remr_u(jit_state_t*, jit_int32_t, jit_int32_t, jit_int32_t);
646 #  define remi_u(r0, r1, i0)		_remi_u(_jit, r0, r1, i0)
647 static void _remi_u(jit_state_t*, jit_int32_t, jit_int32_t, jit_word_t);
648 #  define andr(r0, r1, r2)		AND(r1, r2, r0)
649 #  define andi(r0, r1, i0)		_andi(_jit, r0, r1, i0)
650 static void _andi(jit_state_t*, jit_int32_t, jit_int32_t, jit_word_t);
651 #  define orr(r0, r1, r2)		OR(r1, r2, r0)
652 #  define ori(r0, r1, i0)		_ori(_jit, r0, r1, i0)
653 static void _ori(jit_state_t*, jit_int32_t, jit_int32_t, jit_word_t);
654 #  define xorr(r0, r1, r2)		XOR(r1, r2, r0)
655 #  define xori(r0, r1, i0)		_xori(_jit, r0, r1, i0)
656 static void _xori(jit_state_t*, jit_int32_t, jit_int32_t, jit_word_t);
657 #  if __WORDSIZE == 32
658 #    define lshr(r0, r1, r2)		SLL(r1, r2, r0)
659 #    define lshi(r0, r1, i0)		SLLI(r1, i0, r0)
660 #    define rshr(r0, r1, r2)		SRA(r1, r2, r0)
661 #    define rshi(r0, r1, i0)		SRAI(r1, i0, r0)
662 #    define rshr_u(r0, r1, r2)		SRL(r1, r2, r0)
663 #    define rshi_u(r0, r1, i0)		SRLI(r1, i0, r0)
664 #  else
665 #    define lshr(r0, r1, r2)		SLLX(r1, r2, r0)
666 #    define lshi(r0, r1, i0)		SLLXI(r1, i0, r0)
667 #    define rshr(r0, r1, r2)		SRAX(r1, r2, r0)
668 #    define rshi(r0, r1, i0)		SRAXI(r1, i0, r0)
669 #    define rshr_u(r0, r1, r2)		SRLX(r1, r2, r0)
670 #    define rshi_u(r0, r1, i0)		SRLXI(r1, i0, r0)
671 #  endif
672 #  define htonr_us(r0,r1)		extr_us(r0,r1)
673 #  define extr_c(r0,r1)			_extr_c(_jit,r0,r1)
674 static void _extr_c(jit_state_t*,jit_int32_t,jit_int32_t);
675 #  define extr_uc(r0,r1)		andi(r0, r1, 0xff)
676 #  define extr_s(r0,r1)			_extr_s(_jit,r0,r1)
677 static void _extr_s(jit_state_t*,jit_int32_t,jit_int32_t);
678 #  define extr_us(r0,r1)		_extr_us(_jit,r0,r1)
679 static void _extr_us(jit_state_t*,jit_int32_t,jit_int32_t);
680 #  if __WORDSIZE == 32
681 #    define htonr_ui(r0,r1)		movr(r0,r1)
682 #  else
683 #    define htonr_ui(r0,r1)		extr_ui(r0,r1)
684 #    define htonr_ul(r0,r1)		movr(r0,r1)
685 #    define extr_i(r0,r1)		_extr_i(_jit,r0,r1)
686 static void _extr_i(jit_state_t*,jit_int32_t,jit_int32_t);
687 #    define extr_ui(r0,r1)		_extr_ui(_jit,r0,r1)
688 static void _extr_ui(jit_state_t*,jit_int32_t,jit_int32_t);
689 #  endif
690 #  define cr(cc, r0, r1, r2)		_cr(_jit, cc, r0, r1, r2)
691 static void _cr(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t,jit_int32_t);
692 #  define cw(cc, r0, r1, i0)		_cw(_jit, cc, r0, r1, i0)
693 static void _cw(jit_state_t*,jit_int32_t,jit_int32_t,jit_int32_t,jit_word_t);
694 #  if __WORDSIZE == 32
695 #    define ltr(r0, r1, r2)		cr(SPARC_BL, r0, r1, r2)
696 #    define lti(r0, r1, i0)		cw(SPARC_BL, r0, r1, i0)
697 #    define ltr_u(r0, r1, r2)		cr(SPARC_BLU, r0, r1, r2)
698 #    define lti_u(r0, r1, i0)		cw(SPARC_BLU, r0, r1, i0)
699 #    define ler(r0, r1, r2)		cr(SPARC_BLE, r0, r1, r2)
700 #    define lei(r0, r1, i0)		cw(SPARC_BLE, r0, r1, i0)
701 #    define ler_u(r0, r1, r2)		cr(SPARC_BLEU, r0, r1, r2)
702 #    define lei_u(r0, r1, i0)		cw(SPARC_BLEU, r0, r1, i0)
703 #    define eqr(r0, r1, r2)		cr(SPARC_BE, r0, r1, r2)
704 #    define eqi(r0, r1, i0)		cw(SPARC_BE, r0, r1, i0)
705 #    define ger(r0, r1, r2)		cr(SPARC_BGE, r0, r1, r2)
706 #    define gei(r0, r1, i0)		cw(SPARC_BGE, r0, r1, i0)
707 #    define ger_u(r0, r1, r2)		cr(SPARC_BGEU, r0, r1, r2)
708 #    define gei_u(r0, r1, i0)		cw(SPARC_BGEU, r0, r1, i0)
709 #    define gtr(r0, r1, r2)		cr(SPARC_BG, r0, r1, r2)
710 #    define gti(r0, r1, i0)		cw(SPARC_BG, r0, r1, i0)
711 #    define gtr_u(r0, r1, r2)		cr(SPARC_BGU, r0, r1, r2)
712 #    define gti_u(r0, r1, i0)		cw(SPARC_BGU, r0, r1, i0)
713 #    define ner(r0, r1, r2)		cr(SPARC_BNE, r0, r1, r2)
714 #    define nei(r0, r1, i0)		cw(SPARC_BNE, r0, r1, i0)
715 #  else
716 #  define ltr(r0, r1, r2)		cr(SPARC_BPL, r0, r1, r2)
717 #  define lti(r0, r1, i0)		cw(SPARC_BPL, r0, r1, i0)
718 #  define ltr_u(r0, r1, r2)		cr(SPARC_BPCS, r0, r1, r2)
719 #  define lti_u(r0, r1, i0)		cw(SPARC_BPCS, r0, r1, i0)
720 #  define ler(r0, r1, r2)		cr(SPARC_BPLE, r0, r1, r2)
721 #  define lei(r0, r1, i0)		cw(SPARC_BPLE, r0, r1, i0)
722 #  define ler_u(r0, r1, r2)		cr(SPARC_BPLEU, r0, r1, r2)
723 #  define lei_u(r0, r1, i0)		cw(SPARC_BPLEU, r0, r1, i0)
724 #  define eqr(r0, r1, r2)		cr(SPARC_BPE, r0, r1, r2)
725 #  define eqi(r0, r1, i0)		cw(SPARC_BPE, r0, r1, i0)
726 #  define ger(r0, r1, r2)		cr(SPARC_BPGE, r0, r1, r2)
727 #  define gei(r0, r1, i0)		cw(SPARC_BPGE, r0, r1, i0)
728 #  define ger_u(r0, r1, r2)		cr(SPARC_BPCC, r0, r1, r2)
729 #  define gei_u(r0, r1, i0)		cw(SPARC_BPCC, r0, r1, i0)
730 #  define gtr(r0, r1, r2)		cr(SPARC_BPG, r0, r1, r2)
731 #  define gti(r0, r1, i0)		cw(SPARC_BPG, r0, r1, i0)
732 #  define gtr_u(r0, r1, r2)		cr(SPARC_BPGU, r0, r1, r2)
733 #  define gti_u(r0, r1, i0)		cw(SPARC_BPGU, r0, r1, i0)
734 #  define ner(r0, r1, r2)		cr(SPARC_BPNE, r0, r1, r2)
735 #  define nei(r0, r1, i0)		cw(SPARC_BPNE, r0, r1, i0)
736 #  endif
737 #  define ldr_c(r0, r1)			LDSB(r1, 0, r0)
738 #  define ldi_c(r0, i0)			_ldi_c(_jit, r0, i0)
739 static void _ldi_c(jit_state_t*,jit_int32_t,jit_word_t);
740 #  define ldr_uc(r0, r1)		LDUB(r1, 0, r0)
741 #  define ldi_uc(r0, i0)		_ldi_uc(_jit, r0, i0)
742 static void _ldi_uc(jit_state_t*,jit_int32_t,jit_word_t);
743 #  define ldr_s(r0, r1)			LDSH(r1, 0, r0)
744 #  define ldi_s(r0, i0)			_ldi_s(_jit, r0, i0)
745 static void _ldi_s(jit_state_t*,jit_int32_t,jit_word_t);
746 #  define ldr_us(r0, r1)		LDUH(r1, 0, r0)
747 #  define ldi_us(r0, i0)		_ldi_us(_jit, r0, i0)
748 static void _ldi_us(jit_state_t*,jit_int32_t,jit_word_t);
749 #  if __WORDSIZE == 32
750 #    define ldr_i(r0, r1)		LD(r1, 0, r0)
751 #    define ldr(u, v)			ldr_i(u, v)
752 #    define ldi(u, v)			ldi_i(u, v)
753 #  else
754 #    define ldr_i(r0, r1)		LDSW(r1, 0, r0)
755 #    define ldr_ui(r0, r1)		LDUW(r1, 0, r0)
756 #    define ldr_l(r0, r1)		LDX(r1, 0, r0)
757 #    define ldr(u, v)			ldr_l(u, v)
758 #    define ldi(u, v)			ldi_l(u, v)
759 #  endif
760 #  define ldi_i(r0, i0)			_ldi_i(_jit, r0, i0)
761 static void _ldi_i(jit_state_t*,jit_int32_t,jit_word_t);
762 #  if __WORDSIZE == 64
763 #    define ldi_ui(r0, i0)		_ldi_ui(_jit, r0, i0)
764 static void _ldi_ui(jit_state_t*,jit_int32_t,jit_word_t);
765 #    define ldi_l(r0, i0)		_ldi_l(_jit, r0, i0)
766 static void _ldi_l(jit_state_t*,jit_int32_t,jit_word_t);
767 #  endif
768 #  define ldxr_c(r0, r1, r2)		LDSB(r1, r2, r0)
769 #  define ldxi_c(r0, r1, i0)		_ldxi_c(_jit, r0, r1, i0)
770 static void _ldxi_c(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
771 #  define ldxr_uc(r0, r1, r2)		LDUB(r1, r2, r0)
772 #  define ldxi_uc(r0, r1, i0)		_ldxi_uc(_jit, r0, r1, i0)
773 static void _ldxi_uc(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
774 #  define ldxr_s(r0, r1, r2)		LDSH(r1, r2, r0)
775 #  define ldxi_s(r0, r1, i0)		_ldxi_s(_jit, r0, r1, i0)
776 static void _ldxi_s(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
777 #  define ldxr_us(r0, r1, r2)		LDUH(r1, r2, r0)
778 #  define ldxi_us(r0, r1, i0)		_ldxi_us(_jit, r0, r1, i0)
779 static void _ldxi_us(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
780 #  if __WORDSIZE == 32
781 #    define ldxr(u, v, w)		ldxr_i(u, v, w)
782 #    define ldxr_i(r0, r1, r2)		LD(r1, r2, r0)
783 #    define ldxi(u, v, w)		ldxi_i(u, v, w)
784 #  else
785 #    define ldxr(u, v, w)		ldxr_l(u, v, w)
786 #    define ldxr_i(r0, r1, r2)		LDSW(r1, r2, r0)
787 #    define ldxr_ui(r0, r1, r2)		LDUW(r1, r2, r0)
788 #    define ldxr_l(r0, r1, r2)		LDX(r1, r2, r0)
789 #    define ldxi(u, v, w)		ldxi_l(u, v, w)
790 #  endif
791 #  define ldxi_i(r0, r1, i0)		_ldxi_i(_jit, r0, r1, i0)
792 static void _ldxi_i(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
793 #  if __WORDSIZE == 64
794 #    define ldxi_ui(r0, r1, i0)		_ldxi_ui(_jit, r0, r1, i0)
795 static void _ldxi_ui(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
796 #    define ldxi_l(r0, r1, i0)		_ldxi_l(_jit, r0, r1, i0)
797 static void _ldxi_l(jit_state_t*,jit_int32_t,jit_int32_t,jit_word_t);
798 #  endif
799 #  define str_c(r0, r1)			STB(r1, r0, 0)
800 #  define sti_c(i0, r0)			_sti_c(_jit, i0, r0)
801 static void _sti_c(jit_state_t*,jit_word_t,jit_int32_t);
802 #  define str_s(r0, r1)			STH(r1, r0, 0)
803 #  define sti_s(i0, r0)			_sti_s(_jit, i0, r0)
804 static void _sti_s(jit_state_t*,jit_word_t,jit_int32_t);
805 #  if __WORDSIZE == 32
806 #    define str(u, v)			str_i(u, v)
807 #    define str_i(r0, r1)		STI(r1, r0, 0)
808 #    define sti(u, v)			sti_i(u, v)
809 #  else
810 #    define str(u, v)			str_l(u, v)
811 #    define str_i(r0, r1)		STW(r1, r0, 0)
812 #    define str_l(r0, r1)		STX(r1, r0, 0)
813 #    define sti(u, v)			sti_l(u, v)
814 #  endif
815 #  define sti_i(i0, r0)			_sti_i(_jit, i0, r0)
816 static void _sti_i(jit_state_t*,jit_word_t,jit_int32_t);
817 #  if __WORDSIZE == 64
818 #    define sti_l(i0, r0)		_sti_l(_jit, i0, r0)
819 static void _sti_l(jit_state_t*,jit_word_t,jit_int32_t);
820 #  endif
821 #  define stxr_c(r0, r1, r2)		STB(r2, r1, r0)
822 #  define stxi_c(i0, r0, r1)		_stxi_c(_jit, i0, r0, r1)
823 static void _stxi_c(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
824 #  define stxr_s(r0, r1, r2)		STH(r2, r1, r0)
825 #  define stxi_s(i0, r0, r1)		_stxi_s(_jit, i0, r0, r1)
826 static void _stxi_s(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
827 #  if __WORDSIZE == 32
828 #    define stxr(u, v, w)		stxr_i(u, v, w)
829 #    define stxr_i(r0, r1, r2)		ST(r2, r1, r0)
830 #    define stxi(u, v, w)		stxi_i(u, v, w)
831 #  else
832 #    define stxr(u, v, w)		stxr_l(u, v, w)
833 #    define stxr_i(r0, r1, r2)		STW(r2, r1, r0)
834 #    define stxi(u, v, w)		stxi_l(u, v, w)
835 #    define stxr_l(r0, r1, r2)		STX(r2, r1, r0)
836 #  endif
837 #  define stxi_i(i0, r0, r1)		_stxi_i(_jit, i0, r0, r1)
838 static void _stxi_i(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
839 #  if __WORDSIZE == 64
840 #    define stxi_l(i0, r0, r1)		_stxi_l(_jit, i0, r0, r1)
841 static void _stxi_l(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
842 #  endif
843 #  define br(cc, i0, r0, r1)		_br(_jit, cc, i0, r0, r1)
844 static jit_word_t
845 _br(jit_state_t*,jit_int32_t,jit_word_t,jit_int32_t,jit_int32_t);
846 #  define bw(cc, i0, r0, i1)		_bw(_jit, cc, i0, r0, i1)
847 static jit_word_t
848 _bw(jit_state_t*,jit_int32_t,jit_word_t,jit_int32_t,jit_word_t);
849 #  if __WORDSIZE == 32
850 #    define bltr(i0, r0, r1)		br(SPARC_BL, i0, r0, r1)
851 #    define blti(i0, r0, i1)		bw(SPARC_BL, i0, r0, i1)
852 #    define bltr_u(i0, r0, r1)		br(SPARC_BLU, i0, r0, r1)
853 #    define blti_u(i0, r0, i1)		bw(SPARC_BLU, i0, r0, i1)
854 #    define bler(i0, r0, r1)		br(SPARC_BLE, i0, r0, r1)
855 #    define blei(i0, r0, i1)		bw(SPARC_BLE, i0, r0, i1)
856 #    define bler_u(i0, r0, r1)		br(SPARC_BLEU, i0, r0, r1)
857 #    define blei_u(i0, r0, i1)		bw(SPARC_BLEU, i0, r0, i1)
858 #    define beqr(i0, r0, r1)		br(SPARC_BE, i0, r0, r1)
859 #    define beqi(i0, r0, i1)		bw(SPARC_BE, i0, r0, i1)
860 #    define bger(i0, r0, r1)		br(SPARC_BGE, i0, r0, r1)
861 #    define bgei(i0, r0, i1)		bw(SPARC_BGE, i0, r0, i1)
862 #    define bger_u(i0, r0, r1)		br(SPARC_BGEU, i0, r0, r1)
863 #    define bgei_u(i0, r0, i1)		bw(SPARC_BGEU, i0, r0, i1)
864 #    define bgtr(i0, r0, r1)		br(SPARC_BG, i0, r0, r1)
865 #    define bgti(i0, r0, i1)		bw(SPARC_BG, i0, r0, i1)
866 #    define bgtr_u(i0, r0, r1)		br(SPARC_BGU, i0, r0, r1)
867 #    define bgti_u(i0, r0, i1)		bw(SPARC_BGU, i0, r0, i1)
868 #    define bner(i0, r0, r1)		br(SPARC_BNE, i0, r0, r1)
869 #    define bnei(i0, r0, i1)		bw(SPARC_BNE, i0, r0, i1)
870 #  else
871 #    define bltr(i0, r0, r1)		br(SPARC_BPL, i0, r0, r1)
872 #    define blti(i0, r0, i1)		bw(SPARC_BPL, i0, r0, i1)
873 #    define bltr_u(i0, r0, r1)		br(SPARC_BPCS, i0, r0, r1)
874 #    define blti_u(i0, r0, i1)		bw(SPARC_BPCS, i0, r0, i1)
875 #    define bler(i0, r0, r1)		br(SPARC_BPLE, i0, r0, r1)
876 #    define blei(i0, r0, i1)		bw(SPARC_BPLE, i0, r0, i1)
877 #    define bler_u(i0, r0, r1)		br(SPARC_BPLEU, i0, r0, r1)
878 #    define blei_u(i0, r0, i1)		bw(SPARC_BPLEU, i0, r0, i1)
879 #    define beqr(i0, r0, r1)		br(SPARC_BPE, i0, r0, r1)
880 #    define beqi(i0, r0, i1)		bw(SPARC_BPE, i0, r0, i1)
881 #    define bger(i0, r0, r1)		br(SPARC_BPGE, i0, r0, r1)
882 #    define bgei(i0, r0, i1)		bw(SPARC_BPGE, i0, r0, i1)
883 #    define bger_u(i0, r0, r1)		br(SPARC_BPCC, i0, r0, r1)
884 #    define bgei_u(i0, r0, i1)		bw(SPARC_BPCC, i0, r0, i1)
885 #    define bgtr(i0, r0, r1)		br(SPARC_BPG, i0, r0, r1)
886 #    define bgti(i0, r0, i1)		bw(SPARC_BPG, i0, r0, i1)
887 #    define bgtr_u(i0, r0, r1)		br(SPARC_BPGU, i0, r0, r1)
888 #    define bgti_u(i0, r0, i1)		bw(SPARC_BPGU, i0, r0, i1)
889 #    define bner(i0, r0, r1)		br(SPARC_BPNE, i0, r0, r1)
890 #    define bnei(i0, r0, i1)		bw(SPARC_BPNE, i0, r0, i1)
891 #  endif
892 #  define b_asr(jif,add,sgn,i0,r0,r1)	_b_asr(_jit,jif,add,sgn,i0,r0,r1)
893 static jit_word_t
894 _b_asr(jit_state_t*,jit_bool_t,jit_bool_t,jit_bool_t,
895        jit_word_t,jit_int32_t,jit_int32_t);
896 #  define b_asw(jif,add,sgn,i0,r0,i1)	_b_asw(_jit,jif,add,sgn,i0,r0,i1)
897 static jit_word_t
898 _b_asw(jit_state_t*,jit_bool_t,jit_bool_t,jit_bool_t,
899        jit_word_t,jit_int32_t,jit_word_t);
900 #  define boaddr(i0, r0, r1)		b_asr(1, 1, 1, i0, r0, r1)
901 #  define boaddi(i0, r0, i1)		b_asw(1, 1, 1, i0, r0, i1)
902 #  define boaddr_u(i0, r0, r1)		b_asr(1, 1, 0, i0, r0, r1)
903 #  define boaddi_u(i0, r0, i1)		b_asw(1, 1, 0, i0, r0, i1)
904 #  define bxaddr(i0, r0, r1)		b_asr(0, 1, 1, i0, r0, r1)
905 #  define bxaddi(i0, r0, i1)		b_asw(0, 1, 1, i0, r0, i1)
906 #  define bxaddr_u(i0, r0, r1)		b_asr(0, 1, 0, i0, r0, r1)
907 #  define bxaddi_u(i0, r0, i1)		b_asw(0, 1, 0, i0, r0, i1)
908 #  define bosubr(i0, r0, r1)		b_asr(1, 0, 1, i0, r0, r1)
909 #  define bosubi(i0, r0, i1)		b_asw(1, 0, 1, i0, r0, i1)
910 #  define bosubr_u(i0, r0, r1)		b_asr(1, 0, 0, i0, r0, r1)
911 #  define bosubi_u(i0, r0, i1)		b_asw(1, 0, 0, i0, r0, i1)
912 #  define bxsubr(i0, r0, r1)		b_asr(0, 0, 1, i0, r0, r1)
913 #  define bxsubi(i0, r0, i1)		b_asw(0, 0, 1, i0, r0, i1)
914 #  define bxsubr_u(i0, r0, r1)		b_asr(0, 0, 0, i0, r0, r1)
915 #  define bxsubi_u(i0, r0, i1)		b_asw(0, 0, 0, i0, r0, i1)
916 #  define bm_r(set, i0, r0, r1)		_bm_r(_jit,set,i0,r0,r1)
917 static jit_word_t
918 _bm_r(jit_state_t*,jit_bool_t,jit_word_t,jit_int32_t,jit_int32_t);
919 #  define bm_w(set,i0,r0,i1)		_bm_w(_jit,set,i0,r0,i1)
920 static jit_word_t
921 _bm_w(jit_state_t*,jit_bool_t,jit_word_t,jit_int32_t,jit_word_t);
922 #  define bmsr(i0, r0, r1)		bm_r(1, i0, r0, r1)
923 #  define bmsi(i0, r0, i1)		bm_w(1, i0, r0, i1)
924 #  define bmcr(i0, r0, r1)		bm_r(0, i0, r0, r1)
925 #  define bmci(i0, r0, i1)		bm_w(0, i0, r0, i1)
926 #  define jmpr(r0)			_jmpr(_jit, r0)
927 static void _jmpr(jit_state_t*,jit_int32_t);
928 #  define jmpi(i0)			_jmpi(_jit, i0)
929 static void _jmpi(jit_state_t*,jit_word_t);
930 #  define jmpi_p(i0)			_jmpi_p(_jit, i0)
931 static jit_word_t _jmpi_p(jit_state_t*,jit_word_t);
932 #  define callr(r0)			_callr(_jit, r0)
933 static void _callr(jit_state_t*,jit_int32_t);
934 #  define calli(i0)			_calli(_jit, i0)
935 static void _calli(jit_state_t*,jit_word_t);
936 #  define calli_p(i0)			_calli_p(_jit, i0)
937 static jit_word_t _calli_p(jit_state_t*,jit_word_t);
938 #  define prolog(node)			_prolog(_jit, node)
939 static void _prolog(jit_state_t*,jit_node_t*);
940 #  define epilog(node)			_epilog(_jit, node)
941 static void _epilog(jit_state_t*,jit_node_t*);
942 #define vastart(r0)			_vastart(_jit, r0)
943 static void _vastart(jit_state_t*, jit_int32_t);
944 #define vaarg(r0, r1)			_vaarg(_jit, r0, r1)
945 static void _vaarg(jit_state_t*, jit_int32_t, jit_int32_t);
946 #define patch_at(jump, label)		_patch_at(_jit, jump, label)
947 static void _patch_at(jit_state_t*,jit_word_t,jit_word_t);
948 #endif
949 
950 #if CODE
951 static void
_f2r(jit_state_t * _jit,jit_int32_t op,jit_int32_t rd,jit_int32_t op2,jit_int32_t imm22)952 _f2r(jit_state_t *_jit,
953      jit_int32_t op, jit_int32_t rd, jit_int32_t op2, jit_int32_t imm22)
954 {
955     jit_instr_t		v;
956     assert(!(op  & 0xfffffffc));
957     assert(!(rd  & 0xffffffe0));
958     assert(!(op2 & 0xfffffff8));
959     assert(s22_p(imm22));
960     v.op.b    = op;
961     v.rd.b    = rd;
962     v.op2.b   = op2;
963     v.imm22.b = imm22;
964     ii(v.v);
965 }
966 
967 static void
_f2b(jit_state_t * _jit,jit_int32_t op,jit_int32_t a,jit_int32_t cond,jit_int32_t op2,jit_int32_t disp22)968 _f2b(jit_state_t *_jit,
969      jit_int32_t op, jit_int32_t a, jit_int32_t cond, jit_int32_t op2,
970      jit_int32_t disp22)
971 {
972     jit_instr_t		v;
973     assert(!(op   & 0xfffffffc));
974     assert(!(a    & 0xfffffffe));
975     assert(!(cond & 0xfffffff0));
976     assert(!(op2  & 0xfffffff8));
977     assert(s22_p(disp22));
978     v.op.b     = op;
979     v.a.b      = a;
980     v.cond.b   = cond;
981     v.op2.b    = op2;
982     v.disp22.b = disp22;
983     ii(v.v);
984 }
985 
986 #  if __WORDSIZE == 64
987 static void
_f2bp(jit_state_t * _jit,jit_int32_t op,jit_int32_t a,jit_int32_t cond,jit_int32_t op2,jit_int32_t cc1,jit_int32_t cc0,jit_int32_t p,jit_int32_t disp19)988 _f2bp(jit_state_t *_jit,
989       jit_int32_t op, jit_int32_t a, jit_int32_t cond, jit_int32_t op2,
990       jit_int32_t cc1, jit_int32_t cc0, jit_int32_t p, jit_int32_t disp19)
991 {
992     jit_instr_t		v;
993     assert(!(op   & 0xfffffffc));
994     assert(!(a    & 0xfffffffe));
995     assert(!(cond & 0xfffffff0));
996     assert(!(op2  & 0xfffffff8));
997     assert(s19_p(disp19));
998     v.op.b     = op;
999     v.a.b      = a;
1000     v.cond.b   = cond;
1001     v.op2.b    = op2;
1002     v.cc1.b    = cc1;
1003     v.cc0.b    = cc0;
1004     v.p.b      = p;
1005     v.disp19.b = disp19;
1006     ii(v.v);
1007 }
1008 #  endif
1009 
1010 static void
_f3r(jit_state_t * _jit,jit_int32_t op,jit_int32_t rd,jit_int32_t op3,jit_int32_t rs1,jit_int32_t rs2)1011 _f3r(jit_state_t *_jit, jit_int32_t op, jit_int32_t rd,
1012      jit_int32_t op3, jit_int32_t rs1, jit_int32_t rs2)
1013 {
1014     jit_instr_t		v;
1015     assert(!(op  & 0xfffffffc));
1016     assert(!(rd  & 0xffffffe0));
1017     assert(!(op3 & 0xffffffc0));
1018     assert(!(rs1 & 0xffffffe0));
1019     assert(!(rs2 & 0xffffffe0));
1020     v.op.b  = op;
1021     v.rd.b  = rd;
1022     v.op3.b = op3;
1023     v.rs1.b = rs1;
1024     v.i.b   = 0;
1025     v.asi.b = 0;
1026     v.rs2.b = rs2;
1027     ii(v.v);
1028 }
1029 
1030 #  if __WORDSIZE == 64
1031 static void
_f3rx(jit_state_t * _jit,jit_int32_t op,jit_int32_t rd,jit_int32_t op3,jit_int32_t rs1,jit_int32_t rs2)1032 _f3rx(jit_state_t *_jit, jit_int32_t op, jit_int32_t rd,
1033       jit_int32_t op3, jit_int32_t rs1, jit_int32_t rs2)
1034 {
1035     jit_instr_t		v;
1036     assert(!(op  & 0xfffffffc));
1037     assert(!(rd  & 0xffffffe0));
1038     assert(!(op3 & 0xffffffc0));
1039     assert(!(rs1 & 0xffffffe0));
1040     assert(!(rs2 & 0xffffffe0));
1041     v.op.b   = op;
1042     v.rd.b   = rd;
1043     v.op3.b  = op3;
1044     v.rs1.b  = rs1;
1045     v.i.b    = 0;
1046     v.x.b    = 1;
1047     v.asix.b = 0;
1048     v.rs2.b  = rs2;
1049     ii(v.v);
1050 }
1051 
1052 static void
_f3s(jit_state_t * _jit,jit_int32_t op,jit_int32_t rd,jit_int32_t op3,jit_int32_t rs1,jit_int32_t shim)1053 _f3s(jit_state_t *_jit, jit_int32_t op, jit_int32_t rd,
1054       jit_int32_t op3, jit_int32_t rs1, jit_int32_t shim)
1055 {
1056     jit_instr_t		v;
1057     assert(!(op   & 0xfffffffc));
1058     assert(!(rd   & 0xffffffe0));
1059     assert(!(op3  & 0xffffffc0));
1060     assert(!(rs1  & 0xffffffe0));
1061     assert(!(shim & 0xffffffc0));
1062     v.op.b   = op;
1063     v.rd.b   = rd;
1064     v.op3.b  = op3;
1065     v.rs1.b  = rs1;
1066     v.i.b    = 1;
1067     v.x.b    = 1;
1068     v.asis.b = 0;
1069     v.shim.b = shim;
1070     ii(v.v);
1071 }
1072 #  endif
1073 
1074 static void
_f3i(jit_state_t * _jit,jit_int32_t op,jit_int32_t rd,jit_int32_t op3,jit_int32_t rs1,jit_int32_t simm13)1075 _f3i(jit_state_t *_jit, jit_int32_t op, jit_int32_t rd,
1076      jit_int32_t op3, jit_int32_t rs1, jit_int32_t simm13)
1077 {
1078     jit_instr_t		v;
1079     assert(!(op  & 0xfffffffc));
1080     assert(!(rd  & 0xffffffe0));
1081     assert(!(op3 & 0xffffffc0));
1082     assert(!(rs1 & 0xffffffe0));
1083     assert(s13_p(simm13));
1084     v.op.b     = op;
1085     v.rd.b     = rd;
1086     v.op3.b    = op3;
1087     v.rs1.b    = rs1;
1088     v.i.b      = 1;
1089     v.simm13.b = simm13;
1090     ii(v.v);
1091 }
1092 
1093 static void
_f3t(jit_state_t * _jit,jit_int32_t cond,jit_int32_t rs1,jit_int32_t i,jit_int32_t rs2_imm7)1094 _f3t(jit_state_t *_jit, jit_int32_t cond,
1095      jit_int32_t rs1, jit_int32_t i, jit_int32_t rs2_imm7)
1096 {
1097     jit_instr_t		v;
1098     assert(!(cond & 0xfffffff0));
1099     assert(!(i    & 0xfffffffe));
1100     assert(!(rs1 & 0xffffffe0));
1101     v.op.b     = 2;
1102     v.rd.b     = cond;
1103     v.op3.b    = 58;
1104     v.i.b      = i;
1105     if (i) {
1106 	assert(s7_p(rs2_imm7));
1107 	v.res.b  = 0;
1108 	v.imm7.b = rs2_imm7;
1109     }
1110     else {
1111 	assert(!(rs2_imm7 & 0xffffffe0));
1112 	v.asi.b = 0;
1113 	v.rs2.b = rs2_imm7;
1114     }
1115     ii(v.v);
1116 }
1117 
1118 static void
_f3a(jit_state_t * _jit,jit_int32_t op,jit_int32_t rd,jit_int32_t op3,jit_int32_t rs1,jit_int32_t asi,jit_int32_t rs2)1119 _f3a(jit_state_t *_jit, jit_int32_t op, jit_int32_t rd,
1120      jit_int32_t op3, jit_int32_t rs1, jit_int32_t asi, jit_int32_t rs2)
1121 {
1122     jit_instr_t		v;
1123     assert(!(op  & 0xfffffffc));
1124     assert(!(rd  & 0xffffffe0));
1125     assert(!(op3 & 0xffffffc0));
1126     assert(!(rs1 & 0xffffffe0));
1127     assert(!(asi & 0xffffff00));
1128     assert(!(rs2 & 0xffffffe0));
1129     v.op.b    = op;
1130     v.rd.b    = rd;
1131     v.op3.b   = op3;
1132     v.rs1.b   = rs1;
1133     v.i.b     = 0;
1134     v.asi.b   = asi;
1135     v.rs2.b   = rs2;
1136     ii(v.v);
1137 }
1138 
1139 static void
_f1(jit_state_t * _jit,jit_int32_t op,jit_int32_t disp30)1140 _f1(jit_state_t *_jit, jit_int32_t op, jit_int32_t disp30)
1141 {
1142     jit_instr_t		v;
1143     assert(!(op  & 0xfffffffc));
1144     assert(s30_p(disp30));
1145     v.op.b     = op;
1146     v.disp30.b = disp30;
1147     ii(v.v);
1148 }
1149 
1150 static void
_nop(jit_state_t * _jit,jit_int32_t i0)1151 _nop(jit_state_t *_jit, jit_int32_t i0)
1152 {
1153     for (; i0 > 0; i0 -= 4)
1154 	NOP();
1155     assert(i0 == 0);
1156 }
1157 
1158 static void
_movr(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1)1159 _movr(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1)
1160 {
1161     if (r0 != r1)
1162 	ORI(r1, 0, r0);
1163 }
1164 
1165 static void
_movi(jit_state_t * _jit,jit_int32_t r0,jit_word_t i0)1166 _movi(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
1167 {
1168     if (s13_p(i0))
1169 	ORI(0, i0, r0);
1170     else {
1171 #  if __WORDSIZE == 64
1172 	if (i0 & 0xffffffff00000000) {
1173 	    jit_int32_t	reg = jit_get_reg(jit_class_gpr);
1174 	    movi(rn(reg), (i0 >> 32) & 0xffffffff);
1175 	    movi(r0, i0 & 0xffffffff);
1176 	    lshi(rn(reg), rn(reg), 32);
1177 	    OR(rn(reg), r0, r0);
1178 	    jit_unget_reg(reg);
1179 	}
1180 	else {
1181 #  endif
1182 	    SETHI(HI((int)i0), r0);
1183 	    if (LO(i0))
1184 		ORI(r0, LO(i0), r0);
1185 #  if __WORDSIZE == 64
1186 	}
1187 #  endif
1188     }
1189 }
1190 
1191 static jit_word_t
_movi_p(jit_state_t * _jit,jit_int32_t r0,jit_word_t i0)1192 _movi_p(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
1193 {
1194     jit_word_t		w;
1195 #  if __WORDSIZE == 64
1196     jit_int32_t		reg;
1197 #  endif
1198     w = _jit->pc.w;
1199 #  if __WORDSIZE == 64
1200     reg = jit_get_reg(jit_class_gpr);
1201     SETHI(HI((int)i0), r0);
1202     ORI(r0, LO(i0), r0);
1203     i0 = (int)(i0 >> 32);
1204     SETHI(HI(i0), rn(reg));
1205     ORI(rn(reg), LO(i0), rn(reg));
1206     SLLXI(rn(reg), 32, rn(reg));
1207     OR(rn(reg), r0, r0);
1208     jit_unget_reg(reg);
1209 #  else
1210     SETHI(HI(i0), r0);
1211     ORI(r0, LO(i0), r0);
1212 #  endif
1213     return (w);
1214 }
1215 
1216 static void
_addi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1217 _addi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1218 {
1219     jit_int32_t		reg;
1220     if (s13_p(i0))
1221 	ADDI(r1, i0, r0);
1222     else {
1223 	reg = jit_get_reg(jit_class_gpr);
1224 	movi(rn(reg), i0);
1225 	addr(r0, r1, rn(reg));
1226 	jit_unget_reg(reg);
1227     }
1228 }
1229 
1230 #  if __WORDSIZE == 64
1231 static void
_addcr(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)1232 _addcr(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_int32_t r2)
1233 {
1234     jit_int32_t		reg;
1235     if (jit_carry == _NOREG)
1236 	jit_carry = jit_get_reg(jit_class_gpr);
1237     if (r0 == r1) {
1238 	reg = jit_get_reg(jit_class_gpr);
1239 	addr(rn(reg), r1, r2);
1240 	ltr_u(rn(jit_carry), rn(reg), r1);
1241 	movr(r0, rn(reg));
1242 	jit_unget_reg(reg);
1243     }
1244     else {
1245 	addr(r0, r1, r2);
1246 	ltr_u(rn(jit_carry), r0, r1);
1247     }
1248 }
1249 #  endif
1250 
1251 static void
_addci(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1252 _addci(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1253 {
1254 #  if __WORDSIZE == 32
1255     jit_int32_t		reg;
1256     if (s13_p(i0))
1257 	ADDIcc(r1, i0, r0);
1258     else {
1259 	reg = jit_get_reg(jit_class_gpr);
1260 	movi(rn(reg), i0);
1261 	addcr(r0, r1, rn(reg));
1262 	jit_unget_reg(reg);
1263     }
1264 #  else
1265     jit_int32_t		reg;
1266     if (jit_carry == _NOREG)
1267 	jit_carry = jit_get_reg(jit_class_gpr);
1268     if (r0 == r1) {
1269 	reg = jit_get_reg(jit_class_gpr);
1270 	addi(rn(reg), r1, i0);
1271 	ltr_u(rn(jit_carry), rn(reg), r1);
1272 	movr(r0, rn(reg));
1273 	jit_unget_reg(reg);
1274     }
1275     else {
1276 	addi(r0, r1, i0);
1277 	ltr_u(rn(jit_carry), r0, r1);
1278     }
1279 #  endif
1280 }
1281 
1282 #  if __WORDSIZE == 64
1283 static void
_addxr(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)1284 _addxr(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_int32_t r2)
1285 {
1286     jit_int32_t		reg;
1287     assert(jit_carry != _NOREG);
1288     reg = jit_get_reg(jit_class_gpr);
1289     movr(rn(reg), rn(jit_carry));
1290     addcr(r0, r1, r2);
1291     addcr(r0, r0, rn(reg));
1292     jit_unget_reg(reg);
1293 }
1294 #  endif
1295 
1296 static void
_addxi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1297 _addxi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1298 {
1299 #  if __WORDSIZE == 32
1300     jit_int32_t		reg;
1301     if (s13_p(i0))
1302 	ADDXIcc(r1, i0, r0);
1303     else {
1304 	reg = jit_get_reg(jit_class_gpr);
1305 	movi(rn(reg), i0);
1306 	addxr(r0, r1, rn(reg));
1307 	jit_unget_reg(reg);
1308     }
1309 #  else
1310     jit_int32_t		reg;
1311     assert(jit_carry != _NOREG);
1312     reg = jit_get_reg(jit_class_gpr);
1313     movr(rn(reg), rn(jit_carry));
1314     addci(r0, r1, i0);
1315     addcr(r0, r0, rn(reg));
1316     jit_unget_reg(reg);
1317 #  endif
1318 }
1319 
1320 static void
_subi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1321 _subi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1322 {
1323     jit_int32_t		reg;
1324     if (s13_p(i0))
1325 	SUBI(r1, i0, r0);
1326     else {
1327 	reg = jit_get_reg(jit_class_gpr);
1328 	movi(rn(reg), i0);
1329 	subr(r0, r1, rn(reg));
1330 	jit_unget_reg(reg);
1331     }
1332 }
1333 
1334 #  if __WORDSIZE == 64
1335 static void
_subcr(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)1336 _subcr(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_int32_t r2)
1337 {
1338     jit_int32_t		reg;
1339     if (jit_carry == _NOREG)
1340 	jit_carry = jit_get_reg(jit_class_gpr);
1341     if (r0 == r1) {
1342 	reg = jit_get_reg(jit_class_gpr);
1343 	subr(rn(reg), r1, r2);
1344 	ltr_u(rn(jit_carry), r1, rn(reg));
1345 	movr(r0, rn(reg));
1346 	jit_unget_reg(reg);
1347     }
1348     else {
1349 	subr(r0, r1, r2);
1350 	ltr_u(rn(jit_carry), r1, r0);
1351     }
1352 }
1353 #  endif
1354 
1355 static void
_subci(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1356 _subci(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1357 {
1358 #  if __WORDSIZE == 32
1359     jit_int32_t		reg;
1360     if (s13_p(i0))
1361 	SUBIcc(r1, i0, r0);
1362     else {
1363 	reg = jit_get_reg(jit_class_gpr);
1364 	movi(rn(reg), i0);
1365 	subcr(r0, r1, rn(reg));
1366 	jit_unget_reg(reg);
1367     }
1368 #  else
1369     jit_int32_t		reg;
1370     if (jit_carry == _NOREG)
1371 	jit_carry = jit_get_reg(jit_class_gpr);
1372     if (r0 == r1) {
1373 	reg = jit_get_reg(jit_class_gpr);
1374 	addi(rn(reg), r1, -i0);
1375 	ltr_u(rn(jit_carry), r1, rn(reg));
1376 	movr(r0, rn(reg));
1377 	jit_unget_reg(reg);
1378     }
1379     else {
1380 	addi(r0, r1, -i0);
1381 	ltr_u(rn(jit_carry), r1, r0);
1382     }
1383 #  endif
1384 }
1385 
1386 #  if __WORDSIZE == 64
1387 static void
_subxr(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)1388 _subxr(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_int32_t r2)
1389 {
1390     jit_int32_t		reg;
1391     assert(jit_carry != _NOREG);
1392     reg = jit_get_reg(jit_class_gpr);
1393     movr(rn(reg), rn(jit_carry));
1394     subcr(r0, r1, r2);
1395     subcr(r0, r0, rn(reg));
1396     jit_unget_reg(reg);
1397 }
1398 #endif
1399 
1400 static void
_subxi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1401 _subxi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1402 {
1403 #  if __WORDSIZE == 32
1404     jit_int32_t		reg;
1405     if (s13_p(i0))
1406 	SUBXIcc(r1, i0, r0);
1407     else {
1408 	reg = jit_get_reg(jit_class_gpr);
1409 	movi(rn(reg), i0);
1410 	subxr(r0, r1, rn(reg));
1411 	jit_unget_reg(reg);
1412     }
1413 #  else
1414     jit_int32_t		reg;
1415     assert(jit_carry != _NOREG);
1416     reg = jit_get_reg(jit_class_gpr);
1417     movr(rn(reg), rn(jit_carry));
1418     subci(r0, r1, i0);
1419     subcr(r0, r0, rn(reg));
1420     jit_unget_reg(reg);
1421 #  endif
1422 }
1423 
1424 static void
_rsbi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1425 _rsbi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1426 {
1427     subi(r0, r1, i0);
1428     negr(r0, r0);
1429 }
1430 
1431 static void
_muli(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1432 _muli(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1433 {
1434     jit_int32_t		reg;
1435     if (s13_p(i0)) {
1436 #  if __WORDSIZE == 32
1437 	UMULI(r1, i0, r0);
1438 #  else
1439 	MULXI(r1, i0, r0);
1440 #  endif
1441     }
1442     else {
1443 	reg = jit_get_reg(jit_class_gpr);
1444 	movi(rn(reg), i0);
1445 	mulr(r0, r1, rn(reg));
1446 	jit_unget_reg(reg);
1447     }
1448 }
1449 
1450 #  if __WORDSIZE == 32
1451 static void
_iqmulr(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2,jit_int32_t r3,jit_bool_t sign)1452 _iqmulr(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1,
1453 	jit_int32_t r2, jit_int32_t r3, jit_bool_t sign)
1454 {
1455     if (sign)
1456 	SMUL(r2, r3, r0);
1457     else
1458 	UMUL(r2, r3, r0);
1459     RDY(r1);
1460 }
1461 
1462 static void
_iqmuli(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2,jit_word_t i0,jit_bool_t sign)1463 _iqmuli(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1,
1464 	jit_int32_t r2, jit_word_t i0, jit_bool_t sign)
1465 {
1466     jit_int32_t		reg;
1467     if (s13_p(i0)) {
1468 	if (sign)
1469 	    SMULI(r2, i0, r0);
1470 	else
1471 	    UMULI(r2, i0, r0);
1472 	RDY(r1);
1473     }
1474     else {
1475 	reg = jit_get_reg(jit_class_gpr);
1476 	movi(rn(reg), i0);
1477 	iqmulr(r0, r1, r2, rn(reg), sign);
1478 	jit_unget_reg(reg);
1479     }
1480 }
1481 
1482 #  else
__llmul(jit_word_t a,jit_word_t b)1483 static __int128_t __llmul(jit_word_t a, jit_word_t b)
1484 {
1485     return (__int128_t)a * (__int128_t)b;
1486 }
1487 
1488 #  define QMUL_PROLOG()						\
1489     do {							\
1490 	(void)jit_get_reg(_O0|jit_class_gpr|jit_class_named);	\
1491 	(void)jit_get_reg(_O1|jit_class_gpr|jit_class_named);	\
1492 	if (r0 != _G2_REGNO && r1 != _G2_REGNO)			\
1493 	    stxi(BIAS(-8), _FP_REGNO, _G2_REGNO);		\
1494 	if (r0 != _G3_REGNO && r1 != _G3_REGNO)			\
1495 	    stxi(BIAS(-16), _FP_REGNO, _G3_REGNO);		\
1496 	if (r0 != _G4_REGNO && r1 != _G4_REGNO)			\
1497 	    stxi(BIAS(-24), _FP_REGNO, _G4_REGNO);		\
1498     } while (0)
1499 
1500 #  define QMUL_EPILOG()						\
1501     do {							\
1502 	if (r0 != _G2_REGNO && r1 != _G2_REGNO)			\
1503 	    ldxi(_G2_REGNO, _FP_REGNO, BIAS(-8));		\
1504 	if (r0 != _G3_REGNO && r1 != _G3_REGNO)			\
1505 	    ldxi(_G3_REGNO, _FP_REGNO, BIAS(-16));		\
1506 	if (r0 != _G4_REGNO && r1 != _G4_REGNO)			\
1507 	    ldxi(_G4_REGNO, _FP_REGNO, BIAS(-24));		\
1508 	(void)jit_unget_reg(_O0);				\
1509 	(void)jit_unget_reg(_O1);				\
1510     } while (0)
1511 
1512 static void
_qmulr(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2,jit_int32_t r3)1513 _qmulr(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1,
1514        jit_int32_t r2, jit_int32_t r3)
1515 {
1516     QMUL_PROLOG();
1517     movr(_O0_REGNO, r3);
1518     movr(_O1_REGNO, r2);
1519     calli((jit_word_t)__llmul);
1520     movr(r0, _O1_REGNO);
1521     movr(r1, _O0_REGNO);
1522     QMUL_EPILOG();
1523 }
1524 
1525 static void
_qmuli(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2,jit_word_t i0)1526 _qmuli(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1,
1527        jit_int32_t r2, jit_word_t i0)
1528 {
1529     QMUL_PROLOG();
1530     movi(_O0_REGNO, i0);
1531     movr(_O1_REGNO, r2);
1532     calli((jit_word_t)__llmul);
1533     movr(r0, _O1_REGNO);
1534     movr(r1, _O0_REGNO);
1535     QMUL_EPILOG();
1536 }
1537 
__ullmul(jit_uword_t a,jit_uword_t b)1538 static __uint128_t __ullmul(jit_uword_t a, jit_uword_t b)
1539 {
1540     return (__uint128_t)a * (__uint128_t)b;
1541 }
1542 
1543 static void
_qmulr_u(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2,jit_int32_t r3)1544 _qmulr_u(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1,
1545 	 jit_int32_t r2, jit_int32_t r3)
1546 {
1547     QMUL_PROLOG();
1548     movr(_O0_REGNO, r3);
1549     movr(_O1_REGNO, r2);
1550     calli((jit_word_t)__ullmul);
1551     movr(r0, _O1_REGNO);
1552     movr(r1, _O0_REGNO);
1553     QMUL_EPILOG();
1554 }
1555 
1556 static void
_qmuli_u(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2,jit_word_t i0)1557 _qmuli_u(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1,
1558 	 jit_int32_t r2, jit_word_t i0)
1559 {
1560     QMUL_PROLOG();
1561     movi(_O0_REGNO, i0);
1562     movr(_O1_REGNO, r2);
1563     calli((jit_word_t)__ullmul);
1564     movr(r0, _O1_REGNO);
1565     movr(r1, _O0_REGNO);
1566     QMUL_EPILOG();
1567 }
1568 #  endif
1569 
1570 static void
_divr(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)1571 _divr(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_int32_t r2)
1572 {
1573 #  if __WORDSIZE == 32
1574     jit_int32_t		reg;
1575     reg = jit_get_reg(jit_class_gpr);
1576     rshi(rn(reg), r1, 31);
1577     WRY(rn(reg), 0);
1578     SDIV(r1, r2, r0);
1579     jit_unget_reg(reg);
1580 #  else
1581     SDIVX(r1, r2, r0);
1582 #  endif
1583 }
1584 
1585 static void
_divi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1586 _divi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1587 {
1588     jit_int32_t		reg;
1589 #  if __WORDSIZE == 32
1590     reg = jit_get_reg(jit_class_gpr);
1591 #  endif
1592     if (s13_p(i0)) {
1593 #  if __WORDSIZE == 32
1594 	rshi(rn(reg), r1, 31);
1595 	WRY(rn(reg), 0);
1596 	SDIVI(r1, i0, r0);
1597 #  else
1598 	SDIVXI(r1, i0, r0);
1599 #  endif
1600     }
1601     else {
1602 #  if __WORDSIZE == 64
1603 	reg = jit_get_reg(jit_class_gpr);
1604 #  endif
1605 	movi(rn(reg), i0);
1606 	divr(r0, r1, rn(reg));
1607 #  if __WORDSIZE == 64
1608 	jit_unget_reg(reg);
1609 #  endif
1610     }
1611 #  if __WORDSIZE == 32
1612     jit_unget_reg(reg);
1613 #  endif
1614 }
1615 
1616 static void
_divr_u(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)1617 _divr_u(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_int32_t r2)
1618 {
1619 #  if __WORDSIZE == 32
1620     WRYI(0, 0);
1621     UDIV(r1, r2, r0);
1622 #  else
1623     UDIVX(r1, r2, r0);
1624 #  endif
1625 }
1626 
1627 static void
_divi_u(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1628 _divi_u(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1629 {
1630     jit_int32_t		reg;
1631     if (s13_p(i0)) {
1632 #  if __WORDSIZE == 32
1633 	WRYI(0, 0);
1634 	UDIVI(r1, i0, r0);
1635 #  else
1636 	UDIVXI(r1, i0, r0);
1637 #  endif
1638     }
1639     else {
1640 	reg = jit_get_reg(jit_class_gpr);
1641 	movi(rn(reg), i0);
1642 	divr_u(r0, r1, rn(reg));
1643 	jit_unget_reg(reg);
1644     }
1645 }
1646 
1647 static void
_iqdivr(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2,jit_int32_t r3,jit_bool_t sign)1648 _iqdivr(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1,
1649 	jit_int32_t r2, jit_int32_t r3, jit_bool_t sign)
1650 {
1651     jit_int32_t		sv0, rg0;
1652     jit_int32_t		sv1, rg1;
1653 
1654     if (r0 == r2 || r0 == r3) {
1655 	sv0 = jit_get_reg(jit_class_gpr);
1656 	rg0 = rn(sv0);
1657     }
1658     else
1659 	rg0 = r0;
1660     if (r1 == r2 || r1 == r3) {
1661 	sv1 = jit_get_reg(jit_class_gpr);
1662 	rg1 = rn(sv1);
1663     }
1664     else
1665 	rg1 = r1;
1666 
1667     if (sign)
1668 	divr(rg0, r2, r3);
1669     else
1670 	divr_u(rg0, r2, r3);
1671     mulr(rg1, r3, rg0);
1672     subr(rg1, r2, rg1);
1673     if (rg0 != r0) {
1674 	movr(r0, rg0);
1675 	jit_unget_reg(sv0);
1676     }
1677     if (rg1 != r1) {
1678 	movr(r1, rg1);
1679 	jit_unget_reg(sv1);
1680     }
1681 }
1682 
1683 static void
_iqdivi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2,jit_word_t i0,jit_bool_t sign)1684 _iqdivi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1,
1685 	jit_int32_t r2, jit_word_t i0, jit_bool_t sign)
1686 {
1687     jit_int32_t		reg;
1688     reg = jit_get_reg(jit_class_gpr);
1689     movi(rn(reg), i0);
1690     iqdivr(r0, r1, r2, rn(reg), sign);
1691     jit_unget_reg(reg);
1692 }
1693 
1694 static void
_remr(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)1695 _remr(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_int32_t r2)
1696 {
1697     jit_int32_t		reg;
1698     if (r0 == r1 || r0 == r2) {
1699 	reg = jit_get_reg(jit_class_gpr);
1700 	divr(rn(reg), r1, r2);
1701 	mulr(rn(reg), r2, rn(reg));
1702 	subr(r0, r1, rn(reg));
1703 	jit_unget_reg(reg);
1704     }
1705     else {
1706 	divr(r0, r1, r2);
1707 	mulr(r0, r2, r0);
1708 	subr(r0, r1, r0);
1709     }
1710 }
1711 
1712 static void
_remi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1713 _remi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1714 {
1715     jit_int32_t		reg;
1716     reg = jit_get_reg(jit_class_gpr);
1717     movi(rn(reg), i0);
1718     remr(r0, r1, rn(reg));
1719     jit_unget_reg(reg);
1720 }
1721 
1722 static void
_remr_u(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)1723 _remr_u(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_int32_t r2)
1724 {
1725     jit_int32_t		reg;
1726     if (r0 == r1 || r0 == r2) {
1727 	reg = jit_get_reg(jit_class_gpr);
1728 	divr_u(rn(reg), r1, r2);
1729 	mulr(rn(reg), r2, rn(reg));
1730 	subr(r0, r1, rn(reg));
1731 	jit_unget_reg(reg);
1732     }
1733     else {
1734 	divr_u(r0, r1, r2);
1735 	mulr(r0, r2, r0);
1736 	subr(r0, r1, r0);
1737     }
1738 }
1739 
1740 static void
_remi_u(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1741 _remi_u(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1742 {
1743     jit_int32_t		reg;
1744     reg = jit_get_reg(jit_class_gpr);
1745     movi(rn(reg), i0);
1746     remr_u(r0, r1, rn(reg));
1747     jit_unget_reg(reg);
1748 }
1749 
1750 static void
_andi(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1751 _andi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1752 {
1753     jit_int32_t		reg;
1754     if (s13_p(i0))
1755 	ANDI(r1, i0, r0);
1756     else {
1757 	reg = jit_get_reg(jit_class_gpr);
1758 	movi(rn(reg), i0);
1759 	andr(r0, r1, rn(reg));
1760 	jit_unget_reg(reg);
1761     }
1762 }
1763 
1764 static void
_ori(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1765 _ori(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1766 {
1767     jit_int32_t		reg;
1768     if (s13_p(i0))
1769 	ORI(r1, i0, r0);
1770     else {
1771 	reg = jit_get_reg(jit_class_gpr);
1772 	movi(rn(reg), i0);
1773 	orr(r0, r1, rn(reg));
1774 	jit_unget_reg(reg);
1775     }
1776 }
1777 
1778 static void
_xori(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1779 _xori(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1780 {
1781     jit_int32_t		reg;
1782     if (s13_p(i0))
1783 	XORI(r1, i0, r0);
1784     else {
1785 	reg = jit_get_reg(jit_class_gpr);
1786 	movi(rn(reg), i0);
1787 	xorr(r0, r1, rn(reg));
1788 	jit_unget_reg(reg);
1789     }
1790 }
1791 
1792 static void
_extr_c(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1)1793 _extr_c(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1)
1794 {
1795     lshi(r0, r1, __WORDSIZE - 8);
1796     rshi(r0, r0, __WORDSIZE - 8);
1797 }
1798 
1799 static void
_extr_s(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1)1800 _extr_s(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1)
1801 {
1802     lshi(r0, r1, __WORDSIZE - 16);
1803     rshi(r0, r0, __WORDSIZE - 16);
1804 }
1805 
1806 static void
_extr_us(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1)1807 _extr_us(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1)
1808 {
1809     lshi(r0, r1, __WORDSIZE - 16);
1810     rshi_u(r0, r0, __WORDSIZE - 16);
1811 }
1812 
1813 #if __WORDSIZE == 64
1814 static void
_extr_i(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1)1815 _extr_i(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1)
1816 {
1817     lshi(r0, r1, __WORDSIZE - 32);
1818     rshi(r0, r0, __WORDSIZE - 32);
1819 }
1820 
1821 static void
_extr_ui(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1)1822 _extr_ui(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1)
1823 {
1824     lshi(r0, r1, __WORDSIZE - 32);
1825     rshi_u(r0, r0, __WORDSIZE - 32);
1826 }
1827 #endif
1828 
1829 static void
_cr(jit_state_t * _jit,jit_int32_t cc,jit_int32_t r0,jit_int32_t r1,jit_int32_t r2)1830 _cr(jit_state_t *_jit, jit_int32_t cc,
1831     jit_int32_t r0, jit_int32_t r1, jit_int32_t r2)
1832 {
1833     CMP(r1, r2);
1834 #  if __WORDSIZE == 32
1835     Ba(cc, 3);
1836 #  else
1837     BPa(cc, 3);
1838 #  endif
1839     movi(r0, 1);
1840     movi(r0, 0);
1841 }
1842 
1843 static void
_cw(jit_state_t * _jit,jit_int32_t cc,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1844 _cw(jit_state_t *_jit, jit_int32_t cc,
1845     jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1846 {
1847     jit_int32_t		reg;
1848     if (s13_p(i0)) {
1849 	CMPI(r1, i0);
1850 #  if __WORDSIZE == 32
1851 	Ba(cc, 3);
1852 #  else
1853 	BPa(cc, 3);
1854 #  endif
1855 	movi(r0, 1);
1856 	movi(r0, 0);
1857     }
1858     else {
1859 	reg = jit_get_reg(jit_class_gpr);
1860 	movi(rn(reg), i0);
1861 	cr(cc, r0, r1, rn(reg));
1862 	jit_unget_reg(reg);
1863     }
1864 }
1865 
1866 static void
_ldi_c(jit_state_t * _jit,jit_int32_t r0,jit_word_t i0)1867 _ldi_c(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
1868 {
1869     jit_int32_t		reg;
1870     if (s13_p(i0))
1871 	LDSBI(0, i0, r0);
1872     else {
1873 	reg = jit_get_reg(jit_class_gpr);
1874 	movi(rn(reg), i0);
1875 	ldr_c(r0, rn(reg));
1876 	jit_unget_reg(reg);
1877     }
1878 }
1879 
1880 static void
_ldi_uc(jit_state_t * _jit,jit_int32_t r0,jit_word_t i0)1881 _ldi_uc(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
1882 {
1883     jit_int32_t		reg;
1884     if (s13_p(i0))
1885 	LDUBI(0, i0, r0);
1886     else {
1887 	reg = jit_get_reg(jit_class_gpr);
1888 	movi(rn(reg), i0);
1889 	ldr_uc(r0, rn(reg));
1890 	jit_unget_reg(reg);
1891     }
1892 }
1893 
1894 static void
_ldi_s(jit_state_t * _jit,jit_int32_t r0,jit_word_t i0)1895 _ldi_s(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
1896 {
1897     jit_int32_t		reg;
1898     if (s13_p(i0))
1899 	LDSHI(0, i0, r0);
1900     else {
1901 	reg = jit_get_reg(jit_class_gpr);
1902 	movi(rn(reg), i0);
1903 	ldr_s(r0, rn(reg));
1904 	jit_unget_reg(reg);
1905     }
1906 }
1907 
1908 static void
_ldi_us(jit_state_t * _jit,jit_int32_t r0,jit_word_t i0)1909 _ldi_us(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
1910 {
1911     jit_int32_t		reg;
1912     if (s13_p(i0))
1913 	LDUHI(0, i0, r0);
1914     else {
1915 	reg = jit_get_reg(jit_class_gpr);
1916 	movi(rn(reg), i0);
1917 	ldr_us(r0, rn(reg));
1918 	jit_unget_reg(reg);
1919     }
1920 }
1921 
1922 static void
_ldi_i(jit_state_t * _jit,jit_int32_t r0,jit_word_t i0)1923 _ldi_i(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
1924 {
1925     jit_int32_t		reg;
1926     if (s13_p(i0)) {
1927 #  if __WORDSIZE == 32
1928 	LDI(0, i0, r0);
1929 #  else
1930 	LDSWI(0, i0, r0);
1931 #  endif
1932     }
1933     else {
1934 	reg = jit_get_reg(jit_class_gpr);
1935 	movi(rn(reg), i0);
1936 	ldr_i(r0, rn(reg));
1937 	jit_unget_reg(reg);
1938     }
1939 }
1940 
1941 #  if __WORDSIZE == 64
1942 static void
_ldi_ui(jit_state_t * _jit,jit_int32_t r0,jit_word_t i0)1943 _ldi_ui(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
1944 {
1945     jit_int32_t		reg;
1946     if (s13_p(i0))
1947 	LDUWI(0, i0, r0);
1948     else {
1949 	reg = jit_get_reg(jit_class_gpr);
1950 	movi(rn(reg), i0);
1951 	ldr_ui(r0, rn(reg));
1952 	jit_unget_reg(reg);
1953     }
1954 }
1955 
1956 static void
_ldi_l(jit_state_t * _jit,jit_int32_t r0,jit_word_t i0)1957 _ldi_l(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
1958 {
1959     jit_int32_t		reg;
1960     if (s13_p(i0))
1961 	LDXI(0, i0, r0);
1962     else {
1963 	reg = jit_get_reg(jit_class_gpr);
1964 	movi(rn(reg), i0);
1965 	ldr_l(r0, rn(reg));
1966 	jit_unget_reg(reg);
1967     }
1968 }
1969 #  endif
1970 
1971 static void
_ldxi_c(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1972 _ldxi_c(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1973 {
1974     jit_int32_t		reg;
1975     if (s13_p(i0))
1976 	LDSBI(r1, i0, r0);
1977     else {
1978 	reg = jit_get_reg(jit_class_gpr);
1979 	movi(rn(reg), i0);
1980 	ldxr_c(r0, r1, rn(reg));
1981 	jit_unget_reg(reg);
1982     }
1983 }
1984 
1985 static void
_ldxi_uc(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)1986 _ldxi_uc(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1987 {
1988     jit_int32_t		reg;
1989     if (s13_p(i0))
1990 	LDUBI(r1, i0, r0);
1991     else {
1992 	reg = jit_get_reg(jit_class_gpr);
1993 	movi(rn(reg), i0);
1994 	ldxr_uc(r0, r1, rn(reg));
1995 	jit_unget_reg(reg);
1996     }
1997 }
1998 
1999 static void
_ldxi_s(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)2000 _ldxi_s(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
2001 {
2002     jit_int32_t		reg;
2003     if (s13_p(i0))
2004 	LDSHI(r1, i0, r0);
2005     else {
2006 	reg = jit_get_reg(jit_class_gpr);
2007 	movi(rn(reg), i0);
2008 	ldxr_s(r0, r1, rn(reg));
2009 	jit_unget_reg(reg);
2010     }
2011 }
2012 
2013 static void
_ldxi_us(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)2014 _ldxi_us(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
2015 {
2016     jit_int32_t		reg;
2017     if (s13_p(i0))
2018 	LDUHI(r1, i0, r0);
2019     else {
2020 	reg = jit_get_reg(jit_class_gpr);
2021 	movi(rn(reg), i0);
2022 	ldxr_us(r0, r1, rn(reg));
2023 	jit_unget_reg(reg);
2024     }
2025 }
2026 
2027 static void
_ldxi_i(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)2028 _ldxi_i(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
2029 {
2030     jit_int32_t		reg;
2031     if (s13_p(i0)) {
2032 #  if __WORDSIZE == 32
2033 	LDI(r1, i0, r0);
2034 #  else
2035 	LDSWI(r1, i0, r0);
2036 #  endif
2037     }
2038     else {
2039 	reg = jit_get_reg(jit_class_gpr);
2040 	movi(rn(reg), i0);
2041 	ldxr_i(r0, r1, rn(reg));
2042 	jit_unget_reg(reg);
2043     }
2044 }
2045 
2046 #  if __WORDSIZE == 64
2047 static void
_ldxi_ui(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)2048 _ldxi_ui(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
2049 {
2050     jit_int32_t		reg;
2051     if (s13_p(i0))
2052 	LDUWI(r1, i0, r0);
2053     else {
2054 	reg = jit_get_reg(jit_class_gpr);
2055 	movi(rn(reg), i0);
2056 	ldxr_ui(r0, r1, rn(reg));
2057 	jit_unget_reg(reg);
2058     }
2059 }
2060 
2061 static void
_ldxi_l(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1,jit_word_t i0)2062 _ldxi_l(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
2063 {
2064     jit_int32_t		reg;
2065     if (s13_p(i0))
2066 	LDXI(r1, i0, r0);
2067     else {
2068 	reg = jit_get_reg(jit_class_gpr);
2069 	movi(rn(reg), i0);
2070 	ldxr_l(r0, r1, rn(reg));
2071 	jit_unget_reg(reg);
2072     }
2073 }
2074 #  endif
2075 
2076 static void
_sti_c(jit_state_t * _jit,jit_word_t i0,jit_int32_t r0)2077 _sti_c(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0)
2078 {
2079     jit_int32_t		reg;
2080     if (s13_p(i0))
2081 	STBI(r0, 0, i0);
2082     else {
2083 	reg = jit_get_reg(jit_class_gpr);
2084 	movi(rn(reg), i0);
2085 	str_c(rn(reg), r0);
2086 	jit_unget_reg(reg);
2087     }
2088 }
2089 
2090 static void
_sti_s(jit_state_t * _jit,jit_word_t i0,jit_int32_t r0)2091 _sti_s(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0)
2092 {
2093     jit_int32_t		reg;
2094     if (s13_p(i0))
2095 	STHI(r0, 0, i0);
2096     else {
2097 	reg = jit_get_reg(jit_class_gpr);
2098 	movi(rn(reg), i0);
2099 	str_s(rn(reg), r0);
2100 	jit_unget_reg(reg);
2101     }
2102 }
2103 
2104 static void
_sti_i(jit_state_t * _jit,jit_word_t i0,jit_int32_t r0)2105 _sti_i(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0)
2106 {
2107     jit_int32_t		reg;
2108     if (s13_p(i0)) {
2109 #  if __WORDSIZE == 32
2110 	STI(r0, 0, i0);
2111 #  else
2112 	STWI(r0, 0, i0);
2113 #  endif
2114     }
2115     else {
2116 	reg = jit_get_reg(jit_class_gpr);
2117 	movi(rn(reg), i0);
2118 	str_i(rn(reg), r0);
2119 	jit_unget_reg(reg);
2120     }
2121 }
2122 
2123 #  if __WORDSIZE == 64
2124 static void
_sti_l(jit_state_t * _jit,jit_word_t i0,jit_int32_t r0)2125 _sti_l(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0)
2126 {
2127     jit_int32_t		reg;
2128     if (s13_p(i0))
2129 	STXI(r0, 0, i0);
2130     else {
2131 	reg = jit_get_reg(jit_class_gpr);
2132 	movi(rn(reg), i0);
2133 	str_l(rn(reg), r0);
2134 	jit_unget_reg(reg);
2135     }
2136 }
2137 #  endif
2138 
2139 static void
_stxi_c(jit_state_t * _jit,jit_word_t i0,jit_int32_t r0,jit_int32_t r1)2140 _stxi_c(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_int32_t r1)
2141 {
2142     jit_int32_t		reg;
2143     if (s13_p(i0))
2144 	STBI(r1, r0, i0);
2145     else {
2146 	reg = jit_get_reg(jit_class_gpr);
2147 	movi(rn(reg), i0);
2148 	stxr_c(r0, rn(reg), r1);
2149 	jit_unget_reg(reg);
2150     }
2151 }
2152 
2153 static void
_stxi_s(jit_state_t * _jit,jit_word_t i0,jit_int32_t r0,jit_int32_t r1)2154 _stxi_s(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_int32_t r1)
2155 {
2156     jit_int32_t		reg;
2157     if (s13_p(i0))
2158 	STHI(r1, r0, i0);
2159     else {
2160 	reg = jit_get_reg(jit_class_gpr);
2161 	movi(rn(reg), i0);
2162 	stxr_s(r0, rn(reg), r1);
2163 	jit_unget_reg(reg);
2164     }
2165 }
2166 
2167 static void
_stxi_i(jit_state_t * _jit,jit_word_t i0,jit_int32_t r0,jit_int32_t r1)2168 _stxi_i(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_int32_t r1)
2169 {
2170     jit_int32_t		reg;
2171     if (s13_p(i0)) {
2172 #  if __WORDSIZE == 32
2173 	STI(r1, r0, i0);
2174 #  else
2175 	STWI(r1, r0, i0);
2176 #  endif
2177     }
2178     else {
2179 	reg = jit_get_reg(jit_class_gpr);
2180 	movi(rn(reg), i0);
2181 	stxr_i(r0, rn(reg), r1);
2182 	jit_unget_reg(reg);
2183     }
2184 }
2185 
2186 #  if __WORDSIZE == 64
2187 static void
_stxi_l(jit_state_t * _jit,jit_word_t i0,jit_int32_t r0,jit_int32_t r1)2188 _stxi_l(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_int32_t r1)
2189 {
2190     jit_int32_t		reg;
2191     if (s13_p(i0))
2192 	STXI(r1, r0, i0);
2193     else {
2194 	reg = jit_get_reg(jit_class_gpr);
2195 	movi(rn(reg), i0);
2196 	stxr_l(r0, rn(reg), r1);
2197 	jit_unget_reg(reg);
2198     }
2199 }
2200 #  endif
2201 
2202 static jit_word_t
_br(jit_state_t * _jit,jit_int32_t cc,jit_word_t i0,jit_int32_t r0,jit_int32_t r1)2203 _br(jit_state_t *_jit, jit_int32_t cc,
2204     jit_word_t i0, jit_int32_t r0, jit_int32_t r1)
2205 {
2206     jit_word_t		w;
2207     CMP(r0, r1);
2208     w = _jit->pc.w;
2209 #  if __WORDSIZE == 32
2210     B(cc, (i0 - w) >> 2);
2211 #  else
2212     BP(cc, (i0 - w) >> 2);
2213 #  endif
2214     NOP();
2215     return (w);
2216 }
2217 
2218 static jit_word_t
_bw(jit_state_t * _jit,jit_int32_t cc,jit_word_t i0,jit_int32_t r0,jit_word_t i1)2219 _bw(jit_state_t *_jit, jit_int32_t cc,
2220     jit_word_t i0, jit_int32_t r0, jit_word_t i1)
2221 {
2222     jit_word_t		w;
2223     jit_int32_t		reg;
2224     if (s13_p(i1)) {
2225 	CMPI(r0, i1);
2226 	w = _jit->pc.w;
2227 #  if __WORDSIZE == 32
2228 	B(cc, (i0 - w) >> 2);
2229 #  else
2230 	B(cc, (i0 - w) >> 2);
2231 #  endif
2232 	NOP();
2233     }
2234     else {
2235 	reg = jit_get_reg(jit_class_gpr|jit_class_nospill);
2236 	movi(rn(reg), i1);
2237 	w = br(cc, i0, r0, rn(reg));
2238 	jit_unget_reg(reg);
2239     }
2240     return (w);
2241 }
2242 
2243 static jit_word_t
_b_asr(jit_state_t * _jit,jit_bool_t jif,jit_bool_t add,jit_bool_t sgn,jit_word_t i0,jit_int32_t r0,jit_int32_t r1)2244 _b_asr(jit_state_t *_jit, jit_bool_t jif, jit_bool_t add, jit_bool_t sgn,
2245        jit_word_t i0, jit_int32_t r0, jit_int32_t r1)
2246 {
2247     jit_word_t		w;
2248     if (add)
2249 	ADDcc(r0, r1, r0);
2250     else
2251 	SUBcc(r0, r1, r0);
2252     w = _jit->pc.w;
2253 #  if __WORDSIZE == 32
2254     B(sgn ?
2255       (jif ? SPARC_BVS : SPARC_BVC) :
2256       (jif ? SPARC_BCS : SPARC_BCC),
2257       (i0 - w) >> 2);
2258 #  else
2259     BP(sgn ?
2260        (jif ? SPARC_BPVS : SPARC_BPVC) :
2261        (jif ? SPARC_BPCS : SPARC_BPCC),
2262        (i0 - w) >> 2);
2263 #  endif
2264     NOP();
2265     return (w);
2266 }
2267 
2268 static jit_word_t
_b_asw(jit_state_t * _jit,jit_bool_t jif,jit_bool_t add,jit_bool_t sgn,jit_word_t i0,jit_int32_t r0,jit_word_t i1)2269 _b_asw(jit_state_t *_jit, jit_bool_t jif, jit_bool_t add, jit_bool_t sgn,
2270        jit_word_t i0, jit_int32_t r0, jit_word_t i1)
2271 {
2272     jit_word_t		w;
2273     jit_int32_t		reg;
2274     if (s13_p(i1)) {
2275 	if (add)
2276 	    ADDIcc(r0, i1, r0);
2277 	else
2278 	    SUBIcc(r0, i1, r0);
2279 	w = _jit->pc.w;
2280 #  if __WORDSIZE == 32
2281 	B(sgn ?
2282 	  (jif ? SPARC_BVS : SPARC_BVC) :
2283 	  (jif ? SPARC_BCS : SPARC_BCC),
2284 	  (i0 - w) >> 2);
2285 #  else
2286 	BP(sgn ?
2287 	   (jif ? SPARC_BPVS : SPARC_BPVC) :
2288 	   (jif ? SPARC_BPCS : SPARC_BPCC),
2289 	   (i0 - w) >> 2);
2290 #  endif
2291 	NOP();
2292     }
2293     else {
2294 	reg = jit_get_reg(jit_class_gpr|jit_class_nospill);
2295 	movi(rn(reg), i1);
2296 	w = b_asr(jif, add, sgn, i0, r0, rn(reg));
2297 	jit_unget_reg(reg);
2298     }
2299     return (w);
2300 }
2301 
2302 static jit_word_t
_bm_r(jit_state_t * _jit,jit_bool_t set,jit_word_t i0,jit_int32_t r0,jit_int32_t r1)2303 _bm_r(jit_state_t *_jit, jit_bool_t set,
2304       jit_word_t i0, jit_int32_t r0, jit_int32_t r1)
2305 {
2306     jit_word_t		w;
2307     BTST(r0, r1);
2308     w = _jit->pc.w;
2309 #  if __WORDSIZE == 32
2310     B(set ? SPARC_BNZ : SPARC_BZ, (i0 - w) >> 2);
2311 #  else
2312     BP(set ? SPARC_BPNE : SPARC_BPE, (i0 - w) >> 2);
2313 #  endif
2314     NOP();
2315     return (w);
2316 }
2317 
2318 static jit_word_t
_bm_w(jit_state_t * _jit,jit_bool_t set,jit_word_t i0,jit_int32_t r0,jit_word_t i1)2319 _bm_w(jit_state_t *_jit, jit_bool_t set,
2320       jit_word_t i0, jit_int32_t r0, jit_word_t i1)
2321 {
2322     jit_word_t		w;
2323     jit_int32_t		reg;
2324     if (s13_p(i1)) {
2325 	BTSTI(r0, i1);
2326 	w = _jit->pc.w;
2327 #  if __WORDSIZE == 32
2328 	B(set ? SPARC_BNZ : SPARC_BZ, (i0 - w) >> 2);
2329 #  else
2330 	BP(set ? SPARC_BPNE : SPARC_BPE, (i0 - w) >> 2);
2331 #  endif
2332 	NOP();
2333     }
2334     else {
2335 	reg = jit_get_reg(jit_class_gpr|jit_class_nospill);
2336 	movi(rn(reg), i1);
2337 	w = bm_r(set, i0, r0, rn(reg));
2338 	jit_unget_reg(reg);
2339     }
2340     return (w);
2341 }
2342 
2343 static void
_jmpr(jit_state_t * _jit,jit_int32_t r0)2344 _jmpr(jit_state_t *_jit, jit_int32_t r0)
2345 {
2346     JMPL(0, r0, 0);
2347     NOP();
2348 }
2349 
2350 static void
_jmpi(jit_state_t * _jit,jit_word_t i0)2351 _jmpi(jit_state_t *_jit, jit_word_t i0)
2352 {
2353     jit_word_t		w;
2354     jit_int32_t		reg;
2355     w = (i0 - _jit->pc.w) >> 2;
2356     if (s22_p(w)) {
2357 	BA(w);
2358 	NOP();
2359     }
2360     else {
2361 	reg = jit_get_reg(jit_class_gpr|jit_class_nospill);
2362 	movi(rn(reg), i0);
2363 	jmpr(rn(reg));
2364 	jit_unget_reg(reg);
2365     }
2366 }
2367 
2368 static jit_word_t
_jmpi_p(jit_state_t * _jit,jit_word_t i0)2369 _jmpi_p(jit_state_t *_jit, jit_word_t i0)
2370 {
2371     jit_word_t		w;
2372     jit_int32_t		reg;
2373     reg = jit_get_reg(jit_class_gpr|jit_class_nospill);
2374     w = movi_p(rn(reg), i0);
2375     jmpr(rn(reg));
2376     jit_unget_reg(reg);
2377     return (w);
2378 }
2379 
2380 static void
_callr(jit_state_t * _jit,jit_int32_t r0)2381 _callr(jit_state_t *_jit, jit_int32_t r0)
2382 {
2383     CALL(r0);
2384     NOP();
2385 }
2386 
2387 static void
_calli(jit_state_t * _jit,jit_word_t i0)2388 _calli(jit_state_t *_jit, jit_word_t i0)
2389 {
2390     jit_word_t		w;
2391     w = (i0 - _jit->pc.w) >> 2;
2392     CALLI(w);
2393     NOP();
2394 }
2395 
2396 static jit_word_t
_calli_p(jit_state_t * _jit,jit_word_t i0)2397 _calli_p(jit_state_t *_jit, jit_word_t i0)
2398 {
2399     jit_word_t		w;
2400     jit_int32_t		reg;
2401     reg = jit_get_reg(jit_class_gpr);
2402     w = movi_p(rn(reg), i0);
2403     callr(rn(reg));
2404     jit_unget_reg(reg);
2405     return (w);
2406 }
2407 
2408 #define OFF(n)		BIAS(((n) * sizeof(jit_word_t)))
2409 static void
_prolog(jit_state_t * _jit,jit_node_t * node)2410 _prolog(jit_state_t *_jit, jit_node_t *node)
2411 {
2412     jit_int32_t		reg;
2413     if (_jitc->function->define_frame || _jitc->function->assume_frame) {
2414 	jit_int32_t	frame = -_jitc->function->frame;
2415 	assert(_jitc->function->self.aoff >= frame);
2416 	if (_jitc->function->assume_frame)
2417 	    return;
2418 	_jitc->function->self.aoff = frame;
2419     }
2420     if (_jitc->function->allocar)
2421 	_jitc->function->self.aoff &= -16;
2422     /* align at 16 bytes boundary */
2423     _jitc->function->stack = ((stack_framesize +
2424 			      _jitc->function->self.alen -
2425 			      _jitc->function->self.aoff) + 15) & -16;
2426     SAVEI(_SP_REGNO, -_jitc->function->stack, _SP_REGNO);
2427 
2428     /* (most) other backends do not save incoming arguments, so,
2429      * only save locals here */
2430     if (jit_regset_tstbit(&_jitc->function->regset, _L0))
2431 	stxi(OFF(0), _SP_REGNO, _L0_REGNO);
2432     if (jit_regset_tstbit(&_jitc->function->regset, _L1))
2433 	stxi(OFF(1), _SP_REGNO, _L1_REGNO);
2434     if (jit_regset_tstbit(&_jitc->function->regset, _L2))
2435 	stxi(OFF(2), _SP_REGNO, _L2_REGNO);
2436     if (jit_regset_tstbit(&_jitc->function->regset, _L3))
2437 	stxi(OFF(3), _SP_REGNO, _L3_REGNO);
2438     if (jit_regset_tstbit(&_jitc->function->regset, _L4))
2439 	stxi(OFF(4), _SP_REGNO, _L4_REGNO);
2440     if (jit_regset_tstbit(&_jitc->function->regset, _L5))
2441 	stxi(OFF(5), _SP_REGNO, _L5_REGNO);
2442     if (jit_regset_tstbit(&_jitc->function->regset, _L6))
2443 	stxi(OFF(6), _SP_REGNO, _L6_REGNO);
2444     if (jit_regset_tstbit(&_jitc->function->regset, _L7))
2445 	stxi(OFF(7), _SP_REGNO, _L7_REGNO);
2446 
2447     if (_jitc->function->allocar) {
2448 	reg = jit_get_reg(jit_class_gpr);
2449 	movi(rn(reg), BIAS(_jitc->function->self.aoff));
2450 	/* Already "biased" by allocai */
2451 	stxi_i(_jitc->function->aoffoff, _FP_REGNO, rn(reg));
2452 	jit_unget_reg(reg);
2453     }
2454 
2455     if (_jitc->function->self.call & jit_call_varargs) {
2456 	for (reg = _jitc->function->vagp; jit_arg_reg_p(reg); ++reg)
2457 	    stxi(BIAS((16 + (__WORDSIZE == 32)) * sizeof(jit_word_t) +
2458 		      reg * sizeof(jit_word_t)), _FP_REGNO, rn(_I0 + reg));
2459     }
2460 }
2461 
2462 static void
_epilog(jit_state_t * _jit,jit_node_t * node)2463 _epilog(jit_state_t *_jit, jit_node_t *node)
2464 {
2465     if (_jitc->function->assume_frame)
2466 	return;
2467     /* (most) other backends do not save incoming arguments, so,
2468      * only save locals here */
2469     if (jit_regset_tstbit(&_jitc->function->regset, _L0))
2470 	ldxi(_L0_REGNO, _FP_REGNO, _jitc->function->stack + OFF(0));
2471     if (jit_regset_tstbit(&_jitc->function->regset, _L1))
2472 	ldxi(_L1_REGNO, _FP_REGNO, _jitc->function->stack + OFF(1));
2473     if (jit_regset_tstbit(&_jitc->function->regset, _L2))
2474 	ldxi(_L2_REGNO, _FP_REGNO, _jitc->function->stack + OFF(2));
2475     if (jit_regset_tstbit(&_jitc->function->regset, _L3))
2476 	ldxi(_L3_REGNO, _FP_REGNO, _jitc->function->stack + OFF(3));
2477     if (jit_regset_tstbit(&_jitc->function->regset, _L4))
2478 	ldxi(_L4_REGNO, _FP_REGNO, _jitc->function->stack + OFF(4));
2479     if (jit_regset_tstbit(&_jitc->function->regset, _L5))
2480 	ldxi(_L5_REGNO, _FP_REGNO, _jitc->function->stack + OFF(5));
2481     if (jit_regset_tstbit(&_jitc->function->regset, _L6))
2482 	ldxi(_L6_REGNO, _FP_REGNO, _jitc->function->stack + OFF(6));
2483     if (jit_regset_tstbit(&_jitc->function->regset, _L7))
2484 	ldxi(_L7_REGNO, _FP_REGNO, _jitc->function->stack + OFF(7));
2485     RESTOREI(0, 0, 0);
2486     RETL();
2487     NOP();
2488 }
2489 
2490 static void
_vastart(jit_state_t * _jit,jit_int32_t r0)2491 _vastart(jit_state_t *_jit, jit_int32_t r0)
2492 {
2493     /* Initialize stack pointer to the first stack argument. */
2494     if (jit_arg_reg_p(_jitc->function->vagp))
2495 	addi(r0, _FP_REGNO, BIAS((16 + (__WORDSIZE == 32) +
2496 				  _jitc->function->vagp) *
2497 				 sizeof(jit_word_t)));
2498     else
2499 	addi(r0, _FP_REGNO, BIAS(_jitc->function->self.size));
2500 }
2501 
2502 static void
_vaarg(jit_state_t * _jit,jit_int32_t r0,jit_int32_t r1)2503 _vaarg(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1)
2504 {
2505     assert(_jitc->function->self.call & jit_call_varargs);
2506 
2507     /* Load argument. */
2508     ldr(r0, r1);
2509 
2510     /* Update vararg stack pointer. */
2511     addi(r1, r1, sizeof(jit_word_t));
2512 }
2513 
2514 static void
_patch_at(jit_state_t * _jit,jit_word_t instr,jit_word_t label)2515 _patch_at(jit_state_t *_jit, jit_word_t instr, jit_word_t label)
2516 {
2517     jit_instr_t		 i;
2518     union {
2519 	jit_int32_t	*i;
2520 	jit_word_t	 w;
2521     } u;
2522 
2523     u.w = instr;
2524     i.v = u.i[0];
2525 
2526     if (i.op.b == 0) {				/* conditional branch */
2527 	if (i.op2.b == 2 || i.op2.b == 6) {	/* int or float condition */
2528 	    i.disp22.b = (label - instr) >> 2;
2529 	    u.i[0] = i.v;
2530 	}
2531 #  if __WORDSIZE == 64
2532 	else if (i.op2.b == 1) {
2533 	    i.disp19.b = (label - instr) >> 2;
2534 	    u.i[0] = i.v;
2535 	}
2536 #  endif
2537 	else if (i.op2.b == 4) {	/* movi_p */
2538 	    /* SETHI */
2539 	    i.imm22.b = HI((int)label);
2540 	    u.i[0] = i.v;
2541 	    i.v = u.i[1];
2542 	    if (i.op.b == 2 && i.op3.b == 2) {
2543 		/* ORI */
2544 		i.simm13.b = LO(label);
2545 		u.i[1] = i.v;
2546 #  if __WORDSIZE == 64
2547 		i.v = u.i[2];
2548 		assert(i.op2.b == 4);
2549 		label = (label >> 32) & 0xffffffff;
2550 		i.imm22.b = HI((int)label);
2551 		u.i[2] = i.v;
2552 		i.v = u.i[3];
2553 		assert(i.op.b == 2 && i.op3.b == 2);
2554 		/* ORI */
2555 		i.simm13.b = LO(label);
2556 		u.i[3] = i.v;
2557 #  endif
2558 	    }
2559 	    else
2560 		abort();
2561 	}
2562 	else
2563 	    abort();
2564     }
2565     else
2566 	abort();
2567 }
2568 #endif
2569