-
Notifications
You must be signed in to change notification settings - Fork 0
/
tvm.h
150 lines (121 loc) · 4.11 KB
/
tvm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#ifndef SPRAWL_TVM_TVM_H_
#define SPRAWL_TVM_TVM_H_
#include <cstdint>
#include <iostream>
#include <iomanip>
#include <istream>
#include <string>
#include <map>
#include <vector>
namespace psy::tvm {
typedef std::uint8_t u8;
typedef std::uint16_t u16;
typedef std::uint32_t u32;
typedef std::uint64_t u64;
enum class OpCode { /* 4 bits -- c++14 */
kAdd = 0b0001'0000'00000000,
kAnd = 0b0101'0000'00000000,
kBr = 0b0000'000'000000000,
kBrn = 0b0000'100'000000000,
kBrz = 0b0000'010'000000000,
kBrp = 0b0000'001'000000000,
kBrnp = kBr | kBrn | kBrp,
kBrnz = kBr | kBrn | kBrz,
kBrzp = kBr | kBrz | kBrp,
kBrnzp = kBr | kBrn | kBrz | kBrp,
kJmp = 0b0100'0000'00000000,
kLd = 0b0010'000'000000000,
kLdi = 0b1010'0000'00000000,
kLdr = 0b0110'000'000000000,
kLea = 0b1110'000'000000000,
kSt = 0b0011'0000'00000000,
kNot = 0b1001'0000'00000000,
kTrap = 0b1111'0000'00000000,
kTrapGetc = kTrap | 0x20,
kTrapOut = kTrap | 0x21,
kTrapPuts = kTrap | 0x22,
kTrapIn = kTrap | 0x23,
kTrapPutsp = kTrap | 0x24,
kTrapHalt = kTrap | 0x25,
kRet = 0b1100'000'111'000000,
kIllegal = 0b1101'0000'00000000, /* ☠ */
};
OpCode StringToOpcode(const std::string& opstr);
u16 StringToRegister(const std::string& regstr);
constexpr u16 AsSR1(u16 sr1) { return sr1 << 6; }
constexpr u16 AsDR(u16 dr) { return dr << 9; }
class Token {
public:
enum class Type {
Segment, Instruction, Register,
Label, NumberLiteral, HexNumber,
Punct, String, Unknown };
Token(std::string&& tok, size_t ln) :
value_(std::move(tok)), line_number_(ln),
type_(Type::Unknown), address_(0) {
Classify();
}
std::string TypeString() const;
u16 HexToU16() const;
u16 LitValue() const;
u16 U16Value() const;
std::string value_;
size_t line_number_;
Type type_;
OpCode opcode_;
u16 address_;
private:
void Classify();
};
struct InstructionRow {
InstructionRow() :
label_(nullptr), op_(nullptr),
operands_({nullptr}) {}
inline int AssignLabel(std::vector<Token>::iterator& it,
std::vector<Token>::iterator end);
inline int AssignOp(std::vector<Token>::iterator& it,
std::vector<Token>::iterator end);
inline int AssignOperand(std::vector<Token>::iterator& it,
std::vector<Token>::iterator end,
u8 pos);
Token* label_;
Token* op_;
Token* operands_[3];
friend std::ostream& operator<<(std::ostream& os, const InstructionRow& row) {
auto safe_print_fn = [](const Token* r) noexcept -> std::string {
return r == nullptr ? ".." : r->value_;
};
os << std::setw(20) << std::setfill(' ') << safe_print_fn(row.label_)
<< std::setw(20) << std::setfill(' ') << safe_print_fn(row.op_)
<< std::setw(20) << std::setfill(' ') << safe_print_fn(row.operands_[0])
<< std::setw(20) << std::setfill(' ') << safe_print_fn(row.operands_[1])
<< std::setw(20) << std::setfill(' ') << safe_print_fn(row.operands_[2]);
return os;
}
};
struct InstructionSet {
std::vector<InstructionRow> rows_;
std::map<std::string, const Token*> symtab_;
friend std::ostream& operator<<(std::ostream& os, const InstructionSet& iset) {
size_t address = 0;
os << "INSTA =============" << std::endl;
for (const auto& row : iset.rows_)
os << std::hex << std::setw(4) << std::setfill('0') << 0x10 * address++
<< row << std::endl;
os << "SYMTAB ============" << std::endl;
for (auto it = iset.symtab_.cbegin(); it != iset.symtab_.end(); ++it)
os << it->first << "@" << std::hex << it->second->address_ << std::endl;
return os;
}
};
struct Image {
Image() : data_({0}), len_(0) {}
u16 data_[0xffff];
size_t len_;
};
std::vector<Token> Tokenize(std::istream& is);
void BuildSymtab(InstructionSet& inset);
InstructionSet Parse(std::vector<Token>& tokens);
Image Encode(InstructionSet& inset);
}
#endif