Debian Codegraph @

mip/common.c

raw symbols SCIP
1 /* $Id: common.c,v 1.125 2018/09/15 15:21:45 ragge Exp $ */
2 /*
3 * Copyright (c) 2003 Anders Magnusson ([email protected]).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*
28 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 *
34 * Redistributions of source code and documentation must retain the above
35 * copyright notice, this list of conditions and the following disclaimer.
36 * Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditionsand the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed or owned by Caldera
42 * International, Inc.
43 * Neither the name of Caldera International, Inc. nor the names of other
44 * contributors may be used to endorse or promote products derived from
45 * this software without specific prior written permission.
46 *
47 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
48 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
49 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
50 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
51 * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
52 * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
56 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
57 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58 * POSSIBILITY OF SUCH DAMAGE.
59 */
60
61 #include <stdarg.h>
62 #include <stddef.h>
63 #include <stdlib.h>
64 #include <stdio.h>
65 #include <string.h>
66
67 #include "pass2.h"
68 #include "unicode.h"
69
70 # ifndef EXIT
71 # define EXIT exit
72 # endif
73
74 int nerrors = 0; /* number of errors */
75 extern char *ftitle;
76 int lineno;
77 int savstringsz, newattrsz, nodesszcnt;
78
79 int warniserr = 0;
80
81 #ifndef WHERE
82 #define WHERE(ch) fprintf(stderr, "%s, line %d: ", ftitle, lineno);
83 #endif
84
85 static void
86 incerr(void)
87 {
88 if (++nerrors > 30)
89 cerror("too many errors");
90 }
91
92 /*
93 * nonfatal error message
94 * the routine where is different for pass 1 and pass 2;
95 * it tells where the error took place
96 */
97 void
98 uerror(const char *s, ...)
99 {
100 va_list ap;
101
102 va_start(ap, s);
103 WHERE('u');
104 vfprintf(stderr, s, ap);
105 fprintf(stderr, "\n");
106 va_end(ap);
107 incerr();
108 }
109
110 /*
111 * compiler error: die
112 */
113 void
114 cerror(const char *s, ...)
115 {
116 va_list ap;
117
118 va_start(ap, s);
119 WHERE('c');
120
121 /* give the compiler the benefit of the doubt */
122 if (nerrors && nerrors <= 30) {
123 fprintf(stderr,
124 "cannot recover from earlier errors: goodbye!\n");
125 } else {
126 fprintf(stderr, "compiler error: ");
127 vfprintf(stderr, s, ap);
128 fprintf(stderr, "\n");
129 }
130 va_end(ap);
131 EXIT(1);
132 }
133
134 /*
135 * warning
136 */
137 void
138 u8error(const char *s, ...)
139 {
140 va_list ap;
141 va_start(ap, s);
142 WHERE('w');
143 fprintf(stderr, "warning: ");
144 vfprintf(stderr, s, ap);
145 fprintf(stderr, "\n");
146 va_end(ap);
147 if (warniserr)
148 incerr();
149 }
150
151 #ifdef MKEXT
152 int wdebug;
153 #endif
154
155 /*
156 * warning
157 */
158 void
159 werror(const char *s, ...)
160 {
161 extern int wdebug;
162 va_list ap;
163
164 if (wdebug)
165 return;
166 va_start(ap, s);
167 WHERE('w');
168 fprintf(stderr, "warning: ");
169 vfprintf(stderr, s, ap);
170 fprintf(stderr, "\n");
171 va_end(ap);
172 if (warniserr)
173 incerr();
174 }
175
176 #ifndef MKEXT
177
178 struct Warning {
179 char *flag;
180 char warn;
181 char err;
182 char *fmt;
183 };
184
185 /*
186 * conditional warnings
187 */
188 struct Warning Warnings[] = {
189 {
190 "truncate", 0, 0,
191 "conversion from '%s' to '%s' may alter its value"
192 }, {
193 "strict-prototypes", 0, 0,
194 "function declaration isn't a prototype"
195 }, {
196 "missing-prototypes", 0, 0,
197 "no previous prototype for `%s'"
198 }, {
199 "implicit-int", 0, 0,
200 "return type defaults to `int'",
201 }, {
202 "implicit-function-declaration", 0, 0,
203 "implicit declaration of function '%s'"
204 }, {
205 "shadow", 0, 0,
206 "declaration of '%s' shadows a %s declaration"
207 }, {
208 "pointer-sign", 0, 0,
209 "pointer sign mismatch"
210 }, {
211 "sign-compare", 0, 0,
212 "comparison between signed and unsigned"
213 }, {
214 "unknown-pragmas", 0, 0,
215 "ignoring #pragma %s %s"
216 }, {
217 "unreachable-code", 0, 0,
218 "statement not reached"
219 }, {
220 "deprecated-declarations", 1, 0,
221 "`%s' is deprecated"
222 }, {
223 "attributes", 1, 0,
224 "unsupported attribute `%s'"
225 }, {
226 "uninitialized", 0, 0,
227 "uninitialized variable"
228 }, { NULL }
229 };
230
231 /*
232 * set the warn/err status of a conditional warning
233 */
234 int
235 Wset(char *str, int warn, int err)
236 {
237 struct Warning *w = Warnings;
238
239 for (w = Warnings; w->flag; w++) {
240 if (strcmp(w->flag, str) == 0) {
241 w->warn = warn;
242 w->err = err;
243 return 0;
244 }
245 }
246 return 1;
247 }
248
249 /*
250 * handle a conditional warning flag.
251 */
252 void
253 Wflags(char *str)
254 {
255 struct Warning *w;
256 int isset, iserr;
257
258 /* handle -Werror specially */
259 if (strcmp("error", str) == 0) {
260 for (w = Warnings; w->flag; w++)
261 w->err = 1;
262
263 warniserr = 1;
264 return;
265 }
266
267 isset = 1;
268 if (strncmp("no-", str, 3) == 0) {
269 str += 3;
270 isset = 0;
271 }
272
273 iserr = 0;
274 if (strncmp("error=", str, 6) == 0) {
275 str += 6;
276 iserr = 1;
277 }
278
279 for (w = Warnings; w->flag; w++) {
280 if (strcmp(w->flag, str) != 0)
281 continue;
282
283 if (isset) {
284 if (iserr)
285 w->err = 1;
286 w->warn = 1;
287 } else if (iserr) {
288 w->err = 0;
289 } else {
290 w->warn = 0;
291 }
292
293 return;
294 }
295
296 fprintf(stderr, "unrecognised warning option '%s'\n", str);
297 }
298
299 /*
300 * emit a conditional warning
301 */
302 void
303 warner(int type, ...)
304 {
305 va_list ap;
306 char *t;
307 #ifndef PASS2
308 extern int issyshdr;
309
310 if (issyshdr && type == Wtruncate)
311 return; /* Too many false positives */
312 #endif
313
314 if (Warnings[type].warn == 0)
315 return; /* no warning */
316 if (Warnings[type].err) {
317 t = "error";
318 incerr();
319 } else
320 t = "warning";
321
322 va_start(ap, type);
323 fprintf(stderr, "%s:%d: %s: ", ftitle, lineno, t);
324 vfprintf(stderr, Warnings[type].fmt, ap);
325 fprintf(stderr, "\n");
326 va_end(ap);
327 }
328 #endif /* MKEXT */
329
330 #ifndef MKEXT
331 static NODE *freelink;
332 int usednodes;
333
334 #ifndef LANG_F77
335 NODE *
336 talloc(void)
337 {
338 register NODE *p;
339
340 usednodes++;
341
342 if (freelink != NULL) {
343 p = freelink;
344 freelink = p->n_left;
345 if (p->n_op != FREE)
346 cerror("node not FREE: %p", p);
347 if (ndebug)
348 printf("alloc node %p from freelist\n", p);
349 return p;
350 }
351
352 p = permalloc(sizeof(NODE));
353 nodesszcnt += sizeof(NODE);
354 p->n_op = FREE;
355 if (ndebug)
356 printf("alloc node %p from memory\n", p);
357 return p;
358 }
359 #endif
360
361 /*
362 * make a fresh copy of p
363 */
364 NODE *
365 tcopy(NODE *p)
366 {
367 NODE *q;
368
369 q = talloc();
370 *q = *p;
371
372 switch (optype(q->n_op)) {
373 case BITYPE:
374 q->n_right = tcopy(p->n_right);
375 case UTYPE:
376 q->n_left = tcopy(p->n_left);
377 }
378
379 return(q);
380 }
381
382 #ifndef LANG_F77
383 /*
384 * ensure that all nodes have been freed
385 */
386 void
387 tcheck(void)
388 {
389 #ifdef LANG_CXX
390 extern int inlnodecnt;
391 #else
392 #define inlnodecnt 0
393 #endif
394
395 if (nerrors)
396 return;
397
398 if ((usednodes - inlnodecnt) != 0)
399 cerror("usednodes == %d, inlnodecnt %d", usednodes, inlnodecnt);
400 }
401 #endif
402
403 /*
404 * free the tree p
405 */
406 void
407 tfree(NODE *p)
408 {
409 if (p->n_op != FREE)
410 walkf(p, (void (*)(NODE *, void *))nfree, 0);
411 }
412
413 /*
414 * Free a node, and return its left descendant.
415 * It is up to the caller to know whether the return value is usable.
416 */
417 NODE *
418 nfree(NODE *p)
419 {
420 NODE *l;
421 #ifdef PCC_DEBUG_NODES
422 NODE *q;
423 #endif
424
425 if (p == NULL)
426 cerror("freeing blank node!");
427
428 l = p->n_left;
429 if (p->n_op == FREE)
430 cerror("freeing FREE node", p);
431 #ifdef PCC_DEBUG_NODES
432 q = freelink;
433 while (q != NULL) {
434 if (q == p)
435 cerror("freeing free node %p", p);
436 q = q->n_left;
437 }
438 #endif
439
440 if (ndebug)
441 printf("freeing node %p\n", p);
442 p->n_op = FREE;
443 p->n_left = freelink;
444 freelink = p;
445 usednodes--;
446 return l;
447 }
448 #endif
449
450 #ifdef LANG_F77
451 #define OPTYPE(x) optype(x)
452 #else
453 #define OPTYPE(x) coptype(x)
454 #endif
455
456 #ifdef MKEXT
457 #define coptype(o) (dope[o]&TYFLG)
458 #else
459 #ifndef PASS2
460 int cdope(int);
461 #define coptype(o) (cdope(o)&TYFLG)
462 #else
463 #define coptype(o) (dope[o]&TYFLG)
464 #endif
465 #endif
466
467 void
468 fwalk(NODE *t, void (*f)(NODE *, int, int *, int *), int down)
469 {
470
471 int down1, down2;
472
473 more:
474 down1 = down2 = 0;
475
476 (*f)(t, down, &down1, &down2);
477
478 switch (OPTYPE( t->n_op )) {
479
480 case BITYPE:
481 fwalk( t->n_left, f, down1 );
482 t = t->n_right;
483 down = down2;
484 goto more;
485
486 case UTYPE:
487 t = t->n_left;
488 down = down1;
489 goto more;
490
491 }
492 }
493
494 void
495 walkf(NODE *t, void (*f)(NODE *, void *), void *arg)
496 {
497 int opty;
498
499
500 opty = OPTYPE(t->n_op);
501
502 if (opty != LTYPE)
503 walkf( t->n_left, f, arg );
504 if (opty == BITYPE)
505 walkf( t->n_right, f, arg );
506 (*f)(t, arg);
507 }
508
509 int dope[DSIZE];
510 char *opst[DSIZE];
511
512 struct dopest {
513 int dopeop;
514 char opst[8];
515 int dopeval;
516 } indope[] = {
517 { NAME, "NAME", LTYPE, },
518 { REG, "REG", LTYPE, },
519 { OREG, "OREG", LTYPE, },
520 { TEMP, "TEMP", LTYPE, },
521 { ICON, "ICON", LTYPE, },
522 { FCON, "FCON", LTYPE, },
523 { CCODES, "CCODES", LTYPE, },
524 { UMINUS, "U-", UTYPE, },
525 { UMUL, "U*", UTYPE, },
526 { FUNARG, "FUNARG", UTYPE, },
527 { UCALL, "UCALL", UTYPE|CALLFLG, },
528 { UFORTCALL, "UFCALL", UTYPE|CALLFLG, },
529 { COMPL, "~", UTYPE, },
530 { FORCE, "FORCE", UTYPE, },
531 { XARG, "XARG", UTYPE, },
532 { XASM, "XASM", BITYPE, },
533 { SCONV, "SCONV", UTYPE, },
534 { PCONV, "PCONV", UTYPE, },
535 { PLUS, "+", BITYPE|FLOFLG|SIMPFLG|COMMFLG, },
536 { MINUS, "-", BITYPE|FLOFLG|SIMPFLG, },
537 { MUL, "*", BITYPE|FLOFLG|MULFLG, },
538 { AND, "&", BITYPE|SIMPFLG|COMMFLG, },
539 { CM, ",", BITYPE, },
540 { ASSIGN, "=", BITYPE|ASGFLG, },
541 { DIV, "/", BITYPE|FLOFLG|MULFLG|DIVFLG, },
542 { MOD, "%", BITYPE|DIVFLG, },
543 { LS, "<<", BITYPE|SHFFLG, },
544 { RS, ">>", BITYPE|SHFFLG, },
545 { OR, "|", BITYPE|COMMFLG|SIMPFLG, },
546 { ER, "^", BITYPE|COMMFLG|SIMPFLG, },
547 { CALL, "CALL", BITYPE|CALLFLG, },
548 { FORTCALL, "FCALL", BITYPE|CALLFLG, },
549 { EQ, "==", BITYPE|LOGFLG, },
550 { NE, "!=", BITYPE|LOGFLG, },
551 { LE, "<=", BITYPE|LOGFLG, },
552 { LT, "<", BITYPE|LOGFLG, },
553 { GE, ">=", BITYPE|LOGFLG, },
554 { GT, ">", BITYPE|LOGFLG, },
555 { UGT, "UGT", BITYPE|LOGFLG, },
556 { UGE, "UGE", BITYPE|LOGFLG, },
557 { ULT, "ULT", BITYPE|LOGFLG, },
558 { ULE, "ULE", BITYPE|LOGFLG, },
559 { CBRANCH, "CBRANCH", BITYPE, },
560 { FLD, "FLD", UTYPE, },
561 { PMCONV, "PMCONV", BITYPE, },
562 { PVCONV, "PVCONV", BITYPE, },
563 { RETURN, "RETURN", BITYPE|ASGFLG|ASGOPFLG, },
564 { GOTO, "GOTO", UTYPE, },
565 { STASG, "STASG", BITYPE|ASGFLG, },
566 { STARG, "STARG", UTYPE, },
567 { STCALL, "STCALL", BITYPE|CALLFLG, },
568 { USTCALL, "USTCALL", UTYPE|CALLFLG, },
569 { ADDROF, "U&", UTYPE, },
570
571 { -1, "", 0 },
572 };
573
574 void
575 mkdope(void)
576 {
577 struct dopest *q;
578
579 for( q = indope; q->dopeop >= 0; ++q ){
580 dope[q->dopeop] = q->dopeval;
581 opst[q->dopeop] = q->opst;
582 }
583 }
584
585 /*
586 * output a nice description of the type of t
587 */
588 void
589 tprint(TWORD t, TWORD q)
590 {
591 static char * tnames[BTMASK+1] = {
592 "undef",
593 "bool",
594 "char",
595 "uchar",
596 "short",
597 "ushort",
598 "int",
599 "unsigned",
600 "long",
601 "ulong",
602 "longlong",
603 "ulonglong",
604 "float",
605 "double",
606 "ldouble",
607 "strty",
608 "unionty",
609 "enumty",
610 "moety",
611 "void",
612 "signed", /* pass1 */
613 "farg", /* pass1 */
614 "fimag", /* pass1 */
615 "dimag", /* pass1 */
616 "limag", /* pass1 */
617 "fcomplex", /* pass1 */
618 "dcomplex", /* pass1 */
619 "lcomplex", /* pass1 */
620 "enumty", /* pass1 */
621 "?", "?", "?"
622 };
623
624 for(;; t = DECREF(t), q = DECREF(q)) {
625 if (ISCON(q))
626 putchar('C');
627 if (ISVOL(q))
628 putchar('V');
629
630 if (ISPTR(t))
631 printf("PTR ");
632 else if (ISFTN(t))
633 printf("FTN ");
634 else if (ISARY(t))
635 printf("ARY ");
636 else {
637 printf("%s%s%s", ISCON(q << TSHIFT) ? "const " : "",
638 ISVOL(q << TSHIFT) ? "volatile " : "", tnames[t]);
639 return;
640 }
641 }
642 }
643
644 /*
645 * Memory allocation routines.
646 * Memory are allocated from the system in MEMCHUNKSZ blocks.
647 * permalloc() returns a bunch of memory that is never freed.
648 * Memory allocated through tmpalloc() will be released the
649 * next time a function is ended (via tmpfree()).
650 */
651
652 #define MEMCHUNKSZ 8192 /* 8k per allocation */
653 struct balloc {
654 char a1;
655 union {
656 long long l;
657 long double d;
658 } a2;
659 };
660
661 #define ALIGNMENT offsetof(struct balloc, a2)
662 #define ROUNDUP(x) (((x) + ((ALIGNMENT)-1)) & ~((ALIGNMENT)-1))
663
664 static char *allocpole;
665 static size_t allocleft;
666 size_t permallocsize, tmpallocsize, lostmem;
667
668 void *
669 permalloc(size_t size)
670 {
671 void *rv;
672
673 if (size > MEMCHUNKSZ) {
674 if ((rv = malloc(size)) == NULL)
675 cerror("permalloc: missing %d bytes", size);
676 return rv;
677 }
678 if (size == 0)
679 cerror("permalloc2");
680 if (allocleft < size) {
681 /* loses unused bytes */
682 lostmem += allocleft;
683 if ((allocpole = malloc(MEMCHUNKSZ)) == NULL)
684 cerror("permalloc: out of memory");
685 allocleft = MEMCHUNKSZ;
686 }
687 size = ROUNDUP(size);
688 rv = &allocpole[MEMCHUNKSZ-allocleft];
689 allocleft -= size;
690 permallocsize += size;
691 return rv;
692 }
693
694 void *
695 tmpcalloc(size_t size)
696 {
697 void *rv;
698
699 rv = tmpalloc(size);
700 memset(rv, 0, size);
701 return rv;
702 }
703
704 /*
705 * Duplicate a string onto the temporary heap.
706 */
707 char *
708 tmpstrdup(char *str)
709 {
710 size_t len;
711
712 len = strlen(str) + 1;
713 return memcpy(tmpalloc(len), str, len);
714 }
715
716 /*
717 * Allocation routines for temporary memory.
718 */
719 #if 0
720 #define ALLDEBUG(x) printf x
721 #else
722 #define ALLDEBUG(x)
723 #endif
724
725 #define NELEM ((MEMCHUNKSZ-ROUNDUP(sizeof(struct xalloc *)))/ALIGNMENT)
726 #define ELEMSZ (ALIGNMENT)
727 #define MAXSZ (NELEM*ELEMSZ)
728 struct xalloc {
729 struct xalloc *next;
730 union {
731 struct balloc b; /* for initial alignment */
732 char elm[MAXSZ];
733 } u;
734 } *tapole, *tmpole;
735 size_t uselem = NELEM; /* next unused element */
736
737 void *
738 tmpalloc(size_t size)
739 {
740 struct xalloc *xp;
741 void *rv;
742 size_t nelem;
743
744 nelem = ROUNDUP(size)/ELEMSZ;
745 ALLDEBUG(("tmpalloc(%ld,%ld) %zd (%zd) ", ELEMSZ, NELEM, size, nelem));
746 if (nelem > NELEM/2) {
747 size += ROUNDUP(sizeof(struct xalloc *));
748 if ((xp = malloc(size)) == NULL)
749 cerror("out of memory");
750 ALLDEBUG(("XMEM! (%zd,%p) ", size, xp));
751 xp->next = tmpole;
752 tmpole = xp;
753 ALLDEBUG(("rv %p\n", &xp->u.elm[0]));
754 return &xp->u.elm[0];
755 }
756 if (nelem + uselem >= NELEM) {
757 ALLDEBUG(("MOREMEM! "));
758 /* alloc more */
759 if ((xp = malloc(sizeof(struct xalloc))) == NULL)
760 cerror("out of memory");
761 xp->next = tapole;
762 tapole = xp;
763 uselem = 0;
764 } else
765 xp = tapole;
766 rv = &xp->u.elm[uselem * ELEMSZ];
767 ALLDEBUG(("elemno %d ", uselem));
768 uselem += nelem;
769 ALLDEBUG(("new %d rv %p\n", uselem, rv));
770 return rv;
771 }
772
773 void
774 tmpfree(void)
775 {
776 struct xalloc *x1;
777
778 while (tmpole) {
779 x1 = tmpole;
780 tmpole = tmpole->next;
781 ALLDEBUG(("XMEM! free %p\n", x1));
782 free(x1);
783 }
784 while (tapole && tapole->next) {
785 x1 = tapole;
786 tapole = tapole->next;
787 ALLDEBUG(("MOREMEM! free %p\n", x1));
788 free(x1);
789 }
790 if (tapole)
791 uselem = 0;
792 }
793
794 /*
795 * Set a mark for later removal from the temp heap.
796 */
797 void
798 markset(struct mark *m)
799 {
800 m->tmsav = tmpole;
801 m->tasav = tapole;
802 m->elem = (int)uselem;
803 }
804
805 /*
806 * Remove everything on tmp heap from a mark.
807 */
808 void
809 markfree(struct mark *m)
810 {
811 struct xalloc *x1;
812
813 while (tmpole != m->tmsav) {
814 x1 = tmpole;
815 tmpole = tmpole->next;
816 free(x1);
817 }
818 while (tapole != m->tasav) {
819 x1 = tapole;
820 tapole = tapole->next;
821 free(x1);
822 }
823 uselem = m->elem;
824 }
825
826 /*
827 * Allocate space on the permanent stack for a string of length len+1
828 * and copy it there.
829 * Return the new address.
830 */
831 char *
832 newstring(char *s, size_t len)
833 {
834 char *u, *c;
835
836 len++;
837 savstringsz += (int)len;
838 if (allocleft < len) {
839 u = c = permalloc(len);
840 } else {
841 u = c = &allocpole[MEMCHUNKSZ-allocleft];
842 allocleft -= ROUNDUP(len);
843 permallocsize += ROUNDUP(len);
844 }
845 while (len--)
846 *c++ = *s++;
847 return u;
848 }
849
850 /*
851 * Do a preorder walk of the CM list p and apply function f on each element.
852 */
853 void
854 flist(NODE *p, void (*f)(NODE *, void *), void *arg)
855 {
856 if (p->n_op == CM) {
857 (*f)(p->n_right, arg);
858 flist(p->n_left, f, arg);
859 } else
860 (*f)(p, arg);
861 }
862
863 /*
864 * The same as flist but postorder.
865 */
866 void
867 listf(NODE *p, void (*f)(NODE *))
868 {
869 if (p->n_op == CM) {
870 listf(p->n_left, f);
871 (*f)(p->n_right);
872 } else
873 (*f)(p);
874 }
875
876 /*
877 * Get list argument number n from list, or NIL if out of list.
878 */
879 NODE *
880 listarg(NODE *p, int n, int *cnt)
881 {
882 NODE *r;
883
884 if (p->n_op == CM) {
885 r = listarg(p->n_left, n, cnt);
886 if (n == ++(*cnt))
887 r = p->n_right;
888 } else {
889 *cnt = 0;
890 r = n == 0 ? p : NIL;
891 }
892 return r;
893 }
894
895 /*
896 * Make a type unsigned, if possible.
897 */
898 TWORD
899 enunsign(TWORD t)
900 {
901 if (BTYPE(t) >= CHAR && BTYPE(t) <= ULONGLONG)
902 t |= 1;
903 return t;
904 }
905
906 /*
907 * Make a type signed, if possible.
908 */
909 TWORD
910 deunsign(TWORD t)
911 {
912 if (BTYPE(t) >= CHAR && BTYPE(t) <= ULONGLONG)
913 t &= ~1;
914 return t;
915 }
916
917 /*
918 * Attribute functions.
919 */
920 struct attr *
921 attr_new(int type, int nelem)
922 {
923 struct attr *ap;
924 size_t sz;
925
926 sz = sizeof(struct attr) + nelem * sizeof(union aarg);
927
928 ap = memset(permalloc(sz), 0, sz);
929 newattrsz += (int)sz;
930 ap->atype = type;
931 ap->sz = nelem;
932 return ap;
933 }
934
935 /*
936 * Add attribute list new before old and return new.
937 */
938 struct attr *
939 attr_add(struct attr *old, struct attr *new)
940 {
941 struct attr *ap;
942
943 if (new == NULL)
944 return old; /* nothing to add */
945
946 for (ap = new; ap->next; ap = ap->next)
947 ;
948 ap->next = old;
949 return new;
950 }
951
952 /*
953 * Search for attribute type in list ap. Return entry or NULL.
954 */
955 struct attr *
956 attr_find(struct attr *ap, int type)
957 {
958
959 for (; ap && ap->atype != type; ap = ap->next)
960 ;
961 return ap;
962 }
963
964 /*
965 * Copy an attribute struct.
966 * Return destination.
967 */
968 struct attr *
969 attr_copy(struct attr *aps, struct attr *apd, int n)
970 {
971 size_t sz = sizeof(struct attr) + n * sizeof(union aarg);
972 return memcpy(apd, aps, sz);
973 }
974
975 /*
976 * Duplicate an attribute, like strdup.
977 */
978 struct attr *
979 attr_dup(struct attr *ap)
980 {
981 size_t sz = sizeof(struct attr) + ap->sz * sizeof(union aarg);
982 ap = memcpy(permalloc(sz), ap, sz);
983 ap->next = NULL;
984 return ap;
985 }
986
987 void *
988 xmalloc(int size)
989 {
990 void *rv;
991
992 if ((rv = malloc(size)) == NULL)
993 cerror("out of memory!");
994 return rv;
995 }
996
997 void *
998 xstrdup(char *s)
999 {
1000 void *rv;
1001
1002 if ((rv = strdup(s)) == NULL)
1003 cerror("out of memory!");
1004 return rv;
1005 }
1006
1007 void *
1008 xcalloc(int a, int b)
1009 {
1010 void *rv;
1011
1012 if ((rv = calloc(a, b)) == NULL)
1013 cerror("out of memory!");
1014 return rv;
1015 }