Debian Codegraph @

cc/ccom/builtins.c

raw symbols SCIP
1 /* $Id: builtins.c,v 1.79 2021/10/08 15:59:07 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 # include "pass1.h"
28
29 #define ccopy p1tcopy
30
31 #ifndef MIN
32 #define MIN(a,b) (((a)<(b))?(a):(b))
33 #endif
34 #ifndef MAX
35 #define MAX(a,b) (((a)>(b))?(a):(b))
36 #endif
37
38 #ifndef NO_C_BUILTINS
39
40 extern int dimfuncnt;
41 /*
42 * replace an alloca function with direct allocation on stack.
43 * return a destination temp node.
44 */
45 static P1ND *
46 builtin_alloca(const struct bitable *bt, P1ND *a)
47 {
48 P1ND *t, *u;
49
50 #ifdef notyet
51 if (xnobuiltins)
52 return NULL;
53 #endif
54
55 t = tempnode(0, VOID|PTR, 0, 0);
56 u = tempnode(regno(t), VOID|PTR, 0, 0);
57 spalloc(t, a, SZCHAR);
58 return u;
59 }
60
61 /*
62 * Determine if a value is known to be constant at compile-time and
63 * hence that PCC can perform constant-folding on expressions involving
64 * that value.
65 */
66 static P1ND *
67 builtin_constant_p(const struct bitable *bt, P1ND *a)
68 {
69 P1ND *f;
70 int isconst;
71
72 p1walkf(a, putjops, 0);
73 for (f = a; f->n_op == COMOP; f = f->n_right)
74 ;
75 isconst = nncon(f);
76 p1tfree(a);
77 return bcon(isconst);
78 }
79
80 /*
81 * Hint to the compiler whether this expression will evaluate true or false.
82 * Just ignored for now.
83 */
84 static P1ND *
85 builtin_expect(const struct bitable *bt, P1ND *a)
86 {
87 P1ND *f;
88
89 if (a && a->n_op == CM) {
90 p1tfree(a->n_right);
91 f = a->n_left;
92 p1nfree(a);
93 a = f;
94 }
95
96 return a;
97 }
98
99 /*
100 * Take integer absolute value.
101 * Simply does: ((((x)>>(8*sizeof(x)-1))^(x))-((x)>>(8*sizeof(x)-1)))
102 */
103 static P1ND *
104 builtin_abs(const struct bitable *bt, P1ND *a)
105 {
106 P1ND *p, *q, *r, *t, *t2, *t3;
107 int tmp1, tmp2, shift;
108
109 if (a->n_type != INT)
110 a = cast(a, INT, 0);
111
112 if (a->n_op == ICON) {
113 if (glval(a) < 0)
114 slval(a, -glval(a));
115 p = a;
116 } else {
117 t = tempnode(0, a->n_type, a->n_df, a->n_ap);
118 tmp1 = regno(t);
119 p = buildtree(ASSIGN, t, a);
120
121 t = tempnode(tmp1, a->n_type, a->n_df, a->n_ap);
122 shift = (int)tsize(a->n_type, a->n_df, a->n_ap) - 1;
123 q = buildtree(RS, t, bcon(shift));
124
125 t2 = tempnode(0, a->n_type, a->n_df, a->n_ap);
126 tmp2 = regno(t2);
127 q = buildtree(ASSIGN, t2, q);
128
129 t = tempnode(tmp1, a->n_type, a->n_df, a->n_ap);
130 t2 = tempnode(tmp2, a->n_type, a->n_df, a->n_ap);
131 t3 = tempnode(tmp2, a->n_type, a->n_df, a->n_ap);
132 r = buildtree(MINUS, buildtree(ER, t, t2), t3);
133
134 p = buildtree(COMOP, p, buildtree(COMOP, q, r));
135 }
136
137 return p;
138 }
139
140 #define cmop(x,y) buildtree(COMOP, x, y)
141 #define lblnod(l) nlabel(l)
142
143 #ifndef TARGET_BSWAP
144 static P1ND *
145 builtin_bswap16(const struct bitable *bt, P1ND *a)
146 {
147 P1ND *f, *t1, *t2;
148
149 t1 = buildtree(LS, buildtree(AND, ccopy(a), bcon(255)), bcon(8));
150 t2 = buildtree(AND, buildtree(RS, a, bcon(8)), bcon(255));
151 f = buildtree(OR, t1, t2);
152 return f;
153 }
154
155 static P1ND *
156 builtin_bswap32(const struct bitable *bt, P1ND *a)
157 {
158 P1ND *f, *t1, *t2, *t3, *t4;
159
160 t1 = buildtree(LS, buildtree(AND, ccopy(a), bcon(255)), bcon(24));
161 t2 = buildtree(LS, buildtree(AND, ccopy(a), bcon(255 << 8)), bcon(8));
162 t3 = buildtree(AND, buildtree(RS, ccopy(a), bcon(8)), bcon(255 << 8));
163 t4 = buildtree(AND, buildtree(RS, a, bcon(24)), bcon(255));
164 f = buildtree(OR, buildtree(OR, t1, t2), buildtree(OR, t3, t4));
165 return f;
166 }
167
168 static P1ND *
169 builtin_bswap64(const struct bitable *bt, P1ND *a)
170 {
171 P1ND *f, *t1, *t2, *t3, *t4, *t5, *t6, *t7, *t8;
172
173 #define XBCON(x) xbcon(x, NULL, ctype(ULONGLONG))
174 t1 = buildtree(LS, buildtree(AND, ccopy(a), XBCON(255)), bcon(56));
175 t2 = buildtree(LS, buildtree(AND, ccopy(a), XBCON(255 << 8)), bcon(40));
176 t3 = buildtree(LS, buildtree(AND, ccopy(a), XBCON(255 << 16)), bcon(24));
177 t4 = buildtree(LS, buildtree(AND, ccopy(a), XBCON(255 << 24)), bcon(8));
178 t5 = buildtree(AND, buildtree(RS, ccopy(a), bcon(8)), XBCON(255 << 24));
179 t6 = buildtree(AND, buildtree(RS, ccopy(a), bcon(24)), XBCON(255 << 16));
180 t7 = buildtree(AND, buildtree(RS, ccopy(a), bcon(40)), XBCON(255 << 8));
181 t8 = buildtree(AND, buildtree(RS, a, bcon(56)), XBCON(255));
182 f = buildtree(OR,
183 buildtree(OR, buildtree(OR, t1, t2), buildtree(OR, t3, t4)),
184 buildtree(OR, buildtree(OR, t5, t6), buildtree(OR, t7, t8)));
185 return f;
186 #undef XBCON
187 }
188
189 #endif
190
191 #ifndef TARGET_CXZ
192 /*
193 * Find number of beginning 0's in a word of type t.
194 * t should be deunsigned.
195 */
196 static P1ND *
197 builtin_cxz(P1ND *a, TWORD t, int isclz)
198 {
199 P1ND *t101, *t102;
200 P1ND *rn, *p;
201 int l15, l16, l17;
202 int sz;
203
204 t = ctype(t);
205 sz = (int)tsize(t, 0, 0);
206
207 t101 = tempnode(0, INT, 0, 0);
208 t102 = tempnode(0, t, 0, 0);
209 l15 = getlab();
210 l16 = getlab();
211 l17 = getlab();
212 rn = buildtree(ASSIGN, ccopy(t102), a);
213 rn = cmop(rn, buildtree(ASSIGN, ccopy(t101), bcon(0)));
214 rn = cmop(rn, lblnod(l16));
215
216 p = buildtree(CBRANCH, buildtree(GE, ccopy(t101), bcon(sz)), bcon(l15));
217 rn = cmop(rn, p);
218 if (isclz) {
219 p = buildtree(CBRANCH,
220 buildtree(GE, ccopy(t102), bcon(0)), bcon(l17));
221 } else {
222 p = buildtree(CBRANCH,
223 buildtree(EQ, buildtree(AND, ccopy(t102), bcon(1)),
224 bcon(0)), bcon(l17));
225 }
226 rn = cmop(rn, p);
227
228 rn = cmop(rn, block(GOTO, bcon(l15), NULL, INT, 0, 0));
229
230 rn = cmop(rn, lblnod(l17));
231 rn = cmop(rn, buildtree(isclz ? LSEQ : RSEQ , t102, bcon(1)));
232
233 rn = cmop(rn, buildtree(INCR, ccopy(t101), bcon(1)));
234
235 rn = cmop(rn, block(GOTO, bcon(l16), NULL, INT, 0, 0));
236 rn = cmop(rn, lblnod(l15));
237 return cmop(rn, t101);
238 }
239
240 static P1ND *
241 builtin_clz(const struct bitable *bt, P1ND *a)
242 {
243 return builtin_cxz(a, INT, 1);
244 }
245
246 static P1ND *
247 builtin_clzl(const struct bitable *bt, P1ND *a)
248 {
249 return builtin_cxz(a, LONG, 1);
250 }
251
252 static P1ND *
253 builtin_clzll(const struct bitable *bt, P1ND *a)
254 {
255 return builtin_cxz(a, LONGLONG, 1);
256 }
257
258 static P1ND *
259 builtin_ctz(const struct bitable *bt, P1ND *a)
260 {
261 return builtin_cxz(a, INT, 0);
262 }
263
264 static P1ND *
265 builtin_ctzl(const struct bitable *bt, P1ND *a)
266 {
267 return builtin_cxz(a, LONG, 0);
268 }
269
270 static P1ND *
271 builtin_ctzll(const struct bitable *bt, P1ND *a)
272 {
273 return builtin_cxz(a, LONGLONG, 0);
274 }
275 #endif
276
277 #ifndef TARGET_ERA
278 static P1ND *
279 builtin_era(const struct bitable *bt, P1ND *a)
280 {
281 return a; /* Just pass through */
282 }
283 #endif
284
285 #ifndef TARGET_FFS
286 /*
287 * Find number of beginning 0's in a word of type t.
288 * t should be deunsigned.
289 */
290 static P1ND *
291 builtin_ff(P1ND *a, TWORD t)
292 {
293 P1ND *t101, *t102;
294 P1ND *rn, *p;
295 int l15, l16, l17;
296 int sz;
297
298 t = ctype(t);
299 sz = (int)tsize(t, 0, 0)+1;
300
301 t101 = tempnode(0, INT, 0, 0);
302 t102 = tempnode(0, t, 0, 0);
303 l15 = getlab();
304 l16 = getlab();
305 l17 = getlab();
306 rn = buildtree(ASSIGN, ccopy(t101), bcon(0));
307 rn = cmop(rn, buildtree(ASSIGN, ccopy(t102), a));
308
309 p = buildtree(CBRANCH, buildtree(EQ, ccopy(t102), bcon(0)), bcon(l15));
310 rn = cmop(rn, p);
311
312 rn = cmop(rn, buildtree(INCR, ccopy(t101), bcon(1)));
313
314 rn = cmop(rn, lblnod(l16));
315
316 p = buildtree(CBRANCH, buildtree(GE, ccopy(t101), bcon(sz)), bcon(l15));
317 rn = cmop(rn, p);
318
319 p = buildtree(CBRANCH,
320 buildtree(EQ, buildtree(AND, ccopy(t102), bcon(1)),
321 bcon(0)), bcon(l17));
322 rn = cmop(rn, p);
323
324 rn = cmop(rn, block(GOTO, bcon(l15), NULL, INT, 0, 0));
325
326 rn = cmop(rn, lblnod(l17));
327 rn = cmop(rn, buildtree(RSEQ, t102, bcon(1)));
328
329 rn = cmop(rn, buildtree(INCR, ccopy(t101), bcon(1)));
330
331 rn = cmop(rn, block(GOTO, bcon(l16), NULL, INT, 0, 0));
332 rn = cmop(rn, lblnod(l15));
333 return cmop(rn, t101);
334 }
335
336 static P1ND *
337 builtin_ffs(const struct bitable *bt, P1ND *a)
338 {
339 return builtin_ff(a, INT);
340 }
341
342 static P1ND *
343 builtin_ffsl(const struct bitable *bt, P1ND *a)
344 {
345 return builtin_ff(a, LONG);
346 }
347
348 static P1ND *
349 builtin_ffsll(const struct bitable *bt, P1ND *a)
350 {
351 return builtin_ff(a, LONGLONG);
352 }
353 #endif
354
355 static P1ND *
356 builtin_popcnt(P1ND *a, TWORD t)
357 {
358 P1ND *t101, *t102;
359 P1ND *rn, *p;
360 int l15, l16, l17;
361
362 t101 = tempnode(0, INT, 0, 0); /* counter for set bits */
363 t102 = tempnode(0, t, 0, 0); /* input data */
364 l15 = getlab();
365 l16 = getlab();
366 l17 = getlab();
367 rn = buildtree(ASSIGN, ccopy(t101), bcon(0)); /* bit counter set to 0 */
368 p = buildtree(ASSIGN, ccopy(t102), a); /* input data */
369 rn = cmop(rn, p);
370
371 rn = cmop(rn, lblnod(l15)); /* place a label here */
372
373 p = buildtree(CBRANCH,
374 buildtree(EQ, buildtree(AND, ccopy(t102), bcon(1)),
375 bcon(0)), bcon(l16)); /* if (t103 & 1) == 0, goto l16 */
376 rn = cmop(rn, p);
377
378 rn = cmop(rn, buildtree(INCR, ccopy(t101), bcon(1))); /* bit counter plus 1 */
379
380 rn = cmop(rn, lblnod(l16)); /* label */
381
382 rn = cmop(rn, buildtree(RSEQ, t102, bcon(1))); /* right-shift input data 1 position */
383
384 p = buildtree(CBRANCH, buildtree(EQ, ccopy(t102), bcon(0)), bcon(l17)); /* if all bits unset, goto l17 */
385 rn = cmop(rn, p);
386
387 rn = cmop(rn, block(GOTO, bcon(l15), NULL, INT, 0, 0)); /* go to the beginning of loop */
388
389 rn = cmop(rn, lblnod(l17)); /* label, loop exit here */
390 return cmop(rn, t101);
391 }
392
393 static P1ND *
394 builtin_popcount(const struct bitable *bt, P1ND *a)
395 {
396 return builtin_popcnt(a, UNSIGNED);
397 }
398
399 static P1ND *
400 builtin_popcountl(const struct bitable *bt, P1ND *a)
401 {
402 return builtin_popcnt(a, ULONG);
403 }
404
405 static P1ND *
406 builtin_popcountll(const struct bitable *bt, P1ND *a)
407 {
408 return builtin_popcnt(a, ULONGLONG);
409 }
410
411 /*
412 * Get size of object, if possible.
413 * 0 = whole object, 1 == closest object. Return -1 if not available.
414 * 2 == max of rem object, 3 == min of rem obj. Return 0 if not available.
415 */
416 static P1ND *
417 builtin_object_size(const struct bitable *bt, P1ND *a)
418 {
419 CONSZ v = icons(a->n_right);
420 int r;
421
422 if (v < 0 || v > 3)
423 uerror("arg2 must be between 0 and 3");
424 r = v < 2 ? -1 : 0;
425
426 a = p1nfree(a);
427 #ifdef notyet
428 if (ISPTR(a->n_type)) {
429 a = buildtree(UMUL, a, 0);
430 a = optloop(a);
431 a = doszof(a);
432 }
433 #else
434 p1walkf(a, putjops, 0); /* if ?: exists */
435 p1tfree(a);
436 #endif
437 return xbcon(r, NULL, bt->rt);
438 }
439
440 #ifndef TARGET_STDARGS
441 static P1ND *
442 builtin_stdarg_start(const struct bitable *bt, P1ND *a)
443 {
444 P1ND *p, *q;
445 int sz;
446
447 /* must first deal with argument size; use int size */
448 p = a->n_right;
449 if (p->n_type < INT) {
450 sz = (int)(SZINT/tsize(p->n_type, p->n_df, p->n_ap));
451 } else
452 sz = 1;
453
454 /* do the real job */
455 p = buildtree(ADDROF, p, NULL); /* address of last arg */
456 #ifdef STACK_DOWN
457 p = optim(buildtree(PLUS, p, bcon(sz))); /* add one to it (next arg) */
458 #else
459 p = optim(buildtree(MINUS, p, bcon(sz))); /* add one to it (next arg) */
460 #endif
461 q = block(NAME, NULL, NULL, PTR+VOID, 0, 0); /* create cast node */
462 q = buildtree(CAST, q, p); /* cast to void * (for assignment) */
463 p = q->n_right;
464 p1nfree(q->n_left);
465 p1nfree(q);
466 p = buildtree(ASSIGN, a->n_left, p); /* assign to ap */
467 p1nfree(a);
468 return p;
469 }
470
471 static P1ND *
472 builtin_va_arg(const struct bitable *bt, P1ND *a)
473 {
474 P1ND *p, *q, *r, *rv;
475 int sz, nodnum;
476
477 /* create a copy to a temp node of current ap */
478 p = ccopy(a->n_left);
479 q = tempnode(0, p->n_type, p->n_df, p->n_ap);
480 nodnum = regno(q);
481 rv = buildtree(ASSIGN, q, p);
482
483 r = a->n_right;
484 sz = (int)tsize(r->n_type, r->n_df, r->n_ap);
485 #ifdef MYVAARGSZ
486 SETOFF(sz, MYVAARGSZ);
487 #endif
488 sz /= SZCHAR;
489 /* add one to ap */
490 #ifdef STACK_DOWN
491 rv = buildtree(COMOP, rv , buildtree(PLUSEQ, a->n_left, bcon(sz)));
492 #else
493 ecomp(buildtree(MINUSEQ, a->n_left, bcon(sz)));
494 #endif
495
496 p1nfree(a->n_right);
497 p1nfree(a);
498 r = tempnode(nodnum, INCREF(r->n_type), r->n_df, r->n_ap);
499 return buildtree(COMOP, rv, buildtree(UMUL, r, NULL));
500
501 }
502
503 static P1ND *
504 builtin_va_end(const struct bitable *bt, P1ND *a)
505 {
506 return a; /* may have side effects */
507 }
508
509 static P1ND *
510 builtin_va_copy(const struct bitable *bt, P1ND *a)
511 {
512 P1ND *f;
513
514 f = buildtree(ASSIGN, a->n_left, a->n_right);
515 p1nfree(a);
516 return f;
517 }
518 #endif /* TARGET_STDARGS */
519
520 /*
521 * For unimplemented "builtin" functions, try to invoke the
522 * non-builtin name
523 */
524 static P1ND *
525 binhelp(P1ND *a, TWORD rt, char *n)
526 {
527 P1ND *f = block(NAME, NULL, NULL, INT, 0, 0);
528 int oblvl = blevel;
529
530 blevel = 0;
531 f->n_sp = lookup(addname(n), SNORMAL);
532 blevel = oblvl;
533 if (f->n_sp->sclass == SNULL) {
534 f->n_sp->sclass = EXTERN;
535 f->n_sp->stype = INCREF(rt)+(FTN-PTR);
536 f->n_sp->sdf = permalloc(sizeof(union dimfun));
537 dimfuncnt++;
538 f->n_sp->sdf->dfun = NULL;
539 }
540 f->n_type = f->n_sp->stype;
541 f = clocal(f);
542 return buildtree(CALL, f, a);
543 }
544
545 static P1ND *
546 builtin_unimp(const struct bitable *bt, P1ND *a)
547 {
548 return binhelp(a, bt->rt, &bt->name[10]);
549 }
550
551 #if 0
552 static P1ND *
553 builtin_unimp_f(P1ND *f, P1ND *a, TWORD rt)
554 {
555 return binhelp(f, a, rt, f->n_sp->sname);
556 }
557 #endif
558
559 #ifndef TARGET_PREFETCH
560 static P1ND *
561 builtin_prefetch(const struct bitable *bt, P1ND *a)
562 {
563 p1tfree(a);
564 return bcon(0);
565 }
566 #endif
567
568 /*
569 * check if compatible types.
570 * XXX - all enum are considered equal types
571 */
572 static P1ND *
573 builtin_tc(const struct bitable *bt, P1ND *a)
574 {
575 P1ND *p;
576
577 if (a == NULL || a->n_op != CM ||
578 a->n_left->n_op != TYPE || a->n_right->n_op != TYPE)
579 uerror("bad %s arg", bt->name);
580
581 p = bcon(a->n_left->n_type == a->n_right->n_type);
582 p1nfree(a->n_left);
583 p1nfree(a->n_right);
584 p1nfree(a);
585 return p;
586 }
587
588 static void
589 putinlbl(P1ND *p, void *arg)
590 {
591 if (p->n_op == COMOP && p->n_left->n_op == GOTO) {
592 int v = (int)glval(p->n_left->n_left);
593 send_passt(IP_DEFLAB, v+1);
594 }
595 }
596
597 /*
598 * Similar to ?:
599 */
600 static P1ND *
601 builtin_ce(const struct bitable *bt, P1ND *a)
602 {
603 P1ND *p;
604
605 if (a == NULL || a->n_op != CM ||
606 a->n_left->n_op != CM || a->n_left->n_left->n_op == CM)
607 uerror("bad %s arg", bt->name);
608 if (nncon(a->n_left->n_left) == 0)
609 uerror("arg not constant");
610 if (glval(a->n_left->n_left)) {
611 p = a->n_left->n_right;
612 a->n_left->n_op = UMUL; /* for p1tfree() */
613 p1walkf(a->n_right, putinlbl, 0);
614 } else {
615 p = a->n_right;
616 a->n_op = UMUL; /* for p1tfree() */
617 p1walkf(a->n_left->n_right, putinlbl, 0);
618 }
619 p1tfree(a);
620 return p;
621
622 }
623
624 static P1ND *
625 builtin_classify_type(const struct bitable *bt, P1ND *a)
626 {
627 TWORD t = a->n_type;
628 int rv;
629
630 if (t == BOOL)
631 rv = 4;
632 else if (t == CHAR || t == UCHAR)
633 rv = 2;
634 else if (t <= ULONGLONG)
635 rv = 1;
636 else if (t == STRTY)
637 rv = 12;
638 else if (t == UNIONTY)
639 rv = 13;
640 else if (ISPTR(t))
641 rv = 5;
642 else if (ISFTY(t))
643 rv = 8;
644 else if (ISFTN(t))
645 rv = 10;
646 else if (ISCTY(t))
647 rv = 9;
648 else
649 rv = -1;
650
651 p1tfree(a);
652 return bcon(rv);
653 }
654
655
656 #ifndef TARGET_ISMATH
657 /*
658 * Handle the builtin macros for the math functions is*
659 * To get something that is be somewhat generic assume that
660 * isnan() is a real function and that cast of a NaN type
661 * to double will still be a NaN.
662 */
663 static P1ND *
664 mtisnan(P1ND *p)
665 {
666
667 return binhelp(cast(ccopy(p), DOUBLE, 0), INT, "isnan");
668 }
669
670 static TWORD
671 mtcheck(P1ND *p)
672 {
673 TWORD t1 = p->n_left->n_type, t2 = p->n_right->n_type;
674
675 if ((t1 >= FLOAT && t1 <= LDOUBLE) ||
676 (t2 >= FLOAT && t2 <= LDOUBLE))
677 return MAX(t1, t2);
678 return 0;
679 }
680
681 static P1ND *
682 builtin_isunordered(const struct bitable *bt, P1ND *a)
683 {
684 P1ND *p;
685
686 if (mtcheck(a) == 0)
687 return bcon(0);
688
689 p = buildtree(OROR, mtisnan(a->n_left), mtisnan(a->n_right));
690 p1tfree(a);
691 return p;
692 }
693 static P1ND *
694 builtin_isany(P1ND *a, TWORD rt, int cmpt)
695 {
696 P1ND *p, *q;
697 TWORD t;
698
699 if ((t = mtcheck(a)) == 0)
700 return bcon(0);
701 p = buildtree(OROR, mtisnan(a->n_left), mtisnan(a->n_right));
702 p = buildtree(NOT, p, NULL);
703 q = buildtree(cmpt, cast(ccopy(a->n_left), t, 0),
704 cast(ccopy(a->n_right), t, 0));
705 p = buildtree(ANDAND, p, q);
706 p1tfree(a);
707 return p;
708 }
709 static P1ND *
710 builtin_isgreater(const struct bitable *bt, P1ND *a)
711 {
712 return builtin_isany(a, bt->rt, GT);
713 }
714 static P1ND *
715 builtin_isgreaterequal(const struct bitable *bt, P1ND *a)
716 {
717 return builtin_isany(a, bt->rt, GE);
718 }
719 static P1ND *
720 builtin_isless(const struct bitable *bt, P1ND *a)
721 {
722 return builtin_isany(a, bt->rt, LT);
723 }
724 static P1ND *
725 builtin_islessequal(const struct bitable *bt, P1ND *a)
726 {
727 return builtin_isany(a, bt->rt, LE);
728 }
729 static P1ND *
730 builtin_islessgreater(const struct bitable *bt, P1ND *a)
731 {
732 P1ND *p, *q, *r;
733 TWORD t;
734
735 if ((t = mtcheck(a)) == 0)
736 return bcon(0);
737 p = buildtree(OROR, mtisnan(a->n_left), mtisnan(a->n_right));
738 p = buildtree(NOT, p, NULL);
739 q = buildtree(GT, cast(ccopy(a->n_left), t, 0),
740 cast(ccopy(a->n_right), t, 0));
741 r = buildtree(LT, cast(ccopy(a->n_left), t, 0),
742 cast(ccopy(a->n_right), t, 0));
743 q = buildtree(OROR, q, r);
744 p = buildtree(ANDAND, p, q);
745 p1tfree(a);
746 return p;
747 }
748 #endif
749
750 #ifndef TARGET_SIGNBIT
751 static P1ND *
752 builtin_signbit(const struct bitable *bt, P1ND *a)
753 {
754 return binhelp(a, INT, "__signbitd");
755 }
756 static P1ND *
757 builtin_signbitf(const struct bitable *bt, P1ND *a)
758 {
759 return binhelp(a, INT, "__signbitf");
760 }
761 static P1ND *
762 builtin_signbitl(const struct bitable *bt, P1ND *a)
763 {
764 return binhelp(a, INT, "__signbitl");
765 }
766 #endif
767
768 static P1ND *
769 builtin_huge_val(const struct bitable *bt, P1ND *a)
770 {
771 P1ND *f = block(FCON, NULL, NULL, bt->rt, NULL, 0);
772
773 f->n_scon = sfallo();
774 soft_huge_val(f->n_scon);
775 return f;
776 }
777
778 /*
779 * Return NANs, if reasonable.
780 */
781 static P1ND *
782 builtin_nanx(const struct bitable *bt, P1ND *a)
783 {
784
785 if (a == NULL || a->n_op == CM) {
786 uerror("%s bad argument", bt->name);
787 a = bcon(0);
788 } else if (a->n_op == STRING && *a->n_name == '\0') {
789 p1nfree(a);
790 a = block(FCON, NULL, NULL, bt->rt, NULL, 0);
791 a->n_scon = sfallo();
792 soft_nan(a->n_scon, NULL);
793 } else
794 a = binhelp(eve(a), bt->rt, &bt->name[10]);
795 return a;
796 }
797
798 #ifndef NO_COMPLEX
799 static P1ND *
800 builtin_cir(const struct bitable *bt, P1ND *a)
801 {
802 char *n;
803
804 if (a == NULL || a->n_op == CM) {
805 uerror("wrong argument count to %s", bt->name);
806 return bcon(0);
807 }
808
809 n = addname(bt->name[1] == 'r' ? "__real" : "__imag");
810 return cast(structref(a, DOT, n), bt->rt, 0);
811 }
812
813 #endif
814
815 /*
816 * Target defines, to implement target versions of the generic builtins
817 */
818 #ifndef TARGET_MEMCMP
819 #define builtin_memcmp builtin_unimp
820 #endif
821 #ifndef TARGET_MEMCPY
822 #define builtin_memcpy builtin_unimp
823 #endif
824 #ifndef TARGET_MEMPCPY
825 #define builtin_mempcpy builtin_unimp
826 #endif
827 #ifndef TARGET_MEMSET
828 #define builtin_memset builtin_unimp
829 #endif
830
831 /* Reasonable type of size_t */
832 #ifndef SIZET
833 #if SZINT == SZSHORT
834 #define SIZET UNSIGNED
835 #elif SZLONG > SZINT
836 #define SIZET ULONG
837 #else
838 #define SIZET UNSIGNED
839 #endif
840 #endif
841
842 static TWORD memcpyt[] = { VOID|PTR, VOID|PTR, SIZET, INT };
843 static TWORD memsett[] = { VOID|PTR, INT, SIZET, INT };
844 static TWORD allocat[] = { SIZET };
845 static TWORD expectt[] = { LONG, LONG };
846 static TWORD strcmpt[] = { CHAR|PTR, CHAR|PTR };
847 static TWORD strcpyt[] = { CHAR|PTR, CHAR|PTR, INT };
848 static TWORD strncpyt[] = { CHAR|PTR, CHAR|PTR, SIZET, INT };
849 static TWORD strchrt[] = { CHAR|PTR, INT };
850 static TWORD strcspnt[] = { CHAR|PTR, CHAR|PTR };
851 static TWORD strspnt[] = { CHAR|PTR, CHAR|PTR };
852 static TWORD strpbrkt[] = { CHAR|PTR, CHAR|PTR };
853 static TWORD nant[] = { CHAR|PTR };
854 static TWORD bitt[] = { UNSIGNED };
855 static TWORD bsw16t[] = { USHORT };
856 static TWORD bitlt[] = { ULONG };
857 static TWORD bitllt[] = { ULONGLONG };
858 static TWORD abst[] = { INT };
859 static TWORD fmaxft[] = { FLOAT, FLOAT };
860 static TWORD fmaxt[] = { DOUBLE, DOUBLE };
861 static TWORD fmaxlt[] = { LDOUBLE, LDOUBLE };
862 static TWORD scalbnft[] = { FLOAT, INT };
863 static TWORD scalbnt[] = { DOUBLE, INT };
864 static TWORD scalbnlt[] = { LDOUBLE, INT };
865
866 static const struct bitable bitable[] = {
867 /* gnu universe only */
868 { "alloca", builtin_alloca, BTGNUONLY, 1, allocat, VOID|PTR },
869
870 #ifndef NO_COMPLEX
871 /* builtins for complex operations */
872 { "crealf", builtin_cir, BTNOPROTO, 1, 0, FLOAT },
873 { "creal", builtin_cir, BTNOPROTO, 1, 0, DOUBLE },
874 { "creall", builtin_cir, BTNOPROTO, 1, 0, LDOUBLE },
875 { "cimagf", builtin_cir, BTNOPROTO, 1, 0, FLOAT },
876 { "cimag", builtin_cir, BTNOPROTO, 1, 0, DOUBLE },
877 { "cimagl", builtin_cir, BTNOPROTO, 1, 0, LDOUBLE },
878 #endif
879 /* always existing builtins */
880 { "__builtin___memcpy_chk", builtin_unimp, 0, 4, memcpyt, VOID|PTR },
881 { "__builtin___mempcpy_chk", builtin_unimp, 0, 4, memcpyt, VOID|PTR },
882 { "__builtin___memmove_chk", builtin_unimp, 0, 4, memcpyt, VOID|PTR },
883 { "__builtin___memset_chk", builtin_unimp, 0, 4, memsett, VOID|PTR },
884
885 { "__builtin___strcat_chk", builtin_unimp, 0, 3, strcpyt, CHAR|PTR },
886 { "__builtin___strcpy_chk", builtin_unimp, 0, 3, strcpyt, CHAR|PTR },
887 { "__builtin___stpcpy_chk", builtin_unimp, 0, 3, strcpyt, CHAR|PTR },
888 { "__builtin___strncat_chk", builtin_unimp, 0, 4, strncpyt,CHAR|PTR },
889 { "__builtin___strncpy_chk", builtin_unimp, 0, 4, strncpyt,CHAR|PTR },
890 { "__builtin___stpncpy_chk", builtin_unimp, 0, 4, strncpyt,CHAR|PTR },
891
892 { "__builtin___printf_chk", builtin_unimp, BTNOPROTO, -1, 0, INT },
893 { "__builtin___fprintf_chk", builtin_unimp, BTNOPROTO, -1, 0, INT },
894 { "__builtin___sprintf_chk", builtin_unimp, BTNOPROTO, -1, 0, INT },
895 { "__builtin___snprintf_chk", builtin_unimp, BTNOPROTO, -1, 0, INT },
896 { "__builtin___vprintf_chk", builtin_unimp, BTNOPROTO, -1, 0, INT },
897 { "__builtin___vfprintf_chk", builtin_unimp, BTNOPROTO, -1, 0, INT },
898 { "__builtin___vsprintf_chk", builtin_unimp, BTNOPROTO, -1, 0, INT },
899 { "__builtin___vsnprintf_chk", builtin_unimp, BTNOPROTO, -1, 0, INT },
900
901 { "__builtin_alloca", builtin_alloca, 0, 1, allocat, VOID|PTR },
902 { "__builtin_abs", builtin_abs, 0, 1, abst, INT },
903 { "__builtin_bswap16", builtin_bswap16, 0, 1, bsw16t, USHORT },
904 { "__builtin_bswap32", builtin_bswap32, 0, 1, bitt, UNSIGNED },
905 { "__builtin_bswap64", builtin_bswap64, 0, 1, bitllt, ULONGLONG },
906 { "__builtin_choose_expr", builtin_ce, BTNOPROTO|BTNORVAL, 0, 0, 0 },
907 { "__builtin_clz", builtin_clz, 0, 1, bitt, INT },
908 { "__builtin_clzl", builtin_clzl, 0, 1, bitlt, INT },
909 { "__builtin_clzll", builtin_clzll, 0, 1, bitllt, INT },
910 { "__builtin_ctz", builtin_ctz, 0, 1, bitt, INT },
911 { "__builtin_ctzl", builtin_ctzl, 0, 1, bitlt, INT },
912 { "__builtin_ctzll", builtin_ctzll, 0, 1, bitllt, INT },
913 { "__builtin_extract_return_addr", builtin_era, 0, 1, memcpyt, VOID|PTR },
914 { "__builtin_ffs", builtin_ffs, 0, 1, bitt, INT },
915 { "__builtin_ffsl", builtin_ffsl, 0, 1, bitlt, INT },
916 { "__builtin_ffsll", builtin_ffsll, 0, 1, bitllt, INT },
917 { "__builtin_popcount", builtin_popcount, 0, 1, bitt, UNSIGNED },
918 { "__builtin_popcountl", builtin_popcountl, 0, 1, bitlt, ULONG },
919 { "__builtin_popcountll", builtin_popcountll, 0, 1, bitllt, ULONGLONG },
920
921 { "__builtin_classify_type", builtin_classify_type, 0, 1, 0, INT },
922 { "__builtin_constant_p", builtin_constant_p, 0, 1, 0, INT },
923 { "__builtin_copysignf", builtin_unimp, 0, 2, fmaxft, FLOAT },
924 { "__builtin_copysign", builtin_unimp, 0, 2, fmaxt, DOUBLE },
925 { "__builtin_copysignl", builtin_unimp, 0, 2, fmaxlt, LDOUBLE },
926 { "__builtin_expect", builtin_expect, 0, 2, expectt, LONG },
927 { "__builtin_memcmp", builtin_memcmp, 0, 3, memcpyt, INT },
928 { "__builtin_memcpy", builtin_memcpy, 0, 3, memcpyt, VOID|PTR },
929 { "__builtin_mempcpy", builtin_mempcpy, 0, 3, memcpyt, VOID|PTR },
930 { "__builtin_memset", builtin_memset, 0, 3, memsett, VOID|PTR },
931 { "__builtin_fabsf", builtin_unimp, 0, 1, fmaxft, FLOAT },
932 { "__builtin_fabs", builtin_unimp, 0, 1, fmaxt, DOUBLE },
933 { "__builtin_fabsl", builtin_unimp, 0, 1, fmaxlt, LDOUBLE },
934 { "__builtin_fmaxf", builtin_unimp, 0, 2, fmaxft, FLOAT },
935 { "__builtin_fmax", builtin_unimp, 0, 2, fmaxt, DOUBLE },
936 { "__builtin_fmaxl", builtin_unimp, 0, 2, fmaxlt, LDOUBLE },
937 { "__builtin_huge_valf", builtin_huge_val, 0, 0, 0, FLOAT },
938 { "__builtin_huge_val", builtin_huge_val, 0, 0, 0, DOUBLE },
939 { "__builtin_huge_vall", builtin_huge_val, 0, 0, 0, LDOUBLE },
940 { "__builtin_inff", builtin_huge_val, 0, 0, 0, FLOAT },
941 { "__builtin_inf", builtin_huge_val, 0, 0, 0, DOUBLE },
942 { "__builtin_infl", builtin_huge_val, 0, 0, 0, LDOUBLE },
943 { "__builtin_isgreater", builtin_isgreater, 0, 2, NULL, INT },
944 { "__builtin_isgreaterequal", builtin_isgreaterequal, 0, 2, NULL, INT },
945 { "__builtin_isinff", builtin_unimp, 0, 1, fmaxft, INT },
946 { "__builtin_isinf", builtin_unimp, 0, 1, fmaxt, INT },
947 { "__builtin_isinfl", builtin_unimp, 0, 1, fmaxlt, INT },
948 { "__builtin_isless", builtin_isless, 0, 2, NULL, INT },
949 { "__builtin_islessequal", builtin_islessequal, 0, 2, NULL, INT },
950 { "__builtin_islessgreater", builtin_islessgreater, 0, 2, NULL, INT },
951 { "__builtin_isnanf", builtin_unimp, 0, 1, fmaxft, INT },
952 { "__builtin_isnan", builtin_unimp, 0, 1, fmaxt, INT },
953 { "__builtin_isnanl", builtin_unimp, 0, 1, fmaxlt, INT },
954 { "__builtin_isunordered", builtin_isunordered, 0, 2, NULL, INT },
955 { "__builtin_logbf", builtin_unimp, 0, 1, fmaxft, FLOAT },
956 { "__builtin_logb", builtin_unimp, 0, 1, fmaxt, DOUBLE },
957 { "__builtin_logbl", builtin_unimp, 0, 1, fmaxlt, LDOUBLE },
958 { "__builtin_nanf", builtin_nanx, BTNOEVE, 1, nant, FLOAT },
959 { "__builtin_nan", builtin_nanx, BTNOEVE, 1, nant, DOUBLE },
960 { "__builtin_nanl", builtin_nanx, BTNOEVE, 1, nant, LDOUBLE },
961 { "__builtin_object_size", builtin_object_size, BTNOPROTO, 2, memsett, SIZET },
962 { "__builtin_prefetch", builtin_prefetch, 0, 1, memsett, VOID },
963 { "__builtin_scalbnf", builtin_unimp, 0, 2, scalbnft, FLOAT },
964 { "__builtin_scalbn", builtin_unimp, 0, 2, scalbnt, DOUBLE },
965 { "__builtin_scalbnl", builtin_unimp, 0, 2, scalbnlt, LDOUBLE },
966
967 { "__builtin_signbitf", builtin_signbitf, 0, 1, scalbnft, INT },
968 { "__builtin_signbit", builtin_signbit, 0, 1, scalbnt, INT },
969 { "__builtin_signbitl", builtin_signbitl, 0, 1, scalbnlt, INT },
970
971 { "__builtin_strcmp", builtin_unimp, 0, 2, strcmpt, INT },
972 { "__builtin_strcpy", builtin_unimp, 0, 2, strcpyt, CHAR|PTR },
973 { "__builtin_stpcpy", builtin_unimp, 0, 2, strcpyt, CHAR|PTR },
974 { "__builtin_strchr", builtin_unimp, 0, 2, strchrt, CHAR|PTR },
975 { "__builtin_strlen", builtin_unimp, 0, 1, strcmpt, SIZET },
976 { "__builtin_strrchr", builtin_unimp, 0, 2, strchrt, CHAR|PTR },
977 { "__builtin_strncpy", builtin_unimp, 0, 3, strncpyt, CHAR|PTR },
978 { "__builtin_strncat", builtin_unimp, 0, 3, strncpyt, CHAR|PTR },
979 { "__builtin_strcspn", builtin_unimp, 0, 2, strcspnt, SIZET },
980 { "__builtin_strspn", builtin_unimp, 0, 2, strspnt, SIZET },
981 { "__builtin_strstr", builtin_unimp, 0, 2, strcmpt, CHAR|PTR },
982 { "__builtin_strpbrk", builtin_unimp, 0, 2, strpbrkt, CHAR|PTR },
983 { "__builtin_types_compatible_p", builtin_tc, BTNOPROTO|BTNOEVE, 2, 0, INT },
984 #ifndef TARGET_STDARGS
985 { "__builtin_stdarg_start", builtin_stdarg_start, 0, 2, 0, VOID },
986 { "__builtin_va_start", builtin_stdarg_start, 0, 2, 0, VOID },
987 { "__builtin_va_arg", builtin_va_arg, BTNORVAL|BTNOPROTO, 2, 0, 0 },
988 { "__builtin_va_end", builtin_va_end, 0, 1, 0, VOID },
989 { "__builtin_va_copy", builtin_va_copy, 0, 2, 0, VOID },
990 #endif
991 { "__builtin_dwarf_cfa", builtin_cfa, 0, 0, 0, VOID|PTR },
992 { "__builtin_frame_address",
993 builtin_frame_address, 0, 1, bitt, VOID|PTR },
994 { "__builtin_return_address",
995 builtin_return_address, 0, 1, bitt, VOID|PTR },
996 #ifdef TARGET_BUILTINS
997 TARGET_BUILTINS
998 #endif
999 };
1000
1001 /*
1002 * Check and cast arguments for builtins.
1003 */
1004 static int
1005 acnt(P1ND *a, int narg, TWORD *tp)
1006 {
1007 P1ND *q;
1008 TWORD t;
1009
1010 if (a == NULL)
1011 return narg;
1012 for (; a->n_op == CM; a = a->n_left, narg--) {
1013 if (tp == NULL)
1014 continue;
1015 q = a->n_right;
1016 t = ctype(tp[narg-1]);
1017 if (q->n_type == t)
1018 continue;
1019 a->n_right = ccast(q, t, 0, NULL, 0);
1020 }
1021
1022 /* Last arg is ugly to deal with */
1023 if (narg == 1 && tp != NULL && a->n_type != tp[0]) {
1024 q = p1alloc();
1025 *q = *a;
1026 q = ccast(q, ctype(tp[0]), 0, NULL, 0);
1027 *a = *q;
1028 p1nfree(q);
1029 }
1030 return narg != 1;
1031 }
1032
1033 P1ND *
1034 builtin_check(struct symtab *sp, P1ND *a)
1035 {
1036 const struct bitable *bt;
1037
1038 if (sp->soffset < 0 ||
1039 sp->soffset >= (int)(sizeof(bitable)/sizeof(bitable[0])))
1040 cerror("builtin_check");
1041
1042 bt = &bitable[sp->soffset];
1043 if ((bt->flags & BTNOEVE) == 0 && a != NULL)
1044 a = eve(a);
1045 if (((bt->flags & BTNOPROTO) == 0) && acnt(a, bt->narg, bt->tp)) {
1046 uerror("wrong argument count to %s", bt->name);
1047 return bcon(0);
1048 }
1049 return (*bt->fun)(bt, a);
1050 }
1051
1052 /*
1053 * Put all builtin functions into the global symbol table.
1054 */
1055 void
1056 builtin_init()
1057 {
1058 const struct bitable *bt;
1059 P1ND *p = block(TYPE, 0, 0, 0, 0, 0);
1060 struct symtab *sp;
1061 int i, d_debug;
1062
1063 d_debug = ddebug;
1064 ddebug = 0;
1065 for (i = 0; i < (int)(sizeof(bitable)/sizeof(bitable[0])); i++) {
1066 bt = &bitable[i];
1067 if ((bt->flags & BTGNUONLY) && xgnu99 == 0 && xgnu89 == 0)
1068 continue; /* not in c99 universe, at least for now */
1069 sp = lookup(addname(bt->name), 0);
1070 if (bt->rt == 0 && (bt->flags & BTNORVAL) == 0)
1071 cerror("function '%s' has no return type", bt->name);
1072 p->n_type = INCREF(bt->rt) + (FTN-PTR);
1073 p->n_df = memset(permalloc(sizeof(union dimfun)), 0,
1074 sizeof(union dimfun));
1075 dimfuncnt++;
1076 p->n_sp = sp;
1077 defid(p, EXTERN);
1078 sp->soffset = i;
1079 sp->sflags |= SBUILTIN;
1080 }
1081 p1nfree(p);
1082 ddebug = d_debug;
1083 }
1084 #endif