Reduct  v1.0.4-3-gdaf0d70
A functional and immutable language.
Loading...
Searching...
No Matches
Instruction Format

Detailed Description

Instructions are 32-bit words with the following formats:

Fields:

To determine if the C field is a register or a constant the reduct_mode_t flags are used to modify the opcode.

Note
The reason we avoid formats such as iABx, used within Lua, is that even if it increases the maximum constant capacity it means that operations such as REDUCT_OPCODE_EQUAL always need to act on registers, which introduces unnecessary MOV instructions to load constants into registers before they can be compared.

Macros

#define REDUCT_REG_INVALID   ((reduct_reg_t) - 1)
 Invalid register value.
 
#define REDUCT_INST_WIDTH_OPCODE   6ULL
 Opcode width in bits.
 
#define REDUCT_INST_WIDTH_A   8ULL
 A operand width in bits.
 
#define REDUCT_INST_WIDTH_B   8ULL
 B operand width in bits.
 
#define REDUCT_INST_WIDTH_C   10ULL
 C operand width in bits.
 
#define REDUCT_INST_WIDTH_SBX   (REDUCT_INST_WIDTH_B + REDUCT_INST_WIDTH_C)
 SBx operand width in bits.
 
#define REDUCT_REGISTER_MAX   (1ULL << REDUCT_INST_WIDTH_A)
 The max number of registers per function frame.
 
#define REDUCT_CONSTANT_MAX   (1ULL << REDUCT_INST_WIDTH_C)
 The max number of constants per function.
 
#define REDUCT_INST_POS_OPCODE   0ULL
 Opcode position in bits.
 
#define REDUCT_INST_POS_A   (REDUCT_INST_POS_OPCODE + REDUCT_INST_WIDTH_OPCODE)
 A operand position in bits.
 
#define REDUCT_INST_POS_B   (REDUCT_INST_POS_A + REDUCT_INST_WIDTH_A)
 B operand position in bits.
 
#define REDUCT_INST_POS_C   (REDUCT_INST_POS_B + REDUCT_INST_WIDTH_B)
 C operand position in bits.
 
#define REDUCT_INST_MASK_OPCODE   ((1ULL << REDUCT_INST_WIDTH_OPCODE) - 1ULL)
 Opcode mask.
 
#define REDUCT_INST_MASK_A   ((1ULL << REDUCT_INST_WIDTH_A) - 1ULL)
 A operand mask.
 
#define REDUCT_INST_MASK_B   ((1ULL << REDUCT_INST_WIDTH_B) - 1ULL)
 B operand mask.
 
#define REDUCT_INST_MASK_C   ((1ULL << REDUCT_INST_WIDTH_C) - 1ULL)
 C operand mask.
 
#define REDUCT_INST_MASK_SBX   ((1ULL << REDUCT_INST_WIDTH_SBX) - 1ULL)
 SBx operand mask.
 
#define REDUCT_INST_MAKE_ABC(_op, _a, _b, _c)
 Create an instruction with opcode, A, B, and C operands.
 
#define REDUCT_INST_MAKE_ASBX(_op, _a, _sbx)
 Create an instruction with opcode and A operands, and SBx B operand.
 
#define REDUCT_INST_GET_OP(_inst)   (((_inst) >> REDUCT_INST_POS_OPCODE) & REDUCT_INST_MASK_OPCODE)
 Get the opcode from an instruction.
 
#define REDUCT_INST_GET_OP_BASE(_inst)    (((_inst) >> REDUCT_INST_POS_OPCODE) & (REDUCT_INST_MASK_OPCODE & ~REDUCT_MODE_CONST))
 Get the opcode base (without reduct_mode_t) from an instruction. Mask clears the REDUCT_MODE_CONST bit.
 
#define REDUCT_INST_GET_A(_inst)   (((_inst) >> REDUCT_INST_POS_A) & REDUCT_INST_MASK_A)
 Get the A operand from an instruction.
 
#define REDUCT_INST_GET_B(_inst)   (((_inst) >> REDUCT_INST_POS_B) & REDUCT_INST_MASK_B)
 Get the B operand from an instruction.
 
#define REDUCT_INST_GET_C(_inst)   (((_inst) >> REDUCT_INST_POS_C) & REDUCT_INST_MASK_C)
 Get the C operand from an instruction.
 
#define REDUCT_INST_GET_SBX(_inst)
 Get the SBX operand from an instruction.
 
#define REDUCT_INST_SET_A(_inst, _a)    (((_inst) & ~(REDUCT_INST_MASK_A << REDUCT_INST_POS_A)) | (((_a) & REDUCT_INST_MASK_A) << REDUCT_INST_POS_A))
 Set the A operand in an instruction.
 
#define REDUCT_INST_SET_SBX(_inst, _sbx)    (((_inst) & ~(REDUCT_INST_MASK_SBX << REDUCT_INST_POS_B)) | (((_sbx) & REDUCT_INST_MASK_SBX) << REDUCT_INST_POS_B))
 Set the SBX operand in an instruction.
 

Typedefs

typedef reduct_uint16_t reduct_reg_t
 Register type.
 
typedef reduct_uint32_t reduct_inst_t
 Instruction type.
 

Enumerations

enum  reduct_mode_t { REDUCT_MODE_NONE = -1 , REDUCT_MODE_TARGET = -2 , REDUCT_MODE_REG = 0 , REDUCT_MODE_CONST = 1 << 5 }
 Opcode mode enumeration. More...
 
enum  reduct_opcode_t {
  REDUCT_OPCODE_NONE , REDUCT_OPCODE_LIST , REDUCT_OPCODE_JMP , REDUCT_OPCODE_JMPF ,
  REDUCT_OPCODE_JMPT , REDUCT_OPCODE_JEQ , REDUCT_OPCODE_CALL , REDUCT_OPCODE_MOV ,
  REDUCT_OPCODE_RET , REDUCT_OPCODE_APPEND , REDUCT_OPCODE_EQ , REDUCT_OPCODE_NEQ ,
  REDUCT_OPCODE_SEQ , REDUCT_OPCODE_SNEQ , REDUCT_OPCODE_LT , REDUCT_OPCODE_LE ,
  REDUCT_OPCODE_GT , REDUCT_OPCODE_GE , REDUCT_OPCODE_ADD , REDUCT_OPCODE_SUB ,
  REDUCT_OPCODE_MUL , REDUCT_OPCODE_DIV , REDUCT_OPCODE_MOD , REDUCT_OPCODE_BAND ,
  REDUCT_OPCODE_BOR , REDUCT_OPCODE_BXOR , REDUCT_OPCODE_BNOT , REDUCT_OPCODE_SHL ,
  REDUCT_OPCODE_SHR , REDUCT_OPCODE_CLOSURE , REDUCT_OPCODE_CAPTURE , REDUCT_OPCODE_TAILCALL
}
 Opcode enumeration. More...
 

Macro Definition Documentation

◆ REDUCT_REG_INVALID

#define REDUCT_REG_INVALID   ((reduct_reg_t) - 1)

Invalid register value.

Definition at line 92 of file inst.h.

◆ REDUCT_INST_WIDTH_OPCODE

#define REDUCT_INST_WIDTH_OPCODE   6ULL

Opcode width in bits.

Definition at line 99 of file inst.h.

◆ REDUCT_INST_WIDTH_A

#define REDUCT_INST_WIDTH_A   8ULL

A operand width in bits.

Definition at line 100 of file inst.h.

◆ REDUCT_INST_WIDTH_B

#define REDUCT_INST_WIDTH_B   8ULL

B operand width in bits.

Definition at line 101 of file inst.h.

◆ REDUCT_INST_WIDTH_C

#define REDUCT_INST_WIDTH_C   10ULL

C operand width in bits.

Definition at line 102 of file inst.h.

◆ REDUCT_INST_WIDTH_SBX

#define REDUCT_INST_WIDTH_SBX   (REDUCT_INST_WIDTH_B + REDUCT_INST_WIDTH_C)

SBx operand width in bits.

Definition at line 103 of file inst.h.

◆ REDUCT_REGISTER_MAX

#define REDUCT_REGISTER_MAX   (1ULL << REDUCT_INST_WIDTH_A)

The max number of registers per function frame.

Definition at line 108 of file inst.h.

◆ REDUCT_CONSTANT_MAX

#define REDUCT_CONSTANT_MAX   (1ULL << REDUCT_INST_WIDTH_C)

The max number of constants per function.

Definition at line 112 of file inst.h.

◆ REDUCT_INST_POS_OPCODE

#define REDUCT_INST_POS_OPCODE   0ULL

Opcode position in bits.

Definition at line 114 of file inst.h.

◆ REDUCT_INST_POS_A

#define REDUCT_INST_POS_A   (REDUCT_INST_POS_OPCODE + REDUCT_INST_WIDTH_OPCODE)

A operand position in bits.

Definition at line 115 of file inst.h.

◆ REDUCT_INST_POS_B

#define REDUCT_INST_POS_B   (REDUCT_INST_POS_A + REDUCT_INST_WIDTH_A)

B operand position in bits.

Definition at line 116 of file inst.h.

◆ REDUCT_INST_POS_C

#define REDUCT_INST_POS_C   (REDUCT_INST_POS_B + REDUCT_INST_WIDTH_B)

C operand position in bits.

Definition at line 117 of file inst.h.

◆ REDUCT_INST_MASK_OPCODE

#define REDUCT_INST_MASK_OPCODE   ((1ULL << REDUCT_INST_WIDTH_OPCODE) - 1ULL)

Opcode mask.

Definition at line 119 of file inst.h.

◆ REDUCT_INST_MASK_A

#define REDUCT_INST_MASK_A   ((1ULL << REDUCT_INST_WIDTH_A) - 1ULL)

A operand mask.

Definition at line 120 of file inst.h.

◆ REDUCT_INST_MASK_B

#define REDUCT_INST_MASK_B   ((1ULL << REDUCT_INST_WIDTH_B) - 1ULL)

B operand mask.

Definition at line 121 of file inst.h.

◆ REDUCT_INST_MASK_C

#define REDUCT_INST_MASK_C   ((1ULL << REDUCT_INST_WIDTH_C) - 1ULL)

C operand mask.

Definition at line 122 of file inst.h.

◆ REDUCT_INST_MASK_SBX

#define REDUCT_INST_MASK_SBX   ((1ULL << REDUCT_INST_WIDTH_SBX) - 1ULL)

SBx operand mask.

Definition at line 123 of file inst.h.

◆ REDUCT_INST_MAKE_ABC

#define REDUCT_INST_MAKE_ABC (   _op,
  _a,
  _b,
  _c 
)
Value:
#define REDUCT_INST_POS_C
C operand position in bits.
Definition inst.h:117
#define REDUCT_INST_POS_A
A operand position in bits.
Definition inst.h:115
#define REDUCT_INST_MASK_A
A operand mask.
Definition inst.h:120
reduct_uint32_t reduct_inst_t
Instruction type.
Definition inst.h:97
#define REDUCT_INST_MASK_B
B operand mask.
Definition inst.h:121
#define REDUCT_INST_POS_B
B operand position in bits.
Definition inst.h:116
#define REDUCT_INST_MASK_OPCODE
Opcode mask.
Definition inst.h:119
#define REDUCT_INST_POS_OPCODE
Opcode position in bits.
Definition inst.h:114
#define REDUCT_INST_MASK_C
C operand mask.
Definition inst.h:122

Create an instruction with opcode, A, B, and C operands.

Parameters
_opOpcode operand.
_aA operand.
_bB operand.
_cC operand.

Definition at line 133 of file inst.h.

◆ REDUCT_INST_MAKE_ASBX

#define REDUCT_INST_MAKE_ASBX (   _op,
  _a,
  _sbx 
)
Value:
#define REDUCT_INST_MASK_SBX
SBx operand mask.
Definition inst.h:123

Create an instruction with opcode and A operands, and SBx B operand.

Parameters
_opOpcode operand.
_aA operand.
_sbxSBx operand.

Definition at line 146 of file inst.h.

◆ REDUCT_INST_GET_OP

#define REDUCT_INST_GET_OP (   _inst)    (((_inst) >> REDUCT_INST_POS_OPCODE) & REDUCT_INST_MASK_OPCODE)

Get the opcode from an instruction.

Parameters
_instInstruction.

Definition at line 156 of file inst.h.

◆ REDUCT_INST_GET_OP_BASE

#define REDUCT_INST_GET_OP_BASE (   _inst)     (((_inst) >> REDUCT_INST_POS_OPCODE) & (REDUCT_INST_MASK_OPCODE & ~REDUCT_MODE_CONST))

Get the opcode base (without reduct_mode_t) from an instruction. Mask clears the REDUCT_MODE_CONST bit.

Parameters
_instInstruction.

Definition at line 163 of file inst.h.

◆ REDUCT_INST_GET_A

#define REDUCT_INST_GET_A (   _inst)    (((_inst) >> REDUCT_INST_POS_A) & REDUCT_INST_MASK_A)

Get the A operand from an instruction.

Parameters
_instInstruction.

Definition at line 171 of file inst.h.

◆ REDUCT_INST_GET_B

#define REDUCT_INST_GET_B (   _inst)    (((_inst) >> REDUCT_INST_POS_B) & REDUCT_INST_MASK_B)

Get the B operand from an instruction.

Parameters
_instInstruction.

Definition at line 178 of file inst.h.

◆ REDUCT_INST_GET_C

#define REDUCT_INST_GET_C (   _inst)    (((_inst) >> REDUCT_INST_POS_C) & REDUCT_INST_MASK_C)

Get the C operand from an instruction.

Parameters
_instInstruction.

Definition at line 185 of file inst.h.

◆ REDUCT_INST_GET_SBX

#define REDUCT_INST_GET_SBX (   _inst)
Value:
int64_t reduct_int64_t
Definition defs.h:92
#define REDUCT_INST_WIDTH_SBX
SBx operand width in bits.
Definition inst.h:103

Get the SBX operand from an instruction.

Parameters
_instInstruction.

Definition at line 192 of file inst.h.

◆ REDUCT_INST_SET_A

#define REDUCT_INST_SET_A (   _inst,
  _a 
)     (((_inst) & ~(REDUCT_INST_MASK_A << REDUCT_INST_POS_A)) | (((_a) & REDUCT_INST_MASK_A) << REDUCT_INST_POS_A))

Set the A operand in an instruction.

Parameters
_instInstruction.
_aA operand value.

Definition at line 202 of file inst.h.

◆ REDUCT_INST_SET_SBX

#define REDUCT_INST_SET_SBX (   _inst,
  _sbx 
)     (((_inst) & ~(REDUCT_INST_MASK_SBX << REDUCT_INST_POS_B)) | (((_sbx) & REDUCT_INST_MASK_SBX) << REDUCT_INST_POS_B))

Set the SBX operand in an instruction.

Parameters
_instInstruction.
_sbxSBX operand value.

Definition at line 211 of file inst.h.

Typedef Documentation

◆ reduct_reg_t

Register type.

Definition at line 87 of file inst.h.

◆ reduct_inst_t

Instruction type.

Definition at line 97 of file inst.h.

Enumeration Type Documentation

◆ reduct_mode_t

Opcode mode enumeration.

Enumerator
REDUCT_MODE_NONE 

Invalid mode.

REDUCT_MODE_TARGET 

Compilation target hint mode.

REDUCT_MODE_REG 

Register operand mode.

REDUCT_MODE_CONST 

Constant operand mode.

Definition at line 36 of file inst.h.

◆ reduct_opcode_t

Opcode enumeration.

Enumerator
REDUCT_OPCODE_NONE 
REDUCT_OPCODE_LIST 

(A) Create a new list and store it in R(A).

REDUCT_OPCODE_JMP 

(sBx) Unconditional jump by relative offset sBx.

REDUCT_OPCODE_JMPF 

(A, sBx) Jump by sBx if R(A) is falsy.

REDUCT_OPCODE_JMPT 

(A, sBx) Jump by sBx if R(A) is truthy.

REDUCT_OPCODE_JEQ 

(A, C) Skip the next instruction if R(A) == R/K(C), else continue.

REDUCT_OPCODE_CALL 

(A, B, C) Call callable in R/K(C) with B args starting from R(A). Result in R(A).

REDUCT_OPCODE_MOV 

(A, C) Move value in R/K(C) to R(A).

REDUCT_OPCODE_RET 

(C) Return value in R/K(C).

REDUCT_OPCODE_APPEND 

(A, C) Append value in R/K(C) to the back of the list in R(A).

REDUCT_OPCODE_EQ 

(A, B, C) If R(B) == R/K(C) store true in R(A), else false.

REDUCT_OPCODE_NEQ 

(A, B, C) If R(B) != R/K(C) store true in R(A), else false.

REDUCT_OPCODE_SEQ 

(A, B, C) If R(B) === R/K(C) store true in R(A), else false.

REDUCT_OPCODE_SNEQ 

(A, B, C) If R(B) !== R/K(C) store true in R(A), else false.

REDUCT_OPCODE_LT 

(A, B, C) If R(B) < R/K(C) store true in R(A), else false.

REDUCT_OPCODE_LE 

(A, B, C) If R(B) <= R/K(C) store true in R(A), else false.

REDUCT_OPCODE_GT 

(A, B, C) If R(B) > R/K(C) store true in R(A), else false.

REDUCT_OPCODE_GE 

(A, B, C) If R(B) >= R/K(C) store true in R(A), else false.

REDUCT_OPCODE_ADD 

(A, B, C) R(A) = R(B) + R/K(C)

REDUCT_OPCODE_SUB 

(A, B, C) R(A) = R(B) - R/K(C)

REDUCT_OPCODE_MUL 

(A, B, C) R(A) = R(B) * R/K(C)

REDUCT_OPCODE_DIV 

(A, B, C) R(A) = R(B) / R/K(C)

REDUCT_OPCODE_MOD 

(A, B, C) R(A) = R(B) % R/K(C)

REDUCT_OPCODE_BAND 

(A, B, C) R(A) = R(B) & R/K(C)

REDUCT_OPCODE_BOR 

(A, B, C) R(A) = R(B) | R/K(C)

REDUCT_OPCODE_BXOR 

(A, B, C) R(A) = R(B) ^ R/K(C)

REDUCT_OPCODE_BNOT 

(A, C) R(A) = ~R/K(C)

REDUCT_OPCODE_SHL 

(A, B, C) R(A) = R(B) << R/K(C)

REDUCT_OPCODE_SHR 

(A, B, C) R(A) = R(B) >> R/K(C)

REDUCT_OPCODE_CLOSURE 

(A, C) Wrap the function prototype in K(C) in a closure and store in R(A).

REDUCT_OPCODE_CAPTURE 

(A, B, C) Capture R/K(C) into constant slot B in closure R(A).

REDUCT_OPCODE_TAILCALL 

(A, B, C) Tail call callable in R/K(C) with B args starting from R(A).

Definition at line 48 of file inst.h.