File: catalogue-base.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 (381 lines) | stat: -rw-r--r-- 14,082 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
; x86isa categorized listings of implemented/unimplemented instructions
;
; X86ISA Library
; Copyright (C) 2024 Kestrel Technology, LLC
;
; License: A 3-clause BSD license. See the file books/3BSD-mod.txt.
;
; Author: Sol Swords (sswords@gmail.com)

(in-package "X86ISA")

(include-book "inst-listing")



(local (in-theory (disable inst-list-p-of-maps)))

(define find-insts-named ((names symbol-listp)
                          (inst-list inst-list-p))
  :returns (unimp-insts inst-list-p)
  (b* (((when (atom inst-list))
        nil)
       ((inst inst) (car inst-list))
       ((when (member-symbol-name (if (stringp inst.mnemonic)
                                      inst.mnemonic
                                    (symbol-name inst.mnemonic))
                                  names))
        (cons (inst-fix inst)
              (find-insts-named names (cdr inst-list)))))
    (find-insts-named names (cdr inst-list))))

(define find-insts-named-str ((names string-listp)
                              (inst-list inst-list-p))
  :returns (unimp-insts inst-list-p)
  (b* (((when (atom inst-list))
        nil)
       ((inst inst) (car inst-list))
       ((when (member-equal (if (stringp inst.mnemonic)
                                inst.mnemonic
                              (symbol-name inst.mnemonic))
                            names))
        (cons (inst-fix inst)
              (find-insts-named-str names (cdr inst-list)))))
    (find-insts-named-str names (cdr inst-list))))

(define keep-implemented-insts ((inst-list inst-list-p))
  :returns (impl-insts inst-list-p)
  (if (atom inst-list)
      nil
    (if (inst->fn (car inst-list))
        (cons (inst-fix (car inst-list))
              (keep-implemented-insts (cdr inst-list)))
      (keep-implemented-insts (cdr inst-list)))))

(define keep-unimplemented-insts ((inst-list inst-list-p))
  :returns (unimpl-insts inst-list-p)
  (if (atom inst-list)
      nil
    (if (inst->fn (car inst-list))
        (keep-unimplemented-insts (cdr inst-list))
      (cons (inst-fix (car inst-list))
            (keep-unimplemented-insts (cdr inst-list))))))




(define section-number-< ((x nat-listp)
                          (y nat-listp))
  (cond ((atom y)    nil)
        ((atom x)    t)
        ((< (car x) (car y)) t)
        ((< (car y) (car x)) nil)
        (t (section-number-< (cdr x) (cdr y)))))


(include-book "std/strings/strtok" :dir :system)
(include-book "std/strings/strpos" :dir :system)

(define parse-section-number-aux ((strs string-listp))
  (if (atom strs)
      nil
    (cons (str::strval (car strs))
          (parse-section-number-aux (cdr strs)))))

(define parse-section-number ((str stringp))
  :returns (secnum nat-listp)
  (b* ((lst (parse-section-number-aux (str::strtok str '(#\.)))))
    (if (nat-listp lst)
        lst
      (raise "Bad section number: ~s0" str))))

(define parse-section-heading ((heading stringp))
  :returns (mv (secnum nat-listp)
               (title stringp))
  (b* ((space-idx (str::strpos " " heading))
       ((unless space-idx)
        (mv nil (mbe :logic (if (stringp heading) heading "")
                     :exec heading)))
       (secnum (parse-section-number (subseq heading 0 space-idx)))
       (title (subseq heading (+ 1 space-idx) nil)))
    (mv secnum title)))


(defconst *all-opcode-maps*
  (append *one-byte-opcode-map*
          *two-byte-opcode-map*
          *0f-38-three-byte-opcode-map*
          *0f-3a-three-byte-opcode-map*))

(make-event
 `(define all-opcode-maps ()
    :inline t
    :no-function t
    :returns (insts inst-list-p)
    ',*all-opcode-maps*
    ///
    (in-theory (disable (all-opcode-maps)))))

(define inst-list->mnemonics ((insts inst-list-p))
  :Returns (mnemonics)
  :prepwork ((local (in-theory (disable acl2::member-of-cons member-equal))))
  (if (atom insts)
      nil
    (cons (inst->mnemonic (car insts))
          (inst-list->mnemonics (cdr insts)))))

(define keep-strings (x)
  :returns (strings string-listp)
  (if (atom x)
      nil
    (if (stringp (car x))
        (cons (car x) (keep-strings (cdr x)))
      (keep-strings (cdr x)))))

(make-event
 `(define all-mnemonics ()
    :returns (mnemonics string-listp)
    ',(keep-strings (inst-list->mnemonics (all-opcode-maps)))
    ///
    (in-theory (disable (all-mnemonics)))))

(encapsulate nil
  (local (in-theory (disable inst-list-p acl2::member-of-cons member-equal)))
  (fty::deflist inst-list :elt-type inst :true-listp t))

(fty::deftypes sdm-instruction-table
  (defprod sdm-instruction-table-entry
    ((title stringp)
     (implemented inst-list-p)
     (unimplemented inst-list-p)
     (doc stringp)
     (subsecs sdm-instruction-table))
    :layout :alist
    :measure (+ (* 2 (acl2-count x)) 1))
  (fty::defmap sdm-instruction-table
    :key-type nat-listp
    :val-type sdm-instruction-table-entry
    :true-listp t
    :measure (* 2 (acl2-count x)))
  ///
  (defthm len-of-sdm-instruction-table-fix
    (<= (len (sdm-instruction-table-fix x)) (len x))
    :hints(("Goal" :induct (len x)
            :expand ((sdm-instruction-table-fix x))))
    :rule-classes :linear))



(define sdm-instruction-table-implemented-instructions ((x sdm-instruction-table-p))
  :measure (sdm-instruction-table-count x)
  :returns (impl inst-list-p)
  (b* ((x (sdm-instruction-table-fix x))
       ((when (atom x)) nil)
       ((sdm-instruction-table-entry entry) (cdar x))
       (sub-impl (sdm-instruction-table-implemented-instructions entry.subsecs)))
    (append entry.implemented sub-impl
            (sdm-instruction-table-implemented-instructions (cdr x)))))

(define sdm-instruction-table-unimplemented-instructions ((x sdm-instruction-table-p))
  :measure (sdm-instruction-table-count x)
  :returns (unimpl inst-list-p)
  (b* ((x (sdm-instruction-table-fix x))
       ((when (atom x)) nil)
       ((sdm-instruction-table-entry entry) (cdar x))
       (sub-unimpl (sdm-instruction-table-unimplemented-instructions entry.subsecs)))
    (append entry.unimplemented sub-unimpl
            (sdm-instruction-table-unimplemented-instructions (cdr x)))))

(defconst *def-sdm-instruction-section-verbose* nil)

(define symbol-list->names ((x symbol-listp))
  :returns (names string-listp)
  (if (atom x)
      nil
    (cons (symbol-name (car x))
          (symbol-list->names (cdr x)))))


(defines eval-feature-expr
  (define eval-feature-expr (expr
                             (inst inst-p))
    :measure (acl2-count expr)
    (if (atom expr)
        (and expr
             (or (eq expr t)
                 (and (symbolp expr)
                      (case expr
                        (:vex (opcode->vex (inst->opcode inst)))
                        (:vex-256 (and (opcode->vex (inst->opcode inst))
                                       (member-eq :|256| (opcode->vex (inst->opcode inst)))))
                        (:vex-128 (and (opcode->vex (inst->opcode inst))
                                       (member-eq :|256| (opcode->vex (inst->opcode inst)))))
                        (:evex (opcode->evex (inst->opcode inst)))
                        (:no-vex (b* ((opcode (inst->opcode inst)))
                                   (and (not (opcode->vex opcode))
                                        (not (opcode->evex opcode)))))
                        (:rex-w (eq (opcode->rex (inst->opcode inst)) :w))
                        (t (member-eq expr (opcode->feat (inst->opcode inst))))))
                 (equal expr (inst->mnemonic inst)))
             t)
      (case-match expr
        (('not sub) (not (eval-feature-expr sub inst)))
        ((op . exprlist)
         (if (member-eq op '(and or xor))
             (eval-feature-expr-lst op exprlist inst)
           (raise "bad expr: ~x0~%" expr)))
        (& (raise "bad expr: ~x0~%" expr)))))
  (define eval-feature-expr-lst (op
                                 exprs
                                 (inst inst-p))
    :guard (member-eq op '(and or xor))
    :measure (acl2-count exprs)
    (b* (((when (atom exprs)) (eq op 'and))
         (first (eval-feature-expr (car exprs) inst)))
      (case op
        (and (and first
                  (eval-feature-expr-lst op (cdr exprs) inst)))
        (or (or first
                (eval-feature-expr-lst op (cdr exprs) inst)))
        (t (xor first
                (eval-feature-expr-lst op (cdr exprs) inst)))))))

(define keep-insts-satisfying-feature (expr
                                       (insts inst-list-p))
  :returns (new-insts inst-list-p)
  (b* (((when (atom insts)) nil)
       ((inst inst) (car insts)))
    (if (eval-feature-expr expr inst)
        (cons (inst-fix inst)
              (keep-insts-satisfying-feature expr (cdr insts)))
      (keep-insts-satisfying-feature expr (cdr insts)))))



(define sdm-instruction-pair-p (x)
  (and (consp x)
       (nat-listp (car x))
       (sdm-instruction-table-entry-p (cdr x)))
  ///
  (defthm sdm-instruction-pair-p-when-reqs
    (implies (and (nat-listp (car x))
                  (sdm-instruction-table-entry-p (cdr x)))
             (sdm-instruction-pair-p x))))

(define sdm-instruction-pair-< ((x sdm-instruction-pair-p)
                                (y sdm-instruction-pair-p))
  :prepwork ((local (in-theory (enable sdm-instruction-pair-p))))
  (section-number-< (car X) (car y)))

(encapsulate nil
  (local (in-theory (enable sdm-instruction-pair-<
                            sdm-instruction-pair-p
                            section-number-<)))

  (local (defthm section-number-<-transitive
           (implies (and (section-number-< x y)
                         (section-number-< y z))
                    (section-number-< x z))))

  (local (defthm section-number->=-transitive
           (implies (and (not (section-number-< x y))
                         (not (section-number-< y z)))
                    (not (section-number-< x z)))))

  (local (defthm section-number-<-irreflexive
           (not (section-number-< x x))))

  (local (in-theory (disable section-number-<)))

  (acl2::defsort sdm-instruction-table-sort
    :prefix sdm-instruction-table
    ;; :comparable-listp sdm-instruction-table-p
    :compare< sdm-instruction-pair-<
    :comparablep sdm-instruction-pair-p
    :true-listp t)

  (defthm sdm-instruction-table-list-p-is-sdm-instruction-table-p
    (equal (sdm-instruction-table-list-p x)
           (sdm-instruction-table-p x))
    :hints(("Goal" :in-theory (enable sdm-instruction-table-p
                                      sdm-instruction-table-list-p))))

  (defthm sdm-instruction-table-p-of-insertsort
    (implies (sdm-instruction-table-p x)
             (sdm-instruction-table-p (sdm-instruction-table-insertsort x)))
    :hints (("goal" :use sdm-instruction-table-insertsort-creates-comparable-listp
             :in-theory (disable sdm-instruction-table-insertsort-creates-comparable-listp)))))



(define sdm-instruction-table-gather-subtopics ((prefix nat-listp)
                                        (x sdm-instruction-table-p))
  :returns (mv (prefixed-x sdm-instruction-table-p)
               (rest-x sdm-instruction-table-p))
  :measure (len x)
  :verify-guards nil
  (b* (((when (atom x)) (mv nil nil))
       ((unless (mbt (and (consp (car x))
                          (nat-listp (caar x)))))
        (sdm-instruction-table-gather-subtopics prefix (cdr x)))
       (x1-section (caar x))
       ((unless (acl2::prefixp (acl2::nat-list-fix prefix) x1-section))
        (mv nil (sdm-instruction-table-fix x)))
       ;; first element is still prefixed by the input prefix, continue with the rest
       ;; first gather all the elements that are prefixed by section:
       ((mv x1-subsecs rest-x) (sdm-instruction-table-gather-subtopics x1-section (cdr x)))
       ((sdm-instruction-table-entry entry) (cdar x))
       (new-x1 (cons x1-section (change-sdm-instruction-table-entry entry :subsecs (append entry.subsecs x1-subsecs))))
       ((unless (mbt (< (len rest-x) (len x))))
        (mv (list new-x1) rest-x))
       ((mv rest rest-x) (sdm-instruction-table-gather-subtopics prefix rest-x)))
    (mv (cons new-x1 rest) rest-x))
  ///
  (defret <fn>-rest-x-length
    (<= (len rest-x) (len x))
    :rule-classes :linear)

  (verify-guards sdm-instruction-table-gather-subtopics))

(define sdm-instruction-table-organize ((x sdm-instruction-table-p))
  ;; top level: sort, then gather subtopics
  :returns (organized sdm-instruction-table-p)
  (b* ((sorted (sdm-instruction-table-sort x))
       ((mv gathered &) (sdm-instruction-table-gather-subtopics nil sorted)))
    gathered))




(define def-sdm-instruction-section-fn ((header stringp)
                                        &key
                                        ((mnemonics symbol-listp) 'nil)
                                        ((instructions inst-list-p) 'nil)
                                        (features 't)
                                        (suppress-not-found 'nil)
                                        ((doc stringp) '""))
  (b* (((mv secnum title) (parse-section-heading header))
       (insts (append instructions (find-insts-named mnemonics (all-opcode-maps))))
       (insts (if (eq features t)
                  insts
                (keep-insts-satisfying-feature features insts)))
       (unimpl-insts (keep-unimplemented-insts insts))
       (impl-insts (keep-implemented-insts insts))
       (not-found-insts (set-difference-equal (symbol-list->names mnemonics)
                                              (inst-list->mnemonics insts)))
       (- (and not-found-insts
               (not suppress-not-found)
               (raise "Not found: ~x0~%" not-found-insts)))
       (entry (make-sdm-instruction-table-entry
               :title title
               :implemented impl-insts
               :unimplemented unimpl-insts
               :doc doc))
       (- (and *def-sdm-instruction-section-verbose*
               (cw "Entry: ~x0~%" entry))))
    `(table sdm-instruction-sect ',secnum ',entry)))

(defmacro def-sdm-instruction-section (&rest args)
  `(make-event (def-sdm-instruction-section-fn . ,args)))