forked from kittybupu/openVCB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenVCBAssembler.cpp
More file actions
236 lines (197 loc) · 7.6 KB
/
openVCBAssembler.cpp
File metadata and controls
236 lines (197 loc) · 7.6 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// ReSharper disable CppTooWideScopeInitStatement
// ReSharper disable StringLiteralTypo
// ReSharper disable CppClangTidyCertErr33C
#include "openVCB.h"
#include "openVCB_Expr.hh"
namespace openVCB {
/****************************************************************************************/
template <size_t N>
static constexpr bool
prefix(char const *str, char const (&pre)[N])
{
return ::strncmp(pre, str, N - 1) == 0;
}
static std::string
getNext(char *buf, size_t &pos)
{
while (buf[pos] == ' ' || buf[pos] == '\t')
pos++;
char *ptr = strchr(buf + pos, ' ');
char *tab = strchr(buf + pos, '\t');
if (ptr == nullptr || (tab != nullptr && tab < ptr))
ptr = tab;
if (!ptr) {
// XXX Does this really need to be a copy?
auto res = std::string(buf + pos);
pos = -1;
return res;
}
// XXX This too: wouldn't a string view work?
auto res = std::string(buf + pos, ptr);
while (*ptr == ' ' || *ptr == '\t')
++ptr;
pos = ptr - buf;
return res;
}
static std::string
getNextNoRef(char *buf, size_t pos)
{
return getNext(buf, pos);
}
static std::string
getNextLine(char const *buf, uint32_t &pos, uint32_t &lineNum)
{
while (buf[pos] && (isspace(buf[pos]) || buf[pos] == ';'))
if (buf[pos++] == '\n')
++lineNum;
uint32_t start = pos;
int64_t end = -1;
while (buf[pos] && buf[pos] != '\n' && buf[pos] != ';') {
if (end < 0 && buf[pos] == '#')
end = pos;
++pos;
}
if (buf[pos] == '\n')
++lineNum;
if (end < 0)
end = pos;
// XXX Again here, must this be a copy?
return {buf + start, buf + end};
}
/*--------------------------------------------------------------------------------------*/
void
Project::assembleVmem(char *errp, size_t errSize)
{
if (!vmem.i)
return;
lineNumbers.clear();
char errBuf[512];
// Scan through everything once to obtain values for labels
assemblySymbols.clear();
char const *asmBuffer = assembly.data();
uint32_t loc = 1;
uint32_t lineNum = 0;
uint32_t lineLoc = 0;
while (lineLoc < assembly.size()) {
// ReadOn a line in.
std::string line = getNextLine(asmBuffer, lineLoc, lineNum);
if (line.empty())
continue;
char *buf = line.data();
// Parse this stuff
if (prefix(buf, "symbol") || prefix(buf, "resymb")) {
/* nothing */
} else if (prefix(buf, "unsymb")) {
/* nothing */
} else if (prefix(buf, "unpoint")) {
/* nothing */
} else if (prefix(buf, "pointer") || prefix(buf, "repoint")) {
size_t k = 7;
std::string label = getNext(buf, k);
std::string addr = getNext(buf, k);
if (addr == "inline")
++loc;
} else if (buf[0] == '@') {
size_t k = 1;
std::string label = getNext(buf, k);
assemblySymbols[label] = loc;
} else if (prefix(buf, "bookmark")) {
/* nothing */
} else if (prefix(buf, "sub_bookmark")) {
/* nothing */
} else {
++loc;
}
}
// Scan through everything again to assemble
loc = 1;
lineLoc = 0;
lineNum = 0;
while (lineLoc != assembly.size()) {
uint32_t lNum = lineNum;
// ReadOn a line in.
std::string line = getNextLine(asmBuffer, lineLoc, lineNum);
if (line.empty())
continue;
char *buf = line.data();
errBuf[0] = '\0';
// Parse this stuff
if (prefix(buf, "symbol") || prefix(buf, "resymb")) {
size_t k = 6;
std::string label = getNext(buf, k);
uint32_t val = evalExpr(buf + k, assemblySymbols, errBuf);
assemblySymbols[label] = val;
} else if (prefix(buf, "unsymb")) {
size_t k = 6;
std::string label = getNext(buf, k);
assemblySymbols.erase(label);
} else if (prefix(buf, "pointer") || prefix(buf, "repoint")) {
size_t k = 7;
std::string label = getNext(buf, k);
std::string addr = getNext(buf, k);
uint32_t addrVal = addr == "inline"
? loc++
: evalExpr(addr.c_str(), assemblySymbols, errBuf);
uint32_t val = evalExpr(buf + k, assemblySymbols, errBuf);
addrVal = addrVal % vmemSize;
assemblySymbols[label] = addrVal;
vmem.i[addrVal] = val;
lineNumbers[addrVal] = lNum;
} else if (prefix(buf, "unpoint")) {
std::string label = getNextNoRef(buf, std::size("unpoint") - 1U);
assemblySymbols.erase(label);
} else if (buf[0] == '@') {
/* TODO */
} else if (prefix(buf, "bookmark")) {
/* TODO */
} else if (prefix(buf, "sub_bookmark")) {
/* TODO */
} else {
auto val = evalExpr(buf, assemblySymbols, errBuf);
// printf("%s (0x%08x=0x%08x)\n", buf, loc, val);
auto addrVal = loc++ % vmemSize;
vmem.i[addrVal] = val;
lineNumbers[addrVal] = lNum;
}
if (*errBuf)
snprintf(errp, errSize, "line %u: %s", lineNum, errBuf);
if (loc >= vmemSize) {
if (errp)
std::ranges::copy("VMem exceeded.", errp);
break;
}
}
std::pair<LatchInterface &, char const *> latchWrapper[] = {
{vmAddr, "address"},
{vmData, "data"}
};
// Set VMem latch ids with mostly pointless but nifty c++ features.
for (auto &[data, msg] : latchWrapper) {
for (int i = 0; i < data.numBits; ++i) {
glm::ivec2 pos = data.pos + i * data.stride;
data.gids[i] = indexImage[pos.x + pos.y * width];
if (data.gids[i] == -1 || SetOff(states[data.gids[i]].logic) != Logic::Latch) {
::printf("error: No %s latch at VMem position %d %d\n",
msg, pos.x, pos.y);
::exit(1);
}
}
}
// dumpVMemToText("vmemDump.bin");
}
void
Project::dumpVMemToText(std::string const &p) const
{
FILE *file = ::fopen(p.c_str(), "w");
if (!file) {
auto e = std::system_error{errno, std::generic_category()};
std::cerr << "Error opening file \"" << p
<< "\" for VMem dumping: " << e.what() << '\n';
return;
}
for (size_t i = 0; i < vmemSize; ++i)
::fprintf(file, "a: 0x%08zx = 0x%08x\n", i, vmem.i[i]);
::fclose(file);
}
/****************************************************************************************/
} // namespace openVCB