· Blog · 12 min read
Type Definition of AQ Virtual Machine - AQ
Since different systems, hardware and other external conditions have different support and definitions for memory, it is necessary to design a unified type standard in order to make AQ virtual machine meet the requirements of cross-platform operation. This article defines and standardizes the type of AQ virtual machine to ensure that AQ virtual machines on different systems can run normally.
Introduction
Since different systems
, hardware
and other external conditions have different support and definitions for memory
, it is necessary to design a unified type
standard in order to make AQ virtual machine
meet the requirements of cross-platform operation. This article defines and specifies the type
of the AQ virtual machine
to ensure that the AQ virtual machine
on different systems can run normally.
Design ideas
First, in order to achieve the simplification of type
and higher operation efficiency, the design of native types
(types
that are directly supported by the virtual machine without code definition) should be as few as possible. Therefore, for related complex types, such as enumerations
and structures
, we develop them at the compiler
level to reduce the number and complexity
of types
of the virtual machine
.
According to the definition of
type
inAqvmMemory_Memory
inmemory.h
, eachuint8_t
stores2
types, so the number of
typesshould be between
0x00-
0x0F` (16).
Secondly, through the study of types
in other programming languages
, we summarized the common types
and designed the following types
to achieve a balance between performance and simplicity in virtual machines
.
- null - empty type
- byte -
1
byte signed integer type - int -
4
byte signed integer type - long -
8
byte signed integer type - float -
4
byte single-precision floating-point type - double -
8
byte double-precision floating-point type
Finally, we designed a detailed standard
for types
to ensure that AQ virtual machines
can run cross-platform
.
In order to reduce the type definitions of
virtual machines
,unsigned types
will be implemented at thecompiler
level.
Type
definitions of other programming languages
In order to make AQ
’s type
more extensive and easy for developers to master, we refer to the existing type
definitions of common programming languages
.
Here, the basic types
in the following text are defined as general data
types such as integers, floating-point numbers, and null types. They undertake basic data storage work or have special significance.
C
The current C
standard is ISO/IEC 9899:2018 Information technology — Programming languages — C
. Since the copyright of this standard belongs to ISO (International Organization for Standardization)
and IEC (International Electrotechnical Commission)
, in order to avoid copyright disputes, we have summarized the type
definitions in it. The same below.
Official website: https://www.iso.org/standard/74528.html
_Bool
- Objects declared as_Bool
type are large enough to store values of0
and1
.(unsigned) char
- character type. An object declared as typechar
is large enough to store any member of the basic execution character set. If a member of the basic execution character set is stored in achar
object, its value is guaranteed to be non-negative. If any other character is stored in a character object, the resulting value isimplementation-defned
, but shall be in the range of values representable in the type.signed char
- signed character type.short int
- extended signed integer type.unsigned short int
- extended unsigned integer type.int
- extended standard signed integer type.unsigned int
- standard unsigned integer type.long int
- extended signed integer type.unsigned long int
- extended unsigned integer type.long long int
- extended signed integer type.unsigned long long int
- extended unsigned integer type.float
- floating-point type. The value set of thefloat
type is a subset of the set of values of thedouble
type.double
- floating-point type. The value set of thedouble
type is a subset of the set of values of thelong double
type.long double
- floating-point type.void
- Thevoid
type contains a set of empty values; it is an incomplete object type and cannot be implemented.
In addition, C
has other non-basic types
, such as enumeration
types (enum
types), pointer types, etc. They are not discussed in the design of the virtual machine
.
C++
and other C
variants
The current C++
standard is ISO/IEC 14882:2020 Programming languages — C++
. Since the types of C++
and other C
variants are basically the same as those of C
, they are no longer listed.
Python
The latest official version of Python
is 3.12.4
. The Built-in Types
in the Python 3.12.4 Documentation
documents the standard types built into the Python
interpreter.
The main built-in types are numbers
, sequences
, maps
, classes
, instances
, and exceptions
. Due to space constraints, the contents other than basic types
are not discussed here.
Source link: https://docs.python.org/zh-cn/3/library/stdtypes.html
int
- IntegerInteger
has unlimited precision. Unmodified integer literals (including hexadecimal, octal, and binary numbers) produce integers.float
- Floating-point numbersFloating-point numbers
are usually implemented inC
usingdouble
. Numeric literals containing a decimal point or an exponential sign produce a floating point number.complex
- Complex numbers Complex numbers have a real part and an imaginary part, each of which is a floating point number. Addingj
orJ
to a numeric literal produces an imaginary number (a complex number with a zero real part), which can be added to an integer or a floating point number to produce a complex number with both a real part and an imaginary part.bool
- BooleanBoolean
is also asubtype
ofinteger
. A Boolean object representing a truth value. Thebool
type has only two constant instances:True
andFalse
.list
- ListLists
are mutable sequences, often used to storecollections
of items of the same kind (where the exact degree of similarity will vary depending on the application).tuple
- Tuple A tuple is an immutable sequence, often used to store multi-tuples of heterogeneous data (e.g., tuples produced by theenumerate()
built-in function). Tuples are also used in situations where an immutable sequence of homogeneous data is needed (e.g., to allow storage into instances ofset
ordict
).range
- Therange
type represents an immutable sequence of numbers, often used to iterate a specified number of times in afor
loop.str
- Text sequence type Text data is handled in Python using thestr
object, also known as astring
. Astring
is animmutable sequence
ofUnicode
code points.bytes
- Thebytes
object is animmutable sequence
of single bytes. Since many major binary protocols are based on theASCII
text encoding, thebytes
object provides somemethods
that are only available when dealing withASCII
compatible data, and are closely related to thestring
object in many features.bytearray
- Thebytearray
object is the mutable counterpart of thebytes
object.memoryview
- Memory view Thememoryview
object allowsPython
code to access the internal data of an object as long as the object supports thebuffer protocol
without copying.set
- Set type Theset
object is anunordered multi-item set
of uniquehashable
objects. Common uses include membership testing, removing duplicates from a sequence, and set-like calculations in mathematics, such asintersection
,union
,difference
, andsymmetric difference
. Theset
type is mutable and its contents can be changed using methods such asadd()
andremove()
. As amutable type
, it has nohash value
and cannot be used as adictionary
keyor an
setelement
.frozenset
- Set Types Thefrozenset
type isimmutable
andhashable
, its contents cannot be changed after creation, so it can be used askeys
ofdictionaries
orelements
of othersets
.dict
- Mapping Typesmapping
objects maphashable
values to arbitrary objects.Mappings
aremutable objects
. Currently there is only one standard mapping type,dict
.GenericAlias
-GenericAlias
objects are usually created byextracting
aclass
. They are most commonly used forcontainer classes
, such aslist
ordict
. For example, thelist[int]
GenericAlias
object is created by extracting thelist
class with anint
parameter. The main purpose ofGenericAlias
objects is fortype annotations
.union
-Union objects
contain the values after performing| (bitwise or)
operations on multipletype objects
. These types are mainly used fortype annotations
. Compared withtyping.Union
,union type
expressions can achieve more concise type hint syntax.
Java
The specification
of JVM (Java Virtual Machine)
is The Java® Virtual Machine Specification
, the latest version is Java SE 22 Edition
, and the release date is 2024-02-09
. Compared with the type definition
at the compiler
level of other languages, the situation of JVM
is more in line with the design of virtual machine
. At the same time, the types
of JVM
are divided into primitive types
and reference types
. Due to the needs of virtual machine
development, primitive types
are selected for discussion.
In addition, Java
also has a specification
, The Java Language Specification, Java SE 22 Edition
, HTML
link: https://docs.oracle.com/javase/specs/jls/se22/html/index.html PDF
link: https://docs.oracle.com/javase/specs/jls/se22/jls22.pdf . Due to the special needs of virtual machine
development, in this article, we choose to study the type definition
of JVM
rather than the type definition
of Java
language
Source link (HTML): https://docs.oracle.com/javase/specs/jvms/se22/html/jvms-2.html#jvms-2.3
Source link (PDF): https://docs.oracle.com/javase/specs/jvms/se22/jvms22.pdf
byte
- integer type whose value is an8
-bit signed two’s complement integer, and its default value iszero
. From-128
to127
(-27 to 27 - 1), inclusive.short
- integer type Its value is a16
-bit signed two’s complement integer, whose default value iszero
. From-32768
to32767
(-215 to 215 - 1), inclusive.int
- integer type Its value is a32
-bit signed two’s complement integer, whose default value iszero
. From-2147483648
to2147483647
(-231 to 231 - 1), inclusive.long
- integer type Its value is a64
-bit signed two’s complement integer, whose default value iszero
. From-9223372036854775808
to9223372036854775807
(-263 to 263 - 1), inclusive.char
- integer type whose values are16
-bit unsigned integers representingUnicode
code points in the Basic Multilingual Plane, encoded asUTF-16
, whose default value is thenull
code point (\u0000
). From0
to65535
.float
- floating-point type whose values are exactly in the32
-bit IEEE 754 binary32 format, with a default value ofpositive zero
.double
- Floating point type Its value is exactly the same as the value of64
bit IEEE 754 binary64 format, and the default value ispositive zero
.
Detailed standard
Type definition
Complex types will be processed by the compiler
to ensure the simplicity and efficiency of the virtual machine
, and for simple types, they will be directly supported by the virtual machine
.
The following are the 6
basic types defined by the AQ virtual machine
:
Types that are not directly supported include
unsigned integers
,memory addresses (pointers)
,strings
, etc. These types will be implemented at thecompiler
level. For thevirtual machine
, these types are indirectly implemented.
null
-0x00
- empty type
The empty type only represents an unknown type or a type that is not needed (for example: no return). No length.byte
-0x01
-1
byte (8
bit) signed integer type
Stored in two’s complement. Generally used to storebool
orchar
. From-128
to127
(-27 to 27 - 1), inclusive.int
-0x02
-4
byte (32
bit) signed integer type
Stored in two’s complement. From-2147483648
to2147483647
(-231 to 231 - 1), inclusive.long
-0x03
-8
byte (64
bit) signed integer type
Using two’s complement storage.Memory address (pointer)
is also stored in this way. Its value is a64
bit signed two’s complement integer, and its default value iszero
. From-9223372036854775808
to9223372036854775807
(-263 to 263 - 1), inclusive.float
-0x04
-4
byte (32
bit) single-precision floating-point type
UsingISO/IEC 60559 Information technology — Microprocessor Systems — Floating-Point arithmetic
standard.double
-0x05
-8
byte (64
bit) double precision floating point type
UsingISO/IEC 60559 Information technology — Microprocessor Systems — Floating-Point arithmetic
standard.
Complement code
Definition
Complement code
is a method of representing signed numbers in computers.
Method
The complement code
of a positive number
and 0
is the number itself plus the highest bit 0. The complement code
of a negative number
is the absolute value is bitwise inverted and then 1 is added.
Floating point standard
Definition
Floating point standard
adopts ISO/IEC 60559 Information technology — Microprocessor Systems — Floating-Point arithmetic
standard. This standard is also known as the IEEE Standard for Binary Floating-Point Arithmetic (IEEE 754)
Official website: https://www.iso.org/standard/80985.html
Method
The actual value
of a floating-point number
is equal to the sign bit
multiplied by the exponent offset value
multiplied by the fractional value
. For detailed definition, see the ISO/IEC 60559 Information technology — Microprocessor Systems — Floating-Point arithmetic
standard.
32
bit floating point
Bit length | Name | Bit number |
---|---|---|
1 | Sign | 31 |
8 | Number | 30 to 23 positive value (actual exponent size + 127) |
23 | Significant digit | 22 to 0 bit number (starting from the right with 0) |
64
bit floating point
Bit length | Name | Bit number |
---|---|---|
1 | Sign | 63 |
11 | Number | 62 to 52 positive value (actual exponent size + 1023) |
52 | Significant digit | 51 to 0 bit number (starting from the right with 0) |
types.h
complete code:
For types
there is also relevant code. The following is the code for types.h
:
// Copyright 2024 AQ author, All Rights Reserved.
// This program is licensed under the AQ License. You can find the AQ license in
// the root directory.
#ifndef AQ_AQVM_MEMORY_TYPES_H_
#define AQ_AQVM_MEMORY_TYPES_H_
#include <stdint.h>
// null - 0x00 - null type
// The null type simply represents an unknown type or a type that is not needed
// (e.g., returns nothing). Has no length.
typedef void aqnull;
// byte - 0x01 - 1 byte (8-bit) signed integer type
// Using two's complement storage. Generally used to store bool or char. From
// -128 to 127 (-2^7 to 2^7 - 1), inclusive.
typedef int8_t aqbyte;
// int - 0x02 - 4-byte (32-bit) signed integer type
// Stored in two's complement notation. From -2147483648 to 2147483647 (-2^31 to
// 2^31 - 1), inclusive.
typedef int aqint;
// long - 0x03 - 8-byte (64-bit) signed integer type
// Stored in two's complement notation. From -9223372036854775808 to
// 9223372036854775807 (-2^63 to 2^63 - 1), inclusive.
typedef int64_t aqlong;
// float - 0x04 - 4-byte (32-bit) single-precision floating point type
// Using ISO/IEC 60559 Information technology — Microprocessor Systems —
// Floating-Point arithmetic standard.
typedef float aqfloat;
// double - 0x05 - 8-byte (64-bit) double-precision floating point type
// Using ISO/IEC 60559 Information technology — Microprocessor Systems —
// Floating-Point arithmetic standard.
typedef double aqdouble;
// The part beyond 0x05 and within 0x0F is currently designated as a reserved
// type. The part beyond 0x0F cannot be used because it exceeds the 4-bit size
// limit.
#endif
We are working harder to develop the
AQ virtual machine
. If you want to learn more or participate in the development work, please follow our official website: https://www.axa6.com and Github: https://github.com/aq-org/AQ.
This article is based on the AQ License: https://github.com/aq-org/AQ/blob/main/LICENSE. If necessary, please adapt or reprint according to the AQ License.