OpenASIP  2.0
RFGuardDialog.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 RFGuardDialog.cc
26  *
27  * Implementation of RFGuardDialog class.
28  *
29  * @author Veli-Pekka Jääskeläinen 2004 (vjaaskel-no.spam-cs.tut.fi)
30  */
31 
32 #include <wx/wx.h>
33 #include <wx/statline.h>
34 #include <wx/valgen.h>
35 #include <boost/format.hpp>
36 
37 #include "RFGuardDialog.hh"
38 #include "Guard.hh"
39 #include "WxConversion.hh"
40 #include "Machine.hh"
41 #include "RegisterFile.hh"
42 #include "InformationDialog.hh"
43 #include "WidgetTools.hh"
44 #include "GUITextGenerator.hh"
45 #include "ProDeTextGenerator.hh"
46 
47 using boost::format;
48 using std::string;
49 using namespace TTAMachine;
50 
51 BEGIN_EVENT_TABLE(RFGuardDialog, wxDialog)
52  EVT_CHOICE(ID_RF_NAME, RFGuardDialog::onRFChoice)
56 
57 /**
58  * The Constructor.
59  *
60  * @param parent Parent window of the dialog.
61  */
63  wxWindow* parent,
64  Bus* bus,
65  RegisterGuard* guard):
66  wxDialog(parent, -1, _T(""), wxDefaultPosition),
67  inverted_(false),
68  newInverted_(false),
69  index_(0),
70  newIndex_(0),
71  rf_(NULL),
72  bus_(bus),
73  adding_(false) {
74 
75 
76  if (guard == NULL) {
77  // adding a new guard
78  adding_ = true;
79  rf_ = bus_->machine()->registerFileNavigator().item(0);
80  } else {
81  // editing an old guard
82  rf_ = guard->registerFile();
83  index_ = guard->registerIndex();
84  newIndex_ = index_;
85  inverted_ = guard->isInverted();
86 
87  // The guard is temporarily deleted to simplify legality checks.
88  delete guard;
89  guard = NULL;
90  }
91  newInverted_ = inverted_;
92 
93  createContents(this, true, true);
94 
95  // set pointers to the dialog widgets
96  nameChoice_ = dynamic_cast<wxChoice*>(FindWindow(ID_RF_NAME));
97  indexChoice_ = dynamic_cast<wxChoice*>(FindWindow(ID_RF_INDEX));
98  invertedBox_ = dynamic_cast<wxCheckBox*>(FindWindow(ID_INVERTED));
99 
100  invertedBox_->SetValidator(wxGenericValidator(&newInverted_));
101  indexChoice_->SetValidator(wxGenericValidator(&newIndex_));
102 
103  // set widget texts.
104  setTexts();
105 }
106 
107 
108 /**
109  * The Destructor.
110  */
112 }
113 
114 
115 /**
116  * Sets texts for widgets.
117  */
118 void
122 
123  // Dialog title
124  format fmt = prodeTexts->text(
126  SetTitle(WxConversion::toWxString(fmt.str()));
127 
128  // buttons
129  WidgetTools::setLabel(generator, FindWindow(wxID_OK),
131 
132  WidgetTools::setLabel(generator, FindWindow(wxID_CANCEL),
134 
135  WidgetTools::setLabel(generator, FindWindow(ID_HELP),
137 
138  // widget labels
139  WidgetTools::setLabel(prodeTexts, FindWindow(ID_LABEL_NAME),
141 
142  WidgetTools::setLabel(prodeTexts, FindWindow(ID_LABEL_INDEX),
144 
145  WidgetTools::setLabel(prodeTexts, FindWindow(ID_INVERTED),
147 }
148 
149 
150 /**
151  * Transfers data from the Guard object to the dialog widgets.
152  *
153  * @return true, if the transfer was succesful, false otherwise.
154  */
155 bool
157  nameChoice_->Clear();
158  indexChoice_->Clear();
159 
161  bus_->machine()->registerFileNavigator();
162 
163  // set register file name
164  for (int i = 0; i < navigator.count(); i++) {
165  wxString name = WxConversion::toWxString(navigator.item(i)->name());
166  nameChoice_->Append(name);
167  }
168  nameChoice_->SetStringSelection(WxConversion::toWxString(rf_->name()));
169 
170  // set register index
171  for (int i = 0; i < selectedRF()->numberOfRegisters(); i++) {
172  wxString index = WxConversion::toWxString(i);
173  indexChoice_->Append(index);
174  }
175 
176  // add choice to create guard for every index
177  if (adding_) {
179  format fmtAll = prodeTexts->text(ProDeTextGenerator::TXT_ALL);
180  indexChoice_->Append(WxConversion::toWxString(fmtAll.str()));
181  }
182 
183  indexChoice_->SetSelection(newIndex_);
184 
185  // set inverted flag
186  invertedBox_->SetValue(newInverted_);
187 
188  return wxDialog::TransferDataToWindow();
189 }
190 
191 
192 /**
193  * Updates the index choice when the register file selection is changed.
194  */
195 void
196 RFGuardDialog::onRFChoice(wxCommandEvent&) {
197 
198  int selection = indexChoice_->GetSelection();
199 
200  indexChoice_->Clear();
201 
202  const RegisterFile* rf = selectedRF();
203  for (int i = 0; i < rf->numberOfRegisters(); i++) {
204  wxString index = WxConversion::toWxString(i);
205  indexChoice_->Append(index);
206  }
207 
208  // add choice to create guard for every index
209  if (adding_) {
211  format fmtAll = prodeTexts->text(ProDeTextGenerator::TXT_ALL);
212  indexChoice_->Append(WxConversion::toWxString(fmtAll.str()));
213  }
214 
215  if (static_cast<int>(indexChoice_->GetCount()) >= selection) {
216  indexChoice_->SetSelection(selection);
217  } else {
218  indexChoice_->SetSelection(0);
219  }
220 }
221 
222 
223 /**
224  * Returns a pointer to the selected register file.
225  *
226  * @return Pointer to the selected refister file.
227  */
228 const RegisterFile*
230  string name = WxConversion::toString(nameChoice_->GetStringSelection());
232  bus_->machine()->registerFileNavigator();
233  RegisterFile* rf = navigator.item(name);
234  return rf;
235 }
236 
237 
238 /**
239  * Updates the guard object when the OK-button is pressed.
240  *
241  * Closes the dialog.
242  */
243 void
244 RFGuardDialog::onOK(wxCommandEvent&) {
245  TransferDataFromWindow();
246  try {
247  // get currently selected index and its string representation
248  int indexSelection = indexChoice_->GetSelection();
249  assert(indexSelection != wxNOT_FOUND);
250  wxString choiceText = indexChoice_->GetString(indexSelection);
251 
252  // get the string that resembles all indices
254  format fmtAll = prodeTexts->text(ProDeTextGenerator::TXT_ALL);
255  bool singleGuard = true;
256 
257  // check if user has selected all indices
258  if (choiceText.IsSameAs(WxConversion::toWxString(fmtAll.str()))) {
259  singleGuard = false;
260  }
261 
262  if (!adding_ || (adding_ && singleGuard)) {
263  // add / edit one guard
264  new RegisterGuard(newInverted_, *selectedRF(), newIndex_,
265  bus_);
266  } else {
267  // add new guard for every index
268 
269  string rfNameSel = WxConversion::toString(
270  nameChoice_->GetStringSelection());
271 
272  // loop all bus guards and mark old guards (if there is any)
273  // of the selected register file name to be deleted
274  std::vector<RegisterGuard*> toBeDeleted;
275  for (int i = 0; i < bus_->guardCount(); ++i) {
276  Guard* g = bus_->guard(i);
277  RegisterGuard* rfGuard =
278  dynamic_cast<RegisterGuard*>(g);
279 
280  if (rfGuard != NULL) {
281  string rfName = rfGuard->registerFile()->name();
282 
283  // if register file names match
284  if (rfName.compare(rfNameSel) == 0) {
285  // if inverted setting is same
286  if (rfGuard->isInverted() == newInverted_) {
287  toBeDeleted.push_back(rfGuard);
288  }
289  }
290  }
291  }
292 
293  // delete old guards
294  for (unsigned int i = 0; i < toBeDeleted.size(); ++i) {
295  if (toBeDeleted.at(i) != NULL) {
296  delete toBeDeleted.at(i);
297  toBeDeleted.at(i) = NULL;
298  }
299  }
300 
301  // create new guard for every file register's index
302  for (int index = 0; index < selectedRF()->numberOfRegisters();
303  ++index) {
304  new RegisterGuard(newInverted_, *selectedRF(), index,
305  bus_);
306  }
307  }
308  } catch (ComponentAlreadyExists& e) {
310  format message =
312  InformationDialog dialog(
313  this, WxConversion::toWxString(message.str()));
314  dialog.ShowModal();
315  return;
316  }
317 
318  EndModal(wxID_OK);
319 }
320 
321 
322 /**
323  * Cancels the dialog effects by creating the original register guard.
324  *
325  * Closes the dialog.
326  */
327 void
328 RFGuardDialog::onCancel(wxCommandEvent&) {
329  if (adding_ == false) {
330  new RegisterGuard(inverted_, *rf_, index_, bus_);
331  }
332  EndModal(wxID_CANCEL);
333 }
334 
335 
336 /**
337  * Creates contents of the dialog window.
338  *
339  * Code generated with wxDesigner.
340  *
341  * @param parent Parent dialog of the contents.
342  * @param call_fit If true, fits sizer in dialog window.
343  * @param set_sizer If true, sets sizer as dialog's sizer.
344  * @return Top level sizer of the contents.
345  */
346 wxSizer*
348  wxWindow *parent, bool call_fit, bool set_sizer) {
349 
350  wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
351 
352  wxGridSizer *item1 = new wxGridSizer( 2, 0, 0 );
353 
354  wxStaticText *item2 = new wxStaticText( parent, ID_LABEL_NAME, wxT("Register File Name:"), wxDefaultPosition, wxDefaultSize, 0 );
355  item1->Add( item2, 0, wxALL, 5 );
356 
357  wxString *strs3 = (wxString*) NULL;
358  wxChoice *item3 = new wxChoice( parent, ID_RF_NAME, wxDefaultPosition, wxSize(100,-1), 0, strs3, 0 );
359  item1->Add( item3, 0, wxGROW|wxALL, 5 );
360 
361  wxStaticText *item4 = new wxStaticText( parent, ID_LABEL_INDEX, wxT("Register Index:"), wxDefaultPosition, wxDefaultSize, 0 );
362  item1->Add( item4, 0, wxALL, 5 );
363 
364  wxString *strs5 = (wxString*) NULL;
365  wxChoice *item5 = new wxChoice( parent, ID_RF_INDEX, wxDefaultPosition, wxSize(100,-1), 0, strs5, 0 );
366  item1->Add( item5, 0, wxGROW|wxALL, 5 );
367 
368  item0->Add( item1, 0, wxGROW|wxALL, 5 );
369 
370  wxCheckBox *item6 = new wxCheckBox( parent, ID_INVERTED, wxT("Inverted"), wxDefaultPosition, wxDefaultSize, 0 );
371  item0->Add( item6, 0, wxALL, 5 );
372 
373  wxStaticLine *item7 = new wxStaticLine( parent, ID_LINE, wxDefaultPosition, wxSize(20,-1), wxLI_HORIZONTAL );
374  item0->Add( item7, 0, wxGROW|wxALL, 5 );
375 
376  wxBoxSizer *item8 = new wxBoxSizer( wxHORIZONTAL );
377 
378  wxButton *item9 = new wxButton( parent, ID_HELP, wxT("&Help"), wxDefaultPosition, wxDefaultSize, 0 );
379  item8->Add( item9, 0, wxALIGN_CENTER|wxALL, 5 );
380 
381  wxButton *item10 = new wxButton( parent, wxID_OK, wxT("&OK"), wxDefaultPosition, wxDefaultSize, 0 );
382  item8->Add( item10, 0, wxALIGN_CENTER|wxALL, 5 );
383 
384  wxButton *item11 = new wxButton( parent, wxID_CANCEL, wxT("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
385  item8->Add( item11, 0, wxALIGN_CENTER|wxALL, 5 );
386 
387  item0->Add( item8, 0, wxALIGN_CENTER|wxALL, 5 );
388 
389  if (set_sizer)
390  {
391  parent->SetSizer( item0 );
392  if (call_fit)
393  item0->SetSizeHints( parent );
394  }
395 
396  return item0;
397 }
TTAMachine::Guard
Definition: Guard.hh:55
ProDeTextGenerator::TXT_LABEL_RF_NAME
@ TXT_LABEL_RF_NAME
Label for register file name.
Definition: ProDeTextGenerator.hh:86
WxConversion::toWxString
static wxString toWxString(const std::string &source)
TTAMachine::Component::name
virtual TCEString name() const
Definition: MachinePart.cc:125
WidgetTools::setLabel
static void setLabel(Texts::TextGenerator *generator, wxWindow *widget, int textID)
Definition: WidgetTools.cc:92
TTAMachine::Bus
Definition: Bus.hh:53
GUITextGenerator::instance
static GUITextGenerator * instance()
Definition: GUITextGenerator.cc:67
RFGuardDialog::onCancel
void onCancel(wxCommandEvent &event)
Definition: RFGuardDialog.cc:328
RFGuardDialog::onOK
void onOK(wxCommandEvent &event)
Definition: RFGuardDialog.cc:244
GUITextGenerator
Definition: GUITextGenerator.hh:46
GUITextGenerator::TXT_BUTTON_HELP
@ TXT_BUTTON_HELP
Label for help button.
Definition: GUITextGenerator.hh:60
WidgetTools.hh
FindWindow
Definition: FindWindow.hh:49
TTAMachine::Machine::Navigator::count
int count() const
RFGuardDialog.hh
Texts::TextGenerator::text
virtual boost::format text(int textId)
Definition: TextGenerator.cc:94
ProDeTextGenerator.hh
ProDeTextGenerator
Definition: ProDeTextGenerator.hh:49
ProDeTextGenerator::MSG_ERROR_GUARD_EXISTS
@ MSG_ERROR_GUARD_EXISTS
Error: Equal guard exists.
Definition: ProDeTextGenerator.hh:242
TTAMachine::BaseRegisterFile::numberOfRegisters
virtual int numberOfRegisters() const
RFGuardDialog::TransferDataToWindow
virtual bool TransferDataToWindow()
Definition: RFGuardDialog.cc:156
assert
#define assert(condition)
Definition: Application.hh:86
RFGuardDialog::onRFChoice
void onRFChoice(wxCommandEvent &event)
Definition: RFGuardDialog.cc:196
RFGuardDialog::setTexts
void setTexts()
Definition: RFGuardDialog.cc:119
RFGuardDialog::~RFGuardDialog
virtual ~RFGuardDialog()
Definition: RFGuardDialog.cc:111
ProDeTextGenerator::TXT_ALL
@ TXT_ALL
Text 'all' in lower case.
Definition: ProDeTextGenerator.hh:132
GUITextGenerator::TXT_BUTTON_CANCEL
@ TXT_BUTTON_CANCEL
Label for cancel button.
Definition: GUITextGenerator.hh:55
ProDeTextGenerator::TXT_RF_GUARD_DIALOG_TITLE
@ TXT_RF_GUARD_DIALOG_TITLE
Register file guard dialog title.
Definition: ProDeTextGenerator.hh:199
TTAMachine::RegisterGuard
Definition: Guard.hh:137
InformationDialog.hh
Guard.hh
RFGuardDialog::createContents
wxSizer * createContents(wxWindow *parent, bool call_fit, bool set_sizer)
Definition: RFGuardDialog.cc:347
Machine.hh
GUITextGenerator.hh
RFGuardDialog::selectedRF
const TTAMachine::RegisterFile * selectedRF() const
Definition: RFGuardDialog.cc:229
ProDeTextGenerator::instance
static ProDeTextGenerator * instance()
Definition: ProDeTextGenerator.cc:382
EVT_BUTTON
EVT_BUTTON(ID_EDIT_ARCH_PORT, FUImplementationDialog::onEditArchitecturePort) EVT_BUTTON(ID_ADD_EXTERNAL_PORT
ProDeTextGenerator::TXT_LABEL_INVERTED
@ TXT_LABEL_INVERTED
Label for inverted checkbox.
Definition: ProDeTextGenerator.hh:85
TTAMachine::Guard::isInverted
virtual bool isInverted() const
false
find Finds info of the inner loops in the false
Definition: InnerLoopFinder.cc:81
RegisterFile.hh
ComponentAlreadyExists
Definition: Exception.hh:510
ProDeTextGenerator::TXT_LABEL_REGISTER_INDEX
@ TXT_LABEL_REGISTER_INDEX
Label for register index widget.
Definition: ProDeTextGenerator.hh:87
WxConversion.hh
TTAMachine::Machine::Navigator::item
ComponentType * item(int index) const
TTAMachine::RegisterFile
Definition: RegisterFile.hh:47
InformationDialog
Definition: InformationDialog.hh:42
TTAMachine
Definition: Assembler.hh:48
WxConversion::toString
static std::string toString(const wxString &source)
TTAMachine::RegisterGuard::registerFile
const RegisterFile * registerFile() const
RFGuardDialog
Definition: RFGuardDialog.hh:46
TTAMachine::Machine::Navigator
Definition: Machine.hh:186
END_EVENT_TABLE
END_EVENT_TABLE() using namespace IDF
GUITextGenerator::TXT_BUTTON_OK
@ TXT_BUTTON_OK
Label for OK button.
Definition: GUITextGenerator.hh:59