-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compiler): the compiler now outputs an IR instead of bytecode, w…
…hich is now generated by the IRCompiler
- Loading branch information
Showing
8 changed files
with
509 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
include/Ark/Compiler/IntermediateRepresentation/Entity.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* @file Entity.hpp | ||
* @author Alexandre Plateau ([email protected]) | ||
* @brief An entity in the IR is a bundle of information | ||
* @version 0.1 | ||
* @date 2024-10-05 | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
|
||
#ifndef ARK_COMPILER_INTERMEDIATEREPRESENTATION_ENTITY_HPP | ||
#define ARK_COMPILER_INTERMEDIATEREPRESENTATION_ENTITY_HPP | ||
|
||
#include <cinttypes> | ||
#include <vector> | ||
|
||
#include <Ark/Compiler/Word.hpp> | ||
#include <Ark/Compiler/Instructions.hpp> | ||
|
||
namespace Ark::internal::IR | ||
{ | ||
enum class Kind | ||
{ | ||
Label, | ||
Goto, | ||
GotoIfTrue, | ||
GotoIfFalse, | ||
Opcode | ||
}; | ||
|
||
using label_t = std::size_t; | ||
|
||
class Entity | ||
{ | ||
public: | ||
explicit Entity(Kind kind); | ||
|
||
explicit Entity(Instruction inst, uint16_t arg = 0); | ||
|
||
static Entity Label(); | ||
|
||
static Entity Goto(const Entity& label); | ||
|
||
static Entity GotoIf(const Entity& label, bool cond); | ||
|
||
[[nodiscard]] Word bytecode() const; | ||
|
||
[[nodiscard]] inline label_t label() const { return m_label; } | ||
|
||
[[nodiscard]] inline Kind kind() const { return m_kind; } | ||
|
||
private: | ||
inline static label_t LabelCounter = 0; | ||
|
||
Kind m_kind; | ||
label_t m_label { 0 }; | ||
Instruction m_inst; | ||
uint8_t m_secondary_arg { 0 }; | ||
uint16_t m_primary_arg { 0 }; | ||
}; | ||
|
||
using Block = std::vector<Entity>; | ||
} | ||
|
||
#endif // ARK_COMPILER_INTERMEDIATEREPRESENTATION_ENTITY_HPP |
79 changes: 79 additions & 0 deletions
79
include/Ark/Compiler/IntermediateRepresentation/IRCompiler.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* @file IRCompiler.hpp | ||
* @author Alexandre Plateau ([email protected]) | ||
* @brief Compile the intermediate representation to bytecode | ||
* @version 0.1 | ||
* @date 2024-10-05 | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
|
||
#ifndef ARK_COMPILER_INTERMEDIATEREPRESENTATION_IRCOMPILER_HPP | ||
#define ARK_COMPILER_INTERMEDIATEREPRESENTATION_IRCOMPILER_HPP | ||
|
||
#include <vector> | ||
#include <string> | ||
|
||
#include <Ark/Platform.hpp> | ||
#include <Ark/Logger.hpp> | ||
#include <Ark/Compiler/Common.hpp> | ||
#include <Ark/Compiler/ValTableElem.hpp> | ||
#include <Ark/Compiler/IntermediateRepresentation/Entity.hpp> | ||
|
||
namespace Ark::internal | ||
{ | ||
class ARK_API IRCompiler final | ||
{ | ||
public: | ||
/** | ||
* @brief Create a new IRCompiler | ||
* | ||
* @param debug debug level | ||
*/ | ||
explicit IRCompiler(unsigned debug); | ||
|
||
/** | ||
* @brief Turn a given IR into bytecode | ||
* | ||
* @param pages list of lists of IR entities generated by the compiler | ||
* @param symbols symbol table generated by the compiler | ||
* @param values value table generated by the compiler | ||
*/ | ||
void process(const std::vector<IR::Block>& pages, const std::vector<std::string>& symbols, const std::vector<ValTableElem>& values); | ||
|
||
/** | ||
* @brief Return the constructed bytecode object | ||
* | ||
* @return const bytecode_t& | ||
*/ | ||
[[nodiscard]] const bytecode_t& bytecode() const noexcept; | ||
|
||
private: | ||
Logger m_logger; | ||
bytecode_t m_bytecode; | ||
std::vector<IR::Block> m_ir; | ||
|
||
void compile(); | ||
|
||
/** | ||
* @brief Push a word to the m_bytecode | ||
* @param word | ||
*/ | ||
void pushWord(const Word& word); | ||
|
||
/** | ||
* @brief Push the file headers (magic, version used, timestamp) | ||
* | ||
*/ | ||
void pushFileHeader() noexcept; | ||
|
||
/** | ||
* @brief Push the symbols and values tables | ||
* | ||
*/ | ||
void pushSymAndValTables(const std::vector<std::string>& symbols, const std::vector<ValTableElem>& values); | ||
}; | ||
} | ||
|
||
#endif // ARK_COMPILER_INTERMEDIATEREPRESENTATION_IRCOMPILER_HPP |
Oops, something went wrong.