Prism Bytecode File Structure
The following documentation outlines the structure of the bytecode file used by our Prism programming language. This structure is crucial for understanding how the source code is represented by the compiler and executed by the Prism virtual machine.
Header
The Prism bytecode file begins with a header containing essential information for interpreting the bytecode. Key components of the header include:
Magic Number: A magic number identifying the file as Prism bytecode. This number is "0x505249534D
", corresponding to "PRISM" in ASCII.
Format Version: A version number indicating the bytecode file format version. Currently, we are at version 1.0.
Code Section
The code section in the Prism bytecode file contains the instructions to be executed. Each instruction is represented by an opcode, possibly followed by operands. The Code section is divided into three subsections:
Frame: This part contains a list of instructions for each function defined in the source code.
Labels: The instruction lists at this level correspond to actions to be taken in control structures such as "if
" or "while
".
Main Instructions: This section represents the instructions of the main function in the source code.
Here's a simple example to illustrate the structure :
Opcodes for Builtins
0x00
Add
Performs addition
0x01
Sub
Performs subtraction
0x02
Mul
Performs multiplication
0x03
Div
Performs division
0x04
Eq
Checks for equality
0x05
Less
Checks if the first value is less than the second
Opcodes for Values
0x06
Integer
Represents an integer value
0x07
Boolean
Represents a boolean value
0x08
Op
Represents a built-in operation (addition, subtraction, etc.)
0x09
Function
Represents a function
0x0A
Symbol
Represents a symbol (variable name)
Opcodes for Instructions
0x0B
SysCall
Applies the built-in function on the stack
0x0C
RetValue
Returns the value at the top of the stack and terminates execution
0x0D
PushArg
Pushes the value into the arguments queue
0x0E
PushValue
Adds a new value to the top of the stack
0x0F
PushFunc
Pushes the position of a function onto the stack and adds corresponding instructions
0x10
Loop
Adds instructions from Labels when the loop condition is true
0x11
LoadArg
Finds the index in the queue and pushes it onto the stack
0x12
LoadName
Pushes into the stack the value associated with the name
0x13
StoreValue
Adds the name to the environment
0x14
JumpIfTrue
Adds instructions from Labels when the condition is true
0x15
JumpIfFalse
Adds instructions from Labels when the condition is false
Last updated