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