Chibi-scheme source code analysis-type system

Source: Internet
Author: User

In the implementation of this scheme language, everything is an object, which is its type system.

An object may be an immediate value or a sexp, because it identifies the real type of an object through various tags.

Now, you only need to understand that on a 32-bit machine, the object is a 32-bit black box.

Either the 32-bit itself stores its real data, which is the immediate value.

This is either a pointer or sexp.

Part 1: immediate (immediate value)

Immediate values include integers, Char, and unique immediate symbols.

We know that one byte is 8 bits, so the last three bits of a pointer must be 0. The number of immediate bits is used to take the three bits as the tag bit.

An object, that is, a 32-bit object, first identifies its last three bits. If it is not all 0, it is an immediate value; otherwise it is a pointer.

Annotations in file./include/Chibi/sexp. h

 
/*Tagging System
* Bits end in 00: pointer
* 01: fixnum
* 011: immediate flonum (optional)
* 111: immediate symbol (optional)
* 000110: Char
* 001110: Unique immediate (null, true, false)
*/

Therefore, it is easy to know whether an object is an immediate value.

 # Define Sexp_fixnum_mask 3
# Define Sexp_immediate_mask 7
# Define Sexp_extended_mask 63

# Define Sexp_pointer_tag 0
# Define Sexp_fixnum_tag 1
# Define Sexp_isymbol_tag 7
# Define Sexp_char_tag 6

# Define Sexp_pointerp (x) (sexp_uint_t) (x) & sexp_fixnum_mask) = sexp_pointer_tag)
# Define Sexp_fixnump (x) (sexp_uint_t) (x) & sexp_fixnum_mask) = sexp_fixnum_tag)
# Define Sexp_isymbolp (x) (sexp_uint_t) (x) & sexp_immediate_mask) = sexp_isymbol_tag)
# Define Sexp_charp (x) (sexp_uint_t) (x) & sexp_extended_mask) = sexp_char_tag)

Sexp_uint_t is an unsigned int, and a 32-bit machine is a 32-bit thing.

Here, the integer only uses 30 digits, that is, the value range is 0-2, and the power of 30 minus 1 (if there is no character). In most cases, the number of 30 digits is sufficient.

In addition, Chibi-scheme also implements a large number to support a larger number. For details, seeSource code, This article does not make specific analysis.

Part 2: sexp (s expression)

If an object is not an immediate value (the last 3bit is not 0), it is a sexp.

TypedefStructSexp_struct * sexp;

Sexp_struct is composed of a header with a Union value of various types. The header tag field is used to determine the type, and the markedp and other fields are set for garbage collection.

 
StructSexp_struct {
Sexp_tag_t tag;
CharMarkedp;
......
Union {
DoubleFlonum;
Struct{
Sexp car, CDR;
Sexp source;
} Pair;
Struct{
Sexp_uint_t length;
Sexp data [];
} Vector;
......
} Value;
};

In addition to the immediate value, all Chibi-scheme types are abstracted into sexp, not limited to the basic data types defined by scheme, but also include runtime types such as heap and context, there are also ast tree structures such as sexp_cnd, sexp_ref, sexp_set, sexp_seq, sexp_limit, and so on, which are all sexp.

The tag of sexp_tag_t can be enumerated as follows:

EnumSexp_types {
Sexp_object,
Sexp_type,
Sexp_fixnum,
Sexp_number,
Sexp_char,
Sexp_boolean,
......
Sexp_iport,
Sexp_oport,
Sexp_exception,
Sexp_procedure,
Sexp_macro,
Sexp_synclo,
Sexp_env,
Sexp_bytecode,
Sexp_core,
Sexp_opcode,
Sexp_lambda,
Sexp_cnd,
Sexp_ref,
Sexp_set,
Sexp_seq,
Sexp_timeout,
Sexp_stack,
Sexp_context,
Sexp_cpointer,
Sexp_num_core_types
};
Part 3: sexp operations

To facilitate operations,./include/Chibi/sexp. H provides many macros, including type predicate judgment, space allocation, and access macros for various types of domain members.

Some of the complicated ones are just a function declaration in sexp. H, which is implemented in sexp. C and GC. C.

There are also some arithmetic operation macros for scheme objects and macros for various types of size calculation. For example, the following are some of the sourceCodeParts:

 # Define Sexp_check_tag (x, t) (sexp_pointerp (x) & (sexp_pointer_tag (x) = (t )))
# Define Sexp_typep (x) (sexp_check_tag (x, sexp_type ))
# Define Sexp_pairp (x) (sexp_check_tag (x, sexp_pair ))
# Define Sexp_stringp (x) (sexp_check_tag (x, sexp_string ))
...
# Define Sexp_field (x, type, ID, field) (x)-> value. type. Field)
# Define Sexp_pred_field (x, type, Pred, field) (x)-> value. type. Field)

# Define Sexp_vector_length (x) (sexp_field (x, vector, sexp_vector, length ))
# Define Sexp_vector_data (x) (sexp_field (x, vector, sexp_vector, data ))
...
# Define Sexp_string_length (x) (sexp_field (x, String, sexp_string, length ))
# Define Sexp_string_data (x) (sexp_field (x, String, sexp_string, data ))
...

Note that the data part of sexp is defined by union, and the memory size occupied by union is the largest small size member. Will this cause a great waste of memory?

Later I noticed that in actual memory allocation, the sizeof (union) size is not allocated, but the actual size of each type is allocated.

 
# DefineSexp_sizeof (x) (offsetof (struct sexp_struct, value )\
+Sizeof(Sexp)0)-> Value. X ))

For example, if sexp_make_flonum is used to create a floating point object, its call chain is as follows:

Sexp_make_flonum (sexp CTX, double F)

==> Sexp_alloc_type (CTX, flonum, sexp_flonum)

==> Sexp_alloc_tagged (CTX,Sexp_sizeof (flonum), Sexp_flonum)

==> Sexp_alloc_tagged_aux

==> Sexp_alloc (CTX, size)

The Declaration of the above functions is in sexp. h. The implementation of the sexp_alloc function is in file GC. C, and its function is to allocate size byte space in the context.

Part 4: Type object

Scheme is a dynamic type language, which is called "dynamic". In my understanding, there is no type for the symbol itself, but the value bound to the symbol is typed. A symbol can be bound to any type of value, so it is dynamic. Here, we may use more accurate English words. The symbols I call are more likely to be identifier, symbol, or even name, but not variable.

 
(Define a 100); A is a symbol bound to the integer value of 100.
(Define a "hello World"); A is bound to a string. Note that a itself is not of the type.

The value is of type. The value 100 is an integer. The value "Hello world" is a string. It is obviously not enough to obtain all runtime type information for only one tag field. For example, I want to know the actual bytes occupied by an object. I can easily get the object tag through macro sexp_pointer_tag. However, if you want to use macro sexp_sizeof (TAG) to get the size, an error will be reported because the parameter must be of a type such as flonum, but the tag is only a number. In this case, A tag is only a flonum number.

Naturally, a global table is required to associate the type number with the actual information of the type.

The objects in this table are type objects, which are used to record the specific information of each type. In Chibi-scheme, this design does exist.

One of the union objects in sexp is the type object:

 
StructSexp_struct {
...
Union {
...
StructSexp_type_struct type;
...
} Values;
};

The structure sexp_type_struct is used to record various types of information:

StructSexp_type_struct {
Sexp_tag_t tag;
ShortField_base, field_eq_len_base, field_len_base, field_len_off;
UnsignedShortField_len_scale;
ShortSize_base, size_off;
UnsignedShortSize_scale;
ShortWeak_base, weak_len_base, weak_len_off, weak_len_scale, weak_len_extra;
ShortDepth;
Sexp name, CPL, slots, DL, ID, print;
Sexp_proc2 finalize;
};

This table actually exists. It is actually context-> globals [sexp_g_types]. Here we simply use table for representation.

For example, assume that there is an object X, first obtain its tag through sexp_pointer_tag (x), and then the type object of X can be found through table [tag.

Then you can perform operations on this type of object to know all the type information of X. These operations are encapsulated.

# DefineSexp_object_type (CTX, x) (sexp_type_by_index (CTX, (x)-> tag )))
# DefineSexp_type_by_index (CTX, I) (sexp_context_types (CTX) [I])
# DefineSexp_context_types (CTX) sexp_vector_data (sexp_global (CTX, sexp_g_types ))
# DefineSexp_global (CTX, x) (sexp_vector_data (sexp_context_globals (CTX) [x])
Summary

This article describes the Chibi-scheme type system. The main Code corresponds to the file./include/Chibi/sexp. h.

The type system is very important. All scheme objects are essentially a 32-bit black box, which is either an immediate value or an s expression.

Some bits in the immediate value can be used as an identifier to identify which immediate value is, while the s expression is a pointer to the struct. the type of the tag contained in the struct header is determined.

The macro provided in sexp. H provides excellent support for operating s expressions.ProgramIt is also the header file to be included in the entire program.

These operations include types of judgment predicates, sizes, access macros for various types of domains, and creation of various types.

It is not enough to retrieve all runtime type information by TAG alone. Corresponding, a type object stores a variety of types of information, including the type name, the number of bytes occupied by the type, and the type alignment, whether GC calls a destructor or not. There is also a table that stores various types of objects. With this table, you can associate tags with type objects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.