parser: fix `extension static inline` in bits/byteswap.h
[m6w6/ext-psi] / scripts / gen_calc_basic.php
1 <?php
2
3 include __DIR__."/_include.php";
4
5 $ops = [
6 "add" => "+",
7 "sub" => "-",
8 "mul" => "*",
9 "div" => "/",
10 ];
11
12 function t_for_res($t1, $t2) {
13 global $types;
14
15 $p = array_flip(array_keys($types));
16
17 if ($p[$t2] >= 8 || $p[$t1] >= 8) {
18 if ($p[$t2] > $p[$t1]) {
19 return $t2;
20 }
21 return $t1;
22 }
23
24 $u1 = $t1{0} === "U";
25 $s1 = substr($t1, $u1 ? 4 : 3);
26 $u2 = $t2{0} === "U";
27 $s2 = substr($t2, $u2 ? 4 : 3);
28
29 if ($u1 && $u2) {
30 return "UINT".min(64,max($s1*2,$s2*2));
31 } else {
32 return "INT".min(64,max($s1*2,$s2*2));
33 }
34
35 }
36
37 function v_for_res($t1, $t2) {
38 global $types;
39
40 return $types[t_for_res($t1, $t2)];
41 }
42
43 ?>
44
45
46 <?php foreach ($ops as $op_name => $op) : ?>
47 static inline token_t psi_calc_<?=$op_name?>(token_t t1, impl_val *v1, token_t t2, impl_val *v2, impl_val *res)
48 {
49
50 switch (t1) {<?php /*nobr*/ ?>
51 <?php foreach ($types as $t1 => $v1) : ?>
52 <?php if (t_is_special($t1)) : ?>
53
54 #if HAVE_<?=$t1?>
55 <?php endif; ?>
56
57 case PSI_T_<?=$t1?>:
58 switch (t2) {<?php /*nobr*/ ?>
59 <?php foreach ($types as $t2 => $v2) : ?>
60 <?php if (t_is_special($t2)) :?>
61
62 # if HAVE_<?=$t2?>
63 <?php endif; ?>
64
65 case PSI_T_<?=$t2?>:
66 res-><?=v_for_res($t1, $t2)?> = v1-><?=$v1?> <?=$op?> v2-><?=$v2?>;
67 return PSI_T_<?=t_for_res($t1, $t2)?>;<?php /*nobr*/ ?>
68 <?php if (t_is_special($t2)) : ?>
69
70 # endif
71 <?php endif; ?>
72 <?php endforeach; ?>
73
74 default:
75 assert(0);
76 break;
77 }
78 break;<?php /*nobr*/ ?>
79 <?php if (t_is_special($t1)) : ?>
80
81 #endif
82 <?php endif; ?>
83 <?php endforeach; ?>
84
85 default:
86 assert(0);
87 break;
88 }
89
90 return 0;
91 }
92 <?php endforeach; ?>
93
94
95 static inline token_t psi_calc_mod(token_t t1, impl_val *v1, token_t t2, impl_val *v2, impl_val *res)
96 {
97 impl_val i1, i2;
98
99 switch (t1) {<?php /*nobr*/ ?>
100 <?php foreach ($types as $t1 => $v1) : ?>
101 <?php if (t_is_special($t1)) :?>
102
103 #if HAVE_<?=$t1?>
104 <?php endif; ?>
105
106 case PSI_T_<?=$t1?>:
107 i1.i64 = v1-><?=$v1?>;
108 break;
109 <?php if (t_is_special($t1)) : ?>
110
111 #endif
112 <?php endif; ?>
113 <?php endforeach; ?>
114
115 default:
116 assert(0);
117 break;
118 }
119
120 switch (t2) {<?php /*nobr*/ ?>
121 <?php foreach ($types as $t2 => $v2) : ?>
122 <?php if (t_is_special($t2)) :?>
123
124 #if HAVE_<?=$t2?>
125 <?php endif; ?>
126
127 case PSI_T_<?=$t2?>:
128 i2.i64 = v2-><?=$v2?>;
129 break;<?php /*nobr*/ ?>
130 <?php if (t_is_special($t2)) : ?>
131
132 #endif
133 <?php endif; ?>
134 <?php endforeach; ?>
135
136 default:
137 assert(0);
138 break;
139 }
140
141 res->i64 = i1.i64 % i2.i64;
142 return PSI_T_INT64;
143 }