OpenASIP 2.2
Loading...
Searching...
No Matches
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
43using namespace TPEF;
44
45/////////////////////////////////////////////////////////////////////////////
46/// PrintString actor
47/////////////////////////////////////////////////////////////////////////////
48
49/**
50 * Constructor of actor.
51 *
52 * @param aStr String to print.
53 */
54PrintString::PrintString(const char* aStr) : str_(aStr) {
55}
56
57
58/**
59 * Constructor of actor.
60 *
61 * @param aStr String to print.
62 */
63PrintString::PrintString(std::string& aStr) : str_(aStr.c_str()) {
64}
65
66/**
67 * Prints out string of actor.
68 */
69void
70PrintString::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
89void
90NewCodeSectionActor::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
110void
111AddMoveActor::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
130void
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
150void
151AddDataLineActor::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
174void
175AddLabelActor::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
197void
198AddProcedureActor::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
214void
215SetGlobalActor::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 */
246void
252
253bool
254AssemblerParser::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 */
267UValue
271
272/**
273 * Finalizes parsed TPEF.
274 *
275 * After calling this successfully parser should contain valid tpef.
276 */
277void
278AssemblerParser::finalize(bool littleEndian) const {
281
282 // these must be called in this order to make sure that all label values
283 // are resolved before they are used
287}
unsigned long UValue
const DataLine & dataLine_
DataSectionCreator & creator_
void operator()(const char *, const char *) const
AddDataLineActor(DataSectionCreator &creator, const DataLine &dataLine)
AddDataLineActor.
LabelManager & manager_
AddLabelActor(LabelManager &manager, TPEF::ASpaceElement &aSpace, std::string &name, UValue &value)
AddLabelActor.
void operator()(const char *, const char *) const
TPEF::ASpaceElement & aSpace_
std::string & name_
AddMoveActor(CodeSectionCreator &creator, const ParserMove &move)
AddMoveActor.
const ParserMove & move_
CodeSectionCreator & creator_
void operator()(const char *, const char *) const
AddProcedureActor(LabelManager &manager, std::string &name, UValue &value)
AddProcedureActor.
LabelManager & manager_
void operator()(const char *, const char *) const
void newSection(UValue startAddress)
void addMove(const ParserMove &move)
void finalize(TPEF::Binary &tpef, LabelManager &labels)
void addDataLine(const DataLine &origLine)
void finalize(TPEF::Binary &tpef, LabelManager &labels, bool littleEndian)
void setAreaStartAddress(UValue address)
void addProcedure(std::string &name, UValue value)
void setGlobal(std::string &labelName)
void addLabel(TPEF::ASpaceElement &aSpace, std::string &name, UValue value)
CodeSectionCreator & creator_
void operator()(const char *, const char *) const
NewCodeSectionActor(CodeSectionCreator &creator, UValue &startAddress)
NewCodeSectionActor.
void operator()(const char *, const char *) const
PrintString(const char *aStr)
PrintString actor.
const char * str_
void operator()(const char *start, const char *end) const
SetGlobalActor(LabelManager &manager)
SetGlobalActor.
LabelManager & manager_
DataSectionCreator & creator_
SetStartAddressActor(DataSectionCreator &creator, UValue &startAddress)
SetStartAddressActor.
void operator()(const char *, const char *) const
void setArch(FileArchitecture arch)
@ FT_PARALLEL
Fully scheduled or mixed code.
Definition Binary.hh:61
@ FA_TTA_TUT
New TTA template.
Definition Binary.hh:70
void setType(FileType type)
LabelManager labelManager_
Creates symbol and relocation sections.
void finalize(bool littleEndian) const
AssemblerParser(TPEF::Binary &aBin, const TTAMachine::Machine &aMach, AssemblyParserDiagnostic *parserDiagnostic, bool codeLinesOnly=false)
AssemblerParser.
ParserTemp parserTemp_
Temp-structure containing most recent parsed tokens.
TPEF::Binary & bin_
TPEF where to program is compiled.
DataSectionCreator dataSectionCreator_
Creates data sections.
CodeSectionCreator codeSectionCreator_
Creates code section.
bool compile(const std::string &asmCode) const
UValue lineNumber
Line number of currently parsed line.