|
| 1 | +#ifndef POUND_JIT_IR_INSTRUCTION_H |
| 2 | +#define POUND_JIT_IR_INSTRUCTION_H |
| 3 | + |
| 4 | +#include "opcode.h" |
| 5 | +#include "value.h" |
| 6 | +#include <stddef.h> |
| 7 | + |
| 8 | +namespace pound::jit::ir { |
| 9 | +// Maximum number of arguments an IR instruction can have. |
| 10 | +#define MAX_IR_ARGS 4 |
| 11 | + |
| 12 | +/*! |
| 13 | + * Represents a single instruction in the IR layer. |
| 14 | + * |
| 15 | + * Each instruction node encapsulates an opcode, its arguments, and pointers to |
| 16 | + * form an intrusive double-linked list. |
| 17 | + */ |
| 18 | +typedef struct instruction_t |
| 19 | +{ |
| 20 | + // The opcode for this instruction. |
| 21 | + opcode_t opcode; |
| 22 | + |
| 23 | + // An array of arguments for this instruction. |
| 24 | + value_t args[MAX_IR_ARGS]; |
| 25 | + |
| 26 | + // Pointer to the next instruction in the intrusive list. |
| 27 | + struct instruction_t *next; |
| 28 | + |
| 29 | + // Pointer to the previous instruction the intrusive list. |
| 30 | + struct instruction_t *previous; |
| 31 | +} instruction_t; |
| 32 | + |
| 33 | +/*! |
| 34 | + * @brief Represents a double-linked list of IR instructions. |
| 35 | + * |
| 36 | + * This structure holds the head and tail pointers of an intrusive list |
| 37 | + * composed of `instruction_t` nodes. It is used to store sequences |
| 38 | + */ |
| 39 | +typedef struct |
| 40 | +{ |
| 41 | + // Pointer to the first instruction in the list. |
| 42 | + instruction_t *head; |
| 43 | + |
| 44 | + // Pointer to the last instruction in the list. |
| 45 | + instruction_t *tail; |
| 46 | +} instruction_list_t; |
| 47 | + |
| 48 | +/*! |
| 49 | + * @brief Gets a pointer to the argument at a specific index. |
| 50 | + * |
| 51 | + * @param instruction Pointer to the IR instruction. |
| 52 | + * @param arg_index The index of the argument to retrieve. |
| 53 | + * |
| 54 | + * @return A constant pointer to the argument at the specified index. |
| 55 | + * @pre `instruction` must not be NULL |
| 56 | + * @pre `arg_index` must be less than `MAX_IR_ARGS`. |
| 57 | + */ |
| 58 | +const value_t* instruction_get_arg (const instruction_t *instruction, const size_t arg_index); |
| 59 | + |
| 60 | +/*! |
| 61 | + * Retrieves a U64 argument from an instruction. |
| 62 | + * |
| 63 | + * @param instruction Pointer to the IR instruction. |
| 64 | + * @apram arg_index The index of the argument to retrieve. |
| 65 | + * |
| 66 | + * @return The U64 value of the argument. |
| 67 | + * @pre `instruction` must not be NULL. |
| 68 | + * @pre `arg_index` must be less than `MAX_IR_ARGS`. |
| 69 | + * @pre The argument at `arg_index` must be of type `IR_TYPE_U64`. |
| 70 | + */ |
| 71 | +const uint64_t instruction_get_arg_u64(const instruction_t *instruction, const size_t arg_index); |
| 72 | + |
| 73 | +/*! |
| 74 | + * @brief Retrieves a U32 argument from an instruction. |
| 75 | + * |
| 76 | + * @param instruction Pointer to the IR instruction. |
| 77 | + * @param arg_index The index of the argument to retrieve. |
| 78 | + * |
| 79 | + * @return The U32 value of the argument. |
| 80 | + * @pre `instruction` must not be NULL. |
| 81 | + * @pre `arg_index` must be less than `MAX_IR_ARGS`. |
| 82 | + * @pre The argument at `arg_index` must be of type `IR_TYPE_U32`. |
| 83 | + */ |
| 84 | +const uint32_t instruction_get_arg_u32(const instruction_t *instruction, const size_t arg_index); |
| 85 | + |
| 86 | +/*! |
| 87 | + * Retrives a U8 argument from an instruction. |
| 88 | + * |
| 89 | + * @param instruction Pointer to the IR instruction. |
| 90 | + * @param arg_index The index of the argument to retrieve. |
| 91 | + * |
| 92 | + * @return The U8 value of the argument. |
| 93 | + * @pre `instruction` must not be NULL. |
| 94 | + * @pre `arg_index` must be less than `MAX_IR_ARGS`. |
| 95 | + * @pre The argument at `arg_index` must be of type `IR_TYPE_U8`. |
| 96 | + */ |
| 97 | +const uint8_t instruction_get_arg_u8(const instruction_t *instruction, const size_t arg_index); |
| 98 | + |
| 99 | +/*! |
| 100 | + * @brief Retrieves a U1 (boolean) argument from an instruction. |
| 101 | + * |
| 102 | + * @param instruction Pointer to the IR instruction. |
| 103 | + * @param arg_index The index of the argument to retrieve. |
| 104 | + * |
| 105 | + * @return The boolean value of the argument. |
| 106 | + * @pre `instruction` must not be NULL. |
| 107 | + * @pre `arg_index` must be less than `MAX_IR_ARGS`. |
| 108 | + * @pre The argument at `arg_index` must be of type `IR_TYPE_U1`. |
| 109 | + */ |
| 110 | +const bool instruction_get_arg_u1(const instruction_t *instruction, const size_t arg_index); |
| 111 | + |
| 112 | +/*! |
| 113 | + * @brief Retrieves an A32 register identifier argument from an instruction. |
| 114 | + * |
| 115 | + * @param instruction Pointer to the IR instruction. |
| 116 | + * @param arg_index The index of the argument to retrieve. |
| 117 | + * |
| 118 | + * @return The `a32_register_t` identifier. |
| 119 | + * @pre `instruction` must not be NULL. |
| 120 | + * @pre `arg_index` must be less than `MAX_IR_ARGS`. |
| 121 | + * @pre The argument at `arg_index` must be of type `IR_TYPE_A32_REGISTER`. |
| 122 | + */ |
| 123 | +const pound::jit::a32_register_t instruction_get_arg_a32_register(const instruction_t *instruction, const size_t arg_index); |
| 124 | + |
| 125 | +/*! |
| 126 | + * @brief Gets the return type of an instruction based on its opcode. |
| 127 | + * |
| 128 | + * @param instruction Pointer to the IR instruction. |
| 129 | + * |
| 130 | + * @return The `type_t` that this instruction's opcode returns. |
| 131 | + * @pre `instruction` must not be NULL. |
| 132 | + * @pre `instruction->opcode` must be a valid opcode index (less than `NUM_OPCODE`). |
| 133 | + */ |
| 134 | +const type_t instruction_get_return_type (const instruction_t *instruction); |
| 135 | + |
| 136 | + |
| 137 | +/*! |
| 138 | + * @brief Gets the name of an instruction's opcode as a C-string. |
| 139 | + * |
| 140 | + * @param instruction Pointer to the IR instruction. |
| 141 | + * |
| 142 | + * @return A constant C-string containing the opcode's name. |
| 143 | + * @pre `instruction` must not be NULL. |
| 144 | + * @pre `instruction->opcode` must be a valid opcode index (less than `NUM_OPCODE`). |
| 145 | + * @pre The global `g_opcodes` array must be initialized and accessible. |
| 146 | + */ |
| 147 | +const char* instruction_get_opcode_name(const instruction_t *instruction); |
| 148 | + |
| 149 | +/*! |
| 150 | + * @brief Appends an instruction to the tail of an instruction list. |
| 151 | + * |
| 152 | + * The instruction is added to the end of the list. If the list is empty, |
| 153 | + * the instruction becomes both the head and the tail. |
| 154 | + * |
| 155 | + * @param list Pointer to the instruction list to modify. |
| 156 | + * @param instruction Pointer to the `instruction_t` node to append. |
| 157 | + * |
| 158 | + * @pre `list` must not be NULL. |
| 159 | + * @pre `instruction` must not be NULL. |
| 160 | + */ |
| 161 | +void instruction_list_append (instruction_list_t *list, instruction_t *instruction); |
| 162 | + |
| 163 | +/*! |
| 164 | + * @brief Removes an instruction from an instruction list. |
| 165 | + * |
| 166 | + * @param list Pointer to the instruction list to modify. |
| 167 | + * @param instruction Pointer to the `instruction_t` node to remove. |
| 168 | + * |
| 169 | + * @pre `list` must not be NULL. |
| 170 | + * @pre `instruction` must not be NULL. |
| 171 | + * @pre `instruction` must be a member of `list`. |
| 172 | + */ |
| 173 | +void instruction_list_remove (instruction_list_t *list, instruction_t *instruction); |
| 174 | +} |
| 175 | +#endif // POUND_JIT_IR_INSTRUCTION_H |
0 commit comments