PreviousUpNext

15.4.41  src/app/heap2asm/heap2asm.pkg

## heap2asm.pkg

# Compiled by:
#     src/app/heap2asm/heap2asm.lib

#   Generating an assembly code file corresponding to a heap image.

stipulate
    package fil =  file__premicrothread;                                # file__premicrothread  is from   src/lib/std/src/posix/file--premicrothread.pkg
herein

    package main: (weak) api {
         main: (String, List( String )) -> winix__premicrothread::process::Status;
    }
    {
        nnn = 20;

        fun one (inf, outf)
            =
            {   my (si, so)
                    =
                    (   fil::open_for_read  inf,
                        fil::open_for_write outf
                    );

                fun out s
                    =
                    fil::write (so, s);

                fun finish n
                    =
                    {   out ".text\n\t.align 2\n_lib7_heap_image_len:\n\t.long ";
                        out (int::to_string n);
                        out "\n";
                    };

                fun line l
                    =
                    {   bl = map
                                 (int::to_string o char::to_int)
                                 (string::explode l);

                        out ("\t.byte " + string::join ", " bl + "\n");
                    };

                fun lines n
                    =
                    case (fil::read_n (si, nnn))
                        #
                        ""  => finish n;

                        l   => {   s = size l;
                                    line l;
                                    if (s < nnn ) finish (n+s);
                                               else lines  (n+s);fi;
                                }; esac;

               out "\t.globl _lib7_heap_image\n\
                   \\t.globl _lib7_heap_image_len\n\
                   \.text\n\t.align 2\n\
                   \_lib7_heap_image:\n";

               lines 0;

               fil::close_input  si;
               fil::close_output so;
            };

        fun complain (p, s)
            =
            {    fil::write (fil::stderr, cat [p, ": ", s, "\n"]);
                 winix__premicrothread::process::failure;
            };

        fun main (p, [inf, outf])
                =>
                {   one (inf, outf);
                    winix__premicrothread::process::success;
                }
                except
                    e =  complain (p, "exception: " + exceptions::exception_message e);


           main (p, _)
                =>
                complain (p, "usage: " + p + " heapfile asmfile");
        end;
    };
end;

## Copyright (c) 2005 by The Fellowship of SML/NJ
## Author: Matthias Blume (blume@tti-c.org)
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext