1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* To assign numbers to globals and primitives *)
|
|
|
|
|
|
|
|
open Misc
|
|
|
|
open Asttypes
|
|
|
|
open Lambda
|
|
|
|
open Emitcode
|
|
|
|
|
|
|
|
|
|
|
|
(* Functions for batch linking *)
|
|
|
|
|
|
|
|
type error =
|
|
|
|
Undefined_global of string
|
|
|
|
| Unavailable_primitive of string
|
1998-04-14 07:48:34 -07:00
|
|
|
| Wrong_vm of string
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
exception Error of error
|
|
|
|
|
|
|
|
(* Tables for numbering objects *)
|
|
|
|
|
|
|
|
type 'a numtable =
|
|
|
|
{ num_cnt: int; (* The next number *)
|
|
|
|
num_tbl: ('a, int) Tbl.t } (* The table of already numbered objects *)
|
|
|
|
|
|
|
|
let empty_numtable = { num_cnt = 0; num_tbl = Tbl.empty }
|
|
|
|
|
|
|
|
let find_numtable nt key =
|
|
|
|
Tbl.find key nt.num_tbl
|
|
|
|
|
|
|
|
let enter_numtable nt key =
|
|
|
|
let n = !nt.num_cnt in
|
|
|
|
nt := { num_cnt = n + 1; num_tbl = Tbl.add key n !nt.num_tbl };
|
|
|
|
n
|
|
|
|
|
|
|
|
let incr_numtable nt =
|
|
|
|
let n = !nt.num_cnt in
|
|
|
|
nt := { num_cnt = n + 1; num_tbl = !nt.num_tbl };
|
|
|
|
n
|
|
|
|
|
|
|
|
(* Global variables *)
|
|
|
|
|
|
|
|
let global_table = ref(empty_numtable : Ident.t numtable)
|
|
|
|
and literal_table = ref([] : (int * structured_constant) list)
|
|
|
|
|
|
|
|
let slot_for_getglobal id =
|
|
|
|
try
|
|
|
|
find_numtable !global_table id
|
|
|
|
with Not_found ->
|
|
|
|
raise(Error(Undefined_global(Ident.name id)))
|
|
|
|
|
|
|
|
let slot_for_setglobal id =
|
|
|
|
enter_numtable global_table id
|
|
|
|
|
|
|
|
let slot_for_literal cst =
|
|
|
|
let n = incr_numtable global_table in
|
|
|
|
literal_table := (n, cst) :: !literal_table;
|
|
|
|
n
|
|
|
|
|
|
|
|
(* The C primitives *)
|
|
|
|
|
|
|
|
let c_prim_table = ref(empty_numtable : string numtable)
|
|
|
|
|
1998-11-05 00:00:35 -08:00
|
|
|
let set_prim_table name =
|
1999-02-24 07:21:50 -08:00
|
|
|
ignore(enter_numtable c_prim_table name)
|
1998-11-05 00:00:35 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let num_of_prim name =
|
|
|
|
try
|
|
|
|
find_numtable !c_prim_table name
|
|
|
|
with Not_found ->
|
|
|
|
if !Clflags.custom_runtime
|
|
|
|
then enter_numtable c_prim_table name
|
|
|
|
else raise(Error(Unavailable_primitive name))
|
|
|
|
|
1995-11-05 09:32:12 -08:00
|
|
|
let require_primitive name =
|
1999-02-24 07:21:50 -08:00
|
|
|
if name.[0] <> '%' then ignore(num_of_prim name)
|
1995-11-05 09:32:12 -08:00
|
|
|
|
1997-06-13 08:48:53 -07:00
|
|
|
let all_primitives () =
|
1996-04-22 04:15:41 -07:00
|
|
|
let prim = Array.create !c_prim_table.num_cnt "" in
|
1995-05-04 03:15:53 -07:00
|
|
|
Tbl.iter (fun name number -> prim.(number) <- name) !c_prim_table.num_tbl;
|
1997-06-13 08:48:53 -07:00
|
|
|
prim
|
|
|
|
|
|
|
|
let output_primitive_names outchan =
|
|
|
|
let prim = all_primitives() in
|
|
|
|
for i = 0 to Array.length prim - 1 do
|
|
|
|
output_string outchan prim.(i); output_char outchan '\000'
|
|
|
|
done
|
|
|
|
|
|
|
|
open Printf
|
|
|
|
|
|
|
|
let output_primitive_table outchan =
|
|
|
|
let prim = all_primitives() in
|
1998-11-20 07:36:00 -08:00
|
|
|
fprintf outchan "\
|
|
|
|
#ifdef __cplusplus\n\
|
|
|
|
extern \"C\" {\n\
|
|
|
|
#endif\n";
|
1995-05-04 03:15:53 -07:00
|
|
|
for i = 0 to Array.length prim - 1 do
|
1996-11-07 02:56:52 -08:00
|
|
|
fprintf outchan "extern long %s();\n" prim.(i)
|
1995-05-04 03:15:53 -07:00
|
|
|
done;
|
1996-11-07 02:56:52 -08:00
|
|
|
fprintf outchan "typedef long (*primitive)();\n";
|
|
|
|
fprintf outchan "primitive cprim[] = {\n";
|
1995-05-04 03:15:53 -07:00
|
|
|
for i = 0 to Array.length prim - 1 do
|
1996-11-07 02:56:52 -08:00
|
|
|
fprintf outchan " %s,\n" prim.(i)
|
1995-05-04 03:15:53 -07:00
|
|
|
done;
|
1996-11-07 02:56:52 -08:00
|
|
|
fprintf outchan " (primitive) 0 };\n";
|
|
|
|
fprintf outchan "char * names_of_cprim[] = {\n";
|
1995-05-04 03:15:53 -07:00
|
|
|
for i = 0 to Array.length prim - 1 do
|
1996-11-07 02:56:52 -08:00
|
|
|
fprintf outchan " \"%s\",\n" prim.(i)
|
1995-05-04 03:15:53 -07:00
|
|
|
done;
|
1998-11-20 07:36:00 -08:00
|
|
|
fprintf outchan " (char *) 0 };\n";
|
|
|
|
fprintf outchan "\
|
|
|
|
#ifdef __cplusplus\n\
|
|
|
|
}\n\
|
|
|
|
#endif\n"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Initialization for batch linking *)
|
|
|
|
|
|
|
|
let init () =
|
|
|
|
(* Enter the predefined exceptions *)
|
|
|
|
Array.iter
|
|
|
|
(fun name ->
|
|
|
|
let id =
|
|
|
|
try List.assoc name Predef.builtin_values
|
|
|
|
with Not_found -> fatal_error "Symtable.init" in
|
|
|
|
let c = slot_for_setglobal id in
|
|
|
|
let cst = Const_block(0, [Const_base(Const_string name)]) in
|
|
|
|
literal_table := (c, cst) :: !literal_table)
|
|
|
|
Runtimedef.builtin_exceptions;
|
|
|
|
(* Enter the known C primitives *)
|
1998-10-02 05:40:44 -07:00
|
|
|
if String.length !Clflags.use_prims > 0 then begin
|
|
|
|
let ic = open_in !Clflags.use_prims in
|
|
|
|
try
|
|
|
|
while true do
|
1998-11-05 00:00:35 -08:00
|
|
|
set_prim_table (input_line ic)
|
1998-10-02 05:40:44 -07:00
|
|
|
done
|
|
|
|
with End_of_file -> close_in ic
|
|
|
|
| x -> close_in ic; raise x
|
|
|
|
end else if String.length !Clflags.use_runtime > 0 then begin
|
1998-04-14 07:48:34 -07:00
|
|
|
let primfile = Filename.temp_file "camlprims" "" in
|
|
|
|
try
|
|
|
|
if Sys.command(Printf.sprintf "%s -p > %s"
|
|
|
|
!Clflags.use_runtime primfile) <> 0
|
|
|
|
then raise(Error(Wrong_vm !Clflags.use_runtime));
|
|
|
|
let ic = open_in primfile in
|
|
|
|
try
|
|
|
|
while true do
|
1998-11-05 00:00:35 -08:00
|
|
|
set_prim_table (input_line ic)
|
1998-04-14 07:48:34 -07:00
|
|
|
done
|
|
|
|
with End_of_file -> close_in ic
|
|
|
|
| x -> close_in ic; raise x
|
|
|
|
with x -> remove_file primfile; raise x
|
1998-10-02 05:40:44 -07:00
|
|
|
end else begin
|
1998-11-05 00:00:35 -08:00
|
|
|
Array.iter set_prim_table
|
1998-10-02 05:40:44 -07:00
|
|
|
Runtimedef.builtin_primitives
|
1998-04-14 07:48:34 -07:00
|
|
|
end
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Relocate a block of object bytecode *)
|
|
|
|
|
|
|
|
(* Must use the unsafe String.set here because the block may be
|
|
|
|
a "fake" string as returned by Meta.static_alloc. *)
|
1996-09-24 08:45:14 -07:00
|
|
|
|
1996-09-19 05:56:54 -07:00
|
|
|
let patch_int buff pos n =
|
1995-05-04 03:15:53 -07:00
|
|
|
String.unsafe_set buff pos (Char.unsafe_chr n);
|
1996-09-19 05:56:54 -07:00
|
|
|
String.unsafe_set buff (pos + 1) (Char.unsafe_chr (n asr 8));
|
|
|
|
String.unsafe_set buff (pos + 2) (Char.unsafe_chr (n asr 16));
|
|
|
|
String.unsafe_set buff (pos + 3) (Char.unsafe_chr (n asr 24))
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let patch_object buff patchlist =
|
|
|
|
List.iter
|
|
|
|
(function
|
|
|
|
(Reloc_literal sc, pos) ->
|
1996-09-19 05:56:54 -07:00
|
|
|
patch_int buff pos (slot_for_literal sc)
|
1995-05-04 03:15:53 -07:00
|
|
|
| (Reloc_getglobal id, pos) ->
|
1996-09-19 05:56:54 -07:00
|
|
|
patch_int buff pos (slot_for_getglobal id)
|
1995-05-04 03:15:53 -07:00
|
|
|
| (Reloc_setglobal id, pos) ->
|
1996-09-19 05:56:54 -07:00
|
|
|
patch_int buff pos (slot_for_setglobal id)
|
1995-05-04 03:15:53 -07:00
|
|
|
| (Reloc_primitive name, pos) ->
|
1996-09-19 05:56:54 -07:00
|
|
|
patch_int buff pos (num_of_prim name))
|
1995-05-04 03:15:53 -07:00
|
|
|
patchlist
|
|
|
|
|
|
|
|
(* Translate structured constants *)
|
|
|
|
|
|
|
|
let rec transl_const = function
|
|
|
|
Const_base(Const_int i) -> Obj.repr i
|
|
|
|
| Const_base(Const_char c) -> Obj.repr c
|
|
|
|
| Const_base(Const_string s) -> Obj.repr s
|
|
|
|
| Const_base(Const_float f) -> Obj.repr(float_of_string f)
|
1995-07-02 09:45:21 -07:00
|
|
|
| Const_pointer i -> Obj.repr i
|
1995-05-04 03:15:53 -07:00
|
|
|
| Const_block(tag, fields) ->
|
|
|
|
let block = Obj.new_block tag (List.length fields) in
|
|
|
|
let pos = ref 0 in
|
|
|
|
List.iter
|
|
|
|
(fun c -> Obj.set_field block !pos (transl_const c); incr pos)
|
|
|
|
fields;
|
|
|
|
block
|
1995-07-27 10:40:34 -07:00
|
|
|
| Const_float_array fields ->
|
1998-04-06 02:15:55 -07:00
|
|
|
Obj.repr(Array.of_list(List.map (fun f -> float_of_string f) fields))
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Build the initial table of globals *)
|
|
|
|
|
|
|
|
let initial_global_table () =
|
1996-04-22 04:15:41 -07:00
|
|
|
let glob = Array.create !global_table.num_cnt (Obj.repr 0) in
|
1995-05-04 03:15:53 -07:00
|
|
|
List.iter
|
|
|
|
(fun (slot, cst) -> glob.(slot) <- transl_const cst)
|
|
|
|
!literal_table;
|
|
|
|
literal_table := [];
|
|
|
|
glob
|
|
|
|
|
|
|
|
(* Save the table of globals *)
|
|
|
|
|
|
|
|
let output_global_map oc =
|
1995-08-09 06:17:15 -07:00
|
|
|
output_value oc !global_table
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Functions for toplevel use *)
|
|
|
|
|
|
|
|
(* Update the in-core table of globals *)
|
|
|
|
|
|
|
|
let update_global_table () =
|
|
|
|
let ng = !global_table.num_cnt in
|
1996-09-19 05:56:54 -07:00
|
|
|
if ng > Array.length(Meta.global_data()) then Meta.realloc_global_data ng;
|
1995-05-04 03:15:53 -07:00
|
|
|
let glob = Meta.global_data() in
|
|
|
|
List.iter
|
|
|
|
(fun (slot, cst) -> glob.(slot) <- transl_const cst)
|
|
|
|
!literal_table;
|
|
|
|
literal_table := []
|
|
|
|
|
|
|
|
(* Initialize the linker for toplevel use *)
|
|
|
|
|
|
|
|
let init_toplevel () =
|
|
|
|
(* Read back the known global symbols from the executable file *)
|
|
|
|
let ic = open_in_bin Sys.argv.(0) in
|
|
|
|
let pos_trailer =
|
1998-04-14 07:48:34 -07:00
|
|
|
in_channel_length ic - 24 - String.length Config.exec_magic_number in
|
1995-05-04 03:15:53 -07:00
|
|
|
seek_in ic pos_trailer;
|
1998-04-14 07:48:34 -07:00
|
|
|
let path_size = input_binary_int ic in
|
1995-05-04 03:15:53 -07:00
|
|
|
let code_size = input_binary_int ic in
|
1997-06-15 04:57:23 -07:00
|
|
|
let prim_size = input_binary_int ic in
|
1995-05-04 03:15:53 -07:00
|
|
|
let data_size = input_binary_int ic in
|
|
|
|
let symbol_size = input_binary_int ic in
|
|
|
|
let debug_size = input_binary_int ic in
|
|
|
|
seek_in ic (pos_trailer - debug_size - symbol_size);
|
|
|
|
global_table := (input_value ic : Ident.t numtable);
|
|
|
|
close_in ic;
|
|
|
|
(* Enter the known C primitives *)
|
1998-11-05 00:00:35 -08:00
|
|
|
Array.iter set_prim_table
|
1997-10-31 04:58:12 -08:00
|
|
|
(Meta.available_primitives())
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1995-05-05 03:05:18 -07:00
|
|
|
(* Find the value of a global identifier *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-11-29 10:36:42 -08:00
|
|
|
let get_global_position id = slot_for_getglobal id
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let get_global_value id =
|
|
|
|
(Meta.global_data()).(slot_for_getglobal id)
|
1996-11-29 10:36:42 -08:00
|
|
|
let assign_global_value id v =
|
1995-05-04 03:15:53 -07:00
|
|
|
(Meta.global_data()).(slot_for_getglobal id) <- v
|
|
|
|
|
|
|
|
(* Save and restore the current state *)
|
|
|
|
|
|
|
|
type global_map = Ident.t numtable
|
|
|
|
|
|
|
|
let current_state () = !global_table
|
|
|
|
|
1996-09-24 08:45:14 -07:00
|
|
|
let restore_state st = global_table := st
|
|
|
|
|
|
|
|
let hide_additions st =
|
|
|
|
if st.num_cnt > !global_table.num_cnt then
|
|
|
|
fatal_error "Symtable.hide_additions";
|
|
|
|
global_table :=
|
|
|
|
{ num_cnt = !global_table.num_cnt;
|
|
|
|
num_tbl = st.num_tbl }
|
|
|
|
|
|
|
|
(* "Filter" the global map according to some predicate.
|
|
|
|
Used to expunge the global map for the toplevel. *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let filter_global_map p gmap =
|
|
|
|
let newtbl = ref Tbl.empty in
|
|
|
|
Tbl.iter
|
|
|
|
(fun id num -> if p id then newtbl := Tbl.add id num !newtbl)
|
|
|
|
gmap.num_tbl;
|
|
|
|
{num_cnt = gmap.num_cnt; num_tbl = !newtbl}
|
|
|
|
|
|
|
|
(* Error report *)
|
|
|
|
|
1999-11-08 09:06:33 -08:00
|
|
|
open Formatmsg
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let report_error = function
|
|
|
|
Undefined_global s ->
|
|
|
|
print_string "Reference to undefined global `"; print_string s;
|
|
|
|
print_string "'"
|
|
|
|
| Unavailable_primitive s ->
|
|
|
|
print_string "The external function `"; print_string s;
|
|
|
|
print_string "' is not available"
|
1998-04-14 07:48:34 -07:00
|
|
|
| Wrong_vm s ->
|
|
|
|
print_string "Cannot find or execute the runtime system ";
|
|
|
|
print_string s
|