SCM Repository
Annotation of /sml/trunk/src/compiler/Execution/codeobj/code-obj.sml
Parent Directory
|
Revision Log
Revision 902 - (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 : | obj : Word8Array.array, | ||
17 : | name : string option | ||
18 : | } | ||
19 : | |||
20 : | type csegments = { | ||
21 : | c0 : code_object, | ||
22 : | cn : code_object list, | ||
23 : | data : Word8Vector.vector | ||
24 : | } | ||
25 : | |||
26 : | type executable = object -> object | ||
27 : | |||
28 : | (* raised by input when there are insufficient bytes *) | ||
29 : | exception FormatError | ||
30 : | |||
31 : | local | ||
32 : | structure CI = Unsafe.CInterface | ||
33 : | in | ||
34 : | val allocCode : (int * string option) -> W8A.array = | ||
35 : | CI.c_function "SMLNJ-RunT" "allocCode" | ||
36 : | val mkLiterals : W8V.vector -> object = CI.c_function "SMLNJ-RunT" "mkLiterals" | ||
37 : | val mkExec : W8A.array -> executable = CI.c_function "SMLNJ-RunT" "mkExec" | ||
38 : | end (* local *) | ||
39 : | |||
40 : | (* Allocate an unintialized code object of the given number of bytes. | ||
41 : | * The second argument is the optional name of the object. | ||
42 : | *) | ||
43 : | fun alloc (n, optName) = ( | ||
44 : | if (n <= 0) then raise Size else (); | ||
45 : | C{obj = allocCode (n, optName), name = optName}) | ||
46 : | |||
47 : | (* Allocate a code object of the given size and initialize it | ||
48 : | * from the input stream. The third argument is the optional | ||
49 : | * name of the object. | ||
50 : | * NOTE: someday, we might read the data directly into the code | ||
51 : | * object, but this will require hacking around with the reader. | ||
52 : | *) | ||
53 : | fun input (inStrm, sz, optName) = let | ||
54 : | val (co as C{obj, ...}) = alloc (sz, optName) | ||
55 : | val data = BinIO.inputN (inStrm, sz) | ||
56 : | in | ||
57 : | if (W8V.length data < sz) | ||
58 : | then ( | ||
59 : | Control_Print.say(concat[ | ||
60 : | "Bin file format error: expected ", Int.toString sz, | ||
61 : | " bytes, but only found ", Int.toString(W8V.length data) | ||
62 : | ]); | ||
63 : | raise FormatError) | ||
64 : | else (); | ||
65 : | W8A.copyVec{src=data, si=0, len=NONE, dst=obj, di=0}; | ||
66 : | co | ||
67 : | end | ||
68 : | |||
69 : | (* Output a code object to the given output stream *) | ||
70 : | fun output (outStrm, C{obj, ...}) = ( | ||
71 : | BinIO.output(outStrm, Unsafe.cast obj); | ||
72 : | BinIO.flushOut outStrm) | ||
73 : | |||
74 : | (* View the code object as an updatable array of bytes. *) | ||
75 : | fun bytes (C{obj, ...}) = obj | ||
76 : | |||
77 : | (* View the code object as an executable. This has the side-effect | ||
78 : | * of flushing the instruction cache. | ||
79 : | *) | ||
80 : | fun exec (C{obj, ...}) = mkExec obj | ||
81 : | |||
82 : | (* return the size of the code object *) | ||
83 : | fun size (C{obj, ...}) = W8A.length obj | ||
84 : | |||
85 : | end; | ||
86 : |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |