2 * Copyright (C) 2013-2023 Free Software Foundation, Inc.
4 * This file is part of GNU lightning.
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)
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.
17 * Paulo Cesar Pereira de Andrade
20 #include <lightning.h>
21 #include <lightning/jit_private.h>
23 #define jit_arg_reg_p(i) ((i) >= 0 && (i) < 8)
24 #if __BYTE_ORDER == __LITTLE_ENDIAN
30 # define C_DISP 8 - sizeof(jit_int8_t)
31 # define S_DISP 8 - sizeof(jit_int16_t)
32 # define I_DISP 8 - sizeof(jit_int32_t)
33 # define F_DISP 8 - sizeof(jit_float32_t)
39 typedef jit_pointer_t jit_va_list_t;
44 #define patch(instr, node) _patch(_jit, instr, node)
45 static void _patch(jit_state_t*,jit_word_t,jit_node_t*);
49 extern void __clear_cache(void *, void *);
53 # include "jit_ia64-cpu.c"
54 # include "jit_ia64-fpu.c"
55 # include "jit_fallback.c"
62 jit_register_t _rvs[] = {
67 /* Used when a register cannot be allocated */
69 /* First scratch register */
71 /* Use r4 as lightning fp register */
73 /* Do not touch callee save registers not automatically spill/reloaded */
74 { rc(sav)|5, "r5" }, { rc(sav)|6, "r6" },
76 /* Do not touch return register for the sake of simplicity, besides
77 * having JIT_R0 being the same as JIT_RET usually an optimization */
79 /* Return registers, use as temporaries */
81 { rc(gpr)|10, "r10" }, { rc(gpr)|11, "r11" },
83 { rc(sav)|12, "r12" },
85 { rc(sav)|13, "r13" },
86 /* (Usually) assembly temporaries */
87 { rc(gpr)|31, "r31" }, { rc(gpr)|30, "r30" },
88 { rc(gpr)|29, "r29" }, { rc(gpr)|28, "r28" },
89 { rc(gpr)|27, "r27" }, { rc(gpr)|26, "r26" },
90 { rc(gpr)|25, "r25" }, { rc(gpr)|24, "r24" },
91 { rc(gpr)|23, "r23" }, { rc(gpr)|22, "r22" },
92 { rc(gpr)|21, "r21" }, { rc(gpr)|20, "r20" },
93 { rc(gpr)|19, "r19" }, { rc(gpr)|18, "r18" },
94 { rc(gpr)|17, "r17" }, { rc(gpr)|16, "r16" },
95 { rc(gpr)|15, "r15" }, { rc(gpr)|14, "r14" },
96 /* Do not allow allocating r32-r41 as temoraries for the sake of
97 * avoiding the need of extra complexity in the non backend code */
98 { rc(arg)|32, "r32" }, { rc(arg)|33, "r33" },
99 { rc(arg)|34, "r34" }, { rc(arg)|35, "r35" },
100 { rc(arg)|36, "r36" }, { rc(arg)|37, "r37" },
101 { rc(arg)|38, "r38" }, { rc(arg)|39, "r39" },
103 { rc(gpr)|rc(sav)|40, "r40" }, { rc(gpr)|rc(sav)|41, "r41" },
104 { rc(gpr)|rc(sav)|42, "r42" }, { rc(gpr)|rc(sav)|43, "r43" },
105 { rc(gpr)|rc(sav)|44, "r44" }, { rc(gpr)|rc(sav)|45, "r45" },
106 { rc(gpr)|rc(sav)|46, "r46" }, { rc(gpr)|rc(sav)|47, "r47" },
107 /* Temporaries/locals */
108 { rc(gpr)|rc(sav)|48, "r48" }, { rc(gpr)|rc(sav)|49, "r49" },
109 { rc(gpr)|rc(sav)|50, "r50" }, { rc(gpr)|rc(sav)|51, "r51" },
110 { rc(gpr)|rc(sav)|52, "r52" }, { rc(gpr)|rc(sav)|53, "r53" },
111 { rc(gpr)|rc(sav)|54, "r54" }, { rc(gpr)|rc(sav)|55, "r55" },
112 { rc(gpr)|rc(sav)|56, "r56" }, { rc(gpr)|rc(sav)|57, "r57" },
113 { rc(gpr)|rc(sav)|58, "r58" }, { rc(gpr)|rc(sav)|59, "r59" },
114 { rc(gpr)|rc(sav)|60, "r60" }, { rc(gpr)|rc(sav)|61, "r61" },
115 { rc(gpr)|rc(sav)|62, "r62" }, { rc(gpr)|rc(sav)|63, "r63" },
116 { rc(gpr)|rc(sav)|64, "r64" }, { rc(gpr)|rc(sav)|65, "r65" },
117 { rc(gpr)|rc(sav)|66, "r66" }, { rc(gpr)|rc(sav)|67, "r67" },
118 { rc(gpr)|rc(sav)|68, "r68" }, { rc(gpr)|rc(sav)|69, "r69" },
119 { rc(gpr)|rc(sav)|70, "r70" }, { rc(gpr)|rc(sav)|71, "r71" },
120 { rc(gpr)|rc(sav)|72, "r72" }, { rc(gpr)|rc(sav)|73, "r73" },
121 { rc(gpr)|rc(sav)|74, "r74" }, { rc(gpr)|rc(sav)|75, "r75" },
122 { rc(gpr)|rc(sav)|76, "r76" }, { rc(gpr)|rc(sav)|77, "r77" },
123 { rc(gpr)|rc(sav)|78, "r78" }, { rc(gpr)|rc(sav)|79, "r79" },
124 { rc(gpr)|rc(sav)|80, "r80" }, { rc(gpr)|rc(sav)|81, "r81" },
125 { rc(gpr)|rc(sav)|82, "r82" }, { rc(gpr)|rc(sav)|83, "r83" },
126 { rc(gpr)|rc(sav)|84, "r84" }, { rc(gpr)|rc(sav)|85, "r85" },
127 { rc(gpr)|rc(sav)|86, "r86" }, { rc(gpr)|rc(sav)|87, "r87" },
128 { rc(gpr)|rc(sav)|88, "r88" }, { rc(gpr)|rc(sav)|89, "r89" },
129 { rc(gpr)|rc(sav)|90, "r90" }, { rc(gpr)|rc(sav)|91, "r91" },
130 { rc(gpr)|rc(sav)|92, "r92" }, { rc(gpr)|rc(sav)|93, "r93" },
131 { rc(gpr)|rc(sav)|94, "r94" }, { rc(gpr)|rc(sav)|95, "r95" },
132 { rc(gpr)|rc(sav)|96, "r96" }, { rc(gpr)|rc(sav)|97, "r97" },
133 { rc(gpr)|rc(sav)|98, "r98" }, { rc(gpr)|rc(sav)|99, "r99" },
134 { rc(gpr)|rc(sav)|100,"r100"}, { rc(gpr)|rc(sav)|101, "r101"},
135 { rc(gpr)|rc(sav)|102,"r102"}, { rc(gpr)|rc(sav)|103, "r103"},
136 { rc(gpr)|rc(sav)|104,"r104"}, { rc(gpr)|rc(sav)|105, "r105"},
137 { rc(gpr)|rc(sav)|106,"r106"}, { rc(gpr)|rc(sav)|107, "r107"},
138 { rc(gpr)|rc(sav)|108,"r108"}, { rc(gpr)|rc(sav)|109, "r109"},
139 { rc(gpr)|rc(sav)|110,"r110"}, { rc(gpr)|rc(sav)|111, "r111"},
140 { rc(gpr)|rc(sav)|112,"r112"}, { rc(gpr)|rc(sav)|113, "r113"},
141 { rc(gpr)|rc(sav)|114,"r114"},
142 /* Do not enable these because no matter what, want 13 free registers,
143 * 5 for prolog and epilog and 8 for outgoing arguments */
145 { 116, "r116"}, { 117, "r117"},
146 { 118, "r118"}, { 119, "r119"},
147 { 120, "r120"}, { 121, "r121"},
148 { 122, "r122"}, { 123, "r123"},
149 { 124, "r124"}, { 125, "r125"},
150 { 126, "r126"}, { 127, "r127"},
155 /* Do not touch callee save registers not automatically spill/reloaded */
156 { rc(sav)|2, "f2" }, { rc(sav)|3, "f3" },
157 { rc(sav)|4, "f4" }, { rc(sav)|5, "f5" },
159 { rc(fpr)|6, "f6" }, { rc(fpr)|7, "f7" },
160 /* Do not allocate for the sake of simplification */
162 /* Scratch - Argument/return registers */
164 { rc(arg)|10, "f10" }, { rc(arg)|11, "f11" },
165 { rc(arg)|12, "f12" }, { rc(arg)|13, "f13" },
166 { rc(arg)|14, "f14" }, { rc(arg)|15, "f15" },
167 /* Do not touch callee save registers not automatically spill/reloaded */
168 { rc(sav)|16, "f16" }, { rc(sav)|17, "f17" },
169 { rc(sav)|18, "f18" }, { rc(sav)|19, "f19" },
170 { rc(sav)|20, "f20" }, { rc(sav)|21, "f21" },
171 { rc(sav)|22, "f22" }, { rc(sav)|23, "f23" },
172 { rc(sav)|24, "f24" }, { rc(sav)|25, "f25" },
173 { rc(sav)|26, "f26" }, { rc(sav)|27, "f27" },
174 { rc(sav)|28, "f28" }, { rc(sav)|29, "f29" },
175 { rc(sav)|30, "f30" }, { rc(sav)|31, "f31" },
177 { rc(fpr)|32, "f32" }, { rc(fpr)|33, "f33" },
178 { rc(fpr)|34, "f34" }, { rc(fpr)|35, "f35" },
179 { rc(fpr)|36, "f36" }, { rc(fpr)|37, "f37" },
180 { rc(fpr)|38, "f38" }, { rc(fpr)|39, "f39" },
181 { rc(fpr)|40, "f40" }, { rc(fpr)|41, "f41" },
182 { rc(fpr)|42, "f42" }, { rc(fpr)|43, "f43" },
183 { rc(fpr)|44, "f44" }, { rc(fpr)|45, "f45" },
184 { rc(fpr)|46, "f46" }, { rc(fpr)|47, "f47" },
185 { rc(fpr)|48, "f48" }, { rc(fpr)|49, "f49" },
186 { rc(fpr)|50, "f50" }, { rc(fpr)|51, "f51" },
187 { rc(fpr)|52, "f52" }, { rc(fpr)|53, "f53" },
188 { rc(fpr)|54, "f54" }, { rc(fpr)|55, "f55" },
189 { rc(fpr)|56, "f56" }, { rc(fpr)|57, "f57" },
190 { rc(fpr)|58, "f58" }, { rc(fpr)|59, "f59" },
191 { rc(fpr)|60, "f60" }, { rc(fpr)|61, "f61" },
192 { rc(fpr)|62, "f62" }, { rc(fpr)|63, "f63" },
193 { rc(fpr)|64, "f64" }, { rc(fpr)|65, "f65" },
194 { rc(fpr)|66, "f66" }, { rc(fpr)|67, "f67" },
195 { rc(fpr)|68, "f68" }, { rc(fpr)|69, "f69" },
196 { rc(fpr)|70, "f70" }, { rc(fpr)|71, "f71" },
197 { rc(fpr)|72, "f72" }, { rc(fpr)|73, "f73" },
198 { rc(fpr)|74, "f74" }, { rc(fpr)|75, "f75" },
199 { rc(fpr)|76, "f76" }, { rc(fpr)|77, "f77" },
200 { rc(fpr)|78, "f78" }, { rc(fpr)|79, "f79" },
201 { rc(fpr)|80, "f80" }, { rc(fpr)|81, "f81" },
202 { rc(fpr)|82, "f82" }, { rc(fpr)|83, "f83" },
203 { rc(fpr)|84, "f84" }, { rc(fpr)|85, "f85" },
204 { rc(fpr)|86, "f86" }, { rc(fpr)|87, "f87" },
205 { rc(fpr)|88, "f88" }, { rc(fpr)|89, "f89" },
206 { rc(fpr)|90, "f90" }, { rc(fpr)|91, "f91" },
207 { rc(fpr)|92, "f92" }, { rc(fpr)|93, "f93" },
208 { rc(fpr)|94, "f94" }, { rc(fpr)|95, "f95" },
209 { rc(fpr)|96, "f96" }, { rc(fpr)|97, "f97" },
210 { rc(fpr)|98, "f98" }, { rc(fpr)|99, "f99" },
211 { rc(fpr)|100, "f100"}, { rc(fpr)|101, "f101"},
212 { rc(fpr)|102, "f102"}, { rc(fpr)|103, "f103"},
213 { rc(fpr)|104, "f104"}, { rc(fpr)|105, "f105"},
214 { rc(fpr)|106, "f106"}, { rc(fpr)|107, "f107"},
215 { rc(fpr)|108, "f108"}, { rc(fpr)|109, "f109"},
216 { rc(fpr)|110, "f110"}, { rc(fpr)|111, "f111"},
217 { rc(fpr)|112, "f112"}, { rc(fpr)|113, "f113"},
218 { rc(fpr)|114, "f114"}, { rc(fpr)|115, "f115"},
219 { rc(fpr)|116, "f116"}, { rc(fpr)|117, "f117"},
220 { rc(fpr)|118, "f118"}, { rc(fpr)|119, "f119"},
222 /* commented to fit a jit_regset_t in 256 bits, so that the fake
223 * O0-O7 registers are easily patched when an argument is pushed */
224 { rc(fpr)|120, "f120"}, { rc(fpr)|121, "f121"},
225 { rc(fpr)|122, "f122"}, { rc(fpr)|123, "f123"},
226 { rc(fpr)|124, "f124"}, { rc(fpr)|125, "f125"},
227 { rc(fpr)|126, "f126"}, { rc(fpr)|127, "f127"},
229 /* Fake registers to patch in movr and movi arguments */
230 { rc(arg)|120, "o0" }, { rc(arg)|121, "o1" },
231 { rc(arg)|122, "o2" }, { rc(arg)|123, "o3" },
232 { rc(arg)|124, "o4" }, { rc(arg)|125, "o5" },
233 { rc(arg)|126, "o6" }, { rc(arg)|127, "o7" },
234 { _NOREG, "<none>" },
244 __asm__ volatile("tf.nz.unc p6,p7=32;(p6)mov %0=1;(p7)mov %0=0"
246 assert(clz == 0 || clz == 1);
248 /* By default assume kernel or cpu will not handle unaligned load/store */
249 jit_cpu.unaligned = 1;
253 _jit_init(jit_state_t *_jit)
255 _jitc->reglen = jit_size(_rvs) - 1;
256 jit_regset_new(&_jitc->regs);
261 _jit_prolog(jit_state_t *_jit)
267 assert(jit_regset_cmp_ui(&_jitc->regarg, 0) == 0);
268 jit_regset_set_ui(&_jitc->regsav, 0);
269 offset = _jitc->functions.offset;
270 if (offset >= _jitc->functions.length) {
271 jit_realloc((jit_pointer_t *)&_jitc->functions.ptr,
272 _jitc->functions.length * sizeof(jit_function_t),
273 (_jitc->functions.length + 16) * sizeof(jit_function_t));
274 _jitc->functions.length += 16;
276 _jitc->function = _jitc->functions.ptr + _jitc->functions.offset++;
277 /* params_offset << 1, once for ours arguments, once for arguments
278 * to any called function; required because offsets are computed
279 * as JIT_FP displacement */
280 _jitc->function->self.size = stack_framesize + (params_offset << 1);
281 _jitc->function->self.argi = _jitc->function->self.argf =
282 _jitc->function->self.aoff = _jitc->function->self.alen = 0;
283 _jitc->function->self.call = jit_call_default;
284 jit_alloc((jit_pointer_t *)&_jitc->function->regoff,
285 _jitc->reglen * sizeof(jit_int32_t));
287 /* _no_link here does not mean the jit_link() call can be removed
289 * _jitc->function->prolog = jit_new_node(jit_code_prolog);
291 _jitc->function->prolog = jit_new_node_no_link(jit_code_prolog);
292 jit_link(_jitc->function->prolog);
293 _jitc->function->prolog->w.w = offset;
294 _jitc->function->epilog = jit_new_node_no_link(jit_code_epilog);
296 * v: offset in blocks vector
297 * w: offset in functions vector
299 _jitc->function->epilog->w.w = offset;
301 jit_regset_new(&_jitc->function->regset);
305 _jit_allocai(jit_state_t *_jit, jit_int32_t length)
307 assert(_jitc->function);
309 case 0: case 1: break;
310 case 2: _jitc->function->self.aoff &= -2; break;
311 case 3: case 4: _jitc->function->self.aoff &= -4; break;
312 default: _jitc->function->self.aoff &= -8; break;
314 _jitc->function->self.aoff -= length;
315 if (!_jitc->realize) {
316 jit_inc_synth_ww(allocai, _jitc->function->self.aoff, length);
319 return (_jitc->function->self.aoff);
323 _jit_allocar(jit_state_t *_jit, jit_int32_t u, jit_int32_t v)
326 assert(_jitc->function);
327 jit_inc_synth_ww(allocar, u, v);
328 if (!_jitc->function->allocar) {
329 _jitc->function->aoffoff = jit_allocai(sizeof(jit_int32_t));
330 _jitc->function->allocar = 1;
332 reg = jit_get_reg(jit_class_gpr);
334 jit_andi(reg, reg, -16);
335 jit_ldxi_i(u, JIT_FP, _jitc->function->aoffoff);
337 jit_addr(JIT_SP, JIT_SP, reg);
338 jit_stxi_i(_jitc->function->aoffoff, JIT_FP, u);
344 _jit_ret(jit_state_t *_jit)
347 assert(_jitc->function);
351 jit_patch_at(instr, _jitc->function->epilog);
356 _jit_retr(jit_state_t *_jit, jit_int32_t u, jit_code_t code)
358 jit_code_inc_synth_w(code, u);
359 jit_movr(JIT_RET, u);
365 _jit_reti(jit_state_t *_jit, jit_word_t u, jit_code_t code)
367 jit_code_inc_synth_w(code, u);
368 jit_movi(JIT_RET, u);
374 _jit_retr_f(jit_state_t *_jit, jit_int32_t u)
376 jit_inc_synth_w(retr_f, u);
377 jit_movr_f(JIT_FRET, u);
383 _jit_reti_f(jit_state_t *_jit, jit_float32_t u)
385 jit_inc_synth_f(reti_f, u);
386 jit_movi_f(JIT_FRET, u);
392 _jit_retr_d(jit_state_t *_jit, jit_int32_t u)
394 jit_inc_synth_w(retr_d, u);
395 jit_movr_d(JIT_FRET, u);
401 _jit_reti_d(jit_state_t *_jit, jit_float64_t u)
403 jit_inc_synth_d(reti_d, u);
404 jit_movi_d(JIT_FRET, u);
410 _jit_epilog(jit_state_t *_jit)
412 assert(_jitc->function);
413 assert(_jitc->function->epilog->next == NULL);
414 jit_link(_jitc->function->epilog);
415 _jitc->function = NULL;
419 _jit_arg_register_p(jit_state_t *_jit, jit_node_t *u)
421 if (u->code >= jit_code_arg_c && u->code <= jit_code_arg)
422 return (jit_arg_reg_p(u->u.w));
423 assert(u->code == jit_code_arg_f || u->code == jit_code_arg_d);
424 return (jit_arg_reg_p(u->u.w) || jit_arg_reg_p(u->u.w - 8));
428 _jit_ellipsis(jit_state_t *_jit)
430 jit_inc_synth(ellipsis);
431 if (_jitc->prepare) {
432 assert(!(_jitc->function->call.call & jit_call_varargs));
433 _jitc->function->call.call |= jit_call_varargs;
437 assert(!(_jitc->function->self.call & jit_call_varargs));
438 _jitc->function->self.call |= jit_call_varargs;
439 _jitc->function->vagp = _jitc->function->self.argi;
446 _jit_va_push(jit_state_t *_jit, jit_int32_t u)
448 jit_inc_synth_w(va_push, u);
454 _jit_arg(jit_state_t *_jit, jit_code_t code)
458 assert(_jitc->function);
459 assert(!(_jitc->function->self.call & jit_call_varargs));
460 #if STRONG_TYPE_CHECKING
461 assert(code >= jit_code_arg_c && code <= jit_code_arg);
463 if (jit_arg_reg_p(_jitc->function->self.argi))
464 offset = _jitc->function->self.argi++;
466 offset = _jitc->function->self.size;
467 _jitc->function->self.size += sizeof(jit_word_t);
469 node = jit_new_node_ww(code, offset,
470 ++_jitc->function->self.argn);
476 _jit_arg_f(jit_state_t *_jit)
480 assert(_jitc->function);
481 if (jit_arg_reg_p(_jitc->function->self.argi)) {
482 if (!(_jitc->function->self.call & jit_call_varargs))
483 offset = 8 + _jitc->function->self.argf++;
485 offset = _jitc->function->self.argi;
486 ++_jitc->function->self.argi;
489 offset = _jitc->function->self.size;
490 _jitc->function->self.size += sizeof(jit_word_t);
492 node = jit_new_node_ww(jit_code_arg_f, offset,
493 ++_jitc->function->self.argn);
499 _jit_arg_d(jit_state_t *_jit)
503 assert(_jitc->function);
504 if (jit_arg_reg_p(_jitc->function->self.argi)) {
505 if (!(_jitc->function->self.call & jit_call_varargs))
506 offset = 8 + _jitc->function->self.argf++;
508 offset = _jitc->function->self.argi;
509 ++_jitc->function->self.argi;
512 offset = _jitc->function->self.size;
513 _jitc->function->self.size += sizeof(jit_float64_t);
515 node = jit_new_node_ww(jit_code_arg_d, offset,
516 ++_jitc->function->self.argn);
522 _jit_getarg_c(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
524 assert_arg_type(v->code, jit_code_arg_c);
525 jit_inc_synth_wp(getarg_c, u, v);
526 if (jit_arg_reg_p(v->u.w))
527 jit_extr_c(u, _R32 + v->u.w);
529 jit_ldxi_c(u, JIT_FP, v->u.w + C_DISP);
534 _jit_getarg_uc(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
536 assert_arg_type(v->code, jit_code_arg_c);
537 jit_inc_synth_wp(getarg_uc, u, v);
538 if (jit_arg_reg_p(v->u.w))
539 jit_extr_uc(u, _R32 + v->u.w);
541 jit_ldxi_uc(u, JIT_FP, v->u.w + C_DISP);
546 _jit_getarg_s(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
548 assert_arg_type(v->code, jit_code_arg_s);
549 jit_inc_synth_wp(getarg_s, u, v);
550 if (jit_arg_reg_p(v->u.w))
551 jit_extr_s(u, _R32 + v->u.w);
553 jit_ldxi_s(u, JIT_FP, v->u.w + S_DISP);
558 _jit_getarg_us(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
560 assert_arg_type(v->code, jit_code_arg_s);
561 jit_inc_synth_wp(getarg_us, u, v);
562 if (jit_arg_reg_p(v->u.w))
563 jit_extr_us(u, _R32 + v->u.w);
565 jit_ldxi_us(u, JIT_FP, v->u.w + S_DISP);
570 _jit_getarg_i(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
572 assert_arg_type(v->code, jit_code_arg_i);
573 jit_inc_synth_wp(getarg_i, u, v);
574 if (jit_arg_reg_p(v->u.w))
575 jit_extr_i(u, _R32 + v->u.w);
577 jit_ldxi_i(u, JIT_FP, v->u.w + I_DISP);
582 _jit_getarg_ui(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
584 assert_arg_type(v->code, jit_code_arg_i);
585 jit_inc_synth_wp(getarg_ui, u, v);
586 if (jit_arg_reg_p(v->u.w))
587 jit_extr_ui(u, _R32 + v->u.w);
589 jit_ldxi_ui(u, JIT_FP, v->u.w + I_DISP);
594 _jit_getarg_l(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
596 assert_arg_type(v->code, jit_code_arg_l);
597 jit_inc_synth_wp(getarg_l, u, v);
598 if (jit_arg_reg_p(v->u.w))
599 jit_movr(u, _R32 + v->u.w);
601 jit_ldxi(u, JIT_FP, v->u.w);
606 _jit_putargr(jit_state_t *_jit, jit_int32_t u, jit_node_t *v, jit_code_t code)
608 assert_putarg_type(code, v->code);
609 jit_code_inc_synth_wp(code, u, v);
610 if (jit_arg_reg_p(v->u.w))
611 jit_movr(_R32 + v->u.w, u);
613 jit_stxi(v->u.w, JIT_FP, u);
618 _jit_putargi(jit_state_t *_jit, jit_word_t u, jit_node_t *v, jit_code_t code)
621 assert_putarg_type(code, v->code);
622 jit_code_inc_synth_wp(code, u, v);
623 if (jit_arg_reg_p(v->u.w))
624 jit_movi(_R32 + v->u.w, u);
626 regno = jit_get_reg(jit_class_gpr);
628 jit_stxi(v->u.w, JIT_FP, regno);
629 jit_unget_reg(regno);
635 _jit_getarg_f(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
637 assert(v->code == jit_code_arg_f);
638 jit_inc_synth_wp(getarg_f, u, v);
639 if (jit_arg_reg_p(v->u.w))
640 jit_movr_w_f(u, _OUT0 + v->u.w);
641 else if (jit_arg_reg_p(v->u.w - 8))
642 jit_movr_f(u, _F8 + (v->u.w - 8));
644 jit_ldxi_f(u, JIT_FP, v->u.w + F_DISP);
649 _jit_putargr_f(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
651 assert(v->code == jit_code_arg_f);
652 jit_inc_synth_wp(putargr_f, u, v);
653 if (jit_arg_reg_p(v->u.w))
654 jit_movr_f_w(_OUT0 + v->u.w, u);
655 else if (jit_arg_reg_p(v->u.w - 8))
656 jit_movr_f(_F8 + (v->u.w - 8), u);
658 jit_stxi_f(v->u.w, JIT_FP, u + F_DISP);
663 _jit_putargi_f(jit_state_t *_jit, jit_float32_t u, jit_node_t *v)
666 assert(v->code == jit_code_arg_f);
667 jit_inc_synth_fp(putargi_f, u, v);
668 if (jit_arg_reg_p(v->u.w))
669 jit_movi_f_w(_OUT0 + v->u.w, u);
670 else if (jit_arg_reg_p(v->u.w - 8))
671 jit_movi_f(_F8 + (v->u.w - 8), u);
673 regno = jit_get_reg(jit_class_fpr);
674 jit_movi_f(regno, u);
675 jit_stxi_f(v->u.w, JIT_FP, regno + F_DISP);
676 jit_unget_reg(regno);
682 _jit_getarg_d(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
684 assert(v->code == jit_code_arg_d);
685 jit_inc_synth_wp(getarg_d, u, v);
686 if (jit_arg_reg_p(v->u.w))
687 jit_movr_w_d(u, _OUT0 + v->u.w);
688 else if (jit_arg_reg_p(v->u.w - 8))
689 jit_movr_d(u, _F8 + (v->u.w - 8));
691 jit_ldxi_d(u, JIT_FP, v->u.w);
696 _jit_putargr_d(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
698 assert(v->code == jit_code_arg_d);
699 jit_inc_synth_wp(putargr_d, u, v);
700 if (jit_arg_reg_p(v->u.w))
701 jit_movr_d_w(_OUT0 + v->u.w, u);
702 else if (jit_arg_reg_p(v->u.w - 8))
703 jit_movr_d(_F8 + (v->u.w - 8), u);
705 jit_stxi_d(v->u.w, JIT_FP, u);
710 _jit_putargi_d(jit_state_t *_jit, jit_float64_t u, jit_node_t *v)
713 assert(v->code == jit_code_arg_d);
714 jit_inc_synth_dp(putargi_d, u, v);
715 if (jit_arg_reg_p(v->u.w))
716 jit_movi_d_w(_OUT0 + v->u.w, u);
717 else if (jit_arg_reg_p(v->u.w - 8))
718 jit_movi_d(_F8 + (v->u.w - 8), u);
720 regno = jit_get_reg(jit_class_fpr);
721 jit_movi_d(regno, u);
722 jit_stxi_d(v->u.w, JIT_FP, regno);
723 jit_unget_reg(regno);
729 _jit_pushargr(jit_state_t *_jit, jit_int32_t u, jit_code_t code)
731 assert(_jitc->function);
732 jit_code_inc_synth_w(code, u);
734 if (jit_arg_reg_p(_jitc->function->call.argi)) {
735 jit_movr(_OUT0 + _jitc->function->call.argi, u);
736 ++_jitc->function->call.argi;
739 jit_stxi(_jitc->function->call.size + params_offset, JIT_SP, u);
740 _jitc->function->call.size += sizeof(jit_word_t);
746 _jit_pushargi(jit_state_t *_jit, jit_word_t u, jit_code_t code)
749 assert(_jitc->function);
750 jit_code_inc_synth_w(code, u);
752 if (jit_arg_reg_p(_jitc->function->call.argi)) {
753 jit_movi(_OUT0 + _jitc->function->call.argi, u);
754 ++_jitc->function->call.argi;
757 regno = jit_get_reg(jit_class_gpr);
759 jit_stxi(_jitc->function->call.size + params_offset, JIT_SP, regno);
760 _jitc->function->call.size += sizeof(jit_word_t);
761 jit_unget_reg(regno);
767 _jit_pushargr_f(jit_state_t *_jit, jit_int32_t u)
769 assert(_jitc->function);
770 jit_inc_synth_w(pushargr_f, u);
772 if (jit_arg_reg_p(_jitc->function->call.argi)) {
773 if (!(_jitc->function->call.call & jit_call_varargs)) {
774 jit_movr_f(_F8 + _jitc->function->call.argf, u);
775 ++_jitc->function->call.argf;
778 jit_movr_f_w(_OUT0 + _jitc->function->call.argi, u);
779 ++_jitc->function->call.argi;
782 jit_stxi_f(_jitc->function->call.size + params_offset + F_DISP,
784 _jitc->function->call.size += sizeof(jit_word_t);
790 _jit_pushargi_f(jit_state_t *_jit, jit_float32_t u)
793 assert(_jitc->function);
794 jit_inc_synth_f(pushargi_f, u);
796 if (jit_arg_reg_p(_jitc->function->call.argi)) {
797 if (!(_jitc->function->call.call & jit_call_varargs)) {
798 jit_movi_f(_F8 + _jitc->function->call.argf, u);
799 ++_jitc->function->call.argf;
802 jit_movi_f_w(_OUT0 + _jitc->function->call.argi, u);
803 ++_jitc->function->call.argi;
806 regno = jit_get_reg(jit_class_fpr);
807 jit_movi_f(regno, u);
808 jit_stxi_f(_jitc->function->call.size + params_offset + F_DISP,
810 _jitc->function->call.size += sizeof(jit_word_t);
811 jit_unget_reg(regno);
817 _jit_pushargr_d(jit_state_t *_jit, jit_int32_t u)
819 assert(_jitc->function);
820 jit_inc_synth_w(pushargr_d, u);
822 if (jit_arg_reg_p(_jitc->function->call.argi)) {
823 if (!(_jitc->function->call.call & jit_call_varargs)) {
824 jit_movr_d(_F8 + _jitc->function->call.argf, u);
825 ++_jitc->function->call.argf;
828 jit_movr_d_w(_OUT0 + _jitc->function->call.argi, u);
829 ++_jitc->function->call.argi;
832 jit_stxi_d(_jitc->function->call.size + params_offset, JIT_SP, u);
833 _jitc->function->call.size += sizeof(jit_float64_t);
839 _jit_pushargi_d(jit_state_t *_jit, jit_float64_t u)
842 assert(_jitc->function);
843 jit_inc_synth_d(pushargi_d, u);
845 if (jit_arg_reg_p(_jitc->function->call.argi)) {
846 if (!(_jitc->function->call.call & jit_call_varargs)) {
847 jit_movi_d(_F8 + _jitc->function->call.argf, u);
848 ++_jitc->function->call.argf;
851 jit_movi_d_w(_OUT0 + _jitc->function->call.argi, u);
852 ++_jitc->function->call.argi;
855 regno = jit_get_reg(jit_class_fpr);
856 jit_movi_d(regno, u);
857 jit_stxi_d(_jitc->function->call.size + params_offset, JIT_SP, regno);
858 _jitc->function->call.size += sizeof(jit_float64_t);
859 jit_unget_reg(regno);
865 _jit_regarg_p(jit_state_t *_jit, jit_node_t *node, jit_int32_t regno)
867 /* Argument registers are allocated from the pool of unused registers */
872 _jit_finishr(jit_state_t *_jit, jit_int32_t r0)
875 assert(_jitc->function);
876 jit_inc_synth_w(finishr, r0);
877 if (_jitc->function->self.alen < _jitc->function->call.size)
878 _jitc->function->self.alen = _jitc->function->call.size;
879 call = jit_callr(r0);
880 call->v.w = _jitc->function->call.argi;
881 call->w.w = _jitc->function->call.argf;
882 _jitc->function->call.argi = _jitc->function->call.argf =
883 _jitc->function->call.size = 0;
889 _jit_finishi(jit_state_t *_jit, jit_pointer_t i0)
892 assert(_jitc->function);
893 jit_inc_synth_w(finishi, (jit_word_t)i0);
894 if (_jitc->function->self.alen < _jitc->function->call.size)
895 _jitc->function->self.alen = _jitc->function->call.size;
896 node = jit_calli(i0);
897 node->v.w = _jitc->function->call.argi;
898 node->w.w = _jitc->function->call.argf;
899 _jitc->function->call.argi = _jitc->function->call.argf =
900 _jitc->function->call.size = 0;
907 _jit_retval_c(jit_state_t *_jit, jit_int32_t r0)
909 jit_inc_synth_w(retval_c, r0);
910 jit_extr_c(r0, JIT_RET);
915 _jit_retval_uc(jit_state_t *_jit, jit_int32_t r0)
917 jit_inc_synth_w(retval_uc, r0);
918 jit_extr_uc(r0, JIT_RET);
923 _jit_retval_s(jit_state_t *_jit, jit_int32_t r0)
925 jit_inc_synth_w(retval_s, r0);
926 jit_extr_s(r0, JIT_RET);
931 _jit_retval_us(jit_state_t *_jit, jit_int32_t r0)
933 jit_inc_synth_w(retval_us, r0);
934 jit_extr_us(r0, JIT_RET);
939 _jit_retval_i(jit_state_t *_jit, jit_int32_t r0)
941 jit_inc_synth_w(retval_i, r0);
942 jit_extr_i(r0, JIT_RET);
947 _jit_retval_ui(jit_state_t *_jit, jit_int32_t r0)
949 jit_inc_synth_w(retval_ui, r0);
950 jit_extr_ui(r0, JIT_RET);
955 _jit_retval_l(jit_state_t *_jit, jit_int32_t r0)
957 jit_inc_synth_w(retval_l, r0);
958 jit_movr(r0, JIT_RET);
963 _jit_retval_f(jit_state_t *_jit, jit_int32_t r0)
965 jit_inc_synth_w(retval_l, r0);
966 jit_movr_f(r0, JIT_FRET);
971 _jit_retval_d(jit_state_t *_jit, jit_int32_t r0)
973 jit_inc_synth_w(retval_d, r0);
974 jit_movr_d(r0, JIT_FRET);
979 _emit_code(jit_state_t *_jit)
990 #if DEVEL_DISASSEMBLER
993 jit_int32_t patch_offset;
994 jit_word_t prolog_offset;
996 #if DEVEL_DISASSEMBLER
1000 _jitc->function = NULL;
1002 /* If did resize the code buffer, these were not reset */
1004 jit_regset_set_ui(&_jitc->regs, 0);
1007 jit_reglive_setup();
1011 undo.patch_offset = 0;
1013 #if DEVEL_DISASSEMBLER
1016 undo.prolog_offset = 0;
1017 for (node = _jitc->head; node; node = node->next)
1018 if (node->code != jit_code_label &&
1019 node->code != jit_code_note &&
1020 node->code != jit_code_name)
1022 if (node && (node->code != jit_code_prolog ||
1023 !(_jitc->functions.ptr + node->w.w)->assume_frame)) {
1024 /* code may start with a jump so add an initial function descriptor */
1025 word = _jit->pc.w + 16;
1026 il(word); /* addr */
1029 #define case_rr(name, type) \
1030 case jit_code_##name##r##type: \
1031 name##r##type(rn(node->u.w), rn(node->v.w)); \
1033 #define case_rw(name, type) \
1034 case jit_code_##name##i##type: \
1035 name##i##type(rn(node->u.w), node->v.w); \
1037 #define case_wr(name, type) \
1038 case jit_code_##name##i##type: \
1039 name##i##type(node->u.w, rn(node->v.w)); \
1041 #define case_rrr(name, type) \
1042 case jit_code_##name##r##type: \
1043 name##r##type(rn(node->u.w), \
1044 rn(node->v.w), rn(node->w.w)); \
1046 #define case_rrrr(name, type) \
1047 case jit_code_##name##r##type: \
1048 name##r##type(rn(node->u.q.l), rn(node->u.q.h), \
1049 rn(node->v.w), rn(node->w.w)); \
1051 #define case_rqr(name, type) \
1052 case jit_code_##name##r##type: \
1053 name##r##type(rn(node->u.w), rn(node->v.q.l), \
1054 rn(node->v.q.h), rn(node->w.w)); \
1055 case jit_code_##name##i##type: \
1057 #define case_rrw(name, type) \
1058 case jit_code_##name##i##type: \
1059 name##i##type(rn(node->u.w), rn(node->v.w), node->w.w); \
1061 #define case_rrrw(name, type) \
1062 case jit_code_##name##i##type: \
1063 name##i##type(rn(node->u.q.l), rn(node->u.q.h), \
1064 rn(node->v.w), node->w.w); \
1066 #define case_rrf(name, type, size) \
1067 case jit_code_##name##i##type: \
1068 assert(node->flag & jit_flag_data); \
1069 name##i##type(rn(node->u.w), rn(node->v.w), \
1070 (jit_float##size##_t *)node->w.n->u.w); \
1072 #define case_wrr(name, type) \
1073 case jit_code_##name##i##type: \
1074 name##i##type(node->u.w, rn(node->v.w), rn(node->w.w)); \
1076 #define case_brr(name, type) \
1077 case jit_code_##name##r##type: \
1079 assert(temp->code == jit_code_label || \
1080 temp->code == jit_code_epilog); \
1081 if (temp->flag & jit_flag_patch) \
1082 name##r##type(temp->u.w, rn(node->v.w), \
1085 word = name##r##type(_jit->pc.w, \
1086 rn(node->v.w), rn(node->w.w)); \
1087 patch(word, node); \
1090 #define case_brw(name, type) \
1091 case jit_code_##name##i##type: \
1093 assert(temp->code == jit_code_label || \
1094 temp->code == jit_code_epilog); \
1095 if (temp->flag & jit_flag_patch) \
1096 name##i##type(temp->u.w, \
1097 rn(node->v.w), node->w.w); \
1099 word = name##i##type(_jit->pc.w, \
1100 rn(node->v.w), node->w.w); \
1101 patch(word, node); \
1104 #define case_brf(name, type, size) \
1105 case jit_code_##name##i##type: \
1107 assert(temp->code == jit_code_label || \
1108 temp->code == jit_code_epilog); \
1109 if (temp->flag & jit_flag_patch) \
1110 name##i##type(temp->u.w, rn(node->v.w), \
1111 (jit_float##size##_t *)node->w.n->u.w); \
1113 word = name##i##type(_jit->pc.w, rn(node->v.w), \
1114 (jit_float##size##_t *)node->w.n->u.w); \
1115 patch(word, node); \
1118 for (node = _jitc->head; node; node = node->next) {
1119 if (_jit->pc.uc >= _jitc->code.end)
1122 value = jit_classify(node->code);
1126 #if DEVEL_DISASSEMBLER
1127 /* FIXME DEVEL_DISASSEMBLER should become DISASSEMBLER,
1128 * but a "real" DEVEL_DISASSEMBLER should be required
1129 * to turn the below "#if 0" into "#if 1" */
1130 # if 0 /* Since disassembly outputs 3 instructions at a time,
1131 * make it "#if 1" for more clear debug output. */
1134 node->offset = (jit_uword_t)_jit->pc.w - (jit_uword_t)prevw;
1137 jit_regarg_set(node, value);
1138 switch (node->code) {
1139 case jit_code_align:
1140 assert(!(node->u.w & (node->u.w - 1)));
1147 nop((node->u.w + 7) & ~7);
1149 case jit_code_note: case jit_code_name:
1151 node->u.w = _jit->pc.w;
1153 case jit_code_label:
1155 /* remember label is defined */
1156 node->flag |= jit_flag_patch;
1157 node->u.w = _jit->pc.w;
1180 case_rrrr(qmul, _u);
1181 case_rrrw(qmul, _u);
1192 case_rrrr(qdiv, _u);
1193 case_rrrw(qdiv, _u);
1204 case_rrrr(qlsh, _u);
1205 case_rrrw(qlsh, _u);
1208 case_rrrr(qrsh, _u);
1209 case_rrrw(qrsh, _u);
1214 #define lrotr(r0,r1,r2) fallback_lrotr(r0,r1,r2)
1215 #define lroti(r0,r1,i0) fallback_lroti(r0,r1,i0)
1216 #define rrotr(r0,r1,r2) fallback_rrotr(r0,r1,r2)
1217 #define rroti(r0,r1,i0) fallback_rroti(r0,r1,i0)
1228 #define rbitr(r0, r1) fallback_rbit(r0, r1)
1232 casr(rn(node->u.w), rn(node->v.w),
1233 rn(node->w.q.l), rn(node->w.q.h));
1236 casi(rn(node->u.w), node->v.w,
1237 rn(node->w.q.l), rn(node->w.q.h));
1243 if (node->flag & jit_flag_node) {
1245 if (temp->code == jit_code_data ||
1246 (temp->code == jit_code_label &&
1247 (temp->flag & jit_flag_patch)))
1248 movi(rn(node->u.w), temp->u.w);
1250 assert(temp->code == jit_code_label ||
1251 temp->code == jit_code_epilog);
1252 word = movi_p(rn(node->u.w), node->v.w);
1257 movi(rn(node->u.w), node->v.w);
1262 case_rr(bswap, _us);
1263 case_rr(bswap, _ui);
1264 case_rr(bswap, _ul);
1266 extr(rn(node->u.w), rn(node->v.w), node->w.q.l, node->w.q.h);
1268 case jit_code_extr_u:
1269 extr_u(rn(node->u.w), rn(node->v.w), node->w.q.l, node->w.q.h);
1272 depr(rn(node->u.w), rn(node->v.w), node->w.q.l, node->w.q.h);
1275 depi(rn(node->u.w), node->v.w, node->w.q.l, node->w.q.h);
1283 case_rr(trunc, _f_i);
1284 case_rr(trunc, _d_i);
1285 case_rr(trunc, _f_l);
1286 case_rr(trunc, _d_l);
1335 case jit_code_unldr:
1336 unldr(rn(node->u.w), rn(node->v.w), node->w.w);
1338 case jit_code_unldi:
1339 unldi(rn(node->u.w), node->v.w, node->w.w);
1341 case jit_code_unldr_u:
1342 unldr_u(rn(node->u.w), rn(node->v.w), node->w.w);
1344 case jit_code_unldi_u:
1345 unldi_u(rn(node->u.w), node->v.w, node->w.w);
1363 case jit_code_unstr:
1364 unstr(rn(node->u.w), rn(node->v.w), node->w.w);
1366 case jit_code_unsti:
1367 unsti(node->u.w, rn(node->v.w), node->w.w);
1395 case_brr(boadd, _u);
1396 case_brw(boadd, _u);
1399 case_brr(bxadd, _u);
1400 case_brw(bxadd, _u);
1403 case_brr(bosub, _u);
1404 case_brw(bosub, _u);
1407 case_brr(bxsub, _u);
1408 case_brw(bxsub, _u);
1410 case_rrf(add, _f, 32);
1412 case_rrf(sub, _f, 32);
1413 case_rrf(rsb, _f, 32);
1415 case_rrf(mul, _f, 32);
1417 case_rrf(div, _f, 32);
1430 case jit_code_unldr_x:
1431 unldr_x(rn(node->u.w), rn(node->v.w), node->w.w);
1433 case jit_code_unldi_x:
1434 unldi_x(rn(node->u.w), node->v.w, node->w.w);
1436 case jit_code_unstr_x:
1437 unstr_x(rn(node->u.w), rn(node->v.w), node->w.w);
1439 case jit_code_unsti_x:
1440 unsti_x(node->u.w, rn(node->v.w), node->w.w);
1447 case jit_code_movi_f:
1448 assert(node->flag & jit_flag_data);
1449 movi_f(rn(node->u.w), (jit_float32_t *)node->v.n->u.w);
1453 case_rrf(lt, _f, 32);
1455 case_rrf(le, _f, 32);
1457 case_rrf(eq, _f, 32);
1459 case_rrf(ge, _f, 32);
1461 case_rrf(gt, _f, 32);
1463 case_rrf(ne, _f, 32);
1465 case_rrf(unlt, _f, 32);
1467 case_rrf(unle, _f, 32);
1469 case_rrf(uneq, _f, 32);
1471 case_rrf(unge, _f, 32);
1473 case_rrf(ungt, _f, 32);
1475 case_rrf(ltgt, _f, 32);
1477 case_rrf(ord, _f, 32);
1478 case_rrr(unord, _f);
1479 case_rrf(unord, _f, 32);
1481 case_brf(blt, _f, 32);
1483 case_brf(ble, _f, 32);
1485 case_brf(beq, _f, 32);
1487 case_brf(bge, _f, 32);
1489 case_brf(bgt, _f, 32);
1491 case_brf(bne, _f, 32);
1492 case_brr(bunlt, _f);
1493 case_brf(bunlt, _f, 32);
1494 case_brr(bunle, _f);
1495 case_brf(bunle, _f, 32);
1496 case_brr(buneq, _f);
1497 case_brf(buneq, _f, 32);
1498 case_brr(bunge, _f);
1499 case_brf(bunge, _f, 32);
1500 case_brr(bungt, _f);
1501 case_brf(bungt, _f, 32);
1502 case_brr(bltgt, _f);
1503 case_brf(bltgt, _f, 32);
1505 case_brf(bord, _f, 32);
1506 case_brr(bunord, _f);
1507 case_brf(bunord, _f, 32);
1509 case_rrf(add, _d, 64);
1511 case_rrf(sub, _d, 64);
1512 case_rrf(rsb, _d, 64);
1514 case_rrf(mul, _d, 64);
1516 case_rrf(div, _d, 64);
1534 case jit_code_movi_d:
1535 assert(node->flag & jit_flag_data);
1536 movi_d(rn(node->u.w), (jit_float64_t *)node->v.n->u.w);
1540 case_rrf(lt, _d, 64);
1542 case_rrf(le, _d, 64);
1544 case_rrf(eq, _d, 64);
1546 case_rrf(ge, _d, 64);
1548 case_rrf(gt, _d, 64);
1550 case_rrf(ne, _d, 64);
1552 case_rrf(unlt, _d, 64);
1554 case_rrf(unle, _d, 64);
1556 case_rrf(uneq, _d, 64);
1558 case_rrf(unge, _d, 64);
1560 case_rrf(ungt, _d, 64);
1562 case_rrf(ltgt, _d, 64);
1564 case_rrf(ord, _d, 64);
1565 case_rrr(unord, _d);
1566 case_rrf(unord, _d, 64);
1568 case_brf(blt, _d, 64);
1570 case_brf(ble, _d, 64);
1572 case_brf(beq, _d, 64);
1574 case_brf(bge, _d, 64);
1576 case_brf(bgt, _d, 64);
1578 case_brf(bne, _d, 64);
1579 case_brr(bunlt, _d);
1580 case_brf(bunlt, _d, 64);
1581 case_brr(bunle, _d);
1582 case_brf(bunle, _d, 64);
1583 case_brr(buneq, _d);
1584 case_brf(buneq, _d, 64);
1585 case_brr(bunge, _d);
1586 case_brf(bunge, _d, 64);
1587 case_brr(bungt, _d);
1588 case_brf(bungt, _d, 64);
1589 case_brr(bltgt, _d);
1590 case_brf(bltgt, _d, 64);
1592 case_brf(bord, _d, 64);
1593 case_brr(bunord, _d);
1594 case_brf(bunord, _d, 64);
1596 jmpr(rn(node->u.w));
1599 if (node->flag & jit_flag_node) {
1600 if (_jit->pc.uc == _jit->code.ptr + 16)
1603 assert(temp->code == jit_code_label ||
1604 temp->code == jit_code_epilog);
1605 if (temp->flag & jit_flag_patch)
1608 word = _jit->code.length -
1609 (_jit->pc.uc - _jit->code.ptr);
1610 if (word >= -16777216 && word <= 16777215)
1611 word = jmpi(_jit->pc.w);
1613 word = jmpi_p(_jit->pc.w);
1620 case jit_code_callr:
1621 callr(rn(node->u.w));
1623 case jit_code_calli:
1624 if (node->flag & jit_flag_node) {
1626 assert(temp->code == jit_code_label ||
1627 temp->code == jit_code_epilog);
1628 word = calli_p(temp->u.w);
1629 if (!(temp->flag & jit_flag_patch))
1635 case jit_code_prolog:
1636 jit_regset_set_ui(&_jitc->regs, 0);
1639 _jitc->function = _jitc->functions.ptr + node->w.w;
1641 undo.word = _jit->pc.w;
1642 memcpy(&undo.func, _jitc->function, sizeof(undo.func));
1643 #if DEVEL_DISASSEMBLER
1646 undo.patch_offset = _jitc->patches.offset;
1647 undo.prolog_offset = _jitc->prolog.offset;
1650 if (_jitc->jump && !_jitc->function->assume_frame) {
1651 /* remember prolog to hide offset adjustment for a jump
1652 * to the start of a function, what is expected to be
1653 * a common practice as first jit instruction */
1654 if (_jitc->prolog.offset >= _jitc->prolog.length) {
1655 _jitc->prolog.length += 16;
1656 jit_realloc((jit_pointer_t *)&_jitc->prolog.ptr,
1657 (_jitc->prolog.length - 16) *
1659 _jitc->prolog.length * sizeof(jit_word_t));
1661 _jitc->prolog.ptr[_jitc->prolog.offset++] = _jit->pc.w;
1662 /* function descriptor */
1663 word = _jit->pc.w + 16;
1664 il(word); /* addr */
1669 case jit_code_epilog:
1670 assert(_jitc->function == _jitc->functions.ptr + node->w.w);
1672 for (temp = undo.node->next;
1673 temp != node; temp = temp->next) {
1674 if (temp->code == jit_code_label ||
1675 temp->code == jit_code_epilog)
1676 temp->flag &= ~jit_flag_patch;
1678 temp->flag &= ~jit_flag_patch;
1680 _jit->pc.w = undo.word;
1681 /* undo.func.self.aoff and undo.func.regset should not
1682 * be undone, as they will be further updated, and are
1683 * the reason of the undo. */
1684 undo.func.self.aoff = _jitc->function->frame +
1685 _jitc->function->self.aoff;
1686 jit_regset_set(&undo.func.regset, &_jitc->function->regset);
1687 /* allocar information also does not need to be undone */
1688 undo.func.aoffoff = _jitc->function->aoffoff;
1689 undo.func.allocar = _jitc->function->allocar;
1690 memcpy(_jitc->function, &undo.func, sizeof(undo.func));
1691 #if DEVEL_DISASSEMBLER
1694 _jitc->patches.offset = undo.patch_offset;
1695 _jitc->prolog.offset = undo.prolog_offset;
1697 jit_regset_set_ui(&_jitc->regs, 0);
1699 goto restart_function;
1701 /* remember label is defined */
1703 node->flag |= jit_flag_patch;
1704 node->u.w = _jit->pc.w;
1706 jit_regset_set_ui(&_jitc->regs, 0);
1708 _jitc->function = NULL;
1710 case jit_code_va_start:
1711 vastart(rn(node->u.w));
1713 case jit_code_va_arg:
1714 vaarg(rn(node->u.w), rn(node->v.w));
1716 case jit_code_va_arg_d:
1717 vaarg_d(rn(node->u.w), rn(node->v.w));
1719 case jit_code_live: case jit_code_ellipsis:
1720 case jit_code_va_push:
1721 case jit_code_allocai: case jit_code_allocar:
1722 case jit_code_arg_c: case jit_code_arg_s:
1723 case jit_code_arg_i: case jit_code_arg_l:
1724 case jit_code_arg_f: case jit_code_arg_d:
1725 case jit_code_va_end:
1727 case jit_code_retr_c: case jit_code_reti_c:
1728 case jit_code_retr_uc: case jit_code_reti_uc:
1729 case jit_code_retr_s: case jit_code_reti_s:
1730 case jit_code_retr_us: case jit_code_reti_us:
1731 case jit_code_retr_i: case jit_code_reti_i:
1732 case jit_code_retr_ui: case jit_code_reti_ui:
1733 case jit_code_retr_l: case jit_code_reti_l:
1734 case jit_code_retr_f: case jit_code_reti_f:
1735 case jit_code_retr_d: case jit_code_reti_d:
1736 case jit_code_getarg_c: case jit_code_getarg_uc:
1737 case jit_code_getarg_s: case jit_code_getarg_us:
1738 case jit_code_getarg_i: case jit_code_getarg_ui:
1739 case jit_code_getarg_l:
1740 case jit_code_getarg_f: case jit_code_getarg_d:
1741 case jit_code_putargr_c: case jit_code_putargi_c:
1742 case jit_code_putargr_uc: case jit_code_putargi_uc:
1743 case jit_code_putargr_s: case jit_code_putargi_s:
1744 case jit_code_putargr_us: case jit_code_putargi_us:
1745 case jit_code_putargr_i: case jit_code_putargi_i:
1746 case jit_code_putargr_ui: case jit_code_putargi_ui:
1747 case jit_code_putargr_l: case jit_code_putargi_l:
1748 case jit_code_putargr_f: case jit_code_putargi_f:
1749 case jit_code_putargr_d: case jit_code_putargi_d:
1750 case jit_code_pushargr_c: case jit_code_pushargi_c:
1751 case jit_code_pushargr_uc: case jit_code_pushargi_uc:
1752 case jit_code_pushargr_s: case jit_code_pushargi_s:
1753 case jit_code_pushargr_us: case jit_code_pushargi_us:
1754 case jit_code_pushargr_i: case jit_code_pushargi_i:
1755 case jit_code_pushargr_ui: case jit_code_pushargi_ui:
1756 case jit_code_pushargr_l: case jit_code_pushargi_l:
1757 case jit_code_pushargr_f: case jit_code_pushargi_f:
1758 case jit_code_pushargr_d: case jit_code_pushargi_d:
1759 case jit_code_retval_c: case jit_code_retval_uc:
1760 case jit_code_retval_s: case jit_code_retval_us:
1761 case jit_code_retval_i:
1762 case jit_code_retval_ui: case jit_code_retval_l:
1763 case jit_code_retval_f: case jit_code_retval_d:
1764 case jit_code_prepare:
1765 case jit_code_finishr: case jit_code_finishi:
1766 case jit_code_negi_f: case jit_code_absi_f:
1767 case jit_code_sqrti_f: case jit_code_negi_d:
1768 case jit_code_absi_d: case jit_code_sqrti_d:
1770 case jit_code_movr_w_f:
1771 movr_w_f(rn(node->u.w), rn(node->v.w));
1773 case jit_code_movr_f_w:
1774 movr_f_w(rn(node->u.w), rn(node->v.w));
1776 case jit_code_movr_w_d:
1777 movr_w_d(rn(node->u.w), rn(node->v.w));
1779 case jit_code_movr_d_w:
1780 movr_d_w(rn(node->u.w), rn(node->v.w));
1782 case jit_code_movi_f_w:
1783 assert(node->flag & jit_flag_data);
1784 movi_f_w(rn(node->u.w), *(jit_float32_t *)node->v.n->u.w);
1786 case jit_code_movi_w_f:
1787 movi_w_f(rn(node->u.w), node->v.w);
1789 case jit_code_movi_d_w:
1790 assert(node->flag & jit_flag_data);
1791 movi_d_w(rn(node->u.w), *(jit_float64_t *)node->v.n->u.w);
1793 case jit_code_movi_w_d:
1794 movi_w_d(rn(node->u.w), node->v.w);
1797 negi(rn(node->u.w), node->v.w);
1800 comi(rn(node->u.w), node->v.w);
1802 case jit_code_exti_c:
1803 exti_c(rn(node->u.w), node->v.w);
1805 case jit_code_exti_uc:
1806 exti_uc(rn(node->u.w), node->v.w);
1808 case jit_code_exti_s:
1809 exti_s(rn(node->u.w), node->v.w);
1811 case jit_code_exti_us:
1812 exti_us(rn(node->u.w), node->v.w);
1814 case jit_code_bswapi_us:
1815 bswapi_us(rn(node->u.w), node->v.w);
1817 case jit_code_bswapi_ui:
1818 bswapi_ui(rn(node->u.w), node->v.w);
1820 case jit_code_htoni_us:
1821 htoni_us(rn(node->u.w), node->v.w);
1823 case jit_code_htoni_ui:
1824 htoni_ui(rn(node->u.w), node->v.w);
1826 case jit_code_exti_i:
1827 exti_i(rn(node->u.w), node->v.w);
1829 case jit_code_exti_ui:
1830 exti_ui(rn(node->u.w), node->v.w);
1832 case jit_code_bswapi_ul:
1833 bswapi_ul(rn(node->u.w), node->v.w);
1835 case jit_code_htoni_ul:
1836 htoni_ul(rn(node->u.w), node->v.w);
1839 cloi(rn(node->u.w), node->v.w);
1842 clzi(rn(node->u.w), node->v.w);
1845 ctoi(rn(node->u.w), node->v.w);
1848 ctzi(rn(node->u.w), node->v.w);
1850 case jit_code_rbiti:
1851 rbiti(rn(node->u.w), node->v.w);
1853 case jit_code_popcnti:
1854 popcnti(rn(node->u.w), node->v.w);
1857 exti(rn(node->u.w), node->v.w, node->w.q.l, node->w.q.h);
1859 case jit_code_exti_u:
1860 exti_u(rn(node->u.w), node->v.w, node->w.q.l, node->w.q.h);
1865 if (jit_carry != _NOREG) {
1866 switch (node->code) {
1868 case jit_code_addcr: case jit_code_addci:
1869 case jit_code_addxr: case jit_code_addxi:
1870 case jit_code_subcr: case jit_code_subci:
1871 case jit_code_subxr: case jit_code_subxi:
1874 jit_unget_reg(jit_carry);
1882 jit_regarg_clr(node, value);
1883 if (jit_regset_cmp_ui(&_jitc->regarg, 0) != 0) {
1884 assert(jit_regset_scan1(&_jitc->regarg, 0) == jit_carry);
1885 assert(jit_regset_scan1(&_jitc->regarg, jit_carry + 1) == ULONG_MAX);
1887 assert(_jitc->synth == 0);
1888 /* update register live state */
1905 for (offset = 0; offset < _jitc->patches.offset; offset++) {
1906 node = _jitc->patches.ptr[offset].node;
1907 word = node->code == jit_code_movi ? node->v.n->u.w : node->u.n->u.w;
1908 patch_at(node->code, _jitc->patches.ptr[offset].inst, word);
1911 jit_flush(_jit->code.ptr, _jit->pc.uc);
1913 return (_jit->code.ptr);
1917 # include "jit_ia64-cpu.c"
1918 # include "jit_ia64-fpu.c"
1919 # include "jit_fallback.c"
1923 jit_flush(void *fptr, void *tptr)
1925 #if defined(__GNUC__)
1928 s = sysconf(_SC_PAGE_SIZE);
1929 f = (jit_word_t)fptr & -s;
1930 t = (((jit_word_t)tptr) + s - 1) & -s;
1932 __clear_cache((void *)f, (void *)t);
1934 /* __clear_cache is a noop in (old?) gcc, but cache flush is
1935 * required on a multi processor Linux system. */
1936 for (s = f; s < t; s += 32)
1937 asm volatile("fc %0" :: "r"(s) : "memory");
1942 /* Use r2 that is reserved to not require a jit_get_reg call, also note
1943 * that addil needs a register that first in 2 bits, so, if using a
1944 * register other than r2 must be less than r8 (or check for a smaller
1945 * immediate, i.e. i0 >= -8192 && i0 <= 8191) */
1947 _emit_ldxi(jit_state_t *_jit, jit_gpr_t r0, jit_gpr_t r1, jit_word_t i0)
1949 assert(i0 >= -2097152 && i0 < 2097151);
1950 addi(GR_2, rn(r1), i0);
1955 _emit_stxi(jit_state_t *_jit, jit_word_t i0, jit_gpr_t r0, jit_gpr_t r1)
1957 assert(i0 >= -2097152 && i0 < 2097151);
1958 addi(GR_2, rn(r0), i0);
1963 _emit_ldxi_d(jit_state_t *_jit, jit_fpr_t r0, jit_gpr_t r1, jit_word_t i0)
1965 assert(i0 >= -2097152 && i0 < 2097151);
1966 addi(GR_2, rn(r1), i0);
1967 ldr_d(rn(r0), GR_2);
1971 _emit_stxi_d(jit_state_t *_jit, jit_word_t i0, jit_gpr_t r0, jit_fpr_t r1)
1973 assert(i0 >= -2097152 && i0 < 2097151);
1974 addi(GR_2, rn(r0), i0);
1975 str_d(GR_2, rn(r1));
1979 _patch(jit_state_t *_jit, jit_word_t instr, jit_node_t *node)
1983 assert(node->flag & jit_flag_node);
1984 if (node->code == jit_code_movi)
1985 flag = node->v.n->flag;
1987 flag = node->u.n->flag;
1988 assert(!(flag & jit_flag_patch));
1989 if (_jitc->patches.offset >= _jitc->patches.length) {
1990 jit_realloc((jit_pointer_t *)&_jitc->patches.ptr,
1991 _jitc->patches.length * sizeof(jit_patch_t),
1992 (_jitc->patches.length + 1024) * sizeof(jit_patch_t));
1993 _jitc->patches.length += 1024;
1995 _jitc->patches.ptr[_jitc->patches.offset].inst = instr;
1996 _jitc->patches.ptr[_jitc->patches.offset].node = node;
1997 ++_jitc->patches.offset;