OpenASIP  2.0
OTAOperationDialog.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2022 Tampere University.
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 /**
19  * @file OTAOperationDialog.cc
20  *
21  * Implementation of OTAOperationDialog class.
22  *
23  * @author Kari Hepola 2022
24  * @note rating: red
25  */
26 
27 #include <wx/wx.h>
28 #include <wx/spinctrl.h>
29 #include <wx/statline.h>
30 #include <wx/valgen.h>
31 #include <boost/format.hpp>
32 
33 #include "OTAOperationDialog.hh"
34 #include "Machine.hh"
36 #include "WxConversion.hh"
37 #include "AssocTools.hh"
38 #include "WidgetTools.hh"
39 #include "InformationDialog.hh"
40 #include "ModelConstants.hh"
41 #include "WidgetTools.hh"
42 #include "GUITextGenerator.hh"
43 #include "ProDeTextGenerator.hh"
44 #include "Application.hh"
45 #include "FunctionUnit.hh"
46 #include "HWOperation.hh"
47 #include "ErrorDialog.hh"
48 #include "OperationPool.hh"
49 #include "Operation.hh"
50 #include "RISCVFields.hh"
51 #include "MapTools.hh"
52 
53 using boost::format;
54 using std::string;
55 using namespace TTAMachine;
56 
57 BEGIN_EVENT_TABLE(OTAOperationDialog, wxDialog)
58  EVT_LISTBOX(ID_LIST, OTAOperationDialog::onSelectOperation)
60  EVT_TEXT(ID_OP_FILTER, OTAOperationDialog::onOperationFilterChange)
62 
63 /**
64  * The Constructor.
65  *
66  * @param parent Parent window of the dialog.
67  * @param format OperationTriggeredFormat format to edit.
68  * @param operation Operation to edit, NULL if a new operation is being added.
69  */
71  wxWindow* parent,
73  wxDialog(parent, -1, _T("Choose operation"), wxDefaultPosition),
74  operation_(""),
75  format_(format) {
76 
77  createContents(this, true, true);
78  operationList_ = dynamic_cast<wxListBox*>(FindWindow(ID_LIST));
79  FindWindow(wxID_OK)->Disable();
80 }
81 
82 
83 /**
84  * The Destructor.
85  */
87 }
88 
89 int
91  if (format_->name() == "riscv_r_type" ||
92  format_->name() == "riscv_i_type") {
93  return 2;
94  }
95  if (format_->name() == "riscv_s_type" ||
96  format_->name() == "riscv_b_type") {
97  return 3;
98  }
99  if (format_->name() == "riscv_u_type" ||
100  format_->name() == "riscv_j_type") {
101  return 1;
102  }
103  return 0;
104 }
105 
106 int
108  if (format_->name() == "riscv_r_type" ||
109  format_->name() == "riscv_i_type" ||
110  format_->name() == "riscv_u_type" ||
111  format_->name() == "riscv_j_type") {
112  return 1;
113  }
114  return 0;
115 }
116 
117 bool
119  if (format_->name() == "riscv_r_type" ||
120  format_->name() == "riscv_i_type" ||
121  format_->name() == "riscv_s_type" ||
122  format_->name() == "riscv_b_type" ||
123  format_->name() == "riscv_u_type" ||
124  format_->name() == "riscv_j_type") {
125  return true;
126  }
127  return false;
128 }
129 
130 std::set<TCEString>
131 OTAOperationDialog::addRISCVBaseOperations(std::set<TCEString> opset) const {
132  std::map<std::string, int> ops;
133  if (format_->name() == "riscv_r_type") {
134  ops = riscvRTypeOperations;
135  } else if (format_->name() == "riscv_i_type") {
136  ops = riscvITypeOperations;
137  } else if (format_->name() == "riscv_s_type") {
138  ops = riscvSTypeOperations;
139  } else if (format_->name() == "riscv_b_type") {
140  ops = riscvBTypeOperations;
141  } else if (format_->name() == "riscv_u_type") {
142  ops = riscvUTypeOperations;
143  } else if (format_->name() == "riscv_j_type") {
144  ops = riscvJTypeOperations;
145  }
146  for (const auto& op : ops) {
147  const std::string opName = op.first;
148  if (!opNameFilter_.empty() &&
149  opName.find(opNameFilter_) == std::string::npos) {
150  continue;
151  } else if (format_->hasOperation(opName)) {
152  continue;
153  } else if (op.first == "lui") {
154  opset.insert(opName);
155  continue;
156  }
157  const std::string OAName = operationNameTable.at(opName);
158  if (format_->machine()->hasOperation(OAName)) {
159  opset.insert(opName);
160  }
161  }
162  return opset;
163 }
164 
165 
166 /**
167  * Transfers data to the opset list.
168  */
169 bool
171 
172  operationList_->Clear();
173 
174  std::set<TCEString> opset;
175  if (format_->name() == "riscv_r_type") {
177  format_->machine()->functionUnitNavigator();
178  for (int i = 0; i < nav.count(); i++) {
179  FunctionUnit* fu = nav.item(i);
180  for (int j = 0; j < fu->operationCount(); j++) {
181  HWOperation* op = fu->operation(j);
182  TCEString opName =op->name();
183  opName = opName.lower();
184  if (!opNameFilter_.empty() &&
185  opName.find(opNameFilter_) == std::string::npos) {
186  continue;
187  } else if (format_->hasOperation(opName)) {
188  continue;
189  } else if (op->numberOfInputs() != numberOfInputs()) {
190  continue;
191  } else if (op->numberOfOutputs() != numberOfOutputs()) {
192  continue;
193  } else if (MapTools::containsValue(
194  operationNameTable, opName)) {
195  continue;
196  }
197  opset.insert(opName);
198  }
199  }
200  }
201 
202  opset = addRISCVBaseOperations(opset);
203 
204 
205  for (const auto& opName : opset) {
206  operationList_->Append(WxConversion::toWxString(opName));
207  }
208 
209  return true;
210 }
211 
212 /**
213  * Reads user choices from the dialog widgets.
214  */
215 bool
217  operation_ = WxConversion::toString(operationList_->GetStringSelection());
218  return true;
219 }
220 
221 /**
222  * Event handler for the OK button.
223  */
224 void
225 OTAOperationDialog::onOK(wxCommandEvent&) {
226  if (operationList_->GetSelection() == wxNOT_FOUND) {
227  wxString message = _T("No operation selected.");
228  ErrorDialog dialog(this, message);
229  dialog.ShowModal();
230  return;
231  }
232  if (!format_->hasOperation(operation_)) {
233  format_->addOperation(operation_);
234  }
235  TransferDataFromWindow();
236  EndModal(wxID_OK);
237 }
238 
239 /**
240  * Event handler for the operation list selections.
241  *
242  * Enables and disables the OK button.
243  * Displays operation description and ports count.
244  */
245 void
247  FindWindow(wxID_OK)->Enable(operationList_->GetSelection() != wxNOT_FOUND);
248 
249  operation_ = WxConversion::toString(operationList_->GetStringSelection());
250 }
251 
252 
253 /**
254  * Event handler for opset filtering.
255  */
256 void
258  std::string pattern(event.GetString().mb_str());
259  std::string::iterator it;
260  it = std::remove_if(pattern.begin(), pattern.end(), [](const char& c) {
261  return c == ' ';
262  });
263  pattern.erase(it, pattern.end());
264  for (auto& c : pattern) c = toupper(c);
265  opNameFilter_ = pattern;
266  opNameFilter_ = opNameFilter_.lower();
268 }
269 
270 /**
271  * Creates the dialog widgets.
272  *
273  * @param parent Parent window of the widgets.
274  */
275 wxSizer*
276 OTAOperationDialog::createContents(wxWindow *parent, bool call_fit, bool set_sizer) {
277 
278  wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
279  // Sizer for leftSizer and rightSizer
280  wxBoxSizer *upperSizer = new wxBoxSizer(wxHORIZONTAL);
281 
282  // Sizer for oplistbox, filterlabel and filterinput
283  wxBoxSizer *leftSizer = new wxBoxSizer(wxVERTICAL);
284  // List of operations
285  wxListBox *opListBox = new wxListBox(parent, ID_LIST, wxDefaultPosition,
286  wxSize(210, 150), 0, NULL, wxLB_SINGLE|wxLB_SORT);
287  leftSizer->Add(opListBox, 0, wxEXPAND|wxALL, 5);
288 
289  // Sizer for opNameFilterLabel and opNameFilter
290  wxBoxSizer *filterSizer = new wxBoxSizer(wxHORIZONTAL);
291  // TextLabel "Filter:"
292  wxStaticText *opNameFilterLabel = new wxStaticText(parent,
293  ID_OP_FILTER_LABEL, wxT("Filter:"), wxDefaultPosition, wxDefaultSize,
294  0);
295  // Operation filter input
296  wxTextCtrl *opNameFilter = new wxTextCtrl(parent, ID_OP_FILTER, wxT(""),
297  wxDefaultPosition, wxDefaultSize, 0);
298  filterSizer->Add(opNameFilterLabel, 0, 0);
299  filterSizer->Add(opNameFilter, 1, wxEXPAND);
300  leftSizer->Add(filterSizer, 0, wxEXPAND|wxALL, 5);
301 
302  upperSizer->Add(leftSizer, 0, wxALL, 5);
303 
304  mainSizer->Add(upperSizer, 1, wxEXPAND);
305 
306  // Static line
307  wxStaticLine *horisontalLine = new wxStaticLine(parent, ID_LINE,
308  wxDefaultPosition, wxSize(20,-1), wxLI_HORIZONTAL);
309  mainSizer->Add(horisontalLine, 0, wxEXPAND|wxALL, 5);
310 
311  // Sizer for Cancel and OK buttons
312  wxBoxSizer *buttonsSizer = new wxBoxSizer(wxHORIZONTAL);
313  // Cancel button
314  wxButton *cancelButton = new wxButton(parent, wxID_CANCEL, wxT("&Cancel"),
315  wxDefaultPosition, wxDefaultSize, 0);
316  buttonsSizer->Add(cancelButton, 0, wxALIGN_CENTER|wxALL, 5);
317  // OK button
318  wxButton *okButton = new wxButton(parent, wxID_OK, wxT("&OK"),
319  wxDefaultPosition, wxDefaultSize, 0);
320  buttonsSizer->Add(okButton, 0, wxALIGN_CENTER|wxALL, 5);
321 
322  mainSizer->Add(buttonsSizer, 0, 0, 5);
323 
324  if (set_sizer) {
325  parent->SetSizer(mainSizer);
326  if (call_fit) {
327  mainSizer->SetSizeHints( parent );
328  }
329  }
330 
331  return mainSizer;
332 }
OTAOperationDialog::onSelectOperation
void onSelectOperation(wxCommandEvent &event)
Definition: OTAOperationDialog.cc:246
riscvRTypeOperations
const std::map< std::string, int > riscvRTypeOperations
Definition: RISCVFields.hh:6
WxConversion::toWxString
static wxString toWxString(const std::string &source)
TCEString::lower
TCEString lower() const
Definition: TCEString.cc:78
riscvSTypeOperations
const std::map< std::string, int > riscvSTypeOperations
Definition: RISCVFields.hh:27
TTAMachine::HWOperation
Definition: HWOperation.hh:52
OTAOperationDialog::TransferDataToWindow
virtual bool TransferDataToWindow()
Definition: OTAOperationDialog.cc:170
MapTools.hh
WidgetTools.hh
FindWindow
Definition: FindWindow.hh:49
riscvJTypeOperations
const std::map< std::string, int > riscvJTypeOperations
Definition: RISCVFields.hh:37
TTAMachine::Machine::Navigator::count
int count() const
OTAOperationDialog::TransferDataFromWindow
virtual bool TransferDataFromWindow()
Definition: OTAOperationDialog.cc:216
ProDeTextGenerator.hh
operationNameTable
const std::map< std::string, std::string > operationNameTable
Definition: RISCVFields.hh:39
TTAMachine::FunctionUnit
Definition: FunctionUnit.hh:55
riscvUTypeOperations
const std::map< std::string, int > riscvUTypeOperations
Definition: RISCVFields.hh:34
HWOperation.hh
ErrorDialog
Definition: ErrorDialog.hh:42
TTAMachine::HWOperation::name
const std::string & name() const
Definition: HWOperation.cc:141
ErrorDialog.hh
InformationDialog.hh
OTAOperationDialog::numberOfOutputs
int numberOfOutputs() const
Definition: OTAOperationDialog.cc:107
Application.hh
TTAMachine::HWOperation::numberOfInputs
int numberOfInputs() const
Definition: HWOperation.cc:384
TTAMachine::FunctionUnit::operationCount
virtual int operationCount() const
Definition: FunctionUnit.cc:419
Operation.hh
ModelConstants.hh
OTAOperationDialog::addRISCVBaseOperations
std::set< TCEString > addRISCVBaseOperations(std::set< TCEString > opset) const
Definition: OTAOperationDialog.cc:131
Machine.hh
TTAMachine::OperationTriggeredFormat
Definition: OperationTriggeredFormat.hh:44
OTAOperationDialog
Definition: OTAOperationDialog.hh:42
OTAOperationDialog::numberOfInputs
int numberOfInputs() const
Definition: OTAOperationDialog.cc:90
GUITextGenerator.hh
OTAOperationDialog::onOperationFilterChange
void onOperationFilterChange(wxCommandEvent &event)
Definition: OTAOperationDialog.cc:257
OTAOperationDialog::~OTAOperationDialog
virtual ~OTAOperationDialog()
Definition: OTAOperationDialog.cc:86
EVT_BUTTON
EVT_BUTTON(ID_EDIT_ARCH_PORT, FUImplementationDialog::onEditArchitecturePort) EVT_BUTTON(ID_ADD_EXTERNAL_PORT
MapTools::containsValue
static bool containsValue(const MapType &aMap, const ValueType &aValue)
AssocTools.hh
riscvBTypeOperations
const std::map< std::string, int > riscvBTypeOperations
Definition: RISCVFields.hh:30
TCEString
Definition: TCEString.hh:53
OTAOperationDialog.hh
riscvITypeOperations
const std::map< std::string, int > riscvITypeOperations
Definition: RISCVFields.hh:17
OTAOperationDialog::validFormatName
bool validFormatName() const
Definition: OTAOperationDialog.cc:118
WxConversion.hh
TTAMachine::Machine::Navigator::item
ComponentType * item(int index) const
TTAMachine::HWOperation::numberOfOutputs
int numberOfOutputs() const
Definition: HWOperation.cc:404
TTAMachine::FunctionUnit::operation
virtual HWOperation * operation(const std::string &name) const
Definition: FunctionUnit.cc:363
OperationTriggeredFormat.hh
TTAMachine
Definition: Assembler.hh:48
WxConversion::toString
static std::string toString(const wxString &source)
RISCVFields.hh
OperationPool.hh
TTAMachine::Machine::Navigator
Definition: Machine.hh:186
OTAOperationDialog::onOK
void onOK(wxCommandEvent &event)
Definition: OTAOperationDialog.cc:225
END_EVENT_TABLE
END_EVENT_TABLE() using namespace IDF
FunctionUnit.hh
OTAOperationDialog::createContents
wxSizer * createContents(wxWindow *parent, bool call_fit, bool set_sizer)
Definition: OTAOperationDialog.cc:276