3 dnl m4 macros for gmp assembly code, shared by all CPUs.
5 dnl These macros are designed for use with any m4 and have been used on
6 dnl GNU, FreeBSD, OpenBSD and SysV.
8 dnl GNU m4 and OpenBSD 2.7 m4 will give filenames and line numbers in error
12 dnl Copyright (C) 1999, 2000 Free Software Foundation, Inc.
14 dnl This file is part of the GNU MP Library.
16 dnl The GNU MP Library is free software; you can redistribute it and/or
17 dnl modify it under the terms of the GNU Lesser General Public License as
18 dnl published by the Free Software Foundation; either version 2.1 of the
19 dnl License, or (at your option) any later version.
21 dnl The GNU MP Library is distributed in the hope that it will be useful,
22 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
23 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 dnl Lesser General Public License for more details.
26 dnl You should have received a copy of the GNU Lesser General Public
27 dnl License along with the GNU MP Library; see the file COPYING.LIB. If
28 dnl not, write to the Free Software Foundation, Inc., 59 Temple Place -
29 dnl Suite 330, Boston, MA 02111-1307, USA.
34 dnl Most new m4 specific macros have an "m4_" prefix to emphasise they're
35 dnl m4 expansions. But new defining things like deflit() and defreg() are
36 dnl named like the builtin define(), and forloop() is named following the
37 dnl GNU m4 example on which it's based.
39 dnl GNU m4 with the -P option uses "m4_" as a prefix for builtins, but that
40 dnl option isn't going to be used, so there's no conflict or confusion.
43 dnl Comments in output:
45 dnl The m4 comment delimiters are left at # and \n, the normal assembler
46 dnl commenting for most CPUs. m4 passes comment text through without
47 dnl expanding macros in it, which is generally a good thing since it stops
48 dnl unexpected expansions and possible resultant errors.
50 dnl But note that when a quoted string is being read, a # isn't special, so
51 dnl apostrophes in comments in quoted strings must be avoided or they'll be
52 dnl interpreted as a closing quote mark. But when the quoted text is
53 dnl re-read # will still act like a normal comment, supressing macro
58 dnl # apostrophes in comments that're outside quotes are ok
59 dnl # and using macro names like PROLOGUE is ok too
62 dnl # but apostrophes aren't ok inside quotes
65 dnl # though macro names like PROLOGUE are still ok
69 dnl If macro expansion in a comment is wanted, use `#' in the .asm (ie. a
70 dnl quoted hash symbol), which will turn into # in the .s but get
71 dnl expansions done on that line. This can make the .s more readable to
72 dnl humans, but it won't make a blind bit of difference to the assembler.
74 dnl All the above applies, mutatis mutandis, when changecom() is used to
75 dnl select @ ! ; or whatever other commenting.
78 dnl Variations in m4 affecting gmp:
80 dnl $# - When a macro is called as "foo" with no brackets, BSD m4 sets $#
81 dnl to 1, whereas GNU or SysV m4 set it to 0. In all cases though
82 dnl "foo()" sets $# to 1. This is worked around in various places.
84 dnl len() - When "len()" is given an empty argument, BSD m4 evaluates to
85 dnl nothing, whereas GNU, SysV, and the new OpenBSD, evaluate to 0.
86 dnl See m4_length() below which works around this.
88 dnl translit() - GNU m4 accepts character ranges like A-Z, and the new
89 dnl OpenBSD m4 does under option -g, but basic BSD and SysV don't.
91 dnl popdef() - in BSD and SysV m4 popdef() takes multiple arguments and
92 dnl pops each, but GNU m4 only takes one argument.
94 dnl push back - BSD m4 has some limits on the amount of text that can be
95 dnl pushed back. The limit is reasonably big and so long as macros
96 dnl don't gratuitously duplicate big arguments it isn't a problem.
97 dnl Normally an error message is given, but sometimes it just hangs.
99 dnl eval() &,|,^ - GNU and SysV m4 have bitwise operators &,|,^ available,
100 dnl but BSD m4 doesn't (contrary to what the man page suggests) and
101 dnl instead ^ is exponentiation.
103 dnl eval() ?: - The C ternary operator "?:" is available in BSD m4, but not
104 dnl in SysV or GNU m4 (as of GNU m4 1.4 and betas of 1.5).
106 dnl eval() -2^31 - BSD m4 has a bug where an eval() resulting in -2^31
107 dnl (ie. -2147483648) gives "-(". Using -2147483648 within an
108 dnl expression is ok, it just can't be a final result. "-(" will of
109 dnl course upset parsing, with all sorts of strange effects.
111 dnl eval() <<,>> - SysV m4 doesn't support shift operators in eval() (on
112 dnl SunOS 5.7 /usr/xpg4/m4 has them but /usr/ccs/m4 doesn't). See
113 dnl m4_lshift() and m4_rshift() below for workarounds.
115 dnl m4wrap() - in BSD m4, m4wrap() replaces any previous m4wrap() string,
116 dnl in SysV m4 it appends to it, and in GNU m4 it prepends. See
117 dnl m4wrap_prepend() below which brings uniformity to this.
119 dnl __file__,__line__ - GNU m4 and OpenBSD 2.7 m4 provide these, and
120 dnl they're used here to make error messages more informative. GNU m4
121 dnl gives an unhelpful "NONE 0" in an m4wrap(), but that's worked
124 dnl __file__ quoting - OpenBSD m4, unlike GNU m4, doesn't quote the
125 dnl filename in __file__, so care should be taken that no macro has
126 dnl the same name as a file, or an unwanted expansion will occur when
127 dnl printing an error or warning.
129 dnl OpenBSD 2.6 m4 - this m4 rejects decimal constants containing an 8 or 9
130 dnl in eval(), making it pretty much unusable. This bug is confined
131 dnl to version 2.6 (it's not in 2.5, and has been fixed in 2.7).
133 dnl SunOS /usr/bin/m4 - this m4 lacks a number of desired features,
134 dnl including $# and $@, defn(), m4exit(), m4wrap(), pushdef(),
135 dnl popdef(). /usr/5bin/m4 is a SysV style m4 which should always be
136 dnl available, and "configure" will reject /usr/bin/m4 in favour of
137 dnl /usr/5bin/m4 (if necessary).
139 dnl The sparc code actually has modest m4 requirements currently and
140 dnl could manage with /usr/bin/m4, but there's no reason to put our
141 dnl macros through contortions when /usr/5bin/m4 is available or GNU
142 dnl m4 can be installed.
145 ifdef(`__ASM_DEFS_M4_INCLUDED__',
146 `m4_error(`asm-defs.m4 already included, dont include it twice
148 define(`__ASM_DEFS_M4_INCLUDED__')
151 dnl Detect and give a message about the unsuitable OpenBSD 2.6 m4.
155 `This m4 doesnt accept 8 and/or 9 in constants in eval(), making it unusable.
156 This is probably OpenBSD 2.6 m4 (September 1999). Upgrade to OpenBSD 2.7,
157 or get a bug fix from the CVS (expr.c rev 1.9), or get GNU m4. Dont forget
158 to configure with M4=/wherever/m4 if you install one of these in a directory
163 dnl Detect and give a message about the unsuitable SunOS /usr/bin/m4.
165 dnl Unfortunately this test doesn't work when m4 is run in the normal way
166 dnl from mpn/Makefile with "m4 -DOPERATION_foo foo.asm", since the bad m4
167 dnl takes "-" in "-D..." to mean read stdin, so it will look like it just
168 dnl hangs. But running "m4 asm-defs.m4" to try it out will work.
170 dnl We'd like to abort immediately on finding a problem, but unfortunately
171 dnl the bad m4 doesn't have an m4exit(), nor does an invalid eval() kill
172 dnl it. Unexpanded $#'s in some m4_assert_numargs() later on will comment
173 dnl out some closing parentheses and kill it with "m4: arg stack overflow".
175 define(m4_dollarhash_works_test,``$#'')
176 ifelse(m4_dollarhash_works_test(x),1,,
178 `This m4 doesnt support $# and cant be used for GMP asm processing.
179 If this is on SunOS, ./configure should choose /usr/5bin/m4 if you have that
180 or can get it, otherwise install GNU m4. Dont forget to configure with
181 M4=/wherever/m4 if you install in a directory not in $PATH.
183 undefine(`m4_dollarhash_works_test')
186 dnl --------------------------------------------------------------------------
187 dnl Basic error handling things.
190 dnl Usage: m4_dollarhash_1_if_noparen_p
192 dnl Expand to 1 if a call "foo" gives $# set to 1 (as opposed to 0 like GNU
193 dnl and SysV m4 give).
195 define(m4_dollarhash_1_if_noparen_test,`$#')
196 define(m4_dollarhash_1_if_noparen_p,
197 eval(m4_dollarhash_1_if_noparen_test==1))
198 undefine(`m4_dollarhash_1_if_noparen_test')
201 dnl Usage: m4wrap_prepend(string)
203 dnl Prepend the given string to what will be exapanded under m4wrap at the
206 dnl This macro exists to work around variations in m4wrap() behaviour in
207 dnl the various m4s (notes at the start of this file). Don't use m4wrap()
208 dnl directly since it will interfere with this scheme.
210 define(m4wrap_prepend,
212 `define(`m4wrap_string',`$1'defn(`m4wrap_string'))')
214 m4wrap(`m4wrap_string')
215 define(m4wrap_string,`')
218 dnl Usage: m4_file_and_line
220 dnl Expand to the current file and line number, if the GNU m4 extensions
221 dnl __file__ and __line__ are available.
223 dnl In GNU m4 1.4 at the end of input when m4wrap text is expanded,
224 dnl __file__ is NONE and __line__ is 0, which is not a helpful thing to
225 dnl print. If m4_file_seen() has been called to note the last file seen,
226 dnl then that file at a big line number is used, otherwise "end of input"
227 dnl is used (although "end of input" won't parse as an error message).
229 define(m4_file_and_line,
231 `ifelse(__file__`'__line__,`NONE0',
232 `ifdef(`m4_file_seen_last',`m4_file_seen_last: 999999: ',`end of input: ')',
233 `__file__: __line__: ')')')
236 dnl Usage: m4_errprint_commas(arg,...)
238 dnl The same as errprint(), but commas are printed between arguments
239 dnl instead of spaces.
241 define(m4_errprint_commas,
243 ifelse(eval($#>1),1,`errprint(`,')m4_errprint_commas(shift($@))')')
246 dnl Usage: m4_error(args...)
247 dnl m4_warning(args...)
249 dnl Print an error message, using m4_errprint_commas, prefixed with the
250 dnl current filename and line number (if available). m4_error sets up to
251 dnl give an error exit at the end of processing, m4_warning just prints.
252 dnl These macros are the recommended way to print errors.
254 dnl The arguments here should be quoted in the usual way to prevent them
255 dnl being expanded when the macro call is read. (m4_error takes care not
256 dnl to do any further expansion.)
260 dnl m4_error(`some error message
265 dnl foo.asm:123: some error message
267 dnl or if __file__ and __line__ aren't available
269 dnl some error message
271 dnl The "file:line:" format is a basic style, used by gcc and GNU m4, so
272 dnl emacs and other editors will recognise it in their normal error message
276 `m4_errprint_commas(m4_file_and_line`'$@)')
279 `define(`m4_error_occurred',1)m4_warning($@)')
281 define(`m4_error_occurred',0)
283 dnl This m4wrap_prepend() is first, so it'll be executed last.
285 `ifelse(m4_error_occurred,1,
286 `m4_error(`Errors occurred during m4 processing
290 dnl Usage: m4_assert_numargs(num)
292 dnl Put this unquoted on a line on its own at the start of a macro
293 dnl definition to add some code to check that num many arguments get passed
294 dnl to the macro. For example,
297 dnl m4_assert_numargs(2)
298 dnl `something `$1' and `$2' blah blah')
300 dnl Then a call like foo(one,two,three) will provoke an error like
302 dnl file:10: foo expected 2 arguments, got 3 arguments
304 dnl Here are some calls and how many arguments they're interpreted as passing.
311 dnl The -1 for no parentheses at all means a macro that's meant to be used
312 dnl that way can be checked with m4_assert_numargs(-1). For example,
314 dnl define(SPECIAL_SUFFIX,
315 dnl m4_assert_numargs(-1)
316 dnl `ifdef(`FOO',`_foo',`_bar')')
318 dnl But as an alternative see also deflit() below where parenthesized
319 dnl expressions following a macro are passed through to the output.
321 dnl Note that in BSD m4 there's no way to differentiate calls "foo" and
322 dnl "foo()", so in BSD m4 the distinction between the two isn't enforced.
323 dnl (In GNU and SysV m4 it can be checked, and is.)
326 dnl m4_assert_numargs is able to check its own arguments by calling
327 dnl assert_numargs_internal directly.
329 dnl m4_doublequote($`'0) expands to ``$0'', whereas ``$`'0'' would expand
330 dnl to `$`'0' and do the wrong thing, and likewise for $1. The same is
331 dnl done in other assert macros.
333 dnl $`#' leaves $# in the new macro being defined, and stops # being
334 dnl interpreted as a comment character.
336 dnl `dnl ' means an explicit dnl isn't necessary when m4_assert_numargs is
337 dnl used. The space means that if there is a dnl it'll still work.
339 dnl Usage: m4_doublequote(x) expands to ``x''
340 define(m4_doublequote,
341 `m4_assert_numargs_internal(`$0',1,$#,len(`$1'))``$1''')
343 define(m4_assert_numargs,
344 `m4_assert_numargs_internal(`$0',1,$#,len(`$1'))dnl
345 `m4_assert_numargs_internal'(m4_doublequote($`'0),$1,$`#',`len'(m4_doublequote($`'1)))`dnl '')
347 dnl Called: m4_assert_numargs_internal(`macroname',wantargs,$#,len(`$1'))
348 define(m4_assert_numargs_internal,
349 `m4_assert_numargs_internal_check(`$1',`$2',m4_numargs_count(`$3',`$4'))')
351 dnl Called: m4_assert_numargs_internal_check(`macroname',wantargs,gotargs)
353 dnl If m4_dollarhash_1_if_noparen_p (BSD m4) then gotargs can be 0 when it
354 dnl should be -1. If wantargs is -1 but gotargs is 0 and the two can't be
355 dnl distinguished then it's allowed to pass.
357 define(m4_assert_numargs_internal_check,
358 `ifelse(eval($2 == $3
359 || ($2==-1 && $3==0 && m4_dollarhash_1_if_noparen_p)),0,
360 `m4_error(`$1 expected 'm4_Narguments(`$2')`, got 'm4_Narguments(`$3')
363 dnl Called: m4_numargs_count($#,len(`$1'))
364 dnl If $#==0 then -1 args, if $#==1 but len(`$1')==0 then 0 args, otherwise
366 define(m4_numargs_count,
368 `ifelse(eval($1==1 && $2-0==0),1, 0, $1)')')
370 dnl Usage: m4_Narguments(N)
371 dnl "$1 argument" or "$1 arguments" with the plural according to $1.
372 define(m4_Narguments,
373 `$1 argument`'ifelse(`$1',1,,s)')
376 dnl --------------------------------------------------------------------------
377 dnl Additional error checking things.
380 dnl Usage: m4_file_seen()
382 dnl Record __file__ for the benefit of m4_file_and_line in m4wrap text.
383 dnl The basic __file__ macro comes out quoted, like `foo.asm', and
384 dnl m4_file_seen_last is defined like that too.
386 dnl This only needs to be used with something that could generate an error
387 dnl message in m4wrap text. The x86 PROLOGUE is the only such at the
388 dnl moment (at end of input its m4wrap checks for missing EPILOGUE). A few
389 dnl include()s can easily trick this scheme, but you'd expect an EPILOGUE
390 dnl in the same file as the PROLOGUE.
394 `ifelse(__file__,`NONE',,
395 `define(`m4_file_seen_last',m4_doublequote(__file__))')')
398 dnl Usage: m4_assert_onearg()
400 dnl Put this, unquoted, at the start of a macro definition to add some code
401 dnl to check that one argument is passed to the macro, but with that
402 dnl argument allowed to be empty. For example,
405 dnl m4_assert_onearg()
406 dnl `blah blah $1 blah blah')
408 dnl Calls "foo(xyz)" or "foo()" are accepted. A call "foo(xyz,abc)" fails.
409 dnl A call "foo" fails too, but BSD m4 can't detect this case (GNU and SysV
412 define(m4_assert_onearg,
414 `m4_assert_onearg_internal'(m4_doublequote($`'0),$`#')`dnl ')
416 dnl Called: m4_assert_onearg(`macroname',$#)
417 define(m4_assert_onearg_internal,
419 `m4_error(`$1 expected 1 argument, got 'm4_Narguments(`$2')
423 dnl Usage: m4_assert_numargs_range(low,high)
425 dnl Put this, unquoted, at the start of a macro definition to add some code
426 dnl to check that between low and high many arguments get passed to the
427 dnl macro. For example,
430 dnl m4_assert_numargs_range(3,5)
431 dnl `mandatory $1 $2 $3 optional $4 $5 end')
433 dnl See m4_assert_numargs() for more info.
435 define(m4_assert_numargs_range,
437 ``m4_assert_numargs_range_internal'(m4_doublequote($`'0),$1,$2,$`#',`len'(m4_doublequote($`'1)))`dnl '')
439 dnl Called: m4_assert_numargs_range_internal(`name',low,high,$#,len(`$1'))
440 define(m4_assert_numargs_range_internal,
442 `m4_assert_numargs_range_check(`$1',`$2',`$3',m4_numargs_count(`$4',`$5'))')
444 dnl Called: m4_assert_numargs_range_check(`name',low,high,gotargs)
446 dnl If m4_dollarhash_1_if_noparen_p (BSD m4) then gotargs can be 0 when it
447 dnl should be -1. To ensure a `high' of -1 works, a fudge is applied to
448 dnl gotargs if it's 0 and the 0 and -1 cases can't be distinguished.
450 define(m4_assert_numargs_range_check,
452 `ifelse(eval($2 <= $4 &&
453 ($4 - ($4==0 && m4_dollarhash_1_if_noparen_p) <= $3)),0,
454 `m4_error(`$1 expected $2 to $3 arguments, got 'm4_Narguments(`$4')
458 dnl Usage: m4_assert_defined(symbol)
460 dnl Put this unquoted on a line of its own at the start of a macro
461 dnl definition to add some code to check that the given symbol is defined
462 dnl when the macro is used. For example,
465 dnl m4_assert_defined(`FOO_PREFIX')
466 dnl `FOO_PREFIX whatever')
468 dnl This is a convenient way to check that the user or ./configure or
469 dnl whatever has defined the things needed by a macro, as opposed to
470 dnl silently generating garbage.
472 define(m4_assert_defined,
474 ``m4_assert_defined_internal'(m4_doublequote($`'0),``$1'')`dnl '')
476 dnl Called: m4_assert_defined_internal(`macroname',`define_required')
477 define(m4_assert_defined_internal,
480 `m4_error(`$1 needs $2 defined
484 dnl Usage: m4_not_for_expansion(`SYMBOL')
485 dnl define_not_for_expansion(`SYMBOL')
487 dnl m4_not_for_expansion turns SYMBOL, if defined, into something which
488 dnl will give an error if expanded. For example,
490 dnl m4_not_for_expansion(`PIC')
492 dnl define_not_for_expansion is the same, but always makes a definition.
494 dnl These are for symbols that should be tested with ifdef(`FOO',...)
495 dnl rather than be expanded as such. They guard against accidentally
496 dnl omitting the quotes, as in ifdef(FOO,...). Note though that they only
497 dnl catches this when FOO is defined, so be sure to test code both with and
498 dnl without each definition.
500 define(m4_not_for_expansion,
502 `ifdef(`$1',`define_not_for_expansion(`$1')')')
504 define(define_not_for_expansion,
506 `ifelse(defn(`$1'),,,
507 `m4_error(``$1' has a non-empty value, maybe it shouldnt be munged with m4_not_for_expansion()
509 define(`$1',`m4_not_for_expansion_internal(`$1')')')
511 define(m4_not_for_expansion_internal,
512 `m4_error(``$1' is not meant to be expanded, perhaps you mean `ifdef(`$1',...)'
516 dnl --------------------------------------------------------------------------
517 dnl Various generic m4 things.
520 dnl Usage: m4_ifdef_anyof_p(`symbol',...)
522 dnl Expand to 1 if any of the symbols in the argument list are defined, or
525 define(m4_ifdef_anyof_p,
526 `ifelse(eval($#<=1 && m4_length(`$1')==0),1, 0,
528 `m4_ifdef_anyof_p(shift($@))')')')
531 dnl Usage: m4_length(string)
533 dnl Determine the length of a string. This is the same as len(), but
534 dnl always expands to a number, working around the BSD len() which
535 dnl evaluates to nothing given an empty argument.
542 dnl Usage: m4_stringequal_p(x,y)
544 dnl Expand to 1 or 0 according as strings x and y are equal or not.
546 define(m4_stringequal_p,
547 `ifelse(`$1',`$2',1,0)')
550 dnl Usage: m4_incr_or_decr(n,last)
552 dnl Do an incr(n) or decr(n), whichever is in the direction of "last".
553 dnl Both n and last must be numbers of course.
555 define(m4_incr_or_decr,
557 `ifelse(eval($1<$2),1,incr($1),decr($1))')
560 dnl Usage: forloop(i, first, last, statement)
562 dnl Based on GNU m4 examples/forloop.m4, but extended.
564 dnl statement is expanded repeatedly, with i successively defined as
566 dnl first, first+1, ..., last-1, last
568 dnl Or if first > last, then it's
570 dnl first, first-1, ..., last+1, last
572 dnl If first == last, then one expansion is done.
574 dnl A pushdef/popdef of i is done to preserve any previous definition (or
575 dnl lack of definition). first and last are eval()ed and so can be
578 dnl forloop_first is defined to 1 on the first iteration, 0 on the rest.
579 dnl forloop_last is defined to 1 on the last iteration, 0 on the others.
580 dnl Nested forloops are allowed, in which case forloop_first and
581 dnl forloop_last apply to the innermost loop that's open.
583 dnl A simple example,
585 dnl forloop(i, 1, 2*2+1, `dnl
586 dnl iteration number i ... ifelse(forloop_first,1,FIRST)
590 dnl "i" and "statement" are carefully quoted, but "first" and "last" are
591 dnl just plain numbers once eval()ed.
595 `pushdef(`$1',eval(`$2'))dnl
596 pushdef(`forloop_first',1)dnl
597 pushdef(`forloop_last',0)dnl
598 forloop_internal(`$1',eval(`$3'),`$4')`'dnl
599 popdef(`forloop_first')dnl
600 popdef(`forloop_last')dnl
603 dnl Called: forloop_internal(`var',last,statement)
604 define(`forloop_internal',
607 `define(`forloop_last',1)$3',
609 define(`forloop_first',0)dnl
610 define(`$1',m4_incr_or_decr($1,$2))dnl
611 forloop_internal(`$1',$2,`$3')')')
614 dnl Usage: m4_toupper(x)
617 dnl Convert the argument string to upper or lower case, respectively.
618 dnl Only one argument accepted.
620 dnl BSD m4 doesn't take ranges like a-z in translit(), so the full alphabet
623 define(m4_alphabet_lower, `abcdefghijklmnopqrstuvwxyz')
624 define(m4_alphabet_upper, `ABCDEFGHIJKLMNOPQRSTUVWXYZ')
628 `translit(`$1', m4_alphabet_lower, m4_alphabet_upper)')
632 `translit(`$1', m4_alphabet_upper, m4_alphabet_lower)')
635 dnl Usage: m4_empty_if_zero(x)
637 dnl Evaluate to x, or to nothing if x is 0. x is eval()ed and so can be an
640 dnl This is useful for x86 addressing mode displacements since forms like
641 dnl (%ebx) are one byte shorter than 0(%ebx). A macro `foo' for use as
642 dnl foo(%ebx) could be defined with the following so it'll be empty if the
643 dnl expression comes out zero.
645 dnl deflit(`foo', `m4_empty_if_zero(a+b*4-c)')
647 dnl Naturally this shouldn't be done if, say, a computed jump depends on
648 dnl the code being a particular size.
650 define(m4_empty_if_zero,
652 `ifelse(eval($1),0,,eval($1))')
655 dnl Usage: m4_log2(x)
657 dnl Calculate a logarithm to base 2.
658 dnl x must be an integral power of 2, between 2**0 and 2**30.
659 dnl x is eval()ed, so it can be an expression.
660 dnl An error results if x is invalid.
662 dnl 2**31 isn't supported, because an unsigned 2147483648 is out of range
663 dnl of a 32-bit signed int. Also, the bug in BSD m4 where an eval()
664 dnl resulting in 2147483648 (or -2147483648 as the case may be) gives `-('
665 dnl means tests like eval(1<<31==(x)) would be necessary, but that then
666 dnl gives an unattractive explosion of eval() error messages if x isn't
671 `m4_log2_internal(0,1,eval(`$1'))')
673 dnl Called: m4_log2_internal(n,2**n,target)
674 define(m4_log2_internal,
678 `m4_error(`m4_log2() argument too big or not a power of two: $3
680 `m4_log2_internal(incr($1),eval(2*$2),$3)')')')
683 dnl Usage: m4_div2_towards_zero
685 dnl m4 division is probably whatever a C signed division is, and C doesn't
686 dnl specify what rounding gets used on negatives, so this expression forces
687 dnl a rounding towards zero.
689 define(m4_div2_towards_zero,
691 `eval((($1) + ((($1)<0) & ($1))) / 2)')
694 dnl Usage: m4_lshift(n,count)
695 dnl m4_rshift(n,count)
697 dnl Calculate n shifted left or right by count many bits. Both n and count
698 dnl are eval()ed and so can be expressions.
700 dnl Negative counts are allowed and mean a shift in the opposite direction.
701 dnl Negative n is allowed and right shifts will be arithmetic (meaning
702 dnl divide by 2**count, rounding towards zero, also meaning the sign bit is
705 dnl Use these macros instead of << and >> in eval() since the basic ccs
706 dnl SysV m4 doesn't have those operators.
710 `m4_lshift(`$1',-(`$2'))')
714 `m4_lshift_internal(eval(`$1'),eval(`$2'))')
716 define(m4_lshift_internal,
718 `ifelse(eval($2-0==0),1,$1,
719 `ifelse(eval($2>0),1,
720 `m4_lshift_internal(eval($1*2),decr($2))',
721 `m4_lshift_internal(m4_div2_towards_zero($1),incr($2))')')')
724 dnl Usage: deflit(name,value)
726 dnl Like define(), but "name" expands like a literal, rather than taking
727 dnl arguments. For example "name(%eax)" expands to "value(%eax)".
731 dnl $ characters in the value part must have quotes to stop them looking
732 dnl like macro parameters. For example, deflit(reg,`123+$`'4+567'). See
733 dnl defreg() below for handling simple register definitions like $7 etc.
735 dnl "name()" is turned into "name", unfortunately. In GNU and SysV m4 an
736 dnl error is generated when this happens, but in BSD m4 it will happen
737 dnl silently. The problem is that in BSD m4 $# is 1 in both "name" or
738 dnl "name()", so there's no way to differentiate them. Because we want
739 dnl plain "name" to turn into plain "value", we end up with "name()"
740 dnl turning into plain "value" too.
742 dnl "name(foo)" will lose any whitespace after commas in "foo", for example
743 dnl "disp(%eax, %ecx)" would become "128(%eax,%ecx)".
745 dnl These parentheses oddities shouldn't matter in assembler text, but if
746 dnl they do the suggested workaround is to write "name ()" or "name (foo)"
747 dnl to stop the parentheses looking like a macro argument list. If a space
748 dnl isn't acceptable in the output, then write "name`'()" or "name`'(foo)".
749 dnl The `' is stripped when read, but again stops the parentheses looking
752 dnl Quoting for deflit_emptyargcheck is similar to m4_assert_numargs. The
753 dnl stuff in the ifelse gives a $#, $1 and $@ evaluated in the new macro
754 dnl created, not in deflit.
758 `deflit_emptyargcheck'(``$1'',$`#',m4_doublequote($`'1))`dnl
760 ifelse(eval($'`#>1 || m4_length('m4_doublequote($`'1)`)!=0),1,($'`@))')')
762 dnl Called: deflit_emptyargcheck(macroname,$#,`$1')
763 define(deflit_emptyargcheck,
764 `ifelse(eval($2==1 && !m4_dollarhash_1_if_noparen_p && m4_length(`$3')==0),1,
765 `m4_error(`dont use a deflit as $1() because it loses the brackets (see deflit in asm-incl.m4 for more information)
769 dnl Usage: m4_assert(`expr')
771 dnl Test a compile-time requirement with an m4 expression. The expression
772 dnl should be quoted, and will be eval()ed and expected to be non-zero.
775 dnl m4_assert(`FOO*2+6 < 14')
780 `m4_error(`assertion failed: $1
784 dnl --------------------------------------------------------------------------
785 dnl Various assembler things, not specific to any particular CPU.
789 dnl Usage: include_mpn(`filename')
791 dnl Like include(), but adds a path to the mpn source directory. For
794 dnl include_mpn(`sparc64/addmul_1h.asm')
798 m4_assert_defined(`CONFIG_TOP_SRCDIR')
799 `include(CONFIG_TOP_SRCDIR`/mpn/$1')')
802 dnl Usage: C comment ...
804 dnl "C" works like a FORTRAN-style comment character. This can be used for
805 dnl comments to the right of assembly instructions, where just dnl would
806 dnl remove the linefeed, and concatenate adjacent lines.
808 dnl "C" and/or "dnl" are useful when an assembler doesn't support comments,
809 dnl or where different assemblers for a particular CPU have different
810 dnl comment styles. The intermediate ".s" files will end up with no
811 dnl comments, just code.
813 dnl Using "C" is not intended to cause offence to anyone who doesn't like
814 dnl FORTRAN; but if that happens it's an unexpected bonus.
820 dnl Various possible defines passed from the Makefile that are to be tested
821 dnl with ifdef() rather than be expanded.
823 m4_not_for_expansion(`PIC')
826 m4_not_for_expansion(`OPERATION_add_n')
827 m4_not_for_expansion(`OPERATION_sub_n')
830 m4_not_for_expansion(`OPERATION_addmul_1')
831 m4_not_for_expansion(`OPERATION_submul_1')
834 m4_not_for_expansion(`OPERATION_and_n')
835 m4_not_for_expansion(`OPERATION_andn_n')
836 m4_not_for_expansion(`OPERATION_nand_n')
837 m4_not_for_expansion(`OPERATION_ior_n')
838 m4_not_for_expansion(`OPERATION_iorn_n')
839 m4_not_for_expansion(`OPERATION_nior_n')
840 m4_not_for_expansion(`OPERATION_xor_n')
841 m4_not_for_expansion(`OPERATION_xnor_n')
844 m4_not_for_expansion(`OPERATION_popcount')
845 m4_not_for_expansion(`OPERATION_hamdist')
848 dnl Usage: m4_config_gmp_mparam(`symbol')
850 dnl Check that `symbol' is defined. If it isn't, issue an error and
851 dnl terminate immediately. The error message explains that the symbol
852 dnl should be in config.m4, copied from gmp-mparam.h.
854 dnl Processing is terminated immediately since missing something like
855 dnl KARATSUBA_SQR_THRESHOLD can lead to infinite loops with endless error
858 define(m4_config_gmp_mparam,
861 `m4_error(`$1 is not defined.
862 "configure" should have extracted this from gmp-mparam.h and put it
863 in config.m4, but somehow this has failed.
867 dnl Usage: defreg(name,reg)
869 dnl Give a name to a $ style register. For example,
873 dnl defreg() inserts an extra pair of quotes after the $ so that it's not
874 dnl interpreted as an m4 macro parameter, ie. foo is actually $`'12. m4
875 dnl strips those quotes when foo is expanded.
877 dnl deflit() is used to make the new definition, so it will expand
878 dnl literally even if followed by parentheses ie. foo(99) will become
879 dnl $12(99). (But there's nowhere that would be used is there?)
881 dnl When making further definitions from existing defreg() macros, remember
882 dnl to use defreg() again to protect the $ in the new definitions too. For
889 dnl defreg(PARAM_DST,a0)
891 dnl This is only because a0 is expanding at the time the PARAM_DST
892 dnl definition is made, leaving a literal $4 that must be re-quoted. On
893 dnl the other hand in something like the following ra is only expanded when
894 dnl ret is used and its $`'31 protection will have its desired effect at
899 dnl define(ret,`j ra')
901 dnl Note that only $n forms are meant to be used here, and something like
902 dnl 128($30) doesn't get protected and will come out wrong.
907 substr(`$2',0,1)``''substr(`$2',1))')
910 dnl Usage: m4_instruction_wrapper(num)
912 dnl Put this, unquoted, on a line on its own, at the start of a macro
913 dnl that's a wrapper around an assembler instruction. It adds code to give
914 dnl a descriptive error message if the macro is invoked without arguments.
916 dnl For example, suppose jmp needs to be wrapped,
919 dnl m4_instruction_wrapper()
920 dnl m4_assert_numargs(1)
925 dnl The point of m4_instruction_wrapper is to get a better error message
926 dnl than m4_assert_numargs would give if jmp is accidentally used as plain
927 dnl "jmp foo" instead of the intended "jmp( foo)". "jmp()" with no
928 dnl argument also provokes the error message.
930 dnl m4_instruction_wrapper should only be used with wrapped instructions
931 dnl that take arguments, since obviously something meant to be used as
932 dnl plain "ret", say, doesn't want to give an error when used that way.
934 define(m4_instruction_wrapper,
936 ``m4_instruction_wrapper_internal'(m4_doublequote($`'0),dnl
937 m4_doublequote(ifdef(`__file__',__file__,`the m4 sources')),dnl
938 $`#',m4_doublequote($`'1))`dnl'')
940 dnl Called: m4_instruction_wrapper_internal($0,`filename',$#,$1)
941 define(m4_instruction_wrapper_internal,
942 `ifelse(eval($3<=1 && m4_length(`$4')==0),1,
943 `m4_error(`$1 is a macro replacing that instruction and needs arguments, see $2 for details
947 dnl Usage: UNROLL_LOG2, UNROLL_MASK, UNROLL_BYTES
948 dnl CHUNK_LOG2, CHUNK_MASK, CHUNK_BYTES
950 dnl When code supports a variable amount of loop unrolling, the convention
951 dnl is to define UNROLL_COUNT to the number of limbs processed per loop.
952 dnl When testing code this can be varied to see how much the loop overhead
953 dnl is costing. For example,
955 dnl deflit(UNROLL_COUNT, 32)
957 dnl If the forloop() generating the unrolled loop has a pattern processing
958 dnl more than one limb, the convention is to express this with CHUNK_COUNT.
961 dnl deflit(CHUNK_COUNT, 2)
963 dnl The LOG2, MASK and BYTES definitions below are derived from these COUNT
964 dnl definitions. If COUNT is redefined, the LOG2, MASK and BYTES follow
965 dnl the new definition automatically.
967 dnl LOG2 is the log base 2 of COUNT. MASK is COUNT-1, which can be used as
968 dnl a bit mask. BYTES is BYTES_PER_MP_LIMB*COUNT, the number of bytes
969 dnl processed in each unrolled loop.
971 dnl BYTES_PER_MP_LIMB is defined in a CPU specific m4 include file. It
972 dnl exists only so the BYTES definitions here can be common to all CPUs.
973 dnl In the actual code for a given CPU, an explicit 4 or 8 may as well be
974 dnl used because the code is only for a particular CPU, it doesn't need to
977 dnl Note that none of these macros do anything except give conventional
978 dnl names to commonly used things. You still have to write your own
979 dnl expressions for a forloop() and the resulting address displacements.
980 dnl Something like the following would be typical for 4 bytes per limb.
982 dnl forloop(`i',0,UNROLL_COUNT-1,`
983 dnl deflit(`disp',eval(i*4))
987 dnl Or when using CHUNK_COUNT,
989 dnl forloop(`i',0,UNROLL_COUNT/CHUNK_COUNT-1,`
990 dnl deflit(`disp0',eval(i*CHUNK_COUNT*4))
991 dnl deflit(`disp1',eval(disp0+4))
995 dnl Clearly `i' can be run starting from 1, or from high to low or whatever
999 m4_assert_defined(`UNROLL_COUNT')
1000 `m4_log2(UNROLL_COUNT)')
1003 m4_assert_defined(`UNROLL_COUNT')
1004 `eval(UNROLL_COUNT-1)')
1006 deflit(UNROLL_BYTES,
1007 m4_assert_defined(`UNROLL_COUNT')
1008 m4_assert_defined(`BYTES_PER_MP_LIMB')
1009 `eval(UNROLL_COUNT * BYTES_PER_MP_LIMB)')
1012 m4_assert_defined(`CHUNK_COUNT')
1013 `m4_log2(CHUNK_COUNT)')
1016 m4_assert_defined(`CHUNK_COUNT')
1017 `eval(CHUNK_COUNT-1)')
1020 m4_assert_defined(`CHUNK_COUNT')
1021 m4_assert_defined(`BYTES_PER_MP_LIMB')
1022 `eval(CHUNK_COUNT * BYTES_PER_MP_LIMB)')
1025 dnl Usage: MPN(name)
1027 dnl Add MPN_PREFIX to a name.
1028 dnl MPN_PREFIX defaults to "__gmpn_" if not defined.
1030 ifdef(`MPN_PREFIX',,
1031 `define(`MPN_PREFIX',`__gmpn_')')
1034 m4_assert_numargs(1)
1038 dnl Usage: mpn_add_n, etc
1040 dnl Convenience definitions using MPN(), like the #defines in gmp.h. Each
1041 dnl function that might be implemented in assembler is here.
1044 m4_assert_numargs(1)
1045 `define(`mpn_$1',`MPN(`$1')')')
1051 define_mpn(addmul_1)
1052 define_mpn(addmul_1c)
1053 define_mpn(addsub_n)
1054 define_mpn(addsub_nc)
1062 define_mpn(divexact_by3c)
1064 define_mpn(divrem_1)
1065 define_mpn(divrem_1c)
1066 define_mpn(divrem_2)
1067 define_mpn(divrem_classic)
1068 define_mpn(divrem_newton)
1075 define_mpn(invert_limb)
1078 define_mpn(kara_mul_n)
1079 define_mpn(kara_sqr_n)
1087 define_mpn(mul_basecase)
1089 define_mpn(perfect_square_p)
1090 define_mpn(popcount)
1091 define_mpn(preinv_mod_1)
1101 define_mpn(sqr_basecase)
1108 define_mpn(submul_1)
1109 define_mpn(submul_1c)
1110 define_mpn(toom3_mul_n)
1111 define_mpn(toom3_sqr_n)
1112 define_mpn(umul_ppmm)
1113 define_mpn(udiv_qrnnd)
1124 GLOBL GSYM_PREFIX`$1'
1125 TYPE(GSYM_PREFIX`$1',`function')
1130 SIZE(GSYM_PREFIX`$1',.-GSYM_PREFIX`$1')')
1132 dnl LSYM_PREFIX might be L$, so defn() must be used to quote it or the L
1133 dnl will expand as the L macro, an infinite recursion.
1134 define(`L',`defn(`LSYM_PREFIX')$1')
1152 dnl Usage: ALIGN(bytes)
1154 dnl Emit a ".align" directive. The alignment is specified in bytes, and
1155 dnl will normally need to be a power of 2. The actual ".align" generated
1156 dnl is either bytes or logarithmic according to what ./configure detects.
1158 dnl ALIGN_FILL_0x90, if defined and equal to "yes", means a ", 0x90" should
1159 dnl be appended (this is for x86).
1162 m4_assert_numargs(1)
1163 m4_assert_defined(`ALIGN_LOGARITHMIC')
1164 `.align ifelse(ALIGN_LOGARITHMIC,yes,`m4_log2($1)',`eval($1)')dnl
1165 ifelse(ALIGN_FILL_0x90,yes,`, 0x90')')
1168 dnl Usage: MULFUNC_PROLOGUE(function function...)
1170 dnl A dummy macro which is grepped for by ./configure to know what
1171 dnl functions a multi-function file is providing. Use this if there aren't
1172 dnl explicit PROLOGUE()s for each possible function.
1174 dnl Multiple MULFUNC_PROLOGUEs can be used, or just one with the function
1175 dnl names separated by spaces.
1177 define(`MULFUNC_PROLOGUE',
1178 m4_assert_numargs(1)