ArkScript
A small, fast, functional and scripting language for video games
IROptimizer.hpp
Go to the documentation of this file.
1/**
2 * @file IROptimizer.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief Optimize IR based on IR entity grouped by 2 (or more)
5 * @date 2024-10-11
6 *
7 * @copyright Copyright (c) 2024-2025
8 *
9 */
10#ifndef ARK_COMPILER_INTERMEDIATEREPRESENTATION_IROPTIMIZER_HPP
11#define ARK_COMPILER_INTERMEDIATEREPRESENTATION_IROPTIMIZER_HPP
12
13#include <Ark/Platform.hpp>
14#include <Ark/Logger.hpp>
17
18#include <optional>
19#include <functional>
20
21namespace Ark::internal
22{
23 class ARK_API IROptimizer final
24 {
25 public:
26 /**
27 * @brief Create a new IROptimizer
28 *
29 * @param debug debug level
30 */
31 explicit IROptimizer(unsigned debug);
32
33 /**
34 * @brief Turn a given IR into bytecode
35 *
36 * @param pages list of lists of IR entities generated by the compiler
37 * @param symbols symbol table generated by the compiler
38 * @param values value table generated by the compiler
39 */
40 void process(const std::vector<IR::Block>& pages, const std::vector<std::string>& symbols, const std::vector<ValTableElem>& values);
41
42 /**
43 * @brief Return the IR blocks (one per scope)
44 *
45 * @return const std::vector<Block>&
46 */
47 [[nodiscard]] const std::vector<IR::Block>& intermediateRepresentation() const noexcept;
48
49 private:
50 using Entities = std::vector<IR::Entity>;
51 using DualArgs = std::pair<uint16_t, uint16_t>;
52
53 struct Rule
54 {
55 std::vector<Instruction> expected;
57 std::function<bool(const Entities&)> condition = [](const Entities&) {
58 return true;
59 }; ///< Additional condition to match
60 std::function<DualArgs(const Entities&)> createReplacement =
61 [](const Entities& entities) {
62 return std::make_pair(entities[0].primaryArg(), entities[1].primaryArg());
63 }; ///< Create the replacement instructions from given context
64 };
65
66 std::vector<Rule> m_ruleset_two;
67 std::vector<Rule> m_ruleset_three;
68
70 std::vector<IR::Block> m_ir;
71 std::vector<std::string> m_symbols;
72 std::vector<ValTableElem> m_values;
73
74 [[nodiscard]] bool match(const std::vector<Instruction>& expected_insts, const Entities& entities) const;
75 std::optional<IR::Entity> replaceWithRules(const std::vector<Rule>& rules, const Entities& entities);
76
77 [[nodiscard]] bool isPositiveNumberInlinable(uint16_t id) const;
78 [[nodiscard]] uint16_t numberAsArg(uint16_t id) const;
79 };
80}
81
82#endif // ARK_COMPILER_INTERMEDIATEREPRESENTATION_IROPTIMIZER_HPP
An entity in the IR is a bundle of information.
Internal logger.
#define ARK_API
Definition Module.hpp:28
ArkScript configuration macros.
The basic value type handled by the compiler.
std::vector< ValTableElem > m_values
std::vector< Rule > m_ruleset_two
std::vector< IR::Block > m_ir
std::pair< uint16_t, uint16_t > DualArgs
std::vector< IR::Entity > Entities
std::vector< std::string > m_symbols
std::vector< Rule > m_ruleset_three
Instruction
The different bytecodes are stored here.
std::vector< Instruction > expected