· 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
typeinAqvmMemory_Memoryinmemory.h, eachuint8_tstores2types, so the number oftypesshould be between0x00-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 - 1byte signed integer type
- int - 4byte signed integer type
- long - 8byte signed integer type
- float - 4byte single-precision floating-point type
- double - 8byte 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 typeswill be implemented at thecompilerlevel.
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- _Booltype are large enough to store values of- 0and- 1.
- (unsigned) char- character type. An object declared as type- charis large enough to store any member of the basic execution character set. If a member of the basic execution character set is stored in a- charobject, its value is guaranteed to be non-negative. If any other character is stored in a character object, the resulting value is- implementation-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 the- floattype is a subset of the set of values of the- doubletype.
- double- floating-point type. The value set of the- doubletype is a subset of the set of values of the- long doubletype.
- long double- floating-point type.
- void- The- voidtype 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- Integer- Integerhas unlimited precision. Unmodified integer literals (including hexadecimal, octal, and binary numbers) produce integers.
- float- Floating-point numbers- Floating-point numbersare usually implemented in- Cusing- double. 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. Adding- jor- Jto 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- Boolean- Booleanis also a- subtypeof- integer. A Boolean object representing a truth value. The- booltype has only two constant instances:- Trueand- False.
- list- List- Listsare mutable sequences, often used to store- collectionsof 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 the- enumerate()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 of- setor- dict).
- range- The- rangetype represents an immutable sequence of numbers, often used to iterate a specified number of times in a- forloop.
- str- Text sequence type Text data is handled in Python using the- strobject, also known as a- string. A- stringis an- immutable sequenceof- Unicodecode points.
- bytes- The- bytesobject is an- immutable sequenceof single bytes. Since many major binary protocols are based on the- ASCIItext encoding, the- bytesobject provides some- methodsthat are only available when dealing with- ASCIIcompatible data, and are closely related to the- stringobject in many features.
- bytearray- The- bytearrayobject is the mutable counterpart of the- bytesobject.
- memoryview- Memory view The- memoryviewobject allows- Pythoncode to access the internal data of an object as long as the object supports the- buffer protocolwithout copying.
- set- Set type The- setobject is an- unordered multi-item setof unique- hashableobjects. Common uses include membership testing, removing duplicates from a sequence, and set-like calculations in mathematics, such as- intersection,- union,- difference, and- symmetric difference. The- settype is mutable and its contents can be changed using methods such as- add()and- remove(). As a- mutable type, it has no- hash valueand cannot be used as a- dictionarykey- or anset- element.
- frozenset- Set Types The- frozensettype is- immutableand- hashable, its contents cannot be changed after creation, so it can be used as- keysof- dictionariesor- elementsof other- sets.
- dict- Mapping Types- mappingobjects map- hashablevalues to arbitrary objects.- Mappingsare- mutable objects. Currently there is only one standard mapping type,- dict.
- GenericAlias-- GenericAliasobjects are usually created by- extractinga- class. They are most commonly used for- container classes, such as- listor- dict. For example, the- list[int]- GenericAliasobject is created by extracting the- listclass with an- intparameter. The main purpose of- GenericAliasobjects is for- type annotations.
- union-- Union objectscontain the values after performing- | (bitwise or)operations on multiple- type objects. These types are mainly used for- type annotations. Compared with- typing.Union,- union typeexpressions 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 an- 8-bit signed two’s complement integer, and its default value is- zero. From- -128to- 127(-27 to 27 - 1), inclusive.
- short- integer type Its value is a- 16-bit signed two’s complement integer, whose default value is- zero. From- -32768to- 32767(-215 to 215 - 1), inclusive.
- int- integer type Its value is a- 32-bit signed two’s complement integer, whose default value is- zero. From- -2147483648to- 2147483647(-231 to 231 - 1), inclusive.
- long- integer type Its value is a- 64-bit signed two’s complement integer, whose default value is- zero. From- -9223372036854775808to- 9223372036854775807(-263 to 263 - 1), inclusive.
- char- integer type whose values are- 16-bit unsigned integers representing- Unicodecode points in the Basic Multilingual Plane, encoded as- UTF-16, whose default value is the- nullcode point (- \u0000). From- 0to- 65535.
- float- floating-point type whose values are exactly in the- 32-bit IEEE 754 binary32 format, with a default value of- positive zero.
- double- Floating point type Its value is exactly the same as the value of- 64bit IEEE 754 binary64 format, and the default value is- positive 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 thecompilerlevel. 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-- 1byte (- 8bit) signed integer type
 Stored in two’s complement. Generally used to store- boolor- char. From- -128to- 127(-27 to 27 - 1), inclusive.
- int-- 0x02-- 4byte (- 32bit) signed integer type
 Stored in two’s complement. From- -2147483648to- 2147483647(-231 to 231 - 1), inclusive.
- long-- 0x03-- 8byte (- 64bit) signed integer type
 Using two’s complement storage.- Memory address (pointer)is also stored in this way. Its value is a- 64bit signed two’s complement integer, and its default value is- zero. From- -9223372036854775808to- 9223372036854775807(-263 to 263 - 1), inclusive.
- float-- 0x04-- 4byte (- 32bit) single-precision floating-point type
 Using- ISO/IEC 60559 Information technology — Microprocessor Systems — Floating-Point arithmeticstandard.
- double-- 0x05-- 8byte (- 64bit) double precision floating point type
 Using- ISO/IEC 60559 Information technology — Microprocessor Systems — Floating-Point arithmeticstandard.
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.