OpenASIP  2.0
Assembler.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2017 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 Assembler.cc
26  *
27  * Definition of Assembler class.
28  *
29  * @author Mikael Lepistö 2005 (tmlepist-no.spam-cs.tut.fi)
30  * @author Pekka Jääskeläinen 2006 (pekka.jaaskelainen-no.spam-tut.fi)
31  * @author Henry Linjamäki 2017 (henry.linjamaki-no.spam-tut.fi)
32  *
33  * @note rating: yellow
34  */
35 
36 #include "Assembler.hh"
37 #include "AssemblerParser.hh"
38 
39 #include "Machine.hh"
40 
41 #include "BinaryStream.hh"
42 #include "Binary.hh"
43 
44 using TPEF::Binary;
45 using TPEF::BinaryStream;
46 
48 
49 /**
50  * Constructor.
51  *
52  * @param assemblerFile Stream containing file to assemble.
53  * @param assemblerMachine Machine to use for running compiled binary.
54  */
56  BinaryStream& assemblerFile,
57  Machine& assemblerMachine) :
58  stream_(assemblerFile), mach_(&assemblerMachine) {
59 }
60 
61 /**
62  * Compiles assembler and returns TPEF hierarchy.
63  *
64  * @return Compiled TPEF.
65  * @exception CompileError If there was any problems during the compilation.
66  */
67 Binary*
69  // read binary stream to string
71 
72  // clear old warnings from previous compilation
74 
75  Binary* newBinary = new Binary();
76 
77  // this one does the actual parsing work
78  AssemblerParser parser(*newBinary, *mach_, &parserDiagnostic_);
79 
80  try {
81  try {
82  if (!parser.compile(*asmCode_)) {
83 
85  __FILE__, __LINE__, __func__, "Syntax error.");
86 
87  error.setCodeFileLineNumber(parser.errorLine());
88 
89  throw error;
90  }
91 
92  // ObjectAllreadyExists and IllegalMachine exceptions.
93  } catch (Exception& e) {
94 
96  __FILE__, __LINE__, __func__, e.errorMessage());
97 
98  error.setCause(e);
99  error.setCodeFileLineNumber(parser.errorLine());
100 
101  throw error;
102  }
103 
104  parser.finalize(mach_->isLittleEndian());
105 
106  } catch (CompileError& e) {
107 
111 
112  // clean up internals of creators and managers and delete
113  // partial binary
114  parser.cleanup();
115 
116  delete newBinary;
117  newBinary = NULL;
118 
120  __FILE__, __LINE__, __func__,
121  "Error in line " + Conversion::toString(error_.lineNumber) +
122  ": " + error_.assemblerLine + "\nMessage: " + error_.message);
123 
124  error.setCause(e);
125  error.setCodeFileLineNumber(error_.lineNumber);
126 
127  throw error;
128  }
129 
130  return newBinary;
131 }
132 
133 /**
134  * Adds warning message for client to see.
135  *
136  * @param lineNumber Line number of assembly code, where error accured.
137  * @param errorMessage Warning message.
138  */
139 void
140 Assembler::addWarning(UValue lineNumber, std::string errorMessage) {
141  parserDiagnostic_.addWarning(lineNumber, errorMessage);
142 }
143 
144 /**
145  * Returns the target machine of the assembler.
146  *
147  * @return The target machine.
148  */
149 const TTAMachine::Machine&
151  return *mach_;
152 }
153 
154 const std::set<CompilerMessage>&
156  return parserDiagnostic_.warnings();
157 }
158 
159 /**
160  * Returns last thrown error message..
161  *
162  * @return Error that halted compilation.
163  */
164 const CompilerMessage&
166  return error_;
167 }
168 
169 /**
170  * Reads assembly code from binary stream to a string.
171  */
172 void
174  unsigned int streamPosition = stream_.readPosition();
175 
176  asmCode_ = std::make_shared<std::string>();
177 
178  // read file to string and mark line start indexes.
179  for (unsigned int i = streamPosition; i < stream_.sizeOfFile(); i++) {
180  Byte readByte = stream_.readByte();
181  *asmCode_ += readByte;
182  }
183 
184  // restore stream position...
185  stream_.setReadPosition(streamPosition);
186 }
187 
188 /***
189  * Returns requested code line.
190  *
191  * @param Line number to return.
192  * @return Requested assembly code line.
193  */
194 
195 std::string
196 Assembler::codeLine(UValue lineNumber) const {
197  return parserDiagnostic_.codeLine(lineNumber);
198 }
199 
AssemblerParser
Definition: AssemblerParser.hh:331
AssemblerParser.hh
Assembler::error
const CompilerMessage & error() const
Definition: Assembler.cc:165
Assembler.hh
TPEF::BinaryStream::readPosition
unsigned int readPosition()
Definition: BinaryStream.cc:561
TPEF::BinaryStream::setReadPosition
void setReadPosition(unsigned int position)
Definition: BinaryStream.cc:629
Assembler::warnings
const std::set< CompilerMessage > & warnings() const
Definition: Assembler.cc:155
TPEF::Binary
Definition: Binary.hh:49
TPEF::BinaryStream
Definition: BinaryStream.hh:59
Assembler::stream_
TPEF::BinaryStream & stream_
File containing file to assemble.
Definition: Assembler.hh:77
CompileError
Definition: Exception.hh:1019
Assembler::Assembler
Assembler(TPEF::BinaryStream &assemblerFile, TTAMachine::Machine &assemblerMachine)
Definition: Assembler.cc:55
AssemblerParser::finalize
void finalize(bool littleEndian) const
Definition: AssemblerParser.cc:278
CompilerMessage::message
std::string message
Message.
Definition: AssemblyParserDiagnostic.hh:47
CompilerMessage::assemblerLine
std::string assemblerLine
Assembly code line number.
Definition: AssemblyParserDiagnostic.hh:48
Byte
unsigned char Byte
Definition: BaseType.hh:116
CompileError::codeFileLineNumber
int codeFileLineNumber()
Definition: Exception.cc:1489
Conversion::toString
static std::string toString(const T &source)
Assembler::parserDiagnostic_
AssemblyParserDiagnostic parserDiagnostic_
Parser messages.
Definition: Assembler.hh:86
TTAMachine::Machine::isLittleEndian
bool isLittleEndian() const
Definition: Machine.hh:258
CompilerMessage::lineNumber
UValue lineNumber
Message generation line number.
Definition: AssemblyParserDiagnostic.hh:49
UValue
unsigned long UValue
Definition: ParserStructs.hh:44
CompilerMessage
Definition: AssemblyParserDiagnostic.hh:46
__func__
#define __func__
Definition: Application.hh:67
TPEF::BinaryStream::readByte
Byte readByte()
Definition: BinaryStream.cc:120
Machine.hh
Exception
Definition: Exception.hh:54
Assembler::asmCode_
std::shared_ptr< std::string > asmCode_
String containing code to compile.
Definition: Assembler.hh:89
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
Assembler::mach_
TTAMachine::Machine * mach_
Machine for assembled TPEF.
Definition: Assembler.hh:80
AssemblyParserDiagnostic::codeLine
std::string codeLine(UValue lineNumber) const
Definition: AssemblyParserDiagnostic.cc:96
Assembler::codeLine
std::string codeLine(UValue lineNumber) const
Definition: Assembler.cc:196
Assembler::readAssemblerCode
void readAssemblerCode()
Definition: Assembler.cc:173
Assembler::compile
TPEF::Binary * compile()
Definition: Assembler.cc:68
Assembler::targetMachine
const TTAMachine::Machine & targetMachine() const
Definition: Assembler.cc:150
BinaryStream.hh
AssemblerParser::errorLine
UValue errorLine()
Definition: AssemblerParser.cc:268
AssemblerParser::cleanup
void cleanup()
Definition: AssemblerParser.cc:247
TPEF::BinaryStream::sizeOfFile
unsigned int sizeOfFile()
Definition: BinaryStream.cc:764
AssemblyParserDiagnostic::addWarning
void addWarning(UValue lineNumber, const std::string &message)
Definition: AssemblyParserDiagnostic.cc:56
Assembler::addWarning
void addWarning(UValue lineNumber, std::string errorMessage)
Definition: Assembler.cc:140
AssemblerParser::compile
bool compile(const std::string &asmCode) const
Definition: AssemblerParser.cc:254
AssemblyParserDiagnostic::reset
void reset(std::shared_ptr< const std::string > assemblyText)
Definition: AssemblyParserDiagnostic.cc:40
TTAMachine::Machine
Definition: Machine.hh:73
Binary.hh
Assembler::error_
CompilerMessage error_
Last thrown error message.
Definition: Assembler.hh:83
AssemblyParserDiagnostic::warnings
const std::set< CompilerMessage > & warnings() const
Definition: AssemblyParserDiagnostic.hh:78