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/] [Compiler.h] - Blame information for rev 227

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 216 diegovalve
/* This program is free software. It comes without any warranty, to
2
 * the extent permitted by applicable law. You can redistribute it
3
 * and/or modify it under the terms of the Do What The Fuck You Want
4
 * To Public License, Version 2, as published by Sam Hocevar. See
5
 * http://sam.zoy.org/wtfpl/COPYING for more details. */
6
 
7
#pragma once
8
 
9
#include <fstream>
10
#include "Scanner.h"
11
#include <vector>
12
#include "Instruction.h"
13
#include <iostream>
14
#include <string>
15
#include <iterator>
16
 
17
 
18
        class TheiaCompiler
19
        {
20
                public:
21
                        std::vector<std::string> mPreprocessedFile;
22
                        // can instantiate with either a file name or an already open stream
23
                        inline explicit TheiaCompiler(std::string fileName) throw(std::string);
24
                        inline explicit TheiaCompiler(std::string fileName, bool aMode32) throw(std::string);
25
                        inline explicit TheiaCompiler(std::istream &iniStream) throw(std::string);
26
 
27
                        // Get a value from section and key
28
                        const char * getValue(const char * const section, const char * const key) const;
29
                        //-------------------------------------------------------------------------------------
30
                        void PrependCodeInitialization()
31
                        {
32
 
33
                                Instruction I;
34
                                I.mComment ="Make sure R0 has the appropiate values (0,1,2)";
35
                                I.SetCode( EOPERATION_ADD );
36
                                I.SetDestinationAddress( 0 );
37
                                I.SetImm( 0 );
38
                                I.SetDestZero( true );
39
                                I.SetWriteChannel(ECHANNEL_X);
40
                                mInstructions.push_back( I );
41
                                I.Clear();
42
 
43
 
44
                                I.SetCode( EOPERATION_ADD );
45
                                I.SetDestinationAddress( 0 );
46
                                I.SetImm( 1 );
47
                                I.SetDestZero( true );
48
                                I.SetWriteChannel(ECHANNEL_Y);
49
                                mInstructions.push_back( I );
50
                                I.Clear();
51
 
52
 
53
                                I.SetCode( EOPERATION_ADD );
54
                                I.SetDestinationAddress( 0 );
55
                                I.SetImm( 2 );
56
                                I.SetDestZero( true );
57
                                I.SetWriteChannel(ECHANNEL_Z);
58
                                mInstructions.push_back( I );
59
                                I.Clear();
60
 
61
                                I.mComment = "Disable multi-threading by default";
62
                                I.SetCode( EOPERATION_ADD );
63
                                I.SetDestinationAddress( SPR_CONTROL_REGISTER0 );
64
                                I.SetImm( 0 );
65
                                I.SetDestZero( true );
66
                                I.SetWriteChannel(ECHANNEL_Z);
67
                                mInstructions.push_back( I );
68
                                I.Clear();
69
 
70
                                I.mComment = "Make sure R3 (Frame Offset) is initialized to zero";
71
                                I.SetCode( EOPERATION_ADD );
72
                                I.SetDestinationAddress( SPR_CONTROL_REGISTER );
73
                                I.SetImm( 0 );
74
                                I.SetDestZero( true );
75
                                I.SetWriteChannel(ECHANNEL_XYZ);
76
                                mInstructions.push_back( I );
77
                                I.Clear();
78
                        }
79
                        //-------------------------------------------------------------------------------------
80
                        std::string GetHexCodeDump(void)
81
                        {
82
                                std::ostringstream oss;
83
                                //Add the header
84
                                oss << "//List file created by theia_compile\n";
85
 
86
 
87
                                for ( int i = 0; i < mInstructions.size(); i++)
88
                                {
89
 
90
                                        oss  << GetLineSymbolDefintion( i );
91
 
92
                                        if (mInstructions[i].mSourceLine > 0)
93
                                                        oss << "//" << mPreprocessedFile[ mInstructions[i].mSourceLine -1 ] << "\n";
94
 
95
                                        if (mInstructions[i].mComment.size())
96
                                                        oss << "//" << mInstructions[i].mComment<< "\n";
97
 
98
                                                oss << std::dec << i ;
99
                                                //oss << std::hex << " (0x"  <<  i << ") " ;
100
                                                oss << ":\t" << mInstructions[i].PrintAssembly() << "\n";
101
                                }
102
 
103
                                return oss.str();
104
                        }
105
                        //-------------------------------------------------------------------------------------
106
                        std::string GetLineSymbolDefintion( unsigned int aLine )
107
                        {
108
 
109
                                std::map<std::string, unsigned int>::const_iterator it;
110
                                for (it = mSymbolMap.begin(); it != mSymbolMap.end(); ++it)
111
                                {
112
                                        if (it->second == aLine)
113
                                                return "\n//" + it->first + "\n";
114
 
115
                                }
116
                                return std::string("");
117
                        }
118
                        //-------------------------------------------------------------------------------------
119
                        void Print(void)
120
                        {
121
                                for (int i = 0; i < mInstructions.size(); i++)
122
                                {
123
                                        //std::cout << "EA      : " << i << "\n";
124
                                        //std::cout << mInstructions[i].PrintHex() << "\n";
125
                                        //mInstructions[i].PrintFields();
126
                                        //std::cout << "\n";
127
                                }
128
                        }
129
                        //--------------------------------------------------------------------------------------------------
130
                        std::string PostProcess( std::string fileName )
131
                        {
132
                                std::string Result;
133
                                std::ifstream ifs(fileName.c_str());
134
                                if (!ifs.good())
135
                                        throw std::string("Unable to open file "+ fileName);
136
 
137
                                unsigned int CodeSize = 0;
138
                                while (!ifs.eof())
139
                                {
140
                                        char Buffer[1024];
141
                                        ifs.getline( Buffer,1024 );
142
                                        std::string Line = Buffer;
143
 
144
                                        //Ignore comments
145
                                        if (Line.find("//") != std::string::npos)
146
                                                Line.erase(Line.find("//"),Line.length());
147
 
148
                                        while (Line.find("\t") != std::string::npos)
149
                                                Line.replace(Line.find("\t"),1," ");
150
                                        //Ignore blank lines
151
                                        if (Line.length() == 0)
152
                                                continue;
153
 
154
                                        std::stringstream ss;
155
                                        ss << Line;
156
                                        std::string LineNumber,DestinationSymbol;
157
                                        unsigned int Operation,Destination,Src1,Src0;
158
                                        if (Line.find("@") != std::string::npos)
159
                                        {
160
                                                ss >> std::hex>> LineNumber >> Operation >> DestinationSymbol >>  Src1 >> Src0;
161
                                                DestinationSymbol.erase(0,1);
162 227 diegovalve
                                                DCOUT << "XXXX DestinationSymbol " << DestinationSymbol << "\n";
163
 
164
                                                if (mSymbolMap.find( DestinationSymbol ) == mSymbolMap.end())
165
                                                {
166
                                                        std::cout << "ERROR: Function definition for " << DestinationSymbol << " not found!\n";
167
                                                        exit(1);
168
                                                }
169
 
170 216 diegovalve
                                                Destination = mSymbolMap[ DestinationSymbol ];
171 227 diegovalve
                                                DCOUT << "XXXX Destination " << Destination << "\n";
172 216 diegovalve
                                        }
173
                                        else
174
                                                ss >> std::hex>> LineNumber >> Operation >> Destination >> Src1 >> Src0;
175
 
176
                                                //std::cout << "'" << Line << "'" << std::hex << LineNumber << "," << Operation << "," << Destination << "," << Src1 << "," << Src0 << "\n";
177
                                        Instruction I;
178
                                        I.SetFields(Operation,Destination,Src1,Src0);
179
                                        CodeSize++;
180
                                        if (m32BitMode)
181
                                                Result += I.PrintHex32() + "\n";
182
                                        else
183
                                                Result += I.PrintHex() + "\n";
184
                                }
185
                                std::cout << "CodeBlockSize: " << CodeSize << "\n";
186
                                return Result;
187
                        }
188
 
189
                        //--------------------------------------------------------------------------------------------------
190
 
191
                private:
192
                        // supress default constructor
193
                        TheiaCompiler();
194
                        // supress default copy constructor
195
                        TheiaCompiler(TheiaCompiler const &rhs);
196
                        // supress default assignment operator
197
                        TheiaCompiler &operator=(TheiaCompiler const &rhs);
198
 
199
                        std::vector< Instruction >          mInstructions;
200
                        std::map<std::string,unsigned int>  mSymbolMap;
201
                        bool                                                            mGenerateFixedPointArithmetic;
202
                        bool                                m32BitMode;
203
        };
204
 
205
//-------------------------------------------------------------------------------------------------------
206
        TheiaCompiler::TheiaCompiler(std::string fileName, bool aMode32 = false) throw(std::string)
207
        {
208
                m32BitMode = aMode32;
209
                mGenerateFixedPointArithmetic = false;
210
 
211
                std::ifstream inFile(fileName.c_str());
212
                if (!inFile.good())
213
                        throw std::string("Unable to open file "+ fileName);
214
 
215
                //First get me a local copy of the file into a vector
216
                std::string Line; //int  i = 1;
217
                while( std::getline(inFile, Line) )
218
                {
219
                                mPreprocessedFile.push_back( Line );
220
                                ////std::cout << i++ << " " << Line << "\n";
221
                }
222
 
223
                inFile.close();
224
                inFile.open(fileName.c_str());
225
                if (!inFile.good())
226
                        throw std::string("Unable to open file "+ fileName);
227
 
228
                PrependCodeInitialization();
229
 
230
                Theia::Scanner scanner(&inFile);
231
                Theia::Parser parser(scanner, mSymbolMap , mInstructions, mGenerateFixedPointArithmetic);
232
                std::cout << "parsing file\n";
233
                parser.parse();
234
        }
235
//-------------------------------------------------------------------------------------------------------
236
 
237
        TheiaCompiler::TheiaCompiler(std::istream &iniStream) throw(std::string)
238
        {
239
                mGenerateFixedPointArithmetic = false;
240
                Theia::Scanner scanner(&iniStream);
241
                Theia::Parser parser(scanner, mSymbolMap, mInstructions, mGenerateFixedPointArithmetic);
242
                parser.parse();
243
        }
244
 
245
//-------------------------------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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