1divert(-1)
2dnl
3dnl  m4 macros for gmp assembly code, shared by all CPUs.
4
5dnl  Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
6dnl  Foundation, Inc.
7dnl
8dnl  This file is part of the GNU MP Library.
9dnl
10dnl  The GNU MP Library is free software; you can redistribute it and/or
11dnl  modify it under the terms of the GNU Lesser General Public License as
12dnl  published by the Free Software Foundation; either version 3 of the
13dnl  License, or (at your option) any later version.
14dnl
15dnl  The GNU MP Library is distributed in the hope that it will be useful,
16dnl  but WITHOUT ANY WARRANTY; without even the implied warranty of
17dnl  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18dnl  Lesser General Public License for more details.
19dnl
20dnl  You should have received a copy of the GNU Lesser General Public License
21dnl  along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.
22
23
24dnl  These macros are designed for use with any m4 and have been used on
25dnl  GNU, FreeBSD, NetBSD, OpenBSD and SysV.
26dnl
27dnl  GNU m4 and OpenBSD 2.7 m4 will give filenames and line numbers in error
28dnl  messages.
29dnl
30dnl
31dnl  Macros:
32dnl
33dnl  Most new m4 specific macros have an "m4_" prefix to emphasise they're
34dnl  m4 expansions.  But new defining things like deflit() and defreg() are
35dnl  named like the builtin define(), and forloop() is named following the
36dnl  GNU m4 example on which it's based.
37dnl
38dnl  GNU m4 with the -P option uses "m4_" as a prefix for builtins, but that
39dnl  option isn't going to be used, so there's no conflict or confusion.
40dnl
41dnl
42dnl  Comments in output:
43dnl
44dnl  The m4 comment delimiters are left at # and \n, the normal assembler
45dnl  commenting for most CPUs.  m4 passes comment text through without
46dnl  expanding macros in it, which is generally a good thing since it stops
47dnl  unexpected expansions and possible resultant errors.
48dnl
49dnl  But note that when a quoted string is being read, a # isn't special, so
50dnl  apostrophes in comments in quoted strings must be avoided or they'll be
51dnl  interpreted as a closing quote mark.  But when the quoted text is
52dnl  re-read # will still act like a normal comment, suppressing macro
53dnl  expansion.
54dnl
55dnl  For example,
56dnl
57dnl          # apostrophes in comments that're outside quotes are ok
58dnl          # and using macro names like PROLOGUE is ok too
59dnl          ...
60dnl          ifdef(`PIC',`
61dnl                  # but apostrophes aren't ok inside quotes
62dnl                  #                     ^--wrong
63dnl                  ...
64dnl                  # though macro names like PROLOGUE are still ok
65dnl                  ...
66dnl          ')
67dnl
68dnl  If macro expansion in a comment is wanted, use `#' in the .asm (ie. a
69dnl  quoted hash symbol), which will turn into # in the .s but get
70dnl  expansions done on that line.  This can make the .s more readable to
71dnl  humans, but it won't make a blind bit of difference to the assembler.
72dnl
73dnl  All the above applies, mutatis mutandis, when changecom() is used to
74dnl  select @ ! ; or whatever other commenting.
75dnl
76dnl
77dnl  Variations in m4 affecting gmp:
78dnl
79dnl  $# - When a macro is called as "foo" with no brackets, BSD m4 sets $#
80dnl       to 1, whereas GNU or SysV m4 set it to 0.  In all cases though
81dnl       "foo()" sets $# to 1.  This is worked around in various places.
82dnl
83dnl  len() - When "len()" is given an empty argument, BSD m4 evaluates to
84dnl       nothing, whereas GNU, SysV, and the new OpenBSD, evaluate to 0.
85dnl       See m4_length() below which works around this.
86dnl
87dnl  translit() - GNU m4 accepts character ranges like A-Z, and the new
88dnl       OpenBSD m4 does under option -g, but basic BSD and SysV don't.
89dnl
90dnl  popdef() - in BSD and SysV m4 popdef() takes multiple arguments and
91dnl       pops each, but GNU m4 only takes one argument.
92dnl
93dnl  push back - BSD m4 has some limits on the amount of text that can be
94dnl       pushed back.  The limit is reasonably big and so long as macros
95dnl       don't gratuitously duplicate big arguments it isn't a problem.
96dnl       Normally an error message is given, but sometimes it just hangs.
97dnl
98dnl  eval() &,|,^ - GNU and SysV m4 have bitwise operators &,|,^ available,
99dnl       but BSD m4 doesn't (contrary to what the man page suggests) and
100dnl       instead ^ is exponentiation.
101dnl
102dnl  eval() ?: - The C ternary operator "?:" is available in BSD m4, but not
103dnl       in SysV or GNU m4 (as of GNU m4 1.4 and betas of 1.5).
104dnl
105dnl  eval() -2^31 - BSD m4 has a bug where an eval() resulting in -2^31
106dnl       (ie. -2147483648) gives "-(".  Using -2147483648 within an
107dnl       expression is ok, it just can't be a final result.  "-(" will of
108dnl       course upset parsing, with all sorts of strange effects.
109dnl
110dnl  eval() <<,>> - SysV m4 doesn't support shift operators in eval() (on
111dnl       Solaris 7 /usr/xpg4/m4 has them but /usr/ccs/m4 doesn't).  See
112dnl       m4_lshift() and m4_rshift() below for workarounds.
113dnl
114dnl  ifdef() - OSF 4.0 m4 considers a macro defined to a zero value `0' or
115dnl       `00' etc as not defined.  See m4_ifdef below for a workaround.
116dnl
117dnl  m4wrap() sequence - in BSD m4, m4wrap() replaces any previous m4wrap()
118dnl       string, in SysV m4 it appends to it, and in GNU m4 it prepends.
119dnl       See m4wrap_prepend() below which brings uniformity to this.
120dnl
121dnl  m4wrap() 0xFF - old versions of BSD m4 store EOF in a C "char" under an
122dnl       m4wrap() and on systems where char is unsigned by default a
123dnl       spurious 0xFF is output.  This has been observed on recent Cray
124dnl       Unicos Alpha, Apple MacOS X, and HPUX 11 systems.  An autoconf
125dnl       test is used to check for this, see the m4wrap handling below.  It
126dnl       might work to end the m4wrap string with a dnl to consume the
127dnl       0xFF, but that probably induces the offending m4's to read from an
128dnl       already closed "FILE *", which could be bad on a glibc style
129dnl       stdio.
130dnl
131dnl  __file__,__line__ - GNU m4 and OpenBSD 2.7 m4 provide these, and
132dnl       they're used here to make error messages more informative.  GNU m4
133dnl       gives an unhelpful "NONE 0" in an m4wrap(), but that's worked
134dnl       around.
135dnl
136dnl  __file__ quoting - OpenBSD m4, unlike GNU m4, doesn't quote the
137dnl       filename in __file__, so care should be taken that no macro has
138dnl       the same name as a file, or an unwanted expansion will occur when
139dnl       printing an error or warning.
140dnl
141dnl  changecom() - BSD m4 changecom doesn't quite work like the man page
142dnl       suggests, in particular "changecom" or "changecom()" doesn't
143dnl       disable the comment feature, and multi-character comment sequences
144dnl       don't seem to work.  If the default `#' and newline aren't
145dnl       suitable it's necessary to change it to something else,
146dnl       eg. changecom(;).
147dnl
148dnl  OpenBSD 2.6 m4 - in this m4, eval() rejects decimal constants containing
149dnl       an 8 or 9, making it pretty much unusable.  The bug is confined to
150dnl       version 2.6 (it's not in 2.5, and was fixed in 2.7).
151dnl
152dnl  SunOS /usr/bin/m4 - this m4 lacks a number of desired features,
153dnl       including $# and $@, defn(), m4exit(), m4wrap(), pushdef(),
154dnl       popdef().  /usr/5bin/m4 is a SysV style m4 which should always be
155dnl       available, and "configure" will reject /usr/bin/m4 in favour of
156dnl       /usr/5bin/m4 (if necessary).
157dnl
158dnl       The sparc code actually has modest m4 requirements currently and
159dnl       could manage with /usr/bin/m4, but there's no reason to put our
160dnl       macros through contortions when /usr/5bin/m4 is available or GNU
161dnl       m4 can be installed.
162
163
164ifdef(`__ASM_DEFS_M4_INCLUDED__',
165`m4_error(`asm-defs.m4 already included, dont include it twice
166')m4exit(1)')
167define(`__ASM_DEFS_M4_INCLUDED__')
168
169
170dnl  Detect and give a message about the unsuitable OpenBSD 2.6 m4.
171
172ifelse(eval(89),89,,
173`errprint(
174`This m4 doesnt accept 8 and/or 9 in constants in eval(), making it unusable.
175This is probably OpenBSD 2.6 m4 (September 1999).  Upgrade to OpenBSD 2.7,
176or get a bug fix from the CVS (expr.c rev 1.9), or get GNU m4.  Dont forget
177to configure with M4=/wherever/m4 if you install one of these in a directory
178not in $PATH.
179')m4exit(1)')
180
181
182dnl  Detect and give a message about the unsuitable SunOS /usr/bin/m4.
183dnl
184dnl  Unfortunately this test doesn't work when m4 is run in the normal way
185dnl  from mpn/Makefile with "m4 -DOPERATION_foo foo.asm", since the bad m4
186dnl  takes "-" in "-D..." to mean read stdin, so it will look like it just
187dnl  hangs.  But running "m4 asm-defs.m4" to try it out will work.
188dnl
189dnl  We'd like to abort immediately on finding a problem, but unfortunately
190dnl  the bad m4 doesn't have an m4exit(), nor does an invalid eval() kill
191dnl  it.  Unexpanded $#'s in some m4_assert_numargs() later on will comment
192dnl  out some closing parentheses and kill it with "m4: arg stack overflow".
193
194define(m4_dollarhash_works_test,``$#'')
195ifelse(m4_dollarhash_works_test(x),1,,
196`errprint(
197`This m4 doesnt support $# and cant be used for GMP asm processing.
198If this is on SunOS, ./configure should choose /usr/5bin/m4 if you have that
199or can get it, otherwise install GNU m4.  Dont forget to configure with
200M4=/wherever/m4 if you install in a directory not in $PATH.
201')')
202undefine(`m4_dollarhash_works_test')
203
204
205dnl  --------------------------------------------------------------------------
206dnl  Basic error handling things.
207
208
209dnl  Usage: m4_dollarhash_1_if_noparen_p
210dnl
211dnl  Expand to 1 if a call "foo" gives $# set to 1 (as opposed to 0 like GNU
212dnl  and SysV m4 give).
213
214define(m4_dollarhash_1_if_noparen_test,`$#')
215define(m4_dollarhash_1_if_noparen_p,
216eval(m4_dollarhash_1_if_noparen_test==1))
217undefine(`m4_dollarhash_1_if_noparen_test')
218
219
220dnl  Usage: m4wrap_prepend(string)
221dnl
222dnl  Prepend the given string to what will be expanded under m4wrap at the
223dnl  end of input.
224dnl
225dnl  This macro exists to work around variations in m4wrap() behaviour in
226dnl  the various m4s (notes at the start of this file).  Don't use m4wrap()
227dnl  directly since it will interfere with this scheme.
228
229define(m4wrap_prepend,
230m4_assert_numargs(1)
231`define(`m4wrap_string',`$1'defn(`m4wrap_string'))')
232
233define(m4wrap_string,`')
234
235define(m4wrap_works_p,
236`ifelse(M4WRAP_SPURIOUS,yes,0,1)')
237
238ifelse(m4wrap_works_p,1,
239`m4wrap(`m4wrap_string')')
240
241
242dnl  Usage: m4_file_and_line
243dnl
244dnl  Expand to the current file and line number, if the GNU m4 extensions
245dnl  __file__ and __line__ are available.
246dnl
247dnl  In GNU m4 1.4 at the end of input when m4wrap text is expanded,
248dnl  __file__ is NONE and __line__ is 0, which is not a helpful thing to
249dnl  print.  If m4_file_seen() has been called to note the last file seen,
250dnl  then that file at a big line number is used, otherwise "end of input"
251dnl  is used (although "end of input" won't parse as an error message).
252
253define(m4_file_and_line,
254`ifdef(`__file__',
255`ifelse(__file__`'__line__,`NONE0',
256`ifdef(`m4_file_seen_last',`m4_file_seen_last: 999999: ',`end of input: ')',
257`__file__: __line__: ')')')
258
259
260dnl  Usage: m4_errprint_commas(arg,...)
261dnl
262dnl  The same as errprint(), but commas are printed between arguments
263dnl  instead of spaces.
264
265define(m4_errprint_commas,
266`errprint(`$1')dnl
267ifelse(eval($#>1),1,`errprint(`,')m4_errprint_commas(shift($@))')')
268
269
270dnl  Usage: m4_error(args...)
271dnl         m4_warning(args...)
272dnl
273dnl  Print an error message, using m4_errprint_commas, prefixed with the
274dnl  current filename and line number (if available).  m4_error sets up to
275dnl  give an error exit at the end of processing, m4_warning just prints.
276dnl  These macros are the recommended way to print errors.
277dnl
278dnl  The arguments here should be quoted in the usual way to prevent them
279dnl  being expanded when the macro call is read.  (m4_error takes care not
280dnl  to do any further expansion.)
281dnl
282dnl  For example,
283dnl
284dnl         m4_error(`some error message
285dnl         ')
286dnl
287dnl  which prints
288dnl
289dnl         foo.asm:123: some error message
290dnl
291dnl  or if __file__ and __line__ aren't available
292dnl
293dnl         some error message
294dnl
295dnl  The "file:line:" format is a basic style, used by gcc and GNU m4, so
296dnl  emacs and other editors will recognise it in their normal error message
297dnl  parsing.
298
299define(m4_warning,
300`m4_errprint_commas(m4_file_and_line`'$@)')
301
302define(m4_error,
303`define(`m4_error_occurred',1)m4_warning($@)dnl
304ifelse(m4wrap_works_p,0,`m4exit(1)')')
305
306define(`m4_error_occurred',0)
307
308dnl  This m4wrap_prepend() is first, so it'll be executed last.
309m4wrap_prepend(
310`ifelse(m4_error_occurred,1,
311`m4_error(`Errors occurred during m4 processing
312')m4exit(1)')')
313
314
315dnl  Usage: m4_assert_numargs(num)
316dnl
317dnl  Put this unquoted on a line on its own at the start of a macro
318dnl  definition to add some code to check that num many arguments get passed
319dnl  to the macro.  For example,
320dnl
321dnl         define(foo,
322dnl         m4_assert_numargs(2)
323dnl         `something `$1' and `$2' blah blah')
324dnl
325dnl  Then a call like foo(one,two,three) will provoke an error like
326dnl
327dnl         file:10: foo expected 2 arguments, got 3 arguments
328dnl
329dnl  Here are some calls and how many arguments they're interpreted as passing.
330dnl
331dnl         foo(abc,def)  2
332dnl         foo(xyz)      1
333dnl         foo()         0
334dnl         foo          -1
335dnl
336dnl  The -1 for no parentheses at all means a macro that's meant to be used
337dnl  that way can be checked with m4_assert_numargs(-1).  For example,
338dnl
339dnl         define(SPECIAL_SUFFIX,
340dnl         m4_assert_numargs(-1)
341dnl         `ifdef(`FOO',`_foo',`_bar')')
342dnl
343dnl  But as an alternative see also deflit() below where parenthesized
344dnl  expressions following a macro are passed through to the output.
345dnl
346dnl  Note that in BSD m4 there's no way to differentiate calls "foo" and
347dnl  "foo()", so in BSD m4 the distinction between the two isn't enforced.
348dnl  (In GNU and SysV m4 it can be checked, and is.)
349
350
351dnl  m4_assert_numargs is able to check its own arguments by calling
352dnl  assert_numargs_internal directly.
353dnl
354dnl  m4_doublequote($`'0) expands to ``$0'', whereas ``$`'0'' would expand
355dnl  to `$`'0' and do the wrong thing, and likewise for $1.  The same is
356dnl  done in other assert macros.
357dnl
358dnl  $`#' leaves $# in the new macro being defined, and stops # being
359dnl  interpreted as a comment character.
360dnl
361dnl  `dnl ' means an explicit dnl isn't necessary when m4_assert_numargs is
362dnl  used.  The space means that if there is a dnl it'll still work.
363
364dnl  Usage: m4_doublequote(x) expands to ``x''
365define(m4_doublequote,
366`m4_assert_numargs_internal(`$0',1,$#,len(`$1'))``$1''')
367
368define(m4_assert_numargs,
369`m4_assert_numargs_internal(`$0',1,$#,len(`$1'))dnl
370`m4_assert_numargs_internal'(m4_doublequote($`'0),$1,$`#',`len'(m4_doublequote($`'1)))`dnl '')
371
372dnl  Called: m4_assert_numargs_internal(`macroname',wantargs,$#,len(`$1'))
373define(m4_assert_numargs_internal,
374`m4_assert_numargs_internal_check(`$1',`$2',m4_numargs_count(`$3',`$4'))')
375
376dnl  Called: m4_assert_numargs_internal_check(`macroname',wantargs,gotargs)
377dnl
378dnl  If m4_dollarhash_1_if_noparen_p (BSD m4) then gotargs can be 0 when it
379dnl  should be -1.  If wantargs is -1 but gotargs is 0 and the two can't be
380dnl  distinguished then it's allowed to pass.
381dnl
382define(m4_assert_numargs_internal_check,
383`ifelse(eval($2 == $3
384             || ($2==-1 && $3==0 && m4_dollarhash_1_if_noparen_p)),0,
385`m4_error(`$1 expected 'm4_Narguments(`$2')`, got 'm4_Narguments(`$3')
386)')')
387
388dnl  Called: m4_numargs_count($#,len(`$1'))
389dnl  If $#==0 then -1 args, if $#==1 but len(`$1')==0 then 0 args, otherwise
390dnl  $# args.
391define(m4_numargs_count,
392`ifelse($1,0, -1,
393`ifelse(eval($1==1 && $2-0==0),1, 0, $1)')')
394
395dnl  Usage: m4_Narguments(N)
396dnl  "$1 argument" or "$1 arguments" with the plural according to $1.
397define(m4_Narguments,
398`$1 argument`'ifelse(`$1',1,,s)')
399
400
401dnl  --------------------------------------------------------------------------
402dnl  Additional error checking things.
403
404
405dnl  Usage: m4_file_seen()
406dnl
407dnl  Record __file__ for the benefit of m4_file_and_line in m4wrap text.
408dnl
409dnl  The basic __file__ macro comes out quoted in GNU m4, like `foo.asm',
410dnl  and m4_file_seen_last is defined like that too.
411dnl
412dnl  This is used by PROLOGUE, since that's normally in the main .asm file,
413dnl  and in particular it sets up m4wrap error checks for missing EPILOGUE.
414
415define(m4_file_seen,
416m4_assert_numargs(0)
417`ifelse(__file__,`NONE',,
418`define(`m4_file_seen_last',m4_doublequote(__file__))')')
419
420
421dnl  Usage: m4_assert_onearg()
422dnl
423dnl  Put this, unquoted, at the start of a macro definition to add some code
424dnl  to check that one argument is passed to the macro, but with that
425dnl  argument allowed to be empty.  For example,
426dnl
427dnl          define(foo,
428dnl          m4_assert_onearg()
429dnl          `blah blah $1 blah blah')
430dnl
431dnl  Calls "foo(xyz)" or "foo()" are accepted.  A call "foo(xyz,abc)" fails.
432dnl  A call "foo" fails too, but BSD m4 can't detect this case (GNU and SysV
433dnl  m4 can).
434
435define(m4_assert_onearg,
436m4_assert_numargs(0)
437`m4_assert_onearg_internal'(m4_doublequote($`'0),$`#')`dnl ')
438
439dnl  Called: m4_assert_onearg(`macroname',$#)
440define(m4_assert_onearg_internal,
441`ifelse($2,1,,
442`m4_error(`$1 expected 1 argument, got 'm4_Narguments(`$2')
443)')')
444
445
446dnl  Usage: m4_assert_numargs_range(low,high)
447dnl
448dnl  Put this, unquoted, at the start of a macro definition to add some code
449dnl  to check that between low and high many arguments get passed to the
450dnl  macro.  For example,
451dnl
452dnl         define(foo,
453dnl         m4_assert_numargs_range(3,5)
454dnl         `mandatory $1 $2 $3 optional $4 $5 end')
455dnl
456dnl  See m4_assert_numargs() for more info.
457
458define(m4_assert_numargs_range,
459m4_assert_numargs(2)
460``m4_assert_numargs_range_internal'(m4_doublequote($`'0),$1,$2,$`#',`len'(m4_doublequote($`'1)))`dnl '')
461
462dnl  Called: m4_assert_numargs_range_internal(`name',low,high,$#,len(`$1'))
463define(m4_assert_numargs_range_internal,
464m4_assert_numargs(5)
465`m4_assert_numargs_range_check(`$1',`$2',`$3',m4_numargs_count(`$4',`$5'))')
466
467dnl  Called: m4_assert_numargs_range_check(`name',low,high,gotargs)
468dnl
469dnl  If m4_dollarhash_1_if_noparen_p (BSD m4) then gotargs can be 0 when it
470dnl  should be -1.  To ensure a `high' of -1 works, a fudge is applied to
471dnl  gotargs if it's 0 and the 0 and -1 cases can't be distinguished.
472dnl
473define(m4_assert_numargs_range_check,
474m4_assert_numargs(4)
475`ifelse(eval($2 <= $4 &&
476             ($4 - ($4==0 && m4_dollarhash_1_if_noparen_p) <= $3)),0,
477`m4_error(`$1 expected $2 to $3 arguments, got 'm4_Narguments(`$4')
478)')')
479
480
481dnl  Usage: m4_assert_defined(symbol)
482dnl
483dnl  Put this unquoted on a line of its own at the start of a macro
484dnl  definition to add some code to check that the given symbol is defined
485dnl  when the macro is used.  For example,
486dnl
487dnl          define(foo,
488dnl          m4_assert_defined(`FOO_PREFIX')
489dnl          `FOO_PREFIX whatever')
490dnl
491dnl  This is a convenient way to check that the user or ./configure or
492dnl  whatever has defined the things needed by a macro, as opposed to
493dnl  silently generating garbage.
494
495define(m4_assert_defined,
496m4_assert_numargs(1)
497``m4_assert_defined_internal'(m4_doublequote($`'0),``$1'')`dnl '')
498
499dnl  Called: m4_assert_defined_internal(`macroname',`define_required')
500define(m4_assert_defined_internal,
501m4_assert_numargs(2)
502`m4_ifdef(`$2',,
503`m4_error(`$1 needs $2 defined
504')')')
505
506
507dnl  Usage: m4_not_for_expansion(`SYMBOL')
508dnl         define_not_for_expansion(`SYMBOL')
509dnl
510dnl  m4_not_for_expansion turns SYMBOL, if defined, into something which
511dnl  will give an error if expanded.  For example,
512dnl
513dnl         m4_not_for_expansion(`PIC')
514dnl
515dnl  define_not_for_expansion is the same, but always makes a definition.
516dnl
517dnl  These are for symbols that should be tested with ifdef(`FOO',...)
518dnl  rather than be expanded as such.  They guard against accidentally
519dnl  omitting the quotes, as in ifdef(FOO,...).  Note though that they only
520dnl  catches this when FOO is defined, so be sure to test code both with and
521dnl  without each definition.
522
523define(m4_not_for_expansion,
524m4_assert_numargs(1)
525`ifdef(`$1',`define_not_for_expansion(`$1')')')
526
527define(define_not_for_expansion,
528m4_assert_numargs(1)
529`ifelse(defn(`$1'),,,
530`m4_error(``$1' has a non-empty value, maybe it shouldnt be munged with m4_not_for_expansion()
531')')dnl
532define(`$1',`m4_not_for_expansion_internal(`$1')')')
533
534define(m4_not_for_expansion_internal,
535`m4_error(``$1' is not meant to be expanded, perhaps you mean `ifdef(`$1',...)'
536')')
537
538
539dnl  --------------------------------------------------------------------------
540dnl  Various generic m4 things.
541
542
543dnl  Usage: m4_unquote(macro)
544dnl
545dnl  Allow the argument text to be re-evaluated.  This is useful for "token
546dnl  pasting" like m4_unquote(foo`'bar).
547
548define(m4_unquote,
549m4_assert_onearg()
550`$1')
551
552
553dnl  Usage: m4_ifdef(name,yes[,no])
554dnl
555dnl  Expand to the yes argument if name is defined, or to the no argument if
556dnl  not.
557dnl
558dnl  This is the same as the builtin "ifdef", but avoids an OSF 4.0 m4 bug
559dnl  in which a macro with a zero value `0' or `00' etc is considered not
560dnl  defined.
561dnl
562dnl  There's no particular need to use this everywhere, only if there might
563dnl  be a zero value.
564
565define(m4_ifdef,
566m4_assert_numargs_range(2,3)
567`ifelse(eval(ifdef(`$1',1,0)+m4_length(defn(`$1'))),0,
568`$3',`$2')')
569
570
571dnl  Usage: m4_ifdef_anyof_p(`symbol',...)
572dnl
573dnl  Expand to 1 if any of the symbols in the argument list are defined, or
574dnl  to 0 if not.
575
576define(m4_ifdef_anyof_p,
577`ifelse(eval($#<=1 && m4_length(`$1')==0),1, 0,
578`ifdef(`$1', 1,
579`m4_ifdef_anyof_p(shift($@))')')')
580
581
582dnl  Usage: m4_length(string)
583dnl
584dnl  Determine the length of a string.  This is the same as len(), but
585dnl  always expands to a number, working around the BSD len() which
586dnl  evaluates to nothing given an empty argument.
587
588define(m4_length,
589m4_assert_onearg()
590`eval(len(`$1')-0)')
591
592
593dnl  Usage: m4_stringequal_p(x,y)
594dnl
595dnl  Expand to 1 or 0 according as strings x and y are equal or not.
596
597define(m4_stringequal_p,
598`ifelse(`$1',`$2',1,0)')
599
600
601dnl  Usage: m4_incr_or_decr(n,last)
602dnl
603dnl  Do an incr(n) or decr(n), whichever is in the direction of "last".
604dnl  Both n and last must be numbers of course.
605
606define(m4_incr_or_decr,
607m4_assert_numargs(2)
608`ifelse(eval($1<$2),1,incr($1),decr($1))')
609
610
611dnl  Usage: forloop(i, first, last, statement)
612dnl
613dnl  Based on GNU m4 examples/forloop.m4, but extended.
614dnl
615dnl  statement is expanded repeatedly, with i successively defined as
616dnl
617dnl         first, first+1, ..., last-1, last
618dnl
619dnl  Or if first > last, then it's
620dnl
621dnl         first, first-1, ..., last+1, last
622dnl
623dnl  If first == last, then one expansion is done.
624dnl
625dnl  A pushdef/popdef of i is done to preserve any previous definition (or
626dnl  lack of definition).  first and last are eval()ed and so can be
627dnl  expressions.
628dnl
629dnl  forloop_first is defined to 1 on the first iteration, 0 on the rest.
630dnl  forloop_last is defined to 1 on the last iteration, 0 on the others.
631dnl  Nested forloops are allowed, in which case forloop_first and
632dnl  forloop_last apply to the innermost loop that's open.
633dnl
634dnl  A simple example,
635dnl
636dnl         forloop(i, 1, 2*2+1, `dnl
637dnl         iteration number i ... ifelse(forloop_first,1,FIRST)
638dnl         ')
639
640
641dnl  "i" and "statement" are carefully quoted, but "first" and "last" are
642dnl  just plain numbers once eval()ed.
643
644define(`forloop',
645m4_assert_numargs(4)
646`pushdef(`$1',eval(`$2'))dnl
647pushdef(`forloop_first',1)dnl
648pushdef(`forloop_last',0)dnl
649forloop_internal(`$1',eval(`$3'),`$4')`'dnl
650popdef(`forloop_first')dnl
651popdef(`forloop_last')dnl
652popdef(`$1')')
653
654dnl  Called: forloop_internal(`var',last,statement)
655define(`forloop_internal',
656m4_assert_numargs(3)
657`ifelse($1,$2,
658`define(`forloop_last',1)$3',
659`$3`'dnl
660define(`forloop_first',0)dnl
661define(`$1',m4_incr_or_decr($1,$2))dnl
662forloop_internal(`$1',$2,`$3')')')
663
664
665dnl  Usage: foreach(var,body, item1,item2,...,itemN)
666dnl
667dnl  For each "item" argument, define "var" to that value and expand "body".
668dnl  For example,
669dnl
670dnl         foreach(i, `something i
671dnl         ', one, two)
672dnl  gives
673dnl         something one
674dnl         something two
675dnl
676dnl  Any previous definition of "var", or lack thereof, is saved and
677dnl  restored.  Empty "item"s are not allowed.
678
679define(foreach,
680m4_assert_numargs_range(2,1000)
681`ifelse(`$3',,,
682`pushdef(`$1',`$3')$2`'popdef(`$1')dnl
683foreach(`$1',`$2',shift(shift(shift($@))))')')
684
685
686dnl  Usage: m4_toupper(x)
687dnl         m4_tolower(x)
688dnl
689dnl  Convert the argument string to upper or lower case, respectively.
690dnl  Only one argument accepted.
691dnl
692dnl  BSD m4 doesn't take ranges like a-z in translit(), so the full alphabet
693dnl  is written out.
694
695define(m4_alphabet_lower, `abcdefghijklmnopqrstuvwxyz')
696define(m4_alphabet_upper, `ABCDEFGHIJKLMNOPQRSTUVWXYZ')
697
698define(m4_toupper,
699m4_assert_onearg()
700`translit(`$1', m4_alphabet_lower, m4_alphabet_upper)')
701
702define(m4_tolower,
703m4_assert_onearg()
704`translit(`$1', m4_alphabet_upper, m4_alphabet_lower)')
705
706
707dnl  Usage: m4_empty_if_zero(x)
708dnl
709dnl  Evaluate to x, or to nothing if x is 0.  x is eval()ed and so can be an
710dnl  expression.
711dnl
712dnl  This is useful for x86 addressing mode displacements since forms like
713dnl  (%ebx) are one byte shorter than 0(%ebx).  A macro `foo' for use as
714dnl  foo(%ebx) could be defined with the following so it'll be empty if the
715dnl  expression comes out zero.
716dnl
717dnl	   deflit(`foo', `m4_empty_if_zero(a+b*4-c)')
718dnl
719dnl  Naturally this shouldn't be done if, say, a computed jump depends on
720dnl  the code being a particular size.
721
722define(m4_empty_if_zero,
723m4_assert_onearg()
724`ifelse(eval($1),0,,eval($1))')
725
726
727dnl  Usage: m4_log2(x)
728dnl
729dnl  Calculate a logarithm to base 2.
730dnl  x must be an integral power of 2, between 2**0 and 2**30.
731dnl  x is eval()ed, so it can be an expression.
732dnl  An error results if x is invalid.
733dnl
734dnl  2**31 isn't supported, because an unsigned 2147483648 is out of range
735dnl  of a 32-bit signed int.  Also, the bug in BSD m4 where an eval()
736dnl  resulting in 2147483648 (or -2147483648 as the case may be) gives `-('
737dnl  means tests like eval(1<<31==(x)) would be necessary, but that then
738dnl  gives an unattractive explosion of eval() error messages if x isn't
739dnl  numeric.
740
741define(m4_log2,
742m4_assert_numargs(1)
743`m4_log2_internal(0,1,eval(`$1'))')
744
745dnl  Called: m4_log2_internal(n,2**n,target)
746define(m4_log2_internal,
747m4_assert_numargs(3)
748`ifelse($2,$3,$1,
749`ifelse($1,30,
750`m4_error(`m4_log2() argument too big or not a power of two: $3
751')',
752`m4_log2_internal(incr($1),eval(2*$2),$3)')')')
753
754
755dnl  Usage:  m4_div2_towards_zero
756dnl
757dnl  m4 division is probably whatever a C signed division is, and C doesn't
758dnl  specify what rounding gets used on negatives, so this expression forces
759dnl  a rounding towards zero.
760
761define(m4_div2_towards_zero,
762m4_assert_numargs(1)
763`eval((($1) + ((($1)<0) & ($1))) / 2)')
764
765
766dnl  Usage: m4_lshift(n,count)
767dnl         m4_rshift(n,count)
768dnl
769dnl  Calculate n shifted left or right by count many bits.  Both n and count
770dnl  are eval()ed and so can be expressions.
771dnl
772dnl  Negative counts are allowed and mean a shift in the opposite direction.
773dnl  Negative n is allowed and right shifts will be arithmetic (meaning
774dnl  divide by 2**count, rounding towards zero, also meaning the sign bit is
775dnl  duplicated).
776dnl
777dnl  Use these macros instead of << and >> in eval() since the basic ccs
778dnl  SysV m4 doesn't have those operators.
779
780define(m4_rshift,
781m4_assert_numargs(2)
782`m4_lshift(`$1',-(`$2'))')
783
784define(m4_lshift,
785m4_assert_numargs(2)
786`m4_lshift_internal(eval(`$1'),eval(`$2'))')
787
788define(m4_lshift_internal,
789m4_assert_numargs(2)
790`ifelse(eval($2-0==0),1,$1,
791`ifelse(eval($2>0),1,
792`m4_lshift_internal(eval($1*2),decr($2))',
793`m4_lshift_internal(m4_div2_towards_zero($1),incr($2))')')')
794
795
796dnl  Usage: m4_popcount(n)
797dnl
798dnl  Expand to the number 1 bits in n.
799
800define(m4_popcount,
801m4_assert_numargs(1)
802`m4_popcount_internal(0,eval(`$1'))')
803
804dnl  Called: m4_popcount_internal(count,rem)
805define(m4_popcount_internal,
806m4_assert_numargs(2)
807`ifelse($2,0,$1,
808`m4_popcount_internal(eval($1+($2%2)),eval($2/2))')')
809
810
811dnl  Usage: m4_count_trailing_zeros(N)
812dnl
813dnl  Determine the number of trailing zero bits on N.  N is eval()ed and so
814dnl  can be an expression.  If N is zero an error is generated.
815
816define(m4_count_trailing_zeros,
817m4_assert_numargs(1)
818`m4_count_trailing_zeros_internal(eval(`$1'),0)')
819
820dnl  Called: m4_count_trailing_zeros_internal(val,count)
821define(m4_count_trailing_zeros_internal,
822m4_assert_numargs(2)
823`ifelse($1,0,
824`m4_error(`m4_count_trailing_zeros() given a zero value')',
825`ifelse(eval(($1)%2),1,`$2',
826`m4_count_trailing_zeros_internal(eval($1/2),incr($2))')')')
827
828
829dnl  Usage: deflit(name,value)
830dnl
831dnl  Like define(), but "name" expands like a literal, rather than taking
832dnl  arguments.  For example "name(%eax)" expands to "value(%eax)".
833dnl
834dnl  Limitations:
835dnl
836dnl  $ characters in the value part must have quotes to stop them looking
837dnl  like macro parameters.  For example, deflit(reg,`123+$`'4+567').  See
838dnl  defreg() below for handling simple register definitions like $7 etc.
839dnl
840dnl  "name()" is turned into "name", unfortunately.  In GNU and SysV m4 an
841dnl  error is generated when this happens, but in BSD m4 it will happen
842dnl  silently.  The problem is that in BSD m4 $# is 1 in both "name" or
843dnl  "name()", so there's no way to differentiate them.  Because we want
844dnl  plain "name" to turn into plain "value", we end up with "name()"
845dnl  turning into plain "value" too.
846dnl
847dnl  "name(foo)" will lose any whitespace after commas in "foo", for example
848dnl  "disp(%eax, %ecx)" would become "128(%eax,%ecx)".
849dnl
850dnl  These parentheses oddities shouldn't matter in assembler text, but if
851dnl  they do the suggested workaround is to write "name ()" or "name (foo)"
852dnl  to stop the parentheses looking like a macro argument list.  If a space
853dnl  isn't acceptable in the output, then write "name`'()" or "name`'(foo)".
854dnl  The `' is stripped when read, but again stops the parentheses looking
855dnl  like parameters.
856
857dnl  Quoting for deflit_emptyargcheck is similar to m4_assert_numargs.  The
858dnl  stuff in the ifelse gives a $#, $1 and $@ evaluated in the new macro
859dnl  created, not in deflit.
860define(deflit,
861m4_assert_numargs(2)
862`define(`$1',
863`deflit_emptyargcheck'(``$1'',$`#',m4_doublequote($`'1))`dnl
864$2`'dnl
865ifelse(eval($'`#>1 || m4_length('m4_doublequote($`'1)`)!=0),1,($'`@))')')
866
867dnl  Called: deflit_emptyargcheck(macroname,$#,`$1')
868define(deflit_emptyargcheck,
869`ifelse(eval($2==1 && !m4_dollarhash_1_if_noparen_p && m4_length(`$3')==0),1,
870`m4_error(`dont use a deflit as $1() because it loses the brackets (see deflit in asm-incl.m4 for more information)
871')')')
872
873
874dnl  Usage: m4_assert(`expr')
875dnl
876dnl  Test a compile-time requirement with an m4 expression.  The expression
877dnl  should be quoted, and will be eval()ed and expected to be non-zero.
878dnl  For example,
879dnl
880dnl         m4_assert(`FOO*2+6 < 14')
881
882define(m4_assert,
883m4_assert_numargs(1)
884`ifelse(eval($1),1,,
885`m4_error(`assertion failed: $1
886')')')
887
888
889dnl  Usage: m4_repeat(count,text)
890dnl
891dnl  Expand to the given repetitions of the given text.  A zero count is
892dnl  allowed, and expands to nothing.
893
894define(m4_repeat,
895m4_assert_numargs(2)
896`m4_repeat_internal(eval($1),`$2')')
897
898define(m4_repeat_internal,
899m4_assert_numargs(2)
900`ifelse(`$1',0,,
901`forloop(m4_repeat_internal_counter,1,$1,``$2'')')')
902
903
904dnl  Usage: m4_hex_lowmask(bits)
905dnl
906dnl  Generate a hex constant which is a low mask of the given number of
907dnl  bits.  For example m4_hex_lowmask(10) would give 0x3ff.
908
909define(m4_hex_lowmask,
910m4_assert_numargs(1)
911`m4_cpu_hex_constant(m4_hex_lowmask_internal1(eval(`$1')))')
912
913dnl  Called: m4_hex_lowmask_internal1(bits)
914define(m4_hex_lowmask_internal1,
915m4_assert_numargs(1)
916`ifelse($1,0,`0',
917`m4_hex_lowmask_internal2(eval(($1)%4),eval(($1)/4))')')
918
919dnl  Called: m4_hex_lowmask_internal(remainder,digits)
920define(m4_hex_lowmask_internal2,
921m4_assert_numargs(2)
922`ifelse($1,1,`1',
923`ifelse($1,2,`3',
924`ifelse($1,3,`7')')')dnl
925m4_repeat($2,`f')')
926
927
928dnl  --------------------------------------------------------------------------
929dnl  The following m4_list functions take a list as multiple arguments.
930dnl  Arguments are evaluated multiple times, there's no attempt at strict
931dnl  quoting.  Empty list elements are not allowed, since an empty final
932dnl  argument is ignored.  These restrictions don't affect the current uses,
933dnl  and make the implementation easier.
934
935
936dnl  Usage: m4_list_quote(list,...)
937dnl
938dnl  Produce a list with quoted commas, so it can be a single argument
939dnl  string.  For instance m4_list_quote(a,b,c) gives
940dnl
941dnl         a`,'b`,'c`,'
942dnl
943dnl  This can be used to put a list in a define,
944dnl
945dnl         define(foolist, m4_list_quote(a,b,c))
946dnl
947dnl  Which can then be used for instance as
948dnl
949dnl         m4_list_find(target, foolist)
950
951define(m4_list_quote,
952`ifelse(`$1',,,
953`$1`,'m4_list_quote(shift($@))')')
954
955
956dnl  Usage: m4_list_find(key,list,...)
957dnl
958dnl  Evaluate to 1 or 0 according to whether key is in the list elements.
959
960define(m4_list_find,
961m4_assert_numargs_range(1,1000)
962`ifelse(`$2',,0,
963`ifelse(`$1',`$2',1,
964`m4_list_find(`$1',shift(shift($@)))')')')
965
966
967dnl  Usage: m4_list_remove(key,list,...)
968dnl
969dnl  Evaluate to the given list with `key' removed (if present).
970
971define(m4_list_remove,
972m4_assert_numargs_range(1,1000)
973`ifelse(`$2',,,
974`ifelse(`$1',`$2',,`$2,')dnl
975m4_list_remove(`$1',shift(shift($@)))')')
976
977
978dnl  Usage: m4_list_first(list,...)
979dnl
980dnl  Evaluate to the first element of the list (if any).
981
982define(m4_list_first,`$1')
983
984
985dnl  Usage: m4_list_count(list,...)
986dnl
987dnl  Evaluate to the number of elements in the list.  This can't just use $#
988dnl  because the last element might be empty.
989
990define(m4_list_count,
991`m4_list_count_internal(0,$@)')
992
993dnl  Called: m4_list_internal(count,list,...)
994define(m4_list_count_internal,
995m4_assert_numargs_range(1,1000)
996`ifelse(`$2',,$1,
997`m4_list_count_internal(eval($1+1),shift(shift($@)))')')
998
999
1000dnl  --------------------------------------------------------------------------
1001dnl  Various assembler things, not specific to any particular CPU.
1002dnl
1003
1004
1005dnl  Usage: include_mpn(`filename')
1006dnl
1007dnl  Like include(), but adds a path to the mpn source directory.  For
1008dnl  example,
1009dnl
1010dnl         include_mpn(`sparc64/addmul_1h.asm')
1011
1012define(include_mpn,
1013m4_assert_numargs(1)
1014m4_assert_defined(`CONFIG_TOP_SRCDIR')
1015`include(CONFIG_TOP_SRCDIR`/mpn/$1')')
1016
1017
1018dnl  Usage: C comment ...
1019dnl
1020dnl  This works like a FORTRAN-style comment character.  It can be used for
1021dnl  comments to the right of assembly instructions, where just dnl would
1022dnl  remove the newline and concatenate adjacent lines.
1023dnl
1024dnl  C and/or dnl are useful when an assembler doesn't support comments, or
1025dnl  where different assemblers for a particular CPU need different styles.
1026dnl  The intermediate ".s" files will end up with no comments, just code.
1027dnl
1028dnl  Using C is not intended to cause offence to anyone who doesn't like
1029dnl  FORTRAN; but if that happens it's an unexpected bonus.
1030dnl
1031dnl  During development, if comments are wanted in the .s files to help see
1032dnl  what's expanding where, C can be redefined with something like
1033dnl
1034dnl         define(`C',`#')
1035
1036define(C, `
1037dnl')
1038
1039
1040dnl  Normally PIC is defined (or not) by libtool, but it doesn't set it on
1041dnl  systems which are always PIC.  PIC_ALWAYS established in config.m4
1042dnl  identifies these for us.
1043
1044ifelse(`PIC_ALWAYS',`yes',`define(`PIC')')
1045
1046
1047dnl  Various possible defines passed from the Makefile that are to be tested
1048dnl  with ifdef() rather than be expanded.
1049
1050m4_not_for_expansion(`PIC')
1051m4_not_for_expansion(`DLL_EXPORT')
1052
1053dnl  aors_n
1054m4_not_for_expansion(`OPERATION_add_n')
1055m4_not_for_expansion(`OPERATION_sub_n')
1056
1057dnl  aorsmul_1
1058m4_not_for_expansion(`OPERATION_addmul_1')
1059m4_not_for_expansion(`OPERATION_submul_1')
1060
1061dnl  logops_n
1062m4_not_for_expansion(`OPERATION_and_n')
1063m4_not_for_expansion(`OPERATION_andn_n')
1064m4_not_for_expansion(`OPERATION_nand_n')
1065m4_not_for_expansion(`OPERATION_ior_n')
1066m4_not_for_expansion(`OPERATION_iorn_n')
1067m4_not_for_expansion(`OPERATION_nior_n')
1068m4_not_for_expansion(`OPERATION_xor_n')
1069m4_not_for_expansion(`OPERATION_xnor_n')
1070
1071dnl  popham
1072m4_not_for_expansion(`OPERATION_popcount')
1073m4_not_for_expansion(`OPERATION_hamdist')
1074
1075dnl  lorrshift
1076m4_not_for_expansion(`OPERATION_lshift')
1077m4_not_for_expansion(`OPERATION_rshift')
1078
1079dnl  aorslsh1_n
1080m4_not_for_expansion(`OPERATION_addlsh1_n')
1081m4_not_for_expansion(`OPERATION_sublsh1_n')
1082m4_not_for_expansion(`OPERATION_rsblsh1_n')
1083
1084dnl  aorslsh2_n
1085m4_not_for_expansion(`OPERATION_addlsh2_n')
1086m4_not_for_expansion(`OPERATION_sublsh2_n')
1087m4_not_for_expansion(`OPERATION_rsblsh2_n')
1088
1089dnl  rsh1aors_n
1090m4_not_for_expansion(`OPERATION_rsh1add_n')
1091m4_not_for_expansion(`OPERATION_rsh1sub_n')
1092
1093
1094dnl  Usage: m4_config_gmp_mparam(`symbol')
1095dnl
1096dnl  Check that `symbol' is defined.  If it isn't, issue an error and
1097dnl  terminate immediately.  The error message explains that the symbol
1098dnl  should be in config.m4, copied from gmp-mparam.h.
1099dnl
1100dnl  Termination is immediate since missing say SQR_TOOM2_THRESHOLD can
1101dnl  lead to infinite loops and endless error messages.
1102
1103define(m4_config_gmp_mparam,
1104m4_assert_numargs(1)
1105`ifdef(`$1',,
1106`m4_error(`$1 is not defined.
1107	"configure" should have extracted this from gmp-mparam.h and put it
1108	in config.m4 (or in <cpu>_<file>.asm for a fat binary), but somehow
1109        this has failed.
1110')m4exit(1)')')
1111
1112
1113dnl  Usage: defreg(name,reg)
1114dnl
1115dnl  Give a name to a $ style register.  For example,
1116dnl
1117dnl         defreg(foo,$12)
1118dnl
1119dnl  defreg() inserts an extra pair of quotes after the $ so that it's not
1120dnl  interpreted as an m4 macro parameter, ie. foo is actually $`'12.  m4
1121dnl  strips those quotes when foo is expanded.
1122dnl
1123dnl  deflit() is used to make the new definition, so it will expand
1124dnl  literally even if followed by parentheses ie. foo(99) will become
1125dnl  $12(99).  (But there's nowhere that would be used is there?)
1126dnl
1127dnl  When making further definitions from existing defreg() macros, remember
1128dnl  to use defreg() again to protect the $ in the new definitions too.  For
1129dnl  example,
1130dnl
1131dnl         defreg(a0,$4)
1132dnl         defreg(a1,$5)
1133dnl         ...
1134dnl
1135dnl         defreg(PARAM_DST,a0)
1136dnl
1137dnl  This is only because a0 is expanding at the time the PARAM_DST
1138dnl  definition is made, leaving a literal $4 that must be re-quoted.  On
1139dnl  the other hand in something like the following ra is only expanded when
1140dnl  ret is used and its $`'31 protection will have its desired effect at
1141dnl  that time.
1142dnl
1143dnl         defreg(ra,$31)
1144dnl         ...
1145dnl         define(ret,`j ra')
1146dnl
1147dnl  Note that only $n forms are meant to be used here, and something like
1148dnl  128($30) doesn't get protected and will come out wrong.
1149
1150define(defreg,
1151m4_assert_numargs(2)
1152`deflit(`$1',
1153substr(`$2',0,1)``''substr(`$2',1))')
1154
1155
1156dnl  Usage: m4_instruction_wrapper()
1157dnl
1158dnl  Put this, unquoted, on a line on its own, at the start of a macro
1159dnl  that's a wrapper around an assembler instruction.  It adds code to give
1160dnl  a descriptive error message if the macro is invoked without arguments.
1161dnl
1162dnl  For example, suppose jmp needs to be wrapped,
1163dnl
1164dnl         define(jmp,
1165dnl         m4_instruction_wrapper()
1166dnl         m4_assert_numargs(1)
1167dnl                 `.byte 0x42
1168dnl                 .long  $1
1169dnl                 nop')
1170dnl
1171dnl  The point of m4_instruction_wrapper is to get a better error message
1172dnl  than m4_assert_numargs would give if jmp is accidentally used as plain
1173dnl  "jmp foo" instead of the intended "jmp( foo)".  "jmp()" with no
1174dnl  argument also provokes the error message.
1175dnl
1176dnl  m4_instruction_wrapper should only be used with wrapped instructions
1177dnl  that take arguments, since obviously something meant to be used as say
1178dnl  plain "ret" doesn't want to give an error when used that way.
1179
1180define(m4_instruction_wrapper,
1181m4_assert_numargs(0)
1182``m4_instruction_wrapper_internal'(m4_doublequote($`'0),dnl
1183ifdef(`__file__',`m4_doublequote(__file__)',``the m4 sources''),dnl
1184$`#',m4_doublequote($`'1))`dnl'')
1185
1186dnl  Called: m4_instruction_wrapper_internal($0,`filename',$#,$1)
1187define(m4_instruction_wrapper_internal,
1188`ifelse(eval($3<=1 && m4_length(`$4')==0),1,
1189`m4_error(`$1 is a macro replacing that instruction and needs arguments, see $2 for details
1190')')')
1191
1192
1193dnl  Usage: m4_cpu_hex_constant(string)
1194dnl
1195dnl  Expand to the string prefixed by a suitable `0x' hex marker.  This
1196dnl  should be redefined as necessary for CPUs with different conventions.
1197
1198define(m4_cpu_hex_constant,
1199m4_assert_numargs(1)
1200`0x`$1'')
1201
1202
1203dnl  Usage: UNROLL_LOG2, UNROLL_MASK, UNROLL_BYTES
1204dnl         CHUNK_LOG2, CHUNK_MASK, CHUNK_BYTES
1205dnl
1206dnl  When code supports a variable amount of loop unrolling, the convention
1207dnl  is to define UNROLL_COUNT to the number of limbs processed per loop.
1208dnl  When testing code this can be varied to see how much the loop overhead
1209dnl  is costing.  For example,
1210dnl
1211dnl         deflit(UNROLL_COUNT, 32)
1212dnl
1213dnl  If the forloop() generating the unrolled loop has a pattern processing
1214dnl  more than one limb, the convention is to express this with CHUNK_COUNT.
1215dnl  For example,
1216dnl
1217dnl         deflit(CHUNK_COUNT, 2)
1218dnl
1219dnl  The LOG2, MASK and BYTES definitions below are derived from these COUNT
1220dnl  definitions.  If COUNT is redefined, the LOG2, MASK and BYTES follow
1221dnl  the new definition automatically.
1222dnl
1223dnl  LOG2 is the log base 2 of COUNT.  MASK is COUNT-1, which can be used as
1224dnl  a bit mask.  BYTES is BYTES_PER_MP_LIMB*COUNT, the number of bytes
1225dnl  processed in each unrolled loop.
1226dnl
1227dnl  BYTES_PER_MP_LIMB is defined in a CPU specific m4 include file.  It
1228dnl  exists only so the BYTES definitions here can be common to all CPUs.
1229dnl  In the actual code for a given CPU, an explicit 4 or 8 may as well be
1230dnl  used because the code is only for a particular CPU, it doesn't need to
1231dnl  be general.
1232dnl
1233dnl  Note that none of these macros do anything except give conventional
1234dnl  names to commonly used things.  You still have to write your own
1235dnl  expressions for a forloop() and the resulting address displacements.
1236dnl  Something like the following would be typical for 4 bytes per limb.
1237dnl
1238dnl         forloop(`i',0,UNROLL_COUNT-1,`
1239dnl                 deflit(`disp',eval(i*4))
1240dnl                 ...
1241dnl         ')
1242dnl
1243dnl  Or when using CHUNK_COUNT,
1244dnl
1245dnl         forloop(`i',0,UNROLL_COUNT/CHUNK_COUNT-1,`
1246dnl                 deflit(`disp0',eval(i*CHUNK_COUNT*4))
1247dnl                 deflit(`disp1',eval(disp0+4))
1248dnl                 ...
1249dnl         ')
1250dnl
1251dnl  Clearly `i' can be run starting from 1, or from high to low or whatever
1252dnl  best suits.
1253
1254deflit(UNROLL_LOG2,
1255m4_assert_defined(`UNROLL_COUNT')
1256`m4_log2(UNROLL_COUNT)')
1257
1258deflit(UNROLL_MASK,
1259m4_assert_defined(`UNROLL_COUNT')
1260`eval(UNROLL_COUNT-1)')
1261
1262deflit(UNROLL_BYTES,
1263m4_assert_defined(`UNROLL_COUNT')
1264m4_assert_defined(`BYTES_PER_MP_LIMB')
1265`eval(UNROLL_COUNT * BYTES_PER_MP_LIMB)')
1266
1267deflit(CHUNK_LOG2,
1268m4_assert_defined(`CHUNK_COUNT')
1269`m4_log2(CHUNK_COUNT)')
1270
1271deflit(CHUNK_MASK,
1272m4_assert_defined(`CHUNK_COUNT')
1273`eval(CHUNK_COUNT-1)')
1274
1275deflit(CHUNK_BYTES,
1276m4_assert_defined(`CHUNK_COUNT')
1277m4_assert_defined(`BYTES_PER_MP_LIMB')
1278`eval(CHUNK_COUNT * BYTES_PER_MP_LIMB)')
1279
1280
1281dnl  Usage: MPN(name)
1282dnl
1283dnl  Add MPN_PREFIX to a name.
1284dnl  MPN_PREFIX defaults to "__gmpn_" if not defined.
1285dnl
1286dnl  m4_unquote is used in MPN so that when it expands to say __gmpn_foo,
1287dnl  that identifier will be subject to further macro expansion.  This is
1288dnl  used by some of the fat binary support for renaming symbols.
1289
1290ifdef(`MPN_PREFIX',,
1291`define(`MPN_PREFIX',`__gmpn_')')
1292
1293define(MPN,
1294m4_assert_numargs(1)
1295`m4_unquote(MPN_PREFIX`'$1)')
1296
1297
1298dnl  Usage: mpn_add_n, etc
1299dnl
1300dnl  Convenience definitions using MPN(), like the #defines in gmp.h.  Each
1301dnl  function that might be implemented in assembler is here.
1302
1303define(define_mpn,
1304m4_assert_numargs(1)
1305`define(`mpn_$1',`MPN(`$1')')')
1306
1307define_mpn(add)
1308define_mpn(add_1)
1309define_mpn(add_n)
1310define_mpn(add_nc)
1311define_mpn(addlsh1_n)
1312define_mpn(addlsh2_n)
1313define_mpn(addlsh_n)
1314define_mpn(addmul_1)
1315define_mpn(addmul_1c)
1316define_mpn(addmul_2)
1317define_mpn(addmul_3)
1318define_mpn(addmul_4)
1319define_mpn(add_n_sub_n)
1320define_mpn(add_n_sub_nc)
1321define_mpn(addaddmul_1msb0)
1322define_mpn(and_n)
1323define_mpn(andn_n)
1324define_mpn(bdiv_q_1)
1325define_mpn(pi1_bdiv_q_1)
1326define_mpn(bdiv_dbm1c)
1327define_mpn(cmp)
1328define_mpn(com)
1329define_mpn(copyd)
1330define_mpn(copyi)
1331define_mpn(count_leading_zeros)
1332define_mpn(count_trailing_zeros)
1333define_mpn(divexact_1)
1334define_mpn(divexact_by3c)
1335define_mpn(divrem)
1336define_mpn(divrem_1)
1337define_mpn(divrem_1c)
1338define_mpn(divrem_2)
1339define_mpn(divrem_classic)
1340define_mpn(divrem_newton)
1341define_mpn(dump)
1342define_mpn(gcd)
1343define_mpn(gcd_1)
1344define_mpn(gcdext)
1345define_mpn(get_str)
1346define_mpn(hamdist)
1347define_mpn(invert_limb)
1348define_mpn(ior_n)
1349define_mpn(iorn_n)
1350define_mpn(lshift)
1351define_mpn(lshiftc)
1352define_mpn(mod_1_1p)
1353define_mpn(mod_1_1p_cps)
1354define_mpn(mod_1s_2p)
1355define_mpn(mod_1s_2p_cps)
1356define_mpn(mod_1s_3p)
1357define_mpn(mod_1s_3p_cps)
1358define_mpn(mod_1s_4p)
1359define_mpn(mod_1s_4p_cps)
1360define_mpn(mod_1)
1361define_mpn(mod_1c)
1362define_mpn(mod_34lsub1)
1363define_mpn(modexact_1_odd)
1364define_mpn(modexact_1c_odd)
1365define_mpn(mul)
1366define_mpn(mul_1)
1367define_mpn(mul_1c)
1368define_mpn(mul_2)
1369define_mpn(mul_3)
1370define_mpn(mul_4)
1371define_mpn(mul_basecase)
1372define_mpn(mul_n)
1373define_mpn(mullo_basecase)
1374define_mpn(perfect_square_p)
1375define_mpn(popcount)
1376define_mpn(preinv_divrem_1)
1377define_mpn(preinv_mod_1)
1378define_mpn(nand_n)
1379define_mpn(neg)
1380define_mpn(nior_n)
1381define_mpn(powm)
1382define_mpn(powlo)
1383define_mpn(random)
1384define_mpn(random2)
1385define_mpn(redc_1)
1386define_mpn(redc_2)
1387define_mpn(rsblsh1_n)
1388define_mpn(rsblsh2_n)
1389define_mpn(rsblsh_n)
1390define_mpn(rsh1add_n)
1391define_mpn(rsh1add_nc)
1392define_mpn(rsh1sub_n)
1393define_mpn(rsh1sub_nc)
1394define_mpn(rshift)
1395define_mpn(rshiftc)
1396define_mpn(scan0)
1397define_mpn(scan1)
1398define_mpn(set_str)
1399define_mpn(sqr_basecase)
1400define_mpn(sqr_diagonal)
1401define_mpn(sub_n)
1402define_mpn(sublsh1_n)
1403define_mpn(sublsh2_n)
1404define_mpn(sqrtrem)
1405define_mpn(sub)
1406define_mpn(sub_1)
1407define_mpn(sub_n)
1408define_mpn(sub_nc)
1409define_mpn(submul_1)
1410define_mpn(submul_1c)
1411define_mpn(umul_ppmm)
1412define_mpn(umul_ppmm_r)
1413define_mpn(udiv_qrnnd)
1414define_mpn(udiv_qrnnd_r)
1415define_mpn(xnor_n)
1416define_mpn(xor_n)
1417
1418
1419dnl  Defines for C global arrays and variables, with names matching what's
1420dnl  used in the C code.
1421dnl
1422dnl  Notice that GSYM_PREFIX is included, unlike with the function defines
1423dnl  above.  Also, "deflit" is used so that something like __clz_tab(%ebx)
1424dnl  comes out as __gmpn_clz_tab(%ebx), for the benefit of CPUs with that
1425dnl  style assembler syntax.
1426
1427deflit(__clz_tab,
1428m4_assert_defined(`GSYM_PREFIX')
1429`GSYM_PREFIX`'MPN(`clz_tab')')
1430
1431deflit(binvert_limb_table,
1432m4_assert_defined(`GSYM_PREFIX')
1433`GSYM_PREFIX`'__gmp_binvert_limb_table')
1434
1435
1436dnl  Usage: ASM_START()
1437dnl
1438dnl  Emit any directives needed once at the start of an assembler file, like
1439dnl  ".set noreorder" or whatever.  The default for this is nothing, but
1440dnl  it's redefined by CPU specific m4 files.
1441
1442define(ASM_START)
1443
1444
1445dnl  Usage: ASM_END()
1446dnl
1447dnl  Emit any directives needed once at the end of an assembler file.  The
1448dnl  default for this is nothing, but it's redefined by CPU specific m4 files.
1449
1450define(ASM_END)
1451
1452
1453dnl  Usage: PROLOGUE(foo[,param])
1454dnl         EPILOGUE(foo)
1455dnl
1456dnl  Emit directives to start or end a function.  GSYM_PREFIX is added by
1457dnl  these macros if necessary, so the given "foo" is what the function will
1458dnl  be called in C.
1459dnl
1460dnl  The second parameter to PROLOGUE is used only for some CPUs and should
1461dnl  be omitted if not required.
1462dnl
1463dnl  Nested or overlapping PROLOGUE/EPILOGUE pairs are allowed, if that
1464dnl  makes sense for the system.  The name given to EPILOGUE must be a
1465dnl  currently open PROLOGUE.
1466dnl
1467dnl  If only one PROLOGUE is open then the name can be omitted from
1468dnl  EPILOGUE.  This is encouraged, since it means the name only has to
1469dnl  appear in one place, not two.
1470dnl
1471dnl  The given name "foo" is not fully quoted here, it will be macro
1472dnl  expanded more than once.  This is the way the m4_list macros work, and
1473dnl  it also helps the tune/many.pl program do a renaming like
1474dnl  -D__gmpn_add_n=mpn_add_n_foo when GSYM_PREFIX is not empty.
1475
1476define(PROLOGUE,
1477m4_assert_numargs_range(1,2)
1478`m4_file_seen()dnl
1479define(`PROLOGUE_list',m4_list_quote($1,PROLOGUE_list))dnl
1480ifelse(`$2',,
1481`PROLOGUE_cpu(GSYM_PREFIX`'$1)',
1482`PROLOGUE_cpu(GSYM_PREFIX`'$1,`$2')')')
1483
1484define(EPILOGUE,
1485m4_assert_numargs_range(0,1)
1486`ifelse(`$1',,
1487`ifelse(m4_list_count(PROLOGUE_list),0,
1488`m4_error(`no open functions for EPILOGUE
1489')',
1490`ifelse(m4_list_count(PROLOGUE_list),1,
1491`EPILOGUE_internal(PROLOGUE_current_function)',
1492`m4_error(`more than one open function for EPILOGUE
1493')')')',
1494`EPILOGUE_internal(`$1')')')
1495
1496define(EPILOGUE_internal,
1497m4_assert_numargs(1)
1498m4_assert_defined(`EPILOGUE_cpu')
1499`ifelse(m4_list_find($1,PROLOGUE_list),0,
1500`m4_error(`EPILOGUE without PROLOGUE: $1
1501')')dnl
1502define(`PROLOGUE_list',m4_list_quote(m4_list_remove($1,PROLOGUE_list)))dnl
1503EPILOGUE_cpu(GSYM_PREFIX`$1')')
1504
1505dnl  Currently open PROLOGUEs, as a comma-separated list.
1506define(PROLOGUE_list)
1507
1508
1509dnl  Called: PROLOGUE_check(list,...)
1510dnl  Check there's no remaining open PROLOGUEs at the end of input.
1511define(PROLOGUE_check,
1512`ifelse($1,,,
1513`m4_error(`no EPILOGUE for: $1
1514')dnl
1515PROLOGUE_check(shift($@))')')
1516
1517m4wrap_prepend(`PROLOGUE_check(PROLOGUE_list)')
1518
1519
1520dnl  Usage: PROLOGUE_current_function
1521dnl
1522dnl  This macro expands to the current PROLOGUE/EPILOGUE function, or the
1523dnl  most recent PROLOGUE if such pairs are nested or overlapped.
1524
1525define(PROLOGUE_current_function,
1526m4_assert_numargs(-1)
1527`m4_list_first(PROLOGUE_list)')
1528
1529
1530dnl  Usage: PROLOGUE_cpu(GSYM_PREFIX`'foo[,param])
1531dnl         EPILOGUE_cpu(GSYM_PREFIX`'foo)
1532dnl
1533dnl  These macros hold the CPU-specific parts of PROLOGUE and EPILOGUE.
1534dnl  Both are called with the function name, with GSYM_PREFIX already
1535dnl  prepended.
1536dnl
1537dnl  The definitions here are something typical and sensible, but CPU or
1538dnl  system specific m4 files should redefine them as necessary.  The
1539dnl  optional extra parameter to PROLOGUE_cpu is not expected and not
1540dnl  accepted here.
1541
1542define(PROLOGUE_cpu,
1543m4_assert_numargs(1)
1544`	TEXT
1545	ALIGN(8)
1546	GLOBL	`$1' GLOBL_ATTR
1547	TYPE(`$1',`function')
1548`$1'LABEL_SUFFIX')
1549
1550define(EPILOGUE_cpu,
1551`	SIZE(`$1',.-`$1')')
1552
1553
1554dnl  Usage: L(name)
1555dnl
1556dnl  Generate a local label with the given name.  This is simply a
1557dnl  convenient way to add LSYM_PREFIX.
1558dnl
1559dnl  LSYM_PREFIX might be L$, so defn() must be used to quote it or the L
1560dnl  will expand again as the L macro, making an infinite recursion.
1561
1562define(`L',
1563m4_assert_numargs(1)
1564`defn(`LSYM_PREFIX')$1')
1565
1566
1567dnl  Usage: LDEF(name)
1568dnl
1569dnl  Generate a directive to define a local label.
1570dnl
1571dnl  On systems with a fixed syntax for defining labels there's no need to
1572dnl  use this macro, it's only meant for systems where the syntax varies,
1573dnl  like hppa which is "L(foo):" with gas, but just "L(foo)" in column 0
1574dnl  with the system `as'.
1575dnl
1576dnl  The extra `' after LABEL_SUFFIX avoids any chance of a following
1577dnl  "(...)"  being interpreted as an argument list.  Not that it'd be
1578dnl  sensible to write anything like that after an LDEF(), but just in case.
1579
1580define(LDEF,
1581m4_assert_numargs(1)
1582m4_assert_defined(`LABEL_SUFFIX')
1583`L(`$1')`'LABEL_SUFFIX`'')
1584
1585
1586dnl  Usage: INT32(label,value)
1587dnl         INT64(label,first,second)
1588
1589define(`INT32',
1590m4_assert_defined(`W32')
1591`	ALIGN(4)
1592LDEF(`$1')
1593	W32	$2')
1594
1595define(`INT64',
1596m4_assert_defined(`W32')
1597`	ALIGN(8)
1598LDEF(`$1')
1599	W32	$2
1600	W32	$3')
1601
1602
1603dnl  Usage: ALIGN(bytes)
1604dnl
1605dnl  Emit a ".align" directive.  The alignment is specified in bytes, and
1606dnl  will normally need to be a power of 2.  The actual ".align" generated
1607dnl  is either bytes or logarithmic according to what ./configure finds the
1608dnl  assembler needs.
1609dnl
1610dnl  If ALIGN_FILL_0x90 is defined and equal to "yes", then ", 0x90" is
1611dnl  appended.  This is for x86, see mpn/x86/README.
1612
1613define(ALIGN,
1614m4_assert_numargs(1)
1615m4_assert_defined(`ALIGN_LOGARITHMIC')
1616`.align	ifelse(ALIGN_LOGARITHMIC,yes,`m4_log2($1)',`eval($1)')dnl
1617ifelse(ALIGN_FILL_0x90,yes,`, 0x90')')
1618
1619
1620dnl  Usage: MULFUNC_PROLOGUE(function function...)
1621dnl
1622dnl  A dummy macro which is grepped for by ./configure to know what
1623dnl  functions a multi-function file is providing.  Use this if there aren't
1624dnl  explicit PROLOGUE()s for each possible function.
1625dnl
1626dnl  Multiple MULFUNC_PROLOGUEs can be used, or just one with the function
1627dnl  names separated by spaces.
1628
1629define(`MULFUNC_PROLOGUE',
1630m4_assert_numargs(1)
1631)
1632
1633
1634dnl  Usage: NAILS_SUPPORT(spec spec ...)
1635dnl
1636dnl  A dummy macro which is grepped for by ./configure to know what nails
1637dnl  are supported in an asm file.
1638dnl
1639dnl  Ranges can be given, or just individual values.  Multiple values or
1640dnl  ranges can be given, separated by spaces.  Multiple NAILS_SUPPORT
1641dnl  declarations work too.  Some examples,
1642dnl
1643dnl         NAILS_SUPPORT(1-20)
1644dnl         NAILS_SUPPORT(1 6 9-12)
1645dnl         NAILS_SUPPORT(1-10 16-20)
1646
1647define(NAILS_SUPPORT,
1648m4_assert_numargs(1)
1649)
1650
1651
1652dnl  Usage: GMP_NUMB_MASK
1653dnl
1654dnl  A bit mask for the number part of a limb.  Eg. with 6 bit nails in a
1655dnl  32 bit limb, GMP_NUMB_MASK would be 0x3ffffff.
1656
1657define(GMP_NUMB_MASK,
1658m4_assert_numargs(-1)
1659m4_assert_defined(`GMP_NUMB_BITS')
1660`m4_hex_lowmask(GMP_NUMB_BITS)')
1661
1662
1663divert`'dnl
1664