File: py86-state.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 (970 lines) | stat: -rw-r--r-- 28,923 bytes parent folder | download | duplicates (6)
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
(in-package "ACL2")

(include-book "std/util/bstar" :dir :system)

;;; From x86-state.lisp

; Increase memory for X86 memory.
(include-book "centaur/misc/memory-mgmt-logic" :dir :system)
(value-triple (set-max-mem (* 6 (expt 2 30))))

; Here we include the GL book to help verify various arithmetic facts.
(local (include-book "centaur/gl/gl" :dir :system))

(defun gl-int (start by count)
  (declare (xargs :guard (and (natp start)
                              (natp by)
                              (natp count))))
  (if (zp count)
      nil
    (cons start
          (gl-int (+ by start) by (1- count)))))

;;; From y86.lisp

(include-book "../common/misc-events")
(include-book "../common/operations")
(include-book "../common/constants")

;;;

(defun lookup (i lst default)
  (declare (xargs :guard (and (eqlablep i)
                              (eqlable-alistp lst))))
  (let ((pair (hons-get i lst)))
    (if pair
        (cdr pair)

; Use value of :initially if we don't find the pair.

      default)))

(defun rgfp (x)
  (declare (xargs :guard t :verify-guards t))
  (if (atom x)
      (equal x nil)
    (and (unsigned-byte-p 32 (car x))
         (rgfp (cdr x)))))

(defun nat-listp (l)
  (declare (xargs :guard t))
  (cond ((atom l)
         (eq l nil))
        (t (and (natp (car l))
                (nat-listp (cdr l))))))

(defthm nat-listp-forward-to-integer-listp
  (implies (nat-listp x)
           (integer-listp x))
  :rule-classes :forward-chaining)

(defthm rgfp-forward-to-nat-listp
  (implies (rgfp x)
           (nat-listp x))
  :rule-classes :forward-chaining)

(defun eipp (x)
       (declare (xargs :guard t :verify-guards t))
       (unsigned-byte-p 32 x))

(defun flgp (x)
       (declare (xargs :guard t :verify-guards t))
       (unsigned-byte-p 32 x))

(defun memp (x)
  (declare (xargs :guard t :verify-guards t))
  (if (atom x)
      (equal x nil)
    (and (consp (car x))
         (natp (caar x))
         (unsigned-byte-p 32 (cdar x))
         (memp (cdr x)))))

(defthm memp-forward-to-eqlable-alistp
  (implies (memp x)
           (eqlable-alistp x))
  :rule-classes :forward-chaining)

(defun msp (x)
  (declare (xargs :guard t))
  (declare (ignore x))
  t)

(defun x86-32p (x86-32)
  (declare (xargs :guard t :verify-guards t))
  (and (true-listp x86-32)
       (= (length x86-32) 5)
       (rgfp (nth 0 x86-32))
       (equal (len (nth 0 x86-32)) 8)
       (eipp (nth 1 x86-32))
       (flgp (nth 2 x86-32))
       (memp (nth 3 x86-32))
       (msp  (nth 4 x86-32))
       t))

(defun create-x86-32 nil
  (declare (xargs :guard t :verify-guards t))
  (list (make-list 8 :initial-element '0)
        '0
        '0
        nil
        nil))

(defun rgf-length (x86-32)
       (declare (xargs :guard (x86-32p x86-32)
                       :verify-guards t)
                (ignore x86-32))
       8)

(defun resize-rgf (i x86-32)
  (declare (xargs :guard (x86-32p x86-32)
                  :verify-guards t)
           (ignore i))
  (prog2$
   (hard-error
    'resize-rgf
    "the array field corresponding to accessor ~x0 of ~
              stobj ~x1 was not declared :resizable t.  ~
              therefore, it is illegal to resize this array."
    (list (cons #\0 'rgfi)
          (cons #\1 'x86-32)))
   x86-32))

(defun rgfi (i x86-32)
       (declare (xargs :guard (and (x86-32p x86-32)
                                   (integerp i)
                                   (<= 0 i)
                                   (< i (rgf-length x86-32)))
                       :verify-guards t))
       (nth i (nth 0 x86-32)))

(defun !rgfi (i v x86-32)
  (declare (xargs :guard (and (x86-32p x86-32)
                              (integerp i)
                              (<= 0 i)
                              (< i (rgf-length x86-32))
                              (unsigned-byte-p 32 v))
                  :verify-guards t))
  (update-nth-array 0 i v x86-32))

(defun eip (x86-32)
  (declare (xargs :guard (x86-32p x86-32)
                  :verify-guards t))
  (nth 1 x86-32))

(defun !eip (v x86-32)
  (declare (xargs :guard (and (unsigned-byte-p 32 v)
                              (x86-32p x86-32))
                  :verify-guards t))
  (update-nth 1 v x86-32))

(defun flg (x86-32)
  (declare (xargs :guard (x86-32p x86-32)
                  :verify-guards t))
  (nth 2 x86-32))

(defun !flg (v x86-32)
  (declare (xargs :guard (and (unsigned-byte-p 32 v)
                              (x86-32p x86-32))
                  :verify-guards t))
  (update-nth 2 v x86-32))

#||
(defun mem-length (x86-32)
  (declare (xargs :guard (x86-32p x86-32)
                  :verify-guards t)
           (ignore x86-32))
  #x40000000)
||#

(defun resize-mem (i x86-32)
  (declare (xargs :guard (x86-32p x86-32)
                  :verify-guards t)
           (ignore i))
  (prog2$
   (hard-error
    'resize-mem
    "the array field corresponding to accessor ~x0 of ~
              stobj ~x1 was not declared :resizable t.  ~
              therefore, it is illegal to resize this array."
    (list (cons #\0 'memi)
          (cons #\1 'x86-32)))
   x86-32))

(defun memi (i x86-32)
  (declare (xargs :guard (and (x86-32p x86-32)
                              (integerp i)
                              (<= 0 i))
                  :verify-guards t))
  (lookup i
          (nth 3 x86-32)
; Use value of :initially if we don't find the pair:
          0))

(defun !memi (i v x86-32)
  (declare (xargs :guard (and (x86-32p x86-32)
                              (integerp i)
                              (<= 0 i)
                              (unsigned-byte-p 32 v))
                  :verify-guards t))
  (update-nth 3
              (hons-acons i v (nth 3 x86-32))
              x86-32))

(defun ms (x86-32)
  (declare (xargs :guard (x86-32p x86-32)
                  :verify-guards t))
  (nth 4 x86-32))

(defun !ms (v x86-32)
  (declare (xargs :guard (x86-32p x86-32)
                  :verify-guards t))
  (update-nth 4 v x86-32))

(defconst *rgfi* 0)
(defconst *eip* 1)
(defconst *flg* 2)
(defconst *memi* 3)
(defconst *ms* 4)

; Theorems from ../common/x86-state.lisp

; We first deal with the STOBJ read lemmas

; RGF read lemmas

(defthm natp-nth-of-rgf
  (implies (and (rgfp x)
                (integerp i)
                (<= 0 i)
                (< i (len x)))
           (and (integerp (nth i x))
                (<= 0 (nth i x))))
  :rule-classes :type-prescription
  :hints (("Goal" :in-theory (e/d (nth) ()))))

(defthm nth-of-rgf-<=4294967296
  (implies (and (rgfp x)
                (integerp i)
                (<= 0 i)
                (< i (len x)))
           (< (nth i x) 4294967296))
  :rule-classes :linear
  :hints (("Goal" :in-theory (e/d (nth) ()))))

(defthm natp-rgfi
  (implies (and (force (x86-32p x86-32))
                (force (n03p i)))
           (and (integerp (rgfi i x86-32))
                (<= 0 (rgfi i x86-32))))
  :rule-classes :type-prescription)

(defthm rgfi-less-than-expt-2-32
  (implies (and (x86-32p x86-32)
                (n03p i))
           (< (rgfi i x86-32) 4294967296))
  :rule-classes :linear)


; EIP read lemmas

(defthm natp-eip
  (implies (force (x86-32p x86-32))
           (and (integerp (eip x86-32))
                (<= 0 (eip x86-32))))
  :rule-classes :type-prescription)

(defthm eip-less-than-expt-2-32
  (implies (x86-32p x86-32)
           (< (eip x86-32) 4294967296))
  :rule-classes :linear)


; FLG read lemmas

(defthm natp-flg
  (implies (force (x86-32p x86-32))
           (and (integerp (flg x86-32))
                (<= 0 (flg x86-32))))
  :rule-classes :type-prescription)

(defthm flg-less-than-expt-2-32
  (implies (x86-32p x86-32)
           (< (flg x86-32) 4294967296))
  :rule-classes :linear)

; MEM read lemmas

(defthm natp-lookup
  (implies (and (memp x)
                (natp default))
           (and (integerp (lookup i x default))
                (<= 0 (lookup i x default))))
  :rule-classes :type-prescription)

(defthm lookup-is-unsigned-byte-p-32
  (implies (and (memp x)
                (unsigned-byte-p 32 default))
           (< (lookup i x default) #x100000000))
  :rule-classes :linear
  :hints (("Goal" :in-theory (e/d (nth) ()))))

(in-theory (disable lookup))

(defthm natp-memi-when-n30p-addr
  (implies (and (force (x86-32p x86-32))
                (force (n30p addr)))
           (and (integerp (memi addr x86-32))
                (<= 0 (memi addr x86-32))))
  :rule-classes (:rewrite :type-prescription))

(defthm memi-less-than-expt-2-32
  (implies (and (x86-32p x86-32)
                (force (n30p addr)))
           (< (memi addr x86-32) #x100000000))
  :rule-classes :linear)

(encapsulate
 ()

 (local
  (def-gl-thm n32p-ash--2-is-n30p-lemma
    :hyp (n32p x)
    :concl (n30p (ash x -2))
    :g-bindings
    `((x (:g-number ,(gl-int  0  1  33))))))

 (defthm n32p-ash--2-is-n30p
   (implies (n32p x)
            (n30p (ash x -2)))
   :hints (("Goal" :by n32p-ash--2-is-n30p-lemma))
   :rule-classes ((:type-prescription
                   :corollary (implies (n32p x)
                                       (natp (ash x -2))))
                  (:linear
                   :corollary (implies (n32p x)
                                       (< (ash x -2) 1073741824))))))

; We wonder if the two lemmas about !xxx would be better as
; :FORWARD-CHAINING rules (or, as both :REWRITE and :FORWARD-CHAINING
; rules), using *MEM-SIZE-IN-BYTES* and *REG-SIZE-IN-DWRDS* in the
; hypotheses instead of LEN.

(defthm length-is-len-when-not-stringp
  (implies (not (stringp x))
           (equal (length x)
                  (len x)))
  :hints (("Goal" :in-theory (e/d (length) ()))))

; RGF update lemmas

(defthm rgfp-update-nth
  (implies (and (rgfp x)
                (integerp i)
                (<= 0 i)
                (< i (len x))
                (n32p v))
           (rgfp (update-nth i v x))))

(defthm x86-32p-!rgfi-n03p
  (implies (and (x86-32p x86-32)
                (n03p i)
                (n32p v))
           (x86-32p (!rgfi i v x86-32))))


; EIP update lemma

(defthm x86-32p-!eip
  (implies (and (x86-32p x86-32)
                (n32p v))
           (x86-32p (!eip v x86-32))))

; EFLAGS update lemma

(defthm x86-32p-!flg
  (implies (and (x86-32p x86-32)
                (n32p v))
           (x86-32p (!flg v x86-32))))

; Memory update lemmas

(defthm memp-update-nth
  (implies (and (memp x)
                (integerp i)
                (<= 0 i)
                (n32p v))
           (memp (hons-acons i v x))))

(defthm x86-32p-!memi-n30p
  (implies (and (force (x86-32p x86-32))
                (force (n30p i))
                (force (n32p v)))
           (x86-32p (!memi i v x86-32))))

; MS update lemma

(defthm x86-32p-!ms
  (implies (x86-32p x86-32)
           (x86-32p (!ms v x86-32))))

; Should we have this next group of lemmas?  Probably not, but the
; first, for instance, one allows later lemma X86-32P-WMB-NO-WRAP (see
; below) to be proven.

(defthm len-x86-32-rgf
  (implies (x86-32p x86-32)
           (equal (len (nth *rgfi* x86-32))
                  *m86-32-reg-names-len*)))

#||
(defthm len-x86-32-mem
  (implies (x86-32p x86-32)
           (equal (len (nth *memi* x86-32))
                  *mem-size-in-dwords*)))
||#

(defthm x86-32p-properties
  (implies (x86-32p x86-32)
           (and (true-listp x86-32)
                (equal (len x86-32) 5)

                (rgfp (nth 0 x86-32))
                (equal (len (nth 0 x86-32))
                       *m86-32-reg-names-len*)

                (eipp (nth 1 x86-32))
                (flgp (nth 2 x86-32))

                (memp (nth 3 x86-32))
                ;; (equal (len (nth 3 x86-32)) *mem-size-in-dwords*)

                (msp (nth 4 x86-32))
                ))
  :rule-classes :forward-chaining)

; Additional lemmas to help with later guard proofs.

; Hopefully, we have proven all the facts we need about the X86
; machine state.

(in-theory (disable x86-32p
                    rgfp        rgfi        !rgfi
                    eipp        eip         !eip
                    flgp        flg         !flg
                    memp        memi        !memi
                    msp         ms          !ms
                    ))

; Read memory byte and memory double-word functions

(encapsulate
 ()

 (local
  (def-gl-thm ash-logand-addr-3-is-integerp-less-or-equal-24
    :hyp (n32p addr)
    :concl (<= (ash (logand addr 3) 3) 24)
    :g-bindings
     `((addr (:g-number ,(gl-int  0 1 33))))))

 (local
  (def-gl-thm ash-memi-ash-logand-addr
    :hyp (and (n32p mem-value)
              (n32p addr))
    :concl (< (ash mem-value (- (ash (logand addr 3) 3)))
              4294967296)
    :rule-classes :linear
    :g-bindings
    `((addr      (:g-number ,(gl-int  0 1 33)))
      (mem-value (:g-number ,(gl-int 33 1 33))))))

 (defun rm08 (addr x86-32)
   (declare (xargs :guard (and (n32p addr)
                               (x86-32p x86-32))))
   (declare (type (unsigned-byte 32) addr))
   (let* ((byte-num     (n02 addr))
          (dword-addr   (ash addr -2))
          (dword        (memi dword-addr x86-32))
          (shift-amount (ash byte-num 3))
          ;; Next form causes (callq (@ .SPBUILTIN-ASH)).
          (shifted-word (ash dword (- shift-amount))))
     (declare (type (unsigned-byte  2) byte-num)
              (type (unsigned-byte 30) dword-addr)
              (type (unsigned-byte 32) dword shifted-word)
              (type (integer 0     24) shift-amount))
     (n08 shifted-word)))

 (defun rm16 (addr x86-32)
   (declare (xargs :guard (and (n32p addr)
                               (x86-32p x86-32))))
   (declare (type (unsigned-byte 32) addr))
   (let* ((byte-num   (n02 addr))
          (dword-addr (ash addr -2))
          (dword      (memi dword-addr x86-32)))
     (declare (type (unsigned-byte  2) byte-num)
              (type (unsigned-byte 30) dword-addr)
              (type (unsigned-byte 32) dword))
     (if (= byte-num 3)
       ;; Memory "wrap"
         (let* ((byte0 (rm08       addr    x86-32))
                (byte1 (rm08 (n32+ addr 1) x86-32)))
           (declare (type (unsigned-byte 8) byte0 byte1))
           (logior (ash byte1 8) byte0))
       (logand (ash dword (- (ash byte-num 3)))
               #xffff))))

 (local (defthm integerp-+
          (implies (and (integerp x)
                        (integerp y))
                   (integerp (+ x y)))))

 (local (defthm integerp-expt
          (implies (natp x)
                   (integerp (expt 2 x)))))

 (defun rm32 (addr x86-32)
   (declare (xargs :guard (and (n32p addr)
                               (x86-32p x86-32))))
   (declare (type (unsigned-byte 32) addr))
   (let* ((byte-num   (n02 addr))
          (dword-addr (ash addr -2))
          (dword      (memi dword-addr x86-32)))
     (declare (type (unsigned-byte  2) byte-num)
              (type (unsigned-byte 30) dword-addr)
              (type (unsigned-byte 32) dword))
     (if (= byte-num 0)
         dword

; Here is a picture in the case that byte-num is 3 (each "x" digit is hex):

;     dword-addr+1  dword-addr  ...... 0
;           |        |
; [next-dword] [dword]
;    xxxxxxxx xxxxxxxx  [from old mem]
;                   <>  shift0 [ 8 in this example]
;               <-  ->  shift1 [24 in this example]
;             xx        lo
;      xxxxxx           hi

       (let* ((shift0 (ash (- 4 byte-num) 3))
              (shift1 (ash byte-num 3))
              (lo (ash dword (- shift1)))
              (dword-addr+1 (n30+ dword-addr 1))
              (next-dword (memi dword-addr+1 x86-32))
              (hi (logand next-dword (- (ash 1 shift1)
                                        1))))
         (declare (type (unsigned-byte 32) lo hi))
         (logior lo (ash hi shift0)))))))

(defthm natp-rm08
  (implies (and (force (x86-32p x86-32))
                (force (n32p addr)))
           (and (integerp (rm08 addr x86-32))
                (<= 0 (rm08 addr x86-32))))
  :rule-classes :type-prescription)

(defthm rm08-less-than-expt-2-32
  (implies (and (x86-32p x86-32)
                (n32p addr))
           (< (rm08 addr x86-32) 256))
  :rule-classes :linear)

(in-theory (disable rm08))

(encapsulate
 ()
 (local
  (def-gl-thm logior-ash-bytes-<=-0
    :hyp (and (n08p byte0)
              (n08p byte1))
    :concl
    (<= 0 (logior (ash byte1 8) byte0))
    :g-bindings
    `((byte0 (:g-number ,(gl-int  0 1 9)))
      (byte1 (:g-number ,(gl-int  9 1 9))))))

 (local
  (def-gl-thm logior-ash-bytes-<-4294967296
    :hyp (and (n08p byte0)
              (n08p byte1))
    :concl
    (< (logior (ash byte1 8) byte0)
       65536)
    :g-bindings
    `((byte0 (:g-number ,(gl-int  0 1 9)))
      (byte1 (:g-number ,(gl-int  9 1 9))))))

 (defthm integerp-rm16
   (implies (and (force (x86-32p x86-32))
                 (force (n32p addr)))
            (and (integerp (rm16 addr x86-32))
                 (<= 0 (rm16 addr x86-32))))
   :rule-classes :type-prescription)

 (defthm rm16-less-than-expt-2-16
   (implies (and (x86-32p x86-32)
                 (n32p addr))
            (< (rm16 addr x86-32) 65536))
   :rule-classes :linear))

(in-theory (disable rm16))

(defthm integerp-rm32
  (implies (and (force (x86-32p x86-32))
                (force (n32p addr)))
           (and (integerp (rm32 addr x86-32))
                (<= 0 (rm32 addr x86-32))))
  :rule-classes :type-prescription)

(encapsulate
 ()

 (local
  (def-gl-thm hack
    :hyp (and (n32p dword1)
              (n32p dword2)
              (integerp addr)
              (<= 0 addr)
              (< addr 4294967296)
              (not (equal (logand addr 3) 0)))
    :concl (< (logior (ash dword1
                           (- (ash (logand addr 3) 3)))
                      (ash (logand dword2
                                   (+ -1 (ash 1 (ash (logand addr 3) 3))))
                           (ash (+ 4 (- (logand addr 3))) 3)))
              4294967296)
    :g-bindings
    `((addr (:g-number ,(gl-int 0 1 33)))
      (dword1 (:g-number ,(gl-int 33 1 33)))
      (dword2 (:g-number ,(gl-int 66 1 33))))))

 (defthm rm32-less-than-expt-2-32
   (implies (and (x86-32p x86-32)
                 (n32p addr))
            (< (rm32 addr x86-32) 4294967296))
   :rule-classes :linear))

(in-theory (disable rm32))

; Write memory byte, memory double-word functions

(encapsulate
 ()

 (local
  (def-gl-thm ash-logand-addr-3-is-integerp-less-than-or-equal-24
    :hyp (n32p addr)
    :concl (<= (ash (logand addr 3) 3) 24)
    :g-bindings
    `((addr (:g-number ,(gl-int  0 1 33))))))

 (local
  (def-gl-thm ash-n08p-ash-logand-addr-3-3
    :hyp (and (n32p addr)
              (n08p byte))
    :concl (< (ash byte (ash (logand addr 3) 3))
               4294967296)
    :g-bindings
    `((addr (:g-number ,(gl-int  0 1 33)))
      (byte (:g-number ,(gl-int 33 1 9))))))

 (local
  (def-gl-thm word-to-write-equality
    :hyp (and (n08p byte)
              (n32p val)
              (n32p addr))
    :concl
    (equal (logand (logior (logand (lognot (ash 255 (ash (logand addr 3) 3))) val)
                           (ash byte (ash (logand addr 3) 3)))
                   4294967295)
           (logior (logand (lognot (ash 255 (ash (logand addr 3) 3)))
                           val)
                   (ash byte (ash (logand addr 3) 3))))
    :g-bindings
    `((addr (:g-number ,(gl-int  0 1 33)))
      (byte (:g-number ,(gl-int 33 1 9)))
      (val  (:g-number ,(gl-int 42 1 33))))))

 (local
  (def-gl-thm natp-word-to-write
    :hyp (and (n08p byte)
              (n32p value)
              (n32p addr))
    :concl
    (<= 0 (logior (logand (lognot (ash 255 (ash (logand addr 3) 3))) value)
                  (ash byte (ash (logand addr 3) 3))))
    :rule-classes :linear
    :g-bindings
    `((addr  (:g-number ,(gl-int  0 1 33)))
      (byte  (:g-number ,(gl-int 33 1 9)))
      (value (:g-number ,(gl-int 42 1 33))))))

 (local
  (def-gl-thm word-to-write-equality-less-than-2^32
    :hyp (and (n08p byte)
              (n32p val)
              (n32p addr))
    :concl
    (< (logior (logand (lognot (ash 255 (ash (logand addr 3) 3))) val)
               (ash byte (ash (logand addr 3) 3)))
       4294967296)
    :rule-classes :linear
    :g-bindings
    `((addr (:g-number ,(gl-int  0 1 33)))
      (byte (:g-number ,(gl-int 33 1 9)))
      (val  (:g-number ,(gl-int 42 1 33))))))

 (local
  (def-gl-thm another-logand-fact
    :hyp (and (n32p word)
              (n08p byte)
              (n32p addr))
    :concl
    (<= 0 (logior (logand (+ -1 (- (ash 255 (ash (logand addr 3) 3)))) word)
                  (ash byte (ash (logand addr 3) 3))))
    :g-bindings
    `((addr (:g-number ,(gl-int  0 1 33)))
      (byte (:g-number ,(gl-int 33 1 9)))
      (word (:g-number ,(gl-int 42 1 33))))))

 (defun wm08 (addr byte x86-32)
   (declare (xargs :guard (and (n32p addr)
                               (n08p byte)
                               (x86-32p x86-32))
                   :guard-hints
                   (("Goal" :in-theory (e/d () (lognot))))))
   (declare (type (unsigned-byte 32) addr))
   (let* ((byte-num       (n02 addr))
          (dword-addr     (ash addr -2))
          (dword          (memi dword-addr x86-32))
          (mask           #xFF)
          (shift-amount   (ash byte-num 3))
          (byte-mask      (ash mask shift-amount))
          (dword-masked   (logand (lognot byte-mask) dword))
          (byte-to-write  (ash byte shift-amount))
          (dword-to-write (logior dword-masked byte-to-write)))
     (declare (type (unsigned-byte  2) byte-num)
              (type (integer 0     24) shift-amount)
              (type (unsigned-byte 30) dword-addr)
              (type (unsigned-byte 32)
                    byte-mask dword dword-masked byte-to-write dword-to-write))
     (!memi dword-addr dword-to-write x86-32)))

 (defthm x86-32p-wm08
   (implies (and (x86-32p x86-32)
                 (n32p addr)
                 (n08p byte))
            (x86-32p (wm08 addr byte x86-32)))))

(in-theory (disable wm08))

(encapsulate
 ()

 (local
  (def-gl-thm hack1
    :hyp (and (n32p addr)
              (not (equal (logand addr 3) 3)))
    :concl (<= (ash (logand addr 3) 3) 16)
    :g-bindings
    `((addr (:g-number ,(gl-int  0 1 33))))
    :rule-classes :linear))

 (local
  (def-gl-thm hack2
    :hyp (and (n16p word)
              (natp shift)
              (<= shift 16))
    :concl (< (ash word shift) 4294967296)
    :g-bindings
    `((word  (:g-number ,(gl-int 0 1 17)))
      (shift (:g-number ,(gl-int 17 1 6))))
    :rule-classes :linear))

 (local
  (def-gl-thm logior-ash-bytes-<-4294967296
    :hyp (and (n08p byte0)
              (n08p byte1)
              (n08p byte2)
              (n08p byte3))
    :concl
    (< (logior (ash byte3 24) (ash byte2 16) (ash byte1 8) byte0)
       4294967296)
    :g-bindings
    `((byte0 (:g-number ,(gl-int  0 1 9)))
      (byte1 (:g-number ,(gl-int  9 1 9)))
      (byte2 (:g-number ,(gl-int 18 1 9)))
      (byte3 (:g-number ,(gl-int 27 1 9))))))

 (defun wm16 (addr word x86-32)
   (declare (xargs :guard (and (n32p addr)
                               (n16p word)
                               (x86-32p x86-32))
                   :guard-hints
                   (("Goal" :in-theory (e/d () (lognot))))))
   (declare (type (unsigned-byte 32) addr))
   (let* ((byte-num      (n02 addr))
          (dword-addr    (ash addr -2)))
     (declare (type (unsigned-byte  2) byte-num)
              (type (unsigned-byte 30) dword-addr))
     (if (= byte-num 3) ; memory wrap
         (b* ((x86-32 (wm08       addr    (n08      word     ) x86-32))
              (x86-32 (wm08 (n32+ addr 1) (n08 (ash word  -8)) x86-32)))
             x86-32)
       (let* ((dword          (memi dword-addr x86-32))
              (mask           #xFFFF)
              (shift-amount   (ash byte-num 3))
              (byte-mask      (ash mask shift-amount))
              (dword-masked   (logand (lognot byte-mask) dword))
              (word-to-write  (ash word shift-amount))
              (dword-to-write (logior dword-masked word-to-write)))
         (declare (type (integer 0 16) shift-amount)
                  (type (unsigned-byte 32)
                        byte-mask dword dword-masked word-to-write
                        dword-to-write))
         (!memi dword-addr dword-to-write x86-32)))))

 (defthm x86-32p-wm16
   (implies (and (x86-32p x86-32)
                 (n32p addr)
                 (n16p word))
            (x86-32p (wm16 addr word x86-32)))))

(in-theory (disable wm16))

; The next two hack lemmas are needed not only to admit wm32 (below), but also
; to prove x86-32p-wm32 (below).

(local
 (def-gl-thm hack1
   :hyp (and (n32p dword)
             (n32p addr))
   :concl
   (< (ash (logand dword
                   (+ -1
                      (ash 1 (ash (+ 4 (- (logand addr 3))) 3))))
           (ash (logand addr 3) 3))
      4294967296)
   :g-bindings
   `((dword (:g-number ,(gl-int  0 1 33)))
     (addr  (:g-number ,(gl-int 34 1 33))))))

(local
 (def-gl-thm hack2
   :hyp (and (n32p dword)
             (n32p addr))
   :concl
   (< (ash dword
           (- (ash (+ 4 (- (logand addr 3))) 3)))
      4294967296)
   :g-bindings
   `((dword (:g-number ,(gl-int  0 1 33)))
     (addr  (:g-number ,(gl-int 34 1 33))))))

(defun wm32 (addr dword x86-32)
  (declare (xargs :guard (and (n32p addr)
                              (n32p dword)
                              (x86-32p x86-32))))
  (declare (type (unsigned-byte 32) addr))
  (let* ((byte-num   (n02 addr))
         (dword-addr (ash addr -2)))
    (declare (type (integer 0 3) byte-num)
             (type (unsigned-byte 30) dword-addr))
    (if (= byte-num 0)
        (!memi dword-addr dword x86-32)

; We write two dwords to memory: a lower dword obtained by replacing the upper
; bits of the original lower dword, and similarly, an upper dword obtained by
; replacing the lower bits of the original upper dword.

; Here is a picture in the case that byte-num is 3 (each "x" digit is hex):

;  dword-addr+1  dword-addr  ...... 0
;        |        |
; xxxxxxxx xxxxxxxx  [from old mem]
;   xxxxxx xx        dword
;                <>  shift0 [ 8 in this example]
;                ff  mask0
;            <-  ->  shift1 [24 in this example]
;            ffffff  mask1

      (let* ((dword0-old   (memi dword-addr x86-32))
             (shift0       (ash (- 4 byte-num) 3))
             (mask0        (- (ash 1 shift0) 1))
             (shift1       (ash byte-num 3))
             (mask1        (- (ash 1 shift1) 1))
             (dword0-lo    (logand dword0-old mask1))
             (dword0-hi    (ash (logand dword mask0) shift1))
             (dword0-new   (logior dword0-lo dword0-hi))
             (x86-32       (!memi dword-addr dword0-new x86-32))
             (dword-addr+1 (n30+ dword-addr 1))
             (dword1-old   (memi dword-addr+1 x86-32))
             (dword1-lo    (ash dword (- shift0)))
             (dword1-hi    (logand dword1-old (ash mask0 shift1)))
             (dword1-new   (logior dword1-lo dword1-hi))
             (x86-32       (!memi dword-addr+1 dword1-new x86-32)))
        x86-32))))

(defthm x86-32p-wm32
  (implies (and (x86-32p x86-32)
                (n32p addr)
                (n32p dword))
           (x86-32p (wm32 addr dword x86-32))))

(in-theory (disable wm32))

;;; The following lemmas probably won't be needed once we can traffic in stobjs
;;; when doing alist-based reasoning.

(defthm logior-<=-expt-2-to-n
   (implies (and (natp x) (natp y)
                 (< x (expt 2 n))
                 (< y (expt 2 n)))
            (< (logior x y) (expt 2 n)))
   :rule-classes :linear)

(defthm logior-bound-2^32
  (implies (and (natp x)
                (natp y)
                (< x *2^32*)
                (< y *2^32*))
           (< (logior x y) *2^32*))
  :hints (("Goal" :use ((:instance logior-<=-expt-2-to-n
                                   (n 32))))))

(local
 (def-gl-thm hack3
   :hyp (and (natp addr)
             (< addr 4294967296)
             (natp byte)
             (< byte 256))
   :concl
   (< (ash byte (ash (logand addr 3) 3))
      4294967296)
   :g-bindings
   `((addr (:g-number ,(gl-int  0 1 33)))
     (byte  (:g-number ,(gl-int 34 1 9))))))

;(in-theory (disable (expt)))

(defthm wm08-preserves-x86-32p
  (implies (and (force (n32p addr))
                (force (n08p byte))
                (force (x86-32p x86-32)))
           (x86-32p (wm08 addr byte x86-32)))
  :hints (("Goal" :in-theory (enable wm08))))

(defthm wm16-preserves-x86-32p
  (implies (and (force (n32p addr))
                (force (n16p word))
                (force (x86-32p x86-32)))
           (x86-32p (wm32 addr word x86-32)))
  :hints (("Goal" :in-theory (enable wm16))))

(defthm wm32-preserves-x86-32p
  (implies (and (force (n32p addr))
                (force (n32p dword))
                (force (x86-32p x86-32)))
           (x86-32p (wm32 addr dword x86-32)))
  :hints (("Goal" :in-theory (enable wm32))))