task_register.cc 8.79 KB
Newer Older
Benoit Perrot's avatar
Benoit Perrot committed
1
//
Benoit Perrot's avatar
Benoit Perrot committed
2
// This file is part of Nolimips, a MIPS simulator with unlimited registers
3
// Copyright (C) 2003, 2004, 2006 Benoit Perrot <benoit@lrde.epita.fr>
Benoit Perrot's avatar
Benoit Perrot committed
4
//
Benoit Perrot's avatar
Benoit Perrot committed
5
// Nolimips is free software; you can redistribute it and/or modify
Benoit Perrot's avatar
Benoit Perrot committed
6
7
8
9
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
Benoit Perrot's avatar
Benoit Perrot committed
10
// Nolimips is distributed in the hope that it will be useful,
Benoit Perrot's avatar
Benoit Perrot committed
11
12
13
14
15
16
17
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

#include "config.h"
#include "common.hh"

#include "misc/contract.hh"
#include "misc/has.hh"

#include "task/task.hh"
#include "task/task_register.hh"

namespace task
{

33
34
35
36
37
38
39
  TaskRegister &
  TaskRegister::instance()
  {
    static TaskRegister     unique;
    return unique;
  }

Benoit Perrot's avatar
Benoit Perrot committed
40
41
42
43
  // --------------------------------------------------------------------------
  // Task Register Engine
  // --------------------------------------------------------------------------

44
45
46
47
48
49
50
  // Register a task.
  void
  TaskRegister::register_task (const Task& task)
  {
    std::string		long_opt = "--" + task.long_opt();
    std::string		short_opt = "-" + task.short_opt();

Benoit Perrot's avatar
Benoit Perrot committed
51
52
    assertion(! has_key(tasks_, long_opt));
    tasks_[long_opt] = &task;
53
54
    if (short_opt.size () > 1)
      {
Benoit Perrot's avatar
Benoit Perrot committed
55
56
	assertion(! has_key(tasks_, short_opt));
	tasks_[short_opt] = &task;
57
      }
Benoit Perrot's avatar
Benoit Perrot committed
58
    modules_[task.module()].push_back(&task);
59
60
61
62
63
64
  }
  
  // Enable a task
  void
  TaskRegister::enable_task (const Task& task)
  {
Benoit Perrot's avatar
Benoit Perrot committed
65
    enabled_tasks_.push_back(&task);
66
67
  }
  void
Benoit Perrot's avatar
Benoit Perrot committed
68
  TaskRegister::enable_task (std::string task_name)
69
  {
Benoit Perrot's avatar
Benoit Perrot committed
70
71
72
73
74
75
76
77
    assertion(task_name.size() > 0);
    if (task_name[0] != '-')
      {
	if (task_name.size() > 1)
	  task_name = "--" + task_name;
	else
	  task_name =  "-" + task_name;
      }
Benoit Perrot's avatar
Benoit Perrot committed
78
79
    assertion(has_key(tasks_, task_name));
    enabled_tasks_.push_back(tasks_[task_name]);    
80
81
82
83
84
85
86
87
88
89
90
91
92
  }


  // Resolve dependencies by a simple topological sort
  void
  TaskRegister::resolve_dependencies(const Task* task,
				     std::set<Task const*>& visited_tasks,
				     std::list<Task const*>& sorted_tasks)
  {
    if (has(visited_tasks, task))
      return;
    visited_tasks.insert(task);
    
Benoit Perrot's avatar
Benoit Perrot committed
93
    for (Task::deps_type::const_iterator it = task->dependencies ().begin ();
94
95
96
	 it != task->dependencies ().end (); ++it)
      {
	std::string name = "--" + (*it);
Benoit Perrot's avatar
Benoit Perrot committed
97
98
	assertion(has_key(tasks_, name));
	const Task* dependency = tasks_[name];
99
100
101
102
103
104
105
106
107
108
	resolve_dependencies(dependency, visited_tasks, sorted_tasks);
      }
    sorted_tasks.push_back (task);
  }
  void
  TaskRegister::resolve_dependencies()
  {
    std::list<Task const*>	sorted_tasks;
    std::set<Task const*>	visited_tasks;
    for (std::list<Task const*>::const_iterator
Benoit Perrot's avatar
Benoit Perrot committed
109
	   it = enabled_tasks_.begin (); it != enabled_tasks_.end (); ++it)
110
      resolve_dependencies ((*it), visited_tasks, sorted_tasks);
Benoit Perrot's avatar
Benoit Perrot committed
111
    enabled_tasks_ = sorted_tasks;
112
113
114
  }


Benoit Perrot's avatar
Benoit Perrot committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  // Find a task given its name
  TaskRegister::const_task_iterator
  TaskRegister::find_task(const std::string &option)
  {
    TaskRegister::const_task_iterator it = tasks_.lower_bound(option);

    if (it == tasks_.end() || it->first.find(option) != 0)
      {
	std::cerr << program_name << ": unrecognized option `"
		  << option << "'" << std::endl;
	it = tasks_.end();
      }
    else
      {
	TaskRegister::const_task_iterator next(it);
	++next;
	if (next != tasks_.end() && next->first.find(option) == 0)
	  {
	    std::cerr << program_name << ": ambiguous option `"
		      << option << "'" << std::endl;
	    it = tasks_.end();
	  }
      }

    return it;
  }

Benoit Perrot's avatar
Benoit Perrot committed
142
143
144
145
  // Parse arguments
  char*
  TaskRegister::parse_args (int argc, char *argv[])
  {
146
    char *res = 0;
Benoit Perrot's avatar
Benoit Perrot committed
147

Benoit Perrot's avatar
Benoit Perrot committed
148
149
    for (int i = 1; i < argc; ++i)
      {
Benoit Perrot's avatar
Benoit Perrot committed
150
	std::string arg(argv[i]);
Benoit Perrot's avatar
Benoit Perrot committed
151
152
	
	if ((1 < arg.size ()) && (arg[0] == '-'))
Benoit Perrot's avatar
Benoit Perrot committed
153
	  {
154
	    std::string *value = 0;
Benoit Perrot's avatar
Benoit Perrot committed
155
156
157
158

	    // Identify option
	    TaskRegister::const_task_iterator it(tasks_.end());
	    if (arg[1] == '-') // Long option
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
	      {
		// Split on `=' for value
		unsigned eq = arg.find('=');
		if (eq < arg.size())
		  {
		    // +1: do not include `=' in value
		    value = new std::string(arg, eq + 1);
		    arg.resize(eq);
		  }

		// Search for long option
		it = find_task(arg);
	      }
	    else // Short option(s)
	      {
		for (unsigned l = 1; l < arg.size() && !value; ++l)
		  {
		    std::string short_option("-");
		    short_option += arg[l];

		    // Search for short option
		    it = find_task(short_option);
		    
		    // Let last task fall through the remaining of the
		    // routine
		    if (l + 1 < arg.size())
		      if (it != tasks_.end())
			{
			  if (it->second->needs_value())
			    // Split here for value
			    value = new std::string(arg, l + 1);
			  else
			    // Enable task
			    enable_task(*(it->second));
			}
		  }
	      }
	    
	    // Enable (long or last short) task and set value if needed
Benoit Perrot's avatar
Benoit Perrot committed
198
199
200
	    if (it != tasks_.end())
	      {
		const Task *task = it->second;
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
		
		if (task->needs_value())
		  {
		    if (!value)
		      {
			++i;
			if (i < argc)
			  value = new std::string(argv[i]);
		      }

		    if (!value)
		      std::cerr
			<< program_name
			<< ": option `" << task->long_opt()
			<< "' takes a value" << std::endl;
		    else
		      {
			task->set_value(*value);
			enable_task(*task);
		      }
		  }
		else
		  {
		    if (value)
		      std::cerr
			<< program_name
			<< ": option `" << task->long_opt()
			<< "' does not take a value" << std::endl;
		    else
		      enable_task(*task);
		  }
Benoit Perrot's avatar
Benoit Perrot committed
232
	      }
233
	    else if (value)
Benoit Perrot's avatar
Benoit Perrot committed
234
	      std::cerr 
235
236
237
238
		<< program_name
		<< ": `"<< *value << "': unexpected value" << std::endl;

	    delete value;
Benoit Perrot's avatar
Benoit Perrot committed
239
240
241
242
	  }
	else
	  res = argv[i];
      }
Benoit Perrot's avatar
Benoit Perrot committed
243

Benoit Perrot's avatar
Benoit Perrot committed
244
245
246
247
248
249
250
251
252
253
    return res;
  }


  // Execute tasks, checking dependencies.
  void
  TaskRegister::execute (void)
  {
    resolve_dependencies();
    for (std::list<Task const *>::const_iterator
Benoit Perrot's avatar
Benoit Perrot committed
254
	   it = enabled_tasks_.begin (); it != enabled_tasks_.end (); ++it)
Benoit Perrot's avatar
Benoit Perrot committed
255
256
257
258
259
260
261
262
      (*it)->execute ();
  }


  // --------------------------------------------------------------------------
  // Task Register Options
  // --------------------------------------------------------------------------

263
264
265
266
267
268
269
270
  // Display version
  void
  TaskRegister::version(std::ostream& ostr)
  {
    ostr
      << PACKAGE" ("PACKAGE_NAME") "PACKAGE_VERSION << std::endl
      << "Written by Benoit Perrot." << std::endl
      << std::endl
Benoit Perrot's avatar
Benoit Perrot committed
271
      << "Copyright (C) 2003, 2004 Benoit Perrot." << std::endl
272
273
274
275
276
277
      << PACKAGE" comes with ABSOLUTELY NO WARRANTY." << std::endl
      << "This is free software, and you are welcome to redistribute and "
      << "modify it " << std::endl
      << "under certain conditions; see source for details."
      << std::endl << std::endl;
  }
Benoit Perrot's avatar
Benoit Perrot committed
278

279
280
281
282
283
284
  // Display usage
  void
  TaskRegister::usage(std::ostream& ostr)
  {
    ostr << "Usage: " << program_name << " [-";
    for (std::map<std::string, Task const*>::const_iterator
Benoit Perrot's avatar
Benoit Perrot committed
285
	   it = tasks_.begin(); it != tasks_.end(); ++it)
286
287
288
      if ((*it).first.size() == 2)
	ostr << (*it).second->short_opt();
    ostr << "] ";
Benoit Perrot's avatar
Benoit Perrot committed
289
290

    unsigned len = 80;
Benoit Perrot's avatar
Benoit Perrot committed
291
    const char tab[] = "        ";
Benoit Perrot's avatar
Benoit Perrot committed
292
    for (std::map<std::string, Task const*>::const_iterator
Benoit Perrot's avatar
Benoit Perrot committed
293
	   it = tasks_.begin(); it != tasks_.end(); ++it)
Benoit Perrot's avatar
Benoit Perrot committed
294
295
296
297
298
299
300
      if ((*it).first.size () > 2)
	{
	  if (len + (*it).first.size () + 3 >= 80)
	    {
	      ostr << std::endl << tab;
	      len = sizeof (tab);
	    }
Benoit Perrot's avatar
Benoit Perrot committed
301
	  ostr << "[" << (*it).first << "] ";
Benoit Perrot's avatar
Benoit Perrot committed
302
303
	  len += (*it).first.size () + 3;
	}
Benoit Perrot's avatar
Benoit Perrot committed
304
    ostr << std::endl;
305
  }
Benoit Perrot's avatar
Benoit Perrot committed
306

307
308
309
310
311
  // Display help
  void
  TaskRegister::help(std::ostream& ostr)
  {
    for (std::map<std::string, std::list<Task const*> >::const_iterator
Benoit Perrot's avatar
Benoit Perrot committed
312
	   it = modules_.begin(); it != modules_.end(); ++it)
313
314
315
316
317
318
319
320
321
322
323
324
      {
	if ((*it).first.size () > 0)
	  ostr << " " << (*it).first << std::endl;
	for (std::list<Task const*>::const_iterator
	       jt = (*it).second.begin(); jt != (*it).second.end(); ++jt)
	  {
	    ostr << "  ";
	    if ((*jt)->short_opt ().size() > 0)
	      ostr << "-" << (*jt)->short_opt() << ", ";
	    else
	      ostr << "    ";
	    ostr << "--" << (*jt)->long_opt();
Benoit Perrot's avatar
Benoit Perrot committed
325
326
 	    if ((*jt)->needs_value())
 	      ostr << "=ARG";
327
328
329
330
331
332
333
334
	    if ((*jt)->long_opt().size () <= 8)
	      ostr << '\t';
	    ostr << '\t' << (*jt)->description() << std::endl;
	  }
	ostr << std::endl;
      }
    ostr << "Reports bugs to "PACKAGE_BUGREPORT << std::endl;
  }
Benoit Perrot's avatar
Benoit Perrot committed
335

336
337
338
339
340
341
  // Display tasks selection
  void
  TaskRegister::selection(std::ostream& ostr)
  {
    ostr << "Sorted tasks selection:" << std::endl;
    for (std::list<Task const*>::const_iterator
Benoit Perrot's avatar
Benoit Perrot committed
342
	   it = enabled_tasks_.begin (); it != enabled_tasks_.end (); ++it)
343
344
345
346
      ostr << "\t* " << (*it)->long_opt() << std::endl;
  }

} // namespace task