1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
|
(*
Copyright (c) 2000
Cambridge University Technical Services Limited
Further development copyright David C.J. Matthews 2000-2015
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*)
(*
TODO: This duplicates some of the Word structure although it adds certain
"unsafe" and Poly-specific functions. It probably should be rewritten now
that we have the standard basis library. DCJM June 2000.
*)
signature AddressSig =
sig
type machineWord
type address
type handler
type short = Word.word
val stringOfWord: machineWord -> string
val wordEq : 'a * 'a -> bool
val isShort : machineWord -> bool
exception Cast of string
val toMachineWord: 'a -> machineWord
val toShort: machineWord -> Word.word
val toAddress: machineWord -> address
val loadByte: (address * Word.word) -> Word8.word
val loadWord: (address * Word.word) -> machineWord
val assignByte: (address * Word.word * Word8.word) -> unit
val assignWord: (address * Word.word * machineWord) -> unit
val alloc: (short * Word8.word * machineWord) -> address
val maxAllocation: word
val lock: address -> unit
val length: address -> short
val flags: address -> Word8.word
val setFlags: address * Word8.word -> unit
val getFlags: address -> Word8.word
val wordSize: int
val F_words : Word8.word
val F_bytes : Word8.word
val F_code : Word8.word
val F_negative : Word8.word
val F_mutable : Word8.word
val F_gc : Word8.word
val F_noOverwrite : Word8.word
val F_weak : Word8.word
val F_profile : Word8.word
val isWords : address -> bool
val isBytes : address -> bool
val isCode : address -> bool
val isMutable:address -> bool
val call: (address * machineWord) -> machineWord
val isIoAddress : address -> bool
(* The following function is VERY unsafe and should only be
used by the Poly/ML code generator *)
val offsetAddr : address * short -> handler
end
structure Address :> AddressSig =
struct
type machineWord = word (* a legal ML object (tag = 0 or 1) *)
and address = word (* a normal pointer (tag = 0) *)
and handler = word (* pointer to exception handler (tag = 2!) *)
and short = word (* a 31/63-bit int (tag = 1) *)
open RuntimeCalls
(* pointer equality *)
val wordEq = PolyML.pointerEq
val unsafeCast : 'a -> 'b = RunCall.unsafeCast
val isShort : machineWord->bool = RunCall.run_call1 POLY_SYS_is_short
(* The following cast is always safe *)
val toMachineWord : 'a -> machineWord = unsafeCast
(* The following casts need checking *)
exception Cast of string
fun toAddress (w: machineWord) : address =
if isShort w then raise Cast "toAddress" else unsafeCast w
fun toShort (w: machineWord) : Word.word =
if isShort w then unsafeCast w else raise Cast "toShort"
(* Note:
assignByte should *not* be used with word-objects
(we might copy half a pointer into the object,
then call the garbage collector)
loadWord should *not* be used with byte-objects
(we might load something that's not a valid ML value,
then call the garbage collector)
Violating these assertions may corrupt the heap and cause unpredictable
behaviour.
It's safe to use assignWord with a byte-object or loadByte
with a word-object but it may not do what you expect.
One difference is that loadWord / assignWord leave the tag bits
unchanged but loadByte / assignByte strip and replace them.
Another difference is that the offset for the
"Word" functions is in words, whereas the offset for the
"Byte" functions is in bytes.
*)
val loadByte: (address * Word.word) -> Word8.word =
RunCall.run_call2 POLY_SYS_load_byte
val loadWord: (address * Word.word) -> machineWord =
RunCall.run_call2 POLY_SYS_load_word
val assignByte: (address * Word.word * Word8.word) -> unit =
RunCall.run_call3 POLY_SYS_assign_byte
val assignWord: (address * Word.word * machineWord) -> unit =
RunCall.run_call3 POLY_SYS_assign_word
val maxAllocation: word = RunCall.run_call2 POLY_SYS_process_env(100, ())
fun alloc(len: word, flags: Word8.word, initial: machineWord): address =
(* Zero sized-objects are not allowed. Check that the size is within
the acceptable range. *)
if len = 0w0 orelse len >= maxAllocation
then raise Size
else RunCall.run_call3 POLY_SYS_alloc_store(len, flags, initial)
val lock: address -> unit =
RunCall.run_call1 POLY_SYS_lockseg
val getFlags: address -> Word8.word =
RunCall.run_call1 POLY_SYS_get_flags
val setFlags: address * Word8.word -> unit =
RunCall.run_call2 POLY_SYS_code_flags
val wordSize: int =
RunCall.run_call0 POLY_SYS_bytes_per_word ()
val length: address -> Word.word =
RunCall.run_call1 POLY_SYS_get_length
val flags: address -> Word8.word =
RunCall.run_call1 POLY_SYS_get_flags
(* "call" added SPF 7/7/94, corrected 13/7/94, 8/9/94 *)
(* call takes the address of the code of a function [N.B. *NOT* *)
(* the closure for the function] that uses Poly parameter *)
(* conventions (values in registers e.g. an ML secondary entry *)
(* point) and applies it to the address of a single ML tuple. *)
(* N.B. it MUST be a tuple, even if there's only one parameter. *)
(* However, since unit is a "short" not an "address", the type *)
(* of the second parameter has to be "word" (not "address"). *)
(* The run-time system functions all use Poly convention. *)
val call: (address * machineWord) -> machineWord =
RunCall.run_call1 POLY_SYS_callcode_tupled
val F_words : Word8.word = 0wx00 (* word object - contains pointers and/or tagged values. *)
val F_bytes : Word8.word = 0wx01 (* byte object (contains no pointers) *)
val F_code : Word8.word = 0wx02 (* code object (mixed bytes and words) *)
val F_noOverwrite : Word8.word = 0wx08 (* don't overwrite when loading - mutables only. *)
val F_negative : Word8.word = 0wx10 (* sign bit for arbitrary precision ints (byte objects) *)
val F_profile : Word8.word = 0wx10 (* object has a profile pointer (word objects) *)
val F_weak : Word8.word = 0wx20 (* object contains weak references to option values. *)
val F_mutable : Word8.word = 0wx40 (* object is mutable *)
val F_gc : Word8.word = 0wx80 (* object is (pointer or depth) tombstone *)
local
val doCall: int*address -> bool
= RunCall.run_call2 RuntimeCalls.POLY_SYS_process_env
in
fun isIoAddress (a: address) : bool = doCall(102, a)
end
(* The following function is VERY unsafe and should only be *)
(* used by the Poly/ML code generator (ML version). *)
val offsetAddr : address * short -> handler =
RunCall.run_call2 POLY_SYS_offset_address
local
val typeMask : Word8.word = 0wx03
fun isType (t: Word8.word) (a: address):bool =
Word8.andb(flags a, typeMask) = t
in
val isWords = isType F_words
val isBytes = isType F_bytes
val isCode = isType F_code
(* The mutable flag may be used with any of the others. *)
fun isMutable a = Word8.andb(flags a, F_mutable) = F_mutable
end
local
val rtsTable =
[
(POLY_SYS_exit,"POLY_SYS_exit"),
(POLY_SYS_chdir,"POLY_SYS_chdir"),
(POLY_SYS_alloc_store,"POLY_SYS_alloc_store"),
(POLY_SYS_alloc_uninit,"POLY_SYS_alloc_uninit"),
(POLY_SYS_raisex,"POLY_SYS_raisex"),
(POLY_SYS_get_length,"POLY_SYS_get_length"),
(POLY_SYS_get_flags,"POLY_SYS_get_flags"),
(POLY_SYS_str_compare,"POLY_SYS_str_compare"),
(POLY_SYS_teststrgtr,"POLY_SYS_teststrgtr"),
(POLY_SYS_teststrlss,"POLY_SYS_teststrlss"),
(POLY_SYS_teststrgeq,"POLY_SYS_teststrgeq"),
(POLY_SYS_teststrleq,"POLY_SYS_teststrleq"),
(POLY_SYS_exception_trace,"POLY_SYS_exception_trace"),
(POLY_SYS_give_ex_trace,"POLY_SYS_give_ex_trace"),
(POLY_SYS_exception_trace_fn,"POLY_SYS_exception_trace_fn"),
(POLY_SYS_give_ex_trace_fn,"POLY_SYS_give_ex_trace_fn"),
(POLY_SYS_lockseg,"POLY_SYS_lockseg"),
(POLY_SYS_emptystring,"POLY_SYS_emptystring"),
(POLY_SYS_nullvector,"POLY_SYS_nullvector"),
(POLY_SYS_network,"POLY_SYS_network"),
(POLY_SYS_os_specific,"POLY_SYS_os_specific"),
(POLY_SYS_eq_longword,"POLY_SYS_eq_longword"),
(POLY_SYS_neq_longword,"POLY_SYS_neq_longword"),
(POLY_SYS_geq_longword,"POLY_SYS_geq_longword"),
(POLY_SYS_leq_longword,"POLY_SYS_leq_longword"),
(POLY_SYS_gt_longword,"POLY_SYS_gt_longword"),
(POLY_SYS_lt_longword,"POLY_SYS_lt_longword"),
(POLY_SYS_io_dispatch,"POLY_SYS_io_dispatch"),
(POLY_SYS_signal_handler,"POLY_SYS_signal_handler"),
(POLY_SYS_atomic_reset,"POLY_SYS_atomic_reset"),
(POLY_SYS_atomic_incr,"POLY_SYS_atomic_incr"),
(POLY_SYS_atomic_decr,"POLY_SYS_atomic_decr"),
(POLY_SYS_thread_self,"POLY_SYS_thread_self"),
(POLY_SYS_thread_dispatch,"POLY_SYS_thread_dispatch"),
(POLY_SYS_plus_longword,"POLY_SYS_plus_longword"),
(POLY_SYS_minus_longword,"POLY_SYS_minus_longword"),
(POLY_SYS_mul_longword,"POLY_SYS_mul_longword"),
(POLY_SYS_div_longword,"POLY_SYS_div_longword"),
(POLY_SYS_mod_longword,"POLY_SYS_mod_longword"),
(POLY_SYS_andb_longword,"POLY_SYS_andb_longword"),
(POLY_SYS_orb_longword,"POLY_SYS_orb_longword"),
(POLY_SYS_xorb_longword,"POLY_SYS_xorb_longword"),
(POLY_SYS_kill_self,"POLY_SYS_kill_self"),
(POLY_SYS_shift_left_longword,"POLY_SYS_shift_left_longword"),
(POLY_SYS_shift_right_longword,"POLY_SYS_shift_right_longword"),
(POLY_SYS_shift_right_arith_longword,"POLY_SYS_shift_right_arith_longword"),
(POLY_SYS_longword_to_tagged,"POLY_SYS_longword_to_tagged"),
(POLY_SYS_signed_to_longword,"POLY_SYS_signed_to_longword"),
(POLY_SYS_unsigned_to_longword,"POLY_SYS_unsigned_to_longword"),
(POLY_SYS_profiler,"POLY_SYS_profiler"),
(POLY_SYS_full_gc,"POLY_SYS_full_gc"),
(POLY_SYS_stack_trace,"POLY_SYS_stack_trace"),
(POLY_SYS_timing_dispatch,"POLY_SYS_timing_dispatch"),
(POLY_SYS_objsize,"POLY_SYS_objsize"),
(POLY_SYS_showsize,"POLY_SYS_showsize"),
(POLY_SYS_quotrem,"POLY_SYS_quotrem"),
(POLY_SYS_is_short,"POLY_SYS_is_short"),
(POLY_SYS_aplus,"POLY_SYS_aplus"),
(POLY_SYS_aminus,"POLY_SYS_aminus"),
(POLY_SYS_amul,"POLY_SYS_amul"),
(POLY_SYS_adiv,"POLY_SYS_adiv"),
(POLY_SYS_amod,"POLY_SYS_amod"),
(POLY_SYS_aneg,"POLY_SYS_aneg"),
(POLY_SYS_xora,"POLY_SYS_xora"),
(POLY_SYS_equala,"POLY_SYS_equala"),
(POLY_SYS_ora,"POLY_SYS_ora"),
(POLY_SYS_anda,"POLY_SYS_anda"),
(POLY_SYS_Real_str,"POLY_SYS_Real_str"),
(POLY_SYS_Real_geq,"POLY_SYS_Real_geq"),
(POLY_SYS_Real_leq,"POLY_SYS_Real_leq"),
(POLY_SYS_Real_gtr,"POLY_SYS_Real_gtr"),
(POLY_SYS_Real_lss,"POLY_SYS_Real_lss"),
(POLY_SYS_Real_eq,"POLY_SYS_Real_eq"),
(POLY_SYS_Real_neq,"POLY_SYS_Real_neq"),
(POLY_SYS_Real_Dispatch,"POLY_SYS_Real_Dispatch"),
(POLY_SYS_Add_real,"POLY_SYS_Add_real"),
(POLY_SYS_Sub_real,"POLY_SYS_Sub_real"),
(POLY_SYS_Mul_real,"POLY_SYS_Mul_real"),
(POLY_SYS_Div_real,"POLY_SYS_Div_real"),
(POLY_SYS_Abs_real,"POLY_SYS_Abs_real"),
(POLY_SYS_Neg_real,"POLY_SYS_Neg_real"),
(POLY_SYS_conv_real,"POLY_SYS_conv_real"),
(POLY_SYS_real_to_int,"POLY_SYS_real_to_int"),
(POLY_SYS_int_to_real,"POLY_SYS_int_to_real"),
(POLY_SYS_sqrt_real,"POLY_SYS_sqrt_real"),
(POLY_SYS_sin_real,"POLY_SYS_sin_real"),
(POLY_SYS_cos_real,"POLY_SYS_cos_real"),
(POLY_SYS_arctan_real,"POLY_SYS_arctan_real"),
(POLY_SYS_exp_real,"POLY_SYS_exp_real"),
(POLY_SYS_ln_real,"POLY_SYS_ln_real"),
(POLY_SYS_stdin,"POLY_SYS_stdin"),
(POLY_SYS_stdout,"POLY_SYS_stdout"),
(POLY_SYS_process_env,"POLY_SYS_process_env"),
(POLY_SYS_set_string_length,"POLY_SYS_set_string_length"),
(POLY_SYS_get_first_long_word,"POLY_SYS_get_first_long_word"),
(POLY_SYS_poly_specific,"POLY_SYS_poly_specific"),
(POLY_SYS_bytevec_eq,"POLY_SYS_bytevec_eq"),
(POLY_SYS_cmem_load_8,"POLY_SYS_cmem_load_8"),
(POLY_SYS_cmem_load_16, "POLY_SYS_cmem_load_16"),
(POLY_SYS_cmem_load_32,"POLY_SYS_cmem_load_32"),
(POLY_SYS_cmem_load_64,"POLY_SYS_cmem_load_64"),
(POLY_SYS_cmem_load_float,"POLY_SYS_cmem_load_float"),
(POLY_SYS_cmem_load_double, "POLY_SYS_cmem_load_double"),
(POLY_SYS_cmem_store_8, "POLY_SYS_cmem_store_8"),
(POLY_SYS_cmem_store_16, "POLY_SYS_cmem_store_16"),
(POLY_SYS_cmem_store_32, "POLY_SYS_cmem_store_32"),
(POLY_SYS_cmem_store_64, "POLY_SYS_cmem_store_64"),
(POLY_SYS_cmem_store_float, "POLY_SYS_cmem_store_float"),
(POLY_SYS_cmem_store_double, "POLY_SYS_cmem_store_double"),
(POLY_SYS_io_operation,"POLY_SYS_io_operation"),
(POLY_SYS_ffi, "POLY_SYS_ffi"),
(POLY_SYS_set_code_constant,"POLY_SYS_set_code_constant"),
(POLY_SYS_move_words,"POLY_SYS_move_words"),
(POLY_SYS_shift_right_arith_word,"POLY_SYS_shift_right_arith_word"),
(POLY_SYS_move_bytes,"POLY_SYS_move_bytes"),
(POLY_SYS_code_flags,"POLY_SYS_code_flags"),
(POLY_SYS_shrink_stack,"POLY_SYS_shrink_stack"),
(POLY_SYS_stderr,"POLY_SYS_stderr"),
(POLY_SYS_callcode_tupled,"POLY_SYS_callcode_tupled"),
(POLY_SYS_foreign_dispatch,"POLY_SYS_foreign_dispatch"),
(POLY_SYS_XWindows,"POLY_SYS_XWindows"),
(POLY_SYS_is_big_endian,"POLY_SYS_is_big_endian"),
(POLY_SYS_bytes_per_word,"POLY_SYS_bytes_per_word"),
(POLY_SYS_offset_address,"POLY_SYS_offset_address"),
(POLY_SYS_shift_right_word,"POLY_SYS_shift_right_word"),
(POLY_SYS_word_neq,"POLY_SYS_word_neq"),
(POLY_SYS_not_bool,"POLY_SYS_not_bool"),
(POLY_SYS_string_length,"POLY_SYS_string_length"),
(POLY_SYS_touch_final, "POLY_SYS_touch_final"),
(POLY_SYS_int_geq,"POLY_SYS_int_geq"),
(POLY_SYS_int_leq,"POLY_SYS_int_leq"),
(POLY_SYS_int_gtr,"POLY_SYS_int_gtr"),
(POLY_SYS_int_lss,"POLY_SYS_int_lss"),
(POLY_SYS_load_byte_immut,"POLY_SYS_load_byte_immut"),
(POLY_SYS_load_word_immut,"POLY_SYS_load_word_immut"),
(POLY_SYS_mul_word,"POLY_SYS_mul_word"),
(POLY_SYS_plus_word,"POLY_SYS_plus_word"),
(POLY_SYS_minus_word,"POLY_SYS_minus_word"),
(POLY_SYS_div_word,"POLY_SYS_div_word"),
(POLY_SYS_or_word,"POLY_SYS_or_word"),
(POLY_SYS_and_word,"POLY_SYS_and_word"),
(POLY_SYS_xor_word,"POLY_SYS_xor_word"),
(POLY_SYS_shift_left_word,"POLY_SYS_shift_left_word"),
(POLY_SYS_mod_word,"POLY_SYS_mod_word"),
(POLY_SYS_word_geq,"POLY_SYS_word_geq"),
(POLY_SYS_word_leq,"POLY_SYS_word_leq"),
(POLY_SYS_word_gtr,"POLY_SYS_word_gtr"),
(POLY_SYS_word_lss,"POLY_SYS_word_lss"),
(POLY_SYS_word_eq,"POLY_SYS_word_eq"),
(POLY_SYS_load_byte,"POLY_SYS_load_byte"),
(POLY_SYS_load_word,"POLY_SYS_load_word"),
(POLY_SYS_assign_byte,"POLY_SYS_assign_byte"),
(POLY_SYS_assign_word,"POLY_SYS_assign_word")
]
in
val rtsNames =
Vector.tabulate(256,
fn n => case List.find(fn (rtsNo, _) => rtsNo=n) rtsTable of
SOME(_, name) => name | _ => " RTS" ^ Int.toString n)
end
local
val doCall: int*machineWord -> string
= RunCall.run_call2 RuntimeCalls.POLY_SYS_process_env
in
fun functionName w = doCall(105, w)
end
fun stringOfWord w =
if isShort w
then "LIT" ^ Word.toString (unsafeCast w)
else
let
val v = toAddress w
in
if isIoAddress v
then (* RTS call - print the number. *)
let
val ioOp : int -> machineWord = RunCall.run_call1 RuntimeCalls.POLY_SYS_io_operation
fun matchIo n =
if n = 256 then raise Fail "Unknown RTS entry"
else if wordEq (w, ioOp n)
then Vector.sub(rtsNames, n)
else matchIo (n+1)
in
matchIo 0
end
else if isCode v
then "CODE \"" ^ functionName w ^ "\""
else if isBytes v
then
let
val length = Int.min(Word.toInt(length v) * wordSize, 16)
val data = Word8Vector.tabulate(length, fn n => loadByte(v, Word.fromInt n))
in
"BYTE data" ^ String.toString(Byte.bytesToString data)
end
else if isWords(toAddress w) andalso Word.toInt(length(toAddress w)) >= 1
then (* If it's the closure of a function try to print that. *)
let
val firstWord = loadWord(toAddress w, 0w0)
in
if not (isShort firstWord) andalso isCode(toAddress firstWord)
then "FUN \"" ^ functionName firstWord ^ "\"" (* Get the function name. *)
else "LIT <long word data>"
end
else "LIT <long word data>"
end
end;
(* Add a print function for machineWord. This is really only for
the debugger but prevents addresses being printed as Word.word values. *)
local
open PolyML Address
fun printMachineWord _ _ w = PrettyString(stringOfWord w)
in
val () = addPrettyPrinter printMachineWord
end;
|