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