Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /heap2asm/trunk/heap2asm.sml
ViewVC logotype

Annotation of /heap2asm/trunk/heap2asm.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6109 - (view) (download)

1 : mblume 1747 (* heap2asm.sml
2 :     *
3 : jhr 6104 * COPYRIGHT (c) 2020 The Fellowship of SML/NJ (http://www.smlnj.org)
4 :     * All rights reserved.
5 : mblume 1747 *
6 : jhr 6104 * Generating an assembly code file corresponding to a heap image.
7 : mblume 1747 *
8 : jhr 6104 * NOTE: there is an argument that this program should be part of the
9 :     * runtime system, because it would make getting the correct assembly
10 :     * directives right easier.
11 : mblume 1747 *)
12 : jhr 6104
13 :     structure Main : sig
14 :    
15 : mblume 1747 val main: string * string list -> OS.Process.status
16 :    
17 : jhr 6104 end = struct
18 : mblume 1747
19 : jhr 6104 (* number of bytes per line *)
20 :     val bytesPerLine = 20
21 :    
22 : jhr 6109 (* FIXME: getHostSize is deprecated and should be replaced with getArchSize *)
23 :     val size64 = (SMLofNJ.SysInfo.getHostSize() = 64)
24 : jhr 6104
25 :     (* assembly directives *)
26 :     val textSection = "\t.text\n"
27 :     val alignCode = if size64 then "\t.p2align 4\n" else "\t.p2align 3\n"
28 : jhr 6109 val alignData = "\t.p2align 2\n"
29 :     fun long n = concat ["\t.long ", Int.toString n, "\n"]
30 : jhr 6104 fun global lab = concat["\t.globl ", lab, "\n"]
31 :     fun label s = s ^ ":\n"
32 :    
33 :     fun doFile (inf, outf) = let
34 :     val inS = BinIO.openIn inf
35 :     val outS = TextIO.openOut outf
36 :     fun out s = TextIO.output (outS, s)
37 :     fun finish nb = (
38 :     out textSection;
39 :     out alignData;
40 :     out (label "_smlnj_heap_image_len");
41 : jhr 6109 out (long nb))
42 : jhr 6104 fun lineOut bytes = let
43 :     fun b2s b = Word8.fmt StringCvt.DEC b
44 :     in
45 :     out (concat[
46 :     "\t.byte ",
47 :     String.concatWithMap "," b2s (Word8Vector.toList bytes),
48 :     "\n"
49 :     ])
50 : mblume 1747 end
51 : jhr 6104 fun lines nb = let
52 :     val bytes = BinIO.inputN (inS, bytesPerLine)
53 :     in
54 :     case Word8Vector.length bytes
55 :     of 0 => finish nb
56 :     | n => (
57 :     lineOut bytes;
58 :     if (n < bytesPerLine)
59 :     then finish (nb + n)
60 :     else lines (nb + n))
61 :     (* end case *)
62 :     end
63 :     in
64 :     out (global "_smlnj_heap_image");
65 :     out (global "_smlnj_heap_image_len");
66 :     out textSection;
67 :     out alignCode;
68 :     out (label "_smlnj_heap_image");
69 :     lines 0;
70 :     BinIO.closeIn inS;
71 :     TextIO.closeOut outS
72 :     end
73 : mblume 1747
74 : jhr 6104 fun complain (p, s) = (
75 :     TextIO.output (TextIO.stdErr, concat [p, ": ", s, "\n"]);
76 :     OS.Process.failure)
77 : mblume 1747
78 : jhr 6104 fun main (p, [inf, outf]) = (
79 :     (doFile (inf, outf); OS.Process.success)
80 :     handle e => complain (p, "exception: " ^ General.exnMessage e))
81 : mblume 1747 | main (p, _) = complain (p, "usage: " ^ p ^ " heapfile asmfile")
82 : jhr 6104
83 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0