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

Subversion Repositories theia_gpu

[/] [theia_gpu/] [branches/] [new_compiler/] [Compiler.h] - Blame information for rev 199

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 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::istream &iniStream) throw(std::string);
25
 
26
                        // Get a value from section and key
27
                        const char * getValue(const char * const section, const char * const key) const;
28
                        //-------------------------------------------------------------------------------------
29
                        void PrependCodeInitialization()
30
                        {
31
 
32
                                Instruction I;
33
                                I.mComment ="Make sure R0 has the appropiate values";
34
                                I.SetCode( EOPERATION_ADD );
35
                                I.SetDestinationAddress( 0 );
36
                                I.SetImm( 0 );
37
                                I.SetDestZero( true );
38
                                I.SetWriteChannel(ECHANNEL_X);
39
                                mInstructions.push_back( I );
40
                                I.Clear();
41
 
42
 
43
                                I.SetCode( EOPERATION_ADD );
44
                                I.SetDestinationAddress( 0 );
45
                                I.SetImm( 1 );
46
                                I.SetDestZero( true );
47
                                I.SetWriteChannel(ECHANNEL_Y);
48
                                mInstructions.push_back( I );
49
                                I.Clear();
50
 
51
 
52
                                I.SetCode( EOPERATION_ADD );
53
                                I.SetDestinationAddress( 0 );
54
                                I.SetImm( 2 );
55
                                I.SetDestZero( true );
56
                                I.SetWriteChannel(ECHANNEL_Z);
57
                                mInstructions.push_back( I );
58
                                I.Clear();
59
 
60
 
61
                                I.mComment = "Make sure R30 (Frame Offset) is initialized to zero";
62
                                I.SetCode( EOPERATION_ADD );
63
                                I.SetDestinationAddress( SPR_CONTROL_REGISTER );
64
                                I.SetImm( 0 );
65
                                I.SetDestZero( true );
66
                                I.SetWriteChannel(ECHANNEL_XYZ);
67
                                mInstructions.push_back( I );
68
                                I.Clear();
69
                        }
70
                        //-------------------------------------------------------------------------------------
71
                        std::string GetHexCodeDump(void)
72
                        {
73
                                std::ostringstream oss;
74
                                //Add the header
75
                                oss << "//List file created by theia_compile\n";
76
 
77
 
78
                                for ( int i = 0; i < mInstructions.size(); i++)
79
                                {
80
 
81
                                        oss  << GetLineSymbolDefintion( i );
82
 
83
                                        if (mInstructions[i].mSourceLine > 0)
84
                                                        oss << "//" << mPreprocessedFile[ mInstructions[i].mSourceLine -1 ] << "\n";
85
 
86
                                        if (mInstructions[i].mComment.size())
87
                                                        oss << "//" << mInstructions[i].mComment<< "\n";
88
 
89
                                                oss << std::dec << i ;
90
                                                //oss << std::hex << " (0x"  <<  i << ") " ;
91
                                                oss << ":\t" << mInstructions[i].PrintAssembly() << "\n";
92
                                }
93
 
94
                                return oss.str();
95
                        }
96
                        //-------------------------------------------------------------------------------------
97
                        std::string GetLineSymbolDefintion( unsigned int aLine )
98
                        {
99
 
100
                                std::map<std::string, unsigned int>::const_iterator it;
101
                                for (it = mSymbolMap.begin(); it != mSymbolMap.end(); ++it)
102
                                {
103
                                        if (it->second == aLine)
104
                                                return "\n//" + it->first + "\n";
105
 
106
                                }
107
                                return std::string("");
108
                        }
109
                        //-------------------------------------------------------------------------------------
110
                        void Print(void)
111
                        {
112
                                for (int i = 0; i < mInstructions.size(); i++)
113
                                {
114
                                        //std::cout << "EA      : " << i << "\n";
115
                                        //std::cout << mInstructions[i].PrintHex() << "\n";
116
                                        mInstructions[i].PrintFields();
117
                                        //std::cout << "\n";
118
                                }
119
                        }
120
                        //--------------------------------------------------------------------------------------------------
121
                        std::string PostProcess( std::string fileName )
122
                        {
123
                                std::string Result;
124
                                std::ifstream ifs(fileName.c_str());
125
                                if (!ifs.good())
126
                                        throw std::string("Unable to open file "+ fileName);
127
 
128
                                while (!ifs.eof())
129
                                {
130
                                        char Buffer[1024];
131
                                        ifs.getline( Buffer,1024 );
132
                                        std::string Line = Buffer;
133
 
134
                                        //Ignore comments
135
                                        if (Line.find("//") != std::string::npos)
136
                                                Line.erase(Line.find("//"),Line.length());
137
 
138
                                        while (Line.find("\t") != std::string::npos)
139
                                                Line.replace(Line.find("\t"),1," ");
140
                                        //Ignore blank lines
141
                                        if (Line.length() == 0)
142
                                                continue;
143
 
144
                                        std::stringstream ss;
145
                                        ss << Line;
146
                                        std::string LineNumber,DestinationSymbol;
147
                                        unsigned int Operation,Destination,Src1,Src0;
148
                                        if (Line.find("@") != std::string::npos)
149
                                        {
150
                                                ss >> std::hex>> LineNumber >> Operation >> DestinationSymbol >>  Src1 >> Src0;
151
                                                DestinationSymbol.erase(0,1);
152
                                                //std::cout << "XXXX DestinationSymbol " << DestinationSymbol << "\n";
153
                                                std::ostringstream oss;
154
                                                Destination = mSymbolMap[ DestinationSymbol ];
155
                                                //std::cout << "XXXX Destination " << Destination << "\n";
156
                                        }
157
                                        else
158
                                                ss >> std::hex>> LineNumber >> Operation >> Destination >> Src1 >> Src0;
159
 
160
                                                //std::cout << "'" << Line << "'" << std::hex << LineNumber << "," << Operation << "," << Destination << "," << Src1 << "," << Src0 << "\n";
161
                                        Instruction I;
162
                                        I.SetFields(Operation,Destination,Src1,Src0);
163
                                        Result += I.PrintHex() + "\n";
164
                                }
165
                                return Result;
166
                        }
167
                        //--------------------------------------------------------------------------------------------------
168
 
169
                private:
170
                        // supress default constructor
171
                        TheiaCompiler();
172
                        // supress default copy constructor
173
                        TheiaCompiler(TheiaCompiler const &rhs);
174
                        // supress default assignment operator
175
                        TheiaCompiler &operator=(TheiaCompiler const &rhs);
176
 
177
                        std::vector< Instruction >          mInstructions;
178
                        std::map<std::string,unsigned int>  mSymbolMap;
179
                        bool                                                            mGenerateFixedPointArithmetic;
180
        };
181
 
182
//-------------------------------------------------------------------------------------------------------
183
        TheiaCompiler::TheiaCompiler(std::string fileName) throw(std::string)
184
        {
185
                mGenerateFixedPointArithmetic = false;
186
                std::ifstream inFile(fileName.c_str());
187
                if (!inFile.good())
188
                        throw std::string("Unable to open file "+ fileName);
189
 
190
                //First get me a local copy of the file into a vector
191
                std::string Line; //int  i = 1;
192
                while( std::getline(inFile, Line) )
193
                {
194
                                mPreprocessedFile.push_back( Line );
195
                                ////std::cout << i++ << " " << Line << "\n";
196
                }
197
 
198
                inFile.close();
199
                inFile.open(fileName.c_str());
200
                if (!inFile.good())
201
                        throw std::string("Unable to open file "+ fileName);
202
 
203
                PrependCodeInitialization();
204
 
205
                Theia::Scanner scanner(&inFile);
206
                Theia::Parser parser(scanner, mSymbolMap , mInstructions, mGenerateFixedPointArithmetic);
207
                std::cout << "parsing file\n";
208
                parser.parse();
209
        }
210
//-------------------------------------------------------------------------------------------------------
211
 
212
        TheiaCompiler::TheiaCompiler(std::istream &iniStream) throw(std::string)
213
        {
214
                mGenerateFixedPointArithmetic = false;
215
                Theia::Scanner scanner(&iniStream);
216
                Theia::Parser parser(scanner, mSymbolMap, mInstructions, mGenerateFixedPointArithmetic);
217
                parser.parse();
218
        }
219
 
220
//-------------------------------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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