TCG variable type checking.
[qemu] / target-alpha / op_helper.c
1 /*
2  *  Alpha emulation cpu micro-operations helpers for qemu.
3  *
4  *  Copyright (c) 2007 Jocelyn Mayer
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "exec.h"
22 #include "host-utils.h"
23 #include "softfloat.h"
24 #include "helper.h"
25
26 void helper_tb_flush (void)
27 {
28     tlb_flush(env, 1);
29 }
30
31 /*****************************************************************************/
32 /* Exceptions processing helpers */
33 void helper_excp (int excp, int error)
34 {
35     env->exception_index = excp;
36     env->error_code = error;
37     cpu_loop_exit();
38 }
39
40 uint64_t helper_amask (uint64_t arg)
41 {
42     switch (env->implver) {
43     case IMPLVER_2106x:
44         /* EV4, EV45, LCA, LCA45 & EV5 */
45         break;
46     case IMPLVER_21164:
47     case IMPLVER_21264:
48     case IMPLVER_21364:
49         arg &= ~env->amask;
50         break;
51     }
52     return arg;
53 }
54
55 uint64_t helper_load_pcc (void)
56 {
57     /* XXX: TODO */
58     return 0;
59 }
60
61 uint64_t helper_load_implver (void)
62 {
63     return env->implver;
64 }
65
66 uint64_t helper_load_fpcr (void)
67 {
68     uint64_t ret = 0;
69 #ifdef CONFIG_SOFTFLOAT
70     ret |= env->fp_status.float_exception_flags << 52;
71     if (env->fp_status.float_exception_flags)
72         ret |= 1ULL << 63;
73     env->ipr[IPR_EXC_SUM] &= ~0x3E:
74     env->ipr[IPR_EXC_SUM] |= env->fp_status.float_exception_flags << 1;
75 #endif
76     switch (env->fp_status.float_rounding_mode) {
77     case float_round_nearest_even:
78         ret |= 2ULL << 58;
79         break;
80     case float_round_down:
81         ret |= 1ULL << 58;
82         break;
83     case float_round_up:
84         ret |= 3ULL << 58;
85         break;
86     case float_round_to_zero:
87         break;
88     }
89     return ret;
90 }
91
92 void helper_store_fpcr (uint64_t val)
93 {
94 #ifdef CONFIG_SOFTFLOAT
95     set_float_exception_flags((val >> 52) & 0x3F, &FP_STATUS);
96 #endif
97     switch ((val >> 58) & 3) {
98     case 0:
99         set_float_rounding_mode(float_round_to_zero, &FP_STATUS);
100         break;
101     case 1:
102         set_float_rounding_mode(float_round_down, &FP_STATUS);
103         break;
104     case 2:
105         set_float_rounding_mode(float_round_nearest_even, &FP_STATUS);
106         break;
107     case 3:
108         set_float_rounding_mode(float_round_up, &FP_STATUS);
109         break;
110     }
111 }
112
113 spinlock_t intr_cpu_lock = SPIN_LOCK_UNLOCKED;
114
115 uint64_t helper_rs(void)
116 {
117     uint64_t tmp;
118
119     spin_lock(&intr_cpu_lock);
120     tmp = env->intr_flag;
121     env->intr_flag = 1;
122     spin_unlock(&intr_cpu_lock);
123
124     return tmp;
125 }
126
127 uint64_t helper_rc(void)
128 {
129     uint64_t tmp;
130
131     spin_lock(&intr_cpu_lock);
132     tmp = env->intr_flag;
133     env->intr_flag = 0;
134     spin_unlock(&intr_cpu_lock);
135
136     return tmp;
137 }
138
139 uint64_t helper_addqv (uint64_t op1, uint64_t op2)
140 {
141     uint64_t tmp = op1;
142     op1 += op2;
143     if (unlikely((tmp ^ op2 ^ (-1ULL)) & (tmp ^ op1) & (1ULL << 63))) {
144         helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
145     }
146     return op1;
147 }
148
149 uint64_t helper_addlv (uint64_t op1, uint64_t op2)
150 {
151     uint64_t tmp = op1;
152     op1 = (uint32_t)(op1 + op2);
153     if (unlikely((tmp ^ op2 ^ (-1UL)) & (tmp ^ op1) & (1UL << 31))) {
154         helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
155     }
156     return op1;
157 }
158
159 uint64_t helper_subqv (uint64_t op1, uint64_t op2)
160 {
161     uint64_t tmp = op1;
162     op1 -= op2;
163     if (unlikely(((~tmp) ^ op1 ^ (-1ULL)) & ((~tmp) ^ op2) & (1ULL << 63))) {
164         helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
165     }
166     return op1;
167 }
168
169 uint64_t helper_sublv (uint64_t op1, uint64_t op2)
170 {
171     uint64_t tmp = op1;
172     op1 = (uint32_t)(op1 - op2);
173     if (unlikely(((~tmp) ^ op1 ^ (-1UL)) & ((~tmp) ^ op2) & (1UL << 31))) {
174         helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
175     }
176     return op1;
177 }
178
179 uint64_t helper_mullv (uint64_t op1, uint64_t op2)
180 {
181     int64_t res = (int64_t)op1 * (int64_t)op2;
182
183     if (unlikely((int32_t)res != res)) {
184         helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
185     }
186     return (int64_t)((int32_t)res);
187 }
188
189 uint64_t helper_mulqv (uint64_t op1, uint64_t op2)
190 {
191     uint64_t tl, th;
192
193     muls64(&tl, &th, op1, op2);
194     /* If th != 0 && th != -1, then we had an overflow */
195     if (unlikely((th + 1) > 1)) {
196         helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
197     }
198     return tl;
199 }
200
201 uint64_t helper_umulh (uint64_t op1, uint64_t op2)
202 {
203     uint64_t tl, th;
204
205     mulu64(&tl, &th, op1, op2);
206     return th;
207 }
208
209 uint64_t helper_ctpop (uint64_t arg)
210 {
211     return ctpop64(arg);
212 }
213
214 uint64_t helper_ctlz (uint64_t arg)
215 {
216     return clz64(arg);
217 }
218
219 uint64_t helper_cttz (uint64_t arg)
220 {
221     return ctz64(arg);
222 }
223
224 static always_inline uint64_t byte_zap (uint64_t op, uint8_t mskb)
225 {
226     uint64_t mask;
227
228     mask = 0;
229     mask |= ((mskb >> 0) & 1) * 0x00000000000000FFULL;
230     mask |= ((mskb >> 1) & 1) * 0x000000000000FF00ULL;
231     mask |= ((mskb >> 2) & 1) * 0x0000000000FF0000ULL;
232     mask |= ((mskb >> 3) & 1) * 0x00000000FF000000ULL;
233     mask |= ((mskb >> 4) & 1) * 0x000000FF00000000ULL;
234     mask |= ((mskb >> 5) & 1) * 0x0000FF0000000000ULL;
235     mask |= ((mskb >> 6) & 1) * 0x00FF000000000000ULL;
236     mask |= ((mskb >> 7) & 1) * 0xFF00000000000000ULL;
237
238     return op & ~mask;
239 }
240
241 uint64_t helper_mskbl(uint64_t val, uint64_t mask)
242 {
243     return byte_zap(val, 0x01 << (mask & 7));
244 }
245
246 uint64_t helper_insbl(uint64_t val, uint64_t mask)
247 {
248     val <<= (mask & 7) * 8;
249     return byte_zap(val, ~(0x01 << (mask & 7)));
250 }
251
252 uint64_t helper_mskwl(uint64_t val, uint64_t mask)
253 {
254     return byte_zap(val, 0x03 << (mask & 7));
255 }
256
257 uint64_t helper_inswl(uint64_t val, uint64_t mask)
258 {
259     val <<= (mask & 7) * 8;
260     return byte_zap(val, ~(0x03 << (mask & 7)));
261 }
262
263 uint64_t helper_mskll(uint64_t val, uint64_t mask)
264 {
265     return byte_zap(val, 0x0F << (mask & 7));
266 }
267
268 uint64_t helper_insll(uint64_t val, uint64_t mask)
269 {
270     val <<= (mask & 7) * 8;
271     return byte_zap(val, ~(0x0F << (mask & 7)));
272 }
273
274 uint64_t helper_zap(uint64_t val, uint64_t mask)
275 {
276     return byte_zap(val, mask);
277 }
278
279 uint64_t helper_zapnot(uint64_t val, uint64_t mask)
280 {
281     return byte_zap(val, ~mask);
282 }
283
284 uint64_t helper_mskql(uint64_t val, uint64_t mask)
285 {
286     return byte_zap(val, 0xFF << (mask & 7));
287 }
288
289 uint64_t helper_insql(uint64_t val, uint64_t mask)
290 {
291     val <<= (mask & 7) * 8;
292     return byte_zap(val, ~(0xFF << (mask & 7)));
293 }
294
295 uint64_t helper_mskwh(uint64_t val, uint64_t mask)
296 {
297     return byte_zap(val, (0x03 << (mask & 7)) >> 8);
298 }
299
300 uint64_t helper_inswh(uint64_t val, uint64_t mask)
301 {
302     val >>= 64 - ((mask & 7) * 8);
303     return byte_zap(val, ~((0x03 << (mask & 7)) >> 8));
304 }
305
306 uint64_t helper_msklh(uint64_t val, uint64_t mask)
307 {
308     return byte_zap(val, (0x0F << (mask & 7)) >> 8);
309 }
310
311 uint64_t helper_inslh(uint64_t val, uint64_t mask)
312 {
313     val >>= 64 - ((mask & 7) * 8);
314     return byte_zap(val, ~((0x0F << (mask & 7)) >> 8));
315 }
316
317 uint64_t helper_mskqh(uint64_t val, uint64_t mask)
318 {
319     return byte_zap(val, (0xFF << (mask & 7)) >> 8);
320 }
321
322 uint64_t helper_insqh(uint64_t val, uint64_t mask)
323 {
324     val >>= 64 - ((mask & 7) * 8);
325     return byte_zap(val, ~((0xFF << (mask & 7)) >> 8));
326 }
327
328 uint64_t helper_cmpbge (uint64_t op1, uint64_t op2)
329 {
330     uint8_t opa, opb, res;
331     int i;
332
333     res = 0;
334     for (i = 0; i < 8; i++) {
335         opa = op1 >> (i * 8);
336         opb = op2 >> (i * 8);
337         if (opa >= opb)
338             res |= 1 << i;
339     }
340     return res;
341 }
342
343 /* Floating point helpers */
344
345 /* F floating (VAX) */
346 static always_inline uint64_t float32_to_f (float32 fa)
347 {
348     uint32_t a;
349     uint64_t r, exp, mant, sig;
350
351     a = *(uint32_t*)(&fa);
352     sig = ((uint64_t)a & 0x80000000) << 32;
353     exp = (a >> 23) & 0xff;
354     mant = ((uint64_t)a & 0x007fffff) << 29;
355
356     if (exp == 255) {
357         /* NaN or infinity */
358         r = 1; /* VAX dirty zero */
359     } else if (exp == 0) {
360         if (mant == 0) {
361             /* Zero */
362             r = 0;
363         } else {
364             /* Denormalized */
365             r = sig | ((exp + 1) << 52) | mant;
366         }
367     } else {
368         if (exp >= 253) {
369             /* Overflow */
370             r = 1; /* VAX dirty zero */
371         } else {
372             r = sig | ((exp + 2) << 52);
373         }
374     }
375
376     return r;
377 }
378
379 static always_inline float32 f_to_float32 (uint64_t a)
380 {
381     uint32_t r, exp, mant_sig;
382
383     exp = ((a >> 55) & 0x80) | ((a >> 52) & 0x7f);
384     mant_sig = ((a >> 32) & 0x80000000) | ((a >> 29) & 0x007fffff);
385
386     if (unlikely(!exp && mant_sig)) {
387         /* Reserved operands / Dirty zero */
388         helper_excp(EXCP_OPCDEC, 0);
389     }
390
391     if (exp < 3) {
392         /* Underflow */
393         r = 0;
394     } else {
395         r = ((exp - 2) << 23) | mant_sig;
396     }
397
398     return *(float32*)(&a);
399 }
400
401 uint32_t helper_f_to_memory (uint64_t a)
402 {
403     uint32_t r;
404     r =  (a & 0x00001fffe0000000ull) >> 13;
405     r |= (a & 0x07ffe00000000000ull) >> 45;
406     r |= (a & 0xc000000000000000ull) >> 48;
407     return r;
408 }
409
410 uint64_t helper_memory_to_f (uint32_t a)
411 {
412     uint64_t r;
413     r =  ((uint64_t)(a & 0x0000c000)) << 48;
414     r |= ((uint64_t)(a & 0x003fffff)) << 45;
415     r |= ((uint64_t)(a & 0xffff0000)) << 13;
416     if (!(a & 0x00004000))
417         r |= 0x7ll << 59;
418     return r;
419 }
420
421 uint64_t helper_addf (uint64_t a, uint64_t b)
422 {
423     float32 fa, fb, fr;
424
425     fa = f_to_float32(a);
426     fb = f_to_float32(b);
427     fr = float32_add(fa, fb, &FP_STATUS);
428     return float32_to_f(fr);
429 }
430
431 uint64_t helper_subf (uint64_t a, uint64_t b)
432 {
433     float32 fa, fb, fr;
434
435     fa = f_to_float32(a);
436     fb = f_to_float32(b);
437     fr = float32_sub(fa, fb, &FP_STATUS);
438     return float32_to_f(fr);
439 }
440
441 uint64_t helper_mulf (uint64_t a, uint64_t b)
442 {
443     float32 fa, fb, fr;
444
445     fa = f_to_float32(a);
446     fb = f_to_float32(b);
447     fr = float32_mul(fa, fb, &FP_STATUS);
448     return float32_to_f(fr);
449 }
450
451 uint64_t helper_divf (uint64_t a, uint64_t b)
452 {
453     float32 fa, fb, fr;
454
455     fa = f_to_float32(a);
456     fb = f_to_float32(b);
457     fr = float32_div(fa, fb, &FP_STATUS);
458     return float32_to_f(fr);
459 }
460
461 uint64_t helper_sqrtf (uint64_t t)
462 {
463     float32 ft, fr;
464
465     ft = f_to_float32(t);
466     fr = float32_sqrt(ft, &FP_STATUS);
467     return float32_to_f(fr);
468 }
469
470
471 /* G floating (VAX) */
472 static always_inline uint64_t float64_to_g (float64 fa)
473 {
474     uint64_t a, r, exp, mant, sig;
475
476     a = *(uint64_t*)(&fa);
477     sig = a & 0x8000000000000000ull;
478     exp = (a >> 52) & 0x7ff;
479     mant = a & 0x000fffffffffffffull;
480
481     if (exp == 2047) {
482         /* NaN or infinity */
483         r = 1; /* VAX dirty zero */
484     } else if (exp == 0) {
485         if (mant == 0) {
486             /* Zero */
487             r = 0;
488         } else {
489             /* Denormalized */
490             r = sig | ((exp + 1) << 52) | mant;
491         }
492     } else {
493         if (exp >= 2045) {
494             /* Overflow */
495             r = 1; /* VAX dirty zero */
496         } else {
497             r = sig | ((exp + 2) << 52);
498         }
499     }
500
501     return r;
502 }
503
504 static always_inline float64 g_to_float64 (uint64_t a)
505 {
506     uint64_t r, exp, mant_sig;
507
508     exp = (a >> 52) & 0x7ff;
509     mant_sig = a & 0x800fffffffffffffull;
510
511     if (!exp && mant_sig) {
512         /* Reserved operands / Dirty zero */
513         helper_excp(EXCP_OPCDEC, 0);
514     }
515
516     if (exp < 3) {
517         /* Underflow */
518         r = 0;
519     } else {
520         r = ((exp - 2) << 52) | mant_sig;
521     }
522
523     return *(float64*)(&a);
524 }
525
526 uint64_t helper_g_to_memory (uint64_t a)
527 {
528     uint64_t r;
529     r =  (a & 0x000000000000ffffull) << 48;
530     r |= (a & 0x00000000ffff0000ull) << 16;
531     r |= (a & 0x0000ffff00000000ull) >> 16;
532     r |= (a & 0xffff000000000000ull) >> 48;
533     return r;
534 }
535
536 uint64_t helper_memory_to_g (uint64_t a)
537 {
538     uint64_t r;
539     r =  (a & 0x000000000000ffffull) << 48;
540     r |= (a & 0x00000000ffff0000ull) << 16;
541     r |= (a & 0x0000ffff00000000ull) >> 16;
542     r |= (a & 0xffff000000000000ull) >> 48;
543     return r;
544 }
545
546 uint64_t helper_addg (uint64_t a, uint64_t b)
547 {
548     float64 fa, fb, fr;
549
550     fa = g_to_float64(a);
551     fb = g_to_float64(b);
552     fr = float64_add(fa, fb, &FP_STATUS);
553     return float64_to_g(fr);
554 }
555
556 uint64_t helper_subg (uint64_t a, uint64_t b)
557 {
558     float64 fa, fb, fr;
559
560     fa = g_to_float64(a);
561     fb = g_to_float64(b);
562     fr = float64_sub(fa, fb, &FP_STATUS);
563     return float64_to_g(fr);
564 }
565
566 uint64_t helper_mulg (uint64_t a, uint64_t b)
567 {
568     float64 fa, fb, fr;
569
570     fa = g_to_float64(a);
571     fb = g_to_float64(b);
572     fr = float64_mul(fa, fb, &FP_STATUS);
573     return float64_to_g(fr);
574 }
575
576 uint64_t helper_divg (uint64_t a, uint64_t b)
577 {
578     float64 fa, fb, fr;
579
580     fa = g_to_float64(a);
581     fb = g_to_float64(b);
582     fr = float64_div(fa, fb, &FP_STATUS);
583     return float64_to_g(fr);
584 }
585
586 uint64_t helper_sqrtg (uint64_t a)
587 {
588     float64 fa, fr;
589
590     fa = g_to_float64(a);
591     fr = float64_sqrt(fa, &FP_STATUS);
592     return float64_to_g(fr);
593 }
594
595
596 /* S floating (single) */
597 static always_inline uint64_t float32_to_s (float32 fa)
598 {
599     uint32_t a;
600     uint64_t r;
601
602     a = *(uint32_t*)(&fa);
603
604     r = (((uint64_t)(a & 0xc0000000)) << 32) | (((uint64_t)(a & 0x3fffffff)) << 29);
605     if (((a & 0x7f800000) != 0x7f800000) && (!(a & 0x40000000)))
606         r |= 0x7ll << 59;
607     return r;
608 }
609
610 static always_inline float32 s_to_float32 (uint64_t a)
611 {
612     uint32_t r = ((a >> 32) & 0xc0000000) | ((a >> 29) & 0x3fffffff);
613     return *(float32*)(&r);
614 }
615
616 uint32_t helper_s_to_memory (uint64_t a)
617 {
618     /* Memory format is the same as float32 */
619     float32 fa = s_to_float32(a);
620     return *(uint32_t*)(&fa);
621 }
622
623 uint64_t helper_memory_to_s (uint32_t a)
624 {
625     /* Memory format is the same as float32 */
626     return float32_to_s(*(float32*)(&a));
627 }
628
629 uint64_t helper_adds (uint64_t a, uint64_t b)
630 {
631     float32 fa, fb, fr;
632
633     fa = s_to_float32(a);
634     fb = s_to_float32(b);
635     fr = float32_add(fa, fb, &FP_STATUS);
636     return float32_to_s(fr);
637 }
638
639 uint64_t helper_subs (uint64_t a, uint64_t b)
640 {
641     float32 fa, fb, fr;
642
643     fa = s_to_float32(a);
644     fb = s_to_float32(b);
645     fr = float32_sub(fa, fb, &FP_STATUS);
646     return float32_to_s(fr);
647 }
648
649 uint64_t helper_muls (uint64_t a, uint64_t b)
650 {
651     float32 fa, fb, fr;
652
653     fa = s_to_float32(a);
654     fb = s_to_float32(b);
655     fr = float32_mul(fa, fb, &FP_STATUS);
656     return float32_to_s(fr);
657 }
658
659 uint64_t helper_divs (uint64_t a, uint64_t b)
660 {
661     float32 fa, fb, fr;
662
663     fa = s_to_float32(a);
664     fb = s_to_float32(b);
665     fr = float32_div(fa, fb, &FP_STATUS);
666     return float32_to_s(fr);
667 }
668
669 uint64_t helper_sqrts (uint64_t a)
670 {
671     float32 fa, fr;
672
673     fa = s_to_float32(a);
674     fr = float32_sqrt(fa, &FP_STATUS);
675     return float32_to_s(fr);
676 }
677
678
679 /* T floating (double) */
680 static always_inline float64 t_to_float64 (uint64_t a)
681 {
682     /* Memory format is the same as float64 */
683     return *(float64*)(&a);
684 }
685
686 static always_inline uint64_t float64_to_t (float64 fa)
687 {
688     /* Memory format is the same as float64 */
689     return *(uint64*)(&fa);
690 }
691
692 uint64_t helper_addt (uint64_t a, uint64_t b)
693 {
694     float64 fa, fb, fr;
695
696     fa = t_to_float64(a);
697     fb = t_to_float64(b);
698     fr = float64_add(fa, fb, &FP_STATUS);
699     return float64_to_t(fr);
700 }
701
702 uint64_t helper_subt (uint64_t a, uint64_t b)
703 {
704     float64 fa, fb, fr;
705
706     fa = t_to_float64(a);
707     fb = t_to_float64(b);
708     fr = float64_sub(fa, fb, &FP_STATUS);
709     return float64_to_t(fr);
710 }
711
712 uint64_t helper_mult (uint64_t a, uint64_t b)
713 {
714     float64 fa, fb, fr;
715
716     fa = t_to_float64(a);
717     fb = t_to_float64(b);
718     fr = float64_mul(fa, fb, &FP_STATUS);
719     return float64_to_t(fr);
720 }
721
722 uint64_t helper_divt (uint64_t a, uint64_t b)
723 {
724     float64 fa, fb, fr;
725
726     fa = t_to_float64(a);
727     fb = t_to_float64(b);
728     fr = float64_div(fa, fb, &FP_STATUS);
729     return float64_to_t(fr);
730 }
731
732 uint64_t helper_sqrtt (uint64_t a)
733 {
734     float64 fa, fr;
735
736     fa = t_to_float64(a);
737     fr = float64_sqrt(fa, &FP_STATUS);
738     return float64_to_t(fr);
739 }
740
741
742 /* Sign copy */
743 uint64_t helper_cpys(uint64_t a, uint64_t b)
744 {
745     return (a & 0x8000000000000000ULL) | (b & ~0x8000000000000000ULL);
746 }
747
748 uint64_t helper_cpysn(uint64_t a, uint64_t b)
749 {
750     return ((~a) & 0x8000000000000000ULL) | (b & ~0x8000000000000000ULL);
751 }
752
753 uint64_t helper_cpyse(uint64_t a, uint64_t b)
754 {
755     return (a & 0xFFF0000000000000ULL) | (b & ~0xFFF0000000000000ULL);
756 }
757
758
759 /* Comparisons */
760 uint64_t helper_cmptun (uint64_t a, uint64_t b)
761 {
762     float64 fa, fb;
763
764     fa = t_to_float64(a);
765     fb = t_to_float64(b);
766
767     if (float64_is_nan(fa) || float64_is_nan(fb))
768         return 0x4000000000000000ULL;
769     else
770         return 0;
771 }
772
773 uint64_t helper_cmpteq(uint64_t a, uint64_t b)
774 {
775     float64 fa, fb;
776
777     fa = t_to_float64(a);
778     fb = t_to_float64(b);
779
780     if (float64_eq(fa, fb, &FP_STATUS))
781         return 0x4000000000000000ULL;
782     else
783         return 0;
784 }
785
786 uint64_t helper_cmptle(uint64_t a, uint64_t b)
787 {
788     float64 fa, fb;
789
790     fa = t_to_float64(a);
791     fb = t_to_float64(b);
792
793     if (float64_le(fa, fb, &FP_STATUS))
794         return 0x4000000000000000ULL;
795     else
796         return 0;
797 }
798
799 uint64_t helper_cmptlt(uint64_t a, uint64_t b)
800 {
801     float64 fa, fb;
802
803     fa = t_to_float64(a);
804     fb = t_to_float64(b);
805
806     if (float64_lt(fa, fb, &FP_STATUS))
807         return 0x4000000000000000ULL;
808     else
809         return 0;
810 }
811
812 uint64_t helper_cmpgeq(uint64_t a, uint64_t b)
813 {
814     float64 fa, fb;
815
816     fa = g_to_float64(a);
817     fb = g_to_float64(b);
818
819     if (float64_eq(fa, fb, &FP_STATUS))
820         return 0x4000000000000000ULL;
821     else
822         return 0;
823 }
824
825 uint64_t helper_cmpgle(uint64_t a, uint64_t b)
826 {
827     float64 fa, fb;
828
829     fa = g_to_float64(a);
830     fb = g_to_float64(b);
831
832     if (float64_le(fa, fb, &FP_STATUS))
833         return 0x4000000000000000ULL;
834     else
835         return 0;
836 }
837
838 uint64_t helper_cmpglt(uint64_t a, uint64_t b)
839 {
840     float64 fa, fb;
841
842     fa = g_to_float64(a);
843     fb = g_to_float64(b);
844
845     if (float64_lt(fa, fb, &FP_STATUS))
846         return 0x4000000000000000ULL;
847     else
848         return 0;
849 }
850
851 uint64_t helper_cmpfeq (uint64_t a)
852 {
853     return !(a & 0x7FFFFFFFFFFFFFFFULL);
854 }
855
856 uint64_t helper_cmpfne (uint64_t a)
857 {
858     return (a & 0x7FFFFFFFFFFFFFFFULL);
859 }
860
861 uint64_t helper_cmpflt (uint64_t a)
862 {
863     return (a & 0x8000000000000000ULL) && (a & 0x7FFFFFFFFFFFFFFFULL);
864 }
865
866 uint64_t helper_cmpfle (uint64_t a)
867 {
868     return (a & 0x8000000000000000ULL) || !(a & 0x7FFFFFFFFFFFFFFFULL);
869 }
870
871 uint64_t helper_cmpfgt (uint64_t a)
872 {
873     return !(a & 0x8000000000000000ULL) && (a & 0x7FFFFFFFFFFFFFFFULL);
874 }
875
876 uint64_t helper_cmpfge (uint64_t a)
877 {
878     return !(a & 0x8000000000000000ULL) || !(a & 0x7FFFFFFFFFFFFFFFULL);
879 }
880
881
882 /* Floating point format conversion */
883 uint64_t helper_cvtts (uint64_t a)
884 {
885     float64 fa;
886     float32 fr;
887
888     fa = t_to_float64(a);
889     fr = float64_to_float32(fa, &FP_STATUS);
890     return float32_to_s(fr);
891 }
892
893 uint64_t helper_cvtst (uint64_t a)
894 {
895     float32 fa;
896     float64 fr;
897
898     fa = s_to_float32(a);
899     fr = float32_to_float64(fa, &FP_STATUS);
900     return float64_to_t(fr);
901 }
902
903 uint64_t helper_cvtqs (uint64_t a)
904 {
905     float32 fr = int64_to_float32(a, &FP_STATUS);
906     return float32_to_s(fr);
907 }
908
909 uint64_t helper_cvttq (uint64_t a)
910 {
911     float64 fa = t_to_float64(a);
912     return float64_to_int64_round_to_zero(fa, &FP_STATUS);
913 }
914
915 uint64_t helper_cvtqt (uint64_t a)
916 {
917     float64 fr = int64_to_float64(a, &FP_STATUS);
918     return float64_to_t(fr);
919 }
920
921 uint64_t helper_cvtqf (uint64_t a)
922 {
923     float32 fr = int64_to_float32(a, &FP_STATUS);
924     return float32_to_f(fr);
925 }
926
927 uint64_t helper_cvtgf (uint64_t a)
928 {
929     float64 fa;
930     float32 fr;
931
932     fa = g_to_float64(a);
933     fr = float64_to_float32(fa, &FP_STATUS);
934     return float32_to_f(fr);
935 }
936
937 uint64_t helper_cvtgq (uint64_t a)
938 {
939     float64 fa = g_to_float64(a);
940     return float64_to_int64_round_to_zero(fa, &FP_STATUS);
941 }
942
943 uint64_t helper_cvtqg (uint64_t a)
944 {
945     float64 fr;
946     fr = int64_to_float64(a, &FP_STATUS);
947     return float64_to_g(fr);
948 }
949
950 uint64_t helper_cvtlq (uint64_t a)
951 {
952     return (int64_t)((int32_t)((a >> 32) | ((a >> 29) & 0x3FFFFFFF)));
953 }
954
955 static always_inline uint64_t __helper_cvtql (uint64_t a, int s, int v)
956 {
957     uint64_t r;
958
959     r = ((uint64_t)(a & 0xC0000000)) << 32;
960     r |= ((uint64_t)(a & 0x7FFFFFFF)) << 29;
961
962     if (v && (int64_t)((int32_t)r) != (int64_t)r) {
963         helper_excp(EXCP_ARITH, EXCP_ARITH_OVERFLOW);
964     }
965     if (s) {
966         /* TODO */
967     }
968     return r;
969 }
970
971 uint64_t helper_cvtql (uint64_t a)
972 {
973     return __helper_cvtql(a, 0, 0);
974 }
975
976 uint64_t helper_cvtqlv (uint64_t a)
977 {
978     return __helper_cvtql(a, 0, 1);
979 }
980
981 uint64_t helper_cvtqlsv (uint64_t a)
982 {
983     return __helper_cvtql(a, 1, 1);
984 }
985
986 /* PALcode support special instructions */
987 #if !defined (CONFIG_USER_ONLY)
988 void helper_hw_rei (void)
989 {
990     env->pc = env->ipr[IPR_EXC_ADDR] & ~3;
991     env->ipr[IPR_EXC_ADDR] = env->ipr[IPR_EXC_ADDR] & 1;
992     /* XXX: re-enable interrupts and memory mapping */
993 }
994
995 void helper_hw_ret (uint64_t a)
996 {
997     env->pc = a & ~3;
998     env->ipr[IPR_EXC_ADDR] = a & 1;
999     /* XXX: re-enable interrupts and memory mapping */
1000 }
1001
1002 uint64_t helper_mfpr (int iprn, uint64_t val)
1003 {
1004     uint64_t tmp;
1005
1006     if (cpu_alpha_mfpr(env, iprn, &tmp) == 0)
1007         val = tmp;
1008
1009     return val;
1010 }
1011
1012 void helper_mtpr (int iprn, uint64_t val)
1013 {
1014     cpu_alpha_mtpr(env, iprn, val, NULL);
1015 }
1016
1017 void helper_set_alt_mode (void)
1018 {
1019     env->saved_mode = env->ps & 0xC;
1020     env->ps = (env->ps & ~0xC) | (env->ipr[IPR_ALT_MODE] & 0xC);
1021 }
1022
1023 void helper_restore_mode (void)
1024 {
1025     env->ps = (env->ps & ~0xC) | env->saved_mode;
1026 }
1027
1028 #endif
1029
1030 /*****************************************************************************/
1031 /* Softmmu support */
1032 #if !defined (CONFIG_USER_ONLY)
1033
1034 /* XXX: the two following helpers are pure hacks.
1035  *      Hopefully, we emulate the PALcode, then we should never see
1036  *      HW_LD / HW_ST instructions.
1037  */
1038 uint64_t helper_ld_virt_to_phys (uint64_t virtaddr)
1039 {
1040     uint64_t tlb_addr, physaddr;
1041     int index, mmu_idx;
1042     void *retaddr;
1043
1044     mmu_idx = cpu_mmu_index(env);
1045     index = (virtaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1046  redo:
1047     tlb_addr = env->tlb_table[mmu_idx][index].addr_read;
1048     if ((virtaddr & TARGET_PAGE_MASK) ==
1049         (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1050         physaddr = virtaddr + env->tlb_table[mmu_idx][index].addend;
1051     } else {
1052         /* the page is not in the TLB : fill it */
1053         retaddr = GETPC();
1054         tlb_fill(virtaddr, 0, mmu_idx, retaddr);
1055         goto redo;
1056     }
1057     return physaddr;
1058 }
1059
1060 uint64_t helper_st_virt_to_phys (uint64_t virtaddr)
1061 {
1062     uint64_t tlb_addr, physaddr;
1063     int index, mmu_idx;
1064     void *retaddr;
1065
1066     mmu_idx = cpu_mmu_index(env);
1067     index = (virtaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1068  redo:
1069     tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
1070     if ((virtaddr & TARGET_PAGE_MASK) ==
1071         (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
1072         physaddr = virtaddr + env->tlb_table[mmu_idx][index].addend;
1073     } else {
1074         /* the page is not in the TLB : fill it */
1075         retaddr = GETPC();
1076         tlb_fill(virtaddr, 1, mmu_idx, retaddr);
1077         goto redo;
1078     }
1079     return physaddr;
1080 }
1081
1082 void helper_ldl_raw(uint64_t t0, uint64_t t1)
1083 {
1084     ldl_raw(t1, t0);
1085 }
1086
1087 void helper_ldq_raw(uint64_t t0, uint64_t t1)
1088 {
1089     ldq_raw(t1, t0);
1090 }
1091
1092 void helper_ldl_l_raw(uint64_t t0, uint64_t t1)
1093 {
1094     env->lock = t1;
1095     ldl_raw(t1, t0);
1096 }
1097
1098 void helper_ldq_l_raw(uint64_t t0, uint64_t t1)
1099 {
1100     env->lock = t1;
1101     ldl_raw(t1, t0);
1102 }
1103
1104 void helper_ldl_kernel(uint64_t t0, uint64_t t1)
1105 {
1106     ldl_kernel(t1, t0);
1107 }
1108
1109 void helper_ldq_kernel(uint64_t t0, uint64_t t1)
1110 {
1111     ldq_kernel(t1, t0);
1112 }
1113
1114 void helper_ldl_data(uint64_t t0, uint64_t t1)
1115 {
1116     ldl_data(t1, t0);
1117 }
1118
1119 void helper_ldq_data(uint64_t t0, uint64_t t1)
1120 {
1121     ldq_data(t1, t0);
1122 }
1123
1124 void helper_stl_raw(uint64_t t0, uint64_t t1)
1125 {
1126     stl_raw(t1, t0);
1127 }
1128
1129 void helper_stq_raw(uint64_t t0, uint64_t t1)
1130 {
1131     stq_raw(t1, t0);
1132 }
1133
1134 uint64_t helper_stl_c_raw(uint64_t t0, uint64_t t1)
1135 {
1136     uint64_t ret;
1137
1138     if (t1 == env->lock) {
1139         stl_raw(t1, t0);
1140         ret = 0;
1141     } else
1142         ret = 1;
1143
1144     env->lock = 1;
1145
1146     return ret;
1147 }
1148
1149 uint64_t helper_stq_c_raw(uint64_t t0, uint64_t t1)
1150 {
1151     uint64_t ret;
1152
1153     if (t1 == env->lock) {
1154         stq_raw(t1, t0);
1155         ret = 0;
1156     } else
1157         ret = 1;
1158
1159     env->lock = 1;
1160
1161     return ret;
1162 }
1163
1164 #define MMUSUFFIX _mmu
1165
1166 #define SHIFT 0
1167 #include "softmmu_template.h"
1168
1169 #define SHIFT 1
1170 #include "softmmu_template.h"
1171
1172 #define SHIFT 2
1173 #include "softmmu_template.h"
1174
1175 #define SHIFT 3
1176 #include "softmmu_template.h"
1177
1178 /* try to fill the TLB and return an exception if error. If retaddr is
1179    NULL, it means that the function was called in C code (i.e. not
1180    from generated code or from helper.c) */
1181 /* XXX: fix it to restore all registers */
1182 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
1183 {
1184     TranslationBlock *tb;
1185     CPUState *saved_env;
1186     unsigned long pc;
1187     int ret;
1188
1189     /* XXX: hack to restore env in all cases, even if not called from
1190        generated code */
1191     saved_env = env;
1192     env = cpu_single_env;
1193     ret = cpu_alpha_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
1194     if (!likely(ret == 0)) {
1195         if (likely(retaddr)) {
1196             /* now we have a real cpu fault */
1197             pc = (unsigned long)retaddr;
1198             tb = tb_find_pc(pc);
1199             if (likely(tb)) {
1200                 /* the PC is inside the translated code. It means that we have
1201                    a virtual CPU fault */
1202                 cpu_restore_state(tb, env, pc, NULL);
1203             }
1204         }
1205         /* Exception index and error code are already set */
1206         cpu_loop_exit();
1207     }
1208     env = saved_env;
1209 }
1210
1211 #endif