-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubroutine.cpp
333 lines (315 loc) · 10.8 KB
/
subroutine.cpp
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <string>
#include <vector>
#include <memory>
#include <format>
#include <queue>
#include <cstdlib>
#include "json.hpp"
using namespace std;
using json = nlohmann::json;
enum Type
{
non_terminal,
terminal,
expression
};
struct Node
{
string name;
Type tp; // terminal, non_terminal, expression
vector<Node *> subnode;
};
class Grammar
{
public:
Grammar(json &content, unsigned maxdepth)
{
map<string, vector<vector<string>>> contentInstd = content.template get<map<string, vector<vector<string>>>>(); // get the content from the json file
for (auto key : contentInstd)
{
allocate_node(key.first, Type::non_terminal); // register all the non-terminal nodes
}
for (auto rule : contentInstd)
{
for (auto expression : rule.second)
{
if (expression.size() == 1) // if the expression only has one element
{
Node *newnode;
if (this->mp.count(expression[0]) == 0)
{
newnode = this->allocate_node(expression[0], Type::terminal);
}
else
{
newnode = mp[expression[0]];
}
mp[rule.first]->subnode.push_back(newnode);
continue;
}
Node *optnodes = allocate_node("", Type::expression);
for (auto option : expression)
{
Node *newnode;
if (this->mp.count(option) == 0)
{
newnode = this->allocate_node(option, Type::terminal);
}
else
{
newnode = mp[option];
}
optnodes->subnode.push_back(newnode);
}
mp[rule.first]->subnode.push_back(optnodes);
}
}
this->start = mp["<start>"];
this->maxdepth = maxdepth;
this->getshortcut();
};
void JIT(string file, int count)
{
string code = R"(#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdbool.h>
#define BUFFER_SIZE 512*1024*1024 // buffer for storing text
)";
code += "#define MAX_DEPTH " + to_string(this->maxdepth) + "\n";
code += R"(typedef struct {
char data[BUFFER_SIZE];
unsigned top;
} Buffer;
Buffer buffer; // Declare a global buffer
#define extend(c) { \
buffer.data[buffer.top++] = c; \
}
#define clean() { \
buffer.top = 0; \
}
unsigned seed; // Random seed
unsigned branch; // To hold branch value
typedef void (*Inst)();
Inst *PC;
// xor to get random number
#define xor(l) \
seed ^= seed << 13; \
seed ^= seed >> 17; \
seed ^= seed << 5; \
branch = seed % l
bool endless = false;
)";
// Create the signature of the functions
for(auto &x : nodes){
code += "void func_" + to_string(reinterpret_cast<uintptr_t>(x)) + "(unsigned depth);\n";
}
// Create the PROGRAMS
// string init_program_name = "";
// for(auto x : nodes){
// if(x->tp == Type::non_terminal){
// for(int i=0;i< x->subnode.size();i++){
// if(x!=this->start){
// code+="Inst func_" + to_string(reinterpret_cast<uintptr_t>(x)) + "_op" + to_string(i) + "[2] = {func_" + to_string(reinterpret_cast<uintptr_t>(x->subnode[i])) + ", RETURN};\n";
// } else {
// code+="Inst func_" + to_string(reinterpret_cast<uintptr_t>(x)) + "_op" + to_string(i) + "[2] = {func_" + to_string(reinterpret_cast<uintptr_t>(x->subnode[i])) + ", HALT};\n";
// init_program_name = "func_" + to_string(reinterpret_cast<uintptr_t>(x)) + "_op" + to_string(i);
// }
// }
// } else if(x->tp == Type::expression){
// code += "Inst exp_" + to_string(reinterpret_cast<uintptr_t>(x)) + "[" + to_string(x->subnode.size()+1) + "] = {";
// for(int i=0;i<x->subnode.size();i++){
// code += "func_" + to_string(reinterpret_cast<uintptr_t>(x->subnode[i])) + ", ";
// }
// code += "RETURN};\n";
// }
// }
for(auto &x: nodes){
code += "void func_" + to_string(reinterpret_cast<uintptr_t>(x)) + "(register unsigned depth) {\n";
if(x->tp == Type::non_terminal){
code += " if(depth > MAX_DEPTH) {\n";
for(int j=0;j<this->shortcut[x].size();j++){
code += " extend(" + to_string((unsigned)shortcut[x][j]) + ");\n";
}
code += " return;\n";
code += " }\n";
code += " xor(" + to_string(x->subnode.size()) + ");\n";
code += " switch (branch) {\n";
for(int j=0;j<x->subnode.size();j++){
code += " case " + to_string(j) + ":\n";
code += " func_" + to_string(reinterpret_cast<uintptr_t>(x->subnode[j])) + "(depth+1);\n";
code += " break;\n";
}
code += " }\n";
} else if(x->tp == Type::expression){
code += " if(depth > MAX_DEPTH) {\n";
for(int j=0;j<this->shortcut[x].size();j++){
code += " extend(" + to_string((unsigned)shortcut[x][j]) + ");\n";
}
code += " return;\n";
code += " }\n";
for(auto &k:x->subnode){
code += " func_" + to_string(reinterpret_cast<uintptr_t>(k)) + "(depth+1);\n";
}
} else if(x->tp == Type::terminal){
for(int j=0;j<x->name.size();j++){
code += " extend(" + to_string((unsigned)x->name[j]) + ");\n";
}
}
code += " return;\n";
code += "}\n";
}
code += "int main(void) {\n";
code += " static unsigned count = " + to_string(count) + ";\n";
code += " seed = time(NULL);\n";
if(count == -1){
code += " endless = true;\n";
}
code += " while(endless || (count>0) ) {\n";
code += " func_" + to_string(reinterpret_cast<uintptr_t>(this->start)) + "(1);\n";
code += " count--;\n";
code += " printf(\"%.*s\\n\", (int)buffer.top, buffer.data);\n";
code += " clean();\n";
code += " }\n";
code += " return 0;\n";
code += "}\n";
std::ofstream ofs(file, std::ofstream::out | std::ofstream::trunc);
ofs << code;
ofs.close();
std::cout << "Code written to file successfully." << std::endl;
}
private:
vector<Node *> nodes;
map<string, Node *> mp;
int count = 0;
Node *start;
unsigned maxdepth;
map<Node *, string> shortcut;
Node *allocate_node(string name, Type tp)
{
Node *newnode = new Node();
newnode->tp = tp;
if (tp == Type::expression)
{
newnode->name = "expression" + to_string(count++);
}
else
{
newnode->name = name;
}
nodes.push_back(newnode);
mp[newnode->name] = newnode;
return newnode;
}
void getshortcut()
{
for (auto &i : nodes)
{
if (i->tp == Type::terminal)
{
shortcut[i] = i->name;
}
}
while (1)
{
// indicate at least find a shortcut or not
bool flag = false;
for (auto &i : nodes)
{
if (i->tp == Type::terminal || shortcut.count(i))
{
continue;
}
if (i->tp == Type::non_terminal)
{
for (auto &j : i->subnode)
{
if (shortcut.count(j))
{
shortcut[i] = shortcut[j];
flag = true;
}
}
}
else if (i->tp == Type::expression)
{
string combination = "";
bool allNode = true;
for (auto &j : i->subnode)
{
if (shortcut.count(j))
{
combination += shortcut[j];
}
else
{
allNode = false;
break;
}
}
if (allNode)
{
shortcut[i] = combination;
flag = true;
}
}
}
if (!flag)
{
break;
}
}
}
};
int main(int argc, char *argv[])
{
unsigned depth = 0;
int count = 1;
std::string path;
std::string outputFile;
bool show = false;
for (int i = 1; i < argc; i++)
{
std::string arg = argv[i];
if (arg == "-d" && i + 1 < argc)
{
depth = static_cast<unsigned int>(std::atoi(argv[++i]));
}
else if (arg == "-p" && i + 1 < argc)
{
path = argv[++i];
}
else if (arg == "-o" && i + 1 < argc)
{
outputFile = argv[++i];
}
else if (arg == "-c" && i + 1 < argc)
{
count = std::atoi(argv[++i]);
}
else if (arg == "--endless")
{
count = -1;
}
else if (arg == "--help")
{
std::cerr << "Usage: " << argv[0] << " -d <number> -p <path> -o <output file> -c <count of loops>" << std::endl;
return 1;
}
}
if (depth == 0 || path.empty() || outputFile.empty())
{
std::cerr << "Usage: " << argv[0] << " -d <number> -p <path> -o <output file> [-c <count of loops> | --endless]" << std::endl;
return 1;
}
std::ifstream f(path);
json content = json::parse(f);
Grammar gram = Grammar(content, depth);
gram.JIT(outputFile, count);
return 0;
}