3 * Types for the test-vector framework
5 * (c) 2023 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU Library General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * mLib is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 * License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib. If not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
28 /*----- Header files ------------------------------------------------------*/
48 #include "tvec-adhoc.h"
49 #include "tvec-types.h"
51 /*----- Preliminary utilities ---------------------------------------------*/
53 /* --- @trivial_release@ --- *
55 * Arguments: @union tvec_regval *rv@ = a register value
56 * @const struct tvec_regdef@ = the register definition
60 * Use: Does nothing. Used for register values which don't retain
64 static void trivial_release(union tvec_regval *rv,
65 const struct tvec_regdef *rd)
68 /*----- Integer utilities -------------------------------------------------*/
70 /* --- @unsigned_to_buf@, @signed_to_buf@ --- *
72 * Arguments: @buf *b@ = buffer to write on
73 * @unsigned long u@ or @long i@ = integer to write
75 * Returns: Zero on success, @-1@ on failure.
77 * Use: Write @i@ to the buffer, in big-endian (two's-complement, it
81 static int unsigned_to_buf(buf *b, unsigned long u)
82 { kludge64 k; ASSIGN64(k, u); return (buf_putk64l(b, k)); }
84 static int signed_to_buf(buf *b, long i)
90 if (i >= 0) ASSIGN64(k, u);
91 else { ASSIGN64(k, ~u); CPL64(k, k); }
92 return (buf_putk64l(b, k));
95 /* --- @unsigned_from_buf@, @signed_from_buf@ --- *
97 * Arguments: @buf *b@ = buffer to write on
98 * @unsigned long *u_out@ or @long *i_out@ = where to put the
101 * Returns: Zero on success, @-1@ on failure.
103 * Use: Read an integer, in big-endian (two's-complement, if signed)
104 * format, from the buffer.
107 static int unsigned_from_buf(buf *b, unsigned long *u_out)
111 ASSIGN64(ulmax, ULONG_MAX);
112 if (buf_getk64l(b, &k)) return (-1);
113 if (CMP64(k, >, ulmax)) { buf_break(b); return (-1); }
114 *u_out = GET64(unsigned long, k); return (0);
117 /* --- @hex_width@ --- *
119 * Arguments: @unsigned long u@ = an integer
121 * Returns: A suitable number of digits to use in order to display @u@ in
122 * hex. Currently, we select a power of two sufficient to show
123 * the value, but at least 2.
126 static int hex_width(unsigned long u)
131 for (t = u >> 4, wd = 4; t >>= wd, wd *= 2, t; );
135 /* --- @format_unsigned_hex@, @format_signed_hex@ --- *
137 * Arguments: @const struct gprintf_ops *gops@ = print operations
138 * @void *go@ = print destination
139 * @unsigned long u@ or @long i@ = integer to print
143 * Use: Print an unsigned or signed integer in hexadecimal.
146 static void format_unsigned_hex(const struct gprintf_ops *gops, void *go,
148 { gprintf(gops, go, "0x%0*lx", hex_width(u), u); }
150 static void format_signed_hex(const struct gprintf_ops *gops, void *go,
153 unsigned long u = i >= 0 ? i : -(unsigned long)i;
154 gprintf(gops, go, "%s0x%0*lx", i < 0 ? "-" : "", hex_width(u), u);
157 static int signed_from_buf(buf *b, long *i_out)
159 kludge64 k, lmax, not_lmin;
161 ASSIGN64(lmax, LONG_MAX); ASSIGN64(not_lmin, ~(unsigned long)LONG_MIN);
162 if (buf_getk64l(b, &k)) return (-1);
163 if (CMP64(k, <=, lmax)) *i_out = (long)GET64(unsigned long, k);
166 if (CMP64(k, <=, not_lmin)) *i_out = -(long)GET64(unsigned long, k) - 1;
167 else { buf_break(b); return (-1); }
172 /* --- @check_signed_range@, @check_unsigned_range@ --- *
174 * Arguments: @long i@ or @unsigned long u@ = an integer
175 * @const struct tvec_irange *ir@ or
176 * @const struct tvec_urange *ur@ = range specification,
178 * @struct tvec_state *tv@ = test vector state
179 * @const char *what@ = description of value
181 * Returns: Zero on success, or @-1@ on error.
183 * Use: Check that the integer is within bounds. If not, report a
184 * suitable error and return a failure indication.
187 static int check_signed_range(long i,
188 const struct tvec_irange *ir,
189 struct tvec_state *tv, const char *what)
194 if (ir->min > i || i > ir->max) {
195 tvec_error(tv, "%s %ld out of range (must be in [%ld .. %ld])",
196 what, i, ir->min, ir->max);
199 m = ir->m; if (m > 0) m = -m;
201 /* Reduce both the integer and the intended residue to the canonical
202 * interval [0, m). This is more awkward than it should be because C
203 * (following CPU designs) adopted an unhelpful definition of integer
204 * division when the dividend is negative.
206 * Note that I've canonicalized the divisor to be %%\emph{negative}%%,
207 * because in two's-complement arithmetic, the absolute value of the
208 * most negative representable value is not itself representable. The
209 * residue modulo the most negative value will itself be representable.
212 ii = i%m; if (ii < 0) ii -= m;
213 aa = ir->a%m; if (aa < 0) aa -= m;
215 tvec_error(tv, "%s %ld == %ld =/= %ld (mod %ld)",
216 what, i, ii, ir->a, ir->m);
224 static int check_unsigned_range(unsigned long u,
225 const struct tvec_urange *ur,
226 struct tvec_state *tv, const char *what)
231 if (ur->min > u || u > ur->max) {
232 tvec_error(tv, "%s %lu out of range (must be in [%lu .. %lu])",
233 what, u, ur->min, ur->max);
236 if (ur->m && ur->m != 1) {
238 if (uu != ur->a%ur->m) {
239 tvec_error(tv, "%s %lu == %lu =/= %lu (mod %lu)",
240 what, u, uu, ur->a, ur->m);
248 /* --- @chtodig@ --- *
250 * Arguments: @int ch@ = a character
252 * Returns: The numeric value of the character as a digit, or @-1@ if
253 * it's not a digit. Letters count as extended digits starting
254 * with value 10; case is not significant.
257 static int chtodig(int ch)
259 if ('0' <= ch && ch <= '9') return (ch - '0');
260 else if ('a' <= ch && ch <= 'z') return (ch - 'a' + 10);
261 else if ('A' <= ch && ch <= 'Z') return (ch - 'A' + 10);
265 /* --- @parse_unsigned_integer@, @parse_signed_integer@ --- *
267 * Arguments: @unsigned long *u_out@, @long *i_out@ = where to put the
269 * @const char **q_out@ = where to put the end position
270 * @const char *p@ = pointer to the string to parse
272 * Returns: Zero on success, @-1@ on error.
274 * Use: Parse an integer from a string in the test-vector format.
275 * This is mostly extension of the traditional C @strtoul@
276 * format: supported inputs include:
278 * * NNN -- a decimal number (even if it starts with `0');
279 * * 0xNNN -- hexadecimal;
282 * * NNrNNN -- base NN.
284 * Furthermore, single underscores are permitted internally as
285 * an insignificant digit separator.
288 static int parse_unsigned_integer(unsigned long *u_out, const char **q_out,
295 #define f_implicit 1u /* implicitly reading base 10 */
296 #define f_digit 2u /* read a real digit */
297 #define f_uscore 4u /* found an underscore */
301 * This will deal with the traditional `0[box]...' prefixes. We'll leave
302 * our new `NNr...' syntax for later.
304 if (p[0] != '0' || !p[1]) {
305 d = chtodig(*p); if (0 > d || d >= 10) return (-1);
306 r = 10; u = d; p++; f |= f_implicit | f_digit;
308 u = 0; d = chtodig(p[2]);
309 if (d < 0) { r = 10; f |= f_implicit | f_digit; p++; }
310 else if ((p[1] == 'x' || p[1] == 'X') && d < 16) { r = 16; p += 2; }
311 else if ((p[1] == 'o' || p[1] == 'O') && d < 8) { r = 8; p += 2; }
312 else if ((p[1] == 'b' || p[1] == 'B') && d < 2) { r = 2; p += 2; }
313 else { r = 10; f |= f_digit; p++; }
318 /* Work through the string a character at a time. */
320 ch = *p; switch (ch) {
323 /* An underscore is OK if we haven't just seen one. */
325 if (f&f_uscore) goto done;
326 p++; f = (f&~f_implicit) | f_uscore;
330 /* An `r' is OK if the number so far is small enough to be a sensible
331 * base, and we're scanning decimal implicitly.
334 if (!(f&f_implicit) || !u || u >= 36) goto done;
335 d = chtodig(p[1]); if (0 > d || d >= u) goto done;
336 r = u; u = d; f = (f&~f_implicit) | f_digit; p += 2; q = p;
340 /* Otherwise we expect a valid digit and accumulate it. */
341 d = chtodig(ch); if (d < 0 || d >= r) goto done;
342 if (u > ULONG_MAX/r) return (-1);
343 u *= r; if (u > ULONG_MAX - d) return (-1);
344 u += d; f = (f&~f_uscore) | f_digit; p++; q = p;
350 if (!(f&f_digit)) return (-1);
351 *u_out = u; *q_out = q; return (0);
358 static int parse_signed_integer(long *i_out, const char **q_out,
365 /* Read an initial sign. */
367 else if (*p == '-') { f |= f_neg; p++; }
369 /* Scan an unsigned number. */
370 if (parse_unsigned_integer(&u, q_out, p)) return (-1);
372 /* Check for signed overflow and apply the sign. */
374 if (u > LONG_MAX) return (-1);
377 if (u && u - 1 > -(LONG_MIN + 1)) return (-1);
378 *i_out = u ? -(long)(u - 1) - 1 : 0;
386 /* --- @parse_unsigned@, @parse_signed@ --- *
388 * Arguments: @unsigned long *u_out@ or @long *i_out@ = where to put the
390 * @const char *p@ = string to parse
391 * @const struct tvec_urange *ur@ or
392 * @const struct tvec_irange *ir@ = range specification,
394 * @struct tvec_state *tv@ = test vector state
396 * Returns: Zero on success, @-1@ on error.
398 * Use: Parse and range-check an integer. Unlike @parse_(un)signed_
399 * integer@, these functions check that there's no cruft
400 * following the final digit, and report errors as they find
401 * them rather than leaving that to the caller.
404 static int parse_unsigned(unsigned long *u_out, const char *p,
405 const struct tvec_urange *ur,
406 struct tvec_state *tv)
411 if (parse_unsigned_integer(&u, &q, p))
412 return (tvec_error(tv, "invalid unsigned integer `%s'", p));
413 if (*q) return (tvec_syntax(tv, *q, "end-of-line"));
414 if (check_unsigned_range(u, ur, tv, "integer")) return (-1);
415 *u_out = u; return (0);
418 static int parse_signed(long *i_out, const char *p,
419 const struct tvec_irange *ir,
420 struct tvec_state *tv)
425 if (parse_signed_integer(&i, &q, p))
426 return (tvec_error(tv, "invalid signed integer `%s'", p));
427 if (*q) return (tvec_syntax(tv, *q, "end-of-line"));
428 if (check_signed_range(i, ir, tv, "integer")) return (-1);
429 *i_out = i; return (0);
431 static const char size_units[] = "kMGTPEZY";
433 /* --- @parse_szint@ --- *
435 * Arguments: @struct tvec_state *tv@ = test-vector state
436 * @unsigned long *u_out@ = where to put the answer
437 * @const char *delims@ = delimiters
438 * @const char *what@ = description of what we're parsing
440 * Returns: Zero on success, %$-1$% on failure.
442 * Use: Parse a memory size.
445 static int parse_szint(struct tvec_state *tv, unsigned long *u_out,
446 const char *delims, const char *what)
449 const char *p, *unit;
455 if (tvec_readword(tv, &d, 0, delims, what)) { rc = -1; goto end; }
457 if (parse_unsigned_integer(&u, &p, p)) goto bad;
458 if (!*p) tvec_readword(tv, &d, &p, delims, 0);
460 for (t = u, unit = size_units; *unit; unit++) {
461 if (t > ULONG_MAX/1024) f |= f_range;
464 if (f&f_range) goto rangerr;
477 tvec_error(tv, "invalid %s `%s'", what, d.buf);
481 tvec_error(tv, "%s `%s' out of range", what, d.buf);
487 /* --- @format_size@ --- *
489 * Arguments: @const struct gprintf_ops *gops@ = print operations
490 * @void *go@ = print destination
491 * @unsigned long u@ = a size
492 * @unsigned style@ = style (@TVSF_...@)
496 * Use: Format @u@ as a size in bytes to the destination, expressing
497 * it with a unit prefix if this is possible exactly.
500 static void format_size(const struct gprintf_ops *gops, void *go,
501 unsigned long u, unsigned style)
506 gprintf(gops, go, "%lu", u);
507 else if (!u || u%1024)
508 gprintf(gops, go, "%lu%sB", u, style&TVSF_COMPACT ? "" : " ");
510 for (unit = size_units, u /= 1024;
511 !(u%1024) && unit[1];
513 gprintf(gops, go, "%lu%s%cB", u, style&TVSF_COMPACT ? "" : " ", *unit);
517 /*----- Floating-point utilities ------------------------------------------*/
519 /* --- @eqish_floating_p@ --- *
521 * Arguments: @double x, y@ = two numbers to compare
522 * @const struct tvec_floatinfo *fi@ = floating-point info
524 * Returns: Nonzero if the comparand @x@ is sufficiently close to the
525 * reference @y@, or zero if it's definitely different.
528 static int eqish_floating_p(double x, double y,
529 const struct tvec_floatinfo *fi)
533 /* NaNs and infinities are equal only to each other. */
534 if (NANP(x)) return (NANP(y)); else if (NANP(y)) return (0);
535 if (INFP(x)) return (x == y); else if (INFP(y)) return (0);
537 /* Compare finite values. */
538 switch (fi ? fi->f&TVFF_EQMASK : TVFF_EXACT) {
540 return (x == y && NEGP(x) == NEGP(y));
542 t = fabs(y - x); return (t < fi->delta);
544 t = fabs(y - x); u = fabs(y*fi->delta); if (u < DBL_MIN) u = DBL_MIN;
551 /* --- @format_floating@ --- *
553 * Arguments: @const struct gprintf_ops *gops@ = print operations
554 * @void *go@ = print destination
555 * @double x@ = number to print
559 * Use: Print a floating-point number, accurately.
562 static void format_floating(const struct gprintf_ops *gops, void *go,
568 gprintf(gops, go, "#nan");
570 gprintf(gops, go, x > 0 ? "#+inf" : "#-inf");
572 /* Ugh. C doesn't provide any function for just printing a
573 * floating-point number /correctly/, i.e., so that you can read the
574 * result back and recover the number you first thought of. There are
575 * complicated algorithms published for doing this, but I really don't
576 * want to get into that here. So we have this.
578 * The sign doesn't cause significant difficulty so we're going to ignore
579 * it for now. So suppose we're given a number %$x = f b^e$%, in
580 * base-%$b$% format, so %$f b^n$% and %$e$% are integers, with
581 * %$0 \le f < 1$%. We're going to convert it into the nearest integer
582 * of the form %$X = F B^E$%, with similar conditions, only with the
583 * additional requirement that %$X$% is normalized, i.e., that %$X = 0$%
584 * or %$F \ge B^{-N}$%.
586 * We're rounding to the nearest such %$X$%. If there is to be ambiguity
587 * in the conversion, then some %$x = f b^e$% and the next smallest
588 * representable number %$x' = x + b^{e-n}$% must both map to the same
589 * %$X$%, which means both %$x$% and %$x'$% must be nearer to %$X$% than
590 * any other number representable in the target system. The nest larger
591 * number is %$X' = X + B^{E-N}$%; the next smaller number will normally
592 * be %$W = X - B^{E-N}$%, but if %$F = 1/B$ then the next smaller number
593 * is actually %$X - B^{E-N-1}$%. We ignore this latter possibility in
594 * the pursuit of a conservative estimate (though actually it doesn't
597 * If both %$x$% and %$x'$% map to %$X$% then we must have
598 * %$L = X - B^{E-N}/2 \le x$% and %$x + b^{e-n} \le R = X + B^{E-N}/2$%;
599 * so firstly %$f b^e = x \ge L = W + B^{E-N}/2 > W = (F - B^{-N}) B^E$%,
600 * and secondly %$b^{e-n} \le B^{E-N}$%. Since these inequalities are in
601 * opposite senses, we can divide, giving
603 * %$f b^e/b^{e-n} > (F - B^{-N}) B^E/B^{E-N}$% ,
607 * %$f b^n > (F - B^{-N}) B^N = F B^N - 1$% .
609 * Now %$f \le 1 - b^{-n}$%, and %$F \ge B^{-1}$%, so, for this to be
610 * possible, it must be the case that
612 * %$(1 - b^{-n}) b^n = b^n - 1 > B^{N-1} - 1$% .
614 * Then rearrange and take logarithms, obtaining
616 * %$(N - 1) \log B < n \log b$% ,
620 * %$N < n \log b/\log B + 1$% .
622 * Recall that this is a necessary condition for a collision to occur; we
623 * are therefore safe whenever
625 * %$N \ge n \log b/\log B + 1$% ;
627 * so, taking ceilings,
629 * %$N \ge \lceil n \log b/\log B \rceil + 1$% .
631 * So that's why we have this.
633 * I'm going to assume that @n = DBL_MANT_DIG@ is sufficiently small
634 * that we can calculate this without ending up on the wrong side of an
637 * In C11, we have @DBL_DECIMAL_DIG@, which should be the same value
638 * only as a constant. Except that modern compilers are more than clever
639 * enough to work out that this is a constant anyway.
641 * This is sometimes an overestimate: we'll print out meaningless digits
642 * that don't represent anything we actually know about the number in
643 * question. To fix that, we'd need a complicated algorithm like Steele
644 * and White's Dragon4, Gay's @dtoa@, or Burger and Dybvig's algorithm
645 * (note that Loitsch's Grisu2 is conservative, and Grisu3 hands off to
646 * something else in difficult situations).
649 #ifdef DBL_DECIMAL_DIG
650 prec = DBL_DECIMAL_DIG;
652 prec = ceil(DBL_MANT_DIG*log(FLT_RADIX)/log(10)) + 1;
654 gprintf(gops, go, "%.*g", prec, x);
658 /* --- @parse_floating@ --- *
660 * Arguments: @double *x_out@ = where to put the result
661 * @const char *q_out@ = where to leave end pointer, or null
662 * @const char *p@ = string to parse
663 * @const struct tvec_floatinfo *fi@ = floating-point info
664 * @struct tvec_state *tv@ = test vector state
666 * Returns: Zero on success, @-1@ on error.
668 * Use: Parse a floating-point number from a string. Reports any
669 * necessary errors. If @q_out@ is not null then trailing
670 * material is permitted and a pointer to it (or the end of the
671 * string) is left in @*q_out@.
674 static int parse_floating(double *x_out, const char **q_out, const char *p,
675 const struct tvec_floatinfo *fi,
676 struct tvec_state *tv)
678 const char *pp; char *q;
683 /* Check for special tokens. */
684 if (STRCMP(p, ==, "#nan")) {
686 if (q_out) *q_out = p + strlen(p);
689 tvec_error(tv, "NaN not supported on this system");
694 else if (STRCMP(p, ==, "#inf") ||
695 STRCMP(p, ==, "#+inf") || STRCMP(p, ==, "+#inf")) {
697 if (q_out) *q_out = p + strlen(p);
698 x = INFINITY; rc = 0;
700 tvec_error(tv, "infinity not supported on this system");
705 else if (STRCMP(p, ==, "#-inf") || STRCMP(p, ==, "-#inf")) {
707 if (q_out) *q_out = p + strlen(p);
708 x = -INFINITY; rc = 0;
710 tvec_error(tv, "infinity not supported on this system");
715 /* Check that this looks like a number, so we can exclude `strtod'
716 * recognizing its own non-finite number tokens.
720 if (*pp == '+' || *pp == '-') pp++;
721 if (*pp == '.') pp++;
723 tvec_syntax(tv, *p ? *p : fgetc(tv->fp), "floating-point number");
727 /* Parse the number using the system parser. */
728 olderr = errno; errno = 0;
729 #if __STDC_VERSION__ >= 199901
734 if (q_out) *q_out = q;
735 else if (*q) { tvec_syntax(tv, *q, "end-of-line"); rc = -1; goto end; }
736 if (errno && (errno != ERANGE || (x > 0 ? -x : x) == HUGE_VAL)) {
737 tvec_error(tv, "invalid floating-point number `%.*s': %s",
738 (int)(q - p), p, strerror(errno));
744 /* Check that the number is acceptable. */
745 if (NANP(x) && fi && !(fi->f&TVFF_NANOK)) {
746 tvec_error(tv, "#nan not allowed here");
751 ((!(fi->f&TVFF_NOMIN) && x < fi->min) ||
752 (!(fi->f&TVFF_NOMAX) && x > fi->max)) &&
753 !(INFP(x) && (fi->f&(NEGP(x) ? TVFF_NEGINFOK : TVFF_POSINFOK)))) {
754 dstr_puts(&d, "floating-point number ");
755 format_floating(&dstr_printops, &d, x);
756 dstr_puts(&d, " out of range (must be in ");
757 if (fi->f&TVFF_NOMIN)
758 dstr_puts(&d, "(#-inf");
760 { dstr_putc(&d, '['); format_floating(&dstr_printops, &d, fi->min); }
761 dstr_puts(&d, " .. ");
762 if (fi->f&TVFF_NOMAX)
763 dstr_puts(&d, "#+inf)");
765 { format_floating(&dstr_printops, &d, fi->max); dstr_putc(&d, ']'); }
766 dstr_putc(&d, ')'); dstr_putz(&d);
767 tvec_error(tv, "%s", d.buf); rc = -1; goto end;
777 /*----- String utilities --------------------------------------------------*/
779 /* Special character name table. */
780 static const struct chartab {
781 const char *name; /* character name */
782 int ch; /* character value */
783 unsigned f; /* flags: */
784 #define CTF_PREFER 1u /* preferred name */
785 #define CTF_SHORT 2u /* short name (compact style) */
787 { "#eof", EOF, CTF_PREFER | CTF_SHORT },
788 { "#nul", '\0', CTF_PREFER },
789 { "#bell", '\a', CTF_PREFER },
790 { "#ding", '\a', 0 },
791 { "#bel", '\a', CTF_SHORT },
792 { "#backspace", '\b', CTF_PREFER },
793 { "#bs", '\b', CTF_SHORT },
794 { "#escape", '\x1b', CTF_PREFER },
795 { "#esc", '\x1b', CTF_SHORT },
796 { "#formfeed", '\f', CTF_PREFER },
797 { "#ff", '\f', CTF_SHORT },
798 { "#newline", '\n', CTF_PREFER },
799 { "#linefeed", '\n', 0 },
800 { "#lf", '\n', CTF_SHORT },
802 { "#return", '\r', CTF_PREFER },
803 { "#carriage-return", '\r', 0 },
804 { "#cr", '\r', CTF_SHORT },
805 { "#tab", '\t', CTF_PREFER | CTF_SHORT },
806 { "#horizontal-tab", '\t', 0 },
808 { "#vertical-tab", '\v', CTF_PREFER },
809 { "#vt", '\v', CTF_SHORT },
810 { "#space", ' ', 0 },
811 { "#spc", ' ', CTF_SHORT },
812 { "#delete", '\x7f', CTF_PREFER },
813 { "#del", '\x7f', CTF_SHORT },
817 /* --- @find_charname@ --- *
819 * Arguments: @int ch@ = character to match
820 * @unsigned f@ = flags (@CTF_...@) to match
822 * Returns: The name of the character, or null if no match is found.
824 * Use: Looks up a name for a character. Specifically, it returns
825 * the first entry in the @chartab@ table which matches @ch@ and
826 * which has one of the flags @f@ set.
829 static const char *find_charname(int ch, unsigned f)
831 const struct chartab *ct;
833 for (ct = chartab; ct->name; ct++)
834 if (ct->ch == ch && (ct->f&f)) return (ct->name);
838 /* --- @read_charname@ --- *
840 * Arguments: @int *ch_out@ = where to put the character
841 * @const char *p@ = character name
842 * @unsigned f@ = flags (@TCF_...@)
844 * Returns: Zero if a match was found, @-1@ if not.
846 * Use: Looks up a character by name. If @RCF_EOFOK@ is set in @f@,
847 * then the @EOF@ marker can be matched; otherwise it can't.
851 static int read_charname(int *ch_out, const char *p, unsigned f)
853 const struct chartab *ct;
855 for (ct = chartab; ct->name; ct++)
856 if (STRCMP(p, ==, ct->name) && ((f&RCF_EOFOK) || ct->ch >= 0))
857 { *ch_out = ct->ch; return (0); }
861 /* --- @format_charesc@ --- *
863 * Arguments: @const struct gprintf_ops *gops@ = print operations
864 * @void *go@ = print destination
865 * @int ch@ = character to format
866 * @unsigned f@ = flags (@FCF_...@)
870 * Use: Format a character as an escape sequence, possibly as part of
871 * a larger string. If @FCF_BRACE@ is set in @f@, then put
872 * braces around a `\x...' code, so that it's suitable for use
873 * in a longer string.
877 static void format_charesc(const struct gprintf_ops *gops, void *go,
881 case '\a': gprintf(gops, go, "\\a"); break;
882 case '\b': gprintf(gops, go, "\\b"); break;
883 case '\x1b': gprintf(gops, go, "\\e"); break;
884 case '\f': gprintf(gops, go, "\\f"); break;
885 case '\r': gprintf(gops, go, "\\r"); break;
886 case '\n': gprintf(gops, go, "\\n"); break;
887 case '\t': gprintf(gops, go, "\\t"); break;
888 case '\v': gprintf(gops, go, "\\v"); break;
889 case '\\': gprintf(gops, go, "\\\\"); break;
890 case '\'': gprintf(gops, go, "\\'"); break;
892 if (f&FCF_BRACE) gprintf(gops, go, "\\{0}");
893 else gprintf(gops, go, "\\0");
897 gprintf(gops, go, "\\x{%0*x}", hex_width(UCHAR_MAX), ch);
899 gprintf(gops, go, "\\x%0*x", hex_width(UCHAR_MAX), ch);
904 /* --- @format_char@ --- *
906 * Arguments: @const struct gprintf_ops *gops@ = print operations
907 * @void *go@ = print destination
908 * @int ch@ = character to format
912 * Use: Format a single character.
915 static void format_char(const struct gprintf_ops *gops, void *go, int ch)
918 case '\\': case '\'': escape:
919 gprintf(gops, go, "'");
920 format_charesc(gops, go, ch, 0);
921 gprintf(gops, go, "'");
924 if (!isprint(ch)) goto escape;
925 gprintf(gops, go, "'%c'", ch);
930 /* --- @fill_pattern@ --- *
932 * Arguments: @void *p@ = destination pointer
933 * @size_t sz@ = destination buffer size
934 * @const void *pat@ = pointer to pattern
935 * @size_t patsz@ = pattern size
939 * Use: Fill the destination buffer with as many copies of the
940 * pattern as will fit, followed by as many initial bytes of the
941 * pattern will fit in the remaining space.
944 static void fill_pattern(void *p, size_t sz, const void *pat, size_t patsz)
946 unsigned char *q = p;
949 memset(q, *(unsigned char *)pat, sz);
952 memcpy(q, pat, patsz); pat = q; q += patsz; sz -= patsz;
954 { memcpy(q, pat, patsz); q += patsz; sz -= patsz; patsz *= 2; }
960 /* --- @maybe_format_unsigned_char@, @maybe_format_signed_char@ --- *
962 * Arguments: @const struct gprintf_ops *gops@ = print operations
963 * @void *go@ = print destination
964 * @unsigned long u@ or @long i@ = an integer
968 * Use: Format a (signed or unsigned) integer as a character, if it's
969 * in range, printing something like `= 'q''. It's assumed that
970 * a comment marker has already been output.
973 static void maybe_format_unsigned_char
974 (const struct gprintf_ops *gops, void *go, unsigned long u)
978 p = find_charname(u, CTF_PREFER);
979 if (p) gprintf(gops, go, " = %s", p);
981 { gprintf(gops, go, " = "); format_char(gops, go, u); }
984 static void maybe_format_signed_char
985 (const struct gprintf_ops *gops, void *go, long i)
989 p = find_charname(i, CTF_PREFER);
990 if (p) gprintf(gops, go, " = %s", p);
991 if (0 <= i && i < UCHAR_MAX)
992 { gprintf(gops, go, " = "); format_char(gops, go, i); }
995 /* --- @read_charesc@ --- *
997 * Arguments: @int *ch_out@ = where to put the result
998 * @struct tvec_state *tv@ = test vector state
1000 * Returns: Zero on success, @-1@ on error.
1002 * Use: Parse and convert an escape sequence from @tv@'s input
1003 * stream, assuming that the initial `\' has already been read.
1004 * Reports errors as appropriate.
1007 static int read_charesc(int *ch_out, struct tvec_state *tv)
1016 /* Things we shouldn't find. */
1017 case EOF: case '\n': return (tvec_syntax(tv, ch, "string escape"));
1019 /* Single-character escapes. */
1020 case '\'': *ch_out = '\''; break;
1021 case '\\': *ch_out = '\\'; break;
1022 case '"': *ch_out = '"'; break;
1023 case 'a': *ch_out = '\a'; break;
1024 case 'b': *ch_out = '\b'; break;
1025 case 'e': *ch_out = '\x1b'; break;
1026 case 'f': *ch_out = '\f'; break;
1027 case 'n': *ch_out = '\n'; break;
1028 case 'r': *ch_out = '\r'; break;
1029 case 't': *ch_out = '\t'; break;
1030 case 'v': *ch_out = '\v'; break;
1032 /* Hex escapes, with and without braces. */
1035 if (ch == '{') { f |= f_brace; ch = getc(tv->fp); }
1038 if (esc < 0 || esc >= 16) return (tvec_syntax(tv, ch, "hex digit"));
1040 ch = getc(tv->fp); i = chtodig(ch); if (i < 0 || i >= 16) break;
1042 if (esc > UCHAR_MAX)
1043 return (tvec_error(tv,
1044 "character code %d out of range", esc));
1046 if (!(f&f_brace)) ungetc(ch, tv->fp);
1047 else if (ch != '}') return (tvec_syntax(tv, ch, "`}'"));
1051 /* Other things, primarily octal escapes. */
1053 f |= f_brace; ch = getc(tv->fp);
1056 if ('0' <= ch && ch < '8') {
1057 i = 1; esc = ch - '0';
1060 if ('0' > ch || ch >= '8') { ungetc(ch, tv->fp); break; }
1061 esc = 8*esc + ch - '0';
1062 i++; if (i >= 3) break;
1066 if (ch != '}') return (tvec_syntax(tv, ch, "`}'"));
1068 if (esc > UCHAR_MAX)
1069 return (tvec_error(tv,
1070 "character code %d out of range", esc));
1071 *ch_out = esc; break;
1073 return (tvec_syntax(tv, ch, "string escape"));
1082 /* --- @read_quoted_string@ --- *
1084 * Arguments: @dstr *d@ = string to write to
1085 * @int quote@ = initial quote, `'' or `"'
1086 * @struct tvec_state *tv@ = test vector state
1088 * Returns: Zero on success, @-1@ on error.
1090 * Use: Read the rest of a quoted string into @d@, reporting errors
1093 * A single-quoted string is entirely literal. A double-quoted
1094 * string may contain C-like escapes.
1097 static int read_quoted_string(dstr *d, int quote, struct tvec_state *tv)
1104 case EOF: case '\n':
1105 return (tvec_syntax(tv, ch, "`%c'", quote));
1107 if (quote == '\'') goto ordinary;
1108 ch = getc(tv->fp); if (ch == '\n') { tv->lno++; break; }
1109 ungetc(ch, tv->fp); if (read_charesc(&ch, tv)) return (-1);
1112 if (ch == quote) goto end;
1124 /* --- @collect_bare@ --- *
1126 * Arguments: @dstr *d@ = string to write to
1127 * @struct tvec_state *tv@ = test vector state
1129 * Returns: Zero on success, @-1@ on error.
1131 * Use: Read barewords and the whitespace between them. Stop when we
1132 * encounter something which can't start a bareword.
1135 static int collect_bare(dstr *d, struct tvec_state *tv)
1137 size_t pos = d->len;
1138 enum { WORD, SPACE, ESCAPE }; unsigned s = WORD;
1145 tvec_syntax(tv, ch, "bareword");
1148 if (s == ESCAPE) { tv->lno++; goto addch; }
1149 if (s == WORD) pos = d->len;
1150 ungetc(ch, tv->fp); if (tvec_nexttoken(tv)) { rc = -1; goto end; }
1151 DPUTC(d, ' '); s = SPACE;
1153 case '"': case '\'': case '!': case '#': case ')': case '}': case ']':
1154 if (s == SPACE) { ungetc(ch, tv->fp); goto done; }
1160 if (s != ESCAPE && isspace(ch)) {
1161 if (s == WORD) pos = d->len;
1162 DPUTC(d, ch); s = SPACE;
1166 DPUTC(d, ch); s = WORD;
1171 if (s == SPACE) d->len = pos;
1177 /* --- @set_up_encoding@ --- *
1179 * Arguments: @const codec_class **ccl_out@ = where to put the class
1180 * @unsigned *f_out@ = where to put the flags
1181 * @unsigned code@ = the coding scheme to use (@TVEC_...@)
1185 * Use: Helper for @read_compound_string@ below.
1187 * Return the appropriate codec class and flags for @code@.
1188 * Leaves @*ccl_out@ null if the coding scheme doesn't have a
1189 * backing codec class (e.g., @TVCODE_BARE@).
1192 enum { TVCODE_BARE, TVCODE_HEX, TVCODE_BASE64, TVCODE_BASE32 };
1193 static void set_up_encoding(const codec_class **ccl_out, unsigned *f_out,
1198 *ccl_out = 0; *f_out = 0;
1201 *ccl_out = &hex_class; *f_out = CDCF_IGNCASE;
1204 *ccl_out = &base32_class; *f_out = CDCF_IGNCASE | CDCF_IGNEQPAD;
1207 *ccl_out = &base64_class; *f_out = CDCF_IGNEQPAD;
1214 /* --- @flush_codec@ --- *
1216 * Arguments: @codec *cdc@ = a codec, or null
1217 * @dstr *d@ = output string
1218 * @struct tvec_state *tv@ = test vector state
1220 * Returns: Zero on success, @-1@ on error.
1222 * Use: Helper for @read_compound_string@ below.
1224 * Flush out any final buffered material from @cdc@, and check
1225 * that it's in a good state. Frees the codec on success. Does
1226 * nothing if @cdc@ is null.
1229 static int flush_codec(codec *cdc, dstr *d, struct tvec_state *tv)
1234 err = cdc->ops->code(cdc, 0, 0, d);
1236 return (tvec_error(tv, "invalid %s sequence end: %s",
1237 cdc->ops->c->name, codec_strerror(err)));
1238 cdc->ops->destroy(cdc);
1243 /* --- @read_compound_string@ --- *
1245 * Arguments: @void **p_inout@ = address of output buffer pointer
1246 * @size_t *sz_inout@ = address of buffer size
1247 * @unsigned code@ = initial interpretation of barewords
1248 * @unsigned f@ = other flags (@RCSF_...@)
1249 * @struct tvec_state *tv@ = test vector state
1251 * Returns: Zero on success, @-1@ on error.
1253 * Use: Parse a compound string, i.e., a sequence of stringish pieces
1254 * which might be quoted strings, character names, or barewords
1255 * to be decoded accoding to @code@, interspersed with
1256 * additional directives.
1258 * If the initial buffer pointer is non-null and sufficiently
1259 * large, then it will be reused; otherwise, it is freed and a
1260 * fresh, sufficiently large buffer is allocated and returned.
1261 * This buffer unconditionally uses the standard-library arena.
1264 #define RCSF_NESTED 1u
1265 static int read_compound_string(void **p_inout, size_t *sz_inout,
1266 unsigned code, unsigned f,
1267 struct tvec_state *tv)
1269 const codec_class *ccl; unsigned cdf;
1271 dstr d = DSTR_INIT, w = DSTR_INIT;
1274 void *pp = 0; size_t sz;
1278 set_up_encoding(&ccl, &cdf, code); cdc = 0;
1280 if (tvec_nexttoken(tv)) return (tvec_syntax(tv, fgetc(tv->fp), "string"));
1285 case ')': case ']': case '}':
1286 /* Close brackets. Leave these for recursive caller if there is one,
1290 if (!(f&RCSF_NESTED))
1291 { rc = tvec_syntax(tv, ch, "string"); goto end; }
1292 ungetc(ch, tv->fp); goto done;
1294 case '"': case '\'':
1295 /* Quotes. Read a quoted string. */
1297 if (cdc && flush_codec(cdc, &d, tv)) { rc = -1; goto end; }
1299 if (read_quoted_string(&d, ch, tv)) { rc = -1; goto end; }
1303 /* A named character. */
1306 if (cdc && flush_codec(cdc, &d, tv)) { rc = -1; goto end; }
1308 DRESET(&w); tvec_readword(tv, &w, 0, ";", "character name");
1309 if (STRCMP(w.buf, ==, "#empty")) break;
1310 if (read_charname(&ch, w.buf, RCF_EOFOK)) {
1311 rc = tvec_error(tv, "unknown character name `%s'", d.buf);
1314 DPUTC(&d, ch); break;
1317 /* A magic keyword. */
1319 if (cdc && flush_codec(cdc, &d, tv)) { rc = -1; goto end; }
1322 DRESET(&w); tvec_readword(tv, &w, 0, ";", "`!'-keyword");
1324 /* Change bareword coding system. */
1325 if (STRCMP(w.buf, ==, "!bare"))
1326 { code = TVCODE_BARE; set_up_encoding(&ccl, &cdf, code); }
1327 else if (STRCMP(w.buf, ==, "!hex"))
1328 { code = TVCODE_HEX; set_up_encoding(&ccl, &cdf, code); }
1329 else if (STRCMP(w.buf, ==, "!base32"))
1330 { code = TVCODE_BASE32; set_up_encoding(&ccl, &cdf, code); }
1331 else if (STRCMP(w.buf, ==, "!base64"))
1332 { code = TVCODE_BASE64; set_up_encoding(&ccl, &cdf, code); }
1334 /* Repeated substrings. */
1335 else if (STRCMP(w.buf, ==, "!repeat")) {
1336 if (tvec_nexttoken(tv)) {
1337 rc = tvec_syntax(tv, fgetc(tv->fp), "repeat count");
1341 if (tvec_readword(tv, &w, 0, ";{", "repeat count"))
1342 { rc = -1; goto end; }
1343 if (parse_unsigned_integer(&n, &q, w.buf)) {
1344 rc = tvec_error(tv, "invalid repeat count `%s'", w.buf);
1347 if (*q) { rc = tvec_syntax(tv, *q, "`{'"); goto end; }
1348 if (tvec_nexttoken(tv))
1349 { rc = tvec_syntax(tv, fgetc(tv->fp), "`{'"); goto end; }
1350 ch = getc(tv->fp); if (ch != '{')
1351 { rc = tvec_syntax(tv, ch, "`{'"); goto end; }
1353 if (read_compound_string(&pp, &sz, code, f | RCSF_NESTED, tv))
1354 { rc = -1; goto end; }
1355 ch = getc(tv->fp); if (ch != '}')
1356 { rc = tvec_syntax(tv, ch, "`}'"); goto end; }
1358 if (n > (size_t)-1/sz)
1359 { rc = tvec_error(tv, "repeat size out of range"); goto end; }
1362 fill_pattern(d.buf + d.len, n, pp, sz); d.len += n;
1367 /* Anything else is an error. */
1369 tvec_error(tv, "unknown string keyword `%s'", w.buf);
1375 /* A bareword. Process it according to the current coding system. */
1380 if (collect_bare(&d, tv)) goto done;
1384 ungetc(ch, tv->fp); DRESET(&w);
1385 if (tvec_readword(tv, &w, 0, ";",
1386 "%s-encoded fragment", ccl->name))
1387 { rc = -1; goto end; }
1388 if (!cdc) cdc = ccl->decoder(cdf);
1389 err = cdc->ops->code(cdc, w.buf, w.len, &d);
1391 tvec_error(tv, "invalid %s fragment `%s': %s",
1392 ccl->name, w.buf, codec_strerror(err));
1399 } while (!tvec_nexttoken(tv));
1402 /* Wrap things up. */
1403 if (cdc && flush_codec(cdc, &d, tv)) { rc = -1; goto end; }
1405 if (*sz_inout <= d.len)
1406 { free(*p_inout); *p_inout = x_alloc(&arena_stdlib, d.len + 1); }
1407 p = *p_inout; memcpy(p, d.buf, d.len); p[d.len] = 0; *sz_inout = d.len;
1411 /* Clean up any debris. */
1412 if (cdc) cdc->ops->destroy(cdc);
1414 dstr_destroy(&d); dstr_destroy(&w);
1418 /*----- Signed and unsigned integer types ---------------------------------*/
1420 /* --- @init_int@, @init_uint@ --- *
1422 * Arguments: @union tvec_regval *rv@ = register value
1423 * @const struct tvec_regdef *rd@ = register definition
1427 * Use: Initialize a register value.
1429 * Integer values are initialized to zero.
1432 static void init_int(union tvec_regval *rv, const struct tvec_regdef *rd)
1435 static void init_uint(union tvec_regval *rv, const struct tvec_regdef *rd)
1438 /* --- @eq_int@, @eq_uint@ --- *
1440 * Arguments: @const union tvec_regval *rv0, *rv1@ = register values
1441 * @const struct tvec_regdef *rd@ = register definition
1443 * Returns: Nonzero if the values are equal, zero if unequal
1445 * Use: Compare register values for equality.
1448 static int eq_int(const union tvec_regval *rv0, const union tvec_regval *rv1,
1449 const struct tvec_regdef *rd)
1450 { return (rv0->i == rv1->i); }
1452 static int eq_uint(const union tvec_regval *rv0,
1453 const union tvec_regval *rv1,
1454 const struct tvec_regdef *rd)
1455 { return (rv0->u == rv1->u); }
1457 /* --- @copy_int@, @copy_uint@ --- *
1459 * Arguments: @union tvec_regval *rvd@ = destination register value
1460 * @const union tvec_regval *rvs@ = source register value
1461 * @const struct tvec_regdef *rd@ = register definition
1465 * Use: Copy a register value.
1468 static void copy_int(union tvec_regval *rvd, const union tvec_regval *rvs,
1469 const struct tvec_regdef *rd)
1470 { rvd->i = rvs->i; }
1472 static void copy_uint(union tvec_regval *rvd, const union tvec_regval *rvs,
1473 const struct tvec_regdef *rd)
1474 { rvd->u = rvs->u; }
1476 /* --- @tobuf_int@, @tobuf_uint@ --- *
1478 * Arguments: @buf *b@ = buffer
1479 * @const union tvec_regval *rv@ = register value
1480 * @const struct tvec_regdef *rd@ = register definition
1482 * Returns: Zero on success, %$-1$% on failure.
1484 * Use: Serialize a register value to a buffer.
1486 * Integer values are serialized as little-endian 64-bit signed
1487 * or unsigned integers.
1490 static int tobuf_int(buf *b, const union tvec_regval *rv,
1491 const struct tvec_regdef *rd)
1492 { return (signed_to_buf(b, rv->i)); }
1494 static int tobuf_uint(buf *b, const union tvec_regval *rv,
1495 const struct tvec_regdef *rd)
1496 { return (unsigned_to_buf(b, rv->u)); }
1498 /* --- @frombuf_int@, @frombuf_uint@ --- *
1500 * Arguments: @buf *b@ = buffer
1501 * @union tvec_regval *rv@ = register value
1502 * @const struct tvec_regdef *rd@ = register definition
1504 * Returns: Zero on success, %$-1$% on failure.
1506 * Use: Deserialize a register value from a buffer.
1508 * Integer values are serialized as 64-bit signed or unsigned
1512 static int frombuf_int(buf *b, union tvec_regval *rv,
1513 const struct tvec_regdef *rd)
1514 { return (signed_from_buf(b, &rv->i)); }
1516 static int frombuf_uint(buf *b, union tvec_regval *rv,
1517 const struct tvec_regdef *rd)
1518 { return (unsigned_from_buf(b, &rv->u)); }
1520 /* --- @parse_int@, @parse_uint@ --- *
1522 * Arguments: @union tvec_regval *rv@ = register value
1523 * @const struct tvec_regdef *rd@ = register definition
1524 * @struct tvec_state *tv@ = test-vector state
1526 * Returns: Zero on success, %$-1$% on error.
1528 * Use: Parse a register value from an input file.
1530 * Integers may be input in decimal, hex, binary, or octal,
1531 * following approximately usual conventions.
1533 * * Signed integers may be preceded with a `+' or `-' sign.
1535 * * Decimal integers are just a sequence of decimal digits
1538 * * Octal integers are a sequence of digits `0' ... `7',
1539 * preceded by `0o' or `0O'.
1541 * * Hexadecimal integers are a sequence of digits `0'
1542 * ... `9', `a' ... `f', or `A' ... `F', preceded by `0x' or
1545 * * Radix-B integers are a sequence of digits `0' ... `9',
1546 * `a' ... `f', or `A' ... `F', each with value less than B,
1547 * preceded by `Br' or `BR', where 0 < B < 36 is expressed
1548 * in decimal without any leading `0' or internal
1551 * * A digit sequence may contain internal underscore `_'
1552 * separators, but not before or after all of the digits;
1553 * and two consecutive `_' characters are not permitted.
1556 static int parse_int(union tvec_regval *rv, const struct tvec_regdef *rd,
1557 struct tvec_state *tv)
1562 if (tvec_readword(tv, &d, 0, ";", "signed integer"))
1563 { rc = -1; goto end; }
1564 if (parse_signed(&rv->i, d.buf, rd->arg.p, tv)) { rc = -1; goto end; }
1571 static int parse_uint(union tvec_regval *rv, const struct tvec_regdef *rd,
1572 struct tvec_state *tv)
1577 if (tvec_readword(tv, &d, 0, ";", "unsigned integer"))
1578 { rc = -1; goto end; }
1579 if (parse_unsigned(&rv->u, d.buf, rd->arg.p, tv)) { rc = -1; goto end; }
1586 /* --- @dump_int@, @dump_uint@ --- *
1588 * Arguments: @const union tvec_regval *rv@ = register value
1589 * @const struct tvec_regdef *rd@ = register definition
1590 * @unsigned style@ = output style (@TVSF_...@)
1591 * @const struct gprintf_ops *gops@, @void *gp@ = format output
1595 * Use: Dump a register value to the format output.
1597 * Integer values are dumped in decimal and, unless compact
1598 * output is requested, hex, and maybe a character, as a
1602 static void dump_int(const union tvec_regval *rv,
1603 const struct tvec_regdef *rd,
1605 const struct gprintf_ops *gops, void *go)
1607 if (style&TVSF_RAW) gprintf(gops, go, "int:");
1608 gprintf(gops, go, "%ld", rv->i);
1609 if (!(style&(TVSF_COMPACT | TVSF_RAW))) {
1610 gprintf(gops, go, " ; = ");
1611 format_signed_hex(gops, go, rv->i);
1612 maybe_format_signed_char(gops, go, rv->i);
1616 static void dump_uint(const union tvec_regval *rv,
1617 const struct tvec_regdef *rd,
1619 const struct gprintf_ops *gops, void *go)
1621 if (style&TVSF_RAW) gprintf(gops, go, "uint:");
1622 gprintf(gops, go, "%lu", rv->u);
1623 if (!(style&(TVSF_COMPACT | TVSF_RAW))) {
1624 gprintf(gops, go, " ; = ");
1625 format_unsigned_hex(gops, go, rv->u);
1626 maybe_format_unsigned_char(gops, go, rv->u);
1630 /* Integer type definitions. */
1631 const struct tvec_regty tvty_int = {
1632 init_int, trivial_release, eq_int, copy_int,
1633 tobuf_int, frombuf_int,
1636 const struct tvec_regty tvty_uint = {
1637 init_uint, trivial_release, eq_uint, copy_uint,
1638 tobuf_uint, frombuf_uint,
1639 parse_uint, dump_uint
1642 /* Predefined integer ranges. */
1643 const struct tvec_irange
1644 tvrange_schar = { SCHAR_MIN, SCHAR_MAX, 0, 0 },
1645 tvrange_short = { SHRT_MIN, SHRT_MAX, 0, 0 },
1646 tvrange_int = { INT_MIN, INT_MAX, 0, 0 },
1647 tvrange_long = { LONG_MIN, LONG_MAX, 0, 0 },
1648 tvrange_sbyte = { -128, 127, 0, 0 },
1649 tvrange_i16 = { -32768, +32767, 0, 0 },
1650 tvrange_i32 = { -2147483648, 2147483647, 0, 0 };
1651 const struct tvec_urange
1652 tvrange_uchar = { 0, UCHAR_MAX, 0, 0 },
1653 tvrange_ushort = { 0, USHRT_MAX, 0, 0 },
1654 tvrange_uint = { 0, UINT_MAX, 0, 0 },
1655 tvrange_ulong = { 0, ULONG_MAX, 0, 0 },
1656 tvrange_size = { 0, (size_t)-1, 0, 0 },
1657 tvrange_byte = { 0, 255, 0, 0 },
1658 tvrange_u16 = { 0, 65535, 0, 0 },
1659 tvrange_u32 = { 0, 4294967295, 0, 0 };
1661 /* --- @tvec_claimeq_int@ --- *
1663 * Arguments: @struct tvec_state *tv@ = test-vector state
1664 * @long i0, i1@ = two signed integers
1665 * @const char *file@, @unsigned @lno@ = calling file and line
1666 * @const char *expr@ = the expression to quote on failure
1668 * Returns: Nonzero if @i0@ and @i1@ are equal, otherwise zero.
1670 * Use: Check that values of @i0@ and @i1@ are equal. As for
1671 * @tvec_claim@ above, a test case is automatically begun and
1672 * ended if none is already underway. If the values are
1673 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
1674 * mismatched values are dumped: @i0@ is printed as the output
1675 * value and @i1@ is printed as the input reference.
1678 int tvec_claimeq_int(struct tvec_state *tv, long i0, long i1,
1679 const char *file, unsigned lno, const char *expr)
1681 struct tvec_reg rval, rref;
1683 rval.f = rref.f = TVRF_LIVE; rval.v.i = i0; rref.v.i = i1;
1684 return (tvec_claimeq(tv, &tvty_int, 0, &rval, &rref, file, lno, expr));
1687 /* --- @tvec_claimeq_uint@ --- *
1689 * Arguments: @struct tvec_state *tv@ = test-vector state
1690 * @unsigned long u0, u1@ = two unsigned integers
1691 * @const char *file@, @unsigned @lno@ = calling file and line
1692 * @const char *expr@ = the expression to quote on failure
1694 * Returns: Nonzero if @u0@ and @u1@ are equal, otherwise zero.
1696 * Use: Check that values of @u0@ and @u1@ are equal. As for
1697 * @tvec_claim@ above, a test case is automatically begun and
1698 * ended if none is already underway. If the values are
1699 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
1700 * mismatched values are dumped: @u0@ is printed as the output
1701 * value and @u1@ is printed as the input reference.
1704 int tvec_claimeq_uint(struct tvec_state *tv,
1705 unsigned long u0, unsigned long u1,
1706 const char *file, unsigned lno, const char *expr)
1708 struct tvec_reg rval, rref;
1710 rval.f = rref.f = TVRF_LIVE; rval.v.u = u0; rref.v.u = u1;
1711 return (tvec_claimeq(tv, &tvty_uint, 0, &rval, &rref, file, lno, expr));
1714 /*----- Size type ---------------------------------------------------------*/
1716 /* --- @parse_size@ --- *
1718 * Arguments: @union tvec_regval *rv@ = register value
1719 * @const struct tvec_regdef *rd@ = register definition
1720 * @struct tvec_state *tv@ = test-vector state
1722 * Returns: Zero on success, %$-1$% on error.
1724 * Use: Parse a register value from an input file.
1726 * The input format for a size value consists of an unsigned
1727 * integer followed by an optional unit specifier consisting of
1728 * an SI unit prefix and (optionally) the letter `B'. */
1730 static int parse_size(union tvec_regval *rv, const struct tvec_regdef *rd,
1731 struct tvec_state *tv)
1736 if (parse_szint(tv, &sz, ";", "size")) { rc = -1; goto end; }
1737 if (check_unsigned_range(sz, rd->arg.p, tv, "size")) { rc = -1; goto end; }
1743 /* --- @dump_size@ --- *
1745 * Arguments: @const union tvec_regval *rv@ = register value
1746 * @const struct tvec_regdef *rd@ = register definition
1747 * @unsigned style@ = output style (@TVSF_...@)
1748 * @const struct gprintf_ops *gops@, @void *gp@ = format output
1752 * Use: Dump a register value to the format output.
1754 * Size values are dumped with a unit specifier, with a unit
1755 * prefox only if the size is an exact multiple of the relevant
1756 * power of two. Unless compact style is requested, the plain
1757 * decimal and hex representations of the value are also
1761 static void dump_size(const union tvec_regval *rv,
1762 const struct tvec_regdef *rd,
1764 const struct gprintf_ops *gops, void *go)
1766 if (style&TVSF_RAW) gprintf(gops, go, "size:");
1767 format_size(gops, go, rv->u, style);
1768 if (!(style&(TVSF_COMPACT | TVSF_RAW))) {
1769 gprintf(gops, go, " ; = %lu", (unsigned long)rv->u);
1770 gprintf(gops, go, " = "); format_unsigned_hex(gops, go, rv->u);
1771 maybe_format_unsigned_char(gops, go, rv->u);
1775 /* Size type definitions. */
1776 const struct tvec_regty tvty_size = {
1777 init_uint, trivial_release, eq_uint, copy_uint,
1778 tobuf_uint, frombuf_uint,
1779 parse_size, dump_size
1782 /* --- @tvec_claimeq_size@ --- *
1784 * Arguments: @struct tvec_state *tv@ = test-vector state
1785 * @unsigned long sz0, sz1@ = two sizes
1786 * @const char *file@, @unsigned @lno@ = calling file and line
1787 * @const char *expr@ = the expression to quote on failure
1789 * Returns: Nonzero if @sz0@ and @sz1@ are equal, otherwise zero.
1791 * Use: Check that values of @u0@ and @u1@ are equal. As for
1792 * @tvec_claim@ above, a test case is automatically begun and
1793 * ended if none is already underway. If the values are
1794 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
1795 * mismatched values are dumped: @u0@ is printed as the output
1796 * value and @u1@ is printed as the input reference.
1799 int tvec_claimeq_size(struct tvec_state *tv,
1800 unsigned long sz0, unsigned long sz1,
1801 const char *file, unsigned lno, const char *expr)
1803 struct tvec_reg rval, rref;
1805 rval.f = rref.f = TVRF_LIVE; rval.v.u = sz0; rref.v.u = sz1;
1806 return (tvec_claimeq(tv, &tvty_size, 0, &rval, &rref, file, lno, expr));
1809 /*----- Floating-point type -----------------------------------------------*/
1811 /* --- @int_float@ --- *
1813 * Arguments: @union tvec_regval *rv@ = register value
1814 * @const struct tvec_regdef *rd@ = register definition
1818 * Use: Initialize a register value.
1820 * Floating-point values are initialized to zero.
1823 static void init_float(union tvec_regval *rv, const struct tvec_regdef *rd)
1826 /* --- @eq_float@ --- *
1828 * Arguments: @const union tvec_regval *rv0, *rv1@ = register values
1829 * @const struct tvec_regdef *rd@ = register definition
1831 * Returns: Nonzero if the values are equal, zero if unequal
1833 * Use: Compare register values for equality.
1835 * Floating-point values may be considered equal if their
1836 * absolute or relative difference is sufficiently small, as
1837 * described in the register definition.
1840 static int eq_float(const union tvec_regval *rv0,
1841 const union tvec_regval *rv1,
1842 const struct tvec_regdef *rd)
1843 { return (eqish_floating_p(rv1->f, rv0->f, rd->arg.p)); }
1845 /* --- @copy_float@ --- *
1847 * Arguments: @union tvec_regval *rvd@ = destination register value
1848 * @const union tvec_regval *rvs@ = source register value
1849 * @const struct tvec_regdef *rd@ = register definition
1853 * Use: Copy a register value.
1856 static void copy_float(union tvec_regval *rvd, const union tvec_regval *rvs,
1857 const struct tvec_regdef *rd)
1858 { rvd->f = rvs->f; }
1860 /* --- @tobuf_float@ --- *
1862 * Arguments: @buf *b@ = buffer
1863 * @const union tvec_regval *rv@ = register value
1864 * @const struct tvec_regdef *rd@ = register definition
1866 * Returns: Zero on success, %$-1$% on failure.
1868 * Use: Serialize a register value to a buffer.
1870 * Floating-point values are serialized as little-endian
1871 * IEEE 754 Binary64.
1874 static int tobuf_float(buf *b, const union tvec_regval *rv,
1875 const struct tvec_regdef *rd)
1876 { return (buf_putf64l(b, rv->f)); }
1878 /* --- @frombuf_float@ --- *
1880 * Arguments: @buf *b@ = buffer
1881 * @union tvec_regval *rv@ = register value
1882 * @const struct tvec_regdef *rd@ = register definition
1884 * Returns: Zero on success, %$-1$% on failure.
1886 * Use: Deserialize a register value from a buffer.
1888 * Floating-point values are serialized as little-endian
1889 * IEEE 754 Binary64.
1892 static int frombuf_float(buf *b, union tvec_regval *rv,
1893 const struct tvec_regdef *rd)
1898 rc = buf_getf64l(b, &t); if (!rc) rv->f = t;
1902 /* --- @parse_float@ --- *
1904 * Arguments: @union tvec_regval *rv@ = register value
1905 * @const struct tvec_regdef *rd@ = register definition
1906 * @struct tvec_state *tv@ = test-vector state
1908 * Returns: Zero on success, %$-1$% on error.
1910 * Use: Parse a register value from an input file.
1912 * Floating-point values are either NaN (%|#nan|%, if supported
1913 * by the platform); positive or negative infinity (%|#inf|%,
1914 * %|+#inf|%, or %|#+inf|% (preferring the last), and %|-#inf|%
1915 * or %|#-inf|% (preferring the latter), if supported by the
1916 * platform); or a number in strtod(3) syntax.
1919 static int parse_float(union tvec_regval *rv, const struct tvec_regdef *rd,
1920 struct tvec_state *tv)
1925 if (tvec_readword(tv, &d, 0, ";", "floating-point number"))
1926 { rc = -1; goto end; }
1927 if (parse_floating(&rv->f, 0, d.buf, rd->arg.p, tv))
1928 { rc = -1; goto end; }
1935 /* --- @dump_float@ --- *
1937 * Arguments: @const union tvec_regval *rv@ = register value
1938 * @const struct tvec_regdef *rd@ = register definition
1939 * @unsigned style@ = output style (@TVSF_...@)
1940 * @const struct gprintf_ops *gops@, @void *gp@ = format output
1944 * Use: Dump a register value to the format output.
1946 * Floating-point values are dumped in decimal or as a special
1947 * token beginning with `%|#|%'. Some effort is taken to ensure
1948 * that the output is sufficient to uniquely identify the
1949 * original value, but, honestly, C makes this really hard.
1952 static void dump_float(const union tvec_regval *rv,
1953 const struct tvec_regdef *rd,
1955 const struct gprintf_ops *gops, void *go)
1957 if (style&TVSF_RAW) gprintf(gops, go, "float:");
1958 format_floating(gops, go, rv->f);
1961 /* Floating-point type definition. */
1962 const struct tvec_regty tvty_float = {
1963 init_float, trivial_release, eq_float, copy_float,
1964 tobuf_float, frombuf_float,
1965 parse_float, dump_float
1968 /* Predefined floating-point ranges. */
1969 const struct tvec_floatinfo
1970 tvflt_float = { TVFF_RELDELTA | TVFF_INFOK | TVFF_NANOK,
1971 -FLT_MAX, FLT_MAX, FLT_EPSILON/2 },
1972 tvflt_double = { TVFF_EXACT | TVFF_INFOK | TVFF_NANOK,
1973 -DBL_MAX, DBL_MAX, 0.0 },
1974 tvflt_finite = { TVFF_EXACT, -DBL_MAX, DBL_MAX, 0.0 },
1975 tvflt_nonneg = { TVFF_EXACT, 0, DBL_MAX, 0.0 };
1977 /* --- @tvec_claimeqish_float@ --- *
1979 * Arguments: @struct tvec_state *tv@ = test-vector state
1980 * @double f0, f1@ = two floating-point numbers
1981 * @unsigned f@ = flags (@TVFF_...@)
1982 * @double delta@ = maximum tolerable difference
1983 * @const char *file@, @unsigned @lno@ = calling file and line
1984 * @const char *expr@ = the expression to quote on failure
1986 * Returns: Nonzero if @f0@ and @f1@ are sufficiently close, otherwise
1989 * Use: Check that values of @f0@ and @f1@ are sufficiently close.
1990 * As for @tvec_claim@ above, a test case is automatically begun
1991 * and ended if none is already underway. If the values are
1992 * too far apart, then @tvec_fail@ is called, quoting @expr@,
1993 * and the mismatched values are dumped: @f0@ is printed as the
1994 * output value and @f1@ is printed as the input reference.
1996 * The details for the comparison are as follows.
1998 * * A NaN value matches any other NaN, and nothing else.
2000 * * An infinity matches another infinity of the same sign,
2003 * * If @f&TVFF_EQMASK@ is @TVFF_EXACT@, then any
2004 * representable number matches only itself: in particular,
2005 * positive and negative zero are considered distinct.
2006 * (This allows tests to check that they land on the correct
2007 * side of branch cuts, for example.)
2009 * * If @f&TVFF_EQMASK@ is @TVFF_ABSDELTA@, then %$x$% matches
2010 * %$y$% when %$|x - y| < \delta$%.
2012 * * If @f&TVFF_EQMASK@ is @TVFF_RELDELTA@, then %$x$% matches
2013 * %$y$% when %$|1 - x/y| < \delta$%. (Note that this
2014 * criterion is asymmetric. Write %$x \approx_\delta y$%
2015 * if and only if %$|1 - x/y < \delta$%. Then, for example,
2016 * if %$y/(1 + \delta) < x < y (1 - \delta)$%, then
2017 * %$x \approx_\delta y$%, but %$y \not\approx_\delta x$%.)
2020 int tvec_claimeqish_float(struct tvec_state *tv,
2021 double f0, double f1, unsigned f, double delta,
2022 const char *file, unsigned lno,
2025 struct tvec_floatinfo fi;
2026 struct tvec_reg rval, rref;
2027 union tvec_misc arg;
2029 fi.f = f; fi.min = fi.max = 0.0; fi.delta = delta; arg.p = &fi;
2030 rval.f = rref.f = TVRF_LIVE; rval.v.f = f0; rref.v.f = f1;
2031 return (tvec_claimeq(tv, &tvty_float, &arg,
2032 &rval, &rref, file, lno, expr));
2035 /* --- @tvec_claimeq_float@ --- *
2037 * Arguments: @struct tvec_state *tv@ = test-vector state
2038 * @double f0, f1@ = two floating-point numbers
2039 * @const char *file@, @unsigned @lno@ = calling file and line
2040 * @const char *expr@ = the expression to quote on failure
2042 * Returns: Nonzero if @f0@ and @f1@ are identical, otherwise zero.
2044 * Use: Check that values of @f0@ and @f1@ are identical. The
2045 * function is exactly equivalent to @tvec_claimeqish_float@
2046 * with @f == TVFF_EXACT@.
2049 int tvec_claimeq_float(struct tvec_state *tv,
2050 double f0, double f1,
2051 const char *file, unsigned lno,
2054 return (tvec_claimeqish_float(tv, f0, f1, TVFF_EXACT, 0.0,
2058 /*----- Durations ---------------------------------------------------------*/
2060 /* A duration is a floating-point number of seconds. Initialization and
2061 * teardown, equality comparison, and serialization are as for floating-point
2065 static const struct duration_unit {
2069 #define DUF_PREFER 1u
2070 } duration_units[] = {
2082 { "yr", 31557600.0, DUF_PREFER },
2083 { "year", 31557600.0, 0 },
2084 { "years", 31557600.0, 0 },
2085 { "y", 31557600.0, 0 },
2086 { "wk", 604800.0, DUF_PREFER },
2087 { "week", 604800.0, 0 },
2088 { "weeks", 604800.0, 0 },
2089 { "w", 604800.0, 0 },
2090 { "day", 86400.0, DUF_PREFER },
2091 { "days", 86400.0, 0 },
2092 { "dy", 86400.0, 0 },
2093 { "d", 86400.0, 0 },
2094 { "hr", 3600.0, DUF_PREFER },
2095 { "hour", 3600.0, 0 },
2096 { "hours", 3600.0, 0 },
2098 { "min", 60.0, DUF_PREFER },
2099 { "minute", 60.0, 0 },
2100 { "minutes", 60.0, 0 },
2103 { "s", 1.0, DUF_PREFER },
2105 { "second", 1.0, 0 },
2106 { "seconds", 1.0, 0 },
2110 { "ms", 1e-3, DUF_PREFER },
2111 { "µs", 1e-6, DUF_PREFER },
2112 { "ns", 1e-9, DUF_PREFER },
2113 { "ps", 1e-12, DUF_PREFER },
2114 { "fs", 1e-15, DUF_PREFER },
2115 { "as", 1e-18, DUF_PREFER },
2116 { "zs", 1e-21, DUF_PREFER },
2117 { "ys", 1e-24, DUF_PREFER },
2122 /* --- @tvec_parsedurunit@ --- *
2124 * Arguments: @double *scale_out@ = where to leave the scale
2125 * @const char **p_inout@ = input unit string, updated
2127 * Returns: Zero on success, %$-1$% on error.
2129 * Use: If @*p_inout@ begins with a unit string followed by the end
2130 * of the string or some non-alphanumeric character, then store
2131 * the corresponding scale factor in @*scale_out@, advance
2132 * @*p_inout@ past the unit string, and return zero. Otherwise,
2136 int tvec_parsedurunit(double *scale_out, const char **p_inout)
2138 const char *p = *p_inout, *q;
2139 const struct duration_unit *u;
2142 while (ISSPACE(*p)) p++;
2143 for (q = p; *q && ISALNUM(*q); q++);
2144 n = q - p; if (!n) { *scale_out = 1.0; return (0); }
2146 for (u = duration_units; u->unit; u++)
2147 if (STRNCMP(p, ==, u->unit, n) && !u->unit[n])
2148 { *scale_out = u->scale; *p_inout = q; return (0); }
2152 /* --- @parse_duration@ --- *
2154 * Arguments: @union tvec_regval *rv@ = register value
2155 * @const struct tvec_regdef *rd@ = register definition
2156 * @struct tvec_state *tv@ = test-vector state
2158 * Returns: Zero on success, %$-1$% on error.
2160 * Use: Parse a register value from an input file.
2162 * Duration values are finite nonnegative floating-point
2163 * numbers in @strtod@ syntax, optionally followed by a unit .
2166 static int parse_duration(union tvec_regval *rv,
2167 const struct tvec_regdef *rd,
2168 struct tvec_state *tv)
2170 const struct duration_unit *u;
2176 if (tvec_readword(tv, &d, 0, ";", "duration")) { rc = -1; goto end; }
2177 if (parse_floating(&t, &q, d.buf,
2178 rd->arg.p ? rd->arg.p : &tvflt_nonneg, tv))
2179 { rc = -1; goto end; }
2181 if (!*q) tvec_readword(tv, &d, &q, ";", 0);
2183 for (u = duration_units; u->unit; u++)
2184 if (STRCMP(q, ==, u->unit)) { t *= u->scale; goto found_unit; }
2185 rc = tvec_syntax(tv, *q, "end-of-line"); goto end;
2195 /* --- @dump_duration@ --- *
2197 * Arguments: @const union tvec_regval *rv@ = register value
2198 * @const struct tvec_regdef *rd@ = register definition
2199 * @unsigned style@ = output style (@TVSF_...@)
2200 * @const struct gprintf_ops *gops@, @void *gp@ = format output
2204 * Use: Dump a register value to the format output.
2206 * Durations are dumped as a human-palatable scaled value with
2207 * unit, and, if compact style is not requested, as a raw number
2208 * of seconds at full precision as a comment.
2211 static void dump_duration(const union tvec_regval *rv,
2212 const struct tvec_regdef *rd,
2214 const struct gprintf_ops *gops, void *go)
2216 const struct duration_unit *u;
2219 if (style&TVSF_RAW) {
2220 gprintf(gops, go, "duration:");
2221 format_floating(gops, go, rv->f);
2222 gprintf(gops, go, "s");
2226 for (u = duration_units; u->scale > t && u[1].unit; u++);
2229 gprintf(gops, go, "%.4g %s", t, u ? u->unit : "s");
2231 if (!(style&TVSF_COMPACT)) {
2232 gprintf(gops, go, "; = ");
2233 format_floating(gops, go, rv->f);
2234 gprintf(gops, go, " s");
2239 /* Duration type definition. */
2240 const struct tvec_regty tvty_duration = {
2241 init_float, trivial_release, eq_float, copy_float,
2242 tobuf_float, frombuf_float,
2243 parse_duration, dump_duration
2246 /* --- @tvec_claimeqish_duration@ --- *
2248 * Arguments: @struct tvec_state *tv@ = test-vector state
2249 * @double t0, t1@ = two durations
2250 * @unsigned f@ = flags (@TVFF_...@)
2251 * @double delta@ = maximum tolerable difference
2252 * @const char *file@, @unsigned @lno@ = calling file and line
2253 * @const char *expr@ = the expression to quote on failure
2255 * Returns: Nonzero if @t0@ and @t1@ are sufficiently close, otherwise
2258 * Use: Check that values of @t0@ and @t1@ are sufficiently close.
2259 * This is essentially the same as @tvec_claimeqish_float@, only
2260 * it dumps the values as durations on a mismatch.
2263 int tvec_claimeqish_duration(struct tvec_state *tv,
2264 double t0, double t1, unsigned f, double delta,
2265 const char *file, unsigned lno,
2268 struct tvec_floatinfo fi;
2269 struct tvec_reg rval, rref;
2270 union tvec_misc arg;
2272 fi.f = f; fi.min = fi.max = 0.0; fi.delta = delta; arg.p = &fi;
2273 rval.f = rref.f = TVRF_LIVE; rval.v.f = t0; rref.v.f = t1;
2274 return (tvec_claimeq(tv, &tvty_duration, &arg,
2275 &rval, &rref, file, lno, expr));
2278 /* --- @tvec_claimeq_duration@ --- *
2280 * Arguments: @struct tvec_state *tv@ = test-vector state
2281 * @double t0, t1@ = two durations
2282 * @const char *file@, @unsigned @lno@ = calling file and line
2283 * @const char *expr@ = the expression to quote on failure
2285 * Returns: Nonzero if @t0@ and @t1@ are identical, otherwise zero.
2287 * Use: Check that values of @t0@ and @t1@ are identical. The
2288 * function is exactly equivalent to @tvec_claimeqish_duration@
2289 * with @f == TVFF_EXACT@.
2292 int tvec_claimeq_duration(struct tvec_state *tv,
2293 double t0, double t1,
2294 const char *file, unsigned lno,
2297 return (tvec_claimeqish_duration(tv, t0, t1, TVFF_EXACT, 0.0,
2301 /*----- Enumerations ------------------------------------------------------*/
2303 /* --- @init_tenum@ --- *
2305 * Arguments: @union tvec_regval *rv@ = register value
2306 * @const struct tvec_regdef *rd@ = register definition
2310 * Use: Initialize a register value.
2312 * Integer and floating-point enumeration values are initialized
2313 * as their underlying representations. Pointer enumerations
2314 * are initialized to %|#nil|%.
2317 #define init_ienum init_int
2318 #define init_uenum init_uint
2319 #define init_fenum init_float
2321 static void init_penum(union tvec_regval *rv, const struct tvec_regdef *rd)
2324 /* --- @eq_tenum@ --- *
2326 * Arguments: @const union tvec_regval *rv0, *rv1@ = register values
2327 * @const struct tvec_regdef *rd@ = register definition
2329 * Returns: Nonzero if the values are equal, zero if unequal
2331 * Use: Compare register values for equality.
2333 * Integer and floating-point enumeration values are compared as
2334 * their underlying representations; in particular, floating-
2335 * point enumerations may compare equal if their absolute or
2336 * relative difference is sufficiently small. Pointer
2337 * enumerations are compared as pointers.
2340 #define eq_ienum eq_int
2341 #define eq_uenum eq_uint
2343 static int eq_fenum(const union tvec_regval *rv0,
2344 const union tvec_regval *rv1,
2345 const struct tvec_regdef *rd)
2347 const struct tvec_fenuminfo *ei = rd->arg.p;
2348 return (eqish_floating_p(rv0->f, rv1->f, ei->fi));
2351 static int eq_penum(const union tvec_regval *rv0,
2352 const union tvec_regval *rv1,
2353 const struct tvec_regdef *rd)
2354 { return (rv0->p == rv1->p); }
2356 /* --- @copy_tenum@ --- *
2358 * Arguments: @union tvec_regval *rvd@ = destination register value
2359 * @const union tvec_regval *rvs@ = source register value
2360 * @const struct tvec_regdef *rd@ = register definition
2364 * Use: Copy a register value.
2367 #define copy_ienum copy_int
2368 #define copy_uenum copy_uint
2369 #define copy_fenum copy_float
2371 static void copy_penum(union tvec_regval *rvd, const union tvec_regval *rvs,
2372 const struct tvec_regdef *rd)
2373 { rvd->p = rvs->p; }
2375 /* --- @tobuf_tenum@ --- *
2377 * Arguments: @buf *b@ = buffer
2378 * @const union tvec_regval *rv@ = register value
2379 * @const struct tvec_regdef *rd@ = register definition
2381 * Returns: Zero on success, %$-1$% on failure.
2383 * Use: Serialize a register value to a buffer.
2385 * Integer and floating-point enumeration values are serialized
2386 * as their underlying representations. Pointer enumerations
2387 * are serialized as the signed integer index into the
2388 * association table; %|#nil|% serializes as %$-1$%, and
2389 * unrecognized pointers cause failure.
2392 #define tobuf_ienum tobuf_int
2393 #define tobuf_uenum tobuf_uint
2394 #define tobuf_fenum tobuf_float
2396 static int tobuf_penum(buf *b, const union tvec_regval *rv,
2397 const struct tvec_regdef *rd)
2399 const struct tvec_penuminfo *pei = rd->arg.p;
2400 const struct tvec_passoc *pa;
2403 for (pa = pei->av, i = 0; pa->tag; pa++, i++)
2404 if (pa->p == rv->p) goto found;
2408 return (signed_to_buf(b, i));
2411 /* --- @frombuf_tenum@ --- *
2413 * Arguments: @buf *b@ = buffer
2414 * @union tvec_regval *rv@ = register value
2415 * @const struct tvec_regdef *rd@ = register definition
2417 * Returns: Zero on success, %$-1$% on failure.
2419 * Use: Deserialize a register value from a buffer.
2421 * Integer and floating-point enumeration values are serialized
2422 * as their underlying representations. Pointer enumerations
2423 * are serialized as the signed integer index into the
2424 * association table; %|#nil|% serializes as %$-1$%; out-of-
2425 * range indices cause failure.
2428 #define frombuf_ienum frombuf_int
2429 #define frombuf_uenum frombuf_uint
2430 #define frombuf_fenum frombuf_float
2431 static int frombuf_penum(buf *b, union tvec_regval *rv,
2432 const struct tvec_regdef *rd)
2434 const struct tvec_penuminfo *pei = rd->arg.p;
2435 const struct tvec_passoc *pa;
2438 for (pa = pei->av, n = 0; pa->tag; pa++, n++);
2439 if (signed_from_buf(b, &i)) return (-1);
2440 if (0 <= i && i < n) rv->p = UNCONST(void, pei->av[i].p);
2441 else if (i == -1) rv->p = 0;
2442 else { buf_break(b); return (-1); }
2446 /* --- @parse_tenum@ --- *
2448 * Arguments: @union tvec_regval *rv@ = register value
2449 * @const struct tvec_regdef *rd@ = register definition
2450 * @struct tvec_state *tv@ = test-vector state
2452 * Returns: Zero on success, %$-1$% on error.
2454 * Use: Parse a register value from an input file.
2456 * An enumerated value may be given by name or as a literal
2457 * value. For enumerations based on numeric types, the literal
2458 * values can be written in the same syntax as the underlying
2459 * values. For enumerations based on pointers, the only
2460 * permitted literal is %|#nil|%, which denotes a null pointer.
2463 #define DEFPARSE_ENUM(tag_, ty, slot) \
2464 static int parse_##slot##enum(union tvec_regval *rv, \
2465 const struct tvec_regdef *rd, \
2466 struct tvec_state *tv) \
2468 const struct tvec_##slot##enuminfo *ei = rd->arg.p; \
2469 const struct tvec_##slot##assoc *a; \
2470 dstr d = DSTR_INIT; \
2473 if (tvec_readword(tv, &d, 0, \
2474 ";", "%s tag or " LITSTR_##tag_, ei->name)) \
2475 { rc = -1; goto end; } \
2476 for (a = ei->av; a->tag; a++) \
2477 if (STRCMP(a->tag, ==, d.buf)) { FOUND_##tag_ goto done; } \
2486 #define LITSTR_INT "literal signed integer"
2487 #define FOUND_INT rv->i = a->i;
2488 #define MISSING_INT if (parse_signed(&rv->i, d.buf, ei->ir, tv)) \
2489 { rc = -1; goto end; }
2491 #define LITSTR_UINT "literal unsigned integer"
2492 #define FOUND_UINT rv->u = a->u;
2493 #define MISSING_UINT if (parse_unsigned(&rv->u, d.buf, ei->ur, tv)) \
2494 { rc = -1; goto end; }
2496 #define LITSTR_FLT "literal floating-point number, " \
2497 "`#-inf', `#+inf', or `#nan'"
2498 #define FOUND_FLT rv->f = a->f;
2499 #define MISSING_FLT if (parse_floating(&rv->f, 0, d.buf, ei->fi, tv)) \
2500 { rc = -1; goto end; }
2502 #define LITSTR_PTR "`#nil'"
2503 #define FOUND_PTR rv->p = UNCONST(void, a->p);
2504 #define MISSING_PTR if (STRCMP(d.buf, ==, "#nil")) \
2507 tvec_error(tv, "unknown `%s' value `%s'", \
2509 rc = -1; goto end; \
2512 TVEC_MISCSLOTS(DEFPARSE_ENUM)
2530 #undef DEFPARSE_ENUM
2532 /* --- @dump_tenum@ --- *
2534 * Arguments: @const union tvec_regval *rv@ = register value
2535 * @const struct tvec_regdef *rd@ = register definition
2536 * @unsigned style@ = output style (@TVSF_...@)
2537 * @const struct gprintf_ops *gops@, @void *gp@ = format output
2541 * Use: Dump a register value to the format output.
2543 * Enumeration values are dumped as their symbolic names, if
2544 * possible, with the underlying values provided as a comment
2545 * unless compact output is requested, as for the underlying
2546 * representation. A null pointer is printed as %|#nil|%;
2547 * non-null pointers are printed as %|#<TYPE PTR>|%, with the
2548 * enumeration TYPE and the raw pointer PTR printed with the
2549 * system's %|%p|% format specifier.
2553 #define DEFDUMP_ENUM(tag_, ty, slot) \
2554 static void dump_##slot##enum(const union tvec_regval *rv, \
2555 const struct tvec_regdef *rd, \
2557 const struct gprintf_ops *gops, void *go) \
2559 const struct tvec_##slot##enuminfo *ei = rd->arg.p; \
2560 const struct tvec_##slot##assoc *a; \
2562 if (style&TVSF_RAW) gprintf(gops, go, #slot "enum/%s:", ei->name); \
2563 for (a = ei->av; a->tag; a++) \
2564 if (rv->slot == a->slot) { \
2565 gprintf(gops, go, "%s", a->tag); \
2566 if (style&TVSF_COMPACT) return; \
2567 gprintf(gops, go, " ; = "); break; \
2573 #define MAYBE_PRINT_EXTRA \
2574 if (style&TVSF_COMPACT) /* nothing to do */; \
2575 else if (!a->tag) { gprintf(gops, go, " ; = "); goto _extra; } \
2576 else if (1) { gprintf(gops, go, " = "); goto _extra; } \
2579 #define PRINTRAW_INT gprintf(gops, go, "%ld", rv->i); \
2580 MAYBE_PRINT_EXTRA { \
2581 format_signed_hex(gops, go, rv->i); \
2582 maybe_format_signed_char(gops, go, rv->i); \
2585 #define PRINTRAW_UINT gprintf(gops, go, "%lu", rv->u); \
2586 MAYBE_PRINT_EXTRA { \
2587 format_unsigned_hex(gops, go, rv->u); \
2588 maybe_format_unsigned_char(gops, go, rv->u); \
2591 #define PRINTRAW_FLT format_floating(gops, go, rv->f);
2593 #define PRINTRAW_PTR if (!rv->p) gprintf(gops, go, "#nil"); \
2594 else gprintf(gops, go, "#<%s %p>", ei->name, rv->p);
2596 TVEC_MISCSLOTS(DEFDUMP_ENUM)
2599 #undef PRINTRAW_UINT
2603 #undef MAYBE_PRINT_EXTRA
2606 /* Enumeration type definitions. */
2607 #define DEFTY_ENUM(tag, ty, slot) \
2608 const struct tvec_regty tvty_##slot##enum = { \
2609 init_##slot##enum, trivial_release, eq_##slot##enum, copy_##slot##enum, \
2610 tobuf_##slot##enum, frombuf_##slot##enum, \
2611 parse_##slot##enum, dump_##slot##enum \
2613 TVEC_MISCSLOTS(DEFTY_ENUM)
2616 /* Predefined enumeration types. */
2617 static const struct tvec_iassoc bool_assoc[] = {
2634 const struct tvec_ienuminfo tvenum_bool =
2635 { "bool", bool_assoc, &tvrange_int };
2637 static const struct tvec_iassoc cmp_assoc[] = {
2653 const struct tvec_ienuminfo tvenum_cmp =
2654 { "cmp", cmp_assoc, &tvrange_int };
2656 /* --- @tvec_claimeq_tenum@ --- *
2658 * Arguments: @struct tvec_state *tv@ = test-vector state
2659 * @const struct tvec_typeenuminfo *ei@ = enumeration type info
2660 * @ty t0, t1@ = two values
2661 * @const char *file@, @unsigned @lno@ = calling file and line
2662 * @const char *expr@ = the expression to quote on failure
2664 * Returns: Nonzero if @t0@ and @t1@ are equal, otherwise zero.
2666 * Use: Check that values of @t0@ and @t1@ are equal. As for
2667 * @tvec_claim@ above, a test case is automatically begun and
2668 * ended if none is already underway. If the values are
2669 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
2670 * mismatched values are dumped: @t0@ is printed as the output
2671 * value and @t1@ is printed as the input reference.
2674 #define DEFCLAIM(tag, ty, slot) \
2675 int tvec_claimeq_##slot##enum \
2676 (struct tvec_state *tv, \
2677 const struct tvec_##slot##enuminfo *ei, ty e0, ty e1, \
2678 const char *file, unsigned lno, const char *expr) \
2680 union tvec_misc arg; \
2681 struct tvec_reg rval, rref; \
2684 rval.f = rref.f = TVRF_LIVE; \
2685 rval.v.slot = GET_##tag(e0); rref.v.slot = GET_##tag(e1); \
2686 return (tvec_claimeq(tv, &tvty_##slot##enum, &arg, \
2687 &rval, &rref, file, lno, expr)); \
2689 #define GET_INT(e) (e)
2690 #define GET_UINT(e) (e)
2691 #define GET_FLT(e) (e)
2692 #define GET_PTR(e) (UNCONST(void, (e)))
2693 TVEC_MISCSLOTS(DEFCLAIM)
2700 /*----- Flag types --------------------------------------------------------*/
2702 /* Flag types are initialized, compared, and serialized as unsigned
2706 /* --- @parse_flags@ --- *
2708 * Arguments: @union tvec_regval *rv@ = register value
2709 * @const struct tvec_regdef *rd@ = register definition
2710 * @struct tvec_state *tv@ = test-vector state
2712 * Returns: Zero on success, %$-1$% on error.
2714 * Use: Parse a register value from an input file.
2716 * The input syntax is a sequence of items separated by `|'
2717 * signs. Each item may be the symbolic name of a field value,
2718 * or a literal unsigned integer. The masks associated with the
2719 * given symbolic names must be disjoint. The resulting
2720 * numerical value is simply the bitwise OR of the given values.
2723 static int parse_flags(union tvec_regval *rv, const struct tvec_regdef *rd,
2724 struct tvec_state *tv)
2726 const struct tvec_flaginfo *fi = rd->arg.p;
2727 const struct tvec_flag *f;
2728 unsigned long m = 0, v = 0, t;
2734 /* Read the next item. */
2736 if (tvec_readword(tv, &d, 0, "|;", "%s flag name or integer", fi->name))
2737 { rc = -1; goto end; }
2739 /* Try to find a matching entry in the table. */
2740 for (f = fi->fv; f->tag; f++)
2741 if (STRCMP(f->tag, ==, d.buf)) {
2743 { tvec_error(tv, "colliding flag setting"); rc = -1; goto end; }
2745 { m |= f->m; v |= f->v; goto next; }
2748 /* Otherwise, try to parse it as a raw integer. */
2749 if (parse_unsigned(&t, d.buf, fi->range, tv))
2750 { rc = -1; goto end; }
2754 /* Advance to the next token. If it's a separator then consume it, and
2755 * go round again. Otherwise we stop here.
2757 if (tvec_nexttoken(tv)) break;
2759 if (ch != '|') { tvec_syntax(tv, ch, "`|'"); rc = -1; goto end; }
2760 if (tvec_nexttoken(tv)) {
2761 tvec_syntax(tv, '\n', "%s flag name or integer", fi->name);
2773 /* --- @dump_flags@ --- *
2775 * Arguments: @const union tvec_regval *rv@ = register value
2776 * @const struct tvec_regdef *rd@ = register definition
2777 * @unsigned style@ = output style (@TVSF_...@)
2778 * @const struct gprintf_ops *gops@, @void *gp@ = format output
2782 * Use: Dump a register value to the format output.
2784 * The table of symbolic names and their associated values and
2785 * masks is repeatedly scanned, in order, to find disjoint
2786 * matches -- i.e., entries whose value matches the target value
2787 * in the bit positions indicated by the mask, and whose mask
2788 * doesn't overlap with any previously found matches; the names
2789 * are then output, separated by `|'. Any remaining nonzero
2790 * bits not covered by any of the matching masks are output as a
2791 * single literal integer, in hex.
2793 * Unless compact output is requested, or no symbolic names were
2794 * found, the raw numeric value is also printed in hex, as a
2798 static void dump_flags(const union tvec_regval *rv,
2799 const struct tvec_regdef *rd,
2801 const struct gprintf_ops *gops, void *go)
2803 const struct tvec_flaginfo *fi = rd->arg.p;
2804 const struct tvec_flag *f;
2805 unsigned long m = ~0ul, v = rv->u;
2808 if (style&TVSF_RAW) gprintf(gops, go, "flags/%s:", fi->name);
2810 for (f = fi->fv, sep = ""; f->tag; f++)
2811 if ((m&f->m) && (v&f->m) == f->v) {
2812 gprintf(gops, go, "%s%s", sep, f->tag); m &= ~f->m;
2813 sep = style&TVSF_COMPACT ? "|" : " | ";
2816 if (v&m) gprintf(gops, go, "%s0x%0*lx", sep, hex_width(v), v&m);
2817 else if (!v && m == ~0ul) gprintf(gops, go, "0");
2819 if (!(style&(TVSF_COMPACT | TVSF_RAW)))
2820 gprintf(gops, go, " ; = 0x%0*lx", hex_width(rv->u), rv->u);
2823 /* Flags type definition. */
2824 const struct tvec_regty tvty_flags = {
2825 init_uint, trivial_release, eq_uint, copy_uint,
2826 tobuf_uint, frombuf_uint,
2827 parse_flags, dump_flags
2830 /* --- @tvec_claimeq_flags@ --- *
2832 * Arguments: @struct tvec_state *tv@ = test-vector state
2833 * @const struct tvec_flaginfo *fi@ = flags type info
2834 * @unsigned long f0, f1@ = two values
2835 * @const char *file@, @unsigned @lno@ = calling file and line
2836 * @const char *expr@ = the expression to quote on failure
2838 * Returns: Nonzero if @f0@ and @f1@ are equal, otherwise zero.
2840 * Use: Check that values of @f0@ and @f1@ are equal. As for
2841 * @tvec_claim@ above, a test case is automatically begun and
2842 * ended if none is already underway. If the values are
2843 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
2844 * mismatched values are dumped: @f0@ is printed as the output
2845 * value and @f1@ is printed as the input reference.
2848 int tvec_claimeq_flags(struct tvec_state *tv,
2849 const struct tvec_flaginfo *fi,
2850 unsigned long f0, unsigned long f1,
2851 const char *file, unsigned lno, const char *expr)
2853 union tvec_misc arg;
2854 struct tvec_reg rval, rref;
2856 rval.f = rref.f = TVRF_LIVE; rval.v.u = f0; rref.v.u = f1;
2857 return (tvec_claimeq(tv, &tvty_flags, &arg,
2858 &rval, &rref, file, lno, expr));
2861 /*----- Characters --------------------------------------------------------*/
2863 /* Character values are initialized and compared as signed integers. */
2865 /* --- @tobuf_char@ --- *
2867 * Arguments: @buf *b@ = buffer
2868 * @const union tvec_regval *rv@ = register value
2869 * @const struct tvec_regdef *rd@ = register definition
2871 * Returns: Zero on success, %$-1$% on failure.
2873 * Use: Serialize a register value to a buffer.
2875 * Character values are serialized as little-endian 32-bit
2876 * unsigned integers, with %|EOF|% serialized as all-bits-set.
2879 static int tobuf_char(buf *b, const union tvec_regval *rv,
2880 const struct tvec_regdef *rd)
2884 if (0 <= rv->i && rv->i <= UCHAR_MAX) u = rv->i;
2885 else if (rv->i == EOF) u = MASK32;
2886 else { buf_break(b); return (-1); }
2887 return (buf_putu32l(b, u));
2890 /* --- @frombuf_char@ --- *
2892 * Arguments: @buf *b@ = buffer
2893 * @union tvec_regval *rv@ = register value
2894 * @const struct tvec_regdef *rd@ = register definition
2896 * Returns: Zero on success, %$-1$% on failure.
2898 * Use: Deserialize a register value from a buffer.
2900 * Character values are serialized as little-endian 32-bit
2901 * unsigned integers, with %|EOF|% serialized as all-bits-set.
2904 static int frombuf_char(buf *b, union tvec_regval *rv,
2905 const struct tvec_regdef *rd)
2909 if (buf_getu32l(b, &u)) return (-1);
2910 if (0 <= u && u <= UCHAR_MAX) rv->i = u;
2911 else if (u == MASK32) rv->i = EOF;
2912 else { buf_break(b); return (-1); }
2916 /* --- @parse_char@ --- *
2918 * Arguments: @union tvec_regval *rv@ = register value
2919 * @const struct tvec_regdef *rd@ = register definition
2920 * @struct tvec_state *tv@ = test-vector state
2922 * Returns: Zero on success, %$-1$% on error.
2924 * Use: Parse a register value from an input file.
2926 * A character value can be given by symbolic name, with a
2927 * leading `%|#|%'; or a character or `%|\|%'-escape sequence,
2928 * optionally in single quotes.
2930 * The following escape sequences and character names are
2933 * * `%|#eof|%' is the special end-of-file marker.
2935 * * `%|#nul|%' is the NUL character, sometimes used to
2936 * terminate strings.
2938 * * `%|bell|%', `%|bel|%', `%|ding|%', or `%|\a|%' is the BEL
2939 * character used to ring the terminal bell (or do some other
2940 * thing to attract the user's attention).
2942 * * %|#backspace|%, %|#bs|%, or %|\b|% is the backspace
2943 * character, used to move the cursor backwords by one cell.
2945 * * %|#escape|% %|#esc|%, or%|\e|% is the escape character,
2946 * used to introduce special terminal commands.
2948 * * %|#formfeed|%, %|#ff|%, or %|\f|% is the formfeed
2949 * character, used to separate pages of text.
2951 * * %|#newline|%, %|#linefeed|%, %|#lf|%, %|#nl|%, or %|\n|% is
2952 * the newline character, used to terminate lines of text or
2953 * advance the cursor to the next line (perhaps without
2954 * returning it to the start of the line).
2956 * * %|#return|%, %|#carriage-return|%, %|#cr|%, or %|\r|% is
2957 * the carriage-return character, used to return the cursor to
2958 * the start of the line.
2960 * * %|#tab|%, %|#horizontal-tab|%, %|#ht|%, or %|\t|% is the
2961 * tab character, used to advance the cursor to the next tab
2962 * stop on the current line.
2964 * * %|#vertical-tab|%, %|#vt|%, %|\v|% is the vertical tab
2967 * * %|#space|%, %|#spc|% is the space character.
2969 * * %|#delete|%, %|#del|% is the delete character, used to
2970 * erase the most recent character.
2972 * * %|\'|% is the single-quote character.
2974 * * %|\\|% is the backslash character.
2976 * * %|\"|% is the double-quote character.
2978 * * %|\NNN|% or %|\{NNN}|% is the character with code NNN in
2979 * octal. The NNN may be up to three digits long.
2981 * * %|\xNN|% or %|\x{NN}|% is the character with code NNN in
2985 static int parse_char(union tvec_regval *rv, const struct tvec_regdef *rd,
2986 struct tvec_state *tv)
2993 /* Advance until we find something. */
2994 if (tvec_nexttoken(tv))
2995 return (tvec_syntax(tv, fgetc(tv->fp), "character"));
2997 /* Inspect the character to see what we're up against. */
3001 /* It looks like a special token. Push the `%|#|%' back and fetch the
3002 * whole word. If there's just the `%|#|%' after all, then treat it as
3007 if (tvec_readword(tv, &d, 0, ";", "character name"))
3008 { rc = -1; goto end; }
3009 if (STRCMP(d.buf, !=, "#")) {
3010 if (read_charname(&ch, d.buf, RCF_EOFOK)) {
3011 rc = tvec_error(tv, "unknown character name `%s'", d.buf);
3014 rv->i = ch; rc = 0; goto end;
3018 /* If this is a single quote then we expect to see a matching one later,
3019 * and we should process backslash escapes. Get the next character and see
3022 if (ch == '\'') { f |= f_quote; ch = getc(tv->fp); }
3024 /* Main character dispatch. */
3028 /* A newline. If we saw a single quote, then treat that as literal.
3029 * Otherwise this is an error.
3031 if (!(f&f_quote)) goto nochar;
3032 else { f &= ~f_quote; ungetc(ch, tv->fp); ch = '\''; goto plain; }
3035 /* End-of-file. Similar to newline, but with slightly different
3036 * effects on the parse state.
3038 if (!(f&f_quote)) goto nochar;
3039 else { f &= ~f_quote; ch = '\''; goto plain; }
3042 /* A single quote. This must be the second of a pair, and there should
3043 * have been a character or escape sequence between them.
3045 rc = tvec_syntax(tv, ch, "character"); goto end;
3048 /* A backslash. Read a character escape. */
3049 if (read_charesc(&ch, tv)) return (-1);
3052 /* Anything else. Treat as literal. */
3056 /* If we saw an opening quote, then expect the closing quote. */
3059 if (ch != '\'') { rc = tvec_syntax(tv, ch, "`''"); goto end; }
3071 /* --- @dump_char@ --- *
3073 * Arguments: @const union tvec_regval *rv@ = register value
3074 * @const struct tvec_regdef *rd@ = register definition
3075 * @unsigned style@ = output style (@TVSF_...@)
3076 * @const struct gprintf_ops *gops@, @void *gp@ = format output
3080 * Use: Dump a register value to the format output.
3082 * Character values are dumped as their symbolic names, if any,
3083 * or as a character or escape sequence within single quotes
3084 * (which may be omitted in compact style). If compact output
3085 * is not requested, then the single-quoted representation (for
3086 * characters dumped as symbolic names) and integer code in
3087 * decimal and hex are printed as a comment.
3090 static void dump_char(const union tvec_regval *rv,
3091 const struct tvec_regdef *rd,
3093 const struct gprintf_ops *gops, void *go)
3099 if (style&TVSF_RAW) {
3100 /* Print the raw character unconditionally in single quotes. */
3102 gprintf(gops, go, "char:'");
3103 format_char(gops, go, rv->i);
3104 gprintf(gops, go, "'");
3106 /* Print ina pleasant human-readable way. */
3108 /* Print a character name if we can find one. */
3109 p = find_charname(rv->i, (style&TVSF_COMPACT) ? CTF_SHORT : CTF_PREFER);
3111 gprintf(gops, go, "%s", p);
3112 if (style&TVSF_COMPACT) return;
3113 else { gprintf(gops, go, " ;"); f |= f_semi; }
3116 /* If the character isn't @EOF@ then print it as a single-quoted thing.
3117 * In compact style, see if we can omit the quotes.
3120 if (f&f_semi) gprintf(gops, go, " = ");
3122 case ' ': case '\\': case '\'': quote:
3123 format_char(gops, go, rv->i);
3126 if (!(style&TVSF_COMPACT) || !isprint(rv->i)) goto quote;
3127 gprintf(gops, go, "%c", (int)rv->i);
3132 /* And the character code as an integer. */
3133 if (!(style&TVSF_COMPACT)) {
3134 if (!(f&f_semi)) gprintf(gops, go, " ;");
3135 gprintf(gops, go, " = %ld = ", rv->i);
3136 format_signed_hex(gops, go, rv->i);
3143 /* Character type definition. */
3144 const struct tvec_regty tvty_char = {
3145 init_int, trivial_release, eq_int, copy_int,
3146 tobuf_char, frombuf_char,
3147 parse_char, dump_char
3150 /* --- @tvec_claimeq_char@ --- *
3152 * Arguments: @struct tvec_state *tv@ = test-vector state
3153 * @int ch0, ch1@ = two character codes
3154 * @const char *file@, @unsigned @lno@ = calling file and line
3155 * @const char *expr@ = the expression to quote on failure
3157 * Returns: Nonzero if @ch0@ and @ch1@ are equal, otherwise zero.
3159 * Use: Check that values of @ch0@ and @ch1@ are equal. As for
3160 * @tvec_claim@ above, a test case is automatically begun and
3161 * ended if none is already underway. If the values are
3162 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
3163 * mismatched values are dumped: @ch0@ is printed as the output
3164 * value and @ch1@ is printed as the input reference.
3167 int tvec_claimeq_char(struct tvec_state *tv, int c0, int c1,
3168 const char *file, unsigned lno, const char *expr)
3170 struct tvec_reg rval, rref;
3172 rval.f = rref.f = TVRF_LIVE; rval.v.i = c0; rref.v.i = c1;
3173 return (tvec_claimeq(tv, &tvty_char, 0, &rval, &rref, file, lno, expr));
3176 /*----- Text and byte strings ---------------------------------------------*/
3178 /* --- @init_text@, @init_bytes@ --- *
3180 * Arguments: @union tvec_regval *rv@ = register value
3181 * @const struct tvec_regdef *rd@ = register definition
3185 * Use: Initialize a register value.
3187 * Text and binary string values are initialized with a null
3188 * pointer and zero length.
3191 static void init_text(union tvec_regval *rv, const struct tvec_regdef *rd)
3192 { rv->text.p = 0; rv->text.sz = 0; }
3194 static void init_bytes(union tvec_regval *rv, const struct tvec_regdef *rd)
3195 { rv->bytes.p = 0; rv->bytes.sz = 0; }
3197 /* --- @release_string@, @release_bytes@ --- *
3199 * Arguments: @const union tvec_regval *rv@ = register value
3200 * @const struct tvec_regdef *rd@ = register definition
3204 * Use: Release resources held by a register value.
3206 * Text and binary string buffers are freed.
3209 static void release_text(union tvec_regval *rv,
3210 const struct tvec_regdef *rd)
3211 { free(rv->text.p); }
3213 static void release_bytes(union tvec_regval *rv,
3214 const struct tvec_regdef *rd)
3215 { free(rv->bytes.p); }
3217 /* --- @eq_text@, @eq_bytes@ --- *
3219 * Arguments: @const union tvec_regval *rv0, *rv1@ = register values
3220 * @const struct tvec_regdef *rd@ = register definition
3222 * Returns: Nonzero if the values are equal, zero if unequal
3224 * Use: Compare register values for equality.
3227 static int eq_text(const union tvec_regval *rv0,
3228 const union tvec_regval *rv1,
3229 const struct tvec_regdef *rd)
3231 return (rv0->text.sz == rv1->text.sz &&
3233 MEMCMP(rv0->text.p, ==, rv1->text.p, rv1->text.sz)));
3236 static int eq_bytes(const union tvec_regval *rv0,
3237 const union tvec_regval *rv1,
3238 const struct tvec_regdef *rd)
3240 return (rv0->bytes.sz == rv1->bytes.sz &&
3242 MEMCMP(rv0->bytes.p, ==, rv1->bytes.p, rv1->bytes.sz)));
3245 /* --- @copy_text@, @copy_bytes@ --- *
3247 * Arguments: @union tvec_regval *rvd@ = destination register value
3248 * @const union tvec_regval *rvs@ = source register value
3249 * @const struct tvec_regdef *rd@ = register definition
3253 * Use: Copy a register value.
3256 static void copy_text(union tvec_regval *rvd, const union tvec_regval *rvs,
3257 const struct tvec_regdef *rd)
3259 size_t sz = rvs->text.sz;
3264 tvec_alloctext(rvd, sz);
3265 memcpy(rvd->text.p, rvs->text.p, sz); rvd->text.p[sz] = 0;
3269 static void copy_bytes(union tvec_regval *rvd, const union tvec_regval *rvs,
3270 const struct tvec_regdef *rd)
3272 size_t sz = rvs->bytes.sz;
3277 tvec_alloctext(rvd, sz);
3278 memcpy(rvd->bytes.p, rvs->bytes.p, sz);
3282 /* --- @tobuf_text@, @tobuf_bytes@ --- *
3284 * Arguments: @buf *b@ = buffer
3285 * @const union tvec_regval *rv@ = register value
3286 * @const struct tvec_regdef *rd@ = register definition
3288 * Returns: Zero on success, %$-1$% on failure.
3290 * Use: Serialize a register value to a buffer.
3292 * Text and binary string values are serialized as a little-
3293 * endian 64-bit length %$n$% in bytes followed by %$n$% bytes
3297 static int tobuf_text(buf *b, const union tvec_regval *rv,
3298 const struct tvec_regdef *rd)
3299 { return (buf_putmem64l(b, rv->text.p, rv->text.sz)); }
3301 static int tobuf_bytes(buf *b, const union tvec_regval *rv,
3302 const struct tvec_regdef *rd)
3303 { return (buf_putmem64l(b, rv->bytes.p, rv->bytes.sz)); }
3305 /* --- @frombuf_text@, @frombuf_bytes@ --- *
3307 * Arguments: @buf *b@ = buffer
3308 * @union tvec_regval *rv@ = register value
3309 * @const struct tvec_regdef *rd@ = register definition
3311 * Returns: Zero on success, %$-1$% on failure.
3313 * Use: Deserialize a register value from a buffer.
3315 * Text and binary string values are serialized as a little-
3316 * endian 64-bit length %$n$% in bytes followed by %$n$% bytes
3320 static int frombuf_text(buf *b, union tvec_regval *rv,
3321 const struct tvec_regdef *rd)
3326 p = buf_getmem64l(b, &sz); if (!p) return (-1);
3327 tvec_alloctext(rv, sz); memcpy(rv->text.p, p, sz); rv->text.p[sz] = 0;
3331 static int frombuf_bytes(buf *b, union tvec_regval *rv,
3332 const struct tvec_regdef *rd)
3337 p = buf_getmem64l(b, &sz); if (!p) return (-1);
3338 tvec_allocbytes(rv, sz); memcpy(rv->bytes.p, p, sz);
3342 /* --- @check_string_length@ --- *
3344 * Arguments: @size_t sz@ = found string length
3345 * @const struct tvec_urange *ur@ = acceptable range
3346 * @struct tvec_state *tv@ = test-vector state
3348 * Returns: Zero on success, %$-1$% on error.
3350 * Use: Checks that @sz@ is within the bounds described by @ur@,
3351 * reporting an error if not.
3354 static int check_string_length(size_t sz, const struct tvec_urange *ur,
3355 struct tvec_state *tv)
3360 if (ur->min > sz || sz > ur->max) {
3361 tvec_error(tv, "invalid string length %lu; must be in [%lu .. %lu]",
3362 (unsigned long)sz, ur->min, ur->max);
3365 if (ur->m && ur->m != 1) {
3367 if (uu != ur->a%ur->m) {
3368 tvec_error(tv, "invalid string length %lu == %lu =/= %lu (mod %lu)",
3369 (unsigned long)sz, uu, ur->a, ur->m);
3377 /* --- @parse_text@, @parse_bytes@ --- *
3379 * Arguments: @union tvec_regval *rv@ = register value
3380 * @const struct tvec_regdef *rd@ = register definition
3381 * @struct tvec_state *tv@ = test-vector state
3383 * Returns: Zero on success, %$-1$% on error.
3385 * Use: Parse a register value from an input file.
3387 * The input format for both kinds of strings is basically the
3388 * same: a `compound string', consisting of
3390 * * single-quoted strings, which are interpreted entirely
3391 * literally, but can't contain single quotes or newlines;
3393 * * double-quoted strings, in which `%|\|%'-escapes are
3394 * interpreted as for characters;
3396 * * character names, marked by an initial `%|#|%' sign;
3398 * * special tokens marked by an initial `%|!|%' sign; or
3400 * * barewords interpreted according to the current coding
3403 * The special tokens are
3405 * * `%|!bare|%', which causes subsequent sequences of
3406 * barewords to be treated as plain text;
3408 * * `%|!hex|%', `%|!base32|%', `%|!base64|%', which cause
3409 * subsequent barewords to be decoded in the requested
3412 * * `%|!repeat|% %$n$% %|{|% %%\textit{string}%% %|}|%',
3413 * which includes %$n$% copies of the (compound) string.
3415 * The only difference between text and binary strings is that
3416 * the initial coding scheme is %|bare|% for text strings and
3417 * %|hex|% for binary strings.
3420 static int parse_text(union tvec_regval *rv, const struct tvec_regdef *rd,
3421 struct tvec_state *tv)
3423 void *p = rv->text.p;
3425 if (read_compound_string(&p, &rv->text.sz, TVCODE_BARE, 0, tv))
3428 if (check_string_length(rv->text.sz, rd->arg.p, tv)) return (-1);
3432 static int parse_bytes(union tvec_regval *rv, const struct tvec_regdef *rd,
3433 struct tvec_state *tv)
3435 void *p = rv->bytes.p;
3437 if (read_compound_string(&p, &rv->bytes.sz, TVCODE_HEX, 0, tv))
3440 if (check_string_length(rv->bytes.sz, rd->arg.p, tv)) return (-1);
3444 /* --- @dump_text@, @dump_bytes@ --- *
3446 * Arguments: @const union tvec_regval *rv@ = register value
3447 * @const struct tvec_regdef *rd@ = register definition
3448 * @unsigned style@ = output style (@TVSF_...@)
3449 * @const struct gprintf_ops *gops@, @void *gp@ = format output
3453 * Use: Dump a register value to the format output.
3455 * Text string values are dumped as plain text, in double quotes
3456 * if necessary, and using backslash escape sequences for
3457 * nonprintable characters. Unless compact output is requested,
3458 * strings consisting of multiple lines are dumped with each
3459 * line of the string on a separate output line.
3461 * Binary string values are dumped in hexadecimal. In compact
3462 * style, the output simply consists of a single block of hex
3463 * digits. Otherwise, the dump is a display consisting of
3464 * groups of hex digits, with comments showing the offset (if
3465 * the string is long enough) and the corresponding plain text.
3467 * Empty strings are dumped as %|#empty|%.
3470 static void dump_empty(const char *ty, unsigned style,
3471 const struct gprintf_ops *gops, void *go)
3473 if (style&TVSF_RAW) gprintf(gops, go, "%s:", ty);
3474 if (!(style&TVSF_COMPACT)) gprintf(gops, go, "#empty");
3475 if (!(style&(TVSF_COMPACT | TVSF_RAW))) gprintf(gops, go, " ; = ");
3476 if (!(style&TVSF_RAW)) gprintf(gops, go, "\"\"");
3480 static void dump_text(const union tvec_regval *rv,
3481 const struct tvec_regdef *rd,
3483 const struct gprintf_ops *gops, void *go)
3485 const unsigned char *p, *q, *l;
3487 #define f_nonword 1u
3488 #define f_newline 2u
3490 if (!rv->text.sz) { dump_empty("text", style, gops, go); return; }
3492 p = (const unsigned char *)rv->text.p; l = p + rv->text.sz;
3493 if (style&TVSF_RAW) { gprintf(gops, go, "text:"); goto quote; }
3494 else if (style&TVSF_COMPACT) goto quote;
3497 case '!': case '#': case ';': case '"': case '\'':
3498 case '(': case '{': case '[': case ']': case '}': case ')':
3499 f |= f_nonword; break;
3501 for (q = p; q < l; q++)
3502 if (*q == '\n' && q != l - 1) f |= f_newline;
3503 else if (!*q || !ISGRAPH(*q) || *q == '\\') f |= f_nonword;
3504 if (f&f_newline) { gprintf(gops, go, "\n\t"); goto quote; }
3505 else if (f&f_nonword) goto quote;
3507 gops->putm(go, (const char *)p, rv->text.sz);
3511 gprintf(gops, go, "\"");
3512 for (q = p; q < l; q++)
3513 if (!ISPRINT(*q) || *q == '"') {
3514 if (p < q) gops->putm(go, (const char *)p, q - p);
3515 if (*q != '\n' || (style&TVSF_COMPACT))
3516 format_charesc(gops, go, *q, FCF_BRACE);
3518 if (q + 1 == l) { gprintf(gops, go, "\\n\""); return; }
3519 else gprintf(gops, go, "\\n\"\n\t\"");
3523 if (p < q) gops->putm(go, (const char *)p, q - p);
3524 gprintf(gops, go, "\"");
3530 static void dump_bytes(const union tvec_regval *rv,
3531 const struct tvec_regdef *rd,
3533 const struct gprintf_ops *gops, void *go)
3535 const unsigned char *p = rv->bytes.p, *l = p + rv->bytes.sz;
3536 size_t off, sz = rv->bytes.sz;
3540 if (!rv->text.sz) { dump_empty("bytes", style, gops, go); return; }
3542 if (style&(TVSF_COMPACT | TVSF_RAW)) {
3543 if (style&TVSF_RAW) gprintf(gops, go, "bytes:");
3544 while (p < l) gprintf(gops, go, "%02x", *p++);
3548 if (sz > 16) gprintf(gops, go, "\n\t");
3550 off = 0; wd = hex_width(sz);
3552 if (l - p < 16) n = l - p;
3555 for (i = 0; i < n; i++) {
3556 if (i < n) gprintf(gops, go, "%02x", p[i]);
3557 else gprintf(gops, go, " ");
3558 if (i < n - 1 && i%4 == 3) gprintf(gops, go, " ");
3560 gprintf(gops, go, " ; ");
3561 if (sz > 16) gprintf(gops, go, "[%0*lx] ", wd, (unsigned long)off);
3562 for (i = 0; i < n; i++)
3563 gprintf(gops, go, "%c", isprint(p[i]) ? p[i] : '.');
3565 if (p < l) gprintf(gops, go, "\n\t");
3569 /* Text and byte string type definitions. */
3570 const struct tvec_regty tvty_text = {
3571 init_text, release_text, eq_text, copy_text,
3572 tobuf_text, frombuf_text,
3573 parse_text, dump_text
3575 const struct tvec_regty tvty_bytes = {
3576 init_bytes, release_bytes, eq_bytes, copy_bytes,
3577 tobuf_bytes, frombuf_bytes,
3578 parse_bytes, dump_bytes
3581 /* --- @tvec_claimeq_text@ --- *
3583 * Arguments: @struct tvec_state *tv@ = test-vector state
3584 * @const char *p0@, @size_t sz0@ = first string with length
3585 * @const char *p1@, @size_t sz1@ = second string with length
3586 * @const char *file@, @unsigned @lno@ = calling file and line
3587 * @const char *expr@ = the expression to quote on failure
3589 * Returns: Nonzero if the strings at @p0@ and @p1@ are equal, otherwise
3592 * Use: Check that strings at @p0@ and @p1@ are equal. As for
3593 * @tvec_claim@ above, a test case is automatically begun and
3594 * ended if none is already underway. If the values are
3595 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
3596 * mismatched values are dumped: @p0@ is printed as the output
3597 * value and @p1@ is printed as the input reference.
3600 int tvec_claimeq_text(struct tvec_state *tv,
3601 const char *p0, size_t sz0,
3602 const char *p1, size_t sz1,
3603 const char *file, unsigned lno, const char *expr)
3605 struct tvec_reg rval, rref;
3607 rval.f = rref.f = TVRF_LIVE;
3608 rval.v.text.p = UNCONST(char, p0); rval.v.text.sz = sz0;
3609 rref.v.text.p = UNCONST(char, p1); rref.v.text.sz = sz1;
3610 return (tvec_claimeq(tv, &tvty_text, 0, &rval, &rref, file, lno, expr));
3613 /* --- @tvec_claimeq_textz@ --- *
3615 * Arguments: @struct tvec_state *tv@ = test-vector state
3616 * @const char *p0, *p1@ = two strings to compare
3617 * @const char *file@, @unsigned @lno@ = calling file and line
3618 * @const char *expr@ = the expression to quote on failure
3620 * Returns: Nonzero if the strings at @p0@ and @p1@ are equal, otherwise
3623 * Use: Check that strings at @p0@ and @p1@ are equal, as for
3624 * @tvec_claimeq_string@, except that the strings are assumed
3625 * null-terminated, so their lengths don't need to be supplied
3629 int tvec_claimeq_textz(struct tvec_state *tv,
3630 const char *p0, const char *p1,
3631 const char *file, unsigned lno, const char *expr)
3633 struct tvec_reg rval, rref;
3635 rval.f = rref.f = TVRF_LIVE;
3636 rval.v.text.p = UNCONST(char, p0); rval.v.text.sz = strlen(p0);
3637 rref.v.text.p = UNCONST(char, p1); rref.v.text.sz = strlen(p1);
3638 return (tvec_claimeq(tv, &tvty_text, 0, &rval, &rref, file, lno, expr));
3641 /* --- @tvec_claimeq_bytes@ --- *
3643 * Arguments: @struct tvec_state *tv@ = test-vector state
3644 * @const void *p0@, @size_t sz0@ = first string with length
3645 * @const void *p1@, @size_t sz1@ = second string with length
3646 * @const char *file@, @unsigned @lno@ = calling file and line
3647 * @const char *expr@ = the expression to quote on failure
3649 * Returns: Nonzero if the strings at @p0@ and @p1@ are equal, otherwise
3652 * Use: Check that binary strings at @p0@ and @p1@ are equal. As for
3653 * @tvec_claim@ above, a test case is automatically begun and
3654 * ended if none is already underway. If the values are
3655 * unequal, then @tvec_fail@ is called, quoting @expr@, and the
3656 * mismatched values are dumped: @p0@ is printed as the output
3657 * value and @p1@ is printed as the input reference.
3660 int tvec_claimeq_bytes(struct tvec_state *tv,
3661 const void *p0, size_t sz0,
3662 const void *p1, size_t sz1,
3663 const char *file, unsigned lno, const char *expr)
3665 struct tvec_reg rval, rref;
3667 rval.f = rref.f = TVRF_LIVE;
3668 rval.v.bytes.p = UNCONST(void, p0); rval.v.bytes.sz = sz0;
3669 rref.v.bytes.p = UNCONST(void, p1); rref.v.bytes.sz = sz1;
3670 return (tvec_claimeq(tv, &tvty_bytes, 0, &rval, &rref, file, lno, expr));
3673 /* --- @tvec_alloctext@, @tvec_allocbytes@ --- *
3675 * Arguments: @union tvec_regval *rv@ = register value
3676 * @size_t sz@ = required size
3680 * Use: Allocated space in a text or binary string register. If the
3681 * current register size is sufficient, its buffer is left
3682 * alone; otherwise, the old buffer, if any, is freed and a
3683 * fresh buffer allocated. These functions are not intended to
3684 * be used to adjust a buffer repeatedly, e.g., while building
3685 * output incrementally: (a) they will perform badly, and (b)
3686 * the old buffer contents are simply discarded if reallocation
3687 * is necessary. Instead, use a @dbuf@ or @dstr@.
3689 * The @tvec_alloctext@ function sneakily allocates an extra
3690 * byte for a terminating zero. The @tvec_allocbytes@ function
3694 void tvec_alloctext(union tvec_regval *rv, size_t sz)
3696 if (rv->text.sz <= sz)
3697 { free(rv->text.p); rv->text.p = x_alloc(&arena_stdlib, sz + 1); }
3698 memset(rv->text.p, '?', sz); rv->text.p[sz] = 0; rv->text.sz = sz;
3701 void tvec_allocbytes(union tvec_regval *rv, size_t sz)
3703 if (rv->bytes.sz < sz)
3704 { free(rv->bytes.p); rv->bytes.p = x_alloc(&arena_stdlib, sz); }
3705 memset(rv->bytes.p, '?', sz); rv->bytes.sz = sz;
3708 /*----- Buffer type -------------------------------------------------------*/
3710 /* --- @init_buffer@ --- *
3712 * Arguments: @union tvec_regval *rv@ = register value
3713 * @const struct tvec_regdef *rd@ = register definition
3717 * Use: Initialize a register value.
3719 * Buffer values values are initialized with a null pointer,
3720 * zero length, and zero residue, modulus, and offset.
3723 static void init_buffer(union tvec_regval *rv, const struct tvec_regdef *rd)
3724 { rv->buf.p = 0; rv->buf.sz = rv->buf.a = rv->buf.m = rv->buf.off = 0; }
3726 /* --- @release_buffer@, @release_bytes@ --- *
3728 * Arguments: @const union tvec_regval *rv@ = register value
3729 * @const struct tvec_regdef *rd@ = register definition
3733 * Use: Release resources held by a register value.
3735 * Buffers are freed.
3738 static void release_buffer(union tvec_regval *rv,
3739 const struct tvec_regdef *rd)
3740 { if (rv->buf.p) free(rv->buf.p - rv->buf.off); }
3742 /* --- @eq_buffer@ --- *
3744 * Arguments: @const union tvec_regval *rv0, *rv1@ = register values
3745 * @const struct tvec_regdef *rd@ = register definition
3747 * Returns: Nonzero if the values are equal, zero if unequal
3749 * Use: Compare register values for equality.
3751 * Buffer values are equal if and only if their sizes and
3752 * alignment parameters are equal; their contents are
3753 * %%\emph{not}%% compared.
3756 static int eq_buffer(const union tvec_regval *rv0,
3757 const union tvec_regval *rv1,
3758 const struct tvec_regdef *rd)
3760 return (rv0->buf.sz == rv1->buf.sz &&
3761 rv0->buf.a == rv1->buf.a &&
3762 rv0->buf.m == rv1->buf.m);
3765 /* --- @copy_buffer@ --- *
3767 * Arguments: @union tvec_regval *rvd@ = destination register value
3768 * @const union tvec_regval *rvs@ = source register value
3769 * @const struct tvec_regdef *rd@ = register definition
3773 * Use: Copy a register value.
3776 static void copy_buffer(union tvec_regval *rvd, const union tvec_regval *rvs,
3777 const struct tvec_regdef *rd)
3779 if (rvd->buf.p) { free(rvd->buf.p); rvd->buf.p = 0; }
3780 rvd->buf.sz = rvs->buf.sz;
3781 rvd->buf.a = rvs->buf.a;
3782 rvd->buf.m = rvs->buf.m;
3786 /* --- @tobuf_buffer@ --- *
3788 * Arguments: @buf *b@ = buffer
3789 * @const union tvec_regval *rv@ = register value
3790 * @const struct tvec_regdef *rd@ = register definition
3792 * Returns: Zero on success, %$-1$% on failure.
3794 * Use: Serialize a register value to a buffer.
3796 * Buffer values are serialized as their lengths, residues, and
3797 * moduli, as unsigned integers.
3800 static int tobuf_buffer(buf *b, const union tvec_regval *rv,
3801 const struct tvec_regdef *rd)
3803 return (unsigned_to_buf(b, rv->buf.sz) ||
3804 unsigned_to_buf(b, rv->buf.a) ||
3805 unsigned_to_buf(b, rv->buf.m));
3808 /* --- @frombuf_buffer@ --- *
3810 * Arguments: @buf *b@ = buffer
3811 * @union tvec_regval *rv@ = register value
3812 * @const struct tvec_regdef *rd@ = register definition
3814 * Returns: Zero on success, %$-1$% on failure.
3816 * Use: Deserialize a register value from a buffer.
3818 * Buffer values are serialized as just their lengths, as
3819 * unsigned integers. The buffer is allocated on
3820 * deserialization and filled with a distinctive pattern.
3823 static int frombuf_buffer(buf *b, union tvec_regval *rv,
3824 const struct tvec_regdef *rd)
3826 unsigned long sz, a, m;
3828 if (unsigned_from_buf(b, &sz)) return (-1);
3829 if (unsigned_from_buf(b, &a)) return (-1);
3830 if (unsigned_from_buf(b, &m)) return (-1);
3831 if (sz > (size_t)-1 || a > (size_t)-1 || m > (size_t)-1)
3832 { buf_break(b); return (-1); }
3833 rv->buf.sz = sz; rv->buf.a = a; rv->buf.m = m;
3837 /* --- @parse_buffer@ --- *
3839 * Arguments: @union tvec_regval *rv@ = register value
3840 * @const struct tvec_regdef *rd@ = register definition
3841 * @struct tvec_state *tv@ = test-vector state
3843 * Returns: Zero on success, %$-1$% on error.
3845 * Use: Parse a register value from an input file.
3847 * The input format for a buffer value is a size, followed by an
3848 * optional `%|@$%' and an alignment quantum and a further
3849 * optional `%|+|%' and an alignment offset. The size, quantum,
3850 * and offset are syntactically sizes.
3852 * The buffer is not allocated.
3855 static int parse_buffer(union tvec_regval *rv,
3856 const struct tvec_regdef *rd,
3857 struct tvec_state *tv)
3859 unsigned long sz, a = 0, m = 0;
3862 if (parse_szint(tv, &sz, "@;", "buffer length")) { rc = -1; goto end; }
3863 if (check_unsigned_range(sz, &tvrange_size, tv, "buffer length"))
3864 { rc = -1; goto end; }
3865 if (check_string_length(sz, rd->arg.p, tv)) { rc = -1; goto end; }
3867 if (tvec_nexttoken(tv)) goto done;
3869 if (ch != '@') { rc = tvec_syntax(tv, ch, "`@'"); goto end; }
3871 if (parse_szint(tv, &m, "+;", "alignment quantum")) { rc = -1; goto end; }
3872 if (check_unsigned_range(a, &tvrange_size, tv, "alignment quantum"))
3873 { rc = -1; goto end; }
3876 if (tvec_nexttoken(tv)) goto done;
3878 if (ch != '+') { rc = tvec_syntax(tv, ch, "`+'"); goto end; }
3880 if (parse_szint(tv, &a, ";", "alignment offset")) { rc = -1; goto end; }
3881 if (check_unsigned_range(m, &tvrange_size, tv, "alignment offset"))
3882 { rc = -1; goto end; }
3884 rc = tvec_error(tv, "alignment offset %lu >= quantum %lu",
3885 (unsigned long)a, (unsigned long)m);
3890 rv->buf.sz = sz; rv->buf.a = a; rv->buf.m = m;
3896 /* --- @dump_buffer@ --- *
3898 * Arguments: @const union tvec_regval *rv@ = register value
3899 * @const struct tvec_regdef *rd@ = register definition
3900 * @unsigned style@ = output style (@TVSF_...@)
3901 * @const struct gprintf_ops *gops@, @void *gp@ = format output
3905 * Use: Dump a register value to the format output.
3907 * Buffer values are dumped as their size, with the alignment
3908 * quantum and alignment offset if these are non-default.
3911 static void dump_buffer(const union tvec_regval *rv,
3912 const struct tvec_regdef *rd,
3914 const struct gprintf_ops *gops, void *go)
3916 if (style&TVSF_RAW) gprintf(gops, go, "buffer:");
3917 format_size(gops, go, rv->buf.sz, style);
3919 gprintf(gops, go, style&(TVSF_COMPACT | TVSF_RAW) ? "@" : " @ ");
3920 format_size(gops, go, rv->buf.m, style);
3922 gprintf(gops, go, style&(TVSF_COMPACT | TVSF_RAW) ? "+" : " + ");
3923 format_size(gops, go, rv->buf.a, style);
3926 if (!(style&(TVSF_COMPACT | TVSF_RAW))) {
3927 gprintf(gops, go, " ; = %lu", (unsigned long)rv->buf.sz);
3929 gprintf(gops, go, " @ %lu", (unsigned long)rv->buf.m);
3930 if (rv->buf.a) gprintf(gops, go, " + %lu", (unsigned long)rv->buf.a);
3932 gprintf(gops, go, " = "); format_unsigned_hex(gops, go, rv->buf.sz);
3934 gprintf(gops, go, " @ "); format_unsigned_hex(gops, go, rv->buf.m);
3936 gprintf(gops, go, " + ");
3937 format_unsigned_hex(gops, go, rv->buf.a);
3943 /* Buffer type definition. */
3944 const struct tvec_regty tvty_buffer = {
3945 init_buffer, release_buffer, eq_buffer, copy_buffer,
3946 tobuf_buffer, frombuf_buffer,
3947 parse_buffer, dump_buffer
3950 /* --- @tvec_initbuffer@ --- *
3952 * Arguments: @union tvec_regval *rv@ = register value
3953 * @const union tvec_regval *ref@ = source buffer
3954 * @size_t sz@ = size to allocate
3958 * Use: Initialize the alignment parameters in @rv@ to match @ref@,
3959 * and the size to @sz@.
3962 void tvec_initbuffer(union tvec_regval *rv,
3963 const union tvec_regval *ref, size_t sz)
3964 { rv->buf.sz = sz; rv->buf.a = ref->buf.a; rv->buf.m = ref->buf.m; }
3966 /* --- @tvec_allocbuffer@ --- *
3968 * Arguments: @union tvec_regval *rv@ = register value
3972 * Use: Allocate @sz@ bytes to the buffer and fill the space with a
3973 * distinctive pattern.
3976 void tvec_allocbuffer(union tvec_regval *rv)
3978 unsigned char *p; size_t n;
3980 if (rv->buf.p) free(rv->buf.p - rv->buf.off);
3982 if (rv->buf.m < 2) {
3983 rv->buf.p = x_alloc(&arena_stdlib, rv->buf.sz); rv->buf.off = 0;
3985 p = x_alloc(&arena_stdlib, rv->buf.sz + rv->buf.m - 1);
3986 n = (size_t)p%rv->buf.m;
3987 rv->buf.off = (rv->buf.a - n + rv->buf.m)%rv->buf.m;
3988 rv->buf.p = p + rv->buf.off;
3990 memset(rv->buf.p, '?', rv->buf.sz);
3993 /*----- That's all, folks -------------------------------------------------*/