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 /sml/trunk/src/compiler/Execution/codeobj/code-obj.sml
ViewVC logotype

Annotation of /sml/trunk/src/compiler/Execution/codeobj/code-obj.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 986 - (view) (download)

1 : blume 902 (* code-obj.sml
2 :     *
3 :     * COPYRIGHT (c) 1998 Bell Labs, Lucent Technologies.
4 :     *
5 :     * An interface for manipulating code objects.
6 :     *)
7 :    
8 :     structure CodeObj :> CODE_OBJ =
9 :     struct
10 :    
11 :     structure W8A = Word8Array
12 :     structure W8V = Word8Vector
13 :     type object = Unsafe.Object.object
14 :    
15 :     datatype code_object = C of {
16 : blume 986 obj : Word8Array.array
17 : blume 902 }
18 :    
19 :     type csegments = {
20 :     c0 : code_object,
21 :     cn : code_object list,
22 :     data : Word8Vector.vector
23 :     }
24 :    
25 :     type executable = object -> object
26 :    
27 :     (* raised by input when there are insufficient bytes *)
28 :     exception FormatError
29 :    
30 :     local
31 :     structure CI = Unsafe.CInterface
32 :     in
33 : blume 986 val allocCode : int -> W8A.array =
34 : blume 902 CI.c_function "SMLNJ-RunT" "allocCode"
35 :     val mkLiterals : W8V.vector -> object = CI.c_function "SMLNJ-RunT" "mkLiterals"
36 :     val mkExec : W8A.array -> executable = CI.c_function "SMLNJ-RunT" "mkExec"
37 :     end (* local *)
38 :    
39 : blume 986 (* Allocate an uninitialized code object.
40 : blume 902 *)
41 : blume 986 fun alloc n =
42 :     (if (n <= 0) then raise Size else ();
43 :     C { obj = allocCode n })
44 : blume 902
45 :     (* Allocate a code object of the given size and initialize it
46 : blume 986 * from the input stream.
47 : blume 902 * NOTE: someday, we might read the data directly into the code
48 :     * object, but this will require hacking around with the reader.
49 :     *)
50 : blume 986 fun input (inStrm, sz) = let
51 :     val (co as C{obj, ...}) = alloc sz
52 : blume 902 val data = BinIO.inputN (inStrm, sz)
53 :     in
54 :     if (W8V.length data < sz)
55 :     then (
56 :     Control_Print.say(concat[
57 :     "Bin file format error: expected ", Int.toString sz,
58 :     " bytes, but only found ", Int.toString(W8V.length data)
59 :     ]);
60 :     raise FormatError)
61 :     else ();
62 :     W8A.copyVec{src=data, si=0, len=NONE, dst=obj, di=0};
63 :     co
64 :     end
65 :    
66 :     (* Output a code object to the given output stream *)
67 :     fun output (outStrm, C{obj, ...}) = (
68 :     BinIO.output(outStrm, Unsafe.cast obj);
69 :     BinIO.flushOut outStrm)
70 :    
71 :     (* View the code object as an updatable array of bytes. *)
72 :     fun bytes (C{obj, ...}) = obj
73 :    
74 :     (* View the code object as an executable. This has the side-effect
75 :     * of flushing the instruction cache.
76 :     *)
77 :     fun exec (C{obj, ...}) = mkExec obj
78 :    
79 :     (* return the size of the code object *)
80 :     fun size (C{obj, ...}) = W8A.length obj
81 :    
82 :     end;

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