xref: /netbsd-src/external/lgpl3/gmp/dist/mpn/asm-defs.m4 (revision fdd524d4ccd2bb0c6f67401e938dabf773eb0372)
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, 2011 Free
6dnl  Software 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-defs.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  aors_err1_n
1058m4_not_for_expansion(`OPERATION_add_err1_n')
1059m4_not_for_expansion(`OPERATION_sub_err1_n')
1060
1061dnl  aors_err2_n
1062m4_not_for_expansion(`OPERATION_add_err2_n')
1063m4_not_for_expansion(`OPERATION_sub_err2_n')
1064
1065dnl  aors_err3_n
1066m4_not_for_expansion(`OPERATION_add_err3_n')
1067m4_not_for_expansion(`OPERATION_sub_err3_n')
1068
1069dnl  aorsmul_1
1070m4_not_for_expansion(`OPERATION_addmul_1')
1071m4_not_for_expansion(`OPERATION_submul_1')
1072
1073dnl  logops_n
1074m4_not_for_expansion(`OPERATION_and_n')
1075m4_not_for_expansion(`OPERATION_andn_n')
1076m4_not_for_expansion(`OPERATION_nand_n')
1077m4_not_for_expansion(`OPERATION_ior_n')
1078m4_not_for_expansion(`OPERATION_iorn_n')
1079m4_not_for_expansion(`OPERATION_nior_n')
1080m4_not_for_expansion(`OPERATION_xor_n')
1081m4_not_for_expansion(`OPERATION_xnor_n')
1082
1083dnl  popham
1084m4_not_for_expansion(`OPERATION_popcount')
1085m4_not_for_expansion(`OPERATION_hamdist')
1086
1087dnl  lorrshift
1088m4_not_for_expansion(`OPERATION_lshift')
1089m4_not_for_expansion(`OPERATION_rshift')
1090
1091dnl  aorslsh1_n
1092m4_not_for_expansion(`OPERATION_addlsh1_n')
1093m4_not_for_expansion(`OPERATION_sublsh1_n')
1094m4_not_for_expansion(`OPERATION_rsblsh1_n')
1095
1096dnl  aorslsh2_n
1097m4_not_for_expansion(`OPERATION_addlsh2_n')
1098m4_not_for_expansion(`OPERATION_sublsh2_n')
1099m4_not_for_expansion(`OPERATION_rsblsh2_n')
1100
1101dnl  rsh1aors_n
1102m4_not_for_expansion(`OPERATION_rsh1add_n')
1103m4_not_for_expansion(`OPERATION_rsh1sub_n')
1104
1105
1106dnl  Usage: m4_config_gmp_mparam(`symbol')
1107dnl
1108dnl  Check that `symbol' is defined.  If it isn't, issue an error and
1109dnl  terminate immediately.  The error message explains that the symbol
1110dnl  should be in config.m4, copied from gmp-mparam.h.
1111dnl
1112dnl  Termination is immediate since missing say SQR_TOOM2_THRESHOLD can
1113dnl  lead to infinite loops and endless error messages.
1114
1115define(m4_config_gmp_mparam,
1116m4_assert_numargs(1)
1117`ifdef(`$1',,
1118`m4_error(`$1 is not defined.
1119	"configure" should have extracted this from gmp-mparam.h and put it
1120	in config.m4 (or in <cpu>_<file>.asm for a fat binary), but somehow
1121        this has failed.
1122')m4exit(1)')')
1123
1124
1125dnl  Usage: defreg(name,reg)
1126dnl
1127dnl  Give a name to a $ style register.  For example,
1128dnl
1129dnl         defreg(foo,$12)
1130dnl
1131dnl  defreg() inserts an extra pair of quotes after the $ so that it's not
1132dnl  interpreted as an m4 macro parameter, ie. foo is actually $`'12.  m4
1133dnl  strips those quotes when foo is expanded.
1134dnl
1135dnl  deflit() is used to make the new definition, so it will expand
1136dnl  literally even if followed by parentheses ie. foo(99) will become
1137dnl  $12(99).  (But there's nowhere that would be used is there?)
1138dnl
1139dnl  When making further definitions from existing defreg() macros, remember
1140dnl  to use defreg() again to protect the $ in the new definitions too.  For
1141dnl  example,
1142dnl
1143dnl         defreg(a0,$4)
1144dnl         defreg(a1,$5)
1145dnl         ...
1146dnl
1147dnl         defreg(PARAM_DST,a0)
1148dnl
1149dnl  This is only because a0 is expanding at the time the PARAM_DST
1150dnl  definition is made, leaving a literal $4 that must be re-quoted.  On
1151dnl  the other hand in something like the following ra is only expanded when
1152dnl  ret is used and its $`'31 protection will have its desired effect at
1153dnl  that time.
1154dnl
1155dnl         defreg(ra,$31)
1156dnl         ...
1157dnl         define(ret,`j ra')
1158dnl
1159dnl  Note that only $n forms are meant to be used here, and something like
1160dnl  128($30) doesn't get protected and will come out wrong.
1161
1162define(defreg,
1163m4_assert_numargs(2)
1164`deflit(`$1',
1165substr(`$2',0,1)``''substr(`$2',1))')
1166
1167
1168dnl  Usage: m4_instruction_wrapper()
1169dnl
1170dnl  Put this, unquoted, on a line on its own, at the start of a macro
1171dnl  that's a wrapper around an assembler instruction.  It adds code to give
1172dnl  a descriptive error message if the macro is invoked without arguments.
1173dnl
1174dnl  For example, suppose jmp needs to be wrapped,
1175dnl
1176dnl         define(jmp,
1177dnl         m4_instruction_wrapper()
1178dnl         m4_assert_numargs(1)
1179dnl                 `.byte 0x42
1180dnl                 .long  $1
1181dnl                 nop')
1182dnl
1183dnl  The point of m4_instruction_wrapper is to get a better error message
1184dnl  than m4_assert_numargs would give if jmp is accidentally used as plain
1185dnl  "jmp foo" instead of the intended "jmp( foo)".  "jmp()" with no
1186dnl  argument also provokes the error message.
1187dnl
1188dnl  m4_instruction_wrapper should only be used with wrapped instructions
1189dnl  that take arguments, since obviously something meant to be used as say
1190dnl  plain "ret" doesn't want to give an error when used that way.
1191
1192define(m4_instruction_wrapper,
1193m4_assert_numargs(0)
1194``m4_instruction_wrapper_internal'(m4_doublequote($`'0),dnl
1195ifdef(`__file__',`m4_doublequote(__file__)',``the m4 sources''),dnl
1196$`#',m4_doublequote($`'1))`dnl'')
1197
1198dnl  Called: m4_instruction_wrapper_internal($0,`filename',$#,$1)
1199define(m4_instruction_wrapper_internal,
1200`ifelse(eval($3<=1 && m4_length(`$4')==0),1,
1201`m4_error(`$1 is a macro replacing that instruction and needs arguments, see $2 for details
1202')')')
1203
1204
1205dnl  Usage: m4_cpu_hex_constant(string)
1206dnl
1207dnl  Expand to the string prefixed by a suitable `0x' hex marker.  This
1208dnl  should be redefined as necessary for CPUs with different conventions.
1209
1210define(m4_cpu_hex_constant,
1211m4_assert_numargs(1)
1212`0x`$1'')
1213
1214
1215dnl  Usage: UNROLL_LOG2, UNROLL_MASK, UNROLL_BYTES
1216dnl         CHUNK_LOG2, CHUNK_MASK, CHUNK_BYTES
1217dnl
1218dnl  When code supports a variable amount of loop unrolling, the convention
1219dnl  is to define UNROLL_COUNT to the number of limbs processed per loop.
1220dnl  When testing code this can be varied to see how much the loop overhead
1221dnl  is costing.  For example,
1222dnl
1223dnl         deflit(UNROLL_COUNT, 32)
1224dnl
1225dnl  If the forloop() generating the unrolled loop has a pattern processing
1226dnl  more than one limb, the convention is to express this with CHUNK_COUNT.
1227dnl  For example,
1228dnl
1229dnl         deflit(CHUNK_COUNT, 2)
1230dnl
1231dnl  The LOG2, MASK and BYTES definitions below are derived from these COUNT
1232dnl  definitions.  If COUNT is redefined, the LOG2, MASK and BYTES follow
1233dnl  the new definition automatically.
1234dnl
1235dnl  LOG2 is the log base 2 of COUNT.  MASK is COUNT-1, which can be used as
1236dnl  a bit mask.  BYTES is BYTES_PER_MP_LIMB*COUNT, the number of bytes
1237dnl  processed in each unrolled loop.
1238dnl
1239dnl  BYTES_PER_MP_LIMB is defined in a CPU specific m4 include file.  It
1240dnl  exists only so the BYTES definitions here can be common to all CPUs.
1241dnl  In the actual code for a given CPU, an explicit 4 or 8 may as well be
1242dnl  used because the code is only for a particular CPU, it doesn't need to
1243dnl  be general.
1244dnl
1245dnl  Note that none of these macros do anything except give conventional
1246dnl  names to commonly used things.  You still have to write your own
1247dnl  expressions for a forloop() and the resulting address displacements.
1248dnl  Something like the following would be typical for 4 bytes per limb.
1249dnl
1250dnl         forloop(`i',0,UNROLL_COUNT-1,`
1251dnl                 deflit(`disp',eval(i*4))
1252dnl                 ...
1253dnl         ')
1254dnl
1255dnl  Or when using CHUNK_COUNT,
1256dnl
1257dnl         forloop(`i',0,UNROLL_COUNT/CHUNK_COUNT-1,`
1258dnl                 deflit(`disp0',eval(i*CHUNK_COUNT*4))
1259dnl                 deflit(`disp1',eval(disp0+4))
1260dnl                 ...
1261dnl         ')
1262dnl
1263dnl  Clearly `i' can be run starting from 1, or from high to low or whatever
1264dnl  best suits.
1265
1266deflit(UNROLL_LOG2,
1267m4_assert_defined(`UNROLL_COUNT')
1268`m4_log2(UNROLL_COUNT)')
1269
1270deflit(UNROLL_MASK,
1271m4_assert_defined(`UNROLL_COUNT')
1272`eval(UNROLL_COUNT-1)')
1273
1274deflit(UNROLL_BYTES,
1275m4_assert_defined(`UNROLL_COUNT')
1276m4_assert_defined(`BYTES_PER_MP_LIMB')
1277`eval(UNROLL_COUNT * BYTES_PER_MP_LIMB)')
1278
1279deflit(CHUNK_LOG2,
1280m4_assert_defined(`CHUNK_COUNT')
1281`m4_log2(CHUNK_COUNT)')
1282
1283deflit(CHUNK_MASK,
1284m4_assert_defined(`CHUNK_COUNT')
1285`eval(CHUNK_COUNT-1)')
1286
1287deflit(CHUNK_BYTES,
1288m4_assert_defined(`CHUNK_COUNT')
1289m4_assert_defined(`BYTES_PER_MP_LIMB')
1290`eval(CHUNK_COUNT * BYTES_PER_MP_LIMB)')
1291
1292
1293dnl  Usage: MPN(name)
1294dnl
1295dnl  Add MPN_PREFIX to a name.
1296dnl  MPN_PREFIX defaults to "__gmpn_" if not defined.
1297dnl
1298dnl  m4_unquote is used in MPN so that when it expands to say __gmpn_foo,
1299dnl  that identifier will be subject to further macro expansion.  This is
1300dnl  used by some of the fat binary support for renaming symbols.
1301
1302ifdef(`MPN_PREFIX',,
1303`define(`MPN_PREFIX',`__gmpn_')')
1304
1305define(MPN,
1306m4_assert_numargs(1)
1307`m4_unquote(MPN_PREFIX`'$1)')
1308
1309
1310dnl  Usage: mpn_add_n, etc
1311dnl
1312dnl  Convenience definitions using MPN(), like the #defines in gmp.h.  Each
1313dnl  function that might be implemented in assembler is here.
1314
1315define(define_mpn,
1316m4_assert_numargs(1)
1317`deflit(`mpn_$1',`MPN(`$1')')')
1318
1319define_mpn(add)
1320define_mpn(add_1)
1321define_mpn(add_err1_n)
1322define_mpn(add_err2_n)
1323define_mpn(add_err3_n)
1324define_mpn(add_n)
1325define_mpn(add_nc)
1326define_mpn(addcnd_n)
1327define_mpn(addlsh1_n)
1328define_mpn(addlsh1_nc)
1329define_mpn(addlsh2_n)
1330define_mpn(addlsh2_nc)
1331define_mpn(addlsh_n)
1332define_mpn(addlsh_nc)
1333define_mpn(addlsh1_n_ip1)
1334define_mpn(addlsh1_nc_ip1)
1335define_mpn(addlsh2_n_ip1)
1336define_mpn(addlsh2_nc_ip1)
1337define_mpn(addlsh_n_ip1)
1338define_mpn(addlsh_nc_ip1)
1339define_mpn(addlsh1_n_ip2)
1340define_mpn(addlsh1_nc_ip2)
1341define_mpn(addlsh2_n_ip2)
1342define_mpn(addlsh2_nc_ip2)
1343define_mpn(addlsh_n_ip2)
1344define_mpn(addlsh_nc_ip2)
1345define_mpn(addmul_1)
1346define_mpn(addmul_1c)
1347define_mpn(addmul_2)
1348define_mpn(addmul_3)
1349define_mpn(addmul_4)
1350define_mpn(addmul_5)
1351define_mpn(addmul_6)
1352define_mpn(addmul_7)
1353define_mpn(addmul_8)
1354define_mpn(addmul_2s)
1355define_mpn(add_n_sub_n)
1356define_mpn(add_n_sub_nc)
1357define_mpn(addaddmul_1msb0)
1358define_mpn(and_n)
1359define_mpn(andn_n)
1360define_mpn(bdiv_q_1)
1361define_mpn(pi1_bdiv_q_1)
1362define_mpn(bdiv_dbm1c)
1363define_mpn(cmp)
1364define_mpn(com)
1365define_mpn(copyd)
1366define_mpn(copyi)
1367define_mpn(count_leading_zeros)
1368define_mpn(count_trailing_zeros)
1369define_mpn(div_qr_2)
1370define_mpn(div_qr_2n_pi1)
1371define_mpn(div_qr_2u_pi1)
1372define_mpn(div_qr_2n_pi2)
1373define_mpn(div_qr_2u_pi2)
1374define_mpn(divexact_1)
1375define_mpn(divexact_by3c)
1376define_mpn(divrem)
1377define_mpn(divrem_1)
1378define_mpn(divrem_1c)
1379define_mpn(divrem_2)
1380define_mpn(divrem_classic)
1381define_mpn(divrem_newton)
1382define_mpn(dump)
1383define_mpn(gcd)
1384define_mpn(gcd_1)
1385define_mpn(gcdext)
1386define_mpn(get_str)
1387define_mpn(hamdist)
1388define_mpn(invert_limb)
1389define_mpn(invert_limb_table)
1390define_mpn(ior_n)
1391define_mpn(iorn_n)
1392define_mpn(lshift)
1393define_mpn(lshiftc)
1394define_mpn(mod_1_1p)
1395define_mpn(mod_1_1p_cps)
1396define_mpn(mod_1s_2p)
1397define_mpn(mod_1s_2p_cps)
1398define_mpn(mod_1s_3p)
1399define_mpn(mod_1s_3p_cps)
1400define_mpn(mod_1s_4p)
1401define_mpn(mod_1s_4p_cps)
1402define_mpn(mod_1)
1403define_mpn(mod_1c)
1404define_mpn(mod_34lsub1)
1405define_mpn(modexact_1_odd)
1406define_mpn(modexact_1c_odd)
1407define_mpn(mul)
1408define_mpn(mul_1)
1409define_mpn(mul_1c)
1410define_mpn(mul_2)
1411define_mpn(mul_3)
1412define_mpn(mul_4)
1413define_mpn(mul_5)
1414define_mpn(mul_6)
1415define_mpn(mul_basecase)
1416define_mpn(mul_n)
1417define_mpn(mullo_basecase)
1418define_mpn(mulmid_basecase)
1419define_mpn(perfect_square_p)
1420define_mpn(popcount)
1421define_mpn(preinv_divrem_1)
1422define_mpn(preinv_mod_1)
1423define_mpn(nand_n)
1424define_mpn(neg)
1425define_mpn(nior_n)
1426define_mpn(powm)
1427define_mpn(powlo)
1428define_mpn(random)
1429define_mpn(random2)
1430define_mpn(redc_1)
1431define_mpn(redc_2)
1432define_mpn(rsblsh1_n)
1433define_mpn(rsblsh1_nc)
1434define_mpn(rsblsh2_n)
1435define_mpn(rsblsh2_nc)
1436define_mpn(rsblsh_n)
1437define_mpn(rsblsh_nc)
1438define_mpn(rsh1add_n)
1439define_mpn(rsh1add_nc)
1440define_mpn(rsh1sub_n)
1441define_mpn(rsh1sub_nc)
1442define_mpn(rshift)
1443define_mpn(rshiftc)
1444define_mpn(scan0)
1445define_mpn(scan1)
1446define_mpn(set_str)
1447define_mpn(sqr_basecase)
1448define_mpn(sqr_diagonal)
1449define_mpn(sqr_diag_addlsh1)
1450define_mpn(sub_n)
1451define_mpn(subcnd_n)
1452define_mpn(sublsh1_n)
1453define_mpn(sublsh1_nc)
1454define_mpn(sublsh1_n_ip1)
1455define_mpn(sublsh1_nc_ip1)
1456define_mpn(sublsh2_n)
1457define_mpn(sublsh2_nc)
1458define_mpn(sublsh2_n_ip1)
1459define_mpn(sublsh2_nc_ip1)
1460define_mpn(sublsh_n)
1461define_mpn(sublsh_nc)
1462define_mpn(sublsh_n_ip1)
1463define_mpn(sublsh_nc_ip1)
1464define_mpn(sqrtrem)
1465define_mpn(sub)
1466define_mpn(sub_1)
1467define_mpn(sub_err1_n)
1468define_mpn(sub_err2_n)
1469define_mpn(sub_err3_n)
1470define_mpn(sub_n)
1471define_mpn(sub_nc)
1472define_mpn(submul_1)
1473define_mpn(submul_1c)
1474define_mpn(tabselect)
1475define_mpn(umul_ppmm)
1476define_mpn(umul_ppmm_r)
1477define_mpn(udiv_qrnnd)
1478define_mpn(udiv_qrnnd_r)
1479define_mpn(xnor_n)
1480define_mpn(xor_n)
1481
1482
1483dnl  Defines for C global arrays and variables, with names matching what's
1484dnl  used in the C code.
1485dnl
1486dnl  Notice that GSYM_PREFIX is included, unlike with the function defines
1487dnl  above.  Also, "deflit" is used so that something like __clz_tab(%ebx)
1488dnl  comes out as __gmpn_clz_tab(%ebx), for the benefit of CPUs with that
1489dnl  style assembler syntax.
1490
1491deflit(__clz_tab,
1492m4_assert_defined(`GSYM_PREFIX')
1493`GSYM_PREFIX`'MPN(`clz_tab')')
1494
1495deflit(binvert_limb_table,
1496m4_assert_defined(`GSYM_PREFIX')
1497`GSYM_PREFIX`'__gmp_binvert_limb_table')
1498
1499
1500dnl  Usage: ASM_START()
1501dnl
1502dnl  Emit any directives needed once at the start of an assembler file, like
1503dnl  ".set noreorder" or whatever.  The default for this is nothing, but
1504dnl  it's redefined by CPU specific m4 files.
1505
1506define(ASM_START)
1507
1508
1509dnl  Usage: ASM_END()
1510dnl
1511dnl  Emit any directives needed once at the end of an assembler file.  The
1512dnl  default for this is nothing, but it's redefined by CPU specific m4 files.
1513
1514define(ASM_END)
1515
1516
1517dnl  Usage: PROLOGUE(foo[,param])
1518dnl         EPILOGUE(foo)
1519dnl
1520dnl  Emit directives to start or end a function.  GSYM_PREFIX is added by
1521dnl  these macros if necessary, so the given "foo" is what the function will
1522dnl  be called in C.
1523dnl
1524dnl  The second parameter to PROLOGUE is used only for some CPUs and should
1525dnl  be omitted if not required.
1526dnl
1527dnl  Nested or overlapping PROLOGUE/EPILOGUE pairs are allowed, if that
1528dnl  makes sense for the system.  The name given to EPILOGUE must be a
1529dnl  currently open PROLOGUE.
1530dnl
1531dnl  If only one PROLOGUE is open then the name can be omitted from
1532dnl  EPILOGUE.  This is encouraged, since it means the name only has to
1533dnl  appear in one place, not two.
1534dnl
1535dnl  The given name "foo" is not fully quoted here, it will be macro
1536dnl  expanded more than once.  This is the way the m4_list macros work, and
1537dnl  it also helps the tune/many.pl program do a renaming like
1538dnl  -D__gmpn_add_n=mpn_add_n_foo when GSYM_PREFIX is not empty.
1539
1540define(PROLOGUE,
1541m4_assert_numargs_range(1,2)
1542`m4_file_seen()dnl
1543define(`PROLOGUE_list',m4_list_quote($1,PROLOGUE_list))dnl
1544ifelse(`$2',,
1545`PROLOGUE_cpu(GSYM_PREFIX`'$1)',
1546`PROLOGUE_cpu(GSYM_PREFIX`'$1,`$2')')')
1547
1548define(EPILOGUE,
1549m4_assert_numargs_range(0,1)
1550`ifelse(`$1',,
1551`ifelse(m4_list_count(PROLOGUE_list),0,
1552`m4_error(`no open functions for EPILOGUE
1553')',
1554`ifelse(m4_list_count(PROLOGUE_list),1,
1555`EPILOGUE_internal(PROLOGUE_current_function)',
1556`m4_error(`more than one open function for EPILOGUE
1557')')')',
1558`EPILOGUE_internal(`$1')')')
1559
1560define(EPILOGUE_internal,
1561m4_assert_numargs(1)
1562m4_assert_defined(`EPILOGUE_cpu')
1563`ifelse(m4_list_find($1,PROLOGUE_list),0,
1564`m4_error(`EPILOGUE without PROLOGUE: $1
1565')')dnl
1566define(`PROLOGUE_list',m4_list_quote(m4_list_remove($1,PROLOGUE_list)))dnl
1567EPILOGUE_cpu(GSYM_PREFIX`$1')')
1568
1569dnl  Currently open PROLOGUEs, as a comma-separated list.
1570define(PROLOGUE_list)
1571
1572
1573dnl  Called: PROLOGUE_check(list,...)
1574dnl  Check there's no remaining open PROLOGUEs at the end of input.
1575define(PROLOGUE_check,
1576`ifelse($1,,,
1577`m4_error(`no EPILOGUE for: $1
1578')dnl
1579PROLOGUE_check(shift($@))')')
1580
1581m4wrap_prepend(`PROLOGUE_check(PROLOGUE_list)')
1582
1583
1584dnl  Usage: PROLOGUE_current_function
1585dnl
1586dnl  This macro expands to the current PROLOGUE/EPILOGUE function, or the
1587dnl  most recent PROLOGUE if such pairs are nested or overlapped.
1588
1589define(PROLOGUE_current_function,
1590m4_assert_numargs(-1)
1591`m4_list_first(PROLOGUE_list)')
1592
1593
1594dnl  Usage: PROLOGUE_cpu(GSYM_PREFIX`'foo[,param])
1595dnl         EPILOGUE_cpu(GSYM_PREFIX`'foo)
1596dnl
1597dnl  These macros hold the CPU-specific parts of PROLOGUE and EPILOGUE.
1598dnl  Both are called with the function name, with GSYM_PREFIX already
1599dnl  prepended.
1600dnl
1601dnl  The definitions here are something typical and sensible, but CPU or
1602dnl  system specific m4 files should redefine them as necessary.  The
1603dnl  optional extra parameter to PROLOGUE_cpu is not expected and not
1604dnl  accepted here.
1605
1606define(PROLOGUE_cpu,
1607m4_assert_numargs(1)
1608`	TEXT
1609	ALIGN(8)
1610	GLOBL	`$1' GLOBL_ATTR
1611	TYPE(`$1',`function')
1612`$1'LABEL_SUFFIX')
1613
1614define(EPILOGUE_cpu,
1615`	SIZE(`$1',.-`$1')')
1616
1617
1618dnl  Usage: L(name)
1619dnl
1620dnl  Generate a local label with the given name.  This is simply a
1621dnl  convenient way to add LSYM_PREFIX.
1622dnl
1623dnl  LSYM_PREFIX might be L$, so defn() must be used to quote it or the L
1624dnl  will expand again as the L macro, making an infinite recursion.
1625
1626define(`L',
1627m4_assert_numargs(1)
1628`defn(`LSYM_PREFIX')$1')
1629
1630
1631dnl  Usage: LDEF(name)
1632dnl
1633dnl  Generate a directive to define a local label.
1634dnl
1635dnl  On systems with a fixed syntax for defining labels there's no need to
1636dnl  use this macro, it's only meant for systems where the syntax varies,
1637dnl  like hppa which is "L(foo):" with gas, but just "L(foo)" in column 0
1638dnl  with the system `as'.
1639dnl
1640dnl  The extra `' after LABEL_SUFFIX avoids any chance of a following
1641dnl  "(...)"  being interpreted as an argument list.  Not that it'd be
1642dnl  sensible to write anything like that after an LDEF(), but just in case.
1643
1644define(LDEF,
1645m4_assert_numargs(1)
1646m4_assert_defined(`LABEL_SUFFIX')
1647`L(`$1')`'LABEL_SUFFIX`'')
1648
1649
1650dnl  Usage: INT32(label,value)
1651dnl         INT64(label,first,second)
1652
1653define(`INT32',
1654m4_assert_defined(`W32')
1655`	ALIGN(4)
1656LDEF(`$1')
1657	W32	$2')
1658
1659define(`INT64',
1660m4_assert_defined(`W32')
1661`	ALIGN(8)
1662LDEF(`$1')
1663	W32	$2
1664	W32	$3')
1665
1666
1667dnl  Usage: ALIGN(bytes)
1668dnl
1669dnl  Emit a ".align" directive.  The alignment is specified in bytes, and
1670dnl  will normally need to be a power of 2.  The actual ".align" generated
1671dnl  is either bytes or logarithmic according to what ./configure finds the
1672dnl  assembler needs.
1673dnl
1674dnl  If ALIGN_FILL_0x90 is defined and equal to "yes", then ", 0x90" is
1675dnl  appended.  This is for x86, see mpn/x86/README.
1676
1677define(ALIGN,
1678m4_assert_numargs(1)
1679m4_assert_defined(`ALIGN_LOGARITHMIC')
1680`.align	ifelse(ALIGN_LOGARITHMIC,yes,`m4_log2($1)',`eval($1)')dnl
1681ifelse(ALIGN_FILL_0x90,yes,`, 0x90')')
1682
1683
1684dnl  Usage: MULFUNC_PROLOGUE(function function...)
1685dnl
1686dnl  A dummy macro which is grepped for by ./configure to know what
1687dnl  functions a multi-function file is providing.  Use this if there aren't
1688dnl  explicit PROLOGUE()s for each possible function.
1689dnl
1690dnl  Multiple MULFUNC_PROLOGUEs can be used, or just one with the function
1691dnl  names separated by spaces.
1692
1693define(`MULFUNC_PROLOGUE',
1694m4_assert_numargs(1)
1695)
1696
1697
1698dnl  Usage: NAILS_SUPPORT(spec spec ...)
1699dnl
1700dnl  A dummy macro which is grepped for by ./configure to know what nails
1701dnl  are supported in an asm file.
1702dnl
1703dnl  Ranges can be given, or just individual values.  Multiple values or
1704dnl  ranges can be given, separated by spaces.  Multiple NAILS_SUPPORT
1705dnl  declarations work too.  Some examples,
1706dnl
1707dnl         NAILS_SUPPORT(1-20)
1708dnl         NAILS_SUPPORT(1 6 9-12)
1709dnl         NAILS_SUPPORT(1-10 16-20)
1710
1711define(NAILS_SUPPORT,
1712m4_assert_numargs(1)
1713)
1714
1715
1716dnl  Usage: ABI_SUPPORT(abi)
1717dnl
1718dnl  A dummy macro which is grepped for by ./configure to know what ABIs
1719dnl  are supported in an asm file.
1720dnl
1721dnl  If multiple non-standard ABIs are supported, several ABI_SUPPORT
1722dnl  declarations should be used:
1723dnl
1724dnl         ABI_SUPPORT(FOOABI)
1725dnl         ABI_SUPPORT(BARABI)
1726
1727define(ABI_SUPPORT,
1728m4_assert_numargs(1)
1729)
1730
1731
1732dnl  Usage: GMP_NUMB_MASK
1733dnl
1734dnl  A bit mask for the number part of a limb.  Eg. with 6 bit nails in a
1735dnl  32 bit limb, GMP_NUMB_MASK would be 0x3ffffff.
1736
1737define(GMP_NUMB_MASK,
1738m4_assert_numargs(-1)
1739m4_assert_defined(`GMP_NUMB_BITS')
1740`m4_hex_lowmask(GMP_NUMB_BITS)')
1741
1742
1743dnl  Usage: m4append(`variable',`value-to-append')
1744
1745define(`m4append',
1746`define(`$1',  defn(`$1')`$2')
1747'
1748)
1749
1750divert`'dnl
1751