ArkScript
A small, fast, functional and scripting language for video games
IRCompiler.hpp
Go to the documentation of this file.
1/**
2 * @file IRCompiler.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief Compile the intermediate representation to bytecode
5 * @date 2024-10-05
6 *
7 * @copyright Copyright (c) 2024-2025
8 *
9 */
10
11#ifndef ARK_COMPILER_INTERMEDIATEREPRESENTATION_IRCOMPILER_HPP
12#define ARK_COMPILER_INTERMEDIATEREPRESENTATION_IRCOMPILER_HPP
13
14#include <vector>
15#include <string>
16
17#include <Ark/Platform.hpp>
18#include <Ark/Logger.hpp>
22
23namespace Ark::internal
24{
25 class ARK_API IRCompiler final
26 {
27 public:
28 /**
29 * @brief Create a new IRCompiler
30 *
31 * @param debug debug level
32 */
33 explicit IRCompiler(unsigned debug);
34
35 /**
36 * @brief Turn a given IR into bytecode
37 *
38 * @param pages list of lists of IR entities generated by the compiler
39 * @param symbols symbol table generated by the compiler
40 * @param values value table generated by the compiler
41 */
42 void process(const std::vector<IR::Block>& pages, const std::vector<std::string>& symbols, const std::vector<ValTableElem>& values);
43
44 /**
45 * @brief Dump the IR given to `process` to an output stream
46 *
47 * @param stream output stream
48 */
49 void dumpToStream(std::ostream& stream) const;
50
51 /**
52 * @brief Return the constructed bytecode object
53 *
54 * @return const bytecode_t&
55 */
56 [[nodiscard]] const bytecode_t& bytecode() const noexcept;
57
58 private:
61 std::vector<IR::Block> m_ir;
62 std::vector<std::string> m_filenames;
63
64 void compile();
65
66 /**
67 * @brief Push a word (4 bytes) to the m_bytecode
68 * @param word
69 */
70 void pushWord(const Word& word);
71
72 /**
73 * @brief Push the file headers (magic, version used, timestamp)
74 *
75 */
76 void pushFileHeader() noexcept;
77
78 void pushSymbolTable(const std::vector<std::string>& symbols);
79 void pushValueTable(const std::vector<ValTableElem>& values);
80 void pushFilenameTable();
81 void pushInstLocTable(const std::vector<IR::Block>& pages);
82 };
83}
84
85#endif // ARK_COMPILER_INTERMEDIATEREPRESENTATION_IRCOMPILER_HPP
Common code for the compiler.
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< std::string > m_filenames
std::vector< IR::Block > m_ir
std::vector< uint8_t > bytecode_t
Definition Common.hpp:22
A Compiler Value class helper to handle multiple types.