]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/ddb/makedis.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
31 * Revision 1.1.1.1 1998/09/22 21:05:48 wsanchez
32 * Import of Mac OS X kernel (~semeria)
34 * Revision 1.1.1.1 1998/03/07 02:26:09 wsanchez
35 * Import of OSF Mach kernel (~mburg)
37 * Revision 1.1.2.1 1997/03/27 18:46:52 barbou
39 * [1997/03/27 13:58:42 barbou]
44 /* makedis.c - make a disassembler. */
47 By Eamonn McManus <emcmanus@gr.osf.org>, April 1995.
48 Copyright 1995 by Eamonn McManus. Non-commercial use is permitted. */
52 This program generates a disassembler in C from a file describing the
53 opcodes of the machine in question. Lines in the description file are
54 either comments beginning with #, or contain three fields, with the
55 first two being terminated by space and the third containing the rest
56 of the line. Long logical lines can be split onto several physical
57 lines by ending each one except the last with a \. A logical line
58 can also be split immediately after a |. Unlike \, | is considered
59 part of the logical line. Leading spaces on continuation lines
60 following either \ or | are ignored.
62 Here is a concise description of the meanings of the three fields.
63 Examples later will make it clearer what they are used for.
65 The first field of the three is a function name. This will produce
66 a function or array of the same name in the C output, so it should
67 not conflict with other identifiers or C keywords. By default the
68 function named returns a string (a (char *) in C), but if the first
69 field is preceded by %, the function returns an unsigned long
72 The second field describes the arguments of the function. It consists
73 of two parts, either but not both of which may be omitted. The first
74 part is a string which is a bitmask describing the first argument of
75 the function. Each character of the string represents one bit,
76 with the least significant bit being the last. A character can be
77 0 or 1, representing that constant value, or a letter, representing
78 part of a bitfield. A given bitfield consists of all of the
79 contiguous bits containing the same letter. Upper and lower case
80 letters are considered different.
82 The second part of the second field is a list of parameters
83 describing the parameters of the function, or the parameters after
84 the first if the bitfield part was present. The list is contained
85 in parentheses () and the individual parameters are separated by
86 commas. Spaces are not allowed. Each parameter name is a single
87 letter, optionally preceded by %. The parameter is an unsigned
88 long integer if % is present, otherwise a string. Again, upper and
89 lower case parameter names are different.
91 The third field describes the value of the function. If a bitmask
92 is present in the second field and it contains constant bits (0s or
93 1s), then the third field is the value of the function only in the
94 case where its first argument contains matching values in those bit
95 positions. There can be many different lines naming the same
96 function but with different bitpatterns. The generated C code will
97 arrange to return the value corresponding to the pattern that
98 matches the actual first argument of the function when it is
99 called. This argument should not have bits set in positions beyond
100 those present in the bitpattern.
102 It is only allowed for two different lines to name the same function
103 if there is a bitstring in the second field. It is not allowed for
104 two such lines to specify exactly the same constant bit values. But
105 it is allowed for a line to have all the same constant bit values as
106 another plus some extra constant values. In this case the more
107 specific line applies when all of its constant bits match, and
108 otherwise the less specific line applies.
110 Apart from the contents of the bitstring, the second field must be
111 identical on every line referring to a given function, and the
112 bitstring must always be of the same length.
114 For string-valued functions, the third field is the string value.
115 For integer-valued functions, it is a C integer expression
116 generating the value. In both cases there may be several special
119 - A $ followed by a single letter is replaced by the value of the
120 argument or bitfield with that name. The value of a bitfield is
121 shifted as if that bitfield were in the least-significant bit
122 position. Thus, a single-bit field always has value 0 or 1.
124 - A $ followed by the name of a function and an argument list in
125 parentheses () is replaced by the value returned by the function
126 with those arguments. An integer value cannot be inserted into a
127 string without being converted by a function, nor can a string
128 value be used in an integer expression.
130 - A $ followed by a bitstring enclosed in [] is replaced by the
131 value of that bitstring. The bitstring has the same syntax as in
132 the second field, described above. Each contiguous sequence of
133 the same repeated letter in the bitstring is replaced by the
134 value of the argument or bitfield-argument with that name,
135 shifted into the appropriate position.
137 - A list of strings, separated by |, enclosed in
138 {}, and followed by an integer expression enclosed in [], is
139 replaced by the string in the list whose number matches the value
140 of the expression. The first string in the list is numbered 0.
141 If there is no string corresponding to the value of the
142 expression, the behaviour is undefined. The strings in the list
143 may themselves contain $ or {} operations.
145 - A \ followed by any character is replaced by that
146 character, without regard to any meaning it may usually have.
147 This is used to obtain strings containing characters such as
148 {, $, or \. The use of backslash to split long logical
149 lines takes precedence over this use, so \\ should not appear
150 at the end of a line.
152 The third field may also be a lone colon ":", in which case the
153 function is assumed to be defined externally and only a function
154 declaration (prototype) is generated.
159 Here are some examples from the description file for the Z80
160 microprocessor. This processor has 8-bit opcodes which are
161 disassembled by a generated function "inst" which looks like this:
163 typedef unsigned long bits;
164 char *inst(bits code) {...}
166 The simplest sort of line in the description file is one that looks
171 The first field names the function, "inst". The second field
172 implies that that function has exactly one argument which is an
173 integer, and that this line specifies the value of the function
174 when this integer has the binary value 01110110 (hex 0x76). This
175 value will be the string "halt".
177 A more complex line is one looking like this:
179 inst 001aa111 {daa|cpl|scf|ccf}[$a]
181 This line is compatible with the previous one, because it has the
182 same number of bits and the constant bits are different. It
183 specifies the value of inst when its argument looks like
184 001aa111, i.e., for the binary values
188 00111111. The value of $a for these four values will be
189 respectively binary 00, 01, 10, 11, i.e., 0 to 3. The
190 corresponding values of the inst function will be "daa", "cpl",
193 The description defines a helper function "reg8" like this:
195 reg8 rrr {b|c|d|e|h|l|(hl)|a}[$r]
197 This simply selects one of the eight strings between {} depending
198 on the value of the argument, which is assumed to be a three-bit
199 value. This could just as easily have been written:
201 reg8 (%r) {b|c|d|e|h|l|(hl)|a}[$r]
203 The generated C code is the same -- in each case makedis realises
204 that the function can be represented by an array rather than
205 compiling a C function.
207 The reg8 function is used in lines like this one:
209 inst 01rrrsss ld $reg8($r),$reg8($s)
211 Thus if the argument to inst is
213 then $r is 010 (2) and $s is 011 (3). Since reg8(2) is "d" and
214 reg8(3) is "e", the value of inst with this argument will be the
217 Note that the opcode for "halt" given above matches this pattern,
218 but because the bitpattern for "halt" is more specific (has more
219 constant bits) it is the one chosen when the argument is 01110110.
221 The description also uses an external C function "hexprint" defined
224 char *hexprint(bits digits, bits n) {
225 char *p = dis_alloc(digits + 1);
226 sprintf(p, "%0*lx", (int) digits, n);
230 The value of this function is a string containing the number n
231 spelt out in hex with "digits" digits. In the description
232 file this function is declared like this:
236 The names of the parameters are not important in this case as long
237 as they are letters and are different from each other.
239 The hexprint function is used in lines like this one:
241 inst 11vvv111 rst $hexprint(2,$v << 3)
243 If the argument to inst is
245 then $v is 011 (3) and the arguments to hexprint are 2 and (3 << 3),
246 i.e., 0x18. So the value of inst with this argument will be the
249 Instead of writing $v << 3, it would be possible to write
250 $[00vvv000]. For instance when $v is binary 011, this becomes
251 00011000. The leading 0s could be omitted.
253 The $[...] operation is particularly useful for moving bits around.
254 For instance, the HP PA-RISC opcodes contain bits assigned to
255 apparently random parts of the instruction word. One of the helper
256 functions in its description file looks like this:
258 im21l aaaaabbccddddddddddde l'$hex($[edddddddddddbbaaaaacc00000000000])
260 So 111110011000000000001 produces 10000000000000111111100000000000.
262 The $[...] operation can also be used to spell out binary constants,
263 since C has no syntax for this.
266 ...More to come... */
269 - More error detection, e.g., bitstring or arg not used in entry.
270 - Better error recovery -- nearly all errors are currently fatal.
271 - Clean up type handling, which is somewhat haphazard. It works but there
272 is stuff that is surely redundant.
273 - Make generated functions void by default, with $ prefix to indicate
274 string-value. In a void function, instead of returning a string (or
275 integer) it would be output via a user-supplied function.
276 - Further optimise and tidy generated code, e.g.: arrays of one-character
277 strings could be replaced by arrays of characters; switches with just
278 one case could be replaced by ifs.
290 #define LONG_BIT (CHAR_BIT * sizeof (long))
291 #endif /* LONG_BIT */
293 #define MAXfunction 32 /* Max function name length. */
294 #define MAXBITS LONG_BIT /* Max bitstring length. */
295 typedef unsigned long bits
;
296 enum type
{T_ERROR
, T_UNKNOWN
, T_INTEGER
, T_STRING
};
297 const char *const typename
[] = {"error", "unknown", "integer", "string"};
298 enum walkstringop
{COUNTARRAYS
, DECLAREARRAYS
, COMPILEARRAYS
};
299 char *bitstype
= "unsigned long";
301 int maxfunctionname
, maxargwidth
;
302 char *progname
= "makedis";
305 char *headerfilename
;
309 int debug
, dump
, warnings
;
311 /* componentbits has a 1 bit for every possible number of strings we may want
312 to concatenate together at some stage. A separate C function is compiled
313 for each such case. */
326 int main(int argc
, char **argv
);
327 int makedis(FILE *f
, char *fname
);
328 struct function
*findfunction(char *function
);
329 int parseextern(struct function
*fp
, FILE *f
);
330 struct function
*makefunction(char *function
);
331 int parsebits(struct function
*fp
, char *bitstring
, int nbits
);
332 int parseentrybits(struct entry
*ep
, char *bitstring
, int nbits
, int issplice
);
333 int parsecontrol(char *name
, char *value
);
334 int parseargs(struct function
*fp
, FILE *f
, int *cp
);
335 int parsestring(struct function
*fp
, char *str
);
336 enum type
makestring(struct function
*fp
, struct string
**stringlink
,
337 char **stringp
, char *magic
, enum type targettype
);
338 int parsedollar(struct function
*fp
, char **stringp
, struct string
*sp
);
339 int parsebitsplice(struct function
*fp
, char *bitstring
, int nbits
,
341 int findvariable(struct function
*fp
, int name
, struct string
*sp
);
342 int parsefunctioncall(struct function
*fp
, char *start
, char **stringp
,
344 int parsearray(struct function
*fp
, char **stringp
, struct string
*sp
,
346 void dumpfunctions(void);
347 void dumpfunction(struct function
*fp
);
348 void showentry(FILE *f
, struct function
*fp
, struct entry
*ep
, bits highlight
);
349 void showbits(FILE *f
, struct entry
*ep
, int nbits
, bits highlight
);
350 void showargs(FILE *f
, struct arg
*ap
, int fieldwidth
);
351 void showstring(FILE *f
, struct string
*sp
);
352 void showstringelement(FILE *f
, struct string
*sp
);
353 void showfunctioncall(FILE *f
, struct functioncall
*fcp
);
354 void showarray(FILE *f
, struct array
*ap
);
355 int outputfunctions(void);
356 void outputidentity(FILE *f
);
357 int outputdeclarations(void);
358 void outputconcats(void);
359 void outputconcat(int n
);
360 void outputconcatheader(FILE *f
, int n
);
361 void findarrays(void);
362 int checkfixedlength(struct array
*ap
);
363 int outputfunction(struct function
*fp
);
364 void functionarray(struct function
*fp
);
365 void functionheader(FILE *f
, struct function
*fp
);
366 int simplearray(struct array
*ap
);
367 void compiletype(FILE *f
, enum type
*tp
);
368 int functionswitch(struct function
*fp
, bits mask
, bits value
);
369 int compilestring(int assignto
, struct string
*sp
, enum type type
);
370 int compilecheckedstring(int assignto
, struct string
*sp
, enum type type
);
371 void compileassign(int assignto
);
372 void compiletemp(int tempno
);
373 void compiletext(char *s
);
374 int compileconcat(struct string
*sp
, enum type type
);
375 int compilenull(enum type type
);
376 int compilesimple(struct string
*sp
, enum type type
);
377 int compilearrayref(struct array
*ap
);
378 int compilefunctioncall(struct string
*sp
);
379 int walkstring(struct string
*sp
, enum walkstringop op
, int tempno
);
380 int compilearray(struct array
*ap
);
381 void compilesimplearray(enum type
*tp
, char *name
, int num
, struct array
*ap
);
382 void declarearray(struct array
*ap
);
383 void compilebitstring(struct bits
*bp
);
384 void compilebitsplice(struct bitsplice
*splicep
);
385 int bitcount(bits x
);
386 bits
allbitsset(int nbits
);
387 void findent(FILE *f
);
389 void *xrealloc(char *oldp
, size_t size
);
390 void *xmalloc(size_t size
);
391 void *xstrdup(char *s
);
392 int prematureeof(void);
395 int main(int argc
, char **argv
) {
402 for (i
= 1; i
< argc
&& argv
[i
][0] == '-'; i
++) {
403 switch (argv
[i
][1]) {
407 headerfilename
= argv
[i
]; break;
416 fprintf(stderr
, "Usage: %s [file]\n", progname
);
421 return makedis(stdin
, "<stdin>");
424 if ((f
= fopen(argv
[i
], "r")) == NULL
) {
425 fprintf(stderr
, "%s: %s: %s\n", progname
, argv
[i
], strerror(errno
));
428 return makedis(f
, argv
[i
]);
432 int makedis(FILE *f
, char *fname
) {
434 char function
[MAXfunction
], bitstring
[MAXBITS
];
435 static char *string
= NULL
;
441 /* Loop for every line in the description. */
443 /* Ignore initial spaces and newlines. */
444 while (isspace(c
= getc(f
)))
450 /* Ignore comments. # only allowed at start of line. */
452 while ((c
= getc(f
)) != '\n')
454 return prematureeof();
459 /* Read function name, terminated by space. */
460 for (i
= 0; i
< sizeof function
&& !isspace(c
); i
++, c
= getc(f
)) {
462 return prematureeof();
465 if (i
>= sizeof function
) {
466 fprintf(stderr
, "%s: %s(%d): function name is too long: %.*s\n",
467 progname
, filename
, lineno
, i
, function
);
472 /* Skip to next field. */
473 while (isspace(c
) && c
!= '\n')
476 /* If not a control statement, read bitstring and/or arguments. */
477 if (function
[0] == ':')
478 fp
= 0; /* Silence gcc. */
480 fp
= makefunction(function
);
484 /* Read optional bitstring. */
485 for (i
= 0; i
< sizeof bitstring
&& isalnum(c
); i
++, c
= getc(f
)) {
487 return prematureeof();
491 fprintf(stderr
, "%s: %s(%d): bit string is too long: %.*s\n",
492 progname
, filename
, lineno
, i
, bitstring
);
495 if (parsebits(fp
, bitstring
, i
) != 0)
498 /* Read optional arguments. */
499 if (parseargs(fp
, f
, &c
) != 0)
502 /* Skip to next field. */
503 while (isspace(c
) && c
!= '\n')
506 /* : indicates an external (C) function. */
508 if (parseextern(fp
, f
) != 0)
514 /* Read associated text. */
517 for ( ; c
!= '\n'; i
++, c
= getc(f
)) {
519 return prematureeof();
520 if (i
>= stringlen
) {
521 stringlen
= stringlen
* 2 + 16;
522 string
= xrealloc(string
, stringlen
);
528 switch (string
[i
- 1]) {
533 while (isspace(c
= getc(f
)) && c
!= '\n') ;
539 if (i
>= stringlen
) {
540 stringlen
= stringlen
* 2 + 16;
541 string
= xrealloc(string
, stringlen
);
545 /* Parse the line just read. */
546 if (function
[0] == ':') {
547 if (parsecontrol(function
+ 1, string
) != 0)
550 if (parsestring(fp
, string
) != 0)
556 return outputfunctions();
560 /* A function in the description file. nbits and nargs are -1 until the
561 real values are known. */
563 struct function
*next
;
566 int nbits
; /* Number of bits in the bitpattern, 0 if none. */
567 int nargs
; /* Number of (x,y,...) parameters, 0 if none. */
568 char isarray
; /* Will be represented by a C array. */
569 int fixedlength
; /* If a C array, will be a char [][N] not a char *[]. */
570 struct entry
*first
, *last
;
571 /* Links to the value(s) supplied. */
572 struct arg
*args
; /* List of (x,y,...) names and types. */
574 struct function
*functions
;
577 /* Find the function with the given name. If not found, create a structure
578 for it, fill it out with a template, and return that. */
579 struct function
*findfunction(char *name
) {
582 for (fp
= functions
; fp
!= NULL
; fp
= fp
->next
) {
583 if (strcmp(fp
->name
, name
) == 0)
586 if (strlen(name
) > maxfunctionname
)
587 maxfunctionname
= strlen(name
);
588 fp
= xmalloc(sizeof *fp
);
589 fp
->next
= functions
;
591 fp
->name
= xstrdup(name
);
592 fp
->type
= T_UNKNOWN
;
593 fp
->nbits
= fp
->nargs
= -1; /* nbits will be set correctly later. */
595 fp
->first
= fp
->last
= NULL
;
600 /* Parse an external (C) function declaration. This will look something like:
602 We're called just after seeing the ':'.
603 Return 0 if parsing is successful, 1 otherwise. */
604 int parseextern(struct function
*fp
, FILE *f
) {
607 if ((c
= getc(f
)) != '\n') {
609 "%s: %s(%d): extern declaration should be a lone `:'\n",
610 progname
, filename
, lineno
);
613 if (fp
->nbits
!= 0) {
615 "%s: %s(%d): extern functions should not have bitstrings\n",
616 progname
, filename
, lineno
);
620 fp
->first
= fp
->last
= NULL
;
625 /* A value supplied for a function (the third field in a description line).
626 In general there can be any number of such values, differing in the
627 bitpattern supplied. The mask and value fields describe the constant
628 bits in the bitpattern: mask indicates which bits they are and value
629 indicates the values of those bits. So this entry matches
630 ((x & mask) == value). */
634 struct bits
*bits
; /* List of named bitfields. */
635 struct string
*string
; /* Value of function when bitpattern matched. */
636 char done
; /* This entry has already been compiled. */
640 /* We've just seen a definition of function "name". Make a structure for it
641 if necessary, and a template entry that will describe the value given here.
643 struct function
*makefunction(char *name
) {
645 struct entry
*ep
= xmalloc(sizeof *ep
);
648 if (name
[0] == '%') {
653 fp
= findfunction(name
);
654 if (fp
->type
== T_UNKNOWN
)
656 else if (fp
->type
!= type
) {
657 fprintf(stderr
, "%s: %s(%d): function %s previously declared as %s, "
658 "here as %s\n", progname
, filename
, lineno
, name
,
659 typename
[fp
->type
], typename
[type
]);
665 if (fp
->first
!= NULL
)
674 /* A named bitfield within the bitpattern of a function entry, or within a
675 $[...] bitsplice. The mask covers the bitfield and the shift says how
676 many 0 bits there are after the last 1 in the mask. */
685 /* Parse the bitstring supplied for the given function. nbits says how many
686 bits there are; it can legitimately be 0. Return value is 0 on success. */
687 int parsebits(struct function
*fp
, char *bitstring
, int nbits
) {
690 else if (fp
->nbits
!= nbits
) {
691 fprintf(stderr
, "%s: %s(%d): bit string of length %d;\n",
692 progname
, filename
, lineno
, nbits
);
693 fprintf(stderr
, " function %s has bit strings of length %d\n",
694 fp
->name
, fp
->nbits
);
697 return parseentrybits(fp
->last
, bitstring
, nbits
, 0);
701 /* Parse a bitstring that is the pattern for a function entry or that is in a
702 $[...] bitsplice. Put the result in ep. Return value is 0 on success. */
703 int parseentrybits(struct entry
*ep
, char *bitstring
, int nbits
, int issplice
) {
706 bits mask
, value
, entrymask
;
710 for (i
= 0; i
< nbits
; i
++) {
711 bit
= bitstring
[nbits
- 1 - i
];
721 fprintf(stderr
, "%s: %s(%d): invalid character in bitstring: %c\n",
722 progname
, filename
, lineno
, bit
);
726 for (bp
= ep
->bits
; bp
!= NULL
; bp
= bp
->next
) {
727 if (bp
->name
== bit
) {
729 "%s: %s(%d): bitstring name %c used twice\n",
730 progname
, filename
, lineno
, bit
);
736 for (j
= i
+ 1; j
< nbits
&& bitstring
[nbits
- 1 - j
] == bit
; j
++)
738 bp
= xmalloc(sizeof *bp
);
740 bp
->mask
= entrymask
;
752 /* Parse a control line. This looks something like:
753 :bitstype unsigned int
754 in which case we will be called with name "bitstype" and
755 value "unsigned int". */
756 int parsecontrol(char *name
, char *value
) {
757 if (strcmp(name
, "bitstype") == 0)
758 bitstype
= xstrdup(value
);
760 fprintf(stderr
, "%s: %s(%d): unrecognised control keyword %s\n",
761 progname
, filename
, lineno
, name
);
768 /* A parameter to a function, e.g., x in:
769 %f aaa(%x) $a + $x */
777 /* Parse the parameters (x,y,...) to a function and put the result in fp.
778 The entry that is being built is fp->last. cp points to the opening
779 (; if it does not point to a ( then there are no parameters. If
780 this is the first entry for the function, fp->nargs will be -1 and
781 we will build up an argument list. Otherwise, fp->nargs will be
782 >= 0 and we will only check that the arguments here are consistent
783 with what went before. Return value is 0 on success. */
784 int parseargs(struct function
*fp
, FILE *f
, int *cp
) {
785 struct arg
**arglink
, *ap
;
800 if (fp
->nargs
>= 0 && nargs
> fp
->nargs
) {
802 "%s: %s(%d): %d arg(s) instead of %d for %s\n",
803 progname
, filename
, lineno
, nargs
, fp
->nargs
,
814 if (!isalpha(name
)) {
816 "%s: %s(%d): argument should be letter: %c\n",
817 progname
, filename
, lineno
, name
);
820 for (bp
= fp
->last
->bits
; bp
!= NULL
; bp
= bp
->next
) {
821 if (bp
->name
== name
) {
823 "%s: %s(%d): %c is a bitstring and an arg\n",
824 progname
, filename
, lineno
, name
);
828 if (fp
->nargs
>= 0) {
829 if ((*arglink
)->name
!= name
) {
831 "%s: %s(%d): arg %d of %s is %c not %c\n",
832 progname
, filename
, lineno
, nargs
, fp
->name
,
833 (*arglink
)->name
, name
);
836 if ((*arglink
)->type
!= t
) {
838 "%s: %s(%d): arg %c of %s: inconsistent type\n",
839 progname
, filename
, lineno
, name
, fp
->name
);
843 for (ap
= fp
->args
; ap
!= *arglink
; ap
= ap
->next
) {
844 if (ap
->name
== name
) {
846 "%s: %s(%d): argument name %c used twice\n",
847 progname
, filename
, lineno
, name
);
851 *arglink
= xmalloc(sizeof **arglink
);
852 (*arglink
)->name
= name
;
853 (*arglink
)->type
= t
;
855 arglink
= &(*arglink
)->next
;
861 "%s: %s(%d): bad character in argument list: %c\n"
862 " (arguments must be single letters)\n",
863 progname
, filename
, lineno
, *cp
);
874 if (width
> maxargwidth
)
876 } else if (fp
->nargs
!= nargs
) {
877 fprintf(stderr
, "%s: %s(%d): argument list of length %d;\n",
878 progname
, filename
, lineno
, nargs
);
879 fprintf(stderr
, " function %s has argument lists of length %d\n",
880 fp
->name
, fp
->nargs
);
888 /* Parse the string describing the value of this entry for our
889 function. Return 0 on success. */
890 int parsestring(struct function
*fp
, char *str
) {
893 t
= makestring(fp
, &fp
->last
->string
, &str
, NULL
, fp
->type
);
896 if (fp
->type
!= t
&& t
!= T_UNKNOWN
) {
897 fprintf(stderr
, "%s: %s(%d): function %s has inconsistent types\n",
898 progname
, filename
, lineno
, fp
->name
);
905 /* A parsed representation of the whole string describing a value of a
906 function, or certain strings within that (e.g., array indices). This is a
907 linked list of substrings whose type is given by the type field. */
911 S_TEXT
, S_BITSTRING
, S_BITSPLICE
, S_PARAMETER
, S_FUNCTIONCALL
, S_ARRAY
913 union value
{ /* The fields here correspond to the enum values. */
914 char *text
; /* plain text */
915 struct bits
*bits
; /* $x where x is a bitfield */
916 struct bitsplice
*bitsplice
; /* $[...] */
917 struct arg
*parameter
; /* $x where x is a parameter */
918 struct functioncall
*functioncall
; /* $func(...) */
919 struct array
*array
; /* {...}[...] */
923 /* The representation of a function call $func(...) in the description of a
925 struct functioncall
{
926 struct function
*function
;
927 struct stringlist
*args
;
930 /* The representation of an array selection {...|...}[...] in the description
931 of a function value. tempno is used when constructing a C variable name
932 that will contain the strings or numbers in an array. */
934 struct string
*index
; /* what's between [...] */
935 struct stringlist
*elements
; /* what's between {...} */
936 enum type type
; /* the type of each element */
940 /* A list of strings, being the list of arguments in a function call or the
941 list of elements of an array. This is a linked list of linked lists. */
943 struct stringlist
*next
;
945 struct string
*string
;
949 /* The following are the only characters with special meaning at the top level
950 of parsing of a function value. When parsing arrays or function calls,
951 other characters become special. */
952 #define MAKESTRING_MAGIC "${"/*}*/
955 /* Parse a function return-value string or substring and make a struct string
956 list for it. The string starts at *stringp and ends at a \0 or at any
957 character in the `magic' string other than { or $. *stringp is updated
958 to point to the terminating character. The parsed representation is put
959 at *stringlink. `fp' is the function whose return value is being parsed.
960 `targettype' is the expected type of the result, if known.
961 The return value is the actual type. */
962 enum type
makestring(struct function
*fp
, struct string
**stringlink
,
963 char **stringp
, char *magic
, enum type targettype
) {
965 struct string
*sp
, **firststringlink
;
968 enum type t
= targettype
, newt
;
971 magic
= MAKESTRING_MAGIC
;
973 firststringlink
= stringlink
;
976 sp
= xmalloc(sizeof *sp
);
980 if (strchr(magic
, *q
) != NULL
) {
981 if (*q
!= ')' || parenlevel
== 0)
995 } while (*++q
!= '\0');
998 sp
->value
.text
= q
= xmalloc(n
+ 1);
1006 } else if (*p
== '$') {
1007 if (parsedollar(fp
, &p
, sp
) != 0)
1015 newt
= sp
->value
.parameter
->type
;
1017 case S_FUNCTIONCALL
:
1018 newt
= sp
->value
.functioncall
->function
->type
;
1021 fprintf(stderr
, "makestring type %d\n", sp
->type
);
1024 } else if (*p
== '{'/*}*/) {
1025 if (parsearray(fp
, &p
, sp
, t
) != 0)
1027 newt
= sp
->value
.array
->type
;
1034 else if (newt
!= T_UNKNOWN
&& t
!= newt
) {
1035 if (stringlink
== firststringlink
) {
1036 fprintf(stderr
, "%s: %s(%d): expected %s type:\n", progname
,
1037 filename
, lineno
, typename
[t
]);
1038 showstringelement(stderr
, sp
);
1042 fprintf(stderr
, "%s: %s(%d): mixed types in string:\n",
1043 progname
, filename
, lineno
);
1044 showstring(stderr
, *firststringlink
);
1045 fprintf(stderr
, " -- %s\n", typename
[t
]);
1046 showstringelement(stderr
, sp
);
1047 fprintf(stderr
, " -- %s\n", typename
[newt
]);
1051 stringlink
= &sp
->next
;
1056 if (components
>= MAXBITS
) {
1057 fprintf(stderr
, "%s: %s(%d): excessively complicated string\n",
1058 progname
, filename
, lineno
);
1061 componentbits
|= 1 << components
;
1066 /* Parse a $ operation at **stringp and update *stringp to point past it.
1067 `fp' is the function whose return value is being parsed. The parsed
1068 item will be put at *sp. Return 0 on success, nonzero on error. */
1069 int parsedollar(struct function
*fp
, char **stringp
, struct string
*sp
) {
1077 while (isalnum(*p
) || *p
== '_')
1079 if (*start
== '[') {
1081 fprintf(stderr
, "%s: %s(%d): missing ] or bad character in $[\n",
1082 progname
, filename
, lineno
);
1086 return parsebitsplice(fp
, start
+ 1, p
- start
- 1, sp
);
1089 fprintf(stderr
, "%s: %s(%d): missing identifier after $\n", progname
,
1093 if (p
== start
+ 1) {
1094 if (findvariable(fp
, *start
, sp
) != 0)
1097 if (parsefunctioncall(fp
, start
, &p
, sp
) != 0)
1105 /* The representation of a $[...] bitsplice. It is parsed into a
1106 struct entry just as if it were a bitfield parameter, then analysed
1107 into a chain of struct bitsplicebits. These in conjunction with
1108 the constant portion of the struct entry will allow the bitsplice to
1109 be compiled. Each bitsplicebits element represents either a numeric
1110 argument to the current function, in which case it will be shifted
1111 into place; or a bitfield name from the bitfield description of the
1112 current function, in which case it will be shifted by the difference
1113 between the position of the bitfield in the argument and the position
1114 it occurs in the bitsplice. `shift' indicates how much to shift left
1115 the associated value; if it is negative the value is shifted right.
1116 For instance, in a function like this:
1117 %oh xx00(%y) $[yyxx]
1118 the bitsplicebits for y will have shift = 2 and value.arg pointing to y,
1119 and those for x will have shift = -2 and value.mask = binary 1100.
1120 As an optimisation, contiguous bitfields that are also contiguous in the
1121 bitsplice will be combined. For instance:
1122 %oh xxyy00 $[0xxyy0]
1123 will compile the same code as:
1124 %oh zzzz00 $[0zzzz0].
1125 As another optimisation, a bitfield that occupies the entire bitstring
1126 for a function will be treated like a parameter in that it will not be
1127 masked in the bitsplice. For instance:
1128 %oh xxxxxx $[0xxxxxx0]
1129 will compile the same code as:
1130 %oh (%x) $[0xxxxxx0]. */
1134 struct bitsplicebits
*splice
;
1136 struct bitsplicebits
{
1137 struct bitsplicebits
*next
;
1139 enum elementtype type
;
1147 int parsebitsplice(struct function
*fp
, char *bitstring
, int nbits
,
1148 struct string
*sp
) {
1149 struct bitsplice
*splicep
;
1150 struct bitsplicebits
*bsp
, *lastbsp
, **bspp
;
1152 int shift
, nfrombits
, ntobits
;
1155 splicep
= xmalloc(sizeof *splicep
);
1156 splicep
->nbits
= nbits
;
1157 if (parseentrybits(&splicep
->entry
, bitstring
, nbits
, 1) != 0)
1159 bspp
= &splicep
->splice
;
1161 for (bp
= splicep
->entry
.bits
; bp
!= NULL
; bp
= bp
->next
) {
1162 if (findvariable(fp
, bp
->name
, sp
) != 0)
1165 if (sp
->type
== S_BITSTRING
) {
1166 nfrombits
= bitcount(sp
->value
.bits
->mask
);
1167 ntobits
= bitcount(bp
->mask
);
1169 if (nfrombits
!= ntobits
) {
1170 fprintf(stderr
, "%s: %s(%d): warning: "
1171 "bitstring $%c %ser than its place "
1173 progname
, filename
, lineno
, bp
->name
,
1174 (nfrombits
> ntobits
) ? "bigg" : "small");
1177 shift
-= sp
->value
.bits
->shift
;
1179 /* See if this bitfield can be combined with a previous contiguous
1181 if (lastbsp
!= NULL
&& lastbsp
->type
== S_BITSTRING
1182 && lastbsp
->shift
== shift
) {
1183 lastbsp
->value
.mask
|= sp
->value
.bits
->mask
;
1187 assert(sp
->type
== S_PARAMETER
);
1188 if (sp
->value
.parameter
->type
!= T_INTEGER
) {
1190 "%s: %s(%d): variable %c in $[...] should be integer\n",
1191 progname
, filename
, lineno
, sp
->value
.parameter
->name
);
1195 *bspp
= bsp
= xmalloc(sizeof *bsp
);
1196 bsp
->type
= sp
->type
;
1198 if (sp
->type
== S_PARAMETER
)
1199 bsp
->value
.arg
= sp
->value
.parameter
;
1201 bsp
->value
.mask
= sp
->value
.bits
->mask
;
1207 /* Look for a spliced element that is the entire bitstring argument to
1208 this function and therefore doesn't need to be masked. */
1209 allbits
= allbitsset(fp
->nbits
);
1210 for (bsp
= splicep
->splice
; bsp
!= NULL
; bsp
= bsp
->next
) {
1211 if (bsp
->type
== S_BITSTRING
) {
1212 for (b
= bsp
->value
.mask
; b
!= 0 && !(b
& 1); b
>>= 1) ;
1214 bsp
->value
.mask
= 0;
1217 sp
->type
= S_BITSPLICE
;
1218 sp
->value
.bitsplice
= splicep
;
1223 int findvariable(struct function
*fp
, int name
, struct string
*sp
) {
1227 for (bp
= fp
->last
->bits
; bp
!= NULL
; bp
= bp
->next
) {
1228 if (bp
->name
== name
) {
1229 sp
->type
= S_BITSTRING
;
1230 sp
->value
.bits
= bp
;
1234 for (ap
= fp
->args
; ap
!= NULL
; ap
= ap
->next
) {
1235 if (ap
->name
== name
) {
1236 sp
->type
= S_PARAMETER
;
1237 sp
->value
.parameter
= ap
;
1241 fprintf(stderr
, "%s: %s(%d): undefined parameter %c\n", progname
, filename
,
1247 int parsefunctioncall(struct function
*fp
, char *start
, char **stringp
,
1248 struct string
*sp
) {
1250 struct functioncall
*fcp
;
1251 struct stringlist
**arglink
, *arg
;
1256 fprintf(stderr
, "%s: %s(%d): missing ( after function %.*s\n", progname
,
1257 filename
, lineno
, p
- start
, start
);
1260 sp
->type
= S_FUNCTIONCALL
;
1261 sp
->value
.functioncall
= fcp
= xmalloc(sizeof *fcp
);
1262 *p
= '\0'; /* Ugly. */
1263 fcp
->function
= findfunction(start
);
1265 arglink
= &fcp
->args
;
1268 arg
= xmalloc(sizeof *arg
);
1269 t
= makestring(fp
, &arg
->string
, &p
, MAKESTRING_MAGIC
",)",
1275 arglink
= &arg
->next
;
1289 int parsearray(struct function
*fp
, char **stringp
, struct string
*sp
,
1293 struct stringlist
**elementlink
, *element
;
1296 assert(*p
== '{'/*}*/);
1298 sp
->value
.array
= ap
= xmalloc(sizeof *ap
);
1300 elementlink
= &ap
->elements
;
1302 if (*++p
!= /*{*/'}') {
1304 element
= xmalloc(sizeof *element
);
1305 t
= makestring(fp
, &element
->string
, &p
,
1306 MAKESTRING_MAGIC
/*{*/"|}", t
);
1310 if (ap
->type
== T_UNKNOWN
)
1312 else if (t
!= T_UNKNOWN
&& ap
->type
!= t
) {
1313 fprintf(stderr
, "%s: %s(%d): mixed types in array:\n",
1314 progname
, filename
, lineno
);
1315 showstring(stderr
, ap
->elements
->string
);
1316 fprintf(stderr
, " -- %s\n", typename
[ap
->type
]);
1317 showstring(stderr
, element
->string
);
1318 fprintf(stderr
, " -- %s\n", typename
[t
]);
1321 *elementlink
= element
;
1322 elementlink
= &element
->next
;
1329 *elementlink
= NULL
;
1330 assert(*p
== /*{*/'}');
1332 fprintf(stderr
, "%s: %s(%d): missing [index] after array\n",
1333 progname
, filename
, lineno
);
1337 t
= makestring(fp
, &ap
->index
, &p
, MAKESTRING_MAGIC
"]", T_INTEGER
);
1340 if (t
== T_STRING
) {
1341 fprintf(stderr
, "%s: %s(%d): array index cannot be string:\n",
1342 progname
, filename
, lineno
);
1343 showstring(stderr
, ap
->index
);
1347 fprintf(stderr
, "%s: %s(%d): [ without ]\n", progname
, filename
,
1356 void dumpfunctions() {
1357 struct function
*fp
;
1359 for (fp
= functions
; fp
!= NULL
; fp
= fp
->next
)
1364 void dumpfunction(struct function
*fp
) {
1367 for (ep
= fp
->first
; ep
!= NULL
; ep
= ep
->next
)
1368 showentry(stderr
, fp
, ep
, 0);
1372 /* Entries are not shown exactly as they would be input, since \ would
1373 need to be provided before some characters such as $ or {. But the
1374 characters "|},]" pose a problem since a \ is only needed in certain
1375 contexts and is annoying otherwise. It's not worth doing this right,
1376 since it's only used for error messages. */
1377 void showentry(FILE *f
, struct function
*fp
, struct entry
*ep
, bits highlight
) {
1378 if (fp
->type
== T_INTEGER
)
1380 fprintf(f
, "%-*s ", maxfunctionname
+ 1, fp
->name
);
1381 if (fp
->nbits
== 0 && fp
->nargs
== 0)
1382 fprintf(f
, "%-*s", maxargwidth
, "()");
1384 showbits(f
, ep
, fp
->nbits
, 0);
1385 showargs(f
, fp
->args
, maxargwidth
- fp
->nbits
);
1388 showstring(f
, ep
->string
);
1390 if (highlight
!= 0) {
1391 fprintf(f
, "%-*s ", maxfunctionname
+ 1, "");
1392 showbits(f
, ep
, fp
->nbits
, highlight
);
1398 void showbits(FILE *f
, struct entry
*ep
, int nbits
, bits highlight
) {
1405 i
= 1 << (nbits
- 1);
1417 if (highlight
!= 0 || (ep
->mask
& i
)) {
1418 putc((value
& i
) ? one
: zero
, f
);
1421 assert(bp
!= NULL
&& (bp
->mask
& i
));
1425 } while (bp
->mask
& i
);
1432 void showargs(FILE *f
, struct arg
*ap
, int fieldwidth
) {
1443 isint
= (ap
->type
== T_INTEGER
);
1444 fprintf(f
, "%c%s%c", lastc
, isint
? "%" : "", ap
->name
);
1448 } while (ap
!= NULL
);
1451 fprintf(f
, "%-*s", fieldwidth
- width
, "");
1455 void showstring(FILE *f
, struct string
*sp
) {
1456 for ( ; sp
!= NULL
; sp
= sp
->next
)
1457 showstringelement(f
, sp
);
1461 void showstringelement(FILE *f
, struct string
*sp
) {
1462 struct bitsplice
*bsp
;
1466 fputs(sp
->value
.text
, f
);
1469 fprintf(f
, "$%c", sp
->value
.bits
->name
);
1473 bsp
= sp
->value
.bitsplice
;
1474 showbits(f
, &bsp
->entry
, bsp
->nbits
, 0);
1478 fprintf(f
, "$%c", sp
->value
.parameter
->name
);
1480 case S_FUNCTIONCALL
:
1481 showfunctioncall(f
, sp
->value
.functioncall
);
1484 showarray(f
, sp
->value
.array
);
1487 fprintf(stderr
, "showstring case %d\n", sp
->type
);
1493 void showfunctioncall(FILE *f
, struct functioncall
*fcp
) {
1494 struct stringlist
*sp
;
1497 fprintf(f
, "$%s(", fcp
->function
->name
);
1499 for (sp
= fcp
->args
; sp
!= NULL
; sp
= sp
->next
) {
1502 showstring(f
, sp
->string
);
1508 void showarray(FILE *f
, struct array
*ap
) {
1509 struct stringlist
*sp
;
1514 for (sp
= ap
->elements
; sp
!= NULL
; sp
= sp
->next
) {
1517 showstring(f
, sp
->string
);
1519 fputs(/*{*/"}[", f
);
1520 showstring(f
, ap
->index
);
1525 const char commonpreamble
[] = "\
1530 const char concatpreamble
[] = "\
1531 static char *dis_buf;\n\
1532 static int dis_bufindex, dis_buflen;\n\
1534 void *dis_alloc(size_t size)\n\
1537 int newindex = dis_bufindex + size;\n\
1538 if (newindex > dis_buflen) {\n\
1539 dis_buflen = newindex * 4;\n\
1540 dis_buf = malloc(dis_buflen);\n\
1541 /* We can't use realloc because there might be pointers extant into\n\
1542 the old buffer. So we waste the memory of the old buffer. We\n\
1543 should soon reach an adequate buffer size and stop leaking. */\n\
1544 if (dis_buf == 0) {\n\
1545 perror(\"malloc\");\n\
1548 dis_bufindex = 0;\n\
1550 p = dis_buf + dis_bufindex;\n\
1551 dis_bufindex = newindex;\n\
1557 dis_bufindex = 0;\n\
1562 const char concatdeclarations
[] = "\
1563 #include <string.h>\n\
1564 #include <stdlib.h>\n\
1565 #include <errno.h>\n\
1567 extern void *dis_realloc(void *p, size_t size); /* User-provided. */\n\
1568 void *dis_alloc(size_t size);\n\
1569 void dis_done(void);\n\
1572 const char nonconcatpreamble
[] = "\
1573 void dis_done() {}\n\
1577 int outputfunctions() {
1578 struct function
*fp
;
1580 outputidentity(stdout
);
1581 if (headerfilename
!= NULL
) {
1582 if ((headerfile
= fopen(headerfilename
, "w")) == NULL
) {
1583 fprintf(stderr
, "%s: create %s: %s\n", progname
, headerfilename
,
1587 outputidentity(headerfile
);
1588 fprintf(headerfile
, commonpreamble
, bitstype
);
1589 printf("\n#include \"%s\"\n", headerfilename
);
1591 printf(commonpreamble
, bitstype
);
1593 if (outputdeclarations() != 0)
1596 for (fp
= functions
; fp
!= NULL
; fp
= fp
->next
) {
1600 for (fp
= functions
; fp
!= NULL
; fp
= fp
->next
) {
1601 if (fp
->first
!= NULL
&& !fp
->isarray
) {
1602 if (outputfunction(fp
) != 0)
1610 void outputidentity(FILE *f
) {
1613 fprintf(f
, "/*\n * This file was generated by:\n *");
1614 for (p
= global_argv
; *p
!= NULL
; p
++)
1615 fprintf(f
, " %s", *p
);
1616 fprintf(f
, "\n */\n\n");
1620 int outputdeclarations() {
1621 FILE *f
= headerfile
? headerfile
: stdout
;
1622 struct function
*fp
;
1624 for (fp
= functions
; fp
!= NULL
; fp
= fp
->next
) {
1625 if (fp
->type
!= T_UNKNOWN
) {
1627 fprintf(f
, "extern ");
1628 if (fp
->fixedlength
> 0)
1629 fprintf(f
, "char %s[][%d]", fp
->name
, fp
->fixedlength
);
1631 compiletype(f
, &fp
->type
);
1632 fprintf(f
, "%s[]", fp
->name
);
1635 functionheader(f
, fp
);
1643 void outputconcats() {
1646 if (componentbits
& ~3) {
1647 fputs(concatdeclarations
, headerfile
? headerfile
: stdout
);
1648 fputs(concatpreamble
, stdout
);
1650 fputs(nonconcatpreamble
, stdout
);
1651 for (i
= 2; i
< MAXBITS
; i
++) {
1652 if (componentbits
& (1 << i
))
1658 void outputconcat(int n
) {
1664 outputconcatheader(headerfile
, n
);
1665 fprintf(headerfile
, ";\n");
1667 outputconcatheader(stdout
, n
);
1668 printf("\n{\n void *p;\n int len = ");
1670 for (i
= 0; i
< n
; i
++) {
1671 printf("%sstrlen(p%d)", last
, i
);
1674 printf(";\n p = dis_alloc(len + 1);\n return ");
1675 for (i
= 1; i
< n
; i
++)
1677 printf("strcpy(p, p0)");
1678 for (i
= 1; i
< n
; i
++)
1679 printf(", p%d)", i
);
1684 void outputconcatheader(FILE *f
, int n
) {
1688 fprintf(f
, "char *dis_concat%d(", n
);
1689 for (i
= 0; i
< n
; i
++) {
1690 fprintf(f
, "%schar *p%d", last
, i
);
1698 struct function
*fp
;
1700 struct string
*estr
, *indexstr
;
1703 for (fp
= functions
; fp
!= NULL
; fp
= fp
->next
) {
1704 if (fp
->nbits
> 0 && fp
->nargs
> 0)
1709 if (ep
== NULL
|| ep
->next
!= NULL
)
1712 if (estr
== NULL
|| estr
->next
!= NULL
|| estr
->type
!= S_ARRAY
)
1714 indexstr
= estr
->value
.array
->index
;
1715 if (indexstr
->next
!= NULL
)
1717 if (fp
->nbits
> 0) {
1719 if (bp
== NULL
|| bp
->next
!= NULL
|| bp
->shift
!= 0)
1721 if (bp
->mask
!= allbitsset(fp
->nbits
))
1723 if (indexstr
->type
!= S_BITSTRING
|| indexstr
->value
.bits
!= bp
)
1726 if (indexstr
->type
!= S_PARAMETER
1727 || indexstr
->value
.parameter
!= fp
->args
)
1730 if (!simplearray(estr
->value
.array
))
1734 (fp
->type
== T_INTEGER
) ? 0 : checkfixedlength(estr
->value
.array
);
1739 int checkfixedlength(struct array
*ap
) {
1740 int len
, maxlen
, wasted
, n
;
1741 struct stringlist
*lp
;
1744 for (lp
= ap
->elements
; lp
!= NULL
; lp
= lp
->next
) {
1745 if (lp
->string
== NULL
)
1747 assert(lp
->string
->type
== S_TEXT
);
1748 len
= strlen(lp
->string
->value
.text
);
1752 for (wasted
= n
= 0, lp
= ap
->elements
; lp
!= NULL
; n
++, lp
= lp
->next
) {
1753 if (lp
->string
== NULL
)
1755 wasted
+= maxlen
- strlen(lp
->string
->value
.text
);
1757 if (wasted
< n
* sizeof(char *)) /* Should be target's sizeof. */
1763 int outputfunction(struct function
*fp
) {
1765 functionheader(stdout
, fp
);
1766 printf("\n{\n"/*}*/);
1767 switch (functionswitch(fp
, 0, 0)) {
1772 fprintf(stderr
, "%s: warning: not all cases of %s covered\n",
1773 progname
, fp
->name
);
1781 void functionarray(struct function
*fp
) {
1784 ap
= fp
->first
->string
->value
.array
;
1786 compilesimplearray(&fp
->type
, fp
->name
, 0, ap
);
1790 void functionheader(FILE *f
, struct function
*fp
) {
1794 compiletype(f
, &fp
->type
);
1795 fprintf(f
, "%s(", fp
->name
);
1797 if (fp
->nbits
> 0) {
1798 fprintf(f
, "bits code");
1801 for (ap
= fp
->args
; ap
!= NULL
; ap
= ap
->next
) {
1803 compiletype(f
, &ap
->type
);
1813 int simplearray(struct array
*ap
) {
1814 struct stringlist
*lp
;
1816 for (lp
= ap
->elements
; lp
!= NULL
; lp
= lp
->next
) {
1817 if (lp
->string
!= NULL
1818 && (lp
->string
->next
!= NULL
|| lp
->string
->type
!= S_TEXT
))
1821 return (lp
== NULL
);
1825 void compiletype(FILE *f
, enum type
*tp
) {
1831 fprintf(f
, "char *");
1834 fprintf(f
, "bits ");
1837 fprintf(stderr
, "compiletype type %d\n", *tp
);
1843 /* Generate code for entries in function fp whose bitstring b satisfies
1844 the constraint (b & mask) == value. Return 1 if generated switch
1845 always does `return', 0 if not, -1 on error.
1846 The algorithm is as follows. Scan the eligible entries to find the
1847 largest set of bits not in the passed-in mask which always have a
1848 constant value (are not variable). One `default' entry is allowed
1849 all of whose bits are variable. For each value of the constant bits,
1850 generate a `switch' case and invoke the function recursively with
1851 that value included in the constraint parameters. The recursion
1852 stops when no set of constant bits is found, perhaps because the
1853 mask parameter has all bits set.
1854 This algorithm could be improved. Currently it will fail if there
1855 are input lines "xxyy", "00xx" and "yy00", each of which is default with
1856 respect to the others. The correct behaviour would then be to select
1857 a bit that is sometimes constant and deal with those cases first.
1858 But this problem has not yet arisen in real life. */
1859 int functionswitch(struct function
*fp
, bits mask
, bits value
) {
1860 struct entry
*ep
, *defaultcase
;
1861 bits allbits
, constbits
, missingcases
;
1862 int nhits
, ncases
, nconstbits
, alwaysreturns
;
1865 allbits
= allbitsset(fp
->nbits
);
1866 constbits
= allbits
& ~mask
;
1870 "functionswitch(%s): (x & 0x%lx) == 0x%lx; const == 0x%lx\n",
1871 fp
->name
, mask
, value
, constbits
);
1876 for (ep
= fp
->first
; ep
!= NULL
; ep
= ep
->next
) {
1877 /* If this is not one of the entries under consideration, skip. */
1879 || (ep
->mask
& mask
) != mask
|| (ep
->value
& mask
) != value
)
1883 showentry(stderr
, fp
, ep
, 0);
1885 /* If this entry has no constant bits in the still-variable portion,
1886 it's the default. */
1887 if ((constbits
& ep
->mask
) == 0) {
1888 if (defaultcase
!= NULL
) {
1890 "%s: function %s: unable to distinguish between:\n",
1891 progname
, fp
->name
);
1892 showentry(stderr
, fp
, defaultcase
, 0);
1893 showentry(stderr
, fp
, ep
, 0);
1899 fprintf(stderr
, "^^ default case\n");
1902 if (debug
&& (constbits
& ~ep
->mask
)) {
1904 fprintf(stderr
, "const now 0x%lx\n", constbits
& ep
->mask
);
1906 constbits
&= ep
->mask
;
1912 if (constbits
== allbits
)
1913 printf("switch (code) {\n"/*}*/);
1915 printf("switch (code & 0x%lx) {\n"/*}*/, constbits
);
1916 for (ep
= fp
->first
; ep
!= NULL
; ep
= ep
->next
) {
1917 /* If this is not one of the entries under consideration, skip. */
1918 if ((ep
->mask
& mask
) != mask
|| (ep
->value
& mask
) != value
)
1920 if (ep
->done
|| ep
== defaultcase
)
1924 printf("case 0x%lx:\n", ep
->value
& constbits
);
1925 switch (functionswitch(fp
, mask
| constbits
,
1926 value
| (ep
->value
& constbits
))) {
1931 indentation
++; indent(); indentation
--;
1938 nconstbits
= bitcount(constbits
);
1939 missingcases
= ((nconstbits
== MAXBITS
) ? 0 : 1 << nconstbits
) - ncases
;
1940 if (alwaysreturns
) {
1941 switch (missingcases
) {
1943 if (defaultcase
!= NULL
) {
1944 fprintf(stderr
, "%s: warning: redundant entry:\n", progname
);
1945 showentry(stderr
, fp
, defaultcase
, 0);
1950 if (defaultcase
!= NULL
&& nconstbits
!= 0) {
1952 "%s: warning: variable bit(s) could be constant:\n",
1954 showentry(stderr
, fp
, defaultcase
, constbits
);
1962 if (defaultcase
!= NULL
) {
1963 /* If defaultcase has some constant bits of its own, recursion will
1964 check that they have the required value. */
1965 if ((defaultcase
->mask
& ~mask
) == 0) {
1967 if (compilestring(-1, defaultcase
->string
, fp
->type
) != 0)
1969 defaultcase
->done
= 1;
1972 alwaysreturns
= functionswitch(fp
, mask
, value
);
1977 return alwaysreturns
;
1981 int compilestring(int assignto
, struct string
*sp
, enum type type
) {
1984 tempno
= walkstring(sp
, COUNTARRAYS
, assignto
);
1985 if (tempno
> assignto
) {
1989 (void) walkstring(sp
, DECLAREARRAYS
, assignto
);
1990 if (walkstring(sp
, COMPILEARRAYS
, assignto
) < 0)
1993 if (compilecheckedstring(assignto
, sp
, type
) != 0)
1995 if (tempno
> assignto
) {
2004 int compilecheckedstring(int assignto
, struct string
*sp
, enum type type
) {
2005 compileassign(assignto
);
2006 if (compileconcat(sp
, type
) != 0)
2013 void compileassign(int assignto
) {
2018 compiletemp(assignto
);
2024 void compiletemp(int tempno
) {
2025 printf("t__%d", tempno
);
2029 void compiletext(char *s
) {
2032 for ( ; *s
!= '\0'; s
++) {
2045 int compileconcat(struct string
*sp
, enum type type
) {
2051 return compilenull(type
);
2052 if (sp
->next
== NULL
)
2053 return compilesimple(sp
, type
);
2054 if (type
!= T_INTEGER
) {
2055 for (elements
= 0, sp1
= sp
; sp1
!= NULL
; elements
++, sp1
= sp1
->next
) ;
2056 printf("dis_concat%d(", elements
);
2059 for (sp1
= sp
; sp1
!= NULL
; sp1
= sp1
->next
) {
2061 if (type
!= T_INTEGER
)
2063 if (sp1
->type
== S_ARRAY
)
2064 compilearrayref(sp1
->value
.array
);
2066 if (compilesimple(sp1
, type
) != 0)
2069 if (type
!= T_INTEGER
)
2075 int compilenull(enum type type
) {
2076 if (type
== T_INTEGER
) {
2077 fprintf(stderr
, "%s: empty integer expression\n", progname
);
2085 int compilesimple(struct string
*sp
, enum type type
) {
2087 return compilenull(type
);
2090 if (type
== T_INTEGER
)
2091 printf("%s", sp
->value
.text
);
2093 compiletext(sp
->value
.text
);
2096 compilebitstring(sp
->value
.bits
);
2099 compilebitsplice(sp
->value
.bitsplice
);
2102 putchar(sp
->value
.parameter
->name
);
2104 case S_FUNCTIONCALL
:
2105 return compilefunctioncall(sp
);
2107 if (compilearrayref(sp
->value
.array
) != 0)
2111 fprintf(stderr
, "compilesimple case %d", sp
->type
);
2118 int compilearrayref(struct array
*ap
) {
2119 compiletemp(ap
->tempno
);
2120 if (simplearray(ap
)) {
2122 if (compileconcat(ap
->index
, T_INTEGER
) != 0)
2130 int compilefunctioncall(struct string
*sp
) {
2131 struct function
*fp
;
2132 struct stringlist
*actualp
;
2133 struct arg
*formalp
;
2136 enum type formaltype
;
2138 assert(sp
->type
== S_FUNCTIONCALL
);
2139 fp
= sp
->value
.functioncall
->function
;
2140 printf("%s%c", fp
->name
, fp
->isarray
? '[' : '(');
2144 actualp
= sp
->value
.functioncall
->args
;
2145 while (actualp
!= NULL
) {
2148 formaltype
= T_INTEGER
;
2150 if (formalp
== NULL
) {
2151 fprintf(stderr
, "%s: too many arguments to %s:\n", progname
,
2153 showstring(stderr
, sp
);
2157 formaltype
= formalp
->type
;
2158 formalp
= formalp
->next
;
2160 if (actualp
->type
!= T_UNKNOWN
&& actualp
->type
!= formaltype
) {
2161 fprintf(stderr
, "%s: argument to %s has the wrong type:\n",
2162 progname
, fp
->name
);
2163 showstring(stderr
, actualp
->string
);
2169 if (compileconcat(actualp
->string
, formaltype
) != 0)
2171 actualp
= actualp
->next
;
2173 putchar(fp
->isarray
? ']' : ')');
2178 int walkstring(struct string
*sp
, enum walkstringop op
, int tempno
) {
2179 struct stringlist
*lp
;
2182 for ( ; sp
!= NULL
; sp
= sp
->next
) {
2185 ap
= sp
->value
.array
;
2186 for (lp
= ap
->elements
; lp
!= NULL
; lp
= lp
->next
)
2187 tempno
= walkstring(lp
->string
, op
, tempno
);
2188 tempno
= walkstring(ap
->index
, op
, tempno
);
2189 ap
->tempno
= ++tempno
;
2192 if (simplearray(ap
)) {
2195 compilesimplearray(&ap
->type
, NULL
, tempno
, ap
);
2200 if (!simplearray(ap
))
2201 if (compilearray(ap
) != 0)
2208 case S_FUNCTIONCALL
:
2209 for (lp
= sp
->value
.functioncall
->args
; lp
!= NULL
; lp
= lp
->next
)
2210 tempno
= walkstring(lp
->string
, op
, tempno
);
2220 int compilearray(struct array
*ap
) {
2221 struct stringlist
*ep
;
2226 if (compileconcat(ap
->index
, T_INTEGER
) != 0)
2228 printf(") {\n"/*}*/);
2229 for (i
= 0, ep
= ap
->elements
; ep
!= NULL
; i
++, ep
= ep
->next
) {
2231 printf("case %d:\n", i
);
2233 if (compilecheckedstring(ap
->tempno
, ep
->string
, ap
->type
) != 0)
2245 void compilesimplearray(enum type
*tp
, char *name
, int num
, struct array
*ap
) {
2246 struct stringlist
*lp
;
2249 fixedlength
= (*tp
== T_INTEGER
) ? 0 : checkfixedlength(ap
);
2250 if (fixedlength
> 0)
2253 compiletype(stdout
, tp
);
2259 if (fixedlength
> 0)
2260 printf("[%d]", fixedlength
);
2261 printf(" = {\n"/*}*/);
2263 for (lp
= ap
->elements
; lp
!= NULL
; lp
= lp
->next
) {
2265 compilesimple(lp
->string
, lp
->type
);
2270 printf(/*{*/"};\n");
2274 void declarearray(struct array
*ap
) {
2276 compiletype(stdout
, &ap
->type
);
2277 compiletemp(ap
->tempno
);
2282 void compilebitstring(struct bits
*bp
) {
2286 printf("code & 0x%lx", bp
->mask
);
2288 printf(") >> %d", bp
->shift
);
2293 void compilebitsplice(struct bitsplice
*splicep
) {
2294 struct bitsplicebits
*bsp
;
2298 for (bsp
= splicep
->splice
; bsp
!= NULL
; bsp
= bsp
->next
) {
2301 if (bsp
->type
== S_PARAMETER
)
2302 putchar(bsp
->value
.arg
->name
);
2304 assert(bsp
->type
== S_BITSTRING
);
2305 if (bsp
->value
.mask
== 0)
2308 printf("(code & 0x%lx)", bsp
->value
.mask
);
2311 printf(" << %d", bsp
->shift
);
2312 else if (bsp
->shift
< 0)
2313 printf(" >> %d", -bsp
->shift
);
2315 if (splicep
->entry
.value
!= 0)
2316 printf("%s0x%lx", last
, splicep
->entry
.value
);
2321 int bitcount(bits x
) {
2324 for (nbits
= 0; x
!= 0; x
>>= 1) {
2332 bits
allbitsset(int nbits
) {
2333 return (nbits
== MAXBITS
) ? ~0 : (1 << nbits
) - 1;
2337 void findent(FILE *f
) {
2340 for (i
= 1; i
< indentation
; i
+= 2)
2342 if (i
== indentation
)
2352 void *xrealloc(char *oldp
, size_t size
) {
2358 p
= realloc(oldp
, size
);
2360 fprintf(stderr
, "%s: allocate of %d bytes failed: %s\n", progname
,
2361 (int) size
, strerror(errno
));
2368 void *xmalloc(size_t size
) {
2369 return xrealloc(NULL
, size
);
2373 void *xstrdup(char *s
) {
2376 p
= xmalloc(strlen(s
) + 1);
2382 int prematureeof() {
2383 fprintf(stderr
, "%s: %s(%d): premature end of file\n", progname
, filename
,