ArkScript
A small, lisp-inspired, functional scripting language
Word.hpp
Go to the documentation of this file.
1/**
2 * @file Word.hpp
3 * @author Lex Plateau (lexplt.dev@gmail.com)
4 * @brief Describe an instruction and its immediate argument
5 * @date 2022-07-02
6 *
7 * @copyright Copyright (c) 2022-2025
8 *
9 */
10
11#ifndef ARK_COMPILER_WORD_HPP
12#define ARK_COMPILER_WORD_HPP
13
14#include <cinttypes>
15
16namespace Ark::internal
17{
18 struct Word
19 {
20 uint8_t opcode = 0; ///< Instruction opcode
21 uint8_t byte_1 = 0;
22 uint8_t byte_2 = 0;
23 uint8_t byte_3 = 0;
24
25 explicit Word(const uint8_t inst, const uint16_t arg = 0) :
26 opcode(inst), byte_2(static_cast<uint8_t>(arg >> 8)), byte_3(static_cast<uint8_t>(arg & 0xff))
27 {}
28
29 /**
30 * @brief Construct a word with two arguments, each on 12 bits. It's up to the caller to ensure that no data is lost
31 * @param inst
32 * @param primary_arg argument on 12 bits, the upper 4 bits are lost
33 * @param secondary_arg 2nd argument on 12 bits, the upper 4 bits are lost
34 */
35 Word(const uint8_t inst, const uint16_t primary_arg, const uint16_t secondary_arg) :
36 opcode(inst)
37 {
38 byte_1 = static_cast<uint8_t>((secondary_arg & 0xff0) >> 4);
39 byte_2 = static_cast<uint8_t>((secondary_arg & 0x00f) << 4 | (primary_arg & 0xf00) >> 8);
40 byte_3 = static_cast<uint8_t>(primary_arg & 0x0ff);
41 }
42 };
43}
44
45#endif
uint8_t opcode
Instruction opcode.
Definition Word.hpp:20
Word(const uint8_t inst, const uint16_t primary_arg, const uint16_t secondary_arg)
Construct a word with two arguments, each on 12 bits. It's up to the caller to ensure that no data is...
Definition Word.hpp:35
Word(const uint8_t inst, const uint16_t arg=0)
Definition Word.hpp:25