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

Subversion Repositories theia_gpu

[/] [theia_gpu/] [branches/] [beta_2.0/] [compiler/] [src/] [cp_compiler/] [Compiler.h] - Blame information for rev 216

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::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
                                CControlInstruction I;
32
                                I.Clear();
33
                                I.SetOperation(EOPERATION_NOP);
34
                                mInstructions.push_back(I);
35
                                I.Clear();
36
 
37
                                I.SetOperation(EOPERATION_NOP);
38
                                mInstructions.push_back(I);
39
                                I.Clear();
40
 
41
                                I.SetOperation(EOPERATION_NOP);
42
                                mInstructions.push_back(I);
43
                                I.Clear();
44
 
45
                                I.SetDestinationAddress(0);
46
                                I.SetOperation(EOPERATION_ASSIGN);
47
                                I.SetLiteral(0);
48
                                mInstructions.push_back(I);
49
 
50
                        }
51
                        //-------------------------------------------------------------------------------------
52
                        std::string GetHexCodeDump(void)
53
                        {
54
                                std::ostringstream oss;
55
                                //Add the header
56
                                oss << "//List file created by theia_compile\n";
57
 
58
 
59
                                for ( int i = 0; i < mInstructions.size(); i++)
60
                                {
61
 
62
                                        oss  << GetLineSymbolDefintion( i );
63
 
64
                                        if (mInstructions[i].mSourceLine > 0)
65
                                                        oss << "//" << mPreprocessedFile[ mInstructions[i].mSourceLine -1 ] << "\n";
66
 
67
                                        if (mInstructions[i].mComment.size())
68
                                                        oss << "//" << mInstructions[i].mComment<< "\n";
69
 
70
                                                oss << std::dec << i ;
71
                                                //oss << std::hex << " (0x"  <<  i << ") " ;
72
                                                oss << ":\t" << mInstructions[i].PrintAssembly() << "\n";
73
                                }
74
 
75
                                return oss.str();
76
                        }
77
                        //-------------------------------------------------------------------------------------
78
                        std::string GetLineSymbolDefintion( unsigned int aLine )
79
                        {
80
 
81
                                std::map<std::string, unsigned int>::const_iterator it;
82
                                for (it = mSymbolMap.begin(); it != mSymbolMap.end(); ++it)
83
                                {
84
                                        if (it->second == aLine)
85
                                                return "\n//" + it->first + "\n";
86
 
87
                                }
88
                                return std::string("");
89
                        }
90
 
91
                        std::string PostProcess(std::string aFilePath )
92
                        {
93
                                std::ostringstream oss;
94
                                oss << "//Code generated bt theia_compile\n";
95
                                for ( int i = 0; i < mInstructions.size(); i++)
96
                                {
97
                                        oss << mInstructions[i].PrintAssembly() << "\n";
98
                                }
99
                                return oss.str();
100
                        }
101
                        //--------------------------------------------------------------------------------------------------
102
 
103
                private:
104
                        // supress default constructor
105
                        TheiaCompiler();
106
                        // supress default copy constructor
107
                        TheiaCompiler(TheiaCompiler const &rhs);
108
                        // supress default assignment operator
109
                        TheiaCompiler &operator=(TheiaCompiler const &rhs);
110
 
111
                        std::vector< CControlInstruction >  mInstructions;
112
                        std::map<std::string,unsigned int>  mSymbolMap;
113
                        bool                                                            mGenerateFixedPointArithmetic;
114
        };
115
 
116
//-------------------------------------------------------------------------------------------------------
117
        TheiaCompiler::TheiaCompiler(std::string fileName) throw(std::string)
118
        {
119
                mGenerateFixedPointArithmetic = false;
120
                std::ifstream inFile(fileName.c_str());
121
                if (!inFile.good())
122
                        throw std::string("Unable to open file "+ fileName);
123
 
124
                //First get me a local copy of the file into a vector
125
                std::string Line; //int  i = 1;
126
                while( std::getline(inFile, Line) )
127
                {
128
                                mPreprocessedFile.push_back( Line );
129
                                ////std::cout << i++ << " " << Line << "\n";
130
                }
131
 
132
                inFile.close();
133
                inFile.open(fileName.c_str());
134
                if (!inFile.good())
135
                        throw std::string("Unable to open file "+ fileName);
136
 
137
                PrependCodeInitialization();
138
 
139
                Theia::Scanner scanner(&inFile);
140
                Theia::Parser parser(scanner, mSymbolMap , mInstructions, mGenerateFixedPointArithmetic);
141
                std::cout << "parsing file\n";
142
                parser.parse();
143
        }
144
//-------------------------------------------------------------------------------------------------------
145
 
146
        TheiaCompiler::TheiaCompiler(std::istream &iniStream) throw(std::string)
147
        {
148
                mGenerateFixedPointArithmetic = false;
149
                Theia::Scanner scanner(&iniStream);
150
                Theia::Parser parser(scanner, mSymbolMap, mInstructions, mGenerateFixedPointArithmetic);
151
                parser.parse();
152
        }
153
 
154
//-------------------------------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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