aboutsummaryrefslogtreecommitdiffstats
path: root/src/translation/HTLgenproof.v
blob: 8e97c587b632b9832e51cabe66ede4a5fb2de3c0 (plain)
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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
(*
 * CoqUp: Verified high-level synthesis.
 * Copyright (C) 2020 Yann Herklotz <yann@yannherklotz.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *)

From compcert Require RTL Registers AST Integers.
From compcert Require Import Globalenvs Memory.
From coqup Require Import Coquplib HTLgenspec HTLgen Value AssocMap Array IntegerExtra.
From coqup Require HTL Verilog.

Require Import Lia.

Local Open Scope assocmap.

Hint Resolve Smallstep.forward_simulation_plus : htlproof.
Hint Resolve AssocMap.gss : htlproof.
Hint Resolve AssocMap.gso : htlproof.

Hint Unfold find_assocmap AssocMapExt.get_default : htlproof.

Inductive match_assocmaps : RTL.function -> RTL.regset -> assocmap -> Prop :=
  match_assocmap : forall f rs am,
    (forall r, Ple r (RTL.max_reg_function f) ->
               val_value_lessdef (Registers.Regmap.get r rs) am#r) ->
    match_assocmaps f rs am.
Hint Constructors match_assocmaps : htlproof.

Definition state_st_wf (m : HTL.module) (s : HTL.state) :=
  forall st asa asr res,
  s = HTL.State res m st asa asr ->
  asa!(m.(HTL.mod_st)) = Some (posToValue 32 st).
Hint Unfold state_st_wf : htlproof.

Inductive match_arrs (m : HTL.module) (f : RTL.function) (sp : Values.val) (mem : mem) :
  Verilog.assocmap_arr -> Prop :=
| match_arr : forall asa stack,
    asa ! (m.(HTL.mod_stk)) = Some stack /\
    stack.(arr_length) = Z.to_nat (f.(RTL.fn_stacksize) / 4) /\
    (forall ptr,
        0 <= ptr < Z.of_nat m.(HTL.mod_stk_len) ->
        opt_val_value_lessdef (Mem.loadv AST.Mint32 mem
                                         (Values.Val.offset_ptr sp (Integers.Ptrofs.repr (4 * ptr))))
                              (Option.default (NToValue 32 0)
                                 (Option.join (array_get_error (Z.to_nat ptr) stack)))) ->
    match_arrs m f sp mem asa.

Inductive match_stacks (mem : mem) : list RTL.stackframe -> list HTL.stackframe -> Prop :=
| match_stacks_nil :
    match_stacks mem nil nil
| match_stacks_cons :
    forall cs lr r f sp pc rs m asr asa
           (TF : tr_module f m)
           (ST: match_stacks mem cs lr)
           (MA: match_assocmaps f rs asr)
           (MARR : match_arrs m f sp mem asa),
      match_stacks mem (RTL.Stackframe r f sp pc rs :: cs)
                       (HTL.Stackframe r m pc asr asa :: lr).

Definition stack_based (v : Values.val) (sp : Values.block) : Prop :=
   match v with
   | Values.Vptr sp' off => sp' = sp
   | _ => True
   end.

Definition reg_stack_based_pointers (sp : Values.block) (rs : Registers.Regmap.t Values.val) : Prop :=
  forall r, stack_based (Registers.Regmap.get r rs) sp.

Definition arr_stack_based_pointers (spb : Values.block) (m : mem) (stack_length : Z)
  (sp : Values.val) : Prop :=
  forall ptr,
    0 <= ptr < (stack_length / 4) ->
    stack_based (Option.default
                   Values.Vundef
                   (Mem.loadv AST.Mint32 m
                              (Values.Val.offset_ptr sp (Integers.Ptrofs.repr (4 * ptr)))))
                spb.

Inductive match_states : RTL.state -> HTL.state -> Prop :=
| match_state : forall asa asr sf f sp sp' rs mem m st res
    (MASSOC : match_assocmaps f rs asr)
    (TF : tr_module f m)
    (WF : state_st_wf m (HTL.State res m st asr asa))
    (MS : match_stacks mem sf res)
    (MARR : match_arrs m f sp mem asa)
    (SP : sp = Values.Vptr sp' (Integers.Ptrofs.repr 0))
    (RSBP: reg_stack_based_pointers sp' rs)
    (ASBP: arr_stack_based_pointers sp' mem (f.(RTL.fn_stacksize)) sp),
    match_states (RTL.State sf f sp st rs mem)
                 (HTL.State res m st asr asa)
| match_returnstate :
    forall
      v v' stack mem res
      (MS : match_stacks mem stack res),
      val_value_lessdef v v' ->
      match_states (RTL.Returnstate stack v mem) (HTL.Returnstate res v')
| match_initial_call :
    forall f m m0
    (TF : tr_module f m),
      match_states (RTL.Callstate nil (AST.Internal f) nil m0) (HTL.Callstate nil m nil).
Hint Constructors match_states : htlproof.

Definition match_prog (p: RTL.program) (tp: HTL.program) :=
  Linking.match_program (fun cu f tf => transl_fundef f = Errors.OK tf) eq p tp.

Lemma transf_program_match:
  forall p tp, HTLgen.transl_program p = Errors.OK tp -> match_prog p tp.
Proof.
  intros. apply Linking.match_transform_partial_program; auto.
Qed.

Lemma regs_lessdef_add_greater :
  forall f rs1 rs2 n v,
    Plt (RTL.max_reg_function f) n ->
    match_assocmaps f rs1 rs2 ->
    match_assocmaps f rs1 (AssocMap.set n v rs2).
Proof.
  inversion 2; subst.
  intros. constructor.
  intros. unfold find_assocmap. unfold AssocMapExt.get_default.
  rewrite AssocMap.gso. eauto.
  apply Pos.le_lt_trans with _ _ n in H2.
  unfold not. intros. subst. eapply Pos.lt_irrefl. eassumption. assumption.
Qed.
Hint Resolve regs_lessdef_add_greater : htlproof.

Lemma regs_lessdef_add_match :
  forall f rs am r v v',
    val_value_lessdef v v' ->
    match_assocmaps f rs am ->
    match_assocmaps f (Registers.Regmap.set r v rs) (AssocMap.set r v' am).
Proof.
  inversion 2; subst.
  constructor. intros.
  destruct (peq r0 r); subst.
  rewrite Registers.Regmap.gss.
  unfold find_assocmap. unfold AssocMapExt.get_default.
  rewrite AssocMap.gss. assumption.

  rewrite Registers.Regmap.gso; try assumption.
  unfold find_assocmap. unfold AssocMapExt.get_default.
  rewrite AssocMap.gso; eauto.
Qed.
Hint Resolve regs_lessdef_add_match : htlproof.

Lemma list_combine_none :
  forall n l,
    length l = n ->
    list_combine Verilog.merge_cell (list_repeat None n) l = l.
Proof.
  induction n; intros; simplify.
  - symmetry. apply length_zero_iff_nil. auto.
  - destruct l; simplify.
    rewrite list_repeat_cons.
    simplify. f_equal.
    eauto.
Qed.

Lemma combine_none :
  forall n a,
    a.(arr_length) = n ->
    arr_contents (combine Verilog.merge_cell (arr_repeat None n) a) = arr_contents a.
Proof.
  intros.
  unfold combine.
  simplify.

  rewrite <- (arr_wf a) in H.
  apply list_combine_none.
  assumption.
Qed.

(* Need to eventually move posToValue 32 to posToValueAuto, as that has this proof. *)
Lemma assumption_32bit :
  forall v,
    valueToPos (posToValue 32 v) = v.
Admitted.

Lemma st_greater_than_res :
  forall m res : positive,
    m <> res.
Admitted.

Lemma finish_not_return :
  forall r f : positive,
    r <> f.
Admitted.

Lemma finish_not_res :
  forall f r : positive,
    f <> r.
Admitted.

Lemma greater_than_max_func :
  forall f st,
    Plt (RTL.max_reg_function f) st.
Proof. Admitted.

Ltac inv_state :=
  match goal with
    MSTATE : match_states _ _ |- _ =>
    inversion MSTATE;
    match goal with
      TF : tr_module _ _ |- _ =>
      inversion TF;
      match goal with
        TC : forall _ _,
          Maps.PTree.get _ _ = Some _ -> tr_code _ _ _ _ _ _ _ _ _,
        H : Maps.PTree.get _ _ = Some _ |- _ =>
        apply TC in H; inversion H;
        match goal with
          TI : context[tr_instr] |- _ =>
          inversion TI
        end
      end
    end
end; subst.

Ltac unfold_func H :=
  match type of H with
  | ?f = _ => unfold f in H; repeat (unfold_match H)
  | ?f _ = _ => unfold f in H; repeat (unfold_match H)
  | ?f _ _ = _ => unfold f in H; repeat (unfold_match H)
  | ?f _ _ _ = _ => unfold f in H; repeat (unfold_match H)
  | ?f _ _ _ _ = _ => unfold f in H; repeat (unfold_match H)
  end.

Lemma init_reg_assoc_empty :
  forall f l,
    match_assocmaps f (RTL.init_regs nil l) (HTL.init_regs nil l).
Proof.
  induction l; simpl; constructor; intros.
  - rewrite Registers.Regmap.gi. unfold find_assocmap.
    unfold AssocMapExt.get_default. rewrite AssocMap.gempty.
    constructor.

  - rewrite Registers.Regmap.gi. unfold find_assocmap.
    unfold AssocMapExt.get_default. rewrite AssocMap.gempty.
    constructor.
Qed.

Section CORRECTNESS.

  Variable prog : RTL.program.
  Variable tprog : HTL.program.

  Hypothesis TRANSL : match_prog prog tprog.

  Let ge : RTL.genv := Globalenvs.Genv.globalenv prog.
  Let tge : HTL.genv := Globalenvs.Genv.globalenv tprog.

  Lemma symbols_preserved:
    forall (s: AST.ident), Genv.find_symbol tge s = Genv.find_symbol ge s.
  Proof
    (Genv.find_symbol_transf_partial TRANSL).

  Lemma function_ptr_translated:
    forall (b: Values.block) (f: RTL.fundef),
      Genv.find_funct_ptr ge b = Some f ->
      exists tf,
        Genv.find_funct_ptr tge b = Some tf /\ transl_fundef f = Errors.OK tf.
  Proof.
    intros. exploit (Genv.find_funct_ptr_match TRANSL); eauto.
    intros (cu & tf & P & Q & R); exists tf; auto.
  Qed.

  Lemma functions_translated:
    forall (v: Values.val) (f: RTL.fundef),
      Genv.find_funct ge v = Some f ->
      exists tf,
        Genv.find_funct tge v = Some tf /\ transl_fundef f = Errors.OK tf.
  Proof.
    intros. exploit (Genv.find_funct_match TRANSL); eauto.
    intros (cu & tf & P & Q & R); exists tf; auto.
  Qed.

  Lemma senv_preserved:
    Senv.equiv (Genv.to_senv ge) (Genv.to_senv tge).
  Proof
    (Genv.senv_transf_partial TRANSL).
  Hint Resolve senv_preserved : htlproof.

  Lemma eval_correct :
    forall sp op rs args m v v' e asr asa f,
      Op.eval_operation ge sp op
(List.map (fun r : BinNums.positive => Registers.Regmap.get r rs) args) m = Some v ->
      tr_op op args e ->
      val_value_lessdef v v' ->
      Verilog.expr_runp f asr asa e v'.
  Admitted.

  Lemma eval_cond_correct :
    forall cond (args : list Registers.reg) s1 c s' i rs args m b f asr asa,
    translate_condition cond args s1 = OK c s' i ->
    Op.eval_condition
      cond
      (List.map (fun r : BinNums.positive => Registers.Regmap.get r rs) args)
      m = Some b ->
    Verilog.expr_runp f asr asa c (boolToValue 32 b).
  Admitted.

  (** The proof of semantic preservation for the translation of instructions
      is a simulation argument based on diagrams of the following form:
<<
                      match_states
    code st rs ---------------- State m st assoc
         ||                             |
         ||                             |
         ||                             |
         \/                             v
    code st rs' --------------- State m st assoc'
                      match_states
>>
      where [tr_code c data control fin rtrn st] is assumed to hold.

      The precondition and postcondition is that that should hold is [match_assocmaps rs assoc].
   *)

  Definition transl_instr_prop (instr : RTL.instruction) : Prop :=
    forall m asr asa fin rtrn st stmt trans res,
      tr_instr fin rtrn st (m.(HTL.mod_stk)) instr stmt trans ->
      exists asr' asa',
        HTL.step tge (HTL.State res m st asr asa) Events.E0 (HTL.State res m st asr' asa').


  Theorem transl_step_correct:
    forall (S1 : RTL.state) t S2,
      RTL.step ge S1 t S2 ->
      forall (R1 : HTL.state),
        match_states S1 R1 ->
        exists R2, Smallstep.plus HTL.step tge R1 t R2 /\ match_states S2 R2.
  Proof.
    induction 1; intros R1 MSTATE; try inv_state.
    - (* Inop *)
      unfold match_prog in TRANSL.
      econstructor.
      split.
      apply Smallstep.plus_one.
      eapply HTL.step_module; eauto.
      (* processing of state *)
      econstructor.
      simplify.
      econstructor.
      econstructor.
      econstructor.
      simplify.

      unfold Verilog.merge_regs.
      unfold_merge. apply AssocMap.gss.

      (* prove match_state *)
      rewrite assumption_32bit.
      econstructor; simplify; eauto.

      unfold Verilog.merge_regs.
      unfold_merge. simpl. apply regs_lessdef_add_greater. apply greater_than_max_func.
      assumption.
      unfold Verilog.merge_regs.
      unfold state_st_wf. inversion 1. subst. unfold_merge. apply AssocMap.gss.

      (* prove match_arrs *)
      invert MARR. simplify.
      unfold HTL.empty_stack. simplify. unfold Verilog.merge_arrs.
      econstructor.
      simplify. repeat split.

      rewrite AssocMap.gcombine.
      2: { reflexivity. }
      rewrite AssocMap.gss.
      unfold Verilog.merge_arr.
      setoid_rewrite H3.
      reflexivity.

      rewrite combine_length.
      unfold arr_repeat. simplify.
      rewrite list_repeat_len.
      reflexivity.

      unfold arr_repeat. simplify.
      rewrite list_repeat_len; auto.
      intros.
      erewrite array_get_error_equal.
      eauto. apply combine_none.

      assumption.

    - (* Iop *)
      (* destruct v eqn:?; *)
      (*          try ( *)
      (*            destruct op eqn:?; inversion H21; simpl in H0; repeat (unfold_match H0); *)
      (*            inversion H0; subst; simpl in *; try (unfold_func H4); try (unfold_func H5); *)
      (*            try (unfold_func H6); *)
      (*            try (unfold Op.eval_addressing32 in H6; repeat (unfold_match H6); inversion H6; *)
      (*                 unfold_func H3); *)

      (*            inversion Heql; inversion MASSOC; subst; *)
      (*            assert (HPle : Ple r (RTL.max_reg_function f)) *)
      (*              by (eapply RTL.max_reg_function_use; eauto; simpl; auto); *)
      (*            apply H1 in HPle; inversion HPle; *)
      (*            rewrite H2 in *; discriminate *)
      (*          ). *)

      (* + econstructor. split. *)
      (* apply Smallstep.plus_one. *)
      (* eapply HTL.step_module; eauto. *)
      (* econstructor; simpl; trivial. *)
      (* constructor; trivial. *)
      (* econstructor; simpl; eauto. *)
      (* eapply eval_correct; eauto. constructor. *)
      (* unfold_merge. simpl. *)
      (* rewrite AssocMap.gso. *)
      (* apply AssocMap.gss. *)
      (* apply st_greater_than_res. *)

      (* (* match_states *) *)
      (* assert (pc' = valueToPos (posToValue 32 pc')). auto using assumption_32bit. *)
      (* rewrite <- H1. *)
      (* constructor; auto. *)
      (* unfold_merge. *)
      (* apply regs_lessdef_add_match. *)
      (* constructor. *)
      (* apply regs_lessdef_add_greater. *)
      (* apply greater_than_max_func. *)
      (* assumption. *)

      (* unfold state_st_wf. intros. inversion H2. subst. *)
      (* unfold_merge. *)
      (* rewrite AssocMap.gso. *)
      (* apply AssocMap.gss. *)
      (* apply st_greater_than_res. *)

      (* + econstructor. split. *)
      (* apply Smallstep.plus_one. *)
      (* eapply HTL.step_module; eauto. *)
      (* econstructor; simpl; trivial. *)
      (* constructor; trivial. *)
      (* econstructor; simpl; eauto. *)
      (* eapply eval_correct; eauto. *)
      (* constructor. rewrite valueToInt_intToValue. trivial. *)
      (* unfold_merge. simpl. *)
      (* rewrite AssocMap.gso. *)
      (* apply AssocMap.gss. *)
      (* apply st_greater_than_res. *)

      (* (* match_states *) *)
      (* assert (pc' = valueToPos (posToValue 32 pc')). auto using assumption_32bit. *)
      (* rewrite <- H1. *)
      (* constructor. *)
      (* unfold_merge. *)
      (* apply regs_lessdef_add_match. *)
      (* constructor. *)
      (* symmetry. apply valueToInt_intToValue. *)
      (* apply regs_lessdef_add_greater. *)
      (* apply greater_than_max_func. *)
      (* assumption. assumption. *)

      (* unfold state_st_wf. intros. inversion H2. subst. *)
      (* unfold_merge. *)
      (* rewrite AssocMap.gso. *)
      (* apply AssocMap.gss. *)
      (* apply st_greater_than_res. *)
      (* assumption. *)
      admit.

      Ltac rt :=
        repeat match goal with
        | [ _ : error _ _ = OK _ _ _ |- _ ] => discriminate
        | [ _ : context[if (?x && ?y) then _ else _] |- _ ] =>
          let EQ1 := fresh "EQ" in
          let EQ2 := fresh "EQ" in
          destruct x eqn:EQ1; destruct y eqn:EQ2; simpl in *
        | [ _ : context[if ?x then _ else _] |- _ ] =>
          let EQ := fresh "EQ" in
          destruct x eqn:EQ; simpl in *
        | [ H : ret _ _ = _  |- _ ] => invert H
        | [ _ : context[match ?x with | _ => _ end] |- _ ] => destruct x
        end.

    - (* FIXME: Should be able to use the spec to avoid destructing here? *)
      destruct c, chunk, addr, args; simplify; rt; simplify.

      + (** Preamble *)
        invert MARR. simplify.

        unfold Op.eval_addressing in H0.
        destruct (Archi.ptr64) eqn:ARCHI; simplify.

        unfold reg_stack_based_pointers in RSBP.
        pose proof (RSBP r0) as RSBPr0.

        destruct (Registers.Regmap.get r0 rs) eqn:EQr0; simplify.

        rewrite ARCHI in H1. simplify.
        subst.

        pose proof MASSOC as MASSOC'.
        invert MASSOC'.
        pose proof (H0 r0).
        assert (HPler0 : Ple r0 (RTL.max_reg_function f))
          by (eapply RTL.max_reg_function_use; eauto; simplify; eauto).
        apply H6 in HPler0.
        invert HPler0; try congruence.
        rewrite EQr0 in H8.
        invert H8.
        clear H0. clear H6.

        unfold check_address_parameter in *. simplify.

        remember (Integers.Ptrofs.add (Integers.Ptrofs.repr (valueToZ asr # r0))
                                      (Integers.Ptrofs.of_int (Integers.Int.repr z))) as OFFSET.

        (** Read bounds assumption *)
        assert (0 <= Integers.Ptrofs.signed OFFSET) as READ_BOUND_LOW by admit.
        assert (Integers.Ptrofs.signed OFFSET < f.(RTL.fn_stacksize)) as READ_BOUND_HIGH by admit.

        (** Modular Preservation proof *)
        assert (Integers.Ptrofs.signed OFFSET mod 4 = 0) as MOD_PRESERVE by admit.

        (** Normalisation proof *)
        assert (Integers.Ptrofs.repr
                  (4 * Integers.Ptrofs.unsigned
                         (Integers.Ptrofs.divs OFFSET (Integers.Ptrofs.repr 4))) = OFFSET)
          as NORMALISE.
        { replace 4 with (Integers.Ptrofs.unsigned (Integers.Ptrofs.repr 4)) at 1 by reflexivity.
          rewrite <- PtrofsExtra.mul_unsigned.
          apply PtrofsExtra.mul_divs; auto.
          rewrite Integers.Ptrofs.signed_repr; simplify; lia. }

        (** Normalised bounds proof *)
        assert (0 <=
                Integers.Ptrofs.unsigned (Integers.Ptrofs.divs OFFSET (Integers.Ptrofs.repr 4))
                < (RTL.fn_stacksize f / 4))
        as NORMALISE_BOUND.
        { split.
          apply Integers.Ptrofs.unsigned_range_2.
          assert (forall x y, Integers.Ptrofs.divs x y = Integers.Ptrofs.divs x y ) by reflexivity.
          unfold Integers.Ptrofs.divs at 2 in H0.
          rewrite H0. clear H0.
          rewrite Integers.Ptrofs.unsigned_repr; simplify.
          rewrite Integers.Ptrofs.signed_repr; simplify; try lia.
          rewrite Z.quot_div_nonneg; try (rewrite <- HeqOFFSET; assumption); try lia.
          apply Zmult_lt_reg_r with (p := 4); try lia.
          repeat rewrite ZLib.div_mul_undo; try lia.
          rewrite Integers.Ptrofs.signed_repr.
          rewrite Z.quot_div_nonneg; try (rewrite <- HeqOFFSET; assumption); try lia.
          split.
          apply Z.div_pos; try (rewrite <- HeqOFFSET; assumption); try lia.
          apply Z.div_le_upper_bound; simplify; try (rewrite <- HeqOFFSET; lia); try lia.
          simplify; lia. }

        inversion NORMALISE_BOUND as [ NORMALISE_BOUND_LOW NORMALISE_BOUND_HIGH ];
        clear NORMALISE_BOUND.

        eexists. split.
        eapply Smallstep.plus_one.
        eapply HTL.step_module; eauto.
        econstructor. econstructor. econstructor. simplify.
        econstructor. econstructor. econstructor. simplify.
        eapply Verilog.erun_Vbinop with (EQ := ?[EQ1]). (* FIXME: These will be shelved and cause sadness. *)
        eapply Verilog.erun_Vbinop with (EQ := ?[EQ2]).
        econstructor. econstructor. econstructor. econstructor.
        econstructor. econstructor. econstructor. econstructor.

        all: simplify.

        (** Verilog array lookup *)
        unfold Verilog.arr_assocmap_lookup. setoid_rewrite H5.
        f_equal.

        (** State Lookup *)
        unfold Verilog.merge_regs.
        simplify.
        unfold_merge.
        rewrite AssocMap.gso.
        apply AssocMap.gss.
        apply st_greater_than_res.

        (** Match states *)
        rewrite assumption_32bit.
        econstructor; eauto.

        (** Match assocmaps *)
        unfold Verilog.merge_regs. simplify. unfold_merge.
        apply regs_lessdef_add_match.

        (** Equality proof *)
        assert (Integers.Ptrofs.repr 0 = Integers.Ptrofs.zero) as ZERO by reflexivity.
        rewrite ZERO in H7. clear ZERO.
        setoid_rewrite Integers.Ptrofs.add_zero_l in H7.

        specialize (H7 (Integers.Ptrofs.unsigned
                          (Integers.Ptrofs.divs
                             OFFSET
                             (Integers.Ptrofs.repr 4)))).

        exploit H7.
        rewrite Z2Nat.id; eauto.
        apply Z.div_pos; lia.

        intros I.

        assert (Z.to_nat
                  (Integers.Ptrofs.unsigned
                     (Integers.Ptrofs.divs
                        OFFSET
                        (Integers.Ptrofs.repr 4)))
                =
                valueToNat (vdivs (vplus asr # r0 (ZToValue 32 z) ?EQ2) (ZToValue 32 4) ?EQ1))
          as EXPR_OK by admit.
        rewrite <- EXPR_OK.
        rewrite NORMALISE in I.
        rewrite H1 in I.
        invert I. assumption.

        (** PC match *)
        apply regs_lessdef_add_greater.
        apply greater_than_max_func.
        assumption.

        (** States well formed *)
        unfold state_st_wf. inversion 1. simplify.
        unfold Verilog.merge_regs.
        unfold_merge. rewrite AssocMap.gso.
        apply AssocMap.gss.
        apply st_greater_than_res.

        (** Match arrays *)
        econstructor.
        repeat split; simplify.
        unfold HTL.empty_stack.
        simplify.
        unfold Verilog.merge_arrs.

        rewrite AssocMap.gcombine.
        2: { reflexivity. }
        rewrite AssocMap.gss.
        unfold Verilog.merge_arr.
        setoid_rewrite H5.
        reflexivity.

        rewrite combine_length.
        unfold arr_repeat. simplify.
        rewrite list_repeat_len.
        reflexivity.

        unfold arr_repeat. simplify.
        rewrite list_repeat_len.
        congruence.

        intros.
        erewrite array_get_error_equal.
        eauto. apply combine_none.
        assumption.

        (** RSBP preservation *)
        unfold reg_stack_based_pointers. intros.
        destruct (Pos.eq_dec r1 dst); try rewrite e. (* FIXME: Prepare this for automation *)

        rewrite Registers.Regmap.gss.
        unfold arr_stack_based_pointers in ASBP.
        specialize (ASBP (Integers.Ptrofs.unsigned
                            (Integers.Ptrofs.divs OFFSET (Integers.Ptrofs.repr 4)))).
        exploit ASBP; auto; intros I.

        rewrite NORMALISE in I.
        assert (Integers.Ptrofs.repr 0 = Integers.Ptrofs.zero) as ZERO by reflexivity.
        rewrite ZERO in I. clear ZERO.
        simplify.
        rewrite Integers.Ptrofs.add_zero_l in I.
        rewrite H1 in I.
        assumption.
        simplify.

        rewrite Registers.Regmap.gso; auto.

      + (* FIXME: Why is this degenerate? Should we support this mode? *)
        unfold Op.eval_addressing in H0.
        destruct (Archi.ptr64) eqn:ARCHI; simplify.
        destruct (Registers.Regmap.get r0 rs) eqn:EQr0; discriminate.

      + (** Preamble *)
        invert MARR. simplify.

        unfold Op.eval_addressing in H0.
        destruct (Archi.ptr64) eqn:ARCHI; simplify.

        unfold reg_stack_based_pointers in RSBP.
        pose proof (RSBP r0) as RSBPr0.
        pose proof (RSBP r1) as RSBPr1.

        destruct (Registers.Regmap.get r0 rs) eqn:EQr0;
        destruct (Registers.Regmap.get r1 rs) eqn:EQr1; simplify.

        rewrite ARCHI in H1. simplify.
        subst.
        clear RSBPr1.

        pose proof MASSOC as MASSOC'.
        invert MASSOC'.
        pose proof (H0 r0).
        pose proof (H0 r1).
        assert (HPler0 : Ple r0 (RTL.max_reg_function f))
          by (eapply RTL.max_reg_function_use; eauto; simplify; eauto).
        assert (HPler1 : Ple r1 (RTL.max_reg_function f))
          by (eapply RTL.max_reg_function_use; eauto; simpl; auto).
        apply H6 in HPler0.
        apply H8 in HPler1.
        invert HPler0; invert HPler1; try congruence.
        rewrite EQr0 in H10.
        rewrite EQr1 in H12.
        invert H10. invert H12.
        clear H0. clear H6. clear H8.

        unfold check_address_parameter in *. simplify.

        remember (Integers.Ptrofs.add (Integers.Ptrofs.repr (valueToZ asr # r0))
                                      (Integers.Ptrofs.of_int
                                         (Integers.Int.add (Integers.Int.mul (valueToInt asr # r1) (Integers.Int.repr z))
                                                           (Integers.Int.repr z0)))) as OFFSET.

        (** Read bounds assumption *)
        assert (0 <= Integers.Ptrofs.signed OFFSET) as READ_BOUND_LOW by admit.
        assert (Integers.Ptrofs.signed OFFSET < f.(RTL.fn_stacksize)) as READ_BOUND_HIGH by admit.

        (** Modular Preservation proof *)
        assert (Integers.Ptrofs.signed OFFSET mod 4 = 0) as MOD_PRESERVE by admit.

        (** Normalisation proof *)
        assert (Integers.Ptrofs.repr
                  (4 * Integers.Ptrofs.unsigned
                         (Integers.Ptrofs.divs OFFSET (Integers.Ptrofs.repr 4))) = OFFSET)
          as NORMALISE.
        { replace 4 with (Integers.Ptrofs.unsigned (Integers.Ptrofs.repr 4)) at 1 by reflexivity.
          rewrite <- PtrofsExtra.mul_unsigned.
          apply PtrofsExtra.mul_divs; auto.
          rewrite Integers.Ptrofs.signed_repr; simplify; lia. }

        (** Normalised bounds proof *)
        assert (0 <=
                Integers.Ptrofs.unsigned (Integers.Ptrofs.divs OFFSET (Integers.Ptrofs.repr 4))
                < (RTL.fn_stacksize f / 4))
        as NORMALISE_BOUND.
        { split.
          apply Integers.Ptrofs.unsigned_range_2.
          assert (forall x y, Integers.Ptrofs.divs x y = Integers.Ptrofs.divs x y ) by reflexivity.
          unfold Integers.Ptrofs.divs at 2 in H0.
          rewrite H0. clear H0.
          rewrite Integers.Ptrofs.unsigned_repr; simplify.
          rewrite Integers.Ptrofs.signed_repr; simplify; try lia.
          rewrite Z.quot_div_nonneg; try (rewrite <- HeqOFFSET; assumption); try lia.
          apply Zmult_lt_reg_r with (p := 4); try lia.
          repeat rewrite ZLib.div_mul_undo; try lia.
          rewrite Integers.Ptrofs.signed_repr.
          rewrite Z.quot_div_nonneg; try (rewrite <- HeqOFFSET; assumption); try lia.
          split.
          apply Z.div_pos; try (rewrite <- HeqOFFSET; assumption); try lia.
          apply Z.div_le_upper_bound; simplify; try (rewrite <- HeqOFFSET; lia); try lia.
          simplify; lia. }

        inversion NORMALISE_BOUND as [ NORMALISE_BOUND_LOW NORMALISE_BOUND_HIGH ];
        clear NORMALISE_BOUND.

        (** Start of proof proper *)
        eexists. split.
        eapply Smallstep.plus_one.
        eapply HTL.step_module; eauto.
        econstructor. econstructor. econstructor. simplify.
        econstructor. econstructor. econstructor. simplify.
        eapply Verilog.erun_Vbinop with (EQ := ?[EQ3]). (* FIXME: These will be shelved and cause sadness. *)
        eapply Verilog.erun_Vbinop with (EQ := ?[EQ4]).
        eapply Verilog.erun_Vbinop with (EQ := ?[EQ5]).
        econstructor. econstructor. econstructor. econstructor.
        econstructor.
        eapply Verilog.erun_Vbinop with (EQ := ?[EQ6]).
        econstructor. econstructor. econstructor. econstructor.
        econstructor. econstructor. econstructor. econstructor.
        econstructor. econstructor.

        all: simplify.

        (** Verilog array lookup *)
        unfold Verilog.arr_assocmap_lookup. setoid_rewrite H5.
        f_equal.

        (** State Lookup *)
        unfold Verilog.merge_regs.
        simplify.
        unfold_merge.
        rewrite AssocMap.gso.
        apply AssocMap.gss.
        apply st_greater_than_res.

        (** Match states *)
        rewrite assumption_32bit.
        econstructor; eauto.

        (** Match assocmaps *)
        unfold Verilog.merge_regs. simplify. unfold_merge.
        apply regs_lessdef_add_match.

        (** Equality proof *)
        assert (Integers.Ptrofs.repr 0 = Integers.Ptrofs.zero) as ZERO by reflexivity.
        rewrite ZERO in H7. clear ZERO.
        setoid_rewrite Integers.Ptrofs.add_zero_l in H7.

        specialize (H7 (Integers.Ptrofs.unsigned
                          (Integers.Ptrofs.divs
                             OFFSET
                             (Integers.Ptrofs.repr 4)))).

        exploit H7.
        rewrite Z2Nat.id; eauto.
        apply Z.div_pos; lia.

        intros I.
        assert (Z.to_nat
                  (Integers.Ptrofs.unsigned
                     (Integers.Ptrofs.divs
                        OFFSET
                        (Integers.Ptrofs.repr 4)))
                = valueToNat
                    (vdivs (vplus (vplus asr # r0 (ZToValue 32 z0) ?EQ5)
                                  (vmul asr # r1 (ZToValue 32 z) ?EQ6) ?EQ4) (ZToValue 32 4) ?EQ3))
          as EXPR_OK by admit.
        rewrite <- EXPR_OK.
        rewrite NORMALISE in I.
        rewrite H1 in I.
        invert I. assumption.

        (** PC match *)
        apply regs_lessdef_add_greater.
        apply greater_than_max_func.
        assumption.

        (** States well formed *)
        unfold state_st_wf. inversion 1. simplify.
        unfold Verilog.merge_regs.
        unfold_merge. rewrite AssocMap.gso.
        apply AssocMap.gss.
        apply st_greater_than_res.

        (** Match arrays *)
        econstructor.
        repeat split; simplify.
        unfold HTL.empty_stack.
        simplify.
        unfold Verilog.merge_arrs.

        rewrite AssocMap.gcombine.
        2: { reflexivity. }
        rewrite AssocMap.gss.
        unfold Verilog.merge_arr.
        setoid_rewrite H5.
        reflexivity.

        rewrite combine_length.
        unfold arr_repeat. simplify.
        rewrite list_repeat_len.
        reflexivity.

        unfold arr_repeat. simplify.
        rewrite list_repeat_len.
        congruence.

        intros.
        erewrite array_get_error_equal.
        eauto. apply combine_none.
        assumption.

        (** RSBP preservation *)
        unfold reg_stack_based_pointers. intros.
        destruct (Pos.eq_dec r2 dst); try rewrite e. (* FIXME: Prepare this for automation *)

        rewrite Registers.Regmap.gss.
        unfold arr_stack_based_pointers in ASBP.
        specialize (ASBP (Integers.Ptrofs.unsigned
                            (Integers.Ptrofs.divs OFFSET (Integers.Ptrofs.repr 4)))).
        exploit ASBP; auto; intros I.

        rewrite NORMALISE in I.
        assert (Integers.Ptrofs.repr 0 = Integers.Ptrofs.zero) as ZERO by reflexivity.
        rewrite ZERO in I. clear ZERO.
        simplify.
        rewrite Integers.Ptrofs.add_zero_l in I.
        rewrite H1 in I.
        assumption.
        simplify.

        rewrite Registers.Regmap.gso; auto.

      + invert MARR. simplify.

        unfold Op.eval_addressing in H0.
        destruct (Archi.ptr64) eqn:ARCHI; simplify.
        rewrite ARCHI in H0. simplify.

        unfold check_address_parameter in EQ; simplify.

        assert (Integers.Ptrofs.repr 0 = Integers.Ptrofs.zero) as ZERO by reflexivity.
        rewrite ZERO in H1. clear ZERO.
        rewrite Integers.Ptrofs.add_zero_l in H1.

        remember i0 as OFFSET.

        (** Read bounds assumption *)
        assert (0 <= Integers.Ptrofs.signed OFFSET) as READ_BOUND_LOW by admit.
        assert (Integers.Ptrofs.signed OFFSET < f.(RTL.fn_stacksize)) as READ_BOUND_HIGH by admit.

        (** Modular Preservation proof *)
        assert (Integers.Ptrofs.signed OFFSET mod 4 = 0) as MOD_PRESERVE by admit.

        (** Normalisation proof *)
        assert (Integers.Ptrofs.repr
                  (4 * Integers.Ptrofs.unsigned
                         (Integers.Ptrofs.divs OFFSET (Integers.Ptrofs.repr 4))) = OFFSET)
          as NORMALISE.
        { replace 4 with (Integers.Ptrofs.unsigned (Integers.Ptrofs.repr 4)) at 1 by reflexivity.
          rewrite <- PtrofsExtra.mul_unsigned.
          apply PtrofsExtra.mul_divs; auto.
          rewrite Integers.Ptrofs.signed_repr; simplify; lia. }

        (** Normalised bounds proof *)
        assert (0 <=
                Integers.Ptrofs.unsigned (Integers.Ptrofs.divs OFFSET (Integers.Ptrofs.repr 4))
                < (RTL.fn_stacksize f / 4))
        as NORMALISE_BOUND.
        { split.
          apply Integers.Ptrofs.unsigned_range_2.
          assert (forall x y, Integers.Ptrofs.divs x y = Integers.Ptrofs.divs x y ) by reflexivity.
          unfold Integers.Ptrofs.divs at 2 in H0.
          rewrite H0. clear H0.
          rewrite Integers.Ptrofs.unsigned_repr; simplify.
          rewrite Integers.Ptrofs.signed_repr; simplify; try lia.
          rewrite Z.quot_div_nonneg; try (rewrite <- HeqOFFSET; assumption); try lia.
          apply Zmult_lt_reg_r with (p := 4); try lia.
          repeat rewrite ZLib.div_mul_undo; try lia.
          rewrite Integers.Ptrofs.signed_repr.
          rewrite Z.quot_div_nonneg; try (rewrite <- HeqOFFSET; assumption); try lia.
          split.
          apply Z.div_pos; try (rewrite <- HeqOFFSET; assumption); try lia.
          apply Z.div_le_upper_bound; simplify; try (rewrite <- HeqOFFSET; lia); try lia.
          simplify; lia. }

        inversion NORMALISE_BOUND as [ NORMALISE_BOUND_LOW NORMALISE_BOUND_HIGH ];
        clear NORMALISE_BOUND.

        (** Start of proof proper *)
        eexists. split.
        eapply Smallstep.plus_one.
        eapply HTL.step_module; eauto.
        econstructor. econstructor. econstructor. simplify.
        econstructor. econstructor. econstructor. econstructor. simplify.

        all: simplify.

        (** Verilog array lookup *)
        unfold Verilog.arr_assocmap_lookup. setoid_rewrite H5.
        f_equal.

        (** State Lookup *)
        unfold Verilog.merge_regs.
        simplify.
        unfold_merge.
        rewrite AssocMap.gso.
        apply AssocMap.gss.
        apply st_greater_than_res.

        (** Match states *)
        rewrite assumption_32bit.
        econstructor; eauto.

        (** Match assocmaps *)
        unfold Verilog.merge_regs. simplify. unfold_merge.
        apply regs_lessdef_add_match.

        (** Equality proof *)
        assert (Integers.Ptrofs.repr 0 = Integers.Ptrofs.zero) as ZERO by reflexivity.
        rewrite ZERO in H7. clear ZERO.
        setoid_rewrite Integers.Ptrofs.add_zero_l in H7.

        specialize (H7 (Integers.Ptrofs.unsigned
                          (Integers.Ptrofs.divs
                             OFFSET
                             (Integers.Ptrofs.repr 4)))).

        exploit H7.
        rewrite Z2Nat.id; eauto.
        apply Z.div_pos; lia.

        intros I.
        assert (Z.to_nat
                  (Integers.Ptrofs.unsigned
                     (Integers.Ptrofs.divs
                        OFFSET
                        (Integers.Ptrofs.repr 4)))
                  =
                  valueToNat (ZToValue 32 (Integers.Ptrofs.unsigned OFFSET / 4)))
          as EXPR_OK by admit.
        rewrite <- EXPR_OK.
        rewrite NORMALISE in I.
        rewrite H1 in I.
        invert I. assumption.

        (** PC match *)
        apply regs_lessdef_add_greater.
        apply greater_than_max_func.
        assumption.

        (** States well formed *)
        unfold state_st_wf. inversion 1. simplify.
        unfold Verilog.merge_regs.
        unfold_merge. rewrite AssocMap.gso.
        apply AssocMap.gss.
        apply st_greater_than_res.

        (** Match arrays *)
        econstructor.
        repeat split; simplify.
        unfold HTL.empty_stack.
        simplify.
        unfold Verilog.merge_arrs.

        rewrite AssocMap.gcombine.
        2: { reflexivity. }
        rewrite AssocMap.gss.
        unfold Verilog.merge_arr.
        setoid_rewrite H5.
        reflexivity.

        rewrite combine_length.
        unfold arr_repeat. simplify.
        rewrite list_repeat_len.
        reflexivity.

        unfold arr_repeat. simplify.
        rewrite list_repeat_len.
        congruence.

        intros.
        erewrite array_get_error_equal.
        eauto. apply combine_none.
        assumption.

        (** RSBP preservation *)
        unfold reg_stack_based_pointers. intros.
        destruct (Pos.eq_dec r0 dst); try rewrite e. (* FIXME: Prepare this for automation *)

        rewrite Registers.Regmap.gss.
        unfold arr_stack_based_pointers in ASBP.
        specialize (ASBP (Integers.Ptrofs.unsigned
                            (Integers.Ptrofs.divs OFFSET (Integers.Ptrofs.repr 4)))).
        exploit ASBP; auto; intros I.

        rewrite NORMALISE in I.
        assert (Integers.Ptrofs.repr 0 = Integers.Ptrofs.zero) as ZERO by reflexivity.
        rewrite ZERO in I. clear ZERO.
        simplify.
        rewrite Integers.Ptrofs.add_zero_l in I.
        rewrite H1 in I.
        assumption.
        simplify.

        rewrite Registers.Regmap.gso; auto.

    - destruct c, chunk, addr, args; simplify; rt; simplify.
      + admit.
      + admit.
      + admit.

      (* + eexists. split. *)
      (*   eapply Smallstep.plus_one. *)
      (*   eapply HTL.step_module; eauto. *)
      (*   econstructor. econstructor. econstructor. simplify. *)
      + admit.

    - eexists. split. apply Smallstep.plus_one.
      eapply HTL.step_module; eauto.
      eapply Verilog.stmnt_runp_Vnonblock_reg with
          (rhsval := if b then posToValue 32 ifso else posToValue 32 ifnot).

      constructor.

      simpl.
      destruct b.
      eapply Verilog.erun_Vternary_true.
      eapply eval_cond_correct; eauto.
      constructor.
      apply boolToValue_ValueToBool.
      eapply Verilog.erun_Vternary_false.
      eapply eval_cond_correct; eauto.
      constructor.
      apply boolToValue_ValueToBool.
      constructor.
      unfold_merge.
      apply AssocMap.gss.

      destruct b.
      rewrite assumption_32bit.
      apply match_state.
      unfold_merge.
      apply regs_lessdef_add_greater. apply greater_than_max_func.
      assumption. assumption.

      unfold state_st_wf. intros. inversion H1.
      subst. unfold_merge.
      apply AssocMap.gss.
      assumption.

      assumption.

      rewrite assumption_32bit.
      apply match_state.
      unfold_merge.
      apply regs_lessdef_add_greater. apply greater_than_max_func. assumption.
      assumption.

      unfold state_st_wf. intros. inversion H1.
      subst. unfold_merge.
      apply AssocMap.gss.
      assumption.

      assumption.

    - (* Return *)
      econstructor. split.
      eapply Smallstep.plus_two.
      
      eapply HTL.step_module; eauto.
      constructor.
      econstructor; simpl; trivial.
      econstructor; simpl; trivial.
      constructor.
      econstructor; simpl; trivial.
      constructor.

      constructor. constructor.

      unfold_merge. simpl.
      rewrite AssocMap.gso.
      rewrite AssocMap.gso.
      unfold state_st_wf in WF. eapply WF. reflexivity.
      apply st_greater_than_res. apply st_greater_than_res.

      apply HTL.step_finish.
      unfold_merge; simpl.
      rewrite AssocMap.gso.
      apply AssocMap.gss.
      apply finish_not_return.
      apply AssocMap.gss.
      rewrite Events.E0_left. reflexivity.
      constructor.

      admit.
      constructor.

    - econstructor. split.
      eapply Smallstep.plus_two.
      eapply HTL.step_module; eauto.
      constructor.
      econstructor; simpl; trivial.
      econstructor; simpl; trivial.

      constructor. constructor.

      constructor.
      econstructor; simpl; trivial.
      apply Verilog.erun_Vvar. trivial.
      unfold_merge. simpl.
      rewrite AssocMap.gso.
      rewrite AssocMap.gso.
      unfold state_st_wf in WF. eapply WF. trivial.
      apply st_greater_than_res. apply st_greater_than_res. trivial.

      apply HTL.step_finish.
      unfold_merge.
      rewrite AssocMap.gso.
      apply AssocMap.gss.
      apply finish_not_return.
      apply AssocMap.gss.
      rewrite Events.E0_left. trivial.

      constructor.
      admit.

      simpl. inversion MASSOC. subst.
      unfold find_assocmap, AssocMapExt.get_default. rewrite AssocMap.gso.
      apply H1. eapply RTL.max_reg_function_use. eauto. simpl; tauto.
      apply st_greater_than_res.

    - inversion MSTATE; subst. inversion TF; subst.
      econstructor. split. apply Smallstep.plus_one.
      eapply HTL.step_call. simpl.

      constructor. apply regs_lessdef_add_greater.
      apply greater_than_max_func.
      apply init_reg_assoc_empty. assumption. unfold state_st_wf.
      intros. inv H1. apply AssocMap.gss. constructor.

      econstructor; simpl.
      reflexivity.
      rewrite AssocMap.gss. reflexivity.
      intros.
      destruct (Mem.load AST.Mint32 m' stk
                         (Integers.Ptrofs.unsigned (Integers.Ptrofs.add
                                                      Integers.Ptrofs.zero
                                                      (Integers.Ptrofs.repr ptr)))) eqn:LOAD.
      pose proof Mem.load_alloc_same as LOAD_ALLOC.
      pose proof H as ALLOC.
      eapply LOAD_ALLOC in ALLOC.
      2: { exact LOAD. }
      rewrite ALLOC.
      repeat constructor.
      constructor.

    - inversion MSTATE; subst. inversion MS; subst. econstructor.
      split. apply Smallstep.plus_one.
      constructor.

      constructor; auto. constructor; auto. apply regs_lessdef_add_match; auto.
      apply regs_lessdef_add_greater. apply greater_than_max_func. auto.

      unfold state_st_wf. intros. inv H. rewrite AssocMap.gso.
      rewrite AssocMap.gss. trivial. apply st_greater_than_res.

      Unshelve.
      exact (AssocMap.empty value).
      exact (AssocMap.empty value).
      exact (AssocMap.empty value).
      exact (AssocMap.empty value).
      (* exact (ZToValue 32 0). *)
      (* exact (AssocMap.empty value). *)
      (* exact (AssocMap.empty value). *)
      (* exact (AssocMap.empty value). *)
      (* exact (AssocMap.empty value). *)
  Admitted.
  Hint Resolve transl_step_correct : htlproof.

  (* Had to admit proof because currently there is no way to force main to be Internal. *)
  Lemma transl_initial_states :
    forall s1 : Smallstep.state (RTL.semantics prog),
      Smallstep.initial_state (RTL.semantics prog) s1 ->
      exists s2 : Smallstep.state (HTL.semantics tprog),
        Smallstep.initial_state (HTL.semantics tprog) s2 /\ match_states s1 s2.
  Proof.
    induction 1.
    exploit function_ptr_translated; eauto.
    intros [tf [A B]].
    unfold transl_fundef, Errors.bind in B.
    repeat (unfold_match B). inversion B. subst.
    econstructor; split. econstructor.
    apply (Genv.init_mem_transf_partial TRANSL); eauto.
    replace (AST.prog_main tprog) with (AST.prog_main prog).
    rewrite symbols_preserved; eauto.
    symmetry; eapply Linking.match_program_main; eauto.
    Admitted.
    (*eexact A. trivial.
    constructor.
    apply transl_module_correct. auto.
  Qed.*)
  Hint Resolve transl_initial_states : htlproof.

  Lemma transl_final_states :
    forall (s1 : Smallstep.state (RTL.semantics prog))
           (s2 : Smallstep.state (HTL.semantics tprog))
           (r : Integers.Int.int),
      match_states s1 s2 ->
      Smallstep.final_state (RTL.semantics prog) s1 r ->
      Smallstep.final_state (HTL.semantics tprog) s2 r.
  Proof.
    intros. inv H0. inv H. inv H4. inv MS. constructor. trivial.
  Qed.
  Hint Resolve transl_final_states : htlproof.

Theorem transf_program_correct:
  Smallstep.forward_simulation (RTL.semantics prog) (HTL.semantics tprog).
Proof.
  eapply Smallstep.forward_simulation_plus.
  apply senv_preserved.
  eexact transl_initial_states.
  eexact transl_final_states.
  exact transl_step_correct.
Qed.

End CORRECTNESS.