OpenASIP 2.2
Loading...
Searching...
No Matches
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
44using TPEF::Binary;
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 */
67Binary*
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 */
139void
140Assembler::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 */
151 return *mach_;
152}
153
154const std::set<CompilerMessage>&
157}
158
159/**
160 * Returns last thrown error message..
161 *
162 * @return Error that halted compilation.
163 */
164const CompilerMessage&
166 return error_;
167}
168
169/**
170 * Reads assembly code from binary stream to a string.
171 */
172void
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
195std::string
196Assembler::codeLine(UValue lineNumber) const {
197 return parserDiagnostic_.codeLine(lineNumber);
198}
199
#define __func__
unsigned char Byte
Definition BaseType.hh:116
unsigned long UValue
TTAMachine::Machine * mach_
Machine for assembled TPEF.
Definition Assembler.hh:80
Assembler(TPEF::BinaryStream &assemblerFile, TTAMachine::Machine &assemblerMachine)
Definition Assembler.cc:55
TPEF::Binary * compile()
Definition Assembler.cc:68
AssemblyParserDiagnostic parserDiagnostic_
Parser messages.
Definition Assembler.hh:86
TPEF::BinaryStream & stream_
File containing file to assemble.
Definition Assembler.hh:77
const CompilerMessage & error() const
Definition Assembler.cc:165
std::string codeLine(UValue lineNumber) const
Definition Assembler.cc:196
std::shared_ptr< std::string > asmCode_
String containing code to compile.
Definition Assembler.hh:89
void readAssemblerCode()
Definition Assembler.cc:173
const std::set< CompilerMessage > & warnings() const
Definition Assembler.cc:155
const TTAMachine::Machine & targetMachine() const
Definition Assembler.cc:150
CompilerMessage error_
Last thrown error message.
Definition Assembler.hh:83
void addWarning(UValue lineNumber, std::string errorMessage)
Definition Assembler.cc:140
std::string codeLine(UValue lineNumber) const
const std::set< CompilerMessage > & warnings() const
void reset(std::shared_ptr< const std::string > assemblyText)
void addWarning(UValue lineNumber, const std::string &message)
int codeFileLineNumber()
static std::string toString(const T &source)
std::string errorMessage() const
Definition Exception.cc:123
void setReadPosition(unsigned int position)
unsigned int readPosition()
unsigned int sizeOfFile()
bool isLittleEndian() const
Definition Machine.hh:258
void finalize(bool littleEndian) const
bool compile(const std::string &asmCode) const
std::string message
Message.
std::string assemblerLine
Assembly code line number.
UValue lineNumber
Message generation line number.