1structure export_codeLib :> export_codeLib =
2struct
3
4open HolKernel boolLib bossLib Parse;
5
6open wordsTheory wordsLib helperLib;
7
8fun write_code_to_file filename th = let
9  val _ = print ("Extracting machine code to file \"" ^ filename ^ "\", ")
10  fun int_to_hexstring n = "0x" ^ (Arbnum.toHexString o Arbnum.fromInt) n
11  val (m,pre,c,post) = (dest_spec o concl) th
12  val model_name = fst (dest_const m)
13  val cs = (list_dest pred_setSyntax.dest_insert c)
14  val _ = pred_setSyntax.is_empty (last cs) orelse (print ("\n\nCode does not end in empty set. Found:\n\n"^ (term_to_string (last cs)) ^"\n\n"); fail())
15  val cs = butlast cs
16  val vs = map (pairSyntax.dest_pair) cs
17  val vs = map (fn (x,y) => (x,fst (pairSyntax.dest_pair y) handle e => y)) vs
18  val vs = map (fn (x,y) => (if is_var x then ``0:num`` else
19                             if wordsSyntax.is_word_add x then cdr (cdr x) else
20                               (print ("\n\nBad address: "^(term_to_string x)^"\n\n"); fail()),y)) vs
21  val vs = map (fn (x,y) => (Arbnum.toInt (numSyntax.dest_numeral x),y)) vs
22  val vs = sort (fn (x,_) => fn (y:int,_) => x <= y) vs
23  fun del_repetations (x::y::xs) = if x = y then del_repetations (x::xs) else
24                                            x :: del_repetations (y::xs)
25    | del_repetations zs = zs
26  val vs = del_repetations vs
27  fun no_duplicates (x::y::xs) = if fst x = fst y then (print ("\n\nDuplication at " ^ (int_to_hexstring (fst x)) ^ ": " ^ (term_to_string (snd x)) ^ " and " ^ (term_to_string (snd y)) ^ "\n\n"); fail()) else no_duplicates (y::xs)
28    | no_duplicates _ = true
29  val _ = no_duplicates vs
30  fun term_byte_size tm =
31    if type_of tm = ``:word8`` then 1 else
32    if type_of tm = ``:word32`` then 4 else
33      foldr (fn (x,y) => x + y) 0 (map term_byte_size (fst (listSyntax.dest_list tm)))
34  fun no_holes i [] = true
35    | no_holes i ((j,c)::xs) =
36       if i = j then no_holes (i + (term_byte_size c)) xs else (print ("\n\nNo code at location: "^(int_to_hexstring i)^" (next instruction: "^(int_to_hexstring j)^")\n\n");fail())
37  val _ = no_holes 0 vs
38  val byte_count = ref 0;
39  val big_endian_format = ``[(w2w ((w:word32) >>> 24)):word8;
40                             (w2w ((w:word32) >>> 16)):word8;
41                             (w2w ((w:word32) >>> 8)):word8;
42                             (w2w ((w:word32) >>> 0)):word8]``
43  val little_endian_format = ``[(w2w ((w:word32) >>> 0)):word8;
44                                (w2w ((w:word32) >>> 8)):word8;
45                                (w2w ((w:word32) >>> 16)):word8;
46                                (w2w ((w:word32) >>> 24)):word8]``
47  val format_tm = if model_name = "PPC_MODEL" then big_endian_format else little_endian_format
48  val vs = map (fn (x,y) => (x,(cdr o concl o EVAL o subst [``w:word32``|->y]) format_tm handle e => y)) vs
49  fun fill c d n s = if size s < n then fill c d n (c ^ s ^ d) else s
50  fun word2hex tm = let
51    val _ = (byte_count := !byte_count + (if type_of tm = ``:word8`` then 1 else 4))
52    val s = Arbnum.toHexString (numSyntax.dest_numeral (cdr tm))
53    in "0x" ^ fill "0" "" (if type_of tm = ``:word8`` then 2 else
54                           if type_of tm = ``:word32`` then 8 else fail()) s end
55  fun word2string tm = let
56    val (tms,ty) = if listSyntax.is_list tm
57                   then listSyntax.dest_list tm
58                   else ([tm],type_of tm)
59    val str = if ty = ``:word8`` then "\t.byte\t" else
60              if ty = ``:word32`` then "\t.word\t" else fail()
61    fun foo [] = fail()
62      | foo [x] = word2hex x ^ "\n"
63      | foo (x::y::ys) = word2hex x ^ ", " ^ foo (y::ys)
64    in str ^ foo tms end
65  val rs = map (word2string o snd) vs
66  val instr_count = length rs
67  val size_count = (!byte_count)
68  val l1 = "Machine code automatically extracted from a HOL4 theorem."
69  val l2 = "The code consists of " ^ int_to_string instr_count ^ " instructions (" ^ int_to_string size_count ^ " bytes)."
70  val l3 = "End of automatically extracted machine code."
71  val o1 = "\t/*  " ^ l1 ^ "  */\n"
72  val o2 = "\t/*  " ^ fill "" " " (size l1) l2 ^ "  */\n"
73  val o3 = "\t/*  " ^ fill "" " " (size l1) l3 ^ "  */\n"
74  val output = ["\n","\n",o1,o2,"\n"] @ rs @ ["\n",o3,"\n"]
75  (* val _ = map print output *)
76  (* write to text file *)
77  val t = TextIO.openOut(filename)
78  val _ = map (fn s => TextIO.output(t,s)) output
79  val _ = TextIO.closeOut(t)
80  val _ = print "done.\n"
81  in () end;
82
83end
84