File: mono_rewrites.sail

package info (click to toggle)
sail-ocaml 0.19.1%2Bdfsg5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,008 kB
  • sloc: ml: 75,941; ansic: 8,848; python: 1,342; exp: 560; sh: 474; makefile: 218; cpp: 36
file content (294 lines) | stat: -rw-r--r-- 11,878 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*==========================================================================*/
/*     Sail                                                                 */
/*                                                                          */
/*  Sail and the Sail architecture models here, comprising all files and    */
/*  directories except the ASL-derived Sail code in the aarch64 directory,  */
/*  are subject to the BSD two-clause licence below.                        */
/*                                                                          */
/*  The ASL derived parts of the ARMv8.3 specification in                   */
/*  aarch64/no_vector and aarch64/full are copyright ARM Ltd.               */
/*                                                                          */
/*  Copyright (c) 2013-2021                                                 */
/*    Kathyrn Gray                                                          */
/*    Shaked Flur                                                           */
/*    Stephen Kell                                                          */
/*    Gabriel Kerneis                                                       */
/*    Robert Norton-Wright                                                  */
/*    Christopher Pulte                                                     */
/*    Peter Sewell                                                          */
/*    Alasdair Armstrong                                                    */
/*    Brian Campbell                                                        */
/*    Thomas Bauereiss                                                      */
/*    Anthony Fox                                                           */
/*    Jon French                                                            */
/*    Dominic Mulligan                                                      */
/*    Stephen Kell                                                          */
/*    Mark Wassell                                                          */
/*    Alastair Reid (Arm Ltd)                                               */
/*                                                                          */
/*  All rights reserved.                                                    */
/*                                                                          */
/*  This work was partially supported by EPSRC grant EP/K008528/1 <a        */
/*  href="http://www.cl.cam.ac.uk/users/pes20/rems">REMS: Rigorous          */
/*  Engineering for Mainstream Systems</a>, an ARM iCASE award, EPSRC IAA   */
/*  KTF funding, and donations from Arm.  This project has received         */
/*  funding from the European Research Council (ERC) under the European     */
/*  Union’s Horizon 2020 research and innovation programme (grant           */
/*  agreement No 789108, ELVER).                                            */
/*                                                                          */
/*  This software was developed by SRI International and the University of  */
/*  Cambridge Computer Laboratory (Department of Computer Science and       */
/*  Technology) under DARPA/AFRL contracts FA8650-18-C-7809 ("CIFV")        */
/*  and FA8750-10-C-0237 ("CTSRD").                                         */
/*                                                                          */
/*  SPDX-License-Identifier: BSD-2-Clause                                   */
/*==========================================================================*/

$ifndef _MONO_REWRITES
$define _MONO_REWRITES

/* Definitions for use with the -mono_rewrites option */

$include <arith.sail>
$include <vector_dec.sail>

/* External definitions not in the usual asl prelude */

val extzv = pure {lem: "extz_vec"} : forall 'n 'm, 'm >= 0. (implicit('m), bitvector('n, dec)) -> bitvector('m, dec)
function extzv(m, v) = {
  if m < 'n then truncate(v, m) else sail_zero_extend(v, m)
}

val extsv = pure {lem: "exts_vec"} : forall 'n 'm, 'm >= 0. (implicit('m), bitvector('n, dec)) -> bitvector('m, dec)
function extsv(m, v) = {
  if m < 'n then truncate(v, m) else sail_sign_extend(v, m)
}

/* This is generated internally to deal with case splits which reveal the size
   of a bitvector */
val bitvector_cast_in = pure "zeroExtend" : forall 'n. bits('n) -> bits('n)
val bitvector_cast_out = pure "zeroExtend" : forall 'n. bits('n) -> bits('n)

/* Builtins for the rewrites */
val string_of_bits_subrange = pure "string_of_bits_subrange" : forall 'n. (bits('n), int, int) -> string

/* Definitions for the rewrites */

val is_zero_subrange : forall 'n, 'n >= 0.
  (bits('n), int, int) -> bool

function is_zero_subrange (xs, i, j) = {
  (xs & slice_mask(j, i-j+1)) == extzv([bitzero] : bits(1))
}

val is_zeros_slice : forall 'n, 'n >= 0.
  (bits('n), int, int) -> bool

function is_zeros_slice (xs, i, l) = {
  (xs & slice_mask(i, l)) == extzv([bitzero] : bits(1))
}

val is_ones_subrange : forall 'n, 'n >= 0.
  (bits('n), int, int) -> bool

function is_ones_subrange (xs, i, j) = {
  let m : bits('n) = slice_mask(j,i-j+1) in
  (xs & m) == m
}

val is_ones_slice : forall 'n, 'n >= 0.
  (bits('n), int, int) -> bool

function is_ones_slice (xs, i, j) = {
  let m : bits('n) = slice_mask(i,j) in
  (xs & m) == m
}

val slice_slice_concat : forall 'n 'm 'r, 'n >= 0 & 'm >= 0 & 'r >= 0.
  (implicit('r), bits('n), int, int, bits('m), int, int) -> bits('r)

function slice_slice_concat (r, xs, i, l, ys, i', l') = {
  let xs = sail_shiftright(xs & slice_mask(i,l), i) in
  let ys = sail_shiftright(ys & slice_mask(i',l'), i') in
  sail_shiftleft(extzv(r, xs), l') | extzv(r, ys)
}

val slice_zeros_concat : forall 'n 'p 'q, 'n >= 0 & 'p + 'q >= 0.
  (bits('n), int, atom('p), atom('q)) -> bits('p + 'q)

function slice_zeros_concat (xs, i, l, l') = {
  let xs = sail_shiftright(xs & slice_mask(i,l), i) in
  sail_shiftleft(extzv(l + l', xs), l')
}

val subrange_zeros_concat : forall 'n 'hi 'lo 'q, 'n >= 0 & 'hi - 'lo + 1 + 'q >= 0.
  (bits('n), atom('hi), atom('lo), atom('q)) -> bits('hi - 'lo + 1 + 'q)

function subrange_zeros_concat (xs, hi, lo, l') =
  slice_zeros_concat(xs, lo, hi - lo + 1, l')

/* Assumes initial vectors are of equal size */

val subrange_subrange_eq : forall 'n, 'n >= 0.
  (bits('n), int, int, bits('n), int, int) -> bool

function subrange_subrange_eq (xs, i, j, ys, i', j') = {
  let xs = sail_shiftright(xs & slice_mask(j,i-j+1), j) in
  let ys = sail_shiftright(ys & slice_mask(j',i'-j'+1), j') in
  xs == ys
}

val subrange_subrange_concat : forall 'n 'o 'p 'm 'q 'r 's, 's >= 0 & 'n >= 0 & 'm >= 0.
  (implicit('s), bits('n), atom('o), atom('p), bits('m), atom('q), atom('r)) -> bits('s)

function subrange_subrange_concat (s, xs, i, j, ys, i', j') = {
  let xs = sail_shiftright(xs & slice_mask(j,i-j+1), j) in
  let ys = sail_shiftright(ys & slice_mask(j',i'-j'+1), j') in
  sail_shiftleft(extzv(s, xs), i' - j' + 1) | extzv(s, ys)
}

val place_subrange : forall 'n 'm, 'n >= 0 & 'm >= 0.
  (implicit('m), bits('n), int, int, int) -> bits('m)

function place_subrange(m,xs,i,j,shift) = {
  let xs = sail_shiftright(xs & slice_mask(j,i-j+1), j) in
  sail_shiftleft(extzv(m, xs), shift)
}

val place_slice : forall 'n 'm, 'n >= 0 & 'm >= 0.
  (implicit('m), bits('n), int, int, int) -> bits('m)

function place_slice(m,xs,i,l,shift) = {
  let xs = sail_shiftright(xs & slice_mask(i,l), i) in
  sail_shiftleft(extzv(m, xs), shift)
}

val set_slice_zeros : forall 'n, 'n >= 0.
  (implicit('n), bits('n), int, int) -> bits('n)

function set_slice_zeros(n, xs, i, l) = {
  let ys : bits('n) = slice_mask(n, i, l) in
  xs & not_vec(ys)
}

val set_subrange_zeros : forall 'n, 'n >= 0.
  (implicit('n), bits('n), int, int) -> bits('n)

function set_subrange_zeros(n, xs, hi, lo) =
  set_slice_zeros(n, xs, lo, hi - lo + 1)

val zext_slice : forall 'n 'm, 'n >= 0 & 'm >= 0.
  (implicit('m), bits('n), int, int) -> bits('m)

function zext_slice(m,xs,i,l) = {
  let xs = sail_shiftright(xs & slice_mask(i,l), i) in
  extzv(m, xs)
}

val zext_subrange : forall 'n 'm, 'n >= 0 & 'm >= 0.
  (implicit('m), bits('n), int, int) -> bits('m)

function zext_subrange(m, xs, i, j) = zext_slice(m, xs, j, i - j + 1)

val sext_slice : forall 'n 'm, 'n >= 0 & 'm >= 0.
  (implicit('m), bits('n), int, int) -> bits('m)

function sext_slice(m,xs,i,l) = {
  let xs = sail_arith_shiftright(sail_shiftleft((xs & slice_mask(i,l)), ('n - i - l)), 'n - l) in
  extsv(m, xs)
}

val sext_subrange : forall 'n 'm, 'n >= 0 & 'm >= 0.
  (implicit('m), bits('n), int, int) -> bits('m)

function sext_subrange(m, xs, i, j) = sext_slice(m, xs, j, i - j + 1)

val place_slice_signed : forall 'n 'm, 'n >= 0 & 'm >= 0.
  (implicit('m), bits('n), int, int, int) -> bits('m)

function place_slice_signed(m,xs,i,l,shift) = {
  sail_shiftleft(sext_slice(m, xs, i, l), shift)
}

val place_subrange_signed : forall 'n 'm, 'n >= 0 & 'm >= 0.
  (implicit('m), bits('n), int, int, int) -> bits('m)

function place_subrange_signed(m,xs,i,j,shift) = {
  place_slice_signed(m, xs, j, i-j+1, shift)
}

/* This has different names in the aarch64 prelude (UInt) and the other
   preludes (unsigned).  To avoid variable name clashes, we redeclare it
   here with a suitably awkward name. */
val _builtin_unsigned = pure {
  ocaml: "uint",
  lem: "uint",
  interpreter: "uint",
  c: "sail_unsigned",
  coq: "uint",
  lean: "BitVec.toNat"
} : forall 'n. bits('n) -> {'m, 0 <= 'm < 2 ^ 'n. int('m)}

/* There are different implementation choices for division and remainder, but
   they agree on positive values.  We use this here to give more precise return
   types for unsigned_slice and unsigned_subrange. */

val _builtin_mod_nat = pure {
  smt: "mod",
  ocaml: "modulus",
  lem: "integerMod",
  c: "tmod_int",
  coq: "Z.rem",
  lean: "Nat.mod"
} : forall 'n 'm, 'n >= 0 & 'm >= 0. (int('n), int('m)) -> {'r, 0 <= 'r < 'm. int('r)}

/* Below we need the fact that 2 ^ 'n >= 0, so we axiomatise it in the return
   type of pow2, as SMT solvers tend to have problems with exponentiation. */
val _builtin_pow2 = pure "pow2" : forall 'n, 'n >= 0. int('n) -> {'m, 'm == 2 ^ 'n & 'm >= 0. int('m)}

val unsigned_slice : forall 'n 'l, 'n >= 0 & 'l >= 0.
  (bits('n), int, int('l)) -> {'m, 0 <= 'm < 2 ^ 'l. int('m)}

function unsigned_slice(xs,i,l) = {
  let xs = sail_shiftright(xs & slice_mask(i,l), i) in
  _builtin_mod_nat(_builtin_unsigned(xs), _builtin_pow2(l))
}

val unsigned_subrange : forall 'n 'i 'j, 'n >= 0 & ('i - 'j) >= 0.
  (bits('n), int('i), int('j)) -> {'m, 0 <= 'm < 2 ^ ('i - 'j + 1). int('m)}

function unsigned_subrange(xs,i,j) = {
  let xs = sail_shiftright(xs & slice_mask(j,i-j+1), i) in
  _builtin_mod_nat(_builtin_unsigned(xs), _builtin_pow2(i - j + 1))
}


val zext_ones : forall 'n, 'n >= 0. (implicit('n), int) -> bits('n)

function zext_ones(n, m) = {
  let v : bits('n) = extsv([bitone] : bits(1)) in
  sail_shiftright(v, n - m)
}


val vector_update_subrange_from_subrange : forall 'n1 's1 'e1 'n2 's2 'e2,
    0 <= 'e1 <= 's1 < 'n1 & 0 <= 'e2 <= 's2 < 'n2 & 's1 - 'e1 == 's2 - 'e2.
    (implicit('n1), bits('n1), int('s1), int('e1), bits('n2), int('s2), int('e2)) -> bits('n1)

function vector_update_subrange_from_subrange(n,v1,s1,e1,v2,s2,e2) = {
  let xs = sail_shiftright(v2 & slice_mask(e2,s2-e2+1), e2) in
  let xs = sail_shiftleft(extzv(n, xs), e1) in
  let ys = v1 & not_vec(slice_mask(e1,s1-e1+1)) in
  xs | ys
}

val vector_update_subrange_from_integer_subrange : forall 'n1 's1 'e1 's2 'e2,
    0 <= 'e1 <= 's1 < 'n1 & 0 <= 'e2 <= 's2 & 's1 - 'e1 == 's2 - 'e2.
    (implicit('n1), bits('n1), int('s1), int('e1), int, int('s2), int('e2)) -> bits('n1)

function vector_update_subrange_from_integer_subrange(n1, v1, s1, e1, i, s2, e2) = {
  let v2 : bits('n1) = get_slice_int(n1, i, e2) in
  vector_update_subrange_from_subrange(n1, v1, s1, e1, v2, s2 - e2, 0)
}

$endif