Debian Codegraph @

gcc/testsuite/gcc.target/x86_64/abi/ms-sysv/gen.cc

raw
1 /* Test program generator for 64-bit Microsoft ABI.
2 Copyright (C) 2016-2017 Free Software Foundation, Inc.
3 Contributed by Daniel Santos <[email protected]>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
20
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
25
26 #include <cstdio>
27 #include <cassert>
28 #include <vector>
29 #include <string>
30 #include <cstring>
31 #include <iostream>
32 #include <algorithm>
33 #include <ios>
34 #include <iomanip>
35 #include <sstream>
36 #include <fstream>
37 #include <memory>
38 #include <regex>
39 #include <stdexcept>
40
41 #include <unistd.h>
42 #include <getopt.h>
43
44 using namespace std;
45
46 /* A basic Effective C++ Item 6. */
47 class uncopyable
48 {
49 private:
50 uncopyable (const uncopyable &) = delete;
51 const uncopyable& operator= (const uncopyable &) = delete;
52
53 protected:
54 uncopyable() {}
55 ~uncopyable() {}
56 };
57
58 /* A simple class for adding text delimiters. */
59 class list_delimiter : protected uncopyable
60 {
61 int m_pos;
62 string m_delim;
63 static string s_empty;
64
65 list_delimiter ();
66
67 public:
68 list_delimiter (const char *delim, int init_pos = 0)
69 : m_pos (init_pos), m_delim(delim) {}
70 const string &get () {return m_pos++ ? m_delim : s_empty;}
71 void reset () {m_pos = 0;}
72 int get_pos () {return m_pos;}
73 };
74
75 string list_delimiter::s_empty = "";
76
77 /* Bitmasks for representing non-volatile retisters of an ms_abi call that
78 are not already clobbered by a sysv_abi call. */
79 enum optional_regs
80 {
81 OPTIONAL_REG_RBX = 0x01,
82 OPTIONAL_REG_RBP = 0x02,
83 OPTIONAL_REG_R12 = 0x04,
84 OPTIONAL_REG_R13 = 0x08,
85 OPTIONAL_REG_R14 = 0x10,
86 OPTIONAL_REG_R15 = 0x20,
87
88 OPTIONAL_REG_ALL = 0x3f,
89 OPTIONAL_REG_HFP_ALL = OPTIONAL_REG_ALL & (~OPTIONAL_REG_RBP)
90 };
91
92 static const char * const optional_regs_str[] = {
93 "rbx",
94 "rbp",
95 "r12",
96 "r13",
97 "r14",
98 "r15",
99 };
100
101 /* A simple type & name representation of a function parameter. */
102 class arg
103 {
104 string name;
105 string type;
106 bool type_is_integral:1;
107
108 public:
109 arg(const char *name, const char *type, bool type_is_integral);
110
111 bool is_type_integral () const {return type_is_integral;}
112 const string &get_name () const {return name;}
113 const string &get_type () const {return type;}
114 };
115
116 arg::arg(const char *name, const char *type, bool type_is_integral)
117 : name (name), type (type), type_is_integral (type_is_integral)
118 {
119 }
120
121 /* A stupid operator<< implementation for arg objects. */
122 template<class T> T &operator<< (T &out, const arg &a)
123 {
124 return out << a.get_type () << " " << a.get_name ();
125 }
126
127 /* Bitmask representation of all possible varients of a test function. The
128 value FN_VAR_MSABI is only used internally to distinguish between an
129 ms_abi and sysv_abi function. */
130 enum fn_variants {
131 FN_VAR_MSABI = 0x01,
132 FN_VAR_HFP = 0x02,
133 FN_VAR_REALIGN = 0x04,
134 FN_VAR_ALLOCA = 0x08,
135 FN_VAR_VARARGS = 0x10,
136 FN_VAR_SIBCALL = 0x20,
137 FN_VAR_SHRINK_WRAP = 0x40,
138
139 FN_VAR_HFP_OR_REALIGN = FN_VAR_HFP | FN_VAR_REALIGN,
140 FN_VAR_MASK = 0x7f,
141 FN_VAR_COUNT = 7
142 };
143
144 /* Representation of a Microsoft or System V ABI function with varying
145 parameters, quirks and optimization goals.
146
147 Function name nomenclature:
148 (msabi|sysv)_[xx_][r|f][a][v][s][w]<n>
149 | | | | | | | |
150 | | | | | | | Number of extra (long) parameters
151 | | | | | | shrink wrap
152 | | | | | sibling call
153 | | | | varargs
154 | | | alloca
155 | | Forced realignment or hard frame pointer
156 | Explicit clobbers (hexidecimal mask, ms_abi only)
157 Calling Convention */
158 class fn : protected uncopyable
159 {
160 private:
161 const vector<arg> &m_args;
162 string m_name;
163 string m_attr_decl_str;
164 string m_attr_def_str;
165 int m_clobbers:FN_VAR_COUNT;
166 int m_var;
167
168 public:
169 fn (const vector<arg> &args, int clobbers, int var);
170
171 void print_params (ostream &out) const;
172 void print_decl (ostream &out, bool for_def = false) const;
173 void print_noinfo_def (ostream &out) const;
174 void print_def (ostream &out) const;
175 const string &get_name () const {return m_name;}
176 const vector<arg> &get_args () const {return m_args;}
177
178 bool get_hfp_or_realign () const {return m_var & FN_VAR_HFP_OR_REALIGN;}
179 bool get_msabi () const {return m_var & FN_VAR_MSABI;}
180 bool get_hfp () const {return m_var & FN_VAR_HFP;}
181 bool get_realign () const {return m_var & FN_VAR_REALIGN;}
182 bool get_alloca () const {return m_var & FN_VAR_ALLOCA;}
183 bool get_varargs () const {return m_var & FN_VAR_VARARGS;}
184 bool get_sibcall () const {return m_var & FN_VAR_SIBCALL;}
185 bool get_shrink_wrap () const {return m_var & FN_VAR_SHRINK_WRAP;}
186 };
187
188 fn::fn (const vector<arg> &args, int clobbers, int var)
189 : m_args (args)
190 , m_name ()
191 , m_attr_decl_str ()
192 , m_attr_def_str ("noinline")
193 , m_clobbers (clobbers)
194 , m_var (var)
195 {
196 assert (!(var & ~FN_VAR_MASK));
197
198 if (get_hfp () && get_realign ())
199 throw invalid_argument ("`hfp' with `realign' does nothing.");
200
201 if (get_varargs () && args.empty ())
202 throw invalid_argument ("Need at least one normal argument to use varargs");
203
204 assert (!(get_hfp () || get_realign ()) || !(clobbers & OPTIONAL_REG_RBP));
205
206 stringstream name;
207 name << (get_msabi () ? "msabi_" : "sysv_");
208 if (get_msabi ())
209 name << setfill('0') << setw(2) << hex << m_clobbers << "_";
210 name << (get_realign () ? "r" : (get_hfp () ? "f" : ""))
211 << (get_alloca () ? "a" : "")
212 << (get_varargs () ? "v" : "")
213 << (get_sibcall () ? "s" : "")
214 << (get_shrink_wrap () ? "w" : "")
215 << setw(0) << dec << (unsigned)args.size();
216 m_name = name.str();
217
218 list_delimiter decl_comma (", ", !m_attr_decl_str.empty ());
219 list_delimiter def_comma (", ", !m_attr_def_str.empty ());
220 if (get_msabi ())
221 {
222 m_attr_decl_str += decl_comma.get ();
223 m_attr_decl_str += "ms_abi";
224 m_attr_def_str += def_comma.get ();
225 m_attr_def_str += "ms_abi";
226 }
227
228 if (get_realign ())
229 {
230 m_attr_def_str += def_comma.get();
231 m_attr_def_str += "__force_align_arg_pointer__";
232 }
233 else if (get_hfp ())
234 {
235 m_attr_def_str += def_comma.get();
236 m_attr_def_str += "optimize (\"no-omit-frame-pointer\")";
237 }
238 }
239
240 /* Print the parameters for a function declaration. */
241 void fn::print_params (ostream &out) const
242 {
243 list_delimiter comma (", ");
244
245 vector<arg>::const_iterator i;
246 if (get_alloca () && !get_msabi ())
247 out << comma.get () << "void *alloca_mem";
248 for (i = m_args.begin(); i != m_args.end(); ++i)
249 out << comma.get () << *i;
250
251 if (get_varargs ())
252 out << comma.get () << (get_msabi () ? "..." : "va_list argptr");
253 }
254
255 /* Print the declaration for a function. */
256 void fn::print_decl (ostream &out, bool for_def) const
257 {
258 const string &attr_str = (for_def ? m_attr_def_str : m_attr_decl_str);
259 if (!for_def)
260 out << "extern ";
261
262 if (!attr_str.empty ())
263 out << "__attribute__ ((" << attr_str << ")) ";
264
265 out << "long " << m_name << " (";
266 print_params (out);
267 out << ")";
268 if (!for_def)
269 out << ";" << endl;
270 }
271
272 /* Output a volatile "_noinfo" function pointer definition. */
273 void fn::print_noinfo_def (ostream &out) const
274 {
275 out << "static ";
276 if (!m_attr_decl_str.empty ())
277 out << "__attribute__ ((" << m_attr_decl_str << ")) ";
278 out << "long (*const volatile " << m_name << "_noinfo) (";
279 print_params (out);
280 out << ") = " << m_name << ";" << endl;
281 }
282
283 /* Print the definition of a function. */
284 void fn::print_def (ostream &out) const
285 {
286 vector<arg>::const_iterator i;
287
288 print_decl (out, true);
289 out << endl << "{" << endl;
290
291 if (get_msabi () && get_alloca ())
292 {
293 const char *size_str = m_args.empty () ? "42" : "a";
294 out << " void *alloca_mem = __builtin_alloca (8 + " << size_str << ");" << endl
295 << " *(long*)alloca_mem = FLAG_ALLOCA;" << endl;
296 }
297 if (get_msabi () && get_varargs ())
298 out << " va_list argptr;" << endl;
299 if (get_shrink_wrap ())
300 out << " if (shrink_wrap_global == FLAG_SHRINK_WRAP_FAST_PATH)" << endl
301 << " return FLAG_SHRINK_WRAP_FAST_PATH;" << endl;
302
303 list_delimiter comma (", ");
304 if (m_clobbers)
305 {
306 out << " __asm__ __volatile__ (\"\" :::";
307 unsigned c;
308 unsigned mask = m_clobbers;
309 comma.reset ();
310 for (c = 0, mask = m_clobbers; mask; ++c, mask >>= 1)
311 if (mask & 1)
312 out << comma.get () << "\"" << optional_regs_str[c] << "\"";
313 out << ");" << endl;
314 }
315
316 if (get_msabi () && get_varargs ())
317 {
318 assert (!m_args.empty ());
319 out << " va_start(argptr, " << m_args.back ().get_name () << ");" << endl;
320 }
321
322 out << " return ";
323 if (get_msabi ())
324 {
325 if (get_sibcall ())
326 out << "do_sibcall_noinfo (";
327
328 comma.reset ();
329 out << "sysv_"
330 << (get_alloca () ? "a" : "")
331 << (get_varargs () ? "v" : "")
332 << m_args.size ()
333 << "_noinfo (";
334
335 if (get_alloca ())
336 out << comma.get () << "alloca_mem";
337 for (i = m_args.begin(); i != m_args.end(); ++i)
338 out << comma.get () << i->get_name ();
339 if (get_varargs ())
340 out << comma.get () << "argptr";
341 out << ")";
342 if (get_shrink_wrap ())
343 out << " + FLAG_SHRINK_WRAP_SLOW_PATH";
344 if (get_sibcall ())
345 out << ")";
346 }
347 else
348 {
349 list_delimiter plus (" + ");
350 for (i = m_args.begin(); i != m_args.end(); ++i)
351 if (i->is_type_integral ())
352 out << plus.get () << i->get_name ();
353 if (get_alloca ())
354 out << plus.get () << "*(long*)alloca_mem";
355 if (!plus.get_pos ())
356 out << "0";
357 }
358 out << ";" << endl;
359 if (get_msabi () && get_varargs ())
360 out << " va_end(argptr);" << endl;
361 out << "}" << endl << endl;
362 }
363
364 /* Global variables. */
365 string argv0;
366 string out_file_name;
367 unsigned int extra_params_min = 0;
368 unsigned int extra_params_max = 5;
369 unsigned fn_variant_mask = FN_VAR_MASK;
370 bool omit_rbp_clobbers = false;
371 vector<class fn*> sysv_funcs;
372 vector<class fn*> msabi_funcs;
373
374
375 /* Emit extern for do_test_aligned and do_test_unaligned (defined in do_test.S)
376 followed by all of the various do_test* function function pointers that
377 are just aliases of them. */
378 static void make_do_tests_decl (const vector<class arg> &args, ostream &out)
379 {
380 vector<class arg>::const_iterator ai;
381 unsigned i, varargs, unaligned;
382
383 out << "extern __attribute__ ((ms_abi)) long do_test_aligned ();" << endl
384 << "extern __attribute__ ((ms_abi)) long do_test_unaligned ();" << endl;
385
386 list_delimiter comma (", ");
387 for (i = extra_params_min; i <= args.size (); ++i)
388 for (unaligned = 0; unaligned <= 1; ++unaligned)
389 for (varargs = 0; varargs <= 1; ++varargs)
390 {
391 if (!i && varargs) /* skip varargs version when no other args */
392 continue;
393
394 comma.reset ();
395 out << "static __attribute__ ((ms_abi)) long (*do_test_"
396 << (unaligned ? "u" : "")
397 << (varargs ? "v" : "") << i << ") (";
398
399 unsigned j;
400 for (j = 0, ai = args.begin (); j < i; ++j, ++ai)
401 out << comma.get () << ai->get_type () << " "
402 << ai->get_name ();
403 if (varargs)
404 out << comma.get () << "...";
405 out << ") = (void*)do_test_" << (unaligned ? "un" : "")
406 << "aligned;" << endl;
407 }
408 }
409
410 /* Generate do_tests function. We actually break it up into multiple
411 do_test_xxxx functions to keep compile times down (with just one large
412 function, it is a very slow build). */
413 void make_do_test (const vector<class arg> &args,
414 const vector<class fn*> &msabi_funcs,
415 ostream &out)
416 {
417 const unsigned TESTS_PER_FN_MAX = 64;
418 unsigned i;
419 vector<string> do_tests_fn_names;
420 unsigned fn_count = 0;
421 unsigned test_count = TESTS_PER_FN_MAX;
422 string params_str;
423 string param_names_str;
424 string param_types_str;
425
426 /* Init some commonly used strings. */
427 {
428 stringstream s1, s2, s3;
429 list_delimiter comma(", ");
430 for (auto arg : args)
431 {
432 const string &c = comma.get ();
433 s1 << c << arg;
434 s2 << c << arg.get_name ();
435 s3 << c << arg.get_type ();
436 }
437 params_str = s1.str ();
438 param_names_str = s2.str ();
439 param_types_str = s3.str ();
440 }
441
442 vector<class fn*>::const_iterator fi;
443 for (fi = msabi_funcs.begin(); fi != msabi_funcs.end(); ++fi)
444 {
445 const fn &f = **fi;
446 unsigned unaligned, shrink_wrap;
447
448 for (unaligned = 0; unaligned <= !!f.get_realign (); ++unaligned)
449 for (shrink_wrap = 0; shrink_wrap <= !!f.get_shrink_wrap ();
450 ++shrink_wrap)
451 {
452 const vector<class arg> &fargs = f.get_args ();
453
454 /* To prevent unwieldy build times, we split up tests to 64-ish per
455 function. */
456 if (++test_count > TESTS_PER_FN_MAX)
457 {
458 test_count = 1;
459 if (fn_count > 0) {
460 out << "}" << endl << endl;
461 }
462
463 stringstream fn_name;
464 fn_name << "do_tests_" << setfill('0') << setw(4) << hex
465 << fn_count++;
466 do_tests_fn_names.push_back (fn_name.str ());
467
468 out << "static __attribute__((noinline)) void "
469 << fn_name.str () << " (" << params_str << ")" << endl
470 << "{" << endl
471 << " long ret;" << endl;
472 }
473
474 /* Call init_test. */
475 out << endl
476 << " init_test (" << f.get_name () << ", \""
477 << f.get_name () << "\", ";
478
479 if (f.get_realign ())
480 out << (unaligned ? "ALIGNMENT_MISALIGNED"
481 : "ALIGNMENT_ALIGNED");
482 else
483 out << "ALIGNMENT_NOT_TESTED";
484
485 out << ", ";
486 if (f.get_shrink_wrap ())
487 out << (shrink_wrap ? "SHRINK_WRAP_SLOW_PATH"
488 : "SHRINK_WRAP_FAST_PATH");
489 else
490 out << "SHRINK_WRAP_NONE";
491 out << ", ";
492
493 /* Calculated the expected return value. */
494 if (f.get_shrink_wrap () && shrink_wrap == 0)
495 out << "FLAG_SHRINK_WRAP_FAST_PATH";
496 else
497 {
498 list_delimiter plus (" + ");
499 for (auto const &arg : fargs)
500 out << plus.get () << arg.get_name ();
501 if (f.get_sibcall ())
502 out << plus.get () << "FLAG_SIBCALL";
503 if (f.get_alloca ())
504 out << plus.get () << "FLAG_ALLOCA";
505 if (f.get_shrink_wrap () && shrink_wrap == 1)
506 out << plus.get () << "FLAG_SHRINK_WRAP_SLOW_PATH";
507 if (!plus.get_pos ())
508 out << "0";
509 }
510 out << ");" << endl;
511 /* End if init_test call. */
512
513 if (f.get_realign () && unaligned == 1)
514 out << " __asm__ __volatile__ (\"subq $8,%%rsp\":::\"cc\");"
515 << endl;
516
517 out << " ret = do_test_"
518 << (f.get_realign () && unaligned == 1 ? "u" : "")
519 << (f.get_varargs () ? "v" : "")
520 << fargs.size () << " (";
521
522 list_delimiter comma (", ");
523 for (auto const &arg : fargs)
524 out << comma.get () << arg.get_name ();
525 out << ");" << endl;
526
527 if (f.get_realign () && unaligned == 1)
528 out << " __asm__ __volatile__ (\"addq $8,%%rsp\":::\"cc\");"
529 << endl;
530
531 out << " check_results (ret);" << endl;
532 }
533 }
534
535 /* Close the last function and define the main do_tests function. */
536 out << "}" << endl << endl;
537
538 /* Define _noinfo pointers to each do_tests_* function. */
539 for (auto const &fn_name : do_tests_fn_names)
540 out << " static void (*volatile " << fn_name << "_noinfo) ("
541 << param_types_str << ") = " << fn_name << ";" << endl;
542
543 /* Define main do_tests () function. */
544 out << endl
545 << "void do_tests ()" << endl
546 << "{" << endl;
547 i = 1;
548 for (auto const &arg : args)
549 {
550 out << " " << arg.get_type () << " " << arg.get_name () << " = " << i
551 << ";" << endl;
552 i <<= 1;
553 }
554 out << endl;
555
556 /* Call do_tests_*_noinfo functions. */
557 for (auto const &fn_name : do_tests_fn_names)
558 out << " " << fn_name << "_noinfo (" << param_names_str << ");" << endl;
559 out << "}" << endl << endl;
560 }
561
562 /* Generate output file. */
563 void generate_header (const string &args)
564 {
565 vector<class arg> all_args;
566 vector<vector<class arg> > arg_sets;
567
568 ofstream out;
569 out.exceptions (ios::failbit | ios::badbit);
570 out.open (out_file_name);
571 out << "/* Generated with " << args << " */" << endl << endl;
572
573 assert (extra_params_max < 26);
574
575 /* Build the extra argument array. */
576 for (unsigned int i = 0; i < extra_params_max; ++i)
577 {
578 char name[2] = "a";
579 name[0] += i;
580 class arg myarg (name, "long", true);
581
582 all_args.push_back (myarg);
583 }
584
585 arg_sets.resize (extra_params_max - extra_params_min + 1);
586 for (unsigned int i = 0; i < arg_sets.size (); ++i)
587 arg_sets[i].insert (arg_sets[i].end(), all_args.begin(),
588 all_args.begin () + i + extra_params_min);
589
590 /* Print sysv functions */
591 for (const vector<class arg> &as : arg_sets)
592 {
593 const int alloca_max = !!(fn_variant_mask & FN_VAR_MSABI);
594 const int varargs_max = !!(fn_variant_mask & FN_VAR_VARARGS);
595 fn *fn;
596 for (int _alloca = 0; _alloca <= alloca_max; ++_alloca)
597 for (int varargs = 0; varargs <= varargs_max; ++varargs)
598 {
599 try {
600 int var = (_alloca ? FN_VAR_ALLOCA : 0)
601 | (varargs ? FN_VAR_VARARGS : 0);
602 fn = new ::fn (as, 0, var);
603 } catch (invalid_argument) {
604 continue;
605 }
606 sysv_funcs.push_back (fn);
607 fn->print_def (out);
608 }
609 }
610
611 /* Print _noinfo function pointers for sysv functions. */
612 for (const fn *f : sysv_funcs)
613 f->print_noinfo_def (out);
614
615 /* Print ms_abi functions. */
616 unsigned int var;
617 for (var = 0; var <= FN_VAR_MASK; ++var)
618 {
619 /* We only want ms_abi fns for this. */
620 if (! (var & FN_VAR_MSABI))
621 continue;
622
623 /* */
624 if ((var & fn_variant_mask) != var)
625 continue;
626
627 unsigned clobbers;
628 for (clobbers = 0; clobbers <= OPTIONAL_REG_ALL; ++clobbers)
629 {
630 /* Skip clobbers that would be invalid. */
631 if (clobbers & OPTIONAL_REG_RBP)
632 {
633 /* Whole program built with hard frame pointer. */
634 if (omit_rbp_clobbers)
635 continue;
636
637 /* Uses BP explicitly. */
638 if (var & FN_VAR_HFP_OR_REALIGN)
639 continue;
640
641 /* Alloca seems to require DRAP, which uses BP. */
642 if (var & FN_VAR_ALLOCA)
643 continue;
644 }
645
646 for (auto const &as : arg_sets)
647 {
648 fn *fn;
649 try {
650 fn = new ::fn (as, clobbers, var);
651 } catch (invalid_argument) {
652 continue;
653 }
654
655 msabi_funcs.push_back (fn);
656 fn->print_def (out);
657 }
658 }
659 }
660
661 out << endl;
662 make_do_tests_decl (all_args, out);
663 out << endl;
664
665 make_do_test (all_args, msabi_funcs, out);
666 out.close ();
667 }
668
669 /* Parse a string into a long and return true upon success. */
670 static bool long_optarg (const char *optarg, long &dest)
671 {
672 char *end;
673
674 errno = 0;
675 dest = strtol(optarg, &end, 0);
676 if (errno)
677 cerr << strerror(errno) << endl;
678
679 while (isspace(*end))
680 ++end;
681
682 /* Error if errno non-zero or junk at end of string. */
683 return errno || *end;
684 }
685
686 void usage ()
687 {
688 cerr
689 << "Usage: " << argv0 << " [options] <output_file>" << endl
690 << endl
691 << " -p <n|n-n>, --max-extra-params <expr>" << endl
692 << " A single or range of extra parameters" << endl
693 << " Examples:" << endl
694 << " -p0-5" << endl
695 << " -p12" << endl
696 << endl
697 << " -v <n>, --variant-mask <n>" << endl
698 << " Set mask of test variants (see enum fn_variants for values," << endl
699 << " defaults to 0x" << hex << FN_VAR_MASK << " [FN_VAR_MASK])" << endl
700 << endl
701 << " -0, --omit-rbp-clobbers" << endl
702 << " Omit tests that clobber RBP." << endl;
703 exit (-1);
704 }
705
706 /* Parse string representing a number range or a list of numbers. */
707 void set_extra_param_counts (const char *str)
708 {
709 char copy[0x40];
710 char *max_str;
711 bool bad = false;
712 long int min, max;
713
714 strncpy (copy, str, sizeof (copy) - 1);
715 max_str = strchr(copy, '-');
716 if (max_str)
717 *max_str++ = 0;
718
719 bad = long_optarg (copy, min);
720 if (max_str)
721 bad = bad || long_optarg (max_str, max);
722 else
723 max = min;
724
725 if (min > max)
726 usage ();
727
728 extra_params_min = min;
729 extra_params_max = max;
730 }
731
732 int main (int argc, char *argv[])
733 {
734 argv0 = argv[0];
735 const char *short_options = "p:v:0";
736 const struct option long_options[] = {
737 {"extra-params", required_argument, 0, 'p'},
738 {"variant-mask", required_argument, 0, 'v'},
739 {"omit-rbp-clobbers", no_argument, 0, '0'},
740 {"help", no_argument, 0, 'h'},
741 {0, 0, 0, 0},
742 };
743
744 int option_index = 0;
745 int c;
746 while ((c = getopt_long (argc, argv, short_options, long_options,
747 &option_index)) != -1)
748 {
749 switch (c)
750 {
751 long l;
752
753 case 'p':
754 set_extra_param_counts (optarg);
755 break;
756
757 case 'v':
758 if (long_optarg (optarg, l) || (l & ~FN_VAR_MASK))
759 {
760 cerr << "ERROR: Bad value for -v: `" << optarg << "`" << endl;
761 usage ();
762 }
763 fn_variant_mask = (unsigned)l;
764 break;
765
766 case '0':
767 omit_rbp_clobbers = true;
768 break;
769
770 case 'h':
771 default:
772 usage ();
773 }
774 }
775
776 if (argc - optind != 1)
777 usage ();
778 out_file_name = argv[optind];
779
780 /* Can't skip msabi funcions. */
781 fn_variant_mask |= FN_VAR_MSABI;
782
783 /* If whole program has HFP, explicit tests that enable it are redundant. */
784 if (omit_rbp_clobbers)
785 fn_variant_mask &= ~FN_VAR_HFP;
786
787 stringstream argv_str;
788
789 for (int i = 0; i < argc; ++i)
790 argv_str << (i ? " " : "") << argv[i];
791
792 int ret = 0;
793 try
794 {
795 generate_header (argv_str.str());
796 }
797 catch (exception &e)
798 {
799 cerr << "ERROR: While writing `" << out_file_name << "': "
800 << strerror(errno) << endl;
801 ret = 1;
802 }
803 for_each (sysv_funcs.begin (), sysv_funcs.end (), default_delete<fn> ());
804 for_each (msabi_funcs.begin (), msabi_funcs.end (), default_delete<fn> ());
805
806 return ret;
807 }