File: sub-spec.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 1,111,420 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (291 lines) | stat: -rw-r--r-- 11,591 bytes parent folder | download | duplicates (2)
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
; X86ISA Library

; Note: The license below is based on the template at:
; http://opensource.org/licenses/BSD-3-Clause

; Copyright (C) 2015, Regents of the University of Texas
; All rights reserved.

; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are
; met:

; o Redistributions of source code must retain the above copyright
;   notice, this list of conditions and the following disclaimer.

; o Redistributions in binary form must reproduce the above copyright
;   notice, this list of conditions and the following disclaimer in the
;   documentation and/or other materials provided with the distribution.

; o Neither the name of the copyright holders nor the names of its
;   contributors may be used to endorse or promote products derived
;   from this software without specific prior written permission.

; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
; HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

; Original Author(s):
; Shilpi Goel         <shigoel@cs.utexas.edu>

(in-package "X86ISA")

;; This book contains the specification of the following instructions:
;; sub  sbb

(include-book "structures" :dir :utils)
(include-book "../rflags-spec"
              :ttags (:undef-flg))

(local (include-book "centaur/bitops/ihs-extensions" :dir :system))

;; ======================================================================
;; SPECIFICATION: SUB
;; ======================================================================

;; The SUB instruction performs integer subtraction. It evaluates the
;; result for both signed and unsigned integer operands and sets the
;; OF and CF flags to indicate an overflow in the signed or unsigned
;; result, respectively. The SF flag indicates the sign of the signed
;; result.

;; (def-gl-thm sub-signed-and-unsigned-check
;;   :hyp (and (n32p x)
;;             (n32p y))
;;   :concl (equal (n32 (- (n32-to-i32 x) (n32-to-i32 y)))
;;                 (n32 (- x y)))
;;   :g-bindings `((x (:g-number ,(gl-int 0 2 33)))
;;                 (y (:g-number ,(gl-int 1 2 33)))))

(define gpr-sub-spec-gen-fn ((operand-size :type (member 1 2 4 8)))
  :verify-guards nil

  (b* ((fn-name (mk-name "GPR-SUB-SPEC-" operand-size))
       (result-nbits (ash operand-size 3))
       (str-nbits (if (eql result-nbits 8) "08" result-nbits))
       (ntoi (mk-name "N" str-nbits "-TO-I" str-nbits))
       (?cf-spec-fn (mk-name "CF-SPEC" result-nbits))
       (pf-spec-fn (mk-name "PF-SPEC" result-nbits))
       (af-spec-fn (mk-name "SUB-AF-SPEC" result-nbits))
       (sf-spec-fn (mk-name "SF-SPEC" result-nbits))
       (of-spec-fn (mk-name "OF-SPEC" result-nbits)))


    `(define ,fn-name
       ((dst          :type (unsigned-byte ,result-nbits))
        (src          :type (unsigned-byte ,result-nbits))
        (input-rflags :type (unsigned-byte 32)))

       :parents (,(mk-name "GPR-ARITH/LOGIC-SPEC-" operand-size))
       :guard-hints (("Goal" :in-theory (e/d* (rflag-RoWs-enables)
                                              ((tau-system)))))
       :inline t
       :no-function t

       (b* ((dst (mbe :logic (n-size ,result-nbits dst)
                      :exec dst))
            (src (mbe :logic (n-size ,result-nbits src)
                      :exec src))
            (input-rflags (mbe :logic (n32 input-rflags)
                               :exec input-rflags))

            (signed-raw-result
             (the (signed-byte ,(1+ result-nbits))
               (- (the (signed-byte ,result-nbits)
                    (,ntoi dst))
                  (the (signed-byte ,result-nbits)
                    (,ntoi src)))))

            (result (the (unsigned-byte ,result-nbits)
                      (n-size ,result-nbits signed-raw-result)))

            (cf (the (unsigned-byte 1) (acl2::bool->bit (< dst src))))
            (pf (the (unsigned-byte 1) (,pf-spec-fn result)))
            (af (the (unsigned-byte 1) (,af-spec-fn dst src)))
            (zf (the (unsigned-byte 1) (zf-spec result)))
            (sf (the (unsigned-byte 1) (,sf-spec-fn result)))
            (of (the (unsigned-byte 1) (,of-spec-fn signed-raw-result)))

            (output-rflags (mbe :logic
                                (change-rflagsBits
                                 input-rflags
                                 :cf cf
                                 :pf pf
                                 :af af
                                 :zf zf
                                 :sf sf
                                 :of of)
                                :exec
                                (the (unsigned-byte 32)
                                  (!rflagsBits->cf
                                   cf
                                   (!rflagsBits->pf
                                    pf
                                    (!rflagsBits->af
                                     af
                                     (!rflagsBits->zf
                                      zf
                                      (!rflagsBits->sf
                                       sf
                                       (!rflagsBits->of
                                        of
                                        input-rflags)))))))))

            (output-rflags (mbe :logic (n32 output-rflags)
                                :exec output-rflags))

            (undefined-flags 0))

         (mv result output-rflags undefined-flags))

       ///

       (defthm-unsigned-byte-p ,(mk-name "N" str-nbits "-MV-NTH-0-" fn-name)
         :bound ,result-nbits
         :concl (mv-nth 0 (,fn-name dst src input-rflags))
         :gen-type t
         :gen-linear t)

       (defthm-unsigned-byte-p ,(mk-name "MV-NTH-1-" fn-name)
         :bound 32
         :concl (mv-nth 1 (,fn-name dst src input-rflags))
         :gen-type t
         :gen-linear t)

       (defthm-unsigned-byte-p ,(mk-name "MV-NTH-2-" fn-name)
         :bound 32
         :concl (mv-nth 2 (,fn-name dst src input-rflags))
         :gen-type t
         :gen-linear t))))

(make-event (gpr-sub-spec-gen-fn 1))
(make-event (gpr-sub-spec-gen-fn 2))
(make-event (gpr-sub-spec-gen-fn 4))
(make-event (gpr-sub-spec-gen-fn 8))

;; ======================================================================
;; SPECIFICATION: SBB
;; ======================================================================

(define gpr-sbb-spec-gen-fn ((operand-size :type (member 1 2 4 8)))
  :verify-guards nil

  (b* ((fn-name (mk-name "GPR-SBB-SPEC-" operand-size))
       (result-nbits (ash operand-size 3))
       (str-nbits (if (eql result-nbits 8) "08" result-nbits))
       (ntoi (mk-name "N" str-nbits "-TO-I" str-nbits))
       (?cf-spec-fn (mk-name "CF-SPEC" result-nbits))
       (pf-spec-fn (mk-name "PF-SPEC" result-nbits))
       (af-spec-fn (mk-name "SBB-AF-SPEC" result-nbits))
       (sf-spec-fn (mk-name "SF-SPEC" result-nbits))
       (of-spec-fn (mk-name "OF-SPEC" result-nbits)))


    `(define ,fn-name
       ((dst          :type (unsigned-byte ,result-nbits))
        (src          :type (unsigned-byte ,result-nbits))
        (input-rflags :type (unsigned-byte 32)))
       :parents (,(mk-name "GPR-ARITH/LOGIC-SPEC-" operand-size))
       :guard-hints (("Goal" :in-theory (e/d* (rflag-RoWs-enables)
                                              ((tau-system)))))
       :inline t
       :no-function t

       (b* ((dst (mbe :logic (n-size ,result-nbits dst)
                      :exec dst))
            (src (mbe :logic (n-size ,result-nbits src)
                      :exec src))
            (input-rflags (mbe :logic (n32 input-rflags)
                               :exec input-rflags))
            (input-cf (the (unsigned-byte 1)
                        (rflagsBits->cf input-rflags)))

            (signed-raw-result
             (the (signed-byte ,(+ 2 result-nbits))
               (- (the (signed-byte ,result-nbits)
                    (,ntoi dst))
                  (the (signed-byte ,(1+ result-nbits))
                    (+ (the (signed-byte ,result-nbits)
                         (,ntoi src))
                       input-cf)))))

            (result (the (unsigned-byte ,result-nbits)
                      (n-size ,result-nbits signed-raw-result)))

            (cf (the (unsigned-byte 1)
                  (acl2::bool->bit
                   (< dst
                      (the (unsigned-byte ,(1+ result-nbits))
                        (+ input-cf src))))))
            (pf (the (unsigned-byte 1) (,pf-spec-fn result)))
            (af (the (unsigned-byte 1) (,af-spec-fn dst src input-cf)))
            (zf (the (unsigned-byte 1) (zf-spec result)))
            (sf (the (unsigned-byte 1) (,sf-spec-fn result)))
            (of (the (unsigned-byte 1) (,of-spec-fn signed-raw-result)))

            (output-rflags (mbe :logic
                                (change-rflagsBits
                                 input-rflags
                                 :cf cf
                                 :pf pf
                                 :af af
                                 :zf zf
                                 :sf sf
                                 :of of)
                                :exec
                                (the (unsigned-byte 32)
                                  (!rflagsBits->cf
                                   cf
                                   (!rflagsBits->pf
                                    pf
                                    (!rflagsBits->af
                                     af
                                     (!rflagsBits->zf
                                      zf
                                      (!rflagsBits->sf
                                       sf
                                       (!rflagsBits->of
                                        of
                                        input-rflags)))))))))

            (output-rflags (mbe :logic (n32 output-rflags)
                                :exec output-rflags))

            (undefined-flags 0))

         (mv result output-rflags undefined-flags))

       ///

       (defthm-unsigned-byte-p ,(mk-name "N" str-nbits "-MV-NTH-0-" fn-name)
         :bound ,result-nbits
         :concl (mv-nth 0 (,fn-name dst src cf))
         :gen-type t
         :gen-linear t)

       (defthm-unsigned-byte-p ,(mk-name "MV-NTH-1-" fn-name)
         :bound 32
         :concl (mv-nth 1 (,fn-name dst src input-rflags))
         :gen-type t
         :gen-linear t)

       (defthm-unsigned-byte-p ,(mk-name "MV-NTH-2-" fn-name)
         :bound 32
         :concl (mv-nth 2 (,fn-name dst src input-rflags))
         :gen-type t
         :gen-linear t))))

(make-event (gpr-sbb-spec-gen-fn 1))
(make-event (gpr-sbb-spec-gen-fn 2))
(make-event (gpr-sbb-spec-gen-fn 4))
(make-event (gpr-sbb-spec-gen-fn 8))

;; ======================================================================