-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.cpp
More file actions
156 lines (135 loc) · 4.58 KB
/
runtime.cpp
File metadata and controls
156 lines (135 loc) · 4.58 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
#include <cassert>
#include <algorithm>
#include "main.h"
//////////////////////////////////////////////////////////////////////
RuntimeGenerator::RuntimeGenerator(const std::set<RuntimeSymbol>& needs, FinalModel* intermed)
: m_needs(needs), 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.lines = lines;
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::GenerateRuntime()
{
std::vector<RuntimeSymbol> listneeds;
std::copy(m_needs.begin(), m_needs.end(), std::back_inserter(listneeds));
// 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)
m_needs.insert(need);
//TODO: this should be recursive
}
//NOTE: Now we have all the dependencies in m_needs
// Prepare for the second pass
listneeds.clear();
// Generate all the runtime code
std::copy(m_needs.begin(), m_needs.end(), std::back_inserter(listneeds));
for (RuntimeSymbol rtsymbol : listneeds)
{
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 + ":");
AddLine("; TODO: Runtime generator for symbol " + rtsymbolname + " not found.");
AddLine("\tRETURN ;STUB");
continue;
}
// copy lines to final model
for (string line : rtblock.lines)
AddLine(line);
}
}
void RuntimeGenerator::NeedRuntime(RuntimeSymbol rtsymbol)
{
m_needs.insert(rtsymbol);
}
//////////////////////////////////////////////////////////////////////