-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdeluxe.h
207 lines (163 loc) · 4.79 KB
/
deluxe.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unordered_map>
#include <vector>
#include <string>
#include "tokenizer.h"
#include "registers.h"
#include "stringfragment.h"
enum class OutputKind
{
kStringLiteral,
kNamedRegister,
kSpill,
kRestore,
kProcHeader,
kProcFooter,
kStackVar,
kLineDirective
};
struct ProcedureDef
{
uint32_t m_UsedRegs = 0;
uint32_t m_InputRegs = 0;
uint32_t m_TrashedRegs = 0;
bool m_SaveInputRegs = false;
};
struct OutputElement
{
OutputElement() = default;
constexpr explicit OutputElement(StringFragment f) : m_String(f) {}
explicit OutputElement(OutputKind kind, int intVal) : m_IntValue(intVal), m_Kind(kind) {}
explicit OutputElement(OutputKind kind, StringFragment f) : m_String(f), m_Kind(kind) {}
StringFragment m_String;
int m_IntValue = 0;
OutputKind m_Kind = OutputKind::kStringLiteral;
};
class Deluxe68
{
public:
using PrintCallback = void (const char* buf, size_t len, void* user_data);
public:
struct PendingRegisterSpill;
private:
static constexpr size_t kLineMax = 4096;
const char* m_InputData;
size_t m_InputLen;
mutable PrintCallback* m_PrintCallback = nullptr;
mutable void* m_PrintData = nullptr;
std::vector<OutputElement> m_OutputSchedule;
const char* m_Filename;
int m_LineNumber = 0;
int m_ErrorCount = 0;
const char* m_ParsePoint;
bool m_EmitLineDirectives = false;
bool m_ProcSections = false;
int m_CurrentOutputLine = 0;
StringFragment m_CurrentProcName;
ProcedureDef m_CurrentProc;
struct RegState
{
static constexpr uint32_t kFlagAllocated = 1 << 0;
static constexpr uint32_t kFlagReserved = 1 << 1;
uint32_t m_Flags = 0;
StringFragment m_AllocatingVarName;
std::vector<StringFragment> m_SpilledVars;
bool isAllocated() const { return 0 != (m_Flags & kFlagAllocated); }
bool isReserved() const { return 0 != (m_Flags & kFlagReserved); }
bool isInUse() const { return 0 != (m_Flags & (kFlagReserved | kFlagAllocated)); }
void setAllocated(bool state)
{
if (state)
m_Flags |= kFlagAllocated;
else
m_Flags &= ~kFlagAllocated;
}
void setReserved(bool state)
{
if (state)
m_Flags |= kFlagReserved;
else
m_Flags &= ~kFlagReserved;
}
void reset()
{
m_Flags = 0;
m_AllocatingVarName = StringFragment();
m_SpilledVars.clear();
}
void spill()
{
setAllocated(false);
m_SpilledVars.push_back(m_AllocatingVarName);
m_AllocatingVarName = StringFragment();
}
void restore()
{
}
void handleRename(StringFragment idOld, StringFragment idNew)
{
if (m_AllocatingVarName == idOld)
{
m_AllocatingVarName = idNew;
}
for (auto& str : m_SpilledVars)
{
if (str == idOld)
{
str = idNew;
}
}
}
};
RegState m_Registers[kRegisterCount];
struct RegAlloc
{
uint8_t m_RegIndex = 0;
uint8_t m_Spilled = 0;
int m_StackSlot = 0;
int m_AllocatedLine = 0;
};
int m_SpillStackDepth = 0;
std::unordered_map<StringFragment, RegAlloc> m_LiveRegs;
std::unordered_map<StringFragment, ProcedureDef> m_Procedures;
public:
explicit Deluxe68(const char* ifn, const char* data, size_t len, bool emitLineDirectives, bool procSections);
~Deluxe68();
void error(const char *fmt, ...);
void errorForLine(int line, const char *fmt, ...);
void run();
void generateOutput(FILE* f) const;
void generateOutput(PrintCallback* cb, void* user_data) const;
int errorCount() const { return m_ErrorCount; }
private:
void parseLine(StringFragment line);
void bufferLine(const char* line);
void bufferLine(const std::string& line);
void allocRegs(Tokenizer& tokenizer, TokenType regType);
void killRegs(Tokenizer& tokenizer);
void proc(Tokenizer& tokenizer, bool saveInputs);
void endProc(Tokenizer& tokenizer);
void reserve(Tokenizer& tokenizer);
void unreserve(Tokenizer& tokenizer);
void spill(Tokenizer& tokenizer);
void restore(Tokenizer& tokenizer);
void rename(Tokenizer& tokenizer);
void output(OutputElement elem);
void handleRegularLine(StringFragment line);
void newline();
uint32_t usedRegsForProcecure(const StringFragment& procName) const;
void printSpill(uint32_t regMask) const;
void printRestore(uint32_t regMask) const;
void printMovemList(uint32_t regMask) const;
void killAll();
bool doAllocate(StringFragment id, int regIndex);
bool dataLeft() const;
StringFragment nextLine();
bool expect(Tokenizer& t, TokenType type, Token* out = nullptr);
bool accept(Tokenizer& t, TokenType type, Token* out = nullptr);
int findFirstFree(RegisterClass regClass) const;
void outf(const char* fmt, ...) const;
};