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 and Damien Doligez, 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 */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../LICENSE. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
#ifndef _mlvalues_
|
|
|
|
#define _mlvalues_
|
|
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "misc.h"
|
|
|
|
|
|
|
|
/* Definitions
|
|
|
|
|
|
|
|
word: Four bytes on 32 and 16 bit architectures,
|
|
|
|
eight bytes on 64 bit architectures.
|
|
|
|
long: A C long integer.
|
|
|
|
val: The ML representation of something. A long or a block or a pointer
|
|
|
|
outside the heap. If it is a block, it is the (encoded) address
|
|
|
|
of an object. If it is a long, it is encoded as well.
|
1998-10-26 11:19:32 -08:00
|
|
|
block: Something allocated. It always has a header and some
|
1995-05-04 03:15:53 -07:00
|
|
|
fields or some number of bytes (a multiple of the word size).
|
1998-10-26 11:19:32 -08:00
|
|
|
field: A word-sized val which is part of a block.
|
|
|
|
bp: Pointer to the first byte of a block. (a char *)
|
|
|
|
op: Pointer to the first field of a block. (a value *)
|
|
|
|
hp: Pointer to the header of a block. (a char *)
|
1995-05-04 03:15:53 -07:00
|
|
|
int32: Four bytes on all architectures.
|
2000-02-10 06:04:59 -08:00
|
|
|
int64: Eight bytes on all architectures.
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1998-10-26 11:19:32 -08:00
|
|
|
Remark: A block size is always a multiple of the word size, and at least
|
1995-05-04 03:15:53 -07:00
|
|
|
one word plus the header.
|
|
|
|
|
|
|
|
bosize: Size (in bytes) of the "bytes" part.
|
|
|
|
wosize: Size (in words) of the "fields" part.
|
1998-10-26 11:19:32 -08:00
|
|
|
bhsize: Size (in bytes) of the block with its header.
|
|
|
|
whsize: Size (in words) of the block with its header.
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
hd: A header.
|
|
|
|
tag: The value of the tag field of the header.
|
|
|
|
color: The value of the color field of the header.
|
|
|
|
This is for use only by the GC.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef long value;
|
|
|
|
typedef unsigned long header_t;
|
|
|
|
typedef unsigned long mlsize_t;
|
|
|
|
typedef unsigned int tag_t; /* Actually, an unsigned char */
|
|
|
|
typedef unsigned long color_t;
|
|
|
|
typedef unsigned long mark_t;
|
|
|
|
|
|
|
|
/* Longs vs blocks. */
|
1996-06-11 07:59:11 -07:00
|
|
|
#define Is_long(x) (((x) & 1) != 0)
|
1995-05-04 03:15:53 -07:00
|
|
|
#define Is_block(x) (((x) & 1) == 0)
|
|
|
|
|
|
|
|
/* Conversion macro names are always of the form "to_from". */
|
|
|
|
/* Example: Val_long as in "Val from long" or "Val of long". */
|
|
|
|
#define Val_long(x) (((long)(x) << 1) + 1)
|
|
|
|
#define Long_val(x) ((x) >> 1)
|
|
|
|
#define Max_long ((1L << (8 * sizeof(value) - 2)) - 1)
|
|
|
|
#define Min_long (-(1L << (8 * sizeof(value) - 2)))
|
2001-08-13 06:53:51 -07:00
|
|
|
#define Val_int(x) Val_long(x)
|
1995-05-04 03:15:53 -07:00
|
|
|
#define Int_val(x) ((int) Long_val(x))
|
|
|
|
|
|
|
|
/* Structure of the header:
|
|
|
|
|
|
|
|
For 16-bit and 32-bit architectures:
|
|
|
|
+--------+-------+-----+
|
|
|
|
| wosize | color | tag |
|
|
|
|
+--------+-------+-----+
|
|
|
|
bits 31 10 9 8 7 0
|
|
|
|
|
|
|
|
For 64-bit architectures:
|
|
|
|
|
|
|
|
+--------+-------+-----+
|
|
|
|
| wosize | color | tag |
|
|
|
|
+--------+-------+-----+
|
|
|
|
bits 63 10 9 8 7 0
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define Tag_hd(hd) ((tag_t) ((hd) & 0xFF))
|
|
|
|
#define Wosize_hd(hd) ((mlsize_t) ((hd) >> 10))
|
|
|
|
|
|
|
|
#define Hd_val(val) (((header_t *) (val)) [-1]) /* Also an l-value. */
|
|
|
|
#define Hd_op(op) (Hd_val (op)) /* Also an l-value. */
|
|
|
|
#define Hd_bp(bp) (Hd_val (bp)) /* Also an l-value. */
|
|
|
|
#define Hd_hp(hp) (* ((header_t *) (hp))) /* Also an l-value. */
|
|
|
|
#define Hp_val(val) ((char *) (((header_t *) (val)) - 1))
|
|
|
|
#define Hp_op(op) (Hp_val (op))
|
|
|
|
#define Hp_bp(bp) (Hp_val (bp))
|
|
|
|
#define Val_op(op) ((value) (op))
|
|
|
|
#define Val_hp(hp) ((value) (((header_t *) (hp)) + 1))
|
|
|
|
#define Op_hp(hp) ((value *) Val_hp (hp))
|
|
|
|
#define Bp_hp(hp) ((char *) Val_hp (hp))
|
|
|
|
|
|
|
|
#define Num_tags (1 << 8)
|
1996-07-01 05:43:28 -07:00
|
|
|
#ifdef ARCH_SIXTYFOUR
|
1995-05-04 03:15:53 -07:00
|
|
|
#define Max_wosize ((1L << 54) - 1)
|
|
|
|
#else
|
|
|
|
#define Max_wosize ((1 << 22) - 1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define Wosize_val(val) (Wosize_hd (Hd_val (val)))
|
|
|
|
#define Wosize_op(op) (Wosize_val (op))
|
|
|
|
#define Wosize_bp(bp) (Wosize_val (bp))
|
|
|
|
#define Wosize_hp(hp) (Wosize_hd (Hd_hp (hp)))
|
|
|
|
#define Whsize_wosize(sz) ((sz) + 1)
|
|
|
|
#define Wosize_whsize(sz) ((sz) - 1)
|
|
|
|
#define Wosize_bhsize(sz) ((sz) / sizeof (value) - 1)
|
|
|
|
#define Bsize_wsize(sz) ((sz) * sizeof (value))
|
|
|
|
#define Wsize_bsize(sz) ((sz) / sizeof (value))
|
|
|
|
#define Bhsize_wosize(sz) (Bsize_wsize (Whsize_wosize (sz)))
|
|
|
|
#define Bhsize_bosize(sz) ((sz) + sizeof (header_t))
|
|
|
|
#define Bosize_val(val) (Bsize_wsize (Wosize_val (val)))
|
|
|
|
#define Bosize_op(op) (Bosize_val (Val_op (op)))
|
|
|
|
#define Bosize_bp(bp) (Bosize_val (Val_bp (bp)))
|
|
|
|
#define Bosize_hd(hd) (Bsize_wsize (Wosize_hd (hd)))
|
|
|
|
#define Whsize_hp(hp) (Whsize_wosize (Wosize_hp (hp)))
|
|
|
|
#define Whsize_val(val) (Whsize_hp (Hp_val (val)))
|
|
|
|
#define Whsize_bp(bp) (Whsize_val (Val_bp (bp)))
|
|
|
|
#define Whsize_hd(hd) (Whsize_wosize (Wosize_hd (hd)))
|
|
|
|
#define Bhsize_hp(hp) (Bsize_wsize (Whsize_hp (hp)))
|
|
|
|
#define Bhsize_hd(hd) (Bsize_wsize (Whsize_hd (hd)))
|
|
|
|
|
1996-07-01 05:43:28 -07:00
|
|
|
#ifdef ARCH_BIG_ENDIAN
|
1995-05-04 03:15:53 -07:00
|
|
|
#define Tag_val(val) (((unsigned char *) (val)) [-1])
|
|
|
|
/* Also an l-value. */
|
|
|
|
#define Tag_hp(hp) (((unsigned char *) (hp)) [sizeof(value)-1])
|
|
|
|
/* Also an l-value. */
|
|
|
|
#else
|
|
|
|
#define Tag_val(val) (((unsigned char *) (val)) [-sizeof(value)])
|
|
|
|
/* Also an l-value. */
|
|
|
|
#define Tag_hp(hp) (((unsigned char *) (hp)) [0])
|
|
|
|
/* Also an l-value. */
|
|
|
|
#endif
|
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
/* The lowest tag for blocks containing no value. */
|
1995-07-28 06:59:46 -07:00
|
|
|
#define No_scan_tag 251
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
|
|
|
|
/* 1- If tag < No_scan_tag : a tuple of fields. */
|
|
|
|
|
|
|
|
/* Pointer to the first field. */
|
|
|
|
#define Op_val(x) ((value *) (x))
|
|
|
|
/* Fields are numbered from 0. */
|
|
|
|
#define Field(x, i) (((value *)(x)) [i]) /* Also an l-value. */
|
|
|
|
|
|
|
|
typedef int32 opcode_t;
|
|
|
|
typedef opcode_t * code_t;
|
|
|
|
|
1995-07-28 06:59:46 -07:00
|
|
|
/* Special case of tuples of fields: closures */
|
|
|
|
|
|
|
|
#define Closure_tag 250
|
1995-05-04 03:15:53 -07:00
|
|
|
#define Code_val(val) (((code_t *) (val)) [0]) /* Also an l-value. */
|
|
|
|
|
1995-07-28 06:59:46 -07:00
|
|
|
/* If tag == Infix_tag : an infix header inside a closure */
|
|
|
|
/* Infix_tag must be odd so that the infix header is scanned as an integer */
|
1998-10-26 11:19:32 -08:00
|
|
|
/* Infix_tag must be 1 modulo 4 and infix headers can only occur in blocks
|
1997-05-13 07:45:38 -07:00
|
|
|
with tag Closure_tag (see compact.c). */
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1995-07-28 06:59:46 -07:00
|
|
|
#define Infix_tag 249
|
1995-07-10 02:48:27 -07:00
|
|
|
#define Infix_offset_hd(hd) (Bosize_hd(hd))
|
|
|
|
#define Infix_offset_val(v) Infix_offset_hd(Hd_val(v))
|
|
|
|
|
1997-05-11 15:42:38 -07:00
|
|
|
/* Another special case: objects */
|
|
|
|
#define Object_tag 248
|
2001-08-13 06:53:51 -07:00
|
|
|
#define Class_val(val) Field((val), 0)
|
|
|
|
#define Oid_val(val) Long_val(Field((val), 1))
|
1997-05-11 15:42:38 -07:00
|
|
|
|
2000-04-07 07:43:31 -07:00
|
|
|
/* Another special case: variants */
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLextern value hash_variant(char * tag);
|
2000-04-07 07:43:31 -07:00
|
|
|
|
1999-10-14 06:51:43 -07:00
|
|
|
/* 2- If tag >= No_scan_tag : a sequence of bytes. */
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
/* Pointer to the first byte */
|
|
|
|
#define Bp_val(v) ((char *) (v))
|
|
|
|
#define Val_bp(p) ((value) (p))
|
|
|
|
/* Bytes are numbered from 0. */
|
|
|
|
#define Byte(x, i) (((char *) (x)) [i]) /* Also an l-value. */
|
|
|
|
#define Byte_u(x, i) (((unsigned char *) (x)) [i]) /* Also an l-value. */
|
|
|
|
|
|
|
|
/* Abstract things. Their contents is not traced by the GC; therefore they
|
|
|
|
must not contain any [value].
|
|
|
|
*/
|
1995-07-28 06:59:46 -07:00
|
|
|
#define Abstract_tag 251
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
/* Strings. */
|
1995-07-28 06:59:46 -07:00
|
|
|
#define String_tag 252
|
1995-05-04 03:15:53 -07:00
|
|
|
#define String_val(x) ((char *) Bp_val(x))
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLextern mlsize_t string_length (value);
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
/* Floating-point numbers. */
|
1995-07-28 06:59:46 -07:00
|
|
|
#define Double_tag 253
|
1995-05-04 03:15:53 -07:00
|
|
|
#define Double_wosize ((sizeof(double) / sizeof(value)))
|
1996-07-01 05:43:28 -07:00
|
|
|
#ifndef ARCH_ALIGN_DOUBLE
|
1995-07-27 10:41:09 -07:00
|
|
|
#define Double_val(v) (* (double *)(v))
|
|
|
|
#define Store_double_val(v,d) (* (double *)(v) = (d))
|
1995-05-04 03:15:53 -07:00
|
|
|
#else
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLextern double Double_val (value);
|
|
|
|
CAMLextern void Store_double_val (value,double);
|
1995-05-04 03:15:53 -07:00
|
|
|
#endif
|
|
|
|
|
1995-07-27 10:41:09 -07:00
|
|
|
/* Arrays of floating-point numbers. */
|
1995-07-28 06:59:46 -07:00
|
|
|
#define Double_array_tag 254
|
1995-07-27 10:41:09 -07:00
|
|
|
#define Double_field(v,i) Double_val((value)((double *)(v) + (i)))
|
2001-08-13 06:53:51 -07:00
|
|
|
#define Store_double_field(v,i,d) do{ \
|
|
|
|
mlsize_t caml__temp_i = (i); \
|
2001-08-27 06:46:45 -07:00
|
|
|
double caml__temp_d = (d); \
|
2001-08-13 06:53:51 -07:00
|
|
|
Store_double_val((value)((double *) v + caml__temp_i), caml__temp_d); \
|
|
|
|
}while(0)
|
1995-07-27 10:41:09 -07:00
|
|
|
|
2000-02-10 06:04:59 -08:00
|
|
|
/* Custom blocks. They contain a pointer to a "method suite"
|
|
|
|
of functions (for finalization, comparison, hashing, etc)
|
|
|
|
followed by raw data. The contents of custom blocks is not traced by
|
|
|
|
the GC; therefore, they must not contain any [value].
|
|
|
|
See [custom.h] for operations on method suites. */
|
|
|
|
#define Custom_tag 255
|
2001-08-13 06:53:51 -07:00
|
|
|
#define Data_custom_val(v) ((void *) &Field((v), 1))
|
2000-02-10 06:04:59 -08:00
|
|
|
struct custom_operations; /* defined in [custom.h] */
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2000-02-11 07:09:27 -08:00
|
|
|
/* Int32.t, Int64.t and Nativeint.t are represented as custom blocks. */
|
2000-02-11 04:03:31 -08:00
|
|
|
|
|
|
|
#define Int32_val(v) (*((int32 *) Data_custom_val(v)))
|
2000-02-11 07:09:27 -08:00
|
|
|
#define Nativeint_val(v) (*((long *) Data_custom_val(v)))
|
2000-03-10 09:36:31 -08:00
|
|
|
#ifndef ARCH_ALIGN_INT64
|
|
|
|
#define Int64_val(v) (*((int64 *) Data_custom_val(v)))
|
|
|
|
#else
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLextern int64 Int64_val(value v);
|
2000-03-10 09:36:31 -08:00
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
/* 3- Atoms are 0-tuples. They are statically allocated once and for all. */
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLextern header_t atom_table[];
|
2001-08-13 06:53:51 -07:00
|
|
|
#define Atom(tag) (Val_hp (&(atom_table [(tag)])))
|
1995-07-13 02:02:41 -07:00
|
|
|
|
1995-09-27 03:47:23 -07:00
|
|
|
/* Is_atom tests whether a well-formed block is statically allocated
|
|
|
|
outside the heap. For the bytecode system, only zero-sized block (Atoms)
|
1996-02-20 03:00:21 -08:00
|
|
|
fall in this class. For the native-code generator, data
|
|
|
|
emitted by the code generator (as described in the table
|
|
|
|
caml_data_segments) are also atoms. */
|
1995-07-13 02:02:41 -07:00
|
|
|
|
1995-09-27 03:47:23 -07:00
|
|
|
#ifndef NATIVE_CODE
|
|
|
|
#define Is_atom(v) ((v) >= Atom(0) && (v) <= Atom(255))
|
|
|
|
#else
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLextern char * static_data_start, * static_data_end;
|
1996-02-20 03:00:21 -08:00
|
|
|
#define Is_atom(v) \
|
|
|
|
((((char *)(v) >= static_data_start && (char *)(v) < static_data_end) || \
|
|
|
|
((v) >= Atom(0) && (v) <= Atom(255))))
|
1995-09-27 03:47:23 -07:00
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1995-06-18 07:44:56 -07:00
|
|
|
/* Booleans are integers 0 or 1 */
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1995-06-18 07:44:56 -07:00
|
|
|
#define Val_bool(x) Val_int((x) != 0)
|
|
|
|
#define Bool_val(x) Int_val(x)
|
|
|
|
#define Val_false Val_int(0)
|
|
|
|
#define Val_true Val_int(1)
|
1999-11-08 09:08:57 -08:00
|
|
|
#define Val_not(x) (Val_false + Val_true - (x))
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1999-11-08 09:08:57 -08:00
|
|
|
/* The unit value is 0 (tagged) */
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1995-06-18 07:44:56 -07:00
|
|
|
#define Val_unit Val_int(0)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-01-05 05:55:13 -08:00
|
|
|
/* List constructors */
|
|
|
|
#define Val_emptylist Val_int(0)
|
|
|
|
#define Tag_cons 0
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
/* The table of global identifiers */
|
|
|
|
|
|
|
|
extern value global_data;
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* _mlvalues_ */
|