git subrepo pull (merge) --force deps/lightning
[pcsx_rearmed.git] / deps / lightning / lib / jit_alpha.c
1 /*
2  * Copyright (C) 2014-2023  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 #define jit_arg_reg_p(i)                ((i) >= 0 && (i) < 6)
21 #define jit_arg_f_reg_p(i)              ((i) >= 0 && (i) < 6)
22 #if __BYTE_ORDER == __LITTLE_ENDIAN
23 #  define C_DISP                        0
24 #  define S_DISP                        0
25 #  define I_DISP                        0
26 #  define F_DISP                        0
27 #else
28 #  define C_DISP                        8 - sizeof(jit_int8_t)
29 #  define S_DISP                        8 - sizeof(jit_int16_t)
30 #  define I_DISP                        8 - sizeof(jit_int32_t)
31 #  define F_DISP                        8 - sizeof(jit_float32_t)
32 #endif
33
34 /*
35  * Types
36  */
37 /*
38  * What I could understand from gcc/config/alpha/alpha.c:alpha_build_builtin_va_list()
39  * and other helpers, as well as objdump of simple test programs; could not
40  * get gdb working on the test system I had access...
41  *
42  * base-48 to base is where up to 6 float registers are saved.
43  * base to base+48 is where up to 6 integer registers are saved.
44  * base+48... is where varargs arguments are stored.
45  *
46  *      if (offset < 48) {
47  *              if (type == double)
48  *                      offset -= 48;
49  *      }
50  *      load(reg, base, offset);
51  *      offset += 8;
52  */
53 typedef struct jit_va_list {
54     jit_pointer_t       base;
55     jit_word_t          offset;
56 } jit_va_list_t;
57
58 /*
59  * Prototypes
60  */
61 #define patch(instr, node)              _patch(_jit, instr, node)
62 static void _patch(jit_state_t*,jit_word_t,jit_node_t*);
63
64 #define PROTO                           1
65 #  include "jit_alpha-cpu.c"
66 #  include "jit_alpha-fpu.c"
67 #  include "jit_fallback.c"
68 #undef PROTO
69
70 /*
71  * Initialization
72  */
73 jit_register_t          _rvs[] = {
74     { rc(gpr) | 0x1c,                   "at" },
75     { rc(gpr) | 0x00,                   "v0" },
76     { rc(gpr) | 0x01,                   "t0" },
77     { rc(gpr) | 0x02,                   "t1" },
78     { rc(gpr) | 0x03,                   "t2" },
79     { rc(gpr) | 0x04,                   "t3" },
80     { rc(gpr) | 0x05,                   "t4" },
81     { rc(gpr) | 0x06,                   "t5" },
82     { rc(gpr) | 0x07,                   "t6" },
83     { rc(gpr) | 0x08,                   "t7" },
84     { rc(gpr) | 0x16,                   "t8" },
85     { rc(gpr) | 0x17,                   "t9" },
86     { rc(gpr) | 0x18,                   "t10" },
87     { rc(gpr) | 0x19,                   "t11" },
88     { rc(sav) | rc(gpr) | 0x09,         "s0" },
89     { rc(sav) | rc(gpr) | 0x0a,         "s1" },
90     { rc(sav) | rc(gpr) | 0x0b,         "s2" },
91     { rc(sav) | rc(gpr) | 0x0c,         "s3" },
92     { rc(sav) | rc(gpr) | 0x0d,         "s4" },
93     { rc(sav) | rc(gpr) | 0x0e,         "s5" },
94     { 0x0f,                             "fp" },
95     { rc(arg) | rc(gpr) | 0x15,         "a5" },
96     { rc(arg) | rc(gpr) | 0x14,         "a4" },
97     { rc(arg) | rc(gpr) | 0x13,         "a3" },
98     { rc(arg) | rc(gpr) | 0x12,         "a2" },
99     { rc(arg) | rc(gpr) | 0x11,         "a1" },
100     { rc(arg) | rc(gpr) | 0x10,         "a0" },
101     { 0x1a,                             "ra" },
102     { 0x1b,                             "pv" },
103     { 0x1d,                             "gp" },
104     { 0x1e,                             "sp" },
105     { 0x1f,                             "zero" },
106     { rc(fpr) | 0x00,                   "$f0" },
107     { rc(fpr) | 0x01,                   "$f1" },
108     { rc(sav) | rc(fpr) | 0x02,         "$f2" },
109     { rc(sav) | rc(fpr) | 0x03,         "$f3" },
110     { rc(sav) | rc(fpr) | 0x04,         "$f4" },
111     { rc(sav) | rc(fpr) | 0x05,         "$f5" },
112     { rc(sav) | rc(fpr) | 0x06,         "$f6" },
113     { rc(sav) | rc(fpr) | 0x07,         "$f7" },
114     { rc(sav) | rc(fpr) | 0x08,         "$f8" },
115     { rc(sav) | rc(fpr) | 0x09,         "$f9" },
116     { rc(fpr) | 0x0a,                   "$f10" },
117     { rc(fpr) | 0x0b,                   "$f11" },
118     { rc(fpr) | 0x0c,                   "$f12" },
119     { rc(fpr) | 0x0d,                   "$f13" },
120     { rc(fpr) | 0x0e,                   "$f14" },
121     { rc(fpr) | 0x0f,                   "$f15" },
122     { rc(arg) | rc(fpr) | 0x15,         "$f21" },
123     { rc(arg) | rc(fpr) | 0x14,         "$f20" },
124     { rc(arg) | rc(fpr) | 0x13,         "$f19" },
125     { rc(arg) | rc(fpr) | 0x12,         "$f18" },
126     { rc(arg) | rc(fpr) | 0x11,         "$f17" },
127     { rc(arg) | rc(fpr) | 0x10,         "$f16" },
128     { rc(fpr) | 0x16,                   "$f22" },
129     { rc(fpr) | 0x17,                   "$f23" },
130     { rc(fpr) | 0x18,                   "$f24" },
131     { rc(fpr) | 0x19,                   "$f25" },
132     { rc(fpr) | 0x1a,                   "$f26" },
133     { rc(fpr) | 0x1b,                   "$f27" },
134     { rc(fpr) | 0x1c,                   "$f28" },
135     { rc(fpr) | 0x1d,                   "$f29" },
136     { rc(fpr) | 0x1e,                   "$f30" },
137     { 0x1f,                             "$f31" },
138     { _NOREG,                           "<none>" },
139 };
140
141 /*
142  * Implementation
143  */
144 void
145 jit_get_cpu(void)
146 {
147 }
148
149 void
150 _jit_init(jit_state_t *_jit)
151 {
152     _jitc->reglen = jit_size(_rvs) - 1;
153     jit_carry = _NOREG;
154 }
155
156 void
157 _jit_prolog(jit_state_t *_jit)
158 {
159     jit_int32_t          offset;
160
161     if (_jitc->function)
162         jit_epilog();
163     assert(jit_regset_cmp_ui(&_jitc->regarg, 0) == 0);
164     jit_regset_set_ui(&_jitc->regsav, 0);
165     offset = _jitc->functions.offset;
166     if (offset >= _jitc->functions.length) {
167         jit_realloc((jit_pointer_t *)&_jitc->functions.ptr,
168                     _jitc->functions.length * sizeof(jit_function_t),
169                     (_jitc->functions.length + 16) * sizeof(jit_function_t));
170         _jitc->functions.length += 16;
171     }
172     _jitc->function = _jitc->functions.ptr + _jitc->functions.offset++;
173     _jitc->function->self.size = stack_framesize;
174     _jitc->function->self.argi = _jitc->function->self.alen = 0;
175     /* float conversion */
176     _jitc->function->self.aoff = _jitc->function->cvt_offset = -8;
177     _jitc->function->self.call = jit_call_default;
178     jit_alloc((jit_pointer_t *)&_jitc->function->regoff,
179               _jitc->reglen * sizeof(jit_int32_t));
180
181     /* _no_link here does not mean the jit_link() call can be removed
182      * by rewriting as:
183      * _jitc->function->prolog = jit_new_node(jit_code_prolog);
184      */
185     _jitc->function->prolog = jit_new_node_no_link(jit_code_prolog);
186     jit_link(_jitc->function->prolog);
187     _jitc->function->prolog->w.w = offset;
188     _jitc->function->epilog = jit_new_node_no_link(jit_code_epilog);
189     /*  u:      label value
190      *  v:      offset in blocks vector
191      *  w:      offset in functions vector
192      */
193     _jitc->function->epilog->w.w = offset;
194
195     jit_regset_new(&_jitc->function->regset);
196 }
197
198 jit_int32_t
199 _jit_allocai(jit_state_t *_jit, jit_int32_t length)
200 {
201     assert(_jitc->function != NULL);
202     switch (length) {
203         case 0: case 1:                                         break;
204         case 2:         _jitc->function->self.aoff &= -2;       break;
205         case 3: case 4: _jitc->function->self.aoff &= -4;       break;
206         default:        _jitc->function->self.aoff &= -8;       break;
207     }
208     _jitc->function->self.aoff -= length;
209     if (!_jitc->realize) {
210         jit_inc_synth_ww(allocai, _jitc->function->self.aoff, length);
211         jit_dec_synth();
212     }
213     return (_jitc->function->self.aoff);
214 }
215
216 void
217 _jit_allocar(jit_state_t *_jit, jit_int32_t u, jit_int32_t v)
218 {
219     jit_int32_t          reg;
220     assert(_jitc->function != NULL);
221     jit_inc_synth_ww(allocar, u, v);
222     if (!_jitc->function->allocar) {
223         _jitc->function->aoffoff = jit_allocai(sizeof(jit_int32_t));
224         _jitc->function->allocar = 1;
225     }
226     reg = jit_get_reg(jit_class_gpr);
227     jit_negr(reg, v);
228     jit_andi(reg, reg, -8);
229     jit_ldxi_i(u, JIT_FP, _jitc->function->aoffoff);
230     jit_addr(u, u, reg);
231     jit_addr(JIT_SP, JIT_SP, reg);
232     jit_stxi_i(_jitc->function->aoffoff, JIT_FP, u);
233     jit_unget_reg(reg);
234     jit_dec_synth();
235 }
236
237 void
238 _jit_ret(jit_state_t *_jit)
239 {
240     jit_node_t          *instr;
241     assert(_jitc->function != NULL);
242     jit_inc_synth(ret);
243     /* jump to epilog */
244     instr = jit_jmpi();
245     jit_patch_at(instr, _jitc->function->epilog);
246     jit_dec_synth();
247 }
248
249 void
250 _jit_retr(jit_state_t *_jit, jit_int32_t u, jit_code_t code)
251 {
252     jit_code_inc_synth_w(code, u);
253     jit_movr(JIT_RET, u);
254     jit_ret();
255     jit_dec_synth();
256 }
257
258 void
259 _jit_reti(jit_state_t *_jit, jit_word_t u, jit_code_t code)
260 {
261     jit_code_inc_synth_w(code, u);
262     jit_movi(JIT_RET, u);
263     jit_ret();
264     jit_dec_synth();
265 }
266
267 void
268 _jit_retr_f(jit_state_t *_jit, jit_int32_t u)
269 {
270     jit_inc_synth_w(retr_f, u);
271     if (u != JIT_FRET)
272         jit_movr_f(JIT_FRET, u);
273     else
274         jit_live(JIT_FRET);
275     jit_ret();
276     jit_dec_synth();
277 }
278
279 void
280 _jit_reti_f(jit_state_t *_jit, jit_float32_t u)
281 {
282     jit_inc_synth_f(reti_f, u);
283     jit_movi_f(JIT_FRET, u);
284     jit_ret();
285     jit_dec_synth();
286 }
287
288 void
289 _jit_retr_d(jit_state_t *_jit, jit_int32_t u)
290 {
291     jit_inc_synth_w(retr_d, u);
292     if (u != JIT_FRET)
293         jit_movr_d(JIT_FRET, u);
294     else
295         jit_live(JIT_FRET);
296     jit_ret();
297     jit_dec_synth();
298 }
299
300 void
301 _jit_reti_d(jit_state_t *_jit, jit_float64_t u)
302 {
303     jit_inc_synth_d(reti_d, u);
304     jit_movi_d(JIT_FRET, u);
305     jit_ret();
306     jit_dec_synth();
307 }
308
309 void
310 _jit_epilog(jit_state_t *_jit)
311 {
312     assert(_jitc->function != NULL);
313     assert(_jitc->function->epilog->next == NULL);
314     jit_link(_jitc->function->epilog);
315     _jitc->function = NULL;
316 }
317
318 jit_bool_t
319 _jit_arg_register_p(jit_state_t *_jit, jit_node_t *u)
320 {
321     if (u->code >= jit_code_arg_c && u->code <= jit_code_arg)
322         return (jit_arg_reg_p(u->u.w));
323     assert(u->code == jit_code_arg_f || u->code == jit_code_arg_d);
324     return (jit_arg_f_reg_p(u->u.w));
325 }
326
327 void
328 _jit_ellipsis(jit_state_t *_jit)
329 {
330     jit_inc_synth(ellipsis);
331     if (_jitc->prepare) {
332         jit_link_prepare();
333         assert(!(_jitc->function->call.call & jit_call_varargs));
334         _jitc->function->call.call |= jit_call_varargs;
335     }
336     else {
337         jit_link_prolog();
338         assert(!(_jitc->function->self.call & jit_call_varargs));
339         _jitc->function->self.call |= jit_call_varargs;
340
341         /* Allocate va_list like object in the stack */
342         _jitc->function->vaoff = jit_allocai(sizeof(jit_va_list_t));
343         _jitc->function->vagp = _jitc->function->self.argi;
344     }
345     jit_dec_synth();
346 }
347
348 void
349 _jit_va_push(jit_state_t *_jit, jit_int32_t u)
350 {
351     jit_int32_t         reg;
352     jit_inc_synth_w(va_push, u);
353     reg = jit_get_reg(jit_class_gpr);
354     jit_ldxi(reg, u, offsetof(jit_va_list_t, base));
355     jit_pushargr(reg);
356     jit_ldxi(reg, u, offsetof(jit_va_list_t, offset));
357     jit_pushargr(reg);
358     jit_unget_reg(reg);
359     jit_dec_synth();
360 }
361
362 jit_node_t *
363 _jit_arg(jit_state_t *_jit, jit_code_t code)
364 {
365     jit_node_t          *node;
366     jit_int32_t          offset;
367     assert(_jitc->function != NULL);
368     assert(!(_jitc->function->self.call & jit_call_varargs));
369 #if STRONG_TYPE_CHECKING
370     assert(code >= jit_code_arg_c && code <= jit_code_arg);
371 #endif
372     if (jit_arg_reg_p(_jitc->function->self.argi))
373         offset = _jitc->function->self.argi++;
374     else {
375         offset = _jitc->function->self.size;
376         _jitc->function->self.size += 8;
377     }
378     node = jit_new_node_ww(code, offset,
379                            ++_jitc->function->self.argn);
380     jit_link_prolog();
381     return (node);
382 }
383
384 jit_node_t *
385 _jit_arg_f(jit_state_t *_jit)
386 {
387     jit_node_t          *node;
388     jit_int32_t          offset;
389     assert(_jitc->function != NULL);
390     if (jit_arg_f_reg_p(_jitc->function->self.argi))
391         offset = _jitc->function->self.argi++;
392     else {
393         offset = _jitc->function->self.size;
394         _jitc->function->self.size += 8;
395     }
396     node = jit_new_node_ww(jit_code_arg_f, offset,
397                            ++_jitc->function->self.argn);
398     jit_link_prolog();
399     return (node);
400 }
401
402 jit_node_t *
403 _jit_arg_d(jit_state_t *_jit)
404 {
405     jit_node_t          *node;
406     jit_int32_t          offset;
407     assert(_jitc->function != NULL);
408     if (jit_arg_f_reg_p(_jitc->function->self.argi))
409         offset = _jitc->function->self.argi++;
410     else {
411         offset = _jitc->function->self.size;
412         _jitc->function->self.size += 8;
413     }
414     node = jit_new_node_ww(jit_code_arg_d, offset,
415                            ++_jitc->function->self.argn);
416     jit_link_prolog();
417     return (node);
418 }
419
420 void
421 _jit_getarg_c(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
422 {
423     assert_arg_type(v->code, jit_code_arg_c);
424     jit_inc_synth_wp(getarg_c, u, v);
425     if (jit_arg_reg_p(v->u.w))
426         jit_extr_c(u, _A0 - v->u.w);
427     else
428         jit_ldxi_c(u, _FP, v->u.w + C_DISP);
429     jit_dec_synth();
430 }
431
432 void
433 _jit_getarg_uc(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
434 {
435     assert_arg_type(v->code, jit_code_arg_c);
436     jit_inc_synth_wp(getarg_uc, u, v);
437     if (jit_arg_reg_p(v->u.w))
438         jit_extr_uc(u, _A0 - v->u.w);
439     else
440         jit_ldxi_uc(u, _FP, v->u.w + C_DISP);
441     jit_dec_synth();
442 }
443
444 void
445 _jit_getarg_s(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
446 {
447     assert_arg_type(v->code, jit_code_arg_s);
448     jit_inc_synth_wp(getarg_s, u, v);
449     if (jit_arg_reg_p(v->u.w))
450         jit_extr_s(u, _A0 - v->u.w);
451     else
452         jit_ldxi_s(u, _FP, v->u.w + S_DISP);
453     jit_dec_synth();
454 }
455
456 void
457 _jit_getarg_us(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
458 {
459     assert_arg_type(v->code, jit_code_arg_s);
460     jit_inc_synth_wp(getarg_us, u, v);
461     if (jit_arg_reg_p(v->u.w))
462         jit_extr_us(u, _A0 - v->u.w);
463     else
464         jit_ldxi_us(u, _FP, v->u.w + S_DISP);
465     jit_dec_synth();
466 }
467
468 void
469 _jit_getarg_i(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
470 {
471     assert_arg_type(v->code, jit_code_arg_i);
472     jit_inc_synth_wp(getarg_i, u, v);
473     if (jit_arg_reg_p(v->u.w))
474         jit_extr_i(u, _A0 - v->u.w);
475     else
476         jit_ldxi_i(u, _FP, v->u.w + I_DISP);
477     jit_dec_synth();
478 }
479
480 void
481 _jit_getarg_ui(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
482 {
483     assert_arg_type(v->code, jit_code_arg_i);
484     jit_inc_synth_wp(getarg_ui, u, v);
485     if (jit_arg_reg_p(v->u.w))
486         jit_extr_ui(u, _A0 - v->u.w);
487     else
488         jit_ldxi_ui(u, _FP, v->u.w + I_DISP);
489     jit_dec_synth();
490 }
491
492 void
493 _jit_getarg_l(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
494 {
495     assert_arg_type(v->code, jit_code_arg_l);
496     jit_inc_synth_wp(getarg_l, u, v);
497     if (jit_arg_reg_p(v->u.w))
498         jit_movr(u, _A0 - v->u.w);
499     else
500         jit_ldxi_l(u, _FP, v->u.w);
501     jit_dec_synth();
502 }
503
504 void
505 _jit_putargr(jit_state_t *_jit, jit_int32_t u, jit_node_t *v, jit_code_t code)
506 {
507     assert_putarg_type(code, v->code);
508     jit_code_inc_synth_wp(code, u, v);
509     if (jit_arg_reg_p(v->u.w))
510         jit_movr(_A0 - v->u.w, u);
511     else
512         jit_stxi(v->u.w, _FP, u);
513     jit_dec_synth();
514 }
515
516 void
517 _jit_putargi(jit_state_t *_jit, jit_word_t u, jit_node_t *v, jit_code_t code)
518 {
519     jit_int32_t         regno;
520     assert_putarg_type(code, v->code);
521     jit_code_inc_synth_wp(code, u, v);
522     if (jit_arg_reg_p(v->u.w))
523         jit_movi(_A0 - v->u.w, u);
524     else {
525         regno = jit_get_reg(jit_class_gpr);
526         jit_movi(regno, u);
527         jit_stxi(v->u.w, _FP, regno);
528         jit_unget_reg(regno);
529     }
530     jit_dec_synth();
531 }
532
533 void
534 _jit_getarg_f(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
535 {
536     assert(v->code == jit_code_arg_f);
537     jit_inc_synth_wp(getarg_f, u, v);
538     if (jit_arg_f_reg_p(v->u.w))
539         jit_movr_f(u, _F16 - v->u.w);
540     else
541         jit_ldxi_f(u, _FP, v->u.w + F_DISP);
542     jit_dec_synth();
543 }
544
545 void
546 _jit_putargr_f(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
547 {
548     assert(v->code == jit_code_arg_f);
549     jit_inc_synth_wp(putargr_f, u, v);
550     if (jit_arg_f_reg_p(v->u.w))
551         jit_movr_f(_F16 - v->u.w, u);
552     else
553         jit_stxi_f(v->u.w, _FP, u + F_DISP);
554     jit_dec_synth();
555 }
556
557 void
558 _jit_putargi_f(jit_state_t *_jit, jit_float32_t u, jit_node_t *v)
559 {
560     jit_int32_t         regno;
561     assert(v->code == jit_code_arg_f);
562     jit_inc_synth_fp(putargi_f, u, v);
563     if (jit_arg_f_reg_p(v->u.w))
564         jit_movi_f(_F16 - v->u.w, u);
565     else {
566         regno = jit_get_reg(jit_class_fpr);
567         jit_movi_f(regno, u);
568         jit_stxi_f(v->u.w, _FP, regno + F_DISP);
569         jit_unget_reg(regno);
570     }
571     jit_dec_synth();
572 }
573
574 void
575 _jit_getarg_d(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
576 {
577     assert(v->code == jit_code_arg_d);
578     jit_inc_synth_wp(getarg_d, u, v);
579     if (jit_arg_f_reg_p(v->u.w))
580         jit_movr_d(u, _F16 - v->u.w);
581     else
582         jit_ldxi_d(u, _FP, v->u.w);
583     jit_dec_synth();
584 }
585
586 void
587 _jit_putargr_d(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
588 {
589     assert(v->code == jit_code_arg_d);
590     jit_inc_synth_wp(putargr_d, u, v);
591     if (jit_arg_f_reg_p(v->u.w))
592         jit_movr_d(_F16 - v->u.w, u);
593     else
594         jit_stxi_d(v->u.w, _FP, u);
595     jit_dec_synth();
596 }
597
598 void
599 _jit_putargi_d(jit_state_t *_jit, jit_float64_t u, jit_node_t *v)
600 {
601     jit_int32_t         regno;
602     assert(v->code == jit_code_arg_d);
603     jit_inc_synth_dp(putargi_d, u, v);
604     if (jit_arg_f_reg_p(v->u.w))
605         jit_movi_d(_F16 - v->u.w, u);
606     else {
607         regno = jit_get_reg(jit_class_fpr);
608         jit_movi_d(regno, u);
609         jit_stxi_d(v->u.w, _FP, regno);
610         jit_unget_reg(regno);
611     }
612     jit_dec_synth();
613 }
614
615 void
616 _jit_pushargr(jit_state_t *_jit, jit_int32_t u, jit_code_t code)
617 {
618     assert(_jitc->function != NULL);
619     jit_code_inc_synth_w(code, u);
620     jit_link_prepare();
621     if (jit_arg_reg_p(_jitc->function->call.argi)) {
622         jit_movr(_A0 - _jitc->function->call.argi, u);
623         ++_jitc->function->call.argi;
624     }
625     else {
626         jit_stxi(_jitc->function->call.size, JIT_SP, u);
627         _jitc->function->call.size += 8;
628     }
629     jit_dec_synth();
630 }
631
632 void
633 _jit_pushargi(jit_state_t *_jit, jit_int64_t u, jit_code_t code)
634 {
635     jit_int32_t         regno;
636     assert(_jitc->function != NULL);
637     jit_code_inc_synth_w(code, u);
638     jit_link_prepare();
639     if (jit_arg_reg_p(_jitc->function->call.argi)) {
640         jit_movi(_A0 - _jitc->function->call.argi, u);
641         ++_jitc->function->call.argi;
642     }
643     else {
644         regno = jit_get_reg(jit_class_gpr);
645         jit_movi(regno, u);
646         jit_stxi(_jitc->function->call.size, JIT_SP, regno);
647         _jitc->function->call.size += 8;
648         jit_unget_reg(regno);
649     }
650     jit_dec_synth();
651 }
652
653 void
654 _jit_pushargr_f(jit_state_t *_jit, jit_int32_t u)
655 {
656     assert(_jitc->function != NULL);
657     jit_inc_synth_w(pushargr_f, u);
658     jit_link_prepare();
659     if (jit_arg_f_reg_p(_jitc->function->call.argi)) {
660         jit_movr_f(_F16 - _jitc->function->call.argi, u);
661         ++_jitc->function->call.argi;
662     }
663     else {
664         jit_stxi_f(_jitc->function->call.size + F_DISP, JIT_SP, u);
665         _jitc->function->call.size += 8;
666     }
667     jit_dec_synth();
668 }
669
670 void
671 _jit_pushargi_f(jit_state_t *_jit, jit_float32_t u)
672 {
673     jit_int32_t         regno;
674     assert(_jitc->function != NULL);
675     jit_inc_synth_f(pushargi_f, u);
676     jit_link_prepare();
677     if (jit_arg_f_reg_p(_jitc->function->call.argi)) {
678         jit_movi_f(_F16 - _jitc->function->call.argi, u);
679         ++_jitc->function->call.argi;
680     }
681     else {
682         regno = jit_get_reg(jit_class_fpr);
683         jit_movi_f(regno, u);
684         jit_stxi_f(_jitc->function->call.size + F_DISP, JIT_SP, regno);
685         _jitc->function->call.size += 8;
686         jit_unget_reg(regno);
687     }
688     jit_dec_synth();
689 }
690
691 void
692 _jit_pushargr_d(jit_state_t *_jit, jit_int32_t u)
693 {
694     assert(_jitc->function != NULL);
695     jit_inc_synth_w(pushargr_d, u);
696     jit_link_prepare();
697     if (jit_arg_f_reg_p(_jitc->function->call.argi)) {
698         jit_movr_d(_F16 - _jitc->function->call.argi, u);
699         ++_jitc->function->call.argi;
700     }
701     else {
702         jit_stxi_d(_jitc->function->call.size, JIT_SP, u);
703         _jitc->function->call.size += 8;
704     }
705     jit_dec_synth();
706 }
707
708 void
709 _jit_pushargi_d(jit_state_t *_jit, jit_float64_t u)
710 {
711     jit_int32_t         regno;
712     assert(_jitc->function != NULL);
713     jit_inc_synth_d(pushargi_d, u);
714     jit_link_prepare();
715     if (jit_arg_f_reg_p(_jitc->function->call.argi)) {
716         jit_movi_d(_F16 - _jitc->function->call.argi, u);
717         ++_jitc->function->call.argi;
718     }
719     else {
720         regno = jit_get_reg(jit_class_fpr);
721         jit_movi_d(regno, u);
722         jit_stxi_d(_jitc->function->call.size, JIT_SP, regno);
723         _jitc->function->call.size += 8;
724         jit_unget_reg(regno);
725     }
726     jit_dec_synth();
727 }
728
729 jit_bool_t
730 _jit_regarg_p(jit_state_t *_jit, jit_node_t *node, jit_int32_t regno)
731 {
732     jit_int32_t         spec;
733
734     spec = jit_class(_rvs[regno].spec);
735     if (spec & jit_class_arg) {
736         if (spec & jit_class_gpr) {
737             regno = _A0 - regno;
738             if (regno >= 0 && regno < node->v.w)
739                 return (1);
740         }
741         else if (spec & jit_class_fpr) {
742             regno = _F16 - regno;
743             if (regno >= 0 && regno < node->w.w)
744                 return (1);
745         }
746     }
747
748     return (0);
749 }
750
751 void
752 _jit_finishr(jit_state_t *_jit, jit_int32_t r0)
753 {
754     jit_node_t          *call;
755     assert(_jitc->function != NULL);
756     jit_inc_synth_w(finishr, r0);
757     if (_jitc->function->self.alen < _jitc->function->call.size)
758         _jitc->function->self.alen = _jitc->function->call.size;
759     call = jit_callr(r0);
760     call->v.w = call->w.w = _jitc->function->self.argi;
761     _jitc->function->call.argi = _jitc->function->call.size = 0;
762     _jitc->prepare = 0;
763     jit_dec_synth();
764 }
765
766 jit_node_t *
767 _jit_finishi(jit_state_t *_jit, jit_pointer_t i0)
768 {
769     jit_node_t          *call;
770     assert(_jitc->function != NULL);
771     jit_inc_synth_w(finishi, (jit_word_t)i0);
772     if (_jitc->function->self.alen < _jitc->function->call.size)
773         _jitc->function->self.alen = _jitc->function->call.size;
774     call = jit_calli(i0);
775     call->v.w = call->w.w = _jitc->function->self.argf;
776     _jitc->function->call.argi = _jitc->function->call.size = 0;
777     _jitc->prepare = 0;
778     jit_dec_synth();
779     return (call);
780 }
781
782 void
783 _jit_retval_c(jit_state_t *_jit, jit_int32_t r0)
784 {
785     jit_inc_synth_w(retval_c, r0);
786     jit_extr_c(r0, JIT_RET);
787     jit_dec_synth();
788 }
789
790 void
791 _jit_retval_uc(jit_state_t *_jit, jit_int32_t r0)
792 {
793     jit_inc_synth_w(retval_uc, r0);
794     jit_extr_uc(r0, JIT_RET);
795     jit_dec_synth();
796 }
797
798 void
799 _jit_retval_s(jit_state_t *_jit, jit_int32_t r0)
800 {
801     jit_inc_synth_w(retval_s, r0);
802     jit_extr_s(r0, JIT_RET);
803     jit_dec_synth();
804 }
805
806 void
807 _jit_retval_us(jit_state_t *_jit, jit_int32_t r0)
808 {
809     jit_inc_synth_w(retval_us, r0);
810     jit_extr_us(r0, JIT_RET);
811     jit_dec_synth();
812 }
813
814 void
815 _jit_retval_i(jit_state_t *_jit, jit_int32_t r0)
816 {
817     jit_inc_synth_w(retval_i, r0);
818     jit_extr_i(r0, JIT_RET);
819     jit_dec_synth();
820 }
821
822 void
823 _jit_retval_ui(jit_state_t *_jit, jit_int32_t r0)
824 {
825     jit_inc_synth_w(retval_ui, r0);
826     jit_extr_ui(r0, JIT_RET);
827     jit_dec_synth();
828 }
829
830 void
831 _jit_retval_l(jit_state_t *_jit, jit_int32_t r0)
832 {
833     jit_inc_synth_w(retval_l, r0);
834     if (r0 != JIT_RET)
835         jit_movr(r0, JIT_RET);
836     jit_dec_synth();
837 }
838
839 void
840 _jit_retval_f(jit_state_t *_jit, jit_int32_t r0)
841 {
842     jit_inc_synth_w(retval_f, r0);
843     if (r0 != JIT_FRET)
844         jit_movr_f(r0, JIT_FRET);
845     jit_dec_synth();
846 }
847
848 void
849 _jit_retval_d(jit_state_t *_jit, jit_int32_t r0)
850 {
851     jit_inc_synth_w(retval_d, r0);
852     if (r0 != JIT_FRET)
853         jit_movr_d(r0, JIT_FRET);
854     jit_dec_synth();
855 }
856
857 jit_pointer_t
858 _emit_code(jit_state_t *_jit)
859 {
860     jit_node_t          *node;
861     jit_node_t          *temp;
862     jit_word_t           word;
863     jit_word_t           value;
864     jit_int32_t          offset;
865     struct {
866         jit_node_t      *node;
867         jit_uint8_t     *data;
868         jit_word_t       word;
869         jit_function_t   func;
870 #if DEVEL_DISASSEMBLER
871         jit_word_t       prevw;
872 #endif
873         jit_int32_t      const_offset;
874         jit_int32_t      patch_offset;
875     } undo;
876 #if DEVEL_DISASSEMBLER
877     jit_word_t           prevw;
878 #endif
879
880     _jitc->function = NULL;
881
882     jit_reglive_setup();
883
884     undo.word = 0;
885     undo.node = NULL;
886     undo.const_offset = undo.patch_offset = 0;
887 #define case_rr(name, type)                                             \
888             case jit_code_##name##r##type:                              \
889                 name##r##type(rn(node->u.w), rn(node->v.w));            \
890                 break
891 #define case_rw(name, type)                                             \
892             case jit_code_##name##i##type:                              \
893                 name##i##type(rn(node->u.w), node->v.w);                \
894                 break
895 #define case_wr(name, type)                                             \
896             case jit_code_##name##i##type:                              \
897                 name##i##type(node->u.w, rn(node->v.w));                \
898                 break
899 #define case_rrr(name, type)                                            \
900             case jit_code_##name##r##type:                              \
901                 name##r##type(rn(node->u.w),                            \
902                               rn(node->v.w), rn(node->w.w));            \
903                 break
904 #define case_rrw(name, type)                                            \
905             case jit_code_##name##i##type:                              \
906                 name##i##type(rn(node->u.w), rn(node->v.w), node->w.w); \
907                 break
908 #define case_rrf(name, type, size)                                      \
909             case jit_code_##name##i##type:                              \
910                 assert(node->flag & jit_flag_data);                     \
911                 name##i##type(rn(node->u.w), rn(node->v.w),             \
912                               (jit_float##size##_t *)node->w.n->u.w);   \
913                 break
914 #define case_rqr(name, type)                                            \
915             case jit_code_##name##r##type:                              \
916                 name##r##type(rn(node->u.w), rn(node->v.q.l),           \
917                               rn(node->v.q.h), rn(node->w.w));          \
918             case jit_code_##name##i##type:                              \
919                 break;
920 #define case_rrrr(name, type)                                           \
921             case jit_code_##name##r##type:                              \
922                 name##r##type(rn(node->u.q.l), rn(node->u.q.h),         \
923                               rn(node->v.w), rn(node->w.w));            \
924                 break
925 #define case_rrrw(name, type)                                           \
926             case jit_code_##name##i##type:                              \
927                 name##i##type(rn(node->u.q.l), rn(node->u.q.h),         \
928                               rn(node->v.w), node->w.w);                \
929                 break
930 #define case_wrr(name, type)                                            \
931             case jit_code_##name##i##type:                              \
932                 name##i##type(node->u.w, rn(node->v.w), rn(node->w.w)); \
933                 break
934 #define case_brr(name, type)                                            \
935             case jit_code_##name##r##type:                              \
936                 temp = node->u.n;                                       \
937                 assert(temp->code == jit_code_label ||                  \
938                        temp->code == jit_code_epilog);                  \
939                 if (temp->flag & jit_flag_patch)                        \
940                     name##r##type(temp->u.w, rn(node->v.w),             \
941                                   rn(node->w.w));                       \
942                 else {                                                  \
943                     word = name##r##type(_jit->pc.w,                    \
944                                          rn(node->v.w), rn(node->w.w)); \
945                     patch(word, node);                                  \
946                 }                                                       \
947                 break
948 #define case_brw(name, type)                                            \
949             case jit_code_##name##i##type:                              \
950                 temp = node->u.n;                                       \
951                 assert(temp->code == jit_code_label ||                  \
952                        temp->code == jit_code_epilog);                  \
953                 if (temp->flag & jit_flag_patch)                        \
954                     name##i##type(temp->u.w,                            \
955                                   rn(node->v.w), node->w.w);            \
956                 else {                                                  \
957                     word = name##i##type(_jit->pc.w,                    \
958                                          rn(node->v.w), node->w.w);     \
959                     patch(word, node);                                  \
960                 }                                                       \
961                 break
962 #define case_brf(name, type, size)                                      \
963             case jit_code_##name##i##type:                              \
964                 temp = node->u.n;                                       \
965                 assert(temp->code == jit_code_label ||                  \
966                        temp->code == jit_code_epilog);                  \
967                 if (temp->flag & jit_flag_patch)                        \
968                     name##i##type(temp->u.w, rn(node->v.w),             \
969                                 (jit_float##size##_t *)node->w.n->u.w); \
970                 else {                                                  \
971                     word = name##i##type(_jit->pc.w, rn(node->v.w),     \
972                                 (jit_float##size##_t *)node->w.n->u.w); \
973                     patch(word, node);                                  \
974                 }                                                       \
975                 break
976 #if DEVEL_DISASSEMBLER
977     prevw = _jit->pc.w;
978 #endif
979     for (node = _jitc->head; node; node = node->next) {
980         if (_jit->pc.uc >= _jitc->code.end)
981             return (NULL);
982
983 #if DEVEL_DISASSEMBLER
984         node->offset = (jit_uword_t)_jit->pc.w - (jit_uword_t)prevw;
985         prevw = _jit->pc.w;
986 #endif
987         value = jit_classify(node->code);
988         jit_regarg_set(node, value);
989         switch (node->code) {
990             case jit_code_align:
991                 /* Must align to a power of two */
992                 assert(!(node->u.w & (node->u.w - 1)));
993                 if ((word = _jit->pc.w & (node->u.w - 1)))
994                     nop(node->u.w - word);
995                 break;
996             case jit_code_skip:
997                 nop((node->u.w + 3) & ~3);
998                 break;
999             case jit_code_note:         case jit_code_name:
1000                 node->u.w = _jit->pc.w;
1001                 break;
1002             case jit_code_label:
1003                 /* remember label is defined */
1004                 node->flag |= jit_flag_patch;
1005                 node->u.w = _jit->pc.w;
1006                 break;
1007                 case_rrr(add,);
1008                 case_rrw(add,);
1009                 case_rrr(addc,);
1010                 case_rrw(addc,);
1011                 case_rrr(addx,);
1012                 case_rrw(addx,);
1013                 case_rrr(sub,);
1014                 case_rrw(sub,);
1015                 case_rrr(subc,);
1016                 case_rrw(subc,);
1017                 case_rrr(subx,);
1018                 case_rrw(subx,);
1019                 case_rrw(rsb,);
1020                 case_rrr(mul,);
1021                 case_rrw(mul,);
1022                 case_rrr(hmul,);
1023                 case_rrw(hmul,);
1024                 case_rrr(hmul, _u);
1025                 case_rrw(hmul, _u);
1026                 case_rrrr(qmul,);
1027                 case_rrrw(qmul,);
1028                 case_rrrr(qmul, _u);
1029                 case_rrrw(qmul, _u);
1030                 case_rrr(div,);
1031                 case_rrw(div,);
1032                 case_rrr(div, _u);
1033                 case_rrw(div, _u);
1034                 case_rrrr(qdiv,);
1035                 case_rrrw(qdiv,);
1036                 case_rrrr(qdiv, _u);
1037                 case_rrrw(qdiv, _u);
1038                 case_rrr(rem,);
1039                 case_rrw(rem,);
1040                 case_rrr(rem, _u);
1041                 case_rrw(rem, _u);
1042                 case_rrr(lsh,);
1043                 case_rrw(lsh,);
1044                 case_rrrr(qlsh,);
1045                 case_rrrw(qlsh,);
1046                 case_rrrr(qlsh, _u);
1047                 case_rrrw(qlsh, _u);
1048                 case_rrr(rsh,);
1049                 case_rrw(rsh,);
1050                 case_rrr(rsh, _u);
1051                 case_rrw(rsh, _u);
1052                 case_rrrr(qrsh,);
1053                 case_rrrw(qrsh,);
1054                 case_rrrr(qrsh, _u);
1055                 case_rrrw(qrsh, _u);
1056 #define lrotr(r0,r1,r2)         fallback_lrotr(r0,r1,r2)
1057 #define lroti(r0,r1,i0)         fallback_lroti(r0,r1,i0)
1058 #define rrotr(r0,r1,r2)         fallback_rrotr(r0,r1,r2)
1059 #define rroti(r0,r1,i0)         fallback_rroti(r0,r1,i0)
1060                 case_rrr(lrot,);
1061                 case_rrw(lrot,);
1062                 case_rrr(rrot,);
1063                 case_rrw(rrot,);
1064                 case_rrr(and,);
1065                 case_rrw(and,);
1066                 case_rrr(or,);
1067                 case_rrw(or,);
1068                 case_rrr(xor,);
1069                 case_rrw(xor,);
1070                 case_rr(trunc, _f_i);
1071                 case_rr(trunc, _d_i);
1072                 case_rr(trunc, _f_l);
1073                 case_rr(trunc, _d_l);
1074                 case_rr(ld, _c);
1075                 case_rw(ld, _c);
1076                 case_rr(ld, _uc);
1077                 case_rw(ld, _uc);
1078                 case_rr(ld, _s);
1079                 case_rw(ld, _s);
1080                 case_rr(ld, _us);
1081                 case_rw(ld, _us);
1082                 case_rr(ld, _i);
1083                 case_rw(ld, _i);
1084                 case_rr(ld, _ui);
1085                 case_rw(ld, _ui);
1086                 case_rr(ld, _l);
1087                 case_rw(ld, _l);
1088                 case_rrr(ldx, _c);
1089                 case_rrw(ldx, _c);
1090                 case_rrr(ldx, _uc);
1091                 case_rrw(ldx, _uc);
1092                 case_rrr(ldx, _s);
1093                 case_rrw(ldx, _s);
1094                 case_rrr(ldx, _us);
1095                 case_rrw(ldx, _us);
1096                 case_rrr(ldx, _i);
1097                 case_rrw(ldx, _i);
1098                 case_rrr(ldx, _ui);
1099                 case_rrw(ldx, _ui);
1100                 case_rrr(ldx, _l);
1101                 case_rrw(ldx, _l);
1102             case jit_code_unldr:
1103                 unldr(rn(node->u.w), rn(node->v.w), node->w.w);
1104                 break;
1105             case jit_code_unldi:
1106                 unldi(rn(node->u.w), node->v.w, node->w.w);
1107                 break;
1108             case jit_code_unldr_u:
1109                 unldr_u(rn(node->u.w), rn(node->v.w), node->w.w);
1110                 break;
1111             case jit_code_unldi_u:
1112                 unldi_u(rn(node->u.w), node->v.w, node->w.w);
1113                 break;
1114                 case_rr(st, _c);
1115                 case_wr(st, _c);
1116                 case_rr(st, _s);
1117                 case_wr(st, _s);
1118                 case_rr(st, _i);
1119                 case_wr(st, _i);
1120                 case_rr(st, _l);
1121                 case_wr(st, _l);
1122                 case_rrr(stx, _c);
1123                 case_wrr(stx, _c);
1124                 case_rrr(stx, _s);
1125                 case_wrr(stx, _s);
1126                 case_rrr(stx, _i);
1127                 case_wrr(stx, _i);
1128                 case_rrr(stx, _l);
1129                 case_wrr(stx, _l);
1130             case jit_code_unstr:
1131                 unstr(rn(node->u.w), rn(node->v.w), node->w.w);
1132                 break;
1133             case jit_code_unsti:
1134                 unsti(node->u.w, rn(node->v.w), node->w.w);
1135                 break;
1136                 case_rr(hton, _us);
1137                 case_rr(hton, _ui);
1138                 case_rr(hton, _ul);
1139                 case_rr(bswap, _us);
1140                 case_rr(bswap, _ui);
1141                 case_rr(bswap, _ul);
1142             case jit_code_extr:
1143                 extr(rn(node->u.w), rn(node->v.w), node->w.q.l, node->w.q.h);
1144                 break;
1145             case jit_code_extr_u:
1146                 extr_u(rn(node->u.w), rn(node->v.w), node->w.q.l, node->w.q.h);
1147                 break;
1148             case jit_code_depr:
1149                 depr(rn(node->u.w), rn(node->v.w), node->w.q.l, node->w.q.h);
1150                 break;
1151             case jit_code_depi:
1152                 depi(rn(node->u.w), node->v.w, node->w.q.l, node->w.q.h);
1153                 break;
1154                 case_rr(ext, _c);
1155                 case_rr(ext, _uc);
1156                 case_rr(ext, _s);
1157                 case_rr(ext, _us);
1158                 case_rr(ext, _i);
1159                 case_rr(ext, _ui);
1160             case jit_code_casr:
1161                 casr(rn(node->u.w), rn(node->v.w),
1162                      rn(node->w.q.l), rn(node->w.q.h));
1163                 break;
1164             case jit_code_casi:
1165                 casi(rn(node->u.w), node->v.w,
1166                      rn(node->w.q.l), rn(node->w.q.h));
1167                 break;
1168                 case_rrr(movn,);
1169                 case_rrr(movz,);
1170                 case_rr(mov,);
1171             case jit_code_movi:
1172                 if (node->flag & jit_flag_node) {
1173                     temp = node->v.n;
1174                     if (temp->code == jit_code_data ||
1175                         (temp->code == jit_code_label &&
1176                          (temp->flag & jit_flag_patch)))
1177                         movi(rn(node->u.w), temp->u.w);
1178                     else {
1179                         assert(temp->code == jit_code_label ||
1180                                temp->code == jit_code_epilog);
1181                         word = movi_p(rn(node->u.w), node->v.w);
1182                         patch(word, node);
1183                     }
1184                 }
1185                 else
1186                     movi(rn(node->u.w), node->v.w);
1187                 break;
1188                 case_rr(neg,);
1189                 case_rr(com,);
1190                 case_rr(clo,);
1191                 case_rr(clz,);
1192                 case_rr(cto,);
1193                 case_rr(ctz,);
1194 #define rbitr(r0, r1)   fallback_rbit(r0, r1)
1195                 case_rr(rbit,);
1196                 case_rr(popcnt,);
1197                 case_rrr(lt,);
1198                 case_rrw(lt,);
1199                 case_rrr(lt, _u);
1200                 case_rrw(lt, _u);
1201                 case_rrr(le,);
1202                 case_rrw(le,);
1203                 case_rrr(le, _u);
1204                 case_rrw(le, _u);
1205                 case_rrr(eq,);
1206                 case_rrw(eq,);
1207                 case_rrr(ge,);
1208                 case_rrw(ge,);
1209                 case_rrr(ge, _u);
1210                 case_rrw(ge, _u);
1211                 case_rrr(gt,);
1212                 case_rrw(gt,);
1213                 case_rrr(gt, _u);
1214                 case_rrw(gt, _u);
1215                 case_rrr(ne,);
1216                 case_rrw(ne,);
1217                 case_brr(blt,);
1218                 case_brw(blt,);
1219                 case_brr(blt, _u);
1220                 case_brw(blt, _u);
1221                 case_brr(ble,);
1222                 case_brw(ble,);
1223                 case_brr(ble, _u);
1224                 case_brw(ble, _u);
1225                 case_brr(beq,);
1226                 case_brw(beq,);
1227                 case_brr(bge,);
1228                 case_brw(bge,);
1229                 case_brr(bge, _u);
1230                 case_brw(bge, _u);
1231                 case_brr(bgt,);
1232                 case_brw(bgt,);
1233                 case_brr(bgt, _u);
1234                 case_brw(bgt, _u);
1235                 case_brr(bne,);
1236                 case_brw(bne,);
1237                 case_brr(boadd,);
1238                 case_brw(boadd,);
1239                 case_brr(boadd, _u);
1240                 case_brw(boadd, _u);
1241                 case_brr(bxadd,);
1242                 case_brw(bxadd,);
1243                 case_brr(bxadd, _u);
1244                 case_brw(bxadd, _u);
1245                 case_brr(bosub,);
1246                 case_brw(bosub,);
1247                 case_brr(bosub, _u);
1248                 case_brw(bosub, _u);
1249                 case_brr(bxsub,);
1250                 case_brw(bxsub,);
1251                 case_brr(bxsub, _u);
1252                 case_brw(bxsub, _u);
1253                 case_brr(bms,);
1254                 case_brw(bms,);
1255                 case_brr(bmc,);
1256                 case_brw(bmc,);
1257                 case_rrr(add, _f);
1258                 case_rrf(add, _f, 32);
1259                 case_rrr(sub, _f);
1260                 case_rrf(sub, _f, 32);
1261                 case_rrf(rsb, _f, 32);
1262                 case_rrr(mul, _f);
1263                 case_rrf(mul, _f, 32);
1264                 case_rrr(div, _f);
1265                 case_rrf(div, _f, 32);
1266                 case_rr(abs, _f);
1267                 case_rr(neg, _f);
1268                 case_rr(sqrt, _f);
1269                 case_rqr(fma, _f);
1270                 case_rqr(fms, _f);
1271                 case_rqr(fnma, _f);
1272                 case_rqr(fnms, _f);
1273                 case_rr(ext, _f);
1274                 case_rr(ld, _f);
1275                 case_rw(ld, _f);
1276                 case_rrr(ldx, _f);
1277                 case_rrw(ldx, _f);
1278 #define unldr_x(r0, r1, i0)     fallback_unldr_x(r0, r1, i0)
1279             case jit_code_unldr_x:
1280                 unldr_x(rn(node->u.w), rn(node->v.w), node->w.w);
1281                 break;
1282 #define unldi_x(r0, i0, i1)     fallback_unldi_x(r0, i0, i1)
1283             case jit_code_unldi_x:
1284                 unldi_x(rn(node->u.w), node->v.w, node->w.w);
1285                 break;
1286                 case_rr(st, _f);
1287                 case_wr(st, _f);
1288                 case_rrr(stx, _f);
1289                 case_wrr(stx, _f);
1290                 /* Cost of loading, masking, oring, etc to use STQ_U is
1291                  * too high. Could be branchless for doubles, but would
1292                  * generate larger code, and speed for unaligned double
1293                  * store is not so important; just support it. */
1294 #define unstr_x(r0, r1, i0)     fallback_unstr_x(r0, r1, i0)
1295             case jit_code_unstr_x:
1296                 unstr_x(rn(node->u.w), rn(node->v.w), node->w.w);
1297                 break;
1298 #define unsti_x(i0, r0, i1)     fallback_unsti_x(i0, r0, i1)
1299             case jit_code_unsti_x:
1300                 unsti_x(node->u.w, rn(node->v.w), node->w.w);
1301                 break;
1302                 case_rr(mov, _f);
1303             case jit_code_movi_f:
1304                 assert(node->flag & jit_flag_data);
1305                 movi_f(rn(node->u.w), (jit_float32_t *)node->v.n->u.w);
1306                 break;
1307                 case_rr(ext, _d_f);
1308                 case_rrr(lt, _f);
1309                 case_rrf(lt, _f, 32);
1310                 case_rrr(le, _f);
1311                 case_rrf(le, _f, 32);
1312                 case_rrr(eq, _f);
1313                 case_rrf(eq, _f, 32);
1314                 case_rrr(ge, _f);
1315                 case_rrf(ge, _f, 32);
1316                 case_rrr(gt, _f);
1317                 case_rrf(gt, _f, 32);
1318                 case_rrr(ne, _f);
1319                 case_rrf(ne, _f, 32);
1320                 case_rrr(unlt, _f);
1321                 case_rrf(unlt, _f, 32);
1322                 case_rrr(unle, _f);
1323                 case_rrf(unle, _f, 32);
1324                 case_rrr(uneq, _f);
1325                 case_rrf(uneq, _f, 32);
1326                 case_rrr(unge, _f);
1327                 case_rrf(unge, _f, 32);
1328                 case_rrr(ungt, _f);
1329                 case_rrf(ungt, _f, 32);
1330                 case_rrr(ltgt, _f);
1331                 case_rrf(ltgt, _f, 32);
1332                 case_rrr(ord, _f);
1333                 case_rrf(ord, _f, 32);
1334                 case_rrr(unord, _f);
1335                 case_rrf(unord, _f, 32);
1336                 case_brr(blt, _f);
1337                 case_brf(blt, _f, 32);
1338                 case_brr(ble, _f);
1339                 case_brf(ble, _f, 32);
1340                 case_brr(beq, _f);
1341                 case_brf(beq, _f, 32);
1342                 case_brr(bge, _f);
1343                 case_brf(bge, _f, 32);
1344                 case_brr(bgt, _f);
1345                 case_brf(bgt, _f, 32);
1346                 case_brr(bne, _f);
1347                 case_brf(bne, _f, 32);
1348                 case_brr(bunlt, _f);
1349                 case_brf(bunlt, _f, 32);
1350                 case_brr(bunle, _f);
1351                 case_brf(bunle, _f, 32);
1352                 case_brr(buneq, _f);
1353                 case_brf(buneq, _f, 32);
1354                 case_brr(bunge, _f);
1355                 case_brf(bunge, _f, 32);
1356                 case_brr(bungt, _f);
1357                 case_brf(bungt, _f, 32);
1358                 case_brr(bltgt, _f);
1359                 case_brf(bltgt, _f, 32);
1360                 case_brr(bord, _f);
1361                 case_brf(bord, _f, 32);
1362                 case_brr(bunord, _f);
1363                 case_brf(bunord, _f, 32);
1364                 case_rrr(add, _d);
1365                 case_rrf(add, _d, 64);
1366                 case_rrr(sub, _d);
1367                 case_rrf(sub, _d, 64);
1368                 case_rrf(rsb, _d, 64);
1369                 case_rrr(mul, _d);
1370                 case_rrf(mul, _d, 64);
1371                 case_rrr(div, _d);
1372                 case_rrf(div, _d, 64);
1373                 case_rr(abs, _d);
1374                 case_rr(neg, _d);
1375                 case_rr(sqrt, _d);
1376                 case_rqr(fma, _d);
1377                 case_rqr(fms, _d);
1378                 case_rqr(fnma, _d);
1379                 case_rqr(fnms, _d);
1380                 case_rr(ext, _d);
1381                 case_rr(ld, _d);
1382                 case_rw(ld, _d);
1383                 case_rrr(ldx, _d);
1384                 case_rrw(ldx, _d);
1385                 case_rr(st, _d);
1386                 case_wr(st, _d);
1387                 case_rrr(stx, _d);
1388                 case_wrr(stx, _d);
1389                 case_rr(mov, _d);
1390             case jit_code_movi_d:
1391                 assert(node->flag & jit_flag_data);
1392                 movi_d(rn(node->u.w), (jit_float64_t *)node->v.n->u.w);
1393                 break;
1394                 case_rr(ext, _f_d);
1395                 case_rrr(lt, _d);
1396                 case_rrf(lt, _d, 64);
1397                 case_rrr(le, _d);
1398                 case_rrf(le, _d, 64);
1399                 case_rrr(eq, _d);
1400                 case_rrf(eq, _d, 64);
1401                 case_rrr(ge, _d);
1402                 case_rrf(ge, _d, 64);
1403                 case_rrr(gt, _d);
1404                 case_rrf(gt, _d, 64);
1405                 case_rrr(ne, _d);
1406                 case_rrf(ne, _d, 64);
1407                 case_rrr(unlt, _d);
1408                 case_rrf(unlt, _d, 64);
1409                 case_rrr(unle, _d);
1410                 case_rrf(unle, _d, 64);
1411                 case_rrr(uneq, _d);
1412                 case_rrf(uneq, _d, 64);
1413                 case_rrr(unge, _d);
1414                 case_rrf(unge, _d, 64);
1415                 case_rrr(ungt, _d);
1416                 case_rrf(ungt, _d, 64);
1417                 case_rrr(ltgt, _d);
1418                 case_rrf(ltgt, _d, 64);
1419                 case_rrr(ord, _d);
1420                 case_rrf(ord, _d, 64);
1421                 case_rrr(unord, _d);
1422                 case_rrf(unord, _d, 64);
1423                 case_brr(blt, _d);
1424                 case_brf(blt, _d, 64);
1425                 case_brr(ble, _d);
1426                 case_brf(ble, _d, 64);
1427                 case_brr(beq, _d);
1428                 case_brf(beq, _d, 64);
1429                 case_brr(bge, _d);
1430                 case_brf(bge, _d, 64);
1431                 case_brr(bgt, _d);
1432                 case_brf(bgt, _d, 64);
1433                 case_brr(bne, _d);
1434                 case_brf(bne, _d, 64);
1435                 case_brr(bunlt, _d);
1436                 case_brf(bunlt, _d, 64);
1437                 case_brr(bunle, _d);
1438                 case_brf(bunle, _d, 64);
1439                 case_brr(buneq, _d);
1440                 case_brf(buneq, _d, 64);
1441                 case_brr(bunge, _d);
1442                 case_brf(bunge, _d, 64);
1443                 case_brr(bungt, _d);
1444                 case_brf(bungt, _d, 64);
1445                 case_brr(bltgt, _d);
1446                 case_brf(bltgt, _d, 64);
1447                 case_brr(bord, _d);
1448                 case_brf(bord, _d, 64);
1449                 case_brr(bunord, _d);
1450                 case_brf(bunord, _d, 64);
1451             case jit_code_jmpr:
1452                 jmpr(rn(node->u.w));
1453                 break;
1454             case jit_code_jmpi:
1455                 if (node->flag & jit_flag_node) {
1456                     temp = node->u.n;
1457                     assert(temp->code == jit_code_label ||
1458                            temp->code == jit_code_epilog);
1459                     if (temp->flag & jit_flag_patch)
1460                         jmpi(temp->u.w);
1461                     else {
1462                         word = _jit->code.length -
1463                             (_jit->pc.uc - _jit->code.ptr);
1464                         if (_s21_p(word))
1465                             word = jmpi(_jit->pc.w);
1466                         else
1467                             word = jmpi_p(_jit->pc.w);
1468                         patch(word, node);
1469                     }
1470                 }
1471                 else
1472                     jmpi(node->u.w);
1473                 break;
1474             case jit_code_callr:
1475                 callr(rn(node->u.w));
1476                 break;
1477             case jit_code_calli:
1478                 if (node->flag & jit_flag_node) {
1479                     temp = node->u.n;
1480                     assert(temp->code == jit_code_label ||
1481                            temp->code == jit_code_epilog);
1482                     if (!(temp->flag & jit_flag_patch)) {
1483                         word = calli_p(temp->u.w);
1484                         patch(word, node);
1485                     }
1486                     else
1487                         calli(temp->u.w);
1488                 }
1489                 else
1490                     calli(node->u.w);
1491                 break;
1492             case jit_code_prolog:
1493                 _jitc->function = _jitc->functions.ptr + node->w.w;
1494                 undo.node = node;
1495                 undo.word = _jit->pc.w;
1496                 memcpy(&undo.func, _jitc->function, sizeof(undo.func));
1497 #if DEVEL_DISASSEMBLER
1498                 undo.prevw = prevw;
1499 #endif
1500                 undo.patch_offset = _jitc->patches.offset;
1501             restart_function:
1502                 _jitc->again = 0;
1503                 prolog(node);
1504                 break;
1505             case jit_code_epilog:
1506                 assert(_jitc->function == _jitc->functions.ptr + node->w.w);
1507                 if (_jitc->again) {
1508                     for (temp = undo.node->next;
1509                          temp != node; temp = temp->next) {
1510                         if (temp->code == jit_code_label ||
1511                             temp->code == jit_code_epilog)
1512                             temp->flag &= ~jit_flag_patch;
1513                     }
1514                     temp->flag &= ~jit_flag_patch;
1515                     node = undo.node;
1516                     _jit->pc.w = undo.word;
1517                     /* undo.func.self.aoff and undo.func.regset should not
1518                      * be undone, as they will be further updated, and are
1519                      * the reason of the undo. */
1520                     undo.func.self.aoff = _jitc->function->frame +
1521                         _jitc->function->self.aoff;
1522                     /* allocar information also does not need to be undone */
1523                     undo.func.aoffoff = _jitc->function->aoffoff;
1524                     undo.func.allocar = _jitc->function->allocar;
1525                     jit_regset_set(&undo.func.regset, &_jitc->function->regset);
1526                     memcpy(_jitc->function, &undo.func, sizeof(undo.func));
1527 #if DEVEL_DISASSEMBLER
1528                     prevw = undo.prevw;
1529 #endif
1530                     _jitc->patches.offset = undo.patch_offset;
1531                     goto restart_function;
1532                 }
1533                 /* remember label is defined */
1534                 node->flag |= jit_flag_patch;
1535                 node->u.w = _jit->pc.w;
1536                 epilog(node);
1537                 _jitc->function = NULL;
1538                 break;
1539             case jit_code_movr_w_f:
1540                 movr_w_f(rn(node->u.w), rn(node->v.w));
1541                 break;
1542             case jit_code_movr_f_w:
1543                 movr_f_w(rn(node->u.w), rn(node->v.w));
1544                 break;
1545             case jit_code_movi_f_w:
1546                 assert(node->flag & jit_flag_data);
1547                 movi_f_w(rn(node->u.w), *(jit_float32_t *)node->v.n->u.w);
1548                 break;
1549             case jit_code_movi_w_f:
1550                 movi_w_f(rn(node->u.w), node->v.w);
1551                 break;
1552             case jit_code_movr_d_w:
1553                 movr_d_w(rn(node->u.w), rn(node->v.w));
1554                 break;
1555             case jit_code_movi_d_w:
1556                 assert(node->flag & jit_flag_data);
1557                 movi_d_w(rn(node->u.w), *(jit_float64_t *)node->v.n->u.w);
1558                 break;
1559             case jit_code_movr_w_d:
1560                 movr_w_d(rn(node->u.w), rn(node->v.w));
1561                 break;
1562             case jit_code_movi_w_d:
1563                 movi_w_d(rn(node->u.w), node->v.w);
1564                 break;
1565             case jit_code_va_start:
1566                 vastart(rn(node->u.w));
1567                 break;
1568             case jit_code_va_arg:
1569                 vaarg(rn(node->u.w), rn(node->v.w));
1570                 break;
1571             case jit_code_va_arg_d:
1572                 vaarg_d(rn(node->u.w), rn(node->v.w));
1573                 break;
1574             case jit_code_live:                 case jit_code_ellipsis:
1575             case jit_code_va_push:
1576             case jit_code_allocai:              case jit_code_allocar:
1577             case jit_code_arg_c:                case jit_code_arg_s:
1578             case jit_code_arg_i:                case jit_code_arg_l:
1579             case jit_code_arg_f:                case jit_code_arg_d:
1580             case jit_code_va_end:
1581             case jit_code_ret:
1582             case jit_code_retr_c:               case jit_code_reti_c:
1583             case jit_code_retr_uc:              case jit_code_reti_uc:
1584             case jit_code_retr_s:               case jit_code_reti_s:
1585             case jit_code_retr_us:              case jit_code_reti_us:
1586             case jit_code_retr_i:               case jit_code_reti_i:
1587             case jit_code_retr_ui:              case jit_code_reti_ui:
1588             case jit_code_retr_l:               case jit_code_reti_l:
1589             case jit_code_retr_f:               case jit_code_reti_f:
1590             case jit_code_retr_d:               case jit_code_reti_d:
1591             case jit_code_getarg_c:             case jit_code_getarg_uc:
1592             case jit_code_getarg_s:             case jit_code_getarg_us:
1593             case jit_code_getarg_i:             case jit_code_getarg_ui:
1594             case jit_code_getarg_l:
1595             case jit_code_getarg_f:             case jit_code_getarg_d:
1596             case jit_code_putargr_c:            case jit_code_putargi_c:
1597             case jit_code_putargr_uc:           case jit_code_putargi_uc:
1598             case jit_code_putargr_s:            case jit_code_putargi_s:
1599             case jit_code_putargr_us:           case jit_code_putargi_us:
1600             case jit_code_putargr_i:            case jit_code_putargi_i:
1601             case jit_code_putargr_ui:           case jit_code_putargi_ui:
1602             case jit_code_putargr_l:            case jit_code_putargi_l:
1603             case jit_code_putargr_f:            case jit_code_putargi_f:
1604             case jit_code_putargr_d:            case jit_code_putargi_d:
1605             case jit_code_pushargr_c:           case jit_code_pushargi_c:
1606             case jit_code_pushargr_uc:          case jit_code_pushargi_uc:
1607             case jit_code_pushargr_s:           case jit_code_pushargi_s:
1608             case jit_code_pushargr_us:          case jit_code_pushargi_us:
1609             case jit_code_pushargr_i:           case jit_code_pushargi_i:
1610             case jit_code_pushargr_ui:          case jit_code_pushargi_ui:
1611             case jit_code_pushargr_l:           case jit_code_pushargi_l:
1612             case jit_code_pushargr_f:           case jit_code_pushargi_f:
1613             case jit_code_pushargr_d:           case jit_code_pushargi_d:
1614             case jit_code_retval_c:             case jit_code_retval_uc:
1615             case jit_code_retval_s:             case jit_code_retval_us:
1616             case jit_code_retval_i:
1617             case jit_code_retval_ui:            case jit_code_retval_l:
1618             case jit_code_retval_f:             case jit_code_retval_d:
1619             case jit_code_prepare:
1620             case jit_code_finishr:              case jit_code_finishi:
1621             case jit_code_negi_f:               case jit_code_absi_f:
1622             case jit_code_sqrti_f:              case jit_code_negi_d:
1623             case jit_code_absi_d:               case jit_code_sqrti_d:
1624                 break;
1625             case jit_code_negi:
1626                 negi(rn(node->u.w), node->v.w);
1627                 break;
1628             case jit_code_comi:
1629                 comi(rn(node->u.w), node->v.w);
1630                 break;
1631             case jit_code_exti_c:
1632                 exti_c(rn(node->u.w), node->v.w);
1633                 break;
1634             case jit_code_exti_uc:
1635                 exti_uc(rn(node->u.w), node->v.w);
1636                 break;
1637             case jit_code_exti_s:
1638                 exti_s(rn(node->u.w), node->v.w);
1639                 break;
1640             case jit_code_exti_us:
1641                 exti_us(rn(node->u.w), node->v.w);
1642                 break;
1643             case jit_code_bswapi_us:
1644                 bswapi_us(rn(node->u.w), node->v.w);
1645                 break;
1646             case jit_code_bswapi_ui:
1647                 bswapi_ui(rn(node->u.w), node->v.w);
1648                 break;
1649             case jit_code_htoni_us:
1650                 htoni_us(rn(node->u.w), node->v.w);
1651                 break;
1652             case jit_code_htoni_ui:
1653                 htoni_ui(rn(node->u.w), node->v.w);
1654                 break;
1655             case jit_code_exti_i:
1656                 exti_i(rn(node->u.w), node->v.w);
1657                 break;
1658             case jit_code_exti_ui:
1659                 exti_ui(rn(node->u.w), node->v.w);
1660                 break;
1661             case jit_code_bswapi_ul:
1662                 bswapi_ul(rn(node->u.w), node->v.w);
1663                 break;
1664             case jit_code_htoni_ul:
1665                 htoni_ul(rn(node->u.w), node->v.w);
1666                 break;
1667             case jit_code_cloi:
1668                 cloi(rn(node->u.w), node->v.w);
1669                 break;
1670             case jit_code_clzi:
1671                 clzi(rn(node->u.w), node->v.w);
1672                 break;
1673             case jit_code_ctoi:
1674                 ctoi(rn(node->u.w), node->v.w);
1675                 break;
1676             case jit_code_ctzi:
1677                 ctzi(rn(node->u.w), node->v.w);
1678                 break;
1679             case jit_code_rbiti:
1680                 rbiti(rn(node->u.w), node->v.w);
1681                 break;
1682             case jit_code_popcnti:
1683                 popcnti(rn(node->u.w), node->v.w);
1684                 break;
1685             case jit_code_exti:
1686                 exti(rn(node->u.w), node->v.w, node->w.q.l, node->w.q.h);
1687                 break;
1688             case jit_code_exti_u:
1689                 exti_u(rn(node->u.w), node->v.w, node->w.q.l, node->w.q.h);
1690                 break;
1691             default:
1692                 abort();
1693         }
1694         if (jit_carry != _NOREG) {
1695             switch (node->code) {
1696                 case jit_code_note:
1697                 case jit_code_addcr:            case jit_code_addci:
1698                 case jit_code_addxr:            case jit_code_addxi:
1699                 case jit_code_subcr:            case jit_code_subci:
1700                 case jit_code_subxr:            case jit_code_subxi:
1701                     break;
1702                 default:
1703                     jit_unget_reg(jit_carry);
1704                     jit_carry = _NOREG;
1705                     break;
1706             }
1707         }
1708         jit_regarg_clr(node, value);
1709         assert(_jitc->regarg == 0 ||
1710                (jit_carry != _NOREG && _jitc->regarg == (1 << jit_carry)));
1711         assert(_jitc->synth == 0);
1712         /* update register live state */
1713         jit_reglive(node);
1714     }
1715 #undef case_brf
1716 #undef case_brw
1717 #undef case_brr
1718 #undef case_wrr
1719 #undef case_rrrw
1720 #undef case_rrrr
1721 #undef case_rrf
1722 #undef case_rrw
1723 #undef case_rrr
1724 #undef case_wr
1725 #undef case_rw
1726 #undef case_rr
1727     for (offset = 0; offset < _jitc->patches.offset; offset++) {
1728         node = _jitc->patches.ptr[offset].node;
1729         word = node->code == jit_code_movi ? node->v.n->u.w : node->u.n->u.w;
1730         patch_at(_jitc->patches.ptr[offset].inst, word);
1731     }
1732
1733     jit_flush(_jit->code.ptr, _jit->pc.uc);
1734
1735     return (_jit->code.ptr);
1736 }
1737
1738 #define CODE                            1
1739 #  include "jit_alpha-cpu.c"
1740 #  include "jit_alpha-fpu.c"
1741 #  include "jit_fallback.c"
1742 #undef CODE
1743
1744 void
1745 jit_flush(void *fptr, void *tptr)
1746 {
1747 }
1748
1749 void
1750 _emit_ldxi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1751 {
1752     ldxi(rn(r0), rn(r1), i0);
1753 }
1754
1755 void
1756 _emit_stxi(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_int32_t r1)
1757 {
1758     stxi(i0, rn(r0), rn(r1));
1759 }
1760
1761 void
1762 _emit_ldxi_d(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1763 {
1764     ldxi_d(rn(r0), rn(r1), i0);
1765 }
1766
1767 void
1768 _emit_stxi_d(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_int32_t r1)
1769 {
1770     stxi_d(i0, rn(r0), rn(r1));
1771 }
1772
1773 static void
1774 _patch(jit_state_t *_jit, jit_word_t instr, jit_node_t *node)
1775 {
1776     jit_int32_t          flag;
1777
1778     assert(node->flag & jit_flag_node);
1779     if (node->code == jit_code_movi)
1780         flag = node->v.n->flag;
1781     else
1782         flag = node->u.n->flag;
1783      assert(!(flag & jit_flag_patch));
1784     if (_jitc->patches.offset >= _jitc->patches.length) {
1785         jit_realloc((jit_pointer_t *)&_jitc->patches.ptr,
1786                     _jitc->patches.length * sizeof(jit_patch_t),
1787                     (_jitc->patches.length + 1024) * sizeof(jit_patch_t));
1788         _jitc->patches.length += 1024;
1789     }
1790     _jitc->patches.ptr[_jitc->patches.offset].inst = instr;
1791     _jitc->patches.ptr[_jitc->patches.offset].node = node;
1792     ++_jitc->patches.offset;
1793 }