Skip to content

Commit

Permalink
feat(vm): added 11 (unused) super instructions and their implementati…
Browse files Browse the repository at this point in the history
…on to the VM
  • Loading branch information
SuperFola committed Oct 12, 2024
1 parent e509efd commit 42670fc
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- compile time checks for mutability errors with `append!`, `concat!` and `pop!`
- new `MAKE_CLOSURE <page addr>` instruction, generated in place of a `LOAD_CONST` when a closure is made
- added `-fdump-ir` to dump the IR entities to a file named `{file}.ark.ir`
- added 11 super instructions and their implementation to the VM

### Changed
- instructions are on 4 bytes: 1 byte for the instruction, 1 byte of padding, 2 bytes for an immediate argument
Expand Down Expand Up @@ -84,7 +85,7 @@
- `io:writeFile` no longer takes a mode and has been split into `io:writeFile` and `io:appendToFile`
- instructions are now positioned like this: `inst byte1 byte2 byte3`
- byte1 is 0 if the instruction takes a single argument on 16 bits, split on byte2 and byte3
- if the instruction takes two arguments, they each have 12 bits ; the first one is on byte1 and upper half of byte2, the second on lower half of byte2 and then byte3
- if the instruction takes two arguments, they each have 12 bits ; the second one is on byte1 and upper half of byte2, the first on lower half of byte2 and then byte3

### Removed
- removed unused `NodeType::Closure`
Expand Down
31 changes: 29 additions & 2 deletions include/Ark/Compiler/Instructions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,21 @@ namespace Ark::internal
NOT = 0x30,
LAST_OPERATOR = 0x30,

LAST_INSTRUCTION = 0x30
FIRST_SUPER_INSTRUCTION = 0x31,
LOAD_CONST_LOAD_CONST = 0x31,
LOAD_CONST_STORE = 0x32,
LOAD_CONST_SET_VAL = 0x33,
STORE_FROM = 0x34,
SET_VAL_FROM = 0x35,
INCREMENT = 0x36,
DECREMENT = 0x37,
STORE_TAIL = 0x38,
STORE_HEAD = 0x39,
SET_VAL_TAIL = 0x3a,
SET_VAL_HEAD = 0x3b,
LAST_SUPER_INSTRUCTION = 0x3b,

LAST_INSTRUCTION = 0x3d
};

constexpr std::array InstructionNames = {
Expand Down Expand Up @@ -118,6 +132,7 @@ namespace Ark::internal
"POP_LIST_IN_PLACE",
"POP",
"DUP",
// operators
"ADD",
"SUB",
"MUL",
Expand All @@ -140,7 +155,19 @@ namespace Ark::internal
"MOD",
"TYPE",
"HASFIELD",
"NOT"
"NOT",
// super instructions
"LOAD_CONST_LOAD_CONST",
"LOAD_CONST_STORE",
"LOAD_CONST_SET_VAL",
"STORE_FROM",
"SET_VAL_FROM",
"INCREMENT",
"DECREMENT",
"STORE_TAIL",
"STORE_HEAD",
"SET_VAL_TAIL",
"SET_VAL_HEAD",
};
}

Expand Down
146 changes: 145 additions & 1 deletion src/arkreactor/VM/VM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,12 @@ namespace Ark
#define DISPATCH() \
NEXTOPARG(); \
DISPATCH_GOTO();
#define UNPACK_ARGS() \
do \
{ \
secondary_arg = static_cast<uint16_t>((padding << 4) | (arg & 0xf000) >> 12); \
primary_arg = arg & 0x0fff; \
} while (false)

#if ARK_USE_COMPUTED_GOTOS
# pragma GCC diagnostic push
Expand Down Expand Up @@ -398,15 +404,29 @@ namespace Ark
&&TARGET_TYPE,
&&TARGET_HASFIELD,
&&TARGET_NOT,
&&TARGET_LOAD_CONST_LOAD_CONST,
&&TARGET_LOAD_CONST_STORE,
&&TARGET_LOAD_CONST_SET_VAL,
&&TARGET_STORE_FROM,
&&TARGET_SET_VAL_FROM,
&&TARGET_INCREMENT,
&&TARGET_DECREMENT,
&&TARGET_STORE_TAIL,
&&TARGET_STORE_HEAD,
&&TARGET_SET_VAL_TAIL,
&&TARGET_SET_VAL_HEAD
};
# pragma GCC diagnostic pop
#endif

try
{
[[maybe_unused]] uint8_t padding = 0;
uint8_t inst = 0;
uint8_t padding = 0;
uint16_t arg = 0;
uint16_t primary_arg = 0;
uint16_t secondary_arg = 0;

m_running = true;

DISPATCH();
Expand Down Expand Up @@ -1074,6 +1094,130 @@ namespace Ark
DISPATCH();
}

#pragma endregion

#pragma region "Super Instructions"
TARGET(LOAD_CONST_LOAD_CONST)
{
UNPACK_ARGS();
push(loadConstAsPtr(primary_arg), context);
push(loadConstAsPtr(secondary_arg), context);
DISPATCH();
}

TARGET(LOAD_CONST_STORE)
{
UNPACK_ARGS();
store(secondary_arg, loadConstAsPtr(primary_arg), context);
DISPATCH();
}

TARGET(LOAD_CONST_SET_VAL)
{
UNPACK_ARGS();
setVal(secondary_arg, loadConstAsPtr(primary_arg), context);
DISPATCH();
}

TARGET(STORE_FROM)
{
UNPACK_ARGS();
store(secondary_arg, loadSymbol(primary_arg, context), context);
DISPATCH();
}

TARGET(SET_VAL_FROM)
{
UNPACK_ARGS();
setVal(secondary_arg, loadSymbol(primary_arg, context), context);
DISPATCH();
}

TARGET(INCREMENT)
{
UNPACK_ARGS();
{
Value* var = loadSymbol(primary_arg, context);

// use internal reference, shouldn't break anything so far, unless it's already a ref
if (var->valueType() == ValueType::Reference)
var = var->reference();

if (var->valueType() == ValueType::Number)
push(Value(var->number() + 1), context);
else
types::generateError(
"+",
{ { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
{ *var, Value(1) });
}
DISPATCH();
}

TARGET(DECREMENT)
{
UNPACK_ARGS();
{
Value* var = loadSymbol(primary_arg, context);

// use internal reference, shouldn't break anything so far, unless it's already a ref
if (var->valueType() == ValueType::Reference)
var = var->reference();

if (var->valueType() == ValueType::Number)
push(Value(var->number() - 1), context);
else
types::generateError(
"-",
{ { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
{ *var, Value(1) });
}
DISPATCH();
}

TARGET(STORE_TAIL)
{
UNPACK_ARGS();
{
Value* list = loadSymbol(primary_arg, context);
Value tail = helper::tail(list);
store(secondary_arg, &tail, context);
}
DISPATCH();
}

TARGET(STORE_HEAD)
{
UNPACK_ARGS();
{
Value* list = loadSymbol(primary_arg, context);
Value head = helper::head(list);
store(secondary_arg, &head, context);
}
DISPATCH();
}

TARGET(SET_VAL_TAIL)
{
UNPACK_ARGS();
{
Value* list = loadSymbol(primary_arg, context);
Value tail = helper::tail(list);
setVal(secondary_arg, &tail, context);
}
DISPATCH();
}

TARGET(SET_VAL_HEAD)
{
UNPACK_ARGS();
{
Value* list = loadSymbol(primary_arg, context);
Value head = helper::head(list);
setVal(secondary_arg, &head, context);
}
DISPATCH();
}
#pragma endregion
}
#if ARK_USE_COMPUTED_GOTOS
Expand Down

0 comments on commit 42670fc

Please sign in to comment.