Skip to content

Commit

Permalink
feat(ir optimizer): increment & decrement super instructions can supp…
Browse files Browse the repository at this point in the history
…ort an additional number, between 0 and 4095 (included)
  • Loading branch information
SuperFola committed Dec 11, 2024
1 parent 78c3902 commit 39a05e1
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace Ark::internal
[[nodiscard]] std::optional<IR::Entity> compactEntities(const IR::Entity& first, const IR::Entity& second);
[[nodiscard]] std::optional<IR::Entity> compactEntities(const IR::Entity& first, const IR::Entity& second, const IR::Entity& third);

[[nodiscard]] bool isNumber(uint16_t id, double expected_number) const;
[[nodiscard]] bool isPositiveNumberInlinable(uint16_t id) const;
};
}

Expand Down
25 changes: 16 additions & 9 deletions src/arkreactor/Compiler/IntermediateRepresentation/IROptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ namespace Ark::internal
// LOAD_SYMBOL a
// LOAD_CONST n (1)
// ADD / SUB
// ---> INCREMENT / DECREMENT a
if (third.inst() == ADD && first.inst() == LOAD_CONST && second.inst() == LOAD_SYMBOL && isNumber(first.primaryArg(), 1))
return IR::Entity(INCREMENT, second.primaryArg());
if (third.inst() == ADD && first.inst() == LOAD_SYMBOL && second.inst() == LOAD_CONST && isNumber(second.primaryArg(), 1))
return IR::Entity(INCREMENT, first.primaryArg());
if (third.inst() == SUB && first.inst() == LOAD_SYMBOL && second.inst() == LOAD_CONST && isNumber(second.primaryArg(), 1))
return IR::Entity(DECREMENT, first.primaryArg());
// ---> INCREMENT / DECREMENT a value
if (third.inst() == ADD && first.inst() == LOAD_CONST && second.inst() == LOAD_SYMBOL && isPositiveNumberInlinable(first.primaryArg()))
return IR::Entity(INCREMENT, second.primaryArg(), static_cast<uint16_t>(std::get<double>(m_values[first.primaryArg()].value)));
if (third.inst() == ADD && first.inst() == LOAD_SYMBOL && second.inst() == LOAD_CONST && isPositiveNumberInlinable(second.primaryArg()))
return IR::Entity(INCREMENT, first.primaryArg(), static_cast<uint16_t>(std::get<double>(m_values[second.primaryArg()].value)));
if (third.inst() == SUB && first.inst() == LOAD_SYMBOL && second.inst() == LOAD_CONST && isPositiveNumberInlinable(second.primaryArg()))
return IR::Entity(DECREMENT, first.primaryArg(), static_cast<uint16_t>(std::get<double>(m_values[second.primaryArg()].value)));
// LOAD_SYMBOL list
// TAIL / HEAD
// STORE / SET_VAL a
Expand All @@ -148,8 +148,15 @@ namespace Ark::internal
return std::nullopt;
}

bool IROptimizer::isNumber(const uint16_t id, const double expected_number) const
bool IROptimizer::isPositiveNumberInlinable(const uint16_t id) const
{
return std::cmp_less(id, m_values.size()) && m_values[id].type == ValTableElemType::Number && std::get<double>(m_values[id].value) == expected_number;
if (std::cmp_less(id, m_values.size()) && m_values[id].type == ValTableElemType::Number)
{
const double val = std::get<double>(m_values[id].value);
return val >= 0.0 &&
val < IR::MaxValueForDualArg &&
static_cast<double>(static_cast<long>(val)) == val;
}
return false;
}
}
8 changes: 4 additions & 4 deletions src/arkreactor/VM/VM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1260,12 +1260,12 @@ namespace Ark
var = var->reference();

if (var->valueType() == ValueType::Number)
push(Value(var->number() + 1), context);
push(Value(var->number() + secondary_arg), context);
else
types::generateError(
"+",
{ { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
{ *var, Value(1) });
{ *var, Value(secondary_arg) });
}
DISPATCH();
}
Expand All @@ -1281,12 +1281,12 @@ namespace Ark
var = var->reference();

if (var->valueType() == ValueType::Number)
push(Value(var->number() - 1), context);
push(Value(var->number() - secondary_arg), context);
else
types::generateError(
"-",
{ { types::Contract { { types::Typedef("a", ValueType::Number), types::Typedef("b", ValueType::Number) } } } },
{ *var, Value(1) });
{ *var, Value(secondary_arg) });
}
DISPATCH();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ page_0
CALL_BUILTIN 24, 3
CALL_BUILTIN 9, 1
POP 0
DECREMENT 3
DECREMENT 3, 1
SET_VAL 3
LOAD_CONST 4
LOAD_SYMBOL 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ page_1
LOAD_CONST 1
GT 0
GOTO_IF_TRUE L0
INCREMENT 2
INCREMENT 2, 1
GOTO L1
.L0:
LOAD_CONST 1
LOAD_SYMBOL 2
EQ 0
GOTO_IF_TRUE L2
LOAD_SYMBOL 1
DECREMENT 2
DECREMENT 2, 1
LOAD_SYMBOL 0
CALL 2
DECREMENT 1
DECREMENT 1, 1
JUMP 0
GOTO L3
.L2:
LOAD_CONST 2
DECREMENT 1
DECREMENT 1, 1
JUMP 0
.L3:
.L1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ page_4
HALT 0

page_5
DECREMENT 9
DECREMENT 9, 1
SET_VAL 9
LOAD_SYMBOL 9
RET 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ page_1
LOAD_SYMBOL 3
MUL 0
SET_VAL 2
INCREMENT 3
INCREMENT 3, 1
SET_VAL 3
GOTO L0
.L1:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(mut i 1)
(set i (+ i 4))
(set i (+ 6 i))
(set i (- i 8))
# should not be inlined
(set i (+ i 4096))
(set i (+ i 1.01))
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
page_0
LOAD_CONST_STORE 0, 0
INCREMENT 0, 4
SET_VAL 0
INCREMENT 0, 6
SET_VAL 0
DECREMENT 0, 8
SET_VAL 0
LOAD_SYMBOL 0
LOAD_CONST 4
ADD 0
SET_VAL 0
LOAD_SYMBOL 0
LOAD_CONST 5
ADD 0
SET_VAL 0
HALT 0

0 comments on commit 39a05e1

Please sign in to comment.