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 |
|
|
//std::cout << "XXXX DestinationSymbol " << DestinationSymbol << "\n";
|
163 |
|
|
std::ostringstream oss;
|
164 |
|
|
Destination = mSymbolMap[ DestinationSymbol ];
|
165 |
|
|
//std::cout << "XXXX Destination " << Destination << "\n";
|
166 |
|
|
}
|
167 |
|
|
else
|
168 |
|
|
ss >> std::hex>> LineNumber >> Operation >> Destination >> Src1 >> Src0;
|
169 |
|
|
|
170 |
|
|
//std::cout << "'" << Line << "'" << std::hex << LineNumber << "," << Operation << "," << Destination << "," << Src1 << "," << Src0 << "\n";
|
171 |
|
|
Instruction I;
|
172 |
|
|
I.SetFields(Operation,Destination,Src1,Src0);
|
173 |
|
|
CodeSize++;
|
174 |
|
|
if (m32BitMode)
|
175 |
|
|
Result += I.PrintHex32() + "\n";
|
176 |
|
|
else
|
177 |
|
|
Result += I.PrintHex() + "\n";
|
178 |
|
|
}
|
179 |
|
|
std::cout << "CodeBlockSize: " << CodeSize << "\n";
|
180 |
|
|
return Result;
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
//--------------------------------------------------------------------------------------------------
|
184 |
|
|
|
185 |
|
|
private:
|
186 |
|
|
// supress default constructor
|
187 |
|
|
TheiaCompiler();
|
188 |
|
|
// supress default copy constructor
|
189 |
|
|
TheiaCompiler(TheiaCompiler const &rhs);
|
190 |
|
|
// supress default assignment operator
|
191 |
|
|
TheiaCompiler &operator=(TheiaCompiler const &rhs);
|
192 |
|
|
|
193 |
|
|
std::vector< Instruction > mInstructions;
|
194 |
|
|
std::map<std::string,unsigned int> mSymbolMap;
|
195 |
|
|
bool mGenerateFixedPointArithmetic;
|
196 |
|
|
bool m32BitMode;
|
197 |
|
|
};
|
198 |
|
|
|
199 |
|
|
//-------------------------------------------------------------------------------------------------------
|
200 |
|
|
TheiaCompiler::TheiaCompiler(std::string fileName, bool aMode32 = false) throw(std::string)
|
201 |
|
|
{
|
202 |
|
|
m32BitMode = aMode32;
|
203 |
|
|
mGenerateFixedPointArithmetic = false;
|
204 |
|
|
|
205 |
|
|
std::ifstream inFile(fileName.c_str());
|
206 |
|
|
if (!inFile.good())
|
207 |
|
|
throw std::string("Unable to open file "+ fileName);
|
208 |
|
|
|
209 |
|
|
//First get me a local copy of the file into a vector
|
210 |
|
|
std::string Line; //int i = 1;
|
211 |
|
|
while( std::getline(inFile, Line) )
|
212 |
|
|
{
|
213 |
|
|
mPreprocessedFile.push_back( Line );
|
214 |
|
|
////std::cout << i++ << " " << Line << "\n";
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
inFile.close();
|
218 |
|
|
inFile.open(fileName.c_str());
|
219 |
|
|
if (!inFile.good())
|
220 |
|
|
throw std::string("Unable to open file "+ fileName);
|
221 |
|
|
|
222 |
|
|
PrependCodeInitialization();
|
223 |
|
|
|
224 |
|
|
Theia::Scanner scanner(&inFile);
|
225 |
|
|
Theia::Parser parser(scanner, mSymbolMap , mInstructions, mGenerateFixedPointArithmetic);
|
226 |
|
|
std::cout << "parsing file\n";
|
227 |
|
|
parser.parse();
|
228 |
|
|
}
|
229 |
|
|
//-------------------------------------------------------------------------------------------------------
|
230 |
|
|
|
231 |
|
|
TheiaCompiler::TheiaCompiler(std::istream &iniStream) throw(std::string)
|
232 |
|
|
{
|
233 |
|
|
mGenerateFixedPointArithmetic = false;
|
234 |
|
|
Theia::Scanner scanner(&iniStream);
|
235 |
|
|
Theia::Parser parser(scanner, mSymbolMap, mInstructions, mGenerateFixedPointArithmetic);
|
236 |
|
|
parser.parse();
|
237 |
|
|
}
|
238 |
|
|
|
239 |
|
|
//-------------------------------------------------------------------------------------------------------
|