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 216

Go to most recent revision | 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
                std::cout << "Cannot open file" << aFile << "\n";
155
        cout << "Scanning file " << aFile << "\n";
156
        vector<string> NewFile = ScanFile( ifs );
157
        ifs.close();
158
        cout << "Preprocessing file " << aFile << "\n";
159
        ofstream ofs(string(aFile + ".preprocessed").c_str());
160
        SubstitudeMacros( NewFile, ofs );
161
        ofs.close();
162
}
163
//----------------------------------------------------------------
164
vector<string> TMacroFunction::GetSubstitudedMacro()
165
        {
166
                vector<string> ReturnString;
167
                for (int i = 0; i < mLines.size(); i++)
168
                {
169
 
170
                        string Line = mLines[i];
171
                        for (int j = 0; j < mParamterSymbol.size(); j++)
172
                        {
173
                                while(  Line.find(mParamterSymbol[j]) != string::npos )
174
                                        Line.replace(Line.find(mParamterSymbol[j]),mParamterSymbol[j].length(),mParamterValue[j]);
175
 
176
                                while(  Line.find(mReturnSymbol) != string::npos )
177
                                        Line.replace(Line.find(mReturnSymbol),mReturnSymbol.length(),mReturnValue);
178
 
179
                        }
180
 
181
                        ReturnString.push_back( Line );
182
                }
183
                return ReturnString;
184
        }
185
//----------------------------------------------------------------
186
void Preprocessor::PopulateMacroFunction(string aSignature, std::ifstream & ifs, vector<string> & aNewFile)
187
{
188
 
189
         //Get rid of spaces
190
         int pos = 0;
191
         while ((pos = aSignature.find(" ")) != string::npos)
192
                                 aSignature.erase(pos,1);
193
        //Delete the "#macro" string
194
        aSignature.erase(0,6);
195
        vector<string> Tokens;
196
        Tokenize(aSignature,Tokens,"=");
197
        TMacroFunction M;
198
        M.mReturnSymbol = Tokens[0];
199
 
200
        string Tmp = Tokens[1];
201
        Tokens.clear();
202
        Tokenize(Tmp,Tokens,"(");
203
        M.mName = Tokens[0];
204
        Tmp = Tokens[1];
205
        while ((pos = Tmp.find(")")) != string::npos)
206
                 Tmp.erase(pos,1);
207
         Tokens.clear();
208
         Tokenize(Tmp,Tokens,",");
209
         M.mParamterSymbol = Tokens;
210
 
211
         int MacroLineCount = 0;
212
         while (MacroLineCount < MAX_MACRO_FUNCTION_LINE_COUNT)
213
         {
214
                char Buffer[1024];
215
                 ifs.getline( Buffer,1024 );
216
                 string Line = Buffer;
217
             aNewFile.push_back("//" + Line);
218
                 if (Line.find("#endmacro") != string::npos)
219
                                 break;
220
                 M.mLines.push_back( Line );
221
                 MacroLineCount++;
222
         }
223
 
224
         mMacroFunctions[M.mName] = M;
225
         M.mLines.clear();
226
}
227
//----------------------------------------------------------------

powered by: WebSVN 2.1.0

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