Repository navigation
Expand file tree
/
Copy pathrmlmain.cpp
More file actions
163 lines (144 loc) · 5.03 KB
/
Copy pathrmlmain.cpp
File metadata and controls
163 lines (144 loc) · 5.03 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
#include <cstdio>
using namespace std;
#include <iostream>
#include <fstream>
#include <vector>
#include "rmlsup.h"
#include "rmlsyn.tab.hh"
#include "MyParser.h"
#include "FlexLexer.h"
#include "MyFlexLexer.h"
#include "csvlib.h"
int main(int argc, char **argv)
{
if (argc==2 || argc==3)
{
int optimization=OPTIMIZE_ALL,
fnIndex=1;
bool paraOK=true;
if (argc==3)
{
string flag=string(argv[1]);
paraOK=flag.length()>2 && flag.substr(0,2).compare("-p")==0;
if (paraOK)
{
size_t s;
string flagPara=flag.substr(2, flag.length()-2);
optimization=stoi(flagPara, &s);
paraOK=s==flagPara.length();
if (paraOK)
{
paraOK=optimization>=0 && optimization<=OPTIMIZE_ALL;
if (paraOK)
fnIndex++;
else
cout << "Invalid optimization value after -p. It must be between 0 and 15." << endl;
}
else
cout << "-p flag must be followed by a valid integer." << endl;
}
else
cout << "Invalid optimization flag." << endl;
}
if (paraOK)
{
string arg=string(argv[fnIndex]),
inputFile=arg+".txt",
outputFile=arg+".json",
outputICFile=arg+"-IC.txt";
bool processed=false;
ifstream is(inputFile);
if (is.is_open())
{
MyParser *driver=new MyParser(optimization);
yy::MyParserBase *base=new yy::MyParserBase(driver);
RMLEval *rmlEval=new RMLEval(driver);
ofstream os(outputFile);
if (os.is_open())
{
#if defined(PROJECT04)
ofstream icos(outputICFile);
if (icos.is_open())
{
#endif
driver->compile(base, &is, rmlEval);
#if defined(PROJECT02)
ofstream project2OutputFile=ofstream("project02.txt");
#endif
int pel=driver->getParseErrorLine();
if (pel>=0)
{
cout << "Not recognized!" << endl;
#if defined(PROJECT02)
project2OutputFile << "Line " << pel << ": Syntax Error" << endl;
#endif
}
else
{
cout << "Recognized!" << endl;
#if defined(PROJECT02)
project2OutputFile << driver->stats.size() << endl;
project2OutputFile << driver->selectOpCount << endl;
project2OutputFile << driver->spaceCount << endl;
project2OutputFile << driver->aliasCount << endl;
project2OutputFile << driver->numericConstantCount << endl;
project2OutputFile << driver->stringConstantCount << endl;
project2OutputFile << driver->variableReferenceCount << endl;
project2OutputFile << driver->fqVariableReferenceCount << endl;
project2OutputFile << driver->funcCallCount << endl;
#endif
}
#if defined(PROJECT02)
project2OutputFile.close();
#endif
#if defined(PROJECT04)
if (rmlEval->messageSet.messageCount(RMLEvalMsgSeverity::Error)==0)
rmlEval->ic->writeReadable(&icos);
#endif
processed=true;
#if defined(PROJECT04)
icos.close();
}
else
cout << "Unable to create the IC output file "<<outputICFile<<"."<<endl;
#endif
rmlEval->writeAsJSON(&os);
rmlEval->messageSet.writeToConsole();
os.close();
}
else
cout << "Unable to create the output file "<<outputFile<<"."<<endl;
is.close();
#ifdef DO_X64
if (processed && rmlEval->messageSet.messageCount(RMLEvalMsgSeverity::Error)==0)
{
RMLDynamicFuncDesc *fd=rmlEval->generateCode();
if (fd!=nullptr)
{
if (fd->f!=nullptr){
// DEBUG
// dumps dynamic function contents to file
// objdump -D -b binary -mi386:x86-64 fdump.bin > fdump.txt
// FILE* dump = fopen("fdump.bin", "wb");
// fwrite((uint8_t*)fd->f, 1, fd->codeAllocationSize, dump);
// fclose(dump);
fd->f();
}
cout << "Function: " << (int *)fd->f << endl;
delete fd;
}
}
if (rmlEval->exceptionWord!=nullptr)
cout << "RML exception " << *rmlEval->exceptionWord << endl;
#endif
delete rmlEval;
delete driver;
}
else
cout << "Unable to open the input file "<<inputFile<<"."<<endl;
}
}
else
cout << "Usage is "<<argv[0]<<" <optional optimization parameter> <rml module file name>";
return 0;
}