1/*
2 * Copyright (c) 2014, Red Hat Inc. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#include <stdlib.h>
26#include "decode_aarch64.hpp"
27#include "immediate_aarch64.hpp"
28
29// there are at most 2^13 possible logical immediate encodings
30// however, some combinations of immr and imms are invalid
31static const unsigned  LI_TABLE_SIZE = (1 << 13);
32
33static int li_table_entry_count;
34
35// for forward lookup we just use a direct array lookup
36// and assume that the cient has supplied a valid encoding
37// table[encoding] = immediate
38static u_int64_t LITable[LI_TABLE_SIZE];
39
40// for reverse lookup we need a sparse map so we store a table of
41// immediate and encoding pairs sorted by immediate value
42
43struct li_pair {
44  u_int64_t immediate;
45  u_int32_t encoding;
46};
47
48static struct li_pair InverseLITable[LI_TABLE_SIZE];
49
50// comparator to sort entries in the inverse table
51int compare_immediate_pair(const void *i1, const void *i2)
52{
53  struct li_pair *li1 = (struct li_pair *)i1;
54  struct li_pair *li2 = (struct li_pair *)i2;
55  if (li1->immediate < li2->immediate) {
56    return -1;
57  }
58  if (li1->immediate > li2->immediate) {
59    return 1;
60  }
61  return 0;
62}
63
64// helper functions used by expandLogicalImmediate
65
66// for i = 1, ... N result<i-1> = 1 other bits are zero
67static inline u_int64_t ones(int N)
68{
69  return (N == 64 ? (u_int64_t)-1UL : ((1UL << N) - 1));
70}
71
72// result<0> to val<N>
73static inline u_int64_t pickbit(u_int64_t val, int N)
74{
75  return pickbits64(val, N, N);
76}
77
78
79// SPEC bits(M*N) Replicate(bits(M) x, integer N);
80// this is just an educated guess
81
82u_int64_t replicate(u_int64_t bits, int nbits, int count)
83{
84  u_int64_t result = 0;
85  // nbits may be 64 in which case we want mask to be -1
86  u_int64_t mask = ones(nbits);
87  for (int i = 0; i < count ; i++) {
88    result <<= nbits;
89    result |= (bits & mask);
90  }
91  return result;
92}
93
94// this function writes the supplied bimm reference and returns a
95// boolean to indicate success (1) or fail (0) because an illegal
96// encoding must be treated as an UNALLOC instruction
97
98// construct a 32 bit immediate value for a logical immediate operation
99int expandLogicalImmediate(u_int32_t immN, u_int32_t immr,
100                            u_int32_t imms, u_int64_t &bimm)
101{
102  int len;                  // ought to be <= 6
103  u_int32_t levels;         // 6 bits
104  u_int32_t tmask_and;      // 6 bits
105  u_int32_t wmask_and;      // 6 bits
106  u_int32_t tmask_or;       // 6 bits
107  u_int32_t wmask_or;       // 6 bits
108  u_int64_t imm64;          // 64 bits
109  u_int64_t tmask, wmask;   // 64 bits
110  u_int32_t S, R, diff;     // 6 bits?
111
112  if (immN == 1) {
113    len = 6; // looks like 7 given the spec above but this cannot be!
114  } else {
115    len = 0;
116    u_int32_t val = (~imms & 0x3f);
117    for (int i = 5; i > 0; i--) {
118      if (val & (1 << i)) {
119        len = i;
120        break;
121      }
122    }
123    if (len < 1) {
124      return 0;
125    }
126    // for valid inputs leading 1s in immr must be less than leading
127    // zeros in imms
128    int len2 = 0;                   // ought to be < len
129    u_int32_t val2 = (~immr & 0x3f);
130    for (int i = 5; i > 0; i--) {
131      if (!(val2 & (1 << i))) {
132        len2 = i;
133        break;
134      }
135    }
136    if (len2 >= len) {
137      return 0;
138    }
139  }
140
141  levels = (1 << len) - 1;
142
143  if ((imms & levels) == levels) {
144    return 0;
145  }
146
147  S = imms & levels;
148  R = immr & levels;
149
150 // 6 bit arithmetic!
151  diff = S - R;
152  tmask_and = (diff | ~levels) & 0x3f;
153  tmask_or = (diff & levels) & 0x3f;
154  tmask = 0xffffffffffffffffULL;
155
156  for (int i = 0; i < 6; i++) {
157    int nbits = 1 << i;
158    u_int64_t and_bit = pickbit(tmask_and, i);
159    u_int64_t or_bit = pickbit(tmask_or, i);
160    u_int64_t and_bits_sub = replicate(and_bit, 1, nbits);
161    u_int64_t or_bits_sub = replicate(or_bit, 1, nbits);
162    u_int64_t and_bits_top = (and_bits_sub << nbits) | ones(nbits);
163    u_int64_t or_bits_top = (0 << nbits) | or_bits_sub;
164
165    tmask = ((tmask
166              & (replicate(and_bits_top, 2 * nbits, 32 / nbits)))
167             | replicate(or_bits_top, 2 * nbits, 32 / nbits));
168  }
169
170  wmask_and = (immr | ~levels) & 0x3f;
171  wmask_or = (immr & levels) & 0x3f;
172
173  wmask = 0;
174
175  for (int i = 0; i < 6; i++) {
176    int nbits = 1 << i;
177    u_int64_t and_bit = pickbit(wmask_and, i);
178    u_int64_t or_bit = pickbit(wmask_or, i);
179    u_int64_t and_bits_sub = replicate(and_bit, 1, nbits);
180    u_int64_t or_bits_sub = replicate(or_bit, 1, nbits);
181    u_int64_t and_bits_top = (ones(nbits) << nbits) | and_bits_sub;
182    u_int64_t or_bits_top = (or_bits_sub << nbits) | 0;
183
184    wmask = ((wmask
185              & (replicate(and_bits_top, 2 * nbits, 32 / nbits)))
186             | replicate(or_bits_top, 2 * nbits, 32 / nbits));
187  }
188
189  if (diff & (1U << 6)) {
190    imm64 = tmask & wmask;
191  } else {
192    imm64 = tmask | wmask;
193  }
194
195
196  bimm = imm64;
197  return 1;
198}
199
200// constructor to initialise the lookup tables
201
202static void initLITables() __attribute__ ((constructor));
203static void initLITables()
204{
205  li_table_entry_count = 0;
206  for (unsigned index = 0; index < LI_TABLE_SIZE; index++) {
207    u_int32_t N = uimm(index, 12, 12);
208    u_int32_t immr = uimm(index, 11, 6);
209    u_int32_t imms = uimm(index, 5, 0);
210    if (expandLogicalImmediate(N, immr, imms, LITable[index])) {
211      InverseLITable[li_table_entry_count].immediate = LITable[index];
212      InverseLITable[li_table_entry_count].encoding = index;
213      li_table_entry_count++;
214    }
215  }
216  // now sort the inverse table
217  qsort(InverseLITable, li_table_entry_count,
218        sizeof(InverseLITable[0]), compare_immediate_pair);
219}
220
221// public APIs provided for logical immediate lookup and reverse lookup
222
223u_int64_t logical_immediate_for_encoding(u_int32_t encoding)
224{
225  return LITable[encoding];
226}
227
228u_int32_t encoding_for_logical_immediate(u_int64_t immediate)
229{
230  struct li_pair pair;
231  struct li_pair *result;
232
233  pair.immediate = immediate;
234
235  result = (struct li_pair *)
236    bsearch(&pair, InverseLITable, li_table_entry_count,
237            sizeof(InverseLITable[0]), compare_immediate_pair);
238
239  if (result) {
240    return result->encoding;
241  }
242
243  return 0xffffffff;
244}
245
246// floating point immediates are encoded in 8 bits
247// fpimm[7] = sign bit
248// fpimm[6:4] = signed exponent
249// fpimm[3:0] = fraction (assuming leading 1)
250// i.e. F = s * 1.f * 2^(e - b)
251
252u_int64_t fp_immediate_for_encoding(u_int32_t imm8, int is_dp)
253{
254  union {
255    float fpval;
256    double dpval;
257    u_int64_t val;
258  };
259
260  u_int32_t s, e, f;
261  s = (imm8 >> 7 ) & 0x1;
262  e = (imm8 >> 4) & 0x7;
263  f = imm8 & 0xf;
264  // the fp value is s * n/16 * 2r where n is 16+e
265  fpval = (16.0 + f) / 16.0;
266  // n.b. exponent is signed
267  if (e < 4) {
268    int epos = e;
269    for (int i = 0; i <= epos; i++) {
270      fpval *= 2.0;
271    }
272  } else {
273    int eneg = 7 - e;
274    for (int i = 0; i < eneg; i++) {
275      fpval /= 2.0;
276    }
277  }
278
279  if (s) {
280    fpval = -fpval;
281  }
282  if (is_dp) {
283    dpval = (double)fpval;
284  }
285  return val;
286}
287
288u_int32_t encoding_for_fp_immediate(float immediate)
289{
290  // given a float which is of the form
291  //
292  //     s * n/16 * 2r
293  //
294  // where n is 16+f and imm1:s, imm4:f, simm3:r
295  // return the imm8 result [s:r:f]
296  //
297
298  union {
299    float fpval;
300    u_int32_t val;
301  };
302  fpval = immediate;
303  u_int32_t s, r, f, res;
304  // sign bit is 31
305  s = (val >> 31) & 0x1;
306  // exponent is bits 30-23 but we only want the bottom 3 bits
307  // strictly we ought to check that the bits bits 30-25 are
308  // either all 1s or all 0s
309  r = (val >> 23) & 0x7;
310  // fraction is bits 22-0
311  f = (val >> 19) & 0xf;
312  res = (s << 7) | (r << 4) | f;
313  return res;
314}
315
316