-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.cpp
More file actions
191 lines (165 loc) · 5.65 KB
/
runtime.cpp
File metadata and controls
191 lines (165 loc) · 5.65 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
#include <cassert>
#include <algorithm>
#include "main.h"
//////////////////////////////////////////////////////////////////////
RuntimeGenerator::RuntimeGenerator(FinalModel* intermed)
: m_final(intermed)
{
assert(intermed != nullptr);
}
void RuntimeGenerator::ParseRuntimeTemplate(std::istream* pInput)
{
std::vector<string> lines; // lines for the current block
RuntimeSymbol blockrtsymbol = RuntimeNone; // name for the current block
std::vector<RuntimeSymbol> blockneeds; // dependencies for the current block
char buffer[256];
bool preambule = true;
while (!pInput->eof())
{
pInput->getline(buffer, sizeof(buffer));
if (*buffer == 0) // skip empty lines
continue;
string line(buffer);
if (preambule)
{
if (line.find(";####") == std::string::npos) // not start of block
continue; // skip this line
preambule = false; // start of the first block
}
if (line.find(";####") == 0) // start of block
{
if (!lines.empty())
{
RuntimeBlock block;
block.rtsymbol = blockrtsymbol;
block.lines = lines;
block.needs = blockneeds;
m_rtblocks.push_back(block);
}
lines.clear();
blockrtsymbol = RuntimeNone;
blockneeds.clear();
}
else if (line.find(";## Need ") == 0) // list of dependencies
{
string needname = line.substr(9);
RuntimeSymbol needrtsymbol = FindRuntimeSymbolByName(needname);
if (needrtsymbol == RuntimeNone)
{
std::cerr << "Runtime template parsing ERROR: unknown Need name " << needname << std::endl;
RegisterError();
return;
}
blockneeds.push_back(needrtsymbol);
}
else if (line.find(";## ") == 0) // block name
{
string blockname = line.substr(4);
blockrtsymbol = FindRuntimeSymbolByName(blockname);
if (blockrtsymbol == RuntimeNone)
{
std::cerr << "Runtime template parsing ERROR: unknown block name " << blockname << std::endl;
RegisterError();
return;
}
}
else
{
lines.push_back(line);
}
}
if (!lines.empty())
{
RuntimeBlock block;
block.rtsymbol = blockrtsymbol;
block.lines = lines;
block.needs = blockneeds;
m_rtblocks.push_back(block);
}
}
RuntimeBlock RuntimeGenerator::FindRuntimeBlock(RuntimeSymbol rtsymbol)
{
for (auto it = std::begin(m_rtblocks); it != std::end(m_rtblocks); ++it)
{
if (rtsymbol == it->rtsymbol)
return *it;
}
return RuntimeBlock(); // empty block
}
void RuntimeGenerator::GetRuntimeBlock(RuntimeSymbol rtsymbol, std::vector<string>& copyto)
{
RuntimeBlock rtblock = FindRuntimeBlock(rtsymbol);
if (rtblock.rtsymbol == RuntimeNone)
{
string rtsymbolname = GetRuntimeSymbolName(rtsymbol);
std::cerr << "Runtime block \'" << rtsymbolname << "\' not found." << std::endl;
RegisterError();
return;
}
std::copy(rtblock.lines.begin(), rtblock.lines.end(), std::back_inserter(copyto));
}
void RuntimeGenerator::GenerateRuntime(const std::set<RuntimeSymbol>& needs)
{
for (RuntimeSymbol rtsymbol : needs)
m_needs.insert(rtsymbol);
std::vector<RuntimeSymbol> listnewneeds;
std::copy(m_needs.begin(), m_needs.end(), std::back_inserter(listnewneeds));
while (true)
{
std::vector<RuntimeSymbol> listneeds;
std::copy(listnewneeds.begin(), listnewneeds.end(), std::back_inserter(listneeds));
listnewneeds.clear();
// First collect all dependencies in m_needs
for (RuntimeSymbol rtsymbol : listneeds)
{
string rtsymbolname = GetRuntimeSymbolName(rtsymbol);
// Find symbol block
RuntimeBlock rtblock = FindRuntimeBlock(rtsymbol);
if (rtblock.rtsymbol == RuntimeNone)
{
//TODO: error
}
for (RuntimeSymbol need : rtblock.needs)
{
if (m_needs.find(need) == m_needs.end())
{
listnewneeds.push_back(need);
m_needs.insert(need);
}
}
}
if (listnewneeds.empty())
break;
// Still have newly added needs, repeat
}
//NOTE: Now we have all the dependencies in m_needs
// Prepare for the second pass
// Generate all the runtime code
for (RuntimeSymbol rtsymbol : m_needs)
{
string rtsymbolname = GetRuntimeSymbolName(rtsymbol);
m_final->AddRuntimeLine("");
//AddLine("; " + rtsymbolname);
// Find symbol block
RuntimeBlock rtblock = FindRuntimeBlock(rtsymbol);
if (rtblock.rtsymbol == RuntimeNone)
{
//Error("Runtime generator for symbol " + rtsymbolname + " not found.");
AddLine(rtsymbolname + (g_turbo8 ? ":" : "::"));
AddLine("; TODO: Runtime generator for symbol " + rtsymbolname + " not found.");
AddLine("\tRETURN ;STUB");
continue;
}
if (!g_turbo8)
AddLine("\t.GLOBL\t" + rtsymbolname);
// copy lines to final model
for (string line : rtblock.lines)
AddLine(line);
}
}
void RuntimeGenerator::NeedRuntime(RuntimeSymbol rtsymbol)
{
m_needs.insert(rtsymbol);
}
//////////////////////////////////////////////////////////////////////