ArkScript
A small, lisp-inspired, functional scripting language
IRCompiler.hpp
Go to the documentation of this file.
1/**
2 * @file IRCompiler.hpp
3 * @author Lexy Plateau (lexplt.dev@gmail.com)
4 * @brief Compile the intermediate representation to bytecode
5 * @date 2024-10-05
6 *
7 * @copyright Copyright (c) 2024-2026
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
18#include <Ark/Utils/Logger.hpp>
20#include <Ark/Compiler/Pass.hpp>
23
24namespace Ark::internal
25{
26 class ARK_API IRCompiler final : public Pass
27 {
28 public:
29 /**
30 * @brief Create a new IRCompiler
31 *
32 * @param debug debug level
33 */
34 explicit IRCompiler(unsigned debug);
35
36 /**
37 * @brief Turn a given IR into bytecode
38 *
39 * @param pages list of lists of IR entities generated by the compiler
40 * @param symbols symbol table generated by the compiler
41 * @param values value table generated by the compiler
42 */
43 void process(const std::vector<IR::Block>& pages, const std::vector<std::string>& symbols, const std::vector<ValTableElem>& values);
44
45 /**
46 * @brief Dump the IR given to `process` to an output stream
47 *
48 * @param stream output stream
49 */
50 void dumpToStream(std::ostream& stream) const;
51
52 /**
53 * @brief Return the constructed bytecode object
54 *
55 * @return const bytecode_t&
56 */
57 [[nodiscard]] const bytecode_t& bytecode() const noexcept;
58
59 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:22
Interface for a compiler pass.
ArkScript configuration macros.
The basic value type handled by the compiler.
std::vector< std::string > m_filenames
std::vector< IR::Block > m_ir
An interface to describe compiler passes.
Definition Pass.hpp:24
std::vector< uint8_t > bytecode_t
Definition Common.hpp:22
STL namespace.
A Compiler Value class helper to handle multiple types.