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