OpenASIP  2.0
CUOpcodeGenerator.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2014 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 CUOpcodeGenerator.hh
26  *
27  * Implementation of ProcessorGenerator class.
28  *
29  * @author Henry Linjamäki 2014 (henry.linjamaki-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include <set>
34 #include <algorithm>
35 
36 #include "CUOpcodeGenerator.hh"
37 
38 #include "Machine.hh"
39 #include "ControlUnit.hh"
40 #include "ProGeTypes.hh"
41 #include "MachineInfo.hh"
42 #include "Conversion.hh"
43 #include "MathTools.hh"
44 
45 using namespace std;
46 
47 namespace ProGe {
48 
49 /*
50  * Constructor for the class.
51  *
52  * @param mach The machine.
53  * @param entityName Prefix name for opcode package
54  */
55 CUOpcodeGenerator::CUOpcodeGenerator(
56  const TTAMachine::Machine& mach, const string& entityName)
57  : mach_(&mach), entityStr_(entityName) {}
58 
59 CUOpcodeGenerator::CUOpcodeGenerator() : mach_(NULL), entityStr_() {}
60 
62 
63 /*
64  * Returns encodings for GCU as map<operationName, encoding>.
65  */
68  assert(mach_ != NULL);
69 
72  size_t encoding = 0;
74 
75  MachineInfo::OperationSet::iterator it;
76  for (it = gcuOps.begin(); it != gcuOps.end(); it++) {
77  encMap.insert(make_pair(StringTools::stringToLower(*it), encoding));
78  encoding++;
79  }
80  return encMap;
81 }
82 
83 size_t
84 CUOpcodeGenerator::encoding(const std::string& operName) const {
86  if (encMap.count(operName)) {
87  return encMap.at(operName);
88  } else {
90  InstanceNotFound, "CU does not have operation" + operName + ".");
91  }
92 }
93 
94 /*
95  * Return required width for GCU's opcode ports.
96  */
97 size_t
100 }
101 
102 /*
103  * Generates RTL package that holds GCU's opcode encoding constants.
104  *
105  * The opcode names are written as IFE_<operName> in upper case. For legacy
106  * support if GCU does not include JUMP or CALL operations then those will
107  * be still added to the package too as dummy operations since ifetch
108  * templates refers to them.
109  *
110  * @param language Target language. VHDL and Verilog are supported.
111  * @param stream File stream.
112  */
113 void
115  HDL language, std::ofstream& stream) const {
116  OperationEncodingMapType gcuEncodings = encodings();
117 
118  // Add dummy operations for missing CALL and JUMP operations since ifetch
119  // templates refers to them.
120  gcuEncodings.insert(
121  make_pair(OperationType("call"), gcuEncodings.size()));
122  gcuEncodings.insert(
123  make_pair(OperationType("jump"), gcuEncodings.size()));
124 
125  if (language == VHDL) {
126  WriteVhdlOpcodePackage(gcuEncodings, stream);
127  } else if (language == Verilog) {
128  WriteVerilogOpcodePackage(gcuEncodings, stream);
129  } else {
130  assert(false && "Unsupported HDL.");
131  }
132 }
133 
134 /*
135  * Return required width for GCU's opcode ports.
136  *
137  * @param mach The machine.
138  */
139 size_t
141  const TTAMachine::ControlUnit* gcu = mach.controlUnit();
142  size_t opCount = gcu->operationCount();
143  if (opCount == 0) {
144  return 0;
145  } else {
146  return std::max(MathTools::requiredBits(opCount - 1), 1);
147  }
148 }
149 
150 /*
151  * VHDL version for writing GCU opcode package.
152  */
153 void
155  const OperationEncodingMapType& encodings, std::ofstream& stream) const {
156  stream << "library IEEE;" << endl
157  << "use IEEE.std_logic_1164.all;" << endl
158  << endl
159  << "package " + entityStr_ + "_gcu_opcodes is" << endl;
160  OperationEncodingMapType::const_iterator it;
161  for (it = encodings.begin(); it != encodings.end(); it++) {
162  stream << " constant IFE_" << StringTools::stringToUpper(it->first)
163  << " : natural := " << Conversion::toString(it->second) << ";"
164  << endl;
165  }
166  stream << "end " + entityStr_ + "_gcu_opcodes;" << endl;
167 }
168 
169 /*
170  * Verilog version for writing GCU opcode package.
171  */
172 void
174  const OperationEncodingMapType& encodings, std::ofstream& stream) const {
175  if (encodings.empty()) {
176  return;
177  }
178 
179  OperationEncodingMapType::const_iterator it;
180  for (it = encodings.begin(); it != encodings.end(); it++) {
181  stream << "parameter IFE_" << StringTools::stringToUpper(it->first)
182  << "=" << Conversion::toString(it->second);
183  if (it != --encodings.end()) {
184  stream << "," << endl;
185  } else {
186  stream << endl;
187  }
188  }
189 }
190 
191 } /* namespace ProGe */
ProGe::Verilog
@ Verilog
Verilog.
Definition: ProGeTypes.hh:42
ProGe::CUOpcodeGenerator::OperationType
std::string OperationType
Definition: CUOpcodeGenerator.hh:60
MachineInfo.hh
ProGe::CUOpcodeGenerator::WriteVerilogOpcodePackage
void WriteVerilogOpcodePackage(const OperationEncodingMapType &encodings, std::ofstream &stream) const
Definition: CUOpcodeGenerator.cc:173
Conversion::toString
static std::string toString(const T &source)
StringTools::stringToUpper
static std::string stringToUpper(const std::string &source)
Definition: StringTools.cc:143
assert
#define assert(condition)
Definition: Application.hh:86
ProGe::CUOpcodeGenerator::gcuOpcodeWidth
static size_t gcuOpcodeWidth(const TTAMachine::Machine &mach)
Definition: CUOpcodeGenerator.cc:140
TTAMachine::Machine::controlUnit
virtual ControlUnit * controlUnit() const
Definition: Machine.cc:345
ProGe::VHDL
@ VHDL
VHDL.
Definition: ProGeTypes.hh:41
ProGe::CUOpcodeGenerator::generateOpcodePackage
void generateOpcodePackage(HDL language, std::ofstream &stream) const
Definition: CUOpcodeGenerator.cc:114
TTAMachine::ControlUnit
Definition: ControlUnit.hh:50
THROW_EXCEPTION
#define THROW_EXCEPTION(exceptionType, message)
Exception wrapper macro that automatically includes file name, line number and function name where th...
Definition: Exception.hh:39
Conversion.hh
MachineInfo::getOpset
static OperationSet getOpset(const TTAMachine::Machine &mach)
Definition: MachineInfo.cc:65
ProGe::CUOpcodeGenerator::mach_
const TTAMachine::Machine * mach_
Definition: CUOpcodeGenerator.hh:85
TTAMachine::FunctionUnit::operationCount
virtual int operationCount() const
Definition: FunctionUnit.cc:419
MathTools::requiredBits
static int requiredBits(unsigned long int number)
Machine.hh
ProGe::CUOpcodeGenerator::entityStr_
const std::string entityStr_
Definition: CUOpcodeGenerator.hh:86
ProGeTypes.hh
CUOpcodeGenerator.hh
ProGe::CUOpcodeGenerator::encoding
size_t encoding(const std::string &operName) const
Definition: CUOpcodeGenerator.cc:84
MachineInfo::OperationSet
TCETools::CIStringSet OperationSet
Definition: MachineInfo.hh:60
ProGe
Definition: FUGen.hh:54
ProGe::CUOpcodeGenerator::~CUOpcodeGenerator
virtual ~CUOpcodeGenerator()
Definition: CUOpcodeGenerator.cc:61
ProGe::CUOpcodeGenerator::WriteVhdlOpcodePackage
void WriteVhdlOpcodePackage(const OperationEncodingMapType &encodings, std::ofstream &stream) const
Definition: CUOpcodeGenerator.cc:154
ControlUnit.hh
ProGe::HDL
HDL
HDLs supported by ProGe.
Definition: ProGeTypes.hh:40
ProGe::CUOpcodeGenerator::encodings
OperationEncodingMapType encodings() const
Definition: CUOpcodeGenerator.cc:67
MathTools.hh
ProGe::CUOpcodeGenerator::opcodeWidth
size_t opcodeWidth() const
Definition: CUOpcodeGenerator.cc:98
ProGe::CUOpcodeGenerator::OperationEncodingMapType
std::map< OperationType, EncodingType, TCEString::ICLess > OperationEncodingMapType
Definition: CUOpcodeGenerator.hh:63
StringTools::stringToLower
static std::string stringToLower(const std::string &source)
Definition: StringTools.cc:160
InstanceNotFound
Definition: Exception.hh:304
ProGe::CUOpcodeGenerator::CUOpcodeGenerator
CUOpcodeGenerator()
Definition: CUOpcodeGenerator.cc:59
TTAMachine::Machine
Definition: Machine.hh:73