OpenCores
URL https://opencores.org/ocsvn/theia_gpu/theia_gpu/trunk

Subversion Repositories theia_gpu

[/] [theia_gpu/] [branches/] [beta_2.0/] [compiler/] [src/] [vp_compiler/] [Preprocessor.cpp] - Blame information for rev 224

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 216 diegovalve
#include "Preprocessor.h"
2
#include <iostream>
3
#include <fstream>
4
#include <map>
5
#include <vector>
6
using namespace std;
7
 
8
//----------------------------------------------------------------
9
Preprocessor::Preprocessor()
10
{
11
}
12
//----------------------------------------------------------------
13
Preprocessor::~Preprocessor()
14
{
15
}
16
//-----------------------------------------------------------------------
17
void Tokenize(const string& str,
18
                      vector<string>& tokens,
19
                      const string& delimiters = " ")
20
{
21
 
22
    string::size_type lastPos = str.find_first_not_of(delimiters, 0);
23
      string::size_type pos     = str.find_first_of(delimiters, lastPos);
24
 
25
    while (string::npos != pos || string::npos != lastPos)
26
    {
27
 
28
        tokens.push_back(str.substr(lastPos, pos - lastPos));
29
        lastPos = str.find_first_not_of(delimiters, pos);
30
        pos = str.find_first_of(delimiters, lastPos);
31
    }
32
}
33
//----------------------------------------------------------------
34
vector<string> Preprocessor::ScanFile(  std::ifstream & ifs )
35
{
36
        vector<string> NewFile;
37
        while (!ifs.eof())
38
        {
39
                 char Buffer[1024];
40
                 ifs.getline( Buffer,1024 );
41
                 string Line = Buffer;
42
 
43
                 //replace tabs with spaces
44
                 int pos = 0;
45
                 while ((pos = Line.find("\t")) != string::npos)
46
                                        Line.replace(pos,1," ");
47
 
48
                 if (Line.find("#") == string::npos)
49
                 {
50
                    NewFile.push_back(Line);
51
                        continue;
52
                 }
53
 
54
                 if (Line.find("#macro") != string::npos)
55
                 {
56
 
57
                          NewFile.push_back("//" + Line);
58
                          PopulateMacroFunction(Line,ifs,NewFile);
59
                          continue;
60
             }
61
 
62
                vector<string> Tokens;
63
                Tokenize(Line,Tokens," ");
64
                if (Tokens.size() != 0)
65
                {
66
 
67
                if (Tokens[0] == "#define")
68
                        mMacros[ Tokens[1] ] = Tokens[2];
69
                }
70
 
71
                NewFile.push_back("//" + Line);
72
 
73
        }
74
 
75
        //for( map<string,string>::const_iterator it = mMacros.begin(); it != mMacros.end(); ++it )
76
                //cout << "macro " << it->first << " = " << it->second << "\n";
77
        //cout << "\n";
78
        //for( map<string,TMacroFunction>::iterator it = mMacroFunctions.begin(); it != mMacroFunctions.end(); ++it )
79
                //cout << "macro function " << it->first << "\n";
80
 
81
        return NewFile;
82
}
83
//----------------------------------------------------------------
84
void Preprocessor::SubstitudeMacros( vector<string> & aFile, ofstream & ofs )
85
{
86
        //for (int i = 0; i < NewFile.size(); i++)
87
        int i = 0;
88
        for ( vector<string>::iterator I = aFile.begin(); (I != aFile.end() && i < aFile.size()); I++, i++)
89
        {
90
                //cout << "Line " << *I << "\n";
91
        //Now the macro literals
92
                for( map<string,string>::const_iterator it = mMacros.begin(); it != mMacros.end(); ++it )
93
                {
94
                        string Key = it->first;
95
                        string Value = it->second;
96
                        string Line = aFile[i];
97
                        while (Line.find(Key) != string::npos)
98
                                Line.replace(Line.find(Key),Key.length(),Value);
99
                        aFile[i] = Line;
100
                }
101
 
102
 
103
                //First the macro functions
104
                for( map<string,TMacroFunction>::iterator it = mMacroFunctions.begin(); it != mMacroFunctions.end(); ++it )
105
                {
106
                        string Key = it->first;
107
                        TMacroFunction MacroFunction = it->second;
108
                        string Line =*I;
109
                        if (Line.find("//") != string::npos)
110
                                Line.erase(Line.find("//"),Line.length()-Line.find("//"));
111
                                //continue;
112
 
113
                        if (Line.find(Key) != string::npos)
114
                        {
115
                                int pos = 0;
116
                                 while ((pos = Line.find(" ")) != string::npos)
117
                                        Line.erase(pos,1);
118
                                  vector<string> Tokens;
119
                                Tokenize(Line,Tokens,"=");
120
 
121
                                MacroFunction.mReturnValue = Tokens[0];
122
                                vector<string> Tokens2;
123
                                string Tmp = Tokens[1];
124
                                Tokens.clear();
125
                                Tokenize(Tmp,Tokens,"(");
126
                                Tmp = Tokens[1];
127
                                 while ((pos = Tmp.find(")")) != string::npos)
128
                                         Tmp.erase(pos,1);
129
                                 while ((pos = Tmp.find(";")) != string::npos)
130
                                         Tmp.erase(pos,1);
131
                                 Tokens.clear();
132
                                 Tokenize(Tmp,Tokens,",");
133
                                 MacroFunction.mParamterValue = Tokens;
134
 
135
                                 vector<string> MacroString = MacroFunction.GetSubstitudedMacro();
136
                                 aFile[i] = "//" + aFile[i];
137
                                 I = aFile.begin() + i+1;
138
                                 aFile.insert(I,MacroString.begin(),MacroString.end());
139
 
140
                                 I = aFile.begin() + i;
141
                        }
142
                }
143
 
144
        //      cout <<  aFile[i].c_str() << std::endl;
145
                ofs << aFile[i].c_str() << std::endl;
146
        }
147
}
148
//----------------------------------------------------------------
149
void Preprocessor::Execute( string aFile )
150
{
151
        ifstream ifs;
152
        ifs.open(aFile.c_str());
153
        if (!ifs.good())
154 224 diegovalve
                throw string("Cannot open file" + aFile + "\n");
155
 
156 216 diegovalve
        cout << "Scanning file " << aFile << "\n";
157
        vector<string> NewFile = ScanFile( ifs );
158
        ifs.close();
159
        cout << "Preprocessing file " << aFile << "\n";
160
        ofstream ofs(string(aFile + ".preprocessed").c_str());
161
        SubstitudeMacros( NewFile, ofs );
162
        ofs.close();
163
}
164
//----------------------------------------------------------------
165
vector<string> TMacroFunction::GetSubstitudedMacro()
166
        {
167
                vector<string> ReturnString;
168
                for (int i = 0; i < mLines.size(); i++)
169
                {
170
 
171
                        string Line = mLines[i];
172
                        for (int j = 0; j < mParamterSymbol.size(); j++)
173
                        {
174
                                while(  Line.find(mParamterSymbol[j]) != string::npos )
175
                                        Line.replace(Line.find(mParamterSymbol[j]),mParamterSymbol[j].length(),mParamterValue[j]);
176
 
177
                                while(  Line.find(mReturnSymbol) != string::npos )
178
                                        Line.replace(Line.find(mReturnSymbol),mReturnSymbol.length(),mReturnValue);
179
 
180
                        }
181
 
182
                        ReturnString.push_back( Line );
183
                }
184
                return ReturnString;
185
        }
186
//----------------------------------------------------------------
187
void Preprocessor::PopulateMacroFunction(string aSignature, std::ifstream & ifs, vector<string> & aNewFile)
188
{
189
 
190
         //Get rid of spaces
191
         int pos = 0;
192
         while ((pos = aSignature.find(" ")) != string::npos)
193
                                 aSignature.erase(pos,1);
194
        //Delete the "#macro" string
195
        aSignature.erase(0,6);
196
        vector<string> Tokens;
197
        Tokenize(aSignature,Tokens,"=");
198
        TMacroFunction M;
199
        M.mReturnSymbol = Tokens[0];
200
 
201
        string Tmp = Tokens[1];
202
        Tokens.clear();
203
        Tokenize(Tmp,Tokens,"(");
204
        M.mName = Tokens[0];
205
        Tmp = Tokens[1];
206
        while ((pos = Tmp.find(")")) != string::npos)
207
                 Tmp.erase(pos,1);
208
         Tokens.clear();
209
         Tokenize(Tmp,Tokens,",");
210
         M.mParamterSymbol = Tokens;
211
 
212
         int MacroLineCount = 0;
213
         while (MacroLineCount < MAX_MACRO_FUNCTION_LINE_COUNT)
214
         {
215
                char Buffer[1024];
216
                 ifs.getline( Buffer,1024 );
217
                 string Line = Buffer;
218
             aNewFile.push_back("//" + Line);
219
                 if (Line.find("#endmacro") != string::npos)
220
                                 break;
221
                 M.mLines.push_back( Line );
222
                 MacroLineCount++;
223
         }
224
 
225
         mMacroFunctions[M.mName] = M;
226
         M.mLines.clear();
227
}
228
//----------------------------------------------------------------

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.