OpenASIP  2.0
AssemblerParser.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file AssemblerParser.cc
26  *
27  * Syntax declarations and callbacks of assembler language.
28  *
29  * @author Mikael Lepistö 2005 (tmlepist-no.spam-cs.tut.fi)
30  * @author Pekka Jääskeläinen 2006,2009
31  *
32  * @note rating: yellow
33  */
34 
35 #include "AssemblerParser.hh"
36 
37 #include "Binary.hh"
38 #include "CodeSection.hh"
39 #include "ResourceSection.hh"
40 #include "RelocSection.hh"
41 #include "SymbolSection.hh"
42 
43 using namespace TPEF;
44 
45 /////////////////////////////////////////////////////////////////////////////
46 /// PrintString actor
47 /////////////////////////////////////////////////////////////////////////////
48 
49 /**
50  * Constructor of actor.
51  *
52  * @param aStr String to print.
53  */
54 PrintString::PrintString(const char* aStr) : str_(aStr) {
55 }
56 
57 
58 /**
59  * Constructor of actor.
60  *
61  * @param aStr String to print.
62  */
63 PrintString::PrintString(std::string& aStr) : str_(aStr.c_str()) {
64 }
65 
66 /**
67  * Prints out string of actor.
68  */
69 void
70 PrintString::operator() (const char*, const char*) const {
71  std::cerr << str_;
72 }
73 
74 /////////////////////////////////////////////////////////////////////////////
75 /// NewCodeSectionActor
76 /////////////////////////////////////////////////////////////////////////////
77 /**
78  * Starts new code section starting from defined address.
79  *
80  * @param creator Creator that is used for code section generating.
81  * @param startAddress Start address of code.
82  */
84  CodeSectionCreator& creator,
85  UValue& startAddress) :
86  creator_(creator), startAddress_(startAddress) {
87 }
88 
89 void
90 NewCodeSectionActor::operator()(const char*, const char*) const {
92 }
93 
94 /////////////////////////////////////////////////////////////////////////////
95 /// AddMoveActor
96 /////////////////////////////////////////////////////////////////////////////
97 
98 /**
99  * Adds parsed move to code section creator.
100  *
101  * @param creator Creator where to add move.
102  * @param move Move to add.
103  */
105  CodeSectionCreator& creator,
106  const ParserMove& move) :
107  creator_(creator), move_(move) {
108 }
109 
110 void
111 AddMoveActor::operator() (const char*, const char*) const {
113 }
114 
115 /////////////////////////////////////////////////////////////////////////////
116 /// SetStartAddressActor
117 /////////////////////////////////////////////////////////////////////////////
118 
119 /**
120  * Sets start address for next data area definition.
121  *
122  * @param creator Creator that is used for data section generation.
123  * @param startAddress Address for next data area definition.
124  */
126  UValue& startAddress) :
127  creator_(creator), startAddress_(startAddress) {
128 }
129 
130 void
131 SetStartAddressActor::operator() (const char*, const char*) const {
133 }
134 
135 /////////////////////////////////////////////////////////////////////////////
136 /// AddDataLineActor
137 /////////////////////////////////////////////////////////////////////////////
138 
139 /**
140  * Adds new data area definition to data section creator.
141  *
142  * @param creator Creator that is used for data section generation.
143  * @param dataLine Data area definition to add.
144  */
146  const DataLine& dataLine) :
147  creator_(creator), dataLine_(dataLine) {
148 }
149 
150 void
151 AddDataLineActor::operator() (const char*, const char*) const {
153 }
154 
155 /////////////////////////////////////////////////////////////////////////////
156 /// AddLabelActor
157 /////////////////////////////////////////////////////////////////////////////
158 
159 /**
160  * Adds new label to label manager.
161  *
162  * @param manager Label manager that is used to label bookkeeping.
163  * @param aSpace Address space for label.
164  * @param name Label name.
165  * @param value Value of the label.
166  */
168  TPEF::ASpaceElement& aSpace,
169  std::string& name, UValue& value) :
170  manager_(manager), aSpace_(aSpace),
171  name_(name), value_(value) {
172 }
173 
174 void
175 AddLabelActor::operator() (const char*, const char*) const {
177 }
178 
179 /////////////////////////////////////////////////////////////////////////////
180 /// AddProcedureActor
181 /////////////////////////////////////////////////////////////////////////////
182 
183 /**
184  * Adds procedure symbol to label manager.
185  *
186  * @param manager Label manager that is used to label bookkeeping.
187  * @param name Name of the procedure.
188  * @param value Instruction address to procedure start.
189  */
191  LabelManager& manager,
192  std::string& name, UValue& value) :
193  manager_(manager),
194  name_(name), value_(value) {
195 }
196 
197 void
198 AddProcedureActor::operator() (const char*, const char*) const {
200 }
201 
202 /////////////////////////////////////////////////////////////////////////////
203 /// SetGlobalActor
204 /////////////////////////////////////////////////////////////////////////////
205 /**
206  * Sets label to be globally visible.
207  *
208  * @param manager Manager wehere to set global.
209  */
211  manager_(manager) {
212 }
213 
214 void
215 SetGlobalActor::operator() (const char* start, const char* end) const {
216  std::string str(start, end);
217  manager_.setGlobal(str);
218 }
219 
220 /////////////////////////////////////////////////////////////////////////////
221 /// AssemblerParser
222 /////////////////////////////////////////////////////////////////////////////
223 
224 /**
225  * Constructor.
226  *
227  * @param aBin TPEF object where to create program.
228  * @param aMach Machine which for program is written.
229  * @param parserDiagnostic Assembler root class for warning handling.
230  */
232  TPEF::Binary &aBin, const TTAMachine::Machine &aMach,
233  AssemblyParserDiagnostic* parserDiagnostic,
234  bool codeLinesOnly) :
235  bin_(aBin),
236  resourceManager_(aBin, aMach, parserDiagnostic),
237  dataSectionCreator_(resourceManager_, parserDiagnostic),
238  codeSectionCreator_(resourceManager_, aMach, parserDiagnostic),
239  labelManager_(aBin, resourceManager_, parserDiagnostic),
240  codeLinesOnly_(codeLinesOnly) {
241 }
242 
243 /**
244  * Frees all resources allocated by parser.
245  */
246 void
251 }
252 
253 bool
254 AssemblerParser::compile(const std::string& asmCode) const {
255 #if BOOST_VERSION >= 103800
256  return boost::spirit::classic::parse(asmCode.c_str(), *this).full;
257 #else
258  return boost::spirit::parse(asmCode.c_str(), *this).full;
259 #endif
260 }
261 
262 /**
263  * Returns line number where parse error happened in assembler file.
264  *
265  * @return Line number where parse error happened in assembler file.
266  */
267 UValue
269  return parserTemp_.lineNumber;
270 }
271 
272 /**
273  * Finalizes parsed TPEF.
274  *
275  * After calling this successfully parser should contain valid tpef.
276  */
277 void
278 AssemblerParser::finalize(bool littleEndian) const {
279  bin_.setArch(Binary::FA_TTA_TUT);
280  bin_.setType(Binary::FT_PARALLEL);
281 
282  // these must be called in this order to make sure that all label values
283  // are resolved before they are used
287 }
PrintString::str_
const char * str_
Definition: AssemblerParser.hh:202
SetStartAddressActor::creator_
DataSectionCreator & creator_
Definition: AssemblerParser.hh:246
CodeSectionCreator::newSection
void newSection(UValue startAddress)
Definition: CodeSectionCreator.cc:81
SetStartAddressActor::SetStartAddressActor
SetStartAddressActor(DataSectionCreator &creator, UValue &startAddress)
SetStartAddressActor.
Definition: AssemblerParser.cc:125
AssemblerParser.hh
AddLabelActor::operator()
void operator()(const char *, const char *) const
Definition: AssemblerParser.cc:175
AddDataLineActor::operator()
void operator()(const char *, const char *) const
Definition: AssemblerParser.cc:151
AssemblerParser::labelManager_
LabelManager labelManager_
Creates symbol and relocation sections.
Definition: AssemblerParser.hh:952
NewCodeSectionActor::NewCodeSectionActor
NewCodeSectionActor(CodeSectionCreator &creator, UValue &startAddress)
NewCodeSectionActor.
Definition: AssemblerParser.cc:83
TPEF::Binary
Definition: Binary.hh:49
AddDataLineActor::creator_
DataSectionCreator & creator_
Definition: AssemblerParser.hh:261
AddLabelActor::manager_
LabelManager & manager_
Definition: AssemblerParser.hh:277
PrintString::operator()
void operator()(const char *, const char *) const
Definition: AssemblerParser.cc:70
CodeSectionCreator
Definition: CodeSectionCreator.hh:58
AddProcedureActor::value_
UValue & value_
Definition: AssemblerParser.hh:297
AddMoveActor::move_
const ParserMove & move_
Definition: AssemblerParser.hh:232
LabelManager::addProcedure
void addProcedure(std::string &name, UValue value)
Definition: LabelManager.cc:201
TPEF::Binary::setType
void setType(FileType type)
LabelManager::finalize
void finalize()
Definition: LabelManager.cc:301
AssemblerParser::finalize
void finalize(bool littleEndian) const
Definition: AssemblerParser.cc:278
AddProcedureActor::manager_
LabelManager & manager_
Definition: AssemblerParser.hh:295
DataSectionCreator::cleanup
void cleanup()
Definition: DataSectionCreator.cc:156
ResourceSection.hh
AssemblyParserDiagnostic
Definition: AssemblyParserDiagnostic.hh:68
CodeSectionCreator::addMove
void addMove(const ParserMove &move)
Definition: CodeSectionCreator.cc:102
LabelManager
Definition: LabelManager.hh:64
NewCodeSectionActor::startAddress_
UValue & startAddress_
Definition: AssemblerParser.hh:217
SetStartAddressActor::startAddress_
UValue & startAddress_
Definition: AssemblerParser.hh:247
AddLabelActor::name_
std::string & name_
Definition: AssemblerParser.hh:279
AddLabelActor::value_
UValue & value_
Definition: AssemblerParser.hh:280
NewCodeSectionActor::creator_
CodeSectionCreator & creator_
Definition: AssemblerParser.hh:216
RelocSection.hh
SetGlobalActor::manager_
LabelManager & manager_
Definition: AssemblerParser.hh:310
SymbolSection.hh
LabelManager::cleanup
void cleanup()
Definition: LabelManager.cc:263
UValue
unsigned long UValue
Definition: ParserStructs.hh:44
AddLabelActor::AddLabelActor
AddLabelActor(LabelManager &manager, TPEF::ASpaceElement &aSpace, std::string &name, UValue &value)
AddLabelActor.
Definition: AssemblerParser.cc:167
TPEF::ASpaceElement
Definition: ASpaceElement.hh:48
SetGlobalActor::SetGlobalActor
SetGlobalActor(LabelManager &manager)
SetGlobalActor.
Definition: AssemblerParser.cc:210
ParserTemp::lineNumber
UValue lineNumber
Line number of currently parsed line.
Definition: AssemblerParser.hh:140
PrintString::PrintString
PrintString(const char *aStr)
PrintString actor.
Definition: AssemblerParser.cc:54
LabelManager::addLabel
void addLabel(TPEF::ASpaceElement &aSpace, std::string &name, UValue value)
Definition: LabelManager.cc:160
AssemblerParser::AssemblerParser
AssemblerParser(TPEF::Binary &aBin, const TTAMachine::Machine &aMach, AssemblyParserDiagnostic *parserDiagnostic, bool codeLinesOnly=false)
AssemblerParser.
Definition: AssemblerParser.cc:231
DataSectionCreator::finalize
void finalize(TPEF::Binary &tpef, LabelManager &labels, bool littleEndian)
Definition: DataSectionCreator.cc:190
AssemblerParser::dataSectionCreator_
DataSectionCreator dataSectionCreator_
Creates data sections.
Definition: AssemblerParser.hh:948
LabelManager::setGlobal
void setGlobal(std::string &labelName)
Definition: LabelManager.cc:217
ParserMove
Definition: ParserStructs.hh:391
AddDataLineActor::dataLine_
const DataLine & dataLine_
Definition: AssemblerParser.hh:262
DataLine
Definition: ParserStructs.hh:477
NewCodeSectionActor::operator()
void operator()(const char *, const char *) const
Definition: AssemblerParser.cc:90
CodeSectionCreator::finalize
void finalize(TPEF::Binary &tpef, LabelManager &labels)
Definition: CodeSectionCreator.cc:347
DataSectionCreator::setAreaStartAddress
void setAreaStartAddress(UValue address)
Definition: DataSectionCreator.cc:73
AssemblerParser::parserTemp_
ParserTemp parserTemp_
Temp-structure containing most recent parsed tokens.
Definition: AssemblerParser.hh:955
CodeSectionCreator::cleanup
void cleanup()
Definition: CodeSectionCreator.cc:518
AddProcedureActor::operator()
void operator()(const char *, const char *) const
Definition: AssemblerParser.cc:198
AddLabelActor::aSpace_
TPEF::ASpaceElement & aSpace_
Definition: AssemblerParser.hh:278
DataSectionCreator
Definition: DataSectionCreator.hh:52
AddMoveActor::creator_
CodeSectionCreator & creator_
Definition: AssemblerParser.hh:231
AssemblerParser::codeSectionCreator_
CodeSectionCreator codeSectionCreator_
Creates code section.
Definition: AssemblerParser.hh:950
AddProcedureActor::name_
std::string & name_
Definition: AssemblerParser.hh:296
AddDataLineActor::AddDataLineActor
AddDataLineActor(DataSectionCreator &creator, const DataLine &dataLine)
AddDataLineActor.
Definition: AssemblerParser.cc:145
AssemblerParser::errorLine
UValue errorLine()
Definition: AssemblerParser.cc:268
AssemblerParser::cleanup
void cleanup()
Definition: AssemblerParser.cc:247
SetGlobalActor::operator()
void operator()(const char *start, const char *end) const
Definition: AssemblerParser.cc:215
AddMoveActor::operator()
void operator()(const char *, const char *) const
Definition: AssemblerParser.cc:111
AddMoveActor::AddMoveActor
AddMoveActor(CodeSectionCreator &creator, const ParserMove &move)
AddMoveActor.
Definition: AssemblerParser.cc:104
DataSectionCreator::addDataLine
void addDataLine(const DataLine &origLine)
Definition: DataSectionCreator.cc:84
CodeSection.hh
AssemblerParser::compile
bool compile(const std::string &asmCode) const
Definition: AssemblerParser.cc:254
SetStartAddressActor::operator()
void operator()(const char *, const char *) const
Definition: AssemblerParser.cc:131
TPEF
Definition: Assembler.hh:43
TTAMachine::Machine
Definition: Machine.hh:73
Binary.hh
AssemblerParser::bin_
TPEF::Binary & bin_
TPEF where to program is compiled.
Definition: AssemblerParser.hh:943
AddProcedureActor::AddProcedureActor
AddProcedureActor(LabelManager &manager, std::string &name, UValue &value)
AddProcedureActor.
Definition: AssemblerParser.cc:190
TPEF::Binary::setArch
void setArch(FileArchitecture arch)